1 /*- 2 * Copyright (c) 2016-2018 Netflix, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_inet.h" 31 #include "opt_inet6.h" 32 #include "opt_ipsec.h" 33 #include "opt_tcpdebug.h" 34 35 #include <sys/param.h> 36 #include <sys/module.h> 37 #include <sys/kernel.h> 38 #ifdef TCP_HHOOK 39 #include <sys/hhook.h> 40 #endif 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/mbuf.h> 46 #include <sys/proc.h> /* for proc0 declaration */ 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 #ifdef NETFLIX_STATS 52 #include <sys/stats.h> 53 #endif 54 #include <sys/refcount.h> 55 #include <sys/queue.h> 56 #include <sys/smp.h> 57 #include <sys/kthread.h> 58 #include <sys/kern_prefetch.h> 59 60 #include <vm/uma.h> 61 62 #include <net/route.h> 63 #include <net/vnet.h> 64 65 #define TCPSTATES /* for logging */ 66 67 #include <netinet/in.h> 68 #include <netinet/in_kdtrace.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/ip.h> 71 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 72 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 73 #include <netinet/ip_var.h> 74 #include <netinet/ip6.h> 75 #include <netinet6/in6_pcb.h> 76 #include <netinet6/ip6_var.h> 77 #include <netinet/tcp.h> 78 #define TCPOUTFLAGS 79 #include <netinet/tcp_fsm.h> 80 #include <netinet/tcp_log_buf.h> 81 #include <netinet/tcp_seq.h> 82 #include <netinet/tcp_timer.h> 83 #include <netinet/tcp_var.h> 84 #include <netinet/tcp_hpts.h> 85 #include <netinet/tcpip.h> 86 #include <netinet/cc/cc.h> 87 #ifdef NETFLIX_CWV 88 #include <netinet/tcp_newcwv.h> 89 #endif 90 #include <netinet/tcp_fastopen.h> 91 #ifdef TCPDEBUG 92 #include <netinet/tcp_debug.h> 93 #endif /* TCPDEBUG */ 94 #ifdef TCP_OFFLOAD 95 #include <netinet/tcp_offload.h> 96 #endif 97 #ifdef INET6 98 #include <netinet6/tcp6_var.h> 99 #endif 100 101 #include <netipsec/ipsec_support.h> 102 103 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 104 #include <netipsec/ipsec.h> 105 #include <netipsec/ipsec6.h> 106 #endif /* IPSEC */ 107 108 #include <netinet/udp.h> 109 #include <netinet/udp_var.h> 110 #include <machine/in_cksum.h> 111 112 #ifdef MAC 113 #include <security/mac/mac_framework.h> 114 #endif 115 #include "sack_filter.h" 116 #include "tcp_rack.h" 117 #include "rack_bbr_common.h" 118 119 uma_zone_t rack_zone; 120 uma_zone_t rack_pcb_zone; 121 122 #ifndef TICKS2SBT 123 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t))) 124 #endif 125 126 struct sysctl_ctx_list rack_sysctl_ctx; 127 struct sysctl_oid *rack_sysctl_root; 128 129 #define CUM_ACKED 1 130 #define SACKED 2 131 132 /* 133 * The RACK module incorporates a number of 134 * TCP ideas that have been put out into the IETF 135 * over the last few years: 136 * - Matt Mathis's Rate Halving which slowly drops 137 * the congestion window so that the ack clock can 138 * be maintained during a recovery. 139 * - Yuchung Cheng's RACK TCP (for which its named) that 140 * will stop us using the number of dup acks and instead 141 * use time as the gage of when we retransmit. 142 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 143 * of Dukkipati et.al. 144 * RACK depends on SACK, so if an endpoint arrives that 145 * cannot do SACK the state machine below will shuttle the 146 * connection back to using the "default" TCP stack that is 147 * in FreeBSD. 148 * 149 * To implement RACK the original TCP stack was first decomposed 150 * into a functional state machine with individual states 151 * for each of the possible TCP connection states. The do_segement 152 * functions role in life is to mandate the connection supports SACK 153 * initially and then assure that the RACK state matches the conenction 154 * state before calling the states do_segment function. Each 155 * state is simplified due to the fact that the original do_segment 156 * has been decomposed and we *know* what state we are in (no 157 * switches on the state) and all tests for SACK are gone. This 158 * greatly simplifies what each state does. 159 * 160 * TCP output is also over-written with a new version since it 161 * must maintain the new rack scoreboard. 162 * 163 */ 164 static int32_t rack_precache = 1; 165 static int32_t rack_tlp_thresh = 1; 166 static int32_t rack_reorder_thresh = 2; 167 static int32_t rack_reorder_fade = 60000; /* 0 - never fade, def 60,000 168 * - 60 seconds */ 169 static int32_t rack_pkt_delay = 1; 170 static int32_t rack_inc_var = 0;/* For TLP */ 171 static int32_t rack_reduce_largest_on_idle = 0; 172 static int32_t rack_min_pace_time = 0; 173 static int32_t rack_min_pace_time_seg_req=6; 174 static int32_t rack_early_recovery = 1; 175 static int32_t rack_early_recovery_max_seg = 6; 176 static int32_t rack_send_a_lot_in_prr = 1; 177 static int32_t rack_min_to = 1; /* Number of ms minimum timeout */ 178 static int32_t rack_tlp_in_recovery = 1; /* Can we do TLP in recovery? */ 179 static int32_t rack_verbose_logging = 0; 180 static int32_t rack_ignore_data_after_close = 1; 181 /* 182 * Currently regular tcp has a rto_min of 30ms 183 * the backoff goes 12 times so that ends up 184 * being a total of 122.850 seconds before a 185 * connection is killed. 186 */ 187 static int32_t rack_tlp_min = 10; 188 static int32_t rack_rto_min = 30; /* 30ms same as main freebsd */ 189 static int32_t rack_rto_max = 30000; /* 30 seconds */ 190 static const int32_t rack_free_cache = 2; 191 static int32_t rack_hptsi_segments = 40; 192 static int32_t rack_rate_sample_method = USE_RTT_LOW; 193 static int32_t rack_pace_every_seg = 1; 194 static int32_t rack_delayed_ack_time = 200; /* 200ms */ 195 static int32_t rack_slot_reduction = 4; 196 static int32_t rack_lower_cwnd_at_tlp = 0; 197 static int32_t rack_use_proportional_reduce = 0; 198 static int32_t rack_proportional_rate = 10; 199 static int32_t rack_tlp_max_resend = 2; 200 static int32_t rack_limited_retran = 0; 201 static int32_t rack_always_send_oldest = 0; 202 static int32_t rack_sack_block_limit = 128; 203 static int32_t rack_use_sack_filter = 1; 204 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE; 205 206 /* Rack specific counters */ 207 counter_u64_t rack_badfr; 208 counter_u64_t rack_badfr_bytes; 209 counter_u64_t rack_rtm_prr_retran; 210 counter_u64_t rack_rtm_prr_newdata; 211 counter_u64_t rack_timestamp_mismatch; 212 counter_u64_t rack_reorder_seen; 213 counter_u64_t rack_paced_segments; 214 counter_u64_t rack_unpaced_segments; 215 counter_u64_t rack_saw_enobuf; 216 counter_u64_t rack_saw_enetunreach; 217 218 /* Tail loss probe counters */ 219 counter_u64_t rack_tlp_tot; 220 counter_u64_t rack_tlp_newdata; 221 counter_u64_t rack_tlp_retran; 222 counter_u64_t rack_tlp_retran_bytes; 223 counter_u64_t rack_tlp_retran_fail; 224 counter_u64_t rack_to_tot; 225 counter_u64_t rack_to_arm_rack; 226 counter_u64_t rack_to_arm_tlp; 227 counter_u64_t rack_to_alloc; 228 counter_u64_t rack_to_alloc_hard; 229 counter_u64_t rack_to_alloc_emerg; 230 231 counter_u64_t rack_sack_proc_all; 232 counter_u64_t rack_sack_proc_short; 233 counter_u64_t rack_sack_proc_restart; 234 counter_u64_t rack_runt_sacks; 235 counter_u64_t rack_used_tlpmethod; 236 counter_u64_t rack_used_tlpmethod2; 237 counter_u64_t rack_enter_tlp_calc; 238 counter_u64_t rack_input_idle_reduces; 239 counter_u64_t rack_tlp_does_nada; 240 241 /* Temp CPU counters */ 242 counter_u64_t rack_find_high; 243 244 counter_u64_t rack_progress_drops; 245 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE]; 246 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE]; 247 248 static void 249 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line); 250 251 static int 252 rack_process_ack(struct mbuf *m, struct tcphdr *th, 253 struct socket *so, struct tcpcb *tp, struct tcpopt *to, 254 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val); 255 static int 256 rack_process_data(struct mbuf *m, struct tcphdr *th, 257 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 258 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 259 static void 260 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, 261 struct tcphdr *th, uint16_t nsegs, uint16_t type, int32_t recovery); 262 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack); 263 static struct rack_sendmap * 264 rack_check_recovery_mode(struct tcpcb *tp, 265 uint32_t tsused); 266 static void 267 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th, 268 uint32_t type); 269 static void rack_counter_destroy(void); 270 static int 271 rack_ctloutput(struct socket *so, struct sockopt *sopt, 272 struct inpcb *inp, struct tcpcb *tp); 273 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how); 274 static void 275 rack_do_segment(struct mbuf *m, struct tcphdr *th, 276 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 277 uint8_t iptos); 278 static void rack_dtor(void *mem, int32_t size, void *arg); 279 static void 280 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm, 281 uint32_t t, uint32_t cts); 282 static struct rack_sendmap * 283 rack_find_high_nonack(struct tcp_rack *rack, 284 struct rack_sendmap *rsm); 285 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack); 286 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm); 287 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged); 288 static int 289 rack_get_sockopt(struct socket *so, struct sockopt *sopt, 290 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack); 291 static int32_t rack_handoff_ok(struct tcpcb *tp); 292 static int32_t rack_init(struct tcpcb *tp); 293 static void rack_init_sysctls(void); 294 static void 295 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, 296 struct tcphdr *th); 297 static void 298 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 299 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts, 300 uint8_t pass, struct rack_sendmap *hintrsm); 301 static void 302 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack, 303 struct rack_sendmap *rsm); 304 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num); 305 static int32_t rack_output(struct tcpcb *tp); 306 static void 307 rack_hpts_do_segment(struct mbuf *m, struct tcphdr *th, 308 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 309 uint8_t iptos, int32_t nxt_pkt, struct timeval *tv); 310 311 static uint32_t 312 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, 313 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm, 314 uint32_t cts); 315 static void rack_post_recovery(struct tcpcb *tp, struct tcphdr *th); 316 static void rack_remxt_tmr(struct tcpcb *tp); 317 static int 318 rack_set_sockopt(struct socket *so, struct sockopt *sopt, 319 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack); 320 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack); 321 static int32_t rack_stopall(struct tcpcb *tp); 322 static void 323 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, 324 uint32_t delta); 325 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type); 326 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line); 327 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type); 328 static uint32_t 329 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 330 struct rack_sendmap *rsm, uint32_t ts, int32_t * lenp); 331 static void 332 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 333 struct rack_sendmap *rsm, uint32_t ts); 334 static int 335 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 336 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type); 337 static int32_t tcp_addrack(module_t mod, int32_t type, void *data); 338 static void 339 rack_challenge_ack(struct mbuf *m, struct tcphdr *th, 340 struct tcpcb *tp, int32_t * ret_val); 341 static int 342 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, 343 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 344 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 345 static int 346 rack_do_closing(struct mbuf *m, struct tcphdr *th, 347 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 348 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 349 static void 350 rack_do_drop(struct mbuf *m, struct tcpcb *tp); 351 static void 352 rack_do_dropafterack(struct mbuf *m, struct tcpcb *tp, 353 struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t * ret_val); 354 static void 355 rack_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, 356 struct tcphdr *th, int32_t rstreason, int32_t tlen); 357 static int 358 rack_do_established(struct mbuf *m, struct tcphdr *th, 359 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 360 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 361 static int 362 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, 363 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 364 int32_t tlen, uint32_t tiwin, int32_t nxt_pkt); 365 static int 366 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, 367 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 368 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 369 static int 370 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, 371 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 372 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 373 static int 374 rack_do_lastack(struct mbuf *m, struct tcphdr *th, 375 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 376 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 377 static int 378 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, 379 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 380 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 381 static int 382 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, 383 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 384 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 385 static int 386 rack_drop_checks(struct tcpopt *to, struct mbuf *m, 387 struct tcphdr *th, struct tcpcb *tp, int32_t * tlenp, int32_t * thf, 388 int32_t * drop_hdrlen, int32_t * ret_val); 389 static int 390 rack_process_rst(struct mbuf *m, struct tcphdr *th, 391 struct socket *so, struct tcpcb *tp); 392 struct rack_sendmap * 393 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, 394 uint32_t tsused); 395 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt); 396 static void 397 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th); 398 399 static int 400 rack_ts_check(struct mbuf *m, struct tcphdr *th, 401 struct tcpcb *tp, int32_t tlen, int32_t thflags, int32_t * ret_val); 402 403 int32_t rack_clear_counter=0; 404 405 406 static int 407 sysctl_rack_clear(SYSCTL_HANDLER_ARGS) 408 { 409 uint32_t stat; 410 int32_t error; 411 412 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t)); 413 if (error || req->newptr == NULL) 414 return error; 415 416 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 417 if (error) 418 return (error); 419 if (stat == 1) { 420 #ifdef INVARIANTS 421 printf("Clearing RACK counters\n"); 422 #endif 423 counter_u64_zero(rack_badfr); 424 counter_u64_zero(rack_badfr_bytes); 425 counter_u64_zero(rack_rtm_prr_retran); 426 counter_u64_zero(rack_rtm_prr_newdata); 427 counter_u64_zero(rack_timestamp_mismatch); 428 counter_u64_zero(rack_reorder_seen); 429 counter_u64_zero(rack_tlp_tot); 430 counter_u64_zero(rack_tlp_newdata); 431 counter_u64_zero(rack_tlp_retran); 432 counter_u64_zero(rack_tlp_retran_bytes); 433 counter_u64_zero(rack_tlp_retran_fail); 434 counter_u64_zero(rack_to_tot); 435 counter_u64_zero(rack_to_arm_rack); 436 counter_u64_zero(rack_to_arm_tlp); 437 counter_u64_zero(rack_paced_segments); 438 counter_u64_zero(rack_unpaced_segments); 439 counter_u64_zero(rack_saw_enobuf); 440 counter_u64_zero(rack_saw_enetunreach); 441 counter_u64_zero(rack_to_alloc_hard); 442 counter_u64_zero(rack_to_alloc_emerg); 443 counter_u64_zero(rack_sack_proc_all); 444 counter_u64_zero(rack_sack_proc_short); 445 counter_u64_zero(rack_sack_proc_restart); 446 counter_u64_zero(rack_to_alloc); 447 counter_u64_zero(rack_find_high); 448 counter_u64_zero(rack_runt_sacks); 449 counter_u64_zero(rack_used_tlpmethod); 450 counter_u64_zero(rack_used_tlpmethod2); 451 counter_u64_zero(rack_enter_tlp_calc); 452 counter_u64_zero(rack_progress_drops); 453 counter_u64_zero(rack_tlp_does_nada); 454 } 455 rack_clear_counter = 0; 456 return (0); 457 } 458 459 460 461 static void 462 rack_init_sysctls() 463 { 464 SYSCTL_ADD_S32(&rack_sysctl_ctx, 465 SYSCTL_CHILDREN(rack_sysctl_root), 466 OID_AUTO, "rate_sample_method", CTLFLAG_RW, 467 &rack_rate_sample_method , USE_RTT_LOW, 468 "What method should we use for rate sampling 0=high, 1=low "); 469 SYSCTL_ADD_S32(&rack_sysctl_ctx, 470 SYSCTL_CHILDREN(rack_sysctl_root), 471 OID_AUTO, "data_after_close", CTLFLAG_RW, 472 &rack_ignore_data_after_close, 0, 473 "Do we hold off sending a RST until all pending data is ack'd"); 474 SYSCTL_ADD_S32(&rack_sysctl_ctx, 475 SYSCTL_CHILDREN(rack_sysctl_root), 476 OID_AUTO, "tlpmethod", CTLFLAG_RW, 477 &rack_tlp_threshold_use, TLP_USE_TWO_ONE, 478 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2"); 479 SYSCTL_ADD_S32(&rack_sysctl_ctx, 480 SYSCTL_CHILDREN(rack_sysctl_root), 481 OID_AUTO, "min_pace_time", CTLFLAG_RW, 482 &rack_min_pace_time, 0, 483 "Should we enforce a minimum pace time of 1ms"); 484 SYSCTL_ADD_S32(&rack_sysctl_ctx, 485 SYSCTL_CHILDREN(rack_sysctl_root), 486 OID_AUTO, "min_pace_segs", CTLFLAG_RW, 487 &rack_min_pace_time_seg_req, 6, 488 "How many segments have to be in the len to enforce min-pace-time"); 489 SYSCTL_ADD_S32(&rack_sysctl_ctx, 490 SYSCTL_CHILDREN(rack_sysctl_root), 491 OID_AUTO, "idle_reduce_high", CTLFLAG_RW, 492 &rack_reduce_largest_on_idle, 0, 493 "Should we reduce the largest cwnd seen to IW on idle reduction"); 494 SYSCTL_ADD_S32(&rack_sysctl_ctx, 495 SYSCTL_CHILDREN(rack_sysctl_root), 496 OID_AUTO, "bb_verbose", CTLFLAG_RW, 497 &rack_verbose_logging, 0, 498 "Should RACK black box logging be verbose"); 499 SYSCTL_ADD_S32(&rack_sysctl_ctx, 500 SYSCTL_CHILDREN(rack_sysctl_root), 501 OID_AUTO, "sackfiltering", CTLFLAG_RW, 502 &rack_use_sack_filter, 1, 503 "Do we use sack filtering?"); 504 SYSCTL_ADD_S32(&rack_sysctl_ctx, 505 SYSCTL_CHILDREN(rack_sysctl_root), 506 OID_AUTO, "delayed_ack", CTLFLAG_RW, 507 &rack_delayed_ack_time, 200, 508 "Delayed ack time (200ms)"); 509 SYSCTL_ADD_S32(&rack_sysctl_ctx, 510 SYSCTL_CHILDREN(rack_sysctl_root), 511 OID_AUTO, "tlpminto", CTLFLAG_RW, 512 &rack_tlp_min, 10, 513 "TLP minimum timeout per the specification (10ms)"); 514 SYSCTL_ADD_S32(&rack_sysctl_ctx, 515 SYSCTL_CHILDREN(rack_sysctl_root), 516 OID_AUTO, "precache", CTLFLAG_RW, 517 &rack_precache, 0, 518 "Where should we precache the mcopy (0 is not at all)"); 519 SYSCTL_ADD_S32(&rack_sysctl_ctx, 520 SYSCTL_CHILDREN(rack_sysctl_root), 521 OID_AUTO, "sblklimit", CTLFLAG_RW, 522 &rack_sack_block_limit, 128, 523 "When do we start paying attention to small sack blocks"); 524 SYSCTL_ADD_S32(&rack_sysctl_ctx, 525 SYSCTL_CHILDREN(rack_sysctl_root), 526 OID_AUTO, "send_oldest", CTLFLAG_RW, 527 &rack_always_send_oldest, 1, 528 "Should we always send the oldest TLP and RACK-TLP"); 529 SYSCTL_ADD_S32(&rack_sysctl_ctx, 530 SYSCTL_CHILDREN(rack_sysctl_root), 531 OID_AUTO, "rack_tlp_in_recovery", CTLFLAG_RW, 532 &rack_tlp_in_recovery, 1, 533 "Can we do a TLP during recovery?"); 534 SYSCTL_ADD_S32(&rack_sysctl_ctx, 535 SYSCTL_CHILDREN(rack_sysctl_root), 536 OID_AUTO, "rack_tlimit", CTLFLAG_RW, 537 &rack_limited_retran, 0, 538 "How many times can a rack timeout drive out sends"); 539 SYSCTL_ADD_S32(&rack_sysctl_ctx, 540 SYSCTL_CHILDREN(rack_sysctl_root), 541 OID_AUTO, "minrto", CTLFLAG_RW, 542 &rack_rto_min, 0, 543 "Minimum RTO in ms -- set with caution below 1000 due to TLP"); 544 SYSCTL_ADD_S32(&rack_sysctl_ctx, 545 SYSCTL_CHILDREN(rack_sysctl_root), 546 OID_AUTO, "maxrto", CTLFLAG_RW, 547 &rack_rto_max, 0, 548 "Maxiumum RTO in ms -- should be at least as large as min_rto"); 549 SYSCTL_ADD_S32(&rack_sysctl_ctx, 550 SYSCTL_CHILDREN(rack_sysctl_root), 551 OID_AUTO, "tlp_retry", CTLFLAG_RW, 552 &rack_tlp_max_resend, 2, 553 "How many times does TLP retry a single segment or multiple with no ACK"); 554 SYSCTL_ADD_S32(&rack_sysctl_ctx, 555 SYSCTL_CHILDREN(rack_sysctl_root), 556 OID_AUTO, "recovery_loss_prop", CTLFLAG_RW, 557 &rack_use_proportional_reduce, 0, 558 "Should we proportionaly reduce cwnd based on the number of losses "); 559 SYSCTL_ADD_S32(&rack_sysctl_ctx, 560 SYSCTL_CHILDREN(rack_sysctl_root), 561 OID_AUTO, "recovery_prop", CTLFLAG_RW, 562 &rack_proportional_rate, 10, 563 "What percent reduction per loss"); 564 SYSCTL_ADD_S32(&rack_sysctl_ctx, 565 SYSCTL_CHILDREN(rack_sysctl_root), 566 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW, 567 &rack_lower_cwnd_at_tlp, 0, 568 "When a TLP completes a retran should we enter recovery?"); 569 SYSCTL_ADD_S32(&rack_sysctl_ctx, 570 SYSCTL_CHILDREN(rack_sysctl_root), 571 OID_AUTO, "hptsi_reduces", CTLFLAG_RW, 572 &rack_slot_reduction, 4, 573 "When setting a slot should we reduce by divisor"); 574 SYSCTL_ADD_S32(&rack_sysctl_ctx, 575 SYSCTL_CHILDREN(rack_sysctl_root), 576 OID_AUTO, "hptsi_every_seg", CTLFLAG_RW, 577 &rack_pace_every_seg, 1, 578 "Should we pace out every segment hptsi"); 579 SYSCTL_ADD_S32(&rack_sysctl_ctx, 580 SYSCTL_CHILDREN(rack_sysctl_root), 581 OID_AUTO, "hptsi_seg_max", CTLFLAG_RW, 582 &rack_hptsi_segments, 6, 583 "Should we pace out only a limited size of segments"); 584 SYSCTL_ADD_S32(&rack_sysctl_ctx, 585 SYSCTL_CHILDREN(rack_sysctl_root), 586 OID_AUTO, "prr_sendalot", CTLFLAG_RW, 587 &rack_send_a_lot_in_prr, 1, 588 "Send a lot in prr"); 589 SYSCTL_ADD_S32(&rack_sysctl_ctx, 590 SYSCTL_CHILDREN(rack_sysctl_root), 591 OID_AUTO, "minto", CTLFLAG_RW, 592 &rack_min_to, 1, 593 "Minimum rack timeout in milliseconds"); 594 SYSCTL_ADD_S32(&rack_sysctl_ctx, 595 SYSCTL_CHILDREN(rack_sysctl_root), 596 OID_AUTO, "earlyrecoveryseg", CTLFLAG_RW, 597 &rack_early_recovery_max_seg, 6, 598 "Max segments in early recovery"); 599 SYSCTL_ADD_S32(&rack_sysctl_ctx, 600 SYSCTL_CHILDREN(rack_sysctl_root), 601 OID_AUTO, "earlyrecovery", CTLFLAG_RW, 602 &rack_early_recovery, 1, 603 "Do we do early recovery with rack"); 604 SYSCTL_ADD_S32(&rack_sysctl_ctx, 605 SYSCTL_CHILDREN(rack_sysctl_root), 606 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 607 &rack_reorder_thresh, 2, 608 "What factor for rack will be added when seeing reordering (shift right)"); 609 SYSCTL_ADD_S32(&rack_sysctl_ctx, 610 SYSCTL_CHILDREN(rack_sysctl_root), 611 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 612 &rack_tlp_thresh, 1, 613 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 614 SYSCTL_ADD_S32(&rack_sysctl_ctx, 615 SYSCTL_CHILDREN(rack_sysctl_root), 616 OID_AUTO, "reorder_fade", CTLFLAG_RW, 617 &rack_reorder_fade, 0, 618 "Does reorder detection fade, if so how many ms (0 means never)"); 619 SYSCTL_ADD_S32(&rack_sysctl_ctx, 620 SYSCTL_CHILDREN(rack_sysctl_root), 621 OID_AUTO, "pktdelay", CTLFLAG_RW, 622 &rack_pkt_delay, 1, 623 "Extra RACK time (in ms) besides reordering thresh"); 624 SYSCTL_ADD_S32(&rack_sysctl_ctx, 625 SYSCTL_CHILDREN(rack_sysctl_root), 626 OID_AUTO, "inc_var", CTLFLAG_RW, 627 &rack_inc_var, 0, 628 "Should rack add to the TLP timer the variance in rtt calculation"); 629 rack_badfr = counter_u64_alloc(M_WAITOK); 630 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 631 SYSCTL_CHILDREN(rack_sysctl_root), 632 OID_AUTO, "badfr", CTLFLAG_RD, 633 &rack_badfr, "Total number of bad FRs"); 634 rack_badfr_bytes = counter_u64_alloc(M_WAITOK); 635 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 636 SYSCTL_CHILDREN(rack_sysctl_root), 637 OID_AUTO, "badfr_bytes", CTLFLAG_RD, 638 &rack_badfr_bytes, "Total number of bad FRs"); 639 rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK); 640 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 641 SYSCTL_CHILDREN(rack_sysctl_root), 642 OID_AUTO, "prrsndret", CTLFLAG_RD, 643 &rack_rtm_prr_retran, 644 "Total number of prr based retransmits"); 645 rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK); 646 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 647 SYSCTL_CHILDREN(rack_sysctl_root), 648 OID_AUTO, "prrsndnew", CTLFLAG_RD, 649 &rack_rtm_prr_newdata, 650 "Total number of prr based new transmits"); 651 rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK); 652 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 653 SYSCTL_CHILDREN(rack_sysctl_root), 654 OID_AUTO, "tsnf", CTLFLAG_RD, 655 &rack_timestamp_mismatch, 656 "Total number of timestamps that we could not find the reported ts"); 657 rack_find_high = counter_u64_alloc(M_WAITOK); 658 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 659 SYSCTL_CHILDREN(rack_sysctl_root), 660 OID_AUTO, "findhigh", CTLFLAG_RD, 661 &rack_find_high, 662 "Total number of FIN causing find-high"); 663 rack_reorder_seen = counter_u64_alloc(M_WAITOK); 664 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 665 SYSCTL_CHILDREN(rack_sysctl_root), 666 OID_AUTO, "reordering", CTLFLAG_RD, 667 &rack_reorder_seen, 668 "Total number of times we added delay due to reordering"); 669 rack_tlp_tot = counter_u64_alloc(M_WAITOK); 670 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 671 SYSCTL_CHILDREN(rack_sysctl_root), 672 OID_AUTO, "tlp_to_total", CTLFLAG_RD, 673 &rack_tlp_tot, 674 "Total number of tail loss probe expirations"); 675 rack_tlp_newdata = counter_u64_alloc(M_WAITOK); 676 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 677 SYSCTL_CHILDREN(rack_sysctl_root), 678 OID_AUTO, "tlp_new", CTLFLAG_RD, 679 &rack_tlp_newdata, 680 "Total number of tail loss probe sending new data"); 681 682 rack_tlp_retran = counter_u64_alloc(M_WAITOK); 683 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 684 SYSCTL_CHILDREN(rack_sysctl_root), 685 OID_AUTO, "tlp_retran", CTLFLAG_RD, 686 &rack_tlp_retran, 687 "Total number of tail loss probe sending retransmitted data"); 688 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK); 689 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 690 SYSCTL_CHILDREN(rack_sysctl_root), 691 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD, 692 &rack_tlp_retran_bytes, 693 "Total bytes of tail loss probe sending retransmitted data"); 694 rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK); 695 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 696 SYSCTL_CHILDREN(rack_sysctl_root), 697 OID_AUTO, "tlp_retran_fail", CTLFLAG_RD, 698 &rack_tlp_retran_fail, 699 "Total number of tail loss probe sending retransmitted data that failed (wait for t3)"); 700 rack_to_tot = counter_u64_alloc(M_WAITOK); 701 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 702 SYSCTL_CHILDREN(rack_sysctl_root), 703 OID_AUTO, "rack_to_tot", CTLFLAG_RD, 704 &rack_to_tot, 705 "Total number of times the rack to expired?"); 706 rack_to_arm_rack = counter_u64_alloc(M_WAITOK); 707 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 708 SYSCTL_CHILDREN(rack_sysctl_root), 709 OID_AUTO, "arm_rack", CTLFLAG_RD, 710 &rack_to_arm_rack, 711 "Total number of times the rack timer armed?"); 712 rack_to_arm_tlp = counter_u64_alloc(M_WAITOK); 713 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 714 SYSCTL_CHILDREN(rack_sysctl_root), 715 OID_AUTO, "arm_tlp", CTLFLAG_RD, 716 &rack_to_arm_tlp, 717 "Total number of times the tlp timer armed?"); 718 rack_paced_segments = counter_u64_alloc(M_WAITOK); 719 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 720 SYSCTL_CHILDREN(rack_sysctl_root), 721 OID_AUTO, "paced", CTLFLAG_RD, 722 &rack_paced_segments, 723 "Total number of times a segment send caused hptsi"); 724 rack_unpaced_segments = counter_u64_alloc(M_WAITOK); 725 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 726 SYSCTL_CHILDREN(rack_sysctl_root), 727 OID_AUTO, "unpaced", CTLFLAG_RD, 728 &rack_unpaced_segments, 729 "Total number of times a segment did not cause hptsi"); 730 rack_saw_enobuf = counter_u64_alloc(M_WAITOK); 731 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 732 SYSCTL_CHILDREN(rack_sysctl_root), 733 OID_AUTO, "saw_enobufs", CTLFLAG_RD, 734 &rack_saw_enobuf, 735 "Total number of times a segment did not cause hptsi"); 736 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK); 737 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 738 SYSCTL_CHILDREN(rack_sysctl_root), 739 OID_AUTO, "saw_enetunreach", CTLFLAG_RD, 740 &rack_saw_enetunreach, 741 "Total number of times a segment did not cause hptsi"); 742 rack_to_alloc = counter_u64_alloc(M_WAITOK); 743 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 744 SYSCTL_CHILDREN(rack_sysctl_root), 745 OID_AUTO, "allocs", CTLFLAG_RD, 746 &rack_to_alloc, 747 "Total allocations of tracking structures"); 748 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK); 749 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 750 SYSCTL_CHILDREN(rack_sysctl_root), 751 OID_AUTO, "allochard", CTLFLAG_RD, 752 &rack_to_alloc_hard, 753 "Total allocations done with sleeping the hard way"); 754 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK); 755 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 756 SYSCTL_CHILDREN(rack_sysctl_root), 757 OID_AUTO, "allocemerg", CTLFLAG_RD, 758 &rack_to_alloc_emerg, 759 "Total alocations done from emergency cache"); 760 rack_sack_proc_all = counter_u64_alloc(M_WAITOK); 761 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 762 SYSCTL_CHILDREN(rack_sysctl_root), 763 OID_AUTO, "sack_long", CTLFLAG_RD, 764 &rack_sack_proc_all, 765 "Total times we had to walk whole list for sack processing"); 766 767 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK); 768 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 769 SYSCTL_CHILDREN(rack_sysctl_root), 770 OID_AUTO, "sack_restart", CTLFLAG_RD, 771 &rack_sack_proc_restart, 772 "Total times we had to walk whole list due to a restart"); 773 rack_sack_proc_short = counter_u64_alloc(M_WAITOK); 774 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 775 SYSCTL_CHILDREN(rack_sysctl_root), 776 OID_AUTO, "sack_short", CTLFLAG_RD, 777 &rack_sack_proc_short, 778 "Total times we took shortcut for sack processing"); 779 rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK); 780 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 781 SYSCTL_CHILDREN(rack_sysctl_root), 782 OID_AUTO, "tlp_calc_entered", CTLFLAG_RD, 783 &rack_enter_tlp_calc, 784 "Total times we called calc-tlp"); 785 rack_used_tlpmethod = counter_u64_alloc(M_WAITOK); 786 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 787 SYSCTL_CHILDREN(rack_sysctl_root), 788 OID_AUTO, "hit_tlp_method", CTLFLAG_RD, 789 &rack_used_tlpmethod, 790 "Total number of runt sacks"); 791 rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK); 792 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 793 SYSCTL_CHILDREN(rack_sysctl_root), 794 OID_AUTO, "hit_tlp_method2", CTLFLAG_RD, 795 &rack_used_tlpmethod2, 796 "Total number of runt sacks 2"); 797 rack_runt_sacks = counter_u64_alloc(M_WAITOK); 798 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 799 SYSCTL_CHILDREN(rack_sysctl_root), 800 OID_AUTO, "runtsacks", CTLFLAG_RD, 801 &rack_runt_sacks, 802 "Total number of runt sacks"); 803 rack_progress_drops = counter_u64_alloc(M_WAITOK); 804 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 805 SYSCTL_CHILDREN(rack_sysctl_root), 806 OID_AUTO, "prog_drops", CTLFLAG_RD, 807 &rack_progress_drops, 808 "Total number of progress drops"); 809 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK); 810 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 811 SYSCTL_CHILDREN(rack_sysctl_root), 812 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD, 813 &rack_input_idle_reduces, 814 "Total number of idle reductions on input"); 815 rack_tlp_does_nada = counter_u64_alloc(M_WAITOK); 816 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 817 SYSCTL_CHILDREN(rack_sysctl_root), 818 OID_AUTO, "tlp_nada", CTLFLAG_RD, 819 &rack_tlp_does_nada, 820 "Total number of nada tlp calls"); 821 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 822 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 823 OID_AUTO, "outsize", CTLFLAG_RD, 824 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes"); 825 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK); 826 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 827 OID_AUTO, "opts", CTLFLAG_RD, 828 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats"); 829 SYSCTL_ADD_PROC(&rack_sysctl_ctx, 830 SYSCTL_CHILDREN(rack_sysctl_root), 831 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 832 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters"); 833 } 834 835 static inline int32_t 836 rack_progress_timeout_check(struct tcpcb *tp) 837 { 838 if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) { 839 if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) { 840 /* 841 * There is an assumption that the caller 842 * will drop the connection so we will 843 * increment the counters here. 844 */ 845 struct tcp_rack *rack; 846 rack = (struct tcp_rack *)tp->t_fb_ptr; 847 counter_u64_add(rack_progress_drops, 1); 848 #ifdef NETFLIX_STATS 849 TCPSTAT_INC(tcps_progdrops); 850 #endif 851 rack_log_progress_event(rack, tp, ticks, PROGRESS_DROP, __LINE__); 852 return (1); 853 } 854 } 855 return (0); 856 } 857 858 859 static void 860 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 861 { 862 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 863 union tcp_log_stackspecific log; 864 865 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 866 log.u_bbr.flex1 = TICKS_2_MSEC(rack->rc_tp->t_srtt >> TCP_RTT_SHIFT); 867 log.u_bbr.flex2 = to; 868 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags; 869 log.u_bbr.flex4 = slot; 870 log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot; 871 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 872 log.u_bbr.flex8 = which; 873 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 874 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 875 TCP_LOG_EVENT(rack->rc_tp, NULL, 876 &rack->rc_inp->inp_socket->so_rcv, 877 &rack->rc_inp->inp_socket->so_snd, 878 BBR_LOG_TIMERSTAR, 0, 879 0, &log, false); 880 } 881 } 882 883 static void 884 rack_log_to_event(struct tcp_rack *rack, int32_t to_num) 885 { 886 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 887 union tcp_log_stackspecific log; 888 889 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 890 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 891 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 892 log.u_bbr.flex8 = to_num; 893 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt; 894 log.u_bbr.flex2 = rack->rc_rack_rtt; 895 TCP_LOG_EVENT(rack->rc_tp, NULL, 896 &rack->rc_inp->inp_socket->so_rcv, 897 &rack->rc_inp->inp_socket->so_snd, 898 BBR_LOG_RTO, 0, 899 0, &log, false); 900 } 901 } 902 903 static void 904 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, int32_t t, 905 uint32_t o_srtt, uint32_t o_var) 906 { 907 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 908 union tcp_log_stackspecific log; 909 910 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 911 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 912 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 913 log.u_bbr.flex1 = t; 914 log.u_bbr.flex2 = o_srtt; 915 log.u_bbr.flex3 = o_var; 916 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest; 917 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest; 918 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_rtt_cnt; 919 log.u_bbr.rttProp = rack->r_ctl.rack_rs.rs_rtt_tot; 920 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method; 921 TCP_LOG_EVENT(tp, NULL, 922 &rack->rc_inp->inp_socket->so_rcv, 923 &rack->rc_inp->inp_socket->so_snd, 924 BBR_LOG_BBRRTT, 0, 925 0, &log, false); 926 } 927 } 928 929 static void 930 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt) 931 { 932 /* 933 * Log the rtt sample we are 934 * applying to the srtt algorithm in 935 * useconds. 936 */ 937 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 938 union tcp_log_stackspecific log; 939 struct timeval tv; 940 941 /* Convert our ms to a microsecond */ 942 log.u_bbr.flex1 = rtt * 1000; 943 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 944 TCP_LOG_EVENTP(rack->rc_tp, NULL, 945 &rack->rc_inp->inp_socket->so_rcv, 946 &rack->rc_inp->inp_socket->so_snd, 947 TCP_LOG_RTT, 0, 948 0, &log, false, &tv); 949 } 950 } 951 952 953 static inline void 954 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line) 955 { 956 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) { 957 union tcp_log_stackspecific log; 958 959 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 960 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 961 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 962 log.u_bbr.flex1 = line; 963 log.u_bbr.flex2 = tick; 964 log.u_bbr.flex3 = tp->t_maxunacktime; 965 log.u_bbr.flex4 = tp->t_acktime; 966 log.u_bbr.flex8 = event; 967 TCP_LOG_EVENT(tp, NULL, 968 &rack->rc_inp->inp_socket->so_rcv, 969 &rack->rc_inp->inp_socket->so_snd, 970 BBR_LOG_PROGRESS, 0, 971 0, &log, false); 972 } 973 } 974 975 static void 976 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts) 977 { 978 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 979 union tcp_log_stackspecific log; 980 981 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 982 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 983 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 984 log.u_bbr.flex1 = slot; 985 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags); 986 log.u_bbr.flex8 = rack->rc_in_persist; 987 TCP_LOG_EVENT(rack->rc_tp, NULL, 988 &rack->rc_inp->inp_socket->so_rcv, 989 &rack->rc_inp->inp_socket->so_snd, 990 BBR_LOG_BBRSND, 0, 991 0, &log, false); 992 } 993 } 994 995 static void 996 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out) 997 { 998 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 999 union tcp_log_stackspecific log; 1000 log.u_bbr.flex1 = did_out; 1001 log.u_bbr.flex2 = nxt_pkt; 1002 log.u_bbr.flex3 = way_out; 1003 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 1004 log.u_bbr.flex7 = rack->r_wanted_output; 1005 log.u_bbr.flex8 = rack->rc_in_persist; 1006 TCP_LOG_EVENT(rack->rc_tp, NULL, 1007 &rack->rc_inp->inp_socket->so_rcv, 1008 &rack->rc_inp->inp_socket->so_snd, 1009 BBR_LOG_DOSEG_DONE, 0, 1010 0, &log, false); 1011 } 1012 } 1013 1014 1015 static void 1016 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot, uint8_t hpts_calling) 1017 { 1018 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 1019 union tcp_log_stackspecific log; 1020 1021 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 1022 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 1023 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 1024 log.u_bbr.flex1 = slot; 1025 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags; 1026 log.u_bbr.flex7 = hpts_calling; 1027 log.u_bbr.flex8 = rack->rc_in_persist; 1028 TCP_LOG_EVENT(rack->rc_tp, NULL, 1029 &rack->rc_inp->inp_socket->so_rcv, 1030 &rack->rc_inp->inp_socket->so_snd, 1031 BBR_LOG_JUSTRET, 0, 1032 tlen, &log, false); 1033 } 1034 } 1035 1036 static void 1037 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line) 1038 { 1039 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 1040 union tcp_log_stackspecific log; 1041 1042 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 1043 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 1044 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 1045 log.u_bbr.flex1 = line; 1046 log.u_bbr.flex2 = 0; 1047 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags; 1048 log.u_bbr.flex4 = 0; 1049 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 1050 log.u_bbr.flex8 = hpts_removed; 1051 TCP_LOG_EVENT(rack->rc_tp, NULL, 1052 &rack->rc_inp->inp_socket->so_rcv, 1053 &rack->rc_inp->inp_socket->so_snd, 1054 BBR_LOG_TIMERCANC, 0, 1055 0, &log, false); 1056 } 1057 } 1058 1059 static void 1060 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers) 1061 { 1062 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 1063 union tcp_log_stackspecific log; 1064 1065 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 1066 log.u_bbr.flex1 = timers; 1067 log.u_bbr.flex2 = ret; 1068 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp; 1069 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 1070 log.u_bbr.flex5 = cts; 1071 TCP_LOG_EVENT(rack->rc_tp, NULL, 1072 &rack->rc_inp->inp_socket->so_rcv, 1073 &rack->rc_inp->inp_socket->so_snd, 1074 BBR_LOG_TO_PROCESS, 0, 1075 0, &log, false); 1076 } 1077 } 1078 1079 static void 1080 rack_counter_destroy() 1081 { 1082 counter_u64_free(rack_badfr); 1083 counter_u64_free(rack_badfr_bytes); 1084 counter_u64_free(rack_rtm_prr_retran); 1085 counter_u64_free(rack_rtm_prr_newdata); 1086 counter_u64_free(rack_timestamp_mismatch); 1087 counter_u64_free(rack_reorder_seen); 1088 counter_u64_free(rack_tlp_tot); 1089 counter_u64_free(rack_tlp_newdata); 1090 counter_u64_free(rack_tlp_retran); 1091 counter_u64_free(rack_tlp_retran_bytes); 1092 counter_u64_free(rack_tlp_retran_fail); 1093 counter_u64_free(rack_to_tot); 1094 counter_u64_free(rack_to_arm_rack); 1095 counter_u64_free(rack_to_arm_tlp); 1096 counter_u64_free(rack_paced_segments); 1097 counter_u64_free(rack_unpaced_segments); 1098 counter_u64_free(rack_saw_enobuf); 1099 counter_u64_free(rack_saw_enetunreach); 1100 counter_u64_free(rack_to_alloc_hard); 1101 counter_u64_free(rack_to_alloc_emerg); 1102 counter_u64_free(rack_sack_proc_all); 1103 counter_u64_free(rack_sack_proc_short); 1104 counter_u64_free(rack_sack_proc_restart); 1105 counter_u64_free(rack_to_alloc); 1106 counter_u64_free(rack_find_high); 1107 counter_u64_free(rack_runt_sacks); 1108 counter_u64_free(rack_enter_tlp_calc); 1109 counter_u64_free(rack_used_tlpmethod); 1110 counter_u64_free(rack_used_tlpmethod2); 1111 counter_u64_free(rack_progress_drops); 1112 counter_u64_free(rack_input_idle_reduces); 1113 counter_u64_free(rack_tlp_does_nada); 1114 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE); 1115 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE); 1116 } 1117 1118 static struct rack_sendmap * 1119 rack_alloc(struct tcp_rack *rack) 1120 { 1121 struct rack_sendmap *rsm; 1122 1123 counter_u64_add(rack_to_alloc, 1); 1124 rack->r_ctl.rc_num_maps_alloced++; 1125 rsm = uma_zalloc(rack_zone, M_NOWAIT); 1126 if (rsm) { 1127 return (rsm); 1128 } 1129 if (rack->rc_free_cnt) { 1130 counter_u64_add(rack_to_alloc_emerg, 1); 1131 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 1132 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_next); 1133 rack->rc_free_cnt--; 1134 return (rsm); 1135 } 1136 return (NULL); 1137 } 1138 1139 static void 1140 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm) 1141 { 1142 rack->r_ctl.rc_num_maps_alloced--; 1143 if (rack->r_ctl.rc_tlpsend == rsm) 1144 rack->r_ctl.rc_tlpsend = NULL; 1145 if (rack->r_ctl.rc_next == rsm) 1146 rack->r_ctl.rc_next = NULL; 1147 if (rack->r_ctl.rc_sacklast == rsm) 1148 rack->r_ctl.rc_sacklast = NULL; 1149 if (rack->rc_free_cnt < rack_free_cache) { 1150 memset(rsm, 0, sizeof(struct rack_sendmap)); 1151 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_next); 1152 rack->rc_free_cnt++; 1153 return; 1154 } 1155 uma_zfree(rack_zone, rsm); 1156 } 1157 1158 /* 1159 * CC wrapper hook functions 1160 */ 1161 static void 1162 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, struct tcphdr *th, uint16_t nsegs, 1163 uint16_t type, int32_t recovery) 1164 { 1165 #ifdef NETFLIX_STATS 1166 int32_t gput; 1167 #endif 1168 #ifdef NETFLIX_CWV 1169 u_long old_cwnd = tp->snd_cwnd; 1170 #endif 1171 1172 INP_WLOCK_ASSERT(tp->t_inpcb); 1173 tp->ccv->nsegs = nsegs; 1174 tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 1175 if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) { 1176 uint32_t max; 1177 1178 max = rack->r_ctl.rc_early_recovery_segs * tp->t_maxseg; 1179 if (tp->ccv->bytes_this_ack > max) { 1180 tp->ccv->bytes_this_ack = max; 1181 } 1182 } 1183 if (tp->snd_cwnd <= tp->snd_wnd) 1184 tp->ccv->flags |= CCF_CWND_LIMITED; 1185 else 1186 tp->ccv->flags &= ~CCF_CWND_LIMITED; 1187 1188 if (type == CC_ACK) { 1189 #ifdef NETFLIX_STATS 1190 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 1191 ((int32_t) tp->snd_cwnd) - tp->snd_wnd); 1192 if ((tp->t_flags & TF_GPUTINPROG) && 1193 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 1194 gput = (((int64_t) (th->th_ack - tp->gput_seq)) << 3) / 1195 max(1, tcp_ts_getticks() - tp->gput_ts); 1196 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 1197 gput); 1198 /* 1199 * XXXLAS: This is a temporary hack, and should be 1200 * chained off VOI_TCP_GPUT when stats(9) grows an 1201 * API to deal with chained VOIs. 1202 */ 1203 if (tp->t_stats_gput_prev > 0) 1204 stats_voi_update_abs_s32(tp->t_stats, 1205 VOI_TCP_GPUT_ND, 1206 ((gput - tp->t_stats_gput_prev) * 100) / 1207 tp->t_stats_gput_prev); 1208 tp->t_flags &= ~TF_GPUTINPROG; 1209 tp->t_stats_gput_prev = gput; 1210 #ifdef NETFLIX_CWV 1211 if (tp->t_maxpeakrate) { 1212 /* 1213 * We update t_peakrate_thr. This gives us roughly 1214 * one update per round trip time. 1215 */ 1216 tcp_update_peakrate_thr(tp); 1217 } 1218 #endif 1219 } 1220 #endif 1221 if (tp->snd_cwnd > tp->snd_ssthresh) { 1222 tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 1223 nsegs * V_tcp_abc_l_var * tp->t_maxseg); 1224 if (tp->t_bytes_acked >= tp->snd_cwnd) { 1225 tp->t_bytes_acked -= tp->snd_cwnd; 1226 tp->ccv->flags |= CCF_ABC_SENTAWND; 1227 } 1228 } else { 1229 tp->ccv->flags &= ~CCF_ABC_SENTAWND; 1230 tp->t_bytes_acked = 0; 1231 } 1232 } 1233 if (CC_ALGO(tp)->ack_received != NULL) { 1234 /* XXXLAS: Find a way to live without this */ 1235 tp->ccv->curack = th->th_ack; 1236 CC_ALGO(tp)->ack_received(tp->ccv, type); 1237 } 1238 #ifdef NETFLIX_STATS 1239 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd); 1240 #endif 1241 if (rack->r_ctl.rc_rack_largest_cwnd < tp->snd_cwnd) { 1242 rack->r_ctl.rc_rack_largest_cwnd = tp->snd_cwnd; 1243 } 1244 #ifdef NETFLIX_CWV 1245 if (tp->cwv_enabled) { 1246 /* 1247 * Per RFC 7661: The behaviour in the non-validated phase is 1248 * specified as: o A sender determines whether to increase 1249 * the cwnd based upon whether it is cwnd-limited (see 1250 * Section 4.5.3): * A sender that is cwnd-limited MAY use 1251 * the standard TCP method to increase cwnd (i.e., the 1252 * standard method permits a TCP sender that fully utilises 1253 * the cwnd to increase the cwnd each time it receives an 1254 * ACK). * A sender that is not cwnd-limited MUST NOT 1255 * increase the cwnd when ACK packets are received in this 1256 * phase (i.e., needs to avoid growing the cwnd when it has 1257 * not recently sent using the current size of cwnd). 1258 */ 1259 if ((tp->snd_cwnd > old_cwnd) && 1260 (tp->cwv_cwnd_valid == 0) && 1261 (!(tp->ccv->flags & CCF_CWND_LIMITED))) { 1262 tp->snd_cwnd = old_cwnd; 1263 } 1264 /* Try to update pipeAck and NCWV state */ 1265 if (TCPS_HAVEESTABLISHED(tp->t_state) && 1266 !IN_RECOVERY(tp->t_flags)) { 1267 uint32_t data = sbavail(&(tp->t_inpcb->inp_socket->so_snd)); 1268 1269 tcp_newcwv_update_pipeack(tp, data); 1270 } 1271 } 1272 /* we enforce max peak rate if it is set. */ 1273 if (tp->t_peakrate_thr && tp->snd_cwnd > tp->t_peakrate_thr) { 1274 tp->snd_cwnd = tp->t_peakrate_thr; 1275 } 1276 #endif 1277 } 1278 1279 static void 1280 tcp_rack_partialack(struct tcpcb *tp, struct tcphdr *th) 1281 { 1282 struct tcp_rack *rack; 1283 1284 rack = (struct tcp_rack *)tp->t_fb_ptr; 1285 INP_WLOCK_ASSERT(tp->t_inpcb); 1286 if (rack->r_ctl.rc_prr_sndcnt > 0) 1287 rack->r_wanted_output++; 1288 } 1289 1290 static void 1291 rack_post_recovery(struct tcpcb *tp, struct tcphdr *th) 1292 { 1293 struct tcp_rack *rack; 1294 1295 INP_WLOCK_ASSERT(tp->t_inpcb); 1296 rack = (struct tcp_rack *)tp->t_fb_ptr; 1297 if (CC_ALGO(tp)->post_recovery != NULL) { 1298 tp->ccv->curack = th->th_ack; 1299 CC_ALGO(tp)->post_recovery(tp->ccv); 1300 } 1301 /* 1302 * Here we can in theory adjust cwnd to be based on the number of 1303 * losses in the window (rack->r_ctl.rc_loss_count). This is done 1304 * based on the rack_use_proportional flag. 1305 */ 1306 if (rack->r_ctl.rc_prop_reduce && rack->r_ctl.rc_prop_rate) { 1307 int32_t reduce; 1308 1309 reduce = (rack->r_ctl.rc_loss_count * rack->r_ctl.rc_prop_rate); 1310 if (reduce > 50) { 1311 reduce = 50; 1312 } 1313 tp->snd_cwnd -= ((reduce * tp->snd_cwnd) / 100); 1314 } else { 1315 if (tp->snd_cwnd > tp->snd_ssthresh) { 1316 /* Drop us down to the ssthresh (1/2 cwnd at loss) */ 1317 tp->snd_cwnd = tp->snd_ssthresh; 1318 } 1319 } 1320 if (rack->r_ctl.rc_prr_sndcnt > 0) { 1321 /* Suck the next prr cnt back into cwnd */ 1322 tp->snd_cwnd += rack->r_ctl.rc_prr_sndcnt; 1323 rack->r_ctl.rc_prr_sndcnt = 0; 1324 } 1325 EXIT_RECOVERY(tp->t_flags); 1326 1327 1328 #ifdef NETFLIX_CWV 1329 if (tp->cwv_enabled) { 1330 if ((tp->cwv_cwnd_valid == 0) && 1331 (tp->snd_cwv.in_recovery)) 1332 tcp_newcwv_end_recovery(tp); 1333 } 1334 #endif 1335 } 1336 1337 static void 1338 rack_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 1339 { 1340 struct tcp_rack *rack; 1341 1342 INP_WLOCK_ASSERT(tp->t_inpcb); 1343 1344 rack = (struct tcp_rack *)tp->t_fb_ptr; 1345 switch (type) { 1346 case CC_NDUPACK: 1347 /* rack->r_ctl.rc_ssthresh_set = 1;*/ 1348 if (!IN_FASTRECOVERY(tp->t_flags)) { 1349 rack->r_ctl.rc_tlp_rtx_out = 0; 1350 rack->r_ctl.rc_prr_delivered = 0; 1351 rack->r_ctl.rc_prr_out = 0; 1352 rack->r_ctl.rc_loss_count = 0; 1353 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 1354 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una; 1355 tp->snd_recover = tp->snd_max; 1356 if (tp->t_flags & TF_ECN_PERMIT) 1357 tp->t_flags |= TF_ECN_SND_CWR; 1358 } 1359 break; 1360 case CC_ECN: 1361 if (!IN_CONGRECOVERY(tp->t_flags)) { 1362 TCPSTAT_INC(tcps_ecn_rcwnd); 1363 tp->snd_recover = tp->snd_max; 1364 if (tp->t_flags & TF_ECN_PERMIT) 1365 tp->t_flags |= TF_ECN_SND_CWR; 1366 } 1367 break; 1368 case CC_RTO: 1369 tp->t_dupacks = 0; 1370 tp->t_bytes_acked = 0; 1371 EXIT_RECOVERY(tp->t_flags); 1372 tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1373 tp->t_maxseg) * tp->t_maxseg; 1374 tp->snd_cwnd = tp->t_maxseg; 1375 break; 1376 case CC_RTO_ERR: 1377 TCPSTAT_INC(tcps_sndrexmitbad); 1378 /* RTO was unnecessary, so reset everything. */ 1379 tp->snd_cwnd = tp->snd_cwnd_prev; 1380 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1381 tp->snd_recover = tp->snd_recover_prev; 1382 if (tp->t_flags & TF_WASFRECOVERY) 1383 ENTER_FASTRECOVERY(tp->t_flags); 1384 if (tp->t_flags & TF_WASCRECOVERY) 1385 ENTER_CONGRECOVERY(tp->t_flags); 1386 tp->snd_nxt = tp->snd_max; 1387 tp->t_badrxtwin = 0; 1388 break; 1389 } 1390 1391 if (CC_ALGO(tp)->cong_signal != NULL) { 1392 if (th != NULL) 1393 tp->ccv->curack = th->th_ack; 1394 CC_ALGO(tp)->cong_signal(tp->ccv, type); 1395 } 1396 #ifdef NETFLIX_CWV 1397 if (tp->cwv_enabled) { 1398 if (tp->snd_cwv.in_recovery == 0 && IN_RECOVERY(tp->t_flags)) { 1399 tcp_newcwv_enter_recovery(tp); 1400 } 1401 if (type == CC_RTO) { 1402 tcp_newcwv_reset(tp); 1403 } 1404 } 1405 #endif 1406 } 1407 1408 1409 1410 static inline void 1411 rack_cc_after_idle(struct tcpcb *tp, int reduce_largest) 1412 { 1413 uint32_t i_cwnd; 1414 1415 INP_WLOCK_ASSERT(tp->t_inpcb); 1416 1417 #ifdef NETFLIX_STATS 1418 TCPSTAT_INC(tcps_idle_restarts); 1419 if (tp->t_state == TCPS_ESTABLISHED) 1420 TCPSTAT_INC(tcps_idle_estrestarts); 1421 #endif 1422 if (CC_ALGO(tp)->after_idle != NULL) 1423 CC_ALGO(tp)->after_idle(tp->ccv); 1424 1425 if (tp->snd_cwnd == 1) 1426 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 1427 else 1428 i_cwnd = tcp_compute_initwnd(tcp_maxseg(tp)); 1429 1430 if (reduce_largest) { 1431 /* 1432 * Do we reduce the largest cwnd to make 1433 * rack play nice on restart hptsi wise? 1434 */ 1435 if (((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rack_largest_cwnd > i_cwnd) 1436 ((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rack_largest_cwnd = i_cwnd; 1437 } 1438 /* 1439 * Being idle is no differnt than the initial window. If the cc 1440 * clamps it down below the initial window raise it to the initial 1441 * window. 1442 */ 1443 if (tp->snd_cwnd < i_cwnd) { 1444 tp->snd_cwnd = i_cwnd; 1445 } 1446 } 1447 1448 1449 /* 1450 * Indicate whether this ack should be delayed. We can delay the ack if 1451 * following conditions are met: 1452 * - There is no delayed ack timer in progress. 1453 * - Our last ack wasn't a 0-sized window. We never want to delay 1454 * the ack that opens up a 0-sized window. 1455 * - LRO wasn't used for this segment. We make sure by checking that the 1456 * segment size is not larger than the MSS. 1457 * - Delayed acks are enabled or this is a half-synchronized T/TCP 1458 * connection. 1459 */ 1460 #define DELAY_ACK(tp, tlen) \ 1461 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1462 ((tp->t_flags & TF_DELACK) == 0) && \ 1463 (tlen <= tp->t_maxseg) && \ 1464 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 1465 1466 static inline void 1467 rack_calc_rwin(struct socket *so, struct tcpcb *tp) 1468 { 1469 int32_t win; 1470 1471 /* 1472 * Calculate amount of space in receive window, and then do TCP 1473 * input processing. Receive window is amount of space in rcv queue, 1474 * but not less than advertised window. 1475 */ 1476 win = sbspace(&so->so_rcv); 1477 if (win < 0) 1478 win = 0; 1479 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1480 } 1481 1482 static void 1483 rack_do_drop(struct mbuf *m, struct tcpcb *tp) 1484 { 1485 /* 1486 * Drop space held by incoming segment and return. 1487 */ 1488 if (tp != NULL) 1489 INP_WUNLOCK(tp->t_inpcb); 1490 if (m) 1491 m_freem(m); 1492 } 1493 1494 static void 1495 rack_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, 1496 int32_t rstreason, int32_t tlen) 1497 { 1498 if (tp != NULL) { 1499 tcp_dropwithreset(m, th, tp, tlen, rstreason); 1500 INP_WUNLOCK(tp->t_inpcb); 1501 } else 1502 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1503 } 1504 1505 /* 1506 * The value in ret_val informs the caller 1507 * if we dropped the tcb (and lock) or not. 1508 * 1 = we dropped it, 0 = the TCB is still locked 1509 * and valid. 1510 */ 1511 static void 1512 rack_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t * ret_val) 1513 { 1514 /* 1515 * Generate an ACK dropping incoming segment if it occupies sequence 1516 * space, where the ACK reflects our state. 1517 * 1518 * We can now skip the test for the RST flag since all paths to this 1519 * code happen after packets containing RST have been dropped. 1520 * 1521 * In the SYN-RECEIVED state, don't send an ACK unless the segment 1522 * we received passes the SYN-RECEIVED ACK test. If it fails send a 1523 * RST. This breaks the loop in the "LAND" DoS attack, and also 1524 * prevents an ACK storm between two listening ports that have been 1525 * sent forged SYN segments, each with the source address of the 1526 * other. 1527 */ 1528 struct tcp_rack *rack; 1529 1530 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 1531 (SEQ_GT(tp->snd_una, th->th_ack) || 1532 SEQ_GT(th->th_ack, tp->snd_max))) { 1533 *ret_val = 1; 1534 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 1535 return; 1536 } else 1537 *ret_val = 0; 1538 rack = (struct tcp_rack *)tp->t_fb_ptr; 1539 rack->r_wanted_output++; 1540 tp->t_flags |= TF_ACKNOW; 1541 if (m) 1542 m_freem(m); 1543 } 1544 1545 1546 static int 1547 rack_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, struct tcpcb *tp) 1548 { 1549 /* 1550 * RFC5961 Section 3.2 1551 * 1552 * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in 1553 * window, we send challenge ACK. 1554 * 1555 * Note: to take into account delayed ACKs, we should test against 1556 * last_ack_sent instead of rcv_nxt. Note 2: we handle special case 1557 * of closed window, not covered by the RFC. 1558 */ 1559 int dropped = 0; 1560 1561 if ((SEQ_GEQ(th->th_seq, (tp->last_ack_sent - 1)) && 1562 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 1563 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 1564 1565 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1566 KASSERT(tp->t_state != TCPS_SYN_SENT, 1567 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 1568 __func__, th, tp)); 1569 1570 if (V_tcp_insecure_rst || 1571 (tp->last_ack_sent == th->th_seq) || 1572 (tp->rcv_nxt == th->th_seq) || 1573 ((tp->last_ack_sent - 1) == th->th_seq)) { 1574 TCPSTAT_INC(tcps_drops); 1575 /* Drop the connection. */ 1576 switch (tp->t_state) { 1577 case TCPS_SYN_RECEIVED: 1578 so->so_error = ECONNREFUSED; 1579 goto close; 1580 case TCPS_ESTABLISHED: 1581 case TCPS_FIN_WAIT_1: 1582 case TCPS_FIN_WAIT_2: 1583 case TCPS_CLOSE_WAIT: 1584 case TCPS_CLOSING: 1585 case TCPS_LAST_ACK: 1586 so->so_error = ECONNRESET; 1587 close: 1588 tcp_state_change(tp, TCPS_CLOSED); 1589 /* FALLTHROUGH */ 1590 default: 1591 tp = tcp_close(tp); 1592 } 1593 dropped = 1; 1594 rack_do_drop(m, tp); 1595 } else { 1596 TCPSTAT_INC(tcps_badrst); 1597 /* Send challenge ACK. */ 1598 tcp_respond(tp, mtod(m, void *), th, m, 1599 tp->rcv_nxt, tp->snd_nxt, TH_ACK); 1600 tp->last_ack_sent = tp->rcv_nxt; 1601 } 1602 } else { 1603 m_freem(m); 1604 } 1605 return (dropped); 1606 } 1607 1608 /* 1609 * The value in ret_val informs the caller 1610 * if we dropped the tcb (and lock) or not. 1611 * 1 = we dropped it, 0 = the TCB is still locked 1612 * and valid. 1613 */ 1614 static void 1615 rack_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * ret_val) 1616 { 1617 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1618 1619 TCPSTAT_INC(tcps_badsyn); 1620 if (V_tcp_insecure_syn && 1621 SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1622 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1623 tp = tcp_drop(tp, ECONNRESET); 1624 *ret_val = 1; 1625 rack_do_drop(m, tp); 1626 } else { 1627 /* Send challenge ACK. */ 1628 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 1629 tp->snd_nxt, TH_ACK); 1630 tp->last_ack_sent = tp->rcv_nxt; 1631 m = NULL; 1632 *ret_val = 0; 1633 rack_do_drop(m, NULL); 1634 } 1635 } 1636 1637 /* 1638 * rack_ts_check returns 1 for you should not proceed. It places 1639 * in ret_val what should be returned 1/0 by the caller. The 1 indicates 1640 * that the TCB is unlocked and probably dropped. The 0 indicates the 1641 * TCB is still valid and locked. 1642 */ 1643 static int 1644 rack_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t tlen, int32_t thflags, int32_t * ret_val) 1645 { 1646 1647 /* Check to see if ts_recent is over 24 days old. */ 1648 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 1649 /* 1650 * Invalidate ts_recent. If this segment updates ts_recent, 1651 * the age will be reset later and ts_recent will get a 1652 * valid value. If it does not, setting ts_recent to zero 1653 * will at least satisfy the requirement that zero be placed 1654 * in the timestamp echo reply when ts_recent isn't valid. 1655 * The age isn't reset until we get a valid ts_recent 1656 * because we don't want out-of-order segments to be dropped 1657 * when ts_recent is old. 1658 */ 1659 tp->ts_recent = 0; 1660 } else { 1661 TCPSTAT_INC(tcps_rcvduppack); 1662 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 1663 TCPSTAT_INC(tcps_pawsdrop); 1664 *ret_val = 0; 1665 if (tlen) { 1666 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 1667 } else { 1668 rack_do_drop(m, NULL); 1669 } 1670 return (1); 1671 } 1672 return (0); 1673 } 1674 1675 /* 1676 * rack_drop_checks returns 1 for you should not proceed. It places 1677 * in ret_val what should be returned 1/0 by the caller. The 1 indicates 1678 * that the TCB is unlocked and probably dropped. The 0 indicates the 1679 * TCB is still valid and locked. 1680 */ 1681 static int 1682 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) 1683 { 1684 int32_t todrop; 1685 int32_t thflags; 1686 int32_t tlen; 1687 1688 thflags = *thf; 1689 tlen = *tlenp; 1690 todrop = tp->rcv_nxt - th->th_seq; 1691 if (todrop > 0) { 1692 if (thflags & TH_SYN) { 1693 thflags &= ~TH_SYN; 1694 th->th_seq++; 1695 if (th->th_urp > 1) 1696 th->th_urp--; 1697 else 1698 thflags &= ~TH_URG; 1699 todrop--; 1700 } 1701 /* 1702 * Following if statement from Stevens, vol. 2, p. 960. 1703 */ 1704 if (todrop > tlen 1705 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1706 /* 1707 * Any valid FIN must be to the left of the window. 1708 * At this point the FIN must be a duplicate or out 1709 * of sequence; drop it. 1710 */ 1711 thflags &= ~TH_FIN; 1712 /* 1713 * Send an ACK to resynchronize and drop any data. 1714 * But keep on processing for RST or ACK. 1715 */ 1716 tp->t_flags |= TF_ACKNOW; 1717 todrop = tlen; 1718 TCPSTAT_INC(tcps_rcvduppack); 1719 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 1720 } else { 1721 TCPSTAT_INC(tcps_rcvpartduppack); 1722 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 1723 } 1724 *drop_hdrlen += todrop; /* drop from the top afterwards */ 1725 th->th_seq += todrop; 1726 tlen -= todrop; 1727 if (th->th_urp > todrop) 1728 th->th_urp -= todrop; 1729 else { 1730 thflags &= ~TH_URG; 1731 th->th_urp = 0; 1732 } 1733 } 1734 /* 1735 * If segment ends after window, drop trailing data (and PUSH and 1736 * FIN); if nothing left, just ACK. 1737 */ 1738 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1739 if (todrop > 0) { 1740 TCPSTAT_INC(tcps_rcvpackafterwin); 1741 if (todrop >= tlen) { 1742 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 1743 /* 1744 * If window is closed can only take segments at 1745 * window edge, and have to drop data and PUSH from 1746 * incoming segments. Continue processing, but 1747 * remember to ack. Otherwise, drop segment and 1748 * ack. 1749 */ 1750 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1751 tp->t_flags |= TF_ACKNOW; 1752 TCPSTAT_INC(tcps_rcvwinprobe); 1753 } else { 1754 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 1755 return (1); 1756 } 1757 } else 1758 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1759 m_adj(m, -todrop); 1760 tlen -= todrop; 1761 thflags &= ~(TH_PUSH | TH_FIN); 1762 } 1763 *thf = thflags; 1764 *tlenp = tlen; 1765 return (0); 1766 } 1767 1768 static struct rack_sendmap * 1769 rack_find_lowest_rsm(struct tcp_rack *rack) 1770 { 1771 struct rack_sendmap *rsm; 1772 1773 /* 1774 * Walk the time-order transmitted list looking for an rsm that is 1775 * not acked. This will be the one that was sent the longest time 1776 * ago that is still outstanding. 1777 */ 1778 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 1779 if (rsm->r_flags & RACK_ACKED) { 1780 continue; 1781 } 1782 goto finish; 1783 } 1784 finish: 1785 return (rsm); 1786 } 1787 1788 static struct rack_sendmap * 1789 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm) 1790 { 1791 struct rack_sendmap *prsm; 1792 1793 /* 1794 * Walk the sequence order list backward until we hit and arrive at 1795 * the highest seq not acked. In theory when this is called it 1796 * should be the last segment (which it was not). 1797 */ 1798 counter_u64_add(rack_find_high, 1); 1799 prsm = rsm; 1800 TAILQ_FOREACH_REVERSE_FROM(prsm, &rack->r_ctl.rc_map, rack_head, r_next) { 1801 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) { 1802 continue; 1803 } 1804 return (prsm); 1805 } 1806 return (NULL); 1807 } 1808 1809 1810 static uint32_t 1811 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts) 1812 { 1813 int32_t lro; 1814 uint32_t thresh; 1815 1816 /* 1817 * lro is the flag we use to determine if we have seen reordering. 1818 * If it gets set we have seen reordering. The reorder logic either 1819 * works in one of two ways: 1820 * 1821 * If reorder-fade is configured, then we track the last time we saw 1822 * re-ordering occur. If we reach the point where enough time as 1823 * passed we no longer consider reordering has occuring. 1824 * 1825 * Or if reorder-face is 0, then once we see reordering we consider 1826 * the connection to alway be subject to reordering and just set lro 1827 * to 1. 1828 * 1829 * In the end if lro is non-zero we add the extra time for 1830 * reordering in. 1831 */ 1832 if (srtt == 0) 1833 srtt = 1; 1834 if (rack->r_ctl.rc_reorder_ts) { 1835 if (rack->r_ctl.rc_reorder_fade) { 1836 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) { 1837 lro = cts - rack->r_ctl.rc_reorder_ts; 1838 if (lro == 0) { 1839 /* 1840 * No time as passed since the last 1841 * reorder, mark it as reordering. 1842 */ 1843 lro = 1; 1844 } 1845 } else { 1846 /* Negative time? */ 1847 lro = 0; 1848 } 1849 if (lro > rack->r_ctl.rc_reorder_fade) { 1850 /* Turn off reordering seen too */ 1851 rack->r_ctl.rc_reorder_ts = 0; 1852 lro = 0; 1853 } 1854 } else { 1855 /* Reodering does not fade */ 1856 lro = 1; 1857 } 1858 } else { 1859 lro = 0; 1860 } 1861 thresh = srtt + rack->r_ctl.rc_pkt_delay; 1862 if (lro) { 1863 /* It must be set, if not you get 1/4 rtt */ 1864 if (rack->r_ctl.rc_reorder_shift) 1865 thresh += (srtt >> rack->r_ctl.rc_reorder_shift); 1866 else 1867 thresh += (srtt >> 2); 1868 } else { 1869 thresh += 1; 1870 } 1871 /* We don't let the rack timeout be above a RTO */ 1872 1873 if (thresh > TICKS_2_MSEC(rack->rc_tp->t_rxtcur)) { 1874 thresh = TICKS_2_MSEC(rack->rc_tp->t_rxtcur); 1875 } 1876 /* And we don't want it above the RTO max either */ 1877 if (thresh > rack_rto_max) { 1878 thresh = rack_rto_max; 1879 } 1880 return (thresh); 1881 } 1882 1883 static uint32_t 1884 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack, 1885 struct rack_sendmap *rsm, uint32_t srtt) 1886 { 1887 struct rack_sendmap *prsm; 1888 uint32_t thresh, len; 1889 int maxseg; 1890 1891 if (srtt == 0) 1892 srtt = 1; 1893 if (rack->r_ctl.rc_tlp_threshold) 1894 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold); 1895 else 1896 thresh = (srtt * 2); 1897 1898 /* Get the previous sent packet, if any */ 1899 maxseg = tcp_maxseg(tp); 1900 counter_u64_add(rack_enter_tlp_calc, 1); 1901 len = rsm->r_end - rsm->r_start; 1902 if (rack->rack_tlp_threshold_use == TLP_USE_ID) { 1903 /* Exactly like the ID */ 1904 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= maxseg) { 1905 uint32_t alt_thresh; 1906 /* 1907 * Compensate for delayed-ack with the d-ack time. 1908 */ 1909 counter_u64_add(rack_used_tlpmethod, 1); 1910 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 1911 if (alt_thresh > thresh) 1912 thresh = alt_thresh; 1913 } 1914 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) { 1915 /* 2.1 behavior */ 1916 prsm = TAILQ_PREV(rsm, rack_head, r_tnext); 1917 if (prsm && (len <= maxseg)) { 1918 /* 1919 * Two packets outstanding, thresh should be (2*srtt) + 1920 * possible inter-packet delay (if any). 1921 */ 1922 uint32_t inter_gap = 0; 1923 int idx, nidx; 1924 1925 counter_u64_add(rack_used_tlpmethod, 1); 1926 idx = rsm->r_rtr_cnt - 1; 1927 nidx = prsm->r_rtr_cnt - 1; 1928 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 1929 /* Yes it was sent later (or at the same time) */ 1930 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 1931 } 1932 thresh += inter_gap; 1933 } else if (len <= maxseg) { 1934 /* 1935 * Possibly compensate for delayed-ack. 1936 */ 1937 uint32_t alt_thresh; 1938 1939 counter_u64_add(rack_used_tlpmethod2, 1); 1940 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 1941 if (alt_thresh > thresh) 1942 thresh = alt_thresh; 1943 } 1944 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) { 1945 /* 2.2 behavior */ 1946 if (len <= maxseg) { 1947 uint32_t alt_thresh; 1948 /* 1949 * Compensate for delayed-ack with the d-ack time. 1950 */ 1951 counter_u64_add(rack_used_tlpmethod, 1); 1952 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 1953 if (alt_thresh > thresh) 1954 thresh = alt_thresh; 1955 } 1956 } 1957 /* Not above an RTO */ 1958 if (thresh > TICKS_2_MSEC(tp->t_rxtcur)) { 1959 thresh = TICKS_2_MSEC(tp->t_rxtcur); 1960 } 1961 /* Not above a RTO max */ 1962 if (thresh > rack_rto_max) { 1963 thresh = rack_rto_max; 1964 } 1965 /* Apply user supplied min TLP */ 1966 if (thresh < rack_tlp_min) { 1967 thresh = rack_tlp_min; 1968 } 1969 return (thresh); 1970 } 1971 1972 static struct rack_sendmap * 1973 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused) 1974 { 1975 /* 1976 * Check to see that we don't need to fall into recovery. We will 1977 * need to do so if our oldest transmit is past the time we should 1978 * have had an ack. 1979 */ 1980 struct tcp_rack *rack; 1981 struct rack_sendmap *rsm; 1982 int32_t idx; 1983 uint32_t srtt_cur, srtt, thresh; 1984 1985 rack = (struct tcp_rack *)tp->t_fb_ptr; 1986 if (TAILQ_EMPTY(&rack->r_ctl.rc_map)) { 1987 return (NULL); 1988 } 1989 srtt_cur = tp->t_srtt >> TCP_RTT_SHIFT; 1990 srtt = TICKS_2_MSEC(srtt_cur); 1991 if (rack->rc_rack_rtt && (srtt > rack->rc_rack_rtt)) 1992 srtt = rack->rc_rack_rtt; 1993 1994 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 1995 if (rsm == NULL) 1996 return (NULL); 1997 1998 if (rsm->r_flags & RACK_ACKED) { 1999 rsm = rack_find_lowest_rsm(rack); 2000 if (rsm == NULL) 2001 return (NULL); 2002 } 2003 idx = rsm->r_rtr_cnt - 1; 2004 thresh = rack_calc_thresh_rack(rack, srtt, tsused); 2005 if (tsused < rsm->r_tim_lastsent[idx]) { 2006 return (NULL); 2007 } 2008 if ((tsused - rsm->r_tim_lastsent[idx]) < thresh) { 2009 return (NULL); 2010 } 2011 /* Ok if we reach here we are over-due */ 2012 rack->r_ctl.rc_rsm_start = rsm->r_start; 2013 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 2014 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 2015 rack_cong_signal(tp, NULL, CC_NDUPACK); 2016 return (rsm); 2017 } 2018 2019 static uint32_t 2020 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack) 2021 { 2022 int32_t t; 2023 int32_t tt; 2024 uint32_t ret_val; 2025 2026 t = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT) + ((tp->t_rttvar * 4) >> TCP_RTT_SHIFT)); 2027 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 2028 tcp_persmin, tcp_persmax); 2029 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 2030 tp->t_rxtshift++; 2031 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 2032 ret_val = (uint32_t)tt; 2033 return (ret_val); 2034 } 2035 2036 static uint32_t 2037 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2038 { 2039 /* 2040 * Start the FR timer, we do this based on getting the first one in 2041 * the rc_tmap. Note that if its NULL we must stop the timer. in all 2042 * events we need to stop the running timer (if its running) before 2043 * starting the new one. 2044 */ 2045 uint32_t thresh, exp, to, srtt, time_since_sent; 2046 uint32_t srtt_cur; 2047 int32_t idx; 2048 int32_t is_tlp_timer = 0; 2049 struct rack_sendmap *rsm; 2050 2051 if (rack->t_timers_stopped) { 2052 /* All timers have been stopped none are to run */ 2053 return (0); 2054 } 2055 if (rack->rc_in_persist) { 2056 /* We can't start any timer in persists */ 2057 return (rack_get_persists_timer_val(tp, rack)); 2058 } 2059 if (tp->t_state < TCPS_ESTABLISHED) 2060 goto activate_rxt; 2061 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 2062 if (rsm == NULL) { 2063 /* Nothing on the send map */ 2064 activate_rxt: 2065 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) { 2066 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 2067 to = TICKS_2_MSEC(tp->t_rxtcur); 2068 if (to == 0) 2069 to = 1; 2070 return (to); 2071 } 2072 return (0); 2073 } 2074 if (rsm->r_flags & RACK_ACKED) { 2075 rsm = rack_find_lowest_rsm(rack); 2076 if (rsm == NULL) { 2077 /* No lowest? */ 2078 goto activate_rxt; 2079 } 2080 } 2081 /* Convert from ms to usecs */ 2082 if (rsm->r_flags & RACK_SACK_PASSED) { 2083 if ((tp->t_flags & TF_SENTFIN) && 2084 ((tp->snd_max - tp->snd_una) == 1) && 2085 (rsm->r_flags & RACK_HAS_FIN)) { 2086 /* 2087 * We don't start a rack timer if all we have is a 2088 * FIN outstanding. 2089 */ 2090 goto activate_rxt; 2091 } 2092 if (tp->t_srtt) { 2093 srtt_cur = (tp->t_srtt >> TCP_RTT_SHIFT); 2094 srtt = TICKS_2_MSEC(srtt_cur); 2095 } else 2096 srtt = RACK_INITIAL_RTO; 2097 2098 thresh = rack_calc_thresh_rack(rack, srtt, cts); 2099 idx = rsm->r_rtr_cnt - 1; 2100 exp = rsm->r_tim_lastsent[idx] + thresh; 2101 if (SEQ_GEQ(exp, cts)) { 2102 to = exp - cts; 2103 if (to < rack->r_ctl.rc_min_to) { 2104 to = rack->r_ctl.rc_min_to; 2105 } 2106 } else { 2107 to = rack->r_ctl.rc_min_to; 2108 } 2109 } else { 2110 /* Ok we need to do a TLP not RACK */ 2111 if ((rack->rc_tlp_in_progress != 0) || 2112 (rack->r_ctl.rc_tlp_rtx_out != 0)) { 2113 /* 2114 * The previous send was a TLP or a tlp_rtx is in 2115 * process. 2116 */ 2117 goto activate_rxt; 2118 } 2119 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 2120 if (rsm == NULL) { 2121 /* We found no rsm to TLP with. */ 2122 goto activate_rxt; 2123 } 2124 if (rsm->r_flags & RACK_HAS_FIN) { 2125 /* If its a FIN we dont do TLP */ 2126 rsm = NULL; 2127 goto activate_rxt; 2128 } 2129 idx = rsm->r_rtr_cnt - 1; 2130 if (TSTMP_GT(cts, rsm->r_tim_lastsent[idx])) 2131 time_since_sent = cts - rsm->r_tim_lastsent[idx]; 2132 else 2133 time_since_sent = 0; 2134 is_tlp_timer = 1; 2135 if (tp->t_srtt) { 2136 srtt_cur = (tp->t_srtt >> TCP_RTT_SHIFT); 2137 srtt = TICKS_2_MSEC(srtt_cur); 2138 } else 2139 srtt = RACK_INITIAL_RTO; 2140 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt); 2141 if (thresh > time_since_sent) 2142 to = thresh - time_since_sent; 2143 else 2144 to = rack->r_ctl.rc_min_to; 2145 if (to > TCPTV_REXMTMAX) { 2146 /* 2147 * If the TLP time works out to larger than the max 2148 * RTO lets not do TLP.. just RTO. 2149 */ 2150 goto activate_rxt; 2151 } 2152 if (rsm->r_start != rack->r_ctl.rc_last_tlp_seq) { 2153 /* 2154 * The tail is no longer the last one I did a probe 2155 * on 2156 */ 2157 rack->r_ctl.rc_tlp_seg_send_cnt = 0; 2158 rack->r_ctl.rc_last_tlp_seq = rsm->r_start; 2159 } 2160 } 2161 if (is_tlp_timer == 0) { 2162 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 2163 } else { 2164 if ((rack->r_ctl.rc_tlp_send_cnt > rack_tlp_max_resend) || 2165 (rack->r_ctl.rc_tlp_seg_send_cnt > rack_tlp_max_resend)) { 2166 /* 2167 * We have exceeded how many times we can retran the 2168 * current TLP timer, switch to the RTO timer. 2169 */ 2170 goto activate_rxt; 2171 } else { 2172 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 2173 } 2174 } 2175 if (to == 0) 2176 to = 1; 2177 return (to); 2178 } 2179 2180 static void 2181 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2182 { 2183 if (rack->rc_in_persist == 0) { 2184 if (((tp->t_flags & TF_SENTFIN) == 0) && 2185 (tp->snd_max - tp->snd_una) >= sbavail(&rack->rc_inp->inp_socket->so_snd)) 2186 /* Must need to send more data to enter persist */ 2187 return; 2188 rack->r_ctl.rc_went_idle_time = cts; 2189 rack_timer_cancel(tp, rack, cts, __LINE__); 2190 tp->t_rxtshift = 0; 2191 rack->rc_in_persist = 1; 2192 } 2193 } 2194 2195 static void 2196 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack) 2197 { 2198 if (rack->rc_inp->inp_in_hpts) { 2199 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 2200 rack->r_ctl.rc_hpts_flags = 0; 2201 } 2202 rack->rc_in_persist = 0; 2203 rack->r_ctl.rc_went_idle_time = 0; 2204 tp->t_flags &= ~TF_FORCEDATA; 2205 tp->t_rxtshift = 0; 2206 } 2207 2208 static void 2209 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts, int32_t line, 2210 int32_t slot, uint32_t tot_len_this_send, int32_t frm_out_sbavail) 2211 { 2212 struct inpcb *inp; 2213 uint32_t delayed_ack = 0; 2214 uint32_t hpts_timeout; 2215 uint8_t stopped; 2216 uint32_t left = 0; 2217 2218 inp = tp->t_inpcb; 2219 if (inp->inp_in_hpts) { 2220 /* A previous call is already set up */ 2221 return; 2222 } 2223 if (tp->t_state == TCPS_CLOSED) { 2224 return; 2225 } 2226 stopped = rack->rc_tmr_stopped; 2227 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) { 2228 left = rack->r_ctl.rc_timer_exp - cts; 2229 } 2230 rack->r_ctl.rc_timer_exp = 0; 2231 if (rack->rc_inp->inp_in_hpts == 0) { 2232 rack->r_ctl.rc_hpts_flags = 0; 2233 } 2234 if (slot) { 2235 /* We are hptsi too */ 2236 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 2237 } else if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 2238 /* 2239 * We are still left on the hpts when the to goes 2240 * it will be for output. 2241 */ 2242 if (TSTMP_GT(cts, rack->r_ctl.rc_last_output_to)) 2243 slot = cts - rack->r_ctl.rc_last_output_to; 2244 else 2245 slot = 1; 2246 } 2247 if ((tp->snd_wnd == 0) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2248 /* No send window.. we must enter persist */ 2249 rack_enter_persist(tp, rack, cts); 2250 } else if ((frm_out_sbavail && 2251 (frm_out_sbavail > (tp->snd_max - tp->snd_una)) && 2252 (tp->snd_wnd < tp->t_maxseg)) && 2253 TCPS_HAVEESTABLISHED(tp->t_state)) { 2254 /* 2255 * If we have no window or we can't send a segment (and have 2256 * data to send.. we cheat here and frm_out_sbavail is 2257 * passed in with the sbavail(sb) only from bbr_output) and 2258 * we are established, then we must enter persits (if not 2259 * already in persits). 2260 */ 2261 rack_enter_persist(tp, rack, cts); 2262 } 2263 hpts_timeout = rack_timer_start(tp, rack, cts); 2264 if (tp->t_flags & TF_DELACK) { 2265 delayed_ack = TICKS_2_MSEC(tcp_delacktime); 2266 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK; 2267 } 2268 if (delayed_ack && ((hpts_timeout == 0) || 2269 (delayed_ack < hpts_timeout))) 2270 hpts_timeout = delayed_ack; 2271 else 2272 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 2273 /* 2274 * If no timers are going to run and we will fall off the hptsi 2275 * wheel, we resort to a keep-alive timer if its configured. 2276 */ 2277 if ((hpts_timeout == 0) && 2278 (slot == 0)) { 2279 if ((tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 2280 (tp->t_state <= TCPS_CLOSING)) { 2281 /* 2282 * Ok we have no timer (persists, rack, tlp, rxt or 2283 * del-ack), we don't have segments being paced. So 2284 * all that is left is the keepalive timer. 2285 */ 2286 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2287 /* Get the established keep-alive time */ 2288 hpts_timeout = TP_KEEPIDLE(tp); 2289 } else { 2290 /* Get the initial setup keep-alive time */ 2291 hpts_timeout = TP_KEEPINIT(tp); 2292 } 2293 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 2294 } 2295 } 2296 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 2297 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 2298 /* 2299 * RACK, TLP, persists and RXT timers all are restartable 2300 * based on actions input .. i.e we received a packet (ack 2301 * or sack) and that changes things (rw, or snd_una etc). 2302 * Thus we can restart them with a new value. For 2303 * keep-alive, delayed_ack we keep track of what was left 2304 * and restart the timer with a smaller value. 2305 */ 2306 if (left < hpts_timeout) 2307 hpts_timeout = left; 2308 } 2309 if (hpts_timeout) { 2310 /* 2311 * Hack alert for now we can't time-out over 2,147,483 2312 * seconds (a bit more than 596 hours), which is probably ok 2313 * :). 2314 */ 2315 if (hpts_timeout > 0x7ffffffe) 2316 hpts_timeout = 0x7ffffffe; 2317 rack->r_ctl.rc_timer_exp = cts + hpts_timeout; 2318 } 2319 if (slot) { 2320 rack->r_ctl.rc_last_output_to = cts + slot; 2321 if ((hpts_timeout == 0) || (hpts_timeout > slot)) { 2322 if (rack->rc_inp->inp_in_hpts == 0) 2323 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(slot)); 2324 rack_log_to_start(rack, cts, hpts_timeout, slot, 1); 2325 } else { 2326 /* 2327 * Arrange for the hpts to kick back in after the 2328 * t-o if the t-o does not cause a send. 2329 */ 2330 if (rack->rc_inp->inp_in_hpts == 0) 2331 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout)); 2332 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 2333 } 2334 } else if (hpts_timeout) { 2335 if (rack->rc_inp->inp_in_hpts == 0) 2336 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(hpts_timeout)); 2337 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 2338 } else { 2339 /* No timer starting */ 2340 #ifdef INVARIANTS 2341 if (SEQ_GT(tp->snd_max, tp->snd_una)) { 2342 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?", 2343 tp, rack, tot_len_this_send, cts, slot, hpts_timeout); 2344 } 2345 #endif 2346 } 2347 rack->rc_tmr_stopped = 0; 2348 if (slot) 2349 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, cts); 2350 } 2351 2352 /* 2353 * RACK Timer, here we simply do logging and house keeping. 2354 * the normal rack_output() function will call the 2355 * appropriate thing to check if we need to do a RACK retransmit. 2356 * We return 1, saying don't proceed with rack_output only 2357 * when all timers have been stopped (destroyed PCB?). 2358 */ 2359 static int 2360 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2361 { 2362 /* 2363 * This timer simply provides an internal trigger to send out data. 2364 * The check_recovery_mode call will see if there are needed 2365 * retransmissions, if so we will enter fast-recovery. The output 2366 * call may or may not do the same thing depending on sysctl 2367 * settings. 2368 */ 2369 struct rack_sendmap *rsm; 2370 int32_t recovery; 2371 2372 if (tp->t_timers->tt_flags & TT_STOPPED) { 2373 return (1); 2374 } 2375 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 2376 /* Its not time yet */ 2377 return (0); 2378 } 2379 rack_log_to_event(rack, RACK_TO_FRM_RACK); 2380 recovery = IN_RECOVERY(tp->t_flags); 2381 counter_u64_add(rack_to_tot, 1); 2382 if (rack->r_state && (rack->r_state != tp->t_state)) 2383 rack_set_state(tp, rack); 2384 rsm = rack_check_recovery_mode(tp, cts); 2385 if (rsm) { 2386 uint32_t rtt; 2387 2388 rtt = rack->rc_rack_rtt; 2389 if (rtt == 0) 2390 rtt = 1; 2391 if ((recovery == 0) && 2392 (rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg)) { 2393 /* 2394 * The rack-timeout that enter's us into recovery 2395 * will force out one MSS and set us up so that we 2396 * can do one more send in 2*rtt (transitioning the 2397 * rack timeout into a rack-tlp). 2398 */ 2399 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 2400 } else if ((rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg) && 2401 ((rsm->r_end - rsm->r_start) > rack->r_ctl.rc_prr_sndcnt)) { 2402 /* 2403 * When a rack timer goes, we have to send at 2404 * least one segment. They will be paced a min of 1ms 2405 * apart via the next rack timer (or further 2406 * if the rack timer dictates it). 2407 */ 2408 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 2409 } 2410 } else { 2411 /* This is a case that should happen rarely if ever */ 2412 counter_u64_add(rack_tlp_does_nada, 1); 2413 #ifdef TCP_BLACKBOX 2414 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true); 2415 #endif 2416 rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 2417 } 2418 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 2419 return (0); 2420 } 2421 2422 /* 2423 * TLP Timer, here we simply setup what segment we want to 2424 * have the TLP expire on, the normal rack_output() will then 2425 * send it out. 2426 * 2427 * We return 1, saying don't proceed with rack_output only 2428 * when all timers have been stopped (destroyed PCB?). 2429 */ 2430 static int 2431 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2432 { 2433 /* 2434 * Tail Loss Probe. 2435 */ 2436 struct rack_sendmap *rsm = NULL; 2437 struct socket *so; 2438 uint32_t amm, old_prr_snd = 0; 2439 uint32_t out, avail; 2440 2441 if (tp->t_timers->tt_flags & TT_STOPPED) { 2442 return (1); 2443 } 2444 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 2445 /* Its not time yet */ 2446 return (0); 2447 } 2448 if (rack_progress_timeout_check(tp)) { 2449 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 2450 return (1); 2451 } 2452 /* 2453 * A TLP timer has expired. We have been idle for 2 rtts. So we now 2454 * need to figure out how to force a full MSS segment out. 2455 */ 2456 rack_log_to_event(rack, RACK_TO_FRM_TLP); 2457 counter_u64_add(rack_tlp_tot, 1); 2458 if (rack->r_state && (rack->r_state != tp->t_state)) 2459 rack_set_state(tp, rack); 2460 so = tp->t_inpcb->inp_socket; 2461 avail = sbavail(&so->so_snd); 2462 out = tp->snd_max - tp->snd_una; 2463 rack->rc_timer_up = 1; 2464 /* 2465 * If we are in recovery we can jazz out a segment if new data is 2466 * present simply by setting rc_prr_sndcnt to a segment. 2467 */ 2468 if ((avail > out) && 2469 ((rack_always_send_oldest == 0) || (TAILQ_EMPTY(&rack->r_ctl.rc_tmap)))) { 2470 /* New data is available */ 2471 amm = avail - out; 2472 if (amm > tp->t_maxseg) { 2473 amm = tp->t_maxseg; 2474 } else if ((amm < tp->t_maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 2475 /* not enough to fill a MTU and no-delay is off */ 2476 goto need_retran; 2477 } 2478 if (IN_RECOVERY(tp->t_flags)) { 2479 /* Unlikely */ 2480 old_prr_snd = rack->r_ctl.rc_prr_sndcnt; 2481 if (out + amm <= tp->snd_wnd) 2482 rack->r_ctl.rc_prr_sndcnt = amm; 2483 else 2484 goto need_retran; 2485 } else { 2486 /* Set the send-new override */ 2487 if (out + amm <= tp->snd_wnd) 2488 rack->r_ctl.rc_tlp_new_data = amm; 2489 else 2490 goto need_retran; 2491 } 2492 rack->r_ctl.rc_tlp_seg_send_cnt = 0; 2493 rack->r_ctl.rc_last_tlp_seq = tp->snd_max; 2494 rack->r_ctl.rc_tlpsend = NULL; 2495 counter_u64_add(rack_tlp_newdata, 1); 2496 goto send; 2497 } 2498 need_retran: 2499 /* 2500 * Ok we need to arrange the last un-acked segment to be re-sent, or 2501 * optionally the first un-acked segment. 2502 */ 2503 if (rack_always_send_oldest) 2504 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 2505 else { 2506 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next); 2507 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) { 2508 rsm = rack_find_high_nonack(rack, rsm); 2509 } 2510 } 2511 if (rsm == NULL) { 2512 counter_u64_add(rack_tlp_does_nada, 1); 2513 #ifdef TCP_BLACKBOX 2514 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true); 2515 #endif 2516 goto out; 2517 } 2518 if ((rsm->r_end - rsm->r_start) > tp->t_maxseg) { 2519 /* 2520 * We need to split this the last segment in two. 2521 */ 2522 int32_t idx; 2523 struct rack_sendmap *nrsm; 2524 2525 nrsm = rack_alloc(rack); 2526 if (nrsm == NULL) { 2527 /* 2528 * No memory to split, we will just exit and punt 2529 * off to the RXT timer. 2530 */ 2531 counter_u64_add(rack_tlp_does_nada, 1); 2532 goto out; 2533 } 2534 nrsm->r_start = (rsm->r_end - tp->t_maxseg); 2535 nrsm->r_end = rsm->r_end; 2536 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 2537 nrsm->r_flags = rsm->r_flags; 2538 nrsm->r_sndcnt = rsm->r_sndcnt; 2539 nrsm->r_rtr_bytes = 0; 2540 rsm->r_end = nrsm->r_start; 2541 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 2542 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 2543 } 2544 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next); 2545 if (rsm->r_in_tmap) { 2546 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 2547 nrsm->r_in_tmap = 1; 2548 } 2549 rsm->r_flags &= (~RACK_HAS_FIN); 2550 rsm = nrsm; 2551 } 2552 rack->r_ctl.rc_tlpsend = rsm; 2553 rack->r_ctl.rc_tlp_rtx_out = 1; 2554 if (rsm->r_start == rack->r_ctl.rc_last_tlp_seq) { 2555 rack->r_ctl.rc_tlp_seg_send_cnt++; 2556 tp->t_rxtshift++; 2557 } else { 2558 rack->r_ctl.rc_last_tlp_seq = rsm->r_start; 2559 rack->r_ctl.rc_tlp_seg_send_cnt = 1; 2560 } 2561 send: 2562 rack->r_ctl.rc_tlp_send_cnt++; 2563 if (rack->r_ctl.rc_tlp_send_cnt > rack_tlp_max_resend) { 2564 /* 2565 * Can't [re]/transmit a segment we have not heard from the 2566 * peer in max times. We need the retransmit timer to take 2567 * over. 2568 */ 2569 restore: 2570 rack->r_ctl.rc_tlpsend = NULL; 2571 if (rsm) 2572 rsm->r_flags &= ~RACK_TLP; 2573 rack->r_ctl.rc_prr_sndcnt = old_prr_snd; 2574 counter_u64_add(rack_tlp_retran_fail, 1); 2575 goto out; 2576 } else if (rsm) { 2577 rsm->r_flags |= RACK_TLP; 2578 } 2579 if (rsm && (rsm->r_start == rack->r_ctl.rc_last_tlp_seq) && 2580 (rack->r_ctl.rc_tlp_seg_send_cnt > rack_tlp_max_resend)) { 2581 /* 2582 * We don't want to send a single segment more than the max 2583 * either. 2584 */ 2585 goto restore; 2586 } 2587 rack->r_timer_override = 1; 2588 rack->r_tlp_running = 1; 2589 rack->rc_tlp_in_progress = 1; 2590 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 2591 return (0); 2592 out: 2593 rack->rc_timer_up = 0; 2594 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 2595 return (0); 2596 } 2597 2598 /* 2599 * Delayed ack Timer, here we simply need to setup the 2600 * ACK_NOW flag and remove the DELACK flag. From there 2601 * the output routine will send the ack out. 2602 * 2603 * We only return 1, saying don't proceed, if all timers 2604 * are stopped (destroyed PCB?). 2605 */ 2606 static int 2607 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2608 { 2609 if (tp->t_timers->tt_flags & TT_STOPPED) { 2610 return (1); 2611 } 2612 rack_log_to_event(rack, RACK_TO_FRM_DELACK); 2613 tp->t_flags &= ~TF_DELACK; 2614 tp->t_flags |= TF_ACKNOW; 2615 TCPSTAT_INC(tcps_delack); 2616 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 2617 return (0); 2618 } 2619 2620 /* 2621 * Persists timer, here we simply need to setup the 2622 * FORCE-DATA flag the output routine will send 2623 * the one byte send. 2624 * 2625 * We only return 1, saying don't proceed, if all timers 2626 * are stopped (destroyed PCB?). 2627 */ 2628 static int 2629 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2630 { 2631 struct inpcb *inp; 2632 int32_t retval = 0; 2633 2634 inp = tp->t_inpcb; 2635 2636 if (tp->t_timers->tt_flags & TT_STOPPED) { 2637 return (1); 2638 } 2639 if (rack->rc_in_persist == 0) 2640 return (0); 2641 if (rack_progress_timeout_check(tp)) { 2642 tcp_set_inp_to_drop(inp, ETIMEDOUT); 2643 return (1); 2644 } 2645 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 2646 /* 2647 * Persistence timer into zero window. Force a byte to be output, if 2648 * possible. 2649 */ 2650 TCPSTAT_INC(tcps_persisttimeo); 2651 /* 2652 * Hack: if the peer is dead/unreachable, we do not time out if the 2653 * window is closed. After a full backoff, drop the connection if 2654 * the idle time (no responses to probes) reaches the maximum 2655 * backoff that we would use if retransmitting. 2656 */ 2657 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 2658 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 2659 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 2660 TCPSTAT_INC(tcps_persistdrop); 2661 retval = 1; 2662 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 2663 goto out; 2664 } 2665 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) && 2666 tp->snd_una == tp->snd_max) 2667 rack_exit_persist(tp, rack); 2668 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 2669 /* 2670 * If the user has closed the socket then drop a persisting 2671 * connection after a much reduced timeout. 2672 */ 2673 if (tp->t_state > TCPS_CLOSE_WAIT && 2674 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 2675 retval = 1; 2676 TCPSTAT_INC(tcps_persistdrop); 2677 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 2678 goto out; 2679 } 2680 tp->t_flags |= TF_FORCEDATA; 2681 out: 2682 rack_log_to_event(rack, RACK_TO_FRM_PERSIST); 2683 return (retval); 2684 } 2685 2686 /* 2687 * If a keepalive goes off, we had no other timers 2688 * happening. We always return 1 here since this 2689 * routine either drops the connection or sends 2690 * out a segment with respond. 2691 */ 2692 static int 2693 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2694 { 2695 struct tcptemp *t_template; 2696 struct inpcb *inp; 2697 2698 if (tp->t_timers->tt_flags & TT_STOPPED) { 2699 return (1); 2700 } 2701 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 2702 inp = tp->t_inpcb; 2703 rack_log_to_event(rack, RACK_TO_FRM_KEEP); 2704 /* 2705 * Keep-alive timer went off; send something or drop connection if 2706 * idle for too long. 2707 */ 2708 TCPSTAT_INC(tcps_keeptimeo); 2709 if (tp->t_state < TCPS_ESTABLISHED) 2710 goto dropit; 2711 if ((tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 2712 tp->t_state <= TCPS_CLOSING) { 2713 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 2714 goto dropit; 2715 /* 2716 * Send a packet designed to force a response if the peer is 2717 * up and reachable: either an ACK if the connection is 2718 * still alive, or an RST if the peer has closed the 2719 * connection due to timeout or reboot. Using sequence 2720 * number tp->snd_una-1 causes the transmitted zero-length 2721 * segment to lie outside the receive window; by the 2722 * protocol spec, this requires the correspondent TCP to 2723 * respond. 2724 */ 2725 TCPSTAT_INC(tcps_keepprobe); 2726 t_template = tcpip_maketemplate(inp); 2727 if (t_template) { 2728 tcp_respond(tp, t_template->tt_ipgen, 2729 &t_template->tt_t, (struct mbuf *)NULL, 2730 tp->rcv_nxt, tp->snd_una - 1, 0); 2731 free(t_template, M_TEMP); 2732 } 2733 } 2734 rack_start_hpts_timer(rack, tp, cts, __LINE__, 0, 0, 0); 2735 return (1); 2736 dropit: 2737 TCPSTAT_INC(tcps_keepdrops); 2738 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 2739 return (1); 2740 } 2741 2742 /* 2743 * Retransmit helper function, clear up all the ack 2744 * flags and take care of important book keeping. 2745 */ 2746 static void 2747 rack_remxt_tmr(struct tcpcb *tp) 2748 { 2749 /* 2750 * The retransmit timer went off, all sack'd blocks must be 2751 * un-acked. 2752 */ 2753 struct rack_sendmap *rsm, *trsm = NULL; 2754 struct tcp_rack *rack; 2755 int32_t cnt = 0; 2756 2757 rack = (struct tcp_rack *)tp->t_fb_ptr; 2758 rack_timer_cancel(tp, rack, tcp_ts_getticks(), __LINE__); 2759 rack_log_to_event(rack, RACK_TO_FRM_TMR); 2760 if (rack->r_state && (rack->r_state != tp->t_state)) 2761 rack_set_state(tp, rack); 2762 /* 2763 * Ideally we would like to be able to 2764 * mark SACK-PASS on anything not acked here. 2765 * However, if we do that we would burst out 2766 * all that data 1ms apart. This would be unwise, 2767 * so for now we will just let the normal rxt timer 2768 * and tlp timer take care of it. 2769 */ 2770 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) { 2771 if (rsm->r_flags & RACK_ACKED) { 2772 cnt++; 2773 rsm->r_sndcnt = 0; 2774 if (rsm->r_in_tmap == 0) { 2775 /* We must re-add it back to the tlist */ 2776 if (trsm == NULL) { 2777 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 2778 } else { 2779 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext); 2780 } 2781 rsm->r_in_tmap = 1; 2782 trsm = rsm; 2783 } 2784 } 2785 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS); 2786 } 2787 /* Clear the count (we just un-acked them) */ 2788 rack->r_ctl.rc_sacked = 0; 2789 /* Clear the tlp rtx mark */ 2790 rack->r_ctl.rc_tlp_rtx_out = 0; 2791 rack->r_ctl.rc_tlp_seg_send_cnt = 0; 2792 rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_map); 2793 /* Setup so we send one segment */ 2794 if (rack->r_ctl.rc_prr_sndcnt < tp->t_maxseg) 2795 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 2796 rack->r_timer_override = 1; 2797 } 2798 2799 /* 2800 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 2801 * we will setup to retransmit the lowest seq number outstanding. 2802 */ 2803 static int 2804 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 2805 { 2806 int32_t rexmt; 2807 struct inpcb *inp; 2808 int32_t retval = 0; 2809 2810 inp = tp->t_inpcb; 2811 if (tp->t_timers->tt_flags & TT_STOPPED) { 2812 return (1); 2813 } 2814 if (rack_progress_timeout_check(tp)) { 2815 tcp_set_inp_to_drop(inp, ETIMEDOUT); 2816 return (1); 2817 } 2818 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 2819 if (TCPS_HAVEESTABLISHED(tp->t_state) && 2820 (tp->snd_una == tp->snd_max)) { 2821 /* Nothing outstanding .. nothing to do */ 2822 return (0); 2823 } 2824 /* 2825 * Retransmission timer went off. Message has not been acked within 2826 * retransmit interval. Back off to a longer retransmit interval 2827 * and retransmit one segment. 2828 */ 2829 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 2830 tp->t_rxtshift = TCP_MAXRXTSHIFT; 2831 TCPSTAT_INC(tcps_timeoutdrop); 2832 retval = 1; 2833 tcp_set_inp_to_drop(rack->rc_inp, 2834 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT)); 2835 goto out; 2836 } 2837 rack_remxt_tmr(tp); 2838 if (tp->t_state == TCPS_SYN_SENT) { 2839 /* 2840 * If the SYN was retransmitted, indicate CWND to be limited 2841 * to 1 segment in cc_conn_init(). 2842 */ 2843 tp->snd_cwnd = 1; 2844 } else if (tp->t_rxtshift == 1) { 2845 /* 2846 * first retransmit; record ssthresh and cwnd so they can be 2847 * recovered if this turns out to be a "bad" retransmit. A 2848 * retransmit is considered "bad" if an ACK for this segment 2849 * is received within RTT/2 interval; the assumption here is 2850 * that the ACK was already in flight. See "On Estimating 2851 * End-to-End Network Path Properties" by Allman and Paxson 2852 * for more details. 2853 */ 2854 tp->snd_cwnd_prev = tp->snd_cwnd; 2855 tp->snd_ssthresh_prev = tp->snd_ssthresh; 2856 tp->snd_recover_prev = tp->snd_recover; 2857 if (IN_FASTRECOVERY(tp->t_flags)) 2858 tp->t_flags |= TF_WASFRECOVERY; 2859 else 2860 tp->t_flags &= ~TF_WASFRECOVERY; 2861 if (IN_CONGRECOVERY(tp->t_flags)) 2862 tp->t_flags |= TF_WASCRECOVERY; 2863 else 2864 tp->t_flags &= ~TF_WASCRECOVERY; 2865 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 2866 tp->t_flags |= TF_PREVVALID; 2867 } else 2868 tp->t_flags &= ~TF_PREVVALID; 2869 TCPSTAT_INC(tcps_rexmttimeo); 2870 if ((tp->t_state == TCPS_SYN_SENT) || 2871 (tp->t_state == TCPS_SYN_RECEIVED)) 2872 rexmt = MSEC_2_TICKS(RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift]); 2873 else 2874 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 2875 TCPT_RANGESET(tp->t_rxtcur, rexmt, 2876 max(MSEC_2_TICKS(rack_rto_min), rexmt), 2877 MSEC_2_TICKS(rack_rto_max)); 2878 /* 2879 * We enter the path for PLMTUD if connection is established or, if 2880 * connection is FIN_WAIT_1 status, reason for the last is that if 2881 * amount of data we send is very small, we could send it in couple 2882 * of packets and process straight to FIN. In that case we won't 2883 * catch ESTABLISHED state. 2884 */ 2885 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED)) 2886 || (tp->t_state == TCPS_FIN_WAIT_1))) { 2887 #ifdef INET6 2888 int32_t isipv6; 2889 #endif 2890 2891 /* 2892 * Idea here is that at each stage of mtu probe (usually, 2893 * 1448 -> 1188 -> 524) should be given 2 chances to recover 2894 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 2895 * should take care of that. 2896 */ 2897 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 2898 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 2899 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 2900 tp->t_rxtshift % 2 == 0)) { 2901 /* 2902 * Enter Path MTU Black-hole Detection mechanism: - 2903 * Disable Path MTU Discovery (IP "DF" bit). - 2904 * Reduce MTU to lower value than what we negotiated 2905 * with peer. 2906 */ 2907 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 2908 /* Record that we may have found a black hole. */ 2909 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 2910 /* Keep track of previous MSS. */ 2911 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 2912 } 2913 2914 /* 2915 * Reduce the MSS to blackhole value or to the 2916 * default in an attempt to retransmit. 2917 */ 2918 #ifdef INET6 2919 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0; 2920 if (isipv6 && 2921 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 2922 /* Use the sysctl tuneable blackhole MSS. */ 2923 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 2924 TCPSTAT_INC(tcps_pmtud_blackhole_activated); 2925 } else if (isipv6) { 2926 /* Use the default MSS. */ 2927 tp->t_maxseg = V_tcp_v6mssdflt; 2928 /* 2929 * Disable Path MTU Discovery when we switch 2930 * to minmss. 2931 */ 2932 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 2933 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 2934 } 2935 #endif 2936 #if defined(INET6) && defined(INET) 2937 else 2938 #endif 2939 #ifdef INET 2940 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 2941 /* Use the sysctl tuneable blackhole MSS. */ 2942 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 2943 TCPSTAT_INC(tcps_pmtud_blackhole_activated); 2944 } else { 2945 /* Use the default MSS. */ 2946 tp->t_maxseg = V_tcp_mssdflt; 2947 /* 2948 * Disable Path MTU Discovery when we switch 2949 * to minmss. 2950 */ 2951 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 2952 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 2953 } 2954 #endif 2955 } else { 2956 /* 2957 * If further retransmissions are still unsuccessful 2958 * with a lowered MTU, maybe this isn't a blackhole 2959 * and we restore the previous MSS and blackhole 2960 * detection flags. The limit '6' is determined by 2961 * giving each probe stage (1448, 1188, 524) 2 2962 * chances to recover. 2963 */ 2964 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 2965 (tp->t_rxtshift >= 6)) { 2966 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 2967 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 2968 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 2969 TCPSTAT_INC(tcps_pmtud_blackhole_failed); 2970 } 2971 } 2972 } 2973 /* 2974 * Disable RFC1323 and SACK if we haven't got any response to our 2975 * third SYN to work-around some broken terminal servers (most of 2976 * which have hopefully been retired) that have bad VJ header 2977 * compression code which trashes TCP segments containing 2978 * unknown-to-them TCP options. 2979 */ 2980 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 2981 (tp->t_rxtshift == 3)) 2982 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 2983 /* 2984 * If we backed off this far, our srtt estimate is probably bogus. 2985 * Clobber it so we'll take the next rtt measurement as our srtt; 2986 * move the current srtt into rttvar to keep the current retransmit 2987 * times until then. 2988 */ 2989 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 2990 #ifdef INET6 2991 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 2992 in6_losing(tp->t_inpcb); 2993 else 2994 #endif 2995 in_losing(tp->t_inpcb); 2996 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 2997 tp->t_srtt = 0; 2998 } 2999 if (rack_use_sack_filter) 3000 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 3001 tp->snd_recover = tp->snd_max; 3002 tp->t_flags |= TF_ACKNOW; 3003 tp->t_rtttime = 0; 3004 rack_cong_signal(tp, NULL, CC_RTO); 3005 out: 3006 return (retval); 3007 } 3008 3009 static int 3010 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling) 3011 { 3012 int32_t ret = 0; 3013 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 3014 3015 if (timers == 0) { 3016 return (0); 3017 } 3018 if (tp->t_state == TCPS_LISTEN) { 3019 /* no timers on listen sockets */ 3020 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 3021 return (0); 3022 return (1); 3023 } 3024 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 3025 uint32_t left; 3026 3027 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 3028 ret = -1; 3029 rack_log_to_processing(rack, cts, ret, 0); 3030 return (0); 3031 } 3032 if (hpts_calling == 0) { 3033 ret = -2; 3034 rack_log_to_processing(rack, cts, ret, 0); 3035 return (0); 3036 } 3037 /* 3038 * Ok our timer went off early and we are not paced false 3039 * alarm, go back to sleep. 3040 */ 3041 ret = -3; 3042 left = rack->r_ctl.rc_timer_exp - cts; 3043 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left)); 3044 rack_log_to_processing(rack, cts, ret, left); 3045 rack->rc_last_pto_set = 0; 3046 return (1); 3047 } 3048 rack->rc_tmr_stopped = 0; 3049 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 3050 if (timers & PACE_TMR_DELACK) { 3051 ret = rack_timeout_delack(tp, rack, cts); 3052 } else if (timers & PACE_TMR_RACK) { 3053 ret = rack_timeout_rack(tp, rack, cts); 3054 } else if (timers & PACE_TMR_TLP) { 3055 ret = rack_timeout_tlp(tp, rack, cts); 3056 } else if (timers & PACE_TMR_RXT) { 3057 ret = rack_timeout_rxt(tp, rack, cts); 3058 } else if (timers & PACE_TMR_PERSIT) { 3059 ret = rack_timeout_persist(tp, rack, cts); 3060 } else if (timers & PACE_TMR_KEEP) { 3061 ret = rack_timeout_keepalive(tp, rack, cts); 3062 } 3063 rack_log_to_processing(rack, cts, ret, timers); 3064 return (ret); 3065 } 3066 3067 static void 3068 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line) 3069 { 3070 uint8_t hpts_removed = 0; 3071 3072 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 3073 TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) { 3074 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 3075 hpts_removed = 1; 3076 } 3077 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 3078 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 3079 if (rack->rc_inp->inp_in_hpts && 3080 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) { 3081 /* 3082 * Canceling timer's when we have no output being 3083 * paced. We also must remove ourselves from the 3084 * hpts. 3085 */ 3086 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 3087 hpts_removed = 1; 3088 } 3089 rack_log_to_cancel(rack, hpts_removed, line); 3090 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 3091 } 3092 } 3093 3094 static void 3095 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type) 3096 { 3097 return; 3098 } 3099 3100 static int 3101 rack_stopall(struct tcpcb *tp) 3102 { 3103 struct tcp_rack *rack; 3104 rack = (struct tcp_rack *)tp->t_fb_ptr; 3105 rack->t_timers_stopped = 1; 3106 return (0); 3107 } 3108 3109 static void 3110 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta) 3111 { 3112 return; 3113 } 3114 3115 static int 3116 rack_timer_active(struct tcpcb *tp, uint32_t timer_type) 3117 { 3118 return (0); 3119 } 3120 3121 static void 3122 rack_stop_all_timers(struct tcpcb *tp) 3123 { 3124 struct tcp_rack *rack; 3125 3126 /* 3127 * Assure no timers are running. 3128 */ 3129 if (tcp_timer_active(tp, TT_PERSIST)) { 3130 /* We enter in persists, set the flag appropriately */ 3131 rack = (struct tcp_rack *)tp->t_fb_ptr; 3132 rack->rc_in_persist = 1; 3133 } 3134 tcp_timer_suspend(tp, TT_PERSIST); 3135 tcp_timer_suspend(tp, TT_REXMT); 3136 tcp_timer_suspend(tp, TT_KEEP); 3137 tcp_timer_suspend(tp, TT_DELACK); 3138 } 3139 3140 static void 3141 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 3142 struct rack_sendmap *rsm, uint32_t ts) 3143 { 3144 int32_t idx; 3145 3146 rsm->r_rtr_cnt++; 3147 rsm->r_sndcnt++; 3148 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) { 3149 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS; 3150 rsm->r_flags |= RACK_OVERMAX; 3151 } 3152 if ((rsm->r_rtr_cnt > 1) && (rack->r_tlp_running == 0)) { 3153 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 3154 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 3155 } 3156 idx = rsm->r_rtr_cnt - 1; 3157 rsm->r_tim_lastsent[idx] = ts; 3158 if (rsm->r_flags & RACK_ACKED) { 3159 /* Problably MTU discovery messing with us */ 3160 rsm->r_flags &= ~RACK_ACKED; 3161 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 3162 } 3163 if (rsm->r_in_tmap) { 3164 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 3165 } 3166 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 3167 rsm->r_in_tmap = 1; 3168 if (rsm->r_flags & RACK_SACK_PASSED) { 3169 /* We have retransmitted due to the SACK pass */ 3170 rsm->r_flags &= ~RACK_SACK_PASSED; 3171 rsm->r_flags |= RACK_WAS_SACKPASS; 3172 } 3173 /* Update memory for next rtr */ 3174 rack->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 3175 } 3176 3177 3178 static uint32_t 3179 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 3180 struct rack_sendmap *rsm, uint32_t ts, int32_t * lenp) 3181 { 3182 /* 3183 * We (re-)transmitted starting at rsm->r_start for some length 3184 * (possibly less than r_end. 3185 */ 3186 struct rack_sendmap *nrsm; 3187 uint32_t c_end; 3188 int32_t len; 3189 int32_t idx; 3190 3191 len = *lenp; 3192 c_end = rsm->r_start + len; 3193 if (SEQ_GEQ(c_end, rsm->r_end)) { 3194 /* 3195 * We retransmitted the whole piece or more than the whole 3196 * slopping into the next rsm. 3197 */ 3198 rack_update_rsm(tp, rack, rsm, ts); 3199 if (c_end == rsm->r_end) { 3200 *lenp = 0; 3201 return (0); 3202 } else { 3203 int32_t act_len; 3204 3205 /* Hangs over the end return whats left */ 3206 act_len = rsm->r_end - rsm->r_start; 3207 *lenp = (len - act_len); 3208 return (rsm->r_end); 3209 } 3210 /* We don't get out of this block. */ 3211 } 3212 /* 3213 * Here we retransmitted less than the whole thing which means we 3214 * have to split this into what was transmitted and what was not. 3215 */ 3216 nrsm = rack_alloc(rack); 3217 if (nrsm == NULL) { 3218 /* 3219 * We can't get memory, so lets not proceed. 3220 */ 3221 *lenp = 0; 3222 return (0); 3223 } 3224 /* 3225 * So here we are going to take the original rsm and make it what we 3226 * retransmitted. nrsm will be the tail portion we did not 3227 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 3228 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 3229 * 1, 6 and the new piece will be 6, 11. 3230 */ 3231 nrsm->r_start = c_end; 3232 nrsm->r_end = rsm->r_end; 3233 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 3234 nrsm->r_flags = rsm->r_flags; 3235 nrsm->r_sndcnt = rsm->r_sndcnt; 3236 nrsm->r_rtr_bytes = 0; 3237 rsm->r_end = c_end; 3238 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 3239 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 3240 } 3241 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next); 3242 if (rsm->r_in_tmap) { 3243 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 3244 nrsm->r_in_tmap = 1; 3245 } 3246 rsm->r_flags &= (~RACK_HAS_FIN); 3247 rack_update_rsm(tp, rack, rsm, ts); 3248 *lenp = 0; 3249 return (0); 3250 } 3251 3252 3253 static void 3254 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 3255 uint32_t seq_out, uint8_t th_flags, int32_t err, uint32_t ts, 3256 uint8_t pass, struct rack_sendmap *hintrsm) 3257 { 3258 struct tcp_rack *rack; 3259 struct rack_sendmap *rsm, *nrsm; 3260 register uint32_t snd_max, snd_una; 3261 int32_t idx; 3262 3263 /* 3264 * Add to the RACK log of packets in flight or retransmitted. If 3265 * there is a TS option we will use the TS echoed, if not we will 3266 * grab a TS. 3267 * 3268 * Retransmissions will increment the count and move the ts to its 3269 * proper place. Note that if options do not include TS's then we 3270 * won't be able to effectively use the ACK for an RTT on a retran. 3271 * 3272 * Notes about r_start and r_end. Lets consider a send starting at 3273 * sequence 1 for 10 bytes. In such an example the r_start would be 3274 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 3275 * This means that r_end is actually the first sequence for the next 3276 * slot (11). 3277 * 3278 */ 3279 /* 3280 * If err is set what do we do XXXrrs? should we not add the thing? 3281 * -- i.e. return if err != 0 or should we pretend we sent it? -- 3282 * i.e. proceed with add ** do this for now. 3283 */ 3284 INP_WLOCK_ASSERT(tp->t_inpcb); 3285 if (err) 3286 /* 3287 * We don't log errors -- we could but snd_max does not 3288 * advance in this case either. 3289 */ 3290 return; 3291 3292 if (th_flags & TH_RST) { 3293 /* 3294 * We don't log resets and we return immediately from 3295 * sending 3296 */ 3297 return; 3298 } 3299 rack = (struct tcp_rack *)tp->t_fb_ptr; 3300 snd_una = tp->snd_una; 3301 if (SEQ_LEQ((seq_out + len), snd_una)) { 3302 /* Are sending an old segment to induce an ack (keep-alive)? */ 3303 return; 3304 } 3305 if (SEQ_LT(seq_out, snd_una)) { 3306 /* huh? should we panic? */ 3307 uint32_t end; 3308 3309 end = seq_out + len; 3310 seq_out = snd_una; 3311 len = end - seq_out; 3312 } 3313 snd_max = tp->snd_max; 3314 if (th_flags & (TH_SYN | TH_FIN)) { 3315 /* 3316 * The call to rack_log_output is made before bumping 3317 * snd_max. This means we can record one extra byte on a SYN 3318 * or FIN if seq_out is adding more on and a FIN is present 3319 * (and we are not resending). 3320 */ 3321 if (th_flags & TH_SYN) 3322 len++; 3323 if (th_flags & TH_FIN) 3324 len++; 3325 if (SEQ_LT(snd_max, tp->snd_nxt)) { 3326 /* 3327 * The add/update as not been done for the FIN/SYN 3328 * yet. 3329 */ 3330 snd_max = tp->snd_nxt; 3331 } 3332 } 3333 if (len == 0) { 3334 /* We don't log zero window probes */ 3335 return; 3336 } 3337 rack->r_ctl.rc_time_last_sent = ts; 3338 if (IN_RECOVERY(tp->t_flags)) { 3339 rack->r_ctl.rc_prr_out += len; 3340 } 3341 /* First question is it a retransmission? */ 3342 if (seq_out == snd_max) { 3343 again: 3344 rsm = rack_alloc(rack); 3345 if (rsm == NULL) { 3346 /* 3347 * Hmm out of memory and the tcb got destroyed while 3348 * we tried to wait. 3349 */ 3350 #ifdef INVARIANTS 3351 panic("Out of memory when we should not be rack:%p", rack); 3352 #endif 3353 return; 3354 } 3355 if (th_flags & TH_FIN) { 3356 rsm->r_flags = RACK_HAS_FIN; 3357 } else { 3358 rsm->r_flags = 0; 3359 } 3360 rsm->r_tim_lastsent[0] = ts; 3361 rsm->r_rtr_cnt = 1; 3362 rsm->r_rtr_bytes = 0; 3363 if (th_flags & TH_SYN) { 3364 /* The data space is one beyond snd_una */ 3365 rsm->r_start = seq_out + 1; 3366 rsm->r_end = rsm->r_start + (len - 1); 3367 } else { 3368 /* Normal case */ 3369 rsm->r_start = seq_out; 3370 rsm->r_end = rsm->r_start + len; 3371 } 3372 rsm->r_sndcnt = 0; 3373 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_map, rsm, r_next); 3374 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 3375 rsm->r_in_tmap = 1; 3376 return; 3377 } 3378 /* 3379 * If we reach here its a retransmission and we need to find it. 3380 */ 3381 more: 3382 if (hintrsm && (hintrsm->r_start == seq_out)) { 3383 rsm = hintrsm; 3384 hintrsm = NULL; 3385 } else if (rack->r_ctl.rc_next) { 3386 /* We have a hint from a previous run */ 3387 rsm = rack->r_ctl.rc_next; 3388 } else { 3389 /* No hints sorry */ 3390 rsm = NULL; 3391 } 3392 if ((rsm) && (rsm->r_start == seq_out)) { 3393 /* 3394 * We used rc_next or hintrsm to retransmit, hopefully the 3395 * likely case. 3396 */ 3397 seq_out = rack_update_entry(tp, rack, rsm, ts, &len); 3398 if (len == 0) { 3399 return; 3400 } else { 3401 goto more; 3402 } 3403 } 3404 /* Ok it was not the last pointer go through it the hard way. */ 3405 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) { 3406 if (rsm->r_start == seq_out) { 3407 seq_out = rack_update_entry(tp, rack, rsm, ts, &len); 3408 rack->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 3409 if (len == 0) { 3410 return; 3411 } else { 3412 continue; 3413 } 3414 } 3415 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 3416 /* Transmitted within this piece */ 3417 /* 3418 * Ok we must split off the front and then let the 3419 * update do the rest 3420 */ 3421 nrsm = rack_alloc(rack); 3422 if (nrsm == NULL) { 3423 #ifdef INVARIANTS 3424 panic("Ran out of memory that was preallocated? rack:%p", rack); 3425 #endif 3426 rack_update_rsm(tp, rack, rsm, ts); 3427 return; 3428 } 3429 /* 3430 * copy rsm to nrsm and then trim the front of rsm 3431 * to not include this part. 3432 */ 3433 nrsm->r_start = seq_out; 3434 nrsm->r_end = rsm->r_end; 3435 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 3436 nrsm->r_flags = rsm->r_flags; 3437 nrsm->r_sndcnt = rsm->r_sndcnt; 3438 nrsm->r_rtr_bytes = 0; 3439 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 3440 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 3441 } 3442 rsm->r_end = nrsm->r_start; 3443 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next); 3444 if (rsm->r_in_tmap) { 3445 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 3446 nrsm->r_in_tmap = 1; 3447 } 3448 rsm->r_flags &= (~RACK_HAS_FIN); 3449 seq_out = rack_update_entry(tp, rack, nrsm, ts, &len); 3450 if (len == 0) { 3451 return; 3452 } 3453 } 3454 } 3455 /* 3456 * Hmm not found in map did they retransmit both old and on into the 3457 * new? 3458 */ 3459 if (seq_out == tp->snd_max) { 3460 goto again; 3461 } else if (SEQ_LT(seq_out, tp->snd_max)) { 3462 #ifdef INVARIANTS 3463 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 3464 seq_out, len, tp->snd_una, tp->snd_max); 3465 printf("Starting Dump of all rack entries\n"); 3466 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_map, r_next) { 3467 printf("rsm:%p start:%u end:%u\n", 3468 rsm, rsm->r_start, rsm->r_end); 3469 } 3470 printf("Dump complete\n"); 3471 panic("seq_out not found rack:%p tp:%p", 3472 rack, tp); 3473 #endif 3474 } else { 3475 #ifdef INVARIANTS 3476 /* 3477 * Hmm beyond sndmax? (only if we are using the new rtt-pack 3478 * flag) 3479 */ 3480 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 3481 seq_out, len, tp->snd_max, tp); 3482 #endif 3483 } 3484 } 3485 3486 /* 3487 * Record one of the RTT updates from an ack into 3488 * our sample structure. 3489 */ 3490 static void 3491 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt) 3492 { 3493 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 3494 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) { 3495 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt; 3496 } 3497 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 3498 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) { 3499 rack->r_ctl.rack_rs.rs_rtt_highest = rtt; 3500 } 3501 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID; 3502 rack->r_ctl.rack_rs.rs_rtt_tot += rtt; 3503 rack->r_ctl.rack_rs.rs_rtt_cnt++; 3504 } 3505 3506 /* 3507 * Collect new round-trip time estimate 3508 * and update averages and current timeout. 3509 */ 3510 static void 3511 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp) 3512 { 3513 int32_t delta; 3514 uint32_t o_srtt, o_var; 3515 int32_t rtt; 3516 3517 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) 3518 /* No valid sample */ 3519 return; 3520 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) { 3521 /* We are to use the lowest RTT seen in a single ack */ 3522 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest; 3523 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) { 3524 /* We are to use the highest RTT seen in a single ack */ 3525 rtt = rack->r_ctl.rack_rs.rs_rtt_highest; 3526 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) { 3527 /* We are to use the average RTT seen in a single ack */ 3528 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot / 3529 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt); 3530 } else { 3531 #ifdef INVARIANTS 3532 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method); 3533 #endif 3534 return; 3535 } 3536 if (rtt == 0) 3537 rtt = 1; 3538 rack_log_rtt_sample(rack, rtt); 3539 o_srtt = tp->t_srtt; 3540 o_var = tp->t_rttvar; 3541 rack = (struct tcp_rack *)tp->t_fb_ptr; 3542 if (tp->t_srtt != 0) { 3543 /* 3544 * srtt is stored as fixed point with 5 bits after the 3545 * binary point (i.e., scaled by 8). The following magic is 3546 * equivalent to the smoothing algorithm in rfc793 with an 3547 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 3548 * Adjust rtt to origin 0. 3549 */ 3550 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3551 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3552 3553 tp->t_srtt += delta; 3554 if (tp->t_srtt <= 0) 3555 tp->t_srtt = 1; 3556 3557 /* 3558 * We accumulate a smoothed rtt variance (actually, a 3559 * smoothed mean difference), then set the retransmit timer 3560 * to smoothed rtt + 4 times the smoothed variance. rttvar 3561 * is stored as fixed point with 4 bits after the binary 3562 * point (scaled by 16). The following is equivalent to 3563 * rfc793 smoothing with an alpha of .75 (rttvar = 3564 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 3565 * wired-in beta. 3566 */ 3567 if (delta < 0) 3568 delta = -delta; 3569 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3570 tp->t_rttvar += delta; 3571 if (tp->t_rttvar <= 0) 3572 tp->t_rttvar = 1; 3573 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 3574 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3575 } else { 3576 /* 3577 * No rtt measurement yet - use the unsmoothed rtt. Set the 3578 * variance to half the rtt (so our first retransmit happens 3579 * at 3*rtt). 3580 */ 3581 tp->t_srtt = rtt << TCP_RTT_SHIFT; 3582 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 3583 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3584 } 3585 TCPSTAT_INC(tcps_rttupdated); 3586 rack_log_rtt_upd(tp, rack, rtt, o_srtt, o_var); 3587 tp->t_rttupdated++; 3588 #ifdef NETFLIX_STATS 3589 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt)); 3590 #endif 3591 tp->t_rxtshift = 0; 3592 3593 /* 3594 * the retransmit should happen at rtt + 4 * rttvar. Because of the 3595 * way we do the smoothing, srtt and rttvar will each average +1/2 3596 * tick of bias. When we compute the retransmit timer, we want 1/2 3597 * tick of rounding and 1 extra tick because of +-1/2 tick 3598 * uncertainty in the firing of the timer. The bias will give us 3599 * exactly the 1.5 tick we need. But, because the bias is 3600 * statistical, we have to test that we don't drop below the minimum 3601 * feasible timer (which is 2 ticks). 3602 */ 3603 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3604 max(MSEC_2_TICKS(rack_rto_min), rtt + 2), MSEC_2_TICKS(rack_rto_max)); 3605 tp->t_softerror = 0; 3606 } 3607 3608 static void 3609 rack_earlier_retran(struct tcpcb *tp, struct rack_sendmap *rsm, 3610 uint32_t t, uint32_t cts) 3611 { 3612 /* 3613 * For this RSM, we acknowledged the data from a previous 3614 * transmission, not the last one we made. This means we did a false 3615 * retransmit. 3616 */ 3617 struct tcp_rack *rack; 3618 3619 if (rsm->r_flags & RACK_HAS_FIN) { 3620 /* 3621 * The sending of the FIN often is multiple sent when we 3622 * have everything outstanding ack'd. We ignore this case 3623 * since its over now. 3624 */ 3625 return; 3626 } 3627 if (rsm->r_flags & RACK_TLP) { 3628 /* 3629 * We expect TLP's to have this occur. 3630 */ 3631 return; 3632 } 3633 rack = (struct tcp_rack *)tp->t_fb_ptr; 3634 /* should we undo cc changes and exit recovery? */ 3635 if (IN_RECOVERY(tp->t_flags)) { 3636 if (rack->r_ctl.rc_rsm_start == rsm->r_start) { 3637 /* 3638 * Undo what we ratched down and exit recovery if 3639 * possible 3640 */ 3641 EXIT_RECOVERY(tp->t_flags); 3642 tp->snd_recover = tp->snd_una; 3643 if (rack->r_ctl.rc_cwnd_at > tp->snd_cwnd) 3644 tp->snd_cwnd = rack->r_ctl.rc_cwnd_at; 3645 if (rack->r_ctl.rc_ssthresh_at > tp->snd_ssthresh) 3646 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at; 3647 } 3648 } 3649 if (rsm->r_flags & RACK_WAS_SACKPASS) { 3650 /* 3651 * We retransmitted based on a sack and the earlier 3652 * retransmission ack'd it - re-ordering is occuring. 3653 */ 3654 counter_u64_add(rack_reorder_seen, 1); 3655 rack->r_ctl.rc_reorder_ts = cts; 3656 } 3657 counter_u64_add(rack_badfr, 1); 3658 counter_u64_add(rack_badfr_bytes, (rsm->r_end - rsm->r_start)); 3659 } 3660 3661 3662 static int 3663 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 3664 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type) 3665 { 3666 int32_t i; 3667 uint32_t t; 3668 3669 if (rsm->r_flags & RACK_ACKED) 3670 /* Already done */ 3671 return (0); 3672 3673 3674 if ((rsm->r_rtr_cnt == 1) || 3675 ((ack_type == CUM_ACKED) && 3676 (to->to_flags & TOF_TS) && 3677 (to->to_tsecr) && 3678 (rsm->r_tim_lastsent[rsm->r_rtr_cnt - 1] == to->to_tsecr)) 3679 ) { 3680 /* 3681 * We will only find a matching timestamp if its cum-acked. 3682 * But if its only one retransmission its for-sure matching 3683 * :-) 3684 */ 3685 t = cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 3686 if ((int)t <= 0) 3687 t = 1; 3688 if (!tp->t_rttlow || tp->t_rttlow > t) 3689 tp->t_rttlow = t; 3690 if (!rack->r_ctl.rc_rack_min_rtt || 3691 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 3692 rack->r_ctl.rc_rack_min_rtt = t; 3693 if (rack->r_ctl.rc_rack_min_rtt == 0) { 3694 rack->r_ctl.rc_rack_min_rtt = 1; 3695 } 3696 } 3697 tcp_rack_xmit_timer(rack, TCP_TS_TO_TICKS(t) + 1); 3698 if ((rsm->r_flags & RACK_TLP) && 3699 (!IN_RECOVERY(tp->t_flags))) { 3700 /* Segment was a TLP and our retrans matched */ 3701 if (rack->r_ctl.rc_tlp_cwnd_reduce) { 3702 rack->r_ctl.rc_rsm_start = tp->snd_max; 3703 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 3704 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 3705 rack_cong_signal(tp, NULL, CC_NDUPACK); 3706 /* 3707 * When we enter recovery we need to assure 3708 * we send one packet. 3709 */ 3710 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 3711 } else 3712 rack->r_ctl.rc_tlp_rtx_out = 0; 3713 } 3714 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) { 3715 /* New more recent rack_tmit_time */ 3716 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 3717 rack->rc_rack_rtt = t; 3718 } 3719 return (1); 3720 } 3721 /* 3722 * We clear the soft/rxtshift since we got an ack. 3723 * There is no assurance we will call the commit() function 3724 * so we need to clear these to avoid incorrect handling. 3725 */ 3726 tp->t_rxtshift = 0; 3727 tp->t_softerror = 0; 3728 if ((to->to_flags & TOF_TS) && 3729 (ack_type == CUM_ACKED) && 3730 (to->to_tsecr) && 3731 ((rsm->r_flags & (RACK_DEFERRED | RACK_OVERMAX)) == 0)) { 3732 /* 3733 * Now which timestamp does it match? In this block the ACK 3734 * must be coming from a previous transmission. 3735 */ 3736 for (i = 0; i < rsm->r_rtr_cnt; i++) { 3737 if (rsm->r_tim_lastsent[i] == to->to_tsecr) { 3738 t = cts - rsm->r_tim_lastsent[i]; 3739 if ((int)t <= 0) 3740 t = 1; 3741 if ((i + 1) < rsm->r_rtr_cnt) { 3742 /* Likely */ 3743 rack_earlier_retran(tp, rsm, t, cts); 3744 } 3745 if (!tp->t_rttlow || tp->t_rttlow > t) 3746 tp->t_rttlow = t; 3747 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 3748 rack->r_ctl.rc_rack_min_rtt = t; 3749 if (rack->r_ctl.rc_rack_min_rtt == 0) { 3750 rack->r_ctl.rc_rack_min_rtt = 1; 3751 } 3752 } 3753 /* 3754 * Note the following calls to 3755 * tcp_rack_xmit_timer() are being commented 3756 * out for now. They give us no more accuracy 3757 * and often lead to a wrong choice. We have 3758 * enough samples that have not been 3759 * retransmitted. I leave the commented out 3760 * code in here in case in the future we 3761 * decide to add it back (though I can't forsee 3762 * doing that). That way we will easily see 3763 * where they need to be placed. 3764 */ 3765 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, 3766 rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) { 3767 /* New more recent rack_tmit_time */ 3768 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 3769 rack->rc_rack_rtt = t; 3770 } 3771 return (1); 3772 } 3773 } 3774 goto ts_not_found; 3775 } else { 3776 /* 3777 * Ok its a SACK block that we retransmitted. or a windows 3778 * machine without timestamps. We can tell nothing from the 3779 * time-stamp since its not there or the time the peer last 3780 * recieved a segment that moved forward its cum-ack point. 3781 */ 3782 ts_not_found: 3783 i = rsm->r_rtr_cnt - 1; 3784 t = cts - rsm->r_tim_lastsent[i]; 3785 if ((int)t <= 0) 3786 t = 1; 3787 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 3788 /* 3789 * We retransmitted and the ack came back in less 3790 * than the smallest rtt we have observed. We most 3791 * likey did an improper retransmit as outlined in 3792 * 4.2 Step 3 point 2 in the rack-draft. 3793 */ 3794 i = rsm->r_rtr_cnt - 2; 3795 t = cts - rsm->r_tim_lastsent[i]; 3796 rack_earlier_retran(tp, rsm, t, cts); 3797 } else if (rack->r_ctl.rc_rack_min_rtt) { 3798 /* 3799 * We retransmitted it and the retransmit did the 3800 * job. 3801 */ 3802 if (!rack->r_ctl.rc_rack_min_rtt || 3803 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 3804 rack->r_ctl.rc_rack_min_rtt = t; 3805 if (rack->r_ctl.rc_rack_min_rtt == 0) { 3806 rack->r_ctl.rc_rack_min_rtt = 1; 3807 } 3808 } 3809 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, rsm->r_tim_lastsent[i])) { 3810 /* New more recent rack_tmit_time */ 3811 rack->r_ctl.rc_rack_tmit_time = rsm->r_tim_lastsent[i]; 3812 rack->rc_rack_rtt = t; 3813 } 3814 return (1); 3815 } 3816 } 3817 return (0); 3818 } 3819 3820 /* 3821 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 3822 */ 3823 static void 3824 rack_log_sack_passed(struct tcpcb *tp, 3825 struct tcp_rack *rack, struct rack_sendmap *rsm) 3826 { 3827 struct rack_sendmap *nrsm; 3828 uint32_t ts; 3829 int32_t idx; 3830 3831 idx = rsm->r_rtr_cnt - 1; 3832 ts = rsm->r_tim_lastsent[idx]; 3833 nrsm = rsm; 3834 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap, 3835 rack_head, r_tnext) { 3836 if (nrsm == rsm) { 3837 /* Skip orginal segment he is acked */ 3838 continue; 3839 } 3840 if (nrsm->r_flags & RACK_ACKED) { 3841 /* Skip ack'd segments */ 3842 continue; 3843 } 3844 idx = nrsm->r_rtr_cnt - 1; 3845 if (ts == nrsm->r_tim_lastsent[idx]) { 3846 /* 3847 * For this case lets use seq no, if we sent in a 3848 * big block (TSO) we would have a bunch of segments 3849 * sent at the same time. 3850 * 3851 * We would only get a report if its SEQ is earlier. 3852 * If we have done multiple retransmits the times 3853 * would not be equal. 3854 */ 3855 if (SEQ_LT(nrsm->r_start, rsm->r_start)) { 3856 nrsm->r_flags |= RACK_SACK_PASSED; 3857 nrsm->r_flags &= ~RACK_WAS_SACKPASS; 3858 } 3859 } else { 3860 /* 3861 * Here they were sent at different times, not a big 3862 * block. Since we transmitted this one later and 3863 * see it sack'd then this must also be missing (or 3864 * we would have gotten a sack block for it) 3865 */ 3866 nrsm->r_flags |= RACK_SACK_PASSED; 3867 nrsm->r_flags &= ~RACK_WAS_SACKPASS; 3868 } 3869 } 3870 } 3871 3872 static uint32_t 3873 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack, 3874 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts) 3875 { 3876 int32_t idx; 3877 int32_t times = 0; 3878 uint32_t start, end, changed = 0; 3879 struct rack_sendmap *rsm, *nrsm; 3880 int32_t used_ref = 1; 3881 3882 start = sack->start; 3883 end = sack->end; 3884 rsm = *prsm; 3885 if (rsm && SEQ_LT(start, rsm->r_start)) { 3886 TAILQ_FOREACH_REVERSE_FROM(rsm, &rack->r_ctl.rc_map, rack_head, r_next) { 3887 if (SEQ_GEQ(start, rsm->r_start) && 3888 SEQ_LT(start, rsm->r_end)) { 3889 goto do_rest_ofb; 3890 } 3891 } 3892 } 3893 if (rsm == NULL) { 3894 start_at_beginning: 3895 rsm = NULL; 3896 used_ref = 0; 3897 } 3898 /* First lets locate the block where this guy is */ 3899 TAILQ_FOREACH_FROM(rsm, &rack->r_ctl.rc_map, r_next) { 3900 if (SEQ_GEQ(start, rsm->r_start) && 3901 SEQ_LT(start, rsm->r_end)) { 3902 break; 3903 } 3904 } 3905 do_rest_ofb: 3906 if (rsm == NULL) { 3907 /* 3908 * This happens when we get duplicate sack blocks with the 3909 * same end. For example SACK 4: 100 SACK 3: 100 The sort 3910 * will not change there location so we would just start at 3911 * the end of the first one and get lost. 3912 */ 3913 if (tp->t_flags & TF_SENTFIN) { 3914 /* 3915 * Check to see if we have not logged the FIN that 3916 * went out. 3917 */ 3918 nrsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next); 3919 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 3920 /* 3921 * Ok we did not get the FIN logged. 3922 */ 3923 nrsm->r_end++; 3924 rsm = nrsm; 3925 goto do_rest_ofb; 3926 } 3927 } 3928 if (times == 1) { 3929 #ifdef INVARIANTS 3930 panic("tp:%p rack:%p sack:%p to:%p prsm:%p", 3931 tp, rack, sack, to, prsm); 3932 #else 3933 goto out; 3934 #endif 3935 } 3936 times++; 3937 counter_u64_add(rack_sack_proc_restart, 1); 3938 goto start_at_beginning; 3939 } 3940 /* Ok we have an ACK for some piece of rsm */ 3941 if (rsm->r_start != start) { 3942 /* 3943 * Need to split this in two pieces the before and after. 3944 */ 3945 nrsm = rack_alloc(rack); 3946 if (nrsm == NULL) { 3947 /* 3948 * failed XXXrrs what can we do but loose the sack 3949 * info? 3950 */ 3951 goto out; 3952 } 3953 nrsm->r_start = start; 3954 nrsm->r_rtr_bytes = 0; 3955 nrsm->r_end = rsm->r_end; 3956 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 3957 nrsm->r_flags = rsm->r_flags; 3958 nrsm->r_sndcnt = rsm->r_sndcnt; 3959 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 3960 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 3961 } 3962 rsm->r_end = nrsm->r_start; 3963 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next); 3964 if (rsm->r_in_tmap) { 3965 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 3966 nrsm->r_in_tmap = 1; 3967 } 3968 rsm->r_flags &= (~RACK_HAS_FIN); 3969 rsm = nrsm; 3970 } 3971 if (SEQ_GEQ(end, rsm->r_end)) { 3972 /* 3973 * The end of this block is either beyond this guy or right 3974 * at this guy. 3975 */ 3976 3977 if ((rsm->r_flags & RACK_ACKED) == 0) { 3978 rack_update_rtt(tp, rack, rsm, to, cts, SACKED); 3979 changed += (rsm->r_end - rsm->r_start); 3980 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 3981 rack_log_sack_passed(tp, rack, rsm); 3982 /* Is Reordering occuring? */ 3983 if (rsm->r_flags & RACK_SACK_PASSED) { 3984 counter_u64_add(rack_reorder_seen, 1); 3985 rack->r_ctl.rc_reorder_ts = cts; 3986 } 3987 rsm->r_flags |= RACK_ACKED; 3988 rsm->r_flags &= ~RACK_TLP; 3989 if (rsm->r_in_tmap) { 3990 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 3991 rsm->r_in_tmap = 0; 3992 } 3993 } 3994 if (end == rsm->r_end) { 3995 /* This block only - done */ 3996 goto out; 3997 } 3998 /* There is more not coverend by this rsm move on */ 3999 start = rsm->r_end; 4000 nrsm = TAILQ_NEXT(rsm, r_next); 4001 rsm = nrsm; 4002 times = 0; 4003 goto do_rest_ofb; 4004 } 4005 /* Ok we need to split off this one at the tail */ 4006 nrsm = rack_alloc(rack); 4007 if (nrsm == NULL) { 4008 /* failed rrs what can we do but loose the sack info? */ 4009 goto out; 4010 } 4011 /* Clone it */ 4012 nrsm->r_start = end; 4013 nrsm->r_end = rsm->r_end; 4014 nrsm->r_rtr_bytes = 0; 4015 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4016 nrsm->r_flags = rsm->r_flags; 4017 nrsm->r_sndcnt = rsm->r_sndcnt; 4018 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4019 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4020 } 4021 /* The sack block does not cover this guy fully */ 4022 rsm->r_flags &= (~RACK_HAS_FIN); 4023 rsm->r_end = end; 4024 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_map, rsm, nrsm, r_next); 4025 if (rsm->r_in_tmap) { 4026 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4027 nrsm->r_in_tmap = 1; 4028 } 4029 if (rsm->r_flags & RACK_ACKED) { 4030 /* Been here done that */ 4031 goto out; 4032 } 4033 rack_update_rtt(tp, rack, rsm, to, cts, SACKED); 4034 changed += (rsm->r_end - rsm->r_start); 4035 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 4036 rack_log_sack_passed(tp, rack, rsm); 4037 /* Is Reordering occuring? */ 4038 if (rsm->r_flags & RACK_SACK_PASSED) { 4039 counter_u64_add(rack_reorder_seen, 1); 4040 rack->r_ctl.rc_reorder_ts = cts; 4041 } 4042 rsm->r_flags |= RACK_ACKED; 4043 rsm->r_flags &= ~RACK_TLP; 4044 if (rsm->r_in_tmap) { 4045 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 4046 rsm->r_in_tmap = 0; 4047 } 4048 out: 4049 if (used_ref == 0) { 4050 counter_u64_add(rack_sack_proc_all, 1); 4051 } else { 4052 counter_u64_add(rack_sack_proc_short, 1); 4053 } 4054 /* Save off where we last were */ 4055 if (rsm) 4056 rack->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 4057 else 4058 rack->r_ctl.rc_sacklast = NULL; 4059 *prsm = rsm; 4060 return (changed); 4061 } 4062 4063 static void inline 4064 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack) 4065 { 4066 struct rack_sendmap *tmap; 4067 4068 tmap = NULL; 4069 while (rsm && (rsm->r_flags & RACK_ACKED)) { 4070 /* Its no longer sacked, mark it so */ 4071 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 4072 #ifdef INVARIANTS 4073 if (rsm->r_in_tmap) { 4074 panic("rack:%p rsm:%p flags:0x%x in tmap?", 4075 rack, rsm, rsm->r_flags); 4076 } 4077 #endif 4078 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS); 4079 /* Rebuild it into our tmap */ 4080 if (tmap == NULL) { 4081 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 4082 tmap = rsm; 4083 } else { 4084 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext); 4085 tmap = rsm; 4086 } 4087 tmap->r_in_tmap = 1; 4088 rsm = TAILQ_NEXT(rsm, r_next); 4089 } 4090 /* 4091 * Now lets possibly clear the sack filter so we start 4092 * recognizing sacks that cover this area. 4093 */ 4094 if (rack_use_sack_filter) 4095 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack); 4096 4097 } 4098 4099 static void 4100 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th) 4101 { 4102 uint32_t changed, last_seq, entered_recovery = 0; 4103 struct tcp_rack *rack; 4104 struct rack_sendmap *rsm; 4105 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 4106 register uint32_t th_ack; 4107 int32_t i, j, k, num_sack_blks = 0; 4108 uint32_t cts, acked, ack_point, sack_changed = 0; 4109 4110 INP_WLOCK_ASSERT(tp->t_inpcb); 4111 if (th->th_flags & TH_RST) { 4112 /* We don't log resets */ 4113 return; 4114 } 4115 rack = (struct tcp_rack *)tp->t_fb_ptr; 4116 cts = tcp_ts_getticks(); 4117 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 4118 changed = 0; 4119 th_ack = th->th_ack; 4120 4121 if (SEQ_GT(th_ack, tp->snd_una)) { 4122 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__); 4123 tp->t_acktime = ticks; 4124 } 4125 if (rsm && SEQ_GT(th_ack, rsm->r_start)) 4126 changed = th_ack - rsm->r_start; 4127 if (changed) { 4128 /* 4129 * The ACK point is advancing to th_ack, we must drop off 4130 * the packets in the rack log and calculate any eligble 4131 * RTT's. 4132 */ 4133 rack->r_wanted_output++; 4134 more: 4135 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 4136 if (rsm == NULL) { 4137 if ((th_ack - 1) == tp->iss) { 4138 /* 4139 * For the SYN incoming case we will not 4140 * have called tcp_output for the sending of 4141 * the SYN, so there will be no map. All 4142 * other cases should probably be a panic. 4143 */ 4144 goto proc_sack; 4145 } 4146 if (tp->t_flags & TF_SENTFIN) { 4147 /* if we send a FIN we will not hav a map */ 4148 goto proc_sack; 4149 } 4150 #ifdef INVARIANTS 4151 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", 4152 tp, 4153 th, tp->t_state, rack, 4154 tp->snd_una, tp->snd_max, tp->snd_nxt, changed); 4155 #endif 4156 goto proc_sack; 4157 } 4158 if (SEQ_LT(th_ack, rsm->r_start)) { 4159 /* Huh map is missing this */ 4160 #ifdef INVARIANTS 4161 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n", 4162 rsm->r_start, 4163 th_ack, tp->t_state, rack->r_state); 4164 #endif 4165 goto proc_sack; 4166 } 4167 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED); 4168 /* Now do we consume the whole thing? */ 4169 if (SEQ_GEQ(th_ack, rsm->r_end)) { 4170 /* Its all consumed. */ 4171 uint32_t left; 4172 4173 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 4174 rsm->r_rtr_bytes = 0; 4175 TAILQ_REMOVE(&rack->r_ctl.rc_map, rsm, r_next); 4176 if (rsm->r_in_tmap) { 4177 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 4178 rsm->r_in_tmap = 0; 4179 } 4180 if (rack->r_ctl.rc_next == rsm) { 4181 /* scoot along the marker */ 4182 rack->r_ctl.rc_next = TAILQ_FIRST(&rack->r_ctl.rc_map); 4183 } 4184 if (rsm->r_flags & RACK_ACKED) { 4185 /* 4186 * It was acked on the scoreboard -- remove 4187 * it from total 4188 */ 4189 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 4190 } else if (rsm->r_flags & RACK_SACK_PASSED) { 4191 /* 4192 * There are acked segments ACKED on the 4193 * scoreboard further up. We are seeing 4194 * reordering. 4195 */ 4196 counter_u64_add(rack_reorder_seen, 1); 4197 rsm->r_flags |= RACK_ACKED; 4198 rack->r_ctl.rc_reorder_ts = cts; 4199 } 4200 left = th_ack - rsm->r_end; 4201 if (rsm->r_rtr_cnt > 1) { 4202 /* 4203 * Technically we should make r_rtr_cnt be 4204 * monotonicly increasing and just mod it to 4205 * the timestamp it is replacing.. that way 4206 * we would have the last 3 retransmits. Now 4207 * rc_loss_count will be wrong if we 4208 * retransmit something more than 2 times in 4209 * recovery :( 4210 */ 4211 rack->r_ctl.rc_loss_count += (rsm->r_rtr_cnt - 1); 4212 } 4213 /* Free back to zone */ 4214 rack_free(rack, rsm); 4215 if (left) { 4216 goto more; 4217 } 4218 goto proc_sack; 4219 } 4220 if (rsm->r_flags & RACK_ACKED) { 4221 /* 4222 * It was acked on the scoreboard -- remove it from 4223 * total for the part being cum-acked. 4224 */ 4225 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 4226 } 4227 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 4228 rsm->r_rtr_bytes = 0; 4229 rsm->r_start = th_ack; 4230 } 4231 proc_sack: 4232 /* Check for reneging */ 4233 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 4234 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) { 4235 /* 4236 * The peer has moved snd_una up to 4237 * the edge of this send, i.e. one 4238 * that it had previously acked. The only 4239 * way that can be true if the peer threw 4240 * away data (space issues) that it had 4241 * previously sacked (else it would have 4242 * given us snd_una up to (rsm->r_end). 4243 * We need to undo the acked markings here. 4244 * 4245 * Note we have to look to make sure th_ack is 4246 * our rsm->r_start in case we get an old ack 4247 * where th_ack is behind snd_una. 4248 */ 4249 rack_peer_reneges(rack, rsm, th->th_ack); 4250 } 4251 if ((to->to_flags & TOF_SACK) == 0) { 4252 /* We are done nothing left to log */ 4253 goto out; 4254 } 4255 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_map, rack_sendmap, r_next); 4256 if (rsm) { 4257 last_seq = rsm->r_end; 4258 } else { 4259 last_seq = tp->snd_max; 4260 } 4261 /* Sack block processing */ 4262 if (SEQ_GT(th_ack, tp->snd_una)) 4263 ack_point = th_ack; 4264 else 4265 ack_point = tp->snd_una; 4266 for (i = 0; i < to->to_nsacks; i++) { 4267 bcopy((to->to_sacks + i * TCPOLEN_SACK), 4268 &sack, sizeof(sack)); 4269 sack.start = ntohl(sack.start); 4270 sack.end = ntohl(sack.end); 4271 if (SEQ_GT(sack.end, sack.start) && 4272 SEQ_GT(sack.start, ack_point) && 4273 SEQ_LT(sack.start, tp->snd_max) && 4274 SEQ_GT(sack.end, ack_point) && 4275 SEQ_LEQ(sack.end, tp->snd_max)) { 4276 if ((rack->r_ctl.rc_num_maps_alloced > rack_sack_block_limit) && 4277 (SEQ_LT(sack.end, last_seq)) && 4278 ((sack.end - sack.start) < (tp->t_maxseg / 8))) { 4279 /* 4280 * Not the last piece and its smaller than 4281 * 1/8th of a MSS. We ignore this. 4282 */ 4283 counter_u64_add(rack_runt_sacks, 1); 4284 continue; 4285 } 4286 sack_blocks[num_sack_blks] = sack; 4287 num_sack_blks++; 4288 #ifdef NETFLIX_STATS 4289 } else if (SEQ_LEQ(sack.start, th_ack) && 4290 SEQ_LEQ(sack.end, th_ack)) { 4291 /* 4292 * Its a D-SACK block. 4293 */ 4294 tcp_record_dsack(sack.start, sack.end); 4295 #endif 4296 } 4297 4298 } 4299 if (num_sack_blks == 0) 4300 goto out; 4301 /* 4302 * Sort the SACK blocks so we can update the rack scoreboard with 4303 * just one pass. 4304 */ 4305 if (rack_use_sack_filter) { 4306 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks, num_sack_blks, th->th_ack); 4307 } 4308 if (num_sack_blks < 2) { 4309 goto do_sack_work; 4310 } 4311 /* Sort the sacks */ 4312 for (i = 0; i < num_sack_blks; i++) { 4313 for (j = i + 1; j < num_sack_blks; j++) { 4314 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 4315 sack = sack_blocks[i]; 4316 sack_blocks[i] = sack_blocks[j]; 4317 sack_blocks[j] = sack; 4318 } 4319 } 4320 } 4321 /* 4322 * Now are any of the sack block ends the same (yes some 4323 * implememtations send these)? 4324 */ 4325 again: 4326 if (num_sack_blks > 1) { 4327 for (i = 0; i < num_sack_blks; i++) { 4328 for (j = i + 1; j < num_sack_blks; j++) { 4329 if (sack_blocks[i].end == sack_blocks[j].end) { 4330 /* 4331 * Ok these two have the same end we 4332 * want the smallest end and then 4333 * throw away the larger and start 4334 * again. 4335 */ 4336 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 4337 /* 4338 * The second block covers 4339 * more area use that 4340 */ 4341 sack_blocks[i].start = sack_blocks[j].start; 4342 } 4343 /* 4344 * Now collapse out the dup-sack and 4345 * lower the count 4346 */ 4347 for (k = (j + 1); k < num_sack_blks; k++) { 4348 sack_blocks[j].start = sack_blocks[k].start; 4349 sack_blocks[j].end = sack_blocks[k].end; 4350 j++; 4351 } 4352 num_sack_blks--; 4353 goto again; 4354 } 4355 } 4356 } 4357 } 4358 do_sack_work: 4359 rsm = rack->r_ctl.rc_sacklast; 4360 for (i = 0; i < num_sack_blks; i++) { 4361 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts); 4362 if (acked) { 4363 rack->r_wanted_output++; 4364 changed += acked; 4365 sack_changed += acked; 4366 } 4367 } 4368 out: 4369 if (changed) { 4370 /* Something changed cancel the rack timer */ 4371 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4372 } 4373 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 4374 /* 4375 * Ok we have a high probability that we need to go in to 4376 * recovery since we have data sack'd 4377 */ 4378 struct rack_sendmap *rsm; 4379 uint32_t tsused; 4380 4381 tsused = tcp_ts_getticks(); 4382 rsm = tcp_rack_output(tp, rack, tsused); 4383 if (rsm) { 4384 /* Enter recovery */ 4385 rack->r_ctl.rc_rsm_start = rsm->r_start; 4386 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 4387 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 4388 entered_recovery = 1; 4389 rack_cong_signal(tp, NULL, CC_NDUPACK); 4390 /* 4391 * When we enter recovery we need to assure we send 4392 * one packet. 4393 */ 4394 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 4395 rack->r_timer_override = 1; 4396 } 4397 } 4398 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 4399 /* Deal with changed an PRR here (in recovery only) */ 4400 uint32_t pipe, snd_una; 4401 4402 rack->r_ctl.rc_prr_delivered += changed; 4403 /* Compute prr_sndcnt */ 4404 if (SEQ_GT(tp->snd_una, th_ack)) { 4405 snd_una = tp->snd_una; 4406 } else { 4407 snd_una = th_ack; 4408 } 4409 pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt; 4410 if (pipe > tp->snd_ssthresh) { 4411 long sndcnt; 4412 4413 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh; 4414 if (rack->r_ctl.rc_prr_recovery_fs > 0) 4415 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs; 4416 else { 4417 rack->r_ctl.rc_prr_sndcnt = 0; 4418 sndcnt = 0; 4419 } 4420 sndcnt++; 4421 if (sndcnt > (long)rack->r_ctl.rc_prr_out) 4422 sndcnt -= rack->r_ctl.rc_prr_out; 4423 else 4424 sndcnt = 0; 4425 rack->r_ctl.rc_prr_sndcnt = sndcnt; 4426 } else { 4427 uint32_t limit; 4428 4429 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out) 4430 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out); 4431 else 4432 limit = 0; 4433 if (changed > limit) 4434 limit = changed; 4435 limit += tp->t_maxseg; 4436 if (tp->snd_ssthresh > pipe) { 4437 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit); 4438 } else { 4439 rack->r_ctl.rc_prr_sndcnt = min(0, limit); 4440 } 4441 } 4442 if (rack->r_ctl.rc_prr_sndcnt >= tp->t_maxseg) { 4443 rack->r_timer_override = 1; 4444 } 4445 } 4446 } 4447 4448 /* 4449 * Return value of 1, we do not need to call rack_process_data(). 4450 * return value of 0, rack_process_data can be called. 4451 * For ret_val if its 0 the TCP is locked, if its non-zero 4452 * its unlocked and probably unsafe to touch the TCB. 4453 */ 4454 static int 4455 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 4456 struct tcpcb *tp, struct tcpopt *to, 4457 uint32_t tiwin, int32_t tlen, 4458 int32_t * ofia, int32_t thflags, int32_t * ret_val) 4459 { 4460 int32_t ourfinisacked = 0; 4461 int32_t nsegs, acked_amount; 4462 int32_t acked; 4463 struct mbuf *mfree; 4464 struct tcp_rack *rack; 4465 int32_t recovery = 0; 4466 4467 rack = (struct tcp_rack *)tp->t_fb_ptr; 4468 if (SEQ_GT(th->th_ack, tp->snd_max)) { 4469 rack_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 4470 return (1); 4471 } 4472 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 4473 rack_log_ack(tp, to, th); 4474 } 4475 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 4476 /* 4477 * Old ack, behind (or duplicate to) the last one rcv'd 4478 * Note: Should mark reordering is occuring! We should also 4479 * look for sack blocks arriving e.g. ack 1, 4-4 then ack 1, 4480 * 3-3, 4-4 would be reording. As well as ack 1, 3-3 <no 4481 * retran and> ack 3 4482 */ 4483 return (0); 4484 } 4485 /* 4486 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 4487 * something we sent. 4488 */ 4489 if (tp->t_flags & TF_NEEDSYN) { 4490 /* 4491 * T/TCP: Connection was half-synchronized, and our SYN has 4492 * been ACK'd (so connection is now fully synchronized). Go 4493 * to non-starred state, increment snd_una for ACK of SYN, 4494 * and check if we can do window scaling. 4495 */ 4496 tp->t_flags &= ~TF_NEEDSYN; 4497 tp->snd_una++; 4498 /* Do window scaling? */ 4499 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 4500 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 4501 tp->rcv_scale = tp->request_r_scale; 4502 /* Send window already scaled. */ 4503 } 4504 } 4505 nsegs = max(1, m->m_pkthdr.lro_nsegs); 4506 INP_WLOCK_ASSERT(tp->t_inpcb); 4507 4508 acked = BYTES_THIS_ACK(tp, th); 4509 TCPSTAT_ADD(tcps_rcvackpack, nsegs); 4510 TCPSTAT_ADD(tcps_rcvackbyte, acked); 4511 4512 /* 4513 * If we just performed our first retransmit, and the ACK arrives 4514 * within our recovery window, then it was a mistake to do the 4515 * retransmit in the first place. Recover our original cwnd and 4516 * ssthresh, and proceed to transmit where we left off. 4517 */ 4518 if (tp->t_flags & TF_PREVVALID) { 4519 tp->t_flags &= ~TF_PREVVALID; 4520 if (tp->t_rxtshift == 1 && 4521 (int)(ticks - tp->t_badrxtwin) < 0) 4522 rack_cong_signal(tp, th, CC_RTO_ERR); 4523 } 4524 /* 4525 * If we have a timestamp reply, update smoothed round trip time. If 4526 * no timestamp is present but transmit timer is running and timed 4527 * sequence number was acked, update smoothed round trip time. Since 4528 * we now have an rtt measurement, cancel the timer backoff (cf., 4529 * Phil Karn's retransmit alg.). Recompute the initial retransmit 4530 * timer. 4531 * 4532 * Some boxes send broken timestamp replies during the SYN+ACK 4533 * phase, ignore timestamps of 0 or we could calculate a huge RTT 4534 * and blow up the retransmit timer. 4535 */ 4536 /* 4537 * If all outstanding data is acked, stop retransmit timer and 4538 * remember to restart (more output or persist). If there is more 4539 * data to be acked, restart retransmit timer, using current 4540 * (possibly backed-off) value. 4541 */ 4542 if (th->th_ack == tp->snd_max) { 4543 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4544 rack->r_wanted_output++; 4545 } 4546 /* 4547 * If no data (only SYN) was ACK'd, skip rest of ACK processing. 4548 */ 4549 if (acked == 0) { 4550 if (ofia) 4551 *ofia = ourfinisacked; 4552 return (0); 4553 } 4554 if (rack->r_ctl.rc_early_recovery) { 4555 if (IN_FASTRECOVERY(tp->t_flags)) { 4556 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 4557 tcp_rack_partialack(tp, th); 4558 } else { 4559 rack_post_recovery(tp, th); 4560 recovery = 1; 4561 } 4562 } 4563 } 4564 /* 4565 * Let the congestion control algorithm update congestion control 4566 * related information. This typically means increasing the 4567 * congestion window. 4568 */ 4569 rack_ack_received(tp, rack, th, nsegs, CC_ACK, recovery); 4570 SOCKBUF_LOCK(&so->so_snd); 4571 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 4572 tp->snd_wnd -= acked_amount; 4573 mfree = sbcut_locked(&so->so_snd, acked_amount); 4574 if ((sbused(&so->so_snd) == 0) && 4575 (acked > acked_amount) && 4576 (tp->t_state >= TCPS_FIN_WAIT_1)) { 4577 ourfinisacked = 1; 4578 } 4579 /* NB: sowwakeup_locked() does an implicit unlock. */ 4580 sowwakeup_locked(so); 4581 m_freem(mfree); 4582 if (rack->r_ctl.rc_early_recovery == 0) { 4583 if (IN_FASTRECOVERY(tp->t_flags)) { 4584 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 4585 tcp_rack_partialack(tp, th); 4586 } else { 4587 rack_post_recovery(tp, th); 4588 } 4589 } 4590 } 4591 tp->snd_una = th->th_ack; 4592 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 4593 tp->snd_recover = tp->snd_una; 4594 4595 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) { 4596 tp->snd_nxt = tp->snd_una; 4597 } 4598 if (tp->snd_una == tp->snd_max) { 4599 /* Nothing left outstanding */ 4600 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 4601 tp->t_acktime = 0; 4602 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4603 /* Set need output so persist might get set */ 4604 rack->r_wanted_output++; 4605 if (rack_use_sack_filter) 4606 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 4607 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 4608 (sbavail(&so->so_snd) == 0) && 4609 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 4610 /* 4611 * The socket was gone and the 4612 * peer sent data, time to 4613 * reset him. 4614 */ 4615 *ret_val = 1; 4616 tp = tcp_close(tp); 4617 rack_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 4618 return (1); 4619 } 4620 } 4621 if (ofia) 4622 *ofia = ourfinisacked; 4623 return (0); 4624 } 4625 4626 4627 /* 4628 * Return value of 1, the TCB is unlocked and most 4629 * likely gone, return value of 0, the TCP is still 4630 * locked. 4631 */ 4632 static int 4633 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 4634 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 4635 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 4636 { 4637 /* 4638 * Update window information. Don't look at window if no ACK: TAC's 4639 * send garbage on first SYN. 4640 */ 4641 int32_t nsegs; 4642 int32_t tfo_syn; 4643 struct tcp_rack *rack; 4644 4645 rack = (struct tcp_rack *)tp->t_fb_ptr; 4646 INP_WLOCK_ASSERT(tp->t_inpcb); 4647 nsegs = max(1, m->m_pkthdr.lro_nsegs); 4648 if ((thflags & TH_ACK) && 4649 (SEQ_LT(tp->snd_wl1, th->th_seq) || 4650 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 4651 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 4652 /* keep track of pure window updates */ 4653 if (tlen == 0 && 4654 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 4655 TCPSTAT_INC(tcps_rcvwinupd); 4656 tp->snd_wnd = tiwin; 4657 tp->snd_wl1 = th->th_seq; 4658 tp->snd_wl2 = th->th_ack; 4659 if (tp->snd_wnd > tp->max_sndwnd) 4660 tp->max_sndwnd = tp->snd_wnd; 4661 rack->r_wanted_output++; 4662 } else if (thflags & TH_ACK) { 4663 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 4664 tp->snd_wnd = tiwin; 4665 tp->snd_wl1 = th->th_seq; 4666 tp->snd_wl2 = th->th_ack; 4667 } 4668 } 4669 /* Was persist timer active and now we have window space? */ 4670 if ((rack->rc_in_persist != 0) && tp->snd_wnd) { 4671 rack_exit_persist(tp, rack); 4672 tp->snd_nxt = tp->snd_max; 4673 /* Make sure we output to start the timer */ 4674 rack->r_wanted_output++; 4675 } 4676 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 4677 m_freem(m); 4678 return (0); 4679 } 4680 /* 4681 * Process segments with URG. 4682 */ 4683 if ((thflags & TH_URG) && th->th_urp && 4684 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4685 /* 4686 * This is a kludge, but if we receive and accept random 4687 * urgent pointers, we'll crash in soreceive. It's hard to 4688 * imagine someone actually wanting to send this much urgent 4689 * data. 4690 */ 4691 SOCKBUF_LOCK(&so->so_rcv); 4692 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 4693 th->th_urp = 0; /* XXX */ 4694 thflags &= ~TH_URG; /* XXX */ 4695 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 4696 goto dodata; /* XXX */ 4697 } 4698 /* 4699 * If this segment advances the known urgent pointer, then 4700 * mark the data stream. This should not happen in 4701 * CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since a 4702 * FIN has been received from the remote side. In these 4703 * states we ignore the URG. 4704 * 4705 * According to RFC961 (Assigned Protocols), the urgent 4706 * pointer points to the last octet of urgent data. We 4707 * continue, however, to consider it to indicate the first 4708 * octet of data past the urgent section as the original 4709 * spec states (in one of two places). 4710 */ 4711 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) { 4712 tp->rcv_up = th->th_seq + th->th_urp; 4713 so->so_oobmark = sbavail(&so->so_rcv) + 4714 (tp->rcv_up - tp->rcv_nxt) - 1; 4715 if (so->so_oobmark == 0) 4716 so->so_rcv.sb_state |= SBS_RCVATMARK; 4717 sohasoutofband(so); 4718 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 4719 } 4720 SOCKBUF_UNLOCK(&so->so_rcv); 4721 /* 4722 * Remove out of band data so doesn't get presented to user. 4723 * This can happen independent of advancing the URG pointer, 4724 * but if two URG's are pending at once, some out-of-band 4725 * data may creep in... ick. 4726 */ 4727 if (th->th_urp <= (uint32_t) tlen && 4728 !(so->so_options & SO_OOBINLINE)) { 4729 /* hdr drop is delayed */ 4730 tcp_pulloutofband(so, th, m, drop_hdrlen); 4731 } 4732 } else { 4733 /* 4734 * If no out of band data is expected, pull receive urgent 4735 * pointer along with the receive window. 4736 */ 4737 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 4738 tp->rcv_up = tp->rcv_nxt; 4739 } 4740 dodata: /* XXX */ 4741 INP_WLOCK_ASSERT(tp->t_inpcb); 4742 4743 /* 4744 * Process the segment text, merging it into the TCP sequencing 4745 * queue, and arranging for acknowledgment of receipt if necessary. 4746 * This process logically involves adjusting tp->rcv_wnd as data is 4747 * presented to the user (this happens in tcp_usrreq.c, case 4748 * PRU_RCVD). If a FIN has already been received on this connection 4749 * then we just ignore the text. 4750 */ 4751 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 4752 IS_FASTOPEN(tp->t_flags)); 4753 if ((tlen || (thflags & TH_FIN) || tfo_syn) && 4754 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4755 tcp_seq save_start = th->th_seq; 4756 4757 m_adj(m, drop_hdrlen); /* delayed header drop */ 4758 /* 4759 * Insert segment which includes th into TCP reassembly 4760 * queue with control block tp. Set thflags to whether 4761 * reassembly now includes a segment with FIN. This handles 4762 * the common case inline (segment is the next to be 4763 * received on an established connection, and the queue is 4764 * empty), avoiding linkage into and removal from the queue 4765 * and repetition of various conversions. Set DELACK for 4766 * segments received in order, but ack immediately when 4767 * segments are out of order (so fast retransmit can work). 4768 */ 4769 if (th->th_seq == tp->rcv_nxt && 4770 SEGQ_EMPTY(tp) && 4771 (TCPS_HAVEESTABLISHED(tp->t_state) || 4772 tfo_syn)) { 4773 if (DELAY_ACK(tp, tlen) || tfo_syn) { 4774 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4775 tp->t_flags |= TF_DELACK; 4776 } else { 4777 rack->r_wanted_output++; 4778 tp->t_flags |= TF_ACKNOW; 4779 } 4780 tp->rcv_nxt += tlen; 4781 thflags = th->th_flags & TH_FIN; 4782 TCPSTAT_ADD(tcps_rcvpack, nsegs); 4783 TCPSTAT_ADD(tcps_rcvbyte, tlen); 4784 SOCKBUF_LOCK(&so->so_rcv); 4785 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 4786 m_freem(m); 4787 else 4788 sbappendstream_locked(&so->so_rcv, m, 0); 4789 /* NB: sorwakeup_locked() does an implicit unlock. */ 4790 sorwakeup_locked(so); 4791 } else { 4792 /* 4793 * XXX: Due to the header drop above "th" is 4794 * theoretically invalid by now. Fortunately 4795 * m_adj() doesn't actually frees any mbufs when 4796 * trimming from the head. 4797 */ 4798 thflags = tcp_reass(tp, th, &save_start, &tlen, m); 4799 tp->t_flags |= TF_ACKNOW; 4800 } 4801 if (tlen > 0) 4802 tcp_update_sack_list(tp, save_start, save_start + tlen); 4803 } else { 4804 m_freem(m); 4805 thflags &= ~TH_FIN; 4806 } 4807 4808 /* 4809 * If FIN is received ACK the FIN and let the user know that the 4810 * connection is closing. 4811 */ 4812 if (thflags & TH_FIN) { 4813 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4814 socantrcvmore(so); 4815 /* 4816 * If connection is half-synchronized (ie NEEDSYN 4817 * flag on) then delay ACK, so it may be piggybacked 4818 * when SYN is sent. Otherwise, since we received a 4819 * FIN then no more input can be expected, send ACK 4820 * now. 4821 */ 4822 if (tp->t_flags & TF_NEEDSYN) { 4823 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4824 tp->t_flags |= TF_DELACK; 4825 } else { 4826 tp->t_flags |= TF_ACKNOW; 4827 } 4828 tp->rcv_nxt++; 4829 } 4830 switch (tp->t_state) { 4831 4832 /* 4833 * In SYN_RECEIVED and ESTABLISHED STATES enter the 4834 * CLOSE_WAIT state. 4835 */ 4836 case TCPS_SYN_RECEIVED: 4837 tp->t_starttime = ticks; 4838 /* FALLTHROUGH */ 4839 case TCPS_ESTABLISHED: 4840 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4841 tcp_state_change(tp, TCPS_CLOSE_WAIT); 4842 break; 4843 4844 /* 4845 * If still in FIN_WAIT_1 STATE FIN has not been 4846 * acked so enter the CLOSING state. 4847 */ 4848 case TCPS_FIN_WAIT_1: 4849 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4850 tcp_state_change(tp, TCPS_CLOSING); 4851 break; 4852 4853 /* 4854 * In FIN_WAIT_2 state enter the TIME_WAIT state, 4855 * starting the time-wait timer, turning off the 4856 * other standard timers. 4857 */ 4858 case TCPS_FIN_WAIT_2: 4859 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4860 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 4861 tcp_twstart(tp); 4862 return (1); 4863 } 4864 } 4865 /* 4866 * Return any desired output. 4867 */ 4868 if ((tp->t_flags & TF_ACKNOW) || (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) { 4869 rack->r_wanted_output++; 4870 } 4871 INP_WLOCK_ASSERT(tp->t_inpcb); 4872 return (0); 4873 } 4874 4875 /* 4876 * Here nothing is really faster, its just that we 4877 * have broken out the fast-data path also just like 4878 * the fast-ack. 4879 */ 4880 static int 4881 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 4882 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 4883 uint32_t tiwin, int32_t nxt_pkt) 4884 { 4885 int32_t nsegs; 4886 int32_t newsize = 0; /* automatic sockbuf scaling */ 4887 struct tcp_rack *rack; 4888 #ifdef TCPDEBUG 4889 /* 4890 * The size of tcp_saveipgen must be the size of the max ip header, 4891 * now IPv6. 4892 */ 4893 u_char tcp_saveipgen[IP6_HDR_LEN]; 4894 struct tcphdr tcp_savetcp; 4895 short ostate = 0; 4896 4897 #endif 4898 /* 4899 * If last ACK falls within this segment's sequence numbers, record 4900 * the timestamp. NOTE that the test is modified according to the 4901 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 4902 */ 4903 if (__predict_false(th->th_seq != tp->rcv_nxt)) { 4904 return (0); 4905 } 4906 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 4907 return (0); 4908 } 4909 if (tiwin && tiwin != tp->snd_wnd) { 4910 return (0); 4911 } 4912 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 4913 return (0); 4914 } 4915 if (__predict_false((to->to_flags & TOF_TS) && 4916 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 4917 return (0); 4918 } 4919 if (__predict_false((th->th_ack != tp->snd_una))) { 4920 return (0); 4921 } 4922 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 4923 return (0); 4924 } 4925 if ((to->to_flags & TOF_TS) != 0 && 4926 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 4927 tp->ts_recent_age = tcp_ts_getticks(); 4928 tp->ts_recent = to->to_tsval; 4929 } 4930 rack = (struct tcp_rack *)tp->t_fb_ptr; 4931 /* 4932 * This is a pure, in-sequence data packet with nothing on the 4933 * reassembly queue and we have enough buffer space to take it. 4934 */ 4935 nsegs = max(1, m->m_pkthdr.lro_nsegs); 4936 4937 4938 /* Clean receiver SACK report if present */ 4939 if (tp->rcv_numsacks) 4940 tcp_clean_sackreport(tp); 4941 TCPSTAT_INC(tcps_preddat); 4942 tp->rcv_nxt += tlen; 4943 /* 4944 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 4945 */ 4946 tp->snd_wl1 = th->th_seq; 4947 /* 4948 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 4949 */ 4950 tp->rcv_up = tp->rcv_nxt; 4951 TCPSTAT_ADD(tcps_rcvpack, nsegs); 4952 TCPSTAT_ADD(tcps_rcvbyte, tlen); 4953 #ifdef TCPDEBUG 4954 if (so->so_options & SO_DEBUG) 4955 tcp_trace(TA_INPUT, ostate, tp, 4956 (void *)tcp_saveipgen, &tcp_savetcp, 0); 4957 #endif 4958 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 4959 4960 /* Add data to socket buffer. */ 4961 SOCKBUF_LOCK(&so->so_rcv); 4962 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 4963 m_freem(m); 4964 } else { 4965 /* 4966 * Set new socket buffer size. Give up when limit is 4967 * reached. 4968 */ 4969 if (newsize) 4970 if (!sbreserve_locked(&so->so_rcv, 4971 newsize, so, NULL)) 4972 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 4973 m_adj(m, drop_hdrlen); /* delayed header drop */ 4974 sbappendstream_locked(&so->so_rcv, m, 0); 4975 rack_calc_rwin(so, tp); 4976 } 4977 /* NB: sorwakeup_locked() does an implicit unlock. */ 4978 sorwakeup_locked(so); 4979 if (DELAY_ACK(tp, tlen)) { 4980 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4981 tp->t_flags |= TF_DELACK; 4982 } else { 4983 tp->t_flags |= TF_ACKNOW; 4984 rack->r_wanted_output++; 4985 } 4986 if ((tp->snd_una == tp->snd_max) && rack_use_sack_filter) 4987 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 4988 return (1); 4989 } 4990 4991 /* 4992 * This subfunction is used to try to highly optimize the 4993 * fast path. We again allow window updates that are 4994 * in sequence to remain in the fast-path. We also add 4995 * in the __predict's to attempt to help the compiler. 4996 * Note that if we return a 0, then we can *not* process 4997 * it and the caller should push the packet into the 4998 * slow-path. 4999 */ 5000 static int 5001 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 5002 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5003 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts) 5004 { 5005 int32_t acked; 5006 int32_t nsegs; 5007 5008 #ifdef TCPDEBUG 5009 /* 5010 * The size of tcp_saveipgen must be the size of the max ip header, 5011 * now IPv6. 5012 */ 5013 u_char tcp_saveipgen[IP6_HDR_LEN]; 5014 struct tcphdr tcp_savetcp; 5015 short ostate = 0; 5016 5017 #endif 5018 struct tcp_rack *rack; 5019 5020 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 5021 /* Old ack, behind (or duplicate to) the last one rcv'd */ 5022 return (0); 5023 } 5024 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 5025 /* Above what we have sent? */ 5026 return (0); 5027 } 5028 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 5029 /* We are retransmitting */ 5030 return (0); 5031 } 5032 if (__predict_false(tiwin == 0)) { 5033 /* zero window */ 5034 return (0); 5035 } 5036 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 5037 /* We need a SYN or a FIN, unlikely.. */ 5038 return (0); 5039 } 5040 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 5041 /* Timestamp is behind .. old ack with seq wrap? */ 5042 return (0); 5043 } 5044 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 5045 /* Still recovering */ 5046 return (0); 5047 } 5048 rack = (struct tcp_rack *)tp->t_fb_ptr; 5049 if (rack->r_ctl.rc_sacked) { 5050 /* We have sack holes on our scoreboard */ 5051 return (0); 5052 } 5053 /* Ok if we reach here, we can process a fast-ack */ 5054 nsegs = max(1, m->m_pkthdr.lro_nsegs); 5055 rack_log_ack(tp, to, th); 5056 /* Did the window get updated? */ 5057 if (tiwin != tp->snd_wnd) { 5058 tp->snd_wnd = tiwin; 5059 tp->snd_wl1 = th->th_seq; 5060 if (tp->snd_wnd > tp->max_sndwnd) 5061 tp->max_sndwnd = tp->snd_wnd; 5062 } 5063 if ((rack->rc_in_persist != 0) && (tp->snd_wnd >= tp->t_maxseg)) { 5064 rack_exit_persist(tp, rack); 5065 } 5066 /* 5067 * If last ACK falls within this segment's sequence numbers, record 5068 * the timestamp. NOTE that the test is modified according to the 5069 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 5070 */ 5071 if ((to->to_flags & TOF_TS) != 0 && 5072 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 5073 tp->ts_recent_age = tcp_ts_getticks(); 5074 tp->ts_recent = to->to_tsval; 5075 } 5076 /* 5077 * This is a pure ack for outstanding data. 5078 */ 5079 TCPSTAT_INC(tcps_predack); 5080 5081 /* 5082 * "bad retransmit" recovery. 5083 */ 5084 if (tp->t_flags & TF_PREVVALID) { 5085 tp->t_flags &= ~TF_PREVVALID; 5086 if (tp->t_rxtshift == 1 && 5087 (int)(ticks - tp->t_badrxtwin) < 0) 5088 rack_cong_signal(tp, th, CC_RTO_ERR); 5089 } 5090 /* 5091 * Recalculate the transmit timer / rtt. 5092 * 5093 * Some boxes send broken timestamp replies during the SYN+ACK 5094 * phase, ignore timestamps of 0 or we could calculate a huge RTT 5095 * and blow up the retransmit timer. 5096 */ 5097 acked = BYTES_THIS_ACK(tp, th); 5098 5099 #ifdef TCP_HHOOK 5100 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 5101 hhook_run_tcp_est_in(tp, th, to); 5102 #endif 5103 5104 TCPSTAT_ADD(tcps_rcvackpack, nsegs); 5105 TCPSTAT_ADD(tcps_rcvackbyte, acked); 5106 sbdrop(&so->so_snd, acked); 5107 /* 5108 * Let the congestion control algorithm update congestion control 5109 * related information. This typically means increasing the 5110 * congestion window. 5111 */ 5112 rack_ack_received(tp, rack, th, nsegs, CC_ACK, 0); 5113 5114 tp->snd_una = th->th_ack; 5115 /* 5116 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 5117 */ 5118 tp->snd_wl2 = th->th_ack; 5119 tp->t_dupacks = 0; 5120 m_freem(m); 5121 /* ND6_HINT(tp); *//* Some progress has been made. */ 5122 5123 /* 5124 * If all outstanding data are acked, stop retransmit timer, 5125 * otherwise restart timer using current (possibly backed-off) 5126 * value. If process is waiting for space, wakeup/selwakeup/signal. 5127 * If data are ready to send, let tcp_output decide between more 5128 * output or persist. 5129 */ 5130 #ifdef TCPDEBUG 5131 if (so->so_options & SO_DEBUG) 5132 tcp_trace(TA_INPUT, ostate, tp, 5133 (void *)tcp_saveipgen, 5134 &tcp_savetcp, 0); 5135 #endif 5136 if (tp->snd_una == tp->snd_max) { 5137 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 5138 tp->t_acktime = 0; 5139 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 5140 } 5141 /* Wake up the socket if we have room to write more */ 5142 sowwakeup(so); 5143 if (sbavail(&so->so_snd)) { 5144 rack->r_wanted_output++; 5145 } 5146 return (1); 5147 } 5148 5149 /* 5150 * Return value of 1, the TCB is unlocked and most 5151 * likely gone, return value of 0, the TCP is still 5152 * locked. 5153 */ 5154 static int 5155 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 5156 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5157 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5158 { 5159 int32_t ret_val = 0; 5160 int32_t todrop; 5161 int32_t ourfinisacked = 0; 5162 5163 rack_calc_rwin(so, tp); 5164 /* 5165 * If the state is SYN_SENT: if seg contains an ACK, but not for our 5166 * SYN, drop the input. if seg contains a RST, then drop the 5167 * connection. if seg does not contain SYN, then drop it. Otherwise 5168 * this is an acceptable SYN segment initialize tp->rcv_nxt and 5169 * tp->irs if seg contains ack then advance tp->snd_una if seg 5170 * contains an ECE and ECN support is enabled, the stream is ECN 5171 * capable. if SYN has been acked change to ESTABLISHED else 5172 * SYN_RCVD state arrange for segment to be acked (eventually) 5173 * continue processing rest of data/controls, beginning with URG 5174 */ 5175 if ((thflags & TH_ACK) && 5176 (SEQ_LEQ(th->th_ack, tp->iss) || 5177 SEQ_GT(th->th_ack, tp->snd_max))) { 5178 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5179 return (1); 5180 } 5181 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 5182 TCP_PROBE5(connect__refused, NULL, tp, 5183 mtod(m, const char *), tp, th); 5184 tp = tcp_drop(tp, ECONNREFUSED); 5185 rack_do_drop(m, tp); 5186 return (1); 5187 } 5188 if (thflags & TH_RST) { 5189 rack_do_drop(m, tp); 5190 return (1); 5191 } 5192 if (!(thflags & TH_SYN)) { 5193 rack_do_drop(m, tp); 5194 return (1); 5195 } 5196 tp->irs = th->th_seq; 5197 tcp_rcvseqinit(tp); 5198 if (thflags & TH_ACK) { 5199 int tfo_partial = 0; 5200 5201 TCPSTAT_INC(tcps_connects); 5202 soisconnected(so); 5203 #ifdef MAC 5204 mac_socketpeer_set_from_mbuf(m, so); 5205 #endif 5206 /* Do window scaling on this connection? */ 5207 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 5208 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 5209 tp->rcv_scale = tp->request_r_scale; 5210 } 5211 tp->rcv_adv += min(tp->rcv_wnd, 5212 TCP_MAXWIN << tp->rcv_scale); 5213 /* 5214 * If not all the data that was sent in the TFO SYN 5215 * has been acked, resend the remainder right away. 5216 */ 5217 if (IS_FASTOPEN(tp->t_flags) && 5218 (tp->snd_una != tp->snd_max)) { 5219 tp->snd_nxt = th->th_ack; 5220 tfo_partial = 1; 5221 } 5222 /* 5223 * If there's data, delay ACK; if there's also a FIN ACKNOW 5224 * will be turned on later. 5225 */ 5226 if (DELAY_ACK(tp, tlen) && tlen != 0 && (tfo_partial == 0)) { 5227 rack_timer_cancel(tp, (struct tcp_rack *)tp->t_fb_ptr, 5228 ((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rcvtime, __LINE__); 5229 tp->t_flags |= TF_DELACK; 5230 } else { 5231 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output++; 5232 tp->t_flags |= TF_ACKNOW; 5233 } 5234 5235 if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 5236 V_tcp_do_ecn) { 5237 tp->t_flags |= TF_ECN_PERMIT; 5238 TCPSTAT_INC(tcps_ecn_shs); 5239 } 5240 if (SEQ_GT(th->th_ack, tp->snd_una)) { 5241 /* 5242 * We advance snd_una for the 5243 * fast open case. If th_ack is 5244 * acknowledging data beyond 5245 * snd_una we can't just call 5246 * ack-processing since the 5247 * data stream in our send-map 5248 * will start at snd_una + 1 (one 5249 * beyond the SYN). If its just 5250 * equal we don't need to do that 5251 * and there is no send_map. 5252 */ 5253 tp->snd_una++; 5254 } 5255 /* 5256 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 5257 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 5258 */ 5259 tp->t_starttime = ticks; 5260 if (tp->t_flags & TF_NEEDFIN) { 5261 tcp_state_change(tp, TCPS_FIN_WAIT_1); 5262 tp->t_flags &= ~TF_NEEDFIN; 5263 thflags &= ~TH_SYN; 5264 } else { 5265 tcp_state_change(tp, TCPS_ESTABLISHED); 5266 TCP_PROBE5(connect__established, NULL, tp, 5267 mtod(m, const char *), tp, th); 5268 cc_conn_init(tp); 5269 } 5270 } else { 5271 /* 5272 * Received initial SYN in SYN-SENT[*] state => simultaneous 5273 * open. If segment contains CC option and there is a 5274 * cached CC, apply TAO test. If it succeeds, connection is * 5275 * half-synchronized. Otherwise, do 3-way handshake: 5276 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 5277 * there was no CC option, clear cached CC value. 5278 */ 5279 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 5280 tcp_state_change(tp, TCPS_SYN_RECEIVED); 5281 } 5282 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 5283 INP_WLOCK_ASSERT(tp->t_inpcb); 5284 /* 5285 * Advance th->th_seq to correspond to first data byte. If data, 5286 * trim to stay within window, dropping FIN if necessary. 5287 */ 5288 th->th_seq++; 5289 if (tlen > tp->rcv_wnd) { 5290 todrop = tlen - tp->rcv_wnd; 5291 m_adj(m, -todrop); 5292 tlen = tp->rcv_wnd; 5293 thflags &= ~TH_FIN; 5294 TCPSTAT_INC(tcps_rcvpackafterwin); 5295 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 5296 } 5297 tp->snd_wl1 = th->th_seq - 1; 5298 tp->rcv_up = th->th_seq; 5299 /* 5300 * Client side of transaction: already sent SYN and data. If the 5301 * remote host used T/TCP to validate the SYN, our data will be 5302 * ACK'd; if so, enter normal data segment processing in the middle 5303 * of step 5, ack processing. Otherwise, goto step 6. 5304 */ 5305 if (thflags & TH_ACK) { 5306 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 5307 return (ret_val); 5308 /* We may have changed to FIN_WAIT_1 above */ 5309 if (tp->t_state == TCPS_FIN_WAIT_1) { 5310 /* 5311 * In FIN_WAIT_1 STATE in addition to the processing 5312 * for the ESTABLISHED state if our FIN is now 5313 * acknowledged then enter FIN_WAIT_2. 5314 */ 5315 if (ourfinisacked) { 5316 /* 5317 * If we can't receive any more data, then 5318 * closing user can proceed. Starting the 5319 * timer is contrary to the specification, 5320 * but if we don't get a FIN we'll hang 5321 * forever. 5322 * 5323 * XXXjl: we should release the tp also, and 5324 * use a compressed state. 5325 */ 5326 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5327 soisdisconnected(so); 5328 tcp_timer_activate(tp, TT_2MSL, 5329 (tcp_fast_finwait2_recycle ? 5330 tcp_finwait2_timeout : 5331 TP_MAXIDLE(tp))); 5332 } 5333 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5334 } 5335 } 5336 } 5337 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5338 tiwin, thflags, nxt_pkt)); 5339 } 5340 5341 /* 5342 * Return value of 1, the TCB is unlocked and most 5343 * likely gone, return value of 0, the TCP is still 5344 * locked. 5345 */ 5346 static int 5347 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 5348 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5349 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5350 { 5351 int32_t ret_val = 0; 5352 int32_t ourfinisacked = 0; 5353 5354 rack_calc_rwin(so, tp); 5355 5356 if ((thflags & TH_ACK) && 5357 (SEQ_LEQ(th->th_ack, tp->snd_una) || 5358 SEQ_GT(th->th_ack, tp->snd_max))) { 5359 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5360 return (1); 5361 } 5362 if (IS_FASTOPEN(tp->t_flags)) { 5363 /* 5364 * When a TFO connection is in SYN_RECEIVED, the 5365 * only valid packets are the initial SYN, a 5366 * retransmit/copy of the initial SYN (possibly with 5367 * a subset of the original data), a valid ACK, a 5368 * FIN, or a RST. 5369 */ 5370 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 5371 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5372 return (1); 5373 } else if (thflags & TH_SYN) { 5374 /* non-initial SYN is ignored */ 5375 struct tcp_rack *rack; 5376 5377 rack = (struct tcp_rack *)tp->t_fb_ptr; 5378 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 5379 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 5380 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 5381 rack_do_drop(m, NULL); 5382 return (0); 5383 } 5384 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 5385 rack_do_drop(m, NULL); 5386 return (0); 5387 } 5388 } 5389 if (thflags & TH_RST) 5390 return (rack_process_rst(m, th, so, tp)); 5391 /* 5392 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5393 * it's less than ts_recent, drop it. 5394 */ 5395 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5396 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5397 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5398 return (ret_val); 5399 } 5400 /* 5401 * In the SYN-RECEIVED state, validate that the packet belongs to 5402 * this connection before trimming the data to fit the receive 5403 * window. Check the sequence number versus IRS since we know the 5404 * sequence numbers haven't wrapped. This is a partial fix for the 5405 * "LAND" DoS attack. 5406 */ 5407 if (SEQ_LT(th->th_seq, tp->irs)) { 5408 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5409 return (1); 5410 } 5411 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5412 return (ret_val); 5413 } 5414 /* 5415 * If last ACK falls within this segment's sequence numbers, record 5416 * its timestamp. NOTE: 1) That the test incorporates suggestions 5417 * from the latest proposal of the tcplw@cray.com list (Braden 5418 * 1993/04/26). 2) That updating only on newer timestamps interferes 5419 * with our earlier PAWS tests, so this check should be solely 5420 * predicated on the sequence space of this segment. 3) That we 5421 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5422 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5423 * SEG.Len, This modified check allows us to overcome RFC1323's 5424 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5425 * p.869. In such cases, we can still calculate the RTT correctly 5426 * when RCV.NXT == Last.ACK.Sent. 5427 */ 5428 if ((to->to_flags & TOF_TS) != 0 && 5429 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5430 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5431 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5432 tp->ts_recent_age = tcp_ts_getticks(); 5433 tp->ts_recent = to->to_tsval; 5434 } 5435 tp->snd_wnd = tiwin; 5436 /* 5437 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5438 * is on (half-synchronized state), then queue data for later 5439 * processing; else drop segment and return. 5440 */ 5441 if ((thflags & TH_ACK) == 0) { 5442 if (IS_FASTOPEN(tp->t_flags)) { 5443 cc_conn_init(tp); 5444 } 5445 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5446 tiwin, thflags, nxt_pkt)); 5447 } 5448 TCPSTAT_INC(tcps_connects); 5449 soisconnected(so); 5450 /* Do window scaling? */ 5451 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 5452 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 5453 tp->rcv_scale = tp->request_r_scale; 5454 } 5455 /* 5456 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 5457 * FIN-WAIT-1 5458 */ 5459 tp->t_starttime = ticks; 5460 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 5461 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 5462 tp->t_tfo_pending = NULL; 5463 5464 /* 5465 * Account for the ACK of our SYN prior to 5466 * regular ACK processing below. 5467 */ 5468 tp->snd_una++; 5469 } 5470 if (tp->t_flags & TF_NEEDFIN) { 5471 tcp_state_change(tp, TCPS_FIN_WAIT_1); 5472 tp->t_flags &= ~TF_NEEDFIN; 5473 } else { 5474 tcp_state_change(tp, TCPS_ESTABLISHED); 5475 TCP_PROBE5(accept__established, NULL, tp, 5476 mtod(m, const char *), tp, th); 5477 /* 5478 * TFO connections call cc_conn_init() during SYN 5479 * processing. Calling it again here for such connections 5480 * is not harmless as it would undo the snd_cwnd reduction 5481 * that occurs when a TFO SYN|ACK is retransmitted. 5482 */ 5483 if (!IS_FASTOPEN(tp->t_flags)) 5484 cc_conn_init(tp); 5485 } 5486 /* 5487 * If segment contains data or ACK, will call tcp_reass() later; if 5488 * not, do so now to pass queued data to user. 5489 */ 5490 if (tlen == 0 && (thflags & TH_FIN) == 0) 5491 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 5492 (struct mbuf *)0); 5493 tp->snd_wl1 = th->th_seq - 1; 5494 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5495 return (ret_val); 5496 } 5497 if (tp->t_state == TCPS_FIN_WAIT_1) { 5498 /* We could have went to FIN_WAIT_1 (or EST) above */ 5499 /* 5500 * In FIN_WAIT_1 STATE in addition to the processing for the 5501 * ESTABLISHED state if our FIN is now acknowledged then 5502 * enter FIN_WAIT_2. 5503 */ 5504 if (ourfinisacked) { 5505 /* 5506 * If we can't receive any more data, then closing 5507 * user can proceed. Starting the timer is contrary 5508 * to the specification, but if we don't get a FIN 5509 * we'll hang forever. 5510 * 5511 * XXXjl: we should release the tp also, and use a 5512 * compressed state. 5513 */ 5514 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5515 soisdisconnected(so); 5516 tcp_timer_activate(tp, TT_2MSL, 5517 (tcp_fast_finwait2_recycle ? 5518 tcp_finwait2_timeout : 5519 TP_MAXIDLE(tp))); 5520 } 5521 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5522 } 5523 } 5524 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5525 tiwin, thflags, nxt_pkt)); 5526 } 5527 5528 /* 5529 * Return value of 1, the TCB is unlocked and most 5530 * likely gone, return value of 0, the TCP is still 5531 * locked. 5532 */ 5533 static int 5534 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 5535 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5536 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5537 { 5538 int32_t ret_val = 0; 5539 5540 /* 5541 * Header prediction: check for the two common cases of a 5542 * uni-directional data xfer. If the packet has no control flags, 5543 * is in-sequence, the window didn't change and we're not 5544 * retransmitting, it's a candidate. If the length is zero and the 5545 * ack moved forward, we're the sender side of the xfer. Just free 5546 * the data acked & wake any higher level process that was blocked 5547 * waiting for space. If the length is non-zero and the ack didn't 5548 * move, we're the receiver side. If we're getting packets in-order 5549 * (the reassembly queue is empty), add the data toc The socket 5550 * buffer and note that we need a delayed ack. Make sure that the 5551 * hidden state-flags are also off. Since we check for 5552 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 5553 */ 5554 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 5555 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 5556 __predict_true(SEGQ_EMPTY(tp)) && 5557 __predict_true(th->th_seq == tp->rcv_nxt)) { 5558 struct tcp_rack *rack; 5559 5560 rack = (struct tcp_rack *)tp->t_fb_ptr; 5561 if (tlen == 0) { 5562 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 5563 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) { 5564 return (0); 5565 } 5566 } else { 5567 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 5568 tiwin, nxt_pkt)) { 5569 return (0); 5570 } 5571 } 5572 } 5573 rack_calc_rwin(so, tp); 5574 5575 if (thflags & TH_RST) 5576 return (rack_process_rst(m, th, so, tp)); 5577 5578 /* 5579 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5580 * synchronized state. 5581 */ 5582 if (thflags & TH_SYN) { 5583 rack_challenge_ack(m, th, tp, &ret_val); 5584 return (ret_val); 5585 } 5586 /* 5587 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5588 * it's less than ts_recent, drop it. 5589 */ 5590 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5591 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5592 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5593 return (ret_val); 5594 } 5595 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5596 return (ret_val); 5597 } 5598 /* 5599 * If last ACK falls within this segment's sequence numbers, record 5600 * its timestamp. NOTE: 1) That the test incorporates suggestions 5601 * from the latest proposal of the tcplw@cray.com list (Braden 5602 * 1993/04/26). 2) That updating only on newer timestamps interferes 5603 * with our earlier PAWS tests, so this check should be solely 5604 * predicated on the sequence space of this segment. 3) That we 5605 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5606 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5607 * SEG.Len, This modified check allows us to overcome RFC1323's 5608 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5609 * p.869. In such cases, we can still calculate the RTT correctly 5610 * when RCV.NXT == Last.ACK.Sent. 5611 */ 5612 if ((to->to_flags & TOF_TS) != 0 && 5613 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5614 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5615 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5616 tp->ts_recent_age = tcp_ts_getticks(); 5617 tp->ts_recent = to->to_tsval; 5618 } 5619 /* 5620 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5621 * is on (half-synchronized state), then queue data for later 5622 * processing; else drop segment and return. 5623 */ 5624 if ((thflags & TH_ACK) == 0) { 5625 if (tp->t_flags & TF_NEEDSYN) { 5626 5627 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5628 tiwin, thflags, nxt_pkt)); 5629 5630 } else if (tp->t_flags & TF_ACKNOW) { 5631 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5632 return (ret_val); 5633 } else { 5634 rack_do_drop(m, NULL); 5635 return (0); 5636 } 5637 } 5638 /* 5639 * Ack processing. 5640 */ 5641 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 5642 return (ret_val); 5643 } 5644 if (sbavail(&so->so_snd)) { 5645 if (rack_progress_timeout_check(tp)) { 5646 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5647 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5648 return (1); 5649 } 5650 } 5651 /* State changes only happen in rack_process_data() */ 5652 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5653 tiwin, thflags, nxt_pkt)); 5654 } 5655 5656 /* 5657 * Return value of 1, the TCB is unlocked and most 5658 * likely gone, return value of 0, the TCP is still 5659 * locked. 5660 */ 5661 static int 5662 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 5663 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5664 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5665 { 5666 int32_t ret_val = 0; 5667 5668 rack_calc_rwin(so, tp); 5669 if (thflags & TH_RST) 5670 return (rack_process_rst(m, th, so, tp)); 5671 /* 5672 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5673 * synchronized state. 5674 */ 5675 if (thflags & TH_SYN) { 5676 rack_challenge_ack(m, th, tp, &ret_val); 5677 return (ret_val); 5678 } 5679 /* 5680 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5681 * it's less than ts_recent, drop it. 5682 */ 5683 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5684 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5685 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5686 return (ret_val); 5687 } 5688 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5689 return (ret_val); 5690 } 5691 /* 5692 * If last ACK falls within this segment's sequence numbers, record 5693 * its timestamp. NOTE: 1) That the test incorporates suggestions 5694 * from the latest proposal of the tcplw@cray.com list (Braden 5695 * 1993/04/26). 2) That updating only on newer timestamps interferes 5696 * with our earlier PAWS tests, so this check should be solely 5697 * predicated on the sequence space of this segment. 3) That we 5698 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5699 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5700 * SEG.Len, This modified check allows us to overcome RFC1323's 5701 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5702 * p.869. In such cases, we can still calculate the RTT correctly 5703 * when RCV.NXT == Last.ACK.Sent. 5704 */ 5705 if ((to->to_flags & TOF_TS) != 0 && 5706 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5707 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5708 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5709 tp->ts_recent_age = tcp_ts_getticks(); 5710 tp->ts_recent = to->to_tsval; 5711 } 5712 /* 5713 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5714 * is on (half-synchronized state), then queue data for later 5715 * processing; else drop segment and return. 5716 */ 5717 if ((thflags & TH_ACK) == 0) { 5718 if (tp->t_flags & TF_NEEDSYN) { 5719 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5720 tiwin, thflags, nxt_pkt)); 5721 5722 } else if (tp->t_flags & TF_ACKNOW) { 5723 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5724 return (ret_val); 5725 } else { 5726 rack_do_drop(m, NULL); 5727 return (0); 5728 } 5729 } 5730 /* 5731 * Ack processing. 5732 */ 5733 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 5734 return (ret_val); 5735 } 5736 if (sbavail(&so->so_snd)) { 5737 if (rack_progress_timeout_check(tp)) { 5738 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5739 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5740 return (1); 5741 } 5742 } 5743 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5744 tiwin, thflags, nxt_pkt)); 5745 } 5746 5747 static int 5748 rack_check_data_after_close(struct mbuf *m, 5749 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so) 5750 { 5751 struct tcp_rack *rack; 5752 5753 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 5754 rack = (struct tcp_rack *)tp->t_fb_ptr; 5755 if (rack->rc_allow_data_af_clo == 0) { 5756 close_now: 5757 tp = tcp_close(tp); 5758 TCPSTAT_INC(tcps_rcvafterclose); 5759 rack_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 5760 return (1); 5761 } 5762 if (sbavail(&so->so_snd) == 0) 5763 goto close_now; 5764 /* Ok we allow data that is ignored and a followup reset */ 5765 tp->rcv_nxt = th->th_seq + *tlen; 5766 tp->t_flags2 |= TF2_DROP_AF_DATA; 5767 rack->r_wanted_output = 1; 5768 *tlen = 0; 5769 return (0); 5770 } 5771 5772 /* 5773 * Return value of 1, the TCB is unlocked and most 5774 * likely gone, return value of 0, the TCP is still 5775 * locked. 5776 */ 5777 static int 5778 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 5779 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5780 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5781 { 5782 int32_t ret_val = 0; 5783 int32_t ourfinisacked = 0; 5784 5785 rack_calc_rwin(so, tp); 5786 5787 if (thflags & TH_RST) 5788 return (rack_process_rst(m, th, so, tp)); 5789 /* 5790 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5791 * synchronized state. 5792 */ 5793 if (thflags & TH_SYN) { 5794 rack_challenge_ack(m, th, tp, &ret_val); 5795 return (ret_val); 5796 } 5797 /* 5798 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5799 * it's less than ts_recent, drop it. 5800 */ 5801 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5802 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5803 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5804 return (ret_val); 5805 } 5806 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5807 return (ret_val); 5808 } 5809 /* 5810 * If new data are received on a connection after the user processes 5811 * are gone, then RST the other end. 5812 */ 5813 if ((so->so_state & SS_NOFDREF) && tlen) { 5814 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 5815 return (1); 5816 } 5817 /* 5818 * If last ACK falls within this segment's sequence numbers, record 5819 * its timestamp. NOTE: 1) That the test incorporates suggestions 5820 * from the latest proposal of the tcplw@cray.com list (Braden 5821 * 1993/04/26). 2) That updating only on newer timestamps interferes 5822 * with our earlier PAWS tests, so this check should be solely 5823 * predicated on the sequence space of this segment. 3) That we 5824 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5825 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5826 * SEG.Len, This modified check allows us to overcome RFC1323's 5827 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5828 * p.869. In such cases, we can still calculate the RTT correctly 5829 * when RCV.NXT == Last.ACK.Sent. 5830 */ 5831 if ((to->to_flags & TOF_TS) != 0 && 5832 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5833 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5834 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5835 tp->ts_recent_age = tcp_ts_getticks(); 5836 tp->ts_recent = to->to_tsval; 5837 } 5838 /* 5839 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5840 * is on (half-synchronized state), then queue data for later 5841 * processing; else drop segment and return. 5842 */ 5843 if ((thflags & TH_ACK) == 0) { 5844 if (tp->t_flags & TF_NEEDSYN) { 5845 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5846 tiwin, thflags, nxt_pkt)); 5847 } else if (tp->t_flags & TF_ACKNOW) { 5848 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5849 return (ret_val); 5850 } else { 5851 rack_do_drop(m, NULL); 5852 return (0); 5853 } 5854 } 5855 /* 5856 * Ack processing. 5857 */ 5858 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5859 return (ret_val); 5860 } 5861 if (ourfinisacked) { 5862 /* 5863 * If we can't receive any more data, then closing user can 5864 * proceed. Starting the timer is contrary to the 5865 * specification, but if we don't get a FIN we'll hang 5866 * forever. 5867 * 5868 * XXXjl: we should release the tp also, and use a 5869 * compressed state. 5870 */ 5871 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5872 soisdisconnected(so); 5873 tcp_timer_activate(tp, TT_2MSL, 5874 (tcp_fast_finwait2_recycle ? 5875 tcp_finwait2_timeout : 5876 TP_MAXIDLE(tp))); 5877 } 5878 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5879 } 5880 if (sbavail(&so->so_snd)) { 5881 if (rack_progress_timeout_check(tp)) { 5882 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5883 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5884 return (1); 5885 } 5886 } 5887 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5888 tiwin, thflags, nxt_pkt)); 5889 } 5890 5891 /* 5892 * Return value of 1, the TCB is unlocked and most 5893 * likely gone, return value of 0, the TCP is still 5894 * locked. 5895 */ 5896 static int 5897 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 5898 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5899 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5900 { 5901 int32_t ret_val = 0; 5902 int32_t ourfinisacked = 0; 5903 5904 rack_calc_rwin(so, tp); 5905 5906 if (thflags & TH_RST) 5907 return (rack_process_rst(m, th, so, tp)); 5908 /* 5909 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5910 * synchronized state. 5911 */ 5912 if (thflags & TH_SYN) { 5913 rack_challenge_ack(m, th, tp, &ret_val); 5914 return (ret_val); 5915 } 5916 /* 5917 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5918 * it's less than ts_recent, drop it. 5919 */ 5920 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5921 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5922 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5923 return (ret_val); 5924 } 5925 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5926 return (ret_val); 5927 } 5928 /* 5929 * If new data are received on a connection after the user processes 5930 * are gone, then RST the other end. 5931 */ 5932 if ((so->so_state & SS_NOFDREF) && tlen) { 5933 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 5934 return (1); 5935 } 5936 /* 5937 * If last ACK falls within this segment's sequence numbers, record 5938 * its timestamp. NOTE: 1) That the test incorporates suggestions 5939 * from the latest proposal of the tcplw@cray.com list (Braden 5940 * 1993/04/26). 2) That updating only on newer timestamps interferes 5941 * with our earlier PAWS tests, so this check should be solely 5942 * predicated on the sequence space of this segment. 3) That we 5943 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5944 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5945 * SEG.Len, This modified check allows us to overcome RFC1323's 5946 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5947 * p.869. In such cases, we can still calculate the RTT correctly 5948 * when RCV.NXT == Last.ACK.Sent. 5949 */ 5950 if ((to->to_flags & TOF_TS) != 0 && 5951 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5952 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5953 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5954 tp->ts_recent_age = tcp_ts_getticks(); 5955 tp->ts_recent = to->to_tsval; 5956 } 5957 /* 5958 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5959 * is on (half-synchronized state), then queue data for later 5960 * processing; else drop segment and return. 5961 */ 5962 if ((thflags & TH_ACK) == 0) { 5963 if (tp->t_flags & TF_NEEDSYN) { 5964 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5965 tiwin, thflags, nxt_pkt)); 5966 } else if (tp->t_flags & TF_ACKNOW) { 5967 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5968 return (ret_val); 5969 } else { 5970 rack_do_drop(m, NULL); 5971 return (0); 5972 } 5973 } 5974 /* 5975 * Ack processing. 5976 */ 5977 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5978 return (ret_val); 5979 } 5980 if (ourfinisacked) { 5981 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 5982 tcp_twstart(tp); 5983 m_freem(m); 5984 return (1); 5985 } 5986 if (sbavail(&so->so_snd)) { 5987 if (rack_progress_timeout_check(tp)) { 5988 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5989 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5990 return (1); 5991 } 5992 } 5993 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5994 tiwin, thflags, nxt_pkt)); 5995 } 5996 5997 /* 5998 * Return value of 1, the TCB is unlocked and most 5999 * likely gone, return value of 0, the TCP is still 6000 * locked. 6001 */ 6002 static int 6003 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 6004 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 6005 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 6006 { 6007 int32_t ret_val = 0; 6008 int32_t ourfinisacked = 0; 6009 6010 rack_calc_rwin(so, tp); 6011 6012 if (thflags & TH_RST) 6013 return (rack_process_rst(m, th, so, tp)); 6014 /* 6015 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 6016 * synchronized state. 6017 */ 6018 if (thflags & TH_SYN) { 6019 rack_challenge_ack(m, th, tp, &ret_val); 6020 return (ret_val); 6021 } 6022 /* 6023 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 6024 * it's less than ts_recent, drop it. 6025 */ 6026 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 6027 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 6028 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 6029 return (ret_val); 6030 } 6031 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 6032 return (ret_val); 6033 } 6034 /* 6035 * If new data are received on a connection after the user processes 6036 * are gone, then RST the other end. 6037 */ 6038 if ((so->so_state & SS_NOFDREF) && tlen) { 6039 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 6040 return (1); 6041 } 6042 /* 6043 * If last ACK falls within this segment's sequence numbers, record 6044 * its timestamp. NOTE: 1) That the test incorporates suggestions 6045 * from the latest proposal of the tcplw@cray.com list (Braden 6046 * 1993/04/26). 2) That updating only on newer timestamps interferes 6047 * with our earlier PAWS tests, so this check should be solely 6048 * predicated on the sequence space of this segment. 3) That we 6049 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 6050 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 6051 * SEG.Len, This modified check allows us to overcome RFC1323's 6052 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 6053 * p.869. In such cases, we can still calculate the RTT correctly 6054 * when RCV.NXT == Last.ACK.Sent. 6055 */ 6056 if ((to->to_flags & TOF_TS) != 0 && 6057 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 6058 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 6059 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 6060 tp->ts_recent_age = tcp_ts_getticks(); 6061 tp->ts_recent = to->to_tsval; 6062 } 6063 /* 6064 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 6065 * is on (half-synchronized state), then queue data for later 6066 * processing; else drop segment and return. 6067 */ 6068 if ((thflags & TH_ACK) == 0) { 6069 if (tp->t_flags & TF_NEEDSYN) { 6070 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6071 tiwin, thflags, nxt_pkt)); 6072 } else if (tp->t_flags & TF_ACKNOW) { 6073 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 6074 return (ret_val); 6075 } else { 6076 rack_do_drop(m, NULL); 6077 return (0); 6078 } 6079 } 6080 /* 6081 * case TCPS_LAST_ACK: Ack processing. 6082 */ 6083 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 6084 return (ret_val); 6085 } 6086 if (ourfinisacked) { 6087 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 6088 tp = tcp_close(tp); 6089 rack_do_drop(m, tp); 6090 return (1); 6091 } 6092 if (sbavail(&so->so_snd)) { 6093 if (rack_progress_timeout_check(tp)) { 6094 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6095 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6096 return (1); 6097 } 6098 } 6099 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6100 tiwin, thflags, nxt_pkt)); 6101 } 6102 6103 6104 /* 6105 * Return value of 1, the TCB is unlocked and most 6106 * likely gone, return value of 0, the TCP is still 6107 * locked. 6108 */ 6109 static int 6110 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 6111 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 6112 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 6113 { 6114 int32_t ret_val = 0; 6115 int32_t ourfinisacked = 0; 6116 6117 rack_calc_rwin(so, tp); 6118 6119 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 6120 if (thflags & TH_RST) 6121 return (rack_process_rst(m, th, so, tp)); 6122 /* 6123 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 6124 * synchronized state. 6125 */ 6126 if (thflags & TH_SYN) { 6127 rack_challenge_ack(m, th, tp, &ret_val); 6128 return (ret_val); 6129 } 6130 /* 6131 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 6132 * it's less than ts_recent, drop it. 6133 */ 6134 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 6135 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 6136 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 6137 return (ret_val); 6138 } 6139 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 6140 return (ret_val); 6141 } 6142 /* 6143 * If new data are received on a connection after the user processes 6144 * are gone, then RST the other end. 6145 */ 6146 if ((so->so_state & SS_NOFDREF) && 6147 tlen) { 6148 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 6149 return (1); 6150 } 6151 /* 6152 * If last ACK falls within this segment's sequence numbers, record 6153 * its timestamp. NOTE: 1) That the test incorporates suggestions 6154 * from the latest proposal of the tcplw@cray.com list (Braden 6155 * 1993/04/26). 2) That updating only on newer timestamps interferes 6156 * with our earlier PAWS tests, so this check should be solely 6157 * predicated on the sequence space of this segment. 3) That we 6158 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 6159 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 6160 * SEG.Len, This modified check allows us to overcome RFC1323's 6161 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 6162 * p.869. In such cases, we can still calculate the RTT correctly 6163 * when RCV.NXT == Last.ACK.Sent. 6164 */ 6165 if ((to->to_flags & TOF_TS) != 0 && 6166 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 6167 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 6168 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 6169 tp->ts_recent_age = tcp_ts_getticks(); 6170 tp->ts_recent = to->to_tsval; 6171 } 6172 /* 6173 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 6174 * is on (half-synchronized state), then queue data for later 6175 * processing; else drop segment and return. 6176 */ 6177 if ((thflags & TH_ACK) == 0) { 6178 if (tp->t_flags & TF_NEEDSYN) { 6179 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6180 tiwin, thflags, nxt_pkt)); 6181 } else if (tp->t_flags & TF_ACKNOW) { 6182 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 6183 return (ret_val); 6184 } else { 6185 rack_do_drop(m, NULL); 6186 return (0); 6187 } 6188 } 6189 /* 6190 * Ack processing. 6191 */ 6192 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 6193 return (ret_val); 6194 } 6195 if (sbavail(&so->so_snd)) { 6196 if (rack_progress_timeout_check(tp)) { 6197 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6198 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6199 return (1); 6200 } 6201 } 6202 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6203 tiwin, thflags, nxt_pkt)); 6204 } 6205 6206 6207 static void inline 6208 rack_clear_rate_sample(struct tcp_rack *rack) 6209 { 6210 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY; 6211 rack->r_ctl.rack_rs.rs_rtt_cnt = 0; 6212 rack->r_ctl.rack_rs.rs_rtt_tot = 0; 6213 } 6214 6215 static int 6216 rack_init(struct tcpcb *tp) 6217 { 6218 struct tcp_rack *rack = NULL; 6219 6220 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT); 6221 if (tp->t_fb_ptr == NULL) { 6222 /* 6223 * We need to allocate memory but cant. The INP and INP_INFO 6224 * locks and they are recusive (happens during setup. So a 6225 * scheme to drop the locks fails :( 6226 * 6227 */ 6228 return (ENOMEM); 6229 } 6230 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack)); 6231 6232 rack = (struct tcp_rack *)tp->t_fb_ptr; 6233 TAILQ_INIT(&rack->r_ctl.rc_map); 6234 TAILQ_INIT(&rack->r_ctl.rc_free); 6235 TAILQ_INIT(&rack->r_ctl.rc_tmap); 6236 rack->rc_tp = tp; 6237 if (tp->t_inpcb) { 6238 rack->rc_inp = tp->t_inpcb; 6239 } 6240 /* Probably not needed but lets be sure */ 6241 rack_clear_rate_sample(rack); 6242 rack->r_cpu = 0; 6243 rack->r_ctl.rc_reorder_fade = rack_reorder_fade; 6244 rack->rc_allow_data_af_clo = rack_ignore_data_after_close; 6245 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh; 6246 rack->rc_pace_reduce = rack_slot_reduction; 6247 if (V_tcp_delack_enabled) 6248 tp->t_delayed_ack = 1; 6249 else 6250 tp->t_delayed_ack = 0; 6251 rack->rc_pace_max_segs = rack_hptsi_segments; 6252 rack->r_ctl.rc_early_recovery_segs = rack_early_recovery_max_seg; 6253 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh; 6254 rack->r_ctl.rc_pkt_delay = rack_pkt_delay; 6255 rack->r_ctl.rc_prop_reduce = rack_use_proportional_reduce; 6256 rack->r_idle_reduce_largest = rack_reduce_largest_on_idle; 6257 rack->r_enforce_min_pace = rack_min_pace_time; 6258 rack->r_min_pace_seg_thresh = rack_min_pace_time_seg_req; 6259 rack->r_ctl.rc_prop_rate = rack_proportional_rate; 6260 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp; 6261 rack->r_ctl.rc_early_recovery = rack_early_recovery; 6262 rack->rc_always_pace = rack_pace_every_seg; 6263 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method; 6264 rack->rack_tlp_threshold_use = rack_tlp_threshold_use; 6265 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr; 6266 rack->r_ctl.rc_min_to = rack_min_to; 6267 rack->r_ctl.rc_prr_inc_var = rack_inc_var; 6268 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6269 if (tp->snd_una != tp->snd_max) { 6270 /* Create a send map for the current outstanding data */ 6271 struct rack_sendmap *rsm; 6272 6273 rsm = rack_alloc(rack); 6274 if (rsm == NULL) { 6275 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 6276 tp->t_fb_ptr = NULL; 6277 return (ENOMEM); 6278 } 6279 rsm->r_flags = RACK_OVERMAX; 6280 rsm->r_tim_lastsent[0] = tcp_ts_getticks(); 6281 rsm->r_rtr_cnt = 1; 6282 rsm->r_rtr_bytes = 0; 6283 rsm->r_start = tp->snd_una; 6284 rsm->r_end = tp->snd_max; 6285 rsm->r_sndcnt = 0; 6286 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_map, rsm, r_next); 6287 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 6288 rsm->r_in_tmap = 1; 6289 } 6290 return (0); 6291 } 6292 6293 static int 6294 rack_handoff_ok(struct tcpcb *tp) 6295 { 6296 if ((tp->t_state == TCPS_CLOSED) || 6297 (tp->t_state == TCPS_LISTEN)) { 6298 /* Sure no problem though it may not stick */ 6299 return (0); 6300 } 6301 if ((tp->t_state == TCPS_SYN_SENT) || 6302 (tp->t_state == TCPS_SYN_RECEIVED)) { 6303 /* 6304 * We really don't know you have to get to ESTAB or beyond 6305 * to tell. 6306 */ 6307 return (EAGAIN); 6308 } 6309 if (tp->t_flags & TF_SACK_PERMIT) { 6310 return (0); 6311 } 6312 /* 6313 * If we reach here we don't do SACK on this connection so we can 6314 * never do rack. 6315 */ 6316 return (EINVAL); 6317 } 6318 6319 static void 6320 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged) 6321 { 6322 if (tp->t_fb_ptr) { 6323 struct tcp_rack *rack; 6324 struct rack_sendmap *rsm; 6325 6326 rack = (struct tcp_rack *)tp->t_fb_ptr; 6327 #ifdef TCP_BLACKBOX 6328 tcp_log_flowend(tp); 6329 #endif 6330 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 6331 while (rsm) { 6332 TAILQ_REMOVE(&rack->r_ctl.rc_map, rsm, r_next); 6333 uma_zfree(rack_zone, rsm); 6334 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 6335 } 6336 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 6337 while (rsm) { 6338 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_next); 6339 uma_zfree(rack_zone, rsm); 6340 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 6341 } 6342 rack->rc_free_cnt = 0; 6343 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 6344 tp->t_fb_ptr = NULL; 6345 } 6346 } 6347 6348 static void 6349 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack) 6350 { 6351 switch (tp->t_state) { 6352 case TCPS_SYN_SENT: 6353 rack->r_state = TCPS_SYN_SENT; 6354 rack->r_substate = rack_do_syn_sent; 6355 break; 6356 case TCPS_SYN_RECEIVED: 6357 rack->r_state = TCPS_SYN_RECEIVED; 6358 rack->r_substate = rack_do_syn_recv; 6359 break; 6360 case TCPS_ESTABLISHED: 6361 rack->r_state = TCPS_ESTABLISHED; 6362 rack->r_substate = rack_do_established; 6363 break; 6364 case TCPS_CLOSE_WAIT: 6365 rack->r_state = TCPS_CLOSE_WAIT; 6366 rack->r_substate = rack_do_close_wait; 6367 break; 6368 case TCPS_FIN_WAIT_1: 6369 rack->r_state = TCPS_FIN_WAIT_1; 6370 rack->r_substate = rack_do_fin_wait_1; 6371 break; 6372 case TCPS_CLOSING: 6373 rack->r_state = TCPS_CLOSING; 6374 rack->r_substate = rack_do_closing; 6375 break; 6376 case TCPS_LAST_ACK: 6377 rack->r_state = TCPS_LAST_ACK; 6378 rack->r_substate = rack_do_lastack; 6379 break; 6380 case TCPS_FIN_WAIT_2: 6381 rack->r_state = TCPS_FIN_WAIT_2; 6382 rack->r_substate = rack_do_fin_wait_2; 6383 break; 6384 case TCPS_LISTEN: 6385 case TCPS_CLOSED: 6386 case TCPS_TIME_WAIT: 6387 default: 6388 #ifdef INVARIANTS 6389 panic("tcp tp:%p state:%d sees impossible state?", tp, tp->t_state); 6390 #endif 6391 break; 6392 }; 6393 } 6394 6395 6396 static void 6397 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb) 6398 { 6399 /* 6400 * We received an ack, and then did not 6401 * call send or were bounced out due to the 6402 * hpts was running. Now a timer is up as well, is 6403 * it the right timer? 6404 */ 6405 struct rack_sendmap *rsm; 6406 int tmr_up; 6407 6408 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 6409 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 6410 return; 6411 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6412 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 6413 (tmr_up == PACE_TMR_RXT)) { 6414 /* Should be an RXT */ 6415 return; 6416 } 6417 if (rsm == NULL) { 6418 /* Nothing outstanding? */ 6419 if (tp->t_flags & TF_DELACK) { 6420 if (tmr_up == PACE_TMR_DELACK) 6421 /* We are supposed to have delayed ack up and we do */ 6422 return; 6423 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) { 6424 /* 6425 * if we hit enobufs then we would expect the possiblity 6426 * of nothing outstanding and the RXT up (and the hptsi timer). 6427 */ 6428 return; 6429 } else if (((tcp_always_keepalive || 6430 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 6431 (tp->t_state <= TCPS_CLOSING)) && 6432 (tmr_up == PACE_TMR_KEEP) && 6433 (tp->snd_max == tp->snd_una)) { 6434 /* We should have keep alive up and we do */ 6435 return; 6436 } 6437 } 6438 if (rsm && (rsm->r_flags & RACK_SACK_PASSED)) { 6439 if ((tp->t_flags & TF_SENTFIN) && 6440 ((tp->snd_max - tp->snd_una) == 1) && 6441 (rsm->r_flags & RACK_HAS_FIN)) { 6442 /* needs to be a RXT */ 6443 if (tmr_up == PACE_TMR_RXT) 6444 return; 6445 } else if (tmr_up == PACE_TMR_RACK) 6446 return; 6447 } else if (SEQ_GT(tp->snd_max,tp->snd_una) && 6448 ((tmr_up == PACE_TMR_TLP) || 6449 (tmr_up == PACE_TMR_RXT))) { 6450 /* 6451 * Either a TLP or RXT is fine if no sack-passed 6452 * is in place and data is outstanding. 6453 */ 6454 return; 6455 } else if (tmr_up == PACE_TMR_DELACK) { 6456 /* 6457 * If the delayed ack was going to go off 6458 * before the rtx/tlp/rack timer were going to 6459 * expire, then that would be the timer in control. 6460 * Note we don't check the time here trusting the 6461 * code is correct. 6462 */ 6463 return; 6464 } 6465 /* 6466 * Ok the timer originally started is not what we want now. 6467 * We will force the hpts to be stopped if any, and restart 6468 * with the slot set to what was in the saved slot. 6469 */ 6470 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 6471 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6472 } 6473 6474 static void 6475 rack_hpts_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 6476 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, 6477 int32_t nxt_pkt, struct timeval *tv) 6478 { 6479 int32_t thflags, retval, did_out = 0; 6480 int32_t way_out = 0; 6481 uint32_t cts; 6482 uint32_t tiwin; 6483 struct tcpopt to; 6484 struct tcp_rack *rack; 6485 struct rack_sendmap *rsm; 6486 int32_t prev_state = 0; 6487 6488 cts = tcp_tv_to_mssectick(tv); 6489 rack = (struct tcp_rack *)tp->t_fb_ptr; 6490 6491 kern_prefetch(rack, &prev_state); 6492 prev_state = 0; 6493 thflags = th->th_flags; 6494 /* 6495 * If this is either a state-changing packet or current state isn't 6496 * established, we require a read lock on tcbinfo. Otherwise, we 6497 * allow the tcbinfo to be in either locked or unlocked, as the 6498 * caller may have unnecessarily acquired a lock due to a race. 6499 */ 6500 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 6501 tp->t_state != TCPS_ESTABLISHED) { 6502 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 6503 } 6504 INP_WLOCK_ASSERT(tp->t_inpcb); 6505 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 6506 __func__)); 6507 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 6508 __func__)); 6509 { 6510 union tcp_log_stackspecific log; 6511 6512 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 6513 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 6514 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 6515 TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 6516 tlen, &log, true); 6517 } 6518 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 6519 way_out = 4; 6520 goto done_with_input; 6521 } 6522 /* 6523 * If a segment with the ACK-bit set arrives in the SYN-SENT state 6524 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 6525 */ 6526 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 6527 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 6528 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6529 return; 6530 } 6531 /* 6532 * Segment received on connection. Reset idle time and keep-alive 6533 * timer. XXX: This should be done after segment validation to 6534 * ignore broken/spoofed segs. 6535 */ 6536 if (tp->t_idle_reduce && (tp->snd_max == tp->snd_una)) { 6537 #ifdef NETFLIX_CWV 6538 if ((tp->cwv_enabled) && 6539 ((tp->cwv_cwnd_valid == 0) && 6540 TCPS_HAVEESTABLISHED(tp->t_state) && 6541 (tp->snd_cwnd > tp->snd_cwv.init_cwnd))) { 6542 tcp_newcwv_nvp_closedown(tp); 6543 } else 6544 #endif 6545 if ((ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 6546 counter_u64_add(rack_input_idle_reduces, 1); 6547 rack_cc_after_idle(tp, 6548 (rack->r_idle_reduce_largest ? 1 :0)); 6549 } 6550 } 6551 rack->r_ctl.rc_rcvtime = cts; 6552 tp->t_rcvtime = ticks; 6553 6554 #ifdef NETFLIX_CWV 6555 if (tp->cwv_enabled) { 6556 if ((tp->cwv_cwnd_valid == 0) && 6557 TCPS_HAVEESTABLISHED(tp->t_state) && 6558 (tp->snd_cwnd > tp->snd_cwv.init_cwnd)) 6559 tcp_newcwv_nvp_closedown(tp); 6560 } 6561 #endif 6562 /* 6563 * Unscale the window into a 32-bit value. For the SYN_SENT state 6564 * the scale is zero. 6565 */ 6566 tiwin = th->th_win << tp->snd_scale; 6567 #ifdef NETFLIX_STATS 6568 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 6569 #endif 6570 /* 6571 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move 6572 * this to occur after we've validated the segment. 6573 */ 6574 if (tp->t_flags & TF_ECN_PERMIT) { 6575 if (thflags & TH_CWR) 6576 tp->t_flags &= ~TF_ECN_SND_ECE; 6577 switch (iptos & IPTOS_ECN_MASK) { 6578 case IPTOS_ECN_CE: 6579 tp->t_flags |= TF_ECN_SND_ECE; 6580 TCPSTAT_INC(tcps_ecn_ce); 6581 break; 6582 case IPTOS_ECN_ECT0: 6583 TCPSTAT_INC(tcps_ecn_ect0); 6584 break; 6585 case IPTOS_ECN_ECT1: 6586 TCPSTAT_INC(tcps_ecn_ect1); 6587 break; 6588 } 6589 /* Congestion experienced. */ 6590 if (thflags & TH_ECE) { 6591 rack_cong_signal(tp, th, CC_ECN); 6592 } 6593 } 6594 /* 6595 * Parse options on any incoming segment. 6596 */ 6597 tcp_dooptions(&to, (u_char *)(th + 1), 6598 (th->th_off << 2) - sizeof(struct tcphdr), 6599 (thflags & TH_SYN) ? TO_SYN : 0); 6600 6601 /* 6602 * If echoed timestamp is later than the current time, fall back to 6603 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 6604 * were used when this connection was established. 6605 */ 6606 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 6607 to.to_tsecr -= tp->ts_offset; 6608 if (TSTMP_GT(to.to_tsecr, cts)) 6609 to.to_tsecr = 0; 6610 } 6611 /* 6612 * If its the first time in we need to take care of options and 6613 * verify we can do SACK for rack! 6614 */ 6615 if (rack->r_state == 0) { 6616 /* Should be init'd by rack_init() */ 6617 KASSERT(rack->rc_inp != NULL, 6618 ("%s: rack->rc_inp unexpectedly NULL", __func__)); 6619 if (rack->rc_inp == NULL) { 6620 rack->rc_inp = tp->t_inpcb; 6621 } 6622 6623 /* 6624 * Process options only when we get SYN/ACK back. The SYN 6625 * case for incoming connections is handled in tcp_syncache. 6626 * According to RFC1323 the window field in a SYN (i.e., a 6627 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 6628 * this is traditional behavior, may need to be cleaned up. 6629 */ 6630 rack->r_cpu = inp_to_cpuid(tp->t_inpcb); 6631 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 6632 if ((to.to_flags & TOF_SCALE) && 6633 (tp->t_flags & TF_REQ_SCALE)) { 6634 tp->t_flags |= TF_RCVD_SCALE; 6635 tp->snd_scale = to.to_wscale; 6636 } 6637 /* 6638 * Initial send window. It will be updated with the 6639 * next incoming segment to the scaled value. 6640 */ 6641 tp->snd_wnd = th->th_win; 6642 if (to.to_flags & TOF_TS) { 6643 tp->t_flags |= TF_RCVD_TSTMP; 6644 tp->ts_recent = to.to_tsval; 6645 tp->ts_recent_age = cts; 6646 } 6647 if (to.to_flags & TOF_MSS) 6648 tcp_mss(tp, to.to_mss); 6649 if ((tp->t_flags & TF_SACK_PERMIT) && 6650 (to.to_flags & TOF_SACKPERM) == 0) 6651 tp->t_flags &= ~TF_SACK_PERMIT; 6652 if (IS_FASTOPEN(tp->t_flags)) { 6653 if (to.to_flags & TOF_FASTOPEN) { 6654 uint16_t mss; 6655 6656 if (to.to_flags & TOF_MSS) 6657 mss = to.to_mss; 6658 else 6659 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 6660 mss = TCP6_MSS; 6661 else 6662 mss = TCP_MSS; 6663 tcp_fastopen_update_cache(tp, mss, 6664 to.to_tfo_len, to.to_tfo_cookie); 6665 } else 6666 tcp_fastopen_disable_path(tp); 6667 } 6668 } 6669 /* 6670 * At this point we are at the initial call. Here we decide 6671 * if we are doing RACK or not. We do this by seeing if 6672 * TF_SACK_PERMIT is set, if not rack is *not* possible and 6673 * we switch to the default code. 6674 */ 6675 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 6676 tcp_switch_back_to_default(tp); 6677 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen, 6678 tlen, iptos); 6679 return; 6680 } 6681 /* Set the flag */ 6682 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 6683 tcp_set_hpts(tp->t_inpcb); 6684 rack_stop_all_timers(tp); 6685 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack); 6686 } 6687 /* 6688 * This is the one exception case where we set the rack state 6689 * always. All other times (timers etc) we must have a rack-state 6690 * set (so we assure we have done the checks above for SACK). 6691 */ 6692 if (rack->r_state != tp->t_state) 6693 rack_set_state(tp, rack); 6694 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&rack->r_ctl.rc_map)) != NULL) 6695 kern_prefetch(rsm, &prev_state); 6696 prev_state = rack->r_state; 6697 rack->r_ctl.rc_tlp_send_cnt = 0; 6698 rack_clear_rate_sample(rack); 6699 retval = (*rack->r_substate) (m, th, so, 6700 tp, &to, drop_hdrlen, 6701 tlen, tiwin, thflags, nxt_pkt); 6702 #ifdef INVARIANTS 6703 if ((retval == 0) && 6704 (tp->t_inpcb == NULL)) { 6705 panic("retval:%d tp:%p t_inpcb:NULL state:%d", 6706 retval, tp, prev_state); 6707 } 6708 #endif 6709 if (retval == 0) { 6710 /* 6711 * If retval is 1 the tcb is unlocked and most likely the tp 6712 * is gone. 6713 */ 6714 INP_WLOCK_ASSERT(tp->t_inpcb); 6715 tcp_rack_xmit_timer_commit(rack, tp); 6716 if (((tp->snd_max - tp->snd_una) > tp->snd_wnd) && 6717 (rack->rc_in_persist == 0)){ 6718 /* 6719 * The peer shrunk its window on us to the point 6720 * where we have sent too much. The only thing 6721 * we can do here is stop any timers and 6722 * enter persist. We most likely lost the last 6723 * bytes we sent but oh well, we will have to 6724 * retransmit them after the peer is caught up. 6725 */ 6726 if (rack->rc_inp->inp_in_hpts) 6727 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 6728 rack_timer_cancel(tp, rack, cts, __LINE__); 6729 rack_enter_persist(tp, rack, cts); 6730 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6731 way_out = 3; 6732 goto done_with_input; 6733 } 6734 if (nxt_pkt == 0) { 6735 if (rack->r_wanted_output != 0) { 6736 did_out = 1; 6737 (void)tp->t_fb->tfb_tcp_output(tp); 6738 } 6739 rack_start_hpts_timer(rack, tp, cts, __LINE__, 0, 0, 0); 6740 } 6741 if (((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 6742 (SEQ_GT(tp->snd_max, tp->snd_una) || 6743 (tp->t_flags & TF_DELACK) || 6744 ((tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 6745 (tp->t_state <= TCPS_CLOSING)))) { 6746 /* We could not send (probably in the hpts but stopped the timer earlier)? */ 6747 if ((tp->snd_max == tp->snd_una) && 6748 ((tp->t_flags & TF_DELACK) == 0) && 6749 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 6750 /* keep alive not needed if we are hptsi output yet */ 6751 ; 6752 } else { 6753 if (rack->rc_inp->inp_in_hpts) 6754 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 6755 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6756 } 6757 way_out = 1; 6758 } else { 6759 /* Do we have the correct timer running? */ 6760 rack_timer_audit(tp, rack, &so->so_snd); 6761 way_out = 2; 6762 } 6763 done_with_input: 6764 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out); 6765 if (did_out) 6766 rack->r_wanted_output = 0; 6767 #ifdef INVARIANTS 6768 if (tp->t_inpcb == NULL) { 6769 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d", 6770 did_out, 6771 retval, tp, prev_state); 6772 } 6773 #endif 6774 INP_WUNLOCK(tp->t_inpcb); 6775 } 6776 } 6777 6778 void 6779 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 6780 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 6781 { 6782 struct timeval tv; 6783 #ifdef RSS 6784 struct tcp_function_block *tfb; 6785 struct tcp_rack *rack; 6786 struct epoch_tracker et; 6787 6788 rack = (struct tcp_rack *)tp->t_fb_ptr; 6789 if (rack->r_state == 0) { 6790 /* 6791 * Initial input (ACK to SYN-ACK etc)lets go ahead and get 6792 * it processed 6793 */ 6794 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 6795 tcp_get_usecs(&tv); 6796 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen, 6797 tlen, iptos, 0, &tv); 6798 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 6799 return; 6800 } 6801 tcp_queue_to_input(tp, m, th, tlen, drop_hdrlen, iptos); 6802 INP_WUNLOCK(tp->t_inpcb); 6803 #else 6804 tcp_get_usecs(&tv); 6805 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen, 6806 tlen, iptos, 0, &tv); 6807 #endif 6808 } 6809 6810 struct rack_sendmap * 6811 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused) 6812 { 6813 struct rack_sendmap *rsm = NULL; 6814 int32_t idx; 6815 uint32_t srtt_cur, srtt = 0, thresh = 0, ts_low = 0; 6816 6817 /* Return the next guy to be re-transmitted */ 6818 if (TAILQ_EMPTY(&rack->r_ctl.rc_map)) { 6819 return (NULL); 6820 } 6821 if (tp->t_flags & TF_SENTFIN) { 6822 /* retran the end FIN? */ 6823 return (NULL); 6824 } 6825 /* ok lets look at this one */ 6826 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6827 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) { 6828 goto check_it; 6829 } 6830 rsm = rack_find_lowest_rsm(rack); 6831 if (rsm == NULL) { 6832 return (NULL); 6833 } 6834 check_it: 6835 srtt_cur = tp->t_srtt >> TCP_RTT_SHIFT; 6836 srtt = TICKS_2_MSEC(srtt_cur); 6837 if (rack->rc_rack_rtt && (srtt > rack->rc_rack_rtt)) 6838 srtt = rack->rc_rack_rtt; 6839 if (rsm->r_flags & RACK_ACKED) { 6840 return (NULL); 6841 } 6842 if ((rsm->r_flags & RACK_SACK_PASSED) == 0) { 6843 /* Its not yet ready */ 6844 return (NULL); 6845 } 6846 idx = rsm->r_rtr_cnt - 1; 6847 ts_low = rsm->r_tim_lastsent[idx]; 6848 thresh = rack_calc_thresh_rack(rack, srtt, tsused); 6849 if (tsused <= ts_low) { 6850 return (NULL); 6851 } 6852 if ((tsused - ts_low) >= thresh) { 6853 return (rsm); 6854 } 6855 return (NULL); 6856 } 6857 6858 static int 6859 rack_output(struct tcpcb *tp) 6860 { 6861 struct socket *so; 6862 uint32_t recwin, sendwin; 6863 uint32_t sb_offset; 6864 int32_t len, flags, error = 0; 6865 struct mbuf *m; 6866 struct mbuf *mb; 6867 uint32_t if_hw_tsomaxsegcount = 0; 6868 uint32_t if_hw_tsomaxsegsize; 6869 long tot_len_this_send = 0; 6870 struct ip *ip = NULL; 6871 #ifdef TCPDEBUG 6872 struct ipovly *ipov = NULL; 6873 #endif 6874 struct udphdr *udp = NULL; 6875 struct tcp_rack *rack; 6876 struct tcphdr *th; 6877 uint8_t pass = 0; 6878 uint8_t wanted_cookie = 0; 6879 u_char opt[TCP_MAXOLEN]; 6880 unsigned ipoptlen, optlen, hdrlen, ulen=0; 6881 uint32_t rack_seq; 6882 6883 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 6884 unsigned ipsec_optlen = 0; 6885 6886 #endif 6887 int32_t idle, sendalot; 6888 int32_t sub_from_prr = 0; 6889 volatile int32_t sack_rxmit; 6890 struct rack_sendmap *rsm = NULL; 6891 int32_t tso, mtu, would_have_fin = 0; 6892 struct tcpopt to; 6893 int32_t slot = 0; 6894 uint32_t cts; 6895 uint8_t hpts_calling, doing_tlp = 0; 6896 int32_t do_a_prefetch; 6897 int32_t prefetch_rsm = 0; 6898 int32_t prefetch_so_done = 0; 6899 struct tcp_log_buffer *lgb = NULL; 6900 struct inpcb *inp; 6901 struct sockbuf *sb; 6902 #ifdef INET6 6903 struct ip6_hdr *ip6 = NULL; 6904 int32_t isipv6; 6905 #endif 6906 /* setup and take the cache hits here */ 6907 rack = (struct tcp_rack *)tp->t_fb_ptr; 6908 inp = rack->rc_inp; 6909 so = inp->inp_socket; 6910 sb = &so->so_snd; 6911 kern_prefetch(sb, &do_a_prefetch); 6912 do_a_prefetch = 1; 6913 6914 INP_WLOCK_ASSERT(inp); 6915 #ifdef TCP_OFFLOAD 6916 if (tp->t_flags & TF_TOE) 6917 return (tcp_offload_output(tp)); 6918 #endif 6919 #ifdef INET6 6920 if (rack->r_state) { 6921 /* Use the cache line loaded if possible */ 6922 isipv6 = rack->r_is_v6; 6923 } else { 6924 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 6925 } 6926 #endif 6927 cts = tcp_ts_getticks(); 6928 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 6929 inp->inp_in_hpts) { 6930 /* 6931 * We are on the hpts for some timer but not hptsi output. 6932 * Remove from the hpts unconditionally. 6933 */ 6934 rack_timer_cancel(tp, rack, cts, __LINE__); 6935 } 6936 /* Mark that we have called rack_output(). */ 6937 if ((rack->r_timer_override) || 6938 (tp->t_flags & TF_FORCEDATA) || 6939 (tp->t_state < TCPS_ESTABLISHED)) { 6940 if (tp->t_inpcb->inp_in_hpts) 6941 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT); 6942 } else if (tp->t_inpcb->inp_in_hpts) { 6943 /* 6944 * On the hpts you can't pass even if ACKNOW is on, we will 6945 * when the hpts fires. 6946 */ 6947 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1); 6948 return (0); 6949 } 6950 hpts_calling = inp->inp_hpts_calls; 6951 inp->inp_hpts_calls = 0; 6952 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 6953 if (rack_process_timers(tp, rack, cts, hpts_calling)) { 6954 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1); 6955 return (0); 6956 } 6957 } 6958 rack->r_wanted_output = 0; 6959 rack->r_timer_override = 0; 6960 /* 6961 * For TFO connections in SYN_SENT or SYN_RECEIVED, 6962 * only allow the initial SYN or SYN|ACK and those sent 6963 * by the retransmit timer. 6964 */ 6965 if (IS_FASTOPEN(tp->t_flags) && 6966 ((tp->t_state == TCPS_SYN_RECEIVED) || 6967 (tp->t_state == TCPS_SYN_SENT)) && 6968 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 6969 (tp->t_rxtshift == 0)) /* not a retransmit */ 6970 return (0); 6971 /* 6972 * Determine length of data that should be transmitted, and flags 6973 * that will be used. If there is some data or critical controls 6974 * (SYN, RST) to send, then transmit; otherwise, investigate 6975 * further. 6976 */ 6977 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 6978 #ifdef NETFLIX_CWV 6979 if (tp->cwv_enabled) { 6980 if ((tp->cwv_cwnd_valid == 0) && 6981 TCPS_HAVEESTABLISHED(tp->t_state) && 6982 (tp->snd_cwnd > tp->snd_cwv.init_cwnd)) 6983 tcp_newcwv_nvp_closedown(tp); 6984 } else 6985 #endif 6986 if (tp->t_idle_reduce) { 6987 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) 6988 rack_cc_after_idle(tp, 6989 (rack->r_idle_reduce_largest ? 1 :0)); 6990 } 6991 tp->t_flags &= ~TF_LASTIDLE; 6992 if (idle) { 6993 if (tp->t_flags & TF_MORETOCOME) { 6994 tp->t_flags |= TF_LASTIDLE; 6995 idle = 0; 6996 } 6997 } 6998 again: 6999 /* 7000 * If we've recently taken a timeout, snd_max will be greater than 7001 * snd_nxt. There may be SACK information that allows us to avoid 7002 * resending already delivered data. Adjust snd_nxt accordingly. 7003 */ 7004 sendalot = 0; 7005 cts = tcp_ts_getticks(); 7006 tso = 0; 7007 mtu = 0; 7008 sb_offset = tp->snd_max - tp->snd_una; 7009 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 7010 7011 flags = tcp_outflags[tp->t_state]; 7012 /* 7013 * Send any SACK-generated retransmissions. If we're explicitly 7014 * trying to send out new data (when sendalot is 1), bypass this 7015 * function. If we retransmit in fast recovery mode, decrement 7016 * snd_cwnd, since we're replacing a (future) new transmission with 7017 * a retransmission now, and we previously incremented snd_cwnd in 7018 * tcp_input(). 7019 */ 7020 /* 7021 * Still in sack recovery , reset rxmit flag to zero. 7022 */ 7023 while (rack->rc_free_cnt < rack_free_cache) { 7024 rsm = rack_alloc(rack); 7025 if (rsm == NULL) { 7026 if (inp->inp_hpts_calls) 7027 /* Retry in a ms */ 7028 slot = 1; 7029 goto just_return_nolock; 7030 } 7031 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_next); 7032 rack->rc_free_cnt++; 7033 rsm = NULL; 7034 } 7035 if (inp->inp_hpts_calls) 7036 inp->inp_hpts_calls = 0; 7037 sack_rxmit = 0; 7038 len = 0; 7039 rsm = NULL; 7040 if (flags & TH_RST) { 7041 SOCKBUF_LOCK(sb); 7042 goto send; 7043 } 7044 if (rack->r_ctl.rc_tlpsend) { 7045 /* Tail loss probe */ 7046 long cwin; 7047 long tlen; 7048 7049 doing_tlp = 1; 7050 rsm = rack->r_ctl.rc_tlpsend; 7051 rack->r_ctl.rc_tlpsend = NULL; 7052 sack_rxmit = 1; 7053 tlen = rsm->r_end - rsm->r_start; 7054 if (tlen > tp->t_maxseg) 7055 tlen = tp->t_maxseg; 7056 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 7057 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 7058 __func__, __LINE__, 7059 rsm->r_start, tp->snd_una, tp, rack, rsm)); 7060 sb_offset = rsm->r_start - tp->snd_una; 7061 cwin = min(tp->snd_wnd, tlen); 7062 len = cwin; 7063 } else if (rack->r_ctl.rc_resend) { 7064 /* Retransmit timer */ 7065 rsm = rack->r_ctl.rc_resend; 7066 rack->r_ctl.rc_resend = NULL; 7067 len = rsm->r_end - rsm->r_start; 7068 sack_rxmit = 1; 7069 sendalot = 0; 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", 7072 __func__, __LINE__, 7073 rsm->r_start, tp->snd_una, tp, rack, rsm)); 7074 sb_offset = rsm->r_start - tp->snd_una; 7075 if (len >= tp->t_maxseg) { 7076 len = tp->t_maxseg; 7077 } 7078 } else if ((rack->rc_in_persist == 0) && 7079 ((rsm = tcp_rack_output(tp, rack, cts)) != NULL)) { 7080 long tlen; 7081 7082 if ((!IN_RECOVERY(tp->t_flags)) && 7083 ((tp->t_flags & (TF_WASFRECOVERY | TF_WASCRECOVERY)) == 0)) { 7084 /* Enter recovery if not induced by a time-out */ 7085 rack->r_ctl.rc_rsm_start = rsm->r_start; 7086 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 7087 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 7088 rack_cong_signal(tp, NULL, CC_NDUPACK); 7089 /* 7090 * When we enter recovery we need to assure we send 7091 * one packet. 7092 */ 7093 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 7094 } 7095 #ifdef INVARIANTS 7096 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 7097 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n", 7098 tp, rack, rsm, rsm->r_start, tp->snd_una); 7099 } 7100 #endif 7101 tlen = rsm->r_end - rsm->r_start; 7102 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 7103 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 7104 __func__, __LINE__, 7105 rsm->r_start, tp->snd_una, tp, rack, rsm)); 7106 sb_offset = rsm->r_start - tp->snd_una; 7107 if (tlen > rack->r_ctl.rc_prr_sndcnt) { 7108 len = rack->r_ctl.rc_prr_sndcnt; 7109 } else { 7110 len = tlen; 7111 } 7112 if (len >= tp->t_maxseg) { 7113 sendalot = 1; 7114 len = tp->t_maxseg; 7115 } else { 7116 sendalot = 0; 7117 if ((rack->rc_timer_up == 0) && 7118 (len < tlen)) { 7119 /* 7120 * If its not a timer don't send a partial 7121 * segment. 7122 */ 7123 len = 0; 7124 goto just_return_nolock; 7125 } 7126 } 7127 if (len > 0) { 7128 sub_from_prr = 1; 7129 sack_rxmit = 1; 7130 TCPSTAT_INC(tcps_sack_rexmits); 7131 TCPSTAT_ADD(tcps_sack_rexmit_bytes, 7132 min(len, tp->t_maxseg)); 7133 counter_u64_add(rack_rtm_prr_retran, 1); 7134 } 7135 } 7136 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) { 7137 /* we are retransmitting the fin */ 7138 len--; 7139 if (len) { 7140 /* 7141 * When retransmitting data do *not* include the 7142 * FIN. This could happen from a TLP probe. 7143 */ 7144 flags &= ~TH_FIN; 7145 } 7146 } 7147 #ifdef INVARIANTS 7148 /* For debugging */ 7149 rack->r_ctl.rc_rsm_at_retran = rsm; 7150 #endif 7151 /* 7152 * Get standard flags, and add SYN or FIN if requested by 'hidden' 7153 * state flags. 7154 */ 7155 if (tp->t_flags & TF_NEEDFIN) 7156 flags |= TH_FIN; 7157 if (tp->t_flags & TF_NEEDSYN) 7158 flags |= TH_SYN; 7159 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 7160 void *end_rsm; 7161 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 7162 if (end_rsm) 7163 kern_prefetch(end_rsm, &prefetch_rsm); 7164 prefetch_rsm = 1; 7165 } 7166 SOCKBUF_LOCK(sb); 7167 /* 7168 * If in persist timeout with window of 0, send 1 byte. Otherwise, 7169 * if window is small but nonzero and time TF_SENTFIN expired, we 7170 * will send what we can and go to transmit state. 7171 */ 7172 if (tp->t_flags & TF_FORCEDATA) { 7173 if (sendwin == 0) { 7174 /* 7175 * If we still have some data to send, then clear 7176 * the FIN bit. Usually this would happen below 7177 * when it realizes that we aren't sending all the 7178 * data. However, if we have exactly 1 byte of 7179 * unsent data, then it won't clear the FIN bit 7180 * below, and if we are in persist state, we wind up 7181 * sending the packet without recording that we sent 7182 * the FIN bit. 7183 * 7184 * We can't just blindly clear the FIN bit, because 7185 * if we don't have any more data to send then the 7186 * probe will be the FIN itself. 7187 */ 7188 if (sb_offset < sbused(sb)) 7189 flags &= ~TH_FIN; 7190 sendwin = 1; 7191 } else { 7192 if (rack->rc_in_persist) 7193 rack_exit_persist(tp, rack); 7194 /* 7195 * If we are dropping persist mode then we need to 7196 * correct snd_nxt/snd_max and off. 7197 */ 7198 tp->snd_nxt = tp->snd_max; 7199 sb_offset = tp->snd_nxt - tp->snd_una; 7200 } 7201 } 7202 /* 7203 * If snd_nxt == snd_max and we have transmitted a FIN, the 7204 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 7205 * negative length. This can also occur when TCP opens up its 7206 * congestion window while receiving additional duplicate acks after 7207 * fast-retransmit because TCP will reset snd_nxt to snd_max after 7208 * the fast-retransmit. 7209 * 7210 * In the normal retransmit-FIN-only case, however, snd_nxt will be 7211 * set to snd_una, the sb_offset will be 0, and the length may wind 7212 * up 0. 7213 * 7214 * If sack_rxmit is true we are retransmitting from the scoreboard 7215 * in which case len is already set. 7216 */ 7217 if (sack_rxmit == 0) { 7218 uint32_t avail; 7219 7220 avail = sbavail(sb); 7221 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail) 7222 sb_offset = tp->snd_nxt - tp->snd_una; 7223 else 7224 sb_offset = 0; 7225 if (IN_RECOVERY(tp->t_flags) == 0) { 7226 if (rack->r_ctl.rc_tlp_new_data) { 7227 /* TLP is forcing out new data */ 7228 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) { 7229 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset); 7230 } 7231 if (rack->r_ctl.rc_tlp_new_data > tp->snd_wnd) 7232 len = tp->snd_wnd; 7233 else 7234 len = rack->r_ctl.rc_tlp_new_data; 7235 rack->r_ctl.rc_tlp_new_data = 0; 7236 doing_tlp = 1; 7237 } else { 7238 if (sendwin > avail) { 7239 /* use the available */ 7240 if (avail > sb_offset) { 7241 len = (int32_t)(avail - sb_offset); 7242 } else { 7243 len = 0; 7244 } 7245 } else { 7246 if (sendwin > sb_offset) { 7247 len = (int32_t)(sendwin - sb_offset); 7248 } else { 7249 len = 0; 7250 } 7251 } 7252 } 7253 } else { 7254 uint32_t outstanding; 7255 7256 /* 7257 * We are inside of a SACK recovery episode and are 7258 * sending new data, having retransmitted all the 7259 * data possible so far in the scoreboard. 7260 */ 7261 outstanding = tp->snd_max - tp->snd_una; 7262 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) 7263 len = 0; 7264 else if (avail > sb_offset) 7265 len = avail - sb_offset; 7266 else 7267 len = 0; 7268 if (len > 0) { 7269 if (len > rack->r_ctl.rc_prr_sndcnt) 7270 len = rack->r_ctl.rc_prr_sndcnt; 7271 7272 if (len > 0) { 7273 sub_from_prr = 1; 7274 counter_u64_add(rack_rtm_prr_newdata, 1); 7275 } 7276 } 7277 if (len > tp->t_maxseg) { 7278 /* 7279 * We should never send more than a MSS when 7280 * retransmitting or sending new data in prr 7281 * mode unless the override flag is on. Most 7282 * likely the PRR algorithm is not going to 7283 * let us send a lot as well :-) 7284 */ 7285 if (rack->r_ctl.rc_prr_sendalot == 0) 7286 len = tp->t_maxseg; 7287 } else if (len < tp->t_maxseg) { 7288 /* 7289 * Do we send any? The idea here is if the 7290 * send empty's the socket buffer we want to 7291 * do it. However if not then lets just wait 7292 * for our prr_sndcnt to get bigger. 7293 */ 7294 long leftinsb; 7295 7296 leftinsb = sbavail(sb) - sb_offset; 7297 if (leftinsb > len) { 7298 /* This send does not empty the sb */ 7299 len = 0; 7300 } 7301 } 7302 } 7303 } 7304 if (prefetch_so_done == 0) { 7305 kern_prefetch(so, &prefetch_so_done); 7306 prefetch_so_done = 1; 7307 } 7308 /* 7309 * Lop off SYN bit if it has already been sent. However, if this is 7310 * SYN-SENT state and if segment contains data and if we don't know 7311 * that foreign host supports TAO, suppress sending segment. 7312 */ 7313 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) && 7314 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) { 7315 if (tp->t_state != TCPS_SYN_RECEIVED) 7316 flags &= ~TH_SYN; 7317 /* 7318 * When sending additional segments following a TFO SYN|ACK, 7319 * do not include the SYN bit. 7320 */ 7321 if (IS_FASTOPEN(tp->t_flags) && 7322 (tp->t_state == TCPS_SYN_RECEIVED)) 7323 flags &= ~TH_SYN; 7324 sb_offset--, len++; 7325 } 7326 /* 7327 * Be careful not to send data and/or FIN on SYN segments. This 7328 * measure is needed to prevent interoperability problems with not 7329 * fully conformant TCP implementations. 7330 */ 7331 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 7332 len = 0; 7333 flags &= ~TH_FIN; 7334 } 7335 /* 7336 * On TFO sockets, ensure no data is sent in the following cases: 7337 * 7338 * - When retransmitting SYN|ACK on a passively-created socket 7339 * 7340 * - When retransmitting SYN on an actively created socket 7341 * 7342 * - When sending a zero-length cookie (cookie request) on an 7343 * actively created socket 7344 * 7345 * - When the socket is in the CLOSED state (RST is being sent) 7346 */ 7347 if (IS_FASTOPEN(tp->t_flags) && 7348 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 7349 ((tp->t_state == TCPS_SYN_SENT) && 7350 (tp->t_tfo_client_cookie_len == 0)) || 7351 (flags & TH_RST))) { 7352 sack_rxmit = 0; 7353 len = 0; 7354 } 7355 /* Without fast-open there should never be data sent on a SYN */ 7356 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) 7357 len = 0; 7358 if (len <= 0) { 7359 /* 7360 * If FIN has been sent but not acked, but we haven't been 7361 * called to retransmit, len will be < 0. Otherwise, window 7362 * shrank after we sent into it. If window shrank to 0, 7363 * cancel pending retransmit, pull snd_nxt back to (closed) 7364 * window, and set the persist timer if it isn't already 7365 * going. If the window didn't close completely, just wait 7366 * for an ACK. 7367 * 7368 * We also do a general check here to ensure that we will 7369 * set the persist timer when we have data to send, but a 7370 * 0-byte window. This makes sure the persist timer is set 7371 * even if the packet hits one of the "goto send" lines 7372 * below. 7373 */ 7374 len = 0; 7375 if ((tp->snd_wnd == 0) && 7376 (TCPS_HAVEESTABLISHED(tp->t_state)) && 7377 (sb_offset < (int)sbavail(sb))) { 7378 tp->snd_nxt = tp->snd_una; 7379 rack_enter_persist(tp, rack, cts); 7380 } 7381 } 7382 /* len will be >= 0 after this point. */ 7383 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 7384 tcp_sndbuf_autoscale(tp, so, sendwin); 7385 /* 7386 * Decide if we can use TCP Segmentation Offloading (if supported by 7387 * hardware). 7388 * 7389 * TSO may only be used if we are in a pure bulk sending state. The 7390 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 7391 * options prevent using TSO. With TSO the TCP header is the same 7392 * (except for the sequence number) for all generated packets. This 7393 * makes it impossible to transmit any options which vary per 7394 * generated segment or packet. 7395 * 7396 * IPv4 handling has a clear separation of ip options and ip header 7397 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 7398 * the right thing below to provide length of just ip options and thus 7399 * checking for ipoptlen is enough to decide if ip options are present. 7400 */ 7401 7402 #ifdef INET6 7403 if (isipv6) 7404 ipoptlen = ip6_optlen(tp->t_inpcb); 7405 else 7406 #endif 7407 if (tp->t_inpcb->inp_options) 7408 ipoptlen = tp->t_inpcb->inp_options->m_len - 7409 offsetof(struct ipoption, ipopt_list); 7410 else 7411 ipoptlen = 0; 7412 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7413 /* 7414 * Pre-calculate here as we save another lookup into the darknesses 7415 * of IPsec that way and can actually decide if TSO is ok. 7416 */ 7417 #ifdef INET6 7418 if (isipv6 && IPSEC_ENABLED(ipv6)) 7419 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 7420 #ifdef INET 7421 else 7422 #endif 7423 #endif /* INET6 */ 7424 #ifdef INET 7425 if (IPSEC_ENABLED(ipv4)) 7426 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 7427 #endif /* INET */ 7428 #endif 7429 7430 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7431 ipoptlen += ipsec_optlen; 7432 #endif 7433 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 7434 (tp->t_port == 0) && 7435 ((tp->t_flags & TF_SIGNATURE) == 0) && 7436 tp->rcv_numsacks == 0 && sack_rxmit == 0 && 7437 ipoptlen == 0) 7438 tso = 1; 7439 { 7440 uint32_t outstanding; 7441 7442 outstanding = tp->snd_max - tp->snd_una; 7443 if (tp->t_flags & TF_SENTFIN) { 7444 /* 7445 * If we sent a fin, snd_max is 1 higher than 7446 * snd_una 7447 */ 7448 outstanding--; 7449 } 7450 if (outstanding > 0) { 7451 /* 7452 * This is sub-optimal. We only send a stand alone 7453 * FIN on its own segment. 7454 */ 7455 if (flags & TH_FIN) { 7456 flags &= ~TH_FIN; 7457 would_have_fin = 1; 7458 } 7459 } else if (sack_rxmit) { 7460 if ((rsm->r_flags & RACK_HAS_FIN) == 0) 7461 flags &= ~TH_FIN; 7462 } else { 7463 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 7464 sbused(sb))) 7465 flags &= ~TH_FIN; 7466 } 7467 } 7468 recwin = sbspace(&so->so_rcv); 7469 7470 /* 7471 * Sender silly window avoidance. We transmit under the following 7472 * conditions when len is non-zero: 7473 * 7474 * - We have a full segment (or more with TSO) - This is the last 7475 * buffer in a write()/send() and we are either idle or running 7476 * NODELAY - we've timed out (e.g. persist timer) - we have more 7477 * then 1/2 the maximum send window's worth of data (receiver may be 7478 * limited the window size) - we need to retransmit 7479 */ 7480 if (len) { 7481 if (len >= tp->t_maxseg) { 7482 pass = 1; 7483 goto send; 7484 } 7485 /* 7486 * NOTE! on localhost connections an 'ack' from the remote 7487 * end may occur synchronously with the output and cause us 7488 * to flush a buffer queued with moretocome. XXX 7489 * 7490 */ 7491 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 7492 (idle || (tp->t_flags & TF_NODELAY)) && 7493 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(&so->so_snd)) && 7494 (tp->t_flags & TF_NOPUSH) == 0) { 7495 pass = 2; 7496 goto send; 7497 } 7498 if (tp->t_flags & TF_FORCEDATA) { /* typ. timeout case */ 7499 pass = 3; 7500 goto send; 7501 } 7502 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 7503 goto send; 7504 } 7505 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 7506 pass = 4; 7507 goto send; 7508 } 7509 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */ 7510 pass = 5; 7511 goto send; 7512 } 7513 if (sack_rxmit) { 7514 pass = 6; 7515 goto send; 7516 } 7517 } 7518 /* 7519 * Sending of standalone window updates. 7520 * 7521 * Window updates are important when we close our window due to a 7522 * full socket buffer and are opening it again after the application 7523 * reads data from it. Once the window has opened again and the 7524 * remote end starts to send again the ACK clock takes over and 7525 * provides the most current window information. 7526 * 7527 * We must avoid the silly window syndrome whereas every read from 7528 * the receive buffer, no matter how small, causes a window update 7529 * to be sent. We also should avoid sending a flurry of window 7530 * updates when the socket buffer had queued a lot of data and the 7531 * application is doing small reads. 7532 * 7533 * Prevent a flurry of pointless window updates by only sending an 7534 * update when we can increase the advertized window by more than 7535 * 1/4th of the socket buffer capacity. When the buffer is getting 7536 * full or is very small be more aggressive and send an update 7537 * whenever we can increase by two mss sized segments. In all other 7538 * situations the ACK's to new incoming data will carry further 7539 * window increases. 7540 * 7541 * Don't send an independent window update if a delayed ACK is 7542 * pending (it will get piggy-backed on it) or the remote side 7543 * already has done a half-close and won't send more data. Skip 7544 * this if the connection is in T/TCP half-open state. 7545 */ 7546 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 7547 !(tp->t_flags & TF_DELACK) && 7548 !TCPS_HAVERCVDFIN(tp->t_state)) { 7549 /* 7550 * "adv" is the amount we could increase the window, taking 7551 * into account that we are limited by TCP_MAXWIN << 7552 * tp->rcv_scale. 7553 */ 7554 int32_t adv; 7555 int oldwin; 7556 7557 adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale); 7558 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 7559 oldwin = (tp->rcv_adv - tp->rcv_nxt); 7560 adv -= oldwin; 7561 } else 7562 oldwin = 0; 7563 7564 /* 7565 * If the new window size ends up being the same as the old 7566 * size when it is scaled, then don't force a window update. 7567 */ 7568 if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale) 7569 goto dontupdate; 7570 7571 if (adv >= (int32_t)(2 * tp->t_maxseg) && 7572 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 7573 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) || 7574 so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg)) { 7575 pass = 7; 7576 goto send; 7577 } 7578 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 7579 goto send; 7580 } 7581 dontupdate: 7582 7583 /* 7584 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 7585 * is also a catch-all for the retransmit timer timeout case. 7586 */ 7587 if (tp->t_flags & TF_ACKNOW) { 7588 pass = 8; 7589 goto send; 7590 } 7591 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) { 7592 pass = 9; 7593 goto send; 7594 } 7595 if (SEQ_GT(tp->snd_up, tp->snd_una)) { 7596 pass = 10; 7597 goto send; 7598 } 7599 /* 7600 * If our state indicates that FIN should be sent and we have not 7601 * yet done so, then we need to send. 7602 */ 7603 if ((flags & TH_FIN) && 7604 (tp->snd_nxt == tp->snd_una)) { 7605 pass = 11; 7606 goto send; 7607 } 7608 /* 7609 * No reason to send a segment, just return. 7610 */ 7611 just_return: 7612 SOCKBUF_UNLOCK(sb); 7613 just_return_nolock: 7614 if (tot_len_this_send == 0) 7615 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1); 7616 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1); 7617 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling); 7618 tp->t_flags &= ~TF_FORCEDATA; 7619 return (0); 7620 7621 send: 7622 if (doing_tlp == 0) { 7623 /* 7624 * Data not a TLP, and its not the rxt firing. If it is the 7625 * rxt firing, we want to leave the tlp_in_progress flag on 7626 * so we don't send another TLP. It has to be a rack timer 7627 * or normal send (response to acked data) to clear the tlp 7628 * in progress flag. 7629 */ 7630 rack->rc_tlp_in_progress = 0; 7631 } 7632 SOCKBUF_LOCK_ASSERT(sb); 7633 if (len > 0) { 7634 if (len >= tp->t_maxseg) 7635 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 7636 else 7637 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 7638 } 7639 /* 7640 * Before ESTABLISHED, force sending of initial options unless TCP 7641 * set not to do any options. NOTE: we assume that the IP/TCP header 7642 * plus TCP options always fit in a single mbuf, leaving room for a 7643 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 7644 * + optlen <= MCLBYTES 7645 */ 7646 optlen = 0; 7647 #ifdef INET6 7648 if (isipv6) 7649 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 7650 else 7651 #endif 7652 hdrlen = sizeof(struct tcpiphdr); 7653 7654 /* 7655 * Compute options for segment. We only have to care about SYN and 7656 * established connection segments. Options for SYN-ACK segments 7657 * are handled in TCP syncache. 7658 */ 7659 to.to_flags = 0; 7660 if ((tp->t_flags & TF_NOOPT) == 0) { 7661 /* Maximum segment size. */ 7662 if (flags & TH_SYN) { 7663 tp->snd_nxt = tp->iss; 7664 to.to_mss = tcp_mssopt(&inp->inp_inc); 7665 #ifdef NETFLIX_TCPOUDP 7666 if (tp->t_port) 7667 to.to_mss -= V_tcp_udp_tunneling_overhead; 7668 #endif 7669 to.to_flags |= TOF_MSS; 7670 7671 /* 7672 * On SYN or SYN|ACK transmits on TFO connections, 7673 * only include the TFO option if it is not a 7674 * retransmit, as the presence of the TFO option may 7675 * have caused the original SYN or SYN|ACK to have 7676 * been dropped by a middlebox. 7677 */ 7678 if (IS_FASTOPEN(tp->t_flags) && 7679 (tp->t_rxtshift == 0)) { 7680 if (tp->t_state == TCPS_SYN_RECEIVED) { 7681 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 7682 to.to_tfo_cookie = 7683 (u_int8_t *)&tp->t_tfo_cookie.server; 7684 to.to_flags |= TOF_FASTOPEN; 7685 wanted_cookie = 1; 7686 } else if (tp->t_state == TCPS_SYN_SENT) { 7687 to.to_tfo_len = 7688 tp->t_tfo_client_cookie_len; 7689 to.to_tfo_cookie = 7690 tp->t_tfo_cookie.client; 7691 to.to_flags |= TOF_FASTOPEN; 7692 wanted_cookie = 1; 7693 /* 7694 * If we wind up having more data to 7695 * send with the SYN than can fit in 7696 * one segment, don't send any more 7697 * until the SYN|ACK comes back from 7698 * the other end. 7699 */ 7700 sendalot = 0; 7701 } 7702 } 7703 } 7704 /* Window scaling. */ 7705 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 7706 to.to_wscale = tp->request_r_scale; 7707 to.to_flags |= TOF_SCALE; 7708 } 7709 /* Timestamps. */ 7710 if ((tp->t_flags & TF_RCVD_TSTMP) || 7711 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 7712 to.to_tsval = cts + tp->ts_offset; 7713 to.to_tsecr = tp->ts_recent; 7714 to.to_flags |= TOF_TS; 7715 } 7716 /* Set receive buffer autosizing timestamp. */ 7717 if (tp->rfbuf_ts == 0 && 7718 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 7719 tp->rfbuf_ts = tcp_ts_getticks(); 7720 /* Selective ACK's. */ 7721 if (flags & TH_SYN) 7722 to.to_flags |= TOF_SACKPERM; 7723 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 7724 tp->rcv_numsacks > 0) { 7725 to.to_flags |= TOF_SACK; 7726 to.to_nsacks = tp->rcv_numsacks; 7727 to.to_sacks = (u_char *)tp->sackblks; 7728 } 7729 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 7730 /* TCP-MD5 (RFC2385). */ 7731 if (tp->t_flags & TF_SIGNATURE) 7732 to.to_flags |= TOF_SIGNATURE; 7733 #endif /* TCP_SIGNATURE */ 7734 7735 /* Processing the options. */ 7736 hdrlen += optlen = tcp_addoptions(&to, opt); 7737 /* 7738 * If we wanted a TFO option to be added, but it was unable 7739 * to fit, ensure no data is sent. 7740 */ 7741 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 7742 !(to.to_flags & TOF_FASTOPEN)) 7743 len = 0; 7744 } 7745 #ifdef NETFLIX_TCPOUDP 7746 if (tp->t_port) { 7747 if (V_tcp_udp_tunneling_port == 0) { 7748 /* The port was removed?? */ 7749 SOCKBUF_UNLOCK(&so->so_snd); 7750 return (EHOSTUNREACH); 7751 } 7752 hdrlen += sizeof(struct udphdr); 7753 } 7754 #endif 7755 ipoptlen = 0; 7756 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7757 ipoptlen += ipsec_optlen; 7758 #endif 7759 7760 /* 7761 * Adjust data length if insertion of options will bump the packet 7762 * length beyond the t_maxseg length. Clear the FIN bit because we 7763 * cut off the tail of the segment. 7764 */ 7765 if (len + optlen + ipoptlen > tp->t_maxseg) { 7766 if (flags & TH_FIN) { 7767 would_have_fin = 1; 7768 flags &= ~TH_FIN; 7769 } 7770 if (tso) { 7771 uint32_t if_hw_tsomax; 7772 uint32_t moff; 7773 int32_t max_len; 7774 7775 /* extract TSO information */ 7776 if_hw_tsomax = tp->t_tsomax; 7777 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 7778 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 7779 KASSERT(ipoptlen == 0, 7780 ("%s: TSO can't do IP options", __func__)); 7781 7782 /* 7783 * Check if we should limit by maximum payload 7784 * length: 7785 */ 7786 if (if_hw_tsomax != 0) { 7787 /* compute maximum TSO length */ 7788 max_len = (if_hw_tsomax - hdrlen - 7789 max_linkhdr); 7790 if (max_len <= 0) { 7791 len = 0; 7792 } else if (len > max_len) { 7793 sendalot = 1; 7794 len = max_len; 7795 } 7796 } 7797 /* 7798 * Prevent the last segment from being fractional 7799 * unless the send sockbuf can be emptied: 7800 */ 7801 max_len = (tp->t_maxseg - optlen); 7802 if ((sb_offset + len) < sbavail(sb)) { 7803 moff = len % (u_int)max_len; 7804 if (moff != 0) { 7805 len -= moff; 7806 sendalot = 1; 7807 } 7808 } 7809 /* 7810 * In case there are too many small fragments don't 7811 * use TSO: 7812 */ 7813 if (len <= max_len) { 7814 len = max_len; 7815 sendalot = 1; 7816 tso = 0; 7817 } 7818 /* 7819 * Send the FIN in a separate segment after the bulk 7820 * sending is done. We don't trust the TSO 7821 * implementations to clear the FIN flag on all but 7822 * the last segment. 7823 */ 7824 if (tp->t_flags & TF_NEEDFIN) 7825 sendalot = 1; 7826 7827 } else { 7828 len = tp->t_maxseg - optlen - ipoptlen; 7829 sendalot = 1; 7830 } 7831 } else 7832 tso = 0; 7833 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 7834 ("%s: len > IP_MAXPACKET", __func__)); 7835 #ifdef DIAGNOSTIC 7836 #ifdef INET6 7837 if (max_linkhdr + hdrlen > MCLBYTES) 7838 #else 7839 if (max_linkhdr + hdrlen > MHLEN) 7840 #endif 7841 panic("tcphdr too big"); 7842 #endif 7843 7844 /* 7845 * This KASSERT is here to catch edge cases at a well defined place. 7846 * Before, those had triggered (random) panic conditions further 7847 * down. 7848 */ 7849 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 7850 if ((len == 0) && 7851 (flags & TH_FIN) && 7852 (sbused(sb))) { 7853 /* 7854 * We have outstanding data, don't send a fin by itself!. 7855 */ 7856 goto just_return; 7857 } 7858 /* 7859 * Grab a header mbuf, attaching a copy of data to be transmitted, 7860 * and initialize the header from the template for sends on this 7861 * connection. 7862 */ 7863 if (len) { 7864 uint32_t max_val; 7865 uint32_t moff; 7866 7867 if (rack->rc_pace_max_segs) 7868 max_val = rack->rc_pace_max_segs * tp->t_maxseg; 7869 else 7870 max_val = len; 7871 /* 7872 * We allow a limit on sending with hptsi. 7873 */ 7874 if (len > max_val) { 7875 len = max_val; 7876 } 7877 #ifdef INET6 7878 if (MHLEN < hdrlen + max_linkhdr) 7879 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 7880 else 7881 #endif 7882 m = m_gethdr(M_NOWAIT, MT_DATA); 7883 7884 if (m == NULL) { 7885 SOCKBUF_UNLOCK(sb); 7886 error = ENOBUFS; 7887 sack_rxmit = 0; 7888 goto out; 7889 } 7890 m->m_data += max_linkhdr; 7891 m->m_len = hdrlen; 7892 7893 /* 7894 * Start the m_copy functions from the closest mbuf to the 7895 * sb_offset in the socket buffer chain. 7896 */ 7897 mb = sbsndptr_noadv(sb, sb_offset, &moff); 7898 if (len <= MHLEN - hdrlen - max_linkhdr) { 7899 m_copydata(mb, moff, (int)len, 7900 mtod(m, caddr_t)+hdrlen); 7901 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7902 sbsndptr_adv(sb, mb, len); 7903 m->m_len += len; 7904 } else { 7905 struct sockbuf *msb; 7906 7907 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7908 msb = NULL; 7909 else 7910 msb = sb; 7911 m->m_next = tcp_m_copym(mb, moff, &len, 7912 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb); 7913 if (len <= (tp->t_maxseg - optlen)) { 7914 /* 7915 * Must have ran out of mbufs for the copy 7916 * shorten it to no longer need tso. Lets 7917 * not put on sendalot since we are low on 7918 * mbufs. 7919 */ 7920 tso = 0; 7921 } 7922 if (m->m_next == NULL) { 7923 SOCKBUF_UNLOCK(sb); 7924 (void)m_free(m); 7925 error = ENOBUFS; 7926 sack_rxmit = 0; 7927 goto out; 7928 } 7929 } 7930 if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 7931 TCPSTAT_INC(tcps_sndprobe); 7932 #ifdef NETFLIX_STATS 7933 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7934 stats_voi_update_abs_u32(tp->t_stats, 7935 VOI_TCP_RETXPB, len); 7936 else 7937 stats_voi_update_abs_u64(tp->t_stats, 7938 VOI_TCP_TXPB, len); 7939 #endif 7940 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 7941 if (rsm && (rsm->r_flags & RACK_TLP)) { 7942 /* 7943 * TLP should not count in retran count, but 7944 * in its own bin 7945 */ 7946 counter_u64_add(rack_tlp_retran, 1); 7947 counter_u64_add(rack_tlp_retran_bytes, len); 7948 } else { 7949 tp->t_sndrexmitpack++; 7950 TCPSTAT_INC(tcps_sndrexmitpack); 7951 TCPSTAT_ADD(tcps_sndrexmitbyte, len); 7952 } 7953 #ifdef NETFLIX_STATS 7954 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 7955 len); 7956 #endif 7957 } else { 7958 TCPSTAT_INC(tcps_sndpack); 7959 TCPSTAT_ADD(tcps_sndbyte, len); 7960 #ifdef NETFLIX_STATS 7961 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 7962 len); 7963 #endif 7964 } 7965 /* 7966 * If we're sending everything we've got, set PUSH. (This 7967 * will keep happy those implementations which only give 7968 * data to the user when a buffer fills or a PUSH comes in.) 7969 */ 7970 if (sb_offset + len == sbused(sb) && 7971 sbused(sb) && 7972 !(flags & TH_SYN)) 7973 flags |= TH_PUSH; 7974 7975 /* 7976 * Are we doing hptsi, if so we must calculate the slot. We 7977 * only do hptsi in ESTABLISHED and with no RESET being 7978 * sent where we have data to send. 7979 */ 7980 if (((tp->t_state == TCPS_ESTABLISHED) || 7981 (tp->t_state == TCPS_CLOSE_WAIT) || 7982 ((tp->t_state == TCPS_FIN_WAIT_1) && 7983 ((tp->t_flags & TF_SENTFIN) == 0) && 7984 ((flags & TH_FIN) == 0))) && 7985 ((flags & TH_RST) == 0) && 7986 (rack->rc_always_pace)) { 7987 /* 7988 * We use the most optimistic possible cwnd/srtt for 7989 * sending calculations. This will make our 7990 * calculation anticipate getting more through 7991 * quicker then possible. But thats ok we don't want 7992 * the peer to have a gap in data sending. 7993 */ 7994 uint32_t srtt, cwnd, tr_perms = 0; 7995 7996 if (rack->r_ctl.rc_rack_min_rtt) 7997 srtt = rack->r_ctl.rc_rack_min_rtt; 7998 else 7999 srtt = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT)); 8000 if (rack->r_ctl.rc_rack_largest_cwnd) 8001 cwnd = rack->r_ctl.rc_rack_largest_cwnd; 8002 else 8003 cwnd = tp->snd_cwnd; 8004 tr_perms = cwnd / srtt; 8005 if (tr_perms == 0) { 8006 tr_perms = tp->t_maxseg; 8007 } 8008 tot_len_this_send += len; 8009 /* 8010 * Calculate how long this will take to drain, if 8011 * the calculation comes out to zero, thats ok we 8012 * will use send_a_lot to possibly spin around for 8013 * more increasing tot_len_this_send to the point 8014 * that its going to require a pace, or we hit the 8015 * cwnd. Which in that case we are just waiting for 8016 * a ACK. 8017 */ 8018 slot = tot_len_this_send / tr_perms; 8019 /* Now do we reduce the time so we don't run dry? */ 8020 if (slot && rack->rc_pace_reduce) { 8021 int32_t reduce; 8022 8023 reduce = (slot / rack->rc_pace_reduce); 8024 if (reduce < slot) { 8025 slot -= reduce; 8026 } else 8027 slot = 0; 8028 } 8029 if (rack->r_enforce_min_pace && 8030 (slot == 0) && 8031 (tot_len_this_send >= (rack->r_min_pace_seg_thresh * tp->t_maxseg))) { 8032 /* We are enforcing a minimum pace time of 1ms */ 8033 slot = rack->r_enforce_min_pace; 8034 } 8035 } 8036 SOCKBUF_UNLOCK(sb); 8037 } else { 8038 SOCKBUF_UNLOCK(sb); 8039 if (tp->t_flags & TF_ACKNOW) 8040 TCPSTAT_INC(tcps_sndacks); 8041 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 8042 TCPSTAT_INC(tcps_sndctrl); 8043 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 8044 TCPSTAT_INC(tcps_sndurg); 8045 else 8046 TCPSTAT_INC(tcps_sndwinup); 8047 8048 m = m_gethdr(M_NOWAIT, MT_DATA); 8049 if (m == NULL) { 8050 error = ENOBUFS; 8051 sack_rxmit = 0; 8052 goto out; 8053 } 8054 #ifdef INET6 8055 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 8056 MHLEN >= hdrlen) { 8057 M_ALIGN(m, hdrlen); 8058 } else 8059 #endif 8060 m->m_data += max_linkhdr; 8061 m->m_len = hdrlen; 8062 } 8063 SOCKBUF_UNLOCK_ASSERT(sb); 8064 m->m_pkthdr.rcvif = (struct ifnet *)0; 8065 #ifdef MAC 8066 mac_inpcb_create_mbuf(inp, m); 8067 #endif 8068 #ifdef INET6 8069 if (isipv6) { 8070 ip6 = mtod(m, struct ip6_hdr *); 8071 #ifdef NETFLIX_TCPOUDP 8072 if (tp->t_port) { 8073 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr)); 8074 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 8075 udp->uh_dport = tp->t_port; 8076 ulen = hdrlen + len - sizeof(struct ip6_hdr); 8077 udp->uh_ulen = htons(ulen); 8078 th = (struct tcphdr *)(udp + 1); 8079 } else 8080 #endif 8081 th = (struct tcphdr *)(ip6 + 1); 8082 tcpip_fillheaders(inp, ip6, th); 8083 } else 8084 #endif /* INET6 */ 8085 { 8086 ip = mtod(m, struct ip *); 8087 #ifdef TCPDEBUG 8088 ipov = (struct ipovly *)ip; 8089 #endif 8090 #ifdef NETFLIX_TCPOUDP 8091 if (tp->t_port) { 8092 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip)); 8093 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 8094 udp->uh_dport = tp->t_port; 8095 ulen = hdrlen + len - sizeof(struct ip); 8096 udp->uh_ulen = htons(ulen); 8097 th = (struct tcphdr *)(udp + 1); 8098 } else 8099 #endif 8100 th = (struct tcphdr *)(ip + 1); 8101 tcpip_fillheaders(inp, ip, th); 8102 } 8103 /* 8104 * Fill in fields, remembering maximum advertised window for use in 8105 * delaying messages about window sizes. If resending a FIN, be sure 8106 * not to use a new sequence number. 8107 */ 8108 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 8109 tp->snd_nxt == tp->snd_max) 8110 tp->snd_nxt--; 8111 /* 8112 * If we are starting a connection, send ECN setup SYN packet. If we 8113 * are on a retransmit, we may resend those bits a number of times 8114 * as per RFC 3168. 8115 */ 8116 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) { 8117 if (tp->t_rxtshift >= 1) { 8118 if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 8119 flags |= TH_ECE | TH_CWR; 8120 } else 8121 flags |= TH_ECE | TH_CWR; 8122 } 8123 if (tp->t_state == TCPS_ESTABLISHED && 8124 (tp->t_flags & TF_ECN_PERMIT)) { 8125 /* 8126 * If the peer has ECN, mark data packets with ECN capable 8127 * transmission (ECT). Ignore pure ack packets, 8128 * retransmissions and window probes. 8129 */ 8130 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 8131 !((tp->t_flags & TF_FORCEDATA) && len == 1)) { 8132 #ifdef INET6 8133 if (isipv6) 8134 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 8135 else 8136 #endif 8137 ip->ip_tos |= IPTOS_ECN_ECT0; 8138 TCPSTAT_INC(tcps_ecn_ect0); 8139 } 8140 /* 8141 * Reply with proper ECN notifications. 8142 */ 8143 if (tp->t_flags & TF_ECN_SND_CWR) { 8144 flags |= TH_CWR; 8145 tp->t_flags &= ~TF_ECN_SND_CWR; 8146 } 8147 if (tp->t_flags & TF_ECN_SND_ECE) 8148 flags |= TH_ECE; 8149 } 8150 /* 8151 * If we are doing retransmissions, then snd_nxt will not reflect 8152 * the first unsent octet. For ACK only packets, we do not want the 8153 * sequence number of the retransmitted packet, we want the sequence 8154 * number of the next unsent octet. So, if there is no data (and no 8155 * SYN or FIN), use snd_max instead of snd_nxt when filling in 8156 * ti_seq. But if we are in persist state, snd_max might reflect 8157 * one byte beyond the right edge of the window, so use snd_nxt in 8158 * that case, since we know we aren't doing a retransmission. 8159 * (retransmit and persist are mutually exclusive...) 8160 */ 8161 if (sack_rxmit == 0) { 8162 if (len || (flags & (TH_SYN | TH_FIN)) || 8163 rack->rc_in_persist) { 8164 th->th_seq = htonl(tp->snd_nxt); 8165 rack_seq = tp->snd_nxt; 8166 } else if (flags & TH_RST) { 8167 /* 8168 * For a Reset send the last cum ack in sequence 8169 * (this like any other choice may still generate a 8170 * challenge ack, if a ack-update packet is in 8171 * flight). 8172 */ 8173 th->th_seq = htonl(tp->snd_una); 8174 rack_seq = tp->snd_una; 8175 } else { 8176 th->th_seq = htonl(tp->snd_max); 8177 rack_seq = tp->snd_max; 8178 } 8179 } else { 8180 th->th_seq = htonl(rsm->r_start); 8181 rack_seq = rsm->r_start; 8182 } 8183 th->th_ack = htonl(tp->rcv_nxt); 8184 if (optlen) { 8185 bcopy(opt, th + 1, optlen); 8186 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 8187 } 8188 th->th_flags = flags; 8189 /* 8190 * Calculate receive window. Don't shrink window, but avoid silly 8191 * window syndrome. 8192 * If a RST segment is sent, advertise a window of zero. 8193 */ 8194 if (flags & TH_RST) { 8195 recwin = 0; 8196 } else { 8197 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 8198 recwin < (long)tp->t_maxseg) 8199 recwin = 0; 8200 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 8201 recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 8202 recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 8203 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 8204 recwin = (long)TCP_MAXWIN << tp->rcv_scale; 8205 } 8206 8207 /* 8208 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 8209 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 8210 * handled in syncache. 8211 */ 8212 if (flags & TH_SYN) 8213 th->th_win = htons((u_short) 8214 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 8215 else 8216 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 8217 /* 8218 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 8219 * window. This may cause the remote transmitter to stall. This 8220 * flag tells soreceive() to disable delayed acknowledgements when 8221 * draining the buffer. This can occur if the receiver is 8222 * attempting to read more data than can be buffered prior to 8223 * transmitting on the connection. 8224 */ 8225 if (th->th_win == 0) { 8226 tp->t_sndzerowin++; 8227 tp->t_flags |= TF_RXWIN0SENT; 8228 } else 8229 tp->t_flags &= ~TF_RXWIN0SENT; 8230 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 8231 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 8232 th->th_flags |= TH_URG; 8233 } else 8234 /* 8235 * If no urgent pointer to send, then we pull the urgent 8236 * pointer to the left edge of the send window so that it 8237 * doesn't drift into the send window on sequence number 8238 * wraparound. 8239 */ 8240 tp->snd_up = tp->snd_una; /* drag it along */ 8241 8242 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 8243 if (to.to_flags & TOF_SIGNATURE) { 8244 /* 8245 * Calculate MD5 signature and put it into the place 8246 * determined before. 8247 * NOTE: since TCP options buffer doesn't point into 8248 * mbuf's data, calculate offset and use it. 8249 */ 8250 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 8251 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 8252 /* 8253 * Do not send segment if the calculation of MD5 8254 * digest has failed. 8255 */ 8256 goto out; 8257 } 8258 } 8259 #endif 8260 8261 /* 8262 * Put TCP length in extended header, and then checksum extended 8263 * header and data. 8264 */ 8265 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 8266 #ifdef INET6 8267 if (isipv6) { 8268 /* 8269 * ip6_plen is not need to be filled now, and will be filled 8270 * in ip6_output. 8271 */ 8272 if (tp->t_port) { 8273 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 8274 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 8275 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 8276 th->th_sum = htons(0); 8277 } else { 8278 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 8279 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 8280 th->th_sum = in6_cksum_pseudo(ip6, 8281 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 8282 0); 8283 } 8284 } 8285 #endif 8286 #if defined(INET6) && defined(INET) 8287 else 8288 #endif 8289 #ifdef INET 8290 { 8291 if (tp->t_port) { 8292 m->m_pkthdr.csum_flags = CSUM_UDP; 8293 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 8294 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 8295 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 8296 th->th_sum = htons(0); 8297 } else { 8298 m->m_pkthdr.csum_flags = CSUM_TCP; 8299 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 8300 th->th_sum = in_pseudo(ip->ip_src.s_addr, 8301 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 8302 IPPROTO_TCP + len + optlen)); 8303 } 8304 /* IP version must be set here for ipv4/ipv6 checking later */ 8305 KASSERT(ip->ip_v == IPVERSION, 8306 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 8307 } 8308 #endif 8309 8310 /* 8311 * Enable TSO and specify the size of the segments. The TCP pseudo 8312 * header checksum is always provided. XXX: Fixme: This is currently 8313 * not the case for IPv6. 8314 */ 8315 if (tso) { 8316 KASSERT(len > tp->t_maxseg - optlen, 8317 ("%s: len <= tso_segsz", __func__)); 8318 m->m_pkthdr.csum_flags |= CSUM_TSO; 8319 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 8320 } 8321 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 8322 KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL), 8323 ("%s: mbuf chain shorter than expected: %d + %u + %u - %u != %u", 8324 __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL))); 8325 #else 8326 KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL), 8327 ("%s: mbuf chain shorter than expected: %d + %u + %u != %u", 8328 __func__, len, hdrlen, ipoptlen, m_length(m, NULL))); 8329 #endif 8330 8331 #ifdef TCP_HHOOK 8332 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 8333 hhook_run_tcp_est_out(tp, th, &to, len, tso); 8334 #endif 8335 8336 #ifdef TCPDEBUG 8337 /* 8338 * Trace. 8339 */ 8340 if (so->so_options & SO_DEBUG) { 8341 u_short save = 0; 8342 8343 #ifdef INET6 8344 if (!isipv6) 8345 #endif 8346 { 8347 save = ipov->ih_len; 8348 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + 8349 * (th->th_off << 2) */ ); 8350 } 8351 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 8352 #ifdef INET6 8353 if (!isipv6) 8354 #endif 8355 ipov->ih_len = save; 8356 } 8357 #endif /* TCPDEBUG */ 8358 8359 /* We're getting ready to send; log now. */ 8360 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 8361 union tcp_log_stackspecific log; 8362 8363 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 8364 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 8365 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 8366 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 8367 if (rsm || sack_rxmit) { 8368 log.u_bbr.flex8 = 1; 8369 } else { 8370 log.u_bbr.flex8 = 0; 8371 } 8372 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 8373 len, &log, false, NULL, NULL, 0, NULL); 8374 } else 8375 lgb = NULL; 8376 8377 /* 8378 * Fill in IP length and desired time to live and send to IP level. 8379 * There should be a better way to handle ttl and tos; we could keep 8380 * them in the template, but need a way to checksum without them. 8381 */ 8382 /* 8383 * m->m_pkthdr.len should have been set before cksum calcuration, 8384 * because in6_cksum() need it. 8385 */ 8386 #ifdef INET6 8387 if (isipv6) { 8388 /* 8389 * we separately set hoplimit for every segment, since the 8390 * user might want to change the value via setsockopt. Also, 8391 * desired default hop limit might be changed via Neighbor 8392 * Discovery. 8393 */ 8394 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 8395 8396 /* 8397 * Set the packet size here for the benefit of DTrace 8398 * probes. ip6_output() will set it properly; it's supposed 8399 * to include the option header lengths as well. 8400 */ 8401 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 8402 8403 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 8404 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 8405 else 8406 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8407 8408 if (tp->t_state == TCPS_SYN_SENT) 8409 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 8410 8411 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 8412 /* TODO: IPv6 IP6TOS_ECT bit on */ 8413 error = ip6_output(m, tp->t_inpcb->in6p_outputopts, 8414 &inp->inp_route6, 8415 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 8416 NULL, NULL, inp); 8417 8418 if (error == EMSGSIZE && inp->inp_route6.ro_rt != NULL) 8419 mtu = inp->inp_route6.ro_rt->rt_mtu; 8420 } 8421 #endif /* INET6 */ 8422 #if defined(INET) && defined(INET6) 8423 else 8424 #endif 8425 #ifdef INET 8426 { 8427 ip->ip_len = htons(m->m_pkthdr.len); 8428 #ifdef INET6 8429 if (inp->inp_vflag & INP_IPV6PROTO) 8430 ip->ip_ttl = in6_selecthlim(inp, NULL); 8431 #endif /* INET6 */ 8432 /* 8433 * If we do path MTU discovery, then we set DF on every 8434 * packet. This might not be the best thing to do according 8435 * to RFC3390 Section 2. However the tcp hostcache migitates 8436 * the problem so it affects only the first tcp connection 8437 * with a host. 8438 * 8439 * NB: Don't set DF on small MTU/MSS to have a safe 8440 * fallback. 8441 */ 8442 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 8443 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 8444 if (tp->t_port == 0 || len < V_tcp_minmss) { 8445 ip->ip_off |= htons(IP_DF); 8446 } 8447 } else { 8448 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8449 } 8450 8451 if (tp->t_state == TCPS_SYN_SENT) 8452 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 8453 8454 TCP_PROBE5(send, NULL, tp, ip, tp, th); 8455 8456 error = ip_output(m, tp->t_inpcb->inp_options, &inp->inp_route, 8457 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 8458 inp); 8459 if (error == EMSGSIZE && inp->inp_route.ro_rt != NULL) 8460 mtu = inp->inp_route.ro_rt->rt_mtu; 8461 } 8462 #endif /* INET */ 8463 8464 out: 8465 if (lgb) { 8466 lgb->tlb_errno = error; 8467 lgb = NULL; 8468 } 8469 /* 8470 * In transmit state, time the transmission and arrange for the 8471 * retransmit. In persist state, just set snd_max. 8472 */ 8473 if (error == 0) { 8474 if (len == 0) 8475 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1); 8476 else if (len == 1) { 8477 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1); 8478 } else if (len > 1) { 8479 int idx; 8480 8481 idx = (len / tp->t_maxseg) + 3; 8482 if (idx >= TCP_MSS_ACCT_ATIMER) 8483 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 8484 else 8485 counter_u64_add(rack_out_size[idx], 1); 8486 } 8487 } 8488 if (sub_from_prr && (error == 0)) { 8489 rack->r_ctl.rc_prr_sndcnt -= len; 8490 } 8491 sub_from_prr = 0; 8492 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, cts, 8493 pass, rsm); 8494 if ((tp->t_flags & TF_FORCEDATA) == 0 || 8495 (rack->rc_in_persist == 0)) { 8496 tcp_seq startseq = tp->snd_nxt; 8497 8498 /* 8499 * Advance snd_nxt over sequence space of this segment. 8500 */ 8501 if (error) 8502 /* We don't log or do anything with errors */ 8503 goto timer; 8504 8505 if (flags & (TH_SYN | TH_FIN)) { 8506 if (flags & TH_SYN) 8507 tp->snd_nxt++; 8508 if (flags & TH_FIN) { 8509 tp->snd_nxt++; 8510 tp->t_flags |= TF_SENTFIN; 8511 } 8512 } 8513 /* In the ENOBUFS case we do *not* update snd_max */ 8514 if (sack_rxmit) 8515 goto timer; 8516 8517 tp->snd_nxt += len; 8518 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 8519 if (tp->snd_una == tp->snd_max) { 8520 /* 8521 * Update the time we just added data since 8522 * none was outstanding. 8523 */ 8524 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 8525 tp->t_acktime = ticks; 8526 } 8527 tp->snd_max = tp->snd_nxt; 8528 /* 8529 * Time this transmission if not a retransmission and 8530 * not currently timing anything. 8531 * This is only relevant in case of switching back to 8532 * the base stack. 8533 */ 8534 if (tp->t_rtttime == 0) { 8535 tp->t_rtttime = ticks; 8536 tp->t_rtseq = startseq; 8537 TCPSTAT_INC(tcps_segstimed); 8538 } 8539 #ifdef NETFLIX_STATS 8540 if (!(tp->t_flags & TF_GPUTINPROG) && len) { 8541 tp->t_flags |= TF_GPUTINPROG; 8542 tp->gput_seq = startseq; 8543 tp->gput_ack = startseq + 8544 ulmin(sbavail(sb) - sb_offset, sendwin); 8545 tp->gput_ts = tcp_ts_getticks(); 8546 } 8547 #endif 8548 } 8549 /* 8550 * Set retransmit timer if not currently set, and not doing 8551 * a pure ack or a keep-alive probe. Initial value for 8552 * retransmit timer is smoothed round-trip time + 2 * 8553 * round-trip time variance. Initialize shift counter which 8554 * is used for backoff of retransmit time. 8555 */ 8556 timer: 8557 if ((tp->snd_wnd == 0) && 8558 TCPS_HAVEESTABLISHED(tp->t_state)) { 8559 /* 8560 * If the persists timer was set above (right before 8561 * the goto send), and still needs to be on. Lets 8562 * make sure all is canceled. If the persist timer 8563 * is not running, we want to get it up. 8564 */ 8565 if (rack->rc_in_persist == 0) { 8566 rack_enter_persist(tp, rack, cts); 8567 } 8568 } 8569 } else { 8570 /* 8571 * Persist case, update snd_max but since we are in persist 8572 * mode (no window) we do not update snd_nxt. 8573 */ 8574 int32_t xlen = len; 8575 8576 if (error) 8577 goto nomore; 8578 8579 if (flags & TH_SYN) 8580 ++xlen; 8581 if (flags & TH_FIN) { 8582 ++xlen; 8583 tp->t_flags |= TF_SENTFIN; 8584 } 8585 /* In the ENOBUFS case we do *not* update snd_max */ 8586 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) { 8587 if (tp->snd_una == tp->snd_max) { 8588 /* 8589 * Update the time we just added data since 8590 * none was outstanding. 8591 */ 8592 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 8593 tp->t_acktime = ticks; 8594 } 8595 tp->snd_max = tp->snd_nxt + len; 8596 } 8597 } 8598 nomore: 8599 if (error) { 8600 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */ 8601 /* 8602 * Failures do not advance the seq counter above. For the 8603 * case of ENOBUFS we will fall out and retry in 1ms with 8604 * the hpts. Everything else will just have to retransmit 8605 * with the timer. 8606 * 8607 * In any case, we do not want to loop around for another 8608 * send without a good reason. 8609 */ 8610 sendalot = 0; 8611 switch (error) { 8612 case EPERM: 8613 tp->t_flags &= ~TF_FORCEDATA; 8614 tp->t_softerror = error; 8615 return (error); 8616 case ENOBUFS: 8617 if (slot == 0) { 8618 /* 8619 * Pace us right away to retry in a some 8620 * time 8621 */ 8622 slot = 1 + rack->rc_enobuf; 8623 if (rack->rc_enobuf < 255) 8624 rack->rc_enobuf++; 8625 if (slot > (rack->rc_rack_rtt / 2)) { 8626 slot = rack->rc_rack_rtt / 2; 8627 } 8628 if (slot < 10) 8629 slot = 10; 8630 } 8631 counter_u64_add(rack_saw_enobuf, 1); 8632 error = 0; 8633 goto enobufs; 8634 case EMSGSIZE: 8635 /* 8636 * For some reason the interface we used initially 8637 * to send segments changed to another or lowered 8638 * its MTU. If TSO was active we either got an 8639 * interface without TSO capabilits or TSO was 8640 * turned off. If we obtained mtu from ip_output() 8641 * then update it and try again. 8642 */ 8643 if (tso) 8644 tp->t_flags &= ~TF_TSO; 8645 if (mtu != 0) { 8646 tcp_mss_update(tp, -1, mtu, NULL, NULL); 8647 goto again; 8648 } 8649 slot = 10; 8650 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1); 8651 tp->t_flags &= ~TF_FORCEDATA; 8652 return (error); 8653 case ENETUNREACH: 8654 counter_u64_add(rack_saw_enetunreach, 1); 8655 case EHOSTDOWN: 8656 case EHOSTUNREACH: 8657 case ENETDOWN: 8658 if (TCPS_HAVERCVDSYN(tp->t_state)) { 8659 tp->t_softerror = error; 8660 } 8661 /* FALLTHROUGH */ 8662 default: 8663 slot = 10; 8664 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1); 8665 tp->t_flags &= ~TF_FORCEDATA; 8666 return (error); 8667 } 8668 } else { 8669 rack->rc_enobuf = 0; 8670 } 8671 TCPSTAT_INC(tcps_sndtotal); 8672 8673 /* 8674 * Data sent (as far as we can tell). If this advertises a larger 8675 * window than any other segment, then remember the size of the 8676 * advertised window. Any pending ACK has now been sent. 8677 */ 8678 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 8679 tp->rcv_adv = tp->rcv_nxt + recwin; 8680 tp->last_ack_sent = tp->rcv_nxt; 8681 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 8682 enobufs: 8683 rack->r_tlp_running = 0; 8684 if ((flags & TH_RST) || (would_have_fin == 1)) { 8685 /* 8686 * We don't send again after a RST. We also do *not* send 8687 * again if we would have had a find, but now have 8688 * outstanding data. 8689 */ 8690 slot = 0; 8691 sendalot = 0; 8692 } 8693 if (slot) { 8694 /* set the rack tcb into the slot N */ 8695 counter_u64_add(rack_paced_segments, 1); 8696 } else if (sendalot) { 8697 if (len) 8698 counter_u64_add(rack_unpaced_segments, 1); 8699 sack_rxmit = 0; 8700 tp->t_flags &= ~TF_FORCEDATA; 8701 goto again; 8702 } else if (len) { 8703 counter_u64_add(rack_unpaced_segments, 1); 8704 } 8705 tp->t_flags &= ~TF_FORCEDATA; 8706 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1); 8707 return (error); 8708 } 8709 8710 /* 8711 * rack_ctloutput() must drop the inpcb lock before performing copyin on 8712 * socket option arguments. When it re-acquires the lock after the copy, it 8713 * has to revalidate that the connection is still valid for the socket 8714 * option. 8715 */ 8716 static int 8717 rack_set_sockopt(struct socket *so, struct sockopt *sopt, 8718 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 8719 { 8720 int32_t error = 0, optval; 8721 8722 switch (sopt->sopt_name) { 8723 case TCP_RACK_PROP_RATE: 8724 case TCP_RACK_PROP: 8725 case TCP_RACK_TLP_REDUCE: 8726 case TCP_RACK_EARLY_RECOV: 8727 case TCP_RACK_PACE_ALWAYS: 8728 case TCP_DELACK: 8729 case TCP_RACK_PACE_REDUCE: 8730 case TCP_RACK_PACE_MAX_SEG: 8731 case TCP_RACK_PRR_SENDALOT: 8732 case TCP_RACK_MIN_TO: 8733 case TCP_RACK_EARLY_SEG: 8734 case TCP_RACK_REORD_THRESH: 8735 case TCP_RACK_REORD_FADE: 8736 case TCP_RACK_TLP_THRESH: 8737 case TCP_RACK_PKT_DELAY: 8738 case TCP_RACK_TLP_USE: 8739 case TCP_RACK_TLP_INC_VAR: 8740 case TCP_RACK_IDLE_REDUCE_HIGH: 8741 case TCP_RACK_MIN_PACE: 8742 case TCP_RACK_MIN_PACE_SEG: 8743 case TCP_BBR_RACK_RTT_USE: 8744 case TCP_DATA_AFTER_CLOSE: 8745 break; 8746 default: 8747 return (tcp_default_ctloutput(so, sopt, inp, tp)); 8748 break; 8749 } 8750 INP_WUNLOCK(inp); 8751 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 8752 if (error) 8753 return (error); 8754 INP_WLOCK(inp); 8755 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8756 INP_WUNLOCK(inp); 8757 return (ECONNRESET); 8758 } 8759 tp = intotcpcb(inp); 8760 rack = (struct tcp_rack *)tp->t_fb_ptr; 8761 switch (sopt->sopt_name) { 8762 case TCP_RACK_PROP_RATE: 8763 if ((optval <= 0) || (optval >= 100)) { 8764 error = EINVAL; 8765 break; 8766 } 8767 RACK_OPTS_INC(tcp_rack_prop_rate); 8768 rack->r_ctl.rc_prop_rate = optval; 8769 break; 8770 case TCP_RACK_TLP_USE: 8771 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) { 8772 error = EINVAL; 8773 break; 8774 } 8775 RACK_OPTS_INC(tcp_tlp_use); 8776 rack->rack_tlp_threshold_use = optval; 8777 break; 8778 case TCP_RACK_PROP: 8779 /* RACK proportional rate reduction (bool) */ 8780 RACK_OPTS_INC(tcp_rack_prop); 8781 rack->r_ctl.rc_prop_reduce = optval; 8782 break; 8783 case TCP_RACK_TLP_REDUCE: 8784 /* RACK TLP cwnd reduction (bool) */ 8785 RACK_OPTS_INC(tcp_rack_tlp_reduce); 8786 rack->r_ctl.rc_tlp_cwnd_reduce = optval; 8787 break; 8788 case TCP_RACK_EARLY_RECOV: 8789 /* Should recovery happen early (bool) */ 8790 RACK_OPTS_INC(tcp_rack_early_recov); 8791 rack->r_ctl.rc_early_recovery = optval; 8792 break; 8793 case TCP_RACK_PACE_ALWAYS: 8794 /* Use the always pace method (bool) */ 8795 RACK_OPTS_INC(tcp_rack_pace_always); 8796 if (optval > 0) 8797 rack->rc_always_pace = 1; 8798 else 8799 rack->rc_always_pace = 0; 8800 break; 8801 case TCP_RACK_PACE_REDUCE: 8802 /* RACK Hptsi reduction factor (divisor) */ 8803 RACK_OPTS_INC(tcp_rack_pace_reduce); 8804 if (optval) 8805 /* Must be non-zero */ 8806 rack->rc_pace_reduce = optval; 8807 else 8808 error = EINVAL; 8809 break; 8810 case TCP_RACK_PACE_MAX_SEG: 8811 /* Max segments in a pace */ 8812 RACK_OPTS_INC(tcp_rack_max_seg); 8813 rack->rc_pace_max_segs = optval; 8814 break; 8815 case TCP_RACK_PRR_SENDALOT: 8816 /* Allow PRR to send more than one seg */ 8817 RACK_OPTS_INC(tcp_rack_prr_sendalot); 8818 rack->r_ctl.rc_prr_sendalot = optval; 8819 break; 8820 case TCP_RACK_MIN_TO: 8821 /* Minimum time between rack t-o's in ms */ 8822 RACK_OPTS_INC(tcp_rack_min_to); 8823 rack->r_ctl.rc_min_to = optval; 8824 break; 8825 case TCP_RACK_EARLY_SEG: 8826 /* If early recovery max segments */ 8827 RACK_OPTS_INC(tcp_rack_early_seg); 8828 rack->r_ctl.rc_early_recovery_segs = optval; 8829 break; 8830 case TCP_RACK_REORD_THRESH: 8831 /* RACK reorder threshold (shift amount) */ 8832 RACK_OPTS_INC(tcp_rack_reord_thresh); 8833 if ((optval > 0) && (optval < 31)) 8834 rack->r_ctl.rc_reorder_shift = optval; 8835 else 8836 error = EINVAL; 8837 break; 8838 case TCP_RACK_REORD_FADE: 8839 /* Does reordering fade after ms time */ 8840 RACK_OPTS_INC(tcp_rack_reord_fade); 8841 rack->r_ctl.rc_reorder_fade = optval; 8842 break; 8843 case TCP_RACK_TLP_THRESH: 8844 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 8845 RACK_OPTS_INC(tcp_rack_tlp_thresh); 8846 if (optval) 8847 rack->r_ctl.rc_tlp_threshold = optval; 8848 else 8849 error = EINVAL; 8850 break; 8851 case TCP_RACK_PKT_DELAY: 8852 /* RACK added ms i.e. rack-rtt + reord + N */ 8853 RACK_OPTS_INC(tcp_rack_pkt_delay); 8854 rack->r_ctl.rc_pkt_delay = optval; 8855 break; 8856 case TCP_RACK_TLP_INC_VAR: 8857 /* Does TLP include rtt variance in t-o */ 8858 RACK_OPTS_INC(tcp_rack_tlp_inc_var); 8859 rack->r_ctl.rc_prr_inc_var = optval; 8860 break; 8861 case TCP_RACK_IDLE_REDUCE_HIGH: 8862 RACK_OPTS_INC(tcp_rack_idle_reduce_high); 8863 if (optval) 8864 rack->r_idle_reduce_largest = 1; 8865 else 8866 rack->r_idle_reduce_largest = 0; 8867 break; 8868 case TCP_DELACK: 8869 if (optval == 0) 8870 tp->t_delayed_ack = 0; 8871 else 8872 tp->t_delayed_ack = 1; 8873 if (tp->t_flags & TF_DELACK) { 8874 tp->t_flags &= ~TF_DELACK; 8875 tp->t_flags |= TF_ACKNOW; 8876 rack_output(tp); 8877 } 8878 break; 8879 case TCP_RACK_MIN_PACE: 8880 RACK_OPTS_INC(tcp_rack_min_pace); 8881 if (optval > 3) 8882 rack->r_enforce_min_pace = 3; 8883 else 8884 rack->r_enforce_min_pace = optval; 8885 break; 8886 case TCP_RACK_MIN_PACE_SEG: 8887 RACK_OPTS_INC(tcp_rack_min_pace_seg); 8888 if (optval >= 16) 8889 rack->r_min_pace_seg_thresh = 15; 8890 else 8891 rack->r_min_pace_seg_thresh = optval; 8892 break; 8893 case TCP_BBR_RACK_RTT_USE: 8894 if ((optval != USE_RTT_HIGH) && 8895 (optval != USE_RTT_LOW) && 8896 (optval != USE_RTT_AVG)) 8897 error = EINVAL; 8898 else 8899 rack->r_ctl.rc_rate_sample_method = optval; 8900 break; 8901 case TCP_DATA_AFTER_CLOSE: 8902 if (optval) 8903 rack->rc_allow_data_af_clo = 1; 8904 else 8905 rack->rc_allow_data_af_clo = 0; 8906 break; 8907 default: 8908 return (tcp_default_ctloutput(so, sopt, inp, tp)); 8909 break; 8910 } 8911 #ifdef NETFLIX_STATS 8912 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 8913 #endif 8914 INP_WUNLOCK(inp); 8915 return (error); 8916 } 8917 8918 static int 8919 rack_get_sockopt(struct socket *so, struct sockopt *sopt, 8920 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 8921 { 8922 int32_t error, optval; 8923 8924 /* 8925 * Because all our options are either boolean or an int, we can just 8926 * pull everything into optval and then unlock and copy. If we ever 8927 * add a option that is not a int, then this will have quite an 8928 * impact to this routine. 8929 */ 8930 switch (sopt->sopt_name) { 8931 case TCP_RACK_PROP_RATE: 8932 optval = rack->r_ctl.rc_prop_rate; 8933 break; 8934 case TCP_RACK_PROP: 8935 /* RACK proportional rate reduction (bool) */ 8936 optval = rack->r_ctl.rc_prop_reduce; 8937 break; 8938 case TCP_RACK_TLP_REDUCE: 8939 /* RACK TLP cwnd reduction (bool) */ 8940 optval = rack->r_ctl.rc_tlp_cwnd_reduce; 8941 break; 8942 case TCP_RACK_EARLY_RECOV: 8943 /* Should recovery happen early (bool) */ 8944 optval = rack->r_ctl.rc_early_recovery; 8945 break; 8946 case TCP_RACK_PACE_REDUCE: 8947 /* RACK Hptsi reduction factor (divisor) */ 8948 optval = rack->rc_pace_reduce; 8949 break; 8950 case TCP_RACK_PACE_MAX_SEG: 8951 /* Max segments in a pace */ 8952 optval = rack->rc_pace_max_segs; 8953 break; 8954 case TCP_RACK_PACE_ALWAYS: 8955 /* Use the always pace method */ 8956 optval = rack->rc_always_pace; 8957 break; 8958 case TCP_RACK_PRR_SENDALOT: 8959 /* Allow PRR to send more than one seg */ 8960 optval = rack->r_ctl.rc_prr_sendalot; 8961 break; 8962 case TCP_RACK_MIN_TO: 8963 /* Minimum time between rack t-o's in ms */ 8964 optval = rack->r_ctl.rc_min_to; 8965 break; 8966 case TCP_RACK_EARLY_SEG: 8967 /* If early recovery max segments */ 8968 optval = rack->r_ctl.rc_early_recovery_segs; 8969 break; 8970 case TCP_RACK_REORD_THRESH: 8971 /* RACK reorder threshold (shift amount) */ 8972 optval = rack->r_ctl.rc_reorder_shift; 8973 break; 8974 case TCP_RACK_REORD_FADE: 8975 /* Does reordering fade after ms time */ 8976 optval = rack->r_ctl.rc_reorder_fade; 8977 break; 8978 case TCP_RACK_TLP_THRESH: 8979 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 8980 optval = rack->r_ctl.rc_tlp_threshold; 8981 break; 8982 case TCP_RACK_PKT_DELAY: 8983 /* RACK added ms i.e. rack-rtt + reord + N */ 8984 optval = rack->r_ctl.rc_pkt_delay; 8985 break; 8986 case TCP_RACK_TLP_USE: 8987 optval = rack->rack_tlp_threshold_use; 8988 break; 8989 case TCP_RACK_TLP_INC_VAR: 8990 /* Does TLP include rtt variance in t-o */ 8991 optval = rack->r_ctl.rc_prr_inc_var; 8992 break; 8993 case TCP_RACK_IDLE_REDUCE_HIGH: 8994 optval = rack->r_idle_reduce_largest; 8995 break; 8996 case TCP_RACK_MIN_PACE: 8997 optval = rack->r_enforce_min_pace; 8998 break; 8999 case TCP_RACK_MIN_PACE_SEG: 9000 optval = rack->r_min_pace_seg_thresh; 9001 break; 9002 case TCP_BBR_RACK_RTT_USE: 9003 optval = rack->r_ctl.rc_rate_sample_method; 9004 break; 9005 case TCP_DELACK: 9006 optval = tp->t_delayed_ack; 9007 break; 9008 case TCP_DATA_AFTER_CLOSE: 9009 optval = rack->rc_allow_data_af_clo; 9010 break; 9011 default: 9012 return (tcp_default_ctloutput(so, sopt, inp, tp)); 9013 break; 9014 } 9015 INP_WUNLOCK(inp); 9016 error = sooptcopyout(sopt, &optval, sizeof optval); 9017 return (error); 9018 } 9019 9020 static int 9021 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 9022 { 9023 int32_t error = EINVAL; 9024 struct tcp_rack *rack; 9025 9026 rack = (struct tcp_rack *)tp->t_fb_ptr; 9027 if (rack == NULL) { 9028 /* Huh? */ 9029 goto out; 9030 } 9031 if (sopt->sopt_dir == SOPT_SET) { 9032 return (rack_set_sockopt(so, sopt, inp, tp, rack)); 9033 } else if (sopt->sopt_dir == SOPT_GET) { 9034 return (rack_get_sockopt(so, sopt, inp, tp, rack)); 9035 } 9036 out: 9037 INP_WUNLOCK(inp); 9038 return (error); 9039 } 9040 9041 9042 struct tcp_function_block __tcp_rack = { 9043 .tfb_tcp_block_name = __XSTRING(STACKNAME), 9044 .tfb_tcp_output = rack_output, 9045 .tfb_tcp_do_segment = rack_do_segment, 9046 .tfb_tcp_hpts_do_segment = rack_hpts_do_segment, 9047 .tfb_tcp_ctloutput = rack_ctloutput, 9048 .tfb_tcp_fb_init = rack_init, 9049 .tfb_tcp_fb_fini = rack_fini, 9050 .tfb_tcp_timer_stop_all = rack_stopall, 9051 .tfb_tcp_timer_activate = rack_timer_activate, 9052 .tfb_tcp_timer_active = rack_timer_active, 9053 .tfb_tcp_timer_stop = rack_timer_stop, 9054 .tfb_tcp_rexmit_tmr = rack_remxt_tmr, 9055 .tfb_tcp_handoff_ok = rack_handoff_ok 9056 }; 9057 9058 static const char *rack_stack_names[] = { 9059 __XSTRING(STACKNAME), 9060 #ifdef STACKALIAS 9061 __XSTRING(STACKALIAS), 9062 #endif 9063 }; 9064 9065 static int 9066 rack_ctor(void *mem, int32_t size, void *arg, int32_t how) 9067 { 9068 memset(mem, 0, size); 9069 return (0); 9070 } 9071 9072 static void 9073 rack_dtor(void *mem, int32_t size, void *arg) 9074 { 9075 9076 } 9077 9078 static bool rack_mod_inited = false; 9079 9080 static int 9081 tcp_addrack(module_t mod, int32_t type, void *data) 9082 { 9083 int32_t err = 0; 9084 int num_stacks; 9085 9086 switch (type) { 9087 case MOD_LOAD: 9088 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 9089 sizeof(struct rack_sendmap), 9090 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 9091 9092 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 9093 sizeof(struct tcp_rack), 9094 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 9095 9096 sysctl_ctx_init(&rack_sysctl_ctx); 9097 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 9098 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 9099 OID_AUTO, 9100 __XSTRING(STACKNAME), 9101 CTLFLAG_RW, 0, 9102 ""); 9103 if (rack_sysctl_root == NULL) { 9104 printf("Failed to add sysctl node\n"); 9105 err = EFAULT; 9106 goto free_uma; 9107 } 9108 rack_init_sysctls(); 9109 num_stacks = nitems(rack_stack_names); 9110 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK, 9111 rack_stack_names, &num_stacks); 9112 if (err) { 9113 printf("Failed to register %s stack name for " 9114 "%s module\n", rack_stack_names[num_stacks], 9115 __XSTRING(MODNAME)); 9116 sysctl_ctx_free(&rack_sysctl_ctx); 9117 free_uma: 9118 uma_zdestroy(rack_zone); 9119 uma_zdestroy(rack_pcb_zone); 9120 rack_counter_destroy(); 9121 printf("Failed to register rack module -- err:%d\n", err); 9122 return (err); 9123 } 9124 rack_mod_inited = true; 9125 break; 9126 case MOD_QUIESCE: 9127 err = deregister_tcp_functions(&__tcp_rack, true, false); 9128 break; 9129 case MOD_UNLOAD: 9130 err = deregister_tcp_functions(&__tcp_rack, false, true); 9131 if (err == EBUSY) 9132 break; 9133 if (rack_mod_inited) { 9134 uma_zdestroy(rack_zone); 9135 uma_zdestroy(rack_pcb_zone); 9136 sysctl_ctx_free(&rack_sysctl_ctx); 9137 rack_counter_destroy(); 9138 rack_mod_inited = false; 9139 } 9140 err = 0; 9141 break; 9142 default: 9143 return (EOPNOTSUPP); 9144 } 9145 return (err); 9146 } 9147 9148 static moduledata_t tcp_rack = { 9149 .name = __XSTRING(MODNAME), 9150 .evhand = tcp_addrack, 9151 .priv = 0 9152 }; 9153 9154 MODULE_VERSION(MODNAME, 1); 9155 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 9156 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 9157