]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_stacks/rack.c
tcp: Add hystart-plus to cc_newreno and rack.
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_stacks / rack.c
1 /*-
2  * Copyright (c) 2016-2020 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_ipsec.h"
33 #include "opt_tcpdebug.h"
34 #include "opt_ratelimit.h"
35 #include "opt_kern_tls.h"
36 #include <sys/param.h>
37 #include <sys/arb.h>
38 #include <sys/module.h>
39 #include <sys/kernel.h>
40 #ifdef TCP_HHOOK
41 #include <sys/hhook.h>
42 #endif
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>           /* for proc0 declaration */
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #ifdef STATS
54 #include <sys/qmath.h>
55 #include <sys/tree.h>
56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
57 #else
58 #include <sys/tree.h>
59 #endif
60 #include <sys/refcount.h>
61 #include <sys/queue.h>
62 #include <sys/tim_filter.h>
63 #include <sys/smp.h>
64 #include <sys/kthread.h>
65 #include <sys/kern_prefetch.h>
66 #include <sys/protosw.h>
67 #ifdef TCP_ACCOUNTING
68 #include <sys/sched.h>
69 #include <machine/cpu.h>
70 #endif
71 #include <vm/uma.h>
72
73 #include <net/route.h>
74 #include <net/route/nhop.h>
75 #include <net/vnet.h>
76
77 #define TCPSTATES               /* for logging */
78
79 #include <netinet/in.h>
80 #include <netinet/in_kdtrace.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_icmp.h>    /* required for icmp_var.h */
84 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM */
85 #include <netinet/ip_var.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet/tcp.h>
90 #define TCPOUTFLAGS
91 #include <netinet/tcp_fsm.h>
92 #include <netinet/tcp_log_buf.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcp_hpts.h>
97 #include <netinet/tcp_ratelimit.h>
98 #include <netinet/tcp_accounting.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/cc/cc.h>
101 #include <netinet/cc/cc_newreno.h>
102 #include <netinet/tcp_fastopen.h>
103 #include <netinet/tcp_lro.h>
104 #ifdef NETFLIX_SHARED_CWND
105 #include <netinet/tcp_shared_cwnd.h>
106 #endif
107 #ifdef TCPDEBUG
108 #include <netinet/tcp_debug.h>
109 #endif                          /* TCPDEBUG */
110 #ifdef TCP_OFFLOAD
111 #include <netinet/tcp_offload.h>
112 #endif
113 #ifdef INET6
114 #include <netinet6/tcp6_var.h>
115 #endif
116
117 #include <netipsec/ipsec_support.h>
118
119 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
120 #include <netipsec/ipsec.h>
121 #include <netipsec/ipsec6.h>
122 #endif                          /* IPSEC */
123
124 #include <netinet/udp.h>
125 #include <netinet/udp_var.h>
126 #include <machine/in_cksum.h>
127
128 #ifdef MAC
129 #include <security/mac/mac_framework.h>
130 #endif
131 #include "sack_filter.h"
132 #include "tcp_rack.h"
133 #include "rack_bbr_common.h"
134
135 uma_zone_t rack_zone;
136 uma_zone_t rack_pcb_zone;
137
138 #ifndef TICKS2SBT
139 #define TICKS2SBT(__t)  (tick_sbt * ((sbintime_t)(__t)))
140 #endif
141
142 VNET_DECLARE(uint32_t, newreno_beta);
143 VNET_DECLARE(uint32_t, newreno_beta_ecn);
144 #define V_newreno_beta VNET(newreno_beta)
145 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
146
147
148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb", "TCP fast send block");
149 MALLOC_DEFINE(M_TCPDO, "tcp_do", "TCP deferred options");
150
151 struct sysctl_ctx_list rack_sysctl_ctx;
152 struct sysctl_oid *rack_sysctl_root;
153
154 #define CUM_ACKED 1
155 #define SACKED 2
156
157 /*
158  * The RACK module incorporates a number of
159  * TCP ideas that have been put out into the IETF
160  * over the last few years:
161  * - Matt Mathis's Rate Halving which slowly drops
162  *    the congestion window so that the ack clock can
163  *    be maintained during a recovery.
164  * - Yuchung Cheng's RACK TCP (for which its named) that
165  *    will stop us using the number of dup acks and instead
166  *    use time as the gage of when we retransmit.
167  * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
168  *    of Dukkipati et.al.
169  * RACK depends on SACK, so if an endpoint arrives that
170  * cannot do SACK the state machine below will shuttle the
171  * connection back to using the "default" TCP stack that is
172  * in FreeBSD.
173  *
174  * To implement RACK the original TCP stack was first decomposed
175  * into a functional state machine with individual states
176  * for each of the possible TCP connection states. The do_segement
177  * functions role in life is to mandate the connection supports SACK
178  * initially and then assure that the RACK state matches the conenction
179  * state before calling the states do_segment function. Each
180  * state is simplified due to the fact that the original do_segment
181  * has been decomposed and we *know* what state we are in (no
182  * switches on the state) and all tests for SACK are gone. This
183  * greatly simplifies what each state does.
184  *
185  * TCP output is also over-written with a new version since it
186  * must maintain the new rack scoreboard.
187  *
188  */
189 static int32_t rack_tlp_thresh = 1;
190 static int32_t rack_tlp_limit = 2;      /* No more than 2 TLPs w-out new data */
191 static int32_t rack_tlp_use_greater = 1;
192 static int32_t rack_reorder_thresh = 2;
193 static int32_t rack_reorder_fade = 60000000;    /* 0 - never fade, def 60,000,000
194                                                  * - 60 seconds */
195 static uint8_t rack_req_measurements = 1;
196 /* Attack threshold detections */
197 static uint32_t rack_highest_sack_thresh_seen = 0;
198 static uint32_t rack_highest_move_thresh_seen = 0;
199 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
200 static int32_t rack_hw_pace_extra_slots = 2;    /* 2 extra MSS time betweens */
201 static int32_t rack_hw_rate_caps = 1; /* 1; */
202 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
203 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
204 static int32_t rack_hw_up_only = 1;
205 static int32_t rack_stats_gets_ms_rtt = 1;
206 static int32_t rack_prr_addbackmax = 2;
207 static int32_t rack_do_hystart = 0;
208
209 static int32_t rack_pkt_delay = 1000;
210 static int32_t rack_send_a_lot_in_prr = 1;
211 static int32_t rack_min_to = 1000;      /* Number of microsecond  min timeout */
212 static int32_t rack_verbose_logging = 0;
213 static int32_t rack_ignore_data_after_close = 1;
214 static int32_t rack_enable_shared_cwnd = 1;
215 static int32_t rack_use_cmp_acks = 1;
216 static int32_t rack_use_fsb = 1;
217 static int32_t rack_use_rfo = 1;
218 static int32_t rack_use_rsm_rfo = 1;
219 static int32_t rack_max_abc_post_recovery = 2;
220 static int32_t rack_client_low_buf = 0;
221 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 */
222 #ifdef TCP_ACCOUNTING
223 static int32_t rack_tcp_accounting = 0;
224 #endif
225 static int32_t rack_limits_scwnd = 1;
226 static int32_t rack_enable_mqueue_for_nonpaced = 0;
227 static int32_t rack_disable_prr = 0;
228 static int32_t use_rack_rr = 1;
229 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
230 static int32_t rack_persist_min = 250000;       /* 250usec */
231 static int32_t rack_persist_max = 2000000;      /* 2 Second in usec's */
232 static int32_t rack_sack_not_required = 1;      /* set to one to allow non-sack to use rack */
233 static int32_t rack_default_init_window = 0;    /* Use system default */
234 static int32_t rack_limit_time_with_srtt = 0;
235 static int32_t rack_autosndbuf_inc = 20;        /* In percentage form */
236 static int32_t rack_enobuf_hw_boost_mult = 2;   /* How many times the hw rate we boost slot using time_between */
237 static int32_t rack_enobuf_hw_max = 12000;      /* 12 ms in usecs */
238 static int32_t rack_enobuf_hw_min = 10000;      /* 10 ms in usecs */
239 static int32_t rack_hw_rwnd_factor = 2;         /* How many max_segs the rwnd must be before we hold off sending */
240 /*
241  * Currently regular tcp has a rto_min of 30ms
242  * the backoff goes 12 times so that ends up
243  * being a total of 122.850 seconds before a
244  * connection is killed.
245  */
246 static uint32_t rack_def_data_window = 20;
247 static uint32_t rack_goal_bdp = 2;
248 static uint32_t rack_min_srtts = 1;
249 static uint32_t rack_min_measure_usec = 0;
250 static int32_t rack_tlp_min = 10000;    /* 10ms */
251 static int32_t rack_rto_min = 30000;    /* 30,000 usec same as main freebsd */
252 static int32_t rack_rto_max = 4000000;  /* 4 seconds in usec's */
253 static const int32_t rack_free_cache = 2;
254 static int32_t rack_hptsi_segments = 40;
255 static int32_t rack_rate_sample_method = USE_RTT_LOW;
256 static int32_t rack_pace_every_seg = 0;
257 static int32_t rack_delayed_ack_time = 40000;   /* 40ms in usecs */
258 static int32_t rack_slot_reduction = 4;
259 static int32_t rack_wma_divisor = 8;            /* For WMA calculation */
260 static int32_t rack_cwnd_block_ends_measure = 0;
261 static int32_t rack_rwnd_block_ends_measure = 0;
262 static int32_t rack_def_profile = 0;
263
264 static int32_t rack_lower_cwnd_at_tlp = 0;
265 static int32_t rack_limited_retran = 0;
266 static int32_t rack_always_send_oldest = 0;
267 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
268
269 static uint16_t rack_per_of_gp_ss = 250;        /* 250 % slow-start */
270 static uint16_t rack_per_of_gp_ca = 200;        /* 200 % congestion-avoidance */
271 static uint16_t rack_per_of_gp_rec = 200;       /* 200 % of bw */
272
273 /* Probertt */
274 static uint16_t rack_per_of_gp_probertt = 60;   /* 60% of bw */
275 static uint16_t rack_per_of_gp_lowthresh = 40;  /* 40% is bottom */
276 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
277 static uint16_t rack_atexit_prtt_hbp = 130;     /* Clamp to 130% on exit prtt if highly buffered path */
278 static uint16_t rack_atexit_prtt = 130; /* Clamp to 100% on exit prtt if non highly buffered path */
279
280 static uint32_t rack_max_drain_wait = 2;        /* How man gp srtt's before we give up draining */
281 static uint32_t rack_must_drain = 1;            /* How many GP srtt's we *must* wait */
282 static uint32_t rack_probertt_use_min_rtt_entry = 1;    /* Use the min to calculate the goal else gp_srtt */
283 static uint32_t rack_probertt_use_min_rtt_exit = 0;
284 static uint32_t rack_probe_rtt_sets_cwnd = 0;
285 static uint32_t rack_probe_rtt_safety_val = 2000000;    /* No more than 2 sec in probe-rtt */
286 static uint32_t rack_time_between_probertt = 9600000;   /* 9.6 sec in usecs */
287 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0;       /* How many srtt periods does probe-rtt last top fraction */
288 static uint32_t rack_probertt_gpsrtt_cnt_div = 0;       /* How many srtt periods does probe-rtt last bottom fraction */
289 static uint32_t rack_min_probertt_hold = 40000;         /* Equal to delayed ack time */
290 static uint32_t rack_probertt_filter_life = 10000000;
291 static uint32_t rack_probertt_lower_within = 10;
292 static uint32_t rack_min_rtt_movement = 250000; /* Must move at least 250ms (in microseconds)  to count as a lowering */
293 static int32_t rack_pace_one_seg = 0;           /* Shall we pace for less than 1.4Meg 1MSS at a time */
294 static int32_t rack_probertt_clear_is = 1;
295 static int32_t rack_max_drain_hbp = 1;          /* Extra drain times gpsrtt for highly buffered paths */
296 static int32_t rack_hbp_thresh = 3;             /* what is the divisor max_rtt/min_rtt to decided a hbp */
297
298 /* Part of pacing */
299 static int32_t rack_max_per_above = 30;         /* When we go to increment stop if above 100+this% */
300
301 /* Timely information */
302 /* Combine these two gives the range of 'no change' to bw */
303 /* ie the up/down provide the upper and lower bound */
304 static int32_t rack_gp_per_bw_mul_up = 2;       /* 2% */
305 static int32_t rack_gp_per_bw_mul_down = 4;     /* 4% */
306 static int32_t rack_gp_rtt_maxmul = 3;          /* 3 x maxmin */
307 static int32_t rack_gp_rtt_minmul = 1;          /* minrtt + (minrtt/mindiv) is lower rtt */
308 static int32_t rack_gp_rtt_mindiv = 4;          /* minrtt + (minrtt * minmul/mindiv) is lower rtt */
309 static int32_t rack_gp_decrease_per = 20;       /* 20% decrease in multipler */
310 static int32_t rack_gp_increase_per = 2;        /* 2% increase in multipler */
311 static int32_t rack_per_lower_bound = 50;       /* Don't allow to drop below this multiplier */
312 static int32_t rack_per_upper_bound_ss = 0;     /* Don't allow SS to grow above this */
313 static int32_t rack_per_upper_bound_ca = 0;     /* Don't allow CA to grow above this */
314 static int32_t rack_do_dyn_mul = 0;             /* Are the rack gp multipliers dynamic */
315 static int32_t rack_gp_no_rec_chg = 1;          /* Prohibit recovery from reducing it's multiplier */
316 static int32_t rack_timely_dec_clear = 6;       /* Do we clear decrement count at a value (6)? */
317 static int32_t rack_timely_max_push_rise = 3;   /* One round of pushing */
318 static int32_t rack_timely_max_push_drop = 3;   /* Three round of pushing */
319 static int32_t rack_timely_min_segs = 4;        /* 4 segment minimum */
320 static int32_t rack_use_max_for_nobackoff = 0;
321 static int32_t rack_timely_int_timely_only = 0; /* do interim timely's only use the timely algo (no b/w changes)? */
322 static int32_t rack_timely_no_stopping = 0;
323 static int32_t rack_down_raise_thresh = 100;
324 static int32_t rack_req_segs = 1;
325 static uint64_t rack_bw_rate_cap = 0;
326
327 /* Weird delayed ack mode */
328 static int32_t rack_use_imac_dack = 0;
329 /* Rack specific counters */
330 counter_u64_t rack_badfr;
331 counter_u64_t rack_badfr_bytes;
332 counter_u64_t rack_rtm_prr_retran;
333 counter_u64_t rack_rtm_prr_newdata;
334 counter_u64_t rack_timestamp_mismatch;
335 counter_u64_t rack_reorder_seen;
336 counter_u64_t rack_paced_segments;
337 counter_u64_t rack_unpaced_segments;
338 counter_u64_t rack_calc_zero;
339 counter_u64_t rack_calc_nonzero;
340 counter_u64_t rack_saw_enobuf;
341 counter_u64_t rack_saw_enobuf_hw;
342 counter_u64_t rack_saw_enetunreach;
343 counter_u64_t rack_per_timer_hole;
344 counter_u64_t rack_large_ackcmp;
345 counter_u64_t rack_small_ackcmp;
346 #ifdef INVARIANTS
347 counter_u64_t rack_adjust_map_bw;
348 #endif
349 /* Tail loss probe counters */
350 counter_u64_t rack_tlp_tot;
351 counter_u64_t rack_tlp_newdata;
352 counter_u64_t rack_tlp_retran;
353 counter_u64_t rack_tlp_retran_bytes;
354 counter_u64_t rack_tlp_retran_fail;
355 counter_u64_t rack_to_tot;
356 counter_u64_t rack_to_arm_rack;
357 counter_u64_t rack_to_arm_tlp;
358 counter_u64_t rack_hot_alloc;
359 counter_u64_t rack_to_alloc;
360 counter_u64_t rack_to_alloc_hard;
361 counter_u64_t rack_to_alloc_emerg;
362 counter_u64_t rack_to_alloc_limited;
363 counter_u64_t rack_alloc_limited_conns;
364 counter_u64_t rack_split_limited;
365
366 #define MAX_NUM_OF_CNTS 13
367 counter_u64_t rack_proc_comp_ack[MAX_NUM_OF_CNTS];
368 counter_u64_t rack_multi_single_eq;
369 counter_u64_t rack_proc_non_comp_ack;
370
371 counter_u64_t rack_fto_send;
372 counter_u64_t rack_fto_rsm_send;
373 counter_u64_t rack_nfto_resend;
374 counter_u64_t rack_non_fto_send;
375 counter_u64_t rack_extended_rfo;
376
377 counter_u64_t rack_sack_proc_all;
378 counter_u64_t rack_sack_proc_short;
379 counter_u64_t rack_sack_proc_restart;
380 counter_u64_t rack_sack_attacks_detected;
381 counter_u64_t rack_sack_attacks_reversed;
382 counter_u64_t rack_sack_used_next_merge;
383 counter_u64_t rack_sack_splits;
384 counter_u64_t rack_sack_used_prev_merge;
385 counter_u64_t rack_sack_skipped_acked;
386 counter_u64_t rack_ack_total;
387 counter_u64_t rack_express_sack;
388 counter_u64_t rack_sack_total;
389 counter_u64_t rack_move_none;
390 counter_u64_t rack_move_some;
391
392 counter_u64_t rack_used_tlpmethod;
393 counter_u64_t rack_used_tlpmethod2;
394 counter_u64_t rack_enter_tlp_calc;
395 counter_u64_t rack_input_idle_reduces;
396 counter_u64_t rack_collapsed_win;
397 counter_u64_t rack_tlp_does_nada;
398 counter_u64_t rack_try_scwnd;
399 counter_u64_t rack_hw_pace_init_fail;
400 counter_u64_t rack_hw_pace_lost;
401 counter_u64_t rack_sbsndptr_right;
402 counter_u64_t rack_sbsndptr_wrong;
403
404 /* Temp CPU counters */
405 counter_u64_t rack_find_high;
406
407 counter_u64_t rack_progress_drops;
408 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
409 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
410
411
412 #define RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
413
414 #define RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do {  \
415         (tv) = (value) + slop;   \
416         if ((u_long)(tv) < (u_long)(tvmin)) \
417                 (tv) = (tvmin); \
418         if ((u_long)(tv) > (u_long)(tvmax)) \
419                 (tv) = (tvmax); \
420 } while (0)
421
422 static void
423 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line);
424
425 static int
426 rack_process_ack(struct mbuf *m, struct tcphdr *th,
427     struct socket *so, struct tcpcb *tp, struct tcpopt *to,
428     uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
429 static int
430 rack_process_data(struct mbuf *m, struct tcphdr *th,
431     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
432     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
433 static void
434 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
435    uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
436 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
437 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
438     uint8_t limit_type);
439 static struct rack_sendmap *
440 rack_check_recovery_mode(struct tcpcb *tp,
441     uint32_t tsused);
442 static void
443 rack_cong_signal(struct tcpcb *tp,
444                  uint32_t type, uint32_t ack);
445 static void rack_counter_destroy(void);
446 static int
447 rack_ctloutput(struct socket *so, struct sockopt *sopt,
448     struct inpcb *inp, struct tcpcb *tp);
449 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
450 static void
451 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
452 static void
453 rack_do_segment(struct mbuf *m, struct tcphdr *th,
454     struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
455     uint8_t iptos);
456 static void rack_dtor(void *mem, int32_t size, void *arg);
457 static void
458 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
459     uint32_t flex1, uint32_t flex2,
460     uint32_t flex3, uint32_t flex4,
461     uint32_t flex5, uint32_t flex6,
462     uint16_t flex7, uint8_t mod);
463
464 static void
465 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
466    uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
467    struct rack_sendmap *rsm, uint8_t quality);
468 static struct rack_sendmap *
469 rack_find_high_nonack(struct tcp_rack *rack,
470     struct rack_sendmap *rsm);
471 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
472 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
473 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
474 static int
475 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
476     struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
477 static void
478 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
479                             tcp_seq th_ack, int line, uint8_t quality);
480 static uint32_t
481 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
482 static int32_t rack_handoff_ok(struct tcpcb *tp);
483 static int32_t rack_init(struct tcpcb *tp);
484 static void rack_init_sysctls(void);
485 static void
486 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
487     struct tcphdr *th, int entered_rec, int dup_ack_struck);
488 static void
489 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
490     uint32_t seq_out, uint8_t th_flags, int32_t err, uint64_t ts,
491     struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls);
492
493 static void
494 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
495     struct rack_sendmap *rsm);
496 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
497 static int32_t rack_output(struct tcpcb *tp);
498
499 static uint32_t
500 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
501     struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
502     uint32_t cts, int *moved_two);
503 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
504 static void rack_remxt_tmr(struct tcpcb *tp);
505 static int
506 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
507     struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
508 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
509 static int32_t rack_stopall(struct tcpcb *tp);
510 static void
511 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
512     uint32_t delta);
513 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type);
514 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
515 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type);
516 static uint32_t
517 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
518     struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint16_t add_flag);
519 static void
520 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
521     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag);
522 static int
523 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
524     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
525 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
526 static int
527 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
528     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
529     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
530 static int
531 rack_do_closing(struct mbuf *m, struct tcphdr *th,
532     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
533     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
534 static int
535 rack_do_established(struct mbuf *m, struct tcphdr *th,
536     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
537     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
538 static int
539 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
540     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
541     int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
542 static int
543 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
544     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
545     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
546 static int
547 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
548     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
549     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
550 static int
551 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
552     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
553     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
554 static int
555 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
556     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
557     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
558 static int
559 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
560     struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
561     int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
562 struct rack_sendmap *
563 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
564     uint32_t tsused);
565 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
566     uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
567 static void
568      tcp_rack_partialack(struct tcpcb *tp);
569 static int
570 rack_set_profile(struct tcp_rack *rack, int prof);
571 static void
572 rack_apply_deferred_options(struct tcp_rack *rack);
573
574 int32_t rack_clear_counter=0;
575
576 static void
577 rack_set_cc_pacing(struct tcp_rack *rack)
578 {
579         struct sockopt sopt;
580         struct cc_newreno_opts opt;
581         struct newreno old, *ptr;
582         struct tcpcb *tp;
583         int error;
584
585         if (rack->rc_pacing_cc_set)
586                 return;
587
588         tp = rack->rc_tp;
589         if (tp->cc_algo == NULL) {
590                 /* Tcb is leaving */
591                 printf("No cc algorithm?\n");
592                 return;
593         }
594         rack->rc_pacing_cc_set = 1;
595         if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
596                 /* Not new-reno we can't play games with beta! */
597                 goto out;
598         }
599         ptr = ((struct newreno *)tp->ccv->cc_data);
600         if (CC_ALGO(tp)->ctl_output == NULL)  {
601                 /* Huh, why does new_reno no longer have a set function? */
602                 printf("no ctl_output for algo:%s\n", tp->cc_algo->name);
603                 goto out;
604         }
605         if (ptr == NULL) {
606                 /* Just the default values */
607                 old.beta = V_newreno_beta_ecn;
608                 old.beta_ecn = V_newreno_beta_ecn;
609                 old.newreno_flags = 0;
610         } else {
611                 old.beta = ptr->beta;
612                 old.beta_ecn = ptr->beta_ecn;
613                 old.newreno_flags = ptr->newreno_flags;
614         }
615         sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
616         sopt.sopt_dir = SOPT_SET;
617         opt.name = CC_NEWRENO_BETA;
618         opt.val = rack->r_ctl.rc_saved_beta.beta;
619         error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
620         if (error)  {
621                 printf("Error returned by ctl_output %d\n", error);
622                 goto out;
623         }
624         /*
625          * Hack alert we need to set in our newreno_flags
626          * so that Abe behavior is also applied.
627          */
628         ((struct newreno *)tp->ccv->cc_data)->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
629         opt.name = CC_NEWRENO_BETA_ECN;
630         opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
631         error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
632         if (error) {
633                 printf("Error returned by ctl_output %d\n", error);
634                 goto out;
635         }
636         /* Save off the original values for restoral */
637         memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
638 out:
639         if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
640                 union tcp_log_stackspecific log;
641                 struct timeval tv;
642
643                 ptr = ((struct newreno *)tp->ccv->cc_data);
644                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
645                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
646                 if (ptr) {
647                         log.u_bbr.flex1 = ptr->beta;
648                         log.u_bbr.flex2 = ptr->beta_ecn;
649                         log.u_bbr.flex3 = ptr->newreno_flags;
650                 }
651                 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
652                 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
653                 log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
654                 log.u_bbr.flex7 = rack->gp_ready;
655                 log.u_bbr.flex7 <<= 1;
656                 log.u_bbr.flex7 |= rack->use_fixed_rate;
657                 log.u_bbr.flex7 <<= 1;
658                 log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
659                 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
660                 log.u_bbr.flex8 = 3;
661                 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
662                                0, &log, false, NULL, NULL, 0, &tv);
663         }
664 }
665
666 static void
667 rack_undo_cc_pacing(struct tcp_rack *rack)
668 {
669         struct newreno old, *ptr;
670         struct tcpcb *tp;
671
672         if (rack->rc_pacing_cc_set == 0)
673                 return;
674         tp = rack->rc_tp;
675         rack->rc_pacing_cc_set = 0;
676         if (tp->cc_algo == NULL)
677                 /* Tcb is leaving */
678                 return;
679         if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
680                 /* Not new-reno nothing to do! */
681                 return;
682         }
683         ptr = ((struct newreno *)tp->ccv->cc_data);
684         if (ptr == NULL) {
685                 /*
686                  * This happens at rack_fini() if the
687                  * cc module gets freed on us. In that
688                  * case we loose our "new" settings but
689                  * thats ok, since the tcb is going away anyway.
690                  */
691                 return;
692         }
693         /* Grab out our set values */
694         memcpy(&old, ptr, sizeof(struct newreno));
695         /* Copy back in the original values */
696         memcpy(ptr, &rack->r_ctl.rc_saved_beta, sizeof(struct newreno));
697         /* Now save back the values we had set in (for when pacing is restored) */
698         memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
699         if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
700                 union tcp_log_stackspecific log;
701                 struct timeval tv;
702
703                 ptr = ((struct newreno *)tp->ccv->cc_data);
704                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
705                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
706                 log.u_bbr.flex1 = ptr->beta;
707                 log.u_bbr.flex2 = ptr->beta_ecn;
708                 log.u_bbr.flex3 = ptr->newreno_flags;
709                 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
710                 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
711                 log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags;
712                 log.u_bbr.flex7 = rack->gp_ready;
713                 log.u_bbr.flex7 <<= 1;
714                 log.u_bbr.flex7 |= rack->use_fixed_rate;
715                 log.u_bbr.flex7 <<= 1;
716                 log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
717                 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
718                 log.u_bbr.flex8 = 4;
719                 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
720                                0, &log, false, NULL, NULL, 0, &tv);
721         }
722 }
723
724 #ifdef NETFLIX_PEAKRATE
725 static inline void
726 rack_update_peakrate_thr(struct tcpcb *tp)
727 {
728         /* Keep in mind that t_maxpeakrate is in B/s. */
729         uint64_t peak;
730         peak = uqmax((tp->t_maxseg * 2),
731                      (((uint64_t)tp->t_maxpeakrate * (uint64_t)(tp->t_srtt)) / (uint64_t)HPTS_USEC_IN_SEC));
732         tp->t_peakrate_thr = (uint32_t)uqmin(peak, UINT32_MAX);
733 }
734 #endif
735
736 static int
737 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
738 {
739         uint32_t stat;
740         int32_t error;
741         int i;
742
743         error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
744         if (error || req->newptr == NULL)
745                 return error;
746
747         error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
748         if (error)
749                 return (error);
750         if (stat == 1) {
751 #ifdef INVARIANTS
752                 printf("Clearing RACK counters\n");
753 #endif
754                 counter_u64_zero(rack_badfr);
755                 counter_u64_zero(rack_badfr_bytes);
756                 counter_u64_zero(rack_rtm_prr_retran);
757                 counter_u64_zero(rack_rtm_prr_newdata);
758                 counter_u64_zero(rack_timestamp_mismatch);
759                 counter_u64_zero(rack_reorder_seen);
760                 counter_u64_zero(rack_tlp_tot);
761                 counter_u64_zero(rack_tlp_newdata);
762                 counter_u64_zero(rack_tlp_retran);
763                 counter_u64_zero(rack_tlp_retran_bytes);
764                 counter_u64_zero(rack_tlp_retran_fail);
765                 counter_u64_zero(rack_to_tot);
766                 counter_u64_zero(rack_to_arm_rack);
767                 counter_u64_zero(rack_to_arm_tlp);
768                 counter_u64_zero(rack_paced_segments);
769                 counter_u64_zero(rack_calc_zero);
770                 counter_u64_zero(rack_calc_nonzero);
771                 counter_u64_zero(rack_unpaced_segments);
772                 counter_u64_zero(rack_saw_enobuf);
773                 counter_u64_zero(rack_saw_enobuf_hw);
774                 counter_u64_zero(rack_saw_enetunreach);
775                 counter_u64_zero(rack_per_timer_hole);
776                 counter_u64_zero(rack_large_ackcmp);
777                 counter_u64_zero(rack_small_ackcmp);
778 #ifdef INVARIANTS
779                 counter_u64_zero(rack_adjust_map_bw);
780 #endif
781                 counter_u64_zero(rack_to_alloc_hard);
782                 counter_u64_zero(rack_to_alloc_emerg);
783                 counter_u64_zero(rack_sack_proc_all);
784                 counter_u64_zero(rack_fto_send);
785                 counter_u64_zero(rack_fto_rsm_send);
786                 counter_u64_zero(rack_extended_rfo);
787                 counter_u64_zero(rack_hw_pace_init_fail);
788                 counter_u64_zero(rack_hw_pace_lost);
789                 counter_u64_zero(rack_sbsndptr_wrong);
790                 counter_u64_zero(rack_sbsndptr_right);
791                 counter_u64_zero(rack_non_fto_send);
792                 counter_u64_zero(rack_nfto_resend);
793                 counter_u64_zero(rack_sack_proc_short);
794                 counter_u64_zero(rack_sack_proc_restart);
795                 counter_u64_zero(rack_to_alloc);
796                 counter_u64_zero(rack_to_alloc_limited);
797                 counter_u64_zero(rack_alloc_limited_conns);
798                 counter_u64_zero(rack_split_limited);
799                 for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
800                         counter_u64_zero(rack_proc_comp_ack[i]);
801                 }
802                 counter_u64_zero(rack_multi_single_eq);
803                 counter_u64_zero(rack_proc_non_comp_ack);
804                 counter_u64_zero(rack_find_high);
805                 counter_u64_zero(rack_sack_attacks_detected);
806                 counter_u64_zero(rack_sack_attacks_reversed);
807                 counter_u64_zero(rack_sack_used_next_merge);
808                 counter_u64_zero(rack_sack_used_prev_merge);
809                 counter_u64_zero(rack_sack_splits);
810                 counter_u64_zero(rack_sack_skipped_acked);
811                 counter_u64_zero(rack_ack_total);
812                 counter_u64_zero(rack_express_sack);
813                 counter_u64_zero(rack_sack_total);
814                 counter_u64_zero(rack_move_none);
815                 counter_u64_zero(rack_move_some);
816                 counter_u64_zero(rack_used_tlpmethod);
817                 counter_u64_zero(rack_used_tlpmethod2);
818                 counter_u64_zero(rack_enter_tlp_calc);
819                 counter_u64_zero(rack_progress_drops);
820                 counter_u64_zero(rack_tlp_does_nada);
821                 counter_u64_zero(rack_try_scwnd);
822                 counter_u64_zero(rack_collapsed_win);
823         }
824         rack_clear_counter = 0;
825         return (0);
826 }
827
828 static void
829 rack_init_sysctls(void)
830 {
831         int i;
832         struct sysctl_oid *rack_counters;
833         struct sysctl_oid *rack_attack;
834         struct sysctl_oid *rack_pacing;
835         struct sysctl_oid *rack_timely;
836         struct sysctl_oid *rack_timers;
837         struct sysctl_oid *rack_tlp;
838         struct sysctl_oid *rack_misc;
839         struct sysctl_oid *rack_features;
840         struct sysctl_oid *rack_measure;
841         struct sysctl_oid *rack_probertt;
842         struct sysctl_oid *rack_hw_pacing;
843
844         rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
845             SYSCTL_CHILDREN(rack_sysctl_root),
846             OID_AUTO,
847             "sack_attack",
848             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
849             "Rack Sack Attack Counters and Controls");
850         rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
851             SYSCTL_CHILDREN(rack_sysctl_root),
852             OID_AUTO,
853             "stats",
854             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
855             "Rack Counters");
856         SYSCTL_ADD_S32(&rack_sysctl_ctx,
857             SYSCTL_CHILDREN(rack_sysctl_root),
858             OID_AUTO, "rate_sample_method", CTLFLAG_RW,
859             &rack_rate_sample_method , USE_RTT_LOW,
860             "What method should we use for rate sampling 0=high, 1=low ");
861         /* Probe rtt related controls */
862         rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
863             SYSCTL_CHILDREN(rack_sysctl_root),
864             OID_AUTO,
865             "probertt",
866             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
867             "ProbeRTT related Controls");
868         SYSCTL_ADD_U16(&rack_sysctl_ctx,
869             SYSCTL_CHILDREN(rack_probertt),
870             OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
871             &rack_atexit_prtt_hbp, 130,
872             "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
873         SYSCTL_ADD_U16(&rack_sysctl_ctx,
874             SYSCTL_CHILDREN(rack_probertt),
875             OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
876             &rack_atexit_prtt, 130,
877             "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
878         SYSCTL_ADD_U16(&rack_sysctl_ctx,
879             SYSCTL_CHILDREN(rack_probertt),
880             OID_AUTO, "gp_per_mul", CTLFLAG_RW,
881             &rack_per_of_gp_probertt, 60,
882             "What percentage of goodput do we pace at in probertt");
883         SYSCTL_ADD_U16(&rack_sysctl_ctx,
884             SYSCTL_CHILDREN(rack_probertt),
885             OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
886             &rack_per_of_gp_probertt_reduce, 10,
887             "What percentage of goodput do we reduce every gp_srtt");
888         SYSCTL_ADD_U16(&rack_sysctl_ctx,
889             SYSCTL_CHILDREN(rack_probertt),
890             OID_AUTO, "gp_per_low", CTLFLAG_RW,
891             &rack_per_of_gp_lowthresh, 40,
892             "What percentage of goodput do we allow the multiplier to fall to");
893         SYSCTL_ADD_U32(&rack_sysctl_ctx,
894             SYSCTL_CHILDREN(rack_probertt),
895             OID_AUTO, "time_between", CTLFLAG_RW,
896             & rack_time_between_probertt, 96000000,
897             "How many useconds between the lowest rtt falling must past before we enter probertt");
898         SYSCTL_ADD_U32(&rack_sysctl_ctx,
899             SYSCTL_CHILDREN(rack_probertt),
900             OID_AUTO, "safety", CTLFLAG_RW,
901             &rack_probe_rtt_safety_val, 2000000,
902             "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
903         SYSCTL_ADD_U32(&rack_sysctl_ctx,
904             SYSCTL_CHILDREN(rack_probertt),
905             OID_AUTO, "sets_cwnd", CTLFLAG_RW,
906             &rack_probe_rtt_sets_cwnd, 0,
907             "Do we set the cwnd too (if always_lower is on)");
908         SYSCTL_ADD_U32(&rack_sysctl_ctx,
909             SYSCTL_CHILDREN(rack_probertt),
910             OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
911             &rack_max_drain_wait, 2,
912             "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
913         SYSCTL_ADD_U32(&rack_sysctl_ctx,
914             SYSCTL_CHILDREN(rack_probertt),
915             OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
916             &rack_must_drain, 1,
917             "We must drain this many gp_srtt's waiting for flight to reach goal");
918         SYSCTL_ADD_U32(&rack_sysctl_ctx,
919             SYSCTL_CHILDREN(rack_probertt),
920             OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
921             &rack_probertt_use_min_rtt_entry, 1,
922             "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
923         SYSCTL_ADD_U32(&rack_sysctl_ctx,
924             SYSCTL_CHILDREN(rack_probertt),
925             OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
926             &rack_probertt_use_min_rtt_exit, 0,
927             "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
928         SYSCTL_ADD_U32(&rack_sysctl_ctx,
929             SYSCTL_CHILDREN(rack_probertt),
930             OID_AUTO, "length_div", CTLFLAG_RW,
931             &rack_probertt_gpsrtt_cnt_div, 0,
932             "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
933         SYSCTL_ADD_U32(&rack_sysctl_ctx,
934             SYSCTL_CHILDREN(rack_probertt),
935             OID_AUTO, "length_mul", CTLFLAG_RW,
936             &rack_probertt_gpsrtt_cnt_mul, 0,
937             "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
938         SYSCTL_ADD_U32(&rack_sysctl_ctx,
939             SYSCTL_CHILDREN(rack_probertt),
940             OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
941             &rack_min_probertt_hold, 200000,
942             "What is the minimum time we hold probertt at target");
943         SYSCTL_ADD_U32(&rack_sysctl_ctx,
944             SYSCTL_CHILDREN(rack_probertt),
945             OID_AUTO, "filter_life", CTLFLAG_RW,
946             &rack_probertt_filter_life, 10000000,
947             "What is the time for the filters life in useconds");
948         SYSCTL_ADD_U32(&rack_sysctl_ctx,
949             SYSCTL_CHILDREN(rack_probertt),
950             OID_AUTO, "lower_within", CTLFLAG_RW,
951             &rack_probertt_lower_within, 10,
952             "If the rtt goes lower within this percentage of the time, go into probe-rtt");
953         SYSCTL_ADD_U32(&rack_sysctl_ctx,
954             SYSCTL_CHILDREN(rack_probertt),
955             OID_AUTO, "must_move", CTLFLAG_RW,
956             &rack_min_rtt_movement, 250,
957             "How much is the minimum movement in rtt to count as a drop for probertt purposes");
958         SYSCTL_ADD_U32(&rack_sysctl_ctx,
959             SYSCTL_CHILDREN(rack_probertt),
960             OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
961             &rack_probertt_clear_is, 1,
962             "Do we clear I/S counts on exiting probe-rtt");
963         SYSCTL_ADD_S32(&rack_sysctl_ctx,
964             SYSCTL_CHILDREN(rack_probertt),
965             OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
966             &rack_max_drain_hbp, 1,
967             "How many extra drain gpsrtt's do we get in highly buffered paths");
968         SYSCTL_ADD_S32(&rack_sysctl_ctx,
969             SYSCTL_CHILDREN(rack_probertt),
970             OID_AUTO, "hbp_threshold", CTLFLAG_RW,
971             &rack_hbp_thresh, 3,
972             "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
973         /* Pacing related sysctls */
974         rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
975             SYSCTL_CHILDREN(rack_sysctl_root),
976             OID_AUTO,
977             "pacing",
978             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
979             "Pacing related Controls");
980         SYSCTL_ADD_S32(&rack_sysctl_ctx,
981             SYSCTL_CHILDREN(rack_pacing),
982             OID_AUTO, "max_pace_over", CTLFLAG_RW,
983             &rack_max_per_above, 30,
984             "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
985         SYSCTL_ADD_S32(&rack_sysctl_ctx,
986             SYSCTL_CHILDREN(rack_pacing),
987             OID_AUTO, "pace_to_one", CTLFLAG_RW,
988             &rack_pace_one_seg, 0,
989             "Do we allow low b/w pacing of 1MSS instead of two");
990         SYSCTL_ADD_S32(&rack_sysctl_ctx,
991             SYSCTL_CHILDREN(rack_pacing),
992             OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
993             &rack_limit_time_with_srtt, 0,
994             "Do we limit pacing time based on srtt");
995         SYSCTL_ADD_S32(&rack_sysctl_ctx,
996             SYSCTL_CHILDREN(rack_pacing),
997             OID_AUTO, "init_win", CTLFLAG_RW,
998             &rack_default_init_window, 0,
999             "Do we have a rack initial window 0 = system default");
1000         SYSCTL_ADD_U16(&rack_sysctl_ctx,
1001             SYSCTL_CHILDREN(rack_pacing),
1002             OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1003             &rack_per_of_gp_ss, 250,
1004             "If non zero, what percentage of goodput to pace at in slow start");
1005         SYSCTL_ADD_U16(&rack_sysctl_ctx,
1006             SYSCTL_CHILDREN(rack_pacing),
1007             OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1008             &rack_per_of_gp_ca, 150,
1009             "If non zero, what percentage of goodput to pace at in congestion avoidance");
1010         SYSCTL_ADD_U16(&rack_sysctl_ctx,
1011             SYSCTL_CHILDREN(rack_pacing),
1012             OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1013             &rack_per_of_gp_rec, 200,
1014             "If non zero, what percentage of goodput to pace at in recovery");
1015         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1016             SYSCTL_CHILDREN(rack_pacing),
1017             OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1018             &rack_hptsi_segments, 40,
1019             "What size is the max for TSO segments in pacing and burst mitigation");
1020         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1021             SYSCTL_CHILDREN(rack_pacing),
1022             OID_AUTO, "burst_reduces", CTLFLAG_RW,
1023             &rack_slot_reduction, 4,
1024             "When doing only burst mitigation what is the reduce divisor");
1025         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1026             SYSCTL_CHILDREN(rack_sysctl_root),
1027             OID_AUTO, "use_pacing", CTLFLAG_RW,
1028             &rack_pace_every_seg, 0,
1029             "If set we use pacing, if clear we use only the original burst mitigation");
1030         SYSCTL_ADD_U64(&rack_sysctl_ctx,
1031             SYSCTL_CHILDREN(rack_pacing),
1032             OID_AUTO, "rate_cap", CTLFLAG_RW,
1033             &rack_bw_rate_cap, 0,
1034             "If set we apply this value to the absolute rate cap used by pacing");
1035         SYSCTL_ADD_U8(&rack_sysctl_ctx,
1036             SYSCTL_CHILDREN(rack_sysctl_root),
1037             OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1038             &rack_req_measurements, 1,
1039             "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1040         /* Hardware pacing */
1041         rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1042             SYSCTL_CHILDREN(rack_sysctl_root),
1043             OID_AUTO,
1044             "hdwr_pacing",
1045             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1046             "Pacing related Controls");
1047         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1048             SYSCTL_CHILDREN(rack_hw_pacing),
1049             OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1050             &rack_hw_rwnd_factor, 2,
1051             "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1052         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1053             SYSCTL_CHILDREN(rack_hw_pacing),
1054             OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1055             &rack_enobuf_hw_boost_mult, 2,
1056             "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1057         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1058             SYSCTL_CHILDREN(rack_hw_pacing),
1059             OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1060             &rack_enobuf_hw_max, 2,
1061             "What is the max boost the pacing time if we see a ENOBUFS?");
1062         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1063             SYSCTL_CHILDREN(rack_hw_pacing),
1064             OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1065             &rack_enobuf_hw_min, 2,
1066             "What is the min boost the pacing time if we see a ENOBUFS?");
1067         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1068             SYSCTL_CHILDREN(rack_hw_pacing),
1069             OID_AUTO, "enable", CTLFLAG_RW,
1070             &rack_enable_hw_pacing, 0,
1071             "Should RACK attempt to use hw pacing?");
1072         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1073             SYSCTL_CHILDREN(rack_hw_pacing),
1074             OID_AUTO, "rate_cap", CTLFLAG_RW,
1075             &rack_hw_rate_caps, 1,
1076             "Does the highest hardware pacing rate cap the rate we will send at??");
1077         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1078             SYSCTL_CHILDREN(rack_hw_pacing),
1079             OID_AUTO, "rate_min", CTLFLAG_RW,
1080             &rack_hw_rate_min, 0,
1081             "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1082         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1083             SYSCTL_CHILDREN(rack_hw_pacing),
1084             OID_AUTO, "rate_to_low", CTLFLAG_RW,
1085             &rack_hw_rate_to_low, 0,
1086             "If we fall below this rate, dis-engage hw pacing?");
1087         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1088             SYSCTL_CHILDREN(rack_hw_pacing),
1089             OID_AUTO, "up_only", CTLFLAG_RW,
1090             &rack_hw_up_only, 1,
1091             "Do we allow hw pacing to lower the rate selected?");
1092         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1093             SYSCTL_CHILDREN(rack_hw_pacing),
1094             OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1095             &rack_hw_pace_extra_slots, 2,
1096             "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1097         rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1098             SYSCTL_CHILDREN(rack_sysctl_root),
1099             OID_AUTO,
1100             "timely",
1101             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1102             "Rack Timely RTT Controls");
1103         /* Timely based GP dynmics */
1104         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1105             SYSCTL_CHILDREN(rack_timely),
1106             OID_AUTO, "upper", CTLFLAG_RW,
1107             &rack_gp_per_bw_mul_up, 2,
1108             "Rack timely upper range for equal b/w (in percentage)");
1109         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1110             SYSCTL_CHILDREN(rack_timely),
1111             OID_AUTO, "lower", CTLFLAG_RW,
1112             &rack_gp_per_bw_mul_down, 4,
1113             "Rack timely lower range for equal b/w (in percentage)");
1114         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1115             SYSCTL_CHILDREN(rack_timely),
1116             OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1117             &rack_gp_rtt_maxmul, 3,
1118             "Rack timely multipler of lowest rtt for rtt_max");
1119         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1120             SYSCTL_CHILDREN(rack_timely),
1121             OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1122             &rack_gp_rtt_mindiv, 4,
1123             "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1124         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1125             SYSCTL_CHILDREN(rack_timely),
1126             OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1127             &rack_gp_rtt_minmul, 1,
1128             "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1129         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1130             SYSCTL_CHILDREN(rack_timely),
1131             OID_AUTO, "decrease", CTLFLAG_RW,
1132             &rack_gp_decrease_per, 20,
1133             "Rack timely decrease percentage of our GP multiplication factor");
1134         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1135             SYSCTL_CHILDREN(rack_timely),
1136             OID_AUTO, "increase", CTLFLAG_RW,
1137             &rack_gp_increase_per, 2,
1138             "Rack timely increase perentage of our GP multiplication factor");
1139         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1140             SYSCTL_CHILDREN(rack_timely),
1141             OID_AUTO, "lowerbound", CTLFLAG_RW,
1142             &rack_per_lower_bound, 50,
1143             "Rack timely lowest percentage we allow GP multiplier to fall to");
1144         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1145             SYSCTL_CHILDREN(rack_timely),
1146             OID_AUTO, "upperboundss", CTLFLAG_RW,
1147             &rack_per_upper_bound_ss, 0,
1148             "Rack timely higest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1149         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1150             SYSCTL_CHILDREN(rack_timely),
1151             OID_AUTO, "upperboundca", CTLFLAG_RW,
1152             &rack_per_upper_bound_ca, 0,
1153             "Rack timely higest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1154         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1155             SYSCTL_CHILDREN(rack_timely),
1156             OID_AUTO, "dynamicgp", CTLFLAG_RW,
1157             &rack_do_dyn_mul, 0,
1158             "Rack timely do we enable dynmaic timely goodput by default");
1159         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1160             SYSCTL_CHILDREN(rack_timely),
1161             OID_AUTO, "no_rec_red", CTLFLAG_RW,
1162             &rack_gp_no_rec_chg, 1,
1163             "Rack timely do we prohibit the recovery multiplier from being lowered");
1164         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1165             SYSCTL_CHILDREN(rack_timely),
1166             OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1167             &rack_timely_dec_clear, 6,
1168             "Rack timely what threshold do we count to before another boost during b/w decent");
1169         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1170             SYSCTL_CHILDREN(rack_timely),
1171             OID_AUTO, "max_push_rise", CTLFLAG_RW,
1172             &rack_timely_max_push_rise, 3,
1173             "Rack timely how many times do we push up with b/w increase");
1174         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1175             SYSCTL_CHILDREN(rack_timely),
1176             OID_AUTO, "max_push_drop", CTLFLAG_RW,
1177             &rack_timely_max_push_drop, 3,
1178             "Rack timely how many times do we push back on b/w decent");
1179         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1180             SYSCTL_CHILDREN(rack_timely),
1181             OID_AUTO, "min_segs", CTLFLAG_RW,
1182             &rack_timely_min_segs, 4,
1183             "Rack timely when setting the cwnd what is the min num segments");
1184         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1185             SYSCTL_CHILDREN(rack_timely),
1186             OID_AUTO, "noback_max", CTLFLAG_RW,
1187             &rack_use_max_for_nobackoff, 0,
1188             "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1189         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1190             SYSCTL_CHILDREN(rack_timely),
1191             OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1192             &rack_timely_int_timely_only, 0,
1193             "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1194         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1195             SYSCTL_CHILDREN(rack_timely),
1196             OID_AUTO, "nonstop", CTLFLAG_RW,
1197             &rack_timely_no_stopping, 0,
1198             "Rack timely don't stop increase");
1199         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1200             SYSCTL_CHILDREN(rack_timely),
1201             OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1202             &rack_down_raise_thresh, 100,
1203             "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1204         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1205             SYSCTL_CHILDREN(rack_timely),
1206             OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1207             &rack_req_segs, 1,
1208             "Bottom dragging if not these many segments outstanding and room");
1209
1210         /* TLP and Rack related parameters */
1211         rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1212             SYSCTL_CHILDREN(rack_sysctl_root),
1213             OID_AUTO,
1214             "tlp",
1215             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1216             "TLP and Rack related Controls");
1217         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1218             SYSCTL_CHILDREN(rack_tlp),
1219             OID_AUTO, "use_rrr", CTLFLAG_RW,
1220             &use_rack_rr, 1,
1221             "Do we use Rack Rapid Recovery");
1222         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1223             SYSCTL_CHILDREN(rack_tlp),
1224             OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1225             &rack_max_abc_post_recovery, 2,
1226             "Since we do early recovery, do we override the l_abc to a value, if so what?");
1227         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1228             SYSCTL_CHILDREN(rack_tlp),
1229             OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1230             &rack_non_rxt_use_cr, 0,
1231             "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1232         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1233             SYSCTL_CHILDREN(rack_tlp),
1234             OID_AUTO, "tlpmethod", CTLFLAG_RW,
1235             &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1236             "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1237         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1238             SYSCTL_CHILDREN(rack_tlp),
1239             OID_AUTO, "limit", CTLFLAG_RW,
1240             &rack_tlp_limit, 2,
1241             "How many TLP's can be sent without sending new data");
1242         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1243             SYSCTL_CHILDREN(rack_tlp),
1244             OID_AUTO, "use_greater", CTLFLAG_RW,
1245             &rack_tlp_use_greater, 1,
1246             "Should we use the rack_rtt time if its greater than srtt");
1247         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1248             SYSCTL_CHILDREN(rack_tlp),
1249             OID_AUTO, "tlpminto", CTLFLAG_RW,
1250             &rack_tlp_min, 10000,
1251             "TLP minimum timeout per the specification (in microseconds)");
1252         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1253             SYSCTL_CHILDREN(rack_tlp),
1254             OID_AUTO, "send_oldest", CTLFLAG_RW,
1255             &rack_always_send_oldest, 0,
1256             "Should we always send the oldest TLP and RACK-TLP");
1257         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1258             SYSCTL_CHILDREN(rack_tlp),
1259             OID_AUTO, "rack_tlimit", CTLFLAG_RW,
1260             &rack_limited_retran, 0,
1261             "How many times can a rack timeout drive out sends");
1262         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1263             SYSCTL_CHILDREN(rack_tlp),
1264             OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1265             &rack_lower_cwnd_at_tlp, 0,
1266             "When a TLP completes a retran should we enter recovery");
1267         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1268             SYSCTL_CHILDREN(rack_tlp),
1269             OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1270             &rack_reorder_thresh, 2,
1271             "What factor for rack will be added when seeing reordering (shift right)");
1272         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1273             SYSCTL_CHILDREN(rack_tlp),
1274             OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1275             &rack_tlp_thresh, 1,
1276             "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1277         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1278             SYSCTL_CHILDREN(rack_tlp),
1279             OID_AUTO, "reorder_fade", CTLFLAG_RW,
1280             &rack_reorder_fade, 60000000,
1281             "Does reorder detection fade, if so how many microseconds (0 means never)");
1282         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1283             SYSCTL_CHILDREN(rack_tlp),
1284             OID_AUTO, "pktdelay", CTLFLAG_RW,
1285             &rack_pkt_delay, 1000,
1286             "Extra RACK time (in microseconds) besides reordering thresh");
1287
1288         /* Timer related controls */
1289         rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1290             SYSCTL_CHILDREN(rack_sysctl_root),
1291             OID_AUTO,
1292             "timers",
1293             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1294             "Timer related controls");
1295         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1296             SYSCTL_CHILDREN(rack_timers),
1297             OID_AUTO, "persmin", CTLFLAG_RW,
1298             &rack_persist_min, 250000,
1299             "What is the minimum time in microseconds between persists");
1300         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1301             SYSCTL_CHILDREN(rack_timers),
1302             OID_AUTO, "persmax", CTLFLAG_RW,
1303             &rack_persist_max, 2000000,
1304             "What is the largest delay in microseconds between persists");
1305         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1306             SYSCTL_CHILDREN(rack_timers),
1307             OID_AUTO, "delayed_ack", CTLFLAG_RW,
1308             &rack_delayed_ack_time, 40000,
1309             "Delayed ack time (40ms in microseconds)");
1310         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1311             SYSCTL_CHILDREN(rack_timers),
1312             OID_AUTO, "minrto", CTLFLAG_RW,
1313             &rack_rto_min, 30000,
1314             "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1315         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1316             SYSCTL_CHILDREN(rack_timers),
1317             OID_AUTO, "maxrto", CTLFLAG_RW,
1318             &rack_rto_max, 4000000,
1319             "Maxiumum RTO in microseconds -- should be at least as large as min_rto");
1320         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1321             SYSCTL_CHILDREN(rack_timers),
1322             OID_AUTO, "minto", CTLFLAG_RW,
1323             &rack_min_to, 1000,
1324             "Minimum rack timeout in microseconds");
1325         /* Measure controls */
1326         rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1327             SYSCTL_CHILDREN(rack_sysctl_root),
1328             OID_AUTO,
1329             "measure",
1330             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1331             "Measure related controls");
1332         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1333             SYSCTL_CHILDREN(rack_measure),
1334             OID_AUTO, "wma_divisor", CTLFLAG_RW,
1335             &rack_wma_divisor, 8,
1336             "When doing b/w calculation what is the  divisor for the WMA");
1337         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1338             SYSCTL_CHILDREN(rack_measure),
1339             OID_AUTO, "end_cwnd", CTLFLAG_RW,
1340             &rack_cwnd_block_ends_measure, 0,
1341             "Does a cwnd just-return end the measurement window (app limited)");
1342         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1343             SYSCTL_CHILDREN(rack_measure),
1344             OID_AUTO, "end_rwnd", CTLFLAG_RW,
1345             &rack_rwnd_block_ends_measure, 0,
1346             "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1347         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1348             SYSCTL_CHILDREN(rack_measure),
1349             OID_AUTO, "min_target", CTLFLAG_RW,
1350             &rack_def_data_window, 20,
1351             "What is the minimum target window (in mss) for a GP measurements");
1352         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1353             SYSCTL_CHILDREN(rack_measure),
1354             OID_AUTO, "goal_bdp", CTLFLAG_RW,
1355             &rack_goal_bdp, 2,
1356             "What is the goal BDP to measure");
1357         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1358             SYSCTL_CHILDREN(rack_measure),
1359             OID_AUTO, "min_srtts", CTLFLAG_RW,
1360             &rack_min_srtts, 1,
1361             "What is the goal BDP to measure");
1362         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1363             SYSCTL_CHILDREN(rack_measure),
1364             OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1365             &rack_min_measure_usec, 0,
1366             "What is the Minimum time time for a measurement if 0, this is off");
1367         /* Features */
1368         rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1369             SYSCTL_CHILDREN(rack_sysctl_root),
1370             OID_AUTO,
1371             "features",
1372             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1373             "Feature controls");
1374         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1375             SYSCTL_CHILDREN(rack_features),
1376             OID_AUTO, "cmpack", CTLFLAG_RW,
1377             &rack_use_cmp_acks, 1,
1378             "Should RACK have LRO send compressed acks");
1379         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1380             SYSCTL_CHILDREN(rack_features),
1381             OID_AUTO, "fsb", CTLFLAG_RW,
1382             &rack_use_fsb, 1,
1383             "Should RACK use the fast send block?");
1384         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1385             SYSCTL_CHILDREN(rack_features),
1386             OID_AUTO, "rfo", CTLFLAG_RW,
1387             &rack_use_rfo, 1,
1388             "Should RACK use rack_fast_output()?");
1389         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1390             SYSCTL_CHILDREN(rack_features),
1391             OID_AUTO, "rsmrfo", CTLFLAG_RW,
1392             &rack_use_rsm_rfo, 1,
1393             "Should RACK use rack_fast_rsm_output()?");
1394         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1395             SYSCTL_CHILDREN(rack_features),
1396             OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1397             &rack_enable_mqueue_for_nonpaced, 0,
1398             "Should RACK use mbuf queuing for non-paced connections");
1399         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1400             SYSCTL_CHILDREN(rack_features),
1401             OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1402             &rack_do_hystart, 0,
1403             "Should RACK enable HyStart++ on connections?");
1404         /* Misc rack controls */
1405         rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1406             SYSCTL_CHILDREN(rack_sysctl_root),
1407             OID_AUTO,
1408             "misc",
1409             CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1410             "Misc related controls");
1411 #ifdef TCP_ACCOUNTING
1412         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1413             SYSCTL_CHILDREN(rack_misc),
1414             OID_AUTO, "tcp_acct", CTLFLAG_RW,
1415             &rack_tcp_accounting, 0,
1416             "Should we turn on TCP accounting for all rack sessions?");
1417 #endif
1418         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1419             SYSCTL_CHILDREN(rack_misc),
1420             OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1421             &rack_dsack_std_based, 3,
1422             "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1423         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1424             SYSCTL_CHILDREN(rack_misc),
1425             OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1426             &rack_prr_addbackmax, 2,
1427             "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1428         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1429             SYSCTL_CHILDREN(rack_misc),
1430             OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1431             &rack_stats_gets_ms_rtt, 1,
1432             "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1433         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1434             SYSCTL_CHILDREN(rack_misc),
1435             OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1436             &rack_client_low_buf, 0,
1437             "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1438         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1439             SYSCTL_CHILDREN(rack_misc),
1440             OID_AUTO, "defprofile", CTLFLAG_RW,
1441             &rack_def_profile, 0,
1442             "Should RACK use a default profile (0=no, num == profile num)?");
1443         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1444             SYSCTL_CHILDREN(rack_misc),
1445             OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1446             &rack_enable_shared_cwnd, 1,
1447             "Should RACK try to use the shared cwnd on connections where allowed");
1448         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1449             SYSCTL_CHILDREN(rack_misc),
1450             OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1451             &rack_limits_scwnd, 1,
1452             "Should RACK place low end time limits on the shared cwnd feature");
1453         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1454             SYSCTL_CHILDREN(rack_misc),
1455             OID_AUTO, "iMac_dack", CTLFLAG_RW,
1456             &rack_use_imac_dack, 0,
1457             "Should RACK try to emulate iMac delayed ack");
1458         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1459             SYSCTL_CHILDREN(rack_misc),
1460             OID_AUTO, "no_prr", CTLFLAG_RW,
1461             &rack_disable_prr, 0,
1462             "Should RACK not use prr and only pace (must have pacing on)");
1463         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1464             SYSCTL_CHILDREN(rack_misc),
1465             OID_AUTO, "bb_verbose", CTLFLAG_RW,
1466             &rack_verbose_logging, 0,
1467             "Should RACK black box logging be verbose");
1468         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1469             SYSCTL_CHILDREN(rack_misc),
1470             OID_AUTO, "data_after_close", CTLFLAG_RW,
1471             &rack_ignore_data_after_close, 1,
1472             "Do we hold off sending a RST until all pending data is ack'd");
1473         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1474             SYSCTL_CHILDREN(rack_misc),
1475             OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1476             &rack_sack_not_required, 1,
1477             "Do we allow rack to run on connections not supporting SACK");
1478         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1479             SYSCTL_CHILDREN(rack_misc),
1480             OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1481             &rack_send_a_lot_in_prr, 1,
1482             "Send a lot in prr");
1483         SYSCTL_ADD_S32(&rack_sysctl_ctx,
1484             SYSCTL_CHILDREN(rack_misc),
1485             OID_AUTO, "autoscale", CTLFLAG_RW,
1486             &rack_autosndbuf_inc, 20,
1487             "What percentage should rack scale up its snd buffer by?");
1488         /* Sack Attacker detection stuff */
1489         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1490             SYSCTL_CHILDREN(rack_attack),
1491             OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1492             &rack_highest_sack_thresh_seen, 0,
1493             "Highest sack to ack ratio seen");
1494         SYSCTL_ADD_U32(&rack_sysctl_ctx,
1495             SYSCTL_CHILDREN(rack_attack),
1496             OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1497             &rack_highest_move_thresh_seen, 0,
1498             "Highest move to non-move ratio seen");
1499         rack_ack_total = counter_u64_alloc(M_WAITOK);
1500         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1501             SYSCTL_CHILDREN(rack_attack),
1502             OID_AUTO, "acktotal", CTLFLAG_RD,
1503             &rack_ack_total,
1504             "Total number of Ack's");
1505         rack_express_sack = counter_u64_alloc(M_WAITOK);
1506         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1507             SYSCTL_CHILDREN(rack_attack),
1508             OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1509             &rack_express_sack,
1510             "Total expresss number of Sack's");
1511         rack_sack_total = counter_u64_alloc(M_WAITOK);
1512         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1513             SYSCTL_CHILDREN(rack_attack),
1514             OID_AUTO, "sacktotal", CTLFLAG_RD,
1515             &rack_sack_total,
1516             "Total number of SACKs");
1517         rack_move_none = counter_u64_alloc(M_WAITOK);
1518         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1519             SYSCTL_CHILDREN(rack_attack),
1520             OID_AUTO, "move_none", CTLFLAG_RD,
1521             &rack_move_none,
1522             "Total number of SACK index reuse of postions under threshold");
1523         rack_move_some = counter_u64_alloc(M_WAITOK);
1524         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1525             SYSCTL_CHILDREN(rack_attack),
1526             OID_AUTO, "move_some", CTLFLAG_RD,
1527             &rack_move_some,
1528             "Total number of SACK index reuse of postions over threshold");
1529         rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1530         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1531             SYSCTL_CHILDREN(rack_attack),
1532             OID_AUTO, "attacks", CTLFLAG_RD,
1533             &rack_sack_attacks_detected,
1534             "Total number of SACK attackers that had sack disabled");
1535         rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1536         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1537             SYSCTL_CHILDREN(rack_attack),
1538             OID_AUTO, "reversed", CTLFLAG_RD,
1539             &rack_sack_attacks_reversed,
1540             "Total number of SACK attackers that were later determined false positive");
1541         rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1542         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1543             SYSCTL_CHILDREN(rack_attack),
1544             OID_AUTO, "nextmerge", CTLFLAG_RD,
1545             &rack_sack_used_next_merge,
1546             "Total number of times we used the next merge");
1547         rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1548         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1549             SYSCTL_CHILDREN(rack_attack),
1550             OID_AUTO, "prevmerge", CTLFLAG_RD,
1551             &rack_sack_used_prev_merge,
1552             "Total number of times we used the prev merge");
1553         /* Counters */
1554         rack_fto_send = counter_u64_alloc(M_WAITOK);
1555         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1556             SYSCTL_CHILDREN(rack_counters),
1557             OID_AUTO, "fto_send", CTLFLAG_RD,
1558             &rack_fto_send, "Total number of rack_fast_output sends");
1559         rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1560         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1561             SYSCTL_CHILDREN(rack_counters),
1562             OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1563             &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1564         rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1565         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1566             SYSCTL_CHILDREN(rack_counters),
1567             OID_AUTO, "nfto_resend", CTLFLAG_RD,
1568             &rack_nfto_resend, "Total number of rack_output retransmissions");
1569         rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1570         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1571             SYSCTL_CHILDREN(rack_counters),
1572             OID_AUTO, "nfto_send", CTLFLAG_RD,
1573             &rack_non_fto_send, "Total number of rack_output first sends");
1574         rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1575         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1576             SYSCTL_CHILDREN(rack_counters),
1577             OID_AUTO, "rfo_extended", CTLFLAG_RD,
1578             &rack_extended_rfo, "Total number of times we extended rfo");
1579
1580         rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1581         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1582             SYSCTL_CHILDREN(rack_counters),
1583             OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1584             &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1585         rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1586
1587         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1588             SYSCTL_CHILDREN(rack_counters),
1589             OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1590             &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1591         rack_badfr = counter_u64_alloc(M_WAITOK);
1592         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1593             SYSCTL_CHILDREN(rack_counters),
1594             OID_AUTO, "badfr", CTLFLAG_RD,
1595             &rack_badfr, "Total number of bad FRs");
1596         rack_badfr_bytes = counter_u64_alloc(M_WAITOK);
1597         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1598             SYSCTL_CHILDREN(rack_counters),
1599             OID_AUTO, "badfr_bytes", CTLFLAG_RD,
1600             &rack_badfr_bytes, "Total number of bad FRs");
1601         rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK);
1602         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1603             SYSCTL_CHILDREN(rack_counters),
1604             OID_AUTO, "prrsndret", CTLFLAG_RD,
1605             &rack_rtm_prr_retran,
1606             "Total number of prr based retransmits");
1607         rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK);
1608         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1609             SYSCTL_CHILDREN(rack_counters),
1610             OID_AUTO, "prrsndnew", CTLFLAG_RD,
1611             &rack_rtm_prr_newdata,
1612             "Total number of prr based new transmits");
1613         rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK);
1614         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1615             SYSCTL_CHILDREN(rack_counters),
1616             OID_AUTO, "tsnf", CTLFLAG_RD,
1617             &rack_timestamp_mismatch,
1618             "Total number of timestamps that we could not find the reported ts");
1619         rack_find_high = counter_u64_alloc(M_WAITOK);
1620         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1621             SYSCTL_CHILDREN(rack_counters),
1622             OID_AUTO, "findhigh", CTLFLAG_RD,
1623             &rack_find_high,
1624             "Total number of FIN causing find-high");
1625         rack_reorder_seen = counter_u64_alloc(M_WAITOK);
1626         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1627             SYSCTL_CHILDREN(rack_counters),
1628             OID_AUTO, "reordering", CTLFLAG_RD,
1629             &rack_reorder_seen,
1630             "Total number of times we added delay due to reordering");
1631         rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1632         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1633             SYSCTL_CHILDREN(rack_counters),
1634             OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1635             &rack_tlp_tot,
1636             "Total number of tail loss probe expirations");
1637         rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1638         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1639             SYSCTL_CHILDREN(rack_counters),
1640             OID_AUTO, "tlp_new", CTLFLAG_RD,
1641             &rack_tlp_newdata,
1642             "Total number of tail loss probe sending new data");
1643         rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1644         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1645             SYSCTL_CHILDREN(rack_counters),
1646             OID_AUTO, "tlp_retran", CTLFLAG_RD,
1647             &rack_tlp_retran,
1648             "Total number of tail loss probe sending retransmitted data");
1649         rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1650         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1651             SYSCTL_CHILDREN(rack_counters),
1652             OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1653             &rack_tlp_retran_bytes,
1654             "Total bytes of tail loss probe sending retransmitted data");
1655         rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK);
1656         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1657             SYSCTL_CHILDREN(rack_counters),
1658             OID_AUTO, "tlp_retran_fail", CTLFLAG_RD,
1659             &rack_tlp_retran_fail,
1660             "Total number of tail loss probe sending retransmitted data that failed (wait for t3)");
1661         rack_to_tot = counter_u64_alloc(M_WAITOK);
1662         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1663             SYSCTL_CHILDREN(rack_counters),
1664             OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1665             &rack_to_tot,
1666             "Total number of times the rack to expired");
1667         rack_to_arm_rack = counter_u64_alloc(M_WAITOK);
1668         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1669             SYSCTL_CHILDREN(rack_counters),
1670             OID_AUTO, "arm_rack", CTLFLAG_RD,
1671             &rack_to_arm_rack,
1672             "Total number of times the rack timer armed");
1673         rack_to_arm_tlp = counter_u64_alloc(M_WAITOK);
1674         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1675             SYSCTL_CHILDREN(rack_counters),
1676             OID_AUTO, "arm_tlp", CTLFLAG_RD,
1677             &rack_to_arm_tlp,
1678             "Total number of times the tlp timer armed");
1679         rack_calc_zero = counter_u64_alloc(M_WAITOK);
1680         rack_calc_nonzero = counter_u64_alloc(M_WAITOK);
1681         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1682             SYSCTL_CHILDREN(rack_counters),
1683             OID_AUTO, "calc_zero", CTLFLAG_RD,
1684             &rack_calc_zero,
1685             "Total number of times pacing time worked out to zero");
1686         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1687             SYSCTL_CHILDREN(rack_counters),
1688             OID_AUTO, "calc_nonzero", CTLFLAG_RD,
1689             &rack_calc_nonzero,
1690             "Total number of times pacing time worked out to non-zero");
1691         rack_paced_segments = counter_u64_alloc(M_WAITOK);
1692         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1693             SYSCTL_CHILDREN(rack_counters),
1694             OID_AUTO, "paced", CTLFLAG_RD,
1695             &rack_paced_segments,
1696             "Total number of times a segment send caused hptsi");
1697         rack_unpaced_segments = counter_u64_alloc(M_WAITOK);
1698         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1699             SYSCTL_CHILDREN(rack_counters),
1700             OID_AUTO, "unpaced", CTLFLAG_RD,
1701             &rack_unpaced_segments,
1702             "Total number of times a segment did not cause hptsi");
1703         rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1704         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1705             SYSCTL_CHILDREN(rack_counters),
1706             OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1707             &rack_saw_enobuf,
1708             "Total number of times a sends returned enobuf for non-hdwr paced connections");
1709         rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1710         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1711             SYSCTL_CHILDREN(rack_counters),
1712             OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1713             &rack_saw_enobuf_hw,
1714             "Total number of times a send returned enobuf for hdwr paced connections");
1715         rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1716         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1717             SYSCTL_CHILDREN(rack_counters),
1718             OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1719             &rack_saw_enetunreach,
1720             "Total number of times a send received a enetunreachable");
1721         rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1722         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1723             SYSCTL_CHILDREN(rack_counters),
1724             OID_AUTO, "alloc_hot", CTLFLAG_RD,
1725             &rack_hot_alloc,
1726             "Total allocations from the top of our list");
1727         rack_to_alloc = counter_u64_alloc(M_WAITOK);
1728         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1729             SYSCTL_CHILDREN(rack_counters),
1730             OID_AUTO, "allocs", CTLFLAG_RD,
1731             &rack_to_alloc,
1732             "Total allocations of tracking structures");
1733         rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1734         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1735             SYSCTL_CHILDREN(rack_counters),
1736             OID_AUTO, "allochard", CTLFLAG_RD,
1737             &rack_to_alloc_hard,
1738             "Total allocations done with sleeping the hard way");
1739         rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1740         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1741             SYSCTL_CHILDREN(rack_counters),
1742             OID_AUTO, "allocemerg", CTLFLAG_RD,
1743             &rack_to_alloc_emerg,
1744             "Total allocations done from emergency cache");
1745         rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1746         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1747             SYSCTL_CHILDREN(rack_counters),
1748             OID_AUTO, "alloc_limited", CTLFLAG_RD,
1749             &rack_to_alloc_limited,
1750             "Total allocations dropped due to limit");
1751         rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1752         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1753             SYSCTL_CHILDREN(rack_counters),
1754             OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1755             &rack_alloc_limited_conns,
1756             "Connections with allocations dropped due to limit");
1757         rack_split_limited = counter_u64_alloc(M_WAITOK);
1758         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1759             SYSCTL_CHILDREN(rack_counters),
1760             OID_AUTO, "split_limited", CTLFLAG_RD,
1761             &rack_split_limited,
1762             "Split allocations dropped due to limit");
1763
1764         for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
1765                 char name[32];
1766                 sprintf(name, "cmp_ack_cnt_%d", i);
1767                 rack_proc_comp_ack[i] = counter_u64_alloc(M_WAITOK);
1768                 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1769                                        SYSCTL_CHILDREN(rack_counters),
1770                                        OID_AUTO, name, CTLFLAG_RD,
1771                                        &rack_proc_comp_ack[i],
1772                                        "Number of compressed acks we processed");
1773         }
1774         rack_large_ackcmp = counter_u64_alloc(M_WAITOK);
1775         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1776             SYSCTL_CHILDREN(rack_counters),
1777             OID_AUTO, "cmp_large_mbufs", CTLFLAG_RD,
1778             &rack_large_ackcmp,
1779             "Number of TCP connections with large mbuf's for compressed acks");
1780         rack_small_ackcmp = counter_u64_alloc(M_WAITOK);
1781         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1782             SYSCTL_CHILDREN(rack_counters),
1783             OID_AUTO, "cmp_small_mbufs", CTLFLAG_RD,
1784             &rack_small_ackcmp,
1785             "Number of TCP connections with small mbuf's for compressed acks");
1786 #ifdef INVARIANTS
1787         rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1788         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1789             SYSCTL_CHILDREN(rack_counters),
1790             OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1791             &rack_adjust_map_bw,
1792             "Number of times we hit the case where the sb went up and down on a sendmap entry");
1793 #endif
1794         rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1795         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1796             SYSCTL_CHILDREN(rack_counters),
1797             OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1798             &rack_multi_single_eq,
1799             "Number of compressed acks total represented");
1800         rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1801         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1802             SYSCTL_CHILDREN(rack_counters),
1803             OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1804             &rack_proc_non_comp_ack,
1805             "Number of non compresseds acks that we processed");
1806
1807
1808         rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1809         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1810             SYSCTL_CHILDREN(rack_counters),
1811             OID_AUTO, "sack_long", CTLFLAG_RD,
1812             &rack_sack_proc_all,
1813             "Total times we had to walk whole list for sack processing");
1814         rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1815         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1816             SYSCTL_CHILDREN(rack_counters),
1817             OID_AUTO, "sack_restart", CTLFLAG_RD,
1818             &rack_sack_proc_restart,
1819             "Total times we had to walk whole list due to a restart");
1820         rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1821         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1822             SYSCTL_CHILDREN(rack_counters),
1823             OID_AUTO, "sack_short", CTLFLAG_RD,
1824             &rack_sack_proc_short,
1825             "Total times we took shortcut for sack processing");
1826         rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK);
1827         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1828             SYSCTL_CHILDREN(rack_counters),
1829             OID_AUTO, "tlp_calc_entered", CTLFLAG_RD,
1830             &rack_enter_tlp_calc,
1831             "Total times we called calc-tlp");
1832         rack_used_tlpmethod = counter_u64_alloc(M_WAITOK);
1833         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1834             SYSCTL_CHILDREN(rack_counters),
1835             OID_AUTO, "hit_tlp_method", CTLFLAG_RD,
1836             &rack_used_tlpmethod,
1837             "Total number of runt sacks");
1838         rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK);
1839         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1840             SYSCTL_CHILDREN(rack_counters),
1841             OID_AUTO, "hit_tlp_method2", CTLFLAG_RD,
1842             &rack_used_tlpmethod2,
1843             "Total number of times we hit TLP method 2");
1844         rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1845         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1846             SYSCTL_CHILDREN(rack_attack),
1847             OID_AUTO, "skipacked", CTLFLAG_RD,
1848             &rack_sack_skipped_acked,
1849             "Total number of times we skipped previously sacked");
1850         rack_sack_splits = counter_u64_alloc(M_WAITOK);
1851         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1852             SYSCTL_CHILDREN(rack_attack),
1853             OID_AUTO, "ofsplit", CTLFLAG_RD,
1854             &rack_sack_splits,
1855             "Total number of times we did the old fashion tree split");
1856         rack_progress_drops = counter_u64_alloc(M_WAITOK);
1857         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1858             SYSCTL_CHILDREN(rack_counters),
1859             OID_AUTO, "prog_drops", CTLFLAG_RD,
1860             &rack_progress_drops,
1861             "Total number of progress drops");
1862         rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1863         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1864             SYSCTL_CHILDREN(rack_counters),
1865             OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1866             &rack_input_idle_reduces,
1867             "Total number of idle reductions on input");
1868         rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1869         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1870             SYSCTL_CHILDREN(rack_counters),
1871             OID_AUTO, "collapsed_win", CTLFLAG_RD,
1872             &rack_collapsed_win,
1873             "Total number of collapsed windows");
1874         rack_tlp_does_nada = counter_u64_alloc(M_WAITOK);
1875         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1876             SYSCTL_CHILDREN(rack_counters),
1877             OID_AUTO, "tlp_nada", CTLFLAG_RD,
1878             &rack_tlp_does_nada,
1879             "Total number of nada tlp calls");
1880         rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1881         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1882             SYSCTL_CHILDREN(rack_counters),
1883             OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1884             &rack_try_scwnd,
1885             "Total number of scwnd attempts");
1886
1887         rack_per_timer_hole = counter_u64_alloc(M_WAITOK);
1888         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1889             SYSCTL_CHILDREN(rack_counters),
1890             OID_AUTO, "timer_hole", CTLFLAG_RD,
1891             &rack_per_timer_hole,
1892             "Total persists start in timer hole");
1893
1894         rack_sbsndptr_wrong = counter_u64_alloc(M_WAITOK);
1895         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1896             SYSCTL_CHILDREN(rack_counters),
1897             OID_AUTO, "sndptr_wrong", CTLFLAG_RD,
1898             &rack_sbsndptr_wrong, "Total number of times the saved sbsndptr was incorret");
1899         rack_sbsndptr_right = counter_u64_alloc(M_WAITOK);
1900         SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1901             SYSCTL_CHILDREN(rack_counters),
1902             OID_AUTO, "sndptr_right", CTLFLAG_RD,
1903             &rack_sbsndptr_right, "Total number of times the saved sbsndptr was corret");
1904
1905         COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1906         SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1907             OID_AUTO, "outsize", CTLFLAG_RD,
1908             rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1909         COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1910         SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1911             OID_AUTO, "opts", CTLFLAG_RD,
1912             rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1913         SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1914             SYSCTL_CHILDREN(rack_sysctl_root),
1915             OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1916             &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1917 }
1918
1919 static __inline int
1920 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a)
1921 {
1922         if (SEQ_GEQ(b->r_start, a->r_start) &&
1923             SEQ_LT(b->r_start, a->r_end)) {
1924                 /*
1925                  * The entry b is within the
1926                  * block a. i.e.:
1927                  * a --   |-------------|
1928                  * b --   |----|
1929                  * <or>
1930                  * b --       |------|
1931                  * <or>
1932                  * b --       |-----------|
1933                  */
1934                 return (0);
1935         } else if (SEQ_GEQ(b->r_start, a->r_end)) {
1936                 /*
1937                  * b falls as either the next
1938                  * sequence block after a so a
1939                  * is said to be smaller than b.
1940                  * i.e:
1941                  * a --   |------|
1942                  * b --          |--------|
1943                  * or
1944                  * b --              |-----|
1945                  */
1946                 return (1);
1947         }
1948         /*
1949          * Whats left is where a is
1950          * larger than b. i.e:
1951          * a --         |-------|
1952          * b --  |---|
1953          * or even possibly
1954          * b --   |--------------|
1955          */
1956         return (-1);
1957 }
1958
1959 RB_PROTOTYPE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1960 RB_GENERATE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1961
1962 static uint32_t
1963 rc_init_window(struct tcp_rack *rack)
1964 {
1965         uint32_t win;
1966
1967         if (rack->rc_init_win == 0) {
1968                 /*
1969                  * Nothing set by the user, use the system stack
1970                  * default.
1971                  */
1972                 return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1973         }
1974         win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1975         return (win);
1976 }
1977
1978 static uint64_t
1979 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1980 {
1981         if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1982                 return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1983         else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1984                 return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1985         else
1986                 return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1987 }
1988
1989 static uint64_t
1990 rack_get_bw(struct tcp_rack *rack)
1991 {
1992         if (rack->use_fixed_rate) {
1993                 /* Return the fixed pacing rate */
1994                 return (rack_get_fixed_pacing_bw(rack));
1995         }
1996         if (rack->r_ctl.gp_bw == 0) {
1997                 /*
1998                  * We have yet no b/w measurement,
1999                  * if we have a user set initial bw
2000                  * return it. If we don't have that and
2001                  * we have an srtt, use the tcp IW (10) to
2002                  * calculate a fictional b/w over the SRTT
2003                  * which is more or less a guess. Note
2004                  * we don't use our IW from rack on purpose
2005                  * so if we have like IW=30, we are not
2006                  * calculating a "huge" b/w.
2007                  */
2008                 uint64_t bw, srtt;
2009                 if (rack->r_ctl.init_rate)
2010                         return (rack->r_ctl.init_rate);
2011
2012                 /* Has the user set a max peak rate? */
2013 #ifdef NETFLIX_PEAKRATE
2014                 if (rack->rc_tp->t_maxpeakrate)
2015                         return (rack->rc_tp->t_maxpeakrate);
2016 #endif
2017                 /* Ok lets come up with the IW guess, if we have a srtt */
2018                 if (rack->rc_tp->t_srtt == 0) {
2019                         /*
2020                          * Go with old pacing method
2021                          * i.e. burst mitigation only.
2022                          */
2023                         return (0);
2024                 }
2025                 /* Ok lets get the initial TCP win (not racks) */
2026                 bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2027                 srtt = (uint64_t)rack->rc_tp->t_srtt;
2028                 bw *= (uint64_t)USECS_IN_SECOND;
2029                 bw /= srtt;
2030                 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2031                         bw = rack->r_ctl.bw_rate_cap;
2032                 return (bw);
2033         } else {
2034                 uint64_t bw;
2035
2036                 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2037                         /* Averaging is done, we can return the value */
2038                         bw = rack->r_ctl.gp_bw;
2039                 } else {
2040                         /* Still doing initial average must calculate */
2041                         bw = rack->r_ctl.gp_bw / rack->r_ctl.num_measurements;
2042                 }
2043 #ifdef NETFLIX_PEAKRATE
2044                 if ((rack->rc_tp->t_maxpeakrate) &&
2045                     (bw > rack->rc_tp->t_maxpeakrate)) {
2046                         /* The user has set a peak rate to pace at
2047                          * don't allow us to pace faster than that.
2048                          */
2049                         return (rack->rc_tp->t_maxpeakrate);
2050                 }
2051 #endif
2052                 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2053                         bw = rack->r_ctl.bw_rate_cap;
2054                 return (bw);
2055         }
2056 }
2057
2058 static uint16_t
2059 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2060 {
2061         if (rack->use_fixed_rate) {
2062                 return (100);
2063         } else if (rack->in_probe_rtt && (rsm == NULL))
2064                 return (rack->r_ctl.rack_per_of_gp_probertt);
2065         else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2066                   rack->r_ctl.rack_per_of_gp_rec)) {
2067                 if (rsm) {
2068                         /* a retransmission always use the recovery rate */
2069                         return (rack->r_ctl.rack_per_of_gp_rec);
2070                 } else if (rack->rack_rec_nonrxt_use_cr) {
2071                         /* Directed to use the configured rate */
2072                         goto configured_rate;
2073                 } else if (rack->rack_no_prr &&
2074                            (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2075                         /* No PRR, lets just use the b/w estimate only */
2076                         return (100);
2077                 } else {
2078                         /*
2079                          * Here we may have a non-retransmit but we
2080                          * have no overrides, so just use the recovery
2081                          * rate (prr is in effect).
2082                          */
2083                         return (rack->r_ctl.rack_per_of_gp_rec);
2084                 }
2085         }
2086 configured_rate:
2087         /* For the configured rate we look at our cwnd vs the ssthresh */
2088         if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2089                 return (rack->r_ctl.rack_per_of_gp_ss);
2090         else
2091                 return (rack->r_ctl.rack_per_of_gp_ca);
2092 }
2093
2094 static void
2095 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2096 {
2097         /*
2098          * Types of logs (mod value)
2099          * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2100          * 2 = a dsack round begins, persist is reset to 16.
2101          * 3 = a dsack round ends
2102          * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2103          * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2104          * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2105          */
2106         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2107                 union tcp_log_stackspecific log;
2108                 struct timeval tv;
2109
2110                 memset(&log, 0, sizeof(log));
2111                 log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2112                 log.u_bbr.flex1 <<= 1;
2113                 log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2114                 log.u_bbr.flex1 <<= 1;
2115                 log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2116                 log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2117                 log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2118                 log.u_bbr.flex4 = flex4;
2119                 log.u_bbr.flex5 = flex5;
2120                 log.u_bbr.flex6 = flex6;
2121                 log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2122                 log.u_bbr.flex8 = mod;
2123                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2124                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2125                     &rack->rc_inp->inp_socket->so_rcv,
2126                     &rack->rc_inp->inp_socket->so_snd,
2127                     RACK_DSACK_HANDLING, 0,
2128                     0, &log, false, &tv);
2129         }
2130 }
2131
2132 static void
2133 rack_log_hdwr_pacing(struct tcp_rack *rack,
2134                      uint64_t rate, uint64_t hw_rate, int line,
2135                      int error, uint16_t mod)
2136 {
2137         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2138                 union tcp_log_stackspecific log;
2139                 struct timeval tv;
2140                 const struct ifnet *ifp;
2141
2142                 memset(&log, 0, sizeof(log));
2143                 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2144                 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2145                 if (rack->r_ctl.crte) {
2146                         ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2147                 } else if (rack->rc_inp->inp_route.ro_nh &&
2148                            rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2149                         ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2150                 } else
2151                         ifp = NULL;
2152                 if (ifp) {
2153                         log.u_bbr.flex3 = (((uint64_t)ifp  >> 32) & 0x00000000ffffffff);
2154                         log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2155                 }
2156                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2157                 log.u_bbr.bw_inuse = rate;
2158                 log.u_bbr.flex5 = line;
2159                 log.u_bbr.flex6 = error;
2160                 log.u_bbr.flex7 = mod;
2161                 log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2162                 log.u_bbr.flex8 = rack->use_fixed_rate;
2163                 log.u_bbr.flex8 <<= 1;
2164                 log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2165                 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2166                 log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2167                 if (rack->r_ctl.crte)
2168                         log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2169                 else
2170                         log.u_bbr.cur_del_rate = 0;
2171                 log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2172                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2173                     &rack->rc_inp->inp_socket->so_rcv,
2174                     &rack->rc_inp->inp_socket->so_snd,
2175                     BBR_LOG_HDWR_PACE, 0,
2176                     0, &log, false, &tv);
2177         }
2178 }
2179
2180 static uint64_t
2181 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2182 {
2183         /*
2184          * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2185          */
2186         uint64_t bw_est, high_rate;
2187         uint64_t gain;
2188
2189         gain = (uint64_t)rack_get_output_gain(rack, rsm);
2190         bw_est = bw * gain;
2191         bw_est /= (uint64_t)100;
2192         /* Never fall below the minimum (def 64kbps) */
2193         if (bw_est < RACK_MIN_BW)
2194                 bw_est = RACK_MIN_BW;
2195         if (rack->r_rack_hw_rate_caps) {
2196                 /* Rate caps are in place */
2197                 if (rack->r_ctl.crte != NULL) {
2198                         /* We have a hdwr rate already */
2199                         high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2200                         if (bw_est >= high_rate) {
2201                                 /* We are capping bw at the highest rate table entry */
2202                                 rack_log_hdwr_pacing(rack,
2203                                                      bw_est, high_rate, __LINE__,
2204                                                      0, 3);
2205                                 bw_est = high_rate;
2206                                 if (capped)
2207                                         *capped = 1;
2208                         }
2209                 } else if ((rack->rack_hdrw_pacing == 0) &&
2210                            (rack->rack_hdw_pace_ena) &&
2211                            (rack->rack_attempt_hdwr_pace == 0) &&
2212                            (rack->rc_inp->inp_route.ro_nh != NULL) &&
2213                            (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2214                         /*
2215                          * Special case, we have not yet attempted hardware
2216                          * pacing, and yet we may, when we do, find out if we are
2217                          * above the highest rate. We need to know the maxbw for the interface
2218                          * in question (if it supports ratelimiting). We get back
2219                          * a 0, if the interface is not found in the RL lists.
2220                          */
2221                         high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2222                         if (high_rate) {
2223                                 /* Yep, we have a rate is it above this rate? */
2224                                 if (bw_est > high_rate) {
2225                                         bw_est = high_rate;
2226                                         if (capped)
2227                                                 *capped = 1;
2228                                 }
2229                         }
2230                 }
2231         }
2232         return (bw_est);
2233 }
2234
2235 static void
2236 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2237 {
2238         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2239                 union tcp_log_stackspecific log;
2240                 struct timeval tv;
2241
2242                 if ((mod != 1) && (rack_verbose_logging == 0)) {
2243                         /*
2244                          * We get 3 values currently for mod
2245                          * 1 - We are retransmitting and this tells the reason.
2246                          * 2 - We are clearing a dup-ack count.
2247                          * 3 - We are incrementing a dup-ack count.
2248                          *
2249                          * The clear/increment are only logged
2250                          * if you have BBverbose on.
2251                          */
2252                         return;
2253                 }
2254                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2255                 log.u_bbr.flex1 = tsused;
2256                 log.u_bbr.flex2 = thresh;
2257                 log.u_bbr.flex3 = rsm->r_flags;
2258                 log.u_bbr.flex4 = rsm->r_dupack;
2259                 log.u_bbr.flex5 = rsm->r_start;
2260                 log.u_bbr.flex6 = rsm->r_end;
2261                 log.u_bbr.flex8 = mod;
2262                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2263                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2264                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2265                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2266                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2267                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2268                 log.u_bbr.pacing_gain = rack->r_must_retran;
2269                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2270                     &rack->rc_inp->inp_socket->so_rcv,
2271                     &rack->rc_inp->inp_socket->so_snd,
2272                     BBR_LOG_SETTINGS_CHG, 0,
2273                     0, &log, false, &tv);
2274         }
2275 }
2276
2277 static void
2278 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2279 {
2280         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2281                 union tcp_log_stackspecific log;
2282                 struct timeval tv;
2283
2284                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2285                 log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2286                 log.u_bbr.flex2 = to;
2287                 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2288                 log.u_bbr.flex4 = slot;
2289                 log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot;
2290                 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2291                 log.u_bbr.flex7 = rack->rc_in_persist;
2292                 log.u_bbr.flex8 = which;
2293                 if (rack->rack_no_prr)
2294                         log.u_bbr.pkts_out = 0;
2295                 else
2296                         log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2297                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2298                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2299                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2300                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2301                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2302                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2303                 log.u_bbr.pacing_gain = rack->r_must_retran;
2304                 log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2305                 log.u_bbr.lost = rack_rto_min;
2306                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2307                     &rack->rc_inp->inp_socket->so_rcv,
2308                     &rack->rc_inp->inp_socket->so_snd,
2309                     BBR_LOG_TIMERSTAR, 0,
2310                     0, &log, false, &tv);
2311         }
2312 }
2313
2314 static void
2315 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2316 {
2317         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2318                 union tcp_log_stackspecific log;
2319                 struct timeval tv;
2320
2321                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2322                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2323                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2324                 log.u_bbr.flex8 = to_num;
2325                 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2326                 log.u_bbr.flex2 = rack->rc_rack_rtt;
2327                 if (rsm == NULL)
2328                         log.u_bbr.flex3 = 0;
2329                 else
2330                         log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2331                 if (rack->rack_no_prr)
2332                         log.u_bbr.flex5 = 0;
2333                 else
2334                         log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2335                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2336                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2337                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2338                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2339                 log.u_bbr.pacing_gain = rack->r_must_retran;
2340                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2341                     &rack->rc_inp->inp_socket->so_rcv,
2342                     &rack->rc_inp->inp_socket->so_snd,
2343                     BBR_LOG_RTO, 0,
2344                     0, &log, false, &tv);
2345         }
2346 }
2347
2348 static void
2349 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2350                  struct rack_sendmap *prev,
2351                  struct rack_sendmap *rsm,
2352                  struct rack_sendmap *next,
2353                  int flag, uint32_t th_ack, int line)
2354 {
2355         if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2356                 union tcp_log_stackspecific log;
2357                 struct timeval tv;
2358
2359                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2360                 log.u_bbr.flex8 = flag;
2361                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2362                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2363                 log.u_bbr.cur_del_rate = (uint64_t)prev;
2364                 log.u_bbr.delRate = (uint64_t)rsm;
2365                 log.u_bbr.rttProp = (uint64_t)next;
2366                 log.u_bbr.flex7 = 0;
2367                 if (prev) {
2368                         log.u_bbr.flex1 = prev->r_start;
2369                         log.u_bbr.flex2 = prev->r_end;
2370                         log.u_bbr.flex7 |= 0x4;
2371                 }
2372                 if (rsm) {
2373                         log.u_bbr.flex3 = rsm->r_start;
2374                         log.u_bbr.flex4 = rsm->r_end;
2375                         log.u_bbr.flex7 |= 0x2;
2376                 }
2377                 if (next) {
2378                         log.u_bbr.flex5 = next->r_start;
2379                         log.u_bbr.flex6 = next->r_end;
2380                         log.u_bbr.flex7 |= 0x1;
2381                 }
2382                 log.u_bbr.applimited = line;
2383                 log.u_bbr.pkts_out = th_ack;
2384                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2385                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2386                 if (rack->rack_no_prr)
2387                         log.u_bbr.lost = 0;
2388                 else
2389                         log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2390                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2391                     &rack->rc_inp->inp_socket->so_rcv,
2392                     &rack->rc_inp->inp_socket->so_snd,
2393                     TCP_LOG_MAPCHG, 0,
2394                     0, &log, false, &tv);
2395         }
2396 }
2397
2398 static void
2399 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2400                  struct rack_sendmap *rsm, int conf)
2401 {
2402         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2403                 union tcp_log_stackspecific log;
2404                 struct timeval tv;
2405                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2406                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2407                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2408                 log.u_bbr.flex1 = t;
2409                 log.u_bbr.flex2 = len;
2410                 log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2411                 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2412                 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2413                 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2414                 log.u_bbr.flex7 = conf;
2415                 log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2416                 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2417                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2418                 log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2419                 log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2420                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2421                 if (rsm) {
2422                         log.u_bbr.pkt_epoch = rsm->r_start;
2423                         log.u_bbr.lost = rsm->r_end;
2424                         log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2425                         log.u_bbr.pacing_gain = rsm->r_flags;
2426                 } else {
2427                         /* Its a SYN */
2428                         log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2429                         log.u_bbr.lost = 0;
2430                         log.u_bbr.cwnd_gain = 0;
2431                         log.u_bbr.pacing_gain = 0;
2432                 }
2433                 /* Write out general bits of interest rrs here */
2434                 log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2435                 log.u_bbr.use_lt_bw <<= 1;
2436                 log.u_bbr.use_lt_bw |= rack->forced_ack;
2437                 log.u_bbr.use_lt_bw <<= 1;
2438                 log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2439                 log.u_bbr.use_lt_bw <<= 1;
2440                 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2441                 log.u_bbr.use_lt_bw <<= 1;
2442                 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2443                 log.u_bbr.use_lt_bw <<= 1;
2444                 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2445                 log.u_bbr.use_lt_bw <<= 1;
2446                 log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2447                 log.u_bbr.use_lt_bw <<= 1;
2448                 log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2449                 log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2450                 log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2451                 log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2452                 log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2453                 log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2454                 log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2455                 log.u_bbr.bw_inuse <<= 32;
2456                 if (rsm)
2457                         log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2458                 TCP_LOG_EVENTP(tp, NULL,
2459                     &rack->rc_inp->inp_socket->so_rcv,
2460                     &rack->rc_inp->inp_socket->so_snd,
2461                     BBR_LOG_BBRRTT, 0,
2462                     0, &log, false, &tv);
2463
2464
2465         }
2466 }
2467
2468 static void
2469 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2470 {
2471         /*
2472          * Log the rtt sample we are
2473          * applying to the srtt algorithm in
2474          * useconds.
2475          */
2476         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2477                 union tcp_log_stackspecific log;
2478                 struct timeval tv;
2479
2480                 /* Convert our ms to a microsecond */
2481                 memset(&log, 0, sizeof(log));
2482                 log.u_bbr.flex1 = rtt;
2483                 log.u_bbr.flex2 = rack->r_ctl.ack_count;
2484                 log.u_bbr.flex3 = rack->r_ctl.sack_count;
2485                 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2486                 log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
2487                 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2488                 log.u_bbr.flex7 = 1;
2489                 log.u_bbr.flex8 = rack->sack_attack_disable;
2490                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2491                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2492                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2493                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2494                 log.u_bbr.pacing_gain = rack->r_must_retran;
2495                 /*
2496                  * We capture in delRate the upper 32 bits as
2497                  * the confidence level we had declared, and the
2498                  * lower 32 bits as the actual RTT using the arrival
2499                  * timestamp.
2500                  */
2501                 log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2502                 log.u_bbr.delRate <<= 32;
2503                 log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2504                 /* Lets capture all the things that make up t_rtxcur */
2505                 log.u_bbr.applimited = rack_rto_min;
2506                 log.u_bbr.epoch = rack_rto_max;
2507                 log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2508                 log.u_bbr.lost = rack_rto_min;
2509                 log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2510                 log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2511                 log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2512                 log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2513                 log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2514                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2515                     &rack->rc_inp->inp_socket->so_rcv,
2516                     &rack->rc_inp->inp_socket->so_snd,
2517                     TCP_LOG_RTT, 0,
2518                     0, &log, false, &tv);
2519         }
2520 }
2521
2522 static void
2523 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2524 {
2525         if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2526                 union tcp_log_stackspecific log;
2527                 struct timeval tv;
2528
2529                 /* Convert our ms to a microsecond */
2530                 memset(&log, 0, sizeof(log));
2531                 log.u_bbr.flex1 = rtt;
2532                 log.u_bbr.flex2 = send_time;
2533                 log.u_bbr.flex3 = ack_time;
2534                 log.u_bbr.flex4 = where;
2535                 log.u_bbr.flex7 = 2;
2536                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2537                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2538                     &rack->rc_inp->inp_socket->so_rcv,
2539                     &rack->rc_inp->inp_socket->so_snd,
2540                     TCP_LOG_RTT, 0,
2541                     0, &log, false, &tv);
2542         }
2543 }
2544
2545
2546
2547 static inline void
2548 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick,  int event, int line)
2549 {
2550         if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2551                 union tcp_log_stackspecific log;
2552                 struct timeval tv;
2553
2554                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2555                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2556                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2557                 log.u_bbr.flex1 = line;
2558                 log.u_bbr.flex2 = tick;
2559                 log.u_bbr.flex3 = tp->t_maxunacktime;
2560                 log.u_bbr.flex4 = tp->t_acktime;
2561                 log.u_bbr.flex8 = event;
2562                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2563                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2564                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2565                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2566                 log.u_bbr.pacing_gain = rack->r_must_retran;
2567                 TCP_LOG_EVENTP(tp, NULL,
2568                     &rack->rc_inp->inp_socket->so_rcv,
2569                     &rack->rc_inp->inp_socket->so_snd,
2570                     BBR_LOG_PROGRESS, 0,
2571                     0, &log, false, &tv);
2572         }
2573 }
2574
2575 static void
2576 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv)
2577 {
2578         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2579                 union tcp_log_stackspecific log;
2580
2581                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2582                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2583                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2584                 log.u_bbr.flex1 = slot;
2585                 if (rack->rack_no_prr)
2586                         log.u_bbr.flex2 = 0;
2587                 else
2588                         log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
2589                 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
2590                 log.u_bbr.flex8 = rack->rc_in_persist;
2591                 log.u_bbr.timeStamp = cts;
2592                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2593                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2594                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2595                 log.u_bbr.pacing_gain = rack->r_must_retran;
2596                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2597                     &rack->rc_inp->inp_socket->so_rcv,
2598                     &rack->rc_inp->inp_socket->so_snd,
2599                     BBR_LOG_BBRSND, 0,
2600                     0, &log, false, tv);
2601         }
2602 }
2603
2604 static void
2605 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
2606 {
2607         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2608                 union tcp_log_stackspecific log;
2609                 struct timeval tv;
2610
2611                 memset(&log, 0, sizeof(log));
2612                 log.u_bbr.flex1 = did_out;
2613                 log.u_bbr.flex2 = nxt_pkt;
2614                 log.u_bbr.flex3 = way_out;
2615                 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2616                 if (rack->rack_no_prr)
2617                         log.u_bbr.flex5 = 0;
2618                 else
2619                         log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2620                 log.u_bbr.flex6 = nsegs;
2621                 log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
2622                 log.u_bbr.flex7 = rack->rc_ack_can_sendout_data;        /* Do we have ack-can-send set */
2623                 log.u_bbr.flex7 <<= 1;
2624                 log.u_bbr.flex7 |= rack->r_fast_output; /* is fast output primed */
2625                 log.u_bbr.flex7 <<= 1;
2626                 log.u_bbr.flex7 |= rack->r_wanted_output;       /* Do we want output */
2627                 log.u_bbr.flex8 = rack->rc_in_persist;
2628                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2629                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2630                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2631                 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2632                 log.u_bbr.use_lt_bw <<= 1;
2633                 log.u_bbr.use_lt_bw |= rack->r_might_revert;
2634                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2635                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2636                 log.u_bbr.pacing_gain = rack->r_must_retran;
2637                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2638                     &rack->rc_inp->inp_socket->so_rcv,
2639                     &rack->rc_inp->inp_socket->so_snd,
2640                     BBR_LOG_DOSEG_DONE, 0,
2641                     0, &log, false, &tv);
2642         }
2643 }
2644
2645 static void
2646 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
2647 {
2648         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2649                 union tcp_log_stackspecific log;
2650                 struct timeval tv;
2651                 uint32_t cts;
2652
2653                 memset(&log, 0, sizeof(log));
2654                 cts = tcp_get_usecs(&tv);
2655                 log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
2656                 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
2657                 log.u_bbr.flex4 = arg1;
2658                 log.u_bbr.flex5 = arg2;
2659                 log.u_bbr.flex6 = arg3;
2660                 log.u_bbr.flex8 = frm;
2661                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2662                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2663                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2664                 log.u_bbr.applimited = rack->r_ctl.rc_sacked;
2665                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2666                 log.u_bbr.pacing_gain = rack->r_must_retran;
2667                 TCP_LOG_EVENTP(tp, NULL,
2668                     &tp->t_inpcb->inp_socket->so_rcv,
2669                     &tp->t_inpcb->inp_socket->so_snd,
2670                     TCP_HDWR_PACE_SIZE, 0,
2671                     0, &log, false, &tv);
2672         }
2673 }
2674
2675 static void
2676 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
2677                           uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
2678 {
2679         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2680                 union tcp_log_stackspecific log;
2681                 struct timeval tv;
2682
2683                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2684                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2685                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2686                 log.u_bbr.flex1 = slot;
2687                 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
2688                 log.u_bbr.flex4 = reason;
2689                 if (rack->rack_no_prr)
2690                         log.u_bbr.flex5 = 0;
2691                 else
2692                         log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2693                 log.u_bbr.flex7 = hpts_calling;
2694                 log.u_bbr.flex8 = rack->rc_in_persist;
2695                 log.u_bbr.lt_epoch = cwnd_to_use;
2696                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2697                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2698                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2699                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2700                 log.u_bbr.pacing_gain = rack->r_must_retran;
2701                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2702                     &rack->rc_inp->inp_socket->so_rcv,
2703                     &rack->rc_inp->inp_socket->so_snd,
2704                     BBR_LOG_JUSTRET, 0,
2705                     tlen, &log, false, &tv);
2706         }
2707 }
2708
2709 static void
2710 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
2711                    struct timeval *tv, uint32_t flags_on_entry)
2712 {
2713         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2714                 union tcp_log_stackspecific log;
2715
2716                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2717                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
2718                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
2719                 log.u_bbr.flex1 = line;
2720                 log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
2721                 log.u_bbr.flex3 = flags_on_entry;
2722                 log.u_bbr.flex4 = us_cts;
2723                 if (rack->rack_no_prr)
2724                         log.u_bbr.flex5 = 0;
2725                 else
2726                         log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2727                 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2728                 log.u_bbr.flex7 = hpts_removed;
2729                 log.u_bbr.flex8 = 1;
2730                 log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
2731                 log.u_bbr.timeStamp = us_cts;
2732                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2733                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2734                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2735                 log.u_bbr.pacing_gain = rack->r_must_retran;
2736                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2737                     &rack->rc_inp->inp_socket->so_rcv,
2738                     &rack->rc_inp->inp_socket->so_snd,
2739                     BBR_LOG_TIMERCANC, 0,
2740                     0, &log, false, tv);
2741         }
2742 }
2743
2744 static void
2745 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
2746                           uint32_t flex1, uint32_t flex2,
2747                           uint32_t flex3, uint32_t flex4,
2748                           uint32_t flex5, uint32_t flex6,
2749                           uint16_t flex7, uint8_t mod)
2750 {
2751         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2752                 union tcp_log_stackspecific log;
2753                 struct timeval tv;
2754
2755                 if (mod == 1) {
2756                         /* No you can't use 1, its for the real to cancel */
2757                         return;
2758                 }
2759                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2760                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2761                 log.u_bbr.flex1 = flex1;
2762                 log.u_bbr.flex2 = flex2;
2763                 log.u_bbr.flex3 = flex3;
2764                 log.u_bbr.flex4 = flex4;
2765                 log.u_bbr.flex5 = flex5;
2766                 log.u_bbr.flex6 = flex6;
2767                 log.u_bbr.flex7 = flex7;
2768                 log.u_bbr.flex8 = mod;
2769                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2770                     &rack->rc_inp->inp_socket->so_rcv,
2771                     &rack->rc_inp->inp_socket->so_snd,
2772                     BBR_LOG_TIMERCANC, 0,
2773                     0, &log, false, &tv);
2774         }
2775 }
2776
2777 static void
2778 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
2779 {
2780         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2781                 union tcp_log_stackspecific log;
2782                 struct timeval tv;
2783
2784                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2785                 log.u_bbr.flex1 = timers;
2786                 log.u_bbr.flex2 = ret;
2787                 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
2788                 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2789                 log.u_bbr.flex5 = cts;
2790                 if (rack->rack_no_prr)
2791                         log.u_bbr.flex6 = 0;
2792                 else
2793                         log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
2794                 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2795                 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2796                 log.u_bbr.pacing_gain = rack->r_must_retran;
2797                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2798                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2799                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2800                     &rack->rc_inp->inp_socket->so_rcv,
2801                     &rack->rc_inp->inp_socket->so_snd,
2802                     BBR_LOG_TO_PROCESS, 0,
2803                     0, &log, false, &tv);
2804         }
2805 }
2806
2807 static void
2808 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd)
2809 {
2810         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2811                 union tcp_log_stackspecific log;
2812                 struct timeval tv;
2813
2814                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2815                 log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
2816                 log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
2817                 if (rack->rack_no_prr)
2818                         log.u_bbr.flex3 = 0;
2819                 else
2820                         log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
2821                 log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
2822                 log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
2823                 log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
2824                 log.u_bbr.flex8 = frm;
2825                 log.u_bbr.pkts_out = orig_cwnd;
2826                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2827                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2828                 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2829                 log.u_bbr.use_lt_bw <<= 1;
2830                 log.u_bbr.use_lt_bw |= rack->r_might_revert;
2831                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2832                     &rack->rc_inp->inp_socket->so_rcv,
2833                     &rack->rc_inp->inp_socket->so_snd,
2834                     BBR_LOG_BBRUPD, 0,
2835                     0, &log, false, &tv);
2836         }
2837 }
2838
2839 #ifdef NETFLIX_EXP_DETECTION
2840 static void
2841 rack_log_sad(struct tcp_rack *rack, int event)
2842 {
2843         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2844                 union tcp_log_stackspecific log;
2845                 struct timeval tv;
2846
2847                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2848                 log.u_bbr.flex1 = rack->r_ctl.sack_count;
2849                 log.u_bbr.flex2 = rack->r_ctl.ack_count;
2850                 log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
2851                 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2852                 log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
2853                 log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
2854                 log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
2855                 log.u_bbr.lt_epoch = (tcp_force_detection << 8);
2856                 log.u_bbr.lt_epoch |= rack->do_detection;
2857                 log.u_bbr.applimited = tcp_map_minimum;
2858                 log.u_bbr.flex7 = rack->sack_attack_disable;
2859                 log.u_bbr.flex8 = event;
2860                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2861                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2862                 log.u_bbr.delivered = tcp_sad_decay_val;
2863                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2864                     &rack->rc_inp->inp_socket->so_rcv,
2865                     &rack->rc_inp->inp_socket->so_snd,
2866                     TCP_SAD_DETECTION, 0,
2867                     0, &log, false, &tv);
2868         }
2869 }
2870 #endif
2871
2872 static void
2873 rack_counter_destroy(void)
2874 {
2875         int i;
2876
2877         counter_u64_free(rack_fto_send);
2878         counter_u64_free(rack_fto_rsm_send);
2879         counter_u64_free(rack_nfto_resend);
2880         counter_u64_free(rack_hw_pace_init_fail);
2881         counter_u64_free(rack_hw_pace_lost);
2882         counter_u64_free(rack_non_fto_send);
2883         counter_u64_free(rack_extended_rfo);
2884         counter_u64_free(rack_ack_total);
2885         counter_u64_free(rack_express_sack);
2886         counter_u64_free(rack_sack_total);
2887         counter_u64_free(rack_move_none);
2888         counter_u64_free(rack_move_some);
2889         counter_u64_free(rack_sack_attacks_detected);
2890         counter_u64_free(rack_sack_attacks_reversed);
2891         counter_u64_free(rack_sack_used_next_merge);
2892         counter_u64_free(rack_sack_used_prev_merge);
2893         counter_u64_free(rack_badfr);
2894         counter_u64_free(rack_badfr_bytes);
2895         counter_u64_free(rack_rtm_prr_retran);
2896         counter_u64_free(rack_rtm_prr_newdata);
2897         counter_u64_free(rack_timestamp_mismatch);
2898         counter_u64_free(rack_find_high);
2899         counter_u64_free(rack_reorder_seen);
2900         counter_u64_free(rack_tlp_tot);
2901         counter_u64_free(rack_tlp_newdata);
2902         counter_u64_free(rack_tlp_retran);
2903         counter_u64_free(rack_tlp_retran_bytes);
2904         counter_u64_free(rack_tlp_retran_fail);
2905         counter_u64_free(rack_to_tot);
2906         counter_u64_free(rack_to_arm_rack);
2907         counter_u64_free(rack_to_arm_tlp);
2908         counter_u64_free(rack_calc_zero);
2909         counter_u64_free(rack_calc_nonzero);
2910         counter_u64_free(rack_paced_segments);
2911         counter_u64_free(rack_unpaced_segments);
2912         counter_u64_free(rack_saw_enobuf);
2913         counter_u64_free(rack_saw_enobuf_hw);
2914         counter_u64_free(rack_saw_enetunreach);
2915         counter_u64_free(rack_hot_alloc);
2916         counter_u64_free(rack_to_alloc);
2917         counter_u64_free(rack_to_alloc_hard);
2918         counter_u64_free(rack_to_alloc_emerg);
2919         counter_u64_free(rack_to_alloc_limited);
2920         counter_u64_free(rack_alloc_limited_conns);
2921         counter_u64_free(rack_split_limited);
2922         for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
2923                 counter_u64_free(rack_proc_comp_ack[i]);
2924         }
2925         counter_u64_free(rack_multi_single_eq);
2926         counter_u64_free(rack_proc_non_comp_ack);
2927         counter_u64_free(rack_sack_proc_all);
2928         counter_u64_free(rack_sack_proc_restart);
2929         counter_u64_free(rack_sack_proc_short);
2930         counter_u64_free(rack_enter_tlp_calc);
2931         counter_u64_free(rack_used_tlpmethod);
2932         counter_u64_free(rack_used_tlpmethod2);
2933         counter_u64_free(rack_sack_skipped_acked);
2934         counter_u64_free(rack_sack_splits);
2935         counter_u64_free(rack_progress_drops);
2936         counter_u64_free(rack_input_idle_reduces);
2937         counter_u64_free(rack_collapsed_win);
2938         counter_u64_free(rack_tlp_does_nada);
2939         counter_u64_free(rack_try_scwnd);
2940         counter_u64_free(rack_per_timer_hole);
2941         counter_u64_free(rack_large_ackcmp);
2942         counter_u64_free(rack_small_ackcmp);
2943 #ifdef INVARIANTS
2944         counter_u64_free(rack_adjust_map_bw);
2945 #endif
2946         COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
2947         COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
2948 }
2949
2950 static struct rack_sendmap *
2951 rack_alloc(struct tcp_rack *rack)
2952 {
2953         struct rack_sendmap *rsm;
2954
2955         /*
2956          * First get the top of the list it in
2957          * theory is the "hottest" rsm we have,
2958          * possibly just freed by ack processing.
2959          */
2960         if (rack->rc_free_cnt > rack_free_cache) {
2961                 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2962                 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2963                 counter_u64_add(rack_hot_alloc, 1);
2964                 rack->rc_free_cnt--;
2965                 return (rsm);
2966         }
2967         /*
2968          * Once we get under our free cache we probably
2969          * no longer have a "hot" one available. Lets
2970          * get one from UMA.
2971          */
2972         rsm = uma_zalloc(rack_zone, M_NOWAIT);
2973         if (rsm) {
2974                 rack->r_ctl.rc_num_maps_alloced++;
2975                 counter_u64_add(rack_to_alloc, 1);
2976                 return (rsm);
2977         }
2978         /*
2979          * Dig in to our aux rsm's (the last two) since
2980          * UMA failed to get us one.
2981          */
2982         if (rack->rc_free_cnt) {
2983                 counter_u64_add(rack_to_alloc_emerg, 1);
2984                 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2985                 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2986                 rack->rc_free_cnt--;
2987                 return (rsm);
2988         }
2989         return (NULL);
2990 }
2991
2992 static struct rack_sendmap *
2993 rack_alloc_full_limit(struct tcp_rack *rack)
2994 {
2995         if ((V_tcp_map_entries_limit > 0) &&
2996             (rack->do_detection == 0) &&
2997             (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
2998                 counter_u64_add(rack_to_alloc_limited, 1);
2999                 if (!rack->alloc_limit_reported) {
3000                         rack->alloc_limit_reported = 1;
3001                         counter_u64_add(rack_alloc_limited_conns, 1);
3002                 }
3003                 return (NULL);
3004         }
3005         return (rack_alloc(rack));
3006 }
3007
3008 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3009 static struct rack_sendmap *
3010 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3011 {
3012         struct rack_sendmap *rsm;
3013
3014         if (limit_type) {
3015                 /* currently there is only one limit type */
3016                 if (V_tcp_map_split_limit > 0 &&
3017                     (rack->do_detection == 0) &&
3018                     rack->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
3019                         counter_u64_add(rack_split_limited, 1);
3020                         if (!rack->alloc_limit_reported) {
3021                                 rack->alloc_limit_reported = 1;
3022                                 counter_u64_add(rack_alloc_limited_conns, 1);
3023                         }
3024                         return (NULL);
3025                 }
3026         }
3027
3028         /* allocate and mark in the limit type, if set */
3029         rsm = rack_alloc(rack);
3030         if (rsm != NULL && limit_type) {
3031                 rsm->r_limit_type = limit_type;
3032                 rack->r_ctl.rc_num_split_allocs++;
3033         }
3034         return (rsm);
3035 }
3036
3037 static void
3038 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3039 {
3040         if (rsm->r_flags & RACK_APP_LIMITED) {
3041                 if (rack->r_ctl.rc_app_limited_cnt > 0) {
3042                         rack->r_ctl.rc_app_limited_cnt--;
3043                 }
3044         }
3045         if (rsm->r_limit_type) {
3046                 /* currently there is only one limit type */
3047                 rack->r_ctl.rc_num_split_allocs--;
3048         }
3049         if (rsm == rack->r_ctl.rc_first_appl) {
3050                 if (rack->r_ctl.rc_app_limited_cnt == 0)
3051                         rack->r_ctl.rc_first_appl = NULL;
3052                 else {
3053                         /* Follow the next one out */
3054                         struct rack_sendmap fe;
3055
3056                         fe.r_start = rsm->r_nseq_appl;
3057                         rack->r_ctl.rc_first_appl = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
3058                 }
3059         }
3060         if (rsm == rack->r_ctl.rc_resend)
3061                 rack->r_ctl.rc_resend = NULL;
3062         if (rsm == rack->r_ctl.rc_rsm_at_retran)
3063                 rack->r_ctl.rc_rsm_at_retran = NULL;
3064         if (rsm == rack->r_ctl.rc_end_appl)
3065                 rack->r_ctl.rc_end_appl = NULL;
3066         if (rack->r_ctl.rc_tlpsend == rsm)
3067                 rack->r_ctl.rc_tlpsend = NULL;
3068         if (rack->r_ctl.rc_sacklast == rsm)
3069                 rack->r_ctl.rc_sacklast = NULL;
3070         memset(rsm, 0, sizeof(struct rack_sendmap));
3071         TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3072         rack->rc_free_cnt++;
3073 }
3074
3075 static void
3076 rack_free_trim(struct tcp_rack *rack)
3077 {
3078         struct rack_sendmap *rsm;
3079
3080         /*
3081          * Free up all the tail entries until
3082          * we get our list down to the limit.
3083          */
3084         while (rack->rc_free_cnt > rack_free_cache) {
3085                 rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3086                 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3087                 rack->rc_free_cnt--;
3088                 uma_zfree(rack_zone, rsm);
3089         }
3090 }
3091
3092
3093 static uint32_t
3094 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3095 {
3096         uint64_t srtt, bw, len, tim;
3097         uint32_t segsiz, def_len, minl;
3098
3099         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3100         def_len = rack_def_data_window * segsiz;
3101         if (rack->rc_gp_filled == 0) {
3102                 /*
3103                  * We have no measurement (IW is in flight?) so
3104                  * we can only guess using our data_window sysctl
3105                  * value (usually 20MSS).
3106                  */
3107                 return (def_len);
3108         }
3109         /*
3110          * Now we have a number of factors to consider.
3111          *
3112          * 1) We have a desired BDP which is usually
3113          *    at least 2.
3114          * 2) We have a minimum number of rtt's usually 1 SRTT
3115          *    but we allow it too to be more.
3116          * 3) We want to make sure a measurement last N useconds (if
3117          *    we have set rack_min_measure_usec.
3118          *
3119          * We handle the first concern here by trying to create a data
3120          * window of max(rack_def_data_window, DesiredBDP). The
3121          * second concern we handle in not letting the measurement
3122          * window end normally until at least the required SRTT's
3123          * have gone by which is done further below in
3124          * rack_enough_for_measurement(). Finally the third concern
3125          * we also handle here by calculating how long that time
3126          * would take at the current BW and then return the
3127          * max of our first calculation and that length. Note
3128          * that if rack_min_measure_usec is 0, we don't deal
3129          * with concern 3. Also for both Concern 1 and 3 an
3130          * application limited period could end the measurement
3131          * earlier.
3132          *
3133          * So lets calculate the BDP with the "known" b/w using
3134          * the SRTT has our rtt and then multiply it by the
3135          * goal.
3136          */
3137         bw = rack_get_bw(rack);
3138         srtt = (uint64_t)tp->t_srtt;
3139         len = bw * srtt;
3140         len /= (uint64_t)HPTS_USEC_IN_SEC;
3141         len *= max(1, rack_goal_bdp);
3142         /* Now we need to round up to the nearest MSS */
3143         len = roundup(len, segsiz);
3144         if (rack_min_measure_usec) {
3145                 /* Now calculate our min length for this b/w */
3146                 tim = rack_min_measure_usec;
3147                 minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3148                 if (minl == 0)
3149                         minl = 1;
3150                 minl = roundup(minl, segsiz);
3151                 if (len < minl)
3152                         len = minl;
3153         }
3154         /*
3155          * Now if we have a very small window we want
3156          * to attempt to get the window that is
3157          * as small as possible. This happens on
3158          * low b/w connections and we don't want to
3159          * span huge numbers of rtt's between measurements.
3160          *
3161          * We basically include 2 over our "MIN window" so
3162          * that the measurement can be shortened (possibly) by
3163          * an ack'ed packet.
3164          */
3165         if (len < def_len)
3166                 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3167         else
3168                 return (max((uint32_t)len, def_len));
3169
3170 }
3171
3172 static int
3173 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3174 {
3175         uint32_t tim, srtts, segsiz;
3176
3177         /*
3178          * Has enough time passed for the GP measurement to be valid?
3179          */
3180         if ((tp->snd_max == tp->snd_una) ||
3181             (th_ack == tp->snd_max)){
3182                 /* All is acked */
3183                 *quality = RACK_QUALITY_ALLACKED;
3184                 return (1);
3185         }
3186         if (SEQ_LT(th_ack, tp->gput_seq)) {
3187                 /* Not enough bytes yet */
3188                 return (0);
3189         }
3190         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3191         if (SEQ_LT(th_ack, tp->gput_ack) &&
3192             ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3193                 /* Not enough bytes yet */
3194                 return (0);
3195         }
3196         if (rack->r_ctl.rc_first_appl &&
3197             (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3198                 /*
3199                  * We are up to the app limited send point
3200                  * we have to measure irrespective of the time..
3201                  */
3202                 *quality = RACK_QUALITY_APPLIMITED;
3203                 return (1);
3204         }
3205         /* Now what about time? */
3206         srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3207         tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3208         if (tim >= srtts) {
3209                 *quality = RACK_QUALITY_HIGH;
3210                 return (1);
3211         }
3212         /* Nope not even a full SRTT has passed */
3213         return (0);
3214 }
3215
3216 static void
3217 rack_log_timely(struct tcp_rack *rack,
3218                 uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3219                 uint64_t up_bnd, int line, uint8_t method)
3220 {
3221         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3222                 union tcp_log_stackspecific log;
3223                 struct timeval tv;
3224
3225                 memset(&log, 0, sizeof(log));
3226                 log.u_bbr.flex1 = logged;
3227                 log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3228                 log.u_bbr.flex2 <<= 4;
3229                 log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3230                 log.u_bbr.flex2 <<= 4;
3231                 log.u_bbr.flex2 |= rack->rc_gp_incr;
3232                 log.u_bbr.flex2 <<= 4;
3233                 log.u_bbr.flex2 |= rack->rc_gp_bwred;
3234                 log.u_bbr.flex3 = rack->rc_gp_incr;
3235                 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3236                 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3237                 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3238                 log.u_bbr.flex7 = rack->rc_gp_bwred;
3239                 log.u_bbr.flex8 = method;
3240                 log.u_bbr.cur_del_rate = cur_bw;
3241                 log.u_bbr.delRate = low_bnd;
3242                 log.u_bbr.bw_inuse = up_bnd;
3243                 log.u_bbr.rttProp = rack_get_bw(rack);
3244                 log.u_bbr.pkt_epoch = line;
3245                 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3246                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3247                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3248                 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3249                 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3250                 log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3251                 log.u_bbr.cwnd_gain <<= 1;
3252                 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3253                 log.u_bbr.cwnd_gain <<= 1;
3254                 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3255                 log.u_bbr.cwnd_gain <<= 1;
3256                 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3257                 log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3258                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3259                     &rack->rc_inp->inp_socket->so_rcv,
3260                     &rack->rc_inp->inp_socket->so_snd,
3261                     TCP_TIMELY_WORK, 0,
3262                     0, &log, false, &tv);
3263         }
3264 }
3265
3266 static int
3267 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3268 {
3269         /*
3270          * Before we increase we need to know if
3271          * the estimate just made was less than
3272          * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3273          *
3274          * If we already are pacing at a fast enough
3275          * rate to push us faster there is no sense of
3276          * increasing.
3277          *
3278          * We first caculate our actual pacing rate (ss or ca multipler
3279          * times our cur_bw).
3280          *
3281          * Then we take the last measured rate and multipy by our
3282          * maximum pacing overage to give us a max allowable rate.
3283          *
3284          * If our act_rate is smaller than our max_allowable rate
3285          * then we should increase. Else we should hold steady.
3286          *
3287          */
3288         uint64_t act_rate, max_allow_rate;
3289
3290         if (rack_timely_no_stopping)
3291                 return (1);
3292
3293         if ((cur_bw == 0) || (last_bw_est == 0)) {
3294                 /*
3295                  * Initial startup case or
3296                  * everything is acked case.
3297                  */
3298                 rack_log_timely(rack,  mult, cur_bw, 0, 0,
3299                                 __LINE__, 9);
3300                 return (1);
3301         }
3302         if (mult <= 100) {
3303                 /*
3304                  * We can always pace at or slightly above our rate.
3305                  */
3306                 rack_log_timely(rack,  mult, cur_bw, 0, 0,
3307                                 __LINE__, 9);
3308                 return (1);
3309         }
3310         act_rate = cur_bw * (uint64_t)mult;
3311         act_rate /= 100;
3312         max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3313         max_allow_rate /= 100;
3314         if (act_rate < max_allow_rate) {
3315                 /*
3316                  * Here the rate we are actually pacing at
3317                  * is smaller than 10% above our last measurement.
3318                  * This means we are pacing below what we would
3319                  * like to try to achieve (plus some wiggle room).
3320                  */
3321                 rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3322                                 __LINE__, 9);
3323                 return (1);
3324         } else {
3325                 /*
3326                  * Here we are already pacing at least rack_max_per_above(10%)
3327                  * what we are getting back. This indicates most likely
3328                  * that we are being limited (cwnd/rwnd/app) and can't
3329                  * get any more b/w. There is no sense of trying to
3330                  * raise up the pacing rate its not speeding us up
3331                  * and we already are pacing faster than we are getting.
3332                  */
3333                 rack_log_timely(rack,  mult, cur_bw, act_rate, max_allow_rate,
3334                                 __LINE__, 8);
3335                 return (0);
3336         }
3337 }
3338
3339 static void
3340 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3341 {
3342         /*
3343          * When we drag bottom, we want to assure
3344          * that no multiplier is below 1.0, if so
3345          * we want to restore it to at least that.
3346          */
3347         if (rack->r_ctl.rack_per_of_gp_rec  < 100) {
3348                 /* This is unlikely we usually do not touch recovery */
3349                 rack->r_ctl.rack_per_of_gp_rec = 100;
3350         }
3351         if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3352                 rack->r_ctl.rack_per_of_gp_ca = 100;
3353         }
3354         if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3355                 rack->r_ctl.rack_per_of_gp_ss = 100;
3356         }
3357 }
3358
3359 static void
3360 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3361 {
3362         if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3363                 rack->r_ctl.rack_per_of_gp_ca = 100;
3364         }
3365         if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3366                 rack->r_ctl.rack_per_of_gp_ss = 100;
3367         }
3368 }
3369
3370 static void
3371 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3372 {
3373         int32_t  calc, logged, plus;
3374
3375         logged = 0;
3376
3377         if (override) {
3378                 /*
3379                  * override is passed when we are
3380                  * loosing b/w and making one last
3381                  * gasp at trying to not loose out
3382                  * to a new-reno flow.
3383                  */
3384                 goto extra_boost;
3385         }
3386         /* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3387         if (rack->rc_gp_incr &&
3388             ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3389                 /*
3390                  * Reset and get 5 strokes more before the boost. Note
3391                  * that the count is 0 based so we have to add one.
3392                  */
3393 extra_boost:
3394                 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3395                 rack->rc_gp_timely_inc_cnt = 0;
3396         } else
3397                 plus = (uint32_t)rack_gp_increase_per;
3398         /* Must be at least 1% increase for true timely increases */
3399         if ((plus < 1) &&
3400             ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3401                 plus = 1;
3402         if (rack->rc_gp_saw_rec &&
3403             (rack->rc_gp_no_rec_chg == 0) &&
3404             rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3405                                   rack->r_ctl.rack_per_of_gp_rec)) {
3406                 /* We have been in recovery ding it too */
3407                 calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3408                 if (calc > 0xffff)
3409                         calc = 0xffff;
3410                 logged |= 1;
3411                 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3412                 if (rack_per_upper_bound_ss &&
3413                     (rack->rc_dragged_bottom == 0) &&
3414                     (rack->r_ctl.rack_per_of_gp_rec > rack_per_upper_bound_ss))
3415                         rack->r_ctl.rack_per_of_gp_rec = rack_per_upper_bound_ss;
3416         }
3417         if (rack->rc_gp_saw_ca &&
3418             (rack->rc_gp_saw_ss == 0) &&
3419             rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3420                                   rack->r_ctl.rack_per_of_gp_ca)) {
3421                 /* In CA */
3422                 calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3423                 if (calc > 0xffff)
3424                         calc = 0xffff;
3425                 logged |= 2;
3426                 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3427                 if (rack_per_upper_bound_ca &&
3428                     (rack->rc_dragged_bottom == 0) &&
3429                     (rack->r_ctl.rack_per_of_gp_ca > rack_per_upper_bound_ca))
3430                         rack->r_ctl.rack_per_of_gp_ca = rack_per_upper_bound_ca;
3431         }
3432         if (rack->rc_gp_saw_ss &&
3433             rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3434                                   rack->r_ctl.rack_per_of_gp_ss)) {
3435                 /* In SS */
3436                 calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3437                 if (calc > 0xffff)
3438                         calc = 0xffff;
3439                 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3440                 if (rack_per_upper_bound_ss &&
3441                     (rack->rc_dragged_bottom == 0) &&
3442                     (rack->r_ctl.rack_per_of_gp_ss > rack_per_upper_bound_ss))
3443                         rack->r_ctl.rack_per_of_gp_ss = rack_per_upper_bound_ss;
3444                 logged |= 4;
3445         }
3446         if (logged &&
3447             (rack->rc_gp_incr == 0)){
3448                 /* Go into increment mode */
3449                 rack->rc_gp_incr = 1;
3450                 rack->rc_gp_timely_inc_cnt = 0;
3451         }
3452         if (rack->rc_gp_incr &&
3453             logged &&
3454             (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3455                 rack->rc_gp_timely_inc_cnt++;
3456         }
3457         rack_log_timely(rack,  logged, plus, 0, 0,
3458                         __LINE__, 1);
3459 }
3460
3461 static uint32_t
3462 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3463 {
3464         /*
3465          * norm_grad = rtt_diff / minrtt;
3466          * new_per = curper * (1 - B * norm_grad)
3467          *
3468          * B = rack_gp_decrease_per (default 10%)
3469          * rtt_dif = input var current rtt-diff
3470          * curper = input var current percentage
3471          * minrtt = from rack filter
3472          *
3473          */
3474         uint64_t perf;
3475
3476         perf = (((uint64_t)curper * ((uint64_t)1000000 -
3477                     ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3478                      (((uint64_t)rtt_diff * (uint64_t)1000000)/
3479                       (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3480                      (uint64_t)1000000)) /
3481                 (uint64_t)1000000);
3482         if (perf > curper) {
3483                 /* TSNH */
3484                 perf = curper - 1;
3485         }
3486         return ((uint32_t)perf);
3487 }
3488
3489 static uint32_t
3490 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3491 {
3492         /*
3493          *                                   highrttthresh
3494          * result = curper * (1 - (B * ( 1 -  ------          ))
3495          *                                     gp_srtt
3496          *
3497          * B = rack_gp_decrease_per (default 10%)
3498          * highrttthresh = filter_min * rack_gp_rtt_maxmul
3499          */
3500         uint64_t perf;
3501         uint32_t highrttthresh;
3502
3503         highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3504
3505         perf = (((uint64_t)curper * ((uint64_t)1000000 -
3506                                      ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3507                                         ((uint64_t)highrttthresh * (uint64_t)1000000) /
3508                                                     (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3509         return (perf);
3510 }
3511
3512 static void
3513 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3514 {
3515         uint64_t logvar, logvar2, logvar3;
3516         uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3517
3518         if (rack->rc_gp_incr) {
3519                 /* Turn off increment counting */
3520                 rack->rc_gp_incr = 0;
3521                 rack->rc_gp_timely_inc_cnt = 0;
3522         }
3523         ss_red = ca_red = rec_red = 0;
3524         logged = 0;
3525         /* Calculate the reduction value */
3526         if (rtt_diff < 0) {
3527                 rtt_diff *= -1;
3528         }
3529         /* Must be at least 1% reduction */
3530         if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
3531                 /* We have been in recovery ding it too */
3532                 if (timely_says == 2) {
3533                         new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
3534                         alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3535                         if (alt < new_per)
3536                                 val = alt;
3537                         else
3538                                 val = new_per;
3539                 } else
3540                          val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3541                 if (rack->r_ctl.rack_per_of_gp_rec > val) {
3542                         rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
3543                         rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
3544                 } else {
3545                         rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3546                         rec_red = 0;
3547                 }
3548                 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
3549                         rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3550                 logged |= 1;
3551         }
3552         if (rack->rc_gp_saw_ss) {
3553                 /* Sent in SS */
3554                 if (timely_says == 2) {
3555                         new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
3556                         alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3557                         if (alt < new_per)
3558                                 val = alt;
3559                         else
3560                                 val = new_per;
3561                 } else
3562                         val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
3563                 if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
3564                         ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
3565                         rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
3566                 } else {
3567                         ss_red = new_per;
3568                         rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3569                         logvar = new_per;
3570                         logvar <<= 32;
3571                         logvar |= alt;
3572                         logvar2 = (uint32_t)rtt;
3573                         logvar2 <<= 32;
3574                         logvar2 |= (uint32_t)rtt_diff;
3575                         logvar3 = rack_gp_rtt_maxmul;
3576                         logvar3 <<= 32;
3577                         logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3578                         rack_log_timely(rack, timely_says,
3579                                         logvar2, logvar3,
3580                                         logvar, __LINE__, 10);
3581                 }
3582                 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
3583                         rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3584                 logged |= 4;
3585         } else if (rack->rc_gp_saw_ca) {
3586                 /* Sent in CA */
3587                 if (timely_says == 2) {
3588                         new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
3589                         alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3590                         if (alt < new_per)
3591                                 val = alt;
3592                         else
3593                                 val = new_per;
3594                 } else
3595                         val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
3596                 if (rack->r_ctl.rack_per_of_gp_ca > val) {
3597                         ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
3598                         rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
3599                 } else {
3600                         rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3601                         ca_red = 0;
3602                         logvar = new_per;
3603                         logvar <<= 32;
3604                         logvar |= alt;
3605                         logvar2 = (uint32_t)rtt;
3606                         logvar2 <<= 32;
3607                         logvar2 |= (uint32_t)rtt_diff;
3608                         logvar3 = rack_gp_rtt_maxmul;
3609                         logvar3 <<= 32;
3610                         logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3611                         rack_log_timely(rack, timely_says,
3612                                         logvar2, logvar3,
3613                                         logvar, __LINE__, 10);
3614                 }
3615                 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
3616                         rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3617                 logged |= 2;
3618         }
3619         if (rack->rc_gp_timely_dec_cnt < 0x7) {
3620                 rack->rc_gp_timely_dec_cnt++;
3621                 if (rack_timely_dec_clear &&
3622                     (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
3623                         rack->rc_gp_timely_dec_cnt = 0;
3624         }
3625         logvar = ss_red;
3626         logvar <<= 32;
3627         logvar |= ca_red;
3628         rack_log_timely(rack,  logged, rec_red, rack_per_lower_bound, logvar,
3629                         __LINE__, 2);
3630 }
3631
3632 static void
3633 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
3634                      uint32_t rtt, uint32_t line, uint8_t reas)
3635 {
3636         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3637                 union tcp_log_stackspecific log;
3638                 struct timeval tv;
3639
3640                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3641                 log.u_bbr.flex1 = line;
3642                 log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
3643                 log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
3644                 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3645                 log.u_bbr.flex5 = rtt;
3646                 log.u_bbr.flex6 = rack->rc_highly_buffered;
3647                 log.u_bbr.flex6 <<= 1;
3648                 log.u_bbr.flex6 |= rack->forced_ack;
3649                 log.u_bbr.flex6 <<= 1;
3650                 log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
3651                 log.u_bbr.flex6 <<= 1;
3652                 log.u_bbr.flex6 |= rack->in_probe_rtt;
3653                 log.u_bbr.flex6 <<= 1;
3654                 log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
3655                 log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
3656                 log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
3657                 log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
3658                 log.u_bbr.flex8 = reas;
3659                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3660                 log.u_bbr.delRate = rack_get_bw(rack);
3661                 log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
3662                 log.u_bbr.cur_del_rate <<= 32;
3663                 log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
3664                 log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
3665                 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3666                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3667                 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3668                 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3669                 log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
3670                 log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
3671                 log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3672                 log.u_bbr.rttProp = us_cts;
3673                 log.u_bbr.rttProp <<= 32;
3674                 log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
3675                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3676                     &rack->rc_inp->inp_socket->so_rcv,
3677                     &rack->rc_inp->inp_socket->so_snd,
3678                     BBR_LOG_RTT_SHRINKS, 0,
3679                     0, &log, false, &rack->r_ctl.act_rcv_time);
3680         }
3681 }
3682
3683 static void
3684 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
3685 {
3686         uint64_t bwdp;
3687
3688         bwdp = rack_get_bw(rack);
3689         bwdp *= (uint64_t)rtt;
3690         bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
3691         rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
3692         if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
3693                 /*
3694                  * A window protocol must be able to have 4 packets
3695                  * outstanding as the floor in order to function
3696                  * (especially considering delayed ack :D).
3697                  */
3698                 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
3699         }
3700 }
3701
3702 static void
3703 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
3704 {
3705         /**
3706          * ProbeRTT is a bit different in rack_pacing than in
3707          * BBR. It is like BBR in that it uses the lowering of
3708          * the RTT as a signal that we saw something new and
3709          * counts from there for how long between. But it is
3710          * different in that its quite simple. It does not
3711          * play with the cwnd and wait until we get down
3712          * to N segments outstanding and hold that for
3713          * 200ms. Instead it just sets the pacing reduction
3714          * rate to a set percentage (70 by default) and hold
3715          * that for a number of recent GP Srtt's.
3716          */
3717         uint32_t segsiz;
3718
3719         if (rack->rc_gp_dyn_mul == 0)
3720                 return;
3721
3722         if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
3723                 /* We are idle */
3724                 return;
3725         }
3726         if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3727             SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3728                 /*
3729                  * Stop the goodput now, the idea here is
3730                  * that future measurements with in_probe_rtt
3731                  * won't register if they are not greater so
3732                  * we want to get what info (if any) is available
3733                  * now.
3734                  */
3735                 rack_do_goodput_measurement(rack->rc_tp, rack,
3736                                             rack->rc_tp->snd_una, __LINE__,
3737                                             RACK_QUALITY_PROBERTT);
3738         }
3739         rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3740         rack->r_ctl.rc_time_probertt_entered = us_cts;
3741         segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3742                      rack->r_ctl.rc_pace_min_segs);
3743         rack->in_probe_rtt = 1;
3744         rack->measure_saw_probe_rtt = 1;
3745         rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3746         rack->r_ctl.rc_time_probertt_starts = 0;
3747         rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
3748         if (rack_probertt_use_min_rtt_entry)
3749                 rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3750         else
3751                 rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
3752         rack_log_rtt_shrinks(rack,  us_cts,  get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3753                              __LINE__, RACK_RTTS_ENTERPROBE);
3754 }
3755
3756 static void
3757 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
3758 {
3759         struct rack_sendmap *rsm;
3760         uint32_t segsiz;
3761
3762         segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
3763                      rack->r_ctl.rc_pace_min_segs);
3764         rack->in_probe_rtt = 0;
3765         if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3766             SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
3767                 /*
3768                  * Stop the goodput now, the idea here is
3769                  * that future measurements with in_probe_rtt
3770                  * won't register if they are not greater so
3771                  * we want to get what info (if any) is available
3772                  * now.
3773                  */
3774                 rack_do_goodput_measurement(rack->rc_tp, rack,
3775                                             rack->rc_tp->snd_una, __LINE__,
3776                                             RACK_QUALITY_PROBERTT);
3777         } else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
3778                 /*
3779                  * We don't have enough data to make a measurement.
3780                  * So lets just stop and start here after exiting
3781                  * probe-rtt. We probably are not interested in
3782                  * the results anyway.
3783                  */
3784                 rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
3785         }
3786         /*
3787          * Measurements through the current snd_max are going
3788          * to be limited by the slower pacing rate.
3789          *
3790          * We need to mark these as app-limited so we
3791          * don't collapse the b/w.
3792          */
3793         rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
3794         if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
3795                 if (rack->r_ctl.rc_app_limited_cnt == 0)
3796                         rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
3797                 else {
3798                         /*
3799                          * Go out to the end app limited and mark
3800                          * this new one as next and move the end_appl up
3801                          * to this guy.
3802                          */
3803                         if (rack->r_ctl.rc_end_appl)
3804                                 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
3805                         rack->r_ctl.rc_end_appl = rsm;
3806                 }
3807                 rsm->r_flags |= RACK_APP_LIMITED;
3808                 rack->r_ctl.rc_app_limited_cnt++;
3809         }
3810         /*
3811          * Now, we need to examine our pacing rate multipliers.
3812          * If its under 100%, we need to kick it back up to
3813          * 100%. We also don't let it be over our "max" above
3814          * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
3815          * Note setting clamp_atexit_prtt to 0 has the effect
3816          * of setting CA/SS to 100% always at exit (which is
3817          * the default behavior).
3818          */
3819         if (rack_probertt_clear_is) {
3820                 rack->rc_gp_incr = 0;
3821                 rack->rc_gp_bwred = 0;
3822                 rack->rc_gp_timely_inc_cnt = 0;
3823                 rack->rc_gp_timely_dec_cnt = 0;
3824         }
3825         /* Do we do any clamping at exit? */
3826         if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
3827                 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
3828                 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
3829         }
3830         if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
3831                 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
3832                 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
3833         }
3834         /*
3835          * Lets set rtt_diff to 0, so that we will get a "boost"
3836          * after exiting.
3837          */
3838         rack->r_ctl.rc_rtt_diff = 0;
3839
3840         /* Clear all flags so we start fresh */
3841         rack->rc_tp->t_bytes_acked = 0;
3842         rack->rc_tp->ccv->flags &= ~CCF_ABC_SENTAWND;
3843         /*
3844          * If configured to, set the cwnd and ssthresh to
3845          * our targets.
3846          */
3847         if (rack_probe_rtt_sets_cwnd) {
3848                 uint64_t ebdp;
3849                 uint32_t setto;
3850
3851                 /* Set ssthresh so we get into CA once we hit our target */
3852                 if (rack_probertt_use_min_rtt_exit == 1) {
3853                         /* Set to min rtt */
3854                         rack_set_prtt_target(rack, segsiz,
3855                                              get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3856                 } else if (rack_probertt_use_min_rtt_exit == 2) {
3857                         /* Set to current gp rtt */
3858                         rack_set_prtt_target(rack, segsiz,
3859                                              rack->r_ctl.rc_gp_srtt);
3860                 } else if (rack_probertt_use_min_rtt_exit == 3) {
3861                         /* Set to entry gp rtt */
3862                         rack_set_prtt_target(rack, segsiz,
3863                                              rack->r_ctl.rc_entry_gp_rtt);
3864                 } else {
3865                         uint64_t sum;
3866                         uint32_t setval;
3867
3868                         sum = rack->r_ctl.rc_entry_gp_rtt;
3869                         sum *= 10;
3870                         sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
3871                         if (sum >= 20) {
3872                                 /*
3873                                  * A highly buffered path needs
3874                                  * cwnd space for timely to work.
3875                                  * Lets set things up as if
3876                                  * we are heading back here again.
3877                                  */
3878                                 setval = rack->r_ctl.rc_entry_gp_rtt;
3879                         } else if (sum >= 15) {
3880                                 /*
3881                                  * Lets take the smaller of the
3882                                  * two since we are just somewhat
3883                                  * buffered.
3884                                  */
3885                                 setval = rack->r_ctl.rc_gp_srtt;
3886                                 if (setval > rack->r_ctl.rc_entry_gp_rtt)
3887                                         setval = rack->r_ctl.rc_entry_gp_rtt;
3888                         } else {
3889                                 /*
3890                                  * Here we are not highly buffered
3891                                  * and should pick the min we can to
3892                                  * keep from causing loss.
3893                                  */
3894                                 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3895                         }
3896                         rack_set_prtt_target(rack, segsiz,
3897                                              setval);
3898                 }
3899                 if (rack_probe_rtt_sets_cwnd > 1) {
3900                         /* There is a percentage here to boost */
3901                         ebdp = rack->r_ctl.rc_target_probertt_flight;
3902                         ebdp *= rack_probe_rtt_sets_cwnd;
3903                         ebdp /= 100;
3904                         setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
3905                 } else
3906                         setto = rack->r_ctl.rc_target_probertt_flight;
3907                 rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
3908                 if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
3909                         /* Enforce a min */
3910                         rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
3911                 }
3912                 /* If we set in the cwnd also set the ssthresh point so we are in CA */
3913                 rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
3914         }
3915         rack_log_rtt_shrinks(rack,  us_cts,
3916                              get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3917                              __LINE__, RACK_RTTS_EXITPROBE);
3918         /* Clear times last so log has all the info */
3919         rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
3920         rack->r_ctl.rc_time_probertt_entered = us_cts;
3921         rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3922         rack->r_ctl.rc_time_of_last_probertt = us_cts;
3923 }
3924
3925 static void
3926 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
3927 {
3928         /* Check in on probe-rtt */
3929         if (rack->rc_gp_filled == 0) {
3930                 /* We do not do p-rtt unless we have gp measurements */
3931                 return;
3932         }
3933         if (rack->in_probe_rtt) {
3934                 uint64_t no_overflow;
3935                 uint32_t endtime, must_stay;
3936
3937                 if (rack->r_ctl.rc_went_idle_time &&
3938                     ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
3939                         /*
3940                          * We went idle during prtt, just exit now.
3941                          */
3942                         rack_exit_probertt(rack, us_cts);
3943                 } else if (rack_probe_rtt_safety_val &&
3944                     TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
3945                     ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
3946                         /*
3947                          * Probe RTT safety value triggered!
3948                          */
3949                         rack_log_rtt_shrinks(rack,  us_cts,
3950                                              get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3951                                              __LINE__, RACK_RTTS_SAFETY);
3952                         rack_exit_probertt(rack, us_cts);
3953                 }
3954                 /* Calculate the max we will wait */
3955                 endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
3956                 if (rack->rc_highly_buffered)
3957                         endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
3958                 /* Calculate the min we must wait */
3959                 must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
3960                 if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
3961                     TSTMP_LT(us_cts, endtime)) {
3962                         uint32_t calc;
3963                         /* Do we lower more? */
3964 no_exit:
3965                         if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
3966                                 calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
3967                         else
3968                                 calc = 0;
3969                         calc /= max(rack->r_ctl.rc_gp_srtt, 1);
3970                         if (calc) {
3971                                 /* Maybe */
3972                                 calc *= rack_per_of_gp_probertt_reduce;
3973                                 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
3974                                 /* Limit it too */
3975                                 if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
3976                                         rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
3977                         }
3978                         /* We must reach target or the time set */
3979                         return;
3980                 }
3981                 if (rack->r_ctl.rc_time_probertt_starts == 0) {
3982                         if ((TSTMP_LT(us_cts, must_stay) &&
3983                              rack->rc_highly_buffered) ||
3984                              (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
3985                               rack->r_ctl.rc_target_probertt_flight)) {
3986                                 /* We are not past the must_stay time */
3987                                 goto no_exit;
3988                         }
3989                         rack_log_rtt_shrinks(rack,  us_cts,
3990                                              get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3991                                              __LINE__, RACK_RTTS_REACHTARGET);
3992                         rack->r_ctl.rc_time_probertt_starts = us_cts;
3993                         if (rack->r_ctl.rc_time_probertt_starts == 0)
3994                                 rack->r_ctl.rc_time_probertt_starts = 1;
3995                         /* Restore back to our rate we want to pace at in prtt */
3996                         rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3997                 }
3998                 /*
3999                  * Setup our end time, some number of gp_srtts plus 200ms.
4000                  */
4001                 no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4002                                (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4003                 if (rack_probertt_gpsrtt_cnt_div)
4004                         endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4005                 else
4006                         endtime = 0;
4007                 endtime += rack_min_probertt_hold;
4008                 endtime += rack->r_ctl.rc_time_probertt_starts;
4009                 if (TSTMP_GEQ(us_cts,  endtime)) {
4010                         /* yes, exit probertt */
4011                         rack_exit_probertt(rack, us_cts);
4012                 }
4013
4014         } else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
4015                 /* Go into probertt, its been too long since we went lower */
4016                 rack_enter_probertt(rack, us_cts);
4017         }
4018 }
4019
4020 static void
4021 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4022                        uint32_t rtt, int32_t rtt_diff)
4023 {
4024         uint64_t cur_bw, up_bnd, low_bnd, subfr;
4025         uint32_t losses;
4026
4027         if ((rack->rc_gp_dyn_mul == 0) ||
4028             (rack->use_fixed_rate) ||
4029             (rack->in_probe_rtt) ||
4030             (rack->rc_always_pace == 0)) {
4031                 /* No dynamic GP multipler in play */
4032                 return;
4033         }
4034         losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4035         cur_bw = rack_get_bw(rack);
4036         /* Calculate our up and down range */
4037         up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4038         up_bnd /= 100;
4039         up_bnd += rack->r_ctl.last_gp_comp_bw;
4040
4041         subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4042         subfr /= 100;
4043         low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4044         if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4045                 /*
4046                  * This is the case where our RTT is above
4047                  * the max target and we have been configured
4048                  * to just do timely no bonus up stuff in that case.
4049                  *
4050                  * There are two configurations, set to 1, and we
4051                  * just do timely if we are over our max. If its
4052                  * set above 1 then we slam the multipliers down
4053                  * to 100 and then decrement per timely.
4054                  */
4055                 rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4056                                 __LINE__, 3);
4057                 if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4058                         rack_validate_multipliers_at_or_below_100(rack);
4059                 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4060         } else if ((last_bw_est < low_bnd) && !losses) {
4061                 /*
4062                  * We are decreasing this is a bit complicated this
4063                  * means we are loosing ground. This could be
4064                  * because another flow entered and we are competing
4065                  * for b/w with it. This will push the RTT up which
4066                  * makes timely unusable unless we want to get shoved
4067                  * into a corner and just be backed off (the age
4068                  * old problem with delay based CC).
4069                  *
4070                  * On the other hand if it was a route change we
4071                  * would like to stay somewhat contained and not
4072                  * blow out the buffers.
4073                  */
4074                 rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4075                                 __LINE__, 3);
4076                 rack->r_ctl.last_gp_comp_bw = cur_bw;
4077                 if (rack->rc_gp_bwred == 0) {
4078                         /* Go into reduction counting */
4079                         rack->rc_gp_bwred = 1;
4080                         rack->rc_gp_timely_dec_cnt = 0;
4081                 }
4082                 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) ||
4083                     (timely_says == 0)) {
4084                         /*
4085                          * Push another time with a faster pacing
4086                          * to try to gain back (we include override to
4087                          * get a full raise factor).
4088                          */
4089                         if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4090                             (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4091                             (timely_says == 0) ||
4092                             (rack_down_raise_thresh == 0)) {
4093                                 /*
4094                                  * Do an override up in b/w if we were
4095                                  * below the threshold or if the threshold
4096                                  * is zero we always do the raise.
4097                                  */
4098                                 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4099                         } else {
4100                                 /* Log it stays the same */
4101                                 rack_log_timely(rack,  0, last_bw_est, low_bnd, 0,
4102                                                 __LINE__, 11);
4103                         }
4104                         rack->rc_gp_timely_dec_cnt++;
4105                         /* We are not incrementing really no-count */
4106                         rack->rc_gp_incr = 0;
4107                         rack->rc_gp_timely_inc_cnt = 0;
4108                 } else {
4109                         /*
4110                          * Lets just use the RTT
4111                          * information and give up
4112                          * pushing.
4113                          */
4114                         goto use_timely;
4115                 }
4116         } else if ((timely_says != 2) &&
4117                     !losses &&
4118                     (last_bw_est > up_bnd)) {
4119                 /*
4120                  * We are increasing b/w lets keep going, updating
4121                  * our b/w and ignoring any timely input, unless
4122                  * of course we are at our max raise (if there is one).
4123                  */
4124
4125                 rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4126                                 __LINE__, 3);
4127                 rack->r_ctl.last_gp_comp_bw = cur_bw;
4128                 if (rack->rc_gp_saw_ss &&
4129                     rack_per_upper_bound_ss &&
4130                      (rack->r_ctl.rack_per_of_gp_ss == rack_per_upper_bound_ss)) {
4131                             /*
4132                              * In cases where we can't go higher
4133                              * we should just use timely.
4134                              */
4135                             goto use_timely;
4136                 }
4137                 if (rack->rc_gp_saw_ca &&
4138                     rack_per_upper_bound_ca &&
4139                     (rack->r_ctl.rack_per_of_gp_ca == rack_per_upper_bound_ca)) {
4140                             /*
4141                              * In cases where we can't go higher
4142                              * we should just use timely.
4143                              */
4144                             goto use_timely;
4145                 }
4146                 rack->rc_gp_bwred = 0;
4147                 rack->rc_gp_timely_dec_cnt = 0;
4148                 /* You get a set number of pushes if timely is trying to reduce */
4149                 if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4150                         rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4151                 } else {
4152                         /* Log it stays the same */
4153                         rack_log_timely(rack,  0, last_bw_est, up_bnd, 0,
4154                             __LINE__, 12);
4155                 }
4156                 return;
4157         } else {
4158                 /*
4159                  * We are staying between the lower and upper range bounds
4160                  * so use timely to decide.
4161                  */
4162                 rack_log_timely(rack,  timely_says, cur_bw, low_bnd, up_bnd,
4163                                 __LINE__, 3);
4164 use_timely:
4165                 if (timely_says) {
4166                         rack->rc_gp_incr = 0;
4167                         rack->rc_gp_timely_inc_cnt = 0;
4168                         if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4169                             !losses &&
4170                             (last_bw_est < low_bnd)) {
4171                                 /* We are loosing ground */
4172                                 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4173                                 rack->rc_gp_timely_dec_cnt++;
4174                                 /* We are not incrementing really no-count */
4175                                 rack->rc_gp_incr = 0;
4176                                 rack->rc_gp_timely_inc_cnt = 0;
4177                         } else
4178                                 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4179                 } else {
4180                         rack->rc_gp_bwred = 0;
4181                         rack->rc_gp_timely_dec_cnt = 0;
4182                         rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4183                 }
4184         }
4185 }
4186
4187 static int32_t
4188 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4189 {
4190         int32_t timely_says;
4191         uint64_t log_mult, log_rtt_a_diff;
4192
4193         log_rtt_a_diff = rtt;
4194         log_rtt_a_diff <<= 32;
4195         log_rtt_a_diff |= (uint32_t)rtt_diff;
4196         if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4197                     rack_gp_rtt_maxmul)) {
4198                 /* Reduce the b/w multipler */
4199                 timely_says = 2;
4200                 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4201                 log_mult <<= 32;
4202                 log_mult |= prev_rtt;
4203                 rack_log_timely(rack,  timely_says, log_mult,
4204                                 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4205                                 log_rtt_a_diff, __LINE__, 4);
4206         } else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4207                            ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4208                             max(rack_gp_rtt_mindiv , 1)))) {
4209                 /* Increase the b/w multipler */
4210                 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4211                         ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4212                          max(rack_gp_rtt_mindiv , 1));
4213                 log_mult <<= 32;
4214                 log_mult |= prev_rtt;
4215                 timely_says = 0;
4216                 rack_log_timely(rack,  timely_says, log_mult ,
4217                                 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4218                                 log_rtt_a_diff, __LINE__, 5);
4219         } else {
4220                 /*
4221                  * Use a gradient to find it the timely gradient
4222                  * is:
4223                  * grad = rc_rtt_diff / min_rtt;
4224                  *
4225                  * anything below or equal to 0 will be
4226                  * a increase indication. Anything above
4227                  * zero is a decrease. Note we take care
4228                  * of the actual gradient calculation
4229                  * in the reduction (its not needed for
4230                  * increase).
4231                  */
4232                 log_mult = prev_rtt;
4233                 if (rtt_diff <= 0) {
4234                         /*
4235                          * Rttdiff is less than zero, increase the
4236                          * b/w multipler (its 0 or negative)
4237                          */
4238                         timely_says = 0;
4239                         rack_log_timely(rack,  timely_says, log_mult,
4240                                         get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4241                 } else {
4242                         /* Reduce the b/w multipler */
4243                         timely_says = 1;
4244                         rack_log_timely(rack,  timely_says, log_mult,
4245                                         get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4246                 }
4247         }
4248         return (timely_says);
4249 }
4250
4251 static void
4252 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4253                             tcp_seq th_ack, int line, uint8_t quality)
4254 {
4255         uint64_t tim, bytes_ps, ltim, stim, utim;
4256         uint32_t segsiz, bytes, reqbytes, us_cts;
4257         int32_t gput, new_rtt_diff, timely_says;
4258         uint64_t  resid_bw, subpart = 0, addpart = 0, srtt;
4259         int did_add = 0;
4260
4261         us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4262         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4263         if (TSTMP_GEQ(us_cts, tp->gput_ts))
4264                 tim = us_cts - tp->gput_ts;
4265         else
4266                 tim = 0;
4267         if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4268                 stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4269         else
4270                 stim = 0;
4271         /*
4272          * Use the larger of the send time or ack time. This prevents us
4273          * from being influenced by ack artifacts to come up with too
4274          * high of measurement. Note that since we are spanning over many more
4275          * bytes in most of our measurements hopefully that is less likely to
4276          * occur.
4277          */
4278         if (tim > stim)
4279                 utim = max(tim, 1);
4280         else
4281                 utim = max(stim, 1);
4282         /* Lets get a msec time ltim too for the old stuff */
4283         ltim = max(1, (utim / HPTS_USEC_IN_MSEC));
4284         gput = (((uint64_t) (th_ack - tp->gput_seq)) << 3) / ltim;
4285         reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4286         if ((tim == 0) && (stim == 0)) {
4287                 /*
4288                  * Invalid measurement time, maybe
4289                  * all on one ack/one send?
4290                  */
4291                 bytes = 0;
4292                 bytes_ps = 0;
4293                 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4294                                            0, 0, 0, 10, __LINE__, NULL, quality);
4295                 goto skip_measurement;
4296         }
4297         if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4298                 /* We never made a us_rtt measurement? */
4299                 bytes = 0;
4300                 bytes_ps = 0;
4301                 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4302                                            0, 0, 0, 10, __LINE__, NULL, quality);
4303                 goto skip_measurement;
4304         }
4305         /*
4306          * Calculate the maximum possible b/w this connection
4307          * could have. We base our calculation on the lowest
4308          * rtt we have seen during the measurement and the
4309          * largest rwnd the client has given us in that time. This
4310          * forms a BDP that is the maximum that we could ever
4311          * get to the client. Anything larger is not valid.
4312          *
4313          * I originally had code here that rejected measurements
4314          * where the time was less than 1/2 the latest us_rtt.
4315          * But after thinking on that I realized its wrong since
4316          * say you had a 150Mbps or even 1Gbps link, and you
4317          * were a long way away.. example I am in Europe (100ms rtt)
4318          * talking to my 1Gbps link in S.C. Now measuring say 150,000
4319          * bytes my time would be 1.2ms, and yet my rtt would say
4320          * the measurement was invalid the time was < 50ms. The
4321          * same thing is true for 150Mb (8ms of time).
4322          *
4323          * A better way I realized is to look at what the maximum
4324          * the connection could possibly do. This is gated on
4325          * the lowest RTT we have seen and the highest rwnd.
4326          * We should in theory never exceed that, if we are
4327          * then something on the path is storing up packets
4328          * and then feeding them all at once to our endpoint
4329          * messing up our measurement.
4330          */
4331         rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4332         rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4333         rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4334         if (SEQ_LT(th_ack, tp->gput_seq)) {
4335                 /* No measurement can be made */
4336                 bytes = 0;
4337                 bytes_ps = 0;
4338                 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4339                                            0, 0, 0, 10, __LINE__, NULL, quality);
4340                 goto skip_measurement;
4341         } else
4342                 bytes = (th_ack - tp->gput_seq);
4343         bytes_ps = (uint64_t)bytes;
4344         /*
4345          * Don't measure a b/w for pacing unless we have gotten at least
4346          * an initial windows worth of data in this measurement interval.
4347          *
4348          * Small numbers of bytes get badly influenced by delayed ack and
4349          * other artifacts. Note we take the initial window or our
4350          * defined minimum GP (defaulting to 10 which hopefully is the
4351          * IW).
4352          */
4353         if (rack->rc_gp_filled == 0) {
4354                 /*
4355                  * The initial estimate is special. We
4356                  * have blasted out an IW worth of packets
4357                  * without a real valid ack ts results. We
4358                  * then setup the app_limited_needs_set flag,
4359                  * this should get the first ack in (probably 2
4360                  * MSS worth) to be recorded as the timestamp.
4361                  * We thus allow a smaller number of bytes i.e.
4362                  * IW - 2MSS.
4363                  */
4364                 reqbytes -= (2 * segsiz);
4365                 /* Also lets fill previous for our first measurement to be neutral */
4366                 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4367         }
4368         if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
4369                 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4370                                            rack->r_ctl.rc_app_limited_cnt,
4371                                            0, 0, 10, __LINE__, NULL, quality);
4372                 goto skip_measurement;
4373         }
4374         /*
4375          * We now need to calculate the Timely like status so
4376          * we can update (possibly) the b/w multipliers.
4377          */
4378         new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
4379         if (rack->rc_gp_filled == 0) {
4380                 /* No previous reading */
4381                 rack->r_ctl.rc_rtt_diff = new_rtt_diff;
4382         } else {
4383                 if (rack->measure_saw_probe_rtt == 0) {
4384                         /*
4385                          * We don't want a probertt to be counted
4386                          * since it will be negative incorrectly. We
4387                          * expect to be reducing the RTT when we
4388                          * pace at a slower rate.
4389                          */
4390                         rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
4391                         rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
4392                 }
4393         }
4394         timely_says = rack_make_timely_judgement(rack,
4395                 rack->r_ctl.rc_gp_srtt,
4396                 rack->r_ctl.rc_rtt_diff,
4397                 rack->r_ctl.rc_prev_gp_srtt
4398                 );
4399         bytes_ps *= HPTS_USEC_IN_SEC;
4400         bytes_ps /= utim;
4401         if (bytes_ps > rack->r_ctl.last_max_bw) {
4402                 /*
4403                  * Something is on path playing
4404                  * since this b/w is not possible based
4405                  * on our BDP (highest rwnd and lowest rtt
4406                  * we saw in the measurement window).
4407                  *
4408                  * Another option here would be to
4409                  * instead skip the measurement.
4410                  */
4411                 rack_log_pacing_delay_calc(rack, bytes, reqbytes,
4412                                            bytes_ps, rack->r_ctl.last_max_bw, 0,
4413                                            11, __LINE__, NULL, quality);
4414                 bytes_ps = rack->r_ctl.last_max_bw;
4415         }
4416         /* We store gp for b/w in bytes per second */
4417         if (rack->rc_gp_filled == 0) {
4418                 /* Initial measurment */
4419                 if (bytes_ps) {
4420                         rack->r_ctl.gp_bw = bytes_ps;
4421                         rack->rc_gp_filled = 1;
4422                         rack->r_ctl.num_measurements = 1;
4423                         rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
4424                 } else {
4425                         rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4426                                                    rack->r_ctl.rc_app_limited_cnt,
4427                                                    0, 0, 10, __LINE__, NULL, quality);
4428                 }
4429                 if (rack->rc_inp->inp_in_hpts &&
4430                     (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
4431                         /*
4432                          * Ok we can't trust the pacer in this case
4433                          * where we transition from un-paced to paced.
4434                          * Or for that matter when the burst mitigation
4435                          * was making a wild guess and got it wrong.
4436                          * Stop the pacer and clear up all the aggregate
4437                          * delays etc.
4438                          */
4439                         tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
4440                         rack->r_ctl.rc_hpts_flags = 0;
4441                         rack->r_ctl.rc_last_output_to = 0;
4442                 }
4443                 did_add = 2;
4444         } else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
4445                 /* Still a small number run an average */
4446                 rack->r_ctl.gp_bw += bytes_ps;
4447                 addpart = rack->r_ctl.num_measurements;
4448                 rack->r_ctl.num_measurements++;
4449                 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
4450                         /* We have collected enought to move forward */
4451                         rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
4452                 }
4453                 did_add = 3;
4454         } else {
4455                 /*
4456                  * We want to take 1/wma of the goodput and add in to 7/8th
4457                  * of the old value weighted by the srtt. So if your measurement
4458                  * period is say 2 SRTT's long you would get 1/4 as the
4459                  * value, if it was like 1/2 SRTT then you would get 1/16th.
4460                  *
4461                  * But we must be careful not to take too much i.e. if the
4462                  * srtt is say 20ms and the measurement is taken over
4463                  * 400ms our weight would be 400/20 i.e. 20. On the
4464                  * other hand if we get a measurement over 1ms with a
4465                  * 10ms rtt we only want to take a much smaller portion.
4466                  */
4467                 if (rack->r_ctl.num_measurements < 0xff) {
4468                         rack->r_ctl.num_measurements++;
4469                 }
4470                 srtt = (uint64_t)tp->t_srtt;
4471                 if (srtt == 0) {
4472                         /*
4473                          * Strange why did t_srtt go back to zero?
4474                          */
4475                         if (rack->r_ctl.rc_rack_min_rtt)
4476                                 srtt = rack->r_ctl.rc_rack_min_rtt;
4477                         else
4478                                 srtt = HPTS_USEC_IN_MSEC;
4479                 }
4480                 /*
4481                  * XXXrrs: Note for reviewers, in playing with
4482                  * dynamic pacing I discovered this GP calculation
4483                  * as done originally leads to some undesired results.
4484                  * Basically you can get longer measurements contributing
4485                  * too much to the WMA. Thus I changed it if you are doing
4486                  * dynamic adjustments to only do the aportioned adjustment
4487                  * if we have a very small (time wise) measurement. Longer
4488                  * measurements just get there weight (defaulting to 1/8)
4489                  * add to the WMA. We may want to think about changing
4490                  * this to always do that for both sides i.e. dynamic
4491                  * and non-dynamic... but considering lots of folks
4492                  * were playing with this I did not want to change the
4493                  * calculation per.se. without your thoughts.. Lawerence?
4494                  * Peter??
4495                  */
4496                 if (rack->rc_gp_dyn_mul == 0) {
4497                         subpart = rack->r_ctl.gp_bw * utim;
4498                         subpart /= (srtt * 8);
4499                         if (subpart < (rack->r_ctl.gp_bw / 2)) {
4500                                 /*
4501                                  * The b/w update takes no more
4502                                  * away then 1/2 our running total
4503                                  * so factor it in.
4504                                  */
4505                                 addpart = bytes_ps * utim;
4506                                 addpart /= (srtt * 8);
4507                         } else {
4508                                 /*
4509                                  * Don't allow a single measurement
4510                                  * to account for more than 1/2 of the
4511                                  * WMA. This could happen on a retransmission
4512                                  * where utim becomes huge compared to
4513                                  * srtt (multiple retransmissions when using
4514                                  * the sending rate which factors in all the
4515                                  * transmissions from the first one).
4516                                  */
4517                                 subpart = rack->r_ctl.gp_bw / 2;
4518                                 addpart = bytes_ps / 2;
4519                         }
4520                         resid_bw = rack->r_ctl.gp_bw - subpart;
4521                         rack->r_ctl.gp_bw = resid_bw + addpart;
4522                         did_add = 1;
4523                 } else {
4524                         if ((utim / srtt) <= 1) {
4525                                 /*
4526                                  * The b/w update was over a small period
4527                                  * of time. The idea here is to prevent a small
4528                                  * measurement time period from counting
4529                                  * too much. So we scale it based on the
4530                                  * time so it attributes less than 1/rack_wma_divisor
4531                                  * of its measurement.
4532                                  */
4533                                 subpart = rack->r_ctl.gp_bw * utim;
4534                                 subpart /= (srtt * rack_wma_divisor);
4535                                 addpart = bytes_ps * utim;
4536                                 addpart /= (srtt * rack_wma_divisor);
4537                         } else {
4538                                 /*
4539                                  * The scaled measurement was long
4540                                  * enough so lets just add in the
4541                                  * portion of the measurment i.e. 1/rack_wma_divisor
4542                                  */
4543                                 subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
4544                                 addpart = bytes_ps / rack_wma_divisor;
4545                         }
4546                         if ((rack->measure_saw_probe_rtt == 0) ||
4547                             (bytes_ps > rack->r_ctl.gp_bw)) {
4548                                 /*
4549                                  * For probe-rtt we only add it in
4550                                  * if its larger, all others we just
4551                                  * add in.
4552                                  */
4553                                 did_add = 1;
4554                                 resid_bw = rack->r_ctl.gp_bw - subpart;
4555                                 rack->r_ctl.gp_bw = resid_bw + addpart;
4556                         }
4557                 }
4558         }
4559         if ((rack->gp_ready == 0) &&
4560             (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
4561                 /* We have enough measurements now */
4562                 rack->gp_ready = 1;
4563                 rack_set_cc_pacing(rack);
4564                 if (rack->defer_options)
4565                         rack_apply_deferred_options(rack);
4566         }
4567         rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
4568                                    rack_get_bw(rack), 22, did_add, NULL, quality);
4569         /* We do not update any multipliers if we are in or have seen a probe-rtt */
4570         if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
4571                 rack_update_multiplier(rack, timely_says, bytes_ps,
4572                                        rack->r_ctl.rc_gp_srtt,
4573                                        rack->r_ctl.rc_rtt_diff);
4574         rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
4575                                    rack_get_bw(rack), 3, line, NULL, quality);
4576         /* reset the gp srtt and setup the new prev */
4577         rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4578         /* Record the lost count for the next measurement */
4579         rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
4580         /*
4581          * We restart our diffs based on the gpsrtt in the
4582          * measurement window.
4583          */
4584         rack->rc_gp_rtt_set = 0;
4585         rack->rc_gp_saw_rec = 0;
4586         rack->rc_gp_saw_ca = 0;
4587         rack->rc_gp_saw_ss = 0;
4588         rack->rc_dragged_bottom = 0;
4589 skip_measurement:
4590
4591 #ifdef STATS
4592         stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
4593                                  gput);
4594         /*
4595          * XXXLAS: This is a temporary hack, and should be
4596          * chained off VOI_TCP_GPUT when stats(9) grows an
4597          * API to deal with chained VOIs.
4598          */
4599         if (tp->t_stats_gput_prev > 0)
4600                 stats_voi_update_abs_s32(tp->t_stats,
4601                                          VOI_TCP_GPUT_ND,
4602                                          ((gput - tp->t_stats_gput_prev) * 100) /
4603                                          tp->t_stats_gput_prev);
4604 #endif
4605         tp->t_flags &= ~TF_GPUTINPROG;
4606         tp->t_stats_gput_prev = gput;
4607         /*
4608          * Now are we app limited now and there is space from where we
4609          * were to where we want to go?
4610          *
4611          * We don't do the other case i.e. non-applimited here since
4612          * the next send will trigger us picking up the missing data.
4613          */
4614         if (rack->r_ctl.rc_first_appl &&
4615             TCPS_HAVEESTABLISHED(tp->t_state) &&
4616             rack->r_ctl.rc_app_limited_cnt &&
4617             (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
4618             ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
4619              max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
4620                 /*
4621                  * Yep there is enough outstanding to make a measurement here.
4622                  */
4623                 struct rack_sendmap *rsm, fe;
4624
4625                 rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
4626                 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
4627                 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4628                 rack->app_limited_needs_set = 0;
4629                 tp->gput_seq = th_ack;
4630                 if (rack->in_probe_rtt)
4631                         rack->measure_saw_probe_rtt = 1;
4632                 else if ((rack->measure_saw_probe_rtt) &&
4633                          (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
4634                         rack->measure_saw_probe_rtt = 0;
4635                 if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
4636                         /* There is a full window to gain info from */
4637                         tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
4638                 } else {
4639                         /* We can only measure up to the applimited point */
4640                         tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
4641                         if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
4642                                 /*
4643                                  * We don't have enough to make a measurement.
4644                                  */
4645                                 tp->t_flags &= ~TF_GPUTINPROG;
4646                                 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
4647                                                            0, 0, 0, 6, __LINE__, NULL, quality);
4648                                 return;
4649                         }
4650                 }
4651                 if (tp->t_state >= TCPS_FIN_WAIT_1) {
4652                         /*
4653                          * We will get no more data into the SB
4654                          * this means we need to have the data available
4655                          * before we start a measurement.
4656                          */
4657                         if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < (tp->gput_ack - tp->gput_seq)) {
4658                                 /* Nope not enough data. */
4659                                 return;
4660                         }
4661                 }
4662                 tp->t_flags |= TF_GPUTINPROG;
4663                 /*
4664                  * Now we need to find the timestamp of the send at tp->gput_seq
4665                  * for the send based measurement.
4666                  */
4667                 fe.r_start = tp->gput_seq;
4668                 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
4669                 if (rsm) {
4670                         /* Ok send-based limit is set */
4671                         if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
4672                                 /*
4673                                  * Move back to include the earlier part
4674                                  * so our ack time lines up right (this may
4675                                  * make an overlapping measurement but thats
4676                                  * ok).
4677                                  */
4678                                 tp->gput_seq = rsm->r_start;
4679                         }
4680                         if (rsm->r_flags & RACK_ACKED)
4681                                 tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
4682                         else
4683                                 rack->app_limited_needs_set = 1;
4684                         rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
4685                 } else {
4686                         /*
4687                          * If we don't find the rsm due to some
4688                          * send-limit set the current time, which
4689                          * basically disables the send-limit.
4690                          */
4691                         struct timeval tv;
4692
4693                         microuptime(&tv);
4694                         rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
4695                 }
4696                 rack_log_pacing_delay_calc(rack,
4697                                            tp->gput_seq,
4698                                            tp->gput_ack,
4699                                            (uint64_t)rsm,
4700                                            tp->gput_ts,
4701                                            rack->r_ctl.rc_app_limited_cnt,
4702                                            9,
4703                                            __LINE__, NULL, quality);
4704         }
4705 }
4706
4707 /*
4708  * CC wrapper hook functions
4709  */
4710 static void
4711 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
4712     uint16_t type, int32_t recovery)
4713 {
4714         uint32_t prior_cwnd, acked;
4715         struct tcp_log_buffer *lgb = NULL;
4716         uint8_t labc_to_use, quality;
4717
4718         INP_WLOCK_ASSERT(tp->t_inpcb);
4719         tp->ccv->nsegs = nsegs;
4720         acked = tp->ccv->bytes_this_ack = (th_ack - tp->snd_una);
4721         if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
4722                 uint32_t max;
4723
4724                 max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
4725                 if (tp->ccv->bytes_this_ack > max) {
4726                         tp->ccv->bytes_this_ack = max;
4727                 }
4728         }
4729 #ifdef STATS
4730         stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
4731             ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
4732 #endif
4733         quality = RACK_QUALITY_NONE;
4734         if ((tp->t_flags & TF_GPUTINPROG) &&
4735             rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
4736                 /* Measure the Goodput */
4737                 rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
4738 #ifdef NETFLIX_PEAKRATE
4739                 if ((type == CC_ACK) &&
4740                     (tp->t_maxpeakrate)) {
4741                         /*
4742                          * We update t_peakrate_thr. This gives us roughly
4743                          * one update per round trip time. Note
4744                          * it will only be used if pace_always is off i.e
4745                          * we don't do this for paced flows.
4746                          */
4747                         rack_update_peakrate_thr(tp);
4748                 }
4749 #endif
4750         }
4751         /* Which way our we limited, if not cwnd limited no advance in CA */
4752         if (tp->snd_cwnd <= tp->snd_wnd)
4753                 tp->ccv->flags |= CCF_CWND_LIMITED;
4754         else
4755                 tp->ccv->flags &= ~CCF_CWND_LIMITED;
4756         if (tp->snd_cwnd > tp->snd_ssthresh) {
4757                 tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
4758                          nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
4759                 /* For the setting of a window past use the actual scwnd we are using */
4760                 if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
4761                         tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
4762                         tp->ccv->flags |= CCF_ABC_SENTAWND;
4763                 }
4764         } else {
4765                 tp->ccv->flags &= ~CCF_ABC_SENTAWND;
4766                 tp->t_bytes_acked = 0;
4767         }
4768         prior_cwnd = tp->snd_cwnd;
4769         if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
4770             (rack_client_low_buf && (rack->client_bufferlvl < rack_client_low_buf)))
4771                 labc_to_use = rack->rc_labc;
4772         else
4773                 labc_to_use = rack_max_abc_post_recovery;
4774         if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4775                 union tcp_log_stackspecific log;
4776                 struct timeval tv;
4777
4778                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4779                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4780                 log.u_bbr.flex1 = th_ack;
4781                 log.u_bbr.flex2 = tp->ccv->flags;
4782                 log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4783                 log.u_bbr.flex4 = tp->ccv->nsegs;
4784                 log.u_bbr.flex5 = labc_to_use;
4785                 log.u_bbr.flex6 = prior_cwnd;
4786                 log.u_bbr.flex7 = V_tcp_do_newsack;
4787                 log.u_bbr.flex8 = 1;
4788                 lgb = tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4789                                      0, &log, false, NULL, NULL, 0, &tv);
4790         }
4791         if (CC_ALGO(tp)->ack_received != NULL) {
4792                 /* XXXLAS: Find a way to live without this */
4793                 tp->ccv->curack = th_ack;
4794                 tp->ccv->labc = labc_to_use;
4795                 tp->ccv->flags |= CCF_USE_LOCAL_ABC;
4796                 CC_ALGO(tp)->ack_received(tp->ccv, type);
4797         }
4798         if (lgb) {
4799                 lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
4800         }
4801         if (rack->r_must_retran) {
4802                 if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
4803                         /*
4804                          * We now are beyond the rxt point so lets disable
4805                          * the flag.
4806                          */
4807                         rack->r_ctl.rc_out_at_rto = 0;
4808                         rack->r_must_retran = 0;
4809                 } else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
4810                         /*
4811                          * Only decrement the rc_out_at_rto if the cwnd advances
4812                          * at least a whole segment. Otherwise next time the peer
4813                          * acks, we won't be able to send this generaly happens
4814                          * when we are in Congestion Avoidance.
4815                          */
4816                         if (acked <= rack->r_ctl.rc_out_at_rto){
4817                                 rack->r_ctl.rc_out_at_rto -= acked;
4818                         } else {
4819                                 rack->r_ctl.rc_out_at_rto = 0;
4820                         }
4821                 }
4822         }
4823 #ifdef STATS
4824         stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
4825 #endif
4826         if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
4827                 rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
4828         }
4829 #ifdef NETFLIX_PEAKRATE
4830         /* we enforce max peak rate if it is set and we are not pacing */
4831         if ((rack->rc_always_pace == 0) &&
4832             tp->t_peakrate_thr &&
4833             (tp->snd_cwnd > tp->t_peakrate_thr)) {
4834                 tp->snd_cwnd = tp->t_peakrate_thr;
4835         }
4836 #endif
4837 }
4838
4839 static void
4840 tcp_rack_partialack(struct tcpcb *tp)
4841 {
4842         struct tcp_rack *rack;
4843
4844         rack = (struct tcp_rack *)tp->t_fb_ptr;
4845         INP_WLOCK_ASSERT(tp->t_inpcb);
4846         /*
4847          * If we are doing PRR and have enough
4848          * room to send <or> we are pacing and prr
4849          * is disabled we will want to see if we
4850          * can send data (by setting r_wanted_output to
4851          * true).
4852          */
4853         if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
4854             rack->rack_no_prr)
4855                 rack->r_wanted_output = 1;
4856 }
4857
4858 static void
4859 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
4860 {
4861         struct tcp_rack *rack;
4862         uint32_t orig_cwnd;
4863
4864         orig_cwnd = tp->snd_cwnd;
4865         INP_WLOCK_ASSERT(tp->t_inpcb);
4866         rack = (struct tcp_rack *)tp->t_fb_ptr;
4867         /* only alert CC if we alerted when we entered */
4868         if (CC_ALGO(tp)->post_recovery != NULL) {
4869                 tp->ccv->curack = th_ack;
4870                 CC_ALGO(tp)->post_recovery(tp->ccv);
4871                 if (tp->snd_cwnd < tp->snd_ssthresh) {
4872                         /*
4873                          * Rack has burst control and pacing
4874                          * so lets not set this any lower than
4875                          * snd_ssthresh per RFC-6582 (option 2).
4876                          */
4877                         tp->snd_cwnd = tp->snd_ssthresh;
4878                 }
4879         }
4880         if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4881                 union tcp_log_stackspecific log;
4882                 struct timeval tv;
4883
4884                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4885                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4886                 log.u_bbr.flex1 = th_ack;
4887                 log.u_bbr.flex2 = tp->ccv->flags;
4888                 log.u_bbr.flex3 = tp->ccv->bytes_this_ack;
4889                 log.u_bbr.flex4 = tp->ccv->nsegs;
4890                 log.u_bbr.flex5 = V_tcp_abc_l_var;
4891                 log.u_bbr.flex6 = orig_cwnd;
4892                 log.u_bbr.flex7 = V_tcp_do_newsack;
4893                 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
4894                 log.u_bbr.flex8 = 2;
4895                 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
4896                                0, &log, false, NULL, NULL, 0, &tv);
4897         }
4898         if ((rack->rack_no_prr == 0) &&
4899             (rack->no_prr_addback == 0) &&
4900             (rack->r_ctl.rc_prr_sndcnt > 0)) {
4901                 /*
4902                  * Suck the next prr cnt back into cwnd, but
4903                  * only do that if we are not application limited.
4904                  */
4905                 if (ctf_outstanding(tp) <= sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
4906                         /*
4907                          * We are allowed to add back to the cwnd the amount we did
4908                          * not get out if:
4909                          * a) no_prr_addback is off.
4910                          * b) we are not app limited
4911                          * c) we are doing prr
4912                          * <and>
4913                          * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
4914                          */
4915                         tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
4916                                             rack->r_ctl.rc_prr_sndcnt);
4917                 }
4918                 rack->r_ctl.rc_prr_sndcnt = 0;
4919                 rack_log_to_prr(rack, 1, 0);
4920         }
4921         rack_log_to_prr(rack, 14, orig_cwnd);
4922         tp->snd_recover = tp->snd_una;
4923         if (rack->r_ctl.dsack_persist) {
4924                 rack->r_ctl.dsack_persist--;
4925                 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
4926                         rack->r_ctl.num_dsack = 0;
4927                 }
4928                 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
4929         }
4930         EXIT_RECOVERY(tp->t_flags);
4931 }
4932
4933 static void
4934 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack)
4935 {
4936         struct tcp_rack *rack;
4937         uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
4938
4939         INP_WLOCK_ASSERT(tp->t_inpcb);
4940 #ifdef STATS
4941         stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
4942 #endif
4943         if (IN_RECOVERY(tp->t_flags) == 0) {
4944                 in_rec_at_entry = 0;
4945                 ssthresh_enter = tp->snd_ssthresh;
4946                 cwnd_enter = tp->snd_cwnd;
4947         } else
4948                 in_rec_at_entry = 1;
4949         rack = (struct tcp_rack *)tp->t_fb_ptr;
4950         switch (type) {
4951         case CC_NDUPACK:
4952                 tp->t_flags &= ~TF_WASFRECOVERY;
4953                 tp->t_flags &= ~TF_WASCRECOVERY;
4954                 if (!IN_FASTRECOVERY(tp->t_flags)) {
4955                         rack->r_ctl.rc_prr_delivered = 0;
4956                         rack->r_ctl.rc_prr_out = 0;
4957                         if (rack->rack_no_prr == 0) {
4958                                 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
4959                                 rack_log_to_prr(rack, 2, in_rec_at_entry);
4960                         }
4961                         rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
4962                         tp->snd_recover = tp->snd_max;
4963                         if (tp->t_flags2 & TF2_ECN_PERMIT)
4964                                 tp->t_flags2 |= TF2_ECN_SND_CWR;
4965                 }
4966                 break;
4967         case CC_ECN:
4968                 if (!IN_CONGRECOVERY(tp->t_flags) ||
4969                     /*
4970                      * Allow ECN reaction on ACK to CWR, if
4971                      * that data segment was also CE marked.
4972                      */
4973                     SEQ_GEQ(ack, tp->snd_recover)) {
4974                         EXIT_CONGRECOVERY(tp->t_flags);
4975                         KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
4976                         tp->snd_recover = tp->snd_max + 1;
4977                         if (tp->t_flags2 & TF2_ECN_PERMIT)
4978                                 tp->t_flags2 |= TF2_ECN_SND_CWR;
4979                 }
4980                 break;
4981         case CC_RTO:
4982                 tp->t_dupacks = 0;
4983                 tp->t_bytes_acked = 0;
4984                 EXIT_RECOVERY(tp->t_flags);
4985                 tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
4986                     ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
4987                 orig_cwnd = tp->snd_cwnd;
4988                 tp->snd_cwnd = ctf_fixed_maxseg(tp);
4989                 rack_log_to_prr(rack, 16, orig_cwnd);
4990                 if (tp->t_flags2 & TF2_ECN_PERMIT)
4991                         tp->t_flags2 |= TF2_ECN_SND_CWR;
4992                 break;
4993         case CC_RTO_ERR:
4994                 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4995                 /* RTO was unnecessary, so reset everything. */
4996                 tp->snd_cwnd = tp->snd_cwnd_prev;
4997                 tp->snd_ssthresh = tp->snd_ssthresh_prev;
4998                 tp->snd_recover = tp->snd_recover_prev;
4999                 if (tp->t_flags & TF_WASFRECOVERY) {
5000                         ENTER_FASTRECOVERY(tp->t_flags);
5001                         tp->t_flags &= ~TF_WASFRECOVERY;
5002                 }
5003                 if (tp->t_flags & TF_WASCRECOVERY) {
5004                         ENTER_CONGRECOVERY(tp->t_flags);
5005                         tp->t_flags &= ~TF_WASCRECOVERY;
5006                 }
5007                 tp->snd_nxt = tp->snd_max;
5008                 tp->t_badrxtwin = 0;
5009                 break;
5010         }
5011         if ((CC_ALGO(tp)->cong_signal != NULL)  &&
5012             (type != CC_RTO)){
5013                 tp->ccv->curack = ack;
5014                 CC_ALGO(tp)->cong_signal(tp->ccv, type);
5015         }
5016         if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5017                 rack_log_to_prr(rack, 15, cwnd_enter);
5018                 rack->r_ctl.dsack_byte_cnt = 0;
5019                 rack->r_ctl.retran_during_recovery = 0;
5020                 rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5021                 rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5022                 rack->r_ent_rec_ns = 1;
5023         }
5024 }
5025
5026 static inline void
5027 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5028 {
5029         uint32_t i_cwnd;
5030
5031         INP_WLOCK_ASSERT(tp->t_inpcb);
5032
5033 #ifdef NETFLIX_STATS
5034         KMOD_TCPSTAT_INC(tcps_idle_restarts);
5035         if (tp->t_state == TCPS_ESTABLISHED)
5036                 KMOD_TCPSTAT_INC(tcps_idle_estrestarts);
5037 #endif
5038         if (CC_ALGO(tp)->after_idle != NULL)
5039                 CC_ALGO(tp)->after_idle(tp->ccv);
5040
5041         if (tp->snd_cwnd == 1)
5042                 i_cwnd = tp->t_maxseg;          /* SYN(-ACK) lost */
5043         else
5044                 i_cwnd = rc_init_window(rack);
5045
5046         /*
5047          * Being idle is no differnt than the initial window. If the cc
5048          * clamps it down below the initial window raise it to the initial
5049          * window.
5050          */
5051         if (tp->snd_cwnd < i_cwnd) {
5052                 tp->snd_cwnd = i_cwnd;
5053         }
5054 }
5055
5056 /*
5057  * Indicate whether this ack should be delayed.  We can delay the ack if
5058  * following conditions are met:
5059  *      - There is no delayed ack timer in progress.
5060  *      - Our last ack wasn't a 0-sized window. We never want to delay
5061  *        the ack that opens up a 0-sized window.
5062  *      - LRO wasn't used for this segment. We make sure by checking that the
5063  *        segment size is not larger than the MSS.
5064  *      - Delayed acks are enabled or this is a half-synchronized T/TCP
5065  *        connection.
5066  */
5067 #define DELAY_ACK(tp, tlen)                      \
5068         (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
5069         ((tp->t_flags & TF_DELACK) == 0) &&      \
5070         (tlen <= tp->t_maxseg) &&                \
5071         (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
5072
5073 static struct rack_sendmap *
5074 rack_find_lowest_rsm(struct tcp_rack *rack)
5075 {
5076         struct rack_sendmap *rsm;
5077
5078         /*
5079          * Walk the time-order transmitted list looking for an rsm that is
5080          * not acked. This will be the one that was sent the longest time
5081          * ago that is still outstanding.
5082          */
5083         TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5084                 if (rsm->r_flags & RACK_ACKED) {
5085                         continue;
5086                 }
5087                 goto finish;
5088         }
5089 finish:
5090         return (rsm);
5091 }
5092
5093 static struct rack_sendmap *
5094 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5095 {
5096         struct rack_sendmap *prsm;
5097
5098         /*
5099          * Walk the sequence order list backward until we hit and arrive at
5100          * the highest seq not acked. In theory when this is called it
5101          * should be the last segment (which it was not).
5102          */
5103         counter_u64_add(rack_find_high, 1);
5104         prsm = rsm;
5105         RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) {
5106                 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5107                         continue;
5108                 }
5109                 return (prsm);
5110         }
5111         return (NULL);
5112 }
5113
5114 static uint32_t
5115 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
5116 {
5117         int32_t lro;
5118         uint32_t thresh;
5119
5120         /*
5121          * lro is the flag we use to determine if we have seen reordering.
5122          * If it gets set we have seen reordering. The reorder logic either
5123          * works in one of two ways:
5124          *
5125          * If reorder-fade is configured, then we track the last time we saw
5126          * re-ordering occur. If we reach the point where enough time as
5127          * passed we no longer consider reordering has occuring.
5128          *
5129          * Or if reorder-face is 0, then once we see reordering we consider
5130          * the connection to alway be subject to reordering and just set lro
5131          * to 1.
5132          *
5133          * In the end if lro is non-zero we add the extra time for
5134          * reordering in.
5135          */
5136         if (srtt == 0)
5137                 srtt = 1;
5138         if (rack->r_ctl.rc_reorder_ts) {
5139                 if (rack->r_ctl.rc_reorder_fade) {
5140                         if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
5141                                 lro = cts - rack->r_ctl.rc_reorder_ts;
5142                                 if (lro == 0) {
5143                                         /*
5144                                          * No time as passed since the last
5145                                          * reorder, mark it as reordering.
5146                                          */
5147                                         lro = 1;
5148                                 }
5149                         } else {
5150                                 /* Negative time? */
5151                                 lro = 0;
5152                         }
5153                         if (lro > rack->r_ctl.rc_reorder_fade) {
5154                                 /* Turn off reordering seen too */
5155                                 rack->r_ctl.rc_reorder_ts = 0;
5156                                 lro = 0;
5157                         }
5158                 } else {
5159                         /* Reodering does not fade */
5160                         lro = 1;
5161                 }
5162         } else {
5163                 lro = 0;
5164         }
5165         if (rack->rc_rack_tmr_std_based == 0) {
5166                 thresh = srtt + rack->r_ctl.rc_pkt_delay;
5167         } else {
5168                 /* Standards based pkt-delay is 1/4 srtt */
5169                 thresh = srtt +  (srtt >> 2);
5170         }
5171         if (lro && (rack->rc_rack_tmr_std_based == 0)) {
5172                 /* It must be set, if not you get 1/4 rtt */
5173                 if (rack->r_ctl.rc_reorder_shift)
5174                         thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
5175                 else
5176                         thresh += (srtt >> 2);
5177         }
5178         if (rack->rc_rack_use_dsack &&
5179             lro &&
5180             (rack->r_ctl.num_dsack > 0)) {
5181                 /*
5182                  * We only increase the reordering window if we
5183                  * have seen reordering <and> we have a DSACK count.
5184                  */
5185                 thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5186                 rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
5187         }
5188         /* SRTT * 2 is the ceiling */
5189         if (thresh > (srtt * 2)) {
5190                 thresh = srtt * 2;
5191         }
5192         /* And we don't want it above the RTO max either */
5193         if (thresh > rack_rto_max) {
5194                 thresh = rack_rto_max;
5195         }
5196         rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
5197         return (thresh);
5198 }
5199
5200 static uint32_t
5201 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5202                      struct rack_sendmap *rsm, uint32_t srtt)
5203 {
5204         struct rack_sendmap *prsm;
5205         uint32_t thresh, len;
5206         int segsiz;
5207
5208         if (srtt == 0)
5209                 srtt = 1;
5210         if (rack->r_ctl.rc_tlp_threshold)
5211                 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5212         else
5213                 thresh = (srtt * 2);
5214
5215         /* Get the previous sent packet, if any */
5216         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5217         counter_u64_add(rack_enter_tlp_calc, 1);
5218         len = rsm->r_end - rsm->r_start;
5219         if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
5220                 /* Exactly like the ID */
5221                 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
5222                         uint32_t alt_thresh;
5223                         /*
5224                          * Compensate for delayed-ack with the d-ack time.
5225                          */
5226                         counter_u64_add(rack_used_tlpmethod, 1);
5227                         alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5228                         if (alt_thresh > thresh)
5229                                 thresh = alt_thresh;
5230                 }
5231         } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
5232                 /* 2.1 behavior */
5233                 prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
5234                 if (prsm && (len <= segsiz)) {
5235                         /*
5236                          * Two packets outstanding, thresh should be (2*srtt) +
5237                          * possible inter-packet delay (if any).
5238                          */
5239                         uint32_t inter_gap = 0;
5240                         int idx, nidx;
5241
5242                         counter_u64_add(rack_used_tlpmethod, 1);
5243                         idx = rsm->r_rtr_cnt - 1;
5244                         nidx = prsm->r_rtr_cnt - 1;
5245                         if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
5246                                 /* Yes it was sent later (or at the same time) */
5247                                 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
5248                         }
5249                         thresh += inter_gap;
5250                 } else if (len <= segsiz) {
5251                         /*
5252                          * Possibly compensate for delayed-ack.
5253                          */
5254                         uint32_t alt_thresh;
5255
5256                         counter_u64_add(rack_used_tlpmethod2, 1);
5257                         alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5258                         if (alt_thresh > thresh)
5259                                 thresh = alt_thresh;
5260                 }
5261         } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
5262                 /* 2.2 behavior */
5263                 if (len <= segsiz) {
5264                         uint32_t alt_thresh;
5265                         /*
5266                          * Compensate for delayed-ack with the d-ack time.
5267                          */
5268                         counter_u64_add(rack_used_tlpmethod, 1);
5269                         alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
5270                         if (alt_thresh > thresh)
5271                                 thresh = alt_thresh;
5272                 }
5273         }
5274         /* Not above an RTO */
5275         if (thresh > tp->t_rxtcur) {
5276                 thresh = tp->t_rxtcur;
5277         }
5278         /* Not above a RTO max */
5279         if (thresh > rack_rto_max) {
5280                 thresh = rack_rto_max;
5281         }
5282         /* Apply user supplied min TLP */
5283         if (thresh < rack_tlp_min) {
5284                 thresh = rack_tlp_min;
5285         }
5286         return (thresh);
5287 }
5288
5289 static uint32_t
5290 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
5291 {
5292         /*
5293          * We want the rack_rtt which is the
5294          * last rtt we measured. However if that
5295          * does not exist we fallback to the srtt (which
5296          * we probably will never do) and then as a last
5297          * resort we use RACK_INITIAL_RTO if no srtt is
5298          * yet set.
5299          */
5300         if (rack->rc_rack_rtt)
5301                 return (rack->rc_rack_rtt);
5302         else if (tp->t_srtt == 0)
5303                 return (RACK_INITIAL_RTO);
5304         return (tp->t_srtt);
5305 }
5306
5307 static struct rack_sendmap *
5308 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
5309 {
5310         /*
5311          * Check to see that we don't need to fall into recovery. We will
5312          * need to do so if our oldest transmit is past the time we should
5313          * have had an ack.
5314          */
5315         struct tcp_rack *rack;
5316         struct rack_sendmap *rsm;
5317         int32_t idx;
5318         uint32_t srtt, thresh;
5319
5320         rack = (struct tcp_rack *)tp->t_fb_ptr;
5321         if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
5322                 return (NULL);
5323         }
5324         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5325         if (rsm == NULL)
5326                 return (NULL);
5327
5328         if (rsm->r_flags & RACK_ACKED) {
5329                 rsm = rack_find_lowest_rsm(rack);
5330                 if (rsm == NULL)
5331                         return (NULL);
5332         }
5333         idx = rsm->r_rtr_cnt - 1;
5334         srtt = rack_grab_rtt(tp, rack);
5335         thresh = rack_calc_thresh_rack(rack, srtt, tsused);
5336         if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
5337                 return (NULL);
5338         }
5339         if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
5340                 return (NULL);
5341         }
5342         /* Ok if we reach here we are over-due and this guy can be sent */
5343         if (IN_RECOVERY(tp->t_flags) == 0) {
5344                 /*
5345                  * For the one that enters us into recovery record undo
5346                  * info.
5347                  */
5348                 rack->r_ctl.rc_rsm_start = rsm->r_start;
5349                 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
5350                 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
5351         }
5352         rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
5353         return (rsm);
5354 }
5355
5356 static uint32_t
5357 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
5358 {
5359         int32_t t;
5360         int32_t tt;
5361         uint32_t ret_val;
5362
5363         t = (tp->t_srtt + (tp->t_rttvar << 2));
5364         RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
5365             rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
5366         if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
5367                 tp->t_rxtshift++;
5368         rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
5369         ret_val = (uint32_t)tt;
5370         return (ret_val);
5371 }
5372
5373 static uint32_t
5374 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
5375 {
5376         /*
5377          * Start the FR timer, we do this based on getting the first one in
5378          * the rc_tmap. Note that if its NULL we must stop the timer. in all
5379          * events we need to stop the running timer (if its running) before
5380          * starting the new one.
5381          */
5382         uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
5383         uint32_t srtt_cur;
5384         int32_t idx;
5385         int32_t is_tlp_timer = 0;
5386         struct rack_sendmap *rsm;
5387
5388         if (rack->t_timers_stopped) {
5389                 /* All timers have been stopped none are to run */
5390                 return (0);
5391         }
5392         if (rack->rc_in_persist) {
5393                 /* We can't start any timer in persists */
5394                 return (rack_get_persists_timer_val(tp, rack));
5395         }
5396         rack->rc_on_min_to = 0;
5397         if ((tp->t_state < TCPS_ESTABLISHED) ||
5398             ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
5399                 goto activate_rxt;
5400         }
5401         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5402         if ((rsm == NULL) || sup_rack) {
5403                 /* Nothing on the send map or no rack */
5404 activate_rxt:
5405                 time_since_sent = 0;
5406                 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5407                 if (rsm) {
5408                         /*
5409                          * Should we discount the RTX timer any?
5410                          *
5411                          * We want to discount it the smallest amount.
5412                          * If a timer (Rack/TLP or RXT) has gone off more
5413                          * recently thats the discount we want to use (now - timer time).
5414                          * If the retransmit of the oldest packet was more recent then
5415                          * we want to use that (now - oldest-packet-last_transmit_time).
5416                          *
5417                          */
5418                         idx = rsm->r_rtr_cnt - 1;
5419                         if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
5420                                 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5421                         else
5422                                 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5423                         if (TSTMP_GT(cts, tstmp_touse))
5424                             time_since_sent = cts - tstmp_touse;
5425                 }
5426                 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
5427                         rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
5428                         to = tp->t_rxtcur;
5429                         if (to > time_since_sent)
5430                                 to -= time_since_sent;
5431                         else
5432                                 to = rack->r_ctl.rc_min_to;
5433                         if (to == 0)
5434                                 to = 1;
5435                         /* Special case for KEEPINIT */
5436                         if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
5437                             (TP_KEEPINIT(tp) != 0) &&
5438                             rsm) {
5439                                 /*
5440                                  * We have to put a ceiling on the rxt timer
5441                                  * of the keep-init timeout.
5442                                  */
5443                                 uint32_t max_time, red;
5444
5445                                 max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
5446                                 if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
5447                                         red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
5448                                         if (red < max_time)
5449                                                 max_time -= red;
5450                                         else
5451                                                 max_time = 1;
5452                                 }
5453                                 /* Reduce timeout to the keep value if needed */
5454                                 if (max_time < to)
5455                                         to = max_time;
5456                         }
5457                         return (to);
5458                 }
5459                 return (0);
5460         }
5461         if (rsm->r_flags & RACK_ACKED) {
5462                 rsm = rack_find_lowest_rsm(rack);
5463                 if (rsm == NULL) {
5464                         /* No lowest? */
5465                         goto activate_rxt;
5466                 }
5467         }
5468         if (rack->sack_attack_disable) {
5469                 /*
5470                  * We don't want to do
5471                  * any TLP's if you are an attacker.
5472                  * Though if you are doing what
5473                  * is expected you may still have
5474                  * SACK-PASSED marks.
5475                  */
5476                 goto activate_rxt;
5477         }
5478         /* Convert from ms to usecs */
5479         if ((rsm->r_flags & RACK_SACK_PASSED) || (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
5480                 if ((tp->t_flags & TF_SENTFIN) &&
5481                     ((tp->snd_max - tp->snd_una) == 1) &&
5482                     (rsm->r_flags & RACK_HAS_FIN)) {
5483                         /*
5484                          * We don't start a rack timer if all we have is a
5485                          * FIN outstanding.
5486                          */
5487                         goto activate_rxt;
5488                 }
5489                 if ((rack->use_rack_rr == 0) &&
5490                     (IN_FASTRECOVERY(tp->t_flags)) &&
5491                     (rack->rack_no_prr == 0) &&
5492                      (rack->r_ctl.rc_prr_sndcnt  < ctf_fixed_maxseg(tp))) {
5493                         /*
5494                          * We are not cheating, in recovery  and
5495                          * not enough ack's to yet get our next
5496                          * retransmission out.
5497                          *
5498                          * Note that classified attackers do not
5499                          * get to use the rack-cheat.
5500                          */
5501                         goto activate_tlp;
5502                 }
5503                 srtt = rack_grab_rtt(tp, rack);
5504                 thresh = rack_calc_thresh_rack(rack, srtt, cts);
5505                 idx = rsm->r_rtr_cnt - 1;
5506                 exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
5507                 if (SEQ_GEQ(exp, cts)) {
5508                         to = exp - cts;
5509                         if (to < rack->r_ctl.rc_min_to) {
5510                                 to = rack->r_ctl.rc_min_to;
5511                                 if (rack->r_rr_config == 3)
5512                                         rack->rc_on_min_to = 1;
5513                         }
5514                 } else {
5515                         to = rack->r_ctl.rc_min_to;
5516                         if (rack->r_rr_config == 3)
5517                                 rack->rc_on_min_to = 1;
5518                 }
5519         } else {
5520                 /* Ok we need to do a TLP not RACK */
5521 activate_tlp:
5522                 if ((rack->rc_tlp_in_progress != 0) &&
5523                     (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
5524                         /*
5525                          * The previous send was a TLP and we have sent
5526                          * N TLP's without sending new data.
5527                          */
5528                         goto activate_rxt;
5529                 }
5530                 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
5531                 if (rsm == NULL) {
5532                         /* We found no rsm to TLP with. */
5533                         goto activate_rxt;
5534                 }
5535                 if (rsm->r_flags & RACK_HAS_FIN) {
5536                         /* If its a FIN we dont do TLP */
5537                         rsm = NULL;
5538                         goto activate_rxt;
5539                 }
5540                 idx = rsm->r_rtr_cnt - 1;
5541                 time_since_sent = 0;
5542                 if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
5543                         tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5544                 else
5545                         tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
5546                 if (TSTMP_GT(cts, tstmp_touse))
5547                     time_since_sent = cts - tstmp_touse;
5548                 is_tlp_timer = 1;
5549                 if (tp->t_srtt) {
5550                         if ((rack->rc_srtt_measure_made == 0) &&
5551                             (tp->t_srtt == 1)) {
5552                                 /*
5553                                  * If another stack as run and set srtt to 1,
5554                                  * then the srtt was 0, so lets use the initial.
5555                                  */
5556                                 srtt = RACK_INITIAL_RTO;
5557                         } else {
5558                                 srtt_cur = tp->t_srtt;
5559                                 srtt = srtt_cur;
5560                         }
5561                 } else
5562                         srtt = RACK_INITIAL_RTO;
5563                 /*
5564                  * If the SRTT is not keeping up and the
5565                  * rack RTT has spiked we want to use
5566                  * the last RTT not the smoothed one.
5567                  */
5568                 if (rack_tlp_use_greater &&
5569                     tp->t_srtt &&
5570                     (srtt < rack_grab_rtt(tp, rack))) {
5571                         srtt = rack_grab_rtt(tp, rack);
5572                 }
5573                 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
5574                 if (thresh > time_since_sent) {
5575                         to = thresh - time_since_sent;
5576                 } else {
5577                         to = rack->r_ctl.rc_min_to;
5578                         rack_log_alt_to_to_cancel(rack,
5579                                                   thresh,               /* flex1 */
5580                                                   time_since_sent,      /* flex2 */
5581                                                   tstmp_touse,          /* flex3 */
5582                                                   rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
5583                                                   (uint32_t)rsm->r_tim_lastsent[idx],
5584                                                   srtt,
5585                                                   idx, 99);
5586                 }
5587                 if (to < rack_tlp_min) {
5588                         to = rack_tlp_min;
5589                 }
5590                 if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
5591                         /*
5592                          * If the TLP time works out to larger than the max
5593                          * RTO lets not do TLP.. just RTO.
5594                          */
5595                         goto activate_rxt;
5596                 }
5597         }
5598         if (is_tlp_timer == 0) {
5599                 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
5600         } else {
5601                 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
5602         }
5603         if (to == 0)
5604                 to = 1;
5605         return (to);
5606 }
5607
5608 static void
5609 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5610 {
5611         if (rack->rc_in_persist == 0) {
5612                 if (tp->t_flags & TF_GPUTINPROG) {
5613                         /*
5614                          * Stop the goodput now, the calling of the
5615                          * measurement function clears the flag.
5616                          */
5617                         rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
5618                                                     RACK_QUALITY_PERSIST);
5619                 }
5620 #ifdef NETFLIX_SHARED_CWND
5621                 if (rack->r_ctl.rc_scw) {
5622                         tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5623                         rack->rack_scwnd_is_idle = 1;
5624                 }
5625 #endif
5626                 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
5627                 if (rack->r_ctl.rc_went_idle_time == 0)
5628                         rack->r_ctl.rc_went_idle_time = 1;
5629                 rack_timer_cancel(tp, rack, cts, __LINE__);
5630                 tp->t_rxtshift = 0;
5631                 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5632                               rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5633                 rack->rc_in_persist = 1;
5634         }
5635 }
5636
5637 static void
5638 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5639 {
5640         if (rack->rc_inp->inp_in_hpts) {
5641                 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
5642                 rack->r_ctl.rc_hpts_flags = 0;
5643         }
5644 #ifdef NETFLIX_SHARED_CWND
5645         if (rack->r_ctl.rc_scw) {
5646                 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
5647                 rack->rack_scwnd_is_idle = 0;
5648         }
5649 #endif
5650         if (rack->rc_gp_dyn_mul &&
5651             (rack->use_fixed_rate == 0) &&
5652             (rack->rc_always_pace)) {
5653                 /*
5654                  * Do we count this as if a probe-rtt just
5655                  * finished?
5656                  */
5657                 uint32_t time_idle, idle_min;
5658
5659                 time_idle = tcp_get_usecs(NULL) - rack->r_ctl.rc_went_idle_time;
5660                 idle_min = rack_min_probertt_hold;
5661                 if (rack_probertt_gpsrtt_cnt_div) {
5662                         uint64_t extra;
5663                         extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
5664                                 (uint64_t)rack_probertt_gpsrtt_cnt_mul;
5665                         extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
5666                         idle_min += (uint32_t)extra;
5667                 }
5668                 if (time_idle >= idle_min) {
5669                         /* Yes, we count it as a probe-rtt. */
5670                         uint32_t us_cts;
5671
5672                         us_cts = tcp_get_usecs(NULL);
5673                         if (rack->in_probe_rtt == 0) {
5674                                 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
5675                                 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
5676                                 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
5677                                 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
5678                         } else {
5679                                 rack_exit_probertt(rack, us_cts);
5680                         }
5681                 }
5682         }
5683         rack->rc_in_persist = 0;
5684         rack->r_ctl.rc_went_idle_time = 0;
5685         tp->t_rxtshift = 0;
5686         RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
5687            rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
5688         rack->r_ctl.rc_agg_delayed = 0;
5689         rack->r_early = 0;
5690         rack->r_late = 0;
5691         rack->r_ctl.rc_agg_early = 0;
5692 }
5693
5694 static void
5695 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
5696                    struct hpts_diag *diag, struct timeval *tv)
5697 {
5698         if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5699                 union tcp_log_stackspecific log;
5700
5701                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5702                 log.u_bbr.flex1 = diag->p_nxt_slot;
5703                 log.u_bbr.flex2 = diag->p_cur_slot;
5704                 log.u_bbr.flex3 = diag->slot_req;
5705                 log.u_bbr.flex4 = diag->inp_hptsslot;
5706                 log.u_bbr.flex5 = diag->slot_remaining;
5707                 log.u_bbr.flex6 = diag->need_new_to;
5708                 log.u_bbr.flex7 = diag->p_hpts_active;
5709                 log.u_bbr.flex8 = diag->p_on_min_sleep;
5710                 /* Hijack other fields as needed */
5711                 log.u_bbr.epoch = diag->have_slept;
5712                 log.u_bbr.lt_epoch = diag->yet_to_sleep;
5713                 log.u_bbr.pkts_out = diag->co_ret;
5714                 log.u_bbr.applimited = diag->hpts_sleep_time;
5715                 log.u_bbr.delivered = diag->p_prev_slot;
5716                 log.u_bbr.inflight = diag->p_runningslot;
5717                 log.u_bbr.bw_inuse = diag->wheel_slot;
5718                 log.u_bbr.rttProp = diag->wheel_cts;
5719                 log.u_bbr.timeStamp = cts;
5720                 log.u_bbr.delRate = diag->maxslots;
5721                 log.u_bbr.cur_del_rate = diag->p_curtick;
5722                 log.u_bbr.cur_del_rate <<= 32;
5723                 log.u_bbr.cur_del_rate |= diag->p_lasttick;
5724                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
5725                     &rack->rc_inp->inp_socket->so_rcv,
5726                     &rack->rc_inp->inp_socket->so_snd,
5727                     BBR_LOG_HPTSDIAG, 0,
5728                     0, &log, false, tv);
5729         }
5730
5731 }
5732
5733 static void
5734 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
5735 {
5736         if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5737                 union tcp_log_stackspecific log;
5738                 struct timeval tv;
5739
5740                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5741                 log.u_bbr.flex1 = sb->sb_flags;
5742                 log.u_bbr.flex2 = len;
5743                 log.u_bbr.flex3 = sb->sb_state;
5744                 log.u_bbr.flex8 = type;
5745                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5746                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
5747                     &rack->rc_inp->inp_socket->so_rcv,
5748                     &rack->rc_inp->inp_socket->so_snd,
5749                     TCP_LOG_SB_WAKE, 0,
5750                     len, &log, false, &tv);
5751         }
5752 }
5753
5754 static void
5755 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
5756       int32_t slot, uint32_t tot_len_this_send, int sup_rack)
5757 {
5758         struct hpts_diag diag;
5759         struct inpcb *inp;
5760         struct timeval tv;
5761         uint32_t delayed_ack = 0;
5762         uint32_t hpts_timeout;
5763         uint32_t entry_slot = slot;
5764         uint8_t stopped;
5765         uint32_t left = 0;
5766         uint32_t us_cts;
5767
5768         inp = tp->t_inpcb;
5769         if ((tp->t_state == TCPS_CLOSED) ||
5770             (tp->t_state == TCPS_LISTEN)) {
5771                 return;
5772         }
5773         if (inp->inp_in_hpts) {
5774                 /* Already on the pacer */
5775                 return;
5776         }
5777         stopped = rack->rc_tmr_stopped;
5778         if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
5779                 left = rack->r_ctl.rc_timer_exp - cts;
5780         }
5781         rack->r_ctl.rc_timer_exp = 0;
5782         rack->r_ctl.rc_hpts_flags = 0;
5783         us_cts = tcp_get_usecs(&tv);
5784         /* Now early/late accounting */
5785         rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
5786         if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
5787                 /*
5788                  * We have a early carry over set,
5789                  * we can always add more time so we
5790                  * can always make this compensation.
5791                  *
5792                  * Note if ack's are allowed to wake us do not
5793                  * penalize the next timer for being awoke
5794                  * by an ack aka the rc_agg_early (non-paced mode).
5795                  */
5796                 slot += rack->r_ctl.rc_agg_early;
5797                 rack->r_early = 0;
5798                 rack->r_ctl.rc_agg_early = 0;
5799         }
5800         if (rack->r_late) {
5801                 /*
5802                  * This is harder, we can
5803                  * compensate some but it
5804                  * really depends on what
5805                  * the current pacing time is.
5806                  */
5807                 if (rack->r_ctl.rc_agg_delayed >= slot) {
5808                         /*
5809                          * We can't compensate for it all.
5810                          * And we have to have some time
5811                          * on the clock. We always have a min
5812                          * 10 slots (10 x 10 i.e. 100 usecs).
5813                          */
5814                         if (slot <= HPTS_TICKS_PER_SLOT) {
5815                                 /* We gain delay */
5816                                 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
5817                                 slot = HPTS_TICKS_PER_SLOT;
5818                         } else {
5819                                 /* We take off some */
5820                                 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
5821                                 slot = HPTS_TICKS_PER_SLOT;
5822                         }
5823                 } else {
5824                         slot -= rack->r_ctl.rc_agg_delayed;
5825                         rack->r_ctl.rc_agg_delayed = 0;
5826                         /* Make sure we have 100 useconds at minimum */
5827                         if (slot < HPTS_TICKS_PER_SLOT) {
5828                                 rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
5829                                 slot = HPTS_TICKS_PER_SLOT;
5830                         }
5831                         if (rack->r_ctl.rc_agg_delayed == 0)
5832                                 rack->r_late = 0;
5833                 }
5834         }
5835         if (slot) {
5836                 /* We are pacing too */
5837                 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
5838         }
5839         hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
5840 #ifdef NETFLIX_EXP_DETECTION
5841         if (rack->sack_attack_disable &&
5842             (slot < tcp_sad_pacing_interval)) {
5843                 /*
5844                  * We have a potential attacker on
5845                  * the line. We have possibly some
5846                  * (or now) pacing time set. We want to
5847                  * slow down the processing of sacks by some
5848                  * amount (if it is an attacker). Set the default
5849                  * slot for attackers in place (unless the orginal
5850                  * interval is longer). Its stored in
5851                  * micro-seconds, so lets convert to msecs.
5852                  */
5853                 slot = tcp_sad_pacing_interval;
5854         }
5855 #endif
5856         if (tp->t_flags & TF_DELACK) {
5857                 delayed_ack = TICKS_2_USEC(tcp_delacktime);
5858                 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
5859         }
5860         if (delayed_ack && ((hpts_timeout == 0) ||
5861                             (delayed_ack < hpts_timeout)))
5862                 hpts_timeout = delayed_ack;
5863         else
5864                 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
5865         /*
5866          * If no timers are going to run and we will fall off the hptsi
5867          * wheel, we resort to a keep-alive timer if its configured.
5868          */
5869         if ((hpts_timeout == 0) &&
5870             (slot == 0)) {
5871                 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
5872                     (tp->t_state <= TCPS_CLOSING)) {
5873                         /*
5874                          * Ok we have no timer (persists, rack, tlp, rxt  or
5875                          * del-ack), we don't have segments being paced. So
5876                          * all that is left is the keepalive timer.
5877                          */
5878                         if (TCPS_HAVEESTABLISHED(tp->t_state)) {
5879                                 /* Get the established keep-alive time */
5880                                 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
5881                         } else {
5882                                 /*
5883                                  * Get the initial setup keep-alive time,
5884                                  * note that this is probably not going to
5885                                  * happen, since rack will be running a rxt timer
5886                                  * if a SYN of some sort is outstanding. It is
5887                                  * actually handled in rack_timeout_rxt().
5888                                  */
5889                                 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
5890                         }
5891                         rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
5892                         if (rack->in_probe_rtt) {
5893                                 /*
5894                                  * We want to instead not wake up a long time from
5895                                  * now but to wake up about the time we would
5896                                  * exit probe-rtt and initiate a keep-alive ack.
5897                                  * This will get us out of probe-rtt and update
5898                                  * our min-rtt.
5899                                  */
5900                                 hpts_timeout = rack_min_probertt_hold;
5901                         }
5902                 }
5903         }
5904         if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
5905             (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
5906                 /*
5907                  * RACK, TLP, persists and RXT timers all are restartable
5908                  * based on actions input .. i.e we received a packet (ack
5909                  * or sack) and that changes things (rw, or snd_una etc).
5910                  * Thus we can restart them with a new value. For
5911                  * keep-alive, delayed_ack we keep track of what was left
5912                  * and restart the timer with a smaller value.
5913                  */
5914                 if (left < hpts_timeout)
5915                         hpts_timeout = left;
5916         }
5917         if (hpts_timeout) {
5918                 /*
5919                  * Hack alert for now we can't time-out over 2,147,483
5920                  * seconds (a bit more than 596 hours), which is probably ok
5921                  * :).
5922                  */
5923                 if (hpts_timeout > 0x7ffffffe)
5924                         hpts_timeout = 0x7ffffffe;
5925                 rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
5926         }
5927         rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
5928         if ((rack->gp_ready == 0) &&
5929             (rack->use_fixed_rate == 0) &&
5930             (hpts_timeout < slot) &&
5931             (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
5932                 /*
5933                  * We have no good estimate yet for the
5934                  * old clunky burst mitigation or the
5935                  * real pacing. And the tlp or rxt is smaller
5936                  * than the pacing calculation. Lets not
5937                  * pace that long since we know the calculation
5938                  * so far is not accurate.
5939                  */
5940                 slot = hpts_timeout;
5941         }
5942         rack->r_ctl.last_pacing_time = slot;
5943         /**
5944          * Turn off all the flags for queuing by default. The
5945          * flags have important meanings to what happens when
5946          * LRO interacts with the transport. Most likely (by default now)
5947          * mbuf_queueing and ack compression are on. So the transport
5948          * has a couple of flags that control what happens (if those
5949          * are not on then these flags won't have any effect since it
5950          * won't go through the queuing LRO path).
5951          *
5952          * INP_MBUF_QUEUE_READY - This flags says that I am busy
5953          *                        pacing output, so don't disturb. But
5954          *                        it also means LRO can wake me if there
5955          *                        is a SACK arrival.
5956          *
5957          * INP_DONT_SACK_QUEUE - This flag is used in conjunction
5958          *                       with the above flag (QUEUE_READY) and
5959          *                       when present it says don't even wake me
5960          *                       if a SACK arrives.
5961          *
5962          * The idea behind these flags is that if we are pacing we
5963          * set the MBUF_QUEUE_READY and only get woken up if
5964          * a SACK arrives (which could change things) or if
5965          * our pacing timer expires. If, however, we have a rack
5966          * timer running, then we don't even want a sack to wake
5967          * us since the rack timer has to expire before we can send.
5968          *
5969          * Other cases should usually have none of the flags set
5970          * so LRO can call into us.
5971          */
5972         inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
5973         if (slot) {
5974                 rack->r_ctl.rc_last_output_to = us_cts + slot;
5975                 /*
5976                  * A pacing timer (slot) is being set, in
5977                  * such a case we cannot send (we are blocked by
5978                  * the timer). So lets tell LRO that it should not
5979                  * wake us unless there is a SACK. Note this only
5980                  * will be effective if mbuf queueing is on or
5981                  * compressed acks are being processed.
5982                  */
5983                 inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
5984                 /*
5985                  * But wait if we have a Rack timer running
5986                  * even a SACK should not disturb us (with
5987                  * the exception of r_rr_config 3).
5988                  */
5989                 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
5990                     (rack->r_rr_config != 3))
5991                         inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
5992                 if (rack->rc_ack_can_sendout_data) {
5993                         /*
5994                          * Ahh but wait, this is that special case
5995                          * where the pacing timer can be disturbed
5996                          * backout the changes (used for non-paced
5997                          * burst limiting).
5998                          */
5999                         inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
6000                 }
6001                 if ((rack->use_rack_rr) &&
6002                     (rack->r_rr_config < 2) &&
6003                     ((hpts_timeout) && (hpts_timeout < slot))) {
6004                         /*
6005                          * Arrange for the hpts to kick back in after the
6006                          * t-o if the t-o does not cause a send.
6007                          */
6008                         (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6009                                                    __LINE__, &diag);
6010                         rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6011                         rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6012                 } else {
6013                         (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
6014                                                    __LINE__, &diag);
6015                         rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6016                         rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6017                 }
6018         } else if (hpts_timeout) {
6019                 /*
6020                  * With respect to inp_flags2 here, lets let any new acks wake
6021                  * us up here. Since we are not pacing (no pacing timer), output
6022                  * can happen so we should let it. If its a Rack timer, then any inbound
6023                  * packet probably won't change the sending (we will be blocked)
6024                  * but it may change the prr stats so letting it in (the set defaults
6025                  * at the start of this block) are good enough.
6026                  */
6027                 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6028                                            __LINE__, &diag);
6029                 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6030                 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6031         } else {
6032                 /* No timer starting */
6033 #ifdef INVARIANTS
6034                 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
6035                         panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
6036                             tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
6037                 }
6038 #endif
6039         }
6040         rack->rc_tmr_stopped = 0;
6041         if (slot)
6042                 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv);
6043 }
6044
6045 /*
6046  * RACK Timer, here we simply do logging and house keeping.
6047  * the normal rack_output() function will call the
6048  * appropriate thing to check if we need to do a RACK retransmit.
6049  * We return 1, saying don't proceed with rack_output only
6050  * when all timers have been stopped (destroyed PCB?).
6051  */
6052 static int
6053 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6054 {
6055         /*
6056          * This timer simply provides an internal trigger to send out data.
6057          * The check_recovery_mode call will see if there are needed
6058          * retransmissions, if so we will enter fast-recovery. The output
6059          * call may or may not do the same thing depending on sysctl
6060          * settings.
6061          */
6062         struct rack_sendmap *rsm;
6063
6064         if (tp->t_timers->tt_flags & TT_STOPPED) {
6065                 return (1);
6066         }
6067         counter_u64_add(rack_to_tot, 1);
6068         if (rack->r_state && (rack->r_state != tp->t_state))
6069                 rack_set_state(tp, rack);
6070         rack->rc_on_min_to = 0;
6071         rsm = rack_check_recovery_mode(tp, cts);
6072         rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
6073         if (rsm) {
6074                 rack->r_ctl.rc_resend = rsm;
6075                 rack->r_timer_override = 1;
6076                 if (rack->use_rack_rr) {
6077                         /*
6078                          * Don't accumulate extra pacing delay
6079                          * we are allowing the rack timer to
6080                          * over-ride pacing i.e. rrr takes precedence
6081                          * if the pacing interval is longer than the rrr
6082                          * time (in other words we get the min pacing
6083                          * time versus rrr pacing time).
6084                          */
6085                         rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6086                 }
6087         }
6088         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6089         if (rsm == NULL) {
6090                 /* restart a timer and return 1 */
6091                 rack_start_hpts_timer(rack, tp, cts,
6092                                       0, 0, 0);
6093                 return (1);
6094         }
6095         return (0);
6096 }
6097
6098 static void
6099 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6100 {
6101         if (rsm->m->m_len > rsm->orig_m_len) {
6102                 /*
6103                  * Mbuf grew, caused by sbcompress, our offset does
6104                  * not change.
6105                  */
6106                 rsm->orig_m_len = rsm->m->m_len;
6107         } else if (rsm->m->m_len < rsm->orig_m_len) {
6108                 /*
6109                  * Mbuf shrank, trimmed off the top by an ack, our
6110                  * offset changes.
6111                  */
6112                 rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
6113                 rsm->orig_m_len = rsm->m->m_len;
6114         }
6115 }
6116
6117 static void
6118 rack_setup_offset_for_rsm(struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
6119 {
6120         struct mbuf *m;
6121         uint32_t soff;
6122
6123         if (src_rsm->m && (src_rsm->orig_m_len != src_rsm->m->m_len)) {
6124                 /* Fix up the orig_m_len and possibly the mbuf offset */
6125                 rack_adjust_orig_mlen(src_rsm);
6126         }
6127         m = src_rsm->m;
6128         soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
6129         while (soff >= m->m_len) {
6130                 /* Move out past this mbuf */
6131                 soff -= m->m_len;
6132                 m = m->m_next;
6133                 KASSERT((m != NULL),
6134                         ("rsm:%p nrsm:%p hit at soff:%u null m",
6135                          src_rsm, rsm, soff));
6136         }
6137         rsm->m = m;
6138         rsm->soff = soff;
6139         rsm->orig_m_len = m->m_len;
6140 }
6141
6142 static __inline void
6143 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
6144                struct rack_sendmap *rsm, uint32_t start)
6145 {
6146         int idx;
6147
6148         nrsm->r_start = start;
6149         nrsm->r_end = rsm->r_end;
6150         nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
6151         nrsm->r_flags = rsm->r_flags;
6152         nrsm->r_dupack = rsm->r_dupack;
6153         nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
6154         nrsm->r_rtr_bytes = 0;
6155         nrsm->r_fas = rsm->r_fas;
6156         rsm->r_end = nrsm->r_start;
6157         nrsm->r_just_ret = rsm->r_just_ret;
6158         for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
6159                 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
6160         }
6161         /* Now if we have SYN flag we keep it on the left edge */
6162         if (nrsm->r_flags & RACK_HAS_SYN)
6163                 nrsm->r_flags &= ~RACK_HAS_SYN;
6164         /* Now if we have a FIN flag we keep it on the right edge */
6165         if (rsm->r_flags & RACK_HAS_FIN)
6166                 rsm->r_flags &= ~RACK_HAS_FIN;
6167         /* Push bit must go to the right edge as well */
6168         if (rsm->r_flags & RACK_HAD_PUSH)
6169                 rsm->r_flags &= ~RACK_HAD_PUSH;
6170         /* Clone over the state of the hw_tls flag */
6171         nrsm->r_hw_tls = rsm->r_hw_tls;
6172         /*
6173          * Now we need to find nrsm's new location in the mbuf chain
6174          * we basically calculate a new offset, which is soff +
6175          * how much is left in original rsm. Then we walk out the mbuf
6176          * chain to find the righ postion, it may be the same mbuf
6177          * or maybe not.
6178          */
6179         KASSERT(((rsm->m != NULL) ||
6180                  (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
6181                 ("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
6182         if (rsm->m)
6183                 rack_setup_offset_for_rsm(rsm, nrsm);
6184 }
6185
6186 static struct rack_sendmap *
6187 rack_merge_rsm(struct tcp_rack *rack,
6188                struct rack_sendmap *l_rsm,
6189                struct rack_sendmap *r_rsm)
6190 {
6191         /*
6192          * We are merging two ack'd RSM's,
6193          * the l_rsm is on the left (lower seq
6194          * values) and the r_rsm is on the right
6195          * (higher seq value). The simplest way
6196          * to merge these is to move the right
6197          * one into the left. I don't think there
6198          * is any reason we need to try to find
6199          * the oldest (or last oldest retransmitted).
6200          */
6201         struct rack_sendmap *rm;
6202
6203         rack_log_map_chg(rack->rc_tp, rack, NULL,
6204                          l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
6205         l_rsm->r_end = r_rsm->r_end;
6206         if (l_rsm->r_dupack < r_rsm->r_dupack)
6207                 l_rsm->r_dupack = r_rsm->r_dupack;
6208         if (r_rsm->r_rtr_bytes)
6209                 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
6210         if (r_rsm->r_in_tmap) {
6211                 /* This really should not happen */
6212                 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
6213                 r_rsm->r_in_tmap = 0;
6214         }
6215
6216         /* Now the flags */
6217         if (r_rsm->r_flags & RACK_HAS_FIN)
6218                 l_rsm->r_flags |= RACK_HAS_FIN;
6219         if (r_rsm->r_flags & RACK_TLP)
6220                 l_rsm->r_flags |= RACK_TLP;
6221         if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
6222                 l_rsm->r_flags |= RACK_RWND_COLLAPSED;
6223         if ((r_rsm->r_flags & RACK_APP_LIMITED)  &&
6224             ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
6225                 /*
6226                  * If both are app-limited then let the
6227                  * free lower the count. If right is app
6228                  * limited and left is not, transfer.
6229                  */
6230                 l_rsm->r_flags |= RACK_APP_LIMITED;
6231                 r_rsm->r_flags &= ~RACK_APP_LIMITED;
6232                 if (r_rsm == rack->r_ctl.rc_first_appl)
6233                         rack->r_ctl.rc_first_appl = l_rsm;
6234         }
6235         rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6236 #ifdef INVARIANTS
6237         if (rm != r_rsm) {
6238                 panic("removing head in rack:%p rsm:%p rm:%p",
6239                       rack, r_rsm, rm);
6240         }
6241 #endif
6242         if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
6243                 /* Transfer the split limit to the map we free */
6244                 r_rsm->r_limit_type = l_rsm->r_limit_type;
6245                 l_rsm->r_limit_type = 0;
6246         }
6247         rack_free(rack, r_rsm);
6248         return (l_rsm);
6249 }
6250
6251 /*
6252  * TLP Timer, here we simply setup what segment we want to
6253  * have the TLP expire on, the normal rack_output() will then
6254  * send it out.
6255  *
6256  * We return 1, saying don't proceed with rack_output only
6257  * when all timers have been stopped (destroyed PCB?).
6258  */
6259 static int
6260 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
6261 {
6262         /*
6263          * Tail Loss Probe.
6264          */
6265         struct rack_sendmap *rsm = NULL;
6266         struct rack_sendmap *insret;
6267         struct socket *so;
6268         uint32_t amm;
6269         uint32_t out, avail;
6270         int collapsed_win = 0;
6271
6272         if (tp->t_timers->tt_flags & TT_STOPPED) {
6273                 return (1);
6274         }
6275         if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
6276                 /* Its not time yet */
6277                 return (0);
6278         }
6279         if (ctf_progress_timeout_check(tp, true)) {
6280                 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6281                 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
6282                 return (1);
6283         }
6284         /*
6285          * A TLP timer has expired. We have been idle for 2 rtts. So we now
6286          * need to figure out how to force a full MSS segment out.
6287          */
6288         rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
6289         rack->r_ctl.retran_during_recovery = 0;
6290         rack->r_ctl.dsack_byte_cnt = 0;
6291         counter_u64_add(rack_tlp_tot, 1);
6292         if (rack->r_state && (rack->r_state != tp->t_state))
6293                 rack_set_state(tp, rack);
6294         so = tp->t_inpcb->inp_socket;
6295         avail = sbavail(&so->so_snd);
6296         out = tp->snd_max - tp->snd_una;
6297         if (out > tp->snd_wnd) {
6298                 /* special case, we need a retransmission */
6299                 collapsed_win = 1;
6300                 goto need_retran;
6301         }
6302         if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
6303                 rack->r_ctl.dsack_persist--;
6304                 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6305                         rack->r_ctl.num_dsack = 0;
6306                 }
6307                 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6308         }
6309         if ((tp->t_flags & TF_GPUTINPROG) &&
6310             (rack->r_ctl.rc_tlp_cnt_out == 1)) {
6311                 /*
6312                  * If this is the second in a row
6313                  * TLP and we are doing a measurement
6314                  * its time to abandon the measurement.
6315                  * Something is likely broken on
6316                  * the clients network and measuring a
6317                  * broken network does us no good.
6318                  */
6319                 tp->t_flags &= ~TF_GPUTINPROG;
6320                 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6321                                            rack->r_ctl.rc_gp_srtt /*flex1*/,
6322                                            tp->gput_seq,
6323                                            0, 0, 18, __LINE__, NULL, 0);
6324         }
6325         /*
6326          * Check our send oldest always settings, and if
6327          * there is an oldest to send jump to the need_retran.
6328          */
6329         if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
6330                 goto need_retran;
6331
6332         if (avail > out) {
6333                 /* New data is available */
6334                 amm = avail - out;
6335                 if (amm > ctf_fixed_maxseg(tp)) {
6336                         amm = ctf_fixed_maxseg(tp);
6337                         if ((amm + out) > tp->snd_wnd) {
6338                                 /* We are rwnd limited */
6339                                 goto need_retran;
6340                         }
6341                 } else if (amm < ctf_fixed_maxseg(tp)) {
6342                         /* not enough to fill a MTU */
6343                         goto need_retran;
6344                 }
6345                 if (IN_FASTRECOVERY(tp->t_flags)) {
6346                         /* Unlikely */
6347                         if (rack->rack_no_prr == 0) {
6348                                 if (out + amm <= tp->snd_wnd) {
6349                                         rack->r_ctl.rc_prr_sndcnt = amm;
6350                                         rack->r_ctl.rc_tlp_new_data = amm;
6351                                         rack_log_to_prr(rack, 4, 0);
6352                                 }
6353                         } else
6354                                 goto need_retran;
6355                 } else {
6356                         /* Set the send-new override */
6357                         if (out + amm <= tp->snd_wnd)
6358                                 rack->r_ctl.rc_tlp_new_data = amm;
6359                         else
6360                                 goto need_retran;
6361                 }
6362                 rack->r_ctl.rc_tlpsend = NULL;
6363                 counter_u64_add(rack_tlp_newdata, 1);
6364                 goto send;
6365         }
6366 need_retran:
6367         /*
6368          * Ok we need to arrange the last un-acked segment to be re-sent, or
6369          * optionally the first un-acked segment.
6370          */
6371         if (collapsed_win == 0) {
6372                 if (rack_always_send_oldest)
6373                         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6374                 else {
6375                         rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6376                         if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
6377                                 rsm = rack_find_high_nonack(rack, rsm);
6378                         }
6379                 }
6380                 if (rsm == NULL) {
6381                         counter_u64_add(rack_tlp_does_nada, 1);
6382 #ifdef TCP_BLACKBOX
6383                         tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6384 #endif
6385                         goto out;
6386                 }
6387         } else {
6388                 /*
6389                  * We must find the last segment
6390                  * that was acceptable by the client.
6391                  */
6392                 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6393                         if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) {
6394                                 /* Found one */
6395                                 break;
6396                         }
6397                 }
6398                 if (rsm == NULL) {
6399                         /* None? if so send the first */
6400                         rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6401                         if (rsm == NULL) {
6402                                 counter_u64_add(rack_tlp_does_nada, 1);
6403 #ifdef TCP_BLACKBOX
6404                                 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6405 #endif
6406                                 goto out;
6407                         }
6408                 }
6409         }
6410         if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
6411                 /*
6412                  * We need to split this the last segment in two.
6413                  */
6414                 struct rack_sendmap *nrsm;
6415
6416                 nrsm = rack_alloc_full_limit(rack);
6417                 if (nrsm == NULL) {
6418                         /*
6419                          * No memory to split, we will just exit and punt
6420                          * off to the RXT timer.
6421                          */
6422                         counter_u64_add(rack_tlp_does_nada, 1);
6423                         goto out;
6424                 }
6425                 rack_clone_rsm(rack, nrsm, rsm,
6426                                (rsm->r_end - ctf_fixed_maxseg(tp)));
6427                 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
6428                 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6429 #ifdef INVARIANTS
6430                 if (insret != NULL) {
6431                         panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6432                               nrsm, insret, rack, rsm);
6433                 }
6434 #endif
6435                 if (rsm->r_in_tmap) {
6436                         TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6437                         nrsm->r_in_tmap = 1;
6438                 }
6439                 rsm = nrsm;
6440         }
6441         rack->r_ctl.rc_tlpsend = rsm;
6442 send:
6443         /* Make sure output path knows we are doing a TLP */
6444         *doing_tlp = 1;
6445         rack->r_timer_override = 1;
6446         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6447         return (0);
6448 out:
6449         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6450         return (0);
6451 }
6452
6453 /*
6454  * Delayed ack Timer, here we simply need to setup the
6455  * ACK_NOW flag and remove the DELACK flag. From there
6456  * the output routine will send the ack out.
6457  *
6458  * We only return 1, saying don't proceed, if all timers
6459  * are stopped (destroyed PCB?).
6460  */
6461 static int
6462 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6463 {
6464         if (tp->t_timers->tt_flags & TT_STOPPED) {
6465                 return (1);
6466         }
6467         rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
6468         tp->t_flags &= ~TF_DELACK;
6469         tp->t_flags |= TF_ACKNOW;
6470         KMOD_TCPSTAT_INC(tcps_delack);
6471         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6472         return (0);
6473 }
6474
6475 /*
6476  * Persists timer, here we simply send the
6477  * same thing as a keepalive will.
6478  * the one byte send.
6479  *
6480  * We only return 1, saying don't proceed, if all timers
6481  * are stopped (destroyed PCB?).
6482  */
6483 static int
6484 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6485 {
6486         struct tcptemp *t_template;
6487         struct inpcb *inp;
6488         int32_t retval = 1;
6489
6490         inp = tp->t_inpcb;
6491
6492         if (tp->t_timers->tt_flags & TT_STOPPED) {
6493                 return (1);
6494         }
6495         if (rack->rc_in_persist == 0)
6496                 return (0);
6497         if (ctf_progress_timeout_check(tp, false)) {
6498                 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6499                 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6500                 tcp_set_inp_to_drop(inp, ETIMEDOUT);
6501                 return (1);
6502         }
6503         KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
6504         /*
6505          * Persistence timer into zero window. Force a byte to be output, if
6506          * possible.
6507          */
6508         KMOD_TCPSTAT_INC(tcps_persisttimeo);
6509         /*
6510          * Hack: if the peer is dead/unreachable, we do not time out if the
6511          * window is closed.  After a full backoff, drop the connection if
6512          * the idle time (no responses to probes) reaches the maximum
6513          * backoff that we would use if retransmitting.
6514          */
6515         if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
6516             (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
6517              TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
6518                 KMOD_TCPSTAT_INC(tcps_persistdrop);
6519                 retval = 1;
6520                 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6521                 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
6522                 goto out;
6523         }
6524         if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
6525             tp->snd_una == tp->snd_max)
6526                 rack_exit_persist(tp, rack, cts);
6527         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
6528         /*
6529          * If the user has closed the socket then drop a persisting
6530          * connection after a much reduced timeout.
6531          */
6532         if (tp->t_state > TCPS_CLOSE_WAIT &&
6533             (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
6534                 retval = 1;
6535                 KMOD_TCPSTAT_INC(tcps_persistdrop);
6536                 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6537                 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
6538                 goto out;
6539         }
6540         t_template = tcpip_maketemplate(rack->rc_inp);
6541         if (t_template) {
6542                 /* only set it if we were answered */
6543                 if (rack->forced_ack == 0) {
6544                         rack->forced_ack = 1;
6545                         rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6546                 }
6547                 tcp_respond(tp, t_template->tt_ipgen,
6548                             &t_template->tt_t, (struct mbuf *)NULL,
6549                             tp->rcv_nxt, tp->snd_una - 1, 0);
6550                 /* This sends an ack */
6551                 if (tp->t_flags & TF_DELACK)
6552                         tp->t_flags &= ~TF_DELACK;
6553                 free(t_template, M_TEMP);
6554         }
6555         if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
6556                 tp->t_rxtshift++;
6557 out:
6558         rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
6559         rack_start_hpts_timer(rack, tp, cts,
6560                               0, 0, 0);
6561         return (retval);
6562 }
6563
6564 /*
6565  * If a keepalive goes off, we had no other timers
6566  * happening. We always return 1 here since this
6567  * routine either drops the connection or sends
6568  * out a segment with respond.
6569  */
6570 static int
6571 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6572 {
6573         struct tcptemp *t_template;
6574         struct inpcb *inp;
6575
6576         if (tp->t_timers->tt_flags & TT_STOPPED) {
6577                 return (1);
6578         }
6579         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
6580         inp = tp->t_inpcb;
6581         rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
6582         /*
6583          * Keep-alive timer went off; send something or drop connection if
6584          * idle for too long.
6585          */
6586         KMOD_TCPSTAT_INC(tcps_keeptimeo);
6587         if (tp->t_state < TCPS_ESTABLISHED)
6588                 goto dropit;
6589         if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6590             tp->t_state <= TCPS_CLOSING) {
6591                 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
6592                         goto dropit;
6593                 /*
6594                  * Send a packet designed to force a response if the peer is
6595                  * up and reachable: either an ACK if the connection is
6596                  * still alive, or an RST if the peer has closed the
6597                  * connection due to timeout or reboot. Using sequence
6598                  * number tp->snd_una-1 causes the transmitted zero-length
6599                  * segment to lie outside the receive window; by the
6600                  * protocol spec, this requires the correspondent TCP to
6601                  * respond.
6602                  */
6603                 KMOD_TCPSTAT_INC(tcps_keepprobe);
6604                 t_template = tcpip_maketemplate(inp);
6605                 if (t_template) {
6606                         if (rack->forced_ack == 0) {
6607                                 rack->forced_ack = 1;
6608                                 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
6609                         }
6610                         tcp_respond(tp, t_template->tt_ipgen,
6611                             &t_template->tt_t, (struct mbuf *)NULL,
6612                             tp->rcv_nxt, tp->snd_una - 1, 0);
6613                         free(t_template, M_TEMP);
6614                 }
6615         }
6616         rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
6617         return (1);
6618 dropit:
6619         KMOD_TCPSTAT_INC(tcps_keepdrops);
6620         tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6621         tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
6622         return (1);
6623 }
6624
6625 /*
6626  * Retransmit helper function, clear up all the ack
6627  * flags and take care of important book keeping.
6628  */
6629 static void
6630 rack_remxt_tmr(struct tcpcb *tp)
6631 {
6632         /*
6633          * The retransmit timer went off, all sack'd blocks must be
6634          * un-acked.
6635          */
6636         struct rack_sendmap *rsm, *trsm = NULL;
6637         struct tcp_rack *rack;
6638
6639         rack = (struct tcp_rack *)tp->t_fb_ptr;
6640         rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
6641         rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
6642         if (rack->r_state && (rack->r_state != tp->t_state))
6643                 rack_set_state(tp, rack);
6644         /*
6645          * Ideally we would like to be able to
6646          * mark SACK-PASS on anything not acked here.
6647          *
6648          * However, if we do that we would burst out
6649          * all that data 1ms apart. This would be unwise,
6650          * so for now we will just let the normal rxt timer
6651          * and tlp timer take care of it.
6652          *
6653          * Also we really need to stick them back in sequence
6654          * order. This way we send in the proper order and any
6655          * sacks that come floating in will "re-ack" the data.
6656          * To do this we zap the tmap with an INIT and then
6657          * walk through and place every rsm in the RB tree
6658          * back in its seq ordered place.
6659          */
6660         TAILQ_INIT(&rack->r_ctl.rc_tmap);
6661         RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6662                 rsm->r_dupack = 0;
6663                 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
6664                 /* We must re-add it back to the tlist */
6665                 if (trsm == NULL) {
6666                         TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6667                 } else {
6668                         TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
6669                 }
6670                 rsm->r_in_tmap = 1;
6671                 trsm = rsm;
6672                 if (rsm->r_flags & RACK_ACKED)
6673                         rsm->r_flags |= RACK_WAS_ACKED;
6674                 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS);
6675         }
6676         /* Clear the count (we just un-acked them) */
6677         rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
6678         rack->r_ctl.rc_sacked = 0;
6679         rack->r_ctl.rc_sacklast = NULL;
6680         rack->r_ctl.rc_agg_delayed = 0;
6681         rack->r_early = 0;
6682         rack->r_ctl.rc_agg_early = 0;
6683         rack->r_late = 0;
6684         /* Clear the tlp rtx mark */
6685         rack->r_ctl.rc_resend = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6686         if (rack->r_ctl.rc_resend != NULL)
6687                 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
6688         rack->r_ctl.rc_prr_sndcnt = 0;
6689         rack_log_to_prr(rack, 6, 0);
6690         rack->r_timer_override = 1;
6691         if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
6692 #ifdef NETFLIX_EXP_DETECTION
6693             || (rack->sack_attack_disable != 0)
6694 #endif
6695                     ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
6696                 /*
6697                  * For non-sack customers new data
6698                  * needs to go out as retransmits until
6699                  * we retransmit up to snd_max.
6700                  */
6701                 rack->r_must_retran = 1;
6702                 rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
6703                                                 rack->r_ctl.rc_sacked);
6704         }
6705         rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
6706 }
6707
6708 static void
6709 rack_convert_rtts(struct tcpcb *tp)
6710 {
6711         if (tp->t_srtt > 1) {
6712                 uint32_t val, frac;
6713
6714                 val = tp->t_srtt >> TCP_RTT_SHIFT;
6715                 frac = tp->t_srtt & 0x1f;
6716                 tp->t_srtt = TICKS_2_USEC(val);
6717                 /*
6718                  * frac is the fractional part of the srtt (if any)
6719                  * but its in ticks and every bit represents
6720                  * 1/32nd of a hz.
6721                  */
6722                 if (frac) {
6723                         if (hz == 1000) {
6724                                 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6725                         } else {
6726                                 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6727                         }
6728                         tp->t_srtt += frac;
6729                 }
6730         }
6731         if (tp->t_rttvar) {
6732                 uint32_t val, frac;
6733
6734                 val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
6735                 frac = tp->t_rttvar & 0x1f;
6736                 tp->t_rttvar = TICKS_2_USEC(val);
6737                 /*
6738                  * frac is the fractional part of the srtt (if any)
6739                  * but its in ticks and every bit represents
6740                  * 1/32nd of a hz.
6741                  */
6742                 if (frac) {
6743                         if (hz == 1000) {
6744                                 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6745                         } else {
6746                                 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6747                         }
6748                         tp->t_rttvar += frac;
6749                 }
6750         }
6751         tp->t_rxtcur = RACK_REXMTVAL(tp);
6752         if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6753                 tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
6754         }
6755         if (tp->t_rxtcur > rack_rto_max) {
6756                 tp->t_rxtcur = rack_rto_max;
6757         }
6758 }
6759
6760 static void
6761 rack_cc_conn_init(struct tcpcb *tp)
6762 {
6763         struct tcp_rack *rack;
6764         uint32_t srtt;
6765
6766         rack = (struct tcp_rack *)tp->t_fb_ptr;
6767         srtt = tp->t_srtt;
6768         cc_conn_init(tp);
6769         /*
6770          * Now convert to rack's internal format,
6771          * if required.
6772          */
6773         if ((srtt == 0) && (tp->t_srtt != 0))
6774                 rack_convert_rtts(tp);
6775         /*
6776          * We want a chance to stay in slowstart as
6777          * we create a connection. TCP spec says that
6778          * initially ssthresh is infinite. For our
6779          * purposes that is the snd_wnd.
6780          */
6781         if (tp->snd_ssthresh < tp->snd_wnd) {
6782                 tp->snd_ssthresh = tp->snd_wnd;
6783         }
6784         /*
6785          * We also want to assure a IW worth of
6786          * data can get inflight.
6787          */
6788         if (rc_init_window(rack) < tp->snd_cwnd)
6789                 tp->snd_cwnd = rc_init_window(rack);
6790 }
6791
6792 /*
6793  * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
6794  * we will setup to retransmit the lowest seq number outstanding.
6795  */
6796 static int
6797 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6798 {
6799         int32_t rexmt;
6800         struct inpcb *inp;
6801         int32_t retval = 0;
6802         bool isipv6;
6803
6804         inp = tp->t_inpcb;
6805         if (tp->t_timers->tt_flags & TT_STOPPED) {
6806                 return (1);
6807         }
6808         if ((tp->t_flags & TF_GPUTINPROG) &&
6809             (tp->t_rxtshift)) {
6810                 /*
6811                  * We have had a second timeout
6812                  * measurements on successive rxt's are not profitable.
6813                  * It is unlikely to be of any use (the network is
6814                  * broken or the client went away).
6815                  */
6816                 tp->t_flags &= ~TF_GPUTINPROG;
6817                 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
6818                                            rack->r_ctl.rc_gp_srtt /*flex1*/,
6819                                            tp->gput_seq,
6820                                            0, 0, 18, __LINE__, NULL, 0);
6821         }
6822         if (ctf_progress_timeout_check(tp, false)) {
6823                 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6824                 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
6825                 tcp_set_inp_to_drop(inp, ETIMEDOUT);
6826                 return (1);
6827         }
6828         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
6829         rack->r_ctl.retran_during_recovery = 0;
6830         rack->r_ctl.dsack_byte_cnt = 0;
6831         if (IN_FASTRECOVERY(tp->t_flags))
6832                 tp->t_flags |= TF_WASFRECOVERY;
6833         else
6834                 tp->t_flags &= ~TF_WASFRECOVERY;
6835         if (IN_CONGRECOVERY(tp->t_flags))
6836                 tp->t_flags |= TF_WASCRECOVERY;
6837         else
6838                 tp->t_flags &= ~TF_WASCRECOVERY;
6839         if (TCPS_HAVEESTABLISHED(tp->t_state) &&
6840             (tp->snd_una == tp->snd_max)) {
6841                 /* Nothing outstanding .. nothing to do */
6842                 return (0);
6843         }
6844         if (rack->r_ctl.dsack_persist) {
6845                 rack->r_ctl.dsack_persist--;
6846                 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
6847                         rack->r_ctl.num_dsack = 0;
6848                 }
6849                 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6850         }
6851         /*
6852          * Rack can only run one timer  at a time, so we cannot
6853          * run a KEEPINIT (gating SYN sending) and a retransmit
6854          * timer for the SYN. So if we are in a front state and
6855          * have a KEEPINIT timer we need to check the first transmit
6856          * against now to see if we have exceeded the KEEPINIT time
6857          * (if one is set).
6858          */
6859         if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6860             (TP_KEEPINIT(tp) != 0)) {
6861                 struct rack_sendmap *rsm;
6862
6863                 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6864                 if (rsm) {
6865                         /* Ok we have something outstanding to test keepinit with */
6866                         if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
6867                             ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
6868                                 /* We have exceeded the KEEPINIT time */
6869                                 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
6870                                 goto drop_it;
6871                         }
6872                 }
6873         }
6874         /*
6875          * Retransmission timer went off.  Message has not been acked within
6876          * retransmit interval.  Back off to a longer retransmit interval
6877          * and retransmit one segment.
6878          */
6879         rack_remxt_tmr(tp);
6880         if ((rack->r_ctl.rc_resend == NULL) ||
6881             ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
6882                 /*
6883                  * If the rwnd collapsed on
6884                  * the one we are retransmitting
6885                  * it does not count against the
6886                  * rxt count.
6887                  */
6888                 tp->t_rxtshift++;
6889         }
6890         if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
6891                 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6892 drop_it:
6893                 tp->t_rxtshift = TCP_MAXRXTSHIFT;
6894                 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
6895                 retval = 1;
6896                 tcp_set_inp_to_drop(rack->rc_inp,
6897                     (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
6898                 goto out;
6899         }
6900         if (tp->t_state == TCPS_SYN_SENT) {
6901                 /*
6902                  * If the SYN was retransmitted, indicate CWND to be limited
6903                  * to 1 segment in cc_conn_init().
6904                  */
6905                 tp->snd_cwnd = 1;
6906         } else if (tp->t_rxtshift == 1) {
6907                 /*
6908                  * first retransmit; record ssthresh and cwnd so they can be
6909                  * recovered if this turns out to be a "bad" retransmit. A
6910                  * retransmit is considered "bad" if an ACK for this segment
6911                  * is received within RTT/2 interval; the assumption here is
6912                  * that the ACK was already in flight.  See "On Estimating
6913                  * End-to-End Network Path Properties" by Allman and Paxson
6914                  * for more details.
6915                  */
6916                 tp->snd_cwnd_prev = tp->snd_cwnd;
6917                 tp->snd_ssthresh_prev = tp->snd_ssthresh;
6918                 tp->snd_recover_prev = tp->snd_recover;
6919                 tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
6920                 tp->t_flags |= TF_PREVVALID;
6921         } else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
6922                 tp->t_flags &= ~TF_PREVVALID;
6923         KMOD_TCPSTAT_INC(tcps_rexmttimeo);
6924         if ((tp->t_state == TCPS_SYN_SENT) ||
6925             (tp->t_state == TCPS_SYN_RECEIVED))
6926                 rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
6927         else
6928                 rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
6929
6930         RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
6931            max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
6932         /*
6933          * We enter the path for PLMTUD if connection is established or, if
6934          * connection is FIN_WAIT_1 status, reason for the last is that if
6935          * amount of data we send is very small, we could send it in couple
6936          * of packets and process straight to FIN. In that case we won't
6937          * catch ESTABLISHED state.
6938          */
6939 #ifdef INET6
6940         isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
6941 #else
6942         isipv6 = false;
6943 #endif
6944         if (((V_tcp_pmtud_blackhole_detect == 1) ||
6945             (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
6946             (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
6947             ((tp->t_state == TCPS_ESTABLISHED) ||
6948             (tp->t_state == TCPS_FIN_WAIT_1))) {
6949                 /*
6950                  * Idea here is that at each stage of mtu probe (usually,
6951                  * 1448 -> 1188 -> 524) should be given 2 chances to recover
6952                  * before further clamping down. 'tp->t_rxtshift % 2 == 0'
6953                  * should take care of that.
6954                  */
6955                 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
6956                     (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
6957                     (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
6958                     tp->t_rxtshift % 2 == 0)) {
6959                         /*
6960                          * Enter Path MTU Black-hole Detection mechanism: -
6961                          * Disable Path MTU Discovery (IP "DF" bit). -
6962                          * Reduce MTU to lower value than what we negotiated
6963                          * with peer.
6964                          */
6965                         if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
6966                                 /* Record that we may have found a black hole. */
6967                                 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
6968                                 /* Keep track of previous MSS. */
6969                                 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
6970                         }
6971
6972                         /*
6973                          * Reduce the MSS to blackhole value or to the
6974                          * default in an attempt to retransmit.
6975                          */
6976 #ifdef INET6
6977                         if (isipv6 &&
6978                             tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
6979                                 /* Use the sysctl tuneable blackhole MSS. */
6980                                 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
6981                                 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
6982                         } else if (isipv6) {
6983                                 /* Use the default MSS. */
6984                                 tp->t_maxseg = V_tcp_v6mssdflt;
6985                                 /*
6986                                  * Disable Path MTU Discovery when we switch
6987                                  * to minmss.
6988                                  */
6989                                 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
6990                                 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
6991                         }
6992 #endif
6993 #if defined(INET6) && defined(INET)
6994                         else
6995 #endif
6996 #ifdef INET
6997                         if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
6998                                 /* Use the sysctl tuneable blackhole MSS. */
6999                                 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7000                                 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7001                         } else {
7002                                 /* Use the default MSS. */
7003                                 tp->t_maxseg = V_tcp_mssdflt;
7004                                 /*
7005                                  * Disable Path MTU Discovery when we switch
7006                                  * to minmss.
7007                                  */
7008                                 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7009                                 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7010                         }
7011 #endif
7012                 } else {
7013                         /*
7014                          * If further retransmissions are still unsuccessful
7015                          * with a lowered MTU, maybe this isn't a blackhole
7016                          * and we restore the previous MSS and blackhole
7017                          * detection flags. The limit '6' is determined by
7018                          * giving each probe stage (1448, 1188, 524) 2
7019                          * chances to recover.
7020                          */
7021                         if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7022                             (tp->t_rxtshift >= 6)) {
7023                                 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
7024                                 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7025                                 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
7026                                 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
7027                         }
7028                 }
7029         }
7030         /*
7031          * Disable RFC1323 and SACK if we haven't got any response to
7032          * our third SYN to work-around some broken terminal servers
7033          * (most of which have hopefully been retired) that have bad VJ
7034          * header compression code which trashes TCP segments containing
7035          * unknown-to-them TCP options.
7036          */
7037         if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7038             (tp->t_rxtshift == 3))
7039                 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7040         /*
7041          * If we backed off this far, our srtt estimate is probably bogus.
7042          * Clobber it so we'll take the next rtt measurement as our srtt;
7043          * move the current srtt into rttvar to keep the current retransmit
7044          * times until then.
7045          */
7046         if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7047 #ifdef INET6
7048                 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
7049                         in6_losing(tp->t_inpcb);
7050                 else
7051 #endif
7052                         in_losing(tp->t_inpcb);
7053                 tp->t_rttvar += tp->t_srtt;
7054                 tp->t_srtt = 0;
7055         }
7056         sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
7057         tp->snd_recover = tp->snd_max;
7058         tp->t_flags |= TF_ACKNOW;
7059         tp->t_rtttime = 0;
7060         rack_cong_signal(tp, CC_RTO, tp->snd_una);
7061 out:
7062         return (retval);
7063 }
7064
7065 static int
7066 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7067 {
7068         int32_t ret = 0;
7069         int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7070
7071         if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7072             (tp->t_flags & TF_GPUTINPROG)) {
7073                 /*
7074                  * We have a goodput in progress
7075                  * and we have entered a late state.
7076                  * Do we have enough data in the sb
7077                  * to handle the GPUT request?
7078                  */
7079                 uint32_t bytes;
7080
7081                 bytes = tp->gput_ack - tp->gput_seq;
7082                 if (SEQ_GT(tp->gput_seq, tp->snd_una))
7083                         bytes += tp->gput_seq - tp->snd_una;
7084                 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
7085                         /*
7086                          * There are not enough bytes in the socket
7087                          * buffer that have been sent to cover this
7088                          * measurement. Cancel it.
7089                          */
7090                         rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7091                                                    rack->r_ctl.rc_gp_srtt /*flex1*/,
7092                                                    tp->gput_seq,
7093                                                    0, 0, 18, __LINE__, NULL, 0);
7094                         tp->t_flags &= ~TF_GPUTINPROG;
7095                 }
7096         }
7097         if (timers == 0) {
7098                 return (0);
7099         }
7100         if (tp->t_state == TCPS_LISTEN) {
7101                 /* no timers on listen sockets */
7102                 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
7103                         return (0);
7104                 return (1);
7105         }
7106         if ((timers & PACE_TMR_RACK) &&
7107             rack->rc_on_min_to) {
7108                 /*
7109                  * For the rack timer when we
7110                  * are on a min-timeout (which means rrr_conf = 3)
7111                  * we don't want to check the timer. It may
7112                  * be going off for a pace and thats ok we
7113                  * want to send the retransmit (if its ready).
7114                  *
7115                  * If its on a normal rack timer (non-min) then
7116                  * we will check if its expired.
7117                  */
7118                 goto skip_time_check;
7119         }
7120         if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7121                 uint32_t left;
7122
7123                 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
7124                         ret = -1;
7125                         rack_log_to_processing(rack, cts, ret, 0);
7126                         return (0);
7127                 }
7128                 if (hpts_calling == 0) {
7129                         /*
7130                          * A user send or queued mbuf (sack) has called us? We
7131                          * return 0 and let the pacing guards
7132                          * deal with it if they should or
7133                          * should not cause a send.
7134                          */
7135                         ret = -2;
7136                         rack_log_to_processing(rack, cts, ret, 0);
7137                         return (0);
7138                 }
7139                 /*
7140                  * Ok our timer went off early and we are not paced false
7141                  * alarm, go back to sleep.
7142                  */
7143                 ret = -3;
7144                 left = rack->r_ctl.rc_timer_exp - cts;
7145                 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left));
7146                 rack_log_to_processing(rack, cts, ret, left);
7147                 return (1);
7148         }
7149 skip_time_check:
7150         rack->rc_tmr_stopped = 0;
7151         rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
7152         if (timers & PACE_TMR_DELACK) {
7153                 ret = rack_timeout_delack(tp, rack, cts);
7154         } else if (timers & PACE_TMR_RACK) {
7155                 rack->r_ctl.rc_tlp_rxt_last_time = cts;
7156                 rack->r_fast_output = 0;
7157                 ret = rack_timeout_rack(tp, rack, cts);
7158         } else if (timers & PACE_TMR_TLP) {
7159                 rack->r_ctl.rc_tlp_rxt_last_time = cts;
7160                 ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
7161         } else if (timers & PACE_TMR_RXT) {
7162                 rack->r_ctl.rc_tlp_rxt_last_time = cts;
7163                 rack->r_fast_output = 0;
7164                 ret = rack_timeout_rxt(tp, rack, cts);
7165         } else if (timers & PACE_TMR_PERSIT) {
7166                 ret = rack_timeout_persist(tp, rack, cts);
7167         } else if (timers & PACE_TMR_KEEP) {
7168                 ret = rack_timeout_keepalive(tp, rack, cts);
7169         }
7170         rack_log_to_processing(rack, cts, ret, timers);
7171         return (ret);
7172 }
7173
7174 static void
7175 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
7176 {
7177         struct timeval tv;
7178         uint32_t us_cts, flags_on_entry;
7179         uint8_t hpts_removed = 0;
7180
7181         flags_on_entry = rack->r_ctl.rc_hpts_flags;
7182         us_cts = tcp_get_usecs(&tv);
7183         if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
7184             ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
7185              ((tp->snd_max - tp->snd_una) == 0))) {
7186                 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
7187                 hpts_removed = 1;
7188                 /* If we were not delayed cancel out the flag. */
7189                 if ((tp->snd_max - tp->snd_una) == 0)
7190                         rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
7191                 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7192         }
7193         if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
7194                 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
7195                 if (rack->rc_inp->inp_in_hpts &&
7196                     ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
7197                         /*
7198                          * Canceling timer's when we have no output being
7199                          * paced. We also must remove ourselves from the
7200                          * hpts.
7201                          */
7202                         tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
7203                         hpts_removed = 1;
7204                 }
7205                 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
7206         }
7207         if (hpts_removed == 0)
7208                 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7209 }
7210
7211 static void
7212 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
7213 {
7214         return;
7215 }
7216
7217 static int
7218 rack_stopall(struct tcpcb *tp)
7219 {
7220         struct tcp_rack *rack;
7221         rack = (struct tcp_rack *)tp->t_fb_ptr;
7222         rack->t_timers_stopped = 1;
7223         return (0);
7224 }
7225
7226 static void
7227 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
7228 {
7229         return;
7230 }
7231
7232 static int
7233 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
7234 {
7235         return (0);
7236 }
7237
7238 static void
7239 rack_stop_all_timers(struct tcpcb *tp)
7240 {
7241         struct tcp_rack *rack;
7242
7243         /*
7244          * Assure no timers are running.
7245          */
7246         if (tcp_timer_active(tp, TT_PERSIST)) {
7247                 /* We enter in persists, set the flag appropriately */
7248                 rack = (struct tcp_rack *)tp->t_fb_ptr;
7249                 rack->rc_in_persist = 1;
7250         }
7251         tcp_timer_suspend(tp, TT_PERSIST);
7252         tcp_timer_suspend(tp, TT_REXMT);
7253         tcp_timer_suspend(tp, TT_KEEP);
7254         tcp_timer_suspend(tp, TT_DELACK);
7255 }
7256
7257 static void
7258 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
7259     struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag)
7260 {
7261         int32_t idx;
7262         uint16_t stripped_flags;
7263
7264         rsm->r_rtr_cnt++;
7265         rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7266         rsm->r_dupack = 0;
7267         if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
7268                 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
7269                 rsm->r_flags |= RACK_OVERMAX;
7270         }
7271         if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
7272                 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
7273                 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
7274         }
7275         idx = rsm->r_rtr_cnt - 1;
7276         rsm->r_tim_lastsent[idx] = ts;
7277         /*
7278          * Here we don't add in the len of send, since its already
7279          * in snduna <->snd_max.
7280          */
7281         rsm->r_fas = ctf_flight_size(rack->rc_tp,
7282                                      rack->r_ctl.rc_sacked);
7283         stripped_flags = rsm->r_flags & ~(RACK_SENT_SP|RACK_SENT_FP);
7284         if (rsm->r_flags & RACK_ACKED) {
7285                 /* Problably MTU discovery messing with us */
7286                 rsm->r_flags &= ~RACK_ACKED;
7287                 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7288         }
7289         if (rsm->r_in_tmap) {
7290                 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7291                 rsm->r_in_tmap = 0;
7292         }
7293         TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7294         rsm->r_in_tmap = 1;
7295         if (rsm->r_flags & RACK_SACK_PASSED) {
7296                 /* We have retransmitted due to the SACK pass */
7297                 rsm->r_flags &= ~RACK_SACK_PASSED;
7298                 rsm->r_flags |= RACK_WAS_SACKPASS;
7299         }
7300 }
7301
7302 static uint32_t
7303 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
7304     struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag)
7305 {
7306         /*
7307          * We (re-)transmitted starting at rsm->r_start for some length
7308          * (possibly less than r_end.
7309          */
7310         struct rack_sendmap *nrsm, *insret;
7311         uint32_t c_end;
7312         int32_t len;
7313
7314         len = *lenp;
7315         c_end = rsm->r_start + len;
7316         if (SEQ_GEQ(c_end, rsm->r_end)) {
7317                 /*
7318                  * We retransmitted the whole piece or more than the whole
7319                  * slopping into the next rsm.
7320                  */
7321                 rack_update_rsm(tp, rack, rsm, ts, add_flag);
7322                 if (c_end == rsm->r_end) {
7323                         *lenp = 0;
7324                         return (0);
7325                 } else {
7326                         int32_t act_len;
7327
7328                         /* Hangs over the end return whats left */
7329                         act_len = rsm->r_end - rsm->r_start;
7330                         *lenp = (len - act_len);
7331                         return (rsm->r_end);
7332                 }
7333                 /* We don't get out of this block. */
7334         }
7335         /*
7336          * Here we retransmitted less than the whole thing which means we
7337          * have to split this into what was transmitted and what was not.
7338          */
7339         nrsm = rack_alloc_full_limit(rack);
7340         if (nrsm == NULL) {
7341                 /*
7342                  * We can't get memory, so lets not proceed.
7343                  */
7344                 *lenp = 0;
7345                 return (0);
7346         }
7347         /*
7348          * So here we are going to take the original rsm and make it what we
7349          * retransmitted. nrsm will be the tail portion we did not
7350          * retransmit. For example say the chunk was 1, 11 (10 bytes). And
7351          * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
7352          * 1, 6 and the new piece will be 6, 11.
7353          */
7354         rack_clone_rsm(rack, nrsm, rsm, c_end);
7355         nrsm->r_dupack = 0;
7356         rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
7357         insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7358 #ifdef INVARIANTS
7359         if (insret != NULL) {
7360                 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7361                       nrsm, insret, rack, rsm);
7362         }
7363 #endif
7364         if (rsm->r_in_tmap) {
7365                 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7366                 nrsm->r_in_tmap = 1;
7367         }
7368         rsm->r_flags &= (~RACK_HAS_FIN);
7369         rack_update_rsm(tp, rack, rsm, ts, add_flag);
7370         /* Log a split of rsm into rsm and nrsm */
7371         rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7372         *lenp = 0;
7373         return (0);
7374 }
7375
7376 static void
7377 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
7378                 uint32_t seq_out, uint8_t th_flags, int32_t err, uint64_t cts,
7379                 struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb, uint32_t s_moff, int hw_tls)
7380 {
7381         struct tcp_rack *rack;
7382         struct rack_sendmap *rsm, *nrsm, *insret, fe;
7383         register uint32_t snd_max, snd_una;
7384
7385         /*
7386          * Add to the RACK log of packets in flight or retransmitted. If
7387          * there is a TS option we will use the TS echoed, if not we will
7388          * grab a TS.
7389          *
7390          * Retransmissions will increment the count and move the ts to its
7391          * proper place. Note that if options do not include TS's then we
7392          * won't be able to effectively use the ACK for an RTT on a retran.
7393          *
7394          * Notes about r_start and r_end. Lets consider a send starting at
7395          * sequence 1 for 10 bytes. In such an example the r_start would be
7396          * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
7397          * This means that r_end is actually the first sequence for the next
7398          * slot (11).
7399          *
7400          */
7401         /*
7402          * If err is set what do we do XXXrrs? should we not add the thing?
7403          * -- i.e. return if err != 0 or should we pretend we sent it? --
7404          * i.e. proceed with add ** do this for now.
7405          */
7406         INP_WLOCK_ASSERT(tp->t_inpcb);
7407         if (err)
7408                 /*
7409                  * We don't log errors -- we could but snd_max does not
7410                  * advance in this case either.
7411                  */
7412                 return;
7413
7414         if (th_flags & TH_RST) {
7415                 /*
7416                  * We don't log resets and we return immediately from
7417                  * sending
7418                  */
7419                 return;
7420         }
7421         rack = (struct tcp_rack *)tp->t_fb_ptr;
7422         snd_una = tp->snd_una;
7423         snd_max = tp->snd_max;
7424         if (th_flags & (TH_SYN | TH_FIN)) {
7425                 /*
7426                  * The call to rack_log_output is made before bumping
7427                  * snd_max. This means we can record one extra byte on a SYN
7428                  * or FIN if seq_out is adding more on and a FIN is present
7429                  * (and we are not resending).
7430                  */
7431                 if ((th_flags & TH_SYN) && (seq_out == tp->iss))
7432                         len++;
7433                 if (th_flags & TH_FIN)
7434                         len++;
7435                 if (SEQ_LT(snd_max, tp->snd_nxt)) {
7436                         /*
7437                          * The add/update as not been done for the FIN/SYN
7438                          * yet.
7439                          */
7440                         snd_max = tp->snd_nxt;
7441                 }
7442         }
7443         if (SEQ_LEQ((seq_out + len), snd_una)) {
7444                 /* Are sending an old segment to induce an ack (keep-alive)? */
7445                 return;
7446         }
7447         if (SEQ_LT(seq_out, snd_una)) {
7448                 /* huh? should we panic? */
7449                 uint32_t end;
7450
7451                 end = seq_out + len;
7452                 seq_out = snd_una;
7453                 if (SEQ_GEQ(end, seq_out))
7454                         len = end - seq_out;
7455                 else
7456                         len = 0;
7457         }
7458         if (len == 0) {
7459                 /* We don't log zero window probes */
7460                 return;
7461         }
7462         rack->r_ctl.rc_time_last_sent = cts;
7463         if (IN_FASTRECOVERY(tp->t_flags)) {
7464                 rack->r_ctl.rc_prr_out += len;
7465         }
7466         /* First question is it a retransmission or new? */
7467         if (seq_out == snd_max) {
7468                 /* Its new */
7469 again:
7470                 rsm = rack_alloc(rack);
7471                 if (rsm == NULL) {
7472                         /*
7473                          * Hmm out of memory and the tcb got destroyed while
7474                          * we tried to wait.
7475                          */
7476                         return;
7477                 }
7478                 if (th_flags & TH_FIN) {
7479                         rsm->r_flags = RACK_HAS_FIN|add_flag;
7480                 } else {
7481                         rsm->r_flags = add_flag;
7482                 }
7483                 if (hw_tls)
7484                         rsm->r_hw_tls = 1;
7485                 rsm->r_tim_lastsent[0] = cts;
7486                 rsm->r_rtr_cnt = 1;
7487                 rsm->r_rtr_bytes = 0;
7488                 if (th_flags & TH_SYN) {
7489                         /* The data space is one beyond snd_una */
7490                         rsm->r_flags |= RACK_HAS_SYN;
7491                 }
7492                 rsm->r_start = seq_out;
7493                 rsm->r_end = rsm->r_start + len;
7494                 rsm->r_dupack = 0;
7495                 /*
7496                  * save off the mbuf location that
7497                  * sndmbuf_noadv returned (which is
7498                  * where we started copying from)..
7499                  */
7500                 rsm->m = s_mb;
7501                 rsm->soff = s_moff;
7502                 /*
7503                  * Here we do add in the len of send, since its not yet
7504                  * reflected in in snduna <->snd_max
7505                  */
7506                 rsm->r_fas = (ctf_flight_size(rack->rc_tp,
7507                                               rack->r_ctl.rc_sacked) +
7508                               (rsm->r_end - rsm->r_start));
7509                 /* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
7510                 if (rsm->m) {
7511                         if (rsm->m->m_len <= rsm->soff) {
7512                                 /*
7513                                  * XXXrrs Question, will this happen?
7514                                  *
7515                                  * If sbsndptr is set at the correct place
7516                                  * then s_moff should always be somewhere
7517                                  * within rsm->m. But if the sbsndptr was
7518                                  * off then that won't be true. If it occurs
7519                                  * we need to walkout to the correct location.
7520                                  */
7521                                 struct mbuf *lm;
7522
7523                                 lm = rsm->m;
7524                                 while (lm->m_len <= rsm->soff) {
7525                                         rsm->soff -= lm->m_len;
7526                                         lm = lm->m_next;
7527                                         KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
7528                                                              __func__, rack, s_moff, s_mb, rsm->soff));
7529                                 }
7530                                 rsm->m = lm;
7531                                 counter_u64_add(rack_sbsndptr_wrong, 1);
7532                         } else
7533                                 counter_u64_add(rack_sbsndptr_right, 1);
7534                         rsm->orig_m_len = rsm->m->m_len;
7535                 } else
7536                         rsm->orig_m_len = 0;
7537                 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7538                 /* Log a new rsm */
7539                 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
7540                 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7541 #ifdef INVARIANTS
7542                 if (insret != NULL) {
7543                         panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7544                               nrsm, insret, rack, rsm);
7545                 }
7546 #endif
7547                 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7548                 rsm->r_in_tmap = 1;
7549                 /*
7550                  * Special case detection, is there just a single
7551                  * packet outstanding when we are not in recovery?
7552                  *
7553                  * If this is true mark it so.
7554                  */
7555                 if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
7556                     (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
7557                         struct rack_sendmap *prsm;
7558
7559                         prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7560                         if (prsm)
7561                                 prsm->r_one_out_nr = 1;
7562                 }
7563                 return;
7564         }
7565         /*
7566          * If we reach here its a retransmission and we need to find it.
7567          */
7568         memset(&fe, 0, sizeof(fe));
7569 more:
7570         if (hintrsm && (hintrsm->r_start == seq_out)) {
7571                 rsm = hintrsm;
7572                 hintrsm = NULL;
7573         } else {
7574                 /* No hints sorry */
7575                 rsm = NULL;
7576         }
7577         if ((rsm) && (rsm->r_start == seq_out)) {
7578                 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7579                 if (len == 0) {
7580                         return;
7581                 } else {
7582                         goto more;
7583                 }
7584         }
7585         /* Ok it was not the last pointer go through it the hard way. */
7586 refind:
7587         fe.r_start = seq_out;
7588         rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
7589         if (rsm) {
7590                 if (rsm->r_start == seq_out) {
7591                         seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7592                         if (len == 0) {
7593                                 return;
7594                         } else {
7595                                 goto refind;
7596                         }
7597                 }
7598                 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
7599                         /* Transmitted within this piece */
7600                         /*
7601                          * Ok we must split off the front and then let the
7602                          * update do the rest
7603                          */
7604                         nrsm = rack_alloc_full_limit(rack);
7605                         if (nrsm == NULL) {
7606                                 rack_update_rsm(tp, rack, rsm, cts, add_flag);
7607                                 return;
7608                         }
7609                         /*
7610                          * copy rsm to nrsm and then trim the front of rsm
7611                          * to not include this part.
7612                          */
7613                         rack_clone_rsm(rack, nrsm, rsm, seq_out);
7614                         insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7615                         rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7616 #ifdef INVARIANTS
7617                         if (insret != NULL) {
7618                                 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7619                                       nrsm, insret, rack, rsm);
7620                         }
7621 #endif
7622                         if (rsm->r_in_tmap) {
7623                                 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7624                                 nrsm->r_in_tmap = 1;
7625                         }
7626                         rsm->r_flags &= (~RACK_HAS_FIN);
7627                         seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag);
7628                         if (len == 0) {
7629                                 return;
7630                         } else if (len > 0)
7631                                 goto refind;
7632                 }
7633         }
7634         /*
7635          * Hmm not found in map did they retransmit both old and on into the
7636          * new?
7637          */
7638         if (seq_out == tp->snd_max) {
7639                 goto again;
7640         } else if (SEQ_LT(seq_out, tp->snd_max)) {
7641 #ifdef INVARIANTS
7642                 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
7643                        seq_out, len, tp->snd_una, tp->snd_max);
7644                 printf("Starting Dump of all rack entries\n");
7645                 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
7646                         printf("rsm:%p start:%u end:%u\n",
7647                                rsm, rsm->r_start, rsm->r_end);
7648                 }
7649                 printf("Dump complete\n");
7650                 panic("seq_out not found rack:%p tp:%p",
7651                       rack, tp);
7652 #endif
7653         } else {
7654 #ifdef INVARIANTS
7655                 /*
7656                  * Hmm beyond sndmax? (only if we are using the new rtt-pack
7657                  * flag)
7658                  */
7659                 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
7660                       seq_out, len, tp->snd_max, tp);
7661 #endif
7662         }
7663 }
7664
7665 /*
7666  * Record one of the RTT updates from an ack into
7667  * our sample structure.
7668  */
7669
7670 static void
7671 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
7672                     int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
7673 {
7674         if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7675             (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
7676                 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
7677         }
7678         if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7679             (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
7680                 rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
7681         }
7682         if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
7683             if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
7684                 rack->r_ctl.rc_gp_lowrtt = us_rtt;
7685             if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
7686                     rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
7687         }
7688         if ((confidence == 1) &&
7689             ((rsm == NULL) ||
7690              (rsm->r_just_ret) ||
7691              (rsm->r_one_out_nr &&
7692               len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
7693                 /*
7694                  * If the rsm had a just return
7695                  * hit it then we can't trust the
7696                  * rtt measurement for buffer deterimination
7697                  * Note that a confidence of 2, indicates
7698                  * SACK'd which overrides the r_just_ret or
7699                  * the r_one_out_nr. If it was a CUM-ACK and
7700                  * we had only two outstanding, but get an
7701                  * ack for only 1. Then that also lowers our
7702                  * confidence.
7703                  */
7704                 confidence = 0;
7705         }
7706         if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
7707             (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
7708                 if (rack->r_ctl.rack_rs.confidence == 0) {
7709                         /*
7710                          * We take anything with no current confidence
7711                          * saved.
7712                          */
7713                         rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7714                         rack->r_ctl.rack_rs.confidence = confidence;
7715                         rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7716                 } else if (confidence || rack->r_ctl.rack_rs.confidence) {
7717                         /*
7718                          * Once we have a confident number,
7719                          * we can update it with a smaller
7720                          * value since this confident number
7721                          * may include the DSACK time until
7722                          * the next segment (the second one) arrived.
7723                          */
7724                         rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
7725                         rack->r_ctl.rack_rs.confidence = confidence;
7726                         rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
7727                 }
7728         }
7729         rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
7730         rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
7731         rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
7732         rack->r_ctl.rack_rs.rs_rtt_cnt++;
7733 }
7734
7735 /*
7736  * Collect new round-trip time estimate
7737  * and update averages and current timeout.
7738  */
7739 static void
7740 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
7741 {
7742         int32_t delta;
7743         uint32_t o_srtt, o_var;
7744         int32_t hrtt_up = 0;
7745         int32_t rtt;
7746
7747         if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
7748                 /* No valid sample */
7749                 return;
7750         if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
7751                 /* We are to use the lowest RTT seen in a single ack */
7752                 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
7753         } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
7754                 /* We are to use the highest RTT seen in a single ack */
7755                 rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
7756         } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
7757                 /* We are to use the average RTT seen in a single ack */
7758                 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
7759                                 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
7760         } else {
7761 #ifdef INVARIANTS
7762                 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
7763 #endif
7764                 return;
7765         }
7766         if (rtt == 0)
7767                 rtt = 1;
7768         if (rack->rc_gp_rtt_set == 0) {
7769                 /*
7770                  * With no RTT we have to accept
7771                  * even one we are not confident of.
7772                  */
7773                 rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
7774                 rack->rc_gp_rtt_set = 1;
7775         } else if (rack->r_ctl.rack_rs.confidence) {
7776                 /* update the running gp srtt */
7777                 rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
7778                 rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
7779         }
7780         if (rack->r_ctl.rack_rs.confidence) {
7781                 /*
7782                  * record the low and high for highly buffered path computation,
7783                  * we only do this if we are confident (not a retransmission).
7784                  */
7785                 if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
7786                         rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7787                         hrtt_up = 1;
7788                 }
7789                 if (rack->rc_highly_buffered == 0) {
7790                         /*
7791                          * Currently once we declare a path has
7792                          * highly buffered there is no going
7793                          * back, which may be a problem...
7794                          */
7795                         if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
7796                                 rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
7797                                                      rack->r_ctl.rc_highest_us_rtt,
7798                                                      rack->r_ctl.rc_lowest_us_rtt,
7799                                                      RACK_RTTS_SEEHBP);
7800                                 rack->rc_highly_buffered = 1;
7801                         }
7802                 }
7803         }
7804         if ((rack->r_ctl.rack_rs.confidence) ||
7805             (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
7806                 /*
7807                  * If we are highly confident of it <or> it was
7808                  * never retransmitted we accept it as the last us_rtt.
7809                  */
7810                 rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7811                 /* The lowest rtt can be set if its was not retransmited */
7812                 if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
7813                         rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
7814                         if (rack->r_ctl.rc_lowest_us_rtt == 0)
7815                                 rack->r_ctl.rc_lowest_us_rtt = 1;
7816                 }
7817         }
7818         o_srtt = tp->t_srtt;
7819         o_var = tp->t_rttvar;
7820         rack = (struct tcp_rack *)tp->t_fb_ptr;
7821         if (tp->t_srtt != 0) {
7822                 /*
7823                  * We keep a simple srtt in microseconds, like our rtt
7824                  * measurement. We don't need to do any tricks with shifting
7825                  * etc. Instead we just add in 1/8th of the new measurement
7826                  * and subtract out 1/8 of the old srtt. We do the same with
7827                  * the variance after finding the absolute value of the
7828                  * difference between this sample and the current srtt.
7829                  */
7830                 delta = tp->t_srtt - rtt;
7831                 /* Take off 1/8th of the current sRTT */
7832                 tp->t_srtt -= (tp->t_srtt >> 3);
7833                 /* Add in 1/8th of the new RTT just measured */
7834                 tp->t_srtt += (rtt >> 3);
7835                 if (tp->t_srtt <= 0)
7836                         tp->t_srtt = 1;
7837                 /* Now lets make the absolute value of the variance */
7838                 if (delta < 0)
7839                         delta = -delta;
7840                 /* Subtract out 1/8th */
7841                 tp->t_rttvar -= (tp->t_rttvar >> 3);
7842                 /* Add in 1/8th of the new variance we just saw */
7843                 tp->t_rttvar += (delta >> 3);
7844                 if (tp->t_rttvar <= 0)
7845                         tp->t_rttvar = 1;
7846                 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
7847                         tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7848         } else {
7849                 /*
7850                  * No rtt measurement yet - use the unsmoothed rtt. Set the
7851                  * variance to half the rtt (so our first retransmit happens
7852                  * at 3*rtt).
7853                  */
7854                 tp->t_srtt = rtt;
7855                 tp->t_rttvar = rtt >> 1;
7856                 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7857         }
7858         rack->rc_srtt_measure_made = 1;
7859         KMOD_TCPSTAT_INC(tcps_rttupdated);
7860         tp->t_rttupdated++;
7861 #ifdef STATS
7862         if (rack_stats_gets_ms_rtt == 0) {
7863                 /* Send in the microsecond rtt used for rxt timeout purposes */
7864                 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
7865         } else if (rack_stats_gets_ms_rtt == 1) {
7866                 /* Send in the millisecond rtt used for rxt timeout purposes */
7867                 int32_t ms_rtt;
7868
7869                 /* Round up */
7870                 ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7871                 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7872         } else if (rack_stats_gets_ms_rtt == 2) {
7873                 /* Send in the millisecond rtt has close to the path RTT as we can get  */
7874                 int32_t ms_rtt;
7875
7876                 /* Round up */
7877                 ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
7878                 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
7879         }  else {
7880                 /* Send in the microsecond rtt has close to the path RTT as we can get  */
7881                 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
7882         }
7883
7884 #endif
7885         /*
7886          * the retransmit should happen at rtt + 4 * rttvar. Because of the
7887          * way we do the smoothing, srtt and rttvar will each average +1/2
7888          * tick of bias.  When we compute the retransmit timer, we want 1/2
7889          * tick of rounding and 1 extra tick because of +-1/2 tick
7890          * uncertainty in the firing of the timer.  The bias will give us
7891          * exactly the 1.5 tick we need.  But, because the bias is
7892          * statistical, we have to test that we don't drop below the minimum
7893          * feasible timer (which is 2 ticks).
7894          */
7895         tp->t_rxtshift = 0;
7896         RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
7897                       max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
7898         rack_log_rtt_sample(rack, rtt);
7899         tp->t_softerror = 0;
7900 }
7901
7902
7903 static void
7904 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
7905 {
7906         /*
7907          * Apply to filter the inbound us-rtt at us_cts.
7908          */
7909         uint32_t old_rtt;
7910
7911         old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
7912         apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
7913                                us_rtt, us_cts);
7914         if (rack->r_ctl.last_pacing_time &&
7915             rack->rc_gp_dyn_mul &&
7916             (rack->r_ctl.last_pacing_time > us_rtt))
7917                 rack->pacing_longer_than_rtt = 1;
7918         else
7919                 rack->pacing_longer_than_rtt = 0;
7920         if (old_rtt > us_rtt) {
7921                 /* We just hit a new lower rtt time */
7922                 rack_log_rtt_shrinks(rack,  us_cts,  old_rtt,
7923                                      __LINE__, RACK_RTTS_NEWRTT);
7924                 /*
7925                  * Only count it if its lower than what we saw within our
7926                  * calculated range.
7927                  */
7928                 if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
7929                         if (rack_probertt_lower_within &&
7930                             rack->rc_gp_dyn_mul &&
7931                             (rack->use_fixed_rate == 0) &&
7932                             (rack->rc_always_pace)) {
7933                                 /*
7934                                  * We are seeing a new lower rtt very close
7935                                  * to the time that we would have entered probe-rtt.
7936                                  * This is probably due to the fact that a peer flow
7937                                  * has entered probe-rtt. Lets go in now too.
7938                                  */
7939                                 uint32_t val;
7940
7941                                 val = rack_probertt_lower_within * rack_time_between_probertt;
7942                                 val /= 100;
7943                                 if ((rack->in_probe_rtt == 0)  &&
7944                                     ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val))) {
7945                                         rack_enter_probertt(rack, us_cts);
7946                                 }
7947                         }
7948                         rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
7949                 }
7950         }
7951 }
7952
7953 static int
7954 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
7955     struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
7956 {
7957         uint32_t us_rtt;
7958         int32_t i, all;
7959         uint32_t t, len_acked;
7960
7961         if ((rsm->r_flags & RACK_ACKED) ||
7962             (rsm->r_flags & RACK_WAS_ACKED))
7963                 /* Already done */
7964                 return (0);
7965         if (rsm->r_no_rtt_allowed) {
7966                 /* Not allowed */
7967                 return (0);
7968         }
7969         if (ack_type == CUM_ACKED) {
7970                 if (SEQ_GT(th_ack, rsm->r_end)) {
7971                         len_acked = rsm->r_end - rsm->r_start;
7972                         all = 1;
7973                 } else {
7974                         len_acked = th_ack - rsm->r_start;
7975                         all = 0;
7976                 }
7977         } else {
7978                 len_acked = rsm->r_end - rsm->r_start;
7979                 all = 0;
7980         }
7981         if (rsm->r_rtr_cnt == 1) {
7982
7983                 t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
7984                 if ((int)t <= 0)
7985                         t = 1;
7986                 if (!tp->t_rttlow || tp->t_rttlow > t)
7987                         tp->t_rttlow = t;
7988                 if (!rack->r_ctl.rc_rack_min_rtt ||
7989                     SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
7990                         rack->r_ctl.rc_rack_min_rtt = t;
7991                         if (rack->r_ctl.rc_rack_min_rtt == 0) {
7992                                 rack->r_ctl.rc_rack_min_rtt = 1;
7993                         }
7994                 }
7995                 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
7996                         us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
7997                 else
7998                         us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
7999                 if (us_rtt == 0)
8000                         us_rtt = 1;
8001                 if (CC_ALGO(tp)->rttsample != NULL) {
8002                         /* Kick the RTT to the CC */
8003                         CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
8004                 }
8005                 rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8006                 if (ack_type == SACKED) {
8007                         rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8008                         tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
8009                 } else {
8010                         /*
8011                          * We need to setup what our confidence
8012                          * is in this ack.
8013                          *
8014                          * If the rsm was app limited and it is
8015                          * less than a mss in length (the end
8016                          * of the send) then we have a gap. If we
8017                          * were app limited but say we were sending
8018                          * multiple MSS's then we are more confident
8019                          * int it.
8020                          *
8021                          * When we are not app-limited then we see if
8022                          * the rsm is being included in the current
8023                          * measurement, we tell this by the app_limited_needs_set
8024                          * flag.
8025                          *
8026                          * Note that being cwnd blocked is not applimited
8027                          * as well as the pacing delay between packets which
8028                          * are sending only 1 or 2 MSS's also will show up
8029                          * in the RTT. We probably need to examine this algorithm
8030                          * a bit more and enhance it to account for the delay
8031                          * between rsm's. We could do that by saving off the
8032                          * pacing delay of each rsm (in an rsm) and then
8033                          * factoring that in somehow though for now I am
8034                          * not sure how :)
8035                          */
8036                         int calc_conf = 0;
8037
8038                         if (rsm->r_flags & RACK_APP_LIMITED) {
8039                                 if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8040                                         calc_conf = 0;
8041                                 else
8042                                         calc_conf = 1;
8043                         } else if (rack->app_limited_needs_set == 0) {
8044                                 calc_conf = 1;
8045                         } else {
8046                                 calc_conf = 0;
8047                         }
8048                         rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
8049                         tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
8050                                             calc_conf, rsm, rsm->r_rtr_cnt);
8051                 }
8052                 if ((rsm->r_flags & RACK_TLP) &&
8053                     (!IN_FASTRECOVERY(tp->t_flags))) {
8054                         /* Segment was a TLP and our retrans matched */
8055                         if (rack->r_ctl.rc_tlp_cwnd_reduce) {
8056                                 rack->r_ctl.rc_rsm_start = tp->snd_max;
8057                                 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
8058                                 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
8059                                 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
8060                         }
8061                 }
8062                 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
8063                         /* New more recent rack_tmit_time */
8064                         rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8065                         rack->rc_rack_rtt = t;
8066                 }
8067                 return (1);
8068         }
8069         /*
8070          * We clear the soft/rxtshift since we got an ack.
8071          * There is no assurance we will call the commit() function
8072          * so we need to clear these to avoid incorrect handling.
8073          */
8074         tp->t_rxtshift = 0;
8075         RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8076                       rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
8077         tp->t_softerror = 0;
8078         if (to && (to->to_flags & TOF_TS) &&
8079             (ack_type == CUM_ACKED) &&
8080             (to->to_tsecr) &&
8081             ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8082                 /*
8083                  * Now which timestamp does it match? In this block the ACK
8084                  * must be coming from a previous transmission.
8085                  */
8086                 for (i = 0; i < rsm->r_rtr_cnt; i++) {
8087                         if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
8088                                 t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8089                                 if ((int)t <= 0)
8090                                         t = 1;
8091                                 if (CC_ALGO(tp)->rttsample != NULL) {
8092                                         /*
8093                                          * Kick the RTT to the CC, here
8094                                          * we lie a bit in that we know the
8095                                          * retransmission is correct even though
8096                                          * we retransmitted. This is because
8097                                          * we match the timestamps.
8098                                          */
8099                                         if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
8100                                                 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
8101                                         else
8102                                                 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
8103                                         CC_ALGO(tp)->rttsample(tp->ccv, us_rtt, 1, rsm->r_fas);
8104                                 }
8105                                 if ((i + 1) < rsm->r_rtr_cnt) {
8106                                         /*
8107                                          * The peer ack'd from our previous
8108                                          * transmission. We have a spurious
8109                                          * retransmission and thus we dont
8110                                          * want to update our rack_rtt.
8111                                          *
8112                                          * Hmm should there be a CC revert here?
8113                                          *
8114                                          */
8115                                         return (0);
8116                                 }
8117                                 if (!tp->t_rttlow || tp->t_rttlow > t)
8118                                         tp->t_rttlow = t;
8119                                 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8120                                         rack->r_ctl.rc_rack_min_rtt = t;
8121                                         if (rack->r_ctl.rc_rack_min_rtt == 0) {
8122                                                 rack->r_ctl.rc_rack_min_rtt = 1;
8123                                         }
8124                                 }
8125                                 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
8126                                            (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
8127                                         /* New more recent rack_tmit_time */
8128                                         rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8129                                         rack->rc_rack_rtt = t;
8130                                 }
8131                                 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
8132                                 tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
8133                                                     rsm->r_rtr_cnt);
8134                                 return (1);
8135                         }
8136                 }
8137                 goto ts_not_found;
8138         } else {
8139                 /*
8140                  * Ok its a SACK block that we retransmitted. or a windows
8141                  * machine without timestamps. We can tell nothing from the
8142                  * time-stamp since its not there or the time the peer last
8143                  * recieved a segment that moved forward its cum-ack point.
8144                  */
8145 ts_not_found:
8146                 i = rsm->r_rtr_cnt - 1;
8147                 t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8148                 if ((int)t <= 0)
8149                         t = 1;
8150                 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8151                         /*
8152                          * We retransmitted and the ack came back in less
8153                          * than the smallest rtt we have observed. We most
8154                          * likely did an improper retransmit as outlined in
8155                          * 6.2 Step 2 point 2 in the rack-draft so we
8156                          * don't want to update our rack_rtt. We in
8157                          * theory (in future) might want to think about reverting our
8158                          * cwnd state but we won't for now.
8159                          */
8160                         return (0);
8161                 } else if (rack->r_ctl.rc_rack_min_rtt) {
8162                         /*
8163                          * We retransmitted it and the retransmit did the
8164                          * job.
8165                          */
8166                         if (!rack->r_ctl.rc_rack_min_rtt ||
8167                             SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8168                                 rack->r_ctl.rc_rack_min_rtt = t;
8169                                 if (rack->r_ctl.rc_rack_min_rtt == 0) {
8170                                         rack->r_ctl.rc_rack_min_rtt = 1;
8171                                 }
8172                         }
8173                         if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[i])) {
8174                                 /* New more recent rack_tmit_time */
8175                                 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
8176                                 rack->rc_rack_rtt = t;
8177                         }
8178                         return (1);
8179                 }
8180         }
8181         return (0);
8182 }
8183
8184 /*
8185  * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
8186  */
8187 static void
8188 rack_log_sack_passed(struct tcpcb *tp,
8189     struct tcp_rack *rack, struct rack_sendmap *rsm)
8190 {
8191         struct rack_sendmap *nrsm;
8192
8193         nrsm = rsm;
8194         TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
8195             rack_head, r_tnext) {
8196                 if (nrsm == rsm) {
8197                         /* Skip orginal segment he is acked */
8198                         continue;
8199                 }
8200                 if (nrsm->r_flags & RACK_ACKED) {
8201                         /*
8202                          * Skip ack'd segments, though we
8203                          * should not see these, since tmap
8204                          * should not have ack'd segments.
8205                          */
8206                         continue;
8207                 }
8208                 if (nrsm->r_flags & RACK_SACK_PASSED) {
8209                         /*
8210                          * We found one that is already marked
8211                          * passed, we have been here before and
8212                          * so all others below this are marked.
8213                          */
8214                         break;
8215                 }
8216                 nrsm->r_flags |= RACK_SACK_PASSED;
8217                 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
8218         }
8219 }
8220
8221 static void
8222 rack_need_set_test(struct tcpcb *tp,
8223                    struct tcp_rack *rack,
8224                    struct rack_sendmap *rsm,
8225                    tcp_seq th_ack,
8226                    int line,
8227                    int use_which)
8228 {
8229
8230         if ((tp->t_flags & TF_GPUTINPROG) &&
8231             SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8232                 /*
8233                  * We were app limited, and this ack
8234                  * butts up or goes beyond the point where we want
8235                  * to start our next measurement. We need
8236                  * to record the new gput_ts as here and
8237                  * possibly update the start sequence.
8238                  */
8239                 uint32_t seq, ts;
8240
8241                 if (rsm->r_rtr_cnt > 1) {
8242                         /*
8243                          * This is a retransmit, can we
8244                          * really make any assessment at this
8245                          * point?  We are not really sure of
8246                          * the timestamp, is it this or the
8247                          * previous transmission?
8248                          *
8249                          * Lets wait for something better that
8250                          * is not retransmitted.
8251                          */
8252                         return;
8253                 }
8254                 seq = tp->gput_seq;
8255                 ts = tp->gput_ts;
8256                 rack->app_limited_needs_set = 0;
8257                 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
8258                 /* Do we start at a new end? */
8259                 if ((use_which == RACK_USE_BEG) &&
8260                     SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
8261                         /*
8262                          * When we get an ACK that just eats
8263                          * up some of the rsm, we set RACK_USE_BEG
8264                          * since whats at r_start (i.e. th_ack)
8265                          * is left unacked and thats where the
8266                          * measurement not starts.
8267                          */
8268                         tp->gput_seq = rsm->r_start;
8269                         rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8270                 }
8271                 if ((use_which == RACK_USE_END) &&
8272                     SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
8273                             /*
8274                              * We use the end when the cumack
8275                              * is moving forward and completely
8276                              * deleting the rsm passed so basically
8277                              * r_end holds th_ack.
8278                              *
8279                              * For SACK's we also want to use the end
8280                              * since this piece just got sacked and
8281                              * we want to target anything after that
8282                              * in our measurement.
8283                              */
8284                             tp->gput_seq = rsm->r_end;
8285                             rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8286                 }
8287                 if (use_which == RACK_USE_END_OR_THACK) {
8288                         /*
8289                          * special case for ack moving forward,
8290                          * not a sack, we need to move all the
8291                          * way up to where this ack cum-ack moves
8292                          * to.
8293                          */
8294                         if (SEQ_GT(th_ack, rsm->r_end))
8295                                 tp->gput_seq = th_ack;
8296                         else
8297                                 tp->gput_seq = rsm->r_end;
8298                         rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8299                 }
8300                 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
8301                         /*
8302                          * We moved beyond this guy's range, re-calculate
8303                          * the new end point.
8304                          */
8305                         if (rack->rc_gp_filled == 0) {
8306                                 tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
8307                         } else {
8308                                 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
8309                         }
8310                 }
8311                 /*
8312                  * We are moving the goal post, we may be able to clear the
8313                  * measure_saw_probe_rtt flag.
8314                  */
8315                 if ((rack->in_probe_rtt == 0) &&
8316                     (rack->measure_saw_probe_rtt) &&
8317                     (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
8318                         rack->measure_saw_probe_rtt = 0;
8319                 rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
8320                                            seq, tp->gput_seq, 0, 5, line, NULL, 0);
8321                 if (rack->rc_gp_filled &&
8322                     ((tp->gput_ack - tp->gput_seq) <
8323                      max(rc_init_window(rack), (MIN_GP_WIN *
8324                                                 ctf_fixed_maxseg(tp))))) {
8325                         uint32_t ideal_amount;
8326
8327                         ideal_amount = rack_get_measure_window(tp, rack);
8328                         if (ideal_amount > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
8329                                 /*
8330                                  * There is no sense of continuing this measurement
8331                                  * because its too small to gain us anything we
8332                                  * trust. Skip it and that way we can start a new
8333                                  * measurement quicker.
8334                                  */
8335                                 tp->t_flags &= ~TF_GPUTINPROG;
8336                                 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
8337                                                            0, 0, 0, 6, __LINE__, NULL, 0);
8338                         } else {
8339                                 /*
8340                                  * Reset the window further out.
8341                                  */
8342                                 tp->gput_ack = tp->gput_seq + ideal_amount;
8343                         }
8344                 }
8345         }
8346 }
8347
8348 static inline int
8349 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
8350 {
8351         if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
8352                 /* Behind our TLP definition or right at */
8353                 return (0);
8354         }
8355         if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
8356                 /* The start is beyond or right at our end of TLP definition */
8357                 return (0);
8358         }
8359         /* It has to be a sub-part of the original TLP recorded */
8360         return (1);
8361 }
8362
8363
8364 static uint32_t
8365 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
8366                    struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, int *moved_two)
8367 {
8368         uint32_t start, end, changed = 0;
8369         struct rack_sendmap stack_map;
8370         struct rack_sendmap *rsm, *nrsm, fe, *insret, *prev, *next;
8371         int32_t used_ref = 1;
8372         int moved = 0;
8373
8374         start = sack->start;
8375         end = sack->end;
8376         rsm = *prsm;
8377         memset(&fe, 0, sizeof(fe));
8378 do_rest_ofb:
8379         if ((rsm == NULL) ||
8380             (SEQ_LT(end, rsm->r_start)) ||
8381             (SEQ_GEQ(start, rsm->r_end)) ||
8382             (SEQ_LT(start, rsm->r_start))) {
8383                 /*
8384                  * We are not in the right spot,
8385                  * find the correct spot in the tree.
8386                  */
8387                 used_ref = 0;
8388                 fe.r_start = start;
8389                 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
8390                 moved++;
8391         }
8392         if (rsm == NULL) {
8393                 /* TSNH */
8394                 goto out;
8395         }
8396         /* Ok we have an ACK for some piece of this rsm */
8397         if (rsm->r_start != start) {
8398                 if ((rsm->r_flags & RACK_ACKED) == 0) {
8399                         /*
8400                          * Before any splitting or hookery is
8401                          * done is it a TLP of interest i.e. rxt?
8402                          */
8403                         if ((rsm->r_flags & RACK_TLP) &&
8404                             (rsm->r_rtr_cnt > 1)) {
8405                                 /*
8406                                  * We are splitting a rxt TLP, check
8407                                  * if we need to save off the start/end
8408                                  */
8409                                 if (rack->rc_last_tlp_acked_set &&
8410                                     (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8411                                         /*
8412                                          * We already turned this on since we are inside
8413                                          * the previous one was a partially sack now we
8414                                          * are getting another one (maybe all of it).
8415                                          *
8416                                          */
8417                                         rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8418                                         /*
8419                                          * Lets make sure we have all of it though.
8420                                          */
8421                                         if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8422                                                 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8423                                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8424                                                                      rack->r_ctl.last_tlp_acked_end);
8425                                         }
8426                                         if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8427                                                 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8428                                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8429                                                                      rack->r_ctl.last_tlp_acked_end);
8430                                         }
8431                                 } else {
8432                                         rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8433                                         rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8434                                         rack->rc_last_tlp_past_cumack = 0;
8435                                         rack->rc_last_tlp_acked_set = 1;
8436                                         rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8437                                 }
8438                         }
8439                         /**
8440                          * Need to split this in two pieces the before and after,
8441                          * the before remains in the map, the after must be
8442                          * added. In other words we have:
8443                          * rsm        |--------------|
8444                          * sackblk        |------->
8445                          * rsm will become
8446                          *     rsm    |---|
8447                          * and nrsm will be  the sacked piece
8448                          *     nrsm       |----------|
8449                          *
8450                          * But before we start down that path lets
8451                          * see if the sack spans over on top of
8452                          * the next guy and it is already sacked.
8453                          *
8454                          */
8455                         next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8456                         if (next && (next->r_flags & RACK_ACKED) &&
8457                             SEQ_GEQ(end, next->r_start)) {
8458                                 /**
8459                                  * So the next one is already acked, and
8460                                  * we can thus by hookery use our stack_map
8461                                  * to reflect the piece being sacked and
8462                                  * then adjust the two tree entries moving
8463                                  * the start and ends around. So we start like:
8464                                  *  rsm     |------------|             (not-acked)
8465                                  *  next                 |-----------| (acked)
8466                                  *  sackblk        |-------->
8467                                  *  We want to end like so:
8468                                  *  rsm     |------|                   (not-acked)
8469                                  *  next           |-----------------| (acked)
8470                                  *  nrsm           |-----|
8471                                  * Where nrsm is a temporary stack piece we
8472                                  * use to update all the gizmos.
8473                                  */
8474                                 /* Copy up our fudge block */
8475                                 nrsm = &stack_map;
8476                                 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8477                                 /* Now adjust our tree blocks */
8478                                 rsm->r_end = start;
8479                                 next->r_start = start;
8480                                 /* Now we must adjust back where next->m is */
8481                                 rack_setup_offset_for_rsm(rsm, next);
8482
8483                                 /* We don't need to adjust rsm, it did not change */
8484                                 /* Clear out the dup ack count of the remainder */
8485                                 rsm->r_dupack = 0;
8486                                 rsm->r_just_ret = 0;
8487                                 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8488                                 /* Now lets make sure our fudge block is right */
8489                                 nrsm->r_start = start;
8490                                 /* Now lets update all the stats and such */
8491                                 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8492                                 if (rack->app_limited_needs_set)
8493                                         rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8494                                 changed += (nrsm->r_end - nrsm->r_start);
8495                                 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8496                                 if (nrsm->r_flags & RACK_SACK_PASSED) {
8497                                         counter_u64_add(rack_reorder_seen, 1);
8498                                         rack->r_ctl.rc_reorder_ts = cts;
8499                                 }
8500                                 /*
8501                                  * Now we want to go up from rsm (the
8502                                  * one left un-acked) to the next one
8503                                  * in the tmap. We do this so when
8504                                  * we walk backwards we include marking
8505                                  * sack-passed on rsm (The one passed in
8506                                  * is skipped since it is generally called
8507                                  * on something sacked before removing it
8508                                  * from the tmap).
8509                                  */
8510                                 if (rsm->r_in_tmap) {
8511                                         nrsm = TAILQ_NEXT(rsm, r_tnext);
8512                                         /*
8513                                          * Now that we have the next
8514                                          * one walk backwards from there.
8515                                          */
8516                                         if (nrsm && nrsm->r_in_tmap)
8517                                                 rack_log_sack_passed(tp, rack, nrsm);
8518                                 }
8519                                 /* Now are we done? */
8520                                 if (SEQ_LT(end, next->r_end) ||
8521                                     (end == next->r_end)) {
8522                                         /* Done with block */
8523                                         goto out;
8524                                 }
8525                                 rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
8526                                 counter_u64_add(rack_sack_used_next_merge, 1);
8527                                 /* Postion for the next block */
8528                                 start = next->r_end;
8529                                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, next);
8530                                 if (rsm == NULL)
8531                                         goto out;
8532                         } else {
8533                                 /**
8534                                  * We can't use any hookery here, so we
8535                                  * need to split the map. We enter like
8536                                  * so:
8537                                  *  rsm      |--------|
8538                                  *  sackblk       |----->
8539                                  * We will add the new block nrsm and
8540                                  * that will be the new portion, and then
8541                                  * fall through after reseting rsm. So we
8542                                  * split and look like this:
8543                                  *  rsm      |----|
8544                                  *  sackblk       |----->
8545                                  *  nrsm          |---|
8546                                  * We then fall through reseting
8547                                  * rsm to nrsm, so the next block
8548                                  * picks it up.
8549                                  */
8550                                 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8551                                 if (nrsm == NULL) {
8552                                         /*
8553                                          * failed XXXrrs what can we do but loose the sack
8554                                          * info?
8555                                          */
8556                                         goto out;
8557                                 }
8558                                 counter_u64_add(rack_sack_splits, 1);
8559                                 rack_clone_rsm(rack, nrsm, rsm, start);
8560                                 rsm->r_just_ret = 0;
8561                                 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8562 #ifdef INVARIANTS
8563                                 if (insret != NULL) {
8564                                         panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8565                                               nrsm, insret, rack, rsm);
8566                                 }
8567 #endif
8568                                 if (rsm->r_in_tmap) {
8569                                         TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8570                                         nrsm->r_in_tmap = 1;
8571                                 }
8572                                 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
8573                                 rsm->r_flags &= (~RACK_HAS_FIN);
8574                                 /* Position us to point to the new nrsm that starts the sack blk */
8575                                 rsm = nrsm;
8576                         }
8577                 } else {
8578                         /* Already sacked this piece */
8579                         counter_u64_add(rack_sack_skipped_acked, 1);
8580                         moved++;
8581                         if (end == rsm->r_end) {
8582                                 /* Done with block */
8583                                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8584                                 goto out;
8585                         } else if (SEQ_LT(end, rsm->r_end)) {
8586                                 /* A partial sack to a already sacked block */
8587                                 moved++;
8588                                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8589                                 goto out;
8590                         } else {
8591                                 /*
8592                                  * The end goes beyond this guy
8593                                  * repostion the start to the
8594                                  * next block.
8595                                  */
8596                                 start = rsm->r_end;
8597                                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8598                                 if (rsm == NULL)
8599                                         goto out;
8600                         }
8601                 }
8602         }
8603         if (SEQ_GEQ(end, rsm->r_end)) {
8604                 /**
8605                  * The end of this block is either beyond this guy or right
8606                  * at this guy. I.e.:
8607                  *  rsm ---                 |-----|
8608                  *  end                     |-----|
8609                  *  <or>
8610                  *  end                     |---------|
8611                  */
8612                 if ((rsm->r_flags & RACK_ACKED) == 0) {
8613                         /*
8614                          * Is it a TLP of interest?
8615                          */
8616                         if ((rsm->r_flags & RACK_TLP) &&
8617                             (rsm->r_rtr_cnt > 1)) {
8618                                 /*
8619                                  * We are splitting a rxt TLP, check
8620                                  * if we need to save off the start/end
8621                                  */
8622                                 if (rack->rc_last_tlp_acked_set &&
8623                                     (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8624                                         /*
8625                                          * We already turned this on since we are inside
8626                                          * the previous one was a partially sack now we
8627                                          * are getting another one (maybe all of it).
8628                                          */
8629                                         rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8630                                         /*
8631                                          * Lets make sure we have all of it though.
8632                                          */
8633                                         if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8634                                                 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8635                                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8636                                                                      rack->r_ctl.last_tlp_acked_end);
8637                                         }
8638                                         if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8639                                                 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8640                                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8641                                                                      rack->r_ctl.last_tlp_acked_end);
8642                                         }
8643                                 } else {
8644                                         rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8645                                         rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8646                                         rack->rc_last_tlp_past_cumack = 0;
8647                                         rack->rc_last_tlp_acked_set = 1;
8648                                         rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8649                                 }
8650                         }
8651                         rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8652                         changed += (rsm->r_end - rsm->r_start);
8653                         rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8654                         if (rsm->r_in_tmap) /* should be true */
8655                                 rack_log_sack_passed(tp, rack, rsm);
8656                         /* Is Reordering occuring? */
8657                         if (rsm->r_flags & RACK_SACK_PASSED) {
8658                                 rsm->r_flags &= ~RACK_SACK_PASSED;
8659                                 counter_u64_add(rack_reorder_seen, 1);
8660                                 rack->r_ctl.rc_reorder_ts = cts;
8661                         }
8662                         if (rack->app_limited_needs_set)
8663                                 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8664                         rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8665                         rsm->r_flags |= RACK_ACKED;
8666                         if (rsm->r_in_tmap) {
8667                                 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8668                                 rsm->r_in_tmap = 0;
8669                         }
8670                         rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
8671                 } else {
8672                         counter_u64_add(rack_sack_skipped_acked, 1);
8673                         moved++;
8674                 }
8675                 if (end == rsm->r_end) {
8676                         /* This block only - done, setup for next */
8677                         goto out;
8678                 }
8679                 /*
8680                  * There is more not coverend by this rsm move on
8681                  * to the next block in the RB tree.
8682                  */
8683                 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8684                 start = rsm->r_end;
8685                 rsm = nrsm;
8686                 if (rsm == NULL)
8687                         goto out;
8688                 goto do_rest_ofb;
8689         }
8690         /**
8691          * The end of this sack block is smaller than
8692          * our rsm i.e.:
8693          *  rsm ---                 |-----|
8694          *  end                     |--|
8695          */
8696         if ((rsm->r_flags & RACK_ACKED) == 0) {
8697                 /*
8698                  * Is it a TLP of interest?
8699                  */
8700                 if ((rsm->r_flags & RACK_TLP) &&
8701                     (rsm->r_rtr_cnt > 1)) {
8702                         /*
8703                          * We are splitting a rxt TLP, check
8704                          * if we need to save off the start/end
8705                          */
8706                         if (rack->rc_last_tlp_acked_set &&
8707                             (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8708                                 /*
8709                                  * We already turned this on since we are inside
8710                                  * the previous one was a partially sack now we
8711                                  * are getting another one (maybe all of it).
8712                                  */
8713                                 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8714                                 /*
8715                                  * Lets make sure we have all of it though.
8716                                  */
8717                                 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8718                                         rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8719                                         rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8720                                                              rack->r_ctl.last_tlp_acked_end);
8721                                 }
8722                                 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8723                                         rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8724                                         rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8725                                                              rack->r_ctl.last_tlp_acked_end);
8726                                 }
8727                         } else {
8728                                 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8729                                 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8730                                 rack->rc_last_tlp_past_cumack = 0;
8731                                 rack->rc_last_tlp_acked_set = 1;
8732                                 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8733                         }
8734                 }
8735                 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8736                 if (prev &&
8737                     (prev->r_flags & RACK_ACKED)) {
8738                         /**
8739                          * Goal, we want the right remainder of rsm to shrink
8740                          * in place and span from (rsm->r_start = end) to rsm->r_end.
8741                          * We want to expand prev to go all the way
8742                          * to prev->r_end <- end.
8743                          * so in the tree we have before:
8744                          *   prev     |--------|         (acked)
8745                          *   rsm               |-------| (non-acked)
8746                          *   sackblk           |-|
8747                          * We churn it so we end up with
8748                          *   prev     |----------|       (acked)
8749                          *   rsm                 |-----| (non-acked)
8750                          *   nrsm              |-| (temporary)
8751                          *
8752                          * Note if either prev/rsm is a TLP we don't
8753                          * do this.
8754                          */
8755                         nrsm = &stack_map;
8756                         memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8757                         prev->r_end = end;
8758                         rsm->r_start = end;
8759                         /* Now adjust nrsm (stack copy) to be
8760                          * the one that is the small
8761                          * piece that was "sacked".
8762                          */
8763                         nrsm->r_end = end;
8764                         rsm->r_dupack = 0;
8765                         rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8766                         /*
8767                          * Now that the rsm has had its start moved forward
8768                          * lets go ahead and get its new place in the world.
8769                          */
8770                         rack_setup_offset_for_rsm(prev, rsm);
8771                         /*
8772                          * Now nrsm is our new little piece
8773                          * that is acked (which was merged
8774                          * to prev). Update the rtt and changed
8775                          * based on that. Also check for reordering.
8776                          */
8777                         rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
8778                         if (rack->app_limited_needs_set)
8779                                 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
8780                         changed += (nrsm->r_end - nrsm->r_start);
8781                         rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
8782                         if (nrsm->r_flags & RACK_SACK_PASSED) {
8783                                 counter_u64_add(rack_reorder_seen, 1);
8784                                 rack->r_ctl.rc_reorder_ts = cts;
8785                         }
8786                         rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
8787                         rsm = prev;
8788                         counter_u64_add(rack_sack_used_prev_merge, 1);
8789                 } else {
8790                         /**
8791                          * This is the case where our previous
8792                          * block is not acked either, so we must
8793                          * split the block in two.
8794                          */
8795                         nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8796                         if (nrsm == NULL) {
8797                                 /* failed rrs what can we do but loose the sack info? */
8798                                 goto out;
8799                         }
8800                         if ((rsm->r_flags & RACK_TLP) &&
8801                             (rsm->r_rtr_cnt > 1)) {
8802                                 /*
8803                                  * We are splitting a rxt TLP, check
8804                                  * if we need to save off the start/end
8805                                  */
8806                                 if (rack->rc_last_tlp_acked_set &&
8807                                     (is_rsm_inside_declared_tlp_block(rack, rsm))) {
8808                                             /*
8809                                              * We already turned this on since this block is inside
8810                                              * the previous one was a partially sack now we
8811                                              * are getting another one (maybe all of it).
8812                                              */
8813                                             rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8814                                             /*
8815                                              * Lets make sure we have all of it though.
8816                                              */
8817                                             if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
8818                                                     rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8819                                                     rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8820                                                                          rack->r_ctl.last_tlp_acked_end);
8821                                             }
8822                                             if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
8823                                                     rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8824                                                     rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
8825                                                                          rack->r_ctl.last_tlp_acked_end);
8826                                             }
8827                                     } else {
8828                                             rack->r_ctl.last_tlp_acked_start = rsm->r_start;
8829                                             rack->r_ctl.last_tlp_acked_end = rsm->r_end;
8830                                             rack->rc_last_tlp_acked_set = 1;
8831                                             rack->rc_last_tlp_past_cumack = 0;
8832                                             rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
8833                                     }
8834                         }
8835                         /**
8836                          * In this case nrsm becomes
8837                          * nrsm->r_start = end;
8838                          * nrsm->r_end = rsm->r_end;
8839                          * which is un-acked.
8840                          * <and>
8841                          * rsm->r_end = nrsm->r_start;
8842                          * i.e. the remaining un-acked
8843                          * piece is left on the left
8844                          * hand side.
8845                          *
8846                          * So we start like this
8847                          * rsm      |----------| (not acked)
8848                          * sackblk  |---|
8849                          * build it so we have
8850                          * rsm      |---|         (acked)
8851                          * nrsm         |------|  (not acked)
8852                          */
8853                         counter_u64_add(rack_sack_splits, 1);
8854                         rack_clone_rsm(rack, nrsm, rsm, end);
8855                         rsm->r_flags &= (~RACK_HAS_FIN);
8856                         rsm->r_just_ret = 0;
8857                         insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8858 #ifdef INVARIANTS
8859                         if (insret != NULL) {
8860                                 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8861                                       nrsm, insret, rack, rsm);
8862                         }
8863 #endif
8864                         if (rsm->r_in_tmap) {
8865                                 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8866                                 nrsm->r_in_tmap = 1;
8867                         }
8868                         nrsm->r_dupack = 0;
8869                         rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8870                         rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
8871                         changed += (rsm->r_end - rsm->r_start);
8872                         rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
8873                         if (rsm->r_in_tmap) /* should be true */
8874                                 rack_log_sack_passed(tp, rack, rsm);
8875                         /* Is Reordering occuring? */
8876                         if (rsm->r_flags & RACK_SACK_PASSED) {
8877                                 rsm->r_flags &= ~RACK_SACK_PASSED;
8878                                 counter_u64_add(rack_reorder_seen, 1);
8879                                 rack->r_ctl.rc_reorder_ts = cts;
8880                         }
8881                         if (rack->app_limited_needs_set)
8882                                 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
8883                         rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
8884                         rsm->r_flags |= RACK_ACKED;
8885                         rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
8886                         if (rsm->r_in_tmap) {
8887                                 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8888                                 rsm->r_in_tmap = 0;
8889                         }
8890                 }
8891         } else if (start != end){
8892                 /*
8893                  * The block was already acked.
8894                  */
8895                 counter_u64_add(rack_sack_skipped_acked, 1);
8896                 moved++;
8897         }
8898 out:
8899         if (rsm &&
8900             ((rsm->r_flags & RACK_TLP) == 0) &&
8901             (rsm->r_flags & RACK_ACKED)) {
8902                 /*
8903                  * Now can we merge where we worked
8904                  * with either the previous or
8905                  * next block?
8906                  */
8907                 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8908                 while (next) {
8909                         if (next->r_flags & RACK_TLP)
8910                                 break;
8911                         if (next->r_flags & RACK_ACKED) {
8912                         /* yep this and next can be merged */
8913                                 rsm = rack_merge_rsm(rack, rsm, next);
8914                                 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8915                         } else
8916                                 break;
8917                 }
8918                 /* Now what about the previous? */
8919                 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8920                 while (prev) {
8921                         if (prev->r_flags & RACK_TLP)
8922                                 break;
8923                         if (prev->r_flags & RACK_ACKED) {
8924                                 /* yep the previous and this can be merged */
8925                                 rsm = rack_merge_rsm(rack, prev, rsm);
8926                                 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8927                         } else
8928                                 break;
8929                 }
8930         }
8931         if (used_ref == 0) {
8932                 counter_u64_add(rack_sack_proc_all, 1);
8933         } else {
8934                 counter_u64_add(rack_sack_proc_short, 1);
8935         }
8936         /* Save off the next one for quick reference. */
8937         if (rsm)
8938                 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8939         else
8940                 nrsm = NULL;
8941         *prsm = rack->r_ctl.rc_sacklast = nrsm;
8942         /* Pass back the moved. */
8943         *moved_two = moved;
8944         return (changed);
8945 }
8946
8947 static void inline
8948 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
8949 {
8950         struct rack_sendmap *tmap;
8951
8952         tmap = NULL;
8953         while (rsm && (rsm->r_flags & RACK_ACKED)) {
8954                 /* Its no longer sacked, mark it so */
8955                 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8956 #ifdef INVARIANTS
8957                 if (rsm->r_in_tmap) {
8958                         panic("rack:%p rsm:%p flags:0x%x in tmap?",
8959                               rack, rsm, rsm->r_flags);
8960                 }
8961 #endif
8962                 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
8963                 /* Rebuild it into our tmap */
8964                 if (tmap == NULL) {
8965                         TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8966                         tmap = rsm;
8967                 } else {
8968                         TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
8969                         tmap = rsm;
8970                 }
8971                 tmap->r_in_tmap = 1;
8972                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8973         }
8974         /*
8975          * Now lets possibly clear the sack filter so we start
8976          * recognizing sacks that cover this area.
8977          */
8978         sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
8979
8980 }
8981
8982 static void
8983 rack_do_decay(struct tcp_rack *rack)
8984 {
8985         struct timeval res;
8986
8987 #define timersub(tvp, uvp, vvp)                                         \
8988         do {                                                            \
8989                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
8990                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
8991                 if ((vvp)->tv_usec < 0) {                               \
8992                         (vvp)->tv_sec--;                                \
8993                         (vvp)->tv_usec += 1000000;                      \
8994                 }                                                       \
8995         } while (0)
8996
8997         timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
8998 #undef timersub
8999
9000         rack->r_ctl.input_pkt++;
9001         if ((rack->rc_in_persist) ||
9002             (res.tv_sec >= 1) ||
9003             (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
9004                 /*
9005                  * Check for decay of non-SAD,
9006                  * we want all SAD detection metrics to
9007                  * decay 1/4 per second (or more) passed.
9008                  */
9009                 uint32_t pkt_delta;
9010
9011                 pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
9012                 /* Update our saved tracking values */
9013                 rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
9014                 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
9015                 /* Now do we escape without decay? */
9016 #ifdef NETFLIX_EXP_DETECTION
9017                 if (rack->rc_in_persist ||
9018                     (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
9019                     (pkt_delta < tcp_sad_low_pps)){
9020                         /*
9021                          * We don't decay idle connections
9022                          * or ones that have a low input pps.
9023                          */
9024                         return;
9025                 }
9026                 /* Decay the counters */
9027                 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
9028                                                         tcp_sad_decay_val);
9029                 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
9030                                                          tcp_sad_decay_val);
9031                 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
9032                                                                tcp_sad_decay_val);
9033                 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
9034                                                                 tcp_sad_decay_val);
9035 #endif
9036         }
9037 }
9038
9039 static void
9040 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to)
9041 {
9042         struct rack_sendmap *rsm, *rm;
9043
9044         /*
9045          * The ACK point is advancing to th_ack, we must drop off
9046          * the packets in the rack log and calculate any eligble
9047          * RTT's.
9048          */
9049         rack->r_wanted_output = 1;
9050
9051         /* Tend any TLP that has been marked for 1/2 the seq space (its old)  */
9052         if ((rack->rc_last_tlp_acked_set == 1)&&
9053             (rack->rc_last_tlp_past_cumack == 1) &&
9054             (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
9055                 /*
9056                  * We have reached the point where our last rack
9057                  * tlp retransmit sequence is ahead of the cum-ack.
9058                  * This can only happen when the cum-ack moves all
9059                  * the way around (its been a full 2^^31+1 bytes
9060                  * or more since we sent a retransmitted TLP). Lets
9061                  * turn off the valid flag since its not really valid.
9062                  *
9063                  * Note since sack's also turn on this event we have
9064                  * a complication, we have to wait to age it out until
9065                  * the cum-ack is by the TLP before checking which is
9066                  * what the next else clause does.
9067                  */
9068                 rack_log_dsack_event(rack, 9, __LINE__,
9069                                      rack->r_ctl.last_tlp_acked_start,
9070                                      rack->r_ctl.last_tlp_acked_end);
9071                 rack->rc_last_tlp_acked_set = 0;
9072                 rack->rc_last_tlp_past_cumack = 0;
9073         } else if ((rack->rc_last_tlp_acked_set == 1) &&
9074                    (rack->rc_last_tlp_past_cumack == 0) &&
9075                    (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
9076                 /*
9077                  * It is safe to start aging TLP's out.
9078                  */
9079                 rack->rc_last_tlp_past_cumack = 1;
9080         }
9081         /* We do the same for the tlp send seq as well */
9082         if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
9083             (rack->rc_last_sent_tlp_past_cumack == 1) &&
9084             (SEQ_GT(rack->r_ctl.last_sent_tlp_seq,  th_ack))) {
9085                 rack_log_dsack_event(rack, 9, __LINE__,
9086                                      rack->r_ctl.last_sent_tlp_seq,
9087                                      (rack->r_ctl.last_sent_tlp_seq +
9088                                       rack->r_ctl.last_sent_tlp_len));
9089                 rack->rc_last_sent_tlp_seq_valid = 0;
9090                 rack->rc_last_sent_tlp_past_cumack = 0;
9091         } else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
9092                    (rack->rc_last_sent_tlp_past_cumack == 0) &&
9093                    (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
9094                 /*
9095                  * It is safe to start aging TLP's send.
9096                  */
9097                 rack->rc_last_sent_tlp_past_cumack = 1;
9098         }
9099 more:
9100         rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9101         if (rsm == NULL) {
9102                 if ((th_ack - 1) == tp->iss) {
9103                         /*
9104                          * For the SYN incoming case we will not
9105                          * have called tcp_output for the sending of
9106                          * the SYN, so there will be no map. All
9107                          * other cases should probably be a panic.
9108                          */
9109                         return;
9110                 }
9111                 if (tp->t_flags & TF_SENTFIN) {
9112                         /* if we sent a FIN we often will not have map */
9113                         return;
9114                 }
9115 #ifdef INVARIANTS
9116                 panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
9117                       tp,
9118                       tp->t_state, th_ack, rack,
9119                       tp->snd_una, tp->snd_max, tp->snd_nxt);
9120 #endif
9121                 return;
9122         }
9123         if (SEQ_LT(th_ack, rsm->r_start)) {
9124                 /* Huh map is missing this */
9125 #ifdef INVARIANTS
9126                 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
9127                        rsm->r_start,
9128                        th_ack, tp->t_state, rack->r_state);
9129 #endif
9130                 return;
9131         }
9132         rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
9133
9134         /* Now was it a retransmitted TLP? */
9135         if ((rsm->r_flags & RACK_TLP) &&
9136             (rsm->r_rtr_cnt > 1)) {
9137                 /*
9138                  * Yes, this rsm was a TLP and retransmitted, remember that
9139                  * since if a DSACK comes back on this we don't want
9140                  * to think of it as a reordered segment. This may
9141                  * get updated again with possibly even other TLPs
9142                  * in flight, but thats ok. Only when we don't send
9143                  * a retransmitted TLP for 1/2 the sequences space
9144                  * will it get turned off (above).
9145                  */
9146                 if (rack->rc_last_tlp_acked_set &&
9147                     (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9148                         /*
9149                          * We already turned this on since the end matches,
9150                          * the previous one was a partially ack now we
9151                          * are getting another one (maybe all of it).
9152                          */
9153                         rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9154                         /*
9155                          * Lets make sure we have all of it though.
9156                          */
9157                         if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9158                                 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9159                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9160                                                      rack->r_ctl.last_tlp_acked_end);
9161                         }
9162                         if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9163                                 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9164                                 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9165                                                      rack->r_ctl.last_tlp_acked_end);
9166                         }
9167                 } else {
9168                         rack->rc_last_tlp_past_cumack = 1;
9169                         rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9170                         rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9171                         rack->rc_last_tlp_acked_set = 1;
9172                         rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9173                 }
9174         }
9175         /* Now do we consume the whole thing? */
9176         if (SEQ_GEQ(th_ack, rsm->r_end)) {
9177                 /* Its all consumed. */
9178                 uint32_t left;
9179                 uint8_t newly_acked;
9180
9181                 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
9182                 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
9183                 rsm->r_rtr_bytes = 0;
9184                 /* Record the time of highest cumack sent */
9185                 rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9186                 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
9187 #ifdef INVARIANTS
9188                 if (rm != rsm) {
9189                         panic("removing head in rack:%p rsm:%p rm:%p",
9190                               rack, rsm, rm);
9191                 }
9192 #endif
9193                 if (rsm->r_in_tmap) {
9194                         TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9195                         rsm->r_in_tmap = 0;
9196                 }
9197                 newly_acked = 1;
9198                 if (rsm->r_flags & RACK_ACKED) {
9199                         /*
9200                          * It was acked on the scoreboard -- remove
9201                          * it from total
9202                          */
9203                         rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
9204                         newly_acked = 0;
9205                 } else if (rsm->r_flags & RACK_SACK_PASSED) {
9206                         /*
9207                          * There are segments ACKED on the
9208                          * scoreboard further up. We are seeing
9209                          * reordering.
9210                          */
9211                         rsm->r_flags &= ~RACK_SACK_PASSED;
9212                         counter_u64_add(rack_reorder_seen, 1);
9213                         rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9214                         rsm->r_flags |= RACK_ACKED;
9215                         rack->r_ctl.rc_reorder_ts = cts;
9216                         if (rack->r_ent_rec_ns) {
9217                                 /*
9218                                  * We have sent no more, and we saw an sack
9219                                  * then ack arrive.
9220                                  */
9221                                 rack->r_might_revert = 1;
9222                         }
9223                 }
9224                 if ((rsm->r_flags & RACK_TO_REXT) &&
9225                     (tp->t_flags & TF_RCVD_TSTMP) &&
9226                     (to->to_flags & TOF_TS) &&
9227                     (to->to_tsecr != 0) &&
9228                     (tp->t_flags & TF_PREVVALID)) {
9229                         /*
9230                          * We can use the timestamp to see
9231                          * if this retransmission was from the
9232                          * first transmit. If so we made a mistake.
9233                          */
9234                         tp->t_flags &= ~TF_PREVVALID;
9235                         if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
9236                                 /* The first transmit is what this ack is for */
9237                                 rack_cong_signal(tp, CC_RTO_ERR, th_ack);
9238                         }
9239                 }
9240                 left = th_ack - rsm->r_end;
9241                 if (rack->app_limited_needs_set && newly_acked)
9242                         rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
9243                 /* Free back to zone */
9244                 rack_free(rack, rsm);
9245                 if (left) {
9246                         goto more;
9247                 }
9248                 /* Check for reneging */
9249                 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9250                 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
9251                         /*
9252                          * The peer has moved snd_una up to
9253                          * the edge of this send, i.e. one
9254                          * that it had previously acked. The only
9255                          * way that can be true if the peer threw
9256                          * away data (space issues) that it had
9257                          * previously sacked (else it would have
9258                          * given us snd_una up to (rsm->r_end).
9259                          * We need to undo the acked markings here.
9260                          *
9261                          * Note we have to look to make sure th_ack is
9262                          * our rsm->r_start in case we get an old ack
9263                          * where th_ack is behind snd_una.
9264                          */
9265                         rack_peer_reneges(rack, rsm, th_ack);
9266                 }
9267                 return;
9268         }
9269         if (rsm->r_flags & RACK_ACKED) {
9270                 /*
9271                  * It was acked on the scoreboard -- remove it from
9272                  * total for the part being cum-acked.
9273                  */
9274                 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
9275         }
9276         /*
9277          * Clear the dup ack count for
9278          * the piece that remains.
9279          */
9280         rsm->r_dupack = 0;
9281         rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9282         if (rsm->r_rtr_bytes) {
9283                 /*
9284                  * It was retransmitted adjust the
9285                  * sack holes for what was acked.
9286                  */
9287                 int ack_am;
9288
9289                 ack_am = (th_ack - rsm->r_start);
9290                 if (ack_am >= rsm->r_rtr_bytes) {
9291                         rack->r_ctl.rc_holes_rxt -= ack_am;
9292                         rsm->r_rtr_bytes -= ack_am;
9293                 }
9294         }
9295         /*
9296          * Update where the piece starts and record
9297          * the time of send of highest cumack sent.
9298          */
9299         rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9300         rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
9301         /* Now we need to move our offset forward too */
9302         if (rsm->m && (rsm->orig_m_len != rsm->m->m_len)) {
9303                 /* Fix up the orig_m_len and possibly the mbuf offset */
9304                 rack_adjust_orig_mlen(rsm);
9305         }
9306         rsm->soff += (th_ack - rsm->r_start);
9307         rsm->r_start = th_ack;
9308         /* Now do we need to move the mbuf fwd too? */
9309         if (rsm->m) {
9310                 while (rsm->soff >= rsm->m->m_len) {
9311                         rsm->soff -= rsm->m->m_len;
9312                         rsm->m = rsm->m->m_next;
9313                         KASSERT((rsm->m != NULL),
9314                                 (" nrsm:%p hit at soff:%u null m",
9315                                  rsm, rsm->soff));
9316                 }
9317                 rsm->orig_m_len = rsm->m->m_len;
9318         }
9319         if (rack->app_limited_needs_set)
9320                 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
9321 }
9322
9323 static void
9324 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
9325 {
9326         struct rack_sendmap *rsm;
9327         int sack_pass_fnd = 0;
9328
9329         if (rack->r_might_revert) {
9330                 /*
9331                  * Ok we have reordering, have not sent anything, we
9332                  * might want to revert the congestion state if nothing
9333                  * further has SACK_PASSED on it. Lets check.
9334                  *
9335                  * We also get here when we have DSACKs come in for
9336                  * all the data that we FR'd. Note that a rxt or tlp
9337                  * timer clears this from happening.
9338                  */
9339
9340                 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
9341                         if (rsm->r_flags & RACK_SACK_PASSED) {
9342                                 sack_pass_fnd = 1;
9343                                 break;
9344                         }
9345                 }
9346                 if (sack_pass_fnd == 0) {
9347                         /*
9348                          * We went into recovery
9349                          * incorrectly due to reordering!
9350                          */
9351                         int orig_cwnd;
9352
9353                         rack->r_ent_rec_ns = 0;
9354                         orig_cwnd = tp->snd_cwnd;
9355                         tp->snd_cwnd = rack->r_ctl.rc_cwnd_at_erec;
9356                         tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
9357                         tp->snd_recover = tp->snd_una;
9358                         rack_log_to_prr(rack, 14, orig_cwnd);
9359                         EXIT_RECOVERY(tp->t_flags);
9360                 }
9361                 rack->r_might_revert = 0;
9362         }
9363 }
9364
9365 #ifdef NETFLIX_EXP_DETECTION
9366 static void
9367 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t bytes_this_ack, uint32_t segsiz)
9368 {
9369         if ((rack->do_detection || tcp_force_detection) &&
9370             tcp_sack_to_ack_thresh &&
9371             tcp_sack_to_move_thresh &&
9372             ((rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum) || rack->sack_attack_disable)) {
9373                 /*
9374                  * We have thresholds set to find
9375                  * possible attackers and disable sack.
9376                  * Check them.
9377                  */
9378                 uint64_t ackratio, moveratio, movetotal;
9379
9380                 /* Log detecting */
9381                 rack_log_sad(rack, 1);
9382                 ackratio = (uint64_t)(rack->r_ctl.sack_count);
9383                 ackratio *= (uint64_t)(1000);
9384                 if (rack->r_ctl.ack_count)
9385                         ackratio /= (uint64_t)(rack->r_ctl.ack_count);
9386                 else {
9387                         /* We really should not hit here */
9388                         ackratio = 1000;
9389                 }
9390                 if ((rack->sack_attack_disable == 0) &&
9391                     (ackratio > rack_highest_sack_thresh_seen))
9392                         rack_highest_sack_thresh_seen = (uint32_t)ackratio;
9393                 movetotal = rack->r_ctl.sack_moved_extra;
9394                 movetotal += rack->r_ctl.sack_noextra_move;
9395                 moveratio = rack->r_ctl.sack_moved_extra;
9396                 moveratio *= (uint64_t)1000;
9397                 if (movetotal)
9398                         moveratio /= movetotal;
9399                 else {
9400                         /* No moves, thats pretty good */
9401                         moveratio = 0;
9402                 }
9403                 if ((rack->sack_attack_disable == 0) &&
9404                     (moveratio > rack_highest_move_thresh_seen))
9405                         rack_highest_move_thresh_seen = (uint32_t)moveratio;
9406                 if (rack->sack_attack_disable == 0) {
9407                         if ((ackratio > tcp_sack_to_ack_thresh) &&
9408                             (moveratio > tcp_sack_to_move_thresh)) {
9409                                 /* Disable sack processing */
9410                                 rack->sack_attack_disable = 1;
9411                                 if (rack->r_rep_attack == 0) {
9412                                         rack->r_rep_attack = 1;
9413                                         counter_u64_add(rack_sack_attacks_detected, 1);
9414                                 }
9415                                 if (tcp_attack_on_turns_on_logging) {
9416                                         /*
9417                                          * Turn on logging, used for debugging
9418                                          * false positives.
9419                                          */
9420                                         rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging;
9421                                 }
9422                                 /* Clamp the cwnd at flight size */
9423                                 rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
9424                                 rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
9425                                 rack_log_sad(rack, 2);
9426                         }
9427                 } else {
9428                         /* We are sack-disabled check for false positives */
9429                         if ((ackratio <= tcp_restoral_thresh) ||
9430                             (rack->r_ctl.rc_num_maps_alloced  < tcp_map_minimum)) {
9431                                 rack->sack_attack_disable = 0;
9432                                 rack_log_sad(rack, 3);
9433                                 /* Restart counting */
9434                                 rack->r_ctl.sack_count = 0;
9435                                 rack->r_ctl.sack_moved_extra = 0;
9436                                 rack->r_ctl.sack_noextra_move = 1;
9437                                 rack->r_ctl.ack_count = max(1,
9438                                       (bytes_this_ack / segsiz));
9439
9440                                 if (rack->r_rep_reverse == 0) {
9441                                         rack->r_rep_reverse = 1;
9442                                         counter_u64_add(rack_sack_attacks_reversed, 1);
9443                                 }
9444                                 /* Restore the cwnd */
9445                                 if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
9446                                         rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
9447                         }
9448                 }
9449         }
9450 }
9451 #endif
9452
9453 static int
9454 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
9455 {
9456
9457         uint32_t am, l_end;
9458         int was_tlp = 0;
9459
9460         if (SEQ_GT(end, start))
9461                 am = end - start;
9462         else
9463                 am = 0;
9464         if ((rack->rc_last_tlp_acked_set ) &&
9465             (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
9466             (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
9467                 /*
9468                  * The DSACK is because of a TLP which we don't
9469                  * do anything with the reordering window over since
9470                  * it was not reordering that caused the DSACK but
9471                  * our previous retransmit TLP.
9472                  */
9473                 rack_log_dsack_event(rack, 7, __LINE__, start, end);
9474                 was_tlp = 1;
9475                 goto skip_dsack_round;
9476         }
9477         if (rack->rc_last_sent_tlp_seq_valid) {
9478                 l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
9479                 if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
9480                     (SEQ_LEQ(end, l_end))) {
9481                         /*
9482                          * This dsack is from the last sent TLP, ignore it
9483                          * for reordering purposes.
9484                          */
9485                         rack_log_dsack_event(rack, 7, __LINE__, start, end);
9486                         was_tlp = 1;
9487                         goto skip_dsack_round;
9488                 }
9489         }
9490         if (rack->rc_dsack_round_seen == 0) {
9491                 rack->rc_dsack_round_seen = 1;
9492                 rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
9493                 rack->r_ctl.num_dsack++;
9494                 rack->r_ctl.dsack_persist = 16; /* 16 is from the standard */
9495                 rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
9496         }
9497 skip_dsack_round:
9498         /*
9499          * We keep track of how many DSACK blocks we get
9500          * after a recovery incident.
9501          */
9502         rack->r_ctl.dsack_byte_cnt += am;
9503         if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
9504             rack->r_ctl.retran_during_recovery &&
9505             (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
9506                 /*
9507                  * False recovery most likely culprit is reordering. If
9508                  * nothing else is missing we need to revert.
9509                  */
9510                 rack->r_might_revert = 1;
9511                 rack_handle_might_revert(rack->rc_tp, rack);
9512                 rack->r_might_revert = 0;
9513                 rack->r_ctl.retran_during_recovery = 0;
9514                 rack->r_ctl.dsack_byte_cnt = 0;
9515         }
9516         return (was_tlp);
9517 }
9518
9519 static void
9520 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
9521 {
9522         /* Deal with changed and PRR here (in recovery only) */
9523         uint32_t pipe, snd_una;
9524
9525         rack->r_ctl.rc_prr_delivered += changed;
9526
9527         if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
9528                 /*
9529                  * It is all outstanding, we are application limited
9530                  * and thus we don't need more room to send anything.
9531                  * Note we use tp->snd_una here and not th_ack because
9532                  * the data as yet not been cut from the sb.
9533                  */
9534                 rack->r_ctl.rc_prr_sndcnt = 0;
9535                 return;
9536         }
9537         /* Compute prr_sndcnt */
9538         if (SEQ_GT(tp->snd_una, th_ack)) {
9539                 snd_una = tp->snd_una;
9540         } else {
9541                 snd_una = th_ack;
9542         }
9543         pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
9544         if (pipe > tp->snd_ssthresh) {
9545                 long sndcnt;
9546
9547                 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
9548                 if (rack->r_ctl.rc_prr_recovery_fs > 0)
9549                         sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
9550                 else {
9551                         rack->r_ctl.rc_prr_sndcnt = 0;
9552                         rack_log_to_prr(rack, 9, 0);
9553                         sndcnt = 0;
9554                 }
9555                 sndcnt++;
9556                 if (sndcnt > (long)rack->r_ctl.rc_prr_out)
9557                         sndcnt -= rack->r_ctl.rc_prr_out;
9558                 else
9559                         sndcnt = 0;
9560                 rack->r_ctl.rc_prr_sndcnt = sndcnt;
9561                 rack_log_to_prr(rack, 10, 0);
9562         } else {
9563                 uint32_t limit;
9564
9565                 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
9566                         limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
9567                 else
9568                         limit = 0;
9569                 if (changed > limit)
9570                         limit = changed;
9571                 limit += ctf_fixed_maxseg(tp);
9572                 if (tp->snd_ssthresh > pipe) {
9573                         rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
9574                         rack_log_to_prr(rack, 11, 0);
9575                 } else {
9576                         rack->r_ctl.rc_prr_sndcnt = min(0, limit);
9577                         rack_log_to_prr(rack, 12, 0);
9578                 }
9579         }
9580 }
9581
9582 static void
9583 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck)
9584 {
9585         uint32_t changed;
9586         struct tcp_rack *rack;
9587         struct rack_sendmap *rsm;
9588         struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
9589         register uint32_t th_ack;
9590         int32_t i, j, k, num_sack_blks = 0;
9591         uint32_t cts, acked, ack_point, sack_changed = 0;
9592         int loop_start = 0, moved_two = 0;
9593         uint32_t tsused;
9594
9595
9596         INP_WLOCK_ASSERT(tp->t_inpcb);
9597         if (th->th_flags & TH_RST) {
9598                 /* We don't log resets */
9599                 return;
9600         }
9601         rack = (struct tcp_rack *)tp->t_fb_ptr;
9602         cts = tcp_get_usecs(NULL);
9603         rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9604         changed = 0;
9605         th_ack = th->th_ack;
9606         if (rack->sack_attack_disable == 0)
9607                 rack_do_decay(rack);
9608         if (BYTES_THIS_ACK(tp, th) >= ctf_fixed_maxseg(rack->rc_tp)) {
9609                 /*
9610                  * You only get credit for
9611                  * MSS and greater (and you get extra
9612                  * credit for larger cum-ack moves).
9613                  */
9614                 int ac;
9615
9616                 ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
9617                 rack->r_ctl.ack_count += ac;
9618                 counter_u64_add(rack_ack_total, ac);
9619         }
9620         if (rack->r_ctl.ack_count > 0xfff00000) {
9621                 /*
9622                  * reduce the number to keep us under
9623                  * a uint32_t.
9624                  */
9625                 rack->r_ctl.ack_count /= 2;
9626                 rack->r_ctl.sack_count /= 2;
9627         }
9628         if (SEQ_GT(th_ack, tp->snd_una)) {
9629                 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
9630                 tp->t_acktime = ticks;
9631         }
9632         if (rsm && SEQ_GT(th_ack, rsm->r_start))
9633                 changed = th_ack - rsm->r_start;
9634         if (changed) {
9635                 rack_process_to_cumack(tp, rack, th_ack, cts, to);
9636         }
9637         if ((to->to_flags & TOF_SACK) == 0) {
9638                 /* We are done nothing left and no sack. */
9639                 rack_handle_might_revert(tp, rack);
9640                 /*
9641                  * For cases where we struck a dup-ack
9642                  * with no SACK, add to the changes so
9643                  * PRR will work right.
9644                  */
9645                 if (dup_ack_struck && (changed == 0)) {
9646                         changed += ctf_fixed_maxseg(rack->rc_tp);
9647                 }
9648                 goto out;
9649         }
9650         /* Sack block processing */
9651         if (SEQ_GT(th_ack, tp->snd_una))
9652                 ack_point = th_ack;
9653         else
9654                 ack_point = tp->snd_una;
9655         for (i = 0; i < to->to_nsacks; i++) {
9656                 bcopy((to->to_sacks + i * TCPOLEN_SACK),
9657                       &sack, sizeof(sack));
9658                 sack.start = ntohl(sack.start);
9659                 sack.end = ntohl(sack.end);
9660                 if (SEQ_GT(sack.end, sack.start) &&
9661                     SEQ_GT(sack.start, ack_point) &&
9662                     SEQ_LT(sack.start, tp->snd_max) &&
9663                     SEQ_GT(sack.end, ack_point) &&
9664                     SEQ_LEQ(sack.end, tp->snd_max)) {
9665                         sack_blocks[num_sack_blks] = sack;
9666                         num_sack_blks++;
9667                 } else if (SEQ_LEQ(sack.start, th_ack) &&
9668                            SEQ_LEQ(sack.end, th_ack)) {
9669                         int was_tlp;
9670
9671                         was_tlp = rack_note_dsack(rack, sack.start, sack.end);
9672                         /*
9673                          * Its a D-SACK block.
9674                          */
9675                         tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
9676                 }
9677         }
9678         if (rack->rc_dsack_round_seen) {
9679                 /* Is the dsack roound over? */
9680                 if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
9681                         /* Yes it is */
9682                         rack->rc_dsack_round_seen = 0;
9683                         rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
9684                 }
9685         }
9686         /*
9687          * Sort the SACK blocks so we can update the rack scoreboard with
9688          * just one pass.
9689          */
9690         num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
9691                                          num_sack_blks, th->th_ack);
9692         ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
9693         if (num_sack_blks == 0) {
9694                 /* Nothing to sack (DSACKs?) */
9695                 goto out_with_totals;
9696         }
9697         if (num_sack_blks < 2) {
9698                 /* Only one, we don't need to sort */
9699                 goto do_sack_work;
9700         }
9701         /* Sort the sacks */
9702         for (i = 0; i < num_sack_blks; i++) {
9703                 for (j = i + 1; j < num_sack_blks; j++) {
9704                         if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
9705                                 sack = sack_blocks[i];
9706                                 sack_blocks[i] = sack_blocks[j];
9707                                 sack_blocks[j] = sack;
9708                         }
9709                 }
9710         }
9711         /*
9712          * Now are any of the sack block ends the same (yes some
9713          * implementations send these)?
9714          */
9715 again:
9716         if (num_sack_blks == 0)
9717                 goto out_with_totals;
9718         if (num_sack_blks > 1) {
9719                 for (i = 0; i < num_sack_blks; i++) {
9720                         for (j = i + 1; j < num_sack_blks; j++) {
9721                                 if (sack_blocks[i].end == sack_blocks[j].end) {
9722                                         /*
9723                                          * Ok these two have the same end we
9724                                          * want the smallest end and then
9725                                          * throw away the larger and start
9726                                          * again.
9727                                          */
9728                                         if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
9729                                                 /*
9730                                                  * The second block covers
9731                                                  * more area use that
9732                                                  */
9733                                                 sack_blocks[i].start = sack_blocks[j].start;
9734                                         }
9735                                         /*
9736                                          * Now collapse out the dup-sack and
9737                                          * lower the count
9738                                          */
9739                                         for (k = (j + 1); k < num_sack_blks; k++) {
9740                                                 sack_blocks[j].start = sack_blocks[k].start;
9741                                                 sack_blocks[j].end = sack_blocks[k].end;
9742                                                 j++;
9743                                         }
9744                                         num_sack_blks--;
9745                                         goto again;
9746                                 }
9747                         }
9748                 }
9749         }
9750 do_sack_work:
9751         /*
9752          * First lets look to see if
9753          * we have retransmitted and
9754          * can use the transmit next?
9755          */
9756         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9757         if (rsm &&
9758             SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
9759             SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
9760                 /*
9761                  * We probably did the FR and the next
9762                  * SACK in continues as we would expect.
9763                  */
9764                 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two);
9765                 if (acked) {
9766                         rack->r_wanted_output = 1;
9767                         changed += acked;
9768                         sack_changed += acked;
9769                 }
9770                 if (num_sack_blks == 1) {
9771                         /*
9772                          * This is what we would expect from
9773                          * a normal implementation to happen
9774                          * after we have retransmitted the FR,
9775                          * i.e the sack-filter pushes down
9776                          * to 1 block and the next to be retransmitted
9777                          * is the sequence in the sack block (has more
9778                          * are acked). Count this as ACK'd data to boost
9779                          * up the chances of recovering any false positives.
9780                          */
9781                         rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
9782                         counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
9783                         counter_u64_add(rack_express_sack, 1);
9784                         if (rack->r_ctl.ack_count > 0xfff00000) {
9785                                 /*
9786                                  * reduce the number to keep us under
9787                                  * a uint32_t.
9788                                  */
9789                                 rack->r_ctl.ack_count /= 2;
9790                                 rack->r_ctl.sack_count /= 2;
9791                         }
9792                         goto out_with_totals;
9793                 } else {
9794                         /*
9795                          * Start the loop through the
9796                          * rest of blocks, past the first block.
9797                          */
9798                         moved_two = 0;
9799                         loop_start = 1;
9800                 }
9801         }
9802         /* Its a sack of some sort */
9803         rack->r_ctl.sack_count++;
9804         if (rack->r_ctl.sack_count > 0xfff00000) {
9805                 /*
9806                  * reduce the number to keep us under
9807                  * a uint32_t.
9808                  */
9809                 rack->r_ctl.ack_count /= 2;
9810                 rack->r_ctl.sack_count /= 2;
9811         }
9812         counter_u64_add(rack_sack_total, 1);
9813         if (rack->sack_attack_disable) {
9814                 /* An attacker disablement is in place */
9815                 if (num_sack_blks > 1) {
9816                         rack->r_ctl.sack_count += (num_sack_blks - 1);
9817                         rack->r_ctl.sack_moved_extra++;
9818                         counter_u64_add(rack_move_some, 1);
9819                         if (rack->r_ctl.sack_moved_extra > 0xfff00000) {
9820                                 rack->r_ctl.sack_moved_extra /= 2;
9821                                 rack->r_ctl.sack_noextra_move /= 2;
9822                         }
9823                 }
9824                 goto out;
9825         }
9826         rsm = rack->r_ctl.rc_sacklast;
9827         for (i = loop_start; i < num_sack_blks; i++) {
9828                 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &moved_two);
9829                 if (acked) {
9830                         rack->r_wanted_output = 1;
9831                         changed += acked;
9832                         sack_changed += acked;
9833                 }
9834                 if (moved_two) {
9835                         /*
9836                          * If we did not get a SACK for at least a MSS and
9837                          * had to move at all, or if we moved more than our
9838                          * threshold, it counts against the "extra" move.
9839                          */
9840                         rack->r_ctl.sack_moved_extra += moved_two;
9841                         counter_u64_add(rack_move_some, 1);
9842                 } else {
9843                         /*
9844                          * else we did not have to move
9845                          * any more than we would expect.
9846                          */
9847                         rack->r_ctl.sack_noextra_move++;
9848                         counter_u64_add(rack_move_none, 1);
9849                 }
9850                 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
9851                         /*
9852                          * If the SACK was not a full MSS then
9853                          * we add to sack_count the number of
9854                          * MSS's (or possibly more than
9855                          * a MSS if its a TSO send) we had to skip by.
9856                          */
9857                         rack->r_ctl.sack_count += moved_two;
9858                         counter_u64_add(rack_sack_total, moved_two);
9859                 }
9860                 /*
9861                  * Now we need to setup for the next
9862                  * round. First we make sure we won't
9863                  * exceed the size of our uint32_t on
9864                  * the various counts, and then clear out
9865                  * moved_two.
9866                  */
9867                 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
9868                     (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
9869                         rack->r_ctl.sack_moved_extra /= 2;
9870                         rack->r_ctl.sack_noextra_move /= 2;
9871                 }
9872                 if (rack->r_ctl.sack_count > 0xfff00000) {
9873                         rack->r_ctl.ack_count /= 2;
9874                         rack->r_ctl.sack_count /= 2;
9875                 }
9876                 moved_two = 0;
9877         }
9878 out_with_totals:
9879         if (num_sack_blks > 1) {
9880                 /*
9881                  * You get an extra stroke if
9882                  * you have more than one sack-blk, this
9883                  * could be where we are skipping forward
9884                  * and the sack-filter is still working, or
9885                  * it could be an attacker constantly
9886                  * moving us.
9887                  */
9888                 rack->r_ctl.sack_moved_extra++;
9889                 counter_u64_add(rack_move_some, 1);
9890         }
9891 out:
9892 #ifdef NETFLIX_EXP_DETECTION
9893         rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
9894 #endif
9895         if (changed) {
9896                 /* Something changed cancel the rack timer */
9897                 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9898         }
9899         tsused = tcp_get_usecs(NULL);
9900         rsm = tcp_rack_output(tp, rack, tsused);
9901         if ((!IN_FASTRECOVERY(tp->t_flags)) &&
9902             rsm) {
9903                 /* Enter recovery */
9904                 rack->r_ctl.rc_rsm_start = rsm->r_start;
9905                 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
9906                 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
9907                 entered_recovery = 1;
9908                 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
9909                 /*
9910                  * When we enter recovery we need to assure we send
9911                  * one packet.
9912                  */
9913                 if (rack->rack_no_prr == 0) {
9914                         rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
9915                         rack_log_to_prr(rack, 8, 0);
9916                 }
9917                 rack->r_timer_override = 1;
9918                 rack->r_early = 0;
9919                 rack->r_ctl.rc_agg_early = 0;
9920         } else if (IN_FASTRECOVERY(tp->t_flags) &&
9921                    rsm &&
9922                    (rack->r_rr_config == 3)) {
9923                 /*
9924                  * Assure we can output and we get no
9925                  * remembered pace time except the retransmit.
9926                  */
9927                 rack->r_timer_override = 1;
9928                 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
9929                 rack->r_ctl.rc_resend = rsm;
9930         }
9931         if (IN_FASTRECOVERY(tp->t_flags) &&
9932             (rack->rack_no_prr == 0) &&
9933             (entered_recovery == 0)) {
9934                 rack_update_prr(tp, rack, changed, th_ack);
9935                 if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
9936                      ((rack->rc_inp->inp_in_hpts == 0) &&
9937                       ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
9938                         /*
9939                          * If you are pacing output you don't want
9940                          * to override.
9941                          */
9942                         rack->r_early = 0;
9943                         rack->r_ctl.rc_agg_early = 0;
9944                         rack->r_timer_override = 1;
9945                 }
9946         }
9947 }
9948
9949 static void
9950 rack_strike_dupack(struct tcp_rack *rack)
9951 {
9952         struct rack_sendmap *rsm;
9953
9954         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9955         while (rsm && (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
9956                 rsm = TAILQ_NEXT(rsm, r_tnext);
9957         }
9958         if (rsm && (rsm->r_dupack < 0xff)) {
9959                 rsm->r_dupack++;
9960                 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
9961                         struct timeval tv;
9962                         uint32_t cts;
9963                         /*
9964                          * Here we see if we need to retransmit. For
9965                          * a SACK type connection if enough time has passed
9966                          * we will get a return of the rsm. For a non-sack
9967                          * connection we will get the rsm returned if the
9968                          * dupack value is 3 or more.
9969                          */
9970                         cts = tcp_get_usecs(&tv);
9971                         rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
9972                         if (rack->r_ctl.rc_resend != NULL) {
9973                                 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
9974                                         rack_cong_signal(rack->rc_tp, CC_NDUPACK,
9975                                                          rack->rc_tp->snd_una);
9976                                 }
9977                                 rack->r_wanted_output = 1;
9978                                 rack->r_timer_override = 1;
9979                                 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
9980                         }
9981                 } else {
9982                         rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
9983                 }
9984         }
9985 }
9986
9987 static void
9988 rack_check_bottom_drag(struct tcpcb *tp,
9989                        struct tcp_rack *rack,
9990                        struct socket *so, int32_t acked)
9991 {
9992         uint32_t segsiz, minseg;
9993
9994         segsiz = ctf_fixed_maxseg(tp);
9995         minseg = segsiz;
9996
9997         if (tp->snd_max == tp->snd_una) {
9998                 /*
9999                  * We are doing dynamic pacing and we are way
10000                  * under. Basically everything got acked while
10001                  * we were still waiting on the pacer to expire.
10002                  *
10003                  * This means we need to boost the b/w in
10004                  * addition to any earlier boosting of
10005                  * the multipler.
10006                  */
10007                 rack->rc_dragged_bottom = 1;
10008                 rack_validate_multipliers_at_or_above100(rack);
10009                 /*
10010                  * Lets use the segment bytes acked plus
10011                  * the lowest RTT seen as the basis to
10012                  * form a b/w estimate. This will be off
10013                  * due to the fact that the true estimate
10014                  * should be around 1/2 the time of the RTT
10015                  * but we can settle for that.
10016                  */
10017                 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
10018                     acked) {
10019                         uint64_t bw, calc_bw, rtt;
10020
10021                         rtt = rack->r_ctl.rack_rs.rs_us_rtt;
10022                         if (rtt == 0) {
10023                                 /* no us sample is there a ms one? */
10024                                 if (rack->r_ctl.rack_rs.rs_rtt_lowest) {
10025                                         rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
10026                                 } else {
10027                                         goto no_measurement;
10028                                 }
10029                         }
10030                         bw = acked;
10031                         calc_bw = bw * 1000000;
10032                         calc_bw /= rtt;
10033                         if (rack->r_ctl.last_max_bw &&
10034                             (rack->r_ctl.last_max_bw < calc_bw)) {
10035                                 /*
10036                                  * If we have a last calculated max bw
10037                                  * enforce it.
10038                                  */
10039                                 calc_bw = rack->r_ctl.last_max_bw;
10040                         }
10041                         /* now plop it in */
10042                         if (rack->rc_gp_filled == 0) {
10043                                 if (calc_bw > ONE_POINT_TWO_MEG) {
10044                                         /*
10045                                          * If we have no measurement
10046                                          * don't let us set in more than
10047                                          * 1.2Mbps. If we are still too
10048                                          * low after pacing with this we
10049                                          * will hopefully have a max b/w
10050                                          * available to sanity check things.
10051                                          */
10052                                         calc_bw = ONE_POINT_TWO_MEG;
10053                                 }
10054                                 rack->r_ctl.rc_rtt_diff = 0;
10055                                 rack->r_ctl.gp_bw = calc_bw;
10056                                 rack->rc_gp_filled = 1;
10057                                 if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
10058                                         rack->r_ctl.num_measurements = RACK_REQ_AVG;
10059                                 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
10060                         } else if (calc_bw > rack->r_ctl.gp_bw) {
10061                                 rack->r_ctl.rc_rtt_diff = 0;
10062                                 if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
10063                                         rack->r_ctl.num_measurements = RACK_REQ_AVG;
10064                                 rack->r_ctl.gp_bw = calc_bw;
10065                                 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
10066                         } else
10067                                 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10068                         if ((rack->gp_ready == 0) &&
10069                             (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
10070                                 /* We have enough measurements now */
10071                                 rack->gp_ready = 1;
10072                                 rack_set_cc_pacing(rack);
10073                                 if (rack->defer_options)
10074                                         rack_apply_deferred_options(rack);
10075                         }
10076                         /*
10077                          * For acks over 1mss we do a extra boost to simulate
10078                          * where we would get 2 acks (we want 110 for the mul).
10079                          */
10080                         if (acked > segsiz)
10081                                 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10082                 } else {
10083                         /*
10084                          * zero rtt possibly?, settle for just an old increase.
10085                          */
10086 no_measurement:
10087                         rack_increase_bw_mul(rack, -1, 0, 0, 1);
10088                 }
10089         } else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
10090                    (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
10091                                                minseg)) &&
10092                    (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
10093                    (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
10094                    (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
10095                     (segsiz * rack_req_segs))) {
10096                 /*
10097                  * We are doing dynamic GP pacing and
10098                  * we have everything except 1MSS or less
10099                  * bytes left out. We are still pacing away.
10100                  * And there is data that could be sent, This
10101                  * means we are inserting delayed ack time in
10102                  * our measurements because we are pacing too slow.
10103                  */
10104                 rack_validate_multipliers_at_or_above100(rack);
10105                 rack->rc_dragged_bottom = 1;
10106                 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10107         }
10108 }
10109
10110
10111
10112 static void
10113 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
10114 {
10115         /*
10116          * The fast output path is enabled and we
10117          * have moved the cumack forward. Lets see if
10118          * we can expand forward the fast path length by
10119          * that amount. What we would ideally like to
10120          * do is increase the number of bytes in the
10121          * fast path block (left_to_send) by the
10122          * acked amount. However we have to gate that
10123          * by two factors:
10124          * 1) The amount outstanding and the rwnd of the peer
10125          *    (i.e. we don't want to exceed the rwnd of the peer).
10126          *    <and>
10127          * 2) The amount of data left in the socket buffer (i.e.
10128          *    we can't send beyond what is in the buffer).
10129          *
10130          * Note that this does not take into account any increase
10131          * in the cwnd. We will only extend the fast path by
10132          * what was acked.
10133          */
10134         uint32_t new_total, gating_val;
10135
10136         new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
10137         gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
10138                          (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
10139         if (new_total <= gating_val) {
10140                 /* We can increase left_to_send by the acked amount */
10141                 counter_u64_add(rack_extended_rfo, 1);
10142                 rack->r_ctl.fsb.left_to_send = new_total;
10143                 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
10144                         ("rack:%p left_to_send:%u sbavail:%u out:%u",
10145                          rack, rack->r_ctl.fsb.left_to_send,
10146                          sbavail(&rack->rc_inp->inp_socket->so_snd),
10147                          (tp->snd_max - tp->snd_una)));
10148
10149         }
10150 }
10151
10152 static void
10153 rack_adjust_sendmap(struct tcp_rack *rack, struct sockbuf *sb, tcp_seq snd_una)
10154 {
10155         /*
10156          * Here any sendmap entry that points to the
10157          * beginning mbuf must be adjusted to the correct
10158          * offset. This must be called with:
10159          * 1) The socket buffer locked
10160          * 2) snd_una adjusted to its new postion.
10161          *
10162          * Note that (2) implies rack_ack_received has also
10163          * been called.
10164          *
10165          * We grab the first mbuf in the socket buffer and
10166          * then go through the front of the sendmap, recalculating
10167          * the stored offset for any sendmap entry that has
10168          * that mbuf. We must use the sb functions to do this
10169          * since its possible an add was done has well as
10170          * the subtraction we may have just completed. This should
10171          * not be a penalty though, since we just referenced the sb
10172          * to go in and trim off the mbufs that we freed (of course
10173          * there will be a penalty for the sendmap references though).
10174          */
10175         struct mbuf *m;
10176         struct rack_sendmap *rsm;
10177
10178         SOCKBUF_LOCK_ASSERT(sb);
10179         m = sb->sb_mb;
10180         rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
10181         if ((rsm == NULL) || (m == NULL)) {
10182                 /* Nothing outstanding */
10183                 return;
10184         }
10185         while (rsm->m && (rsm->m == m)) {
10186                 /* one to adjust */
10187 #ifdef INVARIANTS
10188                 struct mbuf *tm;
10189                 uint32_t soff;
10190
10191                 tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
10192                 if (rsm->orig_m_len != m->m_len) {
10193                         rack_adjust_orig_mlen(rsm);
10194                 }
10195                 if (rsm->soff != soff) {
10196                         /*
10197                          * This is not a fatal error, we anticipate it
10198                          * might happen (the else code), so we count it here
10199                          * so that under invariant we can see that it really
10200                          * does happen.
10201                          */
10202                         counter_u64_add(rack_adjust_map_bw, 1);
10203                 }
10204                 rsm->m = tm;
10205                 rsm->soff = soff;
10206                 if (tm)
10207                         rsm->orig_m_len = rsm->m->m_len;
10208                 else
10209                         rsm->orig_m_len = 0;
10210 #else
10211                 rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
10212                 if (rsm->m)
10213                         rsm->orig_m_len = rsm->m->m_len;
10214                 else
10215                         rsm->orig_m_len = 0;
10216 #endif
10217                 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
10218                               rsm);
10219                 if (rsm == NULL)
10220                         break;
10221         }
10222 }
10223
10224 /*
10225  * Return value of 1, we do not need to call rack_process_data().
10226  * return value of 0, rack_process_data can be called.
10227  * For ret_val if its 0 the TCP is locked, if its non-zero
10228  * its unlocked and probably unsafe to touch the TCB.
10229  */
10230 static int
10231 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
10232     struct tcpcb *tp, struct tcpopt *to,
10233     uint32_t tiwin, int32_t tlen,
10234     int32_t * ofia, int32_t thflags, int32_t *ret_val)
10235 {
10236         int32_t ourfinisacked = 0;
10237         int32_t nsegs, acked_amount;
10238         int32_t acked;
10239         struct mbuf *mfree;
10240         struct tcp_rack *rack;
10241         int32_t under_pacing = 0;
10242         int32_t recovery = 0;
10243
10244         rack = (struct tcp_rack *)tp->t_fb_ptr;
10245         if (SEQ_GT(th->th_ack, tp->snd_max)) {
10246                 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
10247                                       &rack->r_ctl.challenge_ack_ts,
10248                                       &rack->r_ctl.challenge_ack_cnt);
10249                 rack->r_wanted_output = 1;
10250                 return (1);
10251         }
10252         if (rack->gp_ready &&
10253             (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
10254                 under_pacing = 1;
10255         }
10256         if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
10257                 int in_rec, dup_ack_struck = 0;
10258
10259                 in_rec = IN_FASTRECOVERY(tp->t_flags);
10260                 if (rack->rc_in_persist) {
10261                         tp->t_rxtshift = 0;
10262                         RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10263                                       rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10264                 }
10265                 if ((th->th_ack == tp->snd_una) &&
10266                     (tiwin == tp->snd_wnd) &&
10267                     ((to->to_flags & TOF_SACK) == 0)) {
10268                         rack_strike_dupack(rack);
10269                         dup_ack_struck = 1;
10270                 }
10271                 rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), dup_ack_struck);
10272         }
10273         if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
10274                 /*
10275                  * Old ack, behind (or duplicate to) the last one rcv'd
10276                  * Note: We mark reordering is occuring if its
10277                  * less than and we have not closed our window.
10278                  */
10279                 if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
10280                         counter_u64_add(rack_reorder_seen, 1);
10281                         rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
10282                 }
10283                 return (0);
10284         }
10285         /*
10286          * If we reach this point, ACK is not a duplicate, i.e., it ACKs
10287          * something we sent.
10288          */
10289         if (tp->t_flags & TF_NEEDSYN) {
10290                 /*
10291                  * T/TCP: Connection was half-synchronized, and our SYN has
10292                  * been ACK'd (so connection is now fully synchronized).  Go
10293                  * to non-starred state, increment snd_una for ACK of SYN,
10294                  * and check if we can do window scaling.
10295                  */
10296                 tp->t_flags &= ~TF_NEEDSYN;
10297                 tp->snd_una++;
10298                 /* Do window scaling? */
10299                 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
10300                     (TF_RCVD_SCALE | TF_REQ_SCALE)) {
10301                         tp->rcv_scale = tp->request_r_scale;
10302                         /* Send window already scaled. */
10303                 }
10304         }
10305         nsegs = max(1, m->m_pkthdr.lro_nsegs);
10306         INP_WLOCK_ASSERT(tp->t_inpcb);
10307
10308         acked = BYTES_THIS_ACK(tp, th);
10309         KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
10310         KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
10311         /*
10312          * If we just performed our first retransmit, and the ACK arrives
10313          * within our recovery window, then it was a mistake to do the
10314          * retransmit in the first place.  Recover our original cwnd and
10315          * ssthresh, and proceed to transmit where we left off.
10316          */
10317         if ((tp->t_flags & TF_PREVVALID) &&
10318             ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
10319                 tp->t_flags &= ~TF_PREVVALID;
10320                 if (tp->t_rxtshift == 1 &&
10321                     (int)(ticks - tp->t_badrxtwin) < 0)
10322                         rack_cong_signal(tp, CC_RTO_ERR, th->th_ack);
10323         }
10324         if (acked) {
10325                 /* assure we are not backed off */
10326                 tp->t_rxtshift = 0;
10327                 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
10328                               rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
10329                 rack->rc_tlp_in_progress = 0;
10330                 rack->r_ctl.rc_tlp_cnt_out = 0;
10331                 /*
10332                  * If it is the RXT timer we want to
10333                  * stop it, so we can restart a TLP.
10334                  */
10335                 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
10336                         rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10337 #ifdef NETFLIX_HTTP_LOGGING
10338                 tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
10339 #endif
10340         }
10341         /*
10342          * If we have a timestamp reply, update smoothed round trip time. If
10343          * no timestamp is present but transmit timer is running and timed
10344          * sequence number was acked, update smoothed round trip time. Since
10345          * we now have an rtt measurement, cancel the timer backoff (cf.,
10346          * Phil Karn's retransmit alg.). Recompute the initial retransmit
10347          * timer.
10348          *
10349          * Some boxes send broken timestamp replies during the SYN+ACK
10350          * phase, ignore timestamps of 0 or we could calculate a huge RTT
10351          * and blow up the retransmit timer.
10352          */
10353         /*
10354          * If all outstanding data is acked, stop retransmit timer and
10355          * remember to restart (more output or persist). If there is more
10356          * data to be acked, restart retransmit timer, using current
10357          * (possibly backed-off) value.
10358          */
10359         if (acked == 0) {
10360                 if (ofia)
10361                         *ofia = ourfinisacked;
10362                 return (0);
10363         }
10364         if (IN_RECOVERY(tp->t_flags)) {
10365                 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
10366                     (SEQ_LT(th->th_ack, tp->snd_max))) {
10367                         tcp_rack_partialack(tp);
10368                 } else {
10369                         rack_post_recovery(tp, th->th_ack);
10370                         recovery = 1;
10371                 }
10372         }
10373         /*
10374          * Let the congestion control algorithm update congestion control
10375          * related information. This typically means increasing the
10376          * congestion window.
10377          */
10378         rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery);
10379         SOCKBUF_LOCK(&so->so_snd);
10380         acked_amount = min(acked, (int)sbavail(&so->so_snd));
10381         tp->snd_wnd -= acked_amount;
10382         mfree = sbcut_locked(&so->so_snd, acked_amount);
10383         if ((sbused(&so->so_snd) == 0) &&
10384             (acked > acked_amount) &&
10385             (tp->t_state >= TCPS_FIN_WAIT_1) &&
10386             (tp->t_flags & TF_SENTFIN)) {
10387                 /*
10388                  * We must be sure our fin
10389                  * was sent and acked (we can be
10390                  * in FIN_WAIT_1 without having
10391                  * sent the fin).
10392                  */
10393                 ourfinisacked = 1;
10394         }
10395         tp->snd_una = th->th_ack;
10396         if (acked_amount && sbavail(&so->so_snd))
10397                 rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
10398         rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
10399         /* NB: sowwakeup_locked() does an implicit unlock. */
10400         sowwakeup_locked(so);
10401         m_freem(mfree);
10402         if (SEQ_GT(tp->snd_una, tp->snd_recover))
10403                 tp->snd_recover = tp->snd_una;
10404
10405         if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
10406                 tp->snd_nxt = tp->snd_una;
10407         }
10408         if (under_pacing &&
10409             (rack->use_fixed_rate == 0) &&
10410             (rack->in_probe_rtt == 0) &&
10411             rack->rc_gp_dyn_mul &&
10412             rack->rc_always_pace) {
10413                 /* Check if we are dragging bottom */
10414                 rack_check_bottom_drag(tp, rack, so, acked);
10415         }
10416         if (tp->snd_una == tp->snd_max) {
10417                 /* Nothing left outstanding */
10418                 tp->t_flags &= ~TF_PREVVALID;
10419                 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
10420                 rack->r_ctl.retran_during_recovery = 0;
10421                 rack->r_ctl.dsack_byte_cnt = 0;
10422                 if (rack->r_ctl.rc_went_idle_time == 0)
10423                         rack->r_ctl.rc_went_idle_time = 1;
10424                 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
10425                 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
10426                         tp->t_acktime = 0;
10427                 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10428                 /* Set need output so persist might get set */
10429                 rack->r_wanted_output = 1;
10430                 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
10431                 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
10432                     (sbavail(&so->so_snd) == 0) &&
10433                     (tp->t_flags2 & TF2_DROP_AF_DATA)) {
10434                         /*
10435                          * The socket was gone and the
10436                          * peer sent data (now or in the past), time to
10437                          * reset him.
10438                          */
10439                         *ret_val = 1;
10440                         /* tcp_close will kill the inp pre-log the Reset */
10441                         tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
10442                         tp = tcp_close(tp);
10443                         ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
10444                         return (1);
10445                 }
10446         }
10447         if (ofia)
10448                 *ofia = ourfinisacked;
10449         return (0);
10450 }
10451
10452 static void
10453 rack_collapsed_window(struct tcp_rack *rack)
10454 {
10455         /*
10456          * Now we must walk the
10457          * send map and divide the
10458          * ones left stranded. These
10459          * guys can't cause us to abort
10460          * the connection and are really
10461          * "unsent". However if a buggy
10462          * client actually did keep some
10463          * of the data i.e. collapsed the win
10464          * and refused to ack and then opened
10465          * the win and acked that data. We would
10466          * get into an ack war, the simplier
10467          * method then of just pretending we
10468          * did not send those segments something
10469          * won't work.
10470          */
10471         struct rack_sendmap *rsm, *nrsm, fe, *insret;
10472         tcp_seq max_seq;
10473
10474         max_seq = rack->rc_tp->snd_una + rack->rc_tp->snd_wnd;
10475         memset(&fe, 0, sizeof(fe));
10476         fe.r_start = max_seq;
10477         /* Find the first seq past or at maxseq */
10478         rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
10479         if (rsm == NULL) {
10480                 /* Nothing to do strange */
10481                 rack->rc_has_collapsed = 0;
10482                 return;
10483         }
10484         /*
10485          * Now do we need to split at
10486          * the collapse point?
10487          */
10488         if (SEQ_GT(max_seq, rsm->r_start)) {
10489                 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
10490                 if (nrsm == NULL) {
10491                         /* We can't get a rsm, mark all? */
10492                         nrsm = rsm;
10493                         goto no_split;
10494                 }
10495                 /* Clone it */
10496                 rack_clone_rsm(rack, nrsm, rsm, max_seq);
10497                 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10498 #ifdef INVARIANTS
10499                 if (insret != NULL) {
10500                         panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
10501                               nrsm, insret, rack, rsm);
10502                 }
10503 #endif
10504                 rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT, max_seq, __LINE__);
10505                 if (rsm->r_in_tmap) {
10506                         TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10507                         nrsm->r_in_tmap = 1;
10508                 }
10509                 /*
10510                  * Set in the new RSM as the
10511                  * collapsed starting point
10512                  */
10513                 rsm = nrsm;
10514         }
10515 no_split:
10516         counter_u64_add(rack_collapsed_win, 1);
10517         RB_FOREACH_FROM(nrsm, rack_rb_tree_head, rsm) {
10518                 nrsm->r_flags |= RACK_RWND_COLLAPSED;
10519         }
10520         rack->rc_has_collapsed = 1;
10521 }
10522
10523 static void
10524 rack_un_collapse_window(struct tcp_rack *rack)
10525 {
10526         struct rack_sendmap *rsm;
10527
10528         RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
10529                 if (rsm->r_flags & RACK_RWND_COLLAPSED)
10530                         rsm->r_flags &= ~RACK_RWND_COLLAPSED;
10531                 else
10532                         break;
10533         }
10534         rack->rc_has_collapsed = 0;
10535 }
10536
10537 static void
10538 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
10539                         int32_t tlen, int32_t tfo_syn)
10540 {
10541         if (DELAY_ACK(tp, tlen) || tfo_syn) {
10542                 if (rack->rc_dack_mode &&
10543                     (tlen > 500) &&
10544                     (rack->rc_dack_toggle == 1)) {
10545                         goto no_delayed_ack;
10546                 }
10547                 rack_timer_cancel(tp, rack,
10548                                   rack->r_ctl.rc_rcvtime, __LINE__);
10549                 tp->t_flags |= TF_DELACK;
10550         } else {
10551 no_delayed_ack:
10552                 rack->r_wanted_output = 1;
10553                 tp->t_flags |= TF_ACKNOW;
10554                 if (rack->rc_dack_mode) {
10555                         if (tp->t_flags & TF_DELACK)
10556                                 rack->rc_dack_toggle = 1;
10557                         else
10558                                 rack->rc_dack_toggle = 0;
10559                 }
10560         }
10561 }
10562
10563 static void
10564 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
10565 {
10566         /*
10567          * If fast output is in progress, lets validate that
10568          * the new window did not shrink on us and make it
10569          * so fast output should end.
10570          */
10571         if (rack->r_fast_output) {
10572                 uint32_t out;
10573
10574                 /*
10575                  * Calculate what we will send if left as is
10576                  * and compare that to our send window.
10577                  */
10578                 out = ctf_outstanding(tp);
10579                 if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
10580                         /* ok we have an issue */
10581                         if (out >= tp->snd_wnd) {
10582                                 /* Turn off fast output the window is met or collapsed */
10583                                 rack->r_fast_output = 0;
10584                         } else {
10585                                 /* we have some room left */
10586                                 rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
10587                                 if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
10588                                         /* If not at least 1 full segment never mind */
10589                                         rack->r_fast_output = 0;
10590                                 }
10591                         }
10592                 }
10593         }
10594 }
10595
10596
10597 /*
10598  * Return value of 1, the TCB is unlocked and most
10599  * likely gone, return value of 0, the TCP is still
10600  * locked.
10601  */
10602 static int
10603 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
10604     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
10605     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
10606 {
10607         /*
10608          * Update window information. Don't look at window if no ACK: TAC's
10609          * send garbage on first SYN.
10610          */
10611         int32_t nsegs;
10612         int32_t tfo_syn;
10613         struct tcp_rack *rack;
10614
10615         rack = (struct tcp_rack *)tp->t_fb_ptr;
10616         INP_WLOCK_ASSERT(tp->t_inpcb);
10617         nsegs = max(1, m->m_pkthdr.lro_nsegs);
10618         if ((thflags & TH_ACK) &&
10619             (SEQ_LT(tp->snd_wl1, th->th_seq) ||
10620             (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
10621             (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
10622                 /* keep track of pure window updates */
10623                 if (tlen == 0 &&
10624                     tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
10625                         KMOD_TCPSTAT_INC(tcps_rcvwinupd);
10626                 tp->snd_wnd = tiwin;
10627                 rack_validate_fo_sendwin_up(tp, rack);
10628                 tp->snd_wl1 = th->th_seq;
10629                 tp->snd_wl2 = th->th_ack;
10630                 if (tp->snd_wnd > tp->max_sndwnd)
10631                         tp->max_sndwnd = tp->snd_wnd;
10632                 rack->r_wanted_output = 1;
10633         } else if (thflags & TH_ACK) {
10634                 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
10635                         tp->snd_wnd = tiwin;
10636                         rack_validate_fo_sendwin_up(tp, rack);
10637                         tp->snd_wl1 = th->th_seq;
10638                         tp->snd_wl2 = th->th_ack;
10639                 }
10640         }
10641         if (tp->snd_wnd < ctf_outstanding(tp))
10642                 /* The peer collapsed the window */
10643                 rack_collapsed_window(rack);
10644         else if (rack->rc_has_collapsed)
10645                 rack_un_collapse_window(rack);
10646         /* Was persist timer active and now we have window space? */
10647         if ((rack->rc_in_persist != 0) &&
10648             (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
10649                                 rack->r_ctl.rc_pace_min_segs))) {
10650                 rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10651                 tp->snd_nxt = tp->snd_max;
10652                 /* Make sure we output to start the timer */
10653                 rack->r_wanted_output = 1;
10654         }
10655         /* Do we enter persists? */
10656         if ((rack->rc_in_persist == 0) &&
10657             (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
10658             TCPS_HAVEESTABLISHED(tp->t_state) &&
10659             (tp->snd_max == tp->snd_una) &&
10660             sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
10661             (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
10662                 /*
10663                  * Here the rwnd is less than
10664                  * the pacing size, we are established,
10665                  * nothing is outstanding, and there is
10666                  * data to send. Enter persists.
10667                  */
10668                 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10669         }
10670         if (tp->t_flags2 & TF2_DROP_AF_DATA) {
10671                 m_freem(m);
10672                 return (0);
10673         }
10674         /*
10675          * don't process the URG bit, ignore them drag
10676          * along the up.
10677          */
10678         tp->rcv_up = tp->rcv_nxt;
10679         INP_WLOCK_ASSERT(tp->t_inpcb);
10680
10681         /*
10682          * Process the segment text, merging it into the TCP sequencing
10683          * queue, and arranging for acknowledgment of receipt if necessary.
10684          * This process logically involves adjusting tp->rcv_wnd as data is
10685          * presented to the user (this happens in tcp_usrreq.c, case
10686          * PRU_RCVD).  If a FIN has already been received on this connection
10687          * then we just ignore the text.
10688          */
10689         tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
10690                    IS_FASTOPEN(tp->t_flags));
10691         if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
10692             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10693                 tcp_seq save_start = th->th_seq;
10694                 tcp_seq save_rnxt  = tp->rcv_nxt;
10695                 int     save_tlen  = tlen;
10696
10697                 m_adj(m, drop_hdrlen);  /* delayed header drop */
10698                 /*
10699                  * Insert segment which includes th into TCP reassembly
10700                  * queue with control block tp.  Set thflags to whether
10701                  * reassembly now includes a segment with FIN.  This handles
10702                  * the common case inline (segment is the next to be
10703                  * received on an established connection, and the queue is
10704                  * empty), avoiding linkage into and removal from the queue
10705                  * and repetition of various conversions. Set DELACK for
10706                  * segments received in order, but ack immediately when
10707                  * segments are out of order (so fast retransmit can work).
10708                  */
10709                 if (th->th_seq == tp->rcv_nxt &&
10710                     SEGQ_EMPTY(tp) &&
10711                     (TCPS_HAVEESTABLISHED(tp->t_state) ||
10712                     tfo_syn)) {
10713 #ifdef NETFLIX_SB_LIMITS
10714                         u_int mcnt, appended;
10715
10716                         if (so->so_rcv.sb_shlim) {
10717                                 mcnt = m_memcnt(m);
10718                                 appended = 0;
10719                                 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
10720                                     CFO_NOSLEEP, NULL) == false) {
10721                                         counter_u64_add(tcp_sb_shlim_fails, 1);
10722                                         m_freem(m);
10723                                         return (0);
10724                                 }
10725                         }
10726 #endif
10727                         rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
10728                         tp->rcv_nxt += tlen;
10729                         if (tlen &&
10730                             ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
10731                             (tp->t_fbyte_in == 0)) {
10732                                 tp->t_fbyte_in = ticks;
10733                                 if (tp->t_fbyte_in == 0)
10734                                         tp->t_fbyte_in = 1;
10735                                 if (tp->t_fbyte_out && tp->t_fbyte_in)
10736                                         tp->t_flags2 |= TF2_FBYTES_COMPLETE;
10737                         }
10738                         thflags = th->th_flags & TH_FIN;
10739                         KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10740                         KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10741                         SOCKBUF_LOCK(&so->so_rcv);
10742                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
10743                                 m_freem(m);
10744                         } else
10745 #ifdef NETFLIX_SB_LIMITS
10746                                 appended =
10747 #endif
10748                                         sbappendstream_locked(&so->so_rcv, m, 0);
10749
10750                         rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
10751                         /* NB: sorwakeup_locked() does an implicit unlock. */
10752                         sorwakeup_locked(so);
10753 #ifdef NETFLIX_SB_LIMITS
10754                         if (so->so_rcv.sb_shlim && appended != mcnt)
10755                                 counter_fo_release(so->so_rcv.sb_shlim,
10756                                     mcnt - appended);
10757 #endif
10758                 } else {
10759                         /*
10760                          * XXX: Due to the header drop above "th" is
10761                          * theoretically invalid by now.  Fortunately
10762                          * m_adj() doesn't actually frees any mbufs when
10763                          * trimming from the head.
10764                          */
10765                         tcp_seq temp = save_start;
10766
10767                         thflags = tcp_reass(tp, th, &temp, &tlen, m);
10768                         tp->t_flags |= TF_ACKNOW;
10769                         if (tp->t_flags & TF_WAKESOR) {
10770                                 tp->t_flags &= ~TF_WAKESOR;
10771                                 /* NB: sorwakeup_locked() does an implicit unlock. */
10772                                 sorwakeup_locked(so);
10773                         }
10774                 }
10775                 if ((tp->t_flags & TF_SACK_PERMIT) &&
10776                     (save_tlen > 0) &&
10777                     TCPS_HAVEESTABLISHED(tp->t_state)) {
10778                         if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
10779                                 /*
10780                                  * DSACK actually handled in the fastpath
10781                                  * above.
10782                                  */
10783                                 RACK_OPTS_INC(tcp_sack_path_1);
10784                                 tcp_update_sack_list(tp, save_start,
10785                                     save_start + save_tlen);
10786                         } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
10787                                 if ((tp->rcv_numsacks >= 1) &&
10788                                     (tp->sackblks[0].end == save_start)) {
10789                                         /*
10790                                          * Partial overlap, recorded at todrop
10791                                          * above.
10792                                          */
10793                                         RACK_OPTS_INC(tcp_sack_path_2a);
10794                                         tcp_update_sack_list(tp,
10795                                             tp->sackblks[0].start,
10796                                             tp->sackblks[0].end);
10797                                 } else {
10798                                         RACK_OPTS_INC(tcp_sack_path_2b);
10799                                         tcp_update_dsack_list(tp, save_start,
10800                                             save_start + save_tlen);
10801                                 }
10802                         } else if (tlen >= save_tlen) {
10803                                 /* Update of sackblks. */
10804                                 RACK_OPTS_INC(tcp_sack_path_3);
10805                                 tcp_update_dsack_list(tp, save_start,
10806                                     save_start + save_tlen);
10807                         } else if (tlen > 0) {
10808                                 RACK_OPTS_INC(tcp_sack_path_4);
10809                                 tcp_update_dsack_list(tp, save_start,
10810                                     save_start + tlen);
10811                         }
10812                 }
10813         } else {
10814                 m_freem(m);
10815                 thflags &= ~TH_FIN;
10816         }
10817
10818         /*
10819          * If FIN is received ACK the FIN and let the user know that the
10820          * connection is closing.
10821          */
10822         if (thflags & TH_FIN) {
10823                 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10824                         /* The socket upcall is handled by socantrcvmore. */
10825                         socantrcvmore(so);
10826                         /*
10827                          * If connection is half-synchronized (ie NEEDSYN
10828                          * flag on) then delay ACK, so it may be piggybacked
10829                          * when SYN is sent. Otherwise, since we received a
10830                          * FIN then no more input can be expected, send ACK
10831                          * now.
10832                          */
10833                         if (tp->t_flags & TF_NEEDSYN) {
10834                                 rack_timer_cancel(tp, rack,
10835                                     rack->r_ctl.rc_rcvtime, __LINE__);
10836                                 tp->t_flags |= TF_DELACK;
10837                         } else {
10838                                 tp->t_flags |= TF_ACKNOW;
10839                         }
10840                         tp->rcv_nxt++;
10841                 }
10842                 switch (tp->t_state) {
10843                         /*
10844                          * In SYN_RECEIVED and ESTABLISHED STATES enter the
10845                          * CLOSE_WAIT state.
10846                          */
10847                 case TCPS_SYN_RECEIVED:
10848                         tp->t_starttime = ticks;
10849                         /* FALLTHROUGH */
10850                 case TCPS_ESTABLISHED:
10851                         rack_timer_cancel(tp, rack,
10852                             rack->r_ctl.rc_rcvtime, __LINE__);
10853                         tcp_state_change(tp, TCPS_CLOSE_WAIT);
10854                         break;
10855
10856                         /*
10857                          * If still in FIN_WAIT_1 STATE FIN has not been
10858                          * acked so enter the CLOSING state.
10859                          */
10860                 case TCPS_FIN_WAIT_1:
10861                         rack_timer_cancel(tp, rack,
10862                             rack->r_ctl.rc_rcvtime, __LINE__);
10863                         tcp_state_change(tp, TCPS_CLOSING);
10864                         break;
10865
10866                         /*
10867                          * In FIN_WAIT_2 state enter the TIME_WAIT state,
10868                          * starting the time-wait timer, turning off the
10869                          * other standard timers.
10870                          */
10871                 case TCPS_FIN_WAIT_2:
10872                         rack_timer_cancel(tp, rack,
10873                             rack->r_ctl.rc_rcvtime, __LINE__);
10874                         tcp_twstart(tp);
10875                         return (1);
10876                 }
10877         }
10878         /*
10879          * Return any desired output.
10880          */
10881         if ((tp->t_flags & TF_ACKNOW) ||
10882             (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
10883                 rack->r_wanted_output = 1;
10884         }
10885         INP_WLOCK_ASSERT(tp->t_inpcb);
10886         return (0);
10887 }
10888
10889 /*
10890  * Here nothing is really faster, its just that we
10891  * have broken out the fast-data path also just like
10892  * the fast-ack.
10893  */
10894 static int
10895 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
10896     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10897     uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
10898 {
10899         int32_t nsegs;
10900         int32_t newsize = 0;    /* automatic sockbuf scaling */
10901         struct tcp_rack *rack;
10902 #ifdef NETFLIX_SB_LIMITS
10903         u_int mcnt, appended;
10904 #endif
10905 #ifdef TCPDEBUG
10906         /*
10907          * The size of tcp_saveipgen must be the size of the max ip header,
10908          * now IPv6.
10909          */
10910         u_char tcp_saveipgen[IP6_HDR_LEN];
10911         struct tcphdr tcp_savetcp;
10912         short ostate = 0;
10913
10914 #endif
10915         /*
10916          * If last ACK falls within this segment's sequence numbers, record
10917          * the timestamp. NOTE that the test is modified according to the
10918          * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
10919          */
10920         if (__predict_false(th->th_seq != tp->rcv_nxt)) {
10921                 return (0);
10922         }
10923         if (__predict_false(tp->snd_nxt != tp->snd_max)) {
10924                 return (0);
10925         }
10926         if (tiwin && tiwin != tp->snd_wnd) {
10927                 return (0);
10928         }
10929         if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
10930                 return (0);
10931         }
10932         if (__predict_false((to->to_flags & TOF_TS) &&
10933             (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
10934                 return (0);
10935         }
10936         if (__predict_false((th->th_ack != tp->snd_una))) {
10937                 return (0);
10938         }
10939         if (__predict_false(tlen > sbspace(&so->so_rcv))) {
10940                 return (0);
10941         }
10942         if ((to->to_flags & TOF_TS) != 0 &&
10943             SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
10944                 tp->ts_recent_age = tcp_ts_getticks();
10945                 tp->ts_recent = to->to_tsval;
10946         }
10947         rack = (struct tcp_rack *)tp->t_fb_ptr;
10948         /*
10949          * This is a pure, in-sequence data packet with nothing on the
10950          * reassembly queue and we have enough buffer space to take it.
10951          */
10952         nsegs = max(1, m->m_pkthdr.lro_nsegs);
10953
10954 #ifdef NETFLIX_SB_LIMITS
10955         if (so->so_rcv.sb_shlim) {
10956                 mcnt = m_memcnt(m);
10957                 appended = 0;
10958                 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
10959                     CFO_NOSLEEP, NULL) == false) {
10960                         counter_u64_add(tcp_sb_shlim_fails, 1);
10961                         m_freem(m);
10962                         return (1);
10963                 }
10964         }
10965 #endif
10966         /* Clean receiver SACK report if present */
10967         if (tp->rcv_numsacks)
10968                 tcp_clean_sackreport(tp);
10969         KMOD_TCPSTAT_INC(tcps_preddat);
10970         tp->rcv_nxt += tlen;
10971         if (tlen &&
10972             ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
10973             (tp->t_fbyte_in == 0)) {
10974                 tp->t_fbyte_in = ticks;
10975                 if (tp->t_fbyte_in == 0)
10976                         tp->t_fbyte_in = 1;
10977                 if (tp->t_fbyte_out && tp->t_fbyte_in)
10978                         tp->t_flags2 |= TF2_FBYTES_COMPLETE;
10979         }
10980         /*
10981          * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
10982          */
10983         tp->snd_wl1 = th->th_seq;
10984         /*
10985          * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
10986          */
10987         tp->rcv_up = tp->rcv_nxt;
10988         KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10989         KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10990 #ifdef TCPDEBUG
10991         if (so->so_options & SO_DEBUG)
10992                 tcp_trace(TA_INPUT, ostate, tp,
10993                     (void *)tcp_saveipgen, &tcp_savetcp, 0);
10994 #endif
10995         newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
10996
10997         /* Add data to socket buffer. */
10998         SOCKBUF_LOCK(&so->so_rcv);
10999         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11000                 m_freem(m);
11001         } else {
11002                 /*
11003                  * Set new socket buffer size. Give up when limit is
11004                  * reached.
11005                  */
11006                 if (newsize)
11007                         if (!sbreserve_locked(&so->so_rcv,
11008                             newsize, so, NULL))
11009                                 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
11010                 m_adj(m, drop_hdrlen);  /* delayed header drop */
11011 #ifdef NETFLIX_SB_LIMITS
11012                 appended =
11013 #endif
11014                         sbappendstream_locked(&so->so_rcv, m, 0);
11015                 ctf_calc_rwin(so, tp);
11016         }
11017         rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
11018         /* NB: sorwakeup_locked() does an implicit unlock. */
11019         sorwakeup_locked(so);
11020 #ifdef NETFLIX_SB_LIMITS
11021         if (so->so_rcv.sb_shlim && mcnt != appended)
11022                 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
11023 #endif
11024         rack_handle_delayed_ack(tp, rack, tlen, 0);
11025         if (tp->snd_una == tp->snd_max)
11026                 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
11027         return (1);
11028 }
11029
11030 /*
11031  * This subfunction is used to try to highly optimize the
11032  * fast path. We again allow window updates that are
11033  * in sequence to remain in the fast-path. We also add
11034  * in the __predict's to attempt to help the compiler.
11035  * Note that if we return a 0, then we can *not* process
11036  * it and the caller should push the packet into the
11037  * slow-path.
11038  */
11039 static int
11040 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
11041     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11042     uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
11043 {
11044         int32_t acked;
11045         int32_t nsegs;
11046 #ifdef TCPDEBUG
11047         /*
11048          * The size of tcp_saveipgen must be the size of the max ip header,
11049          * now IPv6.
11050          */
11051         u_char tcp_saveipgen[IP6_HDR_LEN];
11052         struct tcphdr tcp_savetcp;
11053         short ostate = 0;
11054 #endif
11055         int32_t under_pacing = 0;
11056         struct tcp_rack *rack;
11057
11058         if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
11059                 /* Old ack, behind (or duplicate to) the last one rcv'd */
11060                 return (0);
11061         }
11062         if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
11063                 /* Above what we have sent? */
11064                 return (0);
11065         }
11066         if (__predict_false(tp->snd_nxt != tp->snd_max)) {
11067                 /* We are retransmitting */
11068                 return (0);
11069         }
11070         if (__predict_false(tiwin == 0)) {
11071                 /* zero window */
11072                 return (0);
11073         }
11074         if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
11075                 /* We need a SYN or a FIN, unlikely.. */
11076                 return (0);
11077         }
11078         if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
11079                 /* Timestamp is behind .. old ack with seq wrap? */
11080                 return (0);
11081         }
11082         if (__predict_false(IN_RECOVERY(tp->t_flags))) {
11083                 /* Still recovering */
11084                 return (0);
11085         }
11086         rack = (struct tcp_rack *)tp->t_fb_ptr;
11087         if (rack->r_ctl.rc_sacked) {
11088                 /* We have sack holes on our scoreboard */
11089                 return (0);
11090         }
11091         /* Ok if we reach here, we can process a fast-ack */
11092         if (rack->gp_ready &&
11093             (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11094                 under_pacing = 1;
11095         }
11096         nsegs = max(1, m->m_pkthdr.lro_nsegs);
11097         rack_log_ack(tp, to, th, 0, 0);
11098         /* Did the window get updated? */
11099         if (tiwin != tp->snd_wnd) {
11100                 tp->snd_wnd = tiwin;
11101                 rack_validate_fo_sendwin_up(tp, rack);
11102                 tp->snd_wl1 = th->th_seq;
11103                 if (tp->snd_wnd > tp->max_sndwnd)
11104                         tp->max_sndwnd = tp->snd_wnd;
11105         }
11106         /* Do we exit persists? */
11107         if ((rack->rc_in_persist != 0) &&
11108             (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
11109                                rack->r_ctl.rc_pace_min_segs))) {
11110                 rack_exit_persist(tp, rack, cts);
11111         }
11112         /* Do we enter persists? */
11113         if ((rack->rc_in_persist == 0) &&
11114             (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
11115             TCPS_HAVEESTABLISHED(tp->t_state) &&
11116             (tp->snd_max == tp->snd_una) &&
11117             sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
11118             (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
11119                 /*
11120                  * Here the rwnd is less than
11121                  * the pacing size, we are established,
11122                  * nothing is outstanding, and there is
11123                  * data to send. Enter persists.
11124                  */
11125                 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
11126         }
11127         /*
11128          * If last ACK falls within this segment's sequence numbers, record
11129          * the timestamp. NOTE that the test is modified according to the
11130          * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
11131          */
11132         if ((to->to_flags & TOF_TS) != 0 &&
11133             SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11134                 tp->ts_recent_age = tcp_ts_getticks();
11135                 tp->ts_recent = to->to_tsval;
11136         }
11137         /*
11138          * This is a pure ack for outstanding data.
11139          */
11140         KMOD_TCPSTAT_INC(tcps_predack);
11141
11142         /*
11143          * "bad retransmit" recovery.
11144          */
11145         if ((tp->t_flags & TF_PREVVALID) &&
11146             ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
11147                 tp->t_flags &= ~TF_PREVVALID;
11148                 if (tp->t_rxtshift == 1 &&
11149                     (int)(ticks - tp->t_badrxtwin) < 0)
11150                         rack_cong_signal(tp, CC_RTO_ERR, th->th_ack);
11151         }
11152         /*
11153          * Recalculate the transmit timer / rtt.
11154          *
11155          * Some boxes send broken timestamp replies during the SYN+ACK
11156          * phase, ignore timestamps of 0 or we could calculate a huge RTT
11157          * and blow up the retransmit timer.
11158          */
11159         acked = BYTES_THIS_ACK(tp, th);
11160
11161 #ifdef TCP_HHOOK
11162         /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
11163         hhook_run_tcp_est_in(tp, th, to);
11164 #endif
11165         KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11166         KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11167         if (acked) {
11168                 struct mbuf *mfree;
11169
11170                 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
11171                 SOCKBUF_LOCK(&so->so_snd);
11172                 mfree = sbcut_locked(&so->so_snd, acked);
11173                 tp->snd_una = th->th_ack;
11174                 /* Note we want to hold the sb lock through the sendmap adjust */
11175                 rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
11176                 /* Wake up the socket if we have room to write more */
11177                 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
11178                 sowwakeup_locked(so);
11179                 m_freem(mfree);
11180                 tp->t_rxtshift = 0;
11181                 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
11182                               rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
11183                 rack->rc_tlp_in_progress = 0;
11184                 rack->r_ctl.rc_tlp_cnt_out = 0;
11185                 /*
11186                  * If it is the RXT timer we want to
11187                  * stop it, so we can restart a TLP.
11188                  */
11189                 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
11190                         rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11191 #ifdef NETFLIX_HTTP_LOGGING
11192                 tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
11193 #endif
11194         }
11195         /*
11196          * Let the congestion control algorithm update congestion control
11197          * related information. This typically means increasing the
11198          * congestion window.
11199          */
11200         if (tp->snd_wnd < ctf_outstanding(tp)) {
11201                 /* The peer collapsed the window */
11202                 rack_collapsed_window(rack);
11203         } else if (rack->rc_has_collapsed)
11204                 rack_un_collapse_window(rack);
11205
11206         /*
11207          * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
11208          */
11209         tp->snd_wl2 = th->th_ack;
11210         tp->t_dupacks = 0;
11211         m_freem(m);
11212         /* ND6_HINT(tp);         *//* Some progress has been made. */
11213
11214         /*
11215          * If all outstanding data are acked, stop retransmit timer,
11216          * otherwise restart timer using current (possibly backed-off)
11217          * value. If process is waiting for space, wakeup/selwakeup/signal.
11218          * If data are ready to send, let tcp_output decide between more
11219          * output or persist.
11220          */
11221 #ifdef TCPDEBUG
11222         if (so->so_options & SO_DEBUG)
11223                 tcp_trace(TA_INPUT, ostate, tp,
11224                     (void *)tcp_saveipgen,
11225                     &tcp_savetcp, 0);
11226 #endif
11227         if (under_pacing &&
11228             (rack->use_fixed_rate == 0) &&
11229             (rack->in_probe_rtt == 0) &&
11230             rack->rc_gp_dyn_mul &&
11231             rack->rc_always_pace) {
11232                 /* Check if we are dragging bottom */
11233                 rack_check_bottom_drag(tp, rack, so, acked);
11234         }
11235         if (tp->snd_una == tp->snd_max) {
11236                 tp->t_flags &= ~TF_PREVVALID;
11237                 rack->r_ctl.retran_during_recovery = 0;
11238                 rack->r_ctl.dsack_byte_cnt = 0;
11239                 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
11240                 if (rack->r_ctl.rc_went_idle_time == 0)
11241                         rack->r_ctl.rc_went_idle_time = 1;
11242                 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
11243                 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
11244                         tp->t_acktime = 0;
11245                 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11246         }
11247         if (acked && rack->r_fast_output)
11248                 rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
11249         if (sbavail(&so->so_snd)) {
11250                 rack->r_wanted_output = 1;
11251         }
11252         return (1);
11253 }
11254
11255 /*
11256  * Return value of 1, the TCB is unlocked and most
11257  * likely gone, return value of 0, the TCP is still
11258  * locked.
11259  */
11260 static int
11261 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
11262     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11263     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11264 {
11265         int32_t ret_val = 0;
11266         int32_t todrop;
11267         int32_t ourfinisacked = 0;
11268         struct tcp_rack *rack;
11269
11270         ctf_calc_rwin(so, tp);
11271         /*
11272          * If the state is SYN_SENT: if seg contains an ACK, but not for our
11273          * SYN, drop the input. if seg contains a RST, then drop the
11274          * connection. if seg does not contain SYN, then drop it. Otherwise
11275          * this is an acceptable SYN segment initialize tp->rcv_nxt and
11276          * tp->irs if seg contains ack then advance tp->snd_una if seg
11277          * contains an ECE and ECN support is enabled, the stream is ECN
11278          * capable. if SYN has been acked change to ESTABLISHED else
11279          * SYN_RCVD state arrange for segment to be acked (eventually)
11280          * continue processing rest of data/controls.
11281          */
11282         if ((thflags & TH_ACK) &&
11283             (SEQ_LEQ(th->th_ack, tp->iss) ||
11284             SEQ_GT(th->th_ack, tp->snd_max))) {
11285                 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11286                 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11287                 return (1);
11288         }
11289         if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
11290                 TCP_PROBE5(connect__refused, NULL, tp,
11291                     mtod(m, const char *), tp, th);
11292                 tp = tcp_drop(tp, ECONNREFUSED);
11293                 ctf_do_drop(m, tp);
11294                 return (1);
11295         }
11296         if (thflags & TH_RST) {
11297                 ctf_do_drop(m, tp);
11298                 return (1);
11299         }
11300         if (!(thflags & TH_SYN)) {
11301                 ctf_do_drop(m, tp);
11302                 return (1);
11303         }
11304         tp->irs = th->th_seq;
11305         tcp_rcvseqinit(tp);
11306         rack = (struct tcp_rack *)tp->t_fb_ptr;
11307         if (thflags & TH_ACK) {
11308                 int tfo_partial = 0;
11309
11310                 KMOD_TCPSTAT_INC(tcps_connects);
11311                 soisconnected(so);
11312 #ifdef MAC
11313                 mac_socketpeer_set_from_mbuf(m, so);
11314 #endif
11315                 /* Do window scaling on this connection? */
11316                 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11317                     (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11318                         tp->rcv_scale = tp->request_r_scale;
11319                 }
11320                 tp->rcv_adv += min(tp->rcv_wnd,
11321                     TCP_MAXWIN << tp->rcv_scale);
11322                 /*
11323                  * If not all the data that was sent in the TFO SYN
11324                  * has been acked, resend the remainder right away.
11325                  */
11326                 if (IS_FASTOPEN(tp->t_flags) &&
11327                     (tp->snd_una != tp->snd_max)) {
11328                         tp->snd_nxt = th->th_ack;
11329                         tfo_partial = 1;
11330                 }
11331                 /*
11332                  * If there's data, delay ACK; if there's also a FIN ACKNOW
11333                  * will be turned on later.
11334                  */
11335                 if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
11336                         rack_timer_cancel(tp, rack,
11337                                           rack->r_ctl.rc_rcvtime, __LINE__);
11338                         tp->t_flags |= TF_DELACK;
11339                 } else {
11340                         rack->r_wanted_output = 1;
11341                         tp->t_flags |= TF_ACKNOW;
11342                         rack->rc_dack_toggle = 0;
11343                 }
11344                 if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
11345                     (V_tcp_do_ecn == 1)) {
11346                         tp->t_flags2 |= TF2_ECN_PERMIT;
11347                         KMOD_TCPSTAT_INC(tcps_ecn_shs);
11348                 }
11349                 if (SEQ_GT(th->th_ack, tp->snd_una)) {
11350                         /*
11351                          * We advance snd_una for the
11352                          * fast open case. If th_ack is
11353                          * acknowledging data beyond
11354                          * snd_una we can't just call
11355                          * ack-processing since the
11356                          * data stream in our send-map
11357                          * will start at snd_una + 1 (one
11358                          * beyond the SYN). If its just
11359                          * equal we don't need to do that
11360                          * and there is no send_map.
11361                          */
11362                         tp->snd_una++;
11363                 }
11364                 /*
11365                  * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
11366                  * SYN_SENT  --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
11367                  */
11368                 tp->t_starttime = ticks;
11369                 if (tp->t_flags & TF_NEEDFIN) {
11370                         tcp_state_change(tp, TCPS_FIN_WAIT_1);
11371                         tp->t_flags &= ~TF_NEEDFIN;
11372                         thflags &= ~TH_SYN;
11373                 } else {
11374                         tcp_state_change(tp, TCPS_ESTABLISHED);
11375                         TCP_PROBE5(connect__established, NULL, tp,
11376                             mtod(m, const char *), tp, th);
11377                         rack_cc_conn_init(tp);
11378                 }
11379         } else {
11380                 /*
11381                  * Received initial SYN in SYN-SENT[*] state => simultaneous
11382                  * open.  If segment contains CC option and there is a
11383                  * cached CC, apply TAO test. If it succeeds, connection is *
11384                  * half-synchronized. Otherwise, do 3-way handshake:
11385                  * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
11386                  * there was no CC option, clear cached CC value.
11387                  */
11388                 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
11389                 tcp_state_change(tp, TCPS_SYN_RECEIVED);
11390         }
11391         INP_WLOCK_ASSERT(tp->t_inpcb);
11392         /*
11393          * Advance th->th_seq to correspond to first data byte. If data,
11394          * trim to stay within window, dropping FIN if necessary.
11395          */
11396         th->th_seq++;
11397         if (tlen > tp->rcv_wnd) {
11398                 todrop = tlen - tp->rcv_wnd;
11399                 m_adj(m, -todrop);
11400                 tlen = tp->rcv_wnd;
11401                 thflags &= ~TH_FIN;
11402                 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
11403                 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
11404         }
11405         tp->snd_wl1 = th->th_seq - 1;
11406         tp->rcv_up = th->th_seq;
11407         /*
11408          * Client side of transaction: already sent SYN and data. If the
11409          * remote host used T/TCP to validate the SYN, our data will be
11410          * ACK'd; if so, enter normal data segment processing in the middle
11411          * of step 5, ack processing. Otherwise, goto step 6.
11412          */
11413         if (thflags & TH_ACK) {
11414                 /* For syn-sent we need to possibly update the rtt */
11415                 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11416                         uint32_t t, mcts;
11417
11418                         mcts = tcp_ts_getticks();
11419                         t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11420                         if (!tp->t_rttlow || tp->t_rttlow > t)
11421                                 tp->t_rttlow = t;
11422                         rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
11423                         tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11424                         tcp_rack_xmit_timer_commit(rack, tp);
11425                 }
11426                 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
11427                         return (ret_val);
11428                 /* We may have changed to FIN_WAIT_1 above */
11429                 if (tp->t_state == TCPS_FIN_WAIT_1) {
11430                         /*
11431                          * In FIN_WAIT_1 STATE in addition to the processing
11432                          * for the ESTABLISHED state if our FIN is now
11433                          * acknowledged then enter FIN_WAIT_2.
11434                          */
11435                         if (ourfinisacked) {
11436                                 /*
11437                                  * If we can't receive any more data, then
11438                                  * closing user can proceed. Starting the
11439                                  * timer is contrary to the specification,
11440                                  * but if we don't get a FIN we'll hang
11441                                  * forever.
11442                                  *
11443                                  * XXXjl: we should release the tp also, and
11444                                  * use a compressed state.
11445                                  */
11446                                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11447                                         soisdisconnected(so);
11448                                         tcp_timer_activate(tp, TT_2MSL,
11449                                             (tcp_fast_finwait2_recycle ?
11450                                             tcp_finwait2_timeout :
11451                                             TP_MAXIDLE(tp)));
11452                                 }
11453                                 tcp_state_change(tp, TCPS_FIN_WAIT_2);
11454                         }
11455                 }
11456         }
11457         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11458            tiwin, thflags, nxt_pkt));
11459 }
11460
11461 /*
11462  * Return value of 1, the TCB is unlocked and most
11463  * likely gone, return value of 0, the TCP is still
11464  * locked.
11465  */
11466 static int
11467 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
11468     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11469     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11470 {
11471         struct tcp_rack *rack;
11472         int32_t ret_val = 0;
11473         int32_t ourfinisacked = 0;
11474
11475         ctf_calc_rwin(so, tp);
11476         if ((thflags & TH_ACK) &&
11477             (SEQ_LEQ(th->th_ack, tp->snd_una) ||
11478             SEQ_GT(th->th_ack, tp->snd_max))) {
11479                 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11480                 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11481                 return (1);
11482         }
11483         rack = (struct tcp_rack *)tp->t_fb_ptr;
11484         if (IS_FASTOPEN(tp->t_flags)) {
11485                 /*
11486                  * When a TFO connection is in SYN_RECEIVED, the
11487                  * only valid packets are the initial SYN, a
11488                  * retransmit/copy of the initial SYN (possibly with
11489                  * a subset of the original data), a valid ACK, a
11490                  * FIN, or a RST.
11491                  */
11492                 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
11493                         tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11494                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11495                         return (1);
11496                 } else if (thflags & TH_SYN) {
11497                         /* non-initial SYN is ignored */
11498                         if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
11499                             (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
11500                             (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
11501                                 ctf_do_drop(m, NULL);
11502                                 return (0);
11503                         }
11504                 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
11505                         ctf_do_drop(m, NULL);
11506                         return (0);
11507                 }
11508         }
11509         if ((thflags & TH_RST) ||
11510             (tp->t_fin_is_rst && (thflags & TH_FIN)))
11511                 return (ctf_process_rst(m, th, so, tp));
11512         /*
11513          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11514          * it's less than ts_recent, drop it.
11515          */
11516         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11517             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11518                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11519                         return (ret_val);
11520         }
11521         /*
11522          * In the SYN-RECEIVED state, validate that the packet belongs to
11523          * this connection before trimming the data to fit the receive
11524          * window.  Check the sequence number versus IRS since we know the
11525          * sequence numbers haven't wrapped.  This is a partial fix for the
11526          * "LAND" DoS attack.
11527          */
11528         if (SEQ_LT(th->th_seq, tp->irs)) {
11529                 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
11530                 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11531                 return (1);
11532         }
11533         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11534                               &rack->r_ctl.challenge_ack_ts,
11535                               &rack->r_ctl.challenge_ack_cnt)) {
11536                 return (ret_val);
11537         }
11538         /*
11539          * If last ACK falls within this segment's sequence numbers, record
11540          * its timestamp. NOTE: 1) That the test incorporates suggestions
11541          * from the latest proposal of the tcplw@cray.com list (Braden
11542          * 1993/04/26). 2) That updating only on newer timestamps interferes
11543          * with our earlier PAWS tests, so this check should be solely
11544          * predicated on the sequence space of this segment. 3) That we
11545          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11546          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11547          * SEG.Len, This modified check allows us to overcome RFC1323's
11548          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11549          * p.869. In such cases, we can still calculate the RTT correctly
11550          * when RCV.NXT == Last.ACK.Sent.
11551          */
11552         if ((to->to_flags & TOF_TS) != 0 &&
11553             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11554             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11555             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11556                 tp->ts_recent_age = tcp_ts_getticks();
11557                 tp->ts_recent = to->to_tsval;
11558         }
11559         tp->snd_wnd = tiwin;
11560         rack_validate_fo_sendwin_up(tp, rack);
11561         /*
11562          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11563          * is on (half-synchronized state), then queue data for later
11564          * processing; else drop segment and return.
11565          */
11566         if ((thflags & TH_ACK) == 0) {
11567                 if (IS_FASTOPEN(tp->t_flags)) {
11568                         rack_cc_conn_init(tp);
11569                 }
11570                 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11571                     tiwin, thflags, nxt_pkt));
11572         }
11573         KMOD_TCPSTAT_INC(tcps_connects);
11574         soisconnected(so);
11575         /* Do window scaling? */
11576         if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
11577             (TF_RCVD_SCALE | TF_REQ_SCALE)) {
11578                 tp->rcv_scale = tp->request_r_scale;
11579         }
11580         /*
11581          * Make transitions: SYN-RECEIVED  -> ESTABLISHED SYN-RECEIVED* ->
11582          * FIN-WAIT-1
11583          */
11584         tp->t_starttime = ticks;
11585         if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
11586                 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
11587                 tp->t_tfo_pending = NULL;
11588         }
11589         if (tp->t_flags & TF_NEEDFIN) {
11590                 tcp_state_change(tp, TCPS_FIN_WAIT_1);
11591                 tp->t_flags &= ~TF_NEEDFIN;
11592         } else {
11593                 tcp_state_change(tp, TCPS_ESTABLISHED);
11594                 TCP_PROBE5(accept__established, NULL, tp,
11595                     mtod(m, const char *), tp, th);
11596                 /*
11597                  * TFO connections call cc_conn_init() during SYN
11598                  * processing.  Calling it again here for such connections
11599                  * is not harmless as it would undo the snd_cwnd reduction
11600                  * that occurs when a TFO SYN|ACK is retransmitted.
11601                  */
11602                 if (!IS_FASTOPEN(tp->t_flags))
11603                         rack_cc_conn_init(tp);
11604         }
11605         /*
11606          * Account for the ACK of our SYN prior to
11607          * regular ACK processing below, except for
11608          * simultaneous SYN, which is handled later.
11609          */
11610         if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
11611                 tp->snd_una++;
11612         /*
11613          * If segment contains data or ACK, will call tcp_reass() later; if
11614          * not, do so now to pass queued data to user.
11615          */
11616         if (tlen == 0 && (thflags & TH_FIN) == 0) {
11617                 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
11618                     (struct mbuf *)0);
11619                 if (tp->t_flags & TF_WAKESOR) {
11620                         tp->t_flags &= ~TF_WAKESOR;
11621                         /* NB: sorwakeup_locked() does an implicit unlock. */
11622                         sorwakeup_locked(so);
11623                 }
11624         }
11625         tp->snd_wl1 = th->th_seq - 1;
11626         /* For syn-recv we need to possibly update the rtt */
11627         if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
11628                 uint32_t t, mcts;
11629
11630                 mcts = tcp_ts_getticks();
11631                 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11632                 if (!tp->t_rttlow || tp->t_rttlow > t)
11633                         tp->t_rttlow = t;
11634                 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
11635                 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
11636                 tcp_rack_xmit_timer_commit(rack, tp);
11637         }
11638         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
11639                 return (ret_val);
11640         }
11641         if (tp->t_state == TCPS_FIN_WAIT_1) {
11642                 /* We could have went to FIN_WAIT_1 (or EST) above */
11643                 /*
11644                  * In FIN_WAIT_1 STATE in addition to the processing for the
11645                  * ESTABLISHED state if our FIN is now acknowledged then
11646                  * enter FIN_WAIT_2.
11647                  */
11648                 if (ourfinisacked) {
11649                         /*
11650                          * If we can't receive any more data, then closing
11651                          * user can proceed. Starting the timer is contrary
11652                          * to the specification, but if we don't get a FIN
11653                          * we'll hang forever.
11654                          *
11655                          * XXXjl: we should release the tp also, and use a
11656                          * compressed state.
11657                          */
11658                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11659                                 soisdisconnected(so);
11660                                 tcp_timer_activate(tp, TT_2MSL,
11661                                     (tcp_fast_finwait2_recycle ?
11662                                     tcp_finwait2_timeout :
11663                                     TP_MAXIDLE(tp)));
11664                         }
11665                         tcp_state_change(tp, TCPS_FIN_WAIT_2);
11666                 }
11667         }
11668         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11669             tiwin, thflags, nxt_pkt));
11670 }
11671
11672 /*
11673  * Return value of 1, the TCB is unlocked and most
11674  * likely gone, return value of 0, the TCP is still
11675  * locked.
11676  */
11677 static int
11678 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
11679     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11680     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11681 {
11682         int32_t ret_val = 0;
11683         struct tcp_rack *rack;
11684
11685         /*
11686          * Header prediction: check for the two common cases of a
11687          * uni-directional data xfer.  If the packet has no control flags,
11688          * is in-sequence, the window didn't change and we're not
11689          * retransmitting, it's a candidate.  If the length is zero and the
11690          * ack moved forward, we're the sender side of the xfer.  Just free
11691          * the data acked & wake any higher level process that was blocked
11692          * waiting for space.  If the length is non-zero and the ack didn't
11693          * move, we're the receiver side.  If we're getting packets in-order
11694          * (the reassembly queue is empty), add the data toc The socket
11695          * buffer and note that we need a delayed ack. Make sure that the
11696          * hidden state-flags are also off. Since we check for
11697          * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
11698          */
11699         rack = (struct tcp_rack *)tp->t_fb_ptr;
11700         if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
11701             __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
11702             __predict_true(SEGQ_EMPTY(tp)) &&
11703             __predict_true(th->th_seq == tp->rcv_nxt)) {
11704                 if (tlen == 0) {
11705                         if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
11706                             tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
11707                                 return (0);
11708                         }
11709                 } else {
11710                         if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
11711                             tiwin, nxt_pkt, iptos)) {
11712                                 return (0);
11713                         }
11714                 }
11715         }
11716         ctf_calc_rwin(so, tp);
11717
11718         if ((thflags & TH_RST) ||
11719             (tp->t_fin_is_rst && (thflags & TH_FIN)))
11720                 return (ctf_process_rst(m, th, so, tp));
11721
11722         /*
11723          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11724          * synchronized state.
11725          */
11726         if (thflags & TH_SYN) {
11727                 ctf_challenge_ack(m, th, tp, &ret_val);
11728                 return (ret_val);
11729         }
11730         /*
11731          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11732          * it's less than ts_recent, drop it.
11733          */
11734         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11735             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11736                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11737                         return (ret_val);
11738         }
11739         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11740                               &rack->r_ctl.challenge_ack_ts,
11741                               &rack->r_ctl.challenge_ack_cnt)) {
11742                 return (ret_val);
11743         }
11744         /*
11745          * If last ACK falls within this segment's sequence numbers, record
11746          * its timestamp. NOTE: 1) That the test incorporates suggestions
11747          * from the latest proposal of the tcplw@cray.com list (Braden
11748          * 1993/04/26). 2) That updating only on newer timestamps interferes
11749          * with our earlier PAWS tests, so this check should be solely
11750          * predicated on the sequence space of this segment. 3) That we
11751          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11752          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11753          * SEG.Len, This modified check allows us to overcome RFC1323's
11754          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11755          * p.869. In such cases, we can still calculate the RTT correctly
11756          * when RCV.NXT == Last.ACK.Sent.
11757          */
11758         if ((to->to_flags & TOF_TS) != 0 &&
11759             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11760             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11761             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11762                 tp->ts_recent_age = tcp_ts_getticks();
11763                 tp->ts_recent = to->to_tsval;
11764         }
11765         /*
11766          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11767          * is on (half-synchronized state), then queue data for later
11768          * processing; else drop segment and return.
11769          */
11770         if ((thflags & TH_ACK) == 0) {
11771                 if (tp->t_flags & TF_NEEDSYN) {
11772                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11773                             tiwin, thflags, nxt_pkt));
11774
11775                 } else if (tp->t_flags & TF_ACKNOW) {
11776                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11777                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11778                         return (ret_val);
11779                 } else {
11780                         ctf_do_drop(m, NULL);
11781                         return (0);
11782                 }
11783         }
11784         /*
11785          * Ack processing.
11786          */
11787         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11788                 return (ret_val);
11789         }
11790         if (sbavail(&so->so_snd)) {
11791                 if (ctf_progress_timeout_check(tp, true)) {
11792                         rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
11793                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
11794                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11795                         return (1);
11796                 }
11797         }
11798         /* State changes only happen in rack_process_data() */
11799         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11800             tiwin, thflags, nxt_pkt));
11801 }
11802
11803 /*
11804  * Return value of 1, the TCB is unlocked and most
11805  * likely gone, return value of 0, the TCP is still
11806  * locked.
11807  */
11808 static int
11809 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
11810     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11811     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11812 {
11813         int32_t ret_val = 0;
11814         struct tcp_rack *rack;
11815
11816         rack = (struct tcp_rack *)tp->t_fb_ptr;
11817         ctf_calc_rwin(so, tp);
11818         if ((thflags & TH_RST) ||
11819             (tp->t_fin_is_rst && (thflags & TH_FIN)))
11820                 return (ctf_process_rst(m, th, so, tp));
11821         /*
11822          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11823          * synchronized state.
11824          */
11825         if (thflags & TH_SYN) {
11826                 ctf_challenge_ack(m, th, tp, &ret_val);
11827                 return (ret_val);
11828         }
11829         /*
11830          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11831          * it's less than ts_recent, drop it.
11832          */
11833         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11834             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11835                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11836                         return (ret_val);
11837         }
11838         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11839                               &rack->r_ctl.challenge_ack_ts,
11840                               &rack->r_ctl.challenge_ack_cnt)) {
11841                 return (ret_val);
11842         }
11843         /*
11844          * If last ACK falls within this segment's sequence numbers, record
11845          * its timestamp. NOTE: 1) That the test incorporates suggestions
11846          * from the latest proposal of the tcplw@cray.com list (Braden
11847          * 1993/04/26). 2) That updating only on newer timestamps interferes
11848          * with our earlier PAWS tests, so this check should be solely
11849          * predicated on the sequence space of this segment. 3) That we
11850          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11851          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11852          * SEG.Len, This modified check allows us to overcome RFC1323's
11853          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11854          * p.869. In such cases, we can still calculate the RTT correctly
11855          * when RCV.NXT == Last.ACK.Sent.
11856          */
11857         if ((to->to_flags & TOF_TS) != 0 &&
11858             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11859             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11860             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11861                 tp->ts_recent_age = tcp_ts_getticks();
11862                 tp->ts_recent = to->to_tsval;
11863         }
11864         /*
11865          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
11866          * is on (half-synchronized state), then queue data for later
11867          * processing; else drop segment and return.
11868          */
11869         if ((thflags & TH_ACK) == 0) {
11870                 if (tp->t_flags & TF_NEEDSYN) {
11871                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11872                             tiwin, thflags, nxt_pkt));
11873
11874                 } else if (tp->t_flags & TF_ACKNOW) {
11875                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
11876                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
11877                         return (ret_val);
11878                 } else {
11879                         ctf_do_drop(m, NULL);
11880                         return (0);
11881                 }
11882         }
11883         /*
11884          * Ack processing.
11885          */
11886         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
11887                 return (ret_val);
11888         }
11889         if (sbavail(&so->so_snd)) {
11890                 if (ctf_progress_timeout_check(tp, true)) {
11891                         rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
11892                                                 tp, tick, PROGRESS_DROP, __LINE__);
11893                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
11894                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
11895                         return (1);
11896                 }
11897         }
11898         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11899             tiwin, thflags, nxt_pkt));
11900 }
11901
11902 static int
11903 rack_check_data_after_close(struct mbuf *m,
11904     struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
11905 {
11906         struct tcp_rack *rack;
11907
11908         rack = (struct tcp_rack *)tp->t_fb_ptr;
11909         if (rack->rc_allow_data_af_clo == 0) {
11910         close_now:
11911                 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11912                 /* tcp_close will kill the inp pre-log the Reset */
11913                 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
11914                 tp = tcp_close(tp);
11915                 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
11916                 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
11917                 return (1);
11918         }
11919         if (sbavail(&so->so_snd) == 0)
11920                 goto close_now;
11921         /* Ok we allow data that is ignored and a followup reset */
11922         tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
11923         tp->rcv_nxt = th->th_seq + *tlen;
11924         tp->t_flags2 |= TF2_DROP_AF_DATA;
11925         rack->r_wanted_output = 1;
11926         *tlen = 0;
11927         return (0);
11928 }
11929
11930 /*
11931  * Return value of 1, the TCB is unlocked and most
11932  * likely gone, return value of 0, the TCP is still
11933  * locked.
11934  */
11935 static int
11936 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
11937     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
11938     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
11939 {
11940         int32_t ret_val = 0;
11941         int32_t ourfinisacked = 0;
11942         struct tcp_rack *rack;
11943
11944         rack = (struct tcp_rack *)tp->t_fb_ptr;
11945         ctf_calc_rwin(so, tp);
11946
11947         if ((thflags & TH_RST) ||
11948             (tp->t_fin_is_rst && (thflags & TH_FIN)))
11949                 return (ctf_process_rst(m, th, so, tp));
11950         /*
11951          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11952          * synchronized state.
11953          */
11954         if (thflags & TH_SYN) {
11955                 ctf_challenge_ack(m, th, tp, &ret_val);
11956                 return (ret_val);
11957         }
11958         /*
11959          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11960          * it's less than ts_recent, drop it.
11961          */
11962         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
11963             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
11964                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
11965                         return (ret_val);
11966         }
11967         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
11968                               &rack->r_ctl.challenge_ack_ts,
11969                               &rack->r_ctl.challenge_ack_cnt)) {
11970                 return (ret_val);
11971         }
11972         /*
11973          * If new data are received on a connection after the user processes
11974          * are gone, then RST the other end.
11975          */
11976         if ((so->so_state & SS_NOFDREF) && tlen) {
11977                 if (rack_check_data_after_close(m, tp, &tlen, th, so))
11978                         return (1);
11979         }
11980         /*
11981          * If last ACK falls within this segment's sequence numbers, record
11982          * its timestamp. NOTE: 1) That the test incorporates suggestions
11983          * from the latest proposal of the tcplw@cray.com list (Braden
11984          * 1993/04/26). 2) That updating only on newer timestamps interferes
11985          * with our earlier PAWS tests, so this check should be solely
11986          * predicated on the sequence space of this segment. 3) That we
11987          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
11988          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
11989          * SEG.Len, This modified check allows us to overcome RFC1323's
11990          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
11991          * p.869. In such cases, we can still calculate the RTT correctly
11992          * when RCV.NXT == Last.ACK.Sent.
11993          */
11994         if ((to->to_flags & TOF_TS) != 0 &&
11995             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
11996             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
11997             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
11998                 tp->ts_recent_age = tcp_ts_getticks();
11999                 tp->ts_recent = to->to_tsval;
12000         }
12001         /*
12002          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12003          * is on (half-synchronized state), then queue data for later
12004          * processing; else drop segment and return.
12005          */
12006         if ((thflags & TH_ACK) == 0) {
12007                 if (tp->t_flags & TF_NEEDSYN) {
12008                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12009                             tiwin, thflags, nxt_pkt));
12010                 } else if (tp->t_flags & TF_ACKNOW) {
12011                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12012                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12013                         return (ret_val);
12014                 } else {
12015                         ctf_do_drop(m, NULL);
12016                         return (0);
12017                 }
12018         }
12019         /*
12020          * Ack processing.
12021          */
12022         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12023                 return (ret_val);
12024         }
12025         if (ourfinisacked) {
12026                 /*
12027                  * If we can't receive any more data, then closing user can
12028                  * proceed. Starting the timer is contrary to the
12029                  * specification, but if we don't get a FIN we'll hang
12030                  * forever.
12031                  *
12032                  * XXXjl: we should release the tp also, and use a
12033                  * compressed state.
12034                  */
12035                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12036                         soisdisconnected(so);
12037                         tcp_timer_activate(tp, TT_2MSL,
12038                             (tcp_fast_finwait2_recycle ?
12039                             tcp_finwait2_timeout :
12040                             TP_MAXIDLE(tp)));
12041                 }
12042                 tcp_state_change(tp, TCPS_FIN_WAIT_2);
12043         }
12044         if (sbavail(&so->so_snd)) {
12045                 if (ctf_progress_timeout_check(tp, true)) {
12046                         rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12047                                                 tp, tick, PROGRESS_DROP, __LINE__);
12048                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
12049                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12050                         return (1);
12051                 }
12052         }
12053         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12054             tiwin, thflags, nxt_pkt));
12055 }
12056
12057 /*
12058  * Return value of 1, the TCB is unlocked and most
12059  * likely gone, return value of 0, the TCP is still
12060  * locked.
12061  */
12062 static int
12063 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
12064     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12065     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12066 {
12067         int32_t ret_val = 0;
12068         int32_t ourfinisacked = 0;
12069         struct tcp_rack *rack;
12070
12071         rack = (struct tcp_rack *)tp->t_fb_ptr;
12072         ctf_calc_rwin(so, tp);
12073
12074         if ((thflags & TH_RST) ||
12075             (tp->t_fin_is_rst && (thflags & TH_FIN)))
12076                 return (ctf_process_rst(m, th, so, tp));
12077         /*
12078          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12079          * synchronized state.
12080          */
12081         if (thflags & TH_SYN) {
12082                 ctf_challenge_ack(m, th, tp, &ret_val);
12083                 return (ret_val);
12084         }
12085         /*
12086          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12087          * it's less than ts_recent, drop it.
12088          */
12089         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12090             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12091                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12092                         return (ret_val);
12093         }
12094         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12095                               &rack->r_ctl.challenge_ack_ts,
12096                               &rack->r_ctl.challenge_ack_cnt)) {
12097                 return (ret_val);
12098         }
12099         /*
12100          * If new data are received on a connection after the user processes
12101          * are gone, then RST the other end.
12102          */
12103         if ((so->so_state & SS_NOFDREF) && tlen) {
12104                 if (rack_check_data_after_close(m, tp, &tlen, th, so))
12105                         return (1);
12106         }
12107         /*
12108          * If last ACK falls within this segment's sequence numbers, record
12109          * its timestamp. NOTE: 1) That the test incorporates suggestions
12110          * from the latest proposal of the tcplw@cray.com list (Braden
12111          * 1993/04/26). 2) That updating only on newer timestamps interferes
12112          * with our earlier PAWS tests, so this check should be solely
12113          * predicated on the sequence space of this segment. 3) That we
12114          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12115          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12116          * SEG.Len, This modified check allows us to overcome RFC1323's
12117          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12118          * p.869. In such cases, we can still calculate the RTT correctly
12119          * when RCV.NXT == Last.ACK.Sent.
12120          */
12121         if ((to->to_flags & TOF_TS) != 0 &&
12122             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12123             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12124             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12125                 tp->ts_recent_age = tcp_ts_getticks();
12126                 tp->ts_recent = to->to_tsval;
12127         }
12128         /*
12129          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12130          * is on (half-synchronized state), then queue data for later
12131          * processing; else drop segment and return.
12132          */
12133         if ((thflags & TH_ACK) == 0) {
12134                 if (tp->t_flags & TF_NEEDSYN) {
12135                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12136                             tiwin, thflags, nxt_pkt));
12137                 } else if (tp->t_flags & TF_ACKNOW) {
12138                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12139                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12140                         return (ret_val);
12141                 } else {
12142                         ctf_do_drop(m, NULL);
12143                         return (0);
12144                 }
12145         }
12146         /*
12147          * Ack processing.
12148          */
12149         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12150                 return (ret_val);
12151         }
12152         if (ourfinisacked) {
12153                 tcp_twstart(tp);
12154                 m_freem(m);
12155                 return (1);
12156         }
12157         if (sbavail(&so->so_snd)) {
12158                 if (ctf_progress_timeout_check(tp, true)) {
12159                         rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12160                                                 tp, tick, PROGRESS_DROP, __LINE__);
12161                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
12162                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12163                         return (1);
12164                 }
12165         }
12166         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12167             tiwin, thflags, nxt_pkt));
12168 }
12169
12170 /*
12171  * Return value of 1, the TCB is unlocked and most
12172  * likely gone, return value of 0, the TCP is still
12173  * locked.
12174  */
12175 static int
12176 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12177     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12178     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12179 {
12180         int32_t ret_val = 0;
12181         int32_t ourfinisacked = 0;
12182         struct tcp_rack *rack;
12183
12184         rack = (struct tcp_rack *)tp->t_fb_ptr;
12185         ctf_calc_rwin(so, tp);
12186
12187         if ((thflags & TH_RST) ||
12188             (tp->t_fin_is_rst && (thflags & TH_FIN)))
12189                 return (ctf_process_rst(m, th, so, tp));
12190         /*
12191          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12192          * synchronized state.
12193          */
12194         if (thflags & TH_SYN) {
12195                 ctf_challenge_ack(m, th, tp, &ret_val);
12196                 return (ret_val);
12197         }
12198         /*
12199          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12200          * it's less than ts_recent, drop it.
12201          */
12202         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12203             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12204                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12205                         return (ret_val);
12206         }
12207         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12208                               &rack->r_ctl.challenge_ack_ts,
12209                               &rack->r_ctl.challenge_ack_cnt)) {
12210                 return (ret_val);
12211         }
12212         /*
12213          * If new data are received on a connection after the user processes
12214          * are gone, then RST the other end.
12215          */
12216         if ((so->so_state & SS_NOFDREF) && tlen) {
12217                 if (rack_check_data_after_close(m, tp, &tlen, th, so))
12218                         return (1);
12219         }
12220         /*
12221          * If last ACK falls within this segment's sequence numbers, record
12222          * its timestamp. NOTE: 1) That the test incorporates suggestions
12223          * from the latest proposal of the tcplw@cray.com list (Braden
12224          * 1993/04/26). 2) That updating only on newer timestamps interferes
12225          * with our earlier PAWS tests, so this check should be solely
12226          * predicated on the sequence space of this segment. 3) That we
12227          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12228          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12229          * SEG.Len, This modified check allows us to overcome RFC1323's
12230          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12231          * p.869. In such cases, we can still calculate the RTT correctly
12232          * when RCV.NXT == Last.ACK.Sent.
12233          */
12234         if ((to->to_flags & TOF_TS) != 0 &&
12235             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12236             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12237             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12238                 tp->ts_recent_age = tcp_ts_getticks();
12239                 tp->ts_recent = to->to_tsval;
12240         }
12241         /*
12242          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12243          * is on (half-synchronized state), then queue data for later
12244          * processing; else drop segment and return.
12245          */
12246         if ((thflags & TH_ACK) == 0) {
12247                 if (tp->t_flags & TF_NEEDSYN) {
12248                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12249                             tiwin, thflags, nxt_pkt));
12250                 } else if (tp->t_flags & TF_ACKNOW) {
12251                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12252                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12253                         return (ret_val);
12254                 } else {
12255                         ctf_do_drop(m, NULL);
12256                         return (0);
12257                 }
12258         }
12259         /*
12260          * case TCPS_LAST_ACK: Ack processing.
12261          */
12262         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12263                 return (ret_val);
12264         }
12265         if (ourfinisacked) {
12266                 tp = tcp_close(tp);
12267                 ctf_do_drop(m, tp);
12268                 return (1);
12269         }
12270         if (sbavail(&so->so_snd)) {
12271                 if (ctf_progress_timeout_check(tp, true)) {
12272                         rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12273                                                 tp, tick, PROGRESS_DROP, __LINE__);
12274                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
12275                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12276                         return (1);
12277                 }
12278         }
12279         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12280             tiwin, thflags, nxt_pkt));
12281 }
12282
12283 /*
12284  * Return value of 1, the TCB is unlocked and most
12285  * likely gone, return value of 0, the TCP is still
12286  * locked.
12287  */
12288 static int
12289 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
12290     struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12291     uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
12292 {
12293         int32_t ret_val = 0;
12294         int32_t ourfinisacked = 0;
12295         struct tcp_rack *rack;
12296
12297         rack = (struct tcp_rack *)tp->t_fb_ptr;
12298         ctf_calc_rwin(so, tp);
12299
12300         /* Reset receive buffer auto scaling when not in bulk receive mode. */
12301         if ((thflags & TH_RST) ||
12302             (tp->t_fin_is_rst && (thflags & TH_FIN)))
12303                 return (ctf_process_rst(m, th, so, tp));
12304         /*
12305          * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12306          * synchronized state.
12307          */
12308         if (thflags & TH_SYN) {
12309                 ctf_challenge_ack(m, th, tp, &ret_val);
12310                 return (ret_val);
12311         }
12312         /*
12313          * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12314          * it's less than ts_recent, drop it.
12315          */
12316         if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
12317             TSTMP_LT(to->to_tsval, tp->ts_recent)) {
12318                 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
12319                         return (ret_val);
12320         }
12321         if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
12322                               &rack->r_ctl.challenge_ack_ts,
12323                               &rack->r_ctl.challenge_ack_cnt)) {
12324                 return (ret_val);
12325         }
12326         /*
12327          * If new data are received on a connection after the user processes
12328          * are gone, then RST the other end.
12329          */
12330         if ((so->so_state & SS_NOFDREF) &&
12331             tlen) {
12332                 if (rack_check_data_after_close(m, tp, &tlen, th, so))
12333                         return (1);
12334         }
12335         /*
12336          * If last ACK falls within this segment's sequence numbers, record
12337          * its timestamp. NOTE: 1) That the test incorporates suggestions
12338          * from the latest proposal of the tcplw@cray.com list (Braden
12339          * 1993/04/26). 2) That updating only on newer timestamps interferes
12340          * with our earlier PAWS tests, so this check should be solely
12341          * predicated on the sequence space of this segment. 3) That we
12342          * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
12343          * + SEG.Len  instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
12344          * SEG.Len, This modified check allows us to overcome RFC1323's
12345          * limitations as described in Stevens TCP/IP Illustrated Vol. 2
12346          * p.869. In such cases, we can still calculate the RTT correctly
12347          * when RCV.NXT == Last.ACK.Sent.
12348          */
12349         if ((to->to_flags & TOF_TS) != 0 &&
12350             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
12351             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
12352             ((thflags & (TH_SYN | TH_FIN)) != 0))) {
12353                 tp->ts_recent_age = tcp_ts_getticks();
12354                 tp->ts_recent = to->to_tsval;
12355         }
12356         /*
12357          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN flag
12358          * is on (half-synchronized state), then queue data for later
12359          * processing; else drop segment and return.
12360          */
12361         if ((thflags & TH_ACK) == 0) {
12362                 if (tp->t_flags & TF_NEEDSYN) {
12363                         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12364                             tiwin, thflags, nxt_pkt));
12365                 } else if (tp->t_flags & TF_ACKNOW) {
12366                         ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
12367                         ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
12368                         return (ret_val);
12369                 } else {
12370                         ctf_do_drop(m, NULL);
12371                         return (0);
12372                 }
12373         }
12374         /*
12375          * Ack processing.
12376          */
12377         if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12378                 return (ret_val);
12379         }
12380         if (sbavail(&so->so_snd)) {
12381                 if (ctf_progress_timeout_check(tp, true)) {
12382                         rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
12383                                                 tp, tick, PROGRESS_DROP, __LINE__);
12384                         tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
12385                         ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
12386                         return (1);
12387                 }
12388         }
12389         return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12390             tiwin, thflags, nxt_pkt));
12391 }
12392
12393 static void inline
12394 rack_clear_rate_sample(struct tcp_rack *rack)
12395 {
12396         rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
12397         rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
12398         rack->r_ctl.rack_rs.rs_rtt_tot = 0;
12399 }
12400
12401 static void
12402 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
12403 {
12404         uint64_t bw_est, rate_wanted;
12405         int chged = 0;
12406         uint32_t user_max, orig_min, orig_max;
12407
12408         orig_min = rack->r_ctl.rc_pace_min_segs;
12409         orig_max = rack->r_ctl.rc_pace_max_segs;
12410         user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
12411         if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
12412                 chged = 1;
12413         rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
12414         if (rack->use_fixed_rate || rack->rc_force_max_seg) {
12415                 if (user_max != rack->r_ctl.rc_pace_max_segs)
12416                         chged = 1;
12417         }
12418         if (rack->rc_force_max_seg) {
12419                 rack->r_ctl.rc_pace_max_segs = user_max;
12420         } else if (rack->use_fixed_rate) {
12421                 bw_est = rack_get_bw(rack);
12422                 if ((rack->r_ctl.crte == NULL) ||
12423                     (bw_est != rack->r_ctl.crte->rate)) {
12424                         rack->r_ctl.rc_pace_max_segs = user_max;
12425                 } else {
12426                         /* We are pacing right at the hardware rate */
12427                         uint32_t segsiz;
12428
12429                         segsiz = min(ctf_fixed_maxseg(tp),
12430                                      rack->r_ctl.rc_pace_min_segs);
12431                         rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(
12432                                                            tp, bw_est, segsiz, 0,
12433                                                            rack->r_ctl.crte, NULL);
12434                 }
12435         } else if (rack->rc_always_pace) {
12436                 if (rack->r_ctl.gp_bw ||
12437 #ifdef NETFLIX_PEAKRATE
12438                     rack->rc_tp->t_maxpeakrate ||
12439 #endif
12440                     rack->r_ctl.init_rate) {
12441                         /* We have a rate of some sort set */
12442                         uint32_t  orig;
12443
12444                         bw_est = rack_get_bw(rack);
12445                         orig = rack->r_ctl.rc_pace_max_segs;
12446                         if (fill_override)
12447                                 rate_wanted = *fill_override;
12448                         else
12449                                 rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL);
12450                         if (rate_wanted) {
12451                                 /* We have something */
12452                                 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
12453                                                                                    rate_wanted,
12454                                                                                    ctf_fixed_maxseg(rack->rc_tp));
12455                         } else
12456                                 rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
12457                         if (orig != rack->r_ctl.rc_pace_max_segs)
12458                                 chged = 1;
12459                 } else if ((rack->r_ctl.gp_bw == 0) &&
12460                            (rack->r_ctl.rc_pace_max_segs == 0)) {
12461                         /*
12462                          * If we have nothing limit us to bursting
12463                          * out IW sized pieces.
12464                          */
12465                         chged = 1;
12466                         rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
12467                 }
12468         }
12469         if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
12470                 chged = 1;
12471                 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
12472         }
12473         if (chged)
12474                 rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
12475 }
12476
12477
12478 static void
12479 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack)
12480 {
12481 #ifdef INET6
12482         struct ip6_hdr *ip6 = NULL;
12483 #endif
12484 #ifdef INET
12485         struct ip *ip = NULL;
12486 #endif
12487         struct udphdr *udp = NULL;
12488
12489         /* Ok lets fill in the fast block, it can only be used with no IP options! */
12490 #ifdef INET6
12491         if (rack->r_is_v6) {
12492                 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12493                 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
12494                 if (tp->t_port) {
12495                         rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12496                         udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
12497                         udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12498                         udp->uh_dport = tp->t_port;
12499                         rack->r_ctl.fsb.udp = udp;
12500                         rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12501                 } else
12502                 {
12503                         rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
12504                         rack->r_ctl.fsb.udp = NULL;
12505                 }
12506                 tcpip_fillheaders(rack->rc_inp,
12507                                   tp->t_port,
12508                                   ip6, rack->r_ctl.fsb.th);
12509         } else
12510 #endif                          /* INET6 */
12511         {
12512                 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
12513                 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
12514                 if (tp->t_port) {
12515                         rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
12516                         udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
12517                         udp->uh_sport = htons(V_tcp_udp_tunneling_port);
12518                         udp->uh_dport = tp->t_port;
12519                         rack->r_ctl.fsb.udp = udp;
12520                         rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
12521                 } else
12522                 {
12523                         rack->r_ctl.fsb.udp = NULL;
12524                         rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
12525                 }
12526                 tcpip_fillheaders(rack->rc_inp,
12527                                   tp->t_port,
12528                                   ip, rack->r_ctl.fsb.th);
12529         }
12530         rack->r_fsb_inited = 1;
12531 }
12532
12533 static int
12534 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
12535 {
12536         /*
12537          * Allocate the larger of spaces V6 if available else just
12538          * V4 and include udphdr (overbook)
12539          */
12540 #ifdef INET6
12541         rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
12542 #else
12543         rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
12544 #endif
12545         rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
12546                                             M_TCPFSB, M_NOWAIT|M_ZERO);
12547         if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
12548                 return (ENOMEM);
12549         }
12550         rack->r_fsb_inited = 0;
12551         return (0);
12552 }
12553
12554 static int
12555 rack_init(struct tcpcb *tp)
12556 {
12557         struct tcp_rack *rack = NULL;
12558         struct rack_sendmap *insret;
12559         uint32_t iwin, snt, us_cts;
12560         int err;
12561
12562         tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
12563         if (tp->t_fb_ptr == NULL) {
12564                 /*
12565                  * We need to allocate memory but cant. The INP and INP_INFO
12566                  * locks and they are recusive (happens during setup. So a
12567                  * scheme to drop the locks fails :(
12568                  *
12569                  */
12570                 return (ENOMEM);
12571         }
12572         memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
12573
12574         rack = (struct tcp_rack *)tp->t_fb_ptr;
12575         RB_INIT(&rack->r_ctl.rc_mtree);
12576         TAILQ_INIT(&rack->r_ctl.rc_free);
12577         TAILQ_INIT(&rack->r_ctl.rc_tmap);
12578         rack->rc_tp = tp;
12579         rack->rc_inp = tp->t_inpcb;
12580         /* Set the flag */
12581         rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
12582         /* Probably not needed but lets be sure */
12583         rack_clear_rate_sample(rack);
12584         /*
12585          * Save off the default values, socket options will poke
12586          * at these if pacing is not on or we have not yet
12587          * reached where pacing is on (gp_ready/fixed enabled).
12588          * When they get set into the CC module (when gp_ready
12589          * is enabled or we enable fixed) then we will set these
12590          * values into the CC and place in here the old values
12591          * so we have a restoral. Then we will set the flag
12592          * rc_pacing_cc_set. That way whenever we turn off pacing
12593          * or switch off this stack, we will know to go restore
12594          * the saved values.
12595          */
12596         rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
12597         rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
12598         /* We want abe like behavior as well */
12599         rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
12600         rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
12601         rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
12602         rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
12603         rack->r_ctl.roundends = tp->snd_max;
12604         if (use_rack_rr)
12605                 rack->use_rack_rr = 1;
12606         if (V_tcp_delack_enabled)
12607                 tp->t_delayed_ack = 1;
12608         else
12609                 tp->t_delayed_ack = 0;
12610 #ifdef TCP_ACCOUNTING
12611         if (rack_tcp_accounting) {
12612                 tp->t_flags2 |= TF2_TCP_ACCOUNTING;
12613         }
12614 #endif
12615         if (rack_enable_shared_cwnd)
12616                 rack->rack_enable_scwnd = 1;
12617         rack->rc_user_set_max_segs = rack_hptsi_segments;
12618         rack->rc_force_max_seg = 0;
12619         if (rack_use_imac_dack)
12620                 rack->rc_dack_mode = 1;
12621         TAILQ_INIT(&rack->r_ctl.opt_list);
12622         rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
12623         rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
12624         rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
12625         rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
12626         rack->r_ctl.rc_highest_us_rtt = 0;
12627         rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
12628         rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
12629         if (rack_use_cmp_acks)
12630                 rack->r_use_cmp_ack = 1;
12631         if (rack_disable_prr)
12632                 rack->rack_no_prr = 1;
12633         if (rack_gp_no_rec_chg)
12634                 rack->rc_gp_no_rec_chg = 1;
12635         if (rack_pace_every_seg && tcp_can_enable_pacing()) {
12636                 rack->rc_always_pace = 1;
12637                 if (rack->use_fixed_rate || rack->gp_ready)
12638                         rack_set_cc_pacing(rack);
12639         } else
12640                 rack->rc_always_pace = 0;
12641         if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
12642                 rack->r_mbuf_queue = 1;
12643         else
12644                 rack->r_mbuf_queue = 0;
12645         if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
12646                 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
12647         else
12648                 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
12649         rack_set_pace_segments(tp, rack, __LINE__, NULL);
12650         if (rack_limits_scwnd)
12651                 rack->r_limit_scw = 1;
12652         else
12653                 rack->r_limit_scw = 0;
12654         rack->rc_labc = V_tcp_abc_l_var;
12655         rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
12656         rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
12657         rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
12658         rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
12659         rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
12660         rack->r_ctl.rc_min_to = rack_min_to;
12661         microuptime(&rack->r_ctl.act_rcv_time);
12662         rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
12663         rack->r_running_late = 0;
12664         rack->r_running_early = 0;
12665         rack->rc_init_win = rack_default_init_window;
12666         rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
12667         if (rack_hw_up_only)
12668                 rack->r_up_only = 1;
12669         if (rack_do_dyn_mul) {
12670                 /* When dynamic adjustment is on CA needs to start at 100% */
12671                 rack->rc_gp_dyn_mul = 1;
12672                 if (rack_do_dyn_mul >= 100)
12673                         rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
12674         } else
12675                 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
12676         rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
12677         rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
12678         rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
12679         setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
12680                                 rack_probertt_filter_life);
12681         us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
12682         rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
12683         rack->r_ctl.rc_time_of_last_probertt = us_cts;
12684         rack->r_ctl.challenge_ack_ts = tcp_ts_getticks();
12685         rack->r_ctl.rc_time_probertt_starts = 0;
12686         if (rack_dsack_std_based & 0x1) {
12687                 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
12688                 rack->rc_rack_tmr_std_based = 1;
12689         }
12690         if (rack_dsack_std_based & 0x2) {
12691                 /* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
12692                 rack->rc_rack_use_dsack = 1;
12693         }
12694         /* We require at least one measurement, even if the sysctl is 0 */
12695         if (rack_req_measurements)
12696                 rack->r_ctl.req_measurements = rack_req_measurements;
12697         else
12698                 rack->r_ctl.req_measurements = 1;
12699         if (rack_enable_hw_pacing)
12700                 rack->rack_hdw_pace_ena = 1;
12701         if (rack_hw_rate_caps)
12702                 rack->r_rack_hw_rate_caps = 1;
12703         /* Do we force on detection? */
12704 #ifdef NETFLIX_EXP_DETECTION
12705         if (tcp_force_detection)
12706                 rack->do_detection = 1;
12707         else
12708 #endif
12709                 rack->do_detection = 0;
12710         if (rack_non_rxt_use_cr)
12711                 rack->rack_rec_nonrxt_use_cr = 1;
12712         err = rack_init_fsb(tp, rack);
12713         if (err) {
12714                 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12715                 tp->t_fb_ptr = NULL;
12716                 return (err);
12717         }
12718         if (tp->snd_una != tp->snd_max) {
12719                 /* Create a send map for the current outstanding data */
12720                 struct rack_sendmap *rsm;
12721
12722                 rsm = rack_alloc(rack);
12723                 if (rsm == NULL) {
12724                         uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12725                         tp->t_fb_ptr = NULL;
12726                         return (ENOMEM);
12727                 }
12728                 rsm->r_no_rtt_allowed = 1;
12729                 rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
12730                 rsm->r_rtr_cnt = 1;
12731                 rsm->r_rtr_bytes = 0;
12732                 if (tp->t_flags & TF_SENTFIN) {
12733                         rsm->r_end = tp->snd_max - 1;
12734                         rsm->r_flags |= RACK_HAS_FIN;
12735                 } else {
12736                         rsm->r_end = tp->snd_max;
12737                 }
12738                 if (tp->snd_una == tp->iss) {
12739                         /* The data space is one beyond snd_una */
12740                         rsm->r_flags |= RACK_HAS_SYN;
12741                         rsm->r_start = tp->iss;
12742                         rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
12743                 } else
12744                         rsm->r_start = tp->snd_una;
12745                 rsm->r_dupack = 0;
12746                 if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
12747                         rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
12748                         if (rsm->m)
12749                                 rsm->orig_m_len = rsm->m->m_len;
12750                         else
12751                                 rsm->orig_m_len = 0;
12752                 } else {
12753                         /*
12754                          * This can happen if we have a stand-alone FIN or
12755                          *  SYN.
12756                          */
12757                         rsm->m = NULL;
12758                         rsm->orig_m_len = 0;
12759                         rsm->soff = 0;
12760                 }
12761                 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12762 #ifdef INVARIANTS
12763                 if (insret != NULL) {
12764                         panic("Insert in rb tree fails ret:%p rack:%p rsm:%p",
12765                               insret, rack, rsm);
12766                 }
12767 #endif
12768                 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
12769                 rsm->r_in_tmap = 1;
12770         }
12771         /*
12772          * Timers in Rack are kept in microseconds so lets
12773          * convert any initial incoming variables
12774          * from ticks into usecs. Note that we
12775          * also change the values of t_srtt and t_rttvar, if
12776          * they are non-zero. They are kept with a 5
12777          * bit decimal so we have to carefully convert
12778          * these to get the full precision.
12779          */
12780         rack_convert_rtts(tp);
12781         tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow);
12782         if (rack_do_hystart) {
12783                 struct sockopt sopt;
12784                 struct cc_newreno_opts opt;
12785
12786                 sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
12787                 sopt.sopt_dir = SOPT_SET;
12788                 opt.name = CC_NEWRENO_ENABLE_HYSTART;
12789                 opt.val = rack_do_hystart;
12790                 if (CC_ALGO(tp)->ctl_output != NULL)
12791                         (void)CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
12792         }
12793         if (rack_def_profile)
12794                 rack_set_profile(rack, rack_def_profile);
12795         /* Cancel the GP measurement in progress */
12796         tp->t_flags &= ~TF_GPUTINPROG;
12797         if (SEQ_GT(tp->snd_max, tp->iss))
12798                 snt = tp->snd_max - tp->iss;
12799         else
12800                 snt = 0;
12801         iwin = rc_init_window(rack);
12802         if (snt < iwin) {
12803                 /* We are not past the initial window
12804                  * so we need to make sure cwnd is
12805                  * correct.
12806                  */
12807                 if (tp->snd_cwnd < iwin)
12808                         tp->snd_cwnd = iwin;
12809                 /*
12810                  * If we are within the initial window
12811                  * we want ssthresh to be unlimited. Setting
12812                  * it to the rwnd (which the default stack does
12813                  * and older racks) is not really a good idea
12814                  * since we want to be in SS and grow both the
12815                  * cwnd and the rwnd (via dynamic rwnd growth). If
12816                  * we set it to the rwnd then as the peer grows its
12817                  * rwnd we will be stuck in CA and never hit SS.
12818                  *
12819                  * Its far better to raise it up high (this takes the
12820                  * risk that there as been a loss already, probably
12821                  * we should have an indicator in all stacks of loss
12822                  * but we don't), but considering the normal use this
12823                  * is a risk worth taking. The consequences of not
12824                  * hitting SS are far worse than going one more time
12825                  * into it early on (before we have sent even a IW).
12826                  * It is highly unlikely that we will have had a loss
12827                  * before getting the IW out.
12828                  */
12829                 tp->snd_ssthresh = 0xffffffff;
12830         }
12831         rack_stop_all_timers(tp);
12832         /* Lets setup the fsb block */
12833         rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
12834         rack_log_rtt_shrinks(rack,  us_cts,  tp->t_rxtcur,
12835                              __LINE__, RACK_RTTS_INIT);
12836         return (0);
12837 }
12838
12839 static int
12840 rack_handoff_ok(struct tcpcb *tp)
12841 {
12842         if ((tp->t_state == TCPS_CLOSED) ||
12843             (tp->t_state == TCPS_LISTEN)) {
12844                 /* Sure no problem though it may not stick */
12845                 return (0);
12846         }
12847         if ((tp->t_state == TCPS_SYN_SENT) ||
12848             (tp->t_state == TCPS_SYN_RECEIVED)) {
12849                 /*
12850                  * We really don't know if you support sack,
12851                  * you have to get to ESTAB or beyond to tell.
12852                  */
12853                 return (EAGAIN);
12854         }
12855         if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
12856                 /*
12857                  * Rack will only send a FIN after all data is acknowledged.
12858                  * So in this case we have more data outstanding. We can't
12859                  * switch stacks until either all data and only the FIN
12860                  * is left (in which case rack_init() now knows how
12861                  * to deal with that) <or> all is acknowledged and we
12862                  * are only left with incoming data, though why you
12863                  * would want to switch to rack after all data is acknowledged
12864                  * I have no idea (rrs)!
12865                  */
12866                 return (EAGAIN);
12867         }
12868         if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
12869                 return (0);
12870         }
12871         /*
12872          * If we reach here we don't do SACK on this connection so we can
12873          * never do rack.
12874          */
12875         return (EINVAL);
12876 }
12877
12878
12879 static void
12880 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
12881 {
12882         int ack_cmp = 0;
12883
12884         if (tp->t_fb_ptr) {
12885                 struct tcp_rack *rack;
12886                 struct rack_sendmap *rsm, *nrsm, *rm;
12887
12888                 rack = (struct tcp_rack *)tp->t_fb_ptr;
12889                 if (tp->t_in_pkt) {
12890                         /*
12891                          * It is unsafe to process the packets since a
12892                          * reset may be lurking in them (its rare but it
12893                          * can occur). If we were to find a RST, then we
12894                          * would end up dropping the connection and the
12895                          * INP lock, so when we return the caller (tcp_usrreq)
12896                          * will blow up when it trys to unlock the inp.
12897                          */
12898                         struct mbuf *save, *m;
12899
12900                         m = tp->t_in_pkt;
12901                         tp->t_in_pkt = NULL;
12902                         tp->t_tail_pkt = NULL;
12903                         while (m) {
12904                                 save = m->m_nextpkt;
12905                                 m->m_nextpkt = NULL;
12906                                 m_freem(m);
12907                                 m = save;
12908                         }
12909                         if ((tp->t_inpcb) &&
12910                             (tp->t_inpcb->inp_flags2 & INP_MBUF_ACKCMP))
12911                                 ack_cmp = 1;
12912                         if (ack_cmp) {
12913                                 /* Total if we used large or small (if ack-cmp was used). */
12914                                 if (rack->rc_inp->inp_flags2 & INP_MBUF_L_ACKS)
12915                                         counter_u64_add(rack_large_ackcmp, 1);
12916                                 else
12917                                         counter_u64_add(rack_small_ackcmp, 1);
12918                         }
12919                 }
12920                 tp->t_flags &= ~TF_FORCEDATA;
12921 #ifdef NETFLIX_SHARED_CWND
12922                 if (rack->r_ctl.rc_scw) {
12923                         uint32_t limit;
12924
12925                         if (rack->r_limit_scw)
12926                                 limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
12927                         else
12928                                 limit = 0;
12929                         tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
12930                                                   rack->r_ctl.rc_scw_index,
12931                                                   limit);
12932                         rack->r_ctl.rc_scw = NULL;
12933                 }
12934 #endif
12935                 if (rack->r_ctl.fsb.tcp_ip_hdr) {
12936                         free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
12937                         rack->r_ctl.fsb.tcp_ip_hdr = NULL;
12938                         rack->r_ctl.fsb.th = NULL;
12939                 }
12940                 /* Convert back to ticks, with  */
12941                 if (tp->t_srtt > 1) {
12942                         uint32_t val, frac;
12943
12944                         val = USEC_2_TICKS(tp->t_srtt);
12945                         frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
12946                         tp->t_srtt = val << TCP_RTT_SHIFT;
12947                         /*
12948                          * frac is the fractional part here is left
12949                          * over from converting to hz and shifting.
12950                          * We need to convert this to the 5 bit
12951                          * remainder.
12952                          */
12953                         if (frac) {
12954                                 if (hz == 1000) {
12955                                         frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12956                                 } else {
12957                                         frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12958                                 }
12959                                 tp->t_srtt += frac;
12960                         }
12961                 }
12962                 if (tp->t_rttvar) {
12963                         uint32_t val, frac;
12964
12965                         val = USEC_2_TICKS(tp->t_rttvar);
12966                         frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
12967                         tp->t_rttvar = val <<  TCP_RTTVAR_SHIFT;
12968                         /*
12969                          * frac is the fractional part here is left
12970                          * over from converting to hz and shifting.
12971                          * We need to convert this to the 5 bit
12972                          * remainder.
12973                          */
12974                         if (frac) {
12975                                 if (hz == 1000) {
12976                                         frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12977                                 } else {
12978                                         frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12979                                 }
12980                                 tp->t_rttvar += frac;
12981                         }
12982                 }
12983                 tp->t_rxtcur = USEC_2_TICKS(tp->t_rxtcur);
12984                 tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow);
12985                 if (rack->rc_always_pace) {
12986                         tcp_decrement_paced_conn();
12987                         rack_undo_cc_pacing(rack);
12988                         rack->rc_always_pace = 0;
12989                 }
12990                 /* Clean up any options if they were not applied */
12991                 while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
12992                         struct deferred_opt_list *dol;
12993
12994                         dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
12995                         TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
12996                         free(dol, M_TCPDO);
12997                 }
12998                 /* rack does not use force data but other stacks may clear it */
12999                 if (rack->r_ctl.crte != NULL) {
13000                         tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
13001                         rack->rack_hdrw_pacing = 0;
13002                         rack->r_ctl.crte = NULL;
13003                 }
13004 #ifdef TCP_BLACKBOX
13005                 tcp_log_flowend(tp);
13006 #endif
13007                 RB_FOREACH_SAFE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm) {
13008                         rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
13009 #ifdef INVARIANTS
13010                         if (rm != rsm) {
13011                                 panic("At fini, rack:%p rsm:%p rm:%p",
13012                                       rack, rsm, rm);
13013                         }
13014 #endif
13015                         uma_zfree(rack_zone, rsm);
13016                 }
13017                 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
13018                 while (rsm) {
13019                         TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
13020                         uma_zfree(rack_zone, rsm);
13021                         rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
13022                 }
13023                 rack->rc_free_cnt = 0;
13024                 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
13025                 tp->t_fb_ptr = NULL;
13026         }
13027         if (tp->t_inpcb) {
13028                 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
13029                 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
13030                 tp->t_inpcb->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
13031                 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_ACKCMP;
13032                 /* Cancel the GP measurement in progress */
13033                 tp->t_flags &= ~TF_GPUTINPROG;
13034                 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_L_ACKS;
13035         }
13036         /* Make sure snd_nxt is correctly set */
13037         tp->snd_nxt = tp->snd_max;
13038 }
13039
13040 static void
13041 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
13042 {
13043         if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
13044                 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
13045         }
13046         switch (tp->t_state) {
13047         case TCPS_SYN_SENT:
13048                 rack->r_state = TCPS_SYN_SENT;
13049                 rack->r_substate = rack_do_syn_sent;
13050                 break;
13051         case TCPS_SYN_RECEIVED:
13052                 rack->r_state = TCPS_SYN_RECEIVED;
13053                 rack->r_substate = rack_do_syn_recv;
13054                 break;
13055         case TCPS_ESTABLISHED:
13056                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13057                 rack->r_state = TCPS_ESTABLISHED;
13058                 rack->r_substate = rack_do_established;
13059                 break;
13060         case TCPS_CLOSE_WAIT:
13061                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13062                 rack->r_state = TCPS_CLOSE_WAIT;
13063                 rack->r_substate = rack_do_close_wait;
13064                 break;
13065         case TCPS_FIN_WAIT_1:
13066                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13067                 rack->r_state = TCPS_FIN_WAIT_1;
13068                 rack->r_substate = rack_do_fin_wait_1;
13069                 break;
13070         case TCPS_CLOSING:
13071                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13072                 rack->r_state = TCPS_CLOSING;
13073                 rack->r_substate = rack_do_closing;
13074                 break;
13075         case TCPS_LAST_ACK:
13076                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13077                 rack->r_state = TCPS_LAST_ACK;
13078                 rack->r_substate = rack_do_lastack;
13079                 break;
13080         case TCPS_FIN_WAIT_2:
13081                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13082                 rack->r_state = TCPS_FIN_WAIT_2;
13083                 rack->r_substate = rack_do_fin_wait_2;
13084                 break;
13085         case TCPS_LISTEN:
13086         case TCPS_CLOSED:
13087         case TCPS_TIME_WAIT:
13088         default:
13089                 break;
13090         };
13091         if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
13092                 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
13093
13094 }
13095
13096 static void
13097 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
13098 {
13099         /*
13100          * We received an ack, and then did not
13101          * call send or were bounced out due to the
13102          * hpts was running. Now a timer is up as well, is
13103          * it the right timer?
13104          */
13105         struct rack_sendmap *rsm;
13106         int tmr_up;
13107
13108         tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
13109         if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
13110                 return;
13111         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
13112         if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
13113             (tmr_up == PACE_TMR_RXT)) {
13114                 /* Should be an RXT */
13115                 return;
13116         }
13117         if (rsm == NULL) {
13118                 /* Nothing outstanding? */
13119                 if (tp->t_flags & TF_DELACK) {
13120                         if (tmr_up == PACE_TMR_DELACK)
13121                                 /* We are supposed to have delayed ack up and we do */
13122                                 return;
13123                 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
13124                         /*
13125                          * if we hit enobufs then we would expect the possiblity
13126                          * of nothing outstanding and the RXT up (and the hptsi timer).
13127                          */
13128                         return;
13129                 } else if (((V_tcp_always_keepalive ||
13130                              rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
13131                             (tp->t_state <= TCPS_CLOSING)) &&
13132                            (tmr_up == PACE_TMR_KEEP) &&
13133                            (tp->snd_max == tp->snd_una)) {
13134                         /* We should have keep alive up and we do */
13135                         return;
13136                 }
13137         }
13138         if (SEQ_GT(tp->snd_max, tp->snd_una) &&
13139                    ((tmr_up == PACE_TMR_TLP) ||
13140                     (tmr_up == PACE_TMR_RACK) ||
13141                     (tmr_up == PACE_TMR_RXT))) {
13142                 /*
13143                  * Either a Rack, TLP or RXT is fine if  we
13144                  * have outstanding data.
13145                  */
13146                 return;
13147         } else if (tmr_up == PACE_TMR_DELACK) {
13148                 /*
13149                  * If the delayed ack was going to go off
13150                  * before the rtx/tlp/rack timer were going to
13151                  * expire, then that would be the timer in control.
13152                  * Note we don't check the time here trusting the
13153                  * code is correct.
13154                  */
13155                 return;
13156         }
13157         /*
13158          * Ok the timer originally started is not what we want now.
13159          * We will force the hpts to be stopped if any, and restart
13160          * with the slot set to what was in the saved slot.
13161          */
13162         if (rack->rc_inp->inp_in_hpts) {
13163                 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
13164                         uint32_t us_cts;
13165
13166                         us_cts = tcp_get_usecs(NULL);
13167                         if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
13168                                 rack->r_early = 1;
13169                                 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
13170                         }
13171                         rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
13172                 }
13173                 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
13174         }
13175         rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13176         rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
13177 }
13178
13179
13180 static void
13181 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts, uint32_t high_seq)
13182 {
13183         if ((SEQ_LT(tp->snd_wl1, seq) ||
13184             (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
13185             (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
13186                 /* keep track of pure window updates */
13187                 if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
13188                         KMOD_TCPSTAT_INC(tcps_rcvwinupd);
13189                 tp->snd_wnd = tiwin;
13190                 rack_validate_fo_sendwin_up(tp, rack);
13191                 tp->snd_wl1 = seq;
13192                 tp->snd_wl2 = ack;
13193                 if (tp->snd_wnd > tp->max_sndwnd)
13194                         tp->max_sndwnd = tp->snd_wnd;
13195             rack->r_wanted_output = 1;
13196         } else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
13197                 tp->snd_wnd = tiwin;
13198                 rack_validate_fo_sendwin_up(tp, rack);
13199                 tp->snd_wl1 = seq;
13200                 tp->snd_wl2 = ack;
13201         } else {
13202                 /* Not a valid win update */
13203                 return;
13204         }
13205         if (tp->snd_wnd > tp->max_sndwnd)
13206                 tp->max_sndwnd = tp->snd_wnd;
13207         if (tp->snd_wnd < (tp->snd_max - high_seq)) {
13208                 /* The peer collapsed the window */
13209                 rack_collapsed_window(rack);
13210         } else if (rack->rc_has_collapsed)
13211                 rack_un_collapse_window(rack);
13212         /* Do we exit persists? */
13213         if ((rack->rc_in_persist != 0) &&
13214             (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
13215                                 rack->r_ctl.rc_pace_min_segs))) {
13216                 rack_exit_persist(tp, rack, cts);
13217         }
13218         /* Do we enter persists? */
13219         if ((rack->rc_in_persist == 0) &&
13220             (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
13221             TCPS_HAVEESTABLISHED(tp->t_state) &&
13222             (tp->snd_max == tp->snd_una) &&
13223             sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
13224             (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
13225                 /*
13226                  * Here the rwnd is less than
13227                  * the pacing size, we are established,
13228                  * nothing is outstanding, and there is
13229                  * data to send. Enter persists.
13230                  */
13231                 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
13232         }
13233 }
13234
13235 static void
13236 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
13237 {
13238
13239         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13240                 union tcp_log_stackspecific log;
13241                 struct timeval ltv;
13242                 char tcp_hdr_buf[60];
13243                 struct tcphdr *th;
13244                 struct timespec ts;
13245                 uint32_t orig_snd_una;
13246                 uint8_t xx = 0;
13247
13248 #ifdef NETFLIX_HTTP_LOGGING
13249                 struct http_sendfile_track *http_req;
13250
13251                 if (SEQ_GT(ae->ack, tp->snd_una)) {
13252                         http_req = tcp_http_find_req_for_seq(tp, (ae->ack-1));
13253                 } else {
13254                         http_req = tcp_http_find_req_for_seq(tp, ae->ack);
13255                 }
13256 #endif
13257                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
13258                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
13259                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
13260                 if (rack->rack_no_prr == 0)
13261                         log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
13262                 else
13263                         log.u_bbr.flex1 = 0;
13264                 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
13265                 log.u_bbr.use_lt_bw <<= 1;
13266                 log.u_bbr.use_lt_bw |= rack->r_might_revert;
13267                 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
13268                 log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
13269                 log.u_bbr.pkts_out = tp->t_maxseg;
13270                 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
13271                 log.u_bbr.flex7 = 1;
13272                 log.u_bbr.lost = ae->flags;
13273                 log.u_bbr.cwnd_gain = ackval;
13274                 log.u_bbr.pacing_gain = 0x2;
13275                 if (ae->flags & TSTMP_HDWR) {
13276                         /* Record the hardware timestamp if present */
13277                         log.u_bbr.flex3 = M_TSTMP;
13278                         ts.tv_sec = ae->timestamp / 1000000000;
13279                         ts.tv_nsec = ae->timestamp % 1000000000;
13280                         ltv.tv_sec = ts.tv_sec;
13281                         ltv.tv_usec = ts.tv_nsec / 1000;
13282                         log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
13283                 } else if (ae->flags & TSTMP_LRO) {
13284                         /* Record the LRO the arrival timestamp */
13285                         log.u_bbr.flex3 = M_TSTMP_LRO;
13286                         ts.tv_sec = ae->timestamp / 1000000000;
13287                         ts.tv_nsec = ae->timestamp % 1000000000;
13288                         ltv.tv_sec = ts.tv_sec;
13289                         ltv.tv_usec = ts.tv_nsec / 1000;
13290                         log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
13291                 }
13292                 log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
13293                 /* Log the rcv time */
13294                 log.u_bbr.delRate = ae->timestamp;
13295 #ifdef NETFLIX_HTTP_LOGGING
13296                 log.u_bbr.applimited = tp->t_http_closed;
13297                 log.u_bbr.applimited <<= 8;
13298                 log.u_bbr.applimited |= tp->t_http_open;
13299                 log.u_bbr.applimited <<= 8;
13300                 log.u_bbr.applimited |= tp->t_http_req;
13301                 if (http_req) {
13302                         /* Copy out any client req info */
13303                         /* seconds */
13304                         log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
13305                         /* useconds */
13306                         log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
13307                         log.u_bbr.rttProp = http_req->timestamp;
13308                         log.u_bbr.cur_del_rate = http_req->start;
13309                         if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
13310                                 log.u_bbr.flex8 |= 1;
13311                         } else {
13312                                 log.u_bbr.flex8 |= 2;
13313                                 log.u_bbr.bw_inuse = http_req->end;
13314                         }
13315                         log.u_bbr.flex6 = http_req->start_seq;
13316                         if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
13317                                 log.u_bbr.flex8 |= 4;
13318                                 log.u_bbr.epoch = http_req->end_seq;
13319                         }
13320                 }
13321 #endif
13322                 memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
13323                 th = (struct tcphdr *)tcp_hdr_buf;
13324                 th->th_seq = ae->seq;
13325                 th->th_ack = ae->ack;
13326                 th->th_win = ae->win;
13327                 /* Now fill in the ports */
13328                 th->th_sport = tp->t_inpcb->inp_fport;
13329                 th->th_dport = tp->t_inpcb->inp_lport;
13330                 th->th_flags = ae->flags & 0xff;
13331                 /* Now do we have a timestamp option? */
13332                 if (ae->flags & HAS_TSTMP) {
13333                         u_char *cp;
13334                         uint32_t val;
13335
13336                         th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
13337                         cp = (u_char *)(th + 1);
13338                         *cp = TCPOPT_NOP;
13339                         cp++;
13340                         *cp = TCPOPT_NOP;
13341                         cp++;
13342                         *cp = TCPOPT_TIMESTAMP;
13343                         cp++;
13344                         *cp = TCPOLEN_TIMESTAMP;
13345                         cp++;
13346                         val = htonl(ae->ts_value);
13347                         bcopy((char *)&val,
13348                               (char *)cp, sizeof(uint32_t));
13349                         val = htonl(ae->ts_echo);
13350                         bcopy((char *)&val,
13351                               (char *)(cp + 4), sizeof(uint32_t));
13352                 } else
13353                         th->th_off = (sizeof(struct tcphdr) >> 2);
13354
13355                 /*
13356                  * For sane logging we need to play a little trick.
13357                  * If the ack were fully processed we would have moved
13358                  * snd_una to high_seq, but since compressed acks are
13359                  * processed in two phases, at this point (logging) snd_una
13360                  * won't be advanced. So we would see multiple acks showing
13361                  * the advancement. We can prevent that by "pretending" that
13362                  * snd_una was advanced and then un-advancing it so that the
13363                  * logging code has the right value for tlb_snd_una.
13364                  */
13365                 if (tp->snd_una != high_seq) {
13366                         orig_snd_una = tp->snd_una;
13367                         tp->snd_una = high_seq;
13368                         xx = 1;
13369                 } else
13370                         xx = 0;
13371                 TCP_LOG_EVENTP(tp, th,
13372                                &tp->t_inpcb->inp_socket->so_rcv,
13373                                &tp->t_inpcb->inp_socket->so_snd, TCP_LOG_IN, 0,
13374                                0, &log, true, &ltv);
13375                 if (xx) {
13376                         tp->snd_una = orig_snd_una;
13377                 }
13378         }
13379
13380 }
13381
13382 static int
13383 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
13384 {
13385         /*
13386          * Handle a "special" compressed ack mbuf. Each incoming
13387          * ack has only four possible dispositions:
13388          *
13389          * A) It moves the cum-ack forward
13390          * B) It is behind the cum-ack.
13391          * C) It is a window-update ack.
13392          * D) It is a dup-ack.
13393          *
13394          * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
13395          * in the incoming mbuf. We also need to still pay attention
13396          * to nxt_pkt since there may be another packet after this
13397          * one.
13398          */
13399 #ifdef TCP_ACCOUNTING
13400         uint64_t ts_val;
13401         uint64_t rdstc;
13402 #endif
13403         int segsiz;
13404         struct timespec ts;
13405         struct tcp_rack *rack;
13406         struct tcp_ackent *ae;
13407         uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
13408         int cnt, i, did_out, ourfinisacked = 0;
13409         struct tcpopt to_holder, *to = NULL;
13410         int win_up_req = 0;
13411         int nsegs = 0;
13412         int under_pacing = 1;
13413         int recovery = 0;
13414         int idx;
13415 #ifdef TCP_ACCOUNTING
13416         sched_pin();
13417 #endif
13418         rack = (struct tcp_rack *)tp->t_fb_ptr;
13419         if (rack->gp_ready &&
13420             (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
13421                 under_pacing = 0;
13422         else
13423                 under_pacing = 1;
13424
13425         if (rack->r_state != tp->t_state)
13426                 rack_set_state(tp, rack);
13427         if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13428             (tp->t_flags & TF_GPUTINPROG)) {
13429                 /*
13430                  * We have a goodput in progress
13431                  * and we have entered a late state.
13432                  * Do we have enough data in the sb
13433                  * to handle the GPUT request?
13434                  */
13435                 uint32_t bytes;
13436
13437                 bytes = tp->gput_ack - tp->gput_seq;
13438                 if (SEQ_GT(tp->gput_seq, tp->snd_una))
13439                         bytes += tp->gput_seq - tp->snd_una;
13440                 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
13441                         /*
13442                          * There are not enough bytes in the socket
13443                          * buffer that have been sent to cover this
13444                          * measurement. Cancel it.
13445                          */
13446                         rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
13447                                                    rack->r_ctl.rc_gp_srtt /*flex1*/,
13448                                                    tp->gput_seq,
13449                                                    0, 0, 18, __LINE__, NULL, 0);
13450                         tp->t_flags &= ~TF_GPUTINPROG;
13451                 }
13452         }
13453         to = &to_holder;
13454         to->to_flags = 0;
13455         KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
13456                 ("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
13457         cnt = m->m_len / sizeof(struct tcp_ackent);
13458         idx = cnt / 5;
13459         if (idx >= MAX_NUM_OF_CNTS)
13460                 idx = MAX_NUM_OF_CNTS - 1;
13461         counter_u64_add(rack_proc_comp_ack[idx], 1);
13462         counter_u64_add(rack_multi_single_eq, cnt);
13463         high_seq = tp->snd_una;
13464         the_win = tp->snd_wnd;
13465         win_seq = tp->snd_wl1;
13466         win_upd_ack = tp->snd_wl2;
13467         cts = tcp_tv_to_usectick(tv);
13468         ms_cts = tcp_tv_to_mssectick(tv);
13469         segsiz = ctf_fixed_maxseg(tp);
13470         if ((rack->rc_gp_dyn_mul) &&
13471             (rack->use_fixed_rate == 0) &&
13472             (rack->rc_always_pace)) {
13473                 /* Check in on probertt */
13474                 rack_check_probe_rtt(rack, cts);
13475         }
13476         for (i = 0; i < cnt; i++) {
13477 #ifdef TCP_ACCOUNTING
13478                 ts_val = get_cyclecount();
13479 #endif
13480                 rack_clear_rate_sample(rack);
13481                 ae = ((mtod(m, struct tcp_ackent *)) + i);
13482                 /* Setup the window */
13483                 tiwin = ae->win << tp->snd_scale;
13484                 /* figure out the type of ack */
13485                 if (SEQ_LT(ae->ack, high_seq)) {
13486                         /* Case B*/
13487                         ae->ack_val_set = ACK_BEHIND;
13488                 } else if (SEQ_GT(ae->ack, high_seq)) {
13489                         /* Case A */
13490                         ae->ack_val_set = ACK_CUMACK;
13491                 } else if (tiwin == the_win) {
13492                         /* Case D */
13493                         ae->ack_val_set = ACK_DUPACK;
13494                 } else {
13495                         /* Case C */
13496                         ae->ack_val_set = ACK_RWND;
13497                 }
13498                 rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
13499                 /* Validate timestamp */
13500                 if (ae->flags & HAS_TSTMP) {
13501                         /* Setup for a timestamp */
13502                         to->to_flags = TOF_TS;
13503                         ae->ts_echo -= tp->ts_offset;
13504                         to->to_tsecr = ae->ts_echo;
13505                         to->to_tsval = ae->ts_value;
13506                         /*
13507                          * If echoed timestamp is later than the current time, fall back to
13508                          * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
13509                          * were used when this connection was established.
13510                          */
13511                         if (TSTMP_GT(ae->ts_echo, ms_cts))
13512                                 to->to_tsecr = 0;
13513                         if (tp->ts_recent &&
13514                             TSTMP_LT(ae->ts_value, tp->ts_recent)) {
13515                                 if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
13516 #ifdef TCP_ACCOUNTING
13517                                         rdstc = get_cyclecount();
13518                                         if (rdstc > ts_val) {
13519                                                 counter_u64_add(tcp_proc_time[ae->ack_val_set] ,
13520                                                                 (rdstc - ts_val));
13521                                                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13522                                                         tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13523                                                 }
13524                                         }
13525 #endif
13526                                         continue;
13527                                 }
13528                         }
13529                         if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
13530                             SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
13531                                 tp->ts_recent_age = tcp_ts_getticks();
13532                                 tp->ts_recent = ae->ts_value;
13533                         }
13534                 } else {
13535                         /* Setup for a no options */
13536                         to->to_flags = 0;
13537                 }
13538                 /* Update the rcv time and perform idle reduction possibly */
13539                 if  (tp->t_idle_reduce &&
13540                      (tp->snd_max == tp->snd_una) &&
13541                      ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
13542                         counter_u64_add(rack_input_idle_reduces, 1);
13543                         rack_cc_after_idle(rack, tp);
13544                 }
13545                 tp->t_rcvtime = ticks;
13546                 /* Now what about ECN? */
13547                 if (tp->t_flags2 & TF2_ECN_PERMIT) {
13548                         if (ae->flags & TH_CWR) {
13549                                 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
13550                                 tp->t_flags |= TF_ACKNOW;
13551                         }
13552                         switch (ae->codepoint & IPTOS_ECN_MASK) {
13553                         case IPTOS_ECN_CE:
13554                                 tp->t_flags2 |= TF2_ECN_SND_ECE;
13555                                 KMOD_TCPSTAT_INC(tcps_ecn_ce);
13556                                 break;
13557                         case IPTOS_ECN_ECT0:
13558                                 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
13559                                 break;
13560                         case IPTOS_ECN_ECT1:
13561                                 KMOD_TCPSTAT_INC(tcps_ecn_ect1);
13562                                 break;
13563                         }
13564
13565                         /* Process a packet differently from RFC3168. */
13566                         cc_ecnpkt_handler_flags(tp, ae->flags, ae->codepoint);
13567                         /* Congestion experienced. */
13568                         if (ae->flags & TH_ECE) {
13569                                 rack_cong_signal(tp,  CC_ECN, ae->ack);
13570                         }
13571                 }
13572 #ifdef TCP_ACCOUNTING
13573                 /* Count for the specific type of ack in */
13574                 counter_u64_add(tcp_cnt_counters[ae->ack_val_set], 1);
13575                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13576                         tp->tcp_cnt_counters[ae->ack_val_set]++;
13577                 }
13578 #endif
13579                 /*
13580                  * Note how we could move up these in the determination
13581                  * above, but we don't so that way the timestamp checks (and ECN)
13582                  * is done first before we do any processing on the ACK.
13583                  * The non-compressed path through the code has this
13584                  * weakness (noted by @jtl) that it actually does some
13585                  * processing before verifying the timestamp information.
13586                  * We don't take that path here which is why we set
13587                  * the ack_val_set first, do the timestamp and ecn
13588                  * processing, and then look at what we have setup.
13589                  */
13590                 if (ae->ack_val_set == ACK_BEHIND) {
13591                         /*
13592                          * Case B flag reordering, if window is not closed
13593                          * or it could be a keep-alive or persists
13594                          */
13595                         if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
13596                                 counter_u64_add(rack_reorder_seen, 1);
13597                                 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
13598                         }
13599                 } else if (ae->ack_val_set == ACK_DUPACK) {
13600                         /* Case D */
13601                         rack_strike_dupack(rack);
13602                 } else if (ae->ack_val_set == ACK_RWND) {
13603                         /* Case C */
13604                         win_up_req = 1;
13605                         win_upd_ack = ae->ack;
13606                         win_seq = ae->seq;
13607                         the_win = tiwin;
13608                         rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13609                 } else {
13610                         /* Case A */
13611                         if (SEQ_GT(ae->ack, tp->snd_max)) {
13612                                 /*
13613                                  * We just send an ack since the incoming
13614                                  * ack is beyond the largest seq we sent.
13615                                  */
13616                                 if ((tp->t_flags & TF_ACKNOW) == 0) {
13617                                         ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
13618                                         if (tp->t_flags && TF_ACKNOW)
13619                                                 rack->r_wanted_output = 1;
13620                                 }
13621                         } else {
13622                                 nsegs++;
13623                                 /* If the window changed setup to update */
13624                                 if (tiwin != tp->snd_wnd) {
13625                                         win_upd_ack = ae->ack;
13626                                         win_seq = ae->seq;
13627                                         the_win = tiwin;
13628                                         rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13629                                 }
13630 #ifdef TCP_ACCOUNTING
13631                                 /* Account for the acks */
13632                                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13633                                         tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
13634                                 }
13635                                 counter_u64_add(tcp_cnt_counters[CNT_OF_ACKS_IN],
13636                                                 (((ae->ack - high_seq) + segsiz - 1) / segsiz));
13637 #endif
13638                                 high_seq = ae->ack;
13639                                 if (SEQ_GEQ(high_seq, rack->r_ctl.roundends)) {
13640                                         rack->r_ctl.current_round++;
13641                                         rack->r_ctl.roundends = tp->snd_max;
13642                                         if (CC_ALGO(tp)->newround != NULL) {
13643                                                 CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
13644                                         }
13645                                 }
13646                                 /* Setup our act_rcv_time */
13647                                 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
13648                                         ts.tv_sec = ae->timestamp / 1000000000;
13649                                         ts.tv_nsec = ae->timestamp % 1000000000;
13650                                         rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
13651                                         rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
13652                                 } else {
13653                                         rack->r_ctl.act_rcv_time = *tv;
13654                                 }
13655                                 rack_process_to_cumack(tp, rack, ae->ack, cts, to);
13656                                 if (rack->rc_dsack_round_seen) {
13657                                         /* Is the dsack round over? */
13658                                         if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
13659                                                 /* Yes it is */
13660                                                 rack->rc_dsack_round_seen = 0;
13661                                                 rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
13662                                         }
13663                                 }
13664                         }
13665                 }
13666                 /* And lets be sure to commit the rtt measurements for this ack */
13667                 tcp_rack_xmit_timer_commit(rack, tp);
13668 #ifdef TCP_ACCOUNTING
13669                 rdstc = get_cyclecount();
13670                 if (rdstc > ts_val) {
13671                         counter_u64_add(tcp_proc_time[ae->ack_val_set] , (rdstc - ts_val));
13672                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13673                                 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
13674                                 if (ae->ack_val_set == ACK_CUMACK)
13675                                         tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
13676                         }
13677                 }
13678 #endif
13679         }
13680 #ifdef TCP_ACCOUNTING
13681         ts_val = get_cyclecount();
13682 #endif
13683         acked_amount = acked = (high_seq - tp->snd_una);
13684         if (acked) {
13685                 if (rack->sack_attack_disable == 0)
13686                         rack_do_decay(rack);
13687                 if (acked >= segsiz) {
13688                         /*
13689                          * You only get credit for
13690                          * MSS and greater (and you get extra
13691                          * credit for larger cum-ack moves).
13692                          */
13693                         int ac;
13694
13695                         ac = acked / segsiz;
13696                         rack->r_ctl.ack_count += ac;
13697                         counter_u64_add(rack_ack_total, ac);
13698                 }
13699                 if (rack->r_ctl.ack_count > 0xfff00000) {
13700                         /*
13701                          * reduce the number to keep us under
13702                          * a uint32_t.
13703                          */
13704                         rack->r_ctl.ack_count /= 2;
13705                         rack->r_ctl.sack_count /= 2;
13706                 }
13707                 if (tp->t_flags & TF_NEEDSYN) {
13708                         /*
13709                          * T/TCP: Connection was half-synchronized, and our SYN has
13710                          * been ACK'd (so connection is now fully synchronized).  Go
13711                          * to non-starred state, increment snd_una for ACK of SYN,
13712                          * and check if we can do window scaling.
13713                          */
13714                         tp->t_flags &= ~TF_NEEDSYN;
13715                         tp->snd_una++;
13716                         acked_amount = acked = (high_seq - tp->snd_una);
13717                 }
13718                 if (acked > sbavail(&so->so_snd))
13719                         acked_amount = sbavail(&so->so_snd);
13720 #ifdef NETFLIX_EXP_DETECTION
13721                 /*
13722                  * We only care on a cum-ack move if we are in a sack-disabled
13723                  * state. We have already added in to the ack_count, and we never
13724                  * would disable on a cum-ack move, so we only care to do the
13725                  * detection if it may "undo" it, i.e. we were in disabled already.
13726                  */
13727                 if (rack->sack_attack_disable)
13728                         rack_do_detection(tp, rack, acked_amount, segsiz);
13729 #endif
13730                 if (IN_FASTRECOVERY(tp->t_flags) &&
13731                     (rack->rack_no_prr == 0))
13732                         rack_update_prr(tp, rack, acked_amount, high_seq);
13733                 if (IN_RECOVERY(tp->t_flags)) {
13734                         if (SEQ_LT(high_seq, tp->snd_recover) &&
13735                             (SEQ_LT(high_seq, tp->snd_max))) {
13736                                 tcp_rack_partialack(tp);
13737                         } else {
13738                                 rack_post_recovery(tp, high_seq);
13739                                 recovery = 1;
13740                         }
13741                 }
13742                 /* Handle the rack-log-ack part (sendmap) */
13743                 if ((sbused(&so->so_snd) == 0) &&
13744                     (acked > acked_amount) &&
13745                     (tp->t_state >= TCPS_FIN_WAIT_1) &&
13746                     (tp->t_flags & TF_SENTFIN)) {
13747                         /*
13748                          * We must be sure our fin
13749                          * was sent and acked (we can be
13750                          * in FIN_WAIT_1 without having
13751                          * sent the fin).
13752                          */
13753                         ourfinisacked = 1;
13754                         /*
13755                          * Lets make sure snd_una is updated
13756                          * since most likely acked_amount = 0 (it
13757                          * should be).
13758                          */
13759                         tp->snd_una = high_seq;
13760                 }
13761                 /* Did we make a RTO error? */
13762                 if ((tp->t_flags & TF_PREVVALID) &&
13763                     ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
13764                         tp->t_flags &= ~TF_PREVVALID;
13765                         if (tp->t_rxtshift == 1 &&
13766                             (int)(ticks - tp->t_badrxtwin) < 0)
13767                                 rack_cong_signal(tp, CC_RTO_ERR, high_seq);
13768                 }
13769                 /* Handle the data in the socket buffer */
13770                 KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
13771                 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
13772                 if (acked_amount > 0) {
13773                         struct mbuf *mfree;
13774
13775                         rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery);
13776                         SOCKBUF_LOCK(&so->so_snd);
13777                         mfree = sbcut_locked(&so->so_snd, acked_amount);
13778                         tp->snd_una = high_seq;
13779                         /* Note we want to hold the sb lock through the sendmap adjust */
13780                         rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una);
13781                         /* Wake up the socket if we have room to write more */
13782                         rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
13783                         sowwakeup_locked(so);
13784                         m_freem(mfree);
13785                 }
13786                 /* update progress */
13787                 tp->t_acktime = ticks;
13788                 rack_log_progress_event(rack, tp, tp->t_acktime,
13789                                         PROGRESS_UPDATE, __LINE__);
13790                 /* Clear out shifts and such */
13791                 tp->t_rxtshift = 0;
13792                 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
13793                                    rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
13794                 rack->rc_tlp_in_progress = 0;
13795                 rack->r_ctl.rc_tlp_cnt_out = 0;
13796                 /* Send recover and snd_nxt must be dragged along */
13797                 if (SEQ_GT(tp->snd_una, tp->snd_recover))
13798                         tp->snd_recover = tp->snd_una;
13799                 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
13800                         tp->snd_nxt = tp->snd_una;
13801                 /*
13802                  * If the RXT timer is running we want to
13803                  * stop it, so we can restart a TLP (or new RXT).
13804                  */
13805                 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
13806                         rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13807 #ifdef NETFLIX_HTTP_LOGGING
13808                 tcp_http_check_for_comp(rack->rc_tp, high_seq);
13809 #endif
13810                 tp->snd_wl2 = high_seq;
13811                 tp->t_dupacks = 0;
13812                 if (under_pacing &&
13813                     (rack->use_fixed_rate == 0) &&
13814                     (rack->in_probe_rtt == 0) &&
13815                     rack->rc_gp_dyn_mul &&
13816                     rack->rc_always_pace) {
13817                         /* Check if we are dragging bottom */
13818                         rack_check_bottom_drag(tp, rack, so, acked);
13819                 }
13820                 if (tp->snd_una == tp->snd_max) {
13821                         tp->t_flags &= ~TF_PREVVALID;
13822                         rack->r_ctl.retran_during_recovery = 0;
13823                         rack->r_ctl.dsack_byte_cnt = 0;
13824                         rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13825                         if (rack->r_ctl.rc_went_idle_time == 0)
13826                                 rack->r_ctl.rc_went_idle_time = 1;
13827                         rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13828                         if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
13829                                 tp->t_acktime = 0;
13830                         /* Set so we might enter persists... */
13831                         rack->r_wanted_output = 1;
13832                         rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13833                         sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
13834                         if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
13835                             (sbavail(&so->so_snd) == 0) &&
13836                             (tp->t_flags2 & TF2_DROP_AF_DATA)) {
13837                                 /*
13838                                  * The socket was gone and the
13839                                  * peer sent data (not now in the past), time to
13840                                  * reset him.
13841                                  */
13842                                 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13843                                 /* tcp_close will kill the inp pre-log the Reset */
13844                                 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13845 #ifdef TCP_ACCOUNTING
13846                                 rdstc = get_cyclecount();
13847                                 if (rdstc > ts_val) {
13848                                         counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13849                                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13850                                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13851                                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13852                                         }
13853                                 }
13854 #endif
13855                                 m_freem(m);
13856                                 tp = tcp_close(tp);
13857                                 if (tp == NULL) {
13858 #ifdef TCP_ACCOUNTING
13859                                         sched_unpin();
13860 #endif
13861                                         return (1);
13862                                 }
13863                                 /*
13864                                  * We would normally do drop-with-reset which would
13865                                  * send back a reset. We can't since we don't have
13866                                  * all the needed bits. Instead lets arrange for
13867                                  * a call to tcp_output(). That way since we
13868                                  * are in the closed state we will generate a reset.
13869                                  *
13870                                  * Note if tcp_accounting is on we don't unpin since
13871                                  * we do that after the goto label.
13872                                  */
13873                                 goto send_out_a_rst;
13874                         }
13875                         if ((sbused(&so->so_snd) == 0) &&
13876                             (tp->t_state >= TCPS_FIN_WAIT_1) &&
13877                             (tp->t_flags & TF_SENTFIN)) {
13878                                 /*
13879                                  * If we can't receive any more data, then closing user can
13880                                  * proceed. Starting the timer is contrary to the
13881                                  * specification, but if we don't get a FIN we'll hang
13882                                  * forever.
13883                                  *
13884                                  */
13885                                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13886                                         soisdisconnected(so);
13887                                         tcp_timer_activate(tp, TT_2MSL,
13888                                                            (tcp_fast_finwait2_recycle ?
13889                                                             tcp_finwait2_timeout :
13890                                                             TP_MAXIDLE(tp)));
13891                                 }
13892                                 if (ourfinisacked == 0) {
13893                                         /*
13894                                          * We don't change to fin-wait-2 if we have our fin acked
13895                                          * which means we are probably in TCPS_CLOSING.
13896                                          */
13897                                         tcp_state_change(tp, TCPS_FIN_WAIT_2);
13898                                 }
13899                         }
13900                 }
13901                 /* Wake up the socket if we have room to write more */
13902                 if (sbavail(&so->so_snd)) {
13903                         rack->r_wanted_output = 1;
13904                         if (ctf_progress_timeout_check(tp, true)) {
13905                                 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13906                                                         tp, tick, PROGRESS_DROP, __LINE__);
13907                                 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
13908                                 /*
13909                                  * We cheat here and don't send a RST, we should send one
13910                                  * when the pacer drops the connection.
13911                                  */
13912 #ifdef TCP_ACCOUNTING
13913                                 rdstc = get_cyclecount();
13914                                 if (rdstc > ts_val) {
13915                                         counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13916                                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13917                                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13918                                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13919                                         }
13920                                 }
13921                                 sched_unpin();
13922 #endif
13923                                 INP_WUNLOCK(rack->rc_inp);
13924                                 m_freem(m);
13925                                 return (1);
13926                         }
13927                 }
13928                 if (ourfinisacked) {
13929                         switch(tp->t_state) {
13930                         case TCPS_CLOSING:
13931 #ifdef TCP_ACCOUNTING
13932                                 rdstc = get_cyclecount();
13933                                 if (rdstc > ts_val) {
13934                                         counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13935                                                         (rdstc - ts_val));
13936                                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13937                                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13938                                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13939                                         }
13940                                 }
13941                                 sched_unpin();
13942 #endif
13943                                 tcp_twstart(tp);
13944                                 m_freem(m);
13945                                 return (1);
13946                                 break;
13947                         case TCPS_LAST_ACK:
13948 #ifdef TCP_ACCOUNTING
13949                                 rdstc = get_cyclecount();
13950                                 if (rdstc > ts_val) {
13951                                         counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13952                                                         (rdstc - ts_val));
13953                                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13954                                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13955                                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13956                                         }
13957                                 }
13958                                 sched_unpin();
13959 #endif
13960                                 tp = tcp_close(tp);
13961                                 ctf_do_drop(m, tp);
13962                                 return (1);
13963                                 break;
13964                         case TCPS_FIN_WAIT_1:
13965 #ifdef TCP_ACCOUNTING
13966                                 rdstc = get_cyclecount();
13967                                 if (rdstc > ts_val) {
13968                                         counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
13969                                                         (rdstc - ts_val));
13970                                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13971                                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
13972                                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
13973                                         }
13974                                 }
13975 #endif
13976                                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13977                                         soisdisconnected(so);
13978                                         tcp_timer_activate(tp, TT_2MSL,
13979                                                            (tcp_fast_finwait2_recycle ?
13980                                                             tcp_finwait2_timeout :
13981                                                             TP_MAXIDLE(tp)));
13982                                 }
13983                                 tcp_state_change(tp, TCPS_FIN_WAIT_2);
13984                                 break;
13985                         default:
13986                                 break;
13987                         }
13988                 }
13989                 if (rack->r_fast_output) {
13990                         /*
13991                          * We re doing fast output.. can we expand that?
13992                          */
13993                         rack_gain_for_fastoutput(rack, tp, so, acked_amount);
13994                 }
13995 #ifdef TCP_ACCOUNTING
13996                 rdstc = get_cyclecount();
13997                 if (rdstc > ts_val) {
13998                         counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val));
13999                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14000                                 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
14001                                 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
14002                         }
14003                 }
14004
14005         } else if (win_up_req) {
14006                 rdstc = get_cyclecount();
14007                 if (rdstc > ts_val) {
14008                         counter_u64_add(tcp_proc_time[ACK_RWND] , (rdstc - ts_val));
14009                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14010                                 tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
14011                         }
14012                 }
14013 #endif
14014         }
14015         /* Now is there a next packet, if so we are done */
14016         m_freem(m);
14017         did_out = 0;
14018         if (nxt_pkt) {
14019 #ifdef TCP_ACCOUNTING
14020                 sched_unpin();
14021 #endif
14022                 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
14023                 return (0);
14024         }
14025         rack_handle_might_revert(tp, rack);
14026         ctf_calc_rwin(so, tp);
14027         if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14028         send_out_a_rst:
14029                 (void)tp->t_fb->tfb_tcp_output(tp);
14030                 did_out = 1;
14031         }
14032         rack_free_trim(rack);
14033 #ifdef TCP_ACCOUNTING
14034         sched_unpin();
14035 #endif
14036         rack_timer_audit(tp, rack, &so->so_snd);
14037         rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
14038         return (0);
14039 }
14040
14041
14042 static int
14043 rack_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
14044     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
14045     int32_t nxt_pkt, struct timeval *tv)
14046 {
14047 #ifdef TCP_ACCOUNTING
14048         uint64_t ts_val;
14049 #endif
14050         int32_t thflags, retval, did_out = 0;
14051         int32_t way_out = 0;
14052         /*
14053          * cts - is the current time from tv (caller gets ts) in microseconds.
14054          * ms_cts - is the current time from tv in milliseconds.
14055          * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
14056          */
14057         uint32_t cts, us_cts, ms_cts;
14058         uint32_t tiwin;
14059         struct timespec ts;
14060         struct tcpopt to;
14061         struct tcp_rack *rack;
14062         struct rack_sendmap *rsm;
14063         int32_t prev_state = 0;
14064 #ifdef TCP_ACCOUNTING
14065         int ack_val_set = 0xf;
14066 #endif
14067         int nsegs;
14068         /*
14069          * tv passed from common code is from either M_TSTMP_LRO or
14070          * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
14071          */
14072         rack = (struct tcp_rack *)tp->t_fb_ptr;
14073         if (m->m_flags & M_ACKCMP) {
14074                 return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
14075         }
14076         if (m->m_flags & M_ACKCMP) {
14077                 panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
14078         }
14079         cts = tcp_tv_to_usectick(tv);
14080         ms_cts =  tcp_tv_to_mssectick(tv);
14081         nsegs = m->m_pkthdr.lro_nsegs;
14082         counter_u64_add(rack_proc_non_comp_ack, 1);
14083         thflags = th->th_flags;
14084 #ifdef TCP_ACCOUNTING
14085         sched_pin();
14086         if (thflags & TH_ACK)
14087                 ts_val = get_cyclecount();
14088 #endif
14089         if ((m->m_flags & M_TSTMP) ||
14090             (m->m_flags & M_TSTMP_LRO)) {
14091                 mbuf_tstmp2timespec(m, &ts);
14092                 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
14093                 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
14094         } else
14095                 rack->r_ctl.act_rcv_time = *tv;
14096         kern_prefetch(rack, &prev_state);
14097         prev_state = 0;
14098         /*
14099          * Unscale the window into a 32-bit value. For the SYN_SENT state
14100          * the scale is zero.
14101          */
14102         tiwin = th->th_win << tp->snd_scale;
14103 #ifdef TCP_ACCOUNTING
14104         if (thflags & TH_ACK) {
14105                 /*
14106                  * We have a tradeoff here. We can either do what we are
14107                  * doing i.e. pinning to this CPU and then doing the accounting
14108                  * <or> we could do a critical enter, setup the rdtsc and cpu
14109                  * as in below, and then validate we are on the same CPU on
14110                  * exit. I have choosen to not do the critical enter since
14111                  * that often will gain you a context switch, and instead lock
14112                  * us (line above this if) to the same CPU with sched_pin(). This
14113                  * means we may be context switched out for a higher priority
14114                  * interupt but we won't be moved to another CPU.
14115                  *
14116                  * If this occurs (which it won't very often since we most likely
14117                  * are running this code in interupt context and only a higher
14118                  * priority will bump us ... clock?) we will falsely add in
14119                  * to the time the interupt processing time plus the ack processing
14120                  * time. This is ok since its a rare event.
14121                  */
14122                 ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
14123                                                     ctf_fixed_maxseg(tp));
14124         }
14125 #endif
14126         /*
14127          * Parse options on any incoming segment.
14128          */
14129         memset(&to, 0, sizeof(to));
14130         tcp_dooptions(&to, (u_char *)(th + 1),
14131             (th->th_off << 2) - sizeof(struct tcphdr),
14132             (thflags & TH_SYN) ? TO_SYN : 0);
14133         NET_EPOCH_ASSERT();
14134         INP_WLOCK_ASSERT(tp->t_inpcb);
14135         KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
14136             __func__));
14137         KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
14138             __func__));
14139         if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
14140             (tp->t_flags & TF_GPUTINPROG)) {
14141                 /*
14142                  * We have a goodput in progress
14143                  * and we have entered a late state.
14144                  * Do we have enough data in the sb
14145                  * to handle the GPUT request?
14146                  */
14147                 uint32_t bytes;
14148
14149                 bytes = tp->gput_ack - tp->gput_seq;
14150                 if (SEQ_GT(tp->gput_seq, tp->snd_una))
14151                         bytes += tp->gput_seq - tp->snd_una;
14152                 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
14153                         /*
14154                          * There are not enough bytes in the socket
14155                          * buffer that have been sent to cover this
14156                          * measurement. Cancel it.
14157                          */
14158                         rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
14159                                                    rack->r_ctl.rc_gp_srtt /*flex1*/,
14160                                                    tp->gput_seq,
14161                                                    0, 0, 18, __LINE__, NULL, 0);
14162                         tp->t_flags &= ~TF_GPUTINPROG;
14163                 }
14164         }
14165         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
14166                 union tcp_log_stackspecific log;
14167                 struct timeval ltv;
14168 #ifdef NETFLIX_HTTP_LOGGING
14169                 struct http_sendfile_track *http_req;
14170
14171                 if (SEQ_GT(th->th_ack, tp->snd_una)) {
14172                         http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1));
14173                 } else {
14174                         http_req = tcp_http_find_req_for_seq(tp, th->th_ack);
14175                 }
14176 #endif
14177                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14178                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
14179                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
14180                 if (rack->rack_no_prr == 0)
14181                         log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
14182                 else
14183                         log.u_bbr.flex1 = 0;
14184                 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
14185                 log.u_bbr.use_lt_bw <<= 1;
14186                 log.u_bbr.use_lt_bw |= rack->r_might_revert;
14187                 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
14188                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14189                 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
14190                 log.u_bbr.flex3 = m->m_flags;
14191                 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
14192                 log.u_bbr.lost = thflags;
14193                 log.u_bbr.pacing_gain = 0x1;
14194 #ifdef TCP_ACCOUNTING
14195                 log.u_bbr.cwnd_gain = ack_val_set;
14196 #endif
14197                 log.u_bbr.flex7 = 2;
14198                 if (m->m_flags & M_TSTMP) {
14199                         /* Record the hardware timestamp if present */
14200                         mbuf_tstmp2timespec(m, &ts);
14201                         ltv.tv_sec = ts.tv_sec;
14202                         ltv.tv_usec = ts.tv_nsec / 1000;
14203                         log.u_bbr.lt_epoch = tcp_tv_to_usectick(&ltv);
14204                 } else if (m->m_flags & M_TSTMP_LRO) {
14205                         /* Record the LRO the arrival timestamp */
14206                         mbuf_tstmp2timespec(m, &ts);
14207                         ltv.tv_sec = ts.tv_sec;
14208                         ltv.tv_usec = ts.tv_nsec / 1000;
14209                         log.u_bbr.flex5 = tcp_tv_to_usectick(&ltv);
14210                 }
14211                 log.u_bbr.timeStamp = tcp_get_usecs(&ltv);
14212                 /* Log the rcv time */
14213                 log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
14214 #ifdef NETFLIX_HTTP_LOGGING
14215                 log.u_bbr.applimited = tp->t_http_closed;
14216                 log.u_bbr.applimited <<= 8;
14217                 log.u_bbr.applimited |= tp->t_http_open;
14218                 log.u_bbr.applimited <<= 8;
14219                 log.u_bbr.applimited |= tp->t_http_req;
14220                 if (http_req) {
14221                         /* Copy out any client req info */
14222                         /* seconds */
14223                         log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
14224                         /* useconds */
14225                         log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
14226                         log.u_bbr.rttProp = http_req->timestamp;
14227                         log.u_bbr.cur_del_rate = http_req->start;
14228                         if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
14229                                 log.u_bbr.flex8 |= 1;
14230                         } else {
14231                                 log.u_bbr.flex8 |= 2;
14232                                 log.u_bbr.bw_inuse = http_req->end;
14233                         }
14234                         log.u_bbr.flex6 = http_req->start_seq;
14235                         if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
14236                                 log.u_bbr.flex8 |= 4;
14237                                 log.u_bbr.epoch = http_req->end_seq;
14238                         }
14239                 }
14240 #endif
14241                 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
14242                     tlen, &log, true, &ltv);
14243         }
14244         if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
14245                 way_out = 4;
14246                 retval = 0;
14247                 m_freem(m);
14248                 goto done_with_input;
14249         }
14250         /*
14251          * If a segment with the ACK-bit set arrives in the SYN-SENT state
14252          * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
14253          */
14254         if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
14255             (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
14256                 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
14257                 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14258 #ifdef TCP_ACCOUNTING
14259                 sched_unpin();
14260 #endif
14261                 return (1);
14262         }
14263         /*
14264          * If timestamps were negotiated during SYN/ACK and a
14265          * segment without a timestamp is received, silently drop
14266          * the segment, unless it is a RST segment or missing timestamps are
14267          * tolerated.
14268          * See section 3.2 of RFC 7323.
14269          */
14270         if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
14271             ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
14272                 way_out = 5;
14273                 retval = 0;
14274                 m_freem(m);
14275                 goto done_with_input;
14276         }
14277
14278         /*
14279          * Segment received on connection. Reset idle time and keep-alive
14280          * timer. XXX: This should be done after segment validation to
14281          * ignore broken/spoofed segs.
14282          */
14283         if  (tp->t_idle_reduce &&
14284              (tp->snd_max == tp->snd_una) &&
14285              ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
14286                 counter_u64_add(rack_input_idle_reduces, 1);
14287                 rack_cc_after_idle(rack, tp);
14288         }
14289         tp->t_rcvtime = ticks;
14290 #ifdef STATS
14291         stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
14292 #endif
14293         if (tiwin > rack->r_ctl.rc_high_rwnd)
14294                 rack->r_ctl.rc_high_rwnd = tiwin;
14295         /*
14296          * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
14297          * this to occur after we've validated the segment.
14298          */
14299         if (tp->t_flags2 & TF2_ECN_PERMIT) {
14300                 if (thflags & TH_CWR) {
14301                         tp->t_flags2 &= ~TF2_ECN_SND_ECE;
14302                         tp->t_flags |= TF_ACKNOW;
14303                 }
14304                 switch (iptos & IPTOS_ECN_MASK) {
14305                 case IPTOS_ECN_CE:
14306                         tp->t_flags2 |= TF2_ECN_SND_ECE;
14307                         KMOD_TCPSTAT_INC(tcps_ecn_ce);
14308                         break;
14309                 case IPTOS_ECN_ECT0:
14310                         KMOD_TCPSTAT_INC(tcps_ecn_ect0);
14311                         break;
14312                 case IPTOS_ECN_ECT1:
14313                         KMOD_TCPSTAT_INC(tcps_ecn_ect1);
14314                         break;
14315                 }
14316
14317                 /* Process a packet differently from RFC3168. */
14318                 cc_ecnpkt_handler(tp, th, iptos);
14319
14320                 /* Congestion experienced. */
14321                 if (thflags & TH_ECE) {
14322                         rack_cong_signal(tp, CC_ECN, th->th_ack);
14323                 }
14324         }
14325
14326         /*
14327          * If echoed timestamp is later than the current time, fall back to
14328          * non RFC1323 RTT calculation.  Normalize timestamp if syncookies
14329          * were used when this connection was established.
14330          */
14331         if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
14332                 to.to_tsecr -= tp->ts_offset;
14333                 if (TSTMP_GT(to.to_tsecr, ms_cts))
14334                         to.to_tsecr = 0;
14335         }
14336
14337         /*
14338          * If its the first time in we need to take care of options and
14339          * verify we can do SACK for rack!
14340          */
14341         if (rack->r_state == 0) {
14342                 /* Should be init'd by rack_init() */
14343                 KASSERT(rack->rc_inp != NULL,
14344                     ("%s: rack->rc_inp unexpectedly NULL", __func__));
14345                 if (rack->rc_inp == NULL) {
14346                         rack->rc_inp = tp->t_inpcb;
14347                 }
14348
14349                 /*
14350                  * Process options only when we get SYN/ACK back. The SYN
14351                  * case for incoming connections is handled in tcp_syncache.
14352                  * According to RFC1323 the window field in a SYN (i.e., a
14353                  * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
14354                  * this is traditional behavior, may need to be cleaned up.
14355                  */
14356                 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
14357                         /* Handle parallel SYN for ECN */
14358                         if (!(thflags & TH_ACK) &&
14359                             ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) &&
14360                             ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) {
14361                                 tp->t_flags2 |= TF2_ECN_PERMIT;
14362                                 tp->t_flags2 |= TF2_ECN_SND_ECE;
14363                                 TCPSTAT_INC(tcps_ecn_shs);
14364                         }
14365                         if ((to.to_flags & TOF_SCALE) &&
14366                             (tp->t_flags & TF_REQ_SCALE)) {
14367                                 tp->t_flags |= TF_RCVD_SCALE;
14368                                 tp->snd_scale = to.to_wscale;
14369                         } else
14370                                 tp->t_flags &= ~TF_REQ_SCALE;
14371                         /*
14372                          * Initial send window.  It will be updated with the
14373                          * next incoming segment to the scaled value.
14374                          */
14375                         tp->snd_wnd = th->th_win;
14376                         rack_validate_fo_sendwin_up(tp, rack);
14377                         if ((to.to_flags & TOF_TS) &&
14378                             (tp->t_flags & TF_REQ_TSTMP)) {
14379                                 tp->t_flags |= TF_RCVD_TSTMP;
14380                                 tp->ts_recent = to.to_tsval;
14381                                 tp->ts_recent_age = cts;
14382                         } else
14383                                 tp->t_flags &= ~TF_REQ_TSTMP;
14384                         if (to.to_flags & TOF_MSS) {
14385                                 tcp_mss(tp, to.to_mss);
14386                         }
14387                         if ((tp->t_flags & TF_SACK_PERMIT) &&
14388                             (to.to_flags & TOF_SACKPERM) == 0)
14389                                 tp->t_flags &= ~TF_SACK_PERMIT;
14390                         if (IS_FASTOPEN(tp->t_flags)) {
14391                                 if (to.to_flags & TOF_FASTOPEN) {
14392                                         uint16_t mss;
14393
14394                                         if (to.to_flags & TOF_MSS)
14395                                                 mss = to.to_mss;
14396                                         else
14397                                                 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
14398                                                         mss = TCP6_MSS;
14399                                                 else
14400                                                         mss = TCP_MSS;
14401                                         tcp_fastopen_update_cache(tp, mss,
14402                                             to.to_tfo_len, to.to_tfo_cookie);
14403                                 } else
14404                                         tcp_fastopen_disable_path(tp);
14405                         }
14406                 }
14407                 /*
14408                  * At this point we are at the initial call. Here we decide
14409                  * if we are doing RACK or not. We do this by seeing if
14410                  * TF_SACK_PERMIT is set and the sack-not-required is clear.
14411                  * The code now does do dup-ack counting so if you don't
14412                  * switch back you won't get rack & TLP, but you will still
14413                  * get this stack.
14414                  */
14415
14416                 if ((rack_sack_not_required == 0) &&
14417                     ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
14418                         tcp_switch_back_to_default(tp);
14419                         (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
14420                             tlen, iptos);
14421 #ifdef TCP_ACCOUNTING
14422                         sched_unpin();
14423 #endif
14424                         return (1);
14425                 }
14426                 tcp_set_hpts(tp->t_inpcb);
14427                 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
14428         }
14429         if (thflags & TH_FIN)
14430                 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
14431         us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14432         if ((rack->rc_gp_dyn_mul) &&
14433             (rack->use_fixed_rate == 0) &&
14434             (rack->rc_always_pace)) {
14435                 /* Check in on probertt */
14436                 rack_check_probe_rtt(rack, us_cts);
14437         }
14438         rack_clear_rate_sample(rack);
14439         if (rack->forced_ack) {
14440                 uint32_t us_rtt;
14441
14442                 /*
14443                  * A persist or keep-alive was forced out, update our
14444                  * min rtt time. Note we do not worry about lost
14445                  * retransmissions since KEEP-ALIVES and persists
14446                  * are usually way long on times of sending (though
14447                  * if we were really paranoid or worried we could
14448                  * at least use timestamps if available to validate).
14449                  */
14450                 rack->forced_ack = 0;
14451                 us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
14452                 if (us_rtt == 0)
14453                         us_rtt = 1;
14454                 rack_apply_updated_usrtt(rack, us_rtt, us_cts);
14455                 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
14456         }
14457         /*
14458          * This is the one exception case where we set the rack state
14459          * always. All other times (timers etc) we must have a rack-state
14460          * set (so we assure we have done the checks above for SACK).
14461          */
14462         rack->r_ctl.rc_rcvtime = cts;
14463         if (rack->r_state != tp->t_state)
14464                 rack_set_state(tp, rack);
14465         if (SEQ_GT(th->th_ack, tp->snd_una) &&
14466             (rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree)) != NULL)
14467                 kern_prefetch(rsm, &prev_state);
14468         prev_state = rack->r_state;
14469         retval = (*rack->r_substate) (m, th, so,
14470             tp, &to, drop_hdrlen,
14471             tlen, tiwin, thflags, nxt_pkt, iptos);
14472 #ifdef INVARIANTS
14473         if ((retval == 0) &&
14474             (tp->t_inpcb == NULL)) {
14475                 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
14476                     retval, tp, prev_state);
14477         }
14478 #endif
14479         if (retval == 0) {
14480                 /*
14481                  * If retval is 1 the tcb is unlocked and most likely the tp
14482                  * is gone.
14483                  */
14484                 INP_WLOCK_ASSERT(tp->t_inpcb);
14485                 if ((rack->rc_gp_dyn_mul) &&
14486                     (rack->rc_always_pace) &&
14487                     (rack->use_fixed_rate == 0) &&
14488                     rack->in_probe_rtt &&
14489                     (rack->r_ctl.rc_time_probertt_starts == 0)) {
14490                         /*
14491                          * If we are going for target, lets recheck before
14492                          * we output.
14493                          */
14494                         rack_check_probe_rtt(rack, us_cts);
14495                 }
14496                 if (rack->set_pacing_done_a_iw == 0) {
14497                         /* How much has been acked? */
14498                         if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
14499                                 /* We have enough to set in the pacing segment size */
14500                                 rack->set_pacing_done_a_iw = 1;
14501                                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
14502                         }
14503                 }
14504                 tcp_rack_xmit_timer_commit(rack, tp);
14505 #ifdef TCP_ACCOUNTING
14506                 /*
14507                  * If we set the ack_val_se to what ack processing we are doing
14508                  * we also want to track how many cycles we burned. Note
14509                  * the bits after tcp_output we let be "free". This is because
14510                  * we are also tracking the tcp_output times as well. Note the
14511                  * use of 0xf here since we only have 11 counter (0 - 0xa) and
14512                  * 0xf cannot be returned and is what we initialize it too to
14513                  * indicate we are not doing the tabulations.
14514                  */
14515                 if (ack_val_set != 0xf) {
14516                         uint64_t crtsc;
14517
14518                         crtsc = get_cyclecount();
14519                         counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14520                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
14521                                 tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
14522                         }
14523                 }
14524 #endif
14525                 if (nxt_pkt == 0) {
14526                         if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14527 do_output_now:
14528                                 did_out = 1;
14529                                 (void)tp->t_fb->tfb_tcp_output(tp);
14530                         }
14531                         rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
14532                         rack_free_trim(rack);
14533                 }
14534                 /* Update any rounds needed */
14535                 if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends)) {
14536                         rack->r_ctl.current_round++;
14537                         rack->r_ctl.roundends = tp->snd_max;
14538                         if (CC_ALGO(tp)->newround != NULL) {
14539                                 CC_ALGO(tp)->newround(tp->ccv, rack->r_ctl.current_round);
14540                         }
14541                 }
14542                 if ((nxt_pkt == 0) &&
14543                     ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
14544                     (SEQ_GT(tp->snd_max, tp->snd_una) ||
14545                      (tp->t_flags & TF_DELACK) ||
14546                      ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
14547                       (tp->t_state <= TCPS_CLOSING)))) {
14548                         /* We could not send (probably in the hpts but stopped the timer earlier)? */
14549                         if ((tp->snd_max == tp->snd_una) &&
14550                             ((tp->t_flags & TF_DELACK) == 0) &&
14551                             (rack->rc_inp->inp_in_hpts) &&
14552                             (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
14553                                 /* keep alive not needed if we are hptsi output yet */
14554                                 ;
14555                         } else {
14556                                 int late = 0;
14557                                 if (rack->rc_inp->inp_in_hpts) {
14558                                         if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14559                                                 us_cts = tcp_get_usecs(NULL);
14560                                                 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
14561                                                         rack->r_early = 1;
14562                                                         rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
14563                                                 } else
14564                                                         late = 1;
14565                                                 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
14566                                         }
14567                                         tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
14568                                 }
14569                                 if (late && (did_out == 0)) {
14570                                         /*
14571                                          * We are late in the sending
14572                                          * and we did not call the output
14573                                          * (this probably should not happen).
14574                                          */
14575                                         goto do_output_now;
14576                                 }
14577                                 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14578                         }
14579                         way_out = 1;
14580                 } else if (nxt_pkt == 0) {
14581                         /* Do we have the correct timer running? */
14582                         rack_timer_audit(tp, rack, &so->so_snd);
14583                         way_out = 2;
14584                 }
14585         done_with_input:
14586                 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
14587                 if (did_out)
14588                         rack->r_wanted_output = 0;
14589 #ifdef INVARIANTS
14590                 if (tp->t_inpcb == NULL) {
14591                         panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
14592                               did_out,
14593                               retval, tp, prev_state);
14594                 }
14595 #endif
14596 #ifdef TCP_ACCOUNTING
14597         } else {
14598                 /*
14599                  * Track the time (see above).
14600                  */
14601                 if (ack_val_set != 0xf) {
14602                         uint64_t crtsc;
14603
14604                         crtsc = get_cyclecount();
14605                         counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14606                         /*
14607                          * Note we *DO NOT* increment the per-tcb counters since
14608                          * in the else the TP may be gone!!
14609                          */
14610                 }
14611 #endif
14612         }
14613 #ifdef TCP_ACCOUNTING
14614         sched_unpin();
14615 #endif
14616         return (retval);
14617 }
14618
14619 void
14620 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
14621     struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
14622 {
14623         struct timeval tv;
14624
14625         /* First lets see if we have old packets */
14626         if (tp->t_in_pkt) {
14627                 if (ctf_do_queued_segments(so, tp, 1)) {
14628                         m_freem(m);
14629                         return;
14630                 }
14631         }
14632         if (m->m_flags & M_TSTMP_LRO) {
14633                 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
14634                 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
14635         } else {
14636                 /* Should not be should we kassert instead? */
14637                 tcp_get_usecs(&tv);
14638         }
14639         if (rack_do_segment_nounlock(m, th, so, tp,
14640                                      drop_hdrlen, tlen, iptos, 0, &tv) == 0) {
14641                 INP_WUNLOCK(tp->t_inpcb);
14642         }
14643 }
14644
14645 struct rack_sendmap *
14646 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
14647 {
14648         struct rack_sendmap *rsm = NULL;
14649         int32_t idx;
14650         uint32_t srtt = 0, thresh = 0, ts_low = 0;
14651
14652         /* Return the next guy to be re-transmitted */
14653         if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
14654                 return (NULL);
14655         }
14656         if (tp->t_flags & TF_SENTFIN) {
14657                 /* retran the end FIN? */
14658                 return (NULL);
14659         }
14660         /* ok lets look at this one */
14661         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
14662         if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
14663                 goto check_it;
14664         }
14665         rsm = rack_find_lowest_rsm(rack);
14666         if (rsm == NULL) {
14667                 return (NULL);
14668         }
14669 check_it:
14670         if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
14671             (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
14672                 /*
14673                  * No sack so we automatically do the 3 strikes and
14674                  * retransmit (no rack timer would be started).
14675                  */
14676
14677                 return (rsm);
14678         }
14679         if (rsm->r_flags & RACK_ACKED) {
14680                 return (NULL);
14681         }
14682         if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
14683             (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
14684                 /* Its not yet ready */
14685                 return (NULL);
14686         }
14687         srtt = rack_grab_rtt(tp, rack);
14688         idx = rsm->r_rtr_cnt - 1;
14689         ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
14690         thresh = rack_calc_thresh_rack(rack, srtt, tsused);
14691         if ((tsused == ts_low) ||
14692             (TSTMP_LT(tsused, ts_low))) {
14693                 /* No time since sending */
14694                 return (NULL);
14695         }
14696         if ((tsused - ts_low) < thresh) {
14697                 /* It has not been long enough yet */
14698                 return (NULL);
14699         }
14700         if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
14701             ((rsm->r_flags & RACK_SACK_PASSED) &&
14702              (rack->sack_attack_disable == 0))) {
14703                 /*
14704                  * We have passed the dup-ack threshold <or>
14705                  * a SACK has indicated this is missing.
14706                  * Note that if you are a declared attacker
14707                  * it is only the dup-ack threshold that
14708                  * will cause retransmits.
14709                  */
14710                 /* log retransmit reason */
14711                 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
14712                 rack->r_fast_output = 0;
14713                 return (rsm);
14714         }
14715         return (NULL);
14716 }
14717
14718 static void
14719 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
14720                            uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
14721                            int line, struct rack_sendmap *rsm, uint8_t quality)
14722 {
14723         if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
14724                 union tcp_log_stackspecific log;
14725                 struct timeval tv;
14726
14727                 memset(&log, 0, sizeof(log));
14728                 log.u_bbr.flex1 = slot;
14729                 log.u_bbr.flex2 = len;
14730                 log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
14731                 log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
14732                 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
14733                 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
14734                 log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
14735                 log.u_bbr.use_lt_bw <<= 1;
14736                 log.u_bbr.use_lt_bw |= rack->r_late;
14737                 log.u_bbr.use_lt_bw <<= 1;
14738                 log.u_bbr.use_lt_bw |= rack->r_early;
14739                 log.u_bbr.use_lt_bw <<= 1;
14740                 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
14741                 log.u_bbr.use_lt_bw <<= 1;
14742                 log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
14743                 log.u_bbr.use_lt_bw <<= 1;
14744                 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
14745                 log.u_bbr.use_lt_bw <<= 1;
14746                 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
14747                 log.u_bbr.use_lt_bw <<= 1;
14748                 log.u_bbr.use_lt_bw |= rack->gp_ready;
14749                 log.u_bbr.pkt_epoch = line;
14750                 log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
14751                 log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
14752                 log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
14753                 log.u_bbr.bw_inuse = bw_est;
14754                 log.u_bbr.delRate = bw;
14755                 if (rack->r_ctl.gp_bw == 0)
14756                         log.u_bbr.cur_del_rate = 0;
14757                 else
14758                         log.u_bbr.cur_del_rate = rack_get_bw(rack);
14759                 log.u_bbr.rttProp = len_time;
14760                 log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
14761                 log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
14762                 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
14763                 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
14764                         /* We are in slow start */
14765                         log.u_bbr.flex7 = 1;
14766                 } else {
14767                         /* we are on congestion avoidance */
14768                         log.u_bbr.flex7 = 0;
14769                 }
14770                 log.u_bbr.flex8 = method;
14771                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14772                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14773                 log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
14774                 log.u_bbr.cwnd_gain <<= 1;
14775                 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
14776                 log.u_bbr.cwnd_gain <<= 1;
14777                 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
14778                 log.u_bbr.bbr_substate = quality;
14779                 TCP_LOG_EVENTP(rack->rc_tp, NULL,
14780                     &rack->rc_inp->inp_socket->so_rcv,
14781                     &rack->rc_inp->inp_socket->so_snd,
14782                     BBR_LOG_HPTSI_CALC, 0,
14783                     0, &log, false, &tv);
14784         }
14785 }
14786
14787 static uint32_t
14788 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
14789 {
14790         uint32_t new_tso, user_max;
14791
14792         user_max = rack->rc_user_set_max_segs * mss;
14793         if (rack->rc_force_max_seg) {
14794                 return (user_max);
14795         }
14796         if (rack->use_fixed_rate &&
14797             ((rack->r_ctl.crte == NULL) ||
14798              (bw != rack->r_ctl.crte->rate))) {
14799                 /* Use the user mss since we are not exactly matched */
14800                 return (user_max);
14801         }
14802         new_tso = tcp_get_pacing_burst_size(rack->rc_tp, bw, mss, rack_pace_one_seg, rack->r_ctl.crte, NULL);
14803         if (new_tso > user_max)
14804                 new_tso = user_max;
14805         return (new_tso);
14806 }
14807
14808 static int32_t
14809 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)
14810 {
14811         uint64_t lentim, fill_bw;
14812
14813         /* Lets first see if we are full, if so continue with normal rate */
14814         rack->r_via_fill_cw = 0;
14815         if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
14816                 return (slot);
14817         if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
14818                 return (slot);
14819         if (rack->r_ctl.rc_last_us_rtt == 0)
14820                 return (slot);
14821         if (rack->rc_pace_fill_if_rttin_range &&
14822             (rack->r_ctl.rc_last_us_rtt >=
14823              (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
14824                 /* The rtt is huge, N * smallest, lets not fill */
14825                 return (slot);
14826         }
14827         /*
14828          * first lets calculate the b/w based on the last us-rtt
14829          * and the sndwnd.
14830          */
14831         fill_bw = rack->r_ctl.cwnd_to_use;
14832         /* Take the rwnd if its smaller */
14833         if (fill_bw > rack->rc_tp->snd_wnd)
14834                 fill_bw = rack->rc_tp->snd_wnd;
14835         if (rack->r_fill_less_agg) {
14836                 /*
14837                  * Now take away the inflight (this will reduce our
14838                  * aggressiveness and yeah, if we get that much out in 1RTT
14839                  * we will have had acks come back and still be behind).
14840                  */
14841                 fill_bw -= ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
14842         }
14843         /* Now lets make it into a b/w */
14844         fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
14845         fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
14846         /* We are below the min b/w */
14847         if (non_paced)
14848                 *rate_wanted = fill_bw;
14849         if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
14850                 return (slot);
14851         if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap))
14852                 fill_bw = rack->r_ctl.bw_rate_cap;
14853         rack->r_via_fill_cw = 1;
14854         if (rack->r_rack_hw_rate_caps &&
14855             (rack->r_ctl.crte != NULL)) {
14856                 uint64_t high_rate;
14857
14858                 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
14859                 if (fill_bw > high_rate) {
14860                         /* We are capping bw at the highest rate table entry */
14861                         if (*rate_wanted > high_rate) {
14862                                 /* The original rate was also capped */
14863                                 rack->r_via_fill_cw = 0;
14864                         }
14865                         rack_log_hdwr_pacing(rack,
14866                                              fill_bw, high_rate, __LINE__,
14867                                              0, 3);
14868                         fill_bw = high_rate;
14869                         if (capped)
14870                                 *capped = 1;
14871                 }
14872         } else if ((rack->r_ctl.crte == NULL) &&
14873                    (rack->rack_hdrw_pacing == 0) &&
14874                    (rack->rack_hdw_pace_ena) &&
14875                    rack->r_rack_hw_rate_caps &&
14876                    (rack->rack_attempt_hdwr_pace == 0) &&
14877                    (rack->rc_inp->inp_route.ro_nh != NULL) &&
14878                    (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
14879                 /*
14880                  * Ok we may have a first attempt that is greater than our top rate
14881                  * lets check.
14882                  */
14883                 uint64_t high_rate;
14884
14885                 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
14886                 if (high_rate) {
14887                         if (fill_bw > high_rate) {
14888                                 fill_bw = high_rate;
14889                                 if (capped)
14890                                         *capped = 1;
14891                         }
14892                 }
14893         }
14894         /*
14895          * Ok fill_bw holds our mythical b/w to fill the cwnd
14896          * in a rtt, what does that time wise equate too?
14897          */
14898         lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
14899         lentim /= fill_bw;
14900         *rate_wanted = fill_bw;
14901         if (non_paced || (lentim < slot)) {
14902                 rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
14903                                            0, lentim, 12, __LINE__, NULL, 0);
14904                 return ((int32_t)lentim);
14905         } else
14906                 return (slot);
14907 }
14908
14909 static int32_t
14910 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
14911 {
14912         int32_t slot = 0;
14913         int can_start_hw_pacing = 1;
14914         int err;
14915
14916         if (rack->rc_always_pace == 0) {
14917                 /*
14918                  * We use the most optimistic possible cwnd/srtt for
14919                  * sending calculations. This will make our
14920                  * calculation anticipate getting more through
14921                  * quicker then possible. But thats ok we don't want
14922                  * the peer to have a gap in data sending.
14923                  */
14924                 uint32_t srtt, cwnd, tr_perms = 0;
14925                 int32_t reduce = 0;
14926
14927         old_method:
14928                 /*
14929                  * We keep no precise pacing with the old method
14930                  * instead we use the pacer to mitigate bursts.
14931                  */
14932                 if (rack->r_ctl.rc_rack_min_rtt)
14933                         srtt = rack->r_ctl.rc_rack_min_rtt;
14934                 else
14935                         srtt = max(tp->t_srtt, 1);
14936                 if (rack->r_ctl.rc_rack_largest_cwnd)
14937                         cwnd = rack->r_ctl.rc_rack_largest_cwnd;
14938                 else
14939                         cwnd = rack->r_ctl.cwnd_to_use;
14940                 /* Inflate cwnd by 1000 so srtt of usecs is in ms */
14941                 tr_perms = (cwnd * 1000) / srtt;
14942                 if (tr_perms == 0) {
14943                         tr_perms = ctf_fixed_maxseg(tp);
14944                 }
14945                 /*
14946                  * Calculate how long this will take to drain, if
14947                  * the calculation comes out to zero, thats ok we
14948                  * will use send_a_lot to possibly spin around for
14949                  * more increasing tot_len_this_send to the point
14950                  * that its going to require a pace, or we hit the
14951                  * cwnd. Which in that case we are just waiting for
14952                  * a ACK.
14953                  */
14954                 slot = len / tr_perms;
14955                 /* Now do we reduce the time so we don't run dry? */
14956                 if (slot && rack_slot_reduction) {
14957                         reduce = (slot / rack_slot_reduction);
14958                         if (reduce < slot) {
14959                                 slot -= reduce;
14960                         } else
14961                                 slot = 0;
14962                 }
14963                 slot *= HPTS_USEC_IN_MSEC;
14964                 if (rack->rc_pace_to_cwnd) {
14965                         uint64_t rate_wanted = 0;
14966
14967                         slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
14968                         rack->rc_ack_can_sendout_data = 1;
14969                         rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
14970                 } else
14971                         rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
14972         } else {
14973                 uint64_t bw_est, res, lentim, rate_wanted;
14974                 uint32_t orig_val, srtt, segs, oh;
14975                 int capped = 0;
14976                 int prev_fill;
14977
14978                 if ((rack->r_rr_config == 1) && rsm) {
14979                         return (rack->r_ctl.rc_min_to);
14980                 }
14981                 if (rack->use_fixed_rate) {
14982                         rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
14983                 } else if ((rack->r_ctl.init_rate == 0) &&
14984 #ifdef NETFLIX_PEAKRATE
14985                            (rack->rc_tp->t_maxpeakrate == 0) &&
14986 #endif
14987                            (rack->r_ctl.gp_bw == 0)) {
14988                         /* no way to yet do an estimate */
14989                         bw_est = rate_wanted = 0;
14990                 } else {
14991                         bw_est = rack_get_bw(rack);
14992                         rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
14993                 }
14994                 if ((bw_est == 0) || (rate_wanted == 0) ||
14995                     ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
14996                         /*
14997                          * No way yet to make a b/w estimate or
14998                          * our raise is set incorrectly.
14999                          */
15000                         goto old_method;
15001                 }
15002                 /* We need to account for all the overheads */
15003                 segs = (len + segsiz - 1) / segsiz;
15004                 /*
15005                  * We need the diff between 1514 bytes (e-mtu with e-hdr)
15006                  * and how much data we put in each packet. Yes this
15007                  * means we may be off if we are larger than 1500 bytes
15008                  * or smaller. But this just makes us more conservative.
15009                  */
15010                 if (rack_hw_rate_min &&
15011                     (bw_est < rack_hw_rate_min))
15012                         can_start_hw_pacing = 0;
15013                 if (ETHERNET_SEGMENT_SIZE > segsiz)
15014                         oh = ETHERNET_SEGMENT_SIZE - segsiz;
15015                 else
15016                         oh = 0;
15017                 segs *= oh;
15018                 lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
15019                 res = lentim / rate_wanted;
15020                 slot = (uint32_t)res;
15021                 orig_val = rack->r_ctl.rc_pace_max_segs;
15022                 if (rack->r_ctl.crte == NULL) {
15023                         /*
15024                          * Only do this if we are not hardware pacing
15025                          * since if we are doing hw-pacing below we will
15026                          * set make a call after setting up or changing
15027                          * the rate.
15028                          */
15029                         rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
15030                 } else if (rack->rc_inp->inp_snd_tag == NULL) {
15031                         /*
15032                          * We lost our rate somehow, this can happen
15033                          * if the interface changed underneath us.
15034                          */
15035                         tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15036                         rack->r_ctl.crte = NULL;
15037                         /* Lets re-allow attempting to setup pacing */
15038                         rack->rack_hdrw_pacing = 0;
15039                         rack->rack_attempt_hdwr_pace = 0;
15040                         rack_log_hdwr_pacing(rack,
15041                                              rate_wanted, bw_est, __LINE__,
15042                                              0, 6);
15043                 }
15044                 /* Did we change the TSO size, if so log it */
15045                 if (rack->r_ctl.rc_pace_max_segs != orig_val)
15046                         rack_log_pacing_delay_calc(rack, len, slot, orig_val, 0, 0, 15, __LINE__, NULL, 0);
15047                 prev_fill = rack->r_via_fill_cw;
15048                 if ((rack->rc_pace_to_cwnd) &&
15049                     (capped == 0) &&
15050                     (rack->use_fixed_rate == 0) &&
15051                     (rack->in_probe_rtt == 0) &&
15052                     (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
15053                         /*
15054                          * We want to pace at our rate *or* faster to
15055                          * fill the cwnd to the max if its not full.
15056                          */
15057                         slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
15058                 }
15059                 if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
15060                     (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
15061                         if ((rack->rack_hdw_pace_ena) &&
15062                             (can_start_hw_pacing > 0) &&
15063                             (rack->rack_hdrw_pacing == 0) &&
15064                             (rack->rack_attempt_hdwr_pace == 0)) {
15065                                 /*
15066                                  * Lets attempt to turn on hardware pacing
15067                                  * if we can.
15068                                  */
15069                                 rack->rack_attempt_hdwr_pace = 1;
15070                                 rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
15071                                                                        rack->rc_inp->inp_route.ro_nh->nh_ifp,
15072                                                                        rate_wanted,
15073                                                                        RS_PACING_GEQ,
15074                                                                        &err, &rack->r_ctl.crte_prev_rate);
15075                                 if (rack->r_ctl.crte) {
15076                                         rack->rack_hdrw_pacing = 1;
15077                                         rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted, segsiz,
15078                                                                                                  0, rack->r_ctl.crte,
15079                                                                                                  NULL);
15080                                         rack_log_hdwr_pacing(rack,
15081                                                              rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15082                                                              err, 0);
15083                                         rack->r_ctl.last_hw_bw_req = rate_wanted;
15084                                 } else {
15085                                         counter_u64_add(rack_hw_pace_init_fail, 1);
15086                                 }
15087                         } else if (rack->rack_hdrw_pacing &&
15088                                    (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
15089                                 /* Do we need to adjust our rate? */
15090                                 const struct tcp_hwrate_limit_table *nrte;
15091
15092                                 if (rack->r_up_only &&
15093                                     (rate_wanted < rack->r_ctl.crte->rate)) {
15094                                         /**
15095                                          * We have four possible states here
15096                                          * having to do with the previous time
15097                                          * and this time.
15098                                          *   previous  |  this-time
15099                                          * A)     0      |     0   -- fill_cw not in the picture
15100                                          * B)     1      |     0   -- we were doing a fill-cw but now are not
15101                                          * C)     1      |     1   -- all rates from fill_cw
15102                                          * D)     0      |     1   -- we were doing non-fill and now we are filling
15103                                          *
15104                                          * For case A, C and D we don't allow a drop. But for
15105                                          * case B where we now our on our steady rate we do
15106                                          * allow a drop.
15107                                          *
15108                                          */
15109                                         if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
15110                                                 goto done_w_hdwr;
15111                                 }
15112                                 if ((rate_wanted > rack->r_ctl.crte->rate) ||
15113                                     (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
15114                                         if (rack_hw_rate_to_low &&
15115                                             (bw_est < rack_hw_rate_to_low)) {
15116                                                 /*
15117                                                  * The pacing rate is too low for hardware, but
15118                                                  * do allow hardware pacing to be restarted.
15119                                                  */
15120                                                 rack_log_hdwr_pacing(rack,
15121                                                              bw_est, rack->r_ctl.crte->rate, __LINE__,
15122                                                              0, 5);
15123                                                 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
15124                                                 rack->r_ctl.crte = NULL;
15125                                                 rack->rack_attempt_hdwr_pace = 0;
15126                                                 rack->rack_hdrw_pacing = 0;
15127                                                 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15128                                                 goto done_w_hdwr;
15129                                         }
15130                                         nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
15131                                                                    rack->rc_tp,
15132                                                                    rack->rc_inp->inp_route.ro_nh->nh_ifp,
15133                                                                    rate_wanted,
15134                                                                    RS_PACING_GEQ,
15135                                                                    &err, &rack->r_ctl.crte_prev_rate);
15136                                         if (nrte == NULL) {
15137                                                 /* Lost the rate */
15138                                                 rack->rack_hdrw_pacing = 0;
15139                                                 rack->r_ctl.crte = NULL;
15140                                                 rack_log_hdwr_pacing(rack,
15141                                                                      rate_wanted, 0, __LINE__,
15142                                                                      err, 1);
15143                                                 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15144                                                 counter_u64_add(rack_hw_pace_lost, 1);
15145                                         } else if (nrte != rack->r_ctl.crte) {
15146                                                 rack->r_ctl.crte = nrte;
15147                                                 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted,
15148                                                                                                          segsiz, 0,
15149                                                                                                          rack->r_ctl.crte,
15150                                                                                                          NULL);
15151                                                 rack_log_hdwr_pacing(rack,
15152                                                                      rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15153                                                                      err, 2);
15154                                                 rack->r_ctl.last_hw_bw_req = rate_wanted;
15155                                         }
15156                                 } else {
15157                                         /* We just need to adjust the segment size */
15158                                         rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
15159                                         rack_log_hdwr_pacing(rack,
15160                                                              rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15161                                                              0, 4);
15162                                         rack->r_ctl.last_hw_bw_req = rate_wanted;
15163                                 }
15164                         }
15165                 }
15166                 if ((rack->r_ctl.crte != NULL) &&
15167                     (rack->r_ctl.crte->rate == rate_wanted)) {
15168                         /*
15169                          * We need to add a extra if the rates
15170                          * are exactly matched. The idea is
15171                          * we want the software to make sure the
15172                          * queue is empty before adding more, this
15173                          * gives us N MSS extra pace times where
15174                          * N is our sysctl
15175                          */
15176                         slot += (rack->r_ctl.crte->time_between * rack_hw_pace_extra_slots);
15177                 }
15178 done_w_hdwr:
15179                 if (rack_limit_time_with_srtt &&
15180                     (rack->use_fixed_rate == 0) &&
15181 #ifdef NETFLIX_PEAKRATE
15182                     (rack->rc_tp->t_maxpeakrate == 0) &&
15183 #endif
15184                     (rack->rack_hdrw_pacing == 0)) {
15185                         /*
15186                          * Sanity check, we do not allow the pacing delay
15187                          * to be longer than the SRTT of the path. If it is
15188                          * a slow path, then adding a packet should increase
15189                          * the RTT and compensate for this i.e. the srtt will
15190                          * be greater so the allowed pacing time will be greater.
15191                          *
15192                          * Note this restriction is not for where a peak rate
15193                          * is set, we are doing fixed pacing or hardware pacing.
15194                          */
15195                         if (rack->rc_tp->t_srtt)
15196                                 srtt = rack->rc_tp->t_srtt;
15197                         else
15198                                 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC;    /* its in ms convert */
15199                         if (srtt < slot) {
15200                                 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
15201                                 slot = srtt;
15202                         }
15203                 }
15204                 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
15205         }
15206         if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
15207                 /*
15208                  * If this rate is seeing enobufs when it
15209                  * goes to send then either the nic is out
15210                  * of gas or we are mis-estimating the time
15211                  * somehow and not letting the queue empty
15212                  * completely. Lets add to the pacing time.
15213                  */
15214                 int hw_boost_delay;
15215
15216                 hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
15217                 if (hw_boost_delay > rack_enobuf_hw_max)
15218                         hw_boost_delay = rack_enobuf_hw_max;
15219                 else if (hw_boost_delay < rack_enobuf_hw_min)
15220                         hw_boost_delay = rack_enobuf_hw_min;
15221                 slot += hw_boost_delay;
15222         }
15223         if (slot)
15224                 counter_u64_add(rack_calc_nonzero, 1);
15225         else
15226                 counter_u64_add(rack_calc_zero, 1);
15227         return (slot);
15228 }
15229
15230 static void
15231 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
15232     tcp_seq startseq, uint32_t sb_offset)
15233 {
15234         struct rack_sendmap *my_rsm = NULL;
15235         struct rack_sendmap fe;
15236
15237         if (tp->t_state < TCPS_ESTABLISHED) {
15238                 /*
15239                  * We don't start any measurements if we are
15240                  * not at least established.
15241                  */
15242                 return;
15243         }
15244         if (tp->t_state >= TCPS_FIN_WAIT_1) {
15245                 /*
15246                  * We will get no more data into the SB
15247                  * this means we need to have the data available
15248                  * before we start a measurement.
15249                  */
15250
15251                 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) <
15252                     max(rc_init_window(rack),
15253                         (MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
15254                         /* Nope not enough data */
15255                         return;
15256                 }
15257         }
15258         tp->t_flags |= TF_GPUTINPROG;
15259         rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
15260         rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
15261         tp->gput_seq = startseq;
15262         rack->app_limited_needs_set = 0;
15263         if (rack->in_probe_rtt)
15264                 rack->measure_saw_probe_rtt = 1;
15265         else if ((rack->measure_saw_probe_rtt) &&
15266                  (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
15267                 rack->measure_saw_probe_rtt = 0;
15268         if (rack->rc_gp_filled)
15269                 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15270         else {
15271                 /* Special case initial measurement */
15272                 struct timeval tv;
15273
15274                 tp->gput_ts = tcp_get_usecs(&tv);
15275                 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15276         }
15277         /*
15278          * We take a guess out into the future,
15279          * if we have no measurement and no
15280          * initial rate, we measure the first
15281          * initial-windows worth of data to
15282          * speed up getting some GP measurement and
15283          * thus start pacing.
15284          */
15285         if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
15286                 rack->app_limited_needs_set = 1;
15287                 tp->gput_ack = startseq + max(rc_init_window(rack),
15288                                               (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
15289                 rack_log_pacing_delay_calc(rack,
15290                                            tp->gput_seq,
15291                                            tp->gput_ack,
15292                                            0,
15293                                            tp->gput_ts,
15294                                            rack->r_ctl.rc_app_limited_cnt,
15295                                            9,
15296                                            __LINE__, NULL, 0);
15297                 return;
15298         }
15299         if (sb_offset) {
15300                 /*
15301                  * We are out somewhere in the sb
15302                  * can we use the already outstanding data?
15303                  */
15304                 if (rack->r_ctl.rc_app_limited_cnt == 0) {
15305                         /*
15306                          * Yes first one is good and in this case
15307                          * the tp->gput_ts is correctly set based on
15308                          * the last ack that arrived (no need to
15309                          * set things up when an ack comes in).
15310                          */
15311                         my_rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
15312                         if ((my_rsm == NULL) ||
15313                             (my_rsm->r_rtr_cnt != 1)) {
15314                                 /* retransmission? */
15315                                 goto use_latest;
15316                         }
15317                 } else {
15318                         if (rack->r_ctl.rc_first_appl == NULL) {
15319                                 /*
15320                                  * If rc_first_appl is NULL
15321                                  * then the cnt should be 0.
15322                                  * This is probably an error, maybe
15323                                  * a KASSERT would be approprate.
15324                                  */
15325                                 goto use_latest;
15326                         }
15327                         /*
15328                          * If we have a marker pointer to the last one that is
15329                          * app limited we can use that, but we need to set
15330                          * things up so that when it gets ack'ed we record
15331                          * the ack time (if its not already acked).
15332                          */
15333                         rack->app_limited_needs_set = 1;
15334                         /*
15335                          * We want to get to the rsm that is either
15336                          * next with space i.e. over 1 MSS or the one
15337                          * after that (after the app-limited).
15338                          */
15339                         my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15340                                          rack->r_ctl.rc_first_appl);
15341                         if (my_rsm) {
15342                                 if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
15343                                         /* Have to use the next one */
15344                                         my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15345                                                          my_rsm);
15346                                 else {
15347                                         /* Use after the first MSS of it is acked */
15348                                         tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
15349                                         goto start_set;
15350                                 }
15351                         }
15352                         if ((my_rsm == NULL) ||
15353                             (my_rsm->r_rtr_cnt != 1)) {
15354                                 /*
15355                                  * Either its a retransmit or
15356                                  * the last is the app-limited one.
15357                                  */
15358                                 goto use_latest;
15359                         }
15360                 }
15361                 tp->gput_seq = my_rsm->r_start;
15362 start_set:
15363                 if (my_rsm->r_flags & RACK_ACKED) {
15364                         /*
15365                          * This one has been acked use the arrival ack time
15366                          */
15367                         tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15368                         rack->app_limited_needs_set = 0;
15369                 }
15370                 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15371                 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
15372                 rack_log_pacing_delay_calc(rack,
15373                                            tp->gput_seq,
15374                                            tp->gput_ack,
15375                                            (uint64_t)my_rsm,
15376                                            tp->gput_ts,
15377                                            rack->r_ctl.rc_app_limited_cnt,
15378                                            9,
15379                                            __LINE__, NULL, 0);
15380                 return;
15381         }
15382
15383 use_latest:
15384         /*
15385          * We don't know how long we may have been
15386          * idle or if this is the first-send. Lets
15387          * setup the flag so we will trim off
15388          * the first ack'd data so we get a true
15389          * measurement.
15390          */
15391         rack->app_limited_needs_set = 1;
15392         tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
15393         /* Find this guy so we can pull the send time */
15394         fe.r_start = startseq;
15395         my_rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
15396         if (my_rsm) {
15397                 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)];
15398                 if (my_rsm->r_flags & RACK_ACKED) {
15399                         /*
15400                          * Unlikely since its probably what was
15401                          * just transmitted (but I am paranoid).
15402                          */
15403                         tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15404                         rack->app_limited_needs_set = 0;
15405                 }
15406                 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
15407                         /* This also is unlikely */
15408                         tp->gput_seq = my_rsm->r_start;
15409                 }
15410         } else {
15411                 /*
15412                  * TSNH unless we have some send-map limit,
15413                  * and even at that it should not be hitting
15414                  * that limit (we should have stopped sending).
15415                  */
15416                 struct timeval tv;
15417
15418                 microuptime(&tv);
15419                 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15420         }
15421         rack_log_pacing_delay_calc(rack,
15422                                    tp->gput_seq,
15423                                    tp->gput_ack,
15424                                    (uint64_t)my_rsm,
15425                                    tp->gput_ts,
15426                                    rack->r_ctl.rc_app_limited_cnt,
15427                                    9, __LINE__, NULL, 0);
15428 }
15429
15430 static inline uint32_t
15431 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack,  uint32_t cwnd_to_use,
15432     uint32_t avail, int32_t sb_offset)
15433 {
15434         uint32_t len;
15435         uint32_t sendwin;
15436
15437         if (tp->snd_wnd > cwnd_to_use)
15438                 sendwin = cwnd_to_use;
15439         else
15440                 sendwin = tp->snd_wnd;
15441         if (ctf_outstanding(tp) >= tp->snd_wnd) {
15442                 /* We never want to go over our peers rcv-window */
15443                 len = 0;
15444         } else {
15445                 uint32_t flight;
15446
15447                 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15448                 if (flight >= sendwin) {
15449                         /*
15450                          * We have in flight what we are allowed by cwnd (if
15451                          * it was rwnd blocking it would have hit above out
15452                          * >= tp->snd_wnd).
15453                          */
15454                         return (0);
15455                 }
15456                 len = sendwin - flight;
15457                 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
15458                         /* We would send too much (beyond the rwnd) */
15459                         len = tp->snd_wnd - ctf_outstanding(tp);
15460                 }
15461                 if ((len + sb_offset) > avail) {
15462                         /*
15463                          * We don't have that much in the SB, how much is
15464                          * there?
15465                          */
15466                         len = avail - sb_offset;
15467                 }
15468         }
15469         return (len);
15470 }
15471
15472 static void
15473 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
15474              unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
15475              int rsm_is_null, int optlen, int line, uint16_t mode)
15476 {
15477         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15478                 union tcp_log_stackspecific log;
15479                 struct timeval tv;
15480
15481                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15482                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
15483                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
15484                 log.u_bbr.flex1 = error;
15485                 log.u_bbr.flex2 = flags;
15486                 log.u_bbr.flex3 = rsm_is_null;
15487                 log.u_bbr.flex4 = ipoptlen;
15488                 log.u_bbr.flex5 = tp->rcv_numsacks;
15489                 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
15490                 log.u_bbr.flex7 = optlen;
15491                 log.u_bbr.flex8 = rack->r_fsb_inited;
15492                 log.u_bbr.applimited = rack->r_fast_output;
15493                 log.u_bbr.bw_inuse = rack_get_bw(rack);
15494                 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
15495                 log.u_bbr.cwnd_gain = mode;
15496                 log.u_bbr.pkts_out = orig_len;
15497                 log.u_bbr.lt_epoch = len;
15498                 log.u_bbr.delivered = line;
15499                 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15500                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15501                 tcp_log_event_(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
15502                                len, &log, false, NULL, NULL, 0, &tv);
15503         }
15504 }
15505
15506
15507 static struct mbuf *
15508 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
15509                    struct rack_fast_send_blk *fsb,
15510                    int32_t seglimit, int32_t segsize, int hw_tls)
15511 {
15512 #ifdef KERN_TLS
15513         struct ktls_session *tls, *ntls;
15514         struct mbuf *start;
15515 #endif
15516         struct mbuf *m, *n, **np, *smb;
15517         struct mbuf *top;
15518         int32_t off, soff;
15519         int32_t len = *plen;
15520         int32_t fragsize;
15521         int32_t len_cp = 0;
15522         uint32_t mlen, frags;
15523
15524         soff = off = the_off;
15525         smb = m = the_m;
15526         np = &top;
15527         top = NULL;
15528 #ifdef KERN_TLS
15529         if (hw_tls && (m->m_flags & M_EXTPG))
15530                 tls = m->m_epg_tls;
15531         else
15532                 tls = NULL;
15533         start = m;
15534 #endif
15535         while (len > 0) {
15536                 if (m == NULL) {
15537                         *plen = len_cp;
15538                         break;
15539                 }
15540 #ifdef KERN_TLS
15541                 if (hw_tls) {
15542                         if (m->m_flags & M_EXTPG)
15543                                 ntls = m->m_epg_tls;
15544                         else
15545                                 ntls = NULL;
15546
15547                         /*
15548                          * Avoid mixing TLS records with handshake
15549                          * data or TLS records from different
15550                          * sessions.
15551                          */
15552                         if (tls != ntls) {
15553                                 MPASS(m != start);
15554                                 *plen = len_cp;
15555                                 break;
15556                         }
15557                 }
15558 #endif
15559                 mlen = min(len, m->m_len - off);
15560                 if (seglimit) {
15561                         /*
15562                          * For M_EXTPG mbufs, add 3 segments
15563                          * + 1 in case we are crossing page boundaries
15564                          * + 2 in case the TLS hdr/trailer are used
15565                          * It is cheaper to just add the segments
15566                          * than it is to take the cache miss to look
15567                          * at the mbuf ext_pgs state in detail.
15568                          */
15569                         if (m->m_flags & M_EXTPG) {
15570                                 fragsize = min(segsize, PAGE_SIZE);
15571                                 frags = 3;
15572                         } else {
15573                                 fragsize = segsize;
15574                                 frags = 0;
15575                         }
15576
15577                         /* Break if we really can't fit anymore. */
15578                         if ((frags + 1) >= seglimit) {
15579                                 *plen = len_cp;
15580                                 break;
15581                         }
15582
15583                         /*
15584                          * Reduce size if you can't copy the whole
15585                          * mbuf. If we can't copy the whole mbuf, also
15586                          * adjust len so the loop will end after this
15587                          * mbuf.
15588                          */
15589                         if ((frags + howmany(mlen, fragsize)) >= seglimit) {
15590                                 mlen = (seglimit - frags - 1) * fragsize;
15591                                 len = mlen;
15592                                 *plen = len_cp + len;
15593                         }
15594                         frags += howmany(mlen, fragsize);
15595                         if (frags == 0)
15596                                 frags++;
15597                         seglimit -= frags;
15598                         KASSERT(seglimit > 0,
15599                             ("%s: seglimit went too low", __func__));
15600                 }
15601                 n = m_get(M_NOWAIT, m->m_type);
15602                 *np = n;
15603                 if (n == NULL)
15604                         goto nospace;
15605                 n->m_len = mlen;
15606                 soff += mlen;
15607                 len_cp += n->m_len;
15608                 if (m->m_flags & (M_EXT|M_EXTPG)) {
15609                         n->m_data = m->m_data + off;
15610                         mb_dupcl(n, m);
15611                 } else {
15612                         bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
15613                             (u_int)n->m_len);
15614                 }
15615                 len -= n->m_len;
15616                 off = 0;
15617                 m = m->m_next;
15618                 np = &n->m_next;
15619                 if (len || (soff == smb->m_len)) {
15620                         /*
15621                          * We have more so we move forward  or
15622                          * we have consumed the entire mbuf and
15623                          * len has fell to 0.
15624                          */
15625                         soff = 0;
15626                         smb = m;
15627                 }
15628
15629         }
15630         if (fsb != NULL) {
15631                 fsb->m = smb;
15632                 fsb->off = soff;
15633                 if (smb) {
15634                         /*
15635                          * Save off the size of the mbuf. We do
15636                          * this so that we can recognize when it
15637                          * has been trimmed by sbcut() as acks
15638                          * come in.
15639                          */
15640                         fsb->o_m_len = smb->m_len;
15641                 } else {
15642                         /*
15643                          * This is the case where the next mbuf went to NULL. This
15644                          * means with this copy we have sent everything in the sb.
15645                          * In theory we could clear the fast_output flag, but lets
15646                          * not since its possible that we could get more added
15647                          * and acks that call the extend function which would let
15648                          * us send more.
15649                          */
15650                         fsb->o_m_len = 0;
15651                 }
15652         }
15653         return (top);
15654 nospace:
15655         if (top)
15656                 m_freem(top);
15657         return (NULL);
15658
15659 }
15660
15661 /*
15662  * This is a copy of m_copym(), taking the TSO segment size/limit
15663  * constraints into account, and advancing the sndptr as it goes.
15664  */
15665 static struct mbuf *
15666 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
15667                 int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
15668 {
15669         struct mbuf *m, *n;
15670         int32_t soff;
15671
15672         soff = rack->r_ctl.fsb.off;
15673         m = rack->r_ctl.fsb.m;
15674         if (rack->r_ctl.fsb.o_m_len > m->m_len) {
15675                 /*
15676                  * The mbuf had the front of it chopped off by an ack
15677                  * we need to adjust the soff/off by that difference.
15678                  */
15679                 uint32_t delta;
15680
15681                 delta = rack->r_ctl.fsb.o_m_len - m->m_len;
15682                 soff -= delta;
15683         } else if (rack->r_ctl.fsb.o_m_len < m->m_len) {
15684                 /*
15685                  * The mbuf was expanded probably by
15686                  * a m_compress. Just update o_m_len.
15687                  */
15688                 rack->r_ctl.fsb.o_m_len = m->m_len;
15689         }
15690         KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
15691         KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
15692         KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
15693                                  __FUNCTION__,
15694                                  rack, *plen, m, m->m_len));
15695         /* Save off the right location before we copy and advance */
15696         *s_soff = soff;
15697         *s_mb = rack->r_ctl.fsb.m;
15698         n = rack_fo_base_copym(m, soff, plen,
15699                                &rack->r_ctl.fsb,
15700                                seglimit, segsize, rack->r_ctl.fsb.hw_tls);
15701         return (n);
15702 }
15703
15704 static int
15705 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
15706                      uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
15707 {
15708         /*
15709          * Enter the fast retransmit path. We are given that a sched_pin is
15710          * in place (if accounting is compliled in) and the cycle count taken
15711          * at the entry is in the ts_val. The concept her is that the rsm
15712          * now holds the mbuf offsets and such so we can directly transmit
15713          * without a lot of overhead, the len field is already set for
15714          * us to prohibit us from sending too much (usually its 1MSS).
15715          */
15716         struct ip *ip = NULL;
15717         struct udphdr *udp = NULL;
15718         struct tcphdr *th = NULL;
15719         struct mbuf *m = NULL;
15720         struct inpcb *inp;
15721         uint8_t *cpto;
15722         struct tcp_log_buffer *lgb;
15723 #ifdef TCP_ACCOUNTING
15724         uint64_t crtsc;
15725         int cnt_thru = 1;
15726 #endif
15727         struct tcpopt to;
15728         u_char opt[TCP_MAXOLEN];
15729         uint32_t hdrlen, optlen;
15730         int32_t slot, segsiz, max_val, tso = 0, error, flags, ulen = 0;
15731         uint32_t us_cts;
15732         uint32_t if_hw_tsomaxsegcount = 0, startseq;
15733         uint32_t if_hw_tsomaxsegsize;
15734
15735 #ifdef INET6
15736         struct ip6_hdr *ip6 = NULL;
15737
15738         if (rack->r_is_v6) {
15739                 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
15740                 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
15741         } else
15742 #endif                          /* INET6 */
15743         {
15744                 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
15745                 hdrlen = sizeof(struct tcpiphdr);
15746         }
15747         if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
15748                 goto failed;
15749         }
15750         if (doing_tlp) {
15751                 /* Its a TLP add the flag, it may already be there but be sure */
15752                 rsm->r_flags |= RACK_TLP;
15753         } else {
15754                 /* If it was a TLP it is not not on this retransmit */
15755                 rsm->r_flags &= ~RACK_TLP;
15756         }
15757         startseq = rsm->r_start;
15758         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
15759         inp = rack->rc_inp;
15760         to.to_flags = 0;
15761         flags = tcp_outflags[tp->t_state];
15762         if (flags & (TH_SYN|TH_RST)) {
15763                 goto failed;
15764         }
15765         if (rsm->r_flags & RACK_HAS_FIN) {
15766                 /* We can't send a FIN here */
15767                 goto failed;
15768         }
15769         if (flags & TH_FIN) {
15770                 /* We never send a FIN */
15771                 flags &= ~TH_FIN;
15772         }
15773         if (tp->t_flags & TF_RCVD_TSTMP) {
15774                 to.to_tsval = ms_cts + tp->ts_offset;
15775                 to.to_tsecr = tp->ts_recent;
15776                 to.to_flags = TOF_TS;
15777         }
15778         optlen = tcp_addoptions(&to, opt);
15779         hdrlen += optlen;
15780         udp = rack->r_ctl.fsb.udp;
15781         if (udp)
15782                 hdrlen += sizeof(struct udphdr);
15783         if (rack->r_ctl.rc_pace_max_segs)
15784                 max_val = rack->r_ctl.rc_pace_max_segs;
15785         else if (rack->rc_user_set_max_segs)
15786                 max_val = rack->rc_user_set_max_segs * segsiz;
15787         else
15788                 max_val = len;
15789         if ((tp->t_flags & TF_TSO) &&
15790             V_tcp_do_tso &&
15791             (len > segsiz) &&
15792             (tp->t_port == 0))
15793                 tso = 1;
15794 #ifdef INET6
15795         if (MHLEN < hdrlen + max_linkhdr)
15796                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
15797         else
15798 #endif
15799                 m = m_gethdr(M_NOWAIT, MT_DATA);
15800         if (m == NULL)
15801                 goto failed;
15802         m->m_data += max_linkhdr;
15803         m->m_len = hdrlen;
15804         th = rack->r_ctl.fsb.th;
15805         /* Establish the len to send */
15806         if (len > max_val)
15807                 len = max_val;
15808         if ((tso) && (len + optlen > tp->t_maxseg)) {
15809                 uint32_t if_hw_tsomax;
15810                 int32_t max_len;
15811
15812                 /* extract TSO information */
15813                 if_hw_tsomax = tp->t_tsomax;
15814                 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
15815                 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
15816                 /*
15817                  * Check if we should limit by maximum payload
15818                  * length:
15819                  */
15820                 if (if_hw_tsomax != 0) {
15821                         /* compute maximum TSO length */
15822                         max_len = (if_hw_tsomax - hdrlen -
15823                                    max_linkhdr);
15824                         if (max_len <= 0) {
15825                                 goto failed;
15826                         } else if (len > max_len) {
15827                                 len = max_len;
15828                         }
15829                 }
15830                 if (len <= segsiz) {
15831                         /*
15832                          * In case there are too many small fragments don't
15833                          * use TSO:
15834                          */
15835                         tso = 0;
15836                 }
15837         } else {
15838                 tso = 0;
15839         }
15840         if ((tso == 0) && (len > segsiz))
15841                 len = segsiz;
15842         us_cts = tcp_get_usecs(tv);
15843         if ((len == 0) ||
15844             (len <= MHLEN - hdrlen - max_linkhdr)) {
15845                 goto failed;
15846         }
15847         th->th_seq = htonl(rsm->r_start);
15848         th->th_ack = htonl(tp->rcv_nxt);
15849         /*
15850          * The PUSH bit should only be applied
15851          * if the full retransmission is made. If
15852          * we are sending less than this is the
15853          * left hand edge and should not have
15854          * the PUSH bit.
15855          */
15856         if ((rsm->r_flags & RACK_HAD_PUSH) &&
15857             (len == (rsm->r_end - rsm->r_start)))
15858                 flags |= TH_PUSH;
15859         th->th_flags = flags;
15860         th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
15861         if (th->th_win == 0) {
15862                 tp->t_sndzerowin++;
15863                 tp->t_flags |= TF_RXWIN0SENT;
15864         } else
15865                 tp->t_flags &= ~TF_RXWIN0SENT;
15866         if (rsm->r_flags & RACK_TLP) {
15867                 /*
15868                  * TLP should not count in retran count, but
15869                  * in its own bin
15870                  */
15871                 counter_u64_add(rack_tlp_retran, 1);
15872                 counter_u64_add(rack_tlp_retran_bytes, len);
15873         } else {
15874                 tp->t_sndrexmitpack++;
15875                 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
15876                 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
15877         }
15878 #ifdef STATS
15879         stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
15880                                  len);
15881 #endif
15882         if (rsm->m == NULL)
15883                 goto failed;
15884         if (rsm->orig_m_len != rsm->m->m_len) {
15885                 /* Fix up the orig_m_len and possibly the mbuf offset */
15886                 rack_adjust_orig_mlen(rsm);
15887         }
15888         m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
15889         if (len <= segsiz) {
15890                 /*
15891                  * Must have ran out of mbufs for the copy
15892                  * shorten it to no longer need tso. Lets
15893                  * not put on sendalot since we are low on
15894                  * mbufs.
15895                  */
15896                 tso = 0;
15897         }
15898         if ((m->m_next == NULL) || (len <= 0)){
15899                 goto failed;
15900         }
15901         if (udp) {
15902                 if (rack->r_is_v6)
15903                         ulen = hdrlen + len - sizeof(struct ip6_hdr);
15904                 else
15905                         ulen = hdrlen + len - sizeof(struct ip);
15906                 udp->uh_ulen = htons(ulen);
15907         }
15908         m->m_pkthdr.rcvif = (struct ifnet *)0;
15909         m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
15910 #ifdef INET6
15911         if (rack->r_is_v6) {
15912                 if (tp->t_port) {
15913                         m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
15914                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
15915                         udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
15916                         th->th_sum = htons(0);
15917                         UDPSTAT_INC(udps_opackets);
15918                 } else {
15919                         m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
15920                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
15921                         th->th_sum = in6_cksum_pseudo(ip6,
15922                                                       sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
15923                                                       0);
15924                 }
15925         }
15926 #endif
15927 #if defined(INET6) && defined(INET)
15928         else
15929 #endif
15930 #ifdef INET
15931         {
15932                 if (tp->t_port) {
15933                         m->m_pkthdr.csum_flags = CSUM_UDP;
15934                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
15935                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
15936                                                 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
15937                         th->th_sum = htons(0);
15938                         UDPSTAT_INC(udps_opackets);
15939                 } else {
15940                         m->m_pkthdr.csum_flags = CSUM_TCP;
15941                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
15942                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
15943                                                ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
15944                                                                         IPPROTO_TCP + len + optlen));
15945                 }
15946                 /* IP version must be set here for ipv4/ipv6 checking later */
15947                 KASSERT(ip->ip_v == IPVERSION,
15948                         ("%s: IP version incorrect: %d", __func__, ip->ip_v));
15949         }
15950 #endif
15951         if (tso) {
15952                 KASSERT(len > tp->t_maxseg - optlen,
15953                         ("%s: len <= tso_segsz tp:%p", __func__, tp));
15954                 m->m_pkthdr.csum_flags |= CSUM_TSO;
15955                 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
15956         }
15957 #ifdef INET6
15958         if (rack->r_is_v6) {
15959                 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
15960                 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
15961                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
15962                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
15963                 else
15964                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
15965         }
15966 #endif
15967 #if defined(INET) && defined(INET6)
15968         else
15969 #endif
15970 #ifdef INET
15971         {
15972                 ip->ip_len = htons(m->m_pkthdr.len);
15973                 ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
15974                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
15975                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
15976                         if (tp->t_port == 0 || len < V_tcp_minmss) {
15977                                 ip->ip_off |= htons(IP_DF);
15978                         }
15979                 } else {
15980                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
15981                 }
15982         }
15983 #endif
15984         /* Time to copy in our header */
15985         cpto = mtod(m, uint8_t *);
15986         memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
15987         th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
15988         if (optlen) {
15989                 bcopy(opt, th + 1, optlen);
15990                 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
15991         } else {
15992                 th->th_off = sizeof(struct tcphdr) >> 2;
15993         }
15994         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15995                 union tcp_log_stackspecific log;
15996
15997                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15998                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
15999                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
16000                 if (rack->rack_no_prr)
16001                         log.u_bbr.flex1 = 0;
16002                 else
16003                         log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16004                 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16005                 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16006                 log.u_bbr.flex4 = max_val;
16007                 log.u_bbr.flex5 = 0;
16008                 /* Save off the early/late values */
16009                 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16010                 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16011                 log.u_bbr.bw_inuse = rack_get_bw(rack);
16012                 if (doing_tlp == 0)
16013                         log.u_bbr.flex8 = 1;
16014                 else
16015                         log.u_bbr.flex8 = 2;
16016                 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16017                 log.u_bbr.flex7 = 55;
16018                 log.u_bbr.pkts_out = tp->t_maxseg;
16019                 log.u_bbr.timeStamp = cts;
16020                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16021                 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16022                 log.u_bbr.delivered = 0;
16023                 lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16024                                      len, &log, false, NULL, NULL, 0, tv);
16025         } else
16026                 lgb = NULL;
16027 #ifdef INET6
16028         if (rack->r_is_v6) {
16029                 error = ip6_output(m, NULL,
16030                                    &inp->inp_route6,
16031                                    0, NULL, NULL, inp);
16032         }
16033 #endif
16034 #if defined(INET) && defined(INET6)
16035         else
16036 #endif
16037 #ifdef INET
16038         {
16039                 error = ip_output(m, NULL,
16040                                   &inp->inp_route,
16041                                   0, 0, inp);
16042         }
16043 #endif
16044         m = NULL;
16045         if (lgb) {
16046                 lgb->tlb_errno = error;
16047                 lgb = NULL;
16048         }
16049         if (error) {
16050                 goto failed;
16051         }
16052         rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
16053                         rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls);
16054         if (doing_tlp && (rack->fast_rsm_hack == 0)) {
16055                 rack->rc_tlp_in_progress = 1;
16056                 rack->r_ctl.rc_tlp_cnt_out++;
16057         }
16058         if (error == 0) {
16059                 tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
16060                 if (doing_tlp) {
16061                         rack->rc_last_sent_tlp_past_cumack = 0;
16062                         rack->rc_last_sent_tlp_seq_valid = 1;
16063                         rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
16064                         rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
16065                 }
16066         }
16067         tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16068         rack->forced_ack = 0;   /* If we send something zap the FA flag */
16069         if (IN_FASTRECOVERY(tp->t_flags) && rsm)
16070                 rack->r_ctl.retran_during_recovery += len;
16071         {
16072                 int idx;
16073
16074                 idx = (len / segsiz) + 3;
16075                 if (idx >= TCP_MSS_ACCT_ATIMER)
16076                         counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16077                 else
16078                         counter_u64_add(rack_out_size[idx], 1);
16079         }
16080         if (tp->t_rtttime == 0) {
16081                 tp->t_rtttime = ticks;
16082                 tp->t_rtseq = startseq;
16083                 KMOD_TCPSTAT_INC(tcps_segstimed);
16084         }
16085         counter_u64_add(rack_fto_rsm_send, 1);
16086         if (error && (error == ENOBUFS)) {
16087                 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
16088                 if (rack->rc_enobuf < 0x7f)
16089                         rack->rc_enobuf++;
16090                 if (slot < (10 * HPTS_USEC_IN_MSEC))
16091                         slot = 10 * HPTS_USEC_IN_MSEC;
16092         } else
16093                 slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
16094         if ((slot == 0) ||
16095             (rack->rc_always_pace == 0) ||
16096             (rack->r_rr_config == 1)) {
16097                 /*
16098                  * We have no pacing set or we
16099                  * are using old-style rack or
16100                  * we are overriden to use the old 1ms pacing.
16101                  */
16102                 slot = rack->r_ctl.rc_min_to;
16103         }
16104         rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
16105         if (rack->r_must_retran) {
16106                 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
16107                 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
16108                         /*
16109                          * We have retransmitted all we need.
16110                          */
16111                         rack->r_must_retran = 0;
16112                         rack->r_ctl.rc_out_at_rto = 0;
16113                 }
16114         }
16115 #ifdef TCP_ACCOUNTING
16116         crtsc = get_cyclecount();
16117         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16118                 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16119         }
16120         counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16121         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16122                 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16123         }
16124         counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16125         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16126                 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
16127         }
16128         counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((len + segsiz - 1) / segsiz));
16129         sched_unpin();
16130 #endif
16131         return (0);
16132 failed:
16133         if (m)
16134                 m_free(m);
16135         return (-1);
16136 }
16137
16138 static void
16139 rack_sndbuf_autoscale(struct tcp_rack *rack)
16140 {
16141         /*
16142          * Automatic sizing of send socket buffer.  Often the send buffer
16143          * size is not optimally adjusted to the actual network conditions
16144          * at hand (delay bandwidth product).  Setting the buffer size too
16145          * small limits throughput on links with high bandwidth and high
16146          * delay (eg. trans-continental/oceanic links).  Setting the
16147          * buffer size too big consumes too much real kernel memory,
16148          * especially with many connections on busy servers.
16149          *
16150          * The criteria to step up the send buffer one notch are:
16151          *  1. receive window of remote host is larger than send buffer
16152          *     (with a fudge factor of 5/4th);
16153          *  2. send buffer is filled to 7/8th with data (so we actually
16154          *     have data to make use of it);
16155          *  3. send buffer fill has not hit maximal automatic size;
16156          *  4. our send window (slow start and cogestion controlled) is
16157          *     larger than sent but unacknowledged data in send buffer.
16158          *
16159          * Note that the rack version moves things much faster since
16160          * we want to avoid hitting cache lines in the rack_fast_output()
16161          * path so this is called much less often and thus moves
16162          * the SB forward by a percentage.
16163          */
16164         struct socket *so;
16165         struct tcpcb *tp;
16166         uint32_t sendwin, scaleup;
16167
16168         tp = rack->rc_tp;
16169         so = rack->rc_inp->inp_socket;
16170         sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
16171         if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
16172                 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
16173                     sbused(&so->so_snd) >=
16174                     (so->so_snd.sb_hiwat / 8 * 7) &&
16175                     sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
16176                     sendwin >= (sbused(&so->so_snd) -
16177                     (tp->snd_nxt - tp->snd_una))) {
16178                         if (rack_autosndbuf_inc)
16179                                 scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
16180                         else
16181                                 scaleup = V_tcp_autosndbuf_inc;
16182                         if (scaleup < V_tcp_autosndbuf_inc)
16183                                 scaleup = V_tcp_autosndbuf_inc;
16184                         scaleup += so->so_snd.sb_hiwat;
16185                         if (scaleup > V_tcp_autosndbuf_max)
16186                                 scaleup = V_tcp_autosndbuf_max;
16187                         if (!sbreserve_locked(&so->so_snd, scaleup, so, curthread))
16188                                 so->so_snd.sb_flags &= ~SB_AUTOSIZE;
16189                 }
16190         }
16191 }
16192
16193 static int
16194 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
16195                  uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
16196 {
16197         /*
16198          * Enter to do fast output. We are given that the sched_pin is
16199          * in place (if accounting is compiled in) and the cycle count taken
16200          * at entry is in place in ts_val. The idea here is that
16201          * we know how many more bytes needs to be sent (presumably either
16202          * during pacing or to fill the cwnd and that was greater than
16203          * the max-burst). We have how much to send and all the info we
16204          * need to just send.
16205          */
16206         struct ip *ip = NULL;
16207         struct udphdr *udp = NULL;
16208         struct tcphdr *th = NULL;
16209         struct mbuf *m, *s_mb;
16210         struct inpcb *inp;
16211         uint8_t *cpto;
16212         struct tcp_log_buffer *lgb;
16213 #ifdef TCP_ACCOUNTING
16214         uint64_t crtsc;
16215 #endif
16216         struct tcpopt to;
16217         u_char opt[TCP_MAXOLEN];
16218         uint32_t hdrlen, optlen;
16219         int cnt_thru = 1;
16220         int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, flags, ulen = 0;
16221         uint32_t us_cts, s_soff;
16222         uint32_t if_hw_tsomaxsegcount = 0, startseq;
16223         uint32_t if_hw_tsomaxsegsize;
16224         uint16_t add_flag = RACK_SENT_FP;
16225 #ifdef INET6
16226         struct ip6_hdr *ip6 = NULL;
16227
16228         if (rack->r_is_v6) {
16229                 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
16230                 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
16231         } else
16232 #endif                          /* INET6 */
16233         {
16234                 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
16235                 hdrlen = sizeof(struct tcpiphdr);
16236         }
16237         if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
16238                 m = NULL;
16239                 goto failed;
16240         }
16241         startseq = tp->snd_max;
16242         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16243         inp = rack->rc_inp;
16244         len = rack->r_ctl.fsb.left_to_send;
16245         to.to_flags = 0;
16246         flags = rack->r_ctl.fsb.tcp_flags;
16247         if (tp->t_flags & TF_RCVD_TSTMP) {
16248                 to.to_tsval = ms_cts + tp->ts_offset;
16249                 to.to_tsecr = tp->ts_recent;
16250                 to.to_flags = TOF_TS;
16251         }
16252         optlen = tcp_addoptions(&to, opt);
16253         hdrlen += optlen;
16254         udp = rack->r_ctl.fsb.udp;
16255         if (udp)
16256                 hdrlen += sizeof(struct udphdr);
16257         if (rack->r_ctl.rc_pace_max_segs)
16258                 max_val = rack->r_ctl.rc_pace_max_segs;
16259         else if (rack->rc_user_set_max_segs)
16260                 max_val = rack->rc_user_set_max_segs * segsiz;
16261         else
16262                 max_val = len;
16263         if ((tp->t_flags & TF_TSO) &&
16264             V_tcp_do_tso &&
16265             (len > segsiz) &&
16266             (tp->t_port == 0))
16267                 tso = 1;
16268 again:
16269 #ifdef INET6
16270         if (MHLEN < hdrlen + max_linkhdr)
16271                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
16272         else
16273 #endif
16274                 m = m_gethdr(M_NOWAIT, MT_DATA);
16275         if (m == NULL)
16276                 goto failed;
16277         m->m_data += max_linkhdr;
16278         m->m_len = hdrlen;
16279         th = rack->r_ctl.fsb.th;
16280         /* Establish the len to send */
16281         if (len > max_val)
16282                 len = max_val;
16283         if ((tso) && (len + optlen > tp->t_maxseg)) {
16284                 uint32_t if_hw_tsomax;
16285                 int32_t max_len;
16286
16287                 /* extract TSO information */
16288                 if_hw_tsomax = tp->t_tsomax;
16289                 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
16290                 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
16291                 /*
16292                  * Check if we should limit by maximum payload
16293                  * length:
16294                  */
16295                 if (if_hw_tsomax != 0) {
16296                         /* compute maximum TSO length */
16297                         max_len = (if_hw_tsomax - hdrlen -
16298                                    max_linkhdr);
16299                         if (max_len <= 0) {
16300                                 goto failed;
16301                         } else if (len > max_len) {
16302                                 len = max_len;
16303                         }
16304                 }
16305                 if (len <= segsiz) {
16306                         /*
16307                          * In case there are too many small fragments don't
16308                          * use TSO:
16309                          */
16310                         tso = 0;
16311                 }
16312         } else {
16313                 tso = 0;
16314         }
16315         if ((tso == 0) && (len > segsiz))
16316                 len = segsiz;
16317         us_cts = tcp_get_usecs(tv);
16318         if ((len == 0) ||
16319             (len <= MHLEN - hdrlen - max_linkhdr)) {
16320                 goto failed;
16321         }
16322         sb_offset = tp->snd_max - tp->snd_una;
16323         th->th_seq = htonl(tp->snd_max);
16324         th->th_ack = htonl(tp->rcv_nxt);
16325         th->th_flags = flags;
16326         th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
16327         if (th->th_win == 0) {
16328                 tp->t_sndzerowin++;
16329                 tp->t_flags |= TF_RXWIN0SENT;
16330         } else
16331                 tp->t_flags &= ~TF_RXWIN0SENT;
16332         tp->snd_up = tp->snd_una;       /* drag it along, its deprecated */
16333         KMOD_TCPSTAT_INC(tcps_sndpack);
16334         KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
16335 #ifdef STATS
16336         stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
16337                                  len);
16338 #endif
16339         if (rack->r_ctl.fsb.m == NULL)
16340                 goto failed;
16341
16342         /* s_mb and s_soff are saved for rack_log_output */
16343         m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
16344                                     &s_mb, &s_soff);
16345         if (len <= segsiz) {
16346                 /*
16347                  * Must have ran out of mbufs for the copy
16348                  * shorten it to no longer need tso. Lets
16349                  * not put on sendalot since we are low on
16350                  * mbufs.
16351                  */
16352                 tso = 0;
16353         }
16354         if (rack->r_ctl.fsb.rfo_apply_push &&
16355             (len == rack->r_ctl.fsb.left_to_send)) {
16356                 th->th_flags |= TH_PUSH;
16357                 add_flag |= RACK_HAD_PUSH;
16358         }
16359         if ((m->m_next == NULL) || (len <= 0)){
16360                 goto failed;
16361         }
16362         if (udp) {
16363                 if (rack->r_is_v6)
16364                         ulen = hdrlen + len - sizeof(struct ip6_hdr);
16365                 else
16366                         ulen = hdrlen + len - sizeof(struct ip);
16367                 udp->uh_ulen = htons(ulen);
16368         }
16369         m->m_pkthdr.rcvif = (struct ifnet *)0;
16370         if (tp->t_state == TCPS_ESTABLISHED &&
16371             (tp->t_flags2 & TF2_ECN_PERMIT)) {
16372                 /*
16373                  * If the peer has ECN, mark data packets with ECN capable
16374                  * transmission (ECT). Ignore pure ack packets,
16375                  * retransmissions.
16376                  */
16377                 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max)) {
16378 #ifdef INET6
16379                         if (rack->r_is_v6)
16380                                 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
16381                         else
16382 #endif
16383                                 ip->ip_tos |= IPTOS_ECN_ECT0;
16384                         KMOD_TCPSTAT_INC(tcps_ecn_ect0);
16385                         /*
16386                          * Reply with proper ECN notifications.
16387                          * Only set CWR on new data segments.
16388                          */
16389                         if (tp->t_flags2 & TF2_ECN_SND_CWR) {
16390                                 flags |= TH_CWR;
16391                                 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
16392                         }
16393                 }
16394                 if (tp->t_flags2 & TF2_ECN_SND_ECE)
16395                         flags |= TH_ECE;
16396         }
16397         m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
16398 #ifdef INET6
16399         if (rack->r_is_v6) {
16400                 if (tp->t_port) {
16401                         m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
16402                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16403                         udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
16404                         th->th_sum = htons(0);
16405                         UDPSTAT_INC(udps_opackets);
16406                 } else {
16407                         m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
16408                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16409                         th->th_sum = in6_cksum_pseudo(ip6,
16410                                                       sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
16411                                                       0);
16412                 }
16413         }
16414 #endif
16415 #if defined(INET6) && defined(INET)
16416         else
16417 #endif
16418 #ifdef INET
16419         {
16420                 if (tp->t_port) {
16421                         m->m_pkthdr.csum_flags = CSUM_UDP;
16422                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
16423                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
16424                                                 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
16425                         th->th_sum = htons(0);
16426                         UDPSTAT_INC(udps_opackets);
16427                 } else {
16428                         m->m_pkthdr.csum_flags = CSUM_TCP;
16429                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
16430                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
16431                                                ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
16432                                                                         IPPROTO_TCP + len + optlen));
16433                 }
16434                 /* IP version must be set here for ipv4/ipv6 checking later */
16435                 KASSERT(ip->ip_v == IPVERSION,
16436                         ("%s: IP version incorrect: %d", __func__, ip->ip_v));
16437         }
16438 #endif
16439         if (tso) {
16440                 KASSERT(len > tp->t_maxseg - optlen,
16441                         ("%s: len <= tso_segsz tp:%p", __func__, tp));
16442                 m->m_pkthdr.csum_flags |= CSUM_TSO;
16443                 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
16444         }
16445 #ifdef INET6
16446         if (rack->r_is_v6) {
16447                 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
16448                 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
16449                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
16450                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16451                 else
16452                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16453         }
16454 #endif
16455 #if defined(INET) && defined(INET6)
16456         else
16457 #endif
16458 #ifdef INET
16459         {
16460                 ip->ip_len = htons(m->m_pkthdr.len);
16461                 ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
16462                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
16463                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
16464                         if (tp->t_port == 0 || len < V_tcp_minmss) {
16465                                 ip->ip_off |= htons(IP_DF);
16466                         }
16467                 } else {
16468                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16469                 }
16470         }
16471 #endif
16472         /* Time to copy in our header */
16473         cpto = mtod(m, uint8_t *);
16474         memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
16475         th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
16476         if (optlen) {
16477                 bcopy(opt, th + 1, optlen);
16478                 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
16479         } else {
16480                 th->th_off = sizeof(struct tcphdr) >> 2;
16481         }
16482         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
16483                 union tcp_log_stackspecific log;
16484
16485                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16486                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
16487                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
16488                 if (rack->rack_no_prr)
16489                         log.u_bbr.flex1 = 0;
16490                 else
16491                         log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16492                 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
16493                 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
16494                 log.u_bbr.flex4 = max_val;
16495                 log.u_bbr.flex5 = 0;
16496                 /* Save off the early/late values */
16497                 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
16498                 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
16499                 log.u_bbr.bw_inuse = rack_get_bw(rack);
16500                 log.u_bbr.flex8 = 0;
16501                 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
16502                 log.u_bbr.flex7 = 44;
16503                 log.u_bbr.pkts_out = tp->t_maxseg;
16504                 log.u_bbr.timeStamp = cts;
16505                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16506                 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
16507                 log.u_bbr.delivered = 0;
16508                 lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
16509                                      len, &log, false, NULL, NULL, 0, tv);
16510         } else
16511                 lgb = NULL;
16512 #ifdef INET6
16513         if (rack->r_is_v6) {
16514                 error = ip6_output(m, NULL,
16515                                    &inp->inp_route6,
16516                                    0, NULL, NULL, inp);
16517         }
16518 #endif
16519 #if defined(INET) && defined(INET6)
16520         else
16521 #endif
16522 #ifdef INET
16523         {
16524                 error = ip_output(m, NULL,
16525                                   &inp->inp_route,
16526                                   0, 0, inp);
16527         }
16528 #endif
16529         if (lgb) {
16530                 lgb->tlb_errno = error;
16531                 lgb = NULL;
16532         }
16533         if (error) {
16534                 *send_err = error;
16535                 m = NULL;
16536                 goto failed;
16537         }
16538         rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
16539                         NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls);
16540         m = NULL;
16541         if (tp->snd_una == tp->snd_max) {
16542                 rack->r_ctl.rc_tlp_rxt_last_time = cts;
16543                 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
16544                 tp->t_acktime = ticks;
16545         }
16546         if (error == 0)
16547                 tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
16548
16549         rack->forced_ack = 0;   /* If we send something zap the FA flag */
16550         tot_len += len;
16551         if ((tp->t_flags & TF_GPUTINPROG) == 0)
16552                 rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
16553         tp->snd_max += len;
16554         tp->snd_nxt = tp->snd_max;
16555         {
16556                 int idx;
16557
16558                 idx = (len / segsiz) + 3;
16559                 if (idx >= TCP_MSS_ACCT_ATIMER)
16560                         counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
16561                 else
16562                         counter_u64_add(rack_out_size[idx], 1);
16563         }
16564         if (len <= rack->r_ctl.fsb.left_to_send)
16565                 rack->r_ctl.fsb.left_to_send -= len;
16566         else
16567                 rack->r_ctl.fsb.left_to_send = 0;
16568         if (rack->r_ctl.fsb.left_to_send < segsiz) {
16569                 rack->r_fast_output = 0;
16570                 rack->r_ctl.fsb.left_to_send = 0;
16571                 /* At the end of fast_output scale up the sb */
16572                 SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
16573                 rack_sndbuf_autoscale(rack);
16574                 SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
16575         }
16576         if (tp->t_rtttime == 0) {
16577                 tp->t_rtttime = ticks;
16578                 tp->t_rtseq = startseq;
16579                 KMOD_TCPSTAT_INC(tcps_segstimed);
16580         }
16581         if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
16582             (max_val > len) &&
16583             (tso == 0)) {
16584                 max_val -= len;
16585                 len = segsiz;
16586                 th = rack->r_ctl.fsb.th;
16587                 cnt_thru++;
16588                 goto again;
16589         }
16590         tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
16591         counter_u64_add(rack_fto_send, 1);
16592         slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz);
16593         rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
16594 #ifdef TCP_ACCOUNTING
16595         crtsc = get_cyclecount();
16596         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16597                 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
16598         }
16599         counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru);
16600         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16601                 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
16602         }
16603         counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
16604         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16605                 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
16606         }
16607         counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len + segsiz - 1) / segsiz));
16608         sched_unpin();
16609 #endif
16610         return (0);
16611 failed:
16612         if (m)
16613                 m_free(m);
16614         rack->r_fast_output = 0;
16615         return (-1);
16616 }
16617
16618 static int
16619 rack_output(struct tcpcb *tp)
16620 {
16621         struct socket *so;
16622         uint32_t recwin;
16623         uint32_t sb_offset, s_moff = 0;
16624         int32_t len, flags, error = 0;
16625         struct mbuf *m, *s_mb = NULL;
16626         struct mbuf *mb;
16627         uint32_t if_hw_tsomaxsegcount = 0;
16628         uint32_t if_hw_tsomaxsegsize;
16629         int32_t segsiz, minseg;
16630         long tot_len_this_send = 0;
16631 #ifdef INET
16632         struct ip *ip = NULL;
16633 #endif
16634 #ifdef TCPDEBUG
16635         struct ipovly *ipov = NULL;
16636 #endif
16637         struct udphdr *udp = NULL;
16638         struct tcp_rack *rack;
16639         struct tcphdr *th;
16640         uint8_t pass = 0;
16641         uint8_t mark = 0;
16642         uint8_t wanted_cookie = 0;
16643         u_char opt[TCP_MAXOLEN];
16644         unsigned ipoptlen, optlen, hdrlen, ulen=0;
16645         uint32_t rack_seq;
16646
16647 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
16648         unsigned ipsec_optlen = 0;
16649
16650 #endif
16651         int32_t idle, sendalot;
16652         int32_t sub_from_prr = 0;
16653         volatile int32_t sack_rxmit;
16654         struct rack_sendmap *rsm = NULL;
16655         int32_t tso, mtu;
16656         struct tcpopt to;
16657         int32_t slot = 0;
16658         int32_t sup_rack = 0;
16659         uint32_t cts, ms_cts, delayed, early;
16660         uint16_t add_flag = RACK_SENT_SP;
16661         /* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
16662         uint8_t hpts_calling,  doing_tlp = 0;
16663         uint32_t cwnd_to_use, pace_max_seg;
16664         int32_t do_a_prefetch = 0;
16665         int32_t prefetch_rsm = 0;
16666         int32_t orig_len = 0;
16667         struct timeval tv;
16668         int32_t prefetch_so_done = 0;
16669         struct tcp_log_buffer *lgb;
16670         struct inpcb *inp;
16671         struct sockbuf *sb;
16672         uint64_t ts_val = 0;
16673 #ifdef TCP_ACCOUNTING
16674         uint64_t crtsc;
16675 #endif
16676 #ifdef INET6
16677         struct ip6_hdr *ip6 = NULL;
16678         int32_t isipv6;
16679 #endif
16680         uint8_t filled_all = 0;
16681         bool hw_tls = false;
16682
16683         /* setup and take the cache hits here */
16684         rack = (struct tcp_rack *)tp->t_fb_ptr;
16685 #ifdef TCP_ACCOUNTING
16686         sched_pin();
16687         ts_val = get_cyclecount();
16688 #endif
16689         hpts_calling = rack->rc_inp->inp_hpts_calls;
16690         NET_EPOCH_ASSERT();
16691         INP_WLOCK_ASSERT(rack->rc_inp);
16692 #ifdef TCP_OFFLOAD
16693         if (tp->t_flags & TF_TOE) {
16694 #ifdef TCP_ACCOUNTING
16695                 sched_unpin();
16696 #endif
16697                 return (tcp_offload_output(tp));
16698         }
16699 #endif
16700         /*
16701          * For TFO connections in SYN_RECEIVED, only allow the initial
16702          * SYN|ACK and those sent by the retransmit timer.
16703          */
16704         if (IS_FASTOPEN(tp->t_flags) &&
16705             (tp->t_state == TCPS_SYN_RECEIVED) &&
16706             SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
16707             (rack->r_ctl.rc_resend == NULL)) {         /* not a retransmit */
16708 #ifdef TCP_ACCOUNTING
16709                 sched_unpin();
16710 #endif
16711                 return (0);
16712         }
16713 #ifdef INET6
16714         if (rack->r_state) {
16715                 /* Use the cache line loaded if possible */
16716                 isipv6 = rack->r_is_v6;
16717         } else {
16718                 isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
16719         }
16720 #endif
16721         early = 0;
16722         cts = tcp_get_usecs(&tv);
16723         ms_cts = tcp_tv_to_mssectick(&tv);
16724         if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
16725             rack->rc_inp->inp_in_hpts) {
16726                 /*
16727                  * We are on the hpts for some timer but not hptsi output.
16728                  * Remove from the hpts unconditionally.
16729                  */
16730                 rack_timer_cancel(tp, rack, cts, __LINE__);
16731         }
16732         /* Are we pacing and late? */
16733         if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16734             TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
16735                 /* We are delayed */
16736                 delayed = cts - rack->r_ctl.rc_last_output_to;
16737         } else {
16738                 delayed = 0;
16739         }
16740         /* Do the timers, which may override the pacer */
16741         if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
16742                 if (rack_process_timers(tp, rack, cts, hpts_calling, &doing_tlp)) {
16743                         counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
16744 #ifdef TCP_ACCOUNTING
16745                         sched_unpin();
16746 #endif
16747                         return (0);
16748                 }
16749         }
16750         if (rack->rc_in_persist) {
16751                 if (rack->rc_inp->inp_in_hpts == 0) {
16752                         /* Timer is not running */
16753                         rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
16754                 }
16755 #ifdef TCP_ACCOUNTING
16756                 sched_unpin();
16757 #endif
16758                 return (0);
16759         }
16760         if ((rack->r_timer_override) ||
16761             (rack->rc_ack_can_sendout_data) ||
16762             (delayed) ||
16763             (tp->t_state < TCPS_ESTABLISHED)) {
16764                 rack->rc_ack_can_sendout_data = 0;
16765                 if (rack->rc_inp->inp_in_hpts)
16766                         tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
16767         } else if (rack->rc_inp->inp_in_hpts) {
16768                 /*
16769                  * On the hpts you can't pass even if ACKNOW is on, we will
16770                  * when the hpts fires.
16771                  */
16772 #ifdef TCP_ACCOUNTING
16773                 crtsc = get_cyclecount();
16774                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16775                         tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
16776                 }
16777                 counter_u64_add(tcp_proc_time[SND_BLOCKED], (crtsc - ts_val));
16778                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16779                         tp->tcp_cnt_counters[SND_BLOCKED]++;
16780                 }
16781                 counter_u64_add(tcp_cnt_counters[SND_BLOCKED], 1);
16782                 sched_unpin();
16783 #endif
16784                 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
16785                 return (0);
16786         }
16787         rack->rc_inp->inp_hpts_calls = 0;
16788         /* Finish out both pacing early and late accounting */
16789         if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16790             TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
16791                 early = rack->r_ctl.rc_last_output_to - cts;
16792         } else
16793                 early = 0;
16794         if (delayed) {
16795                 rack->r_ctl.rc_agg_delayed += delayed;
16796                 rack->r_late = 1;
16797         } else if (early) {
16798                 rack->r_ctl.rc_agg_early += early;
16799                 rack->r_early = 1;
16800         }
16801         /* Now that early/late accounting is done turn off the flag */
16802         rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
16803         rack->r_wanted_output = 0;
16804         rack->r_timer_override = 0;
16805         if ((tp->t_state != rack->r_state) &&
16806             TCPS_HAVEESTABLISHED(tp->t_state)) {
16807                 rack_set_state(tp, rack);
16808         }
16809         if ((rack->r_fast_output) &&
16810             (doing_tlp == 0) &&
16811             (tp->rcv_numsacks == 0)) {
16812                 int ret;
16813
16814                 error = 0;
16815                 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
16816                 if (ret >= 0)
16817                         return(ret);
16818                 else if (error) {
16819                         inp = rack->rc_inp;
16820                         so = inp->inp_socket;
16821                         sb = &so->so_snd;
16822                         goto nomore;
16823                 }
16824         }
16825         inp = rack->rc_inp;
16826         /*
16827          * For TFO connections in SYN_SENT or SYN_RECEIVED,
16828          * only allow the initial SYN or SYN|ACK and those sent
16829          * by the retransmit timer.
16830          */
16831         if (IS_FASTOPEN(tp->t_flags) &&
16832             ((tp->t_state == TCPS_SYN_RECEIVED) ||
16833              (tp->t_state == TCPS_SYN_SENT)) &&
16834             SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
16835             (tp->t_rxtshift == 0)) {              /* not a retransmit */
16836                 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
16837                 so = inp->inp_socket;
16838                 sb = &so->so_snd;
16839                 goto just_return_nolock;
16840         }
16841         /*
16842          * Determine length of data that should be transmitted, and flags
16843          * that will be used. If there is some data or critical controls
16844          * (SYN, RST) to send, then transmit; otherwise, investigate
16845          * further.
16846          */
16847         idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
16848         if (tp->t_idle_reduce) {
16849                 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
16850                         rack_cc_after_idle(rack, tp);
16851         }
16852         tp->t_flags &= ~TF_LASTIDLE;
16853         if (idle) {
16854                 if (tp->t_flags & TF_MORETOCOME) {
16855                         tp->t_flags |= TF_LASTIDLE;
16856                         idle = 0;
16857                 }
16858         }
16859         if ((tp->snd_una == tp->snd_max) &&
16860             rack->r_ctl.rc_went_idle_time &&
16861             TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) {
16862                 idle = cts - rack->r_ctl.rc_went_idle_time;
16863                 if (idle > rack_min_probertt_hold) {
16864                         /* Count as a probe rtt */
16865                         if (rack->in_probe_rtt == 0) {
16866                                 rack->r_ctl.rc_lower_rtt_us_cts = cts;
16867                                 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
16868                                 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
16869                                 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
16870                         } else {
16871                                 rack_exit_probertt(rack, cts);
16872                         }
16873                 }
16874                 idle = 0;
16875         }
16876         if (rack_use_fsb && (rack->r_fsb_inited == 0) && (rack->r_state != TCPS_CLOSED))
16877                 rack_init_fsb_block(tp, rack);
16878 again:
16879         /*
16880          * If we've recently taken a timeout, snd_max will be greater than
16881          * snd_nxt.  There may be SACK information that allows us to avoid
16882          * resending already delivered data.  Adjust snd_nxt accordingly.
16883          */
16884         sendalot = 0;
16885         cts = tcp_get_usecs(&tv);
16886         ms_cts = tcp_tv_to_mssectick(&tv);
16887         tso = 0;
16888         mtu = 0;
16889         segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16890         minseg = segsiz;
16891         if (rack->r_ctl.rc_pace_max_segs == 0)
16892                 pace_max_seg = rack->rc_user_set_max_segs * segsiz;
16893         else
16894                 pace_max_seg = rack->r_ctl.rc_pace_max_segs;
16895         sb_offset = tp->snd_max - tp->snd_una;
16896         cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
16897         flags = tcp_outflags[tp->t_state];
16898         while (rack->rc_free_cnt < rack_free_cache) {
16899                 rsm = rack_alloc(rack);
16900                 if (rsm == NULL) {
16901                         if (inp->inp_hpts_calls)
16902                                 /* Retry in a ms */
16903                                 slot = (1 * HPTS_USEC_IN_MSEC);
16904                         so = inp->inp_socket;
16905                         sb = &so->so_snd;
16906                         goto just_return_nolock;
16907                 }
16908                 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
16909                 rack->rc_free_cnt++;
16910                 rsm = NULL;
16911         }
16912         if (inp->inp_hpts_calls)
16913                 inp->inp_hpts_calls = 0;
16914         sack_rxmit = 0;
16915         len = 0;
16916         rsm = NULL;
16917         if (flags & TH_RST) {
16918                 SOCKBUF_LOCK(&inp->inp_socket->so_snd);
16919                 so = inp->inp_socket;
16920                 sb = &so->so_snd;
16921                 goto send;
16922         }
16923         if (rack->r_ctl.rc_resend) {
16924                 /* Retransmit timer */
16925                 rsm = rack->r_ctl.rc_resend;
16926                 rack->r_ctl.rc_resend = NULL;
16927                 len = rsm->r_end - rsm->r_start;
16928                 sack_rxmit = 1;
16929                 sendalot = 0;
16930                 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
16931                         ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
16932                          __func__, __LINE__,
16933                          rsm->r_start, tp->snd_una, tp, rack, rsm));
16934                 sb_offset = rsm->r_start - tp->snd_una;
16935                 if (len >= segsiz)
16936                         len = segsiz;
16937         } else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
16938                 /* We have a retransmit that takes precedence */
16939                 if ((!IN_FASTRECOVERY(tp->t_flags)) &&
16940                     ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
16941                         /* Enter recovery if not induced by a time-out */
16942                         rack->r_ctl.rc_rsm_start = rsm->r_start;
16943                         rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
16944                         rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
16945                         rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
16946                 }
16947 #ifdef INVARIANTS
16948                 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
16949                         panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
16950                               tp, rack, rsm, rsm->r_start, tp->snd_una);
16951                 }
16952 #endif
16953                 len = rsm->r_end - rsm->r_start;
16954                 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
16955                         ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
16956                          __func__, __LINE__,
16957                          rsm->r_start, tp->snd_una, tp, rack, rsm));
16958                 sb_offset = rsm->r_start - tp->snd_una;
16959                 sendalot = 0;
16960                 if (len >= segsiz)
16961                         len = segsiz;
16962                 if (len > 0) {
16963                         sack_rxmit = 1;
16964                         KMOD_TCPSTAT_INC(tcps_sack_rexmits);
16965                         KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
16966                             min(len, segsiz));
16967                         counter_u64_add(rack_rtm_prr_retran, 1);
16968                 }
16969         } else if (rack->r_ctl.rc_tlpsend) {
16970                 /* Tail loss probe */
16971                 long cwin;
16972                 long tlen;
16973
16974                 /*
16975                  * Check if we can do a TLP with a RACK'd packet
16976                  * this can happen if we are not doing the rack
16977                  * cheat and we skipped to a TLP and it
16978                  * went off.
16979                  */
16980                 rsm = rack->r_ctl.rc_tlpsend;
16981                 /* We are doing a TLP make sure the flag is preent */
16982                 rsm->r_flags |= RACK_TLP;
16983                 rack->r_ctl.rc_tlpsend = NULL;
16984                 sack_rxmit = 1;
16985                 tlen = rsm->r_end - rsm->r_start;
16986                 if (tlen > segsiz)
16987                         tlen = segsiz;
16988                 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
16989                         ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
16990                          __func__, __LINE__,
16991                          rsm->r_start, tp->snd_una, tp, rack, rsm));
16992                 sb_offset = rsm->r_start - tp->snd_una;
16993                 cwin = min(tp->snd_wnd, tlen);
16994                 len = cwin;
16995         }
16996         if (rack->r_must_retran &&
16997             (rsm == NULL)) {
16998                 /*
16999                  * Non-Sack and we had a RTO or MTU change, we
17000                  * need to retransmit until we reach
17001                  * the former snd_max (rack->r_ctl.rc_snd_max_at_rto).
17002                  */
17003                 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
17004                         int sendwin, flight;
17005
17006                         sendwin = min(tp->snd_wnd, tp->snd_cwnd);
17007                         flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
17008                         if (flight >= sendwin) {
17009                                 so = inp->inp_socket;
17010                                 sb = &so->so_snd;
17011                                 goto just_return_nolock;
17012                         }
17013                         rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17014                         if (rsm == NULL) {
17015                                 /* TSNH */
17016                                 rack->r_must_retran = 0;
17017                                 rack->r_ctl.rc_out_at_rto = 0;
17018                                 rack->r_must_retran = 0;
17019                                 so = inp->inp_socket;
17020                                 sb = &so->so_snd;
17021                                 goto just_return_nolock;
17022                         }
17023                         sack_rxmit = 1;
17024                         len = rsm->r_end - rsm->r_start;
17025                         sendalot = 0;
17026                         sb_offset = rsm->r_start - tp->snd_una;
17027                         if (len >= segsiz)
17028                                 len = segsiz;
17029                 } else {
17030                         /* We must be done if there is nothing outstanding */
17031                         rack->r_must_retran = 0;
17032                         rack->r_ctl.rc_out_at_rto = 0;
17033                 }
17034         }
17035         /*
17036          * Enforce a connection sendmap count limit if set
17037          * as long as we are not retransmiting.
17038          */
17039         if ((rsm == NULL) &&
17040             (rack->do_detection == 0) &&
17041             (V_tcp_map_entries_limit > 0) &&
17042             (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
17043                 counter_u64_add(rack_to_alloc_limited, 1);
17044                 if (!rack->alloc_limit_reported) {
17045                         rack->alloc_limit_reported = 1;
17046                         counter_u64_add(rack_alloc_limited_conns, 1);
17047                 }
17048                 so = inp->inp_socket;
17049                 sb = &so->so_snd;
17050                 goto just_return_nolock;
17051         }
17052         if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
17053                 /* we are retransmitting the fin */
17054                 len--;
17055                 if (len) {
17056                         /*
17057                          * When retransmitting data do *not* include the
17058                          * FIN. This could happen from a TLP probe.
17059                          */
17060                         flags &= ~TH_FIN;
17061                 }
17062         }
17063 #ifdef INVARIANTS
17064         /* For debugging */
17065         rack->r_ctl.rc_rsm_at_retran = rsm;
17066 #endif
17067         if (rsm && rack->r_fsb_inited && rack_use_rsm_rfo &&
17068             ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
17069                 int ret;
17070
17071                 ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
17072                 if (ret == 0)
17073                         return (0);
17074         }
17075         so = inp->inp_socket;
17076         sb = &so->so_snd;
17077         if (do_a_prefetch == 0) {
17078                 kern_prefetch(sb, &do_a_prefetch);
17079                 do_a_prefetch = 1;
17080         }
17081 #ifdef NETFLIX_SHARED_CWND
17082         if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
17083             rack->rack_enable_scwnd) {
17084                 /* We are doing cwnd sharing */
17085                 if (rack->gp_ready &&
17086                     (rack->rack_attempted_scwnd == 0) &&
17087                     (rack->r_ctl.rc_scw == NULL) &&
17088                     tp->t_lib) {
17089                         /* The pcbid is in, lets make an attempt */
17090                         counter_u64_add(rack_try_scwnd, 1);
17091                         rack->rack_attempted_scwnd = 1;
17092                         rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
17093                                                                    &rack->r_ctl.rc_scw_index,
17094                                                                    segsiz);
17095                 }
17096                 if (rack->r_ctl.rc_scw &&
17097                     (rack->rack_scwnd_is_idle == 1) &&
17098                     sbavail(&so->so_snd)) {
17099                         /* we are no longer out of data */
17100                         tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
17101                         rack->rack_scwnd_is_idle = 0;
17102                 }
17103                 if (rack->r_ctl.rc_scw) {
17104                         /* First lets update and get the cwnd */
17105                         rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
17106                                                                     rack->r_ctl.rc_scw_index,
17107                                                                     tp->snd_cwnd, tp->snd_wnd, segsiz);
17108                 }
17109         }
17110 #endif
17111         /*
17112          * Get standard flags, and add SYN or FIN if requested by 'hidden'
17113          * state flags.
17114          */
17115         if (tp->t_flags & TF_NEEDFIN)
17116                 flags |= TH_FIN;
17117         if (tp->t_flags & TF_NEEDSYN)
17118                 flags |= TH_SYN;
17119         if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
17120                 void *end_rsm;
17121                 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
17122                 if (end_rsm)
17123                         kern_prefetch(end_rsm, &prefetch_rsm);
17124                 prefetch_rsm = 1;
17125         }
17126         SOCKBUF_LOCK(sb);
17127         /*
17128          * If snd_nxt == snd_max and we have transmitted a FIN, the
17129          * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
17130          * negative length.  This can also occur when TCP opens up its
17131          * congestion window while receiving additional duplicate acks after
17132          * fast-retransmit because TCP will reset snd_nxt to snd_max after
17133          * the fast-retransmit.
17134          *
17135          * In the normal retransmit-FIN-only case, however, snd_nxt will be
17136          * set to snd_una, the sb_offset will be 0, and the length may wind
17137          * up 0.
17138          *
17139          * If sack_rxmit is true we are retransmitting from the scoreboard
17140          * in which case len is already set.
17141          */
17142         if ((sack_rxmit == 0) &&
17143             (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
17144                 uint32_t avail;
17145
17146                 avail = sbavail(sb);
17147                 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
17148                         sb_offset = tp->snd_nxt - tp->snd_una;
17149                 else
17150                         sb_offset = 0;
17151                 if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
17152                         if (rack->r_ctl.rc_tlp_new_data) {
17153                                 /* TLP is forcing out new data */
17154                                 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
17155                                         rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
17156                                 }
17157                                 if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
17158                                         if (tp->snd_wnd > sb_offset)
17159                                                 len = tp->snd_wnd - sb_offset;
17160                                         else
17161                                                 len = 0;
17162                                 } else {
17163                                         len = rack->r_ctl.rc_tlp_new_data;
17164                                 }
17165                         }  else {
17166                                 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
17167                         }
17168                         if ((rack->r_ctl.crte == NULL) && IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) {
17169                                 /*
17170                                  * For prr=off, we need to send only 1 MSS
17171                                  * at a time. We do this because another sack could
17172                                  * be arriving that causes us to send retransmits and
17173                                  * we don't want to be on a long pace due to a larger send
17174                                  * that keeps us from sending out the retransmit.
17175                                  */
17176                                 len = segsiz;
17177                         }
17178                 } else {
17179                         uint32_t outstanding;
17180                         /*
17181                          * We are inside of a Fast recovery episode, this
17182                          * is caused by a SACK or 3 dup acks. At this point
17183                          * we have sent all the retransmissions and we rely
17184                          * on PRR to dictate what we will send in the form of
17185                          * new data.
17186                          */
17187
17188                         outstanding = tp->snd_max - tp->snd_una;
17189                         if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
17190                                 if (tp->snd_wnd > outstanding) {
17191                                         len = tp->snd_wnd - outstanding;
17192                                         /* Check to see if we have the data */
17193                                         if ((sb_offset + len) > avail) {
17194                                                 /* It does not all fit */
17195                                                 if (avail > sb_offset)
17196                                                         len = avail - sb_offset;
17197                                                 else
17198                                                         len = 0;
17199                                         }
17200                                 } else {
17201                                         len = 0;
17202                                 }
17203                         } else if (avail > sb_offset) {
17204                                 len = avail - sb_offset;
17205                         } else {
17206                                 len = 0;
17207                         }
17208                         if (len > 0) {
17209                                 if (len > rack->r_ctl.rc_prr_sndcnt) {
17210                                         len = rack->r_ctl.rc_prr_sndcnt;
17211                                 }
17212                                 if (len > 0) {
17213                                         sub_from_prr = 1;
17214                                         counter_u64_add(rack_rtm_prr_newdata, 1);
17215                                 }
17216                         }
17217                         if (len > segsiz) {
17218                                 /*
17219                                  * We should never send more than a MSS when
17220                                  * retransmitting or sending new data in prr
17221                                  * mode unless the override flag is on. Most
17222                                  * likely the PRR algorithm is not going to
17223                                  * let us send a lot as well :-)
17224                                  */
17225                                 if (rack->r_ctl.rc_prr_sendalot == 0) {
17226                                         len = segsiz;
17227                                 }
17228                         } else if (len < segsiz) {
17229                                 /*
17230                                  * Do we send any? The idea here is if the
17231                                  * send empty's the socket buffer we want to
17232                                  * do it. However if not then lets just wait
17233                                  * for our prr_sndcnt to get bigger.
17234                                  */
17235                                 long leftinsb;
17236
17237                                 leftinsb = sbavail(sb) - sb_offset;
17238                                 if (leftinsb > len) {
17239                                         /* This send does not empty the sb */
17240                                         len = 0;
17241                                 }
17242                         }
17243                 }
17244         } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
17245                 /*
17246                  * If you have not established
17247                  * and are not doing FAST OPEN
17248                  * no data please.
17249                  */
17250                 if ((sack_rxmit == 0) &&
17251                     (!IS_FASTOPEN(tp->t_flags))){
17252                         len = 0;
17253                         sb_offset = 0;
17254                 }
17255         }
17256         if (prefetch_so_done == 0) {
17257                 kern_prefetch(so, &prefetch_so_done);
17258                 prefetch_so_done = 1;
17259         }
17260         /*
17261          * Lop off SYN bit if it has already been sent.  However, if this is
17262          * SYN-SENT state and if segment contains data and if we don't know
17263          * that foreign host supports TAO, suppress sending segment.
17264          */
17265         if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
17266             ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
17267                 /*
17268                  * When sending additional segments following a TFO SYN|ACK,
17269                  * do not include the SYN bit.
17270                  */
17271                 if (IS_FASTOPEN(tp->t_flags) &&
17272                     (tp->t_state == TCPS_SYN_RECEIVED))
17273                         flags &= ~TH_SYN;
17274         }
17275         /*
17276          * Be careful not to send data and/or FIN on SYN segments. This
17277          * measure is needed to prevent interoperability problems with not
17278          * fully conformant TCP implementations.
17279          */
17280         if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
17281                 len = 0;
17282                 flags &= ~TH_FIN;
17283         }
17284         /*
17285          * On TFO sockets, ensure no data is sent in the following cases:
17286          *
17287          *  - When retransmitting SYN|ACK on a passively-created socket
17288          *
17289          *  - When retransmitting SYN on an actively created socket
17290          *
17291          *  - When sending a zero-length cookie (cookie request) on an
17292          *    actively created socket
17293          *
17294          *  - When the socket is in the CLOSED state (RST is being sent)
17295          */
17296         if (IS_FASTOPEN(tp->t_flags) &&
17297             (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
17298              ((tp->t_state == TCPS_SYN_SENT) &&
17299               (tp->t_tfo_client_cookie_len == 0)) ||
17300              (flags & TH_RST))) {
17301                 sack_rxmit = 0;
17302                 len = 0;
17303         }
17304         /* Without fast-open there should never be data sent on a SYN */
17305         if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
17306                 tp->snd_nxt = tp->iss;
17307                 len = 0;
17308         }
17309         if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
17310                 /* We only send 1 MSS if we have a DSACK block */
17311                 add_flag |= RACK_SENT_W_DSACK;
17312                 len = segsiz;
17313         }
17314         orig_len = len;
17315         if (len <= 0) {
17316                 /*
17317                  * If FIN has been sent but not acked, but we haven't been
17318                  * called to retransmit, len will be < 0.  Otherwise, window
17319                  * shrank after we sent into it.  If window shrank to 0,
17320                  * cancel pending retransmit, pull snd_nxt back to (closed)
17321                  * window, and set the persist timer if it isn't already
17322                  * going.  If the window didn't close completely, just wait
17323                  * for an ACK.
17324                  *
17325                  * We also do a general check here to ensure that we will
17326                  * set the persist timer when we have data to send, but a
17327                  * 0-byte window. This makes sure the persist timer is set
17328                  * even if the packet hits one of the "goto send" lines
17329                  * below.
17330                  */
17331                 len = 0;
17332                 if ((tp->snd_wnd == 0) &&
17333                     (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17334                     (tp->snd_una == tp->snd_max) &&
17335                     (sb_offset < (int)sbavail(sb))) {
17336                         rack_enter_persist(tp, rack, cts);
17337                 }
17338         } else if ((rsm == NULL) &&
17339                    (doing_tlp == 0) &&
17340                    (len < pace_max_seg)) {
17341                 /*
17342                  * We are not sending a maximum sized segment for
17343                  * some reason. Should we not send anything (think
17344                  * sws or persists)?
17345                  */
17346                 if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17347                     (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17348                     (len < minseg) &&
17349                     (len < (int)(sbavail(sb) - sb_offset))) {
17350                         /*
17351                          * Here the rwnd is less than
17352                          * the minimum pacing size, this is not a retransmit,
17353                          * we are established and
17354                          * the send is not the last in the socket buffer
17355                          * we send nothing, and we may enter persists
17356                          * if nothing is outstanding.
17357                          */
17358                         len = 0;
17359                         if (tp->snd_max == tp->snd_una) {
17360                                 /*
17361                                  * Nothing out we can
17362                                  * go into persists.
17363                                  */
17364                                 rack_enter_persist(tp, rack, cts);
17365                         }
17366                      } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
17367                            (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17368                            (len < (int)(sbavail(sb) - sb_offset)) &&
17369                            (len < minseg)) {
17370                         /*
17371                          * Here we are not retransmitting, and
17372                          * the cwnd is not so small that we could
17373                          * not send at least a min size (rxt timer
17374                          * not having gone off), We have 2 segments or
17375                          * more already in flight, its not the tail end
17376                          * of the socket buffer  and the cwnd is blocking
17377                          * us from sending out a minimum pacing segment size.
17378                          * Lets not send anything.
17379                          */
17380                         len = 0;
17381                 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
17382                             min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17383                            (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
17384                            (len < (int)(sbavail(sb) - sb_offset)) &&
17385                            (TCPS_HAVEESTABLISHED(tp->t_state))) {
17386                         /*
17387                          * Here we have a send window but we have
17388                          * filled it up and we can't send another pacing segment.
17389                          * We also have in flight more than 2 segments
17390                          * and we are not completing the sb i.e. we allow
17391                          * the last bytes of the sb to go out even if
17392                          * its not a full pacing segment.
17393                          */
17394                         len = 0;
17395                 } else if ((rack->r_ctl.crte != NULL) &&
17396                            (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
17397                            (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
17398                            (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
17399                            (len < (int)(sbavail(sb) - sb_offset))) {
17400                         /*
17401                          * Here we are doing hardware pacing, this is not a TLP,
17402                          * we are not sending a pace max segment size, there is rwnd
17403                          * room to send at least N pace_max_seg, the cwnd is greater
17404                          * than or equal to a full pacing segments plus 4 mss and we have 2 or
17405                          * more segments in flight and its not the tail of the socket buffer.
17406                          *
17407                          * We don't want to send instead we need to get more ack's in to
17408                          * allow us to send a full pacing segment. Normally, if we are pacing
17409                          * about the right speed, we should have finished our pacing
17410                          * send as most of the acks have come back if we are at the
17411                          * right rate. This is a bit fuzzy since return path delay
17412                          * can delay the acks, which is why we want to make sure we
17413                          * have cwnd space to have a bit more than a max pace segments in flight.
17414                          *
17415                          * If we have not gotten our acks back we are pacing at too high a
17416                          * rate delaying will not hurt and will bring our GP estimate down by
17417                          * injecting the delay. If we don't do this we will send
17418                          * 2 MSS out in response to the acks being clocked in which
17419                          * defeats the point of hw-pacing (i.e. to help us get
17420                          * larger TSO's out).
17421                          */
17422                         len = 0;
17423
17424                 }
17425
17426         }
17427         /* len will be >= 0 after this point. */
17428         KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
17429         rack_sndbuf_autoscale(rack);
17430         /*
17431          * Decide if we can use TCP Segmentation Offloading (if supported by
17432          * hardware).
17433          *
17434          * TSO may only be used if we are in a pure bulk sending state.  The
17435          * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
17436          * options prevent using TSO.  With TSO the TCP header is the same
17437          * (except for the sequence number) for all generated packets.  This
17438          * makes it impossible to transmit any options which vary per
17439          * generated segment or packet.
17440          *
17441          * IPv4 handling has a clear separation of ip options and ip header
17442          * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
17443          * the right thing below to provide length of just ip options and thus
17444          * checking for ipoptlen is enough to decide if ip options are present.
17445          */
17446         ipoptlen = 0;
17447 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17448         /*
17449          * Pre-calculate here as we save another lookup into the darknesses
17450          * of IPsec that way and can actually decide if TSO is ok.
17451          */
17452 #ifdef INET6
17453         if (isipv6 && IPSEC_ENABLED(ipv6))
17454                 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
17455 #ifdef INET
17456         else
17457 #endif
17458 #endif                          /* INET6 */
17459 #ifdef INET
17460                 if (IPSEC_ENABLED(ipv4))
17461                         ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
17462 #endif                          /* INET */
17463 #endif
17464
17465 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17466         ipoptlen += ipsec_optlen;
17467 #endif
17468         if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
17469             (tp->t_port == 0) &&
17470             ((tp->t_flags & TF_SIGNATURE) == 0) &&
17471             tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
17472             ipoptlen == 0)
17473                 tso = 1;
17474         {
17475                 uint32_t outstanding;
17476
17477                 outstanding = tp->snd_max - tp->snd_una;
17478                 if (tp->t_flags & TF_SENTFIN) {
17479                         /*
17480                          * If we sent a fin, snd_max is 1 higher than
17481                          * snd_una
17482                          */
17483                         outstanding--;
17484                 }
17485                 if (sack_rxmit) {
17486                         if ((rsm->r_flags & RACK_HAS_FIN) == 0)
17487                                 flags &= ~TH_FIN;
17488                 } else {
17489                         if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
17490                                    sbused(sb)))
17491                                 flags &= ~TH_FIN;
17492                 }
17493         }
17494         recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
17495             (long)TCP_MAXWIN << tp->rcv_scale);
17496
17497         /*
17498          * Sender silly window avoidance.   We transmit under the following
17499          * conditions when len is non-zero:
17500          *
17501          * - We have a full segment (or more with TSO) - This is the last
17502          * buffer in a write()/send() and we are either idle or running
17503          * NODELAY - we've timed out (e.g. persist timer) - we have more
17504          * then 1/2 the maximum send window's worth of data (receiver may be
17505          * limited the window size) - we need to retransmit
17506          */
17507         if (len) {
17508                 if (len >= segsiz) {
17509                         goto send;
17510                 }
17511                 /*
17512                  * NOTE! on localhost connections an 'ack' from the remote
17513                  * end may occur synchronously with the output and cause us
17514                  * to flush a buffer queued with moretocome.  XXX
17515                  *
17516                  */
17517                 if (!(tp->t_flags & TF_MORETOCOME) &&   /* normal case */
17518                     (idle || (tp->t_flags & TF_NODELAY)) &&
17519                     ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17520                     (tp->t_flags & TF_NOPUSH) == 0) {
17521                         pass = 2;
17522                         goto send;
17523                 }
17524                 if ((tp->snd_una == tp->snd_max) && len) {      /* Nothing outstanding */
17525                         pass = 22;
17526                         goto send;
17527                 }
17528                 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
17529                         pass = 4;
17530                         goto send;
17531                 }
17532                 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */
17533                         pass = 5;
17534                         goto send;
17535                 }
17536                 if (sack_rxmit) {
17537                         pass = 6;
17538                         goto send;
17539                 }
17540                 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
17541                     (ctf_outstanding(tp) < (segsiz * 2))) {
17542                         /*
17543                          * We have less than two MSS outstanding (delayed ack)
17544                          * and our rwnd will not let us send a full sized
17545                          * MSS. Lets go ahead and let this small segment
17546                          * out because we want to try to have at least two
17547                          * packets inflight to not be caught by delayed ack.
17548                          */
17549                         pass = 12;
17550                         goto send;
17551                 }
17552         }
17553         /*
17554          * Sending of standalone window updates.
17555          *
17556          * Window updates are important when we close our window due to a
17557          * full socket buffer and are opening it again after the application
17558          * reads data from it.  Once the window has opened again and the
17559          * remote end starts to send again the ACK clock takes over and
17560          * provides the most current window information.
17561          *
17562          * We must avoid the silly window syndrome whereas every read from
17563          * the receive buffer, no matter how small, causes a window update
17564          * to be sent.  We also should avoid sending a flurry of window
17565          * updates when the socket buffer had queued a lot of data and the
17566          * application is doing small reads.
17567          *
17568          * Prevent a flurry of pointless window updates by only sending an
17569          * update when we can increase the advertized window by more than
17570          * 1/4th of the socket buffer capacity.  When the buffer is getting
17571          * full or is very small be more aggressive and send an update
17572          * whenever we can increase by two mss sized segments. In all other
17573          * situations the ACK's to new incoming data will carry further
17574          * window increases.
17575          *
17576          * Don't send an independent window update if a delayed ACK is
17577          * pending (it will get piggy-backed on it) or the remote side
17578          * already has done a half-close and won't send more data.  Skip
17579          * this if the connection is in T/TCP half-open state.
17580          */
17581         if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
17582             !(tp->t_flags & TF_DELACK) &&
17583             !TCPS_HAVERCVDFIN(tp->t_state)) {
17584                 /*
17585                  * "adv" is the amount we could increase the window, taking
17586                  * into account that we are limited by TCP_MAXWIN <<
17587                  * tp->rcv_scale.
17588                  */
17589                 int32_t adv;
17590                 int oldwin;
17591
17592                 adv = recwin;
17593                 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
17594                         oldwin = (tp->rcv_adv - tp->rcv_nxt);
17595                         if (adv > oldwin)
17596                             adv -= oldwin;
17597                         else {
17598                                 /* We can't increase the window */
17599                                 adv = 0;
17600                         }
17601                 } else
17602                         oldwin = 0;
17603
17604                 /*
17605                  * If the new window size ends up being the same as or less
17606                  * than the old size when it is scaled, then don't force
17607                  * a window update.
17608                  */
17609                 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
17610                         goto dontupdate;
17611
17612                 if (adv >= (int32_t)(2 * segsiz) &&
17613                     (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
17614                      recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
17615                      so->so_rcv.sb_hiwat <= 8 * segsiz)) {
17616                         pass = 7;
17617                         goto send;
17618                 }
17619                 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
17620                         pass = 23;
17621                         goto send;
17622                 }
17623         }
17624 dontupdate:
17625
17626         /*
17627          * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
17628          * is also a catch-all for the retransmit timer timeout case.
17629          */
17630         if (tp->t_flags & TF_ACKNOW) {
17631                 pass = 8;
17632                 goto send;
17633         }
17634         if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
17635                 pass = 9;
17636                 goto send;
17637         }
17638         /*
17639          * If our state indicates that FIN should be sent and we have not
17640          * yet done so, then we need to send.
17641          */
17642         if ((flags & TH_FIN) &&
17643             (tp->snd_nxt == tp->snd_una)) {
17644                 pass = 11;
17645                 goto send;
17646         }
17647         /*
17648          * No reason to send a segment, just return.
17649          */
17650 just_return:
17651         SOCKBUF_UNLOCK(sb);
17652 just_return_nolock:
17653         {
17654                 int app_limited = CTF_JR_SENT_DATA;
17655
17656                 if (tot_len_this_send > 0) {
17657                         /* Make sure snd_nxt is up to max */
17658                         rack->r_ctl.fsb.recwin = recwin;
17659                         slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
17660                         if ((error == 0) &&
17661                             rack_use_rfo &&
17662                             ((flags & (TH_SYN|TH_FIN)) == 0) &&
17663                             (ipoptlen == 0) &&
17664                             (tp->snd_nxt == tp->snd_max) &&
17665                             (tp->rcv_numsacks == 0) &&
17666                             rack->r_fsb_inited &&
17667                             TCPS_HAVEESTABLISHED(tp->t_state) &&
17668                             (rack->r_must_retran == 0) &&
17669                             ((tp->t_flags & TF_NEEDFIN) == 0) &&
17670                             (len > 0) && (orig_len > 0) &&
17671                             (orig_len > len) &&
17672                             ((orig_len - len) >= segsiz) &&
17673                             ((optlen == 0) ||
17674                              ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
17675                                 /* We can send at least one more MSS using our fsb */
17676
17677                                 rack->r_fast_output = 1;
17678                                 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
17679                                 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
17680                                 rack->r_ctl.fsb.tcp_flags = flags;
17681                                 rack->r_ctl.fsb.left_to_send = orig_len - len;
17682                                 if (hw_tls)
17683                                         rack->r_ctl.fsb.hw_tls = 1;
17684                                 else
17685                                         rack->r_ctl.fsb.hw_tls = 0;
17686                                 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
17687                                         ("rack:%p left_to_send:%u sbavail:%u out:%u",
17688                                         rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
17689                                          (tp->snd_max - tp->snd_una)));
17690                                 if (rack->r_ctl.fsb.left_to_send < segsiz)
17691                                         rack->r_fast_output = 0;
17692                                 else {
17693                                         if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
17694                                                 rack->r_ctl.fsb.rfo_apply_push = 1;
17695                                         else
17696                                                 rack->r_ctl.fsb.rfo_apply_push = 0;
17697                                 }
17698                         } else
17699                                 rack->r_fast_output = 0;
17700
17701
17702                         rack_log_fsb(rack, tp, so, flags,
17703                                      ipoptlen, orig_len, len, 0,
17704                                      1, optlen, __LINE__, 1);
17705                         if (SEQ_GT(tp->snd_max, tp->snd_nxt))
17706                                 tp->snd_nxt = tp->snd_max;
17707                 } else {
17708                         int end_window = 0;
17709                         uint32_t seq = tp->gput_ack;
17710
17711                         rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17712                         if (rsm) {
17713                                 /*
17714                                  * Mark the last sent that we just-returned (hinting
17715                                  * that delayed ack may play a role in any rtt measurement).
17716                                  */
17717                                 rsm->r_just_ret = 1;
17718                         }
17719                         counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
17720                         rack->r_ctl.rc_agg_delayed = 0;
17721                         rack->r_early = 0;
17722                         rack->r_late = 0;
17723                         rack->r_ctl.rc_agg_early = 0;
17724                         if ((ctf_outstanding(tp) +
17725                              min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
17726                                  minseg)) >= tp->snd_wnd) {
17727                                 /* We are limited by the rwnd */
17728                                 app_limited = CTF_JR_RWND_LIMITED;
17729                                 if (IN_FASTRECOVERY(tp->t_flags))
17730                                     rack->r_ctl.rc_prr_sndcnt = 0;
17731                         } else if (ctf_outstanding(tp) >= sbavail(sb)) {
17732                                 /* We are limited by whats available -- app limited */
17733                                 app_limited = CTF_JR_APP_LIMITED;
17734                                 if (IN_FASTRECOVERY(tp->t_flags))
17735                                     rack->r_ctl.rc_prr_sndcnt = 0;
17736                         } else if ((idle == 0) &&
17737                                    ((tp->t_flags & TF_NODELAY) == 0) &&
17738                                    ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
17739                                    (len < segsiz)) {
17740                                 /*
17741                                  * No delay is not on and the
17742                                  * user is sending less than 1MSS. This
17743                                  * brings out SWS avoidance so we
17744                                  * don't send. Another app-limited case.
17745                                  */
17746                                 app_limited = CTF_JR_APP_LIMITED;
17747                         } else if (tp->t_flags & TF_NOPUSH) {
17748                                 /*
17749                                  * The user has requested no push of
17750                                  * the last segment and we are
17751                                  * at the last segment. Another app
17752                                  * limited case.
17753                                  */
17754                                 app_limited = CTF_JR_APP_LIMITED;
17755                         } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
17756                                 /* Its the cwnd */
17757                                 app_limited = CTF_JR_CWND_LIMITED;
17758                         } else if (IN_FASTRECOVERY(tp->t_flags) &&
17759                                    (rack->rack_no_prr == 0) &&
17760                                    (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
17761                                 app_limited = CTF_JR_PRR;
17762                         } else {
17763                                 /* Now why here are we not sending? */
17764 #ifdef NOW
17765 #ifdef INVARIANTS
17766                                 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
17767 #endif
17768 #endif
17769                                 app_limited = CTF_JR_ASSESSING;
17770                         }
17771                         /*
17772                          * App limited in some fashion, for our pacing GP
17773                          * measurements we don't want any gap (even cwnd).
17774                          * Close  down the measurement window.
17775                          */
17776                         if (rack_cwnd_block_ends_measure &&
17777                             ((app_limited == CTF_JR_CWND_LIMITED) ||
17778                              (app_limited == CTF_JR_PRR))) {
17779                                 /*
17780                                  * The reason we are not sending is
17781                                  * the cwnd (or prr). We have been configured
17782                                  * to end the measurement window in
17783                                  * this case.
17784                                  */
17785                                 end_window = 1;
17786                         } else if (rack_rwnd_block_ends_measure &&
17787                                    (app_limited == CTF_JR_RWND_LIMITED)) {
17788                                 /*
17789                                  * We are rwnd limited and have been
17790                                  * configured to end the measurement
17791                                  * window in this case.
17792                                  */
17793                                 end_window = 1;
17794                         } else if (app_limited == CTF_JR_APP_LIMITED) {
17795                                 /*
17796                                  * A true application limited period, we have
17797                                  * ran out of data.
17798                                  */
17799                                 end_window = 1;
17800                         } else if (app_limited == CTF_JR_ASSESSING) {
17801                                 /*
17802                                  * In the assessing case we hit the end of
17803                                  * the if/else and had no known reason
17804                                  * This will panic us under invariants..
17805                                  *
17806                                  * If we get this out in logs we need to
17807                                  * investagate which reason we missed.
17808                                  */
17809                                 end_window = 1;
17810                         }
17811                         if (end_window) {
17812                                 uint8_t log = 0;
17813
17814                                 /* Adjust the Gput measurement */
17815                                 if ((tp->t_flags & TF_GPUTINPROG) &&
17816                                     SEQ_GT(tp->gput_ack, tp->snd_max)) {
17817                                         tp->gput_ack = tp->snd_max;
17818                                         if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
17819                                                 /*
17820                                                  * There is not enough to measure.
17821                                                  */
17822                                                 tp->t_flags &= ~TF_GPUTINPROG;
17823                                                 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
17824                                                                            rack->r_ctl.rc_gp_srtt /*flex1*/,
17825                                                                            tp->gput_seq,
17826                                                                            0, 0, 18, __LINE__, NULL, 0);
17827                                         } else
17828                                                 log = 1;
17829                                 }
17830                                 /* Mark the last packet has app limited */
17831                                 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17832                                 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
17833                                         if (rack->r_ctl.rc_app_limited_cnt == 0)
17834                                                 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
17835                                         else {
17836                                                 /*
17837                                                  * Go out to the end app limited and mark
17838                                                  * this new one as next and move the end_appl up
17839                                                  * to this guy.
17840                                                  */
17841                                                 if (rack->r_ctl.rc_end_appl)
17842                                                         rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
17843                                                 rack->r_ctl.rc_end_appl = rsm;
17844                                         }
17845                                         rsm->r_flags |= RACK_APP_LIMITED;
17846                                         rack->r_ctl.rc_app_limited_cnt++;
17847                                 }
17848                                 if (log)
17849                                         rack_log_pacing_delay_calc(rack,
17850                                                                    rack->r_ctl.rc_app_limited_cnt, seq,
17851                                                                    tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
17852                         }
17853                 }
17854                 if (slot) {
17855                         /* set the rack tcb into the slot N */
17856                         counter_u64_add(rack_paced_segments, 1);
17857                 } else if (tot_len_this_send) {
17858                         counter_u64_add(rack_unpaced_segments, 1);
17859                 }
17860                 /* Check if we need to go into persists or not */
17861                 if ((tp->snd_max == tp->snd_una) &&
17862                     TCPS_HAVEESTABLISHED(tp->t_state) &&
17863                     sbavail(sb) &&
17864                     (sbavail(sb) > tp->snd_wnd) &&
17865                     (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
17866                         /* Yes lets make sure to move to persist before timer-start */
17867                         rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
17868                 }
17869                 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
17870                 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
17871         }
17872 #ifdef NETFLIX_SHARED_CWND
17873         if ((sbavail(sb) == 0) &&
17874             rack->r_ctl.rc_scw) {
17875                 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
17876                 rack->rack_scwnd_is_idle = 1;
17877         }
17878 #endif
17879 #ifdef TCP_ACCOUNTING
17880         if (tot_len_this_send > 0) {
17881                 crtsc = get_cyclecount();
17882                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17883                         tp->tcp_cnt_counters[SND_OUT_DATA]++;
17884                 }
17885                 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
17886                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17887                         tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
17888                 }
17889                 counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val));
17890                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17891                         tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
17892                 }
17893                 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) / segsiz));
17894         } else {
17895                 crtsc = get_cyclecount();
17896                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17897                         tp->tcp_cnt_counters[SND_LIMITED]++;
17898                 }
17899                 counter_u64_add(tcp_cnt_counters[SND_LIMITED], 1);
17900                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17901                         tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
17902                 }
17903                 counter_u64_add(tcp_proc_time[SND_LIMITED], (crtsc - ts_val));
17904         }
17905         sched_unpin();
17906 #endif
17907         return (0);
17908
17909 send:
17910         if (rsm || sack_rxmit)
17911                 counter_u64_add(rack_nfto_resend, 1);
17912         else
17913                 counter_u64_add(rack_non_fto_send, 1);
17914         if ((flags & TH_FIN) &&
17915             sbavail(sb)) {
17916                 /*
17917                  * We do not transmit a FIN
17918                  * with data outstanding. We
17919                  * need to make it so all data
17920                  * is acked first.
17921                  */
17922                 flags &= ~TH_FIN;
17923         }
17924         /* Enforce stack imposed max seg size if we have one */
17925         if (rack->r_ctl.rc_pace_max_segs &&
17926             (len > rack->r_ctl.rc_pace_max_segs)) {
17927                 mark = 1;
17928                 len = rack->r_ctl.rc_pace_max_segs;
17929         }
17930         SOCKBUF_LOCK_ASSERT(sb);
17931         if (len > 0) {
17932                 if (len >= segsiz)
17933                         tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
17934                 else
17935                         tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
17936         }
17937         /*
17938          * Before ESTABLISHED, force sending of initial options unless TCP
17939          * set not to do any options. NOTE: we assume that the IP/TCP header
17940          * plus TCP options always fit in a single mbuf, leaving room for a
17941          * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
17942          * + optlen <= MCLBYTES
17943          */
17944         optlen = 0;
17945 #ifdef INET6
17946         if (isipv6)
17947                 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
17948         else
17949 #endif
17950                 hdrlen = sizeof(struct tcpiphdr);
17951
17952         /*
17953          * Compute options for segment. We only have to care about SYN and
17954          * established connection segments.  Options for SYN-ACK segments
17955          * are handled in TCP syncache.
17956          */
17957         to.to_flags = 0;
17958         if ((tp->t_flags & TF_NOOPT) == 0) {
17959                 /* Maximum segment size. */
17960                 if (flags & TH_SYN) {
17961                         tp->snd_nxt = tp->iss;
17962                         to.to_mss = tcp_mssopt(&inp->inp_inc);
17963                         if (tp->t_port)
17964                                 to.to_mss -= V_tcp_udp_tunneling_overhead;
17965                         to.to_flags |= TOF_MSS;
17966
17967                         /*
17968                          * On SYN or SYN|ACK transmits on TFO connections,
17969                          * only include the TFO option if it is not a
17970                          * retransmit, as the presence of the TFO option may
17971                          * have caused the original SYN or SYN|ACK to have
17972                          * been dropped by a middlebox.
17973                          */
17974                         if (IS_FASTOPEN(tp->t_flags) &&
17975                             (tp->t_rxtshift == 0)) {
17976                                 if (tp->t_state == TCPS_SYN_RECEIVED) {
17977                                         to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
17978                                         to.to_tfo_cookie =
17979                                                 (u_int8_t *)&tp->t_tfo_cookie.server;
17980                                         to.to_flags |= TOF_FASTOPEN;
17981                                         wanted_cookie = 1;
17982                                 } else if (tp->t_state == TCPS_SYN_SENT) {
17983                                         to.to_tfo_len =
17984                                                 tp->t_tfo_client_cookie_len;
17985                                         to.to_tfo_cookie =
17986                                                 tp->t_tfo_cookie.client;
17987                                         to.to_flags |= TOF_FASTOPEN;
17988                                         wanted_cookie = 1;
17989                                         /*
17990                                          * If we wind up having more data to
17991                                          * send with the SYN than can fit in
17992                                          * one segment, don't send any more
17993                                          * until the SYN|ACK comes back from
17994                                          * the other end.
17995                                          */
17996                                         sendalot = 0;
17997                                 }
17998                         }
17999                 }
18000                 /* Window scaling. */
18001                 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
18002                         to.to_wscale = tp->request_r_scale;
18003                         to.to_flags |= TOF_SCALE;
18004                 }
18005                 /* Timestamps. */
18006                 if ((tp->t_flags & TF_RCVD_TSTMP) ||
18007                     ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
18008                         to.to_tsval = ms_cts + tp->ts_offset;
18009                         to.to_tsecr = tp->ts_recent;
18010                         to.to_flags |= TOF_TS;
18011                 }
18012                 /* Set receive buffer autosizing timestamp. */
18013                 if (tp->rfbuf_ts == 0 &&
18014                     (so->so_rcv.sb_flags & SB_AUTOSIZE))
18015                         tp->rfbuf_ts = tcp_ts_getticks();
18016                 /* Selective ACK's. */
18017                 if (tp->t_flags & TF_SACK_PERMIT) {
18018                         if (flags & TH_SYN)
18019                                 to.to_flags |= TOF_SACKPERM;
18020                         else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18021                                  tp->rcv_numsacks > 0) {
18022                                 to.to_flags |= TOF_SACK;
18023                                 to.to_nsacks = tp->rcv_numsacks;
18024                                 to.to_sacks = (u_char *)tp->sackblks;
18025                         }
18026                 }
18027 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18028                 /* TCP-MD5 (RFC2385). */
18029                 if (tp->t_flags & TF_SIGNATURE)
18030                         to.to_flags |= TOF_SIGNATURE;
18031 #endif                          /* TCP_SIGNATURE */
18032
18033                 /* Processing the options. */
18034                 hdrlen += optlen = tcp_addoptions(&to, opt);
18035                 /*
18036                  * If we wanted a TFO option to be added, but it was unable
18037                  * to fit, ensure no data is sent.
18038                  */
18039                 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
18040                     !(to.to_flags & TOF_FASTOPEN))
18041                         len = 0;
18042         }
18043         if (tp->t_port) {
18044                 if (V_tcp_udp_tunneling_port == 0) {
18045                         /* The port was removed?? */
18046                         SOCKBUF_UNLOCK(&so->so_snd);
18047 #ifdef TCP_ACCOUNTING
18048                         crtsc = get_cyclecount();
18049                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18050                                 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
18051                         }
18052                         counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
18053                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18054                                 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
18055                         }
18056                         counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18057                         sched_unpin();
18058 #endif
18059                         return (EHOSTUNREACH);
18060                 }
18061                 hdrlen += sizeof(struct udphdr);
18062         }
18063 #ifdef INET6
18064         if (isipv6)
18065                 ipoptlen = ip6_optlen(tp->t_inpcb);
18066         else
18067 #endif
18068                 if (tp->t_inpcb->inp_options)
18069                         ipoptlen = tp->t_inpcb->inp_options->m_len -
18070                                 offsetof(struct ipoption, ipopt_list);
18071                 else
18072                         ipoptlen = 0;
18073 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18074         ipoptlen += ipsec_optlen;
18075 #endif
18076
18077         /*
18078          * Adjust data length if insertion of options will bump the packet
18079          * length beyond the t_maxseg length. Clear the FIN bit because we
18080          * cut off the tail of the segment.
18081          */
18082         if (len + optlen + ipoptlen > tp->t_maxseg) {
18083                 if (tso) {
18084                         uint32_t if_hw_tsomax;
18085                         uint32_t moff;
18086                         int32_t max_len;
18087
18088                         /* extract TSO information */
18089                         if_hw_tsomax = tp->t_tsomax;
18090                         if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18091                         if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18092                         KASSERT(ipoptlen == 0,
18093                                 ("%s: TSO can't do IP options", __func__));
18094
18095                         /*
18096                          * Check if we should limit by maximum payload
18097                          * length:
18098                          */
18099                         if (if_hw_tsomax != 0) {
18100                                 /* compute maximum TSO length */
18101                                 max_len = (if_hw_tsomax - hdrlen -
18102                                            max_linkhdr);
18103                                 if (max_len <= 0) {
18104                                         len = 0;
18105                                 } else if (len > max_len) {
18106                                         sendalot = 1;
18107                                         len = max_len;
18108                                         mark = 2;
18109                                 }
18110                         }
18111                         /*
18112                          * Prevent the last segment from being fractional
18113                          * unless the send sockbuf can be emptied:
18114                          */
18115                         max_len = (tp->t_maxseg - optlen);
18116                         if ((sb_offset + len) < sbavail(sb)) {
18117                                 moff = len % (u_int)max_len;
18118                                 if (moff != 0) {
18119                                         mark = 3;
18120                                         len -= moff;
18121                                 }
18122                         }
18123                         /*
18124                          * In case there are too many small fragments don't
18125                          * use TSO:
18126                          */
18127                         if (len <= segsiz) {
18128                                 mark = 4;
18129                                 tso = 0;
18130                         }
18131                         /*
18132                          * Send the FIN in a separate segment after the bulk
18133                          * sending is done. We don't trust the TSO
18134                          * implementations to clear the FIN flag on all but
18135                          * the last segment.
18136                          */
18137                         if (tp->t_flags & TF_NEEDFIN) {
18138                                 sendalot = 4;
18139                         }
18140                 } else {
18141                         mark = 5;
18142                         if (optlen + ipoptlen >= tp->t_maxseg) {
18143                                 /*
18144                                  * Since we don't have enough space to put
18145                                  * the IP header chain and the TCP header in
18146                                  * one packet as required by RFC 7112, don't
18147                                  * send it. Also ensure that at least one
18148                                  * byte of the payload can be put into the
18149                                  * TCP segment.
18150                                  */
18151                                 SOCKBUF_UNLOCK(&so->so_snd);
18152                                 error = EMSGSIZE;
18153                                 sack_rxmit = 0;
18154                                 goto out;
18155                         }
18156                         len = tp->t_maxseg - optlen - ipoptlen;
18157                         sendalot = 5;
18158                 }
18159         } else {
18160                 tso = 0;
18161                 mark = 6;
18162         }
18163         KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
18164                 ("%s: len > IP_MAXPACKET", __func__));
18165 #ifdef DIAGNOSTIC
18166 #ifdef INET6
18167         if (max_linkhdr + hdrlen > MCLBYTES)
18168 #else
18169                 if (max_linkhdr + hdrlen > MHLEN)
18170 #endif
18171                         panic("tcphdr too big");
18172 #endif
18173
18174         /*
18175          * This KASSERT is here to catch edge cases at a well defined place.
18176          * Before, those had triggered (random) panic conditions further
18177          * down.
18178          */
18179         KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
18180         if ((len == 0) &&
18181             (flags & TH_FIN) &&
18182             (sbused(sb))) {
18183                 /*
18184                  * We have outstanding data, don't send a fin by itself!.
18185                  */
18186                 goto just_return;
18187         }
18188         /*
18189          * Grab a header mbuf, attaching a copy of data to be transmitted,
18190          * and initialize the header from the template for sends on this
18191          * connection.
18192          */
18193         hw_tls = (sb->sb_flags & SB_TLS_IFNET) != 0;
18194         if (len) {
18195                 uint32_t max_val;
18196                 uint32_t moff;
18197
18198                 if (rack->r_ctl.rc_pace_max_segs)
18199                         max_val = rack->r_ctl.rc_pace_max_segs;
18200                 else if (rack->rc_user_set_max_segs)
18201                         max_val = rack->rc_user_set_max_segs * segsiz;
18202                 else
18203                         max_val = len;
18204                 /*
18205                  * We allow a limit on sending with hptsi.
18206                  */
18207                 if (len > max_val) {
18208                         mark = 7;
18209                         len = max_val;
18210                 }
18211 #ifdef INET6
18212                 if (MHLEN < hdrlen + max_linkhdr)
18213                         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18214                 else
18215 #endif
18216                         m = m_gethdr(M_NOWAIT, MT_DATA);
18217
18218                 if (m == NULL) {
18219                         SOCKBUF_UNLOCK(sb);
18220                         error = ENOBUFS;
18221                         sack_rxmit = 0;
18222                         goto out;
18223                 }
18224                 m->m_data += max_linkhdr;
18225                 m->m_len = hdrlen;
18226
18227                 /*
18228                  * Start the m_copy functions from the closest mbuf to the
18229                  * sb_offset in the socket buffer chain.
18230                  */
18231                 mb = sbsndptr_noadv(sb, sb_offset, &moff);
18232                 s_mb = mb;
18233                 s_moff = moff;
18234                 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
18235                         m_copydata(mb, moff, (int)len,
18236                                    mtod(m, caddr_t)+hdrlen);
18237                         if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18238                                 sbsndptr_adv(sb, mb, len);
18239                         m->m_len += len;
18240                 } else {
18241                         struct sockbuf *msb;
18242
18243                         if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18244                                 msb = NULL;
18245                         else
18246                                 msb = sb;
18247                         m->m_next = tcp_m_copym(
18248                                 mb, moff, &len,
18249                                 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
18250                                 ((rsm == NULL) ? hw_tls : 0)
18251 #ifdef NETFLIX_COPY_ARGS
18252                                 , &filled_all
18253 #endif
18254                                 );
18255                         if (len <= (tp->t_maxseg - optlen)) {
18256                                 /*
18257                                  * Must have ran out of mbufs for the copy
18258                                  * shorten it to no longer need tso. Lets
18259                                  * not put on sendalot since we are low on
18260                                  * mbufs.
18261                                  */
18262                                 tso = 0;
18263                         }
18264                         if (m->m_next == NULL) {
18265                                 SOCKBUF_UNLOCK(sb);
18266                                 (void)m_free(m);
18267                                 error = ENOBUFS;
18268                                 sack_rxmit = 0;
18269                                 goto out;
18270                         }
18271                 }
18272                 if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
18273                         if (rsm && (rsm->r_flags & RACK_TLP)) {
18274                                 /*
18275                                  * TLP should not count in retran count, but
18276                                  * in its own bin
18277                                  */
18278                                 counter_u64_add(rack_tlp_retran, 1);
18279                                 counter_u64_add(rack_tlp_retran_bytes, len);
18280                         } else {
18281                                 tp->t_sndrexmitpack++;
18282                                 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18283                                 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18284                         }
18285 #ifdef STATS
18286                         stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18287                                                  len);
18288 #endif
18289                 } else {
18290                         KMOD_TCPSTAT_INC(tcps_sndpack);
18291                         KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
18292 #ifdef STATS
18293                         stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
18294                                                  len);
18295 #endif
18296                 }
18297                 /*
18298                  * If we're sending everything we've got, set PUSH. (This
18299                  * will keep happy those implementations which only give
18300                  * data to the user when a buffer fills or a PUSH comes in.)
18301                  */
18302                 if (sb_offset + len == sbused(sb) &&
18303                     sbused(sb) &&
18304                     !(flags & TH_SYN)) {
18305                         flags |= TH_PUSH;
18306                         add_flag |= RACK_HAD_PUSH;
18307                 }
18308
18309                 SOCKBUF_UNLOCK(sb);
18310         } else {
18311                 SOCKBUF_UNLOCK(sb);
18312                 if (tp->t_flags & TF_ACKNOW)
18313                         KMOD_TCPSTAT_INC(tcps_sndacks);
18314                 else if (flags & (TH_SYN | TH_FIN | TH_RST))
18315                         KMOD_TCPSTAT_INC(tcps_sndctrl);
18316                 else
18317                         KMOD_TCPSTAT_INC(tcps_sndwinup);
18318
18319                 m = m_gethdr(M_NOWAIT, MT_DATA);
18320                 if (m == NULL) {
18321                         error = ENOBUFS;
18322                         sack_rxmit = 0;
18323                         goto out;
18324                 }
18325 #ifdef INET6
18326                 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
18327                     MHLEN >= hdrlen) {
18328                         M_ALIGN(m, hdrlen);
18329                 } else
18330 #endif
18331                         m->m_data += max_linkhdr;
18332                 m->m_len = hdrlen;
18333         }
18334         SOCKBUF_UNLOCK_ASSERT(sb);
18335         m->m_pkthdr.rcvif = (struct ifnet *)0;
18336 #ifdef MAC
18337         mac_inpcb_create_mbuf(inp, m);
18338 #endif
18339         if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) &&  rack->r_fsb_inited) {
18340 #ifdef INET6
18341                 if (isipv6)
18342                         ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18343                 else
18344 #endif                          /* INET6 */
18345                         ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18346                 th = rack->r_ctl.fsb.th;
18347                 udp = rack->r_ctl.fsb.udp;
18348                 if (udp) {
18349 #ifdef INET6
18350                         if (isipv6)
18351                                 ulen = hdrlen + len - sizeof(struct ip6_hdr);
18352                         else
18353 #endif                          /* INET6 */
18354                                 ulen = hdrlen + len - sizeof(struct ip);
18355                         udp->uh_ulen = htons(ulen);
18356                 }
18357         } else {
18358 #ifdef INET6
18359                 if (isipv6) {
18360                         ip6 = mtod(m, struct ip6_hdr *);
18361                         if (tp->t_port) {
18362                                 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
18363                                 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18364                                 udp->uh_dport = tp->t_port;
18365                                 ulen = hdrlen + len - sizeof(struct ip6_hdr);
18366                                 udp->uh_ulen = htons(ulen);
18367                                 th = (struct tcphdr *)(udp + 1);
18368                         } else
18369                                 th = (struct tcphdr *)(ip6 + 1);
18370                         tcpip_fillheaders(inp, tp->t_port, ip6, th);
18371                 } else
18372 #endif                          /* INET6 */
18373                 {
18374                         ip = mtod(m, struct ip *);
18375 #ifdef TCPDEBUG
18376                         ipov = (struct ipovly *)ip;
18377 #endif
18378                         if (tp->t_port) {
18379                                 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
18380                                 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
18381                                 udp->uh_dport = tp->t_port;
18382                                 ulen = hdrlen + len - sizeof(struct ip);
18383                                 udp->uh_ulen = htons(ulen);
18384                                 th = (struct tcphdr *)(udp + 1);
18385                         } else
18386                                 th = (struct tcphdr *)(ip + 1);
18387                         tcpip_fillheaders(inp, tp->t_port, ip, th);
18388                 }
18389         }
18390         /*
18391          * Fill in fields, remembering maximum advertised window for use in
18392          * delaying messages about window sizes. If resending a FIN, be sure
18393          * not to use a new sequence number.
18394          */
18395         if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
18396             tp->snd_nxt == tp->snd_max)
18397                 tp->snd_nxt--;
18398         /*
18399          * If we are starting a connection, send ECN setup SYN packet. If we
18400          * are on a retransmit, we may resend those bits a number of times
18401          * as per RFC 3168.
18402          */
18403         if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) {
18404                 if (tp->t_rxtshift >= 1) {
18405                         if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
18406                                 flags |= TH_ECE | TH_CWR;
18407                 } else
18408                         flags |= TH_ECE | TH_CWR;
18409         }
18410         /* Handle parallel SYN for ECN */
18411         if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18412             (tp->t_flags2 & TF2_ECN_SND_ECE)) {
18413                 flags |= TH_ECE;
18414                 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18415         }
18416         if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18417             (tp->t_flags2 & TF2_ECN_PERMIT)) {
18418                 /*
18419                  * If the peer has ECN, mark data packets with ECN capable
18420                  * transmission (ECT). Ignore pure ack packets,
18421                  * retransmissions.
18422                  */
18423                 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
18424                     (sack_rxmit == 0)) {
18425 #ifdef INET6
18426                         if (isipv6)
18427                                 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
18428                         else
18429 #endif
18430                                 ip->ip_tos |= IPTOS_ECN_ECT0;
18431                         KMOD_TCPSTAT_INC(tcps_ecn_ect0);
18432                         /*
18433                          * Reply with proper ECN notifications.
18434                          * Only set CWR on new data segments.
18435                          */
18436                         if (tp->t_flags2 & TF2_ECN_SND_CWR) {
18437                                 flags |= TH_CWR;
18438                                 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
18439                         }
18440                 }
18441                 if (tp->t_flags2 & TF2_ECN_SND_ECE)
18442                         flags |= TH_ECE;
18443         }
18444         /*
18445          * If we are doing retransmissions, then snd_nxt will not reflect
18446          * the first unsent octet.  For ACK only packets, we do not want the
18447          * sequence number of the retransmitted packet, we want the sequence
18448          * number of the next unsent octet.  So, if there is no data (and no
18449          * SYN or FIN), use snd_max instead of snd_nxt when filling in
18450          * ti_seq.  But if we are in persist state, snd_max might reflect
18451          * one byte beyond the right edge of the window, so use snd_nxt in
18452          * that case, since we know we aren't doing a retransmission.
18453          * (retransmit and persist are mutually exclusive...)
18454          */
18455         if (sack_rxmit == 0) {
18456                 if (len || (flags & (TH_SYN | TH_FIN))) {
18457                         th->th_seq = htonl(tp->snd_nxt);
18458                         rack_seq = tp->snd_nxt;
18459                 } else {
18460                         th->th_seq = htonl(tp->snd_max);
18461                         rack_seq = tp->snd_max;
18462                 }
18463         } else {
18464                 th->th_seq = htonl(rsm->r_start);
18465                 rack_seq = rsm->r_start;
18466         }
18467         th->th_ack = htonl(tp->rcv_nxt);
18468         th->th_flags = flags;
18469         /*
18470          * Calculate receive window.  Don't shrink window, but avoid silly
18471          * window syndrome.
18472          * If a RST segment is sent, advertise a window of zero.
18473          */
18474         if (flags & TH_RST) {
18475                 recwin = 0;
18476         } else {
18477                 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
18478                     recwin < (long)segsiz) {
18479                         recwin = 0;
18480                 }
18481                 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
18482                     recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
18483                         recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
18484         }
18485
18486         /*
18487          * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
18488          * <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK> case is
18489          * handled in syncache.
18490          */
18491         if (flags & TH_SYN)
18492                 th->th_win = htons((u_short)
18493                                    (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
18494         else {
18495                 /* Avoid shrinking window with window scaling. */
18496                 recwin = roundup2(recwin, 1 << tp->rcv_scale);
18497                 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
18498         }
18499         /*
18500          * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
18501          * window.  This may cause the remote transmitter to stall.  This
18502          * flag tells soreceive() to disable delayed acknowledgements when
18503          * draining the buffer.  This can occur if the receiver is
18504          * attempting to read more data than can be buffered prior to
18505          * transmitting on the connection.
18506          */
18507         if (th->th_win == 0) {
18508                 tp->t_sndzerowin++;
18509                 tp->t_flags |= TF_RXWIN0SENT;
18510         } else
18511                 tp->t_flags &= ~TF_RXWIN0SENT;
18512         tp->snd_up = tp->snd_una;       /* drag it along, its deprecated */
18513         /* Now are we using fsb?, if so copy the template data to the mbuf */
18514         if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
18515                 uint8_t *cpto;
18516
18517                 cpto = mtod(m, uint8_t *);
18518                 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18519                 /*
18520                  * We have just copied in:
18521                  * IP/IP6
18522                  * <optional udphdr>
18523                  * tcphdr (no options)
18524                  *
18525                  * We need to grab the correct pointers into the mbuf
18526                  * for both the tcp header, and possibly the udp header (if tunneling).
18527                  * We do this by using the offset in the copy buffer and adding it
18528                  * to the mbuf base pointer (cpto).
18529                  */
18530 #ifdef INET6
18531                 if (isipv6)
18532                         ip6 = mtod(m, struct ip6_hdr *);
18533                 else
18534 #endif                          /* INET6 */
18535                         ip = mtod(m, struct ip *);
18536                 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18537                 /* If we have a udp header lets set it into the mbuf as well */
18538                 if (udp)
18539                         udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
18540         }
18541 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18542         if (to.to_flags & TOF_SIGNATURE) {
18543                 /*
18544                  * Calculate MD5 signature and put it into the place
18545                  * determined before.
18546                  * NOTE: since TCP options buffer doesn't point into
18547                  * mbuf's data, calculate offset and use it.
18548                  */
18549                 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18550                                                        (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18551                         /*
18552                          * Do not send segment if the calculation of MD5
18553                          * digest has failed.
18554                          */
18555                         goto out;
18556                 }
18557         }
18558 #endif
18559         if (optlen) {
18560                 bcopy(opt, th + 1, optlen);
18561                 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18562         }
18563         /*
18564          * Put TCP length in extended header, and then checksum extended
18565          * header and data.
18566          */
18567         m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
18568 #ifdef INET6
18569         if (isipv6) {
18570                 /*
18571                  * ip6_plen is not need to be filled now, and will be filled
18572                  * in ip6_output.
18573                  */
18574                 if (tp->t_port) {
18575                         m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18576                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18577                         udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18578                         th->th_sum = htons(0);
18579                         UDPSTAT_INC(udps_opackets);
18580                 } else {
18581                         m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18582                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18583                         th->th_sum = in6_cksum_pseudo(ip6,
18584                                                       sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18585                                                       0);
18586                 }
18587         }
18588 #endif
18589 #if defined(INET6) && defined(INET)
18590         else
18591 #endif
18592 #ifdef INET
18593         {
18594                 if (tp->t_port) {
18595                         m->m_pkthdr.csum_flags = CSUM_UDP;
18596                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18597                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18598                                                 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18599                         th->th_sum = htons(0);
18600                         UDPSTAT_INC(udps_opackets);
18601                 } else {
18602                         m->m_pkthdr.csum_flags = CSUM_TCP;
18603                         m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18604                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
18605                                                ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18606                                                                         IPPROTO_TCP + len + optlen));
18607                 }
18608                 /* IP version must be set here for ipv4/ipv6 checking later */
18609                 KASSERT(ip->ip_v == IPVERSION,
18610                         ("%s: IP version incorrect: %d", __func__, ip->ip_v));
18611         }
18612 #endif
18613         /*
18614          * Enable TSO and specify the size of the segments. The TCP pseudo
18615          * header checksum is always provided. XXX: Fixme: This is currently
18616          * not the case for IPv6.
18617          */
18618         if (tso) {
18619                 KASSERT(len > tp->t_maxseg - optlen,
18620                         ("%s: len <= tso_segsz", __func__));
18621                 m->m_pkthdr.csum_flags |= CSUM_TSO;
18622                 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
18623         }
18624         KASSERT(len + hdrlen == m_length(m, NULL),
18625                 ("%s: mbuf chain different than expected: %d + %u != %u",
18626                  __func__, len, hdrlen, m_length(m, NULL)));
18627
18628 #ifdef TCP_HHOOK
18629         /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
18630         hhook_run_tcp_est_out(tp, th, &to, len, tso);
18631 #endif
18632         /* We're getting ready to send; log now. */
18633         if (tp->t_logstate != TCP_LOG_STATE_OFF) {
18634                 union tcp_log_stackspecific log;
18635
18636                 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18637                 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
18638                 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
18639                 if (rack->rack_no_prr)
18640                         log.u_bbr.flex1 = 0;
18641                 else
18642                         log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18643                 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18644                 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18645                 log.u_bbr.flex4 = orig_len;
18646                 if (filled_all)
18647                         log.u_bbr.flex5 = 0x80000000;
18648                 else
18649                         log.u_bbr.flex5 = 0;
18650                 /* Save off the early/late values */
18651                 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18652                 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18653                 log.u_bbr.bw_inuse = rack_get_bw(rack);
18654                 if (rsm || sack_rxmit) {
18655                         if (doing_tlp)
18656                                 log.u_bbr.flex8 = 2;
18657                         else
18658                                 log.u_bbr.flex8 = 1;
18659                 } else {
18660                         if (doing_tlp)
18661                                 log.u_bbr.flex8 = 3;
18662                         else
18663                                 log.u_bbr.flex8 = 0;
18664                 }
18665                 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
18666                 log.u_bbr.flex7 = mark;
18667                 log.u_bbr.flex7 <<= 8;
18668                 log.u_bbr.flex7 |= pass;
18669                 log.u_bbr.pkts_out = tp->t_maxseg;
18670                 log.u_bbr.timeStamp = cts;
18671                 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18672                 log.u_bbr.lt_epoch = cwnd_to_use;
18673                 log.u_bbr.delivered = sendalot;
18674                 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
18675                                      len, &log, false, NULL, NULL, 0, &tv);
18676         } else
18677                 lgb = NULL;
18678
18679         /*
18680          * Fill in IP length and desired time to live and send to IP level.
18681          * There should be a better way to handle ttl and tos; we could keep
18682          * them in the template, but need a way to checksum without them.
18683          */
18684         /*
18685          * m->m_pkthdr.len should have been set before cksum calcuration,
18686          * because in6_cksum() need it.
18687          */
18688 #ifdef INET6
18689         if (isipv6) {
18690                 /*
18691                  * we separately set hoplimit for every segment, since the
18692                  * user might want to change the value via setsockopt. Also,
18693                  * desired default hop limit might be changed via Neighbor
18694                  * Discovery.
18695                  */
18696                 rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
18697
18698                 /*
18699                  * Set the packet size here for the benefit of DTrace
18700                  * probes. ip6_output() will set it properly; it's supposed
18701                  * to include the option header lengths as well.
18702                  */
18703                 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18704
18705                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18706                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18707                 else
18708                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18709
18710                 if (tp->t_state == TCPS_SYN_SENT)
18711                         TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
18712
18713                 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
18714                 /* TODO: IPv6 IP6TOS_ECT bit on */
18715                 error = ip6_output(m,
18716 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18717                                    inp->in6p_outputopts,
18718 #else
18719                                    NULL,
18720 #endif
18721                                    &inp->inp_route6,
18722                                    ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
18723                                    NULL, NULL, inp);
18724
18725                 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
18726                         mtu = inp->inp_route6.ro_nh->nh_mtu;
18727         }
18728 #endif                          /* INET6 */
18729 #if defined(INET) && defined(INET6)
18730         else
18731 #endif
18732 #ifdef INET
18733         {
18734                 ip->ip_len = htons(m->m_pkthdr.len);
18735 #ifdef INET6
18736                 if (inp->inp_vflag & INP_IPV6PROTO)
18737                         ip->ip_ttl = in6_selecthlim(inp, NULL);
18738 #endif                          /* INET6 */
18739                 rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
18740                 /*
18741                  * If we do path MTU discovery, then we set DF on every
18742                  * packet. This might not be the best thing to do according
18743                  * to RFC3390 Section 2. However the tcp hostcache migitates
18744                  * the problem so it affects only the first tcp connection
18745                  * with a host.
18746                  *
18747                  * NB: Don't set DF on small MTU/MSS to have a safe
18748                  * fallback.
18749                  */
18750                 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18751                         tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18752                         if (tp->t_port == 0 || len < V_tcp_minmss) {
18753                                 ip->ip_off |= htons(IP_DF);
18754                         }
18755                 } else {
18756                         tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18757                 }
18758
18759                 if (tp->t_state == TCPS_SYN_SENT)
18760                         TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
18761
18762                 TCP_PROBE5(send, NULL, tp, ip, tp, th);
18763
18764                 error = ip_output(m,
18765 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18766                                   inp->inp_options,
18767 #else
18768                                   NULL,
18769 #endif
18770                                   &inp->inp_route,
18771                                   ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
18772                                   inp);
18773                 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
18774                         mtu = inp->inp_route.ro_nh->nh_mtu;
18775         }
18776 #endif                          /* INET */
18777
18778 out:
18779         if (lgb) {
18780                 lgb->tlb_errno = error;
18781                 lgb = NULL;
18782         }
18783         /*
18784          * In transmit state, time the transmission and arrange for the
18785          * retransmit.  In persist state, just set snd_max.
18786          */
18787         if (error == 0) {
18788                 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
18789                 if (rsm && doing_tlp) {
18790                         rack->rc_last_sent_tlp_past_cumack = 0;
18791                         rack->rc_last_sent_tlp_seq_valid = 1;
18792                         rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18793                         rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18794                 }
18795                 rack->forced_ack = 0;   /* If we send something zap the FA flag */
18796                 if (rsm && (doing_tlp == 0)) {
18797                         /* Set we retransmitted */
18798                         rack->rc_gp_saw_rec = 1;
18799                 } else {
18800                         if (cwnd_to_use > tp->snd_ssthresh) {
18801                                 /* Set we sent in CA */
18802                                 rack->rc_gp_saw_ca = 1;
18803                         } else {
18804                                 /* Set we sent in SS */
18805                                 rack->rc_gp_saw_ss = 1;
18806                         }
18807                 }
18808                 if (doing_tlp && (rsm == NULL)) {
18809                         /* Make sure new data TLP cnt is clear */
18810                         rack->r_ctl.rc_tlp_new_data = 0;
18811                 }
18812                 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18813                     (tp->t_flags & TF_SACK_PERMIT) &&
18814                     tp->rcv_numsacks > 0)
18815                         tcp_clean_dsack_blocks(tp);
18816                 tot_len_this_send += len;
18817                 if (len == 0)
18818                         counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
18819                 else if (len == 1) {
18820                         counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
18821                 } else if (len > 1) {
18822                         int idx;
18823
18824                         idx = (len / segsiz) + 3;
18825                         if (idx >= TCP_MSS_ACCT_ATIMER)
18826                                 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18827                         else
18828                                 counter_u64_add(rack_out_size[idx], 1);
18829                 }
18830         }
18831         if ((rack->rack_no_prr == 0) &&
18832             sub_from_prr &&
18833             (error == 0)) {
18834                 if (rack->r_ctl.rc_prr_sndcnt >= len)
18835                         rack->r_ctl.rc_prr_sndcnt -= len;
18836                 else
18837                         rack->r_ctl.rc_prr_sndcnt = 0;
18838         }
18839         sub_from_prr = 0;
18840         if (doing_tlp) {
18841                 /* Make sure the TLP is added */
18842                 add_flag |= RACK_TLP;
18843         } else if (rsm) {
18844                 /* If its a resend without TLP then it must not have the flag */
18845                 rsm->r_flags &= ~RACK_TLP;
18846         }
18847         rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
18848                         rack_to_usec_ts(&tv),
18849                         rsm, add_flag, s_mb, s_moff, hw_tls);
18850
18851
18852         if ((error == 0) &&
18853             (len > 0) &&
18854             (tp->snd_una == tp->snd_max))
18855                 rack->r_ctl.rc_tlp_rxt_last_time = cts;
18856         {
18857                 tcp_seq startseq = tp->snd_nxt;
18858
18859                 /* Track our lost count */
18860                 if (rsm && (doing_tlp == 0))
18861                         rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
18862                 /*
18863                  * Advance snd_nxt over sequence space of this segment.
18864                  */
18865                 if (error)
18866                         /* We don't log or do anything with errors */
18867                         goto nomore;
18868                 if (doing_tlp == 0) {
18869                         if (rsm == NULL) {
18870                                 /*
18871                                  * Not a retransmission of some
18872                                  * sort, new data is going out so
18873                                  * clear our TLP count and flag.
18874                                  */
18875                                 rack->rc_tlp_in_progress = 0;
18876                                 rack->r_ctl.rc_tlp_cnt_out = 0;
18877                         }
18878                 } else {
18879                         /*
18880                          * We have just sent a TLP, mark that it is true
18881                          * and make sure our in progress is set so we
18882                          * continue to check the count.
18883                          */
18884                         rack->rc_tlp_in_progress = 1;
18885                         rack->r_ctl.rc_tlp_cnt_out++;
18886                 }
18887                 if (flags & (TH_SYN | TH_FIN)) {
18888                         if (flags & TH_SYN)
18889                                 tp->snd_nxt++;
18890                         if (flags & TH_FIN) {
18891                                 tp->snd_nxt++;
18892                                 tp->t_flags |= TF_SENTFIN;
18893                         }
18894                 }
18895                 /* In the ENOBUFS case we do *not* update snd_max */
18896                 if (sack_rxmit)
18897                         goto nomore;
18898
18899                 tp->snd_nxt += len;
18900                 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
18901                         if (tp->snd_una == tp->snd_max) {
18902                                 /*
18903                                  * Update the time we just added data since
18904                                  * none was outstanding.
18905                                  */
18906                                 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
18907                                 tp->t_acktime = ticks;
18908                         }
18909                         tp->snd_max = tp->snd_nxt;
18910                         /*
18911                          * Time this transmission if not a retransmission and
18912                          * not currently timing anything.
18913                          * This is only relevant in case of switching back to
18914                          * the base stack.
18915                          */
18916                         if (tp->t_rtttime == 0) {
18917                                 tp->t_rtttime = ticks;
18918                                 tp->t_rtseq = startseq;
18919                                 KMOD_TCPSTAT_INC(tcps_segstimed);
18920                         }
18921                         if (len &&
18922                             ((tp->t_flags & TF_GPUTINPROG) == 0))
18923                                 rack_start_gp_measurement(tp, rack, startseq, sb_offset);
18924                 }
18925                 /*
18926                  * If we are doing FO we need to update the mbuf position and subtract
18927                  * this happens when the peer sends us duplicate information and
18928                  * we thus want to send a DSACK.
18929                  *
18930                  * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
18931                  * turned off? If not then we are going to echo multiple DSACK blocks
18932                  * out (with the TSO), which we should not be doing.
18933                  */
18934                 if (rack->r_fast_output && len) {
18935                         if (rack->r_ctl.fsb.left_to_send > len)
18936                                 rack->r_ctl.fsb.left_to_send -= len;
18937                         else
18938                                 rack->r_ctl.fsb.left_to_send = 0;
18939                         if (rack->r_ctl.fsb.left_to_send < segsiz)
18940                                 rack->r_fast_output = 0;
18941                         if (rack->r_fast_output) {
18942                                 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
18943                                 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
18944                         }
18945                 }
18946         }
18947 nomore:
18948         if (error) {
18949                 rack->r_ctl.rc_agg_delayed = 0;
18950                 rack->r_early = 0;
18951                 rack->r_late = 0;
18952                 rack->r_ctl.rc_agg_early = 0;
18953                 SOCKBUF_UNLOCK_ASSERT(sb);      /* Check gotos. */
18954                 /*
18955                  * Failures do not advance the seq counter above. For the
18956                  * case of ENOBUFS we will fall out and retry in 1ms with
18957                  * the hpts. Everything else will just have to retransmit
18958                  * with the timer.
18959                  *
18960                  * In any case, we do not want to loop around for another
18961                  * send without a good reason.
18962                  */
18963                 sendalot = 0;
18964                 switch (error) {
18965                 case EPERM:
18966                         tp->t_softerror = error;
18967 #ifdef TCP_ACCOUNTING
18968                         crtsc = get_cyclecount();
18969                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18970                                 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
18971                         }
18972                         counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
18973                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18974                                 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
18975                         }
18976                         counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18977                         sched_unpin();
18978 #endif
18979                         return (error);
18980                 case ENOBUFS:
18981                         /*
18982                          * Pace us right away to retry in a some
18983                          * time
18984                          */
18985                         slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
18986                         if (rack->rc_enobuf < 0x7f)
18987                                 rack->rc_enobuf++;
18988                         if (slot < (10 * HPTS_USEC_IN_MSEC))
18989                                 slot = 10 * HPTS_USEC_IN_MSEC;
18990                         if (rack->r_ctl.crte != NULL) {
18991                                 counter_u64_add(rack_saw_enobuf_hw, 1);
18992                                 tcp_rl_log_enobuf(rack->r_ctl.crte);
18993                         }
18994                         counter_u64_add(rack_saw_enobuf, 1);
18995                         goto enobufs;
18996                 case EMSGSIZE:
18997                         /*
18998                          * For some reason the interface we used initially
18999                          * to send segments changed to another or lowered
19000                          * its MTU. If TSO was active we either got an
19001                          * interface without TSO capabilits or TSO was
19002                          * turned off. If we obtained mtu from ip_output()
19003                          * then update it and try again.
19004                          */
19005                         if (tso)
19006                                 tp->t_flags &= ~TF_TSO;
19007                         if (mtu != 0) {
19008                                 tcp_mss_update(tp, -1, mtu, NULL, NULL);
19009                                 goto again;
19010                         }
19011                         slot = 10 * HPTS_USEC_IN_MSEC;
19012                         rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19013 #ifdef TCP_ACCOUNTING
19014                         crtsc = get_cyclecount();
19015                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19016                                 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19017                         }
19018                         counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19019                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19020                                 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19021                         }
19022                         counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19023                         sched_unpin();
19024 #endif
19025                         return (error);
19026                 case ENETUNREACH:
19027                         counter_u64_add(rack_saw_enetunreach, 1);
19028                 case EHOSTDOWN:
19029                 case EHOSTUNREACH:
19030                 case ENETDOWN:
19031                         if (TCPS_HAVERCVDSYN(tp->t_state)) {
19032                                 tp->t_softerror = error;
19033                         }
19034                         /* FALLTHROUGH */
19035                 default:
19036                         slot = 10 * HPTS_USEC_IN_MSEC;
19037                         rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
19038 #ifdef TCP_ACCOUNTING
19039                         crtsc = get_cyclecount();
19040                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19041                                 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
19042                         }
19043                         counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1);
19044                         if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19045                                 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
19046                         }
19047                         counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19048                         sched_unpin();
19049 #endif
19050                         return (error);
19051                 }
19052         } else {
19053                 rack->rc_enobuf = 0;
19054                 if (IN_FASTRECOVERY(tp->t_flags) && rsm)
19055                         rack->r_ctl.retran_during_recovery += len;
19056         }
19057         KMOD_TCPSTAT_INC(tcps_sndtotal);
19058
19059         /*
19060          * Data sent (as far as we can tell). If this advertises a larger
19061          * window than any other segment, then remember the size of the
19062          * advertised window. Any pending ACK has now been sent.
19063          */
19064         if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
19065                 tp->rcv_adv = tp->rcv_nxt + recwin;
19066
19067         tp->last_ack_sent = tp->rcv_nxt;
19068         tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19069 enobufs:
19070         if (sendalot) {
19071                 /* Do we need to turn off sendalot? */
19072                 if (rack->r_ctl.rc_pace_max_segs &&
19073                     (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
19074                         /* We hit our max. */
19075                         sendalot = 0;
19076                 } else if ((rack->rc_user_set_max_segs) &&
19077                            (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
19078                         /* We hit the user defined max */
19079                         sendalot = 0;
19080                 }
19081         }
19082         if ((error == 0) && (flags & TH_FIN))
19083                 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
19084         if (flags & TH_RST) {
19085                 /*
19086                  * We don't send again after sending a RST.
19087                  */
19088                 slot = 0;
19089                 sendalot = 0;
19090                 if (error == 0)
19091                         tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
19092         } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
19093                 /*
19094                  * Get our pacing rate, if an error
19095                  * occurred in sending (ENOBUF) we would
19096                  * hit the else if with slot preset. Other
19097                  * errors return.
19098                  */
19099                 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
19100         }
19101         if (rsm &&
19102             (rsm->r_flags & RACK_HAS_SYN) == 0 &&
19103             rack->use_rack_rr) {
19104                 /* Its a retransmit and we use the rack cheat? */
19105                 if ((slot == 0) ||
19106                     (rack->rc_always_pace == 0) ||
19107                     (rack->r_rr_config == 1)) {
19108                         /*
19109                          * We have no pacing set or we
19110                          * are using old-style rack or
19111                          * we are overriden to use the old 1ms pacing.
19112                          */
19113                         slot = rack->r_ctl.rc_min_to;
19114                 }
19115         }
19116         /* We have sent clear the flag */
19117         rack->r_ent_rec_ns = 0;
19118         if (rack->r_must_retran) {
19119                 if (rsm) {
19120                         rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
19121                         if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
19122                                 /*
19123                                  * We have retransmitted all.
19124                                  */
19125                                 rack->r_must_retran = 0;
19126                                 rack->r_ctl.rc_out_at_rto = 0;
19127                         }
19128                 } else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19129                         /*
19130                          * Sending new data will also kill
19131                          * the loop.
19132                          */
19133                         rack->r_must_retran = 0;
19134                         rack->r_ctl.rc_out_at_rto = 0;
19135                 }
19136         }
19137         rack->r_ctl.fsb.recwin = recwin;
19138         if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
19139             SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19140                 /*
19141                  * We hit an RTO and now have past snd_max at the RTO
19142                  * clear all the WAS flags.
19143                  */
19144                 tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
19145         }
19146         if (slot) {
19147                 /* set the rack tcb into the slot N */
19148                 counter_u64_add(rack_paced_segments, 1);
19149                 if ((error == 0) &&
19150                     rack_use_rfo &&
19151                     ((flags & (TH_SYN|TH_FIN)) == 0) &&
19152                     (rsm == NULL) &&
19153                     (tp->snd_nxt == tp->snd_max) &&
19154                     (ipoptlen == 0) &&
19155                     (tp->rcv_numsacks == 0) &&
19156                     rack->r_fsb_inited &&
19157                     TCPS_HAVEESTABLISHED(tp->t_state) &&
19158                     (rack->r_must_retran == 0) &&
19159                     ((tp->t_flags & TF_NEEDFIN) == 0) &&
19160                     (len > 0) && (orig_len > 0) &&
19161                     (orig_len > len) &&
19162                     ((orig_len - len) >= segsiz) &&
19163                     ((optlen == 0) ||
19164                      ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19165                         /* We can send at least one more MSS using our fsb */
19166
19167                         rack->r_fast_output = 1;
19168                         rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19169                         rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19170                         rack->r_ctl.fsb.tcp_flags = flags;
19171                         rack->r_ctl.fsb.left_to_send = orig_len - len;
19172                         if (hw_tls)
19173                                 rack->r_ctl.fsb.hw_tls = 1;
19174                         else
19175                                 rack->r_ctl.fsb.hw_tls = 0;
19176                         KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19177                                 ("rack:%p left_to_send:%u sbavail:%u out:%u",
19178                                  rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19179                                  (tp->snd_max - tp->snd_una)));
19180                         if (rack->r_ctl.fsb.left_to_send < segsiz)
19181                                 rack->r_fast_output = 0;
19182                         else {
19183                                 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19184                                         rack->r_ctl.fsb.rfo_apply_push = 1;
19185                                 else
19186                                         rack->r_ctl.fsb.rfo_apply_push = 0;
19187                         }
19188                 } else
19189                         rack->r_fast_output = 0;
19190                 rack_log_fsb(rack, tp, so, flags,
19191                              ipoptlen, orig_len, len, error,
19192                              (rsm == NULL), optlen, __LINE__, 2);
19193         } else if (sendalot) {
19194                 int ret;
19195
19196                 if (len)
19197                         counter_u64_add(rack_unpaced_segments, 1);
19198                 sack_rxmit = 0;
19199                 if ((error == 0) &&
19200                     rack_use_rfo &&
19201                     ((flags & (TH_SYN|TH_FIN)) == 0) &&
19202                     (rsm == NULL) &&
19203                     (ipoptlen == 0) &&
19204                     (tp->rcv_numsacks == 0) &&
19205                     (tp->snd_nxt == tp->snd_max) &&
19206                     (rack->r_must_retran == 0) &&
19207                     rack->r_fsb_inited &&
19208                     TCPS_HAVEESTABLISHED(tp->t_state) &&
19209                     ((tp->t_flags & TF_NEEDFIN) == 0) &&
19210                     (len > 0) && (orig_len > 0) &&
19211                     (orig_len > len) &&
19212                     ((orig_len - len) >= segsiz) &&
19213                     ((optlen == 0) ||
19214                      ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19215                         /* we can use fast_output for more */
19216
19217                         rack->r_fast_output = 1;
19218                         rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19219                         rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19220                         rack->r_ctl.fsb.tcp_flags = flags;
19221                         rack->r_ctl.fsb.left_to_send = orig_len - len;
19222                         if (hw_tls)
19223                                 rack->r_ctl.fsb.hw_tls = 1;
19224                         else
19225                                 rack->r_ctl.fsb.hw_tls = 0;
19226                         KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19227                                 ("rack:%p left_to_send:%u sbavail:%u out:%u",
19228                                  rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19229                                  (tp->snd_max - tp->snd_una)));
19230                         if (rack->r_ctl.fsb.left_to_send < segsiz) {
19231                                 rack->r_fast_output = 0;
19232                         }
19233                         if (rack->r_fast_output) {
19234                                 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19235                                         rack->r_ctl.fsb.rfo_apply_push = 1;
19236                                 else
19237                                         rack->r_ctl.fsb.rfo_apply_push = 0;
19238                                 rack_log_fsb(rack, tp, so, flags,
19239                                              ipoptlen, orig_len, len, error,
19240                                              (rsm == NULL), optlen, __LINE__, 3);
19241                                 error = 0;
19242                                 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19243                                 if (ret >= 0)
19244                                         return (ret);
19245                                 else if (error)
19246                                         goto nomore;
19247
19248                         }
19249                 }
19250                 goto again;
19251         } else if (len) {
19252                 counter_u64_add(rack_unpaced_segments, 1);
19253         }
19254         /* Assure when we leave that snd_nxt will point to top */
19255         if (SEQ_GT(tp->snd_max, tp->snd_nxt))
19256                 tp->snd_nxt = tp->snd_max;
19257         rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
19258 #ifdef TCP_ACCOUNTING
19259         crtsc = get_cyclecount() - ts_val;
19260         if (tot_len_this_send) {
19261                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19262                         tp->tcp_cnt_counters[SND_OUT_DATA]++;
19263                 }
19264                 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1);
19265                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19266                         tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
19267                 }
19268                 counter_u64_add(tcp_proc_time[SND_OUT_DATA], crtsc);
19269                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19270                         tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
19271                 }
19272                 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) /segsiz));
19273         } else {
19274                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19275                         tp->tcp_cnt_counters[SND_OUT_ACK]++;
19276                 }
19277                 counter_u64_add(tcp_cnt_counters[SND_OUT_ACK], 1);
19278                 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19279                         tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
19280                 }
19281                 counter_u64_add(tcp_proc_time[SND_OUT_ACK], crtsc);
19282         }
19283         sched_unpin();
19284 #endif
19285         if (error == ENOBUFS)
19286                 error = 0;
19287         return (error);
19288 }
19289
19290 static void
19291 rack_update_seg(struct tcp_rack *rack)
19292 {
19293         uint32_t orig_val;
19294
19295         orig_val = rack->r_ctl.rc_pace_max_segs;
19296         rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
19297         if (orig_val != rack->r_ctl.rc_pace_max_segs)
19298                 rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
19299 }
19300
19301 static void
19302 rack_mtu_change(struct tcpcb *tp)
19303 {
19304         /*
19305          * The MSS may have changed
19306          */
19307         struct tcp_rack *rack;
19308
19309         rack = (struct tcp_rack *)tp->t_fb_ptr;
19310         if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
19311                 /*
19312                  * The MTU has changed we need to resend everything
19313                  * since all we have sent is lost. We first fix
19314                  * up the mtu though.
19315                  */
19316                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
19317                 /* We treat this like a full retransmit timeout without the cwnd adjustment */
19318                 rack_remxt_tmr(tp);
19319                 rack->r_fast_output = 0;
19320                 rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
19321                                                 rack->r_ctl.rc_sacked);
19322                 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
19323                 rack->r_must_retran = 1;
19324
19325         }
19326         sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
19327         /* We don't use snd_nxt to retransmit */
19328         tp->snd_nxt = tp->snd_max;
19329 }
19330
19331 static int
19332 rack_set_profile(struct tcp_rack *rack, int prof)
19333 {
19334         int err = EINVAL;
19335         if (prof == 1) {
19336                 /* pace_always=1 */
19337                 if (rack->rc_always_pace == 0) {
19338                         if (tcp_can_enable_pacing() == 0)
19339                                 return (EBUSY);
19340                 }
19341                 rack->rc_always_pace = 1;
19342                 if (rack->use_fixed_rate || rack->gp_ready)
19343                         rack_set_cc_pacing(rack);
19344                 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19345                 rack->rack_attempt_hdwr_pace = 0;
19346                 /* cmpack=1 */
19347                 if (rack_use_cmp_acks)
19348                         rack->r_use_cmp_ack = 1;
19349                 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19350                     rack->r_use_cmp_ack)
19351                         rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19352                 /* scwnd=1 */
19353                 rack->rack_enable_scwnd = 1;
19354                 /* dynamic=100 */
19355                 rack->rc_gp_dyn_mul = 1;
19356                 /* gp_inc_ca */
19357                 rack->r_ctl.rack_per_of_gp_ca = 100;
19358                 /* rrr_conf=3 */
19359                 rack->r_rr_config = 3;
19360                 /* npush=2 */
19361                 rack->r_ctl.rc_no_push_at_mrtt = 2;
19362                 /* fillcw=1 */
19363                 rack->rc_pace_to_cwnd = 1;
19364                 rack->rc_pace_fill_if_rttin_range = 0;
19365                 rack->rtt_limit_mul = 0;
19366                 /* noprr=1 */
19367                 rack->rack_no_prr = 1;
19368                 /* lscwnd=1 */
19369                 rack->r_limit_scw = 1;
19370                 /* gp_inc_rec */
19371                 rack->r_ctl.rack_per_of_gp_rec = 90;
19372                 err = 0;
19373
19374         } else if (prof == 3) {
19375                 /* Same as profile one execept fill_cw becomes 2 (less aggressive set) */
19376                 /* pace_always=1 */
19377                 if (rack->rc_always_pace == 0) {
19378                         if (tcp_can_enable_pacing() == 0)
19379                                 return (EBUSY);
19380                 }
19381                 rack->rc_always_pace = 1;
19382                 if (rack->use_fixed_rate || rack->gp_ready)
19383                         rack_set_cc_pacing(rack);
19384                 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19385                 rack->rack_attempt_hdwr_pace = 0;
19386                 /* cmpack=1 */
19387                 if (rack_use_cmp_acks)
19388                         rack->r_use_cmp_ack = 1;
19389                 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
19390                     rack->r_use_cmp_ack)
19391                         rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19392                 /* scwnd=1 */
19393                 rack->rack_enable_scwnd = 1;
19394                 /* dynamic=100 */
19395                 rack->rc_gp_dyn_mul = 1;
19396                 /* gp_inc_ca */
19397                 rack->r_ctl.rack_per_of_gp_ca = 100;
19398                 /* rrr_conf=3 */
19399                 rack->r_rr_config = 3;
19400                 /* npush=2 */
19401                 rack->r_ctl.rc_no_push_at_mrtt = 2;
19402                 /* fillcw=2 */
19403                 rack->rc_pace_to_cwnd = 1;
19404                 rack->r_fill_less_agg = 1;
19405                 rack->rc_pace_fill_if_rttin_range = 0;
19406                 rack->rtt_limit_mul = 0;
19407                 /* noprr=1 */
19408                 rack->rack_no_prr = 1;
19409                 /* lscwnd=1 */
19410                 rack->r_limit_scw = 1;
19411                 /* gp_inc_rec */
19412                 rack->r_ctl.rack_per_of_gp_rec = 90;
19413                 err = 0;
19414
19415
19416         } else if (prof == 2) {
19417                 /* cmpack=1 */
19418                 if (rack->rc_always_pace == 0) {
19419                         if (tcp_can_enable_pacing() == 0)
19420                                 return (EBUSY);
19421                 }
19422                 rack->rc_always_pace = 1;
19423                 if (rack->use_fixed_rate || rack->gp_ready)
19424                         rack_set_cc_pacing(rack);
19425                 rack->r_use_cmp_ack = 1;
19426                 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19427                         rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19428                 /* pace_always=1 */
19429                 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19430                 /* scwnd=1 */
19431                 rack->rack_enable_scwnd = 1;
19432                 /* dynamic=100 */
19433                 rack->rc_gp_dyn_mul = 1;
19434                 rack->r_ctl.rack_per_of_gp_ca = 100;
19435                 /* rrr_conf=3 */
19436                 rack->r_rr_config = 3;
19437                 /* npush=2 */
19438                 rack->r_ctl.rc_no_push_at_mrtt = 2;
19439                 /* fillcw=1 */
19440                 rack->rc_pace_to_cwnd = 1;
19441                 rack->rc_pace_fill_if_rttin_range = 0;
19442                 rack->rtt_limit_mul = 0;
19443                 /* noprr=1 */
19444                 rack->rack_no_prr = 1;
19445                 /* lscwnd=0 */
19446                 rack->r_limit_scw = 0;
19447                 err = 0;
19448         } else if (prof == 0) {
19449                 /* This changes things back to the default settings */
19450                 err = 0;
19451                 if (rack->rc_always_pace) {
19452                         tcp_decrement_paced_conn();
19453                         rack_undo_cc_pacing(rack);
19454                         rack->rc_always_pace = 0;
19455                 }
19456                 if (rack_pace_every_seg && tcp_can_enable_pacing()) {
19457                         rack->rc_always_pace = 1;
19458                         if (rack->use_fixed_rate || rack->gp_ready)
19459                                 rack_set_cc_pacing(rack);
19460                 } else
19461                         rack->rc_always_pace = 0;
19462                 if (rack_dsack_std_based & 0x1) {
19463                         /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
19464                         rack->rc_rack_tmr_std_based = 1;
19465                 }
19466                 if (rack_dsack_std_based & 0x2) {
19467                         /* Basically this means  rack timers are extended based on dsack by up to (2 * srtt) */
19468                         rack->rc_rack_use_dsack = 1;
19469                 }
19470                 if (rack_use_cmp_acks)
19471                         rack->r_use_cmp_ack = 1;
19472                 else
19473                         rack->r_use_cmp_ack = 0;
19474                 if (rack_disable_prr)
19475                         rack->rack_no_prr = 1;
19476                 else
19477                         rack->rack_no_prr = 0;
19478                 if (rack_gp_no_rec_chg)
19479                         rack->rc_gp_no_rec_chg = 1;
19480                 else
19481                         rack->rc_gp_no_rec_chg = 0;
19482                 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
19483                         rack->r_mbuf_queue = 1;
19484                         if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
19485                                 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19486                         rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19487                 } else {
19488                         rack->r_mbuf_queue = 0;
19489                         rack->rc_inp->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19490                 }
19491                 if (rack_enable_shared_cwnd)
19492                         rack->rack_enable_scwnd = 1;
19493                 else
19494                         rack->rack_enable_scwnd = 0;
19495                 if (rack_do_dyn_mul) {
19496                         /* When dynamic adjustment is on CA needs to start at 100% */
19497                         rack->rc_gp_dyn_mul = 1;
19498                         if (rack_do_dyn_mul >= 100)
19499                                 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
19500                 } else {
19501                         rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
19502                         rack->rc_gp_dyn_mul = 0;
19503                 }
19504                 rack->r_rr_config = 0;
19505                 rack->r_ctl.rc_no_push_at_mrtt = 0;
19506                 rack->rc_pace_to_cwnd = 0;
19507                 rack->rc_pace_fill_if_rttin_range = 0;
19508                 rack->rtt_limit_mul = 0;
19509
19510                 if (rack_enable_hw_pacing)
19511                         rack->rack_hdw_pace_ena = 1;
19512                 else
19513                         rack->rack_hdw_pace_ena = 0;
19514                 if (rack_disable_prr)
19515                         rack->rack_no_prr = 1;
19516                 else
19517                         rack->rack_no_prr = 0;
19518                 if (rack_limits_scwnd)
19519                         rack->r_limit_scw  = 1;
19520                 else
19521                         rack->r_limit_scw  = 0;
19522                 err = 0;
19523         }
19524         return (err);
19525 }
19526
19527 static int
19528 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
19529 {
19530         struct deferred_opt_list *dol;
19531
19532         dol = malloc(sizeof(struct deferred_opt_list),
19533                      M_TCPFSB, M_NOWAIT|M_ZERO);
19534         if (dol == NULL) {
19535                 /*
19536                  * No space yikes -- fail out..
19537                  */
19538                 return (0);
19539         }
19540         dol->optname = sopt_name;
19541         dol->optval = loptval;
19542         TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
19543         return (1);
19544 }
19545
19546 static int
19547 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
19548                     uint32_t optval, uint64_t loptval)
19549 {
19550         struct epoch_tracker et;
19551         struct sockopt sopt;
19552         struct cc_newreno_opts opt;
19553         uint64_t val;
19554         int error = 0;
19555         uint16_t ca, ss;
19556
19557         switch (sopt_name) {
19558
19559         case TCP_RACK_DSACK_OPT:
19560                 RACK_OPTS_INC(tcp_rack_dsack_opt);
19561                 if (optval & 0x1) {
19562                         rack->rc_rack_tmr_std_based = 1;
19563                 } else {
19564                         rack->rc_rack_tmr_std_based = 0;
19565                 }
19566                 if (optval & 0x2) {
19567                         rack->rc_rack_use_dsack = 1;
19568                 } else {
19569                         rack->rc_rack_use_dsack = 0;
19570                 }
19571                 rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
19572                 break;
19573         case TCP_RACK_PACING_BETA:
19574                 RACK_OPTS_INC(tcp_rack_beta);
19575                 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19576                         /* This only works for newreno. */
19577                         error = EINVAL;
19578                         break;
19579                 }
19580                 if (rack->rc_pacing_cc_set) {
19581                         /*
19582                          * Set them into the real CC module
19583                          * whats in the rack pcb is the old values
19584                          * to be used on restoral/
19585                          */
19586                         sopt.sopt_dir = SOPT_SET;
19587                         opt.name = CC_NEWRENO_BETA;
19588                         opt.val = optval;
19589                         if (CC_ALGO(tp)->ctl_output != NULL)
19590                                 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19591                         else {
19592                                 error = ENOENT;
19593                                 break;
19594                         }
19595                 } else {
19596                         /*
19597                          * Not pacing yet so set it into our local
19598                          * rack pcb storage.
19599                          */
19600                         rack->r_ctl.rc_saved_beta.beta = optval;
19601                 }
19602                 break;
19603         case TCP_RACK_TIMER_SLOP:
19604                 RACK_OPTS_INC(tcp_rack_timer_slop);
19605                 rack->r_ctl.timer_slop = optval;
19606                 if (rack->rc_tp->t_srtt) {
19607                         /*
19608                          * If we have an SRTT lets update t_rxtcur
19609                          * to have the new slop.
19610                          */
19611                         RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
19612                                            rack_rto_min, rack_rto_max,
19613                                            rack->r_ctl.timer_slop);
19614                 }
19615                 break;
19616         case TCP_RACK_PACING_BETA_ECN:
19617                 RACK_OPTS_INC(tcp_rack_beta_ecn);
19618                 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
19619                         /* This only works for newreno. */
19620                         error = EINVAL;
19621                         break;
19622                 }
19623                 if (rack->rc_pacing_cc_set) {
19624                         /*
19625                          * Set them into the real CC module
19626                          * whats in the rack pcb is the old values
19627                          * to be used on restoral/
19628                          */
19629                         sopt.sopt_dir = SOPT_SET;
19630                         opt.name = CC_NEWRENO_BETA_ECN;
19631                         opt.val = optval;
19632                         if (CC_ALGO(tp)->ctl_output != NULL)
19633                                 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19634                         else
19635                                 error = ENOENT;
19636                 } else {
19637                         /*
19638                          * Not pacing yet so set it into our local
19639                          * rack pcb storage.
19640                          */
19641                         rack->r_ctl.rc_saved_beta.beta_ecn = optval;
19642                         rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
19643                 }
19644                 break;
19645         case TCP_DEFER_OPTIONS:
19646                 RACK_OPTS_INC(tcp_defer_opt);
19647                 if (optval) {
19648                         if (rack->gp_ready) {
19649                                 /* Too late */
19650                                 error = EINVAL;
19651                                 break;
19652                         }
19653                         rack->defer_options = 1;
19654                 } else
19655                         rack->defer_options = 0;
19656                 break;
19657         case TCP_RACK_MEASURE_CNT:
19658                 RACK_OPTS_INC(tcp_rack_measure_cnt);
19659                 if (optval && (optval <= 0xff)) {
19660                         rack->r_ctl.req_measurements = optval;
19661                 } else
19662                         error = EINVAL;
19663                 break;
19664         case TCP_REC_ABC_VAL:
19665                 RACK_OPTS_INC(tcp_rec_abc_val);
19666                 if (optval > 0)
19667                         rack->r_use_labc_for_rec = 1;
19668                 else
19669                         rack->r_use_labc_for_rec = 0;
19670                 break;
19671         case TCP_RACK_ABC_VAL:
19672                 RACK_OPTS_INC(tcp_rack_abc_val);
19673                 if ((optval > 0) && (optval < 255))
19674                         rack->rc_labc = optval;
19675                 else
19676                         error = EINVAL;
19677                 break;
19678         case TCP_HDWR_UP_ONLY:
19679                 RACK_OPTS_INC(tcp_pacing_up_only);
19680                 if (optval)
19681                         rack->r_up_only = 1;
19682                 else
19683                         rack->r_up_only = 0;
19684                 break;
19685         case TCP_PACING_RATE_CAP:
19686                 RACK_OPTS_INC(tcp_pacing_rate_cap);
19687                 rack->r_ctl.bw_rate_cap = loptval;
19688                 break;
19689         case TCP_RACK_PROFILE:
19690                 RACK_OPTS_INC(tcp_profile);
19691                 error = rack_set_profile(rack, optval);
19692                 break;
19693         case TCP_USE_CMP_ACKS:
19694                 RACK_OPTS_INC(tcp_use_cmp_acks);
19695                 if ((optval == 0) && (rack->rc_inp->inp_flags2 & INP_MBUF_ACKCMP)) {
19696                         /* You can't turn it off once its on! */
19697                         error = EINVAL;
19698                 } else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
19699                         rack->r_use_cmp_ack = 1;
19700                         rack->r_mbuf_queue = 1;
19701                         tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19702                 }
19703                 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
19704                         rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19705                 break;
19706         case TCP_SHARED_CWND_TIME_LIMIT:
19707                 RACK_OPTS_INC(tcp_lscwnd);
19708                 if (optval)
19709                         rack->r_limit_scw = 1;
19710                 else
19711                         rack->r_limit_scw = 0;
19712                 break;
19713         case TCP_RACK_PACE_TO_FILL:
19714                 RACK_OPTS_INC(tcp_fillcw);
19715                 if (optval == 0)
19716                         rack->rc_pace_to_cwnd = 0;
19717                 else {
19718                         rack->rc_pace_to_cwnd = 1;
19719                         if (optval > 1)
19720                                 rack->r_fill_less_agg = 1;
19721                 }
19722                 if ((optval >= rack_gp_rtt_maxmul) &&
19723                     rack_gp_rtt_maxmul &&
19724                     (optval < 0xf)) {
19725                         rack->rc_pace_fill_if_rttin_range = 1;
19726                         rack->rtt_limit_mul = optval;
19727                 } else {
19728                         rack->rc_pace_fill_if_rttin_range = 0;
19729                         rack->rtt_limit_mul = 0;
19730                 }
19731                 break;
19732         case TCP_RACK_NO_PUSH_AT_MAX:
19733                 RACK_OPTS_INC(tcp_npush);
19734                 if (optval == 0)
19735                         rack->r_ctl.rc_no_push_at_mrtt = 0;
19736                 else if (optval < 0xff)
19737                         rack->r_ctl.rc_no_push_at_mrtt = optval;
19738                 else
19739                         error = EINVAL;
19740                 break;
19741         case TCP_SHARED_CWND_ENABLE:
19742                 RACK_OPTS_INC(tcp_rack_scwnd);
19743                 if (optval == 0)
19744                         rack->rack_enable_scwnd = 0;
19745                 else
19746                         rack->rack_enable_scwnd = 1;
19747                 break;
19748         case TCP_RACK_MBUF_QUEUE:
19749                 /* Now do we use the LRO mbuf-queue feature */
19750                 RACK_OPTS_INC(tcp_rack_mbufq);
19751                 if (optval || rack->r_use_cmp_ack)
19752                         rack->r_mbuf_queue = 1;
19753                 else
19754                         rack->r_mbuf_queue = 0;
19755                 if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19756                         tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19757                 else
19758                         tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19759                 break;
19760         case TCP_RACK_NONRXT_CFG_RATE:
19761                 RACK_OPTS_INC(tcp_rack_cfg_rate);
19762                 if (optval == 0)
19763                         rack->rack_rec_nonrxt_use_cr = 0;
19764                 else
19765                         rack->rack_rec_nonrxt_use_cr = 1;
19766                 break;
19767         case TCP_NO_PRR:
19768                 RACK_OPTS_INC(tcp_rack_noprr);
19769                 if (optval == 0)
19770                         rack->rack_no_prr = 0;
19771                 else if (optval == 1)
19772                         rack->rack_no_prr = 1;
19773                 else if (optval == 2)
19774                         rack->no_prr_addback = 1;
19775                 else
19776                         error = EINVAL;
19777                 break;
19778         case TCP_TIMELY_DYN_ADJ:
19779                 RACK_OPTS_INC(tcp_timely_dyn);
19780                 if (optval == 0)
19781                         rack->rc_gp_dyn_mul = 0;
19782                 else {
19783                         rack->rc_gp_dyn_mul = 1;
19784                         if (optval >= 100) {
19785                                 /*
19786                                  * If the user sets something 100 or more
19787                                  * its the gp_ca value.
19788                                  */
19789                                 rack->r_ctl.rack_per_of_gp_ca  = optval;
19790                         }
19791                 }
19792                 break;
19793         case TCP_RACK_DO_DETECTION:
19794                 RACK_OPTS_INC(tcp_rack_do_detection);
19795                 if (optval == 0)
19796                         rack->do_detection = 0;
19797                 else
19798                         rack->do_detection = 1;
19799                 break;
19800         case TCP_RACK_TLP_USE:
19801                 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
19802                         error = EINVAL;
19803                         break;
19804                 }
19805                 RACK_OPTS_INC(tcp_tlp_use);
19806                 rack->rack_tlp_threshold_use = optval;
19807                 break;
19808         case TCP_RACK_TLP_REDUCE:
19809                 /* RACK TLP cwnd reduction (bool) */
19810                 RACK_OPTS_INC(tcp_rack_tlp_reduce);
19811                 rack->r_ctl.rc_tlp_cwnd_reduce = optval;
19812                 break;
19813         /*  Pacing related ones */
19814         case TCP_RACK_PACE_ALWAYS:
19815                 /*
19816                  * zero is old rack method, 1 is new
19817                  * method using a pacing rate.
19818                  */
19819                 RACK_OPTS_INC(tcp_rack_pace_always);
19820                 if (optval > 0) {
19821                         if (rack->rc_always_pace) {
19822                                 error = EALREADY;
19823                                 break;
19824                         } else if (tcp_can_enable_pacing()) {
19825                                 rack->rc_always_pace = 1;
19826                                 if (rack->use_fixed_rate || rack->gp_ready)
19827                                         rack_set_cc_pacing(rack);
19828                         }
19829                         else {
19830                                 error = ENOSPC;
19831                                 break;
19832                         }
19833                 } else {
19834                         if (rack->rc_always_pace) {
19835                                 tcp_decrement_paced_conn();
19836                                 rack->rc_always_pace = 0;
19837                                 rack_undo_cc_pacing(rack);
19838                         }
19839                 }
19840                 if  (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19841                         tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
19842                 else
19843                         tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19844                 /* A rate may be set irate or other, if so set seg size */
19845                 rack_update_seg(rack);
19846                 break;
19847         case TCP_BBR_RACK_INIT_RATE:
19848                 RACK_OPTS_INC(tcp_initial_rate);
19849                 val = optval;
19850                 /* Change from kbits per second to bytes per second */
19851                 val *= 1000;
19852                 val /= 8;
19853                 rack->r_ctl.init_rate = val;
19854                 if (rack->rc_init_win != rack_default_init_window) {
19855                         uint32_t win, snt;
19856
19857                         /*
19858                          * Options don't always get applied
19859                          * in the order you think. So in order
19860                          * to assure we update a cwnd we need
19861                          * to check and see if we are still
19862                          * where we should raise the cwnd.
19863                          */
19864                         win = rc_init_window(rack);
19865                         if (SEQ_GT(tp->snd_max, tp->iss))
19866                                 snt = tp->snd_max - tp->iss;
19867                         else
19868                                 snt = 0;
19869                         if ((snt < win) &&
19870                             (tp->snd_cwnd < win))
19871                                 tp->snd_cwnd = win;
19872                 }
19873                 if (rack->rc_always_pace)
19874                         rack_update_seg(rack);
19875                 break;
19876         case TCP_BBR_IWINTSO:
19877                 RACK_OPTS_INC(tcp_initial_win);
19878                 if (optval && (optval <= 0xff)) {
19879                         uint32_t win, snt;
19880
19881                         rack->rc_init_win = optval;
19882                         win = rc_init_window(rack);
19883                         if (SEQ_GT(tp->snd_max, tp->iss))
19884                                 snt = tp->snd_max - tp->iss;
19885                         else
19886                                 snt = 0;
19887                         if ((snt < win) &&
19888                             (tp->t_srtt |
19889 #ifdef NETFLIX_PEAKRATE
19890                              tp->t_maxpeakrate |
19891 #endif
19892                              rack->r_ctl.init_rate)) {
19893                                 /*
19894                                  * We are not past the initial window
19895                                  * and we have some bases for pacing,
19896                                  * so we need to possibly adjust up
19897                                  * the cwnd. Note even if we don't set
19898                                  * the cwnd, its still ok to raise the rc_init_win
19899                                  * which can be used coming out of idle when we
19900                                  * would have a rate.
19901                                  */
19902                                 if (tp->snd_cwnd < win)
19903                                         tp->snd_cwnd = win;
19904                         }
19905                         if (rack->rc_always_pace)
19906                                 rack_update_seg(rack);
19907                 } else
19908                         error = EINVAL;
19909                 break;
19910         case TCP_RACK_FORCE_MSEG:
19911                 RACK_OPTS_INC(tcp_rack_force_max_seg);
19912                 if (optval)
19913                         rack->rc_force_max_seg = 1;
19914                 else
19915                         rack->rc_force_max_seg = 0;
19916                 break;
19917         case TCP_RACK_PACE_MAX_SEG:
19918                 /* Max segments size in a pace in bytes */
19919                 RACK_OPTS_INC(tcp_rack_max_seg);
19920                 rack->rc_user_set_max_segs = optval;
19921                 rack_set_pace_segments(tp, rack, __LINE__, NULL);
19922                 break;
19923         case TCP_RACK_PACE_RATE_REC:
19924                 /* Set the fixed pacing rate in Bytes per second ca */
19925                 RACK_OPTS_INC(tcp_rack_pace_rate_rec);
19926                 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
19927                 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
19928                         rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
19929                 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
19930                         rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
19931                 rack->use_fixed_rate = 1;
19932                 if (rack->rc_always_pace)
19933                         rack_set_cc_pacing(rack);
19934                 rack_log_pacing_delay_calc(rack,
19935                                            rack->r_ctl.rc_fixed_pacing_rate_ss,
19936                                            rack->r_ctl.rc_fixed_pacing_rate_ca,
19937                                            rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
19938                                            __LINE__, NULL,0);
19939                 break;
19940
19941         case TCP_RACK_PACE_RATE_SS:
19942                 /* Set the fixed pacing rate in Bytes per second ca */
19943                 RACK_OPTS_INC(tcp_rack_pace_rate_ss);
19944                 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
19945                 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
19946                         rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
19947                 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
19948                         rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
19949                 rack->use_fixed_rate = 1;
19950                 if (rack->rc_always_pace)
19951                         rack_set_cc_pacing(rack);
19952                 rack_log_pacing_delay_calc(rack,
19953                                            rack->r_ctl.rc_fixed_pacing_rate_ss,
19954                                            rack->r_ctl.rc_fixed_pacing_rate_ca,
19955                                            rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
19956                                            __LINE__, NULL, 0);
19957                 break;
19958
19959         case TCP_RACK_PACE_RATE_CA:
19960                 /* Set the fixed pacing rate in Bytes per second ca */
19961                 RACK_OPTS_INC(tcp_rack_pace_rate_ca);
19962                 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
19963                 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
19964                         rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
19965                 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
19966                         rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
19967                 rack->use_fixed_rate = 1;
19968                 if (rack->rc_always_pace)
19969                         rack_set_cc_pacing(rack);
19970                 rack_log_pacing_delay_calc(rack,
19971                                            rack->r_ctl.rc_fixed_pacing_rate_ss,
19972                                            rack->r_ctl.rc_fixed_pacing_rate_ca,
19973                                            rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
19974                                            __LINE__, NULL, 0);
19975                 break;
19976         case TCP_RACK_GP_INCREASE_REC:
19977                 RACK_OPTS_INC(tcp_gp_inc_rec);
19978                 rack->r_ctl.rack_per_of_gp_rec = optval;
19979                 rack_log_pacing_delay_calc(rack,
19980                                            rack->r_ctl.rack_per_of_gp_ss,
19981                                            rack->r_ctl.rack_per_of_gp_ca,
19982                                            rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
19983                                            __LINE__, NULL, 0);
19984                 break;
19985         case TCP_RACK_GP_INCREASE_CA:
19986                 RACK_OPTS_INC(tcp_gp_inc_ca);
19987                 ca = optval;
19988                 if (ca < 100) {
19989                         /*
19990                          * We don't allow any reduction
19991                          * over the GP b/w.
19992                          */
19993                         error = EINVAL;
19994                         break;
19995                 }
19996                 rack->r_ctl.rack_per_of_gp_ca = ca;
19997                 rack_log_pacing_delay_calc(rack,
19998                                            rack->r_ctl.rack_per_of_gp_ss,
19999                                            rack->r_ctl.rack_per_of_gp_ca,
20000                                            rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20001                                            __LINE__, NULL, 0);
20002                 break;
20003         case TCP_RACK_GP_INCREASE_SS:
20004                 RACK_OPTS_INC(tcp_gp_inc_ss);
20005                 ss = optval;
20006                 if (ss < 100) {
20007                         /*
20008                          * We don't allow any reduction
20009                          * over the GP b/w.
20010                          */
20011                         error = EINVAL;
20012                         break;
20013                 }
20014                 rack->r_ctl.rack_per_of_gp_ss = ss;
20015                 rack_log_pacing_delay_calc(rack,
20016                                            rack->r_ctl.rack_per_of_gp_ss,
20017                                            rack->r_ctl.rack_per_of_gp_ca,
20018                                            rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
20019                                            __LINE__, NULL, 0);
20020                 break;
20021         case TCP_RACK_RR_CONF:
20022                 RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
20023                 if (optval && optval <= 3)
20024                         rack->r_rr_config = optval;
20025                 else
20026                         rack->r_rr_config = 0;
20027                 break;
20028         case TCP_HDWR_RATE_CAP:
20029                 RACK_OPTS_INC(tcp_hdwr_rate_cap);
20030                 if (optval) {
20031                         if (rack->r_rack_hw_rate_caps == 0)
20032                                 rack->r_rack_hw_rate_caps = 1;
20033                         else
20034                                 error = EALREADY;
20035                 } else {
20036                         rack->r_rack_hw_rate_caps = 0;
20037                 }
20038                 break;
20039         case TCP_BBR_HDWR_PACE:
20040                 RACK_OPTS_INC(tcp_hdwr_pacing);
20041                 if (optval){
20042                         if (rack->rack_hdrw_pacing == 0) {
20043                                 rack->rack_hdw_pace_ena = 1;
20044                                 rack->rack_attempt_hdwr_pace = 0;
20045                         } else
20046                                 error = EALREADY;
20047                 } else {
20048                         rack->rack_hdw_pace_ena = 0;
20049 #ifdef RATELIMIT
20050                         if (rack->r_ctl.crte != NULL) {
20051                                 rack->rack_hdrw_pacing = 0;
20052                                 rack->rack_attempt_hdwr_pace = 0;
20053                                 tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
20054                                 rack->r_ctl.crte = NULL;
20055                         }
20056 #endif
20057                 }
20058                 break;
20059         /*  End Pacing related ones */
20060         case TCP_RACK_PRR_SENDALOT:
20061                 /* Allow PRR to send more than one seg */
20062                 RACK_OPTS_INC(tcp_rack_prr_sendalot);
20063                 rack->r_ctl.rc_prr_sendalot = optval;
20064                 break;
20065         case TCP_RACK_MIN_TO:
20066                 /* Minimum time between rack t-o's in ms */
20067                 RACK_OPTS_INC(tcp_rack_min_to);
20068                 rack->r_ctl.rc_min_to = optval;
20069                 break;
20070         case TCP_RACK_EARLY_SEG:
20071                 /* If early recovery max segments */
20072                 RACK_OPTS_INC(tcp_rack_early_seg);
20073                 rack->r_ctl.rc_early_recovery_segs = optval;
20074                 break;
20075         case TCP_RACK_ENABLE_HYSTART:
20076         {
20077                 struct sockopt sopt;
20078                 struct cc_newreno_opts opt;
20079
20080                 sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
20081                 sopt.sopt_dir = SOPT_SET;
20082                 opt.name = CC_NEWRENO_ENABLE_HYSTART;
20083                 opt.val = optval;
20084                 if (CC_ALGO(tp)->ctl_output != NULL)
20085                         error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
20086                 else
20087                         error = EINVAL;
20088         }
20089         break;
20090         case TCP_RACK_REORD_THRESH:
20091                 /* RACK reorder threshold (shift amount) */
20092                 RACK_OPTS_INC(tcp_rack_reord_thresh);
20093                 if ((optval > 0) && (optval < 31))
20094                         rack->r_ctl.rc_reorder_shift = optval;
20095                 else
20096                         error = EINVAL;
20097                 break;
20098         case TCP_RACK_REORD_FADE:
20099                 /* Does reordering fade after ms time */
20100                 RACK_OPTS_INC(tcp_rack_reord_fade);
20101                 rack->r_ctl.rc_reorder_fade = optval;
20102                 break;
20103         case TCP_RACK_TLP_THRESH:
20104                 /* RACK TLP theshold i.e. srtt+(srtt/N) */
20105                 RACK_OPTS_INC(tcp_rack_tlp_thresh);
20106                 if (optval)
20107                         rack->r_ctl.rc_tlp_threshold = optval;
20108                 else
20109                         error = EINVAL;
20110                 break;
20111         case TCP_BBR_USE_RACK_RR:
20112                 RACK_OPTS_INC(tcp_rack_rr);
20113                 if (optval)
20114                         rack->use_rack_rr = 1;
20115                 else
20116                         rack->use_rack_rr = 0;
20117                 break;
20118         case TCP_FAST_RSM_HACK:
20119                 RACK_OPTS_INC(tcp_rack_fastrsm_hack);
20120                 if (optval)
20121                         rack->fast_rsm_hack = 1;
20122                 else
20123                         rack->fast_rsm_hack = 0;
20124                 break;
20125         case TCP_RACK_PKT_DELAY:
20126                 /* RACK added ms i.e. rack-rtt + reord + N */
20127                 RACK_OPTS_INC(tcp_rack_pkt_delay);
20128                 rack->r_ctl.rc_pkt_delay = optval;
20129                 break;
20130         case TCP_DELACK:
20131                 RACK_OPTS_INC(tcp_rack_delayed_ack);
20132                 if (optval == 0)
20133                         tp->t_delayed_ack = 0;
20134                 else
20135                         tp->t_delayed_ack = 1;
20136                 if (tp->t_flags & TF_DELACK) {
20137                         tp->t_flags &= ~TF_DELACK;
20138                         tp->t_flags |= TF_ACKNOW;
20139                         NET_EPOCH_ENTER(et);
20140                         rack_output(tp);
20141                         NET_EPOCH_EXIT(et);
20142                 }
20143                 break;
20144
20145         case TCP_BBR_RACK_RTT_USE:
20146                 RACK_OPTS_INC(tcp_rack_rtt_use);
20147                 if ((optval != USE_RTT_HIGH) &&
20148                     (optval != USE_RTT_LOW) &&
20149                     (optval != USE_RTT_AVG))
20150                         error = EINVAL;
20151                 else
20152                         rack->r_ctl.rc_rate_sample_method = optval;
20153                 break;
20154         case TCP_DATA_AFTER_CLOSE:
20155                 RACK_OPTS_INC(tcp_data_after_close);
20156                 if (optval)
20157                         rack->rc_allow_data_af_clo = 1;
20158                 else
20159                         rack->rc_allow_data_af_clo = 0;
20160                 break;
20161         default:
20162                 break;
20163         }
20164 #ifdef NETFLIX_STATS
20165         tcp_log_socket_option(tp, sopt_name, optval, error);
20166 #endif
20167         return (error);
20168 }
20169
20170
20171 static void
20172 rack_apply_deferred_options(struct tcp_rack *rack)
20173 {
20174         struct deferred_opt_list *dol, *sdol;
20175         uint32_t s_optval;
20176
20177         TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
20178                 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
20179                 /* Disadvantage of deferal is you loose the error return */
20180                 s_optval = (uint32_t)dol->optval;
20181                 (void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval);
20182                 free(dol, M_TCPDO);
20183         }
20184 }
20185
20186 static void
20187 rack_hw_tls_change(struct tcpcb *tp, int chg)
20188 {
20189         /*
20190          * HW tls state has changed.. fix all
20191          * rsm's in flight.
20192          */
20193         struct tcp_rack *rack;
20194         struct rack_sendmap *rsm;
20195
20196         rack = (struct tcp_rack *)tp->t_fb_ptr;
20197         RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
20198                 if (chg)
20199                         rsm->r_hw_tls = 1;
20200                 else
20201                         rsm->r_hw_tls = 0;
20202         }
20203         if (chg)
20204                 rack->r_ctl.fsb.hw_tls = 1;
20205         else
20206                 rack->r_ctl.fsb.hw_tls = 0;
20207 }
20208
20209 static int
20210 rack_pru_options(struct tcpcb *tp, int flags)
20211 {
20212         if (flags & PRUS_OOB)
20213                 return (EOPNOTSUPP);
20214         return (0);
20215 }
20216
20217 static struct tcp_function_block __tcp_rack = {
20218         .tfb_tcp_block_name = __XSTRING(STACKNAME),
20219         .tfb_tcp_output = rack_output,
20220         .tfb_do_queued_segments = ctf_do_queued_segments,
20221         .tfb_do_segment_nounlock = rack_do_segment_nounlock,
20222         .tfb_tcp_do_segment = rack_do_segment,
20223         .tfb_tcp_ctloutput = rack_ctloutput,
20224         .tfb_tcp_fb_init = rack_init,
20225         .tfb_tcp_fb_fini = rack_fini,
20226         .tfb_tcp_timer_stop_all = rack_stopall,
20227         .tfb_tcp_timer_activate = rack_timer_activate,
20228         .tfb_tcp_timer_active = rack_timer_active,
20229         .tfb_tcp_timer_stop = rack_timer_stop,
20230         .tfb_tcp_rexmit_tmr = rack_remxt_tmr,
20231         .tfb_tcp_handoff_ok = rack_handoff_ok,
20232         .tfb_tcp_mtu_chg = rack_mtu_change,
20233         .tfb_pru_options = rack_pru_options,
20234         .tfb_hwtls_change = rack_hw_tls_change,
20235 };
20236
20237 /*
20238  * rack_ctloutput() must drop the inpcb lock before performing copyin on
20239  * socket option arguments.  When it re-acquires the lock after the copy, it
20240  * has to revalidate that the connection is still valid for the socket
20241  * option.
20242  */
20243 static int
20244 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
20245     struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
20246 {
20247         uint64_t loptval;
20248         int32_t error = 0, optval;
20249
20250         switch (sopt->sopt_name) {
20251         case TCP_RACK_TLP_REDUCE:               /*  URL:tlp_reduce */
20252         /*  Pacing related ones */
20253         case TCP_RACK_PACE_ALWAYS:              /*  URL:pace_always */
20254         case TCP_BBR_RACK_INIT_RATE:            /*  URL:irate */
20255         case TCP_BBR_IWINTSO:                   /*  URL:tso_iwin */
20256         case TCP_RACK_PACE_MAX_SEG:             /*  URL:pace_max_seg */
20257         case TCP_RACK_FORCE_MSEG:               /*  URL:force_max_seg */
20258         case TCP_RACK_PACE_RATE_CA:             /*  URL:pr_ca */
20259         case TCP_RACK_PACE_RATE_SS:             /*  URL:pr_ss*/
20260         case TCP_RACK_PACE_RATE_REC:            /*  URL:pr_rec */
20261         case TCP_RACK_GP_INCREASE_CA:           /*  URL:gp_inc_ca */
20262         case TCP_RACK_GP_INCREASE_SS:           /*  URL:gp_inc_ss */
20263         case TCP_RACK_GP_INCREASE_REC:          /*  URL:gp_inc_rec */
20264         case TCP_RACK_RR_CONF:                  /*  URL:rrr_conf */
20265         case TCP_BBR_HDWR_PACE:                 /*  URL:hdwrpace */
20266         case TCP_HDWR_RATE_CAP:                 /*  URL:hdwrcap boolean */
20267         case TCP_PACING_RATE_CAP:               /*  URL:cap  -- used by side-channel */
20268         case TCP_HDWR_UP_ONLY:                  /*  URL:uponly -- hardware pacing  boolean */
20269        /* End pacing related */
20270         case TCP_FAST_RSM_HACK:                 /*  URL:frsm_hack */
20271         case TCP_DELACK:                        /*  URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
20272         case TCP_RACK_PRR_SENDALOT:             /*  URL:prr_sendalot */
20273         case TCP_RACK_MIN_TO:                   /*  URL:min_to */
20274         case TCP_RACK_EARLY_SEG:                /*  URL:early_seg */
20275         case TCP_RACK_REORD_THRESH:             /*  URL:reord_thresh */
20276         case TCP_RACK_REORD_FADE:               /*  URL:reord_fade */
20277         case TCP_RACK_TLP_THRESH:               /*  URL:tlp_thresh */
20278         case TCP_RACK_PKT_DELAY:                /*  URL:pkt_delay */
20279         case TCP_RACK_TLP_USE:                  /*  URL:tlp_use */
20280         case TCP_BBR_RACK_RTT_USE:              /*  URL:rttuse */
20281         case TCP_BBR_USE_RACK_RR:               /*  URL:rackrr */
20282         case TCP_RACK_DO_DETECTION:             /*  URL:detect */
20283         case TCP_NO_PRR:                        /*  URL:noprr */
20284         case TCP_TIMELY_DYN_ADJ:                /*  URL:dynamic */
20285         case TCP_DATA_AFTER_CLOSE:              /*  no URL */
20286         case TCP_RACK_NONRXT_CFG_RATE:          /*  URL:nonrxtcr */
20287         case TCP_SHARED_CWND_ENABLE:            /*  URL:scwnd */
20288         case TCP_RACK_MBUF_QUEUE:               /*  URL:mqueue */
20289         case TCP_RACK_NO_PUSH_AT_MAX:           /*  URL:npush */
20290         case TCP_RACK_PACE_TO_FILL:             /*  URL:fillcw */
20291         case TCP_SHARED_CWND_TIME_LIMIT:        /*  URL:lscwnd */
20292         case TCP_RACK_PROFILE:                  /*  URL:profile */
20293         case TCP_USE_CMP_ACKS:                  /*  URL:cmpack */
20294         case TCP_RACK_ABC_VAL:                  /*  URL:labc */
20295         case TCP_REC_ABC_VAL:                   /*  URL:reclabc */
20296         case TCP_RACK_MEASURE_CNT:              /*  URL:measurecnt */
20297         case TCP_DEFER_OPTIONS:                 /*  URL:defer */
20298         case TCP_RACK_DSACK_OPT:                /*  URL:dsack */
20299         case TCP_RACK_PACING_BETA:              /*  URL:pacing_beta */
20300         case TCP_RACK_PACING_BETA_ECN:          /*  URL:pacing_beta_ecn */
20301         case TCP_RACK_TIMER_SLOP:               /*  URL:timer_slop */
20302         case TCP_RACK_ENABLE_HYSTART:           /*  URL:hystart */
20303                 break;
20304         default:
20305                 /* Filter off all unknown options to the base stack */
20306                 return (tcp_default_ctloutput(so, sopt, inp, tp));
20307                 break;
20308         }
20309         INP_WUNLOCK(inp);
20310         if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
20311                 error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
20312                 /*
20313                  * We truncate it down to 32 bits for the socket-option trace this
20314                  * means rates > 34Gbps won't show right, but thats probably ok.
20315                  */
20316                 optval = (uint32_t)loptval;
20317         } else {
20318                 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
20319                 /* Save it in 64 bit form too */
20320                 loptval = optval;
20321         }
20322         if (error)
20323                 return (error);
20324         INP_WLOCK(inp);
20325         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
20326                 INP_WUNLOCK(inp);
20327                 return (ECONNRESET);
20328         }
20329         if (tp->t_fb != &__tcp_rack) {
20330                 INP_WUNLOCK(inp);
20331                 return (ENOPROTOOPT);
20332         }
20333         if (rack->defer_options && (rack->gp_ready == 0) &&
20334             (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
20335             (sopt->sopt_name != TCP_RACK_PACING_BETA) &&
20336             (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
20337             (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
20338                 /* Options are beind deferred */
20339                 if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
20340                         INP_WUNLOCK(inp);
20341                         return (0);
20342                 } else {
20343                         /* No memory to defer, fail */
20344                         INP_WUNLOCK(inp);
20345                         return (ENOMEM);
20346                 }
20347         }
20348         error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval);
20349         INP_WUNLOCK(inp);
20350         return (error);
20351 }
20352
20353 static void
20354 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
20355 {
20356
20357         INP_WLOCK_ASSERT(tp->t_inpcb);
20358         bzero(ti, sizeof(*ti));
20359
20360         ti->tcpi_state = tp->t_state;
20361         if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
20362                 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
20363         if (tp->t_flags & TF_SACK_PERMIT)
20364                 ti->tcpi_options |= TCPI_OPT_SACK;
20365         if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
20366                 ti->tcpi_options |= TCPI_OPT_WSCALE;
20367                 ti->tcpi_snd_wscale = tp->snd_scale;
20368                 ti->tcpi_rcv_wscale = tp->rcv_scale;
20369         }
20370         if (tp->t_flags2 & TF2_ECN_PERMIT)
20371                 ti->tcpi_options |= TCPI_OPT_ECN;
20372         if (tp->t_flags & TF_FASTOPEN)
20373                 ti->tcpi_options |= TCPI_OPT_TFO;
20374         /* still kept in ticks is t_rcvtime */
20375         ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
20376         /* Since we hold everything in precise useconds this is easy */
20377         ti->tcpi_rtt = tp->t_srtt;
20378         ti->tcpi_rttvar = tp->t_rttvar;
20379         ti->tcpi_rto = tp->t_rxtcur;
20380         ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
20381         ti->tcpi_snd_cwnd = tp->snd_cwnd;
20382         /*
20383          * FreeBSD-specific extension fields for tcp_info.
20384          */
20385         ti->tcpi_rcv_space = tp->rcv_wnd;
20386         ti->tcpi_rcv_nxt = tp->rcv_nxt;
20387         ti->tcpi_snd_wnd = tp->snd_wnd;
20388         ti->tcpi_snd_bwnd = 0;          /* Unused, kept for compat. */
20389         ti->tcpi_snd_nxt = tp->snd_nxt;
20390         ti->tcpi_snd_mss = tp->t_maxseg;
20391         ti->tcpi_rcv_mss = tp->t_maxseg;
20392         ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
20393         ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
20394         ti->tcpi_snd_zerowin = tp->t_sndzerowin;
20395 #ifdef NETFLIX_STATS
20396         ti->tcpi_total_tlp = tp->t_sndtlppack;
20397         ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
20398         memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
20399 #endif
20400 #ifdef TCP_OFFLOAD
20401         if (tp->t_flags & TF_TOE) {
20402                 ti->tcpi_options |= TCPI_OPT_TOE;
20403                 tcp_offload_tcp_info(tp, ti);
20404         }
20405 #endif
20406 }
20407
20408 static int
20409 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
20410     struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
20411 {
20412         int32_t error, optval;
20413         uint64_t val, loptval;
20414         struct  tcp_info ti;
20415         /*
20416          * Because all our options are either boolean or an int, we can just
20417          * pull everything into optval and then unlock and copy. If we ever
20418          * add a option that is not a int, then this will have quite an
20419          * impact to this routine.
20420          */
20421         error = 0;
20422         switch (sopt->sopt_name) {
20423         case TCP_INFO:
20424                 /* First get the info filled */
20425                 rack_fill_info(tp, &ti);
20426                 /* Fix up the rtt related fields if needed */
20427                 INP_WUNLOCK(inp);
20428                 error = sooptcopyout(sopt, &ti, sizeof ti);
20429                 return (error);
20430         /*
20431          * Beta is the congestion control value for NewReno that influences how
20432          * much of a backoff happens when loss is detected. It is normally set
20433          * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
20434          * when you exit recovery.
20435          */
20436         case TCP_RACK_PACING_BETA:
20437                 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20438                         error = EINVAL;
20439                 else if (rack->rc_pacing_cc_set == 0)
20440                         optval = rack->r_ctl.rc_saved_beta.beta;
20441                 else {
20442                         /*
20443                          * Reach out into the CC data and report back what
20444                          * I have previously set. Yeah it looks hackish but
20445                          * we don't want to report the saved values.
20446                          */
20447                         if (tp->ccv->cc_data)
20448                                 optval = ((struct newreno *)tp->ccv->cc_data)->beta;
20449                         else
20450                                 error = EINVAL;
20451                 }
20452                 break;
20453                 /*
20454                  * Beta_ecn is the congestion control value for NewReno that influences how
20455                  * much of a backoff happens when a ECN mark is detected. It is normally set
20456                  * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
20457                  * you exit recovery. Note that classic ECN has a beta of 50, it is only
20458                  * ABE Ecn that uses this "less" value, but we do too with pacing :)
20459                  */
20460
20461         case TCP_RACK_PACING_BETA_ECN:
20462                 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20463                         error = EINVAL;
20464                 else if (rack->rc_pacing_cc_set == 0)
20465                         optval = rack->r_ctl.rc_saved_beta.beta_ecn;
20466                 else {
20467                         /*
20468                          * Reach out into the CC data and report back what
20469                          * I have previously set. Yeah it looks hackish but
20470                          * we don't want to report the saved values.
20471                          */
20472                         if (tp->ccv->cc_data)
20473                                 optval = ((struct newreno *)tp->ccv->cc_data)->beta_ecn;
20474                         else
20475                                 error = EINVAL;
20476                 }
20477                 break;
20478         case TCP_RACK_DSACK_OPT:
20479                 optval = 0;
20480                 if (rack->rc_rack_tmr_std_based) {
20481                         optval |= 1;
20482                 }
20483                 if (rack->rc_rack_use_dsack) {
20484                         optval |= 2;
20485                 }
20486                 break;
20487         case TCP_RACK_ENABLE_HYSTART:
20488         {
20489                 struct sockopt sopt;
20490                 struct cc_newreno_opts opt;
20491
20492                 sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
20493                 sopt.sopt_dir = SOPT_GET;
20494                 opt.name = CC_NEWRENO_ENABLE_HYSTART;
20495                 if (CC_ALGO(tp)->ctl_output != NULL)
20496                         error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
20497                 else
20498                         error = EINVAL;
20499                 optval = opt.val;
20500         }
20501         break;
20502         case TCP_FAST_RSM_HACK:
20503                 optval = rack->fast_rsm_hack;
20504                 break;
20505         case TCP_DEFER_OPTIONS:
20506                 optval = rack->defer_options;
20507                 break;
20508         case TCP_RACK_MEASURE_CNT:
20509                 optval = rack->r_ctl.req_measurements;
20510                 break;
20511         case TCP_REC_ABC_VAL:
20512                 optval = rack->r_use_labc_for_rec;
20513                 break;
20514         case TCP_RACK_ABC_VAL:
20515                 optval = rack->rc_labc;
20516                 break;
20517         case TCP_HDWR_UP_ONLY:
20518                 optval= rack->r_up_only;
20519                 break;
20520         case TCP_PACING_RATE_CAP:
20521                 loptval = rack->r_ctl.bw_rate_cap;
20522                 break;
20523         case TCP_RACK_PROFILE:
20524                 /* You cannot retrieve a profile, its write only */
20525                 error = EINVAL;
20526                 break;
20527         case TCP_USE_CMP_ACKS:
20528                 optval = rack->r_use_cmp_ack;
20529                 break;
20530         case TCP_RACK_PACE_TO_FILL:
20531                 optval = rack->rc_pace_to_cwnd;
20532                 if (optval && rack->r_fill_less_agg)
20533                         optval++;
20534                 break;
20535         case TCP_RACK_NO_PUSH_AT_MAX:
20536                 optval = rack->r_ctl.rc_no_push_at_mrtt;
20537                 break;
20538         case TCP_SHARED_CWND_ENABLE:
20539                 optval = rack->rack_enable_scwnd;
20540                 break;
20541         case TCP_RACK_NONRXT_CFG_RATE:
20542                 optval = rack->rack_rec_nonrxt_use_cr;
20543                 break;
20544         case TCP_NO_PRR:
20545                 if (rack->rack_no_prr  == 1)
20546                         optval = 1;
20547                 else if (rack->no_prr_addback == 1)
20548                         optval = 2;
20549                 else
20550                         optval = 0;
20551                 break;
20552         case TCP_RACK_DO_DETECTION:
20553                 optval = rack->do_detection;
20554                 break;
20555         case TCP_RACK_MBUF_QUEUE:
20556                 /* Now do we use the LRO mbuf-queue feature */
20557                 optval = rack->r_mbuf_queue;
20558                 break;
20559         case TCP_TIMELY_DYN_ADJ:
20560                 optval = rack->rc_gp_dyn_mul;
20561                 break;
20562         case TCP_BBR_IWINTSO:
20563                 optval = rack->rc_init_win;
20564                 break;
20565         case TCP_RACK_TLP_REDUCE:
20566                 /* RACK TLP cwnd reduction (bool) */
20567                 optval = rack->r_ctl.rc_tlp_cwnd_reduce;
20568                 break;
20569         case TCP_BBR_RACK_INIT_RATE:
20570                 val = rack->r_ctl.init_rate;
20571                 /* convert to kbits per sec */
20572                 val *= 8;
20573                 val /= 1000;
20574                 optval = (uint32_t)val;
20575                 break;
20576         case TCP_RACK_FORCE_MSEG:
20577                 optval = rack->rc_force_max_seg;
20578                 break;
20579         case TCP_RACK_PACE_MAX_SEG:
20580                 /* Max segments in a pace */
20581                 optval = rack->rc_user_set_max_segs;
20582                 break;
20583         case TCP_RACK_PACE_ALWAYS:
20584                 /* Use the always pace method */
20585                 optval = rack->rc_always_pace;
20586                 break;
20587         case TCP_RACK_PRR_SENDALOT:
20588                 /* Allow PRR to send more than one seg */
20589                 optval = rack->r_ctl.rc_prr_sendalot;
20590                 break;
20591         case TCP_RACK_MIN_TO:
20592                 /* Minimum time between rack t-o's in ms */
20593                 optval = rack->r_ctl.rc_min_to;
20594                 break;
20595         case TCP_RACK_EARLY_SEG:
20596                 /* If early recovery max segments */
20597                 optval = rack->r_ctl.rc_early_recovery_segs;
20598                 break;
20599         case TCP_RACK_REORD_THRESH:
20600                 /* RACK reorder threshold (shift amount) */
20601                 optval = rack->r_ctl.rc_reorder_shift;
20602                 break;
20603         case TCP_RACK_REORD_FADE:
20604                 /* Does reordering fade after ms time */
20605                 optval = rack->r_ctl.rc_reorder_fade;
20606                 break;
20607         case TCP_BBR_USE_RACK_RR:
20608                 /* Do we use the rack cheat for rxt */
20609                 optval = rack->use_rack_rr;
20610                 break;
20611         case TCP_RACK_RR_CONF:
20612                 optval = rack->r_rr_config;
20613                 break;
20614         case TCP_HDWR_RATE_CAP:
20615                 optval = rack->r_rack_hw_rate_caps;
20616                 break;
20617         case TCP_BBR_HDWR_PACE:
20618                 optval = rack->rack_hdw_pace_ena;
20619                 break;
20620         case TCP_RACK_TLP_THRESH:
20621                 /* RACK TLP theshold i.e. srtt+(srtt/N) */
20622                 optval = rack->r_ctl.rc_tlp_threshold;
20623                 break;
20624         case TCP_RACK_PKT_DELAY:
20625                 /* RACK added ms i.e. rack-rtt + reord + N */
20626                 optval = rack->r_ctl.rc_pkt_delay;
20627                 break;
20628         case TCP_RACK_TLP_USE:
20629                 optval = rack->rack_tlp_threshold_use;
20630                 break;
20631         case TCP_RACK_PACE_RATE_CA:
20632                 optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
20633                 break;
20634         case TCP_RACK_PACE_RATE_SS:
20635                 optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
20636                 break;
20637         case TCP_RACK_PACE_RATE_REC:
20638                 optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
20639                 break;
20640         case TCP_RACK_GP_INCREASE_SS:
20641                 optval = rack->r_ctl.rack_per_of_gp_ca;
20642                 break;
20643         case TCP_RACK_GP_INCREASE_CA:
20644                 optval = rack->r_ctl.rack_per_of_gp_ss;
20645                 break;
20646         case TCP_BBR_RACK_RTT_USE:
20647                 optval = rack->r_ctl.rc_rate_sample_method;
20648                 break;
20649         case TCP_DELACK:
20650                 optval = tp->t_delayed_ack;
20651                 break;
20652         case TCP_DATA_AFTER_CLOSE:
20653                 optval = rack->rc_allow_data_af_clo;
20654                 break;
20655         case TCP_SHARED_CWND_TIME_LIMIT:
20656                 optval = rack->r_limit_scw;
20657                 break;
20658         case TCP_RACK_TIMER_SLOP:
20659                 optval = rack->r_ctl.timer_slop;
20660                 break;
20661         default:
20662                 return (tcp_default_ctloutput(so, sopt, inp, tp));
20663                 break;
20664         }
20665         INP_WUNLOCK(inp);
20666         if (error == 0) {
20667                 if (TCP_PACING_RATE_CAP)
20668                         error = sooptcopyout(sopt, &loptval, sizeof loptval);
20669                 else
20670                         error = sooptcopyout(sopt, &optval, sizeof optval);
20671         }
20672         return (error);
20673 }
20674
20675 static int
20676 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
20677 {
20678         int32_t error = EINVAL;
20679         struct tcp_rack *rack;
20680
20681         rack = (struct tcp_rack *)tp->t_fb_ptr;
20682         if (rack == NULL) {
20683                 /* Huh? */
20684                 goto out;
20685         }
20686         if (sopt->sopt_dir == SOPT_SET) {
20687                 return (rack_set_sockopt(so, sopt, inp, tp, rack));
20688         } else if (sopt->sopt_dir == SOPT_GET) {
20689                 return (rack_get_sockopt(so, sopt, inp, tp, rack));
20690         }
20691 out:
20692         INP_WUNLOCK(inp);
20693         return (error);
20694 }
20695
20696 static const char *rack_stack_names[] = {
20697         __XSTRING(STACKNAME),
20698 #ifdef STACKALIAS
20699         __XSTRING(STACKALIAS),
20700 #endif
20701 };
20702
20703 static int
20704 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
20705 {
20706         memset(mem, 0, size);
20707         return (0);
20708 }
20709
20710 static void
20711 rack_dtor(void *mem, int32_t size, void *arg)
20712 {
20713
20714 }
20715
20716 static bool rack_mod_inited = false;
20717
20718 static int
20719 tcp_addrack(module_t mod, int32_t type, void *data)
20720 {
20721         int32_t err = 0;
20722         int num_stacks;
20723
20724         switch (type) {
20725         case MOD_LOAD:
20726                 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
20727                     sizeof(struct rack_sendmap),
20728                     rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
20729
20730                 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
20731                     sizeof(struct tcp_rack),
20732                     rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
20733
20734                 sysctl_ctx_init(&rack_sysctl_ctx);
20735                 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
20736                     SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
20737                     OID_AUTO,
20738 #ifdef STACKALIAS
20739                     __XSTRING(STACKALIAS),
20740 #else
20741                     __XSTRING(STACKNAME),
20742 #endif
20743                     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
20744                     "");
20745                 if (rack_sysctl_root == NULL) {
20746                         printf("Failed to add sysctl node\n");
20747                         err = EFAULT;
20748                         goto free_uma;
20749                 }
20750                 rack_init_sysctls();
20751                 num_stacks = nitems(rack_stack_names);
20752                 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
20753                     rack_stack_names, &num_stacks);
20754                 if (err) {
20755                         printf("Failed to register %s stack name for "
20756                             "%s module\n", rack_stack_names[num_stacks],
20757                             __XSTRING(MODNAME));
20758                         sysctl_ctx_free(&rack_sysctl_ctx);
20759 free_uma:
20760                         uma_zdestroy(rack_zone);
20761                         uma_zdestroy(rack_pcb_zone);
20762                         rack_counter_destroy();
20763                         printf("Failed to register rack module -- err:%d\n", err);
20764                         return (err);
20765                 }
20766                 tcp_lro_reg_mbufq();
20767                 rack_mod_inited = true;
20768                 break;
20769         case MOD_QUIESCE:
20770                 err = deregister_tcp_functions(&__tcp_rack, true, false);
20771                 break;
20772         case MOD_UNLOAD:
20773                 err = deregister_tcp_functions(&__tcp_rack, false, true);
20774                 if (err == EBUSY)
20775                         break;
20776                 if (rack_mod_inited) {
20777                         uma_zdestroy(rack_zone);
20778                         uma_zdestroy(rack_pcb_zone);
20779                         sysctl_ctx_free(&rack_sysctl_ctx);
20780                         rack_counter_destroy();
20781                         rack_mod_inited = false;
20782                 }
20783                 tcp_lro_dereg_mbufq();
20784                 err = 0;
20785                 break;
20786         default:
20787                 return (EOPNOTSUPP);
20788         }
20789         return (err);
20790 }
20791
20792 static moduledata_t tcp_rack = {
20793         .name = __XSTRING(MODNAME),
20794         .evhand = tcp_addrack,
20795         .priv = 0
20796 };
20797
20798 MODULE_VERSION(MODNAME, 1);
20799 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
20800 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);