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