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 nsegs = max(1, m->m_pkthdr.lro_nsegs); 4661 if ((thflags & TH_ACK) && 4662 (SEQ_LT(tp->snd_wl1, th->th_seq) || 4663 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 4664 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 4665 /* keep track of pure window updates */ 4666 if (tlen == 0 && 4667 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 4668 TCPSTAT_INC(tcps_rcvwinupd); 4669 tp->snd_wnd = tiwin; 4670 tp->snd_wl1 = th->th_seq; 4671 tp->snd_wl2 = th->th_ack; 4672 if (tp->snd_wnd > tp->max_sndwnd) 4673 tp->max_sndwnd = tp->snd_wnd; 4674 rack->r_wanted_output++; 4675 } else if (thflags & TH_ACK) { 4676 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 4677 tp->snd_wnd = tiwin; 4678 tp->snd_wl1 = th->th_seq; 4679 tp->snd_wl2 = th->th_ack; 4680 } 4681 } 4682 /* Was persist timer active and now we have window space? */ 4683 if ((rack->rc_in_persist != 0) && tp->snd_wnd) { 4684 rack_exit_persist(tp, rack); 4685 tp->snd_nxt = tp->snd_max; 4686 /* Make sure we output to start the timer */ 4687 rack->r_wanted_output++; 4688 } 4689 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 4690 m_freem(m); 4691 return (0); 4692 } 4693 /* 4694 * Process segments with URG. 4695 */ 4696 if ((thflags & TH_URG) && th->th_urp && 4697 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4698 /* 4699 * This is a kludge, but if we receive and accept random 4700 * urgent pointers, we'll crash in soreceive. It's hard to 4701 * imagine someone actually wanting to send this much urgent 4702 * data. 4703 */ 4704 SOCKBUF_LOCK(&so->so_rcv); 4705 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 4706 th->th_urp = 0; /* XXX */ 4707 thflags &= ~TH_URG; /* XXX */ 4708 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 4709 goto dodata; /* XXX */ 4710 } 4711 /* 4712 * If this segment advances the known urgent pointer, then 4713 * mark the data stream. This should not happen in 4714 * CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since a 4715 * FIN has been received from the remote side. In these 4716 * states we ignore the URG. 4717 * 4718 * According to RFC961 (Assigned Protocols), the urgent 4719 * pointer points to the last octet of urgent data. We 4720 * continue, however, to consider it to indicate the first 4721 * octet of data past the urgent section as the original 4722 * spec states (in one of two places). 4723 */ 4724 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) { 4725 tp->rcv_up = th->th_seq + th->th_urp; 4726 so->so_oobmark = sbavail(&so->so_rcv) + 4727 (tp->rcv_up - tp->rcv_nxt) - 1; 4728 if (so->so_oobmark == 0) 4729 so->so_rcv.sb_state |= SBS_RCVATMARK; 4730 sohasoutofband(so); 4731 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 4732 } 4733 SOCKBUF_UNLOCK(&so->so_rcv); 4734 /* 4735 * Remove out of band data so doesn't get presented to user. 4736 * This can happen independent of advancing the URG pointer, 4737 * but if two URG's are pending at once, some out-of-band 4738 * data may creep in... ick. 4739 */ 4740 if (th->th_urp <= (uint32_t) tlen && 4741 !(so->so_options & SO_OOBINLINE)) { 4742 /* hdr drop is delayed */ 4743 tcp_pulloutofband(so, th, m, drop_hdrlen); 4744 } 4745 } else { 4746 /* 4747 * If no out of band data is expected, pull receive urgent 4748 * pointer along with the receive window. 4749 */ 4750 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 4751 tp->rcv_up = tp->rcv_nxt; 4752 } 4753 dodata: /* XXX */ 4754 INP_WLOCK_ASSERT(tp->t_inpcb); 4755 4756 /* 4757 * Process the segment text, merging it into the TCP sequencing 4758 * queue, and arranging for acknowledgment of receipt if necessary. 4759 * This process logically involves adjusting tp->rcv_wnd as data is 4760 * presented to the user (this happens in tcp_usrreq.c, case 4761 * PRU_RCVD). If a FIN has already been received on this connection 4762 * then we just ignore the text. 4763 */ 4764 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 4765 IS_FASTOPEN(tp->t_flags)); 4766 if ((tlen || (thflags & TH_FIN) || tfo_syn) && 4767 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4768 tcp_seq save_start = th->th_seq; 4769 4770 m_adj(m, drop_hdrlen); /* delayed header drop */ 4771 /* 4772 * Insert segment which includes th into TCP reassembly 4773 * queue with control block tp. Set thflags to whether 4774 * reassembly now includes a segment with FIN. This handles 4775 * the common case inline (segment is the next to be 4776 * received on an established connection, and the queue is 4777 * empty), avoiding linkage into and removal from the queue 4778 * and repetition of various conversions. Set DELACK for 4779 * segments received in order, but ack immediately when 4780 * segments are out of order (so fast retransmit can work). 4781 */ 4782 if (th->th_seq == tp->rcv_nxt && 4783 LIST_EMPTY(&tp->t_segq) && 4784 (TCPS_HAVEESTABLISHED(tp->t_state) || 4785 tfo_syn)) { 4786 if (DELAY_ACK(tp, tlen) || tfo_syn) { 4787 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4788 tp->t_flags |= TF_DELACK; 4789 } else { 4790 rack->r_wanted_output++; 4791 tp->t_flags |= TF_ACKNOW; 4792 } 4793 tp->rcv_nxt += tlen; 4794 thflags = th->th_flags & TH_FIN; 4795 TCPSTAT_ADD(tcps_rcvpack, nsegs); 4796 TCPSTAT_ADD(tcps_rcvbyte, tlen); 4797 SOCKBUF_LOCK(&so->so_rcv); 4798 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 4799 m_freem(m); 4800 else 4801 sbappendstream_locked(&so->so_rcv, m, 0); 4802 /* NB: sorwakeup_locked() does an implicit unlock. */ 4803 sorwakeup_locked(so); 4804 } else { 4805 /* 4806 * XXX: Due to the header drop above "th" is 4807 * theoretically invalid by now. Fortunately 4808 * m_adj() doesn't actually frees any mbufs when 4809 * trimming from the head. 4810 */ 4811 thflags = tcp_reass(tp, th, &tlen, m); 4812 tp->t_flags |= TF_ACKNOW; 4813 } 4814 if (tlen > 0) 4815 tcp_update_sack_list(tp, save_start, save_start + tlen); 4816 } else { 4817 m_freem(m); 4818 thflags &= ~TH_FIN; 4819 } 4820 4821 /* 4822 * If FIN is received ACK the FIN and let the user know that the 4823 * connection is closing. 4824 */ 4825 if (thflags & TH_FIN) { 4826 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 4827 socantrcvmore(so); 4828 /* 4829 * If connection is half-synchronized (ie NEEDSYN 4830 * flag on) then delay ACK, so it may be piggybacked 4831 * when SYN is sent. Otherwise, since we received a 4832 * FIN then no more input can be expected, send ACK 4833 * now. 4834 */ 4835 if (tp->t_flags & TF_NEEDSYN) { 4836 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4837 tp->t_flags |= TF_DELACK; 4838 } else { 4839 tp->t_flags |= TF_ACKNOW; 4840 } 4841 tp->rcv_nxt++; 4842 } 4843 switch (tp->t_state) { 4844 4845 /* 4846 * In SYN_RECEIVED and ESTABLISHED STATES enter the 4847 * CLOSE_WAIT state. 4848 */ 4849 case TCPS_SYN_RECEIVED: 4850 tp->t_starttime = ticks; 4851 /* FALLTHROUGH */ 4852 case TCPS_ESTABLISHED: 4853 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4854 tcp_state_change(tp, TCPS_CLOSE_WAIT); 4855 break; 4856 4857 /* 4858 * If still in FIN_WAIT_1 STATE FIN has not been 4859 * acked so enter the CLOSING state. 4860 */ 4861 case TCPS_FIN_WAIT_1: 4862 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4863 tcp_state_change(tp, TCPS_CLOSING); 4864 break; 4865 4866 /* 4867 * In FIN_WAIT_2 state enter the TIME_WAIT state, 4868 * starting the time-wait timer, turning off the 4869 * other standard timers. 4870 */ 4871 case TCPS_FIN_WAIT_2: 4872 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4873 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 4874 tcp_twstart(tp); 4875 return (1); 4876 } 4877 } 4878 /* 4879 * Return any desired output. 4880 */ 4881 if ((tp->t_flags & TF_ACKNOW) || (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) { 4882 rack->r_wanted_output++; 4883 } 4884 INP_WLOCK_ASSERT(tp->t_inpcb); 4885 return (0); 4886 } 4887 4888 /* 4889 * Here nothing is really faster, its just that we 4890 * have broken out the fast-data path also just like 4891 * the fast-ack. 4892 */ 4893 static int 4894 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 4895 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 4896 uint32_t tiwin, int32_t nxt_pkt) 4897 { 4898 int32_t nsegs; 4899 int32_t newsize = 0; /* automatic sockbuf scaling */ 4900 struct tcp_rack *rack; 4901 #ifdef TCPDEBUG 4902 /* 4903 * The size of tcp_saveipgen must be the size of the max ip header, 4904 * now IPv6. 4905 */ 4906 u_char tcp_saveipgen[IP6_HDR_LEN]; 4907 struct tcphdr tcp_savetcp; 4908 short ostate = 0; 4909 4910 #endif 4911 /* 4912 * If last ACK falls within this segment's sequence numbers, record 4913 * the timestamp. NOTE that the test is modified according to the 4914 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 4915 */ 4916 if (__predict_false(th->th_seq != tp->rcv_nxt)) { 4917 return (0); 4918 } 4919 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 4920 return (0); 4921 } 4922 if (tiwin && tiwin != tp->snd_wnd) { 4923 return (0); 4924 } 4925 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 4926 return (0); 4927 } 4928 if (__predict_false((to->to_flags & TOF_TS) && 4929 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 4930 return (0); 4931 } 4932 if (__predict_false((th->th_ack != tp->snd_una))) { 4933 return (0); 4934 } 4935 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 4936 return (0); 4937 } 4938 if ((to->to_flags & TOF_TS) != 0 && 4939 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 4940 tp->ts_recent_age = tcp_ts_getticks(); 4941 tp->ts_recent = to->to_tsval; 4942 } 4943 rack = (struct tcp_rack *)tp->t_fb_ptr; 4944 /* 4945 * This is a pure, in-sequence data packet with nothing on the 4946 * reassembly queue and we have enough buffer space to take it. 4947 */ 4948 nsegs = max(1, m->m_pkthdr.lro_nsegs); 4949 4950 4951 /* Clean receiver SACK report if present */ 4952 if (tp->rcv_numsacks) 4953 tcp_clean_sackreport(tp); 4954 TCPSTAT_INC(tcps_preddat); 4955 tp->rcv_nxt += tlen; 4956 /* 4957 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 4958 */ 4959 tp->snd_wl1 = th->th_seq; 4960 /* 4961 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 4962 */ 4963 tp->rcv_up = tp->rcv_nxt; 4964 TCPSTAT_ADD(tcps_rcvpack, nsegs); 4965 TCPSTAT_ADD(tcps_rcvbyte, tlen); 4966 #ifdef TCPDEBUG 4967 if (so->so_options & SO_DEBUG) 4968 tcp_trace(TA_INPUT, ostate, tp, 4969 (void *)tcp_saveipgen, &tcp_savetcp, 0); 4970 #endif 4971 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 4972 4973 /* Add data to socket buffer. */ 4974 SOCKBUF_LOCK(&so->so_rcv); 4975 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 4976 m_freem(m); 4977 } else { 4978 /* 4979 * Set new socket buffer size. Give up when limit is 4980 * reached. 4981 */ 4982 if (newsize) 4983 if (!sbreserve_locked(&so->so_rcv, 4984 newsize, so, NULL)) 4985 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 4986 m_adj(m, drop_hdrlen); /* delayed header drop */ 4987 sbappendstream_locked(&so->so_rcv, m, 0); 4988 rack_calc_rwin(so, tp); 4989 } 4990 /* NB: sorwakeup_locked() does an implicit unlock. */ 4991 sorwakeup_locked(so); 4992 if (DELAY_ACK(tp, tlen)) { 4993 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 4994 tp->t_flags |= TF_DELACK; 4995 } else { 4996 tp->t_flags |= TF_ACKNOW; 4997 rack->r_wanted_output++; 4998 } 4999 if ((tp->snd_una == tp->snd_max) && rack_use_sack_filter) 5000 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 5001 return (1); 5002 } 5003 5004 /* 5005 * This subfunction is used to try to highly optimize the 5006 * fast path. We again allow window updates that are 5007 * in sequence to remain in the fast-path. We also add 5008 * in the __predict's to attempt to help the compiler. 5009 * Note that if we return a 0, then we can *not* process 5010 * it and the caller should push the packet into the 5011 * slow-path. 5012 */ 5013 static int 5014 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 5015 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5016 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts) 5017 { 5018 int32_t acked; 5019 int32_t nsegs; 5020 5021 #ifdef TCPDEBUG 5022 /* 5023 * The size of tcp_saveipgen must be the size of the max ip header, 5024 * now IPv6. 5025 */ 5026 u_char tcp_saveipgen[IP6_HDR_LEN]; 5027 struct tcphdr tcp_savetcp; 5028 short ostate = 0; 5029 5030 #endif 5031 struct tcp_rack *rack; 5032 5033 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 5034 /* Old ack, behind (or duplicate to) the last one rcv'd */ 5035 return (0); 5036 } 5037 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 5038 /* Above what we have sent? */ 5039 return (0); 5040 } 5041 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 5042 /* We are retransmitting */ 5043 return (0); 5044 } 5045 if (__predict_false(tiwin == 0)) { 5046 /* zero window */ 5047 return (0); 5048 } 5049 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 5050 /* We need a SYN or a FIN, unlikely.. */ 5051 return (0); 5052 } 5053 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 5054 /* Timestamp is behind .. old ack with seq wrap? */ 5055 return (0); 5056 } 5057 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 5058 /* Still recovering */ 5059 return (0); 5060 } 5061 rack = (struct tcp_rack *)tp->t_fb_ptr; 5062 if (rack->r_ctl.rc_sacked) { 5063 /* We have sack holes on our scoreboard */ 5064 return (0); 5065 } 5066 /* Ok if we reach here, we can process a fast-ack */ 5067 nsegs = max(1, m->m_pkthdr.lro_nsegs); 5068 rack_log_ack(tp, to, th); 5069 /* Did the window get updated? */ 5070 if (tiwin != tp->snd_wnd) { 5071 tp->snd_wnd = tiwin; 5072 tp->snd_wl1 = th->th_seq; 5073 if (tp->snd_wnd > tp->max_sndwnd) 5074 tp->max_sndwnd = tp->snd_wnd; 5075 } 5076 if ((rack->rc_in_persist != 0) && (tp->snd_wnd >= tp->t_maxseg)) { 5077 rack_exit_persist(tp, rack); 5078 } 5079 /* 5080 * If last ACK falls within this segment's sequence numbers, record 5081 * the timestamp. NOTE that the test is modified according to the 5082 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 5083 */ 5084 if ((to->to_flags & TOF_TS) != 0 && 5085 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 5086 tp->ts_recent_age = tcp_ts_getticks(); 5087 tp->ts_recent = to->to_tsval; 5088 } 5089 /* 5090 * This is a pure ack for outstanding data. 5091 */ 5092 TCPSTAT_INC(tcps_predack); 5093 5094 /* 5095 * "bad retransmit" recovery. 5096 */ 5097 if (tp->t_flags & TF_PREVVALID) { 5098 tp->t_flags &= ~TF_PREVVALID; 5099 if (tp->t_rxtshift == 1 && 5100 (int)(ticks - tp->t_badrxtwin) < 0) 5101 rack_cong_signal(tp, th, CC_RTO_ERR); 5102 } 5103 /* 5104 * Recalculate the transmit timer / rtt. 5105 * 5106 * Some boxes send broken timestamp replies during the SYN+ACK 5107 * phase, ignore timestamps of 0 or we could calculate a huge RTT 5108 * and blow up the retransmit timer. 5109 */ 5110 acked = BYTES_THIS_ACK(tp, th); 5111 5112 #ifdef TCP_HHOOK 5113 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 5114 hhook_run_tcp_est_in(tp, th, to); 5115 #endif 5116 5117 TCPSTAT_ADD(tcps_rcvackpack, nsegs); 5118 TCPSTAT_ADD(tcps_rcvackbyte, acked); 5119 sbdrop(&so->so_snd, acked); 5120 /* 5121 * Let the congestion control algorithm update congestion control 5122 * related information. This typically means increasing the 5123 * congestion window. 5124 */ 5125 rack_ack_received(tp, rack, th, nsegs, CC_ACK, 0); 5126 5127 tp->snd_una = th->th_ack; 5128 /* 5129 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 5130 */ 5131 tp->snd_wl2 = th->th_ack; 5132 tp->t_dupacks = 0; 5133 m_freem(m); 5134 /* ND6_HINT(tp); *//* Some progress has been made. */ 5135 5136 /* 5137 * If all outstanding data are acked, stop retransmit timer, 5138 * otherwise restart timer using current (possibly backed-off) 5139 * value. If process is waiting for space, wakeup/selwakeup/signal. 5140 * If data are ready to send, let tcp_output decide between more 5141 * output or persist. 5142 */ 5143 #ifdef TCPDEBUG 5144 if (so->so_options & SO_DEBUG) 5145 tcp_trace(TA_INPUT, ostate, tp, 5146 (void *)tcp_saveipgen, 5147 &tcp_savetcp, 0); 5148 #endif 5149 if (tp->snd_una == tp->snd_max) { 5150 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 5151 tp->t_acktime = 0; 5152 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 5153 } 5154 /* Wake up the socket if we have room to write more */ 5155 sowwakeup(so); 5156 if (sbavail(&so->so_snd)) { 5157 rack->r_wanted_output++; 5158 } 5159 return (1); 5160 } 5161 5162 /* 5163 * Return value of 1, the TCB is unlocked and most 5164 * likely gone, return value of 0, the TCP is still 5165 * locked. 5166 */ 5167 static int 5168 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 5169 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5170 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5171 { 5172 int32_t ret_val = 0; 5173 int32_t todrop; 5174 int32_t ourfinisacked = 0; 5175 5176 rack_calc_rwin(so, tp); 5177 /* 5178 * If the state is SYN_SENT: if seg contains an ACK, but not for our 5179 * SYN, drop the input. if seg contains a RST, then drop the 5180 * connection. if seg does not contain SYN, then drop it. Otherwise 5181 * this is an acceptable SYN segment initialize tp->rcv_nxt and 5182 * tp->irs if seg contains ack then advance tp->snd_una if seg 5183 * contains an ECE and ECN support is enabled, the stream is ECN 5184 * capable. if SYN has been acked change to ESTABLISHED else 5185 * SYN_RCVD state arrange for segment to be acked (eventually) 5186 * continue processing rest of data/controls, beginning with URG 5187 */ 5188 if ((thflags & TH_ACK) && 5189 (SEQ_LEQ(th->th_ack, tp->iss) || 5190 SEQ_GT(th->th_ack, tp->snd_max))) { 5191 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5192 return (1); 5193 } 5194 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 5195 TCP_PROBE5(connect__refused, NULL, tp, 5196 mtod(m, const char *), tp, th); 5197 tp = tcp_drop(tp, ECONNREFUSED); 5198 rack_do_drop(m, tp); 5199 return (1); 5200 } 5201 if (thflags & TH_RST) { 5202 rack_do_drop(m, tp); 5203 return (1); 5204 } 5205 if (!(thflags & TH_SYN)) { 5206 rack_do_drop(m, tp); 5207 return (1); 5208 } 5209 tp->irs = th->th_seq; 5210 tcp_rcvseqinit(tp); 5211 if (thflags & TH_ACK) { 5212 int tfo_partial = 0; 5213 5214 TCPSTAT_INC(tcps_connects); 5215 soisconnected(so); 5216 #ifdef MAC 5217 mac_socketpeer_set_from_mbuf(m, so); 5218 #endif 5219 /* Do window scaling on this connection? */ 5220 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 5221 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 5222 tp->rcv_scale = tp->request_r_scale; 5223 } 5224 tp->rcv_adv += min(tp->rcv_wnd, 5225 TCP_MAXWIN << tp->rcv_scale); 5226 /* 5227 * If not all the data that was sent in the TFO SYN 5228 * has been acked, resend the remainder right away. 5229 */ 5230 if (IS_FASTOPEN(tp->t_flags) && 5231 (tp->snd_una != tp->snd_max)) { 5232 tp->snd_nxt = th->th_ack; 5233 tfo_partial = 1; 5234 } 5235 /* 5236 * If there's data, delay ACK; if there's also a FIN ACKNOW 5237 * will be turned on later. 5238 */ 5239 if (DELAY_ACK(tp, tlen) && tlen != 0 && (tfo_partial == 0)) { 5240 rack_timer_cancel(tp, (struct tcp_rack *)tp->t_fb_ptr, 5241 ((struct tcp_rack *)tp->t_fb_ptr)->r_ctl.rc_rcvtime, __LINE__); 5242 tp->t_flags |= TF_DELACK; 5243 } else { 5244 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output++; 5245 tp->t_flags |= TF_ACKNOW; 5246 } 5247 5248 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 5249 tp->t_flags |= TF_ECN_PERMIT; 5250 TCPSTAT_INC(tcps_ecn_shs); 5251 } 5252 if (SEQ_GT(th->th_ack, tp->snd_una)) { 5253 /* 5254 * We advance snd_una for the 5255 * fast open case. If th_ack is 5256 * acknowledging data beyond 5257 * snd_una we can't just call 5258 * ack-processing since the 5259 * data stream in our send-map 5260 * will start at snd_una + 1 (one 5261 * beyond the SYN). If its just 5262 * equal we don't need to do that 5263 * and there is no send_map. 5264 */ 5265 tp->snd_una++; 5266 } 5267 /* 5268 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 5269 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 5270 */ 5271 tp->t_starttime = ticks; 5272 if (tp->t_flags & TF_NEEDFIN) { 5273 tcp_state_change(tp, TCPS_FIN_WAIT_1); 5274 tp->t_flags &= ~TF_NEEDFIN; 5275 thflags &= ~TH_SYN; 5276 } else { 5277 tcp_state_change(tp, TCPS_ESTABLISHED); 5278 TCP_PROBE5(connect__established, NULL, tp, 5279 mtod(m, const char *), tp, th); 5280 cc_conn_init(tp); 5281 } 5282 } else { 5283 /* 5284 * Received initial SYN in SYN-SENT[*] state => simultaneous 5285 * open. If segment contains CC option and there is a 5286 * cached CC, apply TAO test. If it succeeds, connection is * 5287 * half-synchronized. Otherwise, do 3-way handshake: 5288 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 5289 * there was no CC option, clear cached CC value. 5290 */ 5291 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 5292 tcp_state_change(tp, TCPS_SYN_RECEIVED); 5293 } 5294 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 5295 INP_WLOCK_ASSERT(tp->t_inpcb); 5296 /* 5297 * Advance th->th_seq to correspond to first data byte. If data, 5298 * trim to stay within window, dropping FIN if necessary. 5299 */ 5300 th->th_seq++; 5301 if (tlen > tp->rcv_wnd) { 5302 todrop = tlen - tp->rcv_wnd; 5303 m_adj(m, -todrop); 5304 tlen = tp->rcv_wnd; 5305 thflags &= ~TH_FIN; 5306 TCPSTAT_INC(tcps_rcvpackafterwin); 5307 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 5308 } 5309 tp->snd_wl1 = th->th_seq - 1; 5310 tp->rcv_up = th->th_seq; 5311 /* 5312 * Client side of transaction: already sent SYN and data. If the 5313 * remote host used T/TCP to validate the SYN, our data will be 5314 * ACK'd; if so, enter normal data segment processing in the middle 5315 * of step 5, ack processing. Otherwise, goto step 6. 5316 */ 5317 if (thflags & TH_ACK) { 5318 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 5319 return (ret_val); 5320 /* We may have changed to FIN_WAIT_1 above */ 5321 if (tp->t_state == TCPS_FIN_WAIT_1) { 5322 /* 5323 * In FIN_WAIT_1 STATE in addition to the processing 5324 * for the ESTABLISHED state if our FIN is now 5325 * acknowledged then enter FIN_WAIT_2. 5326 */ 5327 if (ourfinisacked) { 5328 /* 5329 * If we can't receive any more data, then 5330 * closing user can proceed. Starting the 5331 * timer is contrary to the specification, 5332 * but if we don't get a FIN we'll hang 5333 * forever. 5334 * 5335 * XXXjl: we should release the tp also, and 5336 * use a compressed state. 5337 */ 5338 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5339 soisdisconnected(so); 5340 tcp_timer_activate(tp, TT_2MSL, 5341 (tcp_fast_finwait2_recycle ? 5342 tcp_finwait2_timeout : 5343 TP_MAXIDLE(tp))); 5344 } 5345 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5346 } 5347 } 5348 } 5349 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5350 tiwin, thflags, nxt_pkt)); 5351 } 5352 5353 /* 5354 * Return value of 1, the TCB is unlocked and most 5355 * likely gone, return value of 0, the TCP is still 5356 * locked. 5357 */ 5358 static int 5359 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 5360 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5361 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5362 { 5363 int32_t ret_val = 0; 5364 int32_t ourfinisacked = 0; 5365 5366 rack_calc_rwin(so, tp); 5367 5368 if ((thflags & TH_ACK) && 5369 (SEQ_LEQ(th->th_ack, tp->snd_una) || 5370 SEQ_GT(th->th_ack, tp->snd_max))) { 5371 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5372 return (1); 5373 } 5374 if (IS_FASTOPEN(tp->t_flags)) { 5375 /* 5376 * When a TFO connection is in SYN_RECEIVED, the 5377 * only valid packets are the initial SYN, a 5378 * retransmit/copy of the initial SYN (possibly with 5379 * a subset of the original data), a valid ACK, a 5380 * FIN, or a RST. 5381 */ 5382 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 5383 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5384 return (1); 5385 } else if (thflags & TH_SYN) { 5386 /* non-initial SYN is ignored */ 5387 struct tcp_rack *rack; 5388 5389 rack = (struct tcp_rack *)tp->t_fb_ptr; 5390 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 5391 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 5392 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 5393 rack_do_drop(m, NULL); 5394 return (0); 5395 } 5396 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 5397 rack_do_drop(m, NULL); 5398 return (0); 5399 } 5400 } 5401 if (thflags & TH_RST) 5402 return (rack_process_rst(m, th, so, tp)); 5403 /* 5404 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5405 * synchronized state. 5406 */ 5407 if (thflags & TH_SYN) { 5408 rack_challenge_ack(m, th, tp, &ret_val); 5409 return (ret_val); 5410 } 5411 /* 5412 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5413 * it's less than ts_recent, drop it. 5414 */ 5415 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5416 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5417 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5418 return (ret_val); 5419 } 5420 /* 5421 * In the SYN-RECEIVED state, validate that the packet belongs to 5422 * this connection before trimming the data to fit the receive 5423 * window. Check the sequence number versus IRS since we know the 5424 * sequence numbers haven't wrapped. This is a partial fix for the 5425 * "LAND" DoS attack. 5426 */ 5427 if (SEQ_LT(th->th_seq, tp->irs)) { 5428 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5429 return (1); 5430 } 5431 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5432 return (ret_val); 5433 } 5434 /* 5435 * If last ACK falls within this segment's sequence numbers, record 5436 * its timestamp. NOTE: 1) That the test incorporates suggestions 5437 * from the latest proposal of the tcplw@cray.com list (Braden 5438 * 1993/04/26). 2) That updating only on newer timestamps interferes 5439 * with our earlier PAWS tests, so this check should be solely 5440 * predicated on the sequence space of this segment. 3) That we 5441 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5442 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5443 * SEG.Len, This modified check allows us to overcome RFC1323's 5444 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5445 * p.869. In such cases, we can still calculate the RTT correctly 5446 * when RCV.NXT == Last.ACK.Sent. 5447 */ 5448 if ((to->to_flags & TOF_TS) != 0 && 5449 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5450 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5451 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5452 tp->ts_recent_age = tcp_ts_getticks(); 5453 tp->ts_recent = to->to_tsval; 5454 } 5455 /* 5456 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5457 * is on (half-synchronized state), then queue data for later 5458 * processing; else drop segment and return. 5459 */ 5460 if ((thflags & TH_ACK) == 0) { 5461 if (IS_FASTOPEN(tp->t_flags)) { 5462 tp->snd_wnd = tiwin; 5463 cc_conn_init(tp); 5464 } 5465 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5466 tiwin, thflags, nxt_pkt)); 5467 } 5468 TCPSTAT_INC(tcps_connects); 5469 soisconnected(so); 5470 /* Do window scaling? */ 5471 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 5472 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 5473 tp->rcv_scale = tp->request_r_scale; 5474 tp->snd_wnd = tiwin; 5475 } 5476 /* 5477 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 5478 * FIN-WAIT-1 5479 */ 5480 tp->t_starttime = ticks; 5481 if (tp->t_flags & TF_NEEDFIN) { 5482 tcp_state_change(tp, TCPS_FIN_WAIT_1); 5483 tp->t_flags &= ~TF_NEEDFIN; 5484 } else { 5485 tcp_state_change(tp, TCPS_ESTABLISHED); 5486 TCP_PROBE5(accept__established, NULL, tp, 5487 mtod(m, const char *), tp, th); 5488 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 5489 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 5490 tp->t_tfo_pending = NULL; 5491 5492 /* 5493 * Account for the ACK of our SYN prior to regular 5494 * ACK processing below. 5495 */ 5496 tp->snd_una++; 5497 } 5498 /* 5499 * TFO connections call cc_conn_init() during SYN 5500 * processing. Calling it again here for such connections 5501 * is not harmless as it would undo the snd_cwnd reduction 5502 * that occurs when a TFO SYN|ACK is retransmitted. 5503 */ 5504 if (!IS_FASTOPEN(tp->t_flags)) 5505 cc_conn_init(tp); 5506 } 5507 /* 5508 * If segment contains data or ACK, will call tcp_reass() later; if 5509 * not, do so now to pass queued data to user. 5510 */ 5511 if (tlen == 0 && (thflags & TH_FIN) == 0) 5512 (void)tcp_reass(tp, (struct tcphdr *)0, 0, 5513 (struct mbuf *)0); 5514 tp->snd_wl1 = th->th_seq - 1; 5515 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5516 return (ret_val); 5517 } 5518 if (tp->t_state == TCPS_FIN_WAIT_1) { 5519 /* We could have went to FIN_WAIT_1 (or EST) above */ 5520 /* 5521 * In FIN_WAIT_1 STATE in addition to the processing for the 5522 * ESTABLISHED state if our FIN is now acknowledged then 5523 * enter FIN_WAIT_2. 5524 */ 5525 if (ourfinisacked) { 5526 /* 5527 * If we can't receive any more data, then closing 5528 * user can proceed. Starting the timer is contrary 5529 * to the specification, but if we don't get a FIN 5530 * we'll hang forever. 5531 * 5532 * XXXjl: we should release the tp also, and use a 5533 * compressed state. 5534 */ 5535 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5536 soisdisconnected(so); 5537 tcp_timer_activate(tp, TT_2MSL, 5538 (tcp_fast_finwait2_recycle ? 5539 tcp_finwait2_timeout : 5540 TP_MAXIDLE(tp))); 5541 } 5542 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5543 } 5544 } 5545 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5546 tiwin, thflags, nxt_pkt)); 5547 } 5548 5549 /* 5550 * Return value of 1, the TCB is unlocked and most 5551 * likely gone, return value of 0, the TCP is still 5552 * locked. 5553 */ 5554 static int 5555 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 5556 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5557 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5558 { 5559 int32_t ret_val = 0; 5560 5561 /* 5562 * Header prediction: check for the two common cases of a 5563 * uni-directional data xfer. If the packet has no control flags, 5564 * is in-sequence, the window didn't change and we're not 5565 * retransmitting, it's a candidate. If the length is zero and the 5566 * ack moved forward, we're the sender side of the xfer. Just free 5567 * the data acked & wake any higher level process that was blocked 5568 * waiting for space. If the length is non-zero and the ack didn't 5569 * move, we're the receiver side. If we're getting packets in-order 5570 * (the reassembly queue is empty), add the data toc The socket 5571 * buffer and note that we need a delayed ack. Make sure that the 5572 * hidden state-flags are also off. Since we check for 5573 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 5574 */ 5575 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 5576 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 5577 __predict_true(LIST_EMPTY(&tp->t_segq)) && 5578 __predict_true(th->th_seq == tp->rcv_nxt)) { 5579 struct tcp_rack *rack; 5580 5581 rack = (struct tcp_rack *)tp->t_fb_ptr; 5582 if (tlen == 0) { 5583 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 5584 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) { 5585 return (0); 5586 } 5587 } else { 5588 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 5589 tiwin, nxt_pkt)) { 5590 return (0); 5591 } 5592 } 5593 } 5594 rack_calc_rwin(so, tp); 5595 5596 if (thflags & TH_RST) 5597 return (rack_process_rst(m, th, so, tp)); 5598 5599 /* 5600 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5601 * synchronized state. 5602 */ 5603 if (thflags & TH_SYN) { 5604 rack_challenge_ack(m, th, tp, &ret_val); 5605 return (ret_val); 5606 } 5607 /* 5608 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5609 * it's less than ts_recent, drop it. 5610 */ 5611 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5612 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5613 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5614 return (ret_val); 5615 } 5616 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5617 return (ret_val); 5618 } 5619 /* 5620 * If last ACK falls within this segment's sequence numbers, record 5621 * its timestamp. NOTE: 1) That the test incorporates suggestions 5622 * from the latest proposal of the tcplw@cray.com list (Braden 5623 * 1993/04/26). 2) That updating only on newer timestamps interferes 5624 * with our earlier PAWS tests, so this check should be solely 5625 * predicated on the sequence space of this segment. 3) That we 5626 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5627 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5628 * SEG.Len, This modified check allows us to overcome RFC1323's 5629 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5630 * p.869. In such cases, we can still calculate the RTT correctly 5631 * when RCV.NXT == Last.ACK.Sent. 5632 */ 5633 if ((to->to_flags & TOF_TS) != 0 && 5634 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5635 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5636 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5637 tp->ts_recent_age = tcp_ts_getticks(); 5638 tp->ts_recent = to->to_tsval; 5639 } 5640 /* 5641 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5642 * is on (half-synchronized state), then queue data for later 5643 * processing; else drop segment and return. 5644 */ 5645 if ((thflags & TH_ACK) == 0) { 5646 if (tp->t_flags & TF_NEEDSYN) { 5647 5648 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5649 tiwin, thflags, nxt_pkt)); 5650 5651 } else if (tp->t_flags & TF_ACKNOW) { 5652 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5653 return (ret_val); 5654 } else { 5655 rack_do_drop(m, NULL); 5656 return (0); 5657 } 5658 } 5659 /* 5660 * Ack processing. 5661 */ 5662 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 5663 return (ret_val); 5664 } 5665 if (sbavail(&so->so_snd)) { 5666 if (rack_progress_timeout_check(tp)) { 5667 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5668 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5669 return (1); 5670 } 5671 } 5672 /* State changes only happen in rack_process_data() */ 5673 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5674 tiwin, thflags, nxt_pkt)); 5675 } 5676 5677 /* 5678 * Return value of 1, the TCB is unlocked and most 5679 * likely gone, return value of 0, the TCP is still 5680 * locked. 5681 */ 5682 static int 5683 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 5684 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5685 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5686 { 5687 int32_t ret_val = 0; 5688 5689 rack_calc_rwin(so, tp); 5690 if (thflags & TH_RST) 5691 return (rack_process_rst(m, th, so, tp)); 5692 /* 5693 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5694 * synchronized state. 5695 */ 5696 if (thflags & TH_SYN) { 5697 rack_challenge_ack(m, th, tp, &ret_val); 5698 return (ret_val); 5699 } 5700 /* 5701 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5702 * it's less than ts_recent, drop it. 5703 */ 5704 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5705 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5706 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5707 return (ret_val); 5708 } 5709 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5710 return (ret_val); 5711 } 5712 /* 5713 * If last ACK falls within this segment's sequence numbers, record 5714 * its timestamp. NOTE: 1) That the test incorporates suggestions 5715 * from the latest proposal of the tcplw@cray.com list (Braden 5716 * 1993/04/26). 2) That updating only on newer timestamps interferes 5717 * with our earlier PAWS tests, so this check should be solely 5718 * predicated on the sequence space of this segment. 3) That we 5719 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5720 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5721 * SEG.Len, This modified check allows us to overcome RFC1323's 5722 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5723 * p.869. In such cases, we can still calculate the RTT correctly 5724 * when RCV.NXT == Last.ACK.Sent. 5725 */ 5726 if ((to->to_flags & TOF_TS) != 0 && 5727 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5728 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5729 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5730 tp->ts_recent_age = tcp_ts_getticks(); 5731 tp->ts_recent = to->to_tsval; 5732 } 5733 /* 5734 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5735 * is on (half-synchronized state), then queue data for later 5736 * processing; else drop segment and return. 5737 */ 5738 if ((thflags & TH_ACK) == 0) { 5739 if (tp->t_flags & TF_NEEDSYN) { 5740 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5741 tiwin, thflags, nxt_pkt)); 5742 5743 } else if (tp->t_flags & TF_ACKNOW) { 5744 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5745 return (ret_val); 5746 } else { 5747 rack_do_drop(m, NULL); 5748 return (0); 5749 } 5750 } 5751 /* 5752 * Ack processing. 5753 */ 5754 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 5755 return (ret_val); 5756 } 5757 if (sbavail(&so->so_snd)) { 5758 if (rack_progress_timeout_check(tp)) { 5759 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5760 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5761 return (1); 5762 } 5763 } 5764 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5765 tiwin, thflags, nxt_pkt)); 5766 } 5767 5768 static int 5769 rack_check_data_after_close(struct mbuf *m, 5770 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so) 5771 { 5772 struct tcp_rack *rack; 5773 5774 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 5775 rack = (struct tcp_rack *)tp->t_fb_ptr; 5776 if (rack->rc_allow_data_af_clo == 0) { 5777 close_now: 5778 tp = tcp_close(tp); 5779 TCPSTAT_INC(tcps_rcvafterclose); 5780 rack_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 5781 return (1); 5782 } 5783 if (sbavail(&so->so_snd) == 0) 5784 goto close_now; 5785 /* Ok we allow data that is ignored and a followup reset */ 5786 tp->rcv_nxt = th->th_seq + *tlen; 5787 tp->t_flags2 |= TF2_DROP_AF_DATA; 5788 rack->r_wanted_output = 1; 5789 *tlen = 0; 5790 return (0); 5791 } 5792 5793 /* 5794 * Return value of 1, the TCB is unlocked and most 5795 * likely gone, return value of 0, the TCP is still 5796 * locked. 5797 */ 5798 static int 5799 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 5800 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5801 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5802 { 5803 int32_t ret_val = 0; 5804 int32_t ourfinisacked = 0; 5805 5806 rack_calc_rwin(so, tp); 5807 5808 if (thflags & TH_RST) 5809 return (rack_process_rst(m, th, so, tp)); 5810 /* 5811 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5812 * synchronized state. 5813 */ 5814 if (thflags & TH_SYN) { 5815 rack_challenge_ack(m, th, tp, &ret_val); 5816 return (ret_val); 5817 } 5818 /* 5819 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5820 * it's less than ts_recent, drop it. 5821 */ 5822 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5823 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5824 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5825 return (ret_val); 5826 } 5827 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5828 return (ret_val); 5829 } 5830 /* 5831 * If new data are received on a connection after the user processes 5832 * are gone, then RST the other end. 5833 */ 5834 if ((so->so_state & SS_NOFDREF) && tlen) { 5835 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 5836 return (1); 5837 } 5838 /* 5839 * If last ACK falls within this segment's sequence numbers, record 5840 * its timestamp. NOTE: 1) That the test incorporates suggestions 5841 * from the latest proposal of the tcplw@cray.com list (Braden 5842 * 1993/04/26). 2) That updating only on newer timestamps interferes 5843 * with our earlier PAWS tests, so this check should be solely 5844 * predicated on the sequence space of this segment. 3) That we 5845 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5846 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5847 * SEG.Len, This modified check allows us to overcome RFC1323's 5848 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5849 * p.869. In such cases, we can still calculate the RTT correctly 5850 * when RCV.NXT == Last.ACK.Sent. 5851 */ 5852 if ((to->to_flags & TOF_TS) != 0 && 5853 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5854 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5855 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5856 tp->ts_recent_age = tcp_ts_getticks(); 5857 tp->ts_recent = to->to_tsval; 5858 } 5859 /* 5860 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5861 * is on (half-synchronized state), then queue data for later 5862 * processing; else drop segment and return. 5863 */ 5864 if ((thflags & TH_ACK) == 0) { 5865 if (tp->t_flags & TF_NEEDSYN) { 5866 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5867 tiwin, thflags, nxt_pkt)); 5868 } else if (tp->t_flags & TF_ACKNOW) { 5869 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5870 return (ret_val); 5871 } else { 5872 rack_do_drop(m, NULL); 5873 return (0); 5874 } 5875 } 5876 /* 5877 * Ack processing. 5878 */ 5879 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5880 return (ret_val); 5881 } 5882 if (ourfinisacked) { 5883 /* 5884 * If we can't receive any more data, then closing user can 5885 * proceed. Starting the timer is contrary to the 5886 * specification, but if we don't get a FIN we'll hang 5887 * forever. 5888 * 5889 * XXXjl: we should release the tp also, and use a 5890 * compressed state. 5891 */ 5892 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5893 soisdisconnected(so); 5894 tcp_timer_activate(tp, TT_2MSL, 5895 (tcp_fast_finwait2_recycle ? 5896 tcp_finwait2_timeout : 5897 TP_MAXIDLE(tp))); 5898 } 5899 tcp_state_change(tp, TCPS_FIN_WAIT_2); 5900 } 5901 if (sbavail(&so->so_snd)) { 5902 if (rack_progress_timeout_check(tp)) { 5903 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 5904 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 5905 return (1); 5906 } 5907 } 5908 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5909 tiwin, thflags, nxt_pkt)); 5910 } 5911 5912 /* 5913 * Return value of 1, the TCB is unlocked and most 5914 * likely gone, return value of 0, the TCP is still 5915 * locked. 5916 */ 5917 static int 5918 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 5919 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 5920 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 5921 { 5922 int32_t ret_val = 0; 5923 int32_t ourfinisacked = 0; 5924 5925 rack_calc_rwin(so, tp); 5926 5927 if (thflags & TH_RST) 5928 return (rack_process_rst(m, th, so, tp)); 5929 /* 5930 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 5931 * synchronized state. 5932 */ 5933 if (thflags & TH_SYN) { 5934 rack_challenge_ack(m, th, tp, &ret_val); 5935 return (ret_val); 5936 } 5937 /* 5938 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 5939 * it's less than ts_recent, drop it. 5940 */ 5941 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 5942 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 5943 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 5944 return (ret_val); 5945 } 5946 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 5947 return (ret_val); 5948 } 5949 /* 5950 * If new data are received on a connection after the user processes 5951 * are gone, then RST the other end. 5952 */ 5953 if ((so->so_state & SS_NOFDREF) && tlen) { 5954 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 5955 return (1); 5956 } 5957 /* 5958 * If last ACK falls within this segment's sequence numbers, record 5959 * its timestamp. NOTE: 1) That the test incorporates suggestions 5960 * from the latest proposal of the tcplw@cray.com list (Braden 5961 * 1993/04/26). 2) That updating only on newer timestamps interferes 5962 * with our earlier PAWS tests, so this check should be solely 5963 * predicated on the sequence space of this segment. 3) That we 5964 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 5965 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 5966 * SEG.Len, This modified check allows us to overcome RFC1323's 5967 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 5968 * p.869. In such cases, we can still calculate the RTT correctly 5969 * when RCV.NXT == Last.ACK.Sent. 5970 */ 5971 if ((to->to_flags & TOF_TS) != 0 && 5972 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 5973 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 5974 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 5975 tp->ts_recent_age = tcp_ts_getticks(); 5976 tp->ts_recent = to->to_tsval; 5977 } 5978 /* 5979 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 5980 * is on (half-synchronized state), then queue data for later 5981 * processing; else drop segment and return. 5982 */ 5983 if ((thflags & TH_ACK) == 0) { 5984 if (tp->t_flags & TF_NEEDSYN) { 5985 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 5986 tiwin, thflags, nxt_pkt)); 5987 } else if (tp->t_flags & TF_ACKNOW) { 5988 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 5989 return (ret_val); 5990 } else { 5991 rack_do_drop(m, NULL); 5992 return (0); 5993 } 5994 } 5995 /* 5996 * Ack processing. 5997 */ 5998 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 5999 return (ret_val); 6000 } 6001 if (ourfinisacked) { 6002 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 6003 tcp_twstart(tp); 6004 m_freem(m); 6005 return (1); 6006 } 6007 if (sbavail(&so->so_snd)) { 6008 if (rack_progress_timeout_check(tp)) { 6009 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6010 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6011 return (1); 6012 } 6013 } 6014 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6015 tiwin, thflags, nxt_pkt)); 6016 } 6017 6018 /* 6019 * Return value of 1, the TCB is unlocked and most 6020 * likely gone, return value of 0, the TCP is still 6021 * locked. 6022 */ 6023 static int 6024 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 6025 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 6026 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 6027 { 6028 int32_t ret_val = 0; 6029 int32_t ourfinisacked = 0; 6030 6031 rack_calc_rwin(so, tp); 6032 6033 if (thflags & TH_RST) 6034 return (rack_process_rst(m, th, so, tp)); 6035 /* 6036 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 6037 * synchronized state. 6038 */ 6039 if (thflags & TH_SYN) { 6040 rack_challenge_ack(m, th, tp, &ret_val); 6041 return (ret_val); 6042 } 6043 /* 6044 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 6045 * it's less than ts_recent, drop it. 6046 */ 6047 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 6048 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 6049 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 6050 return (ret_val); 6051 } 6052 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 6053 return (ret_val); 6054 } 6055 /* 6056 * If new data are received on a connection after the user processes 6057 * are gone, then RST the other end. 6058 */ 6059 if ((so->so_state & SS_NOFDREF) && tlen) { 6060 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 6061 return (1); 6062 } 6063 /* 6064 * If last ACK falls within this segment's sequence numbers, record 6065 * its timestamp. NOTE: 1) That the test incorporates suggestions 6066 * from the latest proposal of the tcplw@cray.com list (Braden 6067 * 1993/04/26). 2) That updating only on newer timestamps interferes 6068 * with our earlier PAWS tests, so this check should be solely 6069 * predicated on the sequence space of this segment. 3) That we 6070 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 6071 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 6072 * SEG.Len, This modified check allows us to overcome RFC1323's 6073 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 6074 * p.869. In such cases, we can still calculate the RTT correctly 6075 * when RCV.NXT == Last.ACK.Sent. 6076 */ 6077 if ((to->to_flags & TOF_TS) != 0 && 6078 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 6079 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 6080 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 6081 tp->ts_recent_age = tcp_ts_getticks(); 6082 tp->ts_recent = to->to_tsval; 6083 } 6084 /* 6085 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 6086 * is on (half-synchronized state), then queue data for later 6087 * processing; else drop segment and return. 6088 */ 6089 if ((thflags & TH_ACK) == 0) { 6090 if (tp->t_flags & TF_NEEDSYN) { 6091 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6092 tiwin, thflags, nxt_pkt)); 6093 } else if (tp->t_flags & TF_ACKNOW) { 6094 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 6095 return (ret_val); 6096 } else { 6097 rack_do_drop(m, NULL); 6098 return (0); 6099 } 6100 } 6101 /* 6102 * case TCPS_LAST_ACK: Ack processing. 6103 */ 6104 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 6105 return (ret_val); 6106 } 6107 if (ourfinisacked) { 6108 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 6109 tp = tcp_close(tp); 6110 rack_do_drop(m, tp); 6111 return (1); 6112 } 6113 if (sbavail(&so->so_snd)) { 6114 if (rack_progress_timeout_check(tp)) { 6115 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6116 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6117 return (1); 6118 } 6119 } 6120 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6121 tiwin, thflags, nxt_pkt)); 6122 } 6123 6124 6125 /* 6126 * Return value of 1, the TCB is unlocked and most 6127 * likely gone, return value of 0, the TCP is still 6128 * locked. 6129 */ 6130 static int 6131 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 6132 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 6133 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 6134 { 6135 int32_t ret_val = 0; 6136 int32_t ourfinisacked = 0; 6137 6138 rack_calc_rwin(so, tp); 6139 6140 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 6141 if (thflags & TH_RST) 6142 return (rack_process_rst(m, th, so, tp)); 6143 /* 6144 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 6145 * synchronized state. 6146 */ 6147 if (thflags & TH_SYN) { 6148 rack_challenge_ack(m, th, tp, &ret_val); 6149 return (ret_val); 6150 } 6151 /* 6152 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 6153 * it's less than ts_recent, drop it. 6154 */ 6155 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 6156 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 6157 if (rack_ts_check(m, th, tp, tlen, thflags, &ret_val)) 6158 return (ret_val); 6159 } 6160 if (rack_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 6161 return (ret_val); 6162 } 6163 /* 6164 * If new data are received on a connection after the user processes 6165 * are gone, then RST the other end. 6166 */ 6167 if ((so->so_state & SS_NOFDREF) && 6168 tlen) { 6169 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 6170 return (1); 6171 } 6172 /* 6173 * If last ACK falls within this segment's sequence numbers, record 6174 * its timestamp. NOTE: 1) That the test incorporates suggestions 6175 * from the latest proposal of the tcplw@cray.com list (Braden 6176 * 1993/04/26). 2) That updating only on newer timestamps interferes 6177 * with our earlier PAWS tests, so this check should be solely 6178 * predicated on the sequence space of this segment. 3) That we 6179 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 6180 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 6181 * SEG.Len, This modified check allows us to overcome RFC1323's 6182 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 6183 * p.869. In such cases, we can still calculate the RTT correctly 6184 * when RCV.NXT == Last.ACK.Sent. 6185 */ 6186 if ((to->to_flags & TOF_TS) != 0 && 6187 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 6188 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 6189 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 6190 tp->ts_recent_age = tcp_ts_getticks(); 6191 tp->ts_recent = to->to_tsval; 6192 } 6193 /* 6194 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 6195 * is on (half-synchronized state), then queue data for later 6196 * processing; else drop segment and return. 6197 */ 6198 if ((thflags & TH_ACK) == 0) { 6199 if (tp->t_flags & TF_NEEDSYN) { 6200 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6201 tiwin, thflags, nxt_pkt)); 6202 } else if (tp->t_flags & TF_ACKNOW) { 6203 rack_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 6204 return (ret_val); 6205 } else { 6206 rack_do_drop(m, NULL); 6207 return (0); 6208 } 6209 } 6210 /* 6211 * Ack processing. 6212 */ 6213 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 6214 return (ret_val); 6215 } 6216 if (sbavail(&so->so_snd)) { 6217 if (rack_progress_timeout_check(tp)) { 6218 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6219 rack_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 6220 return (1); 6221 } 6222 } 6223 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 6224 tiwin, thflags, nxt_pkt)); 6225 } 6226 6227 6228 static void inline 6229 rack_clear_rate_sample(struct tcp_rack *rack) 6230 { 6231 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY; 6232 rack->r_ctl.rack_rs.rs_rtt_cnt = 0; 6233 rack->r_ctl.rack_rs.rs_rtt_tot = 0; 6234 } 6235 6236 static int 6237 rack_init(struct tcpcb *tp) 6238 { 6239 struct tcp_rack *rack = NULL; 6240 6241 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT); 6242 if (tp->t_fb_ptr == NULL) { 6243 /* 6244 * We need to allocate memory but cant. The INP and INP_INFO 6245 * locks and they are recusive (happens during setup. So a 6246 * scheme to drop the locks fails :( 6247 * 6248 */ 6249 return (ENOMEM); 6250 } 6251 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack)); 6252 6253 rack = (struct tcp_rack *)tp->t_fb_ptr; 6254 TAILQ_INIT(&rack->r_ctl.rc_map); 6255 TAILQ_INIT(&rack->r_ctl.rc_free); 6256 TAILQ_INIT(&rack->r_ctl.rc_tmap); 6257 rack->rc_tp = tp; 6258 if (tp->t_inpcb) { 6259 rack->rc_inp = tp->t_inpcb; 6260 } 6261 /* Probably not needed but lets be sure */ 6262 rack_clear_rate_sample(rack); 6263 rack->r_cpu = 0; 6264 rack->r_ctl.rc_reorder_fade = rack_reorder_fade; 6265 rack->rc_allow_data_af_clo = rack_ignore_data_after_close; 6266 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh; 6267 rack->rc_pace_reduce = rack_slot_reduction; 6268 if (V_tcp_delack_enabled) 6269 tp->t_delayed_ack = 1; 6270 else 6271 tp->t_delayed_ack = 0; 6272 rack->rc_pace_max_segs = rack_hptsi_segments; 6273 rack->r_ctl.rc_early_recovery_segs = rack_early_recovery_max_seg; 6274 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh; 6275 rack->r_ctl.rc_pkt_delay = rack_pkt_delay; 6276 rack->r_ctl.rc_prop_reduce = rack_use_proportional_reduce; 6277 rack->r_idle_reduce_largest = rack_reduce_largest_on_idle; 6278 rack->r_enforce_min_pace = rack_min_pace_time; 6279 rack->r_min_pace_seg_thresh = rack_min_pace_time_seg_req; 6280 rack->r_ctl.rc_prop_rate = rack_proportional_rate; 6281 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp; 6282 rack->r_ctl.rc_early_recovery = rack_early_recovery; 6283 rack->rc_always_pace = rack_pace_every_seg; 6284 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method; 6285 rack->rack_tlp_threshold_use = rack_tlp_threshold_use; 6286 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr; 6287 rack->r_ctl.rc_min_to = rack_min_to; 6288 rack->r_ctl.rc_prr_inc_var = rack_inc_var; 6289 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6290 if (tp->snd_una != tp->snd_max) { 6291 /* Create a send map for the current outstanding data */ 6292 struct rack_sendmap *rsm; 6293 6294 rsm = rack_alloc(rack); 6295 if (rsm == NULL) { 6296 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 6297 tp->t_fb_ptr = NULL; 6298 return (ENOMEM); 6299 } 6300 rsm->r_flags = RACK_OVERMAX; 6301 rsm->r_tim_lastsent[0] = tcp_ts_getticks(); 6302 rsm->r_rtr_cnt = 1; 6303 rsm->r_rtr_bytes = 0; 6304 rsm->r_start = tp->snd_una; 6305 rsm->r_end = tp->snd_max; 6306 rsm->r_sndcnt = 0; 6307 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_map, rsm, r_next); 6308 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 6309 rsm->r_in_tmap = 1; 6310 } 6311 return (0); 6312 } 6313 6314 static int 6315 rack_handoff_ok(struct tcpcb *tp) 6316 { 6317 if ((tp->t_state == TCPS_CLOSED) || 6318 (tp->t_state == TCPS_LISTEN)) { 6319 /* Sure no problem though it may not stick */ 6320 return (0); 6321 } 6322 if ((tp->t_state == TCPS_SYN_SENT) || 6323 (tp->t_state == TCPS_SYN_RECEIVED)) { 6324 /* 6325 * We really don't know you have to get to ESTAB or beyond 6326 * to tell. 6327 */ 6328 return (EAGAIN); 6329 } 6330 if (tp->t_flags & TF_SACK_PERMIT) { 6331 return (0); 6332 } 6333 /* 6334 * If we reach here we don't do SACK on this connection so we can 6335 * never do rack. 6336 */ 6337 return (EINVAL); 6338 } 6339 6340 static void 6341 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged) 6342 { 6343 if (tp->t_fb_ptr) { 6344 struct tcp_rack *rack; 6345 struct rack_sendmap *rsm; 6346 6347 rack = (struct tcp_rack *)tp->t_fb_ptr; 6348 #ifdef TCP_BLACKBOX 6349 tcp_log_flowend(tp); 6350 #endif 6351 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 6352 while (rsm) { 6353 TAILQ_REMOVE(&rack->r_ctl.rc_map, rsm, r_next); 6354 uma_zfree(rack_zone, rsm); 6355 rsm = TAILQ_FIRST(&rack->r_ctl.rc_map); 6356 } 6357 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 6358 while (rsm) { 6359 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_next); 6360 uma_zfree(rack_zone, rsm); 6361 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 6362 } 6363 rack->rc_free_cnt = 0; 6364 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 6365 tp->t_fb_ptr = NULL; 6366 } 6367 } 6368 6369 static void 6370 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack) 6371 { 6372 switch (tp->t_state) { 6373 case TCPS_SYN_SENT: 6374 rack->r_state = TCPS_SYN_SENT; 6375 rack->r_substate = rack_do_syn_sent; 6376 break; 6377 case TCPS_SYN_RECEIVED: 6378 rack->r_state = TCPS_SYN_RECEIVED; 6379 rack->r_substate = rack_do_syn_recv; 6380 break; 6381 case TCPS_ESTABLISHED: 6382 rack->r_state = TCPS_ESTABLISHED; 6383 rack->r_substate = rack_do_established; 6384 break; 6385 case TCPS_CLOSE_WAIT: 6386 rack->r_state = TCPS_CLOSE_WAIT; 6387 rack->r_substate = rack_do_close_wait; 6388 break; 6389 case TCPS_FIN_WAIT_1: 6390 rack->r_state = TCPS_FIN_WAIT_1; 6391 rack->r_substate = rack_do_fin_wait_1; 6392 break; 6393 case TCPS_CLOSING: 6394 rack->r_state = TCPS_CLOSING; 6395 rack->r_substate = rack_do_closing; 6396 break; 6397 case TCPS_LAST_ACK: 6398 rack->r_state = TCPS_LAST_ACK; 6399 rack->r_substate = rack_do_lastack; 6400 break; 6401 case TCPS_FIN_WAIT_2: 6402 rack->r_state = TCPS_FIN_WAIT_2; 6403 rack->r_substate = rack_do_fin_wait_2; 6404 break; 6405 case TCPS_LISTEN: 6406 case TCPS_CLOSED: 6407 case TCPS_TIME_WAIT: 6408 default: 6409 #ifdef INVARIANTS 6410 panic("tcp tp:%p state:%d sees impossible state?", tp, tp->t_state); 6411 #endif 6412 break; 6413 }; 6414 } 6415 6416 6417 static void 6418 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb) 6419 { 6420 /* 6421 * We received an ack, and then did not 6422 * call send or were bounced out due to the 6423 * hpts was running. Now a timer is up as well, is 6424 * it the right timer? 6425 */ 6426 struct rack_sendmap *rsm; 6427 int tmr_up; 6428 6429 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 6430 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 6431 return; 6432 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6433 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 6434 (tmr_up == PACE_TMR_RXT)) { 6435 /* Should be an RXT */ 6436 return; 6437 } 6438 if (rsm == NULL) { 6439 /* Nothing outstanding? */ 6440 if (tp->t_flags & TF_DELACK) { 6441 if (tmr_up == PACE_TMR_DELACK) 6442 /* We are supposed to have delayed ack up and we do */ 6443 return; 6444 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) { 6445 /* 6446 * if we hit enobufs then we would expect the possiblity 6447 * of nothing outstanding and the RXT up (and the hptsi timer). 6448 */ 6449 return; 6450 } else if (((tcp_always_keepalive || 6451 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 6452 (tp->t_state <= TCPS_CLOSING)) && 6453 (tmr_up == PACE_TMR_KEEP) && 6454 (tp->snd_max == tp->snd_una)) { 6455 /* We should have keep alive up and we do */ 6456 return; 6457 } 6458 } 6459 if (rsm && (rsm->r_flags & RACK_SACK_PASSED)) { 6460 if ((tp->t_flags & TF_SENTFIN) && 6461 ((tp->snd_max - tp->snd_una) == 1) && 6462 (rsm->r_flags & RACK_HAS_FIN)) { 6463 /* needs to be a RXT */ 6464 if (tmr_up == PACE_TMR_RXT) 6465 return; 6466 } else if (tmr_up == PACE_TMR_RACK) 6467 return; 6468 } else if (SEQ_GT(tp->snd_max,tp->snd_una) && 6469 ((tmr_up == PACE_TMR_TLP) || 6470 (tmr_up == PACE_TMR_RXT))) { 6471 /* 6472 * Either a TLP or RXT is fine if no sack-passed 6473 * is in place and data is outstanding. 6474 */ 6475 return; 6476 } else if (tmr_up == PACE_TMR_DELACK) { 6477 /* 6478 * If the delayed ack was going to go off 6479 * before the rtx/tlp/rack timer were going to 6480 * expire, then that would be the timer in control. 6481 * Note we don't check the time here trusting the 6482 * code is correct. 6483 */ 6484 return; 6485 } 6486 /* 6487 * Ok the timer originally started is not what we want now. 6488 * We will force the hpts to be stopped if any, and restart 6489 * with the slot set to what was in the saved slot. 6490 */ 6491 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 6492 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6493 } 6494 6495 static void 6496 rack_hpts_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 6497 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, 6498 int32_t nxt_pkt, struct timeval *tv) 6499 { 6500 int32_t thflags, retval, did_out = 0; 6501 int32_t way_out = 0; 6502 uint32_t cts; 6503 uint32_t tiwin; 6504 struct tcpopt to; 6505 struct tcp_rack *rack; 6506 struct rack_sendmap *rsm; 6507 int32_t prev_state = 0; 6508 6509 cts = tcp_tv_to_mssectick(tv); 6510 rack = (struct tcp_rack *)tp->t_fb_ptr; 6511 6512 kern_prefetch(rack, &prev_state); 6513 prev_state = 0; 6514 thflags = th->th_flags; 6515 /* 6516 * If this is either a state-changing packet or current state isn't 6517 * established, we require a read lock on tcbinfo. Otherwise, we 6518 * allow the tcbinfo to be in either locked or unlocked, as the 6519 * caller may have unnecessarily acquired a lock due to a race. 6520 */ 6521 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 6522 tp->t_state != TCPS_ESTABLISHED) { 6523 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 6524 } 6525 INP_WLOCK_ASSERT(tp->t_inpcb); 6526 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 6527 __func__)); 6528 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 6529 __func__)); 6530 { 6531 union tcp_log_stackspecific log; 6532 6533 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 6534 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 6535 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 6536 TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 6537 tlen, &log, true); 6538 } 6539 /* 6540 * Segment received on connection. Reset idle time and keep-alive 6541 * timer. XXX: This should be done after segment validation to 6542 * ignore broken/spoofed segs. 6543 */ 6544 if (tp->t_idle_reduce && (tp->snd_max == tp->snd_una)) { 6545 #ifdef NETFLIX_CWV 6546 if ((tp->cwv_enabled) && 6547 ((tp->cwv_cwnd_valid == 0) && 6548 TCPS_HAVEESTABLISHED(tp->t_state) && 6549 (tp->snd_cwnd > tp->snd_cwv.init_cwnd))) { 6550 tcp_newcwv_nvp_closedown(tp); 6551 } else 6552 #endif 6553 if ((ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 6554 counter_u64_add(rack_input_idle_reduces, 1); 6555 rack_cc_after_idle(tp, 6556 (rack->r_idle_reduce_largest ? 1 :0)); 6557 } 6558 } 6559 rack->r_ctl.rc_rcvtime = cts; 6560 tp->t_rcvtime = ticks; 6561 6562 #ifdef NETFLIX_CWV 6563 if (tp->cwv_enabled) { 6564 if ((tp->cwv_cwnd_valid == 0) && 6565 TCPS_HAVEESTABLISHED(tp->t_state) && 6566 (tp->snd_cwnd > tp->snd_cwv.init_cwnd)) 6567 tcp_newcwv_nvp_closedown(tp); 6568 } 6569 #endif 6570 /* 6571 * Unscale the window into a 32-bit value. For the SYN_SENT state 6572 * the scale is zero. 6573 */ 6574 tiwin = th->th_win << tp->snd_scale; 6575 #ifdef NETFLIX_STATS 6576 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 6577 #endif 6578 /* 6579 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move 6580 * this to occur after we've validated the segment. 6581 */ 6582 if (tp->t_flags & TF_ECN_PERMIT) { 6583 if (thflags & TH_CWR) 6584 tp->t_flags &= ~TF_ECN_SND_ECE; 6585 switch (iptos & IPTOS_ECN_MASK) { 6586 case IPTOS_ECN_CE: 6587 tp->t_flags |= TF_ECN_SND_ECE; 6588 TCPSTAT_INC(tcps_ecn_ce); 6589 break; 6590 case IPTOS_ECN_ECT0: 6591 TCPSTAT_INC(tcps_ecn_ect0); 6592 break; 6593 case IPTOS_ECN_ECT1: 6594 TCPSTAT_INC(tcps_ecn_ect1); 6595 break; 6596 } 6597 /* Congestion experienced. */ 6598 if (thflags & TH_ECE) { 6599 rack_cong_signal(tp, th, CC_ECN); 6600 } 6601 } 6602 /* 6603 * Parse options on any incoming segment. 6604 */ 6605 tcp_dooptions(&to, (u_char *)(th + 1), 6606 (th->th_off << 2) - sizeof(struct tcphdr), 6607 (thflags & TH_SYN) ? TO_SYN : 0); 6608 6609 /* 6610 * If echoed timestamp is later than the current time, fall back to 6611 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 6612 * were used when this connection was established. 6613 */ 6614 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 6615 to.to_tsecr -= tp->ts_offset; 6616 if (TSTMP_GT(to.to_tsecr, cts)) 6617 to.to_tsecr = 0; 6618 } 6619 /* 6620 * If its the first time in we need to take care of options and 6621 * verify we can do SACK for rack! 6622 */ 6623 if (rack->r_state == 0) { 6624 /* Should be init'd by rack_init() */ 6625 KASSERT(rack->rc_inp != NULL, 6626 ("%s: rack->rc_inp unexpectedly NULL", __func__)); 6627 if (rack->rc_inp == NULL) { 6628 rack->rc_inp = tp->t_inpcb; 6629 } 6630 6631 /* 6632 * Process options only when we get SYN/ACK back. The SYN 6633 * case for incoming connections is handled in tcp_syncache. 6634 * According to RFC1323 the window field in a SYN (i.e., a 6635 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 6636 * this is traditional behavior, may need to be cleaned up. 6637 */ 6638 rack->r_cpu = inp_to_cpuid(tp->t_inpcb); 6639 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 6640 if ((to.to_flags & TOF_SCALE) && 6641 (tp->t_flags & TF_REQ_SCALE)) { 6642 tp->t_flags |= TF_RCVD_SCALE; 6643 tp->snd_scale = to.to_wscale; 6644 } 6645 /* 6646 * Initial send window. It will be updated with the 6647 * next incoming segment to the scaled value. 6648 */ 6649 tp->snd_wnd = th->th_win; 6650 if (to.to_flags & TOF_TS) { 6651 tp->t_flags |= TF_RCVD_TSTMP; 6652 tp->ts_recent = to.to_tsval; 6653 tp->ts_recent_age = cts; 6654 } 6655 if (to.to_flags & TOF_MSS) 6656 tcp_mss(tp, to.to_mss); 6657 if ((tp->t_flags & TF_SACK_PERMIT) && 6658 (to.to_flags & TOF_SACKPERM) == 0) 6659 tp->t_flags &= ~TF_SACK_PERMIT; 6660 if (IS_FASTOPEN(tp->t_flags)) { 6661 if (to.to_flags & TOF_FASTOPEN) { 6662 uint16_t mss; 6663 6664 if (to.to_flags & TOF_MSS) 6665 mss = to.to_mss; 6666 else 6667 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 6668 mss = TCP6_MSS; 6669 else 6670 mss = TCP_MSS; 6671 tcp_fastopen_update_cache(tp, mss, 6672 to.to_tfo_len, to.to_tfo_cookie); 6673 } else 6674 tcp_fastopen_disable_path(tp); 6675 } 6676 } 6677 /* 6678 * At this point we are at the initial call. Here we decide 6679 * if we are doing RACK or not. We do this by seeing if 6680 * TF_SACK_PERMIT is set, if not rack is *not* possible and 6681 * we switch to the default code. 6682 */ 6683 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 6684 tcp_switch_back_to_default(tp); 6685 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen, 6686 tlen, iptos); 6687 return; 6688 } 6689 /* Set the flag */ 6690 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 6691 tcp_set_hpts(tp->t_inpcb); 6692 rack_stop_all_timers(tp); 6693 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack); 6694 } 6695 /* 6696 * This is the one exception case where we set the rack state 6697 * always. All other times (timers etc) we must have a rack-state 6698 * set (so we assure we have done the checks above for SACK). 6699 */ 6700 if (rack->r_state != tp->t_state) 6701 rack_set_state(tp, rack); 6702 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&rack->r_ctl.rc_map)) != NULL) 6703 kern_prefetch(rsm, &prev_state); 6704 prev_state = rack->r_state; 6705 rack->r_ctl.rc_tlp_send_cnt = 0; 6706 rack_clear_rate_sample(rack); 6707 retval = (*rack->r_substate) (m, th, so, 6708 tp, &to, drop_hdrlen, 6709 tlen, tiwin, thflags, nxt_pkt); 6710 #ifdef INVARIANTS 6711 if ((retval == 0) && 6712 (tp->t_inpcb == NULL)) { 6713 panic("retval:%d tp:%p t_inpcb:NULL state:%d", 6714 retval, tp, prev_state); 6715 } 6716 #endif 6717 if (retval == 0) { 6718 /* 6719 * If retval is 1 the tcb is unlocked and most likely the tp 6720 * is gone. 6721 */ 6722 INP_WLOCK_ASSERT(tp->t_inpcb); 6723 tcp_rack_xmit_timer_commit(rack, tp); 6724 if (((tp->snd_max - tp->snd_una) > tp->snd_wnd) && 6725 (rack->rc_in_persist == 0)){ 6726 /* 6727 * The peer shrunk its window on us to the point 6728 * where we have sent too much. The only thing 6729 * we can do here is stop any timers and 6730 * enter persist. We most likely lost the last 6731 * bytes we sent but oh well, we will have to 6732 * retransmit them after the peer is caught up. 6733 */ 6734 if (rack->rc_inp->inp_in_hpts) 6735 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 6736 rack_timer_cancel(tp, rack, cts, __LINE__); 6737 rack_enter_persist(tp, rack, cts); 6738 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6739 way_out = 3; 6740 goto done_with_input; 6741 } 6742 if (nxt_pkt == 0) { 6743 if (rack->r_wanted_output != 0) { 6744 did_out = 1; 6745 (void)tp->t_fb->tfb_tcp_output(tp); 6746 } 6747 rack_start_hpts_timer(rack, tp, cts, __LINE__, 0, 0, 0); 6748 } 6749 if (((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 6750 (SEQ_GT(tp->snd_max, tp->snd_una) || 6751 (tp->t_flags & TF_DELACK) || 6752 ((tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 6753 (tp->t_state <= TCPS_CLOSING)))) { 6754 /* We could not send (probably in the hpts but stopped the timer earlier)? */ 6755 if ((tp->snd_max == tp->snd_una) && 6756 ((tp->t_flags & TF_DELACK) == 0) && 6757 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 6758 /* keep alive not needed if we are hptsi output yet */ 6759 ; 6760 } else { 6761 if (rack->rc_inp->inp_in_hpts) 6762 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 6763 rack_start_hpts_timer(rack, tp, tcp_ts_getticks(), __LINE__, 0, 0, 0); 6764 } 6765 way_out = 1; 6766 } else { 6767 /* Do we have the correct timer running? */ 6768 rack_timer_audit(tp, rack, &so->so_snd); 6769 way_out = 2; 6770 } 6771 done_with_input: 6772 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out); 6773 if (did_out) 6774 rack->r_wanted_output = 0; 6775 #ifdef INVARIANTS 6776 if (tp->t_inpcb == NULL) { 6777 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d", 6778 did_out, 6779 retval, tp, prev_state); 6780 } 6781 #endif 6782 INP_WUNLOCK(tp->t_inpcb); 6783 } 6784 } 6785 6786 void 6787 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 6788 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 6789 { 6790 struct timeval tv; 6791 #ifdef RSS 6792 struct tcp_function_block *tfb; 6793 struct tcp_rack *rack; 6794 struct epoch_tracker et; 6795 6796 rack = (struct tcp_rack *)tp->t_fb_ptr; 6797 if (rack->r_state == 0) { 6798 /* 6799 * Initial input (ACK to SYN-ACK etc)lets go ahead and get 6800 * it processed 6801 */ 6802 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 6803 tcp_get_usecs(&tv); 6804 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen, 6805 tlen, iptos, 0, &tv); 6806 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 6807 return; 6808 } 6809 tcp_queue_to_input(tp, m, th, tlen, drop_hdrlen, iptos); 6810 INP_WUNLOCK(tp->t_inpcb); 6811 #else 6812 tcp_get_usecs(&tv); 6813 rack_hpts_do_segment(m, th, so, tp, drop_hdrlen, 6814 tlen, iptos, 0, &tv); 6815 #endif 6816 } 6817 6818 struct rack_sendmap * 6819 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused) 6820 { 6821 struct rack_sendmap *rsm = NULL; 6822 int32_t idx; 6823 uint32_t srtt_cur, srtt = 0, thresh = 0, ts_low = 0; 6824 6825 /* Return the next guy to be re-transmitted */ 6826 if (TAILQ_EMPTY(&rack->r_ctl.rc_map)) { 6827 return (NULL); 6828 } 6829 if (tp->t_flags & TF_SENTFIN) { 6830 /* retran the end FIN? */ 6831 return (NULL); 6832 } 6833 /* ok lets look at this one */ 6834 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6835 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) { 6836 goto check_it; 6837 } 6838 rsm = rack_find_lowest_rsm(rack); 6839 if (rsm == NULL) { 6840 return (NULL); 6841 } 6842 check_it: 6843 srtt_cur = tp->t_srtt >> TCP_RTT_SHIFT; 6844 srtt = TICKS_2_MSEC(srtt_cur); 6845 if (rack->rc_rack_rtt && (srtt > rack->rc_rack_rtt)) 6846 srtt = rack->rc_rack_rtt; 6847 if (rsm->r_flags & RACK_ACKED) { 6848 return (NULL); 6849 } 6850 if ((rsm->r_flags & RACK_SACK_PASSED) == 0) { 6851 /* Its not yet ready */ 6852 return (NULL); 6853 } 6854 idx = rsm->r_rtr_cnt - 1; 6855 ts_low = rsm->r_tim_lastsent[idx]; 6856 thresh = rack_calc_thresh_rack(rack, srtt, tsused); 6857 if (tsused <= ts_low) { 6858 return (NULL); 6859 } 6860 if ((tsused - ts_low) >= thresh) { 6861 return (rsm); 6862 } 6863 return (NULL); 6864 } 6865 6866 static int 6867 rack_output(struct tcpcb *tp) 6868 { 6869 struct socket *so; 6870 uint32_t recwin, sendwin; 6871 uint32_t sb_offset; 6872 int32_t len, flags, error = 0; 6873 struct mbuf *m; 6874 struct mbuf *mb; 6875 uint32_t if_hw_tsomaxsegcount = 0; 6876 uint32_t if_hw_tsomaxsegsize; 6877 long tot_len_this_send = 0; 6878 struct ip *ip = NULL; 6879 #ifdef TCPDEBUG 6880 struct ipovly *ipov = NULL; 6881 #endif 6882 struct udphdr *udp = NULL; 6883 struct tcp_rack *rack; 6884 struct tcphdr *th; 6885 uint8_t pass = 0; 6886 uint8_t wanted_cookie = 0; 6887 u_char opt[TCP_MAXOLEN]; 6888 unsigned ipoptlen, optlen, hdrlen, ulen=0; 6889 uint32_t rack_seq; 6890 6891 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 6892 unsigned ipsec_optlen = 0; 6893 6894 #endif 6895 int32_t idle, sendalot; 6896 int32_t sub_from_prr = 0; 6897 volatile int32_t sack_rxmit; 6898 struct rack_sendmap *rsm = NULL; 6899 int32_t tso, mtu, would_have_fin = 0; 6900 struct tcpopt to; 6901 int32_t slot = 0; 6902 uint32_t cts; 6903 uint8_t hpts_calling, doing_tlp = 0; 6904 int32_t do_a_prefetch; 6905 int32_t prefetch_rsm = 0; 6906 int32_t prefetch_so_done = 0; 6907 struct tcp_log_buffer *lgb = NULL; 6908 struct inpcb *inp; 6909 struct sockbuf *sb; 6910 #ifdef INET6 6911 struct ip6_hdr *ip6 = NULL; 6912 int32_t isipv6; 6913 #endif 6914 /* setup and take the cache hits here */ 6915 rack = (struct tcp_rack *)tp->t_fb_ptr; 6916 inp = rack->rc_inp; 6917 so = inp->inp_socket; 6918 sb = &so->so_snd; 6919 kern_prefetch(sb, &do_a_prefetch); 6920 do_a_prefetch = 1; 6921 6922 INP_WLOCK_ASSERT(inp); 6923 #ifdef TCP_OFFLOAD 6924 if (tp->t_flags & TF_TOE) 6925 return (tcp_offload_output(tp)); 6926 #endif 6927 6928 /* 6929 * For TFO connections in SYN_RECEIVED, only allow the initial 6930 * SYN|ACK and those sent by the retransmit timer. 6931 */ 6932 if (IS_FASTOPEN(tp->t_flags) && 6933 (tp->t_state == TCPS_SYN_RECEIVED) && 6934 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN|ACK sent */ 6935 (rack->r_ctl.rc_resend == NULL)) /* not a retransmit */ 6936 return (0); 6937 #ifdef INET6 6938 if (rack->r_state) { 6939 /* Use the cache line loaded if possible */ 6940 isipv6 = rack->r_is_v6; 6941 } else { 6942 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 6943 } 6944 #endif 6945 cts = tcp_ts_getticks(); 6946 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 6947 inp->inp_in_hpts) { 6948 /* 6949 * We are on the hpts for some timer but not hptsi output. 6950 * Remove from the hpts unconditionally. 6951 */ 6952 rack_timer_cancel(tp, rack, cts, __LINE__); 6953 } 6954 /* Mark that we have called rack_output(). */ 6955 if ((rack->r_timer_override) || 6956 (tp->t_flags & TF_FORCEDATA) || 6957 (tp->t_state < TCPS_ESTABLISHED)) { 6958 if (tp->t_inpcb->inp_in_hpts) 6959 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT); 6960 } else if (tp->t_inpcb->inp_in_hpts) { 6961 /* 6962 * On the hpts you can't pass even if ACKNOW is on, we will 6963 * when the hpts fires. 6964 */ 6965 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1); 6966 return (0); 6967 } 6968 hpts_calling = inp->inp_hpts_calls; 6969 inp->inp_hpts_calls = 0; 6970 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 6971 if (rack_process_timers(tp, rack, cts, hpts_calling)) { 6972 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1); 6973 return (0); 6974 } 6975 } 6976 rack->r_wanted_output = 0; 6977 rack->r_timer_override = 0; 6978 /* 6979 * Determine length of data that should be transmitted, and flags 6980 * that will be used. If there is some data or critical controls 6981 * (SYN, RST) to send, then transmit; otherwise, investigate 6982 * further. 6983 */ 6984 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 6985 #ifdef NETFLIX_CWV 6986 if (tp->cwv_enabled) { 6987 if ((tp->cwv_cwnd_valid == 0) && 6988 TCPS_HAVEESTABLISHED(tp->t_state) && 6989 (tp->snd_cwnd > tp->snd_cwv.init_cwnd)) 6990 tcp_newcwv_nvp_closedown(tp); 6991 } else 6992 #endif 6993 if (tp->t_idle_reduce) { 6994 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) 6995 rack_cc_after_idle(tp, 6996 (rack->r_idle_reduce_largest ? 1 :0)); 6997 } 6998 tp->t_flags &= ~TF_LASTIDLE; 6999 if (idle) { 7000 if (tp->t_flags & TF_MORETOCOME) { 7001 tp->t_flags |= TF_LASTIDLE; 7002 idle = 0; 7003 } 7004 } 7005 again: 7006 /* 7007 * If we've recently taken a timeout, snd_max will be greater than 7008 * snd_nxt. There may be SACK information that allows us to avoid 7009 * resending already delivered data. Adjust snd_nxt accordingly. 7010 */ 7011 sendalot = 0; 7012 cts = tcp_ts_getticks(); 7013 tso = 0; 7014 mtu = 0; 7015 sb_offset = tp->snd_max - tp->snd_una; 7016 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 7017 7018 flags = tcp_outflags[tp->t_state]; 7019 /* 7020 * Send any SACK-generated retransmissions. If we're explicitly 7021 * trying to send out new data (when sendalot is 1), bypass this 7022 * function. If we retransmit in fast recovery mode, decrement 7023 * snd_cwnd, since we're replacing a (future) new transmission with 7024 * a retransmission now, and we previously incremented snd_cwnd in 7025 * tcp_input(). 7026 */ 7027 /* 7028 * Still in sack recovery , reset rxmit flag to zero. 7029 */ 7030 while (rack->rc_free_cnt < rack_free_cache) { 7031 rsm = rack_alloc(rack); 7032 if (rsm == NULL) { 7033 if (inp->inp_hpts_calls) 7034 /* Retry in a ms */ 7035 slot = 1; 7036 goto just_return_nolock; 7037 } 7038 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_next); 7039 rack->rc_free_cnt++; 7040 rsm = NULL; 7041 } 7042 if (inp->inp_hpts_calls) 7043 inp->inp_hpts_calls = 0; 7044 sack_rxmit = 0; 7045 len = 0; 7046 rsm = NULL; 7047 if (flags & TH_RST) { 7048 SOCKBUF_LOCK(sb); 7049 goto send; 7050 } 7051 if (rack->r_ctl.rc_tlpsend) { 7052 /* Tail loss probe */ 7053 long cwin; 7054 long tlen; 7055 7056 doing_tlp = 1; 7057 rsm = rack->r_ctl.rc_tlpsend; 7058 rack->r_ctl.rc_tlpsend = NULL; 7059 sack_rxmit = 1; 7060 tlen = rsm->r_end - rsm->r_start; 7061 if (tlen > tp->t_maxseg) 7062 tlen = tp->t_maxseg; 7063 #ifdef INVARIANTS 7064 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 7065 panic("tp:%p rack:%p snd_una:%u rsm:%p r_start:%u", 7066 tp, rack, tp->snd_una, rsm, rsm->r_start); 7067 } 7068 #endif 7069 sb_offset = rsm->r_start - tp->snd_una; 7070 cwin = min(tp->snd_wnd, tlen); 7071 len = cwin; 7072 } else if (rack->r_ctl.rc_resend) { 7073 /* Retransmit timer */ 7074 rsm = rack->r_ctl.rc_resend; 7075 rack->r_ctl.rc_resend = NULL; 7076 len = rsm->r_end - rsm->r_start; 7077 sack_rxmit = 1; 7078 sendalot = 0; 7079 sb_offset = rsm->r_start - tp->snd_una; 7080 if (len >= tp->t_maxseg) { 7081 len = tp->t_maxseg; 7082 } 7083 KASSERT(sb_offset >= 0, ("%s: sack block to the left of una : %d", 7084 __func__, sb_offset)); 7085 } else if ((rack->rc_in_persist == 0) && 7086 ((rsm = tcp_rack_output(tp, rack, cts)) != NULL)) { 7087 long tlen; 7088 7089 if ((!IN_RECOVERY(tp->t_flags)) && 7090 ((tp->t_flags & (TF_WASFRECOVERY | TF_WASCRECOVERY)) == 0)) { 7091 /* Enter recovery if not induced by a time-out */ 7092 rack->r_ctl.rc_rsm_start = rsm->r_start; 7093 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 7094 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 7095 rack_cong_signal(tp, NULL, CC_NDUPACK); 7096 /* 7097 * When we enter recovery we need to assure we send 7098 * one packet. 7099 */ 7100 rack->r_ctl.rc_prr_sndcnt = tp->t_maxseg; 7101 } 7102 #ifdef INVARIANTS 7103 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 7104 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n", 7105 tp, rack, rsm, rsm->r_start, tp->snd_una); 7106 } 7107 #endif 7108 tlen = rsm->r_end - rsm->r_start; 7109 sb_offset = rsm->r_start - tp->snd_una; 7110 if (tlen > rack->r_ctl.rc_prr_sndcnt) { 7111 len = rack->r_ctl.rc_prr_sndcnt; 7112 } else { 7113 len = tlen; 7114 } 7115 if (len >= tp->t_maxseg) { 7116 sendalot = 1; 7117 len = tp->t_maxseg; 7118 } else { 7119 sendalot = 0; 7120 if ((rack->rc_timer_up == 0) && 7121 (len < tlen)) { 7122 /* 7123 * If its not a timer don't send a partial 7124 * segment. 7125 */ 7126 len = 0; 7127 goto just_return_nolock; 7128 } 7129 } 7130 KASSERT(sb_offset >= 0, ("%s: sack block to the left of una : %d", 7131 __func__, sb_offset)); 7132 if (len > 0) { 7133 sub_from_prr = 1; 7134 sack_rxmit = 1; 7135 TCPSTAT_INC(tcps_sack_rexmits); 7136 TCPSTAT_ADD(tcps_sack_rexmit_bytes, 7137 min(len, tp->t_maxseg)); 7138 counter_u64_add(rack_rtm_prr_retran, 1); 7139 } 7140 } 7141 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) { 7142 /* we are retransmitting the fin */ 7143 len--; 7144 if (len) { 7145 /* 7146 * When retransmitting data do *not* include the 7147 * FIN. This could happen from a TLP probe. 7148 */ 7149 flags &= ~TH_FIN; 7150 } 7151 } 7152 #ifdef INVARIANTS 7153 /* For debugging */ 7154 rack->r_ctl.rc_rsm_at_retran = rsm; 7155 #endif 7156 /* 7157 * Get standard flags, and add SYN or FIN if requested by 'hidden' 7158 * state flags. 7159 */ 7160 if (tp->t_flags & TF_NEEDFIN) 7161 flags |= TH_FIN; 7162 if (tp->t_flags & TF_NEEDSYN) 7163 flags |= TH_SYN; 7164 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 7165 void *end_rsm; 7166 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 7167 if (end_rsm) 7168 kern_prefetch(end_rsm, &prefetch_rsm); 7169 prefetch_rsm = 1; 7170 } 7171 SOCKBUF_LOCK(sb); 7172 /* 7173 * If in persist timeout with window of 0, send 1 byte. Otherwise, 7174 * if window is small but nonzero and time TF_SENTFIN expired, we 7175 * will send what we can and go to transmit state. 7176 */ 7177 if (tp->t_flags & TF_FORCEDATA) { 7178 if (sendwin == 0) { 7179 /* 7180 * If we still have some data to send, then clear 7181 * the FIN bit. Usually this would happen below 7182 * when it realizes that we aren't sending all the 7183 * data. However, if we have exactly 1 byte of 7184 * unsent data, then it won't clear the FIN bit 7185 * below, and if we are in persist state, we wind up 7186 * sending the packet without recording that we sent 7187 * the FIN bit. 7188 * 7189 * We can't just blindly clear the FIN bit, because 7190 * if we don't have any more data to send then the 7191 * probe will be the FIN itself. 7192 */ 7193 if (sb_offset < sbused(sb)) 7194 flags &= ~TH_FIN; 7195 sendwin = 1; 7196 } else { 7197 if (rack->rc_in_persist) 7198 rack_exit_persist(tp, rack); 7199 /* 7200 * If we are dropping persist mode then we need to 7201 * correct snd_nxt/snd_max and off. 7202 */ 7203 tp->snd_nxt = tp->snd_max; 7204 sb_offset = tp->snd_nxt - tp->snd_una; 7205 } 7206 } 7207 /* 7208 * If snd_nxt == snd_max and we have transmitted a FIN, the 7209 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 7210 * negative length. This can also occur when TCP opens up its 7211 * congestion window while receiving additional duplicate acks after 7212 * fast-retransmit because TCP will reset snd_nxt to snd_max after 7213 * the fast-retransmit. 7214 * 7215 * In the normal retransmit-FIN-only case, however, snd_nxt will be 7216 * set to snd_una, the sb_offset will be 0, and the length may wind 7217 * up 0. 7218 * 7219 * If sack_rxmit is true we are retransmitting from the scoreboard 7220 * in which case len is already set. 7221 */ 7222 if (sack_rxmit == 0) { 7223 uint32_t avail; 7224 7225 avail = sbavail(sb); 7226 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail) 7227 sb_offset = tp->snd_nxt - tp->snd_una; 7228 else 7229 sb_offset = 0; 7230 if (IN_RECOVERY(tp->t_flags) == 0) { 7231 if (rack->r_ctl.rc_tlp_new_data) { 7232 /* TLP is forcing out new data */ 7233 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) { 7234 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset); 7235 } 7236 if (rack->r_ctl.rc_tlp_new_data > tp->snd_wnd) 7237 len = tp->snd_wnd; 7238 else 7239 len = rack->r_ctl.rc_tlp_new_data; 7240 rack->r_ctl.rc_tlp_new_data = 0; 7241 doing_tlp = 1; 7242 } else { 7243 if (sendwin > avail) { 7244 /* use the available */ 7245 if (avail > sb_offset) { 7246 len = (int32_t)(avail - sb_offset); 7247 } else { 7248 len = 0; 7249 } 7250 } else { 7251 if (sendwin > sb_offset) { 7252 len = (int32_t)(sendwin - sb_offset); 7253 } else { 7254 len = 0; 7255 } 7256 } 7257 } 7258 } else { 7259 uint32_t outstanding; 7260 7261 /* 7262 * We are inside of a SACK recovery episode and are 7263 * sending new data, having retransmitted all the 7264 * data possible so far in the scoreboard. 7265 */ 7266 outstanding = tp->snd_max - tp->snd_una; 7267 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) 7268 len = 0; 7269 else if (avail > sb_offset) 7270 len = avail - sb_offset; 7271 else 7272 len = 0; 7273 if (len > 0) { 7274 if (len > rack->r_ctl.rc_prr_sndcnt) 7275 len = rack->r_ctl.rc_prr_sndcnt; 7276 7277 if (len > 0) { 7278 sub_from_prr = 1; 7279 counter_u64_add(rack_rtm_prr_newdata, 1); 7280 } 7281 } 7282 if (len > tp->t_maxseg) { 7283 /* 7284 * We should never send more than a MSS when 7285 * retransmitting or sending new data in prr 7286 * mode unless the override flag is on. Most 7287 * likely the PRR algorithm is not going to 7288 * let us send a lot as well :-) 7289 */ 7290 if (rack->r_ctl.rc_prr_sendalot == 0) 7291 len = tp->t_maxseg; 7292 } else if (len < tp->t_maxseg) { 7293 /* 7294 * Do we send any? The idea here is if the 7295 * send empty's the socket buffer we want to 7296 * do it. However if not then lets just wait 7297 * for our prr_sndcnt to get bigger. 7298 */ 7299 long leftinsb; 7300 7301 leftinsb = sbavail(sb) - sb_offset; 7302 if (leftinsb > len) { 7303 /* This send does not empty the sb */ 7304 len = 0; 7305 } 7306 } 7307 } 7308 } 7309 if (prefetch_so_done == 0) { 7310 kern_prefetch(so, &prefetch_so_done); 7311 prefetch_so_done = 1; 7312 } 7313 /* 7314 * Lop off SYN bit if it has already been sent. However, if this is 7315 * SYN-SENT state and if segment contains data and if we don't know 7316 * that foreign host supports TAO, suppress sending segment. 7317 */ 7318 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) && 7319 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) { 7320 if (tp->t_state != TCPS_SYN_RECEIVED) 7321 flags &= ~TH_SYN; 7322 /* 7323 * When sending additional segments following a TFO SYN|ACK, 7324 * do not include the SYN bit. 7325 */ 7326 if (IS_FASTOPEN(tp->t_flags) && 7327 (tp->t_state == TCPS_SYN_RECEIVED)) 7328 flags &= ~TH_SYN; 7329 sb_offset--, len++; 7330 } 7331 /* 7332 * Be careful not to send data and/or FIN on SYN segments. This 7333 * measure is needed to prevent interoperability problems with not 7334 * fully conformant TCP implementations. 7335 */ 7336 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 7337 len = 0; 7338 flags &= ~TH_FIN; 7339 } 7340 /* 7341 * On TFO sockets, ensure no data is sent in the following cases: 7342 * 7343 * - When retransmitting SYN|ACK on a passively-created socket 7344 * 7345 * - When retransmitting SYN on an actively created socket 7346 * 7347 * - When sending a zero-length cookie (cookie request) on an 7348 * actively created socket 7349 * 7350 * - When the socket is in the CLOSED state (RST is being sent) 7351 */ 7352 if (IS_FASTOPEN(tp->t_flags) && 7353 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 7354 ((tp->t_state == TCPS_SYN_SENT) && 7355 (tp->t_tfo_client_cookie_len == 0)) || 7356 (flags & TH_RST))) 7357 len = 0; 7358 /* Without fast-open there should never be data sent on a SYN */ 7359 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) 7360 len = 0; 7361 if (len <= 0) { 7362 /* 7363 * If FIN has been sent but not acked, but we haven't been 7364 * called to retransmit, len will be < 0. Otherwise, window 7365 * shrank after we sent into it. If window shrank to 0, 7366 * cancel pending retransmit, pull snd_nxt back to (closed) 7367 * window, and set the persist timer if it isn't already 7368 * going. If the window didn't close completely, just wait 7369 * for an ACK. 7370 * 7371 * We also do a general check here to ensure that we will 7372 * set the persist timer when we have data to send, but a 7373 * 0-byte window. This makes sure the persist timer is set 7374 * even if the packet hits one of the "goto send" lines 7375 * below. 7376 */ 7377 len = 0; 7378 if ((tp->snd_wnd == 0) && 7379 (TCPS_HAVEESTABLISHED(tp->t_state)) && 7380 (sb_offset < (int)sbavail(sb))) { 7381 tp->snd_nxt = tp->snd_una; 7382 rack_enter_persist(tp, rack, cts); 7383 } 7384 } 7385 /* len will be >= 0 after this point. */ 7386 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 7387 tcp_sndbuf_autoscale(tp, so, sendwin); 7388 /* 7389 * Decide if we can use TCP Segmentation Offloading (if supported by 7390 * hardware). 7391 * 7392 * TSO may only be used if we are in a pure bulk sending state. The 7393 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 7394 * options prevent using TSO. With TSO the TCP header is the same 7395 * (except for the sequence number) for all generated packets. This 7396 * makes it impossible to transmit any options which vary per 7397 * generated segment or packet. 7398 * 7399 * IPv4 handling has a clear separation of ip options and ip header 7400 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 7401 * the right thing below to provide length of just ip options and thus 7402 * checking for ipoptlen is enough to decide if ip options are present. 7403 */ 7404 7405 #ifdef INET6 7406 if (isipv6) 7407 ipoptlen = ip6_optlen(tp->t_inpcb); 7408 else 7409 #endif 7410 if (tp->t_inpcb->inp_options) 7411 ipoptlen = tp->t_inpcb->inp_options->m_len - 7412 offsetof(struct ipoption, ipopt_list); 7413 else 7414 ipoptlen = 0; 7415 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7416 /* 7417 * Pre-calculate here as we save another lookup into the darknesses 7418 * of IPsec that way and can actually decide if TSO is ok. 7419 */ 7420 #ifdef INET6 7421 if (isipv6 && IPSEC_ENABLED(ipv6)) 7422 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 7423 #ifdef INET 7424 else 7425 #endif 7426 #endif /* INET6 */ 7427 #ifdef INET 7428 if (IPSEC_ENABLED(ipv4)) 7429 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 7430 #endif /* INET */ 7431 #endif 7432 7433 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7434 ipoptlen += ipsec_optlen; 7435 #endif 7436 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 7437 (tp->t_port == 0) && 7438 ((tp->t_flags & TF_SIGNATURE) == 0) && 7439 tp->rcv_numsacks == 0 && sack_rxmit == 0 && 7440 ipoptlen == 0) 7441 tso = 1; 7442 { 7443 uint32_t outstanding; 7444 7445 outstanding = tp->snd_max - tp->snd_una; 7446 if (tp->t_flags & TF_SENTFIN) { 7447 /* 7448 * If we sent a fin, snd_max is 1 higher than 7449 * snd_una 7450 */ 7451 outstanding--; 7452 } 7453 if (outstanding > 0) { 7454 /* 7455 * This is sub-optimal. We only send a stand alone 7456 * FIN on its own segment. 7457 */ 7458 if (flags & TH_FIN) { 7459 flags &= ~TH_FIN; 7460 would_have_fin = 1; 7461 } 7462 } else if (sack_rxmit) { 7463 if ((rsm->r_flags & RACK_HAS_FIN) == 0) 7464 flags &= ~TH_FIN; 7465 } else { 7466 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 7467 sbused(sb))) 7468 flags &= ~TH_FIN; 7469 } 7470 } 7471 recwin = sbspace(&so->so_rcv); 7472 7473 /* 7474 * Sender silly window avoidance. We transmit under the following 7475 * conditions when len is non-zero: 7476 * 7477 * - We have a full segment (or more with TSO) - This is the last 7478 * buffer in a write()/send() and we are either idle or running 7479 * NODELAY - we've timed out (e.g. persist timer) - we have more 7480 * then 1/2 the maximum send window's worth of data (receiver may be 7481 * limited the window size) - we need to retransmit 7482 */ 7483 if (len) { 7484 if (len >= tp->t_maxseg) { 7485 pass = 1; 7486 goto send; 7487 } 7488 /* 7489 * NOTE! on localhost connections an 'ack' from the remote 7490 * end may occur synchronously with the output and cause us 7491 * to flush a buffer queued with moretocome. XXX 7492 * 7493 */ 7494 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 7495 (idle || (tp->t_flags & TF_NODELAY)) && 7496 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(&so->so_snd)) && 7497 (tp->t_flags & TF_NOPUSH) == 0) { 7498 pass = 2; 7499 goto send; 7500 } 7501 if (tp->t_flags & TF_FORCEDATA) { /* typ. timeout case */ 7502 pass = 3; 7503 goto send; 7504 } 7505 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 7506 goto send; 7507 } 7508 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 7509 pass = 4; 7510 goto send; 7511 } 7512 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */ 7513 pass = 5; 7514 goto send; 7515 } 7516 if (sack_rxmit) { 7517 pass = 6; 7518 goto send; 7519 } 7520 } 7521 /* 7522 * Sending of standalone window updates. 7523 * 7524 * Window updates are important when we close our window due to a 7525 * full socket buffer and are opening it again after the application 7526 * reads data from it. Once the window has opened again and the 7527 * remote end starts to send again the ACK clock takes over and 7528 * provides the most current window information. 7529 * 7530 * We must avoid the silly window syndrome whereas every read from 7531 * the receive buffer, no matter how small, causes a window update 7532 * to be sent. We also should avoid sending a flurry of window 7533 * updates when the socket buffer had queued a lot of data and the 7534 * application is doing small reads. 7535 * 7536 * Prevent a flurry of pointless window updates by only sending an 7537 * update when we can increase the advertized window by more than 7538 * 1/4th of the socket buffer capacity. When the buffer is getting 7539 * full or is very small be more aggressive and send an update 7540 * whenever we can increase by two mss sized segments. In all other 7541 * situations the ACK's to new incoming data will carry further 7542 * window increases. 7543 * 7544 * Don't send an independent window update if a delayed ACK is 7545 * pending (it will get piggy-backed on it) or the remote side 7546 * already has done a half-close and won't send more data. Skip 7547 * this if the connection is in T/TCP half-open state. 7548 */ 7549 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 7550 !(tp->t_flags & TF_DELACK) && 7551 !TCPS_HAVERCVDFIN(tp->t_state)) { 7552 /* 7553 * "adv" is the amount we could increase the window, taking 7554 * into account that we are limited by TCP_MAXWIN << 7555 * tp->rcv_scale. 7556 */ 7557 int32_t adv; 7558 int oldwin; 7559 7560 adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale); 7561 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 7562 oldwin = (tp->rcv_adv - tp->rcv_nxt); 7563 adv -= oldwin; 7564 } else 7565 oldwin = 0; 7566 7567 /* 7568 * If the new window size ends up being the same as the old 7569 * size when it is scaled, then don't force a window update. 7570 */ 7571 if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale) 7572 goto dontupdate; 7573 7574 if (adv >= (int32_t)(2 * tp->t_maxseg) && 7575 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 7576 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) || 7577 so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg)) { 7578 pass = 7; 7579 goto send; 7580 } 7581 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 7582 goto send; 7583 } 7584 dontupdate: 7585 7586 /* 7587 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 7588 * is also a catch-all for the retransmit timer timeout case. 7589 */ 7590 if (tp->t_flags & TF_ACKNOW) { 7591 pass = 8; 7592 goto send; 7593 } 7594 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) { 7595 pass = 9; 7596 goto send; 7597 } 7598 if (SEQ_GT(tp->snd_up, tp->snd_una)) { 7599 pass = 10; 7600 goto send; 7601 } 7602 /* 7603 * If our state indicates that FIN should be sent and we have not 7604 * yet done so, then we need to send. 7605 */ 7606 if (flags & TH_FIN) { 7607 if ((tp->t_flags & TF_SENTFIN) || 7608 (((tp->t_flags & TF_SENTFIN) == 0) && 7609 (tp->snd_nxt == tp->snd_una))) { 7610 pass = 11; 7611 goto send; 7612 } 7613 } 7614 /* 7615 * No reason to send a segment, just return. 7616 */ 7617 just_return: 7618 SOCKBUF_UNLOCK(sb); 7619 just_return_nolock: 7620 if (tot_len_this_send == 0) 7621 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1); 7622 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1); 7623 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling); 7624 tp->t_flags &= ~TF_FORCEDATA; 7625 return (0); 7626 7627 send: 7628 if (doing_tlp == 0) { 7629 /* 7630 * Data not a TLP, and its not the rxt firing. If it is the 7631 * rxt firing, we want to leave the tlp_in_progress flag on 7632 * so we don't send another TLP. It has to be a rack timer 7633 * or normal send (response to acked data) to clear the tlp 7634 * in progress flag. 7635 */ 7636 rack->rc_tlp_in_progress = 0; 7637 } 7638 SOCKBUF_LOCK_ASSERT(sb); 7639 if (len > 0) { 7640 if (len >= tp->t_maxseg) 7641 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 7642 else 7643 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 7644 } 7645 /* 7646 * Before ESTABLISHED, force sending of initial options unless TCP 7647 * set not to do any options. NOTE: we assume that the IP/TCP header 7648 * plus TCP options always fit in a single mbuf, leaving room for a 7649 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 7650 * + optlen <= MCLBYTES 7651 */ 7652 optlen = 0; 7653 #ifdef INET6 7654 if (isipv6) 7655 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 7656 else 7657 #endif 7658 hdrlen = sizeof(struct tcpiphdr); 7659 7660 /* 7661 * Compute options for segment. We only have to care about SYN and 7662 * established connection segments. Options for SYN-ACK segments 7663 * are handled in TCP syncache. 7664 */ 7665 to.to_flags = 0; 7666 if ((tp->t_flags & TF_NOOPT) == 0) { 7667 /* Maximum segment size. */ 7668 if (flags & TH_SYN) { 7669 tp->snd_nxt = tp->iss; 7670 to.to_mss = tcp_mssopt(&inp->inp_inc); 7671 #ifdef NETFLIX_TCPOUDP 7672 if (tp->t_port) 7673 to.to_mss -= V_tcp_udp_tunneling_overhead; 7674 #endif 7675 to.to_flags |= TOF_MSS; 7676 7677 /* 7678 * On SYN or SYN|ACK transmits on TFO connections, 7679 * only include the TFO option if it is not a 7680 * retransmit, as the presence of the TFO option may 7681 * have caused the original SYN or SYN|ACK to have 7682 * been dropped by a middlebox. 7683 */ 7684 if (IS_FASTOPEN(tp->t_flags) && 7685 (tp->t_rxtshift == 0)) { 7686 if (tp->t_state == TCPS_SYN_RECEIVED) { 7687 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 7688 to.to_tfo_cookie = 7689 (u_int8_t *)&tp->t_tfo_cookie.server; 7690 to.to_flags |= TOF_FASTOPEN; 7691 wanted_cookie = 1; 7692 } else if (tp->t_state == TCPS_SYN_SENT) { 7693 to.to_tfo_len = 7694 tp->t_tfo_client_cookie_len; 7695 to.to_tfo_cookie = 7696 tp->t_tfo_cookie.client; 7697 to.to_flags |= TOF_FASTOPEN; 7698 wanted_cookie = 1; 7699 /* 7700 * If we wind up having more data to 7701 * send with the SYN than can fit in 7702 * one segment, don't send any more 7703 * until the SYN|ACK comes back from 7704 * the other end. 7705 */ 7706 sendalot = 0; 7707 } 7708 } 7709 } 7710 /* Window scaling. */ 7711 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 7712 to.to_wscale = tp->request_r_scale; 7713 to.to_flags |= TOF_SCALE; 7714 } 7715 /* Timestamps. */ 7716 if ((tp->t_flags & TF_RCVD_TSTMP) || 7717 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 7718 to.to_tsval = cts + tp->ts_offset; 7719 to.to_tsecr = tp->ts_recent; 7720 to.to_flags |= TOF_TS; 7721 } 7722 /* Set receive buffer autosizing timestamp. */ 7723 if (tp->rfbuf_ts == 0 && 7724 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 7725 tp->rfbuf_ts = tcp_ts_getticks(); 7726 /* Selective ACK's. */ 7727 if (flags & TH_SYN) 7728 to.to_flags |= TOF_SACKPERM; 7729 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 7730 tp->rcv_numsacks > 0) { 7731 to.to_flags |= TOF_SACK; 7732 to.to_nsacks = tp->rcv_numsacks; 7733 to.to_sacks = (u_char *)tp->sackblks; 7734 } 7735 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 7736 /* TCP-MD5 (RFC2385). */ 7737 if (tp->t_flags & TF_SIGNATURE) 7738 to.to_flags |= TOF_SIGNATURE; 7739 #endif /* TCP_SIGNATURE */ 7740 7741 /* Processing the options. */ 7742 hdrlen += optlen = tcp_addoptions(&to, opt); 7743 /* 7744 * If we wanted a TFO option to be added, but it was unable 7745 * to fit, ensure no data is sent. 7746 */ 7747 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 7748 !(to.to_flags & TOF_FASTOPEN)) 7749 len = 0; 7750 } 7751 #ifdef NETFLIX_TCPOUDP 7752 if (tp->t_port) { 7753 if (V_tcp_udp_tunneling_port == 0) { 7754 /* The port was removed?? */ 7755 SOCKBUF_UNLOCK(&so->so_snd); 7756 return (EHOSTUNREACH); 7757 } 7758 hdrlen += sizeof(struct udphdr); 7759 } 7760 #endif 7761 ipoptlen = 0; 7762 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 7763 ipoptlen += ipsec_optlen; 7764 #endif 7765 7766 /* 7767 * Adjust data length if insertion of options will bump the packet 7768 * length beyond the t_maxseg length. Clear the FIN bit because we 7769 * cut off the tail of the segment. 7770 */ 7771 if (len + optlen + ipoptlen > tp->t_maxseg) { 7772 if (flags & TH_FIN) { 7773 would_have_fin = 1; 7774 flags &= ~TH_FIN; 7775 } 7776 if (tso) { 7777 uint32_t if_hw_tsomax; 7778 uint32_t moff; 7779 int32_t max_len; 7780 7781 /* extract TSO information */ 7782 if_hw_tsomax = tp->t_tsomax; 7783 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 7784 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 7785 KASSERT(ipoptlen == 0, 7786 ("%s: TSO can't do IP options", __func__)); 7787 7788 /* 7789 * Check if we should limit by maximum payload 7790 * length: 7791 */ 7792 if (if_hw_tsomax != 0) { 7793 /* compute maximum TSO length */ 7794 max_len = (if_hw_tsomax - hdrlen - 7795 max_linkhdr); 7796 if (max_len <= 0) { 7797 len = 0; 7798 } else if (len > max_len) { 7799 sendalot = 1; 7800 len = max_len; 7801 } 7802 } 7803 /* 7804 * Prevent the last segment from being fractional 7805 * unless the send sockbuf can be emptied: 7806 */ 7807 max_len = (tp->t_maxseg - optlen); 7808 if ((sb_offset + len) < sbavail(sb)) { 7809 moff = len % (u_int)max_len; 7810 if (moff != 0) { 7811 len -= moff; 7812 sendalot = 1; 7813 } 7814 } 7815 /* 7816 * In case there are too many small fragments don't 7817 * use TSO: 7818 */ 7819 if (len <= max_len) { 7820 len = max_len; 7821 sendalot = 1; 7822 tso = 0; 7823 } 7824 /* 7825 * Send the FIN in a separate segment after the bulk 7826 * sending is done. We don't trust the TSO 7827 * implementations to clear the FIN flag on all but 7828 * the last segment. 7829 */ 7830 if (tp->t_flags & TF_NEEDFIN) 7831 sendalot = 1; 7832 7833 } else { 7834 len = tp->t_maxseg - optlen - ipoptlen; 7835 sendalot = 1; 7836 } 7837 } else 7838 tso = 0; 7839 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 7840 ("%s: len > IP_MAXPACKET", __func__)); 7841 #ifdef DIAGNOSTIC 7842 #ifdef INET6 7843 if (max_linkhdr + hdrlen > MCLBYTES) 7844 #else 7845 if (max_linkhdr + hdrlen > MHLEN) 7846 #endif 7847 panic("tcphdr too big"); 7848 #endif 7849 7850 /* 7851 * This KASSERT is here to catch edge cases at a well defined place. 7852 * Before, those had triggered (random) panic conditions further 7853 * down. 7854 */ 7855 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 7856 if ((len == 0) && 7857 (flags & TH_FIN) && 7858 (sbused(sb))) { 7859 /* 7860 * We have outstanding data, don't send a fin by itself!. 7861 */ 7862 goto just_return; 7863 } 7864 /* 7865 * Grab a header mbuf, attaching a copy of data to be transmitted, 7866 * and initialize the header from the template for sends on this 7867 * connection. 7868 */ 7869 if (len) { 7870 uint32_t max_val; 7871 uint32_t moff; 7872 7873 if (rack->rc_pace_max_segs) 7874 max_val = rack->rc_pace_max_segs * tp->t_maxseg; 7875 else 7876 max_val = len; 7877 /* 7878 * We allow a limit on sending with hptsi. 7879 */ 7880 if (len > max_val) { 7881 len = max_val; 7882 } 7883 #ifdef INET6 7884 if (MHLEN < hdrlen + max_linkhdr) 7885 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 7886 else 7887 #endif 7888 m = m_gethdr(M_NOWAIT, MT_DATA); 7889 7890 if (m == NULL) { 7891 SOCKBUF_UNLOCK(sb); 7892 error = ENOBUFS; 7893 sack_rxmit = 0; 7894 goto out; 7895 } 7896 m->m_data += max_linkhdr; 7897 m->m_len = hdrlen; 7898 7899 /* 7900 * Start the m_copy functions from the closest mbuf to the 7901 * sb_offset in the socket buffer chain. 7902 */ 7903 mb = sbsndptr_noadv(sb, sb_offset, &moff); 7904 if (len <= MHLEN - hdrlen - max_linkhdr) { 7905 m_copydata(mb, moff, (int)len, 7906 mtod(m, caddr_t)+hdrlen); 7907 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7908 sbsndptr_adv(sb, mb, len); 7909 m->m_len += len; 7910 } else { 7911 struct sockbuf *msb; 7912 7913 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7914 msb = NULL; 7915 else 7916 msb = sb; 7917 m->m_next = tcp_m_copym(mb, moff, &len, 7918 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb); 7919 if (len <= (tp->t_maxseg - optlen)) { 7920 /* 7921 * Must have ran out of mbufs for the copy 7922 * shorten it to no longer need tso. Lets 7923 * not put on sendalot since we are low on 7924 * mbufs. 7925 */ 7926 tso = 0; 7927 } 7928 if (m->m_next == NULL) { 7929 SOCKBUF_UNLOCK(sb); 7930 (void)m_free(m); 7931 error = ENOBUFS; 7932 sack_rxmit = 0; 7933 goto out; 7934 } 7935 } 7936 if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 7937 TCPSTAT_INC(tcps_sndprobe); 7938 #ifdef NETFLIX_STATS 7939 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 7940 stats_voi_update_abs_u32(tp->t_stats, 7941 VOI_TCP_RETXPB, len); 7942 else 7943 stats_voi_update_abs_u64(tp->t_stats, 7944 VOI_TCP_TXPB, len); 7945 #endif 7946 } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 7947 if (rsm && (rsm->r_flags & RACK_TLP)) { 7948 /* 7949 * TLP should not count in retran count, but 7950 * in its own bin 7951 */ 7952 counter_u64_add(rack_tlp_retran, 1); 7953 counter_u64_add(rack_tlp_retran_bytes, len); 7954 } else { 7955 tp->t_sndrexmitpack++; 7956 TCPSTAT_INC(tcps_sndrexmitpack); 7957 TCPSTAT_ADD(tcps_sndrexmitbyte, len); 7958 } 7959 #ifdef NETFLIX_STATS 7960 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 7961 len); 7962 #endif 7963 } else { 7964 TCPSTAT_INC(tcps_sndpack); 7965 TCPSTAT_ADD(tcps_sndbyte, len); 7966 #ifdef NETFLIX_STATS 7967 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 7968 len); 7969 #endif 7970 } 7971 /* 7972 * If we're sending everything we've got, set PUSH. (This 7973 * will keep happy those implementations which only give 7974 * data to the user when a buffer fills or a PUSH comes in.) 7975 */ 7976 if (sb_offset + len == sbused(sb) && 7977 sbused(sb) && 7978 !(flags & TH_SYN)) 7979 flags |= TH_PUSH; 7980 7981 /* 7982 * Are we doing hptsi, if so we must calculate the slot. We 7983 * only do hptsi in ESTABLISHED and with no RESET being 7984 * sent where we have data to send. 7985 */ 7986 if (((tp->t_state == TCPS_ESTABLISHED) || 7987 (tp->t_state == TCPS_CLOSE_WAIT) || 7988 ((tp->t_state == TCPS_FIN_WAIT_1) && 7989 ((tp->t_flags & TF_SENTFIN) == 0) && 7990 ((flags & TH_FIN) == 0))) && 7991 ((flags & TH_RST) == 0) && 7992 (rack->rc_always_pace)) { 7993 /* 7994 * We use the most optimistic possible cwnd/srtt for 7995 * sending calculations. This will make our 7996 * calculation anticipate getting more through 7997 * quicker then possible. But thats ok we don't want 7998 * the peer to have a gap in data sending. 7999 */ 8000 uint32_t srtt, cwnd, tr_perms = 0; 8001 8002 if (rack->r_ctl.rc_rack_min_rtt) 8003 srtt = rack->r_ctl.rc_rack_min_rtt; 8004 else 8005 srtt = TICKS_2_MSEC((tp->t_srtt >> TCP_RTT_SHIFT)); 8006 if (rack->r_ctl.rc_rack_largest_cwnd) 8007 cwnd = rack->r_ctl.rc_rack_largest_cwnd; 8008 else 8009 cwnd = tp->snd_cwnd; 8010 tr_perms = cwnd / srtt; 8011 if (tr_perms == 0) { 8012 tr_perms = tp->t_maxseg; 8013 } 8014 tot_len_this_send += len; 8015 /* 8016 * Calculate how long this will take to drain, if 8017 * the calculation comes out to zero, thats ok we 8018 * will use send_a_lot to possibly spin around for 8019 * more increasing tot_len_this_send to the point 8020 * that its going to require a pace, or we hit the 8021 * cwnd. Which in that case we are just waiting for 8022 * a ACK. 8023 */ 8024 slot = tot_len_this_send / tr_perms; 8025 /* Now do we reduce the time so we don't run dry? */ 8026 if (slot && rack->rc_pace_reduce) { 8027 int32_t reduce; 8028 8029 reduce = (slot / rack->rc_pace_reduce); 8030 if (reduce < slot) { 8031 slot -= reduce; 8032 } else 8033 slot = 0; 8034 } 8035 if (rack->r_enforce_min_pace && 8036 (slot == 0) && 8037 (tot_len_this_send >= (rack->r_min_pace_seg_thresh * tp->t_maxseg))) { 8038 /* We are enforcing a minimum pace time of 1ms */ 8039 slot = rack->r_enforce_min_pace; 8040 } 8041 } 8042 SOCKBUF_UNLOCK(sb); 8043 } else { 8044 SOCKBUF_UNLOCK(sb); 8045 if (tp->t_flags & TF_ACKNOW) 8046 TCPSTAT_INC(tcps_sndacks); 8047 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 8048 TCPSTAT_INC(tcps_sndctrl); 8049 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 8050 TCPSTAT_INC(tcps_sndurg); 8051 else 8052 TCPSTAT_INC(tcps_sndwinup); 8053 8054 m = m_gethdr(M_NOWAIT, MT_DATA); 8055 if (m == NULL) { 8056 error = ENOBUFS; 8057 sack_rxmit = 0; 8058 goto out; 8059 } 8060 #ifdef INET6 8061 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 8062 MHLEN >= hdrlen) { 8063 M_ALIGN(m, hdrlen); 8064 } else 8065 #endif 8066 m->m_data += max_linkhdr; 8067 m->m_len = hdrlen; 8068 } 8069 SOCKBUF_UNLOCK_ASSERT(sb); 8070 m->m_pkthdr.rcvif = (struct ifnet *)0; 8071 #ifdef MAC 8072 mac_inpcb_create_mbuf(inp, m); 8073 #endif 8074 #ifdef INET6 8075 if (isipv6) { 8076 ip6 = mtod(m, struct ip6_hdr *); 8077 #ifdef NETFLIX_TCPOUDP 8078 if (tp->t_port) { 8079 udp = (struct udphdr *)((caddr_t)ip6 + ipoptlen + sizeof(struct ip6_hdr)); 8080 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 8081 udp->uh_dport = tp->t_port; 8082 ulen = hdrlen + len - sizeof(struct ip6_hdr); 8083 udp->uh_ulen = htons(ulen); 8084 th = (struct tcphdr *)(udp + 1); 8085 } else 8086 #endif 8087 th = (struct tcphdr *)(ip6 + 1); 8088 tcpip_fillheaders(inp, ip6, th); 8089 } else 8090 #endif /* INET6 */ 8091 { 8092 ip = mtod(m, struct ip *); 8093 #ifdef TCPDEBUG 8094 ipov = (struct ipovly *)ip; 8095 #endif 8096 #ifdef NETFLIX_TCPOUDP 8097 if (tp->t_port) { 8098 udp = (struct udphdr *)((caddr_t)ip + ipoptlen + sizeof(struct ip)); 8099 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 8100 udp->uh_dport = tp->t_port; 8101 ulen = hdrlen + len - sizeof(struct ip); 8102 udp->uh_ulen = htons(ulen); 8103 th = (struct tcphdr *)(udp + 1); 8104 } else 8105 #endif 8106 th = (struct tcphdr *)(ip + 1); 8107 tcpip_fillheaders(inp, ip, th); 8108 } 8109 /* 8110 * Fill in fields, remembering maximum advertised window for use in 8111 * delaying messages about window sizes. If resending a FIN, be sure 8112 * not to use a new sequence number. 8113 */ 8114 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 8115 tp->snd_nxt == tp->snd_max) 8116 tp->snd_nxt--; 8117 /* 8118 * If we are starting a connection, send ECN setup SYN packet. If we 8119 * are on a retransmit, we may resend those bits a number of times 8120 * as per RFC 3168. 8121 */ 8122 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) { 8123 if (tp->t_rxtshift >= 1) { 8124 if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 8125 flags |= TH_ECE | TH_CWR; 8126 } else 8127 flags |= TH_ECE | TH_CWR; 8128 } 8129 if (tp->t_state == TCPS_ESTABLISHED && 8130 (tp->t_flags & TF_ECN_PERMIT)) { 8131 /* 8132 * If the peer has ECN, mark data packets with ECN capable 8133 * transmission (ECT). Ignore pure ack packets, 8134 * retransmissions and window probes. 8135 */ 8136 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 8137 !((tp->t_flags & TF_FORCEDATA) && len == 1)) { 8138 #ifdef INET6 8139 if (isipv6) 8140 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 8141 else 8142 #endif 8143 ip->ip_tos |= IPTOS_ECN_ECT0; 8144 TCPSTAT_INC(tcps_ecn_ect0); 8145 } 8146 /* 8147 * Reply with proper ECN notifications. 8148 */ 8149 if (tp->t_flags & TF_ECN_SND_CWR) { 8150 flags |= TH_CWR; 8151 tp->t_flags &= ~TF_ECN_SND_CWR; 8152 } 8153 if (tp->t_flags & TF_ECN_SND_ECE) 8154 flags |= TH_ECE; 8155 } 8156 /* 8157 * If we are doing retransmissions, then snd_nxt will not reflect 8158 * the first unsent octet. For ACK only packets, we do not want the 8159 * sequence number of the retransmitted packet, we want the sequence 8160 * number of the next unsent octet. So, if there is no data (and no 8161 * SYN or FIN), use snd_max instead of snd_nxt when filling in 8162 * ti_seq. But if we are in persist state, snd_max might reflect 8163 * one byte beyond the right edge of the window, so use snd_nxt in 8164 * that case, since we know we aren't doing a retransmission. 8165 * (retransmit and persist are mutually exclusive...) 8166 */ 8167 if (sack_rxmit == 0) { 8168 if (len || (flags & (TH_SYN | TH_FIN)) || 8169 rack->rc_in_persist) { 8170 th->th_seq = htonl(tp->snd_nxt); 8171 rack_seq = tp->snd_nxt; 8172 } else if (flags & TH_RST) { 8173 /* 8174 * For a Reset send the last cum ack in sequence 8175 * (this like any other choice may still generate a 8176 * challenge ack, if a ack-update packet is in 8177 * flight). 8178 */ 8179 th->th_seq = htonl(tp->snd_una); 8180 rack_seq = tp->snd_una; 8181 } else { 8182 th->th_seq = htonl(tp->snd_max); 8183 rack_seq = tp->snd_max; 8184 } 8185 } else { 8186 th->th_seq = htonl(rsm->r_start); 8187 rack_seq = rsm->r_start; 8188 } 8189 th->th_ack = htonl(tp->rcv_nxt); 8190 if (optlen) { 8191 bcopy(opt, th + 1, optlen); 8192 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 8193 } 8194 th->th_flags = flags; 8195 /* 8196 * Calculate receive window. Don't shrink window, but avoid silly 8197 * window syndrome. 8198 */ 8199 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 8200 recwin < (long)tp->t_maxseg) 8201 recwin = 0; 8202 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 8203 recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 8204 recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 8205 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 8206 recwin = (long)TCP_MAXWIN << tp->rcv_scale; 8207 8208 /* 8209 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 8210 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 8211 * handled in syncache. 8212 */ 8213 if (flags & TH_SYN) 8214 th->th_win = htons((u_short) 8215 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 8216 else 8217 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 8218 /* 8219 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 8220 * window. This may cause the remote transmitter to stall. This 8221 * flag tells soreceive() to disable delayed acknowledgements when 8222 * draining the buffer. This can occur if the receiver is 8223 * attempting to read more data than can be buffered prior to 8224 * transmitting on the connection. 8225 */ 8226 if (th->th_win == 0) { 8227 tp->t_sndzerowin++; 8228 tp->t_flags |= TF_RXWIN0SENT; 8229 } else 8230 tp->t_flags &= ~TF_RXWIN0SENT; 8231 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 8232 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 8233 th->th_flags |= TH_URG; 8234 } else 8235 /* 8236 * If no urgent pointer to send, then we pull the urgent 8237 * pointer to the left edge of the send window so that it 8238 * doesn't drift into the send window on sequence number 8239 * wraparound. 8240 */ 8241 tp->snd_up = tp->snd_una; /* drag it along */ 8242 8243 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 8244 if (to.to_flags & TOF_SIGNATURE) { 8245 /* 8246 * Calculate MD5 signature and put it into the place 8247 * determined before. 8248 * NOTE: since TCP options buffer doesn't point into 8249 * mbuf's data, calculate offset and use it. 8250 */ 8251 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 8252 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 8253 /* 8254 * Do not send segment if the calculation of MD5 8255 * digest has failed. 8256 */ 8257 goto out; 8258 } 8259 } 8260 #endif 8261 8262 /* 8263 * Put TCP length in extended header, and then checksum extended 8264 * header and data. 8265 */ 8266 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 8267 #ifdef INET6 8268 if (isipv6) { 8269 /* 8270 * ip6_plen is not need to be filled now, and will be filled 8271 * in ip6_output. 8272 */ 8273 if (tp->t_port) { 8274 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 8275 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 8276 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 8277 th->th_sum = htons(0); 8278 } else { 8279 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 8280 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 8281 th->th_sum = in6_cksum_pseudo(ip6, 8282 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 8283 0); 8284 } 8285 } 8286 #endif 8287 #if defined(INET6) && defined(INET) 8288 else 8289 #endif 8290 #ifdef INET 8291 { 8292 if (tp->t_port) { 8293 m->m_pkthdr.csum_flags = CSUM_UDP; 8294 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 8295 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 8296 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 8297 th->th_sum = htons(0); 8298 } else { 8299 m->m_pkthdr.csum_flags = CSUM_TCP; 8300 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 8301 th->th_sum = in_pseudo(ip->ip_src.s_addr, 8302 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 8303 IPPROTO_TCP + len + optlen)); 8304 } 8305 /* IP version must be set here for ipv4/ipv6 checking later */ 8306 KASSERT(ip->ip_v == IPVERSION, 8307 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 8308 } 8309 #endif 8310 8311 /* 8312 * Enable TSO and specify the size of the segments. The TCP pseudo 8313 * header checksum is always provided. XXX: Fixme: This is currently 8314 * not the case for IPv6. 8315 */ 8316 if (tso) { 8317 KASSERT(len > tp->t_maxseg - optlen, 8318 ("%s: len <= tso_segsz", __func__)); 8319 m->m_pkthdr.csum_flags |= CSUM_TSO; 8320 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 8321 } 8322 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 8323 KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL), 8324 ("%s: mbuf chain shorter than expected: %d + %u + %u - %u != %u", 8325 __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL))); 8326 #else 8327 KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL), 8328 ("%s: mbuf chain shorter than expected: %d + %u + %u != %u", 8329 __func__, len, hdrlen, ipoptlen, m_length(m, NULL))); 8330 #endif 8331 8332 #ifdef TCP_HHOOK 8333 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 8334 hhook_run_tcp_est_out(tp, th, &to, len, tso); 8335 #endif 8336 8337 #ifdef TCPDEBUG 8338 /* 8339 * Trace. 8340 */ 8341 if (so->so_options & SO_DEBUG) { 8342 u_short save = 0; 8343 8344 #ifdef INET6 8345 if (!isipv6) 8346 #endif 8347 { 8348 save = ipov->ih_len; 8349 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + 8350 * (th->th_off << 2) */ ); 8351 } 8352 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 8353 #ifdef INET6 8354 if (!isipv6) 8355 #endif 8356 ipov->ih_len = save; 8357 } 8358 #endif /* TCPDEBUG */ 8359 8360 /* We're getting ready to send; log now. */ 8361 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 8362 union tcp_log_stackspecific log; 8363 8364 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 8365 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 8366 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 8367 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 8368 if (rsm || sack_rxmit) { 8369 log.u_bbr.flex8 = 1; 8370 } else { 8371 log.u_bbr.flex8 = 0; 8372 } 8373 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 8374 len, &log, false, NULL, NULL, 0, NULL); 8375 } else 8376 lgb = NULL; 8377 8378 /* 8379 * Fill in IP length and desired time to live and send to IP level. 8380 * There should be a better way to handle ttl and tos; we could keep 8381 * them in the template, but need a way to checksum without them. 8382 */ 8383 /* 8384 * m->m_pkthdr.len should have been set before cksum calcuration, 8385 * because in6_cksum() need it. 8386 */ 8387 #ifdef INET6 8388 if (isipv6) { 8389 /* 8390 * we separately set hoplimit for every segment, since the 8391 * user might want to change the value via setsockopt. Also, 8392 * desired default hop limit might be changed via Neighbor 8393 * Discovery. 8394 */ 8395 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 8396 8397 /* 8398 * Set the packet size here for the benefit of DTrace 8399 * probes. ip6_output() will set it properly; it's supposed 8400 * to include the option header lengths as well. 8401 */ 8402 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 8403 8404 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 8405 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 8406 else 8407 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8408 8409 if (tp->t_state == TCPS_SYN_SENT) 8410 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 8411 8412 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 8413 /* TODO: IPv6 IP6TOS_ECT bit on */ 8414 error = ip6_output(m, tp->t_inpcb->in6p_outputopts, 8415 &inp->inp_route6, 8416 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 8417 NULL, NULL, inp); 8418 8419 if (error == EMSGSIZE && inp->inp_route6.ro_rt != NULL) 8420 mtu = inp->inp_route6.ro_rt->rt_mtu; 8421 } 8422 #endif /* INET6 */ 8423 #if defined(INET) && defined(INET6) 8424 else 8425 #endif 8426 #ifdef INET 8427 { 8428 ip->ip_len = htons(m->m_pkthdr.len); 8429 #ifdef INET6 8430 if (inp->inp_vflag & INP_IPV6PROTO) 8431 ip->ip_ttl = in6_selecthlim(inp, NULL); 8432 #endif /* INET6 */ 8433 /* 8434 * If we do path MTU discovery, then we set DF on every 8435 * packet. This might not be the best thing to do according 8436 * to RFC3390 Section 2. However the tcp hostcache migitates 8437 * the problem so it affects only the first tcp connection 8438 * with a host. 8439 * 8440 * NB: Don't set DF on small MTU/MSS to have a safe 8441 * fallback. 8442 */ 8443 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 8444 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 8445 if (tp->t_port == 0 || len < V_tcp_minmss) { 8446 ip->ip_off |= htons(IP_DF); 8447 } 8448 } else { 8449 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8450 } 8451 8452 if (tp->t_state == TCPS_SYN_SENT) 8453 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 8454 8455 TCP_PROBE5(send, NULL, tp, ip, tp, th); 8456 8457 error = ip_output(m, tp->t_inpcb->inp_options, &inp->inp_route, 8458 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 8459 inp); 8460 if (error == EMSGSIZE && inp->inp_route.ro_rt != NULL) 8461 mtu = inp->inp_route.ro_rt->rt_mtu; 8462 } 8463 #endif /* INET */ 8464 8465 out: 8466 if (lgb) { 8467 lgb->tlb_errno = error; 8468 lgb = NULL; 8469 } 8470 /* 8471 * In transmit state, time the transmission and arrange for the 8472 * retransmit. In persist state, just set snd_max. 8473 */ 8474 if (error == 0) { 8475 if (len == 0) 8476 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1); 8477 else if (len == 1) { 8478 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1); 8479 } else if (len > 1) { 8480 int idx; 8481 8482 idx = (len / tp->t_maxseg) + 3; 8483 if (idx >= TCP_MSS_ACCT_ATIMER) 8484 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 8485 else 8486 counter_u64_add(rack_out_size[idx], 1); 8487 } 8488 } 8489 if (sub_from_prr && (error == 0)) { 8490 rack->r_ctl.rc_prr_sndcnt -= len; 8491 } 8492 sub_from_prr = 0; 8493 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, cts, 8494 pass, rsm); 8495 if ((tp->t_flags & TF_FORCEDATA) == 0 || 8496 (rack->rc_in_persist == 0)) { 8497 #ifdef NETFLIX_STATS 8498 tcp_seq startseq = tp->snd_nxt; 8499 #endif 8500 8501 /* 8502 * Advance snd_nxt over sequence space of this segment. 8503 */ 8504 if (error) 8505 /* We don't log or do anything with errors */ 8506 goto timer; 8507 8508 if (flags & (TH_SYN | TH_FIN)) { 8509 if (flags & TH_SYN) 8510 tp->snd_nxt++; 8511 if (flags & TH_FIN) { 8512 tp->snd_nxt++; 8513 tp->t_flags |= TF_SENTFIN; 8514 } 8515 } 8516 /* In the ENOBUFS case we do *not* update snd_max */ 8517 if (sack_rxmit) 8518 goto timer; 8519 8520 tp->snd_nxt += len; 8521 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 8522 if (tp->snd_una == tp->snd_max) { 8523 /* 8524 * Update the time we just added data since 8525 * none was outstanding. 8526 */ 8527 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 8528 tp->t_acktime = ticks; 8529 } 8530 tp->snd_max = tp->snd_nxt; 8531 #ifdef NETFLIX_STATS 8532 if (!(tp->t_flags & TF_GPUTINPROG) && len) { 8533 tp->t_flags |= TF_GPUTINPROG; 8534 tp->gput_seq = startseq; 8535 tp->gput_ack = startseq + 8536 ulmin(sbavail(sb) - sb_offset, sendwin); 8537 tp->gput_ts = tcp_ts_getticks(); 8538 } 8539 #endif 8540 } 8541 /* 8542 * Set retransmit timer if not currently set, and not doing 8543 * a pure ack or a keep-alive probe. Initial value for 8544 * retransmit timer is smoothed round-trip time + 2 * 8545 * round-trip time variance. Initialize shift counter which 8546 * is used for backoff of retransmit time. 8547 */ 8548 timer: 8549 if ((tp->snd_wnd == 0) && 8550 TCPS_HAVEESTABLISHED(tp->t_state)) { 8551 /* 8552 * If the persists timer was set above (right before 8553 * the goto send), and still needs to be on. Lets 8554 * make sure all is canceled. If the persist timer 8555 * is not running, we want to get it up. 8556 */ 8557 if (rack->rc_in_persist == 0) { 8558 rack_enter_persist(tp, rack, cts); 8559 } 8560 } 8561 } else { 8562 /* 8563 * Persist case, update snd_max but since we are in persist 8564 * mode (no window) we do not update snd_nxt. 8565 */ 8566 int32_t xlen = len; 8567 8568 if (error) 8569 goto nomore; 8570 8571 if (flags & TH_SYN) 8572 ++xlen; 8573 if (flags & TH_FIN) { 8574 ++xlen; 8575 tp->t_flags |= TF_SENTFIN; 8576 } 8577 /* In the ENOBUFS case we do *not* update snd_max */ 8578 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) { 8579 if (tp->snd_una == tp->snd_max) { 8580 /* 8581 * Update the time we just added data since 8582 * none was outstanding. 8583 */ 8584 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 8585 tp->t_acktime = ticks; 8586 } 8587 tp->snd_max = tp->snd_nxt + len; 8588 } 8589 } 8590 nomore: 8591 if (error) { 8592 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */ 8593 /* 8594 * Failures do not advance the seq counter above. For the 8595 * case of ENOBUFS we will fall out and retry in 1ms with 8596 * the hpts. Everything else will just have to retransmit 8597 * with the timer. 8598 * 8599 * In any case, we do not want to loop around for another 8600 * send without a good reason. 8601 */ 8602 sendalot = 0; 8603 switch (error) { 8604 case EPERM: 8605 tp->t_flags &= ~TF_FORCEDATA; 8606 tp->t_softerror = error; 8607 return (error); 8608 case ENOBUFS: 8609 if (slot == 0) { 8610 /* 8611 * Pace us right away to retry in a some 8612 * time 8613 */ 8614 slot = 1 + rack->rc_enobuf; 8615 if (rack->rc_enobuf < 255) 8616 rack->rc_enobuf++; 8617 if (slot > (rack->rc_rack_rtt / 2)) { 8618 slot = rack->rc_rack_rtt / 2; 8619 } 8620 if (slot < 10) 8621 slot = 10; 8622 } 8623 counter_u64_add(rack_saw_enobuf, 1); 8624 error = 0; 8625 goto enobufs; 8626 case EMSGSIZE: 8627 /* 8628 * For some reason the interface we used initially 8629 * to send segments changed to another or lowered 8630 * its MTU. If TSO was active we either got an 8631 * interface without TSO capabilits or TSO was 8632 * turned off. If we obtained mtu from ip_output() 8633 * then update it and try again. 8634 */ 8635 if (tso) 8636 tp->t_flags &= ~TF_TSO; 8637 if (mtu != 0) { 8638 tcp_mss_update(tp, -1, mtu, NULL, NULL); 8639 goto again; 8640 } 8641 slot = 10; 8642 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1); 8643 tp->t_flags &= ~TF_FORCEDATA; 8644 return (error); 8645 case ENETUNREACH: 8646 counter_u64_add(rack_saw_enetunreach, 1); 8647 case EHOSTDOWN: 8648 case EHOSTUNREACH: 8649 case ENETDOWN: 8650 if (TCPS_HAVERCVDSYN(tp->t_state)) { 8651 tp->t_softerror = error; 8652 } 8653 /* FALLTHROUGH */ 8654 default: 8655 slot = 10; 8656 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, 0, 1); 8657 tp->t_flags &= ~TF_FORCEDATA; 8658 return (error); 8659 } 8660 } else { 8661 rack->rc_enobuf = 0; 8662 } 8663 TCPSTAT_INC(tcps_sndtotal); 8664 8665 /* 8666 * Data sent (as far as we can tell). If this advertises a larger 8667 * window than any other segment, then remember the size of the 8668 * advertised window. Any pending ACK has now been sent. 8669 */ 8670 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 8671 tp->rcv_adv = tp->rcv_nxt + recwin; 8672 tp->last_ack_sent = tp->rcv_nxt; 8673 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 8674 enobufs: 8675 rack->r_tlp_running = 0; 8676 if ((flags & TH_RST) || (would_have_fin == 1)) { 8677 /* 8678 * We don't send again after a RST. We also do *not* send 8679 * again if we would have had a find, but now have 8680 * outstanding data. 8681 */ 8682 slot = 0; 8683 sendalot = 0; 8684 } 8685 if (slot) { 8686 /* set the rack tcb into the slot N */ 8687 counter_u64_add(rack_paced_segments, 1); 8688 } else if (sendalot) { 8689 if (len) 8690 counter_u64_add(rack_unpaced_segments, 1); 8691 sack_rxmit = 0; 8692 tp->t_flags &= ~TF_FORCEDATA; 8693 goto again; 8694 } else if (len) { 8695 counter_u64_add(rack_unpaced_segments, 1); 8696 } 8697 tp->t_flags &= ~TF_FORCEDATA; 8698 rack_start_hpts_timer(rack, tp, cts, __LINE__, slot, tot_len_this_send, 1); 8699 return (error); 8700 } 8701 8702 /* 8703 * rack_ctloutput() must drop the inpcb lock before performing copyin on 8704 * socket option arguments. When it re-acquires the lock after the copy, it 8705 * has to revalidate that the connection is still valid for the socket 8706 * option. 8707 */ 8708 static int 8709 rack_set_sockopt(struct socket *so, struct sockopt *sopt, 8710 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 8711 { 8712 int32_t error = 0, optval; 8713 8714 switch (sopt->sopt_name) { 8715 case TCP_RACK_PROP_RATE: 8716 case TCP_RACK_PROP: 8717 case TCP_RACK_TLP_REDUCE: 8718 case TCP_RACK_EARLY_RECOV: 8719 case TCP_RACK_PACE_ALWAYS: 8720 case TCP_DELACK: 8721 case TCP_RACK_PACE_REDUCE: 8722 case TCP_RACK_PACE_MAX_SEG: 8723 case TCP_RACK_PRR_SENDALOT: 8724 case TCP_RACK_MIN_TO: 8725 case TCP_RACK_EARLY_SEG: 8726 case TCP_RACK_REORD_THRESH: 8727 case TCP_RACK_REORD_FADE: 8728 case TCP_RACK_TLP_THRESH: 8729 case TCP_RACK_PKT_DELAY: 8730 case TCP_RACK_TLP_USE: 8731 case TCP_RACK_TLP_INC_VAR: 8732 case TCP_RACK_IDLE_REDUCE_HIGH: 8733 case TCP_RACK_MIN_PACE: 8734 case TCP_RACK_MIN_PACE_SEG: 8735 case TCP_BBR_RACK_RTT_USE: 8736 case TCP_DATA_AFTER_CLOSE: 8737 break; 8738 default: 8739 return (tcp_default_ctloutput(so, sopt, inp, tp)); 8740 break; 8741 } 8742 INP_WUNLOCK(inp); 8743 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 8744 if (error) 8745 return (error); 8746 INP_WLOCK(inp); 8747 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8748 INP_WUNLOCK(inp); 8749 return (ECONNRESET); 8750 } 8751 tp = intotcpcb(inp); 8752 rack = (struct tcp_rack *)tp->t_fb_ptr; 8753 switch (sopt->sopt_name) { 8754 case TCP_RACK_PROP_RATE: 8755 if ((optval <= 0) || (optval >= 100)) { 8756 error = EINVAL; 8757 break; 8758 } 8759 RACK_OPTS_INC(tcp_rack_prop_rate); 8760 rack->r_ctl.rc_prop_rate = optval; 8761 break; 8762 case TCP_RACK_TLP_USE: 8763 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) { 8764 error = EINVAL; 8765 break; 8766 } 8767 RACK_OPTS_INC(tcp_tlp_use); 8768 rack->rack_tlp_threshold_use = optval; 8769 break; 8770 case TCP_RACK_PROP: 8771 /* RACK proportional rate reduction (bool) */ 8772 RACK_OPTS_INC(tcp_rack_prop); 8773 rack->r_ctl.rc_prop_reduce = optval; 8774 break; 8775 case TCP_RACK_TLP_REDUCE: 8776 /* RACK TLP cwnd reduction (bool) */ 8777 RACK_OPTS_INC(tcp_rack_tlp_reduce); 8778 rack->r_ctl.rc_tlp_cwnd_reduce = optval; 8779 break; 8780 case TCP_RACK_EARLY_RECOV: 8781 /* Should recovery happen early (bool) */ 8782 RACK_OPTS_INC(tcp_rack_early_recov); 8783 rack->r_ctl.rc_early_recovery = optval; 8784 break; 8785 case TCP_RACK_PACE_ALWAYS: 8786 /* Use the always pace method (bool) */ 8787 RACK_OPTS_INC(tcp_rack_pace_always); 8788 if (optval > 0) 8789 rack->rc_always_pace = 1; 8790 else 8791 rack->rc_always_pace = 0; 8792 break; 8793 case TCP_RACK_PACE_REDUCE: 8794 /* RACK Hptsi reduction factor (divisor) */ 8795 RACK_OPTS_INC(tcp_rack_pace_reduce); 8796 if (optval) 8797 /* Must be non-zero */ 8798 rack->rc_pace_reduce = optval; 8799 else 8800 error = EINVAL; 8801 break; 8802 case TCP_RACK_PACE_MAX_SEG: 8803 /* Max segments in a pace */ 8804 RACK_OPTS_INC(tcp_rack_max_seg); 8805 rack->rc_pace_max_segs = optval; 8806 break; 8807 case TCP_RACK_PRR_SENDALOT: 8808 /* Allow PRR to send more than one seg */ 8809 RACK_OPTS_INC(tcp_rack_prr_sendalot); 8810 rack->r_ctl.rc_prr_sendalot = optval; 8811 break; 8812 case TCP_RACK_MIN_TO: 8813 /* Minimum time between rack t-o's in ms */ 8814 RACK_OPTS_INC(tcp_rack_min_to); 8815 rack->r_ctl.rc_min_to = optval; 8816 break; 8817 case TCP_RACK_EARLY_SEG: 8818 /* If early recovery max segments */ 8819 RACK_OPTS_INC(tcp_rack_early_seg); 8820 rack->r_ctl.rc_early_recovery_segs = optval; 8821 break; 8822 case TCP_RACK_REORD_THRESH: 8823 /* RACK reorder threshold (shift amount) */ 8824 RACK_OPTS_INC(tcp_rack_reord_thresh); 8825 if ((optval > 0) && (optval < 31)) 8826 rack->r_ctl.rc_reorder_shift = optval; 8827 else 8828 error = EINVAL; 8829 break; 8830 case TCP_RACK_REORD_FADE: 8831 /* Does reordering fade after ms time */ 8832 RACK_OPTS_INC(tcp_rack_reord_fade); 8833 rack->r_ctl.rc_reorder_fade = optval; 8834 break; 8835 case TCP_RACK_TLP_THRESH: 8836 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 8837 RACK_OPTS_INC(tcp_rack_tlp_thresh); 8838 if (optval) 8839 rack->r_ctl.rc_tlp_threshold = optval; 8840 else 8841 error = EINVAL; 8842 break; 8843 case TCP_RACK_PKT_DELAY: 8844 /* RACK added ms i.e. rack-rtt + reord + N */ 8845 RACK_OPTS_INC(tcp_rack_pkt_delay); 8846 rack->r_ctl.rc_pkt_delay = optval; 8847 break; 8848 case TCP_RACK_TLP_INC_VAR: 8849 /* Does TLP include rtt variance in t-o */ 8850 RACK_OPTS_INC(tcp_rack_tlp_inc_var); 8851 rack->r_ctl.rc_prr_inc_var = optval; 8852 break; 8853 case TCP_RACK_IDLE_REDUCE_HIGH: 8854 RACK_OPTS_INC(tcp_rack_idle_reduce_high); 8855 if (optval) 8856 rack->r_idle_reduce_largest = 1; 8857 else 8858 rack->r_idle_reduce_largest = 0; 8859 break; 8860 case TCP_DELACK: 8861 if (optval == 0) 8862 tp->t_delayed_ack = 0; 8863 else 8864 tp->t_delayed_ack = 1; 8865 if (tp->t_flags & TF_DELACK) { 8866 tp->t_flags &= ~TF_DELACK; 8867 tp->t_flags |= TF_ACKNOW; 8868 rack_output(tp); 8869 } 8870 break; 8871 case TCP_RACK_MIN_PACE: 8872 RACK_OPTS_INC(tcp_rack_min_pace); 8873 if (optval > 3) 8874 rack->r_enforce_min_pace = 3; 8875 else 8876 rack->r_enforce_min_pace = optval; 8877 break; 8878 case TCP_RACK_MIN_PACE_SEG: 8879 RACK_OPTS_INC(tcp_rack_min_pace_seg); 8880 if (optval >= 16) 8881 rack->r_min_pace_seg_thresh = 15; 8882 else 8883 rack->r_min_pace_seg_thresh = optval; 8884 break; 8885 case TCP_BBR_RACK_RTT_USE: 8886 if ((optval != USE_RTT_HIGH) && 8887 (optval != USE_RTT_LOW) && 8888 (optval != USE_RTT_AVG)) 8889 error = EINVAL; 8890 else 8891 rack->r_ctl.rc_rate_sample_method = optval; 8892 break; 8893 case TCP_DATA_AFTER_CLOSE: 8894 if (optval) 8895 rack->rc_allow_data_af_clo = 1; 8896 else 8897 rack->rc_allow_data_af_clo = 0; 8898 break; 8899 default: 8900 return (tcp_default_ctloutput(so, sopt, inp, tp)); 8901 break; 8902 } 8903 #ifdef NETFLIX_STATS 8904 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 8905 #endif 8906 INP_WUNLOCK(inp); 8907 return (error); 8908 } 8909 8910 static int 8911 rack_get_sockopt(struct socket *so, struct sockopt *sopt, 8912 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 8913 { 8914 int32_t error, optval; 8915 8916 /* 8917 * Because all our options are either boolean or an int, we can just 8918 * pull everything into optval and then unlock and copy. If we ever 8919 * add a option that is not a int, then this will have quite an 8920 * impact to this routine. 8921 */ 8922 switch (sopt->sopt_name) { 8923 case TCP_RACK_PROP_RATE: 8924 optval = rack->r_ctl.rc_prop_rate; 8925 break; 8926 case TCP_RACK_PROP: 8927 /* RACK proportional rate reduction (bool) */ 8928 optval = rack->r_ctl.rc_prop_reduce; 8929 break; 8930 case TCP_RACK_TLP_REDUCE: 8931 /* RACK TLP cwnd reduction (bool) */ 8932 optval = rack->r_ctl.rc_tlp_cwnd_reduce; 8933 break; 8934 case TCP_RACK_EARLY_RECOV: 8935 /* Should recovery happen early (bool) */ 8936 optval = rack->r_ctl.rc_early_recovery; 8937 break; 8938 case TCP_RACK_PACE_REDUCE: 8939 /* RACK Hptsi reduction factor (divisor) */ 8940 optval = rack->rc_pace_reduce; 8941 break; 8942 case TCP_RACK_PACE_MAX_SEG: 8943 /* Max segments in a pace */ 8944 optval = rack->rc_pace_max_segs; 8945 break; 8946 case TCP_RACK_PACE_ALWAYS: 8947 /* Use the always pace method */ 8948 optval = rack->rc_always_pace; 8949 break; 8950 case TCP_RACK_PRR_SENDALOT: 8951 /* Allow PRR to send more than one seg */ 8952 optval = rack->r_ctl.rc_prr_sendalot; 8953 break; 8954 case TCP_RACK_MIN_TO: 8955 /* Minimum time between rack t-o's in ms */ 8956 optval = rack->r_ctl.rc_min_to; 8957 break; 8958 case TCP_RACK_EARLY_SEG: 8959 /* If early recovery max segments */ 8960 optval = rack->r_ctl.rc_early_recovery_segs; 8961 break; 8962 case TCP_RACK_REORD_THRESH: 8963 /* RACK reorder threshold (shift amount) */ 8964 optval = rack->r_ctl.rc_reorder_shift; 8965 break; 8966 case TCP_RACK_REORD_FADE: 8967 /* Does reordering fade after ms time */ 8968 optval = rack->r_ctl.rc_reorder_fade; 8969 break; 8970 case TCP_RACK_TLP_THRESH: 8971 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 8972 optval = rack->r_ctl.rc_tlp_threshold; 8973 break; 8974 case TCP_RACK_PKT_DELAY: 8975 /* RACK added ms i.e. rack-rtt + reord + N */ 8976 optval = rack->r_ctl.rc_pkt_delay; 8977 break; 8978 case TCP_RACK_TLP_USE: 8979 optval = rack->rack_tlp_threshold_use; 8980 break; 8981 case TCP_RACK_TLP_INC_VAR: 8982 /* Does TLP include rtt variance in t-o */ 8983 optval = rack->r_ctl.rc_prr_inc_var; 8984 break; 8985 case TCP_RACK_IDLE_REDUCE_HIGH: 8986 optval = rack->r_idle_reduce_largest; 8987 break; 8988 case TCP_RACK_MIN_PACE: 8989 optval = rack->r_enforce_min_pace; 8990 break; 8991 case TCP_RACK_MIN_PACE_SEG: 8992 optval = rack->r_min_pace_seg_thresh; 8993 break; 8994 case TCP_BBR_RACK_RTT_USE: 8995 optval = rack->r_ctl.rc_rate_sample_method; 8996 break; 8997 case TCP_DELACK: 8998 optval = tp->t_delayed_ack; 8999 break; 9000 case TCP_DATA_AFTER_CLOSE: 9001 optval = rack->rc_allow_data_af_clo; 9002 break; 9003 default: 9004 return (tcp_default_ctloutput(so, sopt, inp, tp)); 9005 break; 9006 } 9007 INP_WUNLOCK(inp); 9008 error = sooptcopyout(sopt, &optval, sizeof optval); 9009 return (error); 9010 } 9011 9012 static int 9013 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 9014 { 9015 int32_t error = EINVAL; 9016 struct tcp_rack *rack; 9017 9018 rack = (struct tcp_rack *)tp->t_fb_ptr; 9019 if (rack == NULL) { 9020 /* Huh? */ 9021 goto out; 9022 } 9023 if (sopt->sopt_dir == SOPT_SET) { 9024 return (rack_set_sockopt(so, sopt, inp, tp, rack)); 9025 } else if (sopt->sopt_dir == SOPT_GET) { 9026 return (rack_get_sockopt(so, sopt, inp, tp, rack)); 9027 } 9028 out: 9029 INP_WUNLOCK(inp); 9030 return (error); 9031 } 9032 9033 9034 struct tcp_function_block __tcp_rack = { 9035 .tfb_tcp_block_name = __XSTRING(STACKNAME), 9036 .tfb_tcp_output = rack_output, 9037 .tfb_tcp_do_segment = rack_do_segment, 9038 .tfb_tcp_hpts_do_segment = rack_hpts_do_segment, 9039 .tfb_tcp_ctloutput = rack_ctloutput, 9040 .tfb_tcp_fb_init = rack_init, 9041 .tfb_tcp_fb_fini = rack_fini, 9042 .tfb_tcp_timer_stop_all = rack_stopall, 9043 .tfb_tcp_timer_activate = rack_timer_activate, 9044 .tfb_tcp_timer_active = rack_timer_active, 9045 .tfb_tcp_timer_stop = rack_timer_stop, 9046 .tfb_tcp_rexmit_tmr = rack_remxt_tmr, 9047 .tfb_tcp_handoff_ok = rack_handoff_ok 9048 }; 9049 9050 static const char *rack_stack_names[] = { 9051 __XSTRING(STACKNAME), 9052 #ifdef STACKALIAS 9053 __XSTRING(STACKALIAS), 9054 #endif 9055 }; 9056 9057 static int 9058 rack_ctor(void *mem, int32_t size, void *arg, int32_t how) 9059 { 9060 memset(mem, 0, size); 9061 return (0); 9062 } 9063 9064 static void 9065 rack_dtor(void *mem, int32_t size, void *arg) 9066 { 9067 9068 } 9069 9070 static bool rack_mod_inited = false; 9071 9072 static int 9073 tcp_addrack(module_t mod, int32_t type, void *data) 9074 { 9075 int32_t err = 0; 9076 int num_stacks; 9077 9078 switch (type) { 9079 case MOD_LOAD: 9080 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 9081 sizeof(struct rack_sendmap), 9082 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 9083 9084 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 9085 sizeof(struct tcp_rack), 9086 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 9087 9088 sysctl_ctx_init(&rack_sysctl_ctx); 9089 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 9090 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 9091 OID_AUTO, 9092 __XSTRING(STACKNAME), 9093 CTLFLAG_RW, 0, 9094 ""); 9095 if (rack_sysctl_root == NULL) { 9096 printf("Failed to add sysctl node\n"); 9097 err = EFAULT; 9098 goto free_uma; 9099 } 9100 rack_init_sysctls(); 9101 num_stacks = nitems(rack_stack_names); 9102 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK, 9103 rack_stack_names, &num_stacks); 9104 if (err) { 9105 printf("Failed to register %s stack name for " 9106 "%s module\n", rack_stack_names[num_stacks], 9107 __XSTRING(MODNAME)); 9108 sysctl_ctx_free(&rack_sysctl_ctx); 9109 free_uma: 9110 uma_zdestroy(rack_zone); 9111 uma_zdestroy(rack_pcb_zone); 9112 rack_counter_destroy(); 9113 printf("Failed to register rack module -- err:%d\n", err); 9114 return (err); 9115 } 9116 rack_mod_inited = true; 9117 break; 9118 case MOD_QUIESCE: 9119 err = deregister_tcp_functions(&__tcp_rack, true, false); 9120 break; 9121 case MOD_UNLOAD: 9122 err = deregister_tcp_functions(&__tcp_rack, false, true); 9123 if (err == EBUSY) 9124 break; 9125 if (rack_mod_inited) { 9126 uma_zdestroy(rack_zone); 9127 uma_zdestroy(rack_pcb_zone); 9128 sysctl_ctx_free(&rack_sysctl_ctx); 9129 rack_counter_destroy(); 9130 rack_mod_inited = false; 9131 } 9132 err = 0; 9133 break; 9134 default: 9135 return (EOPNOTSUPP); 9136 } 9137 return (err); 9138 } 9139 9140 static moduledata_t tcp_rack = { 9141 .name = __XSTRING(MODNAME), 9142 .evhand = tcp_addrack, 9143 .priv = 0 9144 }; 9145 9146 MODULE_VERSION(MODNAME, 1); 9147 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 9148 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 9149