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