2 * Copyright (c) 2016-2018
3 * Netflix Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
33 #include "opt_ipsec.h"
34 #include "opt_tcpdebug.h"
36 #include <sys/param.h>
37 #include <sys/module.h>
38 #include <sys/kernel.h>
40 #include <sys/hhook.h>
43 #include <sys/malloc.h>
45 #include <sys/mutex.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>
53 #include <sys/stats.h>
55 #include <sys/refcount.h>
56 #include <sys/queue.h>
58 #include <sys/kthread.h>
59 #include <sys/kern_prefetch.h>
63 #include <net/route.h>
66 #define TCPSTATES /* for logging */
68 #include <netinet/in.h>
69 #include <netinet/in_kdtrace.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
73 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
74 #include <netinet/ip_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/in6_pcb.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet/tcp.h>
80 #include <netinet/tcp_fsm.h>
81 #include <netinet/tcp_log_buf.h>
82 #include <netinet/tcp_seq.h>
83 #include <netinet/tcp_timer.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/tcp_hpts.h>
86 #include <netinet/tcpip.h>
87 #include <netinet/cc/cc.h>
89 #include <netinet/tcp_newcwv.h>
91 #include <netinet/tcp_fastopen.h>
93 #include <netinet/tcp_debug.h>
96 #include <netinet/tcp_offload.h>
99 #include <netinet6/tcp6_var.h>
102 #include <netipsec/ipsec_support.h>
104 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
105 #include <netipsec/ipsec.h>
106 #include <netipsec/ipsec6.h>
109 #include <netinet/udp.h>
110 #include <netinet/udp_var.h>
111 #include <machine/in_cksum.h>
114 #include <security/mac/mac_framework.h>
116 #include "sack_filter.h"
117 #include "tcp_rack.h"
118 #include "rack_bbr_common.h"
120 uma_zone_t rack_zone;
121 uma_zone_t rack_pcb_zone;
124 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t)))
127 struct sysctl_ctx_list rack_sysctl_ctx;
128 struct sysctl_oid *rack_sysctl_root;
134 * The RACK module incorporates a number of
135 * TCP ideas that have been put out into the IETF
136 * over the last few years:
137 * - Matt Mathis's Rate Halving which slowly drops
138 * the congestion window so that the ack clock can
139 * be maintained during a recovery.
140 * - Yuchung Cheng's RACK TCP (for which its named) that
141 * will stop us using the number of dup acks and instead
142 * use time as the gage of when we retransmit.
143 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft
144 * of Dukkipati et.al.
145 * RACK depends on SACK, so if an endpoint arrives that
146 * cannot do SACK the state machine below will shuttle the
147 * connection back to using the "default" TCP stack that is
150 * To implement RACK the original TCP stack was first decomposed
151 * into a functional state machine with individual states
152 * for each of the possible TCP connection states. The do_segement
153 * functions role in life is to mandate the connection supports SACK
154 * initially and then assure that the RACK state matches the conenction
155 * state before calling the states do_segment function. Each
156 * state is simplified due to the fact that the original do_segment
157 * has been decomposed and we *know* what state we are in (no
158 * switches on the state) and all tests for SACK are gone. This
159 * greatly simplifies what each state does.
161 * TCP output is also over-written with a new version since it
162 * must maintain the new rack scoreboard.
165 static int32_t rack_precache = 1;
166 static int32_t rack_tlp_thresh = 1;
167 static int32_t rack_reorder_thresh = 2;
168 static int32_t rack_reorder_fade = 60000; /* 0 - never fade, def 60,000
170 static int32_t rack_pkt_delay = 1;
171 static int32_t rack_inc_var = 0;/* For TLP */
172 static int32_t rack_reduce_largest_on_idle = 0;
173 static int32_t rack_min_pace_time = 0;
174 static int32_t rack_min_pace_time_seg_req=6;
175 static int32_t rack_early_recovery = 1;
176 static int32_t rack_early_recovery_max_seg = 6;
177 static int32_t rack_send_a_lot_in_prr = 1;
178 static int32_t rack_min_to = 1; /* Number of ms minimum timeout */
179 static int32_t rack_tlp_in_recovery = 1; /* Can we do TLP in recovery? */
180 static int32_t rack_verbose_logging = 0;
181 static int32_t rack_ignore_data_after_close = 1;
183 * Currently regular tcp has a rto_min of 30ms
184 * the backoff goes 12 times so that ends up
185 * being a total of 122.850 seconds before a
186 * connection is killed.
188 static int32_t rack_tlp_min = 10;
189 static int32_t rack_rto_min = 30; /* 30ms same as main freebsd */
190 static int32_t rack_rto_max = 30000; /* 30 seconds */
191 static const int32_t rack_free_cache = 2;
192 static int32_t rack_hptsi_segments = 40;
193 static int32_t rack_rate_sample_method = USE_RTT_LOW;
194 static int32_t rack_pace_every_seg = 1;
195 static int32_t rack_delayed_ack_time = 200; /* 200ms */
196 static int32_t rack_slot_reduction = 4;
197 static int32_t rack_lower_cwnd_at_tlp = 0;
198 static int32_t rack_use_proportional_reduce = 0;
199 static int32_t rack_proportional_rate = 10;
200 static int32_t rack_tlp_max_resend = 2;
201 static int32_t rack_limited_retran = 0;
202 static int32_t rack_always_send_oldest = 0;
203 static int32_t rack_sack_block_limit = 128;
204 static int32_t rack_use_sack_filter = 1;
205 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE;
207 /* Rack specific counters */
208 counter_u64_t rack_badfr;
209 counter_u64_t rack_badfr_bytes;
210 counter_u64_t rack_rtm_prr_retran;
211 counter_u64_t rack_rtm_prr_newdata;
212 counter_u64_t rack_timestamp_mismatch;
213 counter_u64_t rack_reorder_seen;
214 counter_u64_t rack_paced_segments;
215 counter_u64_t rack_unpaced_segments;
216 counter_u64_t rack_saw_enobuf;
217 counter_u64_t rack_saw_enetunreach;
219 /* Tail loss probe counters */
220 counter_u64_t rack_tlp_tot;
221 counter_u64_t rack_tlp_newdata;
222 counter_u64_t rack_tlp_retran;
223 counter_u64_t rack_tlp_retran_bytes;
224 counter_u64_t rack_tlp_retran_fail;
225 counter_u64_t rack_to_tot;
226 counter_u64_t rack_to_arm_rack;
227 counter_u64_t rack_to_arm_tlp;
228 counter_u64_t rack_to_alloc;
229 counter_u64_t rack_to_alloc_hard;
230 counter_u64_t rack_to_alloc_emerg;
232 counter_u64_t rack_sack_proc_all;
233 counter_u64_t rack_sack_proc_short;
234 counter_u64_t rack_sack_proc_restart;
235 counter_u64_t rack_runt_sacks;
236 counter_u64_t rack_used_tlpmethod;
237 counter_u64_t rack_used_tlpmethod2;
238 counter_u64_t rack_enter_tlp_calc;
239 counter_u64_t rack_input_idle_reduces;
240 counter_u64_t rack_tlp_does_nada;
242 /* Temp CPU counters */
243 counter_u64_t rack_find_high;
245 counter_u64_t rack_progress_drops;
246 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE];
247 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
250 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line);
253 rack_process_ack(struct mbuf *m, struct tcphdr *th,
254 struct socket *so, struct tcpcb *tp, struct tcpopt *to,
255 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val);
257 rack_process_data(struct mbuf *m, struct tcphdr *th,
258 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
259 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
261 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack,
262 struct tcphdr *th, uint16_t nsegs, uint16_t type, int32_t recovery);
263 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack);
264 static struct rack_sendmap *
265 rack_check_recovery_mode(struct tcpcb *tp,
268 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th,
270 static void rack_counter_destroy(void);
272 rack_ctloutput(struct socket *so, struct sockopt *sopt,
273 struct inpcb *inp, struct tcpcb *tp);
274 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how);
276 rack_do_segment(struct mbuf *m, struct tcphdr *th,
277 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
279 static void rack_dtor(void *mem, int32_t size, void *arg);
281 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm,
282 uint32_t t, uint32_t cts);
283 static struct rack_sendmap *
284 rack_find_high_nonack(struct tcp_rack *rack,
285 struct rack_sendmap *rsm);
286 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack);
287 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm);
288 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged);
290 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
291 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
292 static int32_t rack_handoff_ok(struct tcpcb *tp);
293 static int32_t rack_init(struct tcpcb *tp);
294 static void rack_init_sysctls(void);
296 rack_log_ack(struct tcpcb *tp, struct tcpopt *to,
299 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
300 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts,
301 uint8_t pass, struct rack_sendmap *hintrsm);
303 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack,
304 struct rack_sendmap *rsm);
305 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num);
306 static int32_t rack_output(struct tcpcb *tp);
308 rack_hpts_do_segment(struct mbuf *m, struct tcphdr *th,
309 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
310 uint8_t iptos, int32_t nxt_pkt, struct timeval *tv);
313 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack,
314 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm,
316 static void rack_post_recovery(struct tcpcb *tp, struct tcphdr *th);
317 static void rack_remxt_tmr(struct tcpcb *tp);
319 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
320 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack);
321 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack);
322 static int32_t rack_stopall(struct tcpcb *tp);
324 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type,
326 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type);
327 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line);
328 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type);
330 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
331 struct rack_sendmap *rsm, uint32_t ts, int32_t * lenp);
333 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
334 struct rack_sendmap *rsm, uint32_t ts);
336 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
337 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type);
338 static int32_t tcp_addrack(module_t mod, int32_t type, void *data);
340 rack_challenge_ack(struct mbuf *m, struct tcphdr *th,
341 struct tcpcb *tp, int32_t * ret_val);
343 rack_do_close_wait(struct mbuf *m, struct tcphdr *th,
344 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
345 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
347 rack_do_closing(struct mbuf *m, struct tcphdr *th,
348 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
349 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
351 rack_do_drop(struct mbuf *m, struct tcpcb *tp);
353 rack_do_dropafterack(struct mbuf *m, struct tcpcb *tp,
354 struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t * ret_val);
356 rack_do_dropwithreset(struct mbuf *m, struct tcpcb *tp,
357 struct tcphdr *th, int32_t rstreason, int32_t tlen);
359 rack_do_established(struct mbuf *m, struct tcphdr *th,
360 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
361 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
363 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th,
364 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
365 int32_t tlen, uint32_t tiwin, int32_t nxt_pkt);
367 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th,
368 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
369 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
371 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th,
372 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
373 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
375 rack_do_lastack(struct mbuf *m, struct tcphdr *th,
376 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
377 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
379 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th,
380 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
381 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
383 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th,
384 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen,
385 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt);
387 rack_drop_checks(struct tcpopt *to, struct mbuf *m,
388 struct tcphdr *th, struct tcpcb *tp, int32_t * tlenp, int32_t * thf,
389 int32_t * drop_hdrlen, int32_t * ret_val);
391 rack_process_rst(struct mbuf *m, struct tcphdr *th,
392 struct socket *so, struct tcpcb *tp);
393 struct rack_sendmap *
394 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack,
396 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt);
398 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th);
401 rack_ts_check(struct mbuf *m, struct tcphdr *th,
402 struct tcpcb *tp, int32_t tlen, int32_t thflags, int32_t * ret_val);
404 int32_t rack_clear_counter=0;
408 sysctl_rack_clear(SYSCTL_HANDLER_ARGS)
413 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t));
414 if (error || req->newptr == NULL)
417 error = SYSCTL_IN(req, &stat, sizeof(uint32_t));
422 printf("Clearing RACK counters\n");
424 counter_u64_zero(rack_badfr);
425 counter_u64_zero(rack_badfr_bytes);
426 counter_u64_zero(rack_rtm_prr_retran);
427 counter_u64_zero(rack_rtm_prr_newdata);
428 counter_u64_zero(rack_timestamp_mismatch);
429 counter_u64_zero(rack_reorder_seen);
430 counter_u64_zero(rack_tlp_tot);
431 counter_u64_zero(rack_tlp_newdata);
432 counter_u64_zero(rack_tlp_retran);
433 counter_u64_zero(rack_tlp_retran_bytes);
434 counter_u64_zero(rack_tlp_retran_fail);
435 counter_u64_zero(rack_to_tot);
436 counter_u64_zero(rack_to_arm_rack);
437 counter_u64_zero(rack_to_arm_tlp);
438 counter_u64_zero(rack_paced_segments);
439 counter_u64_zero(rack_unpaced_segments);
440 counter_u64_zero(rack_saw_enobuf);
441 counter_u64_zero(rack_saw_enetunreach);
442 counter_u64_zero(rack_to_alloc_hard);
443 counter_u64_zero(rack_to_alloc_emerg);
444 counter_u64_zero(rack_sack_proc_all);
445 counter_u64_zero(rack_sack_proc_short);
446 counter_u64_zero(rack_sack_proc_restart);
447 counter_u64_zero(rack_to_alloc);
448 counter_u64_zero(rack_find_high);
449 counter_u64_zero(rack_runt_sacks);
450 counter_u64_zero(rack_used_tlpmethod);
451 counter_u64_zero(rack_used_tlpmethod2);
452 counter_u64_zero(rack_enter_tlp_calc);
453 counter_u64_zero(rack_progress_drops);
454 counter_u64_zero(rack_tlp_does_nada);
456 rack_clear_counter = 0;
465 SYSCTL_ADD_S32(&rack_sysctl_ctx,
466 SYSCTL_CHILDREN(rack_sysctl_root),
467 OID_AUTO, "rate_sample_method", CTLFLAG_RW,
468 &rack_rate_sample_method , USE_RTT_LOW,
469 "What method should we use for rate sampling 0=high, 1=low ");
470 SYSCTL_ADD_S32(&rack_sysctl_ctx,
471 SYSCTL_CHILDREN(rack_sysctl_root),
472 OID_AUTO, "data_after_close", CTLFLAG_RW,
473 &rack_ignore_data_after_close, 0,
474 "Do we hold off sending a RST until all pending data is ack'd");
475 SYSCTL_ADD_S32(&rack_sysctl_ctx,
476 SYSCTL_CHILDREN(rack_sysctl_root),
477 OID_AUTO, "tlpmethod", CTLFLAG_RW,
478 &rack_tlp_threshold_use, TLP_USE_TWO_ONE,
479 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2");
480 SYSCTL_ADD_S32(&rack_sysctl_ctx,
481 SYSCTL_CHILDREN(rack_sysctl_root),
482 OID_AUTO, "min_pace_time", CTLFLAG_RW,
483 &rack_min_pace_time, 0,
484 "Should we enforce a minimum pace time of 1ms");
485 SYSCTL_ADD_S32(&rack_sysctl_ctx,
486 SYSCTL_CHILDREN(rack_sysctl_root),
487 OID_AUTO, "min_pace_segs", CTLFLAG_RW,
488 &rack_min_pace_time_seg_req, 6,
489 "How many segments have to be in the len to enforce min-pace-time");
490 SYSCTL_ADD_S32(&rack_sysctl_ctx,
491 SYSCTL_CHILDREN(rack_sysctl_root),
492 OID_AUTO, "idle_reduce_high", CTLFLAG_RW,
493 &rack_reduce_largest_on_idle, 0,
494 "Should we reduce the largest cwnd seen to IW on idle reduction");
495 SYSCTL_ADD_S32(&rack_sysctl_ctx,
496 SYSCTL_CHILDREN(rack_sysctl_root),
497 OID_AUTO, "bb_verbose", CTLFLAG_RW,
498 &rack_verbose_logging, 0,
499 "Should RACK black box logging be verbose");
500 SYSCTL_ADD_S32(&rack_sysctl_ctx,
501 SYSCTL_CHILDREN(rack_sysctl_root),
502 OID_AUTO, "sackfiltering", CTLFLAG_RW,
503 &rack_use_sack_filter, 1,
504 "Do we use sack filtering?");
505 SYSCTL_ADD_S32(&rack_sysctl_ctx,
506 SYSCTL_CHILDREN(rack_sysctl_root),
507 OID_AUTO, "delayed_ack", CTLFLAG_RW,
508 &rack_delayed_ack_time, 200,
509 "Delayed ack time (200ms)");
510 SYSCTL_ADD_S32(&rack_sysctl_ctx,
511 SYSCTL_CHILDREN(rack_sysctl_root),
512 OID_AUTO, "tlpminto", CTLFLAG_RW,
514 "TLP minimum timeout per the specification (10ms)");
515 SYSCTL_ADD_S32(&rack_sysctl_ctx,
516 SYSCTL_CHILDREN(rack_sysctl_root),
517 OID_AUTO, "precache", CTLFLAG_RW,
519 "Where should we precache the mcopy (0 is not at all)");
520 SYSCTL_ADD_S32(&rack_sysctl_ctx,
521 SYSCTL_CHILDREN(rack_sysctl_root),
522 OID_AUTO, "sblklimit", CTLFLAG_RW,
523 &rack_sack_block_limit, 128,
524 "When do we start paying attention to small sack blocks");
525 SYSCTL_ADD_S32(&rack_sysctl_ctx,
526 SYSCTL_CHILDREN(rack_sysctl_root),
527 OID_AUTO, "send_oldest", CTLFLAG_RW,
528 &rack_always_send_oldest, 1,
529 "Should we always send the oldest TLP and RACK-TLP");
530 SYSCTL_ADD_S32(&rack_sysctl_ctx,
531 SYSCTL_CHILDREN(rack_sysctl_root),
532 OID_AUTO, "rack_tlp_in_recovery", CTLFLAG_RW,
533 &rack_tlp_in_recovery, 1,
534 "Can we do a TLP during recovery?");
535 SYSCTL_ADD_S32(&rack_sysctl_ctx,
536 SYSCTL_CHILDREN(rack_sysctl_root),
537 OID_AUTO, "rack_tlimit", CTLFLAG_RW,
538 &rack_limited_retran, 0,
539 "How many times can a rack timeout drive out sends");
540 SYSCTL_ADD_S32(&rack_sysctl_ctx,
541 SYSCTL_CHILDREN(rack_sysctl_root),
542 OID_AUTO, "minrto", CTLFLAG_RW,
544 "Minimum RTO in ms -- set with caution below 1000 due to TLP");
545 SYSCTL_ADD_S32(&rack_sysctl_ctx,
546 SYSCTL_CHILDREN(rack_sysctl_root),
547 OID_AUTO, "maxrto", CTLFLAG_RW,
549 "Maxiumum RTO in ms -- should be at least as large as min_rto");
550 SYSCTL_ADD_S32(&rack_sysctl_ctx,
551 SYSCTL_CHILDREN(rack_sysctl_root),
552 OID_AUTO, "tlp_retry", CTLFLAG_RW,
553 &rack_tlp_max_resend, 2,
554 "How many times does TLP retry a single segment or multiple with no ACK");
555 SYSCTL_ADD_S32(&rack_sysctl_ctx,
556 SYSCTL_CHILDREN(rack_sysctl_root),
557 OID_AUTO, "recovery_loss_prop", CTLFLAG_RW,
558 &rack_use_proportional_reduce, 0,
559 "Should we proportionaly reduce cwnd based on the number of losses ");
560 SYSCTL_ADD_S32(&rack_sysctl_ctx,
561 SYSCTL_CHILDREN(rack_sysctl_root),
562 OID_AUTO, "recovery_prop", CTLFLAG_RW,
563 &rack_proportional_rate, 10,
564 "What percent reduction per loss");
565 SYSCTL_ADD_S32(&rack_sysctl_ctx,
566 SYSCTL_CHILDREN(rack_sysctl_root),
567 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW,
568 &rack_lower_cwnd_at_tlp, 0,
569 "When a TLP completes a retran should we enter recovery?");
570 SYSCTL_ADD_S32(&rack_sysctl_ctx,
571 SYSCTL_CHILDREN(rack_sysctl_root),
572 OID_AUTO, "hptsi_reduces", CTLFLAG_RW,
573 &rack_slot_reduction, 4,
574 "When setting a slot should we reduce by divisor");
575 SYSCTL_ADD_S32(&rack_sysctl_ctx,
576 SYSCTL_CHILDREN(rack_sysctl_root),
577 OID_AUTO, "hptsi_every_seg", CTLFLAG_RW,
578 &rack_pace_every_seg, 1,
579 "Should we pace out every segment hptsi");
580 SYSCTL_ADD_S32(&rack_sysctl_ctx,
581 SYSCTL_CHILDREN(rack_sysctl_root),
582 OID_AUTO, "hptsi_seg_max", CTLFLAG_RW,
583 &rack_hptsi_segments, 6,
584 "Should we pace out only a limited size of segments");
585 SYSCTL_ADD_S32(&rack_sysctl_ctx,
586 SYSCTL_CHILDREN(rack_sysctl_root),
587 OID_AUTO, "prr_sendalot", CTLFLAG_RW,
588 &rack_send_a_lot_in_prr, 1,
589 "Send a lot in prr");
590 SYSCTL_ADD_S32(&rack_sysctl_ctx,
591 SYSCTL_CHILDREN(rack_sysctl_root),
592 OID_AUTO, "minto", CTLFLAG_RW,
594 "Minimum rack timeout in milliseconds");
595 SYSCTL_ADD_S32(&rack_sysctl_ctx,
596 SYSCTL_CHILDREN(rack_sysctl_root),
597 OID_AUTO, "earlyrecoveryseg", CTLFLAG_RW,
598 &rack_early_recovery_max_seg, 6,
599 "Max segments in early recovery");
600 SYSCTL_ADD_S32(&rack_sysctl_ctx,
601 SYSCTL_CHILDREN(rack_sysctl_root),
602 OID_AUTO, "earlyrecovery", CTLFLAG_RW,
603 &rack_early_recovery, 1,
604 "Do we do early recovery with rack");
605 SYSCTL_ADD_S32(&rack_sysctl_ctx,
606 SYSCTL_CHILDREN(rack_sysctl_root),
607 OID_AUTO, "reorder_thresh", CTLFLAG_RW,
608 &rack_reorder_thresh, 2,
609 "What factor for rack will be added when seeing reordering (shift right)");
610 SYSCTL_ADD_S32(&rack_sysctl_ctx,
611 SYSCTL_CHILDREN(rack_sysctl_root),
612 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW,
614 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)");
615 SYSCTL_ADD_S32(&rack_sysctl_ctx,
616 SYSCTL_CHILDREN(rack_sysctl_root),
617 OID_AUTO, "reorder_fade", CTLFLAG_RW,
618 &rack_reorder_fade, 0,
619 "Does reorder detection fade, if so how many ms (0 means never)");
620 SYSCTL_ADD_S32(&rack_sysctl_ctx,
621 SYSCTL_CHILDREN(rack_sysctl_root),
622 OID_AUTO, "pktdelay", CTLFLAG_RW,
624 "Extra RACK time (in ms) besides reordering thresh");
625 SYSCTL_ADD_S32(&rack_sysctl_ctx,
626 SYSCTL_CHILDREN(rack_sysctl_root),
627 OID_AUTO, "inc_var", CTLFLAG_RW,
629 "Should rack add to the TLP timer the variance in rtt calculation");
630 rack_badfr = counter_u64_alloc(M_WAITOK);
631 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
632 SYSCTL_CHILDREN(rack_sysctl_root),
633 OID_AUTO, "badfr", CTLFLAG_RD,
634 &rack_badfr, "Total number of bad FRs");
635 rack_badfr_bytes = counter_u64_alloc(M_WAITOK);
636 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
637 SYSCTL_CHILDREN(rack_sysctl_root),
638 OID_AUTO, "badfr_bytes", CTLFLAG_RD,
639 &rack_badfr_bytes, "Total number of bad FRs");
640 rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK);
641 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
642 SYSCTL_CHILDREN(rack_sysctl_root),
643 OID_AUTO, "prrsndret", CTLFLAG_RD,
644 &rack_rtm_prr_retran,
645 "Total number of prr based retransmits");
646 rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK);
647 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
648 SYSCTL_CHILDREN(rack_sysctl_root),
649 OID_AUTO, "prrsndnew", CTLFLAG_RD,
650 &rack_rtm_prr_newdata,
651 "Total number of prr based new transmits");
652 rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK);
653 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
654 SYSCTL_CHILDREN(rack_sysctl_root),
655 OID_AUTO, "tsnf", CTLFLAG_RD,
656 &rack_timestamp_mismatch,
657 "Total number of timestamps that we could not find the reported ts");
658 rack_find_high = counter_u64_alloc(M_WAITOK);
659 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
660 SYSCTL_CHILDREN(rack_sysctl_root),
661 OID_AUTO, "findhigh", CTLFLAG_RD,
663 "Total number of FIN causing find-high");
664 rack_reorder_seen = counter_u64_alloc(M_WAITOK);
665 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
666 SYSCTL_CHILDREN(rack_sysctl_root),
667 OID_AUTO, "reordering", CTLFLAG_RD,
669 "Total number of times we added delay due to reordering");
670 rack_tlp_tot = counter_u64_alloc(M_WAITOK);
671 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
672 SYSCTL_CHILDREN(rack_sysctl_root),
673 OID_AUTO, "tlp_to_total", CTLFLAG_RD,
675 "Total number of tail loss probe expirations");
676 rack_tlp_newdata = counter_u64_alloc(M_WAITOK);
677 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
678 SYSCTL_CHILDREN(rack_sysctl_root),
679 OID_AUTO, "tlp_new", CTLFLAG_RD,
681 "Total number of tail loss probe sending new data");
683 rack_tlp_retran = counter_u64_alloc(M_WAITOK);
684 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
685 SYSCTL_CHILDREN(rack_sysctl_root),
686 OID_AUTO, "tlp_retran", CTLFLAG_RD,
688 "Total number of tail loss probe sending retransmitted data");
689 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK);
690 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
691 SYSCTL_CHILDREN(rack_sysctl_root),
692 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD,
693 &rack_tlp_retran_bytes,
694 "Total bytes of tail loss probe sending retransmitted data");
695 rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK);
696 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
697 SYSCTL_CHILDREN(rack_sysctl_root),
698 OID_AUTO, "tlp_retran_fail", CTLFLAG_RD,
699 &rack_tlp_retran_fail,
700 "Total number of tail loss probe sending retransmitted data that failed (wait for t3)");
701 rack_to_tot = counter_u64_alloc(M_WAITOK);
702 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
703 SYSCTL_CHILDREN(rack_sysctl_root),
704 OID_AUTO, "rack_to_tot", CTLFLAG_RD,
706 "Total number of times the rack to expired?");
707 rack_to_arm_rack = counter_u64_alloc(M_WAITOK);
708 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
709 SYSCTL_CHILDREN(rack_sysctl_root),
710 OID_AUTO, "arm_rack", CTLFLAG_RD,
712 "Total number of times the rack timer armed?");
713 rack_to_arm_tlp = counter_u64_alloc(M_WAITOK);
714 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
715 SYSCTL_CHILDREN(rack_sysctl_root),
716 OID_AUTO, "arm_tlp", CTLFLAG_RD,
718 "Total number of times the tlp timer armed?");
719 rack_paced_segments = counter_u64_alloc(M_WAITOK);
720 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
721 SYSCTL_CHILDREN(rack_sysctl_root),
722 OID_AUTO, "paced", CTLFLAG_RD,
723 &rack_paced_segments,
724 "Total number of times a segment send caused hptsi");
725 rack_unpaced_segments = counter_u64_alloc(M_WAITOK);
726 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
727 SYSCTL_CHILDREN(rack_sysctl_root),
728 OID_AUTO, "unpaced", CTLFLAG_RD,
729 &rack_unpaced_segments,
730 "Total number of times a segment did not cause hptsi");
731 rack_saw_enobuf = counter_u64_alloc(M_WAITOK);
732 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
733 SYSCTL_CHILDREN(rack_sysctl_root),
734 OID_AUTO, "saw_enobufs", CTLFLAG_RD,
736 "Total number of times a segment did not cause hptsi");
737 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK);
738 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
739 SYSCTL_CHILDREN(rack_sysctl_root),
740 OID_AUTO, "saw_enetunreach", CTLFLAG_RD,
741 &rack_saw_enetunreach,
742 "Total number of times a segment did not cause hptsi");
743 rack_to_alloc = counter_u64_alloc(M_WAITOK);
744 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
745 SYSCTL_CHILDREN(rack_sysctl_root),
746 OID_AUTO, "allocs", CTLFLAG_RD,
748 "Total allocations of tracking structures");
749 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK);
750 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
751 SYSCTL_CHILDREN(rack_sysctl_root),
752 OID_AUTO, "allochard", CTLFLAG_RD,
754 "Total allocations done with sleeping the hard way");
755 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK);
756 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
757 SYSCTL_CHILDREN(rack_sysctl_root),
758 OID_AUTO, "allocemerg", CTLFLAG_RD,
759 &rack_to_alloc_emerg,
760 "Total alocations done from emergency cache");
761 rack_sack_proc_all = counter_u64_alloc(M_WAITOK);
762 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
763 SYSCTL_CHILDREN(rack_sysctl_root),
764 OID_AUTO, "sack_long", CTLFLAG_RD,
766 "Total times we had to walk whole list for sack processing");
768 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK);
769 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
770 SYSCTL_CHILDREN(rack_sysctl_root),
771 OID_AUTO, "sack_restart", CTLFLAG_RD,
772 &rack_sack_proc_restart,
773 "Total times we had to walk whole list due to a restart");
774 rack_sack_proc_short = counter_u64_alloc(M_WAITOK);
775 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
776 SYSCTL_CHILDREN(rack_sysctl_root),
777 OID_AUTO, "sack_short", CTLFLAG_RD,
778 &rack_sack_proc_short,
779 "Total times we took shortcut for sack processing");
780 rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK);
781 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
782 SYSCTL_CHILDREN(rack_sysctl_root),
783 OID_AUTO, "tlp_calc_entered", CTLFLAG_RD,
784 &rack_enter_tlp_calc,
785 "Total times we called calc-tlp");
786 rack_used_tlpmethod = counter_u64_alloc(M_WAITOK);
787 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
788 SYSCTL_CHILDREN(rack_sysctl_root),
789 OID_AUTO, "hit_tlp_method", CTLFLAG_RD,
790 &rack_used_tlpmethod,
791 "Total number of runt sacks");
792 rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK);
793 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
794 SYSCTL_CHILDREN(rack_sysctl_root),
795 OID_AUTO, "hit_tlp_method2", CTLFLAG_RD,
796 &rack_used_tlpmethod2,
797 "Total number of runt sacks 2");
798 rack_runt_sacks = counter_u64_alloc(M_WAITOK);
799 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
800 SYSCTL_CHILDREN(rack_sysctl_root),
801 OID_AUTO, "runtsacks", CTLFLAG_RD,
803 "Total number of runt sacks");
804 rack_progress_drops = counter_u64_alloc(M_WAITOK);
805 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
806 SYSCTL_CHILDREN(rack_sysctl_root),
807 OID_AUTO, "prog_drops", CTLFLAG_RD,
808 &rack_progress_drops,
809 "Total number of progress drops");
810 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK);
811 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
812 SYSCTL_CHILDREN(rack_sysctl_root),
813 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD,
814 &rack_input_idle_reduces,
815 "Total number of idle reductions on input");
816 rack_tlp_does_nada = counter_u64_alloc(M_WAITOK);
817 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx,
818 SYSCTL_CHILDREN(rack_sysctl_root),
819 OID_AUTO, "tlp_nada", CTLFLAG_RD,
821 "Total number of nada tlp calls");
822 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK);
823 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
824 OID_AUTO, "outsize", CTLFLAG_RD,
825 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes");
826 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK);
827 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root),
828 OID_AUTO, "opts", CTLFLAG_RD,
829 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats");
830 SYSCTL_ADD_PROC(&rack_sysctl_ctx,
831 SYSCTL_CHILDREN(rack_sysctl_root),
832 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
833 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters");
836 static inline int32_t
837 rack_progress_timeout_check(struct tcpcb *tp)
839 if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) {
840 if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) {
842 * There is an assumption that the caller
843 * will drop the connection so we will
844 * increment the counters here.
846 struct tcp_rack *rack;
847 rack = (struct tcp_rack *)tp->t_fb_ptr;
848 counter_u64_add(rack_progress_drops, 1);
850 TCPSTAT_INC(tcps_progdrops);
852 rack_log_progress_event(rack, tp, ticks, PROGRESS_DROP, __LINE__);
861 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which)
863 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
864 union tcp_log_stackspecific log;
866 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
867 log.u_bbr.flex1 = TICKS_2_MSEC(rack->rc_tp->t_srtt >> TCP_RTT_SHIFT);
868 log.u_bbr.flex2 = to;
869 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
870 log.u_bbr.flex4 = slot;
871 log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot;
872 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
873 log.u_bbr.flex8 = which;
874 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
875 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
876 TCP_LOG_EVENT(rack->rc_tp, NULL,
877 &rack->rc_inp->inp_socket->so_rcv,
878 &rack->rc_inp->inp_socket->so_snd,
879 BBR_LOG_TIMERSTAR, 0,
885 rack_log_to_event(struct tcp_rack *rack, int32_t to_num)
887 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
888 union tcp_log_stackspecific log;
890 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
891 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
892 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
893 log.u_bbr.flex8 = to_num;
894 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt;
895 log.u_bbr.flex2 = rack->rc_rack_rtt;
896 TCP_LOG_EVENT(rack->rc_tp, NULL,
897 &rack->rc_inp->inp_socket->so_rcv,
898 &rack->rc_inp->inp_socket->so_snd,
905 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, int32_t t,
906 uint32_t o_srtt, uint32_t o_var)
908 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
909 union tcp_log_stackspecific log;
911 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
912 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
913 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
915 log.u_bbr.flex2 = o_srtt;
916 log.u_bbr.flex3 = o_var;
917 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest;
918 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest;
919 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_rtt_cnt;
920 log.u_bbr.rttProp = rack->r_ctl.rack_rs.rs_rtt_tot;
921 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method;
922 TCP_LOG_EVENT(tp, NULL,
923 &rack->rc_inp->inp_socket->so_rcv,
924 &rack->rc_inp->inp_socket->so_snd,
931 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt)
934 * Log the rtt sample we are
935 * applying to the srtt algorithm in
938 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
939 union tcp_log_stackspecific log;
942 /* Convert our ms to a microsecond */
943 log.u_bbr.flex1 = rtt * 1000;
944 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
945 TCP_LOG_EVENTP(rack->rc_tp, NULL,
946 &rack->rc_inp->inp_socket->so_rcv,
947 &rack->rc_inp->inp_socket->so_snd,
949 0, &log, false, &tv);
955 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line)
957 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) {
958 union tcp_log_stackspecific log;
960 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
961 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
962 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
963 log.u_bbr.flex1 = line;
964 log.u_bbr.flex2 = tick;
965 log.u_bbr.flex3 = tp->t_maxunacktime;
966 log.u_bbr.flex4 = tp->t_acktime;
967 log.u_bbr.flex8 = event;
968 TCP_LOG_EVENT(tp, NULL,
969 &rack->rc_inp->inp_socket->so_rcv,
970 &rack->rc_inp->inp_socket->so_snd,
977 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts)
979 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
980 union tcp_log_stackspecific log;
982 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
983 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
984 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
985 log.u_bbr.flex1 = slot;
986 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags);
987 log.u_bbr.flex8 = rack->rc_in_persist;
988 TCP_LOG_EVENT(rack->rc_tp, NULL,
989 &rack->rc_inp->inp_socket->so_rcv,
990 &rack->rc_inp->inp_socket->so_snd,
997 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out)
999 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1000 union tcp_log_stackspecific log;
1001 log.u_bbr.flex1 = did_out;
1002 log.u_bbr.flex2 = nxt_pkt;
1003 log.u_bbr.flex3 = way_out;
1004 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
1005 log.u_bbr.flex7 = rack->r_wanted_output;
1006 log.u_bbr.flex8 = rack->rc_in_persist;
1007 TCP_LOG_EVENT(rack->rc_tp, NULL,
1008 &rack->rc_inp->inp_socket->so_rcv,
1009 &rack->rc_inp->inp_socket->so_snd,
1010 BBR_LOG_DOSEG_DONE, 0,
1017 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot, uint8_t hpts_calling)
1019 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1020 union tcp_log_stackspecific log;
1022 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1023 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1024 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1025 log.u_bbr.flex1 = slot;
1026 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags;
1027 log.u_bbr.flex7 = hpts_calling;
1028 log.u_bbr.flex8 = rack->rc_in_persist;
1029 TCP_LOG_EVENT(rack->rc_tp, NULL,
1030 &rack->rc_inp->inp_socket->so_rcv,
1031 &rack->rc_inp->inp_socket->so_snd,
1038 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line)
1040 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1041 union tcp_log_stackspecific log;
1043 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1044 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
1045 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
1046 log.u_bbr.flex1 = line;
1047 log.u_bbr.flex2 = 0;
1048 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags;
1049 log.u_bbr.flex4 = 0;
1050 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur;
1051 log.u_bbr.flex8 = hpts_removed;
1052 TCP_LOG_EVENT(rack->rc_tp, NULL,
1053 &rack->rc_inp->inp_socket->so_rcv,
1054 &rack->rc_inp->inp_socket->so_snd,
1055 BBR_LOG_TIMERCANC, 0,
1061 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers)
1063 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) {
1064 union tcp_log_stackspecific log;
1066 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
1067 log.u_bbr.flex1 = timers;
1068 log.u_bbr.flex2 = ret;
1069 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp;
1070 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags;
1071 log.u_bbr.flex5 = cts;
1072 TCP_LOG_EVENT(rack->rc_tp, NULL,
1073 &rack->rc_inp->inp_socket->so_rcv,
1074 &rack->rc_inp->inp_socket->so_snd,
1075 BBR_LOG_TO_PROCESS, 0,
1081 rack_counter_destroy()
1083 counter_u64_free(rack_badfr);
1084 counter_u64_free(rack_badfr_bytes);
1085 counter_u64_free(rack_rtm_prr_retran);
1086 counter_u64_free(rack_rtm_prr_newdata);
1087 counter_u64_free(rack_timestamp_mismatch);
1088 counter_u64_free(rack_reorder_seen);
1089 counter_u64_free(rack_tlp_tot);
1090 counter_u64_free(rack_tlp_newdata);
1091 counter_u64_free(rack_tlp_retran);
1092 counter_u64_free(rack_tlp_retran_bytes);
1093 counter_u64_free(rack_tlp_retran_fail);
1094 counter_u64_free(rack_to_tot);
1095 counter_u64_free(rack_to_arm_rack);
1096 counter_u64_free(rack_to_arm_tlp);
1097 counter_u64_free(rack_paced_segments);
1098 counter_u64_free(rack_unpaced_segments);
1099 counter_u64_free(rack_saw_enobuf);
1100 counter_u64_free(rack_saw_enetunreach);
1101 counter_u64_free(rack_to_alloc_hard);
1102 counter_u64_free(rack_to_alloc_emerg);
1103 counter_u64_free(rack_sack_proc_all);
1104 counter_u64_free(rack_sack_proc_short);
1105 counter_u64_free(rack_sack_proc_restart);
1106 counter_u64_free(rack_to_alloc);
1107 counter_u64_free(rack_find_high);
1108 counter_u64_free(rack_runt_sacks);
1109 counter_u64_free(rack_enter_tlp_calc);
1110 counter_u64_free(rack_used_tlpmethod);
1111 counter_u64_free(rack_used_tlpmethod2);
1112 counter_u64_free(rack_progress_drops);
1113 counter_u64_free(rack_input_idle_reduces);
1114 counter_u64_free(rack_tlp_does_nada);
1115 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE);
1116 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE);
1119 static struct rack_sendmap *
1120 rack_alloc(struct tcp_rack *rack)
1122 struct rack_sendmap *rsm;
1124 counter_u64_add(rack_to_alloc, 1);
1125 rack->r_ctl.rc_num_maps_alloced++;
1126 rsm = uma_zalloc(rack_zone, M_NOWAIT);
1130 if (rack->rc_free_cnt) {
1131 counter_u64_add(rack_to_alloc_emerg, 1);
1132 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
1133 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_next);
1134 rack->rc_free_cnt--;
1141 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm)
1143 rack->r_ctl.rc_num_maps_alloced--;
1144 if (rack->r_ctl.rc_tlpsend == rsm)
1145 rack->r_ctl.rc_tlpsend = NULL;
1146 if (rack->r_ctl.rc_next == rsm)
1147 rack->r_ctl.rc_next = NULL;
1148 if (rack->r_ctl.rc_sacklast == rsm)
1149 rack->r_ctl.rc_sacklast = NULL;
1150 if (rack->rc_free_cnt < rack_free_cache) {
1151 memset(rsm, 0, sizeof(struct rack_sendmap));
1152 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_next);
1153 rack->rc_free_cnt++;
1156 uma_zfree(rack_zone, rsm);
1160 * CC wrapper hook functions
1163 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, struct tcphdr *th, uint16_t nsegs,
1164 uint16_t type, int32_t recovery)
1166 #ifdef NETFLIX_STATS
1170 u_long old_cwnd = tp->snd_cwnd;
1173 INP_WLOCK_ASSERT(tp->t_inpcb);
1174 tp->ccv->nsegs = nsegs;
1175 tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
1176 if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) {
1179 max = rack->r_ctl.rc_early_recovery_segs * tp->t_maxseg;
1180 if (tp->ccv->bytes_this_ack > max) {
1181 tp->ccv->bytes_this_ack = max;
1184 if (tp->snd_cwnd <= tp->snd_wnd)
1185 tp->ccv->flags |= CCF_CWND_LIMITED;
1187 tp->ccv->flags &= ~CCF_CWND_LIMITED;
1189 if (type == CC_ACK) {
1190 #ifdef NETFLIX_STATS
1191 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
1192 ((int32_t) tp->snd_cwnd) - tp->snd_wnd);
1193 if ((tp->t_flags & TF_GPUTINPROG) &&
1194 SEQ_GEQ(th->th_ack, tp->gput_ack)) {
1195 gput = (((int64_t) (th->th_ack - tp->gput_seq)) << 3) /
1196 max(1, tcp_ts_getticks() - tp->gput_ts);
1197 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
1200 * XXXLAS: This is a temporary hack, and should be
1201 * chained off VOI_TCP_GPUT when stats(9) grows an
1202 * API to deal with chained VOIs.
1204 if (tp->t_stats_gput_prev > 0)
1205 stats_voi_update_abs_s32(tp->t_stats,
1207 ((gput - tp->t_stats_gput_prev) * 100) /
1208 tp->t_stats_gput_prev);
1209 tp->t_flags &= ~TF_GPUTINPROG;
1210 tp->t_stats_gput_prev = gput;
1212 if (tp->t_maxpeakrate) {
1214 * We update t_peakrate_thr. This gives us roughly
1215 * one update per round trip time.
1217 tcp_update_peakrate_thr(tp);
1222 if (tp->snd_cwnd > tp->snd_ssthresh) {
1223 tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
1224 nsegs * V_tcp_abc_l_var * tp->t_maxseg);
1225 if (tp->t_bytes_acked >= tp->snd_cwnd) {
1226 tp->t_bytes_acked -= tp->snd_cwnd;
1227 tp->ccv->flags |= CCF_ABC_SENTAWND;
1230 tp->ccv->flags &= ~CCF_ABC_SENTAWND;
1231 tp->t_bytes_acked = 0;
1234 if (CC_ALGO(tp)->ack_received != NULL) {
1235 /* XXXLAS: Find a way to live without this */
1236 tp->ccv->curack = th->th_ack;
1237 CC_ALGO(tp)->ack_received(tp->ccv, type);
1239 #ifdef NETFLIX_STATS
1240 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
1242 if (rack->r_ctl.rc_rack_largest_cwnd < tp->snd_cwnd) {
1243 rack->r_ctl.rc_rack_largest_cwnd = tp->snd_cwnd;
1246 if (tp->cwv_enabled) {
1248 * Per RFC 7661: The behaviour in the non-validated phase is
1249 * specified as: o A sender determines whether to increase
1250 * the cwnd based upon whether it is cwnd-limited (see
1251 * Section 4.5.3): * A sender that is cwnd-limited MAY use
1252 * the standard TCP method to increase cwnd (i.e., the
1253 * standard method permits a TCP sender that fully utilises
1254 * the cwnd to increase the cwnd each time it receives an
1255 * ACK). * A sender that is not cwnd-limited MUST NOT
1256 * increase the cwnd when ACK packets are received in this
1257 * phase (i.e., needs to avoid growing the cwnd when it has
1258 * not recently sent using the current size of cwnd).
1260 if ((tp->snd_cwnd > old_cwnd) &&
1261 (tp->cwv_cwnd_valid == 0) &&
1262 (!(tp->ccv->flags & CCF_CWND_LIMITED))) {
1263 tp->snd_cwnd = old_cwnd;
1265 /* Try to update pipeAck and NCWV state */
1266 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1267 !IN_RECOVERY(tp->t_flags)) {
1268 uint32_t data = sbavail(&(tp->t_inpcb->inp_socket->so_snd));
1270 tcp_newcwv_update_pipeack(tp, data);
1273 /* we enforce max peak rate if it is set. */
1274 if (tp->t_peakrate_thr && tp->snd_cwnd > tp->t_peakrate_thr) {
1275 tp->snd_cwnd = tp->t_peakrate_thr;
1281 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th)
1283 struct tcp_rack *rack;
1285 rack = (struct tcp_rack *)tp->t_fb_ptr;
1286 INP_WLOCK_ASSERT(tp->t_inpcb);
1287 if (rack->r_ctl.rc_prr_sndcnt > 0)
1288 rack->r_wanted_output++;
1292 rack_post_recovery(struct tcpcb *tp, struct tcphdr *th)
1294 struct tcp_rack *rack;
1296 INP_WLOCK_ASSERT(tp->t_inpcb);
1297 rack = (struct tcp_rack *)tp->t_fb_ptr;
1298 if (CC_ALGO(tp)->post_recovery != NULL) {
1299 tp->ccv->curack = th->th_ack;
1300 CC_ALGO(tp)->post_recovery(tp->ccv);
1303 * Here we can in theory adjust cwnd to be based on the number of
1304 * losses in the window (rack->r_ctl.rc_loss_count). This is done
1305 * based on the rack_use_proportional flag.
1307 if (rack->r_ctl.rc_prop_reduce && rack->r_ctl.rc_prop_rate) {
1310 reduce = (rack->r_ctl.rc_loss_count * rack->r_ctl.rc_prop_rate);
1314 tp->snd_cwnd -= ((reduce * tp->snd_cwnd) / 100);
1316 if (tp->snd_cwnd > tp->snd_ssthresh) {
1317 /* Drop us down to the ssthresh (1/2 cwnd at loss) */
1318 tp->snd_cwnd = tp->snd_ssthresh;
1321 if (rack->r_ctl.rc_prr_sndcnt > 0) {
1322 /* Suck the next prr cnt back into cwnd */
1323 tp->snd_cwnd += rack->r_ctl.rc_prr_sndcnt;
1324 rack->r_ctl.rc_prr_sndcnt = 0;
1326 EXIT_RECOVERY(tp->t_flags);
1330 if (tp->cwv_enabled) {
1331 if ((tp->cwv_cwnd_valid == 0) &&
1332 (tp->snd_cwv.in_recovery))
1333 tcp_newcwv_end_recovery(tp);
1339 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
1341 struct tcp_rack *rack;
1343 INP_WLOCK_ASSERT(tp->t_inpcb);
1345 rack = (struct tcp_rack *)tp->t_fb_ptr;
1348 /* rack->r_ctl.rc_ssthresh_set = 1;*/
1349 if (!IN_FASTRECOVERY(tp->t_flags)) {
1350 rack->r_ctl.rc_tlp_rtx_out = 0;
1351 rack->r_ctl.rc_prr_delivered = 0;
1352 rack->r_ctl.rc_prr_out = 0;
1353 rack->r_ctl.rc_loss_count = 0;
1354 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
1355 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una;
1356 tp->snd_recover = tp->snd_max;
1357 if (tp->t_flags & TF_ECN_PERMIT)
1358 tp->t_flags |= TF_ECN_SND_CWR;
1362 if (!IN_CONGRECOVERY(tp->t_flags)) {
1363 TCPSTAT_INC(tcps_ecn_rcwnd);
1364 tp->snd_recover = tp->snd_max;
1365 if (tp->t_flags & TF_ECN_PERMIT)
1366 tp->t_flags |= TF_ECN_SND_CWR;
1371 tp->t_bytes_acked = 0;
1372 EXIT_RECOVERY(tp->t_flags);
1373 tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1374 tp->t_maxseg) * tp->t_maxseg;
1375 tp->snd_cwnd = tp->t_maxseg;
1378 TCPSTAT_INC(tcps_sndrexmitbad);
1379 /* RTO was unnecessary, so reset everything. */
1380 tp->snd_cwnd = tp->snd_cwnd_prev;
1381 tp->snd_ssthresh = tp->snd_ssthresh_prev;
1382 tp->snd_recover = tp->snd_recover_prev;
1383 if (tp->t_flags & TF_WASFRECOVERY)
1384 ENTER_FASTRECOVERY(tp->t_flags);
1385 if (tp->t_flags & TF_WASCRECOVERY)
1386 ENTER_CONGRECOVERY(tp->t_flags);
1387 tp->snd_nxt = tp->snd_max;
1388 tp->t_badrxtwin = 0;
1392 if (CC_ALGO(tp)->cong_signal != NULL) {
1394 tp->ccv->curack = th->th_ack;
1395 CC_ALGO(tp)->cong_signal(tp->ccv, type);
1398 if (tp->cwv_enabled) {
1399 if (tp->snd_cwv.in_recovery == 0 && IN_RECOVERY(tp->t_flags)) {
1400 tcp_newcwv_enter_recovery(tp);
1402 if (type == CC_RTO) {
1403 tcp_newcwv_reset(tp);
1412 rack_cc_after_idle(struct tcpcb *tp, int reduce_largest)
1416 INP_WLOCK_ASSERT(tp->t_inpcb);
1418 #ifdef NETFLIX_STATS
1419 TCPSTAT_INC(tcps_idle_restarts);
1420 if (tp->t_state == TCPS_ESTABLISHED)
1421 TCPSTAT_INC(tcps_idle_estrestarts);
1423 if (CC_ALGO(tp)->after_idle != NULL)
1424 CC_ALGO(tp)->after_idle(tp->ccv);
1426 if (tp->snd_cwnd == 1)
1427 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */
1428 else if (V_tcp_initcwnd_segments)
1429 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg),
1430 max(2 * tp->t_maxseg, V_tcp_initcwnd_segments * 1460));
1431 else if (V_tcp_do_rfc3390)
1432 i_cwnd = min(4 * tp->t_maxseg,
1433 max(2 * tp->t_maxseg, 4380));
1435 /* Per RFC5681 Section 3.1 */
1436 if (tp->t_maxseg > 2190)
1437 i_cwnd = 2 * tp->t_maxseg;
1438 else if (tp->t_maxseg > 1095)
1439 i_cwnd = 3 * tp->t_maxseg;
1441 i_cwnd = 4 * tp->t_maxseg;
1443 if (reduce_largest) {
1445 * Do we reduce the largest cwnd to make
1446 * rack play nice on restart hptsi wise?
1448 if (((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rack_largest_cwnd > i_cwnd)
1449 ((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rack_largest_cwnd = i_cwnd;
1452 * Being idle is no differnt than the initial window. If the cc
1453 * clamps it down below the initial window raise it to the initial
1456 if (tp->snd_cwnd < i_cwnd) {
1457 tp->snd_cwnd = i_cwnd;
1463 * Indicate whether this ack should be delayed. We can delay the ack if
1464 * following conditions are met:
1465 * - There is no delayed ack timer in progress.
1466 * - Our last ack wasn't a 0-sized window. We never want to delay
1467 * the ack that opens up a 0-sized window.
1468 * - LRO wasn't used for this segment. We make sure by checking that the
1469 * segment size is not larger than the MSS.
1470 * - Delayed acks are enabled or this is a half-synchronized T/TCP
1473 #define DELAY_ACK(tp, tlen) \
1474 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \
1475 ((tp->t_flags & TF_DELACK) == 0) && \
1476 (tlen <= tp->t_maxseg) && \
1477 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN)))
1480 rack_calc_rwin(struct socket *so, struct tcpcb *tp)
1485 * Calculate amount of space in receive window, and then do TCP
1486 * input processing. Receive window is amount of space in rcv queue,
1487 * but not less than advertised window.
1489 win = sbspace(&so->so_rcv);
1492 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1496 rack_do_drop(struct mbuf *m, struct tcpcb *tp)
1499 * Drop space held by incoming segment and return.
1502 INP_WUNLOCK(tp->t_inpcb);
1508 rack_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
1509 int32_t rstreason, int32_t tlen)
1512 tcp_dropwithreset(m, th, tp, tlen, rstreason);
1513 INP_WUNLOCK(tp->t_inpcb);
1515 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1519 * The value in ret_val informs the caller
1520 * if we dropped the tcb (and lock) or not.
1521 * 1 = we dropped it, 0 = the TCB is still locked
1525 rack_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t * ret_val)
1528 * Generate an ACK dropping incoming segment if it occupies sequence
1529 * space, where the ACK reflects our state.
1531 * We can now skip the test for the RST flag since all paths to this
1532 * code happen after packets containing RST have been dropped.
1534 * In the SYN-RECEIVED state, don't send an ACK unless the segment
1535 * we received passes the SYN-RECEIVED ACK test. If it fails send a
1536 * RST. This breaks the loop in the "LAND" DoS attack, and also
1537 * prevents an ACK storm between two listening ports that have been
1538 * sent forged SYN segments, each with the source address of the
1541 struct tcp_rack *rack;
1543 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
1544 (SEQ_GT(tp->snd_una, th->th_ack) ||
1545 SEQ_GT(th->th_ack, tp->snd_max))) {
1547 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
1551 rack = (struct tcp_rack *)tp->t_fb_ptr;
1552 rack->r_wanted_output++;
1553 tp->t_flags |= TF_ACKNOW;
1560 rack_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, struct tcpcb *tp)
1563 * RFC5961 Section 3.2
1565 * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in
1566 * window, we send challenge ACK.
1568 * Note: to take into account delayed ACKs, we should test against
1569 * last_ack_sent instead of rcv_nxt. Note 2: we handle special case
1570 * of closed window, not covered by the RFC.
1574 if ((SEQ_GEQ(th->th_seq, (tp->last_ack_sent - 1)) &&
1575 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
1576 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
1578 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1579 KASSERT(tp->t_state != TCPS_SYN_SENT,
1580 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
1583 if (V_tcp_insecure_rst ||
1584 (tp->last_ack_sent == th->th_seq) ||
1585 (tp->rcv_nxt == th->th_seq) ||
1586 ((tp->last_ack_sent - 1) == th->th_seq)) {
1587 TCPSTAT_INC(tcps_drops);
1588 /* Drop the connection. */
1589 switch (tp->t_state) {
1590 case TCPS_SYN_RECEIVED:
1591 so->so_error = ECONNREFUSED;
1593 case TCPS_ESTABLISHED:
1594 case TCPS_FIN_WAIT_1:
1595 case TCPS_FIN_WAIT_2:
1596 case TCPS_CLOSE_WAIT:
1599 so->so_error = ECONNRESET;
1601 tcp_state_change(tp, TCPS_CLOSED);
1607 rack_do_drop(m, tp);
1609 TCPSTAT_INC(tcps_badrst);
1610 /* Send challenge ACK. */
1611 tcp_respond(tp, mtod(m, void *), th, m,
1612 tp->rcv_nxt, tp->snd_nxt, TH_ACK);
1613 tp->last_ack_sent = tp->rcv_nxt;
1622 * The value in ret_val informs the caller
1623 * if we dropped the tcb (and lock) or not.
1624 * 1 = we dropped it, 0 = the TCB is still locked
1628 rack_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * ret_val)
1630 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1632 TCPSTAT_INC(tcps_badsyn);
1633 if (V_tcp_insecure_syn &&
1634 SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1635 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1636 tp = tcp_drop(tp, ECONNRESET);
1638 rack_do_drop(m, tp);
1640 /* Send challenge ACK. */
1641 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
1642 tp->snd_nxt, TH_ACK);
1643 tp->last_ack_sent = tp->rcv_nxt;
1646 rack_do_drop(m, NULL);
1651 * rack_ts_check returns 1 for you should not proceed. It places
1652 * in ret_val what should be returned 1/0 by the caller. The 1 indicates
1653 * that the TCB is unlocked and probably dropped. The 0 indicates the
1654 * TCB is still valid and locked.
1657 rack_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t tlen, int32_t thflags, int32_t * ret_val)
1660 /* Check to see if ts_recent is over 24 days old. */
1661 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
1663 * Invalidate ts_recent. If this segment updates ts_recent,
1664 * the age will be reset later and ts_recent will get a
1665 * valid value. If it does not, setting ts_recent to zero
1666 * will at least satisfy the requirement that zero be placed
1667 * in the timestamp echo reply when ts_recent isn't valid.
1668 * The age isn't reset until we get a valid ts_recent
1669 * because we don't want out-of-order segments to be dropped
1670 * when ts_recent is old.
1674 TCPSTAT_INC(tcps_rcvduppack);
1675 TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
1676 TCPSTAT_INC(tcps_pawsdrop);
1679 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
1681 rack_do_drop(m, NULL);
1689 * rack_drop_checks returns 1 for you should not proceed. It places
1690 * in ret_val what should be returned 1/0 by the caller. The 1 indicates
1691 * that the TCB is unlocked and probably dropped. The 0 indicates the
1692 * TCB is still valid and locked.
1695 rack_drop_checks(struct tcpopt *to, struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * tlenp, int32_t * thf, int32_t * drop_hdrlen, int32_t * ret_val)
1703 todrop = tp->rcv_nxt - th->th_seq;
1705 if (thflags & TH_SYN) {
1715 * Following if statement from Stevens, vol. 2, p. 960.
1718 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1720 * Any valid FIN must be to the left of the window.
1721 * At this point the FIN must be a duplicate or out
1722 * of sequence; drop it.
1726 * Send an ACK to resynchronize and drop any data.
1727 * But keep on processing for RST or ACK.
1729 tp->t_flags |= TF_ACKNOW;
1731 TCPSTAT_INC(tcps_rcvduppack);
1732 TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
1734 TCPSTAT_INC(tcps_rcvpartduppack);
1735 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
1737 *drop_hdrlen += todrop; /* drop from the top afterwards */
1738 th->th_seq += todrop;
1740 if (th->th_urp > todrop)
1741 th->th_urp -= todrop;
1748 * If segment ends after window, drop trailing data (and PUSH and
1749 * FIN); if nothing left, just ACK.
1751 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
1753 TCPSTAT_INC(tcps_rcvpackafterwin);
1754 if (todrop >= tlen) {
1755 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
1757 * If window is closed can only take segments at
1758 * window edge, and have to drop data and PUSH from
1759 * incoming segments. Continue processing, but
1760 * remember to ack. Otherwise, drop segment and
1763 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1764 tp->t_flags |= TF_ACKNOW;
1765 TCPSTAT_INC(tcps_rcvwinprobe);
1767 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
1771 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1774 thflags &= ~(TH_PUSH | TH_FIN);
1781 static struct rack_sendmap *
1782 rack_find_lowest_rsm(struct tcp_rack *rack)
1784 struct rack_sendmap *rsm;
1787 * Walk the time-order transmitted list looking for an rsm that is
1788 * not acked. This will be the one that was sent the longest time
1789 * ago that is still outstanding.
1791 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) {
1792 if (rsm->r_flags & RACK_ACKED) {
1801 static struct rack_sendmap *
1802 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm)
1804 struct rack_sendmap *prsm;
1807 * Walk the sequence order list backward until we hit and arrive at
1808 * the highest seq not acked. In theory when this is called it
1809 * should be the last segment (which it was not).
1811 counter_u64_add(rack_find_high, 1);
1813 TAILQ_FOREACH_REVERSE_FROM(prsm, &rack->r_ctl.rc_map, rack_head, r_next) {
1814 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) {
1824 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts)
1830 * lro is the flag we use to determine if we have seen reordering.
1831 * If it gets set we have seen reordering. The reorder logic either
1832 * works in one of two ways:
1834 * If reorder-fade is configured, then we track the last time we saw
1835 * re-ordering occur. If we reach the point where enough time as
1836 * passed we no longer consider reordering has occuring.
1838 * Or if reorder-face is 0, then once we see reordering we consider
1839 * the connection to alway be subject to reordering and just set lro
1842 * In the end if lro is non-zero we add the extra time for
1847 if (rack->r_ctl.rc_reorder_ts) {
1848 if (rack->r_ctl.rc_reorder_fade) {
1849 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) {
1850 lro = cts - rack->r_ctl.rc_reorder_ts;
1853 * No time as passed since the last
1854 * reorder, mark it as reordering.
1859 /* Negative time? */
1862 if (lro > rack->r_ctl.rc_reorder_fade) {
1863 /* Turn off reordering seen too */
1864 rack->r_ctl.rc_reorder_ts = 0;
1868 /* Reodering does not fade */
1874 thresh = srtt + rack->r_ctl.rc_pkt_delay;
1876 /* It must be set, if not you get 1/4 rtt */
1877 if (rack->r_ctl.rc_reorder_shift)
1878 thresh += (srtt >> rack->r_ctl.rc_reorder_shift);
1880 thresh += (srtt >> 2);
1884 /* We don't let the rack timeout be above a RTO */
1886 if (thresh > TICKS_2_MSEC(rack->rc_tp->t_rxtcur)) {
1887 thresh = TICKS_2_MSEC(rack->rc_tp->t_rxtcur);
1889 /* And we don't want it above the RTO max either */
1890 if (thresh > rack_rto_max) {
1891 thresh = rack_rto_max;
1897 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack,
1898 struct rack_sendmap *rsm, uint32_t srtt)
1900 struct rack_sendmap *prsm;
1901 uint32_t thresh, len;
1906 if (rack->r_ctl.rc_tlp_threshold)
1907 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold);
1909 thresh = (srtt * 2);
1911 /* Get the previous sent packet, if any */
1912 maxseg = tcp_maxseg(tp);
1913 counter_u64_add(rack_enter_tlp_calc, 1);
1914 len = rsm->r_end - rsm->r_start;
1915 if (rack->rack_tlp_threshold_use == TLP_USE_ID) {
1916 /* Exactly like the ID */
1917 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= maxseg) {
1918 uint32_t alt_thresh;
1920 * Compensate for delayed-ack with the d-ack time.
1922 counter_u64_add(rack_used_tlpmethod, 1);
1923 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
1924 if (alt_thresh > thresh)
1925 thresh = alt_thresh;
1927 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) {
1929 prsm = TAILQ_PREV(rsm, rack_head, r_tnext);
1930 if (prsm && (len <= maxseg)) {
1932 * Two packets outstanding, thresh should be (2*srtt) +
1933 * possible inter-packet delay (if any).
1935 uint32_t inter_gap = 0;
1938 counter_u64_add(rack_used_tlpmethod, 1);
1939 idx = rsm->r_rtr_cnt - 1;
1940 nidx = prsm->r_rtr_cnt - 1;
1941 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) {
1942 /* Yes it was sent later (or at the same time) */
1943 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx];
1945 thresh += inter_gap;
1946 } else if (len <= maxseg) {
1948 * Possibly compensate for delayed-ack.
1950 uint32_t alt_thresh;
1952 counter_u64_add(rack_used_tlpmethod2, 1);
1953 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
1954 if (alt_thresh > thresh)
1955 thresh = alt_thresh;
1957 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) {
1959 if (len <= maxseg) {
1960 uint32_t alt_thresh;
1962 * Compensate for delayed-ack with the d-ack time.
1964 counter_u64_add(rack_used_tlpmethod, 1);
1965 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time;
1966 if (alt_thresh > thresh)
1967 thresh = alt_thresh;
1970 /* Not above an RTO */
1971 if (thresh > TICKS_2_MSEC(tp->t_rxtcur)) {
1972 thresh = TICKS_2_MSEC(tp->t_rxtcur);
1974 /* Not above a RTO max */
1975 if (thresh > rack_rto_max) {
1976 thresh = rack_rto_max;
1978 /* Apply user supplied min TLP */
1979 if (thresh < rack_tlp_min) {
1980 thresh = rack_tlp_min;
1985 static struct rack_sendmap *
1986 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused)
1989 * Check to see that we don't need to fall into recovery. We will
1990 * need to do so if our oldest transmit is past the time we should
1993 struct tcp_rack *rack;
1994 struct rack_sendmap *rsm;
1996 uint32_t srtt_cur, srtt, thresh;
1998 rack = (struct tcp_rack *)tp->t_fb_ptr;
1999 if (TAILQ_EMPTY(&rack->r_ctl.rc_map)) {
2002 srtt_cur = tp->t_srtt >> TCP_RTT_SHIFT;
2003 srtt = TICKS_2_MSEC(srtt_cur);
2004 if (rack->rc_rack_rtt && (srtt > rack->rc_rack_rtt))
2005 srtt = rack->rc_rack_rtt;
2007 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
2011 if (rsm->r_flags & RACK_ACKED) {
2012 rsm = rack_find_lowest_rsm(rack);
2016 idx = rsm->r_rtr_cnt - 1;
2017 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
2018 if (tsused < rsm->r_tim_lastsent[idx]) {
2021 if ((tsused - rsm->r_tim_lastsent[idx]) < thresh) {
2024 /* Ok if we reach here we are over-due */
2025 rack->r_ctl.rc_rsm_start = rsm->r_start;
2026 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
2027 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
2028 rack_cong_signal(tp, NULL, CC_NDUPACK);
2033 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack)
2039 t = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT) + ((tp->t_rttvar * 4) >> TCP_RTT_SHIFT));
2040 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
2041 tcp_persmin, tcp_persmax);
2042 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
2044 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT;
2045 ret_val = (uint32_t)tt;
2050 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2053 * Start the FR timer, we do this based on getting the first one in
2054 * the rc_tmap. Note that if its NULL we must stop the timer. in all
2055 * events we need to stop the running timer (if its running) before
2056 * starting the new one.
2058 uint32_t thresh, exp, to, srtt, time_since_sent;
2061 int32_t is_tlp_timer = 0;
2062 struct rack_sendmap *rsm;
2064 if (rack->t_timers_stopped) {
2065 /* All timers have been stopped none are to run */
2068 if (rack->rc_in_persist) {
2069 /* We can't start any timer in persists */
2070 return (rack_get_persists_timer_val(tp, rack));
2072 if (tp->t_state < TCPS_ESTABLISHED)
2074 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
2076 /* Nothing on the send map */
2078 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) {
2079 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT;
2080 to = TICKS_2_MSEC(tp->t_rxtcur);
2087 if (rsm->r_flags & RACK_ACKED) {
2088 rsm = rack_find_lowest_rsm(rack);
2094 /* Convert from ms to usecs */
2095 if (rsm->r_flags & RACK_SACK_PASSED) {
2096 if ((tp->t_flags & TF_SENTFIN) &&
2097 ((tp->snd_max - tp->snd_una) == 1) &&
2098 (rsm->r_flags & RACK_HAS_FIN)) {
2100 * We don't start a rack timer if all we have is a
2106 srtt_cur = (tp->t_srtt >> TCP_RTT_SHIFT);
2107 srtt = TICKS_2_MSEC(srtt_cur);
2109 srtt = RACK_INITIAL_RTO;
2111 thresh = rack_calc_thresh_rack(rack, srtt, cts);
2112 idx = rsm->r_rtr_cnt - 1;
2113 exp = rsm->r_tim_lastsent[idx] + thresh;
2114 if (SEQ_GEQ(exp, cts)) {
2116 if (to < rack->r_ctl.rc_min_to) {
2117 to = rack->r_ctl.rc_min_to;
2120 to = rack->r_ctl.rc_min_to;
2123 /* Ok we need to do a TLP not RACK */
2124 if ((rack->rc_tlp_in_progress != 0) ||
2125 (rack->r_ctl.rc_tlp_rtx_out != 0)) {
2127 * The previous send was a TLP or a tlp_rtx is in
2132 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
2134 /* We found no rsm to TLP with. */
2137 if (rsm->r_flags & RACK_HAS_FIN) {
2138 /* If its a FIN we dont do TLP */
2142 idx = rsm->r_rtr_cnt - 1;
2143 if (TSTMP_GT(cts, rsm->r_tim_lastsent[idx]))
2144 time_since_sent = cts - rsm->r_tim_lastsent[idx];
2146 time_since_sent = 0;
2149 srtt_cur = (tp->t_srtt >> TCP_RTT_SHIFT);
2150 srtt = TICKS_2_MSEC(srtt_cur);
2152 srtt = RACK_INITIAL_RTO;
2153 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt);
2154 if (thresh > time_since_sent)
2155 to = thresh - time_since_sent;
2157 to = rack->r_ctl.rc_min_to;
2158 if (to > TCPTV_REXMTMAX) {
2160 * If the TLP time works out to larger than the max
2161 * RTO lets not do TLP.. just RTO.
2165 if (rsm->r_start != rack->r_ctl.rc_last_tlp_seq) {
2167 * The tail is no longer the last one I did a probe
2170 rack->r_ctl.rc_tlp_seg_send_cnt = 0;
2171 rack->r_ctl.rc_last_tlp_seq = rsm->r_start;
2174 if (is_tlp_timer == 0) {
2175 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK;
2177 if ((rack->r_ctl.rc_tlp_send_cnt > rack_tlp_max_resend) ||
2178 (rack->r_ctl.rc_tlp_seg_send_cnt > rack_tlp_max_resend)) {
2180 * We have exceeded how many times we can retran the
2181 * current TLP timer, switch to the RTO timer.
2185 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP;
2194 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2196 if (rack->rc_in_persist == 0) {
2197 if (((tp->t_flags & TF_SENTFIN) == 0) &&
2198 (tp->snd_max - tp->snd_una) >= sbavail(&rack->rc_inp->inp_socket->so_snd))
2199 /* Must need to send more data to enter persist */
2201 rack->r_ctl.rc_went_idle_time = cts;
2202 rack_timer_cancel(tp, rack, cts, __LINE__);
2204 rack->rc_in_persist = 1;
2209 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack)
2211 if (rack->rc_inp->inp_in_hpts) {
2212 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
2213 rack->r_ctl.rc_hpts_flags = 0;
2215 rack->rc_in_persist = 0;
2216 rack->r_ctl.rc_went_idle_time = 0;
2217 tp->t_flags &= ~TF_FORCEDATA;
2222 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts, int32_t line,
2223 int32_t slot, uint32_t tot_len_this_send, int32_t frm_out_sbavail)
2226 uint32_t delayed_ack = 0;
2227 uint32_t hpts_timeout;
2232 if (inp->inp_in_hpts) {
2233 /* A previous call is already set up */
2236 if (tp->t_state == TCPS_CLOSED) {
2239 stopped = rack->rc_tmr_stopped;
2240 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) {
2241 left = rack->r_ctl.rc_timer_exp - cts;
2243 rack->r_ctl.rc_timer_exp = 0;
2244 if (rack->rc_inp->inp_in_hpts == 0) {
2245 rack->r_ctl.rc_hpts_flags = 0;
2248 /* We are hptsi too */
2249 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT;
2250 } else if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
2252 * We are still left on the hpts when the to goes
2253 * it will be for output.
2255 if (TSTMP_GT(cts, rack->r_ctl.rc_last_output_to))
2256 slot = cts - rack->r_ctl.rc_last_output_to;
2260 if ((tp->snd_wnd == 0) && TCPS_HAVEESTABLISHED(tp->t_state)) {
2261 /* No send window.. we must enter persist */
2262 rack_enter_persist(tp, rack, cts);
2263 } else if ((frm_out_sbavail &&
2264 (frm_out_sbavail > (tp->snd_max - tp->snd_una)) &&
2265 (tp->snd_wnd < tp->t_maxseg)) &&
2266 TCPS_HAVEESTABLISHED(tp->t_state)) {
2268 * If we have no window or we can't send a segment (and have
2269 * data to send.. we cheat here and frm_out_sbavail is
2270 * passed in with the sbavail(sb) only from bbr_output) and
2271 * we are established, then we must enter persits (if not
2272 * already in persits).
2274 rack_enter_persist(tp, rack, cts);
2276 hpts_timeout = rack_timer_start(tp, rack, cts);
2277 if (tp->t_flags & TF_DELACK) {
2278 delayed_ack = TICKS_2_MSEC(tcp_delacktime);
2279 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK;
2281 if (delayed_ack && ((hpts_timeout == 0) ||
2282 (delayed_ack < hpts_timeout)))
2283 hpts_timeout = delayed_ack;
2285 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
2287 * If no timers are going to run and we will fall off the hptsi
2288 * wheel, we resort to a keep-alive timer if its configured.
2290 if ((hpts_timeout == 0) &&
2292 if ((tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
2293 (tp->t_state <= TCPS_CLOSING)) {
2295 * Ok we have no timer (persists, rack, tlp, rxt or
2296 * del-ack), we don't have segments being paced. So
2297 * all that is left is the keepalive timer.
2299 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2300 /* Get the established keep-alive time */
2301 hpts_timeout = TP_KEEPIDLE(tp);
2303 /* Get the initial setup keep-alive time */
2304 hpts_timeout = TP_KEEPINIT(tp);
2306 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP;
2309 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) ==
2310 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) {
2312 * RACK, TLP, persists and RXT timers all are restartable
2313 * based on actions input .. i.e we received a packet (ack
2314 * or sack) and that changes things (rw, or snd_una etc).
2315 * Thus we can restart them with a new value. For
2316 * keep-alive, delayed_ack we keep track of what was left
2317 * and restart the timer with a smaller value.
2319 if (left < hpts_timeout)
2320 hpts_timeout = left;
2324 * Hack alert for now we can't time-out over 2,147,483
2325 * seconds (a bit more than 596 hours), which is probably ok
2328 if (hpts_timeout > 0x7ffffffe)
2329 hpts_timeout = 0x7ffffffe;
2330 rack->r_ctl.rc_timer_exp = cts + hpts_timeout;
2333 rack->r_ctl.rc_last_output_to = cts + slot;
2334 if ((hpts_timeout == 0) || (hpts_timeout > slot)) {
2335 if (rack->rc_inp->inp_in_hpts == 0)
2336 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(slot));
2337 rack_log_to_start(rack, cts, hpts_timeout, slot, 1);
2340 * Arrange for the hpts to kick back in after the
2341 * t-o if the t-o does not cause a send.
2343 if (rack->rc_inp->inp_in_hpts == 0)
2344 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout));
2345 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
2347 } else if (hpts_timeout) {
2348 if (rack->rc_inp->inp_in_hpts == 0)
2349 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout));
2350 rack_log_to_start(rack, cts, hpts_timeout, slot, 0);
2352 /* No timer starting */
2354 if (SEQ_GT(tp->snd_max, tp->snd_una)) {
2355 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?",
2356 tp, rack, tot_len_this_send, cts, slot, hpts_timeout);
2360 rack->rc_tmr_stopped = 0;
2362 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, cts);
2366 * RACK Timer, here we simply do logging and house keeping.
2367 * the normal rack_output() function will call the
2368 * appropriate thing to check if we need to do a RACK retransmit.
2369 * We return 1, saying don't proceed with rack_output only
2370 * when all timers have been stopped (destroyed PCB?).
2373 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2376 * This timer simply provides an internal trigger to send out data.
2377 * The check_recovery_mode call will see if there are needed
2378 * retransmissions, if so we will enter fast-recovery. The output
2379 * call may or may not do the same thing depending on sysctl
2382 struct rack_sendmap *rsm;
2385 if (tp->t_timers->tt_flags & TT_STOPPED) {
2388 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
2389 /* Its not time yet */
2392 rack_log_to_event(rack, RACK_TO_FRM_RACK);
2393 recovery = IN_RECOVERY(tp->t_flags);
2394 counter_u64_add(rack_to_tot, 1);
2395 if (rack->r_state && (rack->r_state != tp->t_state))
2396 rack_set_state(tp, rack);
2397 rsm = rack_check_recovery_mode(tp, cts);
2401 rtt = rack->rc_rack_rtt;
2404 if ((recovery == 0) &&
2405 (rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg)) {
2407 * The rack-timeout that enter's us into recovery
2408 * will force out one MSS and set us up so that we
2409 * can do one more send in 2*rtt (transitioning the
2410 * rack timeout into a rack-tlp).
2412 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
2413 } else if ((rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg) &&
2414 ((rsm->r_end - rsm->r_start) > rack->r_ctl.rc_prr_sndcnt)) {
2416 * When a rack timer goes, we have to send at
2417 * least one segment. They will be paced a min of 1ms
2418 * apart via the next rack timer (or further
2419 * if the rack timer dictates it).
2421 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
2424 /* This is a case that should happen rarely if ever */
2425 counter_u64_add(rack_tlp_does_nada, 1);
2427 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
2429 rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
2431 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK;
2436 * TLP Timer, here we simply setup what segment we want to
2437 * have the TLP expire on, the normal rack_output() will then
2440 * We return 1, saying don't proceed with rack_output only
2441 * when all timers have been stopped (destroyed PCB?).
2444 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2449 struct rack_sendmap *rsm = NULL;
2451 uint32_t amm, old_prr_snd = 0;
2452 uint32_t out, avail;
2454 if (tp->t_timers->tt_flags & TT_STOPPED) {
2457 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
2458 /* Its not time yet */
2461 if (rack_progress_timeout_check(tp)) {
2462 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
2466 * A TLP timer has expired. We have been idle for 2 rtts. So we now
2467 * need to figure out how to force a full MSS segment out.
2469 rack_log_to_event(rack, RACK_TO_FRM_TLP);
2470 counter_u64_add(rack_tlp_tot, 1);
2471 if (rack->r_state && (rack->r_state != tp->t_state))
2472 rack_set_state(tp, rack);
2473 so = tp->t_inpcb->inp_socket;
2474 avail = sbavail(&so->so_snd);
2475 out = tp->snd_max - tp->snd_una;
2476 rack->rc_timer_up = 1;
2478 * If we are in recovery we can jazz out a segment if new data is
2479 * present simply by setting rc_prr_sndcnt to a segment.
2481 if ((avail > out) &&
2482 ((rack_always_send_oldest == 0) || (TAILQ_EMPTY(&rack->r_ctl.rc_tmap)))) {
2483 /* New data is available */
2485 if (amm > tp->t_maxseg) {
2487 } else if ((amm < tp->t_maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) {
2488 /* not enough to fill a MTU and no-delay is off */
2491 if (IN_RECOVERY(tp->t_flags)) {
2493 old_prr_snd = rack->r_ctl.rc_prr_sndcnt;
2494 if (out + amm <= tp->snd_wnd)
2495 rack->r_ctl.rc_prr_sndcnt = amm;
2499 /* Set the send-new override */
2500 if (out + amm <= tp->snd_wnd)
2501 rack->r_ctl.rc_tlp_new_data = amm;
2505 rack->r_ctl.rc_tlp_seg_send_cnt = 0;
2506 rack->r_ctl.rc_last_tlp_seq = tp->snd_max;
2507 rack->r_ctl.rc_tlpsend = NULL;
2508 counter_u64_add(rack_tlp_newdata, 1);
2513 * Ok we need to arrange the last un-acked segment to be re-sent, or
2514 * optionally the first un-acked segment.
2516 if (rack_always_send_oldest)
2517 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
2519 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next);
2520 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) {
2521 rsm = rack_find_high_nonack(rack, rsm);
2525 counter_u64_add(rack_tlp_does_nada, 1);
2527 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true);
2531 if ((rsm->r_end - rsm->r_start) > tp->t_maxseg) {
2533 * We need to split this the last segment in two.
2536 struct rack_sendmap *nrsm;
2538 nrsm = rack_alloc(rack);
2541 * No memory to split, we will just exit and punt
2542 * off to the RXT timer.
2544 counter_u64_add(rack_tlp_does_nada, 1);
2547 nrsm->r_start = (rsm->r_end - tp->t_maxseg);
2548 nrsm->r_end = rsm->r_end;
2549 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
2550 nrsm->r_flags = rsm->r_flags;
2551 nrsm->r_sndcnt = rsm->r_sndcnt;
2552 nrsm->r_rtr_bytes = 0;
2553 rsm->r_end = nrsm->r_start;
2554 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
2555 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
2557 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next);
2558 if (rsm->r_in_tmap) {
2559 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
2560 nrsm->r_in_tmap = 1;
2562 rsm->r_flags &= (~RACK_HAS_FIN);
2565 rack->r_ctl.rc_tlpsend = rsm;
2566 rack->r_ctl.rc_tlp_rtx_out = 1;
2567 if (rsm->r_start == rack->r_ctl.rc_last_tlp_seq) {
2568 rack->r_ctl.rc_tlp_seg_send_cnt++;
2571 rack->r_ctl.rc_last_tlp_seq = rsm->r_start;
2572 rack->r_ctl.rc_tlp_seg_send_cnt = 1;
2575 rack->r_ctl.rc_tlp_send_cnt++;
2576 if (rack->r_ctl.rc_tlp_send_cnt > rack_tlp_max_resend) {
2578 * Can't [re]/transmit a segment we have not heard from the
2579 * peer in max times. We need the retransmit timer to take
2583 rack->r_ctl.rc_tlpsend = NULL;
2585 rsm->r_flags &= ~RACK_TLP;
2586 rack->r_ctl.rc_prr_sndcnt = old_prr_snd;
2587 counter_u64_add(rack_tlp_retran_fail, 1);
2590 rsm->r_flags |= RACK_TLP;
2592 if (rsm && (rsm->r_start == rack->r_ctl.rc_last_tlp_seq) &&
2593 (rack->r_ctl.rc_tlp_seg_send_cnt > rack_tlp_max_resend)) {
2595 * We don't want to send a single segment more than the max
2600 rack->r_timer_override = 1;
2601 rack->r_tlp_running = 1;
2602 rack->rc_tlp_in_progress = 1;
2603 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
2606 rack->rc_timer_up = 0;
2607 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP;
2612 * Delayed ack Timer, here we simply need to setup the
2613 * ACK_NOW flag and remove the DELACK flag. From there
2614 * the output routine will send the ack out.
2616 * We only return 1, saying don't proceed, if all timers
2617 * are stopped (destroyed PCB?).
2620 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2622 if (tp->t_timers->tt_flags & TT_STOPPED) {
2625 rack_log_to_event(rack, RACK_TO_FRM_DELACK);
2626 tp->t_flags &= ~TF_DELACK;
2627 tp->t_flags |= TF_ACKNOW;
2628 TCPSTAT_INC(tcps_delack);
2629 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK;
2634 * Persists timer, here we simply need to setup the
2635 * FORCE-DATA flag the output routine will send
2636 * the one byte send.
2638 * We only return 1, saying don't proceed, if all timers
2639 * are stopped (destroyed PCB?).
2642 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2649 if (tp->t_timers->tt_flags & TT_STOPPED) {
2652 if (rack->rc_in_persist == 0)
2654 if (rack_progress_timeout_check(tp)) {
2655 tcp_set_inp_to_drop(inp, ETIMEDOUT);
2658 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
2660 * Persistence timer into zero window. Force a byte to be output, if
2663 TCPSTAT_INC(tcps_persisttimeo);
2665 * Hack: if the peer is dead/unreachable, we do not time out if the
2666 * window is closed. After a full backoff, drop the connection if
2667 * the idle time (no responses to probes) reaches the maximum
2668 * backoff that we would use if retransmitting.
2670 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
2671 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
2672 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
2673 TCPSTAT_INC(tcps_persistdrop);
2675 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
2678 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) &&
2679 tp->snd_una == tp->snd_max)
2680 rack_exit_persist(tp, rack);
2681 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT;
2683 * If the user has closed the socket then drop a persisting
2684 * connection after a much reduced timeout.
2686 if (tp->t_state > TCPS_CLOSE_WAIT &&
2687 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
2689 TCPSTAT_INC(tcps_persistdrop);
2690 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
2693 tp->t_flags |= TF_FORCEDATA;
2695 rack_log_to_event(rack, RACK_TO_FRM_PERSIST);
2700 * If a keepalive goes off, we had no other timers
2701 * happening. We always return 1 here since this
2702 * routine either drops the connection or sends
2703 * out a segment with respond.
2706 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2708 struct tcptemp *t_template;
2711 if (tp->t_timers->tt_flags & TT_STOPPED) {
2714 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP;
2716 rack_log_to_event(rack, RACK_TO_FRM_KEEP);
2718 * Keep-alive timer went off; send something or drop connection if
2719 * idle for too long.
2721 TCPSTAT_INC(tcps_keeptimeo);
2722 if (tp->t_state < TCPS_ESTABLISHED)
2724 if ((tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
2725 tp->t_state <= TCPS_CLOSING) {
2726 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
2729 * Send a packet designed to force a response if the peer is
2730 * up and reachable: either an ACK if the connection is
2731 * still alive, or an RST if the peer has closed the
2732 * connection due to timeout or reboot. Using sequence
2733 * number tp->snd_una-1 causes the transmitted zero-length
2734 * segment to lie outside the receive window; by the
2735 * protocol spec, this requires the correspondent TCP to
2738 TCPSTAT_INC(tcps_keepprobe);
2739 t_template = tcpip_maketemplate(inp);
2741 tcp_respond(tp, t_template->tt_ipgen,
2742 &t_template->tt_t, (struct mbuf *)NULL,
2743 tp->rcv_nxt, tp->snd_una - 1, 0);
2744 free(t_template, M_TEMP);
2747 rack_start_hpts_timer(rack, tp, cts, __LINE__, 0, 0, 0);
2750 TCPSTAT_INC(tcps_keepdrops);
2751 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT);
2756 * Retransmit helper function, clear up all the ack
2757 * flags and take care of important book keeping.
2760 rack_remxt_tmr(struct tcpcb *tp)
2763 * The retransmit timer went off, all sack'd blocks must be
2766 struct rack_sendmap *rsm, *trsm = NULL;
2767 struct tcp_rack *rack;
2770 rack = (struct tcp_rack *)tp->t_fb_ptr;
2771 rack_timer_cancel(tp, rack, tcp_ts_getticks(), __LINE__);
2772 rack_log_to_event(rack, RACK_TO_FRM_TMR);
2773 if (rack->r_state && (rack->r_state != tp->t_state))
2774 rack_set_state(tp, rack);
2776 * Ideally we would like to be able to
2777 * mark SACK-PASS on anything not acked here.
2778 * However, if we do that we would burst out
2779 * all that data 1ms apart. This would be unwise,
2780 * so for now we will just let the normal rxt timer
2781 * and tlp timer take care of it.
2783 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) {
2784 if (rsm->r_flags & RACK_ACKED) {
2787 if (rsm->r_in_tmap == 0) {
2788 /* We must re-add it back to the tlist */
2790 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
2792 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext);
2798 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS);
2800 /* Clear the count (we just un-acked them) */
2801 rack->r_ctl.rc_sacked = 0;
2802 /* Clear the tlp rtx mark */
2803 rack->r_ctl.rc_tlp_rtx_out = 0;
2804 rack->r_ctl.rc_tlp_seg_send_cnt = 0;
2805 rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_map);
2806 /* Setup so we send one segment */
2807 if (rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg)
2808 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
2809 rack->r_timer_override = 1;
2813 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise
2814 * we will setup to retransmit the lowest seq number outstanding.
2817 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts)
2824 if (tp->t_timers->tt_flags & TT_STOPPED) {
2827 if (rack_progress_timeout_check(tp)) {
2828 tcp_set_inp_to_drop(inp, ETIMEDOUT);
2831 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT;
2832 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
2833 (tp->snd_una == tp->snd_max)) {
2834 /* Nothing outstanding .. nothing to do */
2838 * Retransmission timer went off. Message has not been acked within
2839 * retransmit interval. Back off to a longer retransmit interval
2840 * and retransmit one segment.
2842 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
2843 tp->t_rxtshift = TCP_MAXRXTSHIFT;
2844 TCPSTAT_INC(tcps_timeoutdrop);
2846 tcp_set_inp_to_drop(rack->rc_inp,
2847 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT));
2851 if (tp->t_state == TCPS_SYN_SENT) {
2853 * If the SYN was retransmitted, indicate CWND to be limited
2854 * to 1 segment in cc_conn_init().
2857 } else if (tp->t_rxtshift == 1) {
2859 * first retransmit; record ssthresh and cwnd so they can be
2860 * recovered if this turns out to be a "bad" retransmit. A
2861 * retransmit is considered "bad" if an ACK for this segment
2862 * is received within RTT/2 interval; the assumption here is
2863 * that the ACK was already in flight. See "On Estimating
2864 * End-to-End Network Path Properties" by Allman and Paxson
2867 tp->snd_cwnd_prev = tp->snd_cwnd;
2868 tp->snd_ssthresh_prev = tp->snd_ssthresh;
2869 tp->snd_recover_prev = tp->snd_recover;
2870 if (IN_FASTRECOVERY(tp->t_flags))
2871 tp->t_flags |= TF_WASFRECOVERY;
2873 tp->t_flags &= ~TF_WASFRECOVERY;
2874 if (IN_CONGRECOVERY(tp->t_flags))
2875 tp->t_flags |= TF_WASCRECOVERY;
2877 tp->t_flags &= ~TF_WASCRECOVERY;
2878 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
2879 tp->t_flags |= TF_PREVVALID;
2881 tp->t_flags &= ~TF_PREVVALID;
2882 TCPSTAT_INC(tcps_rexmttimeo);
2883 if ((tp->t_state == TCPS_SYN_SENT) ||
2884 (tp->t_state == TCPS_SYN_RECEIVED))
2885 rexmt = MSEC_2_TICKS(RACK_INITIAL_RTO * tcp_syn_backoff[tp->t_rxtshift]);
2887 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
2888 TCPT_RANGESET(tp->t_rxtcur, rexmt,
2889 max(MSEC_2_TICKS(rack_rto_min), rexmt),
2890 MSEC_2_TICKS(rack_rto_max));
2892 * We enter the path for PLMTUD if connection is established or, if
2893 * connection is FIN_WAIT_1 status, reason for the last is that if
2894 * amount of data we send is very small, we could send it in couple
2895 * of packets and process straight to FIN. In that case we won't
2896 * catch ESTABLISHED state.
2898 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
2899 || (tp->t_state == TCPS_FIN_WAIT_1))) {
2905 * Idea here is that at each stage of mtu probe (usually,
2906 * 1448 -> 1188 -> 524) should be given 2 chances to recover
2907 * before further clamping down. 'tp->t_rxtshift % 2 == 0'
2908 * should take care of that.
2910 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) ==
2911 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) &&
2912 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
2913 tp->t_rxtshift % 2 == 0)) {
2915 * Enter Path MTU Black-hole Detection mechanism: -
2916 * Disable Path MTU Discovery (IP "DF" bit). -
2917 * Reduce MTU to lower value than what we negotiated
2920 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
2921 /* Record that we may have found a black hole. */
2922 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
2923 /* Keep track of previous MSS. */
2924 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
2928 * Reduce the MSS to blackhole value or to the
2929 * default in an attempt to retransmit.
2932 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
2934 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
2935 /* Use the sysctl tuneable blackhole MSS. */
2936 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
2937 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
2938 } else if (isipv6) {
2939 /* Use the default MSS. */
2940 tp->t_maxseg = V_tcp_v6mssdflt;
2942 * Disable Path MTU Discovery when we switch
2945 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
2946 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
2949 #if defined(INET6) && defined(INET)
2953 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
2954 /* Use the sysctl tuneable blackhole MSS. */
2955 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
2956 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
2958 /* Use the default MSS. */
2959 tp->t_maxseg = V_tcp_mssdflt;
2961 * Disable Path MTU Discovery when we switch
2964 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
2965 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
2970 * If further retransmissions are still unsuccessful
2971 * with a lowered MTU, maybe this isn't a blackhole
2972 * and we restore the previous MSS and blackhole
2973 * detection flags. The limit '6' is determined by
2974 * giving each probe stage (1448, 1188, 524) 2
2975 * chances to recover.
2977 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
2978 (tp->t_rxtshift >= 6)) {
2979 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
2980 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
2981 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
2982 TCPSTAT_INC(tcps_pmtud_blackhole_failed);
2987 * Disable RFC1323 and SACK if we haven't got any response to our
2988 * third SYN to work-around some broken terminal servers (most of
2989 * which have hopefully been retired) that have bad VJ header
2990 * compression code which trashes TCP segments containing
2991 * unknown-to-them TCP options.
2993 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
2994 (tp->t_rxtshift == 3))
2995 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT);
2997 * If we backed off this far, our srtt estimate is probably bogus.
2998 * Clobber it so we'll take the next rtt measurement as our srtt;
2999 * move the current srtt into rttvar to keep the current retransmit
3002 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
3004 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
3005 in6_losing(tp->t_inpcb);
3008 in_losing(tp->t_inpcb);
3009 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
3012 if (rack_use_sack_filter)
3013 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
3014 tp->snd_recover = tp->snd_max;
3015 tp->t_flags |= TF_ACKNOW;
3017 rack_cong_signal(tp, NULL, CC_RTO);
3023 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling)
3026 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK);
3031 if (tp->t_state == TCPS_LISTEN) {
3032 /* no timers on listen sockets */
3033 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)
3037 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) {
3040 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) {
3042 rack_log_to_processing(rack, cts, ret, 0);
3045 if (hpts_calling == 0) {
3047 rack_log_to_processing(rack, cts, ret, 0);
3051 * Ok our timer went off early and we are not paced false
3052 * alarm, go back to sleep.
3055 left = rack->r_ctl.rc_timer_exp - cts;
3056 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left));
3057 rack_log_to_processing(rack, cts, ret, left);
3058 rack->rc_last_pto_set = 0;
3061 rack->rc_tmr_stopped = 0;
3062 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK;
3063 if (timers & PACE_TMR_DELACK) {
3064 ret = rack_timeout_delack(tp, rack, cts);
3065 } else if (timers & PACE_TMR_RACK) {
3066 ret = rack_timeout_rack(tp, rack, cts);
3067 } else if (timers & PACE_TMR_TLP) {
3068 ret = rack_timeout_tlp(tp, rack, cts);
3069 } else if (timers & PACE_TMR_RXT) {
3070 ret = rack_timeout_rxt(tp, rack, cts);
3071 } else if (timers & PACE_TMR_PERSIT) {
3072 ret = rack_timeout_persist(tp, rack, cts);
3073 } else if (timers & PACE_TMR_KEEP) {
3074 ret = rack_timeout_keepalive(tp, rack, cts);
3076 rack_log_to_processing(rack, cts, ret, timers);
3081 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line)
3083 uint8_t hpts_removed = 0;
3085 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) &&
3086 TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) {
3087 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
3090 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
3091 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
3092 if (rack->rc_inp->inp_in_hpts &&
3093 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) {
3095 * Canceling timer's when we have no output being
3096 * paced. We also must remove ourselves from the
3099 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
3102 rack_log_to_cancel(rack, hpts_removed, line);
3103 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK);
3108 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type)
3114 rack_stopall(struct tcpcb *tp)
3116 struct tcp_rack *rack;
3117 rack = (struct tcp_rack *)tp->t_fb_ptr;
3118 rack->t_timers_stopped = 1;
3123 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta)
3129 rack_timer_active(struct tcpcb *tp, uint32_t timer_type)
3135 rack_stop_all_timers(struct tcpcb *tp)
3137 struct tcp_rack *rack;
3140 * Assure no timers are running.
3142 if (tcp_timer_active(tp, TT_PERSIST)) {
3143 /* We enter in persists, set the flag appropriately */
3144 rack = (struct tcp_rack *)tp->t_fb_ptr;
3145 rack->rc_in_persist = 1;
3147 tcp_timer_suspend(tp, TT_PERSIST);
3148 tcp_timer_suspend(tp, TT_REXMT);
3149 tcp_timer_suspend(tp, TT_KEEP);
3150 tcp_timer_suspend(tp, TT_DELACK);
3154 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack,
3155 struct rack_sendmap *rsm, uint32_t ts)
3161 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) {
3162 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS;
3163 rsm->r_flags |= RACK_OVERMAX;
3165 if ((rsm->r_rtr_cnt > 1) && (rack->r_tlp_running == 0)) {
3166 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start);
3167 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start);
3169 idx = rsm->r_rtr_cnt - 1;
3170 rsm->r_tim_lastsent[idx] = ts;
3171 if (rsm->r_flags & RACK_ACKED) {
3172 /* Problably MTU discovery messing with us */
3173 rsm->r_flags &= ~RACK_ACKED;
3174 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
3176 if (rsm->r_in_tmap) {
3177 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
3179 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
3181 if (rsm->r_flags & RACK_SACK_PASSED) {
3182 /* We have retransmitted due to the SACK pass */
3183 rsm->r_flags &= ~RACK_SACK_PASSED;
3184 rsm->r_flags |= RACK_WAS_SACKPASS;
3186 /* Update memory for next rtr */
3187 rack->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
3192 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack,
3193 struct rack_sendmap *rsm, uint32_t ts, int32_t * lenp)
3196 * We (re-)transmitted starting at rsm->r_start for some length
3197 * (possibly less than r_end.
3199 struct rack_sendmap *nrsm;
3205 c_end = rsm->r_start + len;
3206 if (SEQ_GEQ(c_end, rsm->r_end)) {
3208 * We retransmitted the whole piece or more than the whole
3209 * slopping into the next rsm.
3211 rack_update_rsm(tp, rack, rsm, ts);
3212 if (c_end == rsm->r_end) {
3218 /* Hangs over the end return whats left */
3219 act_len = rsm->r_end - rsm->r_start;
3220 *lenp = (len - act_len);
3221 return (rsm->r_end);
3223 /* We don't get out of this block. */
3226 * Here we retransmitted less than the whole thing which means we
3227 * have to split this into what was transmitted and what was not.
3229 nrsm = rack_alloc(rack);
3232 * We can't get memory, so lets not proceed.
3238 * So here we are going to take the original rsm and make it what we
3239 * retransmitted. nrsm will be the tail portion we did not
3240 * retransmit. For example say the chunk was 1, 11 (10 bytes). And
3241 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to
3242 * 1, 6 and the new piece will be 6, 11.
3244 nrsm->r_start = c_end;
3245 nrsm->r_end = rsm->r_end;
3246 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
3247 nrsm->r_flags = rsm->r_flags;
3248 nrsm->r_sndcnt = rsm->r_sndcnt;
3249 nrsm->r_rtr_bytes = 0;
3251 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
3252 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
3254 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next);
3255 if (rsm->r_in_tmap) {
3256 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
3257 nrsm->r_in_tmap = 1;
3259 rsm->r_flags &= (~RACK_HAS_FIN);
3260 rack_update_rsm(tp, rack, rsm, ts);
3267 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len,
3268 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts,
3269 uint8_t pass, struct rack_sendmap *hintrsm)
3271 struct tcp_rack *rack;
3272 struct rack_sendmap *rsm, *nrsm;
3273 register uint32_t snd_max, snd_una;
3277 * Add to the RACK log of packets in flight or retransmitted. If
3278 * there is a TS option we will use the TS echoed, if not we will
3281 * Retransmissions will increment the count and move the ts to its
3282 * proper place. Note that if options do not include TS's then we
3283 * won't be able to effectively use the ACK for an RTT on a retran.
3285 * Notes about r_start and r_end. Lets consider a send starting at
3286 * sequence 1 for 10 bytes. In such an example the r_start would be
3287 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11.
3288 * This means that r_end is actually the first sequence for the next
3293 * If err is set what do we do XXXrrs? should we not add the thing?
3294 * -- i.e. return if err != 0 or should we pretend we sent it? --
3295 * i.e. proceed with add ** do this for now.
3297 INP_WLOCK_ASSERT(tp->t_inpcb);
3300 * We don't log errors -- we could but snd_max does not
3301 * advance in this case either.
3305 if (th_flags & TH_RST) {
3307 * We don't log resets and we return immediately from
3312 rack = (struct tcp_rack *)tp->t_fb_ptr;
3313 snd_una = tp->snd_una;
3314 if (SEQ_LEQ((seq_out + len), snd_una)) {
3315 /* Are sending an old segment to induce an ack (keep-alive)? */
3318 if (SEQ_LT(seq_out, snd_una)) {
3319 /* huh? should we panic? */
3322 end = seq_out + len;
3324 len = end - seq_out;
3326 snd_max = tp->snd_max;
3327 if (th_flags & (TH_SYN | TH_FIN)) {
3329 * The call to rack_log_output is made before bumping
3330 * snd_max. This means we can record one extra byte on a SYN
3331 * or FIN if seq_out is adding more on and a FIN is present
3332 * (and we are not resending).
3334 if (th_flags & TH_SYN)
3336 if (th_flags & TH_FIN)
3338 if (SEQ_LT(snd_max, tp->snd_nxt)) {
3340 * The add/update as not been done for the FIN/SYN
3343 snd_max = tp->snd_nxt;
3347 /* We don't log zero window probes */
3350 rack->r_ctl.rc_time_last_sent = ts;
3351 if (IN_RECOVERY(tp->t_flags)) {
3352 rack->r_ctl.rc_prr_out += len;
3354 /* First question is it a retransmission? */
3355 if (seq_out == snd_max) {
3357 rsm = rack_alloc(rack);
3360 * Hmm out of memory and the tcb got destroyed while
3364 panic("Out of memory when we should not be rack:%p", rack);
3368 if (th_flags & TH_FIN) {
3369 rsm->r_flags = RACK_HAS_FIN;
3373 rsm->r_tim_lastsent[0] = ts;
3375 rsm->r_rtr_bytes = 0;
3376 if (th_flags & TH_SYN) {
3377 /* The data space is one beyond snd_una */
3378 rsm->r_start = seq_out + 1;
3379 rsm->r_end = rsm->r_start + (len - 1);
3382 rsm->r_start = seq_out;
3383 rsm->r_end = rsm->r_start + len;
3386 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_map, rsm, r_next);
3387 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
3392 * If we reach here its a retransmission and we need to find it.
3395 if (hintrsm && (hintrsm->r_start == seq_out)) {
3398 } else if (rack->r_ctl.rc_next) {
3399 /* We have a hint from a previous run */
3400 rsm = rack->r_ctl.rc_next;
3402 /* No hints sorry */
3405 if ((rsm) && (rsm->r_start == seq_out)) {
3407 * We used rc_next or hintrsm to retransmit, hopefully the
3410 seq_out = rack_update_entry(tp, rack, rsm, ts, &len);
3417 /* Ok it was not the last pointer go through it the hard way. */
3418 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) {
3419 if (rsm->r_start == seq_out) {
3420 seq_out = rack_update_entry(tp, rack, rsm, ts, &len);
3421 rack->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next);
3428 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) {
3429 /* Transmitted within this piece */
3431 * Ok we must split off the front and then let the
3432 * update do the rest
3434 nrsm = rack_alloc(rack);
3437 panic("Ran out of memory that was preallocated? rack:%p", rack);
3439 rack_update_rsm(tp, rack, rsm, ts);
3443 * copy rsm to nrsm and then trim the front of rsm
3444 * to not include this part.
3446 nrsm->r_start = seq_out;
3447 nrsm->r_end = rsm->r_end;
3448 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
3449 nrsm->r_flags = rsm->r_flags;
3450 nrsm->r_sndcnt = rsm->r_sndcnt;
3451 nrsm->r_rtr_bytes = 0;
3452 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
3453 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
3455 rsm->r_end = nrsm->r_start;
3456 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next);
3457 if (rsm->r_in_tmap) {
3458 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
3459 nrsm->r_in_tmap = 1;
3461 rsm->r_flags &= (~RACK_HAS_FIN);
3462 seq_out = rack_update_entry(tp, rack, nrsm, ts, &len);
3469 * Hmm not found in map did they retransmit both old and on into the
3472 if (seq_out == tp->snd_max) {
3474 } else if (SEQ_LT(seq_out, tp->snd_max)) {
3476 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n",
3477 seq_out, len, tp->snd_una, tp->snd_max);
3478 printf("Starting Dump of all rack entries\n");
3479 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) {
3480 printf("rsm:%p start:%u end:%u\n",
3481 rsm, rsm->r_start, rsm->r_end);
3483 printf("Dump complete\n");
3484 panic("seq_out not found rack:%p tp:%p",
3490 * Hmm beyond sndmax? (only if we are using the new rtt-pack
3493 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p",
3494 seq_out, len, tp->snd_max, tp);
3500 * Record one of the RTT updates from an ack into
3501 * our sample structure.
3504 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt)
3506 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
3507 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) {
3508 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt;
3510 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) ||
3511 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) {
3512 rack->r_ctl.rack_rs.rs_rtt_highest = rtt;
3514 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID;
3515 rack->r_ctl.rack_rs.rs_rtt_tot += rtt;
3516 rack->r_ctl.rack_rs.rs_rtt_cnt++;
3520 * Collect new round-trip time estimate
3521 * and update averages and current timeout.
3524 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp)
3527 uint32_t o_srtt, o_var;
3530 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY)
3531 /* No valid sample */
3533 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) {
3534 /* We are to use the lowest RTT seen in a single ack */
3535 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest;
3536 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) {
3537 /* We are to use the highest RTT seen in a single ack */
3538 rtt = rack->r_ctl.rack_rs.rs_rtt_highest;
3539 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) {
3540 /* We are to use the average RTT seen in a single ack */
3541 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot /
3542 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt);
3545 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method);
3551 rack_log_rtt_sample(rack, rtt);
3552 o_srtt = tp->t_srtt;
3553 o_var = tp->t_rttvar;
3554 rack = (struct tcp_rack *)tp->t_fb_ptr;
3555 if (tp->t_srtt != 0) {
3557 * srtt is stored as fixed point with 5 bits after the
3558 * binary point (i.e., scaled by 8). The following magic is
3559 * equivalent to the smoothing algorithm in rfc793 with an
3560 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point).
3561 * Adjust rtt to origin 0.
3563 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3564 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3566 tp->t_srtt += delta;
3567 if (tp->t_srtt <= 0)
3571 * We accumulate a smoothed rtt variance (actually, a
3572 * smoothed mean difference), then set the retransmit timer
3573 * to smoothed rtt + 4 times the smoothed variance. rttvar
3574 * is stored as fixed point with 4 bits after the binary
3575 * point (scaled by 16). The following is equivalent to
3576 * rfc793 smoothing with an alpha of .75 (rttvar =
3577 * rttvar*3/4 + |delta| / 4). This replaces rfc793's
3582 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3583 tp->t_rttvar += delta;
3584 if (tp->t_rttvar <= 0)
3586 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
3587 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3590 * No rtt measurement yet - use the unsmoothed rtt. Set the
3591 * variance to half the rtt (so our first retransmit happens
3594 tp->t_srtt = rtt << TCP_RTT_SHIFT;
3595 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3596 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3598 TCPSTAT_INC(tcps_rttupdated);
3599 rack_log_rtt_upd(tp, rack, rtt, o_srtt, o_var);
3601 #ifdef NETFLIX_STATS
3602 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt));
3607 * the retransmit should happen at rtt + 4 * rttvar. Because of the
3608 * way we do the smoothing, srtt and rttvar will each average +1/2
3609 * tick of bias. When we compute the retransmit timer, we want 1/2
3610 * tick of rounding and 1 extra tick because of +-1/2 tick
3611 * uncertainty in the firing of the timer. The bias will give us
3612 * exactly the 1.5 tick we need. But, because the bias is
3613 * statistical, we have to test that we don't drop below the minimum
3614 * feasible timer (which is 2 ticks).
3616 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3617 max(MSEC_2_TICKS(rack_rto_min), rtt + 2), MSEC_2_TICKS(rack_rto_max));
3618 tp->t_softerror = 0;
3622 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm,
3623 uint32_t t, uint32_t cts)
3626 * For this RSM, we acknowledged the data from a previous
3627 * transmission, not the last one we made. This means we did a false
3630 struct tcp_rack *rack;
3632 if (rsm->r_flags & RACK_HAS_FIN) {
3634 * The sending of the FIN often is multiple sent when we
3635 * have everything outstanding ack'd. We ignore this case
3636 * since its over now.
3640 if (rsm->r_flags & RACK_TLP) {
3642 * We expect TLP's to have this occur.
3646 rack = (struct tcp_rack *)tp->t_fb_ptr;
3647 /* should we undo cc changes and exit recovery? */
3648 if (IN_RECOVERY(tp->t_flags)) {
3649 if (rack->r_ctl.rc_rsm_start == rsm->r_start) {
3651 * Undo what we ratched down and exit recovery if
3654 EXIT_RECOVERY(tp->t_flags);
3655 tp->snd_recover = tp->snd_una;
3656 if (rack->r_ctl.rc_cwnd_at > tp->snd_cwnd)
3657 tp->snd_cwnd = rack->r_ctl.rc_cwnd_at;
3658 if (rack->r_ctl.rc_ssthresh_at > tp->snd_ssthresh)
3659 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at;
3662 if (rsm->r_flags & RACK_WAS_SACKPASS) {
3664 * We retransmitted based on a sack and the earlier
3665 * retransmission ack'd it - re-ordering is occuring.
3667 counter_u64_add(rack_reorder_seen, 1);
3668 rack->r_ctl.rc_reorder_ts = cts;
3670 counter_u64_add(rack_badfr, 1);
3671 counter_u64_add(rack_badfr_bytes, (rsm->r_end - rsm->r_start));
3676 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack,
3677 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type)
3682 if (rsm->r_flags & RACK_ACKED)
3687 if ((rsm->r_rtr_cnt == 1) ||
3688 ((ack_type == CUM_ACKED) &&
3689 (to->to_flags & TOF_TS) &&
3691 (rsm->r_tim_lastsent[rsm->r_rtr_cnt - 1] == to->to_tsecr))
3694 * We will only find a matching timestamp if its cum-acked.
3695 * But if its only one retransmission its for-sure matching
3698 t = cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
3701 if (!tp->t_rttlow || tp->t_rttlow > t)
3703 if (!rack->r_ctl.rc_rack_min_rtt ||
3704 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
3705 rack->r_ctl.rc_rack_min_rtt = t;
3706 if (rack->r_ctl.rc_rack_min_rtt == 0) {
3707 rack->r_ctl.rc_rack_min_rtt = 1;
3710 tcp_rack_xmit_timer(rack, TCP_TS_TO_TICKS(t) + 1);
3711 if ((rsm->r_flags & RACK_TLP) &&
3712 (!IN_RECOVERY(tp->t_flags))) {
3713 /* Segment was a TLP and our retrans matched */
3714 if (rack->r_ctl.rc_tlp_cwnd_reduce) {
3715 rack->r_ctl.rc_rsm_start = tp->snd_max;
3716 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
3717 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
3718 rack_cong_signal(tp, NULL, CC_NDUPACK);
3720 * When we enter recovery we need to assure
3721 * we send one packet.
3723 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
3725 rack->r_ctl.rc_tlp_rtx_out = 0;
3727 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
3728 /* New more recent rack_tmit_time */
3729 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
3730 rack->rc_rack_rtt = t;
3735 * We clear the soft/rxtshift since we got an ack.
3736 * There is no assurance we will call the commit() function
3737 * so we need to clear these to avoid incorrect handling.
3740 tp->t_softerror = 0;
3741 if ((to->to_flags & TOF_TS) &&
3742 (ack_type == CUM_ACKED) &&
3744 ((rsm->r_flags & (RACK_DEFERRED | RACK_OVERMAX)) == 0)) {
3746 * Now which timestamp does it match? In this block the ACK
3747 * must be coming from a previous transmission.
3749 for (i = 0; i < rsm->r_rtr_cnt; i++) {
3750 if (rsm->r_tim_lastsent[i] == to->to_tsecr) {
3751 t = cts - rsm->r_tim_lastsent[i];
3754 if ((i + 1) < rsm->r_rtr_cnt) {
3756 rack_earlier_retran(tp, rsm, t, cts);
3758 if (!tp->t_rttlow || tp->t_rttlow > t)
3760 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
3761 rack->r_ctl.rc_rack_min_rtt = t;
3762 if (rack->r_ctl.rc_rack_min_rtt == 0) {
3763 rack->r_ctl.rc_rack_min_rtt = 1;
3767 * Note the following calls to
3768 * tcp_rack_xmit_timer() are being commented
3769 * out for now. They give us no more accuracy
3770 * and often lead to a wrong choice. We have
3771 * enough samples that have not been
3772 * retransmitted. I leave the commented out
3773 * code in here in case in the future we
3774 * decide to add it back (though I can't forsee
3775 * doing that). That way we will easily see
3776 * where they need to be placed.
3778 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time,
3779 rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) {
3780 /* New more recent rack_tmit_time */
3781 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)];
3782 rack->rc_rack_rtt = t;
3790 * Ok its a SACK block that we retransmitted. or a windows
3791 * machine without timestamps. We can tell nothing from the
3792 * time-stamp since its not there or the time the peer last
3793 * recieved a segment that moved forward its cum-ack point.
3796 i = rsm->r_rtr_cnt - 1;
3797 t = cts - rsm->r_tim_lastsent[i];
3800 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
3802 * We retransmitted and the ack came back in less
3803 * than the smallest rtt we have observed. We most
3804 * likey did an improper retransmit as outlined in
3805 * 4.2 Step 3 point 2 in the rack-draft.
3807 i = rsm->r_rtr_cnt - 2;
3808 t = cts - rsm->r_tim_lastsent[i];
3809 rack_earlier_retran(tp, rsm, t, cts);
3810 } else if (rack->r_ctl.rc_rack_min_rtt) {
3812 * We retransmitted it and the retransmit did the
3815 if (!rack->r_ctl.rc_rack_min_rtt ||
3816 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) {
3817 rack->r_ctl.rc_rack_min_rtt = t;
3818 if (rack->r_ctl.rc_rack_min_rtt == 0) {
3819 rack->r_ctl.rc_rack_min_rtt = 1;
3822 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[i])) {
3823 /* New more recent rack_tmit_time */
3824 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[i];
3825 rack->rc_rack_rtt = t;
3834 * Mark the SACK_PASSED flag on all entries prior to rsm send wise.
3837 rack_log_sack_passed(struct tcpcb *tp,
3838 struct tcp_rack *rack, struct rack_sendmap *rsm)
3840 struct rack_sendmap *nrsm;
3844 idx = rsm->r_rtr_cnt - 1;
3845 ts = rsm->r_tim_lastsent[idx];
3847 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap,
3848 rack_head, r_tnext) {
3850 /* Skip orginal segment he is acked */
3853 if (nrsm->r_flags & RACK_ACKED) {
3854 /* Skip ack'd segments */
3857 idx = nrsm->r_rtr_cnt - 1;
3858 if (ts == nrsm->r_tim_lastsent[idx]) {
3860 * For this case lets use seq no, if we sent in a
3861 * big block (TSO) we would have a bunch of segments
3862 * sent at the same time.
3864 * We would only get a report if its SEQ is earlier.
3865 * If we have done multiple retransmits the times
3866 * would not be equal.
3868 if (SEQ_LT(nrsm->r_start, rsm->r_start)) {
3869 nrsm->r_flags |= RACK_SACK_PASSED;
3870 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
3874 * Here they were sent at different times, not a big
3875 * block. Since we transmitted this one later and
3876 * see it sack'd then this must also be missing (or
3877 * we would have gotten a sack block for it)
3879 nrsm->r_flags |= RACK_SACK_PASSED;
3880 nrsm->r_flags &= ~RACK_WAS_SACKPASS;
3886 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack,
3887 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts)
3891 uint32_t start, end, changed = 0;
3892 struct rack_sendmap *rsm, *nrsm;
3893 int32_t used_ref = 1;
3895 start = sack->start;
3898 if (rsm && SEQ_LT(start, rsm->r_start)) {
3899 TAILQ_FOREACH_REVERSE_FROM(rsm, &rack->r_ctl.rc_map, rack_head, r_next) {
3900 if (SEQ_GEQ(start, rsm->r_start) &&
3901 SEQ_LT(start, rsm->r_end)) {
3911 /* First lets locate the block where this guy is */
3912 TAILQ_FOREACH_FROM(rsm, &rack->r_ctl.rc_map, r_next) {
3913 if (SEQ_GEQ(start, rsm->r_start) &&
3914 SEQ_LT(start, rsm->r_end)) {
3921 * This happens when we get duplicate sack blocks with the
3922 * same end. For example SACK 4: 100 SACK 3: 100 The sort
3923 * will not change there location so we would just start at
3924 * the end of the first one and get lost.
3926 if (tp->t_flags & TF_SENTFIN) {
3928 * Check to see if we have not logged the FIN that
3931 nrsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next);
3932 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) {
3934 * Ok we did not get the FIN logged.
3943 panic("tp:%p rack:%p sack:%p to:%p prsm:%p",
3944 tp, rack, sack, to, prsm);
3950 counter_u64_add(rack_sack_proc_restart, 1);
3951 goto start_at_beginning;
3953 /* Ok we have an ACK for some piece of rsm */
3954 if (rsm->r_start != start) {
3956 * Need to split this in two pieces the before and after.
3958 nrsm = rack_alloc(rack);
3961 * failed XXXrrs what can we do but loose the sack
3966 nrsm->r_start = start;
3967 nrsm->r_rtr_bytes = 0;
3968 nrsm->r_end = rsm->r_end;
3969 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
3970 nrsm->r_flags = rsm->r_flags;
3971 nrsm->r_sndcnt = rsm->r_sndcnt;
3972 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
3973 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
3975 rsm->r_end = nrsm->r_start;
3976 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next);
3977 if (rsm->r_in_tmap) {
3978 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
3979 nrsm->r_in_tmap = 1;
3981 rsm->r_flags &= (~RACK_HAS_FIN);
3984 if (SEQ_GEQ(end, rsm->r_end)) {
3986 * The end of this block is either beyond this guy or right
3990 if ((rsm->r_flags & RACK_ACKED) == 0) {
3991 rack_update_rtt(tp, rack, rsm, to, cts, SACKED);
3992 changed += (rsm->r_end - rsm->r_start);
3993 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
3994 rack_log_sack_passed(tp, rack, rsm);
3995 /* Is Reordering occuring? */
3996 if (rsm->r_flags & RACK_SACK_PASSED) {
3997 counter_u64_add(rack_reorder_seen, 1);
3998 rack->r_ctl.rc_reorder_ts = cts;
4000 rsm->r_flags |= RACK_ACKED;
4001 rsm->r_flags &= ~RACK_TLP;
4002 if (rsm->r_in_tmap) {
4003 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
4007 if (end == rsm->r_end) {
4008 /* This block only - done */
4011 /* There is more not coverend by this rsm move on */
4013 nrsm = TAILQ_NEXT(rsm, r_next);
4018 /* Ok we need to split off this one at the tail */
4019 nrsm = rack_alloc(rack);
4021 /* failed rrs what can we do but loose the sack info? */
4025 nrsm->r_start = end;
4026 nrsm->r_end = rsm->r_end;
4027 nrsm->r_rtr_bytes = 0;
4028 nrsm->r_rtr_cnt = rsm->r_rtr_cnt;
4029 nrsm->r_flags = rsm->r_flags;
4030 nrsm->r_sndcnt = rsm->r_sndcnt;
4031 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) {
4032 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx];
4034 /* The sack block does not cover this guy fully */
4035 rsm->r_flags &= (~RACK_HAS_FIN);
4037 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next);
4038 if (rsm->r_in_tmap) {
4039 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext);
4040 nrsm->r_in_tmap = 1;
4042 if (rsm->r_flags & RACK_ACKED) {
4043 /* Been here done that */
4046 rack_update_rtt(tp, rack, rsm, to, cts, SACKED);
4047 changed += (rsm->r_end - rsm->r_start);
4048 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start);
4049 rack_log_sack_passed(tp, rack, rsm);
4050 /* Is Reordering occuring? */
4051 if (rsm->r_flags & RACK_SACK_PASSED) {
4052 counter_u64_add(rack_reorder_seen, 1);
4053 rack->r_ctl.rc_reorder_ts = cts;
4055 rsm->r_flags |= RACK_ACKED;
4056 rsm->r_flags &= ~RACK_TLP;
4057 if (rsm->r_in_tmap) {
4058 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
4062 if (used_ref == 0) {
4063 counter_u64_add(rack_sack_proc_all, 1);
4065 counter_u64_add(rack_sack_proc_short, 1);
4067 /* Save off where we last were */
4069 rack->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next);
4071 rack->r_ctl.rc_sacklast = NULL;
4077 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack)
4079 struct rack_sendmap *tmap;
4082 while (rsm && (rsm->r_flags & RACK_ACKED)) {
4083 /* Its no longer sacked, mark it so */
4084 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
4086 if (rsm->r_in_tmap) {
4087 panic("rack:%p rsm:%p flags:0x%x in tmap?",
4088 rack, rsm, rsm->r_flags);
4091 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS);
4092 /* Rebuild it into our tmap */
4094 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext);
4097 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext);
4100 tmap->r_in_tmap = 1;
4101 rsm = TAILQ_NEXT(rsm, r_next);
4104 * Now lets possibly clear the sack filter so we start
4105 * recognizing sacks that cover this area.
4107 if (rack_use_sack_filter)
4108 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack);
4113 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th)
4115 uint32_t changed, last_seq, entered_recovery = 0;
4116 struct tcp_rack *rack;
4117 struct rack_sendmap *rsm;
4118 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1];
4119 register uint32_t th_ack;
4120 int32_t i, j, k, num_sack_blks = 0;
4121 uint32_t cts, acked, ack_point, sack_changed = 0;
4123 INP_WLOCK_ASSERT(tp->t_inpcb);
4124 if (th->th_flags & TH_RST) {
4125 /* We don't log resets */
4128 rack = (struct tcp_rack *)tp->t_fb_ptr;
4129 cts = tcp_ts_getticks();
4130 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map);
4132 th_ack = th->th_ack;
4134 if (SEQ_GT(th_ack, tp->snd_una)) {
4135 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__);
4136 tp->t_acktime = ticks;
4138 if (rsm && SEQ_GT(th_ack, rsm->r_start))
4139 changed = th_ack - rsm->r_start;
4142 * The ACK point is advancing to th_ack, we must drop off
4143 * the packets in the rack log and calculate any eligble
4146 rack->r_wanted_output++;
4148 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map);
4150 if ((th_ack - 1) == tp->iss) {
4152 * For the SYN incoming case we will not
4153 * have called tcp_output for the sending of
4154 * the SYN, so there will be no map. All
4155 * other cases should probably be a panic.
4159 if (tp->t_flags & TF_SENTFIN) {
4160 /* if we send a FIN we will not hav a map */
4164 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",
4166 th, tp->t_state, rack,
4167 tp->snd_una, tp->snd_max, tp->snd_nxt, changed);
4171 if (SEQ_LT(th_ack, rsm->r_start)) {
4172 /* Huh map is missing this */
4174 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n",
4176 th_ack, tp->t_state, rack->r_state);
4180 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED);
4181 /* Now do we consume the whole thing? */
4182 if (SEQ_GEQ(th_ack, rsm->r_end)) {
4183 /* Its all consumed. */
4186 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
4187 rsm->r_rtr_bytes = 0;
4188 TAILQ_REMOVE(&rack->r_ctl.rc_map, rsm, r_next);
4189 if (rsm->r_in_tmap) {
4190 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext);
4193 if (rack->r_ctl.rc_next == rsm) {
4194 /* scoot along the marker */
4195 rack->r_ctl.rc_next = TAILQ_FIRST(&rack->r_ctl.rc_map);
4197 if (rsm->r_flags & RACK_ACKED) {
4199 * It was acked on the scoreboard -- remove
4202 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start);
4203 } else if (rsm->r_flags & RACK_SACK_PASSED) {
4205 * There are acked segments ACKED on the
4206 * scoreboard further up. We are seeing
4209 counter_u64_add(rack_reorder_seen, 1);
4210 rsm->r_flags |= RACK_ACKED;
4211 rack->r_ctl.rc_reorder_ts = cts;
4213 left = th_ack - rsm->r_end;
4214 if (rsm->r_rtr_cnt > 1) {
4216 * Technically we should make r_rtr_cnt be
4217 * monotonicly increasing and just mod it to
4218 * the timestamp it is replacing.. that way
4219 * we would have the last 3 retransmits. Now
4220 * rc_loss_count will be wrong if we
4221 * retransmit something more than 2 times in
4224 rack->r_ctl.rc_loss_count += (rsm->r_rtr_cnt - 1);
4226 /* Free back to zone */
4227 rack_free(rack, rsm);
4233 if (rsm->r_flags & RACK_ACKED) {
4235 * It was acked on the scoreboard -- remove it from
4236 * total for the part being cum-acked.
4238 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start);
4240 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes;
4241 rsm->r_rtr_bytes = 0;
4242 rsm->r_start = th_ack;
4245 /* Check for reneging */
4246 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map);
4247 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) {
4249 * The peer has moved snd_una up to
4250 * the edge of this send, i.e. one
4251 * that it had previously acked. The only
4252 * way that can be true if the peer threw
4253 * away data (space issues) that it had
4254 * previously sacked (else it would have
4255 * given us snd_una up to (rsm->r_end).
4256 * We need to undo the acked markings here.
4258 * Note we have to look to make sure th_ack is
4259 * our rsm->r_start in case we get an old ack
4260 * where th_ack is behind snd_una.
4262 rack_peer_reneges(rack, rsm, th->th_ack);
4264 if ((to->to_flags & TOF_SACK) == 0) {
4265 /* We are done nothing left to log */
4268 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next);
4270 last_seq = rsm->r_end;
4272 last_seq = tp->snd_max;
4274 /* Sack block processing */
4275 if (SEQ_GT(th_ack, tp->snd_una))
4278 ack_point = tp->snd_una;
4279 for (i = 0; i < to->to_nsacks; i++) {
4280 bcopy((to->to_sacks + i * TCPOLEN_SACK),
4281 &sack, sizeof(sack));
4282 sack.start = ntohl(sack.start);
4283 sack.end = ntohl(sack.end);
4284 if (SEQ_GT(sack.end, sack.start) &&
4285 SEQ_GT(sack.start, ack_point) &&
4286 SEQ_LT(sack.start, tp->snd_max) &&
4287 SEQ_GT(sack.end, ack_point) &&
4288 SEQ_LEQ(sack.end, tp->snd_max)) {
4289 if ((rack->r_ctl.rc_num_maps_alloced > rack_sack_block_limit) &&
4290 (SEQ_LT(sack.end, last_seq)) &&
4291 ((sack.end - sack.start) < (tp->t_maxseg / 8))) {
4293 * Not the last piece and its smaller than
4294 * 1/8th of a MSS. We ignore this.
4296 counter_u64_add(rack_runt_sacks, 1);
4299 sack_blocks[num_sack_blks] = sack;
4301 #ifdef NETFLIX_STATS
4302 } else if (SEQ_LEQ(sack.start, th_ack) &&
4303 SEQ_LEQ(sack.end, th_ack)) {
4305 * Its a D-SACK block.
4307 tcp_record_dsack(sack.start, sack.end);
4312 if (num_sack_blks == 0)
4315 * Sort the SACK blocks so we can update the rack scoreboard with
4318 if (rack_use_sack_filter) {
4319 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks, num_sack_blks, th->th_ack);
4321 if (num_sack_blks < 2) {
4324 /* Sort the sacks */
4325 for (i = 0; i < num_sack_blks; i++) {
4326 for (j = i + 1; j < num_sack_blks; j++) {
4327 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
4328 sack = sack_blocks[i];
4329 sack_blocks[i] = sack_blocks[j];
4330 sack_blocks[j] = sack;
4335 * Now are any of the sack block ends the same (yes some
4336 * implememtations send these)?
4339 if (num_sack_blks > 1) {
4340 for (i = 0; i < num_sack_blks; i++) {
4341 for (j = i + 1; j < num_sack_blks; j++) {
4342 if (sack_blocks[i].end == sack_blocks[j].end) {
4344 * Ok these two have the same end we
4345 * want the smallest end and then
4346 * throw away the larger and start
4349 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) {
4351 * The second block covers
4352 * more area use that
4354 sack_blocks[i].start = sack_blocks[j].start;
4357 * Now collapse out the dup-sack and
4360 for (k = (j + 1); k < num_sack_blks; k++) {
4361 sack_blocks[j].start = sack_blocks[k].start;
4362 sack_blocks[j].end = sack_blocks[k].end;
4372 rsm = rack->r_ctl.rc_sacklast;
4373 for (i = 0; i < num_sack_blks; i++) {
4374 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts);
4376 rack->r_wanted_output++;
4378 sack_changed += acked;
4383 /* Something changed cancel the rack timer */
4384 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4386 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) {
4388 * Ok we have a high probability that we need to go in to
4389 * recovery since we have data sack'd
4391 struct rack_sendmap *rsm;
4394 tsused = tcp_ts_getticks();
4395 rsm = tcp_rack_output(tp, rack, tsused);
4397 /* Enter recovery */
4398 rack->r_ctl.rc_rsm_start = rsm->r_start;
4399 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
4400 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
4401 entered_recovery = 1;
4402 rack_cong_signal(tp, NULL, CC_NDUPACK);
4404 * When we enter recovery we need to assure we send
4407 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
4408 rack->r_timer_override = 1;
4411 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) {
4412 /* Deal with changed an PRR here (in recovery only) */
4413 uint32_t pipe, snd_una;
4415 rack->r_ctl.rc_prr_delivered += changed;
4416 /* Compute prr_sndcnt */
4417 if (SEQ_GT(tp->snd_una, th_ack)) {
4418 snd_una = tp->snd_una;
4422 pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt;
4423 if (pipe > tp->snd_ssthresh) {
4426 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh;
4427 if (rack->r_ctl.rc_prr_recovery_fs > 0)
4428 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs;
4430 rack->r_ctl.rc_prr_sndcnt = 0;
4434 if (sndcnt > (long)rack->r_ctl.rc_prr_out)
4435 sndcnt -= rack->r_ctl.rc_prr_out;
4438 rack->r_ctl.rc_prr_sndcnt = sndcnt;
4442 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out)
4443 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out);
4446 if (changed > limit)
4448 limit += tp->t_maxseg;
4449 if (tp->snd_ssthresh > pipe) {
4450 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit);
4452 rack->r_ctl.rc_prr_sndcnt = min(0, limit);
4455 if (rack->r_ctl.rc_prr_sndcnt >= tp->t_maxseg) {
4456 rack->r_timer_override = 1;
4462 * Return value of 1, we do not need to call rack_process_data().
4463 * return value of 0, rack_process_data can be called.
4464 * For ret_val if its 0 the TCP is locked, if its non-zero
4465 * its unlocked and probably unsafe to touch the TCB.
4468 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so,
4469 struct tcpcb *tp, struct tcpopt *to,
4470 uint32_t tiwin, int32_t tlen,
4471 int32_t * ofia, int32_t thflags, int32_t * ret_val)
4473 int32_t ourfinisacked = 0;
4474 int32_t nsegs, acked_amount;
4477 struct tcp_rack *rack;
4478 int32_t recovery = 0;
4480 rack = (struct tcp_rack *)tp->t_fb_ptr;
4481 if (SEQ_GT(th->th_ack, tp->snd_max)) {
4482 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
4485 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) {
4486 rack_log_ack(tp, to, th);
4488 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
4490 * Old ack, behind (or duplicate to) the last one rcv'd
4491 * Note: Should mark reordering is occuring! We should also
4492 * look for sack blocks arriving e.g. ack 1, 4-4 then ack 1,
4493 * 3-3, 4-4 would be reording. As well as ack 1, 3-3 <no
4499 * If we reach this point, ACK is not a duplicate, i.e., it ACKs
4500 * something we sent.
4502 if (tp->t_flags & TF_NEEDSYN) {
4504 * T/TCP: Connection was half-synchronized, and our SYN has
4505 * been ACK'd (so connection is now fully synchronized). Go
4506 * to non-starred state, increment snd_una for ACK of SYN,
4507 * and check if we can do window scaling.
4509 tp->t_flags &= ~TF_NEEDSYN;
4511 /* Do window scaling? */
4512 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
4513 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
4514 tp->rcv_scale = tp->request_r_scale;
4515 /* Send window already scaled. */
4518 nsegs = max(1, m->m_pkthdr.lro_nsegs);
4519 INP_WLOCK_ASSERT(tp->t_inpcb);
4521 acked = BYTES_THIS_ACK(tp, th);
4522 TCPSTAT_ADD(tcps_rcvackpack, nsegs);
4523 TCPSTAT_ADD(tcps_rcvackbyte, acked);
4526 * If we just performed our first retransmit, and the ACK arrives
4527 * within our recovery window, then it was a mistake to do the
4528 * retransmit in the first place. Recover our original cwnd and
4529 * ssthresh, and proceed to transmit where we left off.
4531 if (tp->t_flags & TF_PREVVALID) {
4532 tp->t_flags &= ~TF_PREVVALID;
4533 if (tp->t_rxtshift == 1 &&
4534 (int)(ticks - tp->t_badrxtwin) < 0)
4535 rack_cong_signal(tp, th, CC_RTO_ERR);
4538 * If we have a timestamp reply, update smoothed round trip time. If
4539 * no timestamp is present but transmit timer is running and timed
4540 * sequence number was acked, update smoothed round trip time. Since
4541 * we now have an rtt measurement, cancel the timer backoff (cf.,
4542 * Phil Karn's retransmit alg.). Recompute the initial retransmit
4545 * Some boxes send broken timestamp replies during the SYN+ACK
4546 * phase, ignore timestamps of 0 or we could calculate a huge RTT
4547 * and blow up the retransmit timer.
4550 * If all outstanding data is acked, stop retransmit timer and
4551 * remember to restart (more output or persist). If there is more
4552 * data to be acked, restart retransmit timer, using current
4553 * (possibly backed-off) value.
4555 if (th->th_ack == tp->snd_max) {
4556 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4557 rack->r_wanted_output++;
4560 * If no data (only SYN) was ACK'd, skip rest of ACK processing.
4564 *ofia = ourfinisacked;
4567 if (rack->r_ctl.rc_early_recovery) {
4568 if (IN_FASTRECOVERY(tp->t_flags)) {
4569 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
4570 tcp_rack_partialack(tp, th);
4572 rack_post_recovery(tp, th);
4578 * Let the congestion control algorithm update congestion control
4579 * related information. This typically means increasing the
4580 * congestion window.
4582 rack_ack_received(tp, rack, th, nsegs, CC_ACK, recovery);
4583 SOCKBUF_LOCK(&so->so_snd);
4584 acked_amount = min(acked, (int)sbavail(&so->so_snd));
4585 tp->snd_wnd -= acked_amount;
4586 mfree = sbcut_locked(&so->so_snd, acked_amount);
4587 if ((sbused(&so->so_snd) == 0) &&
4588 (acked > acked_amount) &&
4589 (tp->t_state >= TCPS_FIN_WAIT_1)) {
4592 /* NB: sowwakeup_locked() does an implicit unlock. */
4593 sowwakeup_locked(so);
4595 if (rack->r_ctl.rc_early_recovery == 0) {
4596 if (IN_FASTRECOVERY(tp->t_flags)) {
4597 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
4598 tcp_rack_partialack(tp, th);
4600 rack_post_recovery(tp, th);
4604 tp->snd_una = th->th_ack;
4605 if (SEQ_GT(tp->snd_una, tp->snd_recover))
4606 tp->snd_recover = tp->snd_una;
4608 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
4609 tp->snd_nxt = tp->snd_una;
4611 if (tp->snd_una == tp->snd_max) {
4612 /* Nothing left outstanding */
4613 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
4615 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4616 /* Set need output so persist might get set */
4617 rack->r_wanted_output++;
4618 if (rack_use_sack_filter)
4619 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
4620 if ((tp->t_state >= TCPS_FIN_WAIT_1) &&
4621 (sbavail(&so->so_snd) == 0) &&
4622 (tp->t_flags2 & TF2_DROP_AF_DATA)) {
4624 * The socket was gone and the
4625 * peer sent data, time to
4630 rack_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen);
4635 *ofia = ourfinisacked;
4641 * Return value of 1, the TCB is unlocked and most
4642 * likely gone, return value of 0, the TCP is still
4646 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so,
4647 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen,
4648 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
4651 * Update window information. Don't look at window if no ACK: TAC's
4652 * send garbage on first SYN.
4656 struct tcp_rack *rack;
4658 rack = (struct tcp_rack *)tp->t_fb_ptr;
4659 INP_WLOCK_ASSERT(tp->t_inpcb);
4660 nsegs = max(1, m->m_pkthdr.lro_nsegs);
4661 if ((thflags & TH_ACK) &&
4662 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
4663 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
4664 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
4665 /* keep track of pure window updates */
4667 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
4668 TCPSTAT_INC(tcps_rcvwinupd);
4669 tp->snd_wnd = tiwin;
4670 tp->snd_wl1 = th->th_seq;
4671 tp->snd_wl2 = th->th_ack;
4672 if (tp->snd_wnd > tp->max_sndwnd)
4673 tp->max_sndwnd = tp->snd_wnd;
4674 rack->r_wanted_output++;
4675 } else if (thflags & TH_ACK) {
4676 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) {
4677 tp->snd_wnd = tiwin;
4678 tp->snd_wl1 = th->th_seq;
4679 tp->snd_wl2 = th->th_ack;
4682 /* Was persist timer active and now we have window space? */
4683 if ((rack->rc_in_persist != 0) && tp->snd_wnd) {
4684 rack_exit_persist(tp, rack);
4685 tp->snd_nxt = tp->snd_max;
4686 /* Make sure we output to start the timer */
4687 rack->r_wanted_output++;
4689 if (tp->t_flags2 & TF2_DROP_AF_DATA) {
4694 * Process segments with URG.
4696 if ((thflags & TH_URG) && th->th_urp &&
4697 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
4699 * This is a kludge, but if we receive and accept random
4700 * urgent pointers, we'll crash in soreceive. It's hard to
4701 * imagine someone actually wanting to send this much urgent
4704 SOCKBUF_LOCK(&so->so_rcv);
4705 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
4706 th->th_urp = 0; /* XXX */
4707 thflags &= ~TH_URG; /* XXX */
4708 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */
4709 goto dodata; /* XXX */
4712 * If this segment advances the known urgent pointer, then
4713 * mark the data stream. This should not happen in
4714 * CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since a
4715 * FIN has been received from the remote side. In these
4716 * states we ignore the URG.
4718 * According to RFC961 (Assigned Protocols), the urgent
4719 * pointer points to the last octet of urgent data. We
4720 * continue, however, to consider it to indicate the first
4721 * octet of data past the urgent section as the original
4722 * spec states (in one of two places).
4724 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) {
4725 tp->rcv_up = th->th_seq + th->th_urp;
4726 so->so_oobmark = sbavail(&so->so_rcv) +
4727 (tp->rcv_up - tp->rcv_nxt) - 1;
4728 if (so->so_oobmark == 0)
4729 so->so_rcv.sb_state |= SBS_RCVATMARK;
4731 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
4733 SOCKBUF_UNLOCK(&so->so_rcv);
4735 * Remove out of band data so doesn't get presented to user.
4736 * This can happen independent of advancing the URG pointer,
4737 * but if two URG's are pending at once, some out-of-band
4738 * data may creep in... ick.
4740 if (th->th_urp <= (uint32_t) tlen &&
4741 !(so->so_options & SO_OOBINLINE)) {
4742 /* hdr drop is delayed */
4743 tcp_pulloutofband(so, th, m, drop_hdrlen);
4747 * If no out of band data is expected, pull receive urgent
4748 * pointer along with the receive window.
4750 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
4751 tp->rcv_up = tp->rcv_nxt;
4754 INP_WLOCK_ASSERT(tp->t_inpcb);
4757 * Process the segment text, merging it into the TCP sequencing
4758 * queue, and arranging for acknowledgment of receipt if necessary.
4759 * This process logically involves adjusting tp->rcv_wnd as data is
4760 * presented to the user (this happens in tcp_usrreq.c, case
4761 * PRU_RCVD). If a FIN has already been received on this connection
4762 * then we just ignore the text.
4764 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
4765 IS_FASTOPEN(tp->t_flags));
4766 if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
4767 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
4768 tcp_seq save_start = th->th_seq;
4770 m_adj(m, drop_hdrlen); /* delayed header drop */
4772 * Insert segment which includes th into TCP reassembly
4773 * queue with control block tp. Set thflags to whether
4774 * reassembly now includes a segment with FIN. This handles
4775 * the common case inline (segment is the next to be
4776 * received on an established connection, and the queue is
4777 * empty), avoiding linkage into and removal from the queue
4778 * and repetition of various conversions. Set DELACK for
4779 * segments received in order, but ack immediately when
4780 * segments are out of order (so fast retransmit can work).
4782 if (th->th_seq == tp->rcv_nxt &&
4784 (TCPS_HAVEESTABLISHED(tp->t_state) ||
4786 if (DELAY_ACK(tp, tlen) || tfo_syn) {
4787 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4788 tp->t_flags |= TF_DELACK;
4790 rack->r_wanted_output++;
4791 tp->t_flags |= TF_ACKNOW;
4793 tp->rcv_nxt += tlen;
4794 thflags = th->th_flags & TH_FIN;
4795 TCPSTAT_ADD(tcps_rcvpack, nsegs);
4796 TCPSTAT_ADD(tcps_rcvbyte, tlen);
4797 SOCKBUF_LOCK(&so->so_rcv);
4798 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
4801 sbappendstream_locked(&so->so_rcv, m, 0);
4802 /* NB: sorwakeup_locked() does an implicit unlock. */
4803 sorwakeup_locked(so);
4806 * XXX: Due to the header drop above "th" is
4807 * theoretically invalid by now. Fortunately
4808 * m_adj() doesn't actually frees any mbufs when
4809 * trimming from the head.
4811 thflags = tcp_reass(tp, th, &save_start, &tlen, m);
4812 tp->t_flags |= TF_ACKNOW;
4815 tcp_update_sack_list(tp, save_start, save_start + tlen);
4822 * If FIN is received ACK the FIN and let the user know that the
4823 * connection is closing.
4825 if (thflags & TH_FIN) {
4826 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
4829 * If connection is half-synchronized (ie NEEDSYN
4830 * flag on) then delay ACK, so it may be piggybacked
4831 * when SYN is sent. Otherwise, since we received a
4832 * FIN then no more input can be expected, send ACK
4835 if (tp->t_flags & TF_NEEDSYN) {
4836 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4837 tp->t_flags |= TF_DELACK;
4839 tp->t_flags |= TF_ACKNOW;
4843 switch (tp->t_state) {
4846 * In SYN_RECEIVED and ESTABLISHED STATES enter the
4849 case TCPS_SYN_RECEIVED:
4850 tp->t_starttime = ticks;
4852 case TCPS_ESTABLISHED:
4853 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4854 tcp_state_change(tp, TCPS_CLOSE_WAIT);
4858 * If still in FIN_WAIT_1 STATE FIN has not been
4859 * acked so enter the CLOSING state.
4861 case TCPS_FIN_WAIT_1:
4862 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4863 tcp_state_change(tp, TCPS_CLOSING);
4867 * In FIN_WAIT_2 state enter the TIME_WAIT state,
4868 * starting the time-wait timer, turning off the
4869 * other standard timers.
4871 case TCPS_FIN_WAIT_2:
4872 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4873 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
4879 * Return any desired output.
4881 if ((tp->t_flags & TF_ACKNOW) || (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) {
4882 rack->r_wanted_output++;
4884 INP_WLOCK_ASSERT(tp->t_inpcb);
4889 * Here nothing is really faster, its just that we
4890 * have broken out the fast-data path also just like
4894 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so,
4895 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
4896 uint32_t tiwin, int32_t nxt_pkt)
4899 int32_t newsize = 0; /* automatic sockbuf scaling */
4900 struct tcp_rack *rack;
4903 * The size of tcp_saveipgen must be the size of the max ip header,
4906 u_char tcp_saveipgen[IP6_HDR_LEN];
4907 struct tcphdr tcp_savetcp;
4912 * If last ACK falls within this segment's sequence numbers, record
4913 * the timestamp. NOTE that the test is modified according to the
4914 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
4916 if (__predict_false(th->th_seq != tp->rcv_nxt)) {
4919 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
4922 if (tiwin && tiwin != tp->snd_wnd) {
4925 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) {
4928 if (__predict_false((to->to_flags & TOF_TS) &&
4929 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) {
4932 if (__predict_false((th->th_ack != tp->snd_una))) {
4935 if (__predict_false(tlen > sbspace(&so->so_rcv))) {
4938 if ((to->to_flags & TOF_TS) != 0 &&
4939 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
4940 tp->ts_recent_age = tcp_ts_getticks();
4941 tp->ts_recent = to->to_tsval;
4943 rack = (struct tcp_rack *)tp->t_fb_ptr;
4945 * This is a pure, in-sequence data packet with nothing on the
4946 * reassembly queue and we have enough buffer space to take it.
4948 nsegs = max(1, m->m_pkthdr.lro_nsegs);
4951 /* Clean receiver SACK report if present */
4952 if (tp->rcv_numsacks)
4953 tcp_clean_sackreport(tp);
4954 TCPSTAT_INC(tcps_preddat);
4955 tp->rcv_nxt += tlen;
4957 * Pull snd_wl1 up to prevent seq wrap relative to th_seq.
4959 tp->snd_wl1 = th->th_seq;
4961 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt.
4963 tp->rcv_up = tp->rcv_nxt;
4964 TCPSTAT_ADD(tcps_rcvpack, nsegs);
4965 TCPSTAT_ADD(tcps_rcvbyte, tlen);
4967 if (so->so_options & SO_DEBUG)
4968 tcp_trace(TA_INPUT, ostate, tp,
4969 (void *)tcp_saveipgen, &tcp_savetcp, 0);
4971 newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
4973 /* Add data to socket buffer. */
4974 SOCKBUF_LOCK(&so->so_rcv);
4975 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
4979 * Set new socket buffer size. Give up when limit is
4983 if (!sbreserve_locked(&so->so_rcv,
4985 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
4986 m_adj(m, drop_hdrlen); /* delayed header drop */
4987 sbappendstream_locked(&so->so_rcv, m, 0);
4988 rack_calc_rwin(so, tp);
4990 /* NB: sorwakeup_locked() does an implicit unlock. */
4991 sorwakeup_locked(so);
4992 if (DELAY_ACK(tp, tlen)) {
4993 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
4994 tp->t_flags |= TF_DELACK;
4996 tp->t_flags |= TF_ACKNOW;
4997 rack->r_wanted_output++;
4999 if ((tp->snd_una == tp->snd_max) && rack_use_sack_filter)
5000 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una);
5005 * This subfunction is used to try to highly optimize the
5006 * fast path. We again allow window updates that are
5007 * in sequence to remain in the fast-path. We also add
5008 * in the __predict's to attempt to help the compiler.
5009 * Note that if we return a 0, then we can *not* process
5010 * it and the caller should push the packet into the
5014 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
5015 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5016 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts)
5023 * The size of tcp_saveipgen must be the size of the max ip header,
5026 u_char tcp_saveipgen[IP6_HDR_LEN];
5027 struct tcphdr tcp_savetcp;
5031 struct tcp_rack *rack;
5033 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) {
5034 /* Old ack, behind (or duplicate to) the last one rcv'd */
5037 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) {
5038 /* Above what we have sent? */
5041 if (__predict_false(tp->snd_nxt != tp->snd_max)) {
5042 /* We are retransmitting */
5045 if (__predict_false(tiwin == 0)) {
5049 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) {
5050 /* We need a SYN or a FIN, unlikely.. */
5053 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) {
5054 /* Timestamp is behind .. old ack with seq wrap? */
5057 if (__predict_false(IN_RECOVERY(tp->t_flags))) {
5058 /* Still recovering */
5061 rack = (struct tcp_rack *)tp->t_fb_ptr;
5062 if (rack->r_ctl.rc_sacked) {
5063 /* We have sack holes on our scoreboard */
5066 /* Ok if we reach here, we can process a fast-ack */
5067 nsegs = max(1, m->m_pkthdr.lro_nsegs);
5068 rack_log_ack(tp, to, th);
5069 /* Did the window get updated? */
5070 if (tiwin != tp->snd_wnd) {
5071 tp->snd_wnd = tiwin;
5072 tp->snd_wl1 = th->th_seq;
5073 if (tp->snd_wnd > tp->max_sndwnd)
5074 tp->max_sndwnd = tp->snd_wnd;
5076 if ((rack->rc_in_persist != 0) && (tp->snd_wnd >= tp->t_maxseg)) {
5077 rack_exit_persist(tp, rack);
5080 * If last ACK falls within this segment's sequence numbers, record
5081 * the timestamp. NOTE that the test is modified according to the
5082 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26).
5084 if ((to->to_flags & TOF_TS) != 0 &&
5085 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
5086 tp->ts_recent_age = tcp_ts_getticks();
5087 tp->ts_recent = to->to_tsval;
5090 * This is a pure ack for outstanding data.
5092 TCPSTAT_INC(tcps_predack);
5095 * "bad retransmit" recovery.
5097 if (tp->t_flags & TF_PREVVALID) {
5098 tp->t_flags &= ~TF_PREVVALID;
5099 if (tp->t_rxtshift == 1 &&
5100 (int)(ticks - tp->t_badrxtwin) < 0)
5101 rack_cong_signal(tp, th, CC_RTO_ERR);
5104 * Recalculate the transmit timer / rtt.
5106 * Some boxes send broken timestamp replies during the SYN+ACK
5107 * phase, ignore timestamps of 0 or we could calculate a huge RTT
5108 * and blow up the retransmit timer.
5110 acked = BYTES_THIS_ACK(tp, th);
5113 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
5114 hhook_run_tcp_est_in(tp, th, to);
5117 TCPSTAT_ADD(tcps_rcvackpack, nsegs);
5118 TCPSTAT_ADD(tcps_rcvackbyte, acked);
5119 sbdrop(&so->so_snd, acked);
5121 * Let the congestion control algorithm update congestion control
5122 * related information. This typically means increasing the
5123 * congestion window.
5125 rack_ack_received(tp, rack, th, nsegs, CC_ACK, 0);
5127 tp->snd_una = th->th_ack;
5129 * Pull snd_wl2 up to prevent seq wrap relative to th_ack.
5131 tp->snd_wl2 = th->th_ack;
5134 /* ND6_HINT(tp); *//* Some progress has been made. */
5137 * If all outstanding data are acked, stop retransmit timer,
5138 * otherwise restart timer using current (possibly backed-off)
5139 * value. If process is waiting for space, wakeup/selwakeup/signal.
5140 * If data are ready to send, let tcp_output decide between more
5141 * output or persist.
5144 if (so->so_options & SO_DEBUG)
5145 tcp_trace(TA_INPUT, ostate, tp,
5146 (void *)tcp_saveipgen,
5149 if (tp->snd_una == tp->snd_max) {
5150 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__);
5152 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
5154 /* Wake up the socket if we have room to write more */
5156 if (sbavail(&so->so_snd)) {
5157 rack->r_wanted_output++;
5163 * Return value of 1, the TCB is unlocked and most
5164 * likely gone, return value of 0, the TCP is still
5168 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so,
5169 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5170 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5172 int32_t ret_val = 0;
5174 int32_t ourfinisacked = 0;
5176 rack_calc_rwin(so, tp);
5178 * If the state is SYN_SENT: if seg contains an ACK, but not for our
5179 * SYN, drop the input. if seg contains a RST, then drop the
5180 * connection. if seg does not contain SYN, then drop it. Otherwise
5181 * this is an acceptable SYN segment initialize tp->rcv_nxt and
5182 * tp->irs if seg contains ack then advance tp->snd_una if seg
5183 * contains an ECE and ECN support is enabled, the stream is ECN
5184 * capable. if SYN has been acked change to ESTABLISHED else
5185 * SYN_RCVD state arrange for segment to be acked (eventually)
5186 * continue processing rest of data/controls, beginning with URG
5188 if ((thflags & TH_ACK) &&
5189 (SEQ_LEQ(th->th_ack, tp->iss) ||
5190 SEQ_GT(th->th_ack, tp->snd_max))) {
5191 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5194 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) {
5195 TCP_PROBE5(connect__refused, NULL, tp,
5196 mtod(m, const char *), tp, th);
5197 tp = tcp_drop(tp, ECONNREFUSED);
5198 rack_do_drop(m, tp);
5201 if (thflags & TH_RST) {
5202 rack_do_drop(m, tp);
5205 if (!(thflags & TH_SYN)) {
5206 rack_do_drop(m, tp);
5209 tp->irs = th->th_seq;
5211 if (thflags & TH_ACK) {
5212 int tfo_partial = 0;
5214 TCPSTAT_INC(tcps_connects);
5217 mac_socketpeer_set_from_mbuf(m, so);
5219 /* Do window scaling on this connection? */
5220 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
5221 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
5222 tp->rcv_scale = tp->request_r_scale;
5224 tp->rcv_adv += min(tp->rcv_wnd,
5225 TCP_MAXWIN << tp->rcv_scale);
5227 * If not all the data that was sent in the TFO SYN
5228 * has been acked, resend the remainder right away.
5230 if (IS_FASTOPEN(tp->t_flags) &&
5231 (tp->snd_una != tp->snd_max)) {
5232 tp->snd_nxt = th->th_ack;
5236 * If there's data, delay ACK; if there's also a FIN ACKNOW
5237 * will be turned on later.
5239 if (DELAY_ACK(tp, tlen) && tlen != 0 && (tfo_partial == 0)) {
5240 rack_timer_cancel(tp, (struct tcp_rack *)tp->t_fb_ptr,
5241 ((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rcvtime, __LINE__);
5242 tp->t_flags |= TF_DELACK;
5244 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output++;
5245 tp->t_flags |= TF_ACKNOW;
5248 if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
5250 tp->t_flags |= TF_ECN_PERMIT;
5251 TCPSTAT_INC(tcps_ecn_shs);
5253 if (SEQ_GT(th->th_ack, tp->snd_una)) {
5255 * We advance snd_una for the
5256 * fast open case. If th_ack is
5257 * acknowledging data beyond
5258 * snd_una we can't just call
5259 * ack-processing since the
5260 * data stream in our send-map
5261 * will start at snd_una + 1 (one
5262 * beyond the SYN). If its just
5263 * equal we don't need to do that
5264 * and there is no send_map.
5269 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions:
5270 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1
5272 tp->t_starttime = ticks;
5273 if (tp->t_flags & TF_NEEDFIN) {
5274 tcp_state_change(tp, TCPS_FIN_WAIT_1);
5275 tp->t_flags &= ~TF_NEEDFIN;
5278 tcp_state_change(tp, TCPS_ESTABLISHED);
5279 TCP_PROBE5(connect__established, NULL, tp,
5280 mtod(m, const char *), tp, th);
5285 * Received initial SYN in SYN-SENT[*] state => simultaneous
5286 * open. If segment contains CC option and there is a
5287 * cached CC, apply TAO test. If it succeeds, connection is *
5288 * half-synchronized. Otherwise, do 3-way handshake:
5289 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If
5290 * there was no CC option, clear cached CC value.
5292 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
5293 tcp_state_change(tp, TCPS_SYN_RECEIVED);
5295 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
5296 INP_WLOCK_ASSERT(tp->t_inpcb);
5298 * Advance th->th_seq to correspond to first data byte. If data,
5299 * trim to stay within window, dropping FIN if necessary.
5302 if (tlen > tp->rcv_wnd) {
5303 todrop = tlen - tp->rcv_wnd;
5307 TCPSTAT_INC(tcps_rcvpackafterwin);
5308 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
5310 tp->snd_wl1 = th->th_seq - 1;
5311 tp->rcv_up = th->th_seq;
5313 * Client side of transaction: already sent SYN and data. If the
5314 * remote host used T/TCP to validate the SYN, our data will be
5315 * ACK'd; if so, enter normal data segment processing in the middle
5316 * of step 5, ack processing. Otherwise, goto step 6.
5318 if (thflags & TH_ACK) {
5319 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val))
5321 /* We may have changed to FIN_WAIT_1 above */
5322 if (tp->t_state == TCPS_FIN_WAIT_1) {
5324 * In FIN_WAIT_1 STATE in addition to the processing
5325 * for the ESTABLISHED state if our FIN is now
5326 * acknowledged then enter FIN_WAIT_2.
5328 if (ourfinisacked) {
5330 * If we can't receive any more data, then
5331 * closing user can proceed. Starting the
5332 * timer is contrary to the specification,
5333 * but if we don't get a FIN we'll hang
5336 * XXXjl: we should release the tp also, and
5337 * use a compressed state.
5339 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5340 soisdisconnected(so);
5341 tcp_timer_activate(tp, TT_2MSL,
5342 (tcp_fast_finwait2_recycle ?
5343 tcp_finwait2_timeout :
5346 tcp_state_change(tp, TCPS_FIN_WAIT_2);
5350 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5351 tiwin, thflags, nxt_pkt));
5355 * Return value of 1, the TCB is unlocked and most
5356 * likely gone, return value of 0, the TCP is still
5360 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so,
5361 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5362 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5364 int32_t ret_val = 0;
5365 int32_t ourfinisacked = 0;
5367 rack_calc_rwin(so, tp);
5369 if ((thflags & TH_ACK) &&
5370 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
5371 SEQ_GT(th->th_ack, tp->snd_max))) {
5372 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5375 if (IS_FASTOPEN(tp->t_flags)) {
5377 * When a TFO connection is in SYN_RECEIVED, the
5378 * only valid packets are the initial SYN, a
5379 * retransmit/copy of the initial SYN (possibly with
5380 * a subset of the original data), a valid ACK, a
5383 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) {
5384 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5386 } else if (thflags & TH_SYN) {
5387 /* non-initial SYN is ignored */
5388 struct tcp_rack *rack;
5390 rack = (struct tcp_rack *)tp->t_fb_ptr;
5391 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) ||
5392 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) ||
5393 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) {
5394 rack_do_drop(m, NULL);
5397 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) {
5398 rack_do_drop(m, NULL);
5402 if (thflags & TH_RST)
5403 return (rack_process_rst(m, th, so, tp));
5405 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
5406 * it's less than ts_recent, drop it.
5408 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
5409 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
5410 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
5414 * In the SYN-RECEIVED state, validate that the packet belongs to
5415 * this connection before trimming the data to fit the receive
5416 * window. Check the sequence number versus IRS since we know the
5417 * sequence numbers haven't wrapped. This is a partial fix for the
5418 * "LAND" DoS attack.
5420 if (SEQ_LT(th->th_seq, tp->irs)) {
5421 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5424 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
5428 * If last ACK falls within this segment's sequence numbers, record
5429 * its timestamp. NOTE: 1) That the test incorporates suggestions
5430 * from the latest proposal of the tcplw@cray.com list (Braden
5431 * 1993/04/26). 2) That updating only on newer timestamps interferes
5432 * with our earlier PAWS tests, so this check should be solely
5433 * predicated on the sequence space of this segment. 3) That we
5434 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
5435 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
5436 * SEG.Len, This modified check allows us to overcome RFC1323's
5437 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
5438 * p.869. In such cases, we can still calculate the RTT correctly
5439 * when RCV.NXT == Last.ACK.Sent.
5441 if ((to->to_flags & TOF_TS) != 0 &&
5442 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
5443 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
5444 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
5445 tp->ts_recent_age = tcp_ts_getticks();
5446 tp->ts_recent = to->to_tsval;
5449 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
5450 * is on (half-synchronized state), then queue data for later
5451 * processing; else drop segment and return.
5453 if ((thflags & TH_ACK) == 0) {
5454 if (IS_FASTOPEN(tp->t_flags)) {
5455 tp->snd_wnd = tiwin;
5458 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5459 tiwin, thflags, nxt_pkt));
5461 TCPSTAT_INC(tcps_connects);
5463 /* Do window scaling? */
5464 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
5465 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
5466 tp->rcv_scale = tp->request_r_scale;
5467 tp->snd_wnd = tiwin;
5470 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* ->
5473 tp->t_starttime = ticks;
5474 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
5475 tcp_fastopen_decrement_counter(tp->t_tfo_pending);
5476 tp->t_tfo_pending = NULL;
5479 * Account for the ACK of our SYN prior to
5480 * regular ACK processing below.
5484 if (tp->t_flags & TF_NEEDFIN) {
5485 tcp_state_change(tp, TCPS_FIN_WAIT_1);
5486 tp->t_flags &= ~TF_NEEDFIN;
5488 tcp_state_change(tp, TCPS_ESTABLISHED);
5489 TCP_PROBE5(accept__established, NULL, tp,
5490 mtod(m, const char *), tp, th);
5492 * TFO connections call cc_conn_init() during SYN
5493 * processing. Calling it again here for such connections
5494 * is not harmless as it would undo the snd_cwnd reduction
5495 * that occurs when a TFO SYN|ACK is retransmitted.
5497 if (!IS_FASTOPEN(tp->t_flags))
5501 * If segment contains data or ACK, will call tcp_reass() later; if
5502 * not, do so now to pass queued data to user.
5504 if (tlen == 0 && (thflags & TH_FIN) == 0)
5505 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
5507 tp->snd_wl1 = th->th_seq - 1;
5508 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
5511 if (tp->t_state == TCPS_FIN_WAIT_1) {
5512 /* We could have went to FIN_WAIT_1 (or EST) above */
5514 * In FIN_WAIT_1 STATE in addition to the processing for the
5515 * ESTABLISHED state if our FIN is now acknowledged then
5518 if (ourfinisacked) {
5520 * If we can't receive any more data, then closing
5521 * user can proceed. Starting the timer is contrary
5522 * to the specification, but if we don't get a FIN
5523 * we'll hang forever.
5525 * XXXjl: we should release the tp also, and use a
5528 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5529 soisdisconnected(so);
5530 tcp_timer_activate(tp, TT_2MSL,
5531 (tcp_fast_finwait2_recycle ?
5532 tcp_finwait2_timeout :
5535 tcp_state_change(tp, TCPS_FIN_WAIT_2);
5538 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5539 tiwin, thflags, nxt_pkt));
5543 * Return value of 1, the TCB is unlocked and most
5544 * likely gone, return value of 0, the TCP is still
5548 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so,
5549 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5550 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5552 int32_t ret_val = 0;
5555 * Header prediction: check for the two common cases of a
5556 * uni-directional data xfer. If the packet has no control flags,
5557 * is in-sequence, the window didn't change and we're not
5558 * retransmitting, it's a candidate. If the length is zero and the
5559 * ack moved forward, we're the sender side of the xfer. Just free
5560 * the data acked & wake any higher level process that was blocked
5561 * waiting for space. If the length is non-zero and the ack didn't
5562 * move, we're the receiver side. If we're getting packets in-order
5563 * (the reassembly queue is empty), add the data toc The socket
5564 * buffer and note that we need a delayed ack. Make sure that the
5565 * hidden state-flags are also off. Since we check for
5566 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN.
5568 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) &&
5569 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) &&
5570 __predict_true(SEGQ_EMPTY(tp)) &&
5571 __predict_true(th->th_seq == tp->rcv_nxt)) {
5572 struct tcp_rack *rack;
5574 rack = (struct tcp_rack *)tp->t_fb_ptr;
5576 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen,
5577 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) {
5581 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen,
5587 rack_calc_rwin(so, tp);
5589 if (thflags & TH_RST)
5590 return (rack_process_rst(m, th, so, tp));
5593 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
5594 * synchronized state.
5596 if (thflags & TH_SYN) {
5597 rack_challenge_ack(m, th, tp, &ret_val);
5601 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
5602 * it's less than ts_recent, drop it.
5604 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
5605 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
5606 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
5609 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
5613 * If last ACK falls within this segment's sequence numbers, record
5614 * its timestamp. NOTE: 1) That the test incorporates suggestions
5615 * from the latest proposal of the tcplw@cray.com list (Braden
5616 * 1993/04/26). 2) That updating only on newer timestamps interferes
5617 * with our earlier PAWS tests, so this check should be solely
5618 * predicated on the sequence space of this segment. 3) That we
5619 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
5620 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
5621 * SEG.Len, This modified check allows us to overcome RFC1323's
5622 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
5623 * p.869. In such cases, we can still calculate the RTT correctly
5624 * when RCV.NXT == Last.ACK.Sent.
5626 if ((to->to_flags & TOF_TS) != 0 &&
5627 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
5628 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
5629 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
5630 tp->ts_recent_age = tcp_ts_getticks();
5631 tp->ts_recent = to->to_tsval;
5634 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
5635 * is on (half-synchronized state), then queue data for later
5636 * processing; else drop segment and return.
5638 if ((thflags & TH_ACK) == 0) {
5639 if (tp->t_flags & TF_NEEDSYN) {
5641 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5642 tiwin, thflags, nxt_pkt));
5644 } else if (tp->t_flags & TF_ACKNOW) {
5645 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
5648 rack_do_drop(m, NULL);
5655 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
5658 if (sbavail(&so->so_snd)) {
5659 if (rack_progress_timeout_check(tp)) {
5660 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
5661 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5665 /* State changes only happen in rack_process_data() */
5666 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5667 tiwin, thflags, nxt_pkt));
5671 * Return value of 1, the TCB is unlocked and most
5672 * likely gone, return value of 0, the TCP is still
5676 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so,
5677 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5678 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5680 int32_t ret_val = 0;
5682 rack_calc_rwin(so, tp);
5683 if (thflags & TH_RST)
5684 return (rack_process_rst(m, th, so, tp));
5686 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
5687 * synchronized state.
5689 if (thflags & TH_SYN) {
5690 rack_challenge_ack(m, th, tp, &ret_val);
5694 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
5695 * it's less than ts_recent, drop it.
5697 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
5698 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
5699 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
5702 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
5706 * If last ACK falls within this segment's sequence numbers, record
5707 * its timestamp. NOTE: 1) That the test incorporates suggestions
5708 * from the latest proposal of the tcplw@cray.com list (Braden
5709 * 1993/04/26). 2) That updating only on newer timestamps interferes
5710 * with our earlier PAWS tests, so this check should be solely
5711 * predicated on the sequence space of this segment. 3) That we
5712 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
5713 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
5714 * SEG.Len, This modified check allows us to overcome RFC1323's
5715 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
5716 * p.869. In such cases, we can still calculate the RTT correctly
5717 * when RCV.NXT == Last.ACK.Sent.
5719 if ((to->to_flags & TOF_TS) != 0 &&
5720 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
5721 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
5722 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
5723 tp->ts_recent_age = tcp_ts_getticks();
5724 tp->ts_recent = to->to_tsval;
5727 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
5728 * is on (half-synchronized state), then queue data for later
5729 * processing; else drop segment and return.
5731 if ((thflags & TH_ACK) == 0) {
5732 if (tp->t_flags & TF_NEEDSYN) {
5733 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5734 tiwin, thflags, nxt_pkt));
5736 } else if (tp->t_flags & TF_ACKNOW) {
5737 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
5740 rack_do_drop(m, NULL);
5747 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) {
5750 if (sbavail(&so->so_snd)) {
5751 if (rack_progress_timeout_check(tp)) {
5752 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
5753 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5757 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5758 tiwin, thflags, nxt_pkt));
5762 rack_check_data_after_close(struct mbuf *m,
5763 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so)
5765 struct tcp_rack *rack;
5767 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
5768 rack = (struct tcp_rack *)tp->t_fb_ptr;
5769 if (rack->rc_allow_data_af_clo == 0) {
5772 TCPSTAT_INC(tcps_rcvafterclose);
5773 rack_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen));
5776 if (sbavail(&so->so_snd) == 0)
5778 /* Ok we allow data that is ignored and a followup reset */
5779 tp->rcv_nxt = th->th_seq + *tlen;
5780 tp->t_flags2 |= TF2_DROP_AF_DATA;
5781 rack->r_wanted_output = 1;
5787 * Return value of 1, the TCB is unlocked and most
5788 * likely gone, return value of 0, the TCP is still
5792 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so,
5793 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5794 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5796 int32_t ret_val = 0;
5797 int32_t ourfinisacked = 0;
5799 rack_calc_rwin(so, tp);
5801 if (thflags & TH_RST)
5802 return (rack_process_rst(m, th, so, tp));
5804 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
5805 * synchronized state.
5807 if (thflags & TH_SYN) {
5808 rack_challenge_ack(m, th, tp, &ret_val);
5812 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
5813 * it's less than ts_recent, drop it.
5815 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
5816 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
5817 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
5820 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
5824 * If new data are received on a connection after the user processes
5825 * are gone, then RST the other end.
5827 if ((so->so_state & SS_NOFDREF) && tlen) {
5828 if (rack_check_data_after_close(m, tp, &tlen, th, so))
5832 * If last ACK falls within this segment's sequence numbers, record
5833 * its timestamp. NOTE: 1) That the test incorporates suggestions
5834 * from the latest proposal of the tcplw@cray.com list (Braden
5835 * 1993/04/26). 2) That updating only on newer timestamps interferes
5836 * with our earlier PAWS tests, so this check should be solely
5837 * predicated on the sequence space of this segment. 3) That we
5838 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
5839 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
5840 * SEG.Len, This modified check allows us to overcome RFC1323's
5841 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
5842 * p.869. In such cases, we can still calculate the RTT correctly
5843 * when RCV.NXT == Last.ACK.Sent.
5845 if ((to->to_flags & TOF_TS) != 0 &&
5846 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
5847 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
5848 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
5849 tp->ts_recent_age = tcp_ts_getticks();
5850 tp->ts_recent = to->to_tsval;
5853 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
5854 * is on (half-synchronized state), then queue data for later
5855 * processing; else drop segment and return.
5857 if ((thflags & TH_ACK) == 0) {
5858 if (tp->t_flags & TF_NEEDSYN) {
5859 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5860 tiwin, thflags, nxt_pkt));
5861 } else if (tp->t_flags & TF_ACKNOW) {
5862 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
5865 rack_do_drop(m, NULL);
5872 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
5875 if (ourfinisacked) {
5877 * If we can't receive any more data, then closing user can
5878 * proceed. Starting the timer is contrary to the
5879 * specification, but if we don't get a FIN we'll hang
5882 * XXXjl: we should release the tp also, and use a
5885 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5886 soisdisconnected(so);
5887 tcp_timer_activate(tp, TT_2MSL,
5888 (tcp_fast_finwait2_recycle ?
5889 tcp_finwait2_timeout :
5892 tcp_state_change(tp, TCPS_FIN_WAIT_2);
5894 if (sbavail(&so->so_snd)) {
5895 if (rack_progress_timeout_check(tp)) {
5896 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
5897 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
5901 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5902 tiwin, thflags, nxt_pkt));
5906 * Return value of 1, the TCB is unlocked and most
5907 * likely gone, return value of 0, the TCP is still
5911 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so,
5912 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
5913 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
5915 int32_t ret_val = 0;
5916 int32_t ourfinisacked = 0;
5918 rack_calc_rwin(so, tp);
5920 if (thflags & TH_RST)
5921 return (rack_process_rst(m, th, so, tp));
5923 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
5924 * synchronized state.
5926 if (thflags & TH_SYN) {
5927 rack_challenge_ack(m, th, tp, &ret_val);
5931 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
5932 * it's less than ts_recent, drop it.
5934 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
5935 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
5936 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
5939 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
5943 * If new data are received on a connection after the user processes
5944 * are gone, then RST the other end.
5946 if ((so->so_state & SS_NOFDREF) && tlen) {
5947 if (rack_check_data_after_close(m, tp, &tlen, th, so))
5951 * If last ACK falls within this segment's sequence numbers, record
5952 * its timestamp. NOTE: 1) That the test incorporates suggestions
5953 * from the latest proposal of the tcplw@cray.com list (Braden
5954 * 1993/04/26). 2) That updating only on newer timestamps interferes
5955 * with our earlier PAWS tests, so this check should be solely
5956 * predicated on the sequence space of this segment. 3) That we
5957 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
5958 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
5959 * SEG.Len, This modified check allows us to overcome RFC1323's
5960 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
5961 * p.869. In such cases, we can still calculate the RTT correctly
5962 * when RCV.NXT == Last.ACK.Sent.
5964 if ((to->to_flags & TOF_TS) != 0 &&
5965 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
5966 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
5967 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
5968 tp->ts_recent_age = tcp_ts_getticks();
5969 tp->ts_recent = to->to_tsval;
5972 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
5973 * is on (half-synchronized state), then queue data for later
5974 * processing; else drop segment and return.
5976 if ((thflags & TH_ACK) == 0) {
5977 if (tp->t_flags & TF_NEEDSYN) {
5978 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
5979 tiwin, thflags, nxt_pkt));
5980 } else if (tp->t_flags & TF_ACKNOW) {
5981 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
5984 rack_do_drop(m, NULL);
5991 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
5994 if (ourfinisacked) {
5995 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
6000 if (sbavail(&so->so_snd)) {
6001 if (rack_progress_timeout_check(tp)) {
6002 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
6003 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
6007 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
6008 tiwin, thflags, nxt_pkt));
6012 * Return value of 1, the TCB is unlocked and most
6013 * likely gone, return value of 0, the TCP is still
6017 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so,
6018 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
6019 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
6021 int32_t ret_val = 0;
6022 int32_t ourfinisacked = 0;
6024 rack_calc_rwin(so, tp);
6026 if (thflags & TH_RST)
6027 return (rack_process_rst(m, th, so, tp));
6029 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
6030 * synchronized state.
6032 if (thflags & TH_SYN) {
6033 rack_challenge_ack(m, th, tp, &ret_val);
6037 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
6038 * it's less than ts_recent, drop it.
6040 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
6041 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
6042 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
6045 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
6049 * If new data are received on a connection after the user processes
6050 * are gone, then RST the other end.
6052 if ((so->so_state & SS_NOFDREF) && tlen) {
6053 if (rack_check_data_after_close(m, tp, &tlen, th, so))
6057 * If last ACK falls within this segment's sequence numbers, record
6058 * its timestamp. NOTE: 1) That the test incorporates suggestions
6059 * from the latest proposal of the tcplw@cray.com list (Braden
6060 * 1993/04/26). 2) That updating only on newer timestamps interferes
6061 * with our earlier PAWS tests, so this check should be solely
6062 * predicated on the sequence space of this segment. 3) That we
6063 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
6064 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
6065 * SEG.Len, This modified check allows us to overcome RFC1323's
6066 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
6067 * p.869. In such cases, we can still calculate the RTT correctly
6068 * when RCV.NXT == Last.ACK.Sent.
6070 if ((to->to_flags & TOF_TS) != 0 &&
6071 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
6072 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
6073 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
6074 tp->ts_recent_age = tcp_ts_getticks();
6075 tp->ts_recent = to->to_tsval;
6078 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
6079 * is on (half-synchronized state), then queue data for later
6080 * processing; else drop segment and return.
6082 if ((thflags & TH_ACK) == 0) {
6083 if (tp->t_flags & TF_NEEDSYN) {
6084 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
6085 tiwin, thflags, nxt_pkt));
6086 } else if (tp->t_flags & TF_ACKNOW) {
6087 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
6090 rack_do_drop(m, NULL);
6095 * case TCPS_LAST_ACK: Ack processing.
6097 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
6100 if (ourfinisacked) {
6101 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
6103 rack_do_drop(m, tp);
6106 if (sbavail(&so->so_snd)) {
6107 if (rack_progress_timeout_check(tp)) {
6108 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
6109 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
6113 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
6114 tiwin, thflags, nxt_pkt));
6119 * Return value of 1, the TCB is unlocked and most
6120 * likely gone, return value of 0, the TCP is still
6124 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so,
6125 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen,
6126 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt)
6128 int32_t ret_val = 0;
6129 int32_t ourfinisacked = 0;
6131 rack_calc_rwin(so, tp);
6133 /* Reset receive buffer auto scaling when not in bulk receive mode. */
6134 if (thflags & TH_RST)
6135 return (rack_process_rst(m, th, so, tp));
6137 * RFC5961 Section 4.2 Send challenge ACK for any SYN in
6138 * synchronized state.
6140 if (thflags & TH_SYN) {
6141 rack_challenge_ack(m, th, tp, &ret_val);
6145 * RFC 1323 PAWS: If we have a timestamp reply on this segment and
6146 * it's less than ts_recent, drop it.
6148 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent &&
6149 TSTMP_LT(to->to_tsval, tp->ts_recent)) {
6150 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val))
6153 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) {
6157 * If new data are received on a connection after the user processes
6158 * are gone, then RST the other end.
6160 if ((so->so_state & SS_NOFDREF) &&
6162 if (rack_check_data_after_close(m, tp, &tlen, th, so))
6166 * If last ACK falls within this segment's sequence numbers, record
6167 * its timestamp. NOTE: 1) That the test incorporates suggestions
6168 * from the latest proposal of the tcplw@cray.com list (Braden
6169 * 1993/04/26). 2) That updating only on newer timestamps interferes
6170 * with our earlier PAWS tests, so this check should be solely
6171 * predicated on the sequence space of this segment. 3) That we
6172 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ
6173 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ +
6174 * SEG.Len, This modified check allows us to overcome RFC1323's
6175 * limitations as described in Stevens TCP/IP Illustrated Vol. 2
6176 * p.869. In such cases, we can still calculate the RTT correctly
6177 * when RCV.NXT == Last.ACK.Sent.
6179 if ((to->to_flags & TOF_TS) != 0 &&
6180 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
6181 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
6182 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
6183 tp->ts_recent_age = tcp_ts_getticks();
6184 tp->ts_recent = to->to_tsval;
6187 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag
6188 * is on (half-synchronized state), then queue data for later
6189 * processing; else drop segment and return.
6191 if ((thflags & TH_ACK) == 0) {
6192 if (tp->t_flags & TF_NEEDSYN) {
6193 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
6194 tiwin, thflags, nxt_pkt));
6195 } else if (tp->t_flags & TF_ACKNOW) {
6196 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val);
6199 rack_do_drop(m, NULL);
6206 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) {
6209 if (sbavail(&so->so_snd)) {
6210 if (rack_progress_timeout_check(tp)) {
6211 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
6212 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
6216 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen,
6217 tiwin, thflags, nxt_pkt));
6222 rack_clear_rate_sample(struct tcp_rack *rack)
6224 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY;
6225 rack->r_ctl.rack_rs.rs_rtt_cnt = 0;
6226 rack->r_ctl.rack_rs.rs_rtt_tot = 0;
6230 rack_init(struct tcpcb *tp)
6232 struct tcp_rack *rack = NULL;
6234 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT);
6235 if (tp->t_fb_ptr == NULL) {
6237 * We need to allocate memory but cant. The INP and INP_INFO
6238 * locks and they are recusive (happens during setup. So a
6239 * scheme to drop the locks fails :(
6244 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack));
6246 rack = (struct tcp_rack *)tp->t_fb_ptr;
6247 TAILQ_INIT(&rack->r_ctl.rc_map);
6248 TAILQ_INIT(&rack->r_ctl.rc_free);
6249 TAILQ_INIT(&rack->r_ctl.rc_tmap);
6252 rack->rc_inp = tp->t_inpcb;
6254 /* Probably not needed but lets be sure */
6255 rack_clear_rate_sample(rack);
6257 rack->r_ctl.rc_reorder_fade = rack_reorder_fade;
6258 rack->rc_allow_data_af_clo = rack_ignore_data_after_close;
6259 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh;
6260 rack->rc_pace_reduce = rack_slot_reduction;
6261 if (V_tcp_delack_enabled)
6262 tp->t_delayed_ack = 1;
6264 tp->t_delayed_ack = 0;
6265 rack->rc_pace_max_segs = rack_hptsi_segments;
6266 rack->r_ctl.rc_early_recovery_segs = rack_early_recovery_max_seg;
6267 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh;
6268 rack->r_ctl.rc_pkt_delay = rack_pkt_delay;
6269 rack->r_ctl.rc_prop_reduce = rack_use_proportional_reduce;
6270 rack->r_idle_reduce_largest = rack_reduce_largest_on_idle;
6271 rack->r_enforce_min_pace = rack_min_pace_time;
6272 rack->r_min_pace_seg_thresh = rack_min_pace_time_seg_req;
6273 rack->r_ctl.rc_prop_rate = rack_proportional_rate;
6274 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp;
6275 rack->r_ctl.rc_early_recovery = rack_early_recovery;
6276 rack->rc_always_pace = rack_pace_every_seg;
6277 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method;
6278 rack->rack_tlp_threshold_use = rack_tlp_threshold_use;
6279 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr;
6280 rack->r_ctl.rc_min_to = rack_min_to;
6281 rack->r_ctl.rc_prr_inc_var = rack_inc_var;
6282 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0);
6283 if (tp->snd_una != tp->snd_max) {
6284 /* Create a send map for the current outstanding data */
6285 struct rack_sendmap *rsm;
6287 rsm = rack_alloc(rack);
6289 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
6290 tp->t_fb_ptr = NULL;
6293 rsm->r_flags = RACK_OVERMAX;
6294 rsm->r_tim_lastsent[0] = tcp_ts_getticks();
6296 rsm->r_rtr_bytes = 0;
6297 rsm->r_start = tp->snd_una;
6298 rsm->r_end = tp->snd_max;
6300 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_map, rsm, r_next);
6301 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext);
6308 rack_handoff_ok(struct tcpcb *tp)
6310 if ((tp->t_state == TCPS_CLOSED) ||
6311 (tp->t_state == TCPS_LISTEN)) {
6312 /* Sure no problem though it may not stick */
6315 if ((tp->t_state == TCPS_SYN_SENT) ||
6316 (tp->t_state == TCPS_SYN_RECEIVED)) {
6318 * We really don't know you have to get to ESTAB or beyond
6323 if (tp->t_flags & TF_SACK_PERMIT) {
6327 * If we reach here we don't do SACK on this connection so we can
6334 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged)
6337 struct tcp_rack *rack;
6338 struct rack_sendmap *rsm;
6340 rack = (struct tcp_rack *)tp->t_fb_ptr;
6342 tcp_log_flowend(tp);
6344 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map);
6346 TAILQ_REMOVE(&rack->r_ctl.rc_map, rsm, r_next);
6347 uma_zfree(rack_zone, rsm);
6348 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map);
6350 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
6352 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_next);
6353 uma_zfree(rack_zone, rsm);
6354 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free);
6356 rack->rc_free_cnt = 0;
6357 uma_zfree(rack_pcb_zone, tp->t_fb_ptr);
6358 tp->t_fb_ptr = NULL;
6363 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack)
6365 switch (tp->t_state) {
6367 rack->r_state = TCPS_SYN_SENT;
6368 rack->r_substate = rack_do_syn_sent;
6370 case TCPS_SYN_RECEIVED:
6371 rack->r_state = TCPS_SYN_RECEIVED;
6372 rack->r_substate = rack_do_syn_recv;
6374 case TCPS_ESTABLISHED:
6375 rack->r_state = TCPS_ESTABLISHED;
6376 rack->r_substate = rack_do_established;
6378 case TCPS_CLOSE_WAIT:
6379 rack->r_state = TCPS_CLOSE_WAIT;
6380 rack->r_substate = rack_do_close_wait;
6382 case TCPS_FIN_WAIT_1:
6383 rack->r_state = TCPS_FIN_WAIT_1;
6384 rack->r_substate = rack_do_fin_wait_1;
6387 rack->r_state = TCPS_CLOSING;
6388 rack->r_substate = rack_do_closing;
6391 rack->r_state = TCPS_LAST_ACK;
6392 rack->r_substate = rack_do_lastack;
6394 case TCPS_FIN_WAIT_2:
6395 rack->r_state = TCPS_FIN_WAIT_2;
6396 rack->r_substate = rack_do_fin_wait_2;
6400 case TCPS_TIME_WAIT:
6403 panic("tcp tp:%p state:%d sees impossible state?", tp, tp->t_state);
6411 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb)
6414 * We received an ack, and then did not
6415 * call send or were bounced out due to the
6416 * hpts was running. Now a timer is up as well, is
6417 * it the right timer?
6419 struct rack_sendmap *rsm;
6422 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK;
6423 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT))
6425 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6426 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) &&
6427 (tmr_up == PACE_TMR_RXT)) {
6428 /* Should be an RXT */
6432 /* Nothing outstanding? */
6433 if (tp->t_flags & TF_DELACK) {
6434 if (tmr_up == PACE_TMR_DELACK)
6435 /* We are supposed to have delayed ack up and we do */
6437 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) {
6439 * if we hit enobufs then we would expect the possiblity
6440 * of nothing outstanding and the RXT up (and the hptsi timer).
6443 } else if (((tcp_always_keepalive ||
6444 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
6445 (tp->t_state <= TCPS_CLOSING)) &&
6446 (tmr_up == PACE_TMR_KEEP) &&
6447 (tp->snd_max == tp->snd_una)) {
6448 /* We should have keep alive up and we do */
6452 if (rsm && (rsm->r_flags & RACK_SACK_PASSED)) {
6453 if ((tp->t_flags & TF_SENTFIN) &&
6454 ((tp->snd_max - tp->snd_una) == 1) &&
6455 (rsm->r_flags & RACK_HAS_FIN)) {
6456 /* needs to be a RXT */
6457 if (tmr_up == PACE_TMR_RXT)
6459 } else if (tmr_up == PACE_TMR_RACK)
6461 } else if (SEQ_GT(tp->snd_max,tp->snd_una) &&
6462 ((tmr_up == PACE_TMR_TLP) ||
6463 (tmr_up == PACE_TMR_RXT))) {
6465 * Either a TLP or RXT is fine if no sack-passed
6466 * is in place and data is outstanding.
6469 } else if (tmr_up == PACE_TMR_DELACK) {
6471 * If the delayed ack was going to go off
6472 * before the rtx/tlp/rack timer were going to
6473 * expire, then that would be the timer in control.
6474 * Note we don't check the time here trusting the
6480 * Ok the timer originally started is not what we want now.
6481 * We will force the hpts to be stopped if any, and restart
6482 * with the slot set to what was in the saved slot.
6484 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__);
6485 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0);
6489 rack_hpts_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
6490 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos,
6491 int32_t nxt_pkt, struct timeval *tv)
6493 int32_t thflags, retval, did_out = 0;
6494 int32_t way_out = 0;
6498 struct tcp_rack *rack;
6499 struct rack_sendmap *rsm;
6500 int32_t prev_state = 0;
6502 cts = tcp_tv_to_mssectick(tv);
6503 rack = (struct tcp_rack *)tp->t_fb_ptr;
6505 kern_prefetch(rack, &prev_state);
6507 thflags = th->th_flags;
6509 * If this is either a state-changing packet or current state isn't
6510 * established, we require a read lock on tcbinfo. Otherwise, we
6511 * allow the tcbinfo to be in either locked or unlocked, as the
6512 * caller may have unnecessarily acquired a lock due to a race.
6514 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
6515 tp->t_state != TCPS_ESTABLISHED) {
6516 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
6518 INP_WLOCK_ASSERT(tp->t_inpcb);
6519 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
6521 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
6524 union tcp_log_stackspecific log;
6526 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
6527 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
6528 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
6529 TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
6532 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
6534 goto done_with_input;
6537 * If a segment with the ACK-bit set arrives in the SYN-SENT state
6538 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9.
6540 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
6541 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
6542 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
6546 * Segment received on connection. Reset idle time and keep-alive
6547 * timer. XXX: This should be done after segment validation to
6548 * ignore broken/spoofed segs.
6550 if (tp->t_idle_reduce && (tp->snd_max == tp->snd_una)) {
6552 if ((tp->cwv_enabled) &&
6553 ((tp->cwv_cwnd_valid == 0) &&
6554 TCPS_HAVEESTABLISHED(tp->t_state) &&
6555 (tp->snd_cwnd > tp->snd_cwv.init_cwnd))) {
6556 tcp_newcwv_nvp_closedown(tp);
6559 if ((ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
6560 counter_u64_add(rack_input_idle_reduces, 1);
6561 rack_cc_after_idle(tp,
6562 (rack->r_idle_reduce_largest ? 1 :0));
6565 rack->r_ctl.rc_rcvtime = cts;
6566 tp->t_rcvtime = ticks;
6569 if (tp->cwv_enabled) {
6570 if ((tp->cwv_cwnd_valid == 0) &&
6571 TCPS_HAVEESTABLISHED(tp->t_state) &&
6572 (tp->snd_cwnd > tp->snd_cwv.init_cwnd))
6573 tcp_newcwv_nvp_closedown(tp);
6577 * Unscale the window into a 32-bit value. For the SYN_SENT state
6578 * the scale is zero.
6580 tiwin = th->th_win << tp->snd_scale;
6581 #ifdef NETFLIX_STATS
6582 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
6585 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move
6586 * this to occur after we've validated the segment.
6588 if (tp->t_flags & TF_ECN_PERMIT) {
6589 if (thflags & TH_CWR)
6590 tp->t_flags &= ~TF_ECN_SND_ECE;
6591 switch (iptos & IPTOS_ECN_MASK) {
6593 tp->t_flags |= TF_ECN_SND_ECE;
6594 TCPSTAT_INC(tcps_ecn_ce);
6596 case IPTOS_ECN_ECT0:
6597 TCPSTAT_INC(tcps_ecn_ect0);
6599 case IPTOS_ECN_ECT1:
6600 TCPSTAT_INC(tcps_ecn_ect1);
6603 /* Congestion experienced. */
6604 if (thflags & TH_ECE) {
6605 rack_cong_signal(tp, th, CC_ECN);
6609 * Parse options on any incoming segment.
6611 tcp_dooptions(&to, (u_char *)(th + 1),
6612 (th->th_off << 2) - sizeof(struct tcphdr),
6613 (thflags & TH_SYN) ? TO_SYN : 0);
6616 * If echoed timestamp is later than the current time, fall back to
6617 * non RFC1323 RTT calculation. Normalize timestamp if syncookies
6618 * were used when this connection was established.
6620 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
6621 to.to_tsecr -= tp->ts_offset;
6622 if (TSTMP_GT(to.to_tsecr, cts))
6626 * If its the first time in we need to take care of options and
6627 * verify we can do SACK for rack!
6629 if (rack->r_state == 0) {
6630 /* Should be init'd by rack_init() */
6631 KASSERT(rack->rc_inp != NULL,
6632 ("%s: rack->rc_inp unexpectedly NULL", __func__));
6633 if (rack->rc_inp == NULL) {
6634 rack->rc_inp = tp->t_inpcb;
6638 * Process options only when we get SYN/ACK back. The SYN
6639 * case for incoming connections is handled in tcp_syncache.
6640 * According to RFC1323 the window field in a SYN (i.e., a
6641 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX
6642 * this is traditional behavior, may need to be cleaned up.
6644 rack->r_cpu = inp_to_cpuid(tp->t_inpcb);
6645 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
6646 if ((to.to_flags & TOF_SCALE) &&
6647 (tp->t_flags & TF_REQ_SCALE)) {
6648 tp->t_flags |= TF_RCVD_SCALE;
6649 tp->snd_scale = to.to_wscale;
6652 * Initial send window. It will be updated with the
6653 * next incoming segment to the scaled value.
6655 tp->snd_wnd = th->th_win;
6656 if (to.to_flags & TOF_TS) {
6657 tp->t_flags |= TF_RCVD_TSTMP;
6658 tp->ts_recent = to.to_tsval;
6659 tp->ts_recent_age = cts;
6661 if (to.to_flags & TOF_MSS)
6662 tcp_mss(tp, to.to_mss);
6663 if ((tp->t_flags & TF_SACK_PERMIT) &&
6664 (to.to_flags & TOF_SACKPERM) == 0)
6665 tp->t_flags &= ~TF_SACK_PERMIT;
6666 if (IS_FASTOPEN(tp->t_flags)) {
6667 if (to.to_flags & TOF_FASTOPEN) {
6670 if (to.to_flags & TOF_MSS)
6673 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
6677 tcp_fastopen_update_cache(tp, mss,
6678 to.to_tfo_len, to.to_tfo_cookie);
6680 tcp_fastopen_disable_path(tp);
6684 * At this point we are at the initial call. Here we decide
6685 * if we are doing RACK or not. We do this by seeing if
6686 * TF_SACK_PERMIT is set, if not rack is *not* possible and
6687 * we switch to the default code.
6689 if ((tp->t_flags & TF_SACK_PERMIT) == 0) {
6690 tcp_switch_back_to_default(tp);
6691 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen,
6696 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
6697 tcp_set_hpts(tp->t_inpcb);
6698 rack_stop_all_timers(tp);
6699 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack);
6702 * This is the one exception case where we set the rack state
6703 * always. All other times (timers etc) we must have a rack-state
6704 * set (so we assure we have done the checks above for SACK).
6706 if (rack->r_state != tp->t_state)
6707 rack_set_state(tp, rack);
6708 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&rack->r_ctl.rc_map)) != NULL)
6709 kern_prefetch(rsm, &prev_state);
6710 prev_state = rack->r_state;
6711 rack->r_ctl.rc_tlp_send_cnt = 0;
6712 rack_clear_rate_sample(rack);
6713 retval = (*rack->r_substate) (m, th, so,
6714 tp, &to, drop_hdrlen,
6715 tlen, tiwin, thflags, nxt_pkt);
6717 if ((retval == 0) &&
6718 (tp->t_inpcb == NULL)) {
6719 panic("retval:%d tp:%p t_inpcb:NULL state:%d",
6720 retval, tp, prev_state);
6725 * If retval is 1 the tcb is unlocked and most likely the tp
6728 INP_WLOCK_ASSERT(tp->t_inpcb);
6729 tcp_rack_xmit_timer_commit(rack, tp);
6730 if (((tp->snd_max - tp->snd_una) > tp->snd_wnd) &&
6731 (rack->rc_in_persist == 0)){
6733 * The peer shrunk its window on us to the point
6734 * where we have sent too much. The only thing
6735 * we can do here is stop any timers and
6736 * enter persist. We most likely lost the last
6737 * bytes we sent but oh well, we will have to
6738 * retransmit them after the peer is caught up.
6740 if (rack->rc_inp->inp_in_hpts)
6741 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
6742 rack_timer_cancel(tp, rack, cts, __LINE__);
6743 rack_enter_persist(tp, rack, cts);
6744 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0);
6746 goto done_with_input;
6749 if (rack->r_wanted_output != 0) {
6751 (void)tp->t_fb->tfb_tcp_output(tp);
6753 rack_start_hpts_timer(rack, tp, cts, __LINE__, 0, 0, 0);
6755 if (((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) &&
6756 (SEQ_GT(tp->snd_max, tp->snd_una) ||
6757 (tp->t_flags & TF_DELACK) ||
6758 ((tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) &&
6759 (tp->t_state <= TCPS_CLOSING)))) {
6760 /* We could not send (probably in the hpts but stopped the timer earlier)? */
6761 if ((tp->snd_max == tp->snd_una) &&
6762 ((tp->t_flags & TF_DELACK) == 0) &&
6763 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) {
6764 /* keep alive not needed if we are hptsi output yet */
6767 if (rack->rc_inp->inp_in_hpts)
6768 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT);
6769 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0);
6773 /* Do we have the correct timer running? */
6774 rack_timer_audit(tp, rack, &so->so_snd);
6778 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out);
6780 rack->r_wanted_output = 0;
6782 if (tp->t_inpcb == NULL) {
6783 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d",
6785 retval, tp, prev_state);
6788 INP_WUNLOCK(tp->t_inpcb);
6793 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
6794 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos)
6798 struct tcp_function_block *tfb;
6799 struct tcp_rack *rack;
6800 struct epoch_tracker et;
6802 rack = (struct tcp_rack *)tp->t_fb_ptr;
6803 if (rack->r_state == 0) {
6805 * Initial input (ACK to SYN-ACK etc)lets go ahead and get
6808 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
6810 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen,
6811 tlen, iptos, 0, &tv);
6812 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
6815 tcp_queue_to_input(tp, m, th, tlen, drop_hdrlen, iptos);
6816 INP_WUNLOCK(tp->t_inpcb);
6819 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen,
6820 tlen, iptos, 0, &tv);
6824 struct rack_sendmap *
6825 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused)
6827 struct rack_sendmap *rsm = NULL;
6829 uint32_t srtt_cur, srtt = 0, thresh = 0, ts_low = 0;
6831 /* Return the next guy to be re-transmitted */
6832 if (TAILQ_EMPTY(&rack->r_ctl.rc_map)) {
6835 if (tp->t_flags & TF_SENTFIN) {
6836 /* retran the end FIN? */
6839 /* ok lets look at this one */
6840 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap);
6841 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) {
6844 rsm = rack_find_lowest_rsm(rack);
6849 srtt_cur = tp->t_srtt >> TCP_RTT_SHIFT;
6850 srtt = TICKS_2_MSEC(srtt_cur);
6851 if (rack->rc_rack_rtt && (srtt > rack->rc_rack_rtt))
6852 srtt = rack->rc_rack_rtt;
6853 if (rsm->r_flags & RACK_ACKED) {
6856 if ((rsm->r_flags & RACK_SACK_PASSED) == 0) {
6857 /* Its not yet ready */
6860 idx = rsm->r_rtr_cnt - 1;
6861 ts_low = rsm->r_tim_lastsent[idx];
6862 thresh = rack_calc_thresh_rack(rack, srtt, tsused);
6863 if (tsused <= ts_low) {
6866 if ((tsused - ts_low) >= thresh) {
6873 rack_output(struct tcpcb *tp)
6876 uint32_t recwin, sendwin;
6878 int32_t len, flags, error = 0;
6881 uint32_t if_hw_tsomaxsegcount = 0;
6882 uint32_t if_hw_tsomaxsegsize;
6883 long tot_len_this_send = 0;
6884 struct ip *ip = NULL;
6886 struct ipovly *ipov = NULL;
6888 struct udphdr *udp = NULL;
6889 struct tcp_rack *rack;
6892 uint8_t wanted_cookie = 0;
6893 u_char opt[TCP_MAXOLEN];
6894 unsigned ipoptlen, optlen, hdrlen, ulen=0;
6897 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
6898 unsigned ipsec_optlen = 0;
6901 int32_t idle, sendalot;
6902 int32_t sub_from_prr = 0;
6903 volatile int32_t sack_rxmit;
6904 struct rack_sendmap *rsm = NULL;
6905 int32_t tso, mtu, would_have_fin = 0;
6909 uint8_t hpts_calling, doing_tlp = 0;
6910 int32_t do_a_prefetch;
6911 int32_t prefetch_rsm = 0;
6912 int32_t prefetch_so_done = 0;
6913 struct tcp_log_buffer *lgb = NULL;
6917 struct ip6_hdr *ip6 = NULL;
6920 /* setup and take the cache hits here */
6921 rack = (struct tcp_rack *)tp->t_fb_ptr;
6923 so = inp->inp_socket;
6925 kern_prefetch(sb, &do_a_prefetch);
6928 INP_WLOCK_ASSERT(inp);
6930 if (tp->t_flags & TF_TOE)
6931 return (tcp_offload_output(tp));
6934 if (rack->r_state) {
6935 /* Use the cache line loaded if possible */
6936 isipv6 = rack->r_is_v6;
6938 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
6941 cts = tcp_ts_getticks();
6942 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) &&
6945 * We are on the hpts for some timer but not hptsi output.
6946 * Remove from the hpts unconditionally.
6948 rack_timer_cancel(tp, rack, cts, __LINE__);
6950 /* Mark that we have called rack_output(). */
6951 if ((rack->r_timer_override) ||
6952 (tp->t_flags & TF_FORCEDATA) ||
6953 (tp->t_state < TCPS_ESTABLISHED)) {
6954 if (tp->t_inpcb->inp_in_hpts)
6955 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT);
6956 } else if (tp->t_inpcb->inp_in_hpts) {
6958 * On the hpts you can't pass even if ACKNOW is on, we will
6959 * when the hpts fires.
6961 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1);
6964 hpts_calling = inp->inp_hpts_calls;
6965 inp->inp_hpts_calls = 0;
6966 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) {
6967 if (rack_process_timers(tp, rack, cts, hpts_calling)) {
6968 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1);
6972 rack->r_wanted_output = 0;
6973 rack->r_timer_override = 0;
6975 * For TFO connections in SYN_SENT or SYN_RECEIVED,
6976 * only allow the initial SYN or SYN|ACK and those sent
6977 * by the retransmit timer.
6979 if (IS_FASTOPEN(tp->t_flags) &&
6980 ((tp->t_state == TCPS_SYN_RECEIVED) ||
6981 (tp->t_state == TCPS_SYN_SENT)) &&
6982 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */
6983 (tp->t_rxtshift == 0)) /* not a retransmit */
6986 * Determine length of data that should be transmitted, and flags
6987 * that will be used. If there is some data or critical controls
6988 * (SYN, RST) to send, then transmit; otherwise, investigate
6991 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
6993 if (tp->cwv_enabled) {
6994 if ((tp->cwv_cwnd_valid == 0) &&
6995 TCPS_HAVEESTABLISHED(tp->t_state) &&
6996 (tp->snd_cwnd > tp->snd_cwv.init_cwnd))
6997 tcp_newcwv_nvp_closedown(tp);
7000 if (tp->t_idle_reduce) {
7001 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
7002 rack_cc_after_idle(tp,
7003 (rack->r_idle_reduce_largest ? 1 :0));
7005 tp->t_flags &= ~TF_LASTIDLE;
7007 if (tp->t_flags & TF_MORETOCOME) {
7008 tp->t_flags |= TF_LASTIDLE;
7014 * If we've recently taken a timeout, snd_max will be greater than
7015 * snd_nxt. There may be SACK information that allows us to avoid
7016 * resending already delivered data. Adjust snd_nxt accordingly.
7019 cts = tcp_ts_getticks();
7022 sb_offset = tp->snd_max - tp->snd_una;
7023 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
7025 flags = tcp_outflags[tp->t_state];
7027 * Send any SACK-generated retransmissions. If we're explicitly
7028 * trying to send out new data (when sendalot is 1), bypass this
7029 * function. If we retransmit in fast recovery mode, decrement
7030 * snd_cwnd, since we're replacing a (future) new transmission with
7031 * a retransmission now, and we previously incremented snd_cwnd in
7035 * Still in sack recovery , reset rxmit flag to zero.
7037 while (rack->rc_free_cnt < rack_free_cache) {
7038 rsm = rack_alloc(rack);
7040 if (inp->inp_hpts_calls)
7043 goto just_return_nolock;
7045 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_next);
7046 rack->rc_free_cnt++;
7049 if (inp->inp_hpts_calls)
7050 inp->inp_hpts_calls = 0;
7054 if (flags & TH_RST) {
7058 if (rack->r_ctl.rc_tlpsend) {
7059 /* Tail loss probe */
7064 rsm = rack->r_ctl.rc_tlpsend;
7065 rack->r_ctl.rc_tlpsend = NULL;
7067 tlen = rsm->r_end - rsm->r_start;
7068 if (tlen > tp->t_maxseg)
7069 tlen = tp->t_maxseg;
7070 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
7071 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
7073 rsm->r_start, tp->snd_una, tp, rack, rsm));
7074 sb_offset = rsm->r_start - tp->snd_una;
7075 cwin = min(tp->snd_wnd, tlen);
7077 } else if (rack->r_ctl.rc_resend) {
7078 /* Retransmit timer */
7079 rsm = rack->r_ctl.rc_resend;
7080 rack->r_ctl.rc_resend = NULL;
7081 len = rsm->r_end - rsm->r_start;
7084 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
7085 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
7087 rsm->r_start, tp->snd_una, tp, rack, rsm));
7088 sb_offset = rsm->r_start - tp->snd_una;
7089 if (len >= tp->t_maxseg) {
7092 } else if ((rack->rc_in_persist == 0) &&
7093 ((rsm = tcp_rack_output(tp, rack, cts)) != NULL)) {
7096 if ((!IN_RECOVERY(tp->t_flags)) &&
7097 ((tp->t_flags & (TF_WASFRECOVERY | TF_WASCRECOVERY)) == 0)) {
7098 /* Enter recovery if not induced by a time-out */
7099 rack->r_ctl.rc_rsm_start = rsm->r_start;
7100 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd;
7101 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh;
7102 rack_cong_signal(tp, NULL, CC_NDUPACK);
7104 * When we enter recovery we need to assure we send
7107 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg;
7110 if (SEQ_LT(rsm->r_start, tp->snd_una)) {
7111 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n",
7112 tp, rack, rsm, rsm->r_start, tp->snd_una);
7115 tlen = rsm->r_end - rsm->r_start;
7116 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start),
7117 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p",
7119 rsm->r_start, tp->snd_una, tp, rack, rsm));
7120 sb_offset = rsm->r_start - tp->snd_una;
7121 if (tlen > rack->r_ctl.rc_prr_sndcnt) {
7122 len = rack->r_ctl.rc_prr_sndcnt;
7126 if (len >= tp->t_maxseg) {
7131 if ((rack->rc_timer_up == 0) &&
7134 * If its not a timer don't send a partial
7138 goto just_return_nolock;
7144 TCPSTAT_INC(tcps_sack_rexmits);
7145 TCPSTAT_ADD(tcps_sack_rexmit_bytes,
7146 min(len, tp->t_maxseg));
7147 counter_u64_add(rack_rtm_prr_retran, 1);
7150 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) {
7151 /* we are retransmitting the fin */
7155 * When retransmitting data do *not* include the
7156 * FIN. This could happen from a TLP probe.
7163 rack->r_ctl.rc_rsm_at_retran = rsm;
7166 * Get standard flags, and add SYN or FIN if requested by 'hidden'
7169 if (tp->t_flags & TF_NEEDFIN)
7171 if (tp->t_flags & TF_NEEDSYN)
7173 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) {
7175 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext);
7177 kern_prefetch(end_rsm, &prefetch_rsm);
7182 * If in persist timeout with window of 0, send 1 byte. Otherwise,
7183 * if window is small but nonzero and time TF_SENTFIN expired, we
7184 * will send what we can and go to transmit state.
7186 if (tp->t_flags & TF_FORCEDATA) {
7189 * If we still have some data to send, then clear
7190 * the FIN bit. Usually this would happen below
7191 * when it realizes that we aren't sending all the
7192 * data. However, if we have exactly 1 byte of
7193 * unsent data, then it won't clear the FIN bit
7194 * below, and if we are in persist state, we wind up
7195 * sending the packet without recording that we sent
7198 * We can't just blindly clear the FIN bit, because
7199 * if we don't have any more data to send then the
7200 * probe will be the FIN itself.
7202 if (sb_offset < sbused(sb))
7206 if (rack->rc_in_persist)
7207 rack_exit_persist(tp, rack);
7209 * If we are dropping persist mode then we need to
7210 * correct snd_nxt/snd_max and off.
7212 tp->snd_nxt = tp->snd_max;
7213 sb_offset = tp->snd_nxt - tp->snd_una;
7217 * If snd_nxt == snd_max and we have transmitted a FIN, the
7218 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a
7219 * negative length. This can also occur when TCP opens up its
7220 * congestion window while receiving additional duplicate acks after
7221 * fast-retransmit because TCP will reset snd_nxt to snd_max after
7222 * the fast-retransmit.
7224 * In the normal retransmit-FIN-only case, however, snd_nxt will be
7225 * set to snd_una, the sb_offset will be 0, and the length may wind
7228 * If sack_rxmit is true we are retransmitting from the scoreboard
7229 * in which case len is already set.
7231 if (sack_rxmit == 0) {
7234 avail = sbavail(sb);
7235 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail)
7236 sb_offset = tp->snd_nxt - tp->snd_una;
7239 if (IN_RECOVERY(tp->t_flags) == 0) {
7240 if (rack->r_ctl.rc_tlp_new_data) {
7241 /* TLP is forcing out new data */
7242 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) {
7243 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset);
7245 if (rack->r_ctl.rc_tlp_new_data > tp->snd_wnd)
7248 len = rack->r_ctl.rc_tlp_new_data;
7249 rack->r_ctl.rc_tlp_new_data = 0;
7252 if (sendwin > avail) {
7253 /* use the available */
7254 if (avail > sb_offset) {
7255 len = (int32_t)(avail - sb_offset);
7260 if (sendwin > sb_offset) {
7261 len = (int32_t)(sendwin - sb_offset);
7268 uint32_t outstanding;
7271 * We are inside of a SACK recovery episode and are
7272 * sending new data, having retransmitted all the
7273 * data possible so far in the scoreboard.
7275 outstanding = tp->snd_max - tp->snd_una;
7276 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd)
7278 else if (avail > sb_offset)
7279 len = avail - sb_offset;
7283 if (len > rack->r_ctl.rc_prr_sndcnt)
7284 len = rack->r_ctl.rc_prr_sndcnt;
7288 counter_u64_add(rack_rtm_prr_newdata, 1);
7291 if (len > tp->t_maxseg) {
7293 * We should never send more than a MSS when
7294 * retransmitting or sending new data in prr
7295 * mode unless the override flag is on. Most
7296 * likely the PRR algorithm is not going to
7297 * let us send a lot as well :-)
7299 if (rack->r_ctl.rc_prr_sendalot == 0)
7301 } else if (len < tp->t_maxseg) {
7303 * Do we send any? The idea here is if the
7304 * send empty's the socket buffer we want to
7305 * do it. However if not then lets just wait
7306 * for our prr_sndcnt to get bigger.
7310 leftinsb = sbavail(sb) - sb_offset;
7311 if (leftinsb > len) {
7312 /* This send does not empty the sb */
7318 if (prefetch_so_done == 0) {
7319 kern_prefetch(so, &prefetch_so_done);
7320 prefetch_so_done = 1;
7323 * Lop off SYN bit if it has already been sent. However, if this is
7324 * SYN-SENT state and if segment contains data and if we don't know
7325 * that foreign host supports TAO, suppress sending segment.
7327 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) &&
7328 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) {
7329 if (tp->t_state != TCPS_SYN_RECEIVED)
7332 * When sending additional segments following a TFO SYN|ACK,
7333 * do not include the SYN bit.
7335 if (IS_FASTOPEN(tp->t_flags) &&
7336 (tp->t_state == TCPS_SYN_RECEIVED))
7341 * Be careful not to send data and/or FIN on SYN segments. This
7342 * measure is needed to prevent interoperability problems with not
7343 * fully conformant TCP implementations.
7345 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
7350 * On TFO sockets, ensure no data is sent in the following cases:
7352 * - When retransmitting SYN|ACK on a passively-created socket
7354 * - When retransmitting SYN on an actively created socket
7356 * - When sending a zero-length cookie (cookie request) on an
7357 * actively created socket
7359 * - When the socket is in the CLOSED state (RST is being sent)
7361 if (IS_FASTOPEN(tp->t_flags) &&
7362 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) ||
7363 ((tp->t_state == TCPS_SYN_SENT) &&
7364 (tp->t_tfo_client_cookie_len == 0)) ||
7365 (flags & TH_RST))) {
7369 /* Without fast-open there should never be data sent on a SYN */
7370 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags)))
7374 * If FIN has been sent but not acked, but we haven't been
7375 * called to retransmit, len will be < 0. Otherwise, window
7376 * shrank after we sent into it. If window shrank to 0,
7377 * cancel pending retransmit, pull snd_nxt back to (closed)
7378 * window, and set the persist timer if it isn't already
7379 * going. If the window didn't close completely, just wait
7382 * We also do a general check here to ensure that we will
7383 * set the persist timer when we have data to send, but a
7384 * 0-byte window. This makes sure the persist timer is set
7385 * even if the packet hits one of the "goto send" lines
7389 if ((tp->snd_wnd == 0) &&
7390 (TCPS_HAVEESTABLISHED(tp->t_state)) &&
7391 (sb_offset < (int)sbavail(sb))) {
7392 tp->snd_nxt = tp->snd_una;
7393 rack_enter_persist(tp, rack, cts);
7396 /* len will be >= 0 after this point. */
7397 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
7398 tcp_sndbuf_autoscale(tp, so, sendwin);
7400 * Decide if we can use TCP Segmentation Offloading (if supported by
7403 * TSO may only be used if we are in a pure bulk sending state. The
7404 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP
7405 * options prevent using TSO. With TSO the TCP header is the same
7406 * (except for the sequence number) for all generated packets. This
7407 * makes it impossible to transmit any options which vary per
7408 * generated segment or packet.
7410 * IPv4 handling has a clear separation of ip options and ip header
7411 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
7412 * the right thing below to provide length of just ip options and thus
7413 * checking for ipoptlen is enough to decide if ip options are present.
7418 ipoptlen = ip6_optlen(tp->t_inpcb);
7421 if (tp->t_inpcb->inp_options)
7422 ipoptlen = tp->t_inpcb->inp_options->m_len -
7423 offsetof(struct ipoption, ipopt_list);
7426 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
7428 * Pre-calculate here as we save another lookup into the darknesses
7429 * of IPsec that way and can actually decide if TSO is ok.
7432 if (isipv6 && IPSEC_ENABLED(ipv6))
7433 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb);
7439 if (IPSEC_ENABLED(ipv4))
7440 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb);
7444 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
7445 ipoptlen += ipsec_optlen;
7447 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg &&
7448 (tp->t_port == 0) &&
7449 ((tp->t_flags & TF_SIGNATURE) == 0) &&
7450 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
7454 uint32_t outstanding;
7456 outstanding = tp->snd_max - tp->snd_una;
7457 if (tp->t_flags & TF_SENTFIN) {
7459 * If we sent a fin, snd_max is 1 higher than
7464 if (outstanding > 0) {
7466 * This is sub-optimal. We only send a stand alone
7467 * FIN on its own segment.
7469 if (flags & TH_FIN) {
7473 } else if (sack_rxmit) {
7474 if ((rsm->r_flags & RACK_HAS_FIN) == 0)
7477 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
7482 recwin = sbspace(&so->so_rcv);
7485 * Sender silly window avoidance. We transmit under the following
7486 * conditions when len is non-zero:
7488 * - We have a full segment (or more with TSO) - This is the last
7489 * buffer in a write()/send() and we are either idle or running
7490 * NODELAY - we've timed out (e.g. persist timer) - we have more
7491 * then 1/2 the maximum send window's worth of data (receiver may be
7492 * limited the window size) - we need to retransmit
7495 if (len >= tp->t_maxseg) {
7500 * NOTE! on localhost connections an 'ack' from the remote
7501 * end may occur synchronously with the output and cause us
7502 * to flush a buffer queued with moretocome. XXX
7505 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
7506 (idle || (tp->t_flags & TF_NODELAY)) &&
7507 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(&so->so_snd)) &&
7508 (tp->t_flags & TF_NOPUSH) == 0) {
7512 if (tp->t_flags & TF_FORCEDATA) { /* typ. timeout case */
7516 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */
7519 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) {
7523 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */
7533 * Sending of standalone window updates.
7535 * Window updates are important when we close our window due to a
7536 * full socket buffer and are opening it again after the application
7537 * reads data from it. Once the window has opened again and the
7538 * remote end starts to send again the ACK clock takes over and
7539 * provides the most current window information.
7541 * We must avoid the silly window syndrome whereas every read from
7542 * the receive buffer, no matter how small, causes a window update
7543 * to be sent. We also should avoid sending a flurry of window
7544 * updates when the socket buffer had queued a lot of data and the
7545 * application is doing small reads.
7547 * Prevent a flurry of pointless window updates by only sending an
7548 * update when we can increase the advertized window by more than
7549 * 1/4th of the socket buffer capacity. When the buffer is getting
7550 * full or is very small be more aggressive and send an update
7551 * whenever we can increase by two mss sized segments. In all other
7552 * situations the ACK's to new incoming data will carry further
7555 * Don't send an independent window update if a delayed ACK is
7556 * pending (it will get piggy-backed on it) or the remote side
7557 * already has done a half-close and won't send more data. Skip
7558 * this if the connection is in T/TCP half-open state.
7560 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
7561 !(tp->t_flags & TF_DELACK) &&
7562 !TCPS_HAVERCVDFIN(tp->t_state)) {
7564 * "adv" is the amount we could increase the window, taking
7565 * into account that we are limited by TCP_MAXWIN <<
7571 adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale);
7572 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
7573 oldwin = (tp->rcv_adv - tp->rcv_nxt);
7579 * If the new window size ends up being the same as the old
7580 * size when it is scaled, then don't force a window update.
7582 if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale)
7585 if (adv >= (int32_t)(2 * tp->t_maxseg) &&
7586 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
7587 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) ||
7588 so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg)) {
7592 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat)
7598 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
7599 * is also a catch-all for the retransmit timer timeout case.
7601 if (tp->t_flags & TF_ACKNOW) {
7605 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) {
7609 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
7614 * If our state indicates that FIN should be sent and we have not
7615 * yet done so, then we need to send.
7617 if ((flags & TH_FIN) &&
7618 (tp->snd_nxt == tp->snd_una)) {
7623 * No reason to send a segment, just return.
7628 if (tot_len_this_send == 0)
7629 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1);
7630 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1);
7631 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling);
7632 tp->t_flags &= ~TF_FORCEDATA;
7636 if (doing_tlp == 0) {
7638 * Data not a TLP, and its not the rxt firing. If it is the
7639 * rxt firing, we want to leave the tlp_in_progress flag on
7640 * so we don't send another TLP. It has to be a rack timer
7641 * or normal send (response to acked data) to clear the tlp
7644 rack->rc_tlp_in_progress = 0;
7646 SOCKBUF_LOCK_ASSERT(sb);
7648 if (len >= tp->t_maxseg)
7649 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
7651 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
7654 * Before ESTABLISHED, force sending of initial options unless TCP
7655 * set not to do any options. NOTE: we assume that the IP/TCP header
7656 * plus TCP options always fit in a single mbuf, leaving room for a
7657 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr)
7658 * + optlen <= MCLBYTES
7663 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
7666 hdrlen = sizeof(struct tcpiphdr);
7669 * Compute options for segment. We only have to care about SYN and
7670 * established connection segments. Options for SYN-ACK segments
7671 * are handled in TCP syncache.
7674 if ((tp->t_flags & TF_NOOPT) == 0) {
7675 /* Maximum segment size. */
7676 if (flags & TH_SYN) {
7677 tp->snd_nxt = tp->iss;
7678 to.to_mss = tcp_mssopt(&inp->inp_inc);
7679 #ifdef NETFLIX_TCPOUDP
7681 to.to_mss -= V_tcp_udp_tunneling_overhead;
7683 to.to_flags |= TOF_MSS;
7686 * On SYN or SYN|ACK transmits on TFO connections,
7687 * only include the TFO option if it is not a
7688 * retransmit, as the presence of the TFO option may
7689 * have caused the original SYN or SYN|ACK to have
7690 * been dropped by a middlebox.
7692 if (IS_FASTOPEN(tp->t_flags) &&
7693 (tp->t_rxtshift == 0)) {
7694 if (tp->t_state == TCPS_SYN_RECEIVED) {
7695 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
7697 (u_int8_t *)&tp->t_tfo_cookie.server;
7698 to.to_flags |= TOF_FASTOPEN;
7700 } else if (tp->t_state == TCPS_SYN_SENT) {
7702 tp->t_tfo_client_cookie_len;
7704 tp->t_tfo_cookie.client;
7705 to.to_flags |= TOF_FASTOPEN;
7708 * If we wind up having more data to
7709 * send with the SYN than can fit in
7710 * one segment, don't send any more
7711 * until the SYN|ACK comes back from
7718 /* Window scaling. */
7719 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
7720 to.to_wscale = tp->request_r_scale;
7721 to.to_flags |= TOF_SCALE;
7724 if ((tp->t_flags & TF_RCVD_TSTMP) ||
7725 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
7726 to.to_tsval = cts + tp->ts_offset;
7727 to.to_tsecr = tp->ts_recent;
7728 to.to_flags |= TOF_TS;
7730 /* Set receive buffer autosizing timestamp. */
7731 if (tp->rfbuf_ts == 0 &&
7732 (so->so_rcv.sb_flags & SB_AUTOSIZE))
7733 tp->rfbuf_ts = tcp_ts_getticks();
7734 /* Selective ACK's. */
7736 to.to_flags |= TOF_SACKPERM;
7737 else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
7738 tp->rcv_numsacks > 0) {
7739 to.to_flags |= TOF_SACK;
7740 to.to_nsacks = tp->rcv_numsacks;
7741 to.to_sacks = (u_char *)tp->sackblks;
7743 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
7744 /* TCP-MD5 (RFC2385). */
7745 if (tp->t_flags & TF_SIGNATURE)
7746 to.to_flags |= TOF_SIGNATURE;
7747 #endif /* TCP_SIGNATURE */
7749 /* Processing the options. */
7750 hdrlen += optlen = tcp_addoptions(&to, opt);
7752 * If we wanted a TFO option to be added, but it was unable
7753 * to fit, ensure no data is sent.
7755 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie &&
7756 !(to.to_flags & TOF_FASTOPEN))
7759 #ifdef NETFLIX_TCPOUDP
7761 if (V_tcp_udp_tunneling_port == 0) {
7762 /* The port was removed?? */
7763 SOCKBUF_UNLOCK(&so->so_snd);
7764 return (EHOSTUNREACH);
7766 hdrlen += sizeof(struct udphdr);
7770 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
7771 ipoptlen += ipsec_optlen;
7775 * Adjust data length if insertion of options will bump the packet
7776 * length beyond the t_maxseg length. Clear the FIN bit because we
7777 * cut off the tail of the segment.
7779 if (len + optlen + ipoptlen > tp->t_maxseg) {
7780 if (flags & TH_FIN) {
7785 uint32_t if_hw_tsomax;
7789 /* extract TSO information */
7790 if_hw_tsomax = tp->t_tsomax;
7791 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
7792 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
7793 KASSERT(ipoptlen == 0,
7794 ("%s: TSO can't do IP options", __func__));
7797 * Check if we should limit by maximum payload
7800 if (if_hw_tsomax != 0) {
7801 /* compute maximum TSO length */
7802 max_len = (if_hw_tsomax - hdrlen -
7806 } else if (len > max_len) {
7812 * Prevent the last segment from being fractional
7813 * unless the send sockbuf can be emptied:
7815 max_len = (tp->t_maxseg - optlen);
7816 if ((sb_offset + len) < sbavail(sb)) {
7817 moff = len % (u_int)max_len;
7824 * In case there are too many small fragments don't
7827 if (len <= max_len) {
7833 * Send the FIN in a separate segment after the bulk
7834 * sending is done. We don't trust the TSO
7835 * implementations to clear the FIN flag on all but
7838 if (tp->t_flags & TF_NEEDFIN)
7842 len = tp->t_maxseg - optlen - ipoptlen;
7847 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
7848 ("%s: len > IP_MAXPACKET", __func__));
7851 if (max_linkhdr + hdrlen > MCLBYTES)
7853 if (max_linkhdr + hdrlen > MHLEN)
7855 panic("tcphdr too big");
7859 * This KASSERT is here to catch edge cases at a well defined place.
7860 * Before, those had triggered (random) panic conditions further
7863 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
7868 * We have outstanding data, don't send a fin by itself!.
7873 * Grab a header mbuf, attaching a copy of data to be transmitted,
7874 * and initialize the header from the template for sends on this
7881 if (rack->rc_pace_max_segs)
7882 max_val = rack->rc_pace_max_segs * tp->t_maxseg;
7886 * We allow a limit on sending with hptsi.
7888 if (len > max_val) {
7892 if (MHLEN < hdrlen + max_linkhdr)
7893 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
7896 m = m_gethdr(M_NOWAIT, MT_DATA);
7904 m->m_data += max_linkhdr;
7908 * Start the m_copy functions from the closest mbuf to the
7909 * sb_offset in the socket buffer chain.
7911 mb = sbsndptr_noadv(sb, sb_offset, &moff);
7912 if (len <= MHLEN - hdrlen - max_linkhdr) {
7913 m_copydata(mb, moff, (int)len,
7914 mtod(m, caddr_t)+hdrlen);
7915 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
7916 sbsndptr_adv(sb, mb, len);
7919 struct sockbuf *msb;
7921 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
7925 m->m_next = tcp_m_copym(mb, moff, &len,
7926 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb);
7927 if (len <= (tp->t_maxseg - optlen)) {
7929 * Must have ran out of mbufs for the copy
7930 * shorten it to no longer need tso. Lets
7931 * not put on sendalot since we are low on
7936 if (m->m_next == NULL) {
7944 if ((tp->t_flags & TF_FORCEDATA) && len == 1) {
7945 TCPSTAT_INC(tcps_sndprobe);
7946 #ifdef NETFLIX_STATS
7947 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
7948 stats_voi_update_abs_u32(tp->t_stats,
7949 VOI_TCP_RETXPB, len);
7951 stats_voi_update_abs_u64(tp->t_stats,
7954 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
7955 if (rsm && (rsm->r_flags & RACK_TLP)) {
7957 * TLP should not count in retran count, but
7960 counter_u64_add(rack_tlp_retran, 1);
7961 counter_u64_add(rack_tlp_retran_bytes, len);
7963 tp->t_sndrexmitpack++;
7964 TCPSTAT_INC(tcps_sndrexmitpack);
7965 TCPSTAT_ADD(tcps_sndrexmitbyte, len);
7967 #ifdef NETFLIX_STATS
7968 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB,
7972 TCPSTAT_INC(tcps_sndpack);
7973 TCPSTAT_ADD(tcps_sndbyte, len);
7974 #ifdef NETFLIX_STATS
7975 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB,
7980 * If we're sending everything we've got, set PUSH. (This
7981 * will keep happy those implementations which only give
7982 * data to the user when a buffer fills or a PUSH comes in.)
7984 if (sb_offset + len == sbused(sb) &&
7990 * Are we doing hptsi, if so we must calculate the slot. We
7991 * only do hptsi in ESTABLISHED and with no RESET being
7992 * sent where we have data to send.
7994 if (((tp->t_state == TCPS_ESTABLISHED) ||
7995 (tp->t_state == TCPS_CLOSE_WAIT) ||
7996 ((tp->t_state == TCPS_FIN_WAIT_1) &&
7997 ((tp->t_flags & TF_SENTFIN) == 0) &&
7998 ((flags & TH_FIN) == 0))) &&
7999 ((flags & TH_RST) == 0) &&
8000 (rack->rc_always_pace)) {
8002 * We use the most optimistic possible cwnd/srtt for
8003 * sending calculations. This will make our
8004 * calculation anticipate getting more through
8005 * quicker then possible. But thats ok we don't want
8006 * the peer to have a gap in data sending.
8008 uint32_t srtt, cwnd, tr_perms = 0;
8010 if (rack->r_ctl.rc_rack_min_rtt)
8011 srtt = rack->r_ctl.rc_rack_min_rtt;
8013 srtt = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT));
8014 if (rack->r_ctl.rc_rack_largest_cwnd)
8015 cwnd = rack->r_ctl.rc_rack_largest_cwnd;
8017 cwnd = tp->snd_cwnd;
8018 tr_perms = cwnd / srtt;
8019 if (tr_perms == 0) {
8020 tr_perms = tp->t_maxseg;
8022 tot_len_this_send += len;
8024 * Calculate how long this will take to drain, if
8025 * the calculation comes out to zero, thats ok we
8026 * will use send_a_lot to possibly spin around for
8027 * more increasing tot_len_this_send to the point
8028 * that its going to require a pace, or we hit the
8029 * cwnd. Which in that case we are just waiting for
8032 slot = tot_len_this_send / tr_perms;
8033 /* Now do we reduce the time so we don't run dry? */
8034 if (slot && rack->rc_pace_reduce) {
8037 reduce = (slot / rack->rc_pace_reduce);
8038 if (reduce < slot) {
8043 if (rack->r_enforce_min_pace &&
8045 (tot_len_this_send >= (rack->r_min_pace_seg_thresh * tp->t_maxseg))) {
8046 /* We are enforcing a minimum pace time of 1ms */
8047 slot = rack->r_enforce_min_pace;
8053 if (tp->t_flags & TF_ACKNOW)
8054 TCPSTAT_INC(tcps_sndacks);
8055 else if (flags & (TH_SYN | TH_FIN | TH_RST))
8056 TCPSTAT_INC(tcps_sndctrl);
8057 else if (SEQ_GT(tp->snd_up, tp->snd_una))
8058 TCPSTAT_INC(tcps_sndurg);
8060 TCPSTAT_INC(tcps_sndwinup);
8062 m = m_gethdr(M_NOWAIT, MT_DATA);
8069 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
8074 m->m_data += max_linkhdr;
8077 SOCKBUF_UNLOCK_ASSERT(sb);
8078 m->m_pkthdr.rcvif = (struct ifnet *)0;
8080 mac_inpcb_create_mbuf(inp, m);
8084 ip6 = mtod(m, struct ip6_hdr *);
8085 #ifdef NETFLIX_TCPOUDP
8087 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr));
8088 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
8089 udp->uh_dport = tp->t_port;
8090 ulen = hdrlen + len - sizeof(struct ip6_hdr);
8091 udp->uh_ulen = htons(ulen);
8092 th = (struct tcphdr *)(udp + 1);
8095 th = (struct tcphdr *)(ip6 + 1);
8096 tcpip_fillheaders(inp, ip6, th);
8100 ip = mtod(m, struct ip *);
8102 ipov = (struct ipovly *)ip;
8104 #ifdef NETFLIX_TCPOUDP
8106 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip));
8107 udp->uh_sport = htons(V_tcp_udp_tunneling_port);
8108 udp->uh_dport = tp->t_port;
8109 ulen = hdrlen + len - sizeof(struct ip);
8110 udp->uh_ulen = htons(ulen);
8111 th = (struct tcphdr *)(udp + 1);
8114 th = (struct tcphdr *)(ip + 1);
8115 tcpip_fillheaders(inp, ip, th);
8118 * Fill in fields, remembering maximum advertised window for use in
8119 * delaying messages about window sizes. If resending a FIN, be sure
8120 * not to use a new sequence number.
8122 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
8123 tp->snd_nxt == tp->snd_max)
8126 * If we are starting a connection, send ECN setup SYN packet. If we
8127 * are on a retransmit, we may resend those bits a number of times
8130 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) {
8131 if (tp->t_rxtshift >= 1) {
8132 if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
8133 flags |= TH_ECE | TH_CWR;
8135 flags |= TH_ECE | TH_CWR;
8137 if (tp->t_state == TCPS_ESTABLISHED &&
8138 (tp->t_flags & TF_ECN_PERMIT)) {
8140 * If the peer has ECN, mark data packets with ECN capable
8141 * transmission (ECT). Ignore pure ack packets,
8142 * retransmissions and window probes.
8144 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
8145 !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
8148 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
8151 ip->ip_tos |= IPTOS_ECN_ECT0;
8152 TCPSTAT_INC(tcps_ecn_ect0);
8155 * Reply with proper ECN notifications.
8157 if (tp->t_flags & TF_ECN_SND_CWR) {
8159 tp->t_flags &= ~TF_ECN_SND_CWR;
8161 if (tp->t_flags & TF_ECN_SND_ECE)
8165 * If we are doing retransmissions, then snd_nxt will not reflect
8166 * the first unsent octet. For ACK only packets, we do not want the
8167 * sequence number of the retransmitted packet, we want the sequence
8168 * number of the next unsent octet. So, if there is no data (and no
8169 * SYN or FIN), use snd_max instead of snd_nxt when filling in
8170 * ti_seq. But if we are in persist state, snd_max might reflect
8171 * one byte beyond the right edge of the window, so use snd_nxt in
8172 * that case, since we know we aren't doing a retransmission.
8173 * (retransmit and persist are mutually exclusive...)
8175 if (sack_rxmit == 0) {
8176 if (len || (flags & (TH_SYN | TH_FIN)) ||
8177 rack->rc_in_persist) {
8178 th->th_seq = htonl(tp->snd_nxt);
8179 rack_seq = tp->snd_nxt;
8180 } else if (flags & TH_RST) {
8182 * For a Reset send the last cum ack in sequence
8183 * (this like any other choice may still generate a
8184 * challenge ack, if a ack-update packet is in
8187 th->th_seq = htonl(tp->snd_una);
8188 rack_seq = tp->snd_una;
8190 th->th_seq = htonl(tp->snd_max);
8191 rack_seq = tp->snd_max;
8194 th->th_seq = htonl(rsm->r_start);
8195 rack_seq = rsm->r_start;
8197 th->th_ack = htonl(tp->rcv_nxt);
8199 bcopy(opt, th + 1, optlen);
8200 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
8202 th->th_flags = flags;
8204 * Calculate receive window. Don't shrink window, but avoid silly
8206 * If a RST segment is sent, advertise a window of zero.
8208 if (flags & TH_RST) {
8211 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
8212 recwin < (long)tp->t_maxseg)
8214 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
8215 recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
8216 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
8217 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
8218 recwin = (long)TCP_MAXWIN << tp->rcv_scale;
8222 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or
8223 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is
8224 * handled in syncache.
8227 th->th_win = htons((u_short)
8228 (min(sbspace(&so->so_rcv), TCP_MAXWIN)));
8230 th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
8232 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0
8233 * window. This may cause the remote transmitter to stall. This
8234 * flag tells soreceive() to disable delayed acknowledgements when
8235 * draining the buffer. This can occur if the receiver is
8236 * attempting to read more data than can be buffered prior to
8237 * transmitting on the connection.
8239 if (th->th_win == 0) {
8241 tp->t_flags |= TF_RXWIN0SENT;
8243 tp->t_flags &= ~TF_RXWIN0SENT;
8244 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
8245 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
8246 th->th_flags |= TH_URG;
8249 * If no urgent pointer to send, then we pull the urgent
8250 * pointer to the left edge of the send window so that it
8251 * doesn't drift into the send window on sequence number
8254 tp->snd_up = tp->snd_una; /* drag it along */
8256 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
8257 if (to.to_flags & TOF_SIGNATURE) {
8259 * Calculate MD5 signature and put it into the place
8260 * determined before.
8261 * NOTE: since TCP options buffer doesn't point into
8262 * mbuf's data, calculate offset and use it.
8264 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th,
8265 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) {
8267 * Do not send segment if the calculation of MD5
8268 * digest has failed.
8276 * Put TCP length in extended header, and then checksum extended
8279 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
8283 * ip6_plen is not need to be filled now, and will be filled
8287 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
8288 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
8289 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
8290 th->th_sum = htons(0);
8292 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
8293 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
8294 th->th_sum = in6_cksum_pseudo(ip6,
8295 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP,
8300 #if defined(INET6) && defined(INET)
8306 m->m_pkthdr.csum_flags = CSUM_UDP;
8307 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
8308 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
8309 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
8310 th->th_sum = htons(0);
8312 m->m_pkthdr.csum_flags = CSUM_TCP;
8313 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
8314 th->th_sum = in_pseudo(ip->ip_src.s_addr,
8315 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) +
8316 IPPROTO_TCP + len + optlen));
8318 /* IP version must be set here for ipv4/ipv6 checking later */
8319 KASSERT(ip->ip_v == IPVERSION,
8320 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
8325 * Enable TSO and specify the size of the segments. The TCP pseudo
8326 * header checksum is always provided. XXX: Fixme: This is currently
8327 * not the case for IPv6.
8330 KASSERT(len > tp->t_maxseg - optlen,
8331 ("%s: len <= tso_segsz", __func__));
8332 m->m_pkthdr.csum_flags |= CSUM_TSO;
8333 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
8335 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
8336 KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL),
8337 ("%s: mbuf chain shorter than expected: %d + %u + %u - %u != %u",
8338 __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL)));
8340 KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL),
8341 ("%s: mbuf chain shorter than expected: %d + %u + %u != %u",
8342 __func__, len, hdrlen, ipoptlen, m_length(m, NULL)));
8346 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
8347 hhook_run_tcp_est_out(tp, th, &to, len, tso);
8354 if (so->so_options & SO_DEBUG) {
8361 save = ipov->ih_len;
8362 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen +
8363 * (th->th_off << 2) */ );
8365 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
8369 ipov->ih_len = save;
8371 #endif /* TCPDEBUG */
8373 /* We're getting ready to send; log now. */
8374 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
8375 union tcp_log_stackspecific log;
8377 memset(&log.u_bbr, 0, sizeof(log.u_bbr));
8378 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts;
8379 log.u_bbr.ininput = rack->rc_inp->inp_in_input;
8380 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt;
8381 if (rsm || sack_rxmit) {
8382 log.u_bbr.flex8 = 1;
8384 log.u_bbr.flex8 = 0;
8386 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK,
8387 len, &log, false, NULL, NULL, 0, NULL);
8392 * Fill in IP length and desired time to live and send to IP level.
8393 * There should be a better way to handle ttl and tos; we could keep
8394 * them in the template, but need a way to checksum without them.
8397 * m->m_pkthdr.len should have been set before cksum calcuration,
8398 * because in6_cksum() need it.
8403 * we separately set hoplimit for every segment, since the
8404 * user might want to change the value via setsockopt. Also,
8405 * desired default hop limit might be changed via Neighbor
8408 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
8411 * Set the packet size here for the benefit of DTrace
8412 * probes. ip6_output() will set it properly; it's supposed
8413 * to include the option header lengths as well.
8415 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
8417 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
8418 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
8420 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
8422 if (tp->t_state == TCPS_SYN_SENT)
8423 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
8425 TCP_PROBE5(send, NULL, tp, ip6, tp, th);
8426 /* TODO: IPv6 IP6TOS_ECT bit on */
8427 error = ip6_output(m, tp->t_inpcb->in6p_outputopts,
8429 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0),
8432 if (error == EMSGSIZE && inp->inp_route6.ro_rt != NULL)
8433 mtu = inp->inp_route6.ro_rt->rt_mtu;
8436 #if defined(INET) && defined(INET6)
8441 ip->ip_len = htons(m->m_pkthdr.len);
8443 if (inp->inp_vflag & INP_IPV6PROTO)
8444 ip->ip_ttl = in6_selecthlim(inp, NULL);
8447 * If we do path MTU discovery, then we set DF on every
8448 * packet. This might not be the best thing to do according
8449 * to RFC3390 Section 2. However the tcp hostcache migitates
8450 * the problem so it affects only the first tcp connection
8453 * NB: Don't set DF on small MTU/MSS to have a safe
8456 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
8457 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
8458 if (tp->t_port == 0 || len < V_tcp_minmss) {
8459 ip->ip_off |= htons(IP_DF);
8462 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
8465 if (tp->t_state == TCPS_SYN_SENT)
8466 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
8468 TCP_PROBE5(send, NULL, tp, ip, tp, th);
8470 error = ip_output(m, tp->t_inpcb->inp_options, &inp->inp_route,
8471 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
8473 if (error == EMSGSIZE && inp->inp_route.ro_rt != NULL)
8474 mtu = inp->inp_route.ro_rt->rt_mtu;
8480 lgb->tlb_errno = error;
8484 * In transmit state, time the transmission and arrange for the
8485 * retransmit. In persist state, just set snd_max.
8489 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1);
8490 else if (len == 1) {
8491 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1);
8492 } else if (len > 1) {
8495 idx = (len / tp->t_maxseg) + 3;
8496 if (idx >= TCP_MSS_ACCT_ATIMER)
8497 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1);
8499 counter_u64_add(rack_out_size[idx], 1);
8502 if (sub_from_prr && (error == 0)) {
8503 rack->r_ctl.rc_prr_sndcnt -= len;
8506 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, cts,
8508 if ((tp->t_flags & TF_FORCEDATA) == 0 ||
8509 (rack->rc_in_persist == 0)) {
8510 tcp_seq startseq = tp->snd_nxt;
8513 * Advance snd_nxt over sequence space of this segment.
8516 /* We don't log or do anything with errors */
8519 if (flags & (TH_SYN | TH_FIN)) {
8522 if (flags & TH_FIN) {
8524 tp->t_flags |= TF_SENTFIN;
8527 /* In the ENOBUFS case we do *not* update snd_max */
8532 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
8533 if (tp->snd_una == tp->snd_max) {
8535 * Update the time we just added data since
8536 * none was outstanding.
8538 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
8539 tp->t_acktime = ticks;
8541 tp->snd_max = tp->snd_nxt;
8543 * Time this transmission if not a retransmission and
8544 * not currently timing anything.
8545 * This is only relevant in case of switching back to
8548 if (tp->t_rtttime == 0) {
8549 tp->t_rtttime = ticks;
8550 tp->t_rtseq = startseq;
8551 TCPSTAT_INC(tcps_segstimed);
8553 #ifdef NETFLIX_STATS
8554 if (!(tp->t_flags & TF_GPUTINPROG) && len) {
8555 tp->t_flags |= TF_GPUTINPROG;
8556 tp->gput_seq = startseq;
8557 tp->gput_ack = startseq +
8558 ulmin(sbavail(sb) - sb_offset, sendwin);
8559 tp->gput_ts = tcp_ts_getticks();
8564 * Set retransmit timer if not currently set, and not doing
8565 * a pure ack or a keep-alive probe. Initial value for
8566 * retransmit timer is smoothed round-trip time + 2 *
8567 * round-trip time variance. Initialize shift counter which
8568 * is used for backoff of retransmit time.
8571 if ((tp->snd_wnd == 0) &&
8572 TCPS_HAVEESTABLISHED(tp->t_state)) {
8574 * If the persists timer was set above (right before
8575 * the goto send), and still needs to be on. Lets
8576 * make sure all is canceled. If the persist timer
8577 * is not running, we want to get it up.
8579 if (rack->rc_in_persist == 0) {
8580 rack_enter_persist(tp, rack, cts);
8585 * Persist case, update snd_max but since we are in persist
8586 * mode (no window) we do not update snd_nxt.
8595 if (flags & TH_FIN) {
8597 tp->t_flags |= TF_SENTFIN;
8599 /* In the ENOBUFS case we do *not* update snd_max */
8600 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) {
8601 if (tp->snd_una == tp->snd_max) {
8603 * Update the time we just added data since
8604 * none was outstanding.
8606 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__);
8607 tp->t_acktime = ticks;
8609 tp->snd_max = tp->snd_nxt + len;
8614 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */
8616 * Failures do not advance the seq counter above. For the
8617 * case of ENOBUFS we will fall out and retry in 1ms with
8618 * the hpts. Everything else will just have to retransmit
8621 * In any case, we do not want to loop around for another
8622 * send without a good reason.
8627 tp->t_flags &= ~TF_FORCEDATA;
8628 tp->t_softerror = error;
8633 * Pace us right away to retry in a some
8636 slot = 1 + rack->rc_enobuf;
8637 if (rack->rc_enobuf < 255)
8639 if (slot > (rack->rc_rack_rtt / 2)) {
8640 slot = rack->rc_rack_rtt / 2;
8645 counter_u64_add(rack_saw_enobuf, 1);
8650 * For some reason the interface we used initially
8651 * to send segments changed to another or lowered
8652 * its MTU. If TSO was active we either got an
8653 * interface without TSO capabilits or TSO was
8654 * turned off. If we obtained mtu from ip_output()
8655 * then update it and try again.
8658 tp->t_flags &= ~TF_TSO;
8660 tcp_mss_update(tp, -1, mtu, NULL, NULL);
8664 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1);
8665 tp->t_flags &= ~TF_FORCEDATA;
8668 counter_u64_add(rack_saw_enetunreach, 1);
8672 if (TCPS_HAVERCVDSYN(tp->t_state)) {
8673 tp->t_softerror = error;
8678 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1);
8679 tp->t_flags &= ~TF_FORCEDATA;
8683 rack->rc_enobuf = 0;
8685 TCPSTAT_INC(tcps_sndtotal);
8688 * Data sent (as far as we can tell). If this advertises a larger
8689 * window than any other segment, then remember the size of the
8690 * advertised window. Any pending ACK has now been sent.
8692 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
8693 tp->rcv_adv = tp->rcv_nxt + recwin;
8694 tp->last_ack_sent = tp->rcv_nxt;
8695 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
8697 rack->r_tlp_running = 0;
8698 if ((flags & TH_RST) || (would_have_fin == 1)) {
8700 * We don't send again after a RST. We also do *not* send
8701 * again if we would have had a find, but now have
8708 /* set the rack tcb into the slot N */
8709 counter_u64_add(rack_paced_segments, 1);
8710 } else if (sendalot) {
8712 counter_u64_add(rack_unpaced_segments, 1);
8714 tp->t_flags &= ~TF_FORCEDATA;
8717 counter_u64_add(rack_unpaced_segments, 1);
8719 tp->t_flags &= ~TF_FORCEDATA;
8720 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1);
8725 * rack_ctloutput() must drop the inpcb lock before performing copyin on
8726 * socket option arguments. When it re-acquires the lock after the copy, it
8727 * has to revalidate that the connection is still valid for the socket
8731 rack_set_sockopt(struct socket *so, struct sockopt *sopt,
8732 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
8734 int32_t error = 0, optval;
8736 switch (sopt->sopt_name) {
8737 case TCP_RACK_PROP_RATE:
8739 case TCP_RACK_TLP_REDUCE:
8740 case TCP_RACK_EARLY_RECOV:
8741 case TCP_RACK_PACE_ALWAYS:
8743 case TCP_RACK_PACE_REDUCE:
8744 case TCP_RACK_PACE_MAX_SEG:
8745 case TCP_RACK_PRR_SENDALOT:
8746 case TCP_RACK_MIN_TO:
8747 case TCP_RACK_EARLY_SEG:
8748 case TCP_RACK_REORD_THRESH:
8749 case TCP_RACK_REORD_FADE:
8750 case TCP_RACK_TLP_THRESH:
8751 case TCP_RACK_PKT_DELAY:
8752 case TCP_RACK_TLP_USE:
8753 case TCP_RACK_TLP_INC_VAR:
8754 case TCP_RACK_IDLE_REDUCE_HIGH:
8755 case TCP_RACK_MIN_PACE:
8756 case TCP_RACK_MIN_PACE_SEG:
8757 case TCP_BBR_RACK_RTT_USE:
8758 case TCP_DATA_AFTER_CLOSE:
8761 return (tcp_default_ctloutput(so, sopt, inp, tp));
8765 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
8769 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8771 return (ECONNRESET);
8773 tp = intotcpcb(inp);
8774 rack = (struct tcp_rack *)tp->t_fb_ptr;
8775 switch (sopt->sopt_name) {
8776 case TCP_RACK_PROP_RATE:
8777 if ((optval <= 0) || (optval >= 100)) {
8781 RACK_OPTS_INC(tcp_rack_prop_rate);
8782 rack->r_ctl.rc_prop_rate = optval;
8784 case TCP_RACK_TLP_USE:
8785 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) {
8789 RACK_OPTS_INC(tcp_tlp_use);
8790 rack->rack_tlp_threshold_use = optval;
8793 /* RACK proportional rate reduction (bool) */
8794 RACK_OPTS_INC(tcp_rack_prop);
8795 rack->r_ctl.rc_prop_reduce = optval;
8797 case TCP_RACK_TLP_REDUCE:
8798 /* RACK TLP cwnd reduction (bool) */
8799 RACK_OPTS_INC(tcp_rack_tlp_reduce);
8800 rack->r_ctl.rc_tlp_cwnd_reduce = optval;
8802 case TCP_RACK_EARLY_RECOV:
8803 /* Should recovery happen early (bool) */
8804 RACK_OPTS_INC(tcp_rack_early_recov);
8805 rack->r_ctl.rc_early_recovery = optval;
8807 case TCP_RACK_PACE_ALWAYS:
8808 /* Use the always pace method (bool) */
8809 RACK_OPTS_INC(tcp_rack_pace_always);
8811 rack->rc_always_pace = 1;
8813 rack->rc_always_pace = 0;
8815 case TCP_RACK_PACE_REDUCE:
8816 /* RACK Hptsi reduction factor (divisor) */
8817 RACK_OPTS_INC(tcp_rack_pace_reduce);
8819 /* Must be non-zero */
8820 rack->rc_pace_reduce = optval;
8824 case TCP_RACK_PACE_MAX_SEG:
8825 /* Max segments in a pace */
8826 RACK_OPTS_INC(tcp_rack_max_seg);
8827 rack->rc_pace_max_segs = optval;
8829 case TCP_RACK_PRR_SENDALOT:
8830 /* Allow PRR to send more than one seg */
8831 RACK_OPTS_INC(tcp_rack_prr_sendalot);
8832 rack->r_ctl.rc_prr_sendalot = optval;
8834 case TCP_RACK_MIN_TO:
8835 /* Minimum time between rack t-o's in ms */
8836 RACK_OPTS_INC(tcp_rack_min_to);
8837 rack->r_ctl.rc_min_to = optval;
8839 case TCP_RACK_EARLY_SEG:
8840 /* If early recovery max segments */
8841 RACK_OPTS_INC(tcp_rack_early_seg);
8842 rack->r_ctl.rc_early_recovery_segs = optval;
8844 case TCP_RACK_REORD_THRESH:
8845 /* RACK reorder threshold (shift amount) */
8846 RACK_OPTS_INC(tcp_rack_reord_thresh);
8847 if ((optval > 0) && (optval < 31))
8848 rack->r_ctl.rc_reorder_shift = optval;
8852 case TCP_RACK_REORD_FADE:
8853 /* Does reordering fade after ms time */
8854 RACK_OPTS_INC(tcp_rack_reord_fade);
8855 rack->r_ctl.rc_reorder_fade = optval;
8857 case TCP_RACK_TLP_THRESH:
8858 /* RACK TLP theshold i.e. srtt+(srtt/N) */
8859 RACK_OPTS_INC(tcp_rack_tlp_thresh);
8861 rack->r_ctl.rc_tlp_threshold = optval;
8865 case TCP_RACK_PKT_DELAY:
8866 /* RACK added ms i.e. rack-rtt + reord + N */
8867 RACK_OPTS_INC(tcp_rack_pkt_delay);
8868 rack->r_ctl.rc_pkt_delay = optval;
8870 case TCP_RACK_TLP_INC_VAR:
8871 /* Does TLP include rtt variance in t-o */
8872 RACK_OPTS_INC(tcp_rack_tlp_inc_var);
8873 rack->r_ctl.rc_prr_inc_var = optval;
8875 case TCP_RACK_IDLE_REDUCE_HIGH:
8876 RACK_OPTS_INC(tcp_rack_idle_reduce_high);
8878 rack->r_idle_reduce_largest = 1;
8880 rack->r_idle_reduce_largest = 0;
8884 tp->t_delayed_ack = 0;
8886 tp->t_delayed_ack = 1;
8887 if (tp->t_flags & TF_DELACK) {
8888 tp->t_flags &= ~TF_DELACK;
8889 tp->t_flags |= TF_ACKNOW;
8893 case TCP_RACK_MIN_PACE:
8894 RACK_OPTS_INC(tcp_rack_min_pace);
8896 rack->r_enforce_min_pace = 3;
8898 rack->r_enforce_min_pace = optval;
8900 case TCP_RACK_MIN_PACE_SEG:
8901 RACK_OPTS_INC(tcp_rack_min_pace_seg);
8903 rack->r_min_pace_seg_thresh = 15;
8905 rack->r_min_pace_seg_thresh = optval;
8907 case TCP_BBR_RACK_RTT_USE:
8908 if ((optval != USE_RTT_HIGH) &&
8909 (optval != USE_RTT_LOW) &&
8910 (optval != USE_RTT_AVG))
8913 rack->r_ctl.rc_rate_sample_method = optval;
8915 case TCP_DATA_AFTER_CLOSE:
8917 rack->rc_allow_data_af_clo = 1;
8919 rack->rc_allow_data_af_clo = 0;
8922 return (tcp_default_ctloutput(so, sopt, inp, tp));
8925 #ifdef NETFLIX_STATS
8926 tcp_log_socket_option(tp, sopt->sopt_name, optval, error);
8933 rack_get_sockopt(struct socket *so, struct sockopt *sopt,
8934 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack)
8936 int32_t error, optval;
8939 * Because all our options are either boolean or an int, we can just
8940 * pull everything into optval and then unlock and copy. If we ever
8941 * add a option that is not a int, then this will have quite an
8942 * impact to this routine.
8944 switch (sopt->sopt_name) {
8945 case TCP_RACK_PROP_RATE:
8946 optval = rack->r_ctl.rc_prop_rate;
8949 /* RACK proportional rate reduction (bool) */
8950 optval = rack->r_ctl.rc_prop_reduce;
8952 case TCP_RACK_TLP_REDUCE:
8953 /* RACK TLP cwnd reduction (bool) */
8954 optval = rack->r_ctl.rc_tlp_cwnd_reduce;
8956 case TCP_RACK_EARLY_RECOV:
8957 /* Should recovery happen early (bool) */
8958 optval = rack->r_ctl.rc_early_recovery;
8960 case TCP_RACK_PACE_REDUCE:
8961 /* RACK Hptsi reduction factor (divisor) */
8962 optval = rack->rc_pace_reduce;
8964 case TCP_RACK_PACE_MAX_SEG:
8965 /* Max segments in a pace */
8966 optval = rack->rc_pace_max_segs;
8968 case TCP_RACK_PACE_ALWAYS:
8969 /* Use the always pace method */
8970 optval = rack->rc_always_pace;
8972 case TCP_RACK_PRR_SENDALOT:
8973 /* Allow PRR to send more than one seg */
8974 optval = rack->r_ctl.rc_prr_sendalot;
8976 case TCP_RACK_MIN_TO:
8977 /* Minimum time between rack t-o's in ms */
8978 optval = rack->r_ctl.rc_min_to;
8980 case TCP_RACK_EARLY_SEG:
8981 /* If early recovery max segments */
8982 optval = rack->r_ctl.rc_early_recovery_segs;
8984 case TCP_RACK_REORD_THRESH:
8985 /* RACK reorder threshold (shift amount) */
8986 optval = rack->r_ctl.rc_reorder_shift;
8988 case TCP_RACK_REORD_FADE:
8989 /* Does reordering fade after ms time */
8990 optval = rack->r_ctl.rc_reorder_fade;
8992 case TCP_RACK_TLP_THRESH:
8993 /* RACK TLP theshold i.e. srtt+(srtt/N) */
8994 optval = rack->r_ctl.rc_tlp_threshold;
8996 case TCP_RACK_PKT_DELAY:
8997 /* RACK added ms i.e. rack-rtt + reord + N */
8998 optval = rack->r_ctl.rc_pkt_delay;
9000 case TCP_RACK_TLP_USE:
9001 optval = rack->rack_tlp_threshold_use;
9003 case TCP_RACK_TLP_INC_VAR:
9004 /* Does TLP include rtt variance in t-o */
9005 optval = rack->r_ctl.rc_prr_inc_var;
9007 case TCP_RACK_IDLE_REDUCE_HIGH:
9008 optval = rack->r_idle_reduce_largest;
9010 case TCP_RACK_MIN_PACE:
9011 optval = rack->r_enforce_min_pace;
9013 case TCP_RACK_MIN_PACE_SEG:
9014 optval = rack->r_min_pace_seg_thresh;
9016 case TCP_BBR_RACK_RTT_USE:
9017 optval = rack->r_ctl.rc_rate_sample_method;
9020 optval = tp->t_delayed_ack;
9022 case TCP_DATA_AFTER_CLOSE:
9023 optval = rack->rc_allow_data_af_clo;
9026 return (tcp_default_ctloutput(so, sopt, inp, tp));
9030 error = sooptcopyout(sopt, &optval, sizeof optval);
9035 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
9037 int32_t error = EINVAL;
9038 struct tcp_rack *rack;
9040 rack = (struct tcp_rack *)tp->t_fb_ptr;
9045 if (sopt->sopt_dir == SOPT_SET) {
9046 return (rack_set_sockopt(so, sopt, inp, tp, rack));
9047 } else if (sopt->sopt_dir == SOPT_GET) {
9048 return (rack_get_sockopt(so, sopt, inp, tp, rack));
9056 struct tcp_function_block __tcp_rack = {
9057 .tfb_tcp_block_name = __XSTRING(STACKNAME),
9058 .tfb_tcp_output = rack_output,
9059 .tfb_tcp_do_segment = rack_do_segment,
9060 .tfb_tcp_hpts_do_segment = rack_hpts_do_segment,
9061 .tfb_tcp_ctloutput = rack_ctloutput,
9062 .tfb_tcp_fb_init = rack_init,
9063 .tfb_tcp_fb_fini = rack_fini,
9064 .tfb_tcp_timer_stop_all = rack_stopall,
9065 .tfb_tcp_timer_activate = rack_timer_activate,
9066 .tfb_tcp_timer_active = rack_timer_active,
9067 .tfb_tcp_timer_stop = rack_timer_stop,
9068 .tfb_tcp_rexmit_tmr = rack_remxt_tmr,
9069 .tfb_tcp_handoff_ok = rack_handoff_ok
9072 static const char *rack_stack_names[] = {
9073 __XSTRING(STACKNAME),
9075 __XSTRING(STACKALIAS),
9080 rack_ctor(void *mem, int32_t size, void *arg, int32_t how)
9082 memset(mem, 0, size);
9087 rack_dtor(void *mem, int32_t size, void *arg)
9092 static bool rack_mod_inited = false;
9095 tcp_addrack(module_t mod, int32_t type, void *data)
9102 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map",
9103 sizeof(struct rack_sendmap),
9104 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
9106 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb",
9107 sizeof(struct tcp_rack),
9108 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
9110 sysctl_ctx_init(&rack_sysctl_ctx);
9111 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx,
9112 SYSCTL_STATIC_CHILDREN(_net_inet_tcp),
9114 __XSTRING(STACKNAME),
9117 if (rack_sysctl_root == NULL) {
9118 printf("Failed to add sysctl node\n");
9122 rack_init_sysctls();
9123 num_stacks = nitems(rack_stack_names);
9124 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK,
9125 rack_stack_names, &num_stacks);
9127 printf("Failed to register %s stack name for "
9128 "%s module\n", rack_stack_names[num_stacks],
9129 __XSTRING(MODNAME));
9130 sysctl_ctx_free(&rack_sysctl_ctx);
9132 uma_zdestroy(rack_zone);
9133 uma_zdestroy(rack_pcb_zone);
9134 rack_counter_destroy();
9135 printf("Failed to register rack module -- err:%d\n", err);
9138 rack_mod_inited = true;
9141 err = deregister_tcp_functions(&__tcp_rack, true, false);
9144 err = deregister_tcp_functions(&__tcp_rack, false, true);
9147 if (rack_mod_inited) {
9148 uma_zdestroy(rack_zone);
9149 uma_zdestroy(rack_pcb_zone);
9150 sysctl_ctx_free(&rack_sysctl_ctx);
9151 rack_counter_destroy();
9152 rack_mod_inited = false;
9157 return (EOPNOTSUPP);
9162 static moduledata_t tcp_rack = {
9163 .name = __XSTRING(MODNAME),
9164 .evhand = tcp_addrack,
9168 MODULE_VERSION(MODNAME, 1);
9169 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
9170 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1);