xref: /freebsd/sys/netinet/tcp_stacks/rack.c (revision 382af4d38b62675f00f64275793a6b5fccfe62fa)
1 /*-
2  * Copyright (c) 2016-2020 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_inet.h"
29 #include "opt_inet6.h"
30 #include "opt_ipsec.h"
31 #include "opt_ratelimit.h"
32 #include "opt_kern_tls.h"
33 #if defined(INET) || defined(INET6)
34 #include <sys/param.h>
35 #include <sys/arb.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #ifdef TCP_HHOOK
39 #include <sys/hhook.h>
40 #endif
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>		/* for proc0 declaration */
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #ifdef STATS
52 #include <sys/qmath.h>
53 #include <sys/tree.h>
54 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
55 #else
56 #include <sys/tree.h>
57 #endif
58 #include <sys/refcount.h>
59 #include <sys/queue.h>
60 #include <sys/tim_filter.h>
61 #include <sys/smp.h>
62 #include <sys/kthread.h>
63 #include <sys/kern_prefetch.h>
64 #include <sys/protosw.h>
65 #ifdef TCP_ACCOUNTING
66 #include <sys/sched.h>
67 #include <machine/cpu.h>
68 #endif
69 #include <vm/uma.h>
70 
71 #include <net/route.h>
72 #include <net/route/nhop.h>
73 #include <net/vnet.h>
74 
75 #define TCPSTATES		/* for logging */
76 
77 #include <netinet/in.h>
78 #include <netinet/in_kdtrace.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
82 #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
83 #include <netinet/ip_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet/tcp.h>
88 #define	TCPOUTFLAGS
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcp_syncache.h>
95 #include <netinet/tcp_hpts.h>
96 #include <netinet/tcp_ratelimit.h>
97 #include <netinet/tcp_accounting.h>
98 #include <netinet/tcpip.h>
99 #include <netinet/cc/cc.h>
100 #include <netinet/cc/cc_newreno.h>
101 #include <netinet/tcp_fastopen.h>
102 #include <netinet/tcp_lro.h>
103 #ifdef NETFLIX_SHARED_CWND
104 #include <netinet/tcp_shared_cwnd.h>
105 #endif
106 #ifdef TCP_OFFLOAD
107 #include <netinet/tcp_offload.h>
108 #endif
109 #ifdef INET6
110 #include <netinet6/tcp6_var.h>
111 #endif
112 #include <netinet/tcp_ecn.h>
113 
114 #include <netipsec/ipsec_support.h>
115 
116 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #endif				/* IPSEC */
120 
121 #include <netinet/udp.h>
122 #include <netinet/udp_var.h>
123 #include <machine/in_cksum.h>
124 
125 #ifdef MAC
126 #include <security/mac/mac_framework.h>
127 #endif
128 #include "sack_filter.h"
129 #include "tcp_rack.h"
130 #include "tailq_hash.h"
131 #include "rack_bbr_common.h"
132 
133 uma_zone_t rack_zone;
134 uma_zone_t rack_pcb_zone;
135 
136 #ifndef TICKS2SBT
137 #define	TICKS2SBT(__t)	(tick_sbt * ((sbintime_t)(__t)))
138 #endif
139 
140 VNET_DECLARE(uint32_t, newreno_beta);
141 VNET_DECLARE(uint32_t, newreno_beta_ecn);
142 #define V_newreno_beta VNET(newreno_beta)
143 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
144 
145 #define	M_TCPFSB	__CONCAT(M_TCPFSB, STACKNAME)
146 #define	M_TCPDO		__CONCAT(M_TCPDO, STACKNAME)
147 
148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb_" __XSTRING(STACKNAME), "TCP fast send block");
149 MALLOC_DEFINE(M_TCPDO, "tcp_do_" __XSTRING(STACKNAME), "TCP deferred options");
150 MALLOC_DEFINE(M_TCPPCM, "tcp_pcm_" __XSTRING(STACKNAME), "TCP PCM measurement information");
151 
152 struct sysctl_ctx_list rack_sysctl_ctx;
153 struct sysctl_oid *rack_sysctl_root;
154 
155 #define CUM_ACKED 1
156 #define SACKED 2
157 
158 /*
159  * The RACK module incorporates a number of
160  * TCP ideas that have been put out into the IETF
161  * over the last few years:
162  * - Matt Mathis's Rate Halving which slowly drops
163  *    the congestion window so that the ack clock can
164  *    be maintained during a recovery.
165  * - Yuchung Cheng's RACK TCP (for which its named) that
166  *    will stop us using the number of dup acks and instead
167  *    use time as the gage of when we retransmit.
168  * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
169  *    of Dukkipati et.al.
170  * RACK depends on SACK, so if an endpoint arrives that
171  * cannot do SACK the state machine below will shuttle the
172  * connection back to using the "default" TCP stack that is
173  * in FreeBSD.
174  *
175  * To implement RACK the original TCP stack was first decomposed
176  * into a functional state machine with individual states
177  * for each of the possible TCP connection states. The do_segment
178  * functions role in life is to mandate the connection supports SACK
179  * initially and then assure that the RACK state matches the conenction
180  * state before calling the states do_segment function. Each
181  * state is simplified due to the fact that the original do_segment
182  * has been decomposed and we *know* what state we are in (no
183  * switches on the state) and all tests for SACK are gone. This
184  * greatly simplifies what each state does.
185  *
186  * TCP output is also over-written with a new version since it
187  * must maintain the new rack scoreboard.
188  *
189  */
190 static int32_t rack_tlp_thresh = 1;
191 static int32_t rack_tlp_limit = 2;	/* No more than 2 TLPs w-out new data */
192 static int32_t rack_tlp_use_greater = 1;
193 static int32_t rack_reorder_thresh = 2;
194 static int32_t rack_reorder_fade = 60000000;	/* 0 - never fade, def 60,000,000
195 						 * - 60 seconds */
196 static uint32_t rack_pcm_every_n_rounds = 100;
197 static uint32_t rack_pcm_blast = 0;
198 static uint32_t rack_pcm_is_enabled = 1;
199 static uint8_t rack_ssthresh_rest_rto_rec = 0; /* Do we restore ssthresh when we have rec -> rto -> rec */
200 
201 static uint32_t rack_gp_gain_req = 1200;		/* Amount percent wise required to gain to record a round has "gaining" */
202 static uint32_t rack_rnd_cnt_req = 0x10005;		/* Default number of rounds if we are below rack_gp_gain_req where we exit ss */
203 
204 
205 static int32_t rack_rxt_scoreboard_clear_thresh = 2;
206 static int32_t rack_dnd_default = 0;		/* For rr_conf = 3, what is the default for dnd */
207 static int32_t rack_rxt_controls = 0;
208 static int32_t rack_fill_cw_state = 0;
209 static uint8_t rack_req_measurements = 1;
210 /* Attack threshold detections */
211 static uint32_t rack_highest_sack_thresh_seen = 0;
212 static uint32_t rack_highest_move_thresh_seen = 0;
213 static uint32_t rack_merge_out_sacks_on_attack = 0;
214 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
215 static int32_t rack_hw_rate_caps = 0; /* 1; */
216 static int32_t rack_hw_rate_cap_per = 0;	/* 0 -- off  */
217 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
218 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
219 static int32_t rack_hw_up_only = 0;
220 static int32_t rack_stats_gets_ms_rtt = 1;
221 static int32_t rack_prr_addbackmax = 2;
222 static int32_t rack_do_hystart = 0;
223 static int32_t rack_apply_rtt_with_reduced_conf = 0;
224 static int32_t rack_hibeta_setting = 0;
225 static int32_t rack_default_pacing_divisor = 250;
226 static uint16_t rack_pacing_min_seg = 0;
227 static int32_t rack_timely_off = 0;
228 
229 static uint32_t sad_seg_size_per = 800;	/* 80.0 % */
230 static int32_t rack_pkt_delay = 1000;
231 static int32_t rack_send_a_lot_in_prr = 1;
232 static int32_t rack_min_to = 1000;	/* Number of microsecond  min timeout */
233 static int32_t rack_verbose_logging = 0;
234 static int32_t rack_ignore_data_after_close = 1;
235 static int32_t rack_enable_shared_cwnd = 1;
236 static int32_t rack_use_cmp_acks = 1;
237 static int32_t rack_use_fsb = 1;
238 static int32_t rack_use_rfo = 1;
239 static int32_t rack_use_rsm_rfo = 1;
240 static int32_t rack_max_abc_post_recovery = 2;
241 static int32_t rack_client_low_buf = 0;
242 static int32_t rack_dsack_std_based = 0x3;	/* bit field bit 1 sets rc_rack_tmr_std_based and bit 2 sets rc_rack_use_dsack */
243 static int32_t rack_bw_multipler = 0;		/* Limit on fill cw's jump up to be this x gp_est */
244 #ifdef TCP_ACCOUNTING
245 static int32_t rack_tcp_accounting = 0;
246 #endif
247 static int32_t rack_limits_scwnd = 1;
248 static int32_t rack_enable_mqueue_for_nonpaced = 0;
249 static int32_t rack_hybrid_allow_set_maxseg = 0;
250 static int32_t rack_disable_prr = 0;
251 static int32_t use_rack_rr = 1;
252 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
253 static int32_t rack_persist_min = 250000;	/* 250usec */
254 static int32_t rack_persist_max = 2000000;	/* 2 Second in usec's */
255 static int32_t rack_honors_hpts_min_to =  1;	/* Do we honor the hpts minimum time out for pacing timers */
256 static uint32_t rack_max_reduce = 10;		/* Percent we can reduce slot by */
257 static int32_t rack_sack_not_required = 1;	/* set to one to allow non-sack to use rack */
258 static int32_t rack_limit_time_with_srtt = 0;
259 static int32_t rack_autosndbuf_inc = 20;	/* In percentage form */
260 static int32_t rack_enobuf_hw_boost_mult = 0;	/* How many times the hw rate we boost slot using time_between */
261 static int32_t rack_enobuf_hw_max = 12000;	/* 12 ms in usecs */
262 static int32_t rack_enobuf_hw_min = 10000;	/* 10 ms in usecs */
263 static int32_t rack_hw_rwnd_factor = 2;		/* How many max_segs the rwnd must be before we hold off sending */
264 static int32_t rack_hw_check_queue = 0;		/* Do we always pre-check queue depth of a hw queue */
265 
266 /*
267  * Currently regular tcp has a rto_min of 30ms
268  * the backoff goes 12 times so that ends up
269  * being a total of 122.850 seconds before a
270  * connection is killed.
271  */
272 static uint32_t rack_def_data_window = 20;
273 static uint32_t rack_goal_bdp = 2;
274 static uint32_t rack_min_srtts = 1;
275 static uint32_t rack_min_measure_usec = 0;
276 static int32_t rack_tlp_min = 10000;	/* 10ms */
277 static int32_t rack_rto_min = 30000;	/* 30,000 usec same as main freebsd */
278 static int32_t rack_rto_max = 4000000;	/* 4 seconds in usec's */
279 static const int32_t rack_free_cache = 2;
280 static int32_t rack_hptsi_segments = 40;
281 static int32_t rack_rate_sample_method = USE_RTT_LOW;
282 static int32_t rack_pace_every_seg = 0;
283 static int32_t rack_delayed_ack_time = 40000;	/* 40ms in usecs */
284 static int32_t rack_slot_reduction = 4;
285 static int32_t rack_wma_divisor = 8;		/* For WMA calculation */
286 static int32_t rack_cwnd_block_ends_measure = 0;
287 static int32_t rack_rwnd_block_ends_measure = 0;
288 static int32_t rack_def_profile = 0;
289 
290 static int32_t rack_lower_cwnd_at_tlp = 0;
291 static int32_t rack_always_send_oldest = 0;
292 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
293 
294 static uint16_t rack_per_of_gp_ss = 250;	/* 250 % slow-start */
295 static uint16_t rack_per_of_gp_ca = 200;	/* 200 % congestion-avoidance */
296 static uint16_t rack_per_of_gp_rec = 200;	/* 200 % of bw */
297 
298 /* Probertt */
299 static uint16_t rack_per_of_gp_probertt = 60;	/* 60% of bw */
300 static uint16_t rack_per_of_gp_lowthresh = 40;	/* 40% is bottom */
301 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
302 static uint16_t rack_atexit_prtt_hbp = 130;	/* Clamp to 130% on exit prtt if highly buffered path */
303 static uint16_t rack_atexit_prtt = 130;	/* Clamp to 100% on exit prtt if non highly buffered path */
304 
305 static uint32_t rack_max_drain_wait = 2;	/* How man gp srtt's before we give up draining */
306 static uint32_t rack_must_drain = 1;		/* How many GP srtt's we *must* wait */
307 static uint32_t rack_probertt_use_min_rtt_entry = 1;	/* Use the min to calculate the goal else gp_srtt */
308 static uint32_t rack_probertt_use_min_rtt_exit = 0;
309 static uint32_t rack_probe_rtt_sets_cwnd = 0;
310 static uint32_t rack_probe_rtt_safety_val = 2000000;	/* No more than 2 sec in probe-rtt */
311 static uint32_t rack_time_between_probertt = 9600000;	/* 9.6 sec in usecs */
312 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;	/* How many srtt periods does probe-rtt last top fraction */
313 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;	/* How many srtt periods does probe-rtt last bottom fraction */
314 static uint32_t rack_min_probertt_hold = 40000;		/* Equal to delayed ack time */
315 static uint32_t rack_probertt_filter_life = 10000000;
316 static uint32_t rack_probertt_lower_within = 10;
317 static uint32_t rack_min_rtt_movement = 250000;	/* Must move at least 250ms (in microseconds)  to count as a lowering */
318 static int32_t rack_pace_one_seg = 0;		/* Shall we pace for less than 1.4Meg 1MSS at a time */
319 static int32_t rack_probertt_clear_is = 1;
320 static int32_t rack_max_drain_hbp = 1;		/* Extra drain times gpsrtt for highly buffered paths */
321 static int32_t rack_hbp_thresh = 3;		/* what is the divisor max_rtt/min_rtt to decided a hbp */
322 
323 /* Part of pacing */
324 static int32_t rack_max_per_above = 30;		/* When we go to increment stop if above 100+this% */
325 
326 /* Timely information:
327  *
328  * Here we have various control parameters on how
329  * timely may change the multiplier. rack_gain_p5_ub
330  * is associated with timely but not directly influencing
331  * the rate decision like the other variables. It controls
332  * the way fill-cw interacts with timely and caps how much
333  * timely can boost the fill-cw b/w.
334  *
335  * The other values are various boost/shrink numbers as well
336  * as potential caps when adjustments are made to the timely
337  * gain (returned by rack_get_output_gain(). Remember too that
338  * the gain returned can be overriden by other factors such as
339  * probeRTT as well as fixed-rate-pacing.
340  */
341 static int32_t rack_gain_p5_ub = 250;
342 static int32_t rack_gp_per_bw_mul_up = 2;	/* 2% */
343 static int32_t rack_gp_per_bw_mul_down = 4;	/* 4% */
344 static int32_t rack_gp_rtt_maxmul = 3;		/* 3 x maxmin */
345 static int32_t rack_gp_rtt_minmul = 1;		/* minrtt + (minrtt/mindiv) is lower rtt */
346 static int32_t rack_gp_rtt_mindiv = 4;		/* minrtt + (minrtt * minmul/mindiv) is lower rtt */
347 static int32_t rack_gp_decrease_per = 80;	/* Beta value of timely decrease (.8) = 80 */
348 static int32_t rack_gp_increase_per = 2;	/* 2% increase in multiplier */
349 static int32_t rack_per_lower_bound = 50;	/* Don't allow to drop below this multiplier */
350 static int32_t rack_per_upper_bound_ss = 0;	/* Don't allow SS to grow above this */
351 static int32_t rack_per_upper_bound_ca = 0;	/* Don't allow CA to grow above this */
352 static int32_t rack_do_dyn_mul = 0;		/* Are the rack gp multipliers dynamic */
353 static int32_t rack_gp_no_rec_chg = 1;		/* Prohibit recovery from reducing it's multiplier */
354 static int32_t rack_timely_dec_clear = 6;	/* Do we clear decrement count at a value (6)? */
355 static int32_t rack_timely_max_push_rise = 3;	/* One round of pushing */
356 static int32_t rack_timely_max_push_drop = 3;	/* Three round of pushing */
357 static int32_t rack_timely_min_segs = 4;	/* 4 segment minimum */
358 static int32_t rack_timely_no_stopping = 0;
359 static int32_t rack_down_raise_thresh = 100;
360 static int32_t rack_req_segs = 1;
361 static uint64_t rack_bw_rate_cap = 0;
362 static uint64_t rack_fillcw_bw_cap = 3750000;	/* Cap fillcw at 30Mbps */
363 
364 
365 /* Rack specific counters */
366 counter_u64_t rack_saw_enobuf;
367 counter_u64_t rack_saw_enobuf_hw;
368 counter_u64_t rack_saw_enetunreach;
369 counter_u64_t rack_persists_sends;
370 counter_u64_t rack_persists_acks;
371 counter_u64_t rack_persists_loss;
372 counter_u64_t rack_persists_lost_ends;
373 counter_u64_t rack_total_bytes;
374 #ifdef INVARIANTS
375 counter_u64_t rack_adjust_map_bw;
376 #endif
377 /* Tail loss probe counters */
378 counter_u64_t rack_tlp_tot;
379 counter_u64_t rack_tlp_newdata;
380 counter_u64_t rack_tlp_retran;
381 counter_u64_t rack_tlp_retran_bytes;
382 counter_u64_t rack_to_tot;
383 counter_u64_t rack_hot_alloc;
384 counter_u64_t rack_to_alloc;
385 counter_u64_t rack_to_alloc_hard;
386 counter_u64_t rack_to_alloc_emerg;
387 counter_u64_t rack_to_alloc_limited;
388 counter_u64_t rack_alloc_limited_conns;
389 counter_u64_t rack_split_limited;
390 counter_u64_t rack_rxt_clamps_cwnd;
391 counter_u64_t rack_rxt_clamps_cwnd_uniq;
392 
393 counter_u64_t rack_multi_single_eq;
394 counter_u64_t rack_proc_non_comp_ack;
395 
396 counter_u64_t rack_fto_send;
397 counter_u64_t rack_fto_rsm_send;
398 counter_u64_t rack_nfto_resend;
399 counter_u64_t rack_non_fto_send;
400 counter_u64_t rack_extended_rfo;
401 
402 counter_u64_t rack_sack_proc_all;
403 counter_u64_t rack_sack_proc_short;
404 counter_u64_t rack_sack_proc_restart;
405 counter_u64_t rack_sack_attacks_detected;
406 counter_u64_t rack_sack_attacks_reversed;
407 counter_u64_t rack_sack_attacks_suspect;
408 counter_u64_t rack_sack_used_next_merge;
409 counter_u64_t rack_sack_splits;
410 counter_u64_t rack_sack_used_prev_merge;
411 counter_u64_t rack_sack_skipped_acked;
412 counter_u64_t rack_ack_total;
413 counter_u64_t rack_express_sack;
414 counter_u64_t rack_sack_total;
415 counter_u64_t rack_move_none;
416 counter_u64_t rack_move_some;
417 
418 counter_u64_t rack_input_idle_reduces;
419 counter_u64_t rack_collapsed_win;
420 counter_u64_t rack_collapsed_win_seen;
421 counter_u64_t rack_collapsed_win_rxt;
422 counter_u64_t rack_collapsed_win_rxt_bytes;
423 counter_u64_t rack_try_scwnd;
424 counter_u64_t rack_hw_pace_init_fail;
425 counter_u64_t rack_hw_pace_lost;
426 
427 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
428 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
429 
430 
431 #define	RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
432 
433 #define	RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {	\
434 	(tv) = (value) + slop;	 \
435 	if ((u_long)(tv) < (u_long)(tvmin)) \
436 		(tv) = (tvmin); \
437 	if ((u_long)(tv) > (u_long)(tvmax)) \
438 		(tv) = (tvmax); \
439 } while (0)
440 
441 static void
442 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
443 
444 static int
445 rack_process_ack(struct mbuf *m, struct tcphdr *th,
446     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
447     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val, int32_t orig_tlen);
448 static int
449 rack_process_data(struct mbuf *m, struct tcphdr *th,
450     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
451     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
452 static void
453 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
454    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
455 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
456 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
457     uint8_t limit_type);
458 static struct rack_sendmap *
459 rack_check_recovery_mode(struct tcpcb *tp,
460     uint32_t tsused);
461 static uint32_t
462 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack);
463 static void
464 rack_cong_signal(struct tcpcb *tp,
465 		 uint32_t type, uint32_t ack, int );
466 static void rack_counter_destroy(void);
467 static int
468 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
469 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
470 static void
471 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
472 static void
473 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
474     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos);
475 static void rack_dtor(void *mem, int32_t size, void *arg);
476 static void
477 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
478     uint32_t flex1, uint32_t flex2,
479     uint32_t flex3, uint32_t flex4,
480     uint32_t flex5, uint32_t flex6,
481     uint16_t flex7, uint8_t mod);
482 
483 static void
484 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
485    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
486    struct rack_sendmap *rsm, uint8_t quality);
487 static struct rack_sendmap *
488 rack_find_high_nonack(struct tcp_rack *rack,
489     struct rack_sendmap *rsm);
490 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
491 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
492 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
493 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt);
494 static void
495 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
496 			    tcp_seq th_ack, int line, uint8_t quality);
497 static void
498 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm);
499 
500 static uint32_t
501 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
502 static int32_t rack_handoff_ok(struct tcpcb *tp);
503 static int32_t rack_init(struct tcpcb *tp, void **ptr);
504 static void rack_init_sysctls(void);
505 
506 static void
507 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
508     struct tcphdr *th, int entered_rec, int dup_ack_struck,
509     int *dsack_seen, int *sacks_seen);
510 static void
511 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
512     uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
513     struct rack_sendmap *hintrsm, uint32_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz);
514 
515 static uint64_t rack_get_gp_est(struct tcp_rack *rack);
516 
517 
518 static void
519 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
520     struct rack_sendmap *rsm, uint32_t cts);
521 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
522 static int32_t rack_output(struct tcpcb *tp);
523 
524 static uint32_t
525 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
526     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
527     uint32_t cts, uint32_t segsiz);
528 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
529 static void rack_remxt_tmr(struct tcpcb *tp);
530 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt);
531 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
532 static int32_t rack_stopall(struct tcpcb *tp);
533 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
534 static uint32_t
535 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
536     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint32_t add_flag, int segsiz);
537 static void
538 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
539     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz);
540 static int
541 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
542     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
543 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
544 static int
545 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
546     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
547     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
548 
549 static int
550 rack_do_closing(struct mbuf *m, struct tcphdr *th,
551     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
552     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
553 static int
554 rack_do_established(struct mbuf *m, struct tcphdr *th,
555     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
556     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
557 static int
558 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
559     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
560     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
561 static int
562 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
563     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
564     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
565 static int
566 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
567     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
568     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
569 static int
570 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
571     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
572     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
573 static int
574 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
575     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
576     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
577 static int
578 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
579     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
580     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
581 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts);
582 struct rack_sendmap *
583 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
584     uint32_t tsused);
585 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
586     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
587 static void
588      tcp_rack_partialack(struct tcpcb *tp);
589 static int
590 rack_set_profile(struct tcp_rack *rack, int prof);
591 static void
592 rack_apply_deferred_options(struct tcp_rack *rack);
593 
594 int32_t rack_clear_counter=0;
595 
596 static uint64_t
rack_get_lt_bw(struct tcp_rack * rack)597 rack_get_lt_bw(struct tcp_rack *rack)
598 {
599 	struct timeval tv;
600 	uint64_t tim, bytes;
601 
602 	tim = rack->r_ctl.lt_bw_time;
603 	bytes = rack->r_ctl.lt_bw_bytes;
604 	if (rack->lt_bw_up) {
605 		/* Include all the current bytes too */
606 		microuptime(&tv);
607 		bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq);
608 		tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
609 	}
610 	if ((bytes != 0) && (tim != 0))
611 		return ((bytes * (uint64_t)1000000) / tim);
612 	else
613 		return (0);
614 }
615 
616 static void
rack_swap_beta_values(struct tcp_rack * rack,uint8_t flex8)617 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8)
618 {
619 	struct sockopt sopt;
620 	struct cc_newreno_opts opt;
621 	struct tcpcb *tp;
622 	uint32_t old_beta;
623 	uint32_t old_beta_ecn;
624 	int error, failed = 0;
625 
626 	tp = rack->rc_tp;
627 	if (tp->t_cc == NULL) {
628 		/* Tcb is leaving */
629 		return;
630 	}
631 	rack->rc_pacing_cc_set = 1;
632 	if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
633 		/* Not new-reno we can't play games with beta! */
634 		failed = 1;
635 		goto out;
636 
637 	}
638 	if (CC_ALGO(tp)->ctl_output == NULL)  {
639 		/* Huh, not using new-reno so no swaps.? */
640 		failed = 2;
641 		goto out;
642 	}
643 	/* Get the current values out */
644 	sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
645 	sopt.sopt_dir = SOPT_GET;
646 	opt.name = CC_NEWRENO_BETA;
647 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
648 	if (error)  {
649 		failed = 3;
650 		goto out;
651 	}
652 	old_beta = opt.val;
653 	opt.name = CC_NEWRENO_BETA_ECN;
654 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
655 	if (error)  {
656 		failed = 4;
657 		goto out;
658 	}
659 	old_beta_ecn = opt.val;
660 
661 	/* Now lets set in the values we have stored */
662 	sopt.sopt_dir = SOPT_SET;
663 	opt.name = CC_NEWRENO_BETA;
664 	opt.val = rack->r_ctl.rc_saved_beta;
665 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
666 	if (error)  {
667 		failed = 5;
668 		goto out;
669 	}
670 	opt.name = CC_NEWRENO_BETA_ECN;
671 	opt.val = rack->r_ctl.rc_saved_beta_ecn;
672 	error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
673 	if (error) {
674 		failed = 6;
675 		goto out;
676 	}
677 	/* Save off the values for restoral */
678 	rack->r_ctl.rc_saved_beta = old_beta;
679 	rack->r_ctl.rc_saved_beta_ecn = old_beta_ecn;
680 out:
681 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
682 		union tcp_log_stackspecific log;
683 		struct timeval tv;
684 		struct newreno *ptr;
685 
686 		ptr = ((struct newreno *)tp->t_ccv.cc_data);
687 		memset(&log, 0, sizeof(log));
688 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
689 		log.u_bbr.flex1 = ptr->beta;
690 		log.u_bbr.flex2 = ptr->beta_ecn;
691 		log.u_bbr.flex3 = ptr->newreno_flags;
692 		log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta;
693 		log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta_ecn;
694 		log.u_bbr.flex6 = failed;
695 		log.u_bbr.flex7 = rack->gp_ready;
696 		log.u_bbr.flex7 <<= 1;
697 		log.u_bbr.flex7 |= rack->use_fixed_rate;
698 		log.u_bbr.flex7 <<= 1;
699 		log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
700 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
701 		log.u_bbr.flex8 = flex8;
702 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
703 			       0, &log, false, NULL, NULL, 0, &tv);
704 	}
705 }
706 
707 static void
rack_set_cc_pacing(struct tcp_rack * rack)708 rack_set_cc_pacing(struct tcp_rack *rack)
709 {
710 	if (rack->rc_pacing_cc_set)
711 		return;
712 	/*
713 	 * Use the swap utility placing in 3 for flex8 to id a
714 	 * set of a new set of values.
715 	 */
716 	rack->rc_pacing_cc_set = 1;
717 	rack_swap_beta_values(rack, 3);
718 }
719 
720 static void
rack_undo_cc_pacing(struct tcp_rack * rack)721 rack_undo_cc_pacing(struct tcp_rack *rack)
722 {
723 	if (rack->rc_pacing_cc_set == 0)
724 		return;
725 	/*
726 	 * Use the swap utility placing in 4 for flex8 to id a
727 	 * restoral of the old values.
728 	 */
729 	rack->rc_pacing_cc_set = 0;
730 	rack_swap_beta_values(rack, 4);
731 }
732 
733 static void
rack_remove_pacing(struct tcp_rack * rack)734 rack_remove_pacing(struct tcp_rack *rack)
735 {
736 	if (rack->rc_pacing_cc_set)
737 		rack_undo_cc_pacing(rack);
738 	if (rack->r_ctl.pacing_method & RACK_REG_PACING)
739 		tcp_decrement_paced_conn();
740 	if (rack->r_ctl.pacing_method & RACK_DGP_PACING)
741 		tcp_dec_dgp_pacing_cnt();
742 	rack->rc_always_pace = 0;
743 	rack->r_ctl.pacing_method = RACK_PACING_NONE;
744 	rack->dgp_on = 0;
745 	rack->rc_hybrid_mode = 0;
746 	rack->use_fixed_rate = 0;
747 }
748 
749 static void
rack_log_gpset(struct tcp_rack * rack,uint32_t seq_end,uint32_t ack_end_t,uint32_t send_end_t,int line,uint8_t mode,struct rack_sendmap * rsm)750 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t,
751 	       uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm)
752 {
753 	if (tcp_bblogging_on(rack->rc_tp) && (rack_verbose_logging != 0)) {
754 		union tcp_log_stackspecific log;
755 		struct timeval tv;
756 
757 		memset(&log, 0, sizeof(log));
758 		log.u_bbr.flex1 = seq_end;
759 		log.u_bbr.flex2 = rack->rc_tp->gput_seq;
760 		log.u_bbr.flex3 = ack_end_t;
761 		log.u_bbr.flex4 = rack->rc_tp->gput_ts;
762 		log.u_bbr.flex5 = send_end_t;
763 		log.u_bbr.flex6 = rack->rc_tp->gput_ack;
764 		log.u_bbr.flex7 = mode;
765 		log.u_bbr.flex8 = 69;
766 		log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts;
767 		log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts;
768 		log.u_bbr.pkts_out = line;
769 		log.u_bbr.cwnd_gain = rack->app_limited_needs_set;
770 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt;
771 		log.u_bbr.epoch = rack->r_ctl.current_round;
772 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
773 		if (rsm != NULL) {
774 			log.u_bbr.applimited = rsm->r_start;
775 			log.u_bbr.delivered = rsm->r_end;
776 			log.u_bbr.epoch = rsm->r_flags;
777 		}
778 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
779 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
780 		    &rack->rc_inp->inp_socket->so_rcv,
781 		    &rack->rc_inp->inp_socket->so_snd,
782 		    BBR_LOG_HPTSI_CALC, 0,
783 		    0, &log, false, &tv);
784 	}
785 }
786 
787 static int
sysctl_rack_clear(SYSCTL_HANDLER_ARGS)788 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
789 {
790 	uint32_t stat;
791 	int32_t error;
792 
793 	error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
794 	if (error || req->newptr == NULL)
795 		return error;
796 
797 	error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
798 	if (error)
799 		return (error);
800 	if (stat == 1) {
801 #ifdef INVARIANTS
802 		printf("Clearing RACK counters\n");
803 #endif
804 		counter_u64_zero(rack_tlp_tot);
805 		counter_u64_zero(rack_tlp_newdata);
806 		counter_u64_zero(rack_tlp_retran);
807 		counter_u64_zero(rack_tlp_retran_bytes);
808 		counter_u64_zero(rack_to_tot);
809 		counter_u64_zero(rack_saw_enobuf);
810 		counter_u64_zero(rack_saw_enobuf_hw);
811 		counter_u64_zero(rack_saw_enetunreach);
812 		counter_u64_zero(rack_persists_sends);
813 		counter_u64_zero(rack_total_bytes);
814 		counter_u64_zero(rack_persists_acks);
815 		counter_u64_zero(rack_persists_loss);
816 		counter_u64_zero(rack_persists_lost_ends);
817 #ifdef INVARIANTS
818 		counter_u64_zero(rack_adjust_map_bw);
819 #endif
820 		counter_u64_zero(rack_to_alloc_hard);
821 		counter_u64_zero(rack_to_alloc_emerg);
822 		counter_u64_zero(rack_sack_proc_all);
823 		counter_u64_zero(rack_fto_send);
824 		counter_u64_zero(rack_fto_rsm_send);
825 		counter_u64_zero(rack_extended_rfo);
826 		counter_u64_zero(rack_hw_pace_init_fail);
827 		counter_u64_zero(rack_hw_pace_lost);
828 		counter_u64_zero(rack_non_fto_send);
829 		counter_u64_zero(rack_nfto_resend);
830 		counter_u64_zero(rack_sack_proc_short);
831 		counter_u64_zero(rack_sack_proc_restart);
832 		counter_u64_zero(rack_to_alloc);
833 		counter_u64_zero(rack_to_alloc_limited);
834 		counter_u64_zero(rack_alloc_limited_conns);
835 		counter_u64_zero(rack_split_limited);
836 		counter_u64_zero(rack_rxt_clamps_cwnd);
837 		counter_u64_zero(rack_rxt_clamps_cwnd_uniq);
838 		counter_u64_zero(rack_multi_single_eq);
839 		counter_u64_zero(rack_proc_non_comp_ack);
840 		counter_u64_zero(rack_sack_attacks_detected);
841 		counter_u64_zero(rack_sack_attacks_reversed);
842 		counter_u64_zero(rack_sack_attacks_suspect);
843 		counter_u64_zero(rack_sack_used_next_merge);
844 		counter_u64_zero(rack_sack_used_prev_merge);
845 		counter_u64_zero(rack_sack_splits);
846 		counter_u64_zero(rack_sack_skipped_acked);
847 		counter_u64_zero(rack_ack_total);
848 		counter_u64_zero(rack_express_sack);
849 		counter_u64_zero(rack_sack_total);
850 		counter_u64_zero(rack_move_none);
851 		counter_u64_zero(rack_move_some);
852 		counter_u64_zero(rack_try_scwnd);
853 		counter_u64_zero(rack_collapsed_win);
854 		counter_u64_zero(rack_collapsed_win_rxt);
855 		counter_u64_zero(rack_collapsed_win_seen);
856 		counter_u64_zero(rack_collapsed_win_rxt_bytes);
857 	} else if (stat == 2) {
858 #ifdef INVARIANTS
859 		printf("Clearing RACK option array\n");
860 #endif
861 		COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE);
862 	} else if (stat == 3) {
863 		printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n");
864 	} else if (stat == 4) {
865 #ifdef INVARIANTS
866 		printf("Clearing RACK out size array\n");
867 #endif
868 		COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE);
869 	}
870 	rack_clear_counter = 0;
871 	return (0);
872 }
873 
874 static void
rack_init_sysctls(void)875 rack_init_sysctls(void)
876 {
877 	struct sysctl_oid *rack_counters;
878 	struct sysctl_oid *rack_attack;
879 	struct sysctl_oid *rack_pacing;
880 	struct sysctl_oid *rack_timely;
881 	struct sysctl_oid *rack_timers;
882 	struct sysctl_oid *rack_tlp;
883 	struct sysctl_oid *rack_misc;
884 	struct sysctl_oid *rack_features;
885 	struct sysctl_oid *rack_measure;
886 	struct sysctl_oid *rack_probertt;
887 	struct sysctl_oid *rack_hw_pacing;
888 
889 	rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
890 	    SYSCTL_CHILDREN(rack_sysctl_root),
891 	    OID_AUTO,
892 	    "sack_attack",
893 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
894 	    "Rack Sack Attack Counters and Controls");
895 	rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
896 	    SYSCTL_CHILDREN(rack_sysctl_root),
897 	    OID_AUTO,
898 	    "stats",
899 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
900 	    "Rack Counters");
901 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
902 	    SYSCTL_CHILDREN(rack_sysctl_root),
903 	    OID_AUTO, "rate_sample_method", CTLFLAG_RW,
904 	    &rack_rate_sample_method , USE_RTT_LOW,
905 	    "What method should we use for rate sampling 0=high, 1=low ");
906 	/* Probe rtt related controls */
907 	rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
908 	    SYSCTL_CHILDREN(rack_sysctl_root),
909 	    OID_AUTO,
910 	    "probertt",
911 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
912 	    "ProbeRTT related Controls");
913 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
914 	    SYSCTL_CHILDREN(rack_probertt),
915 	    OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
916 	    &rack_atexit_prtt_hbp, 130,
917 	    "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
918 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
919 	    SYSCTL_CHILDREN(rack_probertt),
920 	    OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
921 	    &rack_atexit_prtt, 130,
922 	    "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
923 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
924 	    SYSCTL_CHILDREN(rack_probertt),
925 	    OID_AUTO, "gp_per_mul", CTLFLAG_RW,
926 	    &rack_per_of_gp_probertt, 60,
927 	    "What percentage of goodput do we pace at in probertt");
928 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
929 	    SYSCTL_CHILDREN(rack_probertt),
930 	    OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
931 	    &rack_per_of_gp_probertt_reduce, 10,
932 	    "What percentage of goodput do we reduce every gp_srtt");
933 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
934 	    SYSCTL_CHILDREN(rack_probertt),
935 	    OID_AUTO, "gp_per_low", CTLFLAG_RW,
936 	    &rack_per_of_gp_lowthresh, 40,
937 	    "What percentage of goodput do we allow the multiplier to fall to");
938 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
939 	    SYSCTL_CHILDREN(rack_probertt),
940 	    OID_AUTO, "time_between", CTLFLAG_RW,
941 	    & rack_time_between_probertt, 96000000,
942 	    "How many useconds between the lowest rtt falling must past before we enter probertt");
943 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
944 	    SYSCTL_CHILDREN(rack_probertt),
945 	    OID_AUTO, "safety", CTLFLAG_RW,
946 	    &rack_probe_rtt_safety_val, 2000000,
947 	    "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
948 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
949 	    SYSCTL_CHILDREN(rack_probertt),
950 	    OID_AUTO, "sets_cwnd", CTLFLAG_RW,
951 	    &rack_probe_rtt_sets_cwnd, 0,
952 	    "Do we set the cwnd too (if always_lower is on)");
953 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
954 	    SYSCTL_CHILDREN(rack_probertt),
955 	    OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
956 	    &rack_max_drain_wait, 2,
957 	    "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
958 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
959 	    SYSCTL_CHILDREN(rack_probertt),
960 	    OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
961 	    &rack_must_drain, 1,
962 	    "We must drain this many gp_srtt's waiting for flight to reach goal");
963 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
964 	    SYSCTL_CHILDREN(rack_probertt),
965 	    OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
966 	    &rack_probertt_use_min_rtt_entry, 1,
967 	    "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
968 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
969 	    SYSCTL_CHILDREN(rack_probertt),
970 	    OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
971 	    &rack_probertt_use_min_rtt_exit, 0,
972 	    "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
973 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
974 	    SYSCTL_CHILDREN(rack_probertt),
975 	    OID_AUTO, "length_div", CTLFLAG_RW,
976 	    &rack_probertt_gpsrtt_cnt_div, 0,
977 	    "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
978 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
979 	    SYSCTL_CHILDREN(rack_probertt),
980 	    OID_AUTO, "length_mul", CTLFLAG_RW,
981 	    &rack_probertt_gpsrtt_cnt_mul, 0,
982 	    "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
983 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
984 	    SYSCTL_CHILDREN(rack_probertt),
985 	    OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
986 	    &rack_min_probertt_hold, 200000,
987 	    "What is the minimum time we hold probertt at target");
988 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
989 	    SYSCTL_CHILDREN(rack_probertt),
990 	    OID_AUTO, "filter_life", CTLFLAG_RW,
991 	    &rack_probertt_filter_life, 10000000,
992 	    "What is the time for the filters life in useconds");
993 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
994 	    SYSCTL_CHILDREN(rack_probertt),
995 	    OID_AUTO, "lower_within", CTLFLAG_RW,
996 	    &rack_probertt_lower_within, 10,
997 	    "If the rtt goes lower within this percentage of the time, go into probe-rtt");
998 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
999 	    SYSCTL_CHILDREN(rack_probertt),
1000 	    OID_AUTO, "must_move", CTLFLAG_RW,
1001 	    &rack_min_rtt_movement, 250,
1002 	    "How much is the minimum movement in rtt to count as a drop for probertt purposes");
1003 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1004 	    SYSCTL_CHILDREN(rack_probertt),
1005 	    OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
1006 	    &rack_probertt_clear_is, 1,
1007 	    "Do we clear I/S counts on exiting probe-rtt");
1008 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1009 	    SYSCTL_CHILDREN(rack_probertt),
1010 	    OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
1011 	    &rack_max_drain_hbp, 1,
1012 	    "How many extra drain gpsrtt's do we get in highly buffered paths");
1013 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1014 	    SYSCTL_CHILDREN(rack_probertt),
1015 	    OID_AUTO, "hbp_threshold", CTLFLAG_RW,
1016 	    &rack_hbp_thresh, 3,
1017 	    "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
1018 	/* Pacing related sysctls */
1019 	rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1020 	    SYSCTL_CHILDREN(rack_sysctl_root),
1021 	    OID_AUTO,
1022 	    "pacing",
1023 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1024 	    "Pacing related Controls");
1025 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1026 	    SYSCTL_CHILDREN(rack_pacing),
1027 	    OID_AUTO, "pcm_enabled", CTLFLAG_RW,
1028 	    &rack_pcm_is_enabled, 1,
1029 	    "Do we by default do PCM measurements?");
1030 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1031 	    SYSCTL_CHILDREN(rack_pacing),
1032 	    OID_AUTO, "pcm_rnds", CTLFLAG_RW,
1033 	    &rack_pcm_every_n_rounds, 100,
1034 	    "How many rounds before we need to do a PCM measurement");
1035 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1036 	    SYSCTL_CHILDREN(rack_pacing),
1037 	    OID_AUTO, "pcm_blast", CTLFLAG_RW,
1038 	    &rack_pcm_blast, 0,
1039 	    "Blast out the full cwnd/rwnd when doing a PCM measurement");
1040 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1041 	    SYSCTL_CHILDREN(rack_pacing),
1042 	    OID_AUTO, "rnd_gp_gain", CTLFLAG_RW,
1043 	    &rack_gp_gain_req, 1200,
1044 	    "How much do we have to increase the GP to record the round 1200 = 120.0");
1045 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1046 	    SYSCTL_CHILDREN(rack_pacing),
1047 	    OID_AUTO, "dgp_out_of_ss_at", CTLFLAG_RW,
1048 	    &rack_rnd_cnt_req, 0x10005,
1049 	    "How many rounds less than rnd_gp_gain will drop us out of SS");
1050 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1051 	    SYSCTL_CHILDREN(rack_pacing),
1052 	    OID_AUTO, "no_timely", CTLFLAG_RW,
1053 	    &rack_timely_off, 0,
1054 	    "Do we not use timely in DGP?");
1055 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1056 	    SYSCTL_CHILDREN(rack_pacing),
1057 	    OID_AUTO, "fillcw", CTLFLAG_RW,
1058 	    &rack_fill_cw_state, 0,
1059 	    "Enable fillcw on new connections (default=0 off)?");
1060 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1061 	    SYSCTL_CHILDREN(rack_pacing),
1062 	    OID_AUTO, "min_burst", CTLFLAG_RW,
1063 	    &rack_pacing_min_seg, 0,
1064 	    "What is the min burst size for pacing (0 disables)?");
1065 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1066 	    SYSCTL_CHILDREN(rack_pacing),
1067 	    OID_AUTO, "divisor", CTLFLAG_RW,
1068 	    &rack_default_pacing_divisor, 250,
1069 	    "What is the default divisor given to the rl code?");
1070 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1071 	    SYSCTL_CHILDREN(rack_pacing),
1072 	    OID_AUTO, "fillcw_max_mult", CTLFLAG_RW,
1073 	    &rack_bw_multipler, 0,
1074 	    "What is the limit multiplier of the current gp_est that fillcw can increase the b/w too, 200 == 200% (0 = off)?");
1075 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1076 	    SYSCTL_CHILDREN(rack_pacing),
1077 	    OID_AUTO, "max_pace_over", CTLFLAG_RW,
1078 	    &rack_max_per_above, 30,
1079 	    "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
1080 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1081 	    SYSCTL_CHILDREN(rack_pacing),
1082 	    OID_AUTO, "allow1mss", CTLFLAG_RW,
1083 	    &rack_pace_one_seg, 0,
1084 	    "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?");
1085 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1086 	    SYSCTL_CHILDREN(rack_pacing),
1087 	    OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
1088 	    &rack_limit_time_with_srtt, 0,
1089 	    "Do we limit pacing time based on srtt");
1090 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1091 	    SYSCTL_CHILDREN(rack_pacing),
1092 	    OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1093 	    &rack_per_of_gp_ss, 250,
1094 	    "If non zero, what percentage of goodput to pace at in slow start");
1095 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1096 	    SYSCTL_CHILDREN(rack_pacing),
1097 	    OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1098 	    &rack_per_of_gp_ca, 150,
1099 	    "If non zero, what percentage of goodput to pace at in congestion avoidance");
1100 	SYSCTL_ADD_U16(&rack_sysctl_ctx,
1101 	    SYSCTL_CHILDREN(rack_pacing),
1102 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1103 	    &rack_per_of_gp_rec, 200,
1104 	    "If non zero, what percentage of goodput to pace at in recovery");
1105 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1106 	    SYSCTL_CHILDREN(rack_pacing),
1107 	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1108 	    &rack_hptsi_segments, 40,
1109 	    "What size is the max for TSO segments in pacing and burst mitigation");
1110 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1111 	    SYSCTL_CHILDREN(rack_pacing),
1112 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
1113 	    &rack_slot_reduction, 4,
1114 	    "When doing only burst mitigation what is the reduce divisor");
1115 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1116 	    SYSCTL_CHILDREN(rack_sysctl_root),
1117 	    OID_AUTO, "use_pacing", CTLFLAG_RW,
1118 	    &rack_pace_every_seg, 0,
1119 	    "If set we use pacing, if clear we use only the original burst mitigation");
1120 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1121 	    SYSCTL_CHILDREN(rack_pacing),
1122 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1123 	    &rack_bw_rate_cap, 0,
1124 	    "If set we apply this value to the absolute rate cap used by pacing");
1125 	SYSCTL_ADD_U64(&rack_sysctl_ctx,
1126 	    SYSCTL_CHILDREN(rack_pacing),
1127 	    OID_AUTO, "fillcw_cap", CTLFLAG_RW,
1128 	    &rack_fillcw_bw_cap, 3750000,
1129 	    "Do we have an absolute cap on the amount of b/w fillcw can specify (0 = no)?");
1130 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1131 	    SYSCTL_CHILDREN(rack_sysctl_root),
1132 	    OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1133 	    &rack_req_measurements, 1,
1134 	    "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1135 	/* Hardware pacing */
1136 	rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1137 	    SYSCTL_CHILDREN(rack_sysctl_root),
1138 	    OID_AUTO,
1139 	    "hdwr_pacing",
1140 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1141 	    "Pacing related Controls");
1142 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1143 	    SYSCTL_CHILDREN(rack_hw_pacing),
1144 	    OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1145 	    &rack_hw_rwnd_factor, 2,
1146 	    "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1147 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1148 	    SYSCTL_CHILDREN(rack_hw_pacing),
1149 	    OID_AUTO, "precheck", CTLFLAG_RW,
1150 	    &rack_hw_check_queue, 0,
1151 	    "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?");
1152 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1153 	    SYSCTL_CHILDREN(rack_hw_pacing),
1154 	    OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1155 	    &rack_enobuf_hw_boost_mult, 0,
1156 	    "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1157 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1158 	    SYSCTL_CHILDREN(rack_hw_pacing),
1159 	    OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1160 	    &rack_enobuf_hw_max, 2,
1161 	    "What is the max boost the pacing time if we see a ENOBUFS?");
1162 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1163 	    SYSCTL_CHILDREN(rack_hw_pacing),
1164 	    OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1165 	    &rack_enobuf_hw_min, 2,
1166 	    "What is the min boost the pacing time if we see a ENOBUFS?");
1167 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1168 	    SYSCTL_CHILDREN(rack_hw_pacing),
1169 	    OID_AUTO, "enable", CTLFLAG_RW,
1170 	    &rack_enable_hw_pacing, 0,
1171 	    "Should RACK attempt to use hw pacing?");
1172 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1173 	    SYSCTL_CHILDREN(rack_hw_pacing),
1174 	    OID_AUTO, "rate_cap", CTLFLAG_RW,
1175 	    &rack_hw_rate_caps, 0,
1176 	    "Does the highest hardware pacing rate cap the rate we will send at??");
1177 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1178 	    SYSCTL_CHILDREN(rack_hw_pacing),
1179 	    OID_AUTO, "uncap_per", CTLFLAG_RW,
1180 	    &rack_hw_rate_cap_per, 0,
1181 	    "If you go over b/w by this amount you will be uncapped (0 = never)");
1182 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1183 	    SYSCTL_CHILDREN(rack_hw_pacing),
1184 	    OID_AUTO, "rate_min", CTLFLAG_RW,
1185 	    &rack_hw_rate_min, 0,
1186 	    "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1187 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1188 	    SYSCTL_CHILDREN(rack_hw_pacing),
1189 	    OID_AUTO, "rate_to_low", CTLFLAG_RW,
1190 	    &rack_hw_rate_to_low, 0,
1191 	    "If we fall below this rate, dis-engage hw pacing?");
1192 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1193 	    SYSCTL_CHILDREN(rack_hw_pacing),
1194 	    OID_AUTO, "up_only", CTLFLAG_RW,
1195 	    &rack_hw_up_only, 0,
1196 	    "Do we allow hw pacing to lower the rate selected?");
1197 	rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1198 	    SYSCTL_CHILDREN(rack_sysctl_root),
1199 	    OID_AUTO,
1200 	    "timely",
1201 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1202 	    "Rack Timely RTT Controls");
1203 	/* Timely based GP dynmics */
1204 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1205 	    SYSCTL_CHILDREN(rack_timely),
1206 	    OID_AUTO, "upper", CTLFLAG_RW,
1207 	    &rack_gp_per_bw_mul_up, 2,
1208 	    "Rack timely upper range for equal b/w (in percentage)");
1209 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1210 	    SYSCTL_CHILDREN(rack_timely),
1211 	    OID_AUTO, "lower", CTLFLAG_RW,
1212 	    &rack_gp_per_bw_mul_down, 4,
1213 	    "Rack timely lower range for equal b/w (in percentage)");
1214 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1215 	    SYSCTL_CHILDREN(rack_timely),
1216 	    OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1217 	    &rack_gp_rtt_maxmul, 3,
1218 	    "Rack timely multiplier of lowest rtt for rtt_max");
1219 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1220 	    SYSCTL_CHILDREN(rack_timely),
1221 	    OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1222 	    &rack_gp_rtt_mindiv, 4,
1223 	    "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1224 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1225 	    SYSCTL_CHILDREN(rack_timely),
1226 	    OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1227 	    &rack_gp_rtt_minmul, 1,
1228 	    "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1229 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1230 	    SYSCTL_CHILDREN(rack_timely),
1231 	    OID_AUTO, "decrease", CTLFLAG_RW,
1232 	    &rack_gp_decrease_per, 80,
1233 	    "Rack timely Beta value 80 = .8 (scaled by 100)");
1234 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1235 	    SYSCTL_CHILDREN(rack_timely),
1236 	    OID_AUTO, "increase", CTLFLAG_RW,
1237 	    &rack_gp_increase_per, 2,
1238 	    "Rack timely increase perentage of our GP multiplication factor");
1239 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1240 	    SYSCTL_CHILDREN(rack_timely),
1241 	    OID_AUTO, "lowerbound", CTLFLAG_RW,
1242 	    &rack_per_lower_bound, 50,
1243 	    "Rack timely lowest percentage we allow GP multiplier to fall to");
1244 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1245 	    SYSCTL_CHILDREN(rack_timely),
1246 	    OID_AUTO, "p5_upper", CTLFLAG_RW,
1247 	    &rack_gain_p5_ub, 250,
1248 	    "Profile 5 upper bound to timely gain");
1249 
1250 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1251 	    SYSCTL_CHILDREN(rack_timely),
1252 	    OID_AUTO, "upperboundss", CTLFLAG_RW,
1253 	    &rack_per_upper_bound_ss, 0,
1254 	    "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1255 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1256 	    SYSCTL_CHILDREN(rack_timely),
1257 	    OID_AUTO, "upperboundca", CTLFLAG_RW,
1258 	    &rack_per_upper_bound_ca, 0,
1259 	    "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1260 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1261 	    SYSCTL_CHILDREN(rack_timely),
1262 	    OID_AUTO, "dynamicgp", CTLFLAG_RW,
1263 	    &rack_do_dyn_mul, 0,
1264 	    "Rack timely do we enable dynmaic timely goodput by default");
1265 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1266 	    SYSCTL_CHILDREN(rack_timely),
1267 	    OID_AUTO, "no_rec_red", CTLFLAG_RW,
1268 	    &rack_gp_no_rec_chg, 1,
1269 	    "Rack timely do we prohibit the recovery multiplier from being lowered");
1270 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1271 	    SYSCTL_CHILDREN(rack_timely),
1272 	    OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1273 	    &rack_timely_dec_clear, 6,
1274 	    "Rack timely what threshold do we count to before another boost during b/w decent");
1275 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1276 	    SYSCTL_CHILDREN(rack_timely),
1277 	    OID_AUTO, "max_push_rise", CTLFLAG_RW,
1278 	    &rack_timely_max_push_rise, 3,
1279 	    "Rack timely how many times do we push up with b/w increase");
1280 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1281 	    SYSCTL_CHILDREN(rack_timely),
1282 	    OID_AUTO, "max_push_drop", CTLFLAG_RW,
1283 	    &rack_timely_max_push_drop, 3,
1284 	    "Rack timely how many times do we push back on b/w decent");
1285 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1286 	    SYSCTL_CHILDREN(rack_timely),
1287 	    OID_AUTO, "min_segs", CTLFLAG_RW,
1288 	    &rack_timely_min_segs, 4,
1289 	    "Rack timely when setting the cwnd what is the min num segments");
1290 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1291 	    SYSCTL_CHILDREN(rack_timely),
1292 	    OID_AUTO, "nonstop", CTLFLAG_RW,
1293 	    &rack_timely_no_stopping, 0,
1294 	    "Rack timely don't stop increase");
1295 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1296 	    SYSCTL_CHILDREN(rack_timely),
1297 	    OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1298 	    &rack_down_raise_thresh, 100,
1299 	    "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1300 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1301 	    SYSCTL_CHILDREN(rack_timely),
1302 	    OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1303 	    &rack_req_segs, 1,
1304 	    "Bottom dragging if not these many segments outstanding and room");
1305 
1306 	/* TLP and Rack related parameters */
1307 	rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1308 	    SYSCTL_CHILDREN(rack_sysctl_root),
1309 	    OID_AUTO,
1310 	    "tlp",
1311 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1312 	    "TLP and Rack related Controls");
1313 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1314 	    SYSCTL_CHILDREN(rack_tlp),
1315 	    OID_AUTO, "use_rrr", CTLFLAG_RW,
1316 	    &use_rack_rr, 1,
1317 	    "Do we use Rack Rapid Recovery");
1318 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1319 	    SYSCTL_CHILDREN(rack_tlp),
1320 	    OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1321 	    &rack_max_abc_post_recovery, 2,
1322 	    "Since we do early recovery, do we override the l_abc to a value, if so what?");
1323 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1324 	    SYSCTL_CHILDREN(rack_tlp),
1325 	    OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1326 	    &rack_non_rxt_use_cr, 0,
1327 	    "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1328 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1329 	    SYSCTL_CHILDREN(rack_tlp),
1330 	    OID_AUTO, "tlpmethod", CTLFLAG_RW,
1331 	    &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1332 	    "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1333 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1334 	    SYSCTL_CHILDREN(rack_tlp),
1335 	    OID_AUTO, "limit", CTLFLAG_RW,
1336 	    &rack_tlp_limit, 2,
1337 	    "How many TLP's can be sent without sending new data");
1338 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1339 	    SYSCTL_CHILDREN(rack_tlp),
1340 	    OID_AUTO, "use_greater", CTLFLAG_RW,
1341 	    &rack_tlp_use_greater, 1,
1342 	    "Should we use the rack_rtt time if its greater than srtt");
1343 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1344 	    SYSCTL_CHILDREN(rack_tlp),
1345 	    OID_AUTO, "tlpminto", CTLFLAG_RW,
1346 	    &rack_tlp_min, 10000,
1347 	    "TLP minimum timeout per the specification (in microseconds)");
1348 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1349 	    SYSCTL_CHILDREN(rack_tlp),
1350 	    OID_AUTO, "send_oldest", CTLFLAG_RW,
1351 	    &rack_always_send_oldest, 0,
1352 	    "Should we always send the oldest TLP and RACK-TLP");
1353 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1354 	    SYSCTL_CHILDREN(rack_tlp),
1355 	    OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1356 	    &rack_lower_cwnd_at_tlp, 0,
1357 	    "When a TLP completes a retran should we enter recovery");
1358 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1359 	    SYSCTL_CHILDREN(rack_tlp),
1360 	    OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1361 	    &rack_reorder_thresh, 2,
1362 	    "What factor for rack will be added when seeing reordering (shift right)");
1363 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1364 	    SYSCTL_CHILDREN(rack_tlp),
1365 	    OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1366 	    &rack_tlp_thresh, 1,
1367 	    "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1368 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1369 	    SYSCTL_CHILDREN(rack_tlp),
1370 	    OID_AUTO, "reorder_fade", CTLFLAG_RW,
1371 	    &rack_reorder_fade, 60000000,
1372 	    "Does reorder detection fade, if so how many microseconds (0 means never)");
1373 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1374 	    SYSCTL_CHILDREN(rack_tlp),
1375 	    OID_AUTO, "pktdelay", CTLFLAG_RW,
1376 	    &rack_pkt_delay, 1000,
1377 	    "Extra RACK time (in microseconds) besides reordering thresh");
1378 
1379 	/* Timer related controls */
1380 	rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1381 	    SYSCTL_CHILDREN(rack_sysctl_root),
1382 	    OID_AUTO,
1383 	    "timers",
1384 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1385 	    "Timer related controls");
1386 	SYSCTL_ADD_U8(&rack_sysctl_ctx,
1387 	    SYSCTL_CHILDREN(rack_timers),
1388 	    OID_AUTO, "reset_ssth_rec_rto", CTLFLAG_RW,
1389 	    &rack_ssthresh_rest_rto_rec, 0,
1390 	    "When doing recovery -> rto -> recovery do we reset SSthresh?");
1391 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1392 	    SYSCTL_CHILDREN(rack_timers),
1393 	    OID_AUTO, "scoreboard_thresh", CTLFLAG_RW,
1394 	    &rack_rxt_scoreboard_clear_thresh, 2,
1395 	    "How many RTO's are allowed before we clear the scoreboard");
1396 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1397 	    SYSCTL_CHILDREN(rack_timers),
1398 	    OID_AUTO, "honor_hpts_min", CTLFLAG_RW,
1399 	    &rack_honors_hpts_min_to, 1,
1400 	    "Do rack pacing timers honor hpts min timeout");
1401 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1402 	    SYSCTL_CHILDREN(rack_timers),
1403 	    OID_AUTO, "hpts_max_reduce", CTLFLAG_RW,
1404 	    &rack_max_reduce, 10,
1405 	    "Max percentage we will reduce slot by for pacing when we are behind");
1406 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1407 	    SYSCTL_CHILDREN(rack_timers),
1408 	    OID_AUTO, "persmin", CTLFLAG_RW,
1409 	    &rack_persist_min, 250000,
1410 	    "What is the minimum time in microseconds between persists");
1411 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1412 	    SYSCTL_CHILDREN(rack_timers),
1413 	    OID_AUTO, "persmax", CTLFLAG_RW,
1414 	    &rack_persist_max, 2000000,
1415 	    "What is the largest delay in microseconds between persists");
1416 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1417 	    SYSCTL_CHILDREN(rack_timers),
1418 	    OID_AUTO, "delayed_ack", CTLFLAG_RW,
1419 	    &rack_delayed_ack_time, 40000,
1420 	    "Delayed ack time (40ms in microseconds)");
1421 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1422 	    SYSCTL_CHILDREN(rack_timers),
1423 	    OID_AUTO, "minrto", CTLFLAG_RW,
1424 	    &rack_rto_min, 30000,
1425 	    "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1426 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1427 	    SYSCTL_CHILDREN(rack_timers),
1428 	    OID_AUTO, "maxrto", CTLFLAG_RW,
1429 	    &rack_rto_max, 4000000,
1430 	    "Maximum RTO in microseconds -- should be at least as large as min_rto");
1431 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1432 	    SYSCTL_CHILDREN(rack_timers),
1433 	    OID_AUTO, "minto", CTLFLAG_RW,
1434 	    &rack_min_to, 1000,
1435 	    "Minimum rack timeout in microseconds");
1436 	/* Measure controls */
1437 	rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1438 	    SYSCTL_CHILDREN(rack_sysctl_root),
1439 	    OID_AUTO,
1440 	    "measure",
1441 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1442 	    "Measure related controls");
1443 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1444 	    SYSCTL_CHILDREN(rack_measure),
1445 	    OID_AUTO, "wma_divisor", CTLFLAG_RW,
1446 	    &rack_wma_divisor, 8,
1447 	    "When doing b/w calculation what is the  divisor for the WMA");
1448 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1449 	    SYSCTL_CHILDREN(rack_measure),
1450 	    OID_AUTO, "end_cwnd", CTLFLAG_RW,
1451 	    &rack_cwnd_block_ends_measure, 0,
1452 	    "Does a cwnd just-return end the measurement window (app limited)");
1453 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1454 	    SYSCTL_CHILDREN(rack_measure),
1455 	    OID_AUTO, "end_rwnd", CTLFLAG_RW,
1456 	    &rack_rwnd_block_ends_measure, 0,
1457 	    "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1458 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1459 	    SYSCTL_CHILDREN(rack_measure),
1460 	    OID_AUTO, "min_target", CTLFLAG_RW,
1461 	    &rack_def_data_window, 20,
1462 	    "What is the minimum target window (in mss) for a GP measurements");
1463 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1464 	    SYSCTL_CHILDREN(rack_measure),
1465 	    OID_AUTO, "goal_bdp", CTLFLAG_RW,
1466 	    &rack_goal_bdp, 2,
1467 	    "What is the goal BDP to measure");
1468 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1469 	    SYSCTL_CHILDREN(rack_measure),
1470 	    OID_AUTO, "min_srtts", CTLFLAG_RW,
1471 	    &rack_min_srtts, 1,
1472 	    "What is the goal BDP to measure");
1473 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1474 	    SYSCTL_CHILDREN(rack_measure),
1475 	    OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1476 	    &rack_min_measure_usec, 0,
1477 	    "What is the Minimum time time for a measurement if 0, this is off");
1478 	/* Features */
1479 	rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1480 	    SYSCTL_CHILDREN(rack_sysctl_root),
1481 	    OID_AUTO,
1482 	    "features",
1483 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1484 	    "Feature controls");
1485 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1486 	    SYSCTL_CHILDREN(rack_features),
1487 	    OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW,
1488 	    &rack_hybrid_allow_set_maxseg, 0,
1489 	    "Should hybrid pacing allow the setmss command");
1490 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1491 	    SYSCTL_CHILDREN(rack_features),
1492 	    OID_AUTO, "cmpack", CTLFLAG_RW,
1493 	    &rack_use_cmp_acks, 1,
1494 	    "Should RACK have LRO send compressed acks");
1495 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1496 	    SYSCTL_CHILDREN(rack_features),
1497 	    OID_AUTO, "fsb", CTLFLAG_RW,
1498 	    &rack_use_fsb, 1,
1499 	    "Should RACK use the fast send block?");
1500 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1501 	    SYSCTL_CHILDREN(rack_features),
1502 	    OID_AUTO, "rfo", CTLFLAG_RW,
1503 	    &rack_use_rfo, 1,
1504 	    "Should RACK use rack_fast_output()?");
1505 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1506 	    SYSCTL_CHILDREN(rack_features),
1507 	    OID_AUTO, "rsmrfo", CTLFLAG_RW,
1508 	    &rack_use_rsm_rfo, 1,
1509 	    "Should RACK use rack_fast_rsm_output()?");
1510 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1511 	    SYSCTL_CHILDREN(rack_features),
1512 	    OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1513 	    &rack_enable_mqueue_for_nonpaced, 0,
1514 	    "Should RACK use mbuf queuing for non-paced connections");
1515 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1516 	    SYSCTL_CHILDREN(rack_features),
1517 	    OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1518 	    &rack_do_hystart, 0,
1519 	    "Should RACK enable HyStart++ on connections?");
1520 	/* Misc rack controls */
1521 	rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1522 	    SYSCTL_CHILDREN(rack_sysctl_root),
1523 	    OID_AUTO,
1524 	    "misc",
1525 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1526 	    "Misc related controls");
1527 #ifdef TCP_ACCOUNTING
1528 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1529 	    SYSCTL_CHILDREN(rack_misc),
1530 	    OID_AUTO, "tcp_acct", CTLFLAG_RW,
1531 	    &rack_tcp_accounting, 0,
1532 	    "Should we turn on TCP accounting for all rack sessions?");
1533 #endif
1534 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1535 	    SYSCTL_CHILDREN(rack_misc),
1536 	    OID_AUTO, "dnd", CTLFLAG_RW,
1537 	    &rack_dnd_default, 0,
1538 	    "Do not disturb default for rack_rrr = 3");
1539 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1540 	    SYSCTL_CHILDREN(rack_misc),
1541 	    OID_AUTO, "sad_seg_per", CTLFLAG_RW,
1542 	    &sad_seg_size_per, 800,
1543 	    "Percentage of segment size needed in a sack 800 = 80.0?");
1544 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1545 	    SYSCTL_CHILDREN(rack_misc),
1546 	    OID_AUTO, "rxt_controls", CTLFLAG_RW,
1547 	    &rack_rxt_controls, 0,
1548 	    "Retransmit sending size controls (valid  values 0, 1, 2 default=1)?");
1549 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1550 	    SYSCTL_CHILDREN(rack_misc),
1551 	    OID_AUTO, "rack_hibeta", CTLFLAG_RW,
1552 	    &rack_hibeta_setting, 0,
1553 	    "Do we ue a high beta (80 instead of 50)?");
1554 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1555 	    SYSCTL_CHILDREN(rack_misc),
1556 	    OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1557 	    &rack_apply_rtt_with_reduced_conf, 0,
1558 	    "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1559 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1560 	    SYSCTL_CHILDREN(rack_misc),
1561 	    OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1562 	    &rack_dsack_std_based, 3,
1563 	    "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1564 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1565 	    SYSCTL_CHILDREN(rack_misc),
1566 	    OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1567 	    &rack_prr_addbackmax, 2,
1568 	    "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1569 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1570 	    SYSCTL_CHILDREN(rack_misc),
1571 	    OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1572 	    &rack_stats_gets_ms_rtt, 1,
1573 	    "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1574 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1575 	    SYSCTL_CHILDREN(rack_misc),
1576 	    OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1577 	    &rack_client_low_buf, 0,
1578 	    "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1579 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1580 	    SYSCTL_CHILDREN(rack_misc),
1581 	    OID_AUTO, "defprofile", CTLFLAG_RW,
1582 	    &rack_def_profile, 0,
1583 	    "Should RACK use a default profile (0=no, num == profile num)?");
1584 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1585 	    SYSCTL_CHILDREN(rack_misc),
1586 	    OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1587 	    &rack_enable_shared_cwnd, 1,
1588 	    "Should RACK try to use the shared cwnd on connections where allowed");
1589 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1590 	    SYSCTL_CHILDREN(rack_misc),
1591 	    OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1592 	    &rack_limits_scwnd, 1,
1593 	    "Should RACK place low end time limits on the shared cwnd feature");
1594 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1595 	    SYSCTL_CHILDREN(rack_misc),
1596 	    OID_AUTO, "no_prr", CTLFLAG_RW,
1597 	    &rack_disable_prr, 0,
1598 	    "Should RACK not use prr and only pace (must have pacing on)");
1599 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1600 	    SYSCTL_CHILDREN(rack_misc),
1601 	    OID_AUTO, "bb_verbose", CTLFLAG_RW,
1602 	    &rack_verbose_logging, 0,
1603 	    "Should RACK black box logging be verbose");
1604 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1605 	    SYSCTL_CHILDREN(rack_misc),
1606 	    OID_AUTO, "data_after_close", CTLFLAG_RW,
1607 	    &rack_ignore_data_after_close, 1,
1608 	    "Do we hold off sending a RST until all pending data is ack'd");
1609 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1610 	    SYSCTL_CHILDREN(rack_misc),
1611 	    OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1612 	    &rack_sack_not_required, 1,
1613 	    "Do we allow rack to run on connections not supporting SACK");
1614 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1615 	    SYSCTL_CHILDREN(rack_misc),
1616 	    OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1617 	    &rack_send_a_lot_in_prr, 1,
1618 	    "Send a lot in prr");
1619 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
1620 	    SYSCTL_CHILDREN(rack_misc),
1621 	    OID_AUTO, "autoscale", CTLFLAG_RW,
1622 	    &rack_autosndbuf_inc, 20,
1623 	    "What percentage should rack scale up its snd buffer by?");
1624 
1625 
1626 	/* Sack Attacker detection stuff */
1627 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1628 	    SYSCTL_CHILDREN(rack_attack),
1629 	    OID_AUTO, "merge_out", CTLFLAG_RW,
1630 	    &rack_merge_out_sacks_on_attack, 0,
1631 	    "Do we merge the sendmap when we decide we are being attacked?");
1632 
1633 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1634 	    SYSCTL_CHILDREN(rack_attack),
1635 	    OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1636 	    &rack_highest_sack_thresh_seen, 0,
1637 	    "Highest sack to ack ratio seen");
1638 	SYSCTL_ADD_U32(&rack_sysctl_ctx,
1639 	    SYSCTL_CHILDREN(rack_attack),
1640 	    OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1641 	    &rack_highest_move_thresh_seen, 0,
1642 	    "Highest move to non-move ratio seen");
1643 	rack_ack_total = counter_u64_alloc(M_WAITOK);
1644 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1645 	    SYSCTL_CHILDREN(rack_attack),
1646 	    OID_AUTO, "acktotal", CTLFLAG_RD,
1647 	    &rack_ack_total,
1648 	    "Total number of Ack's");
1649 	rack_express_sack = counter_u64_alloc(M_WAITOK);
1650 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1651 	    SYSCTL_CHILDREN(rack_attack),
1652 	    OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1653 	    &rack_express_sack,
1654 	    "Total expresss number of Sack's");
1655 	rack_sack_total = counter_u64_alloc(M_WAITOK);
1656 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1657 	    SYSCTL_CHILDREN(rack_attack),
1658 	    OID_AUTO, "sacktotal", CTLFLAG_RD,
1659 	    &rack_sack_total,
1660 	    "Total number of SACKs");
1661 	rack_move_none = counter_u64_alloc(M_WAITOK);
1662 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1663 	    SYSCTL_CHILDREN(rack_attack),
1664 	    OID_AUTO, "move_none", CTLFLAG_RD,
1665 	    &rack_move_none,
1666 	    "Total number of SACK index reuse of positions under threshold");
1667 	rack_move_some = counter_u64_alloc(M_WAITOK);
1668 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1669 	    SYSCTL_CHILDREN(rack_attack),
1670 	    OID_AUTO, "move_some", CTLFLAG_RD,
1671 	    &rack_move_some,
1672 	    "Total number of SACK index reuse of positions over threshold");
1673 	rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1674 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1675 	    SYSCTL_CHILDREN(rack_attack),
1676 	    OID_AUTO, "attacks", CTLFLAG_RD,
1677 	    &rack_sack_attacks_detected,
1678 	    "Total number of SACK attackers that had sack disabled");
1679 	rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1680 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1681 	    SYSCTL_CHILDREN(rack_attack),
1682 	    OID_AUTO, "reversed", CTLFLAG_RD,
1683 	    &rack_sack_attacks_reversed,
1684 	    "Total number of SACK attackers that were later determined false positive");
1685 	rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK);
1686 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1687 	    SYSCTL_CHILDREN(rack_attack),
1688 	    OID_AUTO, "suspect", CTLFLAG_RD,
1689 	    &rack_sack_attacks_suspect,
1690 	    "Total number of SACKs that triggered early detection");
1691 
1692 	rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1693 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1694 	    SYSCTL_CHILDREN(rack_attack),
1695 	    OID_AUTO, "nextmerge", CTLFLAG_RD,
1696 	    &rack_sack_used_next_merge,
1697 	    "Total number of times we used the next merge");
1698 	rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1699 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1700 	    SYSCTL_CHILDREN(rack_attack),
1701 	    OID_AUTO, "prevmerge", CTLFLAG_RD,
1702 	    &rack_sack_used_prev_merge,
1703 	    "Total number of times we used the prev merge");
1704 	/* Counters */
1705 	rack_total_bytes = counter_u64_alloc(M_WAITOK);
1706 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1707 	    SYSCTL_CHILDREN(rack_counters),
1708 	    OID_AUTO, "totalbytes", CTLFLAG_RD,
1709 	    &rack_total_bytes,
1710 	    "Total number of bytes sent");
1711 	rack_fto_send = counter_u64_alloc(M_WAITOK);
1712 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1713 	    SYSCTL_CHILDREN(rack_counters),
1714 	    OID_AUTO, "fto_send", CTLFLAG_RD,
1715 	    &rack_fto_send, "Total number of rack_fast_output sends");
1716 	rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1717 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1718 	    SYSCTL_CHILDREN(rack_counters),
1719 	    OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1720 	    &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1721 	rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1722 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1723 	    SYSCTL_CHILDREN(rack_counters),
1724 	    OID_AUTO, "nfto_resend", CTLFLAG_RD,
1725 	    &rack_nfto_resend, "Total number of rack_output retransmissions");
1726 	rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1727 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1728 	    SYSCTL_CHILDREN(rack_counters),
1729 	    OID_AUTO, "nfto_send", CTLFLAG_RD,
1730 	    &rack_non_fto_send, "Total number of rack_output first sends");
1731 	rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1732 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1733 	    SYSCTL_CHILDREN(rack_counters),
1734 	    OID_AUTO, "rfo_extended", CTLFLAG_RD,
1735 	    &rack_extended_rfo, "Total number of times we extended rfo");
1736 
1737 	rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1738 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1739 	    SYSCTL_CHILDREN(rack_counters),
1740 	    OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1741 	    &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1742 	rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1743 
1744 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1745 	    SYSCTL_CHILDREN(rack_counters),
1746 	    OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1747 	    &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1748 	rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1749 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1750 	    SYSCTL_CHILDREN(rack_counters),
1751 	    OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1752 	    &rack_tlp_tot,
1753 	    "Total number of tail loss probe expirations");
1754 	rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1755 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1756 	    SYSCTL_CHILDREN(rack_counters),
1757 	    OID_AUTO, "tlp_new", CTLFLAG_RD,
1758 	    &rack_tlp_newdata,
1759 	    "Total number of tail loss probe sending new data");
1760 	rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1761 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1762 	    SYSCTL_CHILDREN(rack_counters),
1763 	    OID_AUTO, "tlp_retran", CTLFLAG_RD,
1764 	    &rack_tlp_retran,
1765 	    "Total number of tail loss probe sending retransmitted data");
1766 	rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1767 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1768 	    SYSCTL_CHILDREN(rack_counters),
1769 	    OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1770 	    &rack_tlp_retran_bytes,
1771 	    "Total bytes of tail loss probe sending retransmitted data");
1772 	rack_to_tot = counter_u64_alloc(M_WAITOK);
1773 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1774 	    SYSCTL_CHILDREN(rack_counters),
1775 	    OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1776 	    &rack_to_tot,
1777 	    "Total number of times the rack to expired");
1778 	rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1779 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1780 	    SYSCTL_CHILDREN(rack_counters),
1781 	    OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1782 	    &rack_saw_enobuf,
1783 	    "Total number of times a sends returned enobuf for non-hdwr paced connections");
1784 	rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1785 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1786 	    SYSCTL_CHILDREN(rack_counters),
1787 	    OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1788 	    &rack_saw_enobuf_hw,
1789 	    "Total number of times a send returned enobuf for hdwr paced connections");
1790 	rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1791 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1792 	    SYSCTL_CHILDREN(rack_counters),
1793 	    OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1794 	    &rack_saw_enetunreach,
1795 	    "Total number of times a send received a enetunreachable");
1796 	rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1797 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1798 	    SYSCTL_CHILDREN(rack_counters),
1799 	    OID_AUTO, "alloc_hot", CTLFLAG_RD,
1800 	    &rack_hot_alloc,
1801 	    "Total allocations from the top of our list");
1802 	rack_to_alloc = counter_u64_alloc(M_WAITOK);
1803 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1804 	    SYSCTL_CHILDREN(rack_counters),
1805 	    OID_AUTO, "allocs", CTLFLAG_RD,
1806 	    &rack_to_alloc,
1807 	    "Total allocations of tracking structures");
1808 	rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1809 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1810 	    SYSCTL_CHILDREN(rack_counters),
1811 	    OID_AUTO, "allochard", CTLFLAG_RD,
1812 	    &rack_to_alloc_hard,
1813 	    "Total allocations done with sleeping the hard way");
1814 	rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1815 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1816 	    SYSCTL_CHILDREN(rack_counters),
1817 	    OID_AUTO, "allocemerg", CTLFLAG_RD,
1818 	    &rack_to_alloc_emerg,
1819 	    "Total allocations done from emergency cache");
1820 	rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1821 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1822 	    SYSCTL_CHILDREN(rack_counters),
1823 	    OID_AUTO, "alloc_limited", CTLFLAG_RD,
1824 	    &rack_to_alloc_limited,
1825 	    "Total allocations dropped due to limit");
1826 	rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1827 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1828 	    SYSCTL_CHILDREN(rack_counters),
1829 	    OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1830 	    &rack_alloc_limited_conns,
1831 	    "Connections with allocations dropped due to limit");
1832 	rack_split_limited = counter_u64_alloc(M_WAITOK);
1833 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1834 	    SYSCTL_CHILDREN(rack_counters),
1835 	    OID_AUTO, "split_limited", CTLFLAG_RD,
1836 	    &rack_split_limited,
1837 	    "Split allocations dropped due to limit");
1838 	rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK);
1839 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1840 	    SYSCTL_CHILDREN(rack_counters),
1841 	    OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD,
1842 	    &rack_rxt_clamps_cwnd,
1843 	    "Number of times that excessive rxt clamped the cwnd down");
1844 	rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK);
1845 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1846 	    SYSCTL_CHILDREN(rack_counters),
1847 	    OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD,
1848 	    &rack_rxt_clamps_cwnd_uniq,
1849 	    "Number of connections that have had excessive rxt clamped the cwnd down");
1850 	rack_persists_sends = counter_u64_alloc(M_WAITOK);
1851 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1852 	    SYSCTL_CHILDREN(rack_counters),
1853 	    OID_AUTO, "persist_sends", CTLFLAG_RD,
1854 	    &rack_persists_sends,
1855 	    "Number of times we sent a persist probe");
1856 	rack_persists_acks = counter_u64_alloc(M_WAITOK);
1857 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1858 	    SYSCTL_CHILDREN(rack_counters),
1859 	    OID_AUTO, "persist_acks", CTLFLAG_RD,
1860 	    &rack_persists_acks,
1861 	    "Number of times a persist probe was acked");
1862 	rack_persists_loss = counter_u64_alloc(M_WAITOK);
1863 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1864 	    SYSCTL_CHILDREN(rack_counters),
1865 	    OID_AUTO, "persist_loss", CTLFLAG_RD,
1866 	    &rack_persists_loss,
1867 	    "Number of times we detected a lost persist probe (no ack)");
1868 	rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1869 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1870 	    SYSCTL_CHILDREN(rack_counters),
1871 	    OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1872 	    &rack_persists_lost_ends,
1873 	    "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1874 #ifdef INVARIANTS
1875 	rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1876 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1877 	    SYSCTL_CHILDREN(rack_counters),
1878 	    OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1879 	    &rack_adjust_map_bw,
1880 	    "Number of times we hit the case where the sb went up and down on a sendmap entry");
1881 #endif
1882 	rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1883 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1884 	    SYSCTL_CHILDREN(rack_counters),
1885 	    OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1886 	    &rack_multi_single_eq,
1887 	    "Number of compressed acks total represented");
1888 	rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1889 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1890 	    SYSCTL_CHILDREN(rack_counters),
1891 	    OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1892 	    &rack_proc_non_comp_ack,
1893 	    "Number of non compresseds acks that we processed");
1894 
1895 
1896 	rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1897 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1898 	    SYSCTL_CHILDREN(rack_counters),
1899 	    OID_AUTO, "sack_long", CTLFLAG_RD,
1900 	    &rack_sack_proc_all,
1901 	    "Total times we had to walk whole list for sack processing");
1902 	rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1903 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1904 	    SYSCTL_CHILDREN(rack_counters),
1905 	    OID_AUTO, "sack_restart", CTLFLAG_RD,
1906 	    &rack_sack_proc_restart,
1907 	    "Total times we had to walk whole list due to a restart");
1908 	rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1909 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1910 	    SYSCTL_CHILDREN(rack_counters),
1911 	    OID_AUTO, "sack_short", CTLFLAG_RD,
1912 	    &rack_sack_proc_short,
1913 	    "Total times we took shortcut for sack processing");
1914 	rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1915 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1916 	    SYSCTL_CHILDREN(rack_attack),
1917 	    OID_AUTO, "skipacked", CTLFLAG_RD,
1918 	    &rack_sack_skipped_acked,
1919 	    "Total number of times we skipped previously sacked");
1920 	rack_sack_splits = counter_u64_alloc(M_WAITOK);
1921 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1922 	    SYSCTL_CHILDREN(rack_attack),
1923 	    OID_AUTO, "ofsplit", CTLFLAG_RD,
1924 	    &rack_sack_splits,
1925 	    "Total number of times we did the old fashion tree split");
1926 	rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1927 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1928 	    SYSCTL_CHILDREN(rack_counters),
1929 	    OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1930 	    &rack_input_idle_reduces,
1931 	    "Total number of idle reductions on input");
1932 	rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
1933 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1934 	    SYSCTL_CHILDREN(rack_counters),
1935 	    OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
1936 	    &rack_collapsed_win_seen,
1937 	    "Total number of collapsed window events seen (where our window shrinks)");
1938 
1939 	rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1940 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1941 	    SYSCTL_CHILDREN(rack_counters),
1942 	    OID_AUTO, "collapsed_win", CTLFLAG_RD,
1943 	    &rack_collapsed_win,
1944 	    "Total number of collapsed window events where we mark packets");
1945 	rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
1946 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1947 	    SYSCTL_CHILDREN(rack_counters),
1948 	    OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
1949 	    &rack_collapsed_win_rxt,
1950 	    "Total number of packets that were retransmitted");
1951 	rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
1952 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1953 	    SYSCTL_CHILDREN(rack_counters),
1954 	    OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
1955 	    &rack_collapsed_win_rxt_bytes,
1956 	    "Total number of bytes that were retransmitted");
1957 	rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1958 	SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1959 	    SYSCTL_CHILDREN(rack_counters),
1960 	    OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1961 	    &rack_try_scwnd,
1962 	    "Total number of scwnd attempts");
1963 	COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1964 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1965 	    OID_AUTO, "outsize", CTLFLAG_RD,
1966 	    rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1967 	COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1968 	SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1969 	    OID_AUTO, "opts", CTLFLAG_RD,
1970 	    rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1971 	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1972 	    SYSCTL_CHILDREN(rack_sysctl_root),
1973 	    OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1974 	    &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1975 }
1976 
1977 static uint32_t
rc_init_window(struct tcp_rack * rack)1978 rc_init_window(struct tcp_rack *rack)
1979 {
1980 	return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1981 
1982 }
1983 
1984 static uint64_t
rack_get_fixed_pacing_bw(struct tcp_rack * rack)1985 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1986 {
1987 	if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1988 		return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1989 	else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1990 		return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1991 	else
1992 		return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1993 }
1994 
1995 static void
rack_log_hybrid_bw(struct tcp_rack * rack,uint32_t seq,uint64_t cbw,uint64_t tim,uint64_t data,uint8_t mod,uint16_t aux,struct tcp_sendfile_track * cur,int line)1996 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim,
1997 	uint64_t data, uint8_t mod, uint16_t aux,
1998 	struct tcp_sendfile_track *cur, int line)
1999 {
2000 #ifdef TCP_REQUEST_TRK
2001 	int do_log = 0;
2002 
2003 	/*
2004 	 * The rate cap one is noisy and only should come out when normal BB logging
2005 	 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out
2006 	 * once per chunk and make up the BBpoint that can be turned on by the client.
2007 	 */
2008 	if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2009 		/*
2010 		 * The very noisy two need to only come out when
2011 		 * we have verbose logging on.
2012 		 */
2013 		if (rack_verbose_logging != 0)
2014 			do_log = tcp_bblogging_on(rack->rc_tp);
2015 		else
2016 			do_log = 0;
2017 	} else if (mod != HYBRID_LOG_BW_MEASURE) {
2018 		/*
2019 		 * All other less noisy logs here except the measure which
2020 		 * also needs to come out on the point and the log.
2021 		 */
2022 		do_log = tcp_bblogging_on(rack->rc_tp);
2023 	} else {
2024 		do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING);
2025 	}
2026 
2027 	if (do_log) {
2028 		union tcp_log_stackspecific log;
2029 		struct timeval tv;
2030 		uint64_t lt_bw;
2031 
2032 		/* Convert our ms to a microsecond */
2033 		memset(&log, 0, sizeof(log));
2034 
2035 		log.u_bbr.cwnd_gain = line;
2036 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2037 		log.u_bbr.rttProp = tim;
2038 		log.u_bbr.bw_inuse = cbw;
2039 		log.u_bbr.delRate = rack_get_gp_est(rack);
2040 		lt_bw = rack_get_lt_bw(rack);
2041 		log.u_bbr.flex1 = seq;
2042 		log.u_bbr.pacing_gain = aux;
2043 		/* lt_bw = < flex3 | flex2 > */
2044 		log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff);
2045 		log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff);
2046 		/* Record the last obtained us rtt in inflight */
2047 		if (cur == NULL) {
2048 			/* Make sure we are looking at the right log if an overide comes in */
2049 			cur = rack->r_ctl.rc_last_sft;
2050 		}
2051 		if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY)
2052 			log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt;
2053 		else {
2054 			/* Use the last known rtt i.e. the rack-rtt */
2055 			log.u_bbr.inflight = rack->rc_rack_rtt;
2056 		}
2057 		if (cur != NULL) {
2058 			uint64_t off;
2059 
2060 			log.u_bbr.cur_del_rate = cur->deadline;
2061 			if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2062 				/* start = < lost | pkt_epoch > */
2063 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2064 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2065 				log.u_bbr.flex6 = cur->start_seq;
2066 				log.u_bbr.pkts_out = cur->end_seq;
2067 			} else {
2068 				/* start = < lost | pkt_epoch > */
2069 				log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2070 				log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2071 				/* end = < pkts_out | flex6 > */
2072 				log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff);
2073 				log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2074 			}
2075 			/* first_send = <lt_epoch | epoch> */
2076 			log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff);
2077 			log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff);
2078 			/* localtime = <delivered | applimited>*/
2079 			log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2080 			log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2081 #ifdef TCP_REQUEST_TRK
2082 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2083 			log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2084 #endif
2085 			log.u_bbr.inhpts = 1;
2086 			log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs);
2087 			log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs);
2088 			log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags;
2089 		} else {
2090 			log.u_bbr.flex7 = 0xffff;
2091 			log.u_bbr.cur_del_rate = 0xffffffffffffffff;
2092 		}
2093 		/*
2094 		 * Compose bbr_state to be a bit wise 0000ADHF
2095 		 * where A is the always_pace flag
2096 		 * where D is the dgp_on flag
2097 		 * where H is the hybrid_mode on flag
2098 		 * where F is the use_fixed_rate flag.
2099 		 */
2100 		log.u_bbr.bbr_state = rack->rc_always_pace;
2101 		log.u_bbr.bbr_state <<= 1;
2102 		log.u_bbr.bbr_state |= rack->dgp_on;
2103 		log.u_bbr.bbr_state <<= 1;
2104 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2105 		log.u_bbr.bbr_state <<= 1;
2106 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2107 		log.u_bbr.flex8 = mod;
2108 		tcp_log_event(rack->rc_tp, NULL,
2109 		    &rack->rc_inp->inp_socket->so_rcv,
2110 		    &rack->rc_inp->inp_socket->so_snd,
2111 		    TCP_HYBRID_PACING_LOG, 0,
2112 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2113 
2114 	}
2115 #endif
2116 }
2117 
2118 #ifdef TCP_REQUEST_TRK
2119 static void
rack_log_hybrid_sends(struct tcp_rack * rack,struct tcp_sendfile_track * cur,int line)2120 rack_log_hybrid_sends(struct tcp_rack *rack, struct tcp_sendfile_track *cur, int line)
2121 {
2122 	if (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING)) {
2123 		union tcp_log_stackspecific log;
2124 		struct timeval tv;
2125 		uint64_t off;
2126 
2127 		/* Convert our ms to a microsecond */
2128 		memset(&log, 0, sizeof(log));
2129 
2130 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2131 		log.u_bbr.delRate = cur->sent_at_fs;
2132 
2133 		if ((cur->flags & TCP_TRK_TRACK_FLG_LSND) == 0) {
2134 			/*
2135 			 * We did not get a new Rules Applied to set so
2136 			 * no overlapping send occured, this means the
2137 			 * current byte counts are correct.
2138 			 */
2139 			log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
2140 			log.u_bbr.rttProp = rack->rc_tp->t_snd_rxt_bytes;
2141 		} else {
2142 			/*
2143 			 * Overlapping send case, we switched to a new
2144 			 * send and did a rules applied.
2145 			 */
2146 			log.u_bbr.cur_del_rate = cur->sent_at_ls;
2147 			log.u_bbr.rttProp = cur->rxt_at_ls;
2148 		}
2149 		log.u_bbr.bw_inuse = cur->rxt_at_fs;
2150 		log.u_bbr.cwnd_gain = line;
2151 		off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2152 		log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2153 		/* start = < flex1 | flex2 > */
2154 		log.u_bbr.flex2 = (uint32_t)(cur->start & 0x00000000ffffffff);
2155 		log.u_bbr.flex1 = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2156 		/* end = < flex3 | flex4 > */
2157 		log.u_bbr.flex4 = (uint32_t)(cur->end & 0x00000000ffffffff);
2158 		log.u_bbr.flex3 = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2159 
2160 		/* localtime = <delivered | applimited>*/
2161 		log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2162 		log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2163 		/* client timestamp = <lt_epoch | epoch>*/
2164 		log.u_bbr.epoch = (uint32_t)(cur->timestamp & 0x00000000ffffffff);
2165 		log.u_bbr.lt_epoch = (uint32_t)((cur->timestamp >> 32) & 0x00000000ffffffff);
2166 		/* now set all the flags in */
2167 		log.u_bbr.pkts_out = cur->hybrid_flags;
2168 		log.u_bbr.lost = cur->playout_ms;
2169 		log.u_bbr.flex6 = cur->flags;
2170 		/*
2171 		 * Last send time  = <flex5 | pkt_epoch>  note we do not distinguish cases
2172 		 * where a false retransmit occurred so first_send  <-> lastsend may
2173 		 * include longer time then it actually took if we have a false rxt.
2174 		 */
2175 		log.u_bbr.pkt_epoch = (uint32_t)(rack->r_ctl.last_tmit_time_acked & 0x00000000ffffffff);
2176 		log.u_bbr.flex5 = (uint32_t)((rack->r_ctl.last_tmit_time_acked >> 32) & 0x00000000ffffffff);
2177 		/*
2178 		 * Compose bbr_state to be a bit wise 0000ADHF
2179 		 * where A is the always_pace flag
2180 		 * where D is the dgp_on flag
2181 		 * where H is the hybrid_mode on flag
2182 		 * where F is the use_fixed_rate flag.
2183 		 */
2184 		log.u_bbr.bbr_state = rack->rc_always_pace;
2185 		log.u_bbr.bbr_state <<= 1;
2186 		log.u_bbr.bbr_state |= rack->dgp_on;
2187 		log.u_bbr.bbr_state <<= 1;
2188 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2189 		log.u_bbr.bbr_state <<= 1;
2190 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
2191 
2192 		log.u_bbr.flex8 = HYBRID_LOG_SENT_LOST;
2193 		tcp_log_event(rack->rc_tp, NULL,
2194 		    &rack->rc_inp->inp_socket->so_rcv,
2195 		    &rack->rc_inp->inp_socket->so_snd,
2196 		    TCP_HYBRID_PACING_LOG, 0,
2197 		    0, &log, false, NULL, __func__, __LINE__, &tv);
2198 	}
2199 }
2200 #endif
2201 
2202 static inline uint64_t
rack_compensate_for_linerate(struct tcp_rack * rack,uint64_t bw)2203 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw)
2204 {
2205 	uint64_t ret_bw, ether;
2206 	uint64_t u_segsiz;
2207 
2208 	ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr);
2209 	if (rack->r_is_v6){
2210 #ifdef INET6
2211 		ether += sizeof(struct ip6_hdr);
2212 #endif
2213 		ether += 14;	/* eheader size 6+6+2 */
2214 	} else {
2215 #ifdef INET
2216 		ether += sizeof(struct ip);
2217 #endif
2218 		ether += 14;	/* eheader size 6+6+2 */
2219 	}
2220 	u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs);
2221 	ret_bw = bw;
2222 	ret_bw *= ether;
2223 	ret_bw /= u_segsiz;
2224 	return (ret_bw);
2225 }
2226 
2227 static void
rack_rate_cap_bw(struct tcp_rack * rack,uint64_t * bw,int * capped)2228 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped)
2229 {
2230 #ifdef TCP_REQUEST_TRK
2231 	struct timeval tv;
2232 	uint64_t timenow, timeleft, lenleft, lengone, calcbw;
2233 #endif
2234 
2235 	if (rack->r_ctl.bw_rate_cap == 0)
2236 		return;
2237 #ifdef TCP_REQUEST_TRK
2238 	if (rack->rc_catch_up && rack->rc_hybrid_mode &&
2239 	    (rack->r_ctl.rc_last_sft != NULL)) {
2240 		/*
2241 		 * We have a dynamic cap. The original target
2242 		 * is in bw_rate_cap, but we need to look at
2243 		 * how long it is until we hit the deadline.
2244 		 */
2245 		struct tcp_sendfile_track *ent;
2246 
2247       		ent = rack->r_ctl.rc_last_sft;
2248 		microuptime(&tv);
2249 		timenow = tcp_tv_to_lusectick(&tv);
2250 		if (timenow >= ent->deadline) {
2251 			/* No time left we do DGP only */
2252 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2253 					   0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2254 			rack->r_ctl.bw_rate_cap = 0;
2255 			return;
2256 		}
2257 		/* We have the time */
2258 		timeleft = rack->r_ctl.rc_last_sft->deadline - timenow;
2259 		if (timeleft < HPTS_MSEC_IN_SEC) {
2260 			/* If there is less than a ms left just use DGPs rate */
2261 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2262 					   0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__);
2263 			rack->r_ctl.bw_rate_cap = 0;
2264 			return;
2265 		}
2266 		/*
2267 		 * Now lets find the amount of data left to send.
2268 		 *
2269 		 * Now ideally we want to use the end_seq to figure out how much more
2270 		 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry..
2271 		 */
2272 		if (ent->flags & TCP_TRK_TRACK_FLG_COMP) {
2273 			if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una))
2274 				lenleft = ent->end_seq - rack->rc_tp->snd_una;
2275 			else {
2276 				/* TSNH, we should catch it at the send */
2277 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2278 						   0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2279 				rack->r_ctl.bw_rate_cap = 0;
2280 				return;
2281 			}
2282 		} else {
2283 			/*
2284 			 * The hard way, figure out how much is gone and then
2285 			 * take that away from the total the client asked for
2286 			 * (thats off by tls overhead if this is tls).
2287 			 */
2288 			if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq))
2289 				lengone = rack->rc_tp->snd_una - ent->start_seq;
2290 			else
2291 				lengone = 0;
2292 			if (lengone < (ent->end - ent->start))
2293 				lenleft = (ent->end - ent->start) - lengone;
2294 			else {
2295 				/* TSNH, we should catch it at the send */
2296 				rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2297 						   0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent, __LINE__);
2298 				rack->r_ctl.bw_rate_cap = 0;
2299 				return;
2300 			}
2301 		}
2302 		if (lenleft == 0) {
2303 			/* We have it all sent */
2304 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2305 					   0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent, __LINE__);
2306 			if (rack->r_ctl.bw_rate_cap)
2307 				goto normal_ratecap;
2308 			else
2309 				return;
2310 		}
2311 		calcbw = lenleft * HPTS_USEC_IN_SEC;
2312 		calcbw /= timeleft;
2313 		/* Now we must compensate for IP/TCP overhead */
2314 		calcbw = rack_compensate_for_linerate(rack, calcbw);
2315 		/* Update the bit rate cap */
2316 		rack->r_ctl.bw_rate_cap = calcbw;
2317 		if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2318 		    (rack_hybrid_allow_set_maxseg == 1) &&
2319 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2320 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2321 			uint32_t orig_max;
2322 
2323 			orig_max = rack->r_ctl.rc_pace_max_segs;
2324 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2325 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp));
2326 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2327 		}
2328 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2329 				   calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent, __LINE__);
2330 		if ((calcbw > 0) && (*bw > calcbw)) {
2331 			rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2332 					   *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent, __LINE__);
2333 			*capped = 1;
2334 			*bw = calcbw;
2335 		}
2336 		return;
2337 	}
2338 normal_ratecap:
2339 #endif
2340 	if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) {
2341 #ifdef TCP_REQUEST_TRK
2342 		if (rack->rc_hybrid_mode &&
2343 		    rack->rc_catch_up &&
2344 		    (rack->r_ctl.rc_last_sft != NULL) &&
2345 		    (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2346 		    (rack_hybrid_allow_set_maxseg == 1) &&
2347 		    ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2348 			/* Lets set in a smaller mss possibly here to match our rate-cap */
2349 			uint32_t orig_max;
2350 
2351 			orig_max = rack->r_ctl.rc_pace_max_segs;
2352 			rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2353 			rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp));
2354 			rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2355 		}
2356 #endif
2357 		*capped = 1;
2358 		*bw = rack->r_ctl.bw_rate_cap;
2359 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2360 				   *bw, 0, 0,
2361 				   HYBRID_LOG_RATE_CAP, 1, NULL, __LINE__);
2362 	}
2363 }
2364 
2365 static uint64_t
rack_get_gp_est(struct tcp_rack * rack)2366 rack_get_gp_est(struct tcp_rack *rack)
2367 {
2368 	uint64_t bw, lt_bw, ret_bw;
2369 
2370 	if (rack->rc_gp_filled == 0) {
2371 		/*
2372 		 * We have yet no b/w measurement,
2373 		 * if we have a user set initial bw
2374 		 * return it. If we don't have that and
2375 		 * we have an srtt, use the tcp IW (10) to
2376 		 * calculate a fictional b/w over the SRTT
2377 		 * which is more or less a guess. Note
2378 		 * we don't use our IW from rack on purpose
2379 		 * so if we have like IW=30, we are not
2380 		 * calculating a "huge" b/w.
2381 		 */
2382 		uint64_t srtt;
2383 
2384 		if (rack->dis_lt_bw == 1)
2385 			lt_bw = 0;
2386 		else
2387 			lt_bw = rack_get_lt_bw(rack);
2388 		if (lt_bw) {
2389 			/*
2390 			 * No goodput bw but a long-term b/w does exist
2391 			 * lets use that.
2392 			 */
2393 			ret_bw = lt_bw;
2394 			goto compensate;
2395 		}
2396 		if (rack->r_ctl.init_rate)
2397 			return (rack->r_ctl.init_rate);
2398 
2399 		/* Ok lets come up with the IW guess, if we have a srtt */
2400 		if (rack->rc_tp->t_srtt == 0) {
2401 			/*
2402 			 * Go with old pacing method
2403 			 * i.e. burst mitigation only.
2404 			 */
2405 			return (0);
2406 		}
2407 		/* Ok lets get the initial TCP win (not racks) */
2408 		bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2409 		srtt = (uint64_t)rack->rc_tp->t_srtt;
2410 		bw *= (uint64_t)USECS_IN_SECOND;
2411 		bw /= srtt;
2412 		ret_bw = bw;
2413 		goto compensate;
2414 
2415 	}
2416 	if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2417 		/* Averaging is done, we can return the value */
2418 		bw = rack->r_ctl.gp_bw;
2419 	} else {
2420 		/* Still doing initial average must calculate */
2421 		bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1);
2422 	}
2423 	if (rack->dis_lt_bw) {
2424 		/* We are not using lt-bw */
2425 		ret_bw = bw;
2426 		goto compensate;
2427 	}
2428 	lt_bw = rack_get_lt_bw(rack);
2429 	if (lt_bw == 0) {
2430 		/* If we don't have one then equate it to the gp_bw */
2431 		lt_bw = rack->r_ctl.gp_bw;
2432 	}
2433 	if (rack->use_lesser_lt_bw) {
2434 		if (lt_bw < bw)
2435 			ret_bw = lt_bw;
2436 		else
2437 			ret_bw = bw;
2438 	} else {
2439 		if (lt_bw > bw)
2440 			ret_bw = lt_bw;
2441 		else
2442 			ret_bw = bw;
2443 	}
2444 	/*
2445 	 * Now lets compensate based on the TCP/IP overhead. Our
2446 	 * Goodput estimate does not include this so we must pace out
2447 	 * a bit faster since our pacing calculations do. The pacing
2448 	 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz
2449 	 * we are using to do this, so we do that here in the opposite
2450 	 * direction as well. This means that if we are tunneled and the
2451 	 * segsiz is say 1200 bytes we will get quite a boost, but its
2452 	 * compensated for in the pacing time the opposite way.
2453 	 */
2454 compensate:
2455 	ret_bw = rack_compensate_for_linerate(rack, ret_bw);
2456 	return(ret_bw);
2457 }
2458 
2459 
2460 static uint64_t
rack_get_bw(struct tcp_rack * rack)2461 rack_get_bw(struct tcp_rack *rack)
2462 {
2463 	uint64_t bw;
2464 
2465 	if (rack->use_fixed_rate) {
2466 		/* Return the fixed pacing rate */
2467 		return (rack_get_fixed_pacing_bw(rack));
2468 	}
2469 	bw = rack_get_gp_est(rack);
2470 	return (bw);
2471 }
2472 
2473 static uint16_t
rack_get_output_gain(struct tcp_rack * rack,struct rack_sendmap * rsm)2474 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2475 {
2476 	if (rack->use_fixed_rate) {
2477 		return (100);
2478 	} else if (rack->in_probe_rtt && (rsm == NULL))
2479 		return (rack->r_ctl.rack_per_of_gp_probertt);
2480 	else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2481 		  rack->r_ctl.rack_per_of_gp_rec)) {
2482 		if (rsm) {
2483 			/* a retransmission always use the recovery rate */
2484 			return (rack->r_ctl.rack_per_of_gp_rec);
2485 		} else if (rack->rack_rec_nonrxt_use_cr) {
2486 			/* Directed to use the configured rate */
2487 			goto configured_rate;
2488 		} else if (rack->rack_no_prr &&
2489 			   (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2490 			/* No PRR, lets just use the b/w estimate only */
2491 			return (100);
2492 		} else {
2493 			/*
2494 			 * Here we may have a non-retransmit but we
2495 			 * have no overrides, so just use the recovery
2496 			 * rate (prr is in effect).
2497 			 */
2498 			return (rack->r_ctl.rack_per_of_gp_rec);
2499 		}
2500 	}
2501 configured_rate:
2502 	/* For the configured rate we look at our cwnd vs the ssthresh */
2503 	if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2504 		return (rack->r_ctl.rack_per_of_gp_ss);
2505 	else
2506 		return (rack->r_ctl.rack_per_of_gp_ca);
2507 }
2508 
2509 static void
rack_log_dsack_event(struct tcp_rack * rack,uint8_t mod,uint32_t flex4,uint32_t flex5,uint32_t flex6)2510 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2511 {
2512 	/*
2513 	 * Types of logs (mod value)
2514 	 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2515 	 * 2 = a dsack round begins, persist is reset to 16.
2516 	 * 3 = a dsack round ends
2517 	 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2518 	 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2519 	 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2520 	 */
2521 	if (tcp_bblogging_on(rack->rc_tp)) {
2522 		union tcp_log_stackspecific log;
2523 		struct timeval tv;
2524 
2525 		memset(&log, 0, sizeof(log));
2526 		log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2527 		log.u_bbr.flex1 <<= 1;
2528 		log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2529 		log.u_bbr.flex1 <<= 1;
2530 		log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2531 		log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2532 		log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2533 		log.u_bbr.flex4 = flex4;
2534 		log.u_bbr.flex5 = flex5;
2535 		log.u_bbr.flex6 = flex6;
2536 		log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2537 		log.u_bbr.flex8 = mod;
2538 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2539 		log.u_bbr.epoch = rack->r_ctl.current_round;
2540 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2541 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2542 		    &rack->rc_inp->inp_socket->so_rcv,
2543 		    &rack->rc_inp->inp_socket->so_snd,
2544 		    RACK_DSACK_HANDLING, 0,
2545 		    0, &log, false, &tv);
2546 	}
2547 }
2548 
2549 static void
rack_log_hdwr_pacing(struct tcp_rack * rack,uint64_t rate,uint64_t hw_rate,int line,int error,uint16_t mod)2550 rack_log_hdwr_pacing(struct tcp_rack *rack,
2551 		     uint64_t rate, uint64_t hw_rate, int line,
2552 		     int error, uint16_t mod)
2553 {
2554 	if (tcp_bblogging_on(rack->rc_tp)) {
2555 		union tcp_log_stackspecific log;
2556 		struct timeval tv;
2557 		const struct ifnet *ifp;
2558 		uint64_t ifp64;
2559 
2560 		memset(&log, 0, sizeof(log));
2561 		log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2562 		log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2563 		if (rack->r_ctl.crte) {
2564 			ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2565 		} else if (rack->rc_inp->inp_route.ro_nh &&
2566 			   rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2567 			ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2568 		} else
2569 			ifp = NULL;
2570 		if (ifp) {
2571 			ifp64 = (uintptr_t)ifp;
2572 			log.u_bbr.flex3 = ((ifp64  >> 32) & 0x00000000ffffffff);
2573 			log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff);
2574 		}
2575 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2576 		log.u_bbr.bw_inuse = rate;
2577 		log.u_bbr.flex5 = line;
2578 		log.u_bbr.flex6 = error;
2579 		log.u_bbr.flex7 = mod;
2580 		log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2581 		log.u_bbr.flex8 = rack->use_fixed_rate;
2582 		log.u_bbr.flex8 <<= 1;
2583 		log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2584 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2585 		log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2586 		if (rack->r_ctl.crte)
2587 			log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2588 		else
2589 			log.u_bbr.cur_del_rate = 0;
2590 		log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2591 		log.u_bbr.epoch = rack->r_ctl.current_round;
2592 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2593 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2594 		    &rack->rc_inp->inp_socket->so_rcv,
2595 		    &rack->rc_inp->inp_socket->so_snd,
2596 		    BBR_LOG_HDWR_PACE, 0,
2597 		    0, &log, false, &tv);
2598 	}
2599 }
2600 
2601 static uint64_t
rack_get_output_bw(struct tcp_rack * rack,uint64_t bw,struct rack_sendmap * rsm,int * capped)2602 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2603 {
2604 	/*
2605 	 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2606 	 */
2607 	uint64_t bw_est, high_rate;
2608 	uint64_t gain;
2609 
2610 	gain = (uint64_t)rack_get_output_gain(rack, rsm);
2611 	bw_est = bw * gain;
2612 	bw_est /= (uint64_t)100;
2613 	/* Never fall below the minimum (def 64kbps) */
2614 	if (bw_est < RACK_MIN_BW)
2615 		bw_est = RACK_MIN_BW;
2616 	if (rack->r_rack_hw_rate_caps) {
2617 		/* Rate caps are in place */
2618 		if (rack->r_ctl.crte != NULL) {
2619 			/* We have a hdwr rate already */
2620 			high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2621 			if (bw_est >= high_rate) {
2622 				/* We are capping bw at the highest rate table entry */
2623 				if (rack_hw_rate_cap_per &&
2624 				    (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) {
2625 					rack->r_rack_hw_rate_caps = 0;
2626 					goto done;
2627 				}
2628 				rack_log_hdwr_pacing(rack,
2629 						     bw_est, high_rate, __LINE__,
2630 						     0, 3);
2631 				bw_est = high_rate;
2632 				if (capped)
2633 					*capped = 1;
2634 			}
2635 		} else if ((rack->rack_hdrw_pacing == 0) &&
2636 			   (rack->rack_hdw_pace_ena) &&
2637 			   (rack->rack_attempt_hdwr_pace == 0) &&
2638 			   (rack->rc_inp->inp_route.ro_nh != NULL) &&
2639 			   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2640 			/*
2641 			 * Special case, we have not yet attempted hardware
2642 			 * pacing, and yet we may, when we do, find out if we are
2643 			 * above the highest rate. We need to know the maxbw for the interface
2644 			 * in question (if it supports ratelimiting). We get back
2645 			 * a 0, if the interface is not found in the RL lists.
2646 			 */
2647 			high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2648 			if (high_rate) {
2649 				/* Yep, we have a rate is it above this rate? */
2650 				if (bw_est > high_rate) {
2651 					bw_est = high_rate;
2652 					if (capped)
2653 						*capped = 1;
2654 				}
2655 			}
2656 		}
2657 	}
2658 done:
2659 	return (bw_est);
2660 }
2661 
2662 static void
rack_log_retran_reason(struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t tsused,uint32_t thresh,int mod)2663 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2664 {
2665 	if (tcp_bblogging_on(rack->rc_tp)) {
2666 		union tcp_log_stackspecific log;
2667 		struct timeval tv;
2668 
2669 		if ((mod != 1) && (rack_verbose_logging == 0))  {
2670 			/*
2671 			 * We get 3 values currently for mod
2672 			 * 1 - We are retransmitting and this tells the reason.
2673 			 * 2 - We are clearing a dup-ack count.
2674 			 * 3 - We are incrementing a dup-ack count.
2675 			 *
2676 			 * The clear/increment are only logged
2677 			 * if you have BBverbose on.
2678 			 */
2679 			return;
2680 		}
2681 		memset(&log, 0, sizeof(log));
2682 		log.u_bbr.flex1 = tsused;
2683 		log.u_bbr.flex2 = thresh;
2684 		log.u_bbr.flex3 = rsm->r_flags;
2685 		log.u_bbr.flex4 = rsm->r_dupack;
2686 		log.u_bbr.flex5 = rsm->r_start;
2687 		log.u_bbr.flex6 = rsm->r_end;
2688 		log.u_bbr.flex8 = mod;
2689 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2690 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2691 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2692 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2693 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2694 		log.u_bbr.pacing_gain = rack->r_must_retran;
2695 		log.u_bbr.epoch = rack->r_ctl.current_round;
2696 		log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost;
2697 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2698 		    &rack->rc_inp->inp_socket->so_rcv,
2699 		    &rack->rc_inp->inp_socket->so_snd,
2700 		    BBR_LOG_SETTINGS_CHG, 0,
2701 		    0, &log, false, &tv);
2702 	}
2703 }
2704 
2705 static void
rack_log_to_start(struct tcp_rack * rack,uint32_t cts,uint32_t to,int32_t slot,uint8_t which)2706 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2707 {
2708 	if (tcp_bblogging_on(rack->rc_tp)) {
2709 		union tcp_log_stackspecific log;
2710 		struct timeval tv;
2711 
2712 		memset(&log, 0, sizeof(log));
2713 		log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2714 		log.u_bbr.flex2 = to;
2715 		log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2716 		log.u_bbr.flex4 = slot;
2717 		log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot;
2718 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2719 		log.u_bbr.flex7 = rack->rc_in_persist;
2720 		log.u_bbr.flex8 = which;
2721 		if (rack->rack_no_prr)
2722 			log.u_bbr.pkts_out = 0;
2723 		else
2724 			log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2725 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2726 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2727 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2728 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2729 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2730 		log.u_bbr.pacing_gain = rack->r_must_retran;
2731 		log.u_bbr.cwnd_gain = rack->rack_deferred_inited;
2732 		log.u_bbr.pkt_epoch = rack->rc_has_collapsed;
2733 		log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2734 		log.u_bbr.lost = rack_rto_min;
2735 		log.u_bbr.epoch = rack->r_ctl.roundends;
2736 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2737 		log.u_bbr.bw_inuse <<= 32;
2738 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2739 		log.u_bbr.applimited = rack->rc_tp->t_flags2;
2740 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2741 		    &rack->rc_inp->inp_socket->so_rcv,
2742 		    &rack->rc_inp->inp_socket->so_snd,
2743 		    BBR_LOG_TIMERSTAR, 0,
2744 		    0, &log, false, &tv);
2745 	}
2746 }
2747 
2748 static void
rack_log_to_event(struct tcp_rack * rack,int32_t to_num,struct rack_sendmap * rsm)2749 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2750 {
2751 	if (tcp_bblogging_on(rack->rc_tp)) {
2752 		union tcp_log_stackspecific log;
2753 		struct timeval tv;
2754 
2755 		memset(&log, 0, sizeof(log));
2756 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2757 		log.u_bbr.flex8 = to_num;
2758 		log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2759 		log.u_bbr.flex2 = rack->rc_rack_rtt;
2760 		if (rsm == NULL)
2761 			log.u_bbr.flex3 = 0;
2762 		else
2763 			log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2764 		if (rack->rack_no_prr)
2765 			log.u_bbr.flex5 = 0;
2766 		else
2767 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2768 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2769 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2770 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2771 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2772 		log.u_bbr.pacing_gain = rack->r_must_retran;
2773 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2774 		log.u_bbr.bw_inuse <<= 32;
2775 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2776 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2777 		    &rack->rc_inp->inp_socket->so_rcv,
2778 		    &rack->rc_inp->inp_socket->so_snd,
2779 		    BBR_LOG_RTO, 0,
2780 		    0, &log, false, &tv);
2781 	}
2782 }
2783 
2784 static void
rack_log_map_chg(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * prev,struct rack_sendmap * rsm,struct rack_sendmap * next,int flag,uint32_t th_ack,int line)2785 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2786 		 struct rack_sendmap *prev,
2787 		 struct rack_sendmap *rsm,
2788 		 struct rack_sendmap *next,
2789 		 int flag, uint32_t th_ack, int line)
2790 {
2791 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2792 		union tcp_log_stackspecific log;
2793 		struct timeval tv;
2794 
2795 		memset(&log, 0, sizeof(log));
2796 		log.u_bbr.flex8 = flag;
2797 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2798 		log.u_bbr.cur_del_rate = (uintptr_t)prev;
2799 		log.u_bbr.delRate = (uintptr_t)rsm;
2800 		log.u_bbr.rttProp = (uintptr_t)next;
2801 		log.u_bbr.flex7 = 0;
2802 		if (prev) {
2803 			log.u_bbr.flex1 = prev->r_start;
2804 			log.u_bbr.flex2 = prev->r_end;
2805 			log.u_bbr.flex7 |= 0x4;
2806 		}
2807 		if (rsm) {
2808 			log.u_bbr.flex3 = rsm->r_start;
2809 			log.u_bbr.flex4 = rsm->r_end;
2810 			log.u_bbr.flex7 |= 0x2;
2811 		}
2812 		if (next) {
2813 			log.u_bbr.flex5 = next->r_start;
2814 			log.u_bbr.flex6 = next->r_end;
2815 			log.u_bbr.flex7 |= 0x1;
2816 		}
2817 		log.u_bbr.applimited = line;
2818 		log.u_bbr.pkts_out = th_ack;
2819 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2820 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2821 		if (rack->rack_no_prr)
2822 			log.u_bbr.lost = 0;
2823 		else
2824 			log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2825 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2826 		log.u_bbr.bw_inuse <<= 32;
2827 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2828 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2829 		    &rack->rc_inp->inp_socket->so_rcv,
2830 		    &rack->rc_inp->inp_socket->so_snd,
2831 		    TCP_LOG_MAPCHG, 0,
2832 		    0, &log, false, &tv);
2833 	}
2834 }
2835 
2836 static void
rack_log_rtt_upd(struct tcpcb * tp,struct tcp_rack * rack,uint32_t t,uint32_t len,struct rack_sendmap * rsm,int conf)2837 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2838 		 struct rack_sendmap *rsm, int conf)
2839 {
2840 	if (tcp_bblogging_on(tp)) {
2841 		union tcp_log_stackspecific log;
2842 		struct timeval tv;
2843 		memset(&log, 0, sizeof(log));
2844 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2845 		log.u_bbr.flex1 = t;
2846 		log.u_bbr.flex2 = len;
2847 		log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2848 		log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2849 		log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2850 		log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2851 		log.u_bbr.flex7 = conf;
2852 		log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2853 		log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2854 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2855 		log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2856 		log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2857 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2858 		if (rsm) {
2859 			log.u_bbr.pkt_epoch = rsm->r_start;
2860 			log.u_bbr.lost = rsm->r_end;
2861 			log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2862 			/* We loose any upper of the 24 bits */
2863 			log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2864 		} else {
2865 			/* Its a SYN */
2866 			log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2867 			log.u_bbr.lost = 0;
2868 			log.u_bbr.cwnd_gain = 0;
2869 			log.u_bbr.pacing_gain = 0;
2870 		}
2871 		/* Write out general bits of interest rrs here */
2872 		log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2873 		log.u_bbr.use_lt_bw <<= 1;
2874 		log.u_bbr.use_lt_bw |= rack->forced_ack;
2875 		log.u_bbr.use_lt_bw <<= 1;
2876 		log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2877 		log.u_bbr.use_lt_bw <<= 1;
2878 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2879 		log.u_bbr.use_lt_bw <<= 1;
2880 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2881 		log.u_bbr.use_lt_bw <<= 1;
2882 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2883 		log.u_bbr.use_lt_bw <<= 1;
2884 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2885 		log.u_bbr.use_lt_bw <<= 1;
2886 		log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2887 		log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2888 		log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2889 		log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2890 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2891 		log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2892 		log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2893 		log.u_bbr.bw_inuse <<= 32;
2894 		if (rsm)
2895 			log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2896 		TCP_LOG_EVENTP(tp, NULL,
2897 		    &rack->rc_inp->inp_socket->so_rcv,
2898 		    &rack->rc_inp->inp_socket->so_snd,
2899 		    BBR_LOG_BBRRTT, 0,
2900 		    0, &log, false, &tv);
2901 
2902 
2903 	}
2904 }
2905 
2906 static void
rack_log_rtt_sample(struct tcp_rack * rack,uint32_t rtt)2907 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2908 {
2909 	/*
2910 	 * Log the rtt sample we are
2911 	 * applying to the srtt algorithm in
2912 	 * useconds.
2913 	 */
2914 	if (tcp_bblogging_on(rack->rc_tp)) {
2915 		union tcp_log_stackspecific log;
2916 		struct timeval tv;
2917 
2918 		/* Convert our ms to a microsecond */
2919 		memset(&log, 0, sizeof(log));
2920 		log.u_bbr.flex1 = rtt;
2921 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2922 		log.u_bbr.flex7 = 1;
2923 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2924 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2925 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2926 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2927 		log.u_bbr.pacing_gain = rack->r_must_retran;
2928 		/*
2929 		 * We capture in delRate the upper 32 bits as
2930 		 * the confidence level we had declared, and the
2931 		 * lower 32 bits as the actual RTT using the arrival
2932 		 * timestamp.
2933 		 */
2934 		log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2935 		log.u_bbr.delRate <<= 32;
2936 		log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2937 		/* Lets capture all the things that make up t_rtxcur */
2938 		log.u_bbr.applimited = rack_rto_min;
2939 		log.u_bbr.epoch = rack_rto_max;
2940 		log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2941 		log.u_bbr.lost = rack_rto_min;
2942 		log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2943 		log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2944 		log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2945 		log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2946 		log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2947 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2948 		    &rack->rc_inp->inp_socket->so_rcv,
2949 		    &rack->rc_inp->inp_socket->so_snd,
2950 		    TCP_LOG_RTT, 0,
2951 		    0, &log, false, &tv);
2952 	}
2953 }
2954 
2955 static void
rack_log_rtt_sample_calc(struct tcp_rack * rack,uint32_t rtt,uint32_t send_time,uint32_t ack_time,int where)2956 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2957 {
2958 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2959 		union tcp_log_stackspecific log;
2960 		struct timeval tv;
2961 
2962 		/* Convert our ms to a microsecond */
2963 		memset(&log, 0, sizeof(log));
2964 		log.u_bbr.flex1 = rtt;
2965 		log.u_bbr.flex2 = send_time;
2966 		log.u_bbr.flex3 = ack_time;
2967 		log.u_bbr.flex4 = where;
2968 		log.u_bbr.flex7 = 2;
2969 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2970 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2971 		log.u_bbr.bw_inuse <<= 32;
2972 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
2973 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
2974 		    &rack->rc_inp->inp_socket->so_rcv,
2975 		    &rack->rc_inp->inp_socket->so_snd,
2976 		    TCP_LOG_RTT, 0,
2977 		    0, &log, false, &tv);
2978 	}
2979 }
2980 
2981 
2982 static void
rack_log_rtt_sendmap(struct tcp_rack * rack,uint32_t idx,uint64_t tsv,uint32_t tsecho)2983 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho)
2984 {
2985 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2986 		union tcp_log_stackspecific log;
2987 		struct timeval tv;
2988 
2989 		/* Convert our ms to a microsecond */
2990 		memset(&log, 0, sizeof(log));
2991 		log.u_bbr.flex1 = idx;
2992 		log.u_bbr.flex2 = rack_ts_to_msec(tsv);
2993 		log.u_bbr.flex3 = tsecho;
2994 		log.u_bbr.flex7 = 3;
2995 		log.u_bbr.rttProp = tsv;
2996 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2997 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
2998 		log.u_bbr.bw_inuse <<= 32;
2999 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3000 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3001 		    &rack->rc_inp->inp_socket->so_rcv,
3002 		    &rack->rc_inp->inp_socket->so_snd,
3003 		    TCP_LOG_RTT, 0,
3004 		    0, &log, false, &tv);
3005 	}
3006 }
3007 
3008 
3009 static inline void
rack_log_progress_event(struct tcp_rack * rack,struct tcpcb * tp,uint32_t tick,int event,int line)3010 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
3011 {
3012 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3013 		union tcp_log_stackspecific log;
3014 		struct timeval tv;
3015 
3016 		memset(&log, 0, sizeof(log));
3017 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3018 		log.u_bbr.flex1 = line;
3019 		log.u_bbr.flex2 = tick;
3020 		log.u_bbr.flex3 = tp->t_maxunacktime;
3021 		log.u_bbr.flex4 = tp->t_acktime;
3022 		log.u_bbr.flex8 = event;
3023 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3024 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3025 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3026 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3027 		log.u_bbr.pacing_gain = rack->r_must_retran;
3028 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3029 		log.u_bbr.bw_inuse <<= 32;
3030 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3031 		TCP_LOG_EVENTP(tp, NULL,
3032 		    &rack->rc_inp->inp_socket->so_rcv,
3033 		    &rack->rc_inp->inp_socket->so_snd,
3034 		    BBR_LOG_PROGRESS, 0,
3035 		    0, &log, false, &tv);
3036 	}
3037 }
3038 
3039 static void
rack_log_type_bbrsnd(struct tcp_rack * rack,uint32_t len,uint32_t slot,uint32_t cts,struct timeval * tv,int line)3040 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line)
3041 {
3042 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3043 		union tcp_log_stackspecific log;
3044 
3045 		memset(&log, 0, sizeof(log));
3046 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3047 		log.u_bbr.flex1 = slot;
3048 		if (rack->rack_no_prr)
3049 			log.u_bbr.flex2 = 0;
3050 		else
3051 			log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
3052 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3053 		log.u_bbr.flex6 = line;
3054 		log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
3055 		log.u_bbr.flex8 = rack->rc_in_persist;
3056 		log.u_bbr.timeStamp = cts;
3057 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3058 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3059 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3060 		log.u_bbr.pacing_gain = rack->r_must_retran;
3061 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3062 		    &rack->rc_inp->inp_socket->so_rcv,
3063 		    &rack->rc_inp->inp_socket->so_snd,
3064 		    BBR_LOG_BBRSND, 0,
3065 		    0, &log, false, tv);
3066 	}
3067 }
3068 
3069 static void
rack_log_doseg_done(struct tcp_rack * rack,uint32_t cts,int32_t nxt_pkt,int32_t did_out,int way_out,int nsegs)3070 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
3071 {
3072 	if (tcp_bblogging_on(rack->rc_tp)) {
3073 		union tcp_log_stackspecific log;
3074 		struct timeval tv;
3075 
3076 		memset(&log, 0, sizeof(log));
3077 		log.u_bbr.flex1 = did_out;
3078 		log.u_bbr.flex2 = nxt_pkt;
3079 		log.u_bbr.flex3 = way_out;
3080 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3081 		if (rack->rack_no_prr)
3082 			log.u_bbr.flex5 = 0;
3083 		else
3084 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3085 		log.u_bbr.flex6 = nsegs;
3086 		log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
3087 		log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;	/* Do we have ack-can-send set */
3088 		log.u_bbr.flex7 <<= 1;
3089 		log.u_bbr.flex7 |= rack->r_fast_output;	/* is fast output primed */
3090 		log.u_bbr.flex7 <<= 1;
3091 		log.u_bbr.flex7 |= rack->r_wanted_output;	/* Do we want output */
3092 		log.u_bbr.flex8 = rack->rc_in_persist;
3093 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3094 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3095 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3096 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3097 		log.u_bbr.use_lt_bw <<= 1;
3098 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3099 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3100 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3101 		log.u_bbr.pacing_gain = rack->r_must_retran;
3102 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3103 		log.u_bbr.bw_inuse <<= 32;
3104 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3105 		log.u_bbr.epoch = rack->rc_inp->inp_socket->so_snd.sb_hiwat;
3106 		log.u_bbr.lt_epoch = rack->rc_inp->inp_socket->so_rcv.sb_hiwat;
3107 		log.u_bbr.lost = rack->rc_tp->t_srtt;
3108 		log.u_bbr.pkt_epoch = rack->rc_tp->rfbuf_cnt;
3109 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3110 		    &rack->rc_inp->inp_socket->so_rcv,
3111 		    &rack->rc_inp->inp_socket->so_snd,
3112 		    BBR_LOG_DOSEG_DONE, 0,
3113 		    0, &log, false, &tv);
3114 	}
3115 }
3116 
3117 static void
rack_log_type_pacing_sizes(struct tcpcb * tp,struct tcp_rack * rack,uint32_t arg1,uint32_t arg2,uint32_t arg3,uint8_t frm)3118 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
3119 {
3120 	if (tcp_bblogging_on(rack->rc_tp)) {
3121 		union tcp_log_stackspecific log;
3122 		struct timeval tv;
3123 
3124 		memset(&log, 0, sizeof(log));
3125 		log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
3126 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
3127 		log.u_bbr.flex4 = arg1;
3128 		log.u_bbr.flex5 = arg2;
3129 		log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs;
3130 		log.u_bbr.flex6 = arg3;
3131 		log.u_bbr.flex8 = frm;
3132 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3133 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3134 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3135 		log.u_bbr.applimited = rack->r_ctl.rc_sacked;
3136 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3137 		log.u_bbr.pacing_gain = rack->r_must_retran;
3138 		TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
3139 		    &tptosocket(tp)->so_snd,
3140 		    TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv);
3141 	}
3142 }
3143 
3144 static void
rack_log_type_just_return(struct tcp_rack * rack,uint32_t cts,uint32_t tlen,uint32_t slot,uint8_t hpts_calling,int reason,uint32_t cwnd_to_use)3145 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
3146 			  uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
3147 {
3148 	if (tcp_bblogging_on(rack->rc_tp)) {
3149 		union tcp_log_stackspecific log;
3150 		struct timeval tv;
3151 
3152 		memset(&log, 0, sizeof(log));
3153 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3154 		log.u_bbr.flex1 = slot;
3155 		log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
3156 		log.u_bbr.flex4 = reason;
3157 		if (rack->rack_no_prr)
3158 			log.u_bbr.flex5 = 0;
3159 		else
3160 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3161 		log.u_bbr.flex7 = hpts_calling;
3162 		log.u_bbr.flex8 = rack->rc_in_persist;
3163 		log.u_bbr.lt_epoch = cwnd_to_use;
3164 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3165 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3166 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3167 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3168 		log.u_bbr.pacing_gain = rack->r_must_retran;
3169 		log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
3170 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3171 		log.u_bbr.bw_inuse <<= 32;
3172 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3173 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3174 		    &rack->rc_inp->inp_socket->so_rcv,
3175 		    &rack->rc_inp->inp_socket->so_snd,
3176 		    BBR_LOG_JUSTRET, 0,
3177 		    tlen, &log, false, &tv);
3178 	}
3179 }
3180 
3181 static void
rack_log_to_cancel(struct tcp_rack * rack,int32_t hpts_removed,int line,uint32_t us_cts,struct timeval * tv,uint32_t flags_on_entry)3182 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
3183 		   struct timeval *tv, uint32_t flags_on_entry)
3184 {
3185 	if (tcp_bblogging_on(rack->rc_tp)) {
3186 		union tcp_log_stackspecific log;
3187 
3188 		memset(&log, 0, sizeof(log));
3189 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3190 		log.u_bbr.flex1 = line;
3191 		log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
3192 		log.u_bbr.flex3 = flags_on_entry;
3193 		log.u_bbr.flex4 = us_cts;
3194 		if (rack->rack_no_prr)
3195 			log.u_bbr.flex5 = 0;
3196 		else
3197 			log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3198 		log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3199 		log.u_bbr.flex7 = hpts_removed;
3200 		log.u_bbr.flex8 = 1;
3201 		log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
3202 		log.u_bbr.timeStamp = us_cts;
3203 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3204 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3205 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3206 		log.u_bbr.pacing_gain = rack->r_must_retran;
3207 		log.u_bbr.bw_inuse = rack->r_ctl.current_round;
3208 		log.u_bbr.bw_inuse <<= 32;
3209 		log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost;
3210 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3211 		    &rack->rc_inp->inp_socket->so_rcv,
3212 		    &rack->rc_inp->inp_socket->so_snd,
3213 		    BBR_LOG_TIMERCANC, 0,
3214 		    0, &log, false, tv);
3215 	}
3216 }
3217 
3218 static void
rack_log_alt_to_to_cancel(struct tcp_rack * rack,uint32_t flex1,uint32_t flex2,uint32_t flex3,uint32_t flex4,uint32_t flex5,uint32_t flex6,uint16_t flex7,uint8_t mod)3219 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
3220 			  uint32_t flex1, uint32_t flex2,
3221 			  uint32_t flex3, uint32_t flex4,
3222 			  uint32_t flex5, uint32_t flex6,
3223 			  uint16_t flex7, uint8_t mod)
3224 {
3225 	if (tcp_bblogging_on(rack->rc_tp)) {
3226 		union tcp_log_stackspecific log;
3227 		struct timeval tv;
3228 
3229 		if (mod == 1) {
3230 			/* No you can't use 1, its for the real to cancel */
3231 			return;
3232 		}
3233 		memset(&log, 0, sizeof(log));
3234 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3235 		log.u_bbr.flex1 = flex1;
3236 		log.u_bbr.flex2 = flex2;
3237 		log.u_bbr.flex3 = flex3;
3238 		log.u_bbr.flex4 = flex4;
3239 		log.u_bbr.flex5 = flex5;
3240 		log.u_bbr.flex6 = flex6;
3241 		log.u_bbr.flex7 = flex7;
3242 		log.u_bbr.flex8 = mod;
3243 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3244 		    &rack->rc_inp->inp_socket->so_rcv,
3245 		    &rack->rc_inp->inp_socket->so_snd,
3246 		    BBR_LOG_TIMERCANC, 0,
3247 		    0, &log, false, &tv);
3248 	}
3249 }
3250 
3251 static void
rack_log_to_processing(struct tcp_rack * rack,uint32_t cts,int32_t ret,int32_t timers)3252 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
3253 {
3254 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
3255 		union tcp_log_stackspecific log;
3256 		struct timeval tv;
3257 
3258 		memset(&log, 0, sizeof(log));
3259 		log.u_bbr.flex1 = timers;
3260 		log.u_bbr.flex2 = ret;
3261 		log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
3262 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3263 		log.u_bbr.flex5 = cts;
3264 		if (rack->rack_no_prr)
3265 			log.u_bbr.flex6 = 0;
3266 		else
3267 			log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
3268 		log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3269 		log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3270 		log.u_bbr.pacing_gain = rack->r_must_retran;
3271 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3272 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3273 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3274 		    &rack->rc_inp->inp_socket->so_rcv,
3275 		    &rack->rc_inp->inp_socket->so_snd,
3276 		    BBR_LOG_TO_PROCESS, 0,
3277 		    0, &log, false, &tv);
3278 	}
3279 }
3280 
3281 static void
rack_log_to_prr(struct tcp_rack * rack,int frm,int orig_cwnd,int line)3282 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
3283 {
3284 	if (tcp_bblogging_on(rack->rc_tp)) {
3285 		union tcp_log_stackspecific log;
3286 		struct timeval tv;
3287 
3288 		memset(&log, 0, sizeof(log));
3289 		log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
3290 		log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
3291 		if (rack->rack_no_prr)
3292 			log.u_bbr.flex3 = 0;
3293 		else
3294 			log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
3295 		log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
3296 		log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
3297 		log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
3298 		log.u_bbr.flex7 = line;
3299 		log.u_bbr.flex8 = frm;
3300 		log.u_bbr.pkts_out = orig_cwnd;
3301 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3302 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3303 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3304 		log.u_bbr.use_lt_bw <<= 1;
3305 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
3306 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3307 		    &rack->rc_inp->inp_socket->so_rcv,
3308 		    &rack->rc_inp->inp_socket->so_snd,
3309 		    BBR_LOG_BBRUPD, 0,
3310 		    0, &log, false, &tv);
3311 	}
3312 }
3313 
3314 static void
rack_counter_destroy(void)3315 rack_counter_destroy(void)
3316 {
3317 	counter_u64_free(rack_total_bytes);
3318 	counter_u64_free(rack_fto_send);
3319 	counter_u64_free(rack_fto_rsm_send);
3320 	counter_u64_free(rack_nfto_resend);
3321 	counter_u64_free(rack_hw_pace_init_fail);
3322 	counter_u64_free(rack_hw_pace_lost);
3323 	counter_u64_free(rack_non_fto_send);
3324 	counter_u64_free(rack_extended_rfo);
3325 	counter_u64_free(rack_ack_total);
3326 	counter_u64_free(rack_express_sack);
3327 	counter_u64_free(rack_sack_total);
3328 	counter_u64_free(rack_move_none);
3329 	counter_u64_free(rack_move_some);
3330 	counter_u64_free(rack_sack_attacks_detected);
3331 	counter_u64_free(rack_sack_attacks_reversed);
3332 	counter_u64_free(rack_sack_attacks_suspect);
3333 	counter_u64_free(rack_sack_used_next_merge);
3334 	counter_u64_free(rack_sack_used_prev_merge);
3335 	counter_u64_free(rack_tlp_tot);
3336 	counter_u64_free(rack_tlp_newdata);
3337 	counter_u64_free(rack_tlp_retran);
3338 	counter_u64_free(rack_tlp_retran_bytes);
3339 	counter_u64_free(rack_to_tot);
3340 	counter_u64_free(rack_saw_enobuf);
3341 	counter_u64_free(rack_saw_enobuf_hw);
3342 	counter_u64_free(rack_saw_enetunreach);
3343 	counter_u64_free(rack_hot_alloc);
3344 	counter_u64_free(rack_to_alloc);
3345 	counter_u64_free(rack_to_alloc_hard);
3346 	counter_u64_free(rack_to_alloc_emerg);
3347 	counter_u64_free(rack_to_alloc_limited);
3348 	counter_u64_free(rack_alloc_limited_conns);
3349 	counter_u64_free(rack_split_limited);
3350 	counter_u64_free(rack_multi_single_eq);
3351 	counter_u64_free(rack_rxt_clamps_cwnd);
3352 	counter_u64_free(rack_rxt_clamps_cwnd_uniq);
3353 	counter_u64_free(rack_proc_non_comp_ack);
3354 	counter_u64_free(rack_sack_proc_all);
3355 	counter_u64_free(rack_sack_proc_restart);
3356 	counter_u64_free(rack_sack_proc_short);
3357 	counter_u64_free(rack_sack_skipped_acked);
3358 	counter_u64_free(rack_sack_splits);
3359 	counter_u64_free(rack_input_idle_reduces);
3360 	counter_u64_free(rack_collapsed_win);
3361 	counter_u64_free(rack_collapsed_win_rxt);
3362 	counter_u64_free(rack_collapsed_win_rxt_bytes);
3363 	counter_u64_free(rack_collapsed_win_seen);
3364 	counter_u64_free(rack_try_scwnd);
3365 	counter_u64_free(rack_persists_sends);
3366 	counter_u64_free(rack_persists_acks);
3367 	counter_u64_free(rack_persists_loss);
3368 	counter_u64_free(rack_persists_lost_ends);
3369 #ifdef INVARIANTS
3370 	counter_u64_free(rack_adjust_map_bw);
3371 #endif
3372 	COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
3373 	COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
3374 }
3375 
3376 static struct rack_sendmap *
rack_alloc(struct tcp_rack * rack)3377 rack_alloc(struct tcp_rack *rack)
3378 {
3379 	struct rack_sendmap *rsm;
3380 
3381 	/*
3382 	 * First get the top of the list it in
3383 	 * theory is the "hottest" rsm we have,
3384 	 * possibly just freed by ack processing.
3385 	 */
3386 	if (rack->rc_free_cnt > rack_free_cache) {
3387 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3388 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3389 		counter_u64_add(rack_hot_alloc, 1);
3390 		rack->rc_free_cnt--;
3391 		return (rsm);
3392 	}
3393 	/*
3394 	 * Once we get under our free cache we probably
3395 	 * no longer have a "hot" one available. Lets
3396 	 * get one from UMA.
3397 	 */
3398 	rsm = uma_zalloc(rack_zone, M_NOWAIT);
3399 	if (rsm) {
3400 		rack->r_ctl.rc_num_maps_alloced++;
3401 		counter_u64_add(rack_to_alloc, 1);
3402 		return (rsm);
3403 	}
3404 	/*
3405 	 * Dig in to our aux rsm's (the last two) since
3406 	 * UMA failed to get us one.
3407 	 */
3408 	if (rack->rc_free_cnt) {
3409 		counter_u64_add(rack_to_alloc_emerg, 1);
3410 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3411 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3412 		rack->rc_free_cnt--;
3413 		return (rsm);
3414 	}
3415 	return (NULL);
3416 }
3417 
3418 static struct rack_sendmap *
rack_alloc_full_limit(struct tcp_rack * rack)3419 rack_alloc_full_limit(struct tcp_rack *rack)
3420 {
3421 	if ((V_tcp_map_entries_limit > 0) &&
3422 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3423 		counter_u64_add(rack_to_alloc_limited, 1);
3424 		if (!rack->alloc_limit_reported) {
3425 			rack->alloc_limit_reported = 1;
3426 			counter_u64_add(rack_alloc_limited_conns, 1);
3427 		}
3428 		return (NULL);
3429 	}
3430 	return (rack_alloc(rack));
3431 }
3432 
3433 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3434 static struct rack_sendmap *
rack_alloc_limit(struct tcp_rack * rack,uint8_t limit_type)3435 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3436 {
3437 	struct rack_sendmap *rsm;
3438 
3439 	if (limit_type) {
3440 		/* currently there is only one limit type */
3441 		if (rack->r_ctl.rc_split_limit > 0 &&
3442 		    rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) {
3443 			counter_u64_add(rack_split_limited, 1);
3444 			if (!rack->alloc_limit_reported) {
3445 				rack->alloc_limit_reported = 1;
3446 				counter_u64_add(rack_alloc_limited_conns, 1);
3447 			}
3448 			return (NULL);
3449 		}
3450 	}
3451 
3452 	/* allocate and mark in the limit type, if set */
3453 	rsm = rack_alloc(rack);
3454 	if (rsm != NULL && limit_type) {
3455 		rsm->r_limit_type = limit_type;
3456 		rack->r_ctl.rc_num_split_allocs++;
3457 	}
3458 	return (rsm);
3459 }
3460 
3461 static void
rack_free_trim(struct tcp_rack * rack)3462 rack_free_trim(struct tcp_rack *rack)
3463 {
3464 	struct rack_sendmap *rsm;
3465 
3466 	/*
3467 	 * Free up all the tail entries until
3468 	 * we get our list down to the limit.
3469 	 */
3470 	while (rack->rc_free_cnt > rack_free_cache) {
3471 		rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3472 		TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3473 		rack->rc_free_cnt--;
3474 		rack->r_ctl.rc_num_maps_alloced--;
3475 		uma_zfree(rack_zone, rsm);
3476 	}
3477 }
3478 
3479 static void
rack_free(struct tcp_rack * rack,struct rack_sendmap * rsm)3480 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3481 {
3482 	if (rsm->r_flags & RACK_APP_LIMITED) {
3483 		if (rack->r_ctl.rc_app_limited_cnt > 0) {
3484 			rack->r_ctl.rc_app_limited_cnt--;
3485 		}
3486 	}
3487 	if (rsm->r_limit_type) {
3488 		/* currently there is only one limit type */
3489 		rack->r_ctl.rc_num_split_allocs--;
3490 	}
3491 	if (rsm == rack->r_ctl.rc_first_appl) {
3492 		rack->r_ctl.cleared_app_ack_seq = rsm->r_start + (rsm->r_end - rsm->r_start);
3493 		rack->r_ctl.cleared_app_ack = 1;
3494 		if (rack->r_ctl.rc_app_limited_cnt == 0)
3495 			rack->r_ctl.rc_first_appl = NULL;
3496 		else
3497 			rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl);
3498 	}
3499 	if (rsm == rack->r_ctl.rc_resend)
3500 		rack->r_ctl.rc_resend = NULL;
3501 	if (rsm == rack->r_ctl.rc_end_appl)
3502 		rack->r_ctl.rc_end_appl = NULL;
3503 	if (rack->r_ctl.rc_tlpsend == rsm)
3504 		rack->r_ctl.rc_tlpsend = NULL;
3505 	if (rack->r_ctl.rc_sacklast == rsm)
3506 		rack->r_ctl.rc_sacklast = NULL;
3507 	memset(rsm, 0, sizeof(struct rack_sendmap));
3508 	/* Make sure we are not going to overrun our count limit of 0xff */
3509 	if ((rack->rc_free_cnt + 1) > RACK_FREE_CNT_MAX) {
3510 		rack_free_trim(rack);
3511 	}
3512 	TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3513 	rack->rc_free_cnt++;
3514 }
3515 
3516 static uint32_t
rack_get_measure_window(struct tcpcb * tp,struct tcp_rack * rack)3517 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3518 {
3519 	uint64_t srtt, bw, len, tim;
3520 	uint32_t segsiz, def_len, minl;
3521 
3522 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3523 	def_len = rack_def_data_window * segsiz;
3524 	if (rack->rc_gp_filled == 0) {
3525 		/*
3526 		 * We have no measurement (IW is in flight?) so
3527 		 * we can only guess using our data_window sysctl
3528 		 * value (usually 20MSS).
3529 		 */
3530 		return (def_len);
3531 	}
3532 	/*
3533 	 * Now we have a number of factors to consider.
3534 	 *
3535 	 * 1) We have a desired BDP which is usually
3536 	 *    at least 2.
3537 	 * 2) We have a minimum number of rtt's usually 1 SRTT
3538 	 *    but we allow it too to be more.
3539 	 * 3) We want to make sure a measurement last N useconds (if
3540 	 *    we have set rack_min_measure_usec.
3541 	 *
3542 	 * We handle the first concern here by trying to create a data
3543 	 * window of max(rack_def_data_window, DesiredBDP). The
3544 	 * second concern we handle in not letting the measurement
3545 	 * window end normally until at least the required SRTT's
3546 	 * have gone by which is done further below in
3547 	 * rack_enough_for_measurement(). Finally the third concern
3548 	 * we also handle here by calculating how long that time
3549 	 * would take at the current BW and then return the
3550 	 * max of our first calculation and that length. Note
3551 	 * that if rack_min_measure_usec is 0, we don't deal
3552 	 * with concern 3. Also for both Concern 1 and 3 an
3553 	 * application limited period could end the measurement
3554 	 * earlier.
3555 	 *
3556 	 * So lets calculate the BDP with the "known" b/w using
3557 	 * the SRTT has our rtt and then multiply it by the
3558 	 * goal.
3559 	 */
3560 	bw = rack_get_bw(rack);
3561 	srtt = (uint64_t)tp->t_srtt;
3562 	len = bw * srtt;
3563 	len /= (uint64_t)HPTS_USEC_IN_SEC;
3564 	len *= max(1, rack_goal_bdp);
3565 	/* Now we need to round up to the nearest MSS */
3566 	len = roundup(len, segsiz);
3567 	if (rack_min_measure_usec) {
3568 		/* Now calculate our min length for this b/w */
3569 		tim = rack_min_measure_usec;
3570 		minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3571 		if (minl == 0)
3572 			minl = 1;
3573 		minl = roundup(minl, segsiz);
3574 		if (len < minl)
3575 			len = minl;
3576 	}
3577 	/*
3578 	 * Now if we have a very small window we want
3579 	 * to attempt to get the window that is
3580 	 * as small as possible. This happens on
3581 	 * low b/w connections and we don't want to
3582 	 * span huge numbers of rtt's between measurements.
3583 	 *
3584 	 * We basically include 2 over our "MIN window" so
3585 	 * that the measurement can be shortened (possibly) by
3586 	 * an ack'ed packet.
3587 	 */
3588 	if (len < def_len)
3589 		return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3590 	else
3591 		return (max((uint32_t)len, def_len));
3592 
3593 }
3594 
3595 static int
rack_enough_for_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq th_ack,uint8_t * quality)3596 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3597 {
3598 	uint32_t tim, srtts, segsiz;
3599 
3600 	/*
3601 	 * Has enough time passed for the GP measurement to be valid?
3602 	 */
3603 	if (SEQ_LT(th_ack, tp->gput_seq)) {
3604 		/* Not enough bytes yet */
3605 		return (0);
3606 	}
3607 	if ((tp->snd_max == tp->snd_una) ||
3608 	    (th_ack == tp->snd_max)){
3609 		/*
3610 		 * All is acked quality of all acked is
3611 		 * usually low or medium, but we in theory could split
3612 		 * all acked into two cases, where you got
3613 		 * a signifigant amount of your window and
3614 		 * where you did not. For now we leave it
3615 		 * but it is something to contemplate in the
3616 		 * future. The danger here is that delayed ack
3617 		 * is effecting the last byte (which is a 50:50 chance).
3618 		 */
3619 		*quality = RACK_QUALITY_ALLACKED;
3620 		return (1);
3621 	}
3622 	if (SEQ_GEQ(th_ack,  tp->gput_ack)) {
3623 		/*
3624 		 * We obtained our entire window of data we wanted
3625 		 * no matter if we are in recovery or not then
3626 		 * its ok since expanding the window does not
3627 		 * make things fuzzy (or at least not as much).
3628 		 */
3629 		*quality = RACK_QUALITY_HIGH;
3630 		return (1);
3631 	}
3632 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3633 	if (SEQ_LT(th_ack, tp->gput_ack) &&
3634 	    ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3635 		/* Not enough bytes yet */
3636 		return (0);
3637 	}
3638 	if (rack->r_ctl.rc_first_appl &&
3639 	    (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3640 		/*
3641 		 * We are up to the app limited send point
3642 		 * we have to measure irrespective of the time..
3643 		 */
3644 		*quality = RACK_QUALITY_APPLIMITED;
3645 		return (1);
3646 	}
3647 	/* Now what about time? */
3648 	srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3649 	tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3650 	if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
3651 		/*
3652 		 * We do not allow a measurement if we are in recovery
3653 		 * that would shrink the goodput window we wanted.
3654 		 * This is to prevent cloudyness of when the last send
3655 		 * was actually made.
3656 		 */
3657 		*quality = RACK_QUALITY_HIGH;
3658 		return (1);
3659 	}
3660 	/* Nope not even a full SRTT has passed */
3661 	return (0);
3662 }
3663 
3664 static void
rack_log_timely(struct tcp_rack * rack,uint32_t logged,uint64_t cur_bw,uint64_t low_bnd,uint64_t up_bnd,int line,uint8_t method)3665 rack_log_timely(struct tcp_rack *rack,
3666 		uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3667 		uint64_t up_bnd, int line, uint8_t method)
3668 {
3669 	if (tcp_bblogging_on(rack->rc_tp)) {
3670 		union tcp_log_stackspecific log;
3671 		struct timeval tv;
3672 
3673 		memset(&log, 0, sizeof(log));
3674 		log.u_bbr.flex1 = logged;
3675 		log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3676 		log.u_bbr.flex2 <<= 4;
3677 		log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3678 		log.u_bbr.flex2 <<= 4;
3679 		log.u_bbr.flex2 |= rack->rc_gp_incr;
3680 		log.u_bbr.flex2 <<= 4;
3681 		log.u_bbr.flex2 |= rack->rc_gp_bwred;
3682 		log.u_bbr.flex3 = rack->rc_gp_incr;
3683 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3684 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3685 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3686 		log.u_bbr.flex7 = rack->rc_gp_bwred;
3687 		log.u_bbr.flex8 = method;
3688 		log.u_bbr.cur_del_rate = cur_bw;
3689 		log.u_bbr.delRate = low_bnd;
3690 		log.u_bbr.bw_inuse = up_bnd;
3691 		log.u_bbr.rttProp = rack_get_bw(rack);
3692 		log.u_bbr.pkt_epoch = line;
3693 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3694 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3695 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3696 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3697 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3698 		log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3699 		log.u_bbr.cwnd_gain <<= 1;
3700 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3701 		log.u_bbr.cwnd_gain <<= 1;
3702 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3703 		log.u_bbr.cwnd_gain <<= 1;
3704 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3705 		log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3706 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
3707 		    &rack->rc_inp->inp_socket->so_rcv,
3708 		    &rack->rc_inp->inp_socket->so_snd,
3709 		    TCP_TIMELY_WORK, 0,
3710 		    0, &log, false, &tv);
3711 	}
3712 }
3713 
3714 static int
rack_bw_can_be_raised(struct tcp_rack * rack,uint64_t cur_bw,uint64_t last_bw_est,uint16_t mult)3715 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3716 {
3717 	/*
3718 	 * Before we increase we need to know if
3719 	 * the estimate just made was less than
3720 	 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3721 	 *
3722 	 * If we already are pacing at a fast enough
3723 	 * rate to push us faster there is no sense of
3724 	 * increasing.
3725 	 *
3726 	 * We first caculate our actual pacing rate (ss or ca multiplier
3727 	 * times our cur_bw).
3728 	 *
3729 	 * Then we take the last measured rate and multipy by our
3730 	 * maximum pacing overage to give us a max allowable rate.
3731 	 *
3732 	 * If our act_rate is smaller than our max_allowable rate
3733 	 * then we should increase. Else we should hold steady.
3734 	 *
3735 	 */
3736 	uint64_t act_rate, max_allow_rate;
3737 
3738 	if (rack_timely_no_stopping)
3739 		return (1);
3740 
3741 	if ((cur_bw == 0) || (last_bw_est == 0)) {
3742 		/*
3743 		 * Initial startup case or
3744 		 * everything is acked case.
3745 		 */
3746 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3747 				__LINE__, 9);
3748 		return (1);
3749 	}
3750 	if (mult <= 100) {
3751 		/*
3752 		 * We can always pace at or slightly above our rate.
3753 		 */
3754 		rack_log_timely(rack,  mult, cur_bw, 0, 0,
3755 				__LINE__, 9);
3756 		return (1);
3757 	}
3758 	act_rate = cur_bw * (uint64_t)mult;
3759 	act_rate /= 100;
3760 	max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3761 	max_allow_rate /= 100;
3762 	if (act_rate < max_allow_rate) {
3763 		/*
3764 		 * Here the rate we are actually pacing at
3765 		 * is smaller than 10% above our last measurement.
3766 		 * This means we are pacing below what we would
3767 		 * like to try to achieve (plus some wiggle room).
3768 		 */
3769 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3770 				__LINE__, 9);
3771 		return (1);
3772 	} else {
3773 		/*
3774 		 * Here we are already pacing at least rack_max_per_above(10%)
3775 		 * what we are getting back. This indicates most likely
3776 		 * that we are being limited (cwnd/rwnd/app) and can't
3777 		 * get any more b/w. There is no sense of trying to
3778 		 * raise up the pacing rate its not speeding us up
3779 		 * and we already are pacing faster than we are getting.
3780 		 */
3781 		rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3782 				__LINE__, 8);
3783 		return (0);
3784 	}
3785 }
3786 
3787 static void
rack_validate_multipliers_at_or_above100(struct tcp_rack * rack)3788 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3789 {
3790 	/*
3791 	 * When we drag bottom, we want to assure
3792 	 * that no multiplier is below 1.0, if so
3793 	 * we want to restore it to at least that.
3794 	 */
3795 	if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3796 		/* This is unlikely we usually do not touch recovery */
3797 		rack->r_ctl.rack_per_of_gp_rec = 100;
3798 	}
3799 	if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3800 		rack->r_ctl.rack_per_of_gp_ca = 100;
3801 	}
3802 	if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3803 		rack->r_ctl.rack_per_of_gp_ss = 100;
3804 	}
3805 }
3806 
3807 static void
rack_validate_multipliers_at_or_below_100(struct tcp_rack * rack)3808 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3809 {
3810 	if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3811 		rack->r_ctl.rack_per_of_gp_ca = 100;
3812 	}
3813 	if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3814 		rack->r_ctl.rack_per_of_gp_ss = 100;
3815 	}
3816 }
3817 
3818 static void
rack_increase_bw_mul(struct tcp_rack * rack,int timely_says,uint64_t cur_bw,uint64_t last_bw_est,int override)3819 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3820 {
3821 	int32_t  calc, logged, plus;
3822 
3823 	logged = 0;
3824 
3825 	if (rack->rc_skip_timely)
3826 		return;
3827 	if (override) {
3828 		/*
3829 		 * override is passed when we are
3830 		 * loosing b/w and making one last
3831 		 * gasp at trying to not loose out
3832 		 * to a new-reno flow.
3833 		 */
3834 		goto extra_boost;
3835 	}
3836 	/* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3837 	if (rack->rc_gp_incr &&
3838 	    ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3839 		/*
3840 		 * Reset and get 5 strokes more before the boost. Note
3841 		 * that the count is 0 based so we have to add one.
3842 		 */
3843 extra_boost:
3844 		plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3845 		rack->rc_gp_timely_inc_cnt = 0;
3846 	} else
3847 		plus = (uint32_t)rack_gp_increase_per;
3848 	/* Must be at least 1% increase for true timely increases */
3849 	if ((plus < 1) &&
3850 	    ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3851 		plus = 1;
3852 	if (rack->rc_gp_saw_rec &&
3853 	    (rack->rc_gp_no_rec_chg == 0) &&
3854 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3855 				  rack->r_ctl.rack_per_of_gp_rec)) {
3856 		/* We have been in recovery ding it too */
3857 		calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3858 		if (calc > 0xffff)
3859 			calc = 0xffff;
3860 		logged |= 1;
3861 		rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3862 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3863 		    (rack->rc_dragged_bottom == 0) &&
3864 		    (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca))
3865 			rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca;
3866 	}
3867 	if (rack->rc_gp_saw_ca &&
3868 	    (rack->rc_gp_saw_ss == 0) &&
3869 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3870 				  rack->r_ctl.rack_per_of_gp_ca)) {
3871 		/* In CA */
3872 		calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3873 		if (calc > 0xffff)
3874 			calc = 0xffff;
3875 		logged |= 2;
3876 		rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3877 		if (rack->r_ctl.rack_per_upper_bound_ca &&
3878 		    (rack->rc_dragged_bottom == 0) &&
3879 		    (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca))
3880 			rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca;
3881 	}
3882 	if (rack->rc_gp_saw_ss &&
3883 	    rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3884 				  rack->r_ctl.rack_per_of_gp_ss)) {
3885 		/* In SS */
3886 		calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3887 		if (calc > 0xffff)
3888 			calc = 0xffff;
3889 		rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3890 		if (rack->r_ctl.rack_per_upper_bound_ss &&
3891 		    (rack->rc_dragged_bottom == 0) &&
3892 		    (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss))
3893 			rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss;
3894 		logged |= 4;
3895 	}
3896 	if (logged &&
3897 	    (rack->rc_gp_incr == 0)){
3898 		/* Go into increment mode */
3899 		rack->rc_gp_incr = 1;
3900 		rack->rc_gp_timely_inc_cnt = 0;
3901 	}
3902 	if (rack->rc_gp_incr &&
3903 	    logged &&
3904 	    (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3905 		rack->rc_gp_timely_inc_cnt++;
3906 	}
3907 	rack_log_timely(rack,  logged, plus, 0, 0,
3908 			__LINE__, 1);
3909 }
3910 
3911 static uint32_t
rack_get_decrease(struct tcp_rack * rack,uint32_t curper,int32_t rtt_diff)3912 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3913 {
3914 	/*-
3915 	 * norm_grad = rtt_diff / minrtt;
3916 	 * new_per = curper * (1 - B * norm_grad)
3917 	 *
3918 	 * B = rack_gp_decrease_per (default 80%)
3919 	 * rtt_dif = input var current rtt-diff
3920 	 * curper = input var current percentage
3921 	 * minrtt = from rack filter
3922 	 *
3923 	 * In order to do the floating point calculations above we
3924 	 * do an integer conversion. The code looks confusing so let me
3925 	 * translate it into something that use more variables and
3926 	 * is clearer for us humans :)
3927 	 *
3928 	 * uint64_t norm_grad, inverse, reduce_by, final_result;
3929 	 * uint32_t perf;
3930 	 *
3931 	 * norm_grad = (((uint64_t)rtt_diff * 1000000) /
3932 	 *             (uint64_t)get_filter_small(&rack->r_ctl.rc_gp_min_rtt));
3933 	 * inverse = ((uint64_t)rack_gp_decrease * (uint64_t)1000000) * norm_grad;
3934 	 * inverse /= 1000000;
3935 	 * reduce_by = (1000000 - inverse);
3936 	 * final_result = (cur_per * reduce_by) / 1000000;
3937 	 * perf = (uint32_t)final_result;
3938 	 */
3939 	uint64_t perf;
3940 
3941 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3942 		    ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3943 		     (((uint64_t)rtt_diff * (uint64_t)1000000)/
3944 		      (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3945 		     (uint64_t)1000000)) /
3946 		(uint64_t)1000000);
3947 	if (perf > curper) {
3948 		/* TSNH */
3949 		perf = curper - 1;
3950 	}
3951 	return ((uint32_t)perf);
3952 }
3953 
3954 static uint32_t
rack_decrease_highrtt(struct tcp_rack * rack,uint32_t curper,uint32_t rtt)3955 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3956 {
3957 	/*
3958 	 *                                   highrttthresh
3959 	 * result = curper * (1 - (B * ( 1 -  ------          ))
3960 	 *                                     gp_srtt
3961 	 *
3962 	 * B = rack_gp_decrease_per (default .8 i.e. 80)
3963 	 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3964 	 */
3965 	uint64_t perf;
3966 	uint32_t highrttthresh;
3967 
3968 	highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3969 
3970 	perf = (((uint64_t)curper * ((uint64_t)1000000 -
3971 				     ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3972 					((uint64_t)highrttthresh * (uint64_t)1000000) /
3973 						    (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3974 	if (tcp_bblogging_on(rack->rc_tp)) {
3975 		uint64_t log1;
3976 
3977 		log1 = rtt;
3978 		log1 <<= 32;
3979 		log1 |= highrttthresh;
3980 		rack_log_timely(rack,
3981 				rack_gp_decrease_per,
3982 				(uint64_t)curper,
3983 				log1,
3984 				perf,
3985 				__LINE__,
3986 				15);
3987 	}
3988 	return (perf);
3989 }
3990 
3991 static void
rack_decrease_bw_mul(struct tcp_rack * rack,int timely_says,uint32_t rtt,int32_t rtt_diff)3992 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3993 {
3994 	uint64_t logvar, logvar2, logvar3;
3995 	uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3996 
3997 	if (rack->rc_skip_timely)
3998 		return;
3999 	if (rack->rc_gp_incr) {
4000 		/* Turn off increment counting */
4001 		rack->rc_gp_incr = 0;
4002 		rack->rc_gp_timely_inc_cnt = 0;
4003 	}
4004 	ss_red = ca_red = rec_red = 0;
4005 	logged = 0;
4006 	/* Calculate the reduction value */
4007 	if (rtt_diff < 0) {
4008 		rtt_diff *= -1;
4009 	}
4010 	/* Must be at least 1% reduction */
4011 	if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
4012 		/* We have been in recovery ding it too */
4013 		if (timely_says == 2) {
4014 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
4015 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4016 			if (alt < new_per)
4017 				val = alt;
4018 			else
4019 				val = new_per;
4020 		} else
4021 			 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
4022 		if (rack->r_ctl.rack_per_of_gp_rec > val) {
4023 			rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
4024 			rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
4025 		} else {
4026 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4027 			rec_red = 0;
4028 		}
4029 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
4030 			rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
4031 		logged |= 1;
4032 	}
4033 	if (rack->rc_gp_saw_ss) {
4034 		/* Sent in SS */
4035 		if (timely_says == 2) {
4036 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
4037 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4038 			if (alt < new_per)
4039 				val = alt;
4040 			else
4041 				val = new_per;
4042 		} else
4043 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
4044 		if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
4045 			ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
4046 			rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
4047 		} else {
4048 			ss_red = new_per;
4049 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4050 			logvar = new_per;
4051 			logvar <<= 32;
4052 			logvar |= alt;
4053 			logvar2 = (uint32_t)rtt;
4054 			logvar2 <<= 32;
4055 			logvar2 |= (uint32_t)rtt_diff;
4056 			logvar3 = rack_gp_rtt_maxmul;
4057 			logvar3 <<= 32;
4058 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4059 			rack_log_timely(rack, timely_says,
4060 					logvar2, logvar3,
4061 					logvar, __LINE__, 10);
4062 		}
4063 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
4064 			rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
4065 		logged |= 4;
4066 	} else if (rack->rc_gp_saw_ca) {
4067 		/* Sent in CA */
4068 		if (timely_says == 2) {
4069 			new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
4070 			alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4071 			if (alt < new_per)
4072 				val = alt;
4073 			else
4074 				val = new_per;
4075 		} else
4076 			val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
4077 		if (rack->r_ctl.rack_per_of_gp_ca > val) {
4078 			ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
4079 			rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
4080 		} else {
4081 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4082 			ca_red = 0;
4083 			logvar = new_per;
4084 			logvar <<= 32;
4085 			logvar |= alt;
4086 			logvar2 = (uint32_t)rtt;
4087 			logvar2 <<= 32;
4088 			logvar2 |= (uint32_t)rtt_diff;
4089 			logvar3 = rack_gp_rtt_maxmul;
4090 			logvar3 <<= 32;
4091 			logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4092 			rack_log_timely(rack, timely_says,
4093 					logvar2, logvar3,
4094 					logvar, __LINE__, 10);
4095 		}
4096 		if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
4097 			rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
4098 		logged |= 2;
4099 	}
4100 	if (rack->rc_gp_timely_dec_cnt < 0x7) {
4101 		rack->rc_gp_timely_dec_cnt++;
4102 		if (rack_timely_dec_clear &&
4103 		    (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
4104 			rack->rc_gp_timely_dec_cnt = 0;
4105 	}
4106 	logvar = ss_red;
4107 	logvar <<= 32;
4108 	logvar |= ca_red;
4109 	rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
4110 			__LINE__, 2);
4111 }
4112 
4113 static void
rack_log_rtt_shrinks(struct tcp_rack * rack,uint32_t us_cts,uint32_t rtt,uint32_t line,uint8_t reas)4114 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
4115 		     uint32_t rtt, uint32_t line, uint8_t reas)
4116 {
4117 	if (tcp_bblogging_on(rack->rc_tp)) {
4118 		union tcp_log_stackspecific log;
4119 		struct timeval tv;
4120 
4121 		memset(&log, 0, sizeof(log));
4122 		log.u_bbr.flex1 = line;
4123 		log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
4124 		log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
4125 		log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
4126 		log.u_bbr.flex5 = rtt;
4127 		log.u_bbr.flex6 = rack->rc_highly_buffered;
4128 		log.u_bbr.flex6 <<= 1;
4129 		log.u_bbr.flex6 |= rack->forced_ack;
4130 		log.u_bbr.flex6 <<= 1;
4131 		log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
4132 		log.u_bbr.flex6 <<= 1;
4133 		log.u_bbr.flex6 |= rack->in_probe_rtt;
4134 		log.u_bbr.flex6 <<= 1;
4135 		log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
4136 		log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
4137 		log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
4138 		log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
4139 		log.u_bbr.flex8 = reas;
4140 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4141 		log.u_bbr.delRate = rack_get_bw(rack);
4142 		log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
4143 		log.u_bbr.cur_del_rate <<= 32;
4144 		log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
4145 		log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
4146 		log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
4147 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
4148 		log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
4149 		log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
4150 		log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
4151 		log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
4152 		log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4153 		log.u_bbr.rttProp = us_cts;
4154 		log.u_bbr.rttProp <<= 32;
4155 		log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
4156 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4157 		    &rack->rc_inp->inp_socket->so_rcv,
4158 		    &rack->rc_inp->inp_socket->so_snd,
4159 		    BBR_LOG_RTT_SHRINKS, 0,
4160 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4161 	}
4162 }
4163 
4164 static void
rack_set_prtt_target(struct tcp_rack * rack,uint32_t segsiz,uint32_t rtt)4165 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
4166 {
4167 	uint64_t bwdp;
4168 
4169 	bwdp = rack_get_bw(rack);
4170 	bwdp *= (uint64_t)rtt;
4171 	bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
4172 	rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
4173 	if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
4174 		/*
4175 		 * A window protocol must be able to have 4 packets
4176 		 * outstanding as the floor in order to function
4177 		 * (especially considering delayed ack :D).
4178 		 */
4179 		rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
4180 	}
4181 }
4182 
4183 static void
rack_enter_probertt(struct tcp_rack * rack,uint32_t us_cts)4184 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
4185 {
4186 	/**
4187 	 * ProbeRTT is a bit different in rack_pacing than in
4188 	 * BBR. It is like BBR in that it uses the lowering of
4189 	 * the RTT as a signal that we saw something new and
4190 	 * counts from there for how long between. But it is
4191 	 * different in that its quite simple. It does not
4192 	 * play with the cwnd and wait until we get down
4193 	 * to N segments outstanding and hold that for
4194 	 * 200ms. Instead it just sets the pacing reduction
4195 	 * rate to a set percentage (70 by default) and hold
4196 	 * that for a number of recent GP Srtt's.
4197 	 */
4198 	uint32_t segsiz;
4199 
4200 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4201 	if (rack->rc_gp_dyn_mul == 0)
4202 		return;
4203 
4204 	if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
4205 		/* We are idle */
4206 		return;
4207 	}
4208 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4209 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4210 		/*
4211 		 * Stop the goodput now, the idea here is
4212 		 * that future measurements with in_probe_rtt
4213 		 * won't register if they are not greater so
4214 		 * we want to get what info (if any) is available
4215 		 * now.
4216 		 */
4217 		rack_do_goodput_measurement(rack->rc_tp, rack,
4218 					    rack->rc_tp->snd_una, __LINE__,
4219 					    RACK_QUALITY_PROBERTT);
4220 	}
4221 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4222 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4223 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4224 		     rack->r_ctl.rc_pace_min_segs);
4225 	rack->in_probe_rtt = 1;
4226 	rack->measure_saw_probe_rtt = 1;
4227 	rack->r_ctl.rc_time_probertt_starts = 0;
4228 	rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
4229 	if (rack_probertt_use_min_rtt_entry)
4230 		rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4231 	else
4232 		rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
4233 	rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4234 			     __LINE__, RACK_RTTS_ENTERPROBE);
4235 }
4236 
4237 static void
rack_exit_probertt(struct tcp_rack * rack,uint32_t us_cts)4238 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
4239 {
4240 	struct rack_sendmap *rsm;
4241 	uint32_t segsiz;
4242 
4243 	segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4244 		     rack->r_ctl.rc_pace_min_segs);
4245 	rack->in_probe_rtt = 0;
4246 	if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4247 	    SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4248 		/*
4249 		 * Stop the goodput now, the idea here is
4250 		 * that future measurements with in_probe_rtt
4251 		 * won't register if they are not greater so
4252 		 * we want to get what info (if any) is available
4253 		 * now.
4254 		 */
4255 		rack_do_goodput_measurement(rack->rc_tp, rack,
4256 					    rack->rc_tp->snd_una, __LINE__,
4257 					    RACK_QUALITY_PROBERTT);
4258 	} else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
4259 		/*
4260 		 * We don't have enough data to make a measurement.
4261 		 * So lets just stop and start here after exiting
4262 		 * probe-rtt. We probably are not interested in
4263 		 * the results anyway.
4264 		 */
4265 		rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
4266 	}
4267 	/*
4268 	 * Measurements through the current snd_max are going
4269 	 * to be limited by the slower pacing rate.
4270 	 *
4271 	 * We need to mark these as app-limited so we
4272 	 * don't collapse the b/w.
4273 	 */
4274 	rsm = tqhash_max(rack->r_ctl.tqh);
4275 	if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
4276 		if (rack->r_ctl.rc_app_limited_cnt == 0)
4277 			rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
4278 		else {
4279 			/*
4280 			 * Go out to the end app limited and mark
4281 			 * this new one as next and move the end_appl up
4282 			 * to this guy.
4283 			 */
4284 			if (rack->r_ctl.rc_end_appl)
4285 				rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
4286 			rack->r_ctl.rc_end_appl = rsm;
4287 		}
4288 		rsm->r_flags |= RACK_APP_LIMITED;
4289 		rack->r_ctl.rc_app_limited_cnt++;
4290 	}
4291 	/*
4292 	 * Now, we need to examine our pacing rate multipliers.
4293 	 * If its under 100%, we need to kick it back up to
4294 	 * 100%. We also don't let it be over our "max" above
4295 	 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
4296 	 * Note setting clamp_atexit_prtt to 0 has the effect
4297 	 * of setting CA/SS to 100% always at exit (which is
4298 	 * the default behavior).
4299 	 */
4300 	if (rack_probertt_clear_is) {
4301 		rack->rc_gp_incr = 0;
4302 		rack->rc_gp_bwred = 0;
4303 		rack->rc_gp_timely_inc_cnt = 0;
4304 		rack->rc_gp_timely_dec_cnt = 0;
4305 	}
4306 	/* Do we do any clamping at exit? */
4307 	if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
4308 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
4309 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
4310 	}
4311 	if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
4312 		rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
4313 		rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
4314 	}
4315 	/*
4316 	 * Lets set rtt_diff to 0, so that we will get a "boost"
4317 	 * after exiting.
4318 	 */
4319 	rack->r_ctl.rc_rtt_diff = 0;
4320 
4321 	/* Clear all flags so we start fresh */
4322 	rack->rc_tp->t_bytes_acked = 0;
4323 	rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
4324 	/*
4325 	 * If configured to, set the cwnd and ssthresh to
4326 	 * our targets.
4327 	 */
4328 	if (rack_probe_rtt_sets_cwnd) {
4329 		uint64_t ebdp;
4330 		uint32_t setto;
4331 
4332 		/* Set ssthresh so we get into CA once we hit our target */
4333 		if (rack_probertt_use_min_rtt_exit == 1) {
4334 			/* Set to min rtt */
4335 			rack_set_prtt_target(rack, segsiz,
4336 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4337 		} else if (rack_probertt_use_min_rtt_exit == 2) {
4338 			/* Set to current gp rtt */
4339 			rack_set_prtt_target(rack, segsiz,
4340 					     rack->r_ctl.rc_gp_srtt);
4341 		} else if (rack_probertt_use_min_rtt_exit == 3) {
4342 			/* Set to entry gp rtt */
4343 			rack_set_prtt_target(rack, segsiz,
4344 					     rack->r_ctl.rc_entry_gp_rtt);
4345 		} else {
4346 			uint64_t sum;
4347 			uint32_t setval;
4348 
4349 			sum = rack->r_ctl.rc_entry_gp_rtt;
4350 			sum *= 10;
4351 			sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
4352 			if (sum >= 20) {
4353 				/*
4354 				 * A highly buffered path needs
4355 				 * cwnd space for timely to work.
4356 				 * Lets set things up as if
4357 				 * we are heading back here again.
4358 				 */
4359 				setval = rack->r_ctl.rc_entry_gp_rtt;
4360 			} else if (sum >= 15) {
4361 				/*
4362 				 * Lets take the smaller of the
4363 				 * two since we are just somewhat
4364 				 * buffered.
4365 				 */
4366 				setval = rack->r_ctl.rc_gp_srtt;
4367 				if (setval > rack->r_ctl.rc_entry_gp_rtt)
4368 					setval = rack->r_ctl.rc_entry_gp_rtt;
4369 			} else {
4370 				/*
4371 				 * Here we are not highly buffered
4372 				 * and should pick the min we can to
4373 				 * keep from causing loss.
4374 				 */
4375 				setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4376 			}
4377 			rack_set_prtt_target(rack, segsiz,
4378 					     setval);
4379 		}
4380 		if (rack_probe_rtt_sets_cwnd > 1) {
4381 			/* There is a percentage here to boost */
4382 			ebdp = rack->r_ctl.rc_target_probertt_flight;
4383 			ebdp *= rack_probe_rtt_sets_cwnd;
4384 			ebdp /= 100;
4385 			setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
4386 		} else
4387 			setto = rack->r_ctl.rc_target_probertt_flight;
4388 		rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
4389 		if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
4390 			/* Enforce a min */
4391 			rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
4392 		}
4393 		/* If we set in the cwnd also set the ssthresh point so we are in CA */
4394 		rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
4395 	}
4396 	rack_log_rtt_shrinks(rack,  us_cts,
4397 			     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4398 			     __LINE__, RACK_RTTS_EXITPROBE);
4399 	/* Clear times last so log has all the info */
4400 	rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
4401 	rack->r_ctl.rc_time_probertt_entered = us_cts;
4402 	rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4403 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
4404 }
4405 
4406 static void
rack_check_probe_rtt(struct tcp_rack * rack,uint32_t us_cts)4407 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
4408 {
4409 	/* Check in on probe-rtt */
4410 
4411 	if (rack->rc_gp_filled == 0) {
4412 		/* We do not do p-rtt unless we have gp measurements */
4413 		return;
4414 	}
4415 	if (rack->in_probe_rtt) {
4416 		uint64_t no_overflow;
4417 		uint32_t endtime, must_stay;
4418 
4419 		if (rack->r_ctl.rc_went_idle_time &&
4420 		    ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
4421 			/*
4422 			 * We went idle during prtt, just exit now.
4423 			 */
4424 			rack_exit_probertt(rack, us_cts);
4425 		} else if (rack_probe_rtt_safety_val &&
4426 		    TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
4427 		    ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
4428 			/*
4429 			 * Probe RTT safety value triggered!
4430 			 */
4431 			rack_log_rtt_shrinks(rack,  us_cts,
4432 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4433 					     __LINE__, RACK_RTTS_SAFETY);
4434 			rack_exit_probertt(rack, us_cts);
4435 		}
4436 		/* Calculate the max we will wait */
4437 		endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
4438 		if (rack->rc_highly_buffered)
4439 			endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
4440 		/* Calculate the min we must wait */
4441 		must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
4442 		if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
4443 		    TSTMP_LT(us_cts, endtime)) {
4444 			uint32_t calc;
4445 			/* Do we lower more? */
4446 no_exit:
4447 			if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
4448 				calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
4449 			else
4450 				calc = 0;
4451 			calc /= max(rack->r_ctl.rc_gp_srtt, 1);
4452 			if (calc) {
4453 				/* Maybe */
4454 				calc *= rack_per_of_gp_probertt_reduce;
4455 				if (calc > rack_per_of_gp_probertt)
4456 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4457 				else
4458 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4459 				/* Limit it too */
4460 				if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4461 					rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4462 			}
4463 			/* We must reach target or the time set */
4464 			return;
4465 		}
4466 		if (rack->r_ctl.rc_time_probertt_starts == 0) {
4467 			if ((TSTMP_LT(us_cts, must_stay) &&
4468 			     rack->rc_highly_buffered) ||
4469 			     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4470 			      rack->r_ctl.rc_target_probertt_flight)) {
4471 				/* We are not past the must_stay time */
4472 				goto no_exit;
4473 			}
4474 			rack_log_rtt_shrinks(rack,  us_cts,
4475 					     get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4476 					     __LINE__, RACK_RTTS_REACHTARGET);
4477 			rack->r_ctl.rc_time_probertt_starts = us_cts;
4478 			if (rack->r_ctl.rc_time_probertt_starts == 0)
4479 				rack->r_ctl.rc_time_probertt_starts = 1;
4480 			/* Restore back to our rate we want to pace at in prtt */
4481 			rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4482 		}
4483 		/*
4484 		 * Setup our end time, some number of gp_srtts plus 200ms.
4485 		 */
4486 		no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4487 			       (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4488 		if (rack_probertt_gpsrtt_cnt_div)
4489 			endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4490 		else
4491 			endtime = 0;
4492 		endtime += rack_min_probertt_hold;
4493 		endtime += rack->r_ctl.rc_time_probertt_starts;
4494 		if (TSTMP_GEQ(us_cts,  endtime)) {
4495 			/* yes, exit probertt */
4496 			rack_exit_probertt(rack, us_cts);
4497 		}
4498 
4499 	} else if ((rack->rc_skip_timely == 0) &&
4500 		   (TSTMP_GT(us_cts, rack->r_ctl.rc_lower_rtt_us_cts)) &&
4501 		   ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt)) {
4502 		/* Go into probertt, its been too long since we went lower */
4503 		rack_enter_probertt(rack, us_cts);
4504 	}
4505 }
4506 
4507 static void
rack_update_multiplier(struct tcp_rack * rack,int32_t timely_says,uint64_t last_bw_est,uint32_t rtt,int32_t rtt_diff)4508 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4509 		       uint32_t rtt, int32_t rtt_diff)
4510 {
4511 	uint64_t cur_bw, up_bnd, low_bnd, subfr;
4512 	uint32_t losses;
4513 
4514 	if ((rack->rc_gp_dyn_mul == 0) ||
4515 	    (rack->use_fixed_rate) ||
4516 	    (rack->in_probe_rtt) ||
4517 	    (rack->rc_always_pace == 0)) {
4518 		/* No dynamic GP multiplier in play */
4519 		return;
4520 	}
4521 	losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4522 	cur_bw = rack_get_bw(rack);
4523 	/* Calculate our up and down range */
4524 	up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4525 	up_bnd /= 100;
4526 	up_bnd += rack->r_ctl.last_gp_comp_bw;
4527 
4528 	subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4529 	subfr /= 100;
4530 	low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4531 	if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4532 		/*
4533 		 * This is the case where our RTT is above
4534 		 * the max target and we have been configured
4535 		 * to just do timely no bonus up stuff in that case.
4536 		 *
4537 		 * There are two configurations, set to 1, and we
4538 		 * just do timely if we are over our max. If its
4539 		 * set above 1 then we slam the multipliers down
4540 		 * to 100 and then decrement per timely.
4541 		 */
4542 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4543 				__LINE__, 3);
4544 		if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4545 			rack_validate_multipliers_at_or_below_100(rack);
4546 		rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4547 	} else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) {
4548 		/*
4549 		 * We are decreasing this is a bit complicated this
4550 		 * means we are loosing ground. This could be
4551 		 * because another flow entered and we are competing
4552 		 * for b/w with it. This will push the RTT up which
4553 		 * makes timely unusable unless we want to get shoved
4554 		 * into a corner and just be backed off (the age
4555 		 * old problem with delay based CC).
4556 		 *
4557 		 * On the other hand if it was a route change we
4558 		 * would like to stay somewhat contained and not
4559 		 * blow out the buffers.
4560 		 */
4561 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4562 				__LINE__, 3);
4563 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4564 		if (rack->rc_gp_bwred == 0) {
4565 			/* Go into reduction counting */
4566 			rack->rc_gp_bwred = 1;
4567 			rack->rc_gp_timely_dec_cnt = 0;
4568 		}
4569 		if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) {
4570 			/*
4571 			 * Push another time with a faster pacing
4572 			 * to try to gain back (we include override to
4573 			 * get a full raise factor).
4574 			 */
4575 			if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4576 			    (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4577 			    (timely_says == 0) ||
4578 			    (rack_down_raise_thresh == 0)) {
4579 				/*
4580 				 * Do an override up in b/w if we were
4581 				 * below the threshold or if the threshold
4582 				 * is zero we always do the raise.
4583 				 */
4584 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4585 			} else {
4586 				/* Log it stays the same */
4587 				rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4588 						__LINE__, 11);
4589 			}
4590 			rack->rc_gp_timely_dec_cnt++;
4591 			/* We are not incrementing really no-count */
4592 			rack->rc_gp_incr = 0;
4593 			rack->rc_gp_timely_inc_cnt = 0;
4594 		} else {
4595 			/*
4596 			 * Lets just use the RTT
4597 			 * information and give up
4598 			 * pushing.
4599 			 */
4600 			goto use_timely;
4601 		}
4602 	} else if ((timely_says != 2) &&
4603 		    !losses &&
4604 		    (last_bw_est > up_bnd)) {
4605 		/*
4606 		 * We are increasing b/w lets keep going, updating
4607 		 * our b/w and ignoring any timely input, unless
4608 		 * of course we are at our max raise (if there is one).
4609 		 */
4610 
4611 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4612 				__LINE__, 3);
4613 		rack->r_ctl.last_gp_comp_bw = cur_bw;
4614 		if (rack->rc_gp_saw_ss &&
4615 		    rack->r_ctl.rack_per_upper_bound_ss &&
4616 		     (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) {
4617 			    /*
4618 			     * In cases where we can't go higher
4619 			     * we should just use timely.
4620 			     */
4621 			    goto use_timely;
4622 		}
4623 		if (rack->rc_gp_saw_ca &&
4624 		    rack->r_ctl.rack_per_upper_bound_ca &&
4625 		    (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) {
4626 			    /*
4627 			     * In cases where we can't go higher
4628 			     * we should just use timely.
4629 			     */
4630 			    goto use_timely;
4631 		}
4632 		rack->rc_gp_bwred = 0;
4633 		rack->rc_gp_timely_dec_cnt = 0;
4634 		/* You get a set number of pushes if timely is trying to reduce */
4635 		if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4636 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4637 		} else {
4638 			/* Log it stays the same */
4639 			rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4640 			    __LINE__, 12);
4641 		}
4642 		return;
4643 	} else {
4644 		/*
4645 		 * We are staying between the lower and upper range bounds
4646 		 * so use timely to decide.
4647 		 */
4648 		rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4649 				__LINE__, 3);
4650 use_timely:
4651 		if (timely_says) {
4652 			rack->rc_gp_incr = 0;
4653 			rack->rc_gp_timely_inc_cnt = 0;
4654 			if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4655 			    !losses &&
4656 			    (last_bw_est < low_bnd)) {
4657 				/* We are loosing ground */
4658 				rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4659 				rack->rc_gp_timely_dec_cnt++;
4660 				/* We are not incrementing really no-count */
4661 				rack->rc_gp_incr = 0;
4662 				rack->rc_gp_timely_inc_cnt = 0;
4663 			} else
4664 				rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4665 		} else {
4666 			rack->rc_gp_bwred = 0;
4667 			rack->rc_gp_timely_dec_cnt = 0;
4668 			rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4669 		}
4670 	}
4671 }
4672 
4673 static int32_t
rack_make_timely_judgement(struct tcp_rack * rack,uint32_t rtt,int32_t rtt_diff,uint32_t prev_rtt)4674 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4675 {
4676 	int32_t timely_says;
4677 	uint64_t log_mult, log_rtt_a_diff;
4678 
4679 	log_rtt_a_diff = rtt;
4680 	log_rtt_a_diff <<= 32;
4681 	log_rtt_a_diff |= (uint32_t)rtt_diff;
4682 	if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4683 		    rack_gp_rtt_maxmul)) {
4684 		/* Reduce the b/w multiplier */
4685 		timely_says = 2;
4686 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4687 		log_mult <<= 32;
4688 		log_mult |= prev_rtt;
4689 		rack_log_timely(rack,  timely_says, log_mult,
4690 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4691 				log_rtt_a_diff, __LINE__, 4);
4692 	} else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4693 			   ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4694 			    max(rack_gp_rtt_mindiv , 1)))) {
4695 		/* Increase the b/w multiplier */
4696 		log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4697 			((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4698 			 max(rack_gp_rtt_mindiv , 1));
4699 		log_mult <<= 32;
4700 		log_mult |= prev_rtt;
4701 		timely_says = 0;
4702 		rack_log_timely(rack,  timely_says, log_mult ,
4703 				get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4704 				log_rtt_a_diff, __LINE__, 5);
4705 	} else {
4706 		/*
4707 		 * Use a gradient to find it the timely gradient
4708 		 * is:
4709 		 * grad = rc_rtt_diff / min_rtt;
4710 		 *
4711 		 * anything below or equal to 0 will be
4712 		 * a increase indication. Anything above
4713 		 * zero is a decrease. Note we take care
4714 		 * of the actual gradient calculation
4715 		 * in the reduction (its not needed for
4716 		 * increase).
4717 		 */
4718 		log_mult = prev_rtt;
4719 		if (rtt_diff <= 0) {
4720 			/*
4721 			 * Rttdiff is less than zero, increase the
4722 			 * b/w multiplier (its 0 or negative)
4723 			 */
4724 			timely_says = 0;
4725 			rack_log_timely(rack,  timely_says, log_mult,
4726 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4727 		} else {
4728 			/* Reduce the b/w multiplier */
4729 			timely_says = 1;
4730 			rack_log_timely(rack,  timely_says, log_mult,
4731 					get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4732 		}
4733 	}
4734 	return (timely_says);
4735 }
4736 
4737 static __inline int
rack_in_gp_window(struct tcpcb * tp,struct rack_sendmap * rsm)4738 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm)
4739 {
4740 	if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4741 	    SEQ_LEQ(rsm->r_end, tp->gput_ack)) {
4742 		/**
4743 		 * This covers the case that the
4744 		 * resent is completely inside
4745 		 * the gp range or up to it.
4746 		 *      |----------------|
4747 		 *      |-----| <or>
4748 		 *            |----|
4749 		 *            <or>   |---|
4750 		 */
4751 		return (1);
4752 	} else if (SEQ_LT(rsm->r_start, tp->gput_seq) &&
4753 		   SEQ_GT(rsm->r_end, tp->gput_seq)){
4754 		/**
4755 		 * This covers the case of
4756 		 *      |--------------|
4757 		 *  |-------->|
4758 		 */
4759 		return (1);
4760 	} else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4761 		   SEQ_LT(rsm->r_start, tp->gput_ack) &&
4762 		   SEQ_GEQ(rsm->r_end, tp->gput_ack)) {
4763 
4764 		/**
4765 		 * This covers the case of
4766 		 *      |--------------|
4767 		 *              |-------->|
4768 		 */
4769 		return (1);
4770 	}
4771 	return (0);
4772 }
4773 
4774 static __inline void
rack_mark_in_gp_win(struct tcpcb * tp,struct rack_sendmap * rsm)4775 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm)
4776 {
4777 
4778 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
4779 		return;
4780 	/*
4781 	 * We have a Goodput measurement in progress. Mark
4782 	 * the send if its within the window. If its not
4783 	 * in the window make sure it does not have the mark.
4784 	 */
4785 	if (rack_in_gp_window(tp, rsm))
4786 		rsm->r_flags |= RACK_IN_GP_WIN;
4787 	else
4788 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4789 }
4790 
4791 static __inline void
rack_clear_gp_marks(struct tcpcb * tp,struct tcp_rack * rack)4792 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4793 {
4794 	/* A GP measurement is ending, clear all marks on the send map*/
4795 	struct rack_sendmap *rsm = NULL;
4796 
4797 	rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4798 	if (rsm == NULL) {
4799 		rsm = tqhash_min(rack->r_ctl.tqh);
4800 	}
4801 	/* Nothing left? */
4802 	while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){
4803 		rsm->r_flags &= ~RACK_IN_GP_WIN;
4804 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4805 	}
4806 }
4807 
4808 
4809 static __inline void
rack_tend_gp_marks(struct tcpcb * tp,struct tcp_rack * rack)4810 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4811 {
4812 	struct rack_sendmap *rsm = NULL;
4813 
4814 	if (tp->snd_una == tp->snd_max) {
4815 		/* Nothing outstanding yet, nothing to do here */
4816 		return;
4817 	}
4818 	if (SEQ_GT(tp->gput_seq, tp->snd_una)) {
4819 		/*
4820 		 * We are measuring ahead of some outstanding
4821 		 * data. We need to walk through up until we get
4822 		 * to gp_seq marking so that no rsm is set incorrectly
4823 		 * with RACK_IN_GP_WIN.
4824 		 */
4825 		rsm = tqhash_min(rack->r_ctl.tqh);
4826 		while (rsm != NULL) {
4827 			rack_mark_in_gp_win(tp, rsm);
4828 			if (SEQ_GEQ(rsm->r_end, tp->gput_seq))
4829 				break;
4830 			rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4831 		}
4832 	}
4833 	if (rsm == NULL) {
4834 		/*
4835 		 * Need to find the GP seq, if rsm is
4836 		 * set we stopped as we hit it.
4837 		 */
4838 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4839 		if (rsm == NULL)
4840 			return;
4841 		rack_mark_in_gp_win(tp, rsm);
4842 	}
4843 	/*
4844 	 * Now we may need to mark already sent rsm, ahead of
4845 	 * gput_seq in the window since they may have been sent
4846 	 * *before* we started our measurment. The rsm, if non-null
4847 	 * has been marked (note if rsm would have been NULL we would have
4848 	 * returned in the previous block). So we go to the next, and continue
4849 	 * until we run out of entries or we exceed the gp_ack value.
4850 	 */
4851 	rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4852 	while (rsm) {
4853 		rack_mark_in_gp_win(tp, rsm);
4854 		if (SEQ_GT(rsm->r_end, tp->gput_ack))
4855 			break;
4856 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4857 	}
4858 }
4859 
4860 static void
rack_log_gp_calc(struct tcp_rack * rack,uint32_t add_part,uint32_t sub_part,uint32_t srtt,uint64_t meas_bw,uint64_t utim,uint8_t meth,uint32_t line)4861 rack_log_gp_calc(struct tcp_rack *rack, uint32_t add_part, uint32_t sub_part, uint32_t srtt, uint64_t meas_bw, uint64_t utim, uint8_t meth, uint32_t line)
4862 {
4863 	if (tcp_bblogging_on(rack->rc_tp)) {
4864 		union tcp_log_stackspecific log;
4865 		struct timeval tv;
4866 
4867 		memset(&log, 0, sizeof(log));
4868 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4869 		log.u_bbr.flex1 = add_part;
4870 		log.u_bbr.flex2 = sub_part;
4871 		log.u_bbr.flex3 = rack_wma_divisor;
4872 		log.u_bbr.flex4 = srtt;
4873 		log.u_bbr.flex7 = (uint16_t)line;
4874 		log.u_bbr.flex8 = meth;
4875 		log.u_bbr.delRate = rack->r_ctl.gp_bw;
4876 		log.u_bbr.cur_del_rate = meas_bw;
4877 		log.u_bbr.rttProp = utim;
4878 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
4879 		    &rack->rc_inp->inp_socket->so_rcv,
4880 		    &rack->rc_inp->inp_socket->so_snd,
4881 		    BBR_LOG_THRESH_CALC, 0,
4882 		    0, &log, false, &rack->r_ctl.act_rcv_time);
4883 	}
4884 }
4885 
4886 static void
rack_do_goodput_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq th_ack,int line,uint8_t quality)4887 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4888 			    tcp_seq th_ack, int line, uint8_t quality)
4889 {
4890 	uint64_t tim, bytes_ps, stim, utim;
4891 	uint32_t segsiz, bytes, reqbytes, us_cts;
4892 	int32_t gput, new_rtt_diff, timely_says;
4893 	uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4894 	int did_add = 0;
4895 
4896 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4897 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4898 	if (TSTMP_GEQ(us_cts, tp->gput_ts))
4899 		tim = us_cts - tp->gput_ts;
4900 	else
4901 		tim = 0;
4902 	if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4903 		stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4904 	else
4905 		stim = 0;
4906 	/*
4907 	 * Use the larger of the send time or ack time. This prevents us
4908 	 * from being influenced by ack artifacts to come up with too
4909 	 * high of measurement. Note that since we are spanning over many more
4910 	 * bytes in most of our measurements hopefully that is less likely to
4911 	 * occur.
4912 	 */
4913 	if (tim > stim)
4914 		utim = max(tim, 1);
4915 	else
4916 		utim = max(stim, 1);
4917 	reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4918 	rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL);
4919 	if ((tim == 0) && (stim == 0)) {
4920 		/*
4921 		 * Invalid measurement time, maybe
4922 		 * all on one ack/one send?
4923 		 */
4924 		bytes = 0;
4925 		bytes_ps = 0;
4926 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4927 					   0, 0, 0, 10, __LINE__, NULL, quality);
4928 		goto skip_measurement;
4929 	}
4930 	if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4931 		/* We never made a us_rtt measurement? */
4932 		bytes = 0;
4933 		bytes_ps = 0;
4934 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4935 					   0, 0, 0, 10, __LINE__, NULL, quality);
4936 		goto skip_measurement;
4937 	}
4938 	/*
4939 	 * Calculate the maximum possible b/w this connection
4940 	 * could have. We base our calculation on the lowest
4941 	 * rtt we have seen during the measurement and the
4942 	 * largest rwnd the client has given us in that time. This
4943 	 * forms a BDP that is the maximum that we could ever
4944 	 * get to the client. Anything larger is not valid.
4945 	 *
4946 	 * I originally had code here that rejected measurements
4947 	 * where the time was less than 1/2 the latest us_rtt.
4948 	 * But after thinking on that I realized its wrong since
4949 	 * say you had a 150Mbps or even 1Gbps link, and you
4950 	 * were a long way away.. example I am in Europe (100ms rtt)
4951 	 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4952 	 * bytes my time would be 1.2ms, and yet my rtt would say
4953 	 * the measurement was invalid the time was < 50ms. The
4954 	 * same thing is true for 150Mb (8ms of time).
4955 	 *
4956 	 * A better way I realized is to look at what the maximum
4957 	 * the connection could possibly do. This is gated on
4958 	 * the lowest RTT we have seen and the highest rwnd.
4959 	 * We should in theory never exceed that, if we are
4960 	 * then something on the path is storing up packets
4961 	 * and then feeding them all at once to our endpoint
4962 	 * messing up our measurement.
4963 	 */
4964 	rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4965 	rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4966 	rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4967 	if (SEQ_LT(th_ack, tp->gput_seq)) {
4968 		/* No measurement can be made */
4969 		bytes = 0;
4970 		bytes_ps = 0;
4971 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4972 					   0, 0, 0, 10, __LINE__, NULL, quality);
4973 		goto skip_measurement;
4974 	} else
4975 		bytes = (th_ack - tp->gput_seq);
4976 	bytes_ps = (uint64_t)bytes;
4977 	/*
4978 	 * Don't measure a b/w for pacing unless we have gotten at least
4979 	 * an initial windows worth of data in this measurement interval.
4980 	 *
4981 	 * Small numbers of bytes get badly influenced by delayed ack and
4982 	 * other artifacts. Note we take the initial window or our
4983 	 * defined minimum GP (defaulting to 10 which hopefully is the
4984 	 * IW).
4985 	 */
4986 	if (rack->rc_gp_filled == 0) {
4987 		/*
4988 		 * The initial estimate is special. We
4989 		 * have blasted out an IW worth of packets
4990 		 * without a real valid ack ts results. We
4991 		 * then setup the app_limited_needs_set flag,
4992 		 * this should get the first ack in (probably 2
4993 		 * MSS worth) to be recorded as the timestamp.
4994 		 * We thus allow a smaller number of bytes i.e.
4995 		 * IW - 2MSS.
4996 		 */
4997 		reqbytes -= (2 * segsiz);
4998 		/* Also lets fill previous for our first measurement to be neutral */
4999 		rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5000 	}
5001 	if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
5002 		rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5003 					   rack->r_ctl.rc_app_limited_cnt,
5004 					   0, 0, 10, __LINE__, NULL, quality);
5005 		goto skip_measurement;
5006 	}
5007 	/*
5008 	 * We now need to calculate the Timely like status so
5009 	 * we can update (possibly) the b/w multipliers.
5010 	 */
5011 	new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
5012 	if (rack->rc_gp_filled == 0) {
5013 		/* No previous reading */
5014 		rack->r_ctl.rc_rtt_diff = new_rtt_diff;
5015 	} else {
5016 		if (rack->measure_saw_probe_rtt == 0) {
5017 			/*
5018 			 * We don't want a probertt to be counted
5019 			 * since it will be negative incorrectly. We
5020 			 * expect to be reducing the RTT when we
5021 			 * pace at a slower rate.
5022 			 */
5023 			rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
5024 			rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
5025 		}
5026 	}
5027 	timely_says = rack_make_timely_judgement(rack,
5028 	    rack->r_ctl.rc_gp_srtt,
5029 	    rack->r_ctl.rc_rtt_diff,
5030 	    rack->r_ctl.rc_prev_gp_srtt
5031 	);
5032 	bytes_ps *= HPTS_USEC_IN_SEC;
5033 	bytes_ps /= utim;
5034 	if (bytes_ps > rack->r_ctl.last_max_bw) {
5035 		/*
5036 		 * Something is on path playing
5037 		 * since this b/w is not possible based
5038 		 * on our BDP (highest rwnd and lowest rtt
5039 		 * we saw in the measurement window).
5040 		 *
5041 		 * Another option here would be to
5042 		 * instead skip the measurement.
5043 		 */
5044 		rack_log_pacing_delay_calc(rack, bytes, reqbytes,
5045 					   bytes_ps, rack->r_ctl.last_max_bw, 0,
5046 					   11, __LINE__, NULL, quality);
5047 		bytes_ps = rack->r_ctl.last_max_bw;
5048 	}
5049 	/* We store gp for b/w in bytes per second */
5050 	if (rack->rc_gp_filled == 0) {
5051 		/* Initial measurement */
5052 		if (bytes_ps) {
5053 			rack->r_ctl.gp_bw = bytes_ps;
5054 			rack->rc_gp_filled = 1;
5055 			rack->r_ctl.num_measurements = 1;
5056 			rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
5057 		} else {
5058 			rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
5059 						   rack->r_ctl.rc_app_limited_cnt,
5060 						   0, 0, 10, __LINE__, NULL, quality);
5061 		}
5062 		if (tcp_in_hpts(rack->rc_tp) &&
5063 		    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
5064 			/*
5065 			 * Ok we can't trust the pacer in this case
5066 			 * where we transition from un-paced to paced.
5067 			 * Or for that matter when the burst mitigation
5068 			 * was making a wild guess and got it wrong.
5069 			 * Stop the pacer and clear up all the aggregate
5070 			 * delays etc.
5071 			 */
5072 			tcp_hpts_remove(rack->rc_tp);
5073 			rack->r_ctl.rc_hpts_flags = 0;
5074 			rack->r_ctl.rc_last_output_to = 0;
5075 		}
5076 		did_add = 2;
5077 	} else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
5078 		/* Still a small number run an average */
5079 		rack->r_ctl.gp_bw += bytes_ps;
5080 		addpart = rack->r_ctl.num_measurements;
5081 		rack->r_ctl.num_measurements++;
5082 		if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
5083 			/* We have collected enough to move forward */
5084 			rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
5085 		}
5086 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5087 		did_add = 3;
5088 	} else {
5089 		/*
5090 		 * We want to take 1/wma of the goodput and add in to 7/8th
5091 		 * of the old value weighted by the srtt. So if your measurement
5092 		 * period is say 2 SRTT's long you would get 1/4 as the
5093 		 * value, if it was like 1/2 SRTT then you would get 1/16th.
5094 		 *
5095 		 * But we must be careful not to take too much i.e. if the
5096 		 * srtt is say 20ms and the measurement is taken over
5097 		 * 400ms our weight would be 400/20 i.e. 20. On the
5098 		 * other hand if we get a measurement over 1ms with a
5099 		 * 10ms rtt we only want to take a much smaller portion.
5100 		 */
5101 		uint8_t meth;
5102 
5103 		if (rack->r_ctl.num_measurements < 0xff) {
5104 			rack->r_ctl.num_measurements++;
5105 		}
5106 		srtt = (uint64_t)tp->t_srtt;
5107 		if (srtt == 0) {
5108 			/*
5109 			 * Strange why did t_srtt go back to zero?
5110 			 */
5111 			if (rack->r_ctl.rc_rack_min_rtt)
5112 				srtt = rack->r_ctl.rc_rack_min_rtt;
5113 			else
5114 				srtt = HPTS_USEC_IN_MSEC;
5115 		}
5116 		/*
5117 		 * XXXrrs: Note for reviewers, in playing with
5118 		 * dynamic pacing I discovered this GP calculation
5119 		 * as done originally leads to some undesired results.
5120 		 * Basically you can get longer measurements contributing
5121 		 * too much to the WMA. Thus I changed it if you are doing
5122 		 * dynamic adjustments to only do the aportioned adjustment
5123 		 * if we have a very small (time wise) measurement. Longer
5124 		 * measurements just get there weight (defaulting to 1/8)
5125 		 * add to the WMA. We may want to think about changing
5126 		 * this to always do that for both sides i.e. dynamic
5127 		 * and non-dynamic... but considering lots of folks
5128 		 * were playing with this I did not want to change the
5129 		 * calculation per.se. without your thoughts.. Lawerence?
5130 		 * Peter??
5131 		 */
5132 		if (rack->rc_gp_dyn_mul == 0) {
5133 			subpart = rack->r_ctl.gp_bw * utim;
5134 			subpart /= (srtt * 8);
5135 			if (subpart < (rack->r_ctl.gp_bw / 2)) {
5136 				/*
5137 				 * The b/w update takes no more
5138 				 * away then 1/2 our running total
5139 				 * so factor it in.
5140 				 */
5141 				addpart = bytes_ps * utim;
5142 				addpart /= (srtt * 8);
5143 				meth = 1;
5144 			} else {
5145 				/*
5146 				 * Don't allow a single measurement
5147 				 * to account for more than 1/2 of the
5148 				 * WMA. This could happen on a retransmission
5149 				 * where utim becomes huge compared to
5150 				 * srtt (multiple retransmissions when using
5151 				 * the sending rate which factors in all the
5152 				 * transmissions from the first one).
5153 				 */
5154 				subpart = rack->r_ctl.gp_bw / 2;
5155 				addpart = bytes_ps / 2;
5156 				meth = 2;
5157 			}
5158 			rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5159 			resid_bw = rack->r_ctl.gp_bw - subpart;
5160 			rack->r_ctl.gp_bw = resid_bw + addpart;
5161 			did_add = 1;
5162 		} else {
5163 			if ((utim / srtt) <= 1) {
5164 				/*
5165 				 * The b/w update was over a small period
5166 				 * of time. The idea here is to prevent a small
5167 				 * measurement time period from counting
5168 				 * too much. So we scale it based on the
5169 				 * time so it attributes less than 1/rack_wma_divisor
5170 				 * of its measurement.
5171 				 */
5172 				subpart = rack->r_ctl.gp_bw * utim;
5173 				subpart /= (srtt * rack_wma_divisor);
5174 				addpart = bytes_ps * utim;
5175 				addpart /= (srtt * rack_wma_divisor);
5176 				meth = 3;
5177 			} else {
5178 				/*
5179 				 * The scaled measurement was long
5180 				 * enough so lets just add in the
5181 				 * portion of the measurement i.e. 1/rack_wma_divisor
5182 				 */
5183 				subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
5184 				addpart = bytes_ps / rack_wma_divisor;
5185 				meth = 4;
5186 			}
5187 			if ((rack->measure_saw_probe_rtt == 0) ||
5188 		            (bytes_ps > rack->r_ctl.gp_bw)) {
5189 				/*
5190 				 * For probe-rtt we only add it in
5191 				 * if its larger, all others we just
5192 				 * add in.
5193 				 */
5194 				did_add = 1;
5195 				rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__);
5196 				resid_bw = rack->r_ctl.gp_bw - subpart;
5197 				rack->r_ctl.gp_bw = resid_bw + addpart;
5198 			}
5199 		}
5200 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
5201 	}
5202 	/*
5203 	 * We only watch the growth of the GP during the initial startup
5204 	 * or first-slowstart that ensues. If we ever needed to watch
5205 	 * growth of gp outside of that period all we need to do is
5206 	 * remove the first clause of this if (rc_initial_ss_comp).
5207 	 */
5208 	if ((rack->rc_initial_ss_comp == 0) &&
5209 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG)) {
5210 		uint64_t gp_est;
5211 
5212 		gp_est = bytes_ps;
5213 		if (tcp_bblogging_on(rack->rc_tp)) {
5214 			union tcp_log_stackspecific log;
5215 			struct timeval tv;
5216 
5217 			memset(&log, 0, sizeof(log));
5218 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5219 			log.u_bbr.flex1 = rack->r_ctl.current_round;
5220 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
5221 			log.u_bbr.delRate = gp_est;
5222 			log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5223 			log.u_bbr.flex8 = 41;
5224 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5225 					    0, &log, false, NULL, __func__, __LINE__,&tv);
5226 		}
5227 		if ((rack->r_ctl.num_measurements == RACK_REQ_AVG) ||
5228 		    (rack->r_ctl.last_gpest == 0)) {
5229 			/*
5230 			 * The round we get our measurement averaging going
5231 			 * is the base round so it always is the source point
5232 			 * for when we had our first increment. From there on
5233 			 * we only record the round that had a rise.
5234 			 */
5235 			rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5236 			rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5237 		} else if (gp_est >= rack->r_ctl.last_gpest) {
5238 			/*
5239 			 * Test to see if its gone up enough
5240 			 * to set the round count up to now. Note
5241 			 * that on the seeding of the 4th measurement we
5242 			 */
5243 			gp_est *= 1000;
5244 			gp_est /= rack->r_ctl.last_gpest;
5245 			if ((uint32_t)gp_est > rack->r_ctl.gp_gain_req) {
5246 				/*
5247 				 * We went up enough to record the round.
5248 				 */
5249 				if (tcp_bblogging_on(rack->rc_tp)) {
5250 					union tcp_log_stackspecific log;
5251 					struct timeval tv;
5252 
5253 					memset(&log, 0, sizeof(log));
5254 					log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5255 					log.u_bbr.flex1 = rack->r_ctl.current_round;
5256 					log.u_bbr.flex2 = (uint32_t)gp_est;
5257 					log.u_bbr.flex3 = rack->r_ctl.gp_gain_req;
5258 					log.u_bbr.delRate = gp_est;
5259 					log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest;
5260 					log.u_bbr.flex8 = 42;
5261 					(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5262 							    0, &log, false, NULL, __func__, __LINE__,&tv);
5263 				}
5264 				rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round;
5265 				if (rack->r_ctl.use_gp_not_last == 1)
5266 					rack->r_ctl.last_gpest = rack->r_ctl.gp_bw;
5267 				else
5268 					rack->r_ctl.last_gpest = bytes_ps;
5269 			}
5270 		}
5271 	}
5272 	if ((rack->gp_ready == 0) &&
5273 	    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
5274 		/* We have enough measurements now */
5275 		rack->gp_ready = 1;
5276 		if (rack->dgp_on ||
5277 		    rack->rack_hibeta)
5278 			rack_set_cc_pacing(rack);
5279 		if (rack->defer_options)
5280 			rack_apply_deferred_options(rack);
5281 	}
5282 	rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
5283 				   rack_get_bw(rack), 22, did_add, NULL, quality);
5284 	/* We do not update any multipliers if we are in or have seen a probe-rtt */
5285 
5286 	if ((rack->measure_saw_probe_rtt == 0) &&
5287 	    rack->rc_gp_rtt_set) {
5288 		if (rack->rc_skip_timely == 0) {
5289 			rack_update_multiplier(rack, timely_says, bytes_ps,
5290 					       rack->r_ctl.rc_gp_srtt,
5291 					       rack->r_ctl.rc_rtt_diff);
5292 		}
5293 	}
5294 	rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
5295 				   rack_get_bw(rack), 3, line, NULL, quality);
5296 	rack_log_pacing_delay_calc(rack,
5297 				   bytes, /* flex2 */
5298 				   tim, /* flex1 */
5299 				   bytes_ps, /* bw_inuse */
5300 				   rack->r_ctl.gp_bw, /* delRate */
5301 				   rack_get_lt_bw(rack), /* rttProp */
5302 				   20, line, NULL, 0);
5303 	/* reset the gp srtt and setup the new prev */
5304 	rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5305 	/* Record the lost count for the next measurement */
5306 	rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
5307 skip_measurement:
5308 	/*
5309 	 * We restart our diffs based on the gpsrtt in the
5310 	 * measurement window.
5311 	 */
5312 	rack->rc_gp_rtt_set = 0;
5313 	rack->rc_gp_saw_rec = 0;
5314 	rack->rc_gp_saw_ca = 0;
5315 	rack->rc_gp_saw_ss = 0;
5316 	rack->rc_dragged_bottom = 0;
5317 	if (quality == RACK_QUALITY_HIGH) {
5318 		/*
5319 		 * Gput in the stats world is in kbps where bytes_ps is
5320 		 * bytes per second so we do ((x * 8)/ 1000).
5321 		 */
5322 		gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000);
5323 #ifdef STATS
5324 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
5325 					 gput);
5326 		/*
5327 		 * XXXLAS: This is a temporary hack, and should be
5328 		 * chained off VOI_TCP_GPUT when stats(9) grows an
5329 		 * API to deal with chained VOIs.
5330 		 */
5331 		if (tp->t_stats_gput_prev > 0)
5332 			stats_voi_update_abs_s32(tp->t_stats,
5333 						 VOI_TCP_GPUT_ND,
5334 						 ((gput - tp->t_stats_gput_prev) * 100) /
5335 						 tp->t_stats_gput_prev);
5336 #endif
5337 		tp->t_stats_gput_prev = gput;
5338 	}
5339 	tp->t_flags &= ~TF_GPUTINPROG;
5340 	/*
5341 	 * Now are we app limited now and there is space from where we
5342 	 * were to where we want to go?
5343 	 *
5344 	 * We don't do the other case i.e. non-applimited here since
5345 	 * the next send will trigger us picking up the missing data.
5346 	 */
5347 	if (rack->r_ctl.rc_first_appl &&
5348 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
5349 	    rack->r_ctl.rc_app_limited_cnt &&
5350 	    (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
5351 	    ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
5352 	     max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
5353 		/*
5354 		 * Yep there is enough outstanding to make a measurement here.
5355 		 */
5356 		struct rack_sendmap *rsm;
5357 
5358 		rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
5359 		rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
5360 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5361 		rack->app_limited_needs_set = 0;
5362 		tp->gput_seq = th_ack;
5363 		if (rack->in_probe_rtt)
5364 			rack->measure_saw_probe_rtt = 1;
5365 		else if ((rack->measure_saw_probe_rtt) &&
5366 			 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
5367 			rack->measure_saw_probe_rtt = 0;
5368 		if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
5369 			/* There is a full window to gain info from */
5370 			tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
5371 		} else {
5372 			/* We can only measure up to the applimited point */
5373 			tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
5374 			if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
5375 				/*
5376 				 * We don't have enough to make a measurement.
5377 				 */
5378 				tp->t_flags &= ~TF_GPUTINPROG;
5379 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
5380 							   0, 0, 0, 6, __LINE__, NULL, quality);
5381 				return;
5382 			}
5383 		}
5384 		if (tp->t_state >= TCPS_FIN_WAIT_1) {
5385 			/*
5386 			 * We will get no more data into the SB
5387 			 * this means we need to have the data available
5388 			 * before we start a measurement.
5389 			 */
5390 			if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) {
5391 				/* Nope not enough data. */
5392 				return;
5393 			}
5394 		}
5395 		tp->t_flags |= TF_GPUTINPROG;
5396 		/*
5397 		 * Now we need to find the timestamp of the send at tp->gput_seq
5398 		 * for the send based measurement.
5399 		 */
5400 		rack->r_ctl.rc_gp_cumack_ts = 0;
5401 		rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
5402 		if (rsm) {
5403 			/* Ok send-based limit is set */
5404 			if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
5405 				/*
5406 				 * Move back to include the earlier part
5407 				 * so our ack time lines up right (this may
5408 				 * make an overlapping measurement but thats
5409 				 * ok).
5410 				 */
5411 				tp->gput_seq = rsm->r_start;
5412 			}
5413 			if (rsm->r_flags & RACK_ACKED) {
5414 				struct rack_sendmap *nrsm;
5415 
5416 				tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
5417 				tp->gput_seq = rsm->r_end;
5418 				nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
5419 				if (nrsm)
5420 					rsm = nrsm;
5421 				else {
5422 					rack->app_limited_needs_set = 1;
5423 				}
5424 			} else
5425 				rack->app_limited_needs_set = 1;
5426 			/* We always go from the first send */
5427 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
5428 		} else {
5429 			/*
5430 			 * If we don't find the rsm due to some
5431 			 * send-limit set the current time, which
5432 			 * basically disables the send-limit.
5433 			 */
5434 			struct timeval tv;
5435 
5436 			microuptime(&tv);
5437 			rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
5438 		}
5439 		rack_tend_gp_marks(tp, rack);
5440 		rack_log_pacing_delay_calc(rack,
5441 					   tp->gput_seq,
5442 					   tp->gput_ack,
5443 					   (uintptr_t)rsm,
5444 					   tp->gput_ts,
5445 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
5446 					   9,
5447 					   __LINE__, rsm, quality);
5448 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
5449 	} else {
5450 		/*
5451 		 * To make sure proper timestamp merging occurs, we need to clear
5452 		 * all GP marks if we don't start a measurement.
5453 		 */
5454 		rack_clear_gp_marks(tp, rack);
5455 	}
5456 }
5457 
5458 /*
5459  * CC wrapper hook functions
5460  */
5461 static void
rack_ack_received(struct tcpcb * tp,struct tcp_rack * rack,uint32_t th_ack,uint16_t nsegs,uint16_t type,int32_t post_recovery)5462 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
5463     uint16_t type, int32_t post_recovery)
5464 {
5465 	uint32_t prior_cwnd, acked;
5466 	struct tcp_log_buffer *lgb = NULL;
5467 	uint8_t labc_to_use, quality;
5468 
5469 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5470 	tp->t_ccv.nsegs = nsegs;
5471 	acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una);
5472 	if ((post_recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
5473 		uint32_t max;
5474 
5475 		max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
5476 		if (tp->t_ccv.bytes_this_ack > max) {
5477 			tp->t_ccv.bytes_this_ack = max;
5478 		}
5479 	}
5480 #ifdef STATS
5481 	stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
5482 	    ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
5483 #endif
5484 	if ((th_ack == tp->snd_max) && rack->lt_bw_up) {
5485 		/*
5486 		 * We will ack all the data, time to end any
5487 		 * lt_bw_up we have running until something
5488 		 * new is sent. Note we need to use the actual
5489 		 * ack_rcv_time which with pacing may be different.
5490 		 */
5491 		uint64_t tmark;
5492 
5493 		rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq);
5494 		rack->r_ctl.lt_seq = tp->snd_max;
5495 		tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
5496 		if (tmark >= rack->r_ctl.lt_timemark) {
5497 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
5498 		}
5499 		rack->r_ctl.lt_timemark = tmark;
5500 		rack->lt_bw_up = 0;
5501 	}
5502 	quality = RACK_QUALITY_NONE;
5503 	if ((tp->t_flags & TF_GPUTINPROG) &&
5504 	    rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
5505 		/* Measure the Goodput */
5506 		rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
5507 	}
5508 	/* Which way our we limited, if not cwnd limited no advance in CA */
5509 	if (tp->snd_cwnd <= tp->snd_wnd)
5510 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
5511 	else
5512 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
5513 	if (tp->snd_cwnd > tp->snd_ssthresh) {
5514 		tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack,
5515 			 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
5516 		/* For the setting of a window past use the actual scwnd we are using */
5517 		if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
5518 			tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
5519 			tp->t_ccv.flags |= CCF_ABC_SENTAWND;
5520 		}
5521 	} else {
5522 		tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
5523 		tp->t_bytes_acked = 0;
5524 	}
5525 	prior_cwnd = tp->snd_cwnd;
5526 	if ((post_recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
5527 	    (rack_client_low_buf && rack->client_bufferlvl &&
5528 	    (rack->client_bufferlvl < rack_client_low_buf)))
5529 		labc_to_use = rack->rc_labc;
5530 	else
5531 		labc_to_use = rack_max_abc_post_recovery;
5532 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5533 		union tcp_log_stackspecific log;
5534 		struct timeval tv;
5535 
5536 		memset(&log, 0, sizeof(log));
5537 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5538 		log.u_bbr.flex1 = th_ack;
5539 		log.u_bbr.flex2 = tp->t_ccv.flags;
5540 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5541 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5542 		log.u_bbr.flex5 = labc_to_use;
5543 		log.u_bbr.flex6 = prior_cwnd;
5544 		log.u_bbr.flex7 = V_tcp_do_newsack;
5545 		log.u_bbr.flex8 = 1;
5546 		lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5547 				     0, &log, false, NULL, __func__, __LINE__,&tv);
5548 	}
5549 	if (CC_ALGO(tp)->ack_received != NULL) {
5550 		/* XXXLAS: Find a way to live without this */
5551 		tp->t_ccv.curack = th_ack;
5552 		tp->t_ccv.labc = labc_to_use;
5553 		tp->t_ccv.flags |= CCF_USE_LOCAL_ABC;
5554 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
5555 	}
5556 	if (lgb) {
5557 		lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
5558 	}
5559 	if (rack->r_must_retran) {
5560 		if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
5561 			/*
5562 			 * We now are beyond the rxt point so lets disable
5563 			 * the flag.
5564 			 */
5565 			rack->r_ctl.rc_out_at_rto = 0;
5566 			rack->r_must_retran = 0;
5567 		} else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
5568 			/*
5569 			 * Only decrement the rc_out_at_rto if the cwnd advances
5570 			 * at least a whole segment. Otherwise next time the peer
5571 			 * acks, we won't be able to send this generaly happens
5572 			 * when we are in Congestion Avoidance.
5573 			 */
5574 			if (acked <= rack->r_ctl.rc_out_at_rto){
5575 				rack->r_ctl.rc_out_at_rto -= acked;
5576 			} else {
5577 				rack->r_ctl.rc_out_at_rto = 0;
5578 			}
5579 		}
5580 	}
5581 #ifdef STATS
5582 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
5583 #endif
5584 	if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
5585 		rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
5586 	}
5587 	if ((rack->rc_initial_ss_comp == 0) &&
5588 	    (tp->snd_cwnd >= tp->snd_ssthresh)) {
5589 		/*
5590 		 * The cwnd has grown beyond ssthresh we have
5591 		 * entered ca and completed our first Slowstart.
5592 		 */
5593 		rack->rc_initial_ss_comp = 1;
5594 	}
5595 }
5596 
5597 static void
tcp_rack_partialack(struct tcpcb * tp)5598 tcp_rack_partialack(struct tcpcb *tp)
5599 {
5600 	struct tcp_rack *rack;
5601 
5602 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5603 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5604 	/*
5605 	 * If we are doing PRR and have enough
5606 	 * room to send <or> we are pacing and prr
5607 	 * is disabled we will want to see if we
5608 	 * can send data (by setting r_wanted_output to
5609 	 * true).
5610 	 */
5611 	if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
5612 	    rack->rack_no_prr)
5613 		rack->r_wanted_output = 1;
5614 }
5615 
5616 static void
rack_exit_recovery(struct tcpcb * tp,struct tcp_rack * rack,int how)5617 rack_exit_recovery(struct tcpcb *tp, struct tcp_rack *rack, int how)
5618 {
5619 	/*
5620 	 * Now exit recovery.
5621 	 */
5622 	EXIT_RECOVERY(tp->t_flags);
5623 }
5624 
5625 static void
rack_post_recovery(struct tcpcb * tp,uint32_t th_ack)5626 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
5627 {
5628 	struct tcp_rack *rack;
5629 	uint32_t orig_cwnd;
5630 
5631 	orig_cwnd = tp->snd_cwnd;
5632 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5633 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5634 	/* only alert CC if we alerted when we entered */
5635 	if (CC_ALGO(tp)->post_recovery != NULL) {
5636 		tp->t_ccv.curack = th_ack;
5637 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
5638 		if (tp->snd_cwnd < tp->snd_ssthresh) {
5639 			/*
5640 			 * Rack has burst control and pacing
5641 			 * so lets not set this any lower than
5642 			 * snd_ssthresh per RFC-6582 (option 2).
5643 			 */
5644 			tp->snd_cwnd = tp->snd_ssthresh;
5645 		}
5646 	}
5647 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5648 		union tcp_log_stackspecific log;
5649 		struct timeval tv;
5650 
5651 		memset(&log, 0, sizeof(log));
5652 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5653 		log.u_bbr.flex1 = th_ack;
5654 		log.u_bbr.flex2 = tp->t_ccv.flags;
5655 		log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5656 		log.u_bbr.flex4 = tp->t_ccv.nsegs;
5657 		log.u_bbr.flex5 = V_tcp_abc_l_var;
5658 		log.u_bbr.flex6 = orig_cwnd;
5659 		log.u_bbr.flex7 = V_tcp_do_newsack;
5660 		log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
5661 		log.u_bbr.flex8 = 2;
5662 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5663 			       0, &log, false, NULL, __func__, __LINE__, &tv);
5664 	}
5665 	if ((rack->rack_no_prr == 0) &&
5666 	    (rack->no_prr_addback == 0) &&
5667 	    (rack->r_ctl.rc_prr_sndcnt > 0)) {
5668 		/*
5669 		 * Suck the next prr cnt back into cwnd, but
5670 		 * only do that if we are not application limited.
5671 		 */
5672 		if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) {
5673 			/*
5674 			 * We are allowed to add back to the cwnd the amount we did
5675 			 * not get out if:
5676 			 * a) no_prr_addback is off.
5677 			 * b) we are not app limited
5678 			 * c) we are doing prr
5679 			 * <and>
5680 			 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
5681 			 */
5682 			tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
5683 					    rack->r_ctl.rc_prr_sndcnt);
5684 		}
5685 		rack->r_ctl.rc_prr_sndcnt = 0;
5686 		rack_log_to_prr(rack, 1, 0, __LINE__);
5687 	}
5688 	rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
5689 	tp->snd_recover = tp->snd_una;
5690 	if (rack->r_ctl.dsack_persist) {
5691 		rack->r_ctl.dsack_persist--;
5692 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
5693 			rack->r_ctl.num_dsack = 0;
5694 		}
5695 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
5696 	}
5697 	if (rack->rto_from_rec == 1) {
5698 		rack->rto_from_rec = 0;
5699 		if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
5700 			tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
5701 	}
5702 	rack_exit_recovery(tp, rack, 1);
5703 }
5704 
5705 static void
rack_cong_signal(struct tcpcb * tp,uint32_t type,uint32_t ack,int line)5706 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
5707 {
5708 	struct tcp_rack *rack;
5709 	uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
5710 
5711 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5712 #ifdef STATS
5713 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
5714 #endif
5715 	if (IN_RECOVERY(tp->t_flags) == 0) {
5716 		in_rec_at_entry = 0;
5717 		ssthresh_enter = tp->snd_ssthresh;
5718 		cwnd_enter = tp->snd_cwnd;
5719 	} else
5720 		in_rec_at_entry = 1;
5721 	rack = (struct tcp_rack *)tp->t_fb_ptr;
5722 	switch (type) {
5723 	case CC_NDUPACK:
5724 		tp->t_flags &= ~TF_WASFRECOVERY;
5725 		tp->t_flags &= ~TF_WASCRECOVERY;
5726 		if (!IN_FASTRECOVERY(tp->t_flags)) {
5727 			/* Check if this is the end of the initial Start-up i.e. initial slow-start */
5728 			if (rack->rc_initial_ss_comp == 0) {
5729 				/* Yep it is the end of the initial slowstart */
5730 				rack->rc_initial_ss_comp = 1;
5731 			}
5732 			rack->r_ctl.rc_prr_delivered = 0;
5733 			rack->r_ctl.rc_prr_out = 0;
5734 			rack->r_fast_output = 0;
5735 			if (rack->rack_no_prr == 0) {
5736 				rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5737 				rack_log_to_prr(rack, 2, in_rec_at_entry, line);
5738 			}
5739 			rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
5740 			tp->snd_recover = tp->snd_max;
5741 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5742 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5743 		}
5744 		break;
5745 	case CC_ECN:
5746 		if (!IN_CONGRECOVERY(tp->t_flags) ||
5747 		    /*
5748 		     * Allow ECN reaction on ACK to CWR, if
5749 		     * that data segment was also CE marked.
5750 		     */
5751 		    SEQ_GEQ(ack, tp->snd_recover)) {
5752 			EXIT_CONGRECOVERY(tp->t_flags);
5753 			KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
5754 			rack->r_fast_output = 0;
5755 			tp->snd_recover = tp->snd_max + 1;
5756 			if (tp->t_flags2 & TF2_ECN_PERMIT)
5757 				tp->t_flags2 |= TF2_ECN_SND_CWR;
5758 		}
5759 		break;
5760 	case CC_RTO:
5761 		tp->t_dupacks = 0;
5762 		tp->t_bytes_acked = 0;
5763 		rack->r_fast_output = 0;
5764 		if (IN_RECOVERY(tp->t_flags))
5765 			rack_exit_recovery(tp, rack, 2);
5766 		orig_cwnd = tp->snd_cwnd;
5767 		rack_log_to_prr(rack, 16, orig_cwnd, line);
5768 		if (CC_ALGO(tp)->cong_signal == NULL) {
5769 			/* TSNH */
5770 			tp->snd_ssthresh = max(2,
5771 			    min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
5772 			    ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
5773 			tp->snd_cwnd = ctf_fixed_maxseg(tp);
5774 		}
5775 		if (tp->t_flags2 & TF2_ECN_PERMIT)
5776 			tp->t_flags2 |= TF2_ECN_SND_CWR;
5777 		break;
5778 	case CC_RTO_ERR:
5779 		KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
5780 		/* RTO was unnecessary, so reset everything. */
5781 		tp->snd_cwnd = tp->snd_cwnd_prev;
5782 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
5783 		tp->snd_recover = tp->snd_recover_prev;
5784 		if (tp->t_flags & TF_WASFRECOVERY) {
5785 			ENTER_FASTRECOVERY(tp->t_flags);
5786 			tp->t_flags &= ~TF_WASFRECOVERY;
5787 		}
5788 		if (tp->t_flags & TF_WASCRECOVERY) {
5789 			ENTER_CONGRECOVERY(tp->t_flags);
5790 			tp->t_flags &= ~TF_WASCRECOVERY;
5791 		}
5792 		tp->snd_nxt = tp->snd_max;
5793 		tp->t_badrxtwin = 0;
5794 		break;
5795 	}
5796 	if ((CC_ALGO(tp)->cong_signal != NULL)  &&
5797 	    (type != CC_RTO)){
5798 		tp->t_ccv.curack = ack;
5799 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
5800 	}
5801 	if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5802 		rack_log_to_prr(rack, 15, cwnd_enter, line);
5803 		rack->r_ctl.dsack_byte_cnt = 0;
5804 		rack->r_ctl.retran_during_recovery = 0;
5805 		rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5806 		rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5807 		rack->r_ent_rec_ns = 1;
5808 	}
5809 }
5810 
5811 static inline void
rack_cc_after_idle(struct tcp_rack * rack,struct tcpcb * tp)5812 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5813 {
5814 	uint32_t i_cwnd;
5815 
5816 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5817 
5818 	if (CC_ALGO(tp)->after_idle != NULL)
5819 		CC_ALGO(tp)->after_idle(&tp->t_ccv);
5820 
5821 	if (tp->snd_cwnd == 1)
5822 		i_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
5823 	else
5824 		i_cwnd = rc_init_window(rack);
5825 
5826 	/*
5827 	 * Being idle is no different than the initial window. If the cc
5828 	 * clamps it down below the initial window raise it to the initial
5829 	 * window.
5830 	 */
5831 	if (tp->snd_cwnd < i_cwnd) {
5832 		tp->snd_cwnd = i_cwnd;
5833 	}
5834 }
5835 
5836 /*
5837  * Indicate whether this ack should be delayed.  We can delay the ack if
5838  * following conditions are met:
5839  *	- There is no delayed ack timer in progress.
5840  *	- Our last ack wasn't a 0-sized window. We never want to delay
5841  *	  the ack that opens up a 0-sized window.
5842  *	- LRO wasn't used for this segment. We make sure by checking that the
5843  *	  segment size is not larger than the MSS.
5844  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
5845  *	  connection.
5846  */
5847 #define DELAY_ACK(tp, tlen)			 \
5848 	(((tp->t_flags & TF_RXWIN0SENT) == 0) && \
5849 	((tp->t_flags & TF_DELACK) == 0) &&	 \
5850 	(tlen <= tp->t_maxseg) &&		 \
5851 	(tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
5852 
5853 static struct rack_sendmap *
rack_find_lowest_rsm(struct tcp_rack * rack)5854 rack_find_lowest_rsm(struct tcp_rack *rack)
5855 {
5856 	struct rack_sendmap *rsm;
5857 
5858 	/*
5859 	 * Walk the time-order transmitted list looking for an rsm that is
5860 	 * not acked. This will be the one that was sent the longest time
5861 	 * ago that is still outstanding.
5862 	 */
5863 	TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5864 		if (rsm->r_flags & RACK_ACKED) {
5865 			continue;
5866 		}
5867 		goto finish;
5868 	}
5869 finish:
5870 	return (rsm);
5871 }
5872 
5873 static struct rack_sendmap *
rack_find_high_nonack(struct tcp_rack * rack,struct rack_sendmap * rsm)5874 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5875 {
5876 	struct rack_sendmap *prsm;
5877 
5878 	/*
5879 	 * Walk the sequence order list backward until we hit and arrive at
5880 	 * the highest seq not acked. In theory when this is called it
5881 	 * should be the last segment (which it was not).
5882 	 */
5883 	prsm = rsm;
5884 
5885 	TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) {
5886 		if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5887 			continue;
5888 		}
5889 		return (prsm);
5890 	}
5891 	return (NULL);
5892 }
5893 
5894 static uint32_t
rack_calc_thresh_rack(struct tcp_rack * rack,uint32_t srtt,uint32_t cts,int line,int log_allowed)5895 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts, int line, int log_allowed)
5896 {
5897 	int32_t lro;
5898 	uint32_t thresh;
5899 
5900 	/*
5901 	 * lro is the flag we use to determine if we have seen reordering.
5902 	 * If it gets set we have seen reordering. The reorder logic either
5903 	 * works in one of two ways:
5904 	 *
5905 	 * If reorder-fade is configured, then we track the last time we saw
5906 	 * re-ordering occur. If we reach the point where enough time as
5907 	 * passed we no longer consider reordering has occuring.
5908 	 *
5909 	 * Or if reorder-face is 0, then once we see reordering we consider
5910 	 * the connection to alway be subject to reordering and just set lro
5911 	 * to 1.
5912 	 *
5913 	 * In the end if lro is non-zero we add the extra time for
5914 	 * reordering in.
5915 	 */
5916 	if (srtt == 0)
5917 		srtt = 1;
5918 	if (rack->r_ctl.rc_reorder_ts) {
5919 		if (rack->r_ctl.rc_reorder_fade) {
5920 			if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
5921 				lro = cts - rack->r_ctl.rc_reorder_ts;
5922 				if (lro == 0) {
5923 					/*
5924 					 * No time as passed since the last
5925 					 * reorder, mark it as reordering.
5926 					 */
5927 					lro = 1;
5928 				}
5929 			} else {
5930 				/* Negative time? */
5931 				lro = 0;
5932 			}
5933 			if (lro > rack->r_ctl.rc_reorder_fade) {
5934 				/* Turn off reordering seen too */
5935 				rack->r_ctl.rc_reorder_ts = 0;
5936 				lro = 0;
5937 			}
5938 		} else {
5939 			/* Reodering does not fade */
5940 			lro = 1;
5941 		}
5942 	} else {
5943 		lro = 0;
5944 	}
5945 	if (rack->rc_rack_tmr_std_based == 0) {
5946 		thresh = srtt + rack->r_ctl.rc_pkt_delay;
5947 	} else {
5948 		/* Standards based pkt-delay is 1/4 srtt */
5949 		thresh = srtt +  (srtt >> 2);
5950 	}
5951 	if (lro && (rack->rc_rack_tmr_std_based == 0)) {
5952 		/* It must be set, if not you get 1/4 rtt */
5953 		if (rack->r_ctl.rc_reorder_shift)
5954 			thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
5955 		else
5956 			thresh += (srtt >> 2);
5957 	}
5958 	if (rack->rc_rack_use_dsack &&
5959 	    lro &&
5960 	    (rack->r_ctl.num_dsack > 0)) {
5961 		/*
5962 		 * We only increase the reordering window if we
5963 		 * have seen reordering <and> we have a DSACK count.
5964 		 */
5965 		thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5966 		if (log_allowed)
5967 			rack_log_dsack_event(rack, 4, line, srtt, thresh);
5968 	}
5969 	/* SRTT * 2 is the ceiling */
5970 	if (thresh > (srtt * 2)) {
5971 		thresh = srtt * 2;
5972 	}
5973 	/* And we don't want it above the RTO max either */
5974 	if (thresh > rack_rto_max) {
5975 		thresh = rack_rto_max;
5976 	}
5977 	if (log_allowed)
5978 		rack_log_dsack_event(rack, 6, line,  srtt, thresh);
5979 	return (thresh);
5980 }
5981 
5982 static uint32_t
rack_calc_thresh_tlp(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t srtt)5983 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5984 		     struct rack_sendmap *rsm, uint32_t srtt)
5985 {
5986 	struct rack_sendmap *prsm;
5987 	uint32_t thresh, len;
5988 	int segsiz;
5989 
5990 	if (srtt == 0)
5991 		srtt = 1;
5992 	if (rack->r_ctl.rc_tlp_threshold)
5993 		thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5994 	else
5995 		thresh = (srtt * 2);
5996 
5997 	/* Get the previous sent packet, if any */
5998 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5999 	len = rsm->r_end - rsm->r_start;
6000 	if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
6001 		/* Exactly like the ID */
6002 		if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
6003 			uint32_t alt_thresh;
6004 			/*
6005 			 * Compensate for delayed-ack with the d-ack time.
6006 			 */
6007 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6008 			if (alt_thresh > thresh)
6009 				thresh = alt_thresh;
6010 		}
6011 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
6012 		/* 2.1 behavior */
6013 		prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
6014 		if (prsm && (len <= segsiz)) {
6015 			/*
6016 			 * Two packets outstanding, thresh should be (2*srtt) +
6017 			 * possible inter-packet delay (if any).
6018 			 */
6019 			uint32_t inter_gap = 0;
6020 			int idx, nidx;
6021 
6022 			idx = rsm->r_rtr_cnt - 1;
6023 			nidx = prsm->r_rtr_cnt - 1;
6024 			if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
6025 				/* Yes it was sent later (or at the same time) */
6026 				inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
6027 			}
6028 			thresh += inter_gap;
6029 		} else if (len <= segsiz) {
6030 			/*
6031 			 * Possibly compensate for delayed-ack.
6032 			 */
6033 			uint32_t alt_thresh;
6034 
6035 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6036 			if (alt_thresh > thresh)
6037 				thresh = alt_thresh;
6038 		}
6039 	} else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
6040 		/* 2.2 behavior */
6041 		if (len <= segsiz) {
6042 			uint32_t alt_thresh;
6043 			/*
6044 			 * Compensate for delayed-ack with the d-ack time.
6045 			 */
6046 			alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6047 			if (alt_thresh > thresh)
6048 				thresh = alt_thresh;
6049 		}
6050 	}
6051 	/* Not above an RTO */
6052 	if (thresh > tp->t_rxtcur) {
6053 		thresh = tp->t_rxtcur;
6054 	}
6055 	/* Not above a RTO max */
6056 	if (thresh > rack_rto_max) {
6057 		thresh = rack_rto_max;
6058 	}
6059 	/* Apply user supplied min TLP */
6060 	if (thresh < rack_tlp_min) {
6061 		thresh = rack_tlp_min;
6062 	}
6063 	return (thresh);
6064 }
6065 
6066 static uint32_t
rack_grab_rtt(struct tcpcb * tp,struct tcp_rack * rack)6067 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
6068 {
6069 	/*
6070 	 * We want the rack_rtt which is the
6071 	 * last rtt we measured. However if that
6072 	 * does not exist we fallback to the srtt (which
6073 	 * we probably will never do) and then as a last
6074 	 * resort we use RACK_INITIAL_RTO if no srtt is
6075 	 * yet set.
6076 	 */
6077 	if (rack->rc_rack_rtt)
6078 		return (rack->rc_rack_rtt);
6079 	else if (tp->t_srtt == 0)
6080 		return (RACK_INITIAL_RTO);
6081 	return (tp->t_srtt);
6082 }
6083 
6084 static struct rack_sendmap *
rack_check_recovery_mode(struct tcpcb * tp,uint32_t tsused)6085 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
6086 {
6087 	/*
6088 	 * Check to see that we don't need to fall into recovery. We will
6089 	 * need to do so if our oldest transmit is past the time we should
6090 	 * have had an ack.
6091 	 */
6092 	struct tcp_rack *rack;
6093 	struct rack_sendmap *rsm;
6094 	int32_t idx;
6095 	uint32_t srtt, thresh;
6096 
6097 	rack = (struct tcp_rack *)tp->t_fb_ptr;
6098 	if (tqhash_empty(rack->r_ctl.tqh)) {
6099 		return (NULL);
6100 	}
6101 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6102 	if (rsm == NULL)
6103 		return (NULL);
6104 
6105 
6106 	if (rsm->r_flags & RACK_ACKED) {
6107 		rsm = rack_find_lowest_rsm(rack);
6108 		if (rsm == NULL)
6109 			return (NULL);
6110 	}
6111 	idx = rsm->r_rtr_cnt - 1;
6112 	srtt = rack_grab_rtt(tp, rack);
6113 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
6114 	if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
6115 		return (NULL);
6116 	}
6117 	if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
6118 		return (NULL);
6119 	}
6120 	/* Ok if we reach here we are over-due and this guy can be sent */
6121 	rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
6122 	return (rsm);
6123 }
6124 
6125 static uint32_t
rack_get_persists_timer_val(struct tcpcb * tp,struct tcp_rack * rack)6126 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
6127 {
6128 	int32_t t;
6129 	int32_t tt;
6130 	uint32_t ret_val;
6131 
6132 	t = (tp->t_srtt + (tp->t_rttvar << 2));
6133 	RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
6134  	    rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
6135 	rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
6136 	ret_val = (uint32_t)tt;
6137 	return (ret_val);
6138 }
6139 
6140 static uint32_t
rack_timer_start(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,int sup_rack)6141 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
6142 {
6143 	/*
6144 	 * Start the FR timer, we do this based on getting the first one in
6145 	 * the rc_tmap. Note that if its NULL we must stop the timer. in all
6146 	 * events we need to stop the running timer (if its running) before
6147 	 * starting the new one.
6148 	 */
6149 	uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
6150 	uint32_t srtt_cur;
6151 	int32_t idx;
6152 	int32_t is_tlp_timer = 0;
6153 	struct rack_sendmap *rsm;
6154 
6155 	if (rack->t_timers_stopped) {
6156 		/* All timers have been stopped none are to run */
6157 		return (0);
6158 	}
6159 	if (rack->rc_in_persist) {
6160 		/* We can't start any timer in persists */
6161 		return (rack_get_persists_timer_val(tp, rack));
6162 	}
6163 	rack->rc_on_min_to = 0;
6164 	if ((tp->t_state < TCPS_ESTABLISHED) ||
6165 	    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
6166 		goto activate_rxt;
6167 	}
6168 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6169 	if ((rsm == NULL) || sup_rack) {
6170 		/* Nothing on the send map or no rack */
6171 activate_rxt:
6172 		time_since_sent = 0;
6173 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6174 		if (rsm) {
6175 			/*
6176 			 * Should we discount the RTX timer any?
6177 			 *
6178 			 * We want to discount it the smallest amount.
6179 			 * If a timer (Rack/TLP or RXT) has gone off more
6180 			 * recently thats the discount we want to use (now - timer time).
6181 			 * If the retransmit of the oldest packet was more recent then
6182 			 * we want to use that (now - oldest-packet-last_transmit_time).
6183 			 *
6184 			 */
6185 			idx = rsm->r_rtr_cnt - 1;
6186 			if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
6187 				tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6188 			else
6189 				tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6190 			if (TSTMP_GT(cts, tstmp_touse))
6191 			    time_since_sent = cts - tstmp_touse;
6192 		}
6193 		if (SEQ_LT(tp->snd_una, tp->snd_max) ||
6194 		    sbavail(&tptosocket(tp)->so_snd)) {
6195 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
6196 			to = tp->t_rxtcur;
6197 			if (to > time_since_sent)
6198 				to -= time_since_sent;
6199 			else
6200 				to = rack->r_ctl.rc_min_to;
6201 			if (to == 0)
6202 				to = 1;
6203 			/* Special case for KEEPINIT */
6204 			if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6205 			    (TP_KEEPINIT(tp) != 0) &&
6206 			    rsm) {
6207 				/*
6208 				 * We have to put a ceiling on the rxt timer
6209 				 * of the keep-init timeout.
6210 				 */
6211 				uint32_t max_time, red;
6212 
6213 				max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
6214 				if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
6215 					red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
6216 					if (red < max_time)
6217 						max_time -= red;
6218 					else
6219 						max_time = 1;
6220 				}
6221 				/* Reduce timeout to the keep value if needed */
6222 				if (max_time < to)
6223 					to = max_time;
6224 			}
6225 			return (to);
6226 		}
6227 		return (0);
6228 	}
6229 	if (rsm->r_flags & RACK_ACKED) {
6230 		rsm = rack_find_lowest_rsm(rack);
6231 		if (rsm == NULL) {
6232 			/* No lowest? */
6233 			goto activate_rxt;
6234 		}
6235 	}
6236 	/* Convert from ms to usecs */
6237 	if ((rsm->r_flags & RACK_SACK_PASSED) ||
6238 	    (rsm->r_flags & RACK_RWND_COLLAPSED) ||
6239 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
6240 		if ((tp->t_flags & TF_SENTFIN) &&
6241 		    ((tp->snd_max - tp->snd_una) == 1) &&
6242 		    (rsm->r_flags & RACK_HAS_FIN)) {
6243 			/*
6244 			 * We don't start a rack timer if all we have is a
6245 			 * FIN outstanding.
6246 			 */
6247 			goto activate_rxt;
6248 		}
6249 		if ((rack->use_rack_rr == 0) &&
6250 		    (IN_FASTRECOVERY(tp->t_flags)) &&
6251 		    (rack->rack_no_prr == 0) &&
6252 		     (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
6253 			/*
6254 			 * We are not cheating, in recovery  and
6255 			 * not enough ack's to yet get our next
6256 			 * retransmission out.
6257 			 *
6258 			 * Note that classified attackers do not
6259 			 * get to use the rack-cheat.
6260 			 */
6261 			goto activate_tlp;
6262 		}
6263 		srtt = rack_grab_rtt(tp, rack);
6264 		thresh = rack_calc_thresh_rack(rack, srtt, cts, __LINE__, 1);
6265 		idx = rsm->r_rtr_cnt - 1;
6266 		exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
6267 		if (SEQ_GEQ(exp, cts)) {
6268 			to = exp - cts;
6269 			if (to < rack->r_ctl.rc_min_to) {
6270 				to = rack->r_ctl.rc_min_to;
6271 				if (rack->r_rr_config == 3)
6272 					rack->rc_on_min_to = 1;
6273 			}
6274 		} else {
6275 			to = rack->r_ctl.rc_min_to;
6276 			if (rack->r_rr_config == 3)
6277 				rack->rc_on_min_to = 1;
6278 		}
6279 	} else {
6280 		/* Ok we need to do a TLP not RACK */
6281 activate_tlp:
6282 		if ((rack->rc_tlp_in_progress != 0) &&
6283 		    (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
6284 			/*
6285 			 * The previous send was a TLP and we have sent
6286 			 * N TLP's without sending new data.
6287 			 */
6288 			goto activate_rxt;
6289 		}
6290 		rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
6291 		if (rsm == NULL) {
6292 			/* We found no rsm to TLP with. */
6293 			goto activate_rxt;
6294 		}
6295 		if (rsm->r_flags & RACK_HAS_FIN) {
6296 			/* If its a FIN we dont do TLP */
6297 			rsm = NULL;
6298 			goto activate_rxt;
6299 		}
6300 		idx = rsm->r_rtr_cnt - 1;
6301 		time_since_sent = 0;
6302 		if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
6303 			tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6304 		else
6305 			tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6306 		if (TSTMP_GT(cts, tstmp_touse))
6307 		    time_since_sent = cts - tstmp_touse;
6308 		is_tlp_timer = 1;
6309 		if (tp->t_srtt) {
6310 			if ((rack->rc_srtt_measure_made == 0) &&
6311 			    (tp->t_srtt == 1)) {
6312 				/*
6313 				 * If another stack as run and set srtt to 1,
6314 				 * then the srtt was 0, so lets use the initial.
6315 				 */
6316 				srtt = RACK_INITIAL_RTO;
6317 			} else {
6318 				srtt_cur = tp->t_srtt;
6319 				srtt = srtt_cur;
6320 			}
6321 		} else
6322 			srtt = RACK_INITIAL_RTO;
6323 		/*
6324 		 * If the SRTT is not keeping up and the
6325 		 * rack RTT has spiked we want to use
6326 		 * the last RTT not the smoothed one.
6327 		 */
6328 		if (rack_tlp_use_greater &&
6329 		    tp->t_srtt &&
6330 		    (srtt < rack_grab_rtt(tp, rack))) {
6331 			srtt = rack_grab_rtt(tp, rack);
6332 		}
6333 		thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
6334 		if (thresh > time_since_sent) {
6335 			to = thresh - time_since_sent;
6336 		} else {
6337 			to = rack->r_ctl.rc_min_to;
6338 			rack_log_alt_to_to_cancel(rack,
6339 						  thresh,		/* flex1 */
6340 						  time_since_sent,	/* flex2 */
6341 						  tstmp_touse,		/* flex3 */
6342 						  rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
6343 						  (uint32_t)rsm->r_tim_lastsent[idx],
6344 						  srtt,
6345 						  idx, 99);
6346 		}
6347 		if (to < rack_tlp_min) {
6348 			to = rack_tlp_min;
6349 		}
6350 		if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
6351 			/*
6352 			 * If the TLP time works out to larger than the max
6353 			 * RTO lets not do TLP.. just RTO.
6354 			 */
6355 			goto activate_rxt;
6356 		}
6357 	}
6358 	if (is_tlp_timer == 0) {
6359 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
6360 	} else {
6361 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
6362 	}
6363 	if (to == 0)
6364 		to = 1;
6365 	return (to);
6366 }
6367 
6368 static void
rack_enter_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,tcp_seq snd_una)6369 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una)
6370 {
6371 	if (rack->rc_in_persist == 0) {
6372 		if (tp->t_flags & TF_GPUTINPROG) {
6373 			/*
6374 			 * Stop the goodput now, the calling of the
6375 			 * measurement function clears the flag.
6376 			 */
6377 			rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
6378 						    RACK_QUALITY_PERSIST);
6379 		}
6380 #ifdef NETFLIX_SHARED_CWND
6381 		if (rack->r_ctl.rc_scw) {
6382 			tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6383 			rack->rack_scwnd_is_idle = 1;
6384 		}
6385 #endif
6386 		rack->r_ctl.rc_went_idle_time = cts;
6387 		if (rack->r_ctl.rc_went_idle_time == 0)
6388 			rack->r_ctl.rc_went_idle_time = 1;
6389 		if (rack->lt_bw_up) {
6390 			/* Suspend our LT BW measurement */
6391 			uint64_t tmark;
6392 
6393 			rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq);
6394 			rack->r_ctl.lt_seq = snd_una;
6395 			tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
6396 			if (tmark >= rack->r_ctl.lt_timemark) {
6397 				rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
6398 			}
6399 			rack->r_ctl.lt_timemark = tmark;
6400 			rack->lt_bw_up = 0;
6401 			rack->r_persist_lt_bw_off = 1;
6402 		}
6403 		rack_timer_cancel(tp, rack, cts, __LINE__);
6404 		rack->r_ctl.persist_lost_ends = 0;
6405 		rack->probe_not_answered = 0;
6406 		rack->forced_ack = 0;
6407 		tp->t_rxtshift = 0;
6408 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6409 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6410 		rack->rc_in_persist = 1;
6411 	}
6412 }
6413 
6414 static void
rack_exit_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)6415 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6416 {
6417 	if (tcp_in_hpts(rack->rc_tp)) {
6418 		tcp_hpts_remove(rack->rc_tp);
6419 		rack->r_ctl.rc_hpts_flags = 0;
6420 	}
6421 #ifdef NETFLIX_SHARED_CWND
6422 	if (rack->r_ctl.rc_scw) {
6423 		tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6424 		rack->rack_scwnd_is_idle = 0;
6425 	}
6426 #endif
6427 	if (rack->rc_gp_dyn_mul &&
6428 	    (rack->use_fixed_rate == 0) &&
6429 	    (rack->rc_always_pace)) {
6430 		/*
6431 		 * Do we count this as if a probe-rtt just
6432 		 * finished?
6433 		 */
6434 		uint32_t time_idle, idle_min;
6435 
6436 		time_idle = cts - rack->r_ctl.rc_went_idle_time;
6437 		idle_min = rack_min_probertt_hold;
6438 		if (rack_probertt_gpsrtt_cnt_div) {
6439 			uint64_t extra;
6440 			extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
6441 				(uint64_t)rack_probertt_gpsrtt_cnt_mul;
6442 			extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
6443 			idle_min += (uint32_t)extra;
6444 		}
6445 		if (time_idle >= idle_min) {
6446 			/* Yes, we count it as a probe-rtt. */
6447 			uint32_t us_cts;
6448 
6449 			us_cts = tcp_get_usecs(NULL);
6450 			if (rack->in_probe_rtt == 0) {
6451 				rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
6452 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
6453 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
6454 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
6455 			} else {
6456 				rack_exit_probertt(rack, us_cts);
6457 			}
6458 		}
6459 	}
6460 	if (rack->r_persist_lt_bw_off) {
6461 		/* Continue where we left off */
6462 		rack->r_ctl.lt_timemark = tcp_get_u64_usecs(NULL);
6463 		rack->lt_bw_up = 1;
6464 		rack->r_persist_lt_bw_off = 0;
6465 	}
6466 	rack->rc_in_persist = 0;
6467 	rack->r_ctl.rc_went_idle_time = 0;
6468 	tp->t_rxtshift = 0;
6469 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6470 	   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6471 	rack->r_ctl.rc_agg_delayed = 0;
6472 	rack->r_early = 0;
6473 	rack->r_late = 0;
6474 	rack->r_ctl.rc_agg_early = 0;
6475 }
6476 
6477 static void
rack_log_hpts_diag(struct tcp_rack * rack,uint32_t cts,struct hpts_diag * diag,struct timeval * tv)6478 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
6479 		   struct hpts_diag *diag, struct timeval *tv)
6480 {
6481 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6482 		union tcp_log_stackspecific log;
6483 
6484 		memset(&log, 0, sizeof(log));
6485 		log.u_bbr.flex1 = diag->p_nxt_slot;
6486 		log.u_bbr.flex2 = diag->p_cur_slot;
6487 		log.u_bbr.flex3 = diag->slot_req;
6488 		log.u_bbr.flex4 = diag->inp_hptsslot;
6489 		log.u_bbr.flex5 = diag->slot_remaining;
6490 		log.u_bbr.flex6 = diag->need_new_to;
6491 		log.u_bbr.flex7 = diag->p_hpts_active;
6492 		log.u_bbr.flex8 = diag->p_on_min_sleep;
6493 		/* Hijack other fields as needed */
6494 		log.u_bbr.epoch = diag->have_slept;
6495 		log.u_bbr.lt_epoch = diag->yet_to_sleep;
6496 		log.u_bbr.pkts_out = diag->co_ret;
6497 		log.u_bbr.applimited = diag->hpts_sleep_time;
6498 		log.u_bbr.delivered = diag->p_prev_slot;
6499 		log.u_bbr.inflight = diag->p_runningslot;
6500 		log.u_bbr.bw_inuse = diag->wheel_slot;
6501 		log.u_bbr.rttProp = diag->wheel_cts;
6502 		log.u_bbr.timeStamp = cts;
6503 		log.u_bbr.delRate = diag->maxslots;
6504 		log.u_bbr.cur_del_rate = diag->p_curtick;
6505 		log.u_bbr.cur_del_rate <<= 32;
6506 		log.u_bbr.cur_del_rate |= diag->p_lasttick;
6507 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6508 		    &rack->rc_inp->inp_socket->so_rcv,
6509 		    &rack->rc_inp->inp_socket->so_snd,
6510 		    BBR_LOG_HPTSDIAG, 0,
6511 		    0, &log, false, tv);
6512 	}
6513 
6514 }
6515 
6516 static void
rack_log_wakeup(struct tcpcb * tp,struct tcp_rack * rack,struct sockbuf * sb,uint32_t len,int type)6517 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
6518 {
6519 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6520 		union tcp_log_stackspecific log;
6521 		struct timeval tv;
6522 
6523 		memset(&log, 0, sizeof(log));
6524 		log.u_bbr.flex1 = sb->sb_flags;
6525 		log.u_bbr.flex2 = len;
6526 		log.u_bbr.flex3 = sb->sb_state;
6527 		log.u_bbr.flex8 = type;
6528 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6529 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
6530 		    &rack->rc_inp->inp_socket->so_rcv,
6531 		    &rack->rc_inp->inp_socket->so_snd,
6532 		    TCP_LOG_SB_WAKE, 0,
6533 		    len, &log, false, &tv);
6534 	}
6535 }
6536 
6537 static void
rack_start_hpts_timer(struct tcp_rack * rack,struct tcpcb * tp,uint32_t cts,int32_t slot,uint32_t tot_len_this_send,int sup_rack)6538 rack_start_hpts_timer (struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
6539       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
6540 {
6541 	struct hpts_diag diag;
6542 	struct inpcb *inp = tptoinpcb(tp);
6543 	struct timeval tv;
6544 	uint32_t delayed_ack = 0;
6545 	uint32_t hpts_timeout;
6546 	uint32_t entry_slot = slot;
6547 	uint8_t stopped;
6548 	uint32_t left = 0;
6549 	uint32_t us_cts;
6550 
6551 	if ((tp->t_state == TCPS_CLOSED) ||
6552 	    (tp->t_state == TCPS_LISTEN)) {
6553 		return;
6554 	}
6555 	if (tcp_in_hpts(tp)) {
6556 		/* Already on the pacer */
6557 		return;
6558 	}
6559 	stopped = rack->rc_tmr_stopped;
6560 	if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
6561 		left = rack->r_ctl.rc_timer_exp - cts;
6562 	}
6563 	rack->r_ctl.rc_timer_exp = 0;
6564 	rack->r_ctl.rc_hpts_flags = 0;
6565 	us_cts = tcp_get_usecs(&tv);
6566 	/* Now early/late accounting */
6567 	rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
6568 	if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
6569 		/*
6570 		 * We have a early carry over set,
6571 		 * we can always add more time so we
6572 		 * can always make this compensation.
6573 		 *
6574 		 * Note if ack's are allowed to wake us do not
6575 		 * penalize the next timer for being awoke
6576 		 * by an ack aka the rc_agg_early (non-paced mode).
6577 		 */
6578 		slot += rack->r_ctl.rc_agg_early;
6579 		rack->r_early = 0;
6580 		rack->r_ctl.rc_agg_early = 0;
6581 	}
6582 	if ((rack->r_late) &&
6583 	    ((rack->r_use_hpts_min == 0) || (rack->dgp_on == 0))) {
6584 		/*
6585 		 * This is harder, we can
6586 		 * compensate some but it
6587 		 * really depends on what
6588 		 * the current pacing time is.
6589 		 */
6590 		if (rack->r_ctl.rc_agg_delayed >= slot) {
6591 			/*
6592 			 * We can't compensate for it all.
6593 			 * And we have to have some time
6594 			 * on the clock. We always have a min
6595 			 * 10 slots (10 x 10 i.e. 100 usecs).
6596 			 */
6597 			if (slot <= HPTS_TICKS_PER_SLOT) {
6598 				/* We gain delay */
6599 				rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
6600 				slot = HPTS_TICKS_PER_SLOT;
6601 			} else {
6602 				/* We take off some */
6603 				rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
6604 				slot = HPTS_TICKS_PER_SLOT;
6605 			}
6606 		} else {
6607 			slot -= rack->r_ctl.rc_agg_delayed;
6608 			rack->r_ctl.rc_agg_delayed = 0;
6609 			/* Make sure we have 100 useconds at minimum */
6610 			if (slot < HPTS_TICKS_PER_SLOT) {
6611 				rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
6612 				slot = HPTS_TICKS_PER_SLOT;
6613 			}
6614 			if (rack->r_ctl.rc_agg_delayed == 0)
6615 				rack->r_late = 0;
6616 		}
6617 	} else if (rack->r_late) {
6618 		/* r_use_hpts_min is on and so is DGP */
6619 		uint32_t max_red;
6620 
6621 		max_red = (slot * rack->r_ctl.max_reduction) / 100;
6622 		if (max_red >= rack->r_ctl.rc_agg_delayed) {
6623 			slot -= rack->r_ctl.rc_agg_delayed;
6624 			rack->r_ctl.rc_agg_delayed = 0;
6625 		} else {
6626 			slot -= max_red;
6627 			rack->r_ctl.rc_agg_delayed -= max_red;
6628 		}
6629 	}
6630 	if ((rack->r_use_hpts_min == 1) &&
6631 	    (slot > 0) &&
6632 	    (rack->dgp_on == 1)) {
6633 		/*
6634 		 * We are enforcing a min pacing timer
6635 		 * based on our hpts min timeout.
6636 		 */
6637 		uint32_t min;
6638 
6639 		min = get_hpts_min_sleep_time();
6640 		if (min > slot) {
6641 			slot = min;
6642 		}
6643 	}
6644 	hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
6645 	if (tp->t_flags & TF_DELACK) {
6646 		delayed_ack = TICKS_2_USEC(tcp_delacktime);
6647 		rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
6648 	}
6649 	if (delayed_ack && ((hpts_timeout == 0) ||
6650 			    (delayed_ack < hpts_timeout)))
6651 		hpts_timeout = delayed_ack;
6652 	else
6653 		rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6654 	/*
6655 	 * If no timers are going to run and we will fall off the hptsi
6656 	 * wheel, we resort to a keep-alive timer if its configured.
6657 	 */
6658 	if ((hpts_timeout == 0) &&
6659 	    (slot == 0)) {
6660 		if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6661 		    (tp->t_state <= TCPS_CLOSING)) {
6662 			/*
6663 			 * Ok we have no timer (persists, rack, tlp, rxt  or
6664 			 * del-ack), we don't have segments being paced. So
6665 			 * all that is left is the keepalive timer.
6666 			 */
6667 			if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6668 				/* Get the established keep-alive time */
6669 				hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
6670 			} else {
6671 				/*
6672 				 * Get the initial setup keep-alive time,
6673 				 * note that this is probably not going to
6674 				 * happen, since rack will be running a rxt timer
6675 				 * if a SYN of some sort is outstanding. It is
6676 				 * actually handled in rack_timeout_rxt().
6677 				 */
6678 				hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
6679 			}
6680 			rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
6681 			if (rack->in_probe_rtt) {
6682 				/*
6683 				 * We want to instead not wake up a long time from
6684 				 * now but to wake up about the time we would
6685 				 * exit probe-rtt and initiate a keep-alive ack.
6686 				 * This will get us out of probe-rtt and update
6687 				 * our min-rtt.
6688 				 */
6689 				hpts_timeout = rack_min_probertt_hold;
6690 			}
6691 		}
6692 	}
6693 	if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
6694 	    (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
6695 		/*
6696 		 * RACK, TLP, persists and RXT timers all are restartable
6697 		 * based on actions input .. i.e we received a packet (ack
6698 		 * or sack) and that changes things (rw, or snd_una etc).
6699 		 * Thus we can restart them with a new value. For
6700 		 * keep-alive, delayed_ack we keep track of what was left
6701 		 * and restart the timer with a smaller value.
6702 		 */
6703 		if (left < hpts_timeout)
6704 			hpts_timeout = left;
6705 	}
6706 	if (hpts_timeout) {
6707 		/*
6708 		 * Hack alert for now we can't time-out over 2,147,483
6709 		 * seconds (a bit more than 596 hours), which is probably ok
6710 		 * :).
6711 		 */
6712 		if (hpts_timeout > 0x7ffffffe)
6713 			hpts_timeout = 0x7ffffffe;
6714 		rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
6715 	}
6716 	rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
6717 	if ((rack->gp_ready == 0) &&
6718 	    (rack->use_fixed_rate == 0) &&
6719 	    (hpts_timeout < slot) &&
6720 	    (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
6721 		/*
6722 		 * We have no good estimate yet for the
6723 		 * old clunky burst mitigation or the
6724 		 * real pacing. And the tlp or rxt is smaller
6725 		 * than the pacing calculation. Lets not
6726 		 * pace that long since we know the calculation
6727 		 * so far is not accurate.
6728 		 */
6729 		slot = hpts_timeout;
6730 	}
6731 	/**
6732 	 * Turn off all the flags for queuing by default. The
6733 	 * flags have important meanings to what happens when
6734 	 * LRO interacts with the transport. Most likely (by default now)
6735 	 * mbuf_queueing and ack compression are on. So the transport
6736 	 * has a couple of flags that control what happens (if those
6737 	 * are not on then these flags won't have any effect since it
6738 	 * won't go through the queuing LRO path).
6739 	 *
6740 	 * TF2_MBUF_QUEUE_READY - This flags says that I am busy
6741 	 *                        pacing output, so don't disturb. But
6742 	 *                        it also means LRO can wake me if there
6743 	 *                        is a SACK arrival.
6744 	 *
6745 	 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction
6746 	 *                       with the above flag (QUEUE_READY) and
6747 	 *                       when present it says don't even wake me
6748 	 *                       if a SACK arrives.
6749 	 *
6750 	 * The idea behind these flags is that if we are pacing we
6751 	 * set the MBUF_QUEUE_READY and only get woken up if
6752 	 * a SACK arrives (which could change things) or if
6753 	 * our pacing timer expires. If, however, we have a rack
6754 	 * timer running, then we don't even want a sack to wake
6755 	 * us since the rack timer has to expire before we can send.
6756 	 *
6757 	 * Other cases should usually have none of the flags set
6758 	 * so LRO can call into us.
6759 	 */
6760 	tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY);
6761 	if (slot) {
6762 		rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
6763 		rack->r_ctl.rc_last_output_to = us_cts + slot;
6764 		/*
6765 		 * A pacing timer (slot) is being set, in
6766 		 * such a case we cannot send (we are blocked by
6767 		 * the timer). So lets tell LRO that it should not
6768 		 * wake us unless there is a SACK. Note this only
6769 		 * will be effective if mbuf queueing is on or
6770 		 * compressed acks are being processed.
6771 		 */
6772 		tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
6773 		/*
6774 		 * But wait if we have a Rack timer running
6775 		 * even a SACK should not disturb us (with
6776 		 * the exception of r_rr_config 3).
6777 		 */
6778 		if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) ||
6779 		    (IN_RECOVERY(tp->t_flags))) {
6780 			if (rack->r_rr_config != 3)
6781 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6782 			else if (rack->rc_pace_dnd) {
6783 				/*
6784 				 * When DND is on, we only let a sack
6785 				 * interrupt us if we are not in recovery.
6786 				 *
6787 				 * If DND is off, then we never hit here
6788 				 * and let all sacks wake us up.
6789 				 *
6790 				 */
6791 				tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6792 			}
6793 		}
6794 		if (rack->rc_ack_can_sendout_data) {
6795 			/*
6796 			 * Ahh but wait, this is that special case
6797 			 * where the pacing timer can be disturbed
6798 			 * backout the changes (used for non-paced
6799 			 * burst limiting).
6800 			 */
6801 			tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE |
6802 			    TF2_MBUF_QUEUE_READY);
6803 		}
6804 		if ((rack->use_rack_rr) &&
6805 		    (rack->r_rr_config < 2) &&
6806 		    ((hpts_timeout) && (hpts_timeout < slot))) {
6807 			/*
6808 			 * Arrange for the hpts to kick back in after the
6809 			 * t-o if the t-o does not cause a send.
6810 			 */
6811 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6812 						   __LINE__, &diag);
6813 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6814 			rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6815 		} else {
6816 			(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
6817 						   __LINE__, &diag);
6818 			rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6819 			rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6820 		}
6821 	} else if (hpts_timeout) {
6822 		/*
6823 		 * With respect to t_flags2(?) here, lets let any new acks wake
6824 		 * us up here. Since we are not pacing (no pacing timer), output
6825 		 * can happen so we should let it. If its a Rack timer, then any inbound
6826 		 * packet probably won't change the sending (we will be blocked)
6827 		 * but it may change the prr stats so letting it in (the set defaults
6828 		 * at the start of this block) are good enough.
6829 		 */
6830 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6831 		(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6832 					   __LINE__, &diag);
6833 		rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6834 		rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6835 	} else {
6836 		/* No timer starting */
6837 #ifdef INVARIANTS
6838 		if (SEQ_GT(tp->snd_max, tp->snd_una)) {
6839 			panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
6840 			    tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
6841 		}
6842 #endif
6843 	}
6844 	rack->rc_tmr_stopped = 0;
6845 	if (slot)
6846 		rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
6847 }
6848 
6849 static void
rack_mark_lost(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t cts)6850 rack_mark_lost(struct tcpcb *tp,
6851     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
6852 {
6853 	struct rack_sendmap *nrsm;
6854 	uint32_t thresh,  exp;
6855 
6856 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
6857 	nrsm = rsm;
6858 	TAILQ_FOREACH_FROM(nrsm, &rack->r_ctl.rc_tmap, r_tnext) {
6859 		if ((nrsm->r_flags & RACK_SACK_PASSED) == 0) {
6860 			/* Got up to all that were marked sack-passed */
6861 			break;
6862 		}
6863 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
6864 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
6865 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
6866 				/* We now consider it lost */
6867 				nrsm->r_flags |= RACK_WAS_LOST;
6868 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
6869 			} else {
6870 				/* Past here it won't be lost so stop */
6871 				break;
6872 			}
6873 		}
6874 	}
6875 }
6876 
6877 /*
6878  * RACK Timer, here we simply do logging and house keeping.
6879  * the normal rack_output() function will call the
6880  * appropriate thing to check if we need to do a RACK retransmit.
6881  * We return 1, saying don't proceed with rack_output only
6882  * when all timers have been stopped (destroyed PCB?).
6883  */
6884 static int
rack_timeout_rack(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)6885 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6886 {
6887 	/*
6888 	 * This timer simply provides an internal trigger to send out data.
6889 	 * The check_recovery_mode call will see if there are needed
6890 	 * retransmissions, if so we will enter fast-recovery. The output
6891 	 * call may or may not do the same thing depending on sysctl
6892 	 * settings.
6893 	 */
6894 	struct rack_sendmap *rsm;
6895 
6896 	counter_u64_add(rack_to_tot, 1);
6897 	if (rack->r_state && (rack->r_state != tp->t_state))
6898 		rack_set_state(tp, rack);
6899 	rack->rc_on_min_to = 0;
6900 	rsm = rack_check_recovery_mode(tp, cts);
6901 	rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
6902 	if (rsm) {
6903 		/* We need to stroke any lost that are now declared as lost */
6904 		rack_mark_lost(tp, rack, rsm, cts);
6905 		rack->r_ctl.rc_resend = rsm;
6906 		rack->r_timer_override = 1;
6907 		if (rack->use_rack_rr) {
6908 			/*
6909 			 * Don't accumulate extra pacing delay
6910 			 * we are allowing the rack timer to
6911 			 * over-ride pacing i.e. rrr takes precedence
6912 			 * if the pacing interval is longer than the rrr
6913 			 * time (in other words we get the min pacing
6914 			 * time versus rrr pacing time).
6915 			 */
6916 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6917 		}
6918 	}
6919 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6920 	if (rsm == NULL) {
6921 		/* restart a timer and return 1 */
6922 		rack_start_hpts_timer(rack, tp, cts,
6923 				      0, 0, 0);
6924 		return (1);
6925 	}
6926 	return (0);
6927 }
6928 
6929 
6930 
6931 static void
rack_adjust_orig_mlen(struct rack_sendmap * rsm)6932 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6933 {
6934 
6935 	if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) {
6936 		/*
6937 		 * The trailing space changed, mbufs can grow
6938 		 * at the tail but they can't shrink from
6939 		 * it, KASSERT that. Adjust the orig_m_len to
6940 		 * compensate for this change.
6941 		 */
6942 		KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)),
6943 			("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
6944 			 rsm->m,
6945 			 rsm,
6946 			 (intmax_t)M_TRAILINGROOM(rsm->m),
6947 			 rsm->orig_t_space,
6948 			 rsm->orig_m_len,
6949 			 rsm->m->m_len));
6950 		rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m));
6951 		rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
6952 	}
6953 	if (rsm->m->m_len < rsm->orig_m_len) {
6954 		/*
6955 		 * Mbuf shrank, trimmed off the top by an ack, our
6956 		 * offset changes.
6957 		 */
6958 		KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)),
6959 			("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n",
6960 			 rsm->m, rsm->m->m_len,
6961 			 rsm, rsm->orig_m_len,
6962 			 rsm->soff));
6963 		if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len))
6964 			rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
6965 		else
6966 			rsm->soff = 0;
6967 		rsm->orig_m_len = rsm->m->m_len;
6968 #ifdef INVARIANTS
6969 	} else if (rsm->m->m_len > rsm->orig_m_len) {
6970 		panic("rsm:%p m:%p m_len grew outside of t_space compensation",
6971 		      rsm, rsm->m);
6972 #endif
6973 	}
6974 }
6975 
6976 static void
rack_setup_offset_for_rsm(struct tcp_rack * rack,struct rack_sendmap * src_rsm,struct rack_sendmap * rsm)6977 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
6978 {
6979 	struct mbuf *m;
6980 	uint32_t soff;
6981 
6982 	if (src_rsm->m &&
6983 	    ((src_rsm->orig_m_len != src_rsm->m->m_len) ||
6984 	     (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) {
6985 		/* Fix up the orig_m_len and possibly the mbuf offset */
6986 		rack_adjust_orig_mlen(src_rsm);
6987 	}
6988 	m = src_rsm->m;
6989 	soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
6990 	while (soff >= m->m_len) {
6991 		/* Move out past this mbuf */
6992 		soff -= m->m_len;
6993 		m = m->m_next;
6994 		KASSERT((m != NULL),
6995 			("rsm:%p nrsm:%p hit at soff:%u null m",
6996 			 src_rsm, rsm, soff));
6997 		if (m == NULL) {
6998 			/* This should *not* happen which is why there is a kassert */
6999 			src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7000 					       (src_rsm->r_start - rack->rc_tp->snd_una),
7001 					       &src_rsm->soff);
7002 			src_rsm->orig_m_len = src_rsm->m->m_len;
7003 			src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m);
7004 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7005 					   (rsm->r_start - rack->rc_tp->snd_una),
7006 					   &rsm->soff);
7007 			rsm->orig_m_len = rsm->m->m_len;
7008 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7009 			return;
7010 		}
7011 	}
7012 	rsm->m = m;
7013 	rsm->soff = soff;
7014 	rsm->orig_m_len = m->m_len;
7015 	rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7016 }
7017 
7018 static __inline void
rack_clone_rsm(struct tcp_rack * rack,struct rack_sendmap * nrsm,struct rack_sendmap * rsm,uint32_t start)7019 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
7020 	       struct rack_sendmap *rsm, uint32_t start)
7021 {
7022 	int idx;
7023 
7024 	nrsm->r_start = start;
7025 	nrsm->r_end = rsm->r_end;
7026 	nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
7027 	nrsm->r_act_rxt_cnt = rsm->r_act_rxt_cnt;
7028 	nrsm->r_flags = rsm->r_flags;
7029 	nrsm->r_dupack = rsm->r_dupack;
7030 	nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
7031 	nrsm->r_rtr_bytes = 0;
7032 	nrsm->r_fas = rsm->r_fas;
7033 	nrsm->r_bas = rsm->r_bas;
7034 	tqhash_update_end(rack->r_ctl.tqh, rsm, nrsm->r_start);
7035 	nrsm->r_just_ret = rsm->r_just_ret;
7036 	for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
7037 		nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
7038 	}
7039 	/* Now if we have SYN flag we keep it on the left edge */
7040 	if (nrsm->r_flags & RACK_HAS_SYN)
7041 		nrsm->r_flags &= ~RACK_HAS_SYN;
7042 	/* Now if we have a FIN flag we keep it on the right edge */
7043 	if (rsm->r_flags & RACK_HAS_FIN)
7044 		rsm->r_flags &= ~RACK_HAS_FIN;
7045 	/* Push bit must go to the right edge as well */
7046 	if (rsm->r_flags & RACK_HAD_PUSH)
7047 		rsm->r_flags &= ~RACK_HAD_PUSH;
7048 	/* Clone over the state of the hw_tls flag */
7049 	nrsm->r_hw_tls = rsm->r_hw_tls;
7050 	/*
7051 	 * Now we need to find nrsm's new location in the mbuf chain
7052 	 * we basically calculate a new offset, which is soff +
7053 	 * how much is left in original rsm. Then we walk out the mbuf
7054 	 * chain to find the righ position, it may be the same mbuf
7055 	 * or maybe not.
7056 	 */
7057 	KASSERT(((rsm->m != NULL) ||
7058 		 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
7059 		("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
7060 	if (rsm->m)
7061 		rack_setup_offset_for_rsm(rack, rsm, nrsm);
7062 }
7063 
7064 static struct rack_sendmap *
rack_merge_rsm(struct tcp_rack * rack,struct rack_sendmap * l_rsm,struct rack_sendmap * r_rsm)7065 rack_merge_rsm(struct tcp_rack *rack,
7066 	       struct rack_sendmap *l_rsm,
7067 	       struct rack_sendmap *r_rsm)
7068 {
7069 	/*
7070 	 * We are merging two ack'd RSM's,
7071 	 * the l_rsm is on the left (lower seq
7072 	 * values) and the r_rsm is on the right
7073 	 * (higher seq value). The simplest way
7074 	 * to merge these is to move the right
7075 	 * one into the left. I don't think there
7076 	 * is any reason we need to try to find
7077 	 * the oldest (or last oldest retransmitted).
7078 	 */
7079 	rack_log_map_chg(rack->rc_tp, rack, NULL,
7080 			 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
7081 	tqhash_update_end(rack->r_ctl.tqh, l_rsm, r_rsm->r_end);
7082 	if (l_rsm->r_dupack < r_rsm->r_dupack)
7083 		l_rsm->r_dupack = r_rsm->r_dupack;
7084 	if (r_rsm->r_rtr_bytes)
7085 		l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
7086 	if (r_rsm->r_in_tmap) {
7087 		/* This really should not happen */
7088 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
7089 		r_rsm->r_in_tmap = 0;
7090 	}
7091 
7092 	/* Now the flags */
7093 	if (r_rsm->r_flags & RACK_HAS_FIN)
7094 		l_rsm->r_flags |= RACK_HAS_FIN;
7095 	if (r_rsm->r_flags & RACK_TLP)
7096 		l_rsm->r_flags |= RACK_TLP;
7097 	if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
7098 		l_rsm->r_flags |= RACK_RWND_COLLAPSED;
7099 	if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
7100 	    ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
7101 		/*
7102 		 * If both are app-limited then let the
7103 		 * free lower the count. If right is app
7104 		 * limited and left is not, transfer.
7105 		 */
7106 		l_rsm->r_flags |= RACK_APP_LIMITED;
7107 		r_rsm->r_flags &= ~RACK_APP_LIMITED;
7108 		if (r_rsm == rack->r_ctl.rc_first_appl)
7109 			rack->r_ctl.rc_first_appl = l_rsm;
7110 	}
7111 	tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE);
7112 	/*
7113 	 * We keep the largest value, which is the newest
7114 	 * send. We do this in case a segment that is
7115 	 * joined together and not part of a GP estimate
7116 	 * later gets expanded into the GP estimate.
7117 	 *
7118 	 * We prohibit the merging of unlike kinds i.e.
7119 	 * all pieces that are in the GP estimate can be
7120 	 * merged and all pieces that are not in a GP estimate
7121 	 * can be merged, but not disimilar pieces. Combine
7122 	 * this with taking the highest here and we should
7123 	 * be ok unless of course the client reneges. Then
7124 	 * all bets are off.
7125 	 */
7126 	if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] <
7127 	   r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) {
7128 		l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)];
7129 	}
7130 	/*
7131 	 * When merging two RSM's we also need to consider the ack time and keep
7132 	 * newest. If the ack gets merged into a measurement then that is the
7133 	 * one we will want to be using.
7134 	 */
7135 	if(l_rsm->r_ack_arrival	 < r_rsm->r_ack_arrival)
7136 		l_rsm->r_ack_arrival = r_rsm->r_ack_arrival;
7137 
7138 	if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
7139 		/* Transfer the split limit to the map we free */
7140 		r_rsm->r_limit_type = l_rsm->r_limit_type;
7141 		l_rsm->r_limit_type = 0;
7142 	}
7143 	rack_free(rack, r_rsm);
7144 	l_rsm->r_flags |= RACK_MERGED;
7145 	return (l_rsm);
7146 }
7147 
7148 /*
7149  * TLP Timer, here we simply setup what segment we want to
7150  * have the TLP expire on, the normal rack_output() will then
7151  * send it out.
7152  *
7153  * We return 1, saying don't proceed with rack_output only
7154  * when all timers have been stopped (destroyed PCB?).
7155  */
7156 static int
rack_timeout_tlp(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,uint8_t * doing_tlp)7157 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
7158 {
7159 	/*
7160 	 * Tail Loss Probe.
7161 	 */
7162 	struct rack_sendmap *rsm = NULL;
7163 	int insret __diagused;
7164 	struct socket *so = tptosocket(tp);
7165 	uint32_t amm;
7166 	uint32_t out, avail;
7167 	int collapsed_win = 0;
7168 
7169 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7170 		/* Its not time yet */
7171 		return (0);
7172 	}
7173 	if (ctf_progress_timeout_check(tp, true)) {
7174 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7175 		return (-ETIMEDOUT);	/* tcp_drop() */
7176 	}
7177 	/*
7178 	 * A TLP timer has expired. We have been idle for 2 rtts. So we now
7179 	 * need to figure out how to force a full MSS segment out.
7180 	 */
7181 	rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
7182 	rack->r_ctl.retran_during_recovery = 0;
7183 	rack->r_might_revert = 0;
7184 	rack->r_ctl.dsack_byte_cnt = 0;
7185 	counter_u64_add(rack_tlp_tot, 1);
7186 	if (rack->r_state && (rack->r_state != tp->t_state))
7187 		rack_set_state(tp, rack);
7188 	avail = sbavail(&so->so_snd);
7189 	out = tp->snd_max - tp->snd_una;
7190 	if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
7191 		/* special case, we need a retransmission */
7192 		collapsed_win = 1;
7193 		goto need_retran;
7194 	}
7195 	if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
7196 		rack->r_ctl.dsack_persist--;
7197 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7198 			rack->r_ctl.num_dsack = 0;
7199 		}
7200 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7201 	}
7202 	if ((tp->t_flags & TF_GPUTINPROG) &&
7203 	    (rack->r_ctl.rc_tlp_cnt_out == 1)) {
7204 		/*
7205 		 * If this is the second in a row
7206 		 * TLP and we are doing a measurement
7207 		 * its time to abandon the measurement.
7208 		 * Something is likely broken on
7209 		 * the clients network and measuring a
7210 		 * broken network does us no good.
7211 		 */
7212 		tp->t_flags &= ~TF_GPUTINPROG;
7213 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7214 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7215 					   tp->gput_seq,
7216 					   0, 0, 18, __LINE__, NULL, 0);
7217 	}
7218 	/*
7219 	 * Check our send oldest always settings, and if
7220 	 * there is an oldest to send jump to the need_retran.
7221 	 */
7222 	if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
7223 		goto need_retran;
7224 
7225 	if (avail > out) {
7226 		/* New data is available */
7227 		amm = avail - out;
7228 		if (amm > ctf_fixed_maxseg(tp)) {
7229 			amm = ctf_fixed_maxseg(tp);
7230 			if ((amm + out) > tp->snd_wnd) {
7231 				/* We are rwnd limited */
7232 				goto need_retran;
7233 			}
7234 		} else if (amm < ctf_fixed_maxseg(tp)) {
7235 			/* not enough to fill a MTU */
7236 			goto need_retran;
7237 		}
7238 		if (IN_FASTRECOVERY(tp->t_flags)) {
7239 			/* Unlikely */
7240 			if (rack->rack_no_prr == 0) {
7241 				if (out + amm <= tp->snd_wnd) {
7242 					rack->r_ctl.rc_prr_sndcnt = amm;
7243 					rack->r_ctl.rc_tlp_new_data = amm;
7244 					rack_log_to_prr(rack, 4, 0, __LINE__);
7245 				}
7246 			} else
7247 				goto need_retran;
7248 		} else {
7249 			/* Set the send-new override */
7250 			if (out + amm <= tp->snd_wnd)
7251 				rack->r_ctl.rc_tlp_new_data = amm;
7252 			else
7253 				goto need_retran;
7254 		}
7255 		rack->r_ctl.rc_tlpsend = NULL;
7256 		counter_u64_add(rack_tlp_newdata, 1);
7257 		goto send;
7258 	}
7259 need_retran:
7260 	/*
7261 	 * Ok we need to arrange the last un-acked segment to be re-sent, or
7262 	 * optionally the first un-acked segment.
7263 	 */
7264 	if (collapsed_win == 0) {
7265 		if (rack_always_send_oldest)
7266 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7267 		else {
7268 			rsm = tqhash_max(rack->r_ctl.tqh);
7269 			if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
7270 				rsm = rack_find_high_nonack(rack, rsm);
7271 			}
7272 		}
7273 		if (rsm == NULL) {
7274 #ifdef TCP_BLACKBOX
7275 			tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
7276 #endif
7277 			goto out;
7278 		}
7279 	} else {
7280 		/*
7281 		 * We had a collapsed window, lets find
7282 		 * the point before the collapse.
7283 		 */
7284 		if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una))
7285 			rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1));
7286 		else {
7287 			rsm = tqhash_min(rack->r_ctl.tqh);
7288 		}
7289 		if (rsm == NULL) {
7290 			/* Huh */
7291 			goto out;
7292 		}
7293 	}
7294 	if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
7295 		/*
7296 		 * We need to split this the last segment in two.
7297 		 */
7298 		struct rack_sendmap *nrsm;
7299 
7300 		nrsm = rack_alloc_full_limit(rack);
7301 		if (nrsm == NULL) {
7302 			/*
7303 			 * No memory to split, we will just exit and punt
7304 			 * off to the RXT timer.
7305 			 */
7306 			goto out;
7307 		}
7308 		rack_clone_rsm(rack, nrsm, rsm,
7309 			       (rsm->r_end - ctf_fixed_maxseg(tp)));
7310 		rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7311 #ifndef INVARIANTS
7312 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
7313 #else
7314 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
7315 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
7316 			      nrsm, insret, rack, rsm);
7317 		}
7318 #endif
7319 		if (rsm->r_in_tmap) {
7320 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7321 			nrsm->r_in_tmap = 1;
7322 		}
7323 		rsm = nrsm;
7324 	}
7325 	rack->r_ctl.rc_tlpsend = rsm;
7326 send:
7327 	/* Make sure output path knows we are doing a TLP */
7328 	*doing_tlp = 1;
7329 	rack->r_timer_override = 1;
7330 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7331 	return (0);
7332 out:
7333 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7334 	return (0);
7335 }
7336 
7337 /*
7338  * Delayed ack Timer, here we simply need to setup the
7339  * ACK_NOW flag and remove the DELACK flag. From there
7340  * the output routine will send the ack out.
7341  *
7342  * We only return 1, saying don't proceed, if all timers
7343  * are stopped (destroyed PCB?).
7344  */
7345 static int
rack_timeout_delack(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)7346 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7347 {
7348 
7349 	rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
7350 	tp->t_flags &= ~TF_DELACK;
7351 	tp->t_flags |= TF_ACKNOW;
7352 	KMOD_TCPSTAT_INC(tcps_delack);
7353 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
7354 	return (0);
7355 }
7356 
7357 static inline int
rack_send_ack_challange(struct tcp_rack * rack)7358 rack_send_ack_challange(struct tcp_rack *rack)
7359 {
7360 	struct tcptemp *t_template;
7361 
7362 	t_template = tcpip_maketemplate(rack->rc_inp);
7363 	if (t_template) {
7364 		if (rack->forced_ack == 0) {
7365 			rack->forced_ack = 1;
7366 			rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7367 		} else {
7368 			rack->probe_not_answered = 1;
7369 		}
7370 		tcp_respond(rack->rc_tp, t_template->tt_ipgen,
7371 			    &t_template->tt_t, (struct mbuf *)NULL,
7372 			    rack->rc_tp->rcv_nxt, rack->rc_tp->snd_una - 1, 0);
7373 		free(t_template, M_TEMP);
7374 		/* This does send an ack so kill any D-ack timer */
7375 		if (rack->rc_tp->t_flags & TF_DELACK)
7376 			rack->rc_tp->t_flags &= ~TF_DELACK;
7377 		return(1);
7378 	} else
7379 		return (0);
7380 
7381 }
7382 
7383 /*
7384  * Persists timer, here we simply send the
7385  * same thing as a keepalive will.
7386  * the one byte send.
7387  *
7388  * We only return 1, saying don't proceed, if all timers
7389  * are stopped (destroyed PCB?).
7390  */
7391 static int
rack_timeout_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)7392 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7393 {
7394 	int32_t retval = 1;
7395 
7396 	if (rack->rc_in_persist == 0)
7397 		return (0);
7398 	if (ctf_progress_timeout_check(tp, false)) {
7399 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7400 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7401 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7402 		return (-ETIMEDOUT);	/* tcp_drop() */
7403 	}
7404 	/*
7405 	 * Persistence timer into zero window. Force a byte to be output, if
7406 	 * possible.
7407 	 */
7408 	KMOD_TCPSTAT_INC(tcps_persisttimeo);
7409 	/*
7410 	 * Hack: if the peer is dead/unreachable, we do not time out if the
7411 	 * window is closed.  After a full backoff, drop the connection if
7412 	 * the idle time (no responses to probes) reaches the maximum
7413 	 * backoff that we would use if retransmitting.
7414 	 */
7415 	if (tp->t_rxtshift >= V_tcp_retries &&
7416 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
7417 	     TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
7418 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7419 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7420 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7421 		retval = -ETIMEDOUT;	/* tcp_drop() */
7422 		goto out;
7423 	}
7424 	if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
7425 	    tp->snd_una == tp->snd_max)
7426 		rack_exit_persist(tp, rack, cts);
7427 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
7428 	/*
7429 	 * If the user has closed the socket then drop a persisting
7430 	 * connection after a much reduced timeout.
7431 	 */
7432 	if (tp->t_state > TCPS_CLOSE_WAIT &&
7433 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
7434 		KMOD_TCPSTAT_INC(tcps_persistdrop);
7435 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7436 		counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7437 		retval = -ETIMEDOUT;	/* tcp_drop() */
7438 		goto out;
7439 	}
7440 	if (rack_send_ack_challange(rack)) {
7441 		/* only set it if we were answered */
7442 		if (rack->probe_not_answered) {
7443 			counter_u64_add(rack_persists_loss, 1);
7444 			rack->r_ctl.persist_lost_ends++;
7445 		}
7446 		counter_u64_add(rack_persists_sends, 1);
7447 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
7448 	}
7449 	if (tp->t_rxtshift < V_tcp_retries)
7450 		tp->t_rxtshift++;
7451 out:
7452 	rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
7453 	rack_start_hpts_timer(rack, tp, cts,
7454 			      0, 0, 0);
7455 	return (retval);
7456 }
7457 
7458 /*
7459  * If a keepalive goes off, we had no other timers
7460  * happening. We always return 1 here since this
7461  * routine either drops the connection or sends
7462  * out a segment with respond.
7463  */
7464 static int
rack_timeout_keepalive(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)7465 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7466 {
7467 	struct inpcb *inp = tptoinpcb(tp);
7468 
7469 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
7470 	rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
7471 	/*
7472 	 * Keep-alive timer went off; send something or drop connection if
7473 	 * idle for too long.
7474 	 */
7475 	KMOD_TCPSTAT_INC(tcps_keeptimeo);
7476 	if (tp->t_state < TCPS_ESTABLISHED)
7477 		goto dropit;
7478 	if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
7479 	    tp->t_state <= TCPS_CLOSING) {
7480 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
7481 			goto dropit;
7482 		/*
7483 		 * Send a packet designed to force a response if the peer is
7484 		 * up and reachable: either an ACK if the connection is
7485 		 * still alive, or an RST if the peer has closed the
7486 		 * connection due to timeout or reboot. Using sequence
7487 		 * number tp->snd_una-1 causes the transmitted zero-length
7488 		 * segment to lie outside the receive window; by the
7489 		 * protocol spec, this requires the correspondent TCP to
7490 		 * respond.
7491 		 */
7492 		KMOD_TCPSTAT_INC(tcps_keepprobe);
7493 		rack_send_ack_challange(rack);
7494 	}
7495 	rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
7496 	return (1);
7497 dropit:
7498 	KMOD_TCPSTAT_INC(tcps_keepdrops);
7499 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7500 	return (-ETIMEDOUT);	/* tcp_drop() */
7501 }
7502 
7503 /*
7504  * Retransmit helper function, clear up all the ack
7505  * flags and take care of important book keeping.
7506  */
7507 static void
rack_remxt_tmr(struct tcpcb * tp)7508 rack_remxt_tmr(struct tcpcb *tp)
7509 {
7510 	/*
7511 	 * The retransmit timer went off, all sack'd blocks must be
7512 	 * un-acked.
7513 	 */
7514 	struct rack_sendmap *rsm, *trsm = NULL;
7515 	struct tcp_rack *rack;
7516 
7517 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7518 	rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
7519 	rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
7520 	rack->r_timer_override = 1;
7521 	rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
7522 	rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
7523 	rack->r_late = 0;
7524 	rack->r_early = 0;
7525 	rack->r_ctl.rc_agg_delayed = 0;
7526 	rack->r_ctl.rc_agg_early = 0;
7527 	if (rack->r_state && (rack->r_state != tp->t_state))
7528 		rack_set_state(tp, rack);
7529 	if (tp->t_rxtshift <= rack_rxt_scoreboard_clear_thresh) {
7530 		/*
7531 		 * We do not clear the scoreboard until we have had
7532 		 * more than rack_rxt_scoreboard_clear_thresh time-outs.
7533 		 */
7534 		rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7535 		if (rack->r_ctl.rc_resend != NULL)
7536 			rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7537 
7538 		return;
7539 	}
7540 	/*
7541 	 * Ideally we would like to be able to
7542 	 * mark SACK-PASS on anything not acked here.
7543 	 *
7544 	 * However, if we do that we would burst out
7545 	 * all that data 1ms apart. This would be unwise,
7546 	 * so for now we will just let the normal rxt timer
7547 	 * and tlp timer take care of it.
7548 	 *
7549 	 * Also we really need to stick them back in sequence
7550 	 * order. This way we send in the proper order and any
7551 	 * sacks that come floating in will "re-ack" the data.
7552 	 * To do this we zap the tmap with an INIT and then
7553 	 * walk through and place every rsm in the tail queue
7554 	 * hash table back in its seq ordered place.
7555 	 */
7556 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
7557 
7558 	TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
7559 		rsm->r_dupack = 0;
7560 		if (rack_verbose_logging)
7561 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7562 		/* We must re-add it back to the tlist */
7563 		if (trsm == NULL) {
7564 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7565 		} else {
7566 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
7567 		}
7568 		rsm->r_in_tmap = 1;
7569 		trsm = rsm;
7570 		if (rsm->r_flags & RACK_ACKED)
7571 			rsm->r_flags |= RACK_WAS_ACKED;
7572 		rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED | RACK_WAS_LOST);
7573 		rsm->r_flags |= RACK_MUST_RXT;
7574 	}
7575 	/* zero the lost since it's all gone */
7576 	rack->r_ctl.rc_considered_lost = 0;
7577 	/* Clear the count (we just un-acked them) */
7578 	rack->r_ctl.rc_sacked = 0;
7579 	rack->r_ctl.rc_sacklast = NULL;
7580 	/* Clear the tlp rtx mark */
7581 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7582 	if (rack->r_ctl.rc_resend != NULL)
7583 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7584 	rack->r_ctl.rc_prr_sndcnt = 0;
7585 	rack_log_to_prr(rack, 6, 0, __LINE__);
7586 	rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7587 	if (rack->r_ctl.rc_resend != NULL)
7588 		rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7589 	if (((tp->t_flags & TF_SACK_PERMIT) == 0) &&
7590 	    ((tp->t_flags & TF_SENTFIN) == 0)) {
7591 		/*
7592 		 * For non-sack customers new data
7593 		 * needs to go out as retransmits until
7594 		 * we retransmit up to snd_max.
7595 		 */
7596 		rack->r_must_retran = 1;
7597 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
7598 							    rack->r_ctl.rc_sacked);
7599 	}
7600 }
7601 
7602 static void
rack_convert_rtts(struct tcpcb * tp)7603 rack_convert_rtts(struct tcpcb *tp)
7604 {
7605 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
7606 	tp->t_rxtcur = RACK_REXMTVAL(tp);
7607 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
7608 		tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
7609 	}
7610 	if (tp->t_rxtcur > rack_rto_max) {
7611 		tp->t_rxtcur = rack_rto_max;
7612 	}
7613 }
7614 
7615 static void
rack_cc_conn_init(struct tcpcb * tp)7616 rack_cc_conn_init(struct tcpcb *tp)
7617 {
7618 	struct tcp_rack *rack;
7619 	uint32_t srtt;
7620 
7621 	rack = (struct tcp_rack *)tp->t_fb_ptr;
7622 	srtt = tp->t_srtt;
7623 	cc_conn_init(tp);
7624 	/*
7625 	 * Now convert to rack's internal format,
7626 	 * if required.
7627 	 */
7628 	if ((srtt == 0) && (tp->t_srtt != 0))
7629 		rack_convert_rtts(tp);
7630 	/*
7631 	 * We want a chance to stay in slowstart as
7632 	 * we create a connection. TCP spec says that
7633 	 * initially ssthresh is infinite. For our
7634 	 * purposes that is the snd_wnd.
7635 	 */
7636 	if (tp->snd_ssthresh < tp->snd_wnd) {
7637 		tp->snd_ssthresh = tp->snd_wnd;
7638 	}
7639 	/*
7640 	 * We also want to assure a IW worth of
7641 	 * data can get inflight.
7642 	 */
7643 	if (rc_init_window(rack) < tp->snd_cwnd)
7644 		tp->snd_cwnd = rc_init_window(rack);
7645 }
7646 
7647 /*
7648  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
7649  * we will setup to retransmit the lowest seq number outstanding.
7650  */
7651 static int
rack_timeout_rxt(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)7652 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7653 {
7654 	struct inpcb *inp = tptoinpcb(tp);
7655 	int32_t rexmt;
7656 	int32_t retval = 0;
7657 	bool isipv6;
7658 
7659 	if ((tp->t_flags & TF_GPUTINPROG) &&
7660 	    (tp->t_rxtshift)) {
7661 		/*
7662 		 * We have had a second timeout
7663 		 * measurements on successive rxt's are not profitable.
7664 		 * It is unlikely to be of any use (the network is
7665 		 * broken or the client went away).
7666 		 */
7667 		tp->t_flags &= ~TF_GPUTINPROG;
7668 		rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7669 					   rack->r_ctl.rc_gp_srtt /*flex1*/,
7670 					   tp->gput_seq,
7671 					   0, 0, 18, __LINE__, NULL, 0);
7672 	}
7673 	if (ctf_progress_timeout_check(tp, false)) {
7674 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7675 		rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7676 		return (-ETIMEDOUT);	/* tcp_drop() */
7677 	}
7678 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
7679 	rack->r_ctl.retran_during_recovery = 0;
7680 	rack->rc_ack_required = 1;
7681 	rack->r_ctl.dsack_byte_cnt = 0;
7682 	if (IN_RECOVERY(tp->t_flags) &&
7683 	    (rack->rto_from_rec == 0)) {
7684 		/*
7685 		 * Mark that we had a rto while in recovery
7686 		 * and save the ssthresh so if we go back
7687 		 * into recovery we will have a chance
7688 		 * to slowstart back to the level.
7689 		 */
7690 		rack->rto_from_rec = 1;
7691 		rack->r_ctl.rto_ssthresh = tp->snd_ssthresh;
7692 	}
7693 	if (IN_FASTRECOVERY(tp->t_flags))
7694 		tp->t_flags |= TF_WASFRECOVERY;
7695 	else
7696 		tp->t_flags &= ~TF_WASFRECOVERY;
7697 	if (IN_CONGRECOVERY(tp->t_flags))
7698 		tp->t_flags |= TF_WASCRECOVERY;
7699 	else
7700 		tp->t_flags &= ~TF_WASCRECOVERY;
7701 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
7702 	    (tp->snd_una == tp->snd_max)) {
7703 		/* Nothing outstanding .. nothing to do */
7704 		return (0);
7705 	}
7706 	if (rack->r_ctl.dsack_persist) {
7707 		rack->r_ctl.dsack_persist--;
7708 		if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7709 			rack->r_ctl.num_dsack = 0;
7710 		}
7711 		rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7712 	}
7713 	/*
7714 	 * Rack can only run one timer  at a time, so we cannot
7715 	 * run a KEEPINIT (gating SYN sending) and a retransmit
7716 	 * timer for the SYN. So if we are in a front state and
7717 	 * have a KEEPINIT timer we need to check the first transmit
7718 	 * against now to see if we have exceeded the KEEPINIT time
7719 	 * (if one is set).
7720 	 */
7721 	if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
7722 	    (TP_KEEPINIT(tp) != 0)) {
7723 		struct rack_sendmap *rsm;
7724 
7725 		rsm = tqhash_min(rack->r_ctl.tqh);
7726 		if (rsm) {
7727 			/* Ok we have something outstanding to test keepinit with */
7728 			if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
7729 			    ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
7730 				/* We have exceeded the KEEPINIT time */
7731 				tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7732 				goto drop_it;
7733 			}
7734 		}
7735 	}
7736 	/*
7737 	 * Retransmission timer went off.  Message has not been acked within
7738 	 * retransmit interval.  Back off to a longer retransmit interval
7739 	 * and retransmit one segment.
7740 	 */
7741 	if ((rack->r_ctl.rc_resend == NULL) ||
7742 	    ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
7743 		/*
7744 		 * If the rwnd collapsed on
7745 		 * the one we are retransmitting
7746 		 * it does not count against the
7747 		 * rxt count.
7748 		 */
7749 		tp->t_rxtshift++;
7750 	}
7751 	rack_remxt_tmr(tp);
7752 	if (tp->t_rxtshift > V_tcp_retries) {
7753 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7754 drop_it:
7755 		tp->t_rxtshift = V_tcp_retries;
7756 		KMOD_TCPSTAT_INC(tcps_timeoutdrop);
7757 		/* XXXGL: previously t_softerror was casted to uint16_t */
7758 		MPASS(tp->t_softerror >= 0);
7759 		retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
7760 		goto out;	/* tcp_drop() */
7761 	}
7762 	if (tp->t_state == TCPS_SYN_SENT) {
7763 		/*
7764 		 * If the SYN was retransmitted, indicate CWND to be limited
7765 		 * to 1 segment in cc_conn_init().
7766 		 */
7767 		tp->snd_cwnd = 1;
7768 	} else if (tp->t_rxtshift == 1) {
7769 		/*
7770 		 * first retransmit; record ssthresh and cwnd so they can be
7771 		 * recovered if this turns out to be a "bad" retransmit. A
7772 		 * retransmit is considered "bad" if an ACK for this segment
7773 		 * is received within RTT/2 interval; the assumption here is
7774 		 * that the ACK was already in flight.  See "On Estimating
7775 		 * End-to-End Network Path Properties" by Allman and Paxson
7776 		 * for more details.
7777 		 */
7778 		tp->snd_cwnd_prev = tp->snd_cwnd;
7779 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
7780 		tp->snd_recover_prev = tp->snd_recover;
7781 		tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
7782 		tp->t_flags |= TF_PREVVALID;
7783 	} else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
7784 		tp->t_flags &= ~TF_PREVVALID;
7785 	KMOD_TCPSTAT_INC(tcps_rexmttimeo);
7786 	if ((tp->t_state == TCPS_SYN_SENT) ||
7787 	    (tp->t_state == TCPS_SYN_RECEIVED))
7788 		rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
7789 	else
7790 		rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
7791 
7792 	RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
7793 	   max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
7794 	/*
7795 	 * We enter the path for PLMTUD if connection is established or, if
7796 	 * connection is FIN_WAIT_1 status, reason for the last is that if
7797 	 * amount of data we send is very small, we could send it in couple
7798 	 * of packets and process straight to FIN. In that case we won't
7799 	 * catch ESTABLISHED state.
7800 	 */
7801 #ifdef INET6
7802 	isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
7803 #else
7804 	isipv6 = false;
7805 #endif
7806 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
7807 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
7808 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
7809 	    ((tp->t_state == TCPS_ESTABLISHED) ||
7810 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
7811 		/*
7812 		 * Idea here is that at each stage of mtu probe (usually,
7813 		 * 1448 -> 1188 -> 524) should be given 2 chances to recover
7814 		 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
7815 		 * should take care of that.
7816 		 */
7817 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
7818 		    (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
7819 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
7820 		    tp->t_rxtshift % 2 == 0)) {
7821 			/*
7822 			 * Enter Path MTU Black-hole Detection mechanism: -
7823 			 * Disable Path MTU Discovery (IP "DF" bit). -
7824 			 * Reduce MTU to lower value than what we negotiated
7825 			 * with peer.
7826 			 */
7827 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
7828 				/* Record that we may have found a black hole. */
7829 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
7830 				/* Keep track of previous MSS. */
7831 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7832 			}
7833 
7834 			/*
7835 			 * Reduce the MSS to blackhole value or to the
7836 			 * default in an attempt to retransmit.
7837 			 */
7838 #ifdef INET6
7839 			if (isipv6 &&
7840 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
7841 				/* Use the sysctl tuneable blackhole MSS. */
7842 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
7843 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7844 			} else if (isipv6) {
7845 				/* Use the default MSS. */
7846 				tp->t_maxseg = V_tcp_v6mssdflt;
7847 				/*
7848 				 * Disable Path MTU Discovery when we switch
7849 				 * to minmss.
7850 				 */
7851 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7852 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7853 			}
7854 #endif
7855 #if defined(INET6) && defined(INET)
7856 			else
7857 #endif
7858 #ifdef INET
7859 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
7860 				/* Use the sysctl tuneable blackhole MSS. */
7861 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7862 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7863 			} else {
7864 				/* Use the default MSS. */
7865 				tp->t_maxseg = V_tcp_mssdflt;
7866 				/*
7867 				 * Disable Path MTU Discovery when we switch
7868 				 * to minmss.
7869 				 */
7870 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7871 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7872 			}
7873 #endif
7874 		} else {
7875 			/*
7876 			 * If further retransmissions are still unsuccessful
7877 			 * with a lowered MTU, maybe this isn't a blackhole
7878 			 * and we restore the previous MSS and blackhole
7879 			 * detection flags. The limit '6' is determined by
7880 			 * giving each probe stage (1448, 1188, 524) 2
7881 			 * chances to recover.
7882 			 */
7883 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7884 			    (tp->t_rxtshift >= 6)) {
7885 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
7886 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7887 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
7888 				if (tp->t_maxseg < V_tcp_mssdflt) {
7889 					/*
7890 					 * The MSS is so small we should not
7891 					 * process incoming SACK's since we are
7892 					 * subject to attack in such a case.
7893 					 */
7894 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
7895 				} else {
7896 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
7897 				}
7898 				KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
7899 			}
7900 		}
7901 	}
7902 	/*
7903 	 * Disable RFC1323 and SACK if we haven't got any response to
7904 	 * our third SYN to work-around some broken terminal servers
7905 	 * (most of which have hopefully been retired) that have bad VJ
7906 	 * header compression code which trashes TCP segments containing
7907 	 * unknown-to-them TCP options.
7908 	 */
7909 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7910 	    (tp->t_rxtshift == 3))
7911 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7912 	/*
7913 	 * If we backed off this far, our srtt estimate is probably bogus.
7914 	 * Clobber it so we'll take the next rtt measurement as our srtt;
7915 	 * move the current srtt into rttvar to keep the current retransmit
7916 	 * times until then.
7917 	 */
7918 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7919 #ifdef INET6
7920 		if ((inp->inp_vflag & INP_IPV6) != 0)
7921 			in6_losing(inp);
7922 		else
7923 #endif
7924 			in_losing(inp);
7925 		tp->t_rttvar += tp->t_srtt;
7926 		tp->t_srtt = 0;
7927 	}
7928 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
7929 	tp->snd_recover = tp->snd_max;
7930 	tp->t_flags |= TF_ACKNOW;
7931 	tp->t_rtttime = 0;
7932 	rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
7933 out:
7934 	return (retval);
7935 }
7936 
7937 static int
rack_process_timers(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,uint8_t hpts_calling,uint8_t * doing_tlp)7938 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7939 {
7940 	int32_t ret = 0;
7941 	int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7942 
7943 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7944 	    (tp->t_flags & TF_GPUTINPROG)) {
7945 		/*
7946 		 * We have a goodput in progress
7947 		 * and we have entered a late state.
7948 		 * Do we have enough data in the sb
7949 		 * to handle the GPUT request?
7950 		 */
7951 		uint32_t bytes;
7952 
7953 		bytes = tp->gput_ack - tp->gput_seq;
7954 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
7955 			bytes += tp->gput_seq - tp->snd_una;
7956 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
7957 			/*
7958 			 * There are not enough bytes in the socket
7959 			 * buffer that have been sent to cover this
7960 			 * measurement. Cancel it.
7961 			 */
7962 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7963 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
7964 						   tp->gput_seq,
7965 						   0, 0, 18, __LINE__, NULL, 0);
7966 			tp->t_flags &= ~TF_GPUTINPROG;
7967 		}
7968 	}
7969 	if (timers == 0) {
7970 		return (0);
7971 	}
7972 	if (tp->t_state == TCPS_LISTEN) {
7973 		/* no timers on listen sockets */
7974 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
7975 			return (0);
7976 		return (1);
7977 	}
7978 	if ((timers & PACE_TMR_RACK) &&
7979 	    rack->rc_on_min_to) {
7980 		/*
7981 		 * For the rack timer when we
7982 		 * are on a min-timeout (which means rrr_conf = 3)
7983 		 * we don't want to check the timer. It may
7984 		 * be going off for a pace and thats ok we
7985 		 * want to send the retransmit (if its ready).
7986 		 *
7987 		 * If its on a normal rack timer (non-min) then
7988 		 * we will check if its expired.
7989 		 */
7990 		goto skip_time_check;
7991 	}
7992 	if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7993 		uint32_t left;
7994 
7995 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
7996 			ret = -1;
7997 			rack_log_to_processing(rack, cts, ret, 0);
7998 			return (0);
7999 		}
8000 		if (hpts_calling == 0) {
8001 			/*
8002 			 * A user send or queued mbuf (sack) has called us? We
8003 			 * return 0 and let the pacing guards
8004 			 * deal with it if they should or
8005 			 * should not cause a send.
8006 			 */
8007 			ret = -2;
8008 			rack_log_to_processing(rack, cts, ret, 0);
8009 			return (0);
8010 		}
8011 		/*
8012 		 * Ok our timer went off early and we are not paced false
8013 		 * alarm, go back to sleep. We make sure we don't have
8014 		 * no-sack wakeup on since we no longer have a PKT_OUTPUT
8015 		 * flag in place.
8016 		 */
8017 		rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
8018 		ret = -3;
8019 		left = rack->r_ctl.rc_timer_exp - cts;
8020 		tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left));
8021 		rack_log_to_processing(rack, cts, ret, left);
8022 		return (1);
8023 	}
8024 skip_time_check:
8025 	rack->rc_tmr_stopped = 0;
8026 	rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
8027 	if (timers & PACE_TMR_DELACK) {
8028 		ret = rack_timeout_delack(tp, rack, cts);
8029 	} else if (timers & PACE_TMR_RACK) {
8030 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8031 		rack->r_fast_output = 0;
8032 		ret = rack_timeout_rack(tp, rack, cts);
8033 	} else if (timers & PACE_TMR_TLP) {
8034 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8035 		ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
8036 	} else if (timers & PACE_TMR_RXT) {
8037 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
8038 		rack->r_fast_output = 0;
8039 		ret = rack_timeout_rxt(tp, rack, cts);
8040 	} else if (timers & PACE_TMR_PERSIT) {
8041 		ret = rack_timeout_persist(tp, rack, cts);
8042 	} else if (timers & PACE_TMR_KEEP) {
8043 		ret = rack_timeout_keepalive(tp, rack, cts);
8044 	}
8045 	rack_log_to_processing(rack, cts, ret, timers);
8046 	return (ret);
8047 }
8048 
8049 static void
rack_timer_cancel(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,int line)8050 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
8051 {
8052 	struct timeval tv;
8053 	uint32_t us_cts, flags_on_entry;
8054 	uint8_t hpts_removed = 0;
8055 
8056 	flags_on_entry = rack->r_ctl.rc_hpts_flags;
8057 	us_cts = tcp_get_usecs(&tv);
8058 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
8059 	    ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
8060 	     ((tp->snd_max - tp->snd_una) == 0))) {
8061 		tcp_hpts_remove(rack->rc_tp);
8062 		hpts_removed = 1;
8063 		/* If we were not delayed cancel out the flag. */
8064 		if ((tp->snd_max - tp->snd_una) == 0)
8065 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8066 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8067 	}
8068 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
8069 		rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
8070 		if (tcp_in_hpts(rack->rc_tp) &&
8071 		    ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
8072 			/*
8073 			 * Canceling timer's when we have no output being
8074 			 * paced. We also must remove ourselves from the
8075 			 * hpts.
8076 			 */
8077 			tcp_hpts_remove(rack->rc_tp);
8078 			hpts_removed = 1;
8079 		}
8080 		rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
8081 	}
8082 	if (hpts_removed == 0)
8083 		rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8084 }
8085 
8086 static int
rack_stopall(struct tcpcb * tp)8087 rack_stopall(struct tcpcb *tp)
8088 {
8089 	struct tcp_rack *rack;
8090 
8091 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8092 	rack->t_timers_stopped = 1;
8093 
8094 	tcp_hpts_remove(tp);
8095 
8096 	return (0);
8097 }
8098 
8099 static void
rack_stop_all_timers(struct tcpcb * tp,struct tcp_rack * rack)8100 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack)
8101 {
8102 	/*
8103 	 * Assure no timers are running.
8104 	 */
8105 	if (tcp_timer_active(tp, TT_PERSIST)) {
8106 		/* We enter in persists, set the flag appropriately */
8107 		rack->rc_in_persist = 1;
8108 	}
8109 	if (tcp_in_hpts(rack->rc_tp)) {
8110 		tcp_hpts_remove(rack->rc_tp);
8111 	}
8112 }
8113 
8114 static void
rack_update_rsm(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint64_t ts,uint32_t add_flag,int segsiz)8115 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
8116     struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz)
8117 {
8118 	int32_t idx;
8119 
8120 	rsm->r_rtr_cnt++;
8121 	if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
8122 		rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
8123 		rsm->r_flags |= RACK_OVERMAX;
8124 	}
8125 	rsm->r_act_rxt_cnt++;
8126 	/* Peg the count/index */
8127 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8128 	rsm->r_dupack = 0;
8129 	if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
8130 		rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
8131 		rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
8132 	}
8133 	if (rsm->r_flags & RACK_WAS_LOST) {
8134 		/*
8135 		 * We retransmitted it putting it back in flight
8136 		 * remove the lost desgination and reduce the
8137 		 * bytes considered lost.
8138 		 */
8139 		rsm->r_flags  &= ~RACK_WAS_LOST;
8140 		KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
8141 			("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
8142 		if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
8143 			rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
8144 		else
8145 			rack->r_ctl.rc_considered_lost = 0;
8146 	}
8147 	idx = rsm->r_rtr_cnt - 1;
8148 	rsm->r_tim_lastsent[idx] = ts;
8149 	/*
8150 	 * Here we don't add in the len of send, since its already
8151 	 * in snduna <->snd_max.
8152 	 */
8153 	rsm->r_fas = ctf_flight_size(rack->rc_tp,
8154 				     rack->r_ctl.rc_sacked);
8155 	if (rsm->r_flags & RACK_ACKED) {
8156 		/* Problably MTU discovery messing with us */
8157 		rsm->r_flags &= ~RACK_ACKED;
8158 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8159 	}
8160 	if (rsm->r_in_tmap) {
8161 		TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8162 		rsm->r_in_tmap = 0;
8163 	}
8164 	/* Lets make sure it really is in or not the GP window */
8165 	rack_mark_in_gp_win(tp, rsm);
8166 	TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8167 	rsm->r_in_tmap = 1;
8168 	rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz);
8169 	/* Take off the must retransmit flag, if its on */
8170 	if (rsm->r_flags & RACK_MUST_RXT) {
8171 		if (rack->r_must_retran)
8172 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
8173 		if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
8174 			/*
8175 			 * We have retransmitted all we need. Clear
8176 			 * any must retransmit flags.
8177 			 */
8178 			rack->r_must_retran = 0;
8179 			rack->r_ctl.rc_out_at_rto = 0;
8180 		}
8181 		rsm->r_flags &= ~RACK_MUST_RXT;
8182 	}
8183 	/* Remove any collapsed flag */
8184 	rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8185 	if (rsm->r_flags & RACK_SACK_PASSED) {
8186 		/* We have retransmitted due to the SACK pass */
8187 		rsm->r_flags &= ~RACK_SACK_PASSED;
8188 		rsm->r_flags |= RACK_WAS_SACKPASS;
8189 	}
8190 }
8191 
8192 static uint32_t
rack_update_entry(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint64_t ts,int32_t * lenp,uint32_t add_flag,int segsiz)8193 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
8194     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint32_t add_flag, int segsiz)
8195 {
8196 	/*
8197 	 * We (re-)transmitted starting at rsm->r_start for some length
8198 	 * (possibly less than r_end.
8199 	 */
8200 	struct rack_sendmap *nrsm;
8201 	int insret __diagused;
8202 	uint32_t c_end;
8203 	int32_t len;
8204 
8205 	len = *lenp;
8206 	c_end = rsm->r_start + len;
8207 	if (SEQ_GEQ(c_end, rsm->r_end)) {
8208 		/*
8209 		 * We retransmitted the whole piece or more than the whole
8210 		 * slopping into the next rsm.
8211 		 */
8212 		rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8213 		if (c_end == rsm->r_end) {
8214 			*lenp = 0;
8215 			return (0);
8216 		} else {
8217 			int32_t act_len;
8218 
8219 			/* Hangs over the end return whats left */
8220 			act_len = rsm->r_end - rsm->r_start;
8221 			*lenp = (len - act_len);
8222 			return (rsm->r_end);
8223 		}
8224 		/* We don't get out of this block. */
8225 	}
8226 	/*
8227 	 * Here we retransmitted less than the whole thing which means we
8228 	 * have to split this into what was transmitted and what was not.
8229 	 */
8230 	nrsm = rack_alloc_full_limit(rack);
8231 	if (nrsm == NULL) {
8232 		/*
8233 		 * We can't get memory, so lets not proceed.
8234 		 */
8235 		*lenp = 0;
8236 		return (0);
8237 	}
8238 	/*
8239 	 * So here we are going to take the original rsm and make it what we
8240 	 * retransmitted. nrsm will be the tail portion we did not
8241 	 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
8242 	 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
8243 	 * 1, 6 and the new piece will be 6, 11.
8244 	 */
8245 	rack_clone_rsm(rack, nrsm, rsm, c_end);
8246 	nrsm->r_dupack = 0;
8247 	rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8248 #ifndef INVARIANTS
8249 	(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8250 #else
8251 	if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8252 		panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8253 		      nrsm, insret, rack, rsm);
8254 	}
8255 #endif
8256 	if (rsm->r_in_tmap) {
8257 		TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8258 		nrsm->r_in_tmap = 1;
8259 	}
8260 	rsm->r_flags &= (~RACK_HAS_FIN);
8261 	rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8262 	/* Log a split of rsm into rsm and nrsm */
8263 	rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8264 	*lenp = 0;
8265 	return (0);
8266 }
8267 
8268 static void
rack_log_output(struct tcpcb * tp,struct tcpopt * to,int32_t len,uint32_t seq_out,uint16_t th_flags,int32_t err,uint64_t cts,struct rack_sendmap * hintrsm,uint32_t add_flag,struct mbuf * s_mb,uint32_t s_moff,int hw_tls,int segsiz)8269 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
8270 		uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
8271 		struct rack_sendmap *hintrsm, uint32_t add_flag, struct mbuf *s_mb,
8272 		uint32_t s_moff, int hw_tls, int segsiz)
8273 {
8274 	struct tcp_rack *rack;
8275 	struct rack_sendmap *rsm, *nrsm;
8276 	int insret __diagused;
8277 
8278 	register uint32_t snd_max, snd_una;
8279 
8280 	/*
8281 	 * Add to the RACK log of packets in flight or retransmitted. If
8282 	 * there is a TS option we will use the TS echoed, if not we will
8283 	 * grab a TS.
8284 	 *
8285 	 * Retransmissions will increment the count and move the ts to its
8286 	 * proper place. Note that if options do not include TS's then we
8287 	 * won't be able to effectively use the ACK for an RTT on a retran.
8288 	 *
8289 	 * Notes about r_start and r_end. Lets consider a send starting at
8290 	 * sequence 1 for 10 bytes. In such an example the r_start would be
8291 	 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
8292 	 * This means that r_end is actually the first sequence for the next
8293 	 * slot (11).
8294 	 *
8295 	 */
8296 	/*
8297 	 * If err is set what do we do XXXrrs? should we not add the thing?
8298 	 * -- i.e. return if err != 0 or should we pretend we sent it? --
8299 	 * i.e. proceed with add ** do this for now.
8300 	 */
8301 	INP_WLOCK_ASSERT(tptoinpcb(tp));
8302 	if (err)
8303 		/*
8304 		 * We don't log errors -- we could but snd_max does not
8305 		 * advance in this case either.
8306 		 */
8307 		return;
8308 
8309 	if (th_flags & TH_RST) {
8310 		/*
8311 		 * We don't log resets and we return immediately from
8312 		 * sending
8313 		 */
8314 		return;
8315 	}
8316 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8317 	snd_una = tp->snd_una;
8318 	snd_max = tp->snd_max;
8319 	if (th_flags & (TH_SYN | TH_FIN)) {
8320 		/*
8321 		 * The call to rack_log_output is made before bumping
8322 		 * snd_max. This means we can record one extra byte on a SYN
8323 		 * or FIN if seq_out is adding more on and a FIN is present
8324 		 * (and we are not resending).
8325 		 */
8326 		if ((th_flags & TH_SYN) && (seq_out == tp->iss))
8327 			len++;
8328 		if (th_flags & TH_FIN)
8329 			len++;
8330 	}
8331 	if (SEQ_LEQ((seq_out + len), snd_una)) {
8332 		/* Are sending an old segment to induce an ack (keep-alive)? */
8333 		return;
8334 	}
8335 	if (SEQ_LT(seq_out, snd_una)) {
8336 		/* huh? should we panic? */
8337 		uint32_t end;
8338 
8339 		end = seq_out + len;
8340 		seq_out = snd_una;
8341 		if (SEQ_GEQ(end, seq_out))
8342 			len = end - seq_out;
8343 		else
8344 			len = 0;
8345 	}
8346 	if (len == 0) {
8347 		/* We don't log zero window probes */
8348 		return;
8349 	}
8350 	if (IN_FASTRECOVERY(tp->t_flags)) {
8351 		rack->r_ctl.rc_prr_out += len;
8352 	}
8353 	/* First question is it a retransmission or new? */
8354 	if (seq_out == snd_max) {
8355 		/* Its new */
8356 		rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts);
8357 again:
8358 		rsm = rack_alloc(rack);
8359 		if (rsm == NULL) {
8360 			/*
8361 			 * Hmm out of memory and the tcb got destroyed while
8362 			 * we tried to wait.
8363 			 */
8364 			return;
8365 		}
8366 		if (th_flags & TH_FIN) {
8367 			rsm->r_flags = RACK_HAS_FIN|add_flag;
8368 		} else {
8369 			rsm->r_flags = add_flag;
8370 		}
8371 		if (hw_tls)
8372 			rsm->r_hw_tls = 1;
8373 		rsm->r_tim_lastsent[0] = cts;
8374 		rsm->r_rtr_cnt = 1;
8375  		rsm->r_act_rxt_cnt = 0;
8376 		rsm->r_rtr_bytes = 0;
8377 		if (th_flags & TH_SYN) {
8378 			/* The data space is one beyond snd_una */
8379 			rsm->r_flags |= RACK_HAS_SYN;
8380 		}
8381 		rsm->r_start = seq_out;
8382 		rsm->r_end = rsm->r_start + len;
8383 		rack_mark_in_gp_win(tp, rsm);
8384 		rsm->r_dupack = 0;
8385 		/*
8386 		 * save off the mbuf location that
8387 		 * sndmbuf_noadv returned (which is
8388 		 * where we started copying from)..
8389 		 */
8390 		rsm->m = s_mb;
8391 		rsm->soff = s_moff;
8392 		/*
8393 		 * Here we do add in the len of send, since its not yet
8394 		 * reflected in in snduna <->snd_max
8395 		 */
8396 		rsm->r_fas = (ctf_flight_size(rack->rc_tp,
8397 					      rack->r_ctl.rc_sacked) +
8398 			      (rsm->r_end - rsm->r_start));
8399 		if ((rack->rc_initial_ss_comp == 0) &&
8400 		    (rack->r_ctl.ss_hi_fs < rsm->r_fas)) {
8401 			   rack->r_ctl.ss_hi_fs = rsm->r_fas;
8402 		}
8403 		/* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
8404 		if (rsm->m) {
8405 			if (rsm->m->m_len <= rsm->soff) {
8406 				/*
8407 				 * XXXrrs Question, will this happen?
8408 				 *
8409 				 * If sbsndptr is set at the correct place
8410 				 * then s_moff should always be somewhere
8411 				 * within rsm->m. But if the sbsndptr was
8412 				 * off then that won't be true. If it occurs
8413 				 * we need to walkout to the correct location.
8414 				 */
8415 				struct mbuf *lm;
8416 
8417 				lm = rsm->m;
8418 				while (lm->m_len <= rsm->soff) {
8419 					rsm->soff -= lm->m_len;
8420 					lm = lm->m_next;
8421 					KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
8422 							     __func__, rack, s_moff, s_mb, rsm->soff));
8423 				}
8424 				rsm->m = lm;
8425 			}
8426 			rsm->orig_m_len = rsm->m->m_len;
8427 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
8428 		} else {
8429 			rsm->orig_m_len = 0;
8430 			rsm->orig_t_space = 0;
8431 		}
8432 		rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz);
8433 		rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8434 		/* Log a new rsm */
8435 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
8436 #ifndef INVARIANTS
8437 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
8438 #else
8439 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
8440 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8441 			      nrsm, insret, rack, rsm);
8442 		}
8443 #endif
8444 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8445 		rsm->r_in_tmap = 1;
8446 		if (rsm->r_flags & RACK_IS_PCM) {
8447 			rack->r_ctl.pcm_i.send_time = cts;
8448 			rack->r_ctl.pcm_i.eseq = rsm->r_end;
8449 			/* First time through we set the start too */
8450 			if (rack->pcm_in_progress == 0)
8451 				rack->r_ctl.pcm_i.sseq = rsm->r_start;
8452 		}
8453 		/*
8454 		 * Special case detection, is there just a single
8455 		 * packet outstanding when we are not in recovery?
8456 		 *
8457 		 * If this is true mark it so.
8458 		 */
8459 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
8460 		    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
8461 			struct rack_sendmap *prsm;
8462 
8463 			prsm = tqhash_prev(rack->r_ctl.tqh, rsm);
8464 			if (prsm)
8465 				prsm->r_one_out_nr = 1;
8466 		}
8467 		return;
8468 	}
8469 	/*
8470 	 * If we reach here its a retransmission and we need to find it.
8471 	 */
8472 more:
8473 	if (hintrsm && (hintrsm->r_start == seq_out)) {
8474 		rsm = hintrsm;
8475 		hintrsm = NULL;
8476 	} else {
8477 		/* No hints sorry */
8478 		rsm = NULL;
8479 	}
8480 	if ((rsm) && (rsm->r_start == seq_out)) {
8481 		seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8482 		if (len == 0) {
8483 			return;
8484 		} else {
8485 			goto more;
8486 		}
8487 	}
8488 	/* Ok it was not the last pointer go through it the hard way. */
8489 refind:
8490 	rsm = tqhash_find(rack->r_ctl.tqh, seq_out);
8491 	if (rsm) {
8492 		if (rsm->r_start == seq_out) {
8493 			seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8494 			if (len == 0) {
8495 				return;
8496 			} else {
8497 				goto refind;
8498 			}
8499 		}
8500 		if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
8501 			/* Transmitted within this piece */
8502 			/*
8503 			 * Ok we must split off the front and then let the
8504 			 * update do the rest
8505 			 */
8506 			nrsm = rack_alloc_full_limit(rack);
8507 			if (nrsm == NULL) {
8508 				rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz);
8509 				return;
8510 			}
8511 			/*
8512 			 * copy rsm to nrsm and then trim the front of rsm
8513 			 * to not include this part.
8514 			 */
8515 			rack_clone_rsm(rack, nrsm, rsm, seq_out);
8516 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8517 #ifndef INVARIANTS
8518 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8519 #else
8520 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8521 				panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
8522 				      nrsm, insret, rack, rsm);
8523 			}
8524 #endif
8525 			if (rsm->r_in_tmap) {
8526 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8527 				nrsm->r_in_tmap = 1;
8528 			}
8529 			rsm->r_flags &= (~RACK_HAS_FIN);
8530 			seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz);
8531 			if (len == 0) {
8532 				return;
8533 			} else if (len > 0)
8534 				goto refind;
8535 		}
8536 	}
8537 	/*
8538 	 * Hmm not found in map did they retransmit both old and on into the
8539 	 * new?
8540 	 */
8541 	if (seq_out == tp->snd_max) {
8542 		goto again;
8543 	} else if (SEQ_LT(seq_out, tp->snd_max)) {
8544 #ifdef INVARIANTS
8545 		printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
8546 		       seq_out, len, tp->snd_una, tp->snd_max);
8547 		printf("Starting Dump of all rack entries\n");
8548 		TQHASH_FOREACH(rsm, rack->r_ctl.tqh)  {
8549 			printf("rsm:%p start:%u end:%u\n",
8550 			       rsm, rsm->r_start, rsm->r_end);
8551 		}
8552 		printf("Dump complete\n");
8553 		panic("seq_out not found rack:%p tp:%p",
8554 		      rack, tp);
8555 #endif
8556 	} else {
8557 #ifdef INVARIANTS
8558 		/*
8559 		 * Hmm beyond sndmax? (only if we are using the new rtt-pack
8560 		 * flag)
8561 		 */
8562 		panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
8563 		      seq_out, len, tp->snd_max, tp);
8564 #endif
8565 	}
8566 }
8567 
8568 /*
8569  * Record one of the RTT updates from an ack into
8570  * our sample structure.
8571  */
8572 
8573 static void
tcp_rack_xmit_timer(struct tcp_rack * rack,int32_t rtt,uint32_t len,uint32_t us_rtt,int confidence,struct rack_sendmap * rsm,uint16_t rtrcnt)8574 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
8575 		    int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
8576 {
8577 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8578 	    (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
8579 		rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
8580 	}
8581 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8582 	    (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
8583 		rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
8584 	}
8585 	if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
8586 	    if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
8587 		rack->r_ctl.rc_gp_lowrtt = us_rtt;
8588 	    if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
8589 		    rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
8590 	}
8591 	if ((confidence == 1) &&
8592 	    ((rsm == NULL) ||
8593 	     (rsm->r_just_ret) ||
8594 	     (rsm->r_one_out_nr &&
8595 	      len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
8596 		/*
8597 		 * If the rsm had a just return
8598 		 * hit it then we can't trust the
8599 		 * rtt measurement for buffer deterimination
8600 		 * Note that a confidence of 2, indicates
8601 		 * SACK'd which overrides the r_just_ret or
8602 		 * the r_one_out_nr. If it was a CUM-ACK and
8603 		 * we had only two outstanding, but get an
8604 		 * ack for only 1. Then that also lowers our
8605 		 * confidence.
8606 		 */
8607 		confidence = 0;
8608 	}
8609 	if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8610 	    (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
8611 		if (rack->r_ctl.rack_rs.confidence == 0) {
8612 			/*
8613 			 * We take anything with no current confidence
8614 			 * saved.
8615 			 */
8616 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8617 			rack->r_ctl.rack_rs.confidence = confidence;
8618 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8619 		} else if (confidence != 0) {
8620 			/*
8621 			 * Once we have a confident number,
8622 			 * we can update it with a smaller
8623 			 * value since this confident number
8624 			 * may include the DSACK time until
8625 			 * the next segment (the second one) arrived.
8626 			 */
8627 			rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8628 			rack->r_ctl.rack_rs.confidence = confidence;
8629 			rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8630 		}
8631 	}
8632 	rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
8633 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
8634 	rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
8635 	rack->r_ctl.rack_rs.rs_rtt_cnt++;
8636 }
8637 
8638 /*
8639  * Collect new round-trip time estimate
8640  * and update averages and current timeout.
8641  */
8642 static void
tcp_rack_xmit_timer_commit(struct tcp_rack * rack,struct tcpcb * tp)8643 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
8644 {
8645 	int32_t delta;
8646 	int32_t rtt;
8647 
8648 	if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
8649 		/* No valid sample */
8650 		return;
8651 	if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
8652 		/* We are to use the lowest RTT seen in a single ack */
8653 		rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
8654 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
8655 		/* We are to use the highest RTT seen in a single ack */
8656 		rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
8657 	} else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
8658 		/* We are to use the average RTT seen in a single ack */
8659 		rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
8660 				(uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
8661 	} else {
8662 #ifdef INVARIANTS
8663 		panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
8664 #endif
8665 		return;
8666 	}
8667 	if (rtt == 0)
8668 		rtt = 1;
8669 	if (rack->rc_gp_rtt_set == 0) {
8670 		/*
8671 		 * With no RTT we have to accept
8672 		 * even one we are not confident of.
8673 		 */
8674 		rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
8675 		rack->rc_gp_rtt_set = 1;
8676 	} else if (rack->r_ctl.rack_rs.confidence) {
8677 		/* update the running gp srtt */
8678 		rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
8679 		rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
8680 	}
8681 	if (rack->r_ctl.rack_rs.confidence) {
8682 		/*
8683 		 * record the low and high for highly buffered path computation,
8684 		 * we only do this if we are confident (not a retransmission).
8685 		 */
8686 		if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
8687 			rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8688 		}
8689 		if (rack->rc_highly_buffered == 0) {
8690 			/*
8691 			 * Currently once we declare a path has
8692 			 * highly buffered there is no going
8693 			 * back, which may be a problem...
8694 			 */
8695 			if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
8696 				rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
8697 						     rack->r_ctl.rc_highest_us_rtt,
8698 						     rack->r_ctl.rc_lowest_us_rtt,
8699 						     RACK_RTTS_SEEHBP);
8700 				rack->rc_highly_buffered = 1;
8701 			}
8702 		}
8703 	}
8704 	if ((rack->r_ctl.rack_rs.confidence) ||
8705 	    (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
8706 		/*
8707 		 * If we are highly confident of it <or> it was
8708 		 * never retransmitted we accept it as the last us_rtt.
8709 		 */
8710 		rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8711 		/* The lowest rtt can be set if its was not retransmited */
8712 		if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
8713 			rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8714 			if (rack->r_ctl.rc_lowest_us_rtt == 0)
8715 				rack->r_ctl.rc_lowest_us_rtt = 1;
8716 		}
8717 	}
8718 	rack = (struct tcp_rack *)tp->t_fb_ptr;
8719 	if (tp->t_srtt != 0) {
8720 		/*
8721 		 * We keep a simple srtt in microseconds, like our rtt
8722 		 * measurement. We don't need to do any tricks with shifting
8723 		 * etc. Instead we just add in 1/8th of the new measurement
8724 		 * and subtract out 1/8 of the old srtt. We do the same with
8725 		 * the variance after finding the absolute value of the
8726 		 * difference between this sample and the current srtt.
8727 		 */
8728 		delta = tp->t_srtt - rtt;
8729 		/* Take off 1/8th of the current sRTT */
8730 		tp->t_srtt -= (tp->t_srtt >> 3);
8731 		/* Add in 1/8th of the new RTT just measured */
8732 		tp->t_srtt += (rtt >> 3);
8733 		if (tp->t_srtt <= 0)
8734 			tp->t_srtt = 1;
8735 		/* Now lets make the absolute value of the variance */
8736 		if (delta < 0)
8737 			delta = -delta;
8738 		/* Subtract out 1/8th */
8739 		tp->t_rttvar -= (tp->t_rttvar >> 3);
8740 		/* Add in 1/8th of the new variance we just saw */
8741 		tp->t_rttvar += (delta >> 3);
8742 		if (tp->t_rttvar <= 0)
8743 			tp->t_rttvar = 1;
8744 	} else {
8745 		/*
8746 		 * No rtt measurement yet - use the unsmoothed rtt. Set the
8747 		 * variance to half the rtt (so our first retransmit happens
8748 		 * at 3*rtt).
8749 		 */
8750 		tp->t_srtt = rtt;
8751 		tp->t_rttvar = rtt >> 1;
8752 	}
8753 	rack->rc_srtt_measure_made = 1;
8754 	KMOD_TCPSTAT_INC(tcps_rttupdated);
8755 	if (tp->t_rttupdated < UCHAR_MAX)
8756 		tp->t_rttupdated++;
8757 #ifdef STATS
8758 	if (rack_stats_gets_ms_rtt == 0) {
8759 		/* Send in the microsecond rtt used for rxt timeout purposes */
8760 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
8761 	} else if (rack_stats_gets_ms_rtt == 1) {
8762 		/* Send in the millisecond rtt used for rxt timeout purposes */
8763 		int32_t ms_rtt;
8764 
8765 		/* Round up */
8766 		ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8767 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8768 	} else if (rack_stats_gets_ms_rtt == 2) {
8769 		/* Send in the millisecond rtt has close to the path RTT as we can get  */
8770 		int32_t ms_rtt;
8771 
8772 		/* Round up */
8773 		ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8774 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8775 	}  else {
8776 		/* Send in the microsecond rtt has close to the path RTT as we can get  */
8777 		stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8778 	}
8779 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8780 #endif
8781 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
8782 	/*
8783 	 * the retransmit should happen at rtt + 4 * rttvar. Because of the
8784 	 * way we do the smoothing, srtt and rttvar will each average +1/2
8785 	 * tick of bias.  When we compute the retransmit timer, we want 1/2
8786 	 * tick of rounding and 1 extra tick because of +-1/2 tick
8787 	 * uncertainty in the firing of the timer.  The bias will give us
8788 	 * exactly the 1.5 tick we need.  But, because the bias is
8789 	 * statistical, we have to test that we don't drop below the minimum
8790 	 * feasible timer (which is 2 ticks).
8791 	 */
8792 	tp->t_rxtshift = 0;
8793 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8794 		      max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
8795 	rack_log_rtt_sample(rack, rtt);
8796 	tp->t_softerror = 0;
8797 }
8798 
8799 
8800 static void
rack_apply_updated_usrtt(struct tcp_rack * rack,uint32_t us_rtt,uint32_t us_cts)8801 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
8802 {
8803 	/*
8804 	 * Apply to filter the inbound us-rtt at us_cts.
8805 	 */
8806 	uint32_t old_rtt;
8807 
8808 	old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
8809 	apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
8810 			       us_rtt, us_cts);
8811 	if (old_rtt > us_rtt) {
8812 		/* We just hit a new lower rtt time */
8813 		rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
8814 				     __LINE__, RACK_RTTS_NEWRTT);
8815 		/*
8816 		 * Only count it if its lower than what we saw within our
8817 		 * calculated range.
8818 		 */
8819 		if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
8820 			if (rack_probertt_lower_within &&
8821 			    rack->rc_gp_dyn_mul &&
8822 			    (rack->use_fixed_rate == 0) &&
8823 			    (rack->rc_always_pace)) {
8824 				/*
8825 				 * We are seeing a new lower rtt very close
8826 				 * to the time that we would have entered probe-rtt.
8827 				 * This is probably due to the fact that a peer flow
8828 				 * has entered probe-rtt. Lets go in now too.
8829 				 */
8830 				uint32_t val;
8831 
8832 				val = rack_probertt_lower_within * rack_time_between_probertt;
8833 				val /= 100;
8834 				if ((rack->in_probe_rtt == 0)  &&
8835 				    (rack->rc_skip_timely == 0) &&
8836 				    ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val)))	{
8837 					rack_enter_probertt(rack, us_cts);
8838 				}
8839 			}
8840 			rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
8841 		}
8842 	}
8843 }
8844 
8845 static int
rack_update_rtt(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,struct tcpopt * to,uint32_t cts,int32_t ack_type,tcp_seq th_ack)8846 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
8847     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
8848 {
8849 	uint32_t us_rtt;
8850 	int32_t i, all;
8851 	uint32_t t, len_acked;
8852 
8853 	if ((rsm->r_flags & RACK_ACKED) ||
8854 	    (rsm->r_flags & RACK_WAS_ACKED))
8855 		/* Already done */
8856 		return (0);
8857 	if (rsm->r_no_rtt_allowed) {
8858 		/* Not allowed */
8859 		return (0);
8860 	}
8861 	if (ack_type == CUM_ACKED) {
8862 		if (SEQ_GT(th_ack, rsm->r_end)) {
8863 			len_acked = rsm->r_end - rsm->r_start;
8864 			all = 1;
8865 		} else {
8866 			len_acked = th_ack - rsm->r_start;
8867 			all = 0;
8868 		}
8869 	} else {
8870 		len_acked = rsm->r_end - rsm->r_start;
8871 		all = 0;
8872 	}
8873 	if (rsm->r_rtr_cnt == 1) {
8874 
8875 		t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8876 		if ((int)t <= 0)
8877 			t = 1;
8878 		if (!tp->t_rttlow || tp->t_rttlow > t)
8879 			tp->t_rttlow = t;
8880 		if (!rack->r_ctl.rc_rack_min_rtt ||
8881 		    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8882 			rack->r_ctl.rc_rack_min_rtt = t;
8883 			if (rack->r_ctl.rc_rack_min_rtt == 0) {
8884 				rack->r_ctl.rc_rack_min_rtt = 1;
8885 			}
8886 		}
8887 		if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
8888 			us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8889 		else
8890 			us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8891 		if (us_rtt == 0)
8892 			us_rtt = 1;
8893 		if (CC_ALGO(tp)->rttsample != NULL) {
8894 			/* Kick the RTT to the CC */
8895 			CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8896 		}
8897 		rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8898 		if (ack_type == SACKED) {
8899 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8900 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
8901 		} else {
8902 			/*
8903 			 * We need to setup what our confidence
8904 			 * is in this ack.
8905 			 *
8906 			 * If the rsm was app limited and it is
8907 			 * less than a mss in length (the end
8908 			 * of the send) then we have a gap. If we
8909 			 * were app limited but say we were sending
8910 			 * multiple MSS's then we are more confident
8911 			 * int it.
8912 			 *
8913 			 * When we are not app-limited then we see if
8914 			 * the rsm is being included in the current
8915 			 * measurement, we tell this by the app_limited_needs_set
8916 			 * flag.
8917 			 *
8918 			 * Note that being cwnd blocked is not applimited
8919 			 * as well as the pacing delay between packets which
8920 			 * are sending only 1 or 2 MSS's also will show up
8921 			 * in the RTT. We probably need to examine this algorithm
8922 			 * a bit more and enhance it to account for the delay
8923 			 * between rsm's. We could do that by saving off the
8924 			 * pacing delay of each rsm (in an rsm) and then
8925 			 * factoring that in somehow though for now I am
8926 			 * not sure how :)
8927 			 */
8928 			int calc_conf = 0;
8929 
8930 			if (rsm->r_flags & RACK_APP_LIMITED) {
8931 				if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8932 					calc_conf = 0;
8933 				else
8934 					calc_conf = 1;
8935 			} else if (rack->app_limited_needs_set == 0) {
8936 				calc_conf = 1;
8937 			} else {
8938 				calc_conf = 0;
8939 			}
8940 			rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
8941 			tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
8942 					    calc_conf, rsm, rsm->r_rtr_cnt);
8943 		}
8944 		if ((rsm->r_flags & RACK_TLP) &&
8945 		    (!IN_FASTRECOVERY(tp->t_flags))) {
8946 			/* Segment was a TLP and our retrans matched */
8947 			if (rack->r_ctl.rc_tlp_cwnd_reduce) {
8948 				rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
8949 			}
8950 		}
8951 		if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
8952 		    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
8953 			    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
8954 			/* New more recent rack_tmit_time */
8955 			rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8956 			if (rack->r_ctl.rc_rack_tmit_time == 0)
8957 				rack->r_ctl.rc_rack_tmit_time = 1;
8958 			rack->rc_rack_rtt = t;
8959 		}
8960 		return (1);
8961 	}
8962 	/*
8963 	 * We clear the soft/rxtshift since we got an ack.
8964 	 * There is no assurance we will call the commit() function
8965 	 * so we need to clear these to avoid incorrect handling.
8966 	 */
8967 	tp->t_rxtshift = 0;
8968 	RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8969 		      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
8970 	tp->t_softerror = 0;
8971 	if (to && (to->to_flags & TOF_TS) &&
8972 	    (ack_type == CUM_ACKED) &&
8973 	    (to->to_tsecr) &&
8974 	    ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8975 		/*
8976 		 * Now which timestamp does it match? In this block the ACK
8977 		 * must be coming from a previous transmission.
8978 		 */
8979 		for (i = 0; i < rsm->r_rtr_cnt; i++) {
8980 			if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
8981 				t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8982 				if ((int)t <= 0)
8983 					t = 1;
8984 				if (CC_ALGO(tp)->rttsample != NULL) {
8985 					/*
8986 					 * Kick the RTT to the CC, here
8987 					 * we lie a bit in that we know the
8988 					 * retransmission is correct even though
8989 					 * we retransmitted. This is because
8990 					 * we match the timestamps.
8991 					 */
8992 					if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
8993 						us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
8994 					else
8995 						us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
8996 					CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8997 				}
8998 				if ((i + 1) < rsm->r_rtr_cnt) {
8999 					/*
9000 					 * The peer ack'd from our previous
9001 					 * transmission. We have a spurious
9002 					 * retransmission and thus we dont
9003 					 * want to update our rack_rtt.
9004 					 *
9005 					 * Hmm should there be a CC revert here?
9006 					 *
9007 					 */
9008 					return (0);
9009 				}
9010 				if (!tp->t_rttlow || tp->t_rttlow > t)
9011 					tp->t_rttlow = t;
9012 				if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9013 					rack->r_ctl.rc_rack_min_rtt = t;
9014 					if (rack->r_ctl.rc_rack_min_rtt == 0) {
9015 						rack->r_ctl.rc_rack_min_rtt = 1;
9016 					}
9017 				}
9018 				if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9019 				    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9020 					    (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9021 					/* New more recent rack_tmit_time */
9022 					rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9023 					if (rack->r_ctl.rc_rack_tmit_time == 0)
9024 						rack->r_ctl.rc_rack_tmit_time = 1;
9025 					rack->rc_rack_rtt = t;
9026 				}
9027 				rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
9028 				tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
9029 						    rsm->r_rtr_cnt);
9030 				return (1);
9031 			}
9032 		}
9033 		/* If we are logging log out the sendmap */
9034 		if (tcp_bblogging_on(rack->rc_tp)) {
9035 			for (i = 0; i < rsm->r_rtr_cnt; i++) {
9036 				rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr);
9037 			}
9038 		}
9039 		goto ts_not_found;
9040 	} else {
9041 		/*
9042 		 * Ok its a SACK block that we retransmitted. or a windows
9043 		 * machine without timestamps. We can tell nothing from the
9044 		 * time-stamp since its not there or the time the peer last
9045 		 * received a segment that moved forward its cum-ack point.
9046 		 */
9047 ts_not_found:
9048 		i = rsm->r_rtr_cnt - 1;
9049 		t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9050 		if ((int)t <= 0)
9051 			t = 1;
9052 		if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9053 			/*
9054 			 * We retransmitted and the ack came back in less
9055 			 * than the smallest rtt we have observed. We most
9056 			 * likely did an improper retransmit as outlined in
9057 			 * 6.2 Step 2 point 2 in the rack-draft so we
9058 			 * don't want to update our rack_rtt. We in
9059 			 * theory (in future) might want to think about reverting our
9060 			 * cwnd state but we won't for now.
9061 			 */
9062 			return (0);
9063 		} else if (rack->r_ctl.rc_rack_min_rtt) {
9064 			/*
9065 			 * We retransmitted it and the retransmit did the
9066 			 * job.
9067 			 */
9068 			if (!rack->r_ctl.rc_rack_min_rtt ||
9069 			    SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9070 				rack->r_ctl.rc_rack_min_rtt = t;
9071 				if (rack->r_ctl.rc_rack_min_rtt == 0) {
9072 					rack->r_ctl.rc_rack_min_rtt = 1;
9073 				}
9074 			}
9075 			if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9076 			    (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9077 				    (uint32_t)rsm->r_tim_lastsent[i]))) {
9078 				/* New more recent rack_tmit_time */
9079 				rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
9080 				if (rack->r_ctl.rc_rack_tmit_time == 0)
9081 					rack->r_ctl.rc_rack_tmit_time = 1;
9082 				rack->rc_rack_rtt = t;
9083 			}
9084 			return (1);
9085 		}
9086 	}
9087 	return (0);
9088 }
9089 
9090 /*
9091  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
9092  */
9093 static void
rack_log_sack_passed(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t cts)9094 rack_log_sack_passed(struct tcpcb *tp,
9095     struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts)
9096 {
9097 	struct rack_sendmap *nrsm;
9098 	uint32_t thresh;
9099 
9100 	/* Get our rxt threshold for lost consideration */
9101 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0);
9102 	/* Now start looking at rsm's */
9103 	nrsm = rsm;
9104 	TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
9105 	    rack_head, r_tnext) {
9106 		if (nrsm == rsm) {
9107 			/* Skip original segment he is acked */
9108 			continue;
9109 		}
9110 		if (nrsm->r_flags & RACK_ACKED) {
9111 			/*
9112 			 * Skip ack'd segments, though we
9113 			 * should not see these, since tmap
9114 			 * should not have ack'd segments.
9115 			 */
9116 			continue;
9117 		}
9118 		if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
9119 			/*
9120 			 * If the peer dropped the rwnd on
9121 			 * these then we don't worry about them.
9122 			 */
9123 			continue;
9124 		}
9125 		/* Check lost state */
9126 		if ((nrsm->r_flags & RACK_WAS_LOST) == 0) {
9127 			uint32_t exp;
9128 
9129 			exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh;
9130 			if (TSTMP_LT(exp, cts) || (exp == cts)) {
9131 				/* We consider it lost */
9132 				nrsm->r_flags |= RACK_WAS_LOST;
9133 				rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start;
9134 			}
9135 		}
9136 		if (nrsm->r_flags & RACK_SACK_PASSED) {
9137 			/*
9138 			 * We found one that is already marked
9139 			 * passed, we have been here before and
9140 			 * so all others below this are marked.
9141 			 */
9142 			break;
9143 		}
9144 		nrsm->r_flags |= RACK_SACK_PASSED;
9145 		nrsm->r_flags &= ~RACK_WAS_SACKPASS;
9146 	}
9147 }
9148 
9149 static void
rack_need_set_test(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,tcp_seq th_ack,int line,int use_which)9150 rack_need_set_test(struct tcpcb *tp,
9151 		   struct tcp_rack *rack,
9152 		   struct rack_sendmap *rsm,
9153 		   tcp_seq th_ack,
9154 		   int line,
9155 		   int use_which)
9156 {
9157 	struct rack_sendmap *s_rsm;
9158 
9159 	if ((tp->t_flags & TF_GPUTINPROG) &&
9160 	    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9161 		/*
9162 		 * We were app limited, and this ack
9163 		 * butts up or goes beyond the point where we want
9164 		 * to start our next measurement. We need
9165 		 * to record the new gput_ts as here and
9166 		 * possibly update the start sequence.
9167 		 */
9168 		uint32_t seq, ts;
9169 
9170 		if (rsm->r_rtr_cnt > 1) {
9171 			/*
9172 			 * This is a retransmit, can we
9173 			 * really make any assessment at this
9174 			 * point?  We are not really sure of
9175 			 * the timestamp, is it this or the
9176 			 * previous transmission?
9177 			 *
9178 			 * Lets wait for something better that
9179 			 * is not retransmitted.
9180 			 */
9181 			return;
9182 		}
9183 		seq = tp->gput_seq;
9184 		ts = tp->gput_ts;
9185 		rack->app_limited_needs_set = 0;
9186 		tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
9187 		/* Do we start at a new end? */
9188 		if ((use_which == RACK_USE_BEG) &&
9189 		    SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
9190 			/*
9191 			 * When we get an ACK that just eats
9192 			 * up some of the rsm, we set RACK_USE_BEG
9193 			 * since whats at r_start (i.e. th_ack)
9194 			 * is left unacked and thats where the
9195 			 * measurement now starts.
9196 			 */
9197 			tp->gput_seq = rsm->r_start;
9198 		}
9199 		if ((use_which == RACK_USE_END) &&
9200 		    SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9201 			/*
9202 			 * We use the end when the cumack
9203 			 * is moving forward and completely
9204 			 * deleting the rsm passed so basically
9205 			 * r_end holds th_ack.
9206 			 *
9207 			 * For SACK's we also want to use the end
9208 			 * since this piece just got sacked and
9209 			 * we want to target anything after that
9210 			 * in our measurement.
9211 			 */
9212 			tp->gput_seq = rsm->r_end;
9213 		}
9214 		if (use_which == RACK_USE_END_OR_THACK) {
9215 			/*
9216 			 * special case for ack moving forward,
9217 			 * not a sack, we need to move all the
9218 			 * way up to where this ack cum-ack moves
9219 			 * to.
9220 			 */
9221 			if (SEQ_GT(th_ack, rsm->r_end))
9222 				tp->gput_seq = th_ack;
9223 			else
9224 				tp->gput_seq = rsm->r_end;
9225 		}
9226 		if (SEQ_LT(tp->gput_seq, tp->snd_max))
9227 			s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
9228 		else
9229 			s_rsm = NULL;
9230 		/*
9231 		 * Pick up the correct send time if we can the rsm passed in
9232 		 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other
9233 		 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will
9234 		 * find a different seq i.e. the next send up.
9235 		 *
9236 		 * If that has not been sent, s_rsm will be NULL and we must
9237 		 * arrange it so this function will get called again by setting
9238 		 * app_limited_needs_set.
9239 		 */
9240 		if (s_rsm)
9241 			rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0];
9242 		else {
9243 			/* If we hit here we have to have *not* sent tp->gput_seq */
9244 			rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
9245 			/* Set it up so we will go through here again */
9246 			rack->app_limited_needs_set = 1;
9247 		}
9248 		if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
9249 			/*
9250 			 * We moved beyond this guy's range, re-calculate
9251 			 * the new end point.
9252 			 */
9253 			if (rack->rc_gp_filled == 0) {
9254 				tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
9255 			} else {
9256 				tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
9257 			}
9258 		}
9259 		/*
9260 		 * We are moving the goal post, we may be able to clear the
9261 		 * measure_saw_probe_rtt flag.
9262 		 */
9263 		if ((rack->in_probe_rtt == 0) &&
9264 		    (rack->measure_saw_probe_rtt) &&
9265 		    (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
9266 			rack->measure_saw_probe_rtt = 0;
9267 		rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
9268 					   seq, tp->gput_seq,
9269 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9270 					    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9271 					   5, line, NULL, 0);
9272 		if (rack->rc_gp_filled &&
9273 		    ((tp->gput_ack - tp->gput_seq) <
9274 		     max(rc_init_window(rack), (MIN_GP_WIN *
9275 						ctf_fixed_maxseg(tp))))) {
9276 			uint32_t ideal_amount;
9277 
9278 			ideal_amount = rack_get_measure_window(tp, rack);
9279 			if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) {
9280 				/*
9281 				 * There is no sense of continuing this measurement
9282 				 * because its too small to gain us anything we
9283 				 * trust. Skip it and that way we can start a new
9284 				 * measurement quicker.
9285 				 */
9286 				tp->t_flags &= ~TF_GPUTINPROG;
9287 				rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
9288 							   0, 0,
9289 							   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9290 							    (uint64_t)rack->r_ctl.rc_gp_output_ts),
9291 							   6, __LINE__, NULL, 0);
9292 			} else {
9293 				/*
9294 				 * Reset the window further out.
9295 				 */
9296 				tp->gput_ack = tp->gput_seq + ideal_amount;
9297 			}
9298 		}
9299 		rack_tend_gp_marks(tp, rack);
9300 		rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm);
9301 	}
9302 }
9303 
9304 static inline int
is_rsm_inside_declared_tlp_block(struct tcp_rack * rack,struct rack_sendmap * rsm)9305 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
9306 {
9307 	if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
9308 		/* Behind our TLP definition or right at */
9309 		return (0);
9310 	}
9311 	if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
9312 		/* The start is beyond or right at our end of TLP definition */
9313 		return (0);
9314 	}
9315 	/* It has to be a sub-part of the original TLP recorded */
9316 	return (1);
9317 }
9318 
9319 static uint32_t
rack_proc_sack_blk(struct tcpcb * tp,struct tcp_rack * rack,struct sackblk * sack,struct tcpopt * to,struct rack_sendmap ** prsm,uint32_t cts,uint32_t segsiz)9320 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
9321 		   struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts,
9322 		   uint32_t segsiz)
9323 {
9324 	uint32_t start, end, changed = 0;
9325 	struct rack_sendmap stack_map;
9326 	struct rack_sendmap *rsm, *nrsm, *prev, *next;
9327 	int insret __diagused;
9328 	int32_t used_ref = 1;
9329 	int can_use_hookery = 0;
9330 
9331 	start = sack->start;
9332 	end = sack->end;
9333 	rsm = *prsm;
9334 
9335 do_rest_ofb:
9336 	if ((rsm == NULL) ||
9337 	    (SEQ_LT(end, rsm->r_start)) ||
9338 	    (SEQ_GEQ(start, rsm->r_end)) ||
9339 	    (SEQ_LT(start, rsm->r_start))) {
9340 		/*
9341 		 * We are not in the right spot,
9342 		 * find the correct spot in the tree.
9343 		 */
9344 		used_ref = 0;
9345 		rsm = tqhash_find(rack->r_ctl.tqh, start);
9346 	}
9347 	if (rsm == NULL) {
9348 		/* TSNH */
9349 		goto out;
9350 	}
9351 	/* Ok we have an ACK for some piece of this rsm */
9352 	if (rsm->r_start != start) {
9353 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9354 			/*
9355 			 * Before any splitting or hookery is
9356 			 * done is it a TLP of interest i.e. rxt?
9357 			 */
9358 			if ((rsm->r_flags & RACK_TLP) &&
9359 			    (rsm->r_rtr_cnt > 1)) {
9360 				/*
9361 				 * We are splitting a rxt TLP, check
9362 				 * if we need to save off the start/end
9363 				 */
9364 				if (rack->rc_last_tlp_acked_set &&
9365 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9366 					/*
9367 					 * We already turned this on since we are inside
9368 					 * the previous one was a partially sack now we
9369 					 * are getting another one (maybe all of it).
9370 					 *
9371 					 */
9372 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9373 					/*
9374 					 * Lets make sure we have all of it though.
9375 					 */
9376 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9377 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9378 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9379 								     rack->r_ctl.last_tlp_acked_end);
9380 					}
9381 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9382 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9383 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9384 								     rack->r_ctl.last_tlp_acked_end);
9385 					}
9386 				} else {
9387 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9388 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9389 					rack->rc_last_tlp_past_cumack = 0;
9390 					rack->rc_last_tlp_acked_set = 1;
9391 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9392 				}
9393 			}
9394 			/**
9395 			 * Need to split this in two pieces the before and after,
9396 			 * the before remains in the map, the after must be
9397 			 * added. In other words we have:
9398 			 * rsm        |--------------|
9399 			 * sackblk        |------->
9400 			 * rsm will become
9401 			 *     rsm    |---|
9402 			 * and nrsm will be  the sacked piece
9403 			 *     nrsm       |----------|
9404 			 *
9405 			 * But before we start down that path lets
9406 			 * see if the sack spans over on top of
9407 			 * the next guy and it is already sacked.
9408 			 *
9409 			 */
9410 			/*
9411 			 * Hookery can only be used if the two entries
9412 			 * are in the same bucket and neither one of
9413 			 * them staddle the bucket line.
9414 			 */
9415 			next = tqhash_next(rack->r_ctl.tqh, rsm);
9416 			if (next &&
9417 			    (rsm->bindex == next->bindex) &&
9418 			    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9419 			    ((next->r_flags & RACK_STRADDLE) == 0) &&
9420 			    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
9421 			    ((next->r_flags & RACK_IS_PCM) == 0) &&
9422 			    (rsm->r_flags & RACK_IN_GP_WIN) &&
9423 			    (next->r_flags & RACK_IN_GP_WIN))
9424 				can_use_hookery = 1;
9425 			else
9426 				can_use_hookery = 0;
9427 			if (next && can_use_hookery &&
9428 			    (next->r_flags & RACK_ACKED) &&
9429 			    SEQ_GEQ(end, next->r_start)) {
9430 				/**
9431 				 * So the next one is already acked, and
9432 				 * we can thus by hookery use our stack_map
9433 				 * to reflect the piece being sacked and
9434 				 * then adjust the two tree entries moving
9435 				 * the start and ends around. So we start like:
9436 				 *  rsm     |------------|             (not-acked)
9437 				 *  next                 |-----------| (acked)
9438 				 *  sackblk        |-------->
9439 				 *  We want to end like so:
9440 				 *  rsm     |------|                   (not-acked)
9441 				 *  next           |-----------------| (acked)
9442 				 *  nrsm           |-----|
9443 				 * Where nrsm is a temporary stack piece we
9444 				 * use to update all the gizmos.
9445 				 */
9446 				/* Copy up our fudge block */
9447 				nrsm = &stack_map;
9448 				memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9449 				/* Now adjust our tree blocks */
9450 				tqhash_update_end(rack->r_ctl.tqh, rsm, start);
9451 				next->r_start = start;
9452  				rsm->r_flags |= RACK_SHUFFLED;
9453 				next->r_flags |= RACK_SHUFFLED;
9454 				/* Now we must adjust back where next->m is */
9455 				rack_setup_offset_for_rsm(rack, rsm, next);
9456 				/*
9457 				 * Which timestamp do we keep? It is rather
9458 				 * important in GP measurements to have the
9459 				 * accurate end of the send window.
9460 				 *
9461 				 * We keep the largest value, which is the newest
9462 				 * send. We do this in case a segment that is
9463 				 * joined together and not part of a GP estimate
9464 				 * later gets expanded into the GP estimate.
9465 				 *
9466 				 * We prohibit the merging of unlike kinds i.e.
9467 				 * all pieces that are in the GP estimate can be
9468 				 * merged and all pieces that are not in a GP estimate
9469 				 * can be merged, but not disimilar pieces. Combine
9470 				 * this with taking the highest here and we should
9471 				 * be ok unless of course the client reneges. Then
9472 				 * all bets are off.
9473 				 */
9474 				if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] <
9475 				    nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)])
9476 					next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)];
9477 				/*
9478 				 * And we must keep the newest ack arrival time.
9479 				 */
9480 				if (next->r_ack_arrival <
9481 				    rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9482 					next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9483 
9484 
9485 				/* We don't need to adjust rsm, it did not change */
9486 				/* Clear out the dup ack count of the remainder */
9487 				rsm->r_dupack = 0;
9488 				rsm->r_just_ret = 0;
9489 				rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9490 				/* Now lets make sure our fudge block is right */
9491 				nrsm->r_start = start;
9492 				/* Now lets update all the stats and such */
9493 				rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9494 				if (rack->app_limited_needs_set)
9495 					rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9496 				changed += (nrsm->r_end - nrsm->r_start);
9497 				rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9498 				if (rsm->r_flags & RACK_WAS_LOST) {
9499 					int my_chg;
9500 
9501 					my_chg = (nrsm->r_end - nrsm->r_start);
9502 					KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9503 						("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9504 					if (my_chg <= rack->r_ctl.rc_considered_lost)
9505 						rack->r_ctl.rc_considered_lost -= my_chg;
9506 					else
9507 						rack->r_ctl.rc_considered_lost = 0;
9508 				}
9509 				if (nrsm->r_flags & RACK_SACK_PASSED) {
9510 					rack->r_ctl.rc_reorder_ts = cts;
9511 					if (rack->r_ctl.rc_reorder_ts == 0)
9512 						rack->r_ctl.rc_reorder_ts = 1;
9513 				}
9514 				/*
9515 				 * Now we want to go up from rsm (the
9516 				 * one left un-acked) to the next one
9517 				 * in the tmap. We do this so when
9518 				 * we walk backwards we include marking
9519 				 * sack-passed on rsm (The one passed in
9520 				 * is skipped since it is generally called
9521 				 * on something sacked before removing it
9522 				 * from the tmap).
9523 				 */
9524 				if (rsm->r_in_tmap) {
9525 					nrsm = TAILQ_NEXT(rsm, r_tnext);
9526 					/*
9527 					 * Now that we have the next
9528 					 * one walk backwards from there.
9529 					 */
9530 					if (nrsm && nrsm->r_in_tmap)
9531 						rack_log_sack_passed(tp, rack, nrsm, cts);
9532 				}
9533 				/* Now are we done? */
9534 				if (SEQ_LT(end, next->r_end) ||
9535 				    (end == next->r_end)) {
9536 					/* Done with block */
9537 					goto out;
9538 				}
9539 				rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
9540 				counter_u64_add(rack_sack_used_next_merge, 1);
9541 				/* Postion for the next block */
9542 				start = next->r_end;
9543 				rsm = tqhash_next(rack->r_ctl.tqh, next);
9544 				if (rsm == NULL)
9545 					goto out;
9546 			} else {
9547 				/**
9548 				 * We can't use any hookery here, so we
9549 				 * need to split the map. We enter like
9550 				 * so:
9551 				 *  rsm      |--------|
9552 				 *  sackblk       |----->
9553 				 * We will add the new block nrsm and
9554 				 * that will be the new portion, and then
9555 				 * fall through after reseting rsm. So we
9556 				 * split and look like this:
9557 				 *  rsm      |----|
9558 				 *  sackblk       |----->
9559 				 *  nrsm          |---|
9560 				 * We then fall through reseting
9561 				 * rsm to nrsm, so the next block
9562 				 * picks it up.
9563 				 */
9564 				nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9565 				if (nrsm == NULL) {
9566 					/*
9567 					 * failed XXXrrs what can we do but loose the sack
9568 					 * info?
9569 					 */
9570 					goto out;
9571 				}
9572 				counter_u64_add(rack_sack_splits, 1);
9573 				rack_clone_rsm(rack, nrsm, rsm, start);
9574 				rsm->r_just_ret = 0;
9575 #ifndef INVARIANTS
9576 				(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9577 #else
9578 				if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9579 					panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
9580 					      nrsm, insret, rack, rsm);
9581 				}
9582 #endif
9583 				if (rsm->r_in_tmap) {
9584 					TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9585 					nrsm->r_in_tmap = 1;
9586 				}
9587 				rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
9588 				rsm->r_flags &= (~RACK_HAS_FIN);
9589 				/* Position us to point to the new nrsm that starts the sack blk */
9590 				rsm = nrsm;
9591 			}
9592 		} else {
9593 			/* Already sacked this piece */
9594 			counter_u64_add(rack_sack_skipped_acked, 1);
9595 			if (end == rsm->r_end) {
9596 				/* Done with block */
9597 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9598 				goto out;
9599 			} else if (SEQ_LT(end, rsm->r_end)) {
9600 				/* A partial sack to a already sacked block */
9601 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9602 				goto out;
9603 			} else {
9604 				/*
9605 				 * The end goes beyond this guy
9606 				 * reposition the start to the
9607 				 * next block.
9608 				 */
9609 				start = rsm->r_end;
9610 				rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9611 				if (rsm == NULL)
9612 					goto out;
9613 			}
9614 		}
9615 	}
9616 	if (SEQ_GEQ(end, rsm->r_end)) {
9617 		/**
9618 		 * The end of this block is either beyond this guy or right
9619 		 * at this guy. I.e.:
9620 		 *  rsm ---                 |-----|
9621 		 *  end                     |-----|
9622 		 *  <or>
9623 		 *  end                     |---------|
9624 		 */
9625 		if ((rsm->r_flags & RACK_ACKED) == 0) {
9626 			/*
9627 			 * Is it a TLP of interest?
9628 			 */
9629 			if ((rsm->r_flags & RACK_TLP) &&
9630 			    (rsm->r_rtr_cnt > 1)) {
9631 				/*
9632 				 * We are splitting a rxt TLP, check
9633 				 * if we need to save off the start/end
9634 				 */
9635 				if (rack->rc_last_tlp_acked_set &&
9636 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9637 					/*
9638 					 * We already turned this on since we are inside
9639 					 * the previous one was a partially sack now we
9640 					 * are getting another one (maybe all of it).
9641 					 */
9642 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9643 					/*
9644 					 * Lets make sure we have all of it though.
9645 					 */
9646 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9647 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9648 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9649 								     rack->r_ctl.last_tlp_acked_end);
9650 					}
9651 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9652 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9653 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9654 								     rack->r_ctl.last_tlp_acked_end);
9655 					}
9656 				} else {
9657 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9658 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9659 					rack->rc_last_tlp_past_cumack = 0;
9660 					rack->rc_last_tlp_acked_set = 1;
9661 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9662 				}
9663 			}
9664 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9665 			changed += (rsm->r_end - rsm->r_start);
9666 			/* You get a count for acking a whole segment or more */
9667 			if (rsm->r_flags & RACK_WAS_LOST) {
9668 				int my_chg;
9669 
9670 				my_chg = (rsm->r_end - rsm->r_start);
9671 				rsm->r_flags &= ~RACK_WAS_LOST;
9672 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9673 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9674 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9675 					rack->r_ctl.rc_considered_lost -= my_chg;
9676 				else
9677 					rack->r_ctl.rc_considered_lost = 0;
9678 			}
9679 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9680 			if (rsm->r_in_tmap) /* should be true */
9681 				rack_log_sack_passed(tp, rack, rsm, cts);
9682 			/* Is Reordering occuring? */
9683 			if (rsm->r_flags & RACK_SACK_PASSED) {
9684 				rsm->r_flags &= ~RACK_SACK_PASSED;
9685 				rack->r_ctl.rc_reorder_ts = cts;
9686 				if (rack->r_ctl.rc_reorder_ts == 0)
9687 					rack->r_ctl.rc_reorder_ts = 1;
9688 			}
9689 			if (rack->app_limited_needs_set)
9690 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9691 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9692 			rsm->r_flags |= RACK_ACKED;
9693 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
9694 			if (rsm->r_in_tmap) {
9695 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9696 				rsm->r_in_tmap = 0;
9697 			}
9698 			rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
9699 		} else {
9700 			counter_u64_add(rack_sack_skipped_acked, 1);
9701 		}
9702 		if (end == rsm->r_end) {
9703 			/* This block only - done, setup for next */
9704 			goto out;
9705 		}
9706 		/*
9707 		 * There is more not coverend by this rsm move on
9708 		 * to the next block in the tail queue hash table.
9709 		 */
9710 		nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
9711 		start = rsm->r_end;
9712 		rsm = nrsm;
9713 		if (rsm == NULL)
9714 			goto out;
9715 		goto do_rest_ofb;
9716 	}
9717 	/**
9718 	 * The end of this sack block is smaller than
9719 	 * our rsm i.e.:
9720 	 *  rsm ---                 |-----|
9721 	 *  end                     |--|
9722 	 */
9723 	if ((rsm->r_flags & RACK_ACKED) == 0) {
9724 		/*
9725 		 * Is it a TLP of interest?
9726 		 */
9727 		if ((rsm->r_flags & RACK_TLP) &&
9728 		    (rsm->r_rtr_cnt > 1)) {
9729 			/*
9730 			 * We are splitting a rxt TLP, check
9731 			 * if we need to save off the start/end
9732 			 */
9733 			if (rack->rc_last_tlp_acked_set &&
9734 			    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9735 				/*
9736 				 * We already turned this on since we are inside
9737 				 * the previous one was a partially sack now we
9738 				 * are getting another one (maybe all of it).
9739 				 */
9740 				rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9741 				/*
9742 				 * Lets make sure we have all of it though.
9743 				 */
9744 				if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9745 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9746 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9747 							     rack->r_ctl.last_tlp_acked_end);
9748 				}
9749 				if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9750 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9751 					rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9752 							     rack->r_ctl.last_tlp_acked_end);
9753 				}
9754 			} else {
9755 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9756 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9757 				rack->rc_last_tlp_past_cumack = 0;
9758 				rack->rc_last_tlp_acked_set = 1;
9759 				rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9760 			}
9761 		}
9762 		/*
9763 		 * Hookery can only be used if the two entries
9764 		 * are in the same bucket and neither one of
9765 		 * them staddle the bucket line.
9766 		 */
9767 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
9768 		if (prev &&
9769 		    (rsm->bindex == prev->bindex) &&
9770 		    ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9771 		    ((prev->r_flags & RACK_STRADDLE) == 0) &&
9772 		    ((rsm->r_flags & RACK_IS_PCM) == 0) &&
9773 		    ((prev->r_flags & RACK_IS_PCM) == 0) &&
9774 		    (rsm->r_flags & RACK_IN_GP_WIN) &&
9775 		    (prev->r_flags & RACK_IN_GP_WIN))
9776 			can_use_hookery = 1;
9777 		else
9778 			can_use_hookery = 0;
9779 		if (prev && can_use_hookery &&
9780 		    (prev->r_flags & RACK_ACKED)) {
9781 			/**
9782 			 * Goal, we want the right remainder of rsm to shrink
9783 			 * in place and span from (rsm->r_start = end) to rsm->r_end.
9784 			 * We want to expand prev to go all the way
9785 			 * to prev->r_end <- end.
9786 			 * so in the tree we have before:
9787 			 *   prev     |--------|         (acked)
9788 			 *   rsm               |-------| (non-acked)
9789 			 *   sackblk           |-|
9790 			 * We churn it so we end up with
9791 			 *   prev     |----------|       (acked)
9792 			 *   rsm                 |-----| (non-acked)
9793 			 *   nrsm              |-| (temporary)
9794 			 *
9795 			 * Note if either prev/rsm is a TLP we don't
9796 			 * do this.
9797 			 */
9798 			nrsm = &stack_map;
9799 			memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9800 			tqhash_update_end(rack->r_ctl.tqh, prev, end);
9801 			rsm->r_start = end;
9802 			rsm->r_flags |= RACK_SHUFFLED;
9803 			prev->r_flags |= RACK_SHUFFLED;
9804 			/* Now adjust nrsm (stack copy) to be
9805 			 * the one that is the small
9806 			 * piece that was "sacked".
9807 			 */
9808 			nrsm->r_end = end;
9809 			rsm->r_dupack = 0;
9810 			/*
9811 			 * Which timestamp do we keep? It is rather
9812 			 * important in GP measurements to have the
9813 			 * accurate end of the send window.
9814 			 *
9815 			 * We keep the largest value, which is the newest
9816 			 * send. We do this in case a segment that is
9817 			 * joined together and not part of a GP estimate
9818 			 * later gets expanded into the GP estimate.
9819 			 *
9820 			 * We prohibit the merging of unlike kinds i.e.
9821 			 * all pieces that are in the GP estimate can be
9822 			 * merged and all pieces that are not in a GP estimate
9823 			 * can be merged, but not disimilar pieces. Combine
9824 			 * this with taking the highest here and we should
9825 			 * be ok unless of course the client reneges. Then
9826 			 * all bets are off.
9827 			 */
9828 			if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] <
9829 			   nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) {
9830 				prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9831 			}
9832 			/*
9833 			 * And we must keep the newest ack arrival time.
9834 			 */
9835 
9836 			if(prev->r_ack_arrival <
9837 			   rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9838 				prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9839 
9840 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9841 			/*
9842 			 * Now that the rsm has had its start moved forward
9843 			 * lets go ahead and get its new place in the world.
9844 			 */
9845 			rack_setup_offset_for_rsm(rack, prev, rsm);
9846 			/*
9847 			 * Now nrsm is our new little piece
9848 			 * that is acked (which was merged
9849 			 * to prev). Update the rtt and changed
9850 			 * based on that. Also check for reordering.
9851 			 */
9852 			rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9853 			if (rack->app_limited_needs_set)
9854 				rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9855 			changed += (nrsm->r_end - nrsm->r_start);
9856 			rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9857 			if (rsm->r_flags & RACK_WAS_LOST) {
9858 				int my_chg;
9859 
9860 				my_chg = (nrsm->r_end - nrsm->r_start);
9861 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9862 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9863 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9864 					rack->r_ctl.rc_considered_lost -= my_chg;
9865 				else
9866 					rack->r_ctl.rc_considered_lost = 0;
9867 			}
9868 			if (nrsm->r_flags & RACK_SACK_PASSED) {
9869 				rack->r_ctl.rc_reorder_ts = cts;
9870 				if (rack->r_ctl.rc_reorder_ts == 0)
9871 					rack->r_ctl.rc_reorder_ts = 1;
9872 			}
9873 			rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
9874 			rsm = prev;
9875 			counter_u64_add(rack_sack_used_prev_merge, 1);
9876 		} else {
9877 			/**
9878 			 * This is the case where our previous
9879 			 * block is not acked either, so we must
9880 			 * split the block in two.
9881 			 */
9882 			nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9883 			if (nrsm == NULL) {
9884 				/* failed rrs what can we do but loose the sack info? */
9885 				goto out;
9886 			}
9887 			if ((rsm->r_flags & RACK_TLP) &&
9888 			    (rsm->r_rtr_cnt > 1)) {
9889 				/*
9890 				 * We are splitting a rxt TLP, check
9891 				 * if we need to save off the start/end
9892 				 */
9893 				if (rack->rc_last_tlp_acked_set &&
9894 				    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9895 					/*
9896 					 * We already turned this on since this block is inside
9897 					 * the previous one was a partially sack now we
9898 					 * are getting another one (maybe all of it).
9899 					 */
9900 					rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9901 					/*
9902 					 * Lets make sure we have all of it though.
9903 					 */
9904 					if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9905 						rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9906 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9907 								     rack->r_ctl.last_tlp_acked_end);
9908 					}
9909 					if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9910 						rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9911 						rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9912 								     rack->r_ctl.last_tlp_acked_end);
9913 					}
9914 				} else {
9915 					rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9916 					rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9917 					rack->rc_last_tlp_acked_set = 1;
9918 					rack->rc_last_tlp_past_cumack = 0;
9919 					rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9920 				}
9921 			}
9922 			/**
9923 			 * In this case nrsm becomes
9924 			 * nrsm->r_start = end;
9925 			 * nrsm->r_end = rsm->r_end;
9926 			 * which is un-acked.
9927 			 * <and>
9928 			 * rsm->r_end = nrsm->r_start;
9929 			 * i.e. the remaining un-acked
9930 			 * piece is left on the left
9931 			 * hand side.
9932 			 *
9933 			 * So we start like this
9934 			 * rsm      |----------| (not acked)
9935 			 * sackblk  |---|
9936 			 * build it so we have
9937 			 * rsm      |---|         (acked)
9938 			 * nrsm         |------|  (not acked)
9939 			 */
9940 			counter_u64_add(rack_sack_splits, 1);
9941 			rack_clone_rsm(rack, nrsm, rsm, end);
9942 			rsm->r_flags &= (~RACK_HAS_FIN);
9943 			rsm->r_just_ret = 0;
9944 #ifndef INVARIANTS
9945 			(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9946 #else
9947 			if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9948 				panic("Insert in tailq_hash of %p fails ret:% rack:%p rsm:%p",
9949 				      nrsm, insret, rack, rsm);
9950 			}
9951 #endif
9952 			if (rsm->r_in_tmap) {
9953 				TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9954 				nrsm->r_in_tmap = 1;
9955 			}
9956 			nrsm->r_dupack = 0;
9957 			rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
9958 			rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9959 			changed += (rsm->r_end - rsm->r_start);
9960 			if (rsm->r_flags & RACK_WAS_LOST) {
9961 				int my_chg;
9962 
9963 				my_chg = (rsm->r_end - rsm->r_start);
9964 				rsm->r_flags &= ~RACK_WAS_LOST;
9965 				KASSERT((rack->r_ctl.rc_considered_lost >= my_chg),
9966 					("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
9967 				if (my_chg <= rack->r_ctl.rc_considered_lost)
9968 					rack->r_ctl.rc_considered_lost -= my_chg;
9969 				else
9970 					rack->r_ctl.rc_considered_lost = 0;
9971 			}
9972 			rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9973 
9974 			if (rsm->r_in_tmap) /* should be true */
9975 				rack_log_sack_passed(tp, rack, rsm, cts);
9976 			/* Is Reordering occuring? */
9977 			if (rsm->r_flags & RACK_SACK_PASSED) {
9978 				rsm->r_flags &= ~RACK_SACK_PASSED;
9979 				rack->r_ctl.rc_reorder_ts = cts;
9980 				if (rack->r_ctl.rc_reorder_ts == 0)
9981 					rack->r_ctl.rc_reorder_ts = 1;
9982 			}
9983 			if (rack->app_limited_needs_set)
9984 				rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9985 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9986 			rsm->r_flags |= RACK_ACKED;
9987 			rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end);
9988 			rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
9989 			if (rsm->r_in_tmap) {
9990 				TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9991 				rsm->r_in_tmap = 0;
9992 			}
9993 		}
9994 	} else if (start != end){
9995 		/*
9996 		 * The block was already acked.
9997 		 */
9998 		counter_u64_add(rack_sack_skipped_acked, 1);
9999 	}
10000 out:
10001 	if (rsm &&
10002 	    ((rsm->r_flags & RACK_TLP) == 0) &&
10003 	    (rsm->r_flags & RACK_ACKED)) {
10004 		/*
10005 		 * Now can we merge where we worked
10006 		 * with either the previous or
10007 		 * next block?
10008 		 */
10009 		next = tqhash_next(rack->r_ctl.tqh, rsm);
10010 		while (next) {
10011 			if (next->r_flags & RACK_TLP)
10012 				break;
10013 			/* Only allow merges between ones in or out of GP window */
10014 			if ((next->r_flags & RACK_IN_GP_WIN) &&
10015 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10016 				break;
10017 			}
10018 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10019 			    ((next->r_flags & RACK_IN_GP_WIN) == 0)) {
10020 				break;
10021 			}
10022 			if (rsm->bindex != next->bindex)
10023 				break;
10024 			if (rsm->r_flags & RACK_STRADDLE)
10025 				break;
10026 			if (rsm->r_flags & RACK_IS_PCM)
10027 				break;
10028 			if (next->r_flags & RACK_STRADDLE)
10029 				break;
10030 			if (next->r_flags & RACK_IS_PCM)
10031 				break;
10032 			if (next->r_flags & RACK_ACKED) {
10033 				/* yep this and next can be merged */
10034 				rsm = rack_merge_rsm(rack, rsm, next);
10035 				next = tqhash_next(rack->r_ctl.tqh, rsm);
10036 			} else
10037 				break;
10038 		}
10039 		/* Now what about the previous? */
10040 		prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10041 		while (prev) {
10042 			if (prev->r_flags & RACK_TLP)
10043 				break;
10044 			/* Only allow merges between ones in or out of GP window */
10045 			if ((prev->r_flags & RACK_IN_GP_WIN) &&
10046 			    ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10047 				break;
10048 			}
10049 			if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10050 			    ((prev->r_flags & RACK_IN_GP_WIN) == 0)) {
10051 				break;
10052 			}
10053 			if (rsm->bindex != prev->bindex)
10054 				break;
10055 			if (rsm->r_flags & RACK_STRADDLE)
10056 				break;
10057 			if (rsm->r_flags & RACK_IS_PCM)
10058 				break;
10059 			if (prev->r_flags & RACK_STRADDLE)
10060 				break;
10061 			if (prev->r_flags & RACK_IS_PCM)
10062 				break;
10063 			if (prev->r_flags & RACK_ACKED) {
10064 				/* yep the previous and this can be merged */
10065 				rsm = rack_merge_rsm(rack, prev, rsm);
10066 				prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10067 			} else
10068 				break;
10069 		}
10070 	}
10071 	if (used_ref == 0) {
10072 		counter_u64_add(rack_sack_proc_all, 1);
10073 	} else {
10074 		counter_u64_add(rack_sack_proc_short, 1);
10075 	}
10076 	/* Save off the next one for quick reference. */
10077 	nrsm = tqhash_find(rack->r_ctl.tqh, end);
10078 	*prsm = rack->r_ctl.rc_sacklast = nrsm;
10079 	return (changed);
10080 }
10081 
10082 static void inline
rack_peer_reneges(struct tcp_rack * rack,struct rack_sendmap * rsm,tcp_seq th_ack)10083 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
10084 {
10085 	struct rack_sendmap *tmap;
10086 
10087 	tmap = NULL;
10088 	while (rsm && (rsm->r_flags & RACK_ACKED)) {
10089 		/* Its no longer sacked, mark it so */
10090 		rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10091 #ifdef INVARIANTS
10092 		if (rsm->r_in_tmap) {
10093 			panic("rack:%p rsm:%p flags:0x%x in tmap?",
10094 			      rack, rsm, rsm->r_flags);
10095 		}
10096 #endif
10097 		rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
10098 		/* Rebuild it into our tmap */
10099 		if (tmap == NULL) {
10100 			TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10101 			tmap = rsm;
10102 		} else {
10103 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
10104 			tmap = rsm;
10105 		}
10106 		tmap->r_in_tmap = 1;
10107 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10108 	}
10109 	/*
10110 	 * Now lets possibly clear the sack filter so we start
10111 	 * recognizing sacks that cover this area.
10112 	 */
10113 	sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
10114 
10115 }
10116 
10117 
10118 static void inline
rack_rsm_sender_update(struct tcp_rack * rack,struct tcpcb * tp,struct rack_sendmap * rsm,uint8_t from)10119 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from)
10120 {
10121 	/*
10122 	 * We look at advancing the end send time for our GP
10123 	 * measurement tracking only as the cumulative acknowledgment
10124 	 * moves forward. You might wonder about this, why not
10125 	 * at every transmission or retransmission within the
10126 	 * GP window update the rc_gp_cumack_ts? Well its rather
10127 	 * nuanced but basically the GP window *may* expand (as
10128 	 * it does below) or worse and harder to track it may shrink.
10129 	 *
10130 	 * This last makes it impossible to track at the time of
10131 	 * the send, since you may set forward your rc_gp_cumack_ts
10132 	 * when you send, because that send *is* in your currently
10133 	 * "guessed" window, but then it shrinks. Now which was
10134 	 * the send time of the last bytes in the window, by the
10135 	 * time you ask that question that part of the sendmap
10136 	 * is freed. So you don't know and you will have too
10137 	 * long of send window. Instead by updating the time
10138 	 * marker only when the cumack advances this assures us
10139 	 * that we will have only the sends in the window of our
10140 	 * GP measurement.
10141 	 *
10142 	 * Another complication from this is the
10143 	 * merging of sendmap entries. During SACK processing this
10144 	 * can happen to conserve the sendmap size. That breaks
10145 	 * everything down in tracking the send window of the GP
10146 	 * estimate. So to prevent that and keep it working with
10147 	 * a tiny bit more limited merging, we only allow like
10148 	 * types to be merged. I.e. if two sends are in the GP window
10149 	 * then its ok to merge them together. If two sends are not
10150 	 * in the GP window its ok to merge them together too. Though
10151 	 * one send in and one send out cannot be merged. We combine
10152 	 * this with never allowing the shrinking of the GP window when
10153 	 * we are in recovery so that we can properly calculate the
10154 	 * sending times.
10155 	 *
10156 	 * This all of course seems complicated, because it is.. :)
10157 	 *
10158 	 * The cum-ack is being advanced upon the sendmap.
10159 	 * If we are not doing a GP estimate don't
10160 	 * proceed.
10161 	 */
10162 	uint64_t ts;
10163 
10164 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
10165 		return;
10166 	/*
10167 	 * If this sendmap entry is going
10168 	 * beyond the measurement window we had picked,
10169 	 * expand the measurement window by that much.
10170 	 */
10171 	if (SEQ_GT(rsm->r_end, tp->gput_ack)) {
10172 		tp->gput_ack = rsm->r_end;
10173 	}
10174 	/*
10175 	 * If we have not setup a ack, then we
10176 	 * have no idea if the newly acked pieces
10177 	 * will be "in our seq measurement range". If
10178 	 * it is when we clear the app_limited_needs_set
10179 	 * flag the timestamp will be updated.
10180 	 */
10181 	if (rack->app_limited_needs_set)
10182 		return;
10183 	/*
10184 	 * Finally, we grab out the latest timestamp
10185 	 * that this packet was sent and then see
10186 	 * if:
10187 	 *  a) The packet touches are newly defined GP range.
10188 	 *  b) The time is greater than (newer) than the
10189 	 *     one we currently have. If so we update
10190 	 *     our sending end time window.
10191 	 *
10192 	 * Note we *do not* do this at send time. The reason
10193 	 * is that if you do you *may* pick up a newer timestamp
10194 	 * for a range you are not going to measure. We project
10195 	 * out how far and then sometimes modify that to be
10196 	 * smaller. If that occurs then you will have a send
10197 	 * that does not belong to the range included.
10198 	 */
10199 	if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <=
10200 	    rack->r_ctl.rc_gp_cumack_ts)
10201 		return;
10202 	if (rack_in_gp_window(tp, rsm)) {
10203 		rack->r_ctl.rc_gp_cumack_ts = ts;
10204 		rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end,
10205 			       __LINE__, from, rsm);
10206 	}
10207 }
10208 
10209 static void
rack_process_to_cumack(struct tcpcb * tp,struct tcp_rack * rack,register uint32_t th_ack,uint32_t cts,struct tcpopt * to,uint64_t acktime)10210 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to, uint64_t acktime)
10211 {
10212 	struct rack_sendmap *rsm;
10213 	/*
10214 	 * The ACK point is advancing to th_ack, we must drop off
10215 	 * the packets in the rack log and calculate any eligble
10216 	 * RTT's.
10217 	 */
10218 
10219 	if (sack_filter_blks_used(&rack->r_ctl.rack_sf)) {
10220 		/*
10221 		 * If we have some sack blocks in the filter
10222 		 * lets prune them out by calling sfb with no blocks.
10223 		 */
10224 		sack_filter_blks(tp, &rack->r_ctl.rack_sf, NULL, 0, th_ack);
10225 	}
10226 	if (SEQ_GT(th_ack, tp->snd_una)) {
10227 		/* Clear any app ack remembered settings */
10228 		rack->r_ctl.cleared_app_ack = 0;
10229 	}
10230 	rack->r_wanted_output = 1;
10231 	if (SEQ_GT(th_ack, tp->snd_una))
10232 		rack->r_ctl.last_cumack_advance = acktime;
10233 
10234 	/* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
10235 	if ((rack->rc_last_tlp_acked_set == 1)&&
10236 	    (rack->rc_last_tlp_past_cumack == 1) &&
10237 	    (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
10238 		/*
10239 		 * We have reached the point where our last rack
10240 		 * tlp retransmit sequence is ahead of the cum-ack.
10241 		 * This can only happen when the cum-ack moves all
10242 		 * the way around (its been a full 2^^31+1 bytes
10243 		 * or more since we sent a retransmitted TLP). Lets
10244 		 * turn off the valid flag since its not really valid.
10245 		 *
10246 		 * Note since sack's also turn on this event we have
10247 		 * a complication, we have to wait to age it out until
10248 		 * the cum-ack is by the TLP before checking which is
10249 		 * what the next else clause does.
10250 		 */
10251 		rack_log_dsack_event(rack, 9, __LINE__,
10252 				     rack->r_ctl.last_tlp_acked_start,
10253 				     rack->r_ctl.last_tlp_acked_end);
10254 		rack->rc_last_tlp_acked_set = 0;
10255 		rack->rc_last_tlp_past_cumack = 0;
10256 	} else if ((rack->rc_last_tlp_acked_set == 1) &&
10257 		   (rack->rc_last_tlp_past_cumack == 0) &&
10258 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
10259 		/*
10260 		 * It is safe to start aging TLP's out.
10261 		 */
10262 		rack->rc_last_tlp_past_cumack = 1;
10263 	}
10264 	/* We do the same for the tlp send seq as well */
10265 	if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10266 	    (rack->rc_last_sent_tlp_past_cumack == 1) &&
10267 	    (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
10268 		rack_log_dsack_event(rack, 9, __LINE__,
10269 				     rack->r_ctl.last_sent_tlp_seq,
10270 				     (rack->r_ctl.last_sent_tlp_seq +
10271 				      rack->r_ctl.last_sent_tlp_len));
10272 		rack->rc_last_sent_tlp_seq_valid = 0;
10273 		rack->rc_last_sent_tlp_past_cumack = 0;
10274 	} else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10275 		   (rack->rc_last_sent_tlp_past_cumack == 0) &&
10276 		   (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
10277 		/*
10278 		 * It is safe to start aging TLP's send.
10279 		 */
10280 		rack->rc_last_sent_tlp_past_cumack = 1;
10281 	}
10282 more:
10283 	rsm = tqhash_min(rack->r_ctl.tqh);
10284 	if (rsm == NULL) {
10285 		if ((th_ack - 1) == tp->iss) {
10286 			/*
10287 			 * For the SYN incoming case we will not
10288 			 * have called tcp_output for the sending of
10289 			 * the SYN, so there will be no map. All
10290 			 * other cases should probably be a panic.
10291 			 */
10292 			return;
10293 		}
10294 		if (tp->t_flags & TF_SENTFIN) {
10295 			/* if we sent a FIN we often will not have map */
10296 			return;
10297 		}
10298 #ifdef INVARIANTS
10299 		panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u\n",
10300 		      tp,
10301 		      tp->t_state, th_ack, rack,
10302 		      tp->snd_una, tp->snd_max);
10303 #endif
10304 		return;
10305 	}
10306 	if (SEQ_LT(th_ack, rsm->r_start)) {
10307 		/* Huh map is missing this */
10308 #ifdef INVARIANTS
10309 		printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
10310 		       rsm->r_start,
10311 		       th_ack, tp->t_state, rack->r_state);
10312 #endif
10313 		return;
10314 	}
10315 	rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
10316 
10317 	/* Now was it a retransmitted TLP? */
10318 	if ((rsm->r_flags & RACK_TLP) &&
10319 	    (rsm->r_rtr_cnt > 1)) {
10320 		/*
10321 		 * Yes, this rsm was a TLP and retransmitted, remember that
10322 		 * since if a DSACK comes back on this we don't want
10323 		 * to think of it as a reordered segment. This may
10324 		 * get updated again with possibly even other TLPs
10325 		 * in flight, but thats ok. Only when we don't send
10326 		 * a retransmitted TLP for 1/2 the sequences space
10327 		 * will it get turned off (above).
10328 		 */
10329 		if (rack->rc_last_tlp_acked_set &&
10330 		    (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10331 			/*
10332 			 * We already turned this on since the end matches,
10333 			 * the previous one was a partially ack now we
10334 			 * are getting another one (maybe all of it).
10335 			 */
10336 			rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10337 			/*
10338 			 * Lets make sure we have all of it though.
10339 			 */
10340 			if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10341 				rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10342 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10343 						     rack->r_ctl.last_tlp_acked_end);
10344 			}
10345 			if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10346 				rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10347 				rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10348 						     rack->r_ctl.last_tlp_acked_end);
10349 			}
10350 		} else {
10351 			rack->rc_last_tlp_past_cumack = 1;
10352 			rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10353 			rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10354 			rack->rc_last_tlp_acked_set = 1;
10355 			rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10356 		}
10357 	}
10358 	/* Now do we consume the whole thing? */
10359 	rack->r_ctl.last_tmit_time_acked = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
10360 	if (SEQ_GEQ(th_ack, rsm->r_end)) {
10361 		/* Its all consumed. */
10362 		uint32_t left;
10363 		uint8_t newly_acked;
10364 
10365 		if (rsm->r_flags & RACK_WAS_LOST) {
10366 			/*
10367 			 * This can happen when we marked it as lost
10368 			 * and yet before retransmitting we get an ack
10369 			 * which can happen due to reordering.
10370 			 */
10371 			rsm->r_flags  &= ~RACK_WAS_LOST;
10372 			KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)),
10373 				("rsm:%p rack:%p rc_considered_lost goes negative", rsm,  rack));
10374 			if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start))
10375 				rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start;
10376 			else
10377 				rack->r_ctl.rc_considered_lost = 0;
10378 		}
10379 		rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
10380 		rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
10381 		rsm->r_rtr_bytes = 0;
10382 		/*
10383 		 * Record the time of highest cumack sent if its in our measurement
10384 		 * window and possibly bump out the end.
10385 		 */
10386 		rack_rsm_sender_update(rack, tp, rsm, 4);
10387 		tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
10388 		if (rsm->r_in_tmap) {
10389 			TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10390 			rsm->r_in_tmap = 0;
10391 		}
10392 		newly_acked = 1;
10393 		if (rsm->r_flags & RACK_ACKED) {
10394 			/*
10395 			 * It was acked on the scoreboard -- remove
10396 			 * it from total
10397 			 */
10398 			rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10399 			newly_acked = 0;
10400 		} else if (rsm->r_flags & RACK_SACK_PASSED) {
10401 			/*
10402 			 * There are segments ACKED on the
10403 			 * scoreboard further up. We are seeing
10404 			 * reordering.
10405 			 */
10406 			rsm->r_flags &= ~RACK_SACK_PASSED;
10407 			rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10408 			rsm->r_flags |= RACK_ACKED;
10409 			rack->r_ctl.rc_reorder_ts = cts;
10410 			if (rack->r_ctl.rc_reorder_ts == 0)
10411 				rack->r_ctl.rc_reorder_ts = 1;
10412 			if (rack->r_ent_rec_ns) {
10413 				/*
10414 				 * We have sent no more, and we saw an sack
10415 				 * then ack arrive.
10416 				 */
10417 				rack->r_might_revert = 1;
10418 			}
10419 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
10420 		} else {
10421 			rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end);
10422 		}
10423 		if ((rsm->r_flags & RACK_TO_REXT) &&
10424 		    (tp->t_flags & TF_RCVD_TSTMP) &&
10425 		    (to->to_flags & TOF_TS) &&
10426 		    (to->to_tsecr != 0) &&
10427 		    (tp->t_flags & TF_PREVVALID)) {
10428 			/*
10429 			 * We can use the timestamp to see
10430 			 * if this retransmission was from the
10431 			 * first transmit. If so we made a mistake.
10432 			 */
10433 			tp->t_flags &= ~TF_PREVVALID;
10434 			if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
10435 				/* The first transmit is what this ack is for */
10436 				rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
10437 			}
10438 		}
10439 		left = th_ack - rsm->r_end;
10440 		if (rack->app_limited_needs_set && newly_acked)
10441 			rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
10442 		/* Free back to zone */
10443 		rack_free(rack, rsm);
10444 		if (left) {
10445 			goto more;
10446 		}
10447 		/* Check for reneging */
10448 		rsm = tqhash_min(rack->r_ctl.tqh);
10449 		if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
10450 			/*
10451 			 * The peer has moved snd_una up to
10452 			 * the edge of this send, i.e. one
10453 			 * that it had previously acked. The only
10454 			 * way that can be true if the peer threw
10455 			 * away data (space issues) that it had
10456 			 * previously sacked (else it would have
10457 			 * given us snd_una up to (rsm->r_end).
10458 			 * We need to undo the acked markings here.
10459 			 *
10460 			 * Note we have to look to make sure th_ack is
10461 			 * our rsm->r_start in case we get an old ack
10462 			 * where th_ack is behind snd_una.
10463 			 */
10464 			rack_peer_reneges(rack, rsm, th_ack);
10465 		}
10466 		return;
10467 	}
10468 	if (rsm->r_flags & RACK_ACKED) {
10469 		/*
10470 		 * It was acked on the scoreboard -- remove it from
10471 		 * total for the part being cum-acked.
10472 		 */
10473 		rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
10474 	} else {
10475 		rack_update_pcm_ack(rack, 1, rsm->r_start, th_ack);
10476 	}
10477 	/* And what about the lost flag? */
10478 	if (rsm->r_flags & RACK_WAS_LOST) {
10479 		/*
10480 		 * This can happen when we marked it as lost
10481 		 * and yet before retransmitting we get an ack
10482 		 * which can happen due to reordering. In this
10483 		 * case its only a partial ack of the send.
10484 		 */
10485 		KASSERT((rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start)),
10486 			("rsm:%p rack:%p rc_considered_lost goes negative th_ack:%u", rsm,  rack, th_ack));
10487 		if (rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start))
10488 			rack->r_ctl.rc_considered_lost -= th_ack - rsm->r_start;
10489 		else
10490 			rack->r_ctl.rc_considered_lost = 0;
10491 	}
10492 	/*
10493 	 * Clear the dup ack count for
10494 	 * the piece that remains.
10495 	 */
10496 	rsm->r_dupack = 0;
10497 	rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10498 	if (rsm->r_rtr_bytes) {
10499 		/*
10500 		 * It was retransmitted adjust the
10501 		 * sack holes for what was acked.
10502 		 */
10503 		int ack_am;
10504 
10505 		ack_am = (th_ack - rsm->r_start);
10506 		if (ack_am >= rsm->r_rtr_bytes) {
10507 			rack->r_ctl.rc_holes_rxt -= ack_am;
10508 			rsm->r_rtr_bytes -= ack_am;
10509 		}
10510 	}
10511 	/*
10512 	 * Update where the piece starts and record
10513 	 * the time of send of highest cumack sent if
10514 	 * its in our GP range.
10515 	 */
10516 	rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
10517 	/* Now we need to move our offset forward too */
10518 	if (rsm->m &&
10519 	    ((rsm->orig_m_len != rsm->m->m_len) ||
10520 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
10521 		/* Fix up the orig_m_len and possibly the mbuf offset */
10522 		rack_adjust_orig_mlen(rsm);
10523 	}
10524 	rsm->soff += (th_ack - rsm->r_start);
10525 	rack_rsm_sender_update(rack, tp, rsm, 5);
10526 	/* The trim will move th_ack into r_start for us */
10527 	tqhash_trim(rack->r_ctl.tqh, th_ack);
10528 	/* Now do we need to move the mbuf fwd too? */
10529 	{
10530 		struct mbuf *m;
10531 		uint32_t soff;
10532 
10533 		m = rsm->m;
10534 		soff = rsm->soff;
10535 		if (m) {
10536 			while (soff >= m->m_len) {
10537 				soff -= m->m_len;
10538 				KASSERT((m->m_next != NULL),
10539 					(" rsm:%p  off:%u soff:%u m:%p",
10540 					 rsm, rsm->soff, soff, m));
10541 				m = m->m_next;
10542 				if (m == NULL) {
10543 					/*
10544 					 * This is a fall-back that prevents a panic. In reality
10545 					 * we should be able to walk the mbuf's and find our place.
10546 					 * At this point snd_una has not been updated with the sbcut() yet
10547 					 * but tqhash_trim did update rsm->r_start so the offset calcuation
10548 					 * should work fine. This is undesirable since we will take cache
10549 					 * hits to access the socket buffer. And even more puzzling is that
10550 					 * it happens occasionally. It should not :(
10551 					 */
10552 					m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
10553 						      (rsm->r_start - tp->snd_una),
10554 						      &soff);
10555 					break;
10556 				}
10557 			}
10558 			/*
10559 			 * Now save in our updated values.
10560 			 */
10561 			rsm->m = m;
10562 			rsm->soff = soff;
10563 			rsm->orig_m_len = rsm->m->m_len;
10564 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
10565 		}
10566 	}
10567 	if (rack->app_limited_needs_set &&
10568 	    SEQ_GEQ(th_ack, tp->gput_seq))
10569 		rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
10570 }
10571 
10572 static void
rack_handle_might_revert(struct tcpcb * tp,struct tcp_rack * rack)10573 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
10574 {
10575 	struct rack_sendmap *rsm;
10576 	int sack_pass_fnd = 0;
10577 
10578 	if (rack->r_might_revert) {
10579 		/*
10580 		 * Ok we have reordering, have not sent anything, we
10581 		 * might want to revert the congestion state if nothing
10582 		 * further has SACK_PASSED on it. Lets check.
10583 		 *
10584 		 * We also get here when we have DSACKs come in for
10585 		 * all the data that we FR'd. Note that a rxt or tlp
10586 		 * timer clears this from happening.
10587 		 */
10588 
10589 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
10590 			if (rsm->r_flags & RACK_SACK_PASSED) {
10591 				sack_pass_fnd = 1;
10592 				break;
10593 			}
10594 		}
10595 		if (sack_pass_fnd == 0) {
10596 			/*
10597 			 * We went into recovery
10598 			 * incorrectly due to reordering!
10599 			 */
10600 			int orig_cwnd;
10601 
10602 			rack->r_ent_rec_ns = 0;
10603 			orig_cwnd = tp->snd_cwnd;
10604 			tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
10605 			tp->snd_recover = tp->snd_una;
10606 			rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
10607 			if (IN_RECOVERY(tp->t_flags)) {
10608 				rack_exit_recovery(tp, rack, 3);
10609 				if ((rack->rto_from_rec == 1) && (rack_ssthresh_rest_rto_rec != 0) ){
10610 					/*
10611 					 * We were in recovery, had an RTO
10612 					 * and then re-entered recovery (more sack's arrived)
10613 					 * and we have properly recorded the old ssthresh from
10614 					 * the first recovery. We want to be able to slow-start
10615 					 * back to this level. The ssthresh from the timeout
10616 					 * and then back into recovery will end up most likely
10617 					 * to be min(cwnd=1mss, 2mss). Which makes it basically
10618 					 * so we get no slow-start after our RTO.
10619 					 */
10620 					rack->rto_from_rec = 0;
10621 					if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh)
10622 						tp->snd_ssthresh = rack->r_ctl.rto_ssthresh;
10623 				}
10624 			}
10625 		}
10626 		rack->r_might_revert = 0;
10627 	}
10628 }
10629 
10630 
10631 static int
rack_note_dsack(struct tcp_rack * rack,tcp_seq start,tcp_seq end)10632 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
10633 {
10634 
10635 	uint32_t am, l_end;
10636 	int was_tlp = 0;
10637 
10638 	if (SEQ_GT(end, start))
10639 		am = end - start;
10640 	else
10641 		am = 0;
10642 	if ((rack->rc_last_tlp_acked_set ) &&
10643 	    (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
10644 	    (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
10645 		/*
10646 		 * The DSACK is because of a TLP which we don't
10647 		 * do anything with the reordering window over since
10648 		 * it was not reordering that caused the DSACK but
10649 		 * our previous retransmit TLP.
10650 		 */
10651 		rack_log_dsack_event(rack, 7, __LINE__, start, end);
10652 		was_tlp = 1;
10653 		goto skip_dsack_round;
10654 	}
10655 	if (rack->rc_last_sent_tlp_seq_valid) {
10656 		l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
10657 		if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
10658 		    (SEQ_LEQ(end, l_end))) {
10659 			/*
10660 			 * This dsack is from the last sent TLP, ignore it
10661 			 * for reordering purposes.
10662 			 */
10663 			rack_log_dsack_event(rack, 7, __LINE__, start, end);
10664 			was_tlp = 1;
10665 			goto skip_dsack_round;
10666 		}
10667 	}
10668 	if (rack->rc_dsack_round_seen == 0) {
10669 		rack->rc_dsack_round_seen = 1;
10670 		rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
10671 		rack->r_ctl.num_dsack++;
10672 		rack->r_ctl.dsack_persist = 16;	/* 16 is from the standard */
10673 		rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
10674 	}
10675 skip_dsack_round:
10676 	/*
10677 	 * We keep track of how many DSACK blocks we get
10678 	 * after a recovery incident.
10679 	 */
10680 	rack->r_ctl.dsack_byte_cnt += am;
10681 	if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
10682 	    rack->r_ctl.retran_during_recovery &&
10683 	    (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
10684 		/*
10685 		 * False recovery most likely culprit is reordering. If
10686 		 * nothing else is missing we need to revert.
10687 		 */
10688 		rack->r_might_revert = 1;
10689 		rack_handle_might_revert(rack->rc_tp, rack);
10690 		rack->r_might_revert = 0;
10691 		rack->r_ctl.retran_during_recovery = 0;
10692 		rack->r_ctl.dsack_byte_cnt = 0;
10693 	}
10694 	return (was_tlp);
10695 }
10696 
10697 static uint32_t
do_rack_compute_pipe(struct tcpcb * tp,struct tcp_rack * rack,uint32_t snd_una)10698 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una)
10699 {
10700 	return (((tp->snd_max - snd_una) -
10701 		 (rack->r_ctl.rc_sacked + rack->r_ctl.rc_considered_lost)) + rack->r_ctl.rc_holes_rxt);
10702 }
10703 
10704 static int32_t
rack_compute_pipe(struct tcpcb * tp)10705 rack_compute_pipe(struct tcpcb *tp)
10706 {
10707 	return ((int32_t)do_rack_compute_pipe(tp,
10708 					      (struct tcp_rack *)tp->t_fb_ptr,
10709 					      tp->snd_una));
10710 }
10711 
10712 static void
rack_update_prr(struct tcpcb * tp,struct tcp_rack * rack,uint32_t changed,tcp_seq th_ack)10713 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
10714 {
10715 	/* Deal with changed and PRR here (in recovery only) */
10716 	uint32_t pipe, snd_una;
10717 
10718 	rack->r_ctl.rc_prr_delivered += changed;
10719 
10720 	if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
10721 		/*
10722 		 * It is all outstanding, we are application limited
10723 		 * and thus we don't need more room to send anything.
10724 		 * Note we use tp->snd_una here and not th_ack because
10725 		 * the data as yet not been cut from the sb.
10726 		 */
10727 		rack->r_ctl.rc_prr_sndcnt = 0;
10728 		return;
10729 	}
10730 	/* Compute prr_sndcnt */
10731 	if (SEQ_GT(tp->snd_una, th_ack)) {
10732 		snd_una = tp->snd_una;
10733 	} else {
10734 		snd_una = th_ack;
10735 	}
10736 	pipe = do_rack_compute_pipe(tp, rack, snd_una);
10737 	if (pipe > tp->snd_ssthresh) {
10738 		long sndcnt;
10739 
10740 		sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
10741 		if (rack->r_ctl.rc_prr_recovery_fs > 0)
10742 			sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
10743 		else {
10744 			rack->r_ctl.rc_prr_sndcnt = 0;
10745 			rack_log_to_prr(rack, 9, 0, __LINE__);
10746 			sndcnt = 0;
10747 		}
10748 		sndcnt++;
10749 		if (sndcnt > (long)rack->r_ctl.rc_prr_out)
10750 			sndcnt -= rack->r_ctl.rc_prr_out;
10751 		else
10752 			sndcnt = 0;
10753 		rack->r_ctl.rc_prr_sndcnt = sndcnt;
10754 		rack_log_to_prr(rack, 10, 0, __LINE__);
10755 	} else {
10756 		uint32_t limit;
10757 
10758 		if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
10759 			limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
10760 		else
10761 			limit = 0;
10762 		if (changed > limit)
10763 			limit = changed;
10764 		limit += ctf_fixed_maxseg(tp);
10765 		if (tp->snd_ssthresh > pipe) {
10766 			rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
10767 			rack_log_to_prr(rack, 11, 0, __LINE__);
10768 		} else {
10769 			rack->r_ctl.rc_prr_sndcnt = min(0, limit);
10770 			rack_log_to_prr(rack, 12, 0, __LINE__);
10771 		}
10772 	}
10773 }
10774 
10775 static void
rack_log_ack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th,int entered_recovery,int dup_ack_struck,int * dsack_seen,int * sacks_seen)10776 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck,
10777 	     int *dsack_seen, int *sacks_seen)
10778 {
10779 	uint32_t changed;
10780 	struct tcp_rack *rack;
10781 	struct rack_sendmap *rsm;
10782 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
10783 	register uint32_t th_ack;
10784 	int32_t i, j, k, num_sack_blks = 0;
10785 	uint32_t cts, acked, ack_point;
10786 	int loop_start = 0;
10787 	uint32_t tsused;
10788 	uint32_t segsiz;
10789 
10790 
10791 	INP_WLOCK_ASSERT(tptoinpcb(tp));
10792 	if (tcp_get_flags(th) & TH_RST) {
10793 		/* We don't log resets */
10794 		return;
10795 	}
10796 	rack = (struct tcp_rack *)tp->t_fb_ptr;
10797 	cts = tcp_get_usecs(NULL);
10798 	rsm = tqhash_min(rack->r_ctl.tqh);
10799 	changed = 0;
10800 	th_ack = th->th_ack;
10801 	segsiz = ctf_fixed_maxseg(rack->rc_tp);
10802 	if (BYTES_THIS_ACK(tp, th) >=  segsiz) {
10803 		/*
10804 		 * You only get credit for
10805 		 * MSS and greater (and you get extra
10806 		 * credit for larger cum-ack moves).
10807 		 */
10808 		int ac;
10809 
10810 		ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
10811 		counter_u64_add(rack_ack_total, ac);
10812 	}
10813 	if (SEQ_GT(th_ack, tp->snd_una)) {
10814 		rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
10815 		tp->t_acktime = ticks;
10816 	}
10817 	if (rsm && SEQ_GT(th_ack, rsm->r_start))
10818 		changed = th_ack - rsm->r_start;
10819 	if (changed) {
10820 		rack_process_to_cumack(tp, rack, th_ack, cts, to,
10821 				       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
10822 	}
10823 	if ((to->to_flags & TOF_SACK) == 0) {
10824 		/* We are done nothing left and no sack. */
10825 		rack_handle_might_revert(tp, rack);
10826 		/*
10827 		 * For cases where we struck a dup-ack
10828 		 * with no SACK, add to the changes so
10829 		 * PRR will work right.
10830 		 */
10831 		if (dup_ack_struck && (changed == 0)) {
10832 			changed += ctf_fixed_maxseg(rack->rc_tp);
10833 		}
10834 		goto out;
10835 	}
10836 	/* Sack block processing */
10837 	if (SEQ_GT(th_ack, tp->snd_una))
10838 		ack_point = th_ack;
10839 	else
10840 		ack_point = tp->snd_una;
10841 	for (i = 0; i < to->to_nsacks; i++) {
10842 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
10843 		      &sack, sizeof(sack));
10844 		sack.start = ntohl(sack.start);
10845 		sack.end = ntohl(sack.end);
10846 		if (SEQ_GT(sack.end, sack.start) &&
10847 		    SEQ_GT(sack.start, ack_point) &&
10848 		    SEQ_LT(sack.start, tp->snd_max) &&
10849 		    SEQ_GT(sack.end, ack_point) &&
10850 		    SEQ_LEQ(sack.end, tp->snd_max)) {
10851 			sack_blocks[num_sack_blks] = sack;
10852 			num_sack_blks++;
10853 		} else if (SEQ_LEQ(sack.start, th_ack) &&
10854 			   SEQ_LEQ(sack.end, th_ack)) {
10855 			int was_tlp;
10856 
10857 			if (dsack_seen != NULL)
10858 				*dsack_seen = 1;
10859 			was_tlp = rack_note_dsack(rack, sack.start, sack.end);
10860 			/*
10861 			 * Its a D-SACK block.
10862 			 */
10863 			tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
10864 		}
10865 	}
10866 	if (rack->rc_dsack_round_seen) {
10867 		/* Is the dsack roound over? */
10868 		if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
10869 			/* Yes it is */
10870 			rack->rc_dsack_round_seen = 0;
10871 			rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
10872 		}
10873 	}
10874 	/*
10875 	 * Sort the SACK blocks so we can update the rack scoreboard with
10876 	 * just one pass.
10877 	 */
10878 	num_sack_blks = sack_filter_blks(tp, &rack->r_ctl.rack_sf, sack_blocks,
10879 					 num_sack_blks, th->th_ack);
10880 	ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
10881 	if (sacks_seen != NULL)
10882 		*sacks_seen = num_sack_blks;
10883 	if (num_sack_blks == 0) {
10884 		/* Nothing to sack, but we need to update counts */
10885 		goto out_with_totals;
10886 	}
10887 	/* Its a sack of some sort */
10888 	if (num_sack_blks < 2) {
10889 		/* Only one, we don't need to sort */
10890 		goto do_sack_work;
10891 	}
10892 	/* Sort the sacks */
10893 	for (i = 0; i < num_sack_blks; i++) {
10894 		for (j = i + 1; j < num_sack_blks; j++) {
10895 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
10896 				sack = sack_blocks[i];
10897 				sack_blocks[i] = sack_blocks[j];
10898 				sack_blocks[j] = sack;
10899 			}
10900 		}
10901 	}
10902 	/*
10903 	 * Now are any of the sack block ends the same (yes some
10904 	 * implementations send these)?
10905 	 */
10906 again:
10907 	if (num_sack_blks == 0)
10908 		goto out_with_totals;
10909 	if (num_sack_blks > 1) {
10910 		for (i = 0; i < num_sack_blks; i++) {
10911 			for (j = i + 1; j < num_sack_blks; j++) {
10912 				if (sack_blocks[i].end == sack_blocks[j].end) {
10913 					/*
10914 					 * Ok these two have the same end we
10915 					 * want the smallest end and then
10916 					 * throw away the larger and start
10917 					 * again.
10918 					 */
10919 					if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
10920 						/*
10921 						 * The second block covers
10922 						 * more area use that
10923 						 */
10924 						sack_blocks[i].start = sack_blocks[j].start;
10925 					}
10926 					/*
10927 					 * Now collapse out the dup-sack and
10928 					 * lower the count
10929 					 */
10930 					for (k = (j + 1); k < num_sack_blks; k++) {
10931 						sack_blocks[j].start = sack_blocks[k].start;
10932 						sack_blocks[j].end = sack_blocks[k].end;
10933 						j++;
10934 					}
10935 					num_sack_blks--;
10936 					goto again;
10937 				}
10938 			}
10939 		}
10940 	}
10941 do_sack_work:
10942 	/*
10943 	 * First lets look to see if
10944 	 * we have retransmitted and
10945 	 * can use the transmit next?
10946 	 */
10947 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
10948 	if (rsm &&
10949 	    SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
10950 	    SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
10951 		/*
10952 		 * We probably did the FR and the next
10953 		 * SACK in continues as we would expect.
10954 		 */
10955 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, segsiz);
10956 		if (acked) {
10957 			rack->r_wanted_output = 1;
10958 			changed += acked;
10959 		}
10960 		if (num_sack_blks == 1) {
10961 			/*
10962 			 * This is what we would expect from
10963 			 * a normal implementation to happen
10964 			 * after we have retransmitted the FR,
10965 			 * i.e the sack-filter pushes down
10966 			 * to 1 block and the next to be retransmitted
10967 			 * is the sequence in the sack block (has more
10968 			 * are acked). Count this as ACK'd data to boost
10969 			 * up the chances of recovering any false positives.
10970 			 */
10971 			counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
10972 			counter_u64_add(rack_express_sack, 1);
10973 			goto out_with_totals;
10974 		} else {
10975 			/*
10976 			 * Start the loop through the
10977 			 * rest of blocks, past the first block.
10978 			 */
10979 			loop_start = 1;
10980 		}
10981 	}
10982 	counter_u64_add(rack_sack_total, 1);
10983 	rsm = rack->r_ctl.rc_sacklast;
10984 	for (i = loop_start; i < num_sack_blks; i++) {
10985 		acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts,  segsiz);
10986 		if (acked) {
10987 			rack->r_wanted_output = 1;
10988 			changed += acked;
10989 		}
10990 	}
10991 out_with_totals:
10992 	if (num_sack_blks > 1) {
10993 		/*
10994 		 * You get an extra stroke if
10995 		 * you have more than one sack-blk, this
10996 		 * could be where we are skipping forward
10997 		 * and the sack-filter is still working, or
10998 		 * it could be an attacker constantly
10999 		 * moving us.
11000 		 */
11001 		counter_u64_add(rack_move_some, 1);
11002 	}
11003 out:
11004 	if (changed) {
11005 		/* Something changed cancel the rack timer */
11006 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11007 	}
11008 	tsused = tcp_get_usecs(NULL);
11009 	rsm = tcp_rack_output(tp, rack, tsused);
11010 	if ((!IN_FASTRECOVERY(tp->t_flags)) &&
11011 	    rsm &&
11012 	    ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
11013 		/* Enter recovery */
11014 		entered_recovery = 1;
11015 		rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__);
11016 		/*
11017 		 * When we enter recovery we need to assure we send
11018 		 * one packet.
11019 		 */
11020 		if (rack->rack_no_prr == 0) {
11021 			rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
11022 			rack_log_to_prr(rack, 8, 0, __LINE__);
11023 		}
11024 		rack->r_timer_override = 1;
11025 		rack->r_early = 0;
11026 		rack->r_ctl.rc_agg_early = 0;
11027 	} else if (IN_FASTRECOVERY(tp->t_flags) &&
11028 		   rsm &&
11029 		   (rack->r_rr_config == 3)) {
11030 		/*
11031 		 * Assure we can output and we get no
11032 		 * remembered pace time except the retransmit.
11033 		 */
11034 		rack->r_timer_override = 1;
11035 		rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
11036 		rack->r_ctl.rc_resend = rsm;
11037 	}
11038 	if (IN_FASTRECOVERY(tp->t_flags) &&
11039 	    (rack->rack_no_prr == 0) &&
11040 	    (entered_recovery == 0)) {
11041 		rack_update_prr(tp, rack, changed, th_ack);
11042 		if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
11043 		     ((tcp_in_hpts(rack->rc_tp) == 0) &&
11044 		      ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
11045 			/*
11046 			 * If you are pacing output you don't want
11047 			 * to override.
11048 			 */
11049 			rack->r_early = 0;
11050 			rack->r_ctl.rc_agg_early = 0;
11051 			rack->r_timer_override = 1;
11052 		}
11053 	}
11054 }
11055 
11056 static void
rack_strike_dupack(struct tcp_rack * rack,tcp_seq th_ack)11057 rack_strike_dupack(struct tcp_rack *rack, tcp_seq th_ack)
11058 {
11059 	struct rack_sendmap *rsm;
11060 
11061 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11062 	while (rsm) {
11063 		/*
11064 		 * We need to skip anything already set
11065 		 * to be retransmitted.
11066 		 */
11067 		if ((rsm->r_dupack >= DUP_ACK_THRESHOLD)  ||
11068 		    (rsm->r_flags & RACK_MUST_RXT)) {
11069 			rsm = TAILQ_NEXT(rsm, r_tnext);
11070 			continue;
11071 		}
11072 		break;
11073 	}
11074 	if (rsm && (rsm->r_dupack < 0xff)) {
11075 		rsm->r_dupack++;
11076 		if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
11077 			struct timeval tv;
11078 			uint32_t cts;
11079 			/*
11080 			 * Here we see if we need to retransmit. For
11081 			 * a SACK type connection if enough time has passed
11082 			 * we will get a return of the rsm. For a non-sack
11083 			 * connection we will get the rsm returned if the
11084 			 * dupack value is 3 or more.
11085 			 */
11086 			cts = tcp_get_usecs(&tv);
11087 			rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
11088 			if (rack->r_ctl.rc_resend != NULL) {
11089 				if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
11090 					rack_cong_signal(rack->rc_tp, CC_NDUPACK,
11091 							 th_ack,  __LINE__);
11092 				}
11093 				rack->r_wanted_output = 1;
11094 				rack->r_timer_override = 1;
11095 				rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
11096 			}
11097 		} else {
11098 			rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
11099 		}
11100 	}
11101 }
11102 
11103 static void
rack_check_bottom_drag(struct tcpcb * tp,struct tcp_rack * rack,struct socket * so)11104 rack_check_bottom_drag(struct tcpcb *tp,
11105 		       struct tcp_rack *rack,
11106 		       struct socket *so)
11107 {
11108 	/*
11109 	 * So what is dragging bottom?
11110 	 *
11111 	 * Dragging bottom means you were under pacing and had a
11112 	 * delay in processing inbound acks waiting on our pacing
11113 	 * timer to expire. While you were waiting all of the acknowledgments
11114 	 * for the packets you sent have arrived. This means we are pacing
11115 	 * way underneath the bottleneck to the point where our Goodput
11116 	 * measurements stop working, since they require more than one
11117 	 * ack (usually at least 8 packets worth with multiple acks so we can
11118 	 * gauge the inter-ack times). If that occurs we have a real problem
11119 	 * since we are stuck in a hole that we can't get out of without
11120 	 * something speeding us up.
11121 	 *
11122 	 * We also check to see if we are widdling down to just one segment
11123 	 * outstanding. If this occurs and we have room to send in our cwnd/rwnd
11124 	 * then we are adding the delayed ack interval into our measurments and
11125 	 * we need to speed up slightly.
11126 	 */
11127 	uint32_t segsiz, minseg;
11128 
11129 	segsiz = ctf_fixed_maxseg(tp);
11130 	minseg = segsiz;
11131 	if (tp->snd_max == tp->snd_una) {
11132 		/*
11133 		 * We are doing dynamic pacing and we are way
11134 		 * under. Basically everything got acked while
11135 		 * we were still waiting on the pacer to expire.
11136 		 *
11137 		 * This means we need to boost the b/w in
11138 		 * addition to any earlier boosting of
11139 		 * the multiplier.
11140 		 */
11141 		uint64_t lt_bw;
11142 
11143 		tcp_trace_point(rack->rc_tp, TCP_TP_PACED_BOTTOM);
11144 		lt_bw = rack_get_lt_bw(rack);
11145 		rack->rc_dragged_bottom = 1;
11146 		rack_validate_multipliers_at_or_above100(rack);
11147 		if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
11148 		    (rack->dis_lt_bw == 0) &&
11149 		    (rack->use_lesser_lt_bw == 0) &&
11150 		    (lt_bw > 0)) {
11151 			/*
11152 			 * Lets use the long-term b/w we have
11153 			 * been getting as a base.
11154 			 */
11155 			if (rack->rc_gp_filled == 0) {
11156 				if (lt_bw > ONE_POINT_TWO_MEG) {
11157 					/*
11158 					 * If we have no measurement
11159 					 * don't let us set in more than
11160 					 * 1.2Mbps. If we are still too
11161 					 * low after pacing with this we
11162 					 * will hopefully have a max b/w
11163 					 * available to sanity check things.
11164 					 */
11165 					lt_bw = ONE_POINT_TWO_MEG;
11166 				}
11167 				rack->r_ctl.rc_rtt_diff = 0;
11168 				rack->r_ctl.gp_bw = lt_bw;
11169 				rack->rc_gp_filled = 1;
11170 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11171 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11172 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11173 			} else if (lt_bw > rack->r_ctl.gp_bw) {
11174 				rack->r_ctl.rc_rtt_diff = 0;
11175 				if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11176 					rack->r_ctl.num_measurements = RACK_REQ_AVG;
11177 				rack->r_ctl.gp_bw = lt_bw;
11178 				rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11179 			} else
11180 				rack_increase_bw_mul(rack, -1, 0, 0, 1);
11181 			if ((rack->gp_ready == 0) &&
11182 			    (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
11183 				/* We have enough measurements now */
11184 				rack->gp_ready = 1;
11185 				if (rack->dgp_on ||
11186 				    rack->rack_hibeta)
11187 					rack_set_cc_pacing(rack);
11188 				if (rack->defer_options)
11189 					rack_apply_deferred_options(rack);
11190 			}
11191 		} else {
11192 			/*
11193 			 * zero rtt possibly?, settle for just an old increase.
11194 			 */
11195 			rack_increase_bw_mul(rack, -1, 0, 0, 1);
11196 		}
11197 	} else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
11198 		   (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
11199 					       minseg)) &&
11200 		   (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11201 		   (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11202 		   (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
11203 		    (segsiz * rack_req_segs))) {
11204 		/*
11205 		 * We are doing dynamic GP pacing and
11206 		 * we have everything except 1MSS or less
11207 		 * bytes left out. We are still pacing away.
11208 		 * And there is data that could be sent, This
11209 		 * means we are inserting delayed ack time in
11210 		 * our measurements because we are pacing too slow.
11211 		 */
11212 		rack_validate_multipliers_at_or_above100(rack);
11213 		rack->rc_dragged_bottom = 1;
11214 		rack_increase_bw_mul(rack, -1, 0, 0, 1);
11215 	}
11216 }
11217 
11218 #ifdef TCP_REQUEST_TRK
11219 static void
rack_log_hybrid(struct tcp_rack * rack,uint32_t seq,struct tcp_sendfile_track * cur,uint8_t mod,int line,int err)11220 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq,
11221 		struct tcp_sendfile_track *cur, uint8_t mod, int line, int err)
11222 {
11223 	int do_log;
11224 
11225 	do_log = tcp_bblogging_on(rack->rc_tp);
11226 	if (do_log == 0) {
11227 		if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0)
11228 			return;
11229 		/* We only allow the three below with point logging on */
11230 		if ((mod != HYBRID_LOG_RULES_APP) &&
11231 		    (mod != HYBRID_LOG_RULES_SET) &&
11232 		    (mod != HYBRID_LOG_REQ_COMP))
11233 			return;
11234 
11235 	}
11236 	if (do_log) {
11237 		union tcp_log_stackspecific log;
11238 		struct timeval tv;
11239 
11240 		/* Convert our ms to a microsecond */
11241 		memset(&log, 0, sizeof(log));
11242 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11243 		log.u_bbr.flex1 = seq;
11244 		log.u_bbr.cwnd_gain = line;
11245 		if (cur != NULL) {
11246 			uint64_t off;
11247 
11248 			log.u_bbr.flex2 = cur->start_seq;
11249 			log.u_bbr.flex3 = cur->end_seq;
11250 			log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
11251 			log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff);
11252 			log.u_bbr.flex6 = cur->flags;
11253 			log.u_bbr.pkts_out = cur->hybrid_flags;
11254 			log.u_bbr.rttProp = cur->timestamp;
11255 			log.u_bbr.cur_del_rate = cur->cspr;
11256 			log.u_bbr.bw_inuse = cur->start;
11257 			log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff);
11258 			log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ;
11259 			log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff);
11260 			log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ;
11261 			log.u_bbr.inhpts = 1;
11262 #ifdef TCP_REQUEST_TRK
11263 			off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
11264 			log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
11265 #endif
11266 		} else {
11267 			log.u_bbr.flex2 = err;
11268 		}
11269 		/*
11270 		 * Fill in flex7 to be CHD (catchup|hybrid|DGP)
11271 		 */
11272 		log.u_bbr.flex7 = rack->rc_catch_up;
11273 		log.u_bbr.flex7 <<= 1;
11274 		log.u_bbr.flex7 |= rack->rc_hybrid_mode;
11275 		log.u_bbr.flex7 <<= 1;
11276 		log.u_bbr.flex7 |= rack->dgp_on;
11277 		/*
11278 		 * Compose bbr_state to be a bit wise 0000ADHF
11279 		 * where A is the always_pace flag
11280 		 * where D is the dgp_on flag
11281 		 * where H is the hybrid_mode on flag
11282 		 * where F is the use_fixed_rate flag.
11283 		 */
11284 		log.u_bbr.bbr_state = rack->rc_always_pace;
11285 		log.u_bbr.bbr_state <<= 1;
11286 		log.u_bbr.bbr_state |= rack->dgp_on;
11287 		log.u_bbr.bbr_state <<= 1;
11288 		log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
11289 		log.u_bbr.bbr_state <<= 1;
11290 		log.u_bbr.bbr_state |= rack->use_fixed_rate;
11291 		log.u_bbr.flex8 = mod;
11292 		log.u_bbr.delRate = rack->r_ctl.bw_rate_cap;
11293 		log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg;
11294 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11295 		log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start;
11296 		log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error;
11297 		log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop;
11298 		tcp_log_event(rack->rc_tp, NULL,
11299 		    &rack->rc_inp->inp_socket->so_rcv,
11300 		    &rack->rc_inp->inp_socket->so_snd,
11301 		    TCP_HYBRID_PACING_LOG, 0,
11302 	            0, &log, false, NULL, __func__, __LINE__, &tv);
11303 	}
11304 }
11305 #endif
11306 
11307 #ifdef TCP_REQUEST_TRK
11308 static void
rack_set_dgp_hybrid_mode(struct tcp_rack * rack,tcp_seq seq,uint32_t len,uint64_t cts)11309 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11310 {
11311 	struct tcp_sendfile_track *rc_cur, *orig_ent;
11312 	struct tcpcb *tp;
11313 	int err = 0;
11314 
11315 	orig_ent = rack->r_ctl.rc_last_sft;
11316 	rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq);
11317 	if (rc_cur == NULL) {
11318 		/* If not in the beginning what about the end piece */
11319 		if (rack->rc_hybrid_mode)
11320 			rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11321 		rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1));
11322 	} else {
11323 		err = 12345;
11324 	}
11325 	/* If we find no parameters we are in straight DGP mode */
11326 	if(rc_cur == NULL) {
11327 		/* None found for this seq, just DGP for now */
11328 		if (rack->rc_hybrid_mode) {
11329 			rack->r_ctl.client_suggested_maxseg = 0;
11330 			rack->rc_catch_up = 0;
11331 			if (rack->cspr_is_fcc == 0)
11332 				rack->r_ctl.bw_rate_cap = 0;
11333 			else
11334 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11335 		}
11336 		if (rack->rc_hybrid_mode) {
11337 			rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11338 		}
11339 		if (rack->r_ctl.rc_last_sft) {
11340 			rack->r_ctl.rc_last_sft = NULL;
11341 		}
11342 		return;
11343 	}
11344 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_WASSET) == 0) {
11345 		/* This entry was never setup for hybrid pacing on/off etc */
11346 		if (rack->rc_hybrid_mode) {
11347 			rack->r_ctl.client_suggested_maxseg = 0;
11348 			rack->rc_catch_up = 0;
11349 			rack->r_ctl.bw_rate_cap = 0;
11350 		}
11351 		if (rack->r_ctl.rc_last_sft) {
11352 			rack->r_ctl.rc_last_sft = NULL;
11353 		}
11354 		if ((rc_cur->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11355 			rc_cur->flags |= TCP_TRK_TRACK_FLG_FSND;
11356 			rc_cur->first_send = cts;
11357 			rc_cur->sent_at_fs = rack->rc_tp->t_sndbytes;
11358 			rc_cur->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11359 		}
11360 		return;
11361 	}
11362 	/*
11363 	 * Ok if we have a new entry *or* have never
11364 	 * set up an entry we need to proceed. If
11365 	 * we have already set it up this entry we
11366 	 * just continue along with what we already
11367 	 * setup.
11368 	 */
11369 	tp = rack->rc_tp;
11370 	if ((rack->r_ctl.rc_last_sft != NULL) &&
11371 	    (rack->r_ctl.rc_last_sft == rc_cur)) {
11372 		/* Its already in place */
11373 		if (rack->rc_hybrid_mode)
11374 			rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0);
11375 		return;
11376 	}
11377 	if (rack->rc_hybrid_mode == 0) {
11378 		rack->r_ctl.rc_last_sft = rc_cur;
11379 		if (orig_ent) {
11380 			orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
11381 			orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
11382 			orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
11383 		}
11384 		rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11385 		return;
11386 	}
11387 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){
11388 		/* Compensate for all the header overhead's */
11389 		if (rack->cspr_is_fcc == 0)
11390 			rack->r_ctl.bw_rate_cap	= rack_compensate_for_linerate(rack, rc_cur->cspr);
11391 		else
11392 			rack->r_ctl.fillcw_cap =  rack_compensate_for_linerate(rack, rc_cur->cspr);
11393 	} else {
11394 		if (rack->rc_hybrid_mode) {
11395 			if (rack->cspr_is_fcc == 0)
11396 				rack->r_ctl.bw_rate_cap = 0;
11397 			else
11398 				rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11399 		}
11400 	}
11401 	if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS)
11402 		rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg;
11403 	else
11404 		rack->r_ctl.client_suggested_maxseg = 0;
11405 	if (rc_cur->timestamp == rack->r_ctl.last_tm_mark) {
11406 		/*
11407 		 * It is the same timestamp as the previous one
11408 		 * add the hybrid flag that will indicate we use
11409 		 * sendtime not arrival time for catch-up mode.
11410 		 */
11411 		rc_cur->hybrid_flags |= TCP_HYBRID_PACING_SENDTIME;
11412 	}
11413 	if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) &&
11414 	    (rc_cur->cspr > 0)) {
11415 		uint64_t len;
11416 
11417 		rack->rc_catch_up = 1;
11418 		/*
11419 		 * Calculate the deadline time, first set the
11420 		 * time to when the request arrived.
11421 		 */
11422 		if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_SENDTIME) {
11423 			/*
11424 			 * For cases where its a duplicate tm (we received more
11425 			 * than one request for a tm) we want to use now, the point
11426 			 * where we are just sending the first bit of the request.
11427 			 */
11428 			rc_cur->deadline = cts;
11429 		} else {
11430 			/*
11431 			 * Here we have a different tm from the last request
11432 			 * so we want to use arrival time as our base.
11433 			 */
11434 			rc_cur->deadline = rc_cur->localtime;
11435 		}
11436 		/*
11437 		 * Next calculate the length and compensate for
11438 		 * TLS if need be.
11439 		 */
11440 		len = rc_cur->end - rc_cur->start;
11441 		if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) {
11442 			/*
11443 			 * This session is doing TLS. Take a swag guess
11444 			 * at the overhead.
11445 			 */
11446 			len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len);
11447 		}
11448 		/*
11449 		 * Now considering the size, and the cspr, what is the time that
11450 		 * would be required at the cspr rate. Here we use the raw
11451 		 * cspr value since the client only looks at the raw data. We
11452 		 * do use len which includes TLS overhead, but not the TCP/IP etc.
11453 		 * That will get made up for in the CU pacing rate set.
11454 		 */
11455 		len *= HPTS_USEC_IN_SEC;
11456 		len /= rc_cur->cspr;
11457 		rc_cur->deadline += len;
11458 	} else {
11459 		rack->rc_catch_up = 0;
11460 		rc_cur->deadline = 0;
11461 	}
11462 	if (rack->r_ctl.client_suggested_maxseg != 0) {
11463 		/*
11464 		 * We need to reset the max pace segs if we have a
11465 		 * client_suggested_maxseg.
11466 		 */
11467 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
11468 	}
11469 	if (orig_ent) {
11470 		orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes;
11471 		orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes;
11472 		orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND;
11473 	}
11474 	rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11475 	/* Remember it for next time and for CU mode */
11476 	rack->r_ctl.rc_last_sft = rc_cur;
11477 	rack->r_ctl.last_tm_mark = rc_cur->timestamp;
11478 }
11479 #endif
11480 
11481 static void
rack_chk_req_and_hybrid_on_out(struct tcp_rack * rack,tcp_seq seq,uint32_t len,uint64_t cts)11482 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11483 {
11484 #ifdef TCP_REQUEST_TRK
11485 	struct tcp_sendfile_track *ent;
11486 
11487 	ent = rack->r_ctl.rc_last_sft;
11488 	if ((ent == NULL) ||
11489 	    (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) ||
11490 	    (SEQ_GEQ(seq, ent->end_seq))) {
11491 		/* Time to update the track. */
11492 		rack_set_dgp_hybrid_mode(rack, seq, len, cts);
11493 		ent = rack->r_ctl.rc_last_sft;
11494 	}
11495 	/* Out of all */
11496 	if (ent == NULL) {
11497 		return;
11498 	}
11499 	if (SEQ_LT(ent->end_seq, (seq + len))) {
11500 		/*
11501 		 * This is the case where our end_seq guess
11502 		 * was wrong. This is usually due to TLS having
11503 		 * more bytes then our guess. It could also be the
11504 		 * case that the client sent in two requests closely
11505 		 * and the SB is full of both so we are sending part
11506 		 * of each (end|beg). In such a case lets move this
11507 		 * guys end to match the end of this send. That
11508 		 * way it will complete when all of it is acked.
11509 		 */
11510 		ent->end_seq = (seq + len);
11511 		if (rack->rc_hybrid_mode)
11512 			rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent, __LINE__);
11513 	}
11514 	/* Now validate we have set the send time of this one */
11515 	if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11516 		ent->flags |= TCP_TRK_TRACK_FLG_FSND;
11517 		ent->first_send = cts;
11518 		ent->sent_at_fs = rack->rc_tp->t_sndbytes;
11519 		ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11520 	}
11521 #endif
11522 }
11523 
11524 static void
rack_gain_for_fastoutput(struct tcp_rack * rack,struct tcpcb * tp,struct socket * so,uint32_t acked_amount)11525 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
11526 {
11527 	/*
11528 	 * The fast output path is enabled and we
11529 	 * have moved the cumack forward. Lets see if
11530 	 * we can expand forward the fast path length by
11531 	 * that amount. What we would ideally like to
11532 	 * do is increase the number of bytes in the
11533 	 * fast path block (left_to_send) by the
11534 	 * acked amount. However we have to gate that
11535 	 * by two factors:
11536 	 * 1) The amount outstanding and the rwnd of the peer
11537 	 *    (i.e. we don't want to exceed the rwnd of the peer).
11538 	 *    <and>
11539 	 * 2) The amount of data left in the socket buffer (i.e.
11540 	 *    we can't send beyond what is in the buffer).
11541 	 *
11542 	 * Note that this does not take into account any increase
11543 	 * in the cwnd. We will only extend the fast path by
11544 	 * what was acked.
11545 	 */
11546 	uint32_t new_total, gating_val;
11547 
11548 	new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
11549 	gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
11550 			 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
11551 	if (new_total <= gating_val) {
11552 		/* We can increase left_to_send by the acked amount */
11553 		counter_u64_add(rack_extended_rfo, 1);
11554 		rack->r_ctl.fsb.left_to_send = new_total;
11555 		KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
11556 			("rack:%p left_to_send:%u sbavail:%u out:%u",
11557 			 rack, rack->r_ctl.fsb.left_to_send,
11558 			 sbavail(&rack->rc_inp->inp_socket->so_snd),
11559 			 (tp->snd_max - tp->snd_una)));
11560 
11561 	}
11562 }
11563 
11564 static void
rack_adjust_sendmap_head(struct tcp_rack * rack,struct sockbuf * sb)11565 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb)
11566 {
11567 	/*
11568 	 * Here any sendmap entry that points to the
11569 	 * beginning mbuf must be adjusted to the correct
11570 	 * offset. This must be called with:
11571 	 * 1) The socket buffer locked
11572 	 * 2) snd_una adjusted to its new position.
11573 	 *
11574 	 * Note that (2) implies rack_ack_received has also
11575 	 * been called and all the sbcut's have been done.
11576 	 *
11577 	 * We grab the first mbuf in the socket buffer and
11578 	 * then go through the front of the sendmap, recalculating
11579 	 * the stored offset for any sendmap entry that has
11580 	 * that mbuf. We must use the sb functions to do this
11581 	 * since its possible an add was done has well as
11582 	 * the subtraction we may have just completed. This should
11583 	 * not be a penalty though, since we just referenced the sb
11584 	 * to go in and trim off the mbufs that we freed (of course
11585 	 * there will be a penalty for the sendmap references though).
11586 	 *
11587 	 * Note also with INVARIANT on, we validate with a KASSERT
11588 	 * that the first sendmap entry has a soff of 0.
11589 	 *
11590 	 */
11591 	struct mbuf *m;
11592 	struct rack_sendmap *rsm;
11593 	tcp_seq snd_una;
11594 #ifdef INVARIANTS
11595 	int first_processed = 0;
11596 #endif
11597 
11598 	snd_una = rack->rc_tp->snd_una;
11599 	SOCKBUF_LOCK_ASSERT(sb);
11600 	m = sb->sb_mb;
11601 	rsm = tqhash_min(rack->r_ctl.tqh);
11602 	if ((rsm == NULL) || (m == NULL)) {
11603 		/* Nothing outstanding */
11604 		return;
11605 	}
11606 	/* The very first RSM's mbuf must point to the head mbuf in the sb */
11607 	KASSERT((rsm->m == m),
11608 		("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb",
11609 		 rack, sb, rsm));
11610 	while (rsm->m && (rsm->m == m)) {
11611 		/* one to adjust */
11612 #ifdef INVARIANTS
11613 		struct mbuf *tm;
11614 		uint32_t soff;
11615 
11616 		tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
11617 		if ((rsm->orig_m_len != m->m_len) ||
11618 		    (rsm->orig_t_space != M_TRAILINGROOM(m))){
11619 			rack_adjust_orig_mlen(rsm);
11620 		}
11621 		if (first_processed == 0) {
11622 			KASSERT((rsm->soff == 0),
11623 				("Rack:%p rsm:%p -- rsm at head but soff not zero",
11624 				 rack, rsm));
11625 			first_processed = 1;
11626 		}
11627 		if ((rsm->soff != soff) || (rsm->m != tm)) {
11628 			/*
11629 			 * This is not a fatal error, we anticipate it
11630 			 * might happen (the else code), so we count it here
11631 			 * so that under invariant we can see that it really
11632 			 * does happen.
11633 			 */
11634 			counter_u64_add(rack_adjust_map_bw, 1);
11635 		}
11636 		rsm->m = tm;
11637 		rsm->soff = soff;
11638 		if (tm) {
11639 			rsm->orig_m_len = rsm->m->m_len;
11640 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11641 		} else {
11642 			rsm->orig_m_len = 0;
11643 			rsm->orig_t_space = 0;
11644 		}
11645 #else
11646 		rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
11647 		if (rsm->m) {
11648 			rsm->orig_m_len = rsm->m->m_len;
11649 			rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11650 		} else {
11651 			rsm->orig_m_len = 0;
11652 			rsm->orig_t_space = 0;
11653 		}
11654 #endif
11655 		rsm = tqhash_next(rack->r_ctl.tqh, rsm);
11656 		if (rsm == NULL)
11657 			break;
11658 	}
11659 }
11660 
11661 #ifdef TCP_REQUEST_TRK
11662 static inline void
rack_req_check_for_comp(struct tcp_rack * rack,tcp_seq th_ack)11663 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack)
11664 {
11665 	struct tcp_sendfile_track *ent;
11666 	int i;
11667 
11668 	if ((rack->rc_hybrid_mode == 0) &&
11669 	    (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) {
11670 		/*
11671 		 * Just do normal completions hybrid pacing is not on
11672 		 * and CLDL is off as well.
11673 		 */
11674 		tcp_req_check_for_comp(rack->rc_tp, th_ack);
11675 		return;
11676 	}
11677 	/*
11678 	 * Originally I was just going to find the th_ack associated
11679 	 * with an entry. But then I realized a large strech ack could
11680 	 * in theory ack two or more requests at once. So instead we
11681 	 * need to find all entries that are completed by th_ack not
11682 	 * just a single entry and do our logging.
11683 	 */
11684 	ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11685 	while (ent != NULL) {
11686 		/*
11687 		 * We may be doing hybrid pacing or CLDL and need more details possibly
11688 		 * so we do it manually instead of calling
11689 		 * tcp_req_check_for_comp()
11690 		 */
11691 		uint64_t laa, tim, data, cbw, ftim;
11692 
11693 		/* Ok this ack frees it */
11694 		rack_log_hybrid(rack, th_ack,
11695 				ent, HYBRID_LOG_REQ_COMP, __LINE__, 0);
11696 		rack_log_hybrid_sends(rack, ent, __LINE__);
11697 		/* calculate the time based on the ack arrival */
11698 		data = ent->end - ent->start;
11699 		laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
11700 		if (ent->flags & TCP_TRK_TRACK_FLG_FSND) {
11701 			if (ent->first_send > ent->localtime)
11702 				ftim = ent->first_send;
11703 			else
11704 				ftim = ent->localtime;
11705 		} else {
11706 			/* TSNH */
11707 			ftim = ent->localtime;
11708 		}
11709 		if (laa > ent->localtime)
11710 			tim = laa - ftim;
11711 		else
11712 			tim = 0;
11713 		cbw = data * HPTS_USEC_IN_SEC;
11714 		if (tim > 0)
11715 			cbw /= tim;
11716 		else
11717 			cbw = 0;
11718 		rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent, __LINE__);
11719 		/*
11720 		 * Check to see if we are freeing what we are pointing to send wise
11721 		 * if so be sure to NULL the pointer so we know we are no longer
11722 		 * set to anything.
11723 		 */
11724 		if (ent == rack->r_ctl.rc_last_sft) {
11725 			rack->r_ctl.rc_last_sft = NULL;
11726 			if (rack->rc_hybrid_mode) {
11727 				rack->rc_catch_up = 0;
11728 				if (rack->cspr_is_fcc == 0)
11729 					rack->r_ctl.bw_rate_cap = 0;
11730 				else
11731 					rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
11732 				rack->r_ctl.client_suggested_maxseg = 0;
11733 			}
11734 		}
11735 		/* Generate the log that the tcp_netflix call would have */
11736 		tcp_req_log_req_info(rack->rc_tp, ent,
11737 				      i, TCP_TRK_REQ_LOG_FREED, 0, 0);
11738 		/* Free it and see if there is another one */
11739 		tcp_req_free_a_slot(rack->rc_tp, ent);
11740 		ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11741 	}
11742 }
11743 #endif
11744 
11745 
11746 /*
11747  * Return value of 1, we do not need to call rack_process_data().
11748  * return value of 0, rack_process_data can be called.
11749  * For ret_val if its 0 the TCP is locked, if its non-zero
11750  * its unlocked and probably unsafe to touch the TCB.
11751  */
11752 static int
rack_process_ack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,uint32_t tiwin,int32_t tlen,int32_t * ofia,int32_t thflags,int32_t * ret_val,int32_t orig_tlen)11753 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
11754     struct tcpcb *tp, struct tcpopt *to,
11755     uint32_t tiwin, int32_t tlen,
11756     int32_t * ofia, int32_t thflags, int32_t *ret_val, int32_t orig_tlen)
11757 {
11758 	int32_t ourfinisacked = 0;
11759 	int32_t nsegs, acked_amount;
11760 	int32_t acked;
11761 	struct mbuf *mfree;
11762 	struct tcp_rack *rack;
11763 	int32_t under_pacing = 0;
11764 	int32_t post_recovery = 0;
11765 	uint32_t p_cwnd;
11766 
11767 	INP_WLOCK_ASSERT(tptoinpcb(tp));
11768 
11769 	rack = (struct tcp_rack *)tp->t_fb_ptr;
11770 	if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) {
11771 		/* Checking SEG.ACK against ISS is definitely redundant. */
11772 		tp->t_flags2 |= TF2_NO_ISS_CHECK;
11773 	}
11774 	if (!V_tcp_insecure_ack) {
11775 		tcp_seq seq_min;
11776 		bool ghost_ack_check;
11777 
11778 		if (tp->t_flags2 & TF2_NO_ISS_CHECK) {
11779 			/* Check for too old ACKs (RFC 5961, Section 5.2). */
11780 			seq_min = tp->snd_una - tp->max_sndwnd;
11781 			ghost_ack_check = false;
11782 		} else {
11783 			if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) {
11784 				/* Checking for ghost ACKs is stricter. */
11785 				seq_min = tp->iss + 1;
11786 				ghost_ack_check = true;
11787 			} else {
11788 				/*
11789 				 * Checking for too old ACKs (RFC 5961,
11790 				 * Section 5.2) is stricter.
11791 				 */
11792 				seq_min = tp->snd_una - tp->max_sndwnd;
11793 				ghost_ack_check = false;
11794 			}
11795 		}
11796 		if (SEQ_LT(th->th_ack, seq_min)) {
11797 			if (ghost_ack_check)
11798 				TCPSTAT_INC(tcps_rcvghostack);
11799 			else
11800 				TCPSTAT_INC(tcps_rcvacktooold);
11801 			/* Send challenge ACK. */
11802 			ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
11803 			rack->r_wanted_output = 1;
11804 			return (1);
11805 		}
11806 	}
11807 	if (SEQ_GT(th->th_ack, tp->snd_max)) {
11808 		ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
11809 		rack->r_wanted_output = 1;
11810 		return (1);
11811 	}
11812 	if (rack->gp_ready &&
11813 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11814 		under_pacing = 1;
11815 	}
11816 	if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
11817 		int in_rec, dup_ack_struck = 0;
11818 		int dsack_seen = 0, sacks_seen = 0;
11819 
11820 		in_rec = IN_FASTRECOVERY(tp->t_flags);
11821 		if (rack->rc_in_persist) {
11822 			tp->t_rxtshift = 0;
11823 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11824 				      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11825 		}
11826 
11827 		if ((th->th_ack == tp->snd_una) &&
11828 		    (tiwin == tp->snd_wnd) &&
11829 		    (orig_tlen == 0) &&
11830 		    ((to->to_flags & TOF_SACK) == 0)) {
11831 			rack_strike_dupack(rack, th->th_ack);
11832 			dup_ack_struck = 1;
11833 		}
11834 		rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)),
11835 			     dup_ack_struck, &dsack_seen, &sacks_seen);
11836 
11837 	}
11838 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
11839 		/*
11840 		 * Old ack, behind (or duplicate to) the last one rcv'd
11841 		 * Note: We mark reordering is occuring if its
11842 		 * less than and we have not closed our window.
11843 		 */
11844 		if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
11845 			rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
11846 			if (rack->r_ctl.rc_reorder_ts == 0)
11847 				rack->r_ctl.rc_reorder_ts = 1;
11848 		}
11849 		return (0);
11850 	}
11851 	/*
11852 	 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
11853 	 * something we sent.
11854 	 */
11855 	if (tp->t_flags & TF_NEEDSYN) {
11856 		/*
11857 		 * T/TCP: Connection was half-synchronized, and our SYN has
11858 		 * been ACK'd (so connection is now fully synchronized).  Go
11859 		 * to non-starred state, increment snd_una for ACK of SYN,
11860 		 * and check if we can do window scaling.
11861 		 */
11862 		tp->t_flags &= ~TF_NEEDSYN;
11863 		tp->snd_una++;
11864 		/* Do window scaling? */
11865 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11866 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11867 			tp->rcv_scale = tp->request_r_scale;
11868 			/* Send window already scaled. */
11869 		}
11870 	}
11871 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
11872 
11873 	acked = BYTES_THIS_ACK(tp, th);
11874 	if (acked) {
11875 		/*
11876 		 * Any time we move the cum-ack forward clear
11877 		 * keep-alive tied probe-not-answered. The
11878 		 * persists clears its own on entry.
11879 		 */
11880 		rack->probe_not_answered = 0;
11881 	}
11882 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11883 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11884 	/*
11885 	 * If we just performed our first retransmit, and the ACK arrives
11886 	 * within our recovery window, then it was a mistake to do the
11887 	 * retransmit in the first place.  Recover our original cwnd and
11888 	 * ssthresh, and proceed to transmit where we left off.
11889 	 */
11890 	if ((tp->t_flags & TF_PREVVALID) &&
11891 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
11892 		tp->t_flags &= ~TF_PREVVALID;
11893 		if (tp->t_rxtshift == 1 &&
11894 		    (int)(ticks - tp->t_badrxtwin) < 0)
11895 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
11896 	}
11897 	if (acked) {
11898 		/* assure we are not backed off */
11899 		tp->t_rxtshift = 0;
11900 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11901 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11902 		rack->rc_tlp_in_progress = 0;
11903 		rack->r_ctl.rc_tlp_cnt_out = 0;
11904 		/*
11905 		 * If it is the RXT timer we want to
11906 		 * stop it, so we can restart a TLP.
11907 		 */
11908 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
11909 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11910 #ifdef TCP_REQUEST_TRK
11911 		rack_req_check_for_comp(rack, th->th_ack);
11912 #endif
11913 	}
11914 	/*
11915 	 * If we have a timestamp reply, update smoothed round trip time. If
11916 	 * no timestamp is present but transmit timer is running and timed
11917 	 * sequence number was acked, update smoothed round trip time. Since
11918 	 * we now have an rtt measurement, cancel the timer backoff (cf.,
11919 	 * Phil Karn's retransmit alg.). Recompute the initial retransmit
11920 	 * timer.
11921 	 *
11922 	 * Some boxes send broken timestamp replies during the SYN+ACK
11923 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
11924 	 * and blow up the retransmit timer.
11925 	 */
11926 	/*
11927 	 * If all outstanding data is acked, stop retransmit timer and
11928 	 * remember to restart (more output or persist). If there is more
11929 	 * data to be acked, restart retransmit timer, using current
11930 	 * (possibly backed-off) value.
11931 	 */
11932 	if (acked == 0) {
11933 		if (ofia)
11934 			*ofia = ourfinisacked;
11935 		return (0);
11936 	}
11937 	if (IN_RECOVERY(tp->t_flags)) {
11938 		if (SEQ_LT(th->th_ack, tp->snd_recover) &&
11939 		    (SEQ_LT(th->th_ack, tp->snd_max))) {
11940 			tcp_rack_partialack(tp);
11941 		} else {
11942 			rack_post_recovery(tp, th->th_ack);
11943 			post_recovery = 1;
11944 			/*
11945 			 * Grab the segsiz, multiply by 2 and add the snd_cwnd
11946 			 * that is the max the CC should add if we are exiting
11947 			 * recovery and doing a late add.
11948 			 */
11949 			p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
11950 			p_cwnd <<= 1;
11951 			p_cwnd += tp->snd_cwnd;
11952 		}
11953 	} else if ((rack->rto_from_rec == 1) &&
11954 		   SEQ_GEQ(th->th_ack, tp->snd_recover)) {
11955 		/*
11956 		 * We were in recovery, hit a rxt timeout
11957 		 * and never re-entered recovery. The timeout(s)
11958 		 * made up all the lost data. In such a case
11959 		 * we need to clear the rto_from_rec flag.
11960 		 */
11961 		rack->rto_from_rec = 0;
11962 	}
11963 	/*
11964 	 * Let the congestion control algorithm update congestion control
11965 	 * related information. This typically means increasing the
11966 	 * congestion window.
11967 	 */
11968 	rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, post_recovery);
11969 	if (post_recovery &&
11970 	    (tp->snd_cwnd > p_cwnd)) {
11971 		/* Must be non-newreno (cubic) getting too ahead of itself */
11972 		tp->snd_cwnd = p_cwnd;
11973 	}
11974 	SOCK_SENDBUF_LOCK(so);
11975 	acked_amount = min(acked, (int)sbavail(&so->so_snd));
11976 	tp->snd_wnd -= acked_amount;
11977 	mfree = sbcut_locked(&so->so_snd, acked_amount);
11978 	if ((sbused(&so->so_snd) == 0) &&
11979 	    (acked > acked_amount) &&
11980 	    (tp->t_state >= TCPS_FIN_WAIT_1) &&
11981 	    (tp->t_flags & TF_SENTFIN)) {
11982 		/*
11983 		 * We must be sure our fin
11984 		 * was sent and acked (we can be
11985 		 * in FIN_WAIT_1 without having
11986 		 * sent the fin).
11987 		 */
11988 		ourfinisacked = 1;
11989 	}
11990 	tp->snd_una = th->th_ack;
11991 	/* wakeups? */
11992 	if (acked_amount && sbavail(&so->so_snd))
11993 		rack_adjust_sendmap_head(rack, &so->so_snd);
11994 	rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
11995 	/* NB: sowwakeup_locked() does an implicit unlock. */
11996 	sowwakeup_locked(so);
11997 	m_freem(mfree);
11998 	if (SEQ_GT(tp->snd_una, tp->snd_recover))
11999 		tp->snd_recover = tp->snd_una;
12000 
12001 	if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
12002 		tp->snd_nxt = tp->snd_max;
12003 	}
12004 	if (under_pacing &&
12005 	    (rack->use_fixed_rate == 0) &&
12006 	    (rack->in_probe_rtt == 0) &&
12007 	    rack->rc_gp_dyn_mul &&
12008 	    rack->rc_always_pace) {
12009 		/* Check if we are dragging bottom */
12010 		rack_check_bottom_drag(tp, rack, so);
12011 	}
12012 	if (tp->snd_una == tp->snd_max) {
12013 		/* Nothing left outstanding */
12014 		tp->t_flags &= ~TF_PREVVALID;
12015 		if (rack->r_ctl.rc_went_idle_time == 0)
12016 			rack->r_ctl.rc_went_idle_time = 1;
12017 		rack->r_ctl.retran_during_recovery = 0;
12018 		rack->r_ctl.dsack_byte_cnt = 0;
12019 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12020 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
12021 			tp->t_acktime = 0;
12022 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12023 		rack->rc_suspicious = 0;
12024 		/* Set need output so persist might get set */
12025 		rack->r_wanted_output = 1;
12026 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12027 		if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
12028 		    (sbavail(&so->so_snd) == 0) &&
12029 		    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
12030 			/*
12031 			 * The socket was gone and the
12032 			 * peer sent data (now or in the past), time to
12033 			 * reset him.
12034 			 */
12035 			*ret_val = 1;
12036 			/* tcp_close will kill the inp pre-log the Reset */
12037 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
12038 			tp = tcp_close(tp);
12039 			ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
12040 			return (1);
12041 		}
12042 	}
12043 	if (ofia)
12044 		*ofia = ourfinisacked;
12045 	return (0);
12046 }
12047 
12048 
12049 static void
rack_log_collapse(struct tcp_rack * rack,uint32_t cnt,uint32_t split,uint32_t out,int line,int dir,uint32_t flags,struct rack_sendmap * rsm)12050 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
12051 		  int dir, uint32_t flags, struct rack_sendmap *rsm)
12052 {
12053 	if (tcp_bblogging_on(rack->rc_tp)) {
12054 		union tcp_log_stackspecific log;
12055 		struct timeval tv;
12056 
12057 		memset(&log, 0, sizeof(log));
12058 		log.u_bbr.flex1 = cnt;
12059 		log.u_bbr.flex2 = split;
12060 		log.u_bbr.flex3 = out;
12061 		log.u_bbr.flex4 = line;
12062 		log.u_bbr.flex5 = rack->r_must_retran;
12063 		log.u_bbr.flex6 = flags;
12064 		log.u_bbr.flex7 = rack->rc_has_collapsed;
12065 		log.u_bbr.flex8 = dir;	/*
12066 					 * 1 is collapsed, 0 is uncollapsed,
12067 					 * 2 is log of a rsm being marked, 3 is a split.
12068 					 */
12069 		if (rsm == NULL)
12070 			log.u_bbr.rttProp = 0;
12071 		else
12072 			log.u_bbr.rttProp = (uintptr_t)rsm;
12073 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
12074 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
12075 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
12076 		    &rack->rc_inp->inp_socket->so_rcv,
12077 		    &rack->rc_inp->inp_socket->so_snd,
12078 		    TCP_RACK_LOG_COLLAPSE, 0,
12079 		    0, &log, false, &tv);
12080 	}
12081 }
12082 
12083 static void
rack_collapsed_window(struct tcp_rack * rack,uint32_t out,tcp_seq th_ack,int line)12084 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line)
12085 {
12086 	/*
12087 	 * Here all we do is mark the collapsed point and set the flag.
12088 	 * This may happen again and again, but there is no
12089 	 * sense splitting our map until we know where the
12090 	 * peer finally lands in the collapse.
12091 	 */
12092 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12093 	if ((rack->rc_has_collapsed == 0) ||
12094 	    (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd)))
12095 		counter_u64_add(rack_collapsed_win_seen, 1);
12096 	rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd;
12097 	rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
12098 	rack->rc_has_collapsed = 1;
12099 	rack->r_collapse_point_valid = 1;
12100 	rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
12101 }
12102 
12103 static void
rack_un_collapse_window(struct tcp_rack * rack,int line)12104 rack_un_collapse_window(struct tcp_rack *rack, int line)
12105 {
12106 	struct rack_sendmap *nrsm, *rsm;
12107 	int cnt = 0, split = 0;
12108 	int insret __diagused;
12109 
12110 
12111 	tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12112 	rack->rc_has_collapsed = 0;
12113 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
12114 	if (rsm == NULL) {
12115 		/* Nothing to do maybe the peer ack'ed it all */
12116 		rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12117 		return;
12118 	}
12119 	/* Now do we need to split this one? */
12120 	if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
12121 		rack_log_collapse(rack, rsm->r_start, rsm->r_end,
12122 				  rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
12123 		nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
12124 		if (nrsm == NULL) {
12125 			/* We can't get a rsm, mark all? */
12126 			nrsm = rsm;
12127 			goto no_split;
12128 		}
12129 		/* Clone it */
12130 		split = 1;
12131 		rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
12132 #ifndef INVARIANTS
12133 		(void)tqhash_insert(rack->r_ctl.tqh, nrsm);
12134 #else
12135 		if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
12136 			panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p",
12137 			      nrsm, insret, rack, rsm);
12138 		}
12139 #endif
12140 		rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
12141 				 rack->r_ctl.last_collapse_point, __LINE__);
12142 		if (rsm->r_in_tmap) {
12143 			TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
12144 			nrsm->r_in_tmap = 1;
12145 		}
12146 		/*
12147 		 * Set in the new RSM as the
12148 		 * collapsed starting point
12149 		 */
12150 		rsm = nrsm;
12151 	}
12152 
12153 no_split:
12154 	TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm)  {
12155 		cnt++;
12156 		nrsm->r_flags |= RACK_RWND_COLLAPSED;
12157 		rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
12158 		cnt++;
12159 	}
12160 	if (cnt) {
12161 		counter_u64_add(rack_collapsed_win, 1);
12162 	}
12163 	rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12164 }
12165 
12166 static void
rack_handle_delayed_ack(struct tcpcb * tp,struct tcp_rack * rack,int32_t tlen,int32_t tfo_syn)12167 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
12168 			int32_t tlen, int32_t tfo_syn)
12169 {
12170 	if (DELAY_ACK(tp, tlen) || tfo_syn) {
12171 		rack_timer_cancel(tp, rack,
12172 				  rack->r_ctl.rc_rcvtime, __LINE__);
12173 		tp->t_flags |= TF_DELACK;
12174 	} else {
12175 		rack->r_wanted_output = 1;
12176 		tp->t_flags |= TF_ACKNOW;
12177 	}
12178 }
12179 
12180 static void
rack_validate_fo_sendwin_up(struct tcpcb * tp,struct tcp_rack * rack)12181 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
12182 {
12183 	/*
12184 	 * If fast output is in progress, lets validate that
12185 	 * the new window did not shrink on us and make it
12186 	 * so fast output should end.
12187 	 */
12188 	if (rack->r_fast_output) {
12189 		uint32_t out;
12190 
12191 		/*
12192 		 * Calculate what we will send if left as is
12193 		 * and compare that to our send window.
12194 		 */
12195 		out = ctf_outstanding(tp);
12196 		if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
12197 			/* ok we have an issue */
12198 			if (out >= tp->snd_wnd) {
12199 				/* Turn off fast output the window is met or collapsed */
12200 				rack->r_fast_output = 0;
12201 			} else {
12202 				/* we have some room left */
12203 				rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
12204 				if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
12205 					/* If not at least 1 full segment never mind */
12206 					rack->r_fast_output = 0;
12207 				}
12208 			}
12209 		}
12210 	}
12211 }
12212 
12213 /*
12214  * Return value of 1, the TCB is unlocked and most
12215  * likely gone, return value of 0, the TCP is still
12216  * locked.
12217  */
12218 static int
rack_process_data(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt)12219 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
12220     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
12221     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
12222 {
12223 	/*
12224 	 * Update window information. Don't look at window if no ACK: TAC's
12225 	 * send garbage on first SYN.
12226 	 */
12227 	int32_t nsegs;
12228 	int32_t tfo_syn;
12229 	struct tcp_rack *rack;
12230 
12231 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12232 
12233 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12234 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12235 	if ((thflags & TH_ACK) &&
12236 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
12237 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
12238 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
12239 		/* keep track of pure window updates */
12240 		if (tlen == 0 &&
12241 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
12242 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
12243 		tp->snd_wnd = tiwin;
12244 		rack_validate_fo_sendwin_up(tp, rack);
12245 		tp->snd_wl1 = th->th_seq;
12246 		tp->snd_wl2 = th->th_ack;
12247 		if (tp->snd_wnd > tp->max_sndwnd)
12248 			tp->max_sndwnd = tp->snd_wnd;
12249 		rack->r_wanted_output = 1;
12250 	} else if (thflags & TH_ACK) {
12251 		if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
12252 			tp->snd_wnd = tiwin;
12253 			rack_validate_fo_sendwin_up(tp, rack);
12254 			tp->snd_wl1 = th->th_seq;
12255 			tp->snd_wl2 = th->th_ack;
12256 		}
12257 	}
12258 	if (tp->snd_wnd < ctf_outstanding(tp))
12259 		/* The peer collapsed the window */
12260 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12261 	else if (rack->rc_has_collapsed)
12262 		rack_un_collapse_window(rack, __LINE__);
12263 	if ((rack->r_collapse_point_valid) &&
12264 	    (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
12265 		rack->r_collapse_point_valid = 0;
12266 	/* Was persist timer active and now we have window space? */
12267 	if ((rack->rc_in_persist != 0) &&
12268 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12269 				rack->r_ctl.rc_pace_min_segs))) {
12270 		rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
12271 		tp->snd_nxt = tp->snd_max;
12272 		/* Make sure we output to start the timer */
12273 		rack->r_wanted_output = 1;
12274 	}
12275 	/* Do we enter persists? */
12276 	if ((rack->rc_in_persist == 0) &&
12277 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12278 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
12279 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12280 	    sbavail(&tptosocket(tp)->so_snd) &&
12281 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12282 		/*
12283 		 * Here the rwnd is less than
12284 		 * the pacing size, we are established,
12285 		 * nothing is outstanding, and there is
12286 		 * data to send. Enter persists.
12287 		 */
12288 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
12289 	}
12290 	if (tp->t_flags2 & TF2_DROP_AF_DATA) {
12291 		m_freem(m);
12292 		return (0);
12293 	}
12294 	/*
12295 	 * don't process the URG bit, ignore them drag
12296 	 * along the up.
12297 	 */
12298 	tp->rcv_up = tp->rcv_nxt;
12299 
12300 	/*
12301 	 * Process the segment text, merging it into the TCP sequencing
12302 	 * queue, and arranging for acknowledgment of receipt if necessary.
12303 	 * This process logically involves adjusting tp->rcv_wnd as data is
12304 	 * presented to the user (this happens in tcp_usrreq.c, case
12305 	 * PRU_RCVD).  If a FIN has already been received on this connection
12306 	 * then we just ignore the text.
12307 	 */
12308 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
12309 	    (tp->t_flags & TF_FASTOPEN));
12310 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
12311 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12312 		tcp_seq save_start = th->th_seq;
12313 		tcp_seq save_rnxt  = tp->rcv_nxt;
12314 		int     save_tlen  = tlen;
12315 
12316 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12317 		/*
12318 		 * Insert segment which includes th into TCP reassembly
12319 		 * queue with control block tp.  Set thflags to whether
12320 		 * reassembly now includes a segment with FIN.  This handles
12321 		 * the common case inline (segment is the next to be
12322 		 * received on an established connection, and the queue is
12323 		 * empty), avoiding linkage into and removal from the queue
12324 		 * and repetition of various conversions. Set DELACK for
12325 		 * segments received in order, but ack immediately when
12326 		 * segments are out of order (so fast retransmit can work).
12327 		 */
12328 		if (th->th_seq == tp->rcv_nxt &&
12329 		    SEGQ_EMPTY(tp) &&
12330 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
12331 		    tfo_syn)) {
12332 #ifdef NETFLIX_SB_LIMITS
12333 			u_int mcnt, appended;
12334 
12335 			if (so->so_rcv.sb_shlim) {
12336 				mcnt = m_memcnt(m);
12337 				appended = 0;
12338 				if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12339 				    CFO_NOSLEEP, NULL) == false) {
12340 					counter_u64_add(tcp_sb_shlim_fails, 1);
12341 					m_freem(m);
12342 					return (0);
12343 				}
12344 			}
12345 #endif
12346 			rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
12347 			tp->rcv_nxt += tlen;
12348 			if (tlen &&
12349 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12350 			    (tp->t_fbyte_in == 0)) {
12351 				tp->t_fbyte_in = ticks;
12352 				if (tp->t_fbyte_in == 0)
12353 					tp->t_fbyte_in = 1;
12354 				if (tp->t_fbyte_out && tp->t_fbyte_in)
12355 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12356 			}
12357 			thflags = tcp_get_flags(th) & TH_FIN;
12358 			KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12359 			KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12360 			SOCK_RECVBUF_LOCK(so);
12361 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12362 				m_freem(m);
12363 			} else {
12364 				int32_t newsize;
12365 
12366 				if (tlen > 0) {
12367 					newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12368 					if (newsize)
12369 						if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12370 							so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12371 				}
12372 #ifdef NETFLIX_SB_LIMITS
12373 				appended =
12374 #endif
12375 					sbappendstream_locked(&so->so_rcv, m, 0);
12376 			}
12377 			rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12378 			/* NB: sorwakeup_locked() does an implicit unlock. */
12379 			sorwakeup_locked(so);
12380 #ifdef NETFLIX_SB_LIMITS
12381 			if (so->so_rcv.sb_shlim && appended != mcnt)
12382 				counter_fo_release(so->so_rcv.sb_shlim,
12383 				    mcnt - appended);
12384 #endif
12385 		} else {
12386 			/*
12387 			 * XXX: Due to the header drop above "th" is
12388 			 * theoretically invalid by now.  Fortunately
12389 			 * m_adj() doesn't actually frees any mbufs when
12390 			 * trimming from the head.
12391 			 */
12392 			tcp_seq temp = save_start;
12393 
12394 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
12395 			tp->t_flags |= TF_ACKNOW;
12396 			if (tp->t_flags & TF_WAKESOR) {
12397 				tp->t_flags &= ~TF_WAKESOR;
12398 				/* NB: sorwakeup_locked() does an implicit unlock. */
12399 				sorwakeup_locked(so);
12400 			}
12401 		}
12402 		if ((tp->t_flags & TF_SACK_PERMIT) &&
12403 		    (save_tlen > 0) &&
12404 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
12405 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
12406 				/*
12407 				 * DSACK actually handled in the fastpath
12408 				 * above.
12409 				 */
12410 				tcp_update_sack_list(tp, save_start,
12411 				    save_start + save_tlen);
12412 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
12413 				if ((tp->rcv_numsacks >= 1) &&
12414 				    (tp->sackblks[0].end == save_start)) {
12415 					/*
12416 					 * Partial overlap, recorded at todrop
12417 					 * above.
12418 					 */
12419 					tcp_update_sack_list(tp,
12420 					    tp->sackblks[0].start,
12421 					    tp->sackblks[0].end);
12422 				} else {
12423 					tcp_update_dsack_list(tp, save_start,
12424 					    save_start + save_tlen);
12425 				}
12426 			} else if (tlen >= save_tlen) {
12427 				/* Update of sackblks. */
12428 				tcp_update_dsack_list(tp, save_start,
12429 				    save_start + save_tlen);
12430 			} else if (tlen > 0) {
12431 				tcp_update_dsack_list(tp, save_start,
12432 				    save_start + tlen);
12433 			}
12434 		}
12435 	} else {
12436 		m_freem(m);
12437 		thflags &= ~TH_FIN;
12438 	}
12439 
12440 	/*
12441 	 * If FIN is received ACK the FIN and let the user know that the
12442 	 * connection is closing.
12443 	 */
12444 	if (thflags & TH_FIN) {
12445 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12446 			/* The socket upcall is handled by socantrcvmore. */
12447 			socantrcvmore(so);
12448 			/*
12449 			 * If connection is half-synchronized (ie NEEDSYN
12450 			 * flag on) then delay ACK, so it may be piggybacked
12451 			 * when SYN is sent. Otherwise, since we received a
12452 			 * FIN then no more input can be expected, send ACK
12453 			 * now.
12454 			 */
12455 			if (tp->t_flags & TF_NEEDSYN) {
12456 				rack_timer_cancel(tp, rack,
12457 				    rack->r_ctl.rc_rcvtime, __LINE__);
12458 				tp->t_flags |= TF_DELACK;
12459 			} else {
12460 				tp->t_flags |= TF_ACKNOW;
12461 			}
12462 			tp->rcv_nxt++;
12463 		}
12464 		switch (tp->t_state) {
12465 			/*
12466 			 * In SYN_RECEIVED and ESTABLISHED STATES enter the
12467 			 * CLOSE_WAIT state.
12468 			 */
12469 		case TCPS_SYN_RECEIVED:
12470 			tp->t_starttime = ticks;
12471 			/* FALLTHROUGH */
12472 		case TCPS_ESTABLISHED:
12473 			rack_timer_cancel(tp, rack,
12474 			    rack->r_ctl.rc_rcvtime, __LINE__);
12475 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
12476 			break;
12477 
12478 			/*
12479 			 * If still in FIN_WAIT_1 STATE FIN has not been
12480 			 * acked so enter the CLOSING state.
12481 			 */
12482 		case TCPS_FIN_WAIT_1:
12483 			rack_timer_cancel(tp, rack,
12484 			    rack->r_ctl.rc_rcvtime, __LINE__);
12485 			tcp_state_change(tp, TCPS_CLOSING);
12486 			break;
12487 
12488 			/*
12489 			 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12490 			 * starting the time-wait timer, turning off the
12491 			 * other standard timers.
12492 			 */
12493 		case TCPS_FIN_WAIT_2:
12494 			rack_timer_cancel(tp, rack,
12495 			    rack->r_ctl.rc_rcvtime, __LINE__);
12496 			tcp_twstart(tp);
12497 			return (1);
12498 		}
12499 	}
12500 	/*
12501 	 * Return any desired output.
12502 	 */
12503 	if ((tp->t_flags & TF_ACKNOW) ||
12504 	    (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
12505 		rack->r_wanted_output = 1;
12506 	}
12507 	return (0);
12508 }
12509 
12510 /*
12511  * Here nothing is really faster, its just that we
12512  * have broken out the fast-data path also just like
12513  * the fast-ack.
12514  */
12515 static int
rack_do_fastnewdata(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt,uint8_t iptos)12516 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
12517     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12518     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
12519 {
12520 	int32_t nsegs;
12521 	int32_t newsize = 0;	/* automatic sockbuf scaling */
12522 	struct tcp_rack *rack;
12523 #ifdef NETFLIX_SB_LIMITS
12524 	u_int mcnt, appended;
12525 #endif
12526 
12527 	/*
12528 	 * If last ACK falls within this segment's sequence numbers, record
12529 	 * the timestamp. NOTE that the test is modified according to the
12530 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12531 	 */
12532 	if (__predict_false(th->th_seq != tp->rcv_nxt)) {
12533 		return (0);
12534 	}
12535 	if (tiwin && tiwin != tp->snd_wnd) {
12536 		return (0);
12537 	}
12538 	if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
12539 		return (0);
12540 	}
12541 	if (__predict_false((to->to_flags & TOF_TS) &&
12542 	    (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
12543 		return (0);
12544 	}
12545 	if (__predict_false((th->th_ack != tp->snd_una))) {
12546 		return (0);
12547 	}
12548 	if (__predict_false(tlen > sbspace(&so->so_rcv))) {
12549 		return (0);
12550 	}
12551 	if ((to->to_flags & TOF_TS) != 0 &&
12552 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12553 		tp->ts_recent_age = tcp_ts_getticks();
12554 		tp->ts_recent = to->to_tsval;
12555 	}
12556 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12557 	/*
12558 	 * This is a pure, in-sequence data packet with nothing on the
12559 	 * reassembly queue and we have enough buffer space to take it.
12560 	 */
12561 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12562 
12563 #ifdef NETFLIX_SB_LIMITS
12564 	if (so->so_rcv.sb_shlim) {
12565 		mcnt = m_memcnt(m);
12566 		appended = 0;
12567 		if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12568 		    CFO_NOSLEEP, NULL) == false) {
12569 			counter_u64_add(tcp_sb_shlim_fails, 1);
12570 			m_freem(m);
12571 			return (1);
12572 		}
12573 	}
12574 #endif
12575 	/* Clean receiver SACK report if present */
12576 	if (tp->rcv_numsacks)
12577 		tcp_clean_sackreport(tp);
12578 	KMOD_TCPSTAT_INC(tcps_preddat);
12579 	tp->rcv_nxt += tlen;
12580 	if (tlen &&
12581 	    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12582 	    (tp->t_fbyte_in == 0)) {
12583 		tp->t_fbyte_in = ticks;
12584 		if (tp->t_fbyte_in == 0)
12585 			tp->t_fbyte_in = 1;
12586 		if (tp->t_fbyte_out && tp->t_fbyte_in)
12587 			tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12588 	}
12589 	/*
12590 	 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
12591 	 */
12592 	tp->snd_wl1 = th->th_seq;
12593 	/*
12594 	 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
12595 	 */
12596 	tp->rcv_up = tp->rcv_nxt;
12597 	KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12598 	KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12599 	newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12600 
12601 	/* Add data to socket buffer. */
12602 	SOCK_RECVBUF_LOCK(so);
12603 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12604 		m_freem(m);
12605 	} else {
12606 		/*
12607 		 * Set new socket buffer size. Give up when limit is
12608 		 * reached.
12609 		 */
12610 		if (newsize)
12611 			if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12612 				so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12613 		m_adj(m, drop_hdrlen);	/* delayed header drop */
12614 #ifdef NETFLIX_SB_LIMITS
12615 		appended =
12616 #endif
12617 			sbappendstream_locked(&so->so_rcv, m, 0);
12618 		ctf_calc_rwin(so, tp);
12619 	}
12620 	rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12621 	/* NB: sorwakeup_locked() does an implicit unlock. */
12622 	sorwakeup_locked(so);
12623 #ifdef NETFLIX_SB_LIMITS
12624 	if (so->so_rcv.sb_shlim && mcnt != appended)
12625 		counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
12626 #endif
12627 	rack_handle_delayed_ack(tp, rack, tlen, 0);
12628 	if (tp->snd_una == tp->snd_max)
12629 		sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12630 	return (1);
12631 }
12632 
12633 /*
12634  * This subfunction is used to try to highly optimize the
12635  * fast path. We again allow window updates that are
12636  * in sequence to remain in the fast-path. We also add
12637  * in the __predict's to attempt to help the compiler.
12638  * Note that if we return a 0, then we can *not* process
12639  * it and the caller should push the packet into the
12640  * slow-path.
12641  */
12642 static int
rack_fastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt,uint32_t cts)12643 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12644     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12645     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
12646 {
12647 	int32_t acked;
12648 	int32_t nsegs;
12649 	int32_t under_pacing = 0;
12650 	struct tcp_rack *rack;
12651 
12652 	if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12653 		/* Old ack, behind (or duplicate to) the last one rcv'd */
12654 		return (0);
12655 	}
12656 	if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
12657 		/* Above what we have sent? */
12658 		return (0);
12659 	}
12660 	if (__predict_false(tiwin == 0)) {
12661 		/* zero window */
12662 		return (0);
12663 	}
12664 	if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
12665 		/* We need a SYN or a FIN, unlikely.. */
12666 		return (0);
12667 	}
12668 	if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
12669 		/* Timestamp is behind .. old ack with seq wrap? */
12670 		return (0);
12671 	}
12672 	if (__predict_false(IN_RECOVERY(tp->t_flags))) {
12673 		/* Still recovering */
12674 		return (0);
12675 	}
12676 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12677 	if (rack->r_ctl.rc_sacked) {
12678 		/* We have sack holes on our scoreboard */
12679 		return (0);
12680 	}
12681 	/* Ok if we reach here, we can process a fast-ack */
12682 	if (rack->gp_ready &&
12683 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12684 		under_pacing = 1;
12685 	}
12686 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
12687 	rack_log_ack(tp, to, th, 0, 0, NULL, NULL);
12688 	/* Did the window get updated? */
12689 	if (tiwin != tp->snd_wnd) {
12690 		tp->snd_wnd = tiwin;
12691 		rack_validate_fo_sendwin_up(tp, rack);
12692 		tp->snd_wl1 = th->th_seq;
12693 		if (tp->snd_wnd > tp->max_sndwnd)
12694 			tp->max_sndwnd = tp->snd_wnd;
12695 	}
12696 	/* Do we exit persists? */
12697 	if ((rack->rc_in_persist != 0) &&
12698 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12699 			       rack->r_ctl.rc_pace_min_segs))) {
12700 		rack_exit_persist(tp, rack, cts);
12701 	}
12702 	/* Do we enter persists? */
12703 	if ((rack->rc_in_persist == 0) &&
12704 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12705 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
12706 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12707 	    sbavail(&tptosocket(tp)->so_snd) &&
12708 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12709 		/*
12710 		 * Here the rwnd is less than
12711 		 * the pacing size, we are established,
12712 		 * nothing is outstanding, and there is
12713 		 * data to send. Enter persists.
12714 		 */
12715 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack);
12716 	}
12717 	/*
12718 	 * If last ACK falls within this segment's sequence numbers, record
12719 	 * the timestamp. NOTE that the test is modified according to the
12720 	 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12721 	 */
12722 	if ((to->to_flags & TOF_TS) != 0 &&
12723 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12724 		tp->ts_recent_age = tcp_ts_getticks();
12725 		tp->ts_recent = to->to_tsval;
12726 	}
12727 	/*
12728 	 * This is a pure ack for outstanding data.
12729 	 */
12730 	KMOD_TCPSTAT_INC(tcps_predack);
12731 
12732 	/*
12733 	 * "bad retransmit" recovery.
12734 	 */
12735 	if ((tp->t_flags & TF_PREVVALID) &&
12736 	    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
12737 		tp->t_flags &= ~TF_PREVVALID;
12738 		if (tp->t_rxtshift == 1 &&
12739 		    (int)(ticks - tp->t_badrxtwin) < 0)
12740 			rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
12741 	}
12742 	/*
12743 	 * Recalculate the transmit timer / rtt.
12744 	 *
12745 	 * Some boxes send broken timestamp replies during the SYN+ACK
12746 	 * phase, ignore timestamps of 0 or we could calculate a huge RTT
12747 	 * and blow up the retransmit timer.
12748 	 */
12749 	acked = BYTES_THIS_ACK(tp, th);
12750 
12751 #ifdef TCP_HHOOK
12752 	/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
12753 	hhook_run_tcp_est_in(tp, th, to);
12754 #endif
12755 	KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
12756 	KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
12757 	if (acked) {
12758 		struct mbuf *mfree;
12759 
12760 		rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
12761 		SOCK_SENDBUF_LOCK(so);
12762 		mfree = sbcut_locked(&so->so_snd, acked);
12763 		tp->snd_una = th->th_ack;
12764 		/* Note we want to hold the sb lock through the sendmap adjust */
12765 		rack_adjust_sendmap_head(rack, &so->so_snd);
12766 		/* Wake up the socket if we have room to write more */
12767 		rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
12768 		sowwakeup_locked(so);
12769 		m_freem(mfree);
12770 		tp->t_rxtshift = 0;
12771 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12772 			      rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12773 		rack->rc_tlp_in_progress = 0;
12774 		rack->r_ctl.rc_tlp_cnt_out = 0;
12775 		/*
12776 		 * If it is the RXT timer we want to
12777 		 * stop it, so we can restart a TLP.
12778 		 */
12779 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
12780 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12781 
12782 #ifdef TCP_REQUEST_TRK
12783 		rack_req_check_for_comp(rack, th->th_ack);
12784 #endif
12785 	}
12786 	/*
12787 	 * Let the congestion control algorithm update congestion control
12788 	 * related information. This typically means increasing the
12789 	 * congestion window.
12790 	 */
12791 	if (tp->snd_wnd < ctf_outstanding(tp)) {
12792 		/* The peer collapsed the window */
12793 		rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12794 	} else if (rack->rc_has_collapsed)
12795 		rack_un_collapse_window(rack, __LINE__);
12796 	if ((rack->r_collapse_point_valid) &&
12797 	    (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
12798 		rack->r_collapse_point_valid = 0;
12799 	/*
12800 	 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
12801 	 */
12802 	tp->snd_wl2 = th->th_ack;
12803 	tp->t_dupacks = 0;
12804 	m_freem(m);
12805 	/* ND6_HINT(tp);	 *//* Some progress has been made. */
12806 
12807 	/*
12808 	 * If all outstanding data are acked, stop retransmit timer,
12809 	 * otherwise restart timer using current (possibly backed-off)
12810 	 * value. If process is waiting for space, wakeup/selwakeup/signal.
12811 	 * If data are ready to send, let tcp_output decide between more
12812 	 * output or persist.
12813 	 */
12814 	if (under_pacing &&
12815 	    (rack->use_fixed_rate == 0) &&
12816 	    (rack->in_probe_rtt == 0) &&
12817 	    rack->rc_gp_dyn_mul &&
12818 	    rack->rc_always_pace) {
12819 		/* Check if we are dragging bottom */
12820 		rack_check_bottom_drag(tp, rack, so);
12821 	}
12822 	if (tp->snd_una == tp->snd_max) {
12823 		tp->t_flags &= ~TF_PREVVALID;
12824 		rack->r_ctl.retran_during_recovery = 0;
12825 		rack->rc_suspicious = 0;
12826 		rack->r_ctl.dsack_byte_cnt = 0;
12827 		rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
12828 		if (rack->r_ctl.rc_went_idle_time == 0)
12829 			rack->r_ctl.rc_went_idle_time = 1;
12830 		rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12831 		if (sbavail(&tptosocket(tp)->so_snd) == 0)
12832 			tp->t_acktime = 0;
12833 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12834 	}
12835 	if (acked && rack->r_fast_output)
12836 		rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
12837 	if (sbavail(&so->so_snd)) {
12838 		rack->r_wanted_output = 1;
12839 	}
12840 	return (1);
12841 }
12842 
12843 /*
12844  * Return value of 1, the TCB is unlocked and most
12845  * likely gone, return value of 0, the TCP is still
12846  * locked.
12847  */
12848 static int
rack_do_syn_sent(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)12849 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
12850     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12851     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12852 {
12853 	int32_t ret_val = 0;
12854 	int32_t orig_tlen = tlen;
12855 	int32_t todrop;
12856 	int32_t ourfinisacked = 0;
12857 	struct tcp_rack *rack;
12858 
12859 	INP_WLOCK_ASSERT(tptoinpcb(tp));
12860 
12861 	ctf_calc_rwin(so, tp);
12862 	/*
12863 	 * If the state is SYN_SENT: if seg contains an ACK, but not for our
12864 	 * SYN, drop the input. if seg contains a RST, then drop the
12865 	 * connection. if seg does not contain SYN, then drop it. Otherwise
12866 	 * this is an acceptable SYN segment initialize tp->rcv_nxt and
12867 	 * tp->irs if seg contains ack then advance tp->snd_una if seg
12868 	 * contains an ECE and ECN support is enabled, the stream is ECN
12869 	 * capable. if SYN has been acked change to ESTABLISHED else
12870 	 * SYN_RCVD state arrange for segment to be acked (eventually)
12871 	 * continue processing rest of data/controls.
12872 	 */
12873 	if ((thflags & TH_ACK) &&
12874 	    (SEQ_LEQ(th->th_ack, tp->iss) ||
12875 	    SEQ_GT(th->th_ack, tp->snd_max))) {
12876 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
12877 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12878 		return (1);
12879 	}
12880 	if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
12881 		TCP_PROBE5(connect__refused, NULL, tp,
12882 		    mtod(m, const char *), tp, th);
12883 		tp = tcp_drop(tp, ECONNREFUSED);
12884 		ctf_do_drop(m, tp);
12885 		return (1);
12886 	}
12887 	if (thflags & TH_RST) {
12888 		ctf_do_drop(m, tp);
12889 		return (1);
12890 	}
12891 	if (!(thflags & TH_SYN)) {
12892 		ctf_do_drop(m, tp);
12893 		return (1);
12894 	}
12895 	tp->irs = th->th_seq;
12896 	tcp_rcvseqinit(tp);
12897 	rack = (struct tcp_rack *)tp->t_fb_ptr;
12898 	if (thflags & TH_ACK) {
12899 		int tfo_partial = 0;
12900 
12901 		KMOD_TCPSTAT_INC(tcps_connects);
12902 		soisconnected(so);
12903 #ifdef MAC
12904 		mac_socketpeer_set_from_mbuf(m, so);
12905 #endif
12906 		/* Do window scaling on this connection? */
12907 		if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
12908 		    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
12909 			tp->rcv_scale = tp->request_r_scale;
12910 		}
12911 		tp->rcv_adv += min(tp->rcv_wnd,
12912 		    TCP_MAXWIN << tp->rcv_scale);
12913 		/*
12914 		 * If not all the data that was sent in the TFO SYN
12915 		 * has been acked, resend the remainder right away.
12916 		 */
12917 		if ((tp->t_flags & TF_FASTOPEN) &&
12918 		    (tp->snd_una != tp->snd_max)) {
12919 			/* Was it a partial ack? */
12920 			if (SEQ_LT(th->th_ack, tp->snd_max))
12921 				tfo_partial = 1;
12922 		}
12923 		/*
12924 		 * If there's data, delay ACK; if there's also a FIN ACKNOW
12925 		 * will be turned on later.
12926 		 */
12927 		if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
12928 			rack_timer_cancel(tp, rack,
12929 					  rack->r_ctl.rc_rcvtime, __LINE__);
12930 			tp->t_flags |= TF_DELACK;
12931 		} else {
12932 			rack->r_wanted_output = 1;
12933 			tp->t_flags |= TF_ACKNOW;
12934 		}
12935 
12936 		tcp_ecn_input_syn_sent(tp, thflags, iptos);
12937 
12938 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
12939 			/*
12940 			 * We advance snd_una for the
12941 			 * fast open case. If th_ack is
12942 			 * acknowledging data beyond
12943 			 * snd_una we can't just call
12944 			 * ack-processing since the
12945 			 * data stream in our send-map
12946 			 * will start at snd_una + 1 (one
12947 			 * beyond the SYN). If its just
12948 			 * equal we don't need to do that
12949 			 * and there is no send_map.
12950 			 */
12951 			tp->snd_una++;
12952 			if (tfo_partial && (SEQ_GT(tp->snd_max, tp->snd_una))) {
12953 				/*
12954 				 * We sent a SYN with data, and thus have a
12955 				 * sendmap entry with a SYN set. Lets find it
12956 				 * and take off the send bit and the byte and
12957 				 * set it up to be what we send (send it next).
12958 				 */
12959 				struct rack_sendmap *rsm;
12960 
12961 				rsm = tqhash_min(rack->r_ctl.tqh);
12962 				if (rsm) {
12963 					if (rsm->r_flags & RACK_HAS_SYN) {
12964 						rsm->r_flags &= ~RACK_HAS_SYN;
12965 						rsm->r_start++;
12966 					}
12967 					rack->r_ctl.rc_resend = rsm;
12968 				}
12969 			}
12970 		}
12971 		/*
12972 		 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
12973 		 * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
12974 		 */
12975 		tp->t_starttime = ticks;
12976 		if (tp->t_flags & TF_NEEDFIN) {
12977 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
12978 			tp->t_flags &= ~TF_NEEDFIN;
12979 			thflags &= ~TH_SYN;
12980 		} else {
12981 			tcp_state_change(tp, TCPS_ESTABLISHED);
12982 			TCP_PROBE5(connect__established, NULL, tp,
12983 			    mtod(m, const char *), tp, th);
12984 			rack_cc_conn_init(tp);
12985 		}
12986 	} else {
12987 		/*
12988 		 * Received initial SYN in SYN-SENT[*] state => simultaneous
12989 		 * open.  If segment contains CC option and there is a
12990 		 * cached CC, apply TAO test. If it succeeds, connection is *
12991 		 * half-synchronized. Otherwise, do 3-way handshake:
12992 		 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
12993 		 * there was no CC option, clear cached CC value.
12994 		 */
12995 		tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
12996 		tcp_state_change(tp, TCPS_SYN_RECEIVED);
12997 	}
12998 	/*
12999 	 * Advance th->th_seq to correspond to first data byte. If data,
13000 	 * trim to stay within window, dropping FIN if necessary.
13001 	 */
13002 	th->th_seq++;
13003 	if (tlen > tp->rcv_wnd) {
13004 		todrop = tlen - tp->rcv_wnd;
13005 		m_adj(m, -todrop);
13006 		tlen = tp->rcv_wnd;
13007 		thflags &= ~TH_FIN;
13008 		KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
13009 		KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
13010 	}
13011 	tp->snd_wl1 = th->th_seq - 1;
13012 	tp->rcv_up = th->th_seq;
13013 	/*
13014 	 * Client side of transaction: already sent SYN and data. If the
13015 	 * remote host used T/TCP to validate the SYN, our data will be
13016 	 * ACK'd; if so, enter normal data segment processing in the middle
13017 	 * of step 5, ack processing. Otherwise, goto step 6.
13018 	 */
13019 	if (thflags & TH_ACK) {
13020 		/* For syn-sent we need to possibly update the rtt */
13021 		if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13022 			uint32_t t, mcts;
13023 
13024 			mcts = tcp_ts_getticks();
13025 			t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13026 			if (!tp->t_rttlow || tp->t_rttlow > t)
13027 				tp->t_rttlow = t;
13028 			rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
13029 			tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13030 			tcp_rack_xmit_timer_commit(rack, tp);
13031 		}
13032 		if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen))
13033 			return (ret_val);
13034 		/* We may have changed to FIN_WAIT_1 above */
13035 		if (tp->t_state == TCPS_FIN_WAIT_1) {
13036 			/*
13037 			 * In FIN_WAIT_1 STATE in addition to the processing
13038 			 * for the ESTABLISHED state if our FIN is now
13039 			 * acknowledged then enter FIN_WAIT_2.
13040 			 */
13041 			if (ourfinisacked) {
13042 				/*
13043 				 * If we can't receive any more data, then
13044 				 * closing user can proceed. Starting the
13045 				 * timer is contrary to the specification,
13046 				 * but if we don't get a FIN we'll hang
13047 				 * forever.
13048 				 *
13049 				 * XXXjl: we should release the tp also, and
13050 				 * use a compressed state.
13051 				 */
13052 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13053 					soisdisconnected(so);
13054 					tcp_timer_activate(tp, TT_2MSL,
13055 					    (tcp_fast_finwait2_recycle ?
13056 					    tcp_finwait2_timeout :
13057 					    TP_MAXIDLE(tp)));
13058 				}
13059 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
13060 			}
13061 		}
13062 	}
13063 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13064 	   tiwin, thflags, nxt_pkt));
13065 }
13066 
13067 /*
13068  * Return value of 1, the TCB is unlocked and most
13069  * likely gone, return value of 0, the TCP is still
13070  * locked.
13071  */
13072 static int
rack_do_syn_recv(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13073 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
13074     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13075     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13076 {
13077 	struct tcp_rack *rack;
13078 	int32_t orig_tlen = tlen;
13079 	int32_t ret_val = 0;
13080 	int32_t ourfinisacked = 0;
13081 
13082 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13083 	ctf_calc_rwin(so, tp);
13084 	if ((thflags & TH_RST) ||
13085 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13086 		return (ctf_process_rst(m, th, so, tp));
13087 	if ((thflags & TH_ACK) &&
13088 	    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
13089 	    SEQ_GT(th->th_ack, tp->snd_max))) {
13090 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13091 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13092 		return (1);
13093 	}
13094 	if (tp->t_flags & TF_FASTOPEN) {
13095 		/*
13096 		 * When a TFO connection is in SYN_RECEIVED, the
13097 		 * only valid packets are the initial SYN, a
13098 		 * retransmit/copy of the initial SYN (possibly with
13099 		 * a subset of the original data), a valid ACK, a
13100 		 * FIN, or a RST.
13101 		 */
13102 		if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
13103 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13104 			ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13105 			return (1);
13106 		} else if (thflags & TH_SYN) {
13107 			/* non-initial SYN is ignored */
13108 			if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
13109 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
13110 			    (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
13111 				ctf_do_drop(m, NULL);
13112 				return (0);
13113 			}
13114 		} else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
13115 			ctf_do_drop(m, NULL);
13116 			return (0);
13117 		}
13118 	}
13119 
13120 	/*
13121 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13122 	 * it's less than ts_recent, drop it.
13123 	 */
13124 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13125 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13126 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13127 			return (ret_val);
13128 	}
13129 	/*
13130 	 * In the SYN-RECEIVED state, validate that the packet belongs to
13131 	 * this connection before trimming the data to fit the receive
13132 	 * window.  Check the sequence number versus IRS since we know the
13133 	 * sequence numbers haven't wrapped.  This is a partial fix for the
13134 	 * "LAND" DoS attack.
13135 	 */
13136 	if (SEQ_LT(th->th_seq, tp->irs)) {
13137 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13138 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13139 		return (1);
13140 	}
13141 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13142 		return (ret_val);
13143 	}
13144 	/*
13145 	 * If last ACK falls within this segment's sequence numbers, record
13146 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13147 	 * from the latest proposal of the tcplw@cray.com list (Braden
13148 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13149 	 * with our earlier PAWS tests, so this check should be solely
13150 	 * predicated on the sequence space of this segment. 3) That we
13151 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13152 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13153 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13154 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13155 	 * p.869. In such cases, we can still calculate the RTT correctly
13156 	 * when RCV.NXT == Last.ACK.Sent.
13157 	 */
13158 	if ((to->to_flags & TOF_TS) != 0 &&
13159 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13160 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13161 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13162 		tp->ts_recent_age = tcp_ts_getticks();
13163 		tp->ts_recent = to->to_tsval;
13164 	}
13165 	tp->snd_wnd = tiwin;
13166 	rack_validate_fo_sendwin_up(tp, rack);
13167 	/*
13168 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13169 	 * is on (half-synchronized state), then queue data for later
13170 	 * processing; else drop segment and return.
13171 	 */
13172 	if ((thflags & TH_ACK) == 0) {
13173 		if (tp->t_flags & TF_FASTOPEN) {
13174 			rack_cc_conn_init(tp);
13175 		}
13176 		return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13177 		    tiwin, thflags, nxt_pkt));
13178 	}
13179 	KMOD_TCPSTAT_INC(tcps_connects);
13180 	if (tp->t_flags & TF_SONOTCONN) {
13181 		tp->t_flags &= ~TF_SONOTCONN;
13182 		soisconnected(so);
13183 	}
13184 	/* Do window scaling? */
13185 	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13186 	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13187 		tp->rcv_scale = tp->request_r_scale;
13188 	}
13189 	/*
13190 	 * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
13191 	 * FIN-WAIT-1
13192 	 */
13193 	tp->t_starttime = ticks;
13194 	if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) {
13195 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
13196 		tp->t_tfo_pending = NULL;
13197 	}
13198 	if (tp->t_flags & TF_NEEDFIN) {
13199 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
13200 		tp->t_flags &= ~TF_NEEDFIN;
13201 	} else {
13202 		tcp_state_change(tp, TCPS_ESTABLISHED);
13203 		TCP_PROBE5(accept__established, NULL, tp,
13204 		    mtod(m, const char *), tp, th);
13205 		/*
13206 		 * TFO connections call cc_conn_init() during SYN
13207 		 * processing.  Calling it again here for such connections
13208 		 * is not harmless as it would undo the snd_cwnd reduction
13209 		 * that occurs when a TFO SYN|ACK is retransmitted.
13210 		 */
13211 		if (!(tp->t_flags & TF_FASTOPEN))
13212 			rack_cc_conn_init(tp);
13213 	}
13214 	/*
13215 	 * Account for the ACK of our SYN prior to
13216 	 * regular ACK processing below, except for
13217 	 * simultaneous SYN, which is handled later.
13218 	 */
13219 	if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
13220 		tp->snd_una++;
13221 	/*
13222 	 * If segment contains data or ACK, will call tcp_reass() later; if
13223 	 * not, do so now to pass queued data to user.
13224 	 */
13225 	if (tlen == 0 && (thflags & TH_FIN) == 0) {
13226 		(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
13227 		    (struct mbuf *)0);
13228 		if (tp->t_flags & TF_WAKESOR) {
13229 			tp->t_flags &= ~TF_WAKESOR;
13230 			/* NB: sorwakeup_locked() does an implicit unlock. */
13231 			sorwakeup_locked(so);
13232 		}
13233 	}
13234 	tp->snd_wl1 = th->th_seq - 1;
13235 	/* For syn-recv we need to possibly update the rtt */
13236 	if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13237 		uint32_t t, mcts;
13238 
13239 		mcts = tcp_ts_getticks();
13240 		t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13241 		if (!tp->t_rttlow || tp->t_rttlow > t)
13242 			tp->t_rttlow = t;
13243 		rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
13244 		tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13245 		tcp_rack_xmit_timer_commit(rack, tp);
13246 	}
13247 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13248 		return (ret_val);
13249 	}
13250 	if (tp->t_state == TCPS_FIN_WAIT_1) {
13251 		/* We could have went to FIN_WAIT_1 (or EST) above */
13252 		/*
13253 		 * In FIN_WAIT_1 STATE in addition to the processing for the
13254 		 * ESTABLISHED state if our FIN is now acknowledged then
13255 		 * enter FIN_WAIT_2.
13256 		 */
13257 		if (ourfinisacked) {
13258 			/*
13259 			 * If we can't receive any more data, then closing
13260 			 * user can proceed. Starting the timer is contrary
13261 			 * to the specification, but if we don't get a FIN
13262 			 * we'll hang forever.
13263 			 *
13264 			 * XXXjl: we should release the tp also, and use a
13265 			 * compressed state.
13266 			 */
13267 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13268 				soisdisconnected(so);
13269 				tcp_timer_activate(tp, TT_2MSL,
13270 				    (tcp_fast_finwait2_recycle ?
13271 				    tcp_finwait2_timeout :
13272 				    TP_MAXIDLE(tp)));
13273 			}
13274 			tcp_state_change(tp, TCPS_FIN_WAIT_2);
13275 		}
13276 	}
13277 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13278 	    tiwin, thflags, nxt_pkt));
13279 }
13280 
13281 /*
13282  * Return value of 1, the TCB is unlocked and most
13283  * likely gone, return value of 0, the TCP is still
13284  * locked.
13285  */
13286 static int
rack_do_established(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13287 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
13288     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13289     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13290 {
13291 	int32_t ret_val = 0;
13292 	int32_t orig_tlen = tlen;
13293 	struct tcp_rack *rack;
13294 
13295 	/*
13296 	 * Header prediction: check for the two common cases of a
13297 	 * uni-directional data xfer.  If the packet has no control flags,
13298 	 * is in-sequence, the window didn't change and we're not
13299 	 * retransmitting, it's a candidate.  If the length is zero and the
13300 	 * ack moved forward, we're the sender side of the xfer.  Just free
13301 	 * the data acked & wake any higher level process that was blocked
13302 	 * waiting for space.  If the length is non-zero and the ack didn't
13303 	 * move, we're the receiver side.  If we're getting packets in-order
13304 	 * (the reassembly queue is empty), add the data toc The socket
13305 	 * buffer and note that we need a delayed ack. Make sure that the
13306 	 * hidden state-flags are also off. Since we check for
13307 	 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
13308 	 */
13309 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13310 	if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
13311 	    __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
13312 	    __predict_true(SEGQ_EMPTY(tp)) &&
13313 	    __predict_true(th->th_seq == tp->rcv_nxt)) {
13314 		if (tlen == 0) {
13315 			if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
13316 			    tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
13317 				return (0);
13318 			}
13319 		} else {
13320 			if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
13321 			    tiwin, nxt_pkt, iptos)) {
13322 				return (0);
13323 			}
13324 		}
13325 	}
13326 	ctf_calc_rwin(so, tp);
13327 
13328 	if ((thflags & TH_RST) ||
13329 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13330 		return (ctf_process_rst(m, th, so, tp));
13331 
13332 	/*
13333 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13334 	 * synchronized state.
13335 	 */
13336 	if (thflags & TH_SYN) {
13337 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13338 		return (ret_val);
13339 	}
13340 	/*
13341 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13342 	 * it's less than ts_recent, drop it.
13343 	 */
13344 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13345 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13346 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13347 			return (ret_val);
13348 	}
13349 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13350 		return (ret_val);
13351 	}
13352 	/*
13353 	 * If last ACK falls within this segment's sequence numbers, record
13354 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13355 	 * from the latest proposal of the tcplw@cray.com list (Braden
13356 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13357 	 * with our earlier PAWS tests, so this check should be solely
13358 	 * predicated on the sequence space of this segment. 3) That we
13359 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13360 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13361 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13362 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13363 	 * p.869. In such cases, we can still calculate the RTT correctly
13364 	 * when RCV.NXT == Last.ACK.Sent.
13365 	 */
13366 	if ((to->to_flags & TOF_TS) != 0 &&
13367 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13368 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13369 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13370 		tp->ts_recent_age = tcp_ts_getticks();
13371 		tp->ts_recent = to->to_tsval;
13372 	}
13373 	/*
13374 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13375 	 * is on (half-synchronized state), then queue data for later
13376 	 * processing; else drop segment and return.
13377 	 */
13378 	if ((thflags & TH_ACK) == 0) {
13379 		if (tp->t_flags & TF_NEEDSYN) {
13380 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13381 			    tiwin, thflags, nxt_pkt));
13382 
13383 		} else if (tp->t_flags & TF_ACKNOW) {
13384 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13385 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13386 			return (ret_val);
13387 		} else {
13388 			ctf_do_drop(m, NULL);
13389 			return (0);
13390 		}
13391 	}
13392 	/*
13393 	 * Ack processing.
13394 	 */
13395 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
13396 		return (ret_val);
13397 	}
13398 	if (sbavail(&so->so_snd)) {
13399 		if (ctf_progress_timeout_check(tp, true)) {
13400 			rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
13401 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13402 			return (1);
13403 		}
13404 	}
13405 	/* State changes only happen in rack_process_data() */
13406 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13407 	    tiwin, thflags, nxt_pkt));
13408 }
13409 
13410 /*
13411  * Return value of 1, the TCB is unlocked and most
13412  * likely gone, return value of 0, the TCP is still
13413  * locked.
13414  */
13415 static int
rack_do_close_wait(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13416 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
13417     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13418     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13419 {
13420 	int32_t ret_val = 0;
13421 	int32_t orig_tlen = tlen;
13422 
13423 	ctf_calc_rwin(so, tp);
13424 	if ((thflags & TH_RST) ||
13425 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13426 		return (ctf_process_rst(m, th, so, tp));
13427 	/*
13428 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13429 	 * synchronized state.
13430 	 */
13431 	if (thflags & TH_SYN) {
13432 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13433 		return (ret_val);
13434 	}
13435 	/*
13436 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13437 	 * it's less than ts_recent, drop it.
13438 	 */
13439 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13440 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13441 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13442 			return (ret_val);
13443 	}
13444 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13445 		return (ret_val);
13446 	}
13447 	/*
13448 	 * If last ACK falls within this segment's sequence numbers, record
13449 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13450 	 * from the latest proposal of the tcplw@cray.com list (Braden
13451 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13452 	 * with our earlier PAWS tests, so this check should be solely
13453 	 * predicated on the sequence space of this segment. 3) That we
13454 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13455 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13456 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13457 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13458 	 * p.869. In such cases, we can still calculate the RTT correctly
13459 	 * when RCV.NXT == Last.ACK.Sent.
13460 	 */
13461 	if ((to->to_flags & TOF_TS) != 0 &&
13462 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13463 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13464 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13465 		tp->ts_recent_age = tcp_ts_getticks();
13466 		tp->ts_recent = to->to_tsval;
13467 	}
13468 	/*
13469 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13470 	 * is on (half-synchronized state), then queue data for later
13471 	 * processing; else drop segment and return.
13472 	 */
13473 	if ((thflags & TH_ACK) == 0) {
13474 		if (tp->t_flags & TF_NEEDSYN) {
13475 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13476 			    tiwin, thflags, nxt_pkt));
13477 
13478 		} else if (tp->t_flags & TF_ACKNOW) {
13479 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13480 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13481 			return (ret_val);
13482 		} else {
13483 			ctf_do_drop(m, NULL);
13484 			return (0);
13485 		}
13486 	}
13487 	/*
13488 	 * Ack processing.
13489 	 */
13490 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) {
13491 		return (ret_val);
13492 	}
13493 	if (sbavail(&so->so_snd)) {
13494 		if (ctf_progress_timeout_check(tp, true)) {
13495 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13496 						tp, tick, PROGRESS_DROP, __LINE__);
13497 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13498 			return (1);
13499 		}
13500 	}
13501 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13502 	    tiwin, thflags, nxt_pkt));
13503 }
13504 
13505 static int
rack_check_data_after_close(struct mbuf * m,struct tcpcb * tp,int32_t * tlen,struct tcphdr * th,struct socket * so)13506 rack_check_data_after_close(struct mbuf *m,
13507     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
13508 {
13509 	struct tcp_rack *rack;
13510 
13511 	rack = (struct tcp_rack *)tp->t_fb_ptr;
13512 	if (rack->rc_allow_data_af_clo == 0) {
13513 	close_now:
13514 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13515 		/* tcp_close will kill the inp pre-log the Reset */
13516 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13517 		tp = tcp_close(tp);
13518 		KMOD_TCPSTAT_INC(tcps_rcvafterclose);
13519 		ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
13520 		return (1);
13521 	}
13522 	if (sbavail(&so->so_snd) == 0)
13523 		goto close_now;
13524 	/* Ok we allow data that is ignored and a followup reset */
13525 	tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13526 	tp->rcv_nxt = th->th_seq + *tlen;
13527 	tp->t_flags2 |= TF2_DROP_AF_DATA;
13528 	rack->r_wanted_output = 1;
13529 	*tlen = 0;
13530 	return (0);
13531 }
13532 
13533 /*
13534  * Return value of 1, the TCB is unlocked and most
13535  * likely gone, return value of 0, the TCP is still
13536  * locked.
13537  */
13538 static int
rack_do_fin_wait_1(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13539 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
13540     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13541     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13542 {
13543 	int32_t ret_val = 0;
13544 	int32_t orig_tlen = tlen;
13545 	int32_t ourfinisacked = 0;
13546 
13547 	ctf_calc_rwin(so, tp);
13548 
13549 	if ((thflags & TH_RST) ||
13550 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13551 		return (ctf_process_rst(m, th, so, tp));
13552 	/*
13553 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13554 	 * synchronized state.
13555 	 */
13556 	if (thflags & TH_SYN) {
13557 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13558 		return (ret_val);
13559 	}
13560 	/*
13561 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13562 	 * it's less than ts_recent, drop it.
13563 	 */
13564 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13565 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13566 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13567 			return (ret_val);
13568 	}
13569 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13570 		return (ret_val);
13571 	}
13572 	/*
13573 	 * If new data are received on a connection after the user processes
13574 	 * are gone, then RST the other end.
13575 	 */
13576 	if ((tp->t_flags & TF_CLOSED) && tlen &&
13577 	    rack_check_data_after_close(m, tp, &tlen, th, so))
13578 		return (1);
13579 	/*
13580 	 * If last ACK falls within this segment's sequence numbers, record
13581 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13582 	 * from the latest proposal of the tcplw@cray.com list (Braden
13583 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13584 	 * with our earlier PAWS tests, so this check should be solely
13585 	 * predicated on the sequence space of this segment. 3) That we
13586 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13587 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13588 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13589 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13590 	 * p.869. In such cases, we can still calculate the RTT correctly
13591 	 * when RCV.NXT == Last.ACK.Sent.
13592 	 */
13593 	if ((to->to_flags & TOF_TS) != 0 &&
13594 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13595 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13596 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13597 		tp->ts_recent_age = tcp_ts_getticks();
13598 		tp->ts_recent = to->to_tsval;
13599 	}
13600 	/*
13601 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13602 	 * is on (half-synchronized state), then queue data for later
13603 	 * processing; else drop segment and return.
13604 	 */
13605 	if ((thflags & TH_ACK) == 0) {
13606 		if (tp->t_flags & TF_NEEDSYN) {
13607 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13608 			    tiwin, thflags, nxt_pkt));
13609 		} else if (tp->t_flags & TF_ACKNOW) {
13610 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13611 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13612 			return (ret_val);
13613 		} else {
13614 			ctf_do_drop(m, NULL);
13615 			return (0);
13616 		}
13617 	}
13618 	/*
13619 	 * Ack processing.
13620 	 */
13621 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13622 		return (ret_val);
13623 	}
13624 	if (ourfinisacked) {
13625 		/*
13626 		 * If we can't receive any more data, then closing user can
13627 		 * proceed. Starting the timer is contrary to the
13628 		 * specification, but if we don't get a FIN we'll hang
13629 		 * forever.
13630 		 *
13631 		 * XXXjl: we should release the tp also, and use a
13632 		 * compressed state.
13633 		 */
13634 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13635 			soisdisconnected(so);
13636 			tcp_timer_activate(tp, TT_2MSL,
13637 			    (tcp_fast_finwait2_recycle ?
13638 			    tcp_finwait2_timeout :
13639 			    TP_MAXIDLE(tp)));
13640 		}
13641 		tcp_state_change(tp, TCPS_FIN_WAIT_2);
13642 	}
13643 	if (sbavail(&so->so_snd)) {
13644 		if (ctf_progress_timeout_check(tp, true)) {
13645 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13646 						tp, tick, PROGRESS_DROP, __LINE__);
13647 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13648 			return (1);
13649 		}
13650 	}
13651 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13652 	    tiwin, thflags, nxt_pkt));
13653 }
13654 
13655 /*
13656  * Return value of 1, the TCB is unlocked and most
13657  * likely gone, return value of 0, the TCP is still
13658  * locked.
13659  */
13660 static int
rack_do_closing(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13661 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
13662     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13663     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13664 {
13665 	int32_t ret_val = 0;
13666 	int32_t orig_tlen = tlen;
13667 	int32_t ourfinisacked = 0;
13668 
13669 	ctf_calc_rwin(so, tp);
13670 
13671 	if ((thflags & TH_RST) ||
13672 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13673 		return (ctf_process_rst(m, th, so, tp));
13674 	/*
13675 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13676 	 * synchronized state.
13677 	 */
13678 	if (thflags & TH_SYN) {
13679 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13680 		return (ret_val);
13681 	}
13682 	/*
13683 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13684 	 * it's less than ts_recent, drop it.
13685 	 */
13686 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13687 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13688 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13689 			return (ret_val);
13690 	}
13691 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13692 		return (ret_val);
13693 	}
13694 	/*
13695 	 * If last ACK falls within this segment's sequence numbers, record
13696 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13697 	 * from the latest proposal of the tcplw@cray.com list (Braden
13698 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13699 	 * with our earlier PAWS tests, so this check should be solely
13700 	 * predicated on the sequence space of this segment. 3) That we
13701 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13702 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13703 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13704 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13705 	 * p.869. In such cases, we can still calculate the RTT correctly
13706 	 * when RCV.NXT == Last.ACK.Sent.
13707 	 */
13708 	if ((to->to_flags & TOF_TS) != 0 &&
13709 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13710 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13711 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13712 		tp->ts_recent_age = tcp_ts_getticks();
13713 		tp->ts_recent = to->to_tsval;
13714 	}
13715 	/*
13716 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13717 	 * is on (half-synchronized state), then queue data for later
13718 	 * processing; else drop segment and return.
13719 	 */
13720 	if ((thflags & TH_ACK) == 0) {
13721 		if (tp->t_flags & TF_NEEDSYN) {
13722 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13723 			    tiwin, thflags, nxt_pkt));
13724 		} else if (tp->t_flags & TF_ACKNOW) {
13725 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13726 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13727 			return (ret_val);
13728 		} else {
13729 			ctf_do_drop(m, NULL);
13730 			return (0);
13731 		}
13732 	}
13733 	/*
13734 	 * Ack processing.
13735 	 */
13736 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13737 		return (ret_val);
13738 	}
13739 	if (ourfinisacked) {
13740 		tcp_twstart(tp);
13741 		m_freem(m);
13742 		return (1);
13743 	}
13744 	if (sbavail(&so->so_snd)) {
13745 		if (ctf_progress_timeout_check(tp, true)) {
13746 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13747 						tp, tick, PROGRESS_DROP, __LINE__);
13748 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13749 			return (1);
13750 		}
13751 	}
13752 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13753 	    tiwin, thflags, nxt_pkt));
13754 }
13755 
13756 /*
13757  * Return value of 1, the TCB is unlocked and most
13758  * likely gone, return value of 0, the TCP is still
13759  * locked.
13760  */
13761 static int
rack_do_lastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13762 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
13763     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13764     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13765 {
13766 	int32_t ret_val = 0;
13767 	int32_t orig_tlen;
13768 	int32_t ourfinisacked = 0;
13769 
13770 	ctf_calc_rwin(so, tp);
13771 
13772 	if ((thflags & TH_RST) ||
13773 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13774 		return (ctf_process_rst(m, th, so, tp));
13775 	/*
13776 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13777 	 * synchronized state.
13778 	 */
13779 	if (thflags & TH_SYN) {
13780 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13781 		return (ret_val);
13782 	}
13783 	/*
13784 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13785 	 * it's less than ts_recent, drop it.
13786 	 */
13787 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13788 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13789 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13790 			return (ret_val);
13791 	}
13792 	orig_tlen = tlen;
13793 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13794 		return (ret_val);
13795 	}
13796 	/*
13797 	 * If last ACK falls within this segment's sequence numbers, record
13798 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13799 	 * from the latest proposal of the tcplw@cray.com list (Braden
13800 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13801 	 * with our earlier PAWS tests, so this check should be solely
13802 	 * predicated on the sequence space of this segment. 3) That we
13803 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13804 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13805 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13806 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13807 	 * p.869. In such cases, we can still calculate the RTT correctly
13808 	 * when RCV.NXT == Last.ACK.Sent.
13809 	 */
13810 	if ((to->to_flags & TOF_TS) != 0 &&
13811 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13812 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13813 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13814 		tp->ts_recent_age = tcp_ts_getticks();
13815 		tp->ts_recent = to->to_tsval;
13816 	}
13817 	/*
13818 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13819 	 * is on (half-synchronized state), then queue data for later
13820 	 * processing; else drop segment and return.
13821 	 */
13822 	if ((thflags & TH_ACK) == 0) {
13823 		if (tp->t_flags & TF_NEEDSYN) {
13824 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13825 			    tiwin, thflags, nxt_pkt));
13826 		} else if (tp->t_flags & TF_ACKNOW) {
13827 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13828 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13829 			return (ret_val);
13830 		} else {
13831 			ctf_do_drop(m, NULL);
13832 			return (0);
13833 		}
13834 	}
13835 	/*
13836 	 * case TCPS_LAST_ACK: Ack processing.
13837 	 */
13838 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13839 		return (ret_val);
13840 	}
13841 	if (ourfinisacked) {
13842 		tp = tcp_close(tp);
13843 		ctf_do_drop(m, tp);
13844 		return (1);
13845 	}
13846 	if (sbavail(&so->so_snd)) {
13847 		if (ctf_progress_timeout_check(tp, true)) {
13848 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13849 						tp, tick, PROGRESS_DROP, __LINE__);
13850 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13851 			return (1);
13852 		}
13853 	}
13854 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13855 	    tiwin, thflags, nxt_pkt));
13856 }
13857 
13858 /*
13859  * Return value of 1, the TCB is unlocked and most
13860  * likely gone, return value of 0, the TCP is still
13861  * locked.
13862  */
13863 static int
rack_do_fin_wait_2(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)13864 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
13865     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13866     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13867 {
13868 	int32_t ret_val = 0;
13869 	int32_t orig_tlen = tlen;
13870 	int32_t ourfinisacked = 0;
13871 
13872 	ctf_calc_rwin(so, tp);
13873 
13874 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
13875 	if ((thflags & TH_RST) ||
13876 	    (tp->t_fin_is_rst && (thflags & TH_FIN)))
13877 		return (ctf_process_rst(m, th, so, tp));
13878 	/*
13879 	 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13880 	 * synchronized state.
13881 	 */
13882 	if (thflags & TH_SYN) {
13883 		ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13884 		return (ret_val);
13885 	}
13886 	/*
13887 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13888 	 * it's less than ts_recent, drop it.
13889 	 */
13890 	if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13891 	    TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13892 		if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13893 			return (ret_val);
13894 	}
13895 	if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
13896 		return (ret_val);
13897 	}
13898 	/*
13899 	 * If new data are received on a connection after the user processes
13900 	 * are gone, then RST the other end.
13901 	 */
13902 	if ((tp->t_flags & TF_CLOSED) && tlen &&
13903 	    rack_check_data_after_close(m, tp, &tlen, th, so))
13904 		return (1);
13905 	/*
13906 	 * If last ACK falls within this segment's sequence numbers, record
13907 	 * its timestamp. NOTE: 1) That the test incorporates suggestions
13908 	 * from the latest proposal of the tcplw@cray.com list (Braden
13909 	 * 1993/04/26). 2) That updating only on newer timestamps interferes
13910 	 * with our earlier PAWS tests, so this check should be solely
13911 	 * predicated on the sequence space of this segment. 3) That we
13912 	 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13913 	 * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13914 	 * SEG.Len, This modified check allows us to overcome RFC1323's
13915 	 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13916 	 * p.869. In such cases, we can still calculate the RTT correctly
13917 	 * when RCV.NXT == Last.ACK.Sent.
13918 	 */
13919 	if ((to->to_flags & TOF_TS) != 0 &&
13920 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13921 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13922 	    ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13923 		tp->ts_recent_age = tcp_ts_getticks();
13924 		tp->ts_recent = to->to_tsval;
13925 	}
13926 	/*
13927 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
13928 	 * is on (half-synchronized state), then queue data for later
13929 	 * processing; else drop segment and return.
13930 	 */
13931 	if ((thflags & TH_ACK) == 0) {
13932 		if (tp->t_flags & TF_NEEDSYN) {
13933 			return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13934 			    tiwin, thflags, nxt_pkt));
13935 		} else if (tp->t_flags & TF_ACKNOW) {
13936 			ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13937 			((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13938 			return (ret_val);
13939 		} else {
13940 			ctf_do_drop(m, NULL);
13941 			return (0);
13942 		}
13943 	}
13944 	/*
13945 	 * Ack processing.
13946 	 */
13947 	if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) {
13948 		return (ret_val);
13949 	}
13950 	if (sbavail(&so->so_snd)) {
13951 		if (ctf_progress_timeout_check(tp, true)) {
13952 			rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13953 						tp, tick, PROGRESS_DROP, __LINE__);
13954 			ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13955 			return (1);
13956 		}
13957 	}
13958 	return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13959 	    tiwin, thflags, nxt_pkt));
13960 }
13961 
13962 static void inline
rack_clear_rate_sample(struct tcp_rack * rack)13963 rack_clear_rate_sample(struct tcp_rack *rack)
13964 {
13965 	rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
13966 	rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
13967 	rack->r_ctl.rack_rs.rs_rtt_tot = 0;
13968 }
13969 
13970 static void
rack_set_pace_segments(struct tcpcb * tp,struct tcp_rack * rack,uint32_t line,uint64_t * fill_override)13971 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
13972 {
13973 	uint64_t bw_est, rate_wanted;
13974 	int chged = 0;
13975 	uint32_t user_max, orig_min, orig_max;
13976 
13977 #ifdef TCP_REQUEST_TRK
13978 	if (rack->rc_hybrid_mode &&
13979 	    (rack->r_ctl.rc_pace_max_segs != 0) &&
13980 	    (rack_hybrid_allow_set_maxseg == 1) &&
13981 	    (rack->r_ctl.rc_last_sft != NULL)) {
13982 		rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS;
13983 		return;
13984 	}
13985 #endif
13986 	orig_min = rack->r_ctl.rc_pace_min_segs;
13987 	orig_max = rack->r_ctl.rc_pace_max_segs;
13988 	user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
13989 	if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
13990 		chged = 1;
13991 	rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
13992 	if (rack->use_fixed_rate || rack->rc_force_max_seg) {
13993 		if (user_max != rack->r_ctl.rc_pace_max_segs)
13994 			chged = 1;
13995 	}
13996 	if (rack->rc_force_max_seg) {
13997 		rack->r_ctl.rc_pace_max_segs = user_max;
13998 	} else if (rack->use_fixed_rate) {
13999 		bw_est = rack_get_bw(rack);
14000 		if ((rack->r_ctl.crte == NULL) ||
14001 		    (bw_est != rack->r_ctl.crte->rate)) {
14002 			rack->r_ctl.rc_pace_max_segs = user_max;
14003 		} else {
14004 			/* We are pacing right at the hardware rate */
14005 			uint32_t segsiz, pace_one;
14006 
14007 			if (rack_pace_one_seg ||
14008 			    (rack->r_ctl.rc_user_set_min_segs == 1))
14009 				pace_one = 1;
14010 			else
14011 				pace_one = 0;
14012 			segsiz = min(ctf_fixed_maxseg(tp),
14013 				     rack->r_ctl.rc_pace_min_segs);
14014 			rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(
14015 				tp, bw_est, segsiz, pace_one,
14016 				rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
14017 		}
14018 	} else if (rack->rc_always_pace) {
14019 		if (rack->r_ctl.gp_bw ||
14020 		    rack->r_ctl.init_rate) {
14021 			/* We have a rate of some sort set */
14022 			uint32_t  orig;
14023 
14024 			bw_est = rack_get_bw(rack);
14025 			orig = rack->r_ctl.rc_pace_max_segs;
14026 			if (fill_override)
14027 				rate_wanted = *fill_override;
14028 			else
14029 				rate_wanted = rack_get_gp_est(rack);
14030 			if (rate_wanted) {
14031 				/* We have something */
14032 				rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
14033 										   rate_wanted,
14034 										   ctf_fixed_maxseg(rack->rc_tp));
14035 			} else
14036 				rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
14037 			if (orig != rack->r_ctl.rc_pace_max_segs)
14038 				chged = 1;
14039 		} else if ((rack->r_ctl.gp_bw == 0) &&
14040 			   (rack->r_ctl.rc_pace_max_segs == 0)) {
14041 			/*
14042 			 * If we have nothing limit us to bursting
14043 			 * out IW sized pieces.
14044 			 */
14045 			chged = 1;
14046 			rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
14047 		}
14048 	}
14049 	if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
14050 		chged = 1;
14051 		rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
14052 	}
14053 	if (chged)
14054 		rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
14055 }
14056 
14057 
14058 static void
rack_init_fsb_block(struct tcpcb * tp,struct tcp_rack * rack,int32_t flags)14059 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags)
14060 {
14061 #ifdef INET6
14062 	struct ip6_hdr *ip6 = NULL;
14063 #endif
14064 #ifdef INET
14065 	struct ip *ip = NULL;
14066 #endif
14067 	struct udphdr *udp = NULL;
14068 
14069 	/* Ok lets fill in the fast block, it can only be used with no IP options! */
14070 #ifdef INET6
14071 	if (rack->r_is_v6) {
14072 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
14073 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
14074 		if (tp->t_port) {
14075 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14076 			udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
14077 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14078 			udp->uh_dport = tp->t_port;
14079 			rack->r_ctl.fsb.udp = udp;
14080 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14081 		} else
14082 		{
14083 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
14084 			rack->r_ctl.fsb.udp = NULL;
14085 		}
14086 		tcpip_fillheaders(rack->rc_inp,
14087 				  tp->t_port,
14088 				  ip6, rack->r_ctl.fsb.th);
14089 		rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL);
14090 	} else
14091 #endif				/* INET6 */
14092 #ifdef INET
14093 	{
14094 		rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
14095 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
14096 		if (tp->t_port) {
14097 			rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14098 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
14099 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14100 			udp->uh_dport = tp->t_port;
14101 			rack->r_ctl.fsb.udp = udp;
14102 			rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14103 		} else
14104 		{
14105 			rack->r_ctl.fsb.udp = NULL;
14106 			rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
14107 		}
14108 		tcpip_fillheaders(rack->rc_inp,
14109 				  tp->t_port,
14110 				  ip, rack->r_ctl.fsb.th);
14111 		rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl;
14112 	}
14113 #endif
14114 	rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0),
14115 	    (long)TCP_MAXWIN << tp->rcv_scale);
14116 	rack->r_fsb_inited = 1;
14117 }
14118 
14119 static int
rack_init_fsb(struct tcpcb * tp,struct tcp_rack * rack)14120 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
14121 {
14122 	/*
14123 	 * Allocate the larger of spaces V6 if available else just
14124 	 * V4 and include udphdr (overbook)
14125 	 */
14126 #ifdef INET6
14127 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
14128 #else
14129 	rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
14130 #endif
14131 	rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
14132 					    M_TCPFSB, M_NOWAIT|M_ZERO);
14133 	if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
14134 		return (ENOMEM);
14135 	}
14136 	rack->r_fsb_inited = 0;
14137 	return (0);
14138 }
14139 
14140 static void
rack_log_hystart_event(struct tcp_rack * rack,uint32_t high_seq,uint8_t mod)14141 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod)
14142 {
14143 	/*
14144 	 * Types of logs (mod value)
14145 	 * 20 - Initial round setup
14146 	 * 21 - Rack declares a new round.
14147 	 */
14148 	struct tcpcb *tp;
14149 
14150 	tp = rack->rc_tp;
14151 	if (tcp_bblogging_on(tp)) {
14152 		union tcp_log_stackspecific log;
14153 		struct timeval tv;
14154 
14155 		memset(&log, 0, sizeof(log));
14156 		log.u_bbr.flex1 = rack->r_ctl.current_round;
14157 		log.u_bbr.flex2 = rack->r_ctl.roundends;
14158 		log.u_bbr.flex3 = high_seq;
14159 		log.u_bbr.flex4 = tp->snd_max;
14160 		log.u_bbr.flex8 = mod;
14161 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14162 		log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
14163 		log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
14164 		TCP_LOG_EVENTP(tp, NULL,
14165 		    &tptosocket(tp)->so_rcv,
14166 		    &tptosocket(tp)->so_snd,
14167 		    TCP_HYSTART, 0,
14168 		    0, &log, false, &tv);
14169 	}
14170 }
14171 
14172 static void
rack_deferred_init(struct tcpcb * tp,struct tcp_rack * rack)14173 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack)
14174 {
14175 	rack->rack_deferred_inited = 1;
14176 	rack->r_ctl.roundends = tp->snd_max;
14177 	rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
14178 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
14179 }
14180 
14181 static void
rack_init_retransmit_value(struct tcp_rack * rack,int ctl)14182 rack_init_retransmit_value(struct tcp_rack *rack, int ctl)
14183 {
14184 	/* Retransmit bit controls.
14185 	 *
14186 	 * The setting of these values control one of
14187 	 * three settings you can have and dictate
14188 	 * how rack does retransmissions. Note this
14189 	 * is in *any* mode i.e. pacing on or off DGP
14190 	 * fixed rate pacing, or just bursting rack.
14191 	 *
14192 	 * 1 - Use full sized retransmits i.e. limit
14193 	 *     the size to whatever the pace_max_segments
14194 	 *     size is.
14195 	 *
14196 	 * 2 - Use pacer min granularity as a guide to
14197 	 *     the size combined with the current calculated
14198 	 *     goodput b/w measurement. So for example if
14199 	 *     the goodput is measured at 20Mbps we would
14200 	 *     calculate 8125 (pacer minimum 250usec in
14201 	 *     that b/w) and then round it up to the next
14202 	 *     MSS i.e. for 1448 mss 6 MSS or 8688 bytes.
14203 	 *
14204 	 * 0 - The rack default 1 MSS (anything not 0/1/2
14205 	 *     fall here too if we are setting via rack_init()).
14206 	 *
14207 	 */
14208 	if (ctl == 1) {
14209 		rack->full_size_rxt = 1;
14210 		rack->shape_rxt_to_pacing_min  = 0;
14211 	} else if (ctl == 2) {
14212 		rack->full_size_rxt = 0;
14213 		rack->shape_rxt_to_pacing_min  = 1;
14214 	} else {
14215 		rack->full_size_rxt = 0;
14216 		rack->shape_rxt_to_pacing_min  = 0;
14217 	}
14218 }
14219 
14220 static void
rack_log_chg_info(struct tcpcb * tp,struct tcp_rack * rack,uint8_t mod,uint32_t flex1,uint32_t flex2,uint32_t flex3)14221 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod,
14222 		  uint32_t flex1,
14223 		  uint32_t flex2,
14224 		  uint32_t flex3)
14225 {
14226 	if (tcp_bblogging_on(rack->rc_tp)) {
14227 		union tcp_log_stackspecific log;
14228 		struct timeval tv;
14229 
14230 		memset(&log, 0, sizeof(log));
14231 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14232 		log.u_bbr.flex8 = mod;
14233 		log.u_bbr.flex1 = flex1;
14234 		log.u_bbr.flex2 = flex2;
14235 		log.u_bbr.flex3 = flex3;
14236 		tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0,
14237 			       0, &log, false, NULL, __func__, __LINE__, &tv);
14238 	}
14239 }
14240 
14241 static int
rack_chg_query(struct tcpcb * tp,struct tcp_query_resp * reqr)14242 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr)
14243 {
14244 	struct tcp_rack *rack;
14245 	struct rack_sendmap *rsm;
14246 	int i;
14247 
14248 
14249 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14250 	switch (reqr->req) {
14251 	case TCP_QUERY_SENDMAP:
14252 		if ((reqr->req_param == tp->snd_max) ||
14253 		    (tp->snd_max == tp->snd_una)){
14254 			/* Unlikely */
14255 			return (0);
14256 		}
14257 		rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param);
14258 		if (rsm == NULL) {
14259 			/* Can't find that seq -- unlikely */
14260 			return (0);
14261 		}
14262 		reqr->sendmap_start = rsm->r_start;
14263 		reqr->sendmap_end = rsm->r_end;
14264 		reqr->sendmap_send_cnt = rsm->r_rtr_cnt;
14265 		reqr->sendmap_fas = rsm->r_fas;
14266 		if (reqr->sendmap_send_cnt > SNDMAP_NRTX)
14267 			reqr->sendmap_send_cnt = SNDMAP_NRTX;
14268 		for(i=0; i<reqr->sendmap_send_cnt; i++)
14269 			reqr->sendmap_time[i] = rsm->r_tim_lastsent[i];
14270 		reqr->sendmap_ack_arrival = rsm->r_ack_arrival;
14271 		reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK;
14272 		reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes;
14273 		reqr->sendmap_dupacks = rsm->r_dupack;
14274 		rack_log_chg_info(tp, rack, 1,
14275 				  rsm->r_start,
14276 				  rsm->r_end,
14277 				  rsm->r_flags);
14278 		return(1);
14279 		break;
14280 	case TCP_QUERY_TIMERS_UP:
14281 		if (rack->r_ctl.rc_hpts_flags == 0) {
14282 			/* no timers up */
14283 			return (0);
14284 		}
14285 		reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags;
14286 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14287 			reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to;
14288 		}
14289 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14290 			reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp;
14291 		}
14292 		rack_log_chg_info(tp, rack, 2,
14293 				  rack->r_ctl.rc_hpts_flags,
14294 				  rack->r_ctl.rc_last_output_to,
14295 				  rack->r_ctl.rc_timer_exp);
14296 		return (1);
14297 		break;
14298 	case TCP_QUERY_RACK_TIMES:
14299 		/* Reordering items */
14300 		reqr->rack_num_dsacks = rack->r_ctl.num_dsack;
14301 		reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts;
14302 		/* Timerstamps and timers */
14303 		reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time;
14304 		reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt;
14305 		reqr->rack_rtt = rack->rc_rack_rtt;
14306 		reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time;
14307 		reqr->rack_srtt_measured = rack->rc_srtt_measure_made;
14308 		/* PRR data */
14309 		reqr->rack_sacked = rack->r_ctl.rc_sacked;
14310 		reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt;
14311 		reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered;
14312 		reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs;
14313 		reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt;
14314 		reqr->rack_prr_out = rack->r_ctl.rc_prr_out;
14315 		/* TLP and persists info */
14316 		reqr->rack_tlp_out = rack->rc_tlp_in_progress;
14317 		reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out;
14318 		if (rack->rc_in_persist) {
14319 			reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time;
14320 			reqr->rack_in_persist = 1;
14321 		} else {
14322 			reqr->rack_time_went_idle = 0;
14323 			reqr->rack_in_persist = 0;
14324 		}
14325 		if (rack->r_wanted_output)
14326 			reqr->rack_wanted_output = 1;
14327 		else
14328 			reqr->rack_wanted_output = 0;
14329 		return (1);
14330 		break;
14331 	default:
14332 		return (-EINVAL);
14333 	}
14334 }
14335 
14336 static void
rack_switch_failed(struct tcpcb * tp)14337 rack_switch_failed(struct tcpcb *tp)
14338 {
14339 	/*
14340 	 * This method gets called if a stack switch was
14341 	 * attempted and it failed. We are left
14342 	 * but our hpts timers were stopped and we
14343 	 * need to validate time units and t_flags2.
14344 	 */
14345 	struct tcp_rack *rack;
14346 	struct timeval tv;
14347 	uint32_t cts;
14348 	uint32_t toval;
14349 	struct hpts_diag diag;
14350 
14351 	rack = (struct tcp_rack *)tp->t_fb_ptr;
14352 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
14353 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14354 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14355 	else
14356 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14357 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14358 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
14359 	if (tp->t_in_hpts > IHPTS_NONE) {
14360 		/* Strange */
14361 		return;
14362 	}
14363 	cts = tcp_get_usecs(&tv);
14364 	if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14365 		if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
14366 			toval = rack->r_ctl.rc_last_output_to - cts;
14367 		} else {
14368 			/* one slot please */
14369 			toval = HPTS_TICKS_PER_SLOT;
14370 		}
14371 	} else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14372 		if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
14373 			toval = rack->r_ctl.rc_timer_exp - cts;
14374 		} else {
14375 			/* one slot please */
14376 			toval = HPTS_TICKS_PER_SLOT;
14377 		}
14378 	} else
14379 		toval = HPTS_TICKS_PER_SLOT;
14380 	(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14381 				   __LINE__, &diag);
14382 	rack_log_hpts_diag(rack, cts, &diag, &tv);
14383 }
14384 
14385 static int
rack_init_outstanding(struct tcpcb * tp,struct tcp_rack * rack,uint32_t us_cts,void * ptr)14386 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr)
14387 {
14388 	struct rack_sendmap *rsm, *ersm;
14389 	int insret __diagused;
14390 	/*
14391 	 * When initing outstanding, we must be quite careful
14392 	 * to not refer to tp->t_fb_ptr. This has the old rack
14393 	 * pointer in it, not the "new" one (when we are doing
14394 	 * a stack switch).
14395 	 */
14396 
14397 
14398 	if (tp->t_fb->tfb_chg_query == NULL) {
14399 		/* Create a send map for the current outstanding data */
14400 
14401 		rsm = rack_alloc(rack);
14402 		if (rsm == NULL) {
14403 			uma_zfree(rack_pcb_zone, ptr);
14404 			return (ENOMEM);
14405 		}
14406 		rsm->r_no_rtt_allowed = 1;
14407 		rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
14408 		rsm->r_rtr_cnt = 1;
14409 		rsm->r_rtr_bytes = 0;
14410 		if (tp->t_flags & TF_SENTFIN)
14411 			rsm->r_flags |= RACK_HAS_FIN;
14412 		rsm->r_end = tp->snd_max;
14413 		if (tp->snd_una == tp->iss) {
14414 			/* The data space is one beyond snd_una */
14415 			rsm->r_flags |= RACK_HAS_SYN;
14416 			rsm->r_start = tp->iss;
14417 			rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
14418 		} else
14419 			rsm->r_start = tp->snd_una;
14420 		rsm->r_dupack = 0;
14421 		if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
14422 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
14423 			if (rsm->m) {
14424 				rsm->orig_m_len = rsm->m->m_len;
14425 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14426 			} else {
14427 				rsm->orig_m_len = 0;
14428 				rsm->orig_t_space = 0;
14429 			}
14430 		} else {
14431 			/*
14432 			 * This can happen if we have a stand-alone FIN or
14433 			 *  SYN.
14434 			 */
14435 			rsm->m = NULL;
14436 			rsm->orig_m_len = 0;
14437 			rsm->orig_t_space = 0;
14438 			rsm->soff = 0;
14439 		}
14440 #ifdef INVARIANTS
14441 		if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14442 			panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
14443 			      insret, rack, rsm);
14444 		}
14445 #else
14446 		(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14447 #endif
14448 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14449 		rsm->r_in_tmap = 1;
14450 	} else {
14451 		/* We have a query mechanism, lets use it */
14452 		struct tcp_query_resp qr;
14453 		int i;
14454 		tcp_seq at;
14455 
14456 		at = tp->snd_una;
14457 		while (at != tp->snd_max) {
14458 			memset(&qr, 0, sizeof(qr));
14459 			qr.req = TCP_QUERY_SENDMAP;
14460 			qr.req_param = at;
14461 			if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0)
14462 				break;
14463 			/* Move forward */
14464 			at = qr.sendmap_end;
14465 			/* Now lets build the entry for this one */
14466 			rsm = rack_alloc(rack);
14467 			if (rsm == NULL) {
14468 				uma_zfree(rack_pcb_zone, ptr);
14469 				return (ENOMEM);
14470 			}
14471 			memset(rsm, 0, sizeof(struct rack_sendmap));
14472 			/* Now configure the rsm and insert it */
14473 			rsm->r_dupack = qr.sendmap_dupacks;
14474 			rsm->r_start = qr.sendmap_start;
14475 			rsm->r_end = qr.sendmap_end;
14476 			if (qr.sendmap_fas)
14477 				rsm->r_fas = qr.sendmap_end;
14478 			else
14479 				rsm->r_fas = rsm->r_start - tp->snd_una;
14480 			/*
14481 			 * We have carefully aligned the bits
14482 			 * so that all we have to do is copy over
14483 			 * the bits with the mask.
14484 			 */
14485 			rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK;
14486 			rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes;
14487 			rsm->r_rtr_cnt = qr.sendmap_send_cnt;
14488 			rsm->r_ack_arrival = qr.sendmap_ack_arrival;
14489 			for (i=0 ; i<rsm->r_rtr_cnt; i++)
14490 				rsm->r_tim_lastsent[i]	= qr.sendmap_time[i];
14491 			rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
14492 					   (rsm->r_start - tp->snd_una), &rsm->soff);
14493 			if (rsm->m) {
14494 				rsm->orig_m_len = rsm->m->m_len;
14495 				rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14496 			} else {
14497 				rsm->orig_m_len = 0;
14498 				rsm->orig_t_space = 0;
14499 			}
14500 #ifdef INVARIANTS
14501 			if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14502 				panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p",
14503 				      insret, rack, rsm);
14504 			}
14505 #else
14506 			(void)tqhash_insert(rack->r_ctl.tqh, rsm);
14507 #endif
14508 			if ((rsm->r_flags & RACK_ACKED) == 0)  {
14509 				TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) {
14510 					if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] >
14511 					    rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) {
14512 						/*
14513 						 * If the existing ersm was sent at
14514 						 * a later time than the new one, then
14515 						 * the new one should appear ahead of this
14516 						 * ersm.
14517 						 */
14518 						rsm->r_in_tmap = 1;
14519 						TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext);
14520 						break;
14521 					}
14522 				}
14523 				if (rsm->r_in_tmap == 0) {
14524 					/*
14525 					 * Not found so shove it on the tail.
14526 					 */
14527 					TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14528 					rsm->r_in_tmap = 1;
14529 				}
14530  			} else {
14531 				if ((rack->r_ctl.rc_sacklast == NULL) ||
14532 				    (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) {
14533 					rack->r_ctl.rc_sacklast = rsm;
14534 				}
14535 			}
14536 			rack_log_chg_info(tp, rack, 3,
14537 					  rsm->r_start,
14538 					  rsm->r_end,
14539 					  rsm->r_flags);
14540 		}
14541 	}
14542 	return (0);
14543 }
14544 
14545 
14546 static int32_t
rack_init(struct tcpcb * tp,void ** ptr)14547 rack_init(struct tcpcb *tp, void **ptr)
14548 {
14549 	struct inpcb *inp = tptoinpcb(tp);
14550 	struct tcp_rack *rack = NULL;
14551 	uint32_t iwin, snt, us_cts;
14552 	size_t sz;
14553 	int err, no_query;
14554 
14555 	tcp_hpts_init(tp);
14556 
14557 	/*
14558 	 * First are we the initial or are we a switched stack?
14559 	 * If we are initing via tcp_newtcppcb the ptr passed
14560 	 * will be tp->t_fb_ptr. If its a stack switch that
14561 	 * has a previous stack we can query it will be a local
14562 	 * var that will in the end be set into t_fb_ptr.
14563 	 */
14564 	if (ptr == &tp->t_fb_ptr)
14565 		no_query = 1;
14566 	else
14567 		no_query = 0;
14568 	*ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
14569 	if (*ptr == NULL) {
14570 		/*
14571 		 * We need to allocate memory but cant. The INP and INP_INFO
14572 		 * locks and they are recursive (happens during setup. So a
14573 		 * scheme to drop the locks fails :(
14574 		 *
14575 		 */
14576 		return(ENOMEM);
14577 	}
14578 	memset(*ptr, 0, sizeof(struct tcp_rack));
14579 	rack = (struct tcp_rack *)*ptr;
14580 	rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT);
14581 	if (rack->r_ctl.tqh == NULL) {
14582 		uma_zfree(rack_pcb_zone, rack);
14583 		return(ENOMEM);
14584 	}
14585 	tqhash_init(rack->r_ctl.tqh);
14586 	TAILQ_INIT(&rack->r_ctl.rc_free);
14587 	TAILQ_INIT(&rack->r_ctl.rc_tmap);
14588 	rack->rc_tp = tp;
14589 	rack->rc_inp = inp;
14590 	/* Set the flag */
14591 	rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
14592 	/* Probably not needed but lets be sure */
14593 	rack_clear_rate_sample(rack);
14594 	/*
14595 	 * Save off the default values, socket options will poke
14596 	 * at these if pacing is not on or we have not yet
14597 	 * reached where pacing is on (gp_ready/fixed enabled).
14598 	 * When they get set into the CC module (when gp_ready
14599 	 * is enabled or we enable fixed) then we will set these
14600 	 * values into the CC and place in here the old values
14601 	 * so we have a restoral. Then we will set the flag
14602 	 * rc_pacing_cc_set. That way whenever we turn off pacing
14603 	 * or switch off this stack, we will know to go restore
14604 	 * the saved values.
14605 	 *
14606 	 * We specifically put into the beta the ecn value for pacing.
14607 	 */
14608 	rack->rc_new_rnd_needed = 1;
14609 	rack->r_ctl.rc_split_limit = V_tcp_map_split_limit;
14610 	/* We want abe like behavior as well */
14611 
14612 	rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
14613 	rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
14614 	rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
14615 	if (rack_fill_cw_state)
14616 		rack->rc_pace_to_cwnd = 1;
14617 	if (rack_pacing_min_seg)
14618 		rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg;
14619 	if (use_rack_rr)
14620 		rack->use_rack_rr = 1;
14621 	if (rack_dnd_default) {
14622 		rack->rc_pace_dnd = 1;
14623 	}
14624 	if (V_tcp_delack_enabled)
14625 		tp->t_delayed_ack = 1;
14626 	else
14627 		tp->t_delayed_ack = 0;
14628 #ifdef TCP_ACCOUNTING
14629 	if (rack_tcp_accounting) {
14630 		tp->t_flags2 |= TF2_TCP_ACCOUNTING;
14631 	}
14632 #endif
14633 	rack->r_ctl.pcm_i.cnt_alloc = RACK_DEFAULT_PCM_ARRAY;
14634 	sz = (sizeof(struct rack_pcm_stats) * rack->r_ctl.pcm_i.cnt_alloc);
14635 	rack->r_ctl.pcm_s = malloc(sz,M_TCPPCM, M_NOWAIT);
14636 	if (rack->r_ctl.pcm_s == NULL) {
14637 		rack->r_ctl.pcm_i.cnt_alloc = 0;
14638 	}
14639 #ifdef NETFLIX_STATS
14640 	rack->r_ctl.side_chan_dis_mask = tcp_sidechannel_disable_mask;
14641 #endif
14642 	rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
14643 	rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
14644 	if (rack_enable_shared_cwnd)
14645 		rack->rack_enable_scwnd = 1;
14646 	rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
14647 	rack->rc_user_set_max_segs = rack_hptsi_segments;
14648 	rack->r_ctl.max_reduction = rack_max_reduce;
14649 	rack->rc_force_max_seg = 0;
14650 	TAILQ_INIT(&rack->r_ctl.opt_list);
14651 	rack->r_ctl.rc_saved_beta = V_newreno_beta_ecn;
14652 	rack->r_ctl.rc_saved_beta_ecn = V_newreno_beta_ecn;
14653 	if (rack_hibeta_setting) {
14654 		rack->rack_hibeta = 1;
14655 		if ((rack_hibeta_setting >= 50) &&
14656 		    (rack_hibeta_setting <= 100)) {
14657 			rack->r_ctl.rc_saved_beta = rack_hibeta_setting;
14658 			rack->r_ctl.saved_hibeta = rack_hibeta_setting;
14659 		}
14660 	} else {
14661 		rack->r_ctl.saved_hibeta = 50;
14662 	}
14663 	/*
14664 	 * We initialize to all ones so we never match 0
14665 	 * just in case the client sends in 0, it hopefully
14666 	 * will never have all 1's in ms :-)
14667 	 */
14668 	rack->r_ctl.last_tm_mark = 0xffffffffffffffff;
14669 	rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
14670 	rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
14671 	rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
14672 	rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
14673 	rack->r_ctl.rc_highest_us_rtt = 0;
14674 	rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
14675 	rack->pcm_enabled = rack_pcm_is_enabled;
14676 	if (rack_fillcw_bw_cap)
14677 		rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap;
14678 	rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
14679 	if (rack_use_cmp_acks)
14680 		rack->r_use_cmp_ack = 1;
14681 	if (rack_disable_prr)
14682 		rack->rack_no_prr = 1;
14683 	if (rack_gp_no_rec_chg)
14684 		rack->rc_gp_no_rec_chg = 1;
14685 	if (rack_pace_every_seg && tcp_can_enable_pacing()) {
14686 		rack->r_ctl.pacing_method |= RACK_REG_PACING;
14687 		rack->rc_always_pace = 1;
14688 		if (rack->rack_hibeta)
14689 			rack_set_cc_pacing(rack);
14690 	} else
14691 		rack->rc_always_pace = 0;
14692 	if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
14693 		rack->r_mbuf_queue = 1;
14694 	else
14695 		rack->r_mbuf_queue = 0;
14696 	rack_set_pace_segments(tp, rack, __LINE__, NULL);
14697 	if (rack_limits_scwnd)
14698 		rack->r_limit_scw = 1;
14699 	else
14700 		rack->r_limit_scw = 0;
14701 	rack_init_retransmit_value(rack, rack_rxt_controls);
14702 	rack->rc_labc = V_tcp_abc_l_var;
14703 	if (rack_honors_hpts_min_to)
14704 		rack->r_use_hpts_min = 1;
14705 	if (tp->snd_una != 0) {
14706 		rack->rc_sendvars_notset = 0;
14707 		/*
14708 		 * Make sure any TCP timers are not running.
14709 		 */
14710 		tcp_timer_stop(tp);
14711 	} else {
14712 		/*
14713 		 * Server side, we are called from the
14714 		 * syn-cache. This means none of the
14715 		 * snd_una/max are set yet so we have
14716 		 * to defer this until the first send.
14717 		 */
14718 		rack->rc_sendvars_notset = 1;
14719 	}
14720 
14721 	rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
14722 	rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
14723 	rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
14724 	rack->r_ctl.rc_min_to = rack_min_to;
14725 	microuptime(&rack->r_ctl.act_rcv_time);
14726 	rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
14727 	rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
14728 	if (rack_hw_up_only)
14729 		rack->r_up_only = 1;
14730 	if (rack_do_dyn_mul) {
14731 		/* When dynamic adjustment is on CA needs to start at 100% */
14732 		rack->rc_gp_dyn_mul = 1;
14733 		if (rack_do_dyn_mul >= 100)
14734 			rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
14735 	} else
14736 		rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
14737 	rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
14738 	if (rack_timely_off) {
14739 		rack->rc_skip_timely = 1;
14740 	}
14741 	if (rack->rc_skip_timely) {
14742 		rack->r_ctl.rack_per_of_gp_rec = 90;
14743 		rack->r_ctl.rack_per_of_gp_ca = 100;
14744 		rack->r_ctl.rack_per_of_gp_ss = 250;
14745 	}
14746 	rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
14747 	rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
14748 	rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
14749 
14750 	setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
14751 				rack_probertt_filter_life);
14752 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14753 	rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
14754 	rack->r_ctl.rc_time_of_last_probertt = us_cts;
14755 	rack->r_ctl.rc_went_idle_time = us_cts;
14756 	rack->r_ctl.rc_time_probertt_starts = 0;
14757 
14758 	rack->r_ctl.gp_rnd_thresh = rack_rnd_cnt_req & 0xff;
14759 	if (rack_rnd_cnt_req  & 0x10000)
14760 		rack->r_ctl.gate_to_fs = 1;
14761 	rack->r_ctl.gp_gain_req = rack_gp_gain_req;
14762 	if ((rack_rnd_cnt_req & 0x100) > 0) {
14763 
14764 	}
14765 	if (rack_dsack_std_based & 0x1) {
14766 		/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
14767 		rack->rc_rack_tmr_std_based = 1;
14768 	}
14769 	if (rack_dsack_std_based & 0x2) {
14770 		/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
14771 		rack->rc_rack_use_dsack = 1;
14772 	}
14773 	/* We require at least one measurement, even if the sysctl is 0 */
14774 	if (rack_req_measurements)
14775 		rack->r_ctl.req_measurements = rack_req_measurements;
14776 	else
14777 		rack->r_ctl.req_measurements = 1;
14778 	if (rack_enable_hw_pacing)
14779 		rack->rack_hdw_pace_ena = 1;
14780 	if (rack_hw_rate_caps)
14781 		rack->r_rack_hw_rate_caps = 1;
14782 	if (rack_non_rxt_use_cr)
14783 		rack->rack_rec_nonrxt_use_cr = 1;
14784 	/* Lets setup the fsb block */
14785 	err = rack_init_fsb(tp, rack);
14786 	if (err) {
14787 		uma_zfree(rack_pcb_zone, *ptr);
14788 		*ptr = NULL;
14789 		return (err);
14790 	}
14791 	if (rack_do_hystart) {
14792 		tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
14793 		if (rack_do_hystart > 1)
14794 			tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
14795 		if (rack_do_hystart > 2)
14796 			tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
14797 	}
14798 	/* Log what we will do with queries */
14799 	rack_log_chg_info(tp, rack, 7,
14800 			  no_query, 0, 0);
14801 	if (rack_def_profile)
14802 		rack_set_profile(rack, rack_def_profile);
14803 	/* Cancel the GP measurement in progress */
14804 	tp->t_flags &= ~TF_GPUTINPROG;
14805 	if ((tp->t_state != TCPS_CLOSED) &&
14806 	    (tp->t_state != TCPS_TIME_WAIT)) {
14807 		/*
14808 		 * We are already open, we may
14809 		 * need to adjust a few things.
14810 		 */
14811 		if (SEQ_GT(tp->snd_max, tp->iss))
14812 			snt = tp->snd_max - tp->iss;
14813 		else
14814 			snt = 0;
14815 		iwin = rc_init_window(rack);
14816 		if ((snt < iwin) &&
14817 		    (no_query == 1)) {
14818 			/* We are not past the initial window
14819 			 * on the first init (i.e. a stack switch
14820 			 * has not yet occured) so we need to make
14821 			 * sure cwnd and ssthresh is correct.
14822 			 */
14823 			if (tp->snd_cwnd < iwin)
14824 				tp->snd_cwnd = iwin;
14825 			/*
14826 			 * If we are within the initial window
14827 			 * we want ssthresh to be unlimited. Setting
14828 			 * it to the rwnd (which the default stack does
14829 			 * and older racks) is not really a good idea
14830 			 * since we want to be in SS and grow both the
14831 			 * cwnd and the rwnd (via dynamic rwnd growth). If
14832 			 * we set it to the rwnd then as the peer grows its
14833 			 * rwnd we will be stuck in CA and never hit SS.
14834 			 *
14835 			 * Its far better to raise it up high (this takes the
14836 			 * risk that there as been a loss already, probably
14837 			 * we should have an indicator in all stacks of loss
14838 			 * but we don't), but considering the normal use this
14839 			 * is a risk worth taking. The consequences of not
14840 			 * hitting SS are far worse than going one more time
14841 			 * into it early on (before we have sent even a IW).
14842 			 * It is highly unlikely that we will have had a loss
14843 			 * before getting the IW out.
14844 			 */
14845 			tp->snd_ssthresh = 0xffffffff;
14846 		}
14847 		/*
14848 		 * Any init based on sequence numbers
14849 		 * should be done in the deferred init path
14850 		 * since we can be CLOSED and not have them
14851 		 * inited when rack_init() is called. We
14852 		 * are not closed so lets call it.
14853 		 */
14854 		rack_deferred_init(tp, rack);
14855 	}
14856 	if ((tp->t_state != TCPS_CLOSED) &&
14857 	    (tp->t_state != TCPS_TIME_WAIT) &&
14858 	    (no_query == 0) &&
14859 	    (tp->snd_una != tp->snd_max))  {
14860 		err = rack_init_outstanding(tp, rack, us_cts, *ptr);
14861 		if (err) {
14862 			*ptr = NULL;
14863 			return(err);
14864 		}
14865 	}
14866 	rack_stop_all_timers(tp, rack);
14867 	/* Setup all the t_flags2 */
14868 	if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14869 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14870 	else
14871 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14872 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14873 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
14874 	/*
14875 	 * Timers in Rack are kept in microseconds so lets
14876 	 * convert any initial incoming variables
14877 	 * from ticks into usecs. Note that we
14878 	 * also change the values of t_srtt and t_rttvar, if
14879 	 * they are non-zero. They are kept with a 5
14880 	 * bit decimal so we have to carefully convert
14881 	 * these to get the full precision.
14882 	 */
14883 	rack_convert_rtts(tp);
14884 	rack_log_hystart_event(rack, rack->r_ctl.roundends, 20);
14885 	if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) {
14886 		/* We do not start any timers on DROPPED connections */
14887 		if (tp->t_fb->tfb_chg_query == NULL) {
14888 			rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14889 		} else {
14890 			struct tcp_query_resp qr;
14891 			int ret;
14892 
14893 			memset(&qr, 0, sizeof(qr));
14894 
14895 			/* Get the misc time stamps and such for rack */
14896 			qr.req = TCP_QUERY_RACK_TIMES;
14897 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
14898 			if (ret == 1) {
14899 				rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts;
14900 				rack->r_ctl.num_dsack  = qr.rack_num_dsacks;
14901 				rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time;
14902 				rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt;
14903 				rack->rc_rack_rtt = qr.rack_rtt;
14904 				rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time;
14905 				rack->r_ctl.rc_sacked = qr.rack_sacked;
14906 				rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt;
14907 				rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered;
14908 				rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs;
14909 				rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt;
14910 				rack->r_ctl.rc_prr_out = qr.rack_prr_out;
14911 				if (qr.rack_tlp_out) {
14912 					rack->rc_tlp_in_progress = 1;
14913 					rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out;
14914 				} else {
14915 					rack->rc_tlp_in_progress = 0;
14916 					rack->r_ctl.rc_tlp_cnt_out = 0;
14917 				}
14918 				if (qr.rack_srtt_measured)
14919 					rack->rc_srtt_measure_made = 1;
14920 				if (qr.rack_in_persist == 1) {
14921 					rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle;
14922 #ifdef NETFLIX_SHARED_CWND
14923 					if (rack->r_ctl.rc_scw) {
14924 						tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
14925 						rack->rack_scwnd_is_idle = 1;
14926 					}
14927 #endif
14928 					rack->r_ctl.persist_lost_ends = 0;
14929 					rack->probe_not_answered = 0;
14930 					rack->forced_ack = 0;
14931 					tp->t_rxtshift = 0;
14932 					rack->rc_in_persist = 1;
14933 					RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
14934 							   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
14935 				}
14936 				if (qr.rack_wanted_output)
14937 					rack->r_wanted_output = 1;
14938 				rack_log_chg_info(tp, rack, 6,
14939 						  qr.rack_min_rtt,
14940 						  qr.rack_rtt,
14941 						  qr.rack_reorder_ts);
14942 			}
14943 			/* Get the old stack timers */
14944 			qr.req_param = 0;
14945 			qr.req = TCP_QUERY_TIMERS_UP;
14946 			ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
14947 			if (ret) {
14948 				/*
14949 				 * non-zero return means we have a timer('s)
14950 				 * to start. Zero means no timer (no keepalive
14951 				 * I suppose).
14952 				 */
14953 				uint32_t tov = 0;
14954 
14955 				rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags;
14956 				if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) {
14957 					rack->r_ctl.rc_last_output_to = qr.timer_pacing_to;
14958 					if (TSTMP_GT(qr.timer_pacing_to, us_cts))
14959 						tov = qr.timer_pacing_to - us_cts;
14960 					else
14961 						tov = HPTS_TICKS_PER_SLOT;
14962 				}
14963 				if (qr.timer_hpts_flags & PACE_TMR_MASK) {
14964 					rack->r_ctl.rc_timer_exp = qr.timer_timer_exp;
14965 					if (tov == 0) {
14966 						if (TSTMP_GT(qr.timer_timer_exp, us_cts))
14967 							tov = qr.timer_timer_exp - us_cts;
14968 						else
14969 							tov = HPTS_TICKS_PER_SLOT;
14970 					}
14971 				}
14972 				rack_log_chg_info(tp, rack, 4,
14973 						  rack->r_ctl.rc_hpts_flags,
14974 						  rack->r_ctl.rc_last_output_to,
14975 						  rack->r_ctl.rc_timer_exp);
14976 				if (tov) {
14977 					struct hpts_diag diag;
14978 
14979 					(void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov),
14980 								   __LINE__, &diag);
14981 					rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time);
14982 				}
14983 			}
14984 		}
14985 		rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
14986 				     __LINE__, RACK_RTTS_INIT);
14987 	}
14988 	return (0);
14989 }
14990 
14991 static int
rack_handoff_ok(struct tcpcb * tp)14992 rack_handoff_ok(struct tcpcb *tp)
14993 {
14994 	if ((tp->t_state == TCPS_CLOSED) ||
14995 	    (tp->t_state == TCPS_LISTEN)) {
14996 		/* Sure no problem though it may not stick */
14997 		return (0);
14998 	}
14999 	if ((tp->t_state == TCPS_SYN_SENT) ||
15000 	    (tp->t_state == TCPS_SYN_RECEIVED)) {
15001 		/*
15002 		 * We really don't know if you support sack,
15003 		 * you have to get to ESTAB or beyond to tell.
15004 		 */
15005 		return (EAGAIN);
15006 	}
15007 	if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
15008 		/*
15009 		 * Rack will only send a FIN after all data is acknowledged.
15010 		 * So in this case we have more data outstanding. We can't
15011 		 * switch stacks until either all data and only the FIN
15012 		 * is left (in which case rack_init() now knows how
15013 		 * to deal with that) <or> all is acknowledged and we
15014 		 * are only left with incoming data, though why you
15015 		 * would want to switch to rack after all data is acknowledged
15016 		 * I have no idea (rrs)!
15017 		 */
15018 		return (EAGAIN);
15019 	}
15020 	if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
15021 		return (0);
15022 	}
15023 	/*
15024 	 * If we reach here we don't do SACK on this connection so we can
15025 	 * never do rack.
15026 	 */
15027 	return (EINVAL);
15028 }
15029 
15030 static void
rack_fini(struct tcpcb * tp,int32_t tcb_is_purged)15031 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
15032 {
15033 
15034 	if (tp->t_fb_ptr) {
15035 		uint32_t cnt_free = 0;
15036 		struct tcp_rack *rack;
15037 		struct rack_sendmap *rsm;
15038 
15039 		tcp_handle_orphaned_packets(tp);
15040 		tp->t_flags &= ~TF_FORCEDATA;
15041 		rack = (struct tcp_rack *)tp->t_fb_ptr;
15042 		rack_log_pacing_delay_calc(rack,
15043 					   0,
15044 					   0,
15045 					   0,
15046 					   rack_get_gp_est(rack), /* delRate */
15047 					   rack_get_lt_bw(rack), /* rttProp */
15048 					   20, __LINE__, NULL, 0);
15049 #ifdef NETFLIX_SHARED_CWND
15050 		if (rack->r_ctl.rc_scw) {
15051 			uint32_t limit;
15052 
15053 			if (rack->r_limit_scw)
15054 				limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
15055 			else
15056 				limit = 0;
15057 			tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
15058 						  rack->r_ctl.rc_scw_index,
15059 						  limit);
15060 			rack->r_ctl.rc_scw = NULL;
15061 		}
15062 #endif
15063 		if (rack->r_ctl.fsb.tcp_ip_hdr) {
15064 			free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
15065 			rack->r_ctl.fsb.tcp_ip_hdr = NULL;
15066 			rack->r_ctl.fsb.th = NULL;
15067 		}
15068 		if (rack->rc_always_pace == 1) {
15069 			rack_remove_pacing(rack);
15070 		}
15071 		/* Clean up any options if they were not applied */
15072 		while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
15073 			struct deferred_opt_list *dol;
15074 
15075 			dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
15076 			TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
15077 			free(dol, M_TCPDO);
15078 		}
15079 		/* rack does not use force data but other stacks may clear it */
15080 		if (rack->r_ctl.crte != NULL) {
15081 			tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
15082 			rack->rack_hdrw_pacing = 0;
15083 			rack->r_ctl.crte = NULL;
15084 		}
15085 #ifdef TCP_BLACKBOX
15086 		tcp_log_flowend(tp);
15087 #endif
15088 		/*
15089 		 * Lets take a different approach to purging just
15090 		 * get each one and free it like a cum-ack would and
15091 		 * not use a foreach loop.
15092 		 */
15093 		rsm = tqhash_min(rack->r_ctl.tqh);
15094 		while (rsm) {
15095 			tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
15096 			rack->r_ctl.rc_num_maps_alloced--;
15097 			uma_zfree(rack_zone, rsm);
15098 			rsm = tqhash_min(rack->r_ctl.tqh);
15099 		}
15100 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15101 		while (rsm) {
15102 			TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
15103 			rack->r_ctl.rc_num_maps_alloced--;
15104 			rack->rc_free_cnt--;
15105 			cnt_free++;
15106 			uma_zfree(rack_zone, rsm);
15107 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15108 		}
15109 		if (rack->r_ctl.pcm_s != NULL) {
15110 			free(rack->r_ctl.pcm_s, M_TCPPCM);
15111 			rack->r_ctl.pcm_s = NULL;
15112 			rack->r_ctl.pcm_i.cnt_alloc = 0;
15113 			rack->r_ctl.pcm_i.cnt = 0;
15114 		}
15115 		if ((rack->r_ctl.rc_num_maps_alloced > 0) &&
15116 		    (tcp_bblogging_on(tp))) {
15117 			union tcp_log_stackspecific log;
15118 			struct timeval tv;
15119 
15120 			memset(&log, 0, sizeof(log));
15121 			log.u_bbr.flex8 = 10;
15122 			log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced;
15123 			log.u_bbr.flex2 = rack->rc_free_cnt;
15124 			log.u_bbr.flex3 = cnt_free;
15125 			log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15126 			rsm = tqhash_min(rack->r_ctl.tqh);
15127 			log.u_bbr.delRate = (uintptr_t)rsm;
15128 			rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15129 			log.u_bbr.cur_del_rate = (uintptr_t)rsm;
15130 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15131 			log.u_bbr.pkt_epoch = __LINE__;
15132 			(void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
15133 					     0, &log, false, NULL, NULL, 0, &tv);
15134 		}
15135 		KASSERT((rack->r_ctl.rc_num_maps_alloced == 0),
15136 			("rack:%p num_aloc:%u after freeing all?",
15137 			 rack,
15138 			 rack->r_ctl.rc_num_maps_alloced));
15139 		rack->rc_free_cnt = 0;
15140 		free(rack->r_ctl.tqh, M_TCPFSB);
15141 		rack->r_ctl.tqh = NULL;
15142 		uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
15143 		tp->t_fb_ptr = NULL;
15144 	}
15145 	/* Make sure snd_nxt is correctly set */
15146 	tp->snd_nxt = tp->snd_max;
15147 }
15148 
15149 static void
rack_set_state(struct tcpcb * tp,struct tcp_rack * rack)15150 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
15151 {
15152 	if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
15153 		rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0;
15154 	}
15155 	switch (tp->t_state) {
15156 	case TCPS_SYN_SENT:
15157 		rack->r_state = TCPS_SYN_SENT;
15158 		rack->r_substate = rack_do_syn_sent;
15159 		break;
15160 	case TCPS_SYN_RECEIVED:
15161 		rack->r_state = TCPS_SYN_RECEIVED;
15162 		rack->r_substate = rack_do_syn_recv;
15163 		break;
15164 	case TCPS_ESTABLISHED:
15165 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15166 		rack->r_state = TCPS_ESTABLISHED;
15167 		rack->r_substate = rack_do_established;
15168 		break;
15169 	case TCPS_CLOSE_WAIT:
15170 		rack->r_state = TCPS_CLOSE_WAIT;
15171 		rack->r_substate = rack_do_close_wait;
15172 		break;
15173 	case TCPS_FIN_WAIT_1:
15174 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15175 		rack->r_state = TCPS_FIN_WAIT_1;
15176 		rack->r_substate = rack_do_fin_wait_1;
15177 		break;
15178 	case TCPS_CLOSING:
15179 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15180 		rack->r_state = TCPS_CLOSING;
15181 		rack->r_substate = rack_do_closing;
15182 		break;
15183 	case TCPS_LAST_ACK:
15184 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
15185 		rack->r_state = TCPS_LAST_ACK;
15186 		rack->r_substate = rack_do_lastack;
15187 		break;
15188 	case TCPS_FIN_WAIT_2:
15189 		rack->r_state = TCPS_FIN_WAIT_2;
15190 		rack->r_substate = rack_do_fin_wait_2;
15191 		break;
15192 	case TCPS_LISTEN:
15193 	case TCPS_CLOSED:
15194 	case TCPS_TIME_WAIT:
15195 	default:
15196 		break;
15197 	};
15198 	if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15199 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
15200 
15201 }
15202 
15203 static void
rack_timer_audit(struct tcpcb * tp,struct tcp_rack * rack,struct sockbuf * sb)15204 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
15205 {
15206 	/*
15207 	 * We received an ack, and then did not
15208 	 * call send or were bounced out due to the
15209 	 * hpts was running. Now a timer is up as well, is
15210 	 * it the right timer?
15211 	 */
15212 	struct rack_sendmap *rsm;
15213 	int tmr_up;
15214 
15215 	tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
15216 	if (tcp_in_hpts(rack->rc_tp) == 0) {
15217 		/*
15218 		 * Ok we probably need some timer up, but no
15219 		 * matter what the mask we are not in hpts. We
15220 		 * may have received an old ack and thus did nothing.
15221 		 */
15222 		rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15223 		rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15224 		return;
15225 	}
15226 	if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
15227 		return;
15228 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
15229 	if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
15230 	    (tmr_up == PACE_TMR_RXT)) {
15231 		/* Should be an RXT */
15232 		return;
15233 	}
15234 	if (rsm == NULL) {
15235 		/* Nothing outstanding? */
15236 		if (tp->t_flags & TF_DELACK) {
15237 			if (tmr_up == PACE_TMR_DELACK)
15238 				/* We are supposed to have delayed ack up and we do */
15239 				return;
15240 		} else if (((V_tcp_always_keepalive ||
15241 			     rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
15242 			    (tp->t_state <= TCPS_CLOSING)) &&
15243 			   (tmr_up == PACE_TMR_KEEP) &&
15244 			   (tp->snd_max == tp->snd_una)) {
15245 			/* We should have keep alive up and we do */
15246 			return;
15247 		}
15248 	}
15249 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
15250 		   ((tmr_up == PACE_TMR_TLP) ||
15251 		    (tmr_up == PACE_TMR_RACK) ||
15252 		    (tmr_up == PACE_TMR_RXT))) {
15253 		/*
15254 		 * Either a Rack, TLP or RXT is fine if  we
15255 		 * have outstanding data.
15256 		 */
15257 		return;
15258 	} else if (tmr_up == PACE_TMR_DELACK) {
15259 		/*
15260 		 * If the delayed ack was going to go off
15261 		 * before the rtx/tlp/rack timer were going to
15262 		 * expire, then that would be the timer in control.
15263 		 * Note we don't check the time here trusting the
15264 		 * code is correct.
15265 		 */
15266 		return;
15267 	}
15268 	/*
15269 	 * Ok the timer originally started is not what we want now.
15270 	 * We will force the hpts to be stopped if any, and restart
15271 	 * with the slot set to what was in the saved slot.
15272 	 */
15273 	if (tcp_in_hpts(rack->rc_tp)) {
15274 		if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15275 			uint32_t us_cts;
15276 
15277 			us_cts = tcp_get_usecs(NULL);
15278 			if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
15279 				rack->r_early = 1;
15280 				rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
15281 			}
15282 			rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
15283 		}
15284 		tcp_hpts_remove(rack->rc_tp);
15285 	}
15286 	rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15287 	rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15288 }
15289 
15290 
15291 static void
rack_do_win_updates(struct tcpcb * tp,struct tcp_rack * rack,uint32_t tiwin,uint32_t seq,uint32_t ack,uint32_t cts)15292 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts)
15293 {
15294 	if ((SEQ_LT(tp->snd_wl1, seq) ||
15295 	    (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
15296 	    (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
15297 		/* keep track of pure window updates */
15298 		if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
15299 			KMOD_TCPSTAT_INC(tcps_rcvwinupd);
15300 		tp->snd_wnd = tiwin;
15301 		rack_validate_fo_sendwin_up(tp, rack);
15302 		tp->snd_wl1 = seq;
15303 		tp->snd_wl2 = ack;
15304 		if (tp->snd_wnd > tp->max_sndwnd)
15305 			tp->max_sndwnd = tp->snd_wnd;
15306 	    rack->r_wanted_output = 1;
15307 	} else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
15308 		tp->snd_wnd = tiwin;
15309 		rack_validate_fo_sendwin_up(tp, rack);
15310 		tp->snd_wl1 = seq;
15311 		tp->snd_wl2 = ack;
15312 	} else {
15313 		/* Not a valid win update */
15314 		return;
15315 	}
15316 	if (tp->snd_wnd > tp->max_sndwnd)
15317 		tp->max_sndwnd = tp->snd_wnd;
15318 	/* Do we exit persists? */
15319 	if ((rack->rc_in_persist != 0) &&
15320 	    (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
15321 				rack->r_ctl.rc_pace_min_segs))) {
15322 		rack_exit_persist(tp, rack, cts);
15323 	}
15324 	/* Do we enter persists? */
15325 	if ((rack->rc_in_persist == 0) &&
15326 	    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
15327 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
15328 	    ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
15329 	    sbavail(&tptosocket(tp)->so_snd) &&
15330 	    (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
15331 		/*
15332 		 * Here the rwnd is less than
15333 		 * the pacing size, we are established,
15334 		 * nothing is outstanding, and there is
15335 		 * data to send. Enter persists.
15336 		 */
15337 		rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack);
15338 	}
15339 }
15340 
15341 static void
rack_log_input_packet(struct tcpcb * tp,struct tcp_rack * rack,struct tcp_ackent * ae,int ackval,uint32_t high_seq)15342 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
15343 {
15344 
15345 	if (tcp_bblogging_on(rack->rc_tp)) {
15346 		struct inpcb *inp = tptoinpcb(tp);
15347 		union tcp_log_stackspecific log;
15348 		struct timeval ltv;
15349 		char tcp_hdr_buf[60];
15350 		struct tcphdr *th;
15351 		struct timespec ts;
15352 		uint32_t orig_snd_una;
15353 		uint8_t xx = 0;
15354 
15355 #ifdef TCP_REQUEST_TRK
15356 		struct tcp_sendfile_track *tcp_req;
15357 
15358 		if (SEQ_GT(ae->ack, tp->snd_una)) {
15359 			tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1));
15360 		} else {
15361 			tcp_req = tcp_req_find_req_for_seq(tp, ae->ack);
15362 		}
15363 #endif
15364 		memset(&log, 0, sizeof(log));
15365 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
15366 		if (rack->rack_no_prr == 0)
15367 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
15368 		else
15369 			log.u_bbr.flex1 = 0;
15370 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
15371 		log.u_bbr.use_lt_bw <<= 1;
15372 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
15373 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
15374 		log.u_bbr.bbr_state = rack->rc_free_cnt;
15375 		log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15376 		log.u_bbr.pkts_out = tp->t_maxseg;
15377 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
15378 		log.u_bbr.flex7 = 1;
15379 		log.u_bbr.lost = ae->flags;
15380 		log.u_bbr.cwnd_gain = ackval;
15381 		log.u_bbr.pacing_gain = 0x2;
15382 		if (ae->flags & TSTMP_HDWR) {
15383 			/* Record the hardware timestamp if present */
15384 			log.u_bbr.flex3 = M_TSTMP;
15385 			ts.tv_sec = ae->timestamp / 1000000000;
15386 			ts.tv_nsec = ae->timestamp % 1000000000;
15387 			ltv.tv_sec = ts.tv_sec;
15388 			ltv.tv_usec = ts.tv_nsec / 1000;
15389 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
15390 		} else if (ae->flags & TSTMP_LRO) {
15391 			/* Record the LRO the arrival timestamp */
15392 			log.u_bbr.flex3 = M_TSTMP_LRO;
15393 			ts.tv_sec = ae->timestamp / 1000000000;
15394 			ts.tv_nsec = ae->timestamp % 1000000000;
15395 			ltv.tv_sec = ts.tv_sec;
15396 			ltv.tv_usec = ts.tv_nsec / 1000;
15397 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
15398 		}
15399 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
15400 		/* Log the rcv time */
15401 		log.u_bbr.delRate = ae->timestamp;
15402 #ifdef TCP_REQUEST_TRK
15403 		log.u_bbr.applimited = tp->t_tcpreq_closed;
15404 		log.u_bbr.applimited <<= 8;
15405 		log.u_bbr.applimited |= tp->t_tcpreq_open;
15406 		log.u_bbr.applimited <<= 8;
15407 		log.u_bbr.applimited |= tp->t_tcpreq_req;
15408 		if (tcp_req) {
15409 			/* Copy out any client req info */
15410 			/* seconds */
15411 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
15412 			/* useconds */
15413 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
15414 			log.u_bbr.rttProp = tcp_req->timestamp;
15415 			log.u_bbr.cur_del_rate = tcp_req->start;
15416 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
15417 				log.u_bbr.flex8 |= 1;
15418 			} else {
15419 				log.u_bbr.flex8 |= 2;
15420 				log.u_bbr.bw_inuse = tcp_req->end;
15421 			}
15422 			log.u_bbr.flex6 = tcp_req->start_seq;
15423 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
15424 				log.u_bbr.flex8 |= 4;
15425 				log.u_bbr.epoch = tcp_req->end_seq;
15426 			}
15427 		}
15428 #endif
15429 		memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
15430 		th = (struct tcphdr *)tcp_hdr_buf;
15431 		th->th_seq = ae->seq;
15432 		th->th_ack = ae->ack;
15433 		th->th_win = ae->win;
15434 		/* Now fill in the ports */
15435 		th->th_sport = inp->inp_fport;
15436 		th->th_dport = inp->inp_lport;
15437 		tcp_set_flags(th, ae->flags);
15438 		/* Now do we have a timestamp option? */
15439 		if (ae->flags & HAS_TSTMP) {
15440 			u_char *cp;
15441 			uint32_t val;
15442 
15443 			th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
15444 			cp = (u_char *)(th + 1);
15445 			*cp = TCPOPT_NOP;
15446 			cp++;
15447 			*cp = TCPOPT_NOP;
15448 			cp++;
15449 			*cp = TCPOPT_TIMESTAMP;
15450 			cp++;
15451 			*cp = TCPOLEN_TIMESTAMP;
15452 			cp++;
15453 			val = htonl(ae->ts_value);
15454 			bcopy((char *)&val,
15455 			      (char *)cp, sizeof(uint32_t));
15456 			val = htonl(ae->ts_echo);
15457 			bcopy((char *)&val,
15458 			      (char *)(cp + 4), sizeof(uint32_t));
15459 		} else
15460 			th->th_off = (sizeof(struct tcphdr) >> 2);
15461 
15462 		/*
15463 		 * For sane logging we need to play a little trick.
15464 		 * If the ack were fully processed we would have moved
15465 		 * snd_una to high_seq, but since compressed acks are
15466 		 * processed in two phases, at this point (logging) snd_una
15467 		 * won't be advanced. So we would see multiple acks showing
15468 		 * the advancement. We can prevent that by "pretending" that
15469 		 * snd_una was advanced and then un-advancing it so that the
15470 		 * logging code has the right value for tlb_snd_una.
15471 		 */
15472 		if (tp->snd_una != high_seq) {
15473 			orig_snd_una = tp->snd_una;
15474 			tp->snd_una = high_seq;
15475 			xx = 1;
15476 		} else
15477 			xx = 0;
15478 		TCP_LOG_EVENTP(tp, th,
15479 			       &tptosocket(tp)->so_rcv,
15480 			       &tptosocket(tp)->so_snd, TCP_LOG_IN, 0,
15481 			       0, &log, true, &ltv);
15482 		if (xx) {
15483 			tp->snd_una = orig_snd_una;
15484 		}
15485 	}
15486 
15487 }
15488 
15489 static void
rack_handle_probe_response(struct tcp_rack * rack,uint32_t tiwin,uint32_t us_cts)15490 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
15491 {
15492 	uint32_t us_rtt;
15493 	/*
15494 	 * A persist or keep-alive was forced out, update our
15495 	 * min rtt time. Note now worry about lost responses.
15496 	 * When a subsequent keep-alive or persist times out
15497 	 * and forced_ack is still on, then the last probe
15498 	 * was not responded to. In such cases we have a
15499 	 * sysctl that controls the behavior. Either we apply
15500 	 * the rtt but with reduced confidence (0). Or we just
15501 	 * plain don't apply the rtt estimate. Having data flow
15502 	 * will clear the probe_not_answered flag i.e. cum-ack
15503 	 * move forward <or> exiting and reentering persists.
15504 	 */
15505 
15506 	rack->forced_ack = 0;
15507 	rack->rc_tp->t_rxtshift = 0;
15508 	if ((rack->rc_in_persist &&
15509 	     (tiwin == rack->rc_tp->snd_wnd)) ||
15510 	    (rack->rc_in_persist == 0)) {
15511 		/*
15512 		 * In persists only apply the RTT update if this is
15513 		 * a response to our window probe. And that
15514 		 * means the rwnd sent must match the current
15515 		 * snd_wnd. If it does not, then we got a
15516 		 * window update ack instead. For keepalive
15517 		 * we allow the answer no matter what the window.
15518 		 *
15519 		 * Note that if the probe_not_answered is set then
15520 		 * the forced_ack_ts is the oldest one i.e. the first
15521 		 * probe sent that might have been lost. This assures
15522 		 * us that if we do calculate an RTT it is longer not
15523 		 * some short thing.
15524 		 */
15525 		if (rack->rc_in_persist)
15526 			counter_u64_add(rack_persists_acks, 1);
15527 		us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
15528 		if (us_rtt == 0)
15529 			us_rtt = 1;
15530 		if (rack->probe_not_answered == 0) {
15531 			rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15532 			tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
15533 		} else {
15534 			/* We have a retransmitted probe here too */
15535 			if (rack_apply_rtt_with_reduced_conf) {
15536 				rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15537 				tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
15538 			}
15539 		}
15540 	}
15541 }
15542 
15543 static void
rack_new_round_starts(struct tcpcb * tp,struct tcp_rack * rack,uint32_t high_seq)15544 rack_new_round_starts(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
15545 {
15546 	/*
15547 	 * The next send has occurred mark the end of the round
15548 	 * as when that data gets acknowledged. We can
15549 	 * also do common things we might need to do when
15550 	 * a round begins.
15551 	 */
15552 	rack->r_ctl.roundends = tp->snd_max;
15553 	rack->rc_new_rnd_needed = 0;
15554 	rack_log_hystart_event(rack, tp->snd_max, 4);
15555 }
15556 
15557 
15558 static void
rack_log_pcm(struct tcp_rack * rack,uint8_t mod,uint32_t flex1,uint32_t flex2,uint32_t flex3)15559 rack_log_pcm(struct tcp_rack *rack, uint8_t mod, uint32_t flex1, uint32_t flex2,
15560 	     uint32_t flex3)
15561 {
15562 	if (tcp_bblogging_on(rack->rc_tp)) {
15563 		union tcp_log_stackspecific log;
15564 		struct timeval tv;
15565 
15566 		(void)tcp_get_usecs(&tv);
15567 		memset(&log, 0, sizeof(log));
15568 		log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv);
15569 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15570 		log.u_bbr.flex8 = mod;
15571 		log.u_bbr.flex1 = flex1;
15572 		log.u_bbr.flex2 = flex2;
15573 		log.u_bbr.flex3 = flex3;
15574 		log.u_bbr.flex4 = rack_pcm_every_n_rounds;
15575 		log.u_bbr.flex5 = rack->r_ctl.pcm_idle_rounds;
15576 		log.u_bbr.bbr_substate = rack->pcm_needed;
15577 		log.u_bbr.bbr_substate <<= 1;
15578 		log.u_bbr.bbr_substate |= rack->pcm_in_progress;
15579 		log.u_bbr.bbr_substate <<= 1;
15580 		log.u_bbr.bbr_substate |= rack->pcm_enabled; /* bits are NIE for Needed, Inprogress, Enabled */
15581 		(void)tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_PCM_MEASURE, ERRNO_UNK,
15582 				    0, &log, false, NULL, NULL, 0, &tv);
15583 	}
15584 }
15585 
15586 static void
rack_new_round_setup(struct tcpcb * tp,struct tcp_rack * rack,uint32_t high_seq)15587 rack_new_round_setup(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq)
15588 {
15589 	/*
15590 	 * The round (current_round) has ended. We now
15591 	 * setup for the next round by incrementing the
15592 	 * round numnber and doing any round specific
15593 	 * things.
15594 	 */
15595 	rack_log_hystart_event(rack, high_seq, 21);
15596 	rack->r_ctl.current_round++;
15597 	/* New round (current_round) begins at next send */
15598 	rack->rc_new_rnd_needed = 1;
15599 	if ((rack->pcm_enabled == 1) &&
15600 	    (rack->pcm_needed == 0) &&
15601 	    (rack->pcm_in_progress == 0)) {
15602 		/*
15603 		 * If we have enabled PCM, then we need to
15604 		 * check if the round has adanced to the state
15605 		 * where one is required.
15606 		 */
15607 		int rnds;
15608 
15609 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
15610 		if ((rnds + rack->r_ctl.pcm_idle_rounds) >= rack_pcm_every_n_rounds) {
15611 			rack->pcm_needed = 1;
15612 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
15613 		} else if (rack_verbose_logging) {
15614 			rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round );
15615 		}
15616 	}
15617 	if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
15618 		/* We have hystart enabled send the round info in */
15619 		if (CC_ALGO(tp)->newround != NULL) {
15620 			CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
15621 		}
15622 	}
15623 	/*
15624 	 * For DGP an initial startup check. We want to validate
15625 	 * that we are not just pushing on slow-start and just
15626 	 * not gaining.. i.e. filling buffers without getting any
15627 	 * boost in b/w during the inital slow-start.
15628 	 */
15629 	if (rack->dgp_on &&
15630 	    (rack->rc_initial_ss_comp == 0) &&
15631 	    (tp->snd_cwnd < tp->snd_ssthresh) &&
15632 	    (rack->r_ctl.num_measurements >= RACK_REQ_AVG) &&
15633 	    (rack->r_ctl.gp_rnd_thresh > 0) &&
15634 	    ((rack->r_ctl.current_round - rack->r_ctl.last_rnd_of_gp_rise) >= rack->r_ctl.gp_rnd_thresh)) {
15635 
15636 		/*
15637 		 * We are in the initial SS and we have hd rack_rnd_cnt_req rounds(def:5) where
15638 		 * we have not gained the required amount in the gp_est (120.0% aka 1200). Lets
15639 		 * exit SS.
15640 		 *
15641 		 * Pick up the flight size now as we enter slowstart (not the
15642 		 * cwnd which may be inflated).
15643 		 */
15644 		rack->rc_initial_ss_comp = 1;
15645 
15646 		if (tcp_bblogging_on(rack->rc_tp)) {
15647 			union tcp_log_stackspecific log;
15648 			struct timeval tv;
15649 
15650 			memset(&log, 0, sizeof(log));
15651 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15652 			log.u_bbr.flex1 = rack->r_ctl.current_round;
15653 			log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise;
15654 			log.u_bbr.flex3 = rack->r_ctl.gp_rnd_thresh;
15655 			log.u_bbr.flex4 = rack->r_ctl.gate_to_fs;
15656 			log.u_bbr.flex5 = rack->r_ctl.ss_hi_fs;
15657 			log.u_bbr.flex8 = 40;
15658 			(void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
15659 					    0, &log, false, NULL, __func__, __LINE__,&tv);
15660 		}
15661 		if ((rack->r_ctl.gate_to_fs == 1) &&
15662 		     (tp->snd_cwnd > rack->r_ctl.ss_hi_fs)) {
15663 			tp->snd_cwnd = rack->r_ctl.ss_hi_fs;
15664 		}
15665 		tp->snd_ssthresh = tp->snd_cwnd - 1;
15666 		/* Turn off any fast output running */
15667 		rack->r_fast_output = 0;
15668 	}
15669 }
15670 
15671 static int
rack_do_compressed_ack_processing(struct tcpcb * tp,struct socket * so,struct mbuf * m,int nxt_pkt,struct timeval * tv)15672 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
15673 {
15674 	/*
15675 	 * Handle a "special" compressed ack mbuf. Each incoming
15676 	 * ack has only four possible dispositions:
15677 	 *
15678 	 * A) It moves the cum-ack forward
15679 	 * B) It is behind the cum-ack.
15680 	 * C) It is a window-update ack.
15681 	 * D) It is a dup-ack.
15682 	 *
15683 	 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
15684 	 * in the incoming mbuf. We also need to still pay attention
15685 	 * to nxt_pkt since there may be another packet after this
15686 	 * one.
15687 	 */
15688 #ifdef TCP_ACCOUNTING
15689 	uint64_t ts_val;
15690 	uint64_t rdstc;
15691 #endif
15692 	int segsiz;
15693 	struct timespec ts;
15694 	struct tcp_rack *rack;
15695 	struct tcp_ackent *ae;
15696 	uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
15697 	int cnt, i, did_out, ourfinisacked = 0;
15698 	struct tcpopt to_holder, *to = NULL;
15699 #ifdef TCP_ACCOUNTING
15700 	int win_up_req = 0;
15701 #endif
15702 	int nsegs = 0;
15703 	int under_pacing = 0;
15704 	int post_recovery = 0;
15705 #ifdef TCP_ACCOUNTING
15706 	sched_pin();
15707 #endif
15708 	rack = (struct tcp_rack *)tp->t_fb_ptr;
15709 	if (rack->gp_ready &&
15710 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
15711 		under_pacing = 1;
15712 
15713 	if (rack->r_state != tp->t_state)
15714 		rack_set_state(tp, rack);
15715 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
15716 	    (tp->t_flags & TF_GPUTINPROG)) {
15717 		/*
15718 		 * We have a goodput in progress
15719 		 * and we have entered a late state.
15720 		 * Do we have enough data in the sb
15721 		 * to handle the GPUT request?
15722 		 */
15723 		uint32_t bytes;
15724 
15725 		bytes = tp->gput_ack - tp->gput_seq;
15726 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
15727 			bytes += tp->gput_seq - tp->snd_una;
15728 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
15729 			/*
15730 			 * There are not enough bytes in the socket
15731 			 * buffer that have been sent to cover this
15732 			 * measurement. Cancel it.
15733 			 */
15734 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
15735 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
15736 						   tp->gput_seq,
15737 						   0, 0, 18, __LINE__, NULL, 0);
15738 			tp->t_flags &= ~TF_GPUTINPROG;
15739 		}
15740 	}
15741 	to = &to_holder;
15742 	to->to_flags = 0;
15743 	KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
15744 		("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
15745 	cnt = m->m_len / sizeof(struct tcp_ackent);
15746 	counter_u64_add(rack_multi_single_eq, cnt);
15747 	high_seq = tp->snd_una;
15748 	the_win = tp->snd_wnd;
15749 	win_seq = tp->snd_wl1;
15750 	win_upd_ack = tp->snd_wl2;
15751 	cts = tcp_tv_to_usectick(tv);
15752 	ms_cts = tcp_tv_to_mssectick(tv);
15753 	rack->r_ctl.rc_rcvtime = cts;
15754 	segsiz = ctf_fixed_maxseg(tp);
15755 	if ((rack->rc_gp_dyn_mul) &&
15756 	    (rack->use_fixed_rate == 0) &&
15757 	    (rack->rc_always_pace)) {
15758 		/* Check in on probertt */
15759 		rack_check_probe_rtt(rack, cts);
15760 	}
15761 	for (i = 0; i < cnt; i++) {
15762 #ifdef TCP_ACCOUNTING
15763 		ts_val = get_cyclecount();
15764 #endif
15765 		rack_clear_rate_sample(rack);
15766 		ae = ((mtod(m, struct tcp_ackent *)) + i);
15767 		if (ae->flags & TH_FIN)
15768 			rack_log_pacing_delay_calc(rack,
15769 						   0,
15770 						   0,
15771 						   0,
15772 						   rack_get_gp_est(rack), /* delRate */
15773 						   rack_get_lt_bw(rack), /* rttProp */
15774 						   20, __LINE__, NULL, 0);
15775 		/* Setup the window */
15776 		tiwin = ae->win << tp->snd_scale;
15777 		if (tiwin > rack->r_ctl.rc_high_rwnd)
15778 			rack->r_ctl.rc_high_rwnd = tiwin;
15779 		/* figure out the type of ack */
15780 		if (SEQ_LT(ae->ack, high_seq)) {
15781 			/* Case B*/
15782 			ae->ack_val_set = ACK_BEHIND;
15783 		} else if (SEQ_GT(ae->ack, high_seq)) {
15784 			/* Case A */
15785 			ae->ack_val_set = ACK_CUMACK;
15786 		} else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
15787 			/* Case D */
15788 			ae->ack_val_set = ACK_DUPACK;
15789 		} else {
15790 			/* Case C */
15791 			ae->ack_val_set = ACK_RWND;
15792 		}
15793 		rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
15794 		rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
15795 		/* Validate timestamp */
15796 		if (ae->flags & HAS_TSTMP) {
15797 			/* Setup for a timestamp */
15798 			to->to_flags = TOF_TS;
15799 			ae->ts_echo -= tp->ts_offset;
15800 			to->to_tsecr = ae->ts_echo;
15801 			to->to_tsval = ae->ts_value;
15802 			/*
15803 			 * If echoed timestamp is later than the current time, fall back to
15804 			 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
15805 			 * were used when this connection was established.
15806 			 */
15807 			if (TSTMP_GT(ae->ts_echo, ms_cts))
15808 				to->to_tsecr = 0;
15809 			if (tp->ts_recent &&
15810 			    TSTMP_LT(ae->ts_value, tp->ts_recent)) {
15811 				if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
15812 #ifdef TCP_ACCOUNTING
15813 					rdstc = get_cyclecount();
15814 					if (rdstc > ts_val) {
15815 						if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15816 							tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
15817 						}
15818 					}
15819 #endif
15820 					continue;
15821 				}
15822 			}
15823 			if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
15824 			    SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
15825 				tp->ts_recent_age = tcp_ts_getticks();
15826 				tp->ts_recent = ae->ts_value;
15827 			}
15828 		} else {
15829 			/* Setup for a no options */
15830 			to->to_flags = 0;
15831 		}
15832 		/* Update the rcv time and perform idle reduction possibly */
15833 		if  (tp->t_idle_reduce &&
15834 		     (tp->snd_max == tp->snd_una) &&
15835 		     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
15836 			counter_u64_add(rack_input_idle_reduces, 1);
15837 			rack_cc_after_idle(rack, tp);
15838 		}
15839 		tp->t_rcvtime = ticks;
15840 		/* Now what about ECN of a chain of pure ACKs? */
15841 		if (tcp_ecn_input_segment(tp, ae->flags, 0,
15842 			tcp_packets_this_ack(tp, ae->ack),
15843 			ae->codepoint))
15844 			rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
15845 #ifdef TCP_ACCOUNTING
15846 		/* Count for the specific type of ack in */
15847 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15848 			tp->tcp_cnt_counters[ae->ack_val_set]++;
15849 		}
15850 #endif
15851 		/*
15852 		 * Note how we could move up these in the determination
15853 		 * above, but we don't so that way the timestamp checks (and ECN)
15854 		 * is done first before we do any processing on the ACK.
15855 		 * The non-compressed path through the code has this
15856 		 * weakness (noted by @jtl) that it actually does some
15857 		 * processing before verifying the timestamp information.
15858 		 * We don't take that path here which is why we set
15859 		 * the ack_val_set first, do the timestamp and ecn
15860 		 * processing, and then look at what we have setup.
15861 		 */
15862 		if (ae->ack_val_set == ACK_BEHIND) {
15863 			/*
15864 			 * Case B flag reordering, if window is not closed
15865 			 * or it could be a keep-alive or persists
15866 			 */
15867 			if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
15868 				rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15869 				if (rack->r_ctl.rc_reorder_ts == 0)
15870 					rack->r_ctl.rc_reorder_ts = 1;
15871 			}
15872 		} else if (ae->ack_val_set == ACK_DUPACK) {
15873 			/* Case D */
15874 			rack_strike_dupack(rack, ae->ack);
15875 		} else if (ae->ack_val_set == ACK_RWND) {
15876 			/* Case C */
15877 			if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
15878 				ts.tv_sec = ae->timestamp / 1000000000;
15879 				ts.tv_nsec = ae->timestamp % 1000000000;
15880 				rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
15881 				rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
15882 			} else {
15883 				rack->r_ctl.act_rcv_time = *tv;
15884 			}
15885 			if (rack->forced_ack) {
15886 				rack_handle_probe_response(rack, tiwin,
15887 							   tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
15888 			}
15889 #ifdef TCP_ACCOUNTING
15890 			win_up_req = 1;
15891 #endif
15892 			win_upd_ack = ae->ack;
15893 			win_seq = ae->seq;
15894 			the_win = tiwin;
15895 			rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
15896 		} else {
15897 			/* Case A */
15898 			if (SEQ_GT(ae->ack, tp->snd_max)) {
15899 				/*
15900 				 * We just send an ack since the incoming
15901 				 * ack is beyond the largest seq we sent.
15902 				 */
15903 				if ((tp->t_flags & TF_ACKNOW) == 0) {
15904 					ctf_ack_war_checks(tp);
15905 					if (tp->t_flags && TF_ACKNOW)
15906 						rack->r_wanted_output = 1;
15907 				}
15908 			} else {
15909 				nsegs++;
15910 				/* If the window changed setup to update */
15911 				if (tiwin != tp->snd_wnd) {
15912 					win_upd_ack = ae->ack;
15913 					win_seq = ae->seq;
15914 					the_win = tiwin;
15915 					rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
15916 				}
15917 #ifdef TCP_ACCOUNTING
15918 				/* Account for the acks */
15919 				if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15920 					tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
15921 				}
15922 #endif
15923 				high_seq = ae->ack;
15924 				/* Setup our act_rcv_time */
15925 				if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
15926 					ts.tv_sec = ae->timestamp / 1000000000;
15927 					ts.tv_nsec = ae->timestamp % 1000000000;
15928 					rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
15929 					rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
15930 				} else {
15931 					rack->r_ctl.act_rcv_time = *tv;
15932 				}
15933 				rack_process_to_cumack(tp, rack, ae->ack, cts, to,
15934 						       tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
15935 #ifdef TCP_REQUEST_TRK
15936 				rack_req_check_for_comp(rack, high_seq);
15937 #endif
15938 				if (rack->rc_dsack_round_seen) {
15939 					/* Is the dsack round over? */
15940 					if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
15941 						/* Yes it is */
15942 						rack->rc_dsack_round_seen = 0;
15943 						rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
15944 					}
15945 				}
15946 			}
15947 		}
15948 		/* And lets be sure to commit the rtt measurements for this ack */
15949 		tcp_rack_xmit_timer_commit(rack, tp);
15950 #ifdef TCP_ACCOUNTING
15951 		rdstc = get_cyclecount();
15952 		if (rdstc > ts_val) {
15953 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15954 				tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
15955 				if (ae->ack_val_set == ACK_CUMACK)
15956 					tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
15957 			}
15958 		}
15959 #endif
15960 	}
15961 #ifdef TCP_ACCOUNTING
15962 	ts_val = get_cyclecount();
15963 #endif
15964 	/* Tend to any collapsed window */
15965 	if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
15966 		/* The peer collapsed the window */
15967 		rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__);
15968 	} else if (rack->rc_has_collapsed)
15969 		rack_un_collapse_window(rack, __LINE__);
15970 	if ((rack->r_collapse_point_valid) &&
15971 	    (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
15972 		rack->r_collapse_point_valid = 0;
15973 	acked_amount = acked = (high_seq - tp->snd_una);
15974 	if (acked) {
15975 		/*
15976 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
15977 		 * causes issues when we are just going app limited. Lets
15978 		 * instead use SEQ_GT <or> where its equal but more data
15979 		 * is outstanding.
15980 		 *
15981 		 * Also make sure we are on the last ack of a series. We
15982 		 * have to have all the ack's processed in queue to know
15983 		 * if there is something left outstanding.
15984 		 *
15985 		 */
15986 		if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) &&
15987 		    (rack->rc_new_rnd_needed == 0) &&
15988 		    (nxt_pkt == 0)) {
15989 			/*
15990 			 * We have crossed into a new round with
15991 			 * this th_ack value.
15992 			 */
15993 			rack_new_round_setup(tp, rack, high_seq);
15994 		}
15995 		/*
15996 		 * Clear the probe not answered flag
15997 		 * since cum-ack moved forward.
15998 		 */
15999 		rack->probe_not_answered = 0;
16000 		if (tp->t_flags & TF_NEEDSYN) {
16001 			/*
16002 			 * T/TCP: Connection was half-synchronized, and our SYN has
16003 			 * been ACK'd (so connection is now fully synchronized).  Go
16004 			 * to non-starred state, increment snd_una for ACK of SYN,
16005 			 * and check if we can do window scaling.
16006 			 */
16007 			tp->t_flags &= ~TF_NEEDSYN;
16008 			tp->snd_una++;
16009 			acked_amount = acked = (high_seq - tp->snd_una);
16010 		}
16011 		if (acked > sbavail(&so->so_snd))
16012 			acked_amount = sbavail(&so->so_snd);
16013 		if (IN_FASTRECOVERY(tp->t_flags) &&
16014 		    (rack->rack_no_prr == 0))
16015 			rack_update_prr(tp, rack, acked_amount, high_seq);
16016 		if (IN_RECOVERY(tp->t_flags)) {
16017 			if (SEQ_LT(high_seq, tp->snd_recover) &&
16018 			    (SEQ_LT(high_seq, tp->snd_max))) {
16019 				tcp_rack_partialack(tp);
16020 			} else {
16021 				rack_post_recovery(tp, high_seq);
16022 				post_recovery = 1;
16023 			}
16024 		}  else if ((rack->rto_from_rec == 1) &&
16025 			    SEQ_GEQ(high_seq, tp->snd_recover)) {
16026 			/*
16027 			 * We were in recovery, hit a rxt timeout
16028 			 * and never re-entered recovery. The timeout(s)
16029 			 * made up all the lost data. In such a case
16030 			 * we need to clear the rto_from_rec flag.
16031 			 */
16032 			rack->rto_from_rec = 0;
16033 		}
16034 		/* Handle the rack-log-ack part (sendmap) */
16035 		if ((sbused(&so->so_snd) == 0) &&
16036 		    (acked > acked_amount) &&
16037 		    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16038 		    (tp->t_flags & TF_SENTFIN)) {
16039 			/*
16040 			 * We must be sure our fin
16041 			 * was sent and acked (we can be
16042 			 * in FIN_WAIT_1 without having
16043 			 * sent the fin).
16044 			 */
16045 			ourfinisacked = 1;
16046 			/*
16047 			 * Lets make sure snd_una is updated
16048 			 * since most likely acked_amount = 0 (it
16049 			 * should be).
16050 			 */
16051 			tp->snd_una = high_seq;
16052 		}
16053 		/* Did we make a RTO error? */
16054 		if ((tp->t_flags & TF_PREVVALID) &&
16055 		    ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
16056 			tp->t_flags &= ~TF_PREVVALID;
16057 			if (tp->t_rxtshift == 1 &&
16058 			    (int)(ticks - tp->t_badrxtwin) < 0)
16059 				rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
16060 		}
16061 		/* Handle the data in the socket buffer */
16062 		KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
16063 		KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
16064 		if (acked_amount > 0) {
16065 			uint32_t p_cwnd;
16066 			struct mbuf *mfree;
16067 
16068 			if (post_recovery) {
16069 				/*
16070 				 * Grab the segsiz, multiply by 2 and add the snd_cwnd
16071 				 * that is the max the CC should add if we are exiting
16072 				 * recovery and doing a late add.
16073 				 */
16074 				p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16075 				p_cwnd <<= 1;
16076 				p_cwnd += tp->snd_cwnd;
16077 			}
16078 			rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, post_recovery);
16079 			if (post_recovery && (tp->snd_cwnd > p_cwnd)) {
16080 				/* Must be non-newreno (cubic) getting too ahead of itself */
16081 				tp->snd_cwnd = p_cwnd;
16082 			}
16083 			SOCK_SENDBUF_LOCK(so);
16084 			mfree = sbcut_locked(&so->so_snd, acked_amount);
16085 			tp->snd_una = high_seq;
16086 			/* Note we want to hold the sb lock through the sendmap adjust */
16087 			rack_adjust_sendmap_head(rack, &so->so_snd);
16088 			/* Wake up the socket if we have room to write more */
16089 			rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
16090 			sowwakeup_locked(so);
16091 			m_freem(mfree);
16092 		}
16093 		/* update progress */
16094 		tp->t_acktime = ticks;
16095 		rack_log_progress_event(rack, tp, tp->t_acktime,
16096 					PROGRESS_UPDATE, __LINE__);
16097 		/* Clear out shifts and such */
16098 		tp->t_rxtshift = 0;
16099 		RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
16100 				   rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
16101 		rack->rc_tlp_in_progress = 0;
16102 		rack->r_ctl.rc_tlp_cnt_out = 0;
16103 		/* Send recover and snd_nxt must be dragged along */
16104 		if (SEQ_GT(tp->snd_una, tp->snd_recover))
16105 			tp->snd_recover = tp->snd_una;
16106 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
16107 			tp->snd_nxt = tp->snd_max;
16108 		/*
16109 		 * If the RXT timer is running we want to
16110 		 * stop it, so we can restart a TLP (or new RXT).
16111 		 */
16112 		if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
16113 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16114 		tp->snd_wl2 = high_seq;
16115 		tp->t_dupacks = 0;
16116 		if (under_pacing &&
16117 		    (rack->use_fixed_rate == 0) &&
16118 		    (rack->in_probe_rtt == 0) &&
16119 		    rack->rc_gp_dyn_mul &&
16120 		    rack->rc_always_pace) {
16121 			/* Check if we are dragging bottom */
16122 			rack_check_bottom_drag(tp, rack, so);
16123 		}
16124 		if (tp->snd_una == tp->snd_max) {
16125 			tp->t_flags &= ~TF_PREVVALID;
16126 			rack->r_ctl.retran_during_recovery = 0;
16127 			rack->rc_suspicious = 0;
16128 			rack->r_ctl.dsack_byte_cnt = 0;
16129 			rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
16130 			if (rack->r_ctl.rc_went_idle_time == 0)
16131 				rack->r_ctl.rc_went_idle_time = 1;
16132 			rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
16133 			if (sbavail(&tptosocket(tp)->so_snd) == 0)
16134 				tp->t_acktime = 0;
16135 			/* Set so we might enter persists... */
16136 			rack->r_wanted_output = 1;
16137 			rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16138 			sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
16139 			if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16140 			    (sbavail(&so->so_snd) == 0) &&
16141 			    (tp->t_flags2 & TF2_DROP_AF_DATA)) {
16142 				/*
16143 				 * The socket was gone and the
16144 				 * peer sent data (not now in the past), time to
16145 				 * reset him.
16146 				 */
16147 				rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16148 				/* tcp_close will kill the inp pre-log the Reset */
16149 				tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
16150 #ifdef TCP_ACCOUNTING
16151 				rdstc = get_cyclecount();
16152 				if (rdstc > ts_val) {
16153 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16154 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16155 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16156 					}
16157 				}
16158 #endif
16159 				m_freem(m);
16160 				tp = tcp_close(tp);
16161 				if (tp == NULL) {
16162 #ifdef TCP_ACCOUNTING
16163 					sched_unpin();
16164 #endif
16165 					return (1);
16166 				}
16167 				/*
16168 				 * We would normally do drop-with-reset which would
16169 				 * send back a reset. We can't since we don't have
16170 				 * all the needed bits. Instead lets arrange for
16171 				 * a call to tcp_output(). That way since we
16172 				 * are in the closed state we will generate a reset.
16173 				 *
16174 				 * Note if tcp_accounting is on we don't unpin since
16175 				 * we do that after the goto label.
16176 				 */
16177 				goto send_out_a_rst;
16178 			}
16179 			if ((sbused(&so->so_snd) == 0) &&
16180 			    (tp->t_state >= TCPS_FIN_WAIT_1) &&
16181 			    (tp->t_flags & TF_SENTFIN)) {
16182 				/*
16183 				 * If we can't receive any more data, then closing user can
16184 				 * proceed. Starting the timer is contrary to the
16185 				 * specification, but if we don't get a FIN we'll hang
16186 				 * forever.
16187 				 *
16188 				 */
16189 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16190 					soisdisconnected(so);
16191 					tcp_timer_activate(tp, TT_2MSL,
16192 							   (tcp_fast_finwait2_recycle ?
16193 							    tcp_finwait2_timeout :
16194 							    TP_MAXIDLE(tp)));
16195 				}
16196 				if (ourfinisacked == 0) {
16197 					/*
16198 					 * We don't change to fin-wait-2 if we have our fin acked
16199 					 * which means we are probably in TCPS_CLOSING.
16200 					 */
16201 					tcp_state_change(tp, TCPS_FIN_WAIT_2);
16202 				}
16203 			}
16204 		}
16205 		/* Wake up the socket if we have room to write more */
16206 		if (sbavail(&so->so_snd)) {
16207 			rack->r_wanted_output = 1;
16208 			if (ctf_progress_timeout_check(tp, true)) {
16209 				rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
16210 							tp, tick, PROGRESS_DROP, __LINE__);
16211 				/*
16212 				 * We cheat here and don't send a RST, we should send one
16213 				 * when the pacer drops the connection.
16214 				 */
16215 #ifdef TCP_ACCOUNTING
16216 				rdstc = get_cyclecount();
16217 				if (rdstc > ts_val) {
16218 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16219 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16220 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16221 					}
16222 				}
16223 				sched_unpin();
16224 #endif
16225 				(void)tcp_drop(tp, ETIMEDOUT);
16226 				m_freem(m);
16227 				return (1);
16228 			}
16229 		}
16230 		if (ourfinisacked) {
16231 			switch(tp->t_state) {
16232 			case TCPS_CLOSING:
16233 #ifdef TCP_ACCOUNTING
16234 				rdstc = get_cyclecount();
16235 				if (rdstc > ts_val) {
16236 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16237 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16238 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16239 					}
16240 				}
16241 				sched_unpin();
16242 #endif
16243 				tcp_twstart(tp);
16244 				m_freem(m);
16245 				return (1);
16246 				break;
16247 			case TCPS_LAST_ACK:
16248 #ifdef TCP_ACCOUNTING
16249 				rdstc = get_cyclecount();
16250 				if (rdstc > ts_val) {
16251 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16252 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16253 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16254 					}
16255 				}
16256 				sched_unpin();
16257 #endif
16258 				tp = tcp_close(tp);
16259 				ctf_do_drop(m, tp);
16260 				return (1);
16261 				break;
16262 			case TCPS_FIN_WAIT_1:
16263 #ifdef TCP_ACCOUNTING
16264 				rdstc = get_cyclecount();
16265 				if (rdstc > ts_val) {
16266 					if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16267 						tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16268 						tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16269 					}
16270 				}
16271 #endif
16272 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16273 					soisdisconnected(so);
16274 					tcp_timer_activate(tp, TT_2MSL,
16275 							   (tcp_fast_finwait2_recycle ?
16276 							    tcp_finwait2_timeout :
16277 							    TP_MAXIDLE(tp)));
16278 				}
16279 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
16280 				break;
16281 			default:
16282 				break;
16283 			}
16284 		}
16285 		if (rack->r_fast_output) {
16286 			/*
16287 			 * We re doing fast output.. can we expand that?
16288 			 */
16289 			rack_gain_for_fastoutput(rack, tp, so, acked_amount);
16290 		}
16291 #ifdef TCP_ACCOUNTING
16292 		rdstc = get_cyclecount();
16293 		if (rdstc > ts_val) {
16294 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16295 				tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16296 				tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16297 			}
16298 		}
16299 
16300 	} else if (win_up_req) {
16301 		rdstc = get_cyclecount();
16302 		if (rdstc > ts_val) {
16303 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16304 				tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
16305 			}
16306 		}
16307 #endif
16308 	}
16309 	/* Now is there a next packet, if so we are done */
16310 	m_freem(m);
16311 	did_out = 0;
16312 	if (nxt_pkt) {
16313 #ifdef TCP_ACCOUNTING
16314 		sched_unpin();
16315 #endif
16316 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
16317 		return (0);
16318 	}
16319 	rack_handle_might_revert(tp, rack);
16320 	ctf_calc_rwin(so, tp);
16321 	if ((rack->r_wanted_output != 0) ||
16322 	    (rack->r_fast_output != 0) ||
16323 	    (tp->t_flags & TF_ACKNOW )) {
16324 	send_out_a_rst:
16325 		if (tcp_output(tp) < 0) {
16326 #ifdef TCP_ACCOUNTING
16327 			sched_unpin();
16328 #endif
16329 			return (1);
16330 		}
16331 		did_out = 1;
16332 	}
16333 	if (tp->t_flags2 & TF2_HPTS_CALLS)
16334 		tp->t_flags2 &= ~TF2_HPTS_CALLS;
16335 	rack_free_trim(rack);
16336 #ifdef TCP_ACCOUNTING
16337 	sched_unpin();
16338 #endif
16339 	rack_timer_audit(tp, rack, &so->so_snd);
16340 	rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
16341 	return (0);
16342 }
16343 
16344 #define	TCP_LRO_TS_OPTION \
16345     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
16346 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
16347 
16348 static int
rack_do_segment_nounlock(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos,int32_t nxt_pkt,struct timeval * tv)16349 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
16350     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
16351     struct timeval *tv)
16352 {
16353 	struct inpcb *inp = tptoinpcb(tp);
16354 	struct socket *so = tptosocket(tp);
16355 #ifdef TCP_ACCOUNTING
16356 	uint64_t ts_val;
16357 #endif
16358 	int32_t thflags, retval, did_out = 0;
16359 	int32_t way_out = 0;
16360 	/*
16361 	 * cts - is the current time from tv (caller gets ts) in microseconds.
16362 	 * ms_cts - is the current time from tv in milliseconds.
16363 	 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
16364 	 */
16365 	uint32_t cts, us_cts, ms_cts;
16366 	uint32_t tiwin;
16367 	struct timespec ts;
16368 	struct tcpopt to;
16369 	struct tcp_rack *rack;
16370 	struct rack_sendmap *rsm;
16371 	int32_t prev_state = 0;
16372 	int no_output = 0;
16373 	int slot_remaining = 0;
16374 #ifdef TCP_ACCOUNTING
16375 	int ack_val_set = 0xf;
16376 #endif
16377 	int nsegs;
16378 
16379 	NET_EPOCH_ASSERT();
16380 	INP_WLOCK_ASSERT(inp);
16381 
16382 	/*
16383 	 * tv passed from common code is from either M_TSTMP_LRO or
16384 	 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
16385 	 */
16386 	rack = (struct tcp_rack *)tp->t_fb_ptr;
16387 	if (rack->rack_deferred_inited == 0) {
16388 		/*
16389 		 * If we are the connecting socket we will
16390 		 * hit rack_init() when no sequence numbers
16391 		 * are setup. This makes it so we must defer
16392 		 * some initialization. Call that now.
16393 		 */
16394 		rack_deferred_init(tp, rack);
16395 	}
16396 	/*
16397 	 * Check to see if we need to skip any output plans. This
16398 	 * can happen in the non-LRO path where we are pacing and
16399 	 * must process the ack coming in but need to defer sending
16400 	 * anything becase a pacing timer is running.
16401 	 */
16402 	us_cts = tcp_tv_to_usectick(tv);
16403 	if (m->m_flags & M_ACKCMP) {
16404 		/*
16405 		 * All compressed ack's are ack's by definition so
16406 		 * remove any ack required flag and then do the processing.
16407 		 */
16408 		rack->rc_ack_required = 0;
16409 		return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
16410 	}
16411 	thflags = tcp_get_flags(th);
16412 	if ((rack->rc_always_pace == 1) &&
16413 	    (rack->rc_ack_can_sendout_data == 0) &&
16414 	    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16415 	    (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) {
16416 		/*
16417 		 * Ok conditions are right for queuing the packets
16418 		 * but we do have to check the flags in the inp, it
16419 		 * could be, if a sack is present, we want to be awoken and
16420 		 * so should process the packets.
16421 		 */
16422 		slot_remaining = rack->r_ctl.rc_last_output_to - us_cts;
16423 		if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) {
16424 			no_output = 1;
16425 		} else {
16426 			/*
16427 			 * If there is no options, or just a
16428 			 * timestamp option, we will want to queue
16429 			 * the packets. This is the same that LRO does
16430 			 * and will need to change with accurate ECN.
16431 			 */
16432 			uint32_t *ts_ptr;
16433 			int optlen;
16434 
16435 			optlen = (th->th_off << 2) - sizeof(struct tcphdr);
16436 			ts_ptr = (uint32_t *)(th + 1);
16437 			if ((optlen == 0) ||
16438 			    ((optlen == TCPOLEN_TSTAMP_APPA) &&
16439 			     (*ts_ptr == TCP_LRO_TS_OPTION)))
16440 				no_output = 1;
16441 		}
16442 		if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) {
16443 			/*
16444 			 * It is unrealistic to think we can pace in less than
16445 			 * the minimum granularity of the pacer (def:250usec). So
16446 			 * if we have less than that time remaining we should go
16447 			 * ahead and allow output to be "early". We will attempt to
16448 			 * make up for it in any pacing time we try to apply on
16449 			 * the outbound packet.
16450 			 */
16451 			no_output = 0;
16452 		}
16453 	}
16454 	/*
16455 	 * If there is a RST or FIN lets dump out the bw
16456 	 * with a FIN the connection may go on but we
16457 	 * may not.
16458 	 */
16459 	if ((thflags & TH_FIN) || (thflags & TH_RST))
16460 		rack_log_pacing_delay_calc(rack,
16461 					   rack->r_ctl.gp_bw,
16462 					   0,
16463 					   0,
16464 					   rack_get_gp_est(rack), /* delRate */
16465 					   rack_get_lt_bw(rack), /* rttProp */
16466 					   20, __LINE__, NULL, 0);
16467 	if (m->m_flags & M_ACKCMP) {
16468 		panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
16469 	}
16470 	cts = tcp_tv_to_usectick(tv);
16471 	ms_cts =  tcp_tv_to_mssectick(tv);
16472 	nsegs = m->m_pkthdr.lro_nsegs;
16473 	counter_u64_add(rack_proc_non_comp_ack, 1);
16474 #ifdef TCP_ACCOUNTING
16475 	sched_pin();
16476 	if (thflags & TH_ACK)
16477 		ts_val = get_cyclecount();
16478 #endif
16479 	if ((m->m_flags & M_TSTMP) ||
16480 	    (m->m_flags & M_TSTMP_LRO)) {
16481 		mbuf_tstmp2timespec(m, &ts);
16482 		rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16483 		rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16484 	} else
16485 		rack->r_ctl.act_rcv_time = *tv;
16486 	kern_prefetch(rack, &prev_state);
16487 	prev_state = 0;
16488 	/*
16489 	 * Unscale the window into a 32-bit value. For the SYN_SENT state
16490 	 * the scale is zero.
16491 	 */
16492 	tiwin = th->th_win << tp->snd_scale;
16493 #ifdef TCP_ACCOUNTING
16494 	if (thflags & TH_ACK) {
16495 		/*
16496 		 * We have a tradeoff here. We can either do what we are
16497 		 * doing i.e. pinning to this CPU and then doing the accounting
16498 		 * <or> we could do a critical enter, setup the rdtsc and cpu
16499 		 * as in below, and then validate we are on the same CPU on
16500 		 * exit. I have choosen to not do the critical enter since
16501 		 * that often will gain you a context switch, and instead lock
16502 		 * us (line above this if) to the same CPU with sched_pin(). This
16503 		 * means we may be context switched out for a higher priority
16504 		 * interupt but we won't be moved to another CPU.
16505 		 *
16506 		 * If this occurs (which it won't very often since we most likely
16507 		 * are running this code in interupt context and only a higher
16508 		 * priority will bump us ... clock?) we will falsely add in
16509 		 * to the time the interupt processing time plus the ack processing
16510 		 * time. This is ok since its a rare event.
16511 		 */
16512 		ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
16513 						    ctf_fixed_maxseg(tp));
16514 	}
16515 #endif
16516 	/*
16517 	 * Parse options on any incoming segment.
16518 	 */
16519 	memset(&to, 0, sizeof(to));
16520 	tcp_dooptions(&to, (u_char *)(th + 1),
16521 	    (th->th_off << 2) - sizeof(struct tcphdr),
16522 	    (thflags & TH_SYN) ? TO_SYN : 0);
16523 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
16524 	    __func__));
16525 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
16526 	    __func__));
16527 	if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) {
16528 		/*
16529 		 * We don't look at sack's from the
16530 		 * peer because the MSS is too small which
16531 		 * can subject us to an attack.
16532 		 */
16533 		to.to_flags &= ~TOF_SACK;
16534 	}
16535 	if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16536 	    (tp->t_flags & TF_GPUTINPROG)) {
16537 		/*
16538 		 * We have a goodput in progress
16539 		 * and we have entered a late state.
16540 		 * Do we have enough data in the sb
16541 		 * to handle the GPUT request?
16542 		 */
16543 		uint32_t bytes;
16544 
16545 		bytes = tp->gput_ack - tp->gput_seq;
16546 		if (SEQ_GT(tp->gput_seq, tp->snd_una))
16547 			bytes += tp->gput_seq - tp->snd_una;
16548 		if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
16549 			/*
16550 			 * There are not enough bytes in the socket
16551 			 * buffer that have been sent to cover this
16552 			 * measurement. Cancel it.
16553 			 */
16554 			rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
16555 						   rack->r_ctl.rc_gp_srtt /*flex1*/,
16556 						   tp->gput_seq,
16557 						   0, 0, 18, __LINE__, NULL, 0);
16558 			tp->t_flags &= ~TF_GPUTINPROG;
16559 		}
16560 	}
16561 	if (tcp_bblogging_on(rack->rc_tp)) {
16562 		union tcp_log_stackspecific log;
16563 		struct timeval ltv;
16564 #ifdef TCP_REQUEST_TRK
16565 		struct tcp_sendfile_track *tcp_req;
16566 
16567 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
16568 			tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1));
16569 		} else {
16570 			tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack);
16571 		}
16572 #endif
16573 		memset(&log, 0, sizeof(log));
16574 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
16575 		if (rack->rack_no_prr == 0)
16576 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16577 		else
16578 			log.u_bbr.flex1 = 0;
16579 		log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
16580 		log.u_bbr.use_lt_bw <<= 1;
16581 		log.u_bbr.use_lt_bw |= rack->r_might_revert;
16582 		log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
16583 		log.u_bbr.bbr_state = rack->rc_free_cnt;
16584 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16585 		log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
16586 		log.u_bbr.flex3 = m->m_flags;
16587 		log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
16588 		log.u_bbr.lost = thflags;
16589 		log.u_bbr.pacing_gain = 0x1;
16590 #ifdef TCP_ACCOUNTING
16591 		log.u_bbr.cwnd_gain = ack_val_set;
16592 #endif
16593 		log.u_bbr.flex7 = 2;
16594 		if (m->m_flags & M_TSTMP) {
16595 			/* Record the hardware timestamp if present */
16596 			mbuf_tstmp2timespec(m, &ts);
16597 			ltv.tv_sec = ts.tv_sec;
16598 			ltv.tv_usec = ts.tv_nsec / 1000;
16599 			log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
16600 		} else if (m->m_flags & M_TSTMP_LRO) {
16601 			/* Record the LRO the arrival timestamp */
16602 			mbuf_tstmp2timespec(m, &ts);
16603 			ltv.tv_sec = ts.tv_sec;
16604 			ltv.tv_usec = ts.tv_nsec / 1000;
16605 			log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
16606 		}
16607 		log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
16608 		/* Log the rcv time */
16609 		log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
16610 #ifdef TCP_REQUEST_TRK
16611 		log.u_bbr.applimited = tp->t_tcpreq_closed;
16612 		log.u_bbr.applimited <<= 8;
16613 		log.u_bbr.applimited |= tp->t_tcpreq_open;
16614 		log.u_bbr.applimited <<= 8;
16615 		log.u_bbr.applimited |= tp->t_tcpreq_req;
16616 		if (tcp_req) {
16617 			/* Copy out any client req info */
16618 			/* seconds */
16619 			log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
16620 			/* useconds */
16621 			log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
16622 			log.u_bbr.rttProp = tcp_req->timestamp;
16623 			log.u_bbr.cur_del_rate = tcp_req->start;
16624 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
16625 				log.u_bbr.flex8 |= 1;
16626 			} else {
16627 				log.u_bbr.flex8 |= 2;
16628 				log.u_bbr.bw_inuse = tcp_req->end;
16629 			}
16630 			log.u_bbr.flex6 = tcp_req->start_seq;
16631 			if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
16632 				log.u_bbr.flex8 |= 4;
16633 				log.u_bbr.epoch = tcp_req->end_seq;
16634 			}
16635 		}
16636 #endif
16637 		TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
16638 		    tlen, &log, true, &ltv);
16639 	}
16640 	/* Remove ack required flag if set, we have one  */
16641 	if (thflags & TH_ACK)
16642 		rack->rc_ack_required = 0;
16643 	rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
16644 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
16645 		way_out = 4;
16646 		retval = 0;
16647 		m_freem(m);
16648 		goto done_with_input;
16649 	}
16650 	/*
16651 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
16652 	 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
16653 	 */
16654 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
16655 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
16656 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
16657 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
16658 #ifdef TCP_ACCOUNTING
16659 		sched_unpin();
16660 #endif
16661 		return (1);
16662 	}
16663 	/*
16664 	 * If timestamps were negotiated during SYN/ACK and a
16665 	 * segment without a timestamp is received, silently drop
16666 	 * the segment, unless it is a RST segment or missing timestamps are
16667 	 * tolerated.
16668 	 * See section 3.2 of RFC 7323.
16669 	 */
16670 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
16671 	    ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
16672 		way_out = 5;
16673 		retval = 0;
16674 		m_freem(m);
16675 		goto done_with_input;
16676 	}
16677 	/*
16678 	 * Segment received on connection. Reset idle time and keep-alive
16679 	 * timer. XXX: This should be done after segment validation to
16680 	 * ignore broken/spoofed segs.
16681 	 */
16682 	if  (tp->t_idle_reduce &&
16683 	     (tp->snd_max == tp->snd_una) &&
16684 	     (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
16685 		counter_u64_add(rack_input_idle_reduces, 1);
16686 		rack_cc_after_idle(rack, tp);
16687 	}
16688 	tp->t_rcvtime = ticks;
16689 #ifdef STATS
16690 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
16691 #endif
16692 	if (tiwin > rack->r_ctl.rc_high_rwnd)
16693 		rack->r_ctl.rc_high_rwnd = tiwin;
16694 	/*
16695 	 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
16696 	 * this to occur after we've validated the segment.
16697 	 */
16698 	if (tcp_ecn_input_segment(tp, thflags, tlen,
16699 	    tcp_packets_this_ack(tp, th->th_ack),
16700 	    iptos))
16701 		rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
16702 
16703 	/*
16704 	 * If echoed timestamp is later than the current time, fall back to
16705 	 * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
16706 	 * were used when this connection was established.
16707 	 */
16708 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
16709 		to.to_tsecr -= tp->ts_offset;
16710 		if (TSTMP_GT(to.to_tsecr, ms_cts))
16711 			to.to_tsecr = 0;
16712 	}
16713 	if ((rack->r_rcvpath_rtt_up == 1) &&
16714 	    (to.to_flags & TOF_TS) &&
16715 	    (TSTMP_GEQ(to.to_tsecr, rack->r_ctl.last_rcv_tstmp_for_rtt))) {
16716 		uint32_t rtt = 0;
16717 
16718 		/*
16719 		 * We are receiving only and thus not sending
16720 		 * data to do an RTT. We set a flag when we first
16721 		 * sent this TS to the peer. We now have it back
16722 		 * and have an RTT to share. We log it as a conf
16723 		 * 4, we are not so sure about it.. since we
16724 		 * may have lost an ack.
16725 		 */
16726 		if (TSTMP_GT(cts, rack->r_ctl.last_time_of_arm_rcv))
16727 		    rtt = (cts - rack->r_ctl.last_time_of_arm_rcv);
16728 		rack->r_rcvpath_rtt_up = 0;
16729 		/* Submit and commit the timer */
16730 		if (rtt > 0) {
16731 			tcp_rack_xmit_timer(rack, rtt, 0, rtt, 4, NULL, 1);
16732 			tcp_rack_xmit_timer_commit(rack, tp);
16733 		}
16734 	}
16735 	/*
16736 	 * If its the first time in we need to take care of options and
16737 	 * verify we can do SACK for rack!
16738 	 */
16739 	if (rack->r_state == 0) {
16740 		/* Should be init'd by rack_init() */
16741 		KASSERT(rack->rc_inp != NULL,
16742 		    ("%s: rack->rc_inp unexpectedly NULL", __func__));
16743 		if (rack->rc_inp == NULL) {
16744 			rack->rc_inp = inp;
16745 		}
16746 
16747 		/*
16748 		 * Process options only when we get SYN/ACK back. The SYN
16749 		 * case for incoming connections is handled in tcp_syncache.
16750 		 * According to RFC1323 the window field in a SYN (i.e., a
16751 		 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
16752 		 * this is traditional behavior, may need to be cleaned up.
16753 		 */
16754 		if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16755 			/* Handle parallel SYN for ECN */
16756 			tcp_ecn_input_parallel_syn(tp, thflags, iptos);
16757 			if ((to.to_flags & TOF_SCALE) &&
16758 			    (tp->t_flags & TF_REQ_SCALE)) {
16759 				tp->t_flags |= TF_RCVD_SCALE;
16760 				tp->snd_scale = to.to_wscale;
16761 			} else
16762 				tp->t_flags &= ~TF_REQ_SCALE;
16763 			/*
16764 			 * Initial send window.  It will be updated with the
16765 			 * next incoming segment to the scaled value.
16766 			 */
16767 			tp->snd_wnd = th->th_win;
16768 			rack_validate_fo_sendwin_up(tp, rack);
16769 			if ((to.to_flags & TOF_TS) &&
16770 			    (tp->t_flags & TF_REQ_TSTMP)) {
16771 				tp->t_flags |= TF_RCVD_TSTMP;
16772 				tp->ts_recent = to.to_tsval;
16773 				tp->ts_recent_age = cts;
16774 			} else
16775 				tp->t_flags &= ~TF_REQ_TSTMP;
16776 			if (to.to_flags & TOF_MSS) {
16777 				tcp_mss(tp, to.to_mss);
16778 			}
16779 			if ((tp->t_flags & TF_SACK_PERMIT) &&
16780 			    (to.to_flags & TOF_SACKPERM) == 0)
16781 				tp->t_flags &= ~TF_SACK_PERMIT;
16782 			if (tp->t_flags & TF_FASTOPEN) {
16783 				if (to.to_flags & TOF_FASTOPEN) {
16784 					uint16_t mss;
16785 
16786 					if (to.to_flags & TOF_MSS)
16787 						mss = to.to_mss;
16788 					else
16789 						if ((inp->inp_vflag & INP_IPV6) != 0)
16790 							mss = TCP6_MSS;
16791 						else
16792 							mss = TCP_MSS;
16793 					tcp_fastopen_update_cache(tp, mss,
16794 					    to.to_tfo_len, to.to_tfo_cookie);
16795 				} else
16796 					tcp_fastopen_disable_path(tp);
16797 			}
16798 		}
16799 		/*
16800 		 * At this point we are at the initial call. Here we decide
16801 		 * if we are doing RACK or not. We do this by seeing if
16802 		 * TF_SACK_PERMIT is set and the sack-not-required is clear.
16803 		 * The code now does do dup-ack counting so if you don't
16804 		 * switch back you won't get rack & TLP, but you will still
16805 		 * get this stack.
16806 		 */
16807 
16808 		if ((rack_sack_not_required == 0) &&
16809 		    ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
16810 			tcp_switch_back_to_default(tp);
16811 			(*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
16812 			    tlen, iptos);
16813 #ifdef TCP_ACCOUNTING
16814 			sched_unpin();
16815 #endif
16816 			return (1);
16817 		}
16818 		tcp_set_hpts(tp);
16819 		sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
16820 	}
16821 	if (thflags & TH_FIN)
16822 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
16823 	us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
16824 	if ((rack->rc_gp_dyn_mul) &&
16825 	    (rack->use_fixed_rate == 0) &&
16826 	    (rack->rc_always_pace)) {
16827 		/* Check in on probertt */
16828 		rack_check_probe_rtt(rack, cts);
16829 	}
16830 	rack_clear_rate_sample(rack);
16831 	if ((rack->forced_ack) &&
16832 	    ((tcp_get_flags(th) & TH_RST) == 0)) {
16833 		rack_handle_probe_response(rack, tiwin, us_cts);
16834 	}
16835 	/*
16836 	 * This is the one exception case where we set the rack state
16837 	 * always. All other times (timers etc) we must have a rack-state
16838 	 * set (so we assure we have done the checks above for SACK).
16839 	 */
16840 	rack->r_ctl.rc_rcvtime = cts;
16841 	if (rack->r_state != tp->t_state)
16842 		rack_set_state(tp, rack);
16843 	if (SEQ_GT(th->th_ack, tp->snd_una) &&
16844 	    (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL)
16845 		kern_prefetch(rsm, &prev_state);
16846 	prev_state = rack->r_state;
16847 	if ((thflags & TH_RST) &&
16848 	    ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
16849 	      SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
16850 	     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) {
16851 		/* The connection will be killed by a reset check the tracepoint */
16852 		tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV);
16853 	}
16854 	retval = (*rack->r_substate) (m, th, so,
16855 	    tp, &to, drop_hdrlen,
16856 	    tlen, tiwin, thflags, nxt_pkt, iptos);
16857 	if (retval == 0) {
16858 		/*
16859 		 * If retval is 1 the tcb is unlocked and most likely the tp
16860 		 * is gone.
16861 		 */
16862 		INP_WLOCK_ASSERT(inp);
16863 		if ((rack->rc_gp_dyn_mul) &&
16864 		    (rack->rc_always_pace) &&
16865 		    (rack->use_fixed_rate == 0) &&
16866 		    rack->in_probe_rtt &&
16867 		    (rack->r_ctl.rc_time_probertt_starts == 0)) {
16868 			/*
16869 			 * If we are going for target, lets recheck before
16870 			 * we output.
16871 			 */
16872 			rack_check_probe_rtt(rack, cts);
16873 		}
16874 		if (rack->set_pacing_done_a_iw == 0) {
16875 			/* How much has been acked? */
16876 			if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
16877 				/* We have enough to set in the pacing segment size */
16878 				rack->set_pacing_done_a_iw = 1;
16879 				rack_set_pace_segments(tp, rack, __LINE__, NULL);
16880 			}
16881 		}
16882 		tcp_rack_xmit_timer_commit(rack, tp);
16883 #ifdef TCP_ACCOUNTING
16884 		/*
16885 		 * If we set the ack_val_se to what ack processing we are doing
16886 		 * we also want to track how many cycles we burned. Note
16887 		 * the bits after tcp_output we let be "free". This is because
16888 		 * we are also tracking the tcp_output times as well. Note the
16889 		 * use of 0xf here since we only have 11 counter (0 - 0xa) and
16890 		 * 0xf cannot be returned and is what we initialize it too to
16891 		 * indicate we are not doing the tabulations.
16892 		 */
16893 		if (ack_val_set != 0xf) {
16894 			uint64_t crtsc;
16895 
16896 			crtsc = get_cyclecount();
16897 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16898 				tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
16899 			}
16900 		}
16901 #endif
16902 		if ((nxt_pkt == 0) && (no_output == 0)) {
16903 			if ((rack->r_wanted_output != 0) ||
16904 			    (tp->t_flags & TF_ACKNOW) ||
16905 			    (rack->r_fast_output != 0)) {
16906 
16907 do_output_now:
16908 				if (tcp_output(tp) < 0) {
16909 #ifdef TCP_ACCOUNTING
16910 					sched_unpin();
16911 #endif
16912 					return (1);
16913 				}
16914 				did_out = 1;
16915 			}
16916 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
16917 			rack_free_trim(rack);
16918 		} else if ((nxt_pkt == 0) && (tp->t_flags & TF_ACKNOW)) {
16919 			goto do_output_now;
16920 		} else if ((no_output == 1) &&
16921 			   (nxt_pkt == 0)  &&
16922 			   (tcp_in_hpts(rack->rc_tp) == 0)) {
16923 			/*
16924 			 * We are not in hpts and we had a pacing timer up. Use
16925 			 * the remaining time (slot_remaining) to restart the timer.
16926 			 */
16927 			KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp));
16928 			rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0);
16929 			rack_free_trim(rack);
16930 		}
16931 		/* Clear the flag, it may have been cleared by output but we may not have  */
16932 		if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
16933 			tp->t_flags2 &= ~TF2_HPTS_CALLS;
16934 		/*
16935 		 * The draft (v3) calls for us to use SEQ_GEQ, but that
16936 		 * causes issues when we are just going app limited. Lets
16937 		 * instead use SEQ_GT <or> where its equal but more data
16938 		 * is outstanding.
16939 		 *
16940 		 * Also make sure we are on the last ack of a series. We
16941 		 * have to have all the ack's processed in queue to know
16942 		 * if there is something left outstanding.
16943 		 */
16944 		if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) &&
16945 		    (rack->rc_new_rnd_needed == 0) &&
16946 		    (nxt_pkt == 0)) {
16947 			/*
16948 			 * We have crossed into a new round with
16949 			 * the new snd_unae.
16950 			 */
16951 			rack_new_round_setup(tp, rack, tp->snd_una);
16952 		}
16953 		if ((nxt_pkt == 0) &&
16954 		    ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
16955 		    (SEQ_GT(tp->snd_max, tp->snd_una) ||
16956 		     (tp->t_flags & TF_DELACK) ||
16957 		     ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
16958 		      (tp->t_state <= TCPS_CLOSING)))) {
16959 			/* We could not send (probably in the hpts but stopped the timer earlier)? */
16960 			if ((tp->snd_max == tp->snd_una) &&
16961 			    ((tp->t_flags & TF_DELACK) == 0) &&
16962 			    (tcp_in_hpts(rack->rc_tp)) &&
16963 			    (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
16964 				/* keep alive not needed if we are hptsi output yet */
16965 				;
16966 			} else {
16967 				int late = 0;
16968 				if (tcp_in_hpts(tp)) {
16969 					if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
16970 						us_cts = tcp_get_usecs(NULL);
16971 						if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
16972 							rack->r_early = 1;
16973 							rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
16974 						} else
16975 							late = 1;
16976 						rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16977 					}
16978 					tcp_hpts_remove(tp);
16979 				}
16980 				if (late && (did_out == 0)) {
16981 					/*
16982 					 * We are late in the sending
16983 					 * and we did not call the output
16984 					 * (this probably should not happen).
16985 					 */
16986 					goto do_output_now;
16987 				}
16988 				rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
16989 			}
16990 			way_out = 1;
16991 		} else if (nxt_pkt == 0) {
16992 			/* Do we have the correct timer running? */
16993 			rack_timer_audit(tp, rack, &so->so_snd);
16994 			way_out = 2;
16995 		}
16996 	done_with_input:
16997 		rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
16998 		if (did_out)
16999 			rack->r_wanted_output = 0;
17000 	}
17001 
17002 #ifdef TCP_ACCOUNTING
17003 	sched_unpin();
17004 #endif
17005 	return (retval);
17006 }
17007 
17008 static void
rack_do_segment(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos)17009 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
17010     int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
17011 {
17012 	struct timeval tv;
17013 
17014 	/* First lets see if we have old packets */
17015 	if (!STAILQ_EMPTY(&tp->t_inqueue)) {
17016 		if (ctf_do_queued_segments(tp, 1)) {
17017 			m_freem(m);
17018 			return;
17019 		}
17020 	}
17021 	if (m->m_flags & M_TSTMP_LRO) {
17022 		mbuf_tstmp2timeval(m, &tv);
17023 	} else {
17024 		/* Should not be should we kassert instead? */
17025 		tcp_get_usecs(&tv);
17026 	}
17027 	if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0,
17028 	    &tv) == 0) {
17029 		INP_WUNLOCK(tptoinpcb(tp));
17030 	}
17031 }
17032 
17033 struct rack_sendmap *
tcp_rack_output(struct tcpcb * tp,struct tcp_rack * rack,uint32_t tsused)17034 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
17035 {
17036 	struct rack_sendmap *rsm = NULL;
17037 	int32_t idx;
17038 	uint32_t srtt = 0, thresh = 0, ts_low = 0;
17039 
17040 	/* Return the next guy to be re-transmitted */
17041 	if (tqhash_empty(rack->r_ctl.tqh)) {
17042 		return (NULL);
17043 	}
17044 	if (tp->t_flags & TF_SENTFIN) {
17045 		/* retran the end FIN? */
17046 		return (NULL);
17047 	}
17048 	/* ok lets look at this one */
17049 	rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17050 	if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
17051 		return (rsm);
17052 	}
17053 	if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
17054 		goto check_it;
17055 	}
17056 	rsm = rack_find_lowest_rsm(rack);
17057 	if (rsm == NULL) {
17058 		return (NULL);
17059 	}
17060 check_it:
17061 	if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
17062 	    (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
17063 		/*
17064 		 * No sack so we automatically do the 3 strikes and
17065 		 * retransmit (no rack timer would be started).
17066 		 */
17067 		return (rsm);
17068 	}
17069 	if (rsm->r_flags & RACK_ACKED) {
17070 		return (NULL);
17071 	}
17072 	if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
17073 	    (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
17074 		/* Its not yet ready */
17075 		return (NULL);
17076 	}
17077 	srtt = rack_grab_rtt(tp, rack);
17078 	idx = rsm->r_rtr_cnt - 1;
17079 	ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
17080 	thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1);
17081 	if ((tsused == ts_low) ||
17082 	    (TSTMP_LT(tsused, ts_low))) {
17083 		/* No time since sending */
17084 		return (NULL);
17085 	}
17086 	if ((tsused - ts_low) < thresh) {
17087 		/* It has not been long enough yet */
17088 		return (NULL);
17089 	}
17090 	if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
17091 	    ((rsm->r_flags & RACK_SACK_PASSED))) {
17092 		/*
17093 		 * We have passed the dup-ack threshold <or>
17094 		 * a SACK has indicated this is missing.
17095 		 * Note that if you are a declared attacker
17096 		 * it is only the dup-ack threshold that
17097 		 * will cause retransmits.
17098 		 */
17099 		/* log retransmit reason */
17100 		rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
17101 		rack->r_fast_output = 0;
17102 		return (rsm);
17103 	}
17104 	return (NULL);
17105 }
17106 
17107 static void
rack_log_pacing_delay_calc(struct tcp_rack * rack,uint32_t len,uint32_t slot,uint64_t bw_est,uint64_t bw,uint64_t len_time,int method,int line,struct rack_sendmap * rsm,uint8_t quality)17108 rack_log_pacing_delay_calc (struct tcp_rack *rack, uint32_t len, uint32_t slot,
17109 			   uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
17110 			   int line, struct rack_sendmap *rsm, uint8_t quality)
17111 {
17112 	if (tcp_bblogging_on(rack->rc_tp)) {
17113 		union tcp_log_stackspecific log;
17114 		struct timeval tv;
17115 
17116 		if (rack_verbose_logging == 0) {
17117 			/*
17118 			 * We are not verbose screen out all but
17119 			 * ones we always want.
17120 			 */
17121 			if ((method != 2) &&
17122 			    (method != 3) &&
17123 			    (method != 7) &&
17124 			    (method != 89) &&
17125 			    (method != 14) &&
17126 			    (method != 20)) {
17127 				return;
17128 			}
17129 		}
17130 		memset(&log, 0, sizeof(log));
17131 		log.u_bbr.flex1 = slot;
17132 		log.u_bbr.flex2 = len;
17133 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
17134 		log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
17135 		log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
17136 		log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
17137 		log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
17138 		log.u_bbr.use_lt_bw <<= 1;
17139 		log.u_bbr.use_lt_bw |= rack->r_late;
17140 		log.u_bbr.use_lt_bw <<= 1;
17141 		log.u_bbr.use_lt_bw |= rack->r_early;
17142 		log.u_bbr.use_lt_bw <<= 1;
17143 		log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
17144 		log.u_bbr.use_lt_bw <<= 1;
17145 		log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
17146 		log.u_bbr.use_lt_bw <<= 1;
17147 		log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
17148 		log.u_bbr.use_lt_bw <<= 1;
17149 		log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
17150 		log.u_bbr.use_lt_bw <<= 1;
17151 		log.u_bbr.use_lt_bw |= rack->gp_ready;
17152 		log.u_bbr.pkt_epoch = line;
17153 		log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
17154 		log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
17155 		log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
17156 		log.u_bbr.bw_inuse = bw_est;
17157 		log.u_bbr.delRate = bw;
17158 		if (rack->r_ctl.gp_bw == 0)
17159 			log.u_bbr.cur_del_rate = 0;
17160 		else
17161 			log.u_bbr.cur_del_rate = rack_get_bw(rack);
17162 		log.u_bbr.rttProp = len_time;
17163 		log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
17164 		log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
17165 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
17166 		if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
17167 			/* We are in slow start */
17168 			log.u_bbr.flex7 = 1;
17169 		} else {
17170 			/* we are on congestion avoidance */
17171 			log.u_bbr.flex7 = 0;
17172 		}
17173 		log.u_bbr.flex8 = method;
17174 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17175 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
17176 		log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
17177 		log.u_bbr.cwnd_gain <<= 1;
17178 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
17179 		log.u_bbr.cwnd_gain <<= 1;
17180 		log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
17181 		log.u_bbr.bbr_substate = quality;
17182 		log.u_bbr.bbr_state = rack->dgp_on;
17183 		log.u_bbr.bbr_state <<= 1;
17184 		log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd;
17185 		log.u_bbr.bbr_state <<= 2;
17186 		TCP_LOG_EVENTP(rack->rc_tp, NULL,
17187 		    &rack->rc_inp->inp_socket->so_rcv,
17188 		    &rack->rc_inp->inp_socket->so_snd,
17189 		    BBR_LOG_HPTSI_CALC, 0,
17190 		    0, &log, false, &tv);
17191 	}
17192 }
17193 
17194 static uint32_t
rack_get_pacing_len(struct tcp_rack * rack,uint64_t bw,uint32_t mss)17195 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
17196 {
17197 	uint32_t new_tso, user_max, pace_one;
17198 
17199 	user_max = rack->rc_user_set_max_segs * mss;
17200 	if (rack->rc_force_max_seg) {
17201 		return (user_max);
17202 	}
17203 	if (rack->use_fixed_rate &&
17204 	    ((rack->r_ctl.crte == NULL) ||
17205 	     (bw != rack->r_ctl.crte->rate))) {
17206 		/* Use the user mss since we are not exactly matched */
17207 		return (user_max);
17208 	}
17209 	if (rack_pace_one_seg ||
17210 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17211 		pace_one = 1;
17212 	else
17213 		pace_one = 0;
17214 
17215 	new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss,
17216 		     pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
17217 	if (new_tso > user_max)
17218 		new_tso = user_max;
17219 	if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) {
17220 		if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso)
17221 			new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss;
17222 	}
17223 	if (rack->r_ctl.rc_user_set_min_segs &&
17224 	    ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso))
17225 	    new_tso = rack->r_ctl.rc_user_set_min_segs * mss;
17226 	return (new_tso);
17227 }
17228 
17229 static uint64_t
rack_arrive_at_discounted_rate(struct tcp_rack * rack,uint64_t window_input,uint32_t * rate_set,uint32_t * gain_b)17230 rack_arrive_at_discounted_rate(struct tcp_rack *rack, uint64_t window_input, uint32_t *rate_set, uint32_t *gain_b)
17231 {
17232 	uint64_t reduced_win;
17233 	uint32_t gain;
17234 
17235 	if (window_input < rc_init_window(rack)) {
17236 		/*
17237 		 * The cwnd is collapsed to
17238 		 * nearly zero, maybe because of a time-out?
17239 		 * Lets drop back to the lt-bw.
17240 		 */
17241 		reduced_win = rack_get_lt_bw(rack);
17242 		/* Set the flag so the caller knows its a rate and not a reduced window */
17243 		*rate_set = 1;
17244 		gain = 100;
17245 	} else if  (IN_RECOVERY(rack->rc_tp->t_flags)) {
17246 		/*
17247 		 * If we are in recover our cwnd needs to be less for
17248 		 * our pacing consideration.
17249 		 */
17250 		if (rack->rack_hibeta == 0) {
17251 			reduced_win = window_input / 2;
17252 			gain = 50;
17253 		} else {
17254 			reduced_win = window_input * rack->r_ctl.saved_hibeta;
17255 			reduced_win /= 100;
17256 			gain = rack->r_ctl.saved_hibeta;
17257 		}
17258 	} else {
17259 		/*
17260 		 * Apply Timely factor to increase/decrease the
17261 		 * amount we are pacing at.
17262 		 */
17263 		gain = rack_get_output_gain(rack, NULL);
17264 		if (gain > rack_gain_p5_ub) {
17265 			gain = rack_gain_p5_ub;
17266 		}
17267 		reduced_win = window_input * gain;
17268 		reduced_win /= 100;
17269 	}
17270 	if (gain_b != NULL)
17271 		*gain_b = gain;
17272 	/*
17273 	 * What is being returned here is a trimmed down
17274 	 * window values in all cases where rate_set is left
17275 	 * at 0. In one case we actually return the rate (lt_bw).
17276 	 * the "reduced_win" is returned as a slimmed down cwnd that
17277 	 * is then calculated by the caller into a rate when rate_set
17278 	 * is 0.
17279 	 */
17280 	return (reduced_win);
17281 }
17282 
17283 static int32_t
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)17284 pace_to_fill_cwnd(struct tcp_rack *rack, int32_t slot, uint32_t len, uint32_t segsiz, int *capped, uint64_t *rate_wanted, uint8_t non_paced)
17285 {
17286 	uint64_t lentim, fill_bw;
17287 
17288 	rack->r_via_fill_cw = 0;
17289 	if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
17290 		return (slot);
17291 	if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
17292 		return (slot);
17293 	if (rack->r_ctl.rc_last_us_rtt == 0)
17294 		return (slot);
17295 	if (rack->rc_pace_fill_if_rttin_range &&
17296 	    (rack->r_ctl.rc_last_us_rtt >=
17297 	     (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
17298 		/* The rtt is huge, N * smallest, lets not fill */
17299 		return (slot);
17300 	}
17301 	if (rack->r_ctl.fillcw_cap && *rate_wanted >= rack->r_ctl.fillcw_cap)
17302 		return (slot);
17303 	/*
17304 	 * first lets calculate the b/w based on the last us-rtt
17305 	 * and the the smallest send window.
17306 	 */
17307 	fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17308 	if (rack->rc_fillcw_apply_discount) {
17309 		uint32_t rate_set = 0;
17310 
17311 		fill_bw = rack_arrive_at_discounted_rate(rack, fill_bw, &rate_set, NULL);
17312 		if (rate_set) {
17313 			goto at_lt_bw;
17314 		}
17315 	}
17316 	/* Take the rwnd if its smaller */
17317 	if (fill_bw > rack->rc_tp->snd_wnd)
17318 		fill_bw = rack->rc_tp->snd_wnd;
17319 	/* Now lets make it into a b/w */
17320 	fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
17321 	fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17322 	/* Adjust to any cap */
17323 	if (rack->r_ctl.fillcw_cap && fill_bw >= rack->r_ctl.fillcw_cap)
17324 		fill_bw = rack->r_ctl.fillcw_cap;
17325 
17326 at_lt_bw:
17327 	if (rack_bw_multipler > 0) {
17328 		/*
17329 		 * We want to limit fill-cw to the some multiplier
17330 		 * of the max(lt_bw, gp_est). The normal default
17331 		 * is 0 for off, so a sysctl has enabled it.
17332 		 */
17333 		uint64_t lt_bw, gp, rate;
17334 
17335 		gp = rack_get_gp_est(rack);
17336 		lt_bw = rack_get_lt_bw(rack);
17337 		if (lt_bw > gp)
17338 			rate = lt_bw;
17339 		else
17340 			rate = gp;
17341 		rate *= rack_bw_multipler;
17342 		rate /= 100;
17343 		if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
17344 			union tcp_log_stackspecific log;
17345 			struct timeval tv;
17346 
17347 			memset(&log, 0, sizeof(log));
17348 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17349 			log.u_bbr.flex1 = rack_bw_multipler;
17350 			log.u_bbr.flex2 = len;
17351 			log.u_bbr.cur_del_rate = gp;
17352 			log.u_bbr.delRate = lt_bw;
17353 			log.u_bbr.bw_inuse = rate;
17354 			log.u_bbr.rttProp = fill_bw;
17355 			log.u_bbr.flex8 = 44;
17356 			tcp_log_event(rack->rc_tp, NULL, NULL, NULL,
17357 				      BBR_LOG_CWND, 0,
17358 				      0, &log, false, NULL,
17359 				      __func__, __LINE__, &tv);
17360 		}
17361 		if (fill_bw > rate)
17362 			fill_bw = rate;
17363 	}
17364 	/* We are below the min b/w */
17365 	if (non_paced)
17366 		*rate_wanted = fill_bw;
17367 	if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
17368 		return (slot);
17369 	rack->r_via_fill_cw = 1;
17370 	if (rack->r_rack_hw_rate_caps &&
17371 	    (rack->r_ctl.crte != NULL)) {
17372 		uint64_t high_rate;
17373 
17374 		high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
17375 		if (fill_bw > high_rate) {
17376 			/* We are capping bw at the highest rate table entry */
17377 			if (*rate_wanted > high_rate) {
17378 				/* The original rate was also capped */
17379 				rack->r_via_fill_cw = 0;
17380 			}
17381 			rack_log_hdwr_pacing(rack,
17382 					     fill_bw, high_rate, __LINE__,
17383 					     0, 3);
17384 			fill_bw = high_rate;
17385 			if (capped)
17386 				*capped = 1;
17387 		}
17388 	} else if ((rack->r_ctl.crte == NULL) &&
17389 		   (rack->rack_hdrw_pacing == 0) &&
17390 		   (rack->rack_hdw_pace_ena) &&
17391 		   rack->r_rack_hw_rate_caps &&
17392 		   (rack->rack_attempt_hdwr_pace == 0) &&
17393 		   (rack->rc_inp->inp_route.ro_nh != NULL) &&
17394 		   (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17395 		/*
17396 		 * Ok we may have a first attempt that is greater than our top rate
17397 		 * lets check.
17398 		 */
17399 		uint64_t high_rate;
17400 
17401 		high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
17402 		if (high_rate) {
17403 			if (fill_bw > high_rate) {
17404 				fill_bw = high_rate;
17405 				if (capped)
17406 					*capped = 1;
17407 			}
17408 		}
17409 	}
17410 	if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) {
17411 		rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
17412 				   fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL, __LINE__);
17413 		fill_bw = rack->r_ctl.bw_rate_cap;
17414 	}
17415 	/*
17416 	 * Ok fill_bw holds our mythical b/w to fill the cwnd
17417 	 * in an rtt (unless it was capped), what does that
17418 	 * time wise equate too?
17419 	 */
17420 	lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
17421 	lentim /= fill_bw;
17422 	*rate_wanted = fill_bw;
17423 	if (non_paced || (lentim < slot)) {
17424 		rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
17425 					   0, lentim, 12, __LINE__, NULL, 0);
17426 		return ((int32_t)lentim);
17427 	} else
17428 		return (slot);
17429 }
17430 
17431 static int32_t
rack_get_pacing_delay(struct tcp_rack * rack,struct tcpcb * tp,uint32_t len,struct rack_sendmap * rsm,uint32_t segsiz,int line)17432 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz, int line)
17433 {
17434 	uint64_t srtt;
17435 	int32_t slot = 0;
17436 	int can_start_hw_pacing = 1;
17437 	int err;
17438 	int pace_one;
17439 
17440 	if (rack_pace_one_seg ||
17441 	    (rack->r_ctl.rc_user_set_min_segs == 1))
17442 		pace_one = 1;
17443 	else
17444 		pace_one = 0;
17445 	if (rack->rc_always_pace == 0) {
17446 		/*
17447 		 * We use the most optimistic possible cwnd/srtt for
17448 		 * sending calculations. This will make our
17449 		 * calculation anticipate getting more through
17450 		 * quicker then possible. But thats ok we don't want
17451 		 * the peer to have a gap in data sending.
17452 		 */
17453 		uint64_t cwnd, tr_perms = 0;
17454 		int32_t reduce;
17455 
17456 	old_method:
17457 		/*
17458 		 * We keep no precise pacing with the old method
17459 		 * instead we use the pacer to mitigate bursts.
17460 		 */
17461 		if (rack->r_ctl.rc_rack_min_rtt)
17462 			srtt = rack->r_ctl.rc_rack_min_rtt;
17463 		else
17464 			srtt = max(tp->t_srtt, 1);
17465 		if (rack->r_ctl.rc_rack_largest_cwnd)
17466 			cwnd = rack->r_ctl.rc_rack_largest_cwnd;
17467 		else
17468 			cwnd = rack->r_ctl.cwnd_to_use;
17469 		/* Inflate cwnd by 1000 so srtt of usecs is in ms */
17470 		tr_perms = (cwnd * 1000) / srtt;
17471 		if (tr_perms == 0) {
17472 			tr_perms = ctf_fixed_maxseg(tp);
17473 		}
17474 		/*
17475 		 * Calculate how long this will take to drain, if
17476 		 * the calculation comes out to zero, thats ok we
17477 		 * will use send_a_lot to possibly spin around for
17478 		 * more increasing tot_len_this_send to the point
17479 		 * that its going to require a pace, or we hit the
17480 		 * cwnd. Which in that case we are just waiting for
17481 		 * a ACK.
17482 		 */
17483 		slot = len / tr_perms;
17484 		/* Now do we reduce the time so we don't run dry? */
17485 		if (slot && rack_slot_reduction) {
17486 			reduce = (slot / rack_slot_reduction);
17487 			if (reduce < slot) {
17488 				slot -= reduce;
17489 			} else
17490 				slot = 0;
17491 		} else
17492 			reduce = 0;
17493 		slot *= HPTS_USEC_IN_MSEC;
17494 		if (rack->rc_pace_to_cwnd) {
17495 			uint64_t rate_wanted = 0;
17496 
17497 			slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
17498 			rack->rc_ack_can_sendout_data = 1;
17499 			rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
17500 		} else
17501 			rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
17502 		/*******************************************************/
17503 		/* RRS: We insert non-paced call to stats here for len */
17504 		/*******************************************************/
17505 	} else {
17506 		uint64_t bw_est, res, lentim, rate_wanted;
17507 		uint32_t segs, oh;
17508 		int capped = 0;
17509 		int prev_fill;
17510 
17511 		if ((rack->r_rr_config == 1) && rsm) {
17512 			return (rack->r_ctl.rc_min_to);
17513 		}
17514 		if (rack->use_fixed_rate) {
17515 			rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
17516 		} else if ((rack->r_ctl.init_rate == 0) &&
17517 			   (rack->r_ctl.gp_bw == 0)) {
17518 			/* no way to yet do an estimate */
17519 			bw_est = rate_wanted = 0;
17520 		} else if (rack->dgp_on) {
17521 			bw_est = rack_get_bw(rack);
17522 			rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
17523 		} else {
17524 			uint32_t gain, rate_set = 0;
17525 
17526 			rate_wanted = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17527 			rate_wanted = rack_arrive_at_discounted_rate(rack, rate_wanted, &rate_set, &gain);
17528 			if (rate_set == 0) {
17529 				if (rate_wanted > rack->rc_tp->snd_wnd)
17530 					rate_wanted = rack->rc_tp->snd_wnd;
17531 				/* Now lets make it into a b/w */
17532 				rate_wanted *= (uint64_t)HPTS_USEC_IN_SEC;
17533 				rate_wanted /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17534 			}
17535 			bw_est = rate_wanted;
17536 			rack_log_pacing_delay_calc(rack, rack->rc_tp->snd_cwnd,
17537 						   rack->r_ctl.cwnd_to_use,
17538 						   rate_wanted, bw_est,
17539 						   rack->r_ctl.rc_last_us_rtt,
17540 						   88, __LINE__, NULL, gain);
17541 		}
17542 		if ((bw_est == 0) || (rate_wanted == 0) ||
17543 		    ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
17544 			/*
17545 			 * No way yet to make a b/w estimate or
17546 			 * our raise is set incorrectly.
17547 			 */
17548 			goto old_method;
17549 		}
17550 		rack_rate_cap_bw(rack, &rate_wanted, &capped);
17551 		/* We need to account for all the overheads */
17552 		segs = (len + segsiz - 1) / segsiz;
17553 		/*
17554 		 * We need the diff between 1514 bytes (e-mtu with e-hdr)
17555 		 * and how much data we put in each packet. Yes this
17556 		 * means we may be off if we are larger than 1500 bytes
17557 		 * or smaller. But this just makes us more conservative.
17558 		 */
17559 
17560 		oh =  (tp->t_maxseg - segsiz) + sizeof(struct tcphdr);
17561 		if (rack->r_is_v6) {
17562 #ifdef INET6
17563 			oh += sizeof(struct ip6_hdr);
17564 #endif
17565 		} else {
17566 #ifdef INET
17567 			oh += sizeof(struct ip);
17568 #endif
17569 		}
17570 		/* We add a fixed 14 for the ethernet header */
17571 		oh += 14;
17572 		segs *= oh;
17573 		lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
17574 		res = lentim / rate_wanted;
17575 		slot = (uint32_t)res;
17576 		if (rack_hw_rate_min &&
17577 		    (rate_wanted < rack_hw_rate_min)) {
17578 			can_start_hw_pacing = 0;
17579 			if (rack->r_ctl.crte) {
17580 				/*
17581 				 * Ok we need to release it, we
17582 				 * have fallen too low.
17583 				 */
17584 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17585 				rack->r_ctl.crte = NULL;
17586 				rack->rack_attempt_hdwr_pace = 0;
17587 				rack->rack_hdrw_pacing = 0;
17588 			}
17589 		}
17590 		if (rack->r_ctl.crte &&
17591 		    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17592 			/*
17593 			 * We want more than the hardware can give us,
17594 			 * don't start any hw pacing.
17595 			 */
17596 			can_start_hw_pacing = 0;
17597 			if (rack->r_rack_hw_rate_caps == 0) {
17598 				/*
17599 				 * Ok we need to release it, we
17600 				 * want more than the card can give us and
17601 				 * no rate cap is in place. Set it up so
17602 				 * when we want less we can retry.
17603 				 */
17604 				tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17605 				rack->r_ctl.crte = NULL;
17606 				rack->rack_attempt_hdwr_pace = 0;
17607 				rack->rack_hdrw_pacing = 0;
17608 			}
17609 		}
17610 		if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) {
17611 			/*
17612 			 * We lost our rate somehow, this can happen
17613 			 * if the interface changed underneath us.
17614 			 */
17615 			tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17616 			rack->r_ctl.crte = NULL;
17617 			/* Lets re-allow attempting to setup pacing */
17618 			rack->rack_hdrw_pacing = 0;
17619 			rack->rack_attempt_hdwr_pace = 0;
17620 			rack_log_hdwr_pacing(rack,
17621 					     rate_wanted, bw_est, __LINE__,
17622 					     0, 6);
17623 		}
17624 		prev_fill = rack->r_via_fill_cw;
17625 		if ((rack->rc_pace_to_cwnd) &&
17626 		    (capped == 0) &&
17627 		    (rack->dgp_on == 1) &&
17628 		    (rack->use_fixed_rate == 0) &&
17629 		    (rack->in_probe_rtt == 0) &&
17630 		    (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
17631 			/*
17632 			 * We want to pace at our rate *or* faster to
17633 			 * fill the cwnd to the max if its not full.
17634 			 */
17635 			slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
17636 			/* Re-check to make sure we are not exceeding our max b/w */
17637 			if ((rack->r_ctl.crte != NULL) &&
17638 			    (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17639 				/*
17640 				 * We want more than the hardware can give us,
17641 				 * don't start any hw pacing.
17642 				 */
17643 				can_start_hw_pacing = 0;
17644 				if (rack->r_rack_hw_rate_caps == 0) {
17645 					/*
17646 					 * Ok we need to release it, we
17647 					 * want more than the card can give us and
17648 					 * no rate cap is in place. Set it up so
17649 					 * when we want less we can retry.
17650 					 */
17651 					tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17652 					rack->r_ctl.crte = NULL;
17653 					rack->rack_attempt_hdwr_pace = 0;
17654 					rack->rack_hdrw_pacing = 0;
17655 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
17656 				}
17657 			}
17658 		}
17659 		if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
17660 		    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17661 			if ((rack->rack_hdw_pace_ena) &&
17662 			    (can_start_hw_pacing > 0) &&
17663 			    (rack->rack_hdrw_pacing == 0) &&
17664 			    (rack->rack_attempt_hdwr_pace == 0)) {
17665 				/*
17666 				 * Lets attempt to turn on hardware pacing
17667 				 * if we can.
17668 				 */
17669 				rack->rack_attempt_hdwr_pace = 1;
17670 				rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
17671 								       rack->rc_inp->inp_route.ro_nh->nh_ifp,
17672 								       rate_wanted,
17673 								       RS_PACING_GEQ,
17674 								       &err, &rack->r_ctl.crte_prev_rate);
17675 				if (rack->r_ctl.crte) {
17676 					rack->rack_hdrw_pacing = 1;
17677 					rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz,
17678 													   pace_one, rack->r_ctl.crte,
17679 													   NULL, rack->r_ctl.pace_len_divisor);
17680 					rack_log_hdwr_pacing(rack,
17681 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17682 							     err, 0);
17683 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17684 				} else {
17685 					counter_u64_add(rack_hw_pace_init_fail, 1);
17686 				}
17687 			} else if (rack->rack_hdrw_pacing &&
17688 				   (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
17689 				/* Do we need to adjust our rate? */
17690 				const struct tcp_hwrate_limit_table *nrte;
17691 
17692 				if (rack->r_up_only &&
17693 				    (rate_wanted < rack->r_ctl.crte->rate)) {
17694 					/**
17695 					 * We have four possible states here
17696 					 * having to do with the previous time
17697 					 * and this time.
17698 					 *   previous  |  this-time
17699 					 * A)     0      |     0   -- fill_cw not in the picture
17700 					 * B)     1      |     0   -- we were doing a fill-cw but now are not
17701 					 * C)     1      |     1   -- all rates from fill_cw
17702 					 * D)     0      |     1   -- we were doing non-fill and now we are filling
17703 					 *
17704 					 * For case A, C and D we don't allow a drop. But for
17705 					 * case B where we now our on our steady rate we do
17706 					 * allow a drop.
17707 					 *
17708 					 */
17709 					if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
17710 						goto done_w_hdwr;
17711 				}
17712 				if ((rate_wanted > rack->r_ctl.crte->rate) ||
17713 				    (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
17714 					if (rack_hw_rate_to_low &&
17715 					    (bw_est < rack_hw_rate_to_low)) {
17716 						/*
17717 						 * The pacing rate is too low for hardware, but
17718 						 * do allow hardware pacing to be restarted.
17719 						 */
17720 						rack_log_hdwr_pacing(rack,
17721 								     bw_est, rack->r_ctl.crte->rate, __LINE__,
17722 								     0, 5);
17723 						tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17724 						rack->r_ctl.crte = NULL;
17725 						rack->rack_attempt_hdwr_pace = 0;
17726 						rack->rack_hdrw_pacing = 0;
17727 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17728 						goto done_w_hdwr;
17729 					}
17730 					nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
17731 								   rack->rc_tp,
17732 								   rack->rc_inp->inp_route.ro_nh->nh_ifp,
17733 								   rate_wanted,
17734 								   RS_PACING_GEQ,
17735 								   &err, &rack->r_ctl.crte_prev_rate);
17736 					if (nrte == NULL) {
17737 						/*
17738 						 * Lost the rate, lets drop hardware pacing
17739 						 * period.
17740 						 */
17741 						rack->rack_hdrw_pacing = 0;
17742 						rack->r_ctl.crte = NULL;
17743 						rack_log_hdwr_pacing(rack,
17744 								     rate_wanted, 0, __LINE__,
17745 								     err, 1);
17746 						rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17747 						counter_u64_add(rack_hw_pace_lost, 1);
17748 					} else if (nrte != rack->r_ctl.crte) {
17749 						rack->r_ctl.crte = nrte;
17750 						rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted,
17751 														   segsiz, pace_one, rack->r_ctl.crte,
17752 														   NULL, rack->r_ctl.pace_len_divisor);
17753 						rack_log_hdwr_pacing(rack,
17754 								     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17755 								     err, 2);
17756 						rack->r_ctl.last_hw_bw_req = rate_wanted;
17757 					}
17758 				} else {
17759 					/* We just need to adjust the segment size */
17760 					rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17761 					rack_log_hdwr_pacing(rack,
17762 							     rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17763 							     0, 4);
17764 					rack->r_ctl.last_hw_bw_req = rate_wanted;
17765 				}
17766 			}
17767 		}
17768 	done_w_hdwr:
17769 		if (rack_limit_time_with_srtt &&
17770 		    (rack->use_fixed_rate == 0) &&
17771 		    (rack->rack_hdrw_pacing == 0)) {
17772 			/*
17773 			 * Sanity check, we do not allow the pacing delay
17774 			 * to be longer than the SRTT of the path. If it is
17775 			 * a slow path, then adding a packet should increase
17776 			 * the RTT and compensate for this i.e. the srtt will
17777 			 * be greater so the allowed pacing time will be greater.
17778 			 *
17779 			 * Note this restriction is not for where a peak rate
17780 			 * is set, we are doing fixed pacing or hardware pacing.
17781 			 */
17782 			if (rack->rc_tp->t_srtt)
17783 				srtt = rack->rc_tp->t_srtt;
17784 			else
17785 				srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;	/* its in ms convert */
17786 			if (srtt < (uint64_t)slot) {
17787 				rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
17788 				slot = srtt;
17789 			}
17790 		}
17791 		/*******************************************************************/
17792 		/* RRS: We insert paced call to stats here for len and rate_wanted */
17793 		/*******************************************************************/
17794 		rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
17795 	}
17796 	if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
17797 		/*
17798 		 * If this rate is seeing enobufs when it
17799 		 * goes to send then either the nic is out
17800 		 * of gas or we are mis-estimating the time
17801 		 * somehow and not letting the queue empty
17802 		 * completely. Lets add to the pacing time.
17803 		 */
17804 		int hw_boost_delay;
17805 
17806 		hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
17807 		if (hw_boost_delay > rack_enobuf_hw_max)
17808 			hw_boost_delay = rack_enobuf_hw_max;
17809 		else if (hw_boost_delay < rack_enobuf_hw_min)
17810 			hw_boost_delay = rack_enobuf_hw_min;
17811 		slot += hw_boost_delay;
17812 	}
17813 	return (slot);
17814 }
17815 
17816 static void
rack_start_gp_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq startseq,uint32_t sb_offset)17817 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
17818     tcp_seq startseq, uint32_t sb_offset)
17819 {
17820 	struct rack_sendmap *my_rsm = NULL;
17821 
17822 	if (tp->t_state < TCPS_ESTABLISHED) {
17823 		/*
17824 		 * We don't start any measurements if we are
17825 		 * not at least established.
17826 		 */
17827 		return;
17828 	}
17829 	if (tp->t_state >= TCPS_FIN_WAIT_1) {
17830 		/*
17831 		 * We will get no more data into the SB
17832 		 * this means we need to have the data available
17833 		 * before we start a measurement.
17834 		 */
17835 
17836 		if (sbavail(&tptosocket(tp)->so_snd) <
17837 		    max(rc_init_window(rack),
17838 			(MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
17839 			/* Nope not enough data */
17840 			return;
17841 		}
17842 	}
17843 	tp->t_flags |= TF_GPUTINPROG;
17844 	rack->r_ctl.rc_gp_cumack_ts = 0;
17845 	rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
17846 	rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
17847 	tp->gput_seq = startseq;
17848 	rack->app_limited_needs_set = 0;
17849 	if (rack->in_probe_rtt)
17850 		rack->measure_saw_probe_rtt = 1;
17851 	else if ((rack->measure_saw_probe_rtt) &&
17852 		 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
17853 		rack->measure_saw_probe_rtt = 0;
17854 	if (rack->rc_gp_filled)
17855 		tp->gput_ts = rack->r_ctl.last_cumack_advance;
17856 	else {
17857 		/* Special case initial measurement */
17858 		struct timeval tv;
17859 
17860 		tp->gput_ts = tcp_get_usecs(&tv);
17861 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
17862 	}
17863 	/*
17864 	 * We take a guess out into the future,
17865 	 * if we have no measurement and no
17866 	 * initial rate, we measure the first
17867 	 * initial-windows worth of data to
17868 	 * speed up getting some GP measurement and
17869 	 * thus start pacing.
17870 	 */
17871 	if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
17872 		rack->app_limited_needs_set = 1;
17873 		tp->gput_ack = startseq + max(rc_init_window(rack),
17874 					      (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
17875 		rack_log_pacing_delay_calc(rack,
17876 					   tp->gput_seq,
17877 					   tp->gput_ack,
17878 					   0,
17879 					   tp->gput_ts,
17880 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17881 					   9,
17882 					   __LINE__, NULL, 0);
17883 		rack_tend_gp_marks(tp, rack);
17884 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
17885 		return;
17886 	}
17887 	if (sb_offset) {
17888 		/*
17889 		 * We are out somewhere in the sb
17890 		 * can we use the already outstanding data?
17891 		 */
17892 
17893 		if (rack->r_ctl.rc_app_limited_cnt == 0) {
17894 			/*
17895 			 * Yes first one is good and in this case
17896 			 * the tp->gput_ts is correctly set based on
17897 			 * the last ack that arrived (no need to
17898 			 * set things up when an ack comes in).
17899 			 */
17900 			my_rsm = tqhash_min(rack->r_ctl.tqh);
17901 			if ((my_rsm == NULL) ||
17902 			    (my_rsm->r_rtr_cnt != 1)) {
17903 				/* retransmission? */
17904 				goto use_latest;
17905 			}
17906 		} else {
17907 			if (rack->r_ctl.rc_first_appl == NULL) {
17908 				/*
17909 				 * If rc_first_appl is NULL
17910 				 * then the cnt should be 0.
17911 				 * This is probably an error, maybe
17912 				 * a KASSERT would be approprate.
17913 				 */
17914 				goto use_latest;
17915 			}
17916 			/*
17917 			 * If we have a marker pointer to the last one that is
17918 			 * app limited we can use that, but we need to set
17919 			 * things up so that when it gets ack'ed we record
17920 			 * the ack time (if its not already acked).
17921 			 */
17922 			rack->app_limited_needs_set = 1;
17923 			/*
17924 			 * We want to get to the rsm that is either
17925 			 * next with space i.e. over 1 MSS or the one
17926 			 * after that (after the app-limited).
17927 			 */
17928 			my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl);
17929 			if (my_rsm) {
17930 				if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
17931 					/* Have to use the next one */
17932 					my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17933 				else {
17934 					/* Use after the first MSS of it is acked */
17935 					tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
17936 					goto start_set;
17937 				}
17938 			}
17939 			if ((my_rsm == NULL) ||
17940 			    (my_rsm->r_rtr_cnt != 1)) {
17941 				/*
17942 				 * Either its a retransmit or
17943 				 * the last is the app-limited one.
17944 				 */
17945 				goto use_latest;
17946 			}
17947 		}
17948 		tp->gput_seq = my_rsm->r_start;
17949 start_set:
17950 		if (my_rsm->r_flags & RACK_ACKED) {
17951 			/*
17952 			 * This one has been acked use the arrival ack time
17953 			 */
17954 			struct rack_sendmap *nrsm;
17955 
17956 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
17957 			rack->app_limited_needs_set = 0;
17958 			/*
17959 			 * Ok in this path we need to use the r_end now
17960 			 * since this guy is the starting ack.
17961 			 */
17962 			tp->gput_seq = my_rsm->r_end;
17963 			/*
17964 			 * We also need to adjust up the sendtime
17965 			 * to the send of the next data after my_rsm.
17966 			 */
17967 			nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17968 			if (nrsm != NULL)
17969 				my_rsm = nrsm;
17970 			else {
17971 				/*
17972 				 * The next as not been sent, thats the
17973 				 * case for using the latest.
17974 				 */
17975 				goto use_latest;
17976 			}
17977 		}
17978 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
17979 		tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
17980 		rack->r_ctl.rc_gp_cumack_ts = 0;
17981 		if ((rack->r_ctl.cleared_app_ack == 1) &&
17982 		    (SEQ_GEQ(rack->r_ctl.cleared_app_ack, tp->gput_seq))) {
17983 			/*
17984 			 * We just cleared an application limited period
17985 			 * so the next seq out needs to skip the first
17986 			 * ack.
17987 			 */
17988 			rack->app_limited_needs_set = 1;
17989 			rack->r_ctl.cleared_app_ack = 0;
17990 		}
17991 		rack_log_pacing_delay_calc(rack,
17992 					   tp->gput_seq,
17993 					   tp->gput_ack,
17994 					   (uintptr_t)my_rsm,
17995 					   tp->gput_ts,
17996 					   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17997 					   9,
17998 					   __LINE__, my_rsm, 0);
17999 		/* Now lets make sure all are marked as they should be */
18000 		rack_tend_gp_marks(tp, rack);
18001 		rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18002 		return;
18003 	}
18004 
18005 use_latest:
18006 	/*
18007 	 * We don't know how long we may have been
18008 	 * idle or if this is the first-send. Lets
18009 	 * setup the flag so we will trim off
18010 	 * the first ack'd data so we get a true
18011 	 * measurement.
18012 	 */
18013 	rack->app_limited_needs_set = 1;
18014 	tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
18015 	rack->r_ctl.rc_gp_cumack_ts = 0;
18016 	/* Find this guy so we can pull the send time */
18017 	my_rsm = tqhash_find(rack->r_ctl.tqh, startseq);
18018 	if (my_rsm) {
18019 		rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
18020 		if (my_rsm->r_flags & RACK_ACKED) {
18021 			/*
18022 			 * Unlikely since its probably what was
18023 			 * just transmitted (but I am paranoid).
18024 			 */
18025 			tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
18026 			rack->app_limited_needs_set = 0;
18027 		}
18028 		if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
18029 			/* This also is unlikely */
18030 			tp->gput_seq = my_rsm->r_start;
18031 		}
18032 	} else {
18033 		/*
18034 		 * TSNH unless we have some send-map limit,
18035 		 * and even at that it should not be hitting
18036 		 * that limit (we should have stopped sending).
18037 		 */
18038 		struct timeval tv;
18039 
18040 		microuptime(&tv);
18041 		rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
18042 	}
18043 	rack_tend_gp_marks(tp, rack);
18044 	rack_log_pacing_delay_calc(rack,
18045 				   tp->gput_seq,
18046 				   tp->gput_ack,
18047 				   (uintptr_t)my_rsm,
18048 				   tp->gput_ts,
18049 				   (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18050 				   9, __LINE__, NULL, 0);
18051 	rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18052 }
18053 
18054 static inline uint32_t
rack_what_can_we_send(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cwnd_to_use,uint32_t avail,int32_t sb_offset)18055 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
18056     uint32_t avail, int32_t sb_offset)
18057 {
18058 	uint32_t len;
18059 	uint32_t sendwin;
18060 
18061 	if (tp->snd_wnd > cwnd_to_use)
18062 		sendwin = cwnd_to_use;
18063 	else
18064 		sendwin = tp->snd_wnd;
18065 	if (ctf_outstanding(tp) >= tp->snd_wnd) {
18066 		/* We never want to go over our peers rcv-window */
18067 		len = 0;
18068 	} else {
18069 		uint32_t flight;
18070 
18071 		flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
18072 		if (flight >= sendwin) {
18073 			/*
18074 			 * We have in flight what we are allowed by cwnd (if
18075 			 * it was rwnd blocking it would have hit above out
18076 			 * >= tp->snd_wnd).
18077 			 */
18078 			return (0);
18079 		}
18080 		len = sendwin - flight;
18081 		if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
18082 			/* We would send too much (beyond the rwnd) */
18083 			len = tp->snd_wnd - ctf_outstanding(tp);
18084 		}
18085 		if ((len + sb_offset) > avail) {
18086 			/*
18087 			 * We don't have that much in the SB, how much is
18088 			 * there?
18089 			 */
18090 			len = avail - sb_offset;
18091 		}
18092 	}
18093 	return (len);
18094 }
18095 
18096 static void
rack_log_fsb(struct tcp_rack * rack,struct tcpcb * tp,struct socket * so,uint32_t flags,unsigned ipoptlen,int32_t orig_len,int32_t len,int error,int rsm_is_null,int optlen,int line,uint16_t mode)18097 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
18098 	     unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
18099 	     int rsm_is_null, int optlen, int line, uint16_t mode)
18100 {
18101 	if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
18102 		union tcp_log_stackspecific log;
18103 		struct timeval tv;
18104 
18105 		memset(&log, 0, sizeof(log));
18106 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18107 		log.u_bbr.flex1 = error;
18108 		log.u_bbr.flex2 = flags;
18109 		log.u_bbr.flex3 = rsm_is_null;
18110 		log.u_bbr.flex4 = ipoptlen;
18111 		log.u_bbr.flex5 = tp->rcv_numsacks;
18112 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18113 		log.u_bbr.flex7 = optlen;
18114 		log.u_bbr.flex8 = rack->r_fsb_inited;
18115 		log.u_bbr.applimited = rack->r_fast_output;
18116 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18117 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18118 		log.u_bbr.cwnd_gain = mode;
18119 		log.u_bbr.pkts_out = orig_len;
18120 		log.u_bbr.lt_epoch = len;
18121 		log.u_bbr.delivered = line;
18122 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18123 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18124 		tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
18125 			       len, &log, false, NULL, __func__, __LINE__, &tv);
18126 	}
18127 }
18128 
18129 
18130 static struct mbuf *
rack_fo_base_copym(struct mbuf * the_m,uint32_t the_off,int32_t * plen,struct rack_fast_send_blk * fsb,int32_t seglimit,int32_t segsize,int hw_tls)18131 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
18132 		   struct rack_fast_send_blk *fsb,
18133 		   int32_t seglimit, int32_t segsize, int hw_tls)
18134 {
18135 #ifdef KERN_TLS
18136 	struct ktls_session *tls, *ntls;
18137 #ifdef INVARIANTS
18138 	struct mbuf *start;
18139 #endif
18140 #endif
18141 	struct mbuf *m, *n, **np, *smb;
18142 	struct mbuf *top;
18143 	int32_t off, soff;
18144 	int32_t len = *plen;
18145 	int32_t fragsize;
18146 	int32_t len_cp = 0;
18147 	uint32_t mlen, frags;
18148 
18149 	soff = off = the_off;
18150 	smb = m = the_m;
18151 	np = &top;
18152 	top = NULL;
18153 #ifdef KERN_TLS
18154 	if (hw_tls && (m->m_flags & M_EXTPG))
18155 		tls = m->m_epg_tls;
18156 	else
18157 		tls = NULL;
18158 #ifdef INVARIANTS
18159 	start = m;
18160 #endif
18161 #endif
18162 	while (len > 0) {
18163 		if (m == NULL) {
18164 			*plen = len_cp;
18165 			break;
18166 		}
18167 #ifdef KERN_TLS
18168 		if (hw_tls) {
18169 			if (m->m_flags & M_EXTPG)
18170 				ntls = m->m_epg_tls;
18171 			else
18172 				ntls = NULL;
18173 
18174 			/*
18175 			 * Avoid mixing TLS records with handshake
18176 			 * data or TLS records from different
18177 			 * sessions.
18178 			 */
18179 			if (tls != ntls) {
18180 				MPASS(m != start);
18181 				*plen = len_cp;
18182 				break;
18183 			}
18184 		}
18185 #endif
18186 		mlen = min(len, m->m_len - off);
18187 		if (seglimit) {
18188 			/*
18189 			 * For M_EXTPG mbufs, add 3 segments
18190 			 * + 1 in case we are crossing page boundaries
18191 			 * + 2 in case the TLS hdr/trailer are used
18192 			 * It is cheaper to just add the segments
18193 			 * than it is to take the cache miss to look
18194 			 * at the mbuf ext_pgs state in detail.
18195 			 */
18196 			if (m->m_flags & M_EXTPG) {
18197 				fragsize = min(segsize, PAGE_SIZE);
18198 				frags = 3;
18199 			} else {
18200 				fragsize = segsize;
18201 				frags = 0;
18202 			}
18203 
18204 			/* Break if we really can't fit anymore. */
18205 			if ((frags + 1) >= seglimit) {
18206 				*plen =	len_cp;
18207 				break;
18208 			}
18209 
18210 			/*
18211 			 * Reduce size if you can't copy the whole
18212 			 * mbuf. If we can't copy the whole mbuf, also
18213 			 * adjust len so the loop will end after this
18214 			 * mbuf.
18215 			 */
18216 			if ((frags + howmany(mlen, fragsize)) >= seglimit) {
18217 				mlen = (seglimit - frags - 1) * fragsize;
18218 				len = mlen;
18219 				*plen = len_cp + len;
18220 			}
18221 			frags += howmany(mlen, fragsize);
18222 			if (frags == 0)
18223 				frags++;
18224 			seglimit -= frags;
18225 			KASSERT(seglimit > 0,
18226 			    ("%s: seglimit went too low", __func__));
18227 		}
18228 		n = m_get(M_NOWAIT, m->m_type);
18229 		*np = n;
18230 		if (n == NULL)
18231 			goto nospace;
18232 		n->m_len = mlen;
18233 		soff += mlen;
18234 		len_cp += n->m_len;
18235 		if (m->m_flags & (M_EXT | M_EXTPG)) {
18236 			n->m_data = m->m_data + off;
18237 			mb_dupcl(n, m);
18238 		} else {
18239 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
18240 			    (u_int)n->m_len);
18241 		}
18242 		len -= n->m_len;
18243 		off = 0;
18244 		m = m->m_next;
18245 		np = &n->m_next;
18246 		if (len || (soff == smb->m_len)) {
18247 			/*
18248 			 * We have more so we move forward  or
18249 			 * we have consumed the entire mbuf and
18250 			 * len has fell to 0.
18251 			 */
18252 			soff = 0;
18253 			smb = m;
18254 		}
18255 
18256 	}
18257 	if (fsb != NULL) {
18258 		fsb->m = smb;
18259 		fsb->off = soff;
18260 		if (smb) {
18261 			/*
18262 			 * Save off the size of the mbuf. We do
18263 			 * this so that we can recognize when it
18264 			 * has been trimmed by sbcut() as acks
18265 			 * come in.
18266 			 */
18267 			fsb->o_m_len = smb->m_len;
18268 			fsb->o_t_len = M_TRAILINGROOM(smb);
18269 		} else {
18270 			/*
18271 			 * This is the case where the next mbuf went to NULL. This
18272 			 * means with this copy we have sent everything in the sb.
18273 			 * In theory we could clear the fast_output flag, but lets
18274 			 * not since its possible that we could get more added
18275 			 * and acks that call the extend function which would let
18276 			 * us send more.
18277 			 */
18278 			fsb->o_m_len = 0;
18279 			fsb->o_t_len = 0;
18280 		}
18281 	}
18282 	return (top);
18283 nospace:
18284 	if (top)
18285 		m_freem(top);
18286 	return (NULL);
18287 
18288 }
18289 
18290 /*
18291  * This is a copy of m_copym(), taking the TSO segment size/limit
18292  * constraints into account, and advancing the sndptr as it goes.
18293  */
18294 static struct mbuf *
rack_fo_m_copym(struct tcp_rack * rack,int32_t * plen,int32_t seglimit,int32_t segsize,struct mbuf ** s_mb,int * s_soff)18295 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
18296 		int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
18297 {
18298 	struct mbuf *m, *n;
18299 	int32_t soff;
18300 
18301 	m = rack->r_ctl.fsb.m;
18302 	if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) {
18303 		/*
18304 		 * The trailing space changed, mbufs can grow
18305 		 * at the tail but they can't shrink from
18306 		 * it, KASSERT that. Adjust the orig_m_len to
18307 		 * compensate for this change.
18308 		 */
18309 		KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)),
18310 			("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
18311 			 m,
18312 			 rack,
18313 			 (intmax_t)M_TRAILINGROOM(m),
18314 			 rack->r_ctl.fsb.o_t_len,
18315 			 rack->r_ctl.fsb.o_m_len,
18316 			 m->m_len));
18317 		rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m));
18318 		rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m);
18319 	}
18320 	if (m->m_len < rack->r_ctl.fsb.o_m_len) {
18321 		/*
18322 		 * Mbuf shrank, trimmed off the top by an ack, our
18323 		 * offset changes.
18324 		 */
18325 		KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)),
18326 			("mbuf:%p len:%u rack:%p oml:%u soff:%u\n",
18327 			 m, m->m_len,
18328 			 rack, rack->r_ctl.fsb.o_m_len,
18329 			 rack->r_ctl.fsb.off));
18330 
18331 		if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len))
18332 			rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len);
18333 		else
18334 			rack->r_ctl.fsb.off = 0;
18335 		rack->r_ctl.fsb.o_m_len = m->m_len;
18336 #ifdef INVARIANTS
18337 	} else if (m->m_len > rack->r_ctl.fsb.o_m_len) {
18338 		panic("rack:%p m:%p m_len grew outside of t_space compensation",
18339 		      rack, m);
18340 #endif
18341 	}
18342 	soff = rack->r_ctl.fsb.off;
18343 	KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
18344 	KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
18345 	KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
18346 				 __FUNCTION__,
18347 				 rack, *plen, m, m->m_len));
18348 	/* Save off the right location before we copy and advance */
18349 	*s_soff = soff;
18350 	*s_mb = rack->r_ctl.fsb.m;
18351 	n = rack_fo_base_copym(m, soff, plen,
18352 			       &rack->r_ctl.fsb,
18353 			       seglimit, segsize, rack->r_ctl.fsb.hw_tls);
18354 	return (n);
18355 }
18356 
18357 /* Log the buffer level */
18358 static void
rack_log_queue_level(struct tcpcb * tp,struct tcp_rack * rack,int len,struct timeval * tv,uint32_t cts)18359 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack,
18360 		     int len, struct timeval *tv,
18361 		     uint32_t cts)
18362 {
18363 	uint32_t p_rate = 0, p_queue = 0, err = 0;
18364 	union tcp_log_stackspecific log;
18365 
18366 #ifdef RATELIMIT
18367 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18368 	err = in_pcbquery_txrtlmt(rack->rc_inp,	&p_rate);
18369 #endif
18370 	memset(&log, 0, sizeof(log));
18371 	log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18372 	log.u_bbr.flex1 = p_rate;
18373 	log.u_bbr.flex2 = p_queue;
18374 	log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18375 	log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18376 	log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18377 	log.u_bbr.flex7 = 99;
18378 	log.u_bbr.flex8 = 0;
18379 	log.u_bbr.pkts_out = err;
18380 	log.u_bbr.delRate = rack->r_ctl.crte->rate;
18381 	log.u_bbr.timeStamp = cts;
18382 	log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18383 	tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18384 		       len, &log, false, NULL, __func__, __LINE__, tv);
18385 
18386 }
18387 
18388 static uint32_t
rack_check_queue_level(struct tcp_rack * rack,struct tcpcb * tp,struct timeval * tv,uint32_t cts,int len,uint32_t segsiz)18389 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp,
18390 		       struct timeval *tv, uint32_t cts, int len, uint32_t segsiz)
18391 {
18392 	uint64_t lentime = 0;
18393 #ifdef RATELIMIT
18394 	uint32_t p_rate = 0, p_queue = 0, err;
18395 	union tcp_log_stackspecific log;
18396 	uint64_t bw;
18397 
18398 	err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18399 	/* Failed or queue is zero */
18400 	if (err || (p_queue == 0)) {
18401 		lentime = 0;
18402 		goto out;
18403 	}
18404 	err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
18405 	if (err) {
18406 		lentime = 0;
18407 		goto out;
18408 	}
18409 	/*
18410 	 * If we reach here we have some bytes in
18411 	 * the queue. The number returned is a value
18412 	 * between 0 and 0xffff where ffff is full
18413 	 * and 0 is empty. So how best to make this into
18414 	 * something usable?
18415 	 *
18416 	 * The "safer" way is lets take the b/w gotten
18417 	 * from the query (which should be our b/w rate)
18418 	 * and pretend that a full send (our rc_pace_max_segs)
18419 	 * is outstanding. We factor it so its as if a full
18420 	 * number of our MSS segment is terms of full
18421 	 * ethernet segments are outstanding.
18422 	 */
18423 	bw = p_rate / 8;
18424 	if (bw) {
18425 		lentime = (rack->r_ctl.rc_pace_max_segs / segsiz);
18426 		lentime *= ETHERNET_SEGMENT_SIZE;
18427 		lentime *= (uint64_t)HPTS_USEC_IN_SEC;
18428 		lentime /= bw;
18429 	} else {
18430 		/* TSNH -- KASSERT? */
18431 		lentime = 0;
18432 	}
18433 out:
18434 	if (tcp_bblogging_on(tp)) {
18435 		memset(&log, 0, sizeof(log));
18436 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18437 		log.u_bbr.flex1 = p_rate;
18438 		log.u_bbr.flex2 = p_queue;
18439 		log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18440 		log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18441 		log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18442 		log.u_bbr.flex7 = 99;
18443 		log.u_bbr.flex8 = 0;
18444 		log.u_bbr.pkts_out = err;
18445 		log.u_bbr.delRate = rack->r_ctl.crte->rate;
18446 		log.u_bbr.cur_del_rate = lentime;
18447 		log.u_bbr.timeStamp = cts;
18448 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18449 		tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18450 			       len, &log, false, NULL, __func__, __LINE__,tv);
18451 	}
18452 #endif
18453 	return ((uint32_t)lentime);
18454 }
18455 
18456 static int
rack_fast_rsm_output(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint64_t ts_val,uint32_t cts,uint32_t ms_cts,struct timeval * tv,int len,uint8_t doing_tlp)18457 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
18458 		     uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
18459 {
18460 	/*
18461 	 * Enter the fast retransmit path. We are given that a sched_pin is
18462 	 * in place (if accounting is compliled in) and the cycle count taken
18463 	 * at the entry is in the ts_val. The concept her is that the rsm
18464 	 * now holds the mbuf offsets and such so we can directly transmit
18465 	 * without a lot of overhead, the len field is already set for
18466 	 * us to prohibit us from sending too much (usually its 1MSS).
18467 	 */
18468 	struct ip *ip = NULL;
18469 	struct udphdr *udp = NULL;
18470 	struct tcphdr *th = NULL;
18471 	struct mbuf *m = NULL;
18472 	struct inpcb *inp;
18473 	uint8_t *cpto;
18474 	struct tcp_log_buffer *lgb;
18475 #ifdef TCP_ACCOUNTING
18476 	uint64_t crtsc;
18477 	int cnt_thru = 1;
18478 #endif
18479 	struct tcpopt to;
18480 	u_char opt[TCP_MAXOLEN];
18481 	uint32_t hdrlen, optlen;
18482 	int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0;
18483 	uint16_t flags;
18484 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
18485 	uint32_t if_hw_tsomaxsegsize;
18486 	int32_t ip_sendflag = IP_NO_SND_TAG_RL;
18487 
18488 #ifdef INET6
18489 	struct ip6_hdr *ip6 = NULL;
18490 
18491 	if (rack->r_is_v6) {
18492 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18493 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18494 	} else
18495 #endif				/* INET6 */
18496 	{
18497 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18498 		hdrlen = sizeof(struct tcpiphdr);
18499 	}
18500 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
18501 		goto failed;
18502 	}
18503 	if (doing_tlp) {
18504 		/* Its a TLP add the flag, it may already be there but be sure */
18505 		rsm->r_flags |= RACK_TLP;
18506 	} else {
18507 		/* If it was a TLP it is not not on this retransmit */
18508 		rsm->r_flags &= ~RACK_TLP;
18509 	}
18510 	startseq = rsm->r_start;
18511 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
18512 	inp = rack->rc_inp;
18513 	to.to_flags = 0;
18514 	flags = tcp_outflags[tp->t_state];
18515 	if (flags & (TH_SYN|TH_RST)) {
18516 		goto failed;
18517 	}
18518 	if (rsm->r_flags & RACK_HAS_FIN) {
18519 		/* We can't send a FIN here */
18520 		goto failed;
18521 	}
18522 	if (flags & TH_FIN) {
18523 		/* We never send a FIN */
18524 		flags &= ~TH_FIN;
18525 	}
18526 	if (tp->t_flags & TF_RCVD_TSTMP) {
18527 		to.to_tsval = ms_cts + tp->ts_offset;
18528 		to.to_tsecr = tp->ts_recent;
18529 		to.to_flags = TOF_TS;
18530 	}
18531 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18532 	/* TCP-MD5 (RFC2385). */
18533 	if (tp->t_flags & TF_SIGNATURE)
18534 		to.to_flags |= TOF_SIGNATURE;
18535 #endif
18536 	optlen = tcp_addoptions(&to, opt);
18537 	hdrlen += optlen;
18538 	udp = rack->r_ctl.fsb.udp;
18539 	if (udp)
18540 		hdrlen += sizeof(struct udphdr);
18541 	if (rack->r_ctl.rc_pace_max_segs)
18542 		max_val = rack->r_ctl.rc_pace_max_segs;
18543 	else if (rack->rc_user_set_max_segs)
18544 		max_val = rack->rc_user_set_max_segs * segsiz;
18545 	else
18546 		max_val = len;
18547 	if ((tp->t_flags & TF_TSO) &&
18548 	    V_tcp_do_tso &&
18549 	    (len > segsiz) &&
18550 	    (tp->t_port == 0))
18551 		tso = 1;
18552 #ifdef INET6
18553 	if (MHLEN < hdrlen + max_linkhdr)
18554 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18555 	else
18556 #endif
18557 		m = m_gethdr(M_NOWAIT, MT_DATA);
18558 	if (m == NULL)
18559 		goto failed;
18560 	m->m_data += max_linkhdr;
18561 	m->m_len = hdrlen;
18562 	th = rack->r_ctl.fsb.th;
18563 	/* Establish the len to send */
18564 	if (len > max_val)
18565 		len = max_val;
18566 	if ((tso) && (len + optlen > segsiz)) {
18567 		uint32_t if_hw_tsomax;
18568 		int32_t max_len;
18569 
18570 		/* extract TSO information */
18571 		if_hw_tsomax = tp->t_tsomax;
18572 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18573 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18574 		/*
18575 		 * Check if we should limit by maximum payload
18576 		 * length:
18577 		 */
18578 		if (if_hw_tsomax != 0) {
18579 			/* compute maximum TSO length */
18580 			max_len = (if_hw_tsomax - hdrlen -
18581 				   max_linkhdr);
18582 			if (max_len <= 0) {
18583 				goto failed;
18584 			} else if (len > max_len) {
18585 				len = max_len;
18586 			}
18587 		}
18588 		if (len <= segsiz) {
18589 			/*
18590 			 * In case there are too many small fragments don't
18591 			 * use TSO:
18592 			 */
18593 			tso = 0;
18594 		}
18595 	} else {
18596 		tso = 0;
18597 	}
18598 	if ((tso == 0) && (len > segsiz))
18599 		len = segsiz;
18600 	(void)tcp_get_usecs(tv);
18601 	if ((len == 0) ||
18602 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
18603 		goto failed;
18604 	}
18605 	th->th_seq = htonl(rsm->r_start);
18606 	th->th_ack = htonl(tp->rcv_nxt);
18607 	/*
18608 	 * The PUSH bit should only be applied
18609 	 * if the full retransmission is made. If
18610 	 * we are sending less than this is the
18611 	 * left hand edge and should not have
18612 	 * the PUSH bit.
18613 	 */
18614 	if ((rsm->r_flags & RACK_HAD_PUSH) &&
18615 	    (len == (rsm->r_end - rsm->r_start)))
18616 		flags |= TH_PUSH;
18617 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
18618 	if (th->th_win == 0) {
18619 		tp->t_sndzerowin++;
18620 		tp->t_flags |= TF_RXWIN0SENT;
18621 	} else
18622 		tp->t_flags &= ~TF_RXWIN0SENT;
18623 	if (rsm->r_flags & RACK_TLP) {
18624 		/*
18625 		 * TLP should not count in retran count, but
18626 		 * in its own bin
18627 		 */
18628 		counter_u64_add(rack_tlp_retran, 1);
18629 		counter_u64_add(rack_tlp_retran_bytes, len);
18630 	} else {
18631 		tp->t_sndrexmitpack++;
18632 		KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18633 		KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18634 	}
18635 #ifdef STATS
18636 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18637 				 len);
18638 #endif
18639 	if (rsm->m == NULL)
18640 		goto failed;
18641 	if (rsm->m &&
18642 	    ((rsm->orig_m_len != rsm->m->m_len) ||
18643 	     (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
18644 		/* Fix up the orig_m_len and possibly the mbuf offset */
18645 		rack_adjust_orig_mlen(rsm);
18646 	}
18647 	m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
18648 	if (len <= segsiz) {
18649 		/*
18650 		 * Must have ran out of mbufs for the copy
18651 		 * shorten it to no longer need tso. Lets
18652 		 * not put on sendalot since we are low on
18653 		 * mbufs.
18654 		 */
18655 		tso = 0;
18656 	}
18657 	if ((m->m_next == NULL) || (len <= 0)){
18658 		goto failed;
18659 	}
18660 	if (udp) {
18661 		if (rack->r_is_v6)
18662 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
18663 		else
18664 			ulen = hdrlen + len - sizeof(struct ip);
18665 		udp->uh_ulen = htons(ulen);
18666 	}
18667 	m->m_pkthdr.rcvif = (struct ifnet *)0;
18668 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
18669 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
18670 		int ect = tcp_ecn_output_established(tp, &flags, len, true);
18671 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18672 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
18673 		    tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18674 #ifdef INET6
18675 		if (rack->r_is_v6) {
18676 		    ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18677 		    ip6->ip6_flow |= htonl(ect << 20);
18678 		}
18679 		else
18680 #endif
18681 		{
18682 		    ip->ip_tos &= ~IPTOS_ECN_MASK;
18683 		    ip->ip_tos |= ect;
18684 		}
18685 	}
18686 	if (rack->r_ctl.crte != NULL) {
18687 		/* See if we can send via the hw queue */
18688 		slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz);
18689 		/* If there is nothing in queue (no pacing time) we can send via the hw queue */
18690 		if (slot == 0)
18691 			ip_sendflag = 0;
18692 	}
18693 	tcp_set_flags(th, flags);
18694 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
18695 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18696 	if (to.to_flags & TOF_SIGNATURE) {
18697 		/*
18698 		 * Calculate MD5 signature and put it into the place
18699 		 * determined before.
18700 		 * NOTE: since TCP options buffer doesn't point into
18701 		 * mbuf's data, calculate offset and use it.
18702 		 */
18703 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18704 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18705 			/*
18706 			 * Do not send segment if the calculation of MD5
18707 			 * digest has failed.
18708 			 */
18709 			goto failed;
18710 		}
18711 	}
18712 #endif
18713 #ifdef INET6
18714 	if (rack->r_is_v6) {
18715 		if (tp->t_port) {
18716 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18717 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18718 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18719 			th->th_sum = htons(0);
18720 			UDPSTAT_INC(udps_opackets);
18721 		} else {
18722 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18723 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18724 			th->th_sum = in6_cksum_pseudo(ip6,
18725 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18726 						      0);
18727 		}
18728 	}
18729 #endif
18730 #if defined(INET6) && defined(INET)
18731 	else
18732 #endif
18733 #ifdef INET
18734 	{
18735 		if (tp->t_port) {
18736 			m->m_pkthdr.csum_flags = CSUM_UDP;
18737 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18738 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18739 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18740 			th->th_sum = htons(0);
18741 			UDPSTAT_INC(udps_opackets);
18742 		} else {
18743 			m->m_pkthdr.csum_flags = CSUM_TCP;
18744 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18745 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
18746 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18747 									IPPROTO_TCP + len + optlen));
18748 		}
18749 		/* IP version must be set here for ipv4/ipv6 checking later */
18750 		KASSERT(ip->ip_v == IPVERSION,
18751 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
18752 	}
18753 #endif
18754 	if (tso) {
18755 		/*
18756 		 * Here we use segsiz since we have no added options besides
18757 		 * any standard timestamp options (no DSACKs or SACKS are sent
18758 		 * via either fast-path).
18759 		 */
18760 		KASSERT(len > segsiz,
18761 			("%s: len <= tso_segsz tp:%p", __func__, tp));
18762 		m->m_pkthdr.csum_flags |= CSUM_TSO;
18763 		m->m_pkthdr.tso_segsz = segsiz;
18764 	}
18765 #ifdef INET6
18766 	if (rack->r_is_v6) {
18767 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
18768 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18769 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18770 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18771 		else
18772 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18773 	}
18774 #endif
18775 #if defined(INET) && defined(INET6)
18776 	else
18777 #endif
18778 #ifdef INET
18779 	{
18780 		ip->ip_len = htons(m->m_pkthdr.len);
18781 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
18782 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18783 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18784 			if (tp->t_port == 0 || len < V_tcp_minmss) {
18785 				ip->ip_off |= htons(IP_DF);
18786 			}
18787 		} else {
18788 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18789 		}
18790 	}
18791 #endif
18792 	if (doing_tlp == 0) {
18793 		/* Set we retransmitted */
18794 		rack->rc_gp_saw_rec = 1;
18795 	} else {
18796 		/* Its a TLP set ca or ss */
18797 		if (tp->snd_cwnd > tp->snd_ssthresh) {
18798 			/* Set we sent in CA */
18799 			rack->rc_gp_saw_ca = 1;
18800 		} else {
18801 			/* Set we sent in SS */
18802 			rack->rc_gp_saw_ss = 1;
18803 		}
18804 	}
18805 	/* Time to copy in our header */
18806 	cpto = mtod(m, uint8_t *);
18807 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18808 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18809 	if (optlen) {
18810 		bcopy(opt, th + 1, optlen);
18811 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18812 	} else {
18813 		th->th_off = sizeof(struct tcphdr) >> 2;
18814 	}
18815 	if (tcp_bblogging_on(rack->rc_tp)) {
18816 		union tcp_log_stackspecific log;
18817 
18818 		if (rsm->r_flags & RACK_RWND_COLLAPSED) {
18819 			rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
18820 			counter_u64_add(rack_collapsed_win_rxt, 1);
18821 			counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
18822 		}
18823 		memset(&log, 0, sizeof(log));
18824 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18825 		if (rack->rack_no_prr)
18826 			log.u_bbr.flex1 = 0;
18827 		else
18828 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18829 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18830 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18831 		log.u_bbr.flex4 = max_val;
18832 		/* Save off the early/late values */
18833 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18834 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18835 		log.u_bbr.bw_inuse = rack_get_bw(rack);
18836 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
18837 		if (doing_tlp == 0)
18838 			log.u_bbr.flex8 = 1;
18839 		else
18840 			log.u_bbr.flex8 = 2;
18841 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18842 		log.u_bbr.flex7 = 55;
18843 		log.u_bbr.pkts_out = tp->t_maxseg;
18844 		log.u_bbr.timeStamp = cts;
18845 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18846 		if (rsm->r_rtr_cnt > 0) {
18847 			/*
18848 			 * When we have a retransmit we want to log the
18849 			 * burst at send and flight at send from before.
18850 			 */
18851 			log.u_bbr.flex5 = rsm->r_fas;
18852 			log.u_bbr.bbr_substate = rsm->r_bas;
18853 		} else {
18854 			/*
18855 			 * This is currently unlikely until we do the
18856 			 * packet pair probes but I will add it for completeness.
18857 			 */
18858 			log.u_bbr.flex5 = log.u_bbr.inflight;
18859 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
18860 		}
18861 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
18862 		log.u_bbr.delivered = 0;
18863 		log.u_bbr.rttProp = (uintptr_t)rsm;
18864 		log.u_bbr.delRate = rsm->r_flags;
18865 		log.u_bbr.delRate <<= 31;
18866 		log.u_bbr.delRate |= rack->r_must_retran;
18867 		log.u_bbr.delRate <<= 1;
18868 		log.u_bbr.delRate |= 1;
18869 		log.u_bbr.pkt_epoch = __LINE__;
18870 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
18871 				     len, &log, false, NULL, __func__, __LINE__, tv);
18872 	} else
18873 		lgb = NULL;
18874 	if ((rack->r_ctl.crte != NULL) &&
18875 	    tcp_bblogging_on(tp)) {
18876 		rack_log_queue_level(tp, rack, len, tv, cts);
18877 	}
18878 #ifdef INET6
18879 	if (rack->r_is_v6) {
18880 		error = ip6_output(m, inp->in6p_outputopts,
18881 				   &inp->inp_route6,
18882 				   ip_sendflag, NULL, NULL, inp);
18883 	}
18884 	else
18885 #endif
18886 #ifdef INET
18887 	{
18888 		error = ip_output(m, NULL,
18889 				  &inp->inp_route,
18890 				  ip_sendflag, 0, inp);
18891 	}
18892 #endif
18893 	m = NULL;
18894 	if (lgb) {
18895 		lgb->tlb_errno = error;
18896 		lgb = NULL;
18897 	}
18898 	/* Move snd_nxt to snd_max so we don't have false retransmissions */
18899 	tp->snd_nxt = tp->snd_max;
18900 	if (error) {
18901 		goto failed;
18902 	} else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) {
18903 		rack->rc_hw_nobuf = 0;
18904 		rack->r_ctl.rc_agg_delayed = 0;
18905 		rack->r_early = 0;
18906 		rack->r_late = 0;
18907 		rack->r_ctl.rc_agg_early = 0;
18908 	}
18909 	rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
18910 			rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz);
18911 	if (doing_tlp) {
18912 		rack->rc_tlp_in_progress = 1;
18913 		rack->r_ctl.rc_tlp_cnt_out++;
18914 	}
18915 	if (error == 0) {
18916 		counter_u64_add(rack_total_bytes, len);
18917 		tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
18918 		if (doing_tlp) {
18919 			rack->rc_last_sent_tlp_past_cumack = 0;
18920 			rack->rc_last_sent_tlp_seq_valid = 1;
18921 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18922 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18923 		}
18924 		if (rack->r_ctl.rc_prr_sndcnt >= len)
18925 			rack->r_ctl.rc_prr_sndcnt -= len;
18926 		else
18927 			rack->r_ctl.rc_prr_sndcnt = 0;
18928 	}
18929 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
18930 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
18931 	if (IN_FASTRECOVERY(tp->t_flags) && rsm)
18932 		rack->r_ctl.retran_during_recovery += len;
18933 	{
18934 		int idx;
18935 
18936 		idx = (len / segsiz) + 3;
18937 		if (idx >= TCP_MSS_ACCT_ATIMER)
18938 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18939 		else
18940 			counter_u64_add(rack_out_size[idx], 1);
18941 	}
18942 	if (tp->t_rtttime == 0) {
18943 		tp->t_rtttime = ticks;
18944 		tp->t_rtseq = startseq;
18945 		KMOD_TCPSTAT_INC(tcps_segstimed);
18946 	}
18947 	counter_u64_add(rack_fto_rsm_send, 1);
18948 	if (error && (error == ENOBUFS)) {
18949 		if (rack->r_ctl.crte != NULL) {
18950 			tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
18951 			if (tcp_bblogging_on(rack->rc_tp))
18952 				rack_log_queue_level(tp, rack, len, tv, cts);
18953 		} else
18954 			tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
18955 		slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
18956 		if (rack->rc_enobuf < 0x7f)
18957 			rack->rc_enobuf++;
18958 		if (slot < (10 * HPTS_USEC_IN_MSEC))
18959 			slot = 10 * HPTS_USEC_IN_MSEC;
18960 		if (rack->r_ctl.crte != NULL) {
18961 			counter_u64_add(rack_saw_enobuf_hw, 1);
18962 			tcp_rl_log_enobuf(rack->r_ctl.crte);
18963 		}
18964 		counter_u64_add(rack_saw_enobuf, 1);
18965 	} else {
18966 		slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz, __LINE__);
18967 	}
18968 	rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
18969 #ifdef TCP_ACCOUNTING
18970 	crtsc = get_cyclecount();
18971 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18972 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
18973 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
18974 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
18975 	}
18976 	sched_unpin();
18977 #endif
18978 	return (0);
18979 failed:
18980 	if (m)
18981 		m_free(m);
18982 	return (-1);
18983 }
18984 
18985 static void
rack_sndbuf_autoscale(struct tcp_rack * rack)18986 rack_sndbuf_autoscale(struct tcp_rack *rack)
18987 {
18988 	/*
18989 	 * Automatic sizing of send socket buffer.  Often the send buffer
18990 	 * size is not optimally adjusted to the actual network conditions
18991 	 * at hand (delay bandwidth product).  Setting the buffer size too
18992 	 * small limits throughput on links with high bandwidth and high
18993 	 * delay (eg. trans-continental/oceanic links).  Setting the
18994 	 * buffer size too big consumes too much real kernel memory,
18995 	 * especially with many connections on busy servers.
18996 	 *
18997 	 * The criteria to step up the send buffer one notch are:
18998 	 *  1. receive window of remote host is larger than send buffer
18999 	 *     (with a fudge factor of 5/4th);
19000 	 *  2. send buffer is filled to 7/8th with data (so we actually
19001 	 *     have data to make use of it);
19002 	 *  3. send buffer fill has not hit maximal automatic size;
19003 	 *  4. our send window (slow start and cogestion controlled) is
19004 	 *     larger than sent but unacknowledged data in send buffer.
19005 	 *
19006 	 * Note that the rack version moves things much faster since
19007 	 * we want to avoid hitting cache lines in the rack_fast_output()
19008 	 * path so this is called much less often and thus moves
19009 	 * the SB forward by a percentage.
19010 	 */
19011 	struct socket *so;
19012 	struct tcpcb *tp;
19013 	uint32_t sendwin, scaleup;
19014 
19015 	tp = rack->rc_tp;
19016 	so = rack->rc_inp->inp_socket;
19017 	sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
19018 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
19019 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
19020 		    sbused(&so->so_snd) >=
19021 		    (so->so_snd.sb_hiwat / 8 * 7) &&
19022 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
19023 		    sendwin >= (sbused(&so->so_snd) -
19024 		    (tp->snd_max - tp->snd_una))) {
19025 			if (rack_autosndbuf_inc)
19026 				scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
19027 			else
19028 				scaleup = V_tcp_autosndbuf_inc;
19029 			if (scaleup < V_tcp_autosndbuf_inc)
19030 				scaleup = V_tcp_autosndbuf_inc;
19031 			scaleup += so->so_snd.sb_hiwat;
19032 			if (scaleup > V_tcp_autosndbuf_max)
19033 				scaleup = V_tcp_autosndbuf_max;
19034 			if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
19035 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
19036 		}
19037 	}
19038 }
19039 
19040 static int
rack_fast_output(struct tcpcb * tp,struct tcp_rack * rack,uint64_t ts_val,uint32_t cts,uint32_t ms_cts,struct timeval * tv,long tot_len,int * send_err)19041 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
19042 		 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
19043 {
19044 	/*
19045 	 * Enter to do fast output. We are given that the sched_pin is
19046 	 * in place (if accounting is compiled in) and the cycle count taken
19047 	 * at entry is in place in ts_val. The idea here is that
19048 	 * we know how many more bytes needs to be sent (presumably either
19049 	 * during pacing or to fill the cwnd and that was greater than
19050 	 * the max-burst). We have how much to send and all the info we
19051 	 * need to just send.
19052 	 */
19053 #ifdef INET
19054 	struct ip *ip = NULL;
19055 #endif
19056 	struct udphdr *udp = NULL;
19057 	struct tcphdr *th = NULL;
19058 	struct mbuf *m, *s_mb;
19059 	struct inpcb *inp;
19060 	uint8_t *cpto;
19061 	struct tcp_log_buffer *lgb;
19062 #ifdef TCP_ACCOUNTING
19063 	uint64_t crtsc;
19064 #endif
19065 	struct tcpopt to;
19066 	u_char opt[TCP_MAXOLEN];
19067 	uint32_t hdrlen, optlen;
19068 #ifdef TCP_ACCOUNTING
19069 	int cnt_thru = 1;
19070 #endif
19071 	int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
19072 	uint16_t flags;
19073 	uint32_t s_soff;
19074 	uint32_t if_hw_tsomaxsegcount = 0, startseq;
19075 	uint32_t if_hw_tsomaxsegsize;
19076 	uint32_t add_flag = RACK_SENT_FP;
19077 #ifdef INET6
19078 	struct ip6_hdr *ip6 = NULL;
19079 
19080 	if (rack->r_is_v6) {
19081 		ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
19082 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
19083 	} else
19084 #endif				/* INET6 */
19085 	{
19086 #ifdef INET
19087 		ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
19088 		hdrlen = sizeof(struct tcpiphdr);
19089 #endif
19090 	}
19091 	if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
19092 		m = NULL;
19093 		goto failed;
19094 	}
19095 	rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
19096 	startseq = tp->snd_max;
19097 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
19098 	inp = rack->rc_inp;
19099 	len = rack->r_ctl.fsb.left_to_send;
19100 	to.to_flags = 0;
19101 	flags = rack->r_ctl.fsb.tcp_flags;
19102 	if (tp->t_flags & TF_RCVD_TSTMP) {
19103 		to.to_tsval = ms_cts + tp->ts_offset;
19104 		to.to_tsecr = tp->ts_recent;
19105 		to.to_flags = TOF_TS;
19106 	}
19107 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19108 	/* TCP-MD5 (RFC2385). */
19109 	if (tp->t_flags & TF_SIGNATURE)
19110 		to.to_flags |= TOF_SIGNATURE;
19111 #endif
19112 	optlen = tcp_addoptions(&to, opt);
19113 	hdrlen += optlen;
19114 	udp = rack->r_ctl.fsb.udp;
19115 	if (udp)
19116 		hdrlen += sizeof(struct udphdr);
19117 	if (rack->r_ctl.rc_pace_max_segs)
19118 		max_val = rack->r_ctl.rc_pace_max_segs;
19119 	else if (rack->rc_user_set_max_segs)
19120 		max_val = rack->rc_user_set_max_segs * segsiz;
19121 	else
19122 		max_val = len;
19123 	if ((tp->t_flags & TF_TSO) &&
19124 	    V_tcp_do_tso &&
19125 	    (len > segsiz) &&
19126 	    (tp->t_port == 0))
19127 		tso = 1;
19128 again:
19129 #ifdef INET6
19130 	if (MHLEN < hdrlen + max_linkhdr)
19131 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
19132 	else
19133 #endif
19134 		m = m_gethdr(M_NOWAIT, MT_DATA);
19135 	if (m == NULL)
19136 		goto failed;
19137 	m->m_data += max_linkhdr;
19138 	m->m_len = hdrlen;
19139 	th = rack->r_ctl.fsb.th;
19140 	/* Establish the len to send */
19141 	if (len > max_val)
19142 		len = max_val;
19143 	if ((tso) && (len + optlen > segsiz)) {
19144 		uint32_t if_hw_tsomax;
19145 		int32_t max_len;
19146 
19147 		/* extract TSO information */
19148 		if_hw_tsomax = tp->t_tsomax;
19149 		if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
19150 		if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
19151 		/*
19152 		 * Check if we should limit by maximum payload
19153 		 * length:
19154 		 */
19155 		if (if_hw_tsomax != 0) {
19156 			/* compute maximum TSO length */
19157 			max_len = (if_hw_tsomax - hdrlen -
19158 				   max_linkhdr);
19159 			if (max_len <= 0) {
19160 				goto failed;
19161 			} else if (len > max_len) {
19162 				len = max_len;
19163 			}
19164 		}
19165 		if (len <= segsiz) {
19166 			/*
19167 			 * In case there are too many small fragments don't
19168 			 * use TSO:
19169 			 */
19170 			tso = 0;
19171 		}
19172 	} else {
19173 		tso = 0;
19174 	}
19175 	if ((tso == 0) && (len > segsiz))
19176 		len = segsiz;
19177 	(void)tcp_get_usecs(tv);
19178 	if ((len == 0) ||
19179 	    (len <= MHLEN - hdrlen - max_linkhdr)) {
19180 		goto failed;
19181 	}
19182 	sb_offset = tp->snd_max - tp->snd_una;
19183 	th->th_seq = htonl(tp->snd_max);
19184 	th->th_ack = htonl(tp->rcv_nxt);
19185 	th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
19186 	if (th->th_win == 0) {
19187 		tp->t_sndzerowin++;
19188 		tp->t_flags |= TF_RXWIN0SENT;
19189 	} else
19190 		tp->t_flags &= ~TF_RXWIN0SENT;
19191 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
19192 	KMOD_TCPSTAT_INC(tcps_sndpack);
19193 	KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
19194 #ifdef STATS
19195 	stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
19196 				 len);
19197 #endif
19198 	if (rack->r_ctl.fsb.m == NULL)
19199 		goto failed;
19200 
19201 	/* s_mb and s_soff are saved for rack_log_output */
19202 	m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
19203 				    &s_mb, &s_soff);
19204 	if (len <= segsiz) {
19205 		/*
19206 		 * Must have ran out of mbufs for the copy
19207 		 * shorten it to no longer need tso. Lets
19208 		 * not put on sendalot since we are low on
19209 		 * mbufs.
19210 		 */
19211 		tso = 0;
19212 	}
19213 	if (rack->r_ctl.fsb.rfo_apply_push &&
19214 	    (len == rack->r_ctl.fsb.left_to_send)) {
19215 		tcp_set_flags(th, flags | TH_PUSH);
19216 		add_flag |= RACK_HAD_PUSH;
19217 	}
19218 	if ((m->m_next == NULL) || (len <= 0)){
19219 		goto failed;
19220 	}
19221 	if (udp) {
19222 		if (rack->r_is_v6)
19223 			ulen = hdrlen + len - sizeof(struct ip6_hdr);
19224 		else
19225 			ulen = hdrlen + len - sizeof(struct ip);
19226 		udp->uh_ulen = htons(ulen);
19227 	}
19228 	m->m_pkthdr.rcvif = (struct ifnet *)0;
19229 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
19230 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
19231 		int ect = tcp_ecn_output_established(tp, &flags, len, false);
19232 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
19233 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
19234 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
19235 #ifdef INET6
19236 		if (rack->r_is_v6) {
19237 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
19238 			ip6->ip6_flow |= htonl(ect << 20);
19239 		}
19240 		else
19241 #endif
19242 		{
19243 #ifdef INET
19244 			ip->ip_tos &= ~IPTOS_ECN_MASK;
19245 			ip->ip_tos |= ect;
19246 #endif
19247 		}
19248 	}
19249 	tcp_set_flags(th, flags);
19250 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
19251 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
19252 	if (to.to_flags & TOF_SIGNATURE) {
19253 		/*
19254 		 * Calculate MD5 signature and put it into the place
19255 		 * determined before.
19256 		 * NOTE: since TCP options buffer doesn't point into
19257 		 * mbuf's data, calculate offset and use it.
19258 		 */
19259 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
19260 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
19261 			/*
19262 			 * Do not send segment if the calculation of MD5
19263 			 * digest has failed.
19264 			 */
19265 			goto failed;
19266 		}
19267 	}
19268 #endif
19269 #ifdef INET6
19270 	if (rack->r_is_v6) {
19271 		if (tp->t_port) {
19272 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
19273 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19274 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
19275 			th->th_sum = htons(0);
19276 			UDPSTAT_INC(udps_opackets);
19277 		} else {
19278 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
19279 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19280 			th->th_sum = in6_cksum_pseudo(ip6,
19281 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
19282 						      0);
19283 		}
19284 	}
19285 #endif
19286 #if defined(INET6) && defined(INET)
19287 	else
19288 #endif
19289 #ifdef INET
19290 	{
19291 		if (tp->t_port) {
19292 			m->m_pkthdr.csum_flags = CSUM_UDP;
19293 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19294 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
19295 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
19296 			th->th_sum = htons(0);
19297 			UDPSTAT_INC(udps_opackets);
19298 		} else {
19299 			m->m_pkthdr.csum_flags = CSUM_TCP;
19300 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19301 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
19302 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
19303 									IPPROTO_TCP + len + optlen));
19304 		}
19305 		/* IP version must be set here for ipv4/ipv6 checking later */
19306 		KASSERT(ip->ip_v == IPVERSION,
19307 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
19308 	}
19309 #endif
19310 	if (tso) {
19311 		/*
19312 		 * Here we use segsiz since we have no added options besides
19313 		 * any standard timestamp options (no DSACKs or SACKS are sent
19314 		 * via either fast-path).
19315 		 */
19316 		KASSERT(len > segsiz,
19317 			("%s: len <= tso_segsz tp:%p", __func__, tp));
19318 		m->m_pkthdr.csum_flags |= CSUM_TSO;
19319 		m->m_pkthdr.tso_segsz = segsiz;
19320 	}
19321 #ifdef INET6
19322 	if (rack->r_is_v6) {
19323 		ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
19324 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
19325 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
19326 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19327 		else
19328 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19329 	}
19330 #endif
19331 #if defined(INET) && defined(INET6)
19332 	else
19333 #endif
19334 #ifdef INET
19335 	{
19336 		ip->ip_len = htons(m->m_pkthdr.len);
19337 		ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
19338 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
19339 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19340 			if (tp->t_port == 0 || len < V_tcp_minmss) {
19341 				ip->ip_off |= htons(IP_DF);
19342 			}
19343 		} else {
19344 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19345 		}
19346 	}
19347 #endif
19348 	if (tp->snd_cwnd > tp->snd_ssthresh) {
19349 		/* Set we sent in CA */
19350 		rack->rc_gp_saw_ca = 1;
19351 	} else {
19352 		/* Set we sent in SS */
19353 		rack->rc_gp_saw_ss = 1;
19354 	}
19355 	/* Time to copy in our header */
19356 	cpto = mtod(m, uint8_t *);
19357 	memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
19358 	th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
19359 	if (optlen) {
19360 		bcopy(opt, th + 1, optlen);
19361 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
19362 	} else {
19363 		th->th_off = sizeof(struct tcphdr) >> 2;
19364 	}
19365 	if ((rack->r_ctl.crte != NULL) &&
19366 	    tcp_bblogging_on(tp)) {
19367 		rack_log_queue_level(tp, rack, len, tv, cts);
19368 	}
19369 	if (tcp_bblogging_on(rack->rc_tp)) {
19370 		union tcp_log_stackspecific log;
19371 
19372 		memset(&log, 0, sizeof(log));
19373 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19374 		if (rack->rack_no_prr)
19375 			log.u_bbr.flex1 = 0;
19376 		else
19377 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
19378 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
19379 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
19380 		log.u_bbr.flex4 = max_val;
19381 		/* Save off the early/late values */
19382 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19383 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
19384 		log.u_bbr.bw_inuse = rack_get_bw(rack);
19385 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
19386 		log.u_bbr.flex8 = 0;
19387 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19388 		log.u_bbr.flex7 = 44;
19389 		log.u_bbr.pkts_out = tp->t_maxseg;
19390 		log.u_bbr.timeStamp = cts;
19391 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19392 		log.u_bbr.flex5 = log.u_bbr.inflight;
19393 		log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
19394 		log.u_bbr.delivered = 0;
19395 		log.u_bbr.rttProp = 0;
19396 		log.u_bbr.delRate = rack->r_must_retran;
19397 		log.u_bbr.delRate <<= 1;
19398 		log.u_bbr.pkt_epoch = __LINE__;
19399 		/* For fast output no retrans so just inflight and how many mss we send */
19400 		log.u_bbr.flex5 = log.u_bbr.inflight;
19401 		log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
19402 		lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
19403 				     len, &log, false, NULL, __func__, __LINE__, tv);
19404 	} else
19405 		lgb = NULL;
19406 #ifdef INET6
19407 	if (rack->r_is_v6) {
19408 		error = ip6_output(m, inp->in6p_outputopts,
19409 				   &inp->inp_route6,
19410 				   0, NULL, NULL, inp);
19411 	}
19412 #endif
19413 #if defined(INET) && defined(INET6)
19414 	else
19415 #endif
19416 #ifdef INET
19417 	{
19418 		error = ip_output(m, NULL,
19419 				  &inp->inp_route,
19420 				  0, 0, inp);
19421 	}
19422 #endif
19423 	if (lgb) {
19424 		lgb->tlb_errno = error;
19425 		lgb = NULL;
19426 	}
19427 	if (error) {
19428 		*send_err = error;
19429 		m = NULL;
19430 		goto failed;
19431 	} else if (rack->rc_hw_nobuf) {
19432 		rack->rc_hw_nobuf = 0;
19433 		rack->r_ctl.rc_agg_delayed = 0;
19434 		rack->r_early = 0;
19435 		rack->r_late = 0;
19436 		rack->r_ctl.rc_agg_early = 0;
19437 	}
19438 	if ((error == 0) && (rack->lt_bw_up == 0)) {
19439 		/* Unlikely */
19440 		rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv);
19441 		rack->r_ctl.lt_seq = tp->snd_una;
19442 		rack->lt_bw_up = 1;
19443 	} else if ((error == 0) &&
19444 		   (((tp->snd_max + len) - rack->r_ctl.lt_seq) > 0x7fffffff)) {
19445 		/*
19446 		 * Need to record what we have since we are
19447 		 * approaching seq wrap.
19448 		 */
19449 		struct timeval tv;
19450 		uint64_t tmark;
19451 
19452 		rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
19453 		rack->r_ctl.lt_seq = tp->snd_una;
19454 		tmark = tcp_get_u64_usecs(&tv);
19455 		if (tmark > rack->r_ctl.lt_timemark) {
19456 			rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
19457 			rack->r_ctl.lt_timemark = tmark;
19458 		}
19459 	}
19460 	rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
19461 			NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz);
19462 	if (tp->snd_una == tp->snd_max) {
19463 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
19464 		rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
19465 		tp->t_acktime = ticks;
19466 	}
19467 	counter_u64_add(rack_total_bytes, len);
19468 	tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
19469 
19470 	rack->forced_ack = 0;	/* If we send something zap the FA flag */
19471 	tot_len += len;
19472 	if ((tp->t_flags & TF_GPUTINPROG) == 0)
19473 		rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
19474 	tp->snd_max += len;
19475 	tp->snd_nxt = tp->snd_max;
19476 	if (rack->rc_new_rnd_needed) {
19477 		rack_new_round_starts(tp, rack, tp->snd_max);
19478 	}
19479 	{
19480 		int idx;
19481 
19482 		idx = (len / segsiz) + 3;
19483 		if (idx >= TCP_MSS_ACCT_ATIMER)
19484 			counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
19485 		else
19486 			counter_u64_add(rack_out_size[idx], 1);
19487 	}
19488 	if (len <= rack->r_ctl.fsb.left_to_send)
19489 		rack->r_ctl.fsb.left_to_send -= len;
19490 	else
19491 		rack->r_ctl.fsb.left_to_send = 0;
19492 	if (rack->r_ctl.fsb.left_to_send < segsiz) {
19493 		rack->r_fast_output = 0;
19494 		rack->r_ctl.fsb.left_to_send = 0;
19495 		/* At the end of fast_output scale up the sb */
19496 		SOCK_SENDBUF_LOCK(rack->rc_inp->inp_socket);
19497 		rack_sndbuf_autoscale(rack);
19498 		SOCK_SENDBUF_UNLOCK(rack->rc_inp->inp_socket);
19499 	}
19500 	if (tp->t_rtttime == 0) {
19501 		tp->t_rtttime = ticks;
19502 		tp->t_rtseq = startseq;
19503 		KMOD_TCPSTAT_INC(tcps_segstimed);
19504 	}
19505 	if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
19506 	    (max_val > len) &&
19507 	    (tso == 0)) {
19508 		max_val -= len;
19509 		len = segsiz;
19510 		th = rack->r_ctl.fsb.th;
19511 #ifdef TCP_ACCOUNTING
19512 		cnt_thru++;
19513 #endif
19514 		goto again;
19515 	}
19516 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19517 	counter_u64_add(rack_fto_send, 1);
19518 	slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz, __LINE__);
19519 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
19520 #ifdef TCP_ACCOUNTING
19521 	crtsc = get_cyclecount();
19522 	if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19523 		tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
19524 		tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
19525 		tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
19526 	}
19527 	sched_unpin();
19528 #endif
19529 	return (0);
19530 failed:
19531 	if (m)
19532 		m_free(m);
19533 	rack->r_fast_output = 0;
19534 	return (-1);
19535 }
19536 
19537 static inline void
rack_setup_fast_output(struct tcpcb * tp,struct tcp_rack * rack,struct sockbuf * sb,int len,int orig_len,int segsiz,uint32_t pace_max_seg,bool hw_tls,uint16_t flags)19538 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack,
19539 		       struct sockbuf *sb,
19540 		       int len, int orig_len, int segsiz, uint32_t pace_max_seg,
19541 		       bool hw_tls,
19542 		       uint16_t flags)
19543 {
19544 	rack->r_fast_output = 1;
19545 	rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19546 	rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19547 	rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
19548 	rack->r_ctl.fsb.tcp_flags = flags;
19549 	rack->r_ctl.fsb.left_to_send = orig_len - len;
19550 	if (rack->r_ctl.fsb.left_to_send < pace_max_seg) {
19551 		/* Less than a full sized pace, lets not  */
19552 		rack->r_fast_output = 0;
19553 		return;
19554 	} else {
19555 		/* Round down to the nearest pace_max_seg */
19556 		rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg);
19557 	}
19558 	if (hw_tls)
19559 		rack->r_ctl.fsb.hw_tls = 1;
19560 	else
19561 		rack->r_ctl.fsb.hw_tls = 0;
19562 	KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19563 		("rack:%p left_to_send:%u sbavail:%u out:%u",
19564 		 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19565 		 (tp->snd_max - tp->snd_una)));
19566 	if (rack->r_ctl.fsb.left_to_send < segsiz)
19567 		rack->r_fast_output = 0;
19568 	else {
19569 		if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19570 			rack->r_ctl.fsb.rfo_apply_push = 1;
19571 		else
19572 			rack->r_ctl.fsb.rfo_apply_push = 0;
19573 	}
19574 }
19575 
19576 static uint32_t
rack_get_hpts_pacing_min_for_bw(struct tcp_rack * rack,int32_t segsiz)19577 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz)
19578 {
19579 	uint64_t min_time;
19580 	uint32_t maxlen;
19581 
19582 	min_time = (uint64_t)get_hpts_min_sleep_time();
19583 	maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC);
19584 	maxlen = roundup(maxlen, segsiz);
19585 	return (maxlen);
19586 }
19587 
19588 static struct rack_sendmap *
rack_check_collapsed(struct tcp_rack * rack,uint32_t cts)19589 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
19590 {
19591 	struct rack_sendmap *rsm = NULL;
19592 	int thresh;
19593 
19594 restart:
19595 	rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
19596 	if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
19597 		/* Nothing, strange turn off validity  */
19598 		rack->r_collapse_point_valid = 0;
19599 		return (NULL);
19600 	}
19601 	/* Can we send it yet? */
19602 	if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
19603 		/*
19604 		 * Receiver window has not grown enough for
19605 		 * the segment to be put on the wire.
19606 		 */
19607 		return (NULL);
19608 	}
19609 	if (rsm->r_flags & RACK_ACKED) {
19610 		/*
19611 		 * It has been sacked, lets move to the
19612 		 * next one if possible.
19613 		 */
19614 		rack->r_ctl.last_collapse_point = rsm->r_end;
19615 		/* Are we done? */
19616 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
19617 			    rack->r_ctl.high_collapse_point)) {
19618 			rack->r_collapse_point_valid = 0;
19619 			return (NULL);
19620 		}
19621 		goto restart;
19622 	}
19623 	/* Now has it been long enough ? */
19624 	thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts, __LINE__, 1);
19625 	if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
19626 		rack_log_collapse(rack, rsm->r_start,
19627 				  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19628 				  thresh, __LINE__, 6, rsm->r_flags, rsm);
19629 		return (rsm);
19630 	}
19631 	/* Not enough time */
19632 	rack_log_collapse(rack, rsm->r_start,
19633 			  (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19634 			  thresh, __LINE__, 7, rsm->r_flags, rsm);
19635 	return (NULL);
19636 }
19637 
19638 static inline void
rack_validate_sizes(struct tcp_rack * rack,int32_t * len,int32_t segsiz,uint32_t pace_max_seg)19639 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg)
19640 {
19641 	if ((rack->full_size_rxt == 0) &&
19642 	    (rack->shape_rxt_to_pacing_min == 0) &&
19643 	    (*len >= segsiz)) {
19644 		*len = segsiz;
19645 	} else if (rack->shape_rxt_to_pacing_min &&
19646 		 rack->gp_ready) {
19647 		/* We use pacing min as shaping len req */
19648 		uint32_t maxlen;
19649 
19650 		maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
19651 		if (*len > maxlen)
19652 			*len = maxlen;
19653 	} else {
19654 		/*
19655 		 * The else is full_size_rxt is on so send it all
19656 		 * note we do need to check this for exceeding
19657 		 * our max segment size due to the fact that
19658 		 * we do sometimes merge chunks together i.e.
19659 		 * we cannot just assume that we will never have
19660 		 * a chunk greater than pace_max_seg
19661 		 */
19662 		if (*len > pace_max_seg)
19663 			*len = pace_max_seg;
19664 	}
19665 }
19666 
19667 static int
rack_output(struct tcpcb * tp)19668 rack_output(struct tcpcb *tp)
19669 {
19670 	struct socket *so;
19671 	uint32_t recwin;
19672 	uint32_t sb_offset, s_moff = 0;
19673 	int32_t len, error = 0;
19674 	uint16_t flags;
19675 	struct mbuf *m, *s_mb = NULL;
19676 	struct mbuf *mb;
19677 	uint32_t if_hw_tsomaxsegcount = 0;
19678 	uint32_t if_hw_tsomaxsegsize;
19679 	int32_t segsiz, minseg;
19680 	long tot_len_this_send = 0;
19681 #ifdef INET
19682 	struct ip *ip = NULL;
19683 #endif
19684 	struct udphdr *udp = NULL;
19685 	struct tcp_rack *rack;
19686 	struct tcphdr *th;
19687 	uint8_t pass = 0;
19688 	uint8_t mark = 0;
19689 	uint8_t check_done = 0;
19690 	uint8_t wanted_cookie = 0;
19691 	u_char opt[TCP_MAXOLEN];
19692 	unsigned ipoptlen, optlen, hdrlen, ulen=0;
19693 	uint32_t rack_seq;
19694 
19695 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
19696 	unsigned ipsec_optlen = 0;
19697 
19698 #endif
19699 	int32_t idle, sendalot;
19700 	uint32_t tot_idle;
19701 	int32_t sub_from_prr = 0;
19702 	volatile int32_t sack_rxmit;
19703 	struct rack_sendmap *rsm = NULL;
19704 	int32_t tso, mtu;
19705 	struct tcpopt to;
19706 	int32_t slot = 0;
19707 	int32_t sup_rack = 0;
19708 	uint32_t cts, ms_cts, delayed, early;
19709 	uint32_t add_flag = RACK_SENT_SP;
19710 	/* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
19711 	uint8_t doing_tlp = 0;
19712 	uint32_t cwnd_to_use, pace_max_seg;
19713 	int32_t do_a_prefetch = 0;
19714 	int32_t prefetch_rsm = 0;
19715 	int32_t orig_len = 0;
19716 	struct timeval tv;
19717 	int32_t prefetch_so_done = 0;
19718 	struct tcp_log_buffer *lgb;
19719 	struct inpcb *inp = tptoinpcb(tp);
19720 	struct sockbuf *sb;
19721 	uint64_t ts_val = 0;
19722 #ifdef TCP_ACCOUNTING
19723 	uint64_t crtsc;
19724 #endif
19725 #ifdef INET6
19726 	struct ip6_hdr *ip6 = NULL;
19727 	int32_t isipv6;
19728 #endif
19729 	bool hpts_calling, hw_tls = false;
19730 
19731 	NET_EPOCH_ASSERT();
19732 	INP_WLOCK_ASSERT(inp);
19733 
19734 	/* setup and take the cache hits here */
19735 	rack = (struct tcp_rack *)tp->t_fb_ptr;
19736 #ifdef TCP_ACCOUNTING
19737 	sched_pin();
19738 	ts_val = get_cyclecount();
19739 #endif
19740 	hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
19741 	tp->t_flags2 &= ~TF2_HPTS_CALLS;
19742 #ifdef TCP_OFFLOAD
19743 	if (tp->t_flags & TF_TOE) {
19744 #ifdef TCP_ACCOUNTING
19745 		sched_unpin();
19746 #endif
19747 		return (tcp_offload_output(tp));
19748 	}
19749 #endif
19750 	if (rack->rack_deferred_inited == 0) {
19751 		/*
19752 		 * If we are the connecting socket we will
19753 		 * hit rack_init() when no sequence numbers
19754 		 * are setup. This makes it so we must defer
19755 		 * some initialization. Call that now.
19756 		 */
19757 		rack_deferred_init(tp, rack);
19758 	}
19759 	/*
19760 	 * For TFO connections in SYN_RECEIVED, only allow the initial
19761 	 * SYN|ACK and those sent by the retransmit timer.
19762 	 */
19763 	if ((tp->t_flags & TF_FASTOPEN) &&
19764 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
19765 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
19766 	    (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
19767 #ifdef TCP_ACCOUNTING
19768 		sched_unpin();
19769 #endif
19770 		return (0);
19771 	}
19772 #ifdef INET6
19773 	if (rack->r_state) {
19774 		/* Use the cache line loaded if possible */
19775 		isipv6 = rack->r_is_v6;
19776 	} else {
19777 		isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
19778 	}
19779 #endif
19780 	early = 0;
19781 	cts = tcp_get_usecs(&tv);
19782 	ms_cts = tcp_tv_to_mssectick(&tv);
19783 	if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
19784 	    tcp_in_hpts(rack->rc_tp)) {
19785 		/*
19786 		 * We are on the hpts for some timer but not hptsi output.
19787 		 * Remove from the hpts unconditionally.
19788 		 */
19789 		rack_timer_cancel(tp, rack, cts, __LINE__);
19790 	}
19791 	/* Are we pacing and late? */
19792 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19793 	    TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
19794 		/* We are delayed */
19795 		delayed = cts - rack->r_ctl.rc_last_output_to;
19796 	} else {
19797 		delayed = 0;
19798 	}
19799 	/* Do the timers, which may override the pacer */
19800 	if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
19801 		int retval;
19802 
19803 		retval = rack_process_timers(tp, rack, cts, hpts_calling,
19804 					     &doing_tlp);
19805 		if (retval != 0) {
19806 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
19807 #ifdef TCP_ACCOUNTING
19808 			sched_unpin();
19809 #endif
19810 			/*
19811 			 * If timers want tcp_drop(), then pass error out,
19812 			 * otherwise suppress it.
19813 			 */
19814 			return (retval < 0 ? retval : 0);
19815 		}
19816 	}
19817 	if (rack->rc_in_persist) {
19818 		if (tcp_in_hpts(rack->rc_tp) == 0) {
19819 			/* Timer is not running */
19820 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19821 		}
19822 #ifdef TCP_ACCOUNTING
19823 		sched_unpin();
19824 #endif
19825 		return (0);
19826 	}
19827 	if ((rack->rc_ack_required == 1) &&
19828 	    (rack->r_timer_override == 0)){
19829 		/* A timeout occurred and no ack has arrived */
19830 		if (tcp_in_hpts(rack->rc_tp) == 0) {
19831 			/* Timer is not running */
19832 			rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19833 		}
19834 #ifdef TCP_ACCOUNTING
19835 		sched_unpin();
19836 #endif
19837 		return (0);
19838 	}
19839 	if ((rack->r_timer_override) ||
19840 	    (rack->rc_ack_can_sendout_data) ||
19841 	    (delayed) ||
19842 	    (tp->t_state < TCPS_ESTABLISHED)) {
19843 		rack->rc_ack_can_sendout_data = 0;
19844 		if (tcp_in_hpts(rack->rc_tp))
19845 			tcp_hpts_remove(rack->rc_tp);
19846 	} else if (tcp_in_hpts(rack->rc_tp)) {
19847 		/*
19848 		 * On the hpts you can't pass even if ACKNOW is on, we will
19849 		 * when the hpts fires.
19850 		 */
19851 #ifdef TCP_ACCOUNTING
19852 		crtsc = get_cyclecount();
19853 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19854 			tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
19855 			tp->tcp_cnt_counters[SND_BLOCKED]++;
19856 		}
19857 		sched_unpin();
19858 #endif
19859 		counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
19860 		return (0);
19861 	}
19862 	/* Finish out both pacing early and late accounting */
19863 	if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19864 	    TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
19865 		early = rack->r_ctl.rc_last_output_to - cts;
19866 	} else
19867 		early = 0;
19868 	if (delayed && (rack->rc_always_pace == 1)) {
19869 		rack->r_ctl.rc_agg_delayed += delayed;
19870 		rack->r_late = 1;
19871 	} else if (early && (rack->rc_always_pace == 1)) {
19872 		rack->r_ctl.rc_agg_early += early;
19873 		rack->r_early = 1;
19874 	} else if (rack->rc_always_pace == 0) {
19875 		/* Non-paced we are not late */
19876 		rack->r_ctl.rc_agg_delayed = rack->r_ctl.rc_agg_early = 0;
19877 		rack->r_early = rack->r_late = 0;
19878 	}
19879 	/* Now that early/late accounting is done turn off the flag */
19880 	rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
19881 	rack->r_wanted_output = 0;
19882 	rack->r_timer_override = 0;
19883 	if ((tp->t_state != rack->r_state) &&
19884 	    TCPS_HAVEESTABLISHED(tp->t_state)) {
19885 		rack_set_state(tp, rack);
19886 	}
19887 	if ((rack->r_fast_output) &&
19888 	    (doing_tlp == 0) &&
19889 	    (tp->rcv_numsacks == 0)) {
19890 		int ret;
19891 
19892 		error = 0;
19893 		ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19894 		if (ret >= 0)
19895 			return(ret);
19896 		else if (error) {
19897 			inp = rack->rc_inp;
19898 			so = inp->inp_socket;
19899 			sb = &so->so_snd;
19900 			goto nomore;
19901 		}
19902 	}
19903 	inp = rack->rc_inp;
19904 	/*
19905 	 * For TFO connections in SYN_SENT or SYN_RECEIVED,
19906 	 * only allow the initial SYN or SYN|ACK and those sent
19907 	 * by the retransmit timer.
19908 	 */
19909 	if ((tp->t_flags & TF_FASTOPEN) &&
19910 	    ((tp->t_state == TCPS_SYN_RECEIVED) ||
19911 	     (tp->t_state == TCPS_SYN_SENT)) &&
19912 	    SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
19913 	    (tp->t_rxtshift == 0)) {              /* not a retransmit */
19914 		rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19915 #ifdef TCP_ACCOUNTING
19916 		sched_unpin();
19917 #endif
19918 		return (0);
19919 	}
19920 	/*
19921 	 * Determine length of data that should be transmitted, and flags
19922 	 * that will be used. If there is some data or critical controls
19923 	 * (SYN, RST) to send, then transmit; otherwise, investigate
19924 	 * further.
19925 	 */
19926 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
19927 	if (tp->t_idle_reduce) {
19928 		if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
19929 			rack_cc_after_idle(rack, tp);
19930 	}
19931 	tp->t_flags &= ~TF_LASTIDLE;
19932 	if (idle) {
19933 		if (tp->t_flags & TF_MORETOCOME) {
19934 			tp->t_flags |= TF_LASTIDLE;
19935 			idle = 0;
19936 		}
19937 	}
19938 	if ((tp->snd_una == tp->snd_max) &&
19939 	    rack->r_ctl.rc_went_idle_time &&
19940 	    (cts > rack->r_ctl.rc_went_idle_time)) {
19941 		tot_idle = (cts - rack->r_ctl.rc_went_idle_time);
19942 		if (tot_idle > rack_min_probertt_hold) {
19943 			/* Count as a probe rtt */
19944 			if (rack->in_probe_rtt == 0) {
19945 				rack->r_ctl.rc_lower_rtt_us_cts = cts;
19946 				rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
19947 				rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
19948 				rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
19949 			} else {
19950 				rack_exit_probertt(rack, cts);
19951 			}
19952 		}
19953 	} else
19954 		tot_idle = 0;
19955 	if (rack_use_fsb &&
19956 	    (rack->r_ctl.fsb.tcp_ip_hdr) &&
19957 	    (rack->r_fsb_inited == 0) &&
19958 	    (rack->r_state != TCPS_CLOSED))
19959 		rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]);
19960 	if (rack->rc_sendvars_notset == 1) {
19961 		rack->rc_sendvars_notset = 0;
19962 		/*
19963 		 * Make sure any TCP timers (keep-alive) is not running.
19964 		 */
19965 		tcp_timer_stop(tp);
19966 	}
19967 	if ((rack->rack_no_prr == 1) &&
19968 	    (rack->rc_always_pace == 0)) {
19969 		/*
19970 		 * Sanity check before sending, if we have
19971 		 * no-pacing enabled and prr is turned off that
19972 		 * is a logistics error. Correct this by turnning
19973 		 * prr back on. A user *must* set some form of
19974 		 * pacing in order to turn PRR off. We do this
19975 		 * in the output path so that we can avoid socket
19976 		 * option ordering issues that would occur if we
19977 		 * tried to do it while setting rack_no_prr on.
19978 		 */
19979 		rack->rack_no_prr = 0;
19980 	}
19981 	if ((rack->pcm_enabled == 1) &&
19982 	    (rack->pcm_needed == 0) &&
19983 	    (tot_idle > 0)) {
19984 		/*
19985 		 * We have been idle some micro seconds. We need
19986 		 * to factor this in to see if a PCM is needed.
19987 		 */
19988 		uint32_t rtts_idle, rnds;
19989 
19990 		if (tp->t_srtt)
19991 			rtts_idle = tot_idle / tp->t_srtt;
19992 		else
19993 			rtts_idle = 0;
19994 		rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round;
19995 		rack->r_ctl.pcm_idle_rounds += rtts_idle;
19996 		if ((rnds + rack->r_ctl.pcm_idle_rounds)  >= rack_pcm_every_n_rounds) {
19997 			rack->pcm_needed = 1;
19998 			rack_log_pcm(rack, 8, rack->r_ctl.last_pcm_round, rtts_idle, rack->r_ctl.current_round );
19999 		}
20000 	}
20001 again:
20002 	sendalot = 0;
20003 	cts = tcp_get_usecs(&tv);
20004 	ms_cts = tcp_tv_to_mssectick(&tv);
20005 	tso = 0;
20006 	mtu = 0;
20007 	segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
20008 	minseg = segsiz;
20009 	if (rack->r_ctl.rc_pace_max_segs == 0)
20010 		pace_max_seg = rack->rc_user_set_max_segs * segsiz;
20011 	else
20012 		pace_max_seg = rack->r_ctl.rc_pace_max_segs;
20013 	if (TCPS_HAVEESTABLISHED(tp->t_state) &&
20014 	    (rack->r_ctl.pcm_max_seg == 0)) {
20015 		/*
20016 		 * We set in our first send so we know that the ctf_fixed_maxseg
20017 		 * has been fully set. If we do it in rack_init() we most likely
20018 		 * see 512 bytes so we end up at 5120, not desirable.
20019 		 */
20020 		rack->r_ctl.pcm_max_seg = rc_init_window(rack);
20021 		if (rack->r_ctl.pcm_max_seg < (ctf_fixed_maxseg(tp) * 10)) {
20022 			/*
20023 			 * Assure our initial PCM probe is at least 10 MSS.
20024 			 */
20025 			rack->r_ctl.pcm_max_seg = ctf_fixed_maxseg(tp) * 10;
20026 		}
20027 	}
20028 	if ((rack->r_ctl.pcm_max_seg != 0)  && (rack->pcm_needed == 1)) {
20029 		uint32_t rw_avail, cwa;
20030 
20031 		if (tp->snd_wnd > ctf_outstanding(tp))
20032 			rw_avail = tp->snd_wnd - ctf_outstanding(tp);
20033 		else
20034 			rw_avail = 0;
20035 		if (tp->snd_cwnd > ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked))
20036 			cwa = tp->snd_cwnd -ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
20037 		else
20038 			cwa = 0;
20039 		if ((cwa >= rack->r_ctl.pcm_max_seg) &&
20040 		    (rw_avail > rack->r_ctl.pcm_max_seg)) {
20041 			/* Raise up the max seg for this trip through */
20042 			pace_max_seg = rack->r_ctl.pcm_max_seg;
20043 			/* Disable any fast output */
20044 			rack->r_fast_output = 0;
20045 		}
20046 		if (rack_verbose_logging) {
20047 			rack_log_pcm(rack, 4,
20048 				     cwa, rack->r_ctl.pcm_max_seg, rw_avail);
20049 		}
20050 	}
20051 	sb_offset = tp->snd_max - tp->snd_una;
20052 	cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
20053 	flags = tcp_outflags[tp->t_state];
20054 	while (rack->rc_free_cnt < rack_free_cache) {
20055 		rsm = rack_alloc(rack);
20056 		if (rsm == NULL) {
20057 			if (hpts_calling)
20058 				/* Retry in a ms */
20059 				slot = (1 * HPTS_USEC_IN_MSEC);
20060 			so = inp->inp_socket;
20061 			sb = &so->so_snd;
20062 			goto just_return_nolock;
20063 		}
20064 		TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
20065 		rack->rc_free_cnt++;
20066 		rsm = NULL;
20067 	}
20068 	sack_rxmit = 0;
20069 	len = 0;
20070 	rsm = NULL;
20071 	if (flags & TH_RST) {
20072 		SOCK_SENDBUF_LOCK(inp->inp_socket);
20073 		so = inp->inp_socket;
20074 		sb = &so->so_snd;
20075 		goto send;
20076 	}
20077 	if (rack->r_ctl.rc_resend) {
20078 		/* Retransmit timer */
20079 		rsm = rack->r_ctl.rc_resend;
20080 		rack->r_ctl.rc_resend = NULL;
20081 		len = rsm->r_end - rsm->r_start;
20082 		sack_rxmit = 1;
20083 		sendalot = 0;
20084 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20085 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20086 			 __func__, __LINE__,
20087 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20088 		sb_offset = rsm->r_start - tp->snd_una;
20089 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20090 	} else if (rack->r_collapse_point_valid &&
20091 		   ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
20092 		/*
20093 		 * If an RSM is returned then enough time has passed
20094 		 * for us to retransmit it. Move up the collapse point,
20095 		 * since this rsm has its chance to retransmit now.
20096 		 */
20097 		tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT);
20098 		rack->r_ctl.last_collapse_point = rsm->r_end;
20099 		/* Are we done? */
20100 		if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
20101 			    rack->r_ctl.high_collapse_point))
20102 			rack->r_collapse_point_valid = 0;
20103 		sack_rxmit = 1;
20104 		/* We are not doing a TLP */
20105 		doing_tlp = 0;
20106 		len = rsm->r_end - rsm->r_start;
20107 		sb_offset = rsm->r_start - tp->snd_una;
20108 		sendalot = 0;
20109 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20110 	} else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
20111 		/* We have a retransmit that takes precedence */
20112 		if ((!IN_FASTRECOVERY(tp->t_flags)) &&
20113 		    ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
20114 		    ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
20115 			/* Enter recovery if not induced by a time-out */
20116 			rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
20117 		}
20118 #ifdef INVARIANTS
20119 		if (SEQ_LT(rsm->r_start, tp->snd_una)) {
20120 			panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
20121 			      tp, rack, rsm, rsm->r_start, tp->snd_una);
20122 		}
20123 #endif
20124 		len = rsm->r_end - rsm->r_start;
20125 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20126 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20127 			 __func__, __LINE__,
20128 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20129 		sb_offset = rsm->r_start - tp->snd_una;
20130 		sendalot = 0;
20131 		rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
20132 		if (len > 0) {
20133 			sack_rxmit = 1;
20134 			KMOD_TCPSTAT_INC(tcps_sack_rexmits);
20135 			KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
20136 					 min(len, segsiz));
20137 		}
20138 	} else if (rack->r_ctl.rc_tlpsend) {
20139 		/* Tail loss probe */
20140 		long cwin;
20141 		long tlen;
20142 
20143 		/*
20144 		 * Check if we can do a TLP with a RACK'd packet
20145 		 * this can happen if we are not doing the rack
20146 		 * cheat and we skipped to a TLP and it
20147 		 * went off.
20148 		 */
20149 		rsm = rack->r_ctl.rc_tlpsend;
20150 		/* We are doing a TLP make sure the flag is preent */
20151 		rsm->r_flags |= RACK_TLP;
20152 		rack->r_ctl.rc_tlpsend = NULL;
20153 		sack_rxmit = 1;
20154 		tlen = rsm->r_end - rsm->r_start;
20155 		if (tlen > segsiz)
20156 			tlen = segsiz;
20157 		KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20158 			("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20159 			 __func__, __LINE__,
20160 			 rsm->r_start, tp->snd_una, tp, rack, rsm));
20161 		sb_offset = rsm->r_start - tp->snd_una;
20162 		cwin = min(tp->snd_wnd, tlen);
20163 		len = cwin;
20164 	}
20165 	if (rack->r_must_retran &&
20166 	    (doing_tlp == 0) &&
20167 	    (SEQ_GT(tp->snd_max, tp->snd_una)) &&
20168 	    (rsm == NULL)) {
20169 		/*
20170 		 * There are two different ways that we
20171 		 * can get into this block:
20172 		 * a) This is a non-sack connection, we had a time-out
20173 		 *    and thus r_must_retran was set and everything
20174 		 *    left outstanding as been marked for retransmit.
20175 		 * b) The MTU of the path shrank, so that everything
20176 		 *    was marked to be retransmitted with the smaller
20177 		 *    mtu and r_must_retran was set.
20178 		 *
20179 		 * This means that we expect the sendmap (outstanding)
20180 		 * to all be marked must. We can use the tmap to
20181 		 * look at them.
20182 		 *
20183 		 */
20184 		int sendwin, flight;
20185 
20186 		sendwin = min(tp->snd_wnd, tp->snd_cwnd);
20187 		flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
20188 		if (flight >= sendwin) {
20189 			/*
20190 			 * We can't send yet.
20191 			 */
20192 			so = inp->inp_socket;
20193 			sb = &so->so_snd;
20194 			goto just_return_nolock;
20195 		}
20196 		/*
20197 		 * This is the case a/b mentioned above. All
20198 		 * outstanding/not-acked should be marked.
20199 		 * We can use the tmap to find them.
20200 		 */
20201 		rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
20202 		if (rsm == NULL) {
20203 			/* TSNH */
20204 			rack->r_must_retran = 0;
20205 			rack->r_ctl.rc_out_at_rto = 0;
20206 			so = inp->inp_socket;
20207 			sb = &so->so_snd;
20208 			goto just_return_nolock;
20209 		}
20210 		if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
20211 			/*
20212 			 * The first one does not have the flag, did we collapse
20213 			 * further up in our list?
20214 			 */
20215 			rack->r_must_retran = 0;
20216 			rack->r_ctl.rc_out_at_rto = 0;
20217 			rsm = NULL;
20218 			sack_rxmit = 0;
20219 		} else {
20220 			sack_rxmit = 1;
20221 			len = rsm->r_end - rsm->r_start;
20222 			sb_offset = rsm->r_start - tp->snd_una;
20223 			sendalot = 0;
20224 			if ((rack->full_size_rxt == 0) &&
20225 			    (rack->shape_rxt_to_pacing_min == 0) &&
20226 			    (len >= segsiz))
20227 				len = segsiz;
20228 			else if (rack->shape_rxt_to_pacing_min &&
20229 				 rack->gp_ready) {
20230 				/* We use pacing min as shaping len req */
20231 				uint32_t maxlen;
20232 
20233 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20234 				if (len > maxlen)
20235 					len = maxlen;
20236 			}
20237 			/*
20238 			 * Delay removing the flag RACK_MUST_RXT so
20239 			 * that the fastpath for retransmit will
20240 			 * work with this rsm.
20241 			 */
20242 		}
20243 	}
20244 	/*
20245 	 * Enforce a connection sendmap count limit if set
20246 	 * as long as we are not retransmiting.
20247 	 */
20248 	if ((rsm == NULL) &&
20249 	    (V_tcp_map_entries_limit > 0) &&
20250 	    (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
20251 		counter_u64_add(rack_to_alloc_limited, 1);
20252 		if (!rack->alloc_limit_reported) {
20253 			rack->alloc_limit_reported = 1;
20254 			counter_u64_add(rack_alloc_limited_conns, 1);
20255 		}
20256 		so = inp->inp_socket;
20257 		sb = &so->so_snd;
20258 		goto just_return_nolock;
20259 	}
20260 	if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
20261 		/* we are retransmitting the fin */
20262 		len--;
20263 		if (len) {
20264 			/*
20265 			 * When retransmitting data do *not* include the
20266 			 * FIN. This could happen from a TLP probe.
20267 			 */
20268 			flags &= ~TH_FIN;
20269 		}
20270 	}
20271 	if (rsm && rack->r_fsb_inited &&
20272 	    rack_use_rsm_rfo &&
20273 	    ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
20274 		int ret;
20275 
20276 		ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
20277 		if (ret == 0)
20278 			return (0);
20279 	}
20280 	so = inp->inp_socket;
20281 	sb = &so->so_snd;
20282 	if (do_a_prefetch == 0) {
20283 		kern_prefetch(sb, &do_a_prefetch);
20284 		do_a_prefetch = 1;
20285 	}
20286 #ifdef NETFLIX_SHARED_CWND
20287 	if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
20288 	    rack->rack_enable_scwnd) {
20289 		/* We are doing cwnd sharing */
20290 		if (rack->gp_ready &&
20291 		    (rack->rack_attempted_scwnd == 0) &&
20292 		    (rack->r_ctl.rc_scw == NULL) &&
20293 		    tp->t_lib) {
20294 			/* The pcbid is in, lets make an attempt */
20295 			counter_u64_add(rack_try_scwnd, 1);
20296 			rack->rack_attempted_scwnd = 1;
20297 			rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
20298 								   &rack->r_ctl.rc_scw_index,
20299 								   segsiz);
20300 		}
20301 		if (rack->r_ctl.rc_scw &&
20302 		    (rack->rack_scwnd_is_idle == 1) &&
20303 		    sbavail(&so->so_snd)) {
20304 			/* we are no longer out of data */
20305 			tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
20306 			rack->rack_scwnd_is_idle = 0;
20307 		}
20308 		if (rack->r_ctl.rc_scw) {
20309 			/* First lets update and get the cwnd */
20310 			rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
20311 										       rack->r_ctl.rc_scw_index,
20312 										       tp->snd_cwnd, tp->snd_wnd, segsiz);
20313 		}
20314 	}
20315 #endif
20316 	/*
20317 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
20318 	 * state flags.
20319 	 */
20320 	if (tp->t_flags & TF_NEEDFIN)
20321 		flags |= TH_FIN;
20322 	if (tp->t_flags & TF_NEEDSYN)
20323 		flags |= TH_SYN;
20324 	if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
20325 		void *end_rsm;
20326 		end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
20327 		if (end_rsm)
20328 			kern_prefetch(end_rsm, &prefetch_rsm);
20329 		prefetch_rsm = 1;
20330 	}
20331 	SOCK_SENDBUF_LOCK(so);
20332 	if ((sack_rxmit == 0) &&
20333 	    (TCPS_HAVEESTABLISHED(tp->t_state) ||
20334 	    (tp->t_flags & TF_FASTOPEN))) {
20335 		/*
20336 		 * We are not retransmitting (sack_rxmit is 0) so we
20337 		 * are sending new data. This is always based on snd_max.
20338 		 * Now in theory snd_max may be equal to snd_una, if so
20339 		 * then nothing is outstanding and the offset would be 0.
20340 		 */
20341 		uint32_t avail;
20342 
20343 		avail = sbavail(sb);
20344 		if (SEQ_GT(tp->snd_max, tp->snd_una) && avail)
20345 			sb_offset = tp->snd_max - tp->snd_una;
20346 		else
20347 			sb_offset = 0;
20348 		if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
20349 			if (rack->r_ctl.rc_tlp_new_data) {
20350 				/* TLP is forcing out new data */
20351 				if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
20352 					rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
20353 				}
20354 				if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
20355 					if (tp->snd_wnd > sb_offset)
20356 						len = tp->snd_wnd - sb_offset;
20357 					else
20358 						len = 0;
20359 				} else {
20360 					len = rack->r_ctl.rc_tlp_new_data;
20361 				}
20362 				rack->r_ctl.rc_tlp_new_data = 0;
20363 			}  else {
20364 				len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
20365 			}
20366 			if ((rack->r_ctl.crte == NULL) &&
20367 			    IN_FASTRECOVERY(tp->t_flags) &&
20368 			    (rack->full_size_rxt == 0) &&
20369 			    (rack->shape_rxt_to_pacing_min == 0) &&
20370 			    (len > segsiz)) {
20371 				/*
20372 				 * For prr=off, we need to send only 1 MSS
20373 				 * at a time. We do this because another sack could
20374 				 * be arriving that causes us to send retransmits and
20375 				 * we don't want to be on a long pace due to a larger send
20376 				 * that keeps us from sending out the retransmit.
20377 				 */
20378 				len = segsiz;
20379 			} else if (rack->shape_rxt_to_pacing_min &&
20380 				   rack->gp_ready) {
20381 				/* We use pacing min as shaping len req */
20382 				uint32_t maxlen;
20383 
20384 				maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20385 				if (len > maxlen)
20386 					len = maxlen;
20387 			}/* The else is full_size_rxt is on so send it all */
20388 		} else {
20389 			uint32_t outstanding;
20390 			/*
20391 			 * We are inside of a Fast recovery episode, this
20392 			 * is caused by a SACK or 3 dup acks. At this point
20393 			 * we have sent all the retransmissions and we rely
20394 			 * on PRR to dictate what we will send in the form of
20395 			 * new data.
20396 			 */
20397 
20398 			outstanding = tp->snd_max - tp->snd_una;
20399 			if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
20400 				if (tp->snd_wnd > outstanding) {
20401 					len = tp->snd_wnd - outstanding;
20402 					/* Check to see if we have the data */
20403 					if ((sb_offset + len) > avail) {
20404 						/* It does not all fit */
20405 						if (avail > sb_offset)
20406 							len = avail - sb_offset;
20407 						else
20408 							len = 0;
20409 					}
20410 				} else {
20411 					len = 0;
20412 				}
20413 			} else if (avail > sb_offset) {
20414 				len = avail - sb_offset;
20415 			} else {
20416 				len = 0;
20417 			}
20418 			if (len > 0) {
20419 				if (len > rack->r_ctl.rc_prr_sndcnt) {
20420 					len = rack->r_ctl.rc_prr_sndcnt;
20421 				}
20422 				if (len > 0) {
20423 					sub_from_prr = 1;
20424 				}
20425 			}
20426 			if (len > segsiz) {
20427 				/*
20428 				 * We should never send more than a MSS when
20429 				 * retransmitting or sending new data in prr
20430 				 * mode unless the override flag is on. Most
20431 				 * likely the PRR algorithm is not going to
20432 				 * let us send a lot as well :-)
20433 				 */
20434 				if (rack->r_ctl.rc_prr_sendalot == 0) {
20435 					len = segsiz;
20436 				}
20437 			} else if (len < segsiz) {
20438 				/*
20439 				 * Do we send any? The idea here is if the
20440 				 * send empty's the socket buffer we want to
20441 				 * do it. However if not then lets just wait
20442 				 * for our prr_sndcnt to get bigger.
20443 				 */
20444 				long leftinsb;
20445 
20446 				leftinsb = sbavail(sb) - sb_offset;
20447 				if (leftinsb > len) {
20448 					/* This send does not empty the sb */
20449 					len = 0;
20450 				}
20451 			}
20452 		}
20453 	} else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
20454 		/*
20455 		 * If you have not established
20456 		 * and are not doing FAST OPEN
20457 		 * no data please.
20458 		 */
20459 		if ((sack_rxmit == 0) &&
20460 		    !(tp->t_flags & TF_FASTOPEN)) {
20461 			len = 0;
20462 			sb_offset = 0;
20463 		}
20464 	}
20465 	if (prefetch_so_done == 0) {
20466 		kern_prefetch(so, &prefetch_so_done);
20467 		prefetch_so_done = 1;
20468 	}
20469 	orig_len = len;
20470 	/*
20471 	 * Lop off SYN bit if it has already been sent.  However, if this is
20472 	 * SYN-SENT state and if segment contains data and if we don't know
20473 	 * that foreign host supports TAO, suppress sending segment.
20474 	 */
20475 	if ((flags & TH_SYN) &&
20476 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
20477 	    ((sack_rxmit == 0) &&
20478 	     (tp->t_rxtshift == 0))) {
20479 		/*
20480 		 * When sending additional segments following a TFO SYN|ACK,
20481 		 * do not include the SYN bit.
20482 		 */
20483 		if ((tp->t_flags & TF_FASTOPEN) &&
20484 		    (tp->t_state == TCPS_SYN_RECEIVED))
20485 			flags &= ~TH_SYN;
20486 	}
20487 	/*
20488 	 * Be careful not to send data and/or FIN on SYN segments. This
20489 	 * measure is needed to prevent interoperability problems with not
20490 	 * fully conformant TCP implementations.
20491 	 */
20492 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
20493 		len = 0;
20494 		flags &= ~TH_FIN;
20495 	}
20496 	/*
20497 	 * On TFO sockets, ensure no data is sent in the following cases:
20498 	 *
20499 	 *  - When retransmitting SYN|ACK on a passively-created socket
20500 	 *
20501 	 *  - When retransmitting SYN on an actively created socket
20502 	 *
20503 	 *  - When sending a zero-length cookie (cookie request) on an
20504 	 *    actively created socket
20505 	 *
20506 	 *  - When the socket is in the CLOSED state (RST is being sent)
20507 	 */
20508 	if ((tp->t_flags & TF_FASTOPEN) &&
20509 	    (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
20510 	     ((tp->t_state == TCPS_SYN_SENT) &&
20511 	      (tp->t_tfo_client_cookie_len == 0)) ||
20512 	     (flags & TH_RST))) {
20513 		sack_rxmit = 0;
20514 		len = 0;
20515 	}
20516 	/* Without fast-open there should never be data sent on a SYN */
20517 	if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) {
20518 		len = 0;
20519 	}
20520 	if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
20521 		/* We only send 1 MSS if we have a DSACK block */
20522 		add_flag |= RACK_SENT_W_DSACK;
20523 		len = segsiz;
20524 	}
20525 	if (len <= 0) {
20526 		/*
20527 		 * We have nothing to send, or the window shrank, or
20528 		 * is closed, do we need to go into persists?
20529 		 */
20530 		len = 0;
20531 		if ((tp->snd_wnd == 0) &&
20532 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20533 		    (tp->snd_una == tp->snd_max) &&
20534 		    (sb_offset < (int)sbavail(sb))) {
20535 			rack_enter_persist(tp, rack, cts, tp->snd_una);
20536 		}
20537 	} else if ((rsm == NULL) &&
20538 		   (doing_tlp == 0) &&
20539 		   (len < pace_max_seg)) {
20540 		/*
20541 		 * We are not sending a maximum sized segment for
20542 		 * some reason. Should we not send anything (think
20543 		 * sws or persists)?
20544 		 */
20545 		if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20546 		    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20547 		    (len < minseg) &&
20548 		    (len < (int)(sbavail(sb) - sb_offset))) {
20549 			/*
20550 			 * Here the rwnd is less than
20551 			 * the minimum pacing size, this is not a retransmit,
20552 			 * we are established and
20553 			 * the send is not the last in the socket buffer
20554 			 * we send nothing, and we may enter persists
20555 			 * if nothing is outstanding.
20556 			 */
20557 			len = 0;
20558 			if (tp->snd_max == tp->snd_una) {
20559 				/*
20560 				 * Nothing out we can
20561 				 * go into persists.
20562 				 */
20563 				rack_enter_persist(tp, rack, cts, tp->snd_una);
20564 			}
20565 		} else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
20566 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20567 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20568 			   (len < minseg)) {
20569 			/*
20570 			 * Here we are not retransmitting, and
20571 			 * the cwnd is not so small that we could
20572 			 * not send at least a min size (rxt timer
20573 			 * not having gone off), We have 2 segments or
20574 			 * more already in flight, its not the tail end
20575 			 * of the socket buffer  and the cwnd is blocking
20576 			 * us from sending out a minimum pacing segment size.
20577 			 * Lets not send anything.
20578 			 */
20579 			len = 0;
20580 		} else if (((tp->snd_wnd - ctf_outstanding(tp)) <
20581 			    min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20582 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20583 			   (len < (int)(sbavail(sb) - sb_offset)) &&
20584 			   (TCPS_HAVEESTABLISHED(tp->t_state))) {
20585 			/*
20586 			 * Here we have a send window but we have
20587 			 * filled it up and we can't send another pacing segment.
20588 			 * We also have in flight more than 2 segments
20589 			 * and we are not completing the sb i.e. we allow
20590 			 * the last bytes of the sb to go out even if
20591 			 * its not a full pacing segment.
20592 			 */
20593 			len = 0;
20594 		} else if ((rack->r_ctl.crte != NULL) &&
20595 			   (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
20596 			   (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
20597 			   (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
20598 			   (len < (int)(sbavail(sb) - sb_offset))) {
20599 			/*
20600 			 * Here we are doing hardware pacing, this is not a TLP,
20601 			 * we are not sending a pace max segment size, there is rwnd
20602 			 * room to send at least N pace_max_seg, the cwnd is greater
20603 			 * than or equal to a full pacing segments plus 4 mss and we have 2 or
20604 			 * more segments in flight and its not the tail of the socket buffer.
20605 			 *
20606 			 * We don't want to send instead we need to get more ack's in to
20607 			 * allow us to send a full pacing segment. Normally, if we are pacing
20608 			 * about the right speed, we should have finished our pacing
20609 			 * send as most of the acks have come back if we are at the
20610 			 * right rate. This is a bit fuzzy since return path delay
20611 			 * can delay the acks, which is why we want to make sure we
20612 			 * have cwnd space to have a bit more than a max pace segments in flight.
20613 			 *
20614 			 * If we have not gotten our acks back we are pacing at too high a
20615 			 * rate delaying will not hurt and will bring our GP estimate down by
20616 			 * injecting the delay. If we don't do this we will send
20617 			 * 2 MSS out in response to the acks being clocked in which
20618 			 * defeats the point of hw-pacing (i.e. to help us get
20619 			 * larger TSO's out).
20620 			 */
20621 			len = 0;
20622 		}
20623 
20624 	}
20625 	/* len will be >= 0 after this point. */
20626 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
20627 	rack_sndbuf_autoscale(rack);
20628 	/*
20629 	 * Decide if we can use TCP Segmentation Offloading (if supported by
20630 	 * hardware).
20631 	 *
20632 	 * TSO may only be used if we are in a pure bulk sending state.  The
20633 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
20634 	 * options prevent using TSO.  With TSO the TCP header is the same
20635 	 * (except for the sequence number) for all generated packets.  This
20636 	 * makes it impossible to transmit any options which vary per
20637 	 * generated segment or packet.
20638 	 *
20639 	 * IPv4 handling has a clear separation of ip options and ip header
20640 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
20641 	 * the right thing below to provide length of just ip options and thus
20642 	 * checking for ipoptlen is enough to decide if ip options are present.
20643 	 */
20644 	ipoptlen = 0;
20645 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20646 	/*
20647 	 * Pre-calculate here as we save another lookup into the darknesses
20648 	 * of IPsec that way and can actually decide if TSO is ok.
20649 	 */
20650 #ifdef INET6
20651 	if (isipv6 && IPSEC_ENABLED(ipv6))
20652 		ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
20653 #ifdef INET
20654 	else
20655 #endif
20656 #endif				/* INET6 */
20657 #ifdef INET
20658 		if (IPSEC_ENABLED(ipv4))
20659 			ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
20660 #endif				/* INET */
20661 #endif
20662 
20663 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20664 	ipoptlen += ipsec_optlen;
20665 #endif
20666 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
20667 	    (tp->t_port == 0) &&
20668 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
20669 	    sack_rxmit == 0 &&
20670 	    ipoptlen == 0)
20671 		tso = 1;
20672 	{
20673 		uint32_t outstanding __unused;
20674 
20675 		outstanding = tp->snd_max - tp->snd_una;
20676 		if (tp->t_flags & TF_SENTFIN) {
20677 			/*
20678 			 * If we sent a fin, snd_max is 1 higher than
20679 			 * snd_una
20680 			 */
20681 			outstanding--;
20682 		}
20683 		if (sack_rxmit) {
20684 			if ((rsm->r_flags & RACK_HAS_FIN) == 0)
20685 				flags &= ~TH_FIN;
20686 		}
20687 	}
20688 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
20689 		      (long)TCP_MAXWIN << tp->rcv_scale);
20690 
20691 	/*
20692 	 * Sender silly window avoidance.   We transmit under the following
20693 	 * conditions when len is non-zero:
20694 	 *
20695 	 * - We have a full segment (or more with TSO) - This is the last
20696 	 * buffer in a write()/send() and we are either idle or running
20697 	 * NODELAY - we've timed out (e.g. persist timer) - we have more
20698 	 * then 1/2 the maximum send window's worth of data (receiver may be
20699 	 * limited the window size) - we need to retransmit
20700 	 */
20701 	if (len) {
20702 		if (len >= segsiz) {
20703 			goto send;
20704 		}
20705 		/*
20706 		 * NOTE! on localhost connections an 'ack' from the remote
20707 		 * end may occur synchronously with the output and cause us
20708 		 * to flush a buffer queued with moretocome.  XXX
20709 		 *
20710 		 */
20711 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
20712 		    (idle || (tp->t_flags & TF_NODELAY)) &&
20713 		    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20714 		    (tp->t_flags & TF_NOPUSH) == 0) {
20715 			pass = 2;
20716 			goto send;
20717 		}
20718 		if ((tp->snd_una == tp->snd_max) && len) {	/* Nothing outstanding */
20719 			pass = 22;
20720 			goto send;
20721 		}
20722 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
20723 			pass = 4;
20724 			goto send;
20725 		}
20726 		if (sack_rxmit) {
20727 			pass = 6;
20728 			goto send;
20729 		}
20730 		if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
20731 		    (ctf_outstanding(tp) < (segsiz * 2))) {
20732 			/*
20733 			 * We have less than two MSS outstanding (delayed ack)
20734 			 * and our rwnd will not let us send a full sized
20735 			 * MSS. Lets go ahead and let this small segment
20736 			 * out because we want to try to have at least two
20737 			 * packets inflight to not be caught by delayed ack.
20738 			 */
20739 			pass = 12;
20740 			goto send;
20741 		}
20742 	}
20743 	/*
20744 	 * Sending of standalone window updates.
20745 	 *
20746 	 * Window updates are important when we close our window due to a
20747 	 * full socket buffer and are opening it again after the application
20748 	 * reads data from it.  Once the window has opened again and the
20749 	 * remote end starts to send again the ACK clock takes over and
20750 	 * provides the most current window information.
20751 	 *
20752 	 * We must avoid the silly window syndrome whereas every read from
20753 	 * the receive buffer, no matter how small, causes a window update
20754 	 * to be sent.  We also should avoid sending a flurry of window
20755 	 * updates when the socket buffer had queued a lot of data and the
20756 	 * application is doing small reads.
20757 	 *
20758 	 * Prevent a flurry of pointless window updates by only sending an
20759 	 * update when we can increase the advertized window by more than
20760 	 * 1/4th of the socket buffer capacity.  When the buffer is getting
20761 	 * full or is very small be more aggressive and send an update
20762 	 * whenever we can increase by two mss sized segments. In all other
20763 	 * situations the ACK's to new incoming data will carry further
20764 	 * window increases.
20765 	 *
20766 	 * Don't send an independent window update if a delayed ACK is
20767 	 * pending (it will get piggy-backed on it) or the remote side
20768 	 * already has done a half-close and won't send more data.  Skip
20769 	 * this if the connection is in T/TCP half-open state.
20770 	 */
20771 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
20772 	    !(tp->t_flags & TF_DELACK) &&
20773 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
20774 		/*
20775 		 * "adv" is the amount we could increase the window, taking
20776 		 * into account that we are limited by TCP_MAXWIN <<
20777 		 * tp->rcv_scale.
20778 		 */
20779 		int32_t adv;
20780 		int oldwin;
20781 
20782 		adv = recwin;
20783 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
20784 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
20785 			if (adv > oldwin)
20786 				adv -= oldwin;
20787 			else {
20788 				/* We can't increase the window */
20789 				adv = 0;
20790 			}
20791 		} else
20792 			oldwin = 0;
20793 
20794 		/*
20795 		 * If the new window size ends up being the same as or less
20796 		 * than the old size when it is scaled, then don't force
20797 		 * a window update.
20798 		 */
20799 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
20800 			goto dontupdate;
20801 
20802 		if (adv >= (int32_t)(2 * segsiz) &&
20803 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
20804 		     recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
20805 		     so->so_rcv.sb_hiwat <= 8 * segsiz)) {
20806 			pass = 7;
20807 			goto send;
20808 		}
20809 		if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
20810 			pass = 23;
20811 			goto send;
20812 		}
20813 	}
20814 dontupdate:
20815 
20816 	/*
20817 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
20818 	 * is also a catch-all for the retransmit timer timeout case.
20819 	 */
20820 	if (tp->t_flags & TF_ACKNOW) {
20821 		pass = 8;
20822 		goto send;
20823 	}
20824 	if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
20825 		pass = 9;
20826 		goto send;
20827 	}
20828 	/*
20829 	 * If our state indicates that FIN should be sent and we have not
20830 	 * yet done so, then we need to send.
20831 	 */
20832 	if ((flags & TH_FIN) &&
20833 	    (tp->snd_max == tp->snd_una)) {
20834 		pass = 11;
20835 		goto send;
20836 	}
20837 	/*
20838 	 * No reason to send a segment, just return.
20839 	 */
20840 just_return:
20841 	SOCK_SENDBUF_UNLOCK(so);
20842 just_return_nolock:
20843 	{
20844 		int app_limited = CTF_JR_SENT_DATA;
20845 
20846 		if ((tp->t_flags & TF_FASTOPEN) == 0 &&
20847 		    (flags & TH_FIN) &&
20848 		    (len == 0) &&
20849 		    (sbused(sb) == (tp->snd_max - tp->snd_una)) &&
20850 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
20851 			/*
20852 			 * Ok less than or right at a MSS is
20853 			 * outstanding. The original FreeBSD stack would
20854 			 * have sent a FIN, which can speed things up for
20855 			 * a transactional application doing a MSG_WAITALL.
20856 			 * To speed things up since we do *not* send a FIN
20857 			 * if data is outstanding, we send a "challenge ack".
20858 			 * The idea behind that is instead of having to have
20859 			 * the peer wait for the delayed-ack timer to run off
20860 			 * we send an ack that makes the peer send us an ack.
20861 			 */
20862 			rack_send_ack_challange(rack);
20863 		}
20864 		if (tot_len_this_send > 0) {
20865 			rack->r_ctl.fsb.recwin = recwin;
20866 			slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz, __LINE__);
20867 			if ((error == 0) &&
20868 			    rack_use_rfo &&
20869 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
20870 			    (ipoptlen == 0) &&
20871 			    rack->r_fsb_inited &&
20872 			    TCPS_HAVEESTABLISHED(tp->t_state) &&
20873 			    ((IN_RECOVERY(tp->t_flags)) == 0) &&
20874 			    (rack->r_must_retran == 0) &&
20875 			    ((tp->t_flags & TF_NEEDFIN) == 0) &&
20876 			    (len > 0) && (orig_len > 0) &&
20877 			    (orig_len > len) &&
20878 			    ((orig_len - len) >= segsiz) &&
20879 			    ((optlen == 0) ||
20880 			     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
20881 				/* We can send at least one more MSS using our fsb */
20882 				rack_setup_fast_output(tp, rack, sb, len, orig_len,
20883 						       segsiz, pace_max_seg, hw_tls, flags);
20884 			} else
20885 				rack->r_fast_output = 0;
20886 			rack_log_fsb(rack, tp, so, flags,
20887 				     ipoptlen, orig_len, len, 0,
20888 				     1, optlen, __LINE__, 1);
20889 			/* Assure when we leave that snd_nxt will point to top */
20890 			if (SEQ_GT(tp->snd_max, tp->snd_nxt))
20891 				tp->snd_nxt = tp->snd_max;
20892 		} else {
20893 			int end_window = 0;
20894 			uint32_t seq = tp->gput_ack;
20895 
20896 			rsm = tqhash_max(rack->r_ctl.tqh);
20897 			if (rsm) {
20898 				/*
20899 				 * Mark the last sent that we just-returned (hinting
20900 				 * that delayed ack may play a role in any rtt measurement).
20901 				 */
20902 				rsm->r_just_ret = 1;
20903 			}
20904 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
20905 			rack->r_ctl.rc_agg_delayed = 0;
20906 			rack->r_early = 0;
20907 			rack->r_late = 0;
20908 			rack->r_ctl.rc_agg_early = 0;
20909 			if ((ctf_outstanding(tp) +
20910 			     min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
20911 				 minseg)) >= tp->snd_wnd) {
20912 				/* We are limited by the rwnd */
20913 				app_limited = CTF_JR_RWND_LIMITED;
20914 				if (IN_FASTRECOVERY(tp->t_flags))
20915 					rack->r_ctl.rc_prr_sndcnt = 0;
20916 			} else if (ctf_outstanding(tp) >= sbavail(sb)) {
20917 				/* We are limited by whats available -- app limited */
20918 				app_limited = CTF_JR_APP_LIMITED;
20919 				if (IN_FASTRECOVERY(tp->t_flags))
20920 					rack->r_ctl.rc_prr_sndcnt = 0;
20921 			} else if ((idle == 0) &&
20922 				   ((tp->t_flags & TF_NODELAY) == 0) &&
20923 				   ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20924 				   (len < segsiz)) {
20925 				/*
20926 				 * No delay is not on and the
20927 				 * user is sending less than 1MSS. This
20928 				 * brings out SWS avoidance so we
20929 				 * don't send. Another app-limited case.
20930 				 */
20931 				app_limited = CTF_JR_APP_LIMITED;
20932 			} else if (tp->t_flags & TF_NOPUSH) {
20933 				/*
20934 				 * The user has requested no push of
20935 				 * the last segment and we are
20936 				 * at the last segment. Another app
20937 				 * limited case.
20938 				 */
20939 				app_limited = CTF_JR_APP_LIMITED;
20940 			} else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
20941 				/* Its the cwnd */
20942 				app_limited = CTF_JR_CWND_LIMITED;
20943 			} else if (IN_FASTRECOVERY(tp->t_flags) &&
20944 				   (rack->rack_no_prr == 0) &&
20945 				   (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
20946 				app_limited = CTF_JR_PRR;
20947 			} else {
20948 				/* Now why here are we not sending? */
20949 #ifdef NOW
20950 #ifdef INVARIANTS
20951 				panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
20952 #endif
20953 #endif
20954 				app_limited = CTF_JR_ASSESSING;
20955 			}
20956 			/*
20957 			 * App limited in some fashion, for our pacing GP
20958 			 * measurements we don't want any gap (even cwnd).
20959 			 * Close  down the measurement window.
20960 			 */
20961 			if (rack_cwnd_block_ends_measure &&
20962 			    ((app_limited == CTF_JR_CWND_LIMITED) ||
20963 			     (app_limited == CTF_JR_PRR))) {
20964 				/*
20965 				 * The reason we are not sending is
20966 				 * the cwnd (or prr). We have been configured
20967 				 * to end the measurement window in
20968 				 * this case.
20969 				 */
20970 				end_window = 1;
20971 			} else if (rack_rwnd_block_ends_measure &&
20972 				   (app_limited == CTF_JR_RWND_LIMITED)) {
20973 				/*
20974 				 * We are rwnd limited and have been
20975 				 * configured to end the measurement
20976 				 * window in this case.
20977 				 */
20978 				end_window = 1;
20979 			} else if (app_limited == CTF_JR_APP_LIMITED) {
20980 				/*
20981 				 * A true application limited period, we have
20982 				 * ran out of data.
20983 				 */
20984 				end_window = 1;
20985 			} else if (app_limited == CTF_JR_ASSESSING) {
20986 				/*
20987 				 * In the assessing case we hit the end of
20988 				 * the if/else and had no known reason
20989 				 * This will panic us under invariants..
20990 				 *
20991 				 * If we get this out in logs we need to
20992 				 * investagate which reason we missed.
20993 				 */
20994 				end_window = 1;
20995 			}
20996 			if (end_window) {
20997 				uint8_t log = 0;
20998 
20999 				/* Adjust the Gput measurement */
21000 				if ((tp->t_flags & TF_GPUTINPROG) &&
21001 				    SEQ_GT(tp->gput_ack, tp->snd_max)) {
21002 					tp->gput_ack = tp->snd_max;
21003 					if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
21004 						/*
21005 						 * There is not enough to measure.
21006 						 */
21007 						tp->t_flags &= ~TF_GPUTINPROG;
21008 						rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
21009 									   rack->r_ctl.rc_gp_srtt /*flex1*/,
21010 									   tp->gput_seq,
21011 									   0, 0, 18, __LINE__, NULL, 0);
21012 					} else
21013 						log = 1;
21014 				}
21015 				/* Mark the last packet has app limited */
21016 				rsm = tqhash_max(rack->r_ctl.tqh);
21017 				if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
21018 					if (rack->r_ctl.rc_app_limited_cnt == 0)
21019 						rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
21020 					else {
21021 						/*
21022 						 * Go out to the end app limited and mark
21023 						 * this new one as next and move the end_appl up
21024 						 * to this guy.
21025 						 */
21026 						if (rack->r_ctl.rc_end_appl)
21027 							rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
21028 						rack->r_ctl.rc_end_appl = rsm;
21029 					}
21030 					rsm->r_flags |= RACK_APP_LIMITED;
21031 					rack->r_ctl.rc_app_limited_cnt++;
21032 				}
21033 				if (log)
21034 					rack_log_pacing_delay_calc(rack,
21035 								   rack->r_ctl.rc_app_limited_cnt, seq,
21036 								   tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
21037 			}
21038 		}
21039 		/* Check if we need to go into persists or not */
21040 		if ((tp->snd_max == tp->snd_una) &&
21041 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
21042 		    sbavail(sb) &&
21043 		    (sbavail(sb) > tp->snd_wnd) &&
21044 		    (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
21045 			/* Yes lets make sure to move to persist before timer-start */
21046 			rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
21047 		}
21048 		rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
21049 		rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
21050 	}
21051 #ifdef NETFLIX_SHARED_CWND
21052 	if ((sbavail(sb) == 0) &&
21053 	    rack->r_ctl.rc_scw) {
21054 		tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
21055 		rack->rack_scwnd_is_idle = 1;
21056 	}
21057 #endif
21058 #ifdef TCP_ACCOUNTING
21059 	if (tot_len_this_send > 0) {
21060 		crtsc = get_cyclecount();
21061 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21062 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
21063 			tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
21064 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
21065 		}
21066 	} else {
21067 		crtsc = get_cyclecount();
21068 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21069 			tp->tcp_cnt_counters[SND_LIMITED]++;
21070 			tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
21071 		}
21072 	}
21073 	sched_unpin();
21074 #endif
21075 	return (0);
21076 
21077 send:
21078 	if ((rack->r_ctl.crte != NULL) &&
21079 	    (rsm == NULL) &&
21080 	    ((rack->rc_hw_nobuf == 1) ||
21081 	     (rack_hw_check_queue && (check_done == 0)))) {
21082 		/*
21083 		 * We only want to do this once with the hw_check_queue,
21084 		 * for the enobuf case we would only do it once if
21085 		 * we come around to again, the flag will be clear.
21086 		 */
21087 		check_done = 1;
21088 		slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz);
21089 		if (slot) {
21090 			rack->r_ctl.rc_agg_delayed = 0;
21091 			rack->r_ctl.rc_agg_early = 0;
21092 			rack->r_early = 0;
21093 			rack->r_late = 0;
21094 			SOCK_SENDBUF_UNLOCK(so);
21095 			goto skip_all_send;
21096 		}
21097 	}
21098 	if (rsm || sack_rxmit)
21099 		counter_u64_add(rack_nfto_resend, 1);
21100 	else
21101 		counter_u64_add(rack_non_fto_send, 1);
21102 	if ((flags & TH_FIN) &&
21103 	    sbavail(sb)) {
21104 		/*
21105 		 * We do not transmit a FIN
21106 		 * with data outstanding. We
21107 		 * need to make it so all data
21108 		 * is acked first.
21109 		 */
21110 		flags &= ~TH_FIN;
21111 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21112 		    (sbused(sb) == (tp->snd_max - tp->snd_una)) &&
21113 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
21114 			/*
21115 			 * Ok less than or right at a MSS is
21116 			 * outstanding. The original FreeBSD stack would
21117 			 * have sent a FIN, which can speed things up for
21118 			 * a transactional application doing a MSG_WAITALL.
21119 			 * To speed things up since we do *not* send a FIN
21120 			 * if data is outstanding, we send a "challenge ack".
21121 			 * The idea behind that is instead of having to have
21122 			 * the peer wait for the delayed-ack timer to run off
21123 			 * we send an ack that makes the peer send us an ack.
21124 			 */
21125 			rack_send_ack_challange(rack);
21126 		}
21127 	}
21128 	/* Enforce stack imposed max seg size if we have one */
21129 	if (pace_max_seg &&
21130 	    (len > pace_max_seg)) {
21131 		mark = 1;
21132 		len = pace_max_seg;
21133 	}
21134 	if ((rsm == NULL) &&
21135 	    (rack->pcm_in_progress == 0) &&
21136 	    (rack->r_ctl.pcm_max_seg > 0) &&
21137 	    (len >= rack->r_ctl.pcm_max_seg)) {
21138 		/* It is large enough for a measurement */
21139 		add_flag |= RACK_IS_PCM;
21140 		rack_log_pcm(rack, 5, len, rack->r_ctl.pcm_max_seg,  add_flag);
21141 	} else if (rack_verbose_logging) {
21142 		rack_log_pcm(rack, 6, len, rack->r_ctl.pcm_max_seg,  add_flag);
21143 	}
21144 
21145 	SOCKBUF_LOCK_ASSERT(sb);
21146 	if (len > 0) {
21147 		if (len >= segsiz)
21148 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
21149 		else
21150 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
21151 	}
21152 	/*
21153 	 * Before ESTABLISHED, force sending of initial options unless TCP
21154 	 * set not to do any options. NOTE: we assume that the IP/TCP header
21155 	 * plus TCP options always fit in a single mbuf, leaving room for a
21156 	 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
21157 	 * + optlen <= MCLBYTES
21158 	 */
21159 	optlen = 0;
21160 #ifdef INET6
21161 	if (isipv6)
21162 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
21163 	else
21164 #endif
21165 		hdrlen = sizeof(struct tcpiphdr);
21166 
21167 	/*
21168 	 * Ok what seq are we sending from. If we have
21169 	 * no rsm to use, then we look at various bits,
21170 	 * if we are putting out a SYN it will be ISS.
21171 	 * If we are retransmitting a FIN it will
21172 	 * be snd_max-1 else its snd_max.
21173 	 */
21174 	if (rsm == NULL) {
21175 		if (flags & TH_SYN)
21176 			rack_seq = tp->iss;
21177 		else if ((flags & TH_FIN) &&
21178 			 (tp->t_flags & TF_SENTFIN))
21179 			rack_seq = tp->snd_max - 1;
21180 		else
21181 			rack_seq = tp->snd_max;
21182 	} else {
21183 		rack_seq = rsm->r_start;
21184 	}
21185 	/*
21186 	 * Compute options for segment. We only have to care about SYN and
21187 	 * established connection segments.  Options for SYN-ACK segments
21188 	 * are handled in TCP syncache.
21189 	 */
21190 	to.to_flags = 0;
21191 	if ((tp->t_flags & TF_NOOPT) == 0) {
21192 		/* Maximum segment size. */
21193 		if (flags & TH_SYN) {
21194 			to.to_mss = tcp_mssopt(&inp->inp_inc);
21195 			if (tp->t_port)
21196 				to.to_mss -= V_tcp_udp_tunneling_overhead;
21197 			to.to_flags |= TOF_MSS;
21198 
21199 			/*
21200 			 * On SYN or SYN|ACK transmits on TFO connections,
21201 			 * only include the TFO option if it is not a
21202 			 * retransmit, as the presence of the TFO option may
21203 			 * have caused the original SYN or SYN|ACK to have
21204 			 * been dropped by a middlebox.
21205 			 */
21206 			if ((tp->t_flags & TF_FASTOPEN) &&
21207 			    (tp->t_rxtshift == 0)) {
21208 				if (tp->t_state == TCPS_SYN_RECEIVED) {
21209 					to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
21210 					to.to_tfo_cookie =
21211 						(u_int8_t *)&tp->t_tfo_cookie.server;
21212 					to.to_flags |= TOF_FASTOPEN;
21213 					wanted_cookie = 1;
21214 				} else if (tp->t_state == TCPS_SYN_SENT) {
21215 					to.to_tfo_len =
21216 						tp->t_tfo_client_cookie_len;
21217 					to.to_tfo_cookie =
21218 						tp->t_tfo_cookie.client;
21219 					to.to_flags |= TOF_FASTOPEN;
21220 					wanted_cookie = 1;
21221 					/*
21222 					 * If we wind up having more data to
21223 					 * send with the SYN than can fit in
21224 					 * one segment, don't send any more
21225 					 * until the SYN|ACK comes back from
21226 					 * the other end.
21227 					 */
21228 					sendalot = 0;
21229 				}
21230 			}
21231 		}
21232 		/* Window scaling. */
21233 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
21234 			to.to_wscale = tp->request_r_scale;
21235 			to.to_flags |= TOF_SCALE;
21236 		}
21237 		/* Timestamps. */
21238 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
21239 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
21240 			uint32_t ts_to_use;
21241 
21242 			if ((rack->r_rcvpath_rtt_up == 1) &&
21243 			    (ms_cts == rack->r_ctl.last_rcv_tstmp_for_rtt)) {
21244 				/*
21245 				 * When we are doing a rcv_rtt probe all
21246 				 * other timestamps use the next msec. This
21247 				 * is safe since our previous ack is in the
21248 				 * air and we will just have a few more
21249 				 * on the next ms. This assures that only
21250 				 * the one ack has the ms_cts that was on
21251 				 * our ack-probe.
21252 				 */
21253 				ts_to_use = ms_cts + 1;
21254 			} else {
21255 				ts_to_use = ms_cts;
21256 			}
21257 			to.to_tsval = ts_to_use + tp->ts_offset;
21258 			to.to_tsecr = tp->ts_recent;
21259 			to.to_flags |= TOF_TS;
21260 			if ((len == 0) &&
21261 			    (TCPS_HAVEESTABLISHED(tp->t_state)) &&
21262 			    ((ms_cts - rack->r_ctl.last_rcv_tstmp_for_rtt) > RCV_PATH_RTT_MS) &&
21263 			    (tp->snd_una == tp->snd_max) &&
21264 			    (flags & TH_ACK) &&
21265 			    (sbavail(sb) == 0) &&
21266 			    (rack->r_ctl.current_round != 0) &&
21267 			    ((flags & (TH_SYN|TH_FIN)) == 0) &&
21268 			    (rack->r_rcvpath_rtt_up == 0)) {
21269 				rack->r_ctl.last_rcv_tstmp_for_rtt = ms_cts;
21270 				rack->r_ctl.last_time_of_arm_rcv = cts;
21271 				rack->r_rcvpath_rtt_up = 1;
21272 				/* Subtract 1 from seq to force a response */
21273 				rack_seq--;
21274 			}
21275 		}
21276 		/* Set receive buffer autosizing timestamp. */
21277 		if (tp->rfbuf_ts == 0 &&
21278 		    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
21279 			tp->rfbuf_ts = ms_cts;
21280 		}
21281 		/* Selective ACK's. */
21282 		if (tp->t_flags & TF_SACK_PERMIT) {
21283 			if (flags & TH_SYN)
21284 				to.to_flags |= TOF_SACKPERM;
21285 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21286 				 tp->rcv_numsacks > 0) {
21287 				to.to_flags |= TOF_SACK;
21288 				to.to_nsacks = tp->rcv_numsacks;
21289 				to.to_sacks = (u_char *)tp->sackblks;
21290 			}
21291 		}
21292 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21293 		/* TCP-MD5 (RFC2385). */
21294 		if (tp->t_flags & TF_SIGNATURE)
21295 			to.to_flags |= TOF_SIGNATURE;
21296 #endif
21297 
21298 		/* Processing the options. */
21299 		hdrlen += optlen = tcp_addoptions(&to, opt);
21300 		/*
21301 		 * If we wanted a TFO option to be added, but it was unable
21302 		 * to fit, ensure no data is sent.
21303 		 */
21304 		if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie &&
21305 		    !(to.to_flags & TOF_FASTOPEN))
21306 			len = 0;
21307 	}
21308 	if (tp->t_port) {
21309 		if (V_tcp_udp_tunneling_port == 0) {
21310 			/* The port was removed?? */
21311 			SOCK_SENDBUF_UNLOCK(so);
21312 #ifdef TCP_ACCOUNTING
21313 			crtsc = get_cyclecount();
21314 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21315 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
21316 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
21317 			}
21318 			sched_unpin();
21319 #endif
21320 			return (EHOSTUNREACH);
21321 		}
21322 		hdrlen += sizeof(struct udphdr);
21323 	}
21324 #ifdef INET6
21325 	if (isipv6)
21326 		ipoptlen = ip6_optlen(inp);
21327 	else
21328 #endif
21329 		if (inp->inp_options)
21330 			ipoptlen = inp->inp_options->m_len -
21331 				offsetof(struct ipoption, ipopt_list);
21332 		else
21333 			ipoptlen = 0;
21334 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21335 	ipoptlen += ipsec_optlen;
21336 #endif
21337 
21338 	/*
21339 	 * Adjust data length if insertion of options will bump the packet
21340 	 * length beyond the t_maxseg length. Clear the FIN bit because we
21341 	 * cut off the tail of the segment.
21342 	 */
21343 	if (len + optlen + ipoptlen > tp->t_maxseg) {
21344 		if (tso) {
21345 			uint32_t if_hw_tsomax;
21346 			uint32_t moff;
21347 			int32_t max_len;
21348 
21349 			/* extract TSO information */
21350 			if_hw_tsomax = tp->t_tsomax;
21351 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
21352 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
21353 			KASSERT(ipoptlen == 0,
21354 				("%s: TSO can't do IP options", __func__));
21355 
21356 			/*
21357 			 * Check if we should limit by maximum payload
21358 			 * length:
21359 			 */
21360 			if (if_hw_tsomax != 0) {
21361 				/* compute maximum TSO length */
21362 				max_len = (if_hw_tsomax - hdrlen -
21363 					   max_linkhdr);
21364 				if (max_len <= 0) {
21365 					len = 0;
21366 				} else if (len > max_len) {
21367 					sendalot = 1;
21368 					len = max_len;
21369 					mark = 2;
21370 				}
21371 			}
21372 			/*
21373 			 * Prevent the last segment from being fractional
21374 			 * unless the send sockbuf can be emptied:
21375 			 */
21376 			max_len = (tp->t_maxseg - optlen);
21377 			if ((sb_offset + len) < sbavail(sb)) {
21378 				moff = len % (u_int)max_len;
21379 				if (moff != 0) {
21380 					mark = 3;
21381 					len -= moff;
21382 				}
21383 			}
21384 			/*
21385 			 * In case there are too many small fragments don't
21386 			 * use TSO:
21387 			 */
21388 			if (len <= max_len) {
21389 				mark = 4;
21390 				tso = 0;
21391 			}
21392 			/*
21393 			 * Send the FIN in a separate segment after the bulk
21394 			 * sending is done. We don't trust the TSO
21395 			 * implementations to clear the FIN flag on all but
21396 			 * the last segment.
21397 			 */
21398 			if (tp->t_flags & TF_NEEDFIN) {
21399 				sendalot = 4;
21400 			}
21401 		} else {
21402 			mark = 5;
21403 			if (optlen + ipoptlen >= tp->t_maxseg) {
21404 				/*
21405 				 * Since we don't have enough space to put
21406 				 * the IP header chain and the TCP header in
21407 				 * one packet as required by RFC 7112, don't
21408 				 * send it. Also ensure that at least one
21409 				 * byte of the payload can be put into the
21410 				 * TCP segment.
21411 				 */
21412 				SOCK_SENDBUF_UNLOCK(so);
21413 				error = EMSGSIZE;
21414 				sack_rxmit = 0;
21415 				goto out;
21416 			}
21417 			len = tp->t_maxseg - optlen - ipoptlen;
21418 			sendalot = 5;
21419 		}
21420 	} else {
21421 		tso = 0;
21422 		mark = 6;
21423 	}
21424 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
21425 		("%s: len > IP_MAXPACKET", __func__));
21426 #ifdef DIAGNOSTIC
21427 #ifdef INET6
21428 	if (max_linkhdr + hdrlen > MCLBYTES)
21429 #else
21430 		if (max_linkhdr + hdrlen > MHLEN)
21431 #endif
21432 			panic("tcphdr too big");
21433 #endif
21434 
21435 	/*
21436 	 * This KASSERT is here to catch edge cases at a well defined place.
21437 	 * Before, those had triggered (random) panic conditions further
21438 	 * down.
21439 	 */
21440 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
21441 	if ((len == 0) &&
21442 	    (flags & TH_FIN) &&
21443 	    (sbused(sb))) {
21444 		/*
21445 		 * We have outstanding data, don't send a fin by itself!.
21446 		 *
21447 		 * Check to see if we need to send a challenge ack.
21448 		 */
21449 		if ((sbused(sb) == (tp->snd_max - tp->snd_una)) &&
21450 		    ((tp->snd_max - tp->snd_una) <= segsiz)) {
21451 			/*
21452 			 * Ok less than or right at a MSS is
21453 			 * outstanding. The original FreeBSD stack would
21454 			 * have sent a FIN, which can speed things up for
21455 			 * a transactional application doing a MSG_WAITALL.
21456 			 * To speed things up since we do *not* send a FIN
21457 			 * if data is outstanding, we send a "challenge ack".
21458 			 * The idea behind that is instead of having to have
21459 			 * the peer wait for the delayed-ack timer to run off
21460 			 * we send an ack that makes the peer send us an ack.
21461 			 */
21462 			rack_send_ack_challange(rack);
21463 		}
21464 		goto just_return;
21465 	}
21466 	/*
21467 	 * Grab a header mbuf, attaching a copy of data to be transmitted,
21468 	 * and initialize the header from the template for sends on this
21469 	 * connection.
21470 	 */
21471 	hw_tls = tp->t_nic_ktls_xmit != 0;
21472 	if (len) {
21473 		uint32_t max_val;
21474 		uint32_t moff;
21475 
21476 		if (pace_max_seg)
21477 			max_val = pace_max_seg;
21478 		else
21479 			max_val = len;
21480 		/*
21481 		 * We allow a limit on sending with hptsi.
21482 		 */
21483 		if (len > max_val) {
21484 			mark = 7;
21485 			len = max_val;
21486 		}
21487 #ifdef INET6
21488 		if (MHLEN < hdrlen + max_linkhdr)
21489 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
21490 		else
21491 #endif
21492 			m = m_gethdr(M_NOWAIT, MT_DATA);
21493 
21494 		if (m == NULL) {
21495 			SOCK_SENDBUF_UNLOCK(so);
21496 			error = ENOBUFS;
21497 			sack_rxmit = 0;
21498 			goto out;
21499 		}
21500 		m->m_data += max_linkhdr;
21501 		m->m_len = hdrlen;
21502 
21503 		/*
21504 		 * Start the m_copy functions from the closest mbuf to the
21505 		 * sb_offset in the socket buffer chain.
21506 		 */
21507 		mb = sbsndptr_noadv(sb, sb_offset, &moff);
21508 		s_mb = mb;
21509 		s_moff = moff;
21510 		if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
21511 			m_copydata(mb, moff, (int)len,
21512 				   mtod(m, caddr_t)+hdrlen);
21513 			/*
21514 			 * If we are not retransmitting advance the
21515 			 * sndptr to help remember the next place in
21516 			 * the sb.
21517 			 */
21518 			if (rsm == NULL)
21519 				sbsndptr_adv(sb, mb, len);
21520 			m->m_len += len;
21521 		} else {
21522 			struct sockbuf *msb;
21523 
21524 			/*
21525 			 * If we are not retransmitting pass in msb so
21526 			 * the socket buffer can be advanced. Otherwise
21527 			 * set it to NULL if its a retransmission since
21528 			 * we don't want to change the sb remembered
21529 			 * location.
21530 			 */
21531 			if (rsm == NULL)
21532 				msb = sb;
21533 			else
21534 				msb = NULL;
21535 			m->m_next = tcp_m_copym(
21536 				mb, moff, &len,
21537 				if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
21538 				((rsm == NULL) ? hw_tls : 0)
21539 #ifdef NETFLIX_COPY_ARGS
21540 				, &s_mb, &s_moff
21541 #endif
21542 				);
21543 			if (len <= (tp->t_maxseg - optlen)) {
21544 				/*
21545 				 * Must have ran out of mbufs for the copy
21546 				 * shorten it to no longer need tso. Lets
21547 				 * not put on sendalot since we are low on
21548 				 * mbufs.
21549 				 */
21550 				tso = 0;
21551 			}
21552 			if (m->m_next == NULL) {
21553 				SOCK_SENDBUF_UNLOCK(so);
21554 				(void)m_free(m);
21555 				error = ENOBUFS;
21556 				sack_rxmit = 0;
21557 				goto out;
21558 			}
21559 		}
21560 		if (sack_rxmit) {
21561 			if (rsm && (rsm->r_flags & RACK_TLP)) {
21562 				/*
21563 				 * TLP should not count in retran count, but
21564 				 * in its own bin
21565 				 */
21566 				counter_u64_add(rack_tlp_retran, 1);
21567 				counter_u64_add(rack_tlp_retran_bytes, len);
21568 			} else {
21569 				tp->t_sndrexmitpack++;
21570 				KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
21571 				KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
21572 			}
21573 #ifdef STATS
21574 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
21575 						 len);
21576 #endif
21577 		} else {
21578 			KMOD_TCPSTAT_INC(tcps_sndpack);
21579 			KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
21580 #ifdef STATS
21581 			stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
21582 						 len);
21583 #endif
21584 		}
21585 		/*
21586 		 * If we're sending everything we've got, set PUSH. (This
21587 		 * will keep happy those implementations which only give
21588 		 * data to the user when a buffer fills or a PUSH comes in.)
21589 		 */
21590 		if (sb_offset + len == sbused(sb) &&
21591 		    sbused(sb) &&
21592 		    !(flags & TH_SYN)) {
21593 			flags |= TH_PUSH;
21594 			add_flag |= RACK_HAD_PUSH;
21595 		}
21596 
21597 		SOCK_SENDBUF_UNLOCK(so);
21598 	} else {
21599 		SOCK_SENDBUF_UNLOCK(so);
21600 		if (tp->t_flags & TF_ACKNOW)
21601 			KMOD_TCPSTAT_INC(tcps_sndacks);
21602 		else if (flags & (TH_SYN | TH_FIN | TH_RST))
21603 			KMOD_TCPSTAT_INC(tcps_sndctrl);
21604 		else
21605 			KMOD_TCPSTAT_INC(tcps_sndwinup);
21606 
21607 		m = m_gethdr(M_NOWAIT, MT_DATA);
21608 		if (m == NULL) {
21609 			error = ENOBUFS;
21610 			sack_rxmit = 0;
21611 			goto out;
21612 		}
21613 #ifdef INET6
21614 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
21615 		    MHLEN >= hdrlen) {
21616 			M_ALIGN(m, hdrlen);
21617 		} else
21618 #endif
21619 			m->m_data += max_linkhdr;
21620 		m->m_len = hdrlen;
21621 	}
21622 	SOCK_SENDBUF_UNLOCK_ASSERT(so);
21623 	m->m_pkthdr.rcvif = (struct ifnet *)0;
21624 #ifdef MAC
21625 	mac_inpcb_create_mbuf(inp, m);
21626 #endif
21627 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
21628 #ifdef INET6
21629 		if (isipv6)
21630 			ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
21631 		else
21632 #endif				/* INET6 */
21633 #ifdef INET
21634 			ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
21635 #endif
21636 		th = rack->r_ctl.fsb.th;
21637 		udp = rack->r_ctl.fsb.udp;
21638 		if (udp) {
21639 #ifdef INET6
21640 			if (isipv6)
21641 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21642 			else
21643 #endif				/* INET6 */
21644 				ulen = hdrlen + len - sizeof(struct ip);
21645 			udp->uh_ulen = htons(ulen);
21646 		}
21647 	} else {
21648 #ifdef INET6
21649 		if (isipv6) {
21650 			ip6 = mtod(m, struct ip6_hdr *);
21651 			if (tp->t_port) {
21652 				udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
21653 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21654 				udp->uh_dport = tp->t_port;
21655 				ulen = hdrlen + len - sizeof(struct ip6_hdr);
21656 				udp->uh_ulen = htons(ulen);
21657 				th = (struct tcphdr *)(udp + 1);
21658 			} else
21659 				th = (struct tcphdr *)(ip6 + 1);
21660 			tcpip_fillheaders(inp, tp->t_port, ip6, th);
21661 		} else
21662 #endif				/* INET6 */
21663 		{
21664 #ifdef INET
21665 			ip = mtod(m, struct ip *);
21666 			if (tp->t_port) {
21667 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
21668 				udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21669 				udp->uh_dport = tp->t_port;
21670 				ulen = hdrlen + len - sizeof(struct ip);
21671 				udp->uh_ulen = htons(ulen);
21672 				th = (struct tcphdr *)(udp + 1);
21673 			} else
21674 				th = (struct tcphdr *)(ip + 1);
21675 			tcpip_fillheaders(inp, tp->t_port, ip, th);
21676 #endif
21677 		}
21678 	}
21679 	/*
21680 	 * If we are starting a connection, send ECN setup SYN packet. If we
21681 	 * are on a retransmit, we may resend those bits a number of times
21682 	 * as per RFC 3168.
21683 	 */
21684 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
21685 		flags |= tcp_ecn_output_syn_sent(tp);
21686 	}
21687 	/* Also handle parallel SYN for ECN */
21688 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
21689 	    (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
21690 		int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
21691 		if ((tp->t_state == TCPS_SYN_RECEIVED) &&
21692 		    (tp->t_flags2 & TF2_ECN_SND_ECE))
21693 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
21694 #ifdef INET6
21695 		if (isipv6) {
21696 			ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
21697 			ip6->ip6_flow |= htonl(ect << 20);
21698 		}
21699 		else
21700 #endif
21701 		{
21702 #ifdef INET
21703 			ip->ip_tos &= ~IPTOS_ECN_MASK;
21704 			ip->ip_tos |= ect;
21705 #endif
21706 		}
21707 	}
21708 	th->th_seq = htonl(rack_seq);
21709 	th->th_ack = htonl(tp->rcv_nxt);
21710 	tcp_set_flags(th, flags);
21711 	/*
21712 	 * Calculate receive window.  Don't shrink window, but avoid silly
21713 	 * window syndrome.
21714 	 * If a RST segment is sent, advertise a window of zero.
21715 	 */
21716 	if (flags & TH_RST) {
21717 		recwin = 0;
21718 	} else {
21719 		if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
21720 		    recwin < (long)segsiz) {
21721 			recwin = 0;
21722 		}
21723 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
21724 		    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
21725 			recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
21726 	}
21727 
21728 	/*
21729 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
21730 	 * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
21731 	 * handled in syncache.
21732 	 */
21733 	if (flags & TH_SYN)
21734 		th->th_win = htons((u_short)
21735 				   (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
21736 	else {
21737 		/* Avoid shrinking window with window scaling. */
21738 		recwin = roundup2(recwin, 1 << tp->rcv_scale);
21739 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
21740 	}
21741 	/*
21742 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
21743 	 * window.  This may cause the remote transmitter to stall.  This
21744 	 * flag tells soreceive() to disable delayed acknowledgements when
21745 	 * draining the buffer.  This can occur if the receiver is
21746 	 * attempting to read more data than can be buffered prior to
21747 	 * transmitting on the connection.
21748 	 */
21749 	if (th->th_win == 0) {
21750 		tp->t_sndzerowin++;
21751 		tp->t_flags |= TF_RXWIN0SENT;
21752 	} else
21753 		tp->t_flags &= ~TF_RXWIN0SENT;
21754 	tp->snd_up = tp->snd_una;	/* drag it along, its deprecated */
21755 	/* Now are we using fsb?, if so copy the template data to the mbuf */
21756 	if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
21757 		uint8_t *cpto;
21758 
21759 		cpto = mtod(m, uint8_t *);
21760 		memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
21761 		/*
21762 		 * We have just copied in:
21763 		 * IP/IP6
21764 		 * <optional udphdr>
21765 		 * tcphdr (no options)
21766 		 *
21767 		 * We need to grab the correct pointers into the mbuf
21768 		 * for both the tcp header, and possibly the udp header (if tunneling).
21769 		 * We do this by using the offset in the copy buffer and adding it
21770 		 * to the mbuf base pointer (cpto).
21771 		 */
21772 #ifdef INET6
21773 		if (isipv6)
21774 			ip6 = mtod(m, struct ip6_hdr *);
21775 		else
21776 #endif				/* INET6 */
21777 #ifdef INET
21778 			ip = mtod(m, struct ip *);
21779 #endif
21780 		th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
21781 		/* If we have a udp header lets set it into the mbuf as well */
21782 		if (udp)
21783 			udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
21784 	}
21785 	if (optlen) {
21786 		bcopy(opt, th + 1, optlen);
21787 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
21788 	}
21789 	/*
21790 	 * Put TCP length in extended header, and then checksum extended
21791 	 * header and data.
21792 	 */
21793 	m->m_pkthdr.len = hdrlen + len;	/* in6_cksum() need this */
21794 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21795 	if (to.to_flags & TOF_SIGNATURE) {
21796 		/*
21797 		 * Calculate MD5 signature and put it into the place
21798 		 * determined before.
21799 		 * NOTE: since TCP options buffer doesn't point into
21800 		 * mbuf's data, calculate offset and use it.
21801 		 */
21802 		if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
21803 						       (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
21804 			/*
21805 			 * Do not send segment if the calculation of MD5
21806 			 * digest has failed.
21807 			 */
21808 			goto out;
21809 		}
21810 	}
21811 #endif
21812 #ifdef INET6
21813 	if (isipv6) {
21814 		/*
21815 		 * ip6_plen is not need to be filled now, and will be filled
21816 		 * in ip6_output.
21817 		 */
21818 		if (tp->t_port) {
21819 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
21820 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21821 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
21822 			th->th_sum = htons(0);
21823 			UDPSTAT_INC(udps_opackets);
21824 		} else {
21825 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
21826 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21827 			th->th_sum = in6_cksum_pseudo(ip6,
21828 						      sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
21829 						      0);
21830 		}
21831 	}
21832 #endif
21833 #if defined(INET6) && defined(INET)
21834 	else
21835 #endif
21836 #ifdef INET
21837 	{
21838 		if (tp->t_port) {
21839 			m->m_pkthdr.csum_flags = CSUM_UDP;
21840 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21841 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
21842 						ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
21843 			th->th_sum = htons(0);
21844 			UDPSTAT_INC(udps_opackets);
21845 		} else {
21846 			m->m_pkthdr.csum_flags = CSUM_TCP;
21847 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21848 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
21849 					       ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
21850 									IPPROTO_TCP + len + optlen));
21851 		}
21852 		/* IP version must be set here for ipv4/ipv6 checking later */
21853 		KASSERT(ip->ip_v == IPVERSION,
21854 			("%s: IP version incorrect: %d", __func__, ip->ip_v));
21855 	}
21856 #endif
21857 	/*
21858 	 * Enable TSO and specify the size of the segments. The TCP pseudo
21859 	 * header checksum is always provided. XXX: Fixme: This is currently
21860 	 * not the case for IPv6.
21861 	 */
21862 	if (tso) {
21863 		/*
21864 		 * Here we must use t_maxseg and the optlen since
21865 		 * the optlen may include SACK's (or DSACK).
21866 		 */
21867 		KASSERT(len > tp->t_maxseg - optlen,
21868 			("%s: len <= tso_segsz", __func__));
21869 		m->m_pkthdr.csum_flags |= CSUM_TSO;
21870 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
21871 	}
21872 	KASSERT(len + hdrlen == m_length(m, NULL),
21873 		("%s: mbuf chain different than expected: %d + %u != %u",
21874 		 __func__, len, hdrlen, m_length(m, NULL)));
21875 
21876 #ifdef TCP_HHOOK
21877 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
21878 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
21879 #endif
21880 	if ((rack->r_ctl.crte != NULL) &&
21881 	    (rack->rc_hw_nobuf == 0) &&
21882 	    tcp_bblogging_on(tp)) {
21883 		rack_log_queue_level(tp, rack, len, &tv, cts);
21884 	}
21885 	/* We're getting ready to send; log now. */
21886 	if (tcp_bblogging_on(rack->rc_tp)) {
21887 		union tcp_log_stackspecific log;
21888 
21889 		memset(&log, 0, sizeof(log));
21890 		log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
21891 		if (rack->rack_no_prr)
21892 			log.u_bbr.flex1 = 0;
21893 		else
21894 			log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
21895 		log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
21896 		log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
21897 		log.u_bbr.flex4 = orig_len;
21898 		/* Save off the early/late values */
21899 		log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
21900 		log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
21901 		log.u_bbr.bw_inuse = rack_get_bw(rack);
21902 		log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
21903 		log.u_bbr.flex8 = 0;
21904 		if (rsm) {
21905 			if (rsm->r_flags & RACK_RWND_COLLAPSED) {
21906 				rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
21907 				counter_u64_add(rack_collapsed_win_rxt, 1);
21908 				counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
21909 			}
21910 			if (doing_tlp)
21911 				log.u_bbr.flex8 = 2;
21912 			else
21913 				log.u_bbr.flex8 = 1;
21914 		} else {
21915 			if (doing_tlp)
21916 				log.u_bbr.flex8 = 3;
21917 		}
21918 		log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
21919 		log.u_bbr.flex7 = mark;
21920 		log.u_bbr.flex7 <<= 8;
21921 		log.u_bbr.flex7 |= pass;
21922 		log.u_bbr.pkts_out = tp->t_maxseg;
21923 		log.u_bbr.timeStamp = cts;
21924 		log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
21925 		if (rsm && (rsm->r_rtr_cnt > 0)) {
21926 			/*
21927 			 * When we have a retransmit we want to log the
21928 			 * burst at send and flight at send from before.
21929 			 */
21930 			log.u_bbr.flex5 = rsm->r_fas;
21931 			log.u_bbr.bbr_substate = rsm->r_bas;
21932 		} else {
21933 			/*
21934 			 * New transmits we log in flex5 the inflight again as
21935 			 * well as the number of segments in our send in the
21936 			 * substate field.
21937 			 */
21938 			log.u_bbr.flex5 = log.u_bbr.inflight;
21939 			log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
21940 		}
21941 		log.u_bbr.lt_epoch = cwnd_to_use;
21942 		log.u_bbr.delivered = sendalot;
21943 		log.u_bbr.rttProp = (uintptr_t)rsm;
21944 		log.u_bbr.pkt_epoch = __LINE__;
21945 		if (rsm) {
21946 			log.u_bbr.delRate = rsm->r_flags;
21947 			log.u_bbr.delRate <<= 31;
21948 			log.u_bbr.delRate |= rack->r_must_retran;
21949 			log.u_bbr.delRate <<= 1;
21950 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21951 		} else {
21952 			log.u_bbr.delRate = rack->r_must_retran;
21953 			log.u_bbr.delRate <<= 1;
21954 			log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21955 		}
21956 		lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
21957 				    len, &log, false, NULL, __func__, __LINE__, &tv);
21958 	} else
21959 		lgb = NULL;
21960 
21961 	/*
21962 	 * Fill in IP length and desired time to live and send to IP level.
21963 	 * There should be a better way to handle ttl and tos; we could keep
21964 	 * them in the template, but need a way to checksum without them.
21965 	 */
21966 	/*
21967 	 * m->m_pkthdr.len should have been set before cksum calcuration,
21968 	 * because in6_cksum() need it.
21969 	 */
21970 #ifdef INET6
21971 	if (isipv6) {
21972 		/*
21973 		 * we separately set hoplimit for every segment, since the
21974 		 * user might want to change the value via setsockopt. Also,
21975 		 * desired default hop limit might be changed via Neighbor
21976 		 * Discovery.
21977 		 */
21978 		rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
21979 
21980 		/*
21981 		 * Set the packet size here for the benefit of DTrace
21982 		 * probes. ip6_output() will set it properly; it's supposed
21983 		 * to include the option header lengths as well.
21984 		 */
21985 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
21986 
21987 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
21988 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
21989 		else
21990 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
21991 
21992 		if (tp->t_state == TCPS_SYN_SENT)
21993 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
21994 
21995 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
21996 		/* TODO: IPv6 IP6TOS_ECT bit on */
21997 		error = ip6_output(m,
21998 				   inp->in6p_outputopts,
21999 				   &inp->inp_route6,
22000 				   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
22001 				   NULL, NULL, inp);
22002 
22003 		if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
22004 			mtu = inp->inp_route6.ro_nh->nh_mtu;
22005 	}
22006 #endif				/* INET6 */
22007 #if defined(INET) && defined(INET6)
22008 	else
22009 #endif
22010 #ifdef INET
22011 	{
22012 		ip->ip_len = htons(m->m_pkthdr.len);
22013 #ifdef INET6
22014 		if (inp->inp_vflag & INP_IPV6PROTO)
22015 			ip->ip_ttl = in6_selecthlim(inp, NULL);
22016 #endif				/* INET6 */
22017 		rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
22018 		/*
22019 		 * If we do path MTU discovery, then we set DF on every
22020 		 * packet. This might not be the best thing to do according
22021 		 * to RFC3390 Section 2. However the tcp hostcache migitates
22022 		 * the problem so it affects only the first tcp connection
22023 		 * with a host.
22024 		 *
22025 		 * NB: Don't set DF on small MTU/MSS to have a safe
22026 		 * fallback.
22027 		 */
22028 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
22029 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
22030 			if (tp->t_port == 0 || len < V_tcp_minmss) {
22031 				ip->ip_off |= htons(IP_DF);
22032 			}
22033 		} else {
22034 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
22035 		}
22036 
22037 		if (tp->t_state == TCPS_SYN_SENT)
22038 			TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
22039 
22040 		TCP_PROBE5(send, NULL, tp, ip, tp, th);
22041 
22042 		error = ip_output(m,
22043 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
22044 				  inp->inp_options,
22045 #else
22046 				  NULL,
22047 #endif
22048 				  &inp->inp_route,
22049 				  ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
22050 				  inp);
22051 		if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
22052 			mtu = inp->inp_route.ro_nh->nh_mtu;
22053 	}
22054 #endif				/* INET */
22055 	if (lgb) {
22056 		lgb->tlb_errno = error;
22057 		lgb = NULL;
22058 	}
22059 
22060 out:
22061 	/*
22062 	 * In transmit state, time the transmission and arrange for the
22063 	 * retransmit.  In persist state, just set snd_max.
22064 	 */
22065 	rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
22066 			rack_to_usec_ts(&tv),
22067 			rsm, add_flag, s_mb, s_moff, hw_tls, segsiz);
22068 	if (error == 0) {
22069 		if (add_flag & RACK_IS_PCM) {
22070 			/* We just launched a PCM */
22071 			/* rrs here log */
22072 			rack->pcm_in_progress = 1;
22073 			rack->pcm_needed = 0;
22074 			rack_log_pcm(rack, 7, len, rack->r_ctl.pcm_max_seg,  add_flag);
22075 		}
22076 		if (rsm == NULL) {
22077 			if (rack->lt_bw_up == 0) {
22078 				rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
22079 				rack->r_ctl.lt_seq = tp->snd_una;
22080 				rack->lt_bw_up = 1;
22081 			} else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) {
22082 				/*
22083 				 * Need to record what we have since we are
22084 				 * approaching seq wrap.
22085 				 */
22086 				uint64_t tmark;
22087 
22088 				rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
22089 				rack->r_ctl.lt_seq = tp->snd_una;
22090 				tmark = tcp_get_u64_usecs(&tv);
22091 				if (tmark > rack->r_ctl.lt_timemark) {
22092 					rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
22093 					rack->r_ctl.lt_timemark = tmark;
22094 				}
22095 			}
22096 		}
22097 		rack->forced_ack = 0;	/* If we send something zap the FA flag */
22098 		counter_u64_add(rack_total_bytes, len);
22099 		tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
22100 		if (rsm && doing_tlp) {
22101 			rack->rc_last_sent_tlp_past_cumack = 0;
22102 			rack->rc_last_sent_tlp_seq_valid = 1;
22103 			rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
22104 			rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
22105 		}
22106 		if (rack->rc_hw_nobuf) {
22107 			rack->rc_hw_nobuf = 0;
22108 			rack->r_ctl.rc_agg_delayed = 0;
22109 			rack->r_early = 0;
22110 			rack->r_late = 0;
22111 			rack->r_ctl.rc_agg_early = 0;
22112 		}
22113 		if (rsm && (doing_tlp == 0)) {
22114 			/* Set we retransmitted */
22115 			rack->rc_gp_saw_rec = 1;
22116 		} else {
22117 			if (cwnd_to_use > tp->snd_ssthresh) {
22118 				/* Set we sent in CA */
22119 				rack->rc_gp_saw_ca = 1;
22120 			} else {
22121 				/* Set we sent in SS */
22122 				rack->rc_gp_saw_ss = 1;
22123 			}
22124 		}
22125 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
22126 		    (tp->t_flags & TF_SACK_PERMIT) &&
22127 		    tp->rcv_numsacks > 0)
22128 			tcp_clean_dsack_blocks(tp);
22129 		tot_len_this_send += len;
22130 		if (len == 0) {
22131 			counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
22132 		} else {
22133 			int idx;
22134 
22135 			idx = (len / segsiz) + 3;
22136 			if (idx >= TCP_MSS_ACCT_ATIMER)
22137 				counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
22138 			else
22139 				counter_u64_add(rack_out_size[idx], 1);
22140 		}
22141 	}
22142 	if ((rack->rack_no_prr == 0) &&
22143 	    sub_from_prr &&
22144 	    (error == 0)) {
22145 		if (rack->r_ctl.rc_prr_sndcnt >= len)
22146 			rack->r_ctl.rc_prr_sndcnt -= len;
22147 		else
22148 			rack->r_ctl.rc_prr_sndcnt = 0;
22149 	}
22150 	sub_from_prr = 0;
22151 	if (doing_tlp) {
22152 		/* Make sure the TLP is added */
22153 		add_flag |= RACK_TLP;
22154 	} else if (rsm) {
22155 		/* If its a resend without TLP then it must not have the flag */
22156 		rsm->r_flags &= ~RACK_TLP;
22157 	}
22158 
22159 
22160 	if ((error == 0) &&
22161 	    (len > 0) &&
22162 	    (tp->snd_una == tp->snd_max))
22163 		rack->r_ctl.rc_tlp_rxt_last_time = cts;
22164 
22165 	{
22166 		/*
22167 		 * This block is not associated with the above error == 0 test.
22168 		 * It is used to advance snd_max if we have a new transmit.
22169 		 */
22170 		tcp_seq startseq = tp->snd_max;
22171 
22172 
22173 		if (rsm && (doing_tlp == 0))
22174 			rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
22175 		if (error)
22176 			/* We don't log or do anything with errors */
22177 			goto nomore;
22178 		if (doing_tlp == 0) {
22179 			if (rsm == NULL) {
22180 				/*
22181 				 * Not a retransmission of some
22182 				 * sort, new data is going out so
22183 				 * clear our TLP count and flag.
22184 				 */
22185 				rack->rc_tlp_in_progress = 0;
22186 				rack->r_ctl.rc_tlp_cnt_out = 0;
22187 			}
22188 		} else {
22189 			/*
22190 			 * We have just sent a TLP, mark that it is true
22191 			 * and make sure our in progress is set so we
22192 			 * continue to check the count.
22193 			 */
22194 			rack->rc_tlp_in_progress = 1;
22195 			rack->r_ctl.rc_tlp_cnt_out++;
22196 		}
22197 		/*
22198 		 * If we are retransmitting we are done, snd_max
22199 		 * does not get updated.
22200 		 */
22201 		if (sack_rxmit)
22202 			goto nomore;
22203 		if ((tp->snd_una == tp->snd_max) && (len > 0)) {
22204 			/*
22205 			 * Update the time we just added data since
22206 			 * nothing was outstanding.
22207 			 */
22208 			rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
22209 			tp->t_acktime = ticks;
22210 		}
22211 		/*
22212 		 * Now for special SYN/FIN handling.
22213 		 */
22214 		if (flags & (TH_SYN | TH_FIN)) {
22215 			if ((flags & TH_SYN) &&
22216 			    ((tp->t_flags & TF_SENTSYN) == 0)) {
22217 				tp->snd_max++;
22218 				tp->t_flags |= TF_SENTSYN;
22219 			}
22220 			if ((flags & TH_FIN) &&
22221 			    ((tp->t_flags & TF_SENTFIN) == 0)) {
22222 				tp->snd_max++;
22223 				tp->t_flags |= TF_SENTFIN;
22224 			}
22225 		}
22226 		tp->snd_max += len;
22227 		if (rack->rc_new_rnd_needed) {
22228 			rack_new_round_starts(tp, rack, tp->snd_max);
22229 		}
22230 		/*
22231 		 * Time this transmission if not a retransmission and
22232 		 * not currently timing anything.
22233 		 * This is only relevant in case of switching back to
22234 		 * the base stack.
22235 		 */
22236 		if (tp->t_rtttime == 0) {
22237 			tp->t_rtttime = ticks;
22238 			tp->t_rtseq = startseq;
22239 			KMOD_TCPSTAT_INC(tcps_segstimed);
22240 		}
22241 		if (len &&
22242 		    ((tp->t_flags & TF_GPUTINPROG) == 0))
22243 			rack_start_gp_measurement(tp, rack, startseq, sb_offset);
22244 		/*
22245 		 * If we are doing FO we need to update the mbuf position and subtract
22246 		 * this happens when the peer sends us duplicate information and
22247 		 * we thus want to send a DSACK.
22248 		 *
22249 		 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
22250 		 * turned off? If not then we are going to echo multiple DSACK blocks
22251 		 * out (with the TSO), which we should not be doing.
22252 		 */
22253 		if (rack->r_fast_output && len) {
22254 			if (rack->r_ctl.fsb.left_to_send > len)
22255 				rack->r_ctl.fsb.left_to_send -= len;
22256 			else
22257 				rack->r_ctl.fsb.left_to_send = 0;
22258 			if (rack->r_ctl.fsb.left_to_send < segsiz)
22259 				rack->r_fast_output = 0;
22260 			if (rack->r_fast_output) {
22261 				rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
22262 				rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
22263 				rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
22264 			}
22265 		}
22266 		if (rack_pcm_blast == 0) {
22267 			if ((orig_len > len) &&
22268 			    (add_flag & RACK_IS_PCM) &&
22269 			    (len < pace_max_seg) &&
22270 			    ((pace_max_seg - len) > segsiz)) {
22271 				/*
22272 				 * We are doing a PCM measurement and we did
22273 				 * not get enough data in the TSO to meet the
22274 				 * burst requirement.
22275 				 */
22276 				uint32_t n_len;
22277 
22278 				n_len = (orig_len - len);
22279 				orig_len -= len;
22280 				pace_max_seg -= len;
22281 				len = n_len;
22282 				sb_offset = tp->snd_max - tp->snd_una;
22283 				/* Re-lock for the next spin */
22284 				SOCK_SENDBUF_LOCK(so);
22285 				goto send;
22286 			}
22287 		} else {
22288 			if ((orig_len > len) &&
22289 			    (add_flag & RACK_IS_PCM) &&
22290 			    ((orig_len - len) > segsiz)) {
22291 				/*
22292 				 * We are doing a PCM measurement and we did
22293 				 * not get enough data in the TSO to meet the
22294 				 * burst requirement.
22295 				 */
22296 				uint32_t n_len;
22297 
22298 				n_len = (orig_len - len);
22299 				orig_len -= len;
22300 				len = n_len;
22301 				sb_offset = tp->snd_max - tp->snd_una;
22302 				/* Re-lock for the next spin */
22303 				SOCK_SENDBUF_LOCK(so);
22304 				goto send;
22305 			}
22306 		}
22307 	}
22308 nomore:
22309 	if (error) {
22310 		rack->r_ctl.rc_agg_delayed = 0;
22311 		rack->r_early = 0;
22312 		rack->r_late = 0;
22313 		rack->r_ctl.rc_agg_early = 0;
22314 		SOCKBUF_UNLOCK_ASSERT(sb);	/* Check gotos. */
22315 		/*
22316 		 * Failures do not advance the seq counter above. For the
22317 		 * case of ENOBUFS we will fall out and retry in 1ms with
22318 		 * the hpts. Everything else will just have to retransmit
22319 		 * with the timer.
22320 		 *
22321 		 * In any case, we do not want to loop around for another
22322 		 * send without a good reason.
22323 		 */
22324 		sendalot = 0;
22325 		switch (error) {
22326 		case EPERM:
22327 		case EACCES:
22328 			tp->t_softerror = error;
22329 #ifdef TCP_ACCOUNTING
22330 			crtsc = get_cyclecount();
22331 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22332 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22333 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22334 			}
22335 			sched_unpin();
22336 #endif
22337 			return (error);
22338 		case ENOBUFS:
22339 			/*
22340 			 * Pace us right away to retry in a some
22341 			 * time
22342 			 */
22343 			if (rack->r_ctl.crte != NULL) {
22344 				tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
22345 				if (tcp_bblogging_on(rack->rc_tp))
22346 					rack_log_queue_level(tp, rack, len, &tv, cts);
22347 			} else
22348 				tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
22349 			slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
22350 			if (rack->rc_enobuf < 0x7f)
22351 				rack->rc_enobuf++;
22352 			if (slot < (10 * HPTS_USEC_IN_MSEC))
22353 				slot = 10 * HPTS_USEC_IN_MSEC;
22354 			if (rack->r_ctl.crte != NULL) {
22355 				counter_u64_add(rack_saw_enobuf_hw, 1);
22356 				tcp_rl_log_enobuf(rack->r_ctl.crte);
22357 			}
22358 			counter_u64_add(rack_saw_enobuf, 1);
22359 			goto enobufs;
22360 		case EMSGSIZE:
22361 			/*
22362 			 * For some reason the interface we used initially
22363 			 * to send segments changed to another or lowered
22364 			 * its MTU. If TSO was active we either got an
22365 			 * interface without TSO capabilits or TSO was
22366 			 * turned off. If we obtained mtu from ip_output()
22367 			 * then update it and try again.
22368 			 */
22369 			if (tso)
22370 				tp->t_flags &= ~TF_TSO;
22371 			if (mtu != 0) {
22372 				int saved_mtu;
22373 
22374 				saved_mtu = tp->t_maxseg;
22375 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
22376 				if (saved_mtu > tp->t_maxseg) {
22377 					goto again;
22378 				}
22379 			}
22380 			slot = 10 * HPTS_USEC_IN_MSEC;
22381 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22382 #ifdef TCP_ACCOUNTING
22383 			crtsc = get_cyclecount();
22384 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22385 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22386 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22387 			}
22388 			sched_unpin();
22389 #endif
22390 			return (error);
22391 		case ENETUNREACH:
22392 			counter_u64_add(rack_saw_enetunreach, 1);
22393 			/* FALLTHROUGH */
22394 		case EHOSTDOWN:
22395 		case EHOSTUNREACH:
22396 		case ENETDOWN:
22397 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
22398 				tp->t_softerror = error;
22399 				error = 0;
22400 			}
22401 			/* FALLTHROUGH */
22402 		default:
22403 			slot = 10 * HPTS_USEC_IN_MSEC;
22404 			rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22405 #ifdef TCP_ACCOUNTING
22406 			crtsc = get_cyclecount();
22407 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22408 				tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22409 				tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22410 			}
22411 			sched_unpin();
22412 #endif
22413 			return (error);
22414 		}
22415 	} else {
22416 		rack->rc_enobuf = 0;
22417 		if (IN_FASTRECOVERY(tp->t_flags) && rsm)
22418 			rack->r_ctl.retran_during_recovery += len;
22419 	}
22420 	KMOD_TCPSTAT_INC(tcps_sndtotal);
22421 
22422 	/*
22423 	 * Data sent (as far as we can tell). If this advertises a larger
22424 	 * window than any other segment, then remember the size of the
22425 	 * advertised window. Any pending ACK has now been sent.
22426 	 */
22427 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
22428 		tp->rcv_adv = tp->rcv_nxt + recwin;
22429 
22430 	tp->last_ack_sent = tp->rcv_nxt;
22431 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
22432 enobufs:
22433 	if (sendalot) {
22434 		/* Do we need to turn off sendalot? */
22435 		if (pace_max_seg &&
22436 		    (tot_len_this_send >= pace_max_seg)) {
22437 			/* We hit our max. */
22438 			sendalot = 0;
22439 		}
22440 	}
22441 	if ((error == 0) && (flags & TH_FIN))
22442 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
22443 	if (flags & TH_RST) {
22444 		/*
22445 		 * We don't send again after sending a RST.
22446 		 */
22447 		slot = 0;
22448 		sendalot = 0;
22449 		if (error == 0)
22450 			tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
22451 	} else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
22452 		/*
22453 		 * Get our pacing rate, if an error
22454 		 * occurred in sending (ENOBUF) we would
22455 		 * hit the else if with slot preset. Other
22456 		 * errors return.
22457 		 */
22458 		slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz, __LINE__);
22459 	}
22460 	/* We have sent clear the flag */
22461 	rack->r_ent_rec_ns = 0;
22462 	if (rack->r_must_retran) {
22463 		if (rsm) {
22464 			rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
22465 			if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
22466 				/*
22467 				 * We have retransmitted all.
22468 				 */
22469 				rack->r_must_retran = 0;
22470 				rack->r_ctl.rc_out_at_rto = 0;
22471 			}
22472 		} else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22473 			/*
22474 			 * Sending new data will also kill
22475 			 * the loop.
22476 			 */
22477 			rack->r_must_retran = 0;
22478 			rack->r_ctl.rc_out_at_rto = 0;
22479 		}
22480 	}
22481 	rack->r_ctl.fsb.recwin = recwin;
22482 	if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
22483 	    SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22484 		/*
22485 		 * We hit an RTO and now have past snd_max at the RTO
22486 		 * clear all the WAS flags.
22487 		 */
22488 		tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
22489 	}
22490 	if (slot) {
22491 		/* set the rack tcb into the slot N */
22492 		if ((error == 0) &&
22493 		    rack_use_rfo &&
22494 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22495 		    (rsm == NULL) &&
22496 		    (ipoptlen == 0) &&
22497 		    rack->r_fsb_inited &&
22498 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22499 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22500 		    (rack->r_must_retran == 0) &&
22501 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22502 		    (len > 0) && (orig_len > 0) &&
22503 		    (orig_len > len) &&
22504 		    ((orig_len - len) >= segsiz) &&
22505 		    ((optlen == 0) ||
22506 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22507 			/* We can send at least one more MSS using our fsb */
22508 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22509 					       segsiz, pace_max_seg, hw_tls, flags);
22510 		} else
22511 			rack->r_fast_output = 0;
22512 		rack_log_fsb(rack, tp, so, flags,
22513 			     ipoptlen, orig_len, len, error,
22514 			     (rsm == NULL), optlen, __LINE__, 2);
22515 	} else if (sendalot) {
22516 		int ret;
22517 
22518 		sack_rxmit = 0;
22519 		if ((error == 0) &&
22520 		    rack_use_rfo &&
22521 		    ((flags & (TH_SYN|TH_FIN)) == 0) &&
22522 		    (rsm == NULL) &&
22523 		    (ipoptlen == 0) &&
22524 		    (rack->r_must_retran == 0) &&
22525 		    rack->r_fsb_inited &&
22526 		    TCPS_HAVEESTABLISHED(tp->t_state) &&
22527 		    ((IN_RECOVERY(tp->t_flags)) == 0) &&
22528 		    ((tp->t_flags & TF_NEEDFIN) == 0) &&
22529 		    (len > 0) && (orig_len > 0) &&
22530 		    (orig_len > len) &&
22531 		    ((orig_len - len) >= segsiz) &&
22532 		    ((optlen == 0) ||
22533 		     ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22534 			/* we can use fast_output for more */
22535 			rack_setup_fast_output(tp, rack, sb, len, orig_len,
22536 					       segsiz, pace_max_seg, hw_tls, flags);
22537 			if (rack->r_fast_output) {
22538 				error = 0;
22539 				ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
22540 				if (ret >= 0)
22541 					return (ret);
22542 			        else if (error)
22543 					goto nomore;
22544 
22545 			}
22546 		}
22547 		goto again;
22548 	}
22549 skip_all_send:
22550 	/* Assure when we leave that snd_nxt will point to top */
22551 	if (SEQ_GT(tp->snd_max, tp->snd_nxt))
22552 		tp->snd_nxt = tp->snd_max;
22553 	rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
22554 #ifdef TCP_ACCOUNTING
22555 	crtsc = get_cyclecount() - ts_val;
22556 	if (tot_len_this_send) {
22557 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22558 			tp->tcp_cnt_counters[SND_OUT_DATA]++;
22559 			tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
22560 			tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
22561 		}
22562 	} else {
22563 		if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22564 			tp->tcp_cnt_counters[SND_OUT_ACK]++;
22565 			tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
22566 		}
22567 	}
22568 	sched_unpin();
22569 #endif
22570 	if (error == ENOBUFS)
22571 		error = 0;
22572 	return (error);
22573 }
22574 
22575 static void
rack_update_seg(struct tcp_rack * rack)22576 rack_update_seg(struct tcp_rack *rack)
22577 {
22578 	uint32_t orig_val;
22579 
22580 	orig_val = rack->r_ctl.rc_pace_max_segs;
22581 	rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
22582 	if (orig_val != rack->r_ctl.rc_pace_max_segs)
22583 		rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
22584 }
22585 
22586 static void
rack_mtu_change(struct tcpcb * tp)22587 rack_mtu_change(struct tcpcb *tp)
22588 {
22589 	/*
22590 	 * The MSS may have changed
22591 	 */
22592 	struct tcp_rack *rack;
22593 	struct rack_sendmap *rsm;
22594 
22595 	rack = (struct tcp_rack *)tp->t_fb_ptr;
22596 	if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
22597 		/*
22598 		 * The MTU has changed we need to resend everything
22599 		 * since all we have sent is lost. We first fix
22600 		 * up the mtu though.
22601 		 */
22602 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
22603 		/* We treat this like a full retransmit timeout without the cwnd adjustment */
22604 		rack_remxt_tmr(tp);
22605 		rack->r_fast_output = 0;
22606 		rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
22607 						rack->r_ctl.rc_sacked);
22608 		rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
22609 		rack->r_must_retran = 1;
22610 		/* Mark all inflight to needing to be rxt'd */
22611 		TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
22612 			rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG);
22613 		}
22614 	}
22615 	sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
22616 	/* We don't use snd_nxt to retransmit */
22617 	tp->snd_nxt = tp->snd_max;
22618 }
22619 
22620 static int
rack_set_dgp(struct tcp_rack * rack)22621 rack_set_dgp(struct tcp_rack *rack)
22622 {
22623 	if (rack->dgp_on == 1)
22624 		return(0);
22625 	if ((rack->use_fixed_rate == 1) &&
22626 	    (rack->rc_always_pace == 1)) {
22627 		/*
22628 		 * We are already pacing another
22629 		 * way.
22630 		 */
22631 		return (EBUSY);
22632 	}
22633 	if (rack->rc_always_pace == 1) {
22634 		rack_remove_pacing(rack);
22635 	}
22636 	if (tcp_incr_dgp_pacing_cnt() == 0)
22637 		return (ENOSPC);
22638 	rack->r_ctl.pacing_method |= RACK_DGP_PACING;
22639 	rack->rc_fillcw_apply_discount = 0;
22640 	rack->dgp_on = 1;
22641 	rack->rc_always_pace = 1;
22642 	rack->rc_pace_dnd = 1;
22643 	rack->use_fixed_rate = 0;
22644 	if (rack->gp_ready)
22645 		rack_set_cc_pacing(rack);
22646 	rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22647 	rack->rack_attempt_hdwr_pace = 0;
22648 	/* rxt settings */
22649 	rack->full_size_rxt = 1;
22650 	rack->shape_rxt_to_pacing_min  = 0;
22651 	/* cmpack=1 */
22652 	rack->r_use_cmp_ack = 1;
22653 	if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
22654 	    rack->r_use_cmp_ack)
22655 		rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22656 	/* scwnd=1 */
22657 	rack->rack_enable_scwnd = 1;
22658 	/* dynamic=100 */
22659 	rack->rc_gp_dyn_mul = 1;
22660 	/* gp_inc_ca */
22661 	rack->r_ctl.rack_per_of_gp_ca = 100;
22662 	/* rrr_conf=3 */
22663 	rack->r_rr_config = 3;
22664 	/* npush=2 */
22665 	rack->r_ctl.rc_no_push_at_mrtt = 2;
22666 	/* fillcw=1 */
22667 	rack->rc_pace_to_cwnd = 1;
22668 	rack->rc_pace_fill_if_rttin_range = 0;
22669 	rack->rtt_limit_mul = 0;
22670 	/* noprr=1 */
22671 	rack->rack_no_prr = 1;
22672 	/* lscwnd=1 */
22673 	rack->r_limit_scw = 1;
22674 	/* gp_inc_rec */
22675 	rack->r_ctl.rack_per_of_gp_rec = 90;
22676 	return (0);
22677 }
22678 
22679 static int
rack_set_profile(struct tcp_rack * rack,int prof)22680 rack_set_profile(struct tcp_rack *rack, int prof)
22681 {
22682 	int err = EINVAL;
22683 	if (prof == 1) {
22684 		/*
22685 		 * Profile 1 is "standard" DGP. It ignores
22686 		 * client buffer level.
22687 		 */
22688 		err = rack_set_dgp(rack);
22689 		if (err)
22690 			return (err);
22691 	} else if (prof == 6) {
22692 		err = rack_set_dgp(rack);
22693 		if (err)
22694 			return (err);
22695 		/*
22696 		 * Profile 6 tweaks DGP so that it will apply to
22697 		 * fill-cw the same settings that profile5 does
22698 		 * to replace DGP. It gets then the max(dgp-rate, fillcw(discounted).
22699 		 */
22700 		rack->rc_fillcw_apply_discount = 1;
22701 	} else if (prof == 0) {
22702 		/* This changes things back to the default settings */
22703 		if (rack->rc_always_pace == 1) {
22704 			rack_remove_pacing(rack);
22705 		} else {
22706 			/* Make sure any stray flags are off */
22707 			rack->dgp_on = 0;
22708 			rack->rc_hybrid_mode = 0;
22709 			rack->use_fixed_rate = 0;
22710 		}
22711 		err = 0;
22712 		if (rack_fill_cw_state)
22713 			rack->rc_pace_to_cwnd = 1;
22714 		else
22715 			rack->rc_pace_to_cwnd = 0;
22716 
22717 		if (rack_pace_every_seg && tcp_can_enable_pacing()) {
22718 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
22719 			rack->rc_always_pace = 1;
22720 			if (rack->rack_hibeta)
22721 				rack_set_cc_pacing(rack);
22722 		} else
22723 			rack->rc_always_pace = 0;
22724 		if (rack_dsack_std_based & 0x1) {
22725 			/* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
22726 			rack->rc_rack_tmr_std_based = 1;
22727 		}
22728 		if (rack_dsack_std_based & 0x2) {
22729 			/* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
22730 			rack->rc_rack_use_dsack = 1;
22731 		}
22732 		if (rack_use_cmp_acks)
22733 			rack->r_use_cmp_ack = 1;
22734 		else
22735 			rack->r_use_cmp_ack = 0;
22736 		if (rack_disable_prr)
22737 			rack->rack_no_prr = 1;
22738 		else
22739 			rack->rack_no_prr = 0;
22740 		if (rack_gp_no_rec_chg)
22741 			rack->rc_gp_no_rec_chg = 1;
22742 		else
22743 			rack->rc_gp_no_rec_chg = 0;
22744 		if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
22745 			rack->r_mbuf_queue = 1;
22746 			if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
22747 				rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22748 			rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22749 		} else {
22750 			rack->r_mbuf_queue = 0;
22751 			rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
22752 		}
22753 		if (rack_enable_shared_cwnd)
22754 			rack->rack_enable_scwnd = 1;
22755 		else
22756 			rack->rack_enable_scwnd = 0;
22757 		if (rack_do_dyn_mul) {
22758 			/* When dynamic adjustment is on CA needs to start at 100% */
22759 			rack->rc_gp_dyn_mul = 1;
22760 			if (rack_do_dyn_mul >= 100)
22761 				rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
22762 		} else {
22763 			rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
22764 			rack->rc_gp_dyn_mul = 0;
22765 		}
22766 		rack->r_rr_config = 0;
22767 		rack->r_ctl.rc_no_push_at_mrtt = 0;
22768 		rack->rc_pace_fill_if_rttin_range = 0;
22769 		rack->rtt_limit_mul = 0;
22770 
22771 		if (rack_enable_hw_pacing)
22772 			rack->rack_hdw_pace_ena = 1;
22773 		else
22774 			rack->rack_hdw_pace_ena = 0;
22775 		if (rack_disable_prr)
22776 			rack->rack_no_prr = 1;
22777 		else
22778 			rack->rack_no_prr = 0;
22779 		if (rack_limits_scwnd)
22780 			rack->r_limit_scw  = 1;
22781 		else
22782 			rack->r_limit_scw  = 0;
22783 		rack_init_retransmit_value(rack, rack_rxt_controls);
22784 		err = 0;
22785 	}
22786 	return (err);
22787 }
22788 
22789 static int
rack_add_deferred_option(struct tcp_rack * rack,int sopt_name,uint64_t loptval)22790 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
22791 {
22792 	struct deferred_opt_list *dol;
22793 
22794 	dol = malloc(sizeof(struct deferred_opt_list),
22795 		     M_TCPDO, M_NOWAIT|M_ZERO);
22796 	if (dol == NULL) {
22797 		/*
22798 		 * No space yikes -- fail out..
22799 		 */
22800 		return (0);
22801 	}
22802 	dol->optname = sopt_name;
22803 	dol->optval = loptval;
22804 	TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
22805 	return (1);
22806 }
22807 
22808 static int
process_hybrid_pacing(struct tcp_rack * rack,struct tcp_hybrid_req * hybrid)22809 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid)
22810 {
22811 #ifdef TCP_REQUEST_TRK
22812 	struct tcp_sendfile_track *sft;
22813 	struct timeval tv;
22814 	tcp_seq seq;
22815 	int err;
22816 
22817 	microuptime(&tv);
22818 
22819 	/* Make sure no fixed rate is on */
22820 	rack->use_fixed_rate = 0;
22821 	rack->r_ctl.rc_fixed_pacing_rate_rec = 0;
22822 	rack->r_ctl.rc_fixed_pacing_rate_ca = 0;
22823 	rack->r_ctl.rc_fixed_pacing_rate_ss = 0;
22824 	/* Now allocate or find our entry that will have these settings */
22825 	sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0);
22826 	if (sft == NULL) {
22827 		rack->rc_tp->tcp_hybrid_error++;
22828 		/* no space, where would it have gone? */
22829 		seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc;
22830 		rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0);
22831 		return (ENOSPC);
22832 	}
22833 	/* mask our internal flags */
22834 	hybrid->hybrid_flags &= TCP_HYBRID_PACING_USER_MASK;
22835 	/* The seq will be snd_una + everything in the buffer */
22836 	seq = sft->start_seq;
22837 	if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) {
22838 		/* Disabling hybrid pacing */
22839 		if (rack->rc_hybrid_mode) {
22840 			rack_set_profile(rack, 0);
22841 			rack->rc_tp->tcp_hybrid_stop++;
22842 		}
22843 		rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0);
22844 		return (0);
22845 	}
22846 	if (rack->dgp_on == 0) {
22847 		/*
22848 		 * If we have not yet turned DGP on, do so
22849 		 * now setting pure DGP mode, no buffer level
22850 		 * response.
22851 		 */
22852 		if ((err = rack_set_profile(rack, 1)) != 0){
22853 			/* Failed to turn pacing on */
22854 			rack->rc_tp->tcp_hybrid_error++;
22855 			rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0);
22856 			return (err);
22857 		}
22858 	}
22859 	/*
22860 	 * Now we must switch to hybrid mode as well which also
22861 	 * means moving to regular pacing.
22862 	 */
22863 	if (rack->rc_hybrid_mode == 0) {
22864 		/* First time */
22865 		if (tcp_can_enable_pacing()) {
22866 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
22867 			rack->rc_hybrid_mode = 1;
22868 		} else {
22869 			return (ENOSPC);
22870 		}
22871 		if (rack->r_ctl.pacing_method & RACK_DGP_PACING) {
22872 			/*
22873 			 * This should be true.
22874 			 */
22875 			tcp_dec_dgp_pacing_cnt();
22876 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
22877 		}
22878 	}
22879 	/* Now set in our flags */
22880 	sft->hybrid_flags = hybrid->hybrid_flags | TCP_HYBRID_PACING_WASSET;
22881 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR)
22882 		sft->cspr = hybrid->cspr;
22883 	else
22884 		sft->cspr = 0;
22885 	if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS)
22886 		sft->hint_maxseg = hybrid->hint_maxseg;
22887 	else
22888 		sft->hint_maxseg = 0;
22889 	rack->rc_tp->tcp_hybrid_start++;
22890 	rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0);
22891 	return (0);
22892 #else
22893 	return (ENOTSUP);
22894 #endif
22895 }
22896 
22897 static int
rack_stack_information(struct tcpcb * tp,struct stack_specific_info * si)22898 rack_stack_information(struct tcpcb *tp, struct stack_specific_info *si)
22899 {
22900 	/* We pulled a SSI info log out what was there */
22901 	si->bytes_transmitted = tp->t_sndbytes;
22902 	si->bytes_retransmitted = tp->t_snd_rxt_bytes;
22903 	return (0);
22904 }
22905 
22906 static int
rack_process_option(struct tcpcb * tp,struct tcp_rack * rack,int sopt_name,uint32_t optval,uint64_t loptval,struct tcp_hybrid_req * hybrid)22907 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
22908 		    uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid)
22909 
22910 {
22911 	struct epoch_tracker et;
22912 	struct sockopt sopt;
22913 	struct cc_newreno_opts opt;
22914 	uint64_t val;
22915 	int error = 0;
22916 	uint16_t ca, ss;
22917 
22918 	switch (sopt_name) {
22919 	case TCP_RACK_SET_RXT_OPTIONS:
22920 		if (optval <= 2) {
22921 			rack_init_retransmit_value(rack, optval);
22922 		} else {
22923 			/*
22924 			 * You must send in 0, 1 or 2 all else is
22925 			 * invalid.
22926 			 */
22927 			error = EINVAL;
22928 		}
22929 		break;
22930 	case TCP_RACK_DSACK_OPT:
22931 		RACK_OPTS_INC(tcp_rack_dsack_opt);
22932 		if (optval & 0x1) {
22933 			rack->rc_rack_tmr_std_based = 1;
22934 		} else {
22935 			rack->rc_rack_tmr_std_based = 0;
22936 		}
22937 		if (optval & 0x2) {
22938 			rack->rc_rack_use_dsack = 1;
22939 		} else {
22940 			rack->rc_rack_use_dsack = 0;
22941 		}
22942 		rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
22943 		break;
22944 	case TCP_RACK_PACING_DIVISOR:
22945 		RACK_OPTS_INC(tcp_rack_pacing_divisor);
22946 		if (optval == 0) {
22947 			rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
22948 		} else {
22949 			if (optval < RL_MIN_DIVISOR)
22950 				rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR;
22951 			else
22952 				rack->r_ctl.pace_len_divisor = optval;
22953 		}
22954 		break;
22955 	case TCP_RACK_HI_BETA:
22956 		RACK_OPTS_INC(tcp_rack_hi_beta);
22957 		if (optval > 0) {
22958 			rack->rack_hibeta = 1;
22959 			if ((optval >= 50) &&
22960 			    (optval <= 100)) {
22961 				/*
22962 				 * User wants to set a custom beta.
22963 				 */
22964 				rack->r_ctl.saved_hibeta = optval;
22965 				if (rack->rc_pacing_cc_set)
22966 					rack_undo_cc_pacing(rack);
22967 				rack->r_ctl.rc_saved_beta = optval;
22968 			}
22969 			if (rack->rc_pacing_cc_set == 0)
22970 				rack_set_cc_pacing(rack);
22971 		} else {
22972 			rack->rack_hibeta = 0;
22973 			if (rack->rc_pacing_cc_set)
22974 				rack_undo_cc_pacing(rack);
22975 		}
22976 		break;
22977 	case TCP_RACK_PACING_BETA:
22978 		error = EINVAL;
22979 		break;
22980 	case TCP_RACK_TIMER_SLOP:
22981 		RACK_OPTS_INC(tcp_rack_timer_slop);
22982 		rack->r_ctl.timer_slop = optval;
22983 		if (rack->rc_tp->t_srtt) {
22984 			/*
22985 			 * If we have an SRTT lets update t_rxtcur
22986 			 * to have the new slop.
22987 			 */
22988 			RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
22989 					   rack_rto_min, rack_rto_max,
22990 					   rack->r_ctl.timer_slop);
22991 		}
22992 		break;
22993 	case TCP_RACK_PACING_BETA_ECN:
22994 		RACK_OPTS_INC(tcp_rack_beta_ecn);
22995 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
22996 			/* This only works for newreno. */
22997 			error = EINVAL;
22998 			break;
22999 		}
23000 		if (rack->rc_pacing_cc_set) {
23001 			/*
23002 			 * Set them into the real CC module
23003 			 * whats in the rack pcb is the old values
23004 			 * to be used on restoral/
23005 			 */
23006 			sopt.sopt_dir = SOPT_SET;
23007 			opt.name = CC_NEWRENO_BETA_ECN;
23008 			opt.val = optval;
23009 			if (CC_ALGO(tp)->ctl_output != NULL)
23010 				error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
23011 			else
23012 				error = ENOENT;
23013 		} else {
23014 			/*
23015 			 * Not pacing yet so set it into our local
23016 			 * rack pcb storage.
23017 			 */
23018 			rack->r_ctl.rc_saved_beta_ecn = optval;
23019 		}
23020 		break;
23021 	case TCP_DEFER_OPTIONS:
23022 		RACK_OPTS_INC(tcp_defer_opt);
23023 		if (optval) {
23024 			if (rack->gp_ready) {
23025 				/* Too late */
23026 				error = EINVAL;
23027 				break;
23028 			}
23029 			rack->defer_options = 1;
23030 		} else
23031 			rack->defer_options = 0;
23032 		break;
23033 	case TCP_RACK_MEASURE_CNT:
23034 		RACK_OPTS_INC(tcp_rack_measure_cnt);
23035 		if (optval && (optval <= 0xff)) {
23036 			rack->r_ctl.req_measurements = optval;
23037 		} else
23038 			error = EINVAL;
23039 		break;
23040 	case TCP_REC_ABC_VAL:
23041 		RACK_OPTS_INC(tcp_rec_abc_val);
23042 		if (optval > 0)
23043 			rack->r_use_labc_for_rec = 1;
23044 		else
23045 			rack->r_use_labc_for_rec = 0;
23046 		break;
23047 	case TCP_RACK_ABC_VAL:
23048 		RACK_OPTS_INC(tcp_rack_abc_val);
23049 		if ((optval > 0) && (optval < 255))
23050 			rack->rc_labc = optval;
23051 		else
23052 			error = EINVAL;
23053 		break;
23054 	case TCP_HDWR_UP_ONLY:
23055 		RACK_OPTS_INC(tcp_pacing_up_only);
23056 		if (optval)
23057 			rack->r_up_only = 1;
23058 		else
23059 			rack->r_up_only = 0;
23060 		break;
23061 	case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
23062 		RACK_OPTS_INC(tcp_fillcw_rate_cap);
23063 		rack->r_ctl.fillcw_cap = loptval;
23064 		break;
23065 	case TCP_PACING_RATE_CAP:
23066 		RACK_OPTS_INC(tcp_pacing_rate_cap);
23067 		if ((rack->dgp_on == 1) &&
23068 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
23069 			/*
23070 			 * If we are doing DGP we need to switch
23071 			 * to using the pacing limit.
23072 			 */
23073 			if (tcp_can_enable_pacing() == 0) {
23074 				error = ENOSPC;
23075 				break;
23076 			}
23077 			/*
23078 			 * Now change up the flags and counts to be correct.
23079 			 */
23080 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
23081 			tcp_dec_dgp_pacing_cnt();
23082 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
23083 		}
23084 		rack->r_ctl.bw_rate_cap = loptval;
23085 		break;
23086 	case TCP_HYBRID_PACING:
23087 		if (hybrid == NULL) {
23088 			error = EINVAL;
23089 			break;
23090 		}
23091 		if (rack->r_ctl.side_chan_dis_mask & HYBRID_DIS_MASK) {
23092 			error = EPERM;
23093 			break;
23094 		}
23095 		error = process_hybrid_pacing(rack, hybrid);
23096 		break;
23097 	case TCP_SIDECHAN_DIS:			/*  URL:scodm */
23098 		if (optval)
23099 			rack->r_ctl.side_chan_dis_mask = optval;
23100 		else
23101 			rack->r_ctl.side_chan_dis_mask = 0;
23102 		break;
23103 	case TCP_RACK_PROFILE:
23104 		RACK_OPTS_INC(tcp_profile);
23105 		error = rack_set_profile(rack, optval);
23106 		break;
23107 	case TCP_USE_CMP_ACKS:
23108 		RACK_OPTS_INC(tcp_use_cmp_acks);
23109 		if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) {
23110 			/* You can't turn it off once its on! */
23111 			error = EINVAL;
23112 		} else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
23113 			rack->r_use_cmp_ack = 1;
23114 			rack->r_mbuf_queue = 1;
23115 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23116 		}
23117 		if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
23118 			tp->t_flags2 |= TF2_MBUF_ACKCMP;
23119 		break;
23120 	case TCP_SHARED_CWND_TIME_LIMIT:
23121 		RACK_OPTS_INC(tcp_lscwnd);
23122 		if (optval)
23123 			rack->r_limit_scw = 1;
23124 		else
23125 			rack->r_limit_scw = 0;
23126 		break;
23127 	case TCP_RACK_DGP_IN_REC:
23128 		error = EINVAL;
23129 		break;
23130  	case TCP_RACK_PACE_TO_FILL:
23131 		RACK_OPTS_INC(tcp_fillcw);
23132 		if (optval == 0)
23133 			rack->rc_pace_to_cwnd = 0;
23134 		else {
23135 			rack->rc_pace_to_cwnd = 1;
23136 		}
23137 		if ((optval >= rack_gp_rtt_maxmul) &&
23138 		    rack_gp_rtt_maxmul &&
23139 		    (optval < 0xf)) {
23140 			rack->rc_pace_fill_if_rttin_range = 1;
23141 			rack->rtt_limit_mul = optval;
23142 		} else {
23143 			rack->rc_pace_fill_if_rttin_range = 0;
23144 			rack->rtt_limit_mul = 0;
23145 		}
23146 		break;
23147 	case TCP_RACK_NO_PUSH_AT_MAX:
23148 		RACK_OPTS_INC(tcp_npush);
23149 		if (optval == 0)
23150 			rack->r_ctl.rc_no_push_at_mrtt = 0;
23151 		else if (optval < 0xff)
23152 			rack->r_ctl.rc_no_push_at_mrtt = optval;
23153 		else
23154 			error = EINVAL;
23155 		break;
23156 	case TCP_SHARED_CWND_ENABLE:
23157 		RACK_OPTS_INC(tcp_rack_scwnd);
23158 		if (optval == 0)
23159 			rack->rack_enable_scwnd = 0;
23160 		else
23161 			rack->rack_enable_scwnd = 1;
23162 		break;
23163 	case TCP_RACK_MBUF_QUEUE:
23164 		/* Now do we use the LRO mbuf-queue feature */
23165 		RACK_OPTS_INC(tcp_rack_mbufq);
23166 		if (optval || rack->r_use_cmp_ack)
23167 			rack->r_mbuf_queue = 1;
23168 		else
23169 			rack->r_mbuf_queue = 0;
23170 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23171 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23172 		else
23173 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23174 		break;
23175 	case TCP_RACK_NONRXT_CFG_RATE:
23176 		RACK_OPTS_INC(tcp_rack_cfg_rate);
23177 		if (optval == 0)
23178 			rack->rack_rec_nonrxt_use_cr = 0;
23179 		else
23180 			rack->rack_rec_nonrxt_use_cr = 1;
23181 		break;
23182 	case TCP_NO_PRR:
23183 		RACK_OPTS_INC(tcp_rack_noprr);
23184 		if (optval == 0)
23185 			rack->rack_no_prr = 0;
23186 		else if (optval == 1)
23187 			rack->rack_no_prr = 1;
23188 		else if (optval == 2)
23189 			rack->no_prr_addback = 1;
23190 		else
23191 			error = EINVAL;
23192 		break;
23193 	case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
23194 		if (optval > 0)
23195 			rack->cspr_is_fcc = 1;
23196 		else
23197 			rack->cspr_is_fcc = 0;
23198 		break;
23199 	case TCP_TIMELY_DYN_ADJ:
23200 		RACK_OPTS_INC(tcp_timely_dyn);
23201 		if (optval == 0)
23202 			rack->rc_gp_dyn_mul = 0;
23203 		else {
23204 			rack->rc_gp_dyn_mul = 1;
23205 			if (optval >= 100) {
23206 				/*
23207 				 * If the user sets something 100 or more
23208 				 * its the gp_ca value.
23209 				 */
23210 				rack->r_ctl.rack_per_of_gp_ca  = optval;
23211 			}
23212 		}
23213 		break;
23214 	case TCP_RACK_DO_DETECTION:
23215 		error = EINVAL;
23216 		break;
23217 	case TCP_RACK_TLP_USE:
23218 		if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
23219 			error = EINVAL;
23220 			break;
23221 		}
23222 		RACK_OPTS_INC(tcp_tlp_use);
23223 		rack->rack_tlp_threshold_use = optval;
23224 		break;
23225 	case TCP_RACK_TLP_REDUCE:
23226 		/* RACK TLP cwnd reduction (bool) */
23227 		RACK_OPTS_INC(tcp_rack_tlp_reduce);
23228 		rack->r_ctl.rc_tlp_cwnd_reduce = optval;
23229 		break;
23230 		/*  Pacing related ones */
23231 	case TCP_RACK_PACE_ALWAYS:
23232 		/*
23233 		 * zero is old rack method, 1 is new
23234 		 * method using a pacing rate.
23235 		 */
23236 		RACK_OPTS_INC(tcp_rack_pace_always);
23237 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23238 			error = EPERM;
23239 			break;
23240 		}
23241 		if (optval > 0) {
23242 			if (rack->rc_always_pace) {
23243 				error = EALREADY;
23244 				break;
23245 			} else if (tcp_can_enable_pacing()) {
23246 				rack->r_ctl.pacing_method |= RACK_REG_PACING;
23247 				rack->rc_always_pace = 1;
23248 				if (rack->rack_hibeta)
23249 					rack_set_cc_pacing(rack);
23250 			}
23251 			else {
23252 				error = ENOSPC;
23253 				break;
23254 			}
23255 		} else {
23256 			if (rack->rc_always_pace == 1) {
23257 				rack_remove_pacing(rack);
23258 			}
23259 		}
23260 		if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23261 			tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23262 		else
23263 			tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23264 		/* A rate may be set irate or other, if so set seg size */
23265 		rack_update_seg(rack);
23266 		break;
23267 	case TCP_BBR_RACK_INIT_RATE:
23268 		RACK_OPTS_INC(tcp_initial_rate);
23269 		val = optval;
23270 		/* Change from kbits per second to bytes per second */
23271 		val *= 1000;
23272 		val /= 8;
23273 		rack->r_ctl.init_rate = val;
23274 		if (rack->rc_always_pace)
23275 			rack_update_seg(rack);
23276 		break;
23277 	case TCP_BBR_IWINTSO:
23278 		error = EINVAL;
23279 		break;
23280 	case TCP_RACK_FORCE_MSEG:
23281 		RACK_OPTS_INC(tcp_rack_force_max_seg);
23282 		if (optval)
23283 			rack->rc_force_max_seg = 1;
23284 		else
23285 			rack->rc_force_max_seg = 0;
23286 		break;
23287 	case TCP_RACK_PACE_MIN_SEG:
23288 		RACK_OPTS_INC(tcp_rack_min_seg);
23289 		rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval);
23290 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23291 		break;
23292 	case TCP_RACK_PACE_MAX_SEG:
23293 		/* Max segments size in a pace in bytes */
23294 		RACK_OPTS_INC(tcp_rack_max_seg);
23295 		if ((rack->dgp_on == 1) &&
23296 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
23297 			/*
23298 			 * If we set a max-seg and are doing DGP then
23299 			 * we now fall under the pacing limits not the
23300 			 * DGP ones.
23301 			 */
23302 			if (tcp_can_enable_pacing() == 0) {
23303 				error = ENOSPC;
23304 				break;
23305 			}
23306 			/*
23307 			 * Now change up the flags and counts to be correct.
23308 			 */
23309 			rack->r_ctl.pacing_method |= RACK_REG_PACING;
23310 			tcp_dec_dgp_pacing_cnt();
23311 			rack->r_ctl.pacing_method &= ~RACK_DGP_PACING;
23312 		}
23313 		if (optval <= MAX_USER_SET_SEG)
23314 			rack->rc_user_set_max_segs = optval;
23315 		else
23316 			rack->rc_user_set_max_segs = MAX_USER_SET_SEG;
23317 		rack_set_pace_segments(tp, rack, __LINE__, NULL);
23318 		break;
23319 	case TCP_RACK_PACE_RATE_REC:
23320 		/* Set the fixed pacing rate in Bytes per second ca */
23321 		RACK_OPTS_INC(tcp_rack_pace_rate_rec);
23322 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23323 			error = EPERM;
23324 			break;
23325 		}
23326 		if (rack->dgp_on) {
23327 			/*
23328 			 * We are already pacing another
23329 			 * way.
23330 			 */
23331 			error = EBUSY;
23332 			break;
23333 		}
23334 		rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23335 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23336 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23337 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23338 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23339 		rack->use_fixed_rate = 1;
23340 		if (rack->rack_hibeta)
23341 			rack_set_cc_pacing(rack);
23342 		rack_log_pacing_delay_calc(rack,
23343 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23344 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23345 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23346 					   __LINE__, NULL,0);
23347 		break;
23348 
23349 	case TCP_RACK_PACE_RATE_SS:
23350 		/* Set the fixed pacing rate in Bytes per second ca */
23351 		RACK_OPTS_INC(tcp_rack_pace_rate_ss);
23352 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23353 			error = EPERM;
23354 			break;
23355 		}
23356 		if (rack->dgp_on) {
23357 			/*
23358 			 * We are already pacing another
23359 			 * way.
23360 			 */
23361 			error = EBUSY;
23362 			break;
23363 		}
23364 		rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23365 		if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23366 			rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23367 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23368 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23369 		rack->use_fixed_rate = 1;
23370 		if (rack->rack_hibeta)
23371 			rack_set_cc_pacing(rack);
23372 		rack_log_pacing_delay_calc(rack,
23373 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23374 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23375 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23376 					   __LINE__, NULL, 0);
23377 		break;
23378 
23379 	case TCP_RACK_PACE_RATE_CA:
23380 		/* Set the fixed pacing rate in Bytes per second ca */
23381 		RACK_OPTS_INC(tcp_rack_pace_rate_ca);
23382 		if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) {
23383 			error = EPERM;
23384 			break;
23385 		}
23386 		if (rack->dgp_on) {
23387 			/*
23388 			 * We are already pacing another
23389 			 * way.
23390 			 */
23391 			error = EBUSY;
23392 			break;
23393 		}
23394 		rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23395 		if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23396 			rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23397 		if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23398 			rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23399 		rack->use_fixed_rate = 1;
23400 		if (rack->rack_hibeta)
23401 			rack_set_cc_pacing(rack);
23402 		rack_log_pacing_delay_calc(rack,
23403 					   rack->r_ctl.rc_fixed_pacing_rate_ss,
23404 					   rack->r_ctl.rc_fixed_pacing_rate_ca,
23405 					   rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23406 					   __LINE__, NULL, 0);
23407 		break;
23408 	case TCP_RACK_GP_INCREASE_REC:
23409 		RACK_OPTS_INC(tcp_gp_inc_rec);
23410 		rack->r_ctl.rack_per_of_gp_rec = optval;
23411 		rack_log_pacing_delay_calc(rack,
23412 					   rack->r_ctl.rack_per_of_gp_ss,
23413 					   rack->r_ctl.rack_per_of_gp_ca,
23414 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23415 					   __LINE__, NULL, 0);
23416 		break;
23417 	case TCP_RACK_GP_INCREASE_CA:
23418 		RACK_OPTS_INC(tcp_gp_inc_ca);
23419 		ca = optval;
23420 		if (ca < 100) {
23421 			/*
23422 			 * We don't allow any reduction
23423 			 * over the GP b/w.
23424 			 */
23425 			error = EINVAL;
23426 			break;
23427 		}
23428 		rack->r_ctl.rack_per_of_gp_ca = ca;
23429 		rack_log_pacing_delay_calc(rack,
23430 					   rack->r_ctl.rack_per_of_gp_ss,
23431 					   rack->r_ctl.rack_per_of_gp_ca,
23432 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23433 					   __LINE__, NULL, 0);
23434 		break;
23435 	case TCP_RACK_GP_INCREASE_SS:
23436 		RACK_OPTS_INC(tcp_gp_inc_ss);
23437 		ss = optval;
23438 		if (ss < 100) {
23439 			/*
23440 			 * We don't allow any reduction
23441 			 * over the GP b/w.
23442 			 */
23443 			error = EINVAL;
23444 			break;
23445 		}
23446 		rack->r_ctl.rack_per_of_gp_ss = ss;
23447 		rack_log_pacing_delay_calc(rack,
23448 					   rack->r_ctl.rack_per_of_gp_ss,
23449 					   rack->r_ctl.rack_per_of_gp_ca,
23450 					   rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23451 					   __LINE__, NULL, 0);
23452 		break;
23453 	case TCP_RACK_RR_CONF:
23454 		RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
23455 		if (optval && optval <= 3)
23456 			rack->r_rr_config = optval;
23457 		else
23458 			rack->r_rr_config = 0;
23459 		break;
23460 	case TCP_PACING_DND:			/*  URL:dnd */
23461 		if (optval > 0)
23462 			rack->rc_pace_dnd = 1;
23463 		else
23464 			rack->rc_pace_dnd = 0;
23465 		break;
23466 	case TCP_HDWR_RATE_CAP:
23467 		RACK_OPTS_INC(tcp_hdwr_rate_cap);
23468 		if (optval) {
23469 			if (rack->r_rack_hw_rate_caps == 0)
23470 				rack->r_rack_hw_rate_caps = 1;
23471 			else
23472 				error = EALREADY;
23473 		} else {
23474 			rack->r_rack_hw_rate_caps = 0;
23475 		}
23476 		break;
23477 	case TCP_DGP_UPPER_BOUNDS:
23478 	{
23479 		uint8_t val;
23480 		val = optval & 0x0000ff;
23481 		rack->r_ctl.rack_per_upper_bound_ca = val;
23482 		val = (optval >> 16) & 0x0000ff;
23483 		rack->r_ctl.rack_per_upper_bound_ss = val;
23484 		break;
23485 	}
23486 	case TCP_SS_EEXIT:			/*  URL:eexit */
23487 		if (optval > 0) {
23488 			rack->r_ctl.gp_rnd_thresh =  optval & 0x0ff;
23489 			if (optval & 0x10000) {
23490 				rack->r_ctl.gate_to_fs = 1;
23491 			} else {
23492 				rack->r_ctl.gate_to_fs = 0;
23493 			}
23494 			if (optval & 0x20000) {
23495 				rack->r_ctl.use_gp_not_last = 1;
23496 			} else {
23497 				rack->r_ctl.use_gp_not_last = 0;
23498 			}
23499 			if (optval & 0xfffc0000) {
23500 				uint32_t v;
23501 
23502 				v = (optval >> 18) & 0x00003fff;
23503 				if (v >= 1000)
23504 					rack->r_ctl.gp_gain_req = v;
23505 			}
23506 		} else {
23507 			/* We do not do ss early exit at all */
23508 			rack->rc_initial_ss_comp = 1;
23509 			rack->r_ctl.gp_rnd_thresh = 0;
23510 		}
23511 		break;
23512 	case TCP_RACK_SPLIT_LIMIT:
23513 		RACK_OPTS_INC(tcp_split_limit);
23514 		rack->r_ctl.rc_split_limit = optval;
23515 		break;
23516 	case TCP_BBR_HDWR_PACE:
23517 		RACK_OPTS_INC(tcp_hdwr_pacing);
23518 		if (optval){
23519 			if (rack->rack_hdrw_pacing == 0) {
23520 				rack->rack_hdw_pace_ena = 1;
23521 				rack->rack_attempt_hdwr_pace = 0;
23522 			} else
23523 				error = EALREADY;
23524 		} else {
23525 			rack->rack_hdw_pace_ena = 0;
23526 #ifdef RATELIMIT
23527 			if (rack->r_ctl.crte != NULL) {
23528 				rack->rack_hdrw_pacing = 0;
23529 				rack->rack_attempt_hdwr_pace = 0;
23530 				tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
23531 				rack->r_ctl.crte = NULL;
23532 			}
23533 #endif
23534 		}
23535 		break;
23536 		/*  End Pacing related ones */
23537 	case TCP_RACK_PRR_SENDALOT:
23538 		/* Allow PRR to send more than one seg */
23539 		RACK_OPTS_INC(tcp_rack_prr_sendalot);
23540 		rack->r_ctl.rc_prr_sendalot = optval;
23541 		break;
23542 	case TCP_RACK_MIN_TO:
23543 		/* Minimum time between rack t-o's in ms */
23544 		RACK_OPTS_INC(tcp_rack_min_to);
23545 		rack->r_ctl.rc_min_to = optval;
23546 		break;
23547 	case TCP_RACK_EARLY_SEG:
23548 		/* If early recovery max segments */
23549 		RACK_OPTS_INC(tcp_rack_early_seg);
23550 		rack->r_ctl.rc_early_recovery_segs = optval;
23551 		break;
23552 	case TCP_RACK_ENABLE_HYSTART:
23553 	{
23554 		if (optval) {
23555 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23556 			if (rack_do_hystart > RACK_HYSTART_ON)
23557 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23558 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23559 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23560 		} else {
23561 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23562 		}
23563 	}
23564 	break;
23565 	case TCP_RACK_REORD_THRESH:
23566 		/* RACK reorder threshold (shift amount) */
23567 		RACK_OPTS_INC(tcp_rack_reord_thresh);
23568 		if ((optval > 0) && (optval < 31))
23569 			rack->r_ctl.rc_reorder_shift = optval;
23570 		else
23571 			error = EINVAL;
23572 		break;
23573 	case TCP_RACK_REORD_FADE:
23574 		/* Does reordering fade after ms time */
23575 		RACK_OPTS_INC(tcp_rack_reord_fade);
23576 		rack->r_ctl.rc_reorder_fade = optval;
23577 		break;
23578 	case TCP_RACK_TLP_THRESH:
23579 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
23580 		RACK_OPTS_INC(tcp_rack_tlp_thresh);
23581 		if (optval)
23582 			rack->r_ctl.rc_tlp_threshold = optval;
23583 		else
23584 			error = EINVAL;
23585 		break;
23586 	case TCP_BBR_USE_RACK_RR:
23587 		RACK_OPTS_INC(tcp_rack_rr);
23588 		if (optval)
23589 			rack->use_rack_rr = 1;
23590 		else
23591 			rack->use_rack_rr = 0;
23592 		break;
23593 	case TCP_RACK_PKT_DELAY:
23594 		/* RACK added ms i.e. rack-rtt + reord + N */
23595 		RACK_OPTS_INC(tcp_rack_pkt_delay);
23596 		rack->r_ctl.rc_pkt_delay = optval;
23597 		break;
23598 	case TCP_DELACK:
23599 		RACK_OPTS_INC(tcp_rack_delayed_ack);
23600 		if (optval == 0)
23601 			tp->t_delayed_ack = 0;
23602 		else
23603 			tp->t_delayed_ack = 1;
23604 		if (tp->t_flags & TF_DELACK) {
23605 			tp->t_flags &= ~TF_DELACK;
23606 			tp->t_flags |= TF_ACKNOW;
23607 			NET_EPOCH_ENTER(et);
23608 			rack_output(tp);
23609 			NET_EPOCH_EXIT(et);
23610 		}
23611 		break;
23612 
23613 	case TCP_BBR_RACK_RTT_USE:
23614 		RACK_OPTS_INC(tcp_rack_rtt_use);
23615 		if ((optval != USE_RTT_HIGH) &&
23616 		    (optval != USE_RTT_LOW) &&
23617 		    (optval != USE_RTT_AVG))
23618 			error = EINVAL;
23619 		else
23620 			rack->r_ctl.rc_rate_sample_method = optval;
23621 		break;
23622 	case TCP_HONOR_HPTS_MIN:
23623 		RACK_OPTS_INC(tcp_honor_hpts);
23624 		if (optval) {
23625 			rack->r_use_hpts_min = 1;
23626 			/*
23627 			 * Must be between 2 - 80% to be a reduction else
23628 			 * we keep the default (10%).
23629 			 */
23630 			if ((optval > 1) && (optval <= 80)) {
23631 				rack->r_ctl.max_reduction = optval;
23632 			}
23633 		} else
23634 			rack->r_use_hpts_min = 0;
23635 		break;
23636 	case TCP_REC_IS_DYN:			/*  URL:dynrec */
23637 		RACK_OPTS_INC(tcp_dyn_rec);
23638 		if (optval)
23639 			rack->rc_gp_no_rec_chg = 1;
23640 		else
23641 			rack->rc_gp_no_rec_chg = 0;
23642 		break;
23643 	case TCP_NO_TIMELY:
23644 		RACK_OPTS_INC(tcp_notimely);
23645 		if (optval) {
23646 			rack->rc_skip_timely = 1;
23647 			rack->r_ctl.rack_per_of_gp_rec = 90;
23648 			rack->r_ctl.rack_per_of_gp_ca = 100;
23649 			rack->r_ctl.rack_per_of_gp_ss = 250;
23650 		} else {
23651 			rack->rc_skip_timely = 0;
23652 		}
23653 		break;
23654 	case TCP_GP_USE_LTBW:
23655 		if (optval == 0) {
23656 			rack->use_lesser_lt_bw = 0;
23657 			rack->dis_lt_bw = 1;
23658 		} else if (optval == 1) {
23659 			rack->use_lesser_lt_bw = 1;
23660 			rack->dis_lt_bw = 0;
23661 		} else if (optval == 2) {
23662 			rack->use_lesser_lt_bw = 0;
23663 			rack->dis_lt_bw = 0;
23664 		}
23665 		break;
23666 	case TCP_DATA_AFTER_CLOSE:
23667 		RACK_OPTS_INC(tcp_data_after_close);
23668 		if (optval)
23669 			rack->rc_allow_data_af_clo = 1;
23670 		else
23671 			rack->rc_allow_data_af_clo = 0;
23672 		break;
23673 	default:
23674 		break;
23675 	}
23676 	tcp_log_socket_option(tp, sopt_name, optval, error);
23677 	return (error);
23678 }
23679 
23680 static void
rack_inherit(struct tcpcb * tp,struct inpcb * parent)23681 rack_inherit(struct tcpcb *tp, struct inpcb *parent)
23682 {
23683 	/*
23684 	 * A new connection has been created (tp) and
23685 	 * the parent is the inpcb given. We want to
23686 	 * apply a read-lock to the parent (we are already
23687 	 * holding a write lock on the tp) and copy anything
23688 	 * out of the rack specific data as long as its tfb is
23689 	 * the same as ours i.e. we are the same stack. Otherwise
23690 	 * we just return.
23691 	 */
23692 	struct tcpcb *par;
23693 	struct tcp_rack *dest, *src;
23694 	int cnt = 0;
23695 
23696 	par = intotcpcb(parent);
23697 	if (par->t_fb != tp->t_fb) {
23698 		/* Not the same stack */
23699 		tcp_log_socket_option(tp, 0, 0, 1);
23700 		return;
23701 	}
23702 	/* Ok if we reach here lets setup the two rack pointers */
23703 	dest = (struct tcp_rack *)tp->t_fb_ptr;
23704 	src = (struct tcp_rack *)par->t_fb_ptr;
23705 	if ((src == NULL) || (dest == NULL)) {
23706 		/* Huh? */
23707 		tcp_log_socket_option(tp, 0, 0, 2);
23708 		return;
23709 	}
23710 	/* Now copy out anything we wish to inherit i.e. things in socket-options */
23711 	/* TCP_RACK_PROFILE we can't know but we can set DGP if its on */
23712 	if ((src->dgp_on) && (dest->dgp_on == 0)) {
23713 		/* Profile 1 had to be set via sock opt */
23714 		rack_set_dgp(dest);
23715 		cnt++;
23716 	}
23717 	/* TCP_RACK_SET_RXT_OPTIONS */
23718 	if (dest->full_size_rxt != src->full_size_rxt) {
23719 		dest->full_size_rxt = src->full_size_rxt;
23720 		cnt++;
23721 	}
23722 	if (dest->shape_rxt_to_pacing_min  != src->shape_rxt_to_pacing_min) {
23723 		dest->shape_rxt_to_pacing_min = src->shape_rxt_to_pacing_min;
23724 		cnt++;
23725 	}
23726 	/* TCP_RACK_DSACK_OPT */
23727 	if (dest->rc_rack_tmr_std_based != src->rc_rack_tmr_std_based) {
23728 		dest->rc_rack_tmr_std_based = src->rc_rack_tmr_std_based;
23729 		cnt++;
23730 	}
23731 	if (dest->rc_rack_use_dsack != src->rc_rack_use_dsack) {
23732 		dest->rc_rack_use_dsack = src->rc_rack_use_dsack;
23733 		cnt++;
23734 	}
23735 	/* TCP_RACK_PACING_DIVISOR */
23736 	if (dest->r_ctl.pace_len_divisor != src->r_ctl.pace_len_divisor) {
23737 		dest->r_ctl.pace_len_divisor = src->r_ctl.pace_len_divisor;
23738 		cnt++;
23739 	}
23740 	/* TCP_RACK_HI_BETA */
23741 	if (src->rack_hibeta != dest->rack_hibeta) {
23742 		cnt++;
23743 		if (src->rack_hibeta) {
23744 			dest->r_ctl.rc_saved_beta = src->r_ctl.rc_saved_beta;
23745 			dest->rack_hibeta = 1;
23746 		} else {
23747 			dest->rack_hibeta = 0;
23748 		}
23749 	}
23750 	/* TCP_RACK_TIMER_SLOP */
23751 	if (dest->r_ctl.timer_slop != src->r_ctl.timer_slop) {
23752 		dest->r_ctl.timer_slop = src->r_ctl.timer_slop;
23753 		cnt++;
23754 	}
23755 	/* TCP_RACK_PACING_BETA_ECN */
23756 	if (dest->r_ctl.rc_saved_beta_ecn != src->r_ctl.rc_saved_beta_ecn) {
23757 		dest->r_ctl.rc_saved_beta_ecn = src->r_ctl.rc_saved_beta_ecn;
23758 		cnt++;
23759 	}
23760 	/* We do not do TCP_DEFER_OPTIONS */
23761 	/* TCP_RACK_MEASURE_CNT */
23762 	if (dest->r_ctl.req_measurements != src->r_ctl.req_measurements) {
23763 		dest->r_ctl.req_measurements = src->r_ctl.req_measurements;
23764 		cnt++;
23765 	}
23766 	/* TCP_HDWR_UP_ONLY */
23767 	if (dest->r_up_only != src->r_up_only) {
23768 		dest->r_up_only = src->r_up_only;
23769 		cnt++;
23770 	}
23771 	/* TCP_FILLCW_RATE_CAP */
23772 	if (dest->r_ctl.fillcw_cap != src->r_ctl.fillcw_cap) {
23773 		dest->r_ctl.fillcw_cap = src->r_ctl.fillcw_cap;
23774 		cnt++;
23775 	}
23776 	/* TCP_PACING_RATE_CAP */
23777 	if (dest->r_ctl.bw_rate_cap != src->r_ctl.bw_rate_cap) {
23778 		dest->r_ctl.bw_rate_cap = src->r_ctl.bw_rate_cap;
23779 		cnt++;
23780 	}
23781 	/* A listener can't set TCP_HYBRID_PACING */
23782 	/* TCP_SIDECHAN_DIS */
23783 	if (dest->r_ctl.side_chan_dis_mask != src->r_ctl.side_chan_dis_mask) {
23784 		dest->r_ctl.side_chan_dis_mask = src->r_ctl.side_chan_dis_mask;
23785 		cnt++;
23786 	}
23787 	/* TCP_SHARED_CWND_TIME_LIMIT */
23788 	if (dest->r_limit_scw != src->r_limit_scw) {
23789 		dest->r_limit_scw = src->r_limit_scw;
23790 		cnt++;
23791 	}
23792 	/* TCP_RACK_PACE_TO_FILL */
23793 	if (dest->rc_pace_to_cwnd != src->rc_pace_to_cwnd) {
23794 		dest->rc_pace_to_cwnd = src->rc_pace_to_cwnd;
23795 		cnt++;
23796 	}
23797 	if (dest->rc_pace_fill_if_rttin_range != src->rc_pace_fill_if_rttin_range) {
23798 		dest->rc_pace_fill_if_rttin_range = src->rc_pace_fill_if_rttin_range;
23799 		cnt++;
23800 	}
23801 	if (dest->rtt_limit_mul != src->rtt_limit_mul) {
23802 		dest->rtt_limit_mul = src->rtt_limit_mul;
23803 		cnt++;
23804 	}
23805 	/* TCP_RACK_NO_PUSH_AT_MAX */
23806 	if (dest->r_ctl.rc_no_push_at_mrtt != src->r_ctl.rc_no_push_at_mrtt) {
23807 		dest->r_ctl.rc_no_push_at_mrtt = src->r_ctl.rc_no_push_at_mrtt;
23808 		cnt++;
23809 	}
23810 	/* TCP_SHARED_CWND_ENABLE */
23811 	if (dest->rack_enable_scwnd != src->rack_enable_scwnd) {
23812 		dest->rack_enable_scwnd = src->rack_enable_scwnd;
23813 		cnt++;
23814 	}
23815 	/* TCP_USE_CMP_ACKS */
23816 	if (dest->r_use_cmp_ack != src->r_use_cmp_ack) {
23817 		dest->r_use_cmp_ack = src->r_use_cmp_ack;
23818 		cnt++;
23819 	}
23820 
23821 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
23822 		dest->r_mbuf_queue = src->r_mbuf_queue;
23823 		cnt++;
23824 	}
23825 	/* TCP_RACK_MBUF_QUEUE */
23826 	if (dest->r_mbuf_queue != src->r_mbuf_queue) {
23827 		dest->r_mbuf_queue = src->r_mbuf_queue;
23828 		cnt++;
23829 	}
23830 	if  (dest->r_mbuf_queue || dest->rc_always_pace || dest->r_use_cmp_ack) {
23831 		tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23832 	} else {
23833 		tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23834 	}
23835 	if (dest->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) {
23836 		tp->t_flags2 |= TF2_MBUF_ACKCMP;
23837 	}
23838 	/* TCP_RACK_NONRXT_CFG_RATE */
23839 	if (dest->rack_rec_nonrxt_use_cr != src->rack_rec_nonrxt_use_cr) {
23840 		dest->rack_rec_nonrxt_use_cr = src->rack_rec_nonrxt_use_cr;
23841 		cnt++;
23842 	}
23843 	/* TCP_NO_PRR */
23844 	if (dest->rack_no_prr != src->rack_no_prr) {
23845 		dest->rack_no_prr = src->rack_no_prr;
23846 		cnt++;
23847 	}
23848 	if (dest->no_prr_addback != src->no_prr_addback) {
23849 		dest->no_prr_addback = src->no_prr_addback;
23850 		cnt++;
23851 	}
23852 	/* RACK_CSPR_IS_FCC */
23853 	if (dest->cspr_is_fcc != src->cspr_is_fcc) {
23854 		dest->cspr_is_fcc = src->cspr_is_fcc;
23855 		cnt++;
23856 	}
23857 	/* TCP_TIMELY_DYN_ADJ */
23858 	if (dest->rc_gp_dyn_mul != src->rc_gp_dyn_mul) {
23859 		dest->rc_gp_dyn_mul = src->rc_gp_dyn_mul;
23860 		cnt++;
23861 	}
23862 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
23863 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
23864 		cnt++;
23865 	}
23866 	/* TCP_RACK_TLP_USE */
23867 	if (dest->rack_tlp_threshold_use != src->rack_tlp_threshold_use) {
23868 		dest->rack_tlp_threshold_use = src->rack_tlp_threshold_use;
23869 		cnt++;
23870 	}
23871 	/* we don't allow inheritence of TCP_RACK_PACE_ALWAYS */
23872 	/* TCP_BBR_RACK_INIT_RATE */
23873 	if (dest->r_ctl.init_rate != src->r_ctl.init_rate) {
23874 		dest->r_ctl.init_rate = src->r_ctl.init_rate;
23875 		cnt++;
23876 	}
23877 	/* TCP_RACK_FORCE_MSEG */
23878 	if (dest->rc_force_max_seg != src->rc_force_max_seg) {
23879 		dest->rc_force_max_seg = src->rc_force_max_seg;
23880 		cnt++;
23881 	}
23882 	/* TCP_RACK_PACE_MIN_SEG */
23883 	if (dest->r_ctl.rc_user_set_min_segs != src->r_ctl.rc_user_set_min_segs) {
23884 		dest->r_ctl.rc_user_set_min_segs = src->r_ctl.rc_user_set_min_segs;
23885 		cnt++;
23886 	}
23887 	/* we don't allow TCP_RACK_PACE_MAX_SEG */
23888 	/* TCP_RACK_PACE_RATE_REC, TCP_RACK_PACE_RATE_SS,  TCP_RACK_PACE_RATE_CA */
23889 	if (dest->r_ctl.rc_fixed_pacing_rate_ca != src->r_ctl.rc_fixed_pacing_rate_ca) {
23890 		dest->r_ctl.rc_fixed_pacing_rate_ca = src->r_ctl.rc_fixed_pacing_rate_ca;
23891 		cnt++;
23892 	}
23893 	if (dest->r_ctl.rc_fixed_pacing_rate_ss != src->r_ctl.rc_fixed_pacing_rate_ss) {
23894 		dest->r_ctl.rc_fixed_pacing_rate_ss = src->r_ctl.rc_fixed_pacing_rate_ss;
23895 		cnt++;
23896 	}
23897 	if (dest->r_ctl.rc_fixed_pacing_rate_rec != src->r_ctl.rc_fixed_pacing_rate_rec) {
23898 		dest->r_ctl.rc_fixed_pacing_rate_rec = src->r_ctl.rc_fixed_pacing_rate_rec;
23899 		cnt++;
23900 	}
23901 	/* TCP_RACK_GP_INCREASE_REC, TCP_RACK_GP_INCREASE_CA, TCP_RACK_GP_INCREASE_SS */
23902 	if (dest->r_ctl.rack_per_of_gp_rec != src->r_ctl.rack_per_of_gp_rec) {
23903 		dest->r_ctl.rack_per_of_gp_rec = src->r_ctl.rack_per_of_gp_rec;
23904 		cnt++;
23905 	}
23906 	if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) {
23907 		dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca;
23908 		cnt++;
23909 	}
23910 
23911 	if (dest->r_ctl.rack_per_of_gp_ss != src->r_ctl.rack_per_of_gp_ss) {
23912 		dest->r_ctl.rack_per_of_gp_ss = src->r_ctl.rack_per_of_gp_ss;
23913 		cnt++;
23914 	}
23915 	/* TCP_RACK_RR_CONF */
23916 	if (dest->r_rr_config != src->r_rr_config) {
23917 		dest->r_rr_config = src->r_rr_config;
23918 		cnt++;
23919 	}
23920 	/* TCP_PACING_DND */
23921 	if (dest->rc_pace_dnd != src->rc_pace_dnd) {
23922 		dest->rc_pace_dnd = src->rc_pace_dnd;
23923 		cnt++;
23924 	}
23925 	/* TCP_HDWR_RATE_CAP */
23926 	if (dest->r_rack_hw_rate_caps != src->r_rack_hw_rate_caps) {
23927 		dest->r_rack_hw_rate_caps = src->r_rack_hw_rate_caps;
23928 		cnt++;
23929 	}
23930 	/* TCP_DGP_UPPER_BOUNDS */
23931 	if (dest->r_ctl.rack_per_upper_bound_ca != src->r_ctl.rack_per_upper_bound_ca) {
23932 		dest->r_ctl.rack_per_upper_bound_ca = src->r_ctl.rack_per_upper_bound_ca;
23933 		cnt++;
23934 	}
23935 	if (dest->r_ctl.rack_per_upper_bound_ss != src->r_ctl.rack_per_upper_bound_ss) {
23936 		dest->r_ctl.rack_per_upper_bound_ss = src->r_ctl.rack_per_upper_bound_ss;
23937 		cnt++;
23938 	}
23939 	/* TCP_SS_EEXIT */
23940 	if (dest->r_ctl.gp_rnd_thresh != src->r_ctl.gp_rnd_thresh) {
23941 		dest->r_ctl.gp_rnd_thresh = src->r_ctl.gp_rnd_thresh;
23942 		cnt++;
23943 	}
23944 	if (dest->r_ctl.gate_to_fs != src->r_ctl.gate_to_fs) {
23945 		dest->r_ctl.gate_to_fs = src->r_ctl.gate_to_fs;
23946 		cnt++;
23947 	}
23948 	if (dest->r_ctl.use_gp_not_last != src->r_ctl.use_gp_not_last) {
23949 		dest->r_ctl.use_gp_not_last = src->r_ctl.use_gp_not_last;
23950 		cnt++;
23951 	}
23952 	if (dest->r_ctl.gp_gain_req != src->r_ctl.gp_gain_req) {
23953 		dest->r_ctl.gp_gain_req = src->r_ctl.gp_gain_req;
23954 		cnt++;
23955 	}
23956 	/* TCP_BBR_HDWR_PACE */
23957 	if (dest->rack_hdw_pace_ena != src->rack_hdw_pace_ena) {
23958 		dest->rack_hdw_pace_ena = src->rack_hdw_pace_ena;
23959 		cnt++;
23960 	}
23961 	if (dest->rack_attempt_hdwr_pace != src->rack_attempt_hdwr_pace) {
23962 		dest->rack_attempt_hdwr_pace = src->rack_attempt_hdwr_pace;
23963 		cnt++;
23964 	}
23965 	/* TCP_RACK_PRR_SENDALOT */
23966 	if (dest->r_ctl.rc_prr_sendalot != src->r_ctl.rc_prr_sendalot) {
23967 		dest->r_ctl.rc_prr_sendalot = src->r_ctl.rc_prr_sendalot;
23968 		cnt++;
23969 	}
23970 	/* TCP_RACK_MIN_TO */
23971 	if (dest->r_ctl.rc_min_to != src->r_ctl.rc_min_to) {
23972 		dest->r_ctl.rc_min_to = src->r_ctl.rc_min_to;
23973 		cnt++;
23974 	}
23975 	/* TCP_RACK_EARLY_SEG */
23976 	if (dest->r_ctl.rc_early_recovery_segs != src->r_ctl.rc_early_recovery_segs) {
23977 		dest->r_ctl.rc_early_recovery_segs = src->r_ctl.rc_early_recovery_segs;
23978 		cnt++;
23979 	}
23980 	/* TCP_RACK_ENABLE_HYSTART */
23981 	if (par->t_ccv.flags != tp->t_ccv.flags) {
23982 		cnt++;
23983 		if (par->t_ccv.flags & CCF_HYSTART_ALLOWED) {
23984 			tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23985 			if (rack_do_hystart > RACK_HYSTART_ON)
23986 				tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23987 			if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23988 				tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23989 		} else {
23990 			tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23991 		}
23992 	}
23993 	/* TCP_RACK_REORD_THRESH */
23994 	if (dest->r_ctl.rc_reorder_shift != src->r_ctl.rc_reorder_shift) {
23995 		dest->r_ctl.rc_reorder_shift = src->r_ctl.rc_reorder_shift;
23996 		cnt++;
23997 	}
23998 	/* TCP_RACK_REORD_FADE */
23999 	if (dest->r_ctl.rc_reorder_fade != src->r_ctl.rc_reorder_fade) {
24000 		dest->r_ctl.rc_reorder_fade = src->r_ctl.rc_reorder_fade;
24001 		cnt++;
24002 	}
24003 	/* TCP_RACK_TLP_THRESH */
24004 	if (dest->r_ctl.rc_tlp_threshold != src->r_ctl.rc_tlp_threshold) {
24005 		dest->r_ctl.rc_tlp_threshold = src->r_ctl.rc_tlp_threshold;
24006 		cnt++;
24007 	}
24008 	/* TCP_BBR_USE_RACK_RR */
24009 	if (dest->use_rack_rr != src->use_rack_rr) {
24010 		dest->use_rack_rr = src->use_rack_rr;
24011 		cnt++;
24012 	}
24013 	/* TCP_RACK_PKT_DELAY */
24014 	if (dest->r_ctl.rc_pkt_delay != src->r_ctl.rc_pkt_delay) {
24015 		dest->r_ctl.rc_pkt_delay = src->r_ctl.rc_pkt_delay;
24016 		cnt++;
24017 	}
24018 	/* TCP_DELACK will get copied via the main code if applicable */
24019 	/* TCP_BBR_RACK_RTT_USE */
24020 	if (dest->r_ctl.rc_rate_sample_method != src->r_ctl.rc_rate_sample_method) {
24021 		dest->r_ctl.rc_rate_sample_method = src->r_ctl.rc_rate_sample_method;
24022 		cnt++;
24023 	}
24024 	/* TCP_HONOR_HPTS_MIN */
24025 	if (dest->r_use_hpts_min != src->r_use_hpts_min) {
24026 		dest->r_use_hpts_min = src->r_use_hpts_min;
24027 		cnt++;
24028 	}
24029 	if (dest->r_ctl.max_reduction != src->r_ctl.max_reduction) {
24030 		dest->r_ctl.max_reduction = src->r_ctl.max_reduction;
24031 		cnt++;
24032 	}
24033 	/* TCP_REC_IS_DYN */
24034 	if (dest->rc_gp_no_rec_chg != src->rc_gp_no_rec_chg) {
24035 		dest->rc_gp_no_rec_chg = src->rc_gp_no_rec_chg;
24036 		cnt++;
24037 	}
24038 	if (dest->rc_skip_timely != src->rc_skip_timely) {
24039 		dest->rc_skip_timely = src->rc_skip_timely;
24040 		cnt++;
24041 	}
24042 	/* TCP_DATA_AFTER_CLOSE */
24043 	if (dest->rc_allow_data_af_clo != src->rc_allow_data_af_clo) {
24044 		dest->rc_allow_data_af_clo = src->rc_allow_data_af_clo;
24045 		cnt++;
24046 	}
24047 	/* TCP_GP_USE_LTBW */
24048 	if (src->use_lesser_lt_bw != dest->use_lesser_lt_bw) {
24049 		dest->use_lesser_lt_bw = src->use_lesser_lt_bw;
24050 		cnt++;
24051 	}
24052 	if (dest->dis_lt_bw != src->dis_lt_bw) {
24053 		dest->dis_lt_bw = src->dis_lt_bw;
24054 		cnt++;
24055 	}
24056 	tcp_log_socket_option(tp, 0, cnt, 0);
24057 }
24058 
24059 
24060 static void
rack_apply_deferred_options(struct tcp_rack * rack)24061 rack_apply_deferred_options(struct tcp_rack *rack)
24062 {
24063 	struct deferred_opt_list *dol, *sdol;
24064 	uint32_t s_optval;
24065 
24066 	TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
24067 		TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
24068 		/* Disadvantage of deferal is you loose the error return */
24069 		s_optval = (uint32_t)dol->optval;
24070 		(void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL);
24071 		free(dol, M_TCPDO);
24072 	}
24073 }
24074 
24075 static void
rack_hw_tls_change(struct tcpcb * tp,int chg)24076 rack_hw_tls_change(struct tcpcb *tp, int chg)
24077 {
24078 	/* Update HW tls state */
24079 	struct tcp_rack *rack;
24080 
24081 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24082 	if (chg)
24083 		rack->r_ctl.fsb.hw_tls = 1;
24084 	else
24085 		rack->r_ctl.fsb.hw_tls = 0;
24086 }
24087 
24088 static int
rack_pru_options(struct tcpcb * tp,int flags)24089 rack_pru_options(struct tcpcb *tp, int flags)
24090 {
24091 	if (flags & PRUS_OOB)
24092 		return (EOPNOTSUPP);
24093 	return (0);
24094 }
24095 
24096 static bool
rack_wake_check(struct tcpcb * tp)24097 rack_wake_check(struct tcpcb *tp)
24098 {
24099 	struct tcp_rack *rack;
24100 	struct timeval tv;
24101 	uint32_t cts;
24102 
24103 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24104 	if (rack->r_ctl.rc_hpts_flags) {
24105 		cts = tcp_get_usecs(&tv);
24106 		if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){
24107 			/*
24108 			 * Pacing timer is up, check if we are ready.
24109 			 */
24110 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to))
24111 				return (true);
24112 		} else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) {
24113 			/*
24114 			 * A timer is up, check if we are ready.
24115 			 */
24116 			if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp))
24117 				return (true);
24118 		}
24119 	}
24120 	return (false);
24121 }
24122 
24123 static struct tcp_function_block __tcp_rack = {
24124 	.tfb_tcp_block_name = __XSTRING(STACKNAME),
24125 	.tfb_tcp_output = rack_output,
24126 	.tfb_do_queued_segments = ctf_do_queued_segments,
24127 	.tfb_do_segment_nounlock = rack_do_segment_nounlock,
24128 	.tfb_tcp_do_segment = rack_do_segment,
24129 	.tfb_tcp_ctloutput = rack_ctloutput,
24130 	.tfb_tcp_fb_init = rack_init,
24131 	.tfb_tcp_fb_fini = rack_fini,
24132 	.tfb_tcp_timer_stop_all = rack_stopall,
24133 	.tfb_tcp_rexmit_tmr = rack_remxt_tmr,
24134 	.tfb_tcp_handoff_ok = rack_handoff_ok,
24135 	.tfb_tcp_mtu_chg = rack_mtu_change,
24136 	.tfb_pru_options = rack_pru_options,
24137 	.tfb_hwtls_change = rack_hw_tls_change,
24138 	.tfb_chg_query = rack_chg_query,
24139 	.tfb_switch_failed = rack_switch_failed,
24140 	.tfb_early_wake_check = rack_wake_check,
24141 	.tfb_compute_pipe = rack_compute_pipe,
24142 	.tfb_stack_info = rack_stack_information,
24143 	.tfb_inherit = rack_inherit,
24144 	.tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK,
24145 
24146 };
24147 
24148 /*
24149  * rack_ctloutput() must drop the inpcb lock before performing copyin on
24150  * socket option arguments.  When it re-acquires the lock after the copy, it
24151  * has to revalidate that the connection is still valid for the socket
24152  * option.
24153  */
24154 static int
rack_set_sockopt(struct tcpcb * tp,struct sockopt * sopt)24155 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
24156 {
24157 	struct inpcb *inp = tptoinpcb(tp);
24158 #ifdef INET
24159 	struct ip *ip;
24160 #endif
24161 	struct tcp_rack *rack;
24162 	struct tcp_hybrid_req hybrid;
24163 	uint64_t loptval;
24164 	int32_t error = 0, optval;
24165 
24166 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24167 	if (rack == NULL) {
24168 		INP_WUNLOCK(inp);
24169 		return (EINVAL);
24170 	}
24171 #ifdef INET
24172 	ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
24173 #endif
24174 
24175 	switch (sopt->sopt_level) {
24176 #ifdef INET6
24177 	case IPPROTO_IPV6:
24178 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
24179 		switch (sopt->sopt_name) {
24180 		case IPV6_USE_MIN_MTU:
24181 			tcp6_use_min_mtu(tp);
24182 			break;
24183 		}
24184 		INP_WUNLOCK(inp);
24185 		return (0);
24186 #endif
24187 #ifdef INET
24188 	case IPPROTO_IP:
24189 		switch (sopt->sopt_name) {
24190 		case IP_TOS:
24191 			/*
24192 			 * The DSCP codepoint has changed, update the fsb.
24193 			 */
24194 			ip->ip_tos = rack->rc_inp->inp_ip_tos;
24195 			break;
24196 		case IP_TTL:
24197 			/*
24198 			 * The TTL has changed, update the fsb.
24199 			 */
24200 			ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
24201 			break;
24202 		}
24203 		INP_WUNLOCK(inp);
24204 		return (0);
24205 #endif
24206 #ifdef SO_PEERPRIO
24207 	case SOL_SOCKET:
24208 		switch (sopt->sopt_name) {
24209 		case SO_PEERPRIO:			/*  SC-URL:bs */
24210 			/* Already read in and sanity checked in sosetopt(). */
24211 			if (inp->inp_socket) {
24212 				rack->client_bufferlvl = inp->inp_socket->so_peerprio;
24213 			}
24214 			break;
24215 		}
24216 		INP_WUNLOCK(inp);
24217 		return (0);
24218 #endif
24219 	case IPPROTO_TCP:
24220 		switch (sopt->sopt_name) {
24221 		case TCP_RACK_TLP_REDUCE:		/*  URL:tlp_reduce */
24222 		/*  Pacing related ones */
24223 		case TCP_RACK_PACE_ALWAYS:		/*  URL:pace_always */
24224 		case TCP_BBR_RACK_INIT_RATE:		/*  URL:irate */
24225 		case TCP_RACK_PACE_MIN_SEG:		/*  URL:pace_min_seg */
24226 		case TCP_RACK_PACE_MAX_SEG:		/*  URL:pace_max_seg */
24227 		case TCP_RACK_FORCE_MSEG:		/*  URL:force_max_seg */
24228 		case TCP_RACK_PACE_RATE_CA:		/*  URL:pr_ca */
24229 		case TCP_RACK_PACE_RATE_SS:		/*  URL:pr_ss*/
24230 		case TCP_RACK_PACE_RATE_REC:		/*  URL:pr_rec */
24231 		case TCP_RACK_GP_INCREASE_CA:		/*  URL:gp_inc_ca */
24232 		case TCP_RACK_GP_INCREASE_SS:		/*  URL:gp_inc_ss */
24233 		case TCP_RACK_GP_INCREASE_REC:		/*  URL:gp_inc_rec */
24234 		case TCP_RACK_RR_CONF:			/*  URL:rrr_conf */
24235 		case TCP_BBR_HDWR_PACE:			/*  URL:hdwrpace */
24236 		case TCP_HDWR_RATE_CAP:			/*  URL:hdwrcap boolean */
24237 		case TCP_PACING_RATE_CAP:		/*  URL:cap  -- used by side-channel */
24238 		case TCP_HDWR_UP_ONLY:			/*  URL:uponly -- hardware pacing  boolean */
24239 		case TCP_FILLCW_RATE_CAP:		/*  URL:fillcw_cap */
24240 		case TCP_RACK_PACING_BETA_ECN:		/*  URL:pacing_beta_ecn */
24241 		case TCP_RACK_PACE_TO_FILL:		/*  URL:fillcw */
24242 			/* End pacing related */
24243 		case TCP_DELACK:			/*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
24244 		case TCP_RACK_PRR_SENDALOT:		/*  URL:prr_sendalot */
24245 		case TCP_RACK_MIN_TO:			/*  URL:min_to */
24246 		case TCP_RACK_EARLY_SEG:		/*  URL:early_seg */
24247 		case TCP_RACK_REORD_THRESH:		/*  URL:reord_thresh */
24248 		case TCP_RACK_REORD_FADE:		/*  URL:reord_fade */
24249 		case TCP_RACK_TLP_THRESH:		/*  URL:tlp_thresh */
24250 		case TCP_RACK_PKT_DELAY:		/*  URL:pkt_delay */
24251 		case TCP_RACK_TLP_USE:			/*  URL:tlp_use */
24252 		case TCP_BBR_RACK_RTT_USE:		/*  URL:rttuse */
24253 		case TCP_BBR_USE_RACK_RR:		/*  URL:rackrr */
24254 		case TCP_NO_PRR:			/*  URL:noprr */
24255 		case TCP_TIMELY_DYN_ADJ:      		/*  URL:dynamic */
24256 		case TCP_DATA_AFTER_CLOSE:		/*  no URL */
24257 		case TCP_RACK_NONRXT_CFG_RATE:		/*  URL:nonrxtcr */
24258 		case TCP_SHARED_CWND_ENABLE:		/*  URL:scwnd */
24259 		case TCP_RACK_MBUF_QUEUE:		/*  URL:mqueue */
24260 		case TCP_RACK_NO_PUSH_AT_MAX:		/*  URL:npush */
24261 		case TCP_SHARED_CWND_TIME_LIMIT:	/*  URL:lscwnd */
24262 		case TCP_RACK_PROFILE:			/*  URL:profile */
24263 		case TCP_SIDECHAN_DIS:			/*  URL:scodm */
24264 		case TCP_HYBRID_PACING:			/*  URL:pacing=hybrid */
24265 		case TCP_USE_CMP_ACKS:			/*  URL:cmpack */
24266 		case TCP_RACK_ABC_VAL:			/*  URL:labc */
24267 		case TCP_REC_ABC_VAL:			/*  URL:reclabc */
24268 		case TCP_RACK_MEASURE_CNT:		/*  URL:measurecnt */
24269 		case TCP_DEFER_OPTIONS:			/*  URL:defer */
24270 		case TCP_RACK_DSACK_OPT:		/*  URL:dsack */
24271 		case TCP_RACK_TIMER_SLOP:		/*  URL:timer_slop */
24272 		case TCP_RACK_ENABLE_HYSTART:		/*  URL:hystart */
24273 		case TCP_RACK_SET_RXT_OPTIONS:		/*  URL:rxtsz */
24274 		case TCP_RACK_HI_BETA:			/*  URL:hibeta */
24275 		case TCP_RACK_SPLIT_LIMIT:		/*  URL:split */
24276 		case TCP_SS_EEXIT:			/*  URL:eexit */
24277 		case TCP_DGP_UPPER_BOUNDS:		/*  URL:upper */
24278 		case TCP_RACK_PACING_DIVISOR:		/*  URL:divisor */
24279 		case TCP_PACING_DND:			/*  URL:dnd */
24280 		case TCP_NO_TIMELY:			/*  URL:notimely */
24281 		case RACK_CSPR_IS_FCC:			/*  URL:csprisfcc */
24282 		case TCP_HONOR_HPTS_MIN:		/*  URL:hptsmin */
24283 		case TCP_REC_IS_DYN:			/*  URL:dynrec */
24284 		case TCP_GP_USE_LTBW:			/*  URL:useltbw */
24285 			goto process_opt;
24286 			break;
24287 		default:
24288 			/* Filter off all unknown options to the base stack */
24289 			return (tcp_default_ctloutput(tp, sopt));
24290 			break;
24291 		}
24292 	default:
24293 		INP_WUNLOCK(inp);
24294 		return (0);
24295 	}
24296 process_opt:
24297 	INP_WUNLOCK(inp);
24298 	if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
24299 	    (sopt->sopt_name == TCP_FILLCW_RATE_CAP)) {
24300 		error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
24301 		/*
24302 		 * We truncate it down to 32 bits for the socket-option trace this
24303 		 * means rates > 34Gbps won't show right, but thats probably ok.
24304 		 */
24305 		optval = (uint32_t)loptval;
24306 	} else if (sopt->sopt_name == TCP_HYBRID_PACING) {
24307 		error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid));
24308 	} else {
24309 		error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
24310 		/* Save it in 64 bit form too */
24311 		loptval = optval;
24312 	}
24313 	if (error)
24314 		return (error);
24315 	INP_WLOCK(inp);
24316 	if (tp->t_fb != &__tcp_rack) {
24317 		INP_WUNLOCK(inp);
24318 		return (ENOPROTOOPT);
24319 	}
24320 	if (rack->defer_options && (rack->gp_ready == 0) &&
24321 	    (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
24322 	    (sopt->sopt_name != TCP_HYBRID_PACING) &&
24323 	    (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) &&
24324 	    (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
24325 	    (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
24326 		/* Options are being deferred */
24327 		if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
24328 			INP_WUNLOCK(inp);
24329 			return (0);
24330 		} else {
24331 			/* No memory to defer, fail */
24332 			INP_WUNLOCK(inp);
24333 			return (ENOMEM);
24334 		}
24335 	}
24336 	error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid);
24337 	INP_WUNLOCK(inp);
24338 	return (error);
24339 }
24340 
24341 static void
rack_fill_info(struct tcpcb * tp,struct tcp_info * ti)24342 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
24343 {
24344 
24345 	INP_WLOCK_ASSERT(tptoinpcb(tp));
24346 	bzero(ti, sizeof(*ti));
24347 
24348 	ti->tcpi_state = tp->t_state;
24349 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
24350 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
24351 	if (tp->t_flags & TF_SACK_PERMIT)
24352 		ti->tcpi_options |= TCPI_OPT_SACK;
24353 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
24354 		ti->tcpi_options |= TCPI_OPT_WSCALE;
24355 		ti->tcpi_snd_wscale = tp->snd_scale;
24356 		ti->tcpi_rcv_wscale = tp->rcv_scale;
24357 	}
24358 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
24359 		ti->tcpi_options |= TCPI_OPT_ECN;
24360 	if (tp->t_flags & TF_FASTOPEN)
24361 		ti->tcpi_options |= TCPI_OPT_TFO;
24362 	/* still kept in ticks is t_rcvtime */
24363 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
24364 	/* Since we hold everything in precise useconds this is easy */
24365 	ti->tcpi_rtt = tp->t_srtt;
24366 	ti->tcpi_rttvar = tp->t_rttvar;
24367 	ti->tcpi_rto = tp->t_rxtcur;
24368 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
24369 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
24370 	/*
24371 	 * FreeBSD-specific extension fields for tcp_info.
24372 	 */
24373 	ti->tcpi_rcv_space = tp->rcv_wnd;
24374 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
24375 	ti->tcpi_snd_wnd = tp->snd_wnd;
24376 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
24377 	ti->tcpi_snd_nxt = tp->snd_nxt;
24378 	ti->tcpi_snd_mss = tp->t_maxseg;
24379 	ti->tcpi_rcv_mss = tp->t_maxseg;
24380 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
24381 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
24382 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
24383 	ti->tcpi_total_tlp = tp->t_sndtlppack;
24384 	ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
24385 	ti->tcpi_rttmin = tp->t_rttlow;
24386 #ifdef NETFLIX_STATS
24387 	memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
24388 #endif
24389 #ifdef TCP_OFFLOAD
24390 	if (tp->t_flags & TF_TOE) {
24391 		ti->tcpi_options |= TCPI_OPT_TOE;
24392 		tcp_offload_tcp_info(tp, ti);
24393 	}
24394 #endif
24395 }
24396 
24397 static int
rack_get_sockopt(struct tcpcb * tp,struct sockopt * sopt)24398 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
24399 {
24400 	struct inpcb *inp = tptoinpcb(tp);
24401 	struct tcp_rack *rack;
24402 	int32_t error, optval;
24403 	uint64_t val, loptval;
24404 	struct	tcp_info ti;
24405 	/*
24406 	 * Because all our options are either boolean or an int, we can just
24407 	 * pull everything into optval and then unlock and copy. If we ever
24408 	 * add a option that is not a int, then this will have quite an
24409 	 * impact to this routine.
24410 	 */
24411 	error = 0;
24412 	rack = (struct tcp_rack *)tp->t_fb_ptr;
24413 	if (rack == NULL) {
24414 		INP_WUNLOCK(inp);
24415 		return (EINVAL);
24416 	}
24417 	switch (sopt->sopt_name) {
24418 	case TCP_INFO:
24419 		/* First get the info filled */
24420 		rack_fill_info(tp, &ti);
24421 		/* Fix up the rtt related fields if needed */
24422 		INP_WUNLOCK(inp);
24423 		error = sooptcopyout(sopt, &ti, sizeof ti);
24424 		return (error);
24425 	/*
24426 	 * Beta is the congestion control value for NewReno that influences how
24427 	 * much of a backoff happens when loss is detected. It is normally set
24428 	 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
24429 	 * when you exit recovery.
24430 	 */
24431 	case TCP_RACK_PACING_BETA:
24432 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24433 			error = EINVAL;
24434 		else if (rack->rc_pacing_cc_set == 0)
24435 			optval = rack->r_ctl.rc_saved_beta;
24436 		else {
24437 			/*
24438 			 * Reach out into the CC data and report back what
24439 			 * I have previously set. Yeah it looks hackish but
24440 			 * we don't want to report the saved values.
24441 			 */
24442 			if (tp->t_ccv.cc_data)
24443 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta;
24444 			else
24445 				error = EINVAL;
24446 		}
24447 		break;
24448 	/*
24449 	 * Beta_ecn is the congestion control value for NewReno that influences how
24450 	 * much of a backoff happens when a ECN mark is detected. It is normally set
24451 	 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
24452 	 * you exit recovery. Note that classic ECN has a beta of 50, it is only
24453 	 * ABE Ecn that uses this "less" value, but we do too with pacing :)
24454 	 */
24455 	case TCP_RACK_PACING_BETA_ECN:
24456 		if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
24457 			error = EINVAL;
24458 		else if (rack->rc_pacing_cc_set == 0)
24459 			optval = rack->r_ctl.rc_saved_beta_ecn;
24460 		else {
24461 			/*
24462 			 * Reach out into the CC data and report back what
24463 			 * I have previously set. Yeah it looks hackish but
24464 			 * we don't want to report the saved values.
24465 			 */
24466 			if (tp->t_ccv.cc_data)
24467 				optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn;
24468 			else
24469 				error = EINVAL;
24470 		}
24471 		break;
24472 	case TCP_RACK_DSACK_OPT:
24473 		optval = 0;
24474 		if (rack->rc_rack_tmr_std_based) {
24475 			optval |= 1;
24476 		}
24477 		if (rack->rc_rack_use_dsack) {
24478 			optval |= 2;
24479 		}
24480 		break;
24481 	case TCP_RACK_ENABLE_HYSTART:
24482 	{
24483 		if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
24484 			optval = RACK_HYSTART_ON;
24485 			if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND)
24486 				optval = RACK_HYSTART_ON_W_SC;
24487 			if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH)
24488 				optval = RACK_HYSTART_ON_W_SC_C;
24489 		} else {
24490 			optval = RACK_HYSTART_OFF;
24491 		}
24492 	}
24493 	break;
24494 	case TCP_RACK_DGP_IN_REC:
24495 		error = EINVAL;
24496 		break;
24497 	case TCP_RACK_HI_BETA:
24498 		optval = rack->rack_hibeta;
24499 		break;
24500 	case TCP_DEFER_OPTIONS:
24501 		optval = rack->defer_options;
24502 		break;
24503 	case TCP_RACK_MEASURE_CNT:
24504 		optval = rack->r_ctl.req_measurements;
24505 		break;
24506 	case TCP_REC_ABC_VAL:
24507 		optval = rack->r_use_labc_for_rec;
24508 		break;
24509 	case TCP_RACK_ABC_VAL:
24510 		optval = rack->rc_labc;
24511 		break;
24512 	case TCP_HDWR_UP_ONLY:
24513 		optval= rack->r_up_only;
24514 		break;
24515 	case TCP_FILLCW_RATE_CAP:
24516 		loptval = rack->r_ctl.fillcw_cap;
24517 		break;
24518 	case TCP_PACING_RATE_CAP:
24519 		loptval = rack->r_ctl.bw_rate_cap;
24520 		break;
24521 	case TCP_RACK_PROFILE:
24522 		/* You cannot retrieve a profile, its write only */
24523 		error = EINVAL;
24524 		break;
24525 	case TCP_SIDECHAN_DIS:
24526 		optval = rack->r_ctl.side_chan_dis_mask;
24527 		break;
24528 	case TCP_HYBRID_PACING:
24529 		/* You cannot retrieve hybrid pacing information, its write only */
24530 		error = EINVAL;
24531 		break;
24532 	case TCP_USE_CMP_ACKS:
24533 		optval = rack->r_use_cmp_ack;
24534 		break;
24535 	case TCP_RACK_PACE_TO_FILL:
24536 		optval = rack->rc_pace_to_cwnd;
24537 		break;
24538 	case TCP_RACK_NO_PUSH_AT_MAX:
24539 		optval = rack->r_ctl.rc_no_push_at_mrtt;
24540 		break;
24541 	case TCP_SHARED_CWND_ENABLE:
24542 		optval = rack->rack_enable_scwnd;
24543 		break;
24544 	case TCP_RACK_NONRXT_CFG_RATE:
24545 		optval = rack->rack_rec_nonrxt_use_cr;
24546 		break;
24547 	case TCP_NO_PRR:
24548 		if (rack->rack_no_prr  == 1)
24549 			optval = 1;
24550 		else if (rack->no_prr_addback == 1)
24551 			optval = 2;
24552 		else
24553 			optval = 0;
24554 		break;
24555 	case TCP_GP_USE_LTBW:
24556 		if (rack->dis_lt_bw) {
24557 			/* It is not used */
24558 			optval = 0;
24559 		} else if (rack->use_lesser_lt_bw) {
24560 			/* we use min() */
24561 			optval = 1;
24562 		} else {
24563 			/* we use max() */
24564 			optval = 2;
24565 		}
24566 		break;
24567 	case TCP_RACK_DO_DETECTION:
24568 		error = EINVAL;
24569 		break;
24570 	case TCP_RACK_MBUF_QUEUE:
24571 		/* Now do we use the LRO mbuf-queue feature */
24572 		optval = rack->r_mbuf_queue;
24573 		break;
24574 	case RACK_CSPR_IS_FCC:
24575 		optval = rack->cspr_is_fcc;
24576 		break;
24577 	case TCP_TIMELY_DYN_ADJ:
24578 		optval = rack->rc_gp_dyn_mul;
24579 		break;
24580 	case TCP_BBR_IWINTSO:
24581 		error = EINVAL;
24582 		break;
24583 	case TCP_RACK_TLP_REDUCE:
24584 		/* RACK TLP cwnd reduction (bool) */
24585 		optval = rack->r_ctl.rc_tlp_cwnd_reduce;
24586 		break;
24587 	case TCP_BBR_RACK_INIT_RATE:
24588 		val = rack->r_ctl.init_rate;
24589 		/* convert to kbits per sec */
24590 		val *= 8;
24591 		val /= 1000;
24592 		optval = (uint32_t)val;
24593 		break;
24594 	case TCP_RACK_FORCE_MSEG:
24595 		optval = rack->rc_force_max_seg;
24596 		break;
24597 	case TCP_RACK_PACE_MIN_SEG:
24598 		optval = rack->r_ctl.rc_user_set_min_segs;
24599 		break;
24600 	case TCP_RACK_PACE_MAX_SEG:
24601 		/* Max segments in a pace */
24602 		optval = rack->rc_user_set_max_segs;
24603 		break;
24604 	case TCP_RACK_PACE_ALWAYS:
24605 		/* Use the always pace method */
24606 		optval = rack->rc_always_pace;
24607 		break;
24608 	case TCP_RACK_PRR_SENDALOT:
24609 		/* Allow PRR to send more than one seg */
24610 		optval = rack->r_ctl.rc_prr_sendalot;
24611 		break;
24612 	case TCP_RACK_MIN_TO:
24613 		/* Minimum time between rack t-o's in ms */
24614 		optval = rack->r_ctl.rc_min_to;
24615 		break;
24616 	case TCP_RACK_SPLIT_LIMIT:
24617 		optval = rack->r_ctl.rc_split_limit;
24618 		break;
24619 	case TCP_RACK_EARLY_SEG:
24620 		/* If early recovery max segments */
24621 		optval = rack->r_ctl.rc_early_recovery_segs;
24622 		break;
24623 	case TCP_RACK_REORD_THRESH:
24624 		/* RACK reorder threshold (shift amount) */
24625 		optval = rack->r_ctl.rc_reorder_shift;
24626 		break;
24627 	case TCP_SS_EEXIT:
24628 		if (rack->r_ctl.gp_rnd_thresh) {
24629 			uint32_t v;
24630 
24631 			v = rack->r_ctl.gp_gain_req;
24632 			v <<= 17;
24633 			optval = v | (rack->r_ctl.gp_rnd_thresh & 0xff);
24634 			if (rack->r_ctl.gate_to_fs == 1)
24635 				optval |= 0x10000;
24636 		} else
24637 			optval = 0;
24638 		break;
24639 	case TCP_RACK_REORD_FADE:
24640 		/* Does reordering fade after ms time */
24641 		optval = rack->r_ctl.rc_reorder_fade;
24642 		break;
24643 	case TCP_BBR_USE_RACK_RR:
24644 		/* Do we use the rack cheat for rxt */
24645 		optval = rack->use_rack_rr;
24646 		break;
24647 	case TCP_RACK_RR_CONF:
24648 		optval = rack->r_rr_config;
24649 		break;
24650 	case TCP_HDWR_RATE_CAP:
24651 		optval = rack->r_rack_hw_rate_caps;
24652 		break;
24653 	case TCP_BBR_HDWR_PACE:
24654 		optval = rack->rack_hdw_pace_ena;
24655 		break;
24656 	case TCP_RACK_TLP_THRESH:
24657 		/* RACK TLP theshold i.e. srtt+(srtt/N) */
24658 		optval = rack->r_ctl.rc_tlp_threshold;
24659 		break;
24660 	case TCP_RACK_PKT_DELAY:
24661 		/* RACK added ms i.e. rack-rtt + reord + N */
24662 		optval = rack->r_ctl.rc_pkt_delay;
24663 		break;
24664 	case TCP_RACK_TLP_USE:
24665 		optval = rack->rack_tlp_threshold_use;
24666 		break;
24667 	case TCP_PACING_DND:
24668 		optval = rack->rc_pace_dnd;
24669 		break;
24670 	case TCP_RACK_PACE_RATE_CA:
24671 		optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
24672 		break;
24673 	case TCP_RACK_PACE_RATE_SS:
24674 		optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
24675 		break;
24676 	case TCP_RACK_PACE_RATE_REC:
24677 		optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
24678 		break;
24679 	case TCP_DGP_UPPER_BOUNDS:
24680 		optval = rack->r_ctl.rack_per_upper_bound_ss;
24681 		optval <<= 16;
24682 		optval |= rack->r_ctl.rack_per_upper_bound_ca;
24683 		break;
24684 	case TCP_RACK_GP_INCREASE_SS:
24685 		optval = rack->r_ctl.rack_per_of_gp_ca;
24686 		break;
24687 	case TCP_RACK_GP_INCREASE_CA:
24688 		optval = rack->r_ctl.rack_per_of_gp_ss;
24689 		break;
24690 	case TCP_RACK_PACING_DIVISOR:
24691 		optval = rack->r_ctl.pace_len_divisor;
24692 		break;
24693 	case TCP_BBR_RACK_RTT_USE:
24694 		optval = rack->r_ctl.rc_rate_sample_method;
24695 		break;
24696 	case TCP_DELACK:
24697 		optval = tp->t_delayed_ack;
24698 		break;
24699 	case TCP_DATA_AFTER_CLOSE:
24700 		optval = rack->rc_allow_data_af_clo;
24701 		break;
24702 	case TCP_SHARED_CWND_TIME_LIMIT:
24703 		optval = rack->r_limit_scw;
24704 		break;
24705 	case TCP_HONOR_HPTS_MIN:
24706 		if (rack->r_use_hpts_min)
24707 			optval = rack->r_ctl.max_reduction;
24708 		else
24709 			optval = 0;
24710 		break;
24711 	case TCP_REC_IS_DYN:
24712 		optval = rack->rc_gp_no_rec_chg;
24713 		break;
24714 	case TCP_NO_TIMELY:
24715 		optval = rack->rc_skip_timely;
24716 		break;
24717 	case TCP_RACK_TIMER_SLOP:
24718 		optval = rack->r_ctl.timer_slop;
24719 		break;
24720 	default:
24721 		return (tcp_default_ctloutput(tp, sopt));
24722 		break;
24723 	}
24724 	INP_WUNLOCK(inp);
24725 	if (error == 0) {
24726 		if ((sopt->sopt_name == TCP_PACING_RATE_CAP) ||
24727 		    (sopt->sopt_name == TCP_FILLCW_RATE_CAP))
24728 			error = sooptcopyout(sopt, &loptval, sizeof loptval);
24729 		else
24730 			error = sooptcopyout(sopt, &optval, sizeof optval);
24731 	}
24732 	return (error);
24733 }
24734 
24735 static int
rack_ctloutput(struct tcpcb * tp,struct sockopt * sopt)24736 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
24737 {
24738 	if (sopt->sopt_dir == SOPT_SET) {
24739 		return (rack_set_sockopt(tp, sopt));
24740 	} else if (sopt->sopt_dir == SOPT_GET) {
24741 		return (rack_get_sockopt(tp, sopt));
24742 	} else {
24743 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
24744 	}
24745 }
24746 
24747 static const char *rack_stack_names[] = {
24748 	__XSTRING(STACKNAME),
24749 #ifdef STACKALIAS
24750 	__XSTRING(STACKALIAS),
24751 #endif
24752 };
24753 
24754 static int
rack_ctor(void * mem,int32_t size,void * arg,int32_t how)24755 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
24756 {
24757 	memset(mem, 0, size);
24758 	return (0);
24759 }
24760 
24761 static void
rack_dtor(void * mem,int32_t size,void * arg)24762 rack_dtor(void *mem, int32_t size, void *arg)
24763 {
24764 
24765 }
24766 
24767 static bool rack_mod_inited = false;
24768 
24769 static int
tcp_addrack(module_t mod,int32_t type,void * data)24770 tcp_addrack(module_t mod, int32_t type, void *data)
24771 {
24772 	int32_t err = 0;
24773 	int num_stacks;
24774 
24775 	switch (type) {
24776 	case MOD_LOAD:
24777 		rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
24778 		    sizeof(struct rack_sendmap),
24779 		    rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
24780 
24781 		rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
24782 		    sizeof(struct tcp_rack),
24783 		    rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
24784 
24785 		sysctl_ctx_init(&rack_sysctl_ctx);
24786 		rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
24787 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
24788 		    OID_AUTO,
24789 #ifdef STACKALIAS
24790 		    __XSTRING(STACKALIAS),
24791 #else
24792 		    __XSTRING(STACKNAME),
24793 #endif
24794 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
24795 		    "");
24796 		if (rack_sysctl_root == NULL) {
24797 			printf("Failed to add sysctl node\n");
24798 			err = EFAULT;
24799 			goto free_uma;
24800 		}
24801 		rack_init_sysctls();
24802 		num_stacks = nitems(rack_stack_names);
24803 		err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
24804 		    rack_stack_names, &num_stacks);
24805 		if (err) {
24806 			printf("Failed to register %s stack name for "
24807 			    "%s module\n", rack_stack_names[num_stacks],
24808 			    __XSTRING(MODNAME));
24809 			sysctl_ctx_free(&rack_sysctl_ctx);
24810 free_uma:
24811 			uma_zdestroy(rack_zone);
24812 			uma_zdestroy(rack_pcb_zone);
24813 			rack_counter_destroy();
24814 			printf("Failed to register rack module -- err:%d\n", err);
24815 			return (err);
24816 		}
24817 		tcp_lro_reg_mbufq();
24818 		rack_mod_inited = true;
24819 		break;
24820 	case MOD_QUIESCE:
24821 		err = deregister_tcp_functions(&__tcp_rack, true, false);
24822 		break;
24823 	case MOD_UNLOAD:
24824 		err = deregister_tcp_functions(&__tcp_rack, false, true);
24825 		if (err == EBUSY)
24826 			break;
24827 		if (rack_mod_inited) {
24828 			uma_zdestroy(rack_zone);
24829 			uma_zdestroy(rack_pcb_zone);
24830 			sysctl_ctx_free(&rack_sysctl_ctx);
24831 			rack_counter_destroy();
24832 			rack_mod_inited = false;
24833 		}
24834 		tcp_lro_dereg_mbufq();
24835 		err = 0;
24836 		break;
24837 	default:
24838 		return (EOPNOTSUPP);
24839 	}
24840 	return (err);
24841 }
24842 
24843 static moduledata_t tcp_rack = {
24844 	.name = __XSTRING(MODNAME),
24845 	.evhand = tcp_addrack,
24846 	.priv = 0
24847 };
24848 
24849 MODULE_VERSION(MODNAME, 1);
24850 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
24851 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
24852 
24853 #endif /* #if !defined(INET) && !defined(INET6) */
24854