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_ratelimit.h"
34 #include "opt_kern_tls.h"
35 #if defined(INET) || defined(INET6)
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_seq.h>
93 #include <netinet/tcp_timer.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/tcp_log_buf.h>
96 #include <netinet/tcp_syncache.h>
97 #include <netinet/tcp_hpts.h>
98 #include <netinet/tcp_ratelimit.h>
99 #include <netinet/tcp_accounting.h>
100 #include <netinet/tcpip.h>
101 #include <netinet/cc/cc.h>
102 #include <netinet/cc/cc_newreno.h>
103 #include <netinet/tcp_fastopen.h>
104 #include <netinet/tcp_lro.h>
105 #ifdef NETFLIX_SHARED_CWND
106 #include <netinet/tcp_shared_cwnd.h>
109 #include <netinet/tcp_offload.h>
112 #include <netinet6/tcp6_var.h>
114 #include <netinet/tcp_ecn.h>
116 #include <netipsec/ipsec_support.h>
118 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
119 #include <netipsec/ipsec.h>
120 #include <netipsec/ipsec6.h>
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
125 #include <machine/in_cksum.h>
128 #include <security/mac/mac_framework.h>
130 #include "sack_filter.h"
131 #include "tcp_rack.h"
132 #include "tailq_hash.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_segment
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 uint32_t rack_clamp_ss_upper = 110;
196 static uint32_t rack_clamp_ca_upper = 105;
197 static uint32_t rack_rxt_min_rnds = 10; /* Min rounds if drastic rxt clamp is in place */
198 static uint32_t rack_unclamp_round_thresh = 100; /* number of perfect rounds before we unclamp */
199 static uint32_t rack_unclamp_rxt_thresh = 5; /* .5% and under */
200 static uint64_t rack_rxt_clamp_thresh = 0; /* Do we do the rxt clamp thing */
201 static int32_t rack_dnd_default = 0; /* For rr_conf = 3, what is the default for dnd */
202 static int32_t rack_rxt_controls = 0;
203 static int32_t rack_fill_cw_state = 0;
204 static uint8_t rack_req_measurements = 1;
205 /* Attack threshold detections */
206 static uint32_t rack_highest_sack_thresh_seen = 0;
207 static uint32_t rack_highest_move_thresh_seen = 0;
208 static uint32_t rack_merge_out_sacks_on_attack = 0;
209 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */
210 static int32_t rack_hw_pace_extra_slots = 0; /* 2 extra MSS time betweens */
211 static int32_t rack_hw_rate_caps = 0; /* 1; */
212 static int32_t rack_hw_rate_cap_per = 0; /* 0 -- off */
213 static int32_t rack_hw_rate_min = 0; /* 1500000;*/
214 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */
215 static int32_t rack_hw_up_only = 0;
216 static int32_t rack_stats_gets_ms_rtt = 1;
217 static int32_t rack_prr_addbackmax = 2;
218 static int32_t rack_do_hystart = 0;
219 static int32_t rack_apply_rtt_with_reduced_conf = 0;
220 static int32_t rack_hibeta_setting = 0;
221 static int32_t rack_default_pacing_divisor = 250;
222 static int32_t rack_uses_full_dgp_in_rec = 1;
223 static uint16_t rack_pacing_min_seg = 0;
226 static uint32_t sad_seg_size_per = 800; /* 80.0 % */
227 static int32_t rack_pkt_delay = 1000;
228 static int32_t rack_send_a_lot_in_prr = 1;
229 static int32_t rack_min_to = 1000; /* Number of microsecond min timeout */
230 static int32_t rack_verbose_logging = 0;
231 static int32_t rack_ignore_data_after_close = 1;
232 static int32_t rack_enable_shared_cwnd = 1;
233 static int32_t rack_use_cmp_acks = 1;
234 static int32_t rack_use_fsb = 1;
235 static int32_t rack_use_rfo = 1;
236 static int32_t rack_use_rsm_rfo = 1;
237 static int32_t rack_max_abc_post_recovery = 2;
238 static int32_t rack_client_low_buf = 0;
239 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 */
240 static int32_t rack_bw_multipler = 2; /* Limit on fill cw's jump up to be this x gp_est */
241 #ifdef TCP_ACCOUNTING
242 static int32_t rack_tcp_accounting = 0;
244 static int32_t rack_limits_scwnd = 1;
245 static int32_t rack_enable_mqueue_for_nonpaced = 0;
246 static int32_t rack_hybrid_allow_set_maxseg = 0;
247 static int32_t rack_disable_prr = 0;
248 static int32_t use_rack_rr = 1;
249 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
250 static int32_t rack_persist_min = 250000; /* 250usec */
251 static int32_t rack_persist_max = 2000000; /* 2 Second in usec's */
252 static int32_t rack_sack_not_required = 1; /* set to one to allow non-sack to use rack */
253 static int32_t rack_default_init_window = 0; /* Use system default */
254 static int32_t rack_limit_time_with_srtt = 0;
255 static int32_t rack_autosndbuf_inc = 20; /* In percentage form */
256 static int32_t rack_enobuf_hw_boost_mult = 0; /* How many times the hw rate we boost slot using time_between */
257 static int32_t rack_enobuf_hw_max = 12000; /* 12 ms in usecs */
258 static int32_t rack_enobuf_hw_min = 10000; /* 10 ms in usecs */
259 static int32_t rack_hw_rwnd_factor = 2; /* How many max_segs the rwnd must be before we hold off sending */
260 static int32_t rack_hw_check_queue = 0; /* Do we always pre-check queue depth of a hw queue */
261 static int32_t rack_full_buffer_discount = 10;
263 * Currently regular tcp has a rto_min of 30ms
264 * the backoff goes 12 times so that ends up
265 * being a total of 122.850 seconds before a
266 * connection is killed.
268 static uint32_t rack_def_data_window = 20;
269 static uint32_t rack_goal_bdp = 2;
270 static uint32_t rack_min_srtts = 1;
271 static uint32_t rack_min_measure_usec = 0;
272 static int32_t rack_tlp_min = 10000; /* 10ms */
273 static int32_t rack_rto_min = 30000; /* 30,000 usec same as main freebsd */
274 static int32_t rack_rto_max = 4000000; /* 4 seconds in usec's */
275 static const int32_t rack_free_cache = 2;
276 static int32_t rack_hptsi_segments = 40;
277 static int32_t rack_rate_sample_method = USE_RTT_LOW;
278 static int32_t rack_pace_every_seg = 0;
279 static int32_t rack_delayed_ack_time = 40000; /* 40ms in usecs */
280 static int32_t rack_slot_reduction = 4;
281 static int32_t rack_wma_divisor = 8; /* For WMA calculation */
282 static int32_t rack_cwnd_block_ends_measure = 0;
283 static int32_t rack_rwnd_block_ends_measure = 0;
284 static int32_t rack_def_profile = 0;
286 static int32_t rack_lower_cwnd_at_tlp = 0;
287 static int32_t rack_limited_retran = 0;
288 static int32_t rack_always_send_oldest = 0;
289 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
291 static uint16_t rack_per_of_gp_ss = 250; /* 250 % slow-start */
292 static uint16_t rack_per_of_gp_ca = 200; /* 200 % congestion-avoidance */
293 static uint16_t rack_per_of_gp_rec = 200; /* 200 % of bw */
296 static uint16_t rack_per_of_gp_probertt = 60; /* 60% of bw */
297 static uint16_t rack_per_of_gp_lowthresh = 40; /* 40% is bottom */
298 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
299 static uint16_t rack_atexit_prtt_hbp = 130; /* Clamp to 130% on exit prtt if highly buffered path */
300 static uint16_t rack_atexit_prtt = 130; /* Clamp to 100% on exit prtt if non highly buffered path */
302 static uint32_t rack_max_drain_wait = 2; /* How man gp srtt's before we give up draining */
303 static uint32_t rack_must_drain = 1; /* How many GP srtt's we *must* wait */
304 static uint32_t rack_probertt_use_min_rtt_entry = 1; /* Use the min to calculate the goal else gp_srtt */
305 static uint32_t rack_probertt_use_min_rtt_exit = 0;
306 static uint32_t rack_probe_rtt_sets_cwnd = 0;
307 static uint32_t rack_probe_rtt_safety_val = 2000000; /* No more than 2 sec in probe-rtt */
308 static uint32_t rack_time_between_probertt = 9600000; /* 9.6 sec in usecs */
309 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0; /* How many srtt periods does probe-rtt last top fraction */
310 static uint32_t rack_probertt_gpsrtt_cnt_div = 0; /* How many srtt periods does probe-rtt last bottom fraction */
311 static uint32_t rack_min_probertt_hold = 40000; /* Equal to delayed ack time */
312 static uint32_t rack_probertt_filter_life = 10000000;
313 static uint32_t rack_probertt_lower_within = 10;
314 static uint32_t rack_min_rtt_movement = 250000; /* Must move at least 250ms (in microseconds) to count as a lowering */
315 static int32_t rack_pace_one_seg = 0; /* Shall we pace for less than 1.4Meg 1MSS at a time */
316 static int32_t rack_probertt_clear_is = 1;
317 static int32_t rack_max_drain_hbp = 1; /* Extra drain times gpsrtt for highly buffered paths */
318 static int32_t rack_hbp_thresh = 3; /* what is the divisor max_rtt/min_rtt to decided a hbp */
321 static int32_t rack_max_per_above = 30; /* When we go to increment stop if above 100+this% */
323 /* Timely information */
324 /* Combine these two gives the range of 'no change' to bw */
325 /* ie the up/down provide the upper and lower bound */
326 static int32_t rack_gp_per_bw_mul_up = 2; /* 2% */
327 static int32_t rack_gp_per_bw_mul_down = 4; /* 4% */
328 static int32_t rack_gp_rtt_maxmul = 3; /* 3 x maxmin */
329 static int32_t rack_gp_rtt_minmul = 1; /* minrtt + (minrtt/mindiv) is lower rtt */
330 static int32_t rack_gp_rtt_mindiv = 4; /* minrtt + (minrtt * minmul/mindiv) is lower rtt */
331 static int32_t rack_gp_decrease_per = 20; /* 20% decrease in multiplier */
332 static int32_t rack_gp_increase_per = 2; /* 2% increase in multiplier */
333 static int32_t rack_per_lower_bound = 50; /* Don't allow to drop below this multiplier */
334 static int32_t rack_per_upper_bound_ss = 0; /* Don't allow SS to grow above this */
335 static int32_t rack_per_upper_bound_ca = 0; /* Don't allow CA to grow above this */
336 static int32_t rack_do_dyn_mul = 0; /* Are the rack gp multipliers dynamic */
337 static int32_t rack_gp_no_rec_chg = 1; /* Prohibit recovery from reducing it's multiplier */
338 static int32_t rack_timely_dec_clear = 6; /* Do we clear decrement count at a value (6)? */
339 static int32_t rack_timely_max_push_rise = 3; /* One round of pushing */
340 static int32_t rack_timely_max_push_drop = 3; /* Three round of pushing */
341 static int32_t rack_timely_min_segs = 4; /* 4 segment minimum */
342 static int32_t rack_use_max_for_nobackoff = 0;
343 static int32_t rack_timely_int_timely_only = 0; /* do interim timely's only use the timely algo (no b/w changes)? */
344 static int32_t rack_timely_no_stopping = 0;
345 static int32_t rack_down_raise_thresh = 100;
346 static int32_t rack_req_segs = 1;
347 static uint64_t rack_bw_rate_cap = 0;
350 /* Rack specific counters */
351 counter_u64_t rack_saw_enobuf;
352 counter_u64_t rack_saw_enobuf_hw;
353 counter_u64_t rack_saw_enetunreach;
354 counter_u64_t rack_persists_sends;
355 counter_u64_t rack_persists_acks;
356 counter_u64_t rack_persists_loss;
357 counter_u64_t rack_persists_lost_ends;
358 counter_u64_t rack_total_bytes;
360 counter_u64_t rack_adjust_map_bw;
362 /* Tail loss probe counters */
363 counter_u64_t rack_tlp_tot;
364 counter_u64_t rack_tlp_newdata;
365 counter_u64_t rack_tlp_retran;
366 counter_u64_t rack_tlp_retran_bytes;
367 counter_u64_t rack_to_tot;
368 counter_u64_t rack_hot_alloc;
369 counter_u64_t rack_to_alloc;
370 counter_u64_t rack_to_alloc_hard;
371 counter_u64_t rack_to_alloc_emerg;
372 counter_u64_t rack_to_alloc_limited;
373 counter_u64_t rack_alloc_limited_conns;
374 counter_u64_t rack_split_limited;
375 counter_u64_t rack_rxt_clamps_cwnd;
376 counter_u64_t rack_rxt_clamps_cwnd_uniq;
378 counter_u64_t rack_multi_single_eq;
379 counter_u64_t rack_proc_non_comp_ack;
381 counter_u64_t rack_fto_send;
382 counter_u64_t rack_fto_rsm_send;
383 counter_u64_t rack_nfto_resend;
384 counter_u64_t rack_non_fto_send;
385 counter_u64_t rack_extended_rfo;
387 counter_u64_t rack_sack_proc_all;
388 counter_u64_t rack_sack_proc_short;
389 counter_u64_t rack_sack_proc_restart;
390 counter_u64_t rack_sack_attacks_detected;
391 counter_u64_t rack_sack_attacks_reversed;
392 counter_u64_t rack_sack_attacks_suspect;
393 counter_u64_t rack_sack_used_next_merge;
394 counter_u64_t rack_sack_splits;
395 counter_u64_t rack_sack_used_prev_merge;
396 counter_u64_t rack_sack_skipped_acked;
397 counter_u64_t rack_ack_total;
398 counter_u64_t rack_express_sack;
399 counter_u64_t rack_sack_total;
400 counter_u64_t rack_move_none;
401 counter_u64_t rack_move_some;
403 counter_u64_t rack_input_idle_reduces;
404 counter_u64_t rack_collapsed_win;
405 counter_u64_t rack_collapsed_win_seen;
406 counter_u64_t rack_collapsed_win_rxt;
407 counter_u64_t rack_collapsed_win_rxt_bytes;
408 counter_u64_t rack_try_scwnd;
409 counter_u64_t rack_hw_pace_init_fail;
410 counter_u64_t rack_hw_pace_lost;
412 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
413 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
416 #define RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2)))
418 #define RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do { \
419 (tv) = (value) + slop; \
420 if ((u_long)(tv) < (u_long)(tvmin)) \
422 if ((u_long)(tv) > (u_long)(tvmax)) \
427 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line);
430 rack_process_ack(struct mbuf *m, struct tcphdr *th,
431 struct socket *so, struct tcpcb *tp, struct tcpopt *to,
432 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
434 rack_process_data(struct mbuf *m, struct tcphdr *th,
435 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
436 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
438 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
439 uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery);
440 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
441 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
443 static struct rack_sendmap *
444 rack_check_recovery_mode(struct tcpcb *tp,
447 rack_cong_signal(struct tcpcb *tp,
448 uint32_t type, uint32_t ack, int );
449 static void rack_counter_destroy(void);
451 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
452 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
454 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override);
456 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
457 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos);
458 static void rack_dtor(void *mem, int32_t size, void *arg);
460 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
461 uint32_t flex1, uint32_t flex2,
462 uint32_t flex3, uint32_t flex4,
463 uint32_t flex5, uint32_t flex6,
464 uint16_t flex7, uint8_t mod);
467 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
468 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line,
469 struct rack_sendmap *rsm, uint8_t quality);
470 static struct rack_sendmap *
471 rack_find_high_nonack(struct tcp_rack *rack,
472 struct rack_sendmap *rsm);
473 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
474 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
475 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
476 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt);
478 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
479 tcp_seq th_ack, int line, uint8_t quality);
481 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm);
484 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
485 static int32_t rack_handoff_ok(struct tcpcb *tp);
486 static int32_t rack_init(struct tcpcb *tp, void **ptr);
487 static void rack_init_sysctls(void);
490 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
491 struct tcphdr *th, int entered_rec, int dup_ack_struck,
492 int *dsack_seen, int *sacks_seen);
494 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
495 uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts,
496 struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz);
498 static uint64_t rack_get_gp_est(struct tcp_rack *rack);
501 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
502 struct rack_sendmap *rsm);
503 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
504 static int32_t rack_output(struct tcpcb *tp);
507 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
508 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
509 uint32_t cts, int *no_extra, int *moved_two, uint32_t segsiz);
510 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq);
511 static void rack_remxt_tmr(struct tcpcb *tp);
512 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt);
513 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
514 static int32_t rack_stopall(struct tcpcb *tp);
515 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
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, int segsiz);
520 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
521 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag, int segsiz);
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 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts);
563 struct rack_sendmap *
564 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
566 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
567 uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
569 tcp_rack_partialack(struct tcpcb *tp);
571 rack_set_profile(struct tcp_rack *rack, int prof);
573 rack_apply_deferred_options(struct tcp_rack *rack);
575 int32_t rack_clear_counter=0;
578 rack_get_lt_bw(struct tcp_rack *rack)
583 tim = rack->r_ctl.lt_bw_time;
584 bytes = rack->r_ctl.lt_bw_bytes;
585 if (rack->lt_bw_up) {
586 /* Include all the current bytes too */
588 bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq);
589 tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
591 if ((bytes != 0) && (tim != 0))
592 return ((bytes * (uint64_t)1000000) / tim);
598 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8)
601 struct cc_newreno_opts opt;
604 int error, failed = 0;
607 if (tp->t_cc == NULL) {
611 rack->rc_pacing_cc_set = 1;
612 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
613 /* Not new-reno we can't play games with beta! */
618 if (CC_ALGO(tp)->ctl_output == NULL) {
619 /* Huh, not using new-reno so no swaps.? */
623 /* Get the current values out */
624 sopt.sopt_valsize = sizeof(struct cc_newreno_opts);
625 sopt.sopt_dir = SOPT_GET;
626 opt.name = CC_NEWRENO_BETA;
627 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
633 opt.name = CC_NEWRENO_BETA_ECN;
634 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
639 old.beta_ecn = opt.val;
641 /* Now lets set in the values we have stored */
642 sopt.sopt_dir = SOPT_SET;
643 opt.name = CC_NEWRENO_BETA;
644 opt.val = rack->r_ctl.rc_saved_beta.beta;
645 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
650 opt.name = CC_NEWRENO_BETA_ECN;
651 opt.val = rack->r_ctl.rc_saved_beta.beta_ecn;
652 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
657 /* Save off the values for restoral */
658 memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno));
660 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
661 union tcp_log_stackspecific log;
665 ptr = ((struct newreno *)tp->t_ccv.cc_data);
666 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
667 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
668 log.u_bbr.flex1 = ptr->beta;
669 log.u_bbr.flex2 = ptr->beta_ecn;
670 log.u_bbr.flex3 = ptr->newreno_flags;
671 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta;
672 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn;
673 log.u_bbr.flex6 = failed;
674 log.u_bbr.flex7 = rack->gp_ready;
675 log.u_bbr.flex7 <<= 1;
676 log.u_bbr.flex7 |= rack->use_fixed_rate;
677 log.u_bbr.flex7 <<= 1;
678 log.u_bbr.flex7 |= rack->rc_pacing_cc_set;
679 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
680 log.u_bbr.flex8 = flex8;
681 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error,
682 0, &log, false, NULL, NULL, 0, &tv);
687 rack_set_cc_pacing(struct tcp_rack *rack)
689 if (rack->rc_pacing_cc_set)
692 * Use the swap utility placing in 3 for flex8 to id a
693 * set of a new set of values.
695 rack->rc_pacing_cc_set = 1;
696 rack_swap_beta_values(rack, 3);
700 rack_undo_cc_pacing(struct tcp_rack *rack)
702 if (rack->rc_pacing_cc_set == 0)
705 * Use the swap utility placing in 4 for flex8 to id a
706 * restoral of the old values.
708 rack->rc_pacing_cc_set = 0;
709 rack_swap_beta_values(rack, 4);
713 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t,
714 uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm)
716 if (tcp_bblogging_on(rack->rc_tp)) {
717 union tcp_log_stackspecific log;
720 memset(&log, 0, sizeof(log));
721 log.u_bbr.flex1 = seq_end;
722 log.u_bbr.flex2 = rack->rc_tp->gput_seq;
723 log.u_bbr.flex3 = ack_end_t;
724 log.u_bbr.flex4 = rack->rc_tp->gput_ts;
725 log.u_bbr.flex5 = send_end_t;
726 log.u_bbr.flex6 = rack->rc_tp->gput_ack;
727 log.u_bbr.flex7 = mode;
728 log.u_bbr.flex8 = 69;
729 log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts;
730 log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts;
731 log.u_bbr.pkts_out = line;
732 log.u_bbr.cwnd_gain = rack->app_limited_needs_set;
733 log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt;
735 log.u_bbr.applimited = rsm->r_start;
736 log.u_bbr.delivered = rsm->r_end;
737 log.u_bbr.epoch = rsm->r_flags;
739 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
740 TCP_LOG_EVENTP(rack->rc_tp, NULL,
741 &rack->rc_inp->inp_socket->so_rcv,
742 &rack->rc_inp->inp_socket->so_snd,
743 BBR_LOG_HPTSI_CALC, 0,
744 0, &log, false, &tv);
749 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
754 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
755 if (error || req->newptr == NULL)
758 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
763 printf("Clearing RACK counters\n");
765 counter_u64_zero(rack_tlp_tot);
766 counter_u64_zero(rack_tlp_newdata);
767 counter_u64_zero(rack_tlp_retran);
768 counter_u64_zero(rack_tlp_retran_bytes);
769 counter_u64_zero(rack_to_tot);
770 counter_u64_zero(rack_saw_enobuf);
771 counter_u64_zero(rack_saw_enobuf_hw);
772 counter_u64_zero(rack_saw_enetunreach);
773 counter_u64_zero(rack_persists_sends);
774 counter_u64_zero(rack_total_bytes);
775 counter_u64_zero(rack_persists_acks);
776 counter_u64_zero(rack_persists_loss);
777 counter_u64_zero(rack_persists_lost_ends);
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_non_fto_send);
790 counter_u64_zero(rack_nfto_resend);
791 counter_u64_zero(rack_sack_proc_short);
792 counter_u64_zero(rack_sack_proc_restart);
793 counter_u64_zero(rack_to_alloc);
794 counter_u64_zero(rack_to_alloc_limited);
795 counter_u64_zero(rack_alloc_limited_conns);
796 counter_u64_zero(rack_split_limited);
797 counter_u64_zero(rack_rxt_clamps_cwnd);
798 counter_u64_zero(rack_rxt_clamps_cwnd_uniq);
799 counter_u64_zero(rack_multi_single_eq);
800 counter_u64_zero(rack_proc_non_comp_ack);
801 counter_u64_zero(rack_sack_attacks_detected);
802 counter_u64_zero(rack_sack_attacks_reversed);
803 counter_u64_zero(rack_sack_attacks_suspect);
804 counter_u64_zero(rack_sack_used_next_merge);
805 counter_u64_zero(rack_sack_used_prev_merge);
806 counter_u64_zero(rack_sack_splits);
807 counter_u64_zero(rack_sack_skipped_acked);
808 counter_u64_zero(rack_ack_total);
809 counter_u64_zero(rack_express_sack);
810 counter_u64_zero(rack_sack_total);
811 counter_u64_zero(rack_move_none);
812 counter_u64_zero(rack_move_some);
813 counter_u64_zero(rack_try_scwnd);
814 counter_u64_zero(rack_collapsed_win);
815 counter_u64_zero(rack_collapsed_win_rxt);
816 counter_u64_zero(rack_collapsed_win_seen);
817 counter_u64_zero(rack_collapsed_win_rxt_bytes);
818 } else if (stat == 2) {
820 printf("Clearing RACK option array\n");
822 COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE);
823 } else if (stat == 3) {
824 printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n");
825 } else if (stat == 4) {
827 printf("Clearing RACK out size array\n");
829 COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE);
831 rack_clear_counter = 0;
836 rack_init_sysctls(void)
838 struct sysctl_oid *rack_counters;
839 struct sysctl_oid *rack_attack;
840 struct sysctl_oid *rack_pacing;
841 struct sysctl_oid *rack_timely;
842 struct sysctl_oid *rack_timers;
843 struct sysctl_oid *rack_tlp;
844 struct sysctl_oid *rack_misc;
845 struct sysctl_oid *rack_features;
846 struct sysctl_oid *rack_measure;
847 struct sysctl_oid *rack_probertt;
848 struct sysctl_oid *rack_hw_pacing;
850 rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
851 SYSCTL_CHILDREN(rack_sysctl_root),
854 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
855 "Rack Sack Attack Counters and Controls");
856 rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
857 SYSCTL_CHILDREN(rack_sysctl_root),
860 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
862 SYSCTL_ADD_S32(&rack_sysctl_ctx,
863 SYSCTL_CHILDREN(rack_sysctl_root),
864 OID_AUTO, "rate_sample_method", CTLFLAG_RW,
865 &rack_rate_sample_method , USE_RTT_LOW,
866 "What method should we use for rate sampling 0=high, 1=low ");
867 /* Probe rtt related controls */
868 rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
869 SYSCTL_CHILDREN(rack_sysctl_root),
872 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
873 "ProbeRTT related Controls");
874 SYSCTL_ADD_U16(&rack_sysctl_ctx,
875 SYSCTL_CHILDREN(rack_probertt),
876 OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
877 &rack_atexit_prtt_hbp, 130,
878 "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
879 SYSCTL_ADD_U16(&rack_sysctl_ctx,
880 SYSCTL_CHILDREN(rack_probertt),
881 OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
882 &rack_atexit_prtt, 130,
883 "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
884 SYSCTL_ADD_U16(&rack_sysctl_ctx,
885 SYSCTL_CHILDREN(rack_probertt),
886 OID_AUTO, "gp_per_mul", CTLFLAG_RW,
887 &rack_per_of_gp_probertt, 60,
888 "What percentage of goodput do we pace at in probertt");
889 SYSCTL_ADD_U16(&rack_sysctl_ctx,
890 SYSCTL_CHILDREN(rack_probertt),
891 OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
892 &rack_per_of_gp_probertt_reduce, 10,
893 "What percentage of goodput do we reduce every gp_srtt");
894 SYSCTL_ADD_U16(&rack_sysctl_ctx,
895 SYSCTL_CHILDREN(rack_probertt),
896 OID_AUTO, "gp_per_low", CTLFLAG_RW,
897 &rack_per_of_gp_lowthresh, 40,
898 "What percentage of goodput do we allow the multiplier to fall to");
899 SYSCTL_ADD_U32(&rack_sysctl_ctx,
900 SYSCTL_CHILDREN(rack_probertt),
901 OID_AUTO, "time_between", CTLFLAG_RW,
902 & rack_time_between_probertt, 96000000,
903 "How many useconds between the lowest rtt falling must past before we enter probertt");
904 SYSCTL_ADD_U32(&rack_sysctl_ctx,
905 SYSCTL_CHILDREN(rack_probertt),
906 OID_AUTO, "safety", CTLFLAG_RW,
907 &rack_probe_rtt_safety_val, 2000000,
908 "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
909 SYSCTL_ADD_U32(&rack_sysctl_ctx,
910 SYSCTL_CHILDREN(rack_probertt),
911 OID_AUTO, "sets_cwnd", CTLFLAG_RW,
912 &rack_probe_rtt_sets_cwnd, 0,
913 "Do we set the cwnd too (if always_lower is on)");
914 SYSCTL_ADD_U32(&rack_sysctl_ctx,
915 SYSCTL_CHILDREN(rack_probertt),
916 OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
917 &rack_max_drain_wait, 2,
918 "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
919 SYSCTL_ADD_U32(&rack_sysctl_ctx,
920 SYSCTL_CHILDREN(rack_probertt),
921 OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
923 "We must drain this many gp_srtt's waiting for flight to reach goal");
924 SYSCTL_ADD_U32(&rack_sysctl_ctx,
925 SYSCTL_CHILDREN(rack_probertt),
926 OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
927 &rack_probertt_use_min_rtt_entry, 1,
928 "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
929 SYSCTL_ADD_U32(&rack_sysctl_ctx,
930 SYSCTL_CHILDREN(rack_probertt),
931 OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
932 &rack_probertt_use_min_rtt_exit, 0,
933 "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
934 SYSCTL_ADD_U32(&rack_sysctl_ctx,
935 SYSCTL_CHILDREN(rack_probertt),
936 OID_AUTO, "length_div", CTLFLAG_RW,
937 &rack_probertt_gpsrtt_cnt_div, 0,
938 "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
939 SYSCTL_ADD_U32(&rack_sysctl_ctx,
940 SYSCTL_CHILDREN(rack_probertt),
941 OID_AUTO, "length_mul", CTLFLAG_RW,
942 &rack_probertt_gpsrtt_cnt_mul, 0,
943 "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
944 SYSCTL_ADD_U32(&rack_sysctl_ctx,
945 SYSCTL_CHILDREN(rack_probertt),
946 OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
947 &rack_min_probertt_hold, 200000,
948 "What is the minimum time we hold probertt at target");
949 SYSCTL_ADD_U32(&rack_sysctl_ctx,
950 SYSCTL_CHILDREN(rack_probertt),
951 OID_AUTO, "filter_life", CTLFLAG_RW,
952 &rack_probertt_filter_life, 10000000,
953 "What is the time for the filters life in useconds");
954 SYSCTL_ADD_U32(&rack_sysctl_ctx,
955 SYSCTL_CHILDREN(rack_probertt),
956 OID_AUTO, "lower_within", CTLFLAG_RW,
957 &rack_probertt_lower_within, 10,
958 "If the rtt goes lower within this percentage of the time, go into probe-rtt");
959 SYSCTL_ADD_U32(&rack_sysctl_ctx,
960 SYSCTL_CHILDREN(rack_probertt),
961 OID_AUTO, "must_move", CTLFLAG_RW,
962 &rack_min_rtt_movement, 250,
963 "How much is the minimum movement in rtt to count as a drop for probertt purposes");
964 SYSCTL_ADD_U32(&rack_sysctl_ctx,
965 SYSCTL_CHILDREN(rack_probertt),
966 OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
967 &rack_probertt_clear_is, 1,
968 "Do we clear I/S counts on exiting probe-rtt");
969 SYSCTL_ADD_S32(&rack_sysctl_ctx,
970 SYSCTL_CHILDREN(rack_probertt),
971 OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
972 &rack_max_drain_hbp, 1,
973 "How many extra drain gpsrtt's do we get in highly buffered paths");
974 SYSCTL_ADD_S32(&rack_sysctl_ctx,
975 SYSCTL_CHILDREN(rack_probertt),
976 OID_AUTO, "hbp_threshold", CTLFLAG_RW,
978 "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
979 /* Pacing related sysctls */
980 rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
981 SYSCTL_CHILDREN(rack_sysctl_root),
984 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
985 "Pacing related Controls");
986 SYSCTL_ADD_S32(&rack_sysctl_ctx,
987 SYSCTL_CHILDREN(rack_pacing),
988 OID_AUTO, "fulldgpinrec", CTLFLAG_RW,
989 &rack_uses_full_dgp_in_rec, 1,
990 "Do we use all DGP features in recovery (fillcw, timely et.al.)?");
991 SYSCTL_ADD_S32(&rack_sysctl_ctx,
992 SYSCTL_CHILDREN(rack_pacing),
993 OID_AUTO, "fullbufdisc", CTLFLAG_RW,
994 &rack_full_buffer_discount, 10,
995 "What percentage b/w reduction over the GP estimate for a full buffer (default=0 off)?");
996 SYSCTL_ADD_S32(&rack_sysctl_ctx,
997 SYSCTL_CHILDREN(rack_pacing),
998 OID_AUTO, "fillcw", CTLFLAG_RW,
999 &rack_fill_cw_state, 0,
1000 "Enable fillcw on new connections (default=0 off)?");
1001 SYSCTL_ADD_U16(&rack_sysctl_ctx,
1002 SYSCTL_CHILDREN(rack_pacing),
1003 OID_AUTO, "min_burst", CTLFLAG_RW,
1004 &rack_pacing_min_seg, 0,
1005 "What is the min burst size for pacing (0 disables)?");
1006 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1007 SYSCTL_CHILDREN(rack_pacing),
1008 OID_AUTO, "divisor", CTLFLAG_RW,
1009 &rack_default_pacing_divisor, 4,
1010 "What is the default divisor given to the rl code?");
1011 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1012 SYSCTL_CHILDREN(rack_pacing),
1013 OID_AUTO, "fillcw_max_mult", CTLFLAG_RW,
1014 &rack_bw_multipler, 2,
1015 "What is the multiplier of the current gp_est that fillcw can increase the b/w too?");
1016 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1017 SYSCTL_CHILDREN(rack_pacing),
1018 OID_AUTO, "max_pace_over", CTLFLAG_RW,
1019 &rack_max_per_above, 30,
1020 "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
1021 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1022 SYSCTL_CHILDREN(rack_pacing),
1023 OID_AUTO, "allow1mss", CTLFLAG_RW,
1024 &rack_pace_one_seg, 0,
1025 "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?");
1026 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1027 SYSCTL_CHILDREN(rack_pacing),
1028 OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
1029 &rack_limit_time_with_srtt, 0,
1030 "Do we limit pacing time based on srtt");
1031 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1032 SYSCTL_CHILDREN(rack_pacing),
1033 OID_AUTO, "init_win", CTLFLAG_RW,
1034 &rack_default_init_window, 0,
1035 "Do we have a rack initial window 0 = system default");
1036 SYSCTL_ADD_U16(&rack_sysctl_ctx,
1037 SYSCTL_CHILDREN(rack_pacing),
1038 OID_AUTO, "gp_per_ss", CTLFLAG_RW,
1039 &rack_per_of_gp_ss, 250,
1040 "If non zero, what percentage of goodput to pace at in slow start");
1041 SYSCTL_ADD_U16(&rack_sysctl_ctx,
1042 SYSCTL_CHILDREN(rack_pacing),
1043 OID_AUTO, "gp_per_ca", CTLFLAG_RW,
1044 &rack_per_of_gp_ca, 150,
1045 "If non zero, what percentage of goodput to pace at in congestion avoidance");
1046 SYSCTL_ADD_U16(&rack_sysctl_ctx,
1047 SYSCTL_CHILDREN(rack_pacing),
1048 OID_AUTO, "gp_per_rec", CTLFLAG_RW,
1049 &rack_per_of_gp_rec, 200,
1050 "If non zero, what percentage of goodput to pace at in recovery");
1051 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1052 SYSCTL_CHILDREN(rack_pacing),
1053 OID_AUTO, "pace_max_seg", CTLFLAG_RW,
1054 &rack_hptsi_segments, 40,
1055 "What size is the max for TSO segments in pacing and burst mitigation");
1056 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1057 SYSCTL_CHILDREN(rack_pacing),
1058 OID_AUTO, "burst_reduces", CTLFLAG_RW,
1059 &rack_slot_reduction, 4,
1060 "When doing only burst mitigation what is the reduce divisor");
1061 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1062 SYSCTL_CHILDREN(rack_sysctl_root),
1063 OID_AUTO, "use_pacing", CTLFLAG_RW,
1064 &rack_pace_every_seg, 0,
1065 "If set we use pacing, if clear we use only the original burst mitigation");
1066 SYSCTL_ADD_U64(&rack_sysctl_ctx,
1067 SYSCTL_CHILDREN(rack_pacing),
1068 OID_AUTO, "rate_cap", CTLFLAG_RW,
1069 &rack_bw_rate_cap, 0,
1070 "If set we apply this value to the absolute rate cap used by pacing");
1071 SYSCTL_ADD_U8(&rack_sysctl_ctx,
1072 SYSCTL_CHILDREN(rack_sysctl_root),
1073 OID_AUTO, "req_measure_cnt", CTLFLAG_RW,
1074 &rack_req_measurements, 1,
1075 "If doing dynamic pacing, how many measurements must be in before we start pacing?");
1076 /* Hardware pacing */
1077 rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1078 SYSCTL_CHILDREN(rack_sysctl_root),
1081 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1082 "Pacing related Controls");
1083 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1084 SYSCTL_CHILDREN(rack_hw_pacing),
1085 OID_AUTO, "rwnd_factor", CTLFLAG_RW,
1086 &rack_hw_rwnd_factor, 2,
1087 "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?");
1088 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1089 SYSCTL_CHILDREN(rack_hw_pacing),
1090 OID_AUTO, "precheck", CTLFLAG_RW,
1091 &rack_hw_check_queue, 0,
1092 "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?");
1093 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1094 SYSCTL_CHILDREN(rack_hw_pacing),
1095 OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW,
1096 &rack_enobuf_hw_boost_mult, 0,
1097 "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?");
1098 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1099 SYSCTL_CHILDREN(rack_hw_pacing),
1100 OID_AUTO, "pace_enobuf_max", CTLFLAG_RW,
1101 &rack_enobuf_hw_max, 2,
1102 "What is the max boost the pacing time if we see a ENOBUFS?");
1103 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1104 SYSCTL_CHILDREN(rack_hw_pacing),
1105 OID_AUTO, "pace_enobuf_min", CTLFLAG_RW,
1106 &rack_enobuf_hw_min, 2,
1107 "What is the min boost the pacing time if we see a ENOBUFS?");
1108 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1109 SYSCTL_CHILDREN(rack_hw_pacing),
1110 OID_AUTO, "enable", CTLFLAG_RW,
1111 &rack_enable_hw_pacing, 0,
1112 "Should RACK attempt to use hw pacing?");
1113 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1114 SYSCTL_CHILDREN(rack_hw_pacing),
1115 OID_AUTO, "rate_cap", CTLFLAG_RW,
1116 &rack_hw_rate_caps, 0,
1117 "Does the highest hardware pacing rate cap the rate we will send at??");
1118 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1119 SYSCTL_CHILDREN(rack_hw_pacing),
1120 OID_AUTO, "uncap_per", CTLFLAG_RW,
1121 &rack_hw_rate_cap_per, 0,
1122 "If you go over b/w by this amount you will be uncapped (0 = never)");
1123 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1124 SYSCTL_CHILDREN(rack_hw_pacing),
1125 OID_AUTO, "rate_min", CTLFLAG_RW,
1126 &rack_hw_rate_min, 0,
1127 "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?");
1128 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1129 SYSCTL_CHILDREN(rack_hw_pacing),
1130 OID_AUTO, "rate_to_low", CTLFLAG_RW,
1131 &rack_hw_rate_to_low, 0,
1132 "If we fall below this rate, dis-engage hw pacing?");
1133 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1134 SYSCTL_CHILDREN(rack_hw_pacing),
1135 OID_AUTO, "up_only", CTLFLAG_RW,
1136 &rack_hw_up_only, 0,
1137 "Do we allow hw pacing to lower the rate selected?");
1138 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1139 SYSCTL_CHILDREN(rack_hw_pacing),
1140 OID_AUTO, "extra_mss_precise", CTLFLAG_RW,
1141 &rack_hw_pace_extra_slots, 0,
1142 "If the rates between software and hardware match precisely how many extra time_betweens do we get?");
1143 rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1144 SYSCTL_CHILDREN(rack_sysctl_root),
1147 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1148 "Rack Timely RTT Controls");
1149 /* Timely based GP dynmics */
1150 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1151 SYSCTL_CHILDREN(rack_timely),
1152 OID_AUTO, "upper", CTLFLAG_RW,
1153 &rack_gp_per_bw_mul_up, 2,
1154 "Rack timely upper range for equal b/w (in percentage)");
1155 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1156 SYSCTL_CHILDREN(rack_timely),
1157 OID_AUTO, "lower", CTLFLAG_RW,
1158 &rack_gp_per_bw_mul_down, 4,
1159 "Rack timely lower range for equal b/w (in percentage)");
1160 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1161 SYSCTL_CHILDREN(rack_timely),
1162 OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
1163 &rack_gp_rtt_maxmul, 3,
1164 "Rack timely multiplier of lowest rtt for rtt_max");
1165 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1166 SYSCTL_CHILDREN(rack_timely),
1167 OID_AUTO, "rtt_min_div", CTLFLAG_RW,
1168 &rack_gp_rtt_mindiv, 4,
1169 "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
1170 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1171 SYSCTL_CHILDREN(rack_timely),
1172 OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
1173 &rack_gp_rtt_minmul, 1,
1174 "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
1175 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1176 SYSCTL_CHILDREN(rack_timely),
1177 OID_AUTO, "decrease", CTLFLAG_RW,
1178 &rack_gp_decrease_per, 20,
1179 "Rack timely decrease percentage of our GP multiplication factor");
1180 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1181 SYSCTL_CHILDREN(rack_timely),
1182 OID_AUTO, "increase", CTLFLAG_RW,
1183 &rack_gp_increase_per, 2,
1184 "Rack timely increase perentage of our GP multiplication factor");
1185 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1186 SYSCTL_CHILDREN(rack_timely),
1187 OID_AUTO, "lowerbound", CTLFLAG_RW,
1188 &rack_per_lower_bound, 50,
1189 "Rack timely lowest percentage we allow GP multiplier to fall to");
1190 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1191 SYSCTL_CHILDREN(rack_timely),
1192 OID_AUTO, "upperboundss", CTLFLAG_RW,
1193 &rack_per_upper_bound_ss, 0,
1194 "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
1195 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1196 SYSCTL_CHILDREN(rack_timely),
1197 OID_AUTO, "upperboundca", CTLFLAG_RW,
1198 &rack_per_upper_bound_ca, 0,
1199 "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
1200 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1201 SYSCTL_CHILDREN(rack_timely),
1202 OID_AUTO, "dynamicgp", CTLFLAG_RW,
1203 &rack_do_dyn_mul, 0,
1204 "Rack timely do we enable dynmaic timely goodput by default");
1205 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1206 SYSCTL_CHILDREN(rack_timely),
1207 OID_AUTO, "no_rec_red", CTLFLAG_RW,
1208 &rack_gp_no_rec_chg, 1,
1209 "Rack timely do we prohibit the recovery multiplier from being lowered");
1210 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1211 SYSCTL_CHILDREN(rack_timely),
1212 OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
1213 &rack_timely_dec_clear, 6,
1214 "Rack timely what threshold do we count to before another boost during b/w decent");
1215 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1216 SYSCTL_CHILDREN(rack_timely),
1217 OID_AUTO, "max_push_rise", CTLFLAG_RW,
1218 &rack_timely_max_push_rise, 3,
1219 "Rack timely how many times do we push up with b/w increase");
1220 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1221 SYSCTL_CHILDREN(rack_timely),
1222 OID_AUTO, "max_push_drop", CTLFLAG_RW,
1223 &rack_timely_max_push_drop, 3,
1224 "Rack timely how many times do we push back on b/w decent");
1225 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1226 SYSCTL_CHILDREN(rack_timely),
1227 OID_AUTO, "min_segs", CTLFLAG_RW,
1228 &rack_timely_min_segs, 4,
1229 "Rack timely when setting the cwnd what is the min num segments");
1230 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1231 SYSCTL_CHILDREN(rack_timely),
1232 OID_AUTO, "noback_max", CTLFLAG_RW,
1233 &rack_use_max_for_nobackoff, 0,
1234 "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
1235 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1236 SYSCTL_CHILDREN(rack_timely),
1237 OID_AUTO, "interim_timely_only", CTLFLAG_RW,
1238 &rack_timely_int_timely_only, 0,
1239 "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
1240 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1241 SYSCTL_CHILDREN(rack_timely),
1242 OID_AUTO, "nonstop", CTLFLAG_RW,
1243 &rack_timely_no_stopping, 0,
1244 "Rack timely don't stop increase");
1245 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1246 SYSCTL_CHILDREN(rack_timely),
1247 OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
1248 &rack_down_raise_thresh, 100,
1249 "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
1250 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1251 SYSCTL_CHILDREN(rack_timely),
1252 OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
1254 "Bottom dragging if not these many segments outstanding and room");
1256 /* TLP and Rack related parameters */
1257 rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1258 SYSCTL_CHILDREN(rack_sysctl_root),
1261 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1262 "TLP and Rack related Controls");
1263 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1264 SYSCTL_CHILDREN(rack_tlp),
1265 OID_AUTO, "use_rrr", CTLFLAG_RW,
1267 "Do we use Rack Rapid Recovery");
1268 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1269 SYSCTL_CHILDREN(rack_tlp),
1270 OID_AUTO, "post_rec_labc", CTLFLAG_RW,
1271 &rack_max_abc_post_recovery, 2,
1272 "Since we do early recovery, do we override the l_abc to a value, if so what?");
1273 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1274 SYSCTL_CHILDREN(rack_tlp),
1275 OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
1276 &rack_non_rxt_use_cr, 0,
1277 "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
1278 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1279 SYSCTL_CHILDREN(rack_tlp),
1280 OID_AUTO, "tlpmethod", CTLFLAG_RW,
1281 &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
1282 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
1283 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1284 SYSCTL_CHILDREN(rack_tlp),
1285 OID_AUTO, "limit", CTLFLAG_RW,
1287 "How many TLP's can be sent without sending new data");
1288 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1289 SYSCTL_CHILDREN(rack_tlp),
1290 OID_AUTO, "use_greater", CTLFLAG_RW,
1291 &rack_tlp_use_greater, 1,
1292 "Should we use the rack_rtt time if its greater than srtt");
1293 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1294 SYSCTL_CHILDREN(rack_tlp),
1295 OID_AUTO, "tlpminto", CTLFLAG_RW,
1296 &rack_tlp_min, 10000,
1297 "TLP minimum timeout per the specification (in microseconds)");
1298 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1299 SYSCTL_CHILDREN(rack_tlp),
1300 OID_AUTO, "send_oldest", CTLFLAG_RW,
1301 &rack_always_send_oldest, 0,
1302 "Should we always send the oldest TLP and RACK-TLP");
1303 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1304 SYSCTL_CHILDREN(rack_tlp),
1305 OID_AUTO, "rack_tlimit", CTLFLAG_RW,
1306 &rack_limited_retran, 0,
1307 "How many times can a rack timeout drive out sends");
1308 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1309 SYSCTL_CHILDREN(rack_tlp),
1310 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
1311 &rack_lower_cwnd_at_tlp, 0,
1312 "When a TLP completes a retran should we enter recovery");
1313 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1314 SYSCTL_CHILDREN(rack_tlp),
1315 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
1316 &rack_reorder_thresh, 2,
1317 "What factor for rack will be added when seeing reordering (shift right)");
1318 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1319 SYSCTL_CHILDREN(rack_tlp),
1320 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
1321 &rack_tlp_thresh, 1,
1322 "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
1323 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1324 SYSCTL_CHILDREN(rack_tlp),
1325 OID_AUTO, "reorder_fade", CTLFLAG_RW,
1326 &rack_reorder_fade, 60000000,
1327 "Does reorder detection fade, if so how many microseconds (0 means never)");
1328 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1329 SYSCTL_CHILDREN(rack_tlp),
1330 OID_AUTO, "pktdelay", CTLFLAG_RW,
1331 &rack_pkt_delay, 1000,
1332 "Extra RACK time (in microseconds) besides reordering thresh");
1334 /* Timer related controls */
1335 rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1336 SYSCTL_CHILDREN(rack_sysctl_root),
1339 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1340 "Timer related controls");
1341 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1342 SYSCTL_CHILDREN(rack_timers),
1343 OID_AUTO, "persmin", CTLFLAG_RW,
1344 &rack_persist_min, 250000,
1345 "What is the minimum time in microseconds between persists");
1346 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1347 SYSCTL_CHILDREN(rack_timers),
1348 OID_AUTO, "persmax", CTLFLAG_RW,
1349 &rack_persist_max, 2000000,
1350 "What is the largest delay in microseconds between persists");
1351 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1352 SYSCTL_CHILDREN(rack_timers),
1353 OID_AUTO, "delayed_ack", CTLFLAG_RW,
1354 &rack_delayed_ack_time, 40000,
1355 "Delayed ack time (40ms in microseconds)");
1356 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1357 SYSCTL_CHILDREN(rack_timers),
1358 OID_AUTO, "minrto", CTLFLAG_RW,
1359 &rack_rto_min, 30000,
1360 "Minimum RTO in microseconds -- set with caution below 1000 due to TLP");
1361 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1362 SYSCTL_CHILDREN(rack_timers),
1363 OID_AUTO, "maxrto", CTLFLAG_RW,
1364 &rack_rto_max, 4000000,
1365 "Maximum RTO in microseconds -- should be at least as large as min_rto");
1366 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1367 SYSCTL_CHILDREN(rack_timers),
1368 OID_AUTO, "minto", CTLFLAG_RW,
1370 "Minimum rack timeout in microseconds");
1371 /* Measure controls */
1372 rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1373 SYSCTL_CHILDREN(rack_sysctl_root),
1376 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1377 "Measure related controls");
1378 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1379 SYSCTL_CHILDREN(rack_measure),
1380 OID_AUTO, "wma_divisor", CTLFLAG_RW,
1381 &rack_wma_divisor, 8,
1382 "When doing b/w calculation what is the divisor for the WMA");
1383 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1384 SYSCTL_CHILDREN(rack_measure),
1385 OID_AUTO, "end_cwnd", CTLFLAG_RW,
1386 &rack_cwnd_block_ends_measure, 0,
1387 "Does a cwnd just-return end the measurement window (app limited)");
1388 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1389 SYSCTL_CHILDREN(rack_measure),
1390 OID_AUTO, "end_rwnd", CTLFLAG_RW,
1391 &rack_rwnd_block_ends_measure, 0,
1392 "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1393 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1394 SYSCTL_CHILDREN(rack_measure),
1395 OID_AUTO, "min_target", CTLFLAG_RW,
1396 &rack_def_data_window, 20,
1397 "What is the minimum target window (in mss) for a GP measurements");
1398 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1399 SYSCTL_CHILDREN(rack_measure),
1400 OID_AUTO, "goal_bdp", CTLFLAG_RW,
1402 "What is the goal BDP to measure");
1403 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1404 SYSCTL_CHILDREN(rack_measure),
1405 OID_AUTO, "min_srtts", CTLFLAG_RW,
1407 "What is the goal BDP to measure");
1408 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1409 SYSCTL_CHILDREN(rack_measure),
1410 OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1411 &rack_min_measure_usec, 0,
1412 "What is the Minimum time time for a measurement if 0, this is off");
1414 rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1415 SYSCTL_CHILDREN(rack_sysctl_root),
1418 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1419 "Feature controls");
1420 SYSCTL_ADD_U64(&rack_sysctl_ctx,
1421 SYSCTL_CHILDREN(rack_features),
1422 OID_AUTO, "rxt_clamp_thresh", CTLFLAG_RW,
1423 &rack_rxt_clamp_thresh, 0,
1424 "Bit encoded clamping setup bits CCCC CCCCC UUUU UULF PPPP PPPP PPPP PPPP");
1425 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1426 SYSCTL_CHILDREN(rack_features),
1427 OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW,
1428 &rack_hybrid_allow_set_maxseg, 0,
1429 "Should hybrid pacing allow the setmss command");
1430 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1431 SYSCTL_CHILDREN(rack_features),
1432 OID_AUTO, "cmpack", CTLFLAG_RW,
1433 &rack_use_cmp_acks, 1,
1434 "Should RACK have LRO send compressed acks");
1435 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1436 SYSCTL_CHILDREN(rack_features),
1437 OID_AUTO, "fsb", CTLFLAG_RW,
1439 "Should RACK use the fast send block?");
1440 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1441 SYSCTL_CHILDREN(rack_features),
1442 OID_AUTO, "rfo", CTLFLAG_RW,
1444 "Should RACK use rack_fast_output()?");
1445 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1446 SYSCTL_CHILDREN(rack_features),
1447 OID_AUTO, "rsmrfo", CTLFLAG_RW,
1448 &rack_use_rsm_rfo, 1,
1449 "Should RACK use rack_fast_rsm_output()?");
1450 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1451 SYSCTL_CHILDREN(rack_features),
1452 OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1453 &rack_enable_mqueue_for_nonpaced, 0,
1454 "Should RACK use mbuf queuing for non-paced connections");
1455 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1456 SYSCTL_CHILDREN(rack_features),
1457 OID_AUTO, "hystartplusplus", CTLFLAG_RW,
1458 &rack_do_hystart, 0,
1459 "Should RACK enable HyStart++ on connections?");
1460 /* Misc rack controls */
1461 rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1462 SYSCTL_CHILDREN(rack_sysctl_root),
1465 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1466 "Misc related controls");
1467 #ifdef TCP_ACCOUNTING
1468 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1469 SYSCTL_CHILDREN(rack_misc),
1470 OID_AUTO, "tcp_acct", CTLFLAG_RW,
1471 &rack_tcp_accounting, 0,
1472 "Should we turn on TCP accounting for all rack sessions?");
1474 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1475 SYSCTL_CHILDREN(rack_misc),
1476 OID_AUTO, "dnd", CTLFLAG_RW,
1477 &rack_dnd_default, 0,
1478 "Do not disturb default for rack_rrr = 3");
1479 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1480 SYSCTL_CHILDREN(rack_misc),
1481 OID_AUTO, "sad_seg_per", CTLFLAG_RW,
1482 &sad_seg_size_per, 800,
1483 "Percentage of segment size needed in a sack 800 = 80.0?");
1484 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1485 SYSCTL_CHILDREN(rack_misc),
1486 OID_AUTO, "rxt_controls", CTLFLAG_RW,
1487 &rack_rxt_controls, 0,
1488 "Retransmit sending size controls (valid values 0, 1, 2 default=1)?");
1489 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1490 SYSCTL_CHILDREN(rack_misc),
1491 OID_AUTO, "rack_hibeta", CTLFLAG_RW,
1492 &rack_hibeta_setting, 0,
1493 "Do we ue a high beta (80 instead of 50)?");
1494 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1495 SYSCTL_CHILDREN(rack_misc),
1496 OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW,
1497 &rack_apply_rtt_with_reduced_conf, 0,
1498 "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?");
1499 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1500 SYSCTL_CHILDREN(rack_misc),
1501 OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW,
1502 &rack_dsack_std_based, 3,
1503 "How do we process dsack with respect to rack timers, bit field, 3 is standards based?");
1504 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1505 SYSCTL_CHILDREN(rack_misc),
1506 OID_AUTO, "prr_addback_max", CTLFLAG_RW,
1507 &rack_prr_addbackmax, 2,
1508 "What is the maximum number of MSS we allow to be added back if prr can't send all its data?");
1509 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1510 SYSCTL_CHILDREN(rack_misc),
1511 OID_AUTO, "stats_gets_ms", CTLFLAG_RW,
1512 &rack_stats_gets_ms_rtt, 1,
1513 "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?");
1514 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1515 SYSCTL_CHILDREN(rack_misc),
1516 OID_AUTO, "clientlowbuf", CTLFLAG_RW,
1517 &rack_client_low_buf, 0,
1518 "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?");
1519 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1520 SYSCTL_CHILDREN(rack_misc),
1521 OID_AUTO, "defprofile", CTLFLAG_RW,
1522 &rack_def_profile, 0,
1523 "Should RACK use a default profile (0=no, num == profile num)?");
1524 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1525 SYSCTL_CHILDREN(rack_misc),
1526 OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1527 &rack_enable_shared_cwnd, 1,
1528 "Should RACK try to use the shared cwnd on connections where allowed");
1529 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1530 SYSCTL_CHILDREN(rack_misc),
1531 OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1532 &rack_limits_scwnd, 1,
1533 "Should RACK place low end time limits on the shared cwnd feature");
1534 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1535 SYSCTL_CHILDREN(rack_misc),
1536 OID_AUTO, "no_prr", CTLFLAG_RW,
1537 &rack_disable_prr, 0,
1538 "Should RACK not use prr and only pace (must have pacing on)");
1539 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1540 SYSCTL_CHILDREN(rack_misc),
1541 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1542 &rack_verbose_logging, 0,
1543 "Should RACK black box logging be verbose");
1544 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1545 SYSCTL_CHILDREN(rack_misc),
1546 OID_AUTO, "data_after_close", CTLFLAG_RW,
1547 &rack_ignore_data_after_close, 1,
1548 "Do we hold off sending a RST until all pending data is ack'd");
1549 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1550 SYSCTL_CHILDREN(rack_misc),
1551 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1552 &rack_sack_not_required, 1,
1553 "Do we allow rack to run on connections not supporting SACK");
1554 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1555 SYSCTL_CHILDREN(rack_misc),
1556 OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1557 &rack_send_a_lot_in_prr, 1,
1558 "Send a lot in prr");
1559 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1560 SYSCTL_CHILDREN(rack_misc),
1561 OID_AUTO, "autoscale", CTLFLAG_RW,
1562 &rack_autosndbuf_inc, 20,
1563 "What percentage should rack scale up its snd buffer by?");
1564 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1565 SYSCTL_CHILDREN(rack_misc),
1566 OID_AUTO, "rnds_for_rxt_clamp", CTLFLAG_RW,
1567 &rack_rxt_min_rnds, 10,
1568 "Number of rounds needed between RTT clamps due to high loss rates");
1569 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1570 SYSCTL_CHILDREN(rack_misc),
1571 OID_AUTO, "rnds_for_unclamp", CTLFLAG_RW,
1572 &rack_unclamp_round_thresh, 100,
1573 "Number of rounds needed with no loss to unclamp");
1574 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1575 SYSCTL_CHILDREN(rack_misc),
1576 OID_AUTO, "rxt_threshs_for_unclamp", CTLFLAG_RW,
1577 &rack_unclamp_rxt_thresh, 5,
1578 "Percentage of retransmits we need to be under to unclamp (5 = .5 percent)\n");
1579 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1580 SYSCTL_CHILDREN(rack_misc),
1581 OID_AUTO, "clamp_ss_upper", CTLFLAG_RW,
1582 &rack_clamp_ss_upper, 110,
1583 "Clamp percentage ceiling in SS?");
1584 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1585 SYSCTL_CHILDREN(rack_misc),
1586 OID_AUTO, "clamp_ca_upper", CTLFLAG_RW,
1587 &rack_clamp_ca_upper, 110,
1588 "Clamp percentage ceiling in CA?");
1589 /* Sack Attacker detection stuff */
1590 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1591 SYSCTL_CHILDREN(rack_attack),
1592 OID_AUTO, "merge_out", CTLFLAG_RW,
1593 &rack_merge_out_sacks_on_attack, 0,
1594 "Do we merge the sendmap when we decide we are being attacked?");
1596 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1597 SYSCTL_CHILDREN(rack_attack),
1598 OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1599 &rack_highest_sack_thresh_seen, 0,
1600 "Highest sack to ack ratio seen");
1601 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1602 SYSCTL_CHILDREN(rack_attack),
1603 OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1604 &rack_highest_move_thresh_seen, 0,
1605 "Highest move to non-move ratio seen");
1606 rack_ack_total = counter_u64_alloc(M_WAITOK);
1607 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1608 SYSCTL_CHILDREN(rack_attack),
1609 OID_AUTO, "acktotal", CTLFLAG_RD,
1611 "Total number of Ack's");
1612 rack_express_sack = counter_u64_alloc(M_WAITOK);
1613 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1614 SYSCTL_CHILDREN(rack_attack),
1615 OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1617 "Total expresss number of Sack's");
1618 rack_sack_total = counter_u64_alloc(M_WAITOK);
1619 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1620 SYSCTL_CHILDREN(rack_attack),
1621 OID_AUTO, "sacktotal", CTLFLAG_RD,
1623 "Total number of SACKs");
1624 rack_move_none = counter_u64_alloc(M_WAITOK);
1625 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1626 SYSCTL_CHILDREN(rack_attack),
1627 OID_AUTO, "move_none", CTLFLAG_RD,
1629 "Total number of SACK index reuse of positions under threshold");
1630 rack_move_some = counter_u64_alloc(M_WAITOK);
1631 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1632 SYSCTL_CHILDREN(rack_attack),
1633 OID_AUTO, "move_some", CTLFLAG_RD,
1635 "Total number of SACK index reuse of positions over threshold");
1636 rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1637 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1638 SYSCTL_CHILDREN(rack_attack),
1639 OID_AUTO, "attacks", CTLFLAG_RD,
1640 &rack_sack_attacks_detected,
1641 "Total number of SACK attackers that had sack disabled");
1642 rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1643 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1644 SYSCTL_CHILDREN(rack_attack),
1645 OID_AUTO, "reversed", CTLFLAG_RD,
1646 &rack_sack_attacks_reversed,
1647 "Total number of SACK attackers that were later determined false positive");
1648 rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK);
1649 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1650 SYSCTL_CHILDREN(rack_attack),
1651 OID_AUTO, "suspect", CTLFLAG_RD,
1652 &rack_sack_attacks_suspect,
1653 "Total number of SACKs that triggered early detection");
1655 rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1656 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1657 SYSCTL_CHILDREN(rack_attack),
1658 OID_AUTO, "nextmerge", CTLFLAG_RD,
1659 &rack_sack_used_next_merge,
1660 "Total number of times we used the next merge");
1661 rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1662 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1663 SYSCTL_CHILDREN(rack_attack),
1664 OID_AUTO, "prevmerge", CTLFLAG_RD,
1665 &rack_sack_used_prev_merge,
1666 "Total number of times we used the prev merge");
1668 rack_total_bytes = counter_u64_alloc(M_WAITOK);
1669 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1670 SYSCTL_CHILDREN(rack_counters),
1671 OID_AUTO, "totalbytes", CTLFLAG_RD,
1673 "Total number of bytes sent");
1674 rack_fto_send = counter_u64_alloc(M_WAITOK);
1675 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1676 SYSCTL_CHILDREN(rack_counters),
1677 OID_AUTO, "fto_send", CTLFLAG_RD,
1678 &rack_fto_send, "Total number of rack_fast_output sends");
1679 rack_fto_rsm_send = counter_u64_alloc(M_WAITOK);
1680 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1681 SYSCTL_CHILDREN(rack_counters),
1682 OID_AUTO, "fto_rsm_send", CTLFLAG_RD,
1683 &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends");
1684 rack_nfto_resend = counter_u64_alloc(M_WAITOK);
1685 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1686 SYSCTL_CHILDREN(rack_counters),
1687 OID_AUTO, "nfto_resend", CTLFLAG_RD,
1688 &rack_nfto_resend, "Total number of rack_output retransmissions");
1689 rack_non_fto_send = counter_u64_alloc(M_WAITOK);
1690 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1691 SYSCTL_CHILDREN(rack_counters),
1692 OID_AUTO, "nfto_send", CTLFLAG_RD,
1693 &rack_non_fto_send, "Total number of rack_output first sends");
1694 rack_extended_rfo = counter_u64_alloc(M_WAITOK);
1695 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1696 SYSCTL_CHILDREN(rack_counters),
1697 OID_AUTO, "rfo_extended", CTLFLAG_RD,
1698 &rack_extended_rfo, "Total number of times we extended rfo");
1700 rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK);
1701 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1702 SYSCTL_CHILDREN(rack_counters),
1703 OID_AUTO, "hwpace_init_fail", CTLFLAG_RD,
1704 &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing");
1705 rack_hw_pace_lost = counter_u64_alloc(M_WAITOK);
1707 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1708 SYSCTL_CHILDREN(rack_counters),
1709 OID_AUTO, "hwpace_lost", CTLFLAG_RD,
1710 &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing");
1711 rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1712 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1713 SYSCTL_CHILDREN(rack_counters),
1714 OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1716 "Total number of tail loss probe expirations");
1717 rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1718 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1719 SYSCTL_CHILDREN(rack_counters),
1720 OID_AUTO, "tlp_new", CTLFLAG_RD,
1722 "Total number of tail loss probe sending new data");
1723 rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1724 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1725 SYSCTL_CHILDREN(rack_counters),
1726 OID_AUTO, "tlp_retran", CTLFLAG_RD,
1728 "Total number of tail loss probe sending retransmitted data");
1729 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1730 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1731 SYSCTL_CHILDREN(rack_counters),
1732 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1733 &rack_tlp_retran_bytes,
1734 "Total bytes of tail loss probe sending retransmitted data");
1735 rack_to_tot = counter_u64_alloc(M_WAITOK);
1736 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1737 SYSCTL_CHILDREN(rack_counters),
1738 OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1740 "Total number of times the rack to expired");
1741 rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1742 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1743 SYSCTL_CHILDREN(rack_counters),
1744 OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1746 "Total number of times a sends returned enobuf for non-hdwr paced connections");
1747 rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK);
1748 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1749 SYSCTL_CHILDREN(rack_counters),
1750 OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD,
1751 &rack_saw_enobuf_hw,
1752 "Total number of times a send returned enobuf for hdwr paced connections");
1753 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1754 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1755 SYSCTL_CHILDREN(rack_counters),
1756 OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1757 &rack_saw_enetunreach,
1758 "Total number of times a send received a enetunreachable");
1759 rack_hot_alloc = counter_u64_alloc(M_WAITOK);
1760 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1761 SYSCTL_CHILDREN(rack_counters),
1762 OID_AUTO, "alloc_hot", CTLFLAG_RD,
1764 "Total allocations from the top of our list");
1765 rack_to_alloc = counter_u64_alloc(M_WAITOK);
1766 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1767 SYSCTL_CHILDREN(rack_counters),
1768 OID_AUTO, "allocs", CTLFLAG_RD,
1770 "Total allocations of tracking structures");
1771 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1772 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1773 SYSCTL_CHILDREN(rack_counters),
1774 OID_AUTO, "allochard", CTLFLAG_RD,
1775 &rack_to_alloc_hard,
1776 "Total allocations done with sleeping the hard way");
1777 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1778 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1779 SYSCTL_CHILDREN(rack_counters),
1780 OID_AUTO, "allocemerg", CTLFLAG_RD,
1781 &rack_to_alloc_emerg,
1782 "Total allocations done from emergency cache");
1783 rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1784 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1785 SYSCTL_CHILDREN(rack_counters),
1786 OID_AUTO, "alloc_limited", CTLFLAG_RD,
1787 &rack_to_alloc_limited,
1788 "Total allocations dropped due to limit");
1789 rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1790 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1791 SYSCTL_CHILDREN(rack_counters),
1792 OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1793 &rack_alloc_limited_conns,
1794 "Connections with allocations dropped due to limit");
1795 rack_split_limited = counter_u64_alloc(M_WAITOK);
1796 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1797 SYSCTL_CHILDREN(rack_counters),
1798 OID_AUTO, "split_limited", CTLFLAG_RD,
1799 &rack_split_limited,
1800 "Split allocations dropped due to limit");
1801 rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK);
1802 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1803 SYSCTL_CHILDREN(rack_counters),
1804 OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD,
1805 &rack_rxt_clamps_cwnd,
1806 "Number of times that excessive rxt clamped the cwnd down");
1807 rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK);
1808 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1809 SYSCTL_CHILDREN(rack_counters),
1810 OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD,
1811 &rack_rxt_clamps_cwnd_uniq,
1812 "Number of connections that have had excessive rxt clamped the cwnd down");
1813 rack_persists_sends = counter_u64_alloc(M_WAITOK);
1814 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1815 SYSCTL_CHILDREN(rack_counters),
1816 OID_AUTO, "persist_sends", CTLFLAG_RD,
1817 &rack_persists_sends,
1818 "Number of times we sent a persist probe");
1819 rack_persists_acks = counter_u64_alloc(M_WAITOK);
1820 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1821 SYSCTL_CHILDREN(rack_counters),
1822 OID_AUTO, "persist_acks", CTLFLAG_RD,
1823 &rack_persists_acks,
1824 "Number of times a persist probe was acked");
1825 rack_persists_loss = counter_u64_alloc(M_WAITOK);
1826 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1827 SYSCTL_CHILDREN(rack_counters),
1828 OID_AUTO, "persist_loss", CTLFLAG_RD,
1829 &rack_persists_loss,
1830 "Number of times we detected a lost persist probe (no ack)");
1831 rack_persists_lost_ends = counter_u64_alloc(M_WAITOK);
1832 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1833 SYSCTL_CHILDREN(rack_counters),
1834 OID_AUTO, "persist_loss_ends", CTLFLAG_RD,
1835 &rack_persists_lost_ends,
1836 "Number of lost persist probe (no ack) that the run ended with a PERSIST abort");
1838 rack_adjust_map_bw = counter_u64_alloc(M_WAITOK);
1839 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1840 SYSCTL_CHILDREN(rack_counters),
1841 OID_AUTO, "map_adjust_req", CTLFLAG_RD,
1842 &rack_adjust_map_bw,
1843 "Number of times we hit the case where the sb went up and down on a sendmap entry");
1845 rack_multi_single_eq = counter_u64_alloc(M_WAITOK);
1846 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1847 SYSCTL_CHILDREN(rack_counters),
1848 OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD,
1849 &rack_multi_single_eq,
1850 "Number of compressed acks total represented");
1851 rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK);
1852 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1853 SYSCTL_CHILDREN(rack_counters),
1854 OID_AUTO, "cmp_ack_not", CTLFLAG_RD,
1855 &rack_proc_non_comp_ack,
1856 "Number of non compresseds acks that we processed");
1859 rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1860 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1861 SYSCTL_CHILDREN(rack_counters),
1862 OID_AUTO, "sack_long", CTLFLAG_RD,
1863 &rack_sack_proc_all,
1864 "Total times we had to walk whole list for sack processing");
1865 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1866 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1867 SYSCTL_CHILDREN(rack_counters),
1868 OID_AUTO, "sack_restart", CTLFLAG_RD,
1869 &rack_sack_proc_restart,
1870 "Total times we had to walk whole list due to a restart");
1871 rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1872 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1873 SYSCTL_CHILDREN(rack_counters),
1874 OID_AUTO, "sack_short", CTLFLAG_RD,
1875 &rack_sack_proc_short,
1876 "Total times we took shortcut for sack processing");
1877 rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1878 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1879 SYSCTL_CHILDREN(rack_attack),
1880 OID_AUTO, "skipacked", CTLFLAG_RD,
1881 &rack_sack_skipped_acked,
1882 "Total number of times we skipped previously sacked");
1883 rack_sack_splits = counter_u64_alloc(M_WAITOK);
1884 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1885 SYSCTL_CHILDREN(rack_attack),
1886 OID_AUTO, "ofsplit", CTLFLAG_RD,
1888 "Total number of times we did the old fashion tree split");
1889 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1890 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1891 SYSCTL_CHILDREN(rack_counters),
1892 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1893 &rack_input_idle_reduces,
1894 "Total number of idle reductions on input");
1895 rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK);
1896 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1897 SYSCTL_CHILDREN(rack_counters),
1898 OID_AUTO, "collapsed_win_seen", CTLFLAG_RD,
1899 &rack_collapsed_win_seen,
1900 "Total number of collapsed window events seen (where our window shrinks)");
1902 rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1903 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1904 SYSCTL_CHILDREN(rack_counters),
1905 OID_AUTO, "collapsed_win", CTLFLAG_RD,
1906 &rack_collapsed_win,
1907 "Total number of collapsed window events where we mark packets");
1908 rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK);
1909 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1910 SYSCTL_CHILDREN(rack_counters),
1911 OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD,
1912 &rack_collapsed_win_rxt,
1913 "Total number of packets that were retransmitted");
1914 rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK);
1915 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1916 SYSCTL_CHILDREN(rack_counters),
1917 OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD,
1918 &rack_collapsed_win_rxt_bytes,
1919 "Total number of bytes that were retransmitted");
1920 rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1921 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1922 SYSCTL_CHILDREN(rack_counters),
1923 OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1925 "Total number of scwnd attempts");
1926 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1927 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1928 OID_AUTO, "outsize", CTLFLAG_RD,
1929 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1930 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1931 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1932 OID_AUTO, "opts", CTLFLAG_RD,
1933 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1934 SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1935 SYSCTL_CHILDREN(rack_sysctl_root),
1936 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1937 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1941 rc_init_window(struct tcp_rack *rack)
1945 if (rack->rc_init_win == 0) {
1947 * Nothing set by the user, use the system stack
1950 return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1952 win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1957 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1959 if (IN_FASTRECOVERY(rack->rc_tp->t_flags))
1960 return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1961 else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1962 return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1964 return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1968 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim,
1969 uint64_t data, uint8_t mod, uint16_t aux,
1970 struct tcp_sendfile_track *cur)
1972 #ifdef TCP_REQUEST_TRK
1976 * The rate cap one is noisy and only should come out when normal BB logging
1977 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out
1978 * once per chunk and make up the BBpoint that can be turned on by the client.
1980 if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
1982 * The very noisy two need to only come out when
1983 * we have verbose logging on.
1985 if (rack_verbose_logging != 0)
1986 do_log = tcp_bblogging_on(rack->rc_tp);
1989 } else if (mod != HYBRID_LOG_BW_MEASURE) {
1991 * All other less noisy logs here except the measure which
1992 * also needs to come out on the point and the log.
1994 do_log = tcp_bblogging_on(rack->rc_tp);
1996 do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING);
2000 union tcp_log_stackspecific log;
2004 /* Convert our ms to a microsecond */
2005 memset(&log, 0, sizeof(log));
2007 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2008 log.u_bbr.rttProp = tim;
2009 log.u_bbr.bw_inuse = cbw;
2010 log.u_bbr.delRate = rack_get_gp_est(rack);
2011 lt_bw = rack_get_lt_bw(rack);
2012 log.u_bbr.flex1 = seq;
2013 log.u_bbr.pacing_gain = aux;
2014 /* lt_bw = < flex3 | flex2 > */
2015 log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff);
2016 log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff);
2017 /* Record the last obtained us rtt in inflight */
2019 /* Make sure we are looking at the right log if an overide comes in */
2020 cur = rack->r_ctl.rc_last_sft;
2022 if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY)
2023 log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt;
2025 /* Use the last known rtt i.e. the rack-rtt */
2026 log.u_bbr.inflight = rack->rc_rack_rtt;
2031 log.u_bbr.cur_del_rate = cur->deadline;
2032 if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) {
2033 /* start = < lost | pkt_epoch > */
2034 log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2035 log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2036 log.u_bbr.flex6 = cur->start_seq;
2037 log.u_bbr.pkts_out = cur->end_seq;
2039 /* start = < lost | pkt_epoch > */
2040 log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff);
2041 log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff);
2042 /* end = < pkts_out | flex6 > */
2043 log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff);
2044 log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff);
2046 /* first_send = <lt_epoch | epoch> */
2047 log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff);
2048 log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff);
2049 /* localtime = <delivered | applimited>*/
2050 log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff);
2051 log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
2052 off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
2053 log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
2054 log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs);
2055 log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs);
2056 log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags;
2058 log.u_bbr.flex7 = 0xffff;
2059 log.u_bbr.cur_del_rate = 0xffffffffffffffff;
2062 * Compose bbr_state to be a bit wise 0000ADHF
2063 * where A is the always_pace flag
2064 * where D is the dgp_on flag
2065 * where H is the hybrid_mode on flag
2066 * where F is the use_fixed_rate flag.
2068 log.u_bbr.bbr_state = rack->rc_always_pace;
2069 log.u_bbr.bbr_state <<= 1;
2070 log.u_bbr.bbr_state |= rack->dgp_on;
2071 log.u_bbr.bbr_state <<= 1;
2072 log.u_bbr.bbr_state |= rack->rc_hybrid_mode;
2073 log.u_bbr.bbr_state <<= 1;
2074 log.u_bbr.bbr_state |= rack->use_fixed_rate;
2075 log.u_bbr.flex8 = mod;
2076 tcp_log_event(rack->rc_tp, NULL,
2077 &rack->rc_inp->inp_socket->so_rcv,
2078 &rack->rc_inp->inp_socket->so_snd,
2079 TCP_HYBRID_PACING_LOG, 0,
2080 0, &log, false, NULL, __func__, __LINE__, &tv);
2086 static inline uint64_t
2087 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw)
2089 uint64_t ret_bw, ether;
2092 ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr);
2095 ether += sizeof(struct ip6_hdr);
2097 ether += 14; /* eheader size 6+6+2 */
2100 ether += sizeof(struct ip);
2102 ether += 14; /* eheader size 6+6+2 */
2104 u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs);
2112 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped)
2114 #ifdef TCP_REQUEST_TRK
2116 uint64_t timenow, timeleft, lenleft, lengone, calcbw;
2119 if (rack->r_ctl.bw_rate_cap == 0)
2121 #ifdef TCP_REQUEST_TRK
2122 if (rack->rc_catch_up && rack->rc_hybrid_mode &&
2123 (rack->r_ctl.rc_last_sft != NULL)) {
2125 * We have a dynamic cap. The original target
2126 * is in bw_rate_cap, but we need to look at
2127 * how long it is until we hit the deadline.
2129 struct tcp_sendfile_track *ent;
2131 ent = rack->r_ctl.rc_last_sft;
2133 timenow = tcp_tv_to_lusectick(&tv);
2134 if (timenow >= ent->deadline) {
2135 /* No time left we do DGP only */
2136 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2137 0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent);
2138 rack->r_ctl.bw_rate_cap = 0;
2141 /* We have the time */
2142 timeleft = rack->r_ctl.rc_last_sft->deadline - timenow;
2143 if (timeleft < HPTS_MSEC_IN_SEC) {
2144 /* If there is less than a ms left just use DGPs rate */
2145 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2146 0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent);
2147 rack->r_ctl.bw_rate_cap = 0;
2151 * Now lets find the amount of data left to send.
2153 * Now ideally we want to use the end_seq to figure out how much more
2154 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry..
2156 if (ent->flags & TCP_TRK_TRACK_FLG_COMP) {
2157 if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una))
2158 lenleft = ent->end_seq - rack->rc_tp->snd_una;
2160 /* TSNH, we should catch it at the send */
2161 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2162 0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent);
2163 rack->r_ctl.bw_rate_cap = 0;
2168 * The hard way, figure out how much is gone and then
2169 * take that away from the total the client asked for
2170 * (thats off by tls overhead if this is tls).
2172 if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq))
2173 lengone = rack->rc_tp->snd_una - ent->start_seq;
2176 if (lengone < (ent->end - ent->start))
2177 lenleft = (ent->end - ent->start) - lengone;
2179 /* TSNH, we should catch it at the send */
2180 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2181 0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent);
2182 rack->r_ctl.bw_rate_cap = 0;
2187 /* We have it all sent */
2188 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2189 0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent);
2190 if (rack->r_ctl.bw_rate_cap)
2191 goto normal_ratecap;
2195 calcbw = lenleft * HPTS_USEC_IN_SEC;
2197 /* Now we must compensate for IP/TCP overhead */
2198 calcbw = rack_compensate_for_linerate(rack, calcbw);
2199 /* Update the bit rate cap */
2200 rack->r_ctl.bw_rate_cap = calcbw;
2201 if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2202 (rack_hybrid_allow_set_maxseg == 1) &&
2203 ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2204 /* Lets set in a smaller mss possibly here to match our rate-cap */
2207 orig_max = rack->r_ctl.rc_pace_max_segs;
2208 rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2209 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp));
2210 rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2212 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2213 calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent);
2214 if ((calcbw > 0) && (*bw > calcbw)) {
2215 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2216 *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent);
2224 if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) {
2225 #ifdef TCP_REQUEST_TRK
2226 if (rack->rc_hybrid_mode &&
2227 rack->rc_catch_up &&
2228 (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) &&
2229 (rack_hybrid_allow_set_maxseg == 1) &&
2230 ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) {
2231 /* Lets set in a smaller mss possibly here to match our rate-cap */
2234 orig_max = rack->r_ctl.rc_pace_max_segs;
2235 rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS;
2236 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp));
2237 rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5);
2241 *bw = rack->r_ctl.bw_rate_cap;
2242 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
2244 HYBRID_LOG_RATE_CAP, 1, NULL);
2249 rack_get_gp_est(struct tcp_rack *rack)
2251 uint64_t bw, lt_bw, ret_bw;
2253 if (rack->rc_gp_filled == 0) {
2255 * We have yet no b/w measurement,
2256 * if we have a user set initial bw
2257 * return it. If we don't have that and
2258 * we have an srtt, use the tcp IW (10) to
2259 * calculate a fictional b/w over the SRTT
2260 * which is more or less a guess. Note
2261 * we don't use our IW from rack on purpose
2262 * so if we have like IW=30, we are not
2263 * calculating a "huge" b/w.
2267 lt_bw = rack_get_lt_bw(rack);
2270 * No goodput bw but a long-term b/w does exist
2276 if (rack->r_ctl.init_rate)
2277 return (rack->r_ctl.init_rate);
2279 /* Ok lets come up with the IW guess, if we have a srtt */
2280 if (rack->rc_tp->t_srtt == 0) {
2282 * Go with old pacing method
2283 * i.e. burst mitigation only.
2287 /* Ok lets get the initial TCP win (not racks) */
2288 bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
2289 srtt = (uint64_t)rack->rc_tp->t_srtt;
2290 bw *= (uint64_t)USECS_IN_SECOND;
2296 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
2297 /* Averaging is done, we can return the value */
2298 bw = rack->r_ctl.gp_bw;
2300 /* Still doing initial average must calculate */
2301 bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1);
2303 lt_bw = rack_get_lt_bw(rack);
2305 /* If we don't have one then equate it to the gp_bw */
2306 lt_bw = rack->r_ctl.gp_bw;
2308 if ((rack->r_cwnd_was_clamped == 1) && (rack->r_clamped_gets_lower > 0)){
2309 /* if clamped take the lowest */
2315 /* If not set for clamped to get lowest, take the highest */
2322 * Now lets compensate based on the TCP/IP overhead. Our
2323 * Goodput estimate does not include this so we must pace out
2324 * a bit faster since our pacing calculations do. The pacing
2325 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz
2326 * we are using to do this, so we do that here in the opposite
2327 * direction as well. This means that if we are tunneled and the
2328 * segsiz is say 1200 bytes we will get quite a boost, but its
2329 * compensated for in the pacing time the opposite way.
2332 ret_bw = rack_compensate_for_linerate(rack, ret_bw);
2338 rack_get_bw(struct tcp_rack *rack)
2342 if (rack->use_fixed_rate) {
2343 /* Return the fixed pacing rate */
2344 return (rack_get_fixed_pacing_bw(rack));
2346 bw = rack_get_gp_est(rack);
2351 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
2353 if (rack->use_fixed_rate) {
2355 } else if (rack->in_probe_rtt && (rsm == NULL))
2356 return (rack->r_ctl.rack_per_of_gp_probertt);
2357 else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
2358 rack->r_ctl.rack_per_of_gp_rec)) {
2360 /* a retransmission always use the recovery rate */
2361 return (rack->r_ctl.rack_per_of_gp_rec);
2362 } else if (rack->rack_rec_nonrxt_use_cr) {
2363 /* Directed to use the configured rate */
2364 goto configured_rate;
2365 } else if (rack->rack_no_prr &&
2366 (rack->r_ctl.rack_per_of_gp_rec > 100)) {
2367 /* No PRR, lets just use the b/w estimate only */
2371 * Here we may have a non-retransmit but we
2372 * have no overrides, so just use the recovery
2373 * rate (prr is in effect).
2375 return (rack->r_ctl.rack_per_of_gp_rec);
2379 /* For the configured rate we look at our cwnd vs the ssthresh */
2380 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
2381 return (rack->r_ctl.rack_per_of_gp_ss);
2383 return (rack->r_ctl.rack_per_of_gp_ca);
2387 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6)
2390 * Types of logs (mod value)
2391 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit.
2392 * 2 = a dsack round begins, persist is reset to 16.
2393 * 3 = a dsack round ends
2394 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh
2395 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack
2396 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh.
2398 if (tcp_bblogging_on(rack->rc_tp)) {
2399 union tcp_log_stackspecific log;
2402 memset(&log, 0, sizeof(log));
2403 log.u_bbr.flex1 = rack->rc_rack_tmr_std_based;
2404 log.u_bbr.flex1 <<= 1;
2405 log.u_bbr.flex1 |= rack->rc_rack_use_dsack;
2406 log.u_bbr.flex1 <<= 1;
2407 log.u_bbr.flex1 |= rack->rc_dsack_round_seen;
2408 log.u_bbr.flex2 = rack->r_ctl.dsack_round_end;
2409 log.u_bbr.flex3 = rack->r_ctl.num_dsack;
2410 log.u_bbr.flex4 = flex4;
2411 log.u_bbr.flex5 = flex5;
2412 log.u_bbr.flex6 = flex6;
2413 log.u_bbr.flex7 = rack->r_ctl.dsack_persist;
2414 log.u_bbr.flex8 = mod;
2415 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2416 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2417 &rack->rc_inp->inp_socket->so_rcv,
2418 &rack->rc_inp->inp_socket->so_snd,
2419 RACK_DSACK_HANDLING, 0,
2420 0, &log, false, &tv);
2425 rack_log_hdwr_pacing(struct tcp_rack *rack,
2426 uint64_t rate, uint64_t hw_rate, int line,
2427 int error, uint16_t mod)
2429 if (tcp_bblogging_on(rack->rc_tp)) {
2430 union tcp_log_stackspecific log;
2432 const struct ifnet *ifp;
2434 memset(&log, 0, sizeof(log));
2435 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
2436 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
2437 if (rack->r_ctl.crte) {
2438 ifp = rack->r_ctl.crte->ptbl->rs_ifp;
2439 } else if (rack->rc_inp->inp_route.ro_nh &&
2440 rack->rc_inp->inp_route.ro_nh->nh_ifp) {
2441 ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp;
2445 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
2446 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
2448 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2449 log.u_bbr.bw_inuse = rate;
2450 log.u_bbr.flex5 = line;
2451 log.u_bbr.flex6 = error;
2452 log.u_bbr.flex7 = mod;
2453 log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
2454 log.u_bbr.flex8 = rack->use_fixed_rate;
2455 log.u_bbr.flex8 <<= 1;
2456 log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
2457 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
2458 log.u_bbr.delRate = rack->r_ctl.crte_prev_rate;
2459 if (rack->r_ctl.crte)
2460 log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate;
2462 log.u_bbr.cur_del_rate = 0;
2463 log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req;
2464 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2465 &rack->rc_inp->inp_socket->so_rcv,
2466 &rack->rc_inp->inp_socket->so_snd,
2467 BBR_LOG_HDWR_PACE, 0,
2468 0, &log, false, &tv);
2473 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped)
2476 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
2478 uint64_t bw_est, high_rate;
2481 if ((rack->r_pacing_discount == 0) ||
2482 (rack_full_buffer_discount == 0)) {
2484 * No buffer level based discount from client buffer
2485 * level is enabled or the feature is disabled.
2487 gain = (uint64_t)rack_get_output_gain(rack, rsm);
2489 bw_est /= (uint64_t)100;
2492 * We have a discount in place apply it with
2493 * just a 100% gain (we get no boost if the buffer
2498 discount = bw * (uint64_t)(rack_full_buffer_discount * rack->r_ctl.pacing_discount_amm);
2500 /* What %% of the b/w do we discount */
2501 bw_est = bw - discount;
2503 /* Never fall below the minimum (def 64kbps) */
2504 if (bw_est < RACK_MIN_BW)
2505 bw_est = RACK_MIN_BW;
2506 if (rack->r_rack_hw_rate_caps) {
2507 /* Rate caps are in place */
2508 if (rack->r_ctl.crte != NULL) {
2509 /* We have a hdwr rate already */
2510 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
2511 if (bw_est >= high_rate) {
2512 /* We are capping bw at the highest rate table entry */
2513 if (rack_hw_rate_cap_per &&
2514 (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) {
2515 rack->r_rack_hw_rate_caps = 0;
2518 rack_log_hdwr_pacing(rack,
2519 bw_est, high_rate, __LINE__,
2525 } else if ((rack->rack_hdrw_pacing == 0) &&
2526 (rack->rack_hdw_pace_ena) &&
2527 (rack->rack_attempt_hdwr_pace == 0) &&
2528 (rack->rc_inp->inp_route.ro_nh != NULL) &&
2529 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
2531 * Special case, we have not yet attempted hardware
2532 * pacing, and yet we may, when we do, find out if we are
2533 * above the highest rate. We need to know the maxbw for the interface
2534 * in question (if it supports ratelimiting). We get back
2535 * a 0, if the interface is not found in the RL lists.
2537 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
2539 /* Yep, we have a rate is it above this rate? */
2540 if (bw_est > high_rate) {
2553 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
2555 if (tcp_bblogging_on(rack->rc_tp)) {
2556 union tcp_log_stackspecific log;
2559 if (rack->sack_attack_disable > 0)
2561 if ((mod != 1) && (rack_verbose_logging == 0)) {
2563 * We get 3 values currently for mod
2564 * 1 - We are retransmitting and this tells the reason.
2565 * 2 - We are clearing a dup-ack count.
2566 * 3 - We are incrementing a dup-ack count.
2568 * The clear/increment are only logged
2569 * if you have BBverbose on.
2574 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2575 log.u_bbr.flex1 = tsused;
2576 log.u_bbr.flex2 = thresh;
2577 log.u_bbr.flex3 = rsm->r_flags;
2578 log.u_bbr.flex4 = rsm->r_dupack;
2579 log.u_bbr.flex5 = rsm->r_start;
2580 log.u_bbr.flex6 = rsm->r_end;
2581 log.u_bbr.flex8 = mod;
2582 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2583 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2584 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2585 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2586 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2587 log.u_bbr.pacing_gain = rack->r_must_retran;
2588 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2589 &rack->rc_inp->inp_socket->so_rcv,
2590 &rack->rc_inp->inp_socket->so_snd,
2591 BBR_LOG_SETTINGS_CHG, 0,
2592 0, &log, false, &tv);
2597 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
2599 if (tcp_bblogging_on(rack->rc_tp)) {
2600 union tcp_log_stackspecific log;
2603 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2604 log.u_bbr.flex1 = rack->rc_tp->t_srtt;
2605 log.u_bbr.flex2 = to;
2606 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
2607 log.u_bbr.flex4 = slot;
2608 log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot;
2609 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2610 log.u_bbr.flex7 = rack->rc_in_persist;
2611 log.u_bbr.flex8 = which;
2612 if (rack->rack_no_prr)
2613 log.u_bbr.pkts_out = 0;
2615 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
2616 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2617 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2618 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2619 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2620 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2621 log.u_bbr.pacing_gain = rack->r_must_retran;
2622 log.u_bbr.cwnd_gain = rack->rack_deferred_inited;
2623 log.u_bbr.pkt_epoch = rack->rc_has_collapsed;
2624 log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift;
2625 log.u_bbr.lost = rack_rto_min;
2626 log.u_bbr.epoch = rack->r_ctl.roundends;
2627 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2628 &rack->rc_inp->inp_socket->so_rcv,
2629 &rack->rc_inp->inp_socket->so_snd,
2630 BBR_LOG_TIMERSTAR, 0,
2631 0, &log, false, &tv);
2636 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
2638 if (tcp_bblogging_on(rack->rc_tp)) {
2639 union tcp_log_stackspecific log;
2642 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2643 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2644 log.u_bbr.flex8 = to_num;
2645 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
2646 log.u_bbr.flex2 = rack->rc_rack_rtt;
2648 log.u_bbr.flex3 = 0;
2650 log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
2651 if (rack->rack_no_prr)
2652 log.u_bbr.flex5 = 0;
2654 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2655 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2656 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2657 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2658 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2659 log.u_bbr.pacing_gain = rack->r_must_retran;
2660 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2661 &rack->rc_inp->inp_socket->so_rcv,
2662 &rack->rc_inp->inp_socket->so_snd,
2664 0, &log, false, &tv);
2669 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack,
2670 struct rack_sendmap *prev,
2671 struct rack_sendmap *rsm,
2672 struct rack_sendmap *next,
2673 int flag, uint32_t th_ack, int line)
2675 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2676 union tcp_log_stackspecific log;
2679 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2680 log.u_bbr.flex8 = flag;
2681 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2682 log.u_bbr.cur_del_rate = (uint64_t)prev;
2683 log.u_bbr.delRate = (uint64_t)rsm;
2684 log.u_bbr.rttProp = (uint64_t)next;
2685 log.u_bbr.flex7 = 0;
2687 log.u_bbr.flex1 = prev->r_start;
2688 log.u_bbr.flex2 = prev->r_end;
2689 log.u_bbr.flex7 |= 0x4;
2692 log.u_bbr.flex3 = rsm->r_start;
2693 log.u_bbr.flex4 = rsm->r_end;
2694 log.u_bbr.flex7 |= 0x2;
2697 log.u_bbr.flex5 = next->r_start;
2698 log.u_bbr.flex6 = next->r_end;
2699 log.u_bbr.flex7 |= 0x1;
2701 log.u_bbr.applimited = line;
2702 log.u_bbr.pkts_out = th_ack;
2703 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2704 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2705 if (rack->rack_no_prr)
2708 log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt;
2709 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2710 &rack->rc_inp->inp_socket->so_rcv,
2711 &rack->rc_inp->inp_socket->so_snd,
2713 0, &log, false, &tv);
2718 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
2719 struct rack_sendmap *rsm, int conf)
2721 if (tcp_bblogging_on(tp)) {
2722 union tcp_log_stackspecific log;
2724 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2725 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2726 log.u_bbr.flex1 = t;
2727 log.u_bbr.flex2 = len;
2728 log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt;
2729 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
2730 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
2731 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2732 log.u_bbr.flex7 = conf;
2733 log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot;
2734 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
2735 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2736 log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt;
2737 log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
2738 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2740 log.u_bbr.pkt_epoch = rsm->r_start;
2741 log.u_bbr.lost = rsm->r_end;
2742 log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
2743 /* We loose any upper of the 24 bits */
2744 log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags;
2747 log.u_bbr.pkt_epoch = rack->rc_tp->iss;
2749 log.u_bbr.cwnd_gain = 0;
2750 log.u_bbr.pacing_gain = 0;
2752 /* Write out general bits of interest rrs here */
2753 log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
2754 log.u_bbr.use_lt_bw <<= 1;
2755 log.u_bbr.use_lt_bw |= rack->forced_ack;
2756 log.u_bbr.use_lt_bw <<= 1;
2757 log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
2758 log.u_bbr.use_lt_bw <<= 1;
2759 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
2760 log.u_bbr.use_lt_bw <<= 1;
2761 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
2762 log.u_bbr.use_lt_bw <<= 1;
2763 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
2764 log.u_bbr.use_lt_bw <<= 1;
2765 log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
2766 log.u_bbr.use_lt_bw <<= 1;
2767 log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
2768 log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
2769 log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
2770 log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
2771 log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
2772 log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
2773 log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
2774 log.u_bbr.bw_inuse <<= 32;
2776 log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]);
2777 TCP_LOG_EVENTP(tp, NULL,
2778 &rack->rc_inp->inp_socket->so_rcv,
2779 &rack->rc_inp->inp_socket->so_snd,
2781 0, &log, false, &tv);
2788 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
2791 * Log the rtt sample we are
2792 * applying to the srtt algorithm in
2795 if (tcp_bblogging_on(rack->rc_tp)) {
2796 union tcp_log_stackspecific log;
2799 /* Convert our ms to a microsecond */
2800 memset(&log, 0, sizeof(log));
2801 log.u_bbr.flex1 = rtt;
2802 log.u_bbr.flex2 = rack->r_ctl.ack_count;
2803 log.u_bbr.flex3 = rack->r_ctl.sack_count;
2804 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2805 log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
2806 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
2807 log.u_bbr.flex7 = 1;
2808 log.u_bbr.flex8 = rack->sack_attack_disable;
2809 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2810 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2811 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2812 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2813 log.u_bbr.pacing_gain = rack->r_must_retran;
2815 * We capture in delRate the upper 32 bits as
2816 * the confidence level we had declared, and the
2817 * lower 32 bits as the actual RTT using the arrival
2820 log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence;
2821 log.u_bbr.delRate <<= 32;
2822 log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt;
2823 /* Lets capture all the things that make up t_rtxcur */
2824 log.u_bbr.applimited = rack_rto_min;
2825 log.u_bbr.epoch = rack_rto_max;
2826 log.u_bbr.lt_epoch = rack->r_ctl.timer_slop;
2827 log.u_bbr.lost = rack_rto_min;
2828 log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop);
2829 log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp);
2830 log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec;
2831 log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC;
2832 log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec;
2833 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2834 &rack->rc_inp->inp_socket->so_rcv,
2835 &rack->rc_inp->inp_socket->so_snd,
2837 0, &log, false, &tv);
2842 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where)
2844 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2845 union tcp_log_stackspecific log;
2848 /* Convert our ms to a microsecond */
2849 memset(&log, 0, sizeof(log));
2850 log.u_bbr.flex1 = rtt;
2851 log.u_bbr.flex2 = send_time;
2852 log.u_bbr.flex3 = ack_time;
2853 log.u_bbr.flex4 = where;
2854 log.u_bbr.flex7 = 2;
2855 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2856 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2857 &rack->rc_inp->inp_socket->so_rcv,
2858 &rack->rc_inp->inp_socket->so_snd,
2860 0, &log, false, &tv);
2866 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho)
2868 if (tcp_bblogging_on(rack->rc_tp)) {
2869 union tcp_log_stackspecific log;
2872 /* Convert our ms to a microsecond */
2873 memset(&log, 0, sizeof(log));
2874 log.u_bbr.flex1 = idx;
2875 log.u_bbr.flex2 = rack_ts_to_msec(tsv);
2876 log.u_bbr.flex3 = tsecho;
2877 log.u_bbr.flex7 = 3;
2878 log.u_bbr.rttProp = tsv;
2879 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2880 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2881 &rack->rc_inp->inp_socket->so_rcv,
2882 &rack->rc_inp->inp_socket->so_snd,
2884 0, &log, false, &tv);
2890 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line)
2892 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
2893 union tcp_log_stackspecific log;
2896 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2897 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2898 log.u_bbr.flex1 = line;
2899 log.u_bbr.flex2 = tick;
2900 log.u_bbr.flex3 = tp->t_maxunacktime;
2901 log.u_bbr.flex4 = tp->t_acktime;
2902 log.u_bbr.flex8 = event;
2903 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2904 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2905 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2906 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2907 log.u_bbr.pacing_gain = rack->r_must_retran;
2908 TCP_LOG_EVENTP(tp, NULL,
2909 &rack->rc_inp->inp_socket->so_rcv,
2910 &rack->rc_inp->inp_socket->so_snd,
2911 BBR_LOG_PROGRESS, 0,
2912 0, &log, false, &tv);
2917 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line)
2919 if (tcp_bblogging_on(rack->rc_tp)) {
2920 union tcp_log_stackspecific log;
2922 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2923 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2924 log.u_bbr.flex1 = slot;
2925 if (rack->rack_no_prr)
2926 log.u_bbr.flex2 = 0;
2928 log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
2929 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2930 log.u_bbr.flex5 = rack->r_ctl.ack_during_sd;
2931 log.u_bbr.flex6 = line;
2932 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
2933 log.u_bbr.flex8 = rack->rc_in_persist;
2934 log.u_bbr.timeStamp = cts;
2935 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2936 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2937 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2938 log.u_bbr.pacing_gain = rack->r_must_retran;
2939 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2940 &rack->rc_inp->inp_socket->so_rcv,
2941 &rack->rc_inp->inp_socket->so_snd,
2943 0, &log, false, tv);
2948 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs)
2950 if (tcp_bblogging_on(rack->rc_tp)) {
2951 union tcp_log_stackspecific log;
2954 memset(&log, 0, sizeof(log));
2955 log.u_bbr.flex1 = did_out;
2956 log.u_bbr.flex2 = nxt_pkt;
2957 log.u_bbr.flex3 = way_out;
2958 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2959 if (rack->rack_no_prr)
2960 log.u_bbr.flex5 = 0;
2962 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
2963 log.u_bbr.flex6 = nsegs;
2964 log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
2965 log.u_bbr.flex7 = rack->rc_ack_can_sendout_data; /* Do we have ack-can-send set */
2966 log.u_bbr.flex7 <<= 1;
2967 log.u_bbr.flex7 |= rack->r_fast_output; /* is fast output primed */
2968 log.u_bbr.flex7 <<= 1;
2969 log.u_bbr.flex7 |= rack->r_wanted_output; /* Do we want output */
2970 log.u_bbr.flex8 = rack->rc_in_persist;
2971 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
2972 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2973 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2974 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
2975 log.u_bbr.use_lt_bw <<= 1;
2976 log.u_bbr.use_lt_bw |= rack->r_might_revert;
2977 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
2978 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
2979 log.u_bbr.pacing_gain = rack->r_must_retran;
2980 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2981 &rack->rc_inp->inp_socket->so_rcv,
2982 &rack->rc_inp->inp_socket->so_snd,
2983 BBR_LOG_DOSEG_DONE, 0,
2984 0, &log, false, &tv);
2989 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm)
2991 if (tcp_bblogging_on(rack->rc_tp)) {
2992 union tcp_log_stackspecific log;
2995 memset(&log, 0, sizeof(log));
2996 log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
2997 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
2998 log.u_bbr.flex4 = arg1;
2999 log.u_bbr.flex5 = arg2;
3000 log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs;
3001 log.u_bbr.flex6 = arg3;
3002 log.u_bbr.flex8 = frm;
3003 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3004 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3005 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3006 log.u_bbr.applimited = rack->r_ctl.rc_sacked;
3007 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3008 log.u_bbr.pacing_gain = rack->r_must_retran;
3009 TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
3010 &tptosocket(tp)->so_snd,
3011 TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv);
3016 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
3017 uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
3019 if (tcp_bblogging_on(rack->rc_tp)) {
3020 union tcp_log_stackspecific log;
3023 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3024 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3025 log.u_bbr.flex1 = slot;
3026 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
3027 log.u_bbr.flex4 = reason;
3028 if (rack->rack_no_prr)
3029 log.u_bbr.flex5 = 0;
3031 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3032 log.u_bbr.flex7 = hpts_calling;
3033 log.u_bbr.flex8 = rack->rc_in_persist;
3034 log.u_bbr.lt_epoch = cwnd_to_use;
3035 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3036 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3037 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3038 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3039 log.u_bbr.pacing_gain = rack->r_must_retran;
3040 log.u_bbr.cwnd_gain = rack->rc_has_collapsed;
3041 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3042 &rack->rc_inp->inp_socket->so_rcv,
3043 &rack->rc_inp->inp_socket->so_snd,
3045 tlen, &log, false, &tv);
3050 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
3051 struct timeval *tv, uint32_t flags_on_entry)
3053 if (tcp_bblogging_on(rack->rc_tp)) {
3054 union tcp_log_stackspecific log;
3056 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3057 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
3058 log.u_bbr.flex1 = line;
3059 log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
3060 log.u_bbr.flex3 = flags_on_entry;
3061 log.u_bbr.flex4 = us_cts;
3062 if (rack->rack_no_prr)
3063 log.u_bbr.flex5 = 0;
3065 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
3066 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
3067 log.u_bbr.flex7 = hpts_removed;
3068 log.u_bbr.flex8 = 1;
3069 log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
3070 log.u_bbr.timeStamp = us_cts;
3071 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3072 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3073 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3074 log.u_bbr.pacing_gain = rack->r_must_retran;
3075 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3076 &rack->rc_inp->inp_socket->so_rcv,
3077 &rack->rc_inp->inp_socket->so_snd,
3078 BBR_LOG_TIMERCANC, 0,
3079 0, &log, false, tv);
3084 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
3085 uint32_t flex1, uint32_t flex2,
3086 uint32_t flex3, uint32_t flex4,
3087 uint32_t flex5, uint32_t flex6,
3088 uint16_t flex7, uint8_t mod)
3090 if (tcp_bblogging_on(rack->rc_tp)) {
3091 union tcp_log_stackspecific log;
3095 /* No you can't use 1, its for the real to cancel */
3098 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3099 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3100 log.u_bbr.flex1 = flex1;
3101 log.u_bbr.flex2 = flex2;
3102 log.u_bbr.flex3 = flex3;
3103 log.u_bbr.flex4 = flex4;
3104 log.u_bbr.flex5 = flex5;
3105 log.u_bbr.flex6 = flex6;
3106 log.u_bbr.flex7 = flex7;
3107 log.u_bbr.flex8 = mod;
3108 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3109 &rack->rc_inp->inp_socket->so_rcv,
3110 &rack->rc_inp->inp_socket->so_snd,
3111 BBR_LOG_TIMERCANC, 0,
3112 0, &log, false, &tv);
3117 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
3119 if (tcp_bblogging_on(rack->rc_tp)) {
3120 union tcp_log_stackspecific log;
3123 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3124 log.u_bbr.flex1 = timers;
3125 log.u_bbr.flex2 = ret;
3126 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
3127 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
3128 log.u_bbr.flex5 = cts;
3129 if (rack->rack_no_prr)
3130 log.u_bbr.flex6 = 0;
3132 log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
3133 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto;
3134 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto;
3135 log.u_bbr.pacing_gain = rack->r_must_retran;
3136 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3137 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3138 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3139 &rack->rc_inp->inp_socket->so_rcv,
3140 &rack->rc_inp->inp_socket->so_snd,
3141 BBR_LOG_TO_PROCESS, 0,
3142 0, &log, false, &tv);
3147 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line)
3149 if (tcp_bblogging_on(rack->rc_tp)) {
3150 union tcp_log_stackspecific log;
3153 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3154 log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
3155 log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
3156 if (rack->rack_no_prr)
3157 log.u_bbr.flex3 = 0;
3159 log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
3160 log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
3161 log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
3162 log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
3163 log.u_bbr.flex7 = line;
3164 log.u_bbr.flex8 = frm;
3165 log.u_bbr.pkts_out = orig_cwnd;
3166 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3167 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3168 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
3169 log.u_bbr.use_lt_bw <<= 1;
3170 log.u_bbr.use_lt_bw |= rack->r_might_revert;
3171 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3172 &rack->rc_inp->inp_socket->so_rcv,
3173 &rack->rc_inp->inp_socket->so_snd,
3175 0, &log, false, &tv);
3179 #ifdef TCP_SAD_DETECTION
3181 rack_log_sad(struct tcp_rack *rack, int event)
3183 if (tcp_bblogging_on(rack->rc_tp)) {
3184 union tcp_log_stackspecific log;
3187 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3188 log.u_bbr.flex1 = rack->r_ctl.sack_count;
3189 log.u_bbr.flex2 = rack->r_ctl.ack_count;
3190 log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
3191 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
3192 log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
3193 log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
3194 log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
3195 log.u_bbr.lt_epoch = (tcp_force_detection << 8);
3196 log.u_bbr.lt_epoch |= rack->do_detection;
3197 log.u_bbr.applimited = tcp_map_minimum;
3198 log.u_bbr.flex7 = rack->sack_attack_disable;
3199 log.u_bbr.flex8 = event;
3200 log.u_bbr.bbr_state = rack->rc_suspicious;
3201 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3202 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3203 log.u_bbr.delivered = tcp_sad_decay_val;
3204 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3205 &rack->rc_inp->inp_socket->so_rcv,
3206 &rack->rc_inp->inp_socket->so_snd,
3208 0, &log, false, &tv);
3214 rack_counter_destroy(void)
3216 counter_u64_free(rack_total_bytes);
3217 counter_u64_free(rack_fto_send);
3218 counter_u64_free(rack_fto_rsm_send);
3219 counter_u64_free(rack_nfto_resend);
3220 counter_u64_free(rack_hw_pace_init_fail);
3221 counter_u64_free(rack_hw_pace_lost);
3222 counter_u64_free(rack_non_fto_send);
3223 counter_u64_free(rack_extended_rfo);
3224 counter_u64_free(rack_ack_total);
3225 counter_u64_free(rack_express_sack);
3226 counter_u64_free(rack_sack_total);
3227 counter_u64_free(rack_move_none);
3228 counter_u64_free(rack_move_some);
3229 counter_u64_free(rack_sack_attacks_detected);
3230 counter_u64_free(rack_sack_attacks_reversed);
3231 counter_u64_free(rack_sack_attacks_suspect);
3232 counter_u64_free(rack_sack_used_next_merge);
3233 counter_u64_free(rack_sack_used_prev_merge);
3234 counter_u64_free(rack_tlp_tot);
3235 counter_u64_free(rack_tlp_newdata);
3236 counter_u64_free(rack_tlp_retran);
3237 counter_u64_free(rack_tlp_retran_bytes);
3238 counter_u64_free(rack_to_tot);
3239 counter_u64_free(rack_saw_enobuf);
3240 counter_u64_free(rack_saw_enobuf_hw);
3241 counter_u64_free(rack_saw_enetunreach);
3242 counter_u64_free(rack_hot_alloc);
3243 counter_u64_free(rack_to_alloc);
3244 counter_u64_free(rack_to_alloc_hard);
3245 counter_u64_free(rack_to_alloc_emerg);
3246 counter_u64_free(rack_to_alloc_limited);
3247 counter_u64_free(rack_alloc_limited_conns);
3248 counter_u64_free(rack_split_limited);
3249 counter_u64_free(rack_multi_single_eq);
3250 counter_u64_free(rack_rxt_clamps_cwnd);
3251 counter_u64_free(rack_rxt_clamps_cwnd_uniq);
3252 counter_u64_free(rack_proc_non_comp_ack);
3253 counter_u64_free(rack_sack_proc_all);
3254 counter_u64_free(rack_sack_proc_restart);
3255 counter_u64_free(rack_sack_proc_short);
3256 counter_u64_free(rack_sack_skipped_acked);
3257 counter_u64_free(rack_sack_splits);
3258 counter_u64_free(rack_input_idle_reduces);
3259 counter_u64_free(rack_collapsed_win);
3260 counter_u64_free(rack_collapsed_win_rxt);
3261 counter_u64_free(rack_collapsed_win_rxt_bytes);
3262 counter_u64_free(rack_collapsed_win_seen);
3263 counter_u64_free(rack_try_scwnd);
3264 counter_u64_free(rack_persists_sends);
3265 counter_u64_free(rack_persists_acks);
3266 counter_u64_free(rack_persists_loss);
3267 counter_u64_free(rack_persists_lost_ends);
3269 counter_u64_free(rack_adjust_map_bw);
3271 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
3272 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
3275 static struct rack_sendmap *
3276 rack_alloc(struct tcp_rack *rack)
3278 struct rack_sendmap *rsm;
3281 * First get the top of the list it in
3282 * theory is the "hottest" rsm we have,
3283 * possibly just freed by ack processing.
3285 if (rack->rc_free_cnt > rack_free_cache) {
3286 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3287 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3288 counter_u64_add(rack_hot_alloc, 1);
3289 rack->rc_free_cnt--;
3293 * Once we get under our free cache we probably
3294 * no longer have a "hot" one available. Lets
3297 rsm = uma_zalloc(rack_zone, M_NOWAIT);
3299 rack->r_ctl.rc_num_maps_alloced++;
3300 counter_u64_add(rack_to_alloc, 1);
3304 * Dig in to our aux rsm's (the last two) since
3305 * UMA failed to get us one.
3307 if (rack->rc_free_cnt) {
3308 counter_u64_add(rack_to_alloc_emerg, 1);
3309 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
3310 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3311 rack->rc_free_cnt--;
3317 static struct rack_sendmap *
3318 rack_alloc_full_limit(struct tcp_rack *rack)
3320 if ((V_tcp_map_entries_limit > 0) &&
3321 (rack->do_detection == 0) &&
3322 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
3323 counter_u64_add(rack_to_alloc_limited, 1);
3324 if (!rack->alloc_limit_reported) {
3325 rack->alloc_limit_reported = 1;
3326 counter_u64_add(rack_alloc_limited_conns, 1);
3330 return (rack_alloc(rack));
3333 /* wrapper to allocate a sendmap entry, subject to a specific limit */
3334 static struct rack_sendmap *
3335 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
3337 struct rack_sendmap *rsm;
3340 /* currently there is only one limit type */
3341 if (rack->r_ctl.rc_split_limit > 0 &&
3342 (rack->do_detection == 0) &&
3343 rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) {
3344 counter_u64_add(rack_split_limited, 1);
3345 if (!rack->alloc_limit_reported) {
3346 rack->alloc_limit_reported = 1;
3347 counter_u64_add(rack_alloc_limited_conns, 1);
3350 #ifdef TCP_SAD_DETECTION
3351 } else if ((tcp_sad_limit != 0) &&
3352 (rack->do_detection == 1) &&
3353 (rack->r_ctl.rc_num_split_allocs >= tcp_sad_limit)) {
3354 counter_u64_add(rack_split_limited, 1);
3355 if (!rack->alloc_limit_reported) {
3356 rack->alloc_limit_reported = 1;
3357 counter_u64_add(rack_alloc_limited_conns, 1);
3364 /* allocate and mark in the limit type, if set */
3365 rsm = rack_alloc(rack);
3366 if (rsm != NULL && limit_type) {
3367 rsm->r_limit_type = limit_type;
3368 rack->r_ctl.rc_num_split_allocs++;
3374 rack_free_trim(struct tcp_rack *rack)
3376 struct rack_sendmap *rsm;
3379 * Free up all the tail entries until
3380 * we get our list down to the limit.
3382 while (rack->rc_free_cnt > rack_free_cache) {
3383 rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head);
3384 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
3385 rack->rc_free_cnt--;
3386 rack->r_ctl.rc_num_maps_alloced--;
3387 uma_zfree(rack_zone, rsm);
3392 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
3394 if (rsm->r_flags & RACK_APP_LIMITED) {
3395 if (rack->r_ctl.rc_app_limited_cnt > 0) {
3396 rack->r_ctl.rc_app_limited_cnt--;
3399 if (rsm->r_limit_type) {
3400 /* currently there is only one limit type */
3401 rack->r_ctl.rc_num_split_allocs--;
3403 if (rsm == rack->r_ctl.rc_first_appl) {
3404 if (rack->r_ctl.rc_app_limited_cnt == 0)
3405 rack->r_ctl.rc_first_appl = NULL;
3407 rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl);
3409 if (rsm == rack->r_ctl.rc_resend)
3410 rack->r_ctl.rc_resend = NULL;
3411 if (rsm == rack->r_ctl.rc_end_appl)
3412 rack->r_ctl.rc_end_appl = NULL;
3413 if (rack->r_ctl.rc_tlpsend == rsm)
3414 rack->r_ctl.rc_tlpsend = NULL;
3415 if (rack->r_ctl.rc_sacklast == rsm)
3416 rack->r_ctl.rc_sacklast = NULL;
3417 memset(rsm, 0, sizeof(struct rack_sendmap));
3418 /* Make sure we are not going to overrun our count limit of 0xff */
3419 if ((rack->rc_free_cnt + 1) > 0xff) {
3420 rack_free_trim(rack);
3422 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext);
3423 rack->rc_free_cnt++;
3427 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
3429 uint64_t srtt, bw, len, tim;
3430 uint32_t segsiz, def_len, minl;
3432 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3433 def_len = rack_def_data_window * segsiz;
3434 if (rack->rc_gp_filled == 0) {
3436 * We have no measurement (IW is in flight?) so
3437 * we can only guess using our data_window sysctl
3438 * value (usually 20MSS).
3443 * Now we have a number of factors to consider.
3445 * 1) We have a desired BDP which is usually
3447 * 2) We have a minimum number of rtt's usually 1 SRTT
3448 * but we allow it too to be more.
3449 * 3) We want to make sure a measurement last N useconds (if
3450 * we have set rack_min_measure_usec.
3452 * We handle the first concern here by trying to create a data
3453 * window of max(rack_def_data_window, DesiredBDP). The
3454 * second concern we handle in not letting the measurement
3455 * window end normally until at least the required SRTT's
3456 * have gone by which is done further below in
3457 * rack_enough_for_measurement(). Finally the third concern
3458 * we also handle here by calculating how long that time
3459 * would take at the current BW and then return the
3460 * max of our first calculation and that length. Note
3461 * that if rack_min_measure_usec is 0, we don't deal
3462 * with concern 3. Also for both Concern 1 and 3 an
3463 * application limited period could end the measurement
3466 * So lets calculate the BDP with the "known" b/w using
3467 * the SRTT has our rtt and then multiply it by the
3470 bw = rack_get_bw(rack);
3471 srtt = (uint64_t)tp->t_srtt;
3473 len /= (uint64_t)HPTS_USEC_IN_SEC;
3474 len *= max(1, rack_goal_bdp);
3475 /* Now we need to round up to the nearest MSS */
3476 len = roundup(len, segsiz);
3477 if (rack_min_measure_usec) {
3478 /* Now calculate our min length for this b/w */
3479 tim = rack_min_measure_usec;
3480 minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
3483 minl = roundup(minl, segsiz);
3488 * Now if we have a very small window we want
3489 * to attempt to get the window that is
3490 * as small as possible. This happens on
3491 * low b/w connections and we don't want to
3492 * span huge numbers of rtt's between measurements.
3494 * We basically include 2 over our "MIN window" so
3495 * that the measurement can be shortened (possibly) by
3499 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
3501 return (max((uint32_t)len, def_len));
3506 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality)
3508 uint32_t tim, srtts, segsiz;
3511 * Has enough time passed for the GP measurement to be valid?
3513 if (SEQ_LT(th_ack, tp->gput_seq)) {
3514 /* Not enough bytes yet */
3517 if ((tp->snd_max == tp->snd_una) ||
3518 (th_ack == tp->snd_max)){
3520 * All is acked quality of all acked is
3521 * usually low or medium, but we in theory could split
3522 * all acked into two cases, where you got
3523 * a signifigant amount of your window and
3524 * where you did not. For now we leave it
3525 * but it is something to contemplate in the
3526 * future. The danger here is that delayed ack
3527 * is effecting the last byte (which is a 50:50 chance).
3529 *quality = RACK_QUALITY_ALLACKED;
3532 if (SEQ_GEQ(th_ack, tp->gput_ack)) {
3534 * We obtained our entire window of data we wanted
3535 * no matter if we are in recovery or not then
3536 * its ok since expanding the window does not
3537 * make things fuzzy (or at least not as much).
3539 *quality = RACK_QUALITY_HIGH;
3542 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3543 if (SEQ_LT(th_ack, tp->gput_ack) &&
3544 ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3545 /* Not enough bytes yet */
3548 if (rack->r_ctl.rc_first_appl &&
3549 (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) {
3551 * We are up to the app limited send point
3552 * we have to measure irrespective of the time..
3554 *quality = RACK_QUALITY_APPLIMITED;
3557 /* Now what about time? */
3558 srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
3559 tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
3560 if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
3562 * We do not allow a measurement if we are in recovery
3563 * that would shrink the goodput window we wanted.
3564 * This is to prevent cloudyness of when the last send
3565 * was actually made.
3567 *quality = RACK_QUALITY_HIGH;
3570 /* Nope not even a full SRTT has passed */
3575 rack_log_timely(struct tcp_rack *rack,
3576 uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
3577 uint64_t up_bnd, int line, uint8_t method)
3579 if (tcp_bblogging_on(rack->rc_tp)) {
3580 union tcp_log_stackspecific log;
3583 memset(&log, 0, sizeof(log));
3584 log.u_bbr.flex1 = logged;
3585 log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
3586 log.u_bbr.flex2 <<= 4;
3587 log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
3588 log.u_bbr.flex2 <<= 4;
3589 log.u_bbr.flex2 |= rack->rc_gp_incr;
3590 log.u_bbr.flex2 <<= 4;
3591 log.u_bbr.flex2 |= rack->rc_gp_bwred;
3592 log.u_bbr.flex3 = rack->rc_gp_incr;
3593 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
3594 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
3595 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
3596 log.u_bbr.flex7 = rack->rc_gp_bwred;
3597 log.u_bbr.flex8 = method;
3598 log.u_bbr.cur_del_rate = cur_bw;
3599 log.u_bbr.delRate = low_bnd;
3600 log.u_bbr.bw_inuse = up_bnd;
3601 log.u_bbr.rttProp = rack_get_bw(rack);
3602 log.u_bbr.pkt_epoch = line;
3603 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
3604 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
3605 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
3606 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
3607 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
3608 log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
3609 log.u_bbr.cwnd_gain <<= 1;
3610 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
3611 log.u_bbr.cwnd_gain <<= 1;
3612 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
3613 log.u_bbr.cwnd_gain <<= 1;
3614 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
3615 log.u_bbr.lost = rack->r_ctl.rc_loss_count;
3616 TCP_LOG_EVENTP(rack->rc_tp, NULL,
3617 &rack->rc_inp->inp_socket->so_rcv,
3618 &rack->rc_inp->inp_socket->so_snd,
3620 0, &log, false, &tv);
3625 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
3628 * Before we increase we need to know if
3629 * the estimate just made was less than
3630 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
3632 * If we already are pacing at a fast enough
3633 * rate to push us faster there is no sense of
3636 * We first caculate our actual pacing rate (ss or ca multiplier
3637 * times our cur_bw).
3639 * Then we take the last measured rate and multipy by our
3640 * maximum pacing overage to give us a max allowable rate.
3642 * If our act_rate is smaller than our max_allowable rate
3643 * then we should increase. Else we should hold steady.
3646 uint64_t act_rate, max_allow_rate;
3648 if (rack_timely_no_stopping)
3651 if ((cur_bw == 0) || (last_bw_est == 0)) {
3653 * Initial startup case or
3654 * everything is acked case.
3656 rack_log_timely(rack, mult, cur_bw, 0, 0,
3662 * We can always pace at or slightly above our rate.
3664 rack_log_timely(rack, mult, cur_bw, 0, 0,
3668 act_rate = cur_bw * (uint64_t)mult;
3670 max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
3671 max_allow_rate /= 100;
3672 if (act_rate < max_allow_rate) {
3674 * Here the rate we are actually pacing at
3675 * is smaller than 10% above our last measurement.
3676 * This means we are pacing below what we would
3677 * like to try to achieve (plus some wiggle room).
3679 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
3684 * Here we are already pacing at least rack_max_per_above(10%)
3685 * what we are getting back. This indicates most likely
3686 * that we are being limited (cwnd/rwnd/app) and can't
3687 * get any more b/w. There is no sense of trying to
3688 * raise up the pacing rate its not speeding us up
3689 * and we already are pacing faster than we are getting.
3691 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
3698 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
3701 * When we drag bottom, we want to assure
3702 * that no multiplier is below 1.0, if so
3703 * we want to restore it to at least that.
3705 if (rack->r_ctl.rack_per_of_gp_rec < 100) {
3706 /* This is unlikely we usually do not touch recovery */
3707 rack->r_ctl.rack_per_of_gp_rec = 100;
3709 if (rack->r_ctl.rack_per_of_gp_ca < 100) {
3710 rack->r_ctl.rack_per_of_gp_ca = 100;
3712 if (rack->r_ctl.rack_per_of_gp_ss < 100) {
3713 rack->r_ctl.rack_per_of_gp_ss = 100;
3718 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
3720 if (rack->r_ctl.rack_per_of_gp_ca > 100) {
3721 rack->r_ctl.rack_per_of_gp_ca = 100;
3723 if (rack->r_ctl.rack_per_of_gp_ss > 100) {
3724 rack->r_ctl.rack_per_of_gp_ss = 100;
3729 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
3731 int32_t calc, logged, plus;
3737 * override is passed when we are
3738 * loosing b/w and making one last
3739 * gasp at trying to not loose out
3740 * to a new-reno flow.
3744 /* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
3745 if (rack->rc_gp_incr &&
3746 ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
3748 * Reset and get 5 strokes more before the boost. Note
3749 * that the count is 0 based so we have to add one.
3752 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
3753 rack->rc_gp_timely_inc_cnt = 0;
3755 plus = (uint32_t)rack_gp_increase_per;
3756 /* Must be at least 1% increase for true timely increases */
3758 ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
3760 if (rack->rc_gp_saw_rec &&
3761 (rack->rc_gp_no_rec_chg == 0) &&
3762 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3763 rack->r_ctl.rack_per_of_gp_rec)) {
3764 /* We have been in recovery ding it too */
3765 calc = rack->r_ctl.rack_per_of_gp_rec + plus;
3769 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
3770 if (rack->r_ctl.rack_per_upper_bound_ca &&
3771 (rack->rc_dragged_bottom == 0) &&
3772 (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca))
3773 rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca;
3775 if (rack->rc_gp_saw_ca &&
3776 (rack->rc_gp_saw_ss == 0) &&
3777 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3778 rack->r_ctl.rack_per_of_gp_ca)) {
3780 calc = rack->r_ctl.rack_per_of_gp_ca + plus;
3784 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
3785 if (rack->r_ctl.rack_per_upper_bound_ca &&
3786 (rack->rc_dragged_bottom == 0) &&
3787 (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca))
3788 rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca;
3790 if (rack->rc_gp_saw_ss &&
3791 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
3792 rack->r_ctl.rack_per_of_gp_ss)) {
3794 calc = rack->r_ctl.rack_per_of_gp_ss + plus;
3797 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
3798 if (rack->r_ctl.rack_per_upper_bound_ss &&
3799 (rack->rc_dragged_bottom == 0) &&
3800 (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss))
3801 rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss;
3805 (rack->rc_gp_incr == 0)){
3806 /* Go into increment mode */
3807 rack->rc_gp_incr = 1;
3808 rack->rc_gp_timely_inc_cnt = 0;
3810 if (rack->rc_gp_incr &&
3812 (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
3813 rack->rc_gp_timely_inc_cnt++;
3815 rack_log_timely(rack, logged, plus, 0, 0,
3820 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
3823 * norm_grad = rtt_diff / minrtt;
3824 * new_per = curper * (1 - B * norm_grad)
3826 * B = rack_gp_decrease_per (default 10%)
3827 * rtt_dif = input var current rtt-diff
3828 * curper = input var current percentage
3829 * minrtt = from rack filter
3834 perf = (((uint64_t)curper * ((uint64_t)1000000 -
3835 ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
3836 (((uint64_t)rtt_diff * (uint64_t)1000000)/
3837 (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
3838 (uint64_t)1000000)) /
3840 if (perf > curper) {
3844 return ((uint32_t)perf);
3848 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
3852 * result = curper * (1 - (B * ( 1 - ------ ))
3855 * B = rack_gp_decrease_per (default 10%)
3856 * highrttthresh = filter_min * rack_gp_rtt_maxmul
3859 uint32_t highrttthresh;
3861 highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3863 perf = (((uint64_t)curper * ((uint64_t)1000000 -
3864 ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
3865 ((uint64_t)highrttthresh * (uint64_t)1000000) /
3866 (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
3871 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
3873 uint64_t logvar, logvar2, logvar3;
3874 uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
3876 if (rack->rc_gp_incr) {
3877 /* Turn off increment counting */
3878 rack->rc_gp_incr = 0;
3879 rack->rc_gp_timely_inc_cnt = 0;
3881 ss_red = ca_red = rec_red = 0;
3883 /* Calculate the reduction value */
3887 /* Must be at least 1% reduction */
3888 if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
3889 /* We have been in recovery ding it too */
3890 if (timely_says == 2) {
3891 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
3892 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3898 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3899 if (rack->r_ctl.rack_per_of_gp_rec > val) {
3900 rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
3901 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
3903 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3906 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
3907 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
3910 if (rack->rc_gp_saw_ss) {
3912 if (timely_says == 2) {
3913 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
3914 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3920 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
3921 if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
3922 ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
3923 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
3926 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3930 logvar2 = (uint32_t)rtt;
3932 logvar2 |= (uint32_t)rtt_diff;
3933 logvar3 = rack_gp_rtt_maxmul;
3935 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3936 rack_log_timely(rack, timely_says,
3938 logvar, __LINE__, 10);
3940 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
3941 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
3943 } else if (rack->rc_gp_saw_ca) {
3945 if (timely_says == 2) {
3946 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
3947 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
3953 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
3954 if (rack->r_ctl.rack_per_of_gp_ca > val) {
3955 ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
3956 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
3958 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3963 logvar2 = (uint32_t)rtt;
3965 logvar2 |= (uint32_t)rtt_diff;
3966 logvar3 = rack_gp_rtt_maxmul;
3968 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3969 rack_log_timely(rack, timely_says,
3971 logvar, __LINE__, 10);
3973 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
3974 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
3977 if (rack->rc_gp_timely_dec_cnt < 0x7) {
3978 rack->rc_gp_timely_dec_cnt++;
3979 if (rack_timely_dec_clear &&
3980 (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
3981 rack->rc_gp_timely_dec_cnt = 0;
3986 rack_log_timely(rack, logged, rec_red, rack_per_lower_bound, logvar,
3991 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
3992 uint32_t rtt, uint32_t line, uint8_t reas)
3994 if (tcp_bblogging_on(rack->rc_tp)) {
3995 union tcp_log_stackspecific log;
3998 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
3999 log.u_bbr.flex1 = line;
4000 log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
4001 log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
4002 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
4003 log.u_bbr.flex5 = rtt;
4004 log.u_bbr.flex6 = rack->rc_highly_buffered;
4005 log.u_bbr.flex6 <<= 1;
4006 log.u_bbr.flex6 |= rack->forced_ack;
4007 log.u_bbr.flex6 <<= 1;
4008 log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
4009 log.u_bbr.flex6 <<= 1;
4010 log.u_bbr.flex6 |= rack->in_probe_rtt;
4011 log.u_bbr.flex6 <<= 1;
4012 log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
4013 log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
4014 log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
4015 log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
4016 log.u_bbr.flex8 = reas;
4017 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4018 log.u_bbr.delRate = rack_get_bw(rack);
4019 log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
4020 log.u_bbr.cur_del_rate <<= 32;
4021 log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
4022 log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
4023 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
4024 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
4025 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
4026 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
4027 log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
4028 log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
4029 log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4030 log.u_bbr.rttProp = us_cts;
4031 log.u_bbr.rttProp <<= 32;
4032 log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
4033 TCP_LOG_EVENTP(rack->rc_tp, NULL,
4034 &rack->rc_inp->inp_socket->so_rcv,
4035 &rack->rc_inp->inp_socket->so_snd,
4036 BBR_LOG_RTT_SHRINKS, 0,
4037 0, &log, false, &rack->r_ctl.act_rcv_time);
4042 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
4046 bwdp = rack_get_bw(rack);
4047 bwdp *= (uint64_t)rtt;
4048 bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
4049 rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
4050 if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
4052 * A window protocol must be able to have 4 packets
4053 * outstanding as the floor in order to function
4054 * (especially considering delayed ack :D).
4056 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
4061 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
4064 * ProbeRTT is a bit different in rack_pacing than in
4065 * BBR. It is like BBR in that it uses the lowering of
4066 * the RTT as a signal that we saw something new and
4067 * counts from there for how long between. But it is
4068 * different in that its quite simple. It does not
4069 * play with the cwnd and wait until we get down
4070 * to N segments outstanding and hold that for
4071 * 200ms. Instead it just sets the pacing reduction
4072 * rate to a set percentage (70 by default) and hold
4073 * that for a number of recent GP Srtt's.
4077 if (rack->rc_gp_dyn_mul == 0)
4080 if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
4084 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4085 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4087 * Stop the goodput now, the idea here is
4088 * that future measurements with in_probe_rtt
4089 * won't register if they are not greater so
4090 * we want to get what info (if any) is available
4093 rack_do_goodput_measurement(rack->rc_tp, rack,
4094 rack->rc_tp->snd_una, __LINE__,
4095 RACK_QUALITY_PROBERTT);
4097 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4098 rack->r_ctl.rc_time_probertt_entered = us_cts;
4099 segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4100 rack->r_ctl.rc_pace_min_segs);
4101 rack->in_probe_rtt = 1;
4102 rack->measure_saw_probe_rtt = 1;
4103 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4104 rack->r_ctl.rc_time_probertt_starts = 0;
4105 rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
4106 if (rack_probertt_use_min_rtt_entry)
4107 rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4109 rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
4110 rack_log_rtt_shrinks(rack, us_cts, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4111 __LINE__, RACK_RTTS_ENTERPROBE);
4115 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
4117 struct rack_sendmap *rsm;
4120 segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
4121 rack->r_ctl.rc_pace_min_segs);
4122 rack->in_probe_rtt = 0;
4123 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
4124 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
4126 * Stop the goodput now, the idea here is
4127 * that future measurements with in_probe_rtt
4128 * won't register if they are not greater so
4129 * we want to get what info (if any) is available
4132 rack_do_goodput_measurement(rack->rc_tp, rack,
4133 rack->rc_tp->snd_una, __LINE__,
4134 RACK_QUALITY_PROBERTT);
4135 } else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
4137 * We don't have enough data to make a measurement.
4138 * So lets just stop and start here after exiting
4139 * probe-rtt. We probably are not interested in
4140 * the results anyway.
4142 rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
4145 * Measurements through the current snd_max are going
4146 * to be limited by the slower pacing rate.
4148 * We need to mark these as app-limited so we
4149 * don't collapse the b/w.
4151 rsm = tqhash_max(rack->r_ctl.tqh);
4152 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
4153 if (rack->r_ctl.rc_app_limited_cnt == 0)
4154 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
4157 * Go out to the end app limited and mark
4158 * this new one as next and move the end_appl up
4161 if (rack->r_ctl.rc_end_appl)
4162 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
4163 rack->r_ctl.rc_end_appl = rsm;
4165 rsm->r_flags |= RACK_APP_LIMITED;
4166 rack->r_ctl.rc_app_limited_cnt++;
4169 * Now, we need to examine our pacing rate multipliers.
4170 * If its under 100%, we need to kick it back up to
4171 * 100%. We also don't let it be over our "max" above
4172 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
4173 * Note setting clamp_atexit_prtt to 0 has the effect
4174 * of setting CA/SS to 100% always at exit (which is
4175 * the default behavior).
4177 if (rack_probertt_clear_is) {
4178 rack->rc_gp_incr = 0;
4179 rack->rc_gp_bwred = 0;
4180 rack->rc_gp_timely_inc_cnt = 0;
4181 rack->rc_gp_timely_dec_cnt = 0;
4183 /* Do we do any clamping at exit? */
4184 if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
4185 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
4186 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
4188 if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
4189 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
4190 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
4193 * Lets set rtt_diff to 0, so that we will get a "boost"
4196 rack->r_ctl.rc_rtt_diff = 0;
4198 /* Clear all flags so we start fresh */
4199 rack->rc_tp->t_bytes_acked = 0;
4200 rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
4202 * If configured to, set the cwnd and ssthresh to
4205 if (rack_probe_rtt_sets_cwnd) {
4209 /* Set ssthresh so we get into CA once we hit our target */
4210 if (rack_probertt_use_min_rtt_exit == 1) {
4211 /* Set to min rtt */
4212 rack_set_prtt_target(rack, segsiz,
4213 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
4214 } else if (rack_probertt_use_min_rtt_exit == 2) {
4215 /* Set to current gp rtt */
4216 rack_set_prtt_target(rack, segsiz,
4217 rack->r_ctl.rc_gp_srtt);
4218 } else if (rack_probertt_use_min_rtt_exit == 3) {
4219 /* Set to entry gp rtt */
4220 rack_set_prtt_target(rack, segsiz,
4221 rack->r_ctl.rc_entry_gp_rtt);
4226 sum = rack->r_ctl.rc_entry_gp_rtt;
4228 sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
4231 * A highly buffered path needs
4232 * cwnd space for timely to work.
4233 * Lets set things up as if
4234 * we are heading back here again.
4236 setval = rack->r_ctl.rc_entry_gp_rtt;
4237 } else if (sum >= 15) {
4239 * Lets take the smaller of the
4240 * two since we are just somewhat
4243 setval = rack->r_ctl.rc_gp_srtt;
4244 if (setval > rack->r_ctl.rc_entry_gp_rtt)
4245 setval = rack->r_ctl.rc_entry_gp_rtt;
4248 * Here we are not highly buffered
4249 * and should pick the min we can to
4250 * keep from causing loss.
4252 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
4254 rack_set_prtt_target(rack, segsiz,
4257 if (rack_probe_rtt_sets_cwnd > 1) {
4258 /* There is a percentage here to boost */
4259 ebdp = rack->r_ctl.rc_target_probertt_flight;
4260 ebdp *= rack_probe_rtt_sets_cwnd;
4262 setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
4264 setto = rack->r_ctl.rc_target_probertt_flight;
4265 rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
4266 if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
4268 rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
4270 /* If we set in the cwnd also set the ssthresh point so we are in CA */
4271 rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
4273 rack_log_rtt_shrinks(rack, us_cts,
4274 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4275 __LINE__, RACK_RTTS_EXITPROBE);
4276 /* Clear times last so log has all the info */
4277 rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
4278 rack->r_ctl.rc_time_probertt_entered = us_cts;
4279 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4280 rack->r_ctl.rc_time_of_last_probertt = us_cts;
4284 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
4286 /* Check in on probe-rtt */
4287 if (rack->rc_gp_filled == 0) {
4288 /* We do not do p-rtt unless we have gp measurements */
4291 if (rack->in_probe_rtt) {
4292 uint64_t no_overflow;
4293 uint32_t endtime, must_stay;
4295 if (rack->r_ctl.rc_went_idle_time &&
4296 ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
4298 * We went idle during prtt, just exit now.
4300 rack_exit_probertt(rack, us_cts);
4301 } else if (rack_probe_rtt_safety_val &&
4302 TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
4303 ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
4305 * Probe RTT safety value triggered!
4307 rack_log_rtt_shrinks(rack, us_cts,
4308 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4309 __LINE__, RACK_RTTS_SAFETY);
4310 rack_exit_probertt(rack, us_cts);
4312 /* Calculate the max we will wait */
4313 endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
4314 if (rack->rc_highly_buffered)
4315 endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
4316 /* Calculate the min we must wait */
4317 must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
4318 if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
4319 TSTMP_LT(us_cts, endtime)) {
4321 /* Do we lower more? */
4323 if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
4324 calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
4327 calc /= max(rack->r_ctl.rc_gp_srtt, 1);
4330 calc *= rack_per_of_gp_probertt_reduce;
4331 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
4333 if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
4334 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
4336 /* We must reach target or the time set */
4339 if (rack->r_ctl.rc_time_probertt_starts == 0) {
4340 if ((TSTMP_LT(us_cts, must_stay) &&
4341 rack->rc_highly_buffered) ||
4342 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
4343 rack->r_ctl.rc_target_probertt_flight)) {
4344 /* We are not past the must_stay time */
4347 rack_log_rtt_shrinks(rack, us_cts,
4348 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4349 __LINE__, RACK_RTTS_REACHTARGET);
4350 rack->r_ctl.rc_time_probertt_starts = us_cts;
4351 if (rack->r_ctl.rc_time_probertt_starts == 0)
4352 rack->r_ctl.rc_time_probertt_starts = 1;
4353 /* Restore back to our rate we want to pace at in prtt */
4354 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
4357 * Setup our end time, some number of gp_srtts plus 200ms.
4359 no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
4360 (uint64_t)rack_probertt_gpsrtt_cnt_mul);
4361 if (rack_probertt_gpsrtt_cnt_div)
4362 endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
4365 endtime += rack_min_probertt_hold;
4366 endtime += rack->r_ctl.rc_time_probertt_starts;
4367 if (TSTMP_GEQ(us_cts, endtime)) {
4368 /* yes, exit probertt */
4369 rack_exit_probertt(rack, us_cts);
4372 } else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
4373 /* Go into probertt, its been too long since we went lower */
4374 rack_enter_probertt(rack, us_cts);
4379 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
4380 uint32_t rtt, int32_t rtt_diff)
4382 uint64_t cur_bw, up_bnd, low_bnd, subfr;
4385 if ((rack->rc_gp_dyn_mul == 0) ||
4386 (rack->use_fixed_rate) ||
4387 (rack->in_probe_rtt) ||
4388 (rack->rc_always_pace == 0)) {
4389 /* No dynamic GP multiplier in play */
4392 losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
4393 cur_bw = rack_get_bw(rack);
4394 /* Calculate our up and down range */
4395 up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
4397 up_bnd += rack->r_ctl.last_gp_comp_bw;
4399 subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
4401 low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
4402 if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
4404 * This is the case where our RTT is above
4405 * the max target and we have been configured
4406 * to just do timely no bonus up stuff in that case.
4408 * There are two configurations, set to 1, and we
4409 * just do timely if we are over our max. If its
4410 * set above 1 then we slam the multipliers down
4411 * to 100 and then decrement per timely.
4413 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
4415 if (rack->r_ctl.rc_no_push_at_mrtt > 1)
4416 rack_validate_multipliers_at_or_below_100(rack);
4417 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4418 } else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) {
4420 * We are decreasing this is a bit complicated this
4421 * means we are loosing ground. This could be
4422 * because another flow entered and we are competing
4423 * for b/w with it. This will push the RTT up which
4424 * makes timely unusable unless we want to get shoved
4425 * into a corner and just be backed off (the age
4426 * old problem with delay based CC).
4428 * On the other hand if it was a route change we
4429 * would like to stay somewhat contained and not
4430 * blow out the buffers.
4432 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
4434 rack->r_ctl.last_gp_comp_bw = cur_bw;
4435 if (rack->rc_gp_bwred == 0) {
4436 /* Go into reduction counting */
4437 rack->rc_gp_bwred = 1;
4438 rack->rc_gp_timely_dec_cnt = 0;
4440 if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) {
4442 * Push another time with a faster pacing
4443 * to try to gain back (we include override to
4444 * get a full raise factor).
4446 if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
4447 (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
4448 (timely_says == 0) ||
4449 (rack_down_raise_thresh == 0)) {
4451 * Do an override up in b/w if we were
4452 * below the threshold or if the threshold
4453 * is zero we always do the raise.
4455 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
4457 /* Log it stays the same */
4458 rack_log_timely(rack, 0, last_bw_est, low_bnd, 0,
4461 rack->rc_gp_timely_dec_cnt++;
4462 /* We are not incrementing really no-count */
4463 rack->rc_gp_incr = 0;
4464 rack->rc_gp_timely_inc_cnt = 0;
4467 * Lets just use the RTT
4468 * information and give up
4473 } else if ((timely_says != 2) &&
4475 (last_bw_est > up_bnd)) {
4477 * We are increasing b/w lets keep going, updating
4478 * our b/w and ignoring any timely input, unless
4479 * of course we are at our max raise (if there is one).
4482 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
4484 rack->r_ctl.last_gp_comp_bw = cur_bw;
4485 if (rack->rc_gp_saw_ss &&
4486 rack->r_ctl.rack_per_upper_bound_ss &&
4487 (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) {
4489 * In cases where we can't go higher
4490 * we should just use timely.
4494 if (rack->rc_gp_saw_ca &&
4495 rack->r_ctl.rack_per_upper_bound_ca &&
4496 (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) {
4498 * In cases where we can't go higher
4499 * we should just use timely.
4503 rack->rc_gp_bwred = 0;
4504 rack->rc_gp_timely_dec_cnt = 0;
4505 /* You get a set number of pushes if timely is trying to reduce */
4506 if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
4507 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4509 /* Log it stays the same */
4510 rack_log_timely(rack, 0, last_bw_est, up_bnd, 0,
4516 * We are staying between the lower and upper range bounds
4517 * so use timely to decide.
4519 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
4523 rack->rc_gp_incr = 0;
4524 rack->rc_gp_timely_inc_cnt = 0;
4525 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
4527 (last_bw_est < low_bnd)) {
4528 /* We are loosing ground */
4529 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4530 rack->rc_gp_timely_dec_cnt++;
4531 /* We are not incrementing really no-count */
4532 rack->rc_gp_incr = 0;
4533 rack->rc_gp_timely_inc_cnt = 0;
4535 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
4537 rack->rc_gp_bwred = 0;
4538 rack->rc_gp_timely_dec_cnt = 0;
4539 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
4545 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
4547 int32_t timely_says;
4548 uint64_t log_mult, log_rtt_a_diff;
4550 log_rtt_a_diff = rtt;
4551 log_rtt_a_diff <<= 32;
4552 log_rtt_a_diff |= (uint32_t)rtt_diff;
4553 if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
4554 rack_gp_rtt_maxmul)) {
4555 /* Reduce the b/w multiplier */
4557 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
4559 log_mult |= prev_rtt;
4560 rack_log_timely(rack, timely_says, log_mult,
4561 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4562 log_rtt_a_diff, __LINE__, 4);
4563 } else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4564 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4565 max(rack_gp_rtt_mindiv , 1)))) {
4566 /* Increase the b/w multiplier */
4567 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
4568 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
4569 max(rack_gp_rtt_mindiv , 1));
4571 log_mult |= prev_rtt;
4573 rack_log_timely(rack, timely_says, log_mult ,
4574 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
4575 log_rtt_a_diff, __LINE__, 5);
4578 * Use a gradient to find it the timely gradient
4580 * grad = rc_rtt_diff / min_rtt;
4582 * anything below or equal to 0 will be
4583 * a increase indication. Anything above
4584 * zero is a decrease. Note we take care
4585 * of the actual gradient calculation
4586 * in the reduction (its not needed for
4589 log_mult = prev_rtt;
4590 if (rtt_diff <= 0) {
4592 * Rttdiff is less than zero, increase the
4593 * b/w multiplier (its 0 or negative)
4596 rack_log_timely(rack, timely_says, log_mult,
4597 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
4599 /* Reduce the b/w multiplier */
4601 rack_log_timely(rack, timely_says, log_mult,
4602 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
4605 return (timely_says);
4609 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm)
4611 if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4612 SEQ_LEQ(rsm->r_end, tp->gput_ack)) {
4614 * This covers the case that the
4615 * resent is completely inside
4616 * the gp range or up to it.
4617 * |----------------|
4623 } else if (SEQ_LT(rsm->r_start, tp->gput_seq) &&
4624 SEQ_GT(rsm->r_end, tp->gput_seq)){
4626 * This covers the case of
4631 } else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) &&
4632 SEQ_LT(rsm->r_start, tp->gput_ack) &&
4633 SEQ_GEQ(rsm->r_end, tp->gput_ack)) {
4636 * This covers the case of
4645 static __inline void
4646 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm)
4649 if ((tp->t_flags & TF_GPUTINPROG) == 0)
4652 * We have a Goodput measurement in progress. Mark
4653 * the send if its within the window. If its not
4654 * in the window make sure it does not have the mark.
4656 if (rack_in_gp_window(tp, rsm))
4657 rsm->r_flags |= RACK_IN_GP_WIN;
4659 rsm->r_flags &= ~RACK_IN_GP_WIN;
4662 static __inline void
4663 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4665 /* A GP measurement is ending, clear all marks on the send map*/
4666 struct rack_sendmap *rsm = NULL;
4668 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4670 rsm = tqhash_min(rack->r_ctl.tqh);
4673 while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){
4674 rsm->r_flags &= ~RACK_IN_GP_WIN;
4675 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4680 static __inline void
4681 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack)
4683 struct rack_sendmap *rsm = NULL;
4685 if (tp->snd_una == tp->snd_max) {
4686 /* Nothing outstanding yet, nothing to do here */
4689 if (SEQ_GT(tp->gput_seq, tp->snd_una)) {
4691 * We are measuring ahead of some outstanding
4692 * data. We need to walk through up until we get
4693 * to gp_seq marking so that no rsm is set incorrectly
4694 * with RACK_IN_GP_WIN.
4696 rsm = tqhash_min(rack->r_ctl.tqh);
4697 while (rsm != NULL) {
4698 rack_mark_in_gp_win(tp, rsm);
4699 if (SEQ_GEQ(rsm->r_end, tp->gput_seq))
4701 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4706 * Need to find the GP seq, if rsm is
4707 * set we stopped as we hit it.
4709 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
4712 rack_mark_in_gp_win(tp, rsm);
4715 * Now we may need to mark already sent rsm, ahead of
4716 * gput_seq in the window since they may have been sent
4717 * *before* we started our measurment. The rsm, if non-null
4718 * has been marked (note if rsm would have been NULL we would have
4719 * returned in the previous block). So we go to the next, and continue
4720 * until we run out of entries or we exceed the gp_ack value.
4722 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4724 rack_mark_in_gp_win(tp, rsm);
4725 if (SEQ_GT(rsm->r_end, tp->gput_ack))
4727 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
4732 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
4733 tcp_seq th_ack, int line, uint8_t quality)
4735 uint64_t tim, bytes_ps, stim, utim;
4736 uint32_t segsiz, bytes, reqbytes, us_cts;
4737 int32_t gput, new_rtt_diff, timely_says;
4738 uint64_t resid_bw, subpart = 0, addpart = 0, srtt;
4741 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
4742 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4743 if (TSTMP_GEQ(us_cts, tp->gput_ts))
4744 tim = us_cts - tp->gput_ts;
4747 if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts)
4748 stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
4752 * Use the larger of the send time or ack time. This prevents us
4753 * from being influenced by ack artifacts to come up with too
4754 * high of measurement. Note that since we are spanning over many more
4755 * bytes in most of our measurements hopefully that is less likely to
4761 utim = max(stim, 1);
4762 reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
4763 rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL);
4764 if ((tim == 0) && (stim == 0)) {
4766 * Invalid measurement time, maybe
4767 * all on one ack/one send?
4771 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4772 0, 0, 0, 10, __LINE__, NULL, quality);
4773 goto skip_measurement;
4775 if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
4776 /* We never made a us_rtt measurement? */
4779 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4780 0, 0, 0, 10, __LINE__, NULL, quality);
4781 goto skip_measurement;
4784 * Calculate the maximum possible b/w this connection
4785 * could have. We base our calculation on the lowest
4786 * rtt we have seen during the measurement and the
4787 * largest rwnd the client has given us in that time. This
4788 * forms a BDP that is the maximum that we could ever
4789 * get to the client. Anything larger is not valid.
4791 * I originally had code here that rejected measurements
4792 * where the time was less than 1/2 the latest us_rtt.
4793 * But after thinking on that I realized its wrong since
4794 * say you had a 150Mbps or even 1Gbps link, and you
4795 * were a long way away.. example I am in Europe (100ms rtt)
4796 * talking to my 1Gbps link in S.C. Now measuring say 150,000
4797 * bytes my time would be 1.2ms, and yet my rtt would say
4798 * the measurement was invalid the time was < 50ms. The
4799 * same thing is true for 150Mb (8ms of time).
4801 * A better way I realized is to look at what the maximum
4802 * the connection could possibly do. This is gated on
4803 * the lowest RTT we have seen and the highest rwnd.
4804 * We should in theory never exceed that, if we are
4805 * then something on the path is storing up packets
4806 * and then feeding them all at once to our endpoint
4807 * messing up our measurement.
4809 rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
4810 rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
4811 rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
4812 if (SEQ_LT(th_ack, tp->gput_seq)) {
4813 /* No measurement can be made */
4816 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4817 0, 0, 0, 10, __LINE__, NULL, quality);
4818 goto skip_measurement;
4820 bytes = (th_ack - tp->gput_seq);
4821 bytes_ps = (uint64_t)bytes;
4823 * Don't measure a b/w for pacing unless we have gotten at least
4824 * an initial windows worth of data in this measurement interval.
4826 * Small numbers of bytes get badly influenced by delayed ack and
4827 * other artifacts. Note we take the initial window or our
4828 * defined minimum GP (defaulting to 10 which hopefully is the
4831 if (rack->rc_gp_filled == 0) {
4833 * The initial estimate is special. We
4834 * have blasted out an IW worth of packets
4835 * without a real valid ack ts results. We
4836 * then setup the app_limited_needs_set flag,
4837 * this should get the first ack in (probably 2
4838 * MSS worth) to be recorded as the timestamp.
4839 * We thus allow a smaller number of bytes i.e.
4842 reqbytes -= (2 * segsiz);
4843 /* Also lets fill previous for our first measurement to be neutral */
4844 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
4846 if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
4847 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4848 rack->r_ctl.rc_app_limited_cnt,
4849 0, 0, 10, __LINE__, NULL, quality);
4850 goto skip_measurement;
4853 * We now need to calculate the Timely like status so
4854 * we can update (possibly) the b/w multipliers.
4856 new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
4857 if (rack->rc_gp_filled == 0) {
4858 /* No previous reading */
4859 rack->r_ctl.rc_rtt_diff = new_rtt_diff;
4861 if (rack->measure_saw_probe_rtt == 0) {
4863 * We don't want a probertt to be counted
4864 * since it will be negative incorrectly. We
4865 * expect to be reducing the RTT when we
4866 * pace at a slower rate.
4868 rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
4869 rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
4872 timely_says = rack_make_timely_judgement(rack,
4873 rack->r_ctl.rc_gp_srtt,
4874 rack->r_ctl.rc_rtt_diff,
4875 rack->r_ctl.rc_prev_gp_srtt
4877 bytes_ps *= HPTS_USEC_IN_SEC;
4879 if (bytes_ps > rack->r_ctl.last_max_bw) {
4881 * Something is on path playing
4882 * since this b/w is not possible based
4883 * on our BDP (highest rwnd and lowest rtt
4884 * we saw in the measurement window).
4886 * Another option here would be to
4887 * instead skip the measurement.
4889 rack_log_pacing_delay_calc(rack, bytes, reqbytes,
4890 bytes_ps, rack->r_ctl.last_max_bw, 0,
4891 11, __LINE__, NULL, quality);
4892 bytes_ps = rack->r_ctl.last_max_bw;
4894 /* We store gp for b/w in bytes per second */
4895 if (rack->rc_gp_filled == 0) {
4896 /* Initial measurement */
4898 rack->r_ctl.gp_bw = bytes_ps;
4899 rack->rc_gp_filled = 1;
4900 rack->r_ctl.num_measurements = 1;
4901 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
4903 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
4904 rack->r_ctl.rc_app_limited_cnt,
4905 0, 0, 10, __LINE__, NULL, quality);
4907 if (tcp_in_hpts(rack->rc_tp) &&
4908 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
4910 * Ok we can't trust the pacer in this case
4911 * where we transition from un-paced to paced.
4912 * Or for that matter when the burst mitigation
4913 * was making a wild guess and got it wrong.
4914 * Stop the pacer and clear up all the aggregate
4917 tcp_hpts_remove(rack->rc_tp);
4918 rack->r_ctl.rc_hpts_flags = 0;
4919 rack->r_ctl.rc_last_output_to = 0;
4922 } else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) {
4923 /* Still a small number run an average */
4924 rack->r_ctl.gp_bw += bytes_ps;
4925 addpart = rack->r_ctl.num_measurements;
4926 rack->r_ctl.num_measurements++;
4927 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) {
4928 /* We have collected enough to move forward */
4929 rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements;
4931 rack_set_pace_segments(tp, rack, __LINE__, NULL);
4935 * We want to take 1/wma of the goodput and add in to 7/8th
4936 * of the old value weighted by the srtt. So if your measurement
4937 * period is say 2 SRTT's long you would get 1/4 as the
4938 * value, if it was like 1/2 SRTT then you would get 1/16th.
4940 * But we must be careful not to take too much i.e. if the
4941 * srtt is say 20ms and the measurement is taken over
4942 * 400ms our weight would be 400/20 i.e. 20. On the
4943 * other hand if we get a measurement over 1ms with a
4944 * 10ms rtt we only want to take a much smaller portion.
4946 if (rack->r_ctl.num_measurements < 0xff) {
4947 rack->r_ctl.num_measurements++;
4949 srtt = (uint64_t)tp->t_srtt;
4952 * Strange why did t_srtt go back to zero?
4954 if (rack->r_ctl.rc_rack_min_rtt)
4955 srtt = rack->r_ctl.rc_rack_min_rtt;
4957 srtt = HPTS_USEC_IN_MSEC;
4960 * XXXrrs: Note for reviewers, in playing with
4961 * dynamic pacing I discovered this GP calculation
4962 * as done originally leads to some undesired results.
4963 * Basically you can get longer measurements contributing
4964 * too much to the WMA. Thus I changed it if you are doing
4965 * dynamic adjustments to only do the aportioned adjustment
4966 * if we have a very small (time wise) measurement. Longer
4967 * measurements just get there weight (defaulting to 1/8)
4968 * add to the WMA. We may want to think about changing
4969 * this to always do that for both sides i.e. dynamic
4970 * and non-dynamic... but considering lots of folks
4971 * were playing with this I did not want to change the
4972 * calculation per.se. without your thoughts.. Lawerence?
4975 if (rack->rc_gp_dyn_mul == 0) {
4976 subpart = rack->r_ctl.gp_bw * utim;
4977 subpart /= (srtt * 8);
4978 if (subpart < (rack->r_ctl.gp_bw / 2)) {
4980 * The b/w update takes no more
4981 * away then 1/2 our running total
4984 addpart = bytes_ps * utim;
4985 addpart /= (srtt * 8);
4988 * Don't allow a single measurement
4989 * to account for more than 1/2 of the
4990 * WMA. This could happen on a retransmission
4991 * where utim becomes huge compared to
4992 * srtt (multiple retransmissions when using
4993 * the sending rate which factors in all the
4994 * transmissions from the first one).
4996 subpart = rack->r_ctl.gp_bw / 2;
4997 addpart = bytes_ps / 2;
4999 resid_bw = rack->r_ctl.gp_bw - subpart;
5000 rack->r_ctl.gp_bw = resid_bw + addpart;
5003 if ((utim / srtt) <= 1) {
5005 * The b/w update was over a small period
5006 * of time. The idea here is to prevent a small
5007 * measurement time period from counting
5008 * too much. So we scale it based on the
5009 * time so it attributes less than 1/rack_wma_divisor
5010 * of its measurement.
5012 subpart = rack->r_ctl.gp_bw * utim;
5013 subpart /= (srtt * rack_wma_divisor);
5014 addpart = bytes_ps * utim;
5015 addpart /= (srtt * rack_wma_divisor);
5018 * The scaled measurement was long
5019 * enough so lets just add in the
5020 * portion of the measurement i.e. 1/rack_wma_divisor
5022 subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
5023 addpart = bytes_ps / rack_wma_divisor;
5025 if ((rack->measure_saw_probe_rtt == 0) ||
5026 (bytes_ps > rack->r_ctl.gp_bw)) {
5028 * For probe-rtt we only add it in
5029 * if its larger, all others we just
5033 resid_bw = rack->r_ctl.gp_bw - subpart;
5034 rack->r_ctl.gp_bw = resid_bw + addpart;
5037 rack_set_pace_segments(tp, rack, __LINE__, NULL);
5039 if ((rack->gp_ready == 0) &&
5040 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
5041 /* We have enough measurements now */
5043 if ((rack->rc_always_pace && (rack->use_fixed_rate == 0)) ||
5045 rack_set_cc_pacing(rack);
5046 if (rack->defer_options)
5047 rack_apply_deferred_options(rack);
5049 rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim,
5050 rack_get_bw(rack), 22, did_add, NULL, quality);
5051 /* We do not update any multipliers if we are in or have seen a probe-rtt */
5052 if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
5053 rack_update_multiplier(rack, timely_says, bytes_ps,
5054 rack->r_ctl.rc_gp_srtt,
5055 rack->r_ctl.rc_rtt_diff);
5056 rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
5057 rack_get_bw(rack), 3, line, NULL, quality);
5058 rack_log_pacing_delay_calc(rack,
5061 bytes_ps, /* bw_inuse */
5062 rack->r_ctl.gp_bw, /* delRate */
5063 rack_get_lt_bw(rack), /* rttProp */
5065 /* reset the gp srtt and setup the new prev */
5066 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
5067 /* Record the lost count for the next measurement */
5068 rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
5071 * We restart our diffs based on the gpsrtt in the
5072 * measurement window.
5074 rack->rc_gp_rtt_set = 0;
5075 rack->rc_gp_saw_rec = 0;
5076 rack->rc_gp_saw_ca = 0;
5077 rack->rc_gp_saw_ss = 0;
5078 rack->rc_dragged_bottom = 0;
5080 if (quality == RACK_QUALITY_HIGH) {
5082 * Gput in the stats world is in kbps where bytes_ps is
5083 * bytes per second so we do ((x * 8)/ 1000).
5085 gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000);
5087 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
5090 * XXXLAS: This is a temporary hack, and should be
5091 * chained off VOI_TCP_GPUT when stats(9) grows an
5092 * API to deal with chained VOIs.
5094 if (tp->t_stats_gput_prev > 0)
5095 stats_voi_update_abs_s32(tp->t_stats,
5097 ((gput - tp->t_stats_gput_prev) * 100) /
5098 tp->t_stats_gput_prev);
5100 tp->t_stats_gput_prev = gput;
5102 tp->t_flags &= ~TF_GPUTINPROG;
5104 * Now are we app limited now and there is space from where we
5105 * were to where we want to go?
5107 * We don't do the other case i.e. non-applimited here since
5108 * the next send will trigger us picking up the missing data.
5110 if (rack->r_ctl.rc_first_appl &&
5111 TCPS_HAVEESTABLISHED(tp->t_state) &&
5112 rack->r_ctl.rc_app_limited_cnt &&
5113 (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
5114 ((rack->r_ctl.rc_first_appl->r_end - th_ack) >
5115 max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
5117 * Yep there is enough outstanding to make a measurement here.
5119 struct rack_sendmap *rsm;
5121 rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
5122 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
5123 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
5124 rack->app_limited_needs_set = 0;
5125 tp->gput_seq = th_ack;
5126 if (rack->in_probe_rtt)
5127 rack->measure_saw_probe_rtt = 1;
5128 else if ((rack->measure_saw_probe_rtt) &&
5129 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
5130 rack->measure_saw_probe_rtt = 0;
5131 if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) {
5132 /* There is a full window to gain info from */
5133 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
5135 /* We can only measure up to the applimited point */
5136 tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack);
5137 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
5139 * We don't have enough to make a measurement.
5141 tp->t_flags &= ~TF_GPUTINPROG;
5142 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
5143 0, 0, 0, 6, __LINE__, NULL, quality);
5147 if (tp->t_state >= TCPS_FIN_WAIT_1) {
5149 * We will get no more data into the SB
5150 * this means we need to have the data available
5151 * before we start a measurement.
5153 if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) {
5154 /* Nope not enough data. */
5158 tp->t_flags |= TF_GPUTINPROG;
5160 * Now we need to find the timestamp of the send at tp->gput_seq
5161 * for the send based measurement.
5163 rack->r_ctl.rc_gp_cumack_ts = 0;
5164 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
5166 /* Ok send-based limit is set */
5167 if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
5169 * Move back to include the earlier part
5170 * so our ack time lines up right (this may
5171 * make an overlapping measurement but thats
5174 tp->gput_seq = rsm->r_start;
5176 if (rsm->r_flags & RACK_ACKED) {
5177 struct rack_sendmap *nrsm;
5179 tp->gput_ts = (uint32_t)rsm->r_ack_arrival;
5180 tp->gput_seq = rsm->r_end;
5181 nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
5185 rack->app_limited_needs_set = 1;
5188 rack->app_limited_needs_set = 1;
5189 /* We always go from the first send */
5190 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
5193 * If we don't find the rsm due to some
5194 * send-limit set the current time, which
5195 * basically disables the send-limit.
5200 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
5202 rack_tend_gp_marks(tp, rack);
5203 rack_log_pacing_delay_calc(rack,
5208 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
5210 __LINE__, rsm, quality);
5211 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
5214 * To make sure proper timestamp merging occurs, we need to clear
5215 * all GP marks if we don't start a measurement.
5217 rack_clear_gp_marks(tp, rack);
5222 * CC wrapper hook functions
5225 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs,
5226 uint16_t type, int32_t recovery)
5228 uint32_t prior_cwnd, acked;
5229 struct tcp_log_buffer *lgb = NULL;
5230 uint8_t labc_to_use, quality;
5232 INP_WLOCK_ASSERT(tptoinpcb(tp));
5233 tp->t_ccv.nsegs = nsegs;
5234 acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una);
5235 if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
5238 max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
5239 if (tp->t_ccv.bytes_this_ack > max) {
5240 tp->t_ccv.bytes_this_ack = max;
5244 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
5245 ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
5247 if ((th_ack == tp->snd_max) && rack->lt_bw_up) {
5248 /* We will ack all, time
5249 * to end any lt_bw_up we
5250 * have running until something
5255 rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq);
5256 rack->r_ctl.lt_seq = tp->snd_max;
5257 (void)tcp_get_usecs(&tv);
5258 rack->r_ctl.lt_bw_time += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark);
5261 quality = RACK_QUALITY_NONE;
5262 if ((tp->t_flags & TF_GPUTINPROG) &&
5263 rack_enough_for_measurement(tp, rack, th_ack, &quality)) {
5264 /* Measure the Goodput */
5265 rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality);
5267 /* Which way our we limited, if not cwnd limited no advance in CA */
5268 if (tp->snd_cwnd <= tp->snd_wnd)
5269 tp->t_ccv.flags |= CCF_CWND_LIMITED;
5271 tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
5272 if (tp->snd_cwnd > tp->snd_ssthresh) {
5273 tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack,
5274 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp));
5275 /* For the setting of a window past use the actual scwnd we are using */
5276 if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
5277 tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
5278 tp->t_ccv.flags |= CCF_ABC_SENTAWND;
5281 tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
5282 tp->t_bytes_acked = 0;
5284 prior_cwnd = tp->snd_cwnd;
5285 if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec ||
5286 (rack_client_low_buf && rack->client_bufferlvl &&
5287 (rack->client_bufferlvl < rack_client_low_buf)))
5288 labc_to_use = rack->rc_labc;
5290 labc_to_use = rack_max_abc_post_recovery;
5291 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5292 union tcp_log_stackspecific log;
5295 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5296 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5297 log.u_bbr.flex1 = th_ack;
5298 log.u_bbr.flex2 = tp->t_ccv.flags;
5299 log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5300 log.u_bbr.flex4 = tp->t_ccv.nsegs;
5301 log.u_bbr.flex5 = labc_to_use;
5302 log.u_bbr.flex6 = prior_cwnd;
5303 log.u_bbr.flex7 = V_tcp_do_newsack;
5304 log.u_bbr.flex8 = 1;
5305 lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5306 0, &log, false, NULL, __func__, __LINE__,&tv);
5308 if (CC_ALGO(tp)->ack_received != NULL) {
5309 /* XXXLAS: Find a way to live without this */
5310 tp->t_ccv.curack = th_ack;
5311 tp->t_ccv.labc = labc_to_use;
5312 tp->t_ccv.flags |= CCF_USE_LOCAL_ABC;
5313 CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
5316 lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd;
5318 if (rack->r_must_retran) {
5319 if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) {
5321 * We now are beyond the rxt point so lets disable
5324 rack->r_ctl.rc_out_at_rto = 0;
5325 rack->r_must_retran = 0;
5326 } else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) {
5328 * Only decrement the rc_out_at_rto if the cwnd advances
5329 * at least a whole segment. Otherwise next time the peer
5330 * acks, we won't be able to send this generaly happens
5331 * when we are in Congestion Avoidance.
5333 if (acked <= rack->r_ctl.rc_out_at_rto){
5334 rack->r_ctl.rc_out_at_rto -= acked;
5336 rack->r_ctl.rc_out_at_rto = 0;
5341 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
5343 if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
5344 rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
5349 tcp_rack_partialack(struct tcpcb *tp)
5351 struct tcp_rack *rack;
5353 rack = (struct tcp_rack *)tp->t_fb_ptr;
5354 INP_WLOCK_ASSERT(tptoinpcb(tp));
5356 * If we are doing PRR and have enough
5357 * room to send <or> we are pacing and prr
5358 * is disabled we will want to see if we
5359 * can send data (by setting r_wanted_output to
5362 if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
5364 rack->r_wanted_output = 1;
5368 rack_set_most_aggr(struct tcp_rack *rack)
5370 rack->r_fill_less_agg = 0;
5371 /* Once the cwnd as been clamped we don't do fill_cw */
5372 if (rack->r_cwnd_was_clamped == 0)
5373 rack->rc_pace_to_cwnd = 1;
5374 rack->r_pacing_discount = 0;
5378 rack_limit_fillcw(struct tcp_rack *rack)
5380 rack->r_fill_less_agg = 1;
5381 /* Once the cwnd as been clamped we don't do fill_cw */
5382 if (rack->r_cwnd_was_clamped == 0)
5383 rack->rc_pace_to_cwnd = 1;
5384 rack->r_pacing_discount = 0;
5388 rack_disable_fillcw(struct tcp_rack *rack)
5390 rack->r_fill_less_agg = 1;
5391 rack->rc_pace_to_cwnd = 0;
5392 rack->r_pacing_discount = 0;
5396 rack_client_buffer_level_set(struct tcp_rack *rack)
5399 * Only if DGP is on do we do anything that
5400 * changes stack behavior. If DGP is off all
5401 * we will do is issue a BB log (if BB logging is
5404 if (rack->dgp_on == 0) {
5405 rack_log_pacing_delay_calc(rack, 0, rack->client_bufferlvl,
5406 0, 0, 0, 30, __LINE__, NULL, 0);
5409 if (IN_RECOVERY(rack->rc_tp->t_flags) && rack->r_ctl.full_dgp_in_rec) {
5413 * We are in DGP so what setting should we
5414 * apply based on where the client is?
5416 switch(rack->r_ctl.rc_dgp_bl_agg) {
5420 rack_set_most_aggr(rack);
5423 if (rack->client_bufferlvl == 4)
5424 rack_limit_fillcw(rack);
5425 else if (rack->client_bufferlvl == 5)
5426 rack_disable_fillcw(rack);
5428 rack_set_most_aggr(rack);
5431 if (rack->client_bufferlvl == 3)
5432 rack_limit_fillcw(rack);
5433 else if (rack->client_bufferlvl == 4)
5434 rack_disable_fillcw(rack);
5435 else if (rack->client_bufferlvl == 5) {
5436 rack_disable_fillcw(rack);
5437 rack->r_pacing_discount = 1;
5438 rack->r_ctl.pacing_discount_amm = 1;
5440 rack_set_most_aggr(rack);
5443 if (rack->client_bufferlvl == 2)
5444 rack_limit_fillcw(rack);
5445 else if (rack->client_bufferlvl == 3)
5446 rack_disable_fillcw(rack);
5447 else if (rack->client_bufferlvl == 4) {
5448 rack_disable_fillcw(rack);
5449 rack->r_pacing_discount = 1;
5450 rack->r_ctl.pacing_discount_amm = 1;
5451 } else if (rack->client_bufferlvl == 5) {
5452 rack_disable_fillcw(rack);
5453 rack->r_pacing_discount = 1;
5454 rack->r_ctl.pacing_discount_amm = 2;
5456 rack_set_most_aggr(rack);
5459 rack_log_pacing_delay_calc(rack, rack->r_ctl.rc_dgp_bl_agg, rack->client_bufferlvl, 0,
5460 0, 0, 30, __LINE__, NULL, 0);
5464 do_rack_check_for_unclamp(struct tcpcb *tp, struct tcp_rack *rack)
5467 * Can we unclamp. We unclamp if more than
5468 * N rounds have transpired with no loss.
5470 uint64_t snds, rxts, rxt_per;
5473 rnds = rack->r_ctl.current_round - rack->r_ctl.last_rnd_rxt_clamped;
5474 if ((rack_unclamp_round_thresh > 0) &&
5475 (rnds >= rack_unclamp_round_thresh)) {
5476 snds = tp->t_sndbytes - rack->r_ctl.last_sndbytes;
5477 KASSERT ((snds > 0), ("rack:%p tp:%p snds:%ju is 0", rack, tp,
5479 rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_snd_rxt_bytes;
5480 rxt_per = rxts * 1000;
5482 if ((uint32_t)rxt_per <= rack_unclamp_rxt_thresh) {
5484 if (tcp_bblogging_on(rack->rc_tp)) {
5485 union tcp_log_stackspecific log;
5488 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5489 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5490 log.u_bbr.flex3 = rnds;
5491 log.u_bbr.flex4 = rack_unclamp_round_thresh;
5492 log.u_bbr.flex5 = (uint32_t)rxt_per;
5493 log.u_bbr.flex8 = 6;
5494 log.u_bbr.pkt_epoch = rack->r_ctl.rc_pace_max_segs;
5495 log.u_bbr.bbr_state = rack->rc_pace_to_cwnd;
5496 log.u_bbr.delivered = rack->r_ctl.num_of_clamps_applied;
5497 log.u_bbr.applimited = rack->r_ctl.max_clamps;
5498 log.u_bbr.epoch = rack->r_ctl.clamp_options;
5499 log.u_bbr.cur_del_rate = rxts;
5500 log.u_bbr.bw_inuse = rack_get_lt_bw(rack);
5501 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
5502 log.u_bbr.lt_epoch = (uint32_t)((rack->r_ctl.gp_bw >> 32) & 0x00000000ffffffff);
5503 log.u_bbr.pkts_out = (uint32_t)(rack->r_ctl.gp_bw & 0x00000000ffffffff);
5504 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5505 0, &log, false, NULL, NULL, 0, &tv);
5507 rack->r_ctl.num_of_clamps_applied = 0;
5508 rack->r_cwnd_was_clamped = 0;
5509 rack->excess_rxt_on = 1;
5510 if (rack->r_ctl.clamp_options) {
5512 * We only allow fillcw to be toggled
5513 * if you are setting a max seg too.
5515 if (rack->r_ctl.clamp_options & 0x1) {
5516 if ((rack->rc_pace_to_cwnd == 0) && (rack->dgp_on == 0)) {
5517 /* turn on fill cw for non-dgp*/
5518 rack->rc_pace_to_cwnd = 0;
5519 } else if ((rack->dgp_on == 1) && (rack->rc_pace_to_cwnd == 1)) {
5520 /* For DGP we want it off */
5521 rack->rc_pace_to_cwnd = 1;
5526 /* Reset all multipliers to 100.0 so just the measured bw */
5527 /* Crash any per boosts down to 100% */
5528 rack->r_ctl.rack_per_of_gp_rec = 100;
5529 rack->r_ctl.rack_per_of_gp_ss = 100;
5530 rack->r_ctl.rack_per_of_gp_ca = 100;
5531 /* Set in an upper bound for ss/ca % increase */
5532 rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
5533 rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
5540 do_rack_excess_rxt(struct tcpcb *tp, struct tcp_rack *rack)
5543 * Rack excess rxt accounting is turned on. If we
5544 * are above a threshold of rxt's in at least N
5545 * rounds, then back off the cwnd and ssthresh
5546 * to fit into the long-term b/w.
5548 uint64_t snds, rxts, rxt_per, lt_bw, bdp;
5549 uint32_t rnds, new_cwnd, new_ssthresh, rtt, shared_cwnd_was_enabled = 0;
5551 /* Is it shut off by 0 rounds? */
5552 if (rack_rxt_min_rnds == 0)
5554 if ((rack->r_ctl.max_clamps > 0) &&
5555 (rack->r_ctl.num_of_clamps_applied >= rack->r_ctl.max_clamps)) {
5557 * The idea, if max_clamps is set, is that if clamping it
5558 * N times did not work again, then there is no sense
5559 * clamping it again. The link is just a lossy link and
5560 * our clamps are doing no good. Turn it off so we don't come
5563 rack->excess_rxt_on = 0;
5564 rack->r_cwnd_was_clamped = 0;
5565 rack->r_ctl.num_of_clamps_applied = 0;
5568 snds = tp->t_sndbytes - rack->r_ctl.last_sndbytes;
5569 rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_snd_rxt_bytes;
5570 rnds = rack->r_ctl.current_round - rack->r_ctl.last_rnd_rxt_clamped;
5571 /* Has enough rounds progressed for us to re-measure? */
5572 if ((rnds >= rack_rxt_min_rnds) &&
5573 (rack->r_ctl.rxt_threshold > 0)){
5574 rxt_per = rxts * 1000;
5576 if (rxt_per >= rack->r_ctl.rxt_threshold) {
5579 * We are above our excess retransmit level, lets
5580 * cut down the cwnd and ssthresh to match the long-term
5581 * b/w we are getting.
5583 /* First disable scwnd if enabled */
5584 #ifdef NETFLIX_SHARED_CWND
5585 rack->rack_enable_scwnd = 0;
5586 if (rack->r_ctl.rc_scw) {
5589 shared_cwnd_was_enabled = 1;
5590 if (rack->r_limit_scw)
5591 limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
5594 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
5595 rack->r_ctl.rc_scw_index,
5597 rack->r_ctl.rc_scw = NULL;
5601 /* Calculate what the cwnd and ssthresh should be */
5602 tcp_trace_point(rack->rc_tp, TCP_TP_EXCESS_RXT);
5603 lt_bw = rack_get_lt_bw(rack);
5606 * No lt_bw, lets chop things to one MSS
5607 * and the ssthresh to the iwnd.
5610 new_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
5611 new_ssthresh = tcp_compute_initwnd(tcp_maxseg(tp));
5613 rtt = rack->rc_rack_rtt;
5615 /* If we have no rack_rtt drop to the IW situation */
5618 bdp = lt_bw * (uint64_t)rtt;
5619 bdp /= HPTS_USEC_IN_SEC;
5620 new_cwnd = (uint32_t)bdp;
5621 new_ssthresh = new_cwnd - 1;
5622 if (new_cwnd < ctf_fixed_maxseg(tp)) {
5623 /* Rock bottom, goto IW settings */
5627 rack->r_cwnd_was_clamped = 1;
5628 rack->r_ctl.num_of_clamps_applied++;
5629 /* Reset the counter fromn now */
5630 tp->t_bytes_acked = 0;
5632 * Now what about options?
5633 * We look at the bottom 8 bits:
5634 * F = fill cw bit (toggle it if set)
5636 * M = set max segment bit
5640 if (rack->r_ctl.clamp_options) {
5641 if (rack->r_ctl.clamp_options & 0x1) {
5642 if ((rack->rc_pace_to_cwnd == 0) && (rack->dgp_on == 0)) {
5643 /* turn on fill cw for non-dgp*/
5644 rack->rc_pace_to_cwnd = 1;
5645 } else if ((rack->dgp_on == 1) && (rack->rc_pace_to_cwnd == 1)) {
5646 /* For DGP we want it off */
5647 rack->rc_pace_to_cwnd = 0;
5652 /* Reset all multipliers to 100.0 so just the measured bw */
5653 /* Crash any per boosts down to 100% */
5654 rack->r_ctl.rack_per_of_gp_rec = 100;
5655 rack->r_ctl.rack_per_of_gp_ss = 100;
5656 rack->r_ctl.rack_per_of_gp_ca = 100;
5657 /* Set in an upper bound for ss/ca % increase */
5658 rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_clamp_ss_upper;
5659 rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_clamp_ca_upper;
5660 /* Now move to the lt_bw */
5661 rack->r_ctl.gp_bw = lt_bw;
5662 rack->rc_gp_filled = 1;
5663 rack->r_ctl.num_measurements = RACK_REQ_AVG;
5665 if (tcp_bblogging_on(rack->rc_tp)) {
5666 union tcp_log_stackspecific log;
5669 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5670 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5671 log.u_bbr.flex1 = new_cwnd;
5672 log.u_bbr.flex2 = new_ssthresh;
5673 log.u_bbr.flex3 = rnds;
5674 log.u_bbr.flex4 = rack_rxt_min_rnds;
5675 log.u_bbr.flex5 = rtt;
5676 log.u_bbr.flex6 = shared_cwnd_was_enabled;
5677 log.u_bbr.flex8 = 5;
5678 log.u_bbr.pkt_epoch = rack->r_ctl.rc_pace_max_segs;
5679 log.u_bbr.bbr_state = rack->rc_pace_to_cwnd;
5680 log.u_bbr.delivered = rack->r_ctl.num_of_clamps_applied;
5681 log.u_bbr.applimited = rack->r_ctl.max_clamps;
5682 log.u_bbr.epoch = rack->r_ctl.clamp_options;
5683 log.u_bbr.cur_del_rate = rxts;
5684 log.u_bbr.delRate = snds;
5685 log.u_bbr.rttProp = rack->r_ctl.rxt_threshold;
5686 log.u_bbr.bw_inuse = lt_bw;
5687 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
5688 log.u_bbr.lt_epoch = (uint32_t)((rack->r_ctl.gp_bw >> 32) & 0x00000000ffffffff);
5689 log.u_bbr.pkts_out = (uint32_t)(rack->r_ctl.gp_bw & 0x00000000ffffffff);
5690 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5691 0, &log, false, NULL, NULL, 0, &tv);
5693 /* Update our point where we did it */
5694 if (rack->r_ctl.already_had_a_excess == 0) {
5695 rack->r_ctl.already_had_a_excess = 1;
5696 counter_u64_add(rack_rxt_clamps_cwnd_uniq, 1);
5698 counter_u64_add(rack_rxt_clamps_cwnd, 1);
5699 rack->r_ctl.last_sndbytes = tp->t_sndbytes;
5700 rack->r_ctl.last_snd_rxt_bytes = tp->t_snd_rxt_bytes;
5701 rack->r_ctl.last_rnd_rxt_clamped = rack->r_ctl.current_round;
5702 if (new_cwnd < tp->snd_cwnd)
5703 tp->snd_cwnd = new_cwnd;
5704 if (new_ssthresh < tp->snd_ssthresh)
5705 tp->snd_ssthresh = new_ssthresh;
5711 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack)
5713 struct tcp_rack *rack;
5716 orig_cwnd = tp->snd_cwnd;
5717 INP_WLOCK_ASSERT(tptoinpcb(tp));
5718 rack = (struct tcp_rack *)tp->t_fb_ptr;
5719 /* only alert CC if we alerted when we entered */
5720 if (CC_ALGO(tp)->post_recovery != NULL) {
5721 tp->t_ccv.curack = th_ack;
5722 CC_ALGO(tp)->post_recovery(&tp->t_ccv);
5723 if (tp->snd_cwnd < tp->snd_ssthresh) {
5725 * Rack has burst control and pacing
5726 * so lets not set this any lower than
5727 * snd_ssthresh per RFC-6582 (option 2).
5729 tp->snd_cwnd = tp->snd_ssthresh;
5732 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
5733 union tcp_log_stackspecific log;
5736 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
5737 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
5738 log.u_bbr.flex1 = th_ack;
5739 log.u_bbr.flex2 = tp->t_ccv.flags;
5740 log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack;
5741 log.u_bbr.flex4 = tp->t_ccv.nsegs;
5742 log.u_bbr.flex5 = V_tcp_abc_l_var;
5743 log.u_bbr.flex6 = orig_cwnd;
5744 log.u_bbr.flex7 = V_tcp_do_newsack;
5745 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
5746 log.u_bbr.flex8 = 2;
5747 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0,
5748 0, &log, false, NULL, __func__, __LINE__, &tv);
5750 if ((rack->rack_no_prr == 0) &&
5751 (rack->no_prr_addback == 0) &&
5752 (rack->r_ctl.rc_prr_sndcnt > 0)) {
5754 * Suck the next prr cnt back into cwnd, but
5755 * only do that if we are not application limited.
5757 if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) {
5759 * We are allowed to add back to the cwnd the amount we did
5761 * a) no_prr_addback is off.
5762 * b) we are not app limited
5763 * c) we are doing prr
5765 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none).
5767 tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax),
5768 rack->r_ctl.rc_prr_sndcnt);
5770 rack->r_ctl.rc_prr_sndcnt = 0;
5771 rack_log_to_prr(rack, 1, 0, __LINE__);
5773 rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
5774 tp->snd_recover = tp->snd_una;
5775 if (rack->r_ctl.dsack_persist) {
5776 rack->r_ctl.dsack_persist--;
5777 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
5778 rack->r_ctl.num_dsack = 0;
5780 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
5782 EXIT_RECOVERY(tp->t_flags);
5783 if (rack->r_ctl.full_dgp_in_rec)
5784 rack_client_buffer_level_set(rack);
5788 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line)
5790 struct tcp_rack *rack;
5791 uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd;
5793 INP_WLOCK_ASSERT(tptoinpcb(tp));
5795 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
5797 if (IN_RECOVERY(tp->t_flags) == 0) {
5798 in_rec_at_entry = 0;
5799 ssthresh_enter = tp->snd_ssthresh;
5800 cwnd_enter = tp->snd_cwnd;
5802 in_rec_at_entry = 1;
5803 rack = (struct tcp_rack *)tp->t_fb_ptr;
5806 tp->t_flags &= ~TF_WASFRECOVERY;
5807 tp->t_flags &= ~TF_WASCRECOVERY;
5808 if (!IN_FASTRECOVERY(tp->t_flags)) {
5809 if (rack->dgp_on && rack->r_cwnd_was_clamped) {
5810 /* Reset the gains so that on exit we will be softer longer */
5811 rack->r_ctl.rack_per_of_gp_rec = 100;
5812 rack->r_ctl.rack_per_of_gp_ss = 98;
5813 rack->r_ctl.rack_per_of_gp_ca = 98;
5815 rack->r_ctl.rc_prr_delivered = 0;
5816 rack->r_ctl.rc_prr_out = 0;
5817 rack->r_fast_output = 0;
5818 if (rack->rack_no_prr == 0) {
5819 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5820 rack_log_to_prr(rack, 2, in_rec_at_entry, line);
5822 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
5823 tp->snd_recover = tp->snd_max;
5824 if (tp->t_flags2 & TF2_ECN_PERMIT)
5825 tp->t_flags2 |= TF2_ECN_SND_CWR;
5829 if (!IN_CONGRECOVERY(tp->t_flags) ||
5831 * Allow ECN reaction on ACK to CWR, if
5832 * that data segment was also CE marked.
5834 SEQ_GEQ(ack, tp->snd_recover)) {
5835 EXIT_CONGRECOVERY(tp->t_flags);
5836 KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
5837 rack->r_fast_output = 0;
5838 tp->snd_recover = tp->snd_max + 1;
5839 if (tp->t_flags2 & TF2_ECN_PERMIT)
5840 tp->t_flags2 |= TF2_ECN_SND_CWR;
5845 tp->t_bytes_acked = 0;
5846 rack->r_fast_output = 0;
5847 EXIT_RECOVERY(tp->t_flags);
5848 tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
5849 ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
5850 orig_cwnd = tp->snd_cwnd;
5851 tp->snd_cwnd = ctf_fixed_maxseg(tp);
5852 rack_log_to_prr(rack, 16, orig_cwnd, line);
5853 if (tp->t_flags2 & TF2_ECN_PERMIT)
5854 tp->t_flags2 |= TF2_ECN_SND_CWR;
5857 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
5858 /* RTO was unnecessary, so reset everything. */
5859 tp->snd_cwnd = tp->snd_cwnd_prev;
5860 tp->snd_ssthresh = tp->snd_ssthresh_prev;
5861 tp->snd_recover = tp->snd_recover_prev;
5862 if (tp->t_flags & TF_WASFRECOVERY) {
5863 ENTER_FASTRECOVERY(tp->t_flags);
5864 tp->t_flags &= ~TF_WASFRECOVERY;
5866 if (tp->t_flags & TF_WASCRECOVERY) {
5867 ENTER_CONGRECOVERY(tp->t_flags);
5868 tp->t_flags &= ~TF_WASCRECOVERY;
5870 tp->snd_nxt = tp->snd_max;
5871 tp->t_badrxtwin = 0;
5874 if ((CC_ALGO(tp)->cong_signal != NULL) &&
5876 tp->t_ccv.curack = ack;
5877 CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
5879 if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) {
5880 rack_log_to_prr(rack, 15, cwnd_enter, line);
5881 if (rack->r_ctl.full_dgp_in_rec)
5882 rack_client_buffer_level_set(rack);
5883 rack->r_ctl.dsack_byte_cnt = 0;
5884 rack->r_ctl.retran_during_recovery = 0;
5885 rack->r_ctl.rc_cwnd_at_erec = cwnd_enter;
5886 rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter;
5887 rack->r_ent_rec_ns = 1;
5892 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
5896 INP_WLOCK_ASSERT(tptoinpcb(tp));
5898 if (CC_ALGO(tp)->after_idle != NULL)
5899 CC_ALGO(tp)->after_idle(&tp->t_ccv);
5901 if (tp->snd_cwnd == 1)
5902 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */
5904 i_cwnd = rc_init_window(rack);
5907 * Being idle is no different than the initial window. If the cc
5908 * clamps it down below the initial window raise it to the initial
5911 if (tp->snd_cwnd < i_cwnd) {
5912 tp->snd_cwnd = i_cwnd;
5917 * Indicate whether this ack should be delayed. We can delay the ack if
5918 * following conditions are met:
5919 * - There is no delayed ack timer in progress.
5920 * - Our last ack wasn't a 0-sized window. We never want to delay
5921 * the ack that opens up a 0-sized window.
5922 * - LRO wasn't used for this segment. We make sure by checking that the
5923 * segment size is not larger than the MSS.
5924 * - Delayed acks are enabled or this is a half-synchronized T/TCP
5927 #define DELAY_ACK(tp, tlen) \
5928 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
5929 ((tp->t_flags & TF_DELACK) == 0) && \
5930 (tlen <= tp->t_maxseg) && \
5931 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
5933 static struct rack_sendmap *
5934 rack_find_lowest_rsm(struct tcp_rack *rack)
5936 struct rack_sendmap *rsm;
5939 * Walk the time-order transmitted list looking for an rsm that is
5940 * not acked. This will be the one that was sent the longest time
5941 * ago that is still outstanding.
5943 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
5944 if (rsm->r_flags & RACK_ACKED) {
5953 static struct rack_sendmap *
5954 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
5956 struct rack_sendmap *prsm;
5959 * Walk the sequence order list backward until we hit and arrive at
5960 * the highest seq not acked. In theory when this is called it
5961 * should be the last segment (which it was not).
5965 TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) {
5966 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
5975 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
5981 * lro is the flag we use to determine if we have seen reordering.
5982 * If it gets set we have seen reordering. The reorder logic either
5983 * works in one of two ways:
5985 * If reorder-fade is configured, then we track the last time we saw
5986 * re-ordering occur. If we reach the point where enough time as
5987 * passed we no longer consider reordering has occuring.
5989 * Or if reorder-face is 0, then once we see reordering we consider
5990 * the connection to alway be subject to reordering and just set lro
5993 * In the end if lro is non-zero we add the extra time for
5998 if (rack->r_ctl.rc_reorder_ts) {
5999 if (rack->r_ctl.rc_reorder_fade) {
6000 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
6001 lro = cts - rack->r_ctl.rc_reorder_ts;
6004 * No time as passed since the last
6005 * reorder, mark it as reordering.
6010 /* Negative time? */
6013 if (lro > rack->r_ctl.rc_reorder_fade) {
6014 /* Turn off reordering seen too */
6015 rack->r_ctl.rc_reorder_ts = 0;
6019 /* Reodering does not fade */
6025 if (rack->rc_rack_tmr_std_based == 0) {
6026 thresh = srtt + rack->r_ctl.rc_pkt_delay;
6028 /* Standards based pkt-delay is 1/4 srtt */
6029 thresh = srtt + (srtt >> 2);
6031 if (lro && (rack->rc_rack_tmr_std_based == 0)) {
6032 /* It must be set, if not you get 1/4 rtt */
6033 if (rack->r_ctl.rc_reorder_shift)
6034 thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
6036 thresh += (srtt >> 2);
6038 if (rack->rc_rack_use_dsack &&
6040 (rack->r_ctl.num_dsack > 0)) {
6042 * We only increase the reordering window if we
6043 * have seen reordering <and> we have a DSACK count.
6045 thresh += rack->r_ctl.num_dsack * (srtt >> 2);
6046 rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh);
6048 /* SRTT * 2 is the ceiling */
6049 if (thresh > (srtt * 2)) {
6052 /* And we don't want it above the RTO max either */
6053 if (thresh > rack_rto_max) {
6054 thresh = rack_rto_max;
6056 rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh);
6061 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
6062 struct rack_sendmap *rsm, uint32_t srtt)
6064 struct rack_sendmap *prsm;
6065 uint32_t thresh, len;
6070 if (rack->r_ctl.rc_tlp_threshold)
6071 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
6073 thresh = (srtt * 2);
6075 /* Get the previous sent packet, if any */
6076 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
6077 len = rsm->r_end - rsm->r_start;
6078 if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
6079 /* Exactly like the ID */
6080 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
6081 uint32_t alt_thresh;
6083 * Compensate for delayed-ack with the d-ack time.
6085 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6086 if (alt_thresh > thresh)
6087 thresh = alt_thresh;
6089 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
6091 prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
6092 if (prsm && (len <= segsiz)) {
6094 * Two packets outstanding, thresh should be (2*srtt) +
6095 * possible inter-packet delay (if any).
6097 uint32_t inter_gap = 0;
6100 idx = rsm->r_rtr_cnt - 1;
6101 nidx = prsm->r_rtr_cnt - 1;
6102 if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) {
6103 /* Yes it was sent later (or at the same time) */
6104 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
6106 thresh += inter_gap;
6107 } else if (len <= segsiz) {
6109 * Possibly compensate for delayed-ack.
6111 uint32_t alt_thresh;
6113 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6114 if (alt_thresh > thresh)
6115 thresh = alt_thresh;
6117 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
6119 if (len <= segsiz) {
6120 uint32_t alt_thresh;
6122 * Compensate for delayed-ack with the d-ack time.
6124 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
6125 if (alt_thresh > thresh)
6126 thresh = alt_thresh;
6129 /* Not above an RTO */
6130 if (thresh > tp->t_rxtcur) {
6131 thresh = tp->t_rxtcur;
6133 /* Not above a RTO max */
6134 if (thresh > rack_rto_max) {
6135 thresh = rack_rto_max;
6137 /* Apply user supplied min TLP */
6138 if (thresh < rack_tlp_min) {
6139 thresh = rack_tlp_min;
6145 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
6148 * We want the rack_rtt which is the
6149 * last rtt we measured. However if that
6150 * does not exist we fallback to the srtt (which
6151 * we probably will never do) and then as a last
6152 * resort we use RACK_INITIAL_RTO if no srtt is
6155 if (rack->rc_rack_rtt)
6156 return (rack->rc_rack_rtt);
6157 else if (tp->t_srtt == 0)
6158 return (RACK_INITIAL_RTO);
6159 return (tp->t_srtt);
6162 static struct rack_sendmap *
6163 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
6166 * Check to see that we don't need to fall into recovery. We will
6167 * need to do so if our oldest transmit is past the time we should
6170 struct tcp_rack *rack;
6171 struct rack_sendmap *rsm;
6173 uint32_t srtt, thresh;
6175 rack = (struct tcp_rack *)tp->t_fb_ptr;
6176 if (tqhash_empty(rack->r_ctl.tqh)) {
6179 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6184 if (rsm->r_flags & RACK_ACKED) {
6185 rsm = rack_find_lowest_rsm(rack);
6189 idx = rsm->r_rtr_cnt - 1;
6190 srtt = rack_grab_rtt(tp, rack);
6191 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
6192 if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) {
6195 if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) {
6198 /* Ok if we reach here we are over-due and this guy can be sent */
6199 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
6204 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
6210 t = (tp->t_srtt + (tp->t_rttvar << 2));
6211 RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
6212 rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop);
6213 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
6214 ret_val = (uint32_t)tt;
6219 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
6222 * Start the FR timer, we do this based on getting the first one in
6223 * the rc_tmap. Note that if its NULL we must stop the timer. in all
6224 * events we need to stop the running timer (if its running) before
6225 * starting the new one.
6227 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
6230 int32_t is_tlp_timer = 0;
6231 struct rack_sendmap *rsm;
6233 if (rack->t_timers_stopped) {
6234 /* All timers have been stopped none are to run */
6237 if (rack->rc_in_persist) {
6238 /* We can't start any timer in persists */
6239 return (rack_get_persists_timer_val(tp, rack));
6241 rack->rc_on_min_to = 0;
6242 if ((tp->t_state < TCPS_ESTABLISHED) ||
6243 (rack->sack_attack_disable > 0) ||
6244 ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
6247 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6248 if ((rsm == NULL) || sup_rack) {
6249 /* Nothing on the send map or no rack */
6251 time_since_sent = 0;
6252 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6255 * Should we discount the RTX timer any?
6257 * We want to discount it the smallest amount.
6258 * If a timer (Rack/TLP or RXT) has gone off more
6259 * recently thats the discount we want to use (now - timer time).
6260 * If the retransmit of the oldest packet was more recent then
6261 * we want to use that (now - oldest-packet-last_transmit_time).
6264 idx = rsm->r_rtr_cnt - 1;
6265 if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx])))
6266 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6268 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6269 if (TSTMP_GT(cts, tstmp_touse))
6270 time_since_sent = cts - tstmp_touse;
6272 if (SEQ_LT(tp->snd_una, tp->snd_max) ||
6273 sbavail(&tptosocket(tp)->so_snd)) {
6274 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
6276 if (to > time_since_sent)
6277 to -= time_since_sent;
6279 to = rack->r_ctl.rc_min_to;
6282 /* Special case for KEEPINIT */
6283 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
6284 (TP_KEEPINIT(tp) != 0) &&
6287 * We have to put a ceiling on the rxt timer
6288 * of the keep-init timeout.
6290 uint32_t max_time, red;
6292 max_time = TICKS_2_USEC(TP_KEEPINIT(tp));
6293 if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) {
6294 red = (cts - (uint32_t)rsm->r_tim_lastsent[0]);
6300 /* Reduce timeout to the keep value if needed */
6308 if (rsm->r_flags & RACK_ACKED) {
6309 rsm = rack_find_lowest_rsm(rack);
6315 if (rack->sack_attack_disable) {
6317 * We don't want to do
6318 * any TLP's if you are an attacker.
6319 * Though if you are doing what
6320 * is expected you may still have
6321 * SACK-PASSED marks.
6325 /* Convert from ms to usecs */
6326 if ((rsm->r_flags & RACK_SACK_PASSED) ||
6327 (rsm->r_flags & RACK_RWND_COLLAPSED) ||
6328 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
6329 if ((tp->t_flags & TF_SENTFIN) &&
6330 ((tp->snd_max - tp->snd_una) == 1) &&
6331 (rsm->r_flags & RACK_HAS_FIN)) {
6333 * We don't start a rack timer if all we have is a
6338 if ((rack->use_rack_rr == 0) &&
6339 (IN_FASTRECOVERY(tp->t_flags)) &&
6340 (rack->rack_no_prr == 0) &&
6341 (rack->r_ctl.rc_prr_sndcnt < ctf_fixed_maxseg(tp))) {
6343 * We are not cheating, in recovery and
6344 * not enough ack's to yet get our next
6345 * retransmission out.
6347 * Note that classified attackers do not
6348 * get to use the rack-cheat.
6352 srtt = rack_grab_rtt(tp, rack);
6353 thresh = rack_calc_thresh_rack(rack, srtt, cts);
6354 idx = rsm->r_rtr_cnt - 1;
6355 exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh;
6356 if (SEQ_GEQ(exp, cts)) {
6358 if (to < rack->r_ctl.rc_min_to) {
6359 to = rack->r_ctl.rc_min_to;
6360 if (rack->r_rr_config == 3)
6361 rack->rc_on_min_to = 1;
6364 to = rack->r_ctl.rc_min_to;
6365 if (rack->r_rr_config == 3)
6366 rack->rc_on_min_to = 1;
6369 /* Ok we need to do a TLP not RACK */
6371 if ((rack->rc_tlp_in_progress != 0) &&
6372 (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
6374 * The previous send was a TLP and we have sent
6375 * N TLP's without sending new data.
6379 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
6381 /* We found no rsm to TLP with. */
6384 if (rsm->r_flags & RACK_HAS_FIN) {
6385 /* If its a FIN we dont do TLP */
6389 idx = rsm->r_rtr_cnt - 1;
6390 time_since_sent = 0;
6391 if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time))
6392 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx];
6394 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time;
6395 if (TSTMP_GT(cts, tstmp_touse))
6396 time_since_sent = cts - tstmp_touse;
6399 if ((rack->rc_srtt_measure_made == 0) &&
6400 (tp->t_srtt == 1)) {
6402 * If another stack as run and set srtt to 1,
6403 * then the srtt was 0, so lets use the initial.
6405 srtt = RACK_INITIAL_RTO;
6407 srtt_cur = tp->t_srtt;
6411 srtt = RACK_INITIAL_RTO;
6413 * If the SRTT is not keeping up and the
6414 * rack RTT has spiked we want to use
6415 * the last RTT not the smoothed one.
6417 if (rack_tlp_use_greater &&
6419 (srtt < rack_grab_rtt(tp, rack))) {
6420 srtt = rack_grab_rtt(tp, rack);
6422 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
6423 if (thresh > time_since_sent) {
6424 to = thresh - time_since_sent;
6426 to = rack->r_ctl.rc_min_to;
6427 rack_log_alt_to_to_cancel(rack,
6429 time_since_sent, /* flex2 */
6430 tstmp_touse, /* flex3 */
6431 rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
6432 (uint32_t)rsm->r_tim_lastsent[idx],
6436 if (to < rack_tlp_min) {
6439 if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) {
6441 * If the TLP time works out to larger than the max
6442 * RTO lets not do TLP.. just RTO.
6447 if (is_tlp_timer == 0) {
6448 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
6450 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
6458 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una)
6462 if (rack->rc_in_persist == 0) {
6463 if (tp->t_flags & TF_GPUTINPROG) {
6465 * Stop the goodput now, the calling of the
6466 * measurement function clears the flag.
6468 rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__,
6469 RACK_QUALITY_PERSIST);
6471 #ifdef NETFLIX_SHARED_CWND
6472 if (rack->r_ctl.rc_scw) {
6473 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6474 rack->rack_scwnd_is_idle = 1;
6477 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(&tv);
6478 if (rack->lt_bw_up) {
6479 /* Suspend our LT BW measurement */
6482 rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq);
6483 rack->r_ctl.lt_seq = snd_una;
6484 tmark = tcp_tv_to_lusectick(&tv);
6485 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
6486 rack->r_ctl.lt_timemark = tmark;
6488 rack->r_persist_lt_bw_off = 1;
6490 if (rack->r_ctl.rc_went_idle_time == 0)
6491 rack->r_ctl.rc_went_idle_time = 1;
6492 rack_timer_cancel(tp, rack, cts, __LINE__);
6493 rack->r_ctl.persist_lost_ends = 0;
6494 rack->probe_not_answered = 0;
6495 rack->forced_ack = 0;
6497 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6498 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6499 rack->rc_in_persist = 1;
6504 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6509 if (tcp_in_hpts(rack->rc_tp)) {
6510 tcp_hpts_remove(rack->rc_tp);
6511 rack->r_ctl.rc_hpts_flags = 0;
6513 #ifdef NETFLIX_SHARED_CWND
6514 if (rack->r_ctl.rc_scw) {
6515 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
6516 rack->rack_scwnd_is_idle = 0;
6519 t_time = tcp_get_usecs(&tv);
6520 if (rack->rc_gp_dyn_mul &&
6521 (rack->use_fixed_rate == 0) &&
6522 (rack->rc_always_pace)) {
6524 * Do we count this as if a probe-rtt just
6527 uint32_t time_idle, idle_min;
6529 time_idle = t_time - rack->r_ctl.rc_went_idle_time;
6530 idle_min = rack_min_probertt_hold;
6531 if (rack_probertt_gpsrtt_cnt_div) {
6533 extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
6534 (uint64_t)rack_probertt_gpsrtt_cnt_mul;
6535 extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
6536 idle_min += (uint32_t)extra;
6538 if (time_idle >= idle_min) {
6539 /* Yes, we count it as a probe-rtt. */
6542 us_cts = tcp_get_usecs(NULL);
6543 if (rack->in_probe_rtt == 0) {
6544 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
6545 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
6546 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
6547 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
6549 rack_exit_probertt(rack, us_cts);
6553 if (rack->r_persist_lt_bw_off) {
6554 /* Continue where we left off */
6555 rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
6557 rack->r_persist_lt_bw_off = 0;
6559 rack->rc_in_persist = 0;
6560 rack->r_ctl.rc_went_idle_time = 0;
6562 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
6563 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
6564 rack->r_ctl.rc_agg_delayed = 0;
6567 rack->r_ctl.rc_agg_early = 0;
6571 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
6572 struct hpts_diag *diag, struct timeval *tv)
6574 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6575 union tcp_log_stackspecific log;
6577 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6578 log.u_bbr.flex1 = diag->p_nxt_slot;
6579 log.u_bbr.flex2 = diag->p_cur_slot;
6580 log.u_bbr.flex3 = diag->slot_req;
6581 log.u_bbr.flex4 = diag->inp_hptsslot;
6582 log.u_bbr.flex5 = diag->slot_remaining;
6583 log.u_bbr.flex6 = diag->need_new_to;
6584 log.u_bbr.flex7 = diag->p_hpts_active;
6585 log.u_bbr.flex8 = diag->p_on_min_sleep;
6586 /* Hijack other fields as needed */
6587 log.u_bbr.epoch = diag->have_slept;
6588 log.u_bbr.lt_epoch = diag->yet_to_sleep;
6589 log.u_bbr.pkts_out = diag->co_ret;
6590 log.u_bbr.applimited = diag->hpts_sleep_time;
6591 log.u_bbr.delivered = diag->p_prev_slot;
6592 log.u_bbr.inflight = diag->p_runningslot;
6593 log.u_bbr.bw_inuse = diag->wheel_slot;
6594 log.u_bbr.rttProp = diag->wheel_cts;
6595 log.u_bbr.timeStamp = cts;
6596 log.u_bbr.delRate = diag->maxslots;
6597 log.u_bbr.cur_del_rate = diag->p_curtick;
6598 log.u_bbr.cur_del_rate <<= 32;
6599 log.u_bbr.cur_del_rate |= diag->p_lasttick;
6600 TCP_LOG_EVENTP(rack->rc_tp, NULL,
6601 &rack->rc_inp->inp_socket->so_rcv,
6602 &rack->rc_inp->inp_socket->so_snd,
6603 BBR_LOG_HPTSDIAG, 0,
6604 0, &log, false, tv);
6610 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type)
6612 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) {
6613 union tcp_log_stackspecific log;
6616 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6617 log.u_bbr.flex1 = sb->sb_flags;
6618 log.u_bbr.flex2 = len;
6619 log.u_bbr.flex3 = sb->sb_state;
6620 log.u_bbr.flex8 = type;
6621 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
6622 TCP_LOG_EVENTP(rack->rc_tp, NULL,
6623 &rack->rc_inp->inp_socket->so_rcv,
6624 &rack->rc_inp->inp_socket->so_snd,
6626 len, &log, false, &tv);
6631 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
6632 int32_t slot, uint32_t tot_len_this_send, int sup_rack)
6634 struct hpts_diag diag;
6635 struct inpcb *inp = tptoinpcb(tp);
6637 uint32_t delayed_ack = 0;
6638 uint32_t hpts_timeout;
6639 uint32_t entry_slot = slot;
6644 if ((tp->t_state == TCPS_CLOSED) ||
6645 (tp->t_state == TCPS_LISTEN)) {
6648 if (tcp_in_hpts(tp)) {
6649 /* Already on the pacer */
6652 stopped = rack->rc_tmr_stopped;
6653 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
6654 left = rack->r_ctl.rc_timer_exp - cts;
6656 rack->r_ctl.rc_timer_exp = 0;
6657 rack->r_ctl.rc_hpts_flags = 0;
6658 us_cts = tcp_get_usecs(&tv);
6659 /* Now early/late accounting */
6660 rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0);
6661 if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) {
6663 * We have a early carry over set,
6664 * we can always add more time so we
6665 * can always make this compensation.
6667 * Note if ack's are allowed to wake us do not
6668 * penalize the next timer for being awoke
6669 * by an ack aka the rc_agg_early (non-paced mode).
6671 slot += rack->r_ctl.rc_agg_early;
6673 rack->r_ctl.rc_agg_early = 0;
6677 * This is harder, we can
6678 * compensate some but it
6679 * really depends on what
6680 * the current pacing time is.
6682 if (rack->r_ctl.rc_agg_delayed >= slot) {
6684 * We can't compensate for it all.
6685 * And we have to have some time
6686 * on the clock. We always have a min
6687 * 10 slots (10 x 10 i.e. 100 usecs).
6689 if (slot <= HPTS_TICKS_PER_SLOT) {
6691 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot);
6692 slot = HPTS_TICKS_PER_SLOT;
6694 /* We take off some */
6695 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT);
6696 slot = HPTS_TICKS_PER_SLOT;
6699 slot -= rack->r_ctl.rc_agg_delayed;
6700 rack->r_ctl.rc_agg_delayed = 0;
6701 /* Make sure we have 100 useconds at minimum */
6702 if (slot < HPTS_TICKS_PER_SLOT) {
6703 rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot;
6704 slot = HPTS_TICKS_PER_SLOT;
6706 if (rack->r_ctl.rc_agg_delayed == 0)
6710 hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
6711 #ifdef TCP_SAD_DETECTION
6712 if (rack->sack_attack_disable &&
6713 (rack->r_ctl.ack_during_sd > 0) &&
6714 (slot < tcp_sad_pacing_interval)) {
6716 * We have a potential attacker on
6717 * the line. We have possibly some
6718 * (or now) pacing time set. We want to
6719 * slow down the processing of sacks by some
6720 * amount (if it is an attacker). Set the default
6721 * slot for attackers in place (unless the original
6722 * interval is longer). Its stored in
6723 * micro-seconds, so lets convert to msecs.
6725 slot = tcp_sad_pacing_interval;
6726 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
6727 rack->r_ctl.ack_during_sd = 0;
6730 if (tp->t_flags & TF_DELACK) {
6731 delayed_ack = TICKS_2_USEC(tcp_delacktime);
6732 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
6734 if (delayed_ack && ((hpts_timeout == 0) ||
6735 (delayed_ack < hpts_timeout)))
6736 hpts_timeout = delayed_ack;
6738 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
6740 * If no timers are going to run and we will fall off the hptsi
6741 * wheel, we resort to a keep-alive timer if its configured.
6743 if ((hpts_timeout == 0) &&
6745 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
6746 (tp->t_state <= TCPS_CLOSING)) {
6748 * Ok we have no timer (persists, rack, tlp, rxt or
6749 * del-ack), we don't have segments being paced. So
6750 * all that is left is the keepalive timer.
6752 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
6753 /* Get the established keep-alive time */
6754 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp));
6757 * Get the initial setup keep-alive time,
6758 * note that this is probably not going to
6759 * happen, since rack will be running a rxt timer
6760 * if a SYN of some sort is outstanding. It is
6761 * actually handled in rack_timeout_rxt().
6763 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp));
6765 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
6766 if (rack->in_probe_rtt) {
6768 * We want to instead not wake up a long time from
6769 * now but to wake up about the time we would
6770 * exit probe-rtt and initiate a keep-alive ack.
6771 * This will get us out of probe-rtt and update
6774 hpts_timeout = rack_min_probertt_hold;
6778 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
6779 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
6781 * RACK, TLP, persists and RXT timers all are restartable
6782 * based on actions input .. i.e we received a packet (ack
6783 * or sack) and that changes things (rw, or snd_una etc).
6784 * Thus we can restart them with a new value. For
6785 * keep-alive, delayed_ack we keep track of what was left
6786 * and restart the timer with a smaller value.
6788 if (left < hpts_timeout)
6789 hpts_timeout = left;
6793 * Hack alert for now we can't time-out over 2,147,483
6794 * seconds (a bit more than 596 hours), which is probably ok
6797 if (hpts_timeout > 0x7ffffffe)
6798 hpts_timeout = 0x7ffffffe;
6799 rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
6801 rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0);
6802 if ((rack->gp_ready == 0) &&
6803 (rack->use_fixed_rate == 0) &&
6804 (hpts_timeout < slot) &&
6805 (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
6807 * We have no good estimate yet for the
6808 * old clunky burst mitigation or the
6809 * real pacing. And the tlp or rxt is smaller
6810 * than the pacing calculation. Lets not
6811 * pace that long since we know the calculation
6812 * so far is not accurate.
6814 slot = hpts_timeout;
6817 * Turn off all the flags for queuing by default. The
6818 * flags have important meanings to what happens when
6819 * LRO interacts with the transport. Most likely (by default now)
6820 * mbuf_queueing and ack compression are on. So the transport
6821 * has a couple of flags that control what happens (if those
6822 * are not on then these flags won't have any effect since it
6823 * won't go through the queuing LRO path).
6825 * TF2_MBUF_QUEUE_READY - This flags says that I am busy
6826 * pacing output, so don't disturb. But
6827 * it also means LRO can wake me if there
6828 * is a SACK arrival.
6830 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction
6831 * with the above flag (QUEUE_READY) and
6832 * when present it says don't even wake me
6833 * if a SACK arrives.
6835 * The idea behind these flags is that if we are pacing we
6836 * set the MBUF_QUEUE_READY and only get woken up if
6837 * a SACK arrives (which could change things) or if
6838 * our pacing timer expires. If, however, we have a rack
6839 * timer running, then we don't even want a sack to wake
6840 * us since the rack timer has to expire before we can send.
6842 * Other cases should usually have none of the flags set
6843 * so LRO can call into us.
6845 tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY);
6847 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
6848 rack->r_ctl.rc_last_output_to = us_cts + slot;
6850 * A pacing timer (slot) is being set, in
6851 * such a case we cannot send (we are blocked by
6852 * the timer). So lets tell LRO that it should not
6853 * wake us unless there is a SACK. Note this only
6854 * will be effective if mbuf queueing is on or
6855 * compressed acks are being processed.
6857 tp->t_flags2 |= TF2_MBUF_QUEUE_READY;
6859 * But wait if we have a Rack timer running
6860 * even a SACK should not disturb us (with
6861 * the exception of r_rr_config 3).
6863 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) {
6864 if (rack->r_rr_config != 3)
6865 tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6866 else if (rack->rc_pace_dnd) {
6867 if (IN_RECOVERY(tp->t_flags)) {
6869 * When DND is on, we only let a sack
6870 * interrupt us if we are not in recovery.
6872 * If DND is off, then we never hit here
6873 * and let all sacks wake us up.
6876 tp->t_flags2 |= TF2_DONT_SACK_QUEUE;
6880 /* For sack attackers we want to ignore sack */
6881 if (rack->sack_attack_disable == 1) {
6882 tp->t_flags2 |= (TF2_DONT_SACK_QUEUE |
6883 TF2_MBUF_QUEUE_READY);
6884 } else if (rack->rc_ack_can_sendout_data) {
6886 * Ahh but wait, this is that special case
6887 * where the pacing timer can be disturbed
6888 * backout the changes (used for non-paced
6891 tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE |
6892 TF2_MBUF_QUEUE_READY);
6894 if ((rack->use_rack_rr) &&
6895 (rack->r_rr_config < 2) &&
6896 ((hpts_timeout) && (hpts_timeout < slot))) {
6898 * Arrange for the hpts to kick back in after the
6899 * t-o if the t-o does not cause a send.
6901 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6903 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6904 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6906 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot),
6908 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6909 rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
6911 } else if (hpts_timeout) {
6913 * With respect to t_flags2(?) here, lets let any new acks wake
6914 * us up here. Since we are not pacing (no pacing timer), output
6915 * can happen so we should let it. If its a Rack timer, then any inbound
6916 * packet probably won't change the sending (we will be blocked)
6917 * but it may change the prr stats so letting it in (the set defaults
6918 * at the start of this block) are good enough.
6920 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6921 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout),
6923 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
6924 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
6926 /* No timer starting */
6928 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
6929 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
6930 tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
6934 rack->rc_tmr_stopped = 0;
6936 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__);
6940 * RACK Timer, here we simply do logging and house keeping.
6941 * the normal rack_output() function will call the
6942 * appropriate thing to check if we need to do a RACK retransmit.
6943 * We return 1, saying don't proceed with rack_output only
6944 * when all timers have been stopped (destroyed PCB?).
6947 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
6950 * This timer simply provides an internal trigger to send out data.
6951 * The check_recovery_mode call will see if there are needed
6952 * retransmissions, if so we will enter fast-recovery. The output
6953 * call may or may not do the same thing depending on sysctl
6956 struct rack_sendmap *rsm;
6958 counter_u64_add(rack_to_tot, 1);
6959 if (rack->r_state && (rack->r_state != tp->t_state))
6960 rack_set_state(tp, rack);
6961 rack->rc_on_min_to = 0;
6962 rsm = rack_check_recovery_mode(tp, cts);
6963 rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
6965 rack->r_ctl.rc_resend = rsm;
6966 rack->r_timer_override = 1;
6967 if (rack->use_rack_rr) {
6969 * Don't accumulate extra pacing delay
6970 * we are allowing the rack timer to
6971 * over-ride pacing i.e. rrr takes precedence
6972 * if the pacing interval is longer than the rrr
6973 * time (in other words we get the min pacing
6974 * time versus rrr pacing time).
6976 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
6979 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
6981 /* restart a timer and return 1 */
6982 rack_start_hpts_timer(rack, tp, cts,
6992 rack_adjust_orig_mlen(struct rack_sendmap *rsm)
6995 if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) {
6997 * The trailing space changed, mbufs can grow
6998 * at the tail but they can't shrink from
6999 * it, KASSERT that. Adjust the orig_m_len to
7000 * compensate for this change.
7002 KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)),
7003 ("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
7006 (intmax_t)M_TRAILINGROOM(rsm->m),
7010 rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m));
7011 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7013 if (rsm->m->m_len < rsm->orig_m_len) {
7015 * Mbuf shrank, trimmed off the top by an ack, our
7018 KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)),
7019 ("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n",
7020 rsm->m, rsm->m->m_len,
7021 rsm, rsm->orig_m_len,
7023 if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len))
7024 rsm->soff -= (rsm->orig_m_len - rsm->m->m_len);
7027 rsm->orig_m_len = rsm->m->m_len;
7029 } else if (rsm->m->m_len > rsm->orig_m_len) {
7030 panic("rsm:%p m:%p m_len grew outside of t_space compensation",
7037 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm)
7043 ((src_rsm->orig_m_len != src_rsm->m->m_len) ||
7044 (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) {
7045 /* Fix up the orig_m_len and possibly the mbuf offset */
7046 rack_adjust_orig_mlen(src_rsm);
7049 soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start);
7050 while (soff >= m->m_len) {
7051 /* Move out past this mbuf */
7054 KASSERT((m != NULL),
7055 ("rsm:%p nrsm:%p hit at soff:%u null m",
7056 src_rsm, rsm, soff));
7058 /* This should *not* happen which is why there is a kassert */
7059 src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7060 (src_rsm->r_start - rack->rc_tp->snd_una),
7062 src_rsm->orig_m_len = src_rsm->m->m_len;
7063 src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m);
7064 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
7065 (rsm->r_start - rack->rc_tp->snd_una),
7067 rsm->orig_m_len = rsm->m->m_len;
7068 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7074 rsm->orig_m_len = m->m_len;
7075 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
7078 static __inline void
7079 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
7080 struct rack_sendmap *rsm, uint32_t start)
7084 nrsm->r_start = start;
7085 nrsm->r_end = rsm->r_end;
7086 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
7087 nrsm->r_flags = rsm->r_flags;
7088 nrsm->r_dupack = rsm->r_dupack;
7089 nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed;
7090 nrsm->r_rtr_bytes = 0;
7091 nrsm->r_fas = rsm->r_fas;
7092 nrsm->r_bas = rsm->r_bas;
7093 rsm->r_end = nrsm->r_start;
7094 nrsm->r_just_ret = rsm->r_just_ret;
7095 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
7096 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
7098 /* Now if we have SYN flag we keep it on the left edge */
7099 if (nrsm->r_flags & RACK_HAS_SYN)
7100 nrsm->r_flags &= ~RACK_HAS_SYN;
7101 /* Now if we have a FIN flag we keep it on the right edge */
7102 if (rsm->r_flags & RACK_HAS_FIN)
7103 rsm->r_flags &= ~RACK_HAS_FIN;
7104 /* Push bit must go to the right edge as well */
7105 if (rsm->r_flags & RACK_HAD_PUSH)
7106 rsm->r_flags &= ~RACK_HAD_PUSH;
7107 /* Clone over the state of the hw_tls flag */
7108 nrsm->r_hw_tls = rsm->r_hw_tls;
7110 * Now we need to find nrsm's new location in the mbuf chain
7111 * we basically calculate a new offset, which is soff +
7112 * how much is left in original rsm. Then we walk out the mbuf
7113 * chain to find the righ position, it may be the same mbuf
7116 KASSERT(((rsm->m != NULL) ||
7117 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))),
7118 ("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack));
7120 rack_setup_offset_for_rsm(rack, rsm, nrsm);
7123 static struct rack_sendmap *
7124 rack_merge_rsm(struct tcp_rack *rack,
7125 struct rack_sendmap *l_rsm,
7126 struct rack_sendmap *r_rsm)
7129 * We are merging two ack'd RSM's,
7130 * the l_rsm is on the left (lower seq
7131 * values) and the r_rsm is on the right
7132 * (higher seq value). The simplest way
7133 * to merge these is to move the right
7134 * one into the left. I don't think there
7135 * is any reason we need to try to find
7136 * the oldest (or last oldest retransmitted).
7138 rack_log_map_chg(rack->rc_tp, rack, NULL,
7139 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__);
7140 l_rsm->r_end = r_rsm->r_end;
7141 if (l_rsm->r_dupack < r_rsm->r_dupack)
7142 l_rsm->r_dupack = r_rsm->r_dupack;
7143 if (r_rsm->r_rtr_bytes)
7144 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
7145 if (r_rsm->r_in_tmap) {
7146 /* This really should not happen */
7147 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
7148 r_rsm->r_in_tmap = 0;
7152 if (r_rsm->r_flags & RACK_HAS_FIN)
7153 l_rsm->r_flags |= RACK_HAS_FIN;
7154 if (r_rsm->r_flags & RACK_TLP)
7155 l_rsm->r_flags |= RACK_TLP;
7156 if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
7157 l_rsm->r_flags |= RACK_RWND_COLLAPSED;
7158 if ((r_rsm->r_flags & RACK_APP_LIMITED) &&
7159 ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
7161 * If both are app-limited then let the
7162 * free lower the count. If right is app
7163 * limited and left is not, transfer.
7165 l_rsm->r_flags |= RACK_APP_LIMITED;
7166 r_rsm->r_flags &= ~RACK_APP_LIMITED;
7167 if (r_rsm == rack->r_ctl.rc_first_appl)
7168 rack->r_ctl.rc_first_appl = l_rsm;
7170 tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE);
7172 * We keep the largest value, which is the newest
7173 * send. We do this in case a segment that is
7174 * joined together and not part of a GP estimate
7175 * later gets expanded into the GP estimate.
7177 * We prohibit the merging of unlike kinds i.e.
7178 * all pieces that are in the GP estimate can be
7179 * merged and all pieces that are not in a GP estimate
7180 * can be merged, but not disimilar pieces. Combine
7181 * this with taking the highest here and we should
7182 * be ok unless of course the client reneges. Then
7185 if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] <
7186 r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) {
7187 l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)];
7190 * When merging two RSM's we also need to consider the ack time and keep
7191 * newest. If the ack gets merged into a measurement then that is the
7192 * one we will want to be using.
7194 if(l_rsm->r_ack_arrival < r_rsm->r_ack_arrival)
7195 l_rsm->r_ack_arrival = r_rsm->r_ack_arrival;
7197 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
7198 /* Transfer the split limit to the map we free */
7199 r_rsm->r_limit_type = l_rsm->r_limit_type;
7200 l_rsm->r_limit_type = 0;
7202 rack_free(rack, r_rsm);
7203 l_rsm->r_flags |= RACK_MERGED;
7208 * TLP Timer, here we simply setup what segment we want to
7209 * have the TLP expire on, the normal rack_output() will then
7212 * We return 1, saying don't proceed with rack_output only
7213 * when all timers have been stopped (destroyed PCB?).
7216 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp)
7221 struct rack_sendmap *rsm = NULL;
7222 int insret __diagused;
7223 struct socket *so = tptosocket(tp);
7225 uint32_t out, avail;
7226 int collapsed_win = 0;
7228 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
7229 /* Its not time yet */
7232 if (ctf_progress_timeout_check(tp, true)) {
7233 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7234 return (-ETIMEDOUT); /* tcp_drop() */
7237 * A TLP timer has expired. We have been idle for 2 rtts. So we now
7238 * need to figure out how to force a full MSS segment out.
7240 rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
7241 rack->r_ctl.retran_during_recovery = 0;
7242 rack->r_ctl.dsack_byte_cnt = 0;
7243 counter_u64_add(rack_tlp_tot, 1);
7244 if (rack->r_state && (rack->r_state != tp->t_state))
7245 rack_set_state(tp, rack);
7246 avail = sbavail(&so->so_snd);
7247 out = tp->snd_max - tp->snd_una;
7248 if ((out > tp->snd_wnd) || rack->rc_has_collapsed) {
7249 /* special case, we need a retransmission */
7253 if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) {
7254 rack->r_ctl.dsack_persist--;
7255 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7256 rack->r_ctl.num_dsack = 0;
7258 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7260 if ((tp->t_flags & TF_GPUTINPROG) &&
7261 (rack->r_ctl.rc_tlp_cnt_out == 1)) {
7263 * If this is the second in a row
7264 * TLP and we are doing a measurement
7265 * its time to abandon the measurement.
7266 * Something is likely broken on
7267 * the clients network and measuring a
7268 * broken network does us no good.
7270 tp->t_flags &= ~TF_GPUTINPROG;
7271 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7272 rack->r_ctl.rc_gp_srtt /*flex1*/,
7274 0, 0, 18, __LINE__, NULL, 0);
7277 * Check our send oldest always settings, and if
7278 * there is an oldest to send jump to the need_retran.
7280 if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
7284 /* New data is available */
7286 if (amm > ctf_fixed_maxseg(tp)) {
7287 amm = ctf_fixed_maxseg(tp);
7288 if ((amm + out) > tp->snd_wnd) {
7289 /* We are rwnd limited */
7292 } else if (amm < ctf_fixed_maxseg(tp)) {
7293 /* not enough to fill a MTU */
7296 if (IN_FASTRECOVERY(tp->t_flags)) {
7298 if (rack->rack_no_prr == 0) {
7299 if (out + amm <= tp->snd_wnd) {
7300 rack->r_ctl.rc_prr_sndcnt = amm;
7301 rack->r_ctl.rc_tlp_new_data = amm;
7302 rack_log_to_prr(rack, 4, 0, __LINE__);
7307 /* Set the send-new override */
7308 if (out + amm <= tp->snd_wnd)
7309 rack->r_ctl.rc_tlp_new_data = amm;
7313 rack->r_ctl.rc_tlpsend = NULL;
7314 counter_u64_add(rack_tlp_newdata, 1);
7319 * Ok we need to arrange the last un-acked segment to be re-sent, or
7320 * optionally the first un-acked segment.
7322 if (collapsed_win == 0) {
7323 if (rack_always_send_oldest)
7324 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7326 rsm = tqhash_max(rack->r_ctl.tqh);
7327 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
7328 rsm = rack_find_high_nonack(rack, rsm);
7333 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
7339 * We had a collapsed window, lets find
7340 * the point before the collapse.
7342 if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una))
7343 rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1));
7345 rsm = tqhash_min(rack->r_ctl.tqh);
7352 if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
7354 * We need to split this the last segment in two.
7356 struct rack_sendmap *nrsm;
7358 nrsm = rack_alloc_full_limit(rack);
7361 * No memory to split, we will just exit and punt
7362 * off to the RXT timer.
7366 rack_clone_rsm(rack, nrsm, rsm,
7367 (rsm->r_end - ctf_fixed_maxseg(tp)));
7368 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
7370 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
7372 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
7373 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
7374 nrsm, insret, rack, rsm);
7377 if (rsm->r_in_tmap) {
7378 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7379 nrsm->r_in_tmap = 1;
7383 rack->r_ctl.rc_tlpsend = rsm;
7385 /* Make sure output path knows we are doing a TLP */
7387 rack->r_timer_override = 1;
7388 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7391 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
7396 * Delayed ack Timer, here we simply need to setup the
7397 * ACK_NOW flag and remove the DELACK flag. From there
7398 * the output routine will send the ack out.
7400 * We only return 1, saying don't proceed, if all timers
7401 * are stopped (destroyed PCB?).
7404 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7407 rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
7408 tp->t_flags &= ~TF_DELACK;
7409 tp->t_flags |= TF_ACKNOW;
7410 KMOD_TCPSTAT_INC(tcps_delack);
7411 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
7416 * Persists timer, here we simply send the
7417 * same thing as a keepalive will.
7418 * the one byte send.
7420 * We only return 1, saying don't proceed, if all timers
7421 * are stopped (destroyed PCB?).
7424 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7426 struct tcptemp *t_template;
7429 if (rack->rc_in_persist == 0)
7431 if (ctf_progress_timeout_check(tp, false)) {
7432 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7433 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7434 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7435 return (-ETIMEDOUT); /* tcp_drop() */
7438 * Persistence timer into zero window. Force a byte to be output, if
7441 KMOD_TCPSTAT_INC(tcps_persisttimeo);
7443 * Hack: if the peer is dead/unreachable, we do not time out if the
7444 * window is closed. After a full backoff, drop the connection if
7445 * the idle time (no responses to probes) reaches the maximum
7446 * backoff that we would use if retransmitting.
7448 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
7449 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
7450 TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) {
7451 KMOD_TCPSTAT_INC(tcps_persistdrop);
7452 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7453 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7454 retval = -ETIMEDOUT; /* tcp_drop() */
7457 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
7458 tp->snd_una == tp->snd_max)
7459 rack_exit_persist(tp, rack, cts);
7460 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
7462 * If the user has closed the socket then drop a persisting
7463 * connection after a much reduced timeout.
7465 if (tp->t_state > TCPS_CLOSE_WAIT &&
7466 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
7467 KMOD_TCPSTAT_INC(tcps_persistdrop);
7468 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
7469 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends);
7470 retval = -ETIMEDOUT; /* tcp_drop() */
7473 t_template = tcpip_maketemplate(rack->rc_inp);
7475 /* only set it if we were answered */
7476 if (rack->forced_ack == 0) {
7477 rack->forced_ack = 1;
7478 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7480 rack->probe_not_answered = 1;
7481 counter_u64_add(rack_persists_loss, 1);
7482 rack->r_ctl.persist_lost_ends++;
7484 counter_u64_add(rack_persists_sends, 1);
7485 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
7486 tcp_respond(tp, t_template->tt_ipgen,
7487 &t_template->tt_t, (struct mbuf *)NULL,
7488 tp->rcv_nxt, tp->snd_una - 1, 0);
7489 /* This sends an ack */
7490 if (tp->t_flags & TF_DELACK)
7491 tp->t_flags &= ~TF_DELACK;
7492 free(t_template, M_TEMP);
7494 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
7497 rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
7498 rack_start_hpts_timer(rack, tp, cts,
7504 * If a keepalive goes off, we had no other timers
7505 * happening. We always return 1 here since this
7506 * routine either drops the connection or sends
7507 * out a segment with respond.
7510 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7512 struct tcptemp *t_template;
7513 struct inpcb *inp = tptoinpcb(tp);
7515 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
7516 rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
7518 * Keep-alive timer went off; send something or drop connection if
7519 * idle for too long.
7521 KMOD_TCPSTAT_INC(tcps_keeptimeo);
7522 if (tp->t_state < TCPS_ESTABLISHED)
7524 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
7525 tp->t_state <= TCPS_CLOSING) {
7526 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
7529 * Send a packet designed to force a response if the peer is
7530 * up and reachable: either an ACK if the connection is
7531 * still alive, or an RST if the peer has closed the
7532 * connection due to timeout or reboot. Using sequence
7533 * number tp->snd_una-1 causes the transmitted zero-length
7534 * segment to lie outside the receive window; by the
7535 * protocol spec, this requires the correspondent TCP to
7538 KMOD_TCPSTAT_INC(tcps_keepprobe);
7539 t_template = tcpip_maketemplate(inp);
7541 if (rack->forced_ack == 0) {
7542 rack->forced_ack = 1;
7543 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
7545 rack->probe_not_answered = 1;
7547 tcp_respond(tp, t_template->tt_ipgen,
7548 &t_template->tt_t, (struct mbuf *)NULL,
7549 tp->rcv_nxt, tp->snd_una - 1, 0);
7550 free(t_template, M_TEMP);
7553 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
7556 KMOD_TCPSTAT_INC(tcps_keepdrops);
7557 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7558 return (-ETIMEDOUT); /* tcp_drop() */
7562 * Retransmit helper function, clear up all the ack
7563 * flags and take care of important book keeping.
7566 rack_remxt_tmr(struct tcpcb *tp)
7569 * The retransmit timer went off, all sack'd blocks must be
7572 struct rack_sendmap *rsm, *trsm = NULL;
7573 struct tcp_rack *rack;
7575 rack = (struct tcp_rack *)tp->t_fb_ptr;
7576 rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__);
7577 rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
7578 if (rack->r_state && (rack->r_state != tp->t_state))
7579 rack_set_state(tp, rack);
7581 * Ideally we would like to be able to
7582 * mark SACK-PASS on anything not acked here.
7584 * However, if we do that we would burst out
7585 * all that data 1ms apart. This would be unwise,
7586 * so for now we will just let the normal rxt timer
7587 * and tlp timer take care of it.
7589 * Also we really need to stick them back in sequence
7590 * order. This way we send in the proper order and any
7591 * sacks that come floating in will "re-ack" the data.
7592 * To do this we zap the tmap with an INIT and then
7593 * walk through and place every rsm in the RB tree
7594 * back in its seq ordered place.
7596 TAILQ_INIT(&rack->r_ctl.rc_tmap);
7598 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) {
7600 if (rack_verbose_logging)
7601 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7602 /* We must re-add it back to the tlist */
7604 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7606 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
7610 if (rsm->r_flags & RACK_ACKED)
7611 rsm->r_flags |= RACK_WAS_ACKED;
7612 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
7613 rsm->r_flags |= RACK_MUST_RXT;
7615 /* Clear the count (we just un-acked them) */
7616 rack->r_ctl.rc_last_timeout_snduna = tp->snd_una;
7617 rack->r_ctl.rc_sacked = 0;
7618 rack->r_ctl.rc_sacklast = NULL;
7619 rack->r_ctl.rc_agg_delayed = 0;
7621 rack->r_ctl.rc_agg_early = 0;
7623 /* Clear the tlp rtx mark */
7624 rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh);
7625 if (rack->r_ctl.rc_resend != NULL)
7626 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT;
7627 rack->r_ctl.rc_prr_sndcnt = 0;
7628 rack_log_to_prr(rack, 6, 0, __LINE__);
7629 rack->r_timer_override = 1;
7630 if ((((tp->t_flags & TF_SACK_PERMIT) == 0)
7631 #ifdef TCP_SAD_DETECTION
7632 || (rack->sack_attack_disable != 0)
7634 ) && ((tp->t_flags & TF_SENTFIN) == 0)) {
7636 * For non-sack customers new data
7637 * needs to go out as retransmits until
7638 * we retransmit up to snd_max.
7640 rack->r_must_retran = 1;
7641 rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp,
7642 rack->r_ctl.rc_sacked);
7644 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
7648 rack_convert_rtts(struct tcpcb *tp)
7650 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
7651 tp->t_rxtcur = RACK_REXMTVAL(tp);
7652 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
7653 tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop);
7655 if (tp->t_rxtcur > rack_rto_max) {
7656 tp->t_rxtcur = rack_rto_max;
7661 rack_cc_conn_init(struct tcpcb *tp)
7663 struct tcp_rack *rack;
7666 rack = (struct tcp_rack *)tp->t_fb_ptr;
7670 * Now convert to rack's internal format,
7673 if ((srtt == 0) && (tp->t_srtt != 0))
7674 rack_convert_rtts(tp);
7676 * We want a chance to stay in slowstart as
7677 * we create a connection. TCP spec says that
7678 * initially ssthresh is infinite. For our
7679 * purposes that is the snd_wnd.
7681 if (tp->snd_ssthresh < tp->snd_wnd) {
7682 tp->snd_ssthresh = tp->snd_wnd;
7685 * We also want to assure a IW worth of
7686 * data can get inflight.
7688 if (rc_init_window(rack) < tp->snd_cwnd)
7689 tp->snd_cwnd = rc_init_window(rack);
7693 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
7694 * we will setup to retransmit the lowest seq number outstanding.
7697 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
7699 struct inpcb *inp = tptoinpcb(tp);
7704 if ((tp->t_flags & TF_GPUTINPROG) &&
7707 * We have had a second timeout
7708 * measurements on successive rxt's are not profitable.
7709 * It is unlikely to be of any use (the network is
7710 * broken or the client went away).
7712 tp->t_flags &= ~TF_GPUTINPROG;
7713 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7714 rack->r_ctl.rc_gp_srtt /*flex1*/,
7716 0, 0, 18, __LINE__, NULL, 0);
7718 if (ctf_progress_timeout_check(tp, false)) {
7719 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7720 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
7721 return (-ETIMEDOUT); /* tcp_drop() */
7723 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
7724 rack->r_ctl.retran_during_recovery = 0;
7725 rack->rc_ack_required = 1;
7726 rack->r_ctl.dsack_byte_cnt = 0;
7727 if (IN_FASTRECOVERY(tp->t_flags))
7728 tp->t_flags |= TF_WASFRECOVERY;
7730 tp->t_flags &= ~TF_WASFRECOVERY;
7731 if (IN_CONGRECOVERY(tp->t_flags))
7732 tp->t_flags |= TF_WASCRECOVERY;
7734 tp->t_flags &= ~TF_WASCRECOVERY;
7735 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
7736 (tp->snd_una == tp->snd_max)) {
7737 /* Nothing outstanding .. nothing to do */
7740 if (rack->r_ctl.dsack_persist) {
7741 rack->r_ctl.dsack_persist--;
7742 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) {
7743 rack->r_ctl.num_dsack = 0;
7745 rack_log_dsack_event(rack, 1, __LINE__, 0, 0);
7748 * Rack can only run one timer at a time, so we cannot
7749 * run a KEEPINIT (gating SYN sending) and a retransmit
7750 * timer for the SYN. So if we are in a front state and
7751 * have a KEEPINIT timer we need to check the first transmit
7752 * against now to see if we have exceeded the KEEPINIT time
7755 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) &&
7756 (TP_KEEPINIT(tp) != 0)) {
7757 struct rack_sendmap *rsm;
7759 rsm = tqhash_min(rack->r_ctl.tqh);
7761 /* Ok we have something outstanding to test keepinit with */
7762 if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) &&
7763 ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) {
7764 /* We have exceeded the KEEPINIT time */
7765 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
7771 * Retransmission timer went off. Message has not been acked within
7772 * retransmit interval. Back off to a longer retransmit interval
7773 * and retransmit one segment.
7776 if ((rack->r_ctl.rc_resend == NULL) ||
7777 ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
7779 * If the rwnd collapsed on
7780 * the one we are retransmitting
7781 * it does not count against the
7786 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
7787 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
7789 tp->t_rxtshift = TCP_MAXRXTSHIFT;
7790 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
7791 /* XXXGL: previously t_softerror was casted to uint16_t */
7792 MPASS(tp->t_softerror >= 0);
7793 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT;
7794 goto out; /* tcp_drop() */
7796 if (tp->t_state == TCPS_SYN_SENT) {
7798 * If the SYN was retransmitted, indicate CWND to be limited
7799 * to 1 segment in cc_conn_init().
7802 } else if (tp->t_rxtshift == 1) {
7804 * first retransmit; record ssthresh and cwnd so they can be
7805 * recovered if this turns out to be a "bad" retransmit. A
7806 * retransmit is considered "bad" if an ACK for this segment
7807 * is received within RTT/2 interval; the assumption here is
7808 * that the ACK was already in flight. See "On Estimating
7809 * End-to-End Network Path Properties" by Allman and Paxson
7812 tp->snd_cwnd_prev = tp->snd_cwnd;
7813 tp->snd_ssthresh_prev = tp->snd_ssthresh;
7814 tp->snd_recover_prev = tp->snd_recover;
7815 tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2);
7816 tp->t_flags |= TF_PREVVALID;
7817 } else if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
7818 tp->t_flags &= ~TF_PREVVALID;
7819 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
7820 if ((tp->t_state == TCPS_SYN_SENT) ||
7821 (tp->t_state == TCPS_SYN_RECEIVED))
7822 rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift];
7824 rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift];
7826 RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt,
7827 max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop);
7829 * We enter the path for PLMTUD if connection is established or, if
7830 * connection is FIN_WAIT_1 status, reason for the last is that if
7831 * amount of data we send is very small, we could send it in couple
7832 * of packets and process straight to FIN. In that case we won't
7833 * catch ESTABLISHED state.
7836 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false;
7840 if (((V_tcp_pmtud_blackhole_detect == 1) ||
7841 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
7842 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
7843 ((tp->t_state == TCPS_ESTABLISHED) ||
7844 (tp->t_state == TCPS_FIN_WAIT_1))) {
7846 * Idea here is that at each stage of mtu probe (usually,
7847 * 1448 -> 1188 -> 524) should be given 2 chances to recover
7848 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
7849 * should take care of that.
7851 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
7852 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
7853 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
7854 tp->t_rxtshift % 2 == 0)) {
7856 * Enter Path MTU Black-hole Detection mechanism: -
7857 * Disable Path MTU Discovery (IP "DF" bit). -
7858 * Reduce MTU to lower value than what we negotiated
7861 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
7862 /* Record that we may have found a black hole. */
7863 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
7864 /* Keep track of previous MSS. */
7865 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7869 * Reduce the MSS to blackhole value or to the
7870 * default in an attempt to retransmit.
7874 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
7875 /* Use the sysctl tuneable blackhole MSS. */
7876 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
7877 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7878 } else if (isipv6) {
7879 /* Use the default MSS. */
7880 tp->t_maxseg = V_tcp_v6mssdflt;
7882 * Disable Path MTU Discovery when we switch
7885 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7886 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7889 #if defined(INET6) && defined(INET)
7893 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
7894 /* Use the sysctl tuneable blackhole MSS. */
7895 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
7896 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
7898 /* Use the default MSS. */
7899 tp->t_maxseg = V_tcp_mssdflt;
7901 * Disable Path MTU Discovery when we switch
7904 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
7905 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
7910 * If further retransmissions are still unsuccessful
7911 * with a lowered MTU, maybe this isn't a blackhole
7912 * and we restore the previous MSS and blackhole
7913 * detection flags. The limit '6' is determined by
7914 * giving each probe stage (1448, 1188, 524) 2
7915 * chances to recover.
7917 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7918 (tp->t_rxtshift >= 6)) {
7919 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
7920 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7921 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
7922 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
7927 * Disable RFC1323 and SACK if we haven't got any response to
7928 * our third SYN to work-around some broken terminal servers
7929 * (most of which have hopefully been retired) that have bad VJ
7930 * header compression code which trashes TCP segments containing
7931 * unknown-to-them TCP options.
7933 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7934 (tp->t_rxtshift == 3))
7935 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7937 * If we backed off this far, our srtt estimate is probably bogus.
7938 * Clobber it so we'll take the next rtt measurement as our srtt;
7939 * move the current srtt into rttvar to keep the current retransmit
7942 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
7944 if ((inp->inp_vflag & INP_IPV6) != 0)
7949 tp->t_rttvar += tp->t_srtt;
7952 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
7953 tp->snd_recover = tp->snd_max;
7954 tp->t_flags |= TF_ACKNOW;
7956 rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__);
7962 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp)
7965 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
7967 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
7968 (tp->t_flags & TF_GPUTINPROG)) {
7970 * We have a goodput in progress
7971 * and we have entered a late state.
7972 * Do we have enough data in the sb
7973 * to handle the GPUT request?
7977 bytes = tp->gput_ack - tp->gput_seq;
7978 if (SEQ_GT(tp->gput_seq, tp->snd_una))
7979 bytes += tp->gput_seq - tp->snd_una;
7980 if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
7982 * There are not enough bytes in the socket
7983 * buffer that have been sent to cover this
7984 * measurement. Cancel it.
7986 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
7987 rack->r_ctl.rc_gp_srtt /*flex1*/,
7989 0, 0, 18, __LINE__, NULL, 0);
7990 tp->t_flags &= ~TF_GPUTINPROG;
7996 if (tp->t_state == TCPS_LISTEN) {
7997 /* no timers on listen sockets */
7998 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
8002 if ((timers & PACE_TMR_RACK) &&
8003 rack->rc_on_min_to) {
8005 * For the rack timer when we
8006 * are on a min-timeout (which means rrr_conf = 3)
8007 * we don't want to check the timer. It may
8008 * be going off for a pace and thats ok we
8009 * want to send the retransmit (if its ready).
8011 * If its on a normal rack timer (non-min) then
8012 * we will check if its expired.
8014 goto skip_time_check;
8016 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
8019 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
8021 rack_log_to_processing(rack, cts, ret, 0);
8024 if (hpts_calling == 0) {
8026 * A user send or queued mbuf (sack) has called us? We
8027 * return 0 and let the pacing guards
8028 * deal with it if they should or
8029 * should not cause a send.
8032 rack_log_to_processing(rack, cts, ret, 0);
8036 * Ok our timer went off early and we are not paced false
8037 * alarm, go back to sleep. We make sure we don't have
8038 * no-sack wakeup on since we no longer have a PKT_OUTPUT
8041 rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE;
8043 left = rack->r_ctl.rc_timer_exp - cts;
8044 tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left));
8045 rack_log_to_processing(rack, cts, ret, left);
8049 rack->rc_tmr_stopped = 0;
8050 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
8051 if (timers & PACE_TMR_DELACK) {
8052 ret = rack_timeout_delack(tp, rack, cts);
8053 } else if (timers & PACE_TMR_RACK) {
8054 rack->r_ctl.rc_tlp_rxt_last_time = cts;
8055 rack->r_fast_output = 0;
8056 ret = rack_timeout_rack(tp, rack, cts);
8057 } else if (timers & PACE_TMR_TLP) {
8058 rack->r_ctl.rc_tlp_rxt_last_time = cts;
8059 ret = rack_timeout_tlp(tp, rack, cts, doing_tlp);
8060 } else if (timers & PACE_TMR_RXT) {
8061 rack->r_ctl.rc_tlp_rxt_last_time = cts;
8062 rack->r_fast_output = 0;
8063 ret = rack_timeout_rxt(tp, rack, cts);
8064 } else if (timers & PACE_TMR_PERSIT) {
8065 ret = rack_timeout_persist(tp, rack, cts);
8066 } else if (timers & PACE_TMR_KEEP) {
8067 ret = rack_timeout_keepalive(tp, rack, cts);
8069 rack_log_to_processing(rack, cts, ret, timers);
8074 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
8077 uint32_t us_cts, flags_on_entry;
8078 uint8_t hpts_removed = 0;
8080 flags_on_entry = rack->r_ctl.rc_hpts_flags;
8081 us_cts = tcp_get_usecs(&tv);
8082 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
8083 ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
8084 ((tp->snd_max - tp->snd_una) == 0))) {
8085 tcp_hpts_remove(rack->rc_tp);
8087 /* If we were not delayed cancel out the flag. */
8088 if ((tp->snd_max - tp->snd_una) == 0)
8089 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8090 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8092 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
8093 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
8094 if (tcp_in_hpts(rack->rc_tp) &&
8095 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
8097 * Canceling timer's when we have no output being
8098 * paced. We also must remove ourselves from the
8101 tcp_hpts_remove(rack->rc_tp);
8104 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
8106 if (hpts_removed == 0)
8107 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
8111 rack_stopall(struct tcpcb *tp)
8113 struct tcp_rack *rack;
8114 rack = (struct tcp_rack *)tp->t_fb_ptr;
8115 rack->t_timers_stopped = 1;
8120 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack)
8123 * Assure no timers are running.
8125 if (tcp_timer_active(tp, TT_PERSIST)) {
8126 /* We enter in persists, set the flag appropriately */
8127 rack->rc_in_persist = 1;
8129 if (tcp_in_hpts(rack->rc_tp)) {
8130 tcp_hpts_remove(rack->rc_tp);
8135 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
8136 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag, int segsiz)
8141 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8143 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
8144 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
8145 rsm->r_flags |= RACK_OVERMAX;
8147 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
8148 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
8149 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
8151 idx = rsm->r_rtr_cnt - 1;
8152 rsm->r_tim_lastsent[idx] = ts;
8154 * Here we don't add in the len of send, since its already
8155 * in snduna <->snd_max.
8157 rsm->r_fas = ctf_flight_size(rack->rc_tp,
8158 rack->r_ctl.rc_sacked);
8159 if (rsm->r_flags & RACK_ACKED) {
8160 /* Problably MTU discovery messing with us */
8161 rsm->r_flags &= ~RACK_ACKED;
8162 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
8164 if (rsm->r_in_tmap) {
8165 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8168 /* Lets make sure it really is in or not the GP window */
8169 rack_mark_in_gp_win(tp, rsm);
8170 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8172 rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz);
8173 /* Take off the must retransmit flag, if its on */
8174 if (rsm->r_flags & RACK_MUST_RXT) {
8175 if (rack->r_must_retran)
8176 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
8177 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
8179 * We have retransmitted all we need. Clear
8180 * any must retransmit flags.
8182 rack->r_must_retran = 0;
8183 rack->r_ctl.rc_out_at_rto = 0;
8185 rsm->r_flags &= ~RACK_MUST_RXT;
8187 /* Remove any collapsed flag */
8188 rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8189 if (rsm->r_flags & RACK_SACK_PASSED) {
8190 /* We have retransmitted due to the SACK pass */
8191 rsm->r_flags &= ~RACK_SACK_PASSED;
8192 rsm->r_flags |= RACK_WAS_SACKPASS;
8197 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
8198 struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag, int segsiz)
8201 * We (re-)transmitted starting at rsm->r_start for some length
8202 * (possibly less than r_end.
8204 struct rack_sendmap *nrsm;
8205 int insret __diagused;
8210 c_end = rsm->r_start + len;
8211 if (SEQ_GEQ(c_end, rsm->r_end)) {
8213 * We retransmitted the whole piece or more than the whole
8214 * slopping into the next rsm.
8216 rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8217 if (c_end == rsm->r_end) {
8223 /* Hangs over the end return whats left */
8224 act_len = rsm->r_end - rsm->r_start;
8225 *lenp = (len - act_len);
8226 return (rsm->r_end);
8228 /* We don't get out of this block. */
8231 * Here we retransmitted less than the whole thing which means we
8232 * have to split this into what was transmitted and what was not.
8234 nrsm = rack_alloc_full_limit(rack);
8237 * We can't get memory, so lets not proceed.
8243 * So here we are going to take the original rsm and make it what we
8244 * retransmitted. nrsm will be the tail portion we did not
8245 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
8246 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
8247 * 1, 6 and the new piece will be 6, 11.
8249 rack_clone_rsm(rack, nrsm, rsm, c_end);
8251 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
8253 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8255 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8256 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8257 nrsm, insret, rack, rsm);
8260 if (rsm->r_in_tmap) {
8261 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8262 nrsm->r_in_tmap = 1;
8264 rsm->r_flags &= (~RACK_HAS_FIN);
8265 rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz);
8266 /* Log a split of rsm into rsm and nrsm */
8267 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8273 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
8274 uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts,
8275 struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb,
8276 uint32_t s_moff, int hw_tls, int segsiz)
8278 struct tcp_rack *rack;
8279 struct rack_sendmap *rsm, *nrsm;
8280 int insret __diagused;
8282 register uint32_t snd_max, snd_una;
8285 * Add to the RACK log of packets in flight or retransmitted. If
8286 * there is a TS option we will use the TS echoed, if not we will
8289 * Retransmissions will increment the count and move the ts to its
8290 * proper place. Note that if options do not include TS's then we
8291 * won't be able to effectively use the ACK for an RTT on a retran.
8293 * Notes about r_start and r_end. Lets consider a send starting at
8294 * sequence 1 for 10 bytes. In such an example the r_start would be
8295 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
8296 * This means that r_end is actually the first sequence for the next
8301 * If err is set what do we do XXXrrs? should we not add the thing?
8302 * -- i.e. return if err != 0 or should we pretend we sent it? --
8303 * i.e. proceed with add ** do this for now.
8305 INP_WLOCK_ASSERT(tptoinpcb(tp));
8308 * We don't log errors -- we could but snd_max does not
8309 * advance in this case either.
8313 if (th_flags & TH_RST) {
8315 * We don't log resets and we return immediately from
8320 rack = (struct tcp_rack *)tp->t_fb_ptr;
8321 snd_una = tp->snd_una;
8322 snd_max = tp->snd_max;
8323 if (th_flags & (TH_SYN | TH_FIN)) {
8325 * The call to rack_log_output is made before bumping
8326 * snd_max. This means we can record one extra byte on a SYN
8327 * or FIN if seq_out is adding more on and a FIN is present
8328 * (and we are not resending).
8330 if ((th_flags & TH_SYN) && (seq_out == tp->iss))
8332 if (th_flags & TH_FIN)
8334 if (SEQ_LT(snd_max, tp->snd_nxt)) {
8336 * The add/update as not been done for the FIN/SYN
8339 snd_max = tp->snd_nxt;
8342 if (SEQ_LEQ((seq_out + len), snd_una)) {
8343 /* Are sending an old segment to induce an ack (keep-alive)? */
8346 if (SEQ_LT(seq_out, snd_una)) {
8347 /* huh? should we panic? */
8350 end = seq_out + len;
8352 if (SEQ_GEQ(end, seq_out))
8353 len = end - seq_out;
8358 /* We don't log zero window probes */
8361 if (IN_FASTRECOVERY(tp->t_flags)) {
8362 rack->r_ctl.rc_prr_out += len;
8364 /* First question is it a retransmission or new? */
8365 if (seq_out == snd_max) {
8367 rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts);
8369 rsm = rack_alloc(rack);
8372 * Hmm out of memory and the tcb got destroyed while
8377 if (th_flags & TH_FIN) {
8378 rsm->r_flags = RACK_HAS_FIN|add_flag;
8380 rsm->r_flags = add_flag;
8384 rsm->r_tim_lastsent[0] = cts;
8386 rsm->r_rtr_bytes = 0;
8387 if (th_flags & TH_SYN) {
8388 /* The data space is one beyond snd_una */
8389 rsm->r_flags |= RACK_HAS_SYN;
8391 rsm->r_start = seq_out;
8392 rsm->r_end = rsm->r_start + len;
8393 rack_mark_in_gp_win(tp, rsm);
8396 * save off the mbuf location that
8397 * sndmbuf_noadv returned (which is
8398 * where we started copying from)..
8403 * Here we do add in the len of send, since its not yet
8404 * reflected in in snduna <->snd_max
8406 rsm->r_fas = (ctf_flight_size(rack->rc_tp,
8407 rack->r_ctl.rc_sacked) +
8408 (rsm->r_end - rsm->r_start));
8409 /* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */
8411 if (rsm->m->m_len <= rsm->soff) {
8413 * XXXrrs Question, will this happen?
8415 * If sbsndptr is set at the correct place
8416 * then s_moff should always be somewhere
8417 * within rsm->m. But if the sbsndptr was
8418 * off then that won't be true. If it occurs
8419 * we need to walkout to the correct location.
8424 while (lm->m_len <= rsm->soff) {
8425 rsm->soff -= lm->m_len;
8427 KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u",
8428 __func__, rack, s_moff, s_mb, rsm->soff));
8432 rsm->orig_m_len = rsm->m->m_len;
8433 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
8435 rsm->orig_m_len = 0;
8436 rsm->orig_t_space = 0;
8438 rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz);
8439 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
8441 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__);
8443 (void)tqhash_insert(rack->r_ctl.tqh, rsm);
8445 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
8446 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8447 nrsm, insret, rack, rsm);
8450 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
8453 * Special case detection, is there just a single
8454 * packet outstanding when we are not in recovery?
8456 * If this is true mark it so.
8458 if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
8459 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
8460 struct rack_sendmap *prsm;
8462 prsm = tqhash_prev(rack->r_ctl.tqh, rsm);
8464 prsm->r_one_out_nr = 1;
8469 * If we reach here its a retransmission and we need to find it.
8472 if (hintrsm && (hintrsm->r_start == seq_out)) {
8476 /* No hints sorry */
8479 if ((rsm) && (rsm->r_start == seq_out)) {
8480 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8487 /* Ok it was not the last pointer go through it the hard way. */
8489 rsm = tqhash_find(rack->r_ctl.tqh, seq_out);
8491 if (rsm->r_start == seq_out) {
8492 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz);
8499 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
8500 /* Transmitted within this piece */
8502 * Ok we must split off the front and then let the
8503 * update do the rest
8505 nrsm = rack_alloc_full_limit(rack);
8507 rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz);
8511 * copy rsm to nrsm and then trim the front of rsm
8512 * to not include this part.
8514 rack_clone_rsm(rack, nrsm, rsm, seq_out);
8515 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__);
8517 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
8519 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
8520 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
8521 nrsm, insret, rack, rsm);
8524 if (rsm->r_in_tmap) {
8525 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8526 nrsm->r_in_tmap = 1;
8528 rsm->r_flags &= (~RACK_HAS_FIN);
8529 seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz);
8537 * Hmm not found in map did they retransmit both old and on into the
8540 if (seq_out == tp->snd_max) {
8542 } else if (SEQ_LT(seq_out, tp->snd_max)) {
8544 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
8545 seq_out, len, tp->snd_una, tp->snd_max);
8546 printf("Starting Dump of all rack entries\n");
8547 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) {
8548 printf("rsm:%p start:%u end:%u\n",
8549 rsm, rsm->r_start, rsm->r_end);
8551 printf("Dump complete\n");
8552 panic("seq_out not found rack:%p tp:%p",
8558 * Hmm beyond sndmax? (only if we are using the new rtt-pack
8561 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
8562 seq_out, len, tp->snd_max, tp);
8568 * Record one of the RTT updates from an ack into
8569 * our sample structure.
8573 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
8574 int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
8576 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8577 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
8578 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
8580 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8581 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
8582 rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
8584 if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
8585 if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
8586 rack->r_ctl.rc_gp_lowrtt = us_rtt;
8587 if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
8588 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
8590 if ((confidence == 1) &&
8592 (rsm->r_just_ret) ||
8593 (rsm->r_one_out_nr &&
8594 len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
8596 * If the rsm had a just return
8597 * hit it then we can't trust the
8598 * rtt measurement for buffer deterimination
8599 * Note that a confidence of 2, indicates
8600 * SACK'd which overrides the r_just_ret or
8601 * the r_one_out_nr. If it was a CUM-ACK and
8602 * we had only two outstanding, but get an
8603 * ack for only 1. Then that also lowers our
8608 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
8609 (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
8610 if (rack->r_ctl.rack_rs.confidence == 0) {
8612 * We take anything with no current confidence
8615 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8616 rack->r_ctl.rack_rs.confidence = confidence;
8617 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8618 } else if (confidence != 0) {
8620 * Once we have a confident number,
8621 * we can update it with a smaller
8622 * value since this confident number
8623 * may include the DSACK time until
8624 * the next segment (the second one) arrived.
8626 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
8627 rack->r_ctl.rack_rs.confidence = confidence;
8628 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
8631 rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
8632 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
8633 rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
8634 rack->r_ctl.rack_rs.rs_rtt_cnt++;
8638 * Collect new round-trip time estimate
8639 * and update averages and current timeout.
8642 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
8647 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
8648 /* No valid sample */
8650 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
8651 /* We are to use the lowest RTT seen in a single ack */
8652 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
8653 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
8654 /* We are to use the highest RTT seen in a single ack */
8655 rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
8656 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
8657 /* We are to use the average RTT seen in a single ack */
8658 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
8659 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
8662 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
8668 if (rack->rc_gp_rtt_set == 0) {
8670 * With no RTT we have to accept
8671 * even one we are not confident of.
8673 rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
8674 rack->rc_gp_rtt_set = 1;
8675 } else if (rack->r_ctl.rack_rs.confidence) {
8676 /* update the running gp srtt */
8677 rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
8678 rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
8680 if (rack->r_ctl.rack_rs.confidence) {
8682 * record the low and high for highly buffered path computation,
8683 * we only do this if we are confident (not a retransmission).
8685 if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
8686 rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8688 if (rack->rc_highly_buffered == 0) {
8690 * Currently once we declare a path has
8691 * highly buffered there is no going
8692 * back, which may be a problem...
8694 if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
8695 rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
8696 rack->r_ctl.rc_highest_us_rtt,
8697 rack->r_ctl.rc_lowest_us_rtt,
8699 rack->rc_highly_buffered = 1;
8703 if ((rack->r_ctl.rack_rs.confidence) ||
8704 (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
8706 * If we are highly confident of it <or> it was
8707 * never retransmitted we accept it as the last us_rtt.
8709 rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8710 /* The lowest rtt can be set if its was not retransmited */
8711 if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
8712 rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8713 if (rack->r_ctl.rc_lowest_us_rtt == 0)
8714 rack->r_ctl.rc_lowest_us_rtt = 1;
8717 rack = (struct tcp_rack *)tp->t_fb_ptr;
8718 if (tp->t_srtt != 0) {
8720 * We keep a simple srtt in microseconds, like our rtt
8721 * measurement. We don't need to do any tricks with shifting
8722 * etc. Instead we just add in 1/8th of the new measurement
8723 * and subtract out 1/8 of the old srtt. We do the same with
8724 * the variance after finding the absolute value of the
8725 * difference between this sample and the current srtt.
8727 delta = tp->t_srtt - rtt;
8728 /* Take off 1/8th of the current sRTT */
8729 tp->t_srtt -= (tp->t_srtt >> 3);
8730 /* Add in 1/8th of the new RTT just measured */
8731 tp->t_srtt += (rtt >> 3);
8732 if (tp->t_srtt <= 0)
8734 /* Now lets make the absolute value of the variance */
8737 /* Subtract out 1/8th */
8738 tp->t_rttvar -= (tp->t_rttvar >> 3);
8739 /* Add in 1/8th of the new variance we just saw */
8740 tp->t_rttvar += (delta >> 3);
8741 if (tp->t_rttvar <= 0)
8745 * No rtt measurement yet - use the unsmoothed rtt. Set the
8746 * variance to half the rtt (so our first retransmit happens
8750 tp->t_rttvar = rtt >> 1;
8752 rack->rc_srtt_measure_made = 1;
8753 KMOD_TCPSTAT_INC(tcps_rttupdated);
8754 if (tp->t_rttupdated < UCHAR_MAX)
8757 if (rack_stats_gets_ms_rtt == 0) {
8758 /* Send in the microsecond rtt used for rxt timeout purposes */
8759 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
8760 } else if (rack_stats_gets_ms_rtt == 1) {
8761 /* Send in the millisecond rtt used for rxt timeout purposes */
8765 ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8766 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8767 } else if (rack_stats_gets_ms_rtt == 2) {
8768 /* Send in the millisecond rtt has close to the path RTT as we can get */
8772 ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC;
8773 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt));
8775 /* Send in the microsecond rtt has close to the path RTT as we can get */
8776 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8778 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt));
8781 * the retransmit should happen at rtt + 4 * rttvar. Because of the
8782 * way we do the smoothing, srtt and rttvar will each average +1/2
8783 * tick of bias. When we compute the retransmit timer, we want 1/2
8784 * tick of rounding and 1 extra tick because of +-1/2 tick
8785 * uncertainty in the firing of the timer. The bias will give us
8786 * exactly the 1.5 tick we need. But, because the bias is
8787 * statistical, we have to test that we don't drop below the minimum
8788 * feasible timer (which is 2 ticks).
8791 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8792 max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop);
8793 rack_log_rtt_sample(rack, rtt);
8794 tp->t_softerror = 0;
8799 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
8802 * Apply to filter the inbound us-rtt at us_cts.
8806 old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
8807 apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
8809 if (old_rtt > us_rtt) {
8810 /* We just hit a new lower rtt time */
8811 rack_log_rtt_shrinks(rack, us_cts, old_rtt,
8812 __LINE__, RACK_RTTS_NEWRTT);
8814 * Only count it if its lower than what we saw within our
8817 if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
8818 if (rack_probertt_lower_within &&
8819 rack->rc_gp_dyn_mul &&
8820 (rack->use_fixed_rate == 0) &&
8821 (rack->rc_always_pace)) {
8823 * We are seeing a new lower rtt very close
8824 * to the time that we would have entered probe-rtt.
8825 * This is probably due to the fact that a peer flow
8826 * has entered probe-rtt. Lets go in now too.
8830 val = rack_probertt_lower_within * rack_time_between_probertt;
8832 if ((rack->in_probe_rtt == 0) &&
8833 ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val))) {
8834 rack_enter_probertt(rack, us_cts);
8837 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
8843 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
8844 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
8848 uint32_t t, len_acked;
8850 if ((rsm->r_flags & RACK_ACKED) ||
8851 (rsm->r_flags & RACK_WAS_ACKED))
8854 if (rsm->r_no_rtt_allowed) {
8858 if (ack_type == CUM_ACKED) {
8859 if (SEQ_GT(th_ack, rsm->r_end)) {
8860 len_acked = rsm->r_end - rsm->r_start;
8863 len_acked = th_ack - rsm->r_start;
8867 len_acked = rsm->r_end - rsm->r_start;
8870 if (rsm->r_rtr_cnt == 1) {
8872 t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8875 if (!tp->t_rttlow || tp->t_rttlow > t)
8877 if (!rack->r_ctl.rc_rack_min_rtt ||
8878 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
8879 rack->r_ctl.rc_rack_min_rtt = t;
8880 if (rack->r_ctl.rc_rack_min_rtt == 0) {
8881 rack->r_ctl.rc_rack_min_rtt = 1;
8884 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]))
8885 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8887 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
8890 if (CC_ALGO(tp)->rttsample != NULL) {
8891 /* Kick the RTT to the CC */
8892 CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8894 rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
8895 if (ack_type == SACKED) {
8896 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1);
8897 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
8900 * We need to setup what our confidence
8903 * If the rsm was app limited and it is
8904 * less than a mss in length (the end
8905 * of the send) then we have a gap. If we
8906 * were app limited but say we were sending
8907 * multiple MSS's then we are more confident
8910 * When we are not app-limited then we see if
8911 * the rsm is being included in the current
8912 * measurement, we tell this by the app_limited_needs_set
8915 * Note that being cwnd blocked is not applimited
8916 * as well as the pacing delay between packets which
8917 * are sending only 1 or 2 MSS's also will show up
8918 * in the RTT. We probably need to examine this algorithm
8919 * a bit more and enhance it to account for the delay
8920 * between rsm's. We could do that by saving off the
8921 * pacing delay of each rsm (in an rsm) and then
8922 * factoring that in somehow though for now I am
8927 if (rsm->r_flags & RACK_APP_LIMITED) {
8928 if (all && (len_acked <= ctf_fixed_maxseg(tp)))
8932 } else if (rack->app_limited_needs_set == 0) {
8937 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2);
8938 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
8939 calc_conf, rsm, rsm->r_rtr_cnt);
8941 if ((rsm->r_flags & RACK_TLP) &&
8942 (!IN_FASTRECOVERY(tp->t_flags))) {
8943 /* Segment was a TLP and our retrans matched */
8944 if (rack->r_ctl.rc_tlp_cwnd_reduce) {
8945 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
8948 if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
8949 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
8950 (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
8951 /* New more recent rack_tmit_time */
8952 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
8953 if (rack->r_ctl.rc_rack_tmit_time == 0)
8954 rack->r_ctl.rc_rack_tmit_time = 1;
8955 rack->rc_rack_rtt = t;
8960 * We clear the soft/rxtshift since we got an ack.
8961 * There is no assurance we will call the commit() function
8962 * so we need to clear these to avoid incorrect handling.
8965 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
8966 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
8967 tp->t_softerror = 0;
8968 if (to && (to->to_flags & TOF_TS) &&
8969 (ack_type == CUM_ACKED) &&
8971 ((rsm->r_flags & RACK_OVERMAX) == 0)) {
8973 * Now which timestamp does it match? In this block the ACK
8974 * must be coming from a previous transmission.
8976 for (i = 0; i < rsm->r_rtr_cnt; i++) {
8977 if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) {
8978 t = cts - (uint32_t)rsm->r_tim_lastsent[i];
8981 if (CC_ALGO(tp)->rttsample != NULL) {
8983 * Kick the RTT to the CC, here
8984 * we lie a bit in that we know the
8985 * retransmission is correct even though
8986 * we retransmitted. This is because
8987 * we match the timestamps.
8989 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i]))
8990 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i];
8992 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i];
8993 CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas);
8995 if ((i + 1) < rsm->r_rtr_cnt) {
8997 * The peer ack'd from our previous
8998 * transmission. We have a spurious
8999 * retransmission and thus we dont
9000 * want to update our rack_rtt.
9002 * Hmm should there be a CC revert here?
9007 if (!tp->t_rttlow || tp->t_rttlow > t)
9009 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9010 rack->r_ctl.rc_rack_min_rtt = t;
9011 if (rack->r_ctl.rc_rack_min_rtt == 0) {
9012 rack->r_ctl.rc_rack_min_rtt = 1;
9015 if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9016 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9017 (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) {
9018 /* New more recent rack_tmit_time */
9019 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
9020 if (rack->r_ctl.rc_rack_tmit_time == 0)
9021 rack->r_ctl.rc_rack_tmit_time = 1;
9022 rack->rc_rack_rtt = t;
9024 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3);
9025 tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm,
9030 /* If we are logging log out the sendmap */
9031 if (tcp_bblogging_on(rack->rc_tp)) {
9032 for (i = 0; i < rsm->r_rtr_cnt; i++) {
9033 rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr);
9039 * Ok its a SACK block that we retransmitted. or a windows
9040 * machine without timestamps. We can tell nothing from the
9041 * time-stamp since its not there or the time the peer last
9042 * recieved a segment that moved forward its cum-ack point.
9045 i = rsm->r_rtr_cnt - 1;
9046 t = cts - (uint32_t)rsm->r_tim_lastsent[i];
9049 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9051 * We retransmitted and the ack came back in less
9052 * than the smallest rtt we have observed. We most
9053 * likely did an improper retransmit as outlined in
9054 * 6.2 Step 2 point 2 in the rack-draft so we
9055 * don't want to update our rack_rtt. We in
9056 * theory (in future) might want to think about reverting our
9057 * cwnd state but we won't for now.
9060 } else if (rack->r_ctl.rc_rack_min_rtt) {
9062 * We retransmitted it and the retransmit did the
9065 if (!rack->r_ctl.rc_rack_min_rtt ||
9066 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
9067 rack->r_ctl.rc_rack_min_rtt = t;
9068 if (rack->r_ctl.rc_rack_min_rtt == 0) {
9069 rack->r_ctl.rc_rack_min_rtt = 1;
9072 if ((rack->r_ctl.rc_rack_tmit_time == 0) ||
9073 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
9074 (uint32_t)rsm->r_tim_lastsent[i]))) {
9075 /* New more recent rack_tmit_time */
9076 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i];
9077 if (rack->r_ctl.rc_rack_tmit_time == 0)
9078 rack->r_ctl.rc_rack_tmit_time = 1;
9079 rack->rc_rack_rtt = t;
9088 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
9091 rack_log_sack_passed(struct tcpcb *tp,
9092 struct tcp_rack *rack, struct rack_sendmap *rsm)
9094 struct rack_sendmap *nrsm;
9097 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
9098 rack_head, r_tnext) {
9100 /* Skip original segment he is acked */
9103 if (nrsm->r_flags & RACK_ACKED) {
9105 * Skip ack'd segments, though we
9106 * should not see these, since tmap
9107 * should not have ack'd segments.
9111 if (nrsm->r_flags & RACK_RWND_COLLAPSED) {
9113 * If the peer dropped the rwnd on
9114 * these then we don't worry about them.
9118 if (nrsm->r_flags & RACK_SACK_PASSED) {
9120 * We found one that is already marked
9121 * passed, we have been here before and
9122 * so all others below this are marked.
9126 nrsm->r_flags |= RACK_SACK_PASSED;
9127 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
9132 rack_need_set_test(struct tcpcb *tp,
9133 struct tcp_rack *rack,
9134 struct rack_sendmap *rsm,
9139 struct rack_sendmap *s_rsm;
9141 if ((tp->t_flags & TF_GPUTINPROG) &&
9142 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9144 * We were app limited, and this ack
9145 * butts up or goes beyond the point where we want
9146 * to start our next measurement. We need
9147 * to record the new gput_ts as here and
9148 * possibly update the start sequence.
9152 if (rsm->r_rtr_cnt > 1) {
9154 * This is a retransmit, can we
9155 * really make any assessment at this
9156 * point? We are not really sure of
9157 * the timestamp, is it this or the
9158 * previous transmission?
9160 * Lets wait for something better that
9161 * is not retransmitted.
9167 rack->app_limited_needs_set = 0;
9168 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
9169 /* Do we start at a new end? */
9170 if ((use_which == RACK_USE_BEG) &&
9171 SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
9173 * When we get an ACK that just eats
9174 * up some of the rsm, we set RACK_USE_BEG
9175 * since whats at r_start (i.e. th_ack)
9176 * is left unacked and thats where the
9177 * measurement now starts.
9179 tp->gput_seq = rsm->r_start;
9181 if ((use_which == RACK_USE_END) &&
9182 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
9184 * We use the end when the cumack
9185 * is moving forward and completely
9186 * deleting the rsm passed so basically
9187 * r_end holds th_ack.
9189 * For SACK's we also want to use the end
9190 * since this piece just got sacked and
9191 * we want to target anything after that
9192 * in our measurement.
9194 tp->gput_seq = rsm->r_end;
9196 if (use_which == RACK_USE_END_OR_THACK) {
9198 * special case for ack moving forward,
9199 * not a sack, we need to move all the
9200 * way up to where this ack cum-ack moves
9203 if (SEQ_GT(th_ack, rsm->r_end))
9204 tp->gput_seq = th_ack;
9206 tp->gput_seq = rsm->r_end;
9208 if (SEQ_LT(tp->gput_seq, tp->snd_max))
9209 s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq);
9213 * Pick up the correct send time if we can the rsm passed in
9214 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other
9215 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will
9216 * find a different seq i.e. the next send up.
9218 * If that has not been sent, s_rsm will be NULL and we must
9219 * arrange it so this function will get called again by setting
9220 * app_limited_needs_set.
9223 rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0];
9225 /* If we hit here we have to have *not* sent tp->gput_seq */
9226 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0];
9227 /* Set it up so we will go through here again */
9228 rack->app_limited_needs_set = 1;
9230 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
9232 * We moved beyond this guy's range, re-calculate
9233 * the new end point.
9235 if (rack->rc_gp_filled == 0) {
9236 tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
9238 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
9242 * We are moving the goal post, we may be able to clear the
9243 * measure_saw_probe_rtt flag.
9245 if ((rack->in_probe_rtt == 0) &&
9246 (rack->measure_saw_probe_rtt) &&
9247 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
9248 rack->measure_saw_probe_rtt = 0;
9249 rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
9251 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9252 (uint64_t)rack->r_ctl.rc_gp_output_ts),
9254 if (rack->rc_gp_filled &&
9255 ((tp->gput_ack - tp->gput_seq) <
9256 max(rc_init_window(rack), (MIN_GP_WIN *
9257 ctf_fixed_maxseg(tp))))) {
9258 uint32_t ideal_amount;
9260 ideal_amount = rack_get_measure_window(tp, rack);
9261 if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) {
9263 * There is no sense of continuing this measurement
9264 * because its too small to gain us anything we
9265 * trust. Skip it and that way we can start a new
9266 * measurement quicker.
9268 tp->t_flags &= ~TF_GPUTINPROG;
9269 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
9271 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) |
9272 (uint64_t)rack->r_ctl.rc_gp_output_ts),
9273 6, __LINE__, NULL, 0);
9276 * Reset the window further out.
9278 tp->gput_ack = tp->gput_seq + ideal_amount;
9281 rack_tend_gp_marks(tp, rack);
9282 rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm);
9287 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm)
9289 if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) {
9290 /* Behind our TLP definition or right at */
9293 if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) {
9294 /* The start is beyond or right at our end of TLP definition */
9297 /* It has to be a sub-part of the original TLP recorded */
9304 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
9305 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts,
9307 int *moved_two, uint32_t segsiz)
9309 uint32_t start, end, changed = 0;
9310 struct rack_sendmap stack_map;
9311 struct rack_sendmap *rsm, *nrsm, *prev, *next;
9312 int insret __diagused;
9313 int32_t used_ref = 1;
9315 #ifdef TCP_SAD_DETECTION
9317 int first_time_through = 1;
9320 int can_use_hookery = 0;
9322 start = sack->start;
9326 #ifdef TCP_SAD_DETECTION
9328 * There are a strange number of proxys and meddle boxes in the world
9329 * that seem to cut up segments on different boundaries. This gets us
9330 * smaller sacks that are still ok in terms of it being an attacker.
9331 * We use the base segsiz to calculate an allowable smallness but
9332 * also enforce a min on the segsiz in case it is an attacker playing
9333 * games with MSS. So basically if the sack arrives and it is
9334 * larger than a worse case 960 bytes, we don't classify the guy
9337 allow_segsiz = max(segsiz, 1200) * sad_seg_size_per;
9338 allow_segsiz /= 1000;
9341 if ((rsm == NULL) ||
9342 (SEQ_LT(end, rsm->r_start)) ||
9343 (SEQ_GEQ(start, rsm->r_end)) ||
9344 (SEQ_LT(start, rsm->r_start))) {
9346 * We are not in the right spot,
9347 * find the correct spot in the tree.
9350 rsm = tqhash_find(rack->r_ctl.tqh, start);
9357 #ifdef TCP_SAD_DETECTION
9358 /* Now we must check for suspicous activity */
9359 if ((first_time_through == 1) &&
9360 ((end - start) < min((rsm->r_end - rsm->r_start), allow_segsiz)) &&
9361 ((rsm->r_flags & RACK_PMTU_CHG) == 0) &&
9362 ((rsm->r_flags & RACK_TLP) == 0)) {
9364 * Its less than a full MSS or the segment being acked
9365 * this should only happen if the rsm in question had the
9366 * r_just_ret flag set <and> the end matches the end of
9369 * Note we do not look at segments that have had TLP's on
9370 * them since we can get un-reported rwnd collapses that
9371 * basically we TLP on and then we get back a sack block
9372 * that goes from the start to only a small way.
9378 if (SEQ_GEQ(end, rsm->r_end)) {
9379 if (rsm->r_just_ret == 1) {
9380 /* This was at the end of a send which is ok */
9383 /* A bit harder was it the end of our segment */
9386 len = (rsm->r_end - rsm->r_start);
9387 segs = len / segsiz;
9389 if ((segs + (rsm->r_end - start)) == len) {
9391 * So this last bit was the
9392 * end of our send if we cut it
9393 * up into segsiz pieces so its ok.
9401 * This guy is doing something suspicious
9402 * lets start detection.
9404 if (rack->rc_suspicious == 0) {
9405 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_SUSPECT);
9406 counter_u64_add(rack_sack_attacks_suspect, 1);
9407 rack->rc_suspicious = 1;
9408 rack_log_sad(rack, 4);
9409 if (tcp_bblogging_on(rack->rc_tp)) {
9410 union tcp_log_stackspecific log;
9413 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
9414 log.u_bbr.flex1 = end;
9415 log.u_bbr.flex2 = start;
9416 log.u_bbr.flex3 = rsm->r_end;
9417 log.u_bbr.flex4 = rsm->r_start;
9418 log.u_bbr.flex5 = segsiz;
9419 log.u_bbr.flex6 = rsm->r_fas;
9420 log.u_bbr.flex7 = rsm->r_bas;
9421 log.u_bbr.flex8 = 5;
9422 log.u_bbr.pkts_out = rsm->r_flags;
9423 log.u_bbr.bbr_state = rack->rc_suspicious;
9424 log.u_bbr.bbr_substate = rsm->r_just_ret;
9425 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
9426 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
9427 TCP_LOG_EVENTP(rack->rc_tp, NULL,
9428 &rack->rc_inp->inp_socket->so_rcv,
9429 &rack->rc_inp->inp_socket->so_snd,
9430 TCP_SAD_DETECTION, 0,
9431 0, &log, false, &tv);
9434 /* You loose some ack count every time you sack
9435 * a small bit that is not butting to the end of
9436 * what we have sent. This is because we never
9437 * send small bits unless its the end of the sb.
9438 * Anyone sending a sack that is not at the end
9439 * is thus very very suspicious.
9441 loss = (segsiz/2) / (end - start);
9442 if (loss < rack->r_ctl.ack_count)
9443 rack->r_ctl.ack_count -= loss;
9445 rack->r_ctl.ack_count = 0;
9448 first_time_through = 0;
9450 /* Ok we have an ACK for some piece of this rsm */
9451 if (rsm->r_start != start) {
9452 if ((rsm->r_flags & RACK_ACKED) == 0) {
9454 * Before any splitting or hookery is
9455 * done is it a TLP of interest i.e. rxt?
9457 if ((rsm->r_flags & RACK_TLP) &&
9458 (rsm->r_rtr_cnt > 1)) {
9460 * We are splitting a rxt TLP, check
9461 * if we need to save off the start/end
9463 if (rack->rc_last_tlp_acked_set &&
9464 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9466 * We already turned this on since we are inside
9467 * the previous one was a partially sack now we
9468 * are getting another one (maybe all of it).
9471 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9473 * Lets make sure we have all of it though.
9475 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9476 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9477 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9478 rack->r_ctl.last_tlp_acked_end);
9480 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9481 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9482 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9483 rack->r_ctl.last_tlp_acked_end);
9486 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9487 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9488 rack->rc_last_tlp_past_cumack = 0;
9489 rack->rc_last_tlp_acked_set = 1;
9490 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9494 * Need to split this in two pieces the before and after,
9495 * the before remains in the map, the after must be
9496 * added. In other words we have:
9497 * rsm |--------------|
9501 * and nrsm will be the sacked piece
9504 * But before we start down that path lets
9505 * see if the sack spans over on top of
9506 * the next guy and it is already sacked.
9510 * Hookery can only be used if the two entries
9511 * are in the same bucket and neither one of
9512 * them staddle the bucket line.
9514 next = tqhash_next(rack->r_ctl.tqh, rsm);
9516 (rsm->bindex == next->bindex) &&
9517 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9518 ((next->r_flags & RACK_STRADDLE) == 0) &&
9519 (rsm->r_flags & RACK_IN_GP_WIN) &&
9520 (next->r_flags & RACK_IN_GP_WIN))
9521 can_use_hookery = 1;
9523 (rsm->bindex == next->bindex) &&
9524 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9525 ((next->r_flags & RACK_STRADDLE) == 0) &&
9526 ((rsm->r_flags & RACK_IN_GP_WIN) == 0) &&
9527 ((next->r_flags & RACK_IN_GP_WIN) == 0))
9528 can_use_hookery = 1;
9530 can_use_hookery = 0;
9531 if (next && can_use_hookery &&
9532 (next->r_flags & RACK_ACKED) &&
9533 SEQ_GEQ(end, next->r_start)) {
9535 * So the next one is already acked, and
9536 * we can thus by hookery use our stack_map
9537 * to reflect the piece being sacked and
9538 * then adjust the two tree entries moving
9539 * the start and ends around. So we start like:
9540 * rsm |------------| (not-acked)
9541 * next |-----------| (acked)
9542 * sackblk |-------->
9543 * We want to end like so:
9544 * rsm |------| (not-acked)
9545 * next |-----------------| (acked)
9547 * Where nrsm is a temporary stack piece we
9548 * use to update all the gizmos.
9550 /* Copy up our fudge block */
9553 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9554 /* Now adjust our tree blocks */
9556 next->r_start = start;
9557 rsm->r_flags |= RACK_SHUFFLED;
9558 next->r_flags |= RACK_SHUFFLED;
9559 /* Now we must adjust back where next->m is */
9560 rack_setup_offset_for_rsm(rack, rsm, next);
9562 * Which timestamp do we keep? It is rather
9563 * important in GP measurements to have the
9564 * accurate end of the send window.
9566 * We keep the largest value, which is the newest
9567 * send. We do this in case a segment that is
9568 * joined together and not part of a GP estimate
9569 * later gets expanded into the GP estimate.
9571 * We prohibit the merging of unlike kinds i.e.
9572 * all pieces that are in the GP estimate can be
9573 * merged and all pieces that are not in a GP estimate
9574 * can be merged, but not disimilar pieces. Combine
9575 * this with taking the highest here and we should
9576 * be ok unless of course the client reneges. Then
9579 if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] <
9580 nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)])
9581 next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)];
9583 * And we must keep the newest ack arrival time.
9585 if (next->r_ack_arrival <
9586 rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9587 next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9590 /* We don't need to adjust rsm, it did not change */
9591 /* Clear out the dup ack count of the remainder */
9593 rsm->r_just_ret = 0;
9594 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9595 /* Now lets make sure our fudge block is right */
9596 nrsm->r_start = start;
9597 /* Now lets update all the stats and such */
9598 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9599 if (rack->app_limited_needs_set)
9600 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9601 changed += (nrsm->r_end - nrsm->r_start);
9602 /* You get a count for acking a whole segment or more */
9603 if ((nrsm->r_end - nrsm->r_start) >= segsiz)
9604 rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
9605 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9606 if (nrsm->r_flags & RACK_SACK_PASSED) {
9607 rack->r_ctl.rc_reorder_ts = cts;
9608 if (rack->r_ctl.rc_reorder_ts == 0)
9609 rack->r_ctl.rc_reorder_ts = 1;
9612 * Now we want to go up from rsm (the
9613 * one left un-acked) to the next one
9614 * in the tmap. We do this so when
9615 * we walk backwards we include marking
9616 * sack-passed on rsm (The one passed in
9617 * is skipped since it is generally called
9618 * on something sacked before removing it
9621 if (rsm->r_in_tmap) {
9622 nrsm = TAILQ_NEXT(rsm, r_tnext);
9624 * Now that we have the next
9625 * one walk backwards from there.
9627 if (nrsm && nrsm->r_in_tmap)
9628 rack_log_sack_passed(tp, rack, nrsm);
9630 /* Now are we done? */
9631 if (SEQ_LT(end, next->r_end) ||
9632 (end == next->r_end)) {
9633 /* Done with block */
9636 rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__);
9637 counter_u64_add(rack_sack_used_next_merge, 1);
9638 /* Postion for the next block */
9639 start = next->r_end;
9640 rsm = tqhash_next(rack->r_ctl.tqh, next);
9645 * We can't use any hookery here, so we
9646 * need to split the map. We enter like
9650 * We will add the new block nrsm and
9651 * that will be the new portion, and then
9652 * fall through after reseting rsm. So we
9653 * split and look like this:
9657 * We then fall through reseting
9658 * rsm to nrsm, so the next block
9661 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9664 * failed XXXrrs what can we do but loose the sack
9669 counter_u64_add(rack_sack_splits, 1);
9670 rack_clone_rsm(rack, nrsm, rsm, start);
9672 rsm->r_just_ret = 0;
9674 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
9676 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
9677 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
9678 nrsm, insret, rack, rsm);
9681 if (rsm->r_in_tmap) {
9682 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
9683 nrsm->r_in_tmap = 1;
9685 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__);
9686 rsm->r_flags &= (~RACK_HAS_FIN);
9687 /* Position us to point to the new nrsm that starts the sack blk */
9691 /* Already sacked this piece */
9692 counter_u64_add(rack_sack_skipped_acked, 1);
9694 if (end == rsm->r_end) {
9695 /* Done with block */
9696 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9698 } else if (SEQ_LT(end, rsm->r_end)) {
9699 /* A partial sack to a already sacked block */
9701 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9705 * The end goes beyond this guy
9706 * reposition the start to the
9710 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
9716 if (SEQ_GEQ(end, rsm->r_end)) {
9718 * The end of this block is either beyond this guy or right
9719 * at this guy. I.e.:
9725 if ((rsm->r_flags & RACK_ACKED) == 0) {
9727 * Is it a TLP of interest?
9729 if ((rsm->r_flags & RACK_TLP) &&
9730 (rsm->r_rtr_cnt > 1)) {
9732 * We are splitting a rxt TLP, check
9733 * if we need to save off the start/end
9735 if (rack->rc_last_tlp_acked_set &&
9736 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9738 * We already turned this on since we are inside
9739 * the previous one was a partially sack now we
9740 * are getting another one (maybe all of it).
9742 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9744 * Lets make sure we have all of it though.
9746 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9747 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9748 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9749 rack->r_ctl.last_tlp_acked_end);
9751 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9752 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9753 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9754 rack->r_ctl.last_tlp_acked_end);
9757 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9758 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9759 rack->rc_last_tlp_past_cumack = 0;
9760 rack->rc_last_tlp_acked_set = 1;
9761 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9764 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
9765 changed += (rsm->r_end - rsm->r_start);
9766 /* You get a count for acking a whole segment or more */
9767 if ((rsm->r_end - rsm->r_start) >= segsiz)
9768 rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
9769 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
9770 if (rsm->r_in_tmap) /* should be true */
9771 rack_log_sack_passed(tp, rack, rsm);
9772 /* Is Reordering occuring? */
9773 if (rsm->r_flags & RACK_SACK_PASSED) {
9774 rsm->r_flags &= ~RACK_SACK_PASSED;
9775 rack->r_ctl.rc_reorder_ts = cts;
9776 if (rack->r_ctl.rc_reorder_ts == 0)
9777 rack->r_ctl.rc_reorder_ts = 1;
9779 if (rack->app_limited_needs_set)
9780 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
9781 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9782 rsm->r_flags |= RACK_ACKED;
9783 if (rsm->r_in_tmap) {
9784 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
9787 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__);
9789 counter_u64_add(rack_sack_skipped_acked, 1);
9792 if (end == rsm->r_end) {
9793 /* This block only - done, setup for next */
9797 * There is more not coverend by this rsm move on
9798 * to the next block in the RB tree.
9800 nrsm = tqhash_next(rack->r_ctl.tqh, rsm);
9808 * The end of this sack block is smaller than
9813 if ((rsm->r_flags & RACK_ACKED) == 0) {
9815 * Is it a TLP of interest?
9817 if ((rsm->r_flags & RACK_TLP) &&
9818 (rsm->r_rtr_cnt > 1)) {
9820 * We are splitting a rxt TLP, check
9821 * if we need to save off the start/end
9823 if (rack->rc_last_tlp_acked_set &&
9824 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9826 * We already turned this on since we are inside
9827 * the previous one was a partially sack now we
9828 * are getting another one (maybe all of it).
9830 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9832 * Lets make sure we have all of it though.
9834 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9835 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9836 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9837 rack->r_ctl.last_tlp_acked_end);
9839 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
9840 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9841 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9842 rack->r_ctl.last_tlp_acked_end);
9845 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9846 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
9847 rack->rc_last_tlp_past_cumack = 0;
9848 rack->rc_last_tlp_acked_set = 1;
9849 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
9853 * Hookery can only be used if the two entries
9854 * are in the same bucket and neither one of
9855 * them staddle the bucket line.
9857 prev = tqhash_prev(rack->r_ctl.tqh, rsm);
9859 (rsm->bindex == prev->bindex) &&
9860 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9861 ((prev->r_flags & RACK_STRADDLE) == 0) &&
9862 (rsm->r_flags & RACK_IN_GP_WIN) &&
9863 (prev->r_flags & RACK_IN_GP_WIN))
9864 can_use_hookery = 1;
9866 (rsm->bindex == prev->bindex) &&
9867 ((rsm->r_flags & RACK_STRADDLE) == 0) &&
9868 ((prev->r_flags & RACK_STRADDLE) == 0) &&
9869 ((rsm->r_flags & RACK_IN_GP_WIN) == 0) &&
9870 ((prev->r_flags & RACK_IN_GP_WIN) == 0))
9871 can_use_hookery = 1;
9873 can_use_hookery = 0;
9875 if (prev && can_use_hookery &&
9876 (prev->r_flags & RACK_ACKED)) {
9878 * Goal, we want the right remainder of rsm to shrink
9879 * in place and span from (rsm->r_start = end) to rsm->r_end.
9880 * We want to expand prev to go all the way
9881 * to prev->r_end <- end.
9882 * so in the tree we have before:
9883 * prev |--------| (acked)
9884 * rsm |-------| (non-acked)
9886 * We churn it so we end up with
9887 * prev |----------| (acked)
9888 * rsm |-----| (non-acked)
9889 * nrsm |-| (temporary)
9891 * Note if either prev/rsm is a TLP we don't
9896 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
9899 rsm->r_flags |= RACK_SHUFFLED;
9900 prev->r_flags |= RACK_SHUFFLED;
9901 /* Now adjust nrsm (stack copy) to be
9902 * the one that is the small
9903 * piece that was "sacked".
9908 * Which timestamp do we keep? It is rather
9909 * important in GP measurements to have the
9910 * accurate end of the send window.
9912 * We keep the largest value, which is the newest
9913 * send. We do this in case a segment that is
9914 * joined together and not part of a GP estimate
9915 * later gets expanded into the GP estimate.
9917 * We prohibit the merging of unlike kinds i.e.
9918 * all pieces that are in the GP estimate can be
9919 * merged and all pieces that are not in a GP estimate
9920 * can be merged, but not disimilar pieces. Combine
9921 * this with taking the highest here and we should
9922 * be ok unless of course the client reneges. Then
9925 if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] <
9926 nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) {
9927 prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)];
9930 * And we must keep the newest ack arrival time.
9933 if(prev->r_ack_arrival <
9934 rack_to_usec_ts(&rack->r_ctl.act_rcv_time))
9935 prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
9937 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
9939 * Now that the rsm has had its start moved forward
9940 * lets go ahead and get its new place in the world.
9942 rack_setup_offset_for_rsm(rack, prev, rsm);
9944 * Now nrsm is our new little piece
9945 * that is acked (which was merged
9946 * to prev). Update the rtt and changed
9947 * based on that. Also check for reordering.
9949 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
9950 if (rack->app_limited_needs_set)
9951 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
9952 changed += (nrsm->r_end - nrsm->r_start);
9953 /* You get a count for acking a whole segment or more */
9954 if ((nrsm->r_end - nrsm->r_start) >= segsiz)
9955 rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz);
9957 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
9958 if (nrsm->r_flags & RACK_SACK_PASSED) {
9959 rack->r_ctl.rc_reorder_ts = cts;
9960 if (rack->r_ctl.rc_reorder_ts == 0)
9961 rack->r_ctl.rc_reorder_ts = 1;
9963 rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__);
9965 counter_u64_add(rack_sack_used_prev_merge, 1);
9968 * This is the case where our previous
9969 * block is not acked either, so we must
9970 * split the block in two.
9972 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
9974 /* failed rrs what can we do but loose the sack info? */
9977 if ((rsm->r_flags & RACK_TLP) &&
9978 (rsm->r_rtr_cnt > 1)) {
9980 * We are splitting a rxt TLP, check
9981 * if we need to save off the start/end
9983 if (rack->rc_last_tlp_acked_set &&
9984 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
9986 * We already turned this on since this block is inside
9987 * the previous one was a partially sack now we
9988 * are getting another one (maybe all of it).
9990 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
9992 * Lets make sure we have all of it though.
9994 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
9995 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
9996 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
9997 rack->r_ctl.last_tlp_acked_end);
9999 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10000 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10001 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10002 rack->r_ctl.last_tlp_acked_end);
10005 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10006 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10007 rack->rc_last_tlp_acked_set = 1;
10008 rack->rc_last_tlp_past_cumack = 0;
10009 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10013 * In this case nrsm becomes
10014 * nrsm->r_start = end;
10015 * nrsm->r_end = rsm->r_end;
10016 * which is un-acked.
10018 * rsm->r_end = nrsm->r_start;
10019 * i.e. the remaining un-acked
10020 * piece is left on the left
10023 * So we start like this
10024 * rsm |----------| (not acked)
10026 * build it so we have
10027 * rsm |---| (acked)
10028 * nrsm |------| (not acked)
10030 counter_u64_add(rack_sack_splits, 1);
10031 rack_clone_rsm(rack, nrsm, rsm, end);
10033 rsm->r_flags &= (~RACK_HAS_FIN);
10034 rsm->r_just_ret = 0;
10036 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
10038 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
10039 panic("Insert in rb tree of %p fails ret:% rack:%p rsm:%p",
10040 nrsm, insret, rack, rsm);
10043 if (rsm->r_in_tmap) {
10044 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
10045 nrsm->r_in_tmap = 1;
10047 nrsm->r_dupack = 0;
10048 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
10049 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
10050 changed += (rsm->r_end - rsm->r_start);
10051 /* You get a count for acking a whole segment or more */
10052 if ((rsm->r_end - rsm->r_start) >= segsiz)
10053 rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz);
10055 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
10056 if (rsm->r_in_tmap) /* should be true */
10057 rack_log_sack_passed(tp, rack, rsm);
10058 /* Is Reordering occuring? */
10059 if (rsm->r_flags & RACK_SACK_PASSED) {
10060 rsm->r_flags &= ~RACK_SACK_PASSED;
10061 rack->r_ctl.rc_reorder_ts = cts;
10062 if (rack->r_ctl.rc_reorder_ts == 0)
10063 rack->r_ctl.rc_reorder_ts = 1;
10065 if (rack->app_limited_needs_set)
10066 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
10067 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10068 rsm->r_flags |= RACK_ACKED;
10069 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__);
10070 if (rsm->r_in_tmap) {
10071 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10072 rsm->r_in_tmap = 0;
10075 } else if (start != end){
10077 * The block was already acked.
10079 counter_u64_add(rack_sack_skipped_acked, 1);
10084 ((rsm->r_flags & RACK_TLP) == 0) &&
10085 (rsm->r_flags & RACK_ACKED)) {
10087 * Now can we merge where we worked
10088 * with either the previous or
10091 next = tqhash_next(rack->r_ctl.tqh, rsm);
10093 if (next->r_flags & RACK_TLP)
10095 /* Only allow merges between ones in or out of GP window */
10096 if ((next->r_flags & RACK_IN_GP_WIN) &&
10097 ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10100 if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10101 ((next->r_flags & RACK_IN_GP_WIN) == 0)) {
10104 if (rsm->bindex != next->bindex)
10106 if (rsm->r_flags & RACK_STRADDLE)
10108 if (next->r_flags & RACK_STRADDLE)
10110 if (next->r_flags & RACK_ACKED) {
10111 /* yep this and next can be merged */
10112 rsm = rack_merge_rsm(rack, rsm, next);
10114 next = tqhash_next(rack->r_ctl.tqh, rsm);
10118 /* Now what about the previous? */
10119 prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10121 if (prev->r_flags & RACK_TLP)
10123 /* Only allow merges between ones in or out of GP window */
10124 if ((prev->r_flags & RACK_IN_GP_WIN) &&
10125 ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) {
10128 if ((rsm->r_flags & RACK_IN_GP_WIN) &&
10129 ((prev->r_flags & RACK_IN_GP_WIN) == 0)) {
10132 if (rsm->bindex != prev->bindex)
10134 if (rsm->r_flags & RACK_STRADDLE)
10136 if (prev->r_flags & RACK_STRADDLE)
10138 if (prev->r_flags & RACK_ACKED) {
10139 /* yep the previous and this can be merged */
10140 rsm = rack_merge_rsm(rack, prev, rsm);
10142 prev = tqhash_prev(rack->r_ctl.tqh, rsm);
10147 if (used_ref == 0) {
10148 counter_u64_add(rack_sack_proc_all, 1);
10150 counter_u64_add(rack_sack_proc_short, 1);
10152 /* Save off the next one for quick reference. */
10153 nrsm = tqhash_find(rack->r_ctl.tqh, end);
10154 *prsm = rack->r_ctl.rc_sacklast = nrsm;
10155 /* Pass back the moved. */
10156 *moved_two = moved;
10157 *no_extra = noextra;
10162 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
10164 struct rack_sendmap *tmap;
10167 while (rsm && (rsm->r_flags & RACK_ACKED)) {
10168 /* Its no longer sacked, mark it so */
10169 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10171 if (rsm->r_in_tmap) {
10172 panic("rack:%p rsm:%p flags:0x%x in tmap?",
10173 rack, rsm, rsm->r_flags);
10176 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
10177 /* Rebuild it into our tmap */
10178 if (tmap == NULL) {
10179 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10182 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
10185 tmap->r_in_tmap = 1;
10186 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
10189 * Now lets possibly clear the sack filter so we start
10190 * recognizing sacks that cover this area.
10192 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
10197 rack_do_decay(struct tcp_rack *rack)
10199 struct timeval res;
10201 #define timersub(tvp, uvp, vvp) \
10203 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
10204 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
10205 if ((vvp)->tv_usec < 0) { \
10207 (vvp)->tv_usec += 1000000; \
10211 timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
10214 rack->r_ctl.input_pkt++;
10215 if ((rack->rc_in_persist) ||
10216 (res.tv_sec >= 1) ||
10217 (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
10219 * Check for decay of non-SAD,
10220 * we want all SAD detection metrics to
10221 * decay 1/4 per second (or more) passed.
10222 * Current default is 800 so it decays
10223 * 80% every second.
10225 #ifdef TCP_SAD_DETECTION
10226 uint32_t pkt_delta;
10228 pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
10230 /* Update our saved tracking values */
10231 rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
10232 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
10233 /* Now do we escape without decay? */
10234 #ifdef TCP_SAD_DETECTION
10235 if (rack->rc_in_persist ||
10236 (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
10237 (pkt_delta < tcp_sad_low_pps)){
10239 * We don't decay idle connections
10240 * or ones that have a low input pps.
10244 /* Decay the counters */
10245 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
10246 tcp_sad_decay_val);
10247 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
10248 tcp_sad_decay_val);
10249 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
10250 tcp_sad_decay_val);
10251 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
10252 tcp_sad_decay_val);
10258 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from)
10261 * We look at advancing the end send time for our GP
10262 * measurement tracking only as the cumulative acknowledgment
10263 * moves forward. You might wonder about this, why not
10264 * at every transmission or retransmission within the
10265 * GP window update the rc_gp_cumack_ts? Well its rather
10266 * nuanced but basically the GP window *may* expand (as
10267 * it does below) or worse and harder to track it may shrink.
10269 * This last makes it impossible to track at the time of
10270 * the send, since you may set forward your rc_gp_cumack_ts
10271 * when you send, because that send *is* in your currently
10272 * "guessed" window, but then it shrinks. Now which was
10273 * the send time of the last bytes in the window, by the
10274 * time you ask that question that part of the sendmap
10275 * is freed. So you don't know and you will have too
10276 * long of send window. Instead by updating the time
10277 * marker only when the cumack advances this assures us
10278 * that we will have only the sends in the window of our
10281 * Another complication from this is the
10282 * merging of sendmap entries. During SACK processing this
10283 * can happen to conserve the sendmap size. That breaks
10284 * everything down in tracking the send window of the GP
10285 * estimate. So to prevent that and keep it working with
10286 * a tiny bit more limited merging, we only allow like
10287 * types to be merged. I.e. if two sends are in the GP window
10288 * then its ok to merge them together. If two sends are not
10289 * in the GP window its ok to merge them together too. Though
10290 * one send in and one send out cannot be merged. We combine
10291 * this with never allowing the shrinking of the GP window when
10292 * we are in recovery so that we can properly calculate the
10295 * This all of course seems complicated, because it is.. :)
10297 * The cum-ack is being advanced upon the sendmap.
10298 * If we are not doing a GP estimate don't
10303 if ((tp->t_flags & TF_GPUTINPROG) == 0)
10306 * If this sendmap entry is going
10307 * beyond the measurement window we had picked,
10308 * expand the measurement window by that much.
10310 if (SEQ_GT(rsm->r_end, tp->gput_ack)) {
10311 tp->gput_ack = rsm->r_end;
10314 * If we have not setup a ack, then we
10315 * have no idea if the newly acked pieces
10316 * will be "in our seq measurement range". If
10317 * it is when we clear the app_limited_needs_set
10318 * flag the timestamp will be updated.
10320 if (rack->app_limited_needs_set)
10323 * Finally, we grab out the latest timestamp
10324 * that this packet was sent and then see
10326 * a) The packet touches are newly defined GP range.
10327 * b) The time is greater than (newer) than the
10328 * one we currently have. If so we update
10329 * our sending end time window.
10331 * Note we *do not* do this at send time. The reason
10332 * is that if you do you *may* pick up a newer timestamp
10333 * for a range you are not going to measure. We project
10334 * out how far and then sometimes modify that to be
10335 * smaller. If that occurs then you will have a send
10336 * that does not belong to the range included.
10338 if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <=
10339 rack->r_ctl.rc_gp_cumack_ts)
10341 if (rack_in_gp_window(tp, rsm)) {
10342 rack->r_ctl.rc_gp_cumack_ts = ts;
10343 rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end,
10344 __LINE__, from, rsm);
10349 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to, uint64_t acktime)
10351 struct rack_sendmap *rsm;
10353 * The ACK point is advancing to th_ack, we must drop off
10354 * the packets in the rack log and calculate any eligble
10358 rack->r_wanted_output = 1;
10359 if (SEQ_GT(th_ack, tp->snd_una))
10360 rack->r_ctl.last_cumack_advance = acktime;
10362 /* Tend any TLP that has been marked for 1/2 the seq space (its old) */
10363 if ((rack->rc_last_tlp_acked_set == 1)&&
10364 (rack->rc_last_tlp_past_cumack == 1) &&
10365 (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) {
10367 * We have reached the point where our last rack
10368 * tlp retransmit sequence is ahead of the cum-ack.
10369 * This can only happen when the cum-ack moves all
10370 * the way around (its been a full 2^^31+1 bytes
10371 * or more since we sent a retransmitted TLP). Lets
10372 * turn off the valid flag since its not really valid.
10374 * Note since sack's also turn on this event we have
10375 * a complication, we have to wait to age it out until
10376 * the cum-ack is by the TLP before checking which is
10377 * what the next else clause does.
10379 rack_log_dsack_event(rack, 9, __LINE__,
10380 rack->r_ctl.last_tlp_acked_start,
10381 rack->r_ctl.last_tlp_acked_end);
10382 rack->rc_last_tlp_acked_set = 0;
10383 rack->rc_last_tlp_past_cumack = 0;
10384 } else if ((rack->rc_last_tlp_acked_set == 1) &&
10385 (rack->rc_last_tlp_past_cumack == 0) &&
10386 (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) {
10388 * It is safe to start aging TLP's out.
10390 rack->rc_last_tlp_past_cumack = 1;
10392 /* We do the same for the tlp send seq as well */
10393 if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10394 (rack->rc_last_sent_tlp_past_cumack == 1) &&
10395 (SEQ_GT(rack->r_ctl.last_sent_tlp_seq, th_ack))) {
10396 rack_log_dsack_event(rack, 9, __LINE__,
10397 rack->r_ctl.last_sent_tlp_seq,
10398 (rack->r_ctl.last_sent_tlp_seq +
10399 rack->r_ctl.last_sent_tlp_len));
10400 rack->rc_last_sent_tlp_seq_valid = 0;
10401 rack->rc_last_sent_tlp_past_cumack = 0;
10402 } else if ((rack->rc_last_sent_tlp_seq_valid == 1) &&
10403 (rack->rc_last_sent_tlp_past_cumack == 0) &&
10404 (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) {
10406 * It is safe to start aging TLP's send.
10408 rack->rc_last_sent_tlp_past_cumack = 1;
10411 rsm = tqhash_min(rack->r_ctl.tqh);
10413 if ((th_ack - 1) == tp->iss) {
10415 * For the SYN incoming case we will not
10416 * have called tcp_output for the sending of
10417 * the SYN, so there will be no map. All
10418 * other cases should probably be a panic.
10422 if (tp->t_flags & TF_SENTFIN) {
10423 /* if we sent a FIN we often will not have map */
10427 panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n",
10429 tp->t_state, th_ack, rack,
10430 tp->snd_una, tp->snd_max, tp->snd_nxt);
10434 if (SEQ_LT(th_ack, rsm->r_start)) {
10435 /* Huh map is missing this */
10437 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
10439 th_ack, tp->t_state, rack->r_state);
10443 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
10445 /* Now was it a retransmitted TLP? */
10446 if ((rsm->r_flags & RACK_TLP) &&
10447 (rsm->r_rtr_cnt > 1)) {
10449 * Yes, this rsm was a TLP and retransmitted, remember that
10450 * since if a DSACK comes back on this we don't want
10451 * to think of it as a reordered segment. This may
10452 * get updated again with possibly even other TLPs
10453 * in flight, but thats ok. Only when we don't send
10454 * a retransmitted TLP for 1/2 the sequences space
10455 * will it get turned off (above).
10457 if (rack->rc_last_tlp_acked_set &&
10458 (is_rsm_inside_declared_tlp_block(rack, rsm))) {
10460 * We already turned this on since the end matches,
10461 * the previous one was a partially ack now we
10462 * are getting another one (maybe all of it).
10464 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end);
10466 * Lets make sure we have all of it though.
10468 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) {
10469 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10470 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10471 rack->r_ctl.last_tlp_acked_end);
10473 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) {
10474 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10475 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start,
10476 rack->r_ctl.last_tlp_acked_end);
10479 rack->rc_last_tlp_past_cumack = 1;
10480 rack->r_ctl.last_tlp_acked_start = rsm->r_start;
10481 rack->r_ctl.last_tlp_acked_end = rsm->r_end;
10482 rack->rc_last_tlp_acked_set = 1;
10483 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end);
10486 /* Now do we consume the whole thing? */
10487 if (SEQ_GEQ(th_ack, rsm->r_end)) {
10488 /* Its all consumed. */
10490 uint8_t newly_acked;
10492 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__);
10493 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
10494 rsm->r_rtr_bytes = 0;
10496 * Record the time of highest cumack sent if its in our measurement
10497 * window and possibly bump out the end.
10499 rack_rsm_sender_update(rack, tp, rsm, 4);
10500 tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
10501 if (rsm->r_in_tmap) {
10502 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10503 rsm->r_in_tmap = 0;
10506 if (rsm->r_flags & RACK_ACKED) {
10508 * It was acked on the scoreboard -- remove
10511 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
10513 } else if (rsm->r_flags & RACK_SACK_PASSED) {
10515 * There are segments ACKED on the
10516 * scoreboard further up. We are seeing
10519 rsm->r_flags &= ~RACK_SACK_PASSED;
10520 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
10521 rsm->r_flags |= RACK_ACKED;
10522 rack->r_ctl.rc_reorder_ts = cts;
10523 if (rack->r_ctl.rc_reorder_ts == 0)
10524 rack->r_ctl.rc_reorder_ts = 1;
10525 if (rack->r_ent_rec_ns) {
10527 * We have sent no more, and we saw an sack
10530 rack->r_might_revert = 1;
10533 if ((rsm->r_flags & RACK_TO_REXT) &&
10534 (tp->t_flags & TF_RCVD_TSTMP) &&
10535 (to->to_flags & TOF_TS) &&
10536 (to->to_tsecr != 0) &&
10537 (tp->t_flags & TF_PREVVALID)) {
10539 * We can use the timestamp to see
10540 * if this retransmission was from the
10541 * first transmit. If so we made a mistake.
10543 tp->t_flags &= ~TF_PREVVALID;
10544 if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) {
10545 /* The first transmit is what this ack is for */
10546 rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__);
10549 left = th_ack - rsm->r_end;
10550 if (rack->app_limited_needs_set && newly_acked)
10551 rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
10552 /* Free back to zone */
10553 rack_free(rack, rsm);
10557 /* Check for reneging */
10558 rsm = tqhash_min(rack->r_ctl.tqh);
10559 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
10561 * The peer has moved snd_una up to
10562 * the edge of this send, i.e. one
10563 * that it had previously acked. The only
10564 * way that can be true if the peer threw
10565 * away data (space issues) that it had
10566 * previously sacked (else it would have
10567 * given us snd_una up to (rsm->r_end).
10568 * We need to undo the acked markings here.
10570 * Note we have to look to make sure th_ack is
10571 * our rsm->r_start in case we get an old ack
10572 * where th_ack is behind snd_una.
10574 rack_peer_reneges(rack, rsm, th_ack);
10578 if (rsm->r_flags & RACK_ACKED) {
10580 * It was acked on the scoreboard -- remove it from
10581 * total for the part being cum-acked.
10583 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
10586 * Clear the dup ack count for
10587 * the piece that remains.
10590 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
10591 if (rsm->r_rtr_bytes) {
10593 * It was retransmitted adjust the
10594 * sack holes for what was acked.
10598 ack_am = (th_ack - rsm->r_start);
10599 if (ack_am >= rsm->r_rtr_bytes) {
10600 rack->r_ctl.rc_holes_rxt -= ack_am;
10601 rsm->r_rtr_bytes -= ack_am;
10605 * Update where the piece starts and record
10606 * the time of send of highest cumack sent if
10607 * its in our GP range.
10609 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__);
10610 /* Now we need to move our offset forward too */
10612 ((rsm->orig_m_len != rsm->m->m_len) ||
10613 (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
10614 /* Fix up the orig_m_len and possibly the mbuf offset */
10615 rack_adjust_orig_mlen(rsm);
10617 rsm->soff += (th_ack - rsm->r_start);
10618 rack_rsm_sender_update(rack, tp, rsm, 5);
10619 /* The trim will move th_ack into r_start for us */
10620 tqhash_trim(rack->r_ctl.tqh, th_ack);
10621 /* Now do we need to move the mbuf fwd too? */
10623 while (rsm->soff >= rsm->m->m_len) {
10624 rsm->soff -= rsm->m->m_len;
10625 rsm->m = rsm->m->m_next;
10626 KASSERT((rsm->m != NULL),
10627 (" nrsm:%p hit at soff:%u null m",
10630 rsm->orig_m_len = rsm->m->m_len;
10631 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
10633 if (rack->app_limited_needs_set &&
10634 SEQ_GEQ(th_ack, tp->gput_seq))
10635 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
10639 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack)
10641 struct rack_sendmap *rsm;
10642 int sack_pass_fnd = 0;
10644 if (rack->r_might_revert) {
10646 * Ok we have reordering, have not sent anything, we
10647 * might want to revert the congestion state if nothing
10648 * further has SACK_PASSED on it. Lets check.
10650 * We also get here when we have DSACKs come in for
10651 * all the data that we FR'd. Note that a rxt or tlp
10652 * timer clears this from happening.
10655 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
10656 if (rsm->r_flags & RACK_SACK_PASSED) {
10661 if (sack_pass_fnd == 0) {
10663 * We went into recovery
10664 * incorrectly due to reordering!
10668 rack->r_ent_rec_ns = 0;
10669 orig_cwnd = tp->snd_cwnd;
10670 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec;
10671 tp->snd_recover = tp->snd_una;
10672 rack_log_to_prr(rack, 14, orig_cwnd, __LINE__);
10673 EXIT_RECOVERY(tp->t_flags);
10675 rack->r_might_revert = 0;
10679 #ifdef TCP_SAD_DETECTION
10682 rack_merge_out_sacks(struct tcp_rack *rack)
10684 struct rack_sendmap *cur, *next, *rsm, *trsm = NULL;
10686 cur = tqhash_min(rack->r_ctl.tqh);
10688 next = tqhash_next(rack->r_ctl.tqh, cur);
10690 * The idea is to go through all and merge back
10691 * together the pieces sent together,
10693 if ((next != NULL) &&
10694 (cur->r_tim_lastsent[0] == next->r_tim_lastsent[0])) {
10695 rack_merge_rsm(rack, cur, next);
10701 * now treat it like a rxt event, everything is outstanding
10702 * and sent nothing acvked and dupacks are all zero. If this
10703 * is not an attacker it will have to dupack its way through
10706 TAILQ_INIT(&rack->r_ctl.rc_tmap);
10707 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) {
10709 /* We must re-add it back to the tlist */
10710 if (trsm == NULL) {
10711 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10713 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
10715 rsm->r_in_tmap = 1;
10717 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED);
10719 sack_filter_clear(&rack->r_ctl.rack_sf, rack->rc_tp->snd_una);
10723 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack, uint32_t bytes_this_ack, uint32_t segsiz)
10725 int do_detection = 0;
10727 if (rack->sack_attack_disable || rack->rc_suspicious) {
10729 * If we have been disabled we must detect
10730 * to possibly reverse it. Or if the guy has
10731 * sent in suspicious sacks we want to do detection too.
10735 } else if ((rack->do_detection || tcp_force_detection) &&
10736 (tcp_sack_to_ack_thresh > 0) &&
10737 (tcp_sack_to_move_thresh > 0) &&
10738 (rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum)) {
10740 * We only detect here if:
10741 * 1) System wide forcing is on <or> do_detection is on
10743 * 2) We have thresholds for move and ack (set one to 0 and we are off)
10745 * 3) We have maps allocated larger than our min (500).
10749 if (do_detection > 0) {
10751 * We have thresholds set to find
10752 * possible attackers and disable sack.
10755 uint64_t ackratio, moveratio, movetotal;
10757 /* Log detecting */
10758 rack_log_sad(rack, 1);
10759 /* Do we establish a ack ratio */
10760 if ((rack->r_ctl.sack_count > tcp_map_minimum) ||
10761 (rack->rc_suspicious == 1) ||
10762 (rack->sack_attack_disable > 0)) {
10763 ackratio = (uint64_t)(rack->r_ctl.sack_count);
10764 ackratio *= (uint64_t)(1000);
10765 if (rack->r_ctl.ack_count)
10766 ackratio /= (uint64_t)(rack->r_ctl.ack_count);
10768 /* We can hit this due to ack totals degregation (via small sacks) */
10773 * No ack ratio needed if we have not
10774 * seen more sacks then the number of map entries.
10775 * The exception to that is if we have disabled sack then
10776 * we need to find a ratio.
10781 if ((rack->sack_attack_disable == 0) &&
10782 (ackratio > rack_highest_sack_thresh_seen))
10783 rack_highest_sack_thresh_seen = (uint32_t)ackratio;
10784 /* Do we establish a move ratio? */
10785 if ((rack->r_ctl.sack_moved_extra > tcp_map_minimum) ||
10786 (rack->rc_suspicious == 1) ||
10787 (rack->sack_attack_disable > 0)) {
10789 * We need to have more sack moves than maps
10790 * allocated to have a move ratio considered.
10792 movetotal = rack->r_ctl.sack_moved_extra;
10793 movetotal += rack->r_ctl.sack_noextra_move;
10794 moveratio = rack->r_ctl.sack_moved_extra;
10795 moveratio *= (uint64_t)1000;
10797 moveratio /= movetotal;
10799 /* No moves, thats pretty good */
10804 * Not enough moves have occured to consider
10805 * if we are out of whack in that ratio.
10806 * The exception to that is if we have disabled sack then
10807 * we need to find a ratio.
10811 if ((rack->sack_attack_disable == 0) &&
10812 (moveratio > rack_highest_move_thresh_seen))
10813 rack_highest_move_thresh_seen = (uint32_t)moveratio;
10814 /* Now the tests */
10815 if (rack->sack_attack_disable == 0) {
10816 /* Not disabled, do we need to disable? */
10817 if ((ackratio > tcp_sack_to_ack_thresh) &&
10818 (moveratio > tcp_sack_to_move_thresh)) {
10819 /* Disable sack processing */
10820 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
10821 rack->sack_attack_disable = 1;
10822 /* set it so we have the built in delay */
10823 rack->r_ctl.ack_during_sd = 1;
10824 if (rack_merge_out_sacks_on_attack)
10825 rack_merge_out_sacks(rack);
10826 counter_u64_add(rack_sack_attacks_detected, 1);
10827 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED);
10828 /* Clamp the cwnd at flight size */
10829 rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
10830 rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
10831 rack_log_sad(rack, 2);
10834 /* We are sack-disabled check for false positives */
10835 if ((ackratio <= tcp_restoral_thresh) ||
10836 ((rack_merge_out_sacks_on_attack == 0) &&
10837 (rack->rc_suspicious == 0) &&
10838 (rack->r_ctl.rc_num_maps_alloced <= (tcp_map_minimum/2)))) {
10839 rack->sack_attack_disable = 0;
10840 rack_log_sad(rack, 3);
10841 /* Restart counting */
10842 rack->r_ctl.sack_count = 0;
10843 rack->r_ctl.sack_moved_extra = 0;
10844 rack->r_ctl.sack_noextra_move = 1;
10845 rack->rc_suspicious = 0;
10846 rack->r_ctl.ack_count = max(1,
10847 (bytes_this_ack / segsiz));
10849 counter_u64_add(rack_sack_attacks_reversed, 1);
10850 /* Restore the cwnd */
10851 if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
10852 rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
10860 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end)
10863 uint32_t am, l_end;
10866 if (SEQ_GT(end, start))
10870 if ((rack->rc_last_tlp_acked_set ) &&
10871 (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) &&
10872 (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) {
10874 * The DSACK is because of a TLP which we don't
10875 * do anything with the reordering window over since
10876 * it was not reordering that caused the DSACK but
10877 * our previous retransmit TLP.
10879 rack_log_dsack_event(rack, 7, __LINE__, start, end);
10881 goto skip_dsack_round;
10883 if (rack->rc_last_sent_tlp_seq_valid) {
10884 l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len;
10885 if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) &&
10886 (SEQ_LEQ(end, l_end))) {
10888 * This dsack is from the last sent TLP, ignore it
10889 * for reordering purposes.
10891 rack_log_dsack_event(rack, 7, __LINE__, start, end);
10893 goto skip_dsack_round;
10896 if (rack->rc_dsack_round_seen == 0) {
10897 rack->rc_dsack_round_seen = 1;
10898 rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max;
10899 rack->r_ctl.num_dsack++;
10900 rack->r_ctl.dsack_persist = 16; /* 16 is from the standard */
10901 rack_log_dsack_event(rack, 2, __LINE__, 0, 0);
10905 * We keep track of how many DSACK blocks we get
10906 * after a recovery incident.
10908 rack->r_ctl.dsack_byte_cnt += am;
10909 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) &&
10910 rack->r_ctl.retran_during_recovery &&
10911 (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) {
10913 * False recovery most likely culprit is reordering. If
10914 * nothing else is missing we need to revert.
10916 rack->r_might_revert = 1;
10917 rack_handle_might_revert(rack->rc_tp, rack);
10918 rack->r_might_revert = 0;
10919 rack->r_ctl.retran_during_recovery = 0;
10920 rack->r_ctl.dsack_byte_cnt = 0;
10926 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una)
10928 return (((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt);
10932 rack_compute_pipe(struct tcpcb *tp)
10934 return ((int32_t)do_rack_compute_pipe(tp,
10935 (struct tcp_rack *)tp->t_fb_ptr,
10940 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack)
10942 /* Deal with changed and PRR here (in recovery only) */
10943 uint32_t pipe, snd_una;
10945 rack->r_ctl.rc_prr_delivered += changed;
10947 if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) {
10949 * It is all outstanding, we are application limited
10950 * and thus we don't need more room to send anything.
10951 * Note we use tp->snd_una here and not th_ack because
10952 * the data as yet not been cut from the sb.
10954 rack->r_ctl.rc_prr_sndcnt = 0;
10957 /* Compute prr_sndcnt */
10958 if (SEQ_GT(tp->snd_una, th_ack)) {
10959 snd_una = tp->snd_una;
10963 pipe = do_rack_compute_pipe(tp, rack, snd_una);
10964 if (pipe > tp->snd_ssthresh) {
10967 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
10968 if (rack->r_ctl.rc_prr_recovery_fs > 0)
10969 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
10971 rack->r_ctl.rc_prr_sndcnt = 0;
10972 rack_log_to_prr(rack, 9, 0, __LINE__);
10976 if (sndcnt > (long)rack->r_ctl.rc_prr_out)
10977 sndcnt -= rack->r_ctl.rc_prr_out;
10980 rack->r_ctl.rc_prr_sndcnt = sndcnt;
10981 rack_log_to_prr(rack, 10, 0, __LINE__);
10985 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
10986 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
10989 if (changed > limit)
10991 limit += ctf_fixed_maxseg(tp);
10992 if (tp->snd_ssthresh > pipe) {
10993 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
10994 rack_log_to_prr(rack, 11, 0, __LINE__);
10996 rack->r_ctl.rc_prr_sndcnt = min(0, limit);
10997 rack_log_to_prr(rack, 12, 0, __LINE__);
11003 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck,
11004 int *dsack_seen, int *sacks_seen)
11007 struct tcp_rack *rack;
11008 struct rack_sendmap *rsm;
11009 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
11010 register uint32_t th_ack;
11011 int32_t i, j, k, num_sack_blks = 0;
11012 uint32_t cts, acked, ack_point;
11013 int loop_start = 0, moved_two = 0, no_extra = 0;
11015 uint32_t segsiz, o_cnt;
11018 INP_WLOCK_ASSERT(tptoinpcb(tp));
11019 if (tcp_get_flags(th) & TH_RST) {
11020 /* We don't log resets */
11023 rack = (struct tcp_rack *)tp->t_fb_ptr;
11024 cts = tcp_get_usecs(NULL);
11025 rsm = tqhash_min(rack->r_ctl.tqh);
11027 th_ack = th->th_ack;
11028 if (rack->sack_attack_disable == 0)
11029 rack_do_decay(rack);
11030 segsiz = ctf_fixed_maxseg(rack->rc_tp);
11031 if (BYTES_THIS_ACK(tp, th) >= segsiz) {
11033 * You only get credit for
11034 * MSS and greater (and you get extra
11035 * credit for larger cum-ack moves).
11039 ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
11040 rack->r_ctl.ack_count += ac;
11041 counter_u64_add(rack_ack_total, ac);
11043 if (rack->r_ctl.ack_count > 0xfff00000) {
11045 * reduce the number to keep us under
11048 rack->r_ctl.ack_count /= 2;
11049 rack->r_ctl.sack_count /= 2;
11051 if (SEQ_GT(th_ack, tp->snd_una)) {
11052 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
11053 tp->t_acktime = ticks;
11055 if (rsm && SEQ_GT(th_ack, rsm->r_start))
11056 changed = th_ack - rsm->r_start;
11058 rack_process_to_cumack(tp, rack, th_ack, cts, to,
11059 tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
11061 if ((to->to_flags & TOF_SACK) == 0) {
11062 /* We are done nothing left and no sack. */
11063 rack_handle_might_revert(tp, rack);
11065 * For cases where we struck a dup-ack
11066 * with no SACK, add to the changes so
11067 * PRR will work right.
11069 if (dup_ack_struck && (changed == 0)) {
11070 changed += ctf_fixed_maxseg(rack->rc_tp);
11074 /* Sack block processing */
11075 if (SEQ_GT(th_ack, tp->snd_una))
11076 ack_point = th_ack;
11078 ack_point = tp->snd_una;
11079 for (i = 0; i < to->to_nsacks; i++) {
11080 bcopy((to->to_sacks + i * TCPOLEN_SACK),
11081 &sack, sizeof(sack));
11082 sack.start = ntohl(sack.start);
11083 sack.end = ntohl(sack.end);
11084 if (SEQ_GT(sack.end, sack.start) &&
11085 SEQ_GT(sack.start, ack_point) &&
11086 SEQ_LT(sack.start, tp->snd_max) &&
11087 SEQ_GT(sack.end, ack_point) &&
11088 SEQ_LEQ(sack.end, tp->snd_max)) {
11089 sack_blocks[num_sack_blks] = sack;
11091 } else if (SEQ_LEQ(sack.start, th_ack) &&
11092 SEQ_LEQ(sack.end, th_ack)) {
11095 if (dsack_seen != NULL)
11097 was_tlp = rack_note_dsack(rack, sack.start, sack.end);
11099 * Its a D-SACK block.
11101 tcp_record_dsack(tp, sack.start, sack.end, was_tlp);
11104 if (rack->rc_dsack_round_seen) {
11105 /* Is the dsack roound over? */
11106 if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) {
11108 rack->rc_dsack_round_seen = 0;
11109 rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
11113 * Sort the SACK blocks so we can update the rack scoreboard with
11116 o_cnt = num_sack_blks;
11117 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
11118 num_sack_blks, th->th_ack);
11119 ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
11120 if (sacks_seen != NULL)
11121 *sacks_seen = num_sack_blks;
11122 if (num_sack_blks == 0) {
11123 /* Nothing to sack, but we need to update counts */
11124 if ((o_cnt == 1) &&
11125 (*dsack_seen != 1))
11126 rack->r_ctl.sack_count++;
11127 else if (o_cnt > 1)
11128 rack->r_ctl.sack_count++;
11129 goto out_with_totals;
11131 if (rack->sack_attack_disable) {
11133 * An attacker disablement is in place, for
11134 * every sack block that is not at least a full MSS
11135 * count up sack_count.
11137 for (i = 0; i < num_sack_blks; i++) {
11138 if ((sack_blocks[i].end - sack_blocks[i].start) < segsiz) {
11139 rack->r_ctl.sack_count++;
11141 if (rack->r_ctl.sack_count > 0xfff00000) {
11143 * reduce the number to keep us under
11146 rack->r_ctl.ack_count /= 2;
11147 rack->r_ctl.sack_count /= 2;
11152 /* Its a sack of some sort */
11153 rack->r_ctl.sack_count += num_sack_blks;
11154 if (rack->r_ctl.sack_count > 0xfff00000) {
11156 * reduce the number to keep us under
11159 rack->r_ctl.ack_count /= 2;
11160 rack->r_ctl.sack_count /= 2;
11162 if (num_sack_blks < 2) {
11163 /* Only one, we don't need to sort */
11166 /* Sort the sacks */
11167 for (i = 0; i < num_sack_blks; i++) {
11168 for (j = i + 1; j < num_sack_blks; j++) {
11169 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
11170 sack = sack_blocks[i];
11171 sack_blocks[i] = sack_blocks[j];
11172 sack_blocks[j] = sack;
11177 * Now are any of the sack block ends the same (yes some
11178 * implementations send these)?
11181 if (num_sack_blks == 0)
11182 goto out_with_totals;
11183 if (num_sack_blks > 1) {
11184 for (i = 0; i < num_sack_blks; i++) {
11185 for (j = i + 1; j < num_sack_blks; j++) {
11186 if (sack_blocks[i].end == sack_blocks[j].end) {
11188 * Ok these two have the same end we
11189 * want the smallest end and then
11190 * throw away the larger and start
11193 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
11195 * The second block covers
11196 * more area use that
11198 sack_blocks[i].start = sack_blocks[j].start;
11201 * Now collapse out the dup-sack and
11204 for (k = (j + 1); k < num_sack_blks; k++) {
11205 sack_blocks[j].start = sack_blocks[k].start;
11206 sack_blocks[j].end = sack_blocks[k].end;
11217 * First lets look to see if
11218 * we have retransmitted and
11219 * can use the transmit next?
11221 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11223 SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
11224 SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
11226 * We probably did the FR and the next
11227 * SACK in continues as we would expect.
11229 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &no_extra, &moved_two, segsiz);
11231 rack->r_wanted_output = 1;
11234 if (num_sack_blks == 1) {
11236 * This is what we would expect from
11237 * a normal implementation to happen
11238 * after we have retransmitted the FR,
11239 * i.e the sack-filter pushes down
11240 * to 1 block and the next to be retransmitted
11241 * is the sequence in the sack block (has more
11242 * are acked). Count this as ACK'd data to boost
11243 * up the chances of recovering any false positives.
11245 rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
11246 counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
11247 counter_u64_add(rack_express_sack, 1);
11248 if (rack->r_ctl.ack_count > 0xfff00000) {
11250 * reduce the number to keep us under
11253 rack->r_ctl.ack_count /= 2;
11254 rack->r_ctl.sack_count /= 2;
11258 * If we did not get a SACK for at least a MSS and
11259 * had to move at all, or if we moved more than our
11260 * threshold, it counts against the "extra" move.
11262 rack->r_ctl.sack_moved_extra += moved_two;
11263 rack->r_ctl.sack_noextra_move += no_extra;
11264 counter_u64_add(rack_move_some, 1);
11267 * else we did not have to move
11268 * any more than we would expect.
11270 rack->r_ctl.sack_noextra_move += no_extra;
11271 rack->r_ctl.sack_noextra_move++;
11272 counter_u64_add(rack_move_none, 1);
11274 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
11275 (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
11276 rack->r_ctl.sack_moved_extra /= 2;
11277 rack->r_ctl.sack_noextra_move /= 2;
11279 goto out_with_totals;
11282 * Start the loop through the
11283 * rest of blocks, past the first block.
11288 counter_u64_add(rack_sack_total, 1);
11289 rsm = rack->r_ctl.rc_sacklast;
11290 for (i = loop_start; i < num_sack_blks; i++) {
11291 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &no_extra, &moved_two, segsiz);
11293 rack->r_wanted_output = 1;
11298 * If we did not get a SACK for at least a MSS and
11299 * had to move at all, or if we moved more than our
11300 * threshold, it counts against the "extra" move.
11302 rack->r_ctl.sack_moved_extra += moved_two;
11303 rack->r_ctl.sack_noextra_move += no_extra;
11304 counter_u64_add(rack_move_some, 1);
11307 * else we did not have to move
11308 * any more than we would expect.
11310 rack->r_ctl.sack_noextra_move += no_extra;
11311 rack->r_ctl.sack_noextra_move++;
11312 counter_u64_add(rack_move_none, 1);
11314 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
11315 (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
11316 rack->r_ctl.sack_moved_extra /= 2;
11317 rack->r_ctl.sack_noextra_move /= 2;
11319 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
11321 * If the SACK was not a full MSS then
11322 * we add to sack_count the number of
11323 * MSS's (or possibly more than
11324 * a MSS if its a TSO send) we had to skip by.
11326 rack->r_ctl.sack_count += moved_two;
11327 if (rack->r_ctl.sack_count > 0xfff00000) {
11328 rack->r_ctl.ack_count /= 2;
11329 rack->r_ctl.sack_count /= 2;
11331 counter_u64_add(rack_sack_total, moved_two);
11334 * Now we need to setup for the next
11335 * round. First we make sure we won't
11336 * exceed the size of our uint32_t on
11337 * the various counts, and then clear out
11344 if (num_sack_blks > 1) {
11346 * You get an extra stroke if
11347 * you have more than one sack-blk, this
11348 * could be where we are skipping forward
11349 * and the sack-filter is still working, or
11350 * it could be an attacker constantly
11353 rack->r_ctl.sack_moved_extra++;
11354 counter_u64_add(rack_move_some, 1);
11357 #ifdef TCP_SAD_DETECTION
11358 rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp));
11361 /* Something changed cancel the rack timer */
11362 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
11364 tsused = tcp_get_usecs(NULL);
11365 rsm = tcp_rack_output(tp, rack, tsused);
11366 if ((!IN_FASTRECOVERY(tp->t_flags)) &&
11368 ((rsm->r_flags & RACK_MUST_RXT) == 0)) {
11369 /* Enter recovery */
11370 entered_recovery = 1;
11371 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
11373 * When we enter recovery we need to assure we send
11376 if (rack->rack_no_prr == 0) {
11377 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
11378 rack_log_to_prr(rack, 8, 0, __LINE__);
11380 rack->r_timer_override = 1;
11382 rack->r_ctl.rc_agg_early = 0;
11383 } else if (IN_FASTRECOVERY(tp->t_flags) &&
11385 (rack->r_rr_config == 3)) {
11387 * Assure we can output and we get no
11388 * remembered pace time except the retransmit.
11390 rack->r_timer_override = 1;
11391 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
11392 rack->r_ctl.rc_resend = rsm;
11394 if (IN_FASTRECOVERY(tp->t_flags) &&
11395 (rack->rack_no_prr == 0) &&
11396 (entered_recovery == 0)) {
11397 rack_update_prr(tp, rack, changed, th_ack);
11398 if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
11399 ((tcp_in_hpts(rack->rc_tp) == 0) &&
11400 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
11402 * If you are pacing output you don't want
11406 rack->r_ctl.rc_agg_early = 0;
11407 rack->r_timer_override = 1;
11413 rack_strike_dupack(struct tcp_rack *rack)
11415 struct rack_sendmap *rsm;
11417 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11420 * We need to skip anything already set
11421 * to be retransmitted.
11423 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
11424 (rsm->r_flags & RACK_MUST_RXT)) {
11425 rsm = TAILQ_NEXT(rsm, r_tnext);
11430 if (rsm && (rsm->r_dupack < 0xff)) {
11432 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
11436 * Here we see if we need to retransmit. For
11437 * a SACK type connection if enough time has passed
11438 * we will get a return of the rsm. For a non-sack
11439 * connection we will get the rsm returned if the
11440 * dupack value is 3 or more.
11442 cts = tcp_get_usecs(&tv);
11443 rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts);
11444 if (rack->r_ctl.rc_resend != NULL) {
11445 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) {
11446 rack_cong_signal(rack->rc_tp, CC_NDUPACK,
11447 rack->rc_tp->snd_una, __LINE__);
11449 rack->r_wanted_output = 1;
11450 rack->r_timer_override = 1;
11451 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
11454 rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
11460 rack_check_bottom_drag(struct tcpcb *tp,
11461 struct tcp_rack *rack,
11464 uint32_t segsiz, minseg;
11466 segsiz = ctf_fixed_maxseg(tp);
11468 if (tp->snd_max == tp->snd_una) {
11470 * We are doing dynamic pacing and we are way
11471 * under. Basically everything got acked while
11472 * we were still waiting on the pacer to expire.
11474 * This means we need to boost the b/w in
11475 * addition to any earlier boosting of
11480 lt_bw = rack_get_lt_bw(rack);
11481 rack->rc_dragged_bottom = 1;
11482 rack_validate_multipliers_at_or_above100(rack);
11483 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
11486 * Lets use the long-term b/w we have
11487 * been getting as a base.
11489 if (rack->rc_gp_filled == 0) {
11490 if (lt_bw > ONE_POINT_TWO_MEG) {
11492 * If we have no measurement
11493 * don't let us set in more than
11494 * 1.2Mbps. If we are still too
11495 * low after pacing with this we
11496 * will hopefully have a max b/w
11497 * available to sanity check things.
11499 lt_bw = ONE_POINT_TWO_MEG;
11501 rack->r_ctl.rc_rtt_diff = 0;
11502 rack->r_ctl.gp_bw = lt_bw;
11503 rack->rc_gp_filled = 1;
11504 if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11505 rack->r_ctl.num_measurements = RACK_REQ_AVG;
11506 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11507 } else if (lt_bw > rack->r_ctl.gp_bw) {
11508 rack->r_ctl.rc_rtt_diff = 0;
11509 if (rack->r_ctl.num_measurements < RACK_REQ_AVG)
11510 rack->r_ctl.num_measurements = RACK_REQ_AVG;
11511 rack->r_ctl.gp_bw = lt_bw;
11512 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
11514 rack_increase_bw_mul(rack, -1, 0, 0, 1);
11515 if ((rack->gp_ready == 0) &&
11516 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) {
11517 /* We have enough measurements now */
11518 rack->gp_ready = 1;
11519 if ((rack->rc_always_pace && (rack->use_fixed_rate == 0)) ||
11521 rack_set_cc_pacing(rack);
11522 if (rack->defer_options)
11523 rack_apply_deferred_options(rack);
11527 * zero rtt possibly?, settle for just an old increase.
11529 rack_increase_bw_mul(rack, -1, 0, 0, 1);
11531 } else if ((IN_FASTRECOVERY(tp->t_flags) == 0) &&
11532 (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
11534 (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11535 (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
11536 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
11537 (segsiz * rack_req_segs))) {
11539 * We are doing dynamic GP pacing and
11540 * we have everything except 1MSS or less
11541 * bytes left out. We are still pacing away.
11542 * And there is data that could be sent, This
11543 * means we are inserting delayed ack time in
11544 * our measurements because we are pacing too slow.
11546 rack_validate_multipliers_at_or_above100(rack);
11547 rack->rc_dragged_bottom = 1;
11548 rack_increase_bw_mul(rack, -1, 0, 0, 1);
11552 #ifdef TCP_REQUEST_TRK
11554 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq,
11555 struct tcp_sendfile_track *cur, uint8_t mod, int line, int err)
11559 do_log = tcp_bblogging_on(rack->rc_tp);
11561 if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0)
11563 /* We only allow the three below with point logging on */
11564 if ((mod != HYBRID_LOG_RULES_APP) &&
11565 (mod != HYBRID_LOG_RULES_SET) &&
11566 (mod != HYBRID_LOG_REQ_COMP))
11571 union tcp_log_stackspecific log;
11574 /* Convert our ms to a microsecond */
11575 memset(&log, 0, sizeof(log));
11576 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11577 log.u_bbr.flex1 = seq;
11578 log.u_bbr.cwnd_gain = line;
11582 log.u_bbr.flex2 = cur->start_seq;
11583 log.u_bbr.flex3 = cur->end_seq;
11584 log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff);
11585 log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff);
11586 log.u_bbr.flex6 = cur->flags;
11587 log.u_bbr.pkts_out = cur->hybrid_flags;
11588 log.u_bbr.rttProp = cur->timestamp;
11589 log.u_bbr.cur_del_rate = cur->cspr;
11590 log.u_bbr.bw_inuse = cur->start;
11591 log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff);
11592 log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ;
11593 log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff);
11594 log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ;
11595 log.u_bbr.bbr_state = 1;
11596 off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]);
11597 log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track));
11599 log.u_bbr.flex2 = err;
11602 * Fill in flex7 to be CHD (catchup|hybrid|DGP)
11604 log.u_bbr.flex7 = rack->rc_catch_up;
11605 log.u_bbr.flex7 <<= 1;
11606 log.u_bbr.flex7 |= rack->rc_hybrid_mode;
11607 log.u_bbr.flex7 <<= 1;
11608 log.u_bbr.flex7 |= rack->dgp_on;
11609 log.u_bbr.flex8 = mod;
11610 log.u_bbr.delRate = rack->r_ctl.bw_rate_cap;
11611 log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg;
11612 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11613 log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start;
11614 log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error;
11615 log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop;
11616 tcp_log_event(rack->rc_tp, NULL,
11617 &rack->rc_inp->inp_socket->so_rcv,
11618 &rack->rc_inp->inp_socket->so_snd,
11619 TCP_HYBRID_PACING_LOG, 0,
11620 0, &log, false, NULL, __func__, __LINE__, &tv);
11625 #ifdef TCP_REQUEST_TRK
11627 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len)
11629 struct tcp_sendfile_track *rc_cur;
11633 rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq);
11634 if (rc_cur == NULL) {
11635 /* If not in the beginning what about the end piece */
11636 rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11637 rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1));
11641 /* If we find no parameters we are in straight DGP mode */
11642 if(rc_cur == NULL) {
11643 /* None found for this seq, just DGP for now */
11644 rack->r_ctl.client_suggested_maxseg = 0;
11645 rack->rc_catch_up = 0;
11646 rack->r_ctl.bw_rate_cap = 0;
11647 rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err);
11648 if (rack->r_ctl.rc_last_sft) {
11649 rack->r_ctl.rc_last_sft = NULL;
11654 * Ok if we have a new entry *or* have never
11655 * set up an entry we need to proceed. If
11656 * we have already set it up this entry we
11657 * just continue along with what we already
11661 if ((rack->r_ctl.rc_last_sft != NULL) &&
11662 (rack->r_ctl.rc_last_sft == rc_cur)) {
11663 /* Its already in place */
11664 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0);
11667 if (rack->rc_hybrid_mode == 0) {
11668 rack->r_ctl.rc_last_sft = rc_cur;
11669 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11672 if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){
11673 /* Compensate for all the header overhead's */
11674 rack->r_ctl.bw_rate_cap = rack_compensate_for_linerate(rack, rc_cur->cspr);
11676 rack->r_ctl.bw_rate_cap = 0;
11677 if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS)
11678 rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg;
11680 rack->r_ctl.client_suggested_maxseg = 0;
11681 if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) &&
11682 (rc_cur->cspr > 0)) {
11685 rack->rc_catch_up = 1;
11687 * Calculate the deadline time, first set the
11688 * time to when the request arrived.
11690 rc_cur->deadline = rc_cur->localtime;
11692 * Next calculate the length and compensate for
11695 len = rc_cur->end - rc_cur->start;
11696 if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) {
11698 * This session is doing TLS. Take a swag guess
11701 len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len);
11704 * Now considering the size, and the cspr, what is the time that
11705 * would be required at the cspr rate. Here we use the raw
11706 * cspr value since the client only looks at the raw data. We
11707 * do use len which includes TLS overhead, but not the TCP/IP etc.
11708 * That will get made up for in the CU pacing rate set.
11710 len *= HPTS_USEC_IN_SEC;
11711 len /= rc_cur->cspr;
11712 rc_cur->deadline += len;
11714 rack->rc_catch_up = 0;
11715 rc_cur->deadline = 0;
11717 if (rack->r_ctl.client_suggested_maxseg != 0) {
11719 * We need to reset the max pace segs if we have a
11720 * client_suggested_maxseg.
11722 rack_set_pace_segments(tp, rack, __LINE__, NULL);
11724 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0);
11725 /* Remember it for next time and for CU mode */
11726 rack->r_ctl.rc_last_sft = rc_cur;
11731 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts)
11733 #ifdef TCP_REQUEST_TRK
11734 struct tcp_sendfile_track *ent;
11736 ent = rack->r_ctl.rc_last_sft;
11737 if ((ent == NULL) ||
11738 (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) ||
11739 (SEQ_GEQ(seq, ent->end_seq))) {
11740 /* Time to update the track. */
11741 rack_set_dgp_hybrid_mode(rack, seq, len);
11742 ent = rack->r_ctl.rc_last_sft;
11748 if (SEQ_LT(ent->end_seq, (seq + len))) {
11750 * This is the case where our end_seq guess
11751 * was wrong. This is usually due to TLS having
11752 * more bytes then our guess. It could also be the
11753 * case that the client sent in two requests closely
11754 * and the SB is full of both so we are sending part
11755 * of each (end|beg). In such a case lets move this
11756 * guys end to match the end of this send. That
11757 * way it will complete when all of it is acked.
11759 ent->end_seq = (seq + len);
11760 rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent);
11762 /* Now validate we have set the send time of this one */
11763 if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) {
11764 ent->flags |= TCP_TRK_TRACK_FLG_FSND;
11765 ent->first_send = cts;
11766 ent->sent_at_fs = rack->rc_tp->t_sndbytes;
11767 ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes;
11773 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount)
11776 * The fast output path is enabled and we
11777 * have moved the cumack forward. Lets see if
11778 * we can expand forward the fast path length by
11779 * that amount. What we would ideally like to
11780 * do is increase the number of bytes in the
11781 * fast path block (left_to_send) by the
11782 * acked amount. However we have to gate that
11784 * 1) The amount outstanding and the rwnd of the peer
11785 * (i.e. we don't want to exceed the rwnd of the peer).
11787 * 2) The amount of data left in the socket buffer (i.e.
11788 * we can't send beyond what is in the buffer).
11790 * Note that this does not take into account any increase
11791 * in the cwnd. We will only extend the fast path by
11794 uint32_t new_total, gating_val;
11796 new_total = acked_amount + rack->r_ctl.fsb.left_to_send;
11797 gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)),
11798 (tp->snd_wnd - (tp->snd_max - tp->snd_una)));
11799 if (new_total <= gating_val) {
11800 /* We can increase left_to_send by the acked amount */
11801 counter_u64_add(rack_extended_rfo, 1);
11802 rack->r_ctl.fsb.left_to_send = new_total;
11803 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))),
11804 ("rack:%p left_to_send:%u sbavail:%u out:%u",
11805 rack, rack->r_ctl.fsb.left_to_send,
11806 sbavail(&rack->rc_inp->inp_socket->so_snd),
11807 (tp->snd_max - tp->snd_una)));
11813 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb)
11816 * Here any sendmap entry that points to the
11817 * beginning mbuf must be adjusted to the correct
11818 * offset. This must be called with:
11819 * 1) The socket buffer locked
11820 * 2) snd_una adjusted to its new position.
11822 * Note that (2) implies rack_ack_received has also
11823 * been called and all the sbcut's have been done.
11825 * We grab the first mbuf in the socket buffer and
11826 * then go through the front of the sendmap, recalculating
11827 * the stored offset for any sendmap entry that has
11828 * that mbuf. We must use the sb functions to do this
11829 * since its possible an add was done has well as
11830 * the subtraction we may have just completed. This should
11831 * not be a penalty though, since we just referenced the sb
11832 * to go in and trim off the mbufs that we freed (of course
11833 * there will be a penalty for the sendmap references though).
11835 * Note also with INVARIANT on, we validate with a KASSERT
11836 * that the first sendmap entry has a soff of 0.
11840 struct rack_sendmap *rsm;
11843 int first_processed = 0;
11846 snd_una = rack->rc_tp->snd_una;
11847 SOCKBUF_LOCK_ASSERT(sb);
11849 rsm = tqhash_min(rack->r_ctl.tqh);
11850 if ((rsm == NULL) || (m == NULL)) {
11851 /* Nothing outstanding */
11854 /* The very first RSM's mbuf must point to the head mbuf in the sb */
11855 KASSERT((rsm->m == m),
11856 ("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb",
11858 while (rsm->m && (rsm->m == m)) {
11859 /* one to adjust */
11864 tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff);
11865 if ((rsm->orig_m_len != m->m_len) ||
11866 (rsm->orig_t_space != M_TRAILINGROOM(m))){
11867 rack_adjust_orig_mlen(rsm);
11869 if (first_processed == 0) {
11870 KASSERT((rsm->soff == 0),
11871 ("Rack:%p rsm:%p -- rsm at head but soff not zero",
11873 first_processed = 1;
11875 if ((rsm->soff != soff) || (rsm->m != tm)) {
11877 * This is not a fatal error, we anticipate it
11878 * might happen (the else code), so we count it here
11879 * so that under invariant we can see that it really
11882 counter_u64_add(rack_adjust_map_bw, 1);
11887 rsm->orig_m_len = rsm->m->m_len;
11888 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11890 rsm->orig_m_len = 0;
11891 rsm->orig_t_space = 0;
11894 rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff);
11896 rsm->orig_m_len = rsm->m->m_len;
11897 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
11899 rsm->orig_m_len = 0;
11900 rsm->orig_t_space = 0;
11903 rsm = tqhash_next(rack->r_ctl.tqh, rsm);
11909 #ifdef TCP_REQUEST_TRK
11911 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack)
11913 struct tcp_sendfile_track *ent;
11916 if ((rack->rc_hybrid_mode == 0) &&
11917 (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) {
11919 * Just do normal completions hybrid pacing is not on
11920 * and CLDL is off as well.
11922 tcp_req_check_for_comp(rack->rc_tp, th_ack);
11926 * Originally I was just going to find the th_ack associated
11927 * with an entry. But then I realized a large strech ack could
11928 * in theory ack two or more requests at once. So instead we
11929 * need to find all entries that are completed by th_ack not
11930 * just a single entry and do our logging.
11932 ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11933 while (ent != NULL) {
11935 * We may be doing hybrid pacing or CLDL and need more details possibly
11936 * so we do it manually instead of calling
11937 * tcp_req_check_for_comp()
11939 uint64_t laa, tim, data, cbw, ftim;
11941 /* Ok this ack frees it */
11942 rack_log_hybrid(rack, th_ack,
11943 ent, HYBRID_LOG_REQ_COMP, __LINE__, 0);
11944 /* calculate the time based on the ack arrival */
11945 data = ent->end - ent->start;
11946 laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time);
11947 if (ent->flags & TCP_TRK_TRACK_FLG_FSND) {
11948 if (ent->first_send > ent->localtime)
11949 ftim = ent->first_send;
11951 ftim = ent->localtime;
11954 ftim = ent->localtime;
11956 if (laa > ent->localtime)
11960 cbw = data * HPTS_USEC_IN_SEC;
11965 rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent);
11967 * Check to see if we are freeing what we are pointing to send wise
11968 * if so be sure to NULL the pointer so we know we are no longer
11971 if (ent == rack->r_ctl.rc_last_sft)
11972 rack->r_ctl.rc_last_sft = NULL;
11973 /* Generate the log that the tcp_netflix call would have */
11974 tcp_req_log_req_info(rack->rc_tp, ent,
11975 i, TCP_TRK_REQ_LOG_FREED, 0, 0);
11976 /* Free it and see if there is another one */
11977 tcp_req_free_a_slot(rack->rc_tp, ent);
11978 ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i);
11985 * Return value of 1, we do not need to call rack_process_data().
11986 * return value of 0, rack_process_data can be called.
11987 * For ret_val if its 0 the TCP is locked, if its non-zero
11988 * its unlocked and probably unsafe to touch the TCB.
11991 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
11992 struct tcpcb *tp, struct tcpopt *to,
11993 uint32_t tiwin, int32_t tlen,
11994 int32_t * ofia, int32_t thflags, int32_t *ret_val)
11996 int32_t ourfinisacked = 0;
11997 int32_t nsegs, acked_amount;
11999 struct mbuf *mfree;
12000 struct tcp_rack *rack;
12001 int32_t under_pacing = 0;
12002 int32_t recovery = 0;
12004 INP_WLOCK_ASSERT(tptoinpcb(tp));
12006 rack = (struct tcp_rack *)tp->t_fb_ptr;
12007 if (SEQ_GT(th->th_ack, tp->snd_max)) {
12008 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val,
12009 &rack->r_ctl.challenge_ack_ts,
12010 &rack->r_ctl.challenge_ack_cnt);
12011 rack->r_wanted_output = 1;
12014 if (rack->gp_ready &&
12015 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12018 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
12019 int in_rec, dup_ack_struck = 0;
12020 int dsack_seen = 0, sacks_seen = 0;
12022 in_rec = IN_FASTRECOVERY(tp->t_flags);
12023 if (rack->rc_in_persist) {
12024 tp->t_rxtshift = 0;
12025 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12026 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12029 if ((th->th_ack == tp->snd_una) &&
12030 (tiwin == tp->snd_wnd) &&
12031 ((to->to_flags & TOF_SACK) == 0)) {
12032 rack_strike_dupack(rack);
12033 dup_ack_struck = 1;
12035 rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)),
12036 dup_ack_struck, &dsack_seen, &sacks_seen);
12037 if ((rack->sack_attack_disable > 0) &&
12038 (th->th_ack == tp->snd_una) &&
12039 (tiwin == tp->snd_wnd) &&
12040 (dsack_seen == 0) &&
12041 (sacks_seen > 0)) {
12043 * If sacks have been disabled we may
12044 * want to strike a dup-ack "ignoring" the
12045 * sack as long as the sack was not a "dsack". Note
12046 * that if no sack is sent (TOF_SACK is off) then the
12047 * normal dsack code above rack_log_ack() would have
12048 * already struck. So this is just to catch the case
12049 * were we are ignoring sacks from this guy due to
12050 * it being a suspected attacker.
12052 rack_strike_dupack(rack);
12056 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12058 * Old ack, behind (or duplicate to) the last one rcv'd
12059 * Note: We mark reordering is occuring if its
12060 * less than and we have not closed our window.
12062 if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) {
12063 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
12064 if (rack->r_ctl.rc_reorder_ts == 0)
12065 rack->r_ctl.rc_reorder_ts = 1;
12070 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
12071 * something we sent.
12073 if (tp->t_flags & TF_NEEDSYN) {
12075 * T/TCP: Connection was half-synchronized, and our SYN has
12076 * been ACK'd (so connection is now fully synchronized). Go
12077 * to non-starred state, increment snd_una for ACK of SYN,
12078 * and check if we can do window scaling.
12080 tp->t_flags &= ~TF_NEEDSYN;
12082 /* Do window scaling? */
12083 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
12084 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
12085 tp->rcv_scale = tp->request_r_scale;
12086 /* Send window already scaled. */
12089 nsegs = max(1, m->m_pkthdr.lro_nsegs);
12091 acked = BYTES_THIS_ACK(tp, th);
12094 * Any time we move the cum-ack forward clear
12095 * keep-alive tied probe-not-answered. The
12096 * persists clears its own on entry.
12098 rack->probe_not_answered = 0;
12100 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
12101 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
12103 * If we just performed our first retransmit, and the ACK arrives
12104 * within our recovery window, then it was a mistake to do the
12105 * retransmit in the first place. Recover our original cwnd and
12106 * ssthresh, and proceed to transmit where we left off.
12108 if ((tp->t_flags & TF_PREVVALID) &&
12109 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
12110 tp->t_flags &= ~TF_PREVVALID;
12111 if (tp->t_rxtshift == 1 &&
12112 (int)(ticks - tp->t_badrxtwin) < 0)
12113 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
12116 /* assure we are not backed off */
12117 tp->t_rxtshift = 0;
12118 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12119 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12120 rack->rc_tlp_in_progress = 0;
12121 rack->r_ctl.rc_tlp_cnt_out = 0;
12123 * If it is the RXT timer we want to
12124 * stop it, so we can restart a TLP.
12126 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
12127 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12128 #ifdef TCP_REQUEST_TRK
12129 rack_req_check_for_comp(rack, th->th_ack);
12133 * If we have a timestamp reply, update smoothed round trip time. If
12134 * no timestamp is present but transmit timer is running and timed
12135 * sequence number was acked, update smoothed round trip time. Since
12136 * we now have an rtt measurement, cancel the timer backoff (cf.,
12137 * Phil Karn's retransmit alg.). Recompute the initial retransmit
12140 * Some boxes send broken timestamp replies during the SYN+ACK
12141 * phase, ignore timestamps of 0 or we could calculate a huge RTT
12142 * and blow up the retransmit timer.
12145 * If all outstanding data is acked, stop retransmit timer and
12146 * remember to restart (more output or persist). If there is more
12147 * data to be acked, restart retransmit timer, using current
12148 * (possibly backed-off) value.
12152 *ofia = ourfinisacked;
12155 if (IN_RECOVERY(tp->t_flags)) {
12156 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
12157 (SEQ_LT(th->th_ack, tp->snd_max))) {
12158 tcp_rack_partialack(tp);
12160 rack_post_recovery(tp, th->th_ack);
12165 * Let the congestion control algorithm update congestion control
12166 * related information. This typically means increasing the
12167 * congestion window.
12169 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery);
12170 SOCKBUF_LOCK(&so->so_snd);
12171 acked_amount = min(acked, (int)sbavail(&so->so_snd));
12172 tp->snd_wnd -= acked_amount;
12173 mfree = sbcut_locked(&so->so_snd, acked_amount);
12174 if ((sbused(&so->so_snd) == 0) &&
12175 (acked > acked_amount) &&
12176 (tp->t_state >= TCPS_FIN_WAIT_1) &&
12177 (tp->t_flags & TF_SENTFIN)) {
12179 * We must be sure our fin
12180 * was sent and acked (we can be
12181 * in FIN_WAIT_1 without having
12186 tp->snd_una = th->th_ack;
12188 if (acked_amount && sbavail(&so->so_snd))
12189 rack_adjust_sendmap_head(rack, &so->so_snd);
12190 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
12191 /* NB: sowwakeup_locked() does an implicit unlock. */
12192 sowwakeup_locked(so);
12193 /* now check the rxt clamps */
12194 if ((recovery == 1) &&
12195 (rack->excess_rxt_on) &&
12196 (rack->r_cwnd_was_clamped == 0)) {
12197 do_rack_excess_rxt(tp, rack);
12198 } else if (rack->r_cwnd_was_clamped)
12199 do_rack_check_for_unclamp(tp, rack);
12201 if (SEQ_GT(tp->snd_una, tp->snd_recover))
12202 tp->snd_recover = tp->snd_una;
12204 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
12205 tp->snd_nxt = tp->snd_una;
12207 if (under_pacing &&
12208 (rack->use_fixed_rate == 0) &&
12209 (rack->in_probe_rtt == 0) &&
12210 rack->rc_gp_dyn_mul &&
12211 rack->rc_always_pace) {
12212 /* Check if we are dragging bottom */
12213 rack_check_bottom_drag(tp, rack, so);
12215 if (tp->snd_una == tp->snd_max) {
12216 /* Nothing left outstanding */
12217 tp->t_flags &= ~TF_PREVVALID;
12218 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
12219 rack->r_ctl.retran_during_recovery = 0;
12220 rack->r_ctl.dsack_byte_cnt = 0;
12221 if (rack->r_ctl.rc_went_idle_time == 0)
12222 rack->r_ctl.rc_went_idle_time = 1;
12223 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
12224 if (sbavail(&tptosocket(tp)->so_snd) == 0)
12226 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12227 rack->rc_suspicious = 0;
12228 /* Set need output so persist might get set */
12229 rack->r_wanted_output = 1;
12230 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12231 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
12232 (sbavail(&so->so_snd) == 0) &&
12233 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
12235 * The socket was gone and the
12236 * peer sent data (now or in the past), time to
12240 /* tcp_close will kill the inp pre-log the Reset */
12241 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
12242 tp = tcp_close(tp);
12243 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
12248 *ofia = ourfinisacked;
12254 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line,
12255 int dir, uint32_t flags, struct rack_sendmap *rsm)
12257 if (tcp_bblogging_on(rack->rc_tp)) {
12258 union tcp_log_stackspecific log;
12261 memset(&log, 0, sizeof(log));
12262 log.u_bbr.flex1 = cnt;
12263 log.u_bbr.flex2 = split;
12264 log.u_bbr.flex3 = out;
12265 log.u_bbr.flex4 = line;
12266 log.u_bbr.flex5 = rack->r_must_retran;
12267 log.u_bbr.flex6 = flags;
12268 log.u_bbr.flex7 = rack->rc_has_collapsed;
12269 log.u_bbr.flex8 = dir; /*
12270 * 1 is collapsed, 0 is uncollapsed,
12271 * 2 is log of a rsm being marked, 3 is a split.
12274 log.u_bbr.rttProp = 0;
12276 log.u_bbr.rttProp = (uint64_t)rsm;
12277 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
12278 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
12279 TCP_LOG_EVENTP(rack->rc_tp, NULL,
12280 &rack->rc_inp->inp_socket->so_rcv,
12281 &rack->rc_inp->inp_socket->so_snd,
12282 TCP_RACK_LOG_COLLAPSE, 0,
12283 0, &log, false, &tv);
12288 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line)
12291 * Here all we do is mark the collapsed point and set the flag.
12292 * This may happen again and again, but there is no
12293 * sense splitting our map until we know where the
12294 * peer finally lands in the collapse.
12296 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12297 if ((rack->rc_has_collapsed == 0) ||
12298 (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd)))
12299 counter_u64_add(rack_collapsed_win_seen, 1);
12300 rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd;
12301 rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max;
12302 rack->rc_has_collapsed = 1;
12303 rack->r_collapse_point_valid = 1;
12304 rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL);
12308 rack_un_collapse_window(struct tcp_rack *rack, int line)
12310 struct rack_sendmap *nrsm, *rsm;
12311 int cnt = 0, split = 0;
12312 int insret __diagused;
12315 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND);
12316 rack->rc_has_collapsed = 0;
12317 rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
12319 /* Nothing to do maybe the peer ack'ed it all */
12320 rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12323 /* Now do we need to split this one? */
12324 if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) {
12325 rack_log_collapse(rack, rsm->r_start, rsm->r_end,
12326 rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm);
12327 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
12328 if (nrsm == NULL) {
12329 /* We can't get a rsm, mark all? */
12335 rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point);
12337 (void)tqhash_insert(rack->r_ctl.tqh, nrsm);
12339 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) {
12340 panic("Insert in rb tree of %p fails ret:%d rack:%p rsm:%p",
12341 nrsm, insret, rack, rsm);
12344 rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT,
12345 rack->r_ctl.last_collapse_point, __LINE__);
12346 if (rsm->r_in_tmap) {
12347 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
12348 nrsm->r_in_tmap = 1;
12351 * Set in the new RSM as the
12352 * collapsed starting point
12358 TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm) {
12360 nrsm->r_flags |= RACK_RWND_COLLAPSED;
12361 rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm);
12365 counter_u64_add(rack_collapsed_win, 1);
12367 rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL);
12371 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
12372 int32_t tlen, int32_t tfo_syn)
12374 if (DELAY_ACK(tp, tlen) || tfo_syn) {
12375 rack_timer_cancel(tp, rack,
12376 rack->r_ctl.rc_rcvtime, __LINE__);
12377 tp->t_flags |= TF_DELACK;
12379 rack->r_wanted_output = 1;
12380 tp->t_flags |= TF_ACKNOW;
12385 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack)
12388 * If fast output is in progress, lets validate that
12389 * the new window did not shrink on us and make it
12390 * so fast output should end.
12392 if (rack->r_fast_output) {
12396 * Calculate what we will send if left as is
12397 * and compare that to our send window.
12399 out = ctf_outstanding(tp);
12400 if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) {
12401 /* ok we have an issue */
12402 if (out >= tp->snd_wnd) {
12403 /* Turn off fast output the window is met or collapsed */
12404 rack->r_fast_output = 0;
12406 /* we have some room left */
12407 rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out;
12408 if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) {
12409 /* If not at least 1 full segment never mind */
12410 rack->r_fast_output = 0;
12419 * Return value of 1, the TCB is unlocked and most
12420 * likely gone, return value of 0, the TCP is still
12424 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
12425 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
12426 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
12429 * Update window information. Don't look at window if no ACK: TAC's
12430 * send garbage on first SYN.
12434 struct tcp_rack *rack;
12436 INP_WLOCK_ASSERT(tptoinpcb(tp));
12438 rack = (struct tcp_rack *)tp->t_fb_ptr;
12439 nsegs = max(1, m->m_pkthdr.lro_nsegs);
12440 if ((thflags & TH_ACK) &&
12441 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
12442 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
12443 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
12444 /* keep track of pure window updates */
12446 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
12447 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
12448 tp->snd_wnd = tiwin;
12449 rack_validate_fo_sendwin_up(tp, rack);
12450 tp->snd_wl1 = th->th_seq;
12451 tp->snd_wl2 = th->th_ack;
12452 if (tp->snd_wnd > tp->max_sndwnd)
12453 tp->max_sndwnd = tp->snd_wnd;
12454 rack->r_wanted_output = 1;
12455 } else if (thflags & TH_ACK) {
12456 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
12457 tp->snd_wnd = tiwin;
12458 rack_validate_fo_sendwin_up(tp, rack);
12459 tp->snd_wl1 = th->th_seq;
12460 tp->snd_wl2 = th->th_ack;
12463 if (tp->snd_wnd < ctf_outstanding(tp))
12464 /* The peer collapsed the window */
12465 rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12466 else if (rack->rc_has_collapsed)
12467 rack_un_collapse_window(rack, __LINE__);
12468 if ((rack->r_collapse_point_valid) &&
12469 (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point)))
12470 rack->r_collapse_point_valid = 0;
12471 /* Was persist timer active and now we have window space? */
12472 if ((rack->rc_in_persist != 0) &&
12473 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12474 rack->r_ctl.rc_pace_min_segs))) {
12475 rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
12476 tp->snd_nxt = tp->snd_max;
12477 /* Make sure we output to start the timer */
12478 rack->r_wanted_output = 1;
12480 /* Do we enter persists? */
12481 if ((rack->rc_in_persist == 0) &&
12482 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12483 TCPS_HAVEESTABLISHED(tp->t_state) &&
12484 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12485 sbavail(&tptosocket(tp)->so_snd) &&
12486 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12488 * Here the rwnd is less than
12489 * the pacing size, we are established,
12490 * nothing is outstanding, and there is
12491 * data to send. Enter persists.
12493 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
12495 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
12500 * don't process the URG bit, ignore them drag
12503 tp->rcv_up = tp->rcv_nxt;
12506 * Process the segment text, merging it into the TCP sequencing
12507 * queue, and arranging for acknowledgment of receipt if necessary.
12508 * This process logically involves adjusting tp->rcv_wnd as data is
12509 * presented to the user (this happens in tcp_usrreq.c, case
12510 * PRU_RCVD). If a FIN has already been received on this connection
12511 * then we just ignore the text.
12513 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
12514 IS_FASTOPEN(tp->t_flags));
12515 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
12516 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12517 tcp_seq save_start = th->th_seq;
12518 tcp_seq save_rnxt = tp->rcv_nxt;
12519 int save_tlen = tlen;
12521 m_adj(m, drop_hdrlen); /* delayed header drop */
12523 * Insert segment which includes th into TCP reassembly
12524 * queue with control block tp. Set thflags to whether
12525 * reassembly now includes a segment with FIN. This handles
12526 * the common case inline (segment is the next to be
12527 * received on an established connection, and the queue is
12528 * empty), avoiding linkage into and removal from the queue
12529 * and repetition of various conversions. Set DELACK for
12530 * segments received in order, but ack immediately when
12531 * segments are out of order (so fast retransmit can work).
12533 if (th->th_seq == tp->rcv_nxt &&
12535 (TCPS_HAVEESTABLISHED(tp->t_state) ||
12537 #ifdef NETFLIX_SB_LIMITS
12538 u_int mcnt, appended;
12540 if (so->so_rcv.sb_shlim) {
12541 mcnt = m_memcnt(m);
12543 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12544 CFO_NOSLEEP, NULL) == false) {
12545 counter_u64_add(tcp_sb_shlim_fails, 1);
12551 rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
12552 tp->rcv_nxt += tlen;
12554 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12555 (tp->t_fbyte_in == 0)) {
12556 tp->t_fbyte_in = ticks;
12557 if (tp->t_fbyte_in == 0)
12558 tp->t_fbyte_in = 1;
12559 if (tp->t_fbyte_out && tp->t_fbyte_in)
12560 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12562 thflags = tcp_get_flags(th) & TH_FIN;
12563 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12564 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12565 SOCKBUF_LOCK(&so->so_rcv);
12566 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12569 #ifdef NETFLIX_SB_LIMITS
12572 sbappendstream_locked(&so->so_rcv, m, 0);
12574 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12575 /* NB: sorwakeup_locked() does an implicit unlock. */
12576 sorwakeup_locked(so);
12577 #ifdef NETFLIX_SB_LIMITS
12578 if (so->so_rcv.sb_shlim && appended != mcnt)
12579 counter_fo_release(so->so_rcv.sb_shlim,
12584 * XXX: Due to the header drop above "th" is
12585 * theoretically invalid by now. Fortunately
12586 * m_adj() doesn't actually frees any mbufs when
12587 * trimming from the head.
12589 tcp_seq temp = save_start;
12591 thflags = tcp_reass(tp, th, &temp, &tlen, m);
12592 tp->t_flags |= TF_ACKNOW;
12593 if (tp->t_flags & TF_WAKESOR) {
12594 tp->t_flags &= ~TF_WAKESOR;
12595 /* NB: sorwakeup_locked() does an implicit unlock. */
12596 sorwakeup_locked(so);
12599 if ((tp->t_flags & TF_SACK_PERMIT) &&
12601 TCPS_HAVEESTABLISHED(tp->t_state)) {
12602 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
12604 * DSACK actually handled in the fastpath
12607 tcp_update_sack_list(tp, save_start,
12608 save_start + save_tlen);
12609 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
12610 if ((tp->rcv_numsacks >= 1) &&
12611 (tp->sackblks[0].end == save_start)) {
12613 * Partial overlap, recorded at todrop
12616 tcp_update_sack_list(tp,
12617 tp->sackblks[0].start,
12618 tp->sackblks[0].end);
12620 tcp_update_dsack_list(tp, save_start,
12621 save_start + save_tlen);
12623 } else if (tlen >= save_tlen) {
12624 /* Update of sackblks. */
12625 tcp_update_dsack_list(tp, save_start,
12626 save_start + save_tlen);
12627 } else if (tlen > 0) {
12628 tcp_update_dsack_list(tp, save_start,
12629 save_start + tlen);
12634 thflags &= ~TH_FIN;
12638 * If FIN is received ACK the FIN and let the user know that the
12639 * connection is closing.
12641 if (thflags & TH_FIN) {
12642 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12643 /* The socket upcall is handled by socantrcvmore. */
12646 * If connection is half-synchronized (ie NEEDSYN
12647 * flag on) then delay ACK, so it may be piggybacked
12648 * when SYN is sent. Otherwise, since we received a
12649 * FIN then no more input can be expected, send ACK
12652 if (tp->t_flags & TF_NEEDSYN) {
12653 rack_timer_cancel(tp, rack,
12654 rack->r_ctl.rc_rcvtime, __LINE__);
12655 tp->t_flags |= TF_DELACK;
12657 tp->t_flags |= TF_ACKNOW;
12661 switch (tp->t_state) {
12663 * In SYN_RECEIVED and ESTABLISHED STATES enter the
12664 * CLOSE_WAIT state.
12666 case TCPS_SYN_RECEIVED:
12667 tp->t_starttime = ticks;
12669 case TCPS_ESTABLISHED:
12670 rack_timer_cancel(tp, rack,
12671 rack->r_ctl.rc_rcvtime, __LINE__);
12672 tcp_state_change(tp, TCPS_CLOSE_WAIT);
12676 * If still in FIN_WAIT_1 STATE FIN has not been
12677 * acked so enter the CLOSING state.
12679 case TCPS_FIN_WAIT_1:
12680 rack_timer_cancel(tp, rack,
12681 rack->r_ctl.rc_rcvtime, __LINE__);
12682 tcp_state_change(tp, TCPS_CLOSING);
12686 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12687 * starting the time-wait timer, turning off the
12688 * other standard timers.
12690 case TCPS_FIN_WAIT_2:
12691 rack_timer_cancel(tp, rack,
12692 rack->r_ctl.rc_rcvtime, __LINE__);
12698 * Return any desired output.
12700 if ((tp->t_flags & TF_ACKNOW) ||
12701 (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
12702 rack->r_wanted_output = 1;
12708 * Here nothing is really faster, its just that we
12709 * have broken out the fast-data path also just like
12713 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
12714 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12715 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
12718 int32_t newsize = 0; /* automatic sockbuf scaling */
12719 struct tcp_rack *rack;
12720 #ifdef NETFLIX_SB_LIMITS
12721 u_int mcnt, appended;
12725 * If last ACK falls within this segment's sequence numbers, record
12726 * the timestamp. NOTE that the test is modified according to the
12727 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12729 if (__predict_false(th->th_seq != tp->rcv_nxt)) {
12732 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
12735 if (tiwin && tiwin != tp->snd_wnd) {
12738 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
12741 if (__predict_false((to->to_flags & TOF_TS) &&
12742 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
12745 if (__predict_false((th->th_ack != tp->snd_una))) {
12748 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
12751 if ((to->to_flags & TOF_TS) != 0 &&
12752 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12753 tp->ts_recent_age = tcp_ts_getticks();
12754 tp->ts_recent = to->to_tsval;
12756 rack = (struct tcp_rack *)tp->t_fb_ptr;
12758 * This is a pure, in-sequence data packet with nothing on the
12759 * reassembly queue and we have enough buffer space to take it.
12761 nsegs = max(1, m->m_pkthdr.lro_nsegs);
12763 #ifdef NETFLIX_SB_LIMITS
12764 if (so->so_rcv.sb_shlim) {
12765 mcnt = m_memcnt(m);
12767 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
12768 CFO_NOSLEEP, NULL) == false) {
12769 counter_u64_add(tcp_sb_shlim_fails, 1);
12775 /* Clean receiver SACK report if present */
12776 if (tp->rcv_numsacks)
12777 tcp_clean_sackreport(tp);
12778 KMOD_TCPSTAT_INC(tcps_preddat);
12779 tp->rcv_nxt += tlen;
12781 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
12782 (tp->t_fbyte_in == 0)) {
12783 tp->t_fbyte_in = ticks;
12784 if (tp->t_fbyte_in == 0)
12785 tp->t_fbyte_in = 1;
12786 if (tp->t_fbyte_out && tp->t_fbyte_in)
12787 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
12790 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
12792 tp->snd_wl1 = th->th_seq;
12794 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
12796 tp->rcv_up = tp->rcv_nxt;
12797 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
12798 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
12799 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
12801 /* Add data to socket buffer. */
12802 SOCKBUF_LOCK(&so->so_rcv);
12803 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
12807 * Set new socket buffer size. Give up when limit is
12811 if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
12812 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
12813 m_adj(m, drop_hdrlen); /* delayed header drop */
12814 #ifdef NETFLIX_SB_LIMITS
12817 sbappendstream_locked(&so->so_rcv, m, 0);
12818 ctf_calc_rwin(so, tp);
12820 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1);
12821 /* NB: sorwakeup_locked() does an implicit unlock. */
12822 sorwakeup_locked(so);
12823 #ifdef NETFLIX_SB_LIMITS
12824 if (so->so_rcv.sb_shlim && mcnt != appended)
12825 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
12827 rack_handle_delayed_ack(tp, rack, tlen, 0);
12828 if (tp->snd_una == tp->snd_max)
12829 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
12834 * This subfunction is used to try to highly optimize the
12835 * fast path. We again allow window updates that are
12836 * in sequence to remain in the fast-path. We also add
12837 * in the __predict's to attempt to help the compiler.
12838 * Note that if we return a 0, then we can *not* process
12839 * it and the caller should push the packet into the
12843 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
12844 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
12845 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
12849 int32_t under_pacing = 0;
12850 struct tcp_rack *rack;
12852 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
12853 /* Old ack, behind (or duplicate to) the last one rcv'd */
12856 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
12857 /* Above what we have sent? */
12860 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
12861 /* We are retransmitting */
12864 if (__predict_false(tiwin == 0)) {
12868 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
12869 /* We need a SYN or a FIN, unlikely.. */
12872 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
12873 /* Timestamp is behind .. old ack with seq wrap? */
12876 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
12877 /* Still recovering */
12880 rack = (struct tcp_rack *)tp->t_fb_ptr;
12881 if (rack->r_ctl.rc_sacked) {
12882 /* We have sack holes on our scoreboard */
12885 /* Ok if we reach here, we can process a fast-ack */
12886 if (rack->gp_ready &&
12887 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
12890 nsegs = max(1, m->m_pkthdr.lro_nsegs);
12891 rack_log_ack(tp, to, th, 0, 0, NULL, NULL);
12892 /* Did the window get updated? */
12893 if (tiwin != tp->snd_wnd) {
12894 tp->snd_wnd = tiwin;
12895 rack_validate_fo_sendwin_up(tp, rack);
12896 tp->snd_wl1 = th->th_seq;
12897 if (tp->snd_wnd > tp->max_sndwnd)
12898 tp->max_sndwnd = tp->snd_wnd;
12900 /* Do we exit persists? */
12901 if ((rack->rc_in_persist != 0) &&
12902 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
12903 rack->r_ctl.rc_pace_min_segs))) {
12904 rack_exit_persist(tp, rack, cts);
12906 /* Do we enter persists? */
12907 if ((rack->rc_in_persist == 0) &&
12908 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
12909 TCPS_HAVEESTABLISHED(tp->t_state) &&
12910 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
12911 sbavail(&tptosocket(tp)->so_snd) &&
12912 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
12914 * Here the rwnd is less than
12915 * the pacing size, we are established,
12916 * nothing is outstanding, and there is
12917 * data to send. Enter persists.
12919 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack);
12922 * If last ACK falls within this segment's sequence numbers, record
12923 * the timestamp. NOTE that the test is modified according to the
12924 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
12926 if ((to->to_flags & TOF_TS) != 0 &&
12927 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12928 tp->ts_recent_age = tcp_ts_getticks();
12929 tp->ts_recent = to->to_tsval;
12932 * This is a pure ack for outstanding data.
12934 KMOD_TCPSTAT_INC(tcps_predack);
12937 * "bad retransmit" recovery.
12939 if ((tp->t_flags & TF_PREVVALID) &&
12940 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
12941 tp->t_flags &= ~TF_PREVVALID;
12942 if (tp->t_rxtshift == 1 &&
12943 (int)(ticks - tp->t_badrxtwin) < 0)
12944 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__);
12947 * Recalculate the transmit timer / rtt.
12949 * Some boxes send broken timestamp replies during the SYN+ACK
12950 * phase, ignore timestamps of 0 or we could calculate a huge RTT
12951 * and blow up the retransmit timer.
12953 acked = BYTES_THIS_ACK(tp, th);
12956 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
12957 hhook_run_tcp_est_in(tp, th, to);
12959 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
12960 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
12962 struct mbuf *mfree;
12964 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0);
12965 SOCKBUF_LOCK(&so->so_snd);
12966 mfree = sbcut_locked(&so->so_snd, acked);
12967 tp->snd_una = th->th_ack;
12968 /* Note we want to hold the sb lock through the sendmap adjust */
12969 rack_adjust_sendmap_head(rack, &so->so_snd);
12970 /* Wake up the socket if we have room to write more */
12971 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
12972 sowwakeup_locked(so);
12974 tp->t_rxtshift = 0;
12975 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
12976 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
12977 rack->rc_tlp_in_progress = 0;
12978 rack->r_ctl.rc_tlp_cnt_out = 0;
12980 * If it is the RXT timer we want to
12981 * stop it, so we can restart a TLP.
12983 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
12984 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
12986 #ifdef TCP_REQUEST_TRK
12987 rack_req_check_for_comp(rack, th->th_ack);
12991 * Let the congestion control algorithm update congestion control
12992 * related information. This typically means increasing the
12993 * congestion window.
12995 if (tp->snd_wnd < ctf_outstanding(tp)) {
12996 /* The peer collapsed the window */
12997 rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__);
12998 } else if (rack->rc_has_collapsed)
12999 rack_un_collapse_window(rack, __LINE__);
13000 if ((rack->r_collapse_point_valid) &&
13001 (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point)))
13002 rack->r_collapse_point_valid = 0;
13004 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
13006 tp->snd_wl2 = th->th_ack;
13009 /* ND6_HINT(tp); *//* Some progress has been made. */
13012 * If all outstanding data are acked, stop retransmit timer,
13013 * otherwise restart timer using current (possibly backed-off)
13014 * value. If process is waiting for space, wakeup/selwakeup/signal.
13015 * If data are ready to send, let tcp_output decide between more
13016 * output or persist.
13018 if (under_pacing &&
13019 (rack->use_fixed_rate == 0) &&
13020 (rack->in_probe_rtt == 0) &&
13021 rack->rc_gp_dyn_mul &&
13022 rack->rc_always_pace) {
13023 /* Check if we are dragging bottom */
13024 rack_check_bottom_drag(tp, rack, so);
13026 if (tp->snd_una == tp->snd_max) {
13027 tp->t_flags &= ~TF_PREVVALID;
13028 rack->r_ctl.retran_during_recovery = 0;
13029 rack->rc_suspicious = 0;
13030 rack->r_ctl.dsack_byte_cnt = 0;
13031 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
13032 if (rack->r_ctl.rc_went_idle_time == 0)
13033 rack->r_ctl.rc_went_idle_time = 1;
13034 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
13035 if (sbavail(&tptosocket(tp)->so_snd) == 0)
13037 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
13039 if (acked && rack->r_fast_output)
13040 rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked);
13041 if (sbavail(&so->so_snd)) {
13042 rack->r_wanted_output = 1;
13048 * Return value of 1, the TCB is unlocked and most
13049 * likely gone, return value of 0, the TCP is still
13053 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
13054 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13055 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13057 int32_t ret_val = 0;
13059 int32_t ourfinisacked = 0;
13060 struct tcp_rack *rack;
13062 INP_WLOCK_ASSERT(tptoinpcb(tp));
13064 ctf_calc_rwin(so, tp);
13066 * If the state is SYN_SENT: if seg contains an ACK, but not for our
13067 * SYN, drop the input. if seg contains a RST, then drop the
13068 * connection. if seg does not contain SYN, then drop it. Otherwise
13069 * this is an acceptable SYN segment initialize tp->rcv_nxt and
13070 * tp->irs if seg contains ack then advance tp->snd_una if seg
13071 * contains an ECE and ECN support is enabled, the stream is ECN
13072 * capable. if SYN has been acked change to ESTABLISHED else
13073 * SYN_RCVD state arrange for segment to be acked (eventually)
13074 * continue processing rest of data/controls.
13076 if ((thflags & TH_ACK) &&
13077 (SEQ_LEQ(th->th_ack, tp->iss) ||
13078 SEQ_GT(th->th_ack, tp->snd_max))) {
13079 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13080 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13083 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
13084 TCP_PROBE5(connect__refused, NULL, tp,
13085 mtod(m, const char *), tp, th);
13086 tp = tcp_drop(tp, ECONNREFUSED);
13087 ctf_do_drop(m, tp);
13090 if (thflags & TH_RST) {
13091 ctf_do_drop(m, tp);
13094 if (!(thflags & TH_SYN)) {
13095 ctf_do_drop(m, tp);
13098 tp->irs = th->th_seq;
13099 tcp_rcvseqinit(tp);
13100 rack = (struct tcp_rack *)tp->t_fb_ptr;
13101 if (thflags & TH_ACK) {
13102 int tfo_partial = 0;
13104 KMOD_TCPSTAT_INC(tcps_connects);
13107 mac_socketpeer_set_from_mbuf(m, so);
13109 /* Do window scaling on this connection? */
13110 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13111 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13112 tp->rcv_scale = tp->request_r_scale;
13114 tp->rcv_adv += min(tp->rcv_wnd,
13115 TCP_MAXWIN << tp->rcv_scale);
13117 * If not all the data that was sent in the TFO SYN
13118 * has been acked, resend the remainder right away.
13120 if (IS_FASTOPEN(tp->t_flags) &&
13121 (tp->snd_una != tp->snd_max)) {
13122 tp->snd_nxt = th->th_ack;
13126 * If there's data, delay ACK; if there's also a FIN ACKNOW
13127 * will be turned on later.
13129 if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
13130 rack_timer_cancel(tp, rack,
13131 rack->r_ctl.rc_rcvtime, __LINE__);
13132 tp->t_flags |= TF_DELACK;
13134 rack->r_wanted_output = 1;
13135 tp->t_flags |= TF_ACKNOW;
13138 tcp_ecn_input_syn_sent(tp, thflags, iptos);
13140 if (SEQ_GT(th->th_ack, tp->snd_una)) {
13142 * We advance snd_una for the
13143 * fast open case. If th_ack is
13144 * acknowledging data beyond
13145 * snd_una we can't just call
13146 * ack-processing since the
13147 * data stream in our send-map
13148 * will start at snd_una + 1 (one
13149 * beyond the SYN). If its just
13150 * equal we don't need to do that
13151 * and there is no send_map.
13156 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
13157 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
13159 tp->t_starttime = ticks;
13160 if (tp->t_flags & TF_NEEDFIN) {
13161 tcp_state_change(tp, TCPS_FIN_WAIT_1);
13162 tp->t_flags &= ~TF_NEEDFIN;
13163 thflags &= ~TH_SYN;
13165 tcp_state_change(tp, TCPS_ESTABLISHED);
13166 TCP_PROBE5(connect__established, NULL, tp,
13167 mtod(m, const char *), tp, th);
13168 rack_cc_conn_init(tp);
13172 * Received initial SYN in SYN-SENT[*] state => simultaneous
13173 * open. If segment contains CC option and there is a
13174 * cached CC, apply TAO test. If it succeeds, connection is *
13175 * half-synchronized. Otherwise, do 3-way handshake:
13176 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
13177 * there was no CC option, clear cached CC value.
13179 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
13180 tcp_state_change(tp, TCPS_SYN_RECEIVED);
13183 * Advance th->th_seq to correspond to first data byte. If data,
13184 * trim to stay within window, dropping FIN if necessary.
13187 if (tlen > tp->rcv_wnd) {
13188 todrop = tlen - tp->rcv_wnd;
13190 tlen = tp->rcv_wnd;
13191 thflags &= ~TH_FIN;
13192 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
13193 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
13195 tp->snd_wl1 = th->th_seq - 1;
13196 tp->rcv_up = th->th_seq;
13198 * Client side of transaction: already sent SYN and data. If the
13199 * remote host used T/TCP to validate the SYN, our data will be
13200 * ACK'd; if so, enter normal data segment processing in the middle
13201 * of step 5, ack processing. Otherwise, goto step 6.
13203 if (thflags & TH_ACK) {
13204 /* For syn-sent we need to possibly update the rtt */
13205 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13208 mcts = tcp_ts_getticks();
13209 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13210 if (!tp->t_rttlow || tp->t_rttlow > t)
13212 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4);
13213 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13214 tcp_rack_xmit_timer_commit(rack, tp);
13216 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
13218 /* We may have changed to FIN_WAIT_1 above */
13219 if (tp->t_state == TCPS_FIN_WAIT_1) {
13221 * In FIN_WAIT_1 STATE in addition to the processing
13222 * for the ESTABLISHED state if our FIN is now
13223 * acknowledged then enter FIN_WAIT_2.
13225 if (ourfinisacked) {
13227 * If we can't receive any more data, then
13228 * closing user can proceed. Starting the
13229 * timer is contrary to the specification,
13230 * but if we don't get a FIN we'll hang
13233 * XXXjl: we should release the tp also, and
13234 * use a compressed state.
13236 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13237 soisdisconnected(so);
13238 tcp_timer_activate(tp, TT_2MSL,
13239 (tcp_fast_finwait2_recycle ?
13240 tcp_finwait2_timeout :
13243 tcp_state_change(tp, TCPS_FIN_WAIT_2);
13247 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13248 tiwin, thflags, nxt_pkt));
13252 * Return value of 1, the TCB is unlocked and most
13253 * likely gone, return value of 0, the TCP is still
13257 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
13258 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13259 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13261 struct tcp_rack *rack;
13262 int32_t ret_val = 0;
13263 int32_t ourfinisacked = 0;
13265 ctf_calc_rwin(so, tp);
13266 if ((thflags & TH_ACK) &&
13267 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
13268 SEQ_GT(th->th_ack, tp->snd_max))) {
13269 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13270 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13273 rack = (struct tcp_rack *)tp->t_fb_ptr;
13274 if (IS_FASTOPEN(tp->t_flags)) {
13276 * When a TFO connection is in SYN_RECEIVED, the
13277 * only valid packets are the initial SYN, a
13278 * retransmit/copy of the initial SYN (possibly with
13279 * a subset of the original data), a valid ACK, a
13282 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
13283 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13284 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13286 } else if (thflags & TH_SYN) {
13287 /* non-initial SYN is ignored */
13288 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
13289 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
13290 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
13291 ctf_do_drop(m, NULL);
13294 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
13295 ctf_do_drop(m, NULL);
13300 if ((thflags & TH_RST) ||
13301 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13302 return (__ctf_process_rst(m, th, so, tp,
13303 &rack->r_ctl.challenge_ack_ts,
13304 &rack->r_ctl.challenge_ack_cnt));
13306 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13307 * it's less than ts_recent, drop it.
13309 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13310 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13311 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13315 * In the SYN-RECEIVED state, validate that the packet belongs to
13316 * this connection before trimming the data to fit the receive
13317 * window. Check the sequence number versus IRS since we know the
13318 * sequence numbers haven't wrapped. This is a partial fix for the
13319 * "LAND" DoS attack.
13321 if (SEQ_LT(th->th_seq, tp->irs)) {
13322 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
13323 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13326 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13327 &rack->r_ctl.challenge_ack_ts,
13328 &rack->r_ctl.challenge_ack_cnt)) {
13332 * If last ACK falls within this segment's sequence numbers, record
13333 * its timestamp. NOTE: 1) That the test incorporates suggestions
13334 * from the latest proposal of the tcplw@cray.com list (Braden
13335 * 1993/04/26). 2) That updating only on newer timestamps interferes
13336 * with our earlier PAWS tests, so this check should be solely
13337 * predicated on the sequence space of this segment. 3) That we
13338 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13339 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13340 * SEG.Len, This modified check allows us to overcome RFC1323's
13341 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13342 * p.869. In such cases, we can still calculate the RTT correctly
13343 * when RCV.NXT == Last.ACK.Sent.
13345 if ((to->to_flags & TOF_TS) != 0 &&
13346 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13347 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13348 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13349 tp->ts_recent_age = tcp_ts_getticks();
13350 tp->ts_recent = to->to_tsval;
13352 tp->snd_wnd = tiwin;
13353 rack_validate_fo_sendwin_up(tp, rack);
13355 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
13356 * is on (half-synchronized state), then queue data for later
13357 * processing; else drop segment and return.
13359 if ((thflags & TH_ACK) == 0) {
13360 if (IS_FASTOPEN(tp->t_flags)) {
13361 rack_cc_conn_init(tp);
13363 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13364 tiwin, thflags, nxt_pkt));
13366 KMOD_TCPSTAT_INC(tcps_connects);
13367 if (tp->t_flags & TF_SONOTCONN) {
13368 tp->t_flags &= ~TF_SONOTCONN;
13371 /* Do window scaling? */
13372 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
13373 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
13374 tp->rcv_scale = tp->request_r_scale;
13377 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
13380 tp->t_starttime = ticks;
13381 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
13382 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
13383 tp->t_tfo_pending = NULL;
13385 if (tp->t_flags & TF_NEEDFIN) {
13386 tcp_state_change(tp, TCPS_FIN_WAIT_1);
13387 tp->t_flags &= ~TF_NEEDFIN;
13389 tcp_state_change(tp, TCPS_ESTABLISHED);
13390 TCP_PROBE5(accept__established, NULL, tp,
13391 mtod(m, const char *), tp, th);
13393 * TFO connections call cc_conn_init() during SYN
13394 * processing. Calling it again here for such connections
13395 * is not harmless as it would undo the snd_cwnd reduction
13396 * that occurs when a TFO SYN|ACK is retransmitted.
13398 if (!IS_FASTOPEN(tp->t_flags))
13399 rack_cc_conn_init(tp);
13402 * Account for the ACK of our SYN prior to
13403 * regular ACK processing below, except for
13404 * simultaneous SYN, which is handled later.
13406 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
13409 * If segment contains data or ACK, will call tcp_reass() later; if
13410 * not, do so now to pass queued data to user.
13412 if (tlen == 0 && (thflags & TH_FIN) == 0) {
13413 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
13415 if (tp->t_flags & TF_WAKESOR) {
13416 tp->t_flags &= ~TF_WAKESOR;
13417 /* NB: sorwakeup_locked() does an implicit unlock. */
13418 sorwakeup_locked(so);
13421 tp->snd_wl1 = th->th_seq - 1;
13422 /* For syn-recv we need to possibly update the rtt */
13423 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
13426 mcts = tcp_ts_getticks();
13427 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC;
13428 if (!tp->t_rttlow || tp->t_rttlow > t)
13430 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5);
13431 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2);
13432 tcp_rack_xmit_timer_commit(rack, tp);
13434 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
13437 if (tp->t_state == TCPS_FIN_WAIT_1) {
13438 /* We could have went to FIN_WAIT_1 (or EST) above */
13440 * In FIN_WAIT_1 STATE in addition to the processing for the
13441 * ESTABLISHED state if our FIN is now acknowledged then
13442 * enter FIN_WAIT_2.
13444 if (ourfinisacked) {
13446 * If we can't receive any more data, then closing
13447 * user can proceed. Starting the timer is contrary
13448 * to the specification, but if we don't get a FIN
13449 * we'll hang forever.
13451 * XXXjl: we should release the tp also, and use a
13452 * compressed state.
13454 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13455 soisdisconnected(so);
13456 tcp_timer_activate(tp, TT_2MSL,
13457 (tcp_fast_finwait2_recycle ?
13458 tcp_finwait2_timeout :
13461 tcp_state_change(tp, TCPS_FIN_WAIT_2);
13464 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13465 tiwin, thflags, nxt_pkt));
13469 * Return value of 1, the TCB is unlocked and most
13470 * likely gone, return value of 0, the TCP is still
13474 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
13475 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13476 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13478 int32_t ret_val = 0;
13479 struct tcp_rack *rack;
13482 * Header prediction: check for the two common cases of a
13483 * uni-directional data xfer. If the packet has no control flags,
13484 * is in-sequence, the window didn't change and we're not
13485 * retransmitting, it's a candidate. If the length is zero and the
13486 * ack moved forward, we're the sender side of the xfer. Just free
13487 * the data acked & wake any higher level process that was blocked
13488 * waiting for space. If the length is non-zero and the ack didn't
13489 * move, we're the receiver side. If we're getting packets in-order
13490 * (the reassembly queue is empty), add the data toc The socket
13491 * buffer and note that we need a delayed ack. Make sure that the
13492 * hidden state-flags are also off. Since we check for
13493 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
13495 rack = (struct tcp_rack *)tp->t_fb_ptr;
13496 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
13497 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
13498 __predict_true(SEGQ_EMPTY(tp)) &&
13499 __predict_true(th->th_seq == tp->rcv_nxt)) {
13501 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
13502 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
13506 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
13507 tiwin, nxt_pkt, iptos)) {
13512 ctf_calc_rwin(so, tp);
13514 if ((thflags & TH_RST) ||
13515 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13516 return (__ctf_process_rst(m, th, so, tp,
13517 &rack->r_ctl.challenge_ack_ts,
13518 &rack->r_ctl.challenge_ack_cnt));
13521 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13522 * synchronized state.
13524 if (thflags & TH_SYN) {
13525 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13529 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13530 * it's less than ts_recent, drop it.
13532 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13533 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13534 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13537 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13538 &rack->r_ctl.challenge_ack_ts,
13539 &rack->r_ctl.challenge_ack_cnt)) {
13543 * If last ACK falls within this segment's sequence numbers, record
13544 * its timestamp. NOTE: 1) That the test incorporates suggestions
13545 * from the latest proposal of the tcplw@cray.com list (Braden
13546 * 1993/04/26). 2) That updating only on newer timestamps interferes
13547 * with our earlier PAWS tests, so this check should be solely
13548 * predicated on the sequence space of this segment. 3) That we
13549 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13550 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13551 * SEG.Len, This modified check allows us to overcome RFC1323's
13552 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13553 * p.869. In such cases, we can still calculate the RTT correctly
13554 * when RCV.NXT == Last.ACK.Sent.
13556 if ((to->to_flags & TOF_TS) != 0 &&
13557 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13558 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13559 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13560 tp->ts_recent_age = tcp_ts_getticks();
13561 tp->ts_recent = to->to_tsval;
13564 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
13565 * is on (half-synchronized state), then queue data for later
13566 * processing; else drop segment and return.
13568 if ((thflags & TH_ACK) == 0) {
13569 if (tp->t_flags & TF_NEEDSYN) {
13570 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13571 tiwin, thflags, nxt_pkt));
13573 } else if (tp->t_flags & TF_ACKNOW) {
13574 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13575 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13578 ctf_do_drop(m, NULL);
13585 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
13588 if (sbavail(&so->so_snd)) {
13589 if (ctf_progress_timeout_check(tp, true)) {
13590 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
13591 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13595 /* State changes only happen in rack_process_data() */
13596 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13597 tiwin, thflags, nxt_pkt));
13601 * Return value of 1, the TCB is unlocked and most
13602 * likely gone, return value of 0, the TCP is still
13606 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
13607 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13608 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13610 int32_t ret_val = 0;
13611 struct tcp_rack *rack;
13613 rack = (struct tcp_rack *)tp->t_fb_ptr;
13614 ctf_calc_rwin(so, tp);
13615 if ((thflags & TH_RST) ||
13616 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13617 return (__ctf_process_rst(m, th, so, tp,
13618 &rack->r_ctl.challenge_ack_ts,
13619 &rack->r_ctl.challenge_ack_cnt));
13621 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13622 * synchronized state.
13624 if (thflags & TH_SYN) {
13625 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13629 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13630 * it's less than ts_recent, drop it.
13632 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13633 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13634 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13637 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13638 &rack->r_ctl.challenge_ack_ts,
13639 &rack->r_ctl.challenge_ack_cnt)) {
13643 * If last ACK falls within this segment's sequence numbers, record
13644 * its timestamp. NOTE: 1) That the test incorporates suggestions
13645 * from the latest proposal of the tcplw@cray.com list (Braden
13646 * 1993/04/26). 2) That updating only on newer timestamps interferes
13647 * with our earlier PAWS tests, so this check should be solely
13648 * predicated on the sequence space of this segment. 3) That we
13649 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13650 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13651 * SEG.Len, This modified check allows us to overcome RFC1323's
13652 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13653 * p.869. In such cases, we can still calculate the RTT correctly
13654 * when RCV.NXT == Last.ACK.Sent.
13656 if ((to->to_flags & TOF_TS) != 0 &&
13657 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13658 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13659 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13660 tp->ts_recent_age = tcp_ts_getticks();
13661 tp->ts_recent = to->to_tsval;
13664 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
13665 * is on (half-synchronized state), then queue data for later
13666 * processing; else drop segment and return.
13668 if ((thflags & TH_ACK) == 0) {
13669 if (tp->t_flags & TF_NEEDSYN) {
13670 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13671 tiwin, thflags, nxt_pkt));
13673 } else if (tp->t_flags & TF_ACKNOW) {
13674 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13675 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13678 ctf_do_drop(m, NULL);
13685 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
13688 if (sbavail(&so->so_snd)) {
13689 if (ctf_progress_timeout_check(tp, true)) {
13690 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13691 tp, tick, PROGRESS_DROP, __LINE__);
13692 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13696 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13697 tiwin, thflags, nxt_pkt));
13701 rack_check_data_after_close(struct mbuf *m,
13702 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
13704 struct tcp_rack *rack;
13706 rack = (struct tcp_rack *)tp->t_fb_ptr;
13707 if (rack->rc_allow_data_af_clo == 0) {
13709 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13710 /* tcp_close will kill the inp pre-log the Reset */
13711 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
13712 tp = tcp_close(tp);
13713 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
13714 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
13717 if (sbavail(&so->so_snd) == 0)
13719 /* Ok we allow data that is ignored and a followup reset */
13720 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
13721 tp->rcv_nxt = th->th_seq + *tlen;
13722 tp->t_flags2 |= TF2_DROP_AF_DATA;
13723 rack->r_wanted_output = 1;
13729 * Return value of 1, the TCB is unlocked and most
13730 * likely gone, return value of 0, the TCP is still
13734 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
13735 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13736 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13738 int32_t ret_val = 0;
13739 int32_t ourfinisacked = 0;
13740 struct tcp_rack *rack;
13742 rack = (struct tcp_rack *)tp->t_fb_ptr;
13743 ctf_calc_rwin(so, tp);
13745 if ((thflags & TH_RST) ||
13746 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13747 return (__ctf_process_rst(m, th, so, tp,
13748 &rack->r_ctl.challenge_ack_ts,
13749 &rack->r_ctl.challenge_ack_cnt));
13751 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13752 * synchronized state.
13754 if (thflags & TH_SYN) {
13755 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13759 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13760 * it's less than ts_recent, drop it.
13762 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13763 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13764 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13767 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13768 &rack->r_ctl.challenge_ack_ts,
13769 &rack->r_ctl.challenge_ack_cnt)) {
13773 * If new data are received on a connection after the user processes
13774 * are gone, then RST the other end.
13776 if ((tp->t_flags & TF_CLOSED) && tlen &&
13777 rack_check_data_after_close(m, tp, &tlen, th, so))
13780 * If last ACK falls within this segment's sequence numbers, record
13781 * its timestamp. NOTE: 1) That the test incorporates suggestions
13782 * from the latest proposal of the tcplw@cray.com list (Braden
13783 * 1993/04/26). 2) That updating only on newer timestamps interferes
13784 * with our earlier PAWS tests, so this check should be solely
13785 * predicated on the sequence space of this segment. 3) That we
13786 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13787 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13788 * SEG.Len, This modified check allows us to overcome RFC1323's
13789 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13790 * p.869. In such cases, we can still calculate the RTT correctly
13791 * when RCV.NXT == Last.ACK.Sent.
13793 if ((to->to_flags & TOF_TS) != 0 &&
13794 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13795 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13796 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13797 tp->ts_recent_age = tcp_ts_getticks();
13798 tp->ts_recent = to->to_tsval;
13801 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
13802 * is on (half-synchronized state), then queue data for later
13803 * processing; else drop segment and return.
13805 if ((thflags & TH_ACK) == 0) {
13806 if (tp->t_flags & TF_NEEDSYN) {
13807 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13808 tiwin, thflags, nxt_pkt));
13809 } else if (tp->t_flags & TF_ACKNOW) {
13810 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13811 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13814 ctf_do_drop(m, NULL);
13821 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
13824 if (ourfinisacked) {
13826 * If we can't receive any more data, then closing user can
13827 * proceed. Starting the timer is contrary to the
13828 * specification, but if we don't get a FIN we'll hang
13831 * XXXjl: we should release the tp also, and use a
13832 * compressed state.
13834 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
13835 soisdisconnected(so);
13836 tcp_timer_activate(tp, TT_2MSL,
13837 (tcp_fast_finwait2_recycle ?
13838 tcp_finwait2_timeout :
13841 tcp_state_change(tp, TCPS_FIN_WAIT_2);
13843 if (sbavail(&so->so_snd)) {
13844 if (ctf_progress_timeout_check(tp, true)) {
13845 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13846 tp, tick, PROGRESS_DROP, __LINE__);
13847 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13851 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13852 tiwin, thflags, nxt_pkt));
13856 * Return value of 1, the TCB is unlocked and most
13857 * likely gone, return value of 0, the TCP is still
13861 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
13862 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13863 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13865 int32_t ret_val = 0;
13866 int32_t ourfinisacked = 0;
13867 struct tcp_rack *rack;
13869 rack = (struct tcp_rack *)tp->t_fb_ptr;
13870 ctf_calc_rwin(so, tp);
13872 if ((thflags & TH_RST) ||
13873 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13874 return (__ctf_process_rst(m, th, so, tp,
13875 &rack->r_ctl.challenge_ack_ts,
13876 &rack->r_ctl.challenge_ack_cnt));
13878 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13879 * synchronized state.
13881 if (thflags & TH_SYN) {
13882 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13886 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
13887 * it's less than ts_recent, drop it.
13889 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
13890 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
13891 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
13894 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
13895 &rack->r_ctl.challenge_ack_ts,
13896 &rack->r_ctl.challenge_ack_cnt)) {
13900 * If new data are received on a connection after the user processes
13901 * are gone, then RST the other end.
13903 if ((tp->t_flags & TF_CLOSED) && tlen &&
13904 rack_check_data_after_close(m, tp, &tlen, th, so))
13907 * If last ACK falls within this segment's sequence numbers, record
13908 * its timestamp. NOTE: 1) That the test incorporates suggestions
13909 * from the latest proposal of the tcplw@cray.com list (Braden
13910 * 1993/04/26). 2) That updating only on newer timestamps interferes
13911 * with our earlier PAWS tests, so this check should be solely
13912 * predicated on the sequence space of this segment. 3) That we
13913 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
13914 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
13915 * SEG.Len, This modified check allows us to overcome RFC1323's
13916 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
13917 * p.869. In such cases, we can still calculate the RTT correctly
13918 * when RCV.NXT == Last.ACK.Sent.
13920 if ((to->to_flags & TOF_TS) != 0 &&
13921 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
13922 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
13923 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
13924 tp->ts_recent_age = tcp_ts_getticks();
13925 tp->ts_recent = to->to_tsval;
13928 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
13929 * is on (half-synchronized state), then queue data for later
13930 * processing; else drop segment and return.
13932 if ((thflags & TH_ACK) == 0) {
13933 if (tp->t_flags & TF_NEEDSYN) {
13934 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13935 tiwin, thflags, nxt_pkt));
13936 } else if (tp->t_flags & TF_ACKNOW) {
13937 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
13938 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
13941 ctf_do_drop(m, NULL);
13948 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
13951 if (ourfinisacked) {
13956 if (sbavail(&so->so_snd)) {
13957 if (ctf_progress_timeout_check(tp, true)) {
13958 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
13959 tp, tick, PROGRESS_DROP, __LINE__);
13960 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
13964 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
13965 tiwin, thflags, nxt_pkt));
13969 * Return value of 1, the TCB is unlocked and most
13970 * likely gone, return value of 0, the TCP is still
13974 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
13975 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
13976 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
13978 int32_t ret_val = 0;
13979 int32_t ourfinisacked = 0;
13980 struct tcp_rack *rack;
13982 rack = (struct tcp_rack *)tp->t_fb_ptr;
13983 ctf_calc_rwin(so, tp);
13985 if ((thflags & TH_RST) ||
13986 (tp->t_fin_is_rst && (thflags & TH_FIN)))
13987 return (__ctf_process_rst(m, th, so, tp,
13988 &rack->r_ctl.challenge_ack_ts,
13989 &rack->r_ctl.challenge_ack_cnt));
13991 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
13992 * synchronized state.
13994 if (thflags & TH_SYN) {
13995 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
13999 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14000 * it's less than ts_recent, drop it.
14002 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14003 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14004 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14007 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14008 &rack->r_ctl.challenge_ack_ts,
14009 &rack->r_ctl.challenge_ack_cnt)) {
14013 * If new data are received on a connection after the user processes
14014 * are gone, then RST the other end.
14016 if ((tp->t_flags & TF_CLOSED) && tlen &&
14017 rack_check_data_after_close(m, tp, &tlen, th, so))
14020 * If last ACK falls within this segment's sequence numbers, record
14021 * its timestamp. NOTE: 1) That the test incorporates suggestions
14022 * from the latest proposal of the tcplw@cray.com list (Braden
14023 * 1993/04/26). 2) That updating only on newer timestamps interferes
14024 * with our earlier PAWS tests, so this check should be solely
14025 * predicated on the sequence space of this segment. 3) That we
14026 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14027 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14028 * SEG.Len, This modified check allows us to overcome RFC1323's
14029 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14030 * p.869. In such cases, we can still calculate the RTT correctly
14031 * when RCV.NXT == Last.ACK.Sent.
14033 if ((to->to_flags & TOF_TS) != 0 &&
14034 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14035 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14036 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14037 tp->ts_recent_age = tcp_ts_getticks();
14038 tp->ts_recent = to->to_tsval;
14041 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
14042 * is on (half-synchronized state), then queue data for later
14043 * processing; else drop segment and return.
14045 if ((thflags & TH_ACK) == 0) {
14046 if (tp->t_flags & TF_NEEDSYN) {
14047 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14048 tiwin, thflags, nxt_pkt));
14049 } else if (tp->t_flags & TF_ACKNOW) {
14050 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14051 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14054 ctf_do_drop(m, NULL);
14059 * case TCPS_LAST_ACK: Ack processing.
14061 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
14064 if (ourfinisacked) {
14065 tp = tcp_close(tp);
14066 ctf_do_drop(m, tp);
14069 if (sbavail(&so->so_snd)) {
14070 if (ctf_progress_timeout_check(tp, true)) {
14071 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14072 tp, tick, PROGRESS_DROP, __LINE__);
14073 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14077 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14078 tiwin, thflags, nxt_pkt));
14082 * Return value of 1, the TCB is unlocked and most
14083 * likely gone, return value of 0, the TCP is still
14087 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
14088 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
14089 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
14091 int32_t ret_val = 0;
14092 int32_t ourfinisacked = 0;
14093 struct tcp_rack *rack;
14095 rack = (struct tcp_rack *)tp->t_fb_ptr;
14096 ctf_calc_rwin(so, tp);
14098 /* Reset receive buffer auto scaling when not in bulk receive mode. */
14099 if ((thflags & TH_RST) ||
14100 (tp->t_fin_is_rst && (thflags & TH_FIN)))
14101 return (__ctf_process_rst(m, th, so, tp,
14102 &rack->r_ctl.challenge_ack_ts,
14103 &rack->r_ctl.challenge_ack_cnt));
14105 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
14106 * synchronized state.
14108 if (thflags & TH_SYN) {
14109 ctf_challenge_ack(m, th, tp, iptos, &ret_val);
14113 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
14114 * it's less than ts_recent, drop it.
14116 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
14117 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
14118 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
14121 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val,
14122 &rack->r_ctl.challenge_ack_ts,
14123 &rack->r_ctl.challenge_ack_cnt)) {
14127 * If new data are received on a connection after the user processes
14128 * are gone, then RST the other end.
14130 if ((tp->t_flags & TF_CLOSED) && tlen &&
14131 rack_check_data_after_close(m, tp, &tlen, th, so))
14134 * If last ACK falls within this segment's sequence numbers, record
14135 * its timestamp. NOTE: 1) That the test incorporates suggestions
14136 * from the latest proposal of the tcplw@cray.com list (Braden
14137 * 1993/04/26). 2) That updating only on newer timestamps interferes
14138 * with our earlier PAWS tests, so this check should be solely
14139 * predicated on the sequence space of this segment. 3) That we
14140 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
14141 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
14142 * SEG.Len, This modified check allows us to overcome RFC1323's
14143 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
14144 * p.869. In such cases, we can still calculate the RTT correctly
14145 * when RCV.NXT == Last.ACK.Sent.
14147 if ((to->to_flags & TOF_TS) != 0 &&
14148 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
14149 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
14150 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
14151 tp->ts_recent_age = tcp_ts_getticks();
14152 tp->ts_recent = to->to_tsval;
14155 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
14156 * is on (half-synchronized state), then queue data for later
14157 * processing; else drop segment and return.
14159 if ((thflags & TH_ACK) == 0) {
14160 if (tp->t_flags & TF_NEEDSYN) {
14161 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14162 tiwin, thflags, nxt_pkt));
14163 } else if (tp->t_flags & TF_ACKNOW) {
14164 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
14165 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
14168 ctf_do_drop(m, NULL);
14175 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
14178 if (sbavail(&so->so_snd)) {
14179 if (ctf_progress_timeout_check(tp, true)) {
14180 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
14181 tp, tick, PROGRESS_DROP, __LINE__);
14182 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
14186 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
14187 tiwin, thflags, nxt_pkt));
14191 rack_clear_rate_sample(struct tcp_rack *rack)
14193 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
14194 rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
14195 rack->r_ctl.rack_rs.rs_rtt_tot = 0;
14199 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override)
14201 uint64_t bw_est, rate_wanted;
14203 uint32_t user_max, orig_min, orig_max;
14205 #ifdef TCP_REQUEST_TRK
14206 if (rack->rc_hybrid_mode &&
14207 (rack->r_ctl.rc_pace_max_segs != 0) &&
14208 (rack_hybrid_allow_set_maxseg == 1) &&
14209 (rack->r_ctl.rc_last_sft != NULL)) {
14210 rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS;
14214 orig_min = rack->r_ctl.rc_pace_min_segs;
14215 orig_max = rack->r_ctl.rc_pace_max_segs;
14216 user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
14217 if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
14219 rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
14220 if (rack->use_fixed_rate || rack->rc_force_max_seg) {
14221 if (user_max != rack->r_ctl.rc_pace_max_segs)
14224 if (rack->rc_force_max_seg) {
14225 rack->r_ctl.rc_pace_max_segs = user_max;
14226 } else if (rack->use_fixed_rate) {
14227 bw_est = rack_get_bw(rack);
14228 if ((rack->r_ctl.crte == NULL) ||
14229 (bw_est != rack->r_ctl.crte->rate)) {
14230 rack->r_ctl.rc_pace_max_segs = user_max;
14232 /* We are pacing right at the hardware rate */
14233 uint32_t segsiz, pace_one;
14235 if (rack_pace_one_seg ||
14236 (rack->r_ctl.rc_user_set_min_segs == 1))
14240 segsiz = min(ctf_fixed_maxseg(tp),
14241 rack->r_ctl.rc_pace_min_segs);
14242 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(
14243 tp, bw_est, segsiz, pace_one,
14244 rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
14246 } else if (rack->rc_always_pace) {
14247 if (rack->r_ctl.gp_bw ||
14248 rack->r_ctl.init_rate) {
14249 /* We have a rate of some sort set */
14252 bw_est = rack_get_bw(rack);
14253 orig = rack->r_ctl.rc_pace_max_segs;
14255 rate_wanted = *fill_override;
14257 rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL);
14259 /* We have something */
14260 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
14262 ctf_fixed_maxseg(rack->rc_tp));
14264 rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
14265 if (orig != rack->r_ctl.rc_pace_max_segs)
14267 } else if ((rack->r_ctl.gp_bw == 0) &&
14268 (rack->r_ctl.rc_pace_max_segs == 0)) {
14270 * If we have nothing limit us to bursting
14271 * out IW sized pieces.
14274 rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
14277 if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
14279 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
14282 rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2);
14287 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags)
14290 struct ip6_hdr *ip6 = NULL;
14293 struct ip *ip = NULL;
14295 struct udphdr *udp = NULL;
14297 /* Ok lets fill in the fast block, it can only be used with no IP options! */
14299 if (rack->r_is_v6) {
14300 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
14301 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
14303 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14304 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
14305 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14306 udp->uh_dport = tp->t_port;
14307 rack->r_ctl.fsb.udp = udp;
14308 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14311 rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1);
14312 rack->r_ctl.fsb.udp = NULL;
14314 tcpip_fillheaders(rack->rc_inp,
14316 ip6, rack->r_ctl.fsb.th);
14317 rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL);
14322 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr);
14323 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
14325 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr);
14326 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
14327 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
14328 udp->uh_dport = tp->t_port;
14329 rack->r_ctl.fsb.udp = udp;
14330 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1);
14333 rack->r_ctl.fsb.udp = NULL;
14334 rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1);
14336 tcpip_fillheaders(rack->rc_inp,
14338 ip, rack->r_ctl.fsb.th);
14339 rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl;
14342 rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0),
14343 (long)TCP_MAXWIN << tp->rcv_scale);
14344 rack->r_fsb_inited = 1;
14348 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack)
14351 * Allocate the larger of spaces V6 if available else just
14352 * V4 and include udphdr (overbook)
14355 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr);
14357 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr);
14359 rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len,
14360 M_TCPFSB, M_NOWAIT|M_ZERO);
14361 if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) {
14364 rack->r_fsb_inited = 0;
14369 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod)
14372 * Types of logs (mod value)
14373 * 20 - Initial round setup
14374 * 21 - Rack declares a new round.
14379 if (tcp_bblogging_on(tp)) {
14380 union tcp_log_stackspecific log;
14383 memset(&log, 0, sizeof(log));
14384 log.u_bbr.flex1 = rack->r_ctl.current_round;
14385 log.u_bbr.flex2 = rack->r_ctl.roundends;
14386 log.u_bbr.flex3 = high_seq;
14387 log.u_bbr.flex4 = tp->snd_max;
14388 log.u_bbr.flex8 = mod;
14389 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14390 log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes;
14391 log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes;
14392 TCP_LOG_EVENTP(tp, NULL,
14393 &tptosocket(tp)->so_rcv,
14394 &tptosocket(tp)->so_snd,
14396 0, &log, false, &tv);
14401 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack)
14403 rack->rack_deferred_inited = 1;
14404 rack->r_ctl.roundends = tp->snd_max;
14405 rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
14406 rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
14410 rack_init_retransmit_value(struct tcp_rack *rack, int ctl)
14412 /* Retransmit bit controls.
14414 * The setting of these values control one of
14415 * three settings you can have and dictate
14416 * how rack does retransmissions. Note this
14417 * is in *any* mode i.e. pacing on or off DGP
14418 * fixed rate pacing, or just bursting rack.
14420 * 1 - Use full sized retransmits i.e. limit
14421 * the size to whatever the pace_max_segments
14424 * 2 - Use pacer min granularity as a guide to
14425 * the size combined with the current calculated
14426 * goodput b/w measurement. So for example if
14427 * the goodput is measured at 20Mbps we would
14428 * calculate 8125 (pacer minimum 250usec in
14429 * that b/w) and then round it up to the next
14430 * MSS i.e. for 1448 mss 6 MSS or 8688 bytes.
14432 * 0 - The rack default 1 MSS (anything not 0/1/2
14433 * fall here too if we are setting via rack_init()).
14437 rack->full_size_rxt = 1;
14438 rack->shape_rxt_to_pacing_min = 0;
14439 } else if (ctl == 2) {
14440 rack->full_size_rxt = 0;
14441 rack->shape_rxt_to_pacing_min = 1;
14443 rack->full_size_rxt = 0;
14444 rack->shape_rxt_to_pacing_min = 0;
14449 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod,
14454 if (tcp_bblogging_on(rack->rc_tp)) {
14455 union tcp_log_stackspecific log;
14458 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
14459 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
14460 log.u_bbr.flex8 = mod;
14461 log.u_bbr.flex1 = flex1;
14462 log.u_bbr.flex2 = flex2;
14463 log.u_bbr.flex3 = flex3;
14464 tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0,
14465 0, &log, false, NULL, __func__, __LINE__, &tv);
14470 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr)
14472 struct tcp_rack *rack;
14473 struct rack_sendmap *rsm;
14477 rack = (struct tcp_rack *)tp->t_fb_ptr;
14478 switch (reqr->req) {
14479 case TCP_QUERY_SENDMAP:
14480 if ((reqr->req_param == tp->snd_max) ||
14481 (tp->snd_max == tp->snd_una)){
14485 rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param);
14487 /* Can't find that seq -- unlikely */
14490 reqr->sendmap_start = rsm->r_start;
14491 reqr->sendmap_end = rsm->r_end;
14492 reqr->sendmap_send_cnt = rsm->r_rtr_cnt;
14493 reqr->sendmap_fas = rsm->r_fas;
14494 if (reqr->sendmap_send_cnt > SNDMAP_NRTX)
14495 reqr->sendmap_send_cnt = SNDMAP_NRTX;
14496 for(i=0; i<reqr->sendmap_send_cnt; i++)
14497 reqr->sendmap_time[i] = rsm->r_tim_lastsent[i];
14498 reqr->sendmap_ack_arrival = rsm->r_ack_arrival;
14499 reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK;
14500 reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes;
14501 reqr->sendmap_dupacks = rsm->r_dupack;
14502 rack_log_chg_info(tp, rack, 1,
14508 case TCP_QUERY_TIMERS_UP:
14509 if (rack->r_ctl.rc_hpts_flags == 0) {
14513 reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags;
14514 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14515 reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to;
14517 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14518 reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp;
14520 rack_log_chg_info(tp, rack, 2,
14521 rack->r_ctl.rc_hpts_flags,
14522 rack->r_ctl.rc_last_output_to,
14523 rack->r_ctl.rc_timer_exp);
14526 case TCP_QUERY_RACK_TIMES:
14527 /* Reordering items */
14528 reqr->rack_num_dsacks = rack->r_ctl.num_dsack;
14529 reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts;
14530 /* Timerstamps and timers */
14531 reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time;
14532 reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt;
14533 reqr->rack_rtt = rack->rc_rack_rtt;
14534 reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time;
14535 reqr->rack_srtt_measured = rack->rc_srtt_measure_made;
14537 reqr->rack_sacked = rack->r_ctl.rc_sacked;
14538 reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt;
14539 reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered;
14540 reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs;
14541 reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt;
14542 reqr->rack_prr_out = rack->r_ctl.rc_prr_out;
14543 /* TLP and persists info */
14544 reqr->rack_tlp_out = rack->rc_tlp_in_progress;
14545 reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out;
14546 if (rack->rc_in_persist) {
14547 reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time;
14548 reqr->rack_in_persist = 1;
14550 reqr->rack_time_went_idle = 0;
14551 reqr->rack_in_persist = 0;
14553 if (rack->r_wanted_output)
14554 reqr->rack_wanted_output = 1;
14556 reqr->rack_wanted_output = 0;
14565 rack_switch_failed(struct tcpcb *tp)
14568 * This method gets called if a stack switch was
14569 * attempted and it failed. We are left
14570 * but our hpts timers were stopped and we
14571 * need to validate time units and t_flags2.
14573 struct tcp_rack *rack;
14577 struct hpts_diag diag;
14579 rack = (struct tcp_rack *)tp->t_fb_ptr;
14580 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC);
14581 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
14582 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
14584 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
14585 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
14586 tp->t_flags2 |= TF2_MBUF_ACKCMP;
14587 if (tp->t_in_hpts > IHPTS_NONE) {
14591 cts = tcp_get_usecs(&tv);
14592 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
14593 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
14594 toval = rack->r_ctl.rc_last_output_to - cts;
14596 /* one slot please */
14597 toval = HPTS_TICKS_PER_SLOT;
14599 } else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
14600 if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
14601 toval = rack->r_ctl.rc_timer_exp - cts;
14603 /* one slot please */
14604 toval = HPTS_TICKS_PER_SLOT;
14607 toval = HPTS_TICKS_PER_SLOT;
14608 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval),
14610 rack_log_hpts_diag(rack, cts, &diag, &tv);
14614 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr)
14616 struct rack_sendmap *rsm, *ersm;
14617 int insret __diagused;
14619 * When initing outstanding, we must be quite careful
14620 * to not refer to tp->t_fb_ptr. This has the old rack
14621 * pointer in it, not the "new" one (when we are doing
14626 if (tp->t_fb->tfb_chg_query == NULL) {
14627 /* Create a send map for the current outstanding data */
14629 rsm = rack_alloc(rack);
14631 uma_zfree(rack_pcb_zone, ptr);
14634 rsm->r_no_rtt_allowed = 1;
14635 rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time);
14636 rsm->r_rtr_cnt = 1;
14637 rsm->r_rtr_bytes = 0;
14638 if (tp->t_flags & TF_SENTFIN)
14639 rsm->r_flags |= RACK_HAS_FIN;
14640 rsm->r_end = tp->snd_max;
14641 if (tp->snd_una == tp->iss) {
14642 /* The data space is one beyond snd_una */
14643 rsm->r_flags |= RACK_HAS_SYN;
14644 rsm->r_start = tp->iss;
14645 rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una);
14647 rsm->r_start = tp->snd_una;
14649 if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) {
14650 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff);
14652 rsm->orig_m_len = rsm->m->m_len;
14653 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14655 rsm->orig_m_len = 0;
14656 rsm->orig_t_space = 0;
14660 * This can happen if we have a stand-alone FIN or
14664 rsm->orig_m_len = 0;
14665 rsm->orig_t_space = 0;
14669 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14670 panic("Insert in rb tree fails ret:%d rack:%p rsm:%p",
14671 insret, rack, rsm);
14674 (void)tqhash_insert(rack->r_ctl.tqh, rsm);
14676 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14677 rsm->r_in_tmap = 1;
14679 /* We have a query mechanism, lets use it */
14680 struct tcp_query_resp qr;
14685 while (at != tp->snd_max) {
14686 memset(&qr, 0, sizeof(qr));
14687 qr.req = TCP_QUERY_SENDMAP;
14689 if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0)
14692 at = qr.sendmap_end;
14693 /* Now lets build the entry for this one */
14694 rsm = rack_alloc(rack);
14696 uma_zfree(rack_pcb_zone, ptr);
14699 memset(rsm, 0, sizeof(struct rack_sendmap));
14700 /* Now configure the rsm and insert it */
14701 rsm->r_dupack = qr.sendmap_dupacks;
14702 rsm->r_start = qr.sendmap_start;
14703 rsm->r_end = qr.sendmap_end;
14704 if (qr.sendmap_fas)
14705 rsm->r_fas = qr.sendmap_end;
14707 rsm->r_fas = rsm->r_start - tp->snd_una;
14709 * We have carefully aligned the bits
14710 * so that all we have to do is copy over
14711 * the bits with the mask.
14713 rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK;
14714 rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes;
14715 rsm->r_rtr_cnt = qr.sendmap_send_cnt;
14716 rsm->r_ack_arrival = qr.sendmap_ack_arrival;
14717 for (i=0 ; i<rsm->r_rtr_cnt; i++)
14718 rsm->r_tim_lastsent[i] = qr.sendmap_time[i];
14719 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd,
14720 (rsm->r_start - tp->snd_una), &rsm->soff);
14722 rsm->orig_m_len = rsm->m->m_len;
14723 rsm->orig_t_space = M_TRAILINGROOM(rsm->m);
14725 rsm->orig_m_len = 0;
14726 rsm->orig_t_space = 0;
14729 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) {
14730 panic("Insert in rb tree fails ret:%d rack:%p rsm:%p",
14731 insret, rack, rsm);
14734 (void)tqhash_insert(rack->r_ctl.tqh, rsm);
14736 if ((rsm->r_flags & RACK_ACKED) == 0) {
14737 TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) {
14738 if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] >
14739 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) {
14741 * If the existing ersm was sent at
14742 * a later time than the new one, then
14743 * the new one should appear ahead of this
14746 rsm->r_in_tmap = 1;
14747 TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext);
14751 if (rsm->r_in_tmap == 0) {
14753 * Not found so shove it on the tail.
14755 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
14756 rsm->r_in_tmap = 1;
14759 if ((rack->r_ctl.rc_sacklast == NULL) ||
14760 (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) {
14761 rack->r_ctl.rc_sacklast = rsm;
14764 rack_log_chg_info(tp, rack, 3,
14774 rack_translate_clamp_value(struct tcp_rack *rack, uint32_t optval)
14778 * F = fill cw bit -- Toggle fillcw if this bit is set.
14780 * M = set max segment bit
14782 * C = If set to non-zero override the max number of clamps.
14783 * L = Bit to indicate if clamped gets lower.
14785 * CCCC CCCCC UUUU UULF PPPP PPPP PPPP PPPP
14787 * The lowest 3 nibbles is the perentage .1 - 6553.5%
14788 * where 10.1 = 101, max 6553.5
14789 * The upper 16 bits holds some options.
14790 * The F bit will turn on fill-cw on if you are
14791 * not pacing, it will turn it off if dgp is on.
14792 * The L bit will change it so when clamped we get
14793 * the min(gp, lt-bw) for dgp.
14797 rack->r_ctl.saved_rxt_clamp_val = optval;
14798 per = optval & 0x0000ffff;
14799 rack->r_ctl.rxt_threshold = (uint64_t)(per & 0xffff);
14801 uint16_t clamp_opt;
14803 rack->excess_rxt_on = 1;
14804 clamp_opt = ((optval & 0xffff0000) >> 16);
14805 rack->r_ctl.clamp_options = clamp_opt & 0x00ff;
14806 if (clamp_opt & 0xff00) {
14807 /* A max clamps is also present */
14808 rack->r_ctl.max_clamps = (clamp_opt >> 8);
14810 /* No specified clamps means no limit */
14811 rack->r_ctl.max_clamps = 0;
14813 if (rack->r_ctl.clamp_options & 0x0002) {
14814 rack->r_clamped_gets_lower = 1;
14816 rack->r_clamped_gets_lower = 0;
14819 /* Turn it off back to default */
14820 rack->excess_rxt_on = 0;
14821 rack->r_clamped_gets_lower = 0;
14828 rack_init(struct tcpcb *tp, void **ptr)
14830 struct inpcb *inp = tptoinpcb(tp);
14831 struct tcp_rack *rack = NULL;
14832 uint32_t iwin, snt, us_cts;
14836 * First are we the initial or are we a switched stack?
14837 * If we are initing via tcp_newtcppcb the ptr passed
14838 * will be tp->t_fb_ptr. If its a stack switch that
14839 * has a previous stack we can query it will be a local
14840 * var that will in the end be set into t_fb_ptr.
14842 if (ptr == &tp->t_fb_ptr)
14846 *ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
14847 if (*ptr == NULL) {
14849 * We need to allocate memory but cant. The INP and INP_INFO
14850 * locks and they are recursive (happens during setup. So a
14851 * scheme to drop the locks fails :(
14856 memset(*ptr, 0, sizeof(struct tcp_rack));
14857 rack = (struct tcp_rack *)*ptr;
14858 rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT);
14859 if (rack->r_ctl.tqh == NULL) {
14860 uma_zfree(rack_pcb_zone, rack);
14863 tqhash_init(rack->r_ctl.tqh);
14864 TAILQ_INIT(&rack->r_ctl.rc_free);
14865 TAILQ_INIT(&rack->r_ctl.rc_tmap);
14867 rack->rc_inp = inp;
14869 rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0;
14870 /* Probably not needed but lets be sure */
14871 rack_clear_rate_sample(rack);
14873 * Save off the default values, socket options will poke
14874 * at these if pacing is not on or we have not yet
14875 * reached where pacing is on (gp_ready/fixed enabled).
14876 * When they get set into the CC module (when gp_ready
14877 * is enabled or we enable fixed) then we will set these
14878 * values into the CC and place in here the old values
14879 * so we have a restoral. Then we will set the flag
14880 * rc_pacing_cc_set. That way whenever we turn off pacing
14881 * or switch off this stack, we will know to go restore
14882 * the saved values.
14884 * We specifically put into the beta the ecn value for pacing.
14886 rack->rc_new_rnd_needed = 1;
14887 rack->r_ctl.rc_split_limit = V_tcp_map_split_limit;
14888 rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn;
14889 rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn;
14890 /* We want abe like behavior as well */
14891 rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
14892 rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
14893 rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
14894 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
14895 if (rack_rxt_clamp_thresh) {
14896 rack_translate_clamp_value(rack, rack_rxt_clamp_thresh);
14897 rack->excess_rxt_on = 1;
14899 if (rack_uses_full_dgp_in_rec)
14900 rack->r_ctl.full_dgp_in_rec = 1;
14901 if (rack_fill_cw_state)
14902 rack->rc_pace_to_cwnd = 1;
14903 if (rack_pacing_min_seg)
14904 rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg;
14906 rack->use_rack_rr = 1;
14907 if (rack_dnd_default) {
14908 rack->rc_pace_dnd = 1;
14910 if (V_tcp_delack_enabled)
14911 tp->t_delayed_ack = 1;
14913 tp->t_delayed_ack = 0;
14914 #ifdef TCP_ACCOUNTING
14915 if (rack_tcp_accounting) {
14916 tp->t_flags2 |= TF2_TCP_ACCOUNTING;
14919 rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss;
14920 rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca;
14921 if (rack_enable_shared_cwnd)
14922 rack->rack_enable_scwnd = 1;
14923 rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
14924 rack->rc_user_set_max_segs = rack_hptsi_segments;
14925 rack->rc_force_max_seg = 0;
14926 TAILQ_INIT(&rack->r_ctl.opt_list);
14927 if (rack_hibeta_setting)
14928 rack->rack_hibeta = 1;
14929 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
14930 rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
14931 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
14932 rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
14933 rack->r_ctl.rc_highest_us_rtt = 0;
14934 rack->r_ctl.bw_rate_cap = rack_bw_rate_cap;
14935 rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop);
14936 if (rack_use_cmp_acks)
14937 rack->r_use_cmp_ack = 1;
14938 if (rack_disable_prr)
14939 rack->rack_no_prr = 1;
14940 if (rack_gp_no_rec_chg)
14941 rack->rc_gp_no_rec_chg = 1;
14942 if (rack_pace_every_seg && tcp_can_enable_pacing()) {
14943 rack->rc_always_pace = 1;
14944 if ((rack->gp_ready) && (rack->rc_always_pace && (rack->use_fixed_rate == 0)))
14945 rack_set_cc_pacing(rack);
14947 rack->rc_always_pace = 0;
14948 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack)
14949 rack->r_mbuf_queue = 1;
14951 rack->r_mbuf_queue = 0;
14952 rack_set_pace_segments(tp, rack, __LINE__, NULL);
14953 if (rack_limits_scwnd)
14954 rack->r_limit_scw = 1;
14956 rack->r_limit_scw = 0;
14957 rack_init_retransmit_value(rack, rack_rxt_controls);
14958 rack->rc_labc = V_tcp_abc_l_var;
14959 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
14960 rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
14961 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
14962 rack->r_ctl.rc_min_to = rack_min_to;
14963 microuptime(&rack->r_ctl.act_rcv_time);
14964 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
14965 rack->rc_init_win = rack_default_init_window;
14966 rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
14967 if (rack_hw_up_only)
14968 rack->r_up_only = 1;
14969 if (rack_do_dyn_mul) {
14970 /* When dynamic adjustment is on CA needs to start at 100% */
14971 rack->rc_gp_dyn_mul = 1;
14972 if (rack_do_dyn_mul >= 100)
14973 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
14975 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
14976 rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
14977 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
14978 rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
14979 setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
14980 rack_probertt_filter_life);
14981 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
14982 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
14983 rack->r_ctl.rc_time_of_last_probertt = us_cts;
14984 rack->r_ctl.challenge_ack_ts = tcp_ts_getticks();
14985 rack->r_ctl.rc_time_probertt_starts = 0;
14986 if (rack_dsack_std_based & 0x1) {
14987 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
14988 rack->rc_rack_tmr_std_based = 1;
14990 if (rack_dsack_std_based & 0x2) {
14991 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */
14992 rack->rc_rack_use_dsack = 1;
14994 /* We require at least one measurement, even if the sysctl is 0 */
14995 if (rack_req_measurements)
14996 rack->r_ctl.req_measurements = rack_req_measurements;
14998 rack->r_ctl.req_measurements = 1;
14999 if (rack_enable_hw_pacing)
15000 rack->rack_hdw_pace_ena = 1;
15001 if (rack_hw_rate_caps)
15002 rack->r_rack_hw_rate_caps = 1;
15003 #ifdef TCP_SAD_DETECTION
15004 rack->do_detection = 1;
15006 rack->do_detection = 0;
15008 if (rack_non_rxt_use_cr)
15009 rack->rack_rec_nonrxt_use_cr = 1;
15010 /* Lets setup the fsb block */
15011 err = rack_init_fsb(tp, rack);
15013 uma_zfree(rack_pcb_zone, *ptr);
15017 if (rack_do_hystart) {
15018 tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
15019 if (rack_do_hystart > 1)
15020 tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
15021 if (rack_do_hystart > 2)
15022 tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
15024 /* Log what we will do with queries */
15025 rack_log_chg_info(tp, rack, 7,
15027 if (rack_def_profile)
15028 rack_set_profile(rack, rack_def_profile);
15029 /* Cancel the GP measurement in progress */
15030 tp->t_flags &= ~TF_GPUTINPROG;
15031 if ((tp->t_state != TCPS_CLOSED) &&
15032 (tp->t_state != TCPS_TIME_WAIT)) {
15034 * We are already open, we may
15035 * need to adjust a few things.
15037 if (SEQ_GT(tp->snd_max, tp->iss))
15038 snt = tp->snd_max - tp->iss;
15041 iwin = rc_init_window(rack);
15042 if ((snt < iwin) &&
15044 /* We are not past the initial window
15045 * on the first init (i.e. a stack switch
15046 * has not yet occured) so we need to make
15047 * sure cwnd and ssthresh is correct.
15049 if (tp->snd_cwnd < iwin)
15050 tp->snd_cwnd = iwin;
15052 * If we are within the initial window
15053 * we want ssthresh to be unlimited. Setting
15054 * it to the rwnd (which the default stack does
15055 * and older racks) is not really a good idea
15056 * since we want to be in SS and grow both the
15057 * cwnd and the rwnd (via dynamic rwnd growth). If
15058 * we set it to the rwnd then as the peer grows its
15059 * rwnd we will be stuck in CA and never hit SS.
15061 * Its far better to raise it up high (this takes the
15062 * risk that there as been a loss already, probably
15063 * we should have an indicator in all stacks of loss
15064 * but we don't), but considering the normal use this
15065 * is a risk worth taking. The consequences of not
15066 * hitting SS are far worse than going one more time
15067 * into it early on (before we have sent even a IW).
15068 * It is highly unlikely that we will have had a loss
15069 * before getting the IW out.
15071 tp->snd_ssthresh = 0xffffffff;
15074 * Any init based on sequence numbers
15075 * should be done in the deferred init path
15076 * since we can be CLOSED and not have them
15077 * inited when rack_init() is called. We
15078 * are not closed so lets call it.
15080 rack_deferred_init(tp, rack);
15082 if ((tp->t_state != TCPS_CLOSED) &&
15083 (tp->t_state != TCPS_TIME_WAIT) &&
15085 (tp->snd_una != tp->snd_max)) {
15086 err = rack_init_outstanding(tp, rack, us_cts, *ptr);
15092 rack_stop_all_timers(tp, rack);
15093 /* Setup all the t_flags2 */
15094 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
15095 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
15097 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
15098 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15099 tp->t_flags2 |= TF2_MBUF_ACKCMP;
15101 * Timers in Rack are kept in microseconds so lets
15102 * convert any initial incoming variables
15103 * from ticks into usecs. Note that we
15104 * also change the values of t_srtt and t_rttvar, if
15105 * they are non-zero. They are kept with a 5
15106 * bit decimal so we have to carefully convert
15107 * these to get the full precision.
15109 rack_convert_rtts(tp);
15110 rack_log_hystart_event(rack, rack->r_ctl.roundends, 20);
15111 if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) {
15112 /* We do not start any timers on DROPPED connections */
15113 if (tp->t_fb->tfb_chg_query == NULL) {
15114 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15116 struct tcp_query_resp qr;
15119 memset(&qr, 0, sizeof(qr));
15121 /* Get the misc time stamps and such for rack */
15122 qr.req = TCP_QUERY_RACK_TIMES;
15123 ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
15125 rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts;
15126 rack->r_ctl.num_dsack = qr.rack_num_dsacks;
15127 rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time;
15128 rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt;
15129 rack->rc_rack_rtt = qr.rack_rtt;
15130 rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time;
15131 rack->r_ctl.rc_sacked = qr.rack_sacked;
15132 rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt;
15133 rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered;
15134 rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs;
15135 rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt;
15136 rack->r_ctl.rc_prr_out = qr.rack_prr_out;
15137 if (qr.rack_tlp_out) {
15138 rack->rc_tlp_in_progress = 1;
15139 rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out;
15141 rack->rc_tlp_in_progress = 0;
15142 rack->r_ctl.rc_tlp_cnt_out = 0;
15144 if (qr.rack_srtt_measured)
15145 rack->rc_srtt_measure_made = 1;
15146 if (qr.rack_in_persist == 1) {
15147 rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle;
15148 #ifdef NETFLIX_SHARED_CWND
15149 if (rack->r_ctl.rc_scw) {
15150 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
15151 rack->rack_scwnd_is_idle = 1;
15154 rack->r_ctl.persist_lost_ends = 0;
15155 rack->probe_not_answered = 0;
15156 rack->forced_ack = 0;
15157 tp->t_rxtshift = 0;
15158 rack->rc_in_persist = 1;
15159 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
15160 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
15162 if (qr.rack_wanted_output)
15163 rack->r_wanted_output = 1;
15164 rack_log_chg_info(tp, rack, 6,
15167 qr.rack_reorder_ts);
15169 /* Get the old stack timers */
15171 qr.req = TCP_QUERY_TIMERS_UP;
15172 ret = (*tp->t_fb->tfb_chg_query)(tp, &qr);
15175 * non-zero return means we have a timer('s)
15176 * to start. Zero means no timer (no keepalive
15181 rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags;
15182 if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) {
15183 rack->r_ctl.rc_last_output_to = qr.timer_pacing_to;
15184 if (TSTMP_GT(qr.timer_pacing_to, us_cts))
15185 tov = qr.timer_pacing_to - us_cts;
15187 tov = HPTS_TICKS_PER_SLOT;
15189 if (qr.timer_hpts_flags & PACE_TMR_MASK) {
15190 rack->r_ctl.rc_timer_exp = qr.timer_timer_exp;
15192 if (TSTMP_GT(qr.timer_timer_exp, us_cts))
15193 tov = qr.timer_timer_exp - us_cts;
15195 tov = HPTS_TICKS_PER_SLOT;
15198 rack_log_chg_info(tp, rack, 4,
15199 rack->r_ctl.rc_hpts_flags,
15200 rack->r_ctl.rc_last_output_to,
15201 rack->r_ctl.rc_timer_exp);
15203 struct hpts_diag diag;
15205 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov),
15207 rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time);
15211 rack_log_rtt_shrinks(rack, us_cts, tp->t_rxtcur,
15212 __LINE__, RACK_RTTS_INIT);
15218 rack_handoff_ok(struct tcpcb *tp)
15220 if ((tp->t_state == TCPS_CLOSED) ||
15221 (tp->t_state == TCPS_LISTEN)) {
15222 /* Sure no problem though it may not stick */
15225 if ((tp->t_state == TCPS_SYN_SENT) ||
15226 (tp->t_state == TCPS_SYN_RECEIVED)) {
15228 * We really don't know if you support sack,
15229 * you have to get to ESTAB or beyond to tell.
15233 if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
15235 * Rack will only send a FIN after all data is acknowledged.
15236 * So in this case we have more data outstanding. We can't
15237 * switch stacks until either all data and only the FIN
15238 * is left (in which case rack_init() now knows how
15239 * to deal with that) <or> all is acknowledged and we
15240 * are only left with incoming data, though why you
15241 * would want to switch to rack after all data is acknowledged
15242 * I have no idea (rrs)!
15246 if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
15250 * If we reach here we don't do SACK on this connection so we can
15257 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
15260 if (tp->t_fb_ptr) {
15261 uint32_t cnt_free = 0;
15262 struct tcp_rack *rack;
15263 struct rack_sendmap *rsm;
15265 tcp_handle_orphaned_packets(tp);
15266 tp->t_flags &= ~TF_FORCEDATA;
15267 rack = (struct tcp_rack *)tp->t_fb_ptr;
15268 rack_log_pacing_delay_calc(rack,
15272 rack_get_gp_est(rack), /* delRate */
15273 rack_get_lt_bw(rack), /* rttProp */
15274 20, __LINE__, NULL, 0);
15275 #ifdef NETFLIX_SHARED_CWND
15276 if (rack->r_ctl.rc_scw) {
15279 if (rack->r_limit_scw)
15280 limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
15283 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
15284 rack->r_ctl.rc_scw_index,
15286 rack->r_ctl.rc_scw = NULL;
15289 if (rack->r_ctl.fsb.tcp_ip_hdr) {
15290 free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB);
15291 rack->r_ctl.fsb.tcp_ip_hdr = NULL;
15292 rack->r_ctl.fsb.th = NULL;
15294 if (rack->rc_always_pace) {
15295 tcp_decrement_paced_conn();
15296 rack_undo_cc_pacing(rack);
15297 rack->rc_always_pace = 0;
15299 /* Clean up any options if they were not applied */
15300 while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) {
15301 struct deferred_opt_list *dol;
15303 dol = TAILQ_FIRST(&rack->r_ctl.opt_list);
15304 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
15305 free(dol, M_TCPDO);
15307 /* rack does not use force data but other stacks may clear it */
15308 if (rack->r_ctl.crte != NULL) {
15309 tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
15310 rack->rack_hdrw_pacing = 0;
15311 rack->r_ctl.crte = NULL;
15313 #ifdef TCP_BLACKBOX
15314 tcp_log_flowend(tp);
15317 * Lets take a different approach to purging just
15318 * get each one and free it like a cum-ack would and
15319 * not use a foreach loop.
15321 rsm = tqhash_min(rack->r_ctl.tqh);
15323 tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK);
15324 rack->r_ctl.rc_num_maps_alloced--;
15325 uma_zfree(rack_zone, rsm);
15326 rsm = tqhash_min(rack->r_ctl.tqh);
15328 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15330 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
15331 rack->r_ctl.rc_num_maps_alloced--;
15332 rack->rc_free_cnt--;
15334 uma_zfree(rack_zone, rsm);
15335 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15337 if ((rack->r_ctl.rc_num_maps_alloced > 0) &&
15338 (tcp_bblogging_on(tp))) {
15339 union tcp_log_stackspecific log;
15342 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15343 log.u_bbr.flex8 = 10;
15344 log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced;
15345 log.u_bbr.flex2 = rack->rc_free_cnt;
15346 log.u_bbr.flex3 = cnt_free;
15347 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
15348 rsm = tqhash_min(rack->r_ctl.tqh);
15349 log.u_bbr.delRate = (uint64_t)rsm;
15350 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
15351 log.u_bbr.cur_del_rate = (uint64_t)rsm;
15352 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
15353 log.u_bbr.pkt_epoch = __LINE__;
15354 (void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
15355 0, &log, false, NULL, NULL, 0, &tv);
15357 KASSERT((rack->r_ctl.rc_num_maps_alloced == 0),
15358 ("rack:%p num_aloc:%u after freeing all?",
15360 rack->r_ctl.rc_num_maps_alloced));
15361 rack->rc_free_cnt = 0;
15362 free(rack->r_ctl.tqh, M_TCPFSB);
15363 rack->r_ctl.tqh = NULL;
15364 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
15365 tp->t_fb_ptr = NULL;
15367 /* Make sure snd_nxt is correctly set */
15368 tp->snd_nxt = tp->snd_max;
15372 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
15374 if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) {
15375 rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0;
15377 switch (tp->t_state) {
15378 case TCPS_SYN_SENT:
15379 rack->r_state = TCPS_SYN_SENT;
15380 rack->r_substate = rack_do_syn_sent;
15382 case TCPS_SYN_RECEIVED:
15383 rack->r_state = TCPS_SYN_RECEIVED;
15384 rack->r_substate = rack_do_syn_recv;
15386 case TCPS_ESTABLISHED:
15387 rack_set_pace_segments(tp, rack, __LINE__, NULL);
15388 rack->r_state = TCPS_ESTABLISHED;
15389 rack->r_substate = rack_do_established;
15391 case TCPS_CLOSE_WAIT:
15392 rack->r_state = TCPS_CLOSE_WAIT;
15393 rack->r_substate = rack_do_close_wait;
15395 case TCPS_FIN_WAIT_1:
15396 rack_set_pace_segments(tp, rack, __LINE__, NULL);
15397 rack->r_state = TCPS_FIN_WAIT_1;
15398 rack->r_substate = rack_do_fin_wait_1;
15401 rack_set_pace_segments(tp, rack, __LINE__, NULL);
15402 rack->r_state = TCPS_CLOSING;
15403 rack->r_substate = rack_do_closing;
15405 case TCPS_LAST_ACK:
15406 rack_set_pace_segments(tp, rack, __LINE__, NULL);
15407 rack->r_state = TCPS_LAST_ACK;
15408 rack->r_substate = rack_do_lastack;
15410 case TCPS_FIN_WAIT_2:
15411 rack->r_state = TCPS_FIN_WAIT_2;
15412 rack->r_substate = rack_do_fin_wait_2;
15416 case TCPS_TIME_WAIT:
15420 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
15421 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
15426 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
15429 * We received an ack, and then did not
15430 * call send or were bounced out due to the
15431 * hpts was running. Now a timer is up as well, is
15432 * it the right timer?
15434 struct rack_sendmap *rsm;
15437 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
15438 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
15440 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
15441 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
15442 (tmr_up == PACE_TMR_RXT)) {
15443 /* Should be an RXT */
15447 /* Nothing outstanding? */
15448 if (tp->t_flags & TF_DELACK) {
15449 if (tmr_up == PACE_TMR_DELACK)
15450 /* We are supposed to have delayed ack up and we do */
15452 } else if (sbavail(&tptosocket(tp)->so_snd) && (tmr_up == PACE_TMR_RXT)) {
15454 * if we hit enobufs then we would expect the possibility
15455 * of nothing outstanding and the RXT up (and the hptsi timer).
15458 } else if (((V_tcp_always_keepalive ||
15459 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
15460 (tp->t_state <= TCPS_CLOSING)) &&
15461 (tmr_up == PACE_TMR_KEEP) &&
15462 (tp->snd_max == tp->snd_una)) {
15463 /* We should have keep alive up and we do */
15467 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
15468 ((tmr_up == PACE_TMR_TLP) ||
15469 (tmr_up == PACE_TMR_RACK) ||
15470 (tmr_up == PACE_TMR_RXT))) {
15472 * Either a Rack, TLP or RXT is fine if we
15473 * have outstanding data.
15476 } else if (tmr_up == PACE_TMR_DELACK) {
15478 * If the delayed ack was going to go off
15479 * before the rtx/tlp/rack timer were going to
15480 * expire, then that would be the timer in control.
15481 * Note we don't check the time here trusting the
15487 * Ok the timer originally started is not what we want now.
15488 * We will force the hpts to be stopped if any, and restart
15489 * with the slot set to what was in the saved slot.
15491 if (tcp_in_hpts(rack->rc_tp)) {
15492 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
15495 us_cts = tcp_get_usecs(NULL);
15496 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
15498 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
15500 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
15502 tcp_hpts_remove(rack->rc_tp);
15504 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
15505 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
15510 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts)
15512 if ((SEQ_LT(tp->snd_wl1, seq) ||
15513 (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) ||
15514 (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) {
15515 /* keep track of pure window updates */
15516 if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd))
15517 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
15518 tp->snd_wnd = tiwin;
15519 rack_validate_fo_sendwin_up(tp, rack);
15522 if (tp->snd_wnd > tp->max_sndwnd)
15523 tp->max_sndwnd = tp->snd_wnd;
15524 rack->r_wanted_output = 1;
15525 } else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) {
15526 tp->snd_wnd = tiwin;
15527 rack_validate_fo_sendwin_up(tp, rack);
15531 /* Not a valid win update */
15534 if (tp->snd_wnd > tp->max_sndwnd)
15535 tp->max_sndwnd = tp->snd_wnd;
15536 /* Do we exit persists? */
15537 if ((rack->rc_in_persist != 0) &&
15538 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
15539 rack->r_ctl.rc_pace_min_segs))) {
15540 rack_exit_persist(tp, rack, cts);
15542 /* Do we enter persists? */
15543 if ((rack->rc_in_persist == 0) &&
15544 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
15545 TCPS_HAVEESTABLISHED(tp->t_state) &&
15546 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) &&
15547 sbavail(&tptosocket(tp)->so_snd) &&
15548 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) {
15550 * Here the rwnd is less than
15551 * the pacing size, we are established,
15552 * nothing is outstanding, and there is
15553 * data to send. Enter persists.
15555 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack);
15560 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq)
15563 if (tcp_bblogging_on(rack->rc_tp)) {
15564 struct inpcb *inp = tptoinpcb(tp);
15565 union tcp_log_stackspecific log;
15566 struct timeval ltv;
15567 char tcp_hdr_buf[60];
15569 struct timespec ts;
15570 uint32_t orig_snd_una;
15573 #ifdef TCP_REQUEST_TRK
15574 struct tcp_sendfile_track *tcp_req;
15576 if (SEQ_GT(ae->ack, tp->snd_una)) {
15577 tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1));
15579 tcp_req = tcp_req_find_req_for_seq(tp, ae->ack);
15582 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
15583 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
15584 if (rack->rack_no_prr == 0)
15585 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
15587 log.u_bbr.flex1 = 0;
15588 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
15589 log.u_bbr.use_lt_bw <<= 1;
15590 log.u_bbr.use_lt_bw |= rack->r_might_revert;
15591 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
15592 log.u_bbr.bbr_state = rack->rc_free_cnt;
15593 log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
15594 log.u_bbr.pkts_out = tp->t_maxseg;
15595 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
15596 log.u_bbr.flex7 = 1;
15597 log.u_bbr.lost = ae->flags;
15598 log.u_bbr.cwnd_gain = ackval;
15599 log.u_bbr.pacing_gain = 0x2;
15600 if (ae->flags & TSTMP_HDWR) {
15601 /* Record the hardware timestamp if present */
15602 log.u_bbr.flex3 = M_TSTMP;
15603 ts.tv_sec = ae->timestamp / 1000000000;
15604 ts.tv_nsec = ae->timestamp % 1000000000;
15605 ltv.tv_sec = ts.tv_sec;
15606 ltv.tv_usec = ts.tv_nsec / 1000;
15607 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v);
15608 } else if (ae->flags & TSTMP_LRO) {
15609 /* Record the LRO the arrival timestamp */
15610 log.u_bbr.flex3 = M_TSTMP_LRO;
15611 ts.tv_sec = ae->timestamp / 1000000000;
15612 ts.tv_nsec = ae->timestamp % 1000000000;
15613 ltv.tv_sec = ts.tv_sec;
15614 ltv.tv_usec = ts.tv_nsec / 1000;
15615 log.u_bbr.flex5 = tcp_tv_to_usectick(<v);
15617 log.u_bbr.timeStamp = tcp_get_usecs(<v);
15618 /* Log the rcv time */
15619 log.u_bbr.delRate = ae->timestamp;
15620 #ifdef TCP_REQUEST_TRK
15621 log.u_bbr.applimited = tp->t_tcpreq_closed;
15622 log.u_bbr.applimited <<= 8;
15623 log.u_bbr.applimited |= tp->t_tcpreq_open;
15624 log.u_bbr.applimited <<= 8;
15625 log.u_bbr.applimited |= tp->t_tcpreq_req;
15627 /* Copy out any client req info */
15629 log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
15631 log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
15632 log.u_bbr.rttProp = tcp_req->timestamp;
15633 log.u_bbr.cur_del_rate = tcp_req->start;
15634 if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
15635 log.u_bbr.flex8 |= 1;
15637 log.u_bbr.flex8 |= 2;
15638 log.u_bbr.bw_inuse = tcp_req->end;
15640 log.u_bbr.flex6 = tcp_req->start_seq;
15641 if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
15642 log.u_bbr.flex8 |= 4;
15643 log.u_bbr.epoch = tcp_req->end_seq;
15647 memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf));
15648 th = (struct tcphdr *)tcp_hdr_buf;
15649 th->th_seq = ae->seq;
15650 th->th_ack = ae->ack;
15651 th->th_win = ae->win;
15652 /* Now fill in the ports */
15653 th->th_sport = inp->inp_fport;
15654 th->th_dport = inp->inp_lport;
15655 tcp_set_flags(th, ae->flags);
15656 /* Now do we have a timestamp option? */
15657 if (ae->flags & HAS_TSTMP) {
15661 th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2);
15662 cp = (u_char *)(th + 1);
15667 *cp = TCPOPT_TIMESTAMP;
15669 *cp = TCPOLEN_TIMESTAMP;
15671 val = htonl(ae->ts_value);
15672 bcopy((char *)&val,
15673 (char *)cp, sizeof(uint32_t));
15674 val = htonl(ae->ts_echo);
15675 bcopy((char *)&val,
15676 (char *)(cp + 4), sizeof(uint32_t));
15678 th->th_off = (sizeof(struct tcphdr) >> 2);
15681 * For sane logging we need to play a little trick.
15682 * If the ack were fully processed we would have moved
15683 * snd_una to high_seq, but since compressed acks are
15684 * processed in two phases, at this point (logging) snd_una
15685 * won't be advanced. So we would see multiple acks showing
15686 * the advancement. We can prevent that by "pretending" that
15687 * snd_una was advanced and then un-advancing it so that the
15688 * logging code has the right value for tlb_snd_una.
15690 if (tp->snd_una != high_seq) {
15691 orig_snd_una = tp->snd_una;
15692 tp->snd_una = high_seq;
15696 TCP_LOG_EVENTP(tp, th,
15697 &tptosocket(tp)->so_rcv,
15698 &tptosocket(tp)->so_snd, TCP_LOG_IN, 0,
15699 0, &log, true, <v);
15701 tp->snd_una = orig_snd_una;
15708 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts)
15712 * A persist or keep-alive was forced out, update our
15713 * min rtt time. Note now worry about lost responses.
15714 * When a subsequent keep-alive or persist times out
15715 * and forced_ack is still on, then the last probe
15716 * was not responded to. In such cases we have a
15717 * sysctl that controls the behavior. Either we apply
15718 * the rtt but with reduced confidence (0). Or we just
15719 * plain don't apply the rtt estimate. Having data flow
15720 * will clear the probe_not_answered flag i.e. cum-ack
15721 * move forward <or> exiting and reentering persists.
15724 rack->forced_ack = 0;
15725 rack->rc_tp->t_rxtshift = 0;
15726 if ((rack->rc_in_persist &&
15727 (tiwin == rack->rc_tp->snd_wnd)) ||
15728 (rack->rc_in_persist == 0)) {
15730 * In persists only apply the RTT update if this is
15731 * a response to our window probe. And that
15732 * means the rwnd sent must match the current
15733 * snd_wnd. If it does not, then we got a
15734 * window update ack instead. For keepalive
15735 * we allow the answer no matter what the window.
15737 * Note that if the probe_not_answered is set then
15738 * the forced_ack_ts is the oldest one i.e. the first
15739 * probe sent that might have been lost. This assures
15740 * us that if we do calculate an RTT it is longer not
15741 * some short thing.
15743 if (rack->rc_in_persist)
15744 counter_u64_add(rack_persists_acks, 1);
15745 us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
15748 if (rack->probe_not_answered == 0) {
15749 rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15750 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1);
15752 /* We have a retransmitted probe here too */
15753 if (rack_apply_rtt_with_reduced_conf) {
15754 rack_apply_updated_usrtt(rack, us_rtt, us_cts);
15755 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1);
15762 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv)
15765 * Handle a "special" compressed ack mbuf. Each incoming
15766 * ack has only four possible dispositions:
15768 * A) It moves the cum-ack forward
15769 * B) It is behind the cum-ack.
15770 * C) It is a window-update ack.
15771 * D) It is a dup-ack.
15773 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES
15774 * in the incoming mbuf. We also need to still pay attention
15775 * to nxt_pkt since there may be another packet after this
15778 #ifdef TCP_ACCOUNTING
15783 struct timespec ts;
15784 struct tcp_rack *rack;
15785 struct tcp_ackent *ae;
15786 uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack;
15787 int cnt, i, did_out, ourfinisacked = 0;
15788 struct tcpopt to_holder, *to = NULL;
15789 #ifdef TCP_ACCOUNTING
15790 int win_up_req = 0;
15793 int under_pacing = 0;
15795 #ifdef TCP_ACCOUNTING
15798 rack = (struct tcp_rack *)tp->t_fb_ptr;
15799 if (rack->gp_ready &&
15800 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT))
15803 if (rack->r_state != tp->t_state)
15804 rack_set_state(tp, rack);
15805 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
15806 (tp->t_flags & TF_GPUTINPROG)) {
15808 * We have a goodput in progress
15809 * and we have entered a late state.
15810 * Do we have enough data in the sb
15811 * to handle the GPUT request?
15815 bytes = tp->gput_ack - tp->gput_seq;
15816 if (SEQ_GT(tp->gput_seq, tp->snd_una))
15817 bytes += tp->gput_seq - tp->snd_una;
15818 if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
15820 * There are not enough bytes in the socket
15821 * buffer that have been sent to cover this
15822 * measurement. Cancel it.
15824 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
15825 rack->r_ctl.rc_gp_srtt /*flex1*/,
15827 0, 0, 18, __LINE__, NULL, 0);
15828 tp->t_flags &= ~TF_GPUTINPROG;
15833 KASSERT((m->m_len >= sizeof(struct tcp_ackent)),
15834 ("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len));
15835 cnt = m->m_len / sizeof(struct tcp_ackent);
15836 counter_u64_add(rack_multi_single_eq, cnt);
15837 high_seq = tp->snd_una;
15838 the_win = tp->snd_wnd;
15839 win_seq = tp->snd_wl1;
15840 win_upd_ack = tp->snd_wl2;
15841 cts = tcp_tv_to_usectick(tv);
15842 ms_cts = tcp_tv_to_mssectick(tv);
15843 rack->r_ctl.rc_rcvtime = cts;
15844 segsiz = ctf_fixed_maxseg(tp);
15845 if ((rack->rc_gp_dyn_mul) &&
15846 (rack->use_fixed_rate == 0) &&
15847 (rack->rc_always_pace)) {
15848 /* Check in on probertt */
15849 rack_check_probe_rtt(rack, cts);
15851 for (i = 0; i < cnt; i++) {
15852 #ifdef TCP_ACCOUNTING
15853 ts_val = get_cyclecount();
15855 rack_clear_rate_sample(rack);
15856 ae = ((mtod(m, struct tcp_ackent *)) + i);
15857 if (ae->flags & TH_FIN)
15858 rack_log_pacing_delay_calc(rack,
15862 rack_get_gp_est(rack), /* delRate */
15863 rack_get_lt_bw(rack), /* rttProp */
15864 20, __LINE__, NULL, 0);
15865 /* Setup the window */
15866 tiwin = ae->win << tp->snd_scale;
15867 if (tiwin > rack->r_ctl.rc_high_rwnd)
15868 rack->r_ctl.rc_high_rwnd = tiwin;
15869 /* figure out the type of ack */
15870 if (SEQ_LT(ae->ack, high_seq)) {
15872 ae->ack_val_set = ACK_BEHIND;
15873 } else if (SEQ_GT(ae->ack, high_seq)) {
15875 ae->ack_val_set = ACK_CUMACK;
15876 } else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){
15878 ae->ack_val_set = ACK_DUPACK;
15881 ae->ack_val_set = ACK_RWND;
15883 if (rack->sack_attack_disable > 0) {
15884 rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
15885 rack->r_ctl.ack_during_sd++;
15887 rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq);
15888 /* Validate timestamp */
15889 if (ae->flags & HAS_TSTMP) {
15890 /* Setup for a timestamp */
15891 to->to_flags = TOF_TS;
15892 ae->ts_echo -= tp->ts_offset;
15893 to->to_tsecr = ae->ts_echo;
15894 to->to_tsval = ae->ts_value;
15896 * If echoed timestamp is later than the current time, fall back to
15897 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
15898 * were used when this connection was established.
15900 if (TSTMP_GT(ae->ts_echo, ms_cts))
15902 if (tp->ts_recent &&
15903 TSTMP_LT(ae->ts_value, tp->ts_recent)) {
15904 if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) {
15905 #ifdef TCP_ACCOUNTING
15906 rdstc = get_cyclecount();
15907 if (rdstc > ts_val) {
15908 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15909 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
15916 if (SEQ_LEQ(ae->seq, tp->last_ack_sent) &&
15917 SEQ_LEQ(tp->last_ack_sent, ae->seq)) {
15918 tp->ts_recent_age = tcp_ts_getticks();
15919 tp->ts_recent = ae->ts_value;
15922 /* Setup for a no options */
15925 /* Update the rcv time and perform idle reduction possibly */
15926 if (tp->t_idle_reduce &&
15927 (tp->snd_max == tp->snd_una) &&
15928 (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
15929 counter_u64_add(rack_input_idle_reduces, 1);
15930 rack_cc_after_idle(rack, tp);
15932 tp->t_rcvtime = ticks;
15933 /* Now what about ECN of a chain of pure ACKs? */
15934 if (tcp_ecn_input_segment(tp, ae->flags, 0,
15935 tcp_packets_this_ack(tp, ae->ack),
15937 rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__);
15938 #ifdef TCP_ACCOUNTING
15939 /* Count for the specific type of ack in */
15940 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
15941 tp->tcp_cnt_counters[ae->ack_val_set]++;
15945 * Note how we could move up these in the determination
15946 * above, but we don't so that way the timestamp checks (and ECN)
15947 * is done first before we do any processing on the ACK.
15948 * The non-compressed path through the code has this
15949 * weakness (noted by @jtl) that it actually does some
15950 * processing before verifying the timestamp information.
15951 * We don't take that path here which is why we set
15952 * the ack_val_set first, do the timestamp and ecn
15953 * processing, and then look at what we have setup.
15955 if (ae->ack_val_set == ACK_BEHIND) {
15957 * Case B flag reordering, if window is not closed
15958 * or it could be a keep-alive or persists
15960 if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) {
15961 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
15962 if (rack->r_ctl.rc_reorder_ts == 0)
15963 rack->r_ctl.rc_reorder_ts = 1;
15965 } else if (ae->ack_val_set == ACK_DUPACK) {
15967 rack_strike_dupack(rack);
15968 } else if (ae->ack_val_set == ACK_RWND) {
15970 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
15971 ts.tv_sec = ae->timestamp / 1000000000;
15972 ts.tv_nsec = ae->timestamp % 1000000000;
15973 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
15974 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
15976 rack->r_ctl.act_rcv_time = *tv;
15978 if (rack->forced_ack) {
15979 rack_handle_probe_response(rack, tiwin,
15980 tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
15982 #ifdef TCP_ACCOUNTING
15985 win_upd_ack = ae->ack;
15988 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
15991 if (SEQ_GT(ae->ack, tp->snd_max)) {
15993 * We just send an ack since the incoming
15994 * ack is beyond the largest seq we sent.
15996 if ((tp->t_flags & TF_ACKNOW) == 0) {
15997 ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt);
15998 if (tp->t_flags && TF_ACKNOW)
15999 rack->r_wanted_output = 1;
16003 /* If the window changed setup to update */
16004 if (tiwin != tp->snd_wnd) {
16005 win_upd_ack = ae->ack;
16008 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts);
16010 #ifdef TCP_ACCOUNTING
16011 /* Account for the acks */
16012 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16013 tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz);
16016 high_seq = ae->ack;
16017 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp))
16018 rack_log_hystart_event(rack, high_seq, 8);
16019 /* Setup our act_rcv_time */
16020 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) {
16021 ts.tv_sec = ae->timestamp / 1000000000;
16022 ts.tv_nsec = ae->timestamp % 1000000000;
16023 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16024 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16026 rack->r_ctl.act_rcv_time = *tv;
16028 rack_process_to_cumack(tp, rack, ae->ack, cts, to,
16029 tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time));
16030 #ifdef TCP_REQUEST_TRK
16031 rack_req_check_for_comp(rack, high_seq);
16033 if (rack->rc_dsack_round_seen) {
16034 /* Is the dsack round over? */
16035 if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) {
16037 rack->rc_dsack_round_seen = 0;
16038 rack_log_dsack_event(rack, 3, __LINE__, 0, 0);
16043 /* And lets be sure to commit the rtt measurements for this ack */
16044 tcp_rack_xmit_timer_commit(rack, tp);
16045 #ifdef TCP_ACCOUNTING
16046 rdstc = get_cyclecount();
16047 if (rdstc > ts_val) {
16048 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16049 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val);
16050 if (ae->ack_val_set == ACK_CUMACK)
16051 tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val);
16056 #ifdef TCP_ACCOUNTING
16057 ts_val = get_cyclecount();
16059 /* Tend to any collapsed window */
16060 if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) {
16061 /* The peer collapsed the window */
16062 rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__);
16063 } else if (rack->rc_has_collapsed)
16064 rack_un_collapse_window(rack, __LINE__);
16065 if ((rack->r_collapse_point_valid) &&
16066 (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point)))
16067 rack->r_collapse_point_valid = 0;
16068 acked_amount = acked = (high_seq - tp->snd_una);
16071 * The draft (v3) calls for us to use SEQ_GEQ, but that
16072 * causes issues when we are just going app limited. Lets
16073 * instead use SEQ_GT <or> where its equal but more data
16076 * Also make sure we are on the last ack of a series. We
16077 * have to have all the ack's processed in queue to know
16078 * if there is something left outstanding.
16081 if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) &&
16082 (rack->rc_new_rnd_needed == 0) &&
16084 rack_log_hystart_event(rack, high_seq, 21);
16085 rack->r_ctl.current_round++;
16086 /* Force the next send to setup the next round */
16087 rack->rc_new_rnd_needed = 1;
16088 if (CC_ALGO(tp)->newround != NULL) {
16089 CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
16093 * Clear the probe not answered flag
16094 * since cum-ack moved forward.
16096 rack->probe_not_answered = 0;
16097 if (rack->sack_attack_disable == 0)
16098 rack_do_decay(rack);
16099 if (acked >= segsiz) {
16101 * You only get credit for
16102 * MSS and greater (and you get extra
16103 * credit for larger cum-ack moves).
16107 ac = acked / segsiz;
16108 rack->r_ctl.ack_count += ac;
16109 counter_u64_add(rack_ack_total, ac);
16111 if (rack->r_ctl.ack_count > 0xfff00000) {
16113 * reduce the number to keep us under
16116 rack->r_ctl.ack_count /= 2;
16117 rack->r_ctl.sack_count /= 2;
16119 if (tp->t_flags & TF_NEEDSYN) {
16121 * T/TCP: Connection was half-synchronized, and our SYN has
16122 * been ACK'd (so connection is now fully synchronized). Go
16123 * to non-starred state, increment snd_una for ACK of SYN,
16124 * and check if we can do window scaling.
16126 tp->t_flags &= ~TF_NEEDSYN;
16128 acked_amount = acked = (high_seq - tp->snd_una);
16130 if (acked > sbavail(&so->so_snd))
16131 acked_amount = sbavail(&so->so_snd);
16132 #ifdef TCP_SAD_DETECTION
16134 * We only care on a cum-ack move if we are in a sack-disabled
16135 * state. We have already added in to the ack_count, and we never
16136 * would disable on a cum-ack move, so we only care to do the
16137 * detection if it may "undo" it, i.e. we were in disabled already.
16139 if (rack->sack_attack_disable)
16140 rack_do_detection(tp, rack, acked_amount, segsiz);
16142 if (IN_FASTRECOVERY(tp->t_flags) &&
16143 (rack->rack_no_prr == 0))
16144 rack_update_prr(tp, rack, acked_amount, high_seq);
16145 if (IN_RECOVERY(tp->t_flags)) {
16146 if (SEQ_LT(high_seq, tp->snd_recover) &&
16147 (SEQ_LT(high_seq, tp->snd_max))) {
16148 tcp_rack_partialack(tp);
16150 rack_post_recovery(tp, high_seq);
16154 /* Handle the rack-log-ack part (sendmap) */
16155 if ((sbused(&so->so_snd) == 0) &&
16156 (acked > acked_amount) &&
16157 (tp->t_state >= TCPS_FIN_WAIT_1) &&
16158 (tp->t_flags & TF_SENTFIN)) {
16160 * We must be sure our fin
16161 * was sent and acked (we can be
16162 * in FIN_WAIT_1 without having
16167 * Lets make sure snd_una is updated
16168 * since most likely acked_amount = 0 (it
16171 tp->snd_una = high_seq;
16173 /* Did we make a RTO error? */
16174 if ((tp->t_flags & TF_PREVVALID) &&
16175 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) {
16176 tp->t_flags &= ~TF_PREVVALID;
16177 if (tp->t_rxtshift == 1 &&
16178 (int)(ticks - tp->t_badrxtwin) < 0)
16179 rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__);
16181 /* Handle the data in the socket buffer */
16182 KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1);
16183 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
16184 if (acked_amount > 0) {
16185 struct mbuf *mfree;
16187 rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery);
16188 SOCKBUF_LOCK(&so->so_snd);
16189 mfree = sbcut_locked(&so->so_snd, acked_amount);
16190 tp->snd_una = high_seq;
16191 /* Note we want to hold the sb lock through the sendmap adjust */
16192 rack_adjust_sendmap_head(rack, &so->so_snd);
16193 /* Wake up the socket if we have room to write more */
16194 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2);
16195 sowwakeup_locked(so);
16196 if ((recovery == 1) &&
16197 (rack->excess_rxt_on) &&
16198 (rack->r_cwnd_was_clamped == 0)) {
16199 do_rack_excess_rxt(tp, rack);
16200 } else if (rack->r_cwnd_was_clamped)
16201 do_rack_check_for_unclamp(tp, rack);
16204 /* update progress */
16205 tp->t_acktime = ticks;
16206 rack_log_progress_event(rack, tp, tp->t_acktime,
16207 PROGRESS_UPDATE, __LINE__);
16208 /* Clear out shifts and such */
16209 tp->t_rxtshift = 0;
16210 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
16211 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop);
16212 rack->rc_tlp_in_progress = 0;
16213 rack->r_ctl.rc_tlp_cnt_out = 0;
16214 /* Send recover and snd_nxt must be dragged along */
16215 if (SEQ_GT(tp->snd_una, tp->snd_recover))
16216 tp->snd_recover = tp->snd_una;
16217 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
16218 tp->snd_nxt = tp->snd_una;
16220 * If the RXT timer is running we want to
16221 * stop it, so we can restart a TLP (or new RXT).
16223 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
16224 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16225 tp->snd_wl2 = high_seq;
16227 if (under_pacing &&
16228 (rack->use_fixed_rate == 0) &&
16229 (rack->in_probe_rtt == 0) &&
16230 rack->rc_gp_dyn_mul &&
16231 rack->rc_always_pace) {
16232 /* Check if we are dragging bottom */
16233 rack_check_bottom_drag(tp, rack, so);
16235 if (tp->snd_una == tp->snd_max) {
16236 tp->t_flags &= ~TF_PREVVALID;
16237 rack->r_ctl.retran_during_recovery = 0;
16238 rack->rc_suspicious = 0;
16239 rack->r_ctl.dsack_byte_cnt = 0;
16240 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
16241 if (rack->r_ctl.rc_went_idle_time == 0)
16242 rack->r_ctl.rc_went_idle_time = 1;
16243 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
16244 if (sbavail(&tptosocket(tp)->so_snd) == 0)
16246 /* Set so we might enter persists... */
16247 rack->r_wanted_output = 1;
16248 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16249 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
16250 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16251 (sbavail(&so->so_snd) == 0) &&
16252 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
16254 * The socket was gone and the
16255 * peer sent data (not now in the past), time to
16258 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
16259 /* tcp_close will kill the inp pre-log the Reset */
16260 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
16261 #ifdef TCP_ACCOUNTING
16262 rdstc = get_cyclecount();
16263 if (rdstc > ts_val) {
16264 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16265 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16266 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16271 tp = tcp_close(tp);
16273 #ifdef TCP_ACCOUNTING
16279 * We would normally do drop-with-reset which would
16280 * send back a reset. We can't since we don't have
16281 * all the needed bits. Instead lets arrange for
16282 * a call to tcp_output(). That way since we
16283 * are in the closed state we will generate a reset.
16285 * Note if tcp_accounting is on we don't unpin since
16286 * we do that after the goto label.
16288 goto send_out_a_rst;
16290 if ((sbused(&so->so_snd) == 0) &&
16291 (tp->t_state >= TCPS_FIN_WAIT_1) &&
16292 (tp->t_flags & TF_SENTFIN)) {
16294 * If we can't receive any more data, then closing user can
16295 * proceed. Starting the timer is contrary to the
16296 * specification, but if we don't get a FIN we'll hang
16300 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16301 soisdisconnected(so);
16302 tcp_timer_activate(tp, TT_2MSL,
16303 (tcp_fast_finwait2_recycle ?
16304 tcp_finwait2_timeout :
16307 if (ourfinisacked == 0) {
16309 * We don't change to fin-wait-2 if we have our fin acked
16310 * which means we are probably in TCPS_CLOSING.
16312 tcp_state_change(tp, TCPS_FIN_WAIT_2);
16316 /* Wake up the socket if we have room to write more */
16317 if (sbavail(&so->so_snd)) {
16318 rack->r_wanted_output = 1;
16319 if (ctf_progress_timeout_check(tp, true)) {
16320 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
16321 tp, tick, PROGRESS_DROP, __LINE__);
16323 * We cheat here and don't send a RST, we should send one
16324 * when the pacer drops the connection.
16326 #ifdef TCP_ACCOUNTING
16327 rdstc = get_cyclecount();
16328 if (rdstc > ts_val) {
16329 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16330 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16331 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16336 (void)tcp_drop(tp, ETIMEDOUT);
16341 if (ourfinisacked) {
16342 switch(tp->t_state) {
16344 #ifdef TCP_ACCOUNTING
16345 rdstc = get_cyclecount();
16346 if (rdstc > ts_val) {
16347 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16348 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16349 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16358 case TCPS_LAST_ACK:
16359 #ifdef TCP_ACCOUNTING
16360 rdstc = get_cyclecount();
16361 if (rdstc > ts_val) {
16362 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16363 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16364 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16369 tp = tcp_close(tp);
16370 ctf_do_drop(m, tp);
16373 case TCPS_FIN_WAIT_1:
16374 #ifdef TCP_ACCOUNTING
16375 rdstc = get_cyclecount();
16376 if (rdstc > ts_val) {
16377 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16378 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16379 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16383 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
16384 soisdisconnected(so);
16385 tcp_timer_activate(tp, TT_2MSL,
16386 (tcp_fast_finwait2_recycle ?
16387 tcp_finwait2_timeout :
16390 tcp_state_change(tp, TCPS_FIN_WAIT_2);
16396 if (rack->r_fast_output) {
16398 * We re doing fast output.. can we expand that?
16400 rack_gain_for_fastoutput(rack, tp, so, acked_amount);
16402 #ifdef TCP_ACCOUNTING
16403 rdstc = get_cyclecount();
16404 if (rdstc > ts_val) {
16405 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16406 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val);
16407 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val);
16411 } else if (win_up_req) {
16412 rdstc = get_cyclecount();
16413 if (rdstc > ts_val) {
16414 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16415 tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val);
16420 /* Now is there a next packet, if so we are done */
16424 #ifdef TCP_ACCOUNTING
16427 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs);
16430 rack_handle_might_revert(tp, rack);
16431 ctf_calc_rwin(so, tp);
16432 if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
16434 if (tcp_output(tp) < 0) {
16435 #ifdef TCP_ACCOUNTING
16442 if (tp->t_flags2 & TF2_HPTS_CALLS)
16443 tp->t_flags2 &= ~TF2_HPTS_CALLS;
16444 rack_free_trim(rack);
16445 #ifdef TCP_ACCOUNTING
16448 rack_timer_audit(tp, rack, &so->so_snd);
16449 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs);
16453 #define TCP_LRO_TS_OPTION \
16454 ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
16455 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
16458 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
16459 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt,
16460 struct timeval *tv)
16462 struct inpcb *inp = tptoinpcb(tp);
16463 struct socket *so = tptosocket(tp);
16464 #ifdef TCP_ACCOUNTING
16467 int32_t thflags, retval, did_out = 0;
16468 int32_t way_out = 0;
16470 * cts - is the current time from tv (caller gets ts) in microseconds.
16471 * ms_cts - is the current time from tv in milliseconds.
16472 * us_cts - is the time that LRO or hardware actually got the packet in microseconds.
16474 uint32_t cts, us_cts, ms_cts;
16475 uint32_t tiwin, high_seq;
16476 struct timespec ts;
16478 struct tcp_rack *rack;
16479 struct rack_sendmap *rsm;
16480 int32_t prev_state = 0;
16482 int slot_remaining = 0;
16483 #ifdef TCP_ACCOUNTING
16484 int ack_val_set = 0xf;
16488 NET_EPOCH_ASSERT();
16489 INP_WLOCK_ASSERT(inp);
16492 * tv passed from common code is from either M_TSTMP_LRO or
16493 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present.
16495 rack = (struct tcp_rack *)tp->t_fb_ptr;
16496 if (rack->rack_deferred_inited == 0) {
16498 * If we are the connecting socket we will
16499 * hit rack_init() when no sequence numbers
16500 * are setup. This makes it so we must defer
16501 * some initialization. Call that now.
16503 rack_deferred_init(tp, rack);
16506 * Check to see if we need to skip any output plans. This
16507 * can happen in the non-LRO path where we are pacing and
16508 * must process the ack coming in but need to defer sending
16509 * anything becase a pacing timer is running.
16511 us_cts = tcp_tv_to_usectick(tv);
16512 if (m->m_flags & M_ACKCMP) {
16514 * All compressed ack's are ack's by definition so
16515 * remove any ack required flag and then do the processing.
16517 rack->rc_ack_required = 0;
16518 return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv));
16520 thflags = tcp_get_flags(th);
16521 if ((rack->rc_always_pace == 1) &&
16522 (rack->rc_ack_can_sendout_data == 0) &&
16523 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
16524 (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) {
16526 * Ok conditions are right for queuing the packets
16527 * but we do have to check the flags in the inp, it
16528 * could be, if a sack is present, we want to be awoken and
16529 * so should process the packets.
16531 slot_remaining = rack->r_ctl.rc_last_output_to - us_cts;
16532 if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) {
16536 * If there is no options, or just a
16537 * timestamp option, we will want to queue
16538 * the packets. This is the same that LRO does
16539 * and will need to change with accurate ECN.
16544 optlen = (th->th_off << 2) - sizeof(struct tcphdr);
16545 ts_ptr = (uint32_t *)(th + 1);
16546 if ((optlen == 0) ||
16547 ((optlen == TCPOLEN_TSTAMP_APPA) &&
16548 (*ts_ptr == TCP_LRO_TS_OPTION)))
16551 if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) {
16553 * It is unrealistic to think we can pace in less than
16554 * the minimum granularity of the pacer (def:250usec). So
16555 * if we have less than that time remaining we should go
16556 * ahead and allow output to be "early". We will attempt to
16557 * make up for it in any pacing time we try to apply on
16558 * the outbound packet.
16564 * If there is a RST or FIN lets dump out the bw
16565 * with a FIN the connection may go on but we
16568 if ((thflags & TH_FIN) || (thflags & TH_RST))
16569 rack_log_pacing_delay_calc(rack,
16573 rack_get_gp_est(rack), /* delRate */
16574 rack_get_lt_bw(rack), /* rttProp */
16575 20, __LINE__, NULL, 0);
16576 if (m->m_flags & M_ACKCMP) {
16577 panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp);
16579 cts = tcp_tv_to_usectick(tv);
16580 ms_cts = tcp_tv_to_mssectick(tv);
16581 nsegs = m->m_pkthdr.lro_nsegs;
16582 counter_u64_add(rack_proc_non_comp_ack, 1);
16583 #ifdef TCP_ACCOUNTING
16585 if (thflags & TH_ACK)
16586 ts_val = get_cyclecount();
16588 if ((m->m_flags & M_TSTMP) ||
16589 (m->m_flags & M_TSTMP_LRO)) {
16590 mbuf_tstmp2timespec(m, &ts);
16591 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
16592 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
16594 rack->r_ctl.act_rcv_time = *tv;
16595 kern_prefetch(rack, &prev_state);
16598 * Unscale the window into a 32-bit value. For the SYN_SENT state
16599 * the scale is zero.
16601 tiwin = th->th_win << tp->snd_scale;
16602 #ifdef TCP_ACCOUNTING
16603 if (thflags & TH_ACK) {
16605 * We have a tradeoff here. We can either do what we are
16606 * doing i.e. pinning to this CPU and then doing the accounting
16607 * <or> we could do a critical enter, setup the rdtsc and cpu
16608 * as in below, and then validate we are on the same CPU on
16609 * exit. I have choosen to not do the critical enter since
16610 * that often will gain you a context switch, and instead lock
16611 * us (line above this if) to the same CPU with sched_pin(). This
16612 * means we may be context switched out for a higher priority
16613 * interupt but we won't be moved to another CPU.
16615 * If this occurs (which it won't very often since we most likely
16616 * are running this code in interupt context and only a higher
16617 * priority will bump us ... clock?) we will falsely add in
16618 * to the time the interupt processing time plus the ack processing
16619 * time. This is ok since its a rare event.
16621 ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin,
16622 ctf_fixed_maxseg(tp));
16626 * Parse options on any incoming segment.
16628 memset(&to, 0, sizeof(to));
16629 tcp_dooptions(&to, (u_char *)(th + 1),
16630 (th->th_off << 2) - sizeof(struct tcphdr),
16631 (thflags & TH_SYN) ? TO_SYN : 0);
16632 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
16634 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
16637 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
16638 (tp->t_flags & TF_GPUTINPROG)) {
16640 * We have a goodput in progress
16641 * and we have entered a late state.
16642 * Do we have enough data in the sb
16643 * to handle the GPUT request?
16647 bytes = tp->gput_ack - tp->gput_seq;
16648 if (SEQ_GT(tp->gput_seq, tp->snd_una))
16649 bytes += tp->gput_seq - tp->snd_una;
16650 if (bytes > sbavail(&tptosocket(tp)->so_snd)) {
16652 * There are not enough bytes in the socket
16653 * buffer that have been sent to cover this
16654 * measurement. Cancel it.
16656 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
16657 rack->r_ctl.rc_gp_srtt /*flex1*/,
16659 0, 0, 18, __LINE__, NULL, 0);
16660 tp->t_flags &= ~TF_GPUTINPROG;
16663 high_seq = th->th_ack;
16664 if (tcp_bblogging_on(rack->rc_tp)) {
16665 union tcp_log_stackspecific log;
16666 struct timeval ltv;
16667 #ifdef TCP_REQUEST_TRK
16668 struct tcp_sendfile_track *tcp_req;
16670 if (SEQ_GT(th->th_ack, tp->snd_una)) {
16671 tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1));
16673 tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack);
16676 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
16677 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
16678 if (rack->rack_no_prr == 0)
16679 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
16681 log.u_bbr.flex1 = 0;
16682 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns;
16683 log.u_bbr.use_lt_bw <<= 1;
16684 log.u_bbr.use_lt_bw |= rack->r_might_revert;
16685 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
16686 log.u_bbr.bbr_state = rack->rc_free_cnt;
16687 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
16688 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
16689 log.u_bbr.flex3 = m->m_flags;
16690 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
16691 log.u_bbr.lost = thflags;
16692 log.u_bbr.pacing_gain = 0x1;
16693 #ifdef TCP_ACCOUNTING
16694 log.u_bbr.cwnd_gain = ack_val_set;
16696 log.u_bbr.flex7 = 2;
16697 if (m->m_flags & M_TSTMP) {
16698 /* Record the hardware timestamp if present */
16699 mbuf_tstmp2timespec(m, &ts);
16700 ltv.tv_sec = ts.tv_sec;
16701 ltv.tv_usec = ts.tv_nsec / 1000;
16702 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v);
16703 } else if (m->m_flags & M_TSTMP_LRO) {
16704 /* Record the LRO the arrival timestamp */
16705 mbuf_tstmp2timespec(m, &ts);
16706 ltv.tv_sec = ts.tv_sec;
16707 ltv.tv_usec = ts.tv_nsec / 1000;
16708 log.u_bbr.flex5 = tcp_tv_to_usectick(<v);
16710 log.u_bbr.timeStamp = tcp_get_usecs(<v);
16711 /* Log the rcv time */
16712 log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
16713 #ifdef TCP_REQUEST_TRK
16714 log.u_bbr.applimited = tp->t_tcpreq_closed;
16715 log.u_bbr.applimited <<= 8;
16716 log.u_bbr.applimited |= tp->t_tcpreq_open;
16717 log.u_bbr.applimited <<= 8;
16718 log.u_bbr.applimited |= tp->t_tcpreq_req;
16720 /* Copy out any client req info */
16722 log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC);
16724 log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC);
16725 log.u_bbr.rttProp = tcp_req->timestamp;
16726 log.u_bbr.cur_del_rate = tcp_req->start;
16727 if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) {
16728 log.u_bbr.flex8 |= 1;
16730 log.u_bbr.flex8 |= 2;
16731 log.u_bbr.bw_inuse = tcp_req->end;
16733 log.u_bbr.flex6 = tcp_req->start_seq;
16734 if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) {
16735 log.u_bbr.flex8 |= 4;
16736 log.u_bbr.epoch = tcp_req->end_seq;
16740 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
16741 tlen, &log, true, <v);
16743 /* Remove ack required flag if set, we have one */
16744 if (thflags & TH_ACK)
16745 rack->rc_ack_required = 0;
16746 if (rack->sack_attack_disable > 0) {
16747 rack->r_ctl.ack_during_sd++;
16748 rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__);
16750 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
16754 goto done_with_input;
16757 * If a segment with the ACK-bit set arrives in the SYN-SENT state
16758 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
16760 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
16761 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
16762 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
16763 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
16764 #ifdef TCP_ACCOUNTING
16770 * If timestamps were negotiated during SYN/ACK and a
16771 * segment without a timestamp is received, silently drop
16772 * the segment, unless it is a RST segment or missing timestamps are
16774 * See section 3.2 of RFC 7323.
16776 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
16777 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
16781 goto done_with_input;
16785 * Segment received on connection. Reset idle time and keep-alive
16786 * timer. XXX: This should be done after segment validation to
16787 * ignore broken/spoofed segs.
16789 if (tp->t_idle_reduce &&
16790 (tp->snd_max == tp->snd_una) &&
16791 (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
16792 counter_u64_add(rack_input_idle_reduces, 1);
16793 rack_cc_after_idle(rack, tp);
16795 tp->t_rcvtime = ticks;
16797 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
16799 if (tiwin > rack->r_ctl.rc_high_rwnd)
16800 rack->r_ctl.rc_high_rwnd = tiwin;
16802 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
16803 * this to occur after we've validated the segment.
16805 if (tcp_ecn_input_segment(tp, thflags, tlen,
16806 tcp_packets_this_ack(tp, th->th_ack),
16808 rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__);
16811 * If echoed timestamp is later than the current time, fall back to
16812 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
16813 * were used when this connection was established.
16815 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
16816 to.to_tsecr -= tp->ts_offset;
16817 if (TSTMP_GT(to.to_tsecr, ms_cts))
16822 * If its the first time in we need to take care of options and
16823 * verify we can do SACK for rack!
16825 if (rack->r_state == 0) {
16826 /* Should be init'd by rack_init() */
16827 KASSERT(rack->rc_inp != NULL,
16828 ("%s: rack->rc_inp unexpectedly NULL", __func__));
16829 if (rack->rc_inp == NULL) {
16830 rack->rc_inp = inp;
16834 * Process options only when we get SYN/ACK back. The SYN
16835 * case for incoming connections is handled in tcp_syncache.
16836 * According to RFC1323 the window field in a SYN (i.e., a
16837 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
16838 * this is traditional behavior, may need to be cleaned up.
16840 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16841 /* Handle parallel SYN for ECN */
16842 tcp_ecn_input_parallel_syn(tp, thflags, iptos);
16843 if ((to.to_flags & TOF_SCALE) &&
16844 (tp->t_flags & TF_REQ_SCALE)) {
16845 tp->t_flags |= TF_RCVD_SCALE;
16846 tp->snd_scale = to.to_wscale;
16848 tp->t_flags &= ~TF_REQ_SCALE;
16850 * Initial send window. It will be updated with the
16851 * next incoming segment to the scaled value.
16853 tp->snd_wnd = th->th_win;
16854 rack_validate_fo_sendwin_up(tp, rack);
16855 if ((to.to_flags & TOF_TS) &&
16856 (tp->t_flags & TF_REQ_TSTMP)) {
16857 tp->t_flags |= TF_RCVD_TSTMP;
16858 tp->ts_recent = to.to_tsval;
16859 tp->ts_recent_age = cts;
16861 tp->t_flags &= ~TF_REQ_TSTMP;
16862 if (to.to_flags & TOF_MSS) {
16863 tcp_mss(tp, to.to_mss);
16865 if ((tp->t_flags & TF_SACK_PERMIT) &&
16866 (to.to_flags & TOF_SACKPERM) == 0)
16867 tp->t_flags &= ~TF_SACK_PERMIT;
16868 if (IS_FASTOPEN(tp->t_flags)) {
16869 if (to.to_flags & TOF_FASTOPEN) {
16872 if (to.to_flags & TOF_MSS)
16875 if ((inp->inp_vflag & INP_IPV6) != 0)
16879 tcp_fastopen_update_cache(tp, mss,
16880 to.to_tfo_len, to.to_tfo_cookie);
16882 tcp_fastopen_disable_path(tp);
16886 * At this point we are at the initial call. Here we decide
16887 * if we are doing RACK or not. We do this by seeing if
16888 * TF_SACK_PERMIT is set and the sack-not-required is clear.
16889 * The code now does do dup-ack counting so if you don't
16890 * switch back you won't get rack & TLP, but you will still
16894 if ((rack_sack_not_required == 0) &&
16895 ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
16896 tcp_switch_back_to_default(tp);
16897 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen,
16899 #ifdef TCP_ACCOUNTING
16905 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
16907 if (thflags & TH_FIN)
16908 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
16909 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
16910 if ((rack->rc_gp_dyn_mul) &&
16911 (rack->use_fixed_rate == 0) &&
16912 (rack->rc_always_pace)) {
16913 /* Check in on probertt */
16914 rack_check_probe_rtt(rack, us_cts);
16916 rack_clear_rate_sample(rack);
16917 if ((rack->forced_ack) &&
16918 ((tcp_get_flags(th) & TH_RST) == 0)) {
16919 rack_handle_probe_response(rack, tiwin, us_cts);
16922 * This is the one exception case where we set the rack state
16923 * always. All other times (timers etc) we must have a rack-state
16924 * set (so we assure we have done the checks above for SACK).
16926 rack->r_ctl.rc_rcvtime = cts;
16927 if (rack->r_state != tp->t_state)
16928 rack_set_state(tp, rack);
16929 if (SEQ_GT(th->th_ack, tp->snd_una) &&
16930 (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL)
16931 kern_prefetch(rsm, &prev_state);
16932 prev_state = rack->r_state;
16933 if ((thflags & TH_RST) &&
16934 ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
16935 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
16936 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) {
16937 /* The connection will be killed by a reset check the tracepoint */
16938 tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV);
16940 retval = (*rack->r_substate) (m, th, so,
16941 tp, &to, drop_hdrlen,
16942 tlen, tiwin, thflags, nxt_pkt, iptos);
16945 * If retval is 1 the tcb is unlocked and most likely the tp
16948 INP_WLOCK_ASSERT(inp);
16949 if ((rack->rc_gp_dyn_mul) &&
16950 (rack->rc_always_pace) &&
16951 (rack->use_fixed_rate == 0) &&
16952 rack->in_probe_rtt &&
16953 (rack->r_ctl.rc_time_probertt_starts == 0)) {
16955 * If we are going for target, lets recheck before
16958 rack_check_probe_rtt(rack, us_cts);
16960 if (rack->set_pacing_done_a_iw == 0) {
16961 /* How much has been acked? */
16962 if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
16963 /* We have enough to set in the pacing segment size */
16964 rack->set_pacing_done_a_iw = 1;
16965 rack_set_pace_segments(tp, rack, __LINE__, NULL);
16968 tcp_rack_xmit_timer_commit(rack, tp);
16969 #ifdef TCP_ACCOUNTING
16971 * If we set the ack_val_se to what ack processing we are doing
16972 * we also want to track how many cycles we burned. Note
16973 * the bits after tcp_output we let be "free". This is because
16974 * we are also tracking the tcp_output times as well. Note the
16975 * use of 0xf here since we only have 11 counter (0 - 0xa) and
16976 * 0xf cannot be returned and is what we initialize it too to
16977 * indicate we are not doing the tabulations.
16979 if (ack_val_set != 0xf) {
16982 crtsc = get_cyclecount();
16983 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
16984 tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val);
16988 if ((nxt_pkt == 0) && (no_output == 0)) {
16989 if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) {
16991 if (tcp_output(tp) < 0) {
16992 #ifdef TCP_ACCOUNTING
16999 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
17000 rack_free_trim(rack);
17001 } else if ((no_output == 1) &&
17003 (tcp_in_hpts(rack->rc_tp) == 0)) {
17005 * We are not in hpts and we had a pacing timer up. Use
17006 * the remaining time (slot_remaining) to restart the timer.
17008 KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp));
17009 rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0);
17010 rack_free_trim(rack);
17012 /* Clear the flag, it may have been cleared by output but we may not have */
17013 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS))
17014 tp->t_flags2 &= ~TF2_HPTS_CALLS;
17015 /* Update any rounds needed */
17016 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp))
17017 rack_log_hystart_event(rack, high_seq, 8);
17019 * The draft (v3) calls for us to use SEQ_GEQ, but that
17020 * causes issues when we are just going app limited. Lets
17021 * instead use SEQ_GT <or> where its equal but more data
17024 * Also make sure we are on the last ack of a series. We
17025 * have to have all the ack's processed in queue to know
17026 * if there is something left outstanding.
17028 if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) &&
17029 (rack->rc_new_rnd_needed == 0) &&
17031 rack_log_hystart_event(rack, tp->snd_una, 21);
17032 rack->r_ctl.current_round++;
17033 /* Force the next send to setup the next round */
17034 rack->rc_new_rnd_needed = 1;
17035 if (CC_ALGO(tp)->newround != NULL) {
17036 CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round);
17039 if ((nxt_pkt == 0) &&
17040 ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
17041 (SEQ_GT(tp->snd_max, tp->snd_una) ||
17042 (tp->t_flags & TF_DELACK) ||
17043 ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
17044 (tp->t_state <= TCPS_CLOSING)))) {
17045 /* We could not send (probably in the hpts but stopped the timer earlier)? */
17046 if ((tp->snd_max == tp->snd_una) &&
17047 ((tp->t_flags & TF_DELACK) == 0) &&
17048 (tcp_in_hpts(rack->rc_tp)) &&
17049 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
17050 /* keep alive not needed if we are hptsi output yet */
17054 if (tcp_in_hpts(tp)) {
17055 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
17056 us_cts = tcp_get_usecs(NULL);
17057 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
17059 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
17062 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
17064 tcp_hpts_remove(tp);
17066 if (late && (did_out == 0)) {
17068 * We are late in the sending
17069 * and we did not call the output
17070 * (this probably should not happen).
17072 goto do_output_now;
17074 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0);
17077 } else if (nxt_pkt == 0) {
17078 /* Do we have the correct timer running? */
17079 rack_timer_audit(tp, rack, &so->so_snd);
17083 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs));
17085 rack->r_wanted_output = 0;
17087 #ifdef TCP_ACCOUNTING
17094 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
17095 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
17099 /* First lets see if we have old packets */
17100 if (!STAILQ_EMPTY(&tp->t_inqueue)) {
17101 if (ctf_do_queued_segments(tp, 1)) {
17106 if (m->m_flags & M_TSTMP_LRO) {
17107 mbuf_tstmp2timeval(m, &tv);
17109 /* Should not be should we kassert instead? */
17110 tcp_get_usecs(&tv);
17112 if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0,
17114 INP_WUNLOCK(tptoinpcb(tp));
17118 struct rack_sendmap *
17119 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
17121 struct rack_sendmap *rsm = NULL;
17123 uint32_t srtt = 0, thresh = 0, ts_low = 0;
17126 /* Return the next guy to be re-transmitted */
17127 if (tqhash_empty(rack->r_ctl.tqh)) {
17130 if (tp->t_flags & TF_SENTFIN) {
17131 /* retran the end FIN? */
17134 /* ok lets look at this one */
17135 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
17136 if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) {
17139 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
17142 rsm = rack_find_lowest_rsm(rack);
17147 if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) ||
17148 (rack->sack_attack_disable > 0)) {
17151 if ((no_sack > 0) &&
17152 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
17154 * No sack so we automatically do the 3 strikes and
17155 * retransmit (no rack timer would be started).
17159 if (rsm->r_flags & RACK_ACKED) {
17162 if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
17163 (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
17164 /* Its not yet ready */
17167 srtt = rack_grab_rtt(tp, rack);
17168 idx = rsm->r_rtr_cnt - 1;
17169 ts_low = (uint32_t)rsm->r_tim_lastsent[idx];
17170 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
17171 if ((tsused == ts_low) ||
17172 (TSTMP_LT(tsused, ts_low))) {
17173 /* No time since sending */
17176 if ((tsused - ts_low) < thresh) {
17177 /* It has not been long enough yet */
17180 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
17181 ((rsm->r_flags & RACK_SACK_PASSED) &&
17182 (rack->sack_attack_disable == 0))) {
17184 * We have passed the dup-ack threshold <or>
17185 * a SACK has indicated this is missing.
17186 * Note that if you are a declared attacker
17187 * it is only the dup-ack threshold that
17188 * will cause retransmits.
17190 /* log retransmit reason */
17191 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
17192 rack->r_fast_output = 0;
17199 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
17200 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
17201 int line, struct rack_sendmap *rsm, uint8_t quality)
17203 if (tcp_bblogging_on(rack->rc_tp)) {
17204 union tcp_log_stackspecific log;
17207 memset(&log, 0, sizeof(log));
17208 log.u_bbr.flex1 = slot;
17209 log.u_bbr.flex2 = len;
17210 log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
17211 log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
17212 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
17213 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
17214 log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data;
17215 log.u_bbr.use_lt_bw <<= 1;
17216 log.u_bbr.use_lt_bw |= rack->r_late;
17217 log.u_bbr.use_lt_bw <<= 1;
17218 log.u_bbr.use_lt_bw |= rack->r_early;
17219 log.u_bbr.use_lt_bw <<= 1;
17220 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
17221 log.u_bbr.use_lt_bw <<= 1;
17222 log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
17223 log.u_bbr.use_lt_bw <<= 1;
17224 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
17225 log.u_bbr.use_lt_bw <<= 1;
17226 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
17227 log.u_bbr.use_lt_bw <<= 1;
17228 log.u_bbr.use_lt_bw |= rack->gp_ready;
17229 log.u_bbr.pkt_epoch = line;
17230 log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed;
17231 log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early;
17232 log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
17233 log.u_bbr.bw_inuse = bw_est;
17234 log.u_bbr.delRate = bw;
17235 if (rack->r_ctl.gp_bw == 0)
17236 log.u_bbr.cur_del_rate = 0;
17238 log.u_bbr.cur_del_rate = rack_get_bw(rack);
17239 log.u_bbr.rttProp = len_time;
17240 log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
17241 log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
17242 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
17243 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
17244 /* We are in slow start */
17245 log.u_bbr.flex7 = 1;
17247 /* we are on congestion avoidance */
17248 log.u_bbr.flex7 = 0;
17250 log.u_bbr.flex8 = method;
17251 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
17252 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
17253 log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
17254 log.u_bbr.cwnd_gain <<= 1;
17255 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
17256 log.u_bbr.cwnd_gain <<= 1;
17257 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
17258 log.u_bbr.bbr_substate = quality;
17259 log.u_bbr.bbr_state = rack->dgp_on;
17260 log.u_bbr.bbr_state <<= 1;
17261 log.u_bbr.bbr_state |= rack->r_fill_less_agg;
17262 log.u_bbr.bbr_state <<= 1;
17263 log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd;
17264 log.u_bbr.bbr_state <<= 2;
17265 log.u_bbr.bbr_state |= rack->r_pacing_discount;
17266 log.u_bbr.flex7 = ((rack->r_ctl.pacing_discount_amm << 1) | log.u_bbr.flex7);
17267 TCP_LOG_EVENTP(rack->rc_tp, NULL,
17268 &rack->rc_inp->inp_socket->so_rcv,
17269 &rack->rc_inp->inp_socket->so_snd,
17270 BBR_LOG_HPTSI_CALC, 0,
17271 0, &log, false, &tv);
17276 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
17278 uint32_t new_tso, user_max, pace_one;
17280 user_max = rack->rc_user_set_max_segs * mss;
17281 if (rack->rc_force_max_seg) {
17284 if (rack->use_fixed_rate &&
17285 ((rack->r_ctl.crte == NULL) ||
17286 (bw != rack->r_ctl.crte->rate))) {
17287 /* Use the user mss since we are not exactly matched */
17290 if (rack_pace_one_seg ||
17291 (rack->r_ctl.rc_user_set_min_segs == 1))
17296 new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss,
17297 pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor);
17298 if (new_tso > user_max)
17299 new_tso = user_max;
17300 if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) {
17301 if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso)
17302 new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss;
17304 if (rack->r_ctl.rc_user_set_min_segs &&
17305 ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso))
17306 new_tso = rack->r_ctl.rc_user_set_min_segs * mss;
17311 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)
17313 uint64_t lentim, fill_bw;
17315 /* Lets first see if we are full, if so continue with normal rate */
17316 rack->r_via_fill_cw = 0;
17317 if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
17319 if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
17321 if (rack->r_ctl.rc_last_us_rtt == 0)
17323 if (rack->rc_pace_fill_if_rttin_range &&
17324 (rack->r_ctl.rc_last_us_rtt >=
17325 (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
17326 /* The rtt is huge, N * smallest, lets not fill */
17330 * first lets calculate the b/w based on the last us-rtt
17331 * and the the smallest send window.
17333 fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use);
17334 /* Take the rwnd if its smaller */
17335 if (fill_bw > rack->rc_tp->snd_wnd)
17336 fill_bw = rack->rc_tp->snd_wnd;
17337 /* Now lets make it into a b/w */
17338 fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
17339 fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
17340 if (rack->r_fill_less_agg) {
17342 * We want the average of the rate_wanted
17343 * and our fill-cw calculated bw. We also want
17344 * to cap any increase to be no more than
17345 * X times the lt_bw (where X is the rack_bw_multipler).
17347 uint64_t lt_bw, rate;
17349 lt_bw = rack_get_lt_bw(rack);
17350 if (lt_bw > *rate_wanted)
17353 rate = *rate_wanted;
17356 if (rack_bw_multipler && (fill_bw > (rate * rack_bw_multipler))) {
17357 fill_bw = rate * rack_bw_multipler;
17360 /* We are below the min b/w */
17362 *rate_wanted = fill_bw;
17363 if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted))
17365 rack->r_via_fill_cw = 1;
17366 if (rack->r_rack_hw_rate_caps &&
17367 (rack->r_ctl.crte != NULL)) {
17368 uint64_t high_rate;
17370 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte);
17371 if (fill_bw > high_rate) {
17372 /* We are capping bw at the highest rate table entry */
17373 if (*rate_wanted > high_rate) {
17374 /* The original rate was also capped */
17375 rack->r_via_fill_cw = 0;
17377 rack_log_hdwr_pacing(rack,
17378 fill_bw, high_rate, __LINE__,
17380 fill_bw = high_rate;
17384 } else if ((rack->r_ctl.crte == NULL) &&
17385 (rack->rack_hdrw_pacing == 0) &&
17386 (rack->rack_hdw_pace_ena) &&
17387 rack->r_rack_hw_rate_caps &&
17388 (rack->rack_attempt_hdwr_pace == 0) &&
17389 (rack->rc_inp->inp_route.ro_nh != NULL) &&
17390 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17392 * Ok we may have a first attempt that is greater than our top rate
17395 uint64_t high_rate;
17397 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp);
17399 if (fill_bw > high_rate) {
17400 fill_bw = high_rate;
17406 if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) {
17407 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max,
17408 fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL);
17409 fill_bw = rack->r_ctl.bw_rate_cap;
17412 * Ok fill_bw holds our mythical b/w to fill the cwnd
17413 * in an rtt (unless it was capped), what does that
17414 * time wise equate too?
17416 lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
17418 *rate_wanted = fill_bw;
17419 if (non_paced || (lentim < slot)) {
17420 rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
17421 0, lentim, 12, __LINE__, NULL, 0);
17422 return ((int32_t)lentim);
17428 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
17432 int32_t minslot = 0;
17433 int can_start_hw_pacing = 1;
17437 if (rack_pace_one_seg ||
17438 (rack->r_ctl.rc_user_set_min_segs == 1))
17442 if (rack->rc_always_pace == 0) {
17444 * We use the most optimistic possible cwnd/srtt for
17445 * sending calculations. This will make our
17446 * calculation anticipate getting more through
17447 * quicker then possible. But thats ok we don't want
17448 * the peer to have a gap in data sending.
17450 uint64_t cwnd, tr_perms = 0;
17451 int32_t reduce = 0;
17455 * We keep no precise pacing with the old method
17456 * instead we use the pacer to mitigate bursts.
17458 if (rack->r_ctl.rc_rack_min_rtt)
17459 srtt = rack->r_ctl.rc_rack_min_rtt;
17461 srtt = max(tp->t_srtt, 1);
17462 if (rack->r_ctl.rc_rack_largest_cwnd)
17463 cwnd = rack->r_ctl.rc_rack_largest_cwnd;
17465 cwnd = rack->r_ctl.cwnd_to_use;
17466 /* Inflate cwnd by 1000 so srtt of usecs is in ms */
17467 tr_perms = (cwnd * 1000) / srtt;
17468 if (tr_perms == 0) {
17469 tr_perms = ctf_fixed_maxseg(tp);
17472 * Calculate how long this will take to drain, if
17473 * the calculation comes out to zero, thats ok we
17474 * will use send_a_lot to possibly spin around for
17475 * more increasing tot_len_this_send to the point
17476 * that its going to require a pace, or we hit the
17477 * cwnd. Which in that case we are just waiting for
17480 slot = len / tr_perms;
17481 /* Now do we reduce the time so we don't run dry? */
17482 if (slot && rack_slot_reduction) {
17483 reduce = (slot / rack_slot_reduction);
17484 if (reduce < slot) {
17489 slot *= HPTS_USEC_IN_MSEC;
17490 if (rack->rc_pace_to_cwnd) {
17491 uint64_t rate_wanted = 0;
17493 slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1);
17494 rack->rc_ack_can_sendout_data = 1;
17495 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0);
17497 rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0);
17498 /*******************************************************/
17499 /* RRS: We insert non-paced call to stats here for len */
17500 /*******************************************************/
17502 uint64_t bw_est, res, lentim, rate_wanted;
17507 if ((rack->r_rr_config == 1) && rsm) {
17508 return (rack->r_ctl.rc_min_to);
17510 if (rack->use_fixed_rate) {
17511 rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
17512 } else if ((rack->r_ctl.init_rate == 0) &&
17513 (rack->r_ctl.gp_bw == 0)) {
17514 /* no way to yet do an estimate */
17515 bw_est = rate_wanted = 0;
17517 bw_est = rack_get_bw(rack);
17518 rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped);
17520 if ((bw_est == 0) || (rate_wanted == 0) ||
17521 ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) {
17523 * No way yet to make a b/w estimate or
17524 * our raise is set incorrectly.
17528 rack_rate_cap_bw(rack, &rate_wanted, &capped);
17529 /* We need to account for all the overheads */
17530 segs = (len + segsiz - 1) / segsiz;
17532 * We need the diff between 1514 bytes (e-mtu with e-hdr)
17533 * and how much data we put in each packet. Yes this
17534 * means we may be off if we are larger than 1500 bytes
17535 * or smaller. But this just makes us more conservative.
17538 oh = (tp->t_maxseg - segsiz) + sizeof(struct tcphdr);
17539 if (rack->r_is_v6) {
17541 oh += sizeof(struct ip6_hdr);
17545 oh += sizeof(struct ip);
17548 /* We add a fixed 14 for the ethernet header */
17551 lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
17552 res = lentim / rate_wanted;
17553 slot = (uint32_t)res;
17554 if (rack_hw_rate_min &&
17555 (rate_wanted < rack_hw_rate_min)) {
17556 can_start_hw_pacing = 0;
17557 if (rack->r_ctl.crte) {
17559 * Ok we need to release it, we
17560 * have fallen too low.
17562 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17563 rack->r_ctl.crte = NULL;
17564 rack->rack_attempt_hdwr_pace = 0;
17565 rack->rack_hdrw_pacing = 0;
17568 if (rack->r_ctl.crte &&
17569 (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17571 * We want more than the hardware can give us,
17572 * don't start any hw pacing.
17574 can_start_hw_pacing = 0;
17575 if (rack->r_rack_hw_rate_caps == 0) {
17577 * Ok we need to release it, we
17578 * want more than the card can give us and
17579 * no rate cap is in place. Set it up so
17580 * when we want less we can retry.
17582 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17583 rack->r_ctl.crte = NULL;
17584 rack->rack_attempt_hdwr_pace = 0;
17585 rack->rack_hdrw_pacing = 0;
17588 if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) {
17590 * We lost our rate somehow, this can happen
17591 * if the interface changed underneath us.
17593 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17594 rack->r_ctl.crte = NULL;
17595 /* Lets re-allow attempting to setup pacing */
17596 rack->rack_hdrw_pacing = 0;
17597 rack->rack_attempt_hdwr_pace = 0;
17598 rack_log_hdwr_pacing(rack,
17599 rate_wanted, bw_est, __LINE__,
17602 prev_fill = rack->r_via_fill_cw;
17603 if ((rack->rc_pace_to_cwnd) &&
17605 (rack->use_fixed_rate == 0) &&
17606 (rack->in_probe_rtt == 0) &&
17607 (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) {
17609 * We want to pace at our rate *or* faster to
17610 * fill the cwnd to the max if its not full.
17612 slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0);
17613 /* Re-check to make sure we are not exceeding our max b/w */
17614 if ((rack->r_ctl.crte != NULL) &&
17615 (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) {
17617 * We want more than the hardware can give us,
17618 * don't start any hw pacing.
17620 can_start_hw_pacing = 0;
17621 if (rack->r_rack_hw_rate_caps == 0) {
17623 * Ok we need to release it, we
17624 * want more than the card can give us and
17625 * no rate cap is in place. Set it up so
17626 * when we want less we can retry.
17628 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17629 rack->r_ctl.crte = NULL;
17630 rack->rack_attempt_hdwr_pace = 0;
17631 rack->rack_hdrw_pacing = 0;
17632 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
17636 if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
17637 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
17638 if ((rack->rack_hdw_pace_ena) &&
17639 (can_start_hw_pacing > 0) &&
17640 (rack->rack_hdrw_pacing == 0) &&
17641 (rack->rack_attempt_hdwr_pace == 0)) {
17643 * Lets attempt to turn on hardware pacing
17646 rack->rack_attempt_hdwr_pace = 1;
17647 rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
17648 rack->rc_inp->inp_route.ro_nh->nh_ifp,
17651 &err, &rack->r_ctl.crte_prev_rate);
17652 if (rack->r_ctl.crte) {
17653 rack->rack_hdrw_pacing = 1;
17654 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz,
17655 pace_one, rack->r_ctl.crte,
17656 NULL, rack->r_ctl.pace_len_divisor);
17657 rack_log_hdwr_pacing(rack,
17658 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17660 rack->r_ctl.last_hw_bw_req = rate_wanted;
17662 counter_u64_add(rack_hw_pace_init_fail, 1);
17664 } else if (rack->rack_hdrw_pacing &&
17665 (rack->r_ctl.last_hw_bw_req != rate_wanted)) {
17666 /* Do we need to adjust our rate? */
17667 const struct tcp_hwrate_limit_table *nrte;
17669 if (rack->r_up_only &&
17670 (rate_wanted < rack->r_ctl.crte->rate)) {
17672 * We have four possible states here
17673 * having to do with the previous time
17675 * previous | this-time
17676 * A) 0 | 0 -- fill_cw not in the picture
17677 * B) 1 | 0 -- we were doing a fill-cw but now are not
17678 * C) 1 | 1 -- all rates from fill_cw
17679 * D) 0 | 1 -- we were doing non-fill and now we are filling
17681 * For case A, C and D we don't allow a drop. But for
17682 * case B where we now our on our steady rate we do
17686 if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0)))
17689 if ((rate_wanted > rack->r_ctl.crte->rate) ||
17690 (rate_wanted <= rack->r_ctl.crte_prev_rate)) {
17691 if (rack_hw_rate_to_low &&
17692 (bw_est < rack_hw_rate_to_low)) {
17694 * The pacing rate is too low for hardware, but
17695 * do allow hardware pacing to be restarted.
17697 rack_log_hdwr_pacing(rack,
17698 bw_est, rack->r_ctl.crte->rate, __LINE__,
17700 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp);
17701 rack->r_ctl.crte = NULL;
17702 rack->rack_attempt_hdwr_pace = 0;
17703 rack->rack_hdrw_pacing = 0;
17704 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17707 nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
17709 rack->rc_inp->inp_route.ro_nh->nh_ifp,
17712 &err, &rack->r_ctl.crte_prev_rate);
17713 if (nrte == NULL) {
17715 * Lost the rate, lets drop hardware pacing
17718 rack->rack_hdrw_pacing = 0;
17719 rack->r_ctl.crte = NULL;
17720 rack_log_hdwr_pacing(rack,
17721 rate_wanted, 0, __LINE__,
17723 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17724 counter_u64_add(rack_hw_pace_lost, 1);
17725 } else if (nrte != rack->r_ctl.crte) {
17726 rack->r_ctl.crte = nrte;
17727 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted,
17728 segsiz, pace_one, rack->r_ctl.crte,
17729 NULL, rack->r_ctl.pace_len_divisor);
17730 rack_log_hdwr_pacing(rack,
17731 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17733 rack->r_ctl.last_hw_bw_req = rate_wanted;
17736 /* We just need to adjust the segment size */
17737 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted);
17738 rack_log_hdwr_pacing(rack,
17739 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
17741 rack->r_ctl.last_hw_bw_req = rate_wanted;
17745 if (minslot && (minslot > slot)) {
17746 rack_log_pacing_delay_calc(rack, minslot, slot, rack->r_ctl.crte->rate, bw_est, lentim,
17747 98, __LINE__, NULL, 0);
17751 if (rack_limit_time_with_srtt &&
17752 (rack->use_fixed_rate == 0) &&
17753 (rack->rack_hdrw_pacing == 0)) {
17755 * Sanity check, we do not allow the pacing delay
17756 * to be longer than the SRTT of the path. If it is
17757 * a slow path, then adding a packet should increase
17758 * the RTT and compensate for this i.e. the srtt will
17759 * be greater so the allowed pacing time will be greater.
17761 * Note this restriction is not for where a peak rate
17762 * is set, we are doing fixed pacing or hardware pacing.
17764 if (rack->rc_tp->t_srtt)
17765 srtt = rack->rc_tp->t_srtt;
17767 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC; /* its in ms convert */
17768 if (srtt < (uint64_t)slot) {
17769 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0);
17773 /*******************************************************************/
17774 /* RRS: We insert paced call to stats here for len and rate_wanted */
17775 /*******************************************************************/
17776 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0);
17778 if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) {
17780 * If this rate is seeing enobufs when it
17781 * goes to send then either the nic is out
17782 * of gas or we are mis-estimating the time
17783 * somehow and not letting the queue empty
17784 * completely. Lets add to the pacing time.
17786 int hw_boost_delay;
17788 hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult;
17789 if (hw_boost_delay > rack_enobuf_hw_max)
17790 hw_boost_delay = rack_enobuf_hw_max;
17791 else if (hw_boost_delay < rack_enobuf_hw_min)
17792 hw_boost_delay = rack_enobuf_hw_min;
17793 slot += hw_boost_delay;
17799 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
17800 tcp_seq startseq, uint32_t sb_offset)
17802 struct rack_sendmap *my_rsm = NULL;
17804 if (tp->t_state < TCPS_ESTABLISHED) {
17806 * We don't start any measurements if we are
17807 * not at least established.
17811 if (tp->t_state >= TCPS_FIN_WAIT_1) {
17813 * We will get no more data into the SB
17814 * this means we need to have the data available
17815 * before we start a measurement.
17818 if (sbavail(&tptosocket(tp)->so_snd) <
17819 max(rc_init_window(rack),
17820 (MIN_GP_WIN * ctf_fixed_maxseg(tp)))) {
17821 /* Nope not enough data */
17825 tp->t_flags |= TF_GPUTINPROG;
17826 rack->r_ctl.rc_gp_cumack_ts = 0;
17827 rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
17828 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
17829 tp->gput_seq = startseq;
17830 rack->app_limited_needs_set = 0;
17831 if (rack->in_probe_rtt)
17832 rack->measure_saw_probe_rtt = 1;
17833 else if ((rack->measure_saw_probe_rtt) &&
17834 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
17835 rack->measure_saw_probe_rtt = 0;
17836 if (rack->rc_gp_filled)
17837 tp->gput_ts = rack->r_ctl.last_cumack_advance;
17839 /* Special case initial measurement */
17842 tp->gput_ts = tcp_get_usecs(&tv);
17843 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
17846 * We take a guess out into the future,
17847 * if we have no measurement and no
17848 * initial rate, we measure the first
17849 * initial-windows worth of data to
17850 * speed up getting some GP measurement and
17851 * thus start pacing.
17853 if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
17854 rack->app_limited_needs_set = 1;
17855 tp->gput_ack = startseq + max(rc_init_window(rack),
17856 (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
17857 rack_log_pacing_delay_calc(rack,
17862 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17864 __LINE__, NULL, 0);
17865 rack_tend_gp_marks(tp, rack);
17866 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
17871 * We are out somewhere in the sb
17872 * can we use the already outstanding data?
17875 if (rack->r_ctl.rc_app_limited_cnt == 0) {
17877 * Yes first one is good and in this case
17878 * the tp->gput_ts is correctly set based on
17879 * the last ack that arrived (no need to
17880 * set things up when an ack comes in).
17882 my_rsm = tqhash_min(rack->r_ctl.tqh);
17883 if ((my_rsm == NULL) ||
17884 (my_rsm->r_rtr_cnt != 1)) {
17885 /* retransmission? */
17889 if (rack->r_ctl.rc_first_appl == NULL) {
17891 * If rc_first_appl is NULL
17892 * then the cnt should be 0.
17893 * This is probably an error, maybe
17894 * a KASSERT would be approprate.
17899 * If we have a marker pointer to the last one that is
17900 * app limited we can use that, but we need to set
17901 * things up so that when it gets ack'ed we record
17902 * the ack time (if its not already acked).
17904 rack->app_limited_needs_set = 1;
17906 * We want to get to the rsm that is either
17907 * next with space i.e. over 1 MSS or the one
17908 * after that (after the app-limited).
17910 my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl);
17912 if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
17913 /* Have to use the next one */
17914 my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17916 /* Use after the first MSS of it is acked */
17917 tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
17921 if ((my_rsm == NULL) ||
17922 (my_rsm->r_rtr_cnt != 1)) {
17924 * Either its a retransmit or
17925 * the last is the app-limited one.
17930 tp->gput_seq = my_rsm->r_start;
17932 if (my_rsm->r_flags & RACK_ACKED) {
17934 * This one has been acked use the arrival ack time
17936 struct rack_sendmap *nrsm;
17938 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
17939 rack->app_limited_needs_set = 0;
17941 * Ok in this path we need to use the r_end now
17942 * since this guy is the starting ack.
17944 tp->gput_seq = my_rsm->r_end;
17946 * We also need to adjust up the sendtime
17947 * to the send of the next data after my_rsm.
17949 nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm);
17954 * The next as not been sent, thats the
17955 * case for using the latest.
17960 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
17961 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
17962 rack->r_ctl.rc_gp_cumack_ts = 0;
17963 rack_log_pacing_delay_calc(rack,
17968 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
17970 __LINE__, my_rsm, 0);
17971 /* Now lets make sure all are marked as they should be */
17972 rack_tend_gp_marks(tp, rack);
17973 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
17979 * We don't know how long we may have been
17980 * idle or if this is the first-send. Lets
17981 * setup the flag so we will trim off
17982 * the first ack'd data so we get a true
17985 rack->app_limited_needs_set = 1;
17986 tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
17987 rack->r_ctl.rc_gp_cumack_ts = 0;
17988 /* Find this guy so we can pull the send time */
17989 my_rsm = tqhash_find(rack->r_ctl.tqh, startseq);
17991 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0];
17992 if (my_rsm->r_flags & RACK_ACKED) {
17994 * Unlikely since its probably what was
17995 * just transmitted (but I am paranoid).
17997 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival;
17998 rack->app_limited_needs_set = 0;
18000 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
18001 /* This also is unlikely */
18002 tp->gput_seq = my_rsm->r_start;
18006 * TSNH unless we have some send-map limit,
18007 * and even at that it should not be hitting
18008 * that limit (we should have stopped sending).
18013 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv);
18015 rack_tend_gp_marks(tp, rack);
18016 rack_log_pacing_delay_calc(rack,
18021 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts),
18022 9, __LINE__, NULL, 0);
18023 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL);
18026 static inline uint32_t
18027 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cwnd_to_use,
18028 uint32_t avail, int32_t sb_offset)
18033 if (tp->snd_wnd > cwnd_to_use)
18034 sendwin = cwnd_to_use;
18036 sendwin = tp->snd_wnd;
18037 if (ctf_outstanding(tp) >= tp->snd_wnd) {
18038 /* We never want to go over our peers rcv-window */
18043 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
18044 if (flight >= sendwin) {
18046 * We have in flight what we are allowed by cwnd (if
18047 * it was rwnd blocking it would have hit above out
18052 len = sendwin - flight;
18053 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
18054 /* We would send too much (beyond the rwnd) */
18055 len = tp->snd_wnd - ctf_outstanding(tp);
18057 if ((len + sb_offset) > avail) {
18059 * We don't have that much in the SB, how much is
18062 len = avail - sb_offset;
18069 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags,
18070 unsigned ipoptlen, int32_t orig_len, int32_t len, int error,
18071 int rsm_is_null, int optlen, int line, uint16_t mode)
18073 if (tcp_bblogging_on(rack->rc_tp)) {
18074 union tcp_log_stackspecific log;
18077 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18078 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18079 log.u_bbr.flex1 = error;
18080 log.u_bbr.flex2 = flags;
18081 log.u_bbr.flex3 = rsm_is_null;
18082 log.u_bbr.flex4 = ipoptlen;
18083 log.u_bbr.flex5 = tp->rcv_numsacks;
18084 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18085 log.u_bbr.flex7 = optlen;
18086 log.u_bbr.flex8 = rack->r_fsb_inited;
18087 log.u_bbr.applimited = rack->r_fast_output;
18088 log.u_bbr.bw_inuse = rack_get_bw(rack);
18089 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18090 log.u_bbr.cwnd_gain = mode;
18091 log.u_bbr.pkts_out = orig_len;
18092 log.u_bbr.lt_epoch = len;
18093 log.u_bbr.delivered = line;
18094 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
18095 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18096 tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0,
18097 len, &log, false, NULL, __func__, __LINE__, &tv);
18102 static struct mbuf *
18103 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen,
18104 struct rack_fast_send_blk *fsb,
18105 int32_t seglimit, int32_t segsize, int hw_tls)
18108 struct ktls_session *tls, *ntls;
18110 struct mbuf *start;
18113 struct mbuf *m, *n, **np, *smb;
18116 int32_t len = *plen;
18118 int32_t len_cp = 0;
18119 uint32_t mlen, frags;
18121 soff = off = the_off;
18126 if (hw_tls && (m->m_flags & M_EXTPG))
18127 tls = m->m_epg_tls;
18141 if (m->m_flags & M_EXTPG)
18142 ntls = m->m_epg_tls;
18147 * Avoid mixing TLS records with handshake
18148 * data or TLS records from different
18158 mlen = min(len, m->m_len - off);
18161 * For M_EXTPG mbufs, add 3 segments
18162 * + 1 in case we are crossing page boundaries
18163 * + 2 in case the TLS hdr/trailer are used
18164 * It is cheaper to just add the segments
18165 * than it is to take the cache miss to look
18166 * at the mbuf ext_pgs state in detail.
18168 if (m->m_flags & M_EXTPG) {
18169 fragsize = min(segsize, PAGE_SIZE);
18172 fragsize = segsize;
18176 /* Break if we really can't fit anymore. */
18177 if ((frags + 1) >= seglimit) {
18183 * Reduce size if you can't copy the whole
18184 * mbuf. If we can't copy the whole mbuf, also
18185 * adjust len so the loop will end after this
18188 if ((frags + howmany(mlen, fragsize)) >= seglimit) {
18189 mlen = (seglimit - frags - 1) * fragsize;
18191 *plen = len_cp + len;
18193 frags += howmany(mlen, fragsize);
18197 KASSERT(seglimit > 0,
18198 ("%s: seglimit went too low", __func__));
18200 n = m_get(M_NOWAIT, m->m_type);
18206 len_cp += n->m_len;
18207 if (m->m_flags & (M_EXT|M_EXTPG)) {
18208 n->m_data = m->m_data + off;
18211 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
18218 if (len || (soff == smb->m_len)) {
18220 * We have more so we move forward or
18221 * we have consumed the entire mbuf and
18222 * len has fell to 0.
18234 * Save off the size of the mbuf. We do
18235 * this so that we can recognize when it
18236 * has been trimmed by sbcut() as acks
18239 fsb->o_m_len = smb->m_len;
18240 fsb->o_t_len = M_TRAILINGROOM(smb);
18243 * This is the case where the next mbuf went to NULL. This
18244 * means with this copy we have sent everything in the sb.
18245 * In theory we could clear the fast_output flag, but lets
18246 * not since its possible that we could get more added
18247 * and acks that call the extend function which would let
18263 * This is a copy of m_copym(), taking the TSO segment size/limit
18264 * constraints into account, and advancing the sndptr as it goes.
18266 static struct mbuf *
18267 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen,
18268 int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff)
18270 struct mbuf *m, *n;
18273 m = rack->r_ctl.fsb.m;
18274 if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) {
18276 * The trailing space changed, mbufs can grow
18277 * at the tail but they can't shrink from
18278 * it, KASSERT that. Adjust the orig_m_len to
18279 * compensate for this change.
18281 KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)),
18282 ("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n",
18285 (intmax_t)M_TRAILINGROOM(m),
18286 rack->r_ctl.fsb.o_t_len,
18287 rack->r_ctl.fsb.o_m_len,
18289 rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m));
18290 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m);
18292 if (m->m_len < rack->r_ctl.fsb.o_m_len) {
18294 * Mbuf shrank, trimmed off the top by an ack, our
18297 KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)),
18298 ("mbuf:%p len:%u rack:%p oml:%u soff:%u\n",
18300 rack, rack->r_ctl.fsb.o_m_len,
18301 rack->r_ctl.fsb.off));
18303 if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len))
18304 rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len);
18306 rack->r_ctl.fsb.off = 0;
18307 rack->r_ctl.fsb.o_m_len = m->m_len;
18309 } else if (m->m_len > rack->r_ctl.fsb.o_m_len) {
18310 panic("rack:%p m:%p m_len grew outside of t_space compensation",
18314 soff = rack->r_ctl.fsb.off;
18315 KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff));
18316 KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen));
18317 KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?",
18319 rack, *plen, m, m->m_len));
18320 /* Save off the right location before we copy and advance */
18322 *s_mb = rack->r_ctl.fsb.m;
18323 n = rack_fo_base_copym(m, soff, plen,
18325 seglimit, segsize, rack->r_ctl.fsb.hw_tls);
18329 /* Log the buffer level */
18331 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack,
18332 int len, struct timeval *tv,
18335 uint32_t p_rate = 0, p_queue = 0, err = 0;
18336 union tcp_log_stackspecific log;
18339 err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18340 err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
18342 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18343 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18344 log.u_bbr.flex1 = p_rate;
18345 log.u_bbr.flex2 = p_queue;
18346 log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18347 log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18348 log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18349 log.u_bbr.flex7 = 99;
18350 log.u_bbr.flex8 = 0;
18351 log.u_bbr.pkts_out = err;
18352 log.u_bbr.delRate = rack->r_ctl.crte->rate;
18353 log.u_bbr.timeStamp = cts;
18354 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18355 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18356 len, &log, false, NULL, __func__, __LINE__, tv);
18361 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp,
18362 struct timeval *tv, uint32_t cts, int len, uint32_t segsiz)
18364 uint64_t lentime = 0;
18366 uint32_t p_rate = 0, p_queue = 0, err;
18367 union tcp_log_stackspecific log;
18370 err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue);
18371 /* Failed or queue is zero */
18372 if (err || (p_queue == 0)) {
18376 err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate);
18382 * If we reach here we have some bytes in
18383 * the queue. The number returned is a value
18384 * between 0 and 0xffff where ffff is full
18385 * and 0 is empty. So how best to make this into
18386 * something usable?
18388 * The "safer" way is lets take the b/w gotten
18389 * from the query (which should be our b/w rate)
18390 * and pretend that a full send (our rc_pace_max_segs)
18391 * is outstanding. We factor it so its as if a full
18392 * number of our MSS segment is terms of full
18393 * ethernet segments are outstanding.
18397 lentime = (rack->r_ctl.rc_pace_max_segs / segsiz);
18398 lentime *= ETHERNET_SEGMENT_SIZE;
18399 lentime *= (uint64_t)HPTS_USEC_IN_SEC;
18402 /* TSNH -- KASSERT? */
18406 if (tcp_bblogging_on(tp)) {
18407 memset(&log, 0, sizeof(log));
18408 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18409 log.u_bbr.flex1 = p_rate;
18410 log.u_bbr.flex2 = p_queue;
18411 log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using;
18412 log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs;
18413 log.u_bbr.flex6 = rack->r_ctl.crte->time_between;
18414 log.u_bbr.flex7 = 99;
18415 log.u_bbr.flex8 = 0;
18416 log.u_bbr.pkts_out = err;
18417 log.u_bbr.delRate = rack->r_ctl.crte->rate;
18418 log.u_bbr.cur_del_rate = lentime;
18419 log.u_bbr.timeStamp = cts;
18420 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18421 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0,
18422 len, &log, false, NULL, __func__, __LINE__,tv);
18425 return ((uint32_t)lentime);
18429 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm,
18430 uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp)
18433 * Enter the fast retransmit path. We are given that a sched_pin is
18434 * in place (if accounting is compliled in) and the cycle count taken
18435 * at the entry is in the ts_val. The concept her is that the rsm
18436 * now holds the mbuf offsets and such so we can directly transmit
18437 * without a lot of overhead, the len field is already set for
18438 * us to prohibit us from sending too much (usually its 1MSS).
18440 struct ip *ip = NULL;
18441 struct udphdr *udp = NULL;
18442 struct tcphdr *th = NULL;
18443 struct mbuf *m = NULL;
18446 struct tcp_log_buffer *lgb;
18447 #ifdef TCP_ACCOUNTING
18452 u_char opt[TCP_MAXOLEN];
18453 uint32_t hdrlen, optlen;
18454 int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0;
18456 uint32_t if_hw_tsomaxsegcount = 0, startseq;
18457 uint32_t if_hw_tsomaxsegsize;
18458 int32_t ip_sendflag = IP_NO_SND_TAG_RL;
18461 struct ip6_hdr *ip6 = NULL;
18463 if (rack->r_is_v6) {
18464 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
18465 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
18469 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
18470 hdrlen = sizeof(struct tcpiphdr);
18472 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
18476 /* Its a TLP add the flag, it may already be there but be sure */
18477 rsm->r_flags |= RACK_TLP;
18479 /* If it was a TLP it is not not on this retransmit */
18480 rsm->r_flags &= ~RACK_TLP;
18482 startseq = rsm->r_start;
18483 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
18484 inp = rack->rc_inp;
18486 flags = tcp_outflags[tp->t_state];
18487 if (flags & (TH_SYN|TH_RST)) {
18490 if (rsm->r_flags & RACK_HAS_FIN) {
18491 /* We can't send a FIN here */
18494 if (flags & TH_FIN) {
18495 /* We never send a FIN */
18498 if (tp->t_flags & TF_RCVD_TSTMP) {
18499 to.to_tsval = ms_cts + tp->ts_offset;
18500 to.to_tsecr = tp->ts_recent;
18501 to.to_flags = TOF_TS;
18503 optlen = tcp_addoptions(&to, opt);
18505 udp = rack->r_ctl.fsb.udp;
18507 hdrlen += sizeof(struct udphdr);
18508 if (rack->r_ctl.rc_pace_max_segs)
18509 max_val = rack->r_ctl.rc_pace_max_segs;
18510 else if (rack->rc_user_set_max_segs)
18511 max_val = rack->rc_user_set_max_segs * segsiz;
18514 if ((tp->t_flags & TF_TSO) &&
18520 if (MHLEN < hdrlen + max_linkhdr)
18521 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
18524 m = m_gethdr(M_NOWAIT, MT_DATA);
18527 m->m_data += max_linkhdr;
18529 th = rack->r_ctl.fsb.th;
18530 /* Establish the len to send */
18533 if ((tso) && (len + optlen > segsiz)) {
18534 uint32_t if_hw_tsomax;
18537 /* extract TSO information */
18538 if_hw_tsomax = tp->t_tsomax;
18539 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
18540 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
18542 * Check if we should limit by maximum payload
18545 if (if_hw_tsomax != 0) {
18546 /* compute maximum TSO length */
18547 max_len = (if_hw_tsomax - hdrlen -
18549 if (max_len <= 0) {
18551 } else if (len > max_len) {
18555 if (len <= segsiz) {
18557 * In case there are too many small fragments don't
18565 if ((tso == 0) && (len > segsiz))
18567 (void)tcp_get_usecs(tv);
18569 (len <= MHLEN - hdrlen - max_linkhdr)) {
18572 th->th_seq = htonl(rsm->r_start);
18573 th->th_ack = htonl(tp->rcv_nxt);
18575 * The PUSH bit should only be applied
18576 * if the full retransmission is made. If
18577 * we are sending less than this is the
18578 * left hand edge and should not have
18581 if ((rsm->r_flags & RACK_HAD_PUSH) &&
18582 (len == (rsm->r_end - rsm->r_start)))
18584 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
18585 if (th->th_win == 0) {
18586 tp->t_sndzerowin++;
18587 tp->t_flags |= TF_RXWIN0SENT;
18589 tp->t_flags &= ~TF_RXWIN0SENT;
18590 if (rsm->r_flags & RACK_TLP) {
18592 * TLP should not count in retran count, but
18595 counter_u64_add(rack_tlp_retran, 1);
18596 counter_u64_add(rack_tlp_retran_bytes, len);
18598 tp->t_sndrexmitpack++;
18599 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
18600 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
18603 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
18606 if (rsm->m == NULL)
18609 ((rsm->orig_m_len != rsm->m->m_len) ||
18610 (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) {
18611 /* Fix up the orig_m_len and possibly the mbuf offset */
18612 rack_adjust_orig_mlen(rsm);
18614 m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls);
18615 if (len <= segsiz) {
18617 * Must have ran out of mbufs for the copy
18618 * shorten it to no longer need tso. Lets
18619 * not put on sendalot since we are low on
18624 if ((m->m_next == NULL) || (len <= 0)){
18629 ulen = hdrlen + len - sizeof(struct ip6_hdr);
18631 ulen = hdrlen + len - sizeof(struct ip);
18632 udp->uh_ulen = htons(ulen);
18634 m->m_pkthdr.rcvif = (struct ifnet *)0;
18635 if (TCPS_HAVERCVDSYN(tp->t_state) &&
18636 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
18637 int ect = tcp_ecn_output_established(tp, &flags, len, true);
18638 if ((tp->t_state == TCPS_SYN_RECEIVED) &&
18639 (tp->t_flags2 & TF2_ECN_SND_ECE))
18640 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
18642 if (rack->r_is_v6) {
18643 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
18644 ip6->ip6_flow |= htonl(ect << 20);
18649 ip->ip_tos &= ~IPTOS_ECN_MASK;
18653 if (rack->r_ctl.crte != NULL) {
18654 /* See if we can send via the hw queue */
18655 slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz);
18656 /* If there is nothing in queue (no pacing time) we can send via the hw queue */
18660 tcp_set_flags(th, flags);
18661 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
18663 if (rack->r_is_v6) {
18665 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
18666 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18667 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
18668 th->th_sum = htons(0);
18669 UDPSTAT_INC(udps_opackets);
18671 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
18672 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18673 th->th_sum = in6_cksum_pseudo(ip6,
18674 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
18679 #if defined(INET6) && defined(INET)
18685 m->m_pkthdr.csum_flags = CSUM_UDP;
18686 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
18687 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
18688 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
18689 th->th_sum = htons(0);
18690 UDPSTAT_INC(udps_opackets);
18692 m->m_pkthdr.csum_flags = CSUM_TCP;
18693 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
18694 th->th_sum = in_pseudo(ip->ip_src.s_addr,
18695 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
18696 IPPROTO_TCP + len + optlen));
18698 /* IP version must be set here for ipv4/ipv6 checking later */
18699 KASSERT(ip->ip_v == IPVERSION,
18700 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
18705 * Here we use segsiz since we have no added options besides
18706 * any standard timestamp options (no DSACKs or SACKS are sent
18707 * via either fast-path).
18709 KASSERT(len > segsiz,
18710 ("%s: len <= tso_segsz tp:%p", __func__, tp));
18711 m->m_pkthdr.csum_flags |= CSUM_TSO;
18712 m->m_pkthdr.tso_segsz = segsiz;
18715 if (rack->r_is_v6) {
18716 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
18717 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
18718 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
18719 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18721 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18724 #if defined(INET) && defined(INET6)
18729 ip->ip_len = htons(m->m_pkthdr.len);
18730 ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
18731 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
18732 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
18733 if (tp->t_port == 0 || len < V_tcp_minmss) {
18734 ip->ip_off |= htons(IP_DF);
18737 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
18741 if (doing_tlp == 0) {
18742 /* Set we retransmitted */
18743 rack->rc_gp_saw_rec = 1;
18745 /* Its a TLP set ca or ss */
18746 if (tp->snd_cwnd > tp->snd_ssthresh) {
18747 /* Set we sent in CA */
18748 rack->rc_gp_saw_ca = 1;
18750 /* Set we sent in SS */
18751 rack->rc_gp_saw_ss = 1;
18754 /* Time to copy in our header */
18755 cpto = mtod(m, uint8_t *);
18756 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
18757 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
18759 bcopy(opt, th + 1, optlen);
18760 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
18762 th->th_off = sizeof(struct tcphdr) >> 2;
18764 if (tcp_bblogging_on(rack->rc_tp)) {
18765 union tcp_log_stackspecific log;
18767 if (rsm->r_flags & RACK_RWND_COLLAPSED) {
18768 rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
18769 counter_u64_add(rack_collapsed_win_rxt, 1);
18770 counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
18772 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
18773 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
18774 if (rack->rack_no_prr)
18775 log.u_bbr.flex1 = 0;
18777 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
18778 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
18779 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
18780 log.u_bbr.flex4 = max_val;
18781 /* Save off the early/late values */
18782 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
18783 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
18784 log.u_bbr.bw_inuse = rack_get_bw(rack);
18785 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
18786 if (doing_tlp == 0)
18787 log.u_bbr.flex8 = 1;
18789 log.u_bbr.flex8 = 2;
18790 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
18791 log.u_bbr.flex7 = 55;
18792 log.u_bbr.pkts_out = tp->t_maxseg;
18793 log.u_bbr.timeStamp = cts;
18794 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
18795 if (rsm && (rsm->r_rtr_cnt > 0)) {
18797 * When we have a retransmit we want to log the
18798 * burst at send and flight at send from before.
18800 log.u_bbr.flex5 = rsm->r_fas;
18801 log.u_bbr.bbr_substate = rsm->r_bas;
18804 * This is currently unlikely until we do the
18805 * packet pair probes but I will add it for completeness.
18807 log.u_bbr.flex5 = log.u_bbr.inflight;
18808 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
18810 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
18811 log.u_bbr.delivered = 0;
18812 log.u_bbr.rttProp = (uint64_t)rsm;
18813 log.u_bbr.delRate = rsm->r_flags;
18814 log.u_bbr.delRate <<= 31;
18815 log.u_bbr.delRate |= rack->r_must_retran;
18816 log.u_bbr.delRate <<= 1;
18817 log.u_bbr.delRate |= 1;
18818 log.u_bbr.pkt_epoch = __LINE__;
18819 lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
18820 len, &log, false, NULL, __func__, __LINE__, tv);
18823 if ((rack->r_ctl.crte != NULL) &&
18824 tcp_bblogging_on(tp)) {
18825 rack_log_queue_level(tp, rack, len, tv, cts);
18828 if (rack->r_is_v6) {
18829 error = ip6_output(m, inp->in6p_outputopts,
18831 ip_sendflag, NULL, NULL, inp);
18837 error = ip_output(m, NULL,
18839 ip_sendflag, 0, inp);
18844 lgb->tlb_errno = error;
18849 } else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) {
18850 rack->rc_hw_nobuf = 0;
18851 rack->r_ctl.rc_agg_delayed = 0;
18854 rack->r_ctl.rc_agg_early = 0;
18857 rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv),
18858 rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz);
18860 rack->rc_tlp_in_progress = 1;
18861 rack->r_ctl.rc_tlp_cnt_out++;
18864 counter_u64_add(rack_total_bytes, len);
18865 tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls);
18867 rack->rc_last_sent_tlp_past_cumack = 0;
18868 rack->rc_last_sent_tlp_seq_valid = 1;
18869 rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
18870 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
18873 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
18874 rack->forced_ack = 0; /* If we send something zap the FA flag */
18875 if (IN_FASTRECOVERY(tp->t_flags) && rsm)
18876 rack->r_ctl.retran_during_recovery += len;
18880 idx = (len / segsiz) + 3;
18881 if (idx >= TCP_MSS_ACCT_ATIMER)
18882 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
18884 counter_u64_add(rack_out_size[idx], 1);
18886 if (tp->t_rtttime == 0) {
18887 tp->t_rtttime = ticks;
18888 tp->t_rtseq = startseq;
18889 KMOD_TCPSTAT_INC(tcps_segstimed);
18891 counter_u64_add(rack_fto_rsm_send, 1);
18892 if (error && (error == ENOBUFS)) {
18893 if (rack->r_ctl.crte != NULL) {
18894 tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
18895 if (tcp_bblogging_on(rack->rc_tp))
18896 rack_log_queue_level(tp, rack, len, tv, cts);
18898 tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
18899 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
18900 if (rack->rc_enobuf < 0x7f)
18902 if (slot < (10 * HPTS_USEC_IN_MSEC))
18903 slot = 10 * HPTS_USEC_IN_MSEC;
18904 if (rack->r_ctl.crte != NULL) {
18905 counter_u64_add(rack_saw_enobuf_hw, 1);
18906 tcp_rl_log_enobuf(rack->r_ctl.crte);
18908 counter_u64_add(rack_saw_enobuf, 1);
18910 slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz);
18912 (rack->rc_always_pace == 0) ||
18913 (rack->r_rr_config == 1)) {
18915 * We have no pacing set or we
18916 * are using old-style rack or
18917 * we are overridden to use the old 1ms pacing.
18919 slot = rack->r_ctl.rc_min_to;
18921 rack_start_hpts_timer(rack, tp, cts, slot, len, 0);
18922 #ifdef TCP_ACCOUNTING
18923 crtsc = get_cyclecount();
18924 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18925 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
18927 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18928 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
18930 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
18931 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz);
18943 rack_sndbuf_autoscale(struct tcp_rack *rack)
18946 * Automatic sizing of send socket buffer. Often the send buffer
18947 * size is not optimally adjusted to the actual network conditions
18948 * at hand (delay bandwidth product). Setting the buffer size too
18949 * small limits throughput on links with high bandwidth and high
18950 * delay (eg. trans-continental/oceanic links). Setting the
18951 * buffer size too big consumes too much real kernel memory,
18952 * especially with many connections on busy servers.
18954 * The criteria to step up the send buffer one notch are:
18955 * 1. receive window of remote host is larger than send buffer
18956 * (with a fudge factor of 5/4th);
18957 * 2. send buffer is filled to 7/8th with data (so we actually
18958 * have data to make use of it);
18959 * 3. send buffer fill has not hit maximal automatic size;
18960 * 4. our send window (slow start and cogestion controlled) is
18961 * larger than sent but unacknowledged data in send buffer.
18963 * Note that the rack version moves things much faster since
18964 * we want to avoid hitting cache lines in the rack_fast_output()
18965 * path so this is called much less often and thus moves
18966 * the SB forward by a percentage.
18970 uint32_t sendwin, scaleup;
18973 so = rack->rc_inp->inp_socket;
18974 sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd);
18975 if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
18976 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
18977 sbused(&so->so_snd) >=
18978 (so->so_snd.sb_hiwat / 8 * 7) &&
18979 sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
18980 sendwin >= (sbused(&so->so_snd) -
18981 (tp->snd_nxt - tp->snd_una))) {
18982 if (rack_autosndbuf_inc)
18983 scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100;
18985 scaleup = V_tcp_autosndbuf_inc;
18986 if (scaleup < V_tcp_autosndbuf_inc)
18987 scaleup = V_tcp_autosndbuf_inc;
18988 scaleup += so->so_snd.sb_hiwat;
18989 if (scaleup > V_tcp_autosndbuf_max)
18990 scaleup = V_tcp_autosndbuf_max;
18991 if (!sbreserve_locked(so, SO_SND, scaleup, curthread))
18992 so->so_snd.sb_flags &= ~SB_AUTOSIZE;
18998 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val,
18999 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err)
19002 * Enter to do fast output. We are given that the sched_pin is
19003 * in place (if accounting is compiled in) and the cycle count taken
19004 * at entry is in place in ts_val. The idea here is that
19005 * we know how many more bytes needs to be sent (presumably either
19006 * during pacing or to fill the cwnd and that was greater than
19007 * the max-burst). We have how much to send and all the info we
19008 * need to just send.
19011 struct ip *ip = NULL;
19013 struct udphdr *udp = NULL;
19014 struct tcphdr *th = NULL;
19015 struct mbuf *m, *s_mb;
19018 struct tcp_log_buffer *lgb;
19019 #ifdef TCP_ACCOUNTING
19023 u_char opt[TCP_MAXOLEN];
19024 uint32_t hdrlen, optlen;
19025 #ifdef TCP_ACCOUNTING
19028 int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0;
19031 uint32_t if_hw_tsomaxsegcount = 0, startseq;
19032 uint32_t if_hw_tsomaxsegsize;
19033 uint16_t add_flag = RACK_SENT_FP;
19035 struct ip6_hdr *ip6 = NULL;
19037 if (rack->r_is_v6) {
19038 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
19039 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
19044 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
19045 hdrlen = sizeof(struct tcpiphdr);
19048 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) {
19052 startseq = tp->snd_max;
19053 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
19054 inp = rack->rc_inp;
19055 len = rack->r_ctl.fsb.left_to_send;
19057 flags = rack->r_ctl.fsb.tcp_flags;
19058 if (tp->t_flags & TF_RCVD_TSTMP) {
19059 to.to_tsval = ms_cts + tp->ts_offset;
19060 to.to_tsecr = tp->ts_recent;
19061 to.to_flags = TOF_TS;
19063 optlen = tcp_addoptions(&to, opt);
19065 udp = rack->r_ctl.fsb.udp;
19067 hdrlen += sizeof(struct udphdr);
19068 if (rack->r_ctl.rc_pace_max_segs)
19069 max_val = rack->r_ctl.rc_pace_max_segs;
19070 else if (rack->rc_user_set_max_segs)
19071 max_val = rack->rc_user_set_max_segs * segsiz;
19074 if ((tp->t_flags & TF_TSO) &&
19081 if (MHLEN < hdrlen + max_linkhdr)
19082 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
19085 m = m_gethdr(M_NOWAIT, MT_DATA);
19088 m->m_data += max_linkhdr;
19090 th = rack->r_ctl.fsb.th;
19091 /* Establish the len to send */
19094 if ((tso) && (len + optlen > segsiz)) {
19095 uint32_t if_hw_tsomax;
19098 /* extract TSO information */
19099 if_hw_tsomax = tp->t_tsomax;
19100 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
19101 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
19103 * Check if we should limit by maximum payload
19106 if (if_hw_tsomax != 0) {
19107 /* compute maximum TSO length */
19108 max_len = (if_hw_tsomax - hdrlen -
19110 if (max_len <= 0) {
19112 } else if (len > max_len) {
19116 if (len <= segsiz) {
19118 * In case there are too many small fragments don't
19126 if ((tso == 0) && (len > segsiz))
19128 (void)tcp_get_usecs(tv);
19130 (len <= MHLEN - hdrlen - max_linkhdr)) {
19133 sb_offset = tp->snd_max - tp->snd_una;
19134 th->th_seq = htonl(tp->snd_max);
19135 th->th_ack = htonl(tp->rcv_nxt);
19136 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale));
19137 if (th->th_win == 0) {
19138 tp->t_sndzerowin++;
19139 tp->t_flags |= TF_RXWIN0SENT;
19141 tp->t_flags &= ~TF_RXWIN0SENT;
19142 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */
19143 KMOD_TCPSTAT_INC(tcps_sndpack);
19144 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
19146 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
19149 if (rack->r_ctl.fsb.m == NULL)
19152 /* s_mb and s_soff are saved for rack_log_output */
19153 m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize,
19155 if (len <= segsiz) {
19157 * Must have ran out of mbufs for the copy
19158 * shorten it to no longer need tso. Lets
19159 * not put on sendalot since we are low on
19164 if (rack->r_ctl.fsb.rfo_apply_push &&
19165 (len == rack->r_ctl.fsb.left_to_send)) {
19166 tcp_set_flags(th, flags | TH_PUSH);
19167 add_flag |= RACK_HAD_PUSH;
19169 if ((m->m_next == NULL) || (len <= 0)){
19174 ulen = hdrlen + len - sizeof(struct ip6_hdr);
19176 ulen = hdrlen + len - sizeof(struct ip);
19177 udp->uh_ulen = htons(ulen);
19179 m->m_pkthdr.rcvif = (struct ifnet *)0;
19180 if (TCPS_HAVERCVDSYN(tp->t_state) &&
19181 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
19182 int ect = tcp_ecn_output_established(tp, &flags, len, false);
19183 if ((tp->t_state == TCPS_SYN_RECEIVED) &&
19184 (tp->t_flags2 & TF2_ECN_SND_ECE))
19185 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
19187 if (rack->r_is_v6) {
19188 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
19189 ip6->ip6_flow |= htonl(ect << 20);
19195 ip->ip_tos &= ~IPTOS_ECN_MASK;
19200 tcp_set_flags(th, flags);
19201 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
19203 if (rack->r_is_v6) {
19205 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
19206 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19207 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
19208 th->th_sum = htons(0);
19209 UDPSTAT_INC(udps_opackets);
19211 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
19212 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19213 th->th_sum = in6_cksum_pseudo(ip6,
19214 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
19219 #if defined(INET6) && defined(INET)
19225 m->m_pkthdr.csum_flags = CSUM_UDP;
19226 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
19227 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
19228 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
19229 th->th_sum = htons(0);
19230 UDPSTAT_INC(udps_opackets);
19232 m->m_pkthdr.csum_flags = CSUM_TCP;
19233 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
19234 th->th_sum = in_pseudo(ip->ip_src.s_addr,
19235 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
19236 IPPROTO_TCP + len + optlen));
19238 /* IP version must be set here for ipv4/ipv6 checking later */
19239 KASSERT(ip->ip_v == IPVERSION,
19240 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
19245 * Here we use segsiz since we have no added options besides
19246 * any standard timestamp options (no DSACKs or SACKS are sent
19247 * via either fast-path).
19249 KASSERT(len > segsiz,
19250 ("%s: len <= tso_segsz tp:%p", __func__, tp));
19251 m->m_pkthdr.csum_flags |= CSUM_TSO;
19252 m->m_pkthdr.tso_segsz = segsiz;
19255 if (rack->r_is_v6) {
19256 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit;
19257 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
19258 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
19259 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19261 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19264 #if defined(INET) && defined(INET6)
19269 ip->ip_len = htons(m->m_pkthdr.len);
19270 ip->ip_ttl = rack->r_ctl.fsb.hoplimit;
19271 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
19272 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
19273 if (tp->t_port == 0 || len < V_tcp_minmss) {
19274 ip->ip_off |= htons(IP_DF);
19277 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
19281 if (tp->snd_cwnd > tp->snd_ssthresh) {
19282 /* Set we sent in CA */
19283 rack->rc_gp_saw_ca = 1;
19285 /* Set we sent in SS */
19286 rack->rc_gp_saw_ss = 1;
19288 /* Time to copy in our header */
19289 cpto = mtod(m, uint8_t *);
19290 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
19291 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
19293 bcopy(opt, th + 1, optlen);
19294 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
19296 th->th_off = sizeof(struct tcphdr) >> 2;
19298 if ((rack->r_ctl.crte != NULL) &&
19299 tcp_bblogging_on(tp)) {
19300 rack_log_queue_level(tp, rack, len, tv, cts);
19302 if (tcp_bblogging_on(rack->rc_tp)) {
19303 union tcp_log_stackspecific log;
19305 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
19306 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
19307 if (rack->rack_no_prr)
19308 log.u_bbr.flex1 = 0;
19310 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
19311 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
19312 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
19313 log.u_bbr.flex4 = max_val;
19314 /* Save off the early/late values */
19315 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
19316 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
19317 log.u_bbr.bw_inuse = rack_get_bw(rack);
19318 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
19319 log.u_bbr.flex8 = 0;
19320 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL);
19321 log.u_bbr.flex7 = 44;
19322 log.u_bbr.pkts_out = tp->t_maxseg;
19323 log.u_bbr.timeStamp = cts;
19324 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
19325 log.u_bbr.flex5 = log.u_bbr.inflight;
19326 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use;
19327 log.u_bbr.delivered = 0;
19328 log.u_bbr.rttProp = 0;
19329 log.u_bbr.delRate = rack->r_must_retran;
19330 log.u_bbr.delRate <<= 1;
19331 log.u_bbr.pkt_epoch = __LINE__;
19332 /* For fast output no retrans so just inflight and how many mss we send */
19333 log.u_bbr.flex5 = log.u_bbr.inflight;
19334 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
19335 lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK,
19336 len, &log, false, NULL, __func__, __LINE__, tv);
19340 if (rack->r_is_v6) {
19341 error = ip6_output(m, inp->in6p_outputopts,
19343 0, NULL, NULL, inp);
19346 #if defined(INET) && defined(INET6)
19351 error = ip_output(m, NULL,
19357 lgb->tlb_errno = error;
19364 } else if (rack->rc_hw_nobuf) {
19365 rack->rc_hw_nobuf = 0;
19366 rack->r_ctl.rc_agg_delayed = 0;
19369 rack->r_ctl.rc_agg_early = 0;
19371 if ((error == 0) && (rack->lt_bw_up == 0)) {
19373 rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv);
19374 rack->r_ctl.lt_seq = tp->snd_una;
19375 rack->lt_bw_up = 1;
19377 rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv),
19378 NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz);
19380 if (tp->snd_una == tp->snd_max) {
19381 rack->r_ctl.rc_tlp_rxt_last_time = cts;
19382 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
19383 tp->t_acktime = ticks;
19385 counter_u64_add(rack_total_bytes, len);
19386 tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls);
19388 rack->forced_ack = 0; /* If we send something zap the FA flag */
19390 if ((tp->t_flags & TF_GPUTINPROG) == 0)
19391 rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset);
19392 tp->snd_max += len;
19393 tp->snd_nxt = tp->snd_max;
19394 if (rack->rc_new_rnd_needed) {
19396 * Update the rnd to start ticking not
19397 * that from a time perspective all of
19398 * the preceding idle time is "in the round"
19400 rack->rc_new_rnd_needed = 0;
19401 rack->r_ctl.roundends = tp->snd_max;
19406 idx = (len / segsiz) + 3;
19407 if (idx >= TCP_MSS_ACCT_ATIMER)
19408 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
19410 counter_u64_add(rack_out_size[idx], 1);
19412 if (len <= rack->r_ctl.fsb.left_to_send)
19413 rack->r_ctl.fsb.left_to_send -= len;
19415 rack->r_ctl.fsb.left_to_send = 0;
19416 if (rack->r_ctl.fsb.left_to_send < segsiz) {
19417 rack->r_fast_output = 0;
19418 rack->r_ctl.fsb.left_to_send = 0;
19419 /* At the end of fast_output scale up the sb */
19420 SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd);
19421 rack_sndbuf_autoscale(rack);
19422 SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd);
19424 if (tp->t_rtttime == 0) {
19425 tp->t_rtttime = ticks;
19426 tp->t_rtseq = startseq;
19427 KMOD_TCPSTAT_INC(tcps_segstimed);
19429 if ((rack->r_ctl.fsb.left_to_send >= segsiz) &&
19434 th = rack->r_ctl.fsb.th;
19435 #ifdef TCP_ACCOUNTING
19440 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
19441 counter_u64_add(rack_fto_send, 1);
19442 slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz);
19443 rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0);
19444 #ifdef TCP_ACCOUNTING
19445 crtsc = get_cyclecount();
19446 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19447 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru;
19449 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19450 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
19452 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19453 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz);
19461 rack->r_fast_output = 0;
19466 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack,
19467 struct sockbuf *sb,
19468 int len, int orig_len, int segsiz, uint32_t pace_max_seg,
19472 rack->r_fast_output = 1;
19473 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
19474 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
19475 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
19476 rack->r_ctl.fsb.tcp_flags = flags;
19477 rack->r_ctl.fsb.left_to_send = orig_len - len;
19478 if (rack->r_ctl.fsb.left_to_send < pace_max_seg) {
19479 /* Less than a full sized pace, lets not */
19480 rack->r_fast_output = 0;
19483 /* Round down to the nearest pace_max_seg */
19484 rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg);
19487 rack->r_ctl.fsb.hw_tls = 1;
19489 rack->r_ctl.fsb.hw_tls = 0;
19490 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))),
19491 ("rack:%p left_to_send:%u sbavail:%u out:%u",
19492 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb),
19493 (tp->snd_max - tp->snd_una)));
19494 if (rack->r_ctl.fsb.left_to_send < segsiz)
19495 rack->r_fast_output = 0;
19497 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una)))
19498 rack->r_ctl.fsb.rfo_apply_push = 1;
19500 rack->r_ctl.fsb.rfo_apply_push = 0;
19505 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz)
19510 min_time = (uint64_t)get_hpts_min_sleep_time();
19511 maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC);
19512 maxlen = roundup(maxlen, segsiz);
19516 static struct rack_sendmap *
19517 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts)
19519 struct rack_sendmap *rsm = NULL;
19523 rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point);
19524 if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) {
19525 /* Nothing, strange turn off validity */
19526 rack->r_collapse_point_valid = 0;
19529 /* Can we send it yet? */
19530 if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) {
19532 * Receiver window has not grown enough for
19533 * the segment to be put on the wire.
19537 if (rsm->r_flags & RACK_ACKED) {
19539 * It has been sacked, lets move to the
19540 * next one if possible.
19542 rack->r_ctl.last_collapse_point = rsm->r_end;
19544 if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
19545 rack->r_ctl.high_collapse_point)) {
19546 rack->r_collapse_point_valid = 0;
19551 /* Now has it been long enough ? */
19552 thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts);
19553 if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) {
19554 rack_log_collapse(rack, rsm->r_start,
19555 (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19556 thresh, __LINE__, 6, rsm->r_flags, rsm);
19559 /* Not enough time */
19560 rack_log_collapse(rack, rsm->r_start,
19561 (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])),
19562 thresh, __LINE__, 7, rsm->r_flags, rsm);
19567 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg)
19569 if ((rack->full_size_rxt == 0) &&
19570 (rack->shape_rxt_to_pacing_min == 0) &&
19571 (*len >= segsiz)) {
19573 } else if (rack->shape_rxt_to_pacing_min &&
19575 /* We use pacing min as shaping len req */
19578 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
19583 * The else is full_size_rxt is on so send it all
19584 * note we do need to check this for exceeding
19585 * our max segment size due to the fact that
19586 * we do sometimes merge chunks together i.e.
19587 * we cannot just assume that we will never have
19588 * a chunk greater than pace_max_seg
19590 if (*len > pace_max_seg)
19591 *len = pace_max_seg;
19596 rack_output(struct tcpcb *tp)
19600 uint32_t sb_offset, s_moff = 0;
19601 int32_t len, error = 0;
19603 struct mbuf *m, *s_mb = NULL;
19605 uint32_t if_hw_tsomaxsegcount = 0;
19606 uint32_t if_hw_tsomaxsegsize;
19607 int32_t segsiz, minseg;
19608 long tot_len_this_send = 0;
19610 struct ip *ip = NULL;
19612 struct udphdr *udp = NULL;
19613 struct tcp_rack *rack;
19617 uint8_t check_done = 0;
19618 uint8_t wanted_cookie = 0;
19619 u_char opt[TCP_MAXOLEN];
19620 unsigned ipoptlen, optlen, hdrlen, ulen=0;
19623 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
19624 unsigned ipsec_optlen = 0;
19627 int32_t idle, sendalot;
19628 int32_t sub_from_prr = 0;
19629 volatile int32_t sack_rxmit;
19630 struct rack_sendmap *rsm = NULL;
19634 int32_t sup_rack = 0;
19635 uint32_t cts, ms_cts, delayed, early;
19636 uint16_t add_flag = RACK_SENT_SP;
19637 /* The doing_tlp flag will be set by the actual rack_timeout_tlp() */
19638 uint8_t doing_tlp = 0;
19639 uint32_t cwnd_to_use, pace_max_seg;
19640 int32_t do_a_prefetch = 0;
19641 int32_t prefetch_rsm = 0;
19642 int32_t orig_len = 0;
19644 int32_t prefetch_so_done = 0;
19645 struct tcp_log_buffer *lgb;
19646 struct inpcb *inp = tptoinpcb(tp);
19647 struct sockbuf *sb;
19648 uint64_t ts_val = 0;
19649 #ifdef TCP_ACCOUNTING
19653 struct ip6_hdr *ip6 = NULL;
19656 bool hpts_calling, hw_tls = false;
19658 NET_EPOCH_ASSERT();
19659 INP_WLOCK_ASSERT(inp);
19661 /* setup and take the cache hits here */
19662 rack = (struct tcp_rack *)tp->t_fb_ptr;
19663 #ifdef TCP_ACCOUNTING
19665 ts_val = get_cyclecount();
19667 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS);
19668 tp->t_flags2 &= ~TF2_HPTS_CALLS;
19670 if (tp->t_flags & TF_TOE) {
19671 #ifdef TCP_ACCOUNTING
19674 return (tcp_offload_output(tp));
19677 if (rack->rack_deferred_inited == 0) {
19679 * If we are the connecting socket we will
19680 * hit rack_init() when no sequence numbers
19681 * are setup. This makes it so we must defer
19682 * some initialization. Call that now.
19684 rack_deferred_init(tp, rack);
19687 * For TFO connections in SYN_RECEIVED, only allow the initial
19688 * SYN|ACK and those sent by the retransmit timer.
19690 if (IS_FASTOPEN(tp->t_flags) &&
19691 (tp->t_state == TCPS_SYN_RECEIVED) &&
19692 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN|ACK sent */
19693 (rack->r_ctl.rc_resend == NULL)) { /* not a retransmit */
19694 #ifdef TCP_ACCOUNTING
19700 if (rack->r_state) {
19701 /* Use the cache line loaded if possible */
19702 isipv6 = rack->r_is_v6;
19704 isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0;
19708 cts = tcp_get_usecs(&tv);
19709 ms_cts = tcp_tv_to_mssectick(&tv);
19710 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
19711 tcp_in_hpts(rack->rc_tp)) {
19713 * We are on the hpts for some timer but not hptsi output.
19714 * Remove from the hpts unconditionally.
19716 rack_timer_cancel(tp, rack, cts, __LINE__);
19718 /* Are we pacing and late? */
19719 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19720 TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
19721 /* We are delayed */
19722 delayed = cts - rack->r_ctl.rc_last_output_to;
19726 /* Do the timers, which may override the pacer */
19727 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
19730 retval = rack_process_timers(tp, rack, cts, hpts_calling,
19733 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
19734 #ifdef TCP_ACCOUNTING
19738 * If timers want tcp_drop(), then pass error out,
19739 * otherwise suppress it.
19741 return (retval < 0 ? retval : 0);
19744 if (rack->rc_in_persist) {
19745 if (tcp_in_hpts(rack->rc_tp) == 0) {
19746 /* Timer is not running */
19747 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19749 #ifdef TCP_ACCOUNTING
19754 if ((rack->rc_ack_required == 1) &&
19755 (rack->r_timer_override == 0)){
19756 /* A timeout occurred and no ack has arrived */
19757 if (tcp_in_hpts(rack->rc_tp) == 0) {
19758 /* Timer is not running */
19759 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
19761 #ifdef TCP_ACCOUNTING
19766 if ((rack->r_timer_override) ||
19767 (rack->rc_ack_can_sendout_data) ||
19769 (tp->t_state < TCPS_ESTABLISHED)) {
19770 rack->rc_ack_can_sendout_data = 0;
19771 if (tcp_in_hpts(rack->rc_tp))
19772 tcp_hpts_remove(rack->rc_tp);
19773 } else if (tcp_in_hpts(rack->rc_tp)) {
19775 * On the hpts you can't pass even if ACKNOW is on, we will
19776 * when the hpts fires.
19778 #ifdef TCP_ACCOUNTING
19779 crtsc = get_cyclecount();
19780 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19781 tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val);
19783 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
19784 tp->tcp_cnt_counters[SND_BLOCKED]++;
19788 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
19791 /* Finish out both pacing early and late accounting */
19792 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
19793 TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) {
19794 early = rack->r_ctl.rc_last_output_to - cts;
19798 rack->r_ctl.rc_agg_delayed += delayed;
19800 } else if (early) {
19801 rack->r_ctl.rc_agg_early += early;
19804 /* Now that early/late accounting is done turn off the flag */
19805 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
19806 rack->r_wanted_output = 0;
19807 rack->r_timer_override = 0;
19808 if ((tp->t_state != rack->r_state) &&
19809 TCPS_HAVEESTABLISHED(tp->t_state)) {
19810 rack_set_state(tp, rack);
19812 if ((rack->r_fast_output) &&
19813 (doing_tlp == 0) &&
19814 (tp->rcv_numsacks == 0)) {
19818 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
19822 inp = rack->rc_inp;
19823 so = inp->inp_socket;
19828 inp = rack->rc_inp;
19830 * For TFO connections in SYN_SENT or SYN_RECEIVED,
19831 * only allow the initial SYN or SYN|ACK and those sent
19832 * by the retransmit timer.
19834 if (IS_FASTOPEN(tp->t_flags) &&
19835 ((tp->t_state == TCPS_SYN_RECEIVED) ||
19836 (tp->t_state == TCPS_SYN_SENT)) &&
19837 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
19838 (tp->t_rxtshift == 0)) { /* not a retransmit */
19839 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
19840 so = inp->inp_socket;
19842 goto just_return_nolock;
19845 * Determine length of data that should be transmitted, and flags
19846 * that will be used. If there is some data or critical controls
19847 * (SYN, RST) to send, then transmit; otherwise, investigate
19850 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
19851 if (tp->t_idle_reduce) {
19852 if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur))
19853 rack_cc_after_idle(rack, tp);
19855 tp->t_flags &= ~TF_LASTIDLE;
19857 if (tp->t_flags & TF_MORETOCOME) {
19858 tp->t_flags |= TF_LASTIDLE;
19862 if ((tp->snd_una == tp->snd_max) &&
19863 rack->r_ctl.rc_went_idle_time &&
19864 TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) {
19865 idle = cts - rack->r_ctl.rc_went_idle_time;
19866 if (idle > rack_min_probertt_hold) {
19867 /* Count as a probe rtt */
19868 if (rack->in_probe_rtt == 0) {
19869 rack->r_ctl.rc_lower_rtt_us_cts = cts;
19870 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
19871 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
19872 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
19874 rack_exit_probertt(rack, cts);
19879 if (rack_use_fsb &&
19880 (rack->r_ctl.fsb.tcp_ip_hdr) &&
19881 (rack->r_fsb_inited == 0) &&
19882 (rack->r_state != TCPS_CLOSED))
19883 rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]);
19886 * If we've recently taken a timeout, snd_max will be greater than
19887 * snd_nxt. There may be SACK information that allows us to avoid
19888 * resending already delivered data. Adjust snd_nxt accordingly.
19891 cts = tcp_get_usecs(&tv);
19892 ms_cts = tcp_tv_to_mssectick(&tv);
19895 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
19897 if (rack->r_ctl.rc_pace_max_segs == 0)
19898 pace_max_seg = rack->rc_user_set_max_segs * segsiz;
19900 pace_max_seg = rack->r_ctl.rc_pace_max_segs;
19901 sb_offset = tp->snd_max - tp->snd_una;
19902 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
19903 flags = tcp_outflags[tp->t_state];
19904 while (rack->rc_free_cnt < rack_free_cache) {
19905 rsm = rack_alloc(rack);
19908 /* Retry in a ms */
19909 slot = (1 * HPTS_USEC_IN_MSEC);
19910 so = inp->inp_socket;
19912 goto just_return_nolock;
19914 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
19915 rack->rc_free_cnt++;
19921 if (flags & TH_RST) {
19922 SOCKBUF_LOCK(&inp->inp_socket->so_snd);
19923 so = inp->inp_socket;
19927 if (rack->r_ctl.rc_resend) {
19928 /* Retransmit timer */
19929 rsm = rack->r_ctl.rc_resend;
19930 rack->r_ctl.rc_resend = NULL;
19931 len = rsm->r_end - rsm->r_start;
19934 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
19935 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
19936 __func__, __LINE__,
19937 rsm->r_start, tp->snd_una, tp, rack, rsm));
19938 sb_offset = rsm->r_start - tp->snd_una;
19939 rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
19940 } else if (rack->r_collapse_point_valid &&
19941 ((rsm = rack_check_collapsed(rack, cts)) != NULL)) {
19943 * If an RSM is returned then enough time has passed
19944 * for us to retransmit it. Move up the collapse point,
19945 * since this rsm has its chance to retransmit now.
19947 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT);
19948 rack->r_ctl.last_collapse_point = rsm->r_end;
19950 if (SEQ_GEQ(rack->r_ctl.last_collapse_point,
19951 rack->r_ctl.high_collapse_point))
19952 rack->r_collapse_point_valid = 0;
19954 /* We are not doing a TLP */
19956 len = rsm->r_end - rsm->r_start;
19957 sb_offset = rsm->r_start - tp->snd_una;
19959 rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
19960 } else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) {
19961 /* We have a retransmit that takes precedence */
19962 if ((!IN_FASTRECOVERY(tp->t_flags)) &&
19963 ((rsm->r_flags & RACK_MUST_RXT) == 0) &&
19964 ((tp->t_flags & TF_WASFRECOVERY) == 0)) {
19965 /* Enter recovery if not induced by a time-out */
19966 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__);
19969 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
19970 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
19971 tp, rack, rsm, rsm->r_start, tp->snd_una);
19974 len = rsm->r_end - rsm->r_start;
19975 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
19976 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
19977 __func__, __LINE__,
19978 rsm->r_start, tp->snd_una, tp, rack, rsm));
19979 sb_offset = rsm->r_start - tp->snd_una;
19981 rack_validate_sizes(rack, &len, segsiz, pace_max_seg);
19984 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
19985 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
19988 } else if (rack->r_ctl.rc_tlpsend) {
19989 /* Tail loss probe */
19994 * Check if we can do a TLP with a RACK'd packet
19995 * this can happen if we are not doing the rack
19996 * cheat and we skipped to a TLP and it
19999 rsm = rack->r_ctl.rc_tlpsend;
20000 /* We are doing a TLP make sure the flag is preent */
20001 rsm->r_flags |= RACK_TLP;
20002 rack->r_ctl.rc_tlpsend = NULL;
20004 tlen = rsm->r_end - rsm->r_start;
20007 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
20008 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
20009 __func__, __LINE__,
20010 rsm->r_start, tp->snd_una, tp, rack, rsm));
20011 sb_offset = rsm->r_start - tp->snd_una;
20012 cwin = min(tp->snd_wnd, tlen);
20015 if (rack->r_must_retran &&
20016 (doing_tlp == 0) &&
20017 (SEQ_GT(tp->snd_max, tp->snd_una)) &&
20020 * There are two different ways that we
20021 * can get into this block:
20022 * a) This is a non-sack connection, we had a time-out
20023 * and thus r_must_retran was set and everything
20024 * left outstanding as been marked for retransmit.
20025 * b) The MTU of the path shrank, so that everything
20026 * was marked to be retransmitted with the smaller
20027 * mtu and r_must_retran was set.
20029 * This means that we expect the sendmap (outstanding)
20030 * to all be marked must. We can use the tmap to
20034 int sendwin, flight;
20036 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
20037 flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto);
20038 if (flight >= sendwin) {
20040 * We can't send yet.
20042 so = inp->inp_socket;
20044 goto just_return_nolock;
20047 * This is the case a/b mentioned above. All
20048 * outstanding/not-acked should be marked.
20049 * We can use the tmap to find them.
20051 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
20054 rack->r_must_retran = 0;
20055 rack->r_ctl.rc_out_at_rto = 0;
20056 so = inp->inp_socket;
20058 goto just_return_nolock;
20060 if ((rsm->r_flags & RACK_MUST_RXT) == 0) {
20062 * The first one does not have the flag, did we collapse
20063 * further up in our list?
20065 rack->r_must_retran = 0;
20066 rack->r_ctl.rc_out_at_rto = 0;
20071 len = rsm->r_end - rsm->r_start;
20072 sb_offset = rsm->r_start - tp->snd_una;
20074 if ((rack->full_size_rxt == 0) &&
20075 (rack->shape_rxt_to_pacing_min == 0) &&
20078 else if (rack->shape_rxt_to_pacing_min &&
20080 /* We use pacing min as shaping len req */
20083 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20088 * Delay removing the flag RACK_MUST_RXT so
20089 * that the fastpath for retransmit will
20090 * work with this rsm.
20095 * Enforce a connection sendmap count limit if set
20096 * as long as we are not retransmiting.
20098 if ((rsm == NULL) &&
20099 (rack->do_detection == 0) &&
20100 (V_tcp_map_entries_limit > 0) &&
20101 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
20102 counter_u64_add(rack_to_alloc_limited, 1);
20103 if (!rack->alloc_limit_reported) {
20104 rack->alloc_limit_reported = 1;
20105 counter_u64_add(rack_alloc_limited_conns, 1);
20107 so = inp->inp_socket;
20109 goto just_return_nolock;
20111 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
20112 /* we are retransmitting the fin */
20116 * When retransmitting data do *not* include the
20117 * FIN. This could happen from a TLP probe.
20122 if (rsm && rack->r_fsb_inited &&
20123 rack_use_rsm_rfo &&
20124 ((rsm->r_flags & RACK_HAS_FIN) == 0)) {
20127 ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp);
20131 so = inp->inp_socket;
20133 if (do_a_prefetch == 0) {
20134 kern_prefetch(sb, &do_a_prefetch);
20137 #ifdef NETFLIX_SHARED_CWND
20138 if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
20139 rack->rack_enable_scwnd) {
20140 /* We are doing cwnd sharing */
20141 if (rack->gp_ready &&
20142 (rack->rack_attempted_scwnd == 0) &&
20143 (rack->r_ctl.rc_scw == NULL) &&
20145 /* The pcbid is in, lets make an attempt */
20146 counter_u64_add(rack_try_scwnd, 1);
20147 rack->rack_attempted_scwnd = 1;
20148 rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
20149 &rack->r_ctl.rc_scw_index,
20152 if (rack->r_ctl.rc_scw &&
20153 (rack->rack_scwnd_is_idle == 1) &&
20154 sbavail(&so->so_snd)) {
20155 /* we are no longer out of data */
20156 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
20157 rack->rack_scwnd_is_idle = 0;
20159 if (rack->r_ctl.rc_scw) {
20160 /* First lets update and get the cwnd */
20161 rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
20162 rack->r_ctl.rc_scw_index,
20163 tp->snd_cwnd, tp->snd_wnd, segsiz);
20168 * Get standard flags, and add SYN or FIN if requested by 'hidden'
20171 if (tp->t_flags & TF_NEEDFIN)
20173 if (tp->t_flags & TF_NEEDSYN)
20175 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
20177 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
20179 kern_prefetch(end_rsm, &prefetch_rsm);
20184 * If snd_nxt == snd_max and we have transmitted a FIN, the
20185 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
20186 * negative length. This can also occur when TCP opens up its
20187 * congestion window while receiving additional duplicate acks after
20188 * fast-retransmit because TCP will reset snd_nxt to snd_max after
20189 * the fast-retransmit.
20191 * In the normal retransmit-FIN-only case, however, snd_nxt will be
20192 * set to snd_una, the sb_offset will be 0, and the length may wind
20195 * If sack_rxmit is true we are retransmitting from the scoreboard
20196 * in which case len is already set.
20198 if ((sack_rxmit == 0) &&
20199 (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
20202 avail = sbavail(sb);
20203 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
20204 sb_offset = tp->snd_nxt - tp->snd_una;
20207 if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
20208 if (rack->r_ctl.rc_tlp_new_data) {
20209 /* TLP is forcing out new data */
20210 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
20211 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
20213 if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) {
20214 if (tp->snd_wnd > sb_offset)
20215 len = tp->snd_wnd - sb_offset;
20219 len = rack->r_ctl.rc_tlp_new_data;
20221 rack->r_ctl.rc_tlp_new_data = 0;
20223 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
20225 if ((rack->r_ctl.crte == NULL) &&
20226 IN_FASTRECOVERY(tp->t_flags) &&
20227 (rack->full_size_rxt == 0) &&
20228 (rack->shape_rxt_to_pacing_min == 0) &&
20231 * For prr=off, we need to send only 1 MSS
20232 * at a time. We do this because another sack could
20233 * be arriving that causes us to send retransmits and
20234 * we don't want to be on a long pace due to a larger send
20235 * that keeps us from sending out the retransmit.
20238 } else if (rack->shape_rxt_to_pacing_min &&
20240 /* We use pacing min as shaping len req */
20243 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz);
20246 }/* The else is full_size_rxt is on so send it all */
20248 uint32_t outstanding;
20250 * We are inside of a Fast recovery episode, this
20251 * is caused by a SACK or 3 dup acks. At this point
20252 * we have sent all the retransmissions and we rely
20253 * on PRR to dictate what we will send in the form of
20257 outstanding = tp->snd_max - tp->snd_una;
20258 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
20259 if (tp->snd_wnd > outstanding) {
20260 len = tp->snd_wnd - outstanding;
20261 /* Check to see if we have the data */
20262 if ((sb_offset + len) > avail) {
20263 /* It does not all fit */
20264 if (avail > sb_offset)
20265 len = avail - sb_offset;
20272 } else if (avail > sb_offset) {
20273 len = avail - sb_offset;
20278 if (len > rack->r_ctl.rc_prr_sndcnt) {
20279 len = rack->r_ctl.rc_prr_sndcnt;
20285 if (len > segsiz) {
20287 * We should never send more than a MSS when
20288 * retransmitting or sending new data in prr
20289 * mode unless the override flag is on. Most
20290 * likely the PRR algorithm is not going to
20291 * let us send a lot as well :-)
20293 if (rack->r_ctl.rc_prr_sendalot == 0) {
20296 } else if (len < segsiz) {
20298 * Do we send any? The idea here is if the
20299 * send empty's the socket buffer we want to
20300 * do it. However if not then lets just wait
20301 * for our prr_sndcnt to get bigger.
20305 leftinsb = sbavail(sb) - sb_offset;
20306 if (leftinsb > len) {
20307 /* This send does not empty the sb */
20312 } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
20314 * If you have not established
20315 * and are not doing FAST OPEN
20318 if ((sack_rxmit == 0) &&
20319 (!IS_FASTOPEN(tp->t_flags))){
20324 if (prefetch_so_done == 0) {
20325 kern_prefetch(so, &prefetch_so_done);
20326 prefetch_so_done = 1;
20329 * Lop off SYN bit if it has already been sent. However, if this is
20330 * SYN-SENT state and if segment contains data and if we don't know
20331 * that foreign host supports TAO, suppress sending segment.
20333 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
20334 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
20336 * When sending additional segments following a TFO SYN|ACK,
20337 * do not include the SYN bit.
20339 if (IS_FASTOPEN(tp->t_flags) &&
20340 (tp->t_state == TCPS_SYN_RECEIVED))
20344 * Be careful not to send data and/or FIN on SYN segments. This
20345 * measure is needed to prevent interoperability problems with not
20346 * fully conformant TCP implementations.
20348 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
20353 * On TFO sockets, ensure no data is sent in the following cases:
20355 * - When retransmitting SYN|ACK on a passively-created socket
20357 * - When retransmitting SYN on an actively created socket
20359 * - When sending a zero-length cookie (cookie request) on an
20360 * actively created socket
20362 * - When the socket is in the CLOSED state (RST is being sent)
20364 if (IS_FASTOPEN(tp->t_flags) &&
20365 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
20366 ((tp->t_state == TCPS_SYN_SENT) &&
20367 (tp->t_tfo_client_cookie_len == 0)) ||
20368 (flags & TH_RST))) {
20372 /* Without fast-open there should never be data sent on a SYN */
20373 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
20374 tp->snd_nxt = tp->iss;
20377 if ((len > segsiz) && (tcp_dsack_block_exists(tp))) {
20378 /* We only send 1 MSS if we have a DSACK block */
20379 add_flag |= RACK_SENT_W_DSACK;
20385 * If FIN has been sent but not acked, but we haven't been
20386 * called to retransmit, len will be < 0. Otherwise, window
20387 * shrank after we sent into it. If window shrank to 0,
20388 * cancel pending retransmit, pull snd_nxt back to (closed)
20389 * window, and set the persist timer if it isn't already
20390 * going. If the window didn't close completely, just wait
20393 * We also do a general check here to ensure that we will
20394 * set the persist timer when we have data to send, but a
20395 * 0-byte window. This makes sure the persist timer is set
20396 * even if the packet hits one of the "goto send" lines
20400 if ((tp->snd_wnd == 0) &&
20401 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20402 (tp->snd_una == tp->snd_max) &&
20403 (sb_offset < (int)sbavail(sb))) {
20404 rack_enter_persist(tp, rack, cts, tp->snd_una);
20406 } else if ((rsm == NULL) &&
20407 (doing_tlp == 0) &&
20408 (len < pace_max_seg)) {
20410 * We are not sending a maximum sized segment for
20411 * some reason. Should we not send anything (think
20412 * sws or persists)?
20414 if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20415 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
20417 (len < (int)(sbavail(sb) - sb_offset))) {
20419 * Here the rwnd is less than
20420 * the minimum pacing size, this is not a retransmit,
20421 * we are established and
20422 * the send is not the last in the socket buffer
20423 * we send nothing, and we may enter persists
20424 * if nothing is outstanding.
20427 if (tp->snd_max == tp->snd_una) {
20429 * Nothing out we can
20430 * go into persists.
20432 rack_enter_persist(tp, rack, cts, tp->snd_una);
20434 } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
20435 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20436 (len < (int)(sbavail(sb) - sb_offset)) &&
20439 * Here we are not retransmitting, and
20440 * the cwnd is not so small that we could
20441 * not send at least a min size (rxt timer
20442 * not having gone off), We have 2 segments or
20443 * more already in flight, its not the tail end
20444 * of the socket buffer and the cwnd is blocking
20445 * us from sending out a minimum pacing segment size.
20446 * Lets not send anything.
20449 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
20450 min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
20451 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
20452 (len < (int)(sbavail(sb) - sb_offset)) &&
20453 (TCPS_HAVEESTABLISHED(tp->t_state))) {
20455 * Here we have a send window but we have
20456 * filled it up and we can't send another pacing segment.
20457 * We also have in flight more than 2 segments
20458 * and we are not completing the sb i.e. we allow
20459 * the last bytes of the sb to go out even if
20460 * its not a full pacing segment.
20463 } else if ((rack->r_ctl.crte != NULL) &&
20464 (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) &&
20465 (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) &&
20466 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) &&
20467 (len < (int)(sbavail(sb) - sb_offset))) {
20469 * Here we are doing hardware pacing, this is not a TLP,
20470 * we are not sending a pace max segment size, there is rwnd
20471 * room to send at least N pace_max_seg, the cwnd is greater
20472 * than or equal to a full pacing segments plus 4 mss and we have 2 or
20473 * more segments in flight and its not the tail of the socket buffer.
20475 * We don't want to send instead we need to get more ack's in to
20476 * allow us to send a full pacing segment. Normally, if we are pacing
20477 * about the right speed, we should have finished our pacing
20478 * send as most of the acks have come back if we are at the
20479 * right rate. This is a bit fuzzy since return path delay
20480 * can delay the acks, which is why we want to make sure we
20481 * have cwnd space to have a bit more than a max pace segments in flight.
20483 * If we have not gotten our acks back we are pacing at too high a
20484 * rate delaying will not hurt and will bring our GP estimate down by
20485 * injecting the delay. If we don't do this we will send
20486 * 2 MSS out in response to the acks being clocked in which
20487 * defeats the point of hw-pacing (i.e. to help us get
20488 * larger TSO's out).
20494 /* len will be >= 0 after this point. */
20495 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
20496 rack_sndbuf_autoscale(rack);
20498 * Decide if we can use TCP Segmentation Offloading (if supported by
20501 * TSO may only be used if we are in a pure bulk sending state. The
20502 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
20503 * options prevent using TSO. With TSO the TCP header is the same
20504 * (except for the sequence number) for all generated packets. This
20505 * makes it impossible to transmit any options which vary per
20506 * generated segment or packet.
20508 * IPv4 handling has a clear separation of ip options and ip header
20509 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
20510 * the right thing below to provide length of just ip options and thus
20511 * checking for ipoptlen is enough to decide if ip options are present.
20514 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20516 * Pre-calculate here as we save another lookup into the darknesses
20517 * of IPsec that way and can actually decide if TSO is ok.
20520 if (isipv6 && IPSEC_ENABLED(ipv6))
20521 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp);
20527 if (IPSEC_ENABLED(ipv4))
20528 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp);
20532 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
20533 ipoptlen += ipsec_optlen;
20535 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
20536 (tp->t_port == 0) &&
20537 ((tp->t_flags & TF_SIGNATURE) == 0) &&
20538 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
20542 uint32_t outstanding __unused;
20544 outstanding = tp->snd_max - tp->snd_una;
20545 if (tp->t_flags & TF_SENTFIN) {
20547 * If we sent a fin, snd_max is 1 higher than
20553 if ((rsm->r_flags & RACK_HAS_FIN) == 0)
20556 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
20561 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
20562 (long)TCP_MAXWIN << tp->rcv_scale);
20565 * Sender silly window avoidance. We transmit under the following
20566 * conditions when len is non-zero:
20568 * - We have a full segment (or more with TSO) - This is the last
20569 * buffer in a write()/send() and we are either idle or running
20570 * NODELAY - we've timed out (e.g. persist timer) - we have more
20571 * then 1/2 the maximum send window's worth of data (receiver may be
20572 * limited the window size) - we need to retransmit
20575 if (len >= segsiz) {
20579 * NOTE! on localhost connections an 'ack' from the remote
20580 * end may occur synchronously with the output and cause us
20581 * to flush a buffer queued with moretocome. XXX
20584 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
20585 (idle || (tp->t_flags & TF_NODELAY)) &&
20586 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20587 (tp->t_flags & TF_NOPUSH) == 0) {
20591 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
20595 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
20599 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */
20607 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
20608 (ctf_outstanding(tp) < (segsiz * 2))) {
20610 * We have less than two MSS outstanding (delayed ack)
20611 * and our rwnd will not let us send a full sized
20612 * MSS. Lets go ahead and let this small segment
20613 * out because we want to try to have at least two
20614 * packets inflight to not be caught by delayed ack.
20621 * Sending of standalone window updates.
20623 * Window updates are important when we close our window due to a
20624 * full socket buffer and are opening it again after the application
20625 * reads data from it. Once the window has opened again and the
20626 * remote end starts to send again the ACK clock takes over and
20627 * provides the most current window information.
20629 * We must avoid the silly window syndrome whereas every read from
20630 * the receive buffer, no matter how small, causes a window update
20631 * to be sent. We also should avoid sending a flurry of window
20632 * updates when the socket buffer had queued a lot of data and the
20633 * application is doing small reads.
20635 * Prevent a flurry of pointless window updates by only sending an
20636 * update when we can increase the advertized window by more than
20637 * 1/4th of the socket buffer capacity. When the buffer is getting
20638 * full or is very small be more aggressive and send an update
20639 * whenever we can increase by two mss sized segments. In all other
20640 * situations the ACK's to new incoming data will carry further
20641 * window increases.
20643 * Don't send an independent window update if a delayed ACK is
20644 * pending (it will get piggy-backed on it) or the remote side
20645 * already has done a half-close and won't send more data. Skip
20646 * this if the connection is in T/TCP half-open state.
20648 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
20649 !(tp->t_flags & TF_DELACK) &&
20650 !TCPS_HAVERCVDFIN(tp->t_state)) {
20652 * "adv" is the amount we could increase the window, taking
20653 * into account that we are limited by TCP_MAXWIN <<
20660 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
20661 oldwin = (tp->rcv_adv - tp->rcv_nxt);
20665 /* We can't increase the window */
20672 * If the new window size ends up being the same as or less
20673 * than the old size when it is scaled, then don't force
20676 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
20679 if (adv >= (int32_t)(2 * segsiz) &&
20680 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
20681 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
20682 so->so_rcv.sb_hiwat <= 8 * segsiz)) {
20686 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
20694 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
20695 * is also a catch-all for the retransmit timer timeout case.
20697 if (tp->t_flags & TF_ACKNOW) {
20701 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
20706 * If our state indicates that FIN should be sent and we have not
20707 * yet done so, then we need to send.
20709 if ((flags & TH_FIN) &&
20710 (tp->snd_nxt == tp->snd_una)) {
20715 * No reason to send a segment, just return.
20718 SOCKBUF_UNLOCK(sb);
20719 just_return_nolock:
20721 int app_limited = CTF_JR_SENT_DATA;
20723 if (tot_len_this_send > 0) {
20724 /* Make sure snd_nxt is up to max */
20725 rack->r_ctl.fsb.recwin = recwin;
20726 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
20727 if ((error == 0) &&
20729 ((flags & (TH_SYN|TH_FIN)) == 0) &&
20731 (tp->snd_nxt == tp->snd_max) &&
20732 (tp->rcv_numsacks == 0) &&
20733 rack->r_fsb_inited &&
20734 TCPS_HAVEESTABLISHED(tp->t_state) &&
20735 ((IN_RECOVERY(tp->t_flags)) == 0) &&
20736 (rack->r_must_retran == 0) &&
20737 ((tp->t_flags & TF_NEEDFIN) == 0) &&
20738 (len > 0) && (orig_len > 0) &&
20739 (orig_len > len) &&
20740 ((orig_len - len) >= segsiz) &&
20742 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
20743 /* We can send at least one more MSS using our fsb */
20744 rack_setup_fast_output(tp, rack, sb, len, orig_len,
20745 segsiz, pace_max_seg, hw_tls, flags);
20747 rack->r_fast_output = 0;
20750 rack_log_fsb(rack, tp, so, flags,
20751 ipoptlen, orig_len, len, 0,
20752 1, optlen, __LINE__, 1);
20753 if (SEQ_GT(tp->snd_max, tp->snd_nxt))
20754 tp->snd_nxt = tp->snd_max;
20756 int end_window = 0;
20757 uint32_t seq = tp->gput_ack;
20759 rsm = tqhash_max(rack->r_ctl.tqh);
20762 * Mark the last sent that we just-returned (hinting
20763 * that delayed ack may play a role in any rtt measurement).
20765 rsm->r_just_ret = 1;
20767 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
20768 rack->r_ctl.rc_agg_delayed = 0;
20771 rack->r_ctl.rc_agg_early = 0;
20772 if ((ctf_outstanding(tp) +
20773 min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
20774 minseg)) >= tp->snd_wnd) {
20775 /* We are limited by the rwnd */
20776 app_limited = CTF_JR_RWND_LIMITED;
20777 if (IN_FASTRECOVERY(tp->t_flags))
20778 rack->r_ctl.rc_prr_sndcnt = 0;
20779 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
20780 /* We are limited by whats available -- app limited */
20781 app_limited = CTF_JR_APP_LIMITED;
20782 if (IN_FASTRECOVERY(tp->t_flags))
20783 rack->r_ctl.rc_prr_sndcnt = 0;
20784 } else if ((idle == 0) &&
20785 ((tp->t_flags & TF_NODELAY) == 0) &&
20786 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
20789 * No delay is not on and the
20790 * user is sending less than 1MSS. This
20791 * brings out SWS avoidance so we
20792 * don't send. Another app-limited case.
20794 app_limited = CTF_JR_APP_LIMITED;
20795 } else if (tp->t_flags & TF_NOPUSH) {
20797 * The user has requested no push of
20798 * the last segment and we are
20799 * at the last segment. Another app
20802 app_limited = CTF_JR_APP_LIMITED;
20803 } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
20805 app_limited = CTF_JR_CWND_LIMITED;
20806 } else if (IN_FASTRECOVERY(tp->t_flags) &&
20807 (rack->rack_no_prr == 0) &&
20808 (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
20809 app_limited = CTF_JR_PRR;
20811 /* Now why here are we not sending? */
20814 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
20817 app_limited = CTF_JR_ASSESSING;
20820 * App limited in some fashion, for our pacing GP
20821 * measurements we don't want any gap (even cwnd).
20822 * Close down the measurement window.
20824 if (rack_cwnd_block_ends_measure &&
20825 ((app_limited == CTF_JR_CWND_LIMITED) ||
20826 (app_limited == CTF_JR_PRR))) {
20828 * The reason we are not sending is
20829 * the cwnd (or prr). We have been configured
20830 * to end the measurement window in
20834 } else if (rack_rwnd_block_ends_measure &&
20835 (app_limited == CTF_JR_RWND_LIMITED)) {
20837 * We are rwnd limited and have been
20838 * configured to end the measurement
20839 * window in this case.
20842 } else if (app_limited == CTF_JR_APP_LIMITED) {
20844 * A true application limited period, we have
20848 } else if (app_limited == CTF_JR_ASSESSING) {
20850 * In the assessing case we hit the end of
20851 * the if/else and had no known reason
20852 * This will panic us under invariants..
20854 * If we get this out in logs we need to
20855 * investagate which reason we missed.
20862 /* Adjust the Gput measurement */
20863 if ((tp->t_flags & TF_GPUTINPROG) &&
20864 SEQ_GT(tp->gput_ack, tp->snd_max)) {
20865 tp->gput_ack = tp->snd_max;
20866 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) {
20868 * There is not enough to measure.
20870 tp->t_flags &= ~TF_GPUTINPROG;
20871 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/,
20872 rack->r_ctl.rc_gp_srtt /*flex1*/,
20874 0, 0, 18, __LINE__, NULL, 0);
20878 /* Mark the last packet has app limited */
20879 rsm = tqhash_max(rack->r_ctl.tqh);
20880 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
20881 if (rack->r_ctl.rc_app_limited_cnt == 0)
20882 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
20885 * Go out to the end app limited and mark
20886 * this new one as next and move the end_appl up
20889 if (rack->r_ctl.rc_end_appl)
20890 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
20891 rack->r_ctl.rc_end_appl = rsm;
20893 rsm->r_flags |= RACK_APP_LIMITED;
20894 rack->r_ctl.rc_app_limited_cnt++;
20897 rack_log_pacing_delay_calc(rack,
20898 rack->r_ctl.rc_app_limited_cnt, seq,
20899 tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0);
20902 /* Check if we need to go into persists or not */
20903 if ((tp->snd_max == tp->snd_una) &&
20904 TCPS_HAVEESTABLISHED(tp->t_state) &&
20906 (sbavail(sb) > tp->snd_wnd) &&
20907 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
20908 /* Yes lets make sure to move to persist before timer-start */
20909 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una);
20911 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
20912 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
20914 #ifdef NETFLIX_SHARED_CWND
20915 if ((sbavail(sb) == 0) &&
20916 rack->r_ctl.rc_scw) {
20917 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
20918 rack->rack_scwnd_is_idle = 1;
20921 #ifdef TCP_ACCOUNTING
20922 if (tot_len_this_send > 0) {
20923 crtsc = get_cyclecount();
20924 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20925 tp->tcp_cnt_counters[SND_OUT_DATA]++;
20927 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20928 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val);
20930 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20931 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz);
20934 crtsc = get_cyclecount();
20935 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20936 tp->tcp_cnt_counters[SND_LIMITED]++;
20938 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
20939 tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val);
20947 if ((rack->r_ctl.crte != NULL) &&
20949 ((rack->rc_hw_nobuf == 1) ||
20950 (rack_hw_check_queue && (check_done == 0)))) {
20952 * We only want to do this once with the hw_check_queue,
20953 * for the enobuf case we would only do it once if
20954 * we come around to again, the flag will be clear.
20957 slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz);
20959 rack->r_ctl.rc_agg_delayed = 0;
20960 rack->r_ctl.rc_agg_early = 0;
20963 SOCKBUF_UNLOCK(&so->so_snd);
20964 goto skip_all_send;
20967 if (rsm || sack_rxmit)
20968 counter_u64_add(rack_nfto_resend, 1);
20970 counter_u64_add(rack_non_fto_send, 1);
20971 if ((flags & TH_FIN) &&
20974 * We do not transmit a FIN
20975 * with data outstanding. We
20976 * need to make it so all data
20981 /* Enforce stack imposed max seg size if we have one */
20982 if (rack->r_ctl.rc_pace_max_segs &&
20983 (len > rack->r_ctl.rc_pace_max_segs)) {
20985 len = rack->r_ctl.rc_pace_max_segs;
20987 SOCKBUF_LOCK_ASSERT(sb);
20990 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
20992 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
20995 * Before ESTABLISHED, force sending of initial options unless TCP
20996 * set not to do any options. NOTE: we assume that the IP/TCP header
20997 * plus TCP options always fit in a single mbuf, leaving room for a
20998 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
20999 * + optlen <= MCLBYTES
21004 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
21007 hdrlen = sizeof(struct tcpiphdr);
21010 * Compute options for segment. We only have to care about SYN and
21011 * established connection segments. Options for SYN-ACK segments
21012 * are handled in TCP syncache.
21015 if ((tp->t_flags & TF_NOOPT) == 0) {
21016 /* Maximum segment size. */
21017 if (flags & TH_SYN) {
21018 tp->snd_nxt = tp->iss;
21019 to.to_mss = tcp_mssopt(&inp->inp_inc);
21021 to.to_mss -= V_tcp_udp_tunneling_overhead;
21022 to.to_flags |= TOF_MSS;
21025 * On SYN or SYN|ACK transmits on TFO connections,
21026 * only include the TFO option if it is not a
21027 * retransmit, as the presence of the TFO option may
21028 * have caused the original SYN or SYN|ACK to have
21029 * been dropped by a middlebox.
21031 if (IS_FASTOPEN(tp->t_flags) &&
21032 (tp->t_rxtshift == 0)) {
21033 if (tp->t_state == TCPS_SYN_RECEIVED) {
21034 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
21036 (u_int8_t *)&tp->t_tfo_cookie.server;
21037 to.to_flags |= TOF_FASTOPEN;
21039 } else if (tp->t_state == TCPS_SYN_SENT) {
21041 tp->t_tfo_client_cookie_len;
21043 tp->t_tfo_cookie.client;
21044 to.to_flags |= TOF_FASTOPEN;
21047 * If we wind up having more data to
21048 * send with the SYN than can fit in
21049 * one segment, don't send any more
21050 * until the SYN|ACK comes back from
21057 /* Window scaling. */
21058 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
21059 to.to_wscale = tp->request_r_scale;
21060 to.to_flags |= TOF_SCALE;
21063 if ((tp->t_flags & TF_RCVD_TSTMP) ||
21064 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
21065 to.to_tsval = ms_cts + tp->ts_offset;
21066 to.to_tsecr = tp->ts_recent;
21067 to.to_flags |= TOF_TS;
21069 /* Set receive buffer autosizing timestamp. */
21070 if (tp->rfbuf_ts == 0 &&
21071 (so->so_rcv.sb_flags & SB_AUTOSIZE))
21072 tp->rfbuf_ts = tcp_ts_getticks();
21073 /* Selective ACK's. */
21074 if (tp->t_flags & TF_SACK_PERMIT) {
21075 if (flags & TH_SYN)
21076 to.to_flags |= TOF_SACKPERM;
21077 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21078 tp->rcv_numsacks > 0) {
21079 to.to_flags |= TOF_SACK;
21080 to.to_nsacks = tp->rcv_numsacks;
21081 to.to_sacks = (u_char *)tp->sackblks;
21084 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21085 /* TCP-MD5 (RFC2385). */
21086 if (tp->t_flags & TF_SIGNATURE)
21087 to.to_flags |= TOF_SIGNATURE;
21088 #endif /* TCP_SIGNATURE */
21090 /* Processing the options. */
21091 hdrlen += optlen = tcp_addoptions(&to, opt);
21093 * If we wanted a TFO option to be added, but it was unable
21094 * to fit, ensure no data is sent.
21096 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
21097 !(to.to_flags & TOF_FASTOPEN))
21101 if (V_tcp_udp_tunneling_port == 0) {
21102 /* The port was removed?? */
21103 SOCKBUF_UNLOCK(&so->so_snd);
21104 #ifdef TCP_ACCOUNTING
21105 crtsc = get_cyclecount();
21106 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21107 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
21109 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
21110 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
21114 return (EHOSTUNREACH);
21116 hdrlen += sizeof(struct udphdr);
21120 ipoptlen = ip6_optlen(inp);
21123 if (inp->inp_options)
21124 ipoptlen = inp->inp_options->m_len -
21125 offsetof(struct ipoption, ipopt_list);
21128 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21129 ipoptlen += ipsec_optlen;
21133 * Adjust data length if insertion of options will bump the packet
21134 * length beyond the t_maxseg length. Clear the FIN bit because we
21135 * cut off the tail of the segment.
21137 if (len + optlen + ipoptlen > tp->t_maxseg) {
21139 uint32_t if_hw_tsomax;
21143 /* extract TSO information */
21144 if_hw_tsomax = tp->t_tsomax;
21145 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
21146 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
21147 KASSERT(ipoptlen == 0,
21148 ("%s: TSO can't do IP options", __func__));
21151 * Check if we should limit by maximum payload
21154 if (if_hw_tsomax != 0) {
21155 /* compute maximum TSO length */
21156 max_len = (if_hw_tsomax - hdrlen -
21158 if (max_len <= 0) {
21160 } else if (len > max_len) {
21167 * Prevent the last segment from being fractional
21168 * unless the send sockbuf can be emptied:
21170 max_len = (tp->t_maxseg - optlen);
21171 if ((sb_offset + len) < sbavail(sb)) {
21172 moff = len % (u_int)max_len;
21179 * In case there are too many small fragments don't
21182 if (len <= max_len) {
21187 * Send the FIN in a separate segment after the bulk
21188 * sending is done. We don't trust the TSO
21189 * implementations to clear the FIN flag on all but
21190 * the last segment.
21192 if (tp->t_flags & TF_NEEDFIN) {
21197 if (optlen + ipoptlen >= tp->t_maxseg) {
21199 * Since we don't have enough space to put
21200 * the IP header chain and the TCP header in
21201 * one packet as required by RFC 7112, don't
21202 * send it. Also ensure that at least one
21203 * byte of the payload can be put into the
21206 SOCKBUF_UNLOCK(&so->so_snd);
21211 len = tp->t_maxseg - optlen - ipoptlen;
21218 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
21219 ("%s: len > IP_MAXPACKET", __func__));
21222 if (max_linkhdr + hdrlen > MCLBYTES)
21224 if (max_linkhdr + hdrlen > MHLEN)
21226 panic("tcphdr too big");
21230 * This KASSERT is here to catch edge cases at a well defined place.
21231 * Before, those had triggered (random) panic conditions further
21234 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
21236 (flags & TH_FIN) &&
21239 * We have outstanding data, don't send a fin by itself!.
21244 * Grab a header mbuf, attaching a copy of data to be transmitted,
21245 * and initialize the header from the template for sends on this
21248 hw_tls = tp->t_nic_ktls_xmit != 0;
21253 if (rack->r_ctl.rc_pace_max_segs)
21254 max_val = rack->r_ctl.rc_pace_max_segs;
21255 else if (rack->rc_user_set_max_segs)
21256 max_val = rack->rc_user_set_max_segs * segsiz;
21260 * We allow a limit on sending with hptsi.
21262 if (len > max_val) {
21267 if (MHLEN < hdrlen + max_linkhdr)
21268 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
21271 m = m_gethdr(M_NOWAIT, MT_DATA);
21274 SOCKBUF_UNLOCK(sb);
21279 m->m_data += max_linkhdr;
21283 * Start the m_copy functions from the closest mbuf to the
21284 * sb_offset in the socket buffer chain.
21286 mb = sbsndptr_noadv(sb, sb_offset, &moff);
21289 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
21290 m_copydata(mb, moff, (int)len,
21291 mtod(m, caddr_t)+hdrlen);
21292 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
21293 sbsndptr_adv(sb, mb, len);
21296 struct sockbuf *msb;
21298 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
21302 m->m_next = tcp_m_copym(
21304 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
21305 ((rsm == NULL) ? hw_tls : 0)
21306 #ifdef NETFLIX_COPY_ARGS
21310 if (len <= (tp->t_maxseg - optlen)) {
21312 * Must have ran out of mbufs for the copy
21313 * shorten it to no longer need tso. Lets
21314 * not put on sendalot since we are low on
21319 if (m->m_next == NULL) {
21320 SOCKBUF_UNLOCK(sb);
21327 if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
21328 if (rsm && (rsm->r_flags & RACK_TLP)) {
21330 * TLP should not count in retran count, but
21333 counter_u64_add(rack_tlp_retran, 1);
21334 counter_u64_add(rack_tlp_retran_bytes, len);
21336 tp->t_sndrexmitpack++;
21337 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
21338 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
21341 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
21345 KMOD_TCPSTAT_INC(tcps_sndpack);
21346 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
21348 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
21353 * If we're sending everything we've got, set PUSH. (This
21354 * will keep happy those implementations which only give
21355 * data to the user when a buffer fills or a PUSH comes in.)
21357 if (sb_offset + len == sbused(sb) &&
21359 !(flags & TH_SYN)) {
21361 add_flag |= RACK_HAD_PUSH;
21364 SOCKBUF_UNLOCK(sb);
21366 SOCKBUF_UNLOCK(sb);
21367 if (tp->t_flags & TF_ACKNOW)
21368 KMOD_TCPSTAT_INC(tcps_sndacks);
21369 else if (flags & (TH_SYN | TH_FIN | TH_RST))
21370 KMOD_TCPSTAT_INC(tcps_sndctrl);
21372 KMOD_TCPSTAT_INC(tcps_sndwinup);
21374 m = m_gethdr(M_NOWAIT, MT_DATA);
21381 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
21383 M_ALIGN(m, hdrlen);
21386 m->m_data += max_linkhdr;
21389 SOCKBUF_UNLOCK_ASSERT(sb);
21390 m->m_pkthdr.rcvif = (struct ifnet *)0;
21392 mac_inpcb_create_mbuf(inp, m);
21394 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
21397 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr;
21401 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
21403 th = rack->r_ctl.fsb.th;
21404 udp = rack->r_ctl.fsb.udp;
21408 ulen = hdrlen + len - sizeof(struct ip6_hdr);
21411 ulen = hdrlen + len - sizeof(struct ip);
21412 udp->uh_ulen = htons(ulen);
21417 ip6 = mtod(m, struct ip6_hdr *);
21419 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
21420 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21421 udp->uh_dport = tp->t_port;
21422 ulen = hdrlen + len - sizeof(struct ip6_hdr);
21423 udp->uh_ulen = htons(ulen);
21424 th = (struct tcphdr *)(udp + 1);
21426 th = (struct tcphdr *)(ip6 + 1);
21427 tcpip_fillheaders(inp, tp->t_port, ip6, th);
21432 ip = mtod(m, struct ip *);
21434 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
21435 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
21436 udp->uh_dport = tp->t_port;
21437 ulen = hdrlen + len - sizeof(struct ip);
21438 udp->uh_ulen = htons(ulen);
21439 th = (struct tcphdr *)(udp + 1);
21441 th = (struct tcphdr *)(ip + 1);
21442 tcpip_fillheaders(inp, tp->t_port, ip, th);
21447 * Fill in fields, remembering maximum advertised window for use in
21448 * delaying messages about window sizes. If resending a FIN, be sure
21449 * not to use a new sequence number.
21451 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
21452 tp->snd_nxt == tp->snd_max)
21455 * If we are starting a connection, send ECN setup SYN packet. If we
21456 * are on a retransmit, we may resend those bits a number of times
21459 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
21460 flags |= tcp_ecn_output_syn_sent(tp);
21462 /* Also handle parallel SYN for ECN */
21463 if (TCPS_HAVERCVDSYN(tp->t_state) &&
21464 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) {
21465 int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit);
21466 if ((tp->t_state == TCPS_SYN_RECEIVED) &&
21467 (tp->t_flags2 & TF2_ECN_SND_ECE))
21468 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
21471 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20);
21472 ip6->ip6_flow |= htonl(ect << 20);
21478 ip->ip_tos &= ~IPTOS_ECN_MASK;
21484 * If we are doing retransmissions, then snd_nxt will not reflect
21485 * the first unsent octet. For ACK only packets, we do not want the
21486 * sequence number of the retransmitted packet, we want the sequence
21487 * number of the next unsent octet. So, if there is no data (and no
21488 * SYN or FIN), use snd_max instead of snd_nxt when filling in
21489 * ti_seq. But if we are in persist state, snd_max might reflect
21490 * one byte beyond the right edge of the window, so use snd_nxt in
21491 * that case, since we know we aren't doing a retransmission.
21492 * (retransmit and persist are mutually exclusive...)
21494 if (sack_rxmit == 0) {
21495 if (len || (flags & (TH_SYN | TH_FIN))) {
21496 th->th_seq = htonl(tp->snd_nxt);
21497 rack_seq = tp->snd_nxt;
21499 th->th_seq = htonl(tp->snd_max);
21500 rack_seq = tp->snd_max;
21503 th->th_seq = htonl(rsm->r_start);
21504 rack_seq = rsm->r_start;
21506 th->th_ack = htonl(tp->rcv_nxt);
21507 tcp_set_flags(th, flags);
21509 * Calculate receive window. Don't shrink window, but avoid silly
21511 * If a RST segment is sent, advertise a window of zero.
21513 if (flags & TH_RST) {
21516 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
21517 recwin < (long)segsiz) {
21520 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
21521 recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
21522 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
21526 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
21527 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
21528 * handled in syncache.
21530 if (flags & TH_SYN)
21531 th->th_win = htons((u_short)
21532 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
21534 /* Avoid shrinking window with window scaling. */
21535 recwin = roundup2(recwin, 1 << tp->rcv_scale);
21536 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
21539 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
21540 * window. This may cause the remote transmitter to stall. This
21541 * flag tells soreceive() to disable delayed acknowledgements when
21542 * draining the buffer. This can occur if the receiver is
21543 * attempting to read more data than can be buffered prior to
21544 * transmitting on the connection.
21546 if (th->th_win == 0) {
21547 tp->t_sndzerowin++;
21548 tp->t_flags |= TF_RXWIN0SENT;
21550 tp->t_flags &= ~TF_RXWIN0SENT;
21551 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */
21552 /* Now are we using fsb?, if so copy the template data to the mbuf */
21553 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) {
21556 cpto = mtod(m, uint8_t *);
21557 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len);
21559 * We have just copied in:
21561 * <optional udphdr>
21562 * tcphdr (no options)
21564 * We need to grab the correct pointers into the mbuf
21565 * for both the tcp header, and possibly the udp header (if tunneling).
21566 * We do this by using the offset in the copy buffer and adding it
21567 * to the mbuf base pointer (cpto).
21571 ip6 = mtod(m, struct ip6_hdr *);
21575 ip = mtod(m, struct ip *);
21577 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr));
21578 /* If we have a udp header lets set it into the mbuf as well */
21580 udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr));
21582 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
21583 if (to.to_flags & TOF_SIGNATURE) {
21585 * Calculate MD5 signature and put it into the place
21586 * determined before.
21587 * NOTE: since TCP options buffer doesn't point into
21588 * mbuf's data, calculate offset and use it.
21590 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
21591 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
21593 * Do not send segment if the calculation of MD5
21594 * digest has failed.
21601 bcopy(opt, th + 1, optlen);
21602 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
21605 * Put TCP length in extended header, and then checksum extended
21608 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
21612 * ip6_plen is not need to be filled now, and will be filled
21616 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
21617 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21618 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
21619 th->th_sum = htons(0);
21620 UDPSTAT_INC(udps_opackets);
21622 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
21623 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21624 th->th_sum = in6_cksum_pseudo(ip6,
21625 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
21630 #if defined(INET6) && defined(INET)
21636 m->m_pkthdr.csum_flags = CSUM_UDP;
21637 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
21638 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
21639 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
21640 th->th_sum = htons(0);
21641 UDPSTAT_INC(udps_opackets);
21643 m->m_pkthdr.csum_flags = CSUM_TCP;
21644 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
21645 th->th_sum = in_pseudo(ip->ip_src.s_addr,
21646 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
21647 IPPROTO_TCP + len + optlen));
21649 /* IP version must be set here for ipv4/ipv6 checking later */
21650 KASSERT(ip->ip_v == IPVERSION,
21651 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
21655 * Enable TSO and specify the size of the segments. The TCP pseudo
21656 * header checksum is always provided. XXX: Fixme: This is currently
21657 * not the case for IPv6.
21661 * Here we must use t_maxseg and the optlen since
21662 * the optlen may include SACK's (or DSACK).
21664 KASSERT(len > tp->t_maxseg - optlen,
21665 ("%s: len <= tso_segsz", __func__));
21666 m->m_pkthdr.csum_flags |= CSUM_TSO;
21667 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
21669 KASSERT(len + hdrlen == m_length(m, NULL),
21670 ("%s: mbuf chain different than expected: %d + %u != %u",
21671 __func__, len, hdrlen, m_length(m, NULL)));
21674 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
21675 hhook_run_tcp_est_out(tp, th, &to, len, tso);
21677 if ((rack->r_ctl.crte != NULL) &&
21678 (rack->rc_hw_nobuf == 0) &&
21679 tcp_bblogging_on(tp)) {
21680 rack_log_queue_level(tp, rack, len, &tv, cts);
21682 /* We're getting ready to send; log now. */
21683 if (tcp_bblogging_on(rack->rc_tp)) {
21684 union tcp_log_stackspecific log;
21686 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
21687 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp);
21688 if (rack->rack_no_prr)
21689 log.u_bbr.flex1 = 0;
21691 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
21692 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
21693 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
21694 log.u_bbr.flex4 = orig_len;
21695 /* Save off the early/late values */
21696 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
21697 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
21698 log.u_bbr.bw_inuse = rack_get_bw(rack);
21699 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw;
21700 log.u_bbr.flex8 = 0;
21702 if (rsm->r_flags & RACK_RWND_COLLAPSED) {
21703 rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm);
21704 counter_u64_add(rack_collapsed_win_rxt, 1);
21705 counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start));
21708 log.u_bbr.flex8 = 2;
21710 log.u_bbr.flex8 = 1;
21713 log.u_bbr.flex8 = 3;
21715 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
21716 log.u_bbr.flex7 = mark;
21717 log.u_bbr.flex7 <<= 8;
21718 log.u_bbr.flex7 |= pass;
21719 log.u_bbr.pkts_out = tp->t_maxseg;
21720 log.u_bbr.timeStamp = cts;
21721 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
21722 if (rsm && (rsm->r_rtr_cnt > 0)) {
21724 * When we have a retransmit we want to log the
21725 * burst at send and flight at send from before.
21727 log.u_bbr.flex5 = rsm->r_fas;
21728 log.u_bbr.bbr_substate = rsm->r_bas;
21731 * New transmits we log in flex5 the inflight again as
21732 * well as the number of segments in our send in the
21735 log.u_bbr.flex5 = log.u_bbr.inflight;
21736 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz);
21738 log.u_bbr.lt_epoch = cwnd_to_use;
21739 log.u_bbr.delivered = sendalot;
21740 log.u_bbr.rttProp = (uint64_t)rsm;
21741 log.u_bbr.pkt_epoch = __LINE__;
21743 log.u_bbr.delRate = rsm->r_flags;
21744 log.u_bbr.delRate <<= 31;
21745 log.u_bbr.delRate |= rack->r_must_retran;
21746 log.u_bbr.delRate <<= 1;
21747 log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21749 log.u_bbr.delRate = rack->r_must_retran;
21750 log.u_bbr.delRate <<= 1;
21751 log.u_bbr.delRate |= (sack_rxmit & 0x00000001);
21753 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
21754 len, &log, false, NULL, __func__, __LINE__, &tv);
21759 * Fill in IP length and desired time to live and send to IP level.
21760 * There should be a better way to handle ttl and tos; we could keep
21761 * them in the template, but need a way to checksum without them.
21764 * m->m_pkthdr.len should have been set before cksum calcuration,
21765 * because in6_cksum() need it.
21770 * we separately set hoplimit for every segment, since the
21771 * user might want to change the value via setsockopt. Also,
21772 * desired default hop limit might be changed via Neighbor
21775 rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL);
21778 * Set the packet size here for the benefit of DTrace
21779 * probes. ip6_output() will set it properly; it's supposed
21780 * to include the option header lengths as well.
21782 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
21784 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
21785 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
21787 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
21789 if (tp->t_state == TCPS_SYN_SENT)
21790 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
21792 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
21793 /* TODO: IPv6 IP6TOS_ECT bit on */
21794 error = ip6_output(m,
21795 inp->in6p_outputopts,
21797 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
21800 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
21801 mtu = inp->inp_route6.ro_nh->nh_mtu;
21804 #if defined(INET) && defined(INET6)
21809 ip->ip_len = htons(m->m_pkthdr.len);
21811 if (inp->inp_vflag & INP_IPV6PROTO)
21812 ip->ip_ttl = in6_selecthlim(inp, NULL);
21814 rack->r_ctl.fsb.hoplimit = ip->ip_ttl;
21816 * If we do path MTU discovery, then we set DF on every
21817 * packet. This might not be the best thing to do according
21818 * to RFC3390 Section 2. However the tcp hostcache migitates
21819 * the problem so it affects only the first tcp connection
21822 * NB: Don't set DF on small MTU/MSS to have a safe
21825 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
21826 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
21827 if (tp->t_port == 0 || len < V_tcp_minmss) {
21828 ip->ip_off |= htons(IP_DF);
21831 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
21834 if (tp->t_state == TCPS_SYN_SENT)
21835 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
21837 TCP_PROBE5(send, NULL, tp, ip, tp, th);
21839 error = ip_output(m,
21840 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
21846 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
21848 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
21849 mtu = inp->inp_route.ro_nh->nh_mtu;
21855 lgb->tlb_errno = error;
21859 * In transmit state, time the transmission and arrange for the
21860 * retransmit. In persist state, just set snd_max.
21862 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error,
21863 rack_to_usec_ts(&tv),
21864 rsm, add_flag, s_mb, s_moff, hw_tls, segsiz);
21867 if (rack->lt_bw_up == 0) {
21868 rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv);
21869 rack->r_ctl.lt_seq = tp->snd_una;
21870 rack->lt_bw_up = 1;
21871 } else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) {
21873 * Need to record what we have since we are
21874 * approaching seq wrap.
21878 rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq);
21879 rack->r_ctl.lt_seq = tp->snd_una;
21880 tmark = tcp_tv_to_lusectick(&tv);
21881 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark);
21882 rack->r_ctl.lt_timemark = tmark;
21885 rack->forced_ack = 0; /* If we send something zap the FA flag */
21886 counter_u64_add(rack_total_bytes, len);
21887 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls);
21888 if (rsm && doing_tlp) {
21889 rack->rc_last_sent_tlp_past_cumack = 0;
21890 rack->rc_last_sent_tlp_seq_valid = 1;
21891 rack->r_ctl.last_sent_tlp_seq = rsm->r_start;
21892 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start;
21894 if (rack->rc_hw_nobuf) {
21895 rack->rc_hw_nobuf = 0;
21896 rack->r_ctl.rc_agg_delayed = 0;
21899 rack->r_ctl.rc_agg_early = 0;
21901 if (rsm && (doing_tlp == 0)) {
21902 /* Set we retransmitted */
21903 rack->rc_gp_saw_rec = 1;
21905 if (cwnd_to_use > tp->snd_ssthresh) {
21906 /* Set we sent in CA */
21907 rack->rc_gp_saw_ca = 1;
21909 /* Set we sent in SS */
21910 rack->rc_gp_saw_ss = 1;
21913 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
21914 (tp->t_flags & TF_SACK_PERMIT) &&
21915 tp->rcv_numsacks > 0)
21916 tcp_clean_dsack_blocks(tp);
21917 tot_len_this_send += len;
21919 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
21923 idx = (len / segsiz) + 3;
21924 if (idx >= TCP_MSS_ACCT_ATIMER)
21925 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
21927 counter_u64_add(rack_out_size[idx], 1);
21930 if ((rack->rack_no_prr == 0) &&
21933 if (rack->r_ctl.rc_prr_sndcnt >= len)
21934 rack->r_ctl.rc_prr_sndcnt -= len;
21936 rack->r_ctl.rc_prr_sndcnt = 0;
21940 /* Make sure the TLP is added */
21941 add_flag |= RACK_TLP;
21943 /* If its a resend without TLP then it must not have the flag */
21944 rsm->r_flags &= ~RACK_TLP;
21948 if ((error == 0) &&
21950 (tp->snd_una == tp->snd_max))
21951 rack->r_ctl.rc_tlp_rxt_last_time = cts;
21953 tcp_seq startseq = tp->snd_nxt;
21955 /* Track our lost count */
21956 if (rsm && (doing_tlp == 0))
21957 rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
21959 * Advance snd_nxt over sequence space of this segment.
21962 /* We don't log or do anything with errors */
21964 if (doing_tlp == 0) {
21967 * Not a retransmission of some
21968 * sort, new data is going out so
21969 * clear our TLP count and flag.
21971 rack->rc_tlp_in_progress = 0;
21972 rack->r_ctl.rc_tlp_cnt_out = 0;
21976 * We have just sent a TLP, mark that it is true
21977 * and make sure our in progress is set so we
21978 * continue to check the count.
21980 rack->rc_tlp_in_progress = 1;
21981 rack->r_ctl.rc_tlp_cnt_out++;
21983 if (flags & (TH_SYN | TH_FIN)) {
21984 if (flags & TH_SYN)
21986 if (flags & TH_FIN) {
21988 tp->t_flags |= TF_SENTFIN;
21991 /* In the ENOBUFS case we do *not* update snd_max */
21995 tp->snd_nxt += len;
21996 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
21997 if (tp->snd_una == tp->snd_max) {
21999 * Update the time we just added data since
22000 * none was outstanding.
22002 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
22003 tp->t_acktime = ticks;
22005 tp->snd_max = tp->snd_nxt;
22006 if (rack->rc_new_rnd_needed) {
22008 * Update the rnd to start ticking not
22009 * that from a time perspective all of
22010 * the preceding idle time is "in the round"
22012 rack->rc_new_rnd_needed = 0;
22013 rack->r_ctl.roundends = tp->snd_max;
22016 * Time this transmission if not a retransmission and
22017 * not currently timing anything.
22018 * This is only relevant in case of switching back to
22021 if (tp->t_rtttime == 0) {
22022 tp->t_rtttime = ticks;
22023 tp->t_rtseq = startseq;
22024 KMOD_TCPSTAT_INC(tcps_segstimed);
22027 ((tp->t_flags & TF_GPUTINPROG) == 0))
22028 rack_start_gp_measurement(tp, rack, startseq, sb_offset);
22031 * If we are doing FO we need to update the mbuf position and subtract
22032 * this happens when the peer sends us duplicate information and
22033 * we thus want to send a DSACK.
22035 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO
22036 * turned off? If not then we are going to echo multiple DSACK blocks
22037 * out (with the TSO), which we should not be doing.
22039 if (rack->r_fast_output && len) {
22040 if (rack->r_ctl.fsb.left_to_send > len)
22041 rack->r_ctl.fsb.left_to_send -= len;
22043 rack->r_ctl.fsb.left_to_send = 0;
22044 if (rack->r_ctl.fsb.left_to_send < segsiz)
22045 rack->r_fast_output = 0;
22046 if (rack->r_fast_output) {
22047 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off);
22048 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len;
22049 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m);
22055 rack->r_ctl.rc_agg_delayed = 0;
22058 rack->r_ctl.rc_agg_early = 0;
22059 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */
22061 * Failures do not advance the seq counter above. For the
22062 * case of ENOBUFS we will fall out and retry in 1ms with
22063 * the hpts. Everything else will just have to retransmit
22066 * In any case, we do not want to loop around for another
22067 * send without a good reason.
22072 tp->t_softerror = error;
22073 #ifdef TCP_ACCOUNTING
22074 crtsc = get_cyclecount();
22075 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22076 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22078 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22079 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22086 * Pace us right away to retry in a some
22089 if (rack->r_ctl.crte != NULL) {
22090 tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF);
22091 if (tcp_bblogging_on(rack->rc_tp))
22092 rack_log_queue_level(tp, rack, len, &tv, cts);
22094 tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF);
22095 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
22096 if (rack->rc_enobuf < 0x7f)
22098 if (slot < (10 * HPTS_USEC_IN_MSEC))
22099 slot = 10 * HPTS_USEC_IN_MSEC;
22100 if (rack->r_ctl.crte != NULL) {
22101 counter_u64_add(rack_saw_enobuf_hw, 1);
22102 tcp_rl_log_enobuf(rack->r_ctl.crte);
22104 counter_u64_add(rack_saw_enobuf, 1);
22108 * For some reason the interface we used initially
22109 * to send segments changed to another or lowered
22110 * its MTU. If TSO was active we either got an
22111 * interface without TSO capabilits or TSO was
22112 * turned off. If we obtained mtu from ip_output()
22113 * then update it and try again.
22116 tp->t_flags &= ~TF_TSO;
22120 saved_mtu = tp->t_maxseg;
22121 tcp_mss_update(tp, -1, mtu, NULL, NULL);
22122 if (saved_mtu > tp->t_maxseg) {
22126 slot = 10 * HPTS_USEC_IN_MSEC;
22127 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22128 #ifdef TCP_ACCOUNTING
22129 crtsc = get_cyclecount();
22130 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22131 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22133 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22134 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22140 counter_u64_add(rack_saw_enetunreach, 1);
22144 if (TCPS_HAVERCVDSYN(tp->t_state)) {
22145 tp->t_softerror = error;
22149 slot = 10 * HPTS_USEC_IN_MSEC;
22150 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
22151 #ifdef TCP_ACCOUNTING
22152 crtsc = get_cyclecount();
22153 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22154 tp->tcp_cnt_counters[SND_OUT_FAIL]++;
22156 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22157 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val);
22164 rack->rc_enobuf = 0;
22165 if (IN_FASTRECOVERY(tp->t_flags) && rsm)
22166 rack->r_ctl.retran_during_recovery += len;
22168 KMOD_TCPSTAT_INC(tcps_sndtotal);
22171 * Data sent (as far as we can tell). If this advertises a larger
22172 * window than any other segment, then remember the size of the
22173 * advertised window. Any pending ACK has now been sent.
22175 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
22176 tp->rcv_adv = tp->rcv_nxt + recwin;
22178 tp->last_ack_sent = tp->rcv_nxt;
22179 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
22182 /* Do we need to turn off sendalot? */
22183 if (rack->r_ctl.rc_pace_max_segs &&
22184 (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
22185 /* We hit our max. */
22187 } else if ((rack->rc_user_set_max_segs) &&
22188 (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
22189 /* We hit the user defined max */
22193 if ((error == 0) && (flags & TH_FIN))
22194 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
22195 if (flags & TH_RST) {
22197 * We don't send again after sending a RST.
22202 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
22203 } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
22205 * Get our pacing rate, if an error
22206 * occurred in sending (ENOBUF) we would
22207 * hit the else if with slot preset. Other
22210 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
22213 (rsm->r_flags & RACK_HAS_SYN) == 0 &&
22214 rack->use_rack_rr) {
22215 /* Its a retransmit and we use the rack cheat? */
22217 (rack->rc_always_pace == 0) ||
22218 (rack->r_rr_config == 1)) {
22220 * We have no pacing set or we
22221 * are using old-style rack or
22222 * we are overridden to use the old 1ms pacing.
22224 slot = rack->r_ctl.rc_min_to;
22227 /* We have sent clear the flag */
22228 rack->r_ent_rec_ns = 0;
22229 if (rack->r_must_retran) {
22231 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start);
22232 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) {
22234 * We have retransmitted all.
22236 rack->r_must_retran = 0;
22237 rack->r_ctl.rc_out_at_rto = 0;
22239 } else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22241 * Sending new data will also kill
22244 rack->r_must_retran = 0;
22245 rack->r_ctl.rc_out_at_rto = 0;
22248 rack->r_ctl.fsb.recwin = recwin;
22249 if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) &&
22250 SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) {
22252 * We hit an RTO and now have past snd_max at the RTO
22253 * clear all the WAS flags.
22255 tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY);
22258 /* set the rack tcb into the slot N */
22259 if ((error == 0) &&
22261 ((flags & (TH_SYN|TH_FIN)) == 0) &&
22263 (tp->snd_nxt == tp->snd_max) &&
22265 (tp->rcv_numsacks == 0) &&
22266 rack->r_fsb_inited &&
22267 TCPS_HAVEESTABLISHED(tp->t_state) &&
22268 ((IN_RECOVERY(tp->t_flags)) == 0) &&
22269 (rack->r_must_retran == 0) &&
22270 ((tp->t_flags & TF_NEEDFIN) == 0) &&
22271 (len > 0) && (orig_len > 0) &&
22272 (orig_len > len) &&
22273 ((orig_len - len) >= segsiz) &&
22275 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22276 /* We can send at least one more MSS using our fsb */
22277 rack_setup_fast_output(tp, rack, sb, len, orig_len,
22278 segsiz, pace_max_seg, hw_tls, flags);
22280 rack->r_fast_output = 0;
22281 rack_log_fsb(rack, tp, so, flags,
22282 ipoptlen, orig_len, len, error,
22283 (rsm == NULL), optlen, __LINE__, 2);
22284 } else if (sendalot) {
22288 if ((error == 0) &&
22290 ((flags & (TH_SYN|TH_FIN)) == 0) &&
22293 (tp->rcv_numsacks == 0) &&
22294 (tp->snd_nxt == tp->snd_max) &&
22295 (rack->r_must_retran == 0) &&
22296 rack->r_fsb_inited &&
22297 TCPS_HAVEESTABLISHED(tp->t_state) &&
22298 ((IN_RECOVERY(tp->t_flags)) == 0) &&
22299 ((tp->t_flags & TF_NEEDFIN) == 0) &&
22300 (len > 0) && (orig_len > 0) &&
22301 (orig_len > len) &&
22302 ((orig_len - len) >= segsiz) &&
22304 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) {
22305 /* we can use fast_output for more */
22306 rack_setup_fast_output(tp, rack, sb, len, orig_len,
22307 segsiz, pace_max_seg, hw_tls, flags);
22308 if (rack->r_fast_output) {
22310 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error);
22320 /* Assure when we leave that snd_nxt will point to top */
22322 if (SEQ_GT(tp->snd_max, tp->snd_nxt))
22323 tp->snd_nxt = tp->snd_max;
22324 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
22325 #ifdef TCP_ACCOUNTING
22326 crtsc = get_cyclecount() - ts_val;
22327 if (tot_len_this_send) {
22328 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22329 tp->tcp_cnt_counters[SND_OUT_DATA]++;
22331 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22332 tp->tcp_proc_time[SND_OUT_DATA] += crtsc;
22334 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22335 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz);
22338 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22339 tp->tcp_cnt_counters[SND_OUT_ACK]++;
22341 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
22342 tp->tcp_proc_time[SND_OUT_ACK] += crtsc;
22347 if (error == ENOBUFS)
22353 rack_update_seg(struct tcp_rack *rack)
22357 orig_val = rack->r_ctl.rc_pace_max_segs;
22358 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL);
22359 if (orig_val != rack->r_ctl.rc_pace_max_segs)
22360 rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0);
22364 rack_mtu_change(struct tcpcb *tp)
22367 * The MSS may have changed
22369 struct tcp_rack *rack;
22370 struct rack_sendmap *rsm;
22372 rack = (struct tcp_rack *)tp->t_fb_ptr;
22373 if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) {
22375 * The MTU has changed we need to resend everything
22376 * since all we have sent is lost. We first fix
22377 * up the mtu though.
22379 rack_set_pace_segments(tp, rack, __LINE__, NULL);
22380 /* We treat this like a full retransmit timeout without the cwnd adjustment */
22381 rack_remxt_tmr(tp);
22382 rack->r_fast_output = 0;
22383 rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp,
22384 rack->r_ctl.rc_sacked);
22385 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max;
22386 rack->r_must_retran = 1;
22387 /* Mark all inflight to needing to be rxt'd */
22388 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
22389 rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG);
22392 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
22393 /* We don't use snd_nxt to retransmit */
22394 tp->snd_nxt = tp->snd_max;
22398 rack_set_dgp(struct tcp_rack *rack)
22400 /* pace_always=1 */
22401 if (rack->rc_always_pace == 0) {
22402 if (tcp_can_enable_pacing() == 0)
22406 rack->rc_always_pace = 1;
22407 rack->use_fixed_rate = 0;
22408 if (rack->gp_ready)
22409 rack_set_cc_pacing(rack);
22410 rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22411 rack->rack_attempt_hdwr_pace = 0;
22413 rack->full_size_rxt = 1;
22414 rack->shape_rxt_to_pacing_min = 0;
22416 rack->r_use_cmp_ack = 1;
22417 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) &&
22418 rack->r_use_cmp_ack)
22419 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22421 rack->rack_enable_scwnd = 1;
22423 rack->rc_gp_dyn_mul = 1;
22425 rack->r_ctl.rack_per_of_gp_ca = 100;
22427 rack->r_rr_config = 3;
22429 rack->r_ctl.rc_no_push_at_mrtt = 2;
22431 if (rack->r_cwnd_was_clamped == 0) {
22432 rack->rc_pace_to_cwnd = 1;
22434 rack->rc_pace_to_cwnd = 0;
22435 /* Reset all multipliers to 100.0 so just the measured bw */
22436 rack->r_ctl.rack_per_of_gp_ss = 100;
22437 rack->r_ctl.rack_per_of_gp_ca = 100;
22439 rack->rc_pace_fill_if_rttin_range = 0;
22440 rack->rtt_limit_mul = 0;
22442 rack->rack_no_prr = 1;
22444 rack->r_limit_scw = 1;
22446 rack->r_ctl.rack_per_of_gp_rec = 90;
22447 rack_client_buffer_level_set(rack);
22454 rack_set_profile(struct tcp_rack *rack, int prof)
22459 * Profile 1 is "standard" DGP. It ignores
22460 * client buffer level.
22462 rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL0;
22463 err = rack_set_dgp(rack);
22466 } else if (prof == 2) {
22468 * Profile 2 is DGP. Less aggressive with
22469 * respect to client buffer level.
22471 rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL1;
22472 err = rack_set_dgp(rack);
22475 } else if (prof == 3) {
22477 * Profile 3 is DGP. Even Less aggressive with
22478 * respect to client buffer level.
22480 rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL2;
22481 err = rack_set_dgp(rack);
22484 } else if (prof == 4) {
22486 * Profile 4 is DGP with the most responsiveness
22487 * to client buffer level.
22489 rack->r_ctl.rc_dgp_bl_agg = DGP_LEVEL3;
22490 err = rack_set_dgp(rack);
22493 } else if (prof == 0) {
22494 /* This changes things back to the default settings */
22496 rack->rc_hybrid_mode = 0;
22498 if (rack_fill_cw_state)
22499 rack->rc_pace_to_cwnd = 1;
22501 rack->rc_pace_to_cwnd = 0;
22502 if (rack->rc_always_pace) {
22503 tcp_decrement_paced_conn();
22504 rack_undo_cc_pacing(rack);
22505 rack->rc_always_pace = 0;
22507 if (rack_pace_every_seg && tcp_can_enable_pacing()) {
22508 rack->rc_always_pace = 1;
22509 if ((rack->gp_ready) && (rack->use_fixed_rate == 0))
22510 rack_set_cc_pacing(rack);
22512 rack->rc_always_pace = 0;
22513 if (rack_dsack_std_based & 0x1) {
22514 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */
22515 rack->rc_rack_tmr_std_based = 1;
22517 if (rack_dsack_std_based & 0x2) {
22518 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */
22519 rack->rc_rack_use_dsack = 1;
22521 if (rack_use_cmp_acks)
22522 rack->r_use_cmp_ack = 1;
22524 rack->r_use_cmp_ack = 0;
22525 if (rack_disable_prr)
22526 rack->rack_no_prr = 1;
22528 rack->rack_no_prr = 0;
22529 if (rack_gp_no_rec_chg)
22530 rack->rc_gp_no_rec_chg = 1;
22532 rack->rc_gp_no_rec_chg = 0;
22533 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) {
22534 rack->r_mbuf_queue = 1;
22535 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state))
22536 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP;
22537 rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22539 rack->r_mbuf_queue = 0;
22540 rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
22542 if (rack_enable_shared_cwnd)
22543 rack->rack_enable_scwnd = 1;
22545 rack->rack_enable_scwnd = 0;
22546 if (rack_do_dyn_mul) {
22547 /* When dynamic adjustment is on CA needs to start at 100% */
22548 rack->rc_gp_dyn_mul = 1;
22549 if (rack_do_dyn_mul >= 100)
22550 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
22552 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
22553 rack->rc_gp_dyn_mul = 0;
22555 rack->r_rr_config = 0;
22556 rack->r_ctl.rc_no_push_at_mrtt = 0;
22557 rack->rc_pace_to_cwnd = 0;
22558 rack->rc_pace_fill_if_rttin_range = 0;
22559 rack->rtt_limit_mul = 0;
22561 if (rack_enable_hw_pacing)
22562 rack->rack_hdw_pace_ena = 1;
22564 rack->rack_hdw_pace_ena = 0;
22565 if (rack_disable_prr)
22566 rack->rack_no_prr = 1;
22568 rack->rack_no_prr = 0;
22569 if (rack_limits_scwnd)
22570 rack->r_limit_scw = 1;
22572 rack->r_limit_scw = 0;
22573 rack_init_retransmit_value(rack, rack_rxt_controls);
22580 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval)
22582 struct deferred_opt_list *dol;
22584 dol = malloc(sizeof(struct deferred_opt_list),
22585 M_TCPFSB, M_NOWAIT|M_ZERO);
22588 * No space yikes -- fail out..
22592 dol->optname = sopt_name;
22593 dol->optval = loptval;
22594 TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next);
22599 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid)
22601 #ifdef TCP_REQUEST_TRK
22602 struct tcp_sendfile_track *sft;
22610 * If BB logging is not on we need to look at the DTL flag.
22611 * If its on already then those reasons override the DTL input.
22612 * We do this with any request, you can turn DTL on, but it does
22613 * not turn off at least from hybrid pacing requests.
22615 if (tcp_bblogging_on(rack->rc_tp) == 0) {
22616 if (hybrid->hybrid_flags & TCP_HYBRID_PACING_DTL) {
22617 /* Turn on BB point logging */
22618 tcp_set_bblog_state(rack->rc_tp, TCP_LOG_VIA_BBPOINTS,
22619 TCP_BBPOINT_REQ_LEVEL_LOGGING);
22622 /* Make sure no fixed rate is on */
22623 rack->use_fixed_rate = 0;
22624 rack->r_ctl.rc_fixed_pacing_rate_rec = 0;
22625 rack->r_ctl.rc_fixed_pacing_rate_ca = 0;
22626 rack->r_ctl.rc_fixed_pacing_rate_ss = 0;
22627 /* Now allocate or find our entry that will have these settings */
22628 sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0);
22630 rack->rc_tp->tcp_hybrid_error++;
22631 /* no space, where would it have gone? */
22632 seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc;
22633 rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0);
22636 /* The seq will be snd_una + everything in the buffer */
22637 seq = sft->start_seq;
22638 if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) {
22639 /* Disabling hybrid pacing */
22640 if (rack->rc_hybrid_mode) {
22641 rack_set_profile(rack, 0);
22642 rack->rc_tp->tcp_hybrid_stop++;
22644 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0);
22647 if (rack->dgp_on == 0) {
22649 * If we have not yet turned DGP on, do so
22650 * now setting pure DGP mode, no buffer level
22653 if ((err = rack_set_profile(rack, 1)) != 0){
22654 /* Failed to turn pacing on */
22655 rack->rc_tp->tcp_hybrid_error++;
22656 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0);
22660 /* Now set in our flags */
22661 sft->hybrid_flags = hybrid->hybrid_flags;
22662 if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR)
22663 sft->cspr = hybrid->cspr;
22666 if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS)
22667 sft->hint_maxseg = hybrid->hint_maxseg;
22669 sft->hint_maxseg = 0;
22670 rack->rc_hybrid_mode = 1;
22671 rack->rc_tp->tcp_hybrid_start++;
22672 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0);
22680 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
22681 uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid)
22684 struct epoch_tracker et;
22685 struct sockopt sopt;
22686 struct cc_newreno_opts opt;
22691 switch (sopt_name) {
22692 case TCP_RACK_SET_RXT_OPTIONS:
22693 if ((optval >= 0) && (optval <= 2)) {
22694 rack_init_retransmit_value(rack, optval);
22697 * You must send in 0, 1 or 2 all else is
22703 case TCP_RACK_DSACK_OPT:
22704 RACK_OPTS_INC(tcp_rack_dsack_opt);
22705 if (optval & 0x1) {
22706 rack->rc_rack_tmr_std_based = 1;
22708 rack->rc_rack_tmr_std_based = 0;
22710 if (optval & 0x2) {
22711 rack->rc_rack_use_dsack = 1;
22713 rack->rc_rack_use_dsack = 0;
22715 rack_log_dsack_event(rack, 5, __LINE__, 0, 0);
22717 case TCP_RACK_PACING_DIVISOR:
22718 RACK_OPTS_INC(tcp_rack_pacing_divisor);
22720 rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor;
22722 if (optval < RL_MIN_DIVISOR)
22723 rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR;
22725 rack->r_ctl.pace_len_divisor = optval;
22728 case TCP_RACK_HI_BETA:
22729 RACK_OPTS_INC(tcp_rack_hi_beta);
22731 rack->rack_hibeta = 1;
22733 rack->rack_hibeta = 0;
22735 case TCP_RACK_PACING_BETA:
22736 RACK_OPTS_INC(tcp_rack_beta);
22737 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
22738 /* This only works for newreno. */
22742 if (rack->rc_pacing_cc_set) {
22744 * Set them into the real CC module
22745 * whats in the rack pcb is the old values
22746 * to be used on restoral/
22748 sopt.sopt_dir = SOPT_SET;
22749 opt.name = CC_NEWRENO_BETA;
22751 if (CC_ALGO(tp)->ctl_output != NULL)
22752 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
22759 * Not pacing yet so set it into our local
22760 * rack pcb storage.
22762 rack->r_ctl.rc_saved_beta.beta = optval;
22765 case TCP_RACK_TIMER_SLOP:
22766 RACK_OPTS_INC(tcp_rack_timer_slop);
22767 rack->r_ctl.timer_slop = optval;
22768 if (rack->rc_tp->t_srtt) {
22770 * If we have an SRTT lets update t_rxtcur
22771 * to have the new slop.
22773 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp),
22774 rack_rto_min, rack_rto_max,
22775 rack->r_ctl.timer_slop);
22778 case TCP_RACK_PACING_BETA_ECN:
22779 RACK_OPTS_INC(tcp_rack_beta_ecn);
22780 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) {
22781 /* This only works for newreno. */
22785 if (rack->rc_pacing_cc_set) {
22787 * Set them into the real CC module
22788 * whats in the rack pcb is the old values
22789 * to be used on restoral/
22791 sopt.sopt_dir = SOPT_SET;
22792 opt.name = CC_NEWRENO_BETA_ECN;
22794 if (CC_ALGO(tp)->ctl_output != NULL)
22795 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt);
22800 * Not pacing yet so set it into our local
22801 * rack pcb storage.
22803 rack->r_ctl.rc_saved_beta.beta_ecn = optval;
22804 rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED;
22807 case TCP_DEFER_OPTIONS:
22808 RACK_OPTS_INC(tcp_defer_opt);
22810 if (rack->gp_ready) {
22815 rack->defer_options = 1;
22817 rack->defer_options = 0;
22819 case TCP_RACK_MEASURE_CNT:
22820 RACK_OPTS_INC(tcp_rack_measure_cnt);
22821 if (optval && (optval <= 0xff)) {
22822 rack->r_ctl.req_measurements = optval;
22826 case TCP_REC_ABC_VAL:
22827 RACK_OPTS_INC(tcp_rec_abc_val);
22829 rack->r_use_labc_for_rec = 1;
22831 rack->r_use_labc_for_rec = 0;
22833 case TCP_RACK_ABC_VAL:
22834 RACK_OPTS_INC(tcp_rack_abc_val);
22835 if ((optval > 0) && (optval < 255))
22836 rack->rc_labc = optval;
22840 case TCP_HDWR_UP_ONLY:
22841 RACK_OPTS_INC(tcp_pacing_up_only);
22843 rack->r_up_only = 1;
22845 rack->r_up_only = 0;
22847 case TCP_PACING_RATE_CAP:
22848 RACK_OPTS_INC(tcp_pacing_rate_cap);
22849 rack->r_ctl.bw_rate_cap = loptval;
22851 case TCP_HYBRID_PACING:
22852 if (hybrid == NULL) {
22856 error = process_hybrid_pacing(rack, hybrid);
22858 case TCP_RACK_PROFILE:
22859 RACK_OPTS_INC(tcp_profile);
22860 error = rack_set_profile(rack, optval);
22862 case TCP_USE_CMP_ACKS:
22863 RACK_OPTS_INC(tcp_use_cmp_acks);
22864 if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) {
22865 /* You can't turn it off once its on! */
22867 } else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) {
22868 rack->r_use_cmp_ack = 1;
22869 rack->r_mbuf_queue = 1;
22870 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22872 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state))
22873 tp->t_flags2 |= TF2_MBUF_ACKCMP;
22875 case TCP_SHARED_CWND_TIME_LIMIT:
22876 RACK_OPTS_INC(tcp_lscwnd);
22878 rack->r_limit_scw = 1;
22880 rack->r_limit_scw = 0;
22882 case TCP_RACK_DGP_IN_REC:
22883 RACK_OPTS_INC(tcp_dgp_in_rec);
22885 rack->r_ctl.full_dgp_in_rec = 1;
22887 rack->r_ctl.full_dgp_in_rec = 0;
22889 case TCP_RXT_CLAMP:
22890 RACK_OPTS_INC(tcp_rxt_clamp);
22891 rack_translate_clamp_value(rack, optval);
22893 case TCP_RACK_PACE_TO_FILL:
22894 RACK_OPTS_INC(tcp_fillcw);
22896 rack->rc_pace_to_cwnd = 0;
22898 rack->rc_pace_to_cwnd = 1;
22900 rack->r_fill_less_agg = 1;
22902 if ((optval >= rack_gp_rtt_maxmul) &&
22903 rack_gp_rtt_maxmul &&
22905 rack->rc_pace_fill_if_rttin_range = 1;
22906 rack->rtt_limit_mul = optval;
22908 rack->rc_pace_fill_if_rttin_range = 0;
22909 rack->rtt_limit_mul = 0;
22912 case TCP_RACK_NO_PUSH_AT_MAX:
22913 RACK_OPTS_INC(tcp_npush);
22915 rack->r_ctl.rc_no_push_at_mrtt = 0;
22916 else if (optval < 0xff)
22917 rack->r_ctl.rc_no_push_at_mrtt = optval;
22921 case TCP_SHARED_CWND_ENABLE:
22922 RACK_OPTS_INC(tcp_rack_scwnd);
22924 rack->rack_enable_scwnd = 0;
22926 rack->rack_enable_scwnd = 1;
22928 case TCP_RACK_MBUF_QUEUE:
22929 /* Now do we use the LRO mbuf-queue feature */
22930 RACK_OPTS_INC(tcp_rack_mbufq);
22931 if (optval || rack->r_use_cmp_ack)
22932 rack->r_mbuf_queue = 1;
22934 rack->r_mbuf_queue = 0;
22935 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
22936 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
22938 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
22940 case TCP_RACK_NONRXT_CFG_RATE:
22941 RACK_OPTS_INC(tcp_rack_cfg_rate);
22943 rack->rack_rec_nonrxt_use_cr = 0;
22945 rack->rack_rec_nonrxt_use_cr = 1;
22948 RACK_OPTS_INC(tcp_rack_noprr);
22950 rack->rack_no_prr = 0;
22951 else if (optval == 1)
22952 rack->rack_no_prr = 1;
22953 else if (optval == 2)
22954 rack->no_prr_addback = 1;
22958 case TCP_TIMELY_DYN_ADJ:
22959 RACK_OPTS_INC(tcp_timely_dyn);
22961 rack->rc_gp_dyn_mul = 0;
22963 rack->rc_gp_dyn_mul = 1;
22964 if (optval >= 100) {
22966 * If the user sets something 100 or more
22967 * its the gp_ca value.
22969 rack->r_ctl.rack_per_of_gp_ca = optval;
22973 case TCP_RACK_DO_DETECTION:
22974 RACK_OPTS_INC(tcp_rack_do_detection);
22976 rack->do_detection = 0;
22978 rack->do_detection = 1;
22980 case TCP_RACK_TLP_USE:
22981 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
22985 RACK_OPTS_INC(tcp_tlp_use);
22986 rack->rack_tlp_threshold_use = optval;
22988 case TCP_RACK_TLP_REDUCE:
22989 /* RACK TLP cwnd reduction (bool) */
22990 RACK_OPTS_INC(tcp_rack_tlp_reduce);
22991 rack->r_ctl.rc_tlp_cwnd_reduce = optval;
22993 /* Pacing related ones */
22994 case TCP_RACK_PACE_ALWAYS:
22996 * zero is old rack method, 1 is new
22997 * method using a pacing rate.
22999 RACK_OPTS_INC(tcp_rack_pace_always);
23001 if (rack->rc_always_pace) {
23004 } else if (tcp_can_enable_pacing()) {
23005 rack->rc_always_pace = 1;
23006 if ((rack->gp_ready) && (rack->use_fixed_rate == 0))
23007 rack_set_cc_pacing(rack);
23014 if (rack->rc_always_pace) {
23015 tcp_decrement_paced_conn();
23016 rack->rc_always_pace = 0;
23017 rack_undo_cc_pacing(rack);
23020 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack)
23021 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ;
23023 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ;
23024 /* A rate may be set irate or other, if so set seg size */
23025 rack_update_seg(rack);
23027 case TCP_BBR_RACK_INIT_RATE:
23028 RACK_OPTS_INC(tcp_initial_rate);
23030 /* Change from kbits per second to bytes per second */
23033 rack->r_ctl.init_rate = val;
23034 if (rack->rc_init_win != rack_default_init_window) {
23038 * Options don't always get applied
23039 * in the order you think. So in order
23040 * to assure we update a cwnd we need
23041 * to check and see if we are still
23042 * where we should raise the cwnd.
23044 win = rc_init_window(rack);
23045 if (SEQ_GT(tp->snd_max, tp->iss))
23046 snt = tp->snd_max - tp->iss;
23050 (tp->snd_cwnd < win))
23051 tp->snd_cwnd = win;
23053 if (rack->rc_always_pace)
23054 rack_update_seg(rack);
23056 case TCP_BBR_IWINTSO:
23057 RACK_OPTS_INC(tcp_initial_win);
23058 if (optval && (optval <= 0xff)) {
23061 rack->rc_init_win = optval;
23062 win = rc_init_window(rack);
23063 if (SEQ_GT(tp->snd_max, tp->iss))
23064 snt = tp->snd_max - tp->iss;
23069 rack->r_ctl.init_rate)) {
23071 * We are not past the initial window
23072 * and we have some bases for pacing,
23073 * so we need to possibly adjust up
23074 * the cwnd. Note even if we don't set
23075 * the cwnd, its still ok to raise the rc_init_win
23076 * which can be used coming out of idle when we
23077 * would have a rate.
23079 if (tp->snd_cwnd < win)
23080 tp->snd_cwnd = win;
23082 if (rack->rc_always_pace)
23083 rack_update_seg(rack);
23087 case TCP_RACK_FORCE_MSEG:
23088 RACK_OPTS_INC(tcp_rack_force_max_seg);
23090 rack->rc_force_max_seg = 1;
23092 rack->rc_force_max_seg = 0;
23094 case TCP_RACK_PACE_MIN_SEG:
23095 RACK_OPTS_INC(tcp_rack_min_seg);
23096 rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval);
23097 rack_set_pace_segments(tp, rack, __LINE__, NULL);
23099 case TCP_RACK_PACE_MAX_SEG:
23100 /* Max segments size in a pace in bytes */
23101 RACK_OPTS_INC(tcp_rack_max_seg);
23102 rack->rc_user_set_max_segs = optval;
23103 rack_set_pace_segments(tp, rack, __LINE__, NULL);
23105 case TCP_RACK_PACE_RATE_REC:
23106 /* Set the fixed pacing rate in Bytes per second ca */
23107 RACK_OPTS_INC(tcp_rack_pace_rate_rec);
23108 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23109 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23110 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23111 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23112 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23113 rack->use_fixed_rate = 1;
23114 if (rack->rc_always_pace && rack->gp_ready && rack->rack_hibeta)
23115 rack_set_cc_pacing(rack);
23116 rack_log_pacing_delay_calc(rack,
23117 rack->r_ctl.rc_fixed_pacing_rate_ss,
23118 rack->r_ctl.rc_fixed_pacing_rate_ca,
23119 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23123 case TCP_RACK_PACE_RATE_SS:
23124 /* Set the fixed pacing rate in Bytes per second ca */
23125 RACK_OPTS_INC(tcp_rack_pace_rate_ss);
23126 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23127 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
23128 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23129 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23130 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23131 rack->use_fixed_rate = 1;
23132 if (rack->rc_always_pace && rack->gp_ready && rack->rack_hibeta)
23133 rack_set_cc_pacing(rack);
23134 rack_log_pacing_delay_calc(rack,
23135 rack->r_ctl.rc_fixed_pacing_rate_ss,
23136 rack->r_ctl.rc_fixed_pacing_rate_ca,
23137 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23138 __LINE__, NULL, 0);
23141 case TCP_RACK_PACE_RATE_CA:
23142 /* Set the fixed pacing rate in Bytes per second ca */
23143 RACK_OPTS_INC(tcp_rack_pace_rate_ca);
23144 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
23145 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
23146 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
23147 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
23148 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
23149 rack->use_fixed_rate = 1;
23150 if (rack->rc_always_pace && rack->gp_ready && rack->rack_hibeta)
23151 rack_set_cc_pacing(rack);
23152 rack_log_pacing_delay_calc(rack,
23153 rack->r_ctl.rc_fixed_pacing_rate_ss,
23154 rack->r_ctl.rc_fixed_pacing_rate_ca,
23155 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
23156 __LINE__, NULL, 0);
23158 case TCP_RACK_GP_INCREASE_REC:
23159 RACK_OPTS_INC(tcp_gp_inc_rec);
23160 rack->r_ctl.rack_per_of_gp_rec = optval;
23161 rack_log_pacing_delay_calc(rack,
23162 rack->r_ctl.rack_per_of_gp_ss,
23163 rack->r_ctl.rack_per_of_gp_ca,
23164 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23165 __LINE__, NULL, 0);
23167 case TCP_RACK_GP_INCREASE_CA:
23168 RACK_OPTS_INC(tcp_gp_inc_ca);
23172 * We don't allow any reduction
23178 rack->r_ctl.rack_per_of_gp_ca = ca;
23179 rack_log_pacing_delay_calc(rack,
23180 rack->r_ctl.rack_per_of_gp_ss,
23181 rack->r_ctl.rack_per_of_gp_ca,
23182 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23183 __LINE__, NULL, 0);
23185 case TCP_RACK_GP_INCREASE_SS:
23186 RACK_OPTS_INC(tcp_gp_inc_ss);
23190 * We don't allow any reduction
23196 rack->r_ctl.rack_per_of_gp_ss = ss;
23197 rack_log_pacing_delay_calc(rack,
23198 rack->r_ctl.rack_per_of_gp_ss,
23199 rack->r_ctl.rack_per_of_gp_ca,
23200 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
23201 __LINE__, NULL, 0);
23203 case TCP_RACK_RR_CONF:
23204 RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
23205 if (optval && optval <= 3)
23206 rack->r_rr_config = optval;
23208 rack->r_rr_config = 0;
23210 case TCP_PACING_DND: /* URL:dnd */
23212 rack->rc_pace_dnd = 1;
23214 rack->rc_pace_dnd = 0;
23216 case TCP_HDWR_RATE_CAP:
23217 RACK_OPTS_INC(tcp_hdwr_rate_cap);
23219 if (rack->r_rack_hw_rate_caps == 0)
23220 rack->r_rack_hw_rate_caps = 1;
23224 rack->r_rack_hw_rate_caps = 0;
23227 case TCP_RACK_SPLIT_LIMIT:
23228 RACK_OPTS_INC(tcp_split_limit);
23229 rack->r_ctl.rc_split_limit = optval;
23231 case TCP_BBR_HDWR_PACE:
23232 RACK_OPTS_INC(tcp_hdwr_pacing);
23234 if (rack->rack_hdrw_pacing == 0) {
23235 rack->rack_hdw_pace_ena = 1;
23236 rack->rack_attempt_hdwr_pace = 0;
23240 rack->rack_hdw_pace_ena = 0;
23242 if (rack->r_ctl.crte != NULL) {
23243 rack->rack_hdrw_pacing = 0;
23244 rack->rack_attempt_hdwr_pace = 0;
23245 tcp_rel_pacing_rate(rack->r_ctl.crte, tp);
23246 rack->r_ctl.crte = NULL;
23251 /* End Pacing related ones */
23252 case TCP_RACK_PRR_SENDALOT:
23253 /* Allow PRR to send more than one seg */
23254 RACK_OPTS_INC(tcp_rack_prr_sendalot);
23255 rack->r_ctl.rc_prr_sendalot = optval;
23257 case TCP_RACK_MIN_TO:
23258 /* Minimum time between rack t-o's in ms */
23259 RACK_OPTS_INC(tcp_rack_min_to);
23260 rack->r_ctl.rc_min_to = optval;
23262 case TCP_RACK_EARLY_SEG:
23263 /* If early recovery max segments */
23264 RACK_OPTS_INC(tcp_rack_early_seg);
23265 rack->r_ctl.rc_early_recovery_segs = optval;
23267 case TCP_RACK_ENABLE_HYSTART:
23270 tp->t_ccv.flags |= CCF_HYSTART_ALLOWED;
23271 if (rack_do_hystart > RACK_HYSTART_ON)
23272 tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND;
23273 if (rack_do_hystart > RACK_HYSTART_ON_W_SC)
23274 tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH;
23276 tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH);
23280 case TCP_RACK_REORD_THRESH:
23281 /* RACK reorder threshold (shift amount) */
23282 RACK_OPTS_INC(tcp_rack_reord_thresh);
23283 if ((optval > 0) && (optval < 31))
23284 rack->r_ctl.rc_reorder_shift = optval;
23288 case TCP_RACK_REORD_FADE:
23289 /* Does reordering fade after ms time */
23290 RACK_OPTS_INC(tcp_rack_reord_fade);
23291 rack->r_ctl.rc_reorder_fade = optval;
23293 case TCP_RACK_TLP_THRESH:
23294 /* RACK TLP theshold i.e. srtt+(srtt/N) */
23295 RACK_OPTS_INC(tcp_rack_tlp_thresh);
23297 rack->r_ctl.rc_tlp_threshold = optval;
23301 case TCP_BBR_USE_RACK_RR:
23302 RACK_OPTS_INC(tcp_rack_rr);
23304 rack->use_rack_rr = 1;
23306 rack->use_rack_rr = 0;
23308 case TCP_RACK_PKT_DELAY:
23309 /* RACK added ms i.e. rack-rtt + reord + N */
23310 RACK_OPTS_INC(tcp_rack_pkt_delay);
23311 rack->r_ctl.rc_pkt_delay = optval;
23314 RACK_OPTS_INC(tcp_rack_delayed_ack);
23316 tp->t_delayed_ack = 0;
23318 tp->t_delayed_ack = 1;
23319 if (tp->t_flags & TF_DELACK) {
23320 tp->t_flags &= ~TF_DELACK;
23321 tp->t_flags |= TF_ACKNOW;
23322 NET_EPOCH_ENTER(et);
23324 NET_EPOCH_EXIT(et);
23328 case TCP_BBR_RACK_RTT_USE:
23329 RACK_OPTS_INC(tcp_rack_rtt_use);
23330 if ((optval != USE_RTT_HIGH) &&
23331 (optval != USE_RTT_LOW) &&
23332 (optval != USE_RTT_AVG))
23335 rack->r_ctl.rc_rate_sample_method = optval;
23337 case TCP_DATA_AFTER_CLOSE:
23338 RACK_OPTS_INC(tcp_data_after_close);
23340 rack->rc_allow_data_af_clo = 1;
23342 rack->rc_allow_data_af_clo = 0;
23347 tcp_log_socket_option(tp, sopt_name, optval, error);
23353 rack_apply_deferred_options(struct tcp_rack *rack)
23355 struct deferred_opt_list *dol, *sdol;
23358 TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) {
23359 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next);
23360 /* Disadvantage of deferal is you loose the error return */
23361 s_optval = (uint32_t)dol->optval;
23362 (void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL);
23363 free(dol, M_TCPDO);
23368 rack_hw_tls_change(struct tcpcb *tp, int chg)
23370 /* Update HW tls state */
23371 struct tcp_rack *rack;
23373 rack = (struct tcp_rack *)tp->t_fb_ptr;
23375 rack->r_ctl.fsb.hw_tls = 1;
23377 rack->r_ctl.fsb.hw_tls = 0;
23381 rack_pru_options(struct tcpcb *tp, int flags)
23383 if (flags & PRUS_OOB)
23384 return (EOPNOTSUPP);
23389 rack_wake_check(struct tcpcb *tp)
23391 struct tcp_rack *rack;
23395 rack = (struct tcp_rack *)tp->t_fb_ptr;
23396 if (rack->r_ctl.rc_hpts_flags) {
23397 cts = tcp_get_usecs(&tv);
23398 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){
23400 * Pacing timer is up, check if we are ready.
23402 if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to))
23404 } else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) {
23406 * A timer is up, check if we are ready.
23408 if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp))
23415 static struct tcp_function_block __tcp_rack = {
23416 .tfb_tcp_block_name = __XSTRING(STACKNAME),
23417 .tfb_tcp_output = rack_output,
23418 .tfb_do_queued_segments = ctf_do_queued_segments,
23419 .tfb_do_segment_nounlock = rack_do_segment_nounlock,
23420 .tfb_tcp_do_segment = rack_do_segment,
23421 .tfb_tcp_ctloutput = rack_ctloutput,
23422 .tfb_tcp_fb_init = rack_init,
23423 .tfb_tcp_fb_fini = rack_fini,
23424 .tfb_tcp_timer_stop_all = rack_stopall,
23425 .tfb_tcp_rexmit_tmr = rack_remxt_tmr,
23426 .tfb_tcp_handoff_ok = rack_handoff_ok,
23427 .tfb_tcp_mtu_chg = rack_mtu_change,
23428 .tfb_pru_options = rack_pru_options,
23429 .tfb_hwtls_change = rack_hw_tls_change,
23430 .tfb_chg_query = rack_chg_query,
23431 .tfb_switch_failed = rack_switch_failed,
23432 .tfb_early_wake_check = rack_wake_check,
23433 .tfb_compute_pipe = rack_compute_pipe,
23434 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP,
23438 * rack_ctloutput() must drop the inpcb lock before performing copyin on
23439 * socket option arguments. When it re-acquires the lock after the copy, it
23440 * has to revalidate that the connection is still valid for the socket
23444 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt)
23446 struct inpcb *inp = tptoinpcb(tp);
23450 struct tcp_rack *rack;
23451 struct tcp_hybrid_req hybrid;
23453 int32_t error = 0, optval;
23455 rack = (struct tcp_rack *)tp->t_fb_ptr;
23456 if (rack == NULL) {
23461 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr;
23464 switch (sopt->sopt_level) {
23467 MPASS(inp->inp_vflag & INP_IPV6PROTO);
23468 switch (sopt->sopt_name) {
23469 case IPV6_USE_MIN_MTU:
23470 tcp6_use_min_mtu(tp);
23478 switch (sopt->sopt_name) {
23481 * The DSCP codepoint has changed, update the fsb.
23483 ip->ip_tos = rack->rc_inp->inp_ip_tos;
23487 * The TTL has changed, update the fsb.
23489 ip->ip_ttl = rack->rc_inp->inp_ip_ttl;
23497 switch (sopt->sopt_name) {
23498 case SO_PEERPRIO: /* SC-URL:bs */
23499 /* Already read in and sanity checked in sosetopt(). */
23500 if (inp->inp_socket) {
23501 rack->client_bufferlvl = inp->inp_socket->so_peerprio;
23502 rack_client_buffer_level_set(rack);
23510 switch (sopt->sopt_name) {
23511 case TCP_RACK_TLP_REDUCE: /* URL:tlp_reduce */
23512 /* Pacing related ones */
23513 case TCP_RACK_PACE_ALWAYS: /* URL:pace_always */
23514 case TCP_BBR_RACK_INIT_RATE: /* URL:irate */
23515 case TCP_BBR_IWINTSO: /* URL:tso_iwin */
23516 case TCP_RACK_PACE_MIN_SEG: /* URL:pace_min_seg */
23517 case TCP_RACK_PACE_MAX_SEG: /* URL:pace_max_seg */
23518 case TCP_RACK_FORCE_MSEG: /* URL:force_max_seg */
23519 case TCP_RACK_PACE_RATE_CA: /* URL:pr_ca */
23520 case TCP_RACK_PACE_RATE_SS: /* URL:pr_ss*/
23521 case TCP_RACK_PACE_RATE_REC: /* URL:pr_rec */
23522 case TCP_RACK_GP_INCREASE_CA: /* URL:gp_inc_ca */
23523 case TCP_RACK_GP_INCREASE_SS: /* URL:gp_inc_ss */
23524 case TCP_RACK_GP_INCREASE_REC: /* URL:gp_inc_rec */
23525 case TCP_RACK_RR_CONF: /* URL:rrr_conf */
23526 case TCP_BBR_HDWR_PACE: /* URL:hdwrpace */
23527 case TCP_HDWR_RATE_CAP: /* URL:hdwrcap boolean */
23528 case TCP_PACING_RATE_CAP: /* URL:cap -- used by side-channel */
23529 case TCP_HDWR_UP_ONLY: /* URL:uponly -- hardware pacing boolean */
23530 case TCP_RACK_PACING_BETA: /* URL:pacing_beta */
23531 case TCP_RACK_PACING_BETA_ECN: /* URL:pacing_beta_ecn */
23532 case TCP_RACK_PACE_TO_FILL: /* URL:fillcw */
23533 case TCP_RACK_DGP_IN_REC: /* URL:dgpinrec */
23534 /* End pacing related */
23535 case TCP_RXT_CLAMP: /* URL:rxtclamp */
23536 case TCP_DELACK: /* URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */
23537 case TCP_RACK_PRR_SENDALOT: /* URL:prr_sendalot */
23538 case TCP_RACK_MIN_TO: /* URL:min_to */
23539 case TCP_RACK_EARLY_SEG: /* URL:early_seg */
23540 case TCP_RACK_REORD_THRESH: /* URL:reord_thresh */
23541 case TCP_RACK_REORD_FADE: /* URL:reord_fade */
23542 case TCP_RACK_TLP_THRESH: /* URL:tlp_thresh */
23543 case TCP_RACK_PKT_DELAY: /* URL:pkt_delay */
23544 case TCP_RACK_TLP_USE: /* URL:tlp_use */
23545 case TCP_BBR_RACK_RTT_USE: /* URL:rttuse */
23546 case TCP_BBR_USE_RACK_RR: /* URL:rackrr */
23547 case TCP_RACK_DO_DETECTION: /* URL:detect */
23548 case TCP_NO_PRR: /* URL:noprr */
23549 case TCP_TIMELY_DYN_ADJ: /* URL:dynamic */
23550 case TCP_DATA_AFTER_CLOSE: /* no URL */
23551 case TCP_RACK_NONRXT_CFG_RATE: /* URL:nonrxtcr */
23552 case TCP_SHARED_CWND_ENABLE: /* URL:scwnd */
23553 case TCP_RACK_MBUF_QUEUE: /* URL:mqueue */
23554 case TCP_RACK_NO_PUSH_AT_MAX: /* URL:npush */
23555 case TCP_SHARED_CWND_TIME_LIMIT: /* URL:lscwnd */
23556 case TCP_RACK_PROFILE: /* URL:profile */
23557 case TCP_HYBRID_PACING: /* URL:hybrid */
23558 case TCP_USE_CMP_ACKS: /* URL:cmpack */
23559 case TCP_RACK_ABC_VAL: /* URL:labc */
23560 case TCP_REC_ABC_VAL: /* URL:reclabc */
23561 case TCP_RACK_MEASURE_CNT: /* URL:measurecnt */
23562 case TCP_DEFER_OPTIONS: /* URL:defer */
23563 case TCP_RACK_DSACK_OPT: /* URL:dsack */
23564 case TCP_RACK_TIMER_SLOP: /* URL:timer_slop */
23565 case TCP_RACK_ENABLE_HYSTART: /* URL:hystart */
23566 case TCP_RACK_SET_RXT_OPTIONS: /* URL:rxtsz */
23567 case TCP_RACK_HI_BETA: /* URL:hibeta */
23568 case TCP_RACK_SPLIT_LIMIT: /* URL:split */
23569 case TCP_RACK_PACING_DIVISOR: /* URL:divisor */
23570 case TCP_PACING_DND: /* URL:dnd */
23574 /* Filter off all unknown options to the base stack */
23575 return (tcp_default_ctloutput(tp, sopt));
23585 if (sopt->sopt_name == TCP_PACING_RATE_CAP) {
23586 error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval));
23588 * We truncate it down to 32 bits for the socket-option trace this
23589 * means rates > 34Gbps won't show right, but thats probably ok.
23591 optval = (uint32_t)loptval;
23592 } else if (sopt->sopt_name == TCP_HYBRID_PACING) {
23593 error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid));
23595 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
23596 /* Save it in 64 bit form too */
23602 if (tp->t_fb != &__tcp_rack) {
23604 return (ENOPROTOOPT);
23606 if (rack->defer_options && (rack->gp_ready == 0) &&
23607 (sopt->sopt_name != TCP_DEFER_OPTIONS) &&
23608 (sopt->sopt_name != TCP_HYBRID_PACING) &&
23609 (sopt->sopt_name != TCP_RACK_PACING_BETA) &&
23610 (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) &&
23611 (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) &&
23612 (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) {
23613 /* Options are beind deferred */
23614 if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) {
23618 /* No memory to defer, fail */
23623 error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid);
23629 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti)
23632 INP_WLOCK_ASSERT(tptoinpcb(tp));
23633 bzero(ti, sizeof(*ti));
23635 ti->tcpi_state = tp->t_state;
23636 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
23637 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
23638 if (tp->t_flags & TF_SACK_PERMIT)
23639 ti->tcpi_options |= TCPI_OPT_SACK;
23640 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
23641 ti->tcpi_options |= TCPI_OPT_WSCALE;
23642 ti->tcpi_snd_wscale = tp->snd_scale;
23643 ti->tcpi_rcv_wscale = tp->rcv_scale;
23645 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
23646 ti->tcpi_options |= TCPI_OPT_ECN;
23647 if (tp->t_flags & TF_FASTOPEN)
23648 ti->tcpi_options |= TCPI_OPT_TFO;
23649 /* still kept in ticks is t_rcvtime */
23650 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
23651 /* Since we hold everything in precise useconds this is easy */
23652 ti->tcpi_rtt = tp->t_srtt;
23653 ti->tcpi_rttvar = tp->t_rttvar;
23654 ti->tcpi_rto = tp->t_rxtcur;
23655 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
23656 ti->tcpi_snd_cwnd = tp->snd_cwnd;
23658 * FreeBSD-specific extension fields for tcp_info.
23660 ti->tcpi_rcv_space = tp->rcv_wnd;
23661 ti->tcpi_rcv_nxt = tp->rcv_nxt;
23662 ti->tcpi_snd_wnd = tp->snd_wnd;
23663 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
23664 ti->tcpi_snd_nxt = tp->snd_nxt;
23665 ti->tcpi_snd_mss = tp->t_maxseg;
23666 ti->tcpi_rcv_mss = tp->t_maxseg;
23667 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
23668 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
23669 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
23670 ti->tcpi_total_tlp = tp->t_sndtlppack;
23671 ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte;
23672 #ifdef NETFLIX_STATS
23673 memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo));
23676 if (tp->t_flags & TF_TOE) {
23677 ti->tcpi_options |= TCPI_OPT_TOE;
23678 tcp_offload_tcp_info(tp, ti);
23684 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt)
23686 struct inpcb *inp = tptoinpcb(tp);
23687 struct tcp_rack *rack;
23688 int32_t error, optval;
23689 uint64_t val, loptval;
23690 struct tcp_info ti;
23692 * Because all our options are either boolean or an int, we can just
23693 * pull everything into optval and then unlock and copy. If we ever
23694 * add a option that is not a int, then this will have quite an
23695 * impact to this routine.
23698 rack = (struct tcp_rack *)tp->t_fb_ptr;
23699 if (rack == NULL) {
23703 switch (sopt->sopt_name) {
23705 /* First get the info filled */
23706 rack_fill_info(tp, &ti);
23707 /* Fix up the rtt related fields if needed */
23709 error = sooptcopyout(sopt, &ti, sizeof ti);
23712 * Beta is the congestion control value for NewReno that influences how
23713 * much of a backoff happens when loss is detected. It is normally set
23714 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value
23715 * when you exit recovery.
23717 case TCP_RACK_PACING_BETA:
23718 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
23720 else if (rack->rc_pacing_cc_set == 0)
23721 optval = rack->r_ctl.rc_saved_beta.beta;
23724 * Reach out into the CC data and report back what
23725 * I have previously set. Yeah it looks hackish but
23726 * we don't want to report the saved values.
23728 if (tp->t_ccv.cc_data)
23729 optval = ((struct newreno *)tp->t_ccv.cc_data)->beta;
23735 * Beta_ecn is the congestion control value for NewReno that influences how
23736 * much of a backoff happens when a ECN mark is detected. It is normally set
23737 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when
23738 * you exit recovery. Note that classic ECN has a beta of 50, it is only
23739 * ABE Ecn that uses this "less" value, but we do too with pacing :)
23742 case TCP_RACK_PACING_BETA_ECN:
23743 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0)
23745 else if (rack->rc_pacing_cc_set == 0)
23746 optval = rack->r_ctl.rc_saved_beta.beta_ecn;
23749 * Reach out into the CC data and report back what
23750 * I have previously set. Yeah it looks hackish but
23751 * we don't want to report the saved values.
23753 if (tp->t_ccv.cc_data)
23754 optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn;
23759 case TCP_RACK_DSACK_OPT:
23761 if (rack->rc_rack_tmr_std_based) {
23764 if (rack->rc_rack_use_dsack) {
23768 case TCP_RACK_ENABLE_HYSTART:
23770 if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) {
23771 optval = RACK_HYSTART_ON;
23772 if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND)
23773 optval = RACK_HYSTART_ON_W_SC;
23774 if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH)
23775 optval = RACK_HYSTART_ON_W_SC_C;
23777 optval = RACK_HYSTART_OFF;
23781 case TCP_RACK_DGP_IN_REC:
23782 optval = rack->r_ctl.full_dgp_in_rec;
23784 case TCP_RACK_HI_BETA:
23785 optval = rack->rack_hibeta;
23787 case TCP_RXT_CLAMP:
23788 optval = rack->r_ctl.saved_rxt_clamp_val;
23790 case TCP_DEFER_OPTIONS:
23791 optval = rack->defer_options;
23793 case TCP_RACK_MEASURE_CNT:
23794 optval = rack->r_ctl.req_measurements;
23796 case TCP_REC_ABC_VAL:
23797 optval = rack->r_use_labc_for_rec;
23799 case TCP_RACK_ABC_VAL:
23800 optval = rack->rc_labc;
23802 case TCP_HDWR_UP_ONLY:
23803 optval= rack->r_up_only;
23805 case TCP_PACING_RATE_CAP:
23806 loptval = rack->r_ctl.bw_rate_cap;
23808 case TCP_RACK_PROFILE:
23809 /* You cannot retrieve a profile, its write only */
23812 case TCP_HYBRID_PACING:
23813 /* You cannot retrieve hybrid pacing information, its write only */
23816 case TCP_USE_CMP_ACKS:
23817 optval = rack->r_use_cmp_ack;
23819 case TCP_RACK_PACE_TO_FILL:
23820 optval = rack->rc_pace_to_cwnd;
23821 if (optval && rack->r_fill_less_agg)
23824 case TCP_RACK_NO_PUSH_AT_MAX:
23825 optval = rack->r_ctl.rc_no_push_at_mrtt;
23827 case TCP_SHARED_CWND_ENABLE:
23828 optval = rack->rack_enable_scwnd;
23830 case TCP_RACK_NONRXT_CFG_RATE:
23831 optval = rack->rack_rec_nonrxt_use_cr;
23834 if (rack->rack_no_prr == 1)
23836 else if (rack->no_prr_addback == 1)
23841 case TCP_RACK_DO_DETECTION:
23842 optval = rack->do_detection;
23844 case TCP_RACK_MBUF_QUEUE:
23845 /* Now do we use the LRO mbuf-queue feature */
23846 optval = rack->r_mbuf_queue;
23848 case TCP_TIMELY_DYN_ADJ:
23849 optval = rack->rc_gp_dyn_mul;
23851 case TCP_BBR_IWINTSO:
23852 optval = rack->rc_init_win;
23854 case TCP_RACK_TLP_REDUCE:
23855 /* RACK TLP cwnd reduction (bool) */
23856 optval = rack->r_ctl.rc_tlp_cwnd_reduce;
23858 case TCP_BBR_RACK_INIT_RATE:
23859 val = rack->r_ctl.init_rate;
23860 /* convert to kbits per sec */
23863 optval = (uint32_t)val;
23865 case TCP_RACK_FORCE_MSEG:
23866 optval = rack->rc_force_max_seg;
23868 case TCP_RACK_PACE_MIN_SEG:
23869 optval = rack->r_ctl.rc_user_set_min_segs;
23871 case TCP_RACK_PACE_MAX_SEG:
23872 /* Max segments in a pace */
23873 optval = rack->rc_user_set_max_segs;
23875 case TCP_RACK_PACE_ALWAYS:
23876 /* Use the always pace method */
23877 optval = rack->rc_always_pace;
23879 case TCP_RACK_PRR_SENDALOT:
23880 /* Allow PRR to send more than one seg */
23881 optval = rack->r_ctl.rc_prr_sendalot;
23883 case TCP_RACK_MIN_TO:
23884 /* Minimum time between rack t-o's in ms */
23885 optval = rack->r_ctl.rc_min_to;
23887 case TCP_RACK_SPLIT_LIMIT:
23888 optval = rack->r_ctl.rc_split_limit;
23890 case TCP_RACK_EARLY_SEG:
23891 /* If early recovery max segments */
23892 optval = rack->r_ctl.rc_early_recovery_segs;
23894 case TCP_RACK_REORD_THRESH:
23895 /* RACK reorder threshold (shift amount) */
23896 optval = rack->r_ctl.rc_reorder_shift;
23898 case TCP_RACK_REORD_FADE:
23899 /* Does reordering fade after ms time */
23900 optval = rack->r_ctl.rc_reorder_fade;
23902 case TCP_BBR_USE_RACK_RR:
23903 /* Do we use the rack cheat for rxt */
23904 optval = rack->use_rack_rr;
23906 case TCP_RACK_RR_CONF:
23907 optval = rack->r_rr_config;
23909 case TCP_HDWR_RATE_CAP:
23910 optval = rack->r_rack_hw_rate_caps;
23912 case TCP_BBR_HDWR_PACE:
23913 optval = rack->rack_hdw_pace_ena;
23915 case TCP_RACK_TLP_THRESH:
23916 /* RACK TLP theshold i.e. srtt+(srtt/N) */
23917 optval = rack->r_ctl.rc_tlp_threshold;
23919 case TCP_RACK_PKT_DELAY:
23920 /* RACK added ms i.e. rack-rtt + reord + N */
23921 optval = rack->r_ctl.rc_pkt_delay;
23923 case TCP_RACK_TLP_USE:
23924 optval = rack->rack_tlp_threshold_use;
23926 case TCP_PACING_DND:
23927 optval = rack->rc_pace_dnd;
23929 case TCP_RACK_PACE_RATE_CA:
23930 optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
23932 case TCP_RACK_PACE_RATE_SS:
23933 optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
23935 case TCP_RACK_PACE_RATE_REC:
23936 optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
23938 case TCP_RACK_GP_INCREASE_SS:
23939 optval = rack->r_ctl.rack_per_of_gp_ca;
23941 case TCP_RACK_GP_INCREASE_CA:
23942 optval = rack->r_ctl.rack_per_of_gp_ss;
23944 case TCP_RACK_PACING_DIVISOR:
23945 optval = rack->r_ctl.pace_len_divisor;
23947 case TCP_BBR_RACK_RTT_USE:
23948 optval = rack->r_ctl.rc_rate_sample_method;
23951 optval = tp->t_delayed_ack;
23953 case TCP_DATA_AFTER_CLOSE:
23954 optval = rack->rc_allow_data_af_clo;
23956 case TCP_SHARED_CWND_TIME_LIMIT:
23957 optval = rack->r_limit_scw;
23959 case TCP_RACK_TIMER_SLOP:
23960 optval = rack->r_ctl.timer_slop;
23963 return (tcp_default_ctloutput(tp, sopt));
23968 if (TCP_PACING_RATE_CAP)
23969 error = sooptcopyout(sopt, &loptval, sizeof loptval);
23971 error = sooptcopyout(sopt, &optval, sizeof optval);
23977 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
23979 if (sopt->sopt_dir == SOPT_SET) {
23980 return (rack_set_sockopt(tp, sopt));
23981 } else if (sopt->sopt_dir == SOPT_GET) {
23982 return (rack_get_sockopt(tp, sopt));
23984 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
23988 static const char *rack_stack_names[] = {
23989 __XSTRING(STACKNAME),
23991 __XSTRING(STACKALIAS),
23996 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
23998 memset(mem, 0, size);
24003 rack_dtor(void *mem, int32_t size, void *arg)
24008 static bool rack_mod_inited = false;
24011 tcp_addrack(module_t mod, int32_t type, void *data)
24018 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
24019 sizeof(struct rack_sendmap),
24020 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
24022 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
24023 sizeof(struct tcp_rack),
24024 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
24026 sysctl_ctx_init(&rack_sysctl_ctx);
24027 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
24028 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
24031 __XSTRING(STACKALIAS),
24033 __XSTRING(STACKNAME),
24035 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
24037 if (rack_sysctl_root == NULL) {
24038 printf("Failed to add sysctl node\n");
24042 rack_init_sysctls();
24043 num_stacks = nitems(rack_stack_names);
24044 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
24045 rack_stack_names, &num_stacks);
24047 printf("Failed to register %s stack name for "
24048 "%s module\n", rack_stack_names[num_stacks],
24049 __XSTRING(MODNAME));
24050 sysctl_ctx_free(&rack_sysctl_ctx);
24052 uma_zdestroy(rack_zone);
24053 uma_zdestroy(rack_pcb_zone);
24054 rack_counter_destroy();
24055 printf("Failed to register rack module -- err:%d\n", err);
24058 tcp_lro_reg_mbufq();
24059 rack_mod_inited = true;
24062 err = deregister_tcp_functions(&__tcp_rack, true, false);
24065 err = deregister_tcp_functions(&__tcp_rack, false, true);
24068 if (rack_mod_inited) {
24069 uma_zdestroy(rack_zone);
24070 uma_zdestroy(rack_pcb_zone);
24071 sysctl_ctx_free(&rack_sysctl_ctx);
24072 rack_counter_destroy();
24073 rack_mod_inited = false;
24075 tcp_lro_dereg_mbufq();
24079 return (EOPNOTSUPP);
24084 static moduledata_t tcp_rack = {
24085 .name = __XSTRING(MODNAME),
24086 .evhand = tcp_addrack,
24090 MODULE_VERSION(MODNAME, 1);
24091 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
24092 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
24094 #endif /* #if !defined(INET) && !defined(INET6) */