1 /*-
2 * Copyright (c) 2016-2020 Netflix, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_ipsec.h"
33 #include "opt_tcpdebug.h"
34 #include "opt_ratelimit.h"
35 #include <sys/param.h>
36 #include <sys/arb.h>
37 #include <sys/module.h>
38 #include <sys/kernel.h>
39 #ifdef TCP_HHOOK
40 #include <sys/hhook.h>
41 #endif
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h> /* for proc0 declaration */
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #ifdef STATS
53 #include <sys/qmath.h>
54 #include <sys/tree.h>
55 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
56 #else
57 #include <sys/tree.h>
58 #endif
59 #include <sys/refcount.h>
60 #include <sys/queue.h>
61 #include <sys/tim_filter.h>
62 #include <sys/smp.h>
63 #include <sys/kthread.h>
64 #include <sys/kern_prefetch.h>
65 #include <sys/protosw.h>
66
67 #include <vm/uma.h>
68
69 #include <net/route.h>
70 #include <net/route/nhop.h>
71 #include <net/vnet.h>
72
73 #define TCPSTATES /* for logging */
74
75 #include <netinet/in.h>
76 #include <netinet/in_kdtrace.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
80 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
81 #include <netinet/ip_var.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/in6_pcb.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/tcp.h>
86 #define TCPOUTFLAGS
87 #include <netinet/tcp_fsm.h>
88 #include <netinet/tcp_log_buf.h>
89 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet/tcp_hpts.h>
93 #include <netinet/tcp_ratelimit.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_fastopen.h>
97 #include <netinet/tcp_lro.h>
98 #ifdef NETFLIX_SHARED_CWND
99 #include <netinet/tcp_shared_cwnd.h>
100 #endif
101 #ifdef TCPDEBUG
102 #include <netinet/tcp_debug.h>
103 #endif /* TCPDEBUG */
104 #ifdef TCP_OFFLOAD
105 #include <netinet/tcp_offload.h>
106 #endif
107 #ifdef INET6
108 #include <netinet6/tcp6_var.h>
109 #endif
110
111 #include <netipsec/ipsec_support.h>
112
113 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
114 #include <netipsec/ipsec.h>
115 #include <netipsec/ipsec6.h>
116 #endif /* IPSEC */
117
118 #include <netinet/udp.h>
119 #include <netinet/udp_var.h>
120 #include <machine/in_cksum.h>
121
122 #ifdef MAC
123 #include <security/mac/mac_framework.h>
124 #endif
125 #include "sack_filter.h"
126 #include "tcp_rack.h"
127 #include "rack_bbr_common.h"
128
129 uma_zone_t rack_zone;
130 uma_zone_t rack_pcb_zone;
131
132 #ifndef TICKS2SBT
133 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t)))
134 #endif
135
136 struct sysctl_ctx_list rack_sysctl_ctx;
137 struct sysctl_oid *rack_sysctl_root;
138
139 #define CUM_ACKED 1
140 #define SACKED 2
141
142 #ifdef FSTACK
143 #define MODNAME tcp_rack
144 #define STACKNAME rack
145 #endif
146
147 /*
148 * The RACK module incorporates a number of
149 * TCP ideas that have been put out into the IETF
150 * over the last few years:
151 * - Matt Mathis's Rate Halving which slowly drops
152 * the congestion window so that the ack clock can
153 * be maintained during a recovery.
154 * - Yuchung Cheng's RACK TCP (for which its named) that
155 * will stop us using the number of dup acks and instead
156 * use time as the gage of when we retransmit.
157 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
158 * of Dukkipati et.al.
159 * RACK depends on SACK, so if an endpoint arrives that
160 * cannot do SACK the state machine below will shuttle the
161 * connection back to using the "default" TCP stack that is
162 * in FreeBSD.
163 *
164 * To implement RACK the original TCP stack was first decomposed
165 * into a functional state machine with individual states
166 * for each of the possible TCP connection states. The do_segement
167 * functions role in life is to mandate the connection supports SACK
168 * initially and then assure that the RACK state matches the conenction
169 * state before calling the states do_segment function. Each
170 * state is simplified due to the fact that the original do_segment
171 * has been decomposed and we *know* what state we are in (no
172 * switches on the state) and all tests for SACK are gone. This
173 * greatly simplifies what each state does.
174 *
175 * TCP output is also over-written with a new version since it
176 * must maintain the new rack scoreboard.
177 *
178 */
179 static int32_t rack_tlp_thresh = 1;
180 static int32_t rack_tlp_limit = 2; /* No more than 2 TLPs w-out new data */
181 static int32_t rack_tlp_use_greater = 1;
182 static int32_t rack_reorder_thresh = 2;
183 static int32_t rack_reorder_fade = 60000; /* 0 - never fade, def 60,000
184 * - 60 seconds */
185 /* Attack threshold detections */
186 static uint32_t rack_highest_sack_thresh_seen = 0;
187 static uint32_t rack_highest_move_thresh_seen = 0;
188
189 static int32_t rack_pkt_delay = 1;
190 static int32_t rack_early_recovery = 1;
191 static int32_t rack_send_a_lot_in_prr = 1;
192 static int32_t rack_min_to = 1; /* Number of ms minimum timeout */
193 static int32_t rack_verbose_logging = 0;
194 static int32_t rack_ignore_data_after_close = 1;
195 static int32_t rack_enable_shared_cwnd = 0;
196 static int32_t rack_limits_scwnd = 1;
197 static int32_t rack_enable_mqueue_for_nonpaced = 0;
198 static int32_t rack_disable_prr = 0;
199 static int32_t use_rack_rr = 1;
200 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */
201 static int32_t rack_persist_min = 250; /* 250ms */
202 static int32_t rack_persist_max = 2000; /* 2 Second */
203 static int32_t rack_sack_not_required = 0; /* set to one to allow non-sack to use rack */
204 static int32_t rack_default_init_window = 0; /* Use system default */
205 static int32_t rack_limit_time_with_srtt = 0;
206 static int32_t rack_hw_pace_adjust = 0;
207 /*
208 * Currently regular tcp has a rto_min of 30ms
209 * the backoff goes 12 times so that ends up
210 * being a total of 122.850 seconds before a
211 * connection is killed.
212 */
213 static uint32_t rack_def_data_window = 20;
214 static uint32_t rack_goal_bdp = 2;
215 static uint32_t rack_min_srtts = 1;
216 static uint32_t rack_min_measure_usec = 0;
217 static int32_t rack_tlp_min = 10;
218 static int32_t rack_rto_min = 30; /* 30ms same as main freebsd */
219 static int32_t rack_rto_max = 4000; /* 4 seconds */
220 static const int32_t rack_free_cache = 2;
221 static int32_t rack_hptsi_segments = 40;
222 static int32_t rack_rate_sample_method = USE_RTT_LOW;
223 static int32_t rack_pace_every_seg = 0;
224 static int32_t rack_delayed_ack_time = 200; /* 200ms */
225 static int32_t rack_slot_reduction = 4;
226 static int32_t rack_wma_divisor = 8; /* For WMA calculation */
227 static int32_t rack_cwnd_block_ends_measure = 0;
228 static int32_t rack_rwnd_block_ends_measure = 0;
229
230 static int32_t rack_lower_cwnd_at_tlp = 0;
231 static int32_t rack_use_proportional_reduce = 0;
232 static int32_t rack_proportional_rate = 10;
233 static int32_t rack_tlp_max_resend = 2;
234 static int32_t rack_limited_retran = 0;
235 static int32_t rack_always_send_oldest = 0;
236 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
237
238 static uint16_t rack_per_of_gp_ss = 250; /* 250 % slow-start */
239 static uint16_t rack_per_of_gp_ca = 200; /* 200 % congestion-avoidance */
240 static uint16_t rack_per_of_gp_rec = 200; /* 200 % of bw */
241
242 /* Probertt */
243 static uint16_t rack_per_of_gp_probertt = 60; /* 60% of bw */
244 static uint16_t rack_per_of_gp_lowthresh = 40; /* 40% is bottom */
245 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */
246 static uint16_t rack_atexit_prtt_hbp = 130; /* Clamp to 130% on exit prtt if highly buffered path */
247 static uint16_t rack_atexit_prtt = 130; /* Clamp to 100% on exit prtt if non highly buffered path */
248
249 static uint32_t rack_max_drain_wait = 2; /* How man gp srtt's before we give up draining */
250 static uint32_t rack_must_drain = 1; /* How many GP srtt's we *must* wait */
251 static uint32_t rack_probertt_use_min_rtt_entry = 1; /* Use the min to calculate the goal else gp_srtt */
252 static uint32_t rack_probertt_use_min_rtt_exit = 0;
253 static uint32_t rack_probe_rtt_sets_cwnd = 0;
254 static uint32_t rack_probe_rtt_safety_val = 2000000; /* No more than 2 sec in probe-rtt */
255 static uint32_t rack_time_between_probertt = 9600000; /* 9.6 sec in us */
256 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0; /* How many srtt periods does probe-rtt last top fraction */
257 static uint32_t rack_probertt_gpsrtt_cnt_div = 0; /* How many srtt periods does probe-rtt last bottom fraction */
258 static uint32_t rack_min_probertt_hold = 200000; /* Equal to delayed ack time */
259 static uint32_t rack_probertt_filter_life = 10000000;
260 static uint32_t rack_probertt_lower_within = 10;
261 static uint32_t rack_min_rtt_movement = 250; /* Must move at least 250 useconds to count as a lowering */
262 static int32_t rack_pace_one_seg = 0; /* Shall we pace for less than 1.4Meg 1MSS at a time */
263 static int32_t rack_probertt_clear_is = 1;
264 static int32_t rack_max_drain_hbp = 1; /* Extra drain times gpsrtt for highly buffered paths */
265 static int32_t rack_hbp_thresh = 3; /* what is the divisor max_rtt/min_rtt to decided a hbp */
266
267 /* Part of pacing */
268 static int32_t rack_max_per_above = 30; /* When we go to increment stop if above 100+this% */
269
270 /* Timely information */
271 /* Combine these two gives the range of 'no change' to bw */
272 /* ie the up/down provide the upper and lower bound */
273 static int32_t rack_gp_per_bw_mul_up = 2; /* 2% */
274 static int32_t rack_gp_per_bw_mul_down = 4; /* 4% */
275 static int32_t rack_gp_rtt_maxmul = 3; /* 3 x maxmin */
276 static int32_t rack_gp_rtt_minmul = 1; /* minrtt + (minrtt/mindiv) is lower rtt */
277 static int32_t rack_gp_rtt_mindiv = 4; /* minrtt + (minrtt * minmul/mindiv) is lower rtt */
278 static int32_t rack_gp_decrease_per = 20; /* 20% decrease in multipler */
279 static int32_t rack_gp_increase_per = 2; /* 2% increase in multipler */
280 static int32_t rack_per_lower_bound = 50; /* Don't allow to drop below this multiplier */
281 static int32_t rack_per_upper_bound_ss = 0; /* Don't allow SS to grow above this */
282 static int32_t rack_per_upper_bound_ca = 0; /* Don't allow CA to grow above this */
283 static int32_t rack_do_dyn_mul = 0; /* Are the rack gp multipliers dynamic */
284 static int32_t rack_gp_no_rec_chg = 1; /* Prohibit recovery from reducing it's multiplier */
285 static int32_t rack_timely_dec_clear = 6; /* Do we clear decrement count at a value (6)? */
286 static int32_t rack_timely_max_push_rise = 3; /* One round of pushing */
287 static int32_t rack_timely_max_push_drop = 3; /* Three round of pushing */
288 static int32_t rack_timely_min_segs = 4; /* 4 segment minimum */
289 static int32_t rack_use_max_for_nobackoff = 0;
290 static int32_t rack_timely_int_timely_only = 0; /* do interim timely's only use the timely algo (no b/w changes)? */
291 static int32_t rack_timely_no_stopping = 0;
292 static int32_t rack_down_raise_thresh = 100;
293 static int32_t rack_req_segs = 1;
294
295 /* Weird delayed ack mode */
296 static int32_t rack_use_imac_dack = 0;
297 /* Rack specific counters */
298 counter_u64_t rack_badfr;
299 counter_u64_t rack_badfr_bytes;
300 counter_u64_t rack_rtm_prr_retran;
301 counter_u64_t rack_rtm_prr_newdata;
302 counter_u64_t rack_timestamp_mismatch;
303 counter_u64_t rack_reorder_seen;
304 counter_u64_t rack_paced_segments;
305 counter_u64_t rack_unpaced_segments;
306 counter_u64_t rack_calc_zero;
307 counter_u64_t rack_calc_nonzero;
308 counter_u64_t rack_saw_enobuf;
309 counter_u64_t rack_saw_enetunreach;
310 counter_u64_t rack_per_timer_hole;
311
312 /* Tail loss probe counters */
313 counter_u64_t rack_tlp_tot;
314 counter_u64_t rack_tlp_newdata;
315 counter_u64_t rack_tlp_retran;
316 counter_u64_t rack_tlp_retran_bytes;
317 counter_u64_t rack_tlp_retran_fail;
318 counter_u64_t rack_to_tot;
319 counter_u64_t rack_to_arm_rack;
320 counter_u64_t rack_to_arm_tlp;
321 counter_u64_t rack_to_alloc;
322 counter_u64_t rack_to_alloc_hard;
323 counter_u64_t rack_to_alloc_emerg;
324 counter_u64_t rack_to_alloc_limited;
325 counter_u64_t rack_alloc_limited_conns;
326 counter_u64_t rack_split_limited;
327
328 counter_u64_t rack_sack_proc_all;
329 counter_u64_t rack_sack_proc_short;
330 counter_u64_t rack_sack_proc_restart;
331 counter_u64_t rack_sack_attacks_detected;
332 counter_u64_t rack_sack_attacks_reversed;
333 counter_u64_t rack_sack_used_next_merge;
334 counter_u64_t rack_sack_splits;
335 counter_u64_t rack_sack_used_prev_merge;
336 counter_u64_t rack_sack_skipped_acked;
337 counter_u64_t rack_ack_total;
338 counter_u64_t rack_express_sack;
339 counter_u64_t rack_sack_total;
340 counter_u64_t rack_move_none;
341 counter_u64_t rack_move_some;
342
343 counter_u64_t rack_used_tlpmethod;
344 counter_u64_t rack_used_tlpmethod2;
345 counter_u64_t rack_enter_tlp_calc;
346 counter_u64_t rack_input_idle_reduces;
347 counter_u64_t rack_collapsed_win;
348 counter_u64_t rack_tlp_does_nada;
349 counter_u64_t rack_try_scwnd;
350
351 /* Temp CPU counters */
352 counter_u64_t rack_find_high;
353
354 counter_u64_t rack_progress_drops;
355 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
356 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
357
358 static void
359 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line);
360
361 static int
362 rack_process_ack(struct mbuf *m, struct tcphdr *th,
363 struct socket *so, struct tcpcb *tp, struct tcpopt *to,
364 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
365 static int
366 rack_process_data(struct mbuf *m, struct tcphdr *th,
367 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
368 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
369 static void
370 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
371 struct tcphdr *th, uint16_t nsegs, uint16_t type, int32_t recovery);
372 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
373 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack,
374 uint8_t limit_type);
375 static struct rack_sendmap *
376 rack_check_recovery_mode(struct tcpcb *tp,
377 uint32_t tsused);
378 static void
379 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th,
380 uint32_t type);
381 static void rack_counter_destroy(void);
382 static int
383 rack_ctloutput(struct socket *so, struct sockopt *sopt,
384 struct inpcb *inp, struct tcpcb *tp);
385 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
386 static void
387 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line);
388 static void
389 rack_do_segment(struct mbuf *m, struct tcphdr *th,
390 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
391 uint8_t iptos);
392 static void rack_dtor(void *mem, int32_t size, void *arg);
393 static void
394 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm,
395 uint32_t t, uint32_t cts);
396 static void
397 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
398 uint32_t flex1, uint32_t flex2,
399 uint32_t flex3, uint32_t flex4,
400 uint32_t flex5, uint32_t flex6,
401 uint16_t flex7, uint8_t mod);
402 static void
403 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
404 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line, struct rack_sendmap *rsm);
405 static struct rack_sendmap *
406 rack_find_high_nonack(struct tcp_rack *rack,
407 struct rack_sendmap *rsm);
408 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
409 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
410 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
411 static int
412 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
413 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
414 static void
415 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
416 tcp_seq th_ack, int line);
417 static uint32_t
418 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss);
419 static int32_t rack_handoff_ok(struct tcpcb *tp);
420 static int32_t rack_init(struct tcpcb *tp);
421 static void rack_init_sysctls(void);
422 static void
423 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
424 struct tcphdr *th);
425 static void
426 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
427 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts,
428 uint8_t pass, struct rack_sendmap *hintrsm, uint32_t us_cts);
429 static void
430 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
431 struct rack_sendmap *rsm);
432 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm);
433 static int32_t rack_output(struct tcpcb *tp);
434
435 static uint32_t
436 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
437 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
438 uint32_t cts, int *moved_two);
439 static void rack_post_recovery(struct tcpcb *tp, struct tcphdr *th);
440 static void rack_remxt_tmr(struct tcpcb *tp);
441 static int
442 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
443 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
444 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
445 static int32_t rack_stopall(struct tcpcb *tp);
446 static void
447 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
448 uint32_t delta);
449 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type);
450 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
451 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type);
452 static uint32_t
453 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
454 struct rack_sendmap *rsm, uint32_t ts, int32_t * lenp);
455 static void
456 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
457 struct rack_sendmap *rsm, uint32_t ts);
458 static int
459 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
460 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack);
461 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
462 static int
463 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
464 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
465 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
466 static int
467 rack_do_closing(struct mbuf *m, struct tcphdr *th,
468 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
469 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
470 static int
471 rack_do_established(struct mbuf *m, struct tcphdr *th,
472 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
473 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
474 static int
475 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
476 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
477 int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos);
478 static int
479 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
480 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
481 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
482 static int
483 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
484 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
485 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
486 static int
487 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
488 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
489 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
490 static int
491 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
492 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
493 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
494 static int
495 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
496 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
497 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos);
498 struct rack_sendmap *
499 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
500 uint32_t tsused);
501 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt,
502 uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt);
503 static void
504 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th);
505
506 int32_t rack_clear_counter=0;
507
508 static int
sysctl_rack_clear(SYSCTL_HANDLER_ARGS)509 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
510 {
511 uint32_t stat;
512 int32_t error;
513
514 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
515 if (error || req->newptr == NULL)
516 return error;
517
518 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
519 if (error)
520 return (error);
521 if (stat == 1) {
522 #ifdef INVARIANTS
523 printf("Clearing RACK counters\n");
524 #endif
525 counter_u64_zero(rack_badfr);
526 counter_u64_zero(rack_badfr_bytes);
527 counter_u64_zero(rack_rtm_prr_retran);
528 counter_u64_zero(rack_rtm_prr_newdata);
529 counter_u64_zero(rack_timestamp_mismatch);
530 counter_u64_zero(rack_reorder_seen);
531 counter_u64_zero(rack_tlp_tot);
532 counter_u64_zero(rack_tlp_newdata);
533 counter_u64_zero(rack_tlp_retran);
534 counter_u64_zero(rack_tlp_retran_bytes);
535 counter_u64_zero(rack_tlp_retran_fail);
536 counter_u64_zero(rack_to_tot);
537 counter_u64_zero(rack_to_arm_rack);
538 counter_u64_zero(rack_to_arm_tlp);
539 counter_u64_zero(rack_paced_segments);
540 counter_u64_zero(rack_calc_zero);
541 counter_u64_zero(rack_calc_nonzero);
542 counter_u64_zero(rack_unpaced_segments);
543 counter_u64_zero(rack_saw_enobuf);
544 counter_u64_zero(rack_saw_enetunreach);
545 counter_u64_zero(rack_per_timer_hole);
546 counter_u64_zero(rack_to_alloc_hard);
547 counter_u64_zero(rack_to_alloc_emerg);
548 counter_u64_zero(rack_sack_proc_all);
549 counter_u64_zero(rack_sack_proc_short);
550 counter_u64_zero(rack_sack_proc_restart);
551 counter_u64_zero(rack_to_alloc);
552 counter_u64_zero(rack_to_alloc_limited);
553 counter_u64_zero(rack_alloc_limited_conns);
554 counter_u64_zero(rack_split_limited);
555 counter_u64_zero(rack_find_high);
556 counter_u64_zero(rack_sack_attacks_detected);
557 counter_u64_zero(rack_sack_attacks_reversed);
558 counter_u64_zero(rack_sack_used_next_merge);
559 counter_u64_zero(rack_sack_used_prev_merge);
560 counter_u64_zero(rack_sack_splits);
561 counter_u64_zero(rack_sack_skipped_acked);
562 counter_u64_zero(rack_ack_total);
563 counter_u64_zero(rack_express_sack);
564 counter_u64_zero(rack_sack_total);
565 counter_u64_zero(rack_move_none);
566 counter_u64_zero(rack_move_some);
567 counter_u64_zero(rack_used_tlpmethod);
568 counter_u64_zero(rack_used_tlpmethod2);
569 counter_u64_zero(rack_enter_tlp_calc);
570 counter_u64_zero(rack_progress_drops);
571 counter_u64_zero(rack_tlp_does_nada);
572 counter_u64_zero(rack_try_scwnd);
573 counter_u64_zero(rack_collapsed_win);
574 }
575 rack_clear_counter = 0;
576 return (0);
577 }
578
579 static void
rack_init_sysctls(void)580 rack_init_sysctls(void)
581 {
582 struct sysctl_oid *rack_counters;
583 struct sysctl_oid *rack_attack;
584 struct sysctl_oid *rack_pacing;
585 struct sysctl_oid *rack_timely;
586 struct sysctl_oid *rack_timers;
587 struct sysctl_oid *rack_tlp;
588 struct sysctl_oid *rack_misc;
589 struct sysctl_oid *rack_measure;
590 struct sysctl_oid *rack_probertt;
591
592 rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
593 SYSCTL_CHILDREN(rack_sysctl_root),
594 OID_AUTO,
595 "sack_attack",
596 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
597 "Rack Sack Attack Counters and Controls");
598 rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
599 SYSCTL_CHILDREN(rack_sysctl_root),
600 OID_AUTO,
601 "stats",
602 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
603 "Rack Counters");
604 SYSCTL_ADD_S32(&rack_sysctl_ctx,
605 SYSCTL_CHILDREN(rack_sysctl_root),
606 OID_AUTO, "rate_sample_method", CTLFLAG_RW,
607 &rack_rate_sample_method , USE_RTT_LOW,
608 "What method should we use for rate sampling 0=high, 1=low ");
609 /* Probe rtt related controls */
610 rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
611 SYSCTL_CHILDREN(rack_sysctl_root),
612 OID_AUTO,
613 "probertt",
614 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
615 "ProbeRTT related Controls");
616 SYSCTL_ADD_U16(&rack_sysctl_ctx,
617 SYSCTL_CHILDREN(rack_probertt),
618 OID_AUTO, "exit_per_hpb", CTLFLAG_RW,
619 &rack_atexit_prtt_hbp, 130,
620 "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%");
621 SYSCTL_ADD_U16(&rack_sysctl_ctx,
622 SYSCTL_CHILDREN(rack_probertt),
623 OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW,
624 &rack_atexit_prtt, 130,
625 "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%");
626 SYSCTL_ADD_U16(&rack_sysctl_ctx,
627 SYSCTL_CHILDREN(rack_probertt),
628 OID_AUTO, "gp_per_mul", CTLFLAG_RW,
629 &rack_per_of_gp_probertt, 60,
630 "What percentage of goodput do we pace at in probertt");
631 SYSCTL_ADD_U16(&rack_sysctl_ctx,
632 SYSCTL_CHILDREN(rack_probertt),
633 OID_AUTO, "gp_per_reduce", CTLFLAG_RW,
634 &rack_per_of_gp_probertt_reduce, 10,
635 "What percentage of goodput do we reduce every gp_srtt");
636 SYSCTL_ADD_U16(&rack_sysctl_ctx,
637 SYSCTL_CHILDREN(rack_probertt),
638 OID_AUTO, "gp_per_low", CTLFLAG_RW,
639 &rack_per_of_gp_lowthresh, 40,
640 "What percentage of goodput do we allow the multiplier to fall to");
641 SYSCTL_ADD_U32(&rack_sysctl_ctx,
642 SYSCTL_CHILDREN(rack_probertt),
643 OID_AUTO, "time_between", CTLFLAG_RW,
644 & rack_time_between_probertt, 96000000,
645 "How many useconds between the lowest rtt falling must past before we enter probertt");
646 SYSCTL_ADD_U32(&rack_sysctl_ctx,
647 SYSCTL_CHILDREN(rack_probertt),
648 OID_AUTO, "safety", CTLFLAG_RW,
649 &rack_probe_rtt_safety_val, 2000000,
650 "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)");
651 SYSCTL_ADD_U32(&rack_sysctl_ctx,
652 SYSCTL_CHILDREN(rack_probertt),
653 OID_AUTO, "sets_cwnd", CTLFLAG_RW,
654 &rack_probe_rtt_sets_cwnd, 0,
655 "Do we set the cwnd too (if always_lower is on)");
656 SYSCTL_ADD_U32(&rack_sysctl_ctx,
657 SYSCTL_CHILDREN(rack_probertt),
658 OID_AUTO, "maxdrainsrtts", CTLFLAG_RW,
659 &rack_max_drain_wait, 2,
660 "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal");
661 SYSCTL_ADD_U32(&rack_sysctl_ctx,
662 SYSCTL_CHILDREN(rack_probertt),
663 OID_AUTO, "mustdrainsrtts", CTLFLAG_RW,
664 &rack_must_drain, 1,
665 "We must drain this many gp_srtt's waiting for flight to reach goal");
666 SYSCTL_ADD_U32(&rack_sysctl_ctx,
667 SYSCTL_CHILDREN(rack_probertt),
668 OID_AUTO, "goal_use_min_entry", CTLFLAG_RW,
669 &rack_probertt_use_min_rtt_entry, 1,
670 "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry");
671 SYSCTL_ADD_U32(&rack_sysctl_ctx,
672 SYSCTL_CHILDREN(rack_probertt),
673 OID_AUTO, "goal_use_min_exit", CTLFLAG_RW,
674 &rack_probertt_use_min_rtt_exit, 0,
675 "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt");
676 SYSCTL_ADD_U32(&rack_sysctl_ctx,
677 SYSCTL_CHILDREN(rack_probertt),
678 OID_AUTO, "length_div", CTLFLAG_RW,
679 &rack_probertt_gpsrtt_cnt_div, 0,
680 "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)");
681 SYSCTL_ADD_U32(&rack_sysctl_ctx,
682 SYSCTL_CHILDREN(rack_probertt),
683 OID_AUTO, "length_mul", CTLFLAG_RW,
684 &rack_probertt_gpsrtt_cnt_mul, 0,
685 "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)");
686 SYSCTL_ADD_U32(&rack_sysctl_ctx,
687 SYSCTL_CHILDREN(rack_probertt),
688 OID_AUTO, "holdtim_at_target", CTLFLAG_RW,
689 &rack_min_probertt_hold, 200000,
690 "What is the minimum time we hold probertt at target");
691 SYSCTL_ADD_U32(&rack_sysctl_ctx,
692 SYSCTL_CHILDREN(rack_probertt),
693 OID_AUTO, "filter_life", CTLFLAG_RW,
694 &rack_probertt_filter_life, 10000000,
695 "What is the time for the filters life in useconds");
696 SYSCTL_ADD_U32(&rack_sysctl_ctx,
697 SYSCTL_CHILDREN(rack_probertt),
698 OID_AUTO, "lower_within", CTLFLAG_RW,
699 &rack_probertt_lower_within, 10,
700 "If the rtt goes lower within this percentage of the time, go into probe-rtt");
701 SYSCTL_ADD_U32(&rack_sysctl_ctx,
702 SYSCTL_CHILDREN(rack_probertt),
703 OID_AUTO, "must_move", CTLFLAG_RW,
704 &rack_min_rtt_movement, 250,
705 "How much is the minimum movement in rtt to count as a drop for probertt purposes");
706 SYSCTL_ADD_U32(&rack_sysctl_ctx,
707 SYSCTL_CHILDREN(rack_probertt),
708 OID_AUTO, "clear_is_cnts", CTLFLAG_RW,
709 &rack_probertt_clear_is, 1,
710 "Do we clear I/S counts on exiting probe-rtt");
711 SYSCTL_ADD_S32(&rack_sysctl_ctx,
712 SYSCTL_CHILDREN(rack_probertt),
713 OID_AUTO, "hbp_extra_drain", CTLFLAG_RW,
714 &rack_max_drain_hbp, 1,
715 "How many extra drain gpsrtt's do we get in highly buffered paths");
716 SYSCTL_ADD_S32(&rack_sysctl_ctx,
717 SYSCTL_CHILDREN(rack_probertt),
718 OID_AUTO, "hbp_threshold", CTLFLAG_RW,
719 &rack_hbp_thresh, 3,
720 "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold");
721 /* Pacing related sysctls */
722 rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
723 SYSCTL_CHILDREN(rack_sysctl_root),
724 OID_AUTO,
725 "pacing",
726 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
727 "Pacing related Controls");
728 SYSCTL_ADD_S32(&rack_sysctl_ctx,
729 SYSCTL_CHILDREN(rack_pacing),
730 OID_AUTO, "max_pace_over", CTLFLAG_RW,
731 &rack_max_per_above, 30,
732 "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)");
733 SYSCTL_ADD_S32(&rack_sysctl_ctx,
734 SYSCTL_CHILDREN(rack_pacing),
735 OID_AUTO, "pace_to_one", CTLFLAG_RW,
736 &rack_pace_one_seg, 0,
737 "Do we allow low b/w pacing of 1MSS instead of two");
738 SYSCTL_ADD_S32(&rack_sysctl_ctx,
739 SYSCTL_CHILDREN(rack_pacing),
740 OID_AUTO, "limit_wsrtt", CTLFLAG_RW,
741 &rack_limit_time_with_srtt, 0,
742 "Do we limit pacing time based on srtt");
743 SYSCTL_ADD_S32(&rack_sysctl_ctx,
744 SYSCTL_CHILDREN(rack_pacing),
745 OID_AUTO, "init_win", CTLFLAG_RW,
746 &rack_default_init_window, 0,
747 "Do we have a rack initial window 0 = system default");
748 SYSCTL_ADD_U32(&rack_sysctl_ctx,
749 SYSCTL_CHILDREN(rack_pacing),
750 OID_AUTO, "hw_pacing_adjust", CTLFLAG_RW,
751 &rack_hw_pace_adjust, 0,
752 "What percentage do we raise the MSS by (11 = 1.1%)");
753 SYSCTL_ADD_U16(&rack_sysctl_ctx,
754 SYSCTL_CHILDREN(rack_pacing),
755 OID_AUTO, "gp_per_ss", CTLFLAG_RW,
756 &rack_per_of_gp_ss, 250,
757 "If non zero, what percentage of goodput to pace at in slow start");
758 SYSCTL_ADD_U16(&rack_sysctl_ctx,
759 SYSCTL_CHILDREN(rack_pacing),
760 OID_AUTO, "gp_per_ca", CTLFLAG_RW,
761 &rack_per_of_gp_ca, 150,
762 "If non zero, what percentage of goodput to pace at in congestion avoidance");
763 SYSCTL_ADD_U16(&rack_sysctl_ctx,
764 SYSCTL_CHILDREN(rack_pacing),
765 OID_AUTO, "gp_per_rec", CTLFLAG_RW,
766 &rack_per_of_gp_rec, 200,
767 "If non zero, what percentage of goodput to pace at in recovery");
768 SYSCTL_ADD_S32(&rack_sysctl_ctx,
769 SYSCTL_CHILDREN(rack_pacing),
770 OID_AUTO, "pace_max_seg", CTLFLAG_RW,
771 &rack_hptsi_segments, 40,
772 "What size is the max for TSO segments in pacing and burst mitigation");
773 SYSCTL_ADD_S32(&rack_sysctl_ctx,
774 SYSCTL_CHILDREN(rack_pacing),
775 OID_AUTO, "burst_reduces", CTLFLAG_RW,
776 &rack_slot_reduction, 4,
777 "When doing only burst mitigation what is the reduce divisor");
778 SYSCTL_ADD_S32(&rack_sysctl_ctx,
779 SYSCTL_CHILDREN(rack_sysctl_root),
780 OID_AUTO, "use_pacing", CTLFLAG_RW,
781 &rack_pace_every_seg, 0,
782 "If set we use pacing, if clear we use only the original burst mitigation");
783
784 rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
785 SYSCTL_CHILDREN(rack_sysctl_root),
786 OID_AUTO,
787 "timely",
788 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
789 "Rack Timely RTT Controls");
790 /* Timely based GP dynmics */
791 SYSCTL_ADD_S32(&rack_sysctl_ctx,
792 SYSCTL_CHILDREN(rack_timely),
793 OID_AUTO, "upper", CTLFLAG_RW,
794 &rack_gp_per_bw_mul_up, 2,
795 "Rack timely upper range for equal b/w (in percentage)");
796 SYSCTL_ADD_S32(&rack_sysctl_ctx,
797 SYSCTL_CHILDREN(rack_timely),
798 OID_AUTO, "lower", CTLFLAG_RW,
799 &rack_gp_per_bw_mul_down, 4,
800 "Rack timely lower range for equal b/w (in percentage)");
801 SYSCTL_ADD_S32(&rack_sysctl_ctx,
802 SYSCTL_CHILDREN(rack_timely),
803 OID_AUTO, "rtt_max_mul", CTLFLAG_RW,
804 &rack_gp_rtt_maxmul, 3,
805 "Rack timely multipler of lowest rtt for rtt_max");
806 SYSCTL_ADD_S32(&rack_sysctl_ctx,
807 SYSCTL_CHILDREN(rack_timely),
808 OID_AUTO, "rtt_min_div", CTLFLAG_RW,
809 &rack_gp_rtt_mindiv, 4,
810 "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt");
811 SYSCTL_ADD_S32(&rack_sysctl_ctx,
812 SYSCTL_CHILDREN(rack_timely),
813 OID_AUTO, "rtt_min_mul", CTLFLAG_RW,
814 &rack_gp_rtt_minmul, 1,
815 "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt");
816 SYSCTL_ADD_S32(&rack_sysctl_ctx,
817 SYSCTL_CHILDREN(rack_timely),
818 OID_AUTO, "decrease", CTLFLAG_RW,
819 &rack_gp_decrease_per, 20,
820 "Rack timely decrease percentage of our GP multiplication factor");
821 SYSCTL_ADD_S32(&rack_sysctl_ctx,
822 SYSCTL_CHILDREN(rack_timely),
823 OID_AUTO, "increase", CTLFLAG_RW,
824 &rack_gp_increase_per, 2,
825 "Rack timely increase perentage of our GP multiplication factor");
826 SYSCTL_ADD_S32(&rack_sysctl_ctx,
827 SYSCTL_CHILDREN(rack_timely),
828 OID_AUTO, "lowerbound", CTLFLAG_RW,
829 &rack_per_lower_bound, 50,
830 "Rack timely lowest percentage we allow GP multiplier to fall to");
831 SYSCTL_ADD_S32(&rack_sysctl_ctx,
832 SYSCTL_CHILDREN(rack_timely),
833 OID_AUTO, "upperboundss", CTLFLAG_RW,
834 &rack_per_upper_bound_ss, 0,
835 "Rack timely higest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)");
836 SYSCTL_ADD_S32(&rack_sysctl_ctx,
837 SYSCTL_CHILDREN(rack_timely),
838 OID_AUTO, "upperboundca", CTLFLAG_RW,
839 &rack_per_upper_bound_ca, 0,
840 "Rack timely higest percentage we allow GP multiplier to CA raise to (0 is no upperbound)");
841 SYSCTL_ADD_S32(&rack_sysctl_ctx,
842 SYSCTL_CHILDREN(rack_timely),
843 OID_AUTO, "dynamicgp", CTLFLAG_RW,
844 &rack_do_dyn_mul, 0,
845 "Rack timely do we enable dynmaic timely goodput by default");
846 SYSCTL_ADD_S32(&rack_sysctl_ctx,
847 SYSCTL_CHILDREN(rack_timely),
848 OID_AUTO, "no_rec_red", CTLFLAG_RW,
849 &rack_gp_no_rec_chg, 1,
850 "Rack timely do we prohibit the recovery multiplier from being lowered");
851 SYSCTL_ADD_S32(&rack_sysctl_ctx,
852 SYSCTL_CHILDREN(rack_timely),
853 OID_AUTO, "red_clear_cnt", CTLFLAG_RW,
854 &rack_timely_dec_clear, 6,
855 "Rack timely what threshold do we count to before another boost during b/w decent");
856 SYSCTL_ADD_S32(&rack_sysctl_ctx,
857 SYSCTL_CHILDREN(rack_timely),
858 OID_AUTO, "max_push_rise", CTLFLAG_RW,
859 &rack_timely_max_push_rise, 3,
860 "Rack timely how many times do we push up with b/w increase");
861 SYSCTL_ADD_S32(&rack_sysctl_ctx,
862 SYSCTL_CHILDREN(rack_timely),
863 OID_AUTO, "max_push_drop", CTLFLAG_RW,
864 &rack_timely_max_push_drop, 3,
865 "Rack timely how many times do we push back on b/w decent");
866 SYSCTL_ADD_S32(&rack_sysctl_ctx,
867 SYSCTL_CHILDREN(rack_timely),
868 OID_AUTO, "min_segs", CTLFLAG_RW,
869 &rack_timely_min_segs, 4,
870 "Rack timely when setting the cwnd what is the min num segments");
871 SYSCTL_ADD_S32(&rack_sysctl_ctx,
872 SYSCTL_CHILDREN(rack_timely),
873 OID_AUTO, "noback_max", CTLFLAG_RW,
874 &rack_use_max_for_nobackoff, 0,
875 "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min");
876 SYSCTL_ADD_S32(&rack_sysctl_ctx,
877 SYSCTL_CHILDREN(rack_timely),
878 OID_AUTO, "interim_timely_only", CTLFLAG_RW,
879 &rack_timely_int_timely_only, 0,
880 "Rack timely when doing interim timely's do we only do timely (no b/w consideration)");
881 SYSCTL_ADD_S32(&rack_sysctl_ctx,
882 SYSCTL_CHILDREN(rack_timely),
883 OID_AUTO, "nonstop", CTLFLAG_RW,
884 &rack_timely_no_stopping, 0,
885 "Rack timely don't stop increase");
886 SYSCTL_ADD_S32(&rack_sysctl_ctx,
887 SYSCTL_CHILDREN(rack_timely),
888 OID_AUTO, "dec_raise_thresh", CTLFLAG_RW,
889 &rack_down_raise_thresh, 100,
890 "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)");
891 SYSCTL_ADD_S32(&rack_sysctl_ctx,
892 SYSCTL_CHILDREN(rack_timely),
893 OID_AUTO, "bottom_drag_segs", CTLFLAG_RW,
894 &rack_req_segs, 1,
895 "Bottom dragging if not these many segments outstanding and room");
896
897 /* TLP and Rack related parameters */
898 rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
899 SYSCTL_CHILDREN(rack_sysctl_root),
900 OID_AUTO,
901 "tlp",
902 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
903 "TLP and Rack related Controls");
904 SYSCTL_ADD_S32(&rack_sysctl_ctx,
905 SYSCTL_CHILDREN(rack_tlp),
906 OID_AUTO, "use_rrr", CTLFLAG_RW,
907 &use_rack_rr, 1,
908 "Do we use Rack Rapid Recovery");
909 SYSCTL_ADD_S32(&rack_sysctl_ctx,
910 SYSCTL_CHILDREN(rack_tlp),
911 OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW,
912 &rack_non_rxt_use_cr, 0,
913 "Do we use ss/ca rate if in recovery we are transmitting a new data chunk");
914 SYSCTL_ADD_S32(&rack_sysctl_ctx,
915 SYSCTL_CHILDREN(rack_tlp),
916 OID_AUTO, "tlpmethod", CTLFLAG_RW,
917 &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
918 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
919 SYSCTL_ADD_S32(&rack_sysctl_ctx,
920 SYSCTL_CHILDREN(rack_tlp),
921 OID_AUTO, "limit", CTLFLAG_RW,
922 &rack_tlp_limit, 2,
923 "How many TLP's can be sent without sending new data");
924 SYSCTL_ADD_S32(&rack_sysctl_ctx,
925 SYSCTL_CHILDREN(rack_tlp),
926 OID_AUTO, "use_greater", CTLFLAG_RW,
927 &rack_tlp_use_greater, 1,
928 "Should we use the rack_rtt time if its greater than srtt");
929 SYSCTL_ADD_S32(&rack_sysctl_ctx,
930 SYSCTL_CHILDREN(rack_tlp),
931 OID_AUTO, "tlpminto", CTLFLAG_RW,
932 &rack_tlp_min, 10,
933 "TLP minimum timeout per the specification (10ms)");
934 SYSCTL_ADD_S32(&rack_sysctl_ctx,
935 SYSCTL_CHILDREN(rack_tlp),
936 OID_AUTO, "send_oldest", CTLFLAG_RW,
937 &rack_always_send_oldest, 0,
938 "Should we always send the oldest TLP and RACK-TLP");
939 SYSCTL_ADD_S32(&rack_sysctl_ctx,
940 SYSCTL_CHILDREN(rack_tlp),
941 OID_AUTO, "rack_tlimit", CTLFLAG_RW,
942 &rack_limited_retran, 0,
943 "How many times can a rack timeout drive out sends");
944 SYSCTL_ADD_S32(&rack_sysctl_ctx,
945 SYSCTL_CHILDREN(rack_tlp),
946 OID_AUTO, "tlp_retry", CTLFLAG_RW,
947 &rack_tlp_max_resend, 2,
948 "How many times does TLP retry a single segment or multiple with no ACK");
949 SYSCTL_ADD_S32(&rack_sysctl_ctx,
950 SYSCTL_CHILDREN(rack_tlp),
951 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
952 &rack_lower_cwnd_at_tlp, 0,
953 "When a TLP completes a retran should we enter recovery");
954 SYSCTL_ADD_S32(&rack_sysctl_ctx,
955 SYSCTL_CHILDREN(rack_tlp),
956 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
957 &rack_reorder_thresh, 2,
958 "What factor for rack will be added when seeing reordering (shift right)");
959 SYSCTL_ADD_S32(&rack_sysctl_ctx,
960 SYSCTL_CHILDREN(rack_tlp),
961 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
962 &rack_tlp_thresh, 1,
963 "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
964 SYSCTL_ADD_S32(&rack_sysctl_ctx,
965 SYSCTL_CHILDREN(rack_tlp),
966 OID_AUTO, "reorder_fade", CTLFLAG_RW,
967 &rack_reorder_fade, 0,
968 "Does reorder detection fade, if so how many ms (0 means never)");
969 SYSCTL_ADD_S32(&rack_sysctl_ctx,
970 SYSCTL_CHILDREN(rack_tlp),
971 OID_AUTO, "pktdelay", CTLFLAG_RW,
972 &rack_pkt_delay, 1,
973 "Extra RACK time (in ms) besides reordering thresh");
974
975 /* Timer related controls */
976 rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
977 SYSCTL_CHILDREN(rack_sysctl_root),
978 OID_AUTO,
979 "timers",
980 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
981 "Timer related controls");
982 SYSCTL_ADD_U32(&rack_sysctl_ctx,
983 SYSCTL_CHILDREN(rack_timers),
984 OID_AUTO, "persmin", CTLFLAG_RW,
985 &rack_persist_min, 250,
986 "What is the minimum time in milliseconds between persists");
987 SYSCTL_ADD_U32(&rack_sysctl_ctx,
988 SYSCTL_CHILDREN(rack_timers),
989 OID_AUTO, "persmax", CTLFLAG_RW,
990 &rack_persist_max, 2000,
991 "What is the largest delay in milliseconds between persists");
992 SYSCTL_ADD_S32(&rack_sysctl_ctx,
993 SYSCTL_CHILDREN(rack_timers),
994 OID_AUTO, "delayed_ack", CTLFLAG_RW,
995 &rack_delayed_ack_time, 200,
996 "Delayed ack time (200ms)");
997 SYSCTL_ADD_S32(&rack_sysctl_ctx,
998 SYSCTL_CHILDREN(rack_timers),
999 OID_AUTO, "minrto", CTLFLAG_RW,
1000 &rack_rto_min, 0,
1001 "Minimum RTO in ms -- set with caution below 1000 due to TLP");
1002 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1003 SYSCTL_CHILDREN(rack_timers),
1004 OID_AUTO, "maxrto", CTLFLAG_RW,
1005 &rack_rto_max, 0,
1006 "Maxiumum RTO in ms -- should be at least as large as min_rto");
1007 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1008 SYSCTL_CHILDREN(rack_timers),
1009 OID_AUTO, "minto", CTLFLAG_RW,
1010 &rack_min_to, 1,
1011 "Minimum rack timeout in milliseconds");
1012 /* Measure controls */
1013 rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1014 SYSCTL_CHILDREN(rack_sysctl_root),
1015 OID_AUTO,
1016 "measure",
1017 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1018 "Measure related controls");
1019 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1020 SYSCTL_CHILDREN(rack_measure),
1021 OID_AUTO, "wma_divisor", CTLFLAG_RW,
1022 &rack_wma_divisor, 8,
1023 "When doing b/w calculation what is the divisor for the WMA");
1024 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1025 SYSCTL_CHILDREN(rack_measure),
1026 OID_AUTO, "end_cwnd", CTLFLAG_RW,
1027 &rack_cwnd_block_ends_measure, 0,
1028 "Does a cwnd just-return end the measurement window (app limited)");
1029 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1030 SYSCTL_CHILDREN(rack_measure),
1031 OID_AUTO, "end_rwnd", CTLFLAG_RW,
1032 &rack_rwnd_block_ends_measure, 0,
1033 "Does an rwnd just-return end the measurement window (app limited -- not persists)");
1034 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1035 SYSCTL_CHILDREN(rack_measure),
1036 OID_AUTO, "min_target", CTLFLAG_RW,
1037 &rack_def_data_window, 20,
1038 "What is the minimum target window (in mss) for a GP measurements");
1039 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1040 SYSCTL_CHILDREN(rack_measure),
1041 OID_AUTO, "goal_bdp", CTLFLAG_RW,
1042 &rack_goal_bdp, 2,
1043 "What is the goal BDP to measure");
1044 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1045 SYSCTL_CHILDREN(rack_measure),
1046 OID_AUTO, "min_srtts", CTLFLAG_RW,
1047 &rack_min_srtts, 1,
1048 "What is the goal BDP to measure");
1049 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1050 SYSCTL_CHILDREN(rack_measure),
1051 OID_AUTO, "min_measure_tim", CTLFLAG_RW,
1052 &rack_min_measure_usec, 0,
1053 "What is the Minimum time time for a measurement if 0, this is off");
1054 /* Misc rack controls */
1055 rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
1056 SYSCTL_CHILDREN(rack_sysctl_root),
1057 OID_AUTO,
1058 "misc",
1059 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1060 "Misc related controls");
1061 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1062 SYSCTL_CHILDREN(rack_misc),
1063 OID_AUTO, "shared_cwnd", CTLFLAG_RW,
1064 &rack_enable_shared_cwnd, 0,
1065 "Should RACK try to use the shared cwnd on connections where allowed");
1066 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1067 SYSCTL_CHILDREN(rack_misc),
1068 OID_AUTO, "limits_on_scwnd", CTLFLAG_RW,
1069 &rack_limits_scwnd, 1,
1070 "Should RACK place low end time limits on the shared cwnd feature");
1071 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1072 SYSCTL_CHILDREN(rack_misc),
1073 OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW,
1074 &rack_enable_mqueue_for_nonpaced, 0,
1075 "Should RACK use mbuf queuing for non-paced connections");
1076 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1077 SYSCTL_CHILDREN(rack_misc),
1078 OID_AUTO, "iMac_dack", CTLFLAG_RW,
1079 &rack_use_imac_dack, 0,
1080 "Should RACK try to emulate iMac delayed ack");
1081 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1082 SYSCTL_CHILDREN(rack_misc),
1083 OID_AUTO, "no_prr", CTLFLAG_RW,
1084 &rack_disable_prr, 0,
1085 "Should RACK not use prr and only pace (must have pacing on)");
1086 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1087 SYSCTL_CHILDREN(rack_misc),
1088 OID_AUTO, "bb_verbose", CTLFLAG_RW,
1089 &rack_verbose_logging, 0,
1090 "Should RACK black box logging be verbose");
1091 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1092 SYSCTL_CHILDREN(rack_misc),
1093 OID_AUTO, "data_after_close", CTLFLAG_RW,
1094 &rack_ignore_data_after_close, 1,
1095 "Do we hold off sending a RST until all pending data is ack'd");
1096 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1097 SYSCTL_CHILDREN(rack_misc),
1098 OID_AUTO, "no_sack_needed", CTLFLAG_RW,
1099 &rack_sack_not_required, 0,
1100 "Do we allow rack to run on connections not supporting SACK");
1101 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1102 SYSCTL_CHILDREN(rack_misc),
1103 OID_AUTO, "recovery_loss_prop", CTLFLAG_RW,
1104 &rack_use_proportional_reduce, 0,
1105 "Should we proportionaly reduce cwnd based on the number of losses ");
1106 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1107 SYSCTL_CHILDREN(rack_misc),
1108 OID_AUTO, "recovery_prop", CTLFLAG_RW,
1109 &rack_proportional_rate, 10,
1110 "What percent reduction per loss");
1111 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1112 SYSCTL_CHILDREN(rack_misc),
1113 OID_AUTO, "prr_sendalot", CTLFLAG_RW,
1114 &rack_send_a_lot_in_prr, 1,
1115 "Send a lot in prr");
1116 SYSCTL_ADD_S32(&rack_sysctl_ctx,
1117 SYSCTL_CHILDREN(rack_misc),
1118 OID_AUTO, "earlyrecovery", CTLFLAG_RW,
1119 &rack_early_recovery, 1,
1120 "Do we do early recovery with rack");
1121 /* Sack Attacker detection stuff */
1122 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1123 SYSCTL_CHILDREN(rack_attack),
1124 OID_AUTO, "detect_highsackratio", CTLFLAG_RW,
1125 &rack_highest_sack_thresh_seen, 0,
1126 "Highest sack to ack ratio seen");
1127 SYSCTL_ADD_U32(&rack_sysctl_ctx,
1128 SYSCTL_CHILDREN(rack_attack),
1129 OID_AUTO, "detect_highmoveratio", CTLFLAG_RW,
1130 &rack_highest_move_thresh_seen, 0,
1131 "Highest move to non-move ratio seen");
1132 rack_ack_total = counter_u64_alloc(M_WAITOK);
1133 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1134 SYSCTL_CHILDREN(rack_attack),
1135 OID_AUTO, "acktotal", CTLFLAG_RD,
1136 &rack_ack_total,
1137 "Total number of Ack's");
1138 rack_express_sack = counter_u64_alloc(M_WAITOK);
1139 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1140 SYSCTL_CHILDREN(rack_attack),
1141 OID_AUTO, "exp_sacktotal", CTLFLAG_RD,
1142 &rack_express_sack,
1143 "Total expresss number of Sack's");
1144 rack_sack_total = counter_u64_alloc(M_WAITOK);
1145 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1146 SYSCTL_CHILDREN(rack_attack),
1147 OID_AUTO, "sacktotal", CTLFLAG_RD,
1148 &rack_sack_total,
1149 "Total number of SACKs");
1150 rack_move_none = counter_u64_alloc(M_WAITOK);
1151 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1152 SYSCTL_CHILDREN(rack_attack),
1153 OID_AUTO, "move_none", CTLFLAG_RD,
1154 &rack_move_none,
1155 "Total number of SACK index reuse of postions under threshold");
1156 rack_move_some = counter_u64_alloc(M_WAITOK);
1157 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1158 SYSCTL_CHILDREN(rack_attack),
1159 OID_AUTO, "move_some", CTLFLAG_RD,
1160 &rack_move_some,
1161 "Total number of SACK index reuse of postions over threshold");
1162 rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK);
1163 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1164 SYSCTL_CHILDREN(rack_attack),
1165 OID_AUTO, "attacks", CTLFLAG_RD,
1166 &rack_sack_attacks_detected,
1167 "Total number of SACK attackers that had sack disabled");
1168 rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK);
1169 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1170 SYSCTL_CHILDREN(rack_attack),
1171 OID_AUTO, "reversed", CTLFLAG_RD,
1172 &rack_sack_attacks_reversed,
1173 "Total number of SACK attackers that were later determined false positive");
1174 rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK);
1175 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1176 SYSCTL_CHILDREN(rack_attack),
1177 OID_AUTO, "nextmerge", CTLFLAG_RD,
1178 &rack_sack_used_next_merge,
1179 "Total number of times we used the next merge");
1180 rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK);
1181 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1182 SYSCTL_CHILDREN(rack_attack),
1183 OID_AUTO, "prevmerge", CTLFLAG_RD,
1184 &rack_sack_used_prev_merge,
1185 "Total number of times we used the prev merge");
1186 /* Counters */
1187 rack_badfr = counter_u64_alloc(M_WAITOK);
1188 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1189 SYSCTL_CHILDREN(rack_counters),
1190 OID_AUTO, "badfr", CTLFLAG_RD,
1191 &rack_badfr, "Total number of bad FRs");
1192 rack_badfr_bytes = counter_u64_alloc(M_WAITOK);
1193 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1194 SYSCTL_CHILDREN(rack_counters),
1195 OID_AUTO, "badfr_bytes", CTLFLAG_RD,
1196 &rack_badfr_bytes, "Total number of bad FRs");
1197 rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK);
1198 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1199 SYSCTL_CHILDREN(rack_counters),
1200 OID_AUTO, "prrsndret", CTLFLAG_RD,
1201 &rack_rtm_prr_retran,
1202 "Total number of prr based retransmits");
1203 rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK);
1204 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1205 SYSCTL_CHILDREN(rack_counters),
1206 OID_AUTO, "prrsndnew", CTLFLAG_RD,
1207 &rack_rtm_prr_newdata,
1208 "Total number of prr based new transmits");
1209 rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK);
1210 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1211 SYSCTL_CHILDREN(rack_counters),
1212 OID_AUTO, "tsnf", CTLFLAG_RD,
1213 &rack_timestamp_mismatch,
1214 "Total number of timestamps that we could not find the reported ts");
1215 rack_find_high = counter_u64_alloc(M_WAITOK);
1216 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1217 SYSCTL_CHILDREN(rack_counters),
1218 OID_AUTO, "findhigh", CTLFLAG_RD,
1219 &rack_find_high,
1220 "Total number of FIN causing find-high");
1221 rack_reorder_seen = counter_u64_alloc(M_WAITOK);
1222 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1223 SYSCTL_CHILDREN(rack_counters),
1224 OID_AUTO, "reordering", CTLFLAG_RD,
1225 &rack_reorder_seen,
1226 "Total number of times we added delay due to reordering");
1227 rack_tlp_tot = counter_u64_alloc(M_WAITOK);
1228 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1229 SYSCTL_CHILDREN(rack_counters),
1230 OID_AUTO, "tlp_to_total", CTLFLAG_RD,
1231 &rack_tlp_tot,
1232 "Total number of tail loss probe expirations");
1233 rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
1234 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1235 SYSCTL_CHILDREN(rack_counters),
1236 OID_AUTO, "tlp_new", CTLFLAG_RD,
1237 &rack_tlp_newdata,
1238 "Total number of tail loss probe sending new data");
1239 rack_tlp_retran = counter_u64_alloc(M_WAITOK);
1240 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1241 SYSCTL_CHILDREN(rack_counters),
1242 OID_AUTO, "tlp_retran", CTLFLAG_RD,
1243 &rack_tlp_retran,
1244 "Total number of tail loss probe sending retransmitted data");
1245 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
1246 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1247 SYSCTL_CHILDREN(rack_counters),
1248 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
1249 &rack_tlp_retran_bytes,
1250 "Total bytes of tail loss probe sending retransmitted data");
1251 rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK);
1252 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1253 SYSCTL_CHILDREN(rack_counters),
1254 OID_AUTO, "tlp_retran_fail", CTLFLAG_RD,
1255 &rack_tlp_retran_fail,
1256 "Total number of tail loss probe sending retransmitted data that failed (wait for t3)");
1257 rack_to_tot = counter_u64_alloc(M_WAITOK);
1258 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1259 SYSCTL_CHILDREN(rack_counters),
1260 OID_AUTO, "rack_to_tot", CTLFLAG_RD,
1261 &rack_to_tot,
1262 "Total number of times the rack to expired");
1263 rack_to_arm_rack = counter_u64_alloc(M_WAITOK);
1264 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1265 SYSCTL_CHILDREN(rack_counters),
1266 OID_AUTO, "arm_rack", CTLFLAG_RD,
1267 &rack_to_arm_rack,
1268 "Total number of times the rack timer armed");
1269 rack_to_arm_tlp = counter_u64_alloc(M_WAITOK);
1270 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1271 SYSCTL_CHILDREN(rack_counters),
1272 OID_AUTO, "arm_tlp", CTLFLAG_RD,
1273 &rack_to_arm_tlp,
1274 "Total number of times the tlp timer armed");
1275 rack_calc_zero = counter_u64_alloc(M_WAITOK);
1276 rack_calc_nonzero = counter_u64_alloc(M_WAITOK);
1277 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1278 SYSCTL_CHILDREN(rack_counters),
1279 OID_AUTO, "calc_zero", CTLFLAG_RD,
1280 &rack_calc_zero,
1281 "Total number of times pacing time worked out to zero");
1282 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1283 SYSCTL_CHILDREN(rack_counters),
1284 OID_AUTO, "calc_nonzero", CTLFLAG_RD,
1285 &rack_calc_nonzero,
1286 "Total number of times pacing time worked out to non-zero");
1287 rack_paced_segments = counter_u64_alloc(M_WAITOK);
1288 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1289 SYSCTL_CHILDREN(rack_counters),
1290 OID_AUTO, "paced", CTLFLAG_RD,
1291 &rack_paced_segments,
1292 "Total number of times a segment send caused hptsi");
1293 rack_unpaced_segments = counter_u64_alloc(M_WAITOK);
1294 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1295 SYSCTL_CHILDREN(rack_counters),
1296 OID_AUTO, "unpaced", CTLFLAG_RD,
1297 &rack_unpaced_segments,
1298 "Total number of times a segment did not cause hptsi");
1299 rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
1300 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1301 SYSCTL_CHILDREN(rack_counters),
1302 OID_AUTO, "saw_enobufs", CTLFLAG_RD,
1303 &rack_saw_enobuf,
1304 "Total number of times a segment did not cause hptsi");
1305 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
1306 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1307 SYSCTL_CHILDREN(rack_counters),
1308 OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
1309 &rack_saw_enetunreach,
1310 "Total number of times a segment did not cause hptsi");
1311 rack_to_alloc = counter_u64_alloc(M_WAITOK);
1312 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1313 SYSCTL_CHILDREN(rack_counters),
1314 OID_AUTO, "allocs", CTLFLAG_RD,
1315 &rack_to_alloc,
1316 "Total allocations of tracking structures");
1317 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
1318 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1319 SYSCTL_CHILDREN(rack_counters),
1320 OID_AUTO, "allochard", CTLFLAG_RD,
1321 &rack_to_alloc_hard,
1322 "Total allocations done with sleeping the hard way");
1323 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
1324 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1325 SYSCTL_CHILDREN(rack_counters),
1326 OID_AUTO, "allocemerg", CTLFLAG_RD,
1327 &rack_to_alloc_emerg,
1328 "Total allocations done from emergency cache");
1329 rack_to_alloc_limited = counter_u64_alloc(M_WAITOK);
1330 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1331 SYSCTL_CHILDREN(rack_counters),
1332 OID_AUTO, "alloc_limited", CTLFLAG_RD,
1333 &rack_to_alloc_limited,
1334 "Total allocations dropped due to limit");
1335 rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK);
1336 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1337 SYSCTL_CHILDREN(rack_counters),
1338 OID_AUTO, "alloc_limited_conns", CTLFLAG_RD,
1339 &rack_alloc_limited_conns,
1340 "Connections with allocations dropped due to limit");
1341 rack_split_limited = counter_u64_alloc(M_WAITOK);
1342 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1343 SYSCTL_CHILDREN(rack_counters),
1344 OID_AUTO, "split_limited", CTLFLAG_RD,
1345 &rack_split_limited,
1346 "Split allocations dropped due to limit");
1347 rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
1348 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1349 SYSCTL_CHILDREN(rack_counters),
1350 OID_AUTO, "sack_long", CTLFLAG_RD,
1351 &rack_sack_proc_all,
1352 "Total times we had to walk whole list for sack processing");
1353 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
1354 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1355 SYSCTL_CHILDREN(rack_counters),
1356 OID_AUTO, "sack_restart", CTLFLAG_RD,
1357 &rack_sack_proc_restart,
1358 "Total times we had to walk whole list due to a restart");
1359 rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
1360 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1361 SYSCTL_CHILDREN(rack_counters),
1362 OID_AUTO, "sack_short", CTLFLAG_RD,
1363 &rack_sack_proc_short,
1364 "Total times we took shortcut for sack processing");
1365 rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK);
1366 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1367 SYSCTL_CHILDREN(rack_counters),
1368 OID_AUTO, "tlp_calc_entered", CTLFLAG_RD,
1369 &rack_enter_tlp_calc,
1370 "Total times we called calc-tlp");
1371 rack_used_tlpmethod = counter_u64_alloc(M_WAITOK);
1372 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1373 SYSCTL_CHILDREN(rack_counters),
1374 OID_AUTO, "hit_tlp_method", CTLFLAG_RD,
1375 &rack_used_tlpmethod,
1376 "Total number of runt sacks");
1377 rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK);
1378 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1379 SYSCTL_CHILDREN(rack_counters),
1380 OID_AUTO, "hit_tlp_method2", CTLFLAG_RD,
1381 &rack_used_tlpmethod2,
1382 "Total number of times we hit TLP method 2");
1383 rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK);
1384 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1385 SYSCTL_CHILDREN(rack_attack),
1386 OID_AUTO, "skipacked", CTLFLAG_RD,
1387 &rack_sack_skipped_acked,
1388 "Total number of times we skipped previously sacked");
1389 rack_sack_splits = counter_u64_alloc(M_WAITOK);
1390 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1391 SYSCTL_CHILDREN(rack_attack),
1392 OID_AUTO, "ofsplit", CTLFLAG_RD,
1393 &rack_sack_splits,
1394 "Total number of times we did the old fashion tree split");
1395 rack_progress_drops = counter_u64_alloc(M_WAITOK);
1396 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1397 SYSCTL_CHILDREN(rack_counters),
1398 OID_AUTO, "prog_drops", CTLFLAG_RD,
1399 &rack_progress_drops,
1400 "Total number of progress drops");
1401 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
1402 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1403 SYSCTL_CHILDREN(rack_counters),
1404 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
1405 &rack_input_idle_reduces,
1406 "Total number of idle reductions on input");
1407 rack_collapsed_win = counter_u64_alloc(M_WAITOK);
1408 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1409 SYSCTL_CHILDREN(rack_counters),
1410 OID_AUTO, "collapsed_win", CTLFLAG_RD,
1411 &rack_collapsed_win,
1412 "Total number of collapsed windows");
1413 rack_tlp_does_nada = counter_u64_alloc(M_WAITOK);
1414 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1415 SYSCTL_CHILDREN(rack_counters),
1416 OID_AUTO, "tlp_nada", CTLFLAG_RD,
1417 &rack_tlp_does_nada,
1418 "Total number of nada tlp calls");
1419 rack_try_scwnd = counter_u64_alloc(M_WAITOK);
1420 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1421 SYSCTL_CHILDREN(rack_counters),
1422 OID_AUTO, "tried_scwnd", CTLFLAG_RD,
1423 &rack_try_scwnd,
1424 "Total number of scwnd attempts");
1425
1426 rack_per_timer_hole = counter_u64_alloc(M_WAITOK);
1427 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
1428 SYSCTL_CHILDREN(rack_counters),
1429 OID_AUTO, "timer_hole", CTLFLAG_RD,
1430 &rack_per_timer_hole,
1431 "Total persists start in timer hole");
1432 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
1433 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1434 OID_AUTO, "outsize", CTLFLAG_RD,
1435 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
1436 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
1437 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
1438 OID_AUTO, "opts", CTLFLAG_RD,
1439 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
1440 SYSCTL_ADD_PROC(&rack_sysctl_ctx,
1441 SYSCTL_CHILDREN(rack_sysctl_root),
1442 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1443 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
1444 }
1445
1446 static __inline int
rb_map_cmp(struct rack_sendmap * b,struct rack_sendmap * a)1447 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a)
1448 {
1449 if (SEQ_GEQ(b->r_start, a->r_start) &&
1450 SEQ_LT(b->r_start, a->r_end)) {
1451 /*
1452 * The entry b is within the
1453 * block a. i.e.:
1454 * a -- |-------------|
1455 * b -- |----|
1456 * <or>
1457 * b -- |------|
1458 * <or>
1459 * b -- |-----------|
1460 */
1461 return (0);
1462 } else if (SEQ_GEQ(b->r_start, a->r_end)) {
1463 /*
1464 * b falls as either the next
1465 * sequence block after a so a
1466 * is said to be smaller than b.
1467 * i.e:
1468 * a -- |------|
1469 * b -- |--------|
1470 * or
1471 * b -- |-----|
1472 */
1473 return (1);
1474 }
1475 /*
1476 * Whats left is where a is
1477 * larger than b. i.e:
1478 * a -- |-------|
1479 * b -- |---|
1480 * or even possibly
1481 * b -- |--------------|
1482 */
1483 return (-1);
1484 }
1485
1486 RB_PROTOTYPE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1487 RB_GENERATE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp);
1488
1489 static uint32_t
rc_init_window(struct tcp_rack * rack)1490 rc_init_window(struct tcp_rack *rack)
1491 {
1492 uint32_t win;
1493
1494 if (rack->rc_init_win == 0) {
1495 /*
1496 * Nothing set by the user, use the system stack
1497 * default.
1498 */
1499 return(tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)));
1500 }
1501 win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win;
1502 return(win);
1503 }
1504
1505 static uint64_t
rack_get_fixed_pacing_bw(struct tcp_rack * rack)1506 rack_get_fixed_pacing_bw(struct tcp_rack *rack)
1507 {
1508 if (IN_RECOVERY(rack->rc_tp->t_flags))
1509 return (rack->r_ctl.rc_fixed_pacing_rate_rec);
1510 else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1511 return (rack->r_ctl.rc_fixed_pacing_rate_ss);
1512 else
1513 return (rack->r_ctl.rc_fixed_pacing_rate_ca);
1514 }
1515
1516 static uint64_t
rack_get_bw(struct tcp_rack * rack)1517 rack_get_bw(struct tcp_rack *rack)
1518 {
1519 if (rack->use_fixed_rate) {
1520 /* Return the fixed pacing rate */
1521 return (rack_get_fixed_pacing_bw(rack));
1522 }
1523 if (rack->r_ctl.gp_bw == 0) {
1524 /*
1525 * We have yet no b/w measurement,
1526 * if we have a user set initial bw
1527 * return it. If we don't have that and
1528 * we have an srtt, use the tcp IW (10) to
1529 * calculate a fictional b/w over the SRTT
1530 * which is more or less a guess. Note
1531 * we don't use our IW from rack on purpose
1532 * so if we have like IW=30, we are not
1533 * calculating a "huge" b/w.
1534 */
1535 uint64_t bw, srtt;
1536 if (rack->r_ctl.init_rate)
1537 return (rack->r_ctl.init_rate);
1538
1539 /* Has the user set a max peak rate? */
1540 #ifdef NETFLIX_PEAKRATE
1541 if (rack->rc_tp->t_maxpeakrate)
1542 return (rack->rc_tp->t_maxpeakrate);
1543 #endif
1544 /* Ok lets come up with the IW guess, if we have a srtt */
1545 if (rack->rc_tp->t_srtt == 0) {
1546 /*
1547 * Go with old pacing method
1548 * i.e. burst mitigation only.
1549 */
1550 return (0);
1551 }
1552 /* Ok lets get the initial TCP win (not racks) */
1553 bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp));
1554 srtt = ((uint64_t)TICKS_2_USEC(rack->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
1555 bw *= (uint64_t)USECS_IN_SECOND;
1556 bw /= srtt;
1557 return (bw);
1558 } else {
1559 uint64_t bw;
1560
1561 if(rack->r_ctl.num_avg >= RACK_REQ_AVG) {
1562 /* Averaging is done, we can return the value */
1563 bw = rack->r_ctl.gp_bw;
1564 } else {
1565 /* Still doing initial average must calculate */
1566 bw = rack->r_ctl.gp_bw / rack->r_ctl.num_avg;
1567 }
1568 #ifdef NETFLIX_PEAKRATE
1569 if ((rack->rc_tp->t_maxpeakrate) &&
1570 (bw > rack->rc_tp->t_maxpeakrate)) {
1571 /* The user has set a peak rate to pace at
1572 * don't allow us to pace faster than that.
1573 */
1574 return (rack->rc_tp->t_maxpeakrate);
1575 }
1576 #endif
1577 return (bw);
1578 }
1579 }
1580
1581 static uint16_t
rack_get_output_gain(struct tcp_rack * rack,struct rack_sendmap * rsm)1582 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm)
1583 {
1584 if (rack->use_fixed_rate) {
1585 return (100);
1586 } else if (rack->in_probe_rtt && (rsm == NULL))
1587 return(rack->r_ctl.rack_per_of_gp_probertt);
1588 else if ((IN_RECOVERY(rack->rc_tp->t_flags) &&
1589 rack->r_ctl.rack_per_of_gp_rec)) {
1590 if (rsm) {
1591 /* a retransmission always use the recovery rate */
1592 return(rack->r_ctl.rack_per_of_gp_rec);
1593 } else if (rack->rack_rec_nonrxt_use_cr) {
1594 /* Directed to use the configured rate */
1595 goto configured_rate;
1596 } else if (rack->rack_no_prr &&
1597 (rack->r_ctl.rack_per_of_gp_rec > 100)) {
1598 /* No PRR, lets just use the b/w estimate only */
1599 return(100);
1600 } else {
1601 /*
1602 * Here we may have a non-retransmit but we
1603 * have no overrides, so just use the recovery
1604 * rate (prr is in effect).
1605 */
1606 return(rack->r_ctl.rack_per_of_gp_rec);
1607 }
1608 }
1609 configured_rate:
1610 /* For the configured rate we look at our cwnd vs the ssthresh */
1611 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh)
1612 return (rack->r_ctl.rack_per_of_gp_ss);
1613 else
1614 return(rack->r_ctl.rack_per_of_gp_ca);
1615 }
1616
1617 static uint64_t
rack_get_output_bw(struct tcp_rack * rack,uint64_t bw,struct rack_sendmap * rsm)1618 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm)
1619 {
1620 /*
1621 * We allow rack_per_of_gp_xx to dictate our bw rate we want.
1622 */
1623 uint64_t bw_est;
1624 uint64_t gain;
1625
1626 gain = (uint64_t)rack_get_output_gain(rack, rsm);
1627 bw_est = bw * gain;
1628 bw_est /= (uint64_t)100;
1629 /* Never fall below the minimum (def 64kbps) */
1630 if (bw_est < RACK_MIN_BW)
1631 bw_est = RACK_MIN_BW;
1632 return (bw_est);
1633 }
1634
1635 static void
rack_log_retran_reason(struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t tsused,uint32_t thresh,int mod)1636 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod)
1637 {
1638 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1639 union tcp_log_stackspecific log;
1640 struct timeval tv;
1641
1642 if ((mod != 1) && (rack_verbose_logging == 0)) {
1643 /*
1644 * We get 3 values currently for mod
1645 * 1 - We are retransmitting and this tells the reason.
1646 * 2 - We are clearing a dup-ack count.
1647 * 3 - We are incrementing a dup-ack count.
1648 *
1649 * The clear/increment are only logged
1650 * if you have BBverbose on.
1651 */
1652 return;
1653 }
1654 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1655 log.u_bbr.flex1 = tsused;
1656 log.u_bbr.flex2 = thresh;
1657 log.u_bbr.flex3 = rsm->r_flags;
1658 log.u_bbr.flex4 = rsm->r_dupack;
1659 log.u_bbr.flex5 = rsm->r_start;
1660 log.u_bbr.flex6 = rsm->r_end;
1661 log.u_bbr.flex8 = mod;
1662 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1663 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1664 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1665 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1666 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1667 &rack->rc_inp->inp_socket->so_rcv,
1668 &rack->rc_inp->inp_socket->so_snd,
1669 BBR_LOG_SETTINGS_CHG, 0,
1670 0, &log, false, &tv);
1671 }
1672 }
1673
1674 static void
rack_log_to_start(struct tcp_rack * rack,uint32_t cts,uint32_t to,int32_t slot,uint8_t which)1675 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
1676 {
1677 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1678 union tcp_log_stackspecific log;
1679 struct timeval tv;
1680
1681 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1682 log.u_bbr.flex1 = TICKS_2_MSEC(rack->rc_tp->t_srtt >> TCP_RTT_SHIFT);
1683 log.u_bbr.flex2 = to * 1000;
1684 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
1685 log.u_bbr.flex4 = slot;
1686 log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot;
1687 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
1688 log.u_bbr.flex7 = rack->rc_in_persist;
1689 log.u_bbr.flex8 = which;
1690 if (rack->rack_no_prr)
1691 log.u_bbr.pkts_out = 0;
1692 else
1693 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
1694 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1695 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1696 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1697 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1698 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1699 &rack->rc_inp->inp_socket->so_rcv,
1700 &rack->rc_inp->inp_socket->so_snd,
1701 BBR_LOG_TIMERSTAR, 0,
1702 0, &log, false, &tv);
1703 }
1704 }
1705
1706 static void
rack_log_to_event(struct tcp_rack * rack,int32_t to_num,struct rack_sendmap * rsm)1707 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm)
1708 {
1709 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1710 union tcp_log_stackspecific log;
1711 struct timeval tv;
1712
1713 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1714 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1715 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1716 log.u_bbr.flex8 = to_num;
1717 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
1718 log.u_bbr.flex2 = rack->rc_rack_rtt;
1719 if (rsm == NULL)
1720 log.u_bbr.flex3 = 0;
1721 else
1722 log.u_bbr.flex3 = rsm->r_end - rsm->r_start;
1723 if (rack->rack_no_prr)
1724 log.u_bbr.flex5 = 0;
1725 else
1726 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
1727 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1728 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1729 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1730 &rack->rc_inp->inp_socket->so_rcv,
1731 &rack->rc_inp->inp_socket->so_snd,
1732 BBR_LOG_RTO, 0,
1733 0, &log, false, &tv);
1734 }
1735 }
1736
1737 static void
rack_log_rtt_upd(struct tcpcb * tp,struct tcp_rack * rack,uint32_t t,uint32_t len,struct rack_sendmap * rsm,int conf)1738 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len,
1739 struct rack_sendmap *rsm, int conf)
1740 {
1741 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1742 union tcp_log_stackspecific log;
1743 struct timeval tv;
1744 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1745 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1746 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1747 log.u_bbr.flex1 = t;
1748 log.u_bbr.flex2 = len;
1749 log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt * HPTS_USEC_IN_MSEC;
1750 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest * HPTS_USEC_IN_MSEC;
1751 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest * HPTS_USEC_IN_MSEC;
1752 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_rtt_cnt;
1753 log.u_bbr.flex7 = conf;
1754 log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot * (uint64_t)HPTS_USEC_IN_MSEC;
1755 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
1756 if (rack->rack_no_prr)
1757 log.u_bbr.pkts_out = 0;
1758 else
1759 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt;
1760 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1761 log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtt;
1762 log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags;
1763 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1764 if (rsm) {
1765 log.u_bbr.pkt_epoch = rsm->r_start;
1766 log.u_bbr.lost = rsm->r_end;
1767 log.u_bbr.cwnd_gain = rsm->r_rtr_cnt;
1768 } else {
1769 /* Its a SYN */
1770 log.u_bbr.pkt_epoch = rack->rc_tp->iss;
1771 log.u_bbr.lost = 0;
1772 log.u_bbr.cwnd_gain = 0;
1773 }
1774 /* Write out general bits of interest rrs here */
1775 log.u_bbr.use_lt_bw = rack->rc_highly_buffered;
1776 log.u_bbr.use_lt_bw <<= 1;
1777 log.u_bbr.use_lt_bw |= rack->forced_ack;
1778 log.u_bbr.use_lt_bw <<= 1;
1779 log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul;
1780 log.u_bbr.use_lt_bw <<= 1;
1781 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
1782 log.u_bbr.use_lt_bw <<= 1;
1783 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
1784 log.u_bbr.use_lt_bw <<= 1;
1785 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set;
1786 log.u_bbr.use_lt_bw <<= 1;
1787 log.u_bbr.use_lt_bw |= rack->rc_gp_filled;
1788 log.u_bbr.use_lt_bw <<= 1;
1789 log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom;
1790 log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight;
1791 log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts;
1792 log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered;
1793 log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts;
1794 log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt;
1795 TCP_LOG_EVENTP(tp, NULL,
1796 &rack->rc_inp->inp_socket->so_rcv,
1797 &rack->rc_inp->inp_socket->so_snd,
1798 BBR_LOG_BBRRTT, 0,
1799 0, &log, false, &tv);
1800 }
1801 }
1802
1803 static void
rack_log_rtt_sample(struct tcp_rack * rack,uint32_t rtt)1804 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
1805 {
1806 /*
1807 * Log the rtt sample we are
1808 * applying to the srtt algorithm in
1809 * useconds.
1810 */
1811 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1812 union tcp_log_stackspecific log;
1813 struct timeval tv;
1814
1815 /* Convert our ms to a microsecond */
1816 memset(&log, 0, sizeof(log));
1817 log.u_bbr.flex1 = rtt * 1000;
1818 log.u_bbr.flex2 = rack->r_ctl.ack_count;
1819 log.u_bbr.flex3 = rack->r_ctl.sack_count;
1820 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
1821 log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra;
1822 log.u_bbr.flex8 = rack->sack_attack_disable;
1823 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1824 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1825 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1826 &rack->rc_inp->inp_socket->so_rcv,
1827 &rack->rc_inp->inp_socket->so_snd,
1828 TCP_LOG_RTT, 0,
1829 0, &log, false, &tv);
1830 }
1831 }
1832
1833 static inline void
rack_log_progress_event(struct tcp_rack * rack,struct tcpcb * tp,uint32_t tick,int event,int line)1834 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line)
1835 {
1836 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
1837 union tcp_log_stackspecific log;
1838 struct timeval tv;
1839
1840 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1841 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1842 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1843 log.u_bbr.flex1 = line;
1844 log.u_bbr.flex2 = tick;
1845 log.u_bbr.flex3 = tp->t_maxunacktime;
1846 log.u_bbr.flex4 = tp->t_acktime;
1847 log.u_bbr.flex8 = event;
1848 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1849 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1850 TCP_LOG_EVENTP(tp, NULL,
1851 &rack->rc_inp->inp_socket->so_rcv,
1852 &rack->rc_inp->inp_socket->so_snd,
1853 BBR_LOG_PROGRESS, 0,
1854 0, &log, false, &tv);
1855 }
1856 }
1857
1858 static void
rack_log_type_bbrsnd(struct tcp_rack * rack,uint32_t len,uint32_t slot,uint32_t cts,struct timeval * tv)1859 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv)
1860 {
1861 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1862 union tcp_log_stackspecific log;
1863
1864 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1865 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1866 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1867 log.u_bbr.flex1 = slot;
1868 if (rack->rack_no_prr)
1869 log.u_bbr.flex2 = 0;
1870 else
1871 log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt;
1872 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
1873 log.u_bbr.flex8 = rack->rc_in_persist;
1874 log.u_bbr.timeStamp = cts;
1875 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1876 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1877 &rack->rc_inp->inp_socket->so_rcv,
1878 &rack->rc_inp->inp_socket->so_snd,
1879 BBR_LOG_BBRSND, 0,
1880 0, &log, false, tv);
1881 }
1882 }
1883
1884 static void
rack_log_doseg_done(struct tcp_rack * rack,uint32_t cts,int32_t nxt_pkt,int32_t did_out,int way_out)1885 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out)
1886 {
1887 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1888 union tcp_log_stackspecific log;
1889 struct timeval tv;
1890
1891 memset(&log, 0, sizeof(log));
1892 log.u_bbr.flex1 = did_out;
1893 log.u_bbr.flex2 = nxt_pkt;
1894 log.u_bbr.flex3 = way_out;
1895 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
1896 if (rack->rack_no_prr)
1897 log.u_bbr.flex5 = 0;
1898 else
1899 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
1900 log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs;
1901 log.u_bbr.flex7 = rack->r_wanted_output;
1902 log.u_bbr.flex8 = rack->rc_in_persist;
1903 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1904 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1905 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1906 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1907 &rack->rc_inp->inp_socket->so_rcv,
1908 &rack->rc_inp->inp_socket->so_snd,
1909 BBR_LOG_DOSEG_DONE, 0,
1910 0, &log, false, &tv);
1911 }
1912 }
1913
1914 static void
rack_log_type_hrdwtso(struct tcpcb * tp,struct tcp_rack * rack,int len,int mod,int32_t orig_len,int frm)1915 rack_log_type_hrdwtso(struct tcpcb *tp, struct tcp_rack *rack, int len, int mod, int32_t orig_len, int frm)
1916 {
1917 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1918 union tcp_log_stackspecific log;
1919 struct timeval tv;
1920 uint32_t cts;
1921
1922 memset(&log, 0, sizeof(log));
1923 cts = tcp_get_usecs(&tv);
1924 log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs;
1925 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
1926 log.u_bbr.flex4 = len;
1927 log.u_bbr.flex5 = orig_len;
1928 log.u_bbr.flex6 = rack->r_ctl.rc_sacked;
1929 log.u_bbr.flex7 = mod;
1930 log.u_bbr.flex8 = frm;
1931 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1932 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1933 TCP_LOG_EVENTP(tp, NULL,
1934 &tp->t_inpcb->inp_socket->so_rcv,
1935 &tp->t_inpcb->inp_socket->so_snd,
1936 TCP_HDWR_TLS, 0,
1937 0, &log, false, &tv);
1938 }
1939 }
1940
1941 static void
rack_log_type_just_return(struct tcp_rack * rack,uint32_t cts,uint32_t tlen,uint32_t slot,uint8_t hpts_calling,int reason,uint32_t cwnd_to_use)1942 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot,
1943 uint8_t hpts_calling, int reason, uint32_t cwnd_to_use)
1944 {
1945 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1946 union tcp_log_stackspecific log;
1947 struct timeval tv;
1948
1949 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1950 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1951 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1952 log.u_bbr.flex1 = slot;
1953 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
1954 log.u_bbr.flex4 = reason;
1955 if (rack->rack_no_prr)
1956 log.u_bbr.flex5 = 0;
1957 else
1958 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
1959 log.u_bbr.flex7 = hpts_calling;
1960 log.u_bbr.flex8 = rack->rc_in_persist;
1961 log.u_bbr.lt_epoch = cwnd_to_use;
1962 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1963 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1964 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1965 &rack->rc_inp->inp_socket->so_rcv,
1966 &rack->rc_inp->inp_socket->so_snd,
1967 BBR_LOG_JUSTRET, 0,
1968 tlen, &log, false, &tv);
1969 }
1970 }
1971
1972 static void
rack_log_to_cancel(struct tcp_rack * rack,int32_t hpts_removed,int line,uint32_t us_cts,struct timeval * tv,uint32_t flags_on_entry)1973 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts,
1974 struct timeval *tv, uint32_t flags_on_entry)
1975 {
1976 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1977 union tcp_log_stackspecific log;
1978
1979 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1980 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1981 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1982 log.u_bbr.flex1 = line;
1983 log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to;
1984 log.u_bbr.flex3 = flags_on_entry;
1985 log.u_bbr.flex4 = us_cts;
1986 if (rack->rack_no_prr)
1987 log.u_bbr.flex5 = 0;
1988 else
1989 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt;
1990 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
1991 log.u_bbr.flex7 = hpts_removed;
1992 log.u_bbr.flex8 = 1;
1993 log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags;
1994 log.u_bbr.timeStamp = us_cts;
1995 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
1996 TCP_LOG_EVENTP(rack->rc_tp, NULL,
1997 &rack->rc_inp->inp_socket->so_rcv,
1998 &rack->rc_inp->inp_socket->so_snd,
1999 BBR_LOG_TIMERCANC, 0,
2000 0, &log, false, tv);
2001 }
2002 }
2003
2004 static void
rack_log_alt_to_to_cancel(struct tcp_rack * rack,uint32_t flex1,uint32_t flex2,uint32_t flex3,uint32_t flex4,uint32_t flex5,uint32_t flex6,uint16_t flex7,uint8_t mod)2005 rack_log_alt_to_to_cancel(struct tcp_rack *rack,
2006 uint32_t flex1, uint32_t flex2,
2007 uint32_t flex3, uint32_t flex4,
2008 uint32_t flex5, uint32_t flex6,
2009 uint16_t flex7, uint8_t mod)
2010 {
2011 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2012 union tcp_log_stackspecific log;
2013 struct timeval tv;
2014
2015 if (mod == 1) {
2016 /* No you can't use 1, its for the real to cancel */
2017 return;
2018 }
2019 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2020 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2021 log.u_bbr.flex1 = flex1;
2022 log.u_bbr.flex2 = flex2;
2023 log.u_bbr.flex3 = flex3;
2024 log.u_bbr.flex4 = flex4;
2025 log.u_bbr.flex5 = flex5;
2026 log.u_bbr.flex6 = flex6;
2027 log.u_bbr.flex7 = flex7;
2028 log.u_bbr.flex8 = mod;
2029 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2030 &rack->rc_inp->inp_socket->so_rcv,
2031 &rack->rc_inp->inp_socket->so_snd,
2032 BBR_LOG_TIMERCANC, 0,
2033 0, &log, false, &tv);
2034 }
2035 }
2036
2037 static void
rack_log_to_processing(struct tcp_rack * rack,uint32_t cts,int32_t ret,int32_t timers)2038 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
2039 {
2040 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2041 union tcp_log_stackspecific log;
2042 struct timeval tv;
2043
2044 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2045 log.u_bbr.flex1 = timers;
2046 log.u_bbr.flex2 = ret;
2047 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
2048 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
2049 log.u_bbr.flex5 = cts;
2050 if (rack->rack_no_prr)
2051 log.u_bbr.flex6 = 0;
2052 else
2053 log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt;
2054 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2055 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2056 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2057 &rack->rc_inp->inp_socket->so_rcv,
2058 &rack->rc_inp->inp_socket->so_snd,
2059 BBR_LOG_TO_PROCESS, 0,
2060 0, &log, false, &tv);
2061 }
2062 }
2063
2064 static void
rack_log_to_prr(struct tcp_rack * rack,int frm,int orig_cwnd)2065 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd)
2066 {
2067 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2068 union tcp_log_stackspecific log;
2069 struct timeval tv;
2070
2071 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2072 log.u_bbr.flex1 = rack->r_ctl.rc_prr_out;
2073 log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs;
2074 if (rack->rack_no_prr)
2075 log.u_bbr.flex3 = 0;
2076 else
2077 log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt;
2078 log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered;
2079 log.u_bbr.flex5 = rack->r_ctl.rc_sacked;
2080 log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt;
2081 log.u_bbr.flex8 = frm;
2082 log.u_bbr.pkts_out = orig_cwnd;
2083 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2084 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2085 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2086 &rack->rc_inp->inp_socket->so_rcv,
2087 &rack->rc_inp->inp_socket->so_snd,
2088 BBR_LOG_BBRUPD, 0,
2089 0, &log, false, &tv);
2090 }
2091 }
2092
2093 #ifdef NETFLIX_EXP_DETECTION
2094 static void
rack_log_sad(struct tcp_rack * rack,int event)2095 rack_log_sad(struct tcp_rack *rack, int event)
2096 {
2097 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2098 union tcp_log_stackspecific log;
2099 struct timeval tv;
2100
2101 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2102 log.u_bbr.flex1 = rack->r_ctl.sack_count;
2103 log.u_bbr.flex2 = rack->r_ctl.ack_count;
2104 log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra;
2105 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move;
2106 log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced;
2107 log.u_bbr.flex6 = tcp_sack_to_ack_thresh;
2108 log.u_bbr.pkts_out = tcp_sack_to_move_thresh;
2109 log.u_bbr.lt_epoch = (tcp_force_detection << 8);
2110 log.u_bbr.lt_epoch |= rack->do_detection;
2111 log.u_bbr.applimited = tcp_map_minimum;
2112 log.u_bbr.flex7 = rack->sack_attack_disable;
2113 log.u_bbr.flex8 = event;
2114 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2115 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2116 log.u_bbr.delivered = tcp_sad_decay_val;
2117 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2118 &rack->rc_inp->inp_socket->so_rcv,
2119 &rack->rc_inp->inp_socket->so_snd,
2120 TCP_SAD_DETECTION, 0,
2121 0, &log, false, &tv);
2122 }
2123 }
2124 #endif
2125
2126 static void
rack_counter_destroy(void)2127 rack_counter_destroy(void)
2128 {
2129 counter_u64_free(rack_ack_total);
2130 counter_u64_free(rack_express_sack);
2131 counter_u64_free(rack_sack_total);
2132 counter_u64_free(rack_move_none);
2133 counter_u64_free(rack_move_some);
2134 counter_u64_free(rack_sack_attacks_detected);
2135 counter_u64_free(rack_sack_attacks_reversed);
2136 counter_u64_free(rack_sack_used_next_merge);
2137 counter_u64_free(rack_sack_used_prev_merge);
2138 counter_u64_free(rack_badfr);
2139 counter_u64_free(rack_badfr_bytes);
2140 counter_u64_free(rack_rtm_prr_retran);
2141 counter_u64_free(rack_rtm_prr_newdata);
2142 counter_u64_free(rack_timestamp_mismatch);
2143 counter_u64_free(rack_find_high);
2144 counter_u64_free(rack_reorder_seen);
2145 counter_u64_free(rack_tlp_tot);
2146 counter_u64_free(rack_tlp_newdata);
2147 counter_u64_free(rack_tlp_retran);
2148 counter_u64_free(rack_tlp_retran_bytes);
2149 counter_u64_free(rack_tlp_retran_fail);
2150 counter_u64_free(rack_to_tot);
2151 counter_u64_free(rack_to_arm_rack);
2152 counter_u64_free(rack_to_arm_tlp);
2153 counter_u64_free(rack_calc_zero);
2154 counter_u64_free(rack_calc_nonzero);
2155 counter_u64_free(rack_paced_segments);
2156 counter_u64_free(rack_unpaced_segments);
2157 counter_u64_free(rack_saw_enobuf);
2158 counter_u64_free(rack_saw_enetunreach);
2159 counter_u64_free(rack_to_alloc);
2160 counter_u64_free(rack_to_alloc_hard);
2161 counter_u64_free(rack_to_alloc_emerg);
2162 counter_u64_free(rack_to_alloc_limited);
2163 counter_u64_free(rack_alloc_limited_conns);
2164 counter_u64_free(rack_split_limited);
2165 counter_u64_free(rack_sack_proc_all);
2166 counter_u64_free(rack_sack_proc_restart);
2167 counter_u64_free(rack_sack_proc_short);
2168 counter_u64_free(rack_enter_tlp_calc);
2169 counter_u64_free(rack_used_tlpmethod);
2170 counter_u64_free(rack_used_tlpmethod2);
2171 counter_u64_free(rack_sack_skipped_acked);
2172 counter_u64_free(rack_sack_splits);
2173 counter_u64_free(rack_progress_drops);
2174 counter_u64_free(rack_input_idle_reduces);
2175 counter_u64_free(rack_collapsed_win);
2176 counter_u64_free(rack_tlp_does_nada);
2177 counter_u64_free(rack_try_scwnd);
2178 counter_u64_free(rack_per_timer_hole);
2179 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
2180 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
2181 }
2182
2183 static struct rack_sendmap *
rack_alloc(struct tcp_rack * rack)2184 rack_alloc(struct tcp_rack *rack)
2185 {
2186 struct rack_sendmap *rsm;
2187
2188 rsm = uma_zalloc(rack_zone, M_NOWAIT);
2189 if (rsm) {
2190 rack->r_ctl.rc_num_maps_alloced++;
2191 counter_u64_add(rack_to_alloc, 1);
2192 return (rsm);
2193 }
2194 if (rack->rc_free_cnt) {
2195 counter_u64_add(rack_to_alloc_emerg, 1);
2196 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
2197 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
2198 rack->rc_free_cnt--;
2199 return (rsm);
2200 }
2201 return (NULL);
2202 }
2203
2204 static struct rack_sendmap *
rack_alloc_full_limit(struct tcp_rack * rack)2205 rack_alloc_full_limit(struct tcp_rack *rack)
2206 {
2207 if ((V_tcp_map_entries_limit > 0) &&
2208 (rack->do_detection == 0) &&
2209 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
2210 counter_u64_add(rack_to_alloc_limited, 1);
2211 if (!rack->alloc_limit_reported) {
2212 rack->alloc_limit_reported = 1;
2213 counter_u64_add(rack_alloc_limited_conns, 1);
2214 }
2215 return (NULL);
2216 }
2217 return (rack_alloc(rack));
2218 }
2219
2220 /* wrapper to allocate a sendmap entry, subject to a specific limit */
2221 static struct rack_sendmap *
rack_alloc_limit(struct tcp_rack * rack,uint8_t limit_type)2222 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type)
2223 {
2224 struct rack_sendmap *rsm;
2225
2226 if (limit_type) {
2227 /* currently there is only one limit type */
2228 if (V_tcp_map_split_limit > 0 &&
2229 (rack->do_detection == 0) &&
2230 rack->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) {
2231 counter_u64_add(rack_split_limited, 1);
2232 if (!rack->alloc_limit_reported) {
2233 rack->alloc_limit_reported = 1;
2234 counter_u64_add(rack_alloc_limited_conns, 1);
2235 }
2236 return (NULL);
2237 }
2238 }
2239
2240 /* allocate and mark in the limit type, if set */
2241 rsm = rack_alloc(rack);
2242 if (rsm != NULL && limit_type) {
2243 rsm->r_limit_type = limit_type;
2244 rack->r_ctl.rc_num_split_allocs++;
2245 }
2246 return (rsm);
2247 }
2248
2249 static void
rack_free(struct tcp_rack * rack,struct rack_sendmap * rsm)2250 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
2251 {
2252 if (rsm->r_flags & RACK_APP_LIMITED) {
2253 if (rack->r_ctl.rc_app_limited_cnt > 0) {
2254 rack->r_ctl.rc_app_limited_cnt--;
2255 }
2256 }
2257 if (rsm->r_limit_type) {
2258 /* currently there is only one limit type */
2259 rack->r_ctl.rc_num_split_allocs--;
2260 }
2261 if (rsm == rack->r_ctl.rc_first_appl) {
2262 if (rack->r_ctl.rc_app_limited_cnt == 0)
2263 rack->r_ctl.rc_first_appl = NULL;
2264 else {
2265 /* Follow the next one out */
2266 struct rack_sendmap fe;
2267
2268 fe.r_start = rsm->r_nseq_appl;
2269 rack->r_ctl.rc_first_appl = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
2270 }
2271 }
2272 if (rsm == rack->r_ctl.rc_resend)
2273 rack->r_ctl.rc_resend = NULL;
2274 if (rsm == rack->r_ctl.rc_rsm_at_retran)
2275 rack->r_ctl.rc_rsm_at_retran = NULL;
2276 if (rsm == rack->r_ctl.rc_end_appl)
2277 rack->r_ctl.rc_end_appl = NULL;
2278 if (rack->r_ctl.rc_tlpsend == rsm)
2279 rack->r_ctl.rc_tlpsend = NULL;
2280 if (rack->r_ctl.rc_sacklast == rsm)
2281 rack->r_ctl.rc_sacklast = NULL;
2282 if (rack->rc_free_cnt < rack_free_cache) {
2283 memset(rsm, 0, sizeof(struct rack_sendmap));
2284 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
2285 rsm->r_limit_type = 0;
2286 rack->rc_free_cnt++;
2287 return;
2288 }
2289 rack->r_ctl.rc_num_maps_alloced--;
2290 uma_zfree(rack_zone, rsm);
2291 }
2292
2293 static uint32_t
rack_get_measure_window(struct tcpcb * tp,struct tcp_rack * rack)2294 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack)
2295 {
2296 uint64_t srtt, bw, len, tim;
2297 uint32_t segsiz, def_len, minl;
2298
2299 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
2300 def_len = rack_def_data_window * segsiz;
2301 if (rack->rc_gp_filled == 0) {
2302 /*
2303 * We have no measurement (IW is in flight?) so
2304 * we can only guess using our data_window sysctl
2305 * value (usually 100MSS).
2306 */
2307 return (def_len);
2308 }
2309 /*
2310 * Now we have a number of factors to consider.
2311 *
2312 * 1) We have a desired BDP which is usually
2313 * at least 2.
2314 * 2) We have a minimum number of rtt's usually 1 SRTT
2315 * but we allow it too to be more.
2316 * 3) We want to make sure a measurement last N useconds (if
2317 * we have set rack_min_measure_usec.
2318 *
2319 * We handle the first concern here by trying to create a data
2320 * window of max(rack_def_data_window, DesiredBDP). The
2321 * second concern we handle in not letting the measurement
2322 * window end normally until at least the required SRTT's
2323 * have gone by which is done further below in
2324 * rack_enough_for_measurement(). Finally the third concern
2325 * we also handle here by calculating how long that time
2326 * would take at the current BW and then return the
2327 * max of our first calculation and that length. Note
2328 * that if rack_min_measure_usec is 0, we don't deal
2329 * with concern 3. Also for both Concern 1 and 3 an
2330 * application limited period could end the measurement
2331 * earlier.
2332 *
2333 * So lets calculate the BDP with the "known" b/w using
2334 * the SRTT has our rtt and then multiply it by the
2335 * goal.
2336 */
2337 bw = rack_get_bw(rack);
2338 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
2339 len = bw * srtt;
2340 len /= (uint64_t)HPTS_USEC_IN_SEC;
2341 len *= max(1, rack_goal_bdp);
2342 /* Now we need to round up to the nearest MSS */
2343 len = roundup(len, segsiz);
2344 if (rack_min_measure_usec) {
2345 /* Now calculate our min length for this b/w */
2346 tim = rack_min_measure_usec;
2347 minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC;
2348 if (minl == 0)
2349 minl = 1;
2350 minl = roundup(minl, segsiz);
2351 if (len < minl)
2352 len = minl;
2353 }
2354 /*
2355 * Now if we have a very small window we want
2356 * to attempt to get the window that is
2357 * as small as possible. This happens on
2358 * low b/w connections and we don't want to
2359 * span huge numbers of rtt's between measurements.
2360 *
2361 * We basically include 2 over our "MIN window" so
2362 * that the measurement can be shortened (possibly) by
2363 * an ack'ed packet.
2364 */
2365 if (len < def_len)
2366 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz)));
2367 else
2368 return (max((uint32_t)len, def_len));
2369
2370 }
2371
2372 static int
rack_enough_for_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq th_ack)2373 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack)
2374 {
2375 uint32_t tim, srtts, segsiz;
2376
2377 /*
2378 * Has enough time passed for the GP measurement to be valid?
2379 */
2380 if ((tp->snd_max == tp->snd_una) ||
2381 (th_ack == tp->snd_max)){
2382 /* All is acked */
2383 return (1);
2384 }
2385 if (SEQ_LT(th_ack, tp->gput_seq)) {
2386 /* Not enough bytes yet */
2387 return (0);
2388 }
2389 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
2390 if (SEQ_LT(th_ack, tp->gput_ack) &&
2391 ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
2392 /* Not enough bytes yet */
2393 return (0);
2394 }
2395 if (rack->r_ctl.rc_first_appl &&
2396 (rack->r_ctl.rc_first_appl->r_start == th_ack)) {
2397 /*
2398 * We are up to the app limited point
2399 * we have to measure irrespective of the time..
2400 */
2401 return (1);
2402 }
2403 /* Now what about time? */
2404 srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts);
2405 tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts;
2406 if (tim >= srtts) {
2407 return (1);
2408 }
2409 /* Nope not even a full SRTT has passed */
2410 return (0);
2411 }
2412
2413 static void
rack_log_timely(struct tcp_rack * rack,uint32_t logged,uint64_t cur_bw,uint64_t low_bnd,uint64_t up_bnd,int line,uint8_t method)2414 rack_log_timely(struct tcp_rack *rack,
2415 uint32_t logged, uint64_t cur_bw, uint64_t low_bnd,
2416 uint64_t up_bnd, int line, uint8_t method)
2417 {
2418 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2419 union tcp_log_stackspecific log;
2420 struct timeval tv;
2421
2422 memset(&log, 0, sizeof(log));
2423 log.u_bbr.flex1 = logged;
2424 log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt;
2425 log.u_bbr.flex2 <<= 4;
2426 log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt;
2427 log.u_bbr.flex2 <<= 4;
2428 log.u_bbr.flex2 |= rack->rc_gp_incr;
2429 log.u_bbr.flex2 <<= 4;
2430 log.u_bbr.flex2 |= rack->rc_gp_bwred;
2431 log.u_bbr.flex3 = rack->rc_gp_incr;
2432 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
2433 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca;
2434 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec;
2435 log.u_bbr.flex7 = rack->rc_gp_bwred;
2436 log.u_bbr.flex8 = method;
2437 log.u_bbr.cur_del_rate = cur_bw;
2438 log.u_bbr.delRate = low_bnd;
2439 log.u_bbr.bw_inuse = up_bnd;
2440 log.u_bbr.rttProp = rack_get_bw(rack);
2441 log.u_bbr.pkt_epoch = line;
2442 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
2443 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2444 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2445 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
2446 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
2447 log.u_bbr.cwnd_gain = rack->rc_dragged_bottom;
2448 log.u_bbr.cwnd_gain <<= 1;
2449 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec;
2450 log.u_bbr.cwnd_gain <<= 1;
2451 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
2452 log.u_bbr.cwnd_gain <<= 1;
2453 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
2454 log.u_bbr.lost = rack->r_ctl.rc_loss_count;
2455 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2456 &rack->rc_inp->inp_socket->so_rcv,
2457 &rack->rc_inp->inp_socket->so_snd,
2458 TCP_TIMELY_WORK, 0,
2459 0, &log, false, &tv);
2460 }
2461 }
2462
2463 static int
rack_bw_can_be_raised(struct tcp_rack * rack,uint64_t cur_bw,uint64_t last_bw_est,uint16_t mult)2464 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult)
2465 {
2466 /*
2467 * Before we increase we need to know if
2468 * the estimate just made was less than
2469 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est)
2470 *
2471 * If we already are pacing at a fast enough
2472 * rate to push us faster there is no sense of
2473 * increasing.
2474 *
2475 * We first caculate our actual pacing rate (ss or ca multipler
2476 * times our cur_bw).
2477 *
2478 * Then we take the last measured rate and multipy by our
2479 * maximum pacing overage to give us a max allowable rate.
2480 *
2481 * If our act_rate is smaller than our max_allowable rate
2482 * then we should increase. Else we should hold steady.
2483 *
2484 */
2485 uint64_t act_rate, max_allow_rate;
2486
2487 if (rack_timely_no_stopping)
2488 return (1);
2489
2490 if ((cur_bw == 0) || (last_bw_est == 0)) {
2491 /*
2492 * Initial startup case or
2493 * everything is acked case.
2494 */
2495 rack_log_timely(rack, mult, cur_bw, 0, 0,
2496 __LINE__, 9);
2497 return (1);
2498 }
2499 if (mult <= 100) {
2500 /*
2501 * We can always pace at or slightly above our rate.
2502 */
2503 rack_log_timely(rack, mult, cur_bw, 0, 0,
2504 __LINE__, 9);
2505 return (1);
2506 }
2507 act_rate = cur_bw * (uint64_t)mult;
2508 act_rate /= 100;
2509 max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100);
2510 max_allow_rate /= 100;
2511 if (act_rate < max_allow_rate) {
2512 /*
2513 * Here the rate we are actually pacing at
2514 * is smaller than 10% above our last measurement.
2515 * This means we are pacing below what we would
2516 * like to try to achieve (plus some wiggle room).
2517 */
2518 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
2519 __LINE__, 9);
2520 return (1);
2521 } else {
2522 /*
2523 * Here we are already pacing at least rack_max_per_above(10%)
2524 * what we are getting back. This indicates most likely
2525 * that we are being limited (cwnd/rwnd/app) and can't
2526 * get any more b/w. There is no sense of trying to
2527 * raise up the pacing rate its not speeding us up
2528 * and we already are pacing faster than we are getting.
2529 */
2530 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate,
2531 __LINE__, 8);
2532 return (0);
2533 }
2534 }
2535
2536 static void
rack_validate_multipliers_at_or_above100(struct tcp_rack * rack)2537 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack)
2538 {
2539 /*
2540 * When we drag bottom, we want to assure
2541 * that no multiplier is below 1.0, if so
2542 * we want to restore it to at least that.
2543 */
2544 if (rack->r_ctl.rack_per_of_gp_rec < 100) {
2545 /* This is unlikely we usually do not touch recovery */
2546 rack->r_ctl.rack_per_of_gp_rec = 100;
2547 }
2548 if (rack->r_ctl.rack_per_of_gp_ca < 100) {
2549 rack->r_ctl.rack_per_of_gp_ca = 100;
2550 }
2551 if (rack->r_ctl.rack_per_of_gp_ss < 100) {
2552 rack->r_ctl.rack_per_of_gp_ss = 100;
2553 }
2554 }
2555
2556 static void
rack_validate_multipliers_at_or_below_100(struct tcp_rack * rack)2557 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack)
2558 {
2559 if (rack->r_ctl.rack_per_of_gp_ca > 100) {
2560 rack->r_ctl.rack_per_of_gp_ca = 100;
2561 }
2562 if (rack->r_ctl.rack_per_of_gp_ss > 100) {
2563 rack->r_ctl.rack_per_of_gp_ss = 100;
2564 }
2565 }
2566
2567 static void
rack_increase_bw_mul(struct tcp_rack * rack,int timely_says,uint64_t cur_bw,uint64_t last_bw_est,int override)2568 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override)
2569 {
2570 int32_t calc, logged, plus;
2571
2572 logged = 0;
2573
2574 if (override) {
2575 /*
2576 * override is passed when we are
2577 * loosing b/w and making one last
2578 * gasp at trying to not loose out
2579 * to a new-reno flow.
2580 */
2581 goto extra_boost;
2582 }
2583 /* In classic timely we boost by 5x if we have 5 increases in a row, lets not */
2584 if (rack->rc_gp_incr &&
2585 ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) {
2586 /*
2587 * Reset and get 5 strokes more before the boost. Note
2588 * that the count is 0 based so we have to add one.
2589 */
2590 extra_boost:
2591 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST;
2592 rack->rc_gp_timely_inc_cnt = 0;
2593 } else
2594 plus = (uint32_t)rack_gp_increase_per;
2595 /* Must be at least 1% increase for true timely increases */
2596 if ((plus < 1) &&
2597 ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0)))
2598 plus = 1;
2599 if (rack->rc_gp_saw_rec &&
2600 (rack->rc_gp_no_rec_chg == 0) &&
2601 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
2602 rack->r_ctl.rack_per_of_gp_rec)) {
2603 /* We have been in recovery ding it too */
2604 calc = rack->r_ctl.rack_per_of_gp_rec + plus;
2605 if (calc > 0xffff)
2606 calc = 0xffff;
2607 logged |= 1;
2608 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc;
2609 if (rack_per_upper_bound_ss &&
2610 (rack->rc_dragged_bottom == 0) &&
2611 (rack->r_ctl.rack_per_of_gp_rec > rack_per_upper_bound_ss))
2612 rack->r_ctl.rack_per_of_gp_rec = rack_per_upper_bound_ss;
2613 }
2614 if (rack->rc_gp_saw_ca &&
2615 (rack->rc_gp_saw_ss == 0) &&
2616 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
2617 rack->r_ctl.rack_per_of_gp_ca)) {
2618 /* In CA */
2619 calc = rack->r_ctl.rack_per_of_gp_ca + plus;
2620 if (calc > 0xffff)
2621 calc = 0xffff;
2622 logged |= 2;
2623 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc;
2624 if (rack_per_upper_bound_ca &&
2625 (rack->rc_dragged_bottom == 0) &&
2626 (rack->r_ctl.rack_per_of_gp_ca > rack_per_upper_bound_ca))
2627 rack->r_ctl.rack_per_of_gp_ca = rack_per_upper_bound_ca;
2628 }
2629 if (rack->rc_gp_saw_ss &&
2630 rack_bw_can_be_raised(rack, cur_bw, last_bw_est,
2631 rack->r_ctl.rack_per_of_gp_ss)) {
2632 /* In SS */
2633 calc = rack->r_ctl.rack_per_of_gp_ss + plus;
2634 if (calc > 0xffff)
2635 calc = 0xffff;
2636 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc;
2637 if (rack_per_upper_bound_ss &&
2638 (rack->rc_dragged_bottom == 0) &&
2639 (rack->r_ctl.rack_per_of_gp_ss > rack_per_upper_bound_ss))
2640 rack->r_ctl.rack_per_of_gp_ss = rack_per_upper_bound_ss;
2641 logged |= 4;
2642 }
2643 if (logged &&
2644 (rack->rc_gp_incr == 0)){
2645 /* Go into increment mode */
2646 rack->rc_gp_incr = 1;
2647 rack->rc_gp_timely_inc_cnt = 0;
2648 }
2649 if (rack->rc_gp_incr &&
2650 logged &&
2651 (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) {
2652 rack->rc_gp_timely_inc_cnt++;
2653 }
2654 rack_log_timely(rack, logged, plus, 0, 0,
2655 __LINE__, 1);
2656 }
2657
2658 static uint32_t
rack_get_decrease(struct tcp_rack * rack,uint32_t curper,int32_t rtt_diff)2659 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff)
2660 {
2661 /*
2662 * norm_grad = rtt_diff / minrtt;
2663 * new_per = curper * (1 - B * norm_grad)
2664 *
2665 * B = rack_gp_decrease_per (default 10%)
2666 * rtt_dif = input var current rtt-diff
2667 * curper = input var current percentage
2668 * minrtt = from rack filter
2669 *
2670 */
2671 uint64_t perf;
2672
2673 perf = (((uint64_t)curper * ((uint64_t)1000000 -
2674 ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 *
2675 (((uint64_t)rtt_diff * (uint64_t)1000000)/
2676 (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/
2677 (uint64_t)1000000)) /
2678 (uint64_t)1000000);
2679 if (perf > curper) {
2680 /* TSNH */
2681 perf = curper - 1;
2682 }
2683 return ((uint32_t)perf);
2684 }
2685
2686 static uint32_t
rack_decrease_highrtt(struct tcp_rack * rack,uint32_t curper,uint32_t rtt)2687 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt)
2688 {
2689 /*
2690 * highrttthresh
2691 * result = curper * (1 - (B * ( 1 - ------ ))
2692 * gp_srtt
2693 *
2694 * B = rack_gp_decrease_per (default 10%)
2695 * highrttthresh = filter_min * rack_gp_rtt_maxmul
2696 */
2697 uint64_t perf;
2698 uint32_t highrttthresh;
2699
2700 highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
2701
2702 perf = (((uint64_t)curper * ((uint64_t)1000000 -
2703 ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 -
2704 ((uint64_t)highrttthresh * (uint64_t)1000000) /
2705 (uint64_t)rtt)) / 100)) /(uint64_t)1000000);
2706 return (perf);
2707 }
2708
2709 static void
rack_decrease_bw_mul(struct tcp_rack * rack,int timely_says,uint32_t rtt,int32_t rtt_diff)2710 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff)
2711 {
2712 uint64_t logvar, logvar2, logvar3;
2713 uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val;
2714
2715 if (rack->rc_gp_incr) {
2716 /* Turn off increment counting */
2717 rack->rc_gp_incr = 0;
2718 rack->rc_gp_timely_inc_cnt = 0;
2719 }
2720 ss_red = ca_red = rec_red = 0;
2721 logged = 0;
2722 /* Calculate the reduction value */
2723 if (rtt_diff < 0) {
2724 rtt_diff *= -1;
2725 }
2726 /* Must be at least 1% reduction */
2727 if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) {
2728 /* We have been in recovery ding it too */
2729 if (timely_says == 2) {
2730 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt);
2731 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
2732 if (alt < new_per)
2733 val = alt;
2734 else
2735 val = new_per;
2736 } else
2737 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
2738 if (rack->r_ctl.rack_per_of_gp_rec > val) {
2739 rec_red = (rack->r_ctl.rack_per_of_gp_rec - val);
2740 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val;
2741 } else {
2742 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
2743 rec_red = 0;
2744 }
2745 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec)
2746 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound;
2747 logged |= 1;
2748 }
2749 if (rack->rc_gp_saw_ss) {
2750 /* Sent in SS */
2751 if (timely_says == 2) {
2752 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt);
2753 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
2754 if (alt < new_per)
2755 val = alt;
2756 else
2757 val = new_per;
2758 } else
2759 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff);
2760 if (rack->r_ctl.rack_per_of_gp_ss > new_per) {
2761 ss_red = rack->r_ctl.rack_per_of_gp_ss - val;
2762 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val;
2763 } else {
2764 ss_red = new_per;
2765 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
2766 logvar = new_per;
2767 logvar <<= 32;
2768 logvar |= alt;
2769 logvar2 = (uint32_t)rtt;
2770 logvar2 <<= 32;
2771 logvar2 |= (uint32_t)rtt_diff;
2772 logvar3 = rack_gp_rtt_maxmul;
2773 logvar3 <<= 32;
2774 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
2775 rack_log_timely(rack, timely_says,
2776 logvar2, logvar3,
2777 logvar, __LINE__, 10);
2778 }
2779 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss)
2780 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound;
2781 logged |= 4;
2782 } else if (rack->rc_gp_saw_ca) {
2783 /* Sent in CA */
2784 if (timely_says == 2) {
2785 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt);
2786 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff);
2787 if (alt < new_per)
2788 val = alt;
2789 else
2790 val = new_per;
2791 } else
2792 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff);
2793 if (rack->r_ctl.rack_per_of_gp_ca > val) {
2794 ca_red = rack->r_ctl.rack_per_of_gp_ca - val;
2795 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val;
2796 } else {
2797 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
2798 ca_red = 0;
2799 logvar = new_per;
2800 logvar <<= 32;
2801 logvar |= alt;
2802 logvar2 = (uint32_t)rtt;
2803 logvar2 <<= 32;
2804 logvar2 |= (uint32_t)rtt_diff;
2805 logvar3 = rack_gp_rtt_maxmul;
2806 logvar3 <<= 32;
2807 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
2808 rack_log_timely(rack, timely_says,
2809 logvar2, logvar3,
2810 logvar, __LINE__, 10);
2811 }
2812 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca)
2813 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound;
2814 logged |= 2;
2815 }
2816 if (rack->rc_gp_timely_dec_cnt < 0x7) {
2817 rack->rc_gp_timely_dec_cnt++;
2818 if (rack_timely_dec_clear &&
2819 (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear))
2820 rack->rc_gp_timely_dec_cnt = 0;
2821 }
2822 logvar = ss_red;
2823 logvar <<= 32;
2824 logvar |= ca_red;
2825 rack_log_timely(rack, logged, rec_red, rack_per_lower_bound, logvar,
2826 __LINE__, 2);
2827 }
2828
2829 static void
rack_log_rtt_shrinks(struct tcp_rack * rack,uint32_t us_cts,uint32_t rtt,uint32_t line,uint8_t reas)2830 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts,
2831 uint32_t rtt, uint32_t line, uint8_t reas)
2832 {
2833 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
2834 union tcp_log_stackspecific log;
2835 struct timeval tv;
2836
2837 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2838 log.u_bbr.flex1 = line;
2839 log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts;
2840 log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts;
2841 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss;
2842 log.u_bbr.flex5 = rtt;
2843 log.u_bbr.flex6 = rack->rc_highly_buffered;
2844 log.u_bbr.flex6 <<= 1;
2845 log.u_bbr.flex6 |= rack->forced_ack;
2846 log.u_bbr.flex6 <<= 1;
2847 log.u_bbr.flex6 |= rack->rc_gp_dyn_mul;
2848 log.u_bbr.flex6 <<= 1;
2849 log.u_bbr.flex6 |= rack->in_probe_rtt;
2850 log.u_bbr.flex6 <<= 1;
2851 log.u_bbr.flex6 |= rack->measure_saw_probe_rtt;
2852 log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt;
2853 log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca;
2854 log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec;
2855 log.u_bbr.flex8 = reas;
2856 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2857 log.u_bbr.delRate = rack_get_bw(rack);
2858 log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt;
2859 log.u_bbr.cur_del_rate <<= 32;
2860 log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt;
2861 log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered;
2862 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff;
2863 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
2864 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt;
2865 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt;
2866 log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts;
2867 log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight;
2868 log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
2869 log.u_bbr.rttProp = us_cts;
2870 log.u_bbr.rttProp <<= 32;
2871 log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt;
2872 TCP_LOG_EVENTP(rack->rc_tp, NULL,
2873 &rack->rc_inp->inp_socket->so_rcv,
2874 &rack->rc_inp->inp_socket->so_snd,
2875 BBR_LOG_RTT_SHRINKS, 0,
2876 0, &log, false, &rack->r_ctl.act_rcv_time);
2877 }
2878 }
2879
2880 static void
rack_set_prtt_target(struct tcp_rack * rack,uint32_t segsiz,uint32_t rtt)2881 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt)
2882 {
2883 uint64_t bwdp;
2884
2885 bwdp = rack_get_bw(rack);
2886 bwdp *= (uint64_t)rtt;
2887 bwdp /= (uint64_t)HPTS_USEC_IN_SEC;
2888 rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz);
2889 if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) {
2890 /*
2891 * A window protocol must be able to have 4 packets
2892 * outstanding as the floor in order to function
2893 * (especially considering delayed ack :D).
2894 */
2895 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs);
2896 }
2897 }
2898
2899 static void
rack_enter_probertt(struct tcp_rack * rack,uint32_t us_cts)2900 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts)
2901 {
2902 /**
2903 * ProbeRTT is a bit different in rack_pacing than in
2904 * BBR. It is like BBR in that it uses the lowering of
2905 * the RTT as a signal that we saw something new and
2906 * counts from there for how long between. But it is
2907 * different in that its quite simple. It does not
2908 * play with the cwnd and wait until we get down
2909 * to N segments outstanding and hold that for
2910 * 200ms. Instead it just sets the pacing reduction
2911 * rate to a set percentage (70 by default) and hold
2912 * that for a number of recent GP Srtt's.
2913 */
2914 uint32_t segsiz;
2915
2916 if (rack->rc_gp_dyn_mul == 0)
2917 return;
2918
2919 if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) {
2920 /* We are idle */
2921 return;
2922 }
2923 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
2924 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
2925 /*
2926 * Stop the goodput now, the idea here is
2927 * that future measurements with in_probe_rtt
2928 * won't register if they are not greater so
2929 * we want to get what info (if any) is available
2930 * now.
2931 */
2932 rack_do_goodput_measurement(rack->rc_tp, rack,
2933 rack->rc_tp->snd_una, __LINE__);
2934 }
2935 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
2936 rack->r_ctl.rc_time_probertt_entered = us_cts;
2937 segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
2938 rack->r_ctl.rc_pace_min_segs);
2939 rack->in_probe_rtt = 1;
2940 rack->measure_saw_probe_rtt = 1;
2941 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
2942 rack->r_ctl.rc_time_probertt_starts = 0;
2943 rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt;
2944 if (rack_probertt_use_min_rtt_entry)
2945 rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
2946 else
2947 rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt);
2948 rack_log_rtt_shrinks(rack, us_cts, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
2949 __LINE__, RACK_RTTS_ENTERPROBE);
2950 }
2951
2952 static void
rack_exit_probertt(struct tcp_rack * rack,uint32_t us_cts)2953 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts)
2954 {
2955 struct rack_sendmap *rsm;
2956 uint32_t segsiz;
2957
2958 segsiz = min(ctf_fixed_maxseg(rack->rc_tp),
2959 rack->r_ctl.rc_pace_min_segs);
2960 rack->in_probe_rtt = 0;
2961 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) &&
2962 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) {
2963 /*
2964 * Stop the goodput now, the idea here is
2965 * that future measurements with in_probe_rtt
2966 * won't register if they are not greater so
2967 * we want to get what info (if any) is available
2968 * now.
2969 */
2970 rack_do_goodput_measurement(rack->rc_tp, rack,
2971 rack->rc_tp->snd_una, __LINE__);
2972 } else if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
2973 /*
2974 * We don't have enough data to make a measurement.
2975 * So lets just stop and start here after exiting
2976 * probe-rtt. We probably are not interested in
2977 * the results anyway.
2978 */
2979 rack->rc_tp->t_flags &= ~TF_GPUTINPROG;
2980 }
2981 /*
2982 * Measurements through the current snd_max are going
2983 * to be limited by the slower pacing rate.
2984 *
2985 * We need to mark these as app-limited so we
2986 * don't collapse the b/w.
2987 */
2988 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
2989 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
2990 if (rack->r_ctl.rc_app_limited_cnt == 0)
2991 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
2992 else {
2993 /*
2994 * Go out to the end app limited and mark
2995 * this new one as next and move the end_appl up
2996 * to this guy.
2997 */
2998 if (rack->r_ctl.rc_end_appl)
2999 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
3000 rack->r_ctl.rc_end_appl = rsm;
3001 }
3002 rsm->r_flags |= RACK_APP_LIMITED;
3003 rack->r_ctl.rc_app_limited_cnt++;
3004 }
3005 /*
3006 * Now, we need to examine our pacing rate multipliers.
3007 * If its under 100%, we need to kick it back up to
3008 * 100%. We also don't let it be over our "max" above
3009 * the actual rate i.e. 100% + rack_clamp_atexit_prtt.
3010 * Note setting clamp_atexit_prtt to 0 has the effect
3011 * of setting CA/SS to 100% always at exit (which is
3012 * the default behavior).
3013 */
3014 if (rack_probertt_clear_is) {
3015 rack->rc_gp_incr = 0;
3016 rack->rc_gp_bwred = 0;
3017 rack->rc_gp_timely_inc_cnt = 0;
3018 rack->rc_gp_timely_dec_cnt = 0;
3019 }
3020 /* Do we do any clamping at exit? */
3021 if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) {
3022 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp;
3023 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp;
3024 }
3025 if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) {
3026 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt;
3027 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt;
3028 }
3029 /*
3030 * Lets set rtt_diff to 0, so that we will get a "boost"
3031 * after exiting.
3032 */
3033 rack->r_ctl.rc_rtt_diff = 0;
3034
3035 /* Clear all flags so we start fresh */
3036 rack->rc_tp->t_bytes_acked = 0;
3037 rack->rc_tp->ccv->flags &= ~CCF_ABC_SENTAWND;
3038 /*
3039 * If configured to, set the cwnd and ssthresh to
3040 * our targets.
3041 */
3042 if (rack_probe_rtt_sets_cwnd) {
3043 uint64_t ebdp;
3044 uint32_t setto;
3045
3046 /* Set ssthresh so we get into CA once we hit our target */
3047 if (rack_probertt_use_min_rtt_exit == 1) {
3048 /* Set to min rtt */
3049 rack_set_prtt_target(rack, segsiz,
3050 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt));
3051 } else if (rack_probertt_use_min_rtt_exit == 2) {
3052 /* Set to current gp rtt */
3053 rack_set_prtt_target(rack, segsiz,
3054 rack->r_ctl.rc_gp_srtt);
3055 } else if (rack_probertt_use_min_rtt_exit == 3) {
3056 /* Set to entry gp rtt */
3057 rack_set_prtt_target(rack, segsiz,
3058 rack->r_ctl.rc_entry_gp_rtt);
3059 } else {
3060 uint64_t sum;
3061 uint32_t setval;
3062
3063 sum = rack->r_ctl.rc_entry_gp_rtt;
3064 sum *= 10;
3065 sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt));
3066 if (sum >= 20) {
3067 /*
3068 * A highly buffered path needs
3069 * cwnd space for timely to work.
3070 * Lets set things up as if
3071 * we are heading back here again.
3072 */
3073 setval = rack->r_ctl.rc_entry_gp_rtt;
3074 } else if (sum >= 15) {
3075 /*
3076 * Lets take the smaller of the
3077 * two since we are just somewhat
3078 * buffered.
3079 */
3080 setval = rack->r_ctl.rc_gp_srtt;
3081 if (setval > rack->r_ctl.rc_entry_gp_rtt)
3082 setval = rack->r_ctl.rc_entry_gp_rtt;
3083 } else {
3084 /*
3085 * Here we are not highly buffered
3086 * and should pick the min we can to
3087 * keep from causing loss.
3088 */
3089 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
3090 }
3091 rack_set_prtt_target(rack, segsiz,
3092 setval);
3093 }
3094 if (rack_probe_rtt_sets_cwnd > 1) {
3095 /* There is a percentage here to boost */
3096 ebdp = rack->r_ctl.rc_target_probertt_flight;
3097 ebdp *= rack_probe_rtt_sets_cwnd;
3098 ebdp /= 100;
3099 setto = rack->r_ctl.rc_target_probertt_flight + ebdp;
3100 } else
3101 setto = rack->r_ctl.rc_target_probertt_flight;
3102 rack->rc_tp->snd_cwnd = roundup(setto, segsiz);
3103 if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) {
3104 /* Enforce a min */
3105 rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs;
3106 }
3107 /* If we set in the cwnd also set the ssthresh point so we are in CA */
3108 rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1);
3109 }
3110 rack_log_rtt_shrinks(rack, us_cts,
3111 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3112 __LINE__, RACK_RTTS_EXITPROBE);
3113 /* Clear times last so log has all the info */
3114 rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max;
3115 rack->r_ctl.rc_time_probertt_entered = us_cts;
3116 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
3117 rack->r_ctl.rc_time_of_last_probertt = us_cts;
3118 }
3119
3120 static void
rack_check_probe_rtt(struct tcp_rack * rack,uint32_t us_cts)3121 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts)
3122 {
3123 /* Check in on probe-rtt */
3124 if (rack->rc_gp_filled == 0) {
3125 /* We do not do p-rtt unless we have gp measurements */
3126 return;
3127 }
3128 if (rack->in_probe_rtt) {
3129 uint64_t no_overflow;
3130 uint32_t endtime, must_stay;
3131
3132 if (rack->r_ctl.rc_went_idle_time &&
3133 ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) {
3134 /*
3135 * We went idle during prtt, just exit now.
3136 */
3137 rack_exit_probertt(rack, us_cts);
3138 } else if (rack_probe_rtt_safety_val &&
3139 TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) &&
3140 ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) {
3141 /*
3142 * Probe RTT safety value triggered!
3143 */
3144 rack_log_rtt_shrinks(rack, us_cts,
3145 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3146 __LINE__, RACK_RTTS_SAFETY);
3147 rack_exit_probertt(rack, us_cts);
3148 }
3149 /* Calculate the max we will wait */
3150 endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait);
3151 if (rack->rc_highly_buffered)
3152 endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp);
3153 /* Calculate the min we must wait */
3154 must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain);
3155 if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) &&
3156 TSTMP_LT(us_cts, endtime)) {
3157 uint32_t calc;
3158 /* Do we lower more? */
3159 no_exit:
3160 if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered))
3161 calc = us_cts - rack->r_ctl.rc_time_probertt_entered;
3162 else
3163 calc = 0;
3164 calc /= max(rack->r_ctl.rc_gp_srtt, 1);
3165 if (calc) {
3166 /* Maybe */
3167 calc *= rack_per_of_gp_probertt_reduce;
3168 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc;
3169 /* Limit it too */
3170 if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh)
3171 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh;
3172 }
3173 /* We must reach target or the time set */
3174 return;
3175 }
3176 if (rack->r_ctl.rc_time_probertt_starts == 0) {
3177 if ((TSTMP_LT(us_cts, must_stay) &&
3178 rack->rc_highly_buffered) ||
3179 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) >
3180 rack->r_ctl.rc_target_probertt_flight)) {
3181 /* We are not past the must_stay time */
3182 goto no_exit;
3183 }
3184 rack_log_rtt_shrinks(rack, us_cts,
3185 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3186 __LINE__, RACK_RTTS_REACHTARGET);
3187 rack->r_ctl.rc_time_probertt_starts = us_cts;
3188 if (rack->r_ctl.rc_time_probertt_starts == 0)
3189 rack->r_ctl.rc_time_probertt_starts = 1;
3190 /* Restore back to our rate we want to pace at in prtt */
3191 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
3192 }
3193 /*
3194 * Setup our end time, some number of gp_srtts plus 200ms.
3195 */
3196 no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt *
3197 (uint64_t)rack_probertt_gpsrtt_cnt_mul);
3198 if (rack_probertt_gpsrtt_cnt_div)
3199 endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div);
3200 else
3201 endtime = 0;
3202 endtime += rack_min_probertt_hold;
3203 endtime += rack->r_ctl.rc_time_probertt_starts;
3204 if (TSTMP_GEQ(us_cts, endtime)) {
3205 /* yes, exit probertt */
3206 rack_exit_probertt(rack, us_cts);
3207 }
3208
3209 } else if((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) {
3210 /* Go into probertt, its been too long since we went lower */
3211 rack_enter_probertt(rack, us_cts);
3212 }
3213 }
3214
3215 static void
rack_update_multiplier(struct tcp_rack * rack,int32_t timely_says,uint64_t last_bw_est,uint32_t rtt,int32_t rtt_diff)3216 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est,
3217 uint32_t rtt, int32_t rtt_diff)
3218 {
3219 uint64_t cur_bw, up_bnd, low_bnd, subfr;
3220 uint32_t losses;
3221
3222 if ((rack->rc_gp_dyn_mul == 0) ||
3223 (rack->use_fixed_rate) ||
3224 (rack->in_probe_rtt) ||
3225 (rack->rc_always_pace == 0)) {
3226 /* No dynamic GP multipler in play */
3227 return;
3228 }
3229 losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start;
3230 cur_bw = rack_get_bw(rack);
3231 /* Calculate our up and down range */
3232 up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up;
3233 up_bnd /= 100;
3234 up_bnd += rack->r_ctl.last_gp_comp_bw;
3235
3236 subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down;
3237 subfr /= 100;
3238 low_bnd = rack->r_ctl.last_gp_comp_bw - subfr;
3239 if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) {
3240 /*
3241 * This is the case where our RTT is above
3242 * the max target and we have been configured
3243 * to just do timely no bonus up stuff in that case.
3244 *
3245 * There are two configurations, set to 1, and we
3246 * just do timely if we are over our max. If its
3247 * set above 1 then we slam the multipliers down
3248 * to 100 and then decrement per timely.
3249 */
3250 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
3251 __LINE__, 3);
3252 if (rack->r_ctl.rc_no_push_at_mrtt > 1)
3253 rack_validate_multipliers_at_or_below_100(rack);
3254 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
3255 } else if ((last_bw_est < low_bnd) && !losses) {
3256 /*
3257 * We are decreasing this is a bit complicated this
3258 * means we are loosing ground. This could be
3259 * because another flow entered and we are competing
3260 * for b/w with it. This will push the RTT up which
3261 * makes timely unusable unless we want to get shoved
3262 * into a corner and just be backed off (the age
3263 * old problem with delay based CC).
3264 *
3265 * On the other hand if it was a route change we
3266 * would like to stay somewhat contained and not
3267 * blow out the buffers.
3268 */
3269 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
3270 __LINE__, 3);
3271 rack->r_ctl.last_gp_comp_bw = cur_bw;
3272 if (rack->rc_gp_bwred == 0) {
3273 /* Go into reduction counting */
3274 rack->rc_gp_bwred = 1;
3275 rack->rc_gp_timely_dec_cnt = 0;
3276 }
3277 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) ||
3278 (timely_says == 0)) {
3279 /*
3280 * Push another time with a faster pacing
3281 * to try to gain back (we include override to
3282 * get a full raise factor).
3283 */
3284 if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) ||
3285 (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) ||
3286 (timely_says == 0) ||
3287 (rack_down_raise_thresh == 0)) {
3288 /*
3289 * Do an override up in b/w if we were
3290 * below the threshold or if the threshold
3291 * is zero we always do the raise.
3292 */
3293 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1);
3294 } else {
3295 /* Log it stays the same */
3296 rack_log_timely(rack, 0, last_bw_est, low_bnd, 0,
3297 __LINE__, 11);
3298 }
3299 rack->rc_gp_timely_dec_cnt++;
3300 /* We are not incrementing really no-count */
3301 rack->rc_gp_incr = 0;
3302 rack->rc_gp_timely_inc_cnt = 0;
3303 } else {
3304 /*
3305 * Lets just use the RTT
3306 * information and give up
3307 * pushing.
3308 */
3309 goto use_timely;
3310 }
3311 } else if ((timely_says != 2) &&
3312 !losses &&
3313 (last_bw_est > up_bnd)) {
3314 /*
3315 * We are increasing b/w lets keep going, updating
3316 * our b/w and ignoring any timely input, unless
3317 * of course we are at our max raise (if there is one).
3318 */
3319
3320 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
3321 __LINE__, 3);
3322 rack->r_ctl.last_gp_comp_bw = cur_bw;
3323 if (rack->rc_gp_saw_ss &&
3324 rack_per_upper_bound_ss &&
3325 (rack->r_ctl.rack_per_of_gp_ss == rack_per_upper_bound_ss)) {
3326 /*
3327 * In cases where we can't go higher
3328 * we should just use timely.
3329 */
3330 goto use_timely;
3331 }
3332 if (rack->rc_gp_saw_ca &&
3333 rack_per_upper_bound_ca &&
3334 (rack->r_ctl.rack_per_of_gp_ca == rack_per_upper_bound_ca)) {
3335 /*
3336 * In cases where we can't go higher
3337 * we should just use timely.
3338 */
3339 goto use_timely;
3340 }
3341 rack->rc_gp_bwred = 0;
3342 rack->rc_gp_timely_dec_cnt = 0;
3343 /* You get a set number of pushes if timely is trying to reduce */
3344 if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) {
3345 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
3346 } else {
3347 /* Log it stays the same */
3348 rack_log_timely(rack, 0, last_bw_est, up_bnd, 0,
3349 __LINE__, 12);
3350 }
3351 return;
3352 } else {
3353 /*
3354 * We are staying between the lower and upper range bounds
3355 * so use timely to decide.
3356 */
3357 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd,
3358 __LINE__, 3);
3359 use_timely:
3360 if (timely_says) {
3361 rack->rc_gp_incr = 0;
3362 rack->rc_gp_timely_inc_cnt = 0;
3363 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) &&
3364 !losses &&
3365 (last_bw_est < low_bnd)) {
3366 /* We are loosing ground */
3367 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
3368 rack->rc_gp_timely_dec_cnt++;
3369 /* We are not incrementing really no-count */
3370 rack->rc_gp_incr = 0;
3371 rack->rc_gp_timely_inc_cnt = 0;
3372 } else
3373 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff);
3374 } else {
3375 rack->rc_gp_bwred = 0;
3376 rack->rc_gp_timely_dec_cnt = 0;
3377 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0);
3378 }
3379 }
3380 }
3381
3382 static int32_t
rack_make_timely_judgement(struct tcp_rack * rack,uint32_t rtt,int32_t rtt_diff,uint32_t prev_rtt)3383 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt)
3384 {
3385 int32_t timely_says;
3386 uint64_t log_mult, log_rtt_a_diff;
3387
3388 log_rtt_a_diff = rtt;
3389 log_rtt_a_diff <<= 32;
3390 log_rtt_a_diff |= (uint32_t)rtt_diff;
3391 if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) *
3392 rack_gp_rtt_maxmul)) {
3393 /* Reduce the b/w multipler */
3394 timely_says = 2;
3395 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul;
3396 log_mult <<= 32;
3397 log_mult |= prev_rtt;
3398 rack_log_timely(rack, timely_says, log_mult,
3399 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3400 log_rtt_a_diff, __LINE__, 4);
3401 } else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
3402 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
3403 max(rack_gp_rtt_mindiv , 1)))) {
3404 /* Increase the b/w multipler */
3405 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) +
3406 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) /
3407 max(rack_gp_rtt_mindiv , 1));
3408 log_mult <<= 32;
3409 log_mult |= prev_rtt;
3410 timely_says = 0;
3411 rack_log_timely(rack, timely_says, log_mult ,
3412 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt),
3413 log_rtt_a_diff, __LINE__, 5);
3414 } else {
3415 /*
3416 * Use a gradient to find it the timely gradient
3417 * is:
3418 * grad = rc_rtt_diff / min_rtt;
3419 *
3420 * anything below or equal to 0 will be
3421 * a increase indication. Anything above
3422 * zero is a decrease. Note we take care
3423 * of the actual gradient calculation
3424 * in the reduction (its not needed for
3425 * increase).
3426 */
3427 log_mult = prev_rtt;
3428 if (rtt_diff <= 0) {
3429 /*
3430 * Rttdiff is less than zero, increase the
3431 * b/w multipler (its 0 or negative)
3432 */
3433 timely_says = 0;
3434 rack_log_timely(rack, timely_says, log_mult,
3435 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6);
3436 } else {
3437 /* Reduce the b/w multipler */
3438 timely_says = 1;
3439 rack_log_timely(rack, timely_says, log_mult,
3440 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7);
3441 }
3442 }
3443 return (timely_says);
3444 }
3445
3446 static void
rack_do_goodput_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq th_ack,int line)3447 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack,
3448 tcp_seq th_ack, int line)
3449 {
3450 uint64_t tim, bytes_ps, ltim, stim, utim;
3451 uint32_t segsiz, bytes, reqbytes, us_cts;
3452 int32_t gput, new_rtt_diff, timely_says;
3453
3454 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
3455 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
3456 if (TSTMP_GEQ(us_cts, tp->gput_ts))
3457 tim = us_cts - tp->gput_ts;
3458 else
3459 tim = 0;
3460
3461 if (TSTMP_GT(rack->r_ctl.rc_gp_cumack_ts, rack->r_ctl.rc_gp_output_ts))
3462 stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts;
3463 else
3464 stim = 0;
3465 /*
3466 * Use the larger of the send time or ack time. This prevents us
3467 * from being influenced by ack artifacts to come up with too
3468 * high of measurement. Note that since we are spanning over many more
3469 * bytes in most of our measurements hopefully that is less likely to
3470 * occur.
3471 */
3472 if (tim > stim)
3473 utim = max(tim, 1);
3474 else
3475 utim = max(stim, 1);
3476 /* Lets validate utim */
3477 ltim = max(1, (utim/HPTS_USEC_IN_MSEC));
3478 gput = (((uint64_t) (th_ack - tp->gput_seq)) << 3) / ltim;
3479 reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz));
3480 if ((tim == 0) && (stim == 0)) {
3481 /*
3482 * Invalid measurement time, maybe
3483 * all on one ack/one send?
3484 */
3485 bytes = 0;
3486 bytes_ps = 0;
3487 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
3488 0, 0, 0, 10, __LINE__, NULL);
3489 goto skip_measurement;
3490 }
3491 if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) {
3492 /* We never made a us_rtt measurement? */
3493 bytes = 0;
3494 bytes_ps = 0;
3495 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
3496 0, 0, 0, 10, __LINE__, NULL);
3497 goto skip_measurement;
3498 }
3499 /*
3500 * Calculate the maximum possible b/w this connection
3501 * could have. We base our calculation on the lowest
3502 * rtt we have seen during the measurement and the
3503 * largest rwnd the client has given us in that time. This
3504 * forms a BDP that is the maximum that we could ever
3505 * get to the client. Anything larger is not valid.
3506 *
3507 * I originally had code here that rejected measurements
3508 * where the time was less than 1/2 the latest us_rtt.
3509 * But after thinking on that I realized its wrong since
3510 * say you had a 150Mbps or even 1Gbps link, and you
3511 * were a long way away.. example I am in Europe (100ms rtt)
3512 * talking to my 1Gbps link in S.C. Now measuring say 150,000
3513 * bytes my time would be 1.2ms, and yet my rtt would say
3514 * the measurement was invalid the time was < 50ms. The
3515 * same thing is true for 150Mb (8ms of time).
3516 *
3517 * A better way I realized is to look at what the maximum
3518 * the connection could possibly do. This is gated on
3519 * the lowest RTT we have seen and the highest rwnd.
3520 * We should in theory never exceed that, if we are
3521 * then something on the path is storing up packets
3522 * and then feeding them all at once to our endpoint
3523 * messing up our measurement.
3524 */
3525 rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd;
3526 rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC;
3527 rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt;
3528 if (SEQ_LT(th_ack, tp->gput_seq)) {
3529 /* No measurement can be made */
3530 bytes = 0;
3531 bytes_ps = 0;
3532 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
3533 0, 0, 0, 10, __LINE__, NULL);
3534 goto skip_measurement;
3535 } else
3536 bytes = (th_ack - tp->gput_seq);
3537 bytes_ps = (uint64_t)bytes;
3538 /*
3539 * Don't measure a b/w for pacing unless we have gotten at least
3540 * an initial windows worth of data in this measurement interval.
3541 *
3542 * Small numbers of bytes get badly influenced by delayed ack and
3543 * other artifacts. Note we take the initial window or our
3544 * defined minimum GP (defaulting to 10 which hopefully is the
3545 * IW).
3546 */
3547 if (rack->rc_gp_filled == 0) {
3548 /*
3549 * The initial estimate is special. We
3550 * have blasted out an IW worth of packets
3551 * without a real valid ack ts results. We
3552 * then setup the app_limited_needs_set flag,
3553 * this should get the first ack in (probably 2
3554 * MSS worth) to be recorded as the timestamp.
3555 * We thus allow a smaller number of bytes i.e.
3556 * IW - 2MSS.
3557 */
3558 reqbytes -= (2 * segsiz);
3559 /* Also lets fill previous for our first measurement to be neutral */
3560 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
3561 }
3562 if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) {
3563 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
3564 rack->r_ctl.rc_app_limited_cnt,
3565 0, 0, 10, __LINE__, NULL);
3566 goto skip_measurement;
3567 }
3568 /*
3569 * We now need to calculate the Timely like status so
3570 * we can update (possibly) the b/w multipliers.
3571 */
3572 new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt;
3573 if (rack->rc_gp_filled == 0) {
3574 /* No previous reading */
3575 rack->r_ctl.rc_rtt_diff = new_rtt_diff;
3576 } else {
3577 if (rack->measure_saw_probe_rtt == 0) {
3578 /*
3579 * We don't want a probertt to be counted
3580 * since it will be negative incorrectly. We
3581 * expect to be reducing the RTT when we
3582 * pace at a slower rate.
3583 */
3584 rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8);
3585 rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8);
3586 }
3587 }
3588 timely_says = rack_make_timely_judgement(rack,
3589 rack->r_ctl.rc_gp_srtt,
3590 rack->r_ctl.rc_rtt_diff,
3591 rack->r_ctl.rc_prev_gp_srtt
3592 );
3593 bytes_ps *= HPTS_USEC_IN_SEC;
3594 bytes_ps /= utim;
3595 if (bytes_ps > rack->r_ctl.last_max_bw) {
3596 /*
3597 * Something is on path playing
3598 * since this b/w is not possible based
3599 * on our BDP (highest rwnd and lowest rtt
3600 * we saw in the measurement window).
3601 *
3602 * Another option here would be to
3603 * instead skip the measurement.
3604 */
3605 rack_log_pacing_delay_calc(rack, bytes, reqbytes,
3606 bytes_ps, rack->r_ctl.last_max_bw, 0,
3607 11, __LINE__, NULL);
3608 bytes_ps = rack->r_ctl.last_max_bw;
3609 }
3610 /* We store gp for b/w in bytes per second */
3611 if (rack->rc_gp_filled == 0) {
3612 /* Initial measurment */
3613 if (bytes_ps) {
3614 rack->r_ctl.gp_bw = bytes_ps;
3615 rack->rc_gp_filled = 1;
3616 rack->r_ctl.num_avg = 1;
3617 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
3618 } else {
3619 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes,
3620 rack->r_ctl.rc_app_limited_cnt,
3621 0, 0, 10, __LINE__, NULL);
3622 }
3623 if (rack->rc_inp->inp_in_hpts &&
3624 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
3625 /*
3626 * Ok we can't trust the pacer in this case
3627 * where we transition from un-paced to paced.
3628 * Or for that matter when the burst mitigation
3629 * was making a wild guess and got it wrong.
3630 * Stop the pacer and clear up all the aggregate
3631 * delays etc.
3632 */
3633 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
3634 rack->r_ctl.rc_hpts_flags = 0;
3635 rack->r_ctl.rc_last_output_to = 0;
3636 }
3637 } else if (rack->r_ctl.num_avg < RACK_REQ_AVG) {
3638 /* Still a small number run an average */
3639 rack->r_ctl.gp_bw += bytes_ps;
3640 rack->r_ctl.num_avg++;
3641 if (rack->r_ctl.num_avg >= RACK_REQ_AVG) {
3642 /* We have collected enought to move forward */
3643 rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_avg;
3644 }
3645 } else {
3646 /*
3647 * We want to take 1/wma of the goodput and add in to 7/8th
3648 * of the old value weighted by the srtt. So if your measurement
3649 * period is say 2 SRTT's long you would get 1/4 as the
3650 * value, if it was like 1/2 SRTT then you would get 1/16th.
3651 *
3652 * But we must be careful not to take too much i.e. if the
3653 * srtt is say 20ms and the measurement is taken over
3654 * 400ms our weight would be 400/20 i.e. 20. On the
3655 * other hand if we get a measurement over 1ms with a
3656 * 10ms rtt we only want to take a much smaller portion.
3657 */
3658 uint64_t resid_bw, subpart, addpart, srtt;
3659
3660 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT);
3661 if (srtt == 0) {
3662 /*
3663 * Strange why did t_srtt go back to zero?
3664 */
3665 if (rack->r_ctl.rc_rack_min_rtt)
3666 srtt = (rack->r_ctl.rc_rack_min_rtt * HPTS_USEC_IN_MSEC);
3667 else
3668 srtt = HPTS_USEC_IN_MSEC;
3669 }
3670 /*
3671 * XXXrrs: Note for reviewers, in playing with
3672 * dynamic pacing I discovered this GP calculation
3673 * as done originally leads to some undesired results.
3674 * Basically you can get longer measurements contributing
3675 * too much to the WMA. Thus I changed it if you are doing
3676 * dynamic adjustments to only do the aportioned adjustment
3677 * if we have a very small (time wise) measurement. Longer
3678 * measurements just get there weight (defaulting to 1/8)
3679 * add to the WMA. We may want to think about changing
3680 * this to always do that for both sides i.e. dynamic
3681 * and non-dynamic... but considering lots of folks
3682 * were playing with this I did not want to change the
3683 * calculation per.se. without your thoughts.. Lawerence?
3684 * Peter??
3685 */
3686 if (rack->rc_gp_dyn_mul == 0) {
3687 subpart = rack->r_ctl.gp_bw * utim;
3688 subpart /= (srtt * 8);
3689 if (subpart < (rack->r_ctl.gp_bw / 2)) {
3690 /*
3691 * The b/w update takes no more
3692 * away then 1/2 our running total
3693 * so factor it in.
3694 */
3695 addpart = bytes_ps * utim;
3696 addpart /= (srtt * 8);
3697 } else {
3698 /*
3699 * Don't allow a single measurement
3700 * to account for more than 1/2 of the
3701 * WMA. This could happen on a retransmission
3702 * where utim becomes huge compared to
3703 * srtt (multiple retransmissions when using
3704 * the sending rate which factors in all the
3705 * transmissions from the first one).
3706 */
3707 subpart = rack->r_ctl.gp_bw / 2;
3708 addpart = bytes_ps / 2;
3709 }
3710 resid_bw = rack->r_ctl.gp_bw - subpart;
3711 rack->r_ctl.gp_bw = resid_bw + addpart;
3712 } else {
3713 if ((utim / srtt) <= 1) {
3714 /*
3715 * The b/w update was over a small period
3716 * of time. The idea here is to prevent a small
3717 * measurement time period from counting
3718 * too much. So we scale it based on the
3719 * time so it attributes less than 1/rack_wma_divisor
3720 * of its measurement.
3721 */
3722 subpart = rack->r_ctl.gp_bw * utim;
3723 subpart /= (srtt * rack_wma_divisor);
3724 addpart = bytes_ps * utim;
3725 addpart /= (srtt * rack_wma_divisor);
3726 } else {
3727 /*
3728 * The scaled measurement was long
3729 * enough so lets just add in the
3730 * portion of the measurment i.e. 1/rack_wma_divisor
3731 */
3732 subpart = rack->r_ctl.gp_bw / rack_wma_divisor;
3733 addpart = bytes_ps / rack_wma_divisor;
3734 }
3735 if ((rack->measure_saw_probe_rtt == 0) ||
3736 (bytes_ps > rack->r_ctl.gp_bw)) {
3737 /*
3738 * For probe-rtt we only add it in
3739 * if its larger, all others we just
3740 * add in.
3741 */
3742 resid_bw = rack->r_ctl.gp_bw - subpart;
3743 rack->r_ctl.gp_bw = resid_bw + addpart;
3744 }
3745 }
3746 }
3747 /* We do not update any multipliers if we are in or have seen a probe-rtt */
3748 if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set)
3749 rack_update_multiplier(rack, timely_says, bytes_ps,
3750 rack->r_ctl.rc_gp_srtt,
3751 rack->r_ctl.rc_rtt_diff);
3752 rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim,
3753 rack_get_bw(rack), 3, line, NULL);
3754 /* reset the gp srtt and setup the new prev */
3755 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt;
3756 /* Record the lost count for the next measurement */
3757 rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count;
3758 /*
3759 * We restart our diffs based on the gpsrtt in the
3760 * measurement window.
3761 */
3762 rack->rc_gp_rtt_set = 0;
3763 rack->rc_gp_saw_rec = 0;
3764 rack->rc_gp_saw_ca = 0;
3765 rack->rc_gp_saw_ss = 0;
3766 rack->rc_dragged_bottom = 0;
3767 skip_measurement:
3768
3769 #ifdef STATS
3770 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
3771 gput);
3772 /*
3773 * XXXLAS: This is a temporary hack, and should be
3774 * chained off VOI_TCP_GPUT when stats(9) grows an
3775 * API to deal with chained VOIs.
3776 */
3777 if (tp->t_stats_gput_prev > 0)
3778 stats_voi_update_abs_s32(tp->t_stats,
3779 VOI_TCP_GPUT_ND,
3780 ((gput - tp->t_stats_gput_prev) * 100) /
3781 tp->t_stats_gput_prev);
3782 #endif
3783 tp->t_flags &= ~TF_GPUTINPROG;
3784 tp->t_stats_gput_prev = gput;
3785 /*
3786 * Now are we app limited now and there is space from where we
3787 * were to where we want to go?
3788 *
3789 * We don't do the other case i.e. non-applimited here since
3790 * the next send will trigger us picking up the missing data.
3791 */
3792 if (rack->r_ctl.rc_first_appl &&
3793 TCPS_HAVEESTABLISHED(tp->t_state) &&
3794 rack->r_ctl.rc_app_limited_cnt &&
3795 (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) &&
3796 ((rack->r_ctl.rc_first_appl->r_start - th_ack) >
3797 max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) {
3798 /*
3799 * Yep there is enough outstanding to make a measurement here.
3800 */
3801 struct rack_sendmap *rsm, fe;
3802
3803 tp->t_flags |= TF_GPUTINPROG;
3804 rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
3805 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
3806 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
3807 rack->app_limited_needs_set = 0;
3808 tp->gput_seq = th_ack;
3809 if (rack->in_probe_rtt)
3810 rack->measure_saw_probe_rtt = 1;
3811 else if ((rack->measure_saw_probe_rtt) &&
3812 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
3813 rack->measure_saw_probe_rtt = 0;
3814 if ((rack->r_ctl.rc_first_appl->r_start - th_ack) >= rack_get_measure_window(tp, rack)) {
3815 /* There is a full window to gain info from */
3816 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
3817 } else {
3818 /* We can only measure up to the applimited point */
3819 tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_start - th_ack);
3820 }
3821 /*
3822 * Now we need to find the timestamp of the send at tp->gput_seq
3823 * for the send based measurement.
3824 */
3825 fe.r_start = tp->gput_seq;
3826 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
3827 if (rsm) {
3828 /* Ok send-based limit is set */
3829 if (SEQ_LT(rsm->r_start, tp->gput_seq)) {
3830 /*
3831 * Move back to include the earlier part
3832 * so our ack time lines up right (this may
3833 * make an overlapping measurement but thats
3834 * ok).
3835 */
3836 tp->gput_seq = rsm->r_start;
3837 }
3838 if (rsm->r_flags & RACK_ACKED)
3839 tp->gput_ts = rsm->r_ack_arrival;
3840 else
3841 rack->app_limited_needs_set = 1;
3842 rack->r_ctl.rc_gp_output_ts = rsm->usec_orig_send;
3843 } else {
3844 /*
3845 * If we don't find the rsm due to some
3846 * send-limit set the current time, which
3847 * basically disables the send-limit.
3848 */
3849 rack->r_ctl.rc_gp_output_ts = tcp_get_usecs(NULL);
3850 }
3851 rack_log_pacing_delay_calc(rack,
3852 tp->gput_seq,
3853 tp->gput_ack,
3854 (uint64_t)rsm,
3855 tp->gput_ts,
3856 rack->r_ctl.rc_app_limited_cnt,
3857 9,
3858 __LINE__, NULL);
3859 }
3860 }
3861
3862 /*
3863 * CC wrapper hook functions
3864 */
3865 static void
rack_ack_received(struct tcpcb * tp,struct tcp_rack * rack,struct tcphdr * th,uint16_t nsegs,uint16_t type,int32_t recovery)3866 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, struct tcphdr *th, uint16_t nsegs,
3867 uint16_t type, int32_t recovery)
3868 {
3869 INP_WLOCK_ASSERT(tp->t_inpcb);
3870 tp->ccv->nsegs = nsegs;
3871 tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
3872 if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
3873 uint32_t max;
3874
3875 max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp);
3876 if (tp->ccv->bytes_this_ack > max) {
3877 tp->ccv->bytes_this_ack = max;
3878 }
3879 }
3880 if (rack->r_ctl.cwnd_to_use <= tp->snd_wnd)
3881 tp->ccv->flags |= CCF_CWND_LIMITED;
3882 else
3883 tp->ccv->flags &= ~CCF_CWND_LIMITED;
3884 #ifdef STATS
3885 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
3886 ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd);
3887 #endif
3888 if ((tp->t_flags & TF_GPUTINPROG) &&
3889 rack_enough_for_measurement(tp, rack, th->th_ack)) {
3890 /* Measure the Goodput */
3891 rack_do_goodput_measurement(tp, rack, th->th_ack, __LINE__);
3892 #ifdef NETFLIX_PEAKRATE
3893 if ((type == CC_ACK) &&
3894 (tp->t_maxpeakrate)) {
3895 /*
3896 * We update t_peakrate_thr. This gives us roughly
3897 * one update per round trip time. Note
3898 * it will only be used if pace_always is off i.e
3899 * we don't do this for paced flows.
3900 */
3901 tcp_update_peakrate_thr(tp);
3902 }
3903 #endif
3904 }
3905 if (rack->r_ctl.cwnd_to_use > tp->snd_ssthresh) {
3906 tp->t_bytes_acked += tp->ccv->bytes_this_ack;
3907 if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) {
3908 tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use;
3909 tp->ccv->flags |= CCF_ABC_SENTAWND;
3910 }
3911 } else {
3912 tp->ccv->flags &= ~CCF_ABC_SENTAWND;
3913 tp->t_bytes_acked = 0;
3914 }
3915 if (CC_ALGO(tp)->ack_received != NULL) {
3916 /* XXXLAS: Find a way to live without this */
3917 tp->ccv->curack = th->th_ack;
3918 CC_ALGO(tp)->ack_received(tp->ccv, type);
3919 }
3920 #ifdef STATS
3921 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use);
3922 #endif
3923 if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) {
3924 rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use;
3925 }
3926 #ifdef NETFLIX_PEAKRATE
3927 /* we enforce max peak rate if it is set and we are not pacing */
3928 if ((rack->rc_always_pace == 0) &&
3929 tp->t_peakrate_thr &&
3930 (tp->snd_cwnd > tp->t_peakrate_thr)) {
3931 tp->snd_cwnd = tp->t_peakrate_thr;
3932 }
3933 #endif
3934 }
3935
3936 static void
tcp_rack_partialack(struct tcpcb * tp,struct tcphdr * th)3937 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th)
3938 {
3939 struct tcp_rack *rack;
3940
3941 rack = (struct tcp_rack *)tp->t_fb_ptr;
3942 INP_WLOCK_ASSERT(tp->t_inpcb);
3943 /*
3944 * If we are doing PRR and have enough
3945 * room to send <or> we are pacing and prr
3946 * is disabled we will want to see if we
3947 * can send data (by setting r_wanted_output to
3948 * true).
3949 */
3950 if ((rack->r_ctl.rc_prr_sndcnt > 0) ||
3951 rack->rack_no_prr)
3952 rack->r_wanted_output = 1;
3953 }
3954
3955 static void
rack_post_recovery(struct tcpcb * tp,struct tcphdr * th)3956 rack_post_recovery(struct tcpcb *tp, struct tcphdr *th)
3957 {
3958 struct tcp_rack *rack;
3959 uint32_t orig_cwnd;
3960
3961 orig_cwnd = tp->snd_cwnd;
3962 INP_WLOCK_ASSERT(tp->t_inpcb);
3963 rack = (struct tcp_rack *)tp->t_fb_ptr;
3964 if (rack->rc_not_backing_off == 0) {
3965 /* only alert CC if we alerted when we entered */
3966 if (CC_ALGO(tp)->post_recovery != NULL) {
3967 tp->ccv->curack = th->th_ack;
3968 CC_ALGO(tp)->post_recovery(tp->ccv);
3969 }
3970 if (tp->snd_cwnd > tp->snd_ssthresh) {
3971 /* Drop us down to the ssthresh (1/2 cwnd at loss) */
3972 tp->snd_cwnd = tp->snd_ssthresh;
3973 }
3974 }
3975 if ((rack->rack_no_prr == 0) &&
3976 (rack->r_ctl.rc_prr_sndcnt > 0)) {
3977 /* Suck the next prr cnt back into cwnd */
3978 tp->snd_cwnd += rack->r_ctl.rc_prr_sndcnt;
3979 rack->r_ctl.rc_prr_sndcnt = 0;
3980 rack_log_to_prr(rack, 1, 0);
3981 }
3982 rack_log_to_prr(rack, 14, orig_cwnd);
3983 tp->snd_recover = tp->snd_una;
3984 EXIT_RECOVERY(tp->t_flags);
3985 }
3986
3987 static void
rack_cong_signal(struct tcpcb * tp,struct tcphdr * th,uint32_t type)3988 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
3989 {
3990 struct tcp_rack *rack;
3991
3992 INP_WLOCK_ASSERT(tp->t_inpcb);
3993
3994 rack = (struct tcp_rack *)tp->t_fb_ptr;
3995 switch (type) {
3996 case CC_NDUPACK:
3997 tp->t_flags &= ~TF_WASFRECOVERY;
3998 tp->t_flags &= ~TF_WASCRECOVERY;
3999 if (!IN_FASTRECOVERY(tp->t_flags)) {
4000 rack->r_ctl.rc_prr_delivered = 0;
4001 rack->r_ctl.rc_prr_out = 0;
4002 if (rack->rack_no_prr == 0) {
4003 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
4004 rack_log_to_prr(rack, 2, 0);
4005 }
4006 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
4007 tp->snd_recover = tp->snd_max;
4008 if (tp->t_flags2 & TF2_ECN_PERMIT)
4009 tp->t_flags2 |= TF2_ECN_SND_CWR;
4010 }
4011 break;
4012 case CC_ECN:
4013 if (!IN_CONGRECOVERY(tp->t_flags) ||
4014 /*
4015 * Allow ECN reaction on ACK to CWR, if
4016 * that data segment was also CE marked.
4017 */
4018 SEQ_GEQ(th->th_ack, tp->snd_recover)) {
4019 EXIT_CONGRECOVERY(tp->t_flags);
4020 KMOD_TCPSTAT_INC(tcps_ecn_rcwnd);
4021 tp->snd_recover = tp->snd_max + 1;
4022 if (tp->t_flags2 & TF2_ECN_PERMIT)
4023 tp->t_flags2 |= TF2_ECN_SND_CWR;
4024 }
4025 break;
4026 case CC_RTO:
4027 tp->t_dupacks = 0;
4028 tp->t_bytes_acked = 0;
4029 EXIT_RECOVERY(tp->t_flags);
4030 tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 /
4031 ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp);
4032 tp->snd_cwnd = ctf_fixed_maxseg(tp);
4033 if (tp->t_flags2 & TF2_ECN_PERMIT)
4034 tp->t_flags2 |= TF2_ECN_SND_CWR;
4035 break;
4036 case CC_RTO_ERR:
4037 KMOD_TCPSTAT_INC(tcps_sndrexmitbad);
4038 /* RTO was unnecessary, so reset everything. */
4039 tp->snd_cwnd = tp->snd_cwnd_prev;
4040 tp->snd_ssthresh = tp->snd_ssthresh_prev;
4041 tp->snd_recover = tp->snd_recover_prev;
4042 if (tp->t_flags & TF_WASFRECOVERY) {
4043 ENTER_FASTRECOVERY(tp->t_flags);
4044 tp->t_flags &= ~TF_WASFRECOVERY;
4045 }
4046 if (tp->t_flags & TF_WASCRECOVERY) {
4047 ENTER_CONGRECOVERY(tp->t_flags);
4048 tp->t_flags &= ~TF_WASCRECOVERY;
4049 }
4050 tp->snd_nxt = tp->snd_max;
4051 tp->t_badrxtwin = 0;
4052 break;
4053 }
4054 /*
4055 * If we are below our max rtt, don't
4056 * signal the CC control to change things.
4057 * instead set it up so that we are in
4058 * recovery but not going to back off.
4059 */
4060
4061 if (rack->rc_highly_buffered) {
4062 /*
4063 * Do we use the higher rtt for
4064 * our threshold to not backoff (like CDG)?
4065 */
4066 uint32_t rtt_mul, rtt_div;
4067
4068 if (rack_use_max_for_nobackoff) {
4069 rtt_mul = (rack_gp_rtt_maxmul - 1);
4070 rtt_div = 1;
4071 } else {
4072 rtt_mul = rack_gp_rtt_minmul;
4073 rtt_div = max(rack_gp_rtt_mindiv , 1);
4074 }
4075 if (rack->r_ctl.rc_gp_srtt <= (rack->r_ctl.rc_lowest_us_rtt +
4076 ((rack->r_ctl.rc_lowest_us_rtt * rtt_mul) /
4077 rtt_div))) {
4078 /* below our min threshold */
4079 rack->rc_not_backing_off = 1;
4080 ENTER_RECOVERY(rack->rc_tp->t_flags);
4081 rack_log_rtt_shrinks(rack, 0,
4082 rtt_mul,
4083 rtt_div,
4084 RACK_RTTS_NOBACKOFF);
4085 return;
4086 }
4087 }
4088 rack->rc_not_backing_off = 0;
4089 if (CC_ALGO(tp)->cong_signal != NULL) {
4090 if (th != NULL)
4091 tp->ccv->curack = th->th_ack;
4092 CC_ALGO(tp)->cong_signal(tp->ccv, type);
4093 }
4094 }
4095
4096 static inline void
rack_cc_after_idle(struct tcp_rack * rack,struct tcpcb * tp)4097 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp)
4098 {
4099 uint32_t i_cwnd;
4100
4101 INP_WLOCK_ASSERT(tp->t_inpcb);
4102
4103 #ifdef NETFLIX_STATS
4104 KMOD_TCPSTAT_INC(tcps_idle_restarts);
4105 if (tp->t_state == TCPS_ESTABLISHED)
4106 KMOD_TCPSTAT_INC(tcps_idle_estrestarts);
4107 #endif
4108 if (CC_ALGO(tp)->after_idle != NULL)
4109 CC_ALGO(tp)->after_idle(tp->ccv);
4110
4111 if (tp->snd_cwnd == 1)
4112 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */
4113 else
4114 i_cwnd = rc_init_window(rack);
4115
4116 /*
4117 * Being idle is no differnt than the initial window. If the cc
4118 * clamps it down below the initial window raise it to the initial
4119 * window.
4120 */
4121 if (tp->snd_cwnd < i_cwnd) {
4122 tp->snd_cwnd = i_cwnd;
4123 }
4124 }
4125
4126 /*
4127 * Indicate whether this ack should be delayed. We can delay the ack if
4128 * following conditions are met:
4129 * - There is no delayed ack timer in progress.
4130 * - Our last ack wasn't a 0-sized window. We never want to delay
4131 * the ack that opens up a 0-sized window.
4132 * - LRO wasn't used for this segment. We make sure by checking that the
4133 * segment size is not larger than the MSS.
4134 * - Delayed acks are enabled or this is a half-synchronized T/TCP
4135 * connection.
4136 */
4137 #define DELAY_ACK(tp, tlen) \
4138 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
4139 ((tp->t_flags & TF_DELACK) == 0) && \
4140 (tlen <= tp->t_maxseg) && \
4141 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
4142
4143 static struct rack_sendmap *
rack_find_lowest_rsm(struct tcp_rack * rack)4144 rack_find_lowest_rsm(struct tcp_rack *rack)
4145 {
4146 struct rack_sendmap *rsm;
4147
4148 /*
4149 * Walk the time-order transmitted list looking for an rsm that is
4150 * not acked. This will be the one that was sent the longest time
4151 * ago that is still outstanding.
4152 */
4153 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
4154 if (rsm->r_flags & RACK_ACKED) {
4155 continue;
4156 }
4157 goto finish;
4158 }
4159 finish:
4160 return (rsm);
4161 }
4162
4163 static struct rack_sendmap *
rack_find_high_nonack(struct tcp_rack * rack,struct rack_sendmap * rsm)4164 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
4165 {
4166 struct rack_sendmap *prsm;
4167
4168 /*
4169 * Walk the sequence order list backward until we hit and arrive at
4170 * the highest seq not acked. In theory when this is called it
4171 * should be the last segment (which it was not).
4172 */
4173 counter_u64_add(rack_find_high, 1);
4174 prsm = rsm;
4175 RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) {
4176 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
4177 continue;
4178 }
4179 return (prsm);
4180 }
4181 return (NULL);
4182 }
4183
4184 static uint32_t
rack_calc_thresh_rack(struct tcp_rack * rack,uint32_t srtt,uint32_t cts)4185 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
4186 {
4187 int32_t lro;
4188 uint32_t thresh;
4189
4190 /*
4191 * lro is the flag we use to determine if we have seen reordering.
4192 * If it gets set we have seen reordering. The reorder logic either
4193 * works in one of two ways:
4194 *
4195 * If reorder-fade is configured, then we track the last time we saw
4196 * re-ordering occur. If we reach the point where enough time as
4197 * passed we no longer consider reordering has occuring.
4198 *
4199 * Or if reorder-face is 0, then once we see reordering we consider
4200 * the connection to alway be subject to reordering and just set lro
4201 * to 1.
4202 *
4203 * In the end if lro is non-zero we add the extra time for
4204 * reordering in.
4205 */
4206 if (srtt == 0)
4207 srtt = 1;
4208 if (rack->r_ctl.rc_reorder_ts) {
4209 if (rack->r_ctl.rc_reorder_fade) {
4210 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
4211 lro = cts - rack->r_ctl.rc_reorder_ts;
4212 if (lro == 0) {
4213 /*
4214 * No time as passed since the last
4215 * reorder, mark it as reordering.
4216 */
4217 lro = 1;
4218 }
4219 } else {
4220 /* Negative time? */
4221 lro = 0;
4222 }
4223 if (lro > rack->r_ctl.rc_reorder_fade) {
4224 /* Turn off reordering seen too */
4225 rack->r_ctl.rc_reorder_ts = 0;
4226 lro = 0;
4227 }
4228 } else {
4229 /* Reodering does not fade */
4230 lro = 1;
4231 }
4232 } else {
4233 lro = 0;
4234 }
4235 thresh = srtt + rack->r_ctl.rc_pkt_delay;
4236 if (lro) {
4237 /* It must be set, if not you get 1/4 rtt */
4238 if (rack->r_ctl.rc_reorder_shift)
4239 thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
4240 else
4241 thresh += (srtt >> 2);
4242 } else {
4243 thresh += 1;
4244 }
4245 /* We don't let the rack timeout be above a RTO */
4246 if (thresh > TICKS_2_MSEC(rack->rc_tp->t_rxtcur)) {
4247 thresh = TICKS_2_MSEC(rack->rc_tp->t_rxtcur);
4248 }
4249 /* And we don't want it above the RTO max either */
4250 if (thresh > rack_rto_max) {
4251 thresh = rack_rto_max;
4252 }
4253 return (thresh);
4254 }
4255
4256 static uint32_t
rack_calc_thresh_tlp(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t srtt)4257 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
4258 struct rack_sendmap *rsm, uint32_t srtt)
4259 {
4260 struct rack_sendmap *prsm;
4261 uint32_t thresh, len;
4262 int segsiz;
4263
4264 if (srtt == 0)
4265 srtt = 1;
4266 if (rack->r_ctl.rc_tlp_threshold)
4267 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
4268 else
4269 thresh = (srtt * 2);
4270
4271 /* Get the previous sent packet, if any */
4272 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
4273 counter_u64_add(rack_enter_tlp_calc, 1);
4274 len = rsm->r_end - rsm->r_start;
4275 if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
4276 /* Exactly like the ID */
4277 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) {
4278 uint32_t alt_thresh;
4279 /*
4280 * Compensate for delayed-ack with the d-ack time.
4281 */
4282 counter_u64_add(rack_used_tlpmethod, 1);
4283 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
4284 if (alt_thresh > thresh)
4285 thresh = alt_thresh;
4286 }
4287 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
4288 /* 2.1 behavior */
4289 prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
4290 if (prsm && (len <= segsiz)) {
4291 /*
4292 * Two packets outstanding, thresh should be (2*srtt) +
4293 * possible inter-packet delay (if any).
4294 */
4295 uint32_t inter_gap = 0;
4296 int idx, nidx;
4297
4298 counter_u64_add(rack_used_tlpmethod, 1);
4299 idx = rsm->r_rtr_cnt - 1;
4300 nidx = prsm->r_rtr_cnt - 1;
4301 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
4302 /* Yes it was sent later (or at the same time) */
4303 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
4304 }
4305 thresh += inter_gap;
4306 } else if (len <= segsiz) {
4307 /*
4308 * Possibly compensate for delayed-ack.
4309 */
4310 uint32_t alt_thresh;
4311
4312 counter_u64_add(rack_used_tlpmethod2, 1);
4313 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
4314 if (alt_thresh > thresh)
4315 thresh = alt_thresh;
4316 }
4317 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
4318 /* 2.2 behavior */
4319 if (len <= segsiz) {
4320 uint32_t alt_thresh;
4321 /*
4322 * Compensate for delayed-ack with the d-ack time.
4323 */
4324 counter_u64_add(rack_used_tlpmethod, 1);
4325 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
4326 if (alt_thresh > thresh)
4327 thresh = alt_thresh;
4328 }
4329 }
4330 /* Not above an RTO */
4331 if (thresh > TICKS_2_MSEC(tp->t_rxtcur)) {
4332 thresh = TICKS_2_MSEC(tp->t_rxtcur);
4333 }
4334 /* Not above a RTO max */
4335 if (thresh > rack_rto_max) {
4336 thresh = rack_rto_max;
4337 }
4338 /* Apply user supplied min TLP */
4339 if (thresh < rack_tlp_min) {
4340 thresh = rack_tlp_min;
4341 }
4342 return (thresh);
4343 }
4344
4345 static uint32_t
rack_grab_rtt(struct tcpcb * tp,struct tcp_rack * rack)4346 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack)
4347 {
4348 /*
4349 * We want the rack_rtt which is the
4350 * last rtt we measured. However if that
4351 * does not exist we fallback to the srtt (which
4352 * we probably will never do) and then as a last
4353 * resort we use RACK_INITIAL_RTO if no srtt is
4354 * yet set.
4355 */
4356 if (rack->rc_rack_rtt)
4357 return(rack->rc_rack_rtt);
4358 else if (tp->t_srtt == 0)
4359 return(RACK_INITIAL_RTO);
4360 return (TICKS_2_MSEC(tp->t_srtt >> TCP_RTT_SHIFT));
4361 }
4362
4363 static struct rack_sendmap *
rack_check_recovery_mode(struct tcpcb * tp,uint32_t tsused)4364 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
4365 {
4366 /*
4367 * Check to see that we don't need to fall into recovery. We will
4368 * need to do so if our oldest transmit is past the time we should
4369 * have had an ack.
4370 */
4371 struct tcp_rack *rack;
4372 struct rack_sendmap *rsm;
4373 int32_t idx;
4374 uint32_t srtt, thresh;
4375
4376 rack = (struct tcp_rack *)tp->t_fb_ptr;
4377 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
4378 return (NULL);
4379 }
4380 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
4381 if (rsm == NULL)
4382 return (NULL);
4383
4384 if (rsm->r_flags & RACK_ACKED) {
4385 rsm = rack_find_lowest_rsm(rack);
4386 if (rsm == NULL)
4387 return (NULL);
4388 }
4389 idx = rsm->r_rtr_cnt - 1;
4390 srtt = rack_grab_rtt(tp, rack);
4391 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
4392 if (TSTMP_LT(tsused, rsm->r_tim_lastsent[idx])) {
4393 return (NULL);
4394 }
4395 if ((tsused - rsm->r_tim_lastsent[idx]) < thresh) {
4396 return (NULL);
4397 }
4398 /* Ok if we reach here we are over-due and this guy can be sent */
4399 if (IN_RECOVERY(tp->t_flags) == 0) {
4400 /*
4401 * For the one that enters us into recovery record undo
4402 * info.
4403 */
4404 rack->r_ctl.rc_rsm_start = rsm->r_start;
4405 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
4406 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
4407 }
4408 rack_cong_signal(tp, NULL, CC_NDUPACK);
4409 return (rsm);
4410 }
4411
4412 static uint32_t
rack_get_persists_timer_val(struct tcpcb * tp,struct tcp_rack * rack)4413 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
4414 {
4415 int32_t t;
4416 int32_t tt;
4417 uint32_t ret_val;
4418
4419 t = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT) + ((tp->t_rttvar * 4) >> TCP_RTT_SHIFT));
4420 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
4421 rack_persist_min, rack_persist_max);
4422 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
4423 tp->t_rxtshift++;
4424 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
4425 ret_val = (uint32_t)tt;
4426 return (ret_val);
4427 }
4428
4429 static uint32_t
rack_timer_start(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,int sup_rack)4430 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack)
4431 {
4432 /*
4433 * Start the FR timer, we do this based on getting the first one in
4434 * the rc_tmap. Note that if its NULL we must stop the timer. in all
4435 * events we need to stop the running timer (if its running) before
4436 * starting the new one.
4437 */
4438 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse;
4439 uint32_t srtt_cur;
4440 int32_t idx;
4441 int32_t is_tlp_timer = 0;
4442 struct rack_sendmap *rsm;
4443
4444 if (rack->t_timers_stopped) {
4445 /* All timers have been stopped none are to run */
4446 return (0);
4447 }
4448 if (rack->rc_in_persist) {
4449 /* We can't start any timer in persists */
4450 return (rack_get_persists_timer_val(tp, rack));
4451 }
4452 rack->rc_on_min_to = 0;
4453 if ((tp->t_state < TCPS_ESTABLISHED) ||
4454 ((tp->t_flags & TF_SACK_PERMIT) == 0))
4455 goto activate_rxt;
4456 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
4457 if ((rsm == NULL) || sup_rack) {
4458 /* Nothing on the send map */
4459 activate_rxt:
4460 time_since_sent = 0;
4461 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
4462 if (rsm) {
4463 idx = rsm->r_rtr_cnt - 1;
4464 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], rack->r_ctl.rc_tlp_rxt_last_time))
4465 tstmp_touse = rsm->r_tim_lastsent[idx];
4466 else
4467 tstmp_touse = rack->r_ctl.rc_tlp_rxt_last_time;
4468 if (TSTMP_GT(cts, tstmp_touse))
4469 time_since_sent = cts - tstmp_touse;
4470 }
4471 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
4472 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
4473 to = TICKS_2_MSEC(tp->t_rxtcur);
4474 if (to > time_since_sent)
4475 to -= time_since_sent;
4476 else
4477 to = rack->r_ctl.rc_min_to;
4478 if (to == 0)
4479 to = 1;
4480 return (to);
4481 }
4482 return (0);
4483 }
4484 if (rsm->r_flags & RACK_ACKED) {
4485 rsm = rack_find_lowest_rsm(rack);
4486 if (rsm == NULL) {
4487 /* No lowest? */
4488 goto activate_rxt;
4489 }
4490 }
4491 if (rack->sack_attack_disable) {
4492 /*
4493 * We don't want to do
4494 * any TLP's if you are an attacker.
4495 * Though if you are doing what
4496 * is expected you may still have
4497 * SACK-PASSED marks.
4498 */
4499 goto activate_rxt;
4500 }
4501 /* Convert from ms to usecs */
4502 if ((rsm->r_flags & RACK_SACK_PASSED) || (rsm->r_dupack >= DUP_ACK_THRESHOLD)) {
4503 if ((tp->t_flags & TF_SENTFIN) &&
4504 ((tp->snd_max - tp->snd_una) == 1) &&
4505 (rsm->r_flags & RACK_HAS_FIN)) {
4506 /*
4507 * We don't start a rack timer if all we have is a
4508 * FIN outstanding.
4509 */
4510 goto activate_rxt;
4511 }
4512 if ((rack->use_rack_rr == 0) &&
4513 (IN_RECOVERY(tp->t_flags)) &&
4514 (rack->rack_no_prr == 0) &&
4515 (rack->r_ctl.rc_prr_sndcnt < ctf_fixed_maxseg(tp))) {
4516 /*
4517 * We are not cheating, in recovery and
4518 * not enough ack's to yet get our next
4519 * retransmission out.
4520 *
4521 * Note that classified attackers do not
4522 * get to use the rack-cheat.
4523 */
4524 goto activate_tlp;
4525 }
4526 srtt = rack_grab_rtt(tp, rack);
4527 thresh = rack_calc_thresh_rack(rack, srtt, cts);
4528 idx = rsm->r_rtr_cnt - 1;
4529 exp = rsm->r_tim_lastsent[idx] + thresh;
4530 if (SEQ_GEQ(exp, cts)) {
4531 to = exp - cts;
4532 if (to < rack->r_ctl.rc_min_to) {
4533 to = rack->r_ctl.rc_min_to;
4534 if (rack->r_rr_config == 3)
4535 rack->rc_on_min_to = 1;
4536 }
4537 } else {
4538 to = rack->r_ctl.rc_min_to;
4539 if (rack->r_rr_config == 3)
4540 rack->rc_on_min_to = 1;
4541 }
4542 } else {
4543 /* Ok we need to do a TLP not RACK */
4544 activate_tlp:
4545 if ((rack->rc_tlp_in_progress != 0) &&
4546 (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) {
4547 /*
4548 * The previous send was a TLP and we have sent
4549 * N TLP's without sending new data.
4550 */
4551 goto activate_rxt;
4552 }
4553 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
4554 if (rsm == NULL) {
4555 /* We found no rsm to TLP with. */
4556 goto activate_rxt;
4557 }
4558 if (rsm->r_flags & RACK_HAS_FIN) {
4559 /* If its a FIN we dont do TLP */
4560 rsm = NULL;
4561 goto activate_rxt;
4562 }
4563 idx = rsm->r_rtr_cnt - 1;
4564 time_since_sent = 0;
4565 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], rack->r_ctl.rc_tlp_rxt_last_time))
4566 tstmp_touse = rsm->r_tim_lastsent[idx];
4567 else
4568 tstmp_touse = rack->r_ctl.rc_tlp_rxt_last_time;
4569 if (TSTMP_GT(cts, tstmp_touse))
4570 time_since_sent = cts - tstmp_touse;
4571 is_tlp_timer = 1;
4572 if (tp->t_srtt) {
4573 srtt_cur = (tp->t_srtt >> TCP_RTT_SHIFT);
4574 srtt = TICKS_2_MSEC(srtt_cur);
4575 } else
4576 srtt = RACK_INITIAL_RTO;
4577 /*
4578 * If the SRTT is not keeping up and the
4579 * rack RTT has spiked we want to use
4580 * the last RTT not the smoothed one.
4581 */
4582 if (rack_tlp_use_greater && (srtt < rack_grab_rtt(tp, rack)))
4583 srtt = rack_grab_rtt(tp, rack);
4584 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
4585 if (thresh > time_since_sent)
4586 to = thresh - time_since_sent;
4587 else {
4588 to = rack->r_ctl.rc_min_to;
4589 rack_log_alt_to_to_cancel(rack,
4590 thresh, /* flex1 */
4591 time_since_sent, /* flex2 */
4592 tstmp_touse, /* flex3 */
4593 rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */
4594 rsm->r_tim_lastsent[idx],
4595 srtt,
4596 idx, 99);
4597 }
4598 if (to > TCPTV_REXMTMAX) {
4599 /*
4600 * If the TLP time works out to larger than the max
4601 * RTO lets not do TLP.. just RTO.
4602 */
4603 goto activate_rxt;
4604 }
4605 }
4606 if (is_tlp_timer == 0) {
4607 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
4608 } else {
4609 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
4610 }
4611 if (to == 0)
4612 to = 1;
4613 return (to);
4614 }
4615
4616 static void
rack_enter_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)4617 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
4618 {
4619 if (rack->rc_in_persist == 0) {
4620 if (tp->t_flags & TF_GPUTINPROG) {
4621 /*
4622 * Stop the goodput now, the calling of the
4623 * measurement function clears the flag.
4624 */
4625 rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__);
4626 }
4627 #ifdef NETFLIX_SHARED_CWND
4628 if (rack->r_ctl.rc_scw) {
4629 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
4630 rack->rack_scwnd_is_idle = 1;
4631 }
4632 #endif
4633 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
4634 if (rack->r_ctl.rc_went_idle_time == 0)
4635 rack->r_ctl.rc_went_idle_time = 1;
4636 rack_timer_cancel(tp, rack, cts, __LINE__);
4637 tp->t_rxtshift = 0;
4638 rack->rc_in_persist = 1;
4639 }
4640 }
4641
4642 static void
rack_exit_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)4643 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
4644 {
4645 if (rack->rc_inp->inp_in_hpts) {
4646 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
4647 rack->r_ctl.rc_hpts_flags = 0;
4648 }
4649 #ifdef NETFLIX_SHARED_CWND
4650 if (rack->r_ctl.rc_scw) {
4651 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
4652 rack->rack_scwnd_is_idle = 0;
4653 }
4654 #endif
4655 if (rack->rc_gp_dyn_mul &&
4656 (rack->use_fixed_rate == 0) &&
4657 (rack->rc_always_pace)) {
4658 /*
4659 * Do we count this as if a probe-rtt just
4660 * finished?
4661 */
4662 uint32_t time_idle, idle_min;
4663
4664 time_idle = tcp_get_usecs(NULL) - rack->r_ctl.rc_went_idle_time;
4665 idle_min = rack_min_probertt_hold;
4666 if (rack_probertt_gpsrtt_cnt_div) {
4667 uint64_t extra;
4668 extra = (uint64_t)rack->r_ctl.rc_gp_srtt *
4669 (uint64_t)rack_probertt_gpsrtt_cnt_mul;
4670 extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div;
4671 idle_min += (uint32_t)extra;
4672 }
4673 if (time_idle >= idle_min) {
4674 /* Yes, we count it as a probe-rtt. */
4675 uint32_t us_cts;
4676
4677 us_cts = tcp_get_usecs(NULL);
4678 if (rack->in_probe_rtt == 0) {
4679 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
4680 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
4681 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
4682 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
4683 } else {
4684 rack_exit_probertt(rack, us_cts);
4685 }
4686 }
4687 }
4688 rack->rc_in_persist = 0;
4689 rack->r_ctl.rc_went_idle_time = 0;
4690 tp->t_rxtshift = 0;
4691 rack->r_ctl.rc_agg_delayed = 0;
4692 rack->r_early = 0;
4693 rack->r_late = 0;
4694 rack->r_ctl.rc_agg_early = 0;
4695 }
4696
4697 static void
rack_log_hpts_diag(struct tcp_rack * rack,uint32_t cts,struct hpts_diag * diag,struct timeval * tv)4698 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts,
4699 struct hpts_diag *diag, struct timeval *tv)
4700 {
4701 if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
4702 union tcp_log_stackspecific log;
4703
4704 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4705 log.u_bbr.flex1 = diag->p_nxt_slot;
4706 log.u_bbr.flex2 = diag->p_cur_slot;
4707 log.u_bbr.flex3 = diag->slot_req;
4708 log.u_bbr.flex4 = diag->inp_hptsslot;
4709 log.u_bbr.flex5 = diag->slot_remaining;
4710 log.u_bbr.flex6 = diag->need_new_to;
4711 log.u_bbr.flex7 = diag->p_hpts_active;
4712 log.u_bbr.flex8 = diag->p_on_min_sleep;
4713 /* Hijack other fields as needed */
4714 log.u_bbr.epoch = diag->have_slept;
4715 log.u_bbr.lt_epoch = diag->yet_to_sleep;
4716 log.u_bbr.pkts_out = diag->co_ret;
4717 log.u_bbr.applimited = diag->hpts_sleep_time;
4718 log.u_bbr.delivered = diag->p_prev_slot;
4719 log.u_bbr.inflight = diag->p_runningtick;
4720 log.u_bbr.bw_inuse = diag->wheel_tick;
4721 log.u_bbr.rttProp = diag->wheel_cts;
4722 log.u_bbr.timeStamp = cts;
4723 log.u_bbr.delRate = diag->maxticks;
4724 log.u_bbr.cur_del_rate = diag->p_curtick;
4725 log.u_bbr.cur_del_rate <<= 32;
4726 log.u_bbr.cur_del_rate |= diag->p_lasttick;
4727 TCP_LOG_EVENTP(rack->rc_tp, NULL,
4728 &rack->rc_inp->inp_socket->so_rcv,
4729 &rack->rc_inp->inp_socket->so_snd,
4730 BBR_LOG_HPTSDIAG, 0,
4731 0, &log, false, tv);
4732 }
4733
4734 }
4735
4736 static void
rack_start_hpts_timer(struct tcp_rack * rack,struct tcpcb * tp,uint32_t cts,int32_t slot,uint32_t tot_len_this_send,int sup_rack)4737 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts,
4738 int32_t slot, uint32_t tot_len_this_send, int sup_rack)
4739 {
4740 struct hpts_diag diag;
4741 struct inpcb *inp;
4742 struct timeval tv;
4743 uint32_t delayed_ack = 0;
4744 uint32_t hpts_timeout;
4745 uint8_t stopped;
4746 uint32_t left = 0;
4747 uint32_t us_cts;
4748
4749 inp = tp->t_inpcb;
4750 if ((tp->t_state == TCPS_CLOSED) ||
4751 (tp->t_state == TCPS_LISTEN)) {
4752 return;
4753 }
4754 if (inp->inp_in_hpts) {
4755 /* Already on the pacer */
4756 return;
4757 }
4758 stopped = rack->rc_tmr_stopped;
4759 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
4760 left = rack->r_ctl.rc_timer_exp - cts;
4761 }
4762 rack->r_ctl.rc_timer_exp = 0;
4763 rack->r_ctl.rc_hpts_flags = 0;
4764 us_cts = tcp_get_usecs(&tv);
4765 /* Now early/late accounting */
4766 if (rack->r_early) {
4767 /*
4768 * We have a early carry over set,
4769 * we can always add more time so we
4770 * can always make this compensation.
4771 */
4772 slot += rack->r_ctl.rc_agg_early;
4773 rack->r_early = 0;
4774 rack->r_ctl.rc_agg_early = 0;
4775 }
4776 if (rack->r_late) {
4777 /*
4778 * This is harder, we can
4779 * compensate some but it
4780 * really depends on what
4781 * the current pacing time is.
4782 */
4783 if (rack->r_ctl.rc_agg_delayed >= slot) {
4784 /*
4785 * We can't compensate for it all.
4786 * And we have to have some time
4787 * on the clock. We always have a min
4788 * 10 slots (10 x 10 i.e. 100 usecs).
4789 */
4790 if (slot <= HPTS_TICKS_PER_USEC) {
4791 /* We gain delay */
4792 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_USEC - slot);
4793 slot = HPTS_TICKS_PER_USEC;
4794 } else {
4795 /* We take off some */
4796 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_USEC);
4797 slot = HPTS_TICKS_PER_USEC;
4798 }
4799 } else {
4800 slot -= rack->r_ctl.rc_agg_delayed;
4801 rack->r_ctl.rc_agg_delayed = 0;
4802 /* Make sure we have 100 useconds at minimum */
4803 if (slot < HPTS_TICKS_PER_USEC) {
4804 rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_USEC - slot;
4805 slot = HPTS_TICKS_PER_USEC;
4806 }
4807 if (rack->r_ctl.rc_agg_delayed == 0)
4808 rack->r_late = 0;
4809 }
4810 }
4811 if (slot) {
4812 /* We are pacing too */
4813 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
4814 }
4815 hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack);
4816 #ifdef NETFLIX_EXP_DETECTION
4817 if (rack->sack_attack_disable &&
4818 (slot < tcp_sad_pacing_interval)) {
4819 /*
4820 * We have a potential attacker on
4821 * the line. We have possibly some
4822 * (or now) pacing time set. We want to
4823 * slow down the processing of sacks by some
4824 * amount (if it is an attacker). Set the default
4825 * slot for attackers in place (unless the orginal
4826 * interval is longer). Its stored in
4827 * micro-seconds, so lets convert to msecs.
4828 */
4829 slot = tcp_sad_pacing_interval;
4830 }
4831 #endif
4832 if (tp->t_flags & TF_DELACK) {
4833 delayed_ack = TICKS_2_MSEC(tcp_delacktime);
4834 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
4835 }
4836 if (delayed_ack && ((hpts_timeout == 0) ||
4837 (delayed_ack < hpts_timeout)))
4838 hpts_timeout = delayed_ack;
4839 else
4840 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
4841 /*
4842 * If no timers are going to run and we will fall off the hptsi
4843 * wheel, we resort to a keep-alive timer if its configured.
4844 */
4845 if ((hpts_timeout == 0) &&
4846 (slot == 0)) {
4847 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4848 (tp->t_state <= TCPS_CLOSING)) {
4849 /*
4850 * Ok we have no timer (persists, rack, tlp, rxt or
4851 * del-ack), we don't have segments being paced. So
4852 * all that is left is the keepalive timer.
4853 */
4854 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
4855 /* Get the established keep-alive time */
4856 hpts_timeout = TP_KEEPIDLE(tp);
4857 } else {
4858 /* Get the initial setup keep-alive time */
4859 hpts_timeout = TP_KEEPINIT(tp);
4860 }
4861 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
4862 if (rack->in_probe_rtt) {
4863 /*
4864 * We want to instead not wake up a long time from
4865 * now but to wake up about the time we would
4866 * exit probe-rtt and initiate a keep-alive ack.
4867 * This will get us out of probe-rtt and update
4868 * our min-rtt.
4869 */
4870 hpts_timeout = (rack_min_probertt_hold / HPTS_USEC_IN_MSEC);
4871 }
4872 }
4873 }
4874 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
4875 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
4876 /*
4877 * RACK, TLP, persists and RXT timers all are restartable
4878 * based on actions input .. i.e we received a packet (ack
4879 * or sack) and that changes things (rw, or snd_una etc).
4880 * Thus we can restart them with a new value. For
4881 * keep-alive, delayed_ack we keep track of what was left
4882 * and restart the timer with a smaller value.
4883 */
4884 if (left < hpts_timeout)
4885 hpts_timeout = left;
4886 }
4887 if (hpts_timeout) {
4888 /*
4889 * Hack alert for now we can't time-out over 2,147,483
4890 * seconds (a bit more than 596 hours), which is probably ok
4891 * :).
4892 */
4893 if (hpts_timeout > 0x7ffffffe)
4894 hpts_timeout = 0x7ffffffe;
4895 rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
4896 }
4897 if ((rack->rc_gp_filled == 0) &&
4898 (hpts_timeout < slot) &&
4899 (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) {
4900 /*
4901 * We have no good estimate yet for the
4902 * old clunky burst mitigation or the
4903 * real pacing. And the tlp or rxt is smaller
4904 * than the pacing calculation. Lets not
4905 * pace that long since we know the calculation
4906 * so far is not accurate.
4907 */
4908 slot = hpts_timeout;
4909 }
4910 rack->r_ctl.last_pacing_time = slot;
4911 if (slot) {
4912 rack->r_ctl.rc_last_output_to = us_cts + slot;
4913 if (rack->rc_always_pace || rack->r_mbuf_queue) {
4914 if ((rack->rc_gp_filled == 0) ||
4915 rack->pacing_longer_than_rtt) {
4916 inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY);
4917 } else {
4918 inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
4919 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) &&
4920 (rack->r_rr_config != 3))
4921 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
4922 else
4923 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
4924 }
4925 }
4926 if ((rack->use_rack_rr) &&
4927 (rack->r_rr_config < 2) &&
4928 ((hpts_timeout) && ((hpts_timeout * HPTS_USEC_IN_MSEC) < slot))) {
4929 /*
4930 * Arrange for the hpts to kick back in after the
4931 * t-o if the t-o does not cause a send.
4932 */
4933 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout),
4934 __LINE__, &diag);
4935 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
4936 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
4937 } else {
4938 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot),
4939 __LINE__, &diag);
4940 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
4941 rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
4942 }
4943 } else if (hpts_timeout) {
4944 if (rack->rc_always_pace || rack->r_mbuf_queue) {
4945 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) {
4946 /* For a rack timer, don't wake us */
4947 inp->inp_flags2 |= INP_MBUF_QUEUE_READY;
4948 if (rack->r_rr_config != 3)
4949 inp->inp_flags2 |= INP_DONT_SACK_QUEUE;
4950 else
4951 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
4952 } else {
4953 /* All other timers wake us up */
4954 inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
4955 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
4956 }
4957 }
4958 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout),
4959 __LINE__, &diag);
4960 rack_log_hpts_diag(rack, us_cts, &diag, &tv);
4961 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
4962 } else {
4963 /* No timer starting */
4964 #ifdef INVARIANTS
4965 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
4966 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
4967 tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
4968 }
4969 #endif
4970 }
4971 rack->rc_tmr_stopped = 0;
4972 if (slot)
4973 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv);
4974 }
4975
4976 /*
4977 * RACK Timer, here we simply do logging and house keeping.
4978 * the normal rack_output() function will call the
4979 * appropriate thing to check if we need to do a RACK retransmit.
4980 * We return 1, saying don't proceed with rack_output only
4981 * when all timers have been stopped (destroyed PCB?).
4982 */
4983 static int
rack_timeout_rack(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)4984 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
4985 {
4986 /*
4987 * This timer simply provides an internal trigger to send out data.
4988 * The check_recovery_mode call will see if there are needed
4989 * retransmissions, if so we will enter fast-recovery. The output
4990 * call may or may not do the same thing depending on sysctl
4991 * settings.
4992 */
4993 struct rack_sendmap *rsm;
4994 int32_t recovery;
4995
4996 if (tp->t_timers->tt_flags & TT_STOPPED) {
4997 return (1);
4998 }
4999 recovery = IN_RECOVERY(tp->t_flags);
5000 counter_u64_add(rack_to_tot, 1);
5001 if (rack->r_state && (rack->r_state != tp->t_state))
5002 rack_set_state(tp, rack);
5003 rack->rc_on_min_to = 0;
5004 rsm = rack_check_recovery_mode(tp, cts);
5005 rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm);
5006 if (rsm) {
5007 uint32_t rtt;
5008
5009 rack->r_ctl.rc_resend = rsm;
5010 if (rack->use_rack_rr) {
5011 /*
5012 * Don't accumulate extra pacing delay
5013 * we are allowing the rack timer to
5014 * over-ride pacing i.e. rrr takes precedence
5015 * if the pacing interval is longer than the rrr
5016 * time (in other words we get the min pacing
5017 * time versus rrr pacing time).
5018 */
5019 rack->r_timer_override = 1;
5020 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
5021 }
5022 rtt = rack->rc_rack_rtt;
5023 if (rtt == 0)
5024 rtt = 1;
5025 if (rack->rack_no_prr == 0) {
5026 if ((recovery == 0) &&
5027 (rack->r_ctl.rc_prr_sndcnt < ctf_fixed_maxseg(tp))) {
5028 /*
5029 * The rack-timeout that enter's us into recovery
5030 * will force out one MSS and set us up so that we
5031 * can do one more send in 2*rtt (transitioning the
5032 * rack timeout into a rack-tlp).
5033 */
5034 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5035 rack->r_timer_override = 1;
5036 rack_log_to_prr(rack, 3, 0);
5037 } else if ((rack->r_ctl.rc_prr_sndcnt < (rsm->r_end - rsm->r_start)) &&
5038 rack->use_rack_rr) {
5039 /*
5040 * When a rack timer goes, if the rack rr is
5041 * on, arrange it so we can send a full segment
5042 * overriding prr (though we pay a price for this
5043 * for future new sends).
5044 */
5045 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
5046 rack_log_to_prr(rack, 4, 0);
5047 }
5048 }
5049 }
5050 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
5051 if (rsm == NULL) {
5052 /* restart a timer and return 1 */
5053 rack_start_hpts_timer(rack, tp, cts,
5054 0, 0, 0);
5055 return (1);
5056 }
5057 return (0);
5058 }
5059
5060 static __inline void
rack_clone_rsm(struct tcp_rack * rack,struct rack_sendmap * nrsm,struct rack_sendmap * rsm,uint32_t start)5061 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm,
5062 struct rack_sendmap *rsm, uint32_t start)
5063 {
5064 int idx;
5065
5066 nrsm->r_start = start;
5067 nrsm->r_end = rsm->r_end;
5068 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
5069 nrsm->r_flags = rsm->r_flags;
5070 nrsm->r_dupack = rsm->r_dupack;
5071 nrsm->usec_orig_send = rsm->usec_orig_send;
5072 nrsm->r_rtr_bytes = 0;
5073 rsm->r_end = nrsm->r_start;
5074 nrsm->r_just_ret = rsm->r_just_ret;
5075 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
5076 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
5077 }
5078 }
5079
5080 static struct rack_sendmap *
rack_merge_rsm(struct tcp_rack * rack,struct rack_sendmap * l_rsm,struct rack_sendmap * r_rsm)5081 rack_merge_rsm(struct tcp_rack *rack,
5082 struct rack_sendmap *l_rsm,
5083 struct rack_sendmap *r_rsm)
5084 {
5085 /*
5086 * We are merging two ack'd RSM's,
5087 * the l_rsm is on the left (lower seq
5088 * values) and the r_rsm is on the right
5089 * (higher seq value). The simplest way
5090 * to merge these is to move the right
5091 * one into the left. I don't think there
5092 * is any reason we need to try to find
5093 * the oldest (or last oldest retransmitted).
5094 */
5095 struct rack_sendmap *rm;
5096
5097 l_rsm->r_end = r_rsm->r_end;
5098 if (l_rsm->r_dupack < r_rsm->r_dupack)
5099 l_rsm->r_dupack = r_rsm->r_dupack;
5100 if (r_rsm->r_rtr_bytes)
5101 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes;
5102 if (r_rsm->r_in_tmap) {
5103 /* This really should not happen */
5104 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext);
5105 r_rsm->r_in_tmap = 0;
5106 }
5107
5108 /* Now the flags */
5109 if (r_rsm->r_flags & RACK_HAS_FIN)
5110 l_rsm->r_flags |= RACK_HAS_FIN;
5111 if (r_rsm->r_flags & RACK_TLP)
5112 l_rsm->r_flags |= RACK_TLP;
5113 if (r_rsm->r_flags & RACK_RWND_COLLAPSED)
5114 l_rsm->r_flags |= RACK_RWND_COLLAPSED;
5115 if ((r_rsm->r_flags & RACK_APP_LIMITED) &&
5116 ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) {
5117 /*
5118 * If both are app-limited then let the
5119 * free lower the count. If right is app
5120 * limited and left is not, transfer.
5121 */
5122 l_rsm->r_flags |= RACK_APP_LIMITED;
5123 r_rsm->r_flags &= ~RACK_APP_LIMITED;
5124 if (r_rsm == rack->r_ctl.rc_first_appl)
5125 rack->r_ctl.rc_first_appl = l_rsm;
5126 }
5127 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm);
5128 #ifdef INVARIANTS
5129 if (rm != r_rsm) {
5130 panic("removing head in rack:%p rsm:%p rm:%p",
5131 rack, r_rsm, rm);
5132 }
5133 #endif
5134 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) {
5135 /* Transfer the split limit to the map we free */
5136 r_rsm->r_limit_type = l_rsm->r_limit_type;
5137 l_rsm->r_limit_type = 0;
5138 }
5139 rack_free(rack, r_rsm);
5140 return(l_rsm);
5141 }
5142
5143 /*
5144 * TLP Timer, here we simply setup what segment we want to
5145 * have the TLP expire on, the normal rack_output() will then
5146 * send it out.
5147 *
5148 * We return 1, saying don't proceed with rack_output only
5149 * when all timers have been stopped (destroyed PCB?).
5150 */
5151 static int
rack_timeout_tlp(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)5152 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5153 {
5154 /*
5155 * Tail Loss Probe.
5156 */
5157 struct rack_sendmap *rsm = NULL;
5158 struct rack_sendmap *insret;
5159 struct socket *so;
5160 uint32_t amm, old_prr_snd = 0;
5161 uint32_t out, avail;
5162 int collapsed_win = 0;
5163
5164 if (tp->t_timers->tt_flags & TT_STOPPED) {
5165 return (1);
5166 }
5167 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
5168 /* Its not time yet */
5169 return (0);
5170 }
5171 if (ctf_progress_timeout_check(tp, true)) {
5172 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
5173 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
5174 return (1);
5175 }
5176 /*
5177 * A TLP timer has expired. We have been idle for 2 rtts. So we now
5178 * need to figure out how to force a full MSS segment out.
5179 */
5180 rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL);
5181 counter_u64_add(rack_tlp_tot, 1);
5182 if (rack->r_state && (rack->r_state != tp->t_state))
5183 rack_set_state(tp, rack);
5184 so = tp->t_inpcb->inp_socket;
5185 avail = sbavail(&so->so_snd);
5186 out = tp->snd_max - tp->snd_una;
5187 if (out > tp->snd_wnd) {
5188 /* special case, we need a retransmission */
5189 collapsed_win = 1;
5190 goto need_retran;
5191 }
5192 /*
5193 * Check our send oldest always settings, and if
5194 * there is an oldest to send jump to the need_retran.
5195 */
5196 if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0))
5197 goto need_retran;
5198
5199 if (avail > out) {
5200 /* New data is available */
5201 amm = avail - out;
5202 if (amm > ctf_fixed_maxseg(tp)) {
5203 amm = ctf_fixed_maxseg(tp);
5204 if ((amm + out) > tp->snd_wnd) {
5205 /* We are rwnd limited */
5206 goto need_retran;
5207 }
5208 } else if (amm < ctf_fixed_maxseg(tp)) {
5209 /* not enough to fill a MTU */
5210 goto need_retran;
5211 }
5212 if (IN_RECOVERY(tp->t_flags)) {
5213 /* Unlikely */
5214 if (rack->rack_no_prr == 0) {
5215 old_prr_snd = rack->r_ctl.rc_prr_sndcnt;
5216 if (out + amm <= tp->snd_wnd) {
5217 rack->r_ctl.rc_prr_sndcnt = amm;
5218 rack_log_to_prr(rack, 4, 0);
5219 }
5220 } else
5221 goto need_retran;
5222 } else {
5223 /* Set the send-new override */
5224 if (out + amm <= tp->snd_wnd)
5225 rack->r_ctl.rc_tlp_new_data = amm;
5226 else
5227 goto need_retran;
5228 }
5229 rack->r_ctl.rc_tlpsend = NULL;
5230 counter_u64_add(rack_tlp_newdata, 1);
5231 goto send;
5232 }
5233 need_retran:
5234 /*
5235 * Ok we need to arrange the last un-acked segment to be re-sent, or
5236 * optionally the first un-acked segment.
5237 */
5238 if (collapsed_win == 0) {
5239 if (rack_always_send_oldest)
5240 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
5241 else {
5242 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
5243 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
5244 rsm = rack_find_high_nonack(rack, rsm);
5245 }
5246 }
5247 if (rsm == NULL) {
5248 counter_u64_add(rack_tlp_does_nada, 1);
5249 #ifdef TCP_BLACKBOX
5250 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
5251 #endif
5252 goto out;
5253 }
5254 } else {
5255 /*
5256 * We must find the last segment
5257 * that was acceptable by the client.
5258 */
5259 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
5260 if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) {
5261 /* Found one */
5262 break;
5263 }
5264 }
5265 if (rsm == NULL) {
5266 /* None? if so send the first */
5267 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
5268 if (rsm == NULL) {
5269 counter_u64_add(rack_tlp_does_nada, 1);
5270 #ifdef TCP_BLACKBOX
5271 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
5272 #endif
5273 goto out;
5274 }
5275 }
5276 }
5277 if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) {
5278 /*
5279 * We need to split this the last segment in two.
5280 */
5281 struct rack_sendmap *nrsm;
5282
5283 nrsm = rack_alloc_full_limit(rack);
5284 if (nrsm == NULL) {
5285 /*
5286 * No memory to split, we will just exit and punt
5287 * off to the RXT timer.
5288 */
5289 counter_u64_add(rack_tlp_does_nada, 1);
5290 goto out;
5291 }
5292 rack_clone_rsm(rack, nrsm, rsm,
5293 (rsm->r_end - ctf_fixed_maxseg(tp)));
5294 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
5295 #ifdef INVARIANTS
5296 if (insret != NULL) {
5297 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
5298 nrsm, insret, rack, rsm);
5299 }
5300 #endif
5301 if (rsm->r_in_tmap) {
5302 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
5303 nrsm->r_in_tmap = 1;
5304 }
5305 rsm->r_flags &= (~RACK_HAS_FIN);
5306 rsm = nrsm;
5307 }
5308 rack->r_ctl.rc_tlpsend = rsm;
5309 send:
5310 rack->r_timer_override = 1;
5311 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
5312 return (0);
5313 out:
5314 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
5315 return (0);
5316 }
5317
5318 /*
5319 * Delayed ack Timer, here we simply need to setup the
5320 * ACK_NOW flag and remove the DELACK flag. From there
5321 * the output routine will send the ack out.
5322 *
5323 * We only return 1, saying don't proceed, if all timers
5324 * are stopped (destroyed PCB?).
5325 */
5326 static int
rack_timeout_delack(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)5327 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5328 {
5329 if (tp->t_timers->tt_flags & TT_STOPPED) {
5330 return (1);
5331 }
5332 rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL);
5333 tp->t_flags &= ~TF_DELACK;
5334 tp->t_flags |= TF_ACKNOW;
5335 KMOD_TCPSTAT_INC(tcps_delack);
5336 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
5337 return (0);
5338 }
5339
5340 /*
5341 * Persists timer, here we simply send the
5342 * same thing as a keepalive will.
5343 * the one byte send.
5344 *
5345 * We only return 1, saying don't proceed, if all timers
5346 * are stopped (destroyed PCB?).
5347 */
5348 static int
rack_timeout_persist(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)5349 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5350 {
5351 struct tcptemp *t_template;
5352 struct inpcb *inp;
5353 int32_t retval = 1;
5354
5355 inp = tp->t_inpcb;
5356
5357 if (tp->t_timers->tt_flags & TT_STOPPED) {
5358 return (1);
5359 }
5360 if (rack->rc_in_persist == 0)
5361 return (0);
5362 if (ctf_progress_timeout_check(tp, false)) {
5363 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
5364 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
5365 tcp_set_inp_to_drop(inp, ETIMEDOUT);
5366 return (1);
5367 }
5368 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
5369 /*
5370 * Persistence timer into zero window. Force a byte to be output, if
5371 * possible.
5372 */
5373 KMOD_TCPSTAT_INC(tcps_persisttimeo);
5374 /*
5375 * Hack: if the peer is dead/unreachable, we do not time out if the
5376 * window is closed. After a full backoff, drop the connection if
5377 * the idle time (no responses to probes) reaches the maximum
5378 * backoff that we would use if retransmitting.
5379 */
5380 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
5381 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
5382 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
5383 KMOD_TCPSTAT_INC(tcps_persistdrop);
5384 retval = 1;
5385 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
5386 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
5387 goto out;
5388 }
5389 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
5390 tp->snd_una == tp->snd_max)
5391 rack_exit_persist(tp, rack, cts);
5392 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
5393 /*
5394 * If the user has closed the socket then drop a persisting
5395 * connection after a much reduced timeout.
5396 */
5397 if (tp->t_state > TCPS_CLOSE_WAIT &&
5398 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
5399 retval = 1;
5400 KMOD_TCPSTAT_INC(tcps_persistdrop);
5401 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
5402 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
5403 goto out;
5404 }
5405 t_template = tcpip_maketemplate(rack->rc_inp);
5406 if (t_template) {
5407 /* only set it if we were answered */
5408 if (rack->forced_ack == 0) {
5409 rack->forced_ack = 1;
5410 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
5411 }
5412 tcp_respond(tp, t_template->tt_ipgen,
5413 &t_template->tt_t, (struct mbuf *)NULL,
5414 tp->rcv_nxt, tp->snd_una - 1, 0);
5415 /* This sends an ack */
5416 if (tp->t_flags & TF_DELACK)
5417 tp->t_flags &= ~TF_DELACK;
5418 free(t_template, M_TEMP);
5419 }
5420 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
5421 tp->t_rxtshift++;
5422 out:
5423 rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL);
5424 rack_start_hpts_timer(rack, tp, cts,
5425 0, 0, 0);
5426 return (retval);
5427 }
5428
5429 /*
5430 * If a keepalive goes off, we had no other timers
5431 * happening. We always return 1 here since this
5432 * routine either drops the connection or sends
5433 * out a segment with respond.
5434 */
5435 static int
rack_timeout_keepalive(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)5436 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5437 {
5438 struct tcptemp *t_template;
5439 struct inpcb *inp;
5440
5441 if (tp->t_timers->tt_flags & TT_STOPPED) {
5442 return (1);
5443 }
5444 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
5445 inp = tp->t_inpcb;
5446 rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL);
5447 /*
5448 * Keep-alive timer went off; send something or drop connection if
5449 * idle for too long.
5450 */
5451 KMOD_TCPSTAT_INC(tcps_keeptimeo);
5452 if (tp->t_state < TCPS_ESTABLISHED)
5453 goto dropit;
5454 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
5455 tp->t_state <= TCPS_CLOSING) {
5456 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
5457 goto dropit;
5458 /*
5459 * Send a packet designed to force a response if the peer is
5460 * up and reachable: either an ACK if the connection is
5461 * still alive, or an RST if the peer has closed the
5462 * connection due to timeout or reboot. Using sequence
5463 * number tp->snd_una-1 causes the transmitted zero-length
5464 * segment to lie outside the receive window; by the
5465 * protocol spec, this requires the correspondent TCP to
5466 * respond.
5467 */
5468 KMOD_TCPSTAT_INC(tcps_keepprobe);
5469 t_template = tcpip_maketemplate(inp);
5470 if (t_template) {
5471 if (rack->forced_ack == 0) {
5472 rack->forced_ack = 1;
5473 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL);
5474 }
5475 tcp_respond(tp, t_template->tt_ipgen,
5476 &t_template->tt_t, (struct mbuf *)NULL,
5477 tp->rcv_nxt, tp->snd_una - 1, 0);
5478 free(t_template, M_TEMP);
5479 }
5480 }
5481 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
5482 return (1);
5483 dropit:
5484 KMOD_TCPSTAT_INC(tcps_keepdrops);
5485 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
5486 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
5487 return (1);
5488 }
5489
5490 /*
5491 * Retransmit helper function, clear up all the ack
5492 * flags and take care of important book keeping.
5493 */
5494 static void
rack_remxt_tmr(struct tcpcb * tp)5495 rack_remxt_tmr(struct tcpcb *tp)
5496 {
5497 /*
5498 * The retransmit timer went off, all sack'd blocks must be
5499 * un-acked.
5500 */
5501 struct rack_sendmap *rsm, *trsm = NULL;
5502 struct tcp_rack *rack;
5503 int32_t cnt = 0;
5504
5505 rack = (struct tcp_rack *)tp->t_fb_ptr;
5506 rack_timer_cancel(tp, rack, tcp_ts_getticks(), __LINE__);
5507 rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL);
5508 if (rack->r_state && (rack->r_state != tp->t_state))
5509 rack_set_state(tp, rack);
5510 /*
5511 * Ideally we would like to be able to
5512 * mark SACK-PASS on anything not acked here.
5513 * However, if we do that we would burst out
5514 * all that data 1ms apart. This would be unwise,
5515 * so for now we will just let the normal rxt timer
5516 * and tlp timer take care of it.
5517 */
5518 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
5519 if (rsm->r_flags & RACK_ACKED) {
5520 cnt++;
5521 rsm->r_dupack = 0;
5522 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
5523 if (rsm->r_in_tmap == 0) {
5524 /* We must re-add it back to the tlist */
5525 if (trsm == NULL) {
5526 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
5527 } else {
5528 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
5529 }
5530 rsm->r_in_tmap = 1;
5531 }
5532 }
5533 trsm = rsm;
5534 if (rsm->r_flags & RACK_ACKED)
5535 rsm->r_flags |= RACK_WAS_ACKED;
5536 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS);
5537 }
5538 /* Clear the count (we just un-acked them) */
5539 rack->r_ctl.rc_sacked = 0;
5540 rack->r_ctl.rc_agg_delayed = 0;
5541 rack->r_early = 0;
5542 rack->r_ctl.rc_agg_early = 0;
5543 rack->r_late = 0;
5544 /* Clear the tlp rtx mark */
5545 rack->r_ctl.rc_resend = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
5546 rack->r_ctl.rc_prr_sndcnt = 0;
5547 rack_log_to_prr(rack, 6, 0);
5548 rack->r_timer_override = 1;
5549 }
5550
5551 static void
rack_cc_conn_init(struct tcpcb * tp)5552 rack_cc_conn_init(struct tcpcb *tp)
5553 {
5554 struct tcp_rack *rack;
5555
5556 rack = (struct tcp_rack *)tp->t_fb_ptr;
5557 cc_conn_init(tp);
5558 /*
5559 * We want a chance to stay in slowstart as
5560 * we create a connection. TCP spec says that
5561 * initially ssthresh is infinite. For our
5562 * purposes that is the snd_wnd.
5563 */
5564 if (tp->snd_ssthresh < tp->snd_wnd) {
5565 tp->snd_ssthresh = tp->snd_wnd;
5566 }
5567 /*
5568 * We also want to assure a IW worth of
5569 * data can get inflight.
5570 */
5571 if (rc_init_window(rack) < tp->snd_cwnd)
5572 tp->snd_cwnd = rc_init_window(rack);
5573 }
5574
5575 /*
5576 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
5577 * we will setup to retransmit the lowest seq number outstanding.
5578 */
5579 static int
rack_timeout_rxt(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts)5580 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
5581 {
5582 int32_t rexmt;
5583 struct inpcb *inp;
5584 int32_t retval = 0;
5585 bool isipv6;
5586
5587 inp = tp->t_inpcb;
5588 if (tp->t_timers->tt_flags & TT_STOPPED) {
5589 return (1);
5590 }
5591 if (ctf_progress_timeout_check(tp, false)) {
5592 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
5593 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
5594 tcp_set_inp_to_drop(inp, ETIMEDOUT);
5595 return (1);
5596 }
5597 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
5598 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
5599 (tp->snd_una == tp->snd_max)) {
5600 /* Nothing outstanding .. nothing to do */
5601 return (0);
5602 }
5603 /*
5604 * Retransmission timer went off. Message has not been acked within
5605 * retransmit interval. Back off to a longer retransmit interval
5606 * and retransmit one segment.
5607 */
5608 rack_remxt_tmr(tp);
5609 if ((rack->r_ctl.rc_resend == NULL) ||
5610 ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) {
5611 /*
5612 * If the rwnd collapsed on
5613 * the one we are retransmitting
5614 * it does not count against the
5615 * rxt count.
5616 */
5617 tp->t_rxtshift++;
5618 }
5619 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) {
5620 tp->t_rxtshift = TCP_MAXRXTSHIFT;
5621 KMOD_TCPSTAT_INC(tcps_timeoutdrop);
5622 retval = 1;
5623 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
5624 tcp_set_inp_to_drop(rack->rc_inp,
5625 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
5626 goto out;
5627 }
5628 if (tp->t_state == TCPS_SYN_SENT) {
5629 /*
5630 * If the SYN was retransmitted, indicate CWND to be limited
5631 * to 1 segment in cc_conn_init().
5632 */
5633 tp->snd_cwnd = 1;
5634 } else if (tp->t_rxtshift == 1) {
5635 /*
5636 * first retransmit; record ssthresh and cwnd so they can be
5637 * recovered if this turns out to be a "bad" retransmit. A
5638 * retransmit is considered "bad" if an ACK for this segment
5639 * is received within RTT/2 interval; the assumption here is
5640 * that the ACK was already in flight. See "On Estimating
5641 * End-to-End Network Path Properties" by Allman and Paxson
5642 * for more details.
5643 */
5644 tp->snd_cwnd_prev = tp->snd_cwnd;
5645 tp->snd_ssthresh_prev = tp->snd_ssthresh;
5646 tp->snd_recover_prev = tp->snd_recover;
5647 if (IN_FASTRECOVERY(tp->t_flags))
5648 tp->t_flags |= TF_WASFRECOVERY;
5649 else
5650 tp->t_flags &= ~TF_WASFRECOVERY;
5651 if (IN_CONGRECOVERY(tp->t_flags))
5652 tp->t_flags |= TF_WASCRECOVERY;
5653 else
5654 tp->t_flags &= ~TF_WASCRECOVERY;
5655 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
5656 tp->t_flags |= TF_PREVVALID;
5657 } else
5658 tp->t_flags &= ~TF_PREVVALID;
5659 KMOD_TCPSTAT_INC(tcps_rexmttimeo);
5660 if ((tp->t_state == TCPS_SYN_SENT) ||
5661 (tp->t_state == TCPS_SYN_RECEIVED))
5662 rexmt = MSEC_2_TICKS(RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift]);
5663 else
5664 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
5665 TCPT_RANGESET(tp->t_rxtcur, rexmt,
5666 max(MSEC_2_TICKS(rack_rto_min), rexmt),
5667 MSEC_2_TICKS(rack_rto_max));
5668 /*
5669 * We enter the path for PLMTUD if connection is established or, if
5670 * connection is FIN_WAIT_1 status, reason for the last is that if
5671 * amount of data we send is very small, we could send it in couple
5672 * of packets and process straight to FIN. In that case we won't
5673 * catch ESTABLISHED state.
5674 */
5675 #ifdef INET6
5676 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
5677 #else
5678 isipv6 = false;
5679 #endif
5680 if (((V_tcp_pmtud_blackhole_detect == 1) ||
5681 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
5682 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
5683 ((tp->t_state == TCPS_ESTABLISHED) ||
5684 (tp->t_state == TCPS_FIN_WAIT_1))) {
5685 /*
5686 * Idea here is that at each stage of mtu probe (usually,
5687 * 1448 -> 1188 -> 524) should be given 2 chances to recover
5688 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
5689 * should take care of that.
5690 */
5691 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
5692 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
5693 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
5694 tp->t_rxtshift % 2 == 0)) {
5695 /*
5696 * Enter Path MTU Black-hole Detection mechanism: -
5697 * Disable Path MTU Discovery (IP "DF" bit). -
5698 * Reduce MTU to lower value than what we negotiated
5699 * with peer.
5700 */
5701 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
5702 /* Record that we may have found a black hole. */
5703 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
5704 /* Keep track of previous MSS. */
5705 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
5706 }
5707
5708 /*
5709 * Reduce the MSS to blackhole value or to the
5710 * default in an attempt to retransmit.
5711 */
5712 #ifdef INET6
5713 if (isipv6 &&
5714 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
5715 /* Use the sysctl tuneable blackhole MSS. */
5716 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
5717 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5718 } else if (isipv6) {
5719 /* Use the default MSS. */
5720 tp->t_maxseg = V_tcp_v6mssdflt;
5721 /*
5722 * Disable Path MTU Discovery when we switch
5723 * to minmss.
5724 */
5725 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5726 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5727 }
5728 #endif
5729 #if defined(INET6) && defined(INET)
5730 else
5731 #endif
5732 #ifdef INET
5733 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
5734 /* Use the sysctl tuneable blackhole MSS. */
5735 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
5736 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated);
5737 } else {
5738 /* Use the default MSS. */
5739 tp->t_maxseg = V_tcp_mssdflt;
5740 /*
5741 * Disable Path MTU Discovery when we switch
5742 * to minmss.
5743 */
5744 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
5745 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
5746 }
5747 #endif
5748 } else {
5749 /*
5750 * If further retransmissions are still unsuccessful
5751 * with a lowered MTU, maybe this isn't a blackhole
5752 * and we restore the previous MSS and blackhole
5753 * detection flags. The limit '6' is determined by
5754 * giving each probe stage (1448, 1188, 524) 2
5755 * chances to recover.
5756 */
5757 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
5758 (tp->t_rxtshift >= 6)) {
5759 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
5760 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
5761 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
5762 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed);
5763 }
5764 }
5765 }
5766 /*
5767 * If we backed off this far, our srtt estimate is probably bogus.
5768 * Clobber it so we'll take the next rtt measurement as our srtt;
5769 * move the current srtt into rttvar to keep the current retransmit
5770 * times until then.
5771 */
5772 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
5773 #ifdef INET6
5774 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
5775 in6_losing(tp->t_inpcb);
5776 else
5777 #endif
5778 in_losing(tp->t_inpcb);
5779 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
5780 tp->t_srtt = 0;
5781 }
5782 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
5783 tp->snd_recover = tp->snd_max;
5784 tp->t_flags |= TF_ACKNOW;
5785 tp->t_rtttime = 0;
5786 rack_cong_signal(tp, NULL, CC_RTO);
5787 out:
5788 return (retval);
5789 }
5790
5791 static int
rack_process_timers(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,uint8_t hpts_calling)5792 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling)
5793 {
5794 int32_t ret = 0;
5795 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
5796
5797 if (timers == 0) {
5798 return (0);
5799 }
5800 if (tp->t_state == TCPS_LISTEN) {
5801 /* no timers on listen sockets */
5802 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
5803 return (0);
5804 return (1);
5805 }
5806 if ((timers & PACE_TMR_RACK) &&
5807 rack->rc_on_min_to) {
5808 /*
5809 * For the rack timer when we
5810 * are on a min-timeout (which means rrr_conf = 3)
5811 * we don't want to check the timer. It may
5812 * be going off for a pace and thats ok we
5813 * want to send the retransmit (if its ready).
5814 *
5815 * If its on a normal rack timer (non-min) then
5816 * we will check if its expired.
5817 */
5818 goto skip_time_check;
5819 }
5820 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
5821 uint32_t left;
5822
5823 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
5824 ret = -1;
5825 rack_log_to_processing(rack, cts, ret, 0);
5826 return (0);
5827 }
5828 if (hpts_calling == 0) {
5829 /*
5830 * A user send or queued mbuf (sack) has called us? We
5831 * return 0 and let the pacing guards
5832 * deal with it if they should or
5833 * should not cause a send.
5834 */
5835 ret = -2;
5836 rack_log_to_processing(rack, cts, ret, 0);
5837 return (0);
5838 }
5839 /*
5840 * Ok our timer went off early and we are not paced false
5841 * alarm, go back to sleep.
5842 */
5843 ret = -3;
5844 left = rack->r_ctl.rc_timer_exp - cts;
5845 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left));
5846 rack_log_to_processing(rack, cts, ret, left);
5847 return (1);
5848 }
5849 skip_time_check:
5850 rack->rc_tmr_stopped = 0;
5851 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
5852 if (timers & PACE_TMR_DELACK) {
5853 ret = rack_timeout_delack(tp, rack, cts);
5854 } else if (timers & PACE_TMR_RACK) {
5855 rack->r_ctl.rc_tlp_rxt_last_time = cts;
5856 ret = rack_timeout_rack(tp, rack, cts);
5857 } else if (timers & PACE_TMR_TLP) {
5858 rack->r_ctl.rc_tlp_rxt_last_time = cts;
5859 ret = rack_timeout_tlp(tp, rack, cts);
5860 } else if (timers & PACE_TMR_RXT) {
5861 rack->r_ctl.rc_tlp_rxt_last_time = cts;
5862 ret = rack_timeout_rxt(tp, rack, cts);
5863 } else if (timers & PACE_TMR_PERSIT) {
5864 ret = rack_timeout_persist(tp, rack, cts);
5865 } else if (timers & PACE_TMR_KEEP) {
5866 ret = rack_timeout_keepalive(tp, rack, cts);
5867 }
5868 rack_log_to_processing(rack, cts, ret, timers);
5869 return (ret);
5870 }
5871
5872 static void
rack_timer_cancel(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cts,int line)5873 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
5874 {
5875 struct timeval tv;
5876 uint32_t us_cts, flags_on_entry;
5877 uint8_t hpts_removed = 0;
5878
5879 flags_on_entry = rack->r_ctl.rc_hpts_flags;
5880 us_cts = tcp_get_usecs(&tv);
5881 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
5882 ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) ||
5883 ((tp->snd_max - tp->snd_una) == 0))) {
5884 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
5885 hpts_removed = 1;
5886 /* If we were not delayed cancel out the flag. */
5887 if ((tp->snd_max - tp->snd_una) == 0)
5888 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
5889 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
5890 }
5891 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
5892 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
5893 if (rack->rc_inp->inp_in_hpts &&
5894 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
5895 /*
5896 * Canceling timer's when we have no output being
5897 * paced. We also must remove ourselves from the
5898 * hpts.
5899 */
5900 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
5901 hpts_removed = 1;
5902 }
5903 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
5904 }
5905 if (hpts_removed == 0)
5906 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry);
5907 }
5908
5909 static void
rack_timer_stop(struct tcpcb * tp,uint32_t timer_type)5910 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
5911 {
5912 return;
5913 }
5914
5915 static int
rack_stopall(struct tcpcb * tp)5916 rack_stopall(struct tcpcb *tp)
5917 {
5918 struct tcp_rack *rack;
5919 rack = (struct tcp_rack *)tp->t_fb_ptr;
5920 rack->t_timers_stopped = 1;
5921 return (0);
5922 }
5923
5924 static void
rack_timer_activate(struct tcpcb * tp,uint32_t timer_type,uint32_t delta)5925 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
5926 {
5927 return;
5928 }
5929
5930 static int
rack_timer_active(struct tcpcb * tp,uint32_t timer_type)5931 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
5932 {
5933 return (0);
5934 }
5935
5936 static void
rack_stop_all_timers(struct tcpcb * tp)5937 rack_stop_all_timers(struct tcpcb *tp)
5938 {
5939 struct tcp_rack *rack;
5940
5941 /*
5942 * Assure no timers are running.
5943 */
5944 if (tcp_timer_active(tp, TT_PERSIST)) {
5945 /* We enter in persists, set the flag appropriately */
5946 rack = (struct tcp_rack *)tp->t_fb_ptr;
5947 rack->rc_in_persist = 1;
5948 }
5949 tcp_timer_suspend(tp, TT_PERSIST);
5950 tcp_timer_suspend(tp, TT_REXMT);
5951 tcp_timer_suspend(tp, TT_KEEP);
5952 tcp_timer_suspend(tp, TT_DELACK);
5953 }
5954
5955 static void
rack_update_rsm(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t ts)5956 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
5957 struct rack_sendmap *rsm, uint32_t ts)
5958 {
5959 int32_t idx;
5960
5961 rsm->r_rtr_cnt++;
5962 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
5963 rsm->r_dupack = 0;
5964 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
5965 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
5966 rsm->r_flags |= RACK_OVERMAX;
5967 }
5968 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) {
5969 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
5970 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
5971 }
5972 idx = rsm->r_rtr_cnt - 1;
5973 rsm->r_tim_lastsent[idx] = ts;
5974 if (rsm->r_flags & RACK_ACKED) {
5975 /* Problably MTU discovery messing with us */
5976 rsm->r_flags &= ~RACK_ACKED;
5977 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
5978 }
5979 if (rsm->r_in_tmap) {
5980 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
5981 rsm->r_in_tmap = 0;
5982 }
5983 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
5984 rsm->r_in_tmap = 1;
5985 if (rsm->r_flags & RACK_SACK_PASSED) {
5986 /* We have retransmitted due to the SACK pass */
5987 rsm->r_flags &= ~RACK_SACK_PASSED;
5988 rsm->r_flags |= RACK_WAS_SACKPASS;
5989 }
5990 }
5991
5992 static uint32_t
rack_update_entry(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,uint32_t ts,int32_t * lenp)5993 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
5994 struct rack_sendmap *rsm, uint32_t ts, int32_t *lenp)
5995 {
5996 /*
5997 * We (re-)transmitted starting at rsm->r_start for some length
5998 * (possibly less than r_end.
5999 */
6000 struct rack_sendmap *nrsm, *insret;
6001 uint32_t c_end;
6002 int32_t len;
6003
6004 len = *lenp;
6005 c_end = rsm->r_start + len;
6006 if (SEQ_GEQ(c_end, rsm->r_end)) {
6007 /*
6008 * We retransmitted the whole piece or more than the whole
6009 * slopping into the next rsm.
6010 */
6011 rack_update_rsm(tp, rack, rsm, ts);
6012 if (c_end == rsm->r_end) {
6013 *lenp = 0;
6014 return (0);
6015 } else {
6016 int32_t act_len;
6017
6018 /* Hangs over the end return whats left */
6019 act_len = rsm->r_end - rsm->r_start;
6020 *lenp = (len - act_len);
6021 return (rsm->r_end);
6022 }
6023 /* We don't get out of this block. */
6024 }
6025 /*
6026 * Here we retransmitted less than the whole thing which means we
6027 * have to split this into what was transmitted and what was not.
6028 */
6029 nrsm = rack_alloc_full_limit(rack);
6030 if (nrsm == NULL) {
6031 /*
6032 * We can't get memory, so lets not proceed.
6033 */
6034 *lenp = 0;
6035 return (0);
6036 }
6037 /*
6038 * So here we are going to take the original rsm and make it what we
6039 * retransmitted. nrsm will be the tail portion we did not
6040 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
6041 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
6042 * 1, 6 and the new piece will be 6, 11.
6043 */
6044 rack_clone_rsm(rack, nrsm, rsm, c_end);
6045 nrsm->r_dupack = 0;
6046 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
6047 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6048 #ifdef INVARIANTS
6049 if (insret != NULL) {
6050 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6051 nrsm, insret, rack, rsm);
6052 }
6053 #endif
6054 if (rsm->r_in_tmap) {
6055 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6056 nrsm->r_in_tmap = 1;
6057 }
6058 rsm->r_flags &= (~RACK_HAS_FIN);
6059 rack_update_rsm(tp, rack, rsm, ts);
6060 *lenp = 0;
6061 return (0);
6062 }
6063
6064 static void
rack_log_output(struct tcpcb * tp,struct tcpopt * to,int32_t len,uint32_t seq_out,uint8_t th_flags,int32_t err,uint32_t ts,uint8_t pass,struct rack_sendmap * hintrsm,uint32_t us_cts)6065 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
6066 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts,
6067 uint8_t pass, struct rack_sendmap *hintrsm, uint32_t us_cts)
6068 {
6069 struct tcp_rack *rack;
6070 struct rack_sendmap *rsm, *nrsm, *insret, fe;
6071 register uint32_t snd_max, snd_una;
6072
6073 /*
6074 * Add to the RACK log of packets in flight or retransmitted. If
6075 * there is a TS option we will use the TS echoed, if not we will
6076 * grab a TS.
6077 *
6078 * Retransmissions will increment the count and move the ts to its
6079 * proper place. Note that if options do not include TS's then we
6080 * won't be able to effectively use the ACK for an RTT on a retran.
6081 *
6082 * Notes about r_start and r_end. Lets consider a send starting at
6083 * sequence 1 for 10 bytes. In such an example the r_start would be
6084 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
6085 * This means that r_end is actually the first sequence for the next
6086 * slot (11).
6087 *
6088 */
6089 /*
6090 * If err is set what do we do XXXrrs? should we not add the thing?
6091 * -- i.e. return if err != 0 or should we pretend we sent it? --
6092 * i.e. proceed with add ** do this for now.
6093 */
6094 INP_WLOCK_ASSERT(tp->t_inpcb);
6095 if (err)
6096 /*
6097 * We don't log errors -- we could but snd_max does not
6098 * advance in this case either.
6099 */
6100 return;
6101
6102 if (th_flags & TH_RST) {
6103 /*
6104 * We don't log resets and we return immediately from
6105 * sending
6106 */
6107 return;
6108 }
6109 rack = (struct tcp_rack *)tp->t_fb_ptr;
6110 snd_una = tp->snd_una;
6111 if (SEQ_LEQ((seq_out + len), snd_una)) {
6112 /* Are sending an old segment to induce an ack (keep-alive)? */
6113 return;
6114 }
6115 if (SEQ_LT(seq_out, snd_una)) {
6116 /* huh? should we panic? */
6117 uint32_t end;
6118
6119 end = seq_out + len;
6120 seq_out = snd_una;
6121 if (SEQ_GEQ(end, seq_out))
6122 len = end - seq_out;
6123 else
6124 len = 0;
6125 }
6126 snd_max = tp->snd_max;
6127 if (th_flags & (TH_SYN | TH_FIN)) {
6128 /*
6129 * The call to rack_log_output is made before bumping
6130 * snd_max. This means we can record one extra byte on a SYN
6131 * or FIN if seq_out is adding more on and a FIN is present
6132 * (and we are not resending).
6133 */
6134 if ((th_flags & TH_SYN) && (seq_out == tp->iss))
6135 len++;
6136 if (th_flags & TH_FIN)
6137 len++;
6138 if (SEQ_LT(snd_max, tp->snd_nxt)) {
6139 /*
6140 * The add/update as not been done for the FIN/SYN
6141 * yet.
6142 */
6143 snd_max = tp->snd_nxt;
6144 }
6145 }
6146 if (len == 0) {
6147 /* We don't log zero window probes */
6148 return;
6149 }
6150 rack->r_ctl.rc_time_last_sent = ts;
6151 if (IN_RECOVERY(tp->t_flags)) {
6152 rack->r_ctl.rc_prr_out += len;
6153 }
6154 /* First question is it a retransmission or new? */
6155 if (seq_out == snd_max) {
6156 /* Its new */
6157 again:
6158 rsm = rack_alloc(rack);
6159 if (rsm == NULL) {
6160 /*
6161 * Hmm out of memory and the tcb got destroyed while
6162 * we tried to wait.
6163 */
6164 return;
6165 }
6166 if (th_flags & TH_FIN) {
6167 rsm->r_flags = RACK_HAS_FIN;
6168 } else {
6169 rsm->r_flags = 0;
6170 }
6171 rsm->r_tim_lastsent[0] = ts;
6172 rsm->r_rtr_cnt = 1;
6173 rsm->r_rtr_bytes = 0;
6174 rsm->usec_orig_send = us_cts;
6175 if (th_flags & TH_SYN) {
6176 /* The data space is one beyond snd_una */
6177 rsm->r_flags |= RACK_HAS_SIN;
6178 rsm->r_start = seq_out + 1;
6179 rsm->r_end = rsm->r_start + (len - 1);
6180 } else {
6181 /* Normal case */
6182 rsm->r_start = seq_out;
6183 rsm->r_end = rsm->r_start + len;
6184 }
6185 rsm->r_dupack = 0;
6186 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
6187 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
6188 #ifdef INVARIANTS
6189 if (insret != NULL) {
6190 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6191 nrsm, insret, rack, rsm);
6192 }
6193 #endif
6194 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6195 rsm->r_in_tmap = 1;
6196 /*
6197 * Special case detection, is there just a single
6198 * packet outstanding when we are not in recovery?
6199 *
6200 * If this is true mark it so.
6201 */
6202 if ((IN_RECOVERY(tp->t_flags) == 0) &&
6203 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) {
6204 struct rack_sendmap *prsm;
6205
6206 prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
6207 if (prsm)
6208 prsm->r_one_out_nr = 1;
6209 }
6210 return;
6211 }
6212 /*
6213 * If we reach here its a retransmission and we need to find it.
6214 */
6215 memset(&fe, 0, sizeof(fe));
6216 more:
6217 if (hintrsm && (hintrsm->r_start == seq_out)) {
6218 rsm = hintrsm;
6219 hintrsm = NULL;
6220 } else {
6221 /* No hints sorry */
6222 rsm = NULL;
6223 }
6224 if ((rsm) && (rsm->r_start == seq_out)) {
6225 seq_out = rack_update_entry(tp, rack, rsm, ts, &len);
6226 if (len == 0) {
6227 return;
6228 } else {
6229 goto more;
6230 }
6231 }
6232 /* Ok it was not the last pointer go through it the hard way. */
6233 refind:
6234 fe.r_start = seq_out;
6235 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
6236 if (rsm) {
6237 if (rsm->r_start == seq_out) {
6238 seq_out = rack_update_entry(tp, rack, rsm, ts, &len);
6239 if (len == 0) {
6240 return;
6241 } else {
6242 goto refind;
6243 }
6244 }
6245 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
6246 /* Transmitted within this piece */
6247 /*
6248 * Ok we must split off the front and then let the
6249 * update do the rest
6250 */
6251 nrsm = rack_alloc_full_limit(rack);
6252 if (nrsm == NULL) {
6253 rack_update_rsm(tp, rack, rsm, ts);
6254 return;
6255 }
6256 /*
6257 * copy rsm to nrsm and then trim the front of rsm
6258 * to not include this part.
6259 */
6260 rack_clone_rsm(rack, nrsm, rsm, seq_out);
6261 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
6262 #ifdef INVARIANTS
6263 if (insret != NULL) {
6264 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
6265 nrsm, insret, rack, rsm);
6266 }
6267 #endif
6268 if (rsm->r_in_tmap) {
6269 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
6270 nrsm->r_in_tmap = 1;
6271 }
6272 rsm->r_flags &= (~RACK_HAS_FIN);
6273 seq_out = rack_update_entry(tp, rack, nrsm, ts, &len);
6274 if (len == 0) {
6275 return;
6276 } else if (len > 0)
6277 goto refind;
6278 }
6279 }
6280 /*
6281 * Hmm not found in map did they retransmit both old and on into the
6282 * new?
6283 */
6284 if (seq_out == tp->snd_max) {
6285 goto again;
6286 } else if (SEQ_LT(seq_out, tp->snd_max)) {
6287 #ifdef INVARIANTS
6288 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
6289 seq_out, len, tp->snd_una, tp->snd_max);
6290 printf("Starting Dump of all rack entries\n");
6291 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
6292 printf("rsm:%p start:%u end:%u\n",
6293 rsm, rsm->r_start, rsm->r_end);
6294 }
6295 printf("Dump complete\n");
6296 panic("seq_out not found rack:%p tp:%p",
6297 rack, tp);
6298 #endif
6299 } else {
6300 #ifdef INVARIANTS
6301 /*
6302 * Hmm beyond sndmax? (only if we are using the new rtt-pack
6303 * flag)
6304 */
6305 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
6306 seq_out, len, tp->snd_max, tp);
6307 #endif
6308 }
6309 }
6310
6311 /*
6312 * Record one of the RTT updates from an ack into
6313 * our sample structure.
6314 */
6315
6316 static void
tcp_rack_xmit_timer(struct tcp_rack * rack,int32_t rtt,uint32_t len,uint32_t us_rtt,int confidence,struct rack_sendmap * rsm,uint16_t rtrcnt)6317 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt,
6318 int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt)
6319 {
6320 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
6321 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
6322 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
6323 }
6324 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
6325 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
6326 rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
6327 }
6328 if (rack->rc_tp->t_flags & TF_GPUTINPROG) {
6329 if (us_rtt < rack->r_ctl.rc_gp_lowrtt)
6330 rack->r_ctl.rc_gp_lowrtt = us_rtt;
6331 if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd)
6332 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
6333 }
6334 if ((confidence == 1) &&
6335 ((rsm == NULL) ||
6336 (rsm->r_just_ret) ||
6337 (rsm->r_one_out_nr &&
6338 len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) {
6339 /*
6340 * If the rsm had a just return
6341 * hit it then we can't trust the
6342 * rtt measurement for buffer deterimination
6343 * Note that a confidence of 2, indicates
6344 * SACK'd which overrides the r_just_ret or
6345 * the r_one_out_nr. If it was a CUM-ACK and
6346 * we had only two outstanding, but get an
6347 * ack for only 1. Then that also lowers our
6348 * confidence.
6349 */
6350 confidence = 0;
6351 }
6352 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
6353 (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) {
6354 if (rack->r_ctl.rack_rs.confidence == 0) {
6355 /*
6356 * We take anything with no current confidence
6357 * saved.
6358 */
6359 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
6360 rack->r_ctl.rack_rs.confidence = confidence;
6361 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
6362 } else if (confidence || rack->r_ctl.rack_rs.confidence) {
6363 /*
6364 * Once we have a confident number,
6365 * we can update it with a smaller
6366 * value since this confident number
6367 * may include the DSACK time until
6368 * the next segment (the second one) arrived.
6369 */
6370 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt;
6371 rack->r_ctl.rack_rs.confidence = confidence;
6372 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt;
6373 }
6374 }
6375 rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence);
6376 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
6377 rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
6378 rack->r_ctl.rack_rs.rs_rtt_cnt++;
6379 }
6380
6381 /*
6382 * Collect new round-trip time estimate
6383 * and update averages and current timeout.
6384 */
6385 static void
tcp_rack_xmit_timer_commit(struct tcp_rack * rack,struct tcpcb * tp)6386 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
6387 {
6388 int32_t delta;
6389 uint32_t o_srtt, o_var;
6390 int32_t hrtt_up = 0;
6391 int32_t rtt;
6392
6393 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
6394 /* No valid sample */
6395 return;
6396 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
6397 /* We are to use the lowest RTT seen in a single ack */
6398 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
6399 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
6400 /* We are to use the highest RTT seen in a single ack */
6401 rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
6402 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
6403 /* We are to use the average RTT seen in a single ack */
6404 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
6405 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
6406 } else {
6407 #ifdef INVARIANTS
6408 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
6409 #endif
6410 return;
6411 }
6412 if (rtt == 0)
6413 rtt = 1;
6414 if (rack->rc_gp_rtt_set == 0) {
6415 /*
6416 * With no RTT we have to accept
6417 * even one we are not confident of.
6418 */
6419 rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt;
6420 rack->rc_gp_rtt_set = 1;
6421 } else if (rack->r_ctl.rack_rs.confidence) {
6422 /* update the running gp srtt */
6423 rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8);
6424 rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8;
6425 }
6426 if (rack->r_ctl.rack_rs.confidence) {
6427 /*
6428 * record the low and high for highly buffered path computation,
6429 * we only do this if we are confident (not a retransmission).
6430 */
6431 if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) {
6432 rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
6433 hrtt_up = 1;
6434 }
6435 if (rack->rc_highly_buffered == 0) {
6436 /*
6437 * Currently once we declare a path has
6438 * highly buffered there is no going
6439 * back, which may be a problem...
6440 */
6441 if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) {
6442 rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt,
6443 rack->r_ctl.rc_highest_us_rtt,
6444 rack->r_ctl.rc_lowest_us_rtt,
6445 RACK_RTTS_SEEHBP);
6446 rack->rc_highly_buffered = 1;
6447 }
6448 }
6449 }
6450 if ((rack->r_ctl.rack_rs.confidence) ||
6451 (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) {
6452 /*
6453 * If we are highly confident of it <or> it was
6454 * never retransmitted we accept it as the last us_rtt.
6455 */
6456 rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
6457 /* The lowest rtt can be set if its was not retransmited */
6458 if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) {
6459 rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt;
6460 if (rack->r_ctl.rc_lowest_us_rtt == 0)
6461 rack->r_ctl.rc_lowest_us_rtt = 1;
6462 }
6463 }
6464 rack_log_rtt_sample(rack, rtt);
6465 o_srtt = tp->t_srtt;
6466 o_var = tp->t_rttvar;
6467 rack = (struct tcp_rack *)tp->t_fb_ptr;
6468 if (tp->t_srtt != 0) {
6469 /*
6470 * srtt is stored as fixed point with 5 bits after the
6471 * binary point (i.e., scaled by 8). The following magic is
6472 * equivalent to the smoothing algorithm in rfc793 with an
6473 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
6474 * Adjust rtt to origin 0.
6475 */
6476 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
6477 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6478
6479 tp->t_srtt += delta;
6480 if (tp->t_srtt <= 0)
6481 tp->t_srtt = 1;
6482
6483 /*
6484 * We accumulate a smoothed rtt variance (actually, a
6485 * smoothed mean difference), then set the retransmit timer
6486 * to smoothed rtt + 4 times the smoothed variance. rttvar
6487 * is stored as fixed point with 4 bits after the binary
6488 * point (scaled by 16). The following is equivalent to
6489 * rfc793 smoothing with an alpha of .75 (rttvar =
6490 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
6491 * wired-in beta.
6492 */
6493 if (delta < 0)
6494 delta = -delta;
6495 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6496 tp->t_rttvar += delta;
6497 if (tp->t_rttvar <= 0)
6498 tp->t_rttvar = 1;
6499 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
6500 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6501 } else {
6502 /*
6503 * No rtt measurement yet - use the unsmoothed rtt. Set the
6504 * variance to half the rtt (so our first retransmit happens
6505 * at 3*rtt).
6506 */
6507 tp->t_srtt = rtt << TCP_RTT_SHIFT;
6508 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
6509 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6510 }
6511 KMOD_TCPSTAT_INC(tcps_rttupdated);
6512 tp->t_rttupdated++;
6513 #ifdef STATS
6514 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
6515 #endif
6516 tp->t_rxtshift = 0;
6517
6518 /*
6519 * the retransmit should happen at rtt + 4 * rttvar. Because of the
6520 * way we do the smoothing, srtt and rttvar will each average +1/2
6521 * tick of bias. When we compute the retransmit timer, we want 1/2
6522 * tick of rounding and 1 extra tick because of +-1/2 tick
6523 * uncertainty in the firing of the timer. The bias will give us
6524 * exactly the 1.5 tick we need. But, because the bias is
6525 * statistical, we have to test that we don't drop below the minimum
6526 * feasible timer (which is 2 ticks).
6527 */
6528 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6529 max(MSEC_2_TICKS(rack_rto_min), rtt + 2), MSEC_2_TICKS(rack_rto_max));
6530 tp->t_softerror = 0;
6531 }
6532
6533 static void
rack_earlier_retran(struct tcpcb * tp,struct rack_sendmap * rsm,uint32_t t,uint32_t cts)6534 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm,
6535 uint32_t t, uint32_t cts)
6536 {
6537 /*
6538 * For this RSM, we acknowledged the data from a previous
6539 * transmission, not the last one we made. This means we did a false
6540 * retransmit.
6541 */
6542 struct tcp_rack *rack;
6543
6544 if (rsm->r_flags & RACK_HAS_FIN) {
6545 /*
6546 * The sending of the FIN often is multiple sent when we
6547 * have everything outstanding ack'd. We ignore this case
6548 * since its over now.
6549 */
6550 return;
6551 }
6552 if (rsm->r_flags & RACK_TLP) {
6553 /*
6554 * We expect TLP's to have this occur.
6555 */
6556 return;
6557 }
6558 rack = (struct tcp_rack *)tp->t_fb_ptr;
6559 /* should we undo cc changes and exit recovery? */
6560 if (IN_RECOVERY(tp->t_flags)) {
6561 if (rack->r_ctl.rc_rsm_start == rsm->r_start) {
6562 /*
6563 * Undo what we ratched down and exit recovery if
6564 * possible
6565 */
6566 EXIT_RECOVERY(tp->t_flags);
6567 tp->snd_recover = tp->snd_una;
6568 if (rack->r_ctl.rc_cwnd_at > tp->snd_cwnd)
6569 tp->snd_cwnd = rack->r_ctl.rc_cwnd_at;
6570 if (rack->r_ctl.rc_ssthresh_at > tp->snd_ssthresh)
6571 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at;
6572 }
6573 }
6574 if (rsm->r_flags & RACK_WAS_SACKPASS) {
6575 /*
6576 * We retransmitted based on a sack and the earlier
6577 * retransmission ack'd it - re-ordering is occuring.
6578 */
6579 counter_u64_add(rack_reorder_seen, 1);
6580 rack->r_ctl.rc_reorder_ts = cts;
6581 }
6582 counter_u64_add(rack_badfr, 1);
6583 counter_u64_add(rack_badfr_bytes, (rsm->r_end - rsm->r_start));
6584 }
6585
6586 static void
rack_apply_updated_usrtt(struct tcp_rack * rack,uint32_t us_rtt,uint32_t us_cts)6587 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts)
6588 {
6589 /*
6590 * Apply to filter the inbound us-rtt at us_cts.
6591 */
6592 uint32_t old_rtt;
6593
6594 old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt);
6595 apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt,
6596 us_rtt, us_cts);
6597 if (rack->r_ctl.last_pacing_time &&
6598 rack->rc_gp_dyn_mul &&
6599 (rack->r_ctl.last_pacing_time > us_rtt))
6600 rack->pacing_longer_than_rtt = 1;
6601 else
6602 rack->pacing_longer_than_rtt = 0;
6603 if (old_rtt > us_rtt) {
6604 /* We just hit a new lower rtt time */
6605 rack_log_rtt_shrinks(rack, us_cts, old_rtt,
6606 __LINE__, RACK_RTTS_NEWRTT);
6607 /*
6608 * Only count it if its lower than what we saw within our
6609 * calculated range.
6610 */
6611 if ((old_rtt - us_rtt) > rack_min_rtt_movement) {
6612 if (rack_probertt_lower_within &&
6613 rack->rc_gp_dyn_mul &&
6614 (rack->use_fixed_rate == 0) &&
6615 (rack->rc_always_pace)) {
6616 /*
6617 * We are seeing a new lower rtt very close
6618 * to the time that we would have entered probe-rtt.
6619 * This is probably due to the fact that a peer flow
6620 * has entered probe-rtt. Lets go in now too.
6621 */
6622 uint32_t val;
6623
6624 val = rack_probertt_lower_within * rack_time_between_probertt;
6625 val /= 100;
6626 if ((rack->in_probe_rtt == 0) &&
6627 ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val))) {
6628 rack_enter_probertt(rack, us_cts);
6629 }
6630 }
6631 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
6632 }
6633 }
6634 }
6635
6636 static int
rack_update_rtt(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,struct tcpopt * to,uint32_t cts,int32_t ack_type,tcp_seq th_ack)6637 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
6638 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack)
6639 {
6640 int32_t i;
6641 uint32_t t, len_acked;
6642
6643 if ((rsm->r_flags & RACK_ACKED) ||
6644 (rsm->r_flags & RACK_WAS_ACKED))
6645 /* Already done */
6646 return (0);
6647
6648 if (ack_type == CUM_ACKED) {
6649 if (SEQ_GT(th_ack, rsm->r_end))
6650 len_acked = rsm->r_end - rsm->r_start;
6651 else
6652 len_acked = th_ack - rsm->r_start;
6653 } else
6654 len_acked = rsm->r_end - rsm->r_start;
6655 if (rsm->r_rtr_cnt == 1) {
6656 uint32_t us_rtt;
6657
6658 t = cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
6659 if ((int)t <= 0)
6660 t = 1;
6661 if (!tp->t_rttlow || tp->t_rttlow > t)
6662 tp->t_rttlow = t;
6663 if (!rack->r_ctl.rc_rack_min_rtt ||
6664 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
6665 rack->r_ctl.rc_rack_min_rtt = t;
6666 if (rack->r_ctl.rc_rack_min_rtt == 0) {
6667 rack->r_ctl.rc_rack_min_rtt = 1;
6668 }
6669 }
6670 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - rsm->usec_orig_send;
6671 if (us_rtt == 0)
6672 us_rtt = 1;
6673 rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time));
6674 if (ack_type == SACKED)
6675 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt);
6676 else {
6677 /*
6678 * For cum-ack we are only confident if what
6679 * is being acked is included in a measurement.
6680 * Otherwise it could be an idle period that
6681 * includes Delayed-ack time.
6682 */
6683 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt,
6684 (rack->app_limited_needs_set ? 0 : 1), rsm, rsm->r_rtr_cnt);
6685 }
6686 if ((rsm->r_flags & RACK_TLP) &&
6687 (!IN_RECOVERY(tp->t_flags))) {
6688 /* Segment was a TLP and our retrans matched */
6689 if (rack->r_ctl.rc_tlp_cwnd_reduce) {
6690 rack->r_ctl.rc_rsm_start = tp->snd_max;
6691 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
6692 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
6693 rack_cong_signal(tp, NULL, CC_NDUPACK);
6694 /*
6695 * When we enter recovery we need to assure
6696 * we send one packet.
6697 */
6698 if (rack->rack_no_prr == 0) {
6699 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
6700 rack_log_to_prr(rack, 7, 0);
6701 }
6702 }
6703 }
6704 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
6705 /* New more recent rack_tmit_time */
6706 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
6707 rack->rc_rack_rtt = t;
6708 }
6709 return (1);
6710 }
6711 /*
6712 * We clear the soft/rxtshift since we got an ack.
6713 * There is no assurance we will call the commit() function
6714 * so we need to clear these to avoid incorrect handling.
6715 */
6716 tp->t_rxtshift = 0;
6717 tp->t_softerror = 0;
6718 if ((to->to_flags & TOF_TS) &&
6719 (ack_type == CUM_ACKED) &&
6720 (to->to_tsecr) &&
6721 ((rsm->r_flags & RACK_OVERMAX) == 0)) {
6722 /*
6723 * Now which timestamp does it match? In this block the ACK
6724 * must be coming from a previous transmission.
6725 */
6726 for (i = 0; i < rsm->r_rtr_cnt; i++) {
6727 if (rsm->r_tim_lastsent[i] == to->to_tsecr) {
6728 t = cts - rsm->r_tim_lastsent[i];
6729 if ((int)t <= 0)
6730 t = 1;
6731 if ((i + 1) < rsm->r_rtr_cnt) {
6732 /* Likely */
6733 rack_earlier_retran(tp, rsm, t, cts);
6734 }
6735 if (!tp->t_rttlow || tp->t_rttlow > t)
6736 tp->t_rttlow = t;
6737 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
6738 rack->r_ctl.rc_rack_min_rtt = t;
6739 if (rack->r_ctl.rc_rack_min_rtt == 0) {
6740 rack->r_ctl.rc_rack_min_rtt = 1;
6741 }
6742 }
6743 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
6744 rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
6745 /* New more recent rack_tmit_time */
6746 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
6747 rack->rc_rack_rtt = t;
6748 }
6749 tcp_rack_xmit_timer(rack, t + 1, len_acked, (t * HPTS_USEC_IN_MSEC), 0, rsm,
6750 rsm->r_rtr_cnt);
6751 return (1);
6752 }
6753 }
6754 goto ts_not_found;
6755 } else {
6756 /*
6757 * Ok its a SACK block that we retransmitted. or a windows
6758 * machine without timestamps. We can tell nothing from the
6759 * time-stamp since its not there or the time the peer last
6760 * recieved a segment that moved forward its cum-ack point.
6761 */
6762 ts_not_found:
6763 i = rsm->r_rtr_cnt - 1;
6764 t = cts - rsm->r_tim_lastsent[i];
6765 if ((int)t <= 0)
6766 t = 1;
6767 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
6768 /*
6769 * We retransmitted and the ack came back in less
6770 * than the smallest rtt we have observed. We most
6771 * likey did an improper retransmit as outlined in
6772 * 4.2 Step 3 point 2 in the rack-draft.
6773 */
6774 i = rsm->r_rtr_cnt - 2;
6775 t = cts - rsm->r_tim_lastsent[i];
6776 rack_earlier_retran(tp, rsm, t, cts);
6777 } else if (rack->r_ctl.rc_rack_min_rtt) {
6778 /*
6779 * We retransmitted it and the retransmit did the
6780 * job.
6781 */
6782 if (!rack->r_ctl.rc_rack_min_rtt ||
6783 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
6784 rack->r_ctl.rc_rack_min_rtt = t;
6785 if (rack->r_ctl.rc_rack_min_rtt == 0) {
6786 rack->r_ctl.rc_rack_min_rtt = 1;
6787 }
6788 }
6789 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[i])) {
6790 /* New more recent rack_tmit_time */
6791 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[i];
6792 rack->rc_rack_rtt = t;
6793 }
6794 return (1);
6795 }
6796 }
6797 return (0);
6798 }
6799
6800 /*
6801 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
6802 */
6803 static void
rack_log_sack_passed(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm)6804 rack_log_sack_passed(struct tcpcb *tp,
6805 struct tcp_rack *rack, struct rack_sendmap *rsm)
6806 {
6807 struct rack_sendmap *nrsm;
6808
6809 nrsm = rsm;
6810 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
6811 rack_head, r_tnext) {
6812 if (nrsm == rsm) {
6813 /* Skip orginal segment he is acked */
6814 continue;
6815 }
6816 if (nrsm->r_flags & RACK_ACKED) {
6817 /*
6818 * Skip ack'd segments, though we
6819 * should not see these, since tmap
6820 * should not have ack'd segments.
6821 */
6822 continue;
6823 }
6824 if (nrsm->r_flags & RACK_SACK_PASSED) {
6825 /*
6826 * We found one that is already marked
6827 * passed, we have been here before and
6828 * so all others below this are marked.
6829 */
6830 break;
6831 }
6832 nrsm->r_flags |= RACK_SACK_PASSED;
6833 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
6834 }
6835 }
6836
6837 static void
rack_need_set_test(struct tcpcb * tp,struct tcp_rack * rack,struct rack_sendmap * rsm,tcp_seq th_ack,int line,int use_which)6838 rack_need_set_test(struct tcpcb *tp,
6839 struct tcp_rack *rack,
6840 struct rack_sendmap *rsm,
6841 tcp_seq th_ack,
6842 int line,
6843 int use_which)
6844 {
6845
6846 if ((tp->t_flags & TF_GPUTINPROG) &&
6847 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
6848 /*
6849 * We were app limited, and this ack
6850 * butts up or goes beyond the point where we want
6851 * to start our next measurement. We need
6852 * to record the new gput_ts as here and
6853 * possibly update the start sequence.
6854 */
6855 uint32_t seq, ts;
6856
6857 if (rsm->r_rtr_cnt > 1) {
6858 /*
6859 * This is a retransmit, can we
6860 * really make any assessment at this
6861 * point? We are not really sure of
6862 * the timestamp, is it this or the
6863 * previous transmission?
6864 *
6865 * Lets wait for something better that
6866 * is not retransmitted.
6867 */
6868 return;
6869 }
6870 seq = tp->gput_seq;
6871 ts = tp->gput_ts;
6872 rack->app_limited_needs_set = 0;
6873 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
6874 /* Do we start at a new end? */
6875 if ((use_which == RACK_USE_BEG) &&
6876 SEQ_GEQ(rsm->r_start, tp->gput_seq)) {
6877 /*
6878 * When we get an ACK that just eats
6879 * up some of the rsm, we set RACK_USE_BEG
6880 * since whats at r_start (i.e. th_ack)
6881 * is left unacked and thats where the
6882 * measurement not starts.
6883 */
6884 tp->gput_seq = rsm->r_start;
6885 rack->r_ctl.rc_gp_output_ts = rsm->usec_orig_send;
6886 }
6887 if ((use_which == RACK_USE_END) &&
6888 SEQ_GEQ(rsm->r_end, tp->gput_seq)) {
6889 /*
6890 * We use the end when the cumack
6891 * is moving forward and completely
6892 * deleting the rsm passed so basically
6893 * r_end holds th_ack.
6894 *
6895 * For SACK's we also want to use the end
6896 * since this piece just got sacked and
6897 * we want to target anything after that
6898 * in our measurement.
6899 */
6900 tp->gput_seq = rsm->r_end;
6901 rack->r_ctl.rc_gp_output_ts = rsm->usec_orig_send;
6902 }
6903 if (use_which == RACK_USE_END_OR_THACK) {
6904 /*
6905 * special case for ack moving forward,
6906 * not a sack, we need to move all the
6907 * way up to where this ack cum-ack moves
6908 * to.
6909 */
6910 if (SEQ_GT(th_ack, rsm->r_end))
6911 tp->gput_seq = th_ack;
6912 else
6913 tp->gput_seq = rsm->r_end;
6914 rack->r_ctl.rc_gp_output_ts = rsm->usec_orig_send;
6915 }
6916 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) {
6917 /*
6918 * We moved beyond this guy's range, re-calculate
6919 * the new end point.
6920 */
6921 if (rack->rc_gp_filled == 0) {
6922 tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
6923 } else {
6924 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
6925 }
6926 }
6927 /*
6928 * We are moving the goal post, we may be able to clear the
6929 * measure_saw_probe_rtt flag.
6930 */
6931 if ((rack->in_probe_rtt == 0) &&
6932 (rack->measure_saw_probe_rtt) &&
6933 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
6934 rack->measure_saw_probe_rtt = 0;
6935 rack_log_pacing_delay_calc(rack, ts, tp->gput_ts,
6936 seq, tp->gput_seq, 0, 5, line, NULL);
6937 if (rack->rc_gp_filled &&
6938 ((tp->gput_ack - tp->gput_seq) <
6939 max(rc_init_window(rack), (MIN_GP_WIN *
6940 ctf_fixed_maxseg(tp))))) {
6941 /*
6942 * There is no sense of continuing this measurement
6943 * because its too small to gain us anything we
6944 * trust. Skip it and that way we can start a new
6945 * measurement quicker.
6946 */
6947 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq,
6948 0, 0, 0, 6, __LINE__, NULL);
6949 tp->t_flags &= ~TF_GPUTINPROG;
6950 }
6951 }
6952 }
6953
6954 static uint32_t
rack_proc_sack_blk(struct tcpcb * tp,struct tcp_rack * rack,struct sackblk * sack,struct tcpopt * to,struct rack_sendmap ** prsm,uint32_t cts,int * moved_two)6955 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
6956 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, int *moved_two)
6957 {
6958 uint32_t start, end, changed = 0;
6959 struct rack_sendmap stack_map;
6960 struct rack_sendmap *rsm, *nrsm, fe, *insret, *prev, *next;
6961 int32_t used_ref = 1;
6962 int moved = 0;
6963
6964 start = sack->start;
6965 end = sack->end;
6966 rsm = *prsm;
6967 memset(&fe, 0, sizeof(fe));
6968 do_rest_ofb:
6969 if ((rsm == NULL) ||
6970 (SEQ_LT(end, rsm->r_start)) ||
6971 (SEQ_GEQ(start, rsm->r_end)) ||
6972 (SEQ_LT(start, rsm->r_start))) {
6973 /*
6974 * We are not in the right spot,
6975 * find the correct spot in the tree.
6976 */
6977 used_ref = 0;
6978 fe.r_start = start;
6979 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
6980 moved++;
6981 }
6982 if (rsm == NULL) {
6983 /* TSNH */
6984 goto out;
6985 }
6986 /* Ok we have an ACK for some piece of this rsm */
6987 if (rsm->r_start != start) {
6988 if ((rsm->r_flags & RACK_ACKED) == 0) {
6989 /**
6990 * Need to split this in two pieces the before and after,
6991 * the before remains in the map, the after must be
6992 * added. In other words we have:
6993 * rsm |--------------|
6994 * sackblk |------->
6995 * rsm will become
6996 * rsm |---|
6997 * and nrsm will be the sacked piece
6998 * nrsm |----------|
6999 *
7000 * But before we start down that path lets
7001 * see if the sack spans over on top of
7002 * the next guy and it is already sacked.
7003 */
7004 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7005 if (next && (next->r_flags & RACK_ACKED) &&
7006 SEQ_GEQ(end, next->r_start)) {
7007 /**
7008 * So the next one is already acked, and
7009 * we can thus by hookery use our stack_map
7010 * to reflect the piece being sacked and
7011 * then adjust the two tree entries moving
7012 * the start and ends around. So we start like:
7013 * rsm |------------| (not-acked)
7014 * next |-----------| (acked)
7015 * sackblk |-------->
7016 * We want to end like so:
7017 * rsm |------| (not-acked)
7018 * next |-----------------| (acked)
7019 * nrsm |-----|
7020 * Where nrsm is a temporary stack piece we
7021 * use to update all the gizmos.
7022 */
7023 /* Copy up our fudge block */
7024 nrsm = &stack_map;
7025 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
7026 /* Now adjust our tree blocks */
7027 rsm->r_end = start;
7028 next->r_start = start;
7029 /* Clear out the dup ack count of the remainder */
7030 rsm->r_dupack = 0;
7031 rsm->r_just_ret = 0;
7032 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7033 /* Now lets make sure our fudge block is right */
7034 nrsm->r_start = start;
7035 /* Now lets update all the stats and such */
7036 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
7037 if (rack->app_limited_needs_set)
7038 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
7039 changed += (nrsm->r_end - nrsm->r_start);
7040 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
7041 if (nrsm->r_flags & RACK_SACK_PASSED) {
7042 counter_u64_add(rack_reorder_seen, 1);
7043 rack->r_ctl.rc_reorder_ts = cts;
7044 }
7045 /*
7046 * Now we want to go up from rsm (the
7047 * one left un-acked) to the next one
7048 * in the tmap. We do this so when
7049 * we walk backwards we include marking
7050 * sack-passed on rsm (The one passed in
7051 * is skipped since it is generally called
7052 * on something sacked before removing it
7053 * from the tmap).
7054 */
7055 if (rsm->r_in_tmap) {
7056 nrsm = TAILQ_NEXT(rsm, r_tnext);
7057 /*
7058 * Now that we have the next
7059 * one walk backwards from there.
7060 */
7061 if (nrsm && nrsm->r_in_tmap)
7062 rack_log_sack_passed(tp, rack, nrsm);
7063 }
7064 /* Now are we done? */
7065 if (SEQ_LT(end, next->r_end) ||
7066 (end == next->r_end)) {
7067 /* Done with block */
7068 goto out;
7069 }
7070 counter_u64_add(rack_sack_used_next_merge, 1);
7071 /* Postion for the next block */
7072 start = next->r_end;
7073 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, next);
7074 if (rsm == NULL)
7075 goto out;
7076 } else {
7077 /**
7078 * We can't use any hookery here, so we
7079 * need to split the map. We enter like
7080 * so:
7081 * rsm |--------|
7082 * sackblk |----->
7083 * We will add the new block nrsm and
7084 * that will be the new portion, and then
7085 * fall through after reseting rsm. So we
7086 * split and look like this:
7087 * rsm |----|
7088 * sackblk |----->
7089 * nrsm |---|
7090 * We then fall through reseting
7091 * rsm to nrsm, so the next block
7092 * picks it up.
7093 */
7094 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
7095 if (nrsm == NULL) {
7096 /*
7097 * failed XXXrrs what can we do but loose the sack
7098 * info?
7099 */
7100 goto out;
7101 }
7102 counter_u64_add(rack_sack_splits, 1);
7103 rack_clone_rsm(rack, nrsm, rsm, start);
7104 rsm->r_just_ret = 0;
7105 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7106 #ifdef INVARIANTS
7107 if (insret != NULL) {
7108 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7109 nrsm, insret, rack, rsm);
7110 }
7111 #endif
7112 if (rsm->r_in_tmap) {
7113 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7114 nrsm->r_in_tmap = 1;
7115 }
7116 rsm->r_flags &= (~RACK_HAS_FIN);
7117 /* Position us to point to the new nrsm that starts the sack blk */
7118 rsm = nrsm;
7119 }
7120 } else {
7121 /* Already sacked this piece */
7122 counter_u64_add(rack_sack_skipped_acked, 1);
7123 moved++;
7124 if (end == rsm->r_end) {
7125 /* Done with block */
7126 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7127 goto out;
7128 } else if (SEQ_LT(end, rsm->r_end)) {
7129 /* A partial sack to a already sacked block */
7130 moved++;
7131 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7132 goto out;
7133 } else {
7134 /*
7135 * The end goes beyond this guy
7136 * repostion the start to the
7137 * next block.
7138 */
7139 start = rsm->r_end;
7140 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7141 if (rsm == NULL)
7142 goto out;
7143 }
7144 }
7145 }
7146 if (SEQ_GEQ(end, rsm->r_end)) {
7147 /**
7148 * The end of this block is either beyond this guy or right
7149 * at this guy. I.e.:
7150 * rsm --- |-----|
7151 * end |-----|
7152 * <or>
7153 * end |---------|
7154 */
7155 if ((rsm->r_flags & RACK_ACKED) == 0) {
7156 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
7157 changed += (rsm->r_end - rsm->r_start);
7158 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7159 if (rsm->r_in_tmap) /* should be true */
7160 rack_log_sack_passed(tp, rack, rsm);
7161 /* Is Reordering occuring? */
7162 if (rsm->r_flags & RACK_SACK_PASSED) {
7163 rsm->r_flags &= ~RACK_SACK_PASSED;
7164 counter_u64_add(rack_reorder_seen, 1);
7165 rack->r_ctl.rc_reorder_ts = cts;
7166 }
7167 if (rack->app_limited_needs_set)
7168 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
7169 rsm->r_ack_arrival = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
7170 rsm->r_flags |= RACK_ACKED;
7171 rsm->r_flags &= ~RACK_TLP;
7172 if (rsm->r_in_tmap) {
7173 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7174 rsm->r_in_tmap = 0;
7175 }
7176 } else {
7177 counter_u64_add(rack_sack_skipped_acked, 1);
7178 moved++;
7179 }
7180 if (end == rsm->r_end) {
7181 /* This block only - done, setup for next */
7182 goto out;
7183 }
7184 /*
7185 * There is more not coverend by this rsm move on
7186 * to the next block in the RB tree.
7187 */
7188 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7189 start = rsm->r_end;
7190 rsm = nrsm;
7191 if (rsm == NULL)
7192 goto out;
7193 goto do_rest_ofb;
7194 }
7195 /**
7196 * The end of this sack block is smaller than
7197 * our rsm i.e.:
7198 * rsm --- |-----|
7199 * end |--|
7200 */
7201 if ((rsm->r_flags & RACK_ACKED) == 0) {
7202 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7203 if (prev && (prev->r_flags & RACK_ACKED)) {
7204 /**
7205 * Goal, we want the right remainder of rsm to shrink
7206 * in place and span from (rsm->r_start = end) to rsm->r_end.
7207 * We want to expand prev to go all the way
7208 * to prev->r_end <- end.
7209 * so in the tree we have before:
7210 * prev |--------| (acked)
7211 * rsm |-------| (non-acked)
7212 * sackblk |-|
7213 * We churn it so we end up with
7214 * prev |----------| (acked)
7215 * rsm |-----| (non-acked)
7216 * nrsm |-| (temporary)
7217 */
7218 nrsm = &stack_map;
7219 memcpy(nrsm, rsm, sizeof(struct rack_sendmap));
7220 prev->r_end = end;
7221 rsm->r_start = end;
7222 /* Now adjust nrsm (stack copy) to be
7223 * the one that is the small
7224 * piece that was "sacked".
7225 */
7226 nrsm->r_end = end;
7227 rsm->r_dupack = 0;
7228 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7229 /*
7230 * Now nrsm is our new little piece
7231 * that is acked (which was merged
7232 * to prev). Update the rtt and changed
7233 * based on that. Also check for reordering.
7234 */
7235 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0);
7236 if (rack->app_limited_needs_set)
7237 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END);
7238 changed += (nrsm->r_end - nrsm->r_start);
7239 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start);
7240 if (nrsm->r_flags & RACK_SACK_PASSED) {
7241 counter_u64_add(rack_reorder_seen, 1);
7242 rack->r_ctl.rc_reorder_ts = cts;
7243 }
7244 rsm = prev;
7245 counter_u64_add(rack_sack_used_prev_merge, 1);
7246 } else {
7247 /**
7248 * This is the case where our previous
7249 * block is not acked either, so we must
7250 * split the block in two.
7251 */
7252 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
7253 if (nrsm == NULL) {
7254 /* failed rrs what can we do but loose the sack info? */
7255 goto out;
7256 }
7257 /**
7258 * In this case nrsm becomes
7259 * nrsm->r_start = end;
7260 * nrsm->r_end = rsm->r_end;
7261 * which is un-acked.
7262 * <and>
7263 * rsm->r_end = nrsm->r_start;
7264 * i.e. the remaining un-acked
7265 * piece is left on the left
7266 * hand side.
7267 *
7268 * So we start like this
7269 * rsm |----------| (not acked)
7270 * sackblk |---|
7271 * build it so we have
7272 * rsm |---| (acked)
7273 * nrsm |------| (not acked)
7274 */
7275 counter_u64_add(rack_sack_splits, 1);
7276 rack_clone_rsm(rack, nrsm, rsm, end);
7277 rsm->r_flags &= (~RACK_HAS_FIN);
7278 rsm->r_just_ret = 0;
7279 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
7280 #ifdef INVARIANTS
7281 if (insret != NULL) {
7282 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
7283 nrsm, insret, rack, rsm);
7284 }
7285 #endif
7286 if (rsm->r_in_tmap) {
7287 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
7288 nrsm->r_in_tmap = 1;
7289 }
7290 nrsm->r_dupack = 0;
7291 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2);
7292 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0);
7293 changed += (rsm->r_end - rsm->r_start);
7294 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
7295 if (rsm->r_in_tmap) /* should be true */
7296 rack_log_sack_passed(tp, rack, rsm);
7297 /* Is Reordering occuring? */
7298 if (rsm->r_flags & RACK_SACK_PASSED) {
7299 rsm->r_flags &= ~RACK_SACK_PASSED;
7300 counter_u64_add(rack_reorder_seen, 1);
7301 rack->r_ctl.rc_reorder_ts = cts;
7302 }
7303 if (rack->app_limited_needs_set)
7304 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END);
7305 rsm->r_ack_arrival = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
7306 rsm->r_flags |= RACK_ACKED;
7307 rsm->r_flags &= ~RACK_TLP;
7308 if (rsm->r_in_tmap) {
7309 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7310 rsm->r_in_tmap = 0;
7311 }
7312 }
7313 } else if (start != end){
7314 /*
7315 * The block was already acked.
7316 */
7317 counter_u64_add(rack_sack_skipped_acked, 1);
7318 moved++;
7319 }
7320 out:
7321 if (rsm && (rsm->r_flags & RACK_ACKED)) {
7322 /*
7323 * Now can we merge where we worked
7324 * with either the previous or
7325 * next block?
7326 */
7327 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7328 while (next) {
7329 if (next->r_flags & RACK_ACKED) {
7330 /* yep this and next can be merged */
7331 rsm = rack_merge_rsm(rack, rsm, next);
7332 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7333 } else
7334 break;
7335 }
7336 /* Now what about the previous? */
7337 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7338 while (prev) {
7339 if (prev->r_flags & RACK_ACKED) {
7340 /* yep the previous and this can be merged */
7341 rsm = rack_merge_rsm(rack, prev, rsm);
7342 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7343 } else
7344 break;
7345 }
7346 }
7347 if (used_ref == 0) {
7348 counter_u64_add(rack_sack_proc_all, 1);
7349 } else {
7350 counter_u64_add(rack_sack_proc_short, 1);
7351 }
7352 /* Save off the next one for quick reference. */
7353 if (rsm)
7354 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7355 else
7356 nrsm = NULL;
7357 *prsm = rack->r_ctl.rc_sacklast = nrsm;
7358 /* Pass back the moved. */
7359 *moved_two = moved;
7360 return (changed);
7361 }
7362
7363 static void inline
rack_peer_reneges(struct tcp_rack * rack,struct rack_sendmap * rsm,tcp_seq th_ack)7364 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
7365 {
7366 struct rack_sendmap *tmap;
7367
7368 tmap = NULL;
7369 while (rsm && (rsm->r_flags & RACK_ACKED)) {
7370 /* Its no longer sacked, mark it so */
7371 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7372 #ifdef INVARIANTS
7373 if (rsm->r_in_tmap) {
7374 panic("rack:%p rsm:%p flags:0x%x in tmap?",
7375 rack, rsm, rsm->r_flags);
7376 }
7377 #endif
7378 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
7379 /* Rebuild it into our tmap */
7380 if (tmap == NULL) {
7381 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7382 tmap = rsm;
7383 } else {
7384 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
7385 tmap = rsm;
7386 }
7387 tmap->r_in_tmap = 1;
7388 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7389 }
7390 /*
7391 * Now lets possibly clear the sack filter so we start
7392 * recognizing sacks that cover this area.
7393 */
7394 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
7395
7396 }
7397
7398 static void
rack_do_decay(struct tcp_rack * rack)7399 rack_do_decay(struct tcp_rack *rack)
7400 {
7401 struct timeval res;
7402
7403 #define timersub(tvp, uvp, vvp) \
7404 do { \
7405 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
7406 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
7407 if ((vvp)->tv_usec < 0) { \
7408 (vvp)->tv_sec--; \
7409 (vvp)->tv_usec += 1000000; \
7410 } \
7411 } while (0)
7412
7413 timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res);
7414 #undef timersub
7415
7416 rack->r_ctl.input_pkt++;
7417 if ((rack->rc_in_persist) ||
7418 (res.tv_sec >= 1) ||
7419 (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) {
7420 /*
7421 * Check for decay of non-SAD,
7422 * we want all SAD detection metrics to
7423 * decay 1/4 per second (or more) passed.
7424 */
7425 uint32_t pkt_delta;
7426
7427 pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt;
7428 /* Update our saved tracking values */
7429 rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt;
7430 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
7431 /* Now do we escape without decay? */
7432 #ifdef NETFLIX_EXP_DETECTION
7433 if (rack->rc_in_persist ||
7434 (rack->rc_tp->snd_max == rack->rc_tp->snd_una) ||
7435 (pkt_delta < tcp_sad_low_pps)){
7436 /*
7437 * We don't decay idle connections
7438 * or ones that have a low input pps.
7439 */
7440 return;
7441 }
7442 /* Decay the counters */
7443 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count,
7444 tcp_sad_decay_val);
7445 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count,
7446 tcp_sad_decay_val);
7447 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra,
7448 tcp_sad_decay_val);
7449 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move,
7450 tcp_sad_decay_val);
7451 #endif
7452 }
7453 }
7454
7455 static void
rack_log_ack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th)7456 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th)
7457 {
7458 uint32_t changed, entered_recovery = 0;
7459 struct tcp_rack *rack;
7460 struct rack_sendmap *rsm, *rm;
7461 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
7462 register uint32_t th_ack;
7463 int32_t i, j, k, num_sack_blks = 0;
7464 uint32_t cts, acked, ack_point, sack_changed = 0;
7465 int loop_start = 0, moved_two = 0;
7466 uint32_t tsused;
7467
7468 INP_WLOCK_ASSERT(tp->t_inpcb);
7469 if (th->th_flags & TH_RST) {
7470 /* We don't log resets */
7471 return;
7472 }
7473 rack = (struct tcp_rack *)tp->t_fb_ptr;
7474 cts = tcp_ts_getticks();
7475 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
7476 changed = 0;
7477 th_ack = th->th_ack;
7478 if (rack->sack_attack_disable == 0)
7479 rack_do_decay(rack);
7480 if (BYTES_THIS_ACK(tp, th) >= ctf_fixed_maxseg(rack->rc_tp)) {
7481 /*
7482 * You only get credit for
7483 * MSS and greater (and you get extra
7484 * credit for larger cum-ack moves).
7485 */
7486 int ac;
7487
7488 ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp);
7489 rack->r_ctl.ack_count += ac;
7490 counter_u64_add(rack_ack_total, ac);
7491 }
7492 if (rack->r_ctl.ack_count > 0xfff00000) {
7493 /*
7494 * reduce the number to keep us under
7495 * a uint32_t.
7496 */
7497 rack->r_ctl.ack_count /= 2;
7498 rack->r_ctl.sack_count /= 2;
7499 }
7500 if (SEQ_GT(th_ack, tp->snd_una)) {
7501 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
7502 tp->t_acktime = ticks;
7503 }
7504 if (rsm && SEQ_GT(th_ack, rsm->r_start))
7505 changed = th_ack - rsm->r_start;
7506 if (changed) {
7507 /*
7508 * The ACK point is advancing to th_ack, we must drop off
7509 * the packets in the rack log and calculate any eligble
7510 * RTT's.
7511 */
7512 rack->r_wanted_output = 1;
7513 more:
7514 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
7515 if (rsm == NULL) {
7516 if ((th_ack - 1) == tp->iss) {
7517 /*
7518 * For the SYN incoming case we will not
7519 * have called tcp_output for the sending of
7520 * the SYN, so there will be no map. All
7521 * other cases should probably be a panic.
7522 */
7523 goto proc_sack;
7524 }
7525 if (tp->t_flags & TF_SENTFIN) {
7526 /* if we send a FIN we will not hav a map */
7527 goto proc_sack;
7528 }
7529 #ifdef INVARIANTS
7530 panic("No rack map tp:%p for th:%p state:%d rack:%p snd_una:%u snd_max:%u snd_nxt:%u chg:%d\n",
7531 tp,
7532 th, tp->t_state, rack,
7533 tp->snd_una, tp->snd_max, tp->snd_nxt, changed);
7534 #endif
7535 goto proc_sack;
7536 }
7537 if (SEQ_LT(th_ack, rsm->r_start)) {
7538 /* Huh map is missing this */
7539 #ifdef INVARIANTS
7540 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
7541 rsm->r_start,
7542 th_ack, tp->t_state, rack->r_state);
7543 #endif
7544 goto proc_sack;
7545 }
7546 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack);
7547 /* Now do we consume the whole thing? */
7548 if (SEQ_GEQ(th_ack, rsm->r_end)) {
7549 /* Its all consumed. */
7550 uint32_t left;
7551 uint8_t newly_acked;
7552
7553 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
7554 rsm->r_rtr_bytes = 0;
7555 /* Record the time of highest cumack sent */
7556 rack->r_ctl.rc_gp_cumack_ts = rsm->usec_orig_send;
7557 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
7558 #ifdef INVARIANTS
7559 if (rm != rsm) {
7560 panic("removing head in rack:%p rsm:%p rm:%p",
7561 rack, rsm, rm);
7562 }
7563 #endif
7564 if (rsm->r_in_tmap) {
7565 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
7566 rsm->r_in_tmap = 0;
7567 }
7568 newly_acked = 1;
7569 if (rsm->r_flags & RACK_ACKED) {
7570 /*
7571 * It was acked on the scoreboard -- remove
7572 * it from total
7573 */
7574 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
7575 newly_acked = 0;
7576 } else if (rsm->r_flags & RACK_SACK_PASSED) {
7577 /*
7578 * There are segments ACKED on the
7579 * scoreboard further up. We are seeing
7580 * reordering.
7581 */
7582 rsm->r_flags &= ~RACK_SACK_PASSED;
7583 counter_u64_add(rack_reorder_seen, 1);
7584 rsm->r_ack_arrival = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
7585 rsm->r_flags |= RACK_ACKED;
7586 rack->r_ctl.rc_reorder_ts = cts;
7587 }
7588 left = th_ack - rsm->r_end;
7589 if (rack->app_limited_needs_set && newly_acked)
7590 rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK);
7591 /* Free back to zone */
7592 rack_free(rack, rsm);
7593 if (left) {
7594 goto more;
7595 }
7596 goto proc_sack;
7597 }
7598 if (rsm->r_flags & RACK_ACKED) {
7599 /*
7600 * It was acked on the scoreboard -- remove it from
7601 * total for the part being cum-acked.
7602 */
7603 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
7604 }
7605 /*
7606 * Clear the dup ack count for
7607 * the piece that remains.
7608 */
7609 rsm->r_dupack = 0;
7610 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2);
7611 if (rsm->r_rtr_bytes) {
7612 /*
7613 * It was retransmitted adjust the
7614 * sack holes for what was acked.
7615 */
7616 int ack_am;
7617
7618 ack_am = (th_ack - rsm->r_start);
7619 if (ack_am >= rsm->r_rtr_bytes) {
7620 rack->r_ctl.rc_holes_rxt -= ack_am;
7621 rsm->r_rtr_bytes -= ack_am;
7622 }
7623 }
7624 /*
7625 * Update where the piece starts and record
7626 * the time of send of highest cumack sent.
7627 */
7628 rack->r_ctl.rc_gp_cumack_ts = rsm->usec_orig_send;
7629 rsm->r_start = th_ack;
7630 if (rack->app_limited_needs_set)
7631 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG);
7632 }
7633 proc_sack:
7634 /* Check for reneging */
7635 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
7636 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
7637 /*
7638 * The peer has moved snd_una up to
7639 * the edge of this send, i.e. one
7640 * that it had previously acked. The only
7641 * way that can be true if the peer threw
7642 * away data (space issues) that it had
7643 * previously sacked (else it would have
7644 * given us snd_una up to (rsm->r_end).
7645 * We need to undo the acked markings here.
7646 *
7647 * Note we have to look to make sure th_ack is
7648 * our rsm->r_start in case we get an old ack
7649 * where th_ack is behind snd_una.
7650 */
7651 rack_peer_reneges(rack, rsm, th->th_ack);
7652 }
7653 if ((to->to_flags & TOF_SACK) == 0) {
7654 /* We are done nothing left */
7655 goto out;
7656 }
7657 /* Sack block processing */
7658 if (SEQ_GT(th_ack, tp->snd_una))
7659 ack_point = th_ack;
7660 else
7661 ack_point = tp->snd_una;
7662 for (i = 0; i < to->to_nsacks; i++) {
7663 bcopy((to->to_sacks + i * TCPOLEN_SACK),
7664 &sack, sizeof(sack));
7665 sack.start = ntohl(sack.start);
7666 sack.end = ntohl(sack.end);
7667 if (SEQ_GT(sack.end, sack.start) &&
7668 SEQ_GT(sack.start, ack_point) &&
7669 SEQ_LT(sack.start, tp->snd_max) &&
7670 SEQ_GT(sack.end, ack_point) &&
7671 SEQ_LEQ(sack.end, tp->snd_max)) {
7672 sack_blocks[num_sack_blks] = sack;
7673 num_sack_blks++;
7674 #ifdef NETFLIX_STATS
7675 } else if (SEQ_LEQ(sack.start, th_ack) &&
7676 SEQ_LEQ(sack.end, th_ack)) {
7677 /*
7678 * Its a D-SACK block.
7679 */
7680 tcp_record_dsack(sack.start, sack.end);
7681 #endif
7682 }
7683 }
7684 /*
7685 * Sort the SACK blocks so we can update the rack scoreboard with
7686 * just one pass.
7687 */
7688 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks,
7689 num_sack_blks, th->th_ack);
7690 ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks);
7691 if (num_sack_blks == 0) {
7692 /* Nothing to sack (DSACKs?) */
7693 goto out_with_totals;
7694 }
7695 if (num_sack_blks < 2) {
7696 /* Only one, we don't need to sort */
7697 goto do_sack_work;
7698 }
7699 /* Sort the sacks */
7700 for (i = 0; i < num_sack_blks; i++) {
7701 for (j = i + 1; j < num_sack_blks; j++) {
7702 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
7703 sack = sack_blocks[i];
7704 sack_blocks[i] = sack_blocks[j];
7705 sack_blocks[j] = sack;
7706 }
7707 }
7708 }
7709 /*
7710 * Now are any of the sack block ends the same (yes some
7711 * implementations send these)?
7712 */
7713 again:
7714 if (num_sack_blks == 0)
7715 goto out_with_totals;
7716 if (num_sack_blks > 1) {
7717 for (i = 0; i < num_sack_blks; i++) {
7718 for (j = i + 1; j < num_sack_blks; j++) {
7719 if (sack_blocks[i].end == sack_blocks[j].end) {
7720 /*
7721 * Ok these two have the same end we
7722 * want the smallest end and then
7723 * throw away the larger and start
7724 * again.
7725 */
7726 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
7727 /*
7728 * The second block covers
7729 * more area use that
7730 */
7731 sack_blocks[i].start = sack_blocks[j].start;
7732 }
7733 /*
7734 * Now collapse out the dup-sack and
7735 * lower the count
7736 */
7737 for (k = (j + 1); k < num_sack_blks; k++) {
7738 sack_blocks[j].start = sack_blocks[k].start;
7739 sack_blocks[j].end = sack_blocks[k].end;
7740 j++;
7741 }
7742 num_sack_blks--;
7743 goto again;
7744 }
7745 }
7746 }
7747 }
7748 do_sack_work:
7749 /*
7750 * First lets look to see if
7751 * we have retransmitted and
7752 * can use the transmit next?
7753 */
7754 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
7755 if (rsm &&
7756 SEQ_GT(sack_blocks[0].end, rsm->r_start) &&
7757 SEQ_LT(sack_blocks[0].start, rsm->r_end)) {
7758 /*
7759 * We probably did the FR and the next
7760 * SACK in continues as we would expect.
7761 */
7762 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two);
7763 if (acked) {
7764 rack->r_wanted_output = 1;
7765 changed += acked;
7766 sack_changed += acked;
7767 }
7768 if (num_sack_blks == 1) {
7769 /*
7770 * This is what we would expect from
7771 * a normal implementation to happen
7772 * after we have retransmitted the FR,
7773 * i.e the sack-filter pushes down
7774 * to 1 block and the next to be retransmitted
7775 * is the sequence in the sack block (has more
7776 * are acked). Count this as ACK'd data to boost
7777 * up the chances of recovering any false positives.
7778 */
7779 rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp));
7780 counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp)));
7781 counter_u64_add(rack_express_sack, 1);
7782 if (rack->r_ctl.ack_count > 0xfff00000) {
7783 /*
7784 * reduce the number to keep us under
7785 * a uint32_t.
7786 */
7787 rack->r_ctl.ack_count /= 2;
7788 rack->r_ctl.sack_count /= 2;
7789 }
7790 goto out_with_totals;
7791 } else {
7792 /*
7793 * Start the loop through the
7794 * rest of blocks, past the first block.
7795 */
7796 moved_two = 0;
7797 loop_start = 1;
7798 }
7799 }
7800 /* Its a sack of some sort */
7801 rack->r_ctl.sack_count++;
7802 if (rack->r_ctl.sack_count > 0xfff00000) {
7803 /*
7804 * reduce the number to keep us under
7805 * a uint32_t.
7806 */
7807 rack->r_ctl.ack_count /= 2;
7808 rack->r_ctl.sack_count /= 2;
7809 }
7810 counter_u64_add(rack_sack_total, 1);
7811 if (rack->sack_attack_disable) {
7812 /* An attacker disablement is in place */
7813 if (num_sack_blks > 1) {
7814 rack->r_ctl.sack_count += (num_sack_blks - 1);
7815 rack->r_ctl.sack_moved_extra++;
7816 counter_u64_add(rack_move_some, 1);
7817 if (rack->r_ctl.sack_moved_extra > 0xfff00000) {
7818 rack->r_ctl.sack_moved_extra /= 2;
7819 rack->r_ctl.sack_noextra_move /= 2;
7820 }
7821 }
7822 goto out;
7823 }
7824 rsm = rack->r_ctl.rc_sacklast;
7825 for (i = loop_start; i < num_sack_blks; i++) {
7826 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &moved_two);
7827 if (acked) {
7828 rack->r_wanted_output = 1;
7829 changed += acked;
7830 sack_changed += acked;
7831 }
7832 if (moved_two) {
7833 /*
7834 * If we did not get a SACK for at least a MSS and
7835 * had to move at all, or if we moved more than our
7836 * threshold, it counts against the "extra" move.
7837 */
7838 rack->r_ctl.sack_moved_extra += moved_two;
7839 counter_u64_add(rack_move_some, 1);
7840 } else {
7841 /*
7842 * else we did not have to move
7843 * any more than we would expect.
7844 */
7845 rack->r_ctl.sack_noextra_move++;
7846 counter_u64_add(rack_move_none, 1);
7847 }
7848 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) {
7849 /*
7850 * If the SACK was not a full MSS then
7851 * we add to sack_count the number of
7852 * MSS's (or possibly more than
7853 * a MSS if its a TSO send) we had to skip by.
7854 */
7855 rack->r_ctl.sack_count += moved_two;
7856 counter_u64_add(rack_sack_total, moved_two);
7857 }
7858 /*
7859 * Now we need to setup for the next
7860 * round. First we make sure we won't
7861 * exceed the size of our uint32_t on
7862 * the various counts, and then clear out
7863 * moved_two.
7864 */
7865 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) ||
7866 (rack->r_ctl.sack_noextra_move > 0xfff00000)) {
7867 rack->r_ctl.sack_moved_extra /= 2;
7868 rack->r_ctl.sack_noextra_move /= 2;
7869 }
7870 if (rack->r_ctl.sack_count > 0xfff00000) {
7871 rack->r_ctl.ack_count /= 2;
7872 rack->r_ctl.sack_count /= 2;
7873 }
7874 moved_two = 0;
7875 }
7876 out_with_totals:
7877 if (num_sack_blks > 1) {
7878 /*
7879 * You get an extra stroke if
7880 * you have more than one sack-blk, this
7881 * could be where we are skipping forward
7882 * and the sack-filter is still working, or
7883 * it could be an attacker constantly
7884 * moving us.
7885 */
7886 rack->r_ctl.sack_moved_extra++;
7887 counter_u64_add(rack_move_some, 1);
7888 }
7889 out:
7890 #ifdef NETFLIX_EXP_DETECTION
7891 if ((rack->do_detection || tcp_force_detection) &&
7892 tcp_sack_to_ack_thresh &&
7893 tcp_sack_to_move_thresh &&
7894 ((rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum) || rack->sack_attack_disable)) {
7895 /*
7896 * We have thresholds set to find
7897 * possible attackers and disable sack.
7898 * Check them.
7899 */
7900 uint64_t ackratio, moveratio, movetotal;
7901
7902 /* Log detecting */
7903 rack_log_sad(rack, 1);
7904 ackratio = (uint64_t)(rack->r_ctl.sack_count);
7905 ackratio *= (uint64_t)(1000);
7906 if (rack->r_ctl.ack_count)
7907 ackratio /= (uint64_t)(rack->r_ctl.ack_count);
7908 else {
7909 /* We really should not hit here */
7910 ackratio = 1000;
7911 }
7912 if ((rack->sack_attack_disable == 0) &&
7913 (ackratio > rack_highest_sack_thresh_seen))
7914 rack_highest_sack_thresh_seen = (uint32_t)ackratio;
7915 movetotal = rack->r_ctl.sack_moved_extra;
7916 movetotal += rack->r_ctl.sack_noextra_move;
7917 moveratio = rack->r_ctl.sack_moved_extra;
7918 moveratio *= (uint64_t)1000;
7919 if (movetotal)
7920 moveratio /= movetotal;
7921 else {
7922 /* No moves, thats pretty good */
7923 moveratio = 0;
7924 }
7925 if ((rack->sack_attack_disable == 0) &&
7926 (moveratio > rack_highest_move_thresh_seen))
7927 rack_highest_move_thresh_seen = (uint32_t)moveratio;
7928 if (rack->sack_attack_disable == 0) {
7929 if ((ackratio > tcp_sack_to_ack_thresh) &&
7930 (moveratio > tcp_sack_to_move_thresh)) {
7931 /* Disable sack processing */
7932 rack->sack_attack_disable = 1;
7933 if (rack->r_rep_attack == 0) {
7934 rack->r_rep_attack = 1;
7935 counter_u64_add(rack_sack_attacks_detected, 1);
7936 }
7937 if (tcp_attack_on_turns_on_logging) {
7938 /*
7939 * Turn on logging, used for debugging
7940 * false positives.
7941 */
7942 rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging;
7943 }
7944 /* Clamp the cwnd at flight size */
7945 rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd;
7946 rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
7947 rack_log_sad(rack, 2);
7948 }
7949 } else {
7950 /* We are sack-disabled check for false positives */
7951 if ((ackratio <= tcp_restoral_thresh) ||
7952 (rack->r_ctl.rc_num_maps_alloced < tcp_map_minimum)) {
7953 rack->sack_attack_disable = 0;
7954 rack_log_sad(rack, 3);
7955 /* Restart counting */
7956 rack->r_ctl.sack_count = 0;
7957 rack->r_ctl.sack_moved_extra = 0;
7958 rack->r_ctl.sack_noextra_move = 1;
7959 rack->r_ctl.ack_count = max(1,
7960 (BYTES_THIS_ACK(tp, th)/ctf_fixed_maxseg(rack->rc_tp)));
7961
7962 if (rack->r_rep_reverse == 0) {
7963 rack->r_rep_reverse = 1;
7964 counter_u64_add(rack_sack_attacks_reversed, 1);
7965 }
7966 /* Restore the cwnd */
7967 if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd)
7968 rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd;
7969 }
7970 }
7971 }
7972 #endif
7973 if (changed) {
7974 /* Something changed cancel the rack timer */
7975 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
7976 }
7977 tsused = tcp_ts_getticks();
7978 rsm = tcp_rack_output(tp, rack, tsused);
7979 if ((!IN_RECOVERY(tp->t_flags)) &&
7980 rsm) {
7981 /* Enter recovery */
7982 rack->r_ctl.rc_rsm_start = rsm->r_start;
7983 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
7984 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
7985 entered_recovery = 1;
7986 rack_cong_signal(tp, NULL, CC_NDUPACK);
7987 /*
7988 * When we enter recovery we need to assure we send
7989 * one packet.
7990 */
7991 if (rack->rack_no_prr == 0) {
7992 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp);
7993 rack_log_to_prr(rack, 8, 0);
7994 }
7995 rack->r_timer_override = 1;
7996 rack->r_early = 0;
7997 rack->r_ctl.rc_agg_early = 0;
7998 } else if (IN_RECOVERY(tp->t_flags) &&
7999 rsm &&
8000 (rack->r_rr_config == 3)) {
8001 /*
8002 * Assure we can output and we get no
8003 * remembered pace time except the retransmit.
8004 */
8005 rack->r_timer_override = 1;
8006 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
8007 rack->r_ctl.rc_resend = rsm;
8008 }
8009 if (IN_RECOVERY(tp->t_flags) &&
8010 (rack->rack_no_prr == 0) &&
8011 (entered_recovery == 0)) {
8012 /* Deal with PRR here (in recovery only) */
8013 uint32_t pipe, snd_una;
8014
8015 rack->r_ctl.rc_prr_delivered += changed;
8016 /* Compute prr_sndcnt */
8017 if (SEQ_GT(tp->snd_una, th_ack)) {
8018 snd_una = tp->snd_una;
8019 } else {
8020 snd_una = th_ack;
8021 }
8022 pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
8023 if (pipe > tp->snd_ssthresh) {
8024 long sndcnt;
8025
8026 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
8027 if (rack->r_ctl.rc_prr_recovery_fs > 0)
8028 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
8029 else {
8030 rack->r_ctl.rc_prr_sndcnt = 0;
8031 rack_log_to_prr(rack, 9, 0);
8032 sndcnt = 0;
8033 }
8034 sndcnt++;
8035 if (sndcnt > (long)rack->r_ctl.rc_prr_out)
8036 sndcnt -= rack->r_ctl.rc_prr_out;
8037 else
8038 sndcnt = 0;
8039 rack->r_ctl.rc_prr_sndcnt = sndcnt;
8040 rack_log_to_prr(rack, 10, 0);
8041 } else {
8042 uint32_t limit;
8043
8044 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
8045 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
8046 else
8047 limit = 0;
8048 if (changed > limit)
8049 limit = changed;
8050 limit += ctf_fixed_maxseg(tp);
8051 if (tp->snd_ssthresh > pipe) {
8052 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
8053 rack_log_to_prr(rack, 11, 0);
8054 } else {
8055 rack->r_ctl.rc_prr_sndcnt = min(0, limit);
8056 rack_log_to_prr(rack, 12, 0);
8057 }
8058 }
8059 if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) &&
8060 ((rack->rc_inp->inp_in_hpts == 0) &&
8061 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) {
8062 /*
8063 * If you are pacing output you don't want
8064 * to override.
8065 */
8066 rack->r_early = 0;
8067 rack->r_ctl.rc_agg_early = 0;
8068 rack->r_timer_override = 1;
8069 }
8070 }
8071 }
8072
8073 static void
rack_strike_dupack(struct tcp_rack * rack)8074 rack_strike_dupack(struct tcp_rack *rack)
8075 {
8076 struct rack_sendmap *rsm;
8077
8078 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
8079 if (rsm && (rsm->r_dupack < 0xff)) {
8080 rsm->r_dupack++;
8081 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) {
8082 rack->r_wanted_output = 1;
8083 rack->r_timer_override = 1;
8084 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3);
8085 } else {
8086 rack_log_retran_reason(rack, rsm, __LINE__, 0, 3);
8087 }
8088 }
8089 }
8090
8091 static void
rack_check_bottom_drag(struct tcpcb * tp,struct tcp_rack * rack,struct socket * so,int32_t acked)8092 rack_check_bottom_drag(struct tcpcb *tp,
8093 struct tcp_rack *rack,
8094 struct socket *so, int32_t acked)
8095 {
8096 uint32_t segsiz, minseg;
8097
8098 segsiz = ctf_fixed_maxseg(tp);
8099 minseg = segsiz;
8100
8101 if (tp->snd_max == tp->snd_una) {
8102 /*
8103 * We are doing dynamic pacing and we are way
8104 * under. Basically everything got acked while
8105 * we were still waiting on the pacer to expire.
8106 *
8107 * This means we need to boost the b/w in
8108 * addition to any earlier boosting of
8109 * the multipler.
8110 */
8111 rack->rc_dragged_bottom = 1;
8112 rack_validate_multipliers_at_or_above100(rack);
8113 /*
8114 * Lets use the segment bytes acked plus
8115 * the lowest RTT seen as the basis to
8116 * form a b/w estimate. This will be off
8117 * due to the fact that the true estimate
8118 * should be around 1/2 the time of the RTT
8119 * but we can settle for that.
8120 */
8121 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) &&
8122 acked) {
8123 uint64_t bw, calc_bw, rtt;
8124
8125 rtt = rack->r_ctl.rack_rs.rs_us_rtt;
8126 bw = acked;
8127 calc_bw = bw * 1000000;
8128 calc_bw /= rtt;
8129 if (rack->r_ctl.last_max_bw &&
8130 (rack->r_ctl.last_max_bw < calc_bw)) {
8131 /*
8132 * If we have a last calculated max bw
8133 * enforce it.
8134 */
8135 calc_bw = rack->r_ctl.last_max_bw;
8136 }
8137 /* now plop it in */
8138 if (rack->rc_gp_filled == 0) {
8139 if (calc_bw > ONE_POINT_TWO_MEG) {
8140 /*
8141 * If we have no measurement
8142 * don't let us set in more than
8143 * 1.2Mbps. If we are still too
8144 * low after pacing with this we
8145 * will hopefully have a max b/w
8146 * available to sanity check things.
8147 */
8148 calc_bw = ONE_POINT_TWO_MEG;
8149 }
8150 rack->r_ctl.rc_rtt_diff = 0;
8151 rack->r_ctl.gp_bw = calc_bw;
8152 rack->rc_gp_filled = 1;
8153 rack->r_ctl.num_avg = RACK_REQ_AVG;
8154 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
8155 } else if (calc_bw > rack->r_ctl.gp_bw) {
8156 rack->r_ctl.rc_rtt_diff = 0;
8157 rack->r_ctl.num_avg = RACK_REQ_AVG;
8158 rack->r_ctl.gp_bw = calc_bw;
8159 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
8160 } else
8161 rack_increase_bw_mul(rack, -1, 0, 0, 1);
8162 /*
8163 * For acks over 1mss we do a extra boost to simulate
8164 * where we would get 2 acks (we want 110 for the mul).
8165 */
8166 if (acked > segsiz)
8167 rack_increase_bw_mul(rack, -1, 0, 0, 1);
8168 } else {
8169 /*
8170 * Huh, this should not be, settle
8171 * for just an old increase.
8172 */
8173 rack_increase_bw_mul(rack, -1, 0, 0, 1);
8174 }
8175 } else if ((IN_RECOVERY(tp->t_flags) == 0) &&
8176 (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)),
8177 minseg)) &&
8178 (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) &&
8179 (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) &&
8180 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <=
8181 (segsiz * rack_req_segs))) {
8182 /*
8183 * We are doing dynamic GP pacing and
8184 * we have everything except 1MSS or less
8185 * bytes left out. We are still pacing away.
8186 * And there is data that could be sent, This
8187 * means we are inserting delayed ack time in
8188 * our measurements because we are pacing too slow.
8189 */
8190 rack_validate_multipliers_at_or_above100(rack);
8191 rack->rc_dragged_bottom = 1;
8192 rack_increase_bw_mul(rack, -1, 0, 0, 1);
8193 }
8194 }
8195
8196 /*
8197 * Return value of 1, we do not need to call rack_process_data().
8198 * return value of 0, rack_process_data can be called.
8199 * For ret_val if its 0 the TCP is locked, if its non-zero
8200 * its unlocked and probably unsafe to touch the TCB.
8201 */
8202 static int
rack_process_ack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,uint32_t tiwin,int32_t tlen,int32_t * ofia,int32_t thflags,int32_t * ret_val)8203 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8204 struct tcpcb *tp, struct tcpopt *to,
8205 uint32_t tiwin, int32_t tlen,
8206 int32_t * ofia, int32_t thflags, int32_t * ret_val)
8207 {
8208 int32_t ourfinisacked = 0;
8209 int32_t nsegs, acked_amount;
8210 int32_t acked;
8211 struct mbuf *mfree;
8212 struct tcp_rack *rack;
8213 int32_t under_pacing = 0;
8214 int32_t recovery = 0;
8215
8216 rack = (struct tcp_rack *)tp->t_fb_ptr;
8217 if (SEQ_GT(th->th_ack, tp->snd_max)) {
8218 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
8219 rack->r_wanted_output = 1;
8220 return (1);
8221 }
8222 if (rack->rc_gp_filled &&
8223 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
8224 under_pacing = 1;
8225 }
8226 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
8227 if (rack->rc_in_persist)
8228 tp->t_rxtshift = 0;
8229 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd))
8230 rack_strike_dupack(rack);
8231 rack_log_ack(tp, to, th);
8232 }
8233 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8234 /*
8235 * Old ack, behind (or duplicate to) the last one rcv'd
8236 * Note: Should mark reordering is occuring! We should also
8237 * look for sack blocks arriving e.g. ack 1, 4-4 then ack 1,
8238 * 3-3, 4-4 would be reording. As well as ack 1, 3-3 <no
8239 * retran and> ack 3
8240 */
8241 return (0);
8242 }
8243 /*
8244 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
8245 * something we sent.
8246 */
8247 if (tp->t_flags & TF_NEEDSYN) {
8248 /*
8249 * T/TCP: Connection was half-synchronized, and our SYN has
8250 * been ACK'd (so connection is now fully synchronized). Go
8251 * to non-starred state, increment snd_una for ACK of SYN,
8252 * and check if we can do window scaling.
8253 */
8254 tp->t_flags &= ~TF_NEEDSYN;
8255 tp->snd_una++;
8256 /* Do window scaling? */
8257 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
8258 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
8259 tp->rcv_scale = tp->request_r_scale;
8260 /* Send window already scaled. */
8261 }
8262 }
8263 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8264 INP_WLOCK_ASSERT(tp->t_inpcb);
8265
8266 acked = BYTES_THIS_ACK(tp, th);
8267 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
8268 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
8269 /*
8270 * If we just performed our first retransmit, and the ACK arrives
8271 * within our recovery window, then it was a mistake to do the
8272 * retransmit in the first place. Recover our original cwnd and
8273 * ssthresh, and proceed to transmit where we left off.
8274 */
8275 if (tp->t_flags & TF_PREVVALID) {
8276 tp->t_flags &= ~TF_PREVVALID;
8277 if (tp->t_rxtshift == 1 &&
8278 (int)(ticks - tp->t_badrxtwin) < 0)
8279 rack_cong_signal(tp, th, CC_RTO_ERR);
8280 }
8281 if (acked) {
8282 /* assure we are not backed off */
8283 tp->t_rxtshift = 0;
8284 rack->rc_tlp_in_progress = 0;
8285 rack->r_ctl.rc_tlp_cnt_out = 0;
8286 /*
8287 * If it is the RXT timer we want to
8288 * stop it, so we can restart a TLP.
8289 */
8290 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
8291 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
8292 #ifdef NETFLIX_HTTP_LOGGING
8293 tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
8294 #endif
8295 }
8296 /*
8297 * If we have a timestamp reply, update smoothed round trip time. If
8298 * no timestamp is present but transmit timer is running and timed
8299 * sequence number was acked, update smoothed round trip time. Since
8300 * we now have an rtt measurement, cancel the timer backoff (cf.,
8301 * Phil Karn's retransmit alg.). Recompute the initial retransmit
8302 * timer.
8303 *
8304 * Some boxes send broken timestamp replies during the SYN+ACK
8305 * phase, ignore timestamps of 0 or we could calculate a huge RTT
8306 * and blow up the retransmit timer.
8307 */
8308 /*
8309 * If all outstanding data is acked, stop retransmit timer and
8310 * remember to restart (more output or persist). If there is more
8311 * data to be acked, restart retransmit timer, using current
8312 * (possibly backed-off) value.
8313 */
8314 if (acked == 0) {
8315 if (ofia)
8316 *ofia = ourfinisacked;
8317 return (0);
8318 }
8319 if (rack->r_ctl.rc_early_recovery) {
8320 if (IN_RECOVERY(tp->t_flags)) {
8321 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
8322 (SEQ_LT(th->th_ack, tp->snd_max))) {
8323 tcp_rack_partialack(tp, th);
8324 } else {
8325 rack_post_recovery(tp, th);
8326 recovery = 1;
8327 }
8328 }
8329 }
8330 /*
8331 * Let the congestion control algorithm update congestion control
8332 * related information. This typically means increasing the
8333 * congestion window.
8334 */
8335 rack_ack_received(tp, rack, th, nsegs, CC_ACK, recovery);
8336 SOCKBUF_LOCK(&so->so_snd);
8337 acked_amount = min(acked, (int)sbavail(&so->so_snd));
8338 tp->snd_wnd -= acked_amount;
8339 mfree = sbcut_locked(&so->so_snd, acked_amount);
8340 if ((sbused(&so->so_snd) == 0) &&
8341 (acked > acked_amount) &&
8342 (tp->t_state >= TCPS_FIN_WAIT_1) &&
8343 (tp->t_flags & TF_SENTFIN)) {
8344 /*
8345 * We must be sure our fin
8346 * was sent and acked (we can be
8347 * in FIN_WAIT_1 without having
8348 * sent the fin).
8349 */
8350 ourfinisacked = 1;
8351 }
8352 SOCKBUF_UNLOCK(&so->so_snd);
8353 tp->t_flags |= TF_WAKESOW;
8354 m_freem(mfree);
8355 if (rack->r_ctl.rc_early_recovery == 0) {
8356 if (IN_RECOVERY(tp->t_flags)) {
8357 if (SEQ_LT(th->th_ack, tp->snd_recover) &&
8358 (SEQ_LT(th->th_ack, tp->snd_max))) {
8359 tcp_rack_partialack(tp, th);
8360 } else {
8361 rack_post_recovery(tp, th);
8362 }
8363 }
8364 }
8365 tp->snd_una = th->th_ack;
8366 if (SEQ_GT(tp->snd_una, tp->snd_recover))
8367 tp->snd_recover = tp->snd_una;
8368
8369 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
8370 tp->snd_nxt = tp->snd_una;
8371 }
8372 if (under_pacing &&
8373 (rack->use_fixed_rate == 0) &&
8374 (rack->in_probe_rtt == 0) &&
8375 rack->rc_gp_dyn_mul &&
8376 rack->rc_always_pace) {
8377 /* Check if we are dragging bottom */
8378 rack_check_bottom_drag(tp, rack, so, acked);
8379 }
8380 if (tp->snd_una == tp->snd_max) {
8381 /* Nothing left outstanding */
8382 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
8383 if (rack->r_ctl.rc_went_idle_time == 0)
8384 rack->r_ctl.rc_went_idle_time = 1;
8385 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
8386 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
8387 tp->t_acktime = 0;
8388 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
8389 /* Set need output so persist might get set */
8390 rack->r_wanted_output = 1;
8391 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
8392 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
8393 (sbavail(&so->so_snd) == 0) &&
8394 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
8395 /*
8396 * The socket was gone and the
8397 * peer sent data, time to
8398 * reset him.
8399 */
8400 *ret_val = 1;
8401 /* tcp_close will kill the inp pre-log the Reset */
8402 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
8403 tp = tcp_close(tp);
8404 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
8405 return (1);
8406 }
8407 }
8408 if (ofia)
8409 *ofia = ourfinisacked;
8410 return (0);
8411 }
8412
8413 static void
rack_collapsed_window(struct tcp_rack * rack)8414 rack_collapsed_window(struct tcp_rack *rack)
8415 {
8416 /*
8417 * Now we must walk the
8418 * send map and divide the
8419 * ones left stranded. These
8420 * guys can't cause us to abort
8421 * the connection and are really
8422 * "unsent". However if a buggy
8423 * client actually did keep some
8424 * of the data i.e. collapsed the win
8425 * and refused to ack and then opened
8426 * the win and acked that data. We would
8427 * get into an ack war, the simplier
8428 * method then of just pretending we
8429 * did not send those segments something
8430 * won't work.
8431 */
8432 struct rack_sendmap *rsm, *nrsm, fe, *insret;
8433 tcp_seq max_seq;
8434
8435 max_seq = rack->rc_tp->snd_una + rack->rc_tp->snd_wnd;
8436 memset(&fe, 0, sizeof(fe));
8437 fe.r_start = max_seq;
8438 /* Find the first seq past or at maxseq */
8439 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
8440 if (rsm == NULL) {
8441 /* Nothing to do strange */
8442 rack->rc_has_collapsed = 0;
8443 return;
8444 }
8445 /*
8446 * Now do we need to split at
8447 * the collapse point?
8448 */
8449 if (SEQ_GT(max_seq, rsm->r_start)) {
8450 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT);
8451 if (nrsm == NULL) {
8452 /* We can't get a rsm, mark all? */
8453 nrsm = rsm;
8454 goto no_split;
8455 }
8456 /* Clone it */
8457 rack_clone_rsm(rack, nrsm, rsm, max_seq);
8458 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm);
8459 #ifdef INVARIANTS
8460 if (insret != NULL) {
8461 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p",
8462 nrsm, insret, rack, rsm);
8463 }
8464 #endif
8465 if (rsm->r_in_tmap) {
8466 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
8467 nrsm->r_in_tmap = 1;
8468 }
8469 /*
8470 * Set in the new RSM as the
8471 * collapsed starting point
8472 */
8473 rsm = nrsm;
8474 }
8475 no_split:
8476 counter_u64_add(rack_collapsed_win, 1);
8477 RB_FOREACH_FROM(nrsm, rack_rb_tree_head, rsm) {
8478 nrsm->r_flags |= RACK_RWND_COLLAPSED;
8479 rack->rc_has_collapsed = 1;
8480 }
8481 }
8482
8483 static void
rack_un_collapse_window(struct tcp_rack * rack)8484 rack_un_collapse_window(struct tcp_rack *rack)
8485 {
8486 struct rack_sendmap *rsm;
8487
8488 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) {
8489 if (rsm->r_flags & RACK_RWND_COLLAPSED)
8490 rsm->r_flags &= ~RACK_RWND_COLLAPSED;
8491 else
8492 break;
8493 }
8494 rack->rc_has_collapsed = 0;
8495 }
8496
8497 static void
rack_handle_delayed_ack(struct tcpcb * tp,struct tcp_rack * rack,int32_t tlen,int32_t tfo_syn)8498 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack,
8499 int32_t tlen, int32_t tfo_syn)
8500 {
8501 if (DELAY_ACK(tp, tlen) || tfo_syn) {
8502 if (rack->rc_dack_mode &&
8503 (tlen > 500) &&
8504 (rack->rc_dack_toggle == 1)) {
8505 goto no_delayed_ack;
8506 }
8507 rack_timer_cancel(tp, rack,
8508 rack->r_ctl.rc_rcvtime, __LINE__);
8509 tp->t_flags |= TF_DELACK;
8510 } else {
8511 no_delayed_ack:
8512 rack->r_wanted_output = 1;
8513 tp->t_flags |= TF_ACKNOW;
8514 if (rack->rc_dack_mode) {
8515 if (tp->t_flags & TF_DELACK)
8516 rack->rc_dack_toggle = 1;
8517 else
8518 rack->rc_dack_toggle = 0;
8519 }
8520 }
8521 }
8522 /*
8523 * Return value of 1, the TCB is unlocked and most
8524 * likely gone, return value of 0, the TCP is still
8525 * locked.
8526 */
8527 static int
rack_process_data(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt)8528 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
8529 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
8530 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
8531 {
8532 /*
8533 * Update window information. Don't look at window if no ACK: TAC's
8534 * send garbage on first SYN.
8535 */
8536 int32_t nsegs;
8537 int32_t tfo_syn;
8538 struct tcp_rack *rack;
8539
8540 rack = (struct tcp_rack *)tp->t_fb_ptr;
8541 INP_WLOCK_ASSERT(tp->t_inpcb);
8542 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8543 if ((thflags & TH_ACK) &&
8544 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
8545 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
8546 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
8547 /* keep track of pure window updates */
8548 if (tlen == 0 &&
8549 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
8550 KMOD_TCPSTAT_INC(tcps_rcvwinupd);
8551 tp->snd_wnd = tiwin;
8552 tp->snd_wl1 = th->th_seq;
8553 tp->snd_wl2 = th->th_ack;
8554 if (tp->snd_wnd > tp->max_sndwnd)
8555 tp->max_sndwnd = tp->snd_wnd;
8556 rack->r_wanted_output = 1;
8557 } else if (thflags & TH_ACK) {
8558 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
8559 tp->snd_wnd = tiwin;
8560 tp->snd_wl1 = th->th_seq;
8561 tp->snd_wl2 = th->th_ack;
8562 }
8563 }
8564 if (tp->snd_wnd < ctf_outstanding(tp))
8565 /* The peer collapsed the window */
8566 rack_collapsed_window(rack);
8567 else if (rack->rc_has_collapsed)
8568 rack_un_collapse_window(rack);
8569 /* Was persist timer active and now we have window space? */
8570 if ((rack->rc_in_persist != 0) &&
8571 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
8572 rack->r_ctl.rc_pace_min_segs))) {
8573 rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime);
8574 tp->snd_nxt = tp->snd_max;
8575 /* Make sure we output to start the timer */
8576 rack->r_wanted_output = 1;
8577 }
8578 /* Do we enter persists? */
8579 if ((rack->rc_in_persist == 0) &&
8580 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
8581 TCPS_HAVEESTABLISHED(tp->t_state) &&
8582 (tp->snd_max == tp->snd_una) &&
8583 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
8584 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
8585 /*
8586 * Here the rwnd is less than
8587 * the pacing size, we are established,
8588 * nothing is outstanding, and there is
8589 * data to send. Enter persists.
8590 */
8591 tp->snd_nxt = tp->snd_una;
8592 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
8593 }
8594 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
8595 m_freem(m);
8596 return (0);
8597 }
8598 /*
8599 * don't process the URG bit, ignore them drag
8600 * along the up.
8601 */
8602 tp->rcv_up = tp->rcv_nxt;
8603 INP_WLOCK_ASSERT(tp->t_inpcb);
8604
8605 /*
8606 * Process the segment text, merging it into the TCP sequencing
8607 * queue, and arranging for acknowledgment of receipt if necessary.
8608 * This process logically involves adjusting tp->rcv_wnd as data is
8609 * presented to the user (this happens in tcp_usrreq.c, case
8610 * PRU_RCVD). If a FIN has already been received on this connection
8611 * then we just ignore the text.
8612 */
8613 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
8614 IS_FASTOPEN(tp->t_flags));
8615 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
8616 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8617 tcp_seq save_start = th->th_seq;
8618 tcp_seq save_rnxt = tp->rcv_nxt;
8619 int save_tlen = tlen;
8620
8621 m_adj(m, drop_hdrlen); /* delayed header drop */
8622 /*
8623 * Insert segment which includes th into TCP reassembly
8624 * queue with control block tp. Set thflags to whether
8625 * reassembly now includes a segment with FIN. This handles
8626 * the common case inline (segment is the next to be
8627 * received on an established connection, and the queue is
8628 * empty), avoiding linkage into and removal from the queue
8629 * and repetition of various conversions. Set DELACK for
8630 * segments received in order, but ack immediately when
8631 * segments are out of order (so fast retransmit can work).
8632 */
8633 if (th->th_seq == tp->rcv_nxt &&
8634 SEGQ_EMPTY(tp) &&
8635 (TCPS_HAVEESTABLISHED(tp->t_state) ||
8636 tfo_syn)) {
8637 #ifdef NETFLIX_SB_LIMITS
8638 u_int mcnt, appended;
8639
8640 if (so->so_rcv.sb_shlim) {
8641 mcnt = m_memcnt(m);
8642 appended = 0;
8643 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8644 CFO_NOSLEEP, NULL) == false) {
8645 counter_u64_add(tcp_sb_shlim_fails, 1);
8646 m_freem(m);
8647 return (0);
8648 }
8649 }
8650 #endif
8651 rack_handle_delayed_ack(tp, rack, tlen, tfo_syn);
8652 tp->rcv_nxt += tlen;
8653 if (tlen &&
8654 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8655 (tp->t_fbyte_in == 0)) {
8656 tp->t_fbyte_in = ticks;
8657 if (tp->t_fbyte_in == 0)
8658 tp->t_fbyte_in = 1;
8659 if (tp->t_fbyte_out && tp->t_fbyte_in)
8660 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8661 }
8662 thflags = th->th_flags & TH_FIN;
8663 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
8664 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8665 SOCKBUF_LOCK(&so->so_rcv);
8666 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8667 m_freem(m);
8668 } else
8669 #ifdef NETFLIX_SB_LIMITS
8670 appended =
8671 #endif
8672 sbappendstream_locked(&so->so_rcv, m, 0);
8673 SOCKBUF_UNLOCK(&so->so_rcv);
8674 tp->t_flags |= TF_WAKESOR;
8675 #ifdef NETFLIX_SB_LIMITS
8676 if (so->so_rcv.sb_shlim && appended != mcnt)
8677 counter_fo_release(so->so_rcv.sb_shlim,
8678 mcnt - appended);
8679 #endif
8680 } else {
8681 /*
8682 * XXX: Due to the header drop above "th" is
8683 * theoretically invalid by now. Fortunately
8684 * m_adj() doesn't actually frees any mbufs when
8685 * trimming from the head.
8686 */
8687 tcp_seq temp = save_start;
8688 thflags = tcp_reass(tp, th, &temp, &tlen, m);
8689 tp->t_flags |= TF_ACKNOW;
8690 }
8691 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
8692 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
8693 /*
8694 * DSACK actually handled in the fastpath
8695 * above.
8696 */
8697 RACK_OPTS_INC(tcp_sack_path_1);
8698 tcp_update_sack_list(tp, save_start,
8699 save_start + save_tlen);
8700 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
8701 if ((tp->rcv_numsacks >= 1) &&
8702 (tp->sackblks[0].end == save_start)) {
8703 /*
8704 * Partial overlap, recorded at todrop
8705 * above.
8706 */
8707 RACK_OPTS_INC(tcp_sack_path_2a);
8708 tcp_update_sack_list(tp,
8709 tp->sackblks[0].start,
8710 tp->sackblks[0].end);
8711 } else {
8712 RACK_OPTS_INC(tcp_sack_path_2b);
8713 tcp_update_dsack_list(tp, save_start,
8714 save_start + save_tlen);
8715 }
8716 } else if (tlen >= save_tlen) {
8717 /* Update of sackblks. */
8718 RACK_OPTS_INC(tcp_sack_path_3);
8719 tcp_update_dsack_list(tp, save_start,
8720 save_start + save_tlen);
8721 } else if (tlen > 0) {
8722 RACK_OPTS_INC(tcp_sack_path_4);
8723 tcp_update_dsack_list(tp, save_start,
8724 save_start + tlen);
8725 }
8726 }
8727 } else {
8728 m_freem(m);
8729 thflags &= ~TH_FIN;
8730 }
8731
8732 /*
8733 * If FIN is received ACK the FIN and let the user know that the
8734 * connection is closing.
8735 */
8736 if (thflags & TH_FIN) {
8737 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8738 socantrcvmore(so);
8739 /* The socket upcall is handled by socantrcvmore. */
8740 tp->t_flags &= ~TF_WAKESOR;
8741 /*
8742 * If connection is half-synchronized (ie NEEDSYN
8743 * flag on) then delay ACK, so it may be piggybacked
8744 * when SYN is sent. Otherwise, since we received a
8745 * FIN then no more input can be expected, send ACK
8746 * now.
8747 */
8748 if (tp->t_flags & TF_NEEDSYN) {
8749 rack_timer_cancel(tp, rack,
8750 rack->r_ctl.rc_rcvtime, __LINE__);
8751 tp->t_flags |= TF_DELACK;
8752 } else {
8753 tp->t_flags |= TF_ACKNOW;
8754 }
8755 tp->rcv_nxt++;
8756 }
8757 switch (tp->t_state) {
8758 /*
8759 * In SYN_RECEIVED and ESTABLISHED STATES enter the
8760 * CLOSE_WAIT state.
8761 */
8762 case TCPS_SYN_RECEIVED:
8763 tp->t_starttime = ticks;
8764 /* FALLTHROUGH */
8765 case TCPS_ESTABLISHED:
8766 rack_timer_cancel(tp, rack,
8767 rack->r_ctl.rc_rcvtime, __LINE__);
8768 tcp_state_change(tp, TCPS_CLOSE_WAIT);
8769 break;
8770
8771 /*
8772 * If still in FIN_WAIT_1 STATE FIN has not been
8773 * acked so enter the CLOSING state.
8774 */
8775 case TCPS_FIN_WAIT_1:
8776 rack_timer_cancel(tp, rack,
8777 rack->r_ctl.rc_rcvtime, __LINE__);
8778 tcp_state_change(tp, TCPS_CLOSING);
8779 break;
8780
8781 /*
8782 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8783 * starting the time-wait timer, turning off the
8784 * other standard timers.
8785 */
8786 case TCPS_FIN_WAIT_2:
8787 rack_timer_cancel(tp, rack,
8788 rack->r_ctl.rc_rcvtime, __LINE__);
8789 tcp_twstart(tp);
8790 return (1);
8791 }
8792 }
8793 /*
8794 * Return any desired output.
8795 */
8796 if ((tp->t_flags & TF_ACKNOW) ||
8797 (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
8798 rack->r_wanted_output = 1;
8799 }
8800 INP_WLOCK_ASSERT(tp->t_inpcb);
8801 return (0);
8802 }
8803
8804 /*
8805 * Here nothing is really faster, its just that we
8806 * have broken out the fast-data path also just like
8807 * the fast-ack.
8808 */
8809 static int
rack_do_fastnewdata(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt,uint8_t iptos)8810 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
8811 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8812 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos)
8813 {
8814 int32_t nsegs;
8815 int32_t newsize = 0; /* automatic sockbuf scaling */
8816 struct tcp_rack *rack;
8817 #ifdef NETFLIX_SB_LIMITS
8818 u_int mcnt, appended;
8819 #endif
8820 #ifdef TCPDEBUG
8821 /*
8822 * The size of tcp_saveipgen must be the size of the max ip header,
8823 * now IPv6.
8824 */
8825 u_char tcp_saveipgen[IP6_HDR_LEN];
8826 struct tcphdr tcp_savetcp;
8827 short ostate = 0;
8828
8829 #endif
8830 /*
8831 * If last ACK falls within this segment's sequence numbers, record
8832 * the timestamp. NOTE that the test is modified according to the
8833 * latest proposal of the [email protected] list (Braden 1993/04/26).
8834 */
8835 if (__predict_false(th->th_seq != tp->rcv_nxt)) {
8836 return (0);
8837 }
8838 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
8839 return (0);
8840 }
8841 if (tiwin && tiwin != tp->snd_wnd) {
8842 return (0);
8843 }
8844 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
8845 return (0);
8846 }
8847 if (__predict_false((to->to_flags & TOF_TS) &&
8848 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
8849 return (0);
8850 }
8851 if (__predict_false((th->th_ack != tp->snd_una))) {
8852 return (0);
8853 }
8854 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
8855 return (0);
8856 }
8857 if ((to->to_flags & TOF_TS) != 0 &&
8858 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8859 tp->ts_recent_age = tcp_ts_getticks();
8860 tp->ts_recent = to->to_tsval;
8861 }
8862 rack = (struct tcp_rack *)tp->t_fb_ptr;
8863 /*
8864 * This is a pure, in-sequence data packet with nothing on the
8865 * reassembly queue and we have enough buffer space to take it.
8866 */
8867 nsegs = max(1, m->m_pkthdr.lro_nsegs);
8868
8869 #ifdef NETFLIX_SB_LIMITS
8870 if (so->so_rcv.sb_shlim) {
8871 mcnt = m_memcnt(m);
8872 appended = 0;
8873 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt,
8874 CFO_NOSLEEP, NULL) == false) {
8875 counter_u64_add(tcp_sb_shlim_fails, 1);
8876 m_freem(m);
8877 return (1);
8878 }
8879 }
8880 #endif
8881 /* Clean receiver SACK report if present */
8882 if (tp->rcv_numsacks)
8883 tcp_clean_sackreport(tp);
8884 KMOD_TCPSTAT_INC(tcps_preddat);
8885 tp->rcv_nxt += tlen;
8886 if (tlen &&
8887 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
8888 (tp->t_fbyte_in == 0)) {
8889 tp->t_fbyte_in = ticks;
8890 if (tp->t_fbyte_in == 0)
8891 tp->t_fbyte_in = 1;
8892 if (tp->t_fbyte_out && tp->t_fbyte_in)
8893 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
8894 }
8895 /*
8896 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
8897 */
8898 tp->snd_wl1 = th->th_seq;
8899 /*
8900 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
8901 */
8902 tp->rcv_up = tp->rcv_nxt;
8903 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs);
8904 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen);
8905 #ifdef TCPDEBUG
8906 if (so->so_options & SO_DEBUG)
8907 tcp_trace(TA_INPUT, ostate, tp,
8908 (void *)tcp_saveipgen, &tcp_savetcp, 0);
8909 #endif
8910 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
8911
8912 /* Add data to socket buffer. */
8913 SOCKBUF_LOCK(&so->so_rcv);
8914 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
8915 m_freem(m);
8916 } else {
8917 /*
8918 * Set new socket buffer size. Give up when limit is
8919 * reached.
8920 */
8921 if (newsize)
8922 if (!sbreserve_locked(&so->so_rcv,
8923 newsize, so, NULL))
8924 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
8925 m_adj(m, drop_hdrlen); /* delayed header drop */
8926 #ifdef NETFLIX_SB_LIMITS
8927 appended =
8928 #endif
8929 sbappendstream_locked(&so->so_rcv, m, 0);
8930 ctf_calc_rwin(so, tp);
8931 }
8932 SOCKBUF_UNLOCK(&so->so_rcv);
8933 tp->t_flags |= TF_WAKESOR;
8934 #ifdef NETFLIX_SB_LIMITS
8935 if (so->so_rcv.sb_shlim && mcnt != appended)
8936 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended);
8937 #endif
8938 rack_handle_delayed_ack(tp, rack, tlen, 0);
8939 if (tp->snd_una == tp->snd_max)
8940 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
8941 return (1);
8942 }
8943
8944 /*
8945 * This subfunction is used to try to highly optimize the
8946 * fast path. We again allow window updates that are
8947 * in sequence to remain in the fast-path. We also add
8948 * in the __predict's to attempt to help the compiler.
8949 * Note that if we return a 0, then we can *not* process
8950 * it and the caller should push the packet into the
8951 * slow-path.
8952 */
8953 static int
rack_fastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t nxt_pkt,uint32_t cts)8954 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
8955 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
8956 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
8957 {
8958 int32_t acked;
8959 int32_t nsegs;
8960 #ifdef TCPDEBUG
8961 /*
8962 * The size of tcp_saveipgen must be the size of the max ip header,
8963 * now IPv6.
8964 */
8965 u_char tcp_saveipgen[IP6_HDR_LEN];
8966 struct tcphdr tcp_savetcp;
8967 short ostate = 0;
8968 #endif
8969 int32_t under_pacing = 0;
8970 struct tcp_rack *rack;
8971
8972 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
8973 /* Old ack, behind (or duplicate to) the last one rcv'd */
8974 return (0);
8975 }
8976 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
8977 /* Above what we have sent? */
8978 return (0);
8979 }
8980 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
8981 /* We are retransmitting */
8982 return (0);
8983 }
8984 if (__predict_false(tiwin == 0)) {
8985 /* zero window */
8986 return (0);
8987 }
8988 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
8989 /* We need a SYN or a FIN, unlikely.. */
8990 return (0);
8991 }
8992 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
8993 /* Timestamp is behind .. old ack with seq wrap? */
8994 return (0);
8995 }
8996 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
8997 /* Still recovering */
8998 return (0);
8999 }
9000 rack = (struct tcp_rack *)tp->t_fb_ptr;
9001 if (rack->r_ctl.rc_sacked) {
9002 /* We have sack holes on our scoreboard */
9003 return (0);
9004 }
9005 /* Ok if we reach here, we can process a fast-ack */
9006 if (rack->rc_gp_filled &&
9007 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
9008 under_pacing = 1;
9009 }
9010 nsegs = max(1, m->m_pkthdr.lro_nsegs);
9011 rack_log_ack(tp, to, th);
9012 /* Did the window get updated? */
9013 if (tiwin != tp->snd_wnd) {
9014 tp->snd_wnd = tiwin;
9015 tp->snd_wl1 = th->th_seq;
9016 if (tp->snd_wnd > tp->max_sndwnd)
9017 tp->max_sndwnd = tp->snd_wnd;
9018 }
9019 /* Do we exit persists? */
9020 if ((rack->rc_in_persist != 0) &&
9021 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2),
9022 rack->r_ctl.rc_pace_min_segs))) {
9023 rack_exit_persist(tp, rack, cts);
9024 }
9025 /* Do we enter persists? */
9026 if ((rack->rc_in_persist == 0) &&
9027 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) &&
9028 TCPS_HAVEESTABLISHED(tp->t_state) &&
9029 (tp->snd_max == tp->snd_una) &&
9030 sbavail(&tp->t_inpcb->inp_socket->so_snd) &&
9031 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) {
9032 /*
9033 * Here the rwnd is less than
9034 * the pacing size, we are established,
9035 * nothing is outstanding, and there is
9036 * data to send. Enter persists.
9037 */
9038 tp->snd_nxt = tp->snd_una;
9039 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
9040 }
9041 /*
9042 * If last ACK falls within this segment's sequence numbers, record
9043 * the timestamp. NOTE that the test is modified according to the
9044 * latest proposal of the [email protected] list (Braden 1993/04/26).
9045 */
9046 if ((to->to_flags & TOF_TS) != 0 &&
9047 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
9048 tp->ts_recent_age = tcp_ts_getticks();
9049 tp->ts_recent = to->to_tsval;
9050 }
9051 /*
9052 * This is a pure ack for outstanding data.
9053 */
9054 KMOD_TCPSTAT_INC(tcps_predack);
9055
9056 /*
9057 * "bad retransmit" recovery.
9058 */
9059 if (tp->t_flags & TF_PREVVALID) {
9060 tp->t_flags &= ~TF_PREVVALID;
9061 if (tp->t_rxtshift == 1 &&
9062 (int)(ticks - tp->t_badrxtwin) < 0)
9063 rack_cong_signal(tp, th, CC_RTO_ERR);
9064 }
9065 /*
9066 * Recalculate the transmit timer / rtt.
9067 *
9068 * Some boxes send broken timestamp replies during the SYN+ACK
9069 * phase, ignore timestamps of 0 or we could calculate a huge RTT
9070 * and blow up the retransmit timer.
9071 */
9072 acked = BYTES_THIS_ACK(tp, th);
9073
9074 #ifdef TCP_HHOOK
9075 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
9076 hhook_run_tcp_est_in(tp, th, to);
9077 #endif
9078
9079 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs);
9080 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked);
9081 sbdrop(&so->so_snd, acked);
9082 if (acked) {
9083 /* assure we are not backed off */
9084 tp->t_rxtshift = 0;
9085 rack->rc_tlp_in_progress = 0;
9086 rack->r_ctl.rc_tlp_cnt_out = 0;
9087 /*
9088 * If it is the RXT timer we want to
9089 * stop it, so we can restart a TLP.
9090 */
9091 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT)
9092 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9093 #ifdef NETFLIX_HTTP_LOGGING
9094 tcp_http_check_for_comp(rack->rc_tp, th->th_ack);
9095 #endif
9096 }
9097 /*
9098 * Let the congestion control algorithm update congestion control
9099 * related information. This typically means increasing the
9100 * congestion window.
9101 */
9102 rack_ack_received(tp, rack, th, nsegs, CC_ACK, 0);
9103
9104 tp->snd_una = th->th_ack;
9105 if (tp->snd_wnd < ctf_outstanding(tp)) {
9106 /* The peer collapsed the window */
9107 rack_collapsed_window(rack);
9108 } else if (rack->rc_has_collapsed)
9109 rack_un_collapse_window(rack);
9110
9111 /*
9112 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
9113 */
9114 tp->snd_wl2 = th->th_ack;
9115 tp->t_dupacks = 0;
9116 m_freem(m);
9117 /* ND6_HINT(tp); *//* Some progress has been made. */
9118
9119 /*
9120 * If all outstanding data are acked, stop retransmit timer,
9121 * otherwise restart timer using current (possibly backed-off)
9122 * value. If process is waiting for space, wakeup/selwakeup/signal.
9123 * If data are ready to send, let tcp_output decide between more
9124 * output or persist.
9125 */
9126 #ifdef TCPDEBUG
9127 if (so->so_options & SO_DEBUG)
9128 tcp_trace(TA_INPUT, ostate, tp,
9129 (void *)tcp_saveipgen,
9130 &tcp_savetcp, 0);
9131 #endif
9132 if (under_pacing &&
9133 (rack->use_fixed_rate == 0) &&
9134 (rack->in_probe_rtt == 0) &&
9135 rack->rc_gp_dyn_mul &&
9136 rack->rc_always_pace) {
9137 /* Check if we are dragging bottom */
9138 rack_check_bottom_drag(tp, rack, so, acked);
9139 }
9140 if (tp->snd_una == tp->snd_max) {
9141 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL);
9142 if (rack->r_ctl.rc_went_idle_time == 0)
9143 rack->r_ctl.rc_went_idle_time = 1;
9144 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
9145 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0)
9146 tp->t_acktime = 0;
9147 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
9148 }
9149 /* Wake up the socket if we have room to write more */
9150 tp->t_flags |= TF_WAKESOW;
9151 if (sbavail(&so->so_snd)) {
9152 rack->r_wanted_output = 1;
9153 }
9154 return (1);
9155 }
9156
9157 /*
9158 * Return value of 1, the TCB is unlocked and most
9159 * likely gone, return value of 0, the TCP is still
9160 * locked.
9161 */
9162 static int
rack_do_syn_sent(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9163 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
9164 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9165 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9166 {
9167 int32_t ret_val = 0;
9168 int32_t todrop;
9169 int32_t ourfinisacked = 0;
9170 struct tcp_rack *rack;
9171
9172 ctf_calc_rwin(so, tp);
9173 /*
9174 * If the state is SYN_SENT: if seg contains an ACK, but not for our
9175 * SYN, drop the input. if seg contains a RST, then drop the
9176 * connection. if seg does not contain SYN, then drop it. Otherwise
9177 * this is an acceptable SYN segment initialize tp->rcv_nxt and
9178 * tp->irs if seg contains ack then advance tp->snd_una if seg
9179 * contains an ECE and ECN support is enabled, the stream is ECN
9180 * capable. if SYN has been acked change to ESTABLISHED else
9181 * SYN_RCVD state arrange for segment to be acked (eventually)
9182 * continue processing rest of data/controls.
9183 */
9184 if ((thflags & TH_ACK) &&
9185 (SEQ_LEQ(th->th_ack, tp->iss) ||
9186 SEQ_GT(th->th_ack, tp->snd_max))) {
9187 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9188 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9189 return (1);
9190 }
9191 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
9192 TCP_PROBE5(connect__refused, NULL, tp,
9193 mtod(m, const char *), tp, th);
9194 tp = tcp_drop(tp, ECONNREFUSED);
9195 ctf_do_drop(m, tp);
9196 return (1);
9197 }
9198 if (thflags & TH_RST) {
9199 ctf_do_drop(m, tp);
9200 return (1);
9201 }
9202 if (!(thflags & TH_SYN)) {
9203 ctf_do_drop(m, tp);
9204 return (1);
9205 }
9206 tp->irs = th->th_seq;
9207 tcp_rcvseqinit(tp);
9208 rack = (struct tcp_rack *)tp->t_fb_ptr;
9209 if (thflags & TH_ACK) {
9210 int tfo_partial = 0;
9211
9212 KMOD_TCPSTAT_INC(tcps_connects);
9213 soisconnected(so);
9214 #ifdef MAC
9215 mac_socketpeer_set_from_mbuf(m, so);
9216 #endif
9217 /* Do window scaling on this connection? */
9218 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9219 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9220 tp->rcv_scale = tp->request_r_scale;
9221 }
9222 tp->rcv_adv += min(tp->rcv_wnd,
9223 TCP_MAXWIN << tp->rcv_scale);
9224 /*
9225 * If not all the data that was sent in the TFO SYN
9226 * has been acked, resend the remainder right away.
9227 */
9228 if (IS_FASTOPEN(tp->t_flags) &&
9229 (tp->snd_una != tp->snd_max)) {
9230 tp->snd_nxt = th->th_ack;
9231 tfo_partial = 1;
9232 }
9233 /*
9234 * If there's data, delay ACK; if there's also a FIN ACKNOW
9235 * will be turned on later.
9236 */
9237 if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) {
9238 rack_timer_cancel(tp, rack,
9239 rack->r_ctl.rc_rcvtime, __LINE__);
9240 tp->t_flags |= TF_DELACK;
9241 } else {
9242 rack->r_wanted_output = 1;
9243 tp->t_flags |= TF_ACKNOW;
9244 rack->rc_dack_toggle = 0;
9245 }
9246 if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
9247 (V_tcp_do_ecn == 1)) {
9248 tp->t_flags2 |= TF2_ECN_PERMIT;
9249 KMOD_TCPSTAT_INC(tcps_ecn_shs);
9250 }
9251 if (SEQ_GT(th->th_ack, tp->snd_una)) {
9252 /*
9253 * We advance snd_una for the
9254 * fast open case. If th_ack is
9255 * acknowledging data beyond
9256 * snd_una we can't just call
9257 * ack-processing since the
9258 * data stream in our send-map
9259 * will start at snd_una + 1 (one
9260 * beyond the SYN). If its just
9261 * equal we don't need to do that
9262 * and there is no send_map.
9263 */
9264 tp->snd_una++;
9265 }
9266 /*
9267 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
9268 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
9269 */
9270 tp->t_starttime = ticks;
9271 if (tp->t_flags & TF_NEEDFIN) {
9272 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9273 tp->t_flags &= ~TF_NEEDFIN;
9274 thflags &= ~TH_SYN;
9275 } else {
9276 tcp_state_change(tp, TCPS_ESTABLISHED);
9277 TCP_PROBE5(connect__established, NULL, tp,
9278 mtod(m, const char *), tp, th);
9279 rack_cc_conn_init(tp);
9280 }
9281 } else {
9282 /*
9283 * Received initial SYN in SYN-SENT[*] state => simultaneous
9284 * open. If segment contains CC option and there is a
9285 * cached CC, apply TAO test. If it succeeds, connection is *
9286 * half-synchronized. Otherwise, do 3-way handshake:
9287 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
9288 * there was no CC option, clear cached CC value.
9289 */
9290 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
9291 tcp_state_change(tp, TCPS_SYN_RECEIVED);
9292 }
9293 INP_WLOCK_ASSERT(tp->t_inpcb);
9294 /*
9295 * Advance th->th_seq to correspond to first data byte. If data,
9296 * trim to stay within window, dropping FIN if necessary.
9297 */
9298 th->th_seq++;
9299 if (tlen > tp->rcv_wnd) {
9300 todrop = tlen - tp->rcv_wnd;
9301 m_adj(m, -todrop);
9302 tlen = tp->rcv_wnd;
9303 thflags &= ~TH_FIN;
9304 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
9305 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
9306 }
9307 tp->snd_wl1 = th->th_seq - 1;
9308 tp->rcv_up = th->th_seq;
9309 /*
9310 * Client side of transaction: already sent SYN and data. If the
9311 * remote host used T/TCP to validate the SYN, our data will be
9312 * ACK'd; if so, enter normal data segment processing in the middle
9313 * of step 5, ack processing. Otherwise, goto step 6.
9314 */
9315 if (thflags & TH_ACK) {
9316 /* For syn-sent we need to possibly update the rtt */
9317 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
9318 uint32_t t;
9319
9320 t = tcp_ts_getticks() - to->to_tsecr;
9321 if (!tp->t_rttlow || tp->t_rttlow > t)
9322 tp->t_rttlow = t;
9323 tcp_rack_xmit_timer(rack, t + 1, 1, (t * HPTS_USEC_IN_MSEC), 0, NULL, 2);
9324 tcp_rack_xmit_timer_commit(rack, tp);
9325 }
9326 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
9327 return (ret_val);
9328 /* We may have changed to FIN_WAIT_1 above */
9329 if (tp->t_state == TCPS_FIN_WAIT_1) {
9330 /*
9331 * In FIN_WAIT_1 STATE in addition to the processing
9332 * for the ESTABLISHED state if our FIN is now
9333 * acknowledged then enter FIN_WAIT_2.
9334 */
9335 if (ourfinisacked) {
9336 /*
9337 * If we can't receive any more data, then
9338 * closing user can proceed. Starting the
9339 * timer is contrary to the specification,
9340 * but if we don't get a FIN we'll hang
9341 * forever.
9342 *
9343 * XXXjl: we should release the tp also, and
9344 * use a compressed state.
9345 */
9346 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9347 soisdisconnected(so);
9348 tcp_timer_activate(tp, TT_2MSL,
9349 (tcp_fast_finwait2_recycle ?
9350 tcp_finwait2_timeout :
9351 TP_MAXIDLE(tp)));
9352 }
9353 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9354 }
9355 }
9356 }
9357 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9358 tiwin, thflags, nxt_pkt));
9359 }
9360
9361 /*
9362 * Return value of 1, the TCB is unlocked and most
9363 * likely gone, return value of 0, the TCP is still
9364 * locked.
9365 */
9366 static int
rack_do_syn_recv(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9367 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
9368 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9369 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9370 {
9371 struct tcp_rack *rack;
9372 int32_t ret_val = 0;
9373 int32_t ourfinisacked = 0;
9374
9375 ctf_calc_rwin(so, tp);
9376 if ((thflags & TH_ACK) &&
9377 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
9378 SEQ_GT(th->th_ack, tp->snd_max))) {
9379 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9380 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9381 return (1);
9382 }
9383 rack = (struct tcp_rack *)tp->t_fb_ptr;
9384 if (IS_FASTOPEN(tp->t_flags)) {
9385 /*
9386 * When a TFO connection is in SYN_RECEIVED, the
9387 * only valid packets are the initial SYN, a
9388 * retransmit/copy of the initial SYN (possibly with
9389 * a subset of the original data), a valid ACK, a
9390 * FIN, or a RST.
9391 */
9392 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
9393 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9394 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9395 return (1);
9396 } else if (thflags & TH_SYN) {
9397 /* non-initial SYN is ignored */
9398 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
9399 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
9400 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
9401 ctf_do_drop(m, NULL);
9402 return (0);
9403 }
9404 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
9405 ctf_do_drop(m, NULL);
9406 return (0);
9407 }
9408 }
9409 if ((thflags & TH_RST) ||
9410 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9411 return (ctf_process_rst(m, th, so, tp));
9412 /*
9413 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9414 * it's less than ts_recent, drop it.
9415 */
9416 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9417 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9418 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9419 return (ret_val);
9420 }
9421 /*
9422 * In the SYN-RECEIVED state, validate that the packet belongs to
9423 * this connection before trimming the data to fit the receive
9424 * window. Check the sequence number versus IRS since we know the
9425 * sequence numbers haven't wrapped. This is a partial fix for the
9426 * "LAND" DoS attack.
9427 */
9428 if (SEQ_LT(th->th_seq, tp->irs)) {
9429 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
9430 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9431 return (1);
9432 }
9433 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9434 return (ret_val);
9435 }
9436 /*
9437 * If last ACK falls within this segment's sequence numbers, record
9438 * its timestamp. NOTE: 1) That the test incorporates suggestions
9439 * from the latest proposal of the [email protected] list (Braden
9440 * 1993/04/26). 2) That updating only on newer timestamps interferes
9441 * with our earlier PAWS tests, so this check should be solely
9442 * predicated on the sequence space of this segment. 3) That we
9443 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9444 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9445 * SEG.Len, This modified check allows us to overcome RFC1323's
9446 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9447 * p.869. In such cases, we can still calculate the RTT correctly
9448 * when RCV.NXT == Last.ACK.Sent.
9449 */
9450 if ((to->to_flags & TOF_TS) != 0 &&
9451 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9452 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9453 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9454 tp->ts_recent_age = tcp_ts_getticks();
9455 tp->ts_recent = to->to_tsval;
9456 }
9457 tp->snd_wnd = tiwin;
9458 /*
9459 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9460 * is on (half-synchronized state), then queue data for later
9461 * processing; else drop segment and return.
9462 */
9463 if ((thflags & TH_ACK) == 0) {
9464 if (IS_FASTOPEN(tp->t_flags)) {
9465 rack_cc_conn_init(tp);
9466 }
9467 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9468 tiwin, thflags, nxt_pkt));
9469 }
9470 KMOD_TCPSTAT_INC(tcps_connects);
9471 soisconnected(so);
9472 /* Do window scaling? */
9473 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
9474 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
9475 tp->rcv_scale = tp->request_r_scale;
9476 }
9477 /*
9478 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
9479 * FIN-WAIT-1
9480 */
9481 tp->t_starttime = ticks;
9482 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
9483 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
9484 tp->t_tfo_pending = NULL;
9485 }
9486 if (tp->t_flags & TF_NEEDFIN) {
9487 tcp_state_change(tp, TCPS_FIN_WAIT_1);
9488 tp->t_flags &= ~TF_NEEDFIN;
9489 } else {
9490 tcp_state_change(tp, TCPS_ESTABLISHED);
9491 TCP_PROBE5(accept__established, NULL, tp,
9492 mtod(m, const char *), tp, th);
9493 /*
9494 * TFO connections call cc_conn_init() during SYN
9495 * processing. Calling it again here for such connections
9496 * is not harmless as it would undo the snd_cwnd reduction
9497 * that occurs when a TFO SYN|ACK is retransmitted.
9498 */
9499 if (!IS_FASTOPEN(tp->t_flags))
9500 rack_cc_conn_init(tp);
9501 }
9502 /*
9503 * Account for the ACK of our SYN prior to
9504 * regular ACK processing below, except for
9505 * simultaneous SYN, which is handled later.
9506 */
9507 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
9508 tp->snd_una++;
9509 /*
9510 * If segment contains data or ACK, will call tcp_reass() later; if
9511 * not, do so now to pass queued data to user.
9512 */
9513 if (tlen == 0 && (thflags & TH_FIN) == 0)
9514 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
9515 (struct mbuf *)0);
9516 tp->snd_wl1 = th->th_seq - 1;
9517 /* For syn-recv we need to possibly update the rtt */
9518 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
9519 uint32_t t;
9520
9521 t = tcp_ts_getticks() - to->to_tsecr;
9522 if (!tp->t_rttlow || tp->t_rttlow > t)
9523 tp->t_rttlow = t;
9524 tcp_rack_xmit_timer(rack, t + 1, 1, (t * HPTS_USEC_IN_MSEC), 0, NULL, 2);
9525 tcp_rack_xmit_timer_commit(rack, tp);
9526 }
9527 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9528 return (ret_val);
9529 }
9530 if (tp->t_state == TCPS_FIN_WAIT_1) {
9531 /* We could have went to FIN_WAIT_1 (or EST) above */
9532 /*
9533 * In FIN_WAIT_1 STATE in addition to the processing for the
9534 * ESTABLISHED state if our FIN is now acknowledged then
9535 * enter FIN_WAIT_2.
9536 */
9537 if (ourfinisacked) {
9538 /*
9539 * If we can't receive any more data, then closing
9540 * user can proceed. Starting the timer is contrary
9541 * to the specification, but if we don't get a FIN
9542 * we'll hang forever.
9543 *
9544 * XXXjl: we should release the tp also, and use a
9545 * compressed state.
9546 */
9547 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9548 soisdisconnected(so);
9549 tcp_timer_activate(tp, TT_2MSL,
9550 (tcp_fast_finwait2_recycle ?
9551 tcp_finwait2_timeout :
9552 TP_MAXIDLE(tp)));
9553 }
9554 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9555 }
9556 }
9557 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9558 tiwin, thflags, nxt_pkt));
9559 }
9560
9561 /*
9562 * Return value of 1, the TCB is unlocked and most
9563 * likely gone, return value of 0, the TCP is still
9564 * locked.
9565 */
9566 static int
rack_do_established(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9567 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
9568 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9569 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9570 {
9571 int32_t ret_val = 0;
9572 struct tcp_rack *rack;
9573
9574 /*
9575 * Header prediction: check for the two common cases of a
9576 * uni-directional data xfer. If the packet has no control flags,
9577 * is in-sequence, the window didn't change and we're not
9578 * retransmitting, it's a candidate. If the length is zero and the
9579 * ack moved forward, we're the sender side of the xfer. Just free
9580 * the data acked & wake any higher level process that was blocked
9581 * waiting for space. If the length is non-zero and the ack didn't
9582 * move, we're the receiver side. If we're getting packets in-order
9583 * (the reassembly queue is empty), add the data toc The socket
9584 * buffer and note that we need a delayed ack. Make sure that the
9585 * hidden state-flags are also off. Since we check for
9586 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
9587 */
9588 rack = (struct tcp_rack *)tp->t_fb_ptr;
9589 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
9590 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) &&
9591 __predict_true(SEGQ_EMPTY(tp)) &&
9592 __predict_true(th->th_seq == tp->rcv_nxt)) {
9593 if (tlen == 0) {
9594 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
9595 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
9596 return (0);
9597 }
9598 } else {
9599 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
9600 tiwin, nxt_pkt, iptos)) {
9601 return (0);
9602 }
9603 }
9604 }
9605 ctf_calc_rwin(so, tp);
9606
9607 if ((thflags & TH_RST) ||
9608 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9609 return (ctf_process_rst(m, th, so, tp));
9610
9611 /*
9612 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9613 * synchronized state.
9614 */
9615 if (thflags & TH_SYN) {
9616 ctf_challenge_ack(m, th, tp, &ret_val);
9617 return (ret_val);
9618 }
9619 /*
9620 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9621 * it's less than ts_recent, drop it.
9622 */
9623 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9624 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9625 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9626 return (ret_val);
9627 }
9628 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9629 return (ret_val);
9630 }
9631 /*
9632 * If last ACK falls within this segment's sequence numbers, record
9633 * its timestamp. NOTE: 1) That the test incorporates suggestions
9634 * from the latest proposal of the [email protected] list (Braden
9635 * 1993/04/26). 2) That updating only on newer timestamps interferes
9636 * with our earlier PAWS tests, so this check should be solely
9637 * predicated on the sequence space of this segment. 3) That we
9638 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9639 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9640 * SEG.Len, This modified check allows us to overcome RFC1323's
9641 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9642 * p.869. In such cases, we can still calculate the RTT correctly
9643 * when RCV.NXT == Last.ACK.Sent.
9644 */
9645 if ((to->to_flags & TOF_TS) != 0 &&
9646 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9647 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9648 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9649 tp->ts_recent_age = tcp_ts_getticks();
9650 tp->ts_recent = to->to_tsval;
9651 }
9652 /*
9653 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9654 * is on (half-synchronized state), then queue data for later
9655 * processing; else drop segment and return.
9656 */
9657 if ((thflags & TH_ACK) == 0) {
9658 if (tp->t_flags & TF_NEEDSYN) {
9659 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9660 tiwin, thflags, nxt_pkt));
9661
9662 } else if (tp->t_flags & TF_ACKNOW) {
9663 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9664 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output= 1;
9665 return (ret_val);
9666 } else {
9667 ctf_do_drop(m, NULL);
9668 return (0);
9669 }
9670 }
9671 /*
9672 * Ack processing.
9673 */
9674 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9675 return (ret_val);
9676 }
9677 if (sbavail(&so->so_snd)) {
9678 if (ctf_progress_timeout_check(tp, true)) {
9679 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__);
9680 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
9681 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9682 return (1);
9683 }
9684 }
9685 /* State changes only happen in rack_process_data() */
9686 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9687 tiwin, thflags, nxt_pkt));
9688 }
9689
9690 /*
9691 * Return value of 1, the TCB is unlocked and most
9692 * likely gone, return value of 0, the TCP is still
9693 * locked.
9694 */
9695 static int
rack_do_close_wait(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9696 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
9697 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9698 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9699 {
9700 int32_t ret_val = 0;
9701
9702 ctf_calc_rwin(so, tp);
9703 if ((thflags & TH_RST) ||
9704 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9705 return (ctf_process_rst(m, th, so, tp));
9706 /*
9707 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9708 * synchronized state.
9709 */
9710 if (thflags & TH_SYN) {
9711 ctf_challenge_ack(m, th, tp, &ret_val);
9712 return (ret_val);
9713 }
9714 /*
9715 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9716 * it's less than ts_recent, drop it.
9717 */
9718 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9719 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9720 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9721 return (ret_val);
9722 }
9723 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9724 return (ret_val);
9725 }
9726 /*
9727 * If last ACK falls within this segment's sequence numbers, record
9728 * its timestamp. NOTE: 1) That the test incorporates suggestions
9729 * from the latest proposal of the [email protected] list (Braden
9730 * 1993/04/26). 2) That updating only on newer timestamps interferes
9731 * with our earlier PAWS tests, so this check should be solely
9732 * predicated on the sequence space of this segment. 3) That we
9733 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9734 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9735 * SEG.Len, This modified check allows us to overcome RFC1323's
9736 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9737 * p.869. In such cases, we can still calculate the RTT correctly
9738 * when RCV.NXT == Last.ACK.Sent.
9739 */
9740 if ((to->to_flags & TOF_TS) != 0 &&
9741 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9742 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9743 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9744 tp->ts_recent_age = tcp_ts_getticks();
9745 tp->ts_recent = to->to_tsval;
9746 }
9747 /*
9748 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9749 * is on (half-synchronized state), then queue data for later
9750 * processing; else drop segment and return.
9751 */
9752 if ((thflags & TH_ACK) == 0) {
9753 if (tp->t_flags & TF_NEEDSYN) {
9754 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9755 tiwin, thflags, nxt_pkt));
9756
9757 } else if (tp->t_flags & TF_ACKNOW) {
9758 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9759 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
9760 return (ret_val);
9761 } else {
9762 ctf_do_drop(m, NULL);
9763 return (0);
9764 }
9765 }
9766 /*
9767 * Ack processing.
9768 */
9769 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
9770 return (ret_val);
9771 }
9772 if (sbavail(&so->so_snd)) {
9773 if (ctf_progress_timeout_check(tp, true)) {
9774 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
9775 tp, tick, PROGRESS_DROP, __LINE__);
9776 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
9777 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9778 return (1);
9779 }
9780 }
9781 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9782 tiwin, thflags, nxt_pkt));
9783 }
9784
9785 static int
rack_check_data_after_close(struct mbuf * m,struct tcpcb * tp,int32_t * tlen,struct tcphdr * th,struct socket * so)9786 rack_check_data_after_close(struct mbuf *m,
9787 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
9788 {
9789 struct tcp_rack *rack;
9790
9791 rack = (struct tcp_rack *)tp->t_fb_ptr;
9792 if (rack->rc_allow_data_af_clo == 0) {
9793 close_now:
9794 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9795 /* tcp_close will kill the inp pre-log the Reset */
9796 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
9797 tp = tcp_close(tp);
9798 KMOD_TCPSTAT_INC(tcps_rcvafterclose);
9799 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
9800 return (1);
9801 }
9802 if (sbavail(&so->so_snd) == 0)
9803 goto close_now;
9804 /* Ok we allow data that is ignored and a followup reset */
9805 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
9806 tp->rcv_nxt = th->th_seq + *tlen;
9807 tp->t_flags2 |= TF2_DROP_AF_DATA;
9808 rack->r_wanted_output = 1;
9809 *tlen = 0;
9810 return (0);
9811 }
9812
9813 /*
9814 * Return value of 1, the TCB is unlocked and most
9815 * likely gone, return value of 0, the TCP is still
9816 * locked.
9817 */
9818 static int
rack_do_fin_wait_1(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9819 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
9820 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9821 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9822 {
9823 int32_t ret_val = 0;
9824 int32_t ourfinisacked = 0;
9825
9826 ctf_calc_rwin(so, tp);
9827
9828 if ((thflags & TH_RST) ||
9829 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9830 return (ctf_process_rst(m, th, so, tp));
9831 /*
9832 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9833 * synchronized state.
9834 */
9835 if (thflags & TH_SYN) {
9836 ctf_challenge_ack(m, th, tp, &ret_val);
9837 return (ret_val);
9838 }
9839 /*
9840 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9841 * it's less than ts_recent, drop it.
9842 */
9843 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9844 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9845 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9846 return (ret_val);
9847 }
9848 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9849 return (ret_val);
9850 }
9851 /*
9852 * If new data are received on a connection after the user processes
9853 * are gone, then RST the other end.
9854 */
9855 if ((so->so_state & SS_NOFDREF) && tlen) {
9856 if (rack_check_data_after_close(m, tp, &tlen, th, so))
9857 return (1);
9858 }
9859 /*
9860 * If last ACK falls within this segment's sequence numbers, record
9861 * its timestamp. NOTE: 1) That the test incorporates suggestions
9862 * from the latest proposal of the [email protected] list (Braden
9863 * 1993/04/26). 2) That updating only on newer timestamps interferes
9864 * with our earlier PAWS tests, so this check should be solely
9865 * predicated on the sequence space of this segment. 3) That we
9866 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9867 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9868 * SEG.Len, This modified check allows us to overcome RFC1323's
9869 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9870 * p.869. In such cases, we can still calculate the RTT correctly
9871 * when RCV.NXT == Last.ACK.Sent.
9872 */
9873 if ((to->to_flags & TOF_TS) != 0 &&
9874 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9875 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9876 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
9877 tp->ts_recent_age = tcp_ts_getticks();
9878 tp->ts_recent = to->to_tsval;
9879 }
9880 /*
9881 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
9882 * is on (half-synchronized state), then queue data for later
9883 * processing; else drop segment and return.
9884 */
9885 if ((thflags & TH_ACK) == 0) {
9886 if (tp->t_flags & TF_NEEDSYN) {
9887 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9888 tiwin, thflags, nxt_pkt));
9889 } else if (tp->t_flags & TF_ACKNOW) {
9890 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
9891 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
9892 return (ret_val);
9893 } else {
9894 ctf_do_drop(m, NULL);
9895 return (0);
9896 }
9897 }
9898 /*
9899 * Ack processing.
9900 */
9901 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
9902 return (ret_val);
9903 }
9904 if (ourfinisacked) {
9905 /*
9906 * If we can't receive any more data, then closing user can
9907 * proceed. Starting the timer is contrary to the
9908 * specification, but if we don't get a FIN we'll hang
9909 * forever.
9910 *
9911 * XXXjl: we should release the tp also, and use a
9912 * compressed state.
9913 */
9914 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
9915 soisdisconnected(so);
9916 tcp_timer_activate(tp, TT_2MSL,
9917 (tcp_fast_finwait2_recycle ?
9918 tcp_finwait2_timeout :
9919 TP_MAXIDLE(tp)));
9920 }
9921 tcp_state_change(tp, TCPS_FIN_WAIT_2);
9922 }
9923 if (sbavail(&so->so_snd)) {
9924 if (ctf_progress_timeout_check(tp, true)) {
9925 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
9926 tp, tick, PROGRESS_DROP, __LINE__);
9927 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
9928 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
9929 return (1);
9930 }
9931 }
9932 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
9933 tiwin, thflags, nxt_pkt));
9934 }
9935
9936 /*
9937 * Return value of 1, the TCB is unlocked and most
9938 * likely gone, return value of 0, the TCP is still
9939 * locked.
9940 */
9941 static int
rack_do_closing(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)9942 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
9943 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
9944 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
9945 {
9946 int32_t ret_val = 0;
9947 int32_t ourfinisacked = 0;
9948
9949 ctf_calc_rwin(so, tp);
9950
9951 if ((thflags & TH_RST) ||
9952 (tp->t_fin_is_rst && (thflags & TH_FIN)))
9953 return (ctf_process_rst(m, th, so, tp));
9954 /*
9955 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
9956 * synchronized state.
9957 */
9958 if (thflags & TH_SYN) {
9959 ctf_challenge_ack(m, th, tp, &ret_val);
9960 return (ret_val);
9961 }
9962 /*
9963 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
9964 * it's less than ts_recent, drop it.
9965 */
9966 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
9967 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
9968 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
9969 return (ret_val);
9970 }
9971 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
9972 return (ret_val);
9973 }
9974 /*
9975 * If new data are received on a connection after the user processes
9976 * are gone, then RST the other end.
9977 */
9978 if ((so->so_state & SS_NOFDREF) && tlen) {
9979 if (rack_check_data_after_close(m, tp, &tlen, th, so))
9980 return (1);
9981 }
9982 /*
9983 * If last ACK falls within this segment's sequence numbers, record
9984 * its timestamp. NOTE: 1) That the test incorporates suggestions
9985 * from the latest proposal of the [email protected] list (Braden
9986 * 1993/04/26). 2) That updating only on newer timestamps interferes
9987 * with our earlier PAWS tests, so this check should be solely
9988 * predicated on the sequence space of this segment. 3) That we
9989 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
9990 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
9991 * SEG.Len, This modified check allows us to overcome RFC1323's
9992 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
9993 * p.869. In such cases, we can still calculate the RTT correctly
9994 * when RCV.NXT == Last.ACK.Sent.
9995 */
9996 if ((to->to_flags & TOF_TS) != 0 &&
9997 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
9998 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
9999 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
10000 tp->ts_recent_age = tcp_ts_getticks();
10001 tp->ts_recent = to->to_tsval;
10002 }
10003 /*
10004 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
10005 * is on (half-synchronized state), then queue data for later
10006 * processing; else drop segment and return.
10007 */
10008 if ((thflags & TH_ACK) == 0) {
10009 if (tp->t_flags & TF_NEEDSYN) {
10010 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10011 tiwin, thflags, nxt_pkt));
10012 } else if (tp->t_flags & TF_ACKNOW) {
10013 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
10014 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output= 1;
10015 return (ret_val);
10016 } else {
10017 ctf_do_drop(m, NULL);
10018 return (0);
10019 }
10020 }
10021 /*
10022 * Ack processing.
10023 */
10024 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
10025 return (ret_val);
10026 }
10027 if (ourfinisacked) {
10028 tcp_twstart(tp);
10029 m_freem(m);
10030 return (1);
10031 }
10032 if (sbavail(&so->so_snd)) {
10033 if (ctf_progress_timeout_check(tp, true)) {
10034 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
10035 tp, tick, PROGRESS_DROP, __LINE__);
10036 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
10037 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10038 return (1);
10039 }
10040 }
10041 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10042 tiwin, thflags, nxt_pkt));
10043 }
10044
10045 /*
10046 * Return value of 1, the TCB is unlocked and most
10047 * likely gone, return value of 0, the TCP is still
10048 * locked.
10049 */
10050 static int
rack_do_lastack(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)10051 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
10052 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10053 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
10054 {
10055 int32_t ret_val = 0;
10056 int32_t ourfinisacked = 0;
10057
10058 ctf_calc_rwin(so, tp);
10059
10060 if ((thflags & TH_RST) ||
10061 (tp->t_fin_is_rst && (thflags & TH_FIN)))
10062 return (ctf_process_rst(m, th, so, tp));
10063 /*
10064 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
10065 * synchronized state.
10066 */
10067 if (thflags & TH_SYN) {
10068 ctf_challenge_ack(m, th, tp, &ret_val);
10069 return (ret_val);
10070 }
10071 /*
10072 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
10073 * it's less than ts_recent, drop it.
10074 */
10075 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
10076 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
10077 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
10078 return (ret_val);
10079 }
10080 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
10081 return (ret_val);
10082 }
10083 /*
10084 * If new data are received on a connection after the user processes
10085 * are gone, then RST the other end.
10086 */
10087 if ((so->so_state & SS_NOFDREF) && tlen) {
10088 if (rack_check_data_after_close(m, tp, &tlen, th, so))
10089 return (1);
10090 }
10091 /*
10092 * If last ACK falls within this segment's sequence numbers, record
10093 * its timestamp. NOTE: 1) That the test incorporates suggestions
10094 * from the latest proposal of the [email protected] list (Braden
10095 * 1993/04/26). 2) That updating only on newer timestamps interferes
10096 * with our earlier PAWS tests, so this check should be solely
10097 * predicated on the sequence space of this segment. 3) That we
10098 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
10099 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
10100 * SEG.Len, This modified check allows us to overcome RFC1323's
10101 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
10102 * p.869. In such cases, we can still calculate the RTT correctly
10103 * when RCV.NXT == Last.ACK.Sent.
10104 */
10105 if ((to->to_flags & TOF_TS) != 0 &&
10106 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
10107 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
10108 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
10109 tp->ts_recent_age = tcp_ts_getticks();
10110 tp->ts_recent = to->to_tsval;
10111 }
10112 /*
10113 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
10114 * is on (half-synchronized state), then queue data for later
10115 * processing; else drop segment and return.
10116 */
10117 if ((thflags & TH_ACK) == 0) {
10118 if (tp->t_flags & TF_NEEDSYN) {
10119 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10120 tiwin, thflags, nxt_pkt));
10121 } else if (tp->t_flags & TF_ACKNOW) {
10122 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
10123 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
10124 return (ret_val);
10125 } else {
10126 ctf_do_drop(m, NULL);
10127 return (0);
10128 }
10129 }
10130 /*
10131 * case TCPS_LAST_ACK: Ack processing.
10132 */
10133 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
10134 return (ret_val);
10135 }
10136 if (ourfinisacked) {
10137 tp = tcp_close(tp);
10138 ctf_do_drop(m, tp);
10139 return (1);
10140 }
10141 if (sbavail(&so->so_snd)) {
10142 if (ctf_progress_timeout_check(tp, true)) {
10143 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
10144 tp, tick, PROGRESS_DROP, __LINE__);
10145 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
10146 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10147 return (1);
10148 }
10149 }
10150 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10151 tiwin, thflags, nxt_pkt));
10152 }
10153
10154 /*
10155 * Return value of 1, the TCB is unlocked and most
10156 * likely gone, return value of 0, the TCP is still
10157 * locked.
10158 */
10159 static int
rack_do_fin_wait_2(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,struct tcpopt * to,int32_t drop_hdrlen,int32_t tlen,uint32_t tiwin,int32_t thflags,int32_t nxt_pkt,uint8_t iptos)10160 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
10161 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
10162 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos)
10163 {
10164 int32_t ret_val = 0;
10165 int32_t ourfinisacked = 0;
10166
10167 ctf_calc_rwin(so, tp);
10168
10169 /* Reset receive buffer auto scaling when not in bulk receive mode. */
10170 if ((thflags & TH_RST) ||
10171 (tp->t_fin_is_rst && (thflags & TH_FIN)))
10172 return (ctf_process_rst(m, th, so, tp));
10173 /*
10174 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
10175 * synchronized state.
10176 */
10177 if (thflags & TH_SYN) {
10178 ctf_challenge_ack(m, th, tp, &ret_val);
10179 return (ret_val);
10180 }
10181 /*
10182 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
10183 * it's less than ts_recent, drop it.
10184 */
10185 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
10186 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
10187 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val))
10188 return (ret_val);
10189 }
10190 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
10191 return (ret_val);
10192 }
10193 /*
10194 * If new data are received on a connection after the user processes
10195 * are gone, then RST the other end.
10196 */
10197 if ((so->so_state & SS_NOFDREF) &&
10198 tlen) {
10199 if (rack_check_data_after_close(m, tp, &tlen, th, so))
10200 return (1);
10201 }
10202 /*
10203 * If last ACK falls within this segment's sequence numbers, record
10204 * its timestamp. NOTE: 1) That the test incorporates suggestions
10205 * from the latest proposal of the [email protected] list (Braden
10206 * 1993/04/26). 2) That updating only on newer timestamps interferes
10207 * with our earlier PAWS tests, so this check should be solely
10208 * predicated on the sequence space of this segment. 3) That we
10209 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
10210 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
10211 * SEG.Len, This modified check allows us to overcome RFC1323's
10212 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
10213 * p.869. In such cases, we can still calculate the RTT correctly
10214 * when RCV.NXT == Last.ACK.Sent.
10215 */
10216 if ((to->to_flags & TOF_TS) != 0 &&
10217 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
10218 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
10219 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
10220 tp->ts_recent_age = tcp_ts_getticks();
10221 tp->ts_recent = to->to_tsval;
10222 }
10223 /*
10224 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
10225 * is on (half-synchronized state), then queue data for later
10226 * processing; else drop segment and return.
10227 */
10228 if ((thflags & TH_ACK) == 0) {
10229 if (tp->t_flags & TF_NEEDSYN) {
10230 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10231 tiwin, thflags, nxt_pkt));
10232 } else if (tp->t_flags & TF_ACKNOW) {
10233 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
10234 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1;
10235 return (ret_val);
10236 } else {
10237 ctf_do_drop(m, NULL);
10238 return (0);
10239 }
10240 }
10241 /*
10242 * Ack processing.
10243 */
10244 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
10245 return (ret_val);
10246 }
10247 if (sbavail(&so->so_snd)) {
10248 if (ctf_progress_timeout_check(tp, true)) {
10249 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr,
10250 tp, tick, PROGRESS_DROP, __LINE__);
10251 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
10252 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10253 return (1);
10254 }
10255 }
10256 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
10257 tiwin, thflags, nxt_pkt));
10258 }
10259
10260 static void inline
rack_clear_rate_sample(struct tcp_rack * rack)10261 rack_clear_rate_sample(struct tcp_rack *rack)
10262 {
10263 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
10264 rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
10265 rack->r_ctl.rack_rs.rs_rtt_tot = 0;
10266 }
10267
10268 static void
rack_set_pace_segments(struct tcpcb * tp,struct tcp_rack * rack,uint32_t line)10269 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line)
10270 {
10271 uint64_t bw_est, rate_wanted;
10272 int chged = 0;
10273 uint32_t user_max;
10274
10275 user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs;
10276 if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs)
10277 chged = 1;
10278 rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp);
10279 if (rack->use_fixed_rate || rack->rc_force_max_seg) {
10280 if (user_max != rack->r_ctl.rc_pace_max_segs)
10281 chged = 1;
10282 }
10283 if (rack->rc_force_max_seg) {
10284 rack->r_ctl.rc_pace_max_segs = user_max;
10285 } else if (rack->use_fixed_rate) {
10286 bw_est = rack_get_bw(rack);
10287 if ((rack->r_ctl.crte == NULL) ||
10288 (bw_est != rack->r_ctl.crte->rate)) {
10289 rack->r_ctl.rc_pace_max_segs = user_max;
10290 } else {
10291 /* We are pacing right at the hardware rate */
10292 uint32_t segsiz;
10293
10294 segsiz = min(ctf_fixed_maxseg(tp),
10295 rack->r_ctl.rc_pace_min_segs);
10296 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(
10297 bw_est, segsiz, 0,
10298 rack->r_ctl.crte, NULL);
10299 }
10300 } else if (rack->rc_always_pace) {
10301 if (rack->r_ctl.gp_bw ||
10302 #ifdef NETFLIX_PEAKRATE
10303 rack->rc_tp->t_maxpeakrate ||
10304 #endif
10305 rack->r_ctl.init_rate) {
10306 /* We have a rate of some sort set */
10307 uint32_t orig;
10308
10309 bw_est = rack_get_bw(rack);
10310 orig = rack->r_ctl.rc_pace_max_segs;
10311 rate_wanted = rack_get_output_bw(rack, bw_est, NULL);
10312 if (rate_wanted) {
10313 /* We have something */
10314 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack,
10315 rate_wanted,
10316 ctf_fixed_maxseg(rack->rc_tp));
10317 } else
10318 rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs;
10319 if (orig != rack->r_ctl.rc_pace_max_segs)
10320 chged = 1;
10321 } else if ((rack->r_ctl.gp_bw == 0) &&
10322 (rack->r_ctl.rc_pace_max_segs == 0)) {
10323 /*
10324 * If we have nothing limit us to bursting
10325 * out IW sized pieces.
10326 */
10327 chged = 1;
10328 rack->r_ctl.rc_pace_max_segs = rc_init_window(rack);
10329 }
10330 }
10331 if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) {
10332 chged = 1;
10333 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES;
10334 }
10335 if (chged)
10336 rack_log_type_hrdwtso(tp, rack, 0, rack->rc_inp->inp_socket->so_snd.sb_flags, line, 2);
10337 }
10338
10339 static int
rack_init(struct tcpcb * tp)10340 rack_init(struct tcpcb *tp)
10341 {
10342 struct tcp_rack *rack = NULL;
10343 struct rack_sendmap *insret;
10344 uint32_t iwin, snt, us_cts;
10345
10346 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
10347 if (tp->t_fb_ptr == NULL) {
10348 /*
10349 * We need to allocate memory but cant. The INP and INP_INFO
10350 * locks and they are recusive (happens during setup. So a
10351 * scheme to drop the locks fails :(
10352 *
10353 */
10354 return (ENOMEM);
10355 }
10356 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
10357
10358 rack = (struct tcp_rack *)tp->t_fb_ptr;
10359 RB_INIT(&rack->r_ctl.rc_mtree);
10360 TAILQ_INIT(&rack->r_ctl.rc_free);
10361 TAILQ_INIT(&rack->r_ctl.rc_tmap);
10362 rack->rc_tp = tp;
10363 if (tp->t_inpcb) {
10364 rack->rc_inp = tp->t_inpcb;
10365 }
10366 /* Probably not needed but lets be sure */
10367 rack_clear_rate_sample(rack);
10368 rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
10369 rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
10370 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
10371 if (use_rack_rr)
10372 rack->use_rack_rr = 1;
10373 if (V_tcp_delack_enabled)
10374 tp->t_delayed_ack = 1;
10375 else
10376 tp->t_delayed_ack = 0;
10377 if (rack_enable_shared_cwnd)
10378 rack->rack_enable_scwnd = 1;
10379 rack->rc_user_set_max_segs = rack_hptsi_segments;
10380 rack->rc_force_max_seg = 0;
10381 if (rack_use_imac_dack)
10382 rack->rc_dack_mode = 1;
10383 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
10384 rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
10385 rack->r_ctl.rc_prop_reduce = rack_use_proportional_reduce;
10386 rack->r_ctl.rc_prop_rate = rack_proportional_rate;
10387 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
10388 rack->r_ctl.rc_early_recovery = rack_early_recovery;
10389 rack->r_ctl.rc_lowest_us_rtt = 0xffffffff;
10390 rack->r_ctl.rc_highest_us_rtt = 0;
10391 if (rack_disable_prr)
10392 rack->rack_no_prr = 1;
10393 if (rack_gp_no_rec_chg)
10394 rack->rc_gp_no_rec_chg = 1;
10395 rack->rc_always_pace = rack_pace_every_seg;
10396 if (rack_enable_mqueue_for_nonpaced)
10397 rack->r_mbuf_queue = 1;
10398 else
10399 rack->r_mbuf_queue = 0;
10400 if (rack->r_mbuf_queue || rack->rc_always_pace)
10401 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
10402 else
10403 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
10404 rack_set_pace_segments(tp, rack, __LINE__);
10405 if (rack_limits_scwnd)
10406 rack->r_limit_scw = 1;
10407 else
10408 rack->r_limit_scw = 0;
10409 rack->r_ctl.rc_high_rwnd = tp->snd_wnd;
10410 rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
10411 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
10412 rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
10413 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
10414 rack->r_ctl.rc_min_to = rack_min_to;
10415 microuptime(&rack->r_ctl.act_rcv_time);
10416 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time;
10417 rack->r_running_late = 0;
10418 rack->r_running_early = 0;
10419 rack->rc_init_win = rack_default_init_window;
10420 rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss;
10421 if (rack_do_dyn_mul) {
10422 /* When dynamic adjustment is on CA needs to start at 100% */
10423 rack->rc_gp_dyn_mul = 1;
10424 if (rack_do_dyn_mul >= 100)
10425 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul;
10426 } else
10427 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca;
10428 rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec;
10429 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt;
10430 rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time);
10431 setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN,
10432 rack_probertt_filter_life);
10433 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
10434 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
10435 rack->r_ctl.rc_time_of_last_probertt = us_cts;
10436 rack->r_ctl.rc_time_probertt_starts = 0;
10437 /* Do we force on detection? */
10438 #ifdef NETFLIX_EXP_DETECTION
10439 if (tcp_force_detection)
10440 rack->do_detection = 1;
10441 else
10442 #endif
10443 rack->do_detection = 0;
10444 if (rack_non_rxt_use_cr)
10445 rack->rack_rec_nonrxt_use_cr = 1;
10446 if (tp->snd_una != tp->snd_max) {
10447 /* Create a send map for the current outstanding data */
10448 struct rack_sendmap *rsm;
10449
10450 rsm = rack_alloc(rack);
10451 if (rsm == NULL) {
10452 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
10453 tp->t_fb_ptr = NULL;
10454 return (ENOMEM);
10455 }
10456 rsm->r_flags = RACK_OVERMAX;
10457 rsm->r_tim_lastsent[0] = rack->r_ctl.rc_tlp_rxt_last_time;
10458 rsm->r_rtr_cnt = 1;
10459 rsm->r_rtr_bytes = 0;
10460 rsm->r_start = tp->snd_una;
10461 if (tp->t_flags & TF_SENTFIN) {
10462 rsm->r_end = tp->snd_max - 1;
10463 rsm->r_flags |= RACK_HAS_FIN;
10464 } else {
10465 rsm->r_end = tp->snd_max;
10466 }
10467 rsm->usec_orig_send = us_cts;
10468 rsm->r_dupack = 0;
10469 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
10470 #ifdef INVARIANTS
10471 if (insret != NULL) {
10472 panic("Insert in rb tree fails ret:%p rack:%p rsm:%p",
10473 insret, rack, rsm);
10474 }
10475 #endif
10476 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
10477 rsm->r_in_tmap = 1;
10478 }
10479 /* Cancel the GP measurement in progress */
10480 tp->t_flags &= ~TF_GPUTINPROG;
10481 if (SEQ_GT(tp->snd_max, tp->iss))
10482 snt = tp->snd_max - tp->iss;
10483 else
10484 snt = 0;
10485 iwin = rc_init_window(rack);
10486 if (snt < iwin) {
10487 /* We are not past the initial window
10488 * so we need to make sure cwnd is
10489 * correct.
10490 */
10491 if (tp->snd_cwnd < iwin)
10492 tp->snd_cwnd = iwin;
10493 /*
10494 * If we are within the initial window
10495 * we want ssthresh to be unlimited. Setting
10496 * it to the rwnd (which the default stack does
10497 * and older racks) is not really a good idea
10498 * since we want to be in SS and grow both the
10499 * cwnd and the rwnd (via dynamic rwnd growth). If
10500 * we set it to the rwnd then as the peer grows its
10501 * rwnd we will be stuck in CA and never hit SS.
10502 *
10503 * Its far better to raise it up high (this takes the
10504 * risk that there as been a loss already, probably
10505 * we should have an indicator in all stacks of loss
10506 * but we don't), but considering the normal use this
10507 * is a risk worth taking. The consequences of not
10508 * hitting SS are far worse than going one more time
10509 * into it early on (before we have sent even a IW).
10510 * It is highly unlikely that we will have had a loss
10511 * before getting the IW out.
10512 */
10513 tp->snd_ssthresh = 0xffffffff;
10514 }
10515 rack_stop_all_timers(tp);
10516 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), 0, 0, 0);
10517 rack_log_rtt_shrinks(rack, us_cts, 0,
10518 __LINE__, RACK_RTTS_INIT);
10519 return (0);
10520 }
10521
10522 static int
rack_handoff_ok(struct tcpcb * tp)10523 rack_handoff_ok(struct tcpcb *tp)
10524 {
10525 if ((tp->t_state == TCPS_CLOSED) ||
10526 (tp->t_state == TCPS_LISTEN)) {
10527 /* Sure no problem though it may not stick */
10528 return (0);
10529 }
10530 if ((tp->t_state == TCPS_SYN_SENT) ||
10531 (tp->t_state == TCPS_SYN_RECEIVED)) {
10532 /*
10533 * We really don't know if you support sack,
10534 * you have to get to ESTAB or beyond to tell.
10535 */
10536 return (EAGAIN);
10537 }
10538 if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) {
10539 /*
10540 * Rack will only send a FIN after all data is acknowledged.
10541 * So in this case we have more data outstanding. We can't
10542 * switch stacks until either all data and only the FIN
10543 * is left (in which case rack_init() now knows how
10544 * to deal with that) <or> all is acknowledged and we
10545 * are only left with incoming data, though why you
10546 * would want to switch to rack after all data is acknowledged
10547 * I have no idea (rrs)!
10548 */
10549 return (EAGAIN);
10550 }
10551 if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){
10552 return (0);
10553 }
10554 /*
10555 * If we reach here we don't do SACK on this connection so we can
10556 * never do rack.
10557 */
10558 return (EINVAL);
10559 }
10560
10561 static void
rack_fini(struct tcpcb * tp,int32_t tcb_is_purged)10562 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
10563 {
10564 if (tp->t_fb_ptr) {
10565 struct tcp_rack *rack;
10566 struct rack_sendmap *rsm, *nrsm, *rm;
10567
10568 rack = (struct tcp_rack *)tp->t_fb_ptr;
10569 #ifdef NETFLIX_SHARED_CWND
10570 if (rack->r_ctl.rc_scw) {
10571 uint32_t limit;
10572
10573 if (rack->r_limit_scw)
10574 limit = max(1, rack->r_ctl.rc_lowest_us_rtt);
10575 else
10576 limit = 0;
10577 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw,
10578 rack->r_ctl.rc_scw_index,
10579 limit);
10580 rack->r_ctl.rc_scw = NULL;
10581 }
10582 #endif
10583 /* rack does not use force data but other stacks may clear it */
10584 tp->t_flags &= ~TF_FORCEDATA;
10585 if (tp->t_inpcb) {
10586 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
10587 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY;
10588 tp->t_inpcb->inp_flags2 &= ~INP_DONT_SACK_QUEUE;
10589 }
10590 #ifdef TCP_BLACKBOX
10591 tcp_log_flowend(tp);
10592 #endif
10593 RB_FOREACH_SAFE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm) {
10594 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm);
10595 #ifdef INVARIANTS
10596 if (rm != rsm) {
10597 panic("At fini, rack:%p rsm:%p rm:%p",
10598 rack, rsm, rm);
10599 }
10600 #endif
10601 uma_zfree(rack_zone, rsm);
10602 }
10603 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
10604 while (rsm) {
10605 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext);
10606 uma_zfree(rack_zone, rsm);
10607 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
10608 }
10609 rack->rc_free_cnt = 0;
10610 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
10611 tp->t_fb_ptr = NULL;
10612 }
10613 /* Cancel the GP measurement in progress */
10614 tp->t_flags &= ~TF_GPUTINPROG;
10615 /* Make sure snd_nxt is correctly set */
10616 tp->snd_nxt = tp->snd_max;
10617 }
10618
10619 static void
rack_set_state(struct tcpcb * tp,struct tcp_rack * rack)10620 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
10621 {
10622 switch (tp->t_state) {
10623 case TCPS_SYN_SENT:
10624 rack->r_state = TCPS_SYN_SENT;
10625 rack->r_substate = rack_do_syn_sent;
10626 break;
10627 case TCPS_SYN_RECEIVED:
10628 rack->r_state = TCPS_SYN_RECEIVED;
10629 rack->r_substate = rack_do_syn_recv;
10630 break;
10631 case TCPS_ESTABLISHED:
10632 rack_set_pace_segments(tp, rack, __LINE__);
10633 rack->r_state = TCPS_ESTABLISHED;
10634 rack->r_substate = rack_do_established;
10635 break;
10636 case TCPS_CLOSE_WAIT:
10637 rack->r_state = TCPS_CLOSE_WAIT;
10638 rack->r_substate = rack_do_close_wait;
10639 break;
10640 case TCPS_FIN_WAIT_1:
10641 rack->r_state = TCPS_FIN_WAIT_1;
10642 rack->r_substate = rack_do_fin_wait_1;
10643 break;
10644 case TCPS_CLOSING:
10645 rack->r_state = TCPS_CLOSING;
10646 rack->r_substate = rack_do_closing;
10647 break;
10648 case TCPS_LAST_ACK:
10649 rack->r_state = TCPS_LAST_ACK;
10650 rack->r_substate = rack_do_lastack;
10651 break;
10652 case TCPS_FIN_WAIT_2:
10653 rack->r_state = TCPS_FIN_WAIT_2;
10654 rack->r_substate = rack_do_fin_wait_2;
10655 break;
10656 case TCPS_LISTEN:
10657 case TCPS_CLOSED:
10658 case TCPS_TIME_WAIT:
10659 default:
10660 break;
10661 };
10662 }
10663
10664 static void
rack_timer_audit(struct tcpcb * tp,struct tcp_rack * rack,struct sockbuf * sb)10665 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
10666 {
10667 /*
10668 * We received an ack, and then did not
10669 * call send or were bounced out due to the
10670 * hpts was running. Now a timer is up as well, is
10671 * it the right timer?
10672 */
10673 struct rack_sendmap *rsm;
10674 int tmr_up;
10675
10676 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
10677 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
10678 return;
10679 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
10680 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
10681 (tmr_up == PACE_TMR_RXT)) {
10682 /* Should be an RXT */
10683 return;
10684 }
10685 if (rsm == NULL) {
10686 /* Nothing outstanding? */
10687 if (tp->t_flags & TF_DELACK) {
10688 if (tmr_up == PACE_TMR_DELACK)
10689 /* We are supposed to have delayed ack up and we do */
10690 return;
10691 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
10692 /*
10693 * if we hit enobufs then we would expect the possiblity
10694 * of nothing outstanding and the RXT up (and the hptsi timer).
10695 */
10696 return;
10697 } else if (((V_tcp_always_keepalive ||
10698 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
10699 (tp->t_state <= TCPS_CLOSING)) &&
10700 (tmr_up == PACE_TMR_KEEP) &&
10701 (tp->snd_max == tp->snd_una)) {
10702 /* We should have keep alive up and we do */
10703 return;
10704 }
10705 }
10706 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
10707 ((tmr_up == PACE_TMR_TLP) ||
10708 (tmr_up == PACE_TMR_RACK) ||
10709 (tmr_up == PACE_TMR_RXT))) {
10710 /*
10711 * Either a Rack, TLP or RXT is fine if we
10712 * have outstanding data.
10713 */
10714 return;
10715 } else if (tmr_up == PACE_TMR_DELACK) {
10716 /*
10717 * If the delayed ack was going to go off
10718 * before the rtx/tlp/rack timer were going to
10719 * expire, then that would be the timer in control.
10720 * Note we don't check the time here trusting the
10721 * code is correct.
10722 */
10723 return;
10724 }
10725 /*
10726 * Ok the timer originally started is not what we want now.
10727 * We will force the hpts to be stopped if any, and restart
10728 * with the slot set to what was in the saved slot.
10729 */
10730 if (rack->rc_inp->inp_in_hpts) {
10731 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
10732 uint32_t us_cts;
10733
10734 us_cts = tcp_get_usecs(NULL);
10735 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
10736 rack->r_early = 1;
10737 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
10738 }
10739 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
10740 }
10741 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
10742 }
10743 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
10744 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), 0, 0, 0);
10745 }
10746
10747 static int
rack_do_segment_nounlock(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos,int32_t nxt_pkt,struct timeval * tv)10748 rack_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so,
10749 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
10750 int32_t nxt_pkt, struct timeval *tv)
10751 {
10752 int32_t thflags, retval, did_out = 0;
10753 int32_t way_out = 0;
10754 uint32_t cts;
10755 uint32_t tiwin;
10756 struct timespec ts;
10757 struct tcpopt to;
10758 struct tcp_rack *rack;
10759 struct rack_sendmap *rsm;
10760 int32_t prev_state = 0;
10761 uint32_t us_cts;
10762 /*
10763 * tv passed from common code is from either M_TSTMP_LRO or
10764 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present. The
10765 * rack_pacing stack assumes tv always refers to 'now', so we overwrite
10766 * tv here to guarantee that.
10767 */
10768 if (m->m_flags & M_TSTMP_LRO)
10769 tcp_get_usecs(tv);
10770
10771 cts = tcp_tv_to_mssectick(tv);
10772 rack = (struct tcp_rack *)tp->t_fb_ptr;
10773
10774 if ((m->m_flags & M_TSTMP) ||
10775 (m->m_flags & M_TSTMP_LRO)) {
10776 mbuf_tstmp2timespec(m, &ts);
10777 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec;
10778 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000;
10779 } else
10780 rack->r_ctl.act_rcv_time = *tv;
10781 kern_prefetch(rack, &prev_state);
10782 prev_state = 0;
10783 thflags = th->th_flags;
10784
10785 NET_EPOCH_ASSERT();
10786 INP_WLOCK_ASSERT(tp->t_inpcb);
10787 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
10788 __func__));
10789 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
10790 __func__));
10791 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
10792 union tcp_log_stackspecific log;
10793 struct timeval ltv;
10794 #ifdef NETFLIX_HTTP_LOGGING
10795 struct http_sendfile_track *http_req;
10796
10797 if (SEQ_GT(th->th_ack, tp->snd_una)) {
10798 http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1));
10799 } else {
10800 http_req = tcp_http_find_req_for_seq(tp, th->th_ack);
10801 }
10802 #endif
10803 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
10804 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
10805 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
10806 if (rack->rack_no_prr == 0)
10807 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
10808 else
10809 log.u_bbr.flex1 = 0;
10810 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced;
10811 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
10812 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
10813 log.u_bbr.flex3 = m->m_flags;
10814 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
10815 if (m->m_flags & M_TSTMP) {
10816 /* Record the hardware timestamp if present */
10817 mbuf_tstmp2timespec(m, &ts);
10818 ltv.tv_sec = ts.tv_sec;
10819 ltv.tv_usec = ts.tv_nsec / 1000;
10820 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v);
10821 } else if (m->m_flags & M_TSTMP_LRO) {
10822 /* Record the LRO the arrival timestamp */
10823 mbuf_tstmp2timespec(m, &ts);
10824 ltv.tv_sec = ts.tv_sec;
10825 ltv.tv_usec = ts.tv_nsec / 1000;
10826 log.u_bbr.flex5 = tcp_tv_to_usectick(<v);
10827 }
10828 log.u_bbr.timeStamp = tcp_get_usecs(<v);
10829 /* Log the rcv time */
10830 log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp;
10831 #ifdef NETFLIX_HTTP_LOGGING
10832 log.u_bbr.applimited = tp->t_http_closed;
10833 log.u_bbr.applimited <<= 8;
10834 log.u_bbr.applimited |= tp->t_http_open;
10835 log.u_bbr.applimited <<= 8;
10836 log.u_bbr.applimited |= tp->t_http_req;
10837 if (http_req) {
10838 /* Copy out any client req info */
10839 /* seconds */
10840 log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC);
10841 /* useconds */
10842 log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC);
10843 log.u_bbr.rttProp = http_req->timestamp;
10844 log.u_bbr.cur_del_rate = http_req->start;
10845 if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) {
10846 log.u_bbr.flex8 |= 1;
10847 } else {
10848 log.u_bbr.flex8 |= 2;
10849 log.u_bbr.bw_inuse = http_req->end;
10850 }
10851 log.u_bbr.flex6 = http_req->start_seq;
10852 if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) {
10853 log.u_bbr.flex8 |= 4;
10854 log.u_bbr.epoch = http_req->end_seq;
10855 }
10856 }
10857 #endif
10858 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
10859 tlen, &log, true, <v);
10860 }
10861 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
10862 way_out = 4;
10863 retval = 0;
10864 goto done_with_input;
10865 }
10866 /*
10867 * If a segment with the ACK-bit set arrives in the SYN-SENT state
10868 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
10869 */
10870 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
10871 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
10872 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
10873 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
10874 return(1);
10875 }
10876
10877 /*
10878 * Parse options on any incoming segment.
10879 */
10880 tcp_dooptions(&to, (u_char *)(th + 1),
10881 (th->th_off << 2) - sizeof(struct tcphdr),
10882 (thflags & TH_SYN) ? TO_SYN : 0);
10883
10884 /*
10885 * If timestamps were negotiated during SYN/ACK and a
10886 * segment without a timestamp is received, silently drop
10887 * the segment, unless it is a RST segment or missing timestamps are
10888 * tolerated.
10889 * See section 3.2 of RFC 7323.
10890 */
10891 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) &&
10892 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) {
10893 way_out = 5;
10894 retval = 0;
10895 goto done_with_input;
10896 }
10897
10898 /*
10899 * Segment received on connection. Reset idle time and keep-alive
10900 * timer. XXX: This should be done after segment validation to
10901 * ignore broken/spoofed segs.
10902 */
10903 if (tp->t_idle_reduce &&
10904 (tp->snd_max == tp->snd_una) &&
10905 ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) {
10906 counter_u64_add(rack_input_idle_reduces, 1);
10907 rack_cc_after_idle(rack, tp);
10908 }
10909 tp->t_rcvtime = ticks;
10910 /*
10911 * Unscale the window into a 32-bit value. For the SYN_SENT state
10912 * the scale is zero.
10913 */
10914 tiwin = th->th_win << tp->snd_scale;
10915 #ifdef STATS
10916 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
10917 #endif
10918 if (tiwin > rack->r_ctl.rc_high_rwnd)
10919 rack->r_ctl.rc_high_rwnd = tiwin;
10920 /*
10921 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
10922 * this to occur after we've validated the segment.
10923 */
10924 if (tp->t_flags2 & TF2_ECN_PERMIT) {
10925 if (thflags & TH_CWR) {
10926 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
10927 tp->t_flags |= TF_ACKNOW;
10928 }
10929 switch (iptos & IPTOS_ECN_MASK) {
10930 case IPTOS_ECN_CE:
10931 tp->t_flags2 |= TF2_ECN_SND_ECE;
10932 KMOD_TCPSTAT_INC(tcps_ecn_ce);
10933 break;
10934 case IPTOS_ECN_ECT0:
10935 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
10936 break;
10937 case IPTOS_ECN_ECT1:
10938 KMOD_TCPSTAT_INC(tcps_ecn_ect1);
10939 break;
10940 }
10941
10942 /* Process a packet differently from RFC3168. */
10943 cc_ecnpkt_handler(tp, th, iptos);
10944
10945 /* Congestion experienced. */
10946 if (thflags & TH_ECE) {
10947 rack_cong_signal(tp, th, CC_ECN);
10948 }
10949 }
10950
10951 /*
10952 * If echoed timestamp is later than the current time, fall back to
10953 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
10954 * were used when this connection was established.
10955 */
10956 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
10957 to.to_tsecr -= tp->ts_offset;
10958 if (TSTMP_GT(to.to_tsecr, cts))
10959 to.to_tsecr = 0;
10960 }
10961
10962 /*
10963 * If its the first time in we need to take care of options and
10964 * verify we can do SACK for rack!
10965 */
10966 if (rack->r_state == 0) {
10967 /* Should be init'd by rack_init() */
10968 KASSERT(rack->rc_inp != NULL,
10969 ("%s: rack->rc_inp unexpectedly NULL", __func__));
10970 if (rack->rc_inp == NULL) {
10971 rack->rc_inp = tp->t_inpcb;
10972 }
10973
10974 /*
10975 * Process options only when we get SYN/ACK back. The SYN
10976 * case for incoming connections is handled in tcp_syncache.
10977 * According to RFC1323 the window field in a SYN (i.e., a
10978 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
10979 * this is traditional behavior, may need to be cleaned up.
10980 */
10981 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
10982 /* Handle parallel SYN for ECN */
10983 if (!(thflags & TH_ACK) &&
10984 ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) &&
10985 ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) {
10986 tp->t_flags2 |= TF2_ECN_PERMIT;
10987 tp->t_flags2 |= TF2_ECN_SND_ECE;
10988 TCPSTAT_INC(tcps_ecn_shs);
10989 }
10990 if ((to.to_flags & TOF_SCALE) &&
10991 (tp->t_flags & TF_REQ_SCALE)) {
10992 tp->t_flags |= TF_RCVD_SCALE;
10993 tp->snd_scale = to.to_wscale;
10994 } else
10995 tp->t_flags &= ~TF_REQ_SCALE;
10996 /*
10997 * Initial send window. It will be updated with the
10998 * next incoming segment to the scaled value.
10999 */
11000 tp->snd_wnd = th->th_win;
11001 if ((to.to_flags & TOF_TS) &&
11002 (tp->t_flags & TF_REQ_TSTMP)) {
11003 tp->t_flags |= TF_RCVD_TSTMP;
11004 tp->ts_recent = to.to_tsval;
11005 tp->ts_recent_age = cts;
11006 } else
11007 tp->t_flags &= ~TF_REQ_TSTMP;
11008 if (to.to_flags & TOF_MSS)
11009 tcp_mss(tp, to.to_mss);
11010 if ((tp->t_flags & TF_SACK_PERMIT) &&
11011 (to.to_flags & TOF_SACKPERM) == 0)
11012 tp->t_flags &= ~TF_SACK_PERMIT;
11013 if (IS_FASTOPEN(tp->t_flags)) {
11014 if (to.to_flags & TOF_FASTOPEN) {
11015 uint16_t mss;
11016
11017 if (to.to_flags & TOF_MSS)
11018 mss = to.to_mss;
11019 else
11020 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
11021 mss = TCP6_MSS;
11022 else
11023 mss = TCP_MSS;
11024 tcp_fastopen_update_cache(tp, mss,
11025 to.to_tfo_len, to.to_tfo_cookie);
11026 } else
11027 tcp_fastopen_disable_path(tp);
11028 }
11029 }
11030 /*
11031 * At this point we are at the initial call. Here we decide
11032 * if we are doing RACK or not. We do this by seeing if
11033 * TF_SACK_PERMIT is set and the sack-not-required is clear.
11034 * The code now does do dup-ack counting so if you don't
11035 * switch back you won't get rack & TLP, but you will still
11036 * get this stack.
11037 */
11038
11039 if ((rack_sack_not_required == 0) &&
11040 ((tp->t_flags & TF_SACK_PERMIT) == 0)) {
11041 tcp_switch_back_to_default(tp);
11042 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
11043 tlen, iptos);
11044 return (1);
11045 }
11046 /* Set the flag */
11047 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
11048 tcp_set_hpts(tp->t_inpcb);
11049 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
11050 }
11051 if (thflags & TH_FIN)
11052 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
11053 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
11054 if ((rack->rc_gp_dyn_mul) &&
11055 (rack->use_fixed_rate == 0) &&
11056 (rack->rc_always_pace)) {
11057 /* Check in on probertt */
11058 rack_check_probe_rtt(rack, us_cts);
11059 }
11060 if (rack->forced_ack) {
11061 uint32_t us_rtt;
11062
11063 /*
11064 * A persist or keep-alive was forced out, update our
11065 * min rtt time. Note we do not worry about lost
11066 * retransmissions since KEEP-ALIVES and persists
11067 * are usually way long on times of sending (though
11068 * if we were really paranoid or worried we could
11069 * at least use timestamps if available to validate).
11070 */
11071 rack->forced_ack = 0;
11072 us_rtt = us_cts - rack->r_ctl.forced_ack_ts;
11073 if (us_rtt == 0)
11074 us_rtt = 1;
11075 rack_log_rtt_upd(tp, rack, us_rtt, 0, NULL, 3);
11076 rack_apply_updated_usrtt(rack, us_rtt, us_cts);
11077 }
11078 /*
11079 * This is the one exception case where we set the rack state
11080 * always. All other times (timers etc) we must have a rack-state
11081 * set (so we assure we have done the checks above for SACK).
11082 */
11083 rack->r_ctl.rc_rcvtime = cts;
11084 if (rack->r_state != tp->t_state)
11085 rack_set_state(tp, rack);
11086 if (SEQ_GT(th->th_ack, tp->snd_una) &&
11087 (rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree)) != NULL)
11088 kern_prefetch(rsm, &prev_state);
11089 prev_state = rack->r_state;
11090 rack_clear_rate_sample(rack);
11091 retval = (*rack->r_substate) (m, th, so,
11092 tp, &to, drop_hdrlen,
11093 tlen, tiwin, thflags, nxt_pkt, iptos);
11094 #ifdef INVARIANTS
11095 if ((retval == 0) &&
11096 (tp->t_inpcb == NULL)) {
11097 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
11098 retval, tp, prev_state);
11099 }
11100 #endif
11101 if (retval == 0) {
11102 /*
11103 * If retval is 1 the tcb is unlocked and most likely the tp
11104 * is gone.
11105 */
11106 INP_WLOCK_ASSERT(tp->t_inpcb);
11107 if ((rack->rc_gp_dyn_mul) &&
11108 (rack->rc_always_pace) &&
11109 (rack->use_fixed_rate == 0) &&
11110 rack->in_probe_rtt &&
11111 (rack->r_ctl.rc_time_probertt_starts == 0)) {
11112 /*
11113 * If we are going for target, lets recheck before
11114 * we output.
11115 */
11116 rack_check_probe_rtt(rack, us_cts);
11117 }
11118 if (rack->set_pacing_done_a_iw == 0) {
11119 /* How much has been acked? */
11120 if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) {
11121 /* We have enough to set in the pacing segment size */
11122 rack->set_pacing_done_a_iw = 1;
11123 rack_set_pace_segments(tp, rack, __LINE__);
11124 }
11125 }
11126 tcp_rack_xmit_timer_commit(rack, tp);
11127 if (nxt_pkt == 0) {
11128 if (rack->r_wanted_output != 0) {
11129 do_output_now:
11130 did_out = 1;
11131 (void)tp->t_fb->tfb_tcp_output(tp);
11132 }
11133 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0);
11134 }
11135 if ((nxt_pkt == 0) &&
11136 ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
11137 (SEQ_GT(tp->snd_max, tp->snd_una) ||
11138 (tp->t_flags & TF_DELACK) ||
11139 ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
11140 (tp->t_state <= TCPS_CLOSING)))) {
11141 /* We could not send (probably in the hpts but stopped the timer earlier)? */
11142 if ((tp->snd_max == tp->snd_una) &&
11143 ((tp->t_flags & TF_DELACK) == 0) &&
11144 (rack->rc_inp->inp_in_hpts) &&
11145 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
11146 /* keep alive not needed if we are hptsi output yet */
11147 ;
11148 } else {
11149 int late = 0;
11150 if (rack->rc_inp->inp_in_hpts) {
11151 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
11152 us_cts = tcp_get_usecs(NULL);
11153 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
11154 rack->r_early = 1;
11155 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts);
11156 } else
11157 late = 1;
11158 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
11159 }
11160 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
11161 }
11162 if (late && (did_out == 0)) {
11163 /*
11164 * We are late in the sending
11165 * and we did not call the output
11166 * (this probably should not happen).
11167 */
11168 goto do_output_now;
11169 }
11170 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), 0, 0, 0);
11171 }
11172 way_out = 1;
11173 } else if (nxt_pkt == 0) {
11174 /* Do we have the correct timer running? */
11175 rack_timer_audit(tp, rack, &so->so_snd);
11176 way_out = 2;
11177 }
11178 done_with_input:
11179 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out);
11180 if (did_out)
11181 rack->r_wanted_output = 0;
11182 #ifdef INVARIANTS
11183 if (tp->t_inpcb == NULL) {
11184 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
11185 did_out,
11186 retval, tp, prev_state);
11187 }
11188 #endif
11189 }
11190 return (retval);
11191 }
11192
11193 void
rack_do_segment(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int32_t drop_hdrlen,int32_t tlen,uint8_t iptos)11194 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
11195 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
11196 {
11197 struct timeval tv;
11198
11199 /* First lets see if we have old packets */
11200 if (tp->t_in_pkt) {
11201 if (ctf_do_queued_segments(so, tp, 1)) {
11202 m_freem(m);
11203 return;
11204 }
11205 }
11206 if (m->m_flags & M_TSTMP_LRO) {
11207 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
11208 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
11209 } else {
11210 /* Should not be should we kassert instead? */
11211 tcp_get_usecs(&tv);
11212 }
11213 if(rack_do_segment_nounlock(m, th, so, tp,
11214 drop_hdrlen, tlen, iptos, 0, &tv) == 0) {
11215 tcp_handle_wakeup(tp, so);
11216 INP_WUNLOCK(tp->t_inpcb);
11217 }
11218 }
11219
11220 struct rack_sendmap *
tcp_rack_output(struct tcpcb * tp,struct tcp_rack * rack,uint32_t tsused)11221 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
11222 {
11223 struct rack_sendmap *rsm = NULL;
11224 int32_t idx;
11225 uint32_t srtt = 0, thresh = 0, ts_low = 0;
11226
11227 /* Return the next guy to be re-transmitted */
11228 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) {
11229 return (NULL);
11230 }
11231 if (tp->t_flags & TF_SENTFIN) {
11232 /* retran the end FIN? */
11233 return (NULL);
11234 }
11235 /* ok lets look at this one */
11236 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
11237 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
11238 goto check_it;
11239 }
11240 rsm = rack_find_lowest_rsm(rack);
11241 if (rsm == NULL) {
11242 return (NULL);
11243 }
11244 check_it:
11245 if (rsm->r_flags & RACK_ACKED) {
11246 return (NULL);
11247 }
11248 if (((rsm->r_flags & RACK_SACK_PASSED) == 0) &&
11249 (rsm->r_dupack < DUP_ACK_THRESHOLD)) {
11250 /* Its not yet ready */
11251 return (NULL);
11252 }
11253 srtt = rack_grab_rtt(tp, rack);
11254 idx = rsm->r_rtr_cnt - 1;
11255 ts_low = rsm->r_tim_lastsent[idx];
11256 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
11257 if ((tsused == ts_low) ||
11258 (TSTMP_LT(tsused, ts_low))) {
11259 /* No time since sending */
11260 return (NULL);
11261 }
11262 if ((tsused - ts_low) < thresh) {
11263 /* It has not been long enough yet */
11264 return (NULL);
11265 }
11266 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) ||
11267 ((rsm->r_flags & RACK_SACK_PASSED) &&
11268 (rack->sack_attack_disable == 0))) {
11269 /*
11270 * We have passed the dup-ack threshold <or>
11271 * a SACK has indicated this is missing.
11272 * Note that if you are a declared attacker
11273 * it is only the dup-ack threshold that
11274 * will cause retransmits.
11275 */
11276 /* log retransmit reason */
11277 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1);
11278 return (rsm);
11279 }
11280 return (NULL);
11281 }
11282
11283 static void
rack_log_pacing_delay_calc(struct tcp_rack * rack,uint32_t len,uint32_t slot,uint64_t bw_est,uint64_t bw,uint64_t len_time,int method,int line,struct rack_sendmap * rsm)11284 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot,
11285 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method,
11286 int line, struct rack_sendmap *rsm)
11287 {
11288 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
11289 union tcp_log_stackspecific log;
11290 struct timeval tv;
11291
11292 memset(&log, 0, sizeof(log));
11293 log.u_bbr.flex1 = slot;
11294 log.u_bbr.flex2 = len;
11295 log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs;
11296 log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs;
11297 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss;
11298 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca;
11299 log.u_bbr.use_lt_bw = rack->app_limited_needs_set;
11300 log.u_bbr.use_lt_bw <<= 1;
11301 log.u_bbr.use_lt_bw = rack->rc_gp_filled;
11302 log.u_bbr.use_lt_bw <<= 1;
11303 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt;
11304 log.u_bbr.use_lt_bw <<= 1;
11305 log.u_bbr.use_lt_bw |= rack->in_probe_rtt;
11306 log.u_bbr.pkt_epoch = line;
11307 log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec;
11308 log.u_bbr.bw_inuse = bw_est;
11309 log.u_bbr.delRate = bw;
11310 if (rack->r_ctl.gp_bw == 0)
11311 log.u_bbr.cur_del_rate = 0;
11312 else
11313 log.u_bbr.cur_del_rate = rack_get_bw(rack);
11314 log.u_bbr.rttProp = len_time;
11315 log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt;
11316 log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit;
11317 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
11318 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) {
11319 /* We are in slow start */
11320 log.u_bbr.flex7 = 1;
11321 } else {
11322 /* we are on congestion avoidance */
11323 log.u_bbr.flex7 = 0;
11324 }
11325 log.u_bbr.flex8 = method;
11326 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11327 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
11328 log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec;
11329 log.u_bbr.cwnd_gain <<= 1;
11330 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss;
11331 log.u_bbr.cwnd_gain <<= 1;
11332 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca;
11333 TCP_LOG_EVENTP(rack->rc_tp, NULL,
11334 &rack->rc_inp->inp_socket->so_rcv,
11335 &rack->rc_inp->inp_socket->so_snd,
11336 BBR_LOG_HPTSI_CALC, 0,
11337 0, &log, false, &tv);
11338 }
11339 }
11340
11341 static uint32_t
rack_get_pacing_len(struct tcp_rack * rack,uint64_t bw,uint32_t mss)11342 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss)
11343 {
11344 uint32_t new_tso, user_max;
11345
11346 user_max = rack->rc_user_set_max_segs * mss;
11347 if (rack->rc_force_max_seg) {
11348 return (user_max);
11349 }
11350 if (rack->use_fixed_rate &&
11351 ((rack->r_ctl.crte == NULL) ||
11352 (bw != rack->r_ctl.crte->rate))) {
11353 /* Use the user mss since we are not exactly matched */
11354 return (user_max);
11355 }
11356 new_tso = tcp_get_pacing_burst_size(bw, mss, rack_pace_one_seg, rack->r_ctl.crte, NULL);
11357 if (new_tso > user_max)
11358 new_tso = user_max;
11359 return(new_tso);
11360 }
11361
11362 static void
rack_log_hdwr_pacing(struct tcp_rack * rack,const struct ifnet * ifp,uint64_t rate,uint64_t hw_rate,int line,int error)11363 rack_log_hdwr_pacing(struct tcp_rack *rack, const struct ifnet *ifp,
11364 uint64_t rate, uint64_t hw_rate, int line,
11365 int error)
11366 {
11367 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
11368 union tcp_log_stackspecific log;
11369 struct timeval tv;
11370
11371 memset(&log, 0, sizeof(log));
11372 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff);
11373 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff);
11374 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff);
11375 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff);
11376 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
11377 log.u_bbr.bw_inuse = rate;
11378 log.u_bbr.flex5 = line;
11379 log.u_bbr.flex6 = error;
11380 log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs;
11381 log.u_bbr.flex8 = rack->use_fixed_rate;
11382 log.u_bbr.flex8 <<= 1;
11383 log.u_bbr.flex8 |= rack->rack_hdrw_pacing;
11384 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg;
11385 TCP_LOG_EVENTP(rack->rc_tp, NULL,
11386 &rack->rc_inp->inp_socket->so_rcv,
11387 &rack->rc_inp->inp_socket->so_snd,
11388 BBR_LOG_HDWR_PACE, 0,
11389 0, &log, false, &tv);
11390 }
11391 }
11392
11393 static int32_t
pace_to_fill_cwnd(struct tcp_rack * rack,int32_t slot,uint32_t len,uint32_t segsiz)11394 pace_to_fill_cwnd(struct tcp_rack *rack, int32_t slot, uint32_t len, uint32_t segsiz)
11395 {
11396 uint64_t lentim, fill_bw;
11397
11398 /* Lets first see if we are full, if so continue with normal rate */
11399 if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use)
11400 return (slot);
11401 if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd)
11402 return (slot);
11403 if (rack->r_ctl.rc_last_us_rtt == 0)
11404 return (slot);
11405 if (rack->rc_pace_fill_if_rttin_range &&
11406 (rack->r_ctl.rc_last_us_rtt >=
11407 (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) {
11408 /* The rtt is huge, N * smallest, lets not fill */
11409 return (slot);
11410 }
11411 /*
11412 * first lets calculate the b/w based on the last us-rtt
11413 * and the sndwnd.
11414 */
11415 fill_bw = rack->r_ctl.cwnd_to_use;
11416 /* Take the rwnd if its smaller */
11417 if (fill_bw > rack->rc_tp->snd_wnd)
11418 fill_bw = rack->rc_tp->snd_wnd;
11419 fill_bw *= (uint64_t)HPTS_USEC_IN_SEC;
11420 fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt;
11421 /* We are below the min b/w */
11422 if (fill_bw < RACK_MIN_BW)
11423 return (slot);
11424 /*
11425 * Ok fill_bw holds our mythical b/w to fill the cwnd
11426 * in a rtt, what does that time wise equate too?
11427 */
11428 lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC;
11429 lentim /= fill_bw;
11430 if (lentim < slot) {
11431 rack_log_pacing_delay_calc(rack, len, slot, fill_bw,
11432 0, lentim, 12, __LINE__, NULL);
11433 return ((int32_t)lentim);
11434 } else
11435 return (slot);
11436 }
11437
11438 static int32_t
rack_get_pacing_delay(struct tcp_rack * rack,struct tcpcb * tp,uint32_t len,struct rack_sendmap * rsm,uint32_t segsiz)11439 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz)
11440 {
11441 struct rack_sendmap *lrsm;
11442 int32_t slot = 0;
11443 int err;
11444
11445 if (rack->rc_always_pace == 0) {
11446 /*
11447 * We use the most optimistic possible cwnd/srtt for
11448 * sending calculations. This will make our
11449 * calculation anticipate getting more through
11450 * quicker then possible. But thats ok we don't want
11451 * the peer to have a gap in data sending.
11452 */
11453 uint32_t srtt, cwnd, tr_perms = 0;
11454 int32_t reduce = 0;
11455
11456 old_method:
11457 /*
11458 * We keep no precise pacing with the old method
11459 * instead we use the pacer to mitigate bursts.
11460 */
11461 rack->r_ctl.rc_agg_delayed = 0;
11462 rack->r_early = 0;
11463 rack->r_late = 0;
11464 rack->r_ctl.rc_agg_early = 0;
11465 if (rack->r_ctl.rc_rack_min_rtt)
11466 srtt = rack->r_ctl.rc_rack_min_rtt;
11467 else
11468 srtt = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT));
11469 if (rack->r_ctl.rc_rack_largest_cwnd)
11470 cwnd = rack->r_ctl.rc_rack_largest_cwnd;
11471 else
11472 cwnd = rack->r_ctl.cwnd_to_use;
11473 tr_perms = cwnd / srtt;
11474 if (tr_perms == 0) {
11475 tr_perms = ctf_fixed_maxseg(tp);
11476 }
11477 /*
11478 * Calculate how long this will take to drain, if
11479 * the calculation comes out to zero, thats ok we
11480 * will use send_a_lot to possibly spin around for
11481 * more increasing tot_len_this_send to the point
11482 * that its going to require a pace, or we hit the
11483 * cwnd. Which in that case we are just waiting for
11484 * a ACK.
11485 */
11486 slot = len / tr_perms;
11487 /* Now do we reduce the time so we don't run dry? */
11488 if (slot && rack_slot_reduction) {
11489 reduce = (slot / rack_slot_reduction);
11490 if (reduce < slot) {
11491 slot -= reduce;
11492 } else
11493 slot = 0;
11494 }
11495 slot *= HPTS_USEC_IN_MSEC;
11496 if (rsm == NULL) {
11497 /*
11498 * We always consider ourselves app limited with old style
11499 * that are not retransmits. This could be the initial
11500 * measurement, but thats ok its all setup and specially
11501 * handled. If another send leaks out, then that too will
11502 * be mark app-limited.
11503 */
11504 lrsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
11505 if (lrsm && ((lrsm->r_flags & RACK_APP_LIMITED) == 0)) {
11506 rack->r_ctl.rc_first_appl = lrsm;
11507 lrsm->r_flags |= RACK_APP_LIMITED;
11508 rack->r_ctl.rc_app_limited_cnt++;
11509 }
11510 }
11511 rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL);
11512 } else {
11513 uint64_t bw_est, res, lentim, rate_wanted;
11514 uint32_t orig_val, srtt, segs, oh;
11515
11516 if ((rack->r_rr_config == 1) && rsm) {
11517 return (rack->r_ctl.rc_min_to * HPTS_USEC_IN_MSEC);
11518 }
11519 if (rack->use_fixed_rate) {
11520 rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack);
11521 } else if ((rack->r_ctl.init_rate == 0) &&
11522 #ifdef NETFLIX_PEAKRATE
11523 (rack->rc_tp->t_maxpeakrate == 0) &&
11524 #endif
11525 (rack->r_ctl.gp_bw == 0)) {
11526 /* no way to yet do an estimate */
11527 bw_est = rate_wanted = 0;
11528 } else {
11529 bw_est = rack_get_bw(rack);
11530 rate_wanted = rack_get_output_bw(rack, bw_est, rsm);
11531 }
11532 if ((bw_est == 0) || (rate_wanted == 0)) {
11533 /*
11534 * No way yet to make a b/w estimate or
11535 * our raise is set incorrectly.
11536 */
11537 goto old_method;
11538 }
11539 /* We need to account for all the overheads */
11540 segs = (len + segsiz - 1) / segsiz;
11541 /*
11542 * We need the diff between 1514 bytes (e-mtu with e-hdr)
11543 * and how much data we put in each packet. Yes this
11544 * means we may be off if we are larger than 1500 bytes
11545 * or smaller. But this just makes us more conservative.
11546 */
11547 if (ETHERNET_SEGMENT_SIZE > segsiz)
11548 oh = ETHERNET_SEGMENT_SIZE - segsiz;
11549 else
11550 oh = 0;
11551 segs *= oh;
11552 lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC;
11553 res = lentim / rate_wanted;
11554 slot = (uint32_t)res;
11555 orig_val = rack->r_ctl.rc_pace_max_segs;
11556 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
11557 /* Did we change the TSO size, if so log it */
11558 if (rack->r_ctl.rc_pace_max_segs != orig_val)
11559 rack_log_pacing_delay_calc(rack, len, slot, orig_val, 0, 0, 15, __LINE__, NULL);
11560 if ((rack->rc_pace_to_cwnd) &&
11561 (rack->in_probe_rtt == 0) &&
11562 (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) {
11563 /*
11564 * We want to pace at our rate *or* faster to
11565 * fill the cwnd to the max if its not full.
11566 */
11567 slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz);
11568 }
11569 if ((rack->rc_inp->inp_route.ro_nh != NULL) &&
11570 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) {
11571 if ((rack->rack_hdw_pace_ena) &&
11572 (rack->rack_hdrw_pacing == 0) &&
11573 (rack->rack_attempt_hdwr_pace == 0)) {
11574 /*
11575 * Lets attempt to turn on hardware pacing
11576 * if we can.
11577 */
11578 rack->rack_attempt_hdwr_pace = 1;
11579 rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp,
11580 rack->rc_inp->inp_route.ro_nh->nh_ifp,
11581 rate_wanted,
11582 RS_PACING_GEQ,
11583 &err);
11584 if (rack->r_ctl.crte) {
11585 rack->rack_hdrw_pacing = 1;
11586 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(rate_wanted, segsiz,
11587 0, rack->r_ctl.crte,
11588 NULL);
11589 rack_log_hdwr_pacing(rack, rack->rc_inp->inp_route.ro_nh->nh_ifp,
11590 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
11591 err);
11592 }
11593 } else if (rack->rack_hdrw_pacing &&
11594 (rack->r_ctl.crte->rate != rate_wanted)) {
11595 /* Do we need to adjust our rate? */
11596 const struct tcp_hwrate_limit_table *nrte;
11597
11598 nrte = tcp_chg_pacing_rate(rack->r_ctl.crte,
11599 rack->rc_tp,
11600 rack->rc_inp->inp_route.ro_nh->nh_ifp,
11601 rate_wanted,
11602 RS_PACING_GEQ,
11603 &err);
11604 if (nrte == NULL) {
11605 /* Lost the rate */
11606 rack->rack_hdrw_pacing = 0;
11607 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
11608 } else if (nrte != rack->r_ctl.crte) {
11609 rack->r_ctl.crte = nrte;
11610 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(rate_wanted,
11611 segsiz, 0,
11612 rack->r_ctl.crte,
11613 NULL);
11614 rack_log_hdwr_pacing(rack, rack->rc_inp->inp_route.ro_nh->nh_ifp,
11615 rate_wanted, rack->r_ctl.crte->rate, __LINE__,
11616 err);
11617 }
11618 }
11619 }
11620 if (rack_limit_time_with_srtt &&
11621 (rack->use_fixed_rate == 0) &&
11622 #ifdef NETFLIX_PEAKRATE
11623 (rack->rc_tp->t_maxpeakrate == 0) &&
11624 #endif
11625 (rack->rack_hdrw_pacing == 0)) {
11626 /*
11627 * Sanity check, we do not allow the pacing delay
11628 * to be longer than the SRTT of the path. If it is
11629 * a slow path, then adding a packet should increase
11630 * the RTT and compensate for this i.e. the srtt will
11631 * be greater so the allowed pacing time will be greater.
11632 *
11633 * Note this restriction is not for where a peak rate
11634 * is set, we are doing fixed pacing or hardware pacing.
11635 */
11636 if (rack->rc_tp->t_srtt)
11637 srtt = (TICKS_2_USEC(rack->rc_tp->t_srtt) >> TCP_RTT_SHIFT);
11638 else
11639 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC; /* its in ms convert */
11640 if (srtt < slot) {
11641 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL);
11642 slot = srtt;
11643 }
11644 }
11645 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm);
11646 }
11647 if (slot)
11648 counter_u64_add(rack_calc_nonzero, 1);
11649 else
11650 counter_u64_add(rack_calc_zero, 1);
11651 return (slot);
11652 }
11653
11654 static void
rack_start_gp_measurement(struct tcpcb * tp,struct tcp_rack * rack,tcp_seq startseq,uint32_t sb_offset)11655 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack,
11656 tcp_seq startseq, uint32_t sb_offset)
11657 {
11658 struct rack_sendmap *my_rsm = NULL;
11659 struct rack_sendmap fe;
11660
11661 if (tp->t_state < TCPS_ESTABLISHED) {
11662 /*
11663 * We don't start any measurements if we are
11664 * not at least established.
11665 */
11666 return;
11667 }
11668 tp->t_flags |= TF_GPUTINPROG;
11669 rack->r_ctl.rc_gp_lowrtt = 0xffffffff;
11670 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd;
11671 tp->gput_seq = startseq;
11672 rack->app_limited_needs_set = 0;
11673 if (rack->in_probe_rtt)
11674 rack->measure_saw_probe_rtt = 1;
11675 else if ((rack->measure_saw_probe_rtt) &&
11676 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit)))
11677 rack->measure_saw_probe_rtt = 0;
11678 if (rack->rc_gp_filled)
11679 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time);
11680 else {
11681 /* Special case initial measurement */
11682 rack->r_ctl.rc_gp_output_ts = tp->gput_ts = tcp_get_usecs(NULL);
11683 }
11684 /*
11685 * We take a guess out into the future,
11686 * if we have no measurement and no
11687 * initial rate, we measure the first
11688 * initial-windows worth of data to
11689 * speed up getting some GP measurement and
11690 * thus start pacing.
11691 */
11692 if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) {
11693 rack->app_limited_needs_set = 1;
11694 tp->gput_ack = startseq + max(rc_init_window(rack),
11695 (MIN_GP_WIN * ctf_fixed_maxseg(tp)));
11696 rack_log_pacing_delay_calc(rack,
11697 tp->gput_seq,
11698 tp->gput_ack,
11699 0,
11700 tp->gput_ts,
11701 rack->r_ctl.rc_app_limited_cnt,
11702 9,
11703 __LINE__, NULL);
11704 return;
11705 }
11706 if (sb_offset) {
11707 /*
11708 * We are out somewhere in the sb
11709 * can we use the already outstanding data?
11710 */
11711
11712 if (rack->r_ctl.rc_app_limited_cnt == 0) {
11713 /*
11714 * Yes first one is good and in this case
11715 * the tp->gput_ts is correctly set based on
11716 * the last ack that arrived (no need to
11717 * set things up when an ack comes in).
11718 */
11719 my_rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
11720 if ((my_rsm == NULL) ||
11721 (my_rsm->r_rtr_cnt != 1)) {
11722 /* retransmission? */
11723 goto use_latest;
11724 }
11725 } else {
11726 if (rack->r_ctl.rc_first_appl == NULL) {
11727 /*
11728 * If rc_first_appl is NULL
11729 * then the cnt should be 0.
11730 * This is probably an error, maybe
11731 * a KASSERT would be approprate.
11732 */
11733 goto use_latest;
11734 }
11735 /*
11736 * If we have a marker pointer to the last one that is
11737 * app limited we can use that, but we need to set
11738 * things up so that when it gets ack'ed we record
11739 * the ack time (if its not already acked).
11740 */
11741 rack->app_limited_needs_set = 1;
11742 /*
11743 * We want to get to the rsm that is either
11744 * next with space i.e. over 1 MSS or the one
11745 * after that (after the app-limited).
11746 */
11747 my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
11748 rack->r_ctl.rc_first_appl);
11749 if (my_rsm) {
11750 if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp))
11751 /* Have to use the next one */
11752 my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree,
11753 my_rsm);
11754 else {
11755 /* Use after the first MSS of it is acked */
11756 tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp);
11757 goto start_set;
11758 }
11759 }
11760 if ((my_rsm == NULL) ||
11761 (my_rsm->r_rtr_cnt != 1)) {
11762 /*
11763 * Either its a retransmit or
11764 * the last is the app-limited one.
11765 */
11766 goto use_latest;
11767 }
11768 }
11769 tp->gput_seq = my_rsm->r_start;
11770 start_set:
11771 if (my_rsm->r_flags & RACK_ACKED) {
11772 /*
11773 * This one has been acked use the arrival ack time
11774 */
11775 tp->gput_ts = my_rsm->r_ack_arrival;
11776 rack->app_limited_needs_set = 0;
11777 }
11778 rack->r_ctl.rc_gp_output_ts = my_rsm->usec_orig_send;
11779 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack);
11780 rack_log_pacing_delay_calc(rack,
11781 tp->gput_seq,
11782 tp->gput_ack,
11783 (uint64_t)my_rsm,
11784 tp->gput_ts,
11785 rack->r_ctl.rc_app_limited_cnt,
11786 9,
11787 __LINE__, NULL);
11788 return;
11789 }
11790
11791 use_latest:
11792 /*
11793 * We don't know how long we may have been
11794 * idle or if this is the first-send. Lets
11795 * setup the flag so we will trim off
11796 * the first ack'd data so we get a true
11797 * measurement.
11798 */
11799 rack->app_limited_needs_set = 1;
11800 tp->gput_ack = startseq + rack_get_measure_window(tp, rack);
11801 /* Find this guy so we can pull the send time */
11802 fe.r_start = startseq;
11803 my_rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe);
11804 if (my_rsm) {
11805 rack->r_ctl.rc_gp_output_ts = my_rsm->usec_orig_send;
11806 if (my_rsm->r_flags & RACK_ACKED) {
11807 /*
11808 * Unlikely since its probably what was
11809 * just transmitted (but I am paranoid).
11810 */
11811 tp->gput_ts = my_rsm->r_ack_arrival;
11812 rack->app_limited_needs_set = 0;
11813 }
11814 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) {
11815 /* This also is unlikely */
11816 tp->gput_seq = my_rsm->r_start;
11817 }
11818 } else {
11819 /*
11820 * TSNH unless we have some send-map limit,
11821 * and even at that it should not be hitting
11822 * that limit (we should have stopped sending).
11823 */
11824 rack->r_ctl.rc_gp_output_ts = tcp_get_usecs(NULL);
11825 }
11826 rack_log_pacing_delay_calc(rack,
11827 tp->gput_seq,
11828 tp->gput_ack,
11829 (uint64_t)my_rsm,
11830 tp->gput_ts,
11831 rack->r_ctl.rc_app_limited_cnt,
11832 9, __LINE__, NULL);
11833 }
11834
11835 static inline uint32_t
rack_what_can_we_send(struct tcpcb * tp,struct tcp_rack * rack,uint32_t cwnd_to_use,uint32_t avail,int32_t sb_offset)11836 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cwnd_to_use,
11837 uint32_t avail, int32_t sb_offset)
11838 {
11839 uint32_t len;
11840 uint32_t sendwin;
11841
11842 if (tp->snd_wnd > cwnd_to_use)
11843 sendwin = cwnd_to_use;
11844 else
11845 sendwin = tp->snd_wnd;
11846 if (ctf_outstanding(tp) >= tp->snd_wnd) {
11847 /* We never want to go over our peers rcv-window */
11848 len = 0;
11849 } else {
11850 uint32_t flight;
11851
11852 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked);
11853 if (flight >= sendwin) {
11854 /*
11855 * We have in flight what we are allowed by cwnd (if
11856 * it was rwnd blocking it would have hit above out
11857 * >= tp->snd_wnd).
11858 */
11859 return (0);
11860 }
11861 len = sendwin - flight;
11862 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) {
11863 /* We would send too much (beyond the rwnd) */
11864 len = tp->snd_wnd - ctf_outstanding(tp);
11865 }
11866 if ((len + sb_offset) > avail) {
11867 /*
11868 * We don't have that much in the SB, how much is
11869 * there?
11870 */
11871 len = avail - sb_offset;
11872 }
11873 }
11874 return (len);
11875 }
11876
11877 static int
rack_output(struct tcpcb * tp)11878 rack_output(struct tcpcb *tp)
11879 {
11880 struct socket *so;
11881 uint32_t recwin;
11882 uint32_t sb_offset;
11883 int32_t len, flags, error = 0;
11884 struct mbuf *m;
11885 struct mbuf *mb;
11886 uint32_t if_hw_tsomaxsegcount = 0;
11887 uint32_t if_hw_tsomaxsegsize;
11888 int32_t segsiz, minseg;
11889 long tot_len_this_send = 0;
11890 struct ip *ip = NULL;
11891 #ifdef TCPDEBUG
11892 struct ipovly *ipov = NULL;
11893 #endif
11894 struct udphdr *udp = NULL;
11895 struct tcp_rack *rack;
11896 struct tcphdr *th;
11897 uint8_t pass = 0;
11898 uint8_t mark = 0;
11899 uint8_t wanted_cookie = 0;
11900 u_char opt[TCP_MAXOLEN];
11901 unsigned ipoptlen, optlen, hdrlen, ulen=0;
11902 uint32_t rack_seq;
11903
11904 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
11905 unsigned ipsec_optlen = 0;
11906
11907 #endif
11908 int32_t idle, sendalot;
11909 int32_t sub_from_prr = 0;
11910 volatile int32_t sack_rxmit;
11911 struct rack_sendmap *rsm = NULL;
11912 int32_t tso, mtu;
11913 struct tcpopt to;
11914 int32_t slot = 0;
11915 int32_t sup_rack = 0;
11916 uint32_t cts, us_cts, delayed, early;
11917 uint8_t hpts_calling, new_data_tlp = 0, doing_tlp = 0;
11918 uint32_t cwnd_to_use;
11919 int32_t do_a_prefetch;
11920 int32_t prefetch_rsm = 0;
11921 int32_t orig_len;
11922 struct timeval tv;
11923 int32_t prefetch_so_done = 0;
11924 struct tcp_log_buffer *lgb = NULL;
11925 struct inpcb *inp;
11926 struct sockbuf *sb;
11927 #ifdef INET6
11928 struct ip6_hdr *ip6 = NULL;
11929 int32_t isipv6;
11930 #endif
11931 uint8_t filled_all = 0;
11932 bool hw_tls = false;
11933
11934 /* setup and take the cache hits here */
11935 rack = (struct tcp_rack *)tp->t_fb_ptr;
11936 inp = rack->rc_inp;
11937 so = inp->inp_socket;
11938 sb = &so->so_snd;
11939 kern_prefetch(sb, &do_a_prefetch);
11940 do_a_prefetch = 1;
11941 hpts_calling = inp->inp_hpts_calls;
11942 hw_tls = (so->so_snd.sb_flags & SB_TLS_IFNET) != 0;
11943
11944 NET_EPOCH_ASSERT();
11945 INP_WLOCK_ASSERT(inp);
11946 #ifdef TCP_OFFLOAD
11947 if (tp->t_flags & TF_TOE)
11948 return (tcp_offload_output(tp));
11949 #endif
11950 /*
11951 * For TFO connections in SYN_RECEIVED, only allow the initial
11952 * SYN|ACK and those sent by the retransmit timer.
11953 */
11954 if (IS_FASTOPEN(tp->t_flags) &&
11955 (tp->t_state == TCPS_SYN_RECEIVED) &&
11956 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN|ACK sent */
11957 (rack->r_ctl.rc_resend == NULL)) /* not a retransmit */
11958 return (0);
11959 #ifdef INET6
11960 if (rack->r_state) {
11961 /* Use the cache line loaded if possible */
11962 isipv6 = rack->r_is_v6;
11963 } else {
11964 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
11965 }
11966 #endif
11967 early = 0;
11968 us_cts = tcp_get_usecs(&tv);
11969 cts = tcp_tv_to_mssectick(&tv);
11970 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
11971 inp->inp_in_hpts) {
11972 /*
11973 * We are on the hpts for some timer but not hptsi output.
11974 * Remove from the hpts unconditionally.
11975 */
11976 rack_timer_cancel(tp, rack, cts, __LINE__);
11977 }
11978 /* Are we pacing and late? */
11979 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
11980 TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) {
11981 /* We are delayed */
11982 delayed = us_cts - rack->r_ctl.rc_last_output_to;
11983 } else {
11984 delayed = 0;
11985 }
11986 /* Do the timers, which may override the pacer */
11987 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
11988 if (rack_process_timers(tp, rack, cts, hpts_calling)) {
11989 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
11990 return (0);
11991 }
11992 }
11993 if ((rack->r_timer_override) ||
11994 (delayed) ||
11995 (tp->t_state < TCPS_ESTABLISHED)) {
11996 if (tp->t_inpcb->inp_in_hpts)
11997 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
11998 } else if (tp->t_inpcb->inp_in_hpts) {
11999 /*
12000 * On the hpts you can't pass even if ACKNOW is on, we will
12001 * when the hpts fires.
12002 */
12003 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
12004 return (0);
12005 }
12006 inp->inp_hpts_calls = 0;
12007 /* Finish out both pacing early and late accounting */
12008 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
12009 TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) {
12010 early = rack->r_ctl.rc_last_output_to - us_cts;
12011 } else
12012 early = 0;
12013 if (delayed) {
12014 rack->r_ctl.rc_agg_delayed += delayed;
12015 rack->r_late = 1;
12016 } else if (early) {
12017 rack->r_ctl.rc_agg_early += early;
12018 rack->r_early = 1;
12019 }
12020 /* Now that early/late accounting is done turn off the flag */
12021 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT;
12022 rack->r_wanted_output = 0;
12023 rack->r_timer_override = 0;
12024 /*
12025 * For TFO connections in SYN_SENT or SYN_RECEIVED,
12026 * only allow the initial SYN or SYN|ACK and those sent
12027 * by the retransmit timer.
12028 */
12029 if (IS_FASTOPEN(tp->t_flags) &&
12030 ((tp->t_state == TCPS_SYN_RECEIVED) ||
12031 (tp->t_state == TCPS_SYN_SENT)) &&
12032 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
12033 (tp->t_rxtshift == 0)) { /* not a retransmit */
12034 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
12035 goto just_return_nolock;
12036 }
12037 /*
12038 * Determine length of data that should be transmitted, and flags
12039 * that will be used. If there is some data or critical controls
12040 * (SYN, RST) to send, then transmit; otherwise, investigate
12041 * further.
12042 */
12043 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
12044 if (tp->t_idle_reduce) {
12045 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
12046 rack_cc_after_idle(rack, tp);
12047 }
12048 tp->t_flags &= ~TF_LASTIDLE;
12049 if (idle) {
12050 if (tp->t_flags & TF_MORETOCOME) {
12051 tp->t_flags |= TF_LASTIDLE;
12052 idle = 0;
12053 }
12054 }
12055 if ((tp->snd_una == tp->snd_max) &&
12056 rack->r_ctl.rc_went_idle_time &&
12057 TSTMP_GT(us_cts, rack->r_ctl.rc_went_idle_time)) {
12058 idle = us_cts - rack->r_ctl.rc_went_idle_time;
12059 if (idle > rack_min_probertt_hold) {
12060 /* Count as a probe rtt */
12061 if (rack->in_probe_rtt == 0) {
12062 rack->r_ctl.rc_lower_rtt_us_cts = us_cts;
12063 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts;
12064 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts;
12065 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts;
12066 } else {
12067 rack_exit_probertt(rack, us_cts);
12068 }
12069 }
12070 idle = 0;
12071 }
12072 again:
12073 /*
12074 * If we've recently taken a timeout, snd_max will be greater than
12075 * snd_nxt. There may be SACK information that allows us to avoid
12076 * resending already delivered data. Adjust snd_nxt accordingly.
12077 */
12078 sendalot = 0;
12079 us_cts = tcp_get_usecs(&tv);
12080 cts = tcp_tv_to_mssectick(&tv);
12081 tso = 0;
12082 mtu = 0;
12083 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs);
12084 minseg = segsiz;
12085 sb_offset = tp->snd_max - tp->snd_una;
12086 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd;
12087 #ifdef NETFLIX_SHARED_CWND
12088 if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) &&
12089 rack->rack_enable_scwnd) {
12090 /* We are doing cwnd sharing */
12091 if (rack->rc_gp_filled &&
12092 (rack->rack_attempted_scwnd == 0) &&
12093 (rack->r_ctl.rc_scw == NULL) &&
12094 tp->t_lib) {
12095 /* The pcbid is in, lets make an attempt */
12096 counter_u64_add(rack_try_scwnd, 1);
12097 rack->rack_attempted_scwnd = 1;
12098 rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp,
12099 &rack->r_ctl.rc_scw_index,
12100 segsiz);
12101 }
12102 if (rack->r_ctl.rc_scw &&
12103 (rack->rack_scwnd_is_idle == 1) &&
12104 (rack->rc_in_persist == 0) &&
12105 sbavail(sb)) {
12106 /* we are no longer out of data */
12107 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
12108 rack->rack_scwnd_is_idle = 0;
12109 }
12110 if (rack->r_ctl.rc_scw) {
12111 /* First lets update and get the cwnd */
12112 rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw,
12113 rack->r_ctl.rc_scw_index,
12114 tp->snd_cwnd, tp->snd_wnd, segsiz);
12115 }
12116 }
12117 #endif
12118 flags = tcp_outflags[tp->t_state];
12119 while (rack->rc_free_cnt < rack_free_cache) {
12120 rsm = rack_alloc(rack);
12121 if (rsm == NULL) {
12122 if (inp->inp_hpts_calls)
12123 /* Retry in a ms */
12124 slot = (1 * HPTS_USEC_IN_MSEC);
12125 goto just_return_nolock;
12126 }
12127 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext);
12128 rack->rc_free_cnt++;
12129 rsm = NULL;
12130 }
12131 if (inp->inp_hpts_calls)
12132 inp->inp_hpts_calls = 0;
12133 sack_rxmit = 0;
12134 len = 0;
12135 rsm = NULL;
12136 if (flags & TH_RST) {
12137 SOCKBUF_LOCK(sb);
12138 goto send;
12139 }
12140 if (rack->r_ctl.rc_resend) {
12141 /* Retransmit timer */
12142 rsm = rack->r_ctl.rc_resend;
12143 rack->r_ctl.rc_resend = NULL;
12144 rsm->r_flags &= ~RACK_TLP;
12145 len = rsm->r_end - rsm->r_start;
12146 sack_rxmit = 1;
12147 sendalot = 0;
12148 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
12149 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
12150 __func__, __LINE__,
12151 rsm->r_start, tp->snd_una, tp, rack, rsm));
12152 sb_offset = rsm->r_start - tp->snd_una;
12153 if (len >= segsiz)
12154 len = segsiz;
12155 } else if ((rack->rc_in_persist == 0) &&
12156 ((rsm = tcp_rack_output(tp, rack, cts)) != NULL)) {
12157 /* We have a retransmit that takes precedence */
12158 rsm->r_flags &= ~RACK_TLP;
12159 if ((!IN_RECOVERY(tp->t_flags)) &&
12160 ((tp->t_flags & (TF_WASFRECOVERY | TF_WASCRECOVERY)) == 0)) {
12161 /* Enter recovery if not induced by a time-out */
12162 rack->r_ctl.rc_rsm_start = rsm->r_start;
12163 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
12164 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
12165 rack_cong_signal(tp, NULL, CC_NDUPACK);
12166 /*
12167 * When we enter recovery we need to assure we send
12168 * one packet.
12169 */
12170 if (rack->rack_no_prr == 0) {
12171 rack->r_ctl.rc_prr_sndcnt = segsiz;
12172 rack_log_to_prr(rack, 13, 0);
12173 }
12174 }
12175 #ifdef INVARIANTS
12176 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
12177 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
12178 tp, rack, rsm, rsm->r_start, tp->snd_una);
12179 }
12180 #endif
12181 len = rsm->r_end - rsm->r_start;
12182 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
12183 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
12184 __func__, __LINE__,
12185 rsm->r_start, tp->snd_una, tp, rack, rsm));
12186 sb_offset = rsm->r_start - tp->snd_una;
12187 /* Can we send it within the PRR boundary? */
12188 if (rack->rack_no_prr == 0) {
12189 if ((rack->use_rack_rr == 0) && (len > rack->r_ctl.rc_prr_sndcnt)) {
12190 /* It does not fit */
12191 if ((ctf_flight_size(tp, rack->r_ctl.rc_sacked) > len) &&
12192 (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
12193 /*
12194 * prr is less than a segment, we
12195 * have more acks due in besides
12196 * what we need to resend. Lets not send
12197 * to avoid sending small pieces of
12198 * what we need to retransmit.
12199 */
12200 len = 0;
12201 goto just_return_nolock;
12202 }
12203 len = rack->r_ctl.rc_prr_sndcnt;
12204 }
12205 }
12206 sendalot = 0;
12207 if (len >= segsiz)
12208 len = segsiz;
12209 if (len > 0) {
12210 sub_from_prr = 1;
12211 sack_rxmit = 1;
12212 KMOD_TCPSTAT_INC(tcps_sack_rexmits);
12213 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes,
12214 min(len, segsiz));
12215 counter_u64_add(rack_rtm_prr_retran, 1);
12216 }
12217 } else if (rack->r_ctl.rc_tlpsend) {
12218 /* Tail loss probe */
12219 long cwin;
12220 long tlen;
12221
12222 doing_tlp = 1;
12223 /*
12224 * Check if we can do a TLP with a RACK'd packet
12225 * this can happen if we are not doing the rack
12226 * cheat and we skipped to a TLP and it
12227 * went off.
12228 */
12229 rsm = rack->r_ctl.rc_tlpsend;
12230 rsm->r_flags |= RACK_TLP;
12231 rack->r_ctl.rc_tlpsend = NULL;
12232 sack_rxmit = 1;
12233 tlen = rsm->r_end - rsm->r_start;
12234 if (tlen > segsiz)
12235 tlen = segsiz;
12236 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
12237 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
12238 __func__, __LINE__,
12239 rsm->r_start, tp->snd_una, tp, rack, rsm));
12240 sb_offset = rsm->r_start - tp->snd_una;
12241 cwin = min(tp->snd_wnd, tlen);
12242 len = cwin;
12243 }
12244 /*
12245 * Enforce a connection sendmap count limit if set
12246 * as long as we are not retransmiting.
12247 */
12248 if ((rsm == NULL) &&
12249 (rack->do_detection == 0) &&
12250 (V_tcp_map_entries_limit > 0) &&
12251 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) {
12252 counter_u64_add(rack_to_alloc_limited, 1);
12253 if (!rack->alloc_limit_reported) {
12254 rack->alloc_limit_reported = 1;
12255 counter_u64_add(rack_alloc_limited_conns, 1);
12256 }
12257 goto just_return_nolock;
12258 }
12259 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
12260 /* we are retransmitting the fin */
12261 len--;
12262 if (len) {
12263 /*
12264 * When retransmitting data do *not* include the
12265 * FIN. This could happen from a TLP probe.
12266 */
12267 flags &= ~TH_FIN;
12268 }
12269 }
12270 #ifdef INVARIANTS
12271 /* For debugging */
12272 rack->r_ctl.rc_rsm_at_retran = rsm;
12273 #endif
12274 /*
12275 * Get standard flags, and add SYN or FIN if requested by 'hidden'
12276 * state flags.
12277 */
12278 if (tp->t_flags & TF_NEEDFIN)
12279 flags |= TH_FIN;
12280 if (tp->t_flags & TF_NEEDSYN)
12281 flags |= TH_SYN;
12282 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
12283 void *end_rsm;
12284 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
12285 if (end_rsm)
12286 kern_prefetch(end_rsm, &prefetch_rsm);
12287 prefetch_rsm = 1;
12288 }
12289 SOCKBUF_LOCK(sb);
12290 /*
12291 * If snd_nxt == snd_max and we have transmitted a FIN, the
12292 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
12293 * negative length. This can also occur when TCP opens up its
12294 * congestion window while receiving additional duplicate acks after
12295 * fast-retransmit because TCP will reset snd_nxt to snd_max after
12296 * the fast-retransmit.
12297 *
12298 * In the normal retransmit-FIN-only case, however, snd_nxt will be
12299 * set to snd_una, the sb_offset will be 0, and the length may wind
12300 * up 0.
12301 *
12302 * If sack_rxmit is true we are retransmitting from the scoreboard
12303 * in which case len is already set.
12304 */
12305 if ((sack_rxmit == 0) &&
12306 (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) {
12307 uint32_t avail;
12308
12309 avail = sbavail(sb);
12310 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
12311 sb_offset = tp->snd_nxt - tp->snd_una;
12312 else
12313 sb_offset = 0;
12314 if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) {
12315 if (rack->r_ctl.rc_tlp_new_data) {
12316 /* TLP is forcing out new data */
12317 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
12318 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
12319 }
12320 if (rack->r_ctl.rc_tlp_new_data > tp->snd_wnd)
12321 len = tp->snd_wnd;
12322 else
12323 len = rack->r_ctl.rc_tlp_new_data;
12324 rack->r_ctl.rc_tlp_new_data = 0;
12325 new_data_tlp = doing_tlp = 1;
12326 } else
12327 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset);
12328 if (IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) {
12329 /*
12330 * For prr=off, we need to send only 1 MSS
12331 * at a time. We do this because another sack could
12332 * be arriving that causes us to send retransmits and
12333 * we don't want to be on a long pace due to a larger send
12334 * that keeps us from sending out the retransmit.
12335 */
12336 len = segsiz;
12337 }
12338 } else {
12339 uint32_t outstanding;
12340
12341 /*
12342 * We are inside of a SACK recovery episode and are
12343 * sending new data, having retransmitted all the
12344 * data possible so far in the scoreboard.
12345 */
12346 outstanding = tp->snd_max - tp->snd_una;
12347 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) {
12348 if (tp->snd_wnd > outstanding) {
12349 len = tp->snd_wnd - outstanding;
12350 /* Check to see if we have the data */
12351 if ((sb_offset + len) > avail) {
12352 /* It does not all fit */
12353 if (avail > sb_offset)
12354 len = avail - sb_offset;
12355 else
12356 len = 0;
12357 }
12358 } else
12359 len = 0;
12360 } else if (avail > sb_offset)
12361 len = avail - sb_offset;
12362 else
12363 len = 0;
12364 if (len > 0) {
12365 if (len > rack->r_ctl.rc_prr_sndcnt)
12366 len = rack->r_ctl.rc_prr_sndcnt;
12367 if (len > 0) {
12368 sub_from_prr = 1;
12369 counter_u64_add(rack_rtm_prr_newdata, 1);
12370 }
12371 }
12372 if (len > segsiz) {
12373 /*
12374 * We should never send more than a MSS when
12375 * retransmitting or sending new data in prr
12376 * mode unless the override flag is on. Most
12377 * likely the PRR algorithm is not going to
12378 * let us send a lot as well :-)
12379 */
12380 if (rack->r_ctl.rc_prr_sendalot == 0)
12381 len = segsiz;
12382 } else if (len < segsiz) {
12383 /*
12384 * Do we send any? The idea here is if the
12385 * send empty's the socket buffer we want to
12386 * do it. However if not then lets just wait
12387 * for our prr_sndcnt to get bigger.
12388 */
12389 long leftinsb;
12390
12391 leftinsb = sbavail(sb) - sb_offset;
12392 if (leftinsb > len) {
12393 /* This send does not empty the sb */
12394 len = 0;
12395 }
12396 }
12397 }
12398 } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
12399 /*
12400 * If you have not established
12401 * and are not doing FAST OPEN
12402 * no data please.
12403 */
12404 if ((sack_rxmit == 0) &&
12405 (!IS_FASTOPEN(tp->t_flags))){
12406 len = 0;
12407 sb_offset = 0;
12408 }
12409 }
12410 if (prefetch_so_done == 0) {
12411 kern_prefetch(so, &prefetch_so_done);
12412 prefetch_so_done = 1;
12413 }
12414 /*
12415 * Lop off SYN bit if it has already been sent. However, if this is
12416 * SYN-SENT state and if segment contains data and if we don't know
12417 * that foreign host supports TAO, suppress sending segment.
12418 */
12419 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
12420 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
12421 /*
12422 * When sending additional segments following a TFO SYN|ACK,
12423 * do not include the SYN bit.
12424 */
12425 if (IS_FASTOPEN(tp->t_flags) &&
12426 (tp->t_state == TCPS_SYN_RECEIVED))
12427 flags &= ~TH_SYN;
12428 }
12429 /*
12430 * Be careful not to send data and/or FIN on SYN segments. This
12431 * measure is needed to prevent interoperability problems with not
12432 * fully conformant TCP implementations.
12433 */
12434 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
12435 len = 0;
12436 flags &= ~TH_FIN;
12437 }
12438 /*
12439 * On TFO sockets, ensure no data is sent in the following cases:
12440 *
12441 * - When retransmitting SYN|ACK on a passively-created socket
12442 *
12443 * - When retransmitting SYN on an actively created socket
12444 *
12445 * - When sending a zero-length cookie (cookie request) on an
12446 * actively created socket
12447 *
12448 * - When the socket is in the CLOSED state (RST is being sent)
12449 */
12450 if (IS_FASTOPEN(tp->t_flags) &&
12451 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
12452 ((tp->t_state == TCPS_SYN_SENT) &&
12453 (tp->t_tfo_client_cookie_len == 0)) ||
12454 (flags & TH_RST))) {
12455 sack_rxmit = 0;
12456 len = 0;
12457 }
12458 /* Without fast-open there should never be data sent on a SYN */
12459 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) {
12460 tp->snd_nxt = tp->iss;
12461 len = 0;
12462 }
12463 orig_len = len;
12464 if (len <= 0) {
12465 /*
12466 * If FIN has been sent but not acked, but we haven't been
12467 * called to retransmit, len will be < 0. Otherwise, window
12468 * shrank after we sent into it. If window shrank to 0,
12469 * cancel pending retransmit, pull snd_nxt back to (closed)
12470 * window, and set the persist timer if it isn't already
12471 * going. If the window didn't close completely, just wait
12472 * for an ACK.
12473 *
12474 * We also do a general check here to ensure that we will
12475 * set the persist timer when we have data to send, but a
12476 * 0-byte window. This makes sure the persist timer is set
12477 * even if the packet hits one of the "goto send" lines
12478 * below.
12479 */
12480 len = 0;
12481 if ((tp->snd_wnd == 0) &&
12482 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12483 (tp->snd_una == tp->snd_max) &&
12484 (sb_offset < (int)sbavail(sb))) {
12485 tp->snd_nxt = tp->snd_una;
12486 rack_enter_persist(tp, rack, cts);
12487 }
12488 } else if ((rsm == NULL) &&
12489 ((doing_tlp == 0) || (new_data_tlp == 1)) &&
12490 (len < rack->r_ctl.rc_pace_max_segs)) {
12491 /*
12492 * We are not sending a maximum sized segment for
12493 * some reason. Should we not send anything (think
12494 * sws or persists)?
12495 */
12496 if ((tp->snd_wnd < min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)), minseg)) &&
12497 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
12498 (len < minseg) &&
12499 (len < (int)(sbavail(sb) - sb_offset))) {
12500 /*
12501 * Here the rwnd is less than
12502 * the minimum pacing size, this is not a retransmit,
12503 * we are established and
12504 * the send is not the last in the socket buffer
12505 * we send nothing, and we may enter persists
12506 * if nothing is outstanding.
12507 */
12508 len = 0;
12509 if (tp->snd_max == tp->snd_una) {
12510 /*
12511 * Nothing out we can
12512 * go into persists.
12513 */
12514 rack_enter_persist(tp, rack, cts);
12515 tp->snd_nxt = tp->snd_una;
12516 }
12517 } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) &&
12518 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
12519 (len < (int)(sbavail(sb) - sb_offset)) &&
12520 (len < minseg)) {
12521 /*
12522 * Here we are not retransmitting, and
12523 * the cwnd is not so small that we could
12524 * not send at least a min size (rxt timer
12525 * not having gone off), We have 2 segments or
12526 * more already in flight, its not the tail end
12527 * of the socket buffer and the cwnd is blocking
12528 * us from sending out a minimum pacing segment size.
12529 * Lets not send anything.
12530 */
12531 len = 0;
12532 } else if (((tp->snd_wnd - ctf_outstanding(tp)) <
12533 min((rack->r_ctl.rc_high_rwnd/2), minseg)) &&
12534 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) &&
12535 (len < (int)(sbavail(sb) - sb_offset)) &&
12536 (TCPS_HAVEESTABLISHED(tp->t_state))) {
12537 /*
12538 * Here we have a send window but we have
12539 * filled it up and we can't send another pacing segment.
12540 * We also have in flight more than 2 segments
12541 * and we are not completing the sb i.e. we allow
12542 * the last bytes of the sb to go out even if
12543 * its not a full pacing segment.
12544 */
12545 len = 0;
12546 }
12547 }
12548 /* len will be >= 0 after this point. */
12549 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
12550 tcp_sndbuf_autoscale(tp, so, min(tp->snd_wnd, cwnd_to_use));
12551 /*
12552 * Decide if we can use TCP Segmentation Offloading (if supported by
12553 * hardware).
12554 *
12555 * TSO may only be used if we are in a pure bulk sending state. The
12556 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
12557 * options prevent using TSO. With TSO the TCP header is the same
12558 * (except for the sequence number) for all generated packets. This
12559 * makes it impossible to transmit any options which vary per
12560 * generated segment or packet.
12561 *
12562 * IPv4 handling has a clear separation of ip options and ip header
12563 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
12564 * the right thing below to provide length of just ip options and thus
12565 * checking for ipoptlen is enough to decide if ip options are present.
12566 */
12567
12568 #ifdef INET6
12569 if (isipv6)
12570 ipoptlen = ip6_optlen(tp->t_inpcb);
12571 else
12572 #endif
12573 if (tp->t_inpcb->inp_options)
12574 ipoptlen = tp->t_inpcb->inp_options->m_len -
12575 offsetof(struct ipoption, ipopt_list);
12576 else
12577 ipoptlen = 0;
12578 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12579 /*
12580 * Pre-calculate here as we save another lookup into the darknesses
12581 * of IPsec that way and can actually decide if TSO is ok.
12582 */
12583 #ifdef INET6
12584 if (isipv6 && IPSEC_ENABLED(ipv6))
12585 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
12586 #ifdef INET
12587 else
12588 #endif
12589 #endif /* INET6 */
12590 #ifdef INET
12591 if (IPSEC_ENABLED(ipv4))
12592 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
12593 #endif /* INET */
12594 #endif
12595
12596 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12597 ipoptlen += ipsec_optlen;
12598 #endif
12599 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz &&
12600 (tp->t_port == 0) &&
12601 ((tp->t_flags & TF_SIGNATURE) == 0) &&
12602 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
12603 ipoptlen == 0)
12604 tso = 1;
12605 {
12606 uint32_t outstanding;
12607
12608 outstanding = tp->snd_max - tp->snd_una;
12609 if (tp->t_flags & TF_SENTFIN) {
12610 /*
12611 * If we sent a fin, snd_max is 1 higher than
12612 * snd_una
12613 */
12614 outstanding--;
12615 }
12616 if (sack_rxmit) {
12617 if ((rsm->r_flags & RACK_HAS_FIN) == 0)
12618 flags &= ~TH_FIN;
12619 } else {
12620 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
12621 sbused(sb)))
12622 flags &= ~TH_FIN;
12623 }
12624 }
12625 recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
12626 (long)TCP_MAXWIN << tp->rcv_scale);
12627
12628 /*
12629 * Sender silly window avoidance. We transmit under the following
12630 * conditions when len is non-zero:
12631 *
12632 * - We have a full segment (or more with TSO) - This is the last
12633 * buffer in a write()/send() and we are either idle or running
12634 * NODELAY - we've timed out (e.g. persist timer) - we have more
12635 * then 1/2 the maximum send window's worth of data (receiver may be
12636 * limited the window size) - we need to retransmit
12637 */
12638 if (len) {
12639 if (len >= segsiz) {
12640 goto send;
12641 }
12642 /*
12643 * NOTE! on localhost connections an 'ack' from the remote
12644 * end may occur synchronously with the output and cause us
12645 * to flush a buffer queued with moretocome. XXX
12646 *
12647 */
12648 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
12649 (idle || (tp->t_flags & TF_NODELAY)) &&
12650 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
12651 (tp->t_flags & TF_NOPUSH) == 0) {
12652 pass = 2;
12653 goto send;
12654 }
12655 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
12656 pass = 22;
12657 goto send;
12658 }
12659 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
12660 pass = 4;
12661 goto send;
12662 }
12663 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */
12664 pass = 5;
12665 goto send;
12666 }
12667 if (sack_rxmit) {
12668 pass = 6;
12669 goto send;
12670 }
12671 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) &&
12672 (ctf_outstanding(tp) < (segsiz * 2))) {
12673 /*
12674 * We have less than two MSS outstanding (delayed ack)
12675 * and our rwnd will not let us send a full sized
12676 * MSS. Lets go ahead and let this small segment
12677 * out because we want to try to have at least two
12678 * packets inflight to not be caught by delayed ack.
12679 */
12680 pass = 12;
12681 goto send;
12682 }
12683 }
12684 /*
12685 * Sending of standalone window updates.
12686 *
12687 * Window updates are important when we close our window due to a
12688 * full socket buffer and are opening it again after the application
12689 * reads data from it. Once the window has opened again and the
12690 * remote end starts to send again the ACK clock takes over and
12691 * provides the most current window information.
12692 *
12693 * We must avoid the silly window syndrome whereas every read from
12694 * the receive buffer, no matter how small, causes a window update
12695 * to be sent. We also should avoid sending a flurry of window
12696 * updates when the socket buffer had queued a lot of data and the
12697 * application is doing small reads.
12698 *
12699 * Prevent a flurry of pointless window updates by only sending an
12700 * update when we can increase the advertized window by more than
12701 * 1/4th of the socket buffer capacity. When the buffer is getting
12702 * full or is very small be more aggressive and send an update
12703 * whenever we can increase by two mss sized segments. In all other
12704 * situations the ACK's to new incoming data will carry further
12705 * window increases.
12706 *
12707 * Don't send an independent window update if a delayed ACK is
12708 * pending (it will get piggy-backed on it) or the remote side
12709 * already has done a half-close and won't send more data. Skip
12710 * this if the connection is in T/TCP half-open state.
12711 */
12712 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
12713 !(tp->t_flags & TF_DELACK) &&
12714 !TCPS_HAVERCVDFIN(tp->t_state)) {
12715 /*
12716 * "adv" is the amount we could increase the window, taking
12717 * into account that we are limited by TCP_MAXWIN <<
12718 * tp->rcv_scale.
12719 */
12720 int32_t adv;
12721 int oldwin;
12722
12723 adv = recwin;
12724 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
12725 oldwin = (tp->rcv_adv - tp->rcv_nxt);
12726 if (adv > oldwin)
12727 adv -= oldwin;
12728 else {
12729 /* We can't increase the window */
12730 adv = 0;
12731 }
12732 } else
12733 oldwin = 0;
12734
12735 /*
12736 * If the new window size ends up being the same as or less
12737 * than the old size when it is scaled, then don't force
12738 * a window update.
12739 */
12740 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
12741 goto dontupdate;
12742
12743 if (adv >= (int32_t)(2 * segsiz) &&
12744 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
12745 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
12746 so->so_rcv.sb_hiwat <= 8 * segsiz)) {
12747 pass = 7;
12748 goto send;
12749 }
12750 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) {
12751 pass = 23;
12752 goto send;
12753 }
12754 }
12755 dontupdate:
12756
12757 /*
12758 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
12759 * is also a catch-all for the retransmit timer timeout case.
12760 */
12761 if (tp->t_flags & TF_ACKNOW) {
12762 pass = 8;
12763 goto send;
12764 }
12765 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
12766 pass = 9;
12767 goto send;
12768 }
12769 /*
12770 * If our state indicates that FIN should be sent and we have not
12771 * yet done so, then we need to send.
12772 */
12773 if ((flags & TH_FIN) &&
12774 (tp->snd_nxt == tp->snd_una)) {
12775 pass = 11;
12776 goto send;
12777 }
12778 /*
12779 * No reason to send a segment, just return.
12780 */
12781 just_return:
12782 SOCKBUF_UNLOCK(sb);
12783 just_return_nolock:
12784 {
12785 int app_limited = CTF_JR_SENT_DATA;
12786
12787 if (tot_len_this_send > 0) {
12788 /* Make sure snd_nxt is up to max */
12789 if (SEQ_GT(tp->snd_max, tp->snd_nxt))
12790 tp->snd_nxt = tp->snd_max;
12791 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz);
12792 } else {
12793 int end_window = 0;
12794 uint32_t seq = tp->gput_ack;
12795
12796 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
12797 if (rsm) {
12798 /*
12799 * Mark the last sent that we just-returned (hinting
12800 * that delayed ack may play a role in any rtt measurement).
12801 */
12802 rsm->r_just_ret = 1;
12803 }
12804 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
12805 rack->r_ctl.rc_agg_delayed = 0;
12806 rack->r_early = 0;
12807 rack->r_late = 0;
12808 rack->r_ctl.rc_agg_early = 0;
12809 if ((ctf_outstanding(tp) +
12810 min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)),
12811 minseg)) >= tp->snd_wnd) {
12812 /* We are limited by the rwnd */
12813 app_limited = CTF_JR_RWND_LIMITED;
12814 } else if (ctf_outstanding(tp) >= sbavail(sb)) {
12815 /* We are limited by whats available -- app limited */
12816 app_limited = CTF_JR_APP_LIMITED;
12817 } else if ((idle == 0) &&
12818 ((tp->t_flags & TF_NODELAY) == 0) &&
12819 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) &&
12820 (len < segsiz)) {
12821 /*
12822 * No delay is not on and the
12823 * user is sending less than 1MSS. This
12824 * brings out SWS avoidance so we
12825 * don't send. Another app-limited case.
12826 */
12827 app_limited = CTF_JR_APP_LIMITED;
12828 } else if (tp->t_flags & TF_NOPUSH) {
12829 /*
12830 * The user has requested no push of
12831 * the last segment and we are
12832 * at the last segment. Another app
12833 * limited case.
12834 */
12835 app_limited = CTF_JR_APP_LIMITED;
12836 } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) {
12837 /* Its the cwnd */
12838 app_limited = CTF_JR_CWND_LIMITED;
12839 } else if (rack->rc_in_persist == 1) {
12840 /* We are in persists */
12841 app_limited = CTF_JR_PERSISTS;
12842 } else if (IN_RECOVERY(tp->t_flags) &&
12843 (rack->rack_no_prr == 0) &&
12844 (rack->r_ctl.rc_prr_sndcnt < segsiz)) {
12845 app_limited = CTF_JR_PRR;
12846 } else {
12847 /* Now why here are we not sending? */
12848 #ifdef NOW
12849 #ifdef INVARIANTS
12850 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use);
12851 #endif
12852 #endif
12853 app_limited = CTF_JR_ASSESSING;
12854 }
12855 /*
12856 * App limited in some fashion, for our pacing GP
12857 * measurements we don't want any gap (even cwnd).
12858 * Close down the measurement window.
12859 */
12860 if (rack_cwnd_block_ends_measure &&
12861 ((app_limited == CTF_JR_CWND_LIMITED) ||
12862 (app_limited == CTF_JR_PRR))) {
12863 /*
12864 * The reason we are not sending is
12865 * the cwnd (or prr). We have been configured
12866 * to end the measurement window in
12867 * this case.
12868 */
12869 end_window = 1;
12870 } else if (app_limited == CTF_JR_PERSISTS) {
12871 /*
12872 * We never end the measurement window
12873 * in persists, though in theory we
12874 * should be only entering after everything
12875 * is acknowledged (so we will probably
12876 * never come here).
12877 */
12878 end_window = 0;
12879 } else if (rack_rwnd_block_ends_measure &&
12880 (app_limited == CTF_JR_RWND_LIMITED)) {
12881 /*
12882 * We are rwnd limited and have been
12883 * configured to end the measurement
12884 * window in this case.
12885 */
12886 end_window = 1;
12887 } else if (app_limited == CTF_JR_APP_LIMITED) {
12888 /*
12889 * A true application limited period, we have
12890 * ran out of data.
12891 */
12892 end_window = 1;
12893 } else if (app_limited == CTF_JR_ASSESSING) {
12894 /*
12895 * In the assessing case we hit the end of
12896 * the if/else and had no known reason
12897 * This will panic us under invariants..
12898 *
12899 * If we get this out in logs we need to
12900 * investagate which reason we missed.
12901 */
12902 end_window = 1;
12903 }
12904 if (end_window) {
12905 uint8_t log = 0;
12906
12907 if ((tp->t_flags & TF_GPUTINPROG) &&
12908 SEQ_GT(tp->gput_ack, tp->snd_max)) {
12909 /* Mark the last packet has app limited */
12910 tp->gput_ack = tp->snd_max;
12911 log = 1;
12912 }
12913 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree);
12914 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) {
12915 if (rack->r_ctl.rc_app_limited_cnt == 0)
12916 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm;
12917 else {
12918 /*
12919 * Go out to the end app limited and mark
12920 * this new one as next and move the end_appl up
12921 * to this guy.
12922 */
12923 if (rack->r_ctl.rc_end_appl)
12924 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start;
12925 rack->r_ctl.rc_end_appl = rsm;
12926 }
12927 rsm->r_flags |= RACK_APP_LIMITED;
12928 rack->r_ctl.rc_app_limited_cnt++;
12929 }
12930 if (log)
12931 rack_log_pacing_delay_calc(rack,
12932 rack->r_ctl.rc_app_limited_cnt, seq,
12933 tp->gput_ack, 0, 0, 4, __LINE__, NULL);
12934 }
12935 }
12936 if (slot) {
12937 /* set the rack tcb into the slot N */
12938 counter_u64_add(rack_paced_segments, 1);
12939 } else if (tot_len_this_send) {
12940 counter_u64_add(rack_unpaced_segments, 1);
12941 }
12942 /* Check if we need to go into persists or not */
12943 if ((rack->rc_in_persist == 0) &&
12944 (tp->snd_max == tp->snd_una) &&
12945 TCPS_HAVEESTABLISHED(tp->t_state) &&
12946 sbavail(sb) &&
12947 (sbavail(sb) > tp->snd_wnd) &&
12948 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) {
12949 /* Yes lets make sure to move to persist before timer-start */
12950 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime);
12951 }
12952 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack);
12953 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use);
12954 }
12955 #ifdef NETFLIX_SHARED_CWND
12956 if ((sbavail(sb) == 0) &&
12957 rack->r_ctl.rc_scw) {
12958 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index);
12959 rack->rack_scwnd_is_idle = 1;
12960 }
12961 #endif
12962 return (0);
12963
12964 send:
12965 if ((flags & TH_FIN) &&
12966 sbavail(sb)) {
12967 /*
12968 * We do not transmit a FIN
12969 * with data outstanding. We
12970 * need to make it so all data
12971 * is acked first.
12972 */
12973 flags &= ~TH_FIN;
12974 }
12975 /* Enforce stack imposed max seg size if we have one */
12976 if (rack->r_ctl.rc_pace_max_segs &&
12977 (len > rack->r_ctl.rc_pace_max_segs)) {
12978 mark = 1;
12979 len = rack->r_ctl.rc_pace_max_segs;
12980 }
12981 SOCKBUF_LOCK_ASSERT(sb);
12982 if (len > 0) {
12983 if (len >= segsiz)
12984 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
12985 else
12986 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
12987 }
12988 /*
12989 * Before ESTABLISHED, force sending of initial options unless TCP
12990 * set not to do any options. NOTE: we assume that the IP/TCP header
12991 * plus TCP options always fit in a single mbuf, leaving room for a
12992 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
12993 * + optlen <= MCLBYTES
12994 */
12995 optlen = 0;
12996 #ifdef INET6
12997 if (isipv6)
12998 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
12999 else
13000 #endif
13001 hdrlen = sizeof(struct tcpiphdr);
13002
13003 /*
13004 * Compute options for segment. We only have to care about SYN and
13005 * established connection segments. Options for SYN-ACK segments
13006 * are handled in TCP syncache.
13007 */
13008 to.to_flags = 0;
13009 if ((tp->t_flags & TF_NOOPT) == 0) {
13010 /* Maximum segment size. */
13011 if (flags & TH_SYN) {
13012 tp->snd_nxt = tp->iss;
13013 to.to_mss = tcp_mssopt(&inp->inp_inc);
13014 #ifdef NETFLIX_TCPOUDP
13015 if (tp->t_port)
13016 to.to_mss -= V_tcp_udp_tunneling_overhead;
13017 #endif
13018 to.to_flags |= TOF_MSS;
13019
13020 /*
13021 * On SYN or SYN|ACK transmits on TFO connections,
13022 * only include the TFO option if it is not a
13023 * retransmit, as the presence of the TFO option may
13024 * have caused the original SYN or SYN|ACK to have
13025 * been dropped by a middlebox.
13026 */
13027 if (IS_FASTOPEN(tp->t_flags) &&
13028 (tp->t_rxtshift == 0)) {
13029 if (tp->t_state == TCPS_SYN_RECEIVED) {
13030 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
13031 to.to_tfo_cookie =
13032 (u_int8_t *)&tp->t_tfo_cookie.server;
13033 to.to_flags |= TOF_FASTOPEN;
13034 wanted_cookie = 1;
13035 } else if (tp->t_state == TCPS_SYN_SENT) {
13036 to.to_tfo_len =
13037 tp->t_tfo_client_cookie_len;
13038 to.to_tfo_cookie =
13039 tp->t_tfo_cookie.client;
13040 to.to_flags |= TOF_FASTOPEN;
13041 wanted_cookie = 1;
13042 /*
13043 * If we wind up having more data to
13044 * send with the SYN than can fit in
13045 * one segment, don't send any more
13046 * until the SYN|ACK comes back from
13047 * the other end.
13048 */
13049 sendalot = 0;
13050 }
13051 }
13052 }
13053 /* Window scaling. */
13054 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
13055 to.to_wscale = tp->request_r_scale;
13056 to.to_flags |= TOF_SCALE;
13057 }
13058 /* Timestamps. */
13059 if ((tp->t_flags & TF_RCVD_TSTMP) ||
13060 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
13061 to.to_tsval = cts + tp->ts_offset;
13062 to.to_tsecr = tp->ts_recent;
13063 to.to_flags |= TOF_TS;
13064 }
13065 /* Set receive buffer autosizing timestamp. */
13066 if (tp->rfbuf_ts == 0 &&
13067 (so->so_rcv.sb_flags & SB_AUTOSIZE))
13068 tp->rfbuf_ts = tcp_ts_getticks();
13069 /* Selective ACK's. */
13070 if (flags & TH_SYN)
13071 to.to_flags |= TOF_SACKPERM;
13072 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13073 tp->rcv_numsacks > 0) {
13074 to.to_flags |= TOF_SACK;
13075 to.to_nsacks = tp->rcv_numsacks;
13076 to.to_sacks = (u_char *)tp->sackblks;
13077 }
13078 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13079 /* TCP-MD5 (RFC2385). */
13080 if (tp->t_flags & TF_SIGNATURE)
13081 to.to_flags |= TOF_SIGNATURE;
13082 #endif /* TCP_SIGNATURE */
13083
13084 /* Processing the options. */
13085 hdrlen += optlen = tcp_addoptions(&to, opt);
13086 /*
13087 * If we wanted a TFO option to be added, but it was unable
13088 * to fit, ensure no data is sent.
13089 */
13090 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
13091 !(to.to_flags & TOF_FASTOPEN))
13092 len = 0;
13093 }
13094 #ifdef NETFLIX_TCPOUDP
13095 if (tp->t_port) {
13096 if (V_tcp_udp_tunneling_port == 0) {
13097 /* The port was removed?? */
13098 SOCKBUF_UNLOCK(&so->so_snd);
13099 return (EHOSTUNREACH);
13100 }
13101 hdrlen += sizeof(struct udphdr);
13102 }
13103 #endif
13104 #ifdef INET6
13105 if (isipv6)
13106 ipoptlen = ip6_optlen(tp->t_inpcb);
13107 else
13108 #endif
13109 if (tp->t_inpcb->inp_options)
13110 ipoptlen = tp->t_inpcb->inp_options->m_len -
13111 offsetof(struct ipoption, ipopt_list);
13112 else
13113 ipoptlen = 0;
13114 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13115 ipoptlen += ipsec_optlen;
13116 #endif
13117
13118 /*
13119 * Adjust data length if insertion of options will bump the packet
13120 * length beyond the t_maxseg length. Clear the FIN bit because we
13121 * cut off the tail of the segment.
13122 */
13123 if (len + optlen + ipoptlen > tp->t_maxseg) {
13124 if (tso) {
13125 uint32_t if_hw_tsomax;
13126 uint32_t moff;
13127 int32_t max_len;
13128
13129 /* extract TSO information */
13130 if_hw_tsomax = tp->t_tsomax;
13131 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
13132 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
13133 KASSERT(ipoptlen == 0,
13134 ("%s: TSO can't do IP options", __func__));
13135
13136 /*
13137 * Check if we should limit by maximum payload
13138 * length:
13139 */
13140 if (if_hw_tsomax != 0) {
13141 /* compute maximum TSO length */
13142 max_len = (if_hw_tsomax - hdrlen -
13143 max_linkhdr);
13144 if (max_len <= 0) {
13145 len = 0;
13146 } else if (len > max_len) {
13147 sendalot = 1;
13148 len = max_len;
13149 mark = 2;
13150 }
13151 }
13152 /*
13153 * Prevent the last segment from being fractional
13154 * unless the send sockbuf can be emptied:
13155 */
13156 max_len = (tp->t_maxseg - optlen);
13157 if ((sb_offset + len) < sbavail(sb)) {
13158 moff = len % (u_int)max_len;
13159 if (moff != 0) {
13160 mark = 3;
13161 len -= moff;
13162 }
13163 }
13164 /*
13165 * In case there are too many small fragments don't
13166 * use TSO:
13167 */
13168 if (len <= segsiz) {
13169 mark = 4;
13170 tso = 0;
13171 }
13172 /*
13173 * Send the FIN in a separate segment after the bulk
13174 * sending is done. We don't trust the TSO
13175 * implementations to clear the FIN flag on all but
13176 * the last segment.
13177 */
13178 if (tp->t_flags & TF_NEEDFIN) {
13179 sendalot = 4;
13180 }
13181 } else {
13182 mark = 5;
13183 if (optlen + ipoptlen >= tp->t_maxseg) {
13184 /*
13185 * Since we don't have enough space to put
13186 * the IP header chain and the TCP header in
13187 * one packet as required by RFC 7112, don't
13188 * send it. Also ensure that at least one
13189 * byte of the payload can be put into the
13190 * TCP segment.
13191 */
13192 SOCKBUF_UNLOCK(&so->so_snd);
13193 error = EMSGSIZE;
13194 sack_rxmit = 0;
13195 goto out;
13196 }
13197 len = tp->t_maxseg - optlen - ipoptlen;
13198 sendalot = 5;
13199 }
13200 } else {
13201 tso = 0;
13202 mark = 6;
13203 }
13204 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
13205 ("%s: len > IP_MAXPACKET", __func__));
13206 #ifdef DIAGNOSTIC
13207 #ifdef INET6
13208 if (max_linkhdr + hdrlen > MCLBYTES)
13209 #else
13210 if (max_linkhdr + hdrlen > MHLEN)
13211 #endif
13212 panic("tcphdr too big");
13213 #endif
13214
13215 /*
13216 * This KASSERT is here to catch edge cases at a well defined place.
13217 * Before, those had triggered (random) panic conditions further
13218 * down.
13219 */
13220 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
13221 if ((len == 0) &&
13222 (flags & TH_FIN) &&
13223 (sbused(sb))) {
13224 /*
13225 * We have outstanding data, don't send a fin by itself!.
13226 */
13227 goto just_return;
13228 }
13229 /*
13230 * Grab a header mbuf, attaching a copy of data to be transmitted,
13231 * and initialize the header from the template for sends on this
13232 * connection.
13233 */
13234 if (len) {
13235 uint32_t max_val;
13236 uint32_t moff;
13237
13238 if (rack->r_ctl.rc_pace_max_segs)
13239 max_val = rack->r_ctl.rc_pace_max_segs;
13240 else if (rack->rc_user_set_max_segs)
13241 max_val = rack->rc_user_set_max_segs * segsiz;
13242 else
13243 max_val = len;
13244 /*
13245 * We allow a limit on sending with hptsi.
13246 */
13247 if (len > max_val) {
13248 mark = 7;
13249 len = max_val;
13250 }
13251 #ifdef INET6
13252 if (MHLEN < hdrlen + max_linkhdr)
13253 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13254 else
13255 #endif
13256 m = m_gethdr(M_NOWAIT, MT_DATA);
13257
13258 if (m == NULL) {
13259 SOCKBUF_UNLOCK(sb);
13260 error = ENOBUFS;
13261 sack_rxmit = 0;
13262 goto out;
13263 }
13264 m->m_data += max_linkhdr;
13265 m->m_len = hdrlen;
13266
13267 /*
13268 * Start the m_copy functions from the closest mbuf to the
13269 * sb_offset in the socket buffer chain.
13270 */
13271 mb = sbsndptr_noadv(sb, sb_offset, &moff);
13272 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) {
13273 m_copydata(mb, moff, (int)len,
13274 mtod(m, caddr_t)+hdrlen);
13275 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
13276 sbsndptr_adv(sb, mb, len);
13277 m->m_len += len;
13278 } else {
13279 struct sockbuf *msb;
13280
13281 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
13282 msb = NULL;
13283 else
13284 msb = sb;
13285 m->m_next = tcp_m_copym(
13286 mb, moff, &len,
13287 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb,
13288 ((rsm == NULL) ? hw_tls : 0)
13289 #ifdef NETFLIX_COPY_ARGS
13290 , &filled_all
13291 #endif
13292 );
13293 if (len <= (tp->t_maxseg - optlen)) {
13294 /*
13295 * Must have ran out of mbufs for the copy
13296 * shorten it to no longer need tso. Lets
13297 * not put on sendalot since we are low on
13298 * mbufs.
13299 */
13300 tso = 0;
13301 }
13302 if (m->m_next == NULL) {
13303 SOCKBUF_UNLOCK(sb);
13304 (void)m_free(m);
13305 error = ENOBUFS;
13306 sack_rxmit = 0;
13307 goto out;
13308 }
13309 }
13310 if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
13311 if (rsm && (rsm->r_flags & RACK_TLP)) {
13312 /*
13313 * TLP should not count in retran count, but
13314 * in its own bin
13315 */
13316 counter_u64_add(rack_tlp_retran, 1);
13317 counter_u64_add(rack_tlp_retran_bytes, len);
13318 } else {
13319 tp->t_sndrexmitpack++;
13320 KMOD_TCPSTAT_INC(tcps_sndrexmitpack);
13321 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len);
13322 }
13323 #ifdef STATS
13324 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
13325 len);
13326 #endif
13327 } else {
13328 KMOD_TCPSTAT_INC(tcps_sndpack);
13329 KMOD_TCPSTAT_ADD(tcps_sndbyte, len);
13330 #ifdef STATS
13331 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
13332 len);
13333 #endif
13334 }
13335 /*
13336 * If we're sending everything we've got, set PUSH. (This
13337 * will keep happy those implementations which only give
13338 * data to the user when a buffer fills or a PUSH comes in.)
13339 */
13340 if (sb_offset + len == sbused(sb) &&
13341 sbused(sb) &&
13342 !(flags & TH_SYN))
13343 flags |= TH_PUSH;
13344
13345 SOCKBUF_UNLOCK(sb);
13346 } else {
13347 SOCKBUF_UNLOCK(sb);
13348 if (tp->t_flags & TF_ACKNOW)
13349 KMOD_TCPSTAT_INC(tcps_sndacks);
13350 else if (flags & (TH_SYN | TH_FIN | TH_RST))
13351 KMOD_TCPSTAT_INC(tcps_sndctrl);
13352 else
13353 KMOD_TCPSTAT_INC(tcps_sndwinup);
13354
13355 m = m_gethdr(M_NOWAIT, MT_DATA);
13356 if (m == NULL) {
13357 error = ENOBUFS;
13358 sack_rxmit = 0;
13359 goto out;
13360 }
13361 #ifdef INET6
13362 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
13363 MHLEN >= hdrlen) {
13364 M_ALIGN(m, hdrlen);
13365 } else
13366 #endif
13367 m->m_data += max_linkhdr;
13368 m->m_len = hdrlen;
13369 }
13370 SOCKBUF_UNLOCK_ASSERT(sb);
13371 m->m_pkthdr.rcvif = (struct ifnet *)0;
13372 #ifdef MAC
13373 mac_inpcb_create_mbuf(inp, m);
13374 #endif
13375 #ifdef INET6
13376 if (isipv6) {
13377 ip6 = mtod(m, struct ip6_hdr *);
13378 #ifdef NETFLIX_TCPOUDP
13379 if (tp->t_port) {
13380 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr));
13381 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13382 udp->uh_dport = tp->t_port;
13383 ulen = hdrlen + len - sizeof(struct ip6_hdr);
13384 udp->uh_ulen = htons(ulen);
13385 th = (struct tcphdr *)(udp + 1);
13386 } else
13387 #endif
13388 th = (struct tcphdr *)(ip6 + 1);
13389 tcpip_fillheaders(inp,
13390 #ifdef NETFLIX_TCPOUDP
13391 tp->t_port,
13392 #endif
13393 ip6, th);
13394 } else
13395 #endif /* INET6 */
13396 {
13397 ip = mtod(m, struct ip *);
13398 #ifdef TCPDEBUG
13399 ipov = (struct ipovly *)ip;
13400 #endif
13401 #ifdef NETFLIX_TCPOUDP
13402 if (tp->t_port) {
13403 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip));
13404 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
13405 udp->uh_dport = tp->t_port;
13406 ulen = hdrlen + len - sizeof(struct ip);
13407 udp->uh_ulen = htons(ulen);
13408 th = (struct tcphdr *)(udp + 1);
13409 } else
13410 #endif
13411 th = (struct tcphdr *)(ip + 1);
13412 tcpip_fillheaders(inp,
13413 #ifdef NETFLIX_TCPOUDP
13414 tp->t_port,
13415 #endif
13416 ip, th);
13417 }
13418 /*
13419 * Fill in fields, remembering maximum advertised window for use in
13420 * delaying messages about window sizes. If resending a FIN, be sure
13421 * not to use a new sequence number.
13422 */
13423 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
13424 tp->snd_nxt == tp->snd_max)
13425 tp->snd_nxt--;
13426 /*
13427 * If we are starting a connection, send ECN setup SYN packet. If we
13428 * are on a retransmit, we may resend those bits a number of times
13429 * as per RFC 3168.
13430 */
13431 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) {
13432 if (tp->t_rxtshift >= 1) {
13433 if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
13434 flags |= TH_ECE | TH_CWR;
13435 } else
13436 flags |= TH_ECE | TH_CWR;
13437 }
13438 /* Handle parallel SYN for ECN */
13439 if ((tp->t_state == TCPS_SYN_RECEIVED) &&
13440 (tp->t_flags2 & TF2_ECN_SND_ECE)) {
13441 flags |= TH_ECE;
13442 tp->t_flags2 &= ~TF2_ECN_SND_ECE;
13443 }
13444 if (tp->t_state == TCPS_ESTABLISHED &&
13445 (tp->t_flags2 & TF2_ECN_PERMIT)) {
13446 /*
13447 * If the peer has ECN, mark data packets with ECN capable
13448 * transmission (ECT). Ignore pure ack packets,
13449 * retransmissions.
13450 */
13451 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
13452 (sack_rxmit == 0)) {
13453 #ifdef INET6
13454 if (isipv6)
13455 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
13456 else
13457 #endif
13458 ip->ip_tos |= IPTOS_ECN_ECT0;
13459 KMOD_TCPSTAT_INC(tcps_ecn_ect0);
13460 /*
13461 * Reply with proper ECN notifications.
13462 * Only set CWR on new data segments.
13463 */
13464 if (tp->t_flags2 & TF2_ECN_SND_CWR) {
13465 flags |= TH_CWR;
13466 tp->t_flags2 &= ~TF2_ECN_SND_CWR;
13467 }
13468 }
13469 if (tp->t_flags2 & TF2_ECN_SND_ECE)
13470 flags |= TH_ECE;
13471 }
13472 /*
13473 * If we are doing retransmissions, then snd_nxt will not reflect
13474 * the first unsent octet. For ACK only packets, we do not want the
13475 * sequence number of the retransmitted packet, we want the sequence
13476 * number of the next unsent octet. So, if there is no data (and no
13477 * SYN or FIN), use snd_max instead of snd_nxt when filling in
13478 * ti_seq. But if we are in persist state, snd_max might reflect
13479 * one byte beyond the right edge of the window, so use snd_nxt in
13480 * that case, since we know we aren't doing a retransmission.
13481 * (retransmit and persist are mutually exclusive...)
13482 */
13483 if (sack_rxmit == 0) {
13484 if (len || (flags & (TH_SYN | TH_FIN)) ||
13485 rack->rc_in_persist) {
13486 th->th_seq = htonl(tp->snd_nxt);
13487 rack_seq = tp->snd_nxt;
13488 } else if (flags & TH_RST) {
13489 /*
13490 * For a Reset send the last cum ack in sequence
13491 * (this like any other choice may still generate a
13492 * challenge ack, if a ack-update packet is in
13493 * flight).
13494 */
13495 th->th_seq = htonl(tp->snd_una);
13496 rack_seq = tp->snd_una;
13497 } else {
13498 th->th_seq = htonl(tp->snd_max);
13499 rack_seq = tp->snd_max;
13500 }
13501 } else {
13502 th->th_seq = htonl(rsm->r_start);
13503 rack_seq = rsm->r_start;
13504 }
13505 th->th_ack = htonl(tp->rcv_nxt);
13506 if (optlen) {
13507 bcopy(opt, th + 1, optlen);
13508 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
13509 }
13510 th->th_flags = flags;
13511 /*
13512 * Calculate receive window. Don't shrink window, but avoid silly
13513 * window syndrome.
13514 * If a RST segment is sent, advertise a window of zero.
13515 */
13516 if (flags & TH_RST) {
13517 recwin = 0;
13518 } else {
13519 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
13520 recwin < (long)segsiz)
13521 recwin = 0;
13522 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
13523 recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
13524 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
13525 }
13526
13527 /*
13528 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
13529 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
13530 * handled in syncache.
13531 */
13532 if (flags & TH_SYN)
13533 th->th_win = htons((u_short)
13534 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
13535 else {
13536 /* Avoid shrinking window with window scaling. */
13537 recwin = roundup2(recwin, 1 << tp->rcv_scale);
13538 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
13539 }
13540 /*
13541 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
13542 * window. This may cause the remote transmitter to stall. This
13543 * flag tells soreceive() to disable delayed acknowledgements when
13544 * draining the buffer. This can occur if the receiver is
13545 * attempting to read more data than can be buffered prior to
13546 * transmitting on the connection.
13547 */
13548 if (th->th_win == 0) {
13549 tp->t_sndzerowin++;
13550 tp->t_flags |= TF_RXWIN0SENT;
13551 } else
13552 tp->t_flags &= ~TF_RXWIN0SENT;
13553 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */
13554
13555 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
13556 if (to.to_flags & TOF_SIGNATURE) {
13557 /*
13558 * Calculate MD5 signature and put it into the place
13559 * determined before.
13560 * NOTE: since TCP options buffer doesn't point into
13561 * mbuf's data, calculate offset and use it.
13562 */
13563 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
13564 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
13565 /*
13566 * Do not send segment if the calculation of MD5
13567 * digest has failed.
13568 */
13569 goto out;
13570 }
13571 }
13572 #endif
13573
13574 /*
13575 * Put TCP length in extended header, and then checksum extended
13576 * header and data.
13577 */
13578 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
13579 #ifdef INET6
13580 if (isipv6) {
13581 /*
13582 * ip6_plen is not need to be filled now, and will be filled
13583 * in ip6_output.
13584 */
13585 if (tp->t_port) {
13586 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
13587 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13588 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
13589 th->th_sum = htons(0);
13590 UDPSTAT_INC(udps_opackets);
13591 } else {
13592 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
13593 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13594 th->th_sum = in6_cksum_pseudo(ip6,
13595 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
13596 0);
13597 }
13598 }
13599 #endif
13600 #if defined(INET6) && defined(INET)
13601 else
13602 #endif
13603 #ifdef INET
13604 {
13605 if (tp->t_port) {
13606 m->m_pkthdr.csum_flags = CSUM_UDP;
13607 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
13608 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
13609 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
13610 th->th_sum = htons(0);
13611 UDPSTAT_INC(udps_opackets);
13612 } else {
13613 m->m_pkthdr.csum_flags = CSUM_TCP;
13614 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
13615 th->th_sum = in_pseudo(ip->ip_src.s_addr,
13616 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
13617 IPPROTO_TCP + len + optlen));
13618 }
13619 /* IP version must be set here for ipv4/ipv6 checking later */
13620 KASSERT(ip->ip_v == IPVERSION,
13621 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
13622 }
13623 #endif
13624 /*
13625 * Enable TSO and specify the size of the segments. The TCP pseudo
13626 * header checksum is always provided. XXX: Fixme: This is currently
13627 * not the case for IPv6.
13628 */
13629 if (tso) {
13630 KASSERT(len > tp->t_maxseg - optlen,
13631 ("%s: len <= tso_segsz", __func__));
13632 m->m_pkthdr.csum_flags |= CSUM_TSO;
13633 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
13634 }
13635 KASSERT(len + hdrlen == m_length(m, NULL),
13636 ("%s: mbuf chain different than expected: %d + %u != %u",
13637 __func__, len, hdrlen, m_length(m, NULL)));
13638
13639 #ifdef TCP_HHOOK
13640 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
13641 hhook_run_tcp_est_out(tp, th, &to, len, tso);
13642 #endif
13643 #ifdef TCPDEBUG
13644 /*
13645 * Trace.
13646 */
13647 if (so->so_options & SO_DEBUG) {
13648 u_short save = 0;
13649
13650 #ifdef INET6
13651 if (!isipv6)
13652 #endif
13653 {
13654 save = ipov->ih_len;
13655 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen +
13656 * (th->th_off << 2) */ );
13657 }
13658 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13659 #ifdef INET6
13660 if (!isipv6)
13661 #endif
13662 ipov->ih_len = save;
13663 }
13664 #endif /* TCPDEBUG */
13665
13666 /* We're getting ready to send; log now. */
13667 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
13668 union tcp_log_stackspecific log;
13669 struct timeval tv;
13670
13671 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
13672 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
13673 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
13674 if (rack->rack_no_prr)
13675 log.u_bbr.flex1 = 0;
13676 else
13677 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
13678 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs;
13679 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs;
13680 log.u_bbr.flex4 = orig_len;
13681 if (filled_all)
13682 log.u_bbr.flex5 = 0x80000000;
13683 else
13684 log.u_bbr.flex5 = 0;
13685 /* Save off the early/late values */
13686 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early;
13687 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed;
13688 log.u_bbr.bw_inuse = rack_get_bw(rack);
13689 if (rsm || sack_rxmit) {
13690 if (doing_tlp)
13691 log.u_bbr.flex8 = 2;
13692 else
13693 log.u_bbr.flex8 = 1;
13694 } else {
13695 log.u_bbr.flex8 = 0;
13696 }
13697 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm);
13698 log.u_bbr.flex7 = mark;
13699 log.u_bbr.pkts_out = tp->t_maxseg;
13700 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
13701 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked);
13702 log.u_bbr.lt_epoch = cwnd_to_use;
13703 log.u_bbr.delivered = sendalot;
13704 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
13705 len, &log, false, NULL, NULL, 0, &tv);
13706 } else
13707 lgb = NULL;
13708
13709 /*
13710 * Fill in IP length and desired time to live and send to IP level.
13711 * There should be a better way to handle ttl and tos; we could keep
13712 * them in the template, but need a way to checksum without them.
13713 */
13714 /*
13715 * m->m_pkthdr.len should have been set before cksum calcuration,
13716 * because in6_cksum() need it.
13717 */
13718 #ifdef INET6
13719 if (isipv6) {
13720 /*
13721 * we separately set hoplimit for every segment, since the
13722 * user might want to change the value via setsockopt. Also,
13723 * desired default hop limit might be changed via Neighbor
13724 * Discovery.
13725 */
13726 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
13727
13728 /*
13729 * Set the packet size here for the benefit of DTrace
13730 * probes. ip6_output() will set it properly; it's supposed
13731 * to include the option header lengths as well.
13732 */
13733 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
13734
13735 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
13736 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13737 else
13738 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13739
13740 if (tp->t_state == TCPS_SYN_SENT)
13741 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
13742
13743 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
13744 /* TODO: IPv6 IP6TOS_ECT bit on */
13745 error = ip6_output(m, inp->in6p_outputopts,
13746 &inp->inp_route6,
13747 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0),
13748 NULL, NULL, inp);
13749
13750 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL)
13751 mtu = inp->inp_route6.ro_nh->nh_mtu;
13752 }
13753 #endif /* INET6 */
13754 #if defined(INET) && defined(INET6)
13755 else
13756 #endif
13757 #ifdef INET
13758 {
13759 ip->ip_len = htons(m->m_pkthdr.len);
13760 #ifdef INET6
13761 if (inp->inp_vflag & INP_IPV6PROTO)
13762 ip->ip_ttl = in6_selecthlim(inp, NULL);
13763 #endif /* INET6 */
13764 /*
13765 * If we do path MTU discovery, then we set DF on every
13766 * packet. This might not be the best thing to do according
13767 * to RFC3390 Section 2. However the tcp hostcache migitates
13768 * the problem so it affects only the first tcp connection
13769 * with a host.
13770 *
13771 * NB: Don't set DF on small MTU/MSS to have a safe
13772 * fallback.
13773 */
13774 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13775 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13776 if (tp->t_port == 0 || len < V_tcp_minmss) {
13777 ip->ip_off |= htons(IP_DF);
13778 }
13779 } else {
13780 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13781 }
13782
13783 if (tp->t_state == TCPS_SYN_SENT)
13784 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
13785
13786 TCP_PROBE5(send, NULL, tp, ip, tp, th);
13787
13788 error = ip_output(m, inp->inp_options, &inp->inp_route,
13789 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0,
13790 inp);
13791 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL)
13792 mtu = inp->inp_route.ro_nh->nh_mtu;
13793 }
13794 #endif /* INET */
13795
13796 out:
13797 if (lgb) {
13798 lgb->tlb_errno = error;
13799 lgb = NULL;
13800 }
13801 /*
13802 * In transmit state, time the transmission and arrange for the
13803 * retransmit. In persist state, just set snd_max.
13804 */
13805 if (error == 0) {
13806 rack->forced_ack = 0; /* If we send something zap the FA flag */
13807 if (rsm && (doing_tlp == 0)) {
13808 /* Set we retransmitted */
13809 rack->rc_gp_saw_rec = 1;
13810 } else {
13811 if (cwnd_to_use > tp->snd_ssthresh) {
13812 /* Set we sent in CA */
13813 rack->rc_gp_saw_ca = 1;
13814 } else {
13815 /* Set we sent in SS */
13816 rack->rc_gp_saw_ss = 1;
13817 }
13818 }
13819 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
13820 (tp->t_flags & TF_SACK_PERMIT) &&
13821 tp->rcv_numsacks > 0)
13822 tcp_clean_dsack_blocks(tp);
13823 tot_len_this_send += len;
13824 if (len == 0)
13825 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
13826 else if (len == 1) {
13827 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
13828 } else if (len > 1) {
13829 int idx;
13830
13831 idx = (len / segsiz) + 3;
13832 if (idx >= TCP_MSS_ACCT_ATIMER)
13833 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
13834 else
13835 counter_u64_add(rack_out_size[idx], 1);
13836 }
13837 }
13838 if (rack->rack_no_prr == 0) {
13839 if (sub_from_prr && (error == 0)) {
13840 if (rack->r_ctl.rc_prr_sndcnt >= len)
13841 rack->r_ctl.rc_prr_sndcnt -= len;
13842 else
13843 rack->r_ctl.rc_prr_sndcnt = 0;
13844 }
13845 }
13846 sub_from_prr = 0;
13847 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, cts,
13848 pass, rsm, us_cts);
13849 if ((error == 0) &&
13850 (len > 0) &&
13851 (tp->snd_una == tp->snd_max))
13852 rack->r_ctl.rc_tlp_rxt_last_time = cts;
13853 /* Now are we in persists? */
13854 if (rack->rc_in_persist == 0) {
13855 tcp_seq startseq = tp->snd_nxt;
13856
13857 /* Track our lost count */
13858 if (rsm && (doing_tlp == 0))
13859 rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start;
13860 /*
13861 * Advance snd_nxt over sequence space of this segment.
13862 */
13863 if (error)
13864 /* We don't log or do anything with errors */
13865 goto nomore;
13866 if (doing_tlp == 0) {
13867 if (rsm == NULL) {
13868 /*
13869 * Not a retransmission of some
13870 * sort, new data is going out so
13871 * clear our TLP count and flag.
13872 */
13873 rack->rc_tlp_in_progress = 0;
13874 rack->r_ctl.rc_tlp_cnt_out = 0;
13875 }
13876 } else {
13877 /*
13878 * We have just sent a TLP, mark that it is true
13879 * and make sure our in progress is set so we
13880 * continue to check the count.
13881 */
13882 rack->rc_tlp_in_progress = 1;
13883 rack->r_ctl.rc_tlp_cnt_out++;
13884 }
13885 if (flags & (TH_SYN | TH_FIN)) {
13886 if (flags & TH_SYN)
13887 tp->snd_nxt++;
13888 if (flags & TH_FIN) {
13889 tp->snd_nxt++;
13890 tp->t_flags |= TF_SENTFIN;
13891 }
13892 }
13893 /* In the ENOBUFS case we do *not* update snd_max */
13894 if (sack_rxmit)
13895 goto nomore;
13896
13897 tp->snd_nxt += len;
13898 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
13899 if (tp->snd_una == tp->snd_max) {
13900 /*
13901 * Update the time we just added data since
13902 * none was outstanding.
13903 */
13904 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
13905 tp->t_acktime = ticks;
13906 }
13907 tp->snd_max = tp->snd_nxt;
13908 /*
13909 * Time this transmission if not a retransmission and
13910 * not currently timing anything.
13911 * This is only relevant in case of switching back to
13912 * the base stack.
13913 */
13914 if (tp->t_rtttime == 0) {
13915 tp->t_rtttime = ticks;
13916 tp->t_rtseq = startseq;
13917 KMOD_TCPSTAT_INC(tcps_segstimed);
13918 }
13919 if (len &&
13920 ((tp->t_flags & TF_GPUTINPROG) == 0))
13921 rack_start_gp_measurement(tp, rack, startseq, sb_offset);
13922 }
13923 } else {
13924 /*
13925 * Persist case, update snd_max but since we are in persist
13926 * mode (no window) we do not update snd_nxt.
13927 */
13928 int32_t xlen = len;
13929
13930 if (error)
13931 goto nomore;
13932
13933 if (flags & TH_SYN)
13934 ++xlen;
13935 if (flags & TH_FIN) {
13936 ++xlen;
13937 tp->t_flags |= TF_SENTFIN;
13938 }
13939 /* In the ENOBUFS case we do *not* update snd_max */
13940 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) {
13941 if (tp->snd_una == tp->snd_max) {
13942 /*
13943 * Update the time we just added data since
13944 * none was outstanding.
13945 */
13946 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
13947 tp->t_acktime = ticks;
13948 }
13949 tp->snd_max = tp->snd_nxt + len;
13950 }
13951 }
13952 nomore:
13953 if (error) {
13954 rack->r_ctl.rc_agg_delayed = 0;
13955 rack->r_early = 0;
13956 rack->r_late = 0;
13957 rack->r_ctl.rc_agg_early = 0;
13958 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */
13959 /*
13960 * Failures do not advance the seq counter above. For the
13961 * case of ENOBUFS we will fall out and retry in 1ms with
13962 * the hpts. Everything else will just have to retransmit
13963 * with the timer.
13964 *
13965 * In any case, we do not want to loop around for another
13966 * send without a good reason.
13967 */
13968 sendalot = 0;
13969 switch (error) {
13970 case EPERM:
13971 tp->t_softerror = error;
13972 return (error);
13973 case ENOBUFS:
13974 if (slot == 0) {
13975 /*
13976 * Pace us right away to retry in a some
13977 * time
13978 */
13979 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC);
13980 if (rack->rc_enobuf < 126)
13981 rack->rc_enobuf++;
13982 if (slot > ((rack->rc_rack_rtt / 2) * HPTS_USEC_IN_MSEC)) {
13983 slot = (rack->rc_rack_rtt / 2) * HPTS_USEC_IN_MSEC;
13984 }
13985 if (slot < (10 * HPTS_USEC_IN_MSEC))
13986 slot = 10 * HPTS_USEC_IN_MSEC;
13987 }
13988 counter_u64_add(rack_saw_enobuf, 1);
13989 error = 0;
13990 goto enobufs;
13991 case EMSGSIZE:
13992 /*
13993 * For some reason the interface we used initially
13994 * to send segments changed to another or lowered
13995 * its MTU. If TSO was active we either got an
13996 * interface without TSO capabilits or TSO was
13997 * turned off. If we obtained mtu from ip_output()
13998 * then update it and try again.
13999 */
14000 if (tso)
14001 tp->t_flags &= ~TF_TSO;
14002 if (mtu != 0) {
14003 tcp_mss_update(tp, -1, mtu, NULL, NULL);
14004 goto again;
14005 }
14006 slot = 10 * HPTS_USEC_IN_MSEC;
14007 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
14008 return (error);
14009 case ENETUNREACH:
14010 counter_u64_add(rack_saw_enetunreach, 1);
14011 case EHOSTDOWN:
14012 case EHOSTUNREACH:
14013 case ENETDOWN:
14014 if (TCPS_HAVERCVDSYN(tp->t_state)) {
14015 tp->t_softerror = error;
14016 }
14017 /* FALLTHROUGH */
14018 default:
14019 slot = 10 * HPTS_USEC_IN_MSEC;
14020 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0);
14021 return (error);
14022 }
14023 } else {
14024 rack->rc_enobuf = 0;
14025 }
14026 KMOD_TCPSTAT_INC(tcps_sndtotal);
14027
14028 /*
14029 * Data sent (as far as we can tell). If this advertises a larger
14030 * window than any other segment, then remember the size of the
14031 * advertised window. Any pending ACK has now been sent.
14032 */
14033 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
14034 tp->rcv_adv = tp->rcv_nxt + recwin;
14035 tp->last_ack_sent = tp->rcv_nxt;
14036 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
14037 enobufs:
14038 /* Assure when we leave that snd_nxt will point to top */
14039 if (SEQ_GT(tp->snd_max, tp->snd_nxt))
14040 tp->snd_nxt = tp->snd_max;
14041 if (sendalot) {
14042 /* Do we need to turn off sendalot? */
14043 if (rack->r_ctl.rc_pace_max_segs &&
14044 (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) {
14045 /* We hit our max. */
14046 sendalot = 0;
14047 } else if ((rack->rc_user_set_max_segs) &&
14048 (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) {
14049 /* We hit the user defined max */
14050 sendalot = 0;
14051 }
14052 }
14053 if ((error == 0) && (flags & TH_FIN))
14054 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN);
14055 if (flags & TH_RST) {
14056 /*
14057 * We don't send again after sending a RST.
14058 */
14059 slot = 0;
14060 sendalot = 0;
14061 if (error == 0)
14062 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
14063 } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) {
14064 /*
14065 * Get our pacing rate, if an error
14066 * occured in sending (ENOBUF) we would
14067 * hit the else if with slot preset. Other
14068 * errors return.
14069 */
14070 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz);
14071 }
14072 if (rsm &&
14073 rack->use_rack_rr) {
14074 /* Its a retransmit and we use the rack cheat? */
14075 if ((slot == 0) ||
14076 (rack->rc_always_pace == 0) ||
14077 (rack->r_rr_config == 1)) {
14078 /*
14079 * We have no pacing set or we
14080 * are using old-style rack or
14081 * we are overriden to use the old 1ms pacing.
14082 */
14083 slot = rack->r_ctl.rc_min_to * HPTS_USEC_IN_MSEC;
14084 }
14085 }
14086 if (slot) {
14087 /* set the rack tcb into the slot N */
14088 counter_u64_add(rack_paced_segments, 1);
14089 } else if (sendalot) {
14090 if (len)
14091 counter_u64_add(rack_unpaced_segments, 1);
14092 sack_rxmit = 0;
14093 goto again;
14094 } else if (len) {
14095 counter_u64_add(rack_unpaced_segments, 1);
14096 }
14097 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0);
14098 return (error);
14099 }
14100
14101 static void
rack_update_seg(struct tcp_rack * rack)14102 rack_update_seg(struct tcp_rack *rack)
14103 {
14104 uint32_t orig_val;
14105
14106 orig_val = rack->r_ctl.rc_pace_max_segs;
14107 rack_set_pace_segments(rack->rc_tp, rack, __LINE__);
14108 if (orig_val != rack->r_ctl.rc_pace_max_segs)
14109 rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL);
14110 }
14111
14112 /*
14113 * rack_ctloutput() must drop the inpcb lock before performing copyin on
14114 * socket option arguments. When it re-acquires the lock after the copy, it
14115 * has to revalidate that the connection is still valid for the socket
14116 * option.
14117 */
14118 static int
rack_set_sockopt(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp,struct tcp_rack * rack)14119 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
14120 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
14121 {
14122 struct epoch_tracker et;
14123 uint64_t val;
14124 int32_t error = 0, optval;
14125 uint16_t ca, ss;
14126
14127 switch (sopt->sopt_name) {
14128 case TCP_RACK_PROP_RATE: /* URL:prop_rate */
14129 case TCP_RACK_PROP : /* URL:prop */
14130 case TCP_RACK_TLP_REDUCE: /* URL:tlp_reduce */
14131 case TCP_RACK_EARLY_RECOV: /* URL:early_recov */
14132 case TCP_RACK_PACE_REDUCE: /* Not used */
14133 /* Pacing related ones */
14134 case TCP_RACK_PACE_ALWAYS: /* URL:pace_always */
14135 case TCP_BBR_RACK_INIT_RATE: /* URL:irate */
14136 case TCP_BBR_IWINTSO: /* URL:tso_iwin */
14137 case TCP_RACK_PACE_MAX_SEG: /* URL:pace_max_seg */
14138 case TCP_RACK_FORCE_MSEG: /* URL:force_max_seg */
14139 case TCP_RACK_PACE_RATE_CA: /* URL:pr_ca */
14140 case TCP_RACK_PACE_RATE_SS: /* URL:pr_ss*/
14141 case TCP_RACK_PACE_RATE_REC: /* URL:pr_rec */
14142 case TCP_RACK_GP_INCREASE_CA: /* URL:gp_inc_ca */
14143 case TCP_RACK_GP_INCREASE_SS: /* URL:gp_inc_ss */
14144 case TCP_RACK_GP_INCREASE_REC: /* URL:gp_inc_rec */
14145 case TCP_RACK_RR_CONF: /* URL:rrr_conf */
14146 case TCP_BBR_HDWR_PACE: /* URL:hdwrpace */
14147 /* End pacing related */
14148 case TCP_DELACK:
14149 case TCP_RACK_PRR_SENDALOT: /* URL:prr_sendalot */
14150 case TCP_RACK_MIN_TO: /* URL:min_to */
14151 case TCP_RACK_EARLY_SEG: /* URL:early_seg */
14152 case TCP_RACK_REORD_THRESH: /* URL:reord_thresh */
14153 case TCP_RACK_REORD_FADE: /* URL:reord_fade */
14154 case TCP_RACK_TLP_THRESH: /* URL:tlp_thresh */
14155 case TCP_RACK_PKT_DELAY: /* URL:pkt_delay */
14156 case TCP_RACK_TLP_USE: /* URL:tlp_use */
14157 case TCP_RACK_TLP_INC_VAR: /* URL:tlp_inc_var */
14158 case TCP_RACK_IDLE_REDUCE_HIGH: /* URL:idle_reduce_high */
14159 case TCP_BBR_RACK_RTT_USE: /* URL:rttuse */
14160 case TCP_BBR_USE_RACK_RR: /* URL:rackrr */
14161 case TCP_RACK_DO_DETECTION: /* URL:detect */
14162 case TCP_NO_PRR: /* URL:noprr */
14163 case TCP_TIMELY_DYN_ADJ: /* URL:dynamic */
14164 case TCP_DATA_AFTER_CLOSE:
14165 case TCP_RACK_NONRXT_CFG_RATE: /* URL:nonrxtcr */
14166 case TCP_SHARED_CWND_ENABLE: /* URL:scwnd */
14167 case TCP_RACK_MBUF_QUEUE: /* URL:mqueue */
14168 case TCP_RACK_NO_PUSH_AT_MAX: /* URL:npush */
14169 case TCP_RACK_PACE_TO_FILL: /* URL:fillcw */
14170 case TCP_SHARED_CWND_TIME_LIMIT: /* URL:lscwnd */
14171 case TCP_RACK_PROFILE: /* URL:profile */
14172 break;
14173 default:
14174 return (tcp_default_ctloutput(so, sopt, inp, tp));
14175 break;
14176 }
14177 INP_WUNLOCK(inp);
14178 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
14179 if (error)
14180 return (error);
14181 INP_WLOCK(inp);
14182 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14183 INP_WUNLOCK(inp);
14184 return (ECONNRESET);
14185 }
14186 tp = intotcpcb(inp);
14187 rack = (struct tcp_rack *)tp->t_fb_ptr;
14188 switch (sopt->sopt_name) {
14189 case TCP_RACK_PROFILE:
14190 RACK_OPTS_INC(tcp_profile);
14191 if (optval == 1) {
14192 /* pace_always=1 */
14193 rack->rc_always_pace = 1;
14194 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
14195 /* scwnd=1 */
14196 rack->rack_enable_scwnd = 1;
14197 /* dynamic=100 */
14198 rack->rc_gp_dyn_mul = 1;
14199 rack->r_ctl.rack_per_of_gp_ca = 100;
14200 /* rrr_conf=3 */
14201 rack->r_rr_config = 3;
14202 /* npush=2 */
14203 rack->r_ctl.rc_no_push_at_mrtt = 2;
14204 /* fillcw=1 */
14205 rack->rc_pace_to_cwnd = 1;
14206 rack->rc_pace_fill_if_rttin_range = 0;
14207 rack->rtt_limit_mul = 0;
14208 /* noprr=1 */
14209 rack->rack_no_prr = 1;
14210 /* lscwnd=1 */
14211 rack->r_limit_scw = 1;
14212 } else if (optval == 2) {
14213 /* pace_always=1 */
14214 rack->rc_always_pace = 1;
14215 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
14216 /* scwnd=1 */
14217 rack->rack_enable_scwnd = 1;
14218 /* dynamic=100 */
14219 rack->rc_gp_dyn_mul = 1;
14220 rack->r_ctl.rack_per_of_gp_ca = 100;
14221 /* rrr_conf=3 */
14222 rack->r_rr_config = 3;
14223 /* npush=2 */
14224 rack->r_ctl.rc_no_push_at_mrtt = 2;
14225 /* fillcw=1 */
14226 rack->rc_pace_to_cwnd = 1;
14227 rack->rc_pace_fill_if_rttin_range = 0;
14228 rack->rtt_limit_mul = 0;
14229 /* noprr=1 */
14230 rack->rack_no_prr = 1;
14231 /* lscwnd=0 */
14232 rack->r_limit_scw = 0;
14233 }
14234 break;
14235 case TCP_SHARED_CWND_TIME_LIMIT:
14236 RACK_OPTS_INC(tcp_lscwnd);
14237 if (optval)
14238 rack->r_limit_scw = 1;
14239 else
14240 rack->r_limit_scw = 0;
14241 break;
14242 case TCP_RACK_PACE_TO_FILL:
14243 RACK_OPTS_INC(tcp_fillcw);
14244 if (optval == 0)
14245 rack->rc_pace_to_cwnd = 0;
14246 else
14247 rack->rc_pace_to_cwnd = 1;
14248 if ((optval >= rack_gp_rtt_maxmul) &&
14249 rack_gp_rtt_maxmul &&
14250 (optval < 0xf)) {
14251 rack->rc_pace_fill_if_rttin_range = 1;
14252 rack->rtt_limit_mul = optval;
14253 } else {
14254 rack->rc_pace_fill_if_rttin_range = 0;
14255 rack->rtt_limit_mul = 0;
14256 }
14257 break;
14258 case TCP_RACK_NO_PUSH_AT_MAX:
14259 RACK_OPTS_INC(tcp_npush);
14260 if (optval == 0)
14261 rack->r_ctl.rc_no_push_at_mrtt = 0;
14262 else if (optval < 0xff)
14263 rack->r_ctl.rc_no_push_at_mrtt = optval;
14264 else
14265 error = EINVAL;
14266 break;
14267 case TCP_SHARED_CWND_ENABLE:
14268 RACK_OPTS_INC(tcp_rack_scwnd);
14269 if (optval == 0)
14270 rack->rack_enable_scwnd = 0;
14271 else
14272 rack->rack_enable_scwnd = 1;
14273 break;
14274 case TCP_RACK_MBUF_QUEUE:
14275 /* Now do we use the LRO mbuf-queue feature */
14276 RACK_OPTS_INC(tcp_rack_mbufq);
14277 if (optval)
14278 rack->r_mbuf_queue = 1;
14279 else
14280 rack->r_mbuf_queue = 0;
14281 if (rack->r_mbuf_queue || rack->rc_always_pace)
14282 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
14283 else
14284 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
14285 break;
14286 case TCP_RACK_NONRXT_CFG_RATE:
14287 RACK_OPTS_INC(tcp_rack_cfg_rate);
14288 if (optval == 0)
14289 rack->rack_rec_nonrxt_use_cr = 0;
14290 else
14291 rack->rack_rec_nonrxt_use_cr = 1;
14292 break;
14293 case TCP_NO_PRR:
14294 RACK_OPTS_INC(tcp_rack_noprr);
14295 if (optval == 0)
14296 rack->rack_no_prr = 0;
14297 else
14298 rack->rack_no_prr = 1;
14299 break;
14300 case TCP_TIMELY_DYN_ADJ:
14301 RACK_OPTS_INC(tcp_timely_dyn);
14302 if (optval == 0)
14303 rack->rc_gp_dyn_mul = 0;
14304 else {
14305 rack->rc_gp_dyn_mul = 1;
14306 if (optval >= 100) {
14307 /*
14308 * If the user sets something 100 or more
14309 * its the gp_ca value.
14310 */
14311 rack->r_ctl.rack_per_of_gp_ca = optval;
14312 }
14313 }
14314 break;
14315 case TCP_RACK_DO_DETECTION:
14316 RACK_OPTS_INC(tcp_rack_do_detection);
14317 if (optval == 0)
14318 rack->do_detection = 0;
14319 else
14320 rack->do_detection = 1;
14321 break;
14322 case TCP_RACK_PROP_RATE:
14323 if ((optval <= 0) || (optval >= 100)) {
14324 error = EINVAL;
14325 break;
14326 }
14327 RACK_OPTS_INC(tcp_rack_prop_rate);
14328 rack->r_ctl.rc_prop_rate = optval;
14329 break;
14330 case TCP_RACK_TLP_USE:
14331 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
14332 error = EINVAL;
14333 break;
14334 }
14335 RACK_OPTS_INC(tcp_tlp_use);
14336 rack->rack_tlp_threshold_use = optval;
14337 break;
14338 case TCP_RACK_PROP:
14339 /* RACK proportional rate reduction (bool) */
14340 RACK_OPTS_INC(tcp_rack_prop);
14341 rack->r_ctl.rc_prop_reduce = optval;
14342 break;
14343 case TCP_RACK_TLP_REDUCE:
14344 /* RACK TLP cwnd reduction (bool) */
14345 RACK_OPTS_INC(tcp_rack_tlp_reduce);
14346 rack->r_ctl.rc_tlp_cwnd_reduce = optval;
14347 break;
14348 case TCP_RACK_EARLY_RECOV:
14349 /* Should recovery happen early (bool) */
14350 RACK_OPTS_INC(tcp_rack_early_recov);
14351 rack->r_ctl.rc_early_recovery = optval;
14352 break;
14353
14354 /* Pacing related ones */
14355 case TCP_RACK_PACE_ALWAYS:
14356 /*
14357 * zero is old rack method, 1 is new
14358 * method using a pacing rate.
14359 */
14360 RACK_OPTS_INC(tcp_rack_pace_always);
14361 if (optval > 0)
14362 rack->rc_always_pace = 1;
14363 else
14364 rack->rc_always_pace = 0;
14365 if (rack->r_mbuf_queue || rack->rc_always_pace)
14366 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ;
14367 else
14368 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ;
14369 /* A rate may be set irate or other, if so set seg size */
14370 rack_update_seg(rack);
14371 break;
14372 case TCP_BBR_RACK_INIT_RATE:
14373 RACK_OPTS_INC(tcp_initial_rate);
14374 val = optval;
14375 /* Change from kbits per second to bytes per second */
14376 val *= 1000;
14377 val /= 8;
14378 rack->r_ctl.init_rate = val;
14379 if (rack->rc_init_win != rack_default_init_window) {
14380 uint32_t win, snt;
14381
14382 /*
14383 * Options don't always get applied
14384 * in the order you think. So in order
14385 * to assure we update a cwnd we need
14386 * to check and see if we are still
14387 * where we should raise the cwnd.
14388 */
14389 win = rc_init_window(rack);
14390 if (SEQ_GT(tp->snd_max, tp->iss))
14391 snt = tp->snd_max - tp->iss;
14392 else
14393 snt = 0;
14394 if ((snt < win) &&
14395 (tp->snd_cwnd < win))
14396 tp->snd_cwnd = win;
14397 }
14398 if (rack->rc_always_pace)
14399 rack_update_seg(rack);
14400 break;
14401 case TCP_BBR_IWINTSO:
14402 RACK_OPTS_INC(tcp_initial_win);
14403 if (optval && (optval <= 0xff)) {
14404 uint32_t win, snt;
14405
14406 rack->rc_init_win = optval;
14407 win = rc_init_window(rack);
14408 if (SEQ_GT(tp->snd_max, tp->iss))
14409 snt = tp->snd_max - tp->iss;
14410 else
14411 snt = 0;
14412 if ((snt < win) &&
14413 (tp->t_srtt |
14414 #ifdef NETFLIX_PEAKRATE
14415 tp->t_maxpeakrate |
14416 #endif
14417 rack->r_ctl.init_rate)) {
14418 /*
14419 * We are not past the initial window
14420 * and we have some bases for pacing,
14421 * so we need to possibly adjust up
14422 * the cwnd. Note even if we don't set
14423 * the cwnd, its still ok to raise the rc_init_win
14424 * which can be used coming out of idle when we
14425 * would have a rate.
14426 */
14427 if (tp->snd_cwnd < win)
14428 tp->snd_cwnd = win;
14429 }
14430 if (rack->rc_always_pace)
14431 rack_update_seg(rack);
14432 } else
14433 error = EINVAL;
14434 break;
14435 case TCP_RACK_FORCE_MSEG:
14436 RACK_OPTS_INC(tcp_rack_force_max_seg);
14437 if (optval)
14438 rack->rc_force_max_seg = 1;
14439 else
14440 rack->rc_force_max_seg = 0;
14441 break;
14442 case TCP_RACK_PACE_MAX_SEG:
14443 /* Max segments size in a pace in bytes */
14444 RACK_OPTS_INC(tcp_rack_max_seg);
14445 rack->rc_user_set_max_segs = optval;
14446 rack_set_pace_segments(tp, rack, __LINE__);
14447 break;
14448 case TCP_RACK_PACE_RATE_REC:
14449 /* Set the fixed pacing rate in Bytes per second ca */
14450 RACK_OPTS_INC(tcp_rack_pace_rate_rec);
14451 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
14452 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
14453 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
14454 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
14455 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
14456 rack->use_fixed_rate = 1;
14457 rack_log_pacing_delay_calc(rack,
14458 rack->r_ctl.rc_fixed_pacing_rate_ss,
14459 rack->r_ctl.rc_fixed_pacing_rate_ca,
14460 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
14461 __LINE__, NULL);
14462 break;
14463
14464 case TCP_RACK_PACE_RATE_SS:
14465 /* Set the fixed pacing rate in Bytes per second ca */
14466 RACK_OPTS_INC(tcp_rack_pace_rate_ss);
14467 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
14468 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0)
14469 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
14470 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
14471 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
14472 rack->use_fixed_rate = 1;
14473 rack_log_pacing_delay_calc(rack,
14474 rack->r_ctl.rc_fixed_pacing_rate_ss,
14475 rack->r_ctl.rc_fixed_pacing_rate_ca,
14476 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
14477 __LINE__, NULL);
14478 break;
14479
14480 case TCP_RACK_PACE_RATE_CA:
14481 /* Set the fixed pacing rate in Bytes per second ca */
14482 RACK_OPTS_INC(tcp_rack_pace_rate_ca);
14483 rack->r_ctl.rc_fixed_pacing_rate_ca = optval;
14484 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0)
14485 rack->r_ctl.rc_fixed_pacing_rate_ss = optval;
14486 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0)
14487 rack->r_ctl.rc_fixed_pacing_rate_rec = optval;
14488 rack->use_fixed_rate = 1;
14489 rack_log_pacing_delay_calc(rack,
14490 rack->r_ctl.rc_fixed_pacing_rate_ss,
14491 rack->r_ctl.rc_fixed_pacing_rate_ca,
14492 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8,
14493 __LINE__, NULL);
14494 break;
14495 case TCP_RACK_GP_INCREASE_REC:
14496 RACK_OPTS_INC(tcp_gp_inc_rec);
14497 rack->r_ctl.rack_per_of_gp_rec = optval;
14498 rack_log_pacing_delay_calc(rack,
14499 rack->r_ctl.rack_per_of_gp_ss,
14500 rack->r_ctl.rack_per_of_gp_ca,
14501 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
14502 __LINE__, NULL);
14503 break;
14504 case TCP_RACK_GP_INCREASE_CA:
14505 RACK_OPTS_INC(tcp_gp_inc_ca);
14506 ca = optval;
14507 if (ca < 100) {
14508 /*
14509 * We don't allow any reduction
14510 * over the GP b/w.
14511 */
14512 error = EINVAL;
14513 break;
14514 }
14515 rack->r_ctl.rack_per_of_gp_ca = ca;
14516 rack_log_pacing_delay_calc(rack,
14517 rack->r_ctl.rack_per_of_gp_ss,
14518 rack->r_ctl.rack_per_of_gp_ca,
14519 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
14520 __LINE__, NULL);
14521 break;
14522 case TCP_RACK_GP_INCREASE_SS:
14523 RACK_OPTS_INC(tcp_gp_inc_ss);
14524 ss = optval;
14525 if (ss < 100) {
14526 /*
14527 * We don't allow any reduction
14528 * over the GP b/w.
14529 */
14530 error = EINVAL;
14531 break;
14532 }
14533 rack->r_ctl.rack_per_of_gp_ss = ss;
14534 rack_log_pacing_delay_calc(rack,
14535 rack->r_ctl.rack_per_of_gp_ss,
14536 rack->r_ctl.rack_per_of_gp_ca,
14537 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1,
14538 __LINE__, NULL);
14539 break;
14540 case TCP_RACK_RR_CONF:
14541 RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate);
14542 if (optval && optval <= 3)
14543 rack->r_rr_config = optval;
14544 else
14545 rack->r_rr_config = 0;
14546 break;
14547 case TCP_BBR_HDWR_PACE:
14548 RACK_OPTS_INC(tcp_hdwr_pacing);
14549 if (optval){
14550 if (rack->rack_hdrw_pacing == 0) {
14551 rack->rack_hdw_pace_ena = 1;
14552 rack->rack_attempt_hdwr_pace = 0;
14553 } else
14554 error = EALREADY;
14555 } else {
14556 rack->rack_hdw_pace_ena = 0;
14557 #ifdef RATELIMIT
14558 if (rack->rack_hdrw_pacing) {
14559 rack->rack_hdrw_pacing = 0;
14560 in_pcbdetach_txrtlmt(rack->rc_inp);
14561 }
14562 #endif
14563 }
14564 break;
14565 /* End Pacing related ones */
14566 case TCP_RACK_PRR_SENDALOT:
14567 /* Allow PRR to send more than one seg */
14568 RACK_OPTS_INC(tcp_rack_prr_sendalot);
14569 rack->r_ctl.rc_prr_sendalot = optval;
14570 break;
14571 case TCP_RACK_MIN_TO:
14572 /* Minimum time between rack t-o's in ms */
14573 RACK_OPTS_INC(tcp_rack_min_to);
14574 rack->r_ctl.rc_min_to = optval;
14575 break;
14576 case TCP_RACK_EARLY_SEG:
14577 /* If early recovery max segments */
14578 RACK_OPTS_INC(tcp_rack_early_seg);
14579 rack->r_ctl.rc_early_recovery_segs = optval;
14580 break;
14581 case TCP_RACK_REORD_THRESH:
14582 /* RACK reorder threshold (shift amount) */
14583 RACK_OPTS_INC(tcp_rack_reord_thresh);
14584 if ((optval > 0) && (optval < 31))
14585 rack->r_ctl.rc_reorder_shift = optval;
14586 else
14587 error = EINVAL;
14588 break;
14589 case TCP_RACK_REORD_FADE:
14590 /* Does reordering fade after ms time */
14591 RACK_OPTS_INC(tcp_rack_reord_fade);
14592 rack->r_ctl.rc_reorder_fade = optval;
14593 break;
14594 case TCP_RACK_TLP_THRESH:
14595 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14596 RACK_OPTS_INC(tcp_rack_tlp_thresh);
14597 if (optval)
14598 rack->r_ctl.rc_tlp_threshold = optval;
14599 else
14600 error = EINVAL;
14601 break;
14602 case TCP_BBR_USE_RACK_RR:
14603 RACK_OPTS_INC(tcp_rack_rr);
14604 if (optval)
14605 rack->use_rack_rr = 1;
14606 else
14607 rack->use_rack_rr = 0;
14608 break;
14609 case TCP_RACK_PKT_DELAY:
14610 /* RACK added ms i.e. rack-rtt + reord + N */
14611 RACK_OPTS_INC(tcp_rack_pkt_delay);
14612 rack->r_ctl.rc_pkt_delay = optval;
14613 break;
14614 case TCP_RACK_TLP_INC_VAR:
14615 /* Does TLP include rtt variance in t-o */
14616 error = EINVAL;
14617 break;
14618 case TCP_RACK_IDLE_REDUCE_HIGH:
14619 error = EINVAL;
14620 break;
14621 case TCP_DELACK:
14622 if (optval == 0)
14623 tp->t_delayed_ack = 0;
14624 else
14625 tp->t_delayed_ack = 1;
14626 if (tp->t_flags & TF_DELACK) {
14627 tp->t_flags &= ~TF_DELACK;
14628 tp->t_flags |= TF_ACKNOW;
14629 NET_EPOCH_ENTER(et);
14630 rack_output(tp);
14631 NET_EPOCH_EXIT(et);
14632 }
14633 break;
14634
14635 case TCP_BBR_RACK_RTT_USE:
14636 if ((optval != USE_RTT_HIGH) &&
14637 (optval != USE_RTT_LOW) &&
14638 (optval != USE_RTT_AVG))
14639 error = EINVAL;
14640 else
14641 rack->r_ctl.rc_rate_sample_method = optval;
14642 break;
14643 case TCP_DATA_AFTER_CLOSE:
14644 if (optval)
14645 rack->rc_allow_data_af_clo = 1;
14646 else
14647 rack->rc_allow_data_af_clo = 0;
14648 break;
14649 case TCP_RACK_PACE_REDUCE:
14650 /* sysctl only now */
14651 error = EINVAL;
14652 break;
14653 default:
14654 return (tcp_default_ctloutput(so, sopt, inp, tp));
14655 break;
14656 }
14657 #ifdef NETFLIX_STATS
14658 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
14659 #endif
14660 INP_WUNLOCK(inp);
14661 return (error);
14662 }
14663
14664 static int
rack_get_sockopt(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp,struct tcp_rack * rack)14665 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
14666 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
14667 {
14668 int32_t error, optval;
14669 uint64_t val;
14670 /*
14671 * Because all our options are either boolean or an int, we can just
14672 * pull everything into optval and then unlock and copy. If we ever
14673 * add a option that is not a int, then this will have quite an
14674 * impact to this routine.
14675 */
14676 error = 0;
14677 switch (sopt->sopt_name) {
14678 case TCP_RACK_PROFILE:
14679 /* You cannot retrieve a profile, its write only */
14680 error = EINVAL;
14681 break;
14682 case TCP_RACK_PACE_TO_FILL:
14683 optval = rack->rc_pace_to_cwnd;
14684 break;
14685 case TCP_RACK_NO_PUSH_AT_MAX:
14686 optval = rack->r_ctl.rc_no_push_at_mrtt;
14687 break;
14688 case TCP_SHARED_CWND_ENABLE:
14689 optval = rack->rack_enable_scwnd;
14690 break;
14691 case TCP_RACK_NONRXT_CFG_RATE:
14692 optval = rack->rack_rec_nonrxt_use_cr;
14693 break;
14694 case TCP_NO_PRR:
14695 optval = rack->rack_no_prr;
14696 break;
14697 case TCP_RACK_DO_DETECTION:
14698 optval = rack->do_detection;
14699 break;
14700 case TCP_RACK_MBUF_QUEUE:
14701 /* Now do we use the LRO mbuf-queue feature */
14702 optval = rack->r_mbuf_queue;
14703 break;
14704 case TCP_TIMELY_DYN_ADJ:
14705 optval = rack->rc_gp_dyn_mul;
14706 break;
14707 case TCP_BBR_IWINTSO:
14708 optval = rack->rc_init_win;
14709 break;
14710 case TCP_RACK_PROP_RATE:
14711 optval = rack->r_ctl.rc_prop_rate;
14712 break;
14713 case TCP_RACK_PROP:
14714 /* RACK proportional rate reduction (bool) */
14715 optval = rack->r_ctl.rc_prop_reduce;
14716 break;
14717 case TCP_RACK_TLP_REDUCE:
14718 /* RACK TLP cwnd reduction (bool) */
14719 optval = rack->r_ctl.rc_tlp_cwnd_reduce;
14720 break;
14721 case TCP_RACK_EARLY_RECOV:
14722 /* Should recovery happen early (bool) */
14723 optval = rack->r_ctl.rc_early_recovery;
14724 break;
14725 case TCP_RACK_PACE_REDUCE:
14726 /* RACK Hptsi reduction factor (divisor) */
14727 error = EINVAL;
14728 break;
14729 case TCP_BBR_RACK_INIT_RATE:
14730 val = rack->r_ctl.init_rate;
14731 /* convert to kbits per sec */
14732 val *= 8;
14733 val /= 1000;
14734 optval = (uint32_t)val;
14735 break;
14736 case TCP_RACK_FORCE_MSEG:
14737 optval = rack->rc_force_max_seg;
14738 break;
14739 case TCP_RACK_PACE_MAX_SEG:
14740 /* Max segments in a pace */
14741 optval = rack->rc_user_set_max_segs;
14742 break;
14743 case TCP_RACK_PACE_ALWAYS:
14744 /* Use the always pace method */
14745 optval = rack->rc_always_pace;
14746 break;
14747 case TCP_RACK_PRR_SENDALOT:
14748 /* Allow PRR to send more than one seg */
14749 optval = rack->r_ctl.rc_prr_sendalot;
14750 break;
14751 case TCP_RACK_MIN_TO:
14752 /* Minimum time between rack t-o's in ms */
14753 optval = rack->r_ctl.rc_min_to;
14754 break;
14755 case TCP_RACK_EARLY_SEG:
14756 /* If early recovery max segments */
14757 optval = rack->r_ctl.rc_early_recovery_segs;
14758 break;
14759 case TCP_RACK_REORD_THRESH:
14760 /* RACK reorder threshold (shift amount) */
14761 optval = rack->r_ctl.rc_reorder_shift;
14762 break;
14763 case TCP_RACK_REORD_FADE:
14764 /* Does reordering fade after ms time */
14765 optval = rack->r_ctl.rc_reorder_fade;
14766 break;
14767 case TCP_BBR_USE_RACK_RR:
14768 /* Do we use the rack cheat for rxt */
14769 optval = rack->use_rack_rr;
14770 break;
14771 case TCP_RACK_RR_CONF:
14772 optval = rack->r_rr_config;
14773 break;
14774 case TCP_BBR_HDWR_PACE:
14775 optval = rack->rack_hdw_pace_ena;
14776 break;
14777 case TCP_RACK_TLP_THRESH:
14778 /* RACK TLP theshold i.e. srtt+(srtt/N) */
14779 optval = rack->r_ctl.rc_tlp_threshold;
14780 break;
14781 case TCP_RACK_PKT_DELAY:
14782 /* RACK added ms i.e. rack-rtt + reord + N */
14783 optval = rack->r_ctl.rc_pkt_delay;
14784 break;
14785 case TCP_RACK_TLP_USE:
14786 optval = rack->rack_tlp_threshold_use;
14787 break;
14788 case TCP_RACK_TLP_INC_VAR:
14789 /* Does TLP include rtt variance in t-o */
14790 error = EINVAL;
14791 break;
14792 case TCP_RACK_IDLE_REDUCE_HIGH:
14793 error = EINVAL;
14794 break;
14795 case TCP_RACK_PACE_RATE_CA:
14796 optval = rack->r_ctl.rc_fixed_pacing_rate_ca;
14797 break;
14798 case TCP_RACK_PACE_RATE_SS:
14799 optval = rack->r_ctl.rc_fixed_pacing_rate_ss;
14800 break;
14801 case TCP_RACK_PACE_RATE_REC:
14802 optval = rack->r_ctl.rc_fixed_pacing_rate_rec;
14803 break;
14804 case TCP_RACK_GP_INCREASE_SS:
14805 optval = rack->r_ctl.rack_per_of_gp_ca;
14806 break;
14807 case TCP_RACK_GP_INCREASE_CA:
14808 optval = rack->r_ctl.rack_per_of_gp_ss;
14809 break;
14810 case TCP_BBR_RACK_RTT_USE:
14811 optval = rack->r_ctl.rc_rate_sample_method;
14812 break;
14813 case TCP_DELACK:
14814 optval = tp->t_delayed_ack;
14815 break;
14816 case TCP_DATA_AFTER_CLOSE:
14817 optval = rack->rc_allow_data_af_clo;
14818 break;
14819 case TCP_SHARED_CWND_TIME_LIMIT:
14820 optval = rack->r_limit_scw;
14821 break;
14822 default:
14823 return (tcp_default_ctloutput(so, sopt, inp, tp));
14824 break;
14825 }
14826 INP_WUNLOCK(inp);
14827 if (error == 0) {
14828 error = sooptcopyout(sopt, &optval, sizeof optval);
14829 }
14830 return (error);
14831 }
14832
14833 static int
rack_ctloutput(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp)14834 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
14835 {
14836 int32_t error = EINVAL;
14837 struct tcp_rack *rack;
14838
14839 rack = (struct tcp_rack *)tp->t_fb_ptr;
14840 if (rack == NULL) {
14841 /* Huh? */
14842 goto out;
14843 }
14844 if (sopt->sopt_dir == SOPT_SET) {
14845 return (rack_set_sockopt(so, sopt, inp, tp, rack));
14846 } else if (sopt->sopt_dir == SOPT_GET) {
14847 return (rack_get_sockopt(so, sopt, inp, tp, rack));
14848 }
14849 out:
14850 INP_WUNLOCK(inp);
14851 return (error);
14852 }
14853
14854 static int
rack_pru_options(struct tcpcb * tp,int flags)14855 rack_pru_options(struct tcpcb *tp, int flags)
14856 {
14857 if (flags & PRUS_OOB)
14858 return (EOPNOTSUPP);
14859 return (0);
14860 }
14861
14862 static struct tcp_function_block __tcp_rack = {
14863 .tfb_tcp_block_name = __XSTRING(STACKNAME),
14864 .tfb_tcp_output = rack_output,
14865 .tfb_do_queued_segments = ctf_do_queued_segments,
14866 .tfb_do_segment_nounlock = rack_do_segment_nounlock,
14867 .tfb_tcp_do_segment = rack_do_segment,
14868 .tfb_tcp_ctloutput = rack_ctloutput,
14869 .tfb_tcp_fb_init = rack_init,
14870 .tfb_tcp_fb_fini = rack_fini,
14871 .tfb_tcp_timer_stop_all = rack_stopall,
14872 .tfb_tcp_timer_activate = rack_timer_activate,
14873 .tfb_tcp_timer_active = rack_timer_active,
14874 .tfb_tcp_timer_stop = rack_timer_stop,
14875 .tfb_tcp_rexmit_tmr = rack_remxt_tmr,
14876 .tfb_tcp_handoff_ok = rack_handoff_ok,
14877 .tfb_pru_options = rack_pru_options,
14878 };
14879
14880 static const char *rack_stack_names[] = {
14881 __XSTRING(STACKNAME),
14882 #ifdef STACKALIAS
14883 __XSTRING(STACKALIAS),
14884 #endif
14885 };
14886
14887 static int
rack_ctor(void * mem,int32_t size,void * arg,int32_t how)14888 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
14889 {
14890 memset(mem, 0, size);
14891 return (0);
14892 }
14893
14894 static void
rack_dtor(void * mem,int32_t size,void * arg)14895 rack_dtor(void *mem, int32_t size, void *arg)
14896 {
14897
14898 }
14899
14900 static bool rack_mod_inited = false;
14901
14902 static int
tcp_addrack(module_t mod,int32_t type,void * data)14903 tcp_addrack(module_t mod, int32_t type, void *data)
14904 {
14905 int32_t err = 0;
14906 int num_stacks;
14907
14908 switch (type) {
14909 case MOD_LOAD:
14910 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
14911 sizeof(struct rack_sendmap),
14912 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
14913
14914 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
14915 sizeof(struct tcp_rack),
14916 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
14917
14918 sysctl_ctx_init(&rack_sysctl_ctx);
14919 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
14920 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
14921 OID_AUTO,
14922 #ifdef STACKALIAS
14923 __XSTRING(STACKALIAS),
14924 #else
14925 __XSTRING(STACKNAME),
14926 #endif
14927 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
14928 "");
14929 if (rack_sysctl_root == NULL) {
14930 printf("Failed to add sysctl node\n");
14931 err = EFAULT;
14932 goto free_uma;
14933 }
14934 rack_init_sysctls();
14935 num_stacks = nitems(rack_stack_names);
14936 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
14937 rack_stack_names, &num_stacks);
14938 if (err) {
14939 printf("Failed to register %s stack name for "
14940 "%s module\n", rack_stack_names[num_stacks],
14941 __XSTRING(MODNAME));
14942 sysctl_ctx_free(&rack_sysctl_ctx);
14943 free_uma:
14944 uma_zdestroy(rack_zone);
14945 uma_zdestroy(rack_pcb_zone);
14946 rack_counter_destroy();
14947 printf("Failed to register rack module -- err:%d\n", err);
14948 return (err);
14949 }
14950 tcp_lro_reg_mbufq();
14951 rack_mod_inited = true;
14952 break;
14953 case MOD_QUIESCE:
14954 err = deregister_tcp_functions(&__tcp_rack, true, false);
14955 break;
14956 case MOD_UNLOAD:
14957 err = deregister_tcp_functions(&__tcp_rack, false, true);
14958 if (err == EBUSY)
14959 break;
14960 if (rack_mod_inited) {
14961 uma_zdestroy(rack_zone);
14962 uma_zdestroy(rack_pcb_zone);
14963 sysctl_ctx_free(&rack_sysctl_ctx);
14964 rack_counter_destroy();
14965 rack_mod_inited = false;
14966 }
14967 tcp_lro_dereg_mbufq();
14968 err = 0;
14969 break;
14970 default:
14971 return (EOPNOTSUPP);
14972 }
14973 return (err);
14974 }
14975
14976 static moduledata_t tcp_rack = {
14977 .name = __XSTRING(MODNAME),
14978 .evhand = tcp_addrack,
14979 .priv = 0
14980 };
14981
14982 MODULE_VERSION(MODNAME, 1);
14983 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
14984 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);
14985