2 * Copyright (c) 2016-2020 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
38 #include <sys/module.h>
39 #include <sys/kernel.h>
41 #include <sys/hhook.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.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>
54 #include <sys/qmath.h>
56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
60 #include <sys/refcount.h>
61 #include <sys/queue.h>
62 #include <sys/tim_filter.h>
64 #include <sys/kthread.h>
65 #include <sys/kern_prefetch.h>
66 #include <sys/protosw.h>
68 #include <sys/sched.h>
69 #include <machine/cpu.h>
73 #include <net/route.h>
74 #include <net/route/nhop.h>
77 #define TCPSTATES /* for logging */
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>
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>
108 #include <netinet/tcp_debug.h>
109 #endif /* TCPDEBUG */
111 #include <netinet/tcp_offload.h>
114 #include <netinet6/tcp6_var.h>
117 #include <netipsec/ipsec_support.h>
119 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
120 #include <netipsec/ipsec.h>
121 #include <netipsec/ipsec6.h>
124 #include <netinet/udp.h>
125 #include <netinet/udp_var.h>
126 #include <machine/in_cksum.h>
129 #include <security/mac/mac_framework.h>
131 #include "sack_filter.h"
132 #include "tcp_rack.h"
133 #include "rack_bbr_common.h"
135 uma_zone_t rack_zone;
136 uma_zone_t rack_pcb_zone;
139 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t)))
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)
148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb", "TCP fast send block");
149 MALLOC_DEFINE(M_TCPDO, "tcp_do", "TCP deferred options");
151 struct sysctl_ctx_list rack_sysctl_ctx;
152 struct sysctl_oid *rack_sysctl_root;
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
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.
185 * TCP output is also over-written with a new version since it
186 * must maintain the new rack scoreboard.
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
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;
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;
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 */
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.
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;
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;
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 */
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 */
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 */
299 static int32_t rack_max_per_above = 30; /* When we go to increment stop if above 100+this% */
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;
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;
347 counter_u64_t rack_adjust_map_bw;
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;
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;
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;
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;
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;
404 /* Temp CPU counters */
405 counter_u64_t rack_find_high;
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];
412 #define RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
414 #define RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do { \
415 (tv) = (value) + slop; \
416 if ((u_long)(tv) < (u_long)(tvmin)) \
418 if ((u_long)(tv) > (u_long)(tvmax)) \
423 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line);
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);
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);
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,
439 static struct rack_sendmap *
440 rack_check_recovery_mode(struct tcpcb *tp,
443 rack_cong_signal(struct tcpcb *tp,
444 uint32_t type, uint32_t ack);
445 static void rack_counter_destroy(void);
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);
451 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
453 rack_do_segment(struct mbuf *m, struct tcphdr *th,
454 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
456 static void rack_dtor(void *mem, int32_t size, void *arg);
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);
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);
475 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
476 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
478 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
479 tcp_seq th_ack, int line, uint8_t quality);
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);
486 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
487 struct tcphdr *th, int entered_rec, int dup_ack_struck);
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);
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);
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);
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);
511 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
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);
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);
520 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
521 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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,
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);
568 tcp_rack_partialack(struct tcpcb *tp);
570 rack_set_profile(struct tcp_rack *rack, int prof);
572 rack_apply_deferred_options(struct tcp_rack *rack);
574 int32_t rack_clear_counter=0;
577 rack_set_cc_pacing(struct tcp_rack *rack)
580 struct cc_newreno_opts opt;
581 struct newreno old, *ptr;
585 if (rack->rc_pacing_cc_set)
589 if (tp->cc_algo == NULL) {
591 printf("No cc algorithm?\n");
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! */
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);
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;
611 old.beta = ptr->beta;
612 old.beta_ecn = ptr->beta_ecn;
613 old.newreno_flags = ptr->newreno_flags;
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);
621 printf("Error returned by ctl_output %d\n", error);
625 * Hack alert we need to set in our newreno_flags
626 * so that Abe behavior is also applied.
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);
633 printf("Error returned by ctl_output %d\n", error);
636 /* Save off the original values for restoral */
637 memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
639 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
640 union tcp_log_stackspecific log;
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);
647 log.u_bbr.flex1 = ptr->beta;
648 log.u_bbr.flex2 = ptr->beta_ecn;
649 log.u_bbr.flex3 = ptr->newreno_flags;
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;
661 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
662 0, &log, false, NULL, NULL, 0, &tv);
667 rack_undo_cc_pacing(struct tcp_rack *rack)
669 struct newreno old, *ptr;
672 if (rack->rc_pacing_cc_set == 0)
675 rack->rc_pacing_cc_set = 0;
676 if (tp->cc_algo == NULL)
679 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) {
680 /* Not new-reno nothing to do! */
683 ptr = ((struct newreno *)tp->ccv->cc_data);
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.
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;
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;
719 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
720 0, &log, false, NULL, NULL, 0, &tv);
724 #ifdef NETFLIX_PEAKRATE
726 rack_update_peakrate_thr(struct tcpcb *tp)
728 /* Keep in mind that t_maxpeakrate is in B/s. */
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);
737 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
743 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
744 if (error || req->newptr == NULL)
747 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
752 printf("Clearing RACK counters\n");
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);
779 counter_u64_zero(rack_adjust_map_bw);
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]);
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);
824 rack_clear_counter = 0;
829 rack_init_sysctls(void)
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;
844 rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
845 SYSCTL_CHILDREN(rack_sysctl_root),
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),
854 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
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),
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,
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,
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),
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),
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),
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,
1208 "Bottom dragging if not these many segments outstanding and room");
1210 /* TLP and Rack related parameters */
1211 rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1212 SYSCTL_CHILDREN(rack_sysctl_root),
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,
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,
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");
1288 /* Timer related controls */
1289 rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1290 SYSCTL_CHILDREN(rack_sysctl_root),
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,
1324 "Minimum rack timeout in microseconds");
1325 /* Measure controls */
1326 rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1327 SYSCTL_CHILDREN(rack_sysctl_root),
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,
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,
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");
1368 rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1369 SYSCTL_CHILDREN(rack_sysctl_root),
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,
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,
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),
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?");
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,
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,
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,
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,
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,
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");
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");
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);
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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");
1764 for (i = 0; i < MAX_NUM_OF_CNTS; i++) {
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");
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,
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,
1785 "Number of TCP connections with small mbuf's for compressed acks");
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");
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");
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,
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,
1885 "Total number of scwnd attempts");
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");
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");
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");
1920 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a)
1922 if (SEQ_GEQ(b->r_start, a->r_start) &&
1923 SEQ_LT(b->r_start, a->r_end)) {
1925 * The entry b is within the
1927 * a -- |-------------|
1932 * b -- |-----------|
1935 } else if (SEQ_GEQ(b->r_start, a->r_end)) {
1937 * b falls as either the next
1938 * sequence block after a so a
1939 * is said to be smaller than b.
1949 * Whats left is where a is
1950 * larger than b. i.e:
1954 * b -- |--------------|
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);
1963 rc_init_window(struct tcp_rack *rack)
1967 if (rack->rc_init_win == 0) {
1969 * Nothing set by the user, use the system stack
1972 return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1974 win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1979 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
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);
1986 return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1990 rack_get_bw(struct tcp_rack *rack)
1992 if (rack->use_fixed_rate) {
1993 /* Return the fixed pacing rate */
1994 return (rack_get_fixed_pacing_bw(rack));
1996 if (rack->r_ctl.gp_bw == 0) {
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.
2009 if (rack->r_ctl.init_rate)
2010 return (rack->r_ctl.init_rate);
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);
2017 /* Ok lets come up with the IW guess, if we have a srtt */
2018 if (rack->rc_tp->t_srtt == 0) {
2020 * Go with old pacing method
2021 * i.e. burst mitigation only.
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;
2030 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2031 bw = rack->r_ctl.bw_rate_cap;
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;
2040 /* Still doing initial average must calculate */
2041 bw = rack->r_ctl.gp_bw / rack->r_ctl.num_measurements;
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.
2049 return (rack->rc_tp->t_maxpeakrate);
2052 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap))
2053 bw = rack->r_ctl.bw_rate_cap;
2059 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2061 if (rack->use_fixed_rate) {
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)) {
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 */
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).
2083 return (rack->r_ctl.rack_per_of_gp_rec);
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);
2091 return (rack->r_ctl.rack_per_of_gp_ca);
2095 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
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.
2106 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2107 union tcp_log_stackspecific log;
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);
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)
2137 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2138 union tcp_log_stackspecific log;
2140 const struct ifnet *ifp;
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;
2153 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2154 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
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;
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);
2181 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2184 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2186 uint64_t bw_est, high_rate;
2189 gain = (uint64_t)rack_get_output_gain(rack, rsm);
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__,
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)) {
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.
2221 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2223 /* Yep, we have a rate is it above this rate? */
2224 if (bw_est > high_rate) {
2236 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2238 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2239 union tcp_log_stackspecific log;
2242 if ((mod != 1) && (rack_verbose_logging == 0)) {
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.
2249 * The clear/increment are only logged
2250 * if you have BBverbose on.
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);
2278 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2280 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2281 union tcp_log_stackspecific log;
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;
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);
2315 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2317 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2318 union tcp_log_stackspecific log;
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;
2328 log.u_bbr.flex3 = 0;
2330 log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2331 if (rack->rack_no_prr)
2332 log.u_bbr.flex5 = 0;
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,
2344 0, &log, false, &tv);
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)
2355 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2356 union tcp_log_stackspecific log;
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;
2368 log.u_bbr.flex1 = prev->r_start;
2369 log.u_bbr.flex2 = prev->r_end;
2370 log.u_bbr.flex7 |= 0x4;
2373 log.u_bbr.flex3 = rsm->r_start;
2374 log.u_bbr.flex4 = rsm->r_end;
2375 log.u_bbr.flex7 |= 0x2;
2378 log.u_bbr.flex5 = next->r_start;
2379 log.u_bbr.flex6 = next->r_end;
2380 log.u_bbr.flex7 |= 0x1;
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)
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,
2394 0, &log, false, &tv);
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)
2402 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2403 union tcp_log_stackspecific log;
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);
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;
2428 log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2430 log.u_bbr.cwnd_gain = 0;
2431 log.u_bbr.pacing_gain = 0;
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;
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,
2462 0, &log, false, &tv);
2469 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2472 * Log the rtt sample we are
2473 * applying to the srtt algorithm in
2476 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2477 union tcp_log_stackspecific log;
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;
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
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,
2518 0, &log, false, &tv);
2523 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2525 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
2526 union tcp_log_stackspecific log;
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,
2541 0, &log, false, &tv);
2548 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line)
2550 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
2551 union tcp_log_stackspecific log;
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);
2576 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv)
2578 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2579 union tcp_log_stackspecific log;
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;
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,
2600 0, &log, false, tv);
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)
2607 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2608 union tcp_log_stackspecific log;
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;
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);
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)
2648 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
2649 union tcp_log_stackspecific log;
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);
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)
2679 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2680 union tcp_log_stackspecific log;
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;
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,
2705 tlen, &log, false, &tv);
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)
2713 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2714 union tcp_log_stackspecific log;
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;
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);
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)
2751 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2752 union tcp_log_stackspecific log;
2756 /* No you can't use 1, its for the real to cancel */
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);
2778 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
2780 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2781 union tcp_log_stackspecific log;
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;
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);
2808 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd)
2810 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2811 union tcp_log_stackspecific log;
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;
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,
2835 0, &log, false, &tv);
2839 #ifdef NETFLIX_EXP_DETECTION
2841 rack_log_sad(struct tcp_rack *rack, int event)
2843 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2844 union tcp_log_stackspecific log;
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);
2873 rack_counter_destroy(void)
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]);
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);
2944 counter_u64_free(rack_adjust_map_bw);
2946 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
2947 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
2950 static struct rack_sendmap *
2951 rack_alloc(struct tcp_rack *rack)
2953 struct rack_sendmap *rsm;
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.
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--;
2968 * Once we get under our free cache we probably
2969 * no longer have a "hot" one available. Lets
2972 rsm = uma_zalloc(rack_zone, M_NOWAIT);
2974 rack->r_ctl.rc_num_maps_alloced++;
2975 counter_u64_add(rack_to_alloc, 1);
2979 * Dig in to our aux rsm's (the last two) since
2980 * UMA failed to get us one.
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--;
2992 static struct rack_sendmap *
2993 rack_alloc_full_limit(struct tcp_rack *rack)
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);
3005 return (rack_alloc(rack));
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)
3012 struct rack_sendmap *rsm;
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);
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++;
3038 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
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--;
3045 if (rsm->r_limit_type) {
3046 /* currently there is only one limit type */
3047 rack->r_ctl.rc_num_split_allocs--;
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;
3053 /* Follow the next one out */
3054 struct rack_sendmap fe;
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);
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++;
3076 rack_free_trim(struct tcp_rack *rack)
3078 struct rack_sendmap *rsm;
3081 * Free up all the tail entries until
3082 * we get our list down to the limit.
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);
3094 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3096 uint64_t srtt, bw, len, tim;
3097 uint32_t segsiz, def_len, minl;
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) {
3103 * We have no measurement (IW is in flight?) so
3104 * we can only guess using our data_window sysctl
3105 * value (usually 20MSS).
3110 * Now we have a number of factors to consider.
3112 * 1) We have a desired BDP which is usually
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.
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
3133 * So lets calculate the BDP with the "known" b/w using
3134 * the SRTT has our rtt and then multiply it by the
3137 bw = rack_get_bw(rack);
3138 srtt = (uint64_t)tp->t_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;
3150 minl = roundup(minl, segsiz);
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.
3161 * We basically include 2 over our "MIN window" so
3162 * that the measurement can be shortened (possibly) by
3166 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3168 return (max((uint32_t)len, def_len));
3173 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3175 uint32_t tim, srtts, segsiz;
3178 * Has enough time passed for the GP measurement to be valid?
3180 if ((tp->snd_max == tp->snd_una) ||
3181 (th_ack == tp->snd_max)){
3183 *quality = RACK_QUALITY_ALLACKED;
3186 if (SEQ_LT(th_ack, tp->gput_seq)) {
3187 /* Not enough bytes yet */
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 */
3196 if (rack->r_ctl.rc_first_appl &&
3197 (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3199 * We are up to the app limited send point
3200 * we have to measure irrespective of the time..
3202 *quality = RACK_QUALITY_APPLIMITED;
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;
3209 *quality = RACK_QUALITY_HIGH;
3212 /* Nope not even a full SRTT has passed */
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)
3221 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3222 union tcp_log_stackspecific log;
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,
3262 0, &log, false, &tv);
3267 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
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)
3274 * If we already are pacing at a fast enough
3275 * rate to push us faster there is no sense of
3278 * We first caculate our actual pacing rate (ss or ca multipler
3279 * times our cur_bw).
3281 * Then we take the last measured rate and multipy by our
3282 * maximum pacing overage to give us a max allowable rate.
3284 * If our act_rate is smaller than our max_allowable rate
3285 * then we should increase. Else we should hold steady.
3288 uint64_t act_rate, max_allow_rate;
3290 if (rack_timely_no_stopping)
3293 if ((cur_bw == 0) || (last_bw_est == 0)) {
3295 * Initial startup case or
3296 * everything is acked case.
3298 rack_log_timely(rack, mult, cur_bw, 0, 0,
3304 * We can always pace at or slightly above our rate.
3306 rack_log_timely(rack, mult, cur_bw, 0, 0,
3310 act_rate = cur_bw * (uint64_t)mult;
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) {
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).
3321 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
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.
3333 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
3340 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
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.
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;
3351 if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3352 rack->r_ctl.rack_per_of_gp_ca = 100;
3354 if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3355 rack->r_ctl.rack_per_of_gp_ss = 100;
3360 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3362 if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3363 rack->r_ctl.rack_per_of_gp_ca = 100;
3365 if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3366 rack->r_ctl.rack_per_of_gp_ss = 100;
3371 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3373 int32_t calc, logged, plus;
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.
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)) {
3390 * Reset and get 5 strokes more before the boost. Note
3391 * that the count is 0 based so we have to add one.
3394 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3395 rack->rc_gp_timely_inc_cnt = 0;
3397 plus = (uint32_t)rack_gp_increase_per;
3398 /* Must be at least 1% increase for true timely increases */
3400 ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
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;
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;
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)) {
3422 calc = rack->r_ctl.rack_per_of_gp_ca + plus;
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;
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)) {
3436 calc = rack->r_ctl.rack_per_of_gp_ss + plus;
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;
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;
3452 if (rack->rc_gp_incr &&
3454 (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3455 rack->rc_gp_timely_inc_cnt++;
3457 rack_log_timely(rack, logged, plus, 0, 0,
3462 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3465 * norm_grad = rtt_diff / minrtt;
3466 * new_per = curper * (1 - B * norm_grad)
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
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)) /
3482 if (perf > curper) {
3486 return ((uint32_t)perf);
3490 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3494 * result = curper * (1 - (B * ( 1 - ------ ))
3497 * B = rack_gp_decrease_per (default 10%)
3498 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3501 uint32_t highrttthresh;
3503 highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
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);
3513 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3515 uint64_t logvar, logvar2, logvar3;
3516 uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
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;
3523 ss_red = ca_red = rec_red = 0;
3525 /* Calculate the reduction value */
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);
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;
3545 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
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;
3552 if (rack->rc_gp_saw_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);
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;
3568 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3572 logvar2 = (uint32_t)rtt;
3574 logvar2 |= (uint32_t)rtt_diff;
3575 logvar3 = rack_gp_rtt_maxmul;
3577 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3578 rack_log_timely(rack, timely_says,
3580 logvar, __LINE__, 10);
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;
3585 } else if (rack->rc_gp_saw_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);
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;
3600 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3605 logvar2 = (uint32_t)rtt;
3607 logvar2 |= (uint32_t)rtt_diff;
3608 logvar3 = rack_gp_rtt_maxmul;
3610 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3611 rack_log_timely(rack, timely_says,
3613 logvar, __LINE__, 10);
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;
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;
3628 rack_log_timely(rack, logged, rec_red, rack_per_lower_bound, logvar,
3633 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
3634 uint32_t rtt, uint32_t line, uint8_t reas)
3636 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
3637 union tcp_log_stackspecific log;
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);
3684 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
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)) {
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).
3698 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
3703 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
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.
3719 if (rack->rc_gp_dyn_mul == 0)
3722 if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
3726 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
3727 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
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
3735 rack_do_goodput_measurement(rack->rc_tp, rack,
3736 rack->rc_tp->snd_una, __LINE__,
3737 RACK_QUALITY_PROBERTT);
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));
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);
3757 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
3759 struct rack_sendmap *rsm;
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)) {
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
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) {
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.
3784 rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
3787 * Measurements through the current snd_max are going
3788 * to be limited by the slower pacing rate.
3790 * We need to mark these as app-limited so we
3791 * don't collapse the b/w.
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;
3799 * Go out to the end app limited and mark
3800 * this new one as next and move the end_appl up
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;
3807 rsm->r_flags |= RACK_APP_LIMITED;
3808 rack->r_ctl.rc_app_limited_cnt++;
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).
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;
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;
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;
3835 * Lets set rtt_diff to 0, so that we will get a "boost"
3838 rack->r_ctl.rc_rtt_diff = 0;
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;
3844 * If configured to, set the cwnd and ssthresh to
3847 if (rack_probe_rtt_sets_cwnd) {
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);
3868 sum = rack->r_ctl.rc_entry_gp_rtt;
3870 sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
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.
3878 setval = rack->r_ctl.rc_entry_gp_rtt;
3879 } else if (sum >= 15) {
3881 * Lets take the smaller of the
3882 * two since we are just somewhat
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;
3890 * Here we are not highly buffered
3891 * and should pick the min we can to
3892 * keep from causing loss.
3894 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3896 rack_set_prtt_target(rack, segsiz,
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;
3904 setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
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)) {
3910 rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
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);
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;
3926 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
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 */
3933 if (rack->in_probe_rtt) {
3934 uint64_t no_overflow;
3935 uint32_t endtime, must_stay;
3937 if (rack->r_ctl.rc_went_idle_time &&
3938 ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
3940 * We went idle during prtt, just exit now.
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)) {
3947 * Probe RTT safety value triggered!
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);
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)) {
3963 /* Do we lower more? */
3965 if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
3966 calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
3969 calc /= max(rack->r_ctl.rc_gp_srtt, 1);
3972 calc *= rack_per_of_gp_probertt_reduce;
3973 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
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;
3978 /* We must reach target or the time set */
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 */
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;
3999 * Setup our end time, some number of gp_srtts plus 200ms.
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);
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);
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);
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)
4024 uint64_t cur_bw, up_bnd, low_bnd, subfr;
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 */
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;
4039 up_bnd += rack->r_ctl.last_gp_comp_bw;
4041 subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4043 low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4044 if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
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.
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.
4055 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
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) {
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).
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.
4074 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
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;
4082 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) ||
4083 (timely_says == 0)) {
4085 * Push another time with a faster pacing
4086 * to try to gain back (we include override to
4087 * get a full raise factor).
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)) {
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.
4098 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4100 /* Log it stays the same */
4101 rack_log_timely(rack, 0, last_bw_est, low_bnd, 0,
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;
4110 * Lets just use the RTT
4111 * information and give up
4116 } else if ((timely_says != 2) &&
4118 (last_bw_est > up_bnd)) {
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).
4125 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
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)) {
4132 * In cases where we can't go higher
4133 * we should just use timely.
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)) {
4141 * In cases where we can't go higher
4142 * we should just use timely.
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);
4152 /* Log it stays the same */
4153 rack_log_timely(rack, 0, last_bw_est, up_bnd, 0,
4159 * We are staying between the lower and upper range bounds
4160 * so use timely to decide.
4162 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
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) &&
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;
4178 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
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);
4188 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4190 int32_t timely_says;
4191 uint64_t log_mult, log_rtt_a_diff;
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 */
4200 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
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));
4214 log_mult |= prev_rtt;
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);
4221 * Use a gradient to find it the timely gradient
4223 * grad = rc_rtt_diff / min_rtt;
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
4232 log_mult = prev_rtt;
4233 if (rtt_diff <= 0) {
4235 * Rttdiff is less than zero, increase the
4236 * b/w multipler (its 0 or negative)
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);
4242 /* Reduce the b/w multipler */
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);
4248 return (timely_says);
4252 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4253 tcp_seq th_ack, int line, uint8_t quality)
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;
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;
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;
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
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)) {
4288 * Invalid measurement time, maybe
4289 * all on one ack/one send?
4293 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4294 0, 0, 0, 10, __LINE__, NULL, quality);
4295 goto skip_measurement;
4297 if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4298 /* We never made a us_rtt measurement? */
4301 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4302 0, 0, 0, 10, __LINE__, NULL, quality);
4303 goto skip_measurement;
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.
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).
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.
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 */
4338 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4339 0, 0, 0, 10, __LINE__, NULL, quality);
4340 goto skip_measurement;
4342 bytes = (th_ack - tp->gput_seq);
4343 bytes_ps = (uint64_t)bytes;
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.
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
4353 if (rack->rc_gp_filled == 0) {
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.
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;
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;
4375 * We now need to calculate the Timely like status so
4376 * we can update (possibly) the b/w multipliers.
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;
4383 if (rack->measure_saw_probe_rtt == 0) {
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.
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);
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
4399 bytes_ps *= HPTS_USEC_IN_SEC;
4401 if (bytes_ps > rack->r_ctl.last_max_bw) {
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).
4408 * Another option here would be to
4409 * instead skip the measurement.
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;
4416 /* We store gp for b/w in bytes per second */
4417 if (rack->rc_gp_filled == 0) {
4418 /* Initial measurment */
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);
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);
4429 if (rack->rc_inp->inp_in_hpts &&
4430 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
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
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;
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;
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.
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.
4467 if (rack->r_ctl.num_measurements < 0xff) {
4468 rack->r_ctl.num_measurements++;
4470 srtt = (uint64_t)tp->t_srtt;
4473 * Strange why did t_srtt go back to zero?
4475 if (rack->r_ctl.rc_rack_min_rtt)
4476 srtt = rack->r_ctl.rc_rack_min_rtt;
4478 srtt = HPTS_USEC_IN_MSEC;
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?
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)) {
4501 * The b/w update takes no more
4502 * away then 1/2 our running total
4505 addpart = bytes_ps * utim;
4506 addpart /= (srtt * 8);
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).
4517 subpart = rack->r_ctl.gp_bw / 2;
4518 addpart = bytes_ps / 2;
4520 resid_bw = rack->r_ctl.gp_bw - subpart;
4521 rack->r_ctl.gp_bw = resid_bw + addpart;
4524 if ((utim / srtt) <= 1) {
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.
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);
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
4543 subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
4544 addpart = bytes_ps / rack_wma_divisor;
4546 if ((rack->measure_saw_probe_rtt == 0) ||
4547 (bytes_ps > rack->r_ctl.gp_bw)) {
4549 * For probe-rtt we only add it in
4550 * if its larger, all others we just
4554 resid_bw = rack->r_ctl.gp_bw - subpart;
4555 rack->r_ctl.gp_bw = resid_bw + addpart;
4559 if ((rack->gp_ready == 0) &&
4560 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
4561 /* We have enough measurements now */
4563 rack_set_cc_pacing(rack);
4564 if (rack->defer_options)
4565 rack_apply_deferred_options(rack);
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;
4581 * We restart our diffs based on the gpsrtt in the
4582 * measurement window.
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;
4592 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
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.
4599 if (tp->t_stats_gput_prev > 0)
4600 stats_voi_update_abs_s32(tp->t_stats,
4602 ((gput - tp->t_stats_gput_prev) * 100) /
4603 tp->t_stats_gput_prev);
4605 tp->t_flags &= ~TF_GPUTINPROG;
4606 tp->t_stats_gput_prev = gput;
4608 * Now are we app limited now and there is space from where we
4609 * were to where we want to go?
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.
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)))) {
4621 * Yep there is enough outstanding to make a measurement here.
4623 struct rack_sendmap *rsm, fe;
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);
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)) {
4643 * We don't have enough to make a measurement.
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);
4651 if (tp->t_state >= TCPS_FIN_WAIT_1) {
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.
4657 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < (tp->gput_ack - tp->gput_seq)) {
4658 /* Nope not enough data. */
4662 tp->t_flags |= TF_GPUTINPROG;
4664 * Now we need to find the timestamp of the send at tp->gput_seq
4665 * for the send based measurement.
4667 fe.r_start = tp->gput_seq;
4668 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
4670 /* Ok send-based limit is set */
4671 if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
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
4678 tp->gput_seq = rsm->r_start;
4680 if (rsm->r_flags & RACK_ACKED)
4681 tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
4683 rack->app_limited_needs_set = 1;
4684 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
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.
4694 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
4696 rack_log_pacing_delay_calc(rack,
4701 rack->r_ctl.rc_app_limited_cnt,
4703 __LINE__, NULL, quality);
4708 * CC wrapper hook functions
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)
4714 uint32_t prior_cwnd, acked;
4715 struct tcp_log_buffer *lgb = NULL;
4716 uint8_t labc_to_use, quality;
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)) {
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;
4730 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
4731 ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
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)) {
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.
4747 rack_update_peakrate_thr(tp);
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;
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;
4765 tp->ccv->flags &= ~CCF_ABC_SENTAWND;
4766 tp->t_bytes_acked = 0;
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;
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;
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);
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);
4799 lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
4801 if (rack->r_must_retran) {
4802 if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
4804 * We now are beyond the rxt point so lets disable
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) {
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.
4816 if (acked <= rack->r_ctl.rc_out_at_rto){
4817 rack->r_ctl.rc_out_at_rto -= acked;
4819 rack->r_ctl.rc_out_at_rto = 0;
4824 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
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;
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;
4840 tcp_rack_partialack(struct tcpcb *tp)
4842 struct tcp_rack *rack;
4844 rack = (struct tcp_rack *)tp->t_fb_ptr;
4845 INP_WLOCK_ASSERT(tp->t_inpcb);
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
4853 if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
4855 rack->r_wanted_output = 1;
4859 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
4861 struct tcp_rack *rack;
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) {
4873 * Rack has burst control and pacing
4874 * so lets not set this any lower than
4875 * snd_ssthresh per RFC-6582 (option 2).
4877 tp->snd_cwnd = tp->snd_ssthresh;
4880 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) {
4881 union tcp_log_stackspecific log;
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);
4898 if ((rack->rack_no_prr == 0) &&
4899 (rack->no_prr_addback == 0) &&
4900 (rack->r_ctl.rc_prr_sndcnt > 0)) {
4902 * Suck the next prr cnt back into cwnd, but
4903 * only do that if we are not application limited.
4905 if (ctf_outstanding(tp) <= sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
4907 * We are allowed to add back to the cwnd the amount we did
4909 * a) no_prr_addback is off.
4910 * b) we are not app limited
4911 * c) we are doing prr
4913 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
4915 tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
4916 rack->r_ctl.rc_prr_sndcnt);
4918 rack->r_ctl.rc_prr_sndcnt = 0;
4919 rack_log_to_prr(rack, 1, 0);
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;
4928 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
4930 EXIT_RECOVERY(tp->t_flags);
4934 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack)
4936 struct tcp_rack *rack;
4937 uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
4939 INP_WLOCK_ASSERT(tp->t_inpcb);
4941 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
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;
4948 in_rec_at_entry = 1;
4949 rack = (struct tcp_rack *)tp->t_fb_ptr;
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);
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;
4968 if (!IN_CONGRECOVERY(tp->t_flags) ||
4970 * Allow ECN reaction on ACK to CWR, if
4971 * that data segment was also CE marked.
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;
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;
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;
5003 if (tp->t_flags & TF_WASCRECOVERY) {
5004 ENTER_CONGRECOVERY(tp->t_flags);
5005 tp->t_flags &= ~TF_WASCRECOVERY;
5007 tp->snd_nxt = tp->snd_max;
5008 tp->t_badrxtwin = 0;
5011 if ((CC_ALGO(tp)->cong_signal != NULL) &&
5013 tp->ccv->curack = ack;
5014 CC_ALGO(tp)->cong_signal(tp->ccv, type);
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;
5027 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5031 INP_WLOCK_ASSERT(tp->t_inpcb);
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);
5038 if (CC_ALGO(tp)->after_idle != NULL)
5039 CC_ALGO(tp)->after_idle(tp->ccv);
5041 if (tp->snd_cwnd == 1)
5042 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */
5044 i_cwnd = rc_init_window(rack);
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
5051 if (tp->snd_cwnd < i_cwnd) {
5052 tp->snd_cwnd = i_cwnd;
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
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)))
5073 static struct rack_sendmap *
5074 rack_find_lowest_rsm(struct tcp_rack *rack)
5076 struct rack_sendmap *rsm;
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.
5083 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5084 if (rsm->r_flags & RACK_ACKED) {
5093 static struct rack_sendmap *
5094 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5096 struct rack_sendmap *prsm;
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).
5103 counter_u64_add(rack_find_high, 1);
5105 RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) {
5106 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5115 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
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:
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.
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
5133 * In the end if lro is non-zero we add the extra time for
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;
5144 * No time as passed since the last
5145 * reorder, mark it as reordering.
5150 /* Negative time? */
5153 if (lro > rack->r_ctl.rc_reorder_fade) {
5154 /* Turn off reordering seen too */
5155 rack->r_ctl.rc_reorder_ts = 0;
5159 /* Reodering does not fade */
5165 if (rack->rc_rack_tmr_std_based == 0) {
5166 thresh = srtt + rack->r_ctl.rc_pkt_delay;
5168 /* Standards based pkt-delay is 1/4 srtt */
5169 thresh = srtt + (srtt >> 2);
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);
5176 thresh += (srtt >> 2);
5178 if (rack->rc_rack_use_dsack &&
5180 (rack->r_ctl.num_dsack > 0)) {
5182 * We only increase the reordering window if we
5183 * have seen reordering <and> we have a DSACK count.
5185 thresh += rack->r_ctl.num_dsack * (srtt >> 2);
5186 rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
5188 /* SRTT * 2 is the ceiling */
5189 if (thresh > (srtt * 2)) {
5192 /* And we don't want it above the RTO max either */
5193 if (thresh > rack_rto_max) {
5194 thresh = rack_rto_max;
5196 rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
5201 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
5202 struct rack_sendmap *rsm, uint32_t srtt)
5204 struct rack_sendmap *prsm;
5205 uint32_t thresh, len;
5210 if (rack->r_ctl.rc_tlp_threshold)
5211 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
5213 thresh = (srtt * 2);
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;
5224 * Compensate for delayed-ack with the d-ack time.
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;
5231 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
5233 prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
5234 if (prsm && (len <= segsiz)) {
5236 * Two packets outstanding, thresh should be (2*srtt) +
5237 * possible inter-packet delay (if any).
5239 uint32_t inter_gap = 0;
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];
5249 thresh += inter_gap;
5250 } else if (len <= segsiz) {
5252 * Possibly compensate for delayed-ack.
5254 uint32_t alt_thresh;
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;
5261 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
5263 if (len <= segsiz) {
5264 uint32_t alt_thresh;
5266 * Compensate for delayed-ack with the d-ack time.
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;
5274 /* Not above an RTO */
5275 if (thresh > tp->t_rxtcur) {
5276 thresh = tp->t_rxtcur;
5278 /* Not above a RTO max */
5279 if (thresh > rack_rto_max) {
5280 thresh = rack_rto_max;
5282 /* Apply user supplied min TLP */
5283 if (thresh < rack_tlp_min) {
5284 thresh = rack_tlp_min;
5290 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
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
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);
5307 static struct rack_sendmap *
5308 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
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
5315 struct tcp_rack *rack;
5316 struct rack_sendmap *rsm;
5318 uint32_t srtt, thresh;
5320 rack = (struct tcp_rack *)tp->t_fb_ptr;
5321 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
5324 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5328 if (rsm->r_flags & RACK_ACKED) {
5329 rsm = rack_find_lowest_rsm(rack);
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]))) {
5339 if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
5342 /* Ok if we reach here we are over-due and this guy can be sent */
5343 if (IN_RECOVERY(tp->t_flags) == 0) {
5345 * For the one that enters us into recovery record undo
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;
5352 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una);
5357 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
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)
5368 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
5369 ret_val = (uint32_t)tt;
5374 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
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.
5382 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
5385 int32_t is_tlp_timer = 0;
5386 struct rack_sendmap *rsm;
5388 if (rack->t_timers_stopped) {
5389 /* All timers have been stopped none are to run */
5392 if (rack->rc_in_persist) {
5393 /* We can't start any timer in persists */
5394 return (rack_get_persists_timer_val(tp, rack));
5396 rack->rc_on_min_to = 0;
5397 if ((tp->t_state < TCPS_ESTABLISHED) ||
5398 ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
5401 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5402 if ((rsm == NULL) || sup_rack) {
5403 /* Nothing on the send map or no rack */
5405 time_since_sent = 0;
5406 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5409 * Should we discount the RTX timer any?
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).
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;
5422 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
5423 if (TSTMP_GT(cts, tstmp_touse))
5424 time_since_sent = cts - tstmp_touse;
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;
5429 if (to > time_since_sent)
5430 to -= time_since_sent;
5432 to = rack->r_ctl.rc_min_to;
5435 /* Special case for KEEPINIT */
5436 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
5437 (TP_KEEPINIT(tp) != 0) &&
5440 * We have to put a ceiling on the rxt timer
5441 * of the keep-init timeout.
5443 uint32_t max_time, red;
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]);
5453 /* Reduce timeout to the keep value if needed */
5461 if (rsm->r_flags & RACK_ACKED) {
5462 rsm = rack_find_lowest_rsm(rack);
5468 if (rack->sack_attack_disable) {
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.
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)) {
5484 * We don't start a rack timer if all we have is a
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))) {
5494 * We are not cheating, in recovery and
5495 * not enough ack's to yet get our next
5496 * retransmission out.
5498 * Note that classified attackers do not
5499 * get to use the rack-cheat.
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)) {
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;
5515 to = rack->r_ctl.rc_min_to;
5516 if (rack->r_rr_config == 3)
5517 rack->rc_on_min_to = 1;
5520 /* Ok we need to do a TLP not RACK */
5522 if ((rack->rc_tlp_in_progress != 0) &&
5523 (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
5525 * The previous send was a TLP and we have sent
5526 * N TLP's without sending new data.
5530 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
5532 /* We found no rsm to TLP with. */
5535 if (rsm->r_flags & RACK_HAS_FIN) {
5536 /* If its a FIN we dont do TLP */
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];
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;
5550 if ((rack->rc_srtt_measure_made == 0) &&
5551 (tp->t_srtt == 1)) {
5553 * If another stack as run and set srtt to 1,
5554 * then the srtt was 0, so lets use the initial.
5556 srtt = RACK_INITIAL_RTO;
5558 srtt_cur = tp->t_srtt;
5562 srtt = RACK_INITIAL_RTO;
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.
5568 if (rack_tlp_use_greater &&
5570 (srtt < rack_grab_rtt(tp, rack))) {
5571 srtt = rack_grab_rtt(tp, rack);
5573 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
5574 if (thresh > time_since_sent) {
5575 to = thresh - time_since_sent;
5577 to = rack->r_ctl.rc_min_to;
5578 rack_log_alt_to_to_cancel(rack,
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],
5587 if (to < rack_tlp_min) {
5590 if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
5592 * If the TLP time works out to larger than the max
5593 * RTO lets not do TLP.. just RTO.
5598 if (is_tlp_timer == 0) {
5599 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
5601 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
5609 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5611 if (rack->rc_in_persist == 0) {
5612 if (tp->t_flags & TF_GPUTINPROG) {
5614 * Stop the goodput now, the calling of the
5615 * measurement function clears the flag.
5617 rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
5618 RACK_QUALITY_PERSIST);
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;
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__);
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;
5638 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
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;
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;
5650 if (rack->rc_gp_dyn_mul &&
5651 (rack->use_fixed_rate == 0) &&
5652 (rack->rc_always_pace)) {
5654 * Do we count this as if a probe-rtt just
5657 uint32_t time_idle, idle_min;
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) {
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;
5668 if (time_idle >= idle_min) {
5669 /* Yes, we count it as a probe-rtt. */
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;
5679 rack_exit_probertt(rack, us_cts);
5683 rack->rc_in_persist = 0;
5684 rack->r_ctl.rc_went_idle_time = 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;
5691 rack->r_ctl.rc_agg_early = 0;
5695 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
5696 struct hpts_diag *diag, struct timeval *tv)
5698 if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5699 union tcp_log_stackspecific log;
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);
5734 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
5736 if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
5737 union tcp_log_stackspecific log;
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,
5750 len, &log, false, &tv);
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)
5758 struct hpts_diag diag;
5761 uint32_t delayed_ack = 0;
5762 uint32_t hpts_timeout;
5763 uint32_t entry_slot = slot;
5769 if ((tp->t_state == TCPS_CLOSED) ||
5770 (tp->t_state == TCPS_LISTEN)) {
5773 if (inp->inp_in_hpts) {
5774 /* Already on the pacer */
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;
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)) {
5788 * We have a early carry over set,
5789 * we can always add more time so we
5790 * can always make this compensation.
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).
5796 slot += rack->r_ctl.rc_agg_early;
5798 rack->r_ctl.rc_agg_early = 0;
5802 * This is harder, we can
5803 * compensate some but it
5804 * really depends on what
5805 * the current pacing time is.
5807 if (rack->r_ctl.rc_agg_delayed >= slot) {
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).
5814 if (slot <= HPTS_TICKS_PER_SLOT) {
5816 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
5817 slot = HPTS_TICKS_PER_SLOT;
5819 /* We take off some */
5820 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
5821 slot = HPTS_TICKS_PER_SLOT;
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;
5831 if (rack->r_ctl.rc_agg_delayed == 0)
5836 /* We are pacing too */
5837 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
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)) {
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.
5853 slot = tcp_sad_pacing_interval;
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;
5860 if (delayed_ack && ((hpts_timeout == 0) ||
5861 (delayed_ack < hpts_timeout)))
5862 hpts_timeout = delayed_ack;
5864 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
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.
5869 if ((hpts_timeout == 0) &&
5871 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
5872 (tp->t_state <= TCPS_CLOSING)) {
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.
5878 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
5879 /* Get the established keep-alive time */
5880 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
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().
5889 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
5891 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
5892 if (rack->in_probe_rtt) {
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
5900 hpts_timeout = rack_min_probertt_hold;
5904 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
5905 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
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.
5914 if (left < hpts_timeout)
5915 hpts_timeout = left;
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
5923 if (hpts_timeout > 0x7ffffffe)
5924 hpts_timeout = 0x7ffffffe;
5925 rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
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))) {
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.
5940 slot = hpts_timeout;
5942 rack->r_ctl.last_pacing_time = slot;
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).
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.
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.
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.
5969 * Other cases should usually have none of the flags set
5970 * so LRO can call into us.
5972 inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
5974 rack->r_ctl.rc_last_output_to = us_cts + slot;
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.
5983 inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
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).
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) {
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
5999 inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
6001 if ((rack->use_rack_rr) &&
6002 (rack->r_rr_config < 2) &&
6003 ((hpts_timeout) && (hpts_timeout < slot))) {
6005 * Arrange for the hpts to kick back in after the
6006 * t-o if the t-o does not cause a send.
6008 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6010 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6011 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6013 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
6015 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6016 rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6018 } else if (hpts_timeout) {
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.
6027 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout),
6029 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6030 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6032 /* No timer starting */
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);
6040 rack->rc_tmr_stopped = 0;
6042 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv);
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?).
6053 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
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
6062 struct rack_sendmap *rsm;
6064 if (tp->t_timers->tt_flags & TT_STOPPED) {
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);
6074 rack->r_ctl.rc_resend = rsm;
6075 rack->r_timer_override = 1;
6076 if (rack->use_rack_rr) {
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).
6085 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6088 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6090 /* restart a timer and return 1 */
6091 rack_start_hpts_timer(rack, tp, cts,
6099 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6101 if (rsm->m->m_len > rsm->orig_m_len) {
6103 * Mbuf grew, caused by sbcompress, our offset does
6106 rsm->orig_m_len = rsm->m->m_len;
6107 } else if (rsm->m->m_len < rsm->orig_m_len) {
6109 * Mbuf shrank, trimmed off the top by an ack, our
6112 rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
6113 rsm->orig_m_len = rsm->m->m_len;
6118 rack_setup_offset_for_rsm(struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
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);
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 */
6133 KASSERT((m != NULL),
6134 ("rsm:%p nrsm:%p hit at soff:%u null m",
6135 src_rsm, rsm, soff));
6139 rsm->orig_m_len = m->m_len;
6142 static __inline void
6143 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
6144 struct rack_sendmap *rsm, uint32_t start)
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];
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;
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
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));
6183 rack_setup_offset_for_rsm(rsm, nrsm);
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)
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).
6201 struct rack_sendmap *rm;
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;
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)) {
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.
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;
6235 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
6238 panic("removing head in rack:%p rsm:%p rm:%p",
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;
6247 rack_free(rack, r_rsm);
6252 * TLP Timer, here we simply setup what segment we want to
6253 * have the TLP expire on, the normal rack_output() will then
6256 * We return 1, saying don't proceed with rack_output only
6257 * when all timers have been stopped (destroyed PCB?).
6260 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
6265 struct rack_sendmap *rsm = NULL;
6266 struct rack_sendmap *insret;
6269 uint32_t out, avail;
6270 int collapsed_win = 0;
6272 if (tp->t_timers->tt_flags & TT_STOPPED) {
6275 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
6276 /* Its not time yet */
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);
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.
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 */
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;
6307 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
6309 if ((tp->t_flags & TF_GPUTINPROG) &&
6310 (rack->r_ctl.rc_tlp_cnt_out == 1)) {
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.
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*/,
6323 0, 0, 18, __LINE__, NULL, 0);
6326 * Check our send oldest always settings, and if
6327 * there is an oldest to send jump to the need_retran.
6329 if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
6333 /* New data is available */
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 */
6341 } else if (amm < ctf_fixed_maxseg(tp)) {
6342 /* not enough to fill a MTU */
6345 if (IN_FASTRECOVERY(tp->t_flags)) {
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);
6356 /* Set the send-new override */
6357 if (out + amm <= tp->snd_wnd)
6358 rack->r_ctl.rc_tlp_new_data = amm;
6362 rack->r_ctl.rc_tlpsend = NULL;
6363 counter_u64_add(rack_tlp_newdata, 1);
6368 * Ok we need to arrange the last un-acked segment to be re-sent, or
6369 * optionally the first un-acked segment.
6371 if (collapsed_win == 0) {
6372 if (rack_always_send_oldest)
6373 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
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);
6381 counter_u64_add(rack_tlp_does_nada, 1);
6383 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6389 * We must find the last segment
6390 * that was acceptable by the client.
6392 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6393 if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) {
6399 /* None? if so send the first */
6400 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
6402 counter_u64_add(rack_tlp_does_nada, 1);
6404 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
6410 if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
6412 * We need to split this the last segment in two.
6414 struct rack_sendmap *nrsm;
6416 nrsm = rack_alloc_full_limit(rack);
6419 * No memory to split, we will just exit and punt
6420 * off to the RXT timer.
6422 counter_u64_add(rack_tlp_does_nada, 1);
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);
6430 if (insret != NULL) {
6431 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6432 nrsm, insret, rack, rsm);
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;
6441 rack->r_ctl.rc_tlpsend = rsm;
6443 /* Make sure output path knows we are doing a TLP */
6445 rack->r_timer_override = 1;
6446 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
6449 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
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.
6458 * We only return 1, saying don't proceed, if all timers
6459 * are stopped (destroyed PCB?).
6462 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6464 if (tp->t_timers->tt_flags & TT_STOPPED) {
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;
6476 * Persists timer, here we simply send the
6477 * same thing as a keepalive will.
6478 * the one byte send.
6480 * We only return 1, saying don't proceed, if all timers
6481 * are stopped (destroyed PCB?).
6484 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6486 struct tcptemp *t_template;
6492 if (tp->t_timers->tt_flags & TT_STOPPED) {
6495 if (rack->rc_in_persist == 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);
6503 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
6505 * Persistence timer into zero window. Force a byte to be output, if
6508 KMOD_TCPSTAT_INC(tcps_persisttimeo);
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.
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);
6520 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
6521 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
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;
6529 * If the user has closed the socket then drop a persisting
6530 * connection after a much reduced timeout.
6532 if (tp->t_state > TCPS_CLOSE_WAIT &&
6533 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
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);
6540 t_template = tcpip_maketemplate(rack->rc_inp);
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);
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);
6555 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
6558 rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
6559 rack_start_hpts_timer(rack, tp, cts,
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.
6571 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6573 struct tcptemp *t_template;
6576 if (tp->t_timers->tt_flags & TT_STOPPED) {
6579 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
6581 rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
6583 * Keep-alive timer went off; send something or drop connection if
6584 * idle for too long.
6586 KMOD_TCPSTAT_INC(tcps_keeptimeo);
6587 if (tp->t_state < TCPS_ESTABLISHED)
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))
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
6603 KMOD_TCPSTAT_INC(tcps_keepprobe);
6604 t_template = tcpip_maketemplate(inp);
6606 if (rack->forced_ack == 0) {
6607 rack->forced_ack = 1;
6608 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
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);
6616 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
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);
6626 * Retransmit helper function, clear up all the ack
6627 * flags and take care of important book keeping.
6630 rack_remxt_tmr(struct tcpcb *tp)
6633 * The retransmit timer went off, all sack'd blocks must be
6636 struct rack_sendmap *rsm, *trsm = NULL;
6637 struct tcp_rack *rack;
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);
6645 * Ideally we would like to be able to
6646 * mark SACK-PASS on anything not acked here.
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.
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.
6660 TAILQ_INIT(&rack->r_ctl.rc_tmap);
6661 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6663 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
6664 /* We must re-add it back to the tlist */
6666 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6668 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
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);
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;
6682 rack->r_ctl.rc_agg_early = 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)
6695 ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
6697 * For non-sack customers new data
6698 * needs to go out as retransmits until
6699 * we retransmit up to snd_max.
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);
6705 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
6709 rack_convert_rtts(struct tcpcb *tp)
6711 if (tp->t_srtt > 1) {
6714 val = tp->t_srtt >> TCP_RTT_SHIFT;
6715 frac = tp->t_srtt & 0x1f;
6716 tp->t_srtt = TICKS_2_USEC(val);
6718 * frac is the fractional part of the srtt (if any)
6719 * but its in ticks and every bit represents
6724 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6726 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6734 val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
6735 frac = tp->t_rttvar & 0x1f;
6736 tp->t_rttvar = TICKS_2_USEC(val);
6738 * frac is the fractional part of the srtt (if any)
6739 * but its in ticks and every bit represents
6744 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
6746 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
6748 tp->t_rttvar += frac;
6751 tp->t_rxtcur = RACK_REXMTVAL(tp);
6752 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6753 tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
6755 if (tp->t_rxtcur > rack_rto_max) {
6756 tp->t_rxtcur = rack_rto_max;
6761 rack_cc_conn_init(struct tcpcb *tp)
6763 struct tcp_rack *rack;
6766 rack = (struct tcp_rack *)tp->t_fb_ptr;
6770 * Now convert to rack's internal format,
6773 if ((srtt == 0) && (tp->t_srtt != 0))
6774 rack_convert_rtts(tp);
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.
6781 if (tp->snd_ssthresh < tp->snd_wnd) {
6782 tp->snd_ssthresh = tp->snd_wnd;
6785 * We also want to assure a IW worth of
6786 * data can get inflight.
6788 if (rc_init_window(rack) < tp->snd_cwnd)
6789 tp->snd_cwnd = rc_init_window(rack);
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.
6797 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6805 if (tp->t_timers->tt_flags & TT_STOPPED) {
6808 if ((tp->t_flags & TF_GPUTINPROG) &&
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).
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*/,
6820 0, 0, 18, __LINE__, NULL, 0);
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);
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;
6834 tp->t_flags &= ~TF_WASFRECOVERY;
6835 if (IN_CONGRECOVERY(tp->t_flags))
6836 tp->t_flags |= TF_WASCRECOVERY;
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 */
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;
6849 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
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
6859 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6860 (TP_KEEPINIT(tp) != 0)) {
6861 struct rack_sendmap *rsm;
6863 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
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);
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.
6880 if ((rack->r_ctl.rc_resend == NULL) ||
6881 ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
6883 * If the rwnd collapsed on
6884 * the one we are retransmitting
6885 * it does not count against the
6890 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
6891 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
6893 tp->t_rxtshift = TCP_MAXRXTSHIFT;
6894 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
6896 tcp_set_inp_to_drop(rack->rc_inp,
6897 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
6900 if (tp->t_state == TCPS_SYN_SENT) {
6902 * If the SYN was retransmitted, indicate CWND to be limited
6903 * to 1 segment in cc_conn_init().
6906 } else if (tp->t_rxtshift == 1) {
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
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];
6928 rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
6930 RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
6931 max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
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.
6940 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
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))) {
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.
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)) {
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
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;
6973 * Reduce the MSS to blackhole value or to the
6974 * default in an attempt to retransmit.
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;
6986 * Disable Path MTU Discovery when we switch
6989 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
6990 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
6993 #if defined(INET6) && defined(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);
7002 /* Use the default MSS. */
7003 tp->t_maxseg = V_tcp_mssdflt;
7005 * Disable Path MTU Discovery when we switch
7008 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7009 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
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.
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);
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.
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);
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
7046 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7048 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
7049 in6_losing(tp->t_inpcb);
7052 in_losing(tp->t_inpcb);
7053 tp->t_rttvar += tp->t_srtt;
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;
7060 rack_cong_signal(tp, CC_RTO, tp->snd_una);
7066 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7069 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7071 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7072 (tp->t_flags & TF_GPUTINPROG)) {
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?
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)) {
7086 * There are not enough bytes in the socket
7087 * buffer that have been sent to cover this
7088 * measurement. Cancel it.
7090 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7091 rack->r_ctl.rc_gp_srtt /*flex1*/,
7093 0, 0, 18, __LINE__, NULL, 0);
7094 tp->t_flags &= ~TF_GPUTINPROG;
7100 if (tp->t_state == TCPS_LISTEN) {
7101 /* no timers on listen sockets */
7102 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
7106 if ((timers & PACE_TMR_RACK) &&
7107 rack->rc_on_min_to) {
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).
7115 * If its on a normal rack timer (non-min) then
7116 * we will check if its expired.
7118 goto skip_time_check;
7120 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7123 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
7125 rack_log_to_processing(rack, cts, ret, 0);
7128 if (hpts_calling == 0) {
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.
7136 rack_log_to_processing(rack, cts, ret, 0);
7140 * Ok our timer went off early and we are not paced false
7141 * alarm, go back to sleep.
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);
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);
7170 rack_log_to_processing(rack, cts, ret, timers);
7175 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
7178 uint32_t us_cts, flags_on_entry;
7179 uint8_t hpts_removed = 0;
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);
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);
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)) {
7198 * Canceling timer's when we have no output being
7199 * paced. We also must remove ourselves from the
7202 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
7205 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
7207 if (hpts_removed == 0)
7208 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
7212 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
7218 rack_stopall(struct tcpcb *tp)
7220 struct tcp_rack *rack;
7221 rack = (struct tcp_rack *)tp->t_fb_ptr;
7222 rack->t_timers_stopped = 1;
7227 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
7233 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
7239 rack_stop_all_timers(struct tcpcb *tp)
7241 struct tcp_rack *rack;
7244 * Assure no timers are running.
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;
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);
7258 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
7259 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag)
7262 uint16_t stripped_flags;
7265 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
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;
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);
7275 idx = rsm->r_rtr_cnt - 1;
7276 rsm->r_tim_lastsent[idx] = ts;
7278 * Here we don't add in the len of send, since its already
7279 * in snduna <->snd_max.
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);
7289 if (rsm->r_in_tmap) {
7290 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7293 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
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;
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)
7307 * We (re-)transmitted starting at rsm->r_start for some length
7308 * (possibly less than r_end.
7310 struct rack_sendmap *nrsm, *insret;
7315 c_end = rsm->r_start + len;
7316 if (SEQ_GEQ(c_end, rsm->r_end)) {
7318 * We retransmitted the whole piece or more than the whole
7319 * slopping into the next rsm.
7321 rack_update_rsm(tp, rack, rsm, ts, add_flag);
7322 if (c_end == rsm->r_end) {
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);
7333 /* We don't get out of this block. */
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.
7339 nrsm = rack_alloc_full_limit(rack);
7342 * We can't get memory, so lets not proceed.
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.
7354 rack_clone_rsm(rack, nrsm, rsm, c_end);
7356 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
7357 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7359 if (insret != NULL) {
7360 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7361 nrsm, insret, rack, rsm);
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;
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__);
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)
7381 struct tcp_rack *rack;
7382 struct rack_sendmap *rsm, *nrsm, *insret, fe;
7383 register uint32_t snd_max, snd_una;
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
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.
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
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.
7406 INP_WLOCK_ASSERT(tp->t_inpcb);
7409 * We don't log errors -- we could but snd_max does not
7410 * advance in this case either.
7414 if (th_flags & TH_RST) {
7416 * We don't log resets and we return immediately from
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)) {
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).
7431 if ((th_flags & TH_SYN) && (seq_out == tp->iss))
7433 if (th_flags & TH_FIN)
7435 if (SEQ_LT(snd_max, tp->snd_nxt)) {
7437 * The add/update as not been done for the FIN/SYN
7440 snd_max = tp->snd_nxt;
7443 if (SEQ_LEQ((seq_out + len), snd_una)) {
7444 /* Are sending an old segment to induce an ack (keep-alive)? */
7447 if (SEQ_LT(seq_out, snd_una)) {
7448 /* huh? should we panic? */
7451 end = seq_out + len;
7453 if (SEQ_GEQ(end, seq_out))
7454 len = end - seq_out;
7459 /* We don't log zero window probes */
7462 rack->r_ctl.rc_time_last_sent = cts;
7463 if (IN_FASTRECOVERY(tp->t_flags)) {
7464 rack->r_ctl.rc_prr_out += len;
7466 /* First question is it a retransmission or new? */
7467 if (seq_out == snd_max) {
7470 rsm = rack_alloc(rack);
7473 * Hmm out of memory and the tcb got destroyed while
7478 if (th_flags & TH_FIN) {
7479 rsm->r_flags = RACK_HAS_FIN|add_flag;
7481 rsm->r_flags = add_flag;
7485 rsm->r_tim_lastsent[0] = cts;
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;
7492 rsm->r_start = seq_out;
7493 rsm->r_end = rsm->r_start + len;
7496 * save off the mbuf location that
7497 * sndmbuf_noadv returned (which is
7498 * where we started copying from)..
7503 * Here we do add in the len of send, since its not yet
7504 * reflected in in snduna <->snd_max
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 */
7511 if (rsm->m->m_len <= rsm->soff) {
7513 * XXXrrs Question, will this happen?
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.
7524 while (lm->m_len <= rsm->soff) {
7525 rsm->soff -= lm->m_len;
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));
7531 counter_u64_add(rack_sbsndptr_wrong, 1);
7533 counter_u64_add(rack_sbsndptr_right, 1);
7534 rsm->orig_m_len = rsm->m->m_len;
7536 rsm->orig_m_len = 0;
7537 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
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);
7542 if (insret != NULL) {
7543 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7544 nrsm, insret, rack, rsm);
7547 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7550 * Special case detection, is there just a single
7551 * packet outstanding when we are not in recovery?
7553 * If this is true mark it so.
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;
7559 prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7561 prsm->r_one_out_nr = 1;
7566 * If we reach here its a retransmission and we need to find it.
7568 memset(&fe, 0, sizeof(fe));
7570 if (hintrsm && (hintrsm->r_start == seq_out)) {
7574 /* No hints sorry */
7577 if ((rsm) && (rsm->r_start == seq_out)) {
7578 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7585 /* Ok it was not the last pointer go through it the hard way. */
7587 fe.r_start = seq_out;
7588 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
7590 if (rsm->r_start == seq_out) {
7591 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag);
7598 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
7599 /* Transmitted within this piece */
7601 * Ok we must split off the front and then let the
7602 * update do the rest
7604 nrsm = rack_alloc_full_limit(rack);
7606 rack_update_rsm(tp, rack, rsm, cts, add_flag);
7610 * copy rsm to nrsm and then trim the front of rsm
7611 * to not include this part.
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__);
7617 if (insret != NULL) {
7618 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7619 nrsm, insret, rack, rsm);
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;
7626 rsm->r_flags &= (~RACK_HAS_FIN);
7627 seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag);
7635 * Hmm not found in map did they retransmit both old and on into the
7638 if (seq_out == tp->snd_max) {
7640 } else if (SEQ_LT(seq_out, tp->snd_max)) {
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);
7649 printf("Dump complete\n");
7650 panic("seq_out not found rack:%p tp:%p",
7656 * Hmm beyond sndmax? (only if we are using the new rtt-pack
7659 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
7660 seq_out, len, tp->snd_max, tp);
7666 * Record one of the RTT updates from an ack into
7667 * our sample structure.
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)
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;
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;
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;
7688 if ((confidence == 1) &&
7690 (rsm->r_just_ret) ||
7691 (rsm->r_one_out_nr &&
7692 len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
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
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) {
7710 * We take anything with no current confidence
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) {
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.
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;
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++;
7736 * Collect new round-trip time estimate
7737 * and update averages and current timeout.
7740 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
7743 uint32_t o_srtt, o_var;
7744 int32_t hrtt_up = 0;
7747 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
7748 /* No valid sample */
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);
7762 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
7768 if (rack->rc_gp_rtt_set == 0) {
7770 * With no RTT we have to accept
7771 * even one we are not confident of.
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;
7780 if (rack->r_ctl.rack_rs.confidence) {
7782 * record the low and high for highly buffered path computation,
7783 * we only do this if we are confident (not a retransmission).
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;
7789 if (rack->rc_highly_buffered == 0) {
7791 * Currently once we declare a path has
7792 * highly buffered there is no going
7793 * back, which may be a problem...
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,
7800 rack->rc_highly_buffered = 1;
7804 if ((rack->r_ctl.rack_rs.confidence) ||
7805 (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
7807 * If we are highly confident of it <or> it was
7808 * never retransmitted we accept it as the last us_rtt.
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;
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) {
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.
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)
7837 /* Now lets make the absolute value of the variance */
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)
7846 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
7847 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7850 * No rtt measurement yet - use the unsmoothed rtt. Set the
7851 * variance to half the rtt (so our first retransmit happens
7855 tp->t_rttvar = rtt >> 1;
7856 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
7858 rack->rc_srtt_measure_made = 1;
7859 KMOD_TCPSTAT_INC(tcps_rttupdated);
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 */
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 */
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));
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));
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).
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;
7904 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
7907 * Apply to filter the inbound us-rtt at us_cts.
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,
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;
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);
7925 * Only count it if its lower than what we saw within our
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)) {
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.
7941 val = rack_probertt_lower_within * rack_time_between_probertt;
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);
7948 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
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)
7959 uint32_t t, len_acked;
7961 if ((rsm->r_flags & RACK_ACKED) ||
7962 (rsm->r_flags & RACK_WAS_ACKED))
7965 if (rsm->r_no_rtt_allowed) {
7969 if (ack_type == CUM_ACKED) {
7970 if (SEQ_GT(th_ack, rsm->r_end)) {
7971 len_acked = rsm->r_end - rsm->r_start;
7974 len_acked = th_ack - rsm->r_start;
7978 len_acked = rsm->r_end - rsm->r_start;
7981 if (rsm->r_rtr_cnt == 1) {
7983 t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
7986 if (!tp->t_rttlow || 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;
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)];
7998 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-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);
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);
8011 * We need to setup what our confidence
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
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
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
8038 if (rsm->r_flags & RACK_APP_LIMITED) {
8039 if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8043 } else if (rack->app_limited_needs_set == 0) {
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);
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);
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;
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.
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) &&
8081 ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8083 * Now which timestamp does it match? In this block the ACK
8084 * must be coming from a previous transmission.
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];
8091 if (CC_ALGO(tp)->rttsample != NULL) {
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.
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];
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);
8105 if ((i + 1) < rsm->r_rtr_cnt) {
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.
8112 * Hmm should there be a CC revert here?
8117 if (!tp->t_rttlow || 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;
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;
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,
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.
8146 i = rsm->r_rtr_cnt - 1;
8147 t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8150 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
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.
8161 } else if (rack->r_ctl.rc_rack_min_rtt) {
8163 * We retransmitted it and the retransmit did the
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;
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;
8185 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
8188 rack_log_sack_passed(struct tcpcb *tp,
8189 struct tcp_rack *rack, struct rack_sendmap *rsm)
8191 struct rack_sendmap *nrsm;
8194 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
8195 rack_head, r_tnext) {
8197 /* Skip orginal segment he is acked */
8200 if (nrsm->r_flags & RACK_ACKED) {
8202 * Skip ack'd segments, though we
8203 * should not see these, since tmap
8204 * should not have ack'd segments.
8208 if (nrsm->r_flags & RACK_SACK_PASSED) {
8210 * We found one that is already marked
8211 * passed, we have been here before and
8212 * so all others below this are marked.
8216 nrsm->r_flags |= RACK_SACK_PASSED;
8217 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
8222 rack_need_set_test(struct tcpcb *tp,
8223 struct tcp_rack *rack,
8224 struct rack_sendmap *rsm,
8230 if ((tp->t_flags & TF_GPUTINPROG) &&
8231 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
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.
8241 if (rsm->r_rtr_cnt > 1) {
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?
8249 * Lets wait for something better that
8250 * is not retransmitted.
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)) {
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.
8268 tp->gput_seq = rsm->r_start;
8269 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8271 if ((use_which == RACK_USE_END) &&
8272 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
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.
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.
8284 tp->gput_seq = rsm->r_end;
8285 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8287 if (use_which == RACK_USE_END_OR_THACK) {
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
8294 if (SEQ_GT(th_ack, rsm->r_end))
8295 tp->gput_seq = th_ack;
8297 tp->gput_seq = rsm->r_end;
8298 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8300 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
8302 * We moved beyond this guy's range, re-calculate
8303 * the new end point.
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)));
8308 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
8312 * We are moving the goal post, we may be able to clear the
8313 * measure_saw_probe_rtt flag.
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;
8327 ideal_amount = rack_get_measure_window(tp, rack);
8328 if (ideal_amount > sbavail(&tp->t_inpcb->inp_socket->so_snd)) {
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.
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);
8340 * Reset the window further out.
8342 tp->gput_ack = tp->gput_seq + ideal_amount;
8349 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
8351 if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
8352 /* Behind our TLP definition or right at */
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 */
8359 /* It has to be a sub-part of the original TLP recorded */
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)
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;
8374 start = sack->start;
8377 memset(&fe, 0, sizeof(fe));
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))) {
8384 * We are not in the right spot,
8385 * find the correct spot in the tree.
8389 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
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) {
8400 * Before any splitting or hookery is
8401 * done is it a TLP of interest i.e. rxt?
8403 if ((rsm->r_flags & RACK_TLP) &&
8404 (rsm->r_rtr_cnt > 1)) {
8406 * We are splitting a rxt TLP, check
8407 * if we need to save off the start/end
8409 if (rack->rc_last_tlp_acked_set &&
8410 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
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).
8417 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8419 * Lets make sure we have all of it though.
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);
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);
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);
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 |--------------|
8447 * and nrsm will be the sacked piece
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.
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)) {
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)
8471 * Where nrsm is a temporary stack piece we
8472 * use to update all the gizmos.
8474 /* Copy up our fudge block */
8476 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8477 /* Now adjust our tree blocks */
8479 next->r_start = start;
8480 /* Now we must adjust back where next->m is */
8481 rack_setup_offset_for_rsm(rsm, next);
8483 /* We don't need to adjust rsm, it did not change */
8484 /* Clear out the dup ack count of the remainder */
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;
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
8510 if (rsm->r_in_tmap) {
8511 nrsm = TAILQ_NEXT(rsm, r_tnext);
8513 * Now that we have the next
8514 * one walk backwards from there.
8516 if (nrsm && nrsm->r_in_tmap)
8517 rack_log_sack_passed(tp, rack, nrsm);
8519 /* Now are we done? */
8520 if (SEQ_LT(end, next->r_end) ||
8521 (end == next->r_end)) {
8522 /* Done with block */
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);
8534 * We can't use any hookery here, so we
8535 * need to split the map. We enter like
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:
8546 * We then fall through reseting
8547 * rsm to nrsm, so the next block
8550 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8553 * failed XXXrrs what can we do but loose the sack
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);
8563 if (insret != NULL) {
8564 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8565 nrsm, insret, rack, rsm);
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;
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 */
8578 /* Already sacked this piece */
8579 counter_u64_add(rack_sack_skipped_acked, 1);
8581 if (end == rsm->r_end) {
8582 /* Done with block */
8583 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8585 } else if (SEQ_LT(end, rsm->r_end)) {
8586 /* A partial sack to a already sacked block */
8588 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8592 * The end goes beyond this guy
8593 * repostion the start to the
8597 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8603 if (SEQ_GEQ(end, rsm->r_end)) {
8605 * The end of this block is either beyond this guy or right
8606 * at this guy. I.e.:
8612 if ((rsm->r_flags & RACK_ACKED) == 0) {
8614 * Is it a TLP of interest?
8616 if ((rsm->r_flags & RACK_TLP) &&
8617 (rsm->r_rtr_cnt > 1)) {
8619 * We are splitting a rxt TLP, check
8620 * if we need to save off the start/end
8622 if (rack->rc_last_tlp_acked_set &&
8623 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
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).
8629 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8631 * Lets make sure we have all of it though.
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);
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);
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);
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;
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);
8670 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
8672 counter_u64_add(rack_sack_skipped_acked, 1);
8675 if (end == rsm->r_end) {
8676 /* This block only - done, setup for next */
8680 * There is more not coverend by this rsm move on
8681 * to the next block in the RB tree.
8683 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8691 * The end of this sack block is smaller than
8696 if ((rsm->r_flags & RACK_ACKED) == 0) {
8698 * Is it a TLP of interest?
8700 if ((rsm->r_flags & RACK_TLP) &&
8701 (rsm->r_rtr_cnt > 1)) {
8703 * We are splitting a rxt TLP, check
8704 * if we need to save off the start/end
8706 if (rack->rc_last_tlp_acked_set &&
8707 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
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).
8713 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8715 * Lets make sure we have all of it though.
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);
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);
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);
8735 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8737 (prev->r_flags & RACK_ACKED)) {
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)
8747 * We churn it so we end up with
8748 * prev |----------| (acked)
8749 * rsm |-----| (non-acked)
8750 * nrsm |-| (temporary)
8752 * Note if either prev/rsm is a TLP we don't
8756 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
8759 /* Now adjust nrsm (stack copy) to be
8760 * the one that is the small
8761 * piece that was "sacked".
8765 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8767 * Now that the rsm has had its start moved forward
8768 * lets go ahead and get its new place in the world.
8770 rack_setup_offset_for_rsm(prev, rsm);
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.
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;
8786 rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
8788 counter_u64_add(rack_sack_used_prev_merge, 1);
8791 * This is the case where our previous
8792 * block is not acked either, so we must
8793 * split the block in two.
8795 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8797 /* failed rrs what can we do but loose the sack info? */
8800 if ((rsm->r_flags & RACK_TLP) &&
8801 (rsm->r_rtr_cnt > 1)) {
8803 * We are splitting a rxt TLP, check
8804 * if we need to save off the start/end
8806 if (rack->rc_last_tlp_acked_set &&
8807 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
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).
8813 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
8815 * Lets make sure we have all of it though.
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);
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);
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);
8836 * In this case nrsm becomes
8837 * nrsm->r_start = end;
8838 * nrsm->r_end = rsm->r_end;
8839 * which is un-acked.
8841 * rsm->r_end = nrsm->r_start;
8842 * i.e. the remaining un-acked
8843 * piece is left on the left
8846 * So we start like this
8847 * rsm |----------| (not acked)
8849 * build it so we have
8851 * nrsm |------| (not acked)
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);
8859 if (insret != NULL) {
8860 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8861 nrsm, insret, rack, rsm);
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;
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;
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);
8891 } else if (start != end){
8893 * The block was already acked.
8895 counter_u64_add(rack_sack_skipped_acked, 1);
8900 ((rsm->r_flags & RACK_TLP) == 0) &&
8901 (rsm->r_flags & RACK_ACKED)) {
8903 * Now can we merge where we worked
8904 * with either the previous or
8907 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8909 if (next->r_flags & RACK_TLP)
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);
8918 /* Now what about the previous? */
8919 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8921 if (prev->r_flags & RACK_TLP)
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);
8931 if (used_ref == 0) {
8932 counter_u64_add(rack_sack_proc_all, 1);
8934 counter_u64_add(rack_sack_proc_short, 1);
8936 /* Save off the next one for quick reference. */
8938 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8941 *prsm = rack->r_ctl.rc_sacklast = nrsm;
8942 /* Pass back the moved. */
8948 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
8950 struct rack_sendmap *tmap;
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);
8957 if (rsm->r_in_tmap) {
8958 panic("rack:%p rsm:%p flags:0x%x in tmap?",
8959 rack, rsm, rsm->r_flags);
8962 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
8963 /* Rebuild it into our tmap */
8965 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8968 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
8971 tmap->r_in_tmap = 1;
8972 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
8975 * Now lets possibly clear the sack filter so we start
8976 * recognizing sacks that cover this area.
8978 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
8983 rack_do_decay(struct tcp_rack *rack)
8987 #define timersub(tvp, uvp, vvp) \
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) { \
8993 (vvp)->tv_usec += 1000000; \
8997 timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
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)) {
9005 * Check for decay of non-SAD,
9006 * we want all SAD detection metrics to
9007 * decay 1/4 per second (or more) passed.
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)){
9021 * We don't decay idle connections
9022 * or ones that have a low input pps.
9026 /* Decay the counters */
9027 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
9029 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
9031 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
9033 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
9040 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to)
9042 struct rack_sendmap *rsm, *rm;
9045 * The ACK point is advancing to th_ack, we must drop off
9046 * the packets in the rack log and calculate any eligble
9049 rack->r_wanted_output = 1;
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))) {
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.
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.
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))) {
9077 * It is safe to start aging TLP's out.
9079 rack->rc_last_tlp_past_cumack = 1;
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))) {
9095 * It is safe to start aging TLP's send.
9097 rack->rc_last_sent_tlp_past_cumack = 1;
9100 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
9102 if ((th_ack - 1) == tp->iss) {
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.
9111 if (tp->t_flags & TF_SENTFIN) {
9112 /* if we sent a FIN we often will not have map */
9116 panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
9118 tp->t_state, th_ack, rack,
9119 tp->snd_una, tp->snd_max, tp->snd_nxt);
9123 if (SEQ_LT(th_ack, rsm->r_start)) {
9124 /* Huh map is missing this */
9126 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
9128 th_ack, tp->t_state, rack->r_state);
9132 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
9134 /* Now was it a retransmitted TLP? */
9135 if ((rsm->r_flags & RACK_TLP) &&
9136 (rsm->r_rtr_cnt > 1)) {
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).
9146 if (rack->rc_last_tlp_acked_set &&
9147 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
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).
9153 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9155 * Lets make sure we have all of it though.
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);
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);
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);
9175 /* Now do we consume the whole thing? */
9176 if (SEQ_GEQ(th_ack, rsm->r_end)) {
9177 /* Its all consumed. */
9179 uint8_t newly_acked;
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);
9189 panic("removing head in rack:%p rsm:%p rm:%p",
9193 if (rsm->r_in_tmap) {
9194 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9198 if (rsm->r_flags & RACK_ACKED) {
9200 * It was acked on the scoreboard -- remove
9203 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
9205 } else if (rsm->r_flags & RACK_SACK_PASSED) {
9207 * There are segments ACKED on the
9208 * scoreboard further up. We are seeing
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) {
9218 * We have sent no more, and we saw an sack
9221 rack->r_might_revert = 1;
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)) {
9230 * We can use the timestamp to see
9231 * if this retransmission was from the
9232 * first transmit. If so we made a mistake.
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);
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);
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)) {
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.
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.
9265 rack_peer_reneges(rack, rsm, th_ack);
9269 if (rsm->r_flags & RACK_ACKED) {
9271 * It was acked on the scoreboard -- remove it from
9272 * total for the part being cum-acked.
9274 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
9277 * Clear the dup ack count for
9278 * the piece that remains.
9281 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9282 if (rsm->r_rtr_bytes) {
9284 * It was retransmitted adjust the
9285 * sack holes for what was acked.
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;
9296 * Update where the piece starts and record
9297 * the time of send of highest cumack sent.
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);
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? */
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",
9317 rsm->orig_m_len = rsm->m->m_len;
9319 if (rack->app_limited_needs_set)
9320 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
9324 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
9326 struct rack_sendmap *rsm;
9327 int sack_pass_fnd = 0;
9329 if (rack->r_might_revert) {
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.
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.
9340 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
9341 if (rsm->r_flags & RACK_SACK_PASSED) {
9346 if (sack_pass_fnd == 0) {
9348 * We went into recovery
9349 * incorrectly due to reordering!
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);
9361 rack->r_might_revert = 0;
9365 #ifdef NETFLIX_EXP_DETECTION
9367 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack, uint32_t bytes_this_ack, uint32_t segsiz)
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)) {
9374 * We have thresholds set to find
9375 * possible attackers and disable sack.
9378 uint64_t ackratio, moveratio, movetotal;
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);
9387 /* We really should not hit here */
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;
9398 moveratio /= movetotal;
9400 /* No moves, thats pretty good */
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);
9415 if (tcp_attack_on_turns_on_logging) {
9417 * Turn on logging, used for debugging
9420 rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging;
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);
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));
9440 if (rack->r_rep_reverse == 0) {
9441 rack->r_rep_reverse = 1;
9442 counter_u64_add(rack_sack_attacks_reversed, 1);
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;
9454 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
9460 if (SEQ_GT(end, start))
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))) {
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.
9473 rack_log_dsack_event(rack, 7, __LINE__, start, end);
9475 goto skip_dsack_round;
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))) {
9482 * This dsack is from the last sent TLP, ignore it
9483 * for reordering purposes.
9485 rack_log_dsack_event(rack, 7, __LINE__, start, end);
9487 goto skip_dsack_round;
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);
9499 * We keep track of how many DSACK blocks we get
9500 * after a recovery incident.
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)) {
9507 * False recovery most likely culprit is reordering. If
9508 * nothing else is missing we need to revert.
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;
9520 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
9522 /* Deal with changed and PRR here (in recovery only) */
9523 uint32_t pipe, snd_una;
9525 rack->r_ctl.rc_prr_delivered += changed;
9527 if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
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.
9534 rack->r_ctl.rc_prr_sndcnt = 0;
9537 /* Compute prr_sndcnt */
9538 if (SEQ_GT(tp->snd_una, th_ack)) {
9539 snd_una = tp->snd_una;
9543 pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
9544 if (pipe > tp->snd_ssthresh) {
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;
9551 rack->r_ctl.rc_prr_sndcnt = 0;
9552 rack_log_to_prr(rack, 9, 0);
9556 if (sndcnt > (long)rack->r_ctl.rc_prr_out)
9557 sndcnt -= rack->r_ctl.rc_prr_out;
9560 rack->r_ctl.rc_prr_sndcnt = sndcnt;
9561 rack_log_to_prr(rack, 10, 0);
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);
9569 if (changed > limit)
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);
9576 rack->r_ctl.rc_prr_sndcnt = min(0, limit);
9577 rack_log_to_prr(rack, 12, 0);
9583 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck)
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;
9596 INP_WLOCK_ASSERT(tp->t_inpcb);
9597 if (th->th_flags & TH_RST) {
9598 /* We don't log resets */
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);
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)) {
9610 * You only get credit for
9611 * MSS and greater (and you get extra
9612 * credit for larger cum-ack moves).
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);
9620 if (rack->r_ctl.ack_count > 0xfff00000) {
9622 * reduce the number to keep us under
9625 rack->r_ctl.ack_count /= 2;
9626 rack->r_ctl.sack_count /= 2;
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;
9632 if (rsm && SEQ_GT(th_ack, rsm->r_start))
9633 changed = th_ack - rsm->r_start;
9635 rack_process_to_cumack(tp, rack, th_ack, cts, to);
9637 if ((to->to_flags & TOF_SACK) == 0) {
9638 /* We are done nothing left and no sack. */
9639 rack_handle_might_revert(tp, rack);
9641 * For cases where we struck a dup-ack
9642 * with no SACK, add to the changes so
9643 * PRR will work right.
9645 if (dup_ack_struck && (changed == 0)) {
9646 changed += ctf_fixed_maxseg(rack->rc_tp);
9650 /* Sack block processing */
9651 if (SEQ_GT(th_ack, tp->snd_una))
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;
9667 } else if (SEQ_LEQ(sack.start, th_ack) &&
9668 SEQ_LEQ(sack.end, th_ack)) {
9671 was_tlp = rack_note_dsack(rack, sack.start, sack.end);
9673 * Its a D-SACK block.
9675 tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
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)) {
9682 rack->rc_dsack_round_seen = 0;
9683 rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
9687 * Sort the SACK blocks so we can update the rack scoreboard with
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;
9697 if (num_sack_blks < 2) {
9698 /* Only one, we don't need to sort */
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;
9712 * Now are any of the sack block ends the same (yes some
9713 * implementations send these)?
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) {
9723 * Ok these two have the same end we
9724 * want the smallest end and then
9725 * throw away the larger and start
9728 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
9730 * The second block covers
9731 * more area use that
9733 sack_blocks[i].start = sack_blocks[j].start;
9736 * Now collapse out the dup-sack and
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;
9752 * First lets look to see if
9753 * we have retransmitted and
9754 * can use the transmit next?
9756 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
9758 SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
9759 SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
9761 * We probably did the FR and the next
9762 * SACK in continues as we would expect.
9764 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two);
9766 rack->r_wanted_output = 1;
9768 sack_changed += acked;
9770 if (num_sack_blks == 1) {
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.
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) {
9786 * reduce the number to keep us under
9789 rack->r_ctl.ack_count /= 2;
9790 rack->r_ctl.sack_count /= 2;
9792 goto out_with_totals;
9795 * Start the loop through the
9796 * rest of blocks, past the first block.
9802 /* Its a sack of some sort */
9803 rack->r_ctl.sack_count++;
9804 if (rack->r_ctl.sack_count > 0xfff00000) {
9806 * reduce the number to keep us under
9809 rack->r_ctl.ack_count /= 2;
9810 rack->r_ctl.sack_count /= 2;
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;
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);
9830 rack->r_wanted_output = 1;
9832 sack_changed += acked;
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.
9840 rack->r_ctl.sack_moved_extra += moved_two;
9841 counter_u64_add(rack_move_some, 1);
9844 * else we did not have to move
9845 * any more than we would expect.
9847 rack->r_ctl.sack_noextra_move++;
9848 counter_u64_add(rack_move_none, 1);
9850 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
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.
9857 rack->r_ctl.sack_count += moved_two;
9858 counter_u64_add(rack_sack_total, moved_two);
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
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;
9872 if (rack->r_ctl.sack_count > 0xfff00000) {
9873 rack->r_ctl.ack_count /= 2;
9874 rack->r_ctl.sack_count /= 2;
9879 if (num_sack_blks > 1) {
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
9888 rack->r_ctl.sack_moved_extra++;
9889 counter_u64_add(rack_move_some, 1);
9892 #ifdef NETFLIX_EXP_DETECTION
9893 rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
9896 /* Something changed cancel the rack timer */
9897 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9899 tsused = tcp_get_usecs(NULL);
9900 rsm = tcp_rack_output(tp, rack, tsused);
9901 if ((!IN_FASTRECOVERY(tp->t_flags)) &&
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);
9910 * When we enter recovery we need to assure we send
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);
9917 rack->r_timer_override = 1;
9919 rack->r_ctl.rc_agg_early = 0;
9920 } else if (IN_FASTRECOVERY(tp->t_flags) &&
9922 (rack->r_rr_config == 3)) {
9924 * Assure we can output and we get no
9925 * remembered pace time except the retransmit.
9927 rack->r_timer_override = 1;
9928 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
9929 rack->r_ctl.rc_resend = rsm;
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)))) {
9939 * If you are pacing output you don't want
9943 rack->r_ctl.rc_agg_early = 0;
9944 rack->r_timer_override = 1;
9950 rack_strike_dupack(struct tcp_rack *rack)
9952 struct rack_sendmap *rsm;
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);
9958 if (rsm && (rsm->r_dupack < 0xff)) {
9960 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
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.
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);
9977 rack->r_wanted_output = 1;
9978 rack->r_timer_override = 1;
9979 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
9982 rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
9988 rack_check_bottom_drag(struct tcpcb *tp,
9989 struct tcp_rack *rack,
9990 struct socket *so, int32_t acked)
9992 uint32_t segsiz, minseg;
9994 segsiz = ctf_fixed_maxseg(tp);
9997 if (tp->snd_max == tp->snd_una) {
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.
10003 * This means we need to boost the b/w in
10004 * addition to any earlier boosting of
10007 rack->rc_dragged_bottom = 1;
10008 rack_validate_multipliers_at_or_above100(rack);
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.
10017 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
10019 uint64_t bw, calc_bw, rtt;
10021 rtt = rack->r_ctl.rack_rs.rs_us_rtt;
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;
10027 goto no_measurement;
10031 calc_bw = bw * 1000000;
10033 if (rack->r_ctl.last_max_bw &&
10034 (rack->r_ctl.last_max_bw < calc_bw)) {
10036 * If we have a last calculated max bw
10039 calc_bw = rack->r_ctl.last_max_bw;
10041 /* now plop it in */
10042 if (rack->rc_gp_filled == 0) {
10043 if (calc_bw > ONE_POINT_TWO_MEG) {
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.
10052 calc_bw = ONE_POINT_TWO_MEG;
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);
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);
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).
10080 if (acked > segsiz)
10081 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10084 * zero rtt possibly?, settle for just an old increase.
10087 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10089 } else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
10090 (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
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))) {
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.
10104 rack_validate_multipliers_at_or_above100(rack);
10105 rack->rc_dragged_bottom = 1;
10106 rack_increase_bw_mul(rack, -1, 0, 0, 1);
10113 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
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
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).
10127 * 2) The amount of data left in the socket buffer (i.e.
10128 * we can't send beyond what is in the buffer).
10130 * Note that this does not take into account any increase
10131 * in the cwnd. We will only extend the fast path by
10134 uint32_t new_total, gating_val;
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)));
10153 rack_adjust_sendmap(struct tcp_rack *rack, struct sockbuf *sb, tcp_seq snd_una)
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.
10162 * Note that (2) implies rack_ack_received has also
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).
10176 struct rack_sendmap *rsm;
10178 SOCKBUF_LOCK_ASSERT(sb);
10180 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
10181 if ((rsm == NULL) || (m == NULL)) {
10182 /* Nothing outstanding */
10185 while (rsm->m && (rsm->m == m)) {
10186 /* one to adjust */
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);
10195 if (rsm->soff != soff) {
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
10202 counter_u64_add(rack_adjust_map_bw, 1);
10207 rsm->orig_m_len = rsm->m->m_len;
10209 rsm->orig_m_len = 0;
10211 rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
10213 rsm->orig_m_len = rsm->m->m_len;
10215 rsm->orig_m_len = 0;
10217 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
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.
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)
10236 int32_t ourfinisacked = 0;
10237 int32_t nsegs, acked_amount;
10239 struct mbuf *mfree;
10240 struct tcp_rack *rack;
10241 int32_t under_pacing = 0;
10242 int32_t recovery = 0;
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;
10252 if (rack->gp_ready &&
10253 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
10256 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
10257 int in_rec, dup_ack_struck = 0;
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);
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;
10271 rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), dup_ack_struck);
10273 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
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.
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);
10286 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
10287 * something we sent.
10289 if (tp->t_flags & TF_NEEDSYN) {
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.
10296 tp->t_flags &= ~TF_NEEDSYN;
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. */
10305 nsegs = max(1, m->m_pkthdr.lro_nsegs);
10306 INP_WLOCK_ASSERT(tp->t_inpcb);
10308 acked = BYTES_THIS_ACK(tp, th);
10309 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
10310 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
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.
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);
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;
10332 * If it is the RXT timer we want to
10333 * stop it, so we can restart a TLP.
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);
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
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.
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.
10361 *ofia = ourfinisacked;
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);
10369 rack_post_recovery(tp, th->th_ack);
10374 * Let the congestion control algorithm update congestion control
10375 * related information. This typically means increasing the
10376 * congestion window.
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)) {
10388 * We must be sure our fin
10389 * was sent and acked (we can be
10390 * in FIN_WAIT_1 without having
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);
10402 if (SEQ_GT(tp->snd_una, tp->snd_recover))
10403 tp->snd_recover = tp->snd_una;
10405 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
10406 tp->snd_nxt = tp->snd_una;
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);
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)
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)) {
10435 * The socket was gone and the
10436 * peer sent data (now or in the past), time to
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);
10448 *ofia = ourfinisacked;
10453 rack_collapsed_window(struct tcp_rack *rack)
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
10471 struct rack_sendmap *rsm, *nrsm, fe, *insret;
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);
10480 /* Nothing to do strange */
10481 rack->rc_has_collapsed = 0;
10485 * Now do we need to split at
10486 * the collapse point?
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? */
10496 rack_clone_rsm(rack, nrsm, rsm, max_seq);
10497 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
10499 if (insret != NULL) {
10500 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
10501 nrsm, insret, rack, rsm);
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;
10510 * Set in the new RSM as the
10511 * collapsed starting point
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;
10520 rack->rc_has_collapsed = 1;
10524 rack_un_collapse_window(struct tcp_rack *rack)
10526 struct rack_sendmap *rsm;
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;
10534 rack->rc_has_collapsed = 0;
10538 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
10539 int32_t tlen, int32_t tfo_syn)
10541 if (DELAY_ACK(tp, tlen) || tfo_syn) {
10542 if (rack->rc_dack_mode &&
10544 (rack->rc_dack_toggle == 1)) {
10545 goto no_delayed_ack;
10547 rack_timer_cancel(tp, rack,
10548 rack->r_ctl.rc_rcvtime, __LINE__);
10549 tp->t_flags |= TF_DELACK;
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;
10558 rack->rc_dack_toggle = 0;
10564 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
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.
10571 if (rack->r_fast_output) {
10575 * Calculate what we will send if left as is
10576 * and compare that to our send window.
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;
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;
10598 * Return value of 1, the TCB is unlocked and most
10599 * likely gone, return value of 0, the TCP is still
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)
10608 * Update window information. Don't look at window if no ACK: TAC's
10609 * send garbage on first SYN.
10613 struct tcp_rack *rack;
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 */
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;
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;
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)) {
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.
10668 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
10670 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
10675 * don't process the URG bit, ignore them drag
10678 tp->rcv_up = tp->rcv_nxt;
10679 INP_WLOCK_ASSERT(tp->t_inpcb);
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.
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;
10697 m_adj(m, drop_hdrlen); /* delayed header drop */
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).
10709 if (th->th_seq == tp->rcv_nxt &&
10711 (TCPS_HAVEESTABLISHED(tp->t_state) ||
10713 #ifdef NETFLIX_SB_LIMITS
10714 u_int mcnt, appended;
10716 if (so->so_rcv.sb_shlim) {
10717 mcnt = m_memcnt(m);
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);
10727 rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
10728 tp->rcv_nxt += 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;
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) {
10745 #ifdef NETFLIX_SB_LIMITS
10748 sbappendstream_locked(&so->so_rcv, m, 0);
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,
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.
10765 tcp_seq temp = save_start;
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);
10775 if ((tp->t_flags & TF_SACK_PERMIT) &&
10777 TCPS_HAVEESTABLISHED(tp->t_state)) {
10778 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
10780 * DSACK actually handled in the fastpath
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)) {
10790 * Partial overlap, recorded at todrop
10793 RACK_OPTS_INC(tcp_sack_path_2a);
10794 tcp_update_sack_list(tp,
10795 tp->sackblks[0].start,
10796 tp->sackblks[0].end);
10798 RACK_OPTS_INC(tcp_sack_path_2b);
10799 tcp_update_dsack_list(tp, save_start,
10800 save_start + save_tlen);
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);
10815 thflags &= ~TH_FIN;
10819 * If FIN is received ACK the FIN and let the user know that the
10820 * connection is closing.
10822 if (thflags & TH_FIN) {
10823 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10824 /* The socket upcall is handled by socantrcvmore. */
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
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;
10838 tp->t_flags |= TF_ACKNOW;
10842 switch (tp->t_state) {
10844 * In SYN_RECEIVED and ESTABLISHED STATES enter the
10845 * CLOSE_WAIT state.
10847 case TCPS_SYN_RECEIVED:
10848 tp->t_starttime = ticks;
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);
10857 * If still in FIN_WAIT_1 STATE FIN has not been
10858 * acked so enter the CLOSING state.
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);
10867 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10868 * starting the time-wait timer, turning off the
10869 * other standard timers.
10871 case TCPS_FIN_WAIT_2:
10872 rack_timer_cancel(tp, rack,
10873 rack->r_ctl.rc_rcvtime, __LINE__);
10879 * Return any desired output.
10881 if ((tp->t_flags & TF_ACKNOW) ||
10882 (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
10883 rack->r_wanted_output = 1;
10885 INP_WLOCK_ASSERT(tp->t_inpcb);
10890 * Here nothing is really faster, its just that we
10891 * have broken out the fast-data path also just like
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)
10900 int32_t newsize = 0; /* automatic sockbuf scaling */
10901 struct tcp_rack *rack;
10902 #ifdef NETFLIX_SB_LIMITS
10903 u_int mcnt, appended;
10907 * The size of tcp_saveipgen must be the size of the max ip header,
10910 u_char tcp_saveipgen[IP6_HDR_LEN];
10911 struct tcphdr tcp_savetcp;
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).
10920 if (__predict_false(th->th_seq != tp->rcv_nxt)) {
10923 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
10926 if (tiwin && tiwin != tp->snd_wnd) {
10929 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
10932 if (__predict_false((to->to_flags & TOF_TS) &&
10933 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
10936 if (__predict_false((th->th_ack != tp->snd_una))) {
10939 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
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;
10947 rack = (struct tcp_rack *)tp->t_fb_ptr;
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.
10952 nsegs = max(1, m->m_pkthdr.lro_nsegs);
10954 #ifdef NETFLIX_SB_LIMITS
10955 if (so->so_rcv.sb_shlim) {
10956 mcnt = m_memcnt(m);
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);
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;
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;
10981 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
10983 tp->snd_wl1 = th->th_seq;
10985 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
10987 tp->rcv_up = tp->rcv_nxt;
10988 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
10989 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
10991 if (so->so_options & SO_DEBUG)
10992 tcp_trace(TA_INPUT, ostate, tp,
10993 (void *)tcp_saveipgen, &tcp_savetcp, 0);
10995 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
10997 /* Add data to socket buffer. */
10998 SOCKBUF_LOCK(&so->so_rcv);
10999 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
11003 * Set new socket buffer size. Give up when limit is
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
11014 sbappendstream_locked(&so->so_rcv, m, 0);
11015 ctf_calc_rwin(so, tp);
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);
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);
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
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)
11048 * The size of tcp_saveipgen must be the size of the max ip header,
11051 u_char tcp_saveipgen[IP6_HDR_LEN];
11052 struct tcphdr tcp_savetcp;
11055 int32_t under_pacing = 0;
11056 struct tcp_rack *rack;
11058 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
11059 /* Old ack, behind (or duplicate to) the last one rcv'd */
11062 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
11063 /* Above what we have sent? */
11066 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
11067 /* We are retransmitting */
11070 if (__predict_false(tiwin == 0)) {
11074 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
11075 /* We need a SYN or a FIN, unlikely.. */
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? */
11082 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
11083 /* Still recovering */
11086 rack = (struct tcp_rack *)tp->t_fb_ptr;
11087 if (rack->r_ctl.rc_sacked) {
11088 /* We have sack holes on our scoreboard */
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)) {
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;
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);
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)) {
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.
11125 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
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).
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;
11138 * This is a pure ack for outstanding data.
11140 KMOD_TCPSTAT_INC(tcps_predack);
11143 * "bad retransmit" recovery.
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);
11153 * Recalculate the transmit timer / rtt.
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.
11159 acked = BYTES_THIS_ACK(tp, th);
11162 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
11163 hhook_run_tcp_est_in(tp, th, to);
11165 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
11166 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
11168 struct mbuf *mfree;
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);
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;
11186 * If it is the RXT timer we want to
11187 * stop it, so we can restart a TLP.
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);
11196 * Let the congestion control algorithm update congestion control
11197 * related information. This typically means increasing the
11198 * congestion window.
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);
11207 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
11209 tp->snd_wl2 = th->th_ack;
11212 /* ND6_HINT(tp); *//* Some progress has been made. */
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.
11222 if (so->so_options & SO_DEBUG)
11223 tcp_trace(TA_INPUT, ostate, tp,
11224 (void *)tcp_saveipgen,
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);
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)
11245 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
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;
11256 * Return value of 1, the TCB is unlocked and most
11257 * likely gone, return value of 0, the TCP is still
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)
11265 int32_t ret_val = 0;
11267 int32_t ourfinisacked = 0;
11268 struct tcp_rack *rack;
11270 ctf_calc_rwin(so, tp);
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.
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);
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);
11296 if (thflags & TH_RST) {
11297 ctf_do_drop(m, tp);
11300 if (!(thflags & TH_SYN)) {
11301 ctf_do_drop(m, tp);
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;
11310 KMOD_TCPSTAT_INC(tcps_connects);
11313 mac_socketpeer_set_from_mbuf(m, so);
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;
11320 tp->rcv_adv += min(tp->rcv_wnd,
11321 TCP_MAXWIN << tp->rcv_scale);
11323 * If not all the data that was sent in the TFO SYN
11324 * has been acked, resend the remainder right away.
11326 if (IS_FASTOPEN(tp->t_flags) &&
11327 (tp->snd_una != tp->snd_max)) {
11328 tp->snd_nxt = th->th_ack;
11332 * If there's data, delay ACK; if there's also a FIN ACKNOW
11333 * will be turned on later.
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;
11340 rack->r_wanted_output = 1;
11341 tp->t_flags |= TF_ACKNOW;
11342 rack->rc_dack_toggle = 0;
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);
11349 if (SEQ_GT(th->th_ack, tp->snd_una)) {
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.
11365 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
11366 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
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;
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);
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.
11388 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
11389 tcp_state_change(tp, TCPS_SYN_RECEIVED);
11391 INP_WLOCK_ASSERT(tp->t_inpcb);
11393 * Advance th->th_seq to correspond to first data byte. If data,
11394 * trim to stay within window, dropping FIN if necessary.
11397 if (tlen > tp->rcv_wnd) {
11398 todrop = tlen - tp->rcv_wnd;
11400 tlen = tp->rcv_wnd;
11401 thflags &= ~TH_FIN;
11402 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
11403 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
11405 tp->snd_wl1 = th->th_seq - 1;
11406 tp->rcv_up = th->th_seq;
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.
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) {
11418 mcts = tcp_ts_getticks();
11419 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11420 if (!tp->t_rttlow || 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);
11426 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
11428 /* We may have changed to FIN_WAIT_1 above */
11429 if (tp->t_state == TCPS_FIN_WAIT_1) {
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.
11435 if (ourfinisacked) {
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
11443 * XXXjl: we should release the tp also, and
11444 * use a compressed state.
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 :
11453 tcp_state_change(tp, TCPS_FIN_WAIT_2);
11457 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11458 tiwin, thflags, nxt_pkt));
11462 * Return value of 1, the TCB is unlocked and most
11463 * likely gone, return value of 0, the TCP is still
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)
11471 struct tcp_rack *rack;
11472 int32_t ret_val = 0;
11473 int32_t ourfinisacked = 0;
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);
11483 rack = (struct tcp_rack *)tp->t_fb_ptr;
11484 if (IS_FASTOPEN(tp->t_flags)) {
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
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);
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);
11504 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
11505 ctf_do_drop(m, NULL);
11509 if ((thflags & TH_RST) ||
11510 (tp->t_fin_is_rst && (thflags & TH_FIN)))
11511 return (ctf_process_rst(m, th, so, tp));
11513 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11514 * it's less than ts_recent, drop it.
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))
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.
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);
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)) {
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.
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;
11559 tp->snd_wnd = tiwin;
11560 rack_validate_fo_sendwin_up(tp, rack);
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.
11566 if ((thflags & TH_ACK) == 0) {
11567 if (IS_FASTOPEN(tp->t_flags)) {
11568 rack_cc_conn_init(tp);
11570 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11571 tiwin, thflags, nxt_pkt));
11573 KMOD_TCPSTAT_INC(tcps_connects);
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;
11581 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
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;
11589 if (tp->t_flags & TF_NEEDFIN) {
11590 tcp_state_change(tp, TCPS_FIN_WAIT_1);
11591 tp->t_flags &= ~TF_NEEDFIN;
11593 tcp_state_change(tp, TCPS_ESTABLISHED);
11594 TCP_PROBE5(accept__established, NULL, tp,
11595 mtod(m, const char *), tp, th);
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.
11602 if (!IS_FASTOPEN(tp->t_flags))
11603 rack_cc_conn_init(tp);
11606 * Account for the ACK of our SYN prior to
11607 * regular ACK processing below, except for
11608 * simultaneous SYN, which is handled later.
11610 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
11613 * If segment contains data or ACK, will call tcp_reass() later; if
11614 * not, do so now to pass queued data to user.
11616 if (tlen == 0 && (thflags & TH_FIN) == 0) {
11617 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 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);
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) {
11630 mcts = tcp_ts_getticks();
11631 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
11632 if (!tp->t_rttlow || 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);
11638 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
11641 if (tp->t_state == TCPS_FIN_WAIT_1) {
11642 /* We could have went to FIN_WAIT_1 (or EST) above */
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.
11648 if (ourfinisacked) {
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.
11655 * XXXjl: we should release the tp also, and use a
11656 * compressed state.
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 :
11665 tcp_state_change(tp, TCPS_FIN_WAIT_2);
11668 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11669 tiwin, thflags, nxt_pkt));
11673 * Return value of 1, the TCB is unlocked and most
11674 * likely gone, return value of 0, the TCP is still
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)
11682 int32_t ret_val = 0;
11683 struct tcp_rack *rack;
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.
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)) {
11705 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
11706 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
11710 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
11711 tiwin, nxt_pkt, iptos)) {
11716 ctf_calc_rwin(so, tp);
11718 if ((thflags & TH_RST) ||
11719 (tp->t_fin_is_rst && (thflags & TH_FIN)))
11720 return (ctf_process_rst(m, th, so, tp));
11723 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11724 * synchronized state.
11726 if (thflags & TH_SYN) {
11727 ctf_challenge_ack(m, th, tp, &ret_val);
11731 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11732 * it's less than ts_recent, drop it.
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))
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)) {
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.
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;
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.
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));
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;
11780 ctf_do_drop(m, NULL);
11787 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
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);
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));
11804 * Return value of 1, the TCB is unlocked and most
11805 * likely gone, return value of 0, the TCP is still
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)
11813 int32_t ret_val = 0;
11814 struct tcp_rack *rack;
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));
11822 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11823 * synchronized state.
11825 if (thflags & TH_SYN) {
11826 ctf_challenge_ack(m, th, tp, &ret_val);
11830 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11831 * it's less than ts_recent, drop it.
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))
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)) {
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.
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;
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.
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));
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;
11879 ctf_do_drop(m, NULL);
11886 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
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);
11898 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
11899 tiwin, thflags, nxt_pkt));
11903 rack_check_data_after_close(struct mbuf *m,
11904 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
11906 struct tcp_rack *rack;
11908 rack = (struct tcp_rack *)tp->t_fb_ptr;
11909 if (rack->rc_allow_data_af_clo == 0) {
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));
11919 if (sbavail(&so->so_snd) == 0)
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;
11931 * Return value of 1, the TCB is unlocked and most
11932 * likely gone, return value of 0, the TCP is still
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)
11940 int32_t ret_val = 0;
11941 int32_t ourfinisacked = 0;
11942 struct tcp_rack *rack;
11944 rack = (struct tcp_rack *)tp->t_fb_ptr;
11945 ctf_calc_rwin(so, tp);
11947 if ((thflags & TH_RST) ||
11948 (tp->t_fin_is_rst && (thflags & TH_FIN)))
11949 return (ctf_process_rst(m, th, so, tp));
11951 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
11952 * synchronized state.
11954 if (thflags & TH_SYN) {
11955 ctf_challenge_ack(m, th, tp, &ret_val);
11959 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
11960 * it's less than ts_recent, drop it.
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))
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)) {
11973 * If new data are received on a connection after the user processes
11974 * are gone, then RST the other end.
11976 if ((so->so_state & SS_NOFDREF) && tlen) {
11977 if (rack_check_data_after_close(m, tp, &tlen, th, so))
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.
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;
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.
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;
12015 ctf_do_drop(m, NULL);
12022 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12025 if (ourfinisacked) {
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
12032 * XXXjl: we should release the tp also, and use a
12033 * compressed state.
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 :
12042 tcp_state_change(tp, TCPS_FIN_WAIT_2);
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);
12053 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12054 tiwin, thflags, nxt_pkt));
12058 * Return value of 1, the TCB is unlocked and most
12059 * likely gone, return value of 0, the TCP is still
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)
12067 int32_t ret_val = 0;
12068 int32_t ourfinisacked = 0;
12069 struct tcp_rack *rack;
12071 rack = (struct tcp_rack *)tp->t_fb_ptr;
12072 ctf_calc_rwin(so, tp);
12074 if ((thflags & TH_RST) ||
12075 (tp->t_fin_is_rst && (thflags & TH_FIN)))
12076 return (ctf_process_rst(m, th, so, tp));
12078 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12079 * synchronized state.
12081 if (thflags & TH_SYN) {
12082 ctf_challenge_ack(m, th, tp, &ret_val);
12086 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12087 * it's less than ts_recent, drop it.
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))
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)) {
12100 * If new data are received on a connection after the user processes
12101 * are gone, then RST the other end.
12103 if ((so->so_state & SS_NOFDREF) && tlen) {
12104 if (rack_check_data_after_close(m, tp, &tlen, th, so))
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.
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;
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.
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;
12142 ctf_do_drop(m, NULL);
12149 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12152 if (ourfinisacked) {
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);
12166 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12167 tiwin, thflags, nxt_pkt));
12171 * Return value of 1, the TCB is unlocked and most
12172 * likely gone, return value of 0, the TCP is still
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)
12180 int32_t ret_val = 0;
12181 int32_t ourfinisacked = 0;
12182 struct tcp_rack *rack;
12184 rack = (struct tcp_rack *)tp->t_fb_ptr;
12185 ctf_calc_rwin(so, tp);
12187 if ((thflags & TH_RST) ||
12188 (tp->t_fin_is_rst && (thflags & TH_FIN)))
12189 return (ctf_process_rst(m, th, so, tp));
12191 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12192 * synchronized state.
12194 if (thflags & TH_SYN) {
12195 ctf_challenge_ack(m, th, tp, &ret_val);
12199 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12200 * it's less than ts_recent, drop it.
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))
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)) {
12213 * If new data are received on a connection after the user processes
12214 * are gone, then RST the other end.
12216 if ((so->so_state & SS_NOFDREF) && tlen) {
12217 if (rack_check_data_after_close(m, tp, &tlen, th, so))
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.
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;
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.
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;
12255 ctf_do_drop(m, NULL);
12260 * case TCPS_LAST_ACK: Ack processing.
12262 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
12265 if (ourfinisacked) {
12266 tp = tcp_close(tp);
12267 ctf_do_drop(m, tp);
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);
12279 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12280 tiwin, thflags, nxt_pkt));
12284 * Return value of 1, the TCB is unlocked and most
12285 * likely gone, return value of 0, the TCP is still
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)
12293 int32_t ret_val = 0;
12294 int32_t ourfinisacked = 0;
12295 struct tcp_rack *rack;
12297 rack = (struct tcp_rack *)tp->t_fb_ptr;
12298 ctf_calc_rwin(so, tp);
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));
12305 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
12306 * synchronized state.
12308 if (thflags & TH_SYN) {
12309 ctf_challenge_ack(m, th, tp, &ret_val);
12313 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
12314 * it's less than ts_recent, drop it.
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))
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)) {
12327 * If new data are received on a connection after the user processes
12328 * are gone, then RST the other end.
12330 if ((so->so_state & SS_NOFDREF) &&
12332 if (rack_check_data_after_close(m, tp, &tlen, th, so))
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.
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;
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.
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;
12370 ctf_do_drop(m, NULL);
12377 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
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);
12389 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
12390 tiwin, thflags, nxt_pkt));
12394 rack_clear_rate_sample(struct tcp_rack *rack)
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;
12402 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
12404 uint64_t bw_est, rate_wanted;
12406 uint32_t user_max, orig_min, orig_max;
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)
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)
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;
12426 /* We are pacing right at the hardware rate */
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);
12435 } else if (rack->rc_always_pace) {
12436 if (rack->r_ctl.gp_bw ||
12437 #ifdef NETFLIX_PEAKRATE
12438 rack->rc_tp->t_maxpeakrate ||
12440 rack->r_ctl.init_rate) {
12441 /* We have a rate of some sort set */
12444 bw_est = rack_get_bw(rack);
12445 orig = rack->r_ctl.rc_pace_max_segs;
12447 rate_wanted = *fill_override;
12449 rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL);
12451 /* We have something */
12452 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
12454 ctf_fixed_maxseg(rack->rc_tp));
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)
12459 } else if ((rack->r_ctl.gp_bw == 0) &&
12460 (rack->r_ctl.rc_pace_max_segs == 0)) {
12462 * If we have nothing limit us to bursting
12463 * out IW sized pieces.
12466 rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
12469 if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
12471 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
12474 rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
12479 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack)
12482 struct ip6_hdr *ip6 = NULL;
12485 struct ip *ip = NULL;
12487 struct udphdr *udp = NULL;
12489 /* Ok lets fill in the fast block, it can only be used with no IP options! */
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;
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);
12503 rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
12504 rack->r_ctl.fsb.udp = NULL;
12506 tcpip_fillheaders(rack->rc_inp,
12508 ip6, rack->r_ctl.fsb.th);
12512 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
12513 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
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);
12523 rack->r_ctl.fsb.udp = NULL;
12524 rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
12526 tcpip_fillheaders(rack->rc_inp,
12528 ip, rack->r_ctl.fsb.th);
12530 rack->r_fsb_inited = 1;
12534 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
12537 * Allocate the larger of spaces V6 if available else just
12538 * V4 and include udphdr (overbook)
12541 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
12543 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
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) {
12550 rack->r_fsb_inited = 0;
12555 rack_init(struct tcpcb *tp)
12557 struct tcp_rack *rack = NULL;
12558 struct rack_sendmap *insret;
12559 uint32_t iwin, snt, us_cts;
12562 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
12563 if (tp->t_fb_ptr == NULL) {
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 :(
12572 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
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);
12579 rack->rc_inp = tp->t_inpcb;
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);
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.
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;
12605 rack->use_rack_rr = 1;
12606 if (V_tcp_delack_enabled)
12607 tp->t_delayed_ack = 1;
12609 tp->t_delayed_ack = 0;
12610 #ifdef TCP_ACCOUNTING
12611 if (rack_tcp_accounting) {
12612 tp->t_flags2 |= TF2_TCP_ACCOUNTING;
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);
12640 rack->rc_always_pace = 0;
12641 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
12642 rack->r_mbuf_queue = 1;
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;
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;
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;
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;
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;
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;
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;
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);
12714 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12715 tp->t_fb_ptr = NULL;
12718 if (tp->snd_una != tp->snd_max) {
12719 /* Create a send map for the current outstanding data */
12720 struct rack_sendmap *rsm;
12722 rsm = rack_alloc(rack);
12724 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
12725 tp->t_fb_ptr = NULL;
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;
12736 rsm->r_end = tp->snd_max;
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);
12744 rsm->r_start = tp->snd_una;
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);
12749 rsm->orig_m_len = rsm->m->m_len;
12751 rsm->orig_m_len = 0;
12754 * This can happen if we have a stand-alone FIN or
12758 rsm->orig_m_len = 0;
12761 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
12763 if (insret != NULL) {
12764 panic("Insert in rb tree fails ret:%p rack:%p rsm:%p",
12765 insret, rack, rsm);
12768 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
12769 rsm->r_in_tmap = 1;
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.
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;
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);
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;
12801 iwin = rc_init_window(rack);
12803 /* We are not past the initial window
12804 * so we need to make sure cwnd is
12807 if (tp->snd_cwnd < iwin)
12808 tp->snd_cwnd = iwin;
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.
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.
12829 tp->snd_ssthresh = 0xffffffff;
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);
12840 rack_handoff_ok(struct tcpcb *tp)
12842 if ((tp->t_state == TCPS_CLOSED) ||
12843 (tp->t_state == TCPS_LISTEN)) {
12844 /* Sure no problem though it may not stick */
12847 if ((tp->t_state == TCPS_SYN_SENT) ||
12848 (tp->t_state == TCPS_SYN_RECEIVED)) {
12850 * We really don't know if you support sack,
12851 * you have to get to ESTAB or beyond to tell.
12855 if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
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)!
12868 if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
12872 * If we reach here we don't do SACK on this connection so we can
12880 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
12884 if (tp->t_fb_ptr) {
12885 struct tcp_rack *rack;
12886 struct rack_sendmap *rsm, *nrsm, *rm;
12888 rack = (struct tcp_rack *)tp->t_fb_ptr;
12889 if (tp->t_in_pkt) {
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.
12898 struct mbuf *save, *m;
12901 tp->t_in_pkt = NULL;
12902 tp->t_tail_pkt = NULL;
12904 save = m->m_nextpkt;
12905 m->m_nextpkt = NULL;
12909 if ((tp->t_inpcb) &&
12910 (tp->t_inpcb->inp_flags2 & INP_MBUF_ACKCMP))
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);
12917 counter_u64_add(rack_small_ackcmp, 1);
12920 tp->t_flags &= ~TF_FORCEDATA;
12921 #ifdef NETFLIX_SHARED_CWND
12922 if (rack->r_ctl.rc_scw) {
12925 if (rack->r_limit_scw)
12926 limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
12929 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
12930 rack->r_ctl.rc_scw_index,
12932 rack->r_ctl.rc_scw = NULL;
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;
12940 /* Convert back to ticks, with */
12941 if (tp->t_srtt > 1) {
12942 uint32_t val, frac;
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;
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
12955 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12957 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12959 tp->t_srtt += frac;
12962 if (tp->t_rttvar) {
12963 uint32_t val, frac;
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;
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
12976 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
12978 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
12980 tp->t_rttvar += frac;
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;
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;
12994 dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
12995 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
12996 free(dol, M_TCPDO);
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;
13004 #ifdef TCP_BLACKBOX
13005 tcp_log_flowend(tp);
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);
13011 panic("At fini, rack:%p rsm:%p rm:%p",
13015 uma_zfree(rack_zone, rsm);
13017 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
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);
13023 rack->rc_free_cnt = 0;
13024 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
13025 tp->t_fb_ptr = NULL;
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;
13036 /* Make sure snd_nxt is correctly set */
13037 tp->snd_nxt = tp->snd_max;
13041 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
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;
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;
13051 case TCPS_SYN_RECEIVED:
13052 rack->r_state = TCPS_SYN_RECEIVED;
13053 rack->r_substate = rack_do_syn_recv;
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;
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;
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;
13071 rack_set_pace_segments(tp, rack, __LINE__, NULL);
13072 rack->r_state = TCPS_CLOSING;
13073 rack->r_substate = rack_do_closing;
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;
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;
13087 case TCPS_TIME_WAIT:
13091 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
13092 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
13097 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
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?
13105 struct rack_sendmap *rsm;
13108 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
13109 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
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 */
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 */
13123 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
13125 * if we hit enobufs then we would expect the possiblity
13126 * of nothing outstanding and the RXT up (and the hptsi timer).
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 */
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))) {
13143 * Either a Rack, TLP or RXT is fine if we
13144 * have outstanding data.
13147 } else if (tmr_up == PACE_TMR_DELACK) {
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
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.
13162 if (rack->rc_inp->inp_in_hpts) {
13163 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
13166 us_cts = tcp_get_usecs(NULL);
13167 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
13169 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
13171 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
13173 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
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);
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)
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);
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);
13202 /* Not a valid win update */
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);
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)) {
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.
13231 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
13236 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
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];
13244 struct timespec ts;
13245 uint32_t orig_snd_una;
13248 #ifdef NETFLIX_HTTP_LOGGING
13249 struct http_sendfile_track *http_req;
13251 if (SEQ_GT(ae->ack, tp->snd_una)) {
13252 http_req = tcp_http_find_req_for_seq(tp, (ae->ack-1));
13254 http_req = tcp_http_find_req_for_seq(tp, ae->ack);
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;
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(<v);
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(<v);
13292 log.u_bbr.timeStamp = tcp_get_usecs(<v);
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;
13302 /* Copy out any client req info */
13304 log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
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;
13312 log.u_bbr.flex8 |= 2;
13313 log.u_bbr.bw_inuse = http_req->end;
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;
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) {
13336 th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
13337 cp = (u_char *)(th + 1);
13342 *cp = TCPOPT_TIMESTAMP;
13344 *cp = TCPOLEN_TIMESTAMP;
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));
13353 th->th_off = (sizeof(struct tcphdr) >> 2);
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.
13365 if (tp->snd_una != high_seq) {
13366 orig_snd_una = tp->snd_una;
13367 tp->snd_una = high_seq;
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, <v);
13376 tp->snd_una = orig_snd_una;
13383 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
13386 * Handle a "special" compressed ack mbuf. Each incoming
13387 * ack has only four possible dispositions:
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.
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
13399 #ifdef TCP_ACCOUNTING
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;
13412 int under_pacing = 1;
13415 #ifdef TCP_ACCOUNTING
13418 rack = (struct tcp_rack *)tp->t_fb_ptr;
13419 if (rack->gp_ready &&
13420 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
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)) {
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?
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)) {
13442 * There are not enough bytes in the socket
13443 * buffer that have been sent to cover this
13444 * measurement. Cancel it.
13446 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
13447 rack->r_ctl.rc_gp_srtt /*flex1*/,
13449 0, 0, 18, __LINE__, NULL, 0);
13450 tp->t_flags &= ~TF_GPUTINPROG;
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);
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);
13476 for (i = 0; i < cnt; i++) {
13477 #ifdef TCP_ACCOUNTING
13478 ts_val = get_cyclecount();
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)) {
13487 ae->ack_val_set = ACK_BEHIND;
13488 } else if (SEQ_GT(ae->ack, high_seq)) {
13490 ae->ack_val_set = ACK_CUMACK;
13491 } else if (tiwin == the_win) {
13493 ae->ack_val_set = ACK_DUPACK;
13496 ae->ack_val_set = ACK_RWND;
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;
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.
13511 if (TSTMP_GT(ae->ts_echo, ms_cts))
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] ,
13521 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
13522 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
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;
13535 /* Setup for a no options */
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);
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;
13552 switch (ae->codepoint & IPTOS_ECN_MASK) {
13554 tp->t_flags2 |= TF2_ECN_SND_ECE;
13555 KMOD_TCPSTAT_INC(tcps_ecn_ce);
13557 case IPTOS_ECN_ECT0:
13558 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
13560 case IPTOS_ECN_ECT1:
13561 KMOD_TCPSTAT_INC(tcps_ecn_ect1);
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);
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]++;
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.
13590 if (ae->ack_val_set == ACK_BEHIND) {
13592 * Case B flag reordering, if window is not closed
13593 * or it could be a keep-alive or persists
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);
13599 } else if (ae->ack_val_set == ACK_DUPACK) {
13601 rack_strike_dupack(rack);
13602 } else if (ae->ack_val_set == ACK_RWND) {
13605 win_upd_ack = ae->ack;
13608 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
13611 if (SEQ_GT(ae->ack, tp->snd_max)) {
13613 * We just send an ack since the incoming
13614 * ack is beyond the largest seq we sent.
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;
13623 /* If the window changed setup to update */
13624 if (tiwin != tp->snd_wnd) {
13625 win_upd_ack = ae->ack;
13628 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq);
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);
13635 counter_u64_add(tcp_cnt_counters[CNT_OF_ACKS_IN],
13636 (((ae->ack - high_seq) + segsiz - 1) / segsiz));
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);
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;
13653 rack->r_ctl.act_rcv_time = *tv;
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)) {
13660 rack->rc_dsack_round_seen = 0;
13661 rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
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);
13680 #ifdef TCP_ACCOUNTING
13681 ts_val = get_cyclecount();
13683 acked_amount = acked = (high_seq - tp->snd_una);
13685 if (rack->sack_attack_disable == 0)
13686 rack_do_decay(rack);
13687 if (acked >= segsiz) {
13689 * You only get credit for
13690 * MSS and greater (and you get extra
13691 * credit for larger cum-ack moves).
13695 ac = acked / segsiz;
13696 rack->r_ctl.ack_count += ac;
13697 counter_u64_add(rack_ack_total, ac);
13699 if (rack->r_ctl.ack_count > 0xfff00000) {
13701 * reduce the number to keep us under
13704 rack->r_ctl.ack_count /= 2;
13705 rack->r_ctl.sack_count /= 2;
13707 if (tp->t_flags & TF_NEEDSYN) {
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.
13714 tp->t_flags &= ~TF_NEEDSYN;
13716 acked_amount = acked = (high_seq - tp->snd_una);
13718 if (acked > sbavail(&so->so_snd))
13719 acked_amount = sbavail(&so->so_snd);
13720 #ifdef NETFLIX_EXP_DETECTION
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.
13727 if (rack->sack_attack_disable)
13728 rack_do_detection(tp, rack, acked_amount, segsiz);
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);
13738 rack_post_recovery(tp, high_seq);
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)) {
13748 * We must be sure our fin
13749 * was sent and acked (we can be
13750 * in FIN_WAIT_1 without having
13755 * Lets make sure snd_una is updated
13756 * since most likely acked_amount = 0 (it
13759 tp->snd_una = high_seq;
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);
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;
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);
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;
13802 * If the RXT timer is running we want to
13803 * stop it, so we can restart a TLP (or new RXT).
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);
13810 tp->snd_wl2 = high_seq;
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);
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)
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)) {
13838 * The socket was gone and the
13839 * peer sent data (not now in the past), time to
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);
13856 tp = tcp_close(tp);
13858 #ifdef TCP_ACCOUNTING
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.
13870 * Note if tcp_accounting is on we don't unpin since
13871 * we do that after the goto label.
13873 goto send_out_a_rst;
13875 if ((sbused(&so->so_snd) == 0) &&
13876 (tp->t_state >= TCPS_FIN_WAIT_1) &&
13877 (tp->t_flags & TF_SENTFIN)) {
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
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 :
13892 if (ourfinisacked == 0) {
13894 * We don't change to fin-wait-2 if we have our fin acked
13895 * which means we are probably in TCPS_CLOSING.
13897 tcp_state_change(tp, TCPS_FIN_WAIT_2);
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);
13909 * We cheat here and don't send a RST, we should send one
13910 * when the pacer drops the connection.
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);
13923 INP_WUNLOCK(rack->rc_inp);
13928 if (ourfinisacked) {
13929 switch(tp->t_state) {
13931 #ifdef TCP_ACCOUNTING
13932 rdstc = get_cyclecount();
13933 if (rdstc > ts_val) {
13934 counter_u64_add(tcp_proc_time[ACK_CUMACK] ,
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);
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] ,
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);
13960 tp = tcp_close(tp);
13961 ctf_do_drop(m, tp);
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] ,
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);
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 :
13983 tcp_state_change(tp, TCPS_FIN_WAIT_2);
13989 if (rack->r_fast_output) {
13991 * We re doing fast output.. can we expand that?
13993 rack_gain_for_fastoutput(rack, tp, so, acked_amount);
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);
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);
14015 /* Now is there a next packet, if so we are done */
14019 #ifdef TCP_ACCOUNTING
14022 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
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)) {
14029 (void)tp->t_fb->tfb_tcp_output(tp);
14032 rack_free_trim(rack);
14033 #ifdef TCP_ACCOUNTING
14036 rack_timer_audit(tp, rack, &so->so_snd);
14037 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
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)
14047 #ifdef TCP_ACCOUNTING
14050 int32_t thflags, retval, did_out = 0;
14051 int32_t way_out = 0;
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.
14057 uint32_t cts, us_cts, ms_cts;
14059 struct timespec ts;
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;
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.
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));
14076 if (m->m_flags & M_ACKCMP) {
14077 panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
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
14086 if (thflags & TH_ACK)
14087 ts_val = get_cyclecount();
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;
14095 rack->r_ctl.act_rcv_time = *tv;
14096 kern_prefetch(rack, &prev_state);
14099 * Unscale the window into a 32-bit value. For the SYN_SENT state
14100 * the scale is zero.
14102 tiwin = th->th_win << tp->snd_scale;
14103 #ifdef TCP_ACCOUNTING
14104 if (thflags & TH_ACK) {
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.
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.
14122 ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
14123 ctf_fixed_maxseg(tp));
14127 * Parse options on any incoming segment.
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",
14137 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
14139 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
14140 (tp->t_flags & TF_GPUTINPROG)) {
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?
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)) {
14154 * There are not enough bytes in the socket
14155 * buffer that have been sent to cover this
14156 * measurement. Cancel it.
14158 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
14159 rack->r_ctl.rc_gp_srtt /*flex1*/,
14161 0, 0, 18, __LINE__, NULL, 0);
14162 tp->t_flags &= ~TF_GPUTINPROG;
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;
14171 if (SEQ_GT(th->th_ack, tp->snd_una)) {
14172 http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1));
14174 http_req = tcp_http_find_req_for_seq(tp, th->th_ack);
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;
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;
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(<v);
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(<v);
14211 log.u_bbr.timeStamp = tcp_get_usecs(<v);
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;
14221 /* Copy out any client req info */
14223 log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
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;
14231 log.u_bbr.flex8 |= 2;
14232 log.u_bbr.bw_inuse = http_req->end;
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;
14241 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
14242 tlen, &log, true, <v);
14244 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
14248 goto done_with_input;
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.
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
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
14268 * See section 3.2 of RFC 7323.
14270 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
14271 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
14275 goto done_with_input;
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.
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);
14289 tp->t_rcvtime = ticks;
14291 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
14293 if (tiwin > rack->r_ctl.rc_high_rwnd)
14294 rack->r_ctl.rc_high_rwnd = tiwin;
14296 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
14297 * this to occur after we've validated the segment.
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;
14304 switch (iptos & IPTOS_ECN_MASK) {
14306 tp->t_flags2 |= TF2_ECN_SND_ECE;
14307 KMOD_TCPSTAT_INC(tcps_ecn_ce);
14309 case IPTOS_ECN_ECT0:
14310 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
14312 case IPTOS_ECN_ECT1:
14313 KMOD_TCPSTAT_INC(tcps_ecn_ect1);
14317 /* Process a packet differently from RFC3168. */
14318 cc_ecnpkt_handler(tp, th, iptos);
14320 /* Congestion experienced. */
14321 if (thflags & TH_ECE) {
14322 rack_cong_signal(tp, CC_ECN, th->th_ack);
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.
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))
14338 * If its the first time in we need to take care of options and
14339 * verify we can do SACK for rack!
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;
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.
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);
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;
14370 tp->t_flags &= ~TF_REQ_SCALE;
14372 * Initial send window. It will be updated with the
14373 * next incoming segment to the scaled value.
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;
14383 tp->t_flags &= ~TF_REQ_TSTMP;
14384 if (to.to_flags & TOF_MSS) {
14385 tcp_mss(tp, to.to_mss);
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) {
14394 if (to.to_flags & TOF_MSS)
14397 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
14401 tcp_fastopen_update_cache(tp, mss,
14402 to.to_tfo_len, to.to_tfo_cookie);
14404 tcp_fastopen_disable_path(tp);
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
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,
14421 #ifdef TCP_ACCOUNTING
14426 tcp_set_hpts(tp->t_inpcb);
14427 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
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);
14438 rack_clear_rate_sample(rack);
14439 if (rack->forced_ack) {
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).
14450 rack->forced_ack = 0;
14451 us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
14454 rack_apply_updated_usrtt(rack, us_rtt, us_cts);
14455 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
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).
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);
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);
14481 * If retval is 1 the tcb is unlocked and most likely the tp
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)) {
14491 * If we are going for target, lets recheck before
14494 rack_check_probe_rtt(rack, us_cts);
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);
14504 tcp_rack_xmit_timer_commit(rack, tp);
14505 #ifdef TCP_ACCOUNTING
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.
14515 if (ack_val_set != 0xf) {
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);
14525 if (nxt_pkt == 0) {
14526 if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
14529 (void)tp->t_fb->tfb_tcp_output(tp);
14531 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
14532 rack_free_trim(rack);
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);
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 */
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)) {
14562 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
14565 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
14567 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
14569 if (late && (did_out == 0)) {
14571 * We are late in the sending
14572 * and we did not call the output
14573 * (this probably should not happen).
14575 goto do_output_now;
14577 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
14580 } else if (nxt_pkt == 0) {
14581 /* Do we have the correct timer running? */
14582 rack_timer_audit(tp, rack, &so->so_snd);
14586 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
14588 rack->r_wanted_output = 0;
14590 if (tp->t_inpcb == NULL) {
14591 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
14593 retval, tp, prev_state);
14596 #ifdef TCP_ACCOUNTING
14599 * Track the time (see above).
14601 if (ack_val_set != 0xf) {
14604 crtsc = get_cyclecount();
14605 counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val));
14607 * Note we *DO NOT* increment the per-tcb counters since
14608 * in the else the TP may be gone!!
14613 #ifdef TCP_ACCOUNTING
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)
14625 /* First lets see if we have old packets */
14626 if (tp->t_in_pkt) {
14627 if (ctf_do_queued_segments(so, tp, 1)) {
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;
14636 /* Should not be should we kassert instead? */
14637 tcp_get_usecs(&tv);
14639 if (rack_do_segment_nounlock(m, th, so, tp,
14640 drop_hdrlen, tlen, iptos, 0, &tv) == 0) {
14641 INP_WUNLOCK(tp->t_inpcb);
14645 struct rack_sendmap *
14646 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
14648 struct rack_sendmap *rsm = NULL;
14650 uint32_t srtt = 0, thresh = 0, ts_low = 0;
14652 /* Return the next guy to be re-transmitted */
14653 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
14656 if (tp->t_flags & TF_SENTFIN) {
14657 /* retran the end FIN? */
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)) {
14665 rsm = rack_find_lowest_rsm(rack);
14670 if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) &&
14671 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
14673 * No sack so we automatically do the 3 strikes and
14674 * retransmit (no rack timer would be started).
14679 if (rsm->r_flags & RACK_ACKED) {
14682 if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
14683 (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
14684 /* Its not yet ready */
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 */
14696 if ((tsused - ts_low) < thresh) {
14697 /* It has not been long enough yet */
14700 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
14701 ((rsm->r_flags & RACK_SACK_PASSED) &&
14702 (rack->sack_attack_disable == 0))) {
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.
14710 /* log retransmit reason */
14711 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
14712 rack->r_fast_output = 0;
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)
14723 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
14724 union tcp_log_stackspecific log;
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;
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;
14767 /* we are on congestion avoidance */
14768 log.u_bbr.flex7 = 0;
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);
14788 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
14790 uint32_t new_tso, user_max;
14792 user_max = rack->rc_user_set_max_segs * mss;
14793 if (rack->rc_force_max_seg) {
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 */
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;
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)
14811 uint64_t lentim, fill_bw;
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)
14817 if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
14819 if (rack->r_ctl.rc_last_us_rtt == 0)
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 */
14828 * first lets calculate the b/w based on the last us-rtt
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) {
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).
14841 fill_bw -= ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
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 */
14848 *rate_wanted = fill_bw;
14849 if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
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;
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;
14865 rack_log_hdwr_pacing(rack,
14866 fill_bw, high_rate, __LINE__,
14868 fill_bw = high_rate;
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)) {
14880 * Ok we may have a first attempt that is greater than our top rate
14883 uint64_t high_rate;
14885 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
14887 if (fill_bw > high_rate) {
14888 fill_bw = high_rate;
14895 * Ok fill_bw holds our mythical b/w to fill the cwnd
14896 * in a rtt, what does that time wise equate too?
14898 lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
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);
14910 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
14913 int can_start_hw_pacing = 1;
14916 if (rack->rc_always_pace == 0) {
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.
14924 uint32_t srtt, cwnd, tr_perms = 0;
14925 int32_t reduce = 0;
14929 * We keep no precise pacing with the old method
14930 * instead we use the pacer to mitigate bursts.
14932 if (rack->r_ctl.rc_rack_min_rtt)
14933 srtt = rack->r_ctl.rc_rack_min_rtt;
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;
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);
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
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) {
14963 slot *= HPTS_USEC_IN_MSEC;
14964 if (rack->rc_pace_to_cwnd) {
14965 uint64_t rate_wanted = 0;
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);
14971 rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
14973 uint64_t bw_est, res, lentim, rate_wanted;
14974 uint32_t orig_val, srtt, segs, oh;
14978 if ((rack->r_rr_config == 1) && rsm) {
14979 return (rack->r_ctl.rc_min_to);
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) &&
14987 (rack->r_ctl.gp_bw == 0)) {
14988 /* no way to yet do an estimate */
14989 bw_est = rate_wanted = 0;
14991 bw_est = rack_get_bw(rack);
14992 rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
14994 if ((bw_est == 0) || (rate_wanted == 0) ||
14995 ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
14997 * No way yet to make a b/w estimate or
14998 * our raise is set incorrectly.
15002 /* We need to account for all the overheads */
15003 segs = (len + segsiz - 1) / segsiz;
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.
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;
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) {
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
15029 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
15030 } else if (rack->rc_inp->inp_snd_tag == NULL) {
15032 * We lost our rate somehow, this can happen
15033 * if the interface changed underneath us.
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__,
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) &&
15050 (rack->use_fixed_rate == 0) &&
15051 (rack->in_probe_rtt == 0) &&
15052 (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
15054 * We want to pace at our rate *or* faster to
15055 * fill the cwnd to the max if its not full.
15057 slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
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)) {
15066 * Lets attempt to turn on hardware pacing
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,
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,
15080 rack_log_hdwr_pacing(rack,
15081 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15083 rack->r_ctl.last_hw_bw_req = rate_wanted;
15085 counter_u64_add(rack_hw_pace_init_fail, 1);
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;
15092 if (rack->r_up_only &&
15093 (rate_wanted < rack->r_ctl.crte->rate)) {
15095 * We have four possible states here
15096 * having to do with the previous 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
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
15109 if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
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)) {
15117 * The pacing rate is too low for hardware, but
15118 * do allow hardware pacing to be restarted.
15120 rack_log_hdwr_pacing(rack,
15121 bw_est, rack->r_ctl.crte->rate, __LINE__,
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);
15130 nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
15132 rack->rc_inp->inp_route.ro_nh->nh_ifp,
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__,
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,
15151 rack_log_hdwr_pacing(rack,
15152 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
15154 rack->r_ctl.last_hw_bw_req = rate_wanted;
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__,
15162 rack->r_ctl.last_hw_bw_req = rate_wanted;
15166 if ((rack->r_ctl.crte != NULL) &&
15167 (rack->r_ctl.crte->rate == rate_wanted)) {
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
15176 slot += (rack->r_ctl.crte->time_between * rack_hw_pace_extra_slots);
15179 if (rack_limit_time_with_srtt &&
15180 (rack->use_fixed_rate == 0) &&
15181 #ifdef NETFLIX_PEAKRATE
15182 (rack->rc_tp->t_maxpeakrate == 0) &&
15184 (rack->rack_hdrw_pacing == 0)) {
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.
15192 * Note this restriction is not for where a peak rate
15193 * is set, we are doing fixed pacing or hardware pacing.
15195 if (rack->rc_tp->t_srtt)
15196 srtt = rack->rc_tp->t_srtt;
15198 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC; /* its in ms convert */
15200 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
15204 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
15206 if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
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.
15214 int hw_boost_delay;
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;
15224 counter_u64_add(rack_calc_nonzero, 1);
15226 counter_u64_add(rack_calc_zero, 1);
15231 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
15232 tcp_seq startseq, uint32_t sb_offset)
15234 struct rack_sendmap *my_rsm = NULL;
15235 struct rack_sendmap fe;
15237 if (tp->t_state < TCPS_ESTABLISHED) {
15239 * We don't start any measurements if we are
15240 * not at least established.
15244 if (tp->t_state >= TCPS_FIN_WAIT_1) {
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.
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 */
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);
15271 /* Special case initial measurement */
15274 tp->gput_ts = tcp_get_usecs(&tv);
15275 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
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.
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,
15294 rack->r_ctl.rc_app_limited_cnt,
15296 __LINE__, NULL, 0);
15301 * We are out somewhere in the sb
15302 * can we use the already outstanding data?
15304 if (rack->r_ctl.rc_app_limited_cnt == 0) {
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).
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? */
15318 if (rack->r_ctl.rc_first_appl == NULL) {
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.
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).
15333 rack->app_limited_needs_set = 1;
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).
15339 my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
15340 rack->r_ctl.rc_first_appl);
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,
15347 /* Use after the first MSS of it is acked */
15348 tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
15352 if ((my_rsm == NULL) ||
15353 (my_rsm->r_rtr_cnt != 1)) {
15355 * Either its a retransmit or
15356 * the last is the app-limited one.
15361 tp->gput_seq = my_rsm->r_start;
15363 if (my_rsm->r_flags & RACK_ACKED) {
15365 * This one has been acked use the arrival ack time
15367 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15368 rack->app_limited_needs_set = 0;
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,
15377 rack->r_ctl.rc_app_limited_cnt,
15379 __LINE__, NULL, 0);
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
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);
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) {
15400 * Unlikely since its probably what was
15401 * just transmitted (but I am paranoid).
15403 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
15404 rack->app_limited_needs_set = 0;
15406 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
15407 /* This also is unlikely */
15408 tp->gput_seq = my_rsm->r_start;
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).
15419 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
15421 rack_log_pacing_delay_calc(rack,
15426 rack->r_ctl.rc_app_limited_cnt,
15427 9, __LINE__, NULL, 0);
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)
15437 if (tp->snd_wnd > cwnd_to_use)
15438 sendwin = cwnd_to_use;
15440 sendwin = tp->snd_wnd;
15441 if (ctf_outstanding(tp) >= tp->snd_wnd) {
15442 /* We never want to go over our peers rcv-window */
15447 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15448 if (flight >= sendwin) {
15450 * We have in flight what we are allowed by cwnd (if
15451 * it was rwnd blocking it would have hit above out
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);
15461 if ((len + sb_offset) > avail) {
15463 * We don't have that much in the SB, how much is
15466 len = avail - sb_offset;
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)
15477 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15478 union tcp_log_stackspecific log;
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);
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)
15513 struct ktls_session *tls, *ntls;
15514 struct mbuf *start;
15516 struct mbuf *m, *n, **np, *smb;
15519 int32_t len = *plen;
15521 int32_t len_cp = 0;
15522 uint32_t mlen, frags;
15524 soff = off = the_off;
15529 if (hw_tls && (m->m_flags & M_EXTPG))
15530 tls = m->m_epg_tls;
15542 if (m->m_flags & M_EXTPG)
15543 ntls = m->m_epg_tls;
15548 * Avoid mixing TLS records with handshake
15549 * data or TLS records from different
15559 mlen = min(len, m->m_len - off);
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.
15569 if (m->m_flags & M_EXTPG) {
15570 fragsize = min(segsize, PAGE_SIZE);
15573 fragsize = segsize;
15577 /* Break if we really can't fit anymore. */
15578 if ((frags + 1) >= seglimit) {
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
15589 if ((frags + howmany(mlen, fragsize)) >= seglimit) {
15590 mlen = (seglimit - frags - 1) * fragsize;
15592 *plen = len_cp + len;
15594 frags += howmany(mlen, fragsize);
15598 KASSERT(seglimit > 0,
15599 ("%s: seglimit went too low", __func__));
15601 n = m_get(M_NOWAIT, m->m_type);
15607 len_cp += n->m_len;
15608 if (m->m_flags & (M_EXT|M_EXTPG)) {
15609 n->m_data = m->m_data + off;
15612 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
15619 if (len || (soff == smb->m_len)) {
15621 * We have more so we move forward or
15622 * we have consumed the entire mbuf and
15623 * len has fell to 0.
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
15640 fsb->o_m_len = smb->m_len;
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
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.
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)
15669 struct mbuf *m, *n;
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) {
15676 * The mbuf had the front of it chopped off by an ack
15677 * we need to adjust the soff/off by that difference.
15681 delta = rack->r_ctl.fsb.o_m_len - m->m_len;
15683 } else if (rack->r_ctl.fsb.o_m_len < m->m_len) {
15685 * The mbuf was expanded probably by
15686 * a m_compress. Just update o_m_len.
15688 rack->r_ctl.fsb.o_m_len = m->m_len;
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?",
15694 rack, *plen, m, m->m_len));
15695 /* Save off the right location before we copy and advance */
15697 *s_mb = rack->r_ctl.fsb.m;
15698 n = rack_fo_base_copym(m, soff, plen,
15700 seglimit, segsize, rack->r_ctl.fsb.hw_tls);
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)
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).
15716 struct ip *ip = NULL;
15717 struct udphdr *udp = NULL;
15718 struct tcphdr *th = NULL;
15719 struct mbuf *m = NULL;
15722 struct tcp_log_buffer *lgb;
15723 #ifdef TCP_ACCOUNTING
15728 u_char opt[TCP_MAXOLEN];
15729 uint32_t hdrlen, optlen;
15730 int32_t slot, segsiz, max_val, tso = 0, error, flags, ulen = 0;
15732 uint32_t if_hw_tsomaxsegcount = 0, startseq;
15733 uint32_t if_hw_tsomaxsegsize;
15736 struct ip6_hdr *ip6 = NULL;
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);
15744 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
15745 hdrlen = sizeof(struct tcpiphdr);
15747 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
15751 /* Its a TLP add the flag, it may already be there but be sure */
15752 rsm->r_flags |= RACK_TLP;
15754 /* If it was a TLP it is not not on this retransmit */
15755 rsm->r_flags &= ~RACK_TLP;
15757 startseq = rsm->r_start;
15758 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
15759 inp = rack->rc_inp;
15761 flags = tcp_outflags[tp->t_state];
15762 if (flags & (TH_SYN|TH_RST)) {
15765 if (rsm->r_flags & RACK_HAS_FIN) {
15766 /* We can't send a FIN here */
15769 if (flags & TH_FIN) {
15770 /* We never send a FIN */
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;
15778 optlen = tcp_addoptions(&to, opt);
15780 udp = rack->r_ctl.fsb.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;
15789 if ((tp->t_flags & TF_TSO) &&
15795 if (MHLEN < hdrlen + max_linkhdr)
15796 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
15799 m = m_gethdr(M_NOWAIT, MT_DATA);
15802 m->m_data += max_linkhdr;
15804 th = rack->r_ctl.fsb.th;
15805 /* Establish the len to send */
15808 if ((tso) && (len + optlen > tp->t_maxseg)) {
15809 uint32_t if_hw_tsomax;
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;
15817 * Check if we should limit by maximum payload
15820 if (if_hw_tsomax != 0) {
15821 /* compute maximum TSO length */
15822 max_len = (if_hw_tsomax - hdrlen -
15824 if (max_len <= 0) {
15826 } else if (len > max_len) {
15830 if (len <= segsiz) {
15832 * In case there are too many small fragments don't
15840 if ((tso == 0) && (len > segsiz))
15842 us_cts = tcp_get_usecs(tv);
15844 (len <= MHLEN - hdrlen - max_linkhdr)) {
15847 th->th_seq = htonl(rsm->r_start);
15848 th->th_ack = htonl(tp->rcv_nxt);
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
15856 if ((rsm->r_flags & RACK_HAD_PUSH) &&
15857 (len == (rsm->r_end - rsm->r_start)))
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;
15865 tp->t_flags &= ~TF_RXWIN0SENT;
15866 if (rsm->r_flags & RACK_TLP) {
15868 * TLP should not count in retran count, but
15871 counter_u64_add(rack_tlp_retran, 1);
15872 counter_u64_add(rack_tlp_retran_bytes, len);
15874 tp->t_sndrexmitpack++;
15875 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
15876 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
15879 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
15882 if (rsm->m == NULL)
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);
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) {
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
15898 if ((m->m_next == NULL) || (len <= 0)){
15903 ulen = hdrlen + len - sizeof(struct ip6_hdr);
15905 ulen = hdrlen + len - sizeof(struct ip);
15906 udp->uh_ulen = htons(ulen);
15908 m->m_pkthdr.rcvif = (struct ifnet *)0;
15909 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
15911 if (rack->r_is_v6) {
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);
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,
15927 #if defined(INET6) && defined(INET)
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);
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));
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));
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;
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;
15964 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
15967 #if defined(INET) && defined(INET6)
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);
15980 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
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));
15989 bcopy(opt, th + 1, optlen);
15990 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
15992 th->th_off = sizeof(struct tcphdr) >> 2;
15994 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
15995 union tcp_log_stackspecific log;
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;
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;
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);
16028 if (rack->r_is_v6) {
16029 error = ip6_output(m, NULL,
16031 0, NULL, NULL, inp);
16034 #if defined(INET) && defined(INET6)
16039 error = ip_output(m, NULL,
16046 lgb->tlb_errno = error;
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++;
16059 tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
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;
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;
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);
16078 counter_u64_add(rack_out_size[idx], 1);
16080 if (tp->t_rtttime == 0) {
16081 tp->t_rtttime = ticks;
16082 tp->t_rtseq = startseq;
16083 KMOD_TCPSTAT_INC(tcps_segstimed);
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)
16090 if (slot < (10 * HPTS_USEC_IN_MSEC))
16091 slot = 10 * HPTS_USEC_IN_MSEC;
16093 slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
16095 (rack->rc_always_pace == 0) ||
16096 (rack->r_rr_config == 1)) {
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.
16102 slot = rack->r_ctl.rc_min_to;
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)) {
16109 * We have retransmitted all we need.
16111 rack->r_must_retran = 0;
16112 rack->r_ctl.rc_out_at_rto = 0;
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;
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);
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);
16128 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((len + segsiz - 1) / segsiz));
16139 rack_sndbuf_autoscale(struct tcp_rack *rack)
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.
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.
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.
16166 uint32_t sendwin, scaleup;
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;
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;
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)
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.
16206 struct ip *ip = NULL;
16207 struct udphdr *udp = NULL;
16208 struct tcphdr *th = NULL;
16209 struct mbuf *m, *s_mb;
16212 struct tcp_log_buffer *lgb;
16213 #ifdef TCP_ACCOUNTING
16217 u_char opt[TCP_MAXOLEN];
16218 uint32_t hdrlen, optlen;
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;
16226 struct ip6_hdr *ip6 = NULL;
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);
16234 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
16235 hdrlen = sizeof(struct tcpiphdr);
16237 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
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;
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;
16252 optlen = tcp_addoptions(&to, opt);
16254 udp = rack->r_ctl.fsb.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;
16263 if ((tp->t_flags & TF_TSO) &&
16270 if (MHLEN < hdrlen + max_linkhdr)
16271 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
16274 m = m_gethdr(M_NOWAIT, MT_DATA);
16277 m->m_data += max_linkhdr;
16279 th = rack->r_ctl.fsb.th;
16280 /* Establish the len to send */
16283 if ((tso) && (len + optlen > tp->t_maxseg)) {
16284 uint32_t if_hw_tsomax;
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;
16292 * Check if we should limit by maximum payload
16295 if (if_hw_tsomax != 0) {
16296 /* compute maximum TSO length */
16297 max_len = (if_hw_tsomax - hdrlen -
16299 if (max_len <= 0) {
16301 } else if (len > max_len) {
16305 if (len <= segsiz) {
16307 * In case there are too many small fragments don't
16315 if ((tso == 0) && (len > segsiz))
16317 us_cts = tcp_get_usecs(tv);
16319 (len <= MHLEN - hdrlen - max_linkhdr)) {
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;
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);
16336 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
16339 if (rack->r_ctl.fsb.m == NULL)
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,
16345 if (len <= segsiz) {
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
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;
16359 if ((m->m_next == NULL) || (len <= 0)){
16364 ulen = hdrlen + len - sizeof(struct ip6_hdr);
16366 ulen = hdrlen + len - sizeof(struct ip);
16367 udp->uh_ulen = htons(ulen);
16369 m->m_pkthdr.rcvif = (struct ifnet *)0;
16370 if (tp->t_state == TCPS_ESTABLISHED &&
16371 (tp->t_flags2 & TF2_ECN_PERMIT)) {
16373 * If the peer has ECN, mark data packets with ECN capable
16374 * transmission (ECT). Ignore pure ack packets,
16377 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max)) {
16380 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
16383 ip->ip_tos |= IPTOS_ECN_ECT0;
16384 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
16386 * Reply with proper ECN notifications.
16387 * Only set CWR on new data segments.
16389 if (tp->t_flags2 & TF2_ECN_SND_CWR) {
16391 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
16394 if (tp->t_flags2 & TF2_ECN_SND_ECE)
16397 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
16399 if (rack->r_is_v6) {
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);
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,
16415 #if defined(INET6) && defined(INET)
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);
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));
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));
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;
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;
16452 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
16455 #if defined(INET) && defined(INET6)
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);
16468 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
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));
16477 bcopy(opt, th + 1, optlen);
16478 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
16480 th->th_off = sizeof(struct tcphdr) >> 2;
16482 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
16483 union tcp_log_stackspecific log;
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;
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);
16513 if (rack->r_is_v6) {
16514 error = ip6_output(m, NULL,
16516 0, NULL, NULL, inp);
16519 #if defined(INET) && defined(INET6)
16524 error = ip_output(m, NULL,
16530 lgb->tlb_errno = error;
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);
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;
16547 tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
16549 rack->forced_ack = 0; /* If we send something zap the FA flag */
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;
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);
16562 counter_u64_add(rack_out_size[idx], 1);
16564 if (len <= rack->r_ctl.fsb.left_to_send)
16565 rack->r_ctl.fsb.left_to_send -= len;
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);
16576 if (tp->t_rtttime == 0) {
16577 tp->t_rtttime = ticks;
16578 tp->t_rtseq = startseq;
16579 KMOD_TCPSTAT_INC(tcps_segstimed);
16581 if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
16586 th = rack->r_ctl.fsb.th;
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;
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);
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);
16607 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len + segsiz - 1) / segsiz));
16614 rack->r_fast_output = 0;
16619 rack_output(struct tcpcb *tp)
16623 uint32_t sb_offset, s_moff = 0;
16624 int32_t len, flags, error = 0;
16625 struct mbuf *m, *s_mb = NULL;
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;
16632 struct ip *ip = NULL;
16635 struct ipovly *ipov = NULL;
16637 struct udphdr *udp = NULL;
16638 struct tcp_rack *rack;
16642 uint8_t wanted_cookie = 0;
16643 u_char opt[TCP_MAXOLEN];
16644 unsigned ipoptlen, optlen, hdrlen, ulen=0;
16647 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
16648 unsigned ipsec_optlen = 0;
16651 int32_t idle, sendalot;
16652 int32_t sub_from_prr = 0;
16653 volatile int32_t sack_rxmit;
16654 struct rack_sendmap *rsm = NULL;
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;
16668 int32_t prefetch_so_done = 0;
16669 struct tcp_log_buffer *lgb;
16671 struct sockbuf *sb;
16672 uint64_t ts_val = 0;
16673 #ifdef TCP_ACCOUNTING
16677 struct ip6_hdr *ip6 = NULL;
16680 uint8_t filled_all = 0;
16681 bool hw_tls = false;
16683 /* setup and take the cache hits here */
16684 rack = (struct tcp_rack *)tp->t_fb_ptr;
16685 #ifdef TCP_ACCOUNTING
16687 ts_val = get_cyclecount();
16689 hpts_calling = rack->rc_inp->inp_hpts_calls;
16690 NET_EPOCH_ASSERT();
16691 INP_WLOCK_ASSERT(rack->rc_inp);
16693 if (tp->t_flags & TF_TOE) {
16694 #ifdef TCP_ACCOUNTING
16697 return (tcp_offload_output(tp));
16701 * For TFO connections in SYN_RECEIVED, only allow the initial
16702 * SYN|ACK and those sent by the retransmit timer.
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
16714 if (rack->r_state) {
16715 /* Use the cache line loaded if possible */
16716 isipv6 = rack->r_is_v6;
16718 isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 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) {
16727 * We are on the hpts for some timer but not hptsi output.
16728 * Remove from the hpts unconditionally.
16730 rack_timer_cancel(tp, rack, cts, __LINE__);
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;
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
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);
16755 #ifdef TCP_ACCOUNTING
16760 if ((rack->r_timer_override) ||
16761 (rack->rc_ack_can_sendout_data) ||
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) {
16769 * On the hpts you can't pass even if ACKNOW is on, we will
16770 * when the hpts fires.
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);
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]++;
16781 counter_u64_add(tcp_cnt_counters[SND_BLOCKED], 1);
16784 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
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;
16795 rack->r_ctl.rc_agg_delayed += delayed;
16797 } else if (early) {
16798 rack->r_ctl.rc_agg_early += early;
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);
16809 if ((rack->r_fast_output) &&
16810 (doing_tlp == 0) &&
16811 (tp->rcv_numsacks == 0)) {
16815 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
16819 inp = rack->rc_inp;
16820 so = inp->inp_socket;
16825 inp = rack->rc_inp;
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.
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;
16839 goto just_return_nolock;
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
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);
16852 tp->t_flags &= ~TF_LASTIDLE;
16854 if (tp->t_flags & TF_MORETOCOME) {
16855 tp->t_flags |= TF_LASTIDLE;
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;
16871 rack_exit_probertt(rack, cts);
16876 if (rack_use_fsb && (rack->r_fsb_inited == 0) && (rack->r_state != TCPS_CLOSED))
16877 rack_init_fsb_block(tp, rack);
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.
16885 cts = tcp_get_usecs(&tv);
16886 ms_cts = tcp_tv_to_mssectick(&tv);
16889 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
16891 if (rack->r_ctl.rc_pace_max_segs == 0)
16892 pace_max_seg = rack->rc_user_set_max_segs * segsiz;
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);
16901 if (inp->inp_hpts_calls)
16902 /* Retry in a ms */
16903 slot = (1 * HPTS_USEC_IN_MSEC);
16904 so = inp->inp_socket;
16906 goto just_return_nolock;
16908 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
16909 rack->rc_free_cnt++;
16912 if (inp->inp_hpts_calls)
16913 inp->inp_hpts_calls = 0;
16917 if (flags & TH_RST) {
16918 SOCKBUF_LOCK(&inp->inp_socket->so_snd);
16919 so = inp->inp_socket;
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;
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;
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);
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);
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;
16964 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
16965 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
16967 counter_u64_add(rack_rtm_prr_retran, 1);
16969 } else if (rack->r_ctl.rc_tlpsend) {
16970 /* Tail loss probe */
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
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;
16985 tlen = rsm->r_end - rsm->r_start;
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);
16996 if (rack->r_must_retran &&
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).
17003 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
17004 int sendwin, flight;
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;
17011 goto just_return_nolock;
17013 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
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;
17021 goto just_return_nolock;
17024 len = rsm->r_end - rsm->r_start;
17026 sb_offset = rsm->r_start - tp->snd_una;
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;
17036 * Enforce a connection sendmap count limit if set
17037 * as long as we are not retransmiting.
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);
17048 so = inp->inp_socket;
17050 goto just_return_nolock;
17052 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
17053 /* we are retransmitting the fin */
17057 * When retransmitting data do *not* include the
17058 * FIN. This could happen from a TLP probe.
17064 /* For debugging */
17065 rack->r_ctl.rc_rsm_at_retran = rsm;
17067 if (rsm && rack->r_fsb_inited && rack_use_rsm_rfo &&
17068 ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
17071 ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
17075 so = inp->inp_socket;
17077 if (do_a_prefetch == 0) {
17078 kern_prefetch(sb, &do_a_prefetch);
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) &&
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,
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;
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);
17112 * Get standard flags, and add SYN or FIN if requested by 'hidden'
17115 if (tp->t_flags & TF_NEEDFIN)
17117 if (tp->t_flags & TF_NEEDSYN)
17119 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
17121 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
17123 kern_prefetch(end_rsm, &prefetch_rsm);
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.
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
17139 * If sack_rxmit is true we are retransmitting from the scoreboard
17140 * in which case len is already set.
17142 if ((sack_rxmit == 0) &&
17143 (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
17146 avail = sbavail(sb);
17147 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
17148 sb_offset = tp->snd_nxt - tp->snd_una;
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);
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;
17163 len = rack->r_ctl.rc_tlp_new_data;
17166 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
17168 if ((rack->r_ctl.crte == NULL) && IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) {
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.
17179 uint32_t outstanding;
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
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;
17203 } else if (avail > sb_offset) {
17204 len = avail - sb_offset;
17209 if (len > rack->r_ctl.rc_prr_sndcnt) {
17210 len = rack->r_ctl.rc_prr_sndcnt;
17214 counter_u64_add(rack_rtm_prr_newdata, 1);
17217 if (len > segsiz) {
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 :-)
17225 if (rack->r_ctl.rc_prr_sendalot == 0) {
17228 } else if (len < segsiz) {
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.
17237 leftinsb = sbavail(sb) - sb_offset;
17238 if (leftinsb > len) {
17239 /* This send does not empty the sb */
17244 } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
17246 * If you have not established
17247 * and are not doing FAST OPEN
17250 if ((sack_rxmit == 0) &&
17251 (!IS_FASTOPEN(tp->t_flags))){
17256 if (prefetch_so_done == 0) {
17257 kern_prefetch(so, &prefetch_so_done);
17258 prefetch_so_done = 1;
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.
17265 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
17266 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
17268 * When sending additional segments following a TFO SYN|ACK,
17269 * do not include the SYN bit.
17271 if (IS_FASTOPEN(tp->t_flags) &&
17272 (tp->t_state == TCPS_SYN_RECEIVED))
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.
17280 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
17285 * On TFO sockets, ensure no data is sent in the following cases:
17287 * - When retransmitting SYN|ACK on a passively-created socket
17289 * - When retransmitting SYN on an actively created socket
17291 * - When sending a zero-length cookie (cookie request) on an
17292 * actively created socket
17294 * - When the socket is in the CLOSED state (RST is being sent)
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))) {
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;
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;
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
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
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);
17338 } else if ((rsm == NULL) &&
17339 (doing_tlp == 0) &&
17340 (len < pace_max_seg)) {
17342 * We are not sending a maximum sized segment for
17343 * some reason. Should we not send anything (think
17344 * sws or persists)?
17346 if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
17347 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
17349 (len < (int)(sbavail(sb) - sb_offset))) {
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.
17359 if (tp->snd_max == tp->snd_una) {
17361 * Nothing out we can
17362 * go into persists.
17364 rack_enter_persist(tp, rack, cts);
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)) &&
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.
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))) {
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.
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))) {
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.
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.
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).
17427 /* len will be >= 0 after this point. */
17428 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
17429 rack_sndbuf_autoscale(rack);
17431 * Decide if we can use TCP Segmentation Offloading (if supported by
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.
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.
17447 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
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.
17453 if (isipv6 && IPSEC_ENABLED(ipv6))
17454 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
17460 if (IPSEC_ENABLED(ipv4))
17461 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
17465 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
17466 ipoptlen += ipsec_optlen;
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 &&
17475 uint32_t outstanding;
17477 outstanding = tp->snd_max - tp->snd_una;
17478 if (tp->t_flags & TF_SENTFIN) {
17480 * If we sent a fin, snd_max is 1 higher than
17486 if ((rsm->r_flags & RACK_HAS_FIN) == 0)
17489 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
17494 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
17495 (long)TCP_MAXWIN << tp->rcv_scale);
17498 * Sender silly window avoidance. We transmit under the following
17499 * conditions when len is non-zero:
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
17508 if (len >= segsiz) {
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
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) {
17524 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
17528 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
17532 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */
17540 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
17541 (ctf_outstanding(tp) < (segsiz * 2))) {
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.
17554 * Sending of standalone window updates.
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.
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.
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.
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.
17581 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
17582 !(tp->t_flags & TF_DELACK) &&
17583 !TCPS_HAVERCVDFIN(tp->t_state)) {
17585 * "adv" is the amount we could increase the window, taking
17586 * into account that we are limited by TCP_MAXWIN <<
17593 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
17594 oldwin = (tp->rcv_adv - tp->rcv_nxt);
17598 /* We can't increase the window */
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
17609 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
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)) {
17619 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
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.
17630 if (tp->t_flags & TF_ACKNOW) {
17634 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
17639 * If our state indicates that FIN should be sent and we have not
17640 * yet done so, then we need to send.
17642 if ((flags & TH_FIN) &&
17643 (tp->snd_nxt == tp->snd_una)) {
17648 * No reason to send a segment, just return.
17651 SOCKBUF_UNLOCK(sb);
17652 just_return_nolock:
17654 int app_limited = CTF_JR_SENT_DATA;
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) &&
17662 ((flags & (TH_SYN|TH_FIN)) == 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) &&
17674 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
17675 /* We can send at least one more MSS using our fsb */
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;
17683 rack->r_ctl.fsb.hw_tls = 1;
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;
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;
17696 rack->r_ctl.fsb.rfo_apply_push = 0;
17699 rack->r_fast_output = 0;
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;
17708 int end_window = 0;
17709 uint32_t seq = tp->gput_ack;
17711 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
17714 * Mark the last sent that we just-returned (hinting
17715 * that delayed ack may play a role in any rtt measurement).
17717 rsm->r_just_ret = 1;
17719 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
17720 rack->r_ctl.rc_agg_delayed = 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)) &&
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.
17746 app_limited = CTF_JR_APP_LIMITED;
17747 } else if (tp->t_flags & TF_NOPUSH) {
17749 * The user has requested no push of
17750 * the last segment and we are
17751 * at the last segment. Another app
17754 app_limited = CTF_JR_APP_LIMITED;
17755 } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
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;
17763 /* Now why here are we not sending? */
17766 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
17769 app_limited = CTF_JR_ASSESSING;
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.
17776 if (rack_cwnd_block_ends_measure &&
17777 ((app_limited == CTF_JR_CWND_LIMITED) ||
17778 (app_limited == CTF_JR_PRR))) {
17780 * The reason we are not sending is
17781 * the cwnd (or prr). We have been configured
17782 * to end the measurement window in
17786 } else if (rack_rwnd_block_ends_measure &&
17787 (app_limited == CTF_JR_RWND_LIMITED)) {
17789 * We are rwnd limited and have been
17790 * configured to end the measurement
17791 * window in this case.
17794 } else if (app_limited == CTF_JR_APP_LIMITED) {
17796 * A true application limited period, we have
17800 } else if (app_limited == CTF_JR_ASSESSING) {
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..
17806 * If we get this out in logs we need to
17807 * investagate which reason we missed.
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)) {
17820 * There is not enough to measure.
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*/,
17826 0, 0, 18, __LINE__, NULL, 0);
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;
17837 * Go out to the end app limited and mark
17838 * this new one as next and move the end_appl up
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;
17845 rsm->r_flags |= RACK_APP_LIMITED;
17846 rack->r_ctl.rc_app_limited_cnt++;
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);
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);
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) &&
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);
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);
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;
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]++;
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);
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);
17893 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) / segsiz));
17895 crtsc = get_cyclecount();
17896 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
17897 tp->tcp_cnt_counters[SND_LIMITED]++;
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);
17903 counter_u64_add(tcp_proc_time[SND_LIMITED], (crtsc - ts_val));
17910 if (rsm || sack_rxmit)
17911 counter_u64_add(rack_nfto_resend, 1);
17913 counter_u64_add(rack_non_fto_send, 1);
17914 if ((flags & TH_FIN) &&
17917 * We do not transmit a FIN
17918 * with data outstanding. We
17919 * need to make it so all data
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)) {
17928 len = rack->r_ctl.rc_pace_max_segs;
17930 SOCKBUF_LOCK_ASSERT(sb);
17933 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
17935 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
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
17947 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
17950 hdrlen = sizeof(struct tcpiphdr);
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.
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);
17964 to.to_mss -= V_tcp_udp_tunneling_overhead;
17965 to.to_flags |= TOF_MSS;
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.
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;
17979 (u_int8_t *)&tp->t_tfo_cookie.server;
17980 to.to_flags |= TOF_FASTOPEN;
17982 } else if (tp->t_state == TCPS_SYN_SENT) {
17984 tp->t_tfo_client_cookie_len;
17986 tp->t_tfo_cookie.client;
17987 to.to_flags |= TOF_FASTOPEN;
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
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;
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;
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;
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 */
18033 /* Processing the options. */
18034 hdrlen += optlen = tcp_addoptions(&to, opt);
18036 * If we wanted a TFO option to be added, but it was unable
18037 * to fit, ensure no data is sent.
18039 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
18040 !(to.to_flags & TOF_FASTOPEN))
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]++;
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);
18056 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18059 return (EHOSTUNREACH);
18061 hdrlen += sizeof(struct udphdr);
18065 ipoptlen = ip6_optlen(tp->t_inpcb);
18068 if (tp->t_inpcb->inp_options)
18069 ipoptlen = tp->t_inpcb->inp_options->m_len -
18070 offsetof(struct ipoption, ipopt_list);
18073 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18074 ipoptlen += ipsec_optlen;
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.
18082 if (len + optlen + ipoptlen > tp->t_maxseg) {
18084 uint32_t if_hw_tsomax;
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__));
18096 * Check if we should limit by maximum payload
18099 if (if_hw_tsomax != 0) {
18100 /* compute maximum TSO length */
18101 max_len = (if_hw_tsomax - hdrlen -
18103 if (max_len <= 0) {
18105 } else if (len > max_len) {
18112 * Prevent the last segment from being fractional
18113 * unless the send sockbuf can be emptied:
18115 max_len = (tp->t_maxseg - optlen);
18116 if ((sb_offset + len) < sbavail(sb)) {
18117 moff = len % (u_int)max_len;
18124 * In case there are too many small fragments don't
18127 if (len <= segsiz) {
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.
18137 if (tp->t_flags & TF_NEEDFIN) {
18142 if (optlen + ipoptlen >= tp->t_maxseg) {
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
18151 SOCKBUF_UNLOCK(&so->so_snd);
18156 len = tp->t_maxseg - optlen - ipoptlen;
18163 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
18164 ("%s: len > IP_MAXPACKET", __func__));
18167 if (max_linkhdr + hdrlen > MCLBYTES)
18169 if (max_linkhdr + hdrlen > MHLEN)
18171 panic("tcphdr too big");
18175 * This KASSERT is here to catch edge cases at a well defined place.
18176 * Before, those had triggered (random) panic conditions further
18179 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
18181 (flags & TH_FIN) &&
18184 * We have outstanding data, don't send a fin by itself!.
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
18193 hw_tls = (sb->sb_flags & SB_TLS_IFNET) != 0;
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;
18205 * We allow a limit on sending with hptsi.
18207 if (len > max_val) {
18212 if (MHLEN < hdrlen + max_linkhdr)
18213 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18216 m = m_gethdr(M_NOWAIT, MT_DATA);
18219 SOCKBUF_UNLOCK(sb);
18224 m->m_data += max_linkhdr;
18228 * Start the m_copy functions from the closest mbuf to the
18229 * sb_offset in the socket buffer chain.
18231 mb = sbsndptr_noadv(sb, sb_offset, &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);
18241 struct sockbuf *msb;
18243 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
18247 m->m_next = tcp_m_copym(
18249 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
18250 ((rsm == NULL) ? hw_tls : 0)
18251 #ifdef NETFLIX_COPY_ARGS
18255 if (len <= (tp->t_maxseg - optlen)) {
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
18264 if (m->m_next == NULL) {
18265 SOCKBUF_UNLOCK(sb);
18272 if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
18273 if (rsm && (rsm->r_flags & RACK_TLP)) {
18275 * TLP should not count in retran count, but
18278 counter_u64_add(rack_tlp_retran, 1);
18279 counter_u64_add(rack_tlp_retran_bytes, len);
18281 tp->t_sndrexmitpack++;
18282 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18283 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18286 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18290 KMOD_TCPSTAT_INC(tcps_sndpack);
18291 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
18293 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
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.)
18302 if (sb_offset + len == sbused(sb) &&
18304 !(flags & TH_SYN)) {
18306 add_flag |= RACK_HAD_PUSH;
18309 SOCKBUF_UNLOCK(sb);
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);
18317 KMOD_TCPSTAT_INC(tcps_sndwinup);
18319 m = m_gethdr(M_NOWAIT, MT_DATA);
18326 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
18328 M_ALIGN(m, hdrlen);
18331 m->m_data += max_linkhdr;
18334 SOCKBUF_UNLOCK_ASSERT(sb);
18335 m->m_pkthdr.rcvif = (struct ifnet *)0;
18337 mac_inpcb_create_mbuf(inp, m);
18339 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
18342 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
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;
18351 ulen = hdrlen + len - sizeof(struct ip6_hdr);
18354 ulen = hdrlen + len - sizeof(struct ip);
18355 udp->uh_ulen = htons(ulen);
18360 ip6 = mtod(m, struct ip6_hdr *);
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);
18369 th = (struct tcphdr *)(ip6 + 1);
18370 tcpip_fillheaders(inp, tp->t_port, ip6, th);
18374 ip = mtod(m, struct ip *);
18376 ipov = (struct ipovly *)ip;
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);
18386 th = (struct tcphdr *)(ip + 1);
18387 tcpip_fillheaders(inp, tp->t_port, ip, th);
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.
18395 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
18396 tp->snd_nxt == tp->snd_max)
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
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;
18408 flags |= TH_ECE | TH_CWR;
18410 /* Handle parallel SYN for ECN */
18411 if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18412 (tp->t_flags2 & TF2_ECN_SND_ECE)) {
18414 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18416 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
18417 (tp->t_flags2 & TF2_ECN_PERMIT)) {
18419 * If the peer has ECN, mark data packets with ECN capable
18420 * transmission (ECT). Ignore pure ack packets,
18423 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
18424 (sack_rxmit == 0)) {
18427 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
18430 ip->ip_tos |= IPTOS_ECN_ECT0;
18431 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
18433 * Reply with proper ECN notifications.
18434 * Only set CWR on new data segments.
18436 if (tp->t_flags2 & TF2_ECN_SND_CWR) {
18438 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
18441 if (tp->t_flags2 & TF2_ECN_SND_ECE)
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...)
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;
18460 th->th_seq = htonl(tp->snd_max);
18461 rack_seq = tp->snd_max;
18464 th->th_seq = htonl(rsm->r_start);
18465 rack_seq = rsm->r_start;
18467 th->th_ack = htonl(tp->rcv_nxt);
18468 th->th_flags = flags;
18470 * Calculate receive window. Don't shrink window, but avoid silly
18472 * If a RST segment is sent, advertise a window of zero.
18474 if (flags & TH_RST) {
18477 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
18478 recwin < (long)segsiz) {
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);
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.
18491 if (flags & TH_SYN)
18492 th->th_win = htons((u_short)
18493 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
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));
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.
18507 if (th->th_win == 0) {
18508 tp->t_sndzerowin++;
18509 tp->t_flags |= TF_RXWIN0SENT;
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) {
18517 cpto = mtod(m, uint8_t *);
18518 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18520 * We have just copied in:
18522 * <optional udphdr>
18523 * tcphdr (no options)
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).
18532 ip6 = mtod(m, struct ip6_hdr *);
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 */
18539 udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
18541 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
18542 if (to.to_flags & TOF_SIGNATURE) {
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.
18549 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
18550 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
18552 * Do not send segment if the calculation of MD5
18553 * digest has failed.
18560 bcopy(opt, th + 1, optlen);
18561 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18564 * Put TCP length in extended header, and then checksum extended
18567 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
18571 * ip6_plen is not need to be filled now, and will be filled
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);
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,
18589 #if defined(INET6) && defined(INET)
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);
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));
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));
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.
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;
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)));
18629 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
18630 hhook_run_tcp_est_out(tp, th, &to, len, tso);
18632 /* We're getting ready to send; log now. */
18633 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
18634 union tcp_log_stackspecific log;
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;
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;
18647 log.u_bbr.flex5 = 0x80000000;
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) {
18656 log.u_bbr.flex8 = 2;
18658 log.u_bbr.flex8 = 1;
18661 log.u_bbr.flex8 = 3;
18663 log.u_bbr.flex8 = 0;
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);
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.
18685 * m->m_pkthdr.len should have been set before cksum calcuration,
18686 * because in6_cksum() need it.
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
18696 rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
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.
18703 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18705 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18706 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18708 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18710 if (tp->t_state == TCPS_SYN_SENT)
18711 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
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,
18722 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
18725 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
18726 mtu = inp->inp_route6.ro_nh->nh_mtu;
18729 #if defined(INET) && defined(INET6)
18734 ip->ip_len = htons(m->m_pkthdr.len);
18736 if (inp->inp_vflag & INP_IPV6PROTO)
18737 ip->ip_ttl = in6_selecthlim(inp, NULL);
18739 rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
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
18747 * NB: Don't set DF on small MTU/MSS to have a safe
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);
18756 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18759 if (tp->t_state == TCPS_SYN_SENT)
18760 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
18762 TCP_PROBE5(send, NULL, tp, ip, tp, th);
18764 error = ip_output(m,
18765 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
18771 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
18773 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
18774 mtu = inp->inp_route.ro_nh->nh_mtu;
18780 lgb->tlb_errno = error;
18784 * In transmit state, time the transmission and arrange for the
18785 * retransmit. In persist state, just set snd_max.
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;
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;
18800 if (cwnd_to_use > tp->snd_ssthresh) {
18801 /* Set we sent in CA */
18802 rack->rc_gp_saw_ca = 1;
18804 /* Set we sent in SS */
18805 rack->rc_gp_saw_ss = 1;
18808 if (doing_tlp && (rsm == NULL)) {
18809 /* Make sure new data TLP cnt is clear */
18810 rack->r_ctl.rc_tlp_new_data = 0;
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;
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) {
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);
18828 counter_u64_add(rack_out_size[idx], 1);
18831 if ((rack->rack_no_prr == 0) &&
18834 if (rack->r_ctl.rc_prr_sndcnt >= len)
18835 rack->r_ctl.rc_prr_sndcnt -= len;
18837 rack->r_ctl.rc_prr_sndcnt = 0;
18841 /* Make sure the TLP is added */
18842 add_flag |= RACK_TLP;
18844 /* If its a resend without TLP then it must not have the flag */
18845 rsm->r_flags &= ~RACK_TLP;
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);
18852 if ((error == 0) &&
18854 (tp->snd_una == tp->snd_max))
18855 rack->r_ctl.rc_tlp_rxt_last_time = cts;
18857 tcp_seq startseq = tp->snd_nxt;
18859 /* Track our lost count */
18860 if (rsm && (doing_tlp == 0))
18861 rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
18863 * Advance snd_nxt over sequence space of this segment.
18866 /* We don't log or do anything with errors */
18868 if (doing_tlp == 0) {
18871 * Not a retransmission of some
18872 * sort, new data is going out so
18873 * clear our TLP count and flag.
18875 rack->rc_tlp_in_progress = 0;
18876 rack->r_ctl.rc_tlp_cnt_out = 0;
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.
18884 rack->rc_tlp_in_progress = 1;
18885 rack->r_ctl.rc_tlp_cnt_out++;
18887 if (flags & (TH_SYN | TH_FIN)) {
18888 if (flags & TH_SYN)
18890 if (flags & TH_FIN) {
18892 tp->t_flags |= TF_SENTFIN;
18895 /* In the ENOBUFS case we do *not* update snd_max */
18899 tp->snd_nxt += len;
18900 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
18901 if (tp->snd_una == tp->snd_max) {
18903 * Update the time we just added data since
18904 * none was outstanding.
18906 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
18907 tp->t_acktime = ticks;
18909 tp->snd_max = tp->snd_nxt;
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
18916 if (tp->t_rtttime == 0) {
18917 tp->t_rtttime = ticks;
18918 tp->t_rtseq = startseq;
18919 KMOD_TCPSTAT_INC(tcps_segstimed);
18922 ((tp->t_flags & TF_GPUTINPROG) == 0))
18923 rack_start_gp_measurement(tp, rack, startseq, sb_offset);
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.
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.
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;
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;
18949 rack->r_ctl.rc_agg_delayed = 0;
18952 rack->r_ctl.rc_agg_early = 0;
18953 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */
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
18960 * In any case, we do not want to loop around for another
18961 * send without a good reason.
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]++;
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);
18976 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
18982 * Pace us right away to retry in a some
18985 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
18986 if (rack->rc_enobuf < 0x7f)
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);
18994 counter_u64_add(rack_saw_enobuf, 1);
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.
19006 tp->t_flags &= ~TF_TSO;
19008 tcp_mss_update(tp, -1, mtu, NULL, NULL);
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]++;
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);
19022 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19027 counter_u64_add(rack_saw_enetunreach, 1);
19031 if (TCPS_HAVERCVDSYN(tp->t_state)) {
19032 tp->t_softerror = error;
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]++;
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);
19047 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val));
19053 rack->rc_enobuf = 0;
19054 if (IN_FASTRECOVERY(tp->t_flags) && rsm)
19055 rack->r_ctl.retran_during_recovery += len;
19057 KMOD_TCPSTAT_INC(tcps_sndtotal);
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.
19064 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
19065 tp->rcv_adv = tp->rcv_nxt + recwin;
19067 tp->last_ack_sent = tp->rcv_nxt;
19068 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
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. */
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 */
19082 if ((error == 0) && (flags & TH_FIN))
19083 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
19084 if (flags & TH_RST) {
19086 * We don't send again after sending a RST.
19091 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
19092 } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
19094 * Get our pacing rate, if an error
19095 * occurred in sending (ENOBUF) we would
19096 * hit the else if with slot preset. Other
19099 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
19102 (rsm->r_flags & RACK_HAS_SYN) == 0 &&
19103 rack->use_rack_rr) {
19104 /* Its a retransmit and we use the rack cheat? */
19106 (rack->rc_always_pace == 0) ||
19107 (rack->r_rr_config == 1)) {
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.
19113 slot = rack->r_ctl.rc_min_to;
19116 /* We have sent clear the flag */
19117 rack->r_ent_rec_ns = 0;
19118 if (rack->r_must_retran) {
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)) {
19123 * We have retransmitted all.
19125 rack->r_must_retran = 0;
19126 rack->r_ctl.rc_out_at_rto = 0;
19128 } else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
19130 * Sending new data will also kill
19133 rack->r_must_retran = 0;
19134 rack->r_ctl.rc_out_at_rto = 0;
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)) {
19141 * We hit an RTO and now have past snd_max at the RTO
19142 * clear all the WAS flags.
19144 tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
19147 /* set the rack tcb into the slot N */
19148 counter_u64_add(rack_paced_segments, 1);
19149 if ((error == 0) &&
19151 ((flags & (TH_SYN|TH_FIN)) == 0) &&
19153 (tp->snd_nxt == tp->snd_max) &&
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) &&
19164 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19165 /* We can send at least one more MSS using our fsb */
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;
19173 rack->r_ctl.fsb.hw_tls = 1;
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;
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;
19186 rack->r_ctl.fsb.rfo_apply_push = 0;
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) {
19197 counter_u64_add(rack_unpaced_segments, 1);
19199 if ((error == 0) &&
19201 ((flags & (TH_SYN|TH_FIN)) == 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) &&
19214 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
19215 /* we can use fast_output for more */
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;
19223 rack->r_ctl.fsb.hw_tls = 1;
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;
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;
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);
19242 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19252 counter_u64_add(rack_unpaced_segments, 1);
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]++;
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;
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);
19272 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) /segsiz));
19274 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19275 tp->tcp_cnt_counters[SND_OUT_ACK]++;
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;
19281 counter_u64_add(tcp_proc_time[SND_OUT_ACK], crtsc);
19285 if (error == ENOBUFS)
19291 rack_update_seg(struct tcp_rack *rack)
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);
19302 rack_mtu_change(struct tcpcb *tp)
19305 * The MSS may have changed
19307 struct tcp_rack *rack;
19309 rack = (struct tcp_rack *)tp->t_fb_ptr;
19310 if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
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.
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;
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;
19332 rack_set_profile(struct tcp_rack *rack, int prof)
19336 /* pace_always=1 */
19337 if (rack->rc_always_pace == 0) {
19338 if (tcp_can_enable_pacing() == 0)
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;
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;
19353 rack->rack_enable_scwnd = 1;
19355 rack->rc_gp_dyn_mul = 1;
19357 rack->r_ctl.rack_per_of_gp_ca = 100;
19359 rack->r_rr_config = 3;
19361 rack->r_ctl.rc_no_push_at_mrtt = 2;
19363 rack->rc_pace_to_cwnd = 1;
19364 rack->rc_pace_fill_if_rttin_range = 0;
19365 rack->rtt_limit_mul = 0;
19367 rack->rack_no_prr = 1;
19369 rack->r_limit_scw = 1;
19371 rack->r_ctl.rack_per_of_gp_rec = 90;
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)
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;
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;
19393 rack->rack_enable_scwnd = 1;
19395 rack->rc_gp_dyn_mul = 1;
19397 rack->r_ctl.rack_per_of_gp_ca = 100;
19399 rack->r_rr_config = 3;
19401 rack->r_ctl.rc_no_push_at_mrtt = 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;
19408 rack->rack_no_prr = 1;
19410 rack->r_limit_scw = 1;
19412 rack->r_ctl.rack_per_of_gp_rec = 90;
19416 } else if (prof == 2) {
19418 if (rack->rc_always_pace == 0) {
19419 if (tcp_can_enable_pacing() == 0)
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;
19431 rack->rack_enable_scwnd = 1;
19433 rack->rc_gp_dyn_mul = 1;
19434 rack->r_ctl.rack_per_of_gp_ca = 100;
19436 rack->r_rr_config = 3;
19438 rack->r_ctl.rc_no_push_at_mrtt = 2;
19440 rack->rc_pace_to_cwnd = 1;
19441 rack->rc_pace_fill_if_rttin_range = 0;
19442 rack->rtt_limit_mul = 0;
19444 rack->rack_no_prr = 1;
19446 rack->r_limit_scw = 0;
19448 } else if (prof == 0) {
19449 /* This changes things back to the default settings */
19451 if (rack->rc_always_pace) {
19452 tcp_decrement_paced_conn();
19453 rack_undo_cc_pacing(rack);
19454 rack->rc_always_pace = 0;
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);
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;
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;
19470 if (rack_use_cmp_acks)
19471 rack->r_use_cmp_ack = 1;
19473 rack->r_use_cmp_ack = 0;
19474 if (rack_disable_prr)
19475 rack->rack_no_prr = 1;
19477 rack->rack_no_prr = 0;
19478 if (rack_gp_no_rec_chg)
19479 rack->rc_gp_no_rec_chg = 1;
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;
19488 rack->r_mbuf_queue = 0;
19489 rack->rc_inp->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19491 if (rack_enable_shared_cwnd)
19492 rack->rack_enable_scwnd = 1;
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;
19501 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
19502 rack->rc_gp_dyn_mul = 0;
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;
19510 if (rack_enable_hw_pacing)
19511 rack->rack_hdw_pace_ena = 1;
19513 rack->rack_hdw_pace_ena = 0;
19514 if (rack_disable_prr)
19515 rack->rack_no_prr = 1;
19517 rack->rack_no_prr = 0;
19518 if (rack_limits_scwnd)
19519 rack->r_limit_scw = 1;
19521 rack->r_limit_scw = 0;
19528 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
19530 struct deferred_opt_list *dol;
19532 dol = malloc(sizeof(struct deferred_opt_list),
19533 M_TCPFSB, M_NOWAIT|M_ZERO);
19536 * No space yikes -- fail out..
19540 dol->optname = sopt_name;
19541 dol->optval = loptval;
19542 TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
19547 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
19548 uint32_t optval, uint64_t loptval)
19550 struct epoch_tracker et;
19551 struct sockopt sopt;
19552 struct cc_newreno_opts opt;
19557 switch (sopt_name) {
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;
19564 rack->rc_rack_tmr_std_based = 0;
19566 if (optval & 0x2) {
19567 rack->rc_rack_use_dsack = 1;
19569 rack->rc_rack_use_dsack = 0;
19571 rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
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. */
19580 if (rack->rc_pacing_cc_set) {
19582 * Set them into the real CC module
19583 * whats in the rack pcb is the old values
19584 * to be used on restoral/
19586 sopt.sopt_dir = SOPT_SET;
19587 opt.name = CC_NEWRENO_BETA;
19589 if (CC_ALGO(tp)->ctl_output != NULL)
19590 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19597 * Not pacing yet so set it into our local
19598 * rack pcb storage.
19600 rack->r_ctl.rc_saved_beta.beta = optval;
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) {
19608 * If we have an SRTT lets update t_rxtcur
19609 * to have the new slop.
19611 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
19612 rack_rto_min, rack_rto_max,
19613 rack->r_ctl.timer_slop);
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. */
19623 if (rack->rc_pacing_cc_set) {
19625 * Set them into the real CC module
19626 * whats in the rack pcb is the old values
19627 * to be used on restoral/
19629 sopt.sopt_dir = SOPT_SET;
19630 opt.name = CC_NEWRENO_BETA_ECN;
19632 if (CC_ALGO(tp)->ctl_output != NULL)
19633 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
19638 * Not pacing yet so set it into our local
19639 * rack pcb storage.
19641 rack->r_ctl.rc_saved_beta.beta_ecn = optval;
19642 rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
19645 case TCP_DEFER_OPTIONS:
19646 RACK_OPTS_INC(tcp_defer_opt);
19648 if (rack->gp_ready) {
19653 rack->defer_options = 1;
19655 rack->defer_options = 0;
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;
19664 case TCP_REC_ABC_VAL:
19665 RACK_OPTS_INC(tcp_rec_abc_val);
19667 rack->r_use_labc_for_rec = 1;
19669 rack->r_use_labc_for_rec = 0;
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;
19678 case TCP_HDWR_UP_ONLY:
19679 RACK_OPTS_INC(tcp_pacing_up_only);
19681 rack->r_up_only = 1;
19683 rack->r_up_only = 0;
19685 case TCP_PACING_RATE_CAP:
19686 RACK_OPTS_INC(tcp_pacing_rate_cap);
19687 rack->r_ctl.bw_rate_cap = loptval;
19689 case TCP_RACK_PROFILE:
19690 RACK_OPTS_INC(tcp_profile);
19691 error = rack_set_profile(rack, optval);
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! */
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;
19703 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
19704 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP;
19706 case TCP_SHARED_CWND_TIME_LIMIT:
19707 RACK_OPTS_INC(tcp_lscwnd);
19709 rack->r_limit_scw = 1;
19711 rack->r_limit_scw = 0;
19713 case TCP_RACK_PACE_TO_FILL:
19714 RACK_OPTS_INC(tcp_fillcw);
19716 rack->rc_pace_to_cwnd = 0;
19718 rack->rc_pace_to_cwnd = 1;
19720 rack->r_fill_less_agg = 1;
19722 if ((optval >= rack_gp_rtt_maxmul) &&
19723 rack_gp_rtt_maxmul &&
19725 rack->rc_pace_fill_if_rttin_range = 1;
19726 rack->rtt_limit_mul = optval;
19728 rack->rc_pace_fill_if_rttin_range = 0;
19729 rack->rtt_limit_mul = 0;
19732 case TCP_RACK_NO_PUSH_AT_MAX:
19733 RACK_OPTS_INC(tcp_npush);
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;
19741 case TCP_SHARED_CWND_ENABLE:
19742 RACK_OPTS_INC(tcp_rack_scwnd);
19744 rack->rack_enable_scwnd = 0;
19746 rack->rack_enable_scwnd = 1;
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;
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;
19758 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
19760 case TCP_RACK_NONRXT_CFG_RATE:
19761 RACK_OPTS_INC(tcp_rack_cfg_rate);
19763 rack->rack_rec_nonrxt_use_cr = 0;
19765 rack->rack_rec_nonrxt_use_cr = 1;
19768 RACK_OPTS_INC(tcp_rack_noprr);
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;
19778 case TCP_TIMELY_DYN_ADJ:
19779 RACK_OPTS_INC(tcp_timely_dyn);
19781 rack->rc_gp_dyn_mul = 0;
19783 rack->rc_gp_dyn_mul = 1;
19784 if (optval >= 100) {
19786 * If the user sets something 100 or more
19787 * its the gp_ca value.
19789 rack->r_ctl.rack_per_of_gp_ca = optval;
19793 case TCP_RACK_DO_DETECTION:
19794 RACK_OPTS_INC(tcp_rack_do_detection);
19796 rack->do_detection = 0;
19798 rack->do_detection = 1;
19800 case TCP_RACK_TLP_USE:
19801 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
19805 RACK_OPTS_INC(tcp_tlp_use);
19806 rack->rack_tlp_threshold_use = optval;
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;
19813 /* Pacing related ones */
19814 case TCP_RACK_PACE_ALWAYS:
19816 * zero is old rack method, 1 is new
19817 * method using a pacing rate.
19819 RACK_OPTS_INC(tcp_rack_pace_always);
19821 if (rack->rc_always_pace) {
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);
19834 if (rack->rc_always_pace) {
19835 tcp_decrement_paced_conn();
19836 rack->rc_always_pace = 0;
19837 rack_undo_cc_pacing(rack);
19840 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
19841 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
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);
19847 case TCP_BBR_RACK_INIT_RATE:
19848 RACK_OPTS_INC(tcp_initial_rate);
19850 /* Change from kbits per second to bytes per second */
19853 rack->r_ctl.init_rate = val;
19854 if (rack->rc_init_win != rack_default_init_window) {
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.
19864 win = rc_init_window(rack);
19865 if (SEQ_GT(tp->snd_max, tp->iss))
19866 snt = tp->snd_max - tp->iss;
19870 (tp->snd_cwnd < win))
19871 tp->snd_cwnd = win;
19873 if (rack->rc_always_pace)
19874 rack_update_seg(rack);
19876 case TCP_BBR_IWINTSO:
19877 RACK_OPTS_INC(tcp_initial_win);
19878 if (optval && (optval <= 0xff)) {
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;
19889 #ifdef NETFLIX_PEAKRATE
19890 tp->t_maxpeakrate |
19892 rack->r_ctl.init_rate)) {
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.
19902 if (tp->snd_cwnd < win)
19903 tp->snd_cwnd = win;
19905 if (rack->rc_always_pace)
19906 rack_update_seg(rack);
19910 case TCP_RACK_FORCE_MSEG:
19911 RACK_OPTS_INC(tcp_rack_force_max_seg);
19913 rack->rc_force_max_seg = 1;
19915 rack->rc_force_max_seg = 0;
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);
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,
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);
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);
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);
19985 case TCP_RACK_GP_INCREASE_CA:
19986 RACK_OPTS_INC(tcp_gp_inc_ca);
19990 * We don't allow any reduction
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);
20003 case TCP_RACK_GP_INCREASE_SS:
20004 RACK_OPTS_INC(tcp_gp_inc_ss);
20008 * We don't allow any reduction
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);
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;
20026 rack->r_rr_config = 0;
20028 case TCP_HDWR_RATE_CAP:
20029 RACK_OPTS_INC(tcp_hdwr_rate_cap);
20031 if (rack->r_rack_hw_rate_caps == 0)
20032 rack->r_rack_hw_rate_caps = 1;
20036 rack->r_rack_hw_rate_caps = 0;
20039 case TCP_BBR_HDWR_PACE:
20040 RACK_OPTS_INC(tcp_hdwr_pacing);
20042 if (rack->rack_hdrw_pacing == 0) {
20043 rack->rack_hdw_pace_ena = 1;
20044 rack->rack_attempt_hdwr_pace = 0;
20048 rack->rack_hdw_pace_ena = 0;
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;
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;
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;
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;
20075 case TCP_RACK_ENABLE_HYSTART:
20077 struct sockopt sopt;
20078 struct cc_newreno_opts opt;
20080 sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
20081 sopt.sopt_dir = SOPT_SET;
20082 opt.name = CC_NEWRENO_ENABLE_HYSTART;
20084 if (CC_ALGO(tp)->ctl_output != NULL)
20085 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt);
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;
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;
20103 case TCP_RACK_TLP_THRESH:
20104 /* RACK TLP theshold i.e. srtt+(srtt/N) */
20105 RACK_OPTS_INC(tcp_rack_tlp_thresh);
20107 rack->r_ctl.rc_tlp_threshold = optval;
20111 case TCP_BBR_USE_RACK_RR:
20112 RACK_OPTS_INC(tcp_rack_rr);
20114 rack->use_rack_rr = 1;
20116 rack->use_rack_rr = 0;
20118 case TCP_FAST_RSM_HACK:
20119 RACK_OPTS_INC(tcp_rack_fastrsm_hack);
20121 rack->fast_rsm_hack = 1;
20123 rack->fast_rsm_hack = 0;
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;
20131 RACK_OPTS_INC(tcp_rack_delayed_ack);
20133 tp->t_delayed_ack = 0;
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);
20141 NET_EPOCH_EXIT(et);
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))
20152 rack->r_ctl.rc_rate_sample_method = optval;
20154 case TCP_DATA_AFTER_CLOSE:
20155 RACK_OPTS_INC(tcp_data_after_close);
20157 rack->rc_allow_data_af_clo = 1;
20159 rack->rc_allow_data_af_clo = 0;
20164 #ifdef NETFLIX_STATS
20165 tcp_log_socket_option(tp, sopt_name, optval, error);
20172 rack_apply_deferred_options(struct tcp_rack *rack)
20174 struct deferred_opt_list *dol, *sdol;
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);
20187 rack_hw_tls_change(struct tcpcb *tp, int chg)
20190 * HW tls state has changed.. fix all
20193 struct tcp_rack *rack;
20194 struct rack_sendmap *rsm;
20196 rack = (struct tcp_rack *)tp->t_fb_ptr;
20197 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
20204 rack->r_ctl.fsb.hw_tls = 1;
20206 rack->r_ctl.fsb.hw_tls = 0;
20210 rack_pru_options(struct tcpcb *tp, int flags)
20212 if (flags & PRUS_OOB)
20213 return (EOPNOTSUPP);
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,
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
20244 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
20245 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
20248 int32_t error = 0, optval;
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 */
20305 /* Filter off all unknown options to the base stack */
20306 return (tcp_default_ctloutput(so, sopt, inp, tp));
20310 if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
20311 error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
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.
20316 optval = (uint32_t)loptval;
20318 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
20319 /* Save it in 64 bit form too */
20325 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
20327 return (ECONNRESET);
20329 if (tp->t_fb != &__tcp_rack) {
20331 return (ENOPROTOOPT);
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)) {
20343 /* No memory to defer, fail */
20348 error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval);
20354 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
20357 INP_WLOCK_ASSERT(tp->t_inpcb);
20358 bzero(ti, sizeof(*ti));
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;
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;
20383 * FreeBSD-specific extension fields for tcp_info.
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));
20401 if (tp->t_flags & TF_TOE) {
20402 ti->tcpi_options |= TCPI_OPT_TOE;
20403 tcp_offload_tcp_info(tp, ti);
20409 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
20410 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
20412 int32_t error, optval;
20413 uint64_t val, loptval;
20414 struct tcp_info ti;
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.
20422 switch (sopt->sopt_name) {
20424 /* First get the info filled */
20425 rack_fill_info(tp, &ti);
20426 /* Fix up the rtt related fields if needed */
20428 error = sooptcopyout(sopt, &ti, sizeof ti);
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.
20436 case TCP_RACK_PACING_BETA:
20437 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20439 else if (rack->rc_pacing_cc_set == 0)
20440 optval = rack->r_ctl.rc_saved_beta.beta;
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.
20447 if (tp->ccv->cc_data)
20448 optval = ((struct newreno *)tp->ccv->cc_data)->beta;
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 :)
20461 case TCP_RACK_PACING_BETA_ECN:
20462 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0)
20464 else if (rack->rc_pacing_cc_set == 0)
20465 optval = rack->r_ctl.rc_saved_beta.beta_ecn;
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.
20472 if (tp->ccv->cc_data)
20473 optval = ((struct newreno *)tp->ccv->cc_data)->beta_ecn;
20478 case TCP_RACK_DSACK_OPT:
20480 if (rack->rc_rack_tmr_std_based) {
20483 if (rack->rc_rack_use_dsack) {
20487 case TCP_RACK_ENABLE_HYSTART:
20489 struct sockopt sopt;
20490 struct cc_newreno_opts opt;
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);
20502 case TCP_FAST_RSM_HACK:
20503 optval = rack->fast_rsm_hack;
20505 case TCP_DEFER_OPTIONS:
20506 optval = rack->defer_options;
20508 case TCP_RACK_MEASURE_CNT:
20509 optval = rack->r_ctl.req_measurements;
20511 case TCP_REC_ABC_VAL:
20512 optval = rack->r_use_labc_for_rec;
20514 case TCP_RACK_ABC_VAL:
20515 optval = rack->rc_labc;
20517 case TCP_HDWR_UP_ONLY:
20518 optval= rack->r_up_only;
20520 case TCP_PACING_RATE_CAP:
20521 loptval = rack->r_ctl.bw_rate_cap;
20523 case TCP_RACK_PROFILE:
20524 /* You cannot retrieve a profile, its write only */
20527 case TCP_USE_CMP_ACKS:
20528 optval = rack->r_use_cmp_ack;
20530 case TCP_RACK_PACE_TO_FILL:
20531 optval = rack->rc_pace_to_cwnd;
20532 if (optval && rack->r_fill_less_agg)
20535 case TCP_RACK_NO_PUSH_AT_MAX:
20536 optval = rack->r_ctl.rc_no_push_at_mrtt;
20538 case TCP_SHARED_CWND_ENABLE:
20539 optval = rack->rack_enable_scwnd;
20541 case TCP_RACK_NONRXT_CFG_RATE:
20542 optval = rack->rack_rec_nonrxt_use_cr;
20545 if (rack->rack_no_prr == 1)
20547 else if (rack->no_prr_addback == 1)
20552 case TCP_RACK_DO_DETECTION:
20553 optval = rack->do_detection;
20555 case TCP_RACK_MBUF_QUEUE:
20556 /* Now do we use the LRO mbuf-queue feature */
20557 optval = rack->r_mbuf_queue;
20559 case TCP_TIMELY_DYN_ADJ:
20560 optval = rack->rc_gp_dyn_mul;
20562 case TCP_BBR_IWINTSO:
20563 optval = rack->rc_init_win;
20565 case TCP_RACK_TLP_REDUCE:
20566 /* RACK TLP cwnd reduction (bool) */
20567 optval = rack->r_ctl.rc_tlp_cwnd_reduce;
20569 case TCP_BBR_RACK_INIT_RATE:
20570 val = rack->r_ctl.init_rate;
20571 /* convert to kbits per sec */
20574 optval = (uint32_t)val;
20576 case TCP_RACK_FORCE_MSEG:
20577 optval = rack->rc_force_max_seg;
20579 case TCP_RACK_PACE_MAX_SEG:
20580 /* Max segments in a pace */
20581 optval = rack->rc_user_set_max_segs;
20583 case TCP_RACK_PACE_ALWAYS:
20584 /* Use the always pace method */
20585 optval = rack->rc_always_pace;
20587 case TCP_RACK_PRR_SENDALOT:
20588 /* Allow PRR to send more than one seg */
20589 optval = rack->r_ctl.rc_prr_sendalot;
20591 case TCP_RACK_MIN_TO:
20592 /* Minimum time between rack t-o's in ms */
20593 optval = rack->r_ctl.rc_min_to;
20595 case TCP_RACK_EARLY_SEG:
20596 /* If early recovery max segments */
20597 optval = rack->r_ctl.rc_early_recovery_segs;
20599 case TCP_RACK_REORD_THRESH:
20600 /* RACK reorder threshold (shift amount) */
20601 optval = rack->r_ctl.rc_reorder_shift;
20603 case TCP_RACK_REORD_FADE:
20604 /* Does reordering fade after ms time */
20605 optval = rack->r_ctl.rc_reorder_fade;
20607 case TCP_BBR_USE_RACK_RR:
20608 /* Do we use the rack cheat for rxt */
20609 optval = rack->use_rack_rr;
20611 case TCP_RACK_RR_CONF:
20612 optval = rack->r_rr_config;
20614 case TCP_HDWR_RATE_CAP:
20615 optval = rack->r_rack_hw_rate_caps;
20617 case TCP_BBR_HDWR_PACE:
20618 optval = rack->rack_hdw_pace_ena;
20620 case TCP_RACK_TLP_THRESH:
20621 /* RACK TLP theshold i.e. srtt+(srtt/N) */
20622 optval = rack->r_ctl.rc_tlp_threshold;
20624 case TCP_RACK_PKT_DELAY:
20625 /* RACK added ms i.e. rack-rtt + reord + N */
20626 optval = rack->r_ctl.rc_pkt_delay;
20628 case TCP_RACK_TLP_USE:
20629 optval = rack->rack_tlp_threshold_use;
20631 case TCP_RACK_PACE_RATE_CA:
20632 optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
20634 case TCP_RACK_PACE_RATE_SS:
20635 optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
20637 case TCP_RACK_PACE_RATE_REC:
20638 optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
20640 case TCP_RACK_GP_INCREASE_SS:
20641 optval = rack->r_ctl.rack_per_of_gp_ca;
20643 case TCP_RACK_GP_INCREASE_CA:
20644 optval = rack->r_ctl.rack_per_of_gp_ss;
20646 case TCP_BBR_RACK_RTT_USE:
20647 optval = rack->r_ctl.rc_rate_sample_method;
20650 optval = tp->t_delayed_ack;
20652 case TCP_DATA_AFTER_CLOSE:
20653 optval = rack->rc_allow_data_af_clo;
20655 case TCP_SHARED_CWND_TIME_LIMIT:
20656 optval = rack->r_limit_scw;
20658 case TCP_RACK_TIMER_SLOP:
20659 optval = rack->r_ctl.timer_slop;
20662 return (tcp_default_ctloutput(so, sopt, inp, tp));
20667 if (TCP_PACING_RATE_CAP)
20668 error = sooptcopyout(sopt, &loptval, sizeof loptval);
20670 error = sooptcopyout(sopt, &optval, sizeof optval);
20676 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
20678 int32_t error = EINVAL;
20679 struct tcp_rack *rack;
20681 rack = (struct tcp_rack *)tp->t_fb_ptr;
20682 if (rack == NULL) {
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));
20696 static const char *rack_stack_names[] = {
20697 __XSTRING(STACKNAME),
20699 __XSTRING(STACKALIAS),
20704 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
20706 memset(mem, 0, size);
20711 rack_dtor(void *mem, int32_t size, void *arg)
20716 static bool rack_mod_inited = false;
20719 tcp_addrack(module_t mod, int32_t type, void *data)
20726 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
20727 sizeof(struct rack_sendmap),
20728 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
20730 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
20731 sizeof(struct tcp_rack),
20732 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
20734 sysctl_ctx_init(&rack_sysctl_ctx);
20735 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
20736 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
20739 __XSTRING(STACKALIAS),
20741 __XSTRING(STACKNAME),
20743 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
20745 if (rack_sysctl_root == NULL) {
20746 printf("Failed to add sysctl node\n");
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);
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);
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);
20766 tcp_lro_reg_mbufq();
20767 rack_mod_inited = true;
20770 err = deregister_tcp_functions(&__tcp_rack, true, false);
20773 err = deregister_tcp_functions(&__tcp_rack, false, true);
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;
20783 tcp_lro_dereg_mbufq();
20787 return (EOPNOTSUPP);
20792 static moduledata_t tcp_rack = {
20793 .name = __XSTRING(MODNAME),
20794 .evhand = tcp_addrack,
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);