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