1 /*- 2 * Copyright (c) 2016-2020 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 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 #ifdef STATS 54 #include <sys/qmath.h> 55 #include <sys/tree.h> 56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 57 #else 58 #include <sys/tree.h> 59 #endif 60 #include <sys/refcount.h> 61 #include <sys/queue.h> 62 #include <sys/tim_filter.h> 63 #include <sys/smp.h> 64 #include <sys/kthread.h> 65 #include <sys/kern_prefetch.h> 66 #include <sys/protosw.h> 67 #ifdef TCP_ACCOUNTING 68 #include <sys/sched.h> 69 #include <machine/cpu.h> 70 #endif 71 #include <vm/uma.h> 72 73 #include <net/route.h> 74 #include <net/route/nhop.h> 75 #include <net/vnet.h> 76 77 #define TCPSTATES /* for logging */ 78 79 #include <netinet/in.h> 80 #include <netinet/in_kdtrace.h> 81 #include <netinet/in_pcb.h> 82 #include <netinet/ip.h> 83 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 84 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 85 #include <netinet/ip_var.h> 86 #include <netinet/ip6.h> 87 #include <netinet6/in6_pcb.h> 88 #include <netinet6/ip6_var.h> 89 #include <netinet/tcp.h> 90 #define TCPOUTFLAGS 91 #include <netinet/tcp_fsm.h> 92 #include <netinet/tcp_log_buf.h> 93 #include <netinet/tcp_seq.h> 94 #include <netinet/tcp_timer.h> 95 #include <netinet/tcp_var.h> 96 #include <netinet/tcp_hpts.h> 97 #include <netinet/tcp_ratelimit.h> 98 #include <netinet/tcp_accounting.h> 99 #include <netinet/tcpip.h> 100 #include <netinet/cc/cc.h> 101 #include <netinet/cc/cc_newreno.h> 102 #include <netinet/tcp_fastopen.h> 103 #include <netinet/tcp_lro.h> 104 #ifdef NETFLIX_SHARED_CWND 105 #include <netinet/tcp_shared_cwnd.h> 106 #endif 107 #ifdef TCPDEBUG 108 #include <netinet/tcp_debug.h> 109 #endif /* TCPDEBUG */ 110 #ifdef TCP_OFFLOAD 111 #include <netinet/tcp_offload.h> 112 #endif 113 #ifdef INET6 114 #include <netinet6/tcp6_var.h> 115 #endif 116 117 #include <netipsec/ipsec_support.h> 118 119 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 120 #include <netipsec/ipsec.h> 121 #include <netipsec/ipsec6.h> 122 #endif /* IPSEC */ 123 124 #include <netinet/udp.h> 125 #include <netinet/udp_var.h> 126 #include <machine/in_cksum.h> 127 128 #ifdef MAC 129 #include <security/mac/mac_framework.h> 130 #endif 131 #include "sack_filter.h" 132 #include "tcp_rack.h" 133 #include "rack_bbr_common.h" 134 135 uma_zone_t rack_zone; 136 uma_zone_t rack_pcb_zone; 137 138 #ifndef TICKS2SBT 139 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t))) 140 #endif 141 142 VNET_DECLARE(uint32_t, newreno_beta); 143 VNET_DECLARE(uint32_t, newreno_beta_ecn); 144 #define V_newreno_beta VNET(newreno_beta) 145 #define V_newreno_beta_ecn VNET(newreno_beta_ecn) 146 147 148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb", "TCP fast send block"); 149 MALLOC_DEFINE(M_TCPDO, "tcp_do", "TCP deferred options"); 150 151 struct sysctl_ctx_list rack_sysctl_ctx; 152 struct sysctl_oid *rack_sysctl_root; 153 154 #define CUM_ACKED 1 155 #define SACKED 2 156 157 /* 158 * The RACK module incorporates a number of 159 * TCP ideas that have been put out into the IETF 160 * over the last few years: 161 * - Matt Mathis's Rate Halving which slowly drops 162 * the congestion window so that the ack clock can 163 * be maintained during a recovery. 164 * - Yuchung Cheng's RACK TCP (for which its named) that 165 * will stop us using the number of dup acks and instead 166 * use time as the gage of when we retransmit. 167 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 168 * of Dukkipati et.al. 169 * RACK depends on SACK, so if an endpoint arrives that 170 * cannot do SACK the state machine below will shuttle the 171 * connection back to using the "default" TCP stack that is 172 * in FreeBSD. 173 * 174 * To implement RACK the original TCP stack was first decomposed 175 * into a functional state machine with individual states 176 * for each of the possible TCP connection states. The do_segement 177 * functions role in life is to mandate the connection supports SACK 178 * initially and then assure that the RACK state matches the conenction 179 * state before calling the states do_segment function. Each 180 * state is simplified due to the fact that the original do_segment 181 * has been decomposed and we *know* what state we are in (no 182 * switches on the state) and all tests for SACK are gone. This 183 * greatly simplifies what each state does. 184 * 185 * TCP output is also over-written with a new version since it 186 * must maintain the new rack scoreboard. 187 * 188 */ 189 static int32_t rack_tlp_thresh = 1; 190 static int32_t rack_tlp_limit = 2; /* No more than 2 TLPs w-out new data */ 191 static int32_t rack_tlp_use_greater = 1; 192 static int32_t rack_reorder_thresh = 2; 193 static int32_t rack_reorder_fade = 60000000; /* 0 - never fade, def 60,000,000 194 * - 60 seconds */ 195 static uint8_t rack_req_measurements = 1; 196 /* Attack threshold detections */ 197 static uint32_t rack_highest_sack_thresh_seen = 0; 198 static uint32_t rack_highest_move_thresh_seen = 0; 199 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */ 200 static int32_t rack_hw_pace_extra_slots = 2; /* 2 extra MSS time betweens */ 201 static int32_t rack_hw_rate_caps = 1; /* 1; */ 202 static int32_t rack_hw_rate_min = 0; /* 1500000;*/ 203 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */ 204 static int32_t rack_hw_up_only = 1; 205 static int32_t rack_stats_gets_ms_rtt = 1; 206 static int32_t rack_prr_addbackmax = 2; 207 208 static int32_t rack_pkt_delay = 1000; 209 static int32_t rack_send_a_lot_in_prr = 1; 210 static int32_t rack_min_to = 1000; /* Number of microsecond min timeout */ 211 static int32_t rack_verbose_logging = 0; 212 static int32_t rack_ignore_data_after_close = 1; 213 static int32_t rack_enable_shared_cwnd = 1; 214 static int32_t rack_use_cmp_acks = 1; 215 static int32_t rack_use_fsb = 1; 216 static int32_t rack_use_rfo = 1; 217 static int32_t rack_use_rsm_rfo = 1; 218 static int32_t rack_max_abc_post_recovery = 2; 219 static int32_t rack_client_low_buf = 0; 220 static int32_t rack_dsack_std_based = 0x3; /* bit field bit 1 sets rc_rack_tmr_std_based and bit 2 sets rc_rack_use_dsack */ 221 #ifdef TCP_ACCOUNTING 222 static int32_t rack_tcp_accounting = 0; 223 #endif 224 static int32_t rack_limits_scwnd = 1; 225 static int32_t rack_enable_mqueue_for_nonpaced = 0; 226 static int32_t rack_disable_prr = 0; 227 static int32_t use_rack_rr = 1; 228 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */ 229 static int32_t rack_persist_min = 250000; /* 250usec */ 230 static int32_t rack_persist_max = 2000000; /* 2 Second in usec's */ 231 static int32_t rack_sack_not_required = 1; /* set to one to allow non-sack to use rack */ 232 static int32_t rack_default_init_window = 0; /* Use system default */ 233 static int32_t rack_limit_time_with_srtt = 0; 234 static int32_t rack_autosndbuf_inc = 20; /* In percentage form */ 235 static int32_t rack_enobuf_hw_boost_mult = 2; /* How many times the hw rate we boost slot using time_between */ 236 static int32_t rack_enobuf_hw_max = 12000; /* 12 ms in usecs */ 237 static int32_t rack_enobuf_hw_min = 10000; /* 10 ms in usecs */ 238 static int32_t rack_hw_rwnd_factor = 2; /* How many max_segs the rwnd must be before we hold off sending */ 239 /* 240 * Currently regular tcp has a rto_min of 30ms 241 * the backoff goes 12 times so that ends up 242 * being a total of 122.850 seconds before a 243 * connection is killed. 244 */ 245 static uint32_t rack_def_data_window = 20; 246 static uint32_t rack_goal_bdp = 2; 247 static uint32_t rack_min_srtts = 1; 248 static uint32_t rack_min_measure_usec = 0; 249 static int32_t rack_tlp_min = 10000; /* 10ms */ 250 static int32_t rack_rto_min = 30000; /* 30,000 usec same as main freebsd */ 251 static int32_t rack_rto_max = 4000000; /* 4 seconds in usec's */ 252 static const int32_t rack_free_cache = 2; 253 static int32_t rack_hptsi_segments = 40; 254 static int32_t rack_rate_sample_method = USE_RTT_LOW; 255 static int32_t rack_pace_every_seg = 0; 256 static int32_t rack_delayed_ack_time = 40000; /* 40ms in usecs */ 257 static int32_t rack_slot_reduction = 4; 258 static int32_t rack_wma_divisor = 8; /* For WMA calculation */ 259 static int32_t rack_cwnd_block_ends_measure = 0; 260 static int32_t rack_rwnd_block_ends_measure = 0; 261 static int32_t rack_def_profile = 0; 262 263 static int32_t rack_lower_cwnd_at_tlp = 0; 264 static int32_t rack_limited_retran = 0; 265 static int32_t rack_always_send_oldest = 0; 266 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE; 267 268 static uint16_t rack_per_of_gp_ss = 250; /* 250 % slow-start */ 269 static uint16_t rack_per_of_gp_ca = 200; /* 200 % congestion-avoidance */ 270 static uint16_t rack_per_of_gp_rec = 200; /* 200 % of bw */ 271 272 /* Probertt */ 273 static uint16_t rack_per_of_gp_probertt = 60; /* 60% of bw */ 274 static uint16_t rack_per_of_gp_lowthresh = 40; /* 40% is bottom */ 275 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */ 276 static uint16_t rack_atexit_prtt_hbp = 130; /* Clamp to 130% on exit prtt if highly buffered path */ 277 static uint16_t rack_atexit_prtt = 130; /* Clamp to 100% on exit prtt if non highly buffered path */ 278 279 static uint32_t rack_max_drain_wait = 2; /* How man gp srtt's before we give up draining */ 280 static uint32_t rack_must_drain = 1; /* How many GP srtt's we *must* wait */ 281 static uint32_t rack_probertt_use_min_rtt_entry = 1; /* Use the min to calculate the goal else gp_srtt */ 282 static uint32_t rack_probertt_use_min_rtt_exit = 0; 283 static uint32_t rack_probe_rtt_sets_cwnd = 0; 284 static uint32_t rack_probe_rtt_safety_val = 2000000; /* No more than 2 sec in probe-rtt */ 285 static uint32_t rack_time_between_probertt = 9600000; /* 9.6 sec in usecs */ 286 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0; /* How many srtt periods does probe-rtt last top fraction */ 287 static uint32_t rack_probertt_gpsrtt_cnt_div = 0; /* How many srtt periods does probe-rtt last bottom fraction */ 288 static uint32_t rack_min_probertt_hold = 40000; /* Equal to delayed ack time */ 289 static uint32_t rack_probertt_filter_life = 10000000; 290 static uint32_t rack_probertt_lower_within = 10; 291 static uint32_t rack_min_rtt_movement = 250000; /* Must move at least 250ms (in microseconds) to count as a lowering */ 292 static int32_t rack_pace_one_seg = 0; /* Shall we pace for less than 1.4Meg 1MSS at a time */ 293 static int32_t rack_probertt_clear_is = 1; 294 static int32_t rack_max_drain_hbp = 1; /* Extra drain times gpsrtt for highly buffered paths */ 295 static int32_t rack_hbp_thresh = 3; /* what is the divisor max_rtt/min_rtt to decided a hbp */ 296 297 /* Part of pacing */ 298 static int32_t rack_max_per_above = 30; /* When we go to increment stop if above 100+this% */ 299 300 /* Timely information */ 301 /* Combine these two gives the range of 'no change' to bw */ 302 /* ie the up/down provide the upper and lower bound */ 303 static int32_t rack_gp_per_bw_mul_up = 2; /* 2% */ 304 static int32_t rack_gp_per_bw_mul_down = 4; /* 4% */ 305 static int32_t rack_gp_rtt_maxmul = 3; /* 3 x maxmin */ 306 static int32_t rack_gp_rtt_minmul = 1; /* minrtt + (minrtt/mindiv) is lower rtt */ 307 static int32_t rack_gp_rtt_mindiv = 4; /* minrtt + (minrtt * minmul/mindiv) is lower rtt */ 308 static int32_t rack_gp_decrease_per = 20; /* 20% decrease in multipler */ 309 static int32_t rack_gp_increase_per = 2; /* 2% increase in multipler */ 310 static int32_t rack_per_lower_bound = 50; /* Don't allow to drop below this multiplier */ 311 static int32_t rack_per_upper_bound_ss = 0; /* Don't allow SS to grow above this */ 312 static int32_t rack_per_upper_bound_ca = 0; /* Don't allow CA to grow above this */ 313 static int32_t rack_do_dyn_mul = 0; /* Are the rack gp multipliers dynamic */ 314 static int32_t rack_gp_no_rec_chg = 1; /* Prohibit recovery from reducing it's multiplier */ 315 static int32_t rack_timely_dec_clear = 6; /* Do we clear decrement count at a value (6)? */ 316 static int32_t rack_timely_max_push_rise = 3; /* One round of pushing */ 317 static int32_t rack_timely_max_push_drop = 3; /* Three round of pushing */ 318 static int32_t rack_timely_min_segs = 4; /* 4 segment minimum */ 319 static int32_t rack_use_max_for_nobackoff = 0; 320 static int32_t rack_timely_int_timely_only = 0; /* do interim timely's only use the timely algo (no b/w changes)? */ 321 static int32_t rack_timely_no_stopping = 0; 322 static int32_t rack_down_raise_thresh = 100; 323 static int32_t rack_req_segs = 1; 324 static uint64_t rack_bw_rate_cap = 0; 325 326 /* Weird delayed ack mode */ 327 static int32_t rack_use_imac_dack = 0; 328 /* Rack specific counters */ 329 counter_u64_t rack_badfr; 330 counter_u64_t rack_badfr_bytes; 331 counter_u64_t rack_rtm_prr_retran; 332 counter_u64_t rack_rtm_prr_newdata; 333 counter_u64_t rack_timestamp_mismatch; 334 counter_u64_t rack_reorder_seen; 335 counter_u64_t rack_paced_segments; 336 counter_u64_t rack_unpaced_segments; 337 counter_u64_t rack_calc_zero; 338 counter_u64_t rack_calc_nonzero; 339 counter_u64_t rack_saw_enobuf; 340 counter_u64_t rack_saw_enobuf_hw; 341 counter_u64_t rack_saw_enetunreach; 342 counter_u64_t rack_per_timer_hole; 343 counter_u64_t rack_large_ackcmp; 344 counter_u64_t rack_small_ackcmp; 345 #ifdef INVARIANTS 346 counter_u64_t rack_adjust_map_bw; 347 #endif 348 /* Tail loss probe counters */ 349 counter_u64_t rack_tlp_tot; 350 counter_u64_t rack_tlp_newdata; 351 counter_u64_t rack_tlp_retran; 352 counter_u64_t rack_tlp_retran_bytes; 353 counter_u64_t rack_tlp_retran_fail; 354 counter_u64_t rack_to_tot; 355 counter_u64_t rack_to_arm_rack; 356 counter_u64_t rack_to_arm_tlp; 357 counter_u64_t rack_hot_alloc; 358 counter_u64_t rack_to_alloc; 359 counter_u64_t rack_to_alloc_hard; 360 counter_u64_t rack_to_alloc_emerg; 361 counter_u64_t rack_to_alloc_limited; 362 counter_u64_t rack_alloc_limited_conns; 363 counter_u64_t rack_split_limited; 364 365 #define MAX_NUM_OF_CNTS 13 366 counter_u64_t rack_proc_comp_ack[MAX_NUM_OF_CNTS]; 367 counter_u64_t rack_multi_single_eq; 368 counter_u64_t rack_proc_non_comp_ack; 369 370 counter_u64_t rack_fto_send; 371 counter_u64_t rack_fto_rsm_send; 372 counter_u64_t rack_nfto_resend; 373 counter_u64_t rack_non_fto_send; 374 counter_u64_t rack_extended_rfo; 375 376 counter_u64_t rack_sack_proc_all; 377 counter_u64_t rack_sack_proc_short; 378 counter_u64_t rack_sack_proc_restart; 379 counter_u64_t rack_sack_attacks_detected; 380 counter_u64_t rack_sack_attacks_reversed; 381 counter_u64_t rack_sack_used_next_merge; 382 counter_u64_t rack_sack_splits; 383 counter_u64_t rack_sack_used_prev_merge; 384 counter_u64_t rack_sack_skipped_acked; 385 counter_u64_t rack_ack_total; 386 counter_u64_t rack_express_sack; 387 counter_u64_t rack_sack_total; 388 counter_u64_t rack_move_none; 389 counter_u64_t rack_move_some; 390 391 counter_u64_t rack_used_tlpmethod; 392 counter_u64_t rack_used_tlpmethod2; 393 counter_u64_t rack_enter_tlp_calc; 394 counter_u64_t rack_input_idle_reduces; 395 counter_u64_t rack_collapsed_win; 396 counter_u64_t rack_tlp_does_nada; 397 counter_u64_t rack_try_scwnd; 398 counter_u64_t rack_hw_pace_init_fail; 399 counter_u64_t rack_hw_pace_lost; 400 counter_u64_t rack_sbsndptr_right; 401 counter_u64_t rack_sbsndptr_wrong; 402 403 /* Temp CPU counters */ 404 counter_u64_t rack_find_high; 405 406 counter_u64_t rack_progress_drops; 407 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE]; 408 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE]; 409 410 411 #define RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2))) 412 413 #define RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do { \ 414 (tv) = (value) + slop; \ 415 if ((u_long)(tv) < (u_long)(tvmin)) \ 416 (tv) = (tvmin); \ 417 if ((u_long)(tv) > (u_long)(tvmax)) \ 418 (tv) = (tvmax); \ 419 } while (0) 420 421 static void 422 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line); 423 424 static int 425 rack_process_ack(struct mbuf *m, struct tcphdr *th, 426 struct socket *so, struct tcpcb *tp, struct tcpopt *to, 427 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val); 428 static int 429 rack_process_data(struct mbuf *m, struct tcphdr *th, 430 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 431 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 432 static void 433 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, 434 uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery); 435 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack); 436 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack, 437 uint8_t limit_type); 438 static struct rack_sendmap * 439 rack_check_recovery_mode(struct tcpcb *tp, 440 uint32_t tsused); 441 static void 442 rack_cong_signal(struct tcpcb *tp, 443 uint32_t type, uint32_t ack); 444 static void rack_counter_destroy(void); 445 static int 446 rack_ctloutput(struct socket *so, struct sockopt *sopt, 447 struct inpcb *inp, struct tcpcb *tp); 448 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how); 449 static void 450 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override); 451 static void 452 rack_do_segment(struct mbuf *m, struct tcphdr *th, 453 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 454 uint8_t iptos); 455 static void rack_dtor(void *mem, int32_t size, void *arg); 456 static void 457 rack_log_alt_to_to_cancel(struct tcp_rack *rack, 458 uint32_t flex1, uint32_t flex2, 459 uint32_t flex3, uint32_t flex4, 460 uint32_t flex5, uint32_t flex6, 461 uint16_t flex7, uint8_t mod); 462 463 static void 464 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot, 465 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line, 466 struct rack_sendmap *rsm, uint8_t quality); 467 static struct rack_sendmap * 468 rack_find_high_nonack(struct tcp_rack *rack, 469 struct rack_sendmap *rsm); 470 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack); 471 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm); 472 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged); 473 static int 474 rack_get_sockopt(struct socket *so, struct sockopt *sopt, 475 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack); 476 static void 477 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack, 478 tcp_seq th_ack, int line, uint8_t quality); 479 static uint32_t 480 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss); 481 static int32_t rack_handoff_ok(struct tcpcb *tp); 482 static int32_t rack_init(struct tcpcb *tp); 483 static void rack_init_sysctls(void); 484 static void 485 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, 486 struct tcphdr *th, int entered_rec, int dup_ack_struck); 487 static void 488 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 489 uint32_t seq_out, uint8_t th_flags, int32_t err, uint64_t ts, 490 struct rack_sendmap *hintrsm, uint16_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls); 491 492 static void 493 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack, 494 struct rack_sendmap *rsm); 495 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm); 496 static int32_t rack_output(struct tcpcb *tp); 497 498 static uint32_t 499 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, 500 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm, 501 uint32_t cts, int *moved_two); 502 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq); 503 static void rack_remxt_tmr(struct tcpcb *tp); 504 static int 505 rack_set_sockopt(struct socket *so, struct sockopt *sopt, 506 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack); 507 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack); 508 static int32_t rack_stopall(struct tcpcb *tp); 509 static void 510 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, 511 uint32_t delta); 512 static int32_t rack_timer_active(struct tcpcb *tp, uint32_t timer_type); 513 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line); 514 static void rack_timer_stop(struct tcpcb *tp, uint32_t timer_type); 515 static uint32_t 516 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 517 struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint16_t add_flag); 518 static void 519 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 520 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag); 521 static int 522 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 523 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack); 524 static int32_t tcp_addrack(module_t mod, int32_t type, void *data); 525 static int 526 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, 527 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 528 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 529 static int 530 rack_do_closing(struct mbuf *m, struct tcphdr *th, 531 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 532 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 533 static int 534 rack_do_established(struct mbuf *m, struct tcphdr *th, 535 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 536 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 537 static int 538 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, 539 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 540 int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos); 541 static int 542 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, 543 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 544 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 545 static int 546 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, 547 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 548 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 549 static int 550 rack_do_lastack(struct mbuf *m, struct tcphdr *th, 551 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 552 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 553 static int 554 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, 555 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 556 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 557 static int 558 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, 559 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 560 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 561 struct rack_sendmap * 562 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, 563 uint32_t tsused); 564 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, 565 uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt); 566 static void 567 tcp_rack_partialack(struct tcpcb *tp); 568 static int 569 rack_set_profile(struct tcp_rack *rack, int prof); 570 static void 571 rack_apply_deferred_options(struct tcp_rack *rack); 572 573 int32_t rack_clear_counter=0; 574 575 static void 576 rack_set_cc_pacing(struct tcp_rack *rack) 577 { 578 struct sockopt sopt; 579 struct cc_newreno_opts opt; 580 struct newreno old, *ptr; 581 struct tcpcb *tp; 582 int error; 583 584 if (rack->rc_pacing_cc_set) 585 return; 586 587 tp = rack->rc_tp; 588 if (tp->cc_algo == NULL) { 589 /* Tcb is leaving */ 590 printf("No cc algorithm?\n"); 591 return; 592 } 593 rack->rc_pacing_cc_set = 1; 594 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) { 595 /* Not new-reno we can't play games with beta! */ 596 goto out; 597 } 598 ptr = ((struct newreno *)tp->ccv->cc_data); 599 if (CC_ALGO(tp)->ctl_output == NULL) { 600 /* Huh, why does new_reno no longer have a set function? */ 601 printf("no ctl_output for algo:%s\n", tp->cc_algo->name); 602 goto out; 603 } 604 if (ptr == NULL) { 605 /* Just the default values */ 606 old.beta = V_newreno_beta_ecn; 607 old.beta_ecn = V_newreno_beta_ecn; 608 old.newreno_flags = 0; 609 } else { 610 old.beta = ptr->beta; 611 old.beta_ecn = ptr->beta_ecn; 612 old.newreno_flags = ptr->newreno_flags; 613 } 614 sopt.sopt_valsize = sizeof(struct cc_newreno_opts); 615 sopt.sopt_dir = SOPT_SET; 616 opt.name = CC_NEWRENO_BETA; 617 opt.val = rack->r_ctl.rc_saved_beta.beta; 618 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt); 619 if (error) { 620 printf("Error returned by ctl_output %d\n", error); 621 goto out; 622 } 623 /* 624 * Hack alert we need to set in our newreno_flags 625 * so that Abe behavior is also applied. 626 */ 627 ((struct newreno *)tp->ccv->cc_data)->newreno_flags = CC_NEWRENO_BETA_ECN; 628 opt.name = CC_NEWRENO_BETA_ECN; 629 opt.val = rack->r_ctl.rc_saved_beta.beta_ecn; 630 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt); 631 if (error) { 632 printf("Error returned by ctl_output %d\n", error); 633 goto out; 634 } 635 /* Save off the original values for restoral */ 636 memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno)); 637 out: 638 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) { 639 union tcp_log_stackspecific log; 640 struct timeval tv; 641 642 ptr = ((struct newreno *)tp->ccv->cc_data); 643 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 644 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 645 if (ptr) { 646 log.u_bbr.flex1 = ptr->beta; 647 log.u_bbr.flex2 = ptr->beta_ecn; 648 log.u_bbr.flex3 = ptr->newreno_flags; 649 } 650 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta; 651 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn; 652 log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags; 653 log.u_bbr.flex7 = rack->gp_ready; 654 log.u_bbr.flex7 <<= 1; 655 log.u_bbr.flex7 |= rack->use_fixed_rate; 656 log.u_bbr.flex7 <<= 1; 657 log.u_bbr.flex7 |= rack->rc_pacing_cc_set; 658 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 659 log.u_bbr.flex8 = 3; 660 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, error, 661 0, &log, false, NULL, NULL, 0, &tv); 662 } 663 } 664 665 static void 666 rack_undo_cc_pacing(struct tcp_rack *rack) 667 { 668 struct newreno old, *ptr; 669 struct tcpcb *tp; 670 671 if (rack->rc_pacing_cc_set == 0) 672 return; 673 tp = rack->rc_tp; 674 rack->rc_pacing_cc_set = 0; 675 if (tp->cc_algo == NULL) 676 /* Tcb is leaving */ 677 return; 678 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) { 679 /* Not new-reno nothing to do! */ 680 return; 681 } 682 ptr = ((struct newreno *)tp->ccv->cc_data); 683 if (ptr == NULL) { 684 /* 685 * This happens at rack_fini() if the 686 * cc module gets freed on us. In that 687 * case we loose our "new" settings but 688 * thats ok, since the tcb is going away anyway. 689 */ 690 return; 691 } 692 /* Grab out our set values */ 693 memcpy(&old, ptr, sizeof(struct newreno)); 694 /* Copy back in the original values */ 695 memcpy(ptr, &rack->r_ctl.rc_saved_beta, sizeof(struct newreno)); 696 /* Now save back the values we had set in (for when pacing is restored) */ 697 memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno)); 698 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) { 699 union tcp_log_stackspecific log; 700 struct timeval tv; 701 702 ptr = ((struct newreno *)tp->ccv->cc_data); 703 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 704 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 705 log.u_bbr.flex1 = ptr->beta; 706 log.u_bbr.flex2 = ptr->beta_ecn; 707 log.u_bbr.flex3 = ptr->newreno_flags; 708 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta; 709 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn; 710 log.u_bbr.flex6 = rack->r_ctl.rc_saved_beta.newreno_flags; 711 log.u_bbr.flex7 = rack->gp_ready; 712 log.u_bbr.flex7 <<= 1; 713 log.u_bbr.flex7 |= rack->use_fixed_rate; 714 log.u_bbr.flex7 <<= 1; 715 log.u_bbr.flex7 |= rack->rc_pacing_cc_set; 716 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 717 log.u_bbr.flex8 = 4; 718 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 719 0, &log, false, NULL, NULL, 0, &tv); 720 } 721 } 722 723 #ifdef NETFLIX_PEAKRATE 724 static inline void 725 rack_update_peakrate_thr(struct tcpcb *tp) 726 { 727 /* Keep in mind that t_maxpeakrate is in B/s. */ 728 uint64_t peak; 729 peak = uqmax((tp->t_maxseg * 2), 730 (((uint64_t)tp->t_maxpeakrate * (uint64_t)(tp->t_srtt)) / (uint64_t)HPTS_USEC_IN_SEC)); 731 tp->t_peakrate_thr = (uint32_t)uqmin(peak, UINT32_MAX); 732 } 733 #endif 734 735 static int 736 sysctl_rack_clear(SYSCTL_HANDLER_ARGS) 737 { 738 uint32_t stat; 739 int32_t error; 740 int i; 741 742 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t)); 743 if (error || req->newptr == NULL) 744 return error; 745 746 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 747 if (error) 748 return (error); 749 if (stat == 1) { 750 #ifdef INVARIANTS 751 printf("Clearing RACK counters\n"); 752 #endif 753 counter_u64_zero(rack_badfr); 754 counter_u64_zero(rack_badfr_bytes); 755 counter_u64_zero(rack_rtm_prr_retran); 756 counter_u64_zero(rack_rtm_prr_newdata); 757 counter_u64_zero(rack_timestamp_mismatch); 758 counter_u64_zero(rack_reorder_seen); 759 counter_u64_zero(rack_tlp_tot); 760 counter_u64_zero(rack_tlp_newdata); 761 counter_u64_zero(rack_tlp_retran); 762 counter_u64_zero(rack_tlp_retran_bytes); 763 counter_u64_zero(rack_tlp_retran_fail); 764 counter_u64_zero(rack_to_tot); 765 counter_u64_zero(rack_to_arm_rack); 766 counter_u64_zero(rack_to_arm_tlp); 767 counter_u64_zero(rack_paced_segments); 768 counter_u64_zero(rack_calc_zero); 769 counter_u64_zero(rack_calc_nonzero); 770 counter_u64_zero(rack_unpaced_segments); 771 counter_u64_zero(rack_saw_enobuf); 772 counter_u64_zero(rack_saw_enobuf_hw); 773 counter_u64_zero(rack_saw_enetunreach); 774 counter_u64_zero(rack_per_timer_hole); 775 counter_u64_zero(rack_large_ackcmp); 776 counter_u64_zero(rack_small_ackcmp); 777 #ifdef INVARIANTS 778 counter_u64_zero(rack_adjust_map_bw); 779 #endif 780 counter_u64_zero(rack_to_alloc_hard); 781 counter_u64_zero(rack_to_alloc_emerg); 782 counter_u64_zero(rack_sack_proc_all); 783 counter_u64_zero(rack_fto_send); 784 counter_u64_zero(rack_fto_rsm_send); 785 counter_u64_zero(rack_extended_rfo); 786 counter_u64_zero(rack_hw_pace_init_fail); 787 counter_u64_zero(rack_hw_pace_lost); 788 counter_u64_zero(rack_sbsndptr_wrong); 789 counter_u64_zero(rack_sbsndptr_right); 790 counter_u64_zero(rack_non_fto_send); 791 counter_u64_zero(rack_nfto_resend); 792 counter_u64_zero(rack_sack_proc_short); 793 counter_u64_zero(rack_sack_proc_restart); 794 counter_u64_zero(rack_to_alloc); 795 counter_u64_zero(rack_to_alloc_limited); 796 counter_u64_zero(rack_alloc_limited_conns); 797 counter_u64_zero(rack_split_limited); 798 for (i = 0; i < MAX_NUM_OF_CNTS; i++) { 799 counter_u64_zero(rack_proc_comp_ack[i]); 800 } 801 counter_u64_zero(rack_multi_single_eq); 802 counter_u64_zero(rack_proc_non_comp_ack); 803 counter_u64_zero(rack_find_high); 804 counter_u64_zero(rack_sack_attacks_detected); 805 counter_u64_zero(rack_sack_attacks_reversed); 806 counter_u64_zero(rack_sack_used_next_merge); 807 counter_u64_zero(rack_sack_used_prev_merge); 808 counter_u64_zero(rack_sack_splits); 809 counter_u64_zero(rack_sack_skipped_acked); 810 counter_u64_zero(rack_ack_total); 811 counter_u64_zero(rack_express_sack); 812 counter_u64_zero(rack_sack_total); 813 counter_u64_zero(rack_move_none); 814 counter_u64_zero(rack_move_some); 815 counter_u64_zero(rack_used_tlpmethod); 816 counter_u64_zero(rack_used_tlpmethod2); 817 counter_u64_zero(rack_enter_tlp_calc); 818 counter_u64_zero(rack_progress_drops); 819 counter_u64_zero(rack_tlp_does_nada); 820 counter_u64_zero(rack_try_scwnd); 821 counter_u64_zero(rack_collapsed_win); 822 } 823 rack_clear_counter = 0; 824 return (0); 825 } 826 827 static void 828 rack_init_sysctls(void) 829 { 830 int i; 831 struct sysctl_oid *rack_counters; 832 struct sysctl_oid *rack_attack; 833 struct sysctl_oid *rack_pacing; 834 struct sysctl_oid *rack_timely; 835 struct sysctl_oid *rack_timers; 836 struct sysctl_oid *rack_tlp; 837 struct sysctl_oid *rack_misc; 838 struct sysctl_oid *rack_measure; 839 struct sysctl_oid *rack_probertt; 840 struct sysctl_oid *rack_hw_pacing; 841 842 rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 843 SYSCTL_CHILDREN(rack_sysctl_root), 844 OID_AUTO, 845 "sack_attack", 846 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 847 "Rack Sack Attack Counters and Controls"); 848 rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 849 SYSCTL_CHILDREN(rack_sysctl_root), 850 OID_AUTO, 851 "stats", 852 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 853 "Rack Counters"); 854 SYSCTL_ADD_S32(&rack_sysctl_ctx, 855 SYSCTL_CHILDREN(rack_sysctl_root), 856 OID_AUTO, "rate_sample_method", CTLFLAG_RW, 857 &rack_rate_sample_method , USE_RTT_LOW, 858 "What method should we use for rate sampling 0=high, 1=low "); 859 /* Probe rtt related controls */ 860 rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 861 SYSCTL_CHILDREN(rack_sysctl_root), 862 OID_AUTO, 863 "probertt", 864 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 865 "ProbeRTT related Controls"); 866 SYSCTL_ADD_U16(&rack_sysctl_ctx, 867 SYSCTL_CHILDREN(rack_probertt), 868 OID_AUTO, "exit_per_hpb", CTLFLAG_RW, 869 &rack_atexit_prtt_hbp, 130, 870 "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%"); 871 SYSCTL_ADD_U16(&rack_sysctl_ctx, 872 SYSCTL_CHILDREN(rack_probertt), 873 OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW, 874 &rack_atexit_prtt, 130, 875 "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%"); 876 SYSCTL_ADD_U16(&rack_sysctl_ctx, 877 SYSCTL_CHILDREN(rack_probertt), 878 OID_AUTO, "gp_per_mul", CTLFLAG_RW, 879 &rack_per_of_gp_probertt, 60, 880 "What percentage of goodput do we pace at in probertt"); 881 SYSCTL_ADD_U16(&rack_sysctl_ctx, 882 SYSCTL_CHILDREN(rack_probertt), 883 OID_AUTO, "gp_per_reduce", CTLFLAG_RW, 884 &rack_per_of_gp_probertt_reduce, 10, 885 "What percentage of goodput do we reduce every gp_srtt"); 886 SYSCTL_ADD_U16(&rack_sysctl_ctx, 887 SYSCTL_CHILDREN(rack_probertt), 888 OID_AUTO, "gp_per_low", CTLFLAG_RW, 889 &rack_per_of_gp_lowthresh, 40, 890 "What percentage of goodput do we allow the multiplier to fall to"); 891 SYSCTL_ADD_U32(&rack_sysctl_ctx, 892 SYSCTL_CHILDREN(rack_probertt), 893 OID_AUTO, "time_between", CTLFLAG_RW, 894 & rack_time_between_probertt, 96000000, 895 "How many useconds between the lowest rtt falling must past before we enter probertt"); 896 SYSCTL_ADD_U32(&rack_sysctl_ctx, 897 SYSCTL_CHILDREN(rack_probertt), 898 OID_AUTO, "safety", CTLFLAG_RW, 899 &rack_probe_rtt_safety_val, 2000000, 900 "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)"); 901 SYSCTL_ADD_U32(&rack_sysctl_ctx, 902 SYSCTL_CHILDREN(rack_probertt), 903 OID_AUTO, "sets_cwnd", CTLFLAG_RW, 904 &rack_probe_rtt_sets_cwnd, 0, 905 "Do we set the cwnd too (if always_lower is on)"); 906 SYSCTL_ADD_U32(&rack_sysctl_ctx, 907 SYSCTL_CHILDREN(rack_probertt), 908 OID_AUTO, "maxdrainsrtts", CTLFLAG_RW, 909 &rack_max_drain_wait, 2, 910 "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal"); 911 SYSCTL_ADD_U32(&rack_sysctl_ctx, 912 SYSCTL_CHILDREN(rack_probertt), 913 OID_AUTO, "mustdrainsrtts", CTLFLAG_RW, 914 &rack_must_drain, 1, 915 "We must drain this many gp_srtt's waiting for flight to reach goal"); 916 SYSCTL_ADD_U32(&rack_sysctl_ctx, 917 SYSCTL_CHILDREN(rack_probertt), 918 OID_AUTO, "goal_use_min_entry", CTLFLAG_RW, 919 &rack_probertt_use_min_rtt_entry, 1, 920 "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry"); 921 SYSCTL_ADD_U32(&rack_sysctl_ctx, 922 SYSCTL_CHILDREN(rack_probertt), 923 OID_AUTO, "goal_use_min_exit", CTLFLAG_RW, 924 &rack_probertt_use_min_rtt_exit, 0, 925 "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt"); 926 SYSCTL_ADD_U32(&rack_sysctl_ctx, 927 SYSCTL_CHILDREN(rack_probertt), 928 OID_AUTO, "length_div", CTLFLAG_RW, 929 &rack_probertt_gpsrtt_cnt_div, 0, 930 "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)"); 931 SYSCTL_ADD_U32(&rack_sysctl_ctx, 932 SYSCTL_CHILDREN(rack_probertt), 933 OID_AUTO, "length_mul", CTLFLAG_RW, 934 &rack_probertt_gpsrtt_cnt_mul, 0, 935 "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)"); 936 SYSCTL_ADD_U32(&rack_sysctl_ctx, 937 SYSCTL_CHILDREN(rack_probertt), 938 OID_AUTO, "holdtim_at_target", CTLFLAG_RW, 939 &rack_min_probertt_hold, 200000, 940 "What is the minimum time we hold probertt at target"); 941 SYSCTL_ADD_U32(&rack_sysctl_ctx, 942 SYSCTL_CHILDREN(rack_probertt), 943 OID_AUTO, "filter_life", CTLFLAG_RW, 944 &rack_probertt_filter_life, 10000000, 945 "What is the time for the filters life in useconds"); 946 SYSCTL_ADD_U32(&rack_sysctl_ctx, 947 SYSCTL_CHILDREN(rack_probertt), 948 OID_AUTO, "lower_within", CTLFLAG_RW, 949 &rack_probertt_lower_within, 10, 950 "If the rtt goes lower within this percentage of the time, go into probe-rtt"); 951 SYSCTL_ADD_U32(&rack_sysctl_ctx, 952 SYSCTL_CHILDREN(rack_probertt), 953 OID_AUTO, "must_move", CTLFLAG_RW, 954 &rack_min_rtt_movement, 250, 955 "How much is the minimum movement in rtt to count as a drop for probertt purposes"); 956 SYSCTL_ADD_U32(&rack_sysctl_ctx, 957 SYSCTL_CHILDREN(rack_probertt), 958 OID_AUTO, "clear_is_cnts", CTLFLAG_RW, 959 &rack_probertt_clear_is, 1, 960 "Do we clear I/S counts on exiting probe-rtt"); 961 SYSCTL_ADD_S32(&rack_sysctl_ctx, 962 SYSCTL_CHILDREN(rack_probertt), 963 OID_AUTO, "hbp_extra_drain", CTLFLAG_RW, 964 &rack_max_drain_hbp, 1, 965 "How many extra drain gpsrtt's do we get in highly buffered paths"); 966 SYSCTL_ADD_S32(&rack_sysctl_ctx, 967 SYSCTL_CHILDREN(rack_probertt), 968 OID_AUTO, "hbp_threshold", CTLFLAG_RW, 969 &rack_hbp_thresh, 3, 970 "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold"); 971 /* Pacing related sysctls */ 972 rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 973 SYSCTL_CHILDREN(rack_sysctl_root), 974 OID_AUTO, 975 "pacing", 976 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 977 "Pacing related Controls"); 978 SYSCTL_ADD_S32(&rack_sysctl_ctx, 979 SYSCTL_CHILDREN(rack_pacing), 980 OID_AUTO, "max_pace_over", CTLFLAG_RW, 981 &rack_max_per_above, 30, 982 "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)"); 983 SYSCTL_ADD_S32(&rack_sysctl_ctx, 984 SYSCTL_CHILDREN(rack_pacing), 985 OID_AUTO, "pace_to_one", CTLFLAG_RW, 986 &rack_pace_one_seg, 0, 987 "Do we allow low b/w pacing of 1MSS instead of two"); 988 SYSCTL_ADD_S32(&rack_sysctl_ctx, 989 SYSCTL_CHILDREN(rack_pacing), 990 OID_AUTO, "limit_wsrtt", CTLFLAG_RW, 991 &rack_limit_time_with_srtt, 0, 992 "Do we limit pacing time based on srtt"); 993 SYSCTL_ADD_S32(&rack_sysctl_ctx, 994 SYSCTL_CHILDREN(rack_pacing), 995 OID_AUTO, "init_win", CTLFLAG_RW, 996 &rack_default_init_window, 0, 997 "Do we have a rack initial window 0 = system default"); 998 SYSCTL_ADD_U16(&rack_sysctl_ctx, 999 SYSCTL_CHILDREN(rack_pacing), 1000 OID_AUTO, "gp_per_ss", CTLFLAG_RW, 1001 &rack_per_of_gp_ss, 250, 1002 "If non zero, what percentage of goodput to pace at in slow start"); 1003 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1004 SYSCTL_CHILDREN(rack_pacing), 1005 OID_AUTO, "gp_per_ca", CTLFLAG_RW, 1006 &rack_per_of_gp_ca, 150, 1007 "If non zero, what percentage of goodput to pace at in congestion avoidance"); 1008 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1009 SYSCTL_CHILDREN(rack_pacing), 1010 OID_AUTO, "gp_per_rec", CTLFLAG_RW, 1011 &rack_per_of_gp_rec, 200, 1012 "If non zero, what percentage of goodput to pace at in recovery"); 1013 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1014 SYSCTL_CHILDREN(rack_pacing), 1015 OID_AUTO, "pace_max_seg", CTLFLAG_RW, 1016 &rack_hptsi_segments, 40, 1017 "What size is the max for TSO segments in pacing and burst mitigation"); 1018 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1019 SYSCTL_CHILDREN(rack_pacing), 1020 OID_AUTO, "burst_reduces", CTLFLAG_RW, 1021 &rack_slot_reduction, 4, 1022 "When doing only burst mitigation what is the reduce divisor"); 1023 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1024 SYSCTL_CHILDREN(rack_sysctl_root), 1025 OID_AUTO, "use_pacing", CTLFLAG_RW, 1026 &rack_pace_every_seg, 0, 1027 "If set we use pacing, if clear we use only the original burst mitigation"); 1028 SYSCTL_ADD_U64(&rack_sysctl_ctx, 1029 SYSCTL_CHILDREN(rack_pacing), 1030 OID_AUTO, "rate_cap", CTLFLAG_RW, 1031 &rack_bw_rate_cap, 0, 1032 "If set we apply this value to the absolute rate cap used by pacing"); 1033 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1034 SYSCTL_CHILDREN(rack_sysctl_root), 1035 OID_AUTO, "req_measure_cnt", CTLFLAG_RW, 1036 &rack_req_measurements, 1, 1037 "If doing dynamic pacing, how many measurements must be in before we start pacing?"); 1038 /* Hardware pacing */ 1039 rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1040 SYSCTL_CHILDREN(rack_sysctl_root), 1041 OID_AUTO, 1042 "hdwr_pacing", 1043 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1044 "Pacing related Controls"); 1045 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1046 SYSCTL_CHILDREN(rack_hw_pacing), 1047 OID_AUTO, "rwnd_factor", CTLFLAG_RW, 1048 &rack_hw_rwnd_factor, 2, 1049 "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?"); 1050 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1051 SYSCTL_CHILDREN(rack_hw_pacing), 1052 OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW, 1053 &rack_enobuf_hw_boost_mult, 2, 1054 "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?"); 1055 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1056 SYSCTL_CHILDREN(rack_hw_pacing), 1057 OID_AUTO, "pace_enobuf_max", CTLFLAG_RW, 1058 &rack_enobuf_hw_max, 2, 1059 "What is the max boost the pacing time if we see a ENOBUFS?"); 1060 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1061 SYSCTL_CHILDREN(rack_hw_pacing), 1062 OID_AUTO, "pace_enobuf_min", CTLFLAG_RW, 1063 &rack_enobuf_hw_min, 2, 1064 "What is the min boost the pacing time if we see a ENOBUFS?"); 1065 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1066 SYSCTL_CHILDREN(rack_hw_pacing), 1067 OID_AUTO, "enable", CTLFLAG_RW, 1068 &rack_enable_hw_pacing, 0, 1069 "Should RACK attempt to use hw pacing?"); 1070 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1071 SYSCTL_CHILDREN(rack_hw_pacing), 1072 OID_AUTO, "rate_cap", CTLFLAG_RW, 1073 &rack_hw_rate_caps, 1, 1074 "Does the highest hardware pacing rate cap the rate we will send at??"); 1075 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1076 SYSCTL_CHILDREN(rack_hw_pacing), 1077 OID_AUTO, "rate_min", CTLFLAG_RW, 1078 &rack_hw_rate_min, 0, 1079 "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?"); 1080 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1081 SYSCTL_CHILDREN(rack_hw_pacing), 1082 OID_AUTO, "rate_to_low", CTLFLAG_RW, 1083 &rack_hw_rate_to_low, 0, 1084 "If we fall below this rate, dis-engage hw pacing?"); 1085 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1086 SYSCTL_CHILDREN(rack_hw_pacing), 1087 OID_AUTO, "up_only", CTLFLAG_RW, 1088 &rack_hw_up_only, 1, 1089 "Do we allow hw pacing to lower the rate selected?"); 1090 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1091 SYSCTL_CHILDREN(rack_hw_pacing), 1092 OID_AUTO, "extra_mss_precise", CTLFLAG_RW, 1093 &rack_hw_pace_extra_slots, 2, 1094 "If the rates between software and hardware match precisely how many extra time_betweens do we get?"); 1095 rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1096 SYSCTL_CHILDREN(rack_sysctl_root), 1097 OID_AUTO, 1098 "timely", 1099 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1100 "Rack Timely RTT Controls"); 1101 /* Timely based GP dynmics */ 1102 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1103 SYSCTL_CHILDREN(rack_timely), 1104 OID_AUTO, "upper", CTLFLAG_RW, 1105 &rack_gp_per_bw_mul_up, 2, 1106 "Rack timely upper range for equal b/w (in percentage)"); 1107 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1108 SYSCTL_CHILDREN(rack_timely), 1109 OID_AUTO, "lower", CTLFLAG_RW, 1110 &rack_gp_per_bw_mul_down, 4, 1111 "Rack timely lower range for equal b/w (in percentage)"); 1112 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1113 SYSCTL_CHILDREN(rack_timely), 1114 OID_AUTO, "rtt_max_mul", CTLFLAG_RW, 1115 &rack_gp_rtt_maxmul, 3, 1116 "Rack timely multipler of lowest rtt for rtt_max"); 1117 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1118 SYSCTL_CHILDREN(rack_timely), 1119 OID_AUTO, "rtt_min_div", CTLFLAG_RW, 1120 &rack_gp_rtt_mindiv, 4, 1121 "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt"); 1122 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1123 SYSCTL_CHILDREN(rack_timely), 1124 OID_AUTO, "rtt_min_mul", CTLFLAG_RW, 1125 &rack_gp_rtt_minmul, 1, 1126 "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt"); 1127 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1128 SYSCTL_CHILDREN(rack_timely), 1129 OID_AUTO, "decrease", CTLFLAG_RW, 1130 &rack_gp_decrease_per, 20, 1131 "Rack timely decrease percentage of our GP multiplication factor"); 1132 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1133 SYSCTL_CHILDREN(rack_timely), 1134 OID_AUTO, "increase", CTLFLAG_RW, 1135 &rack_gp_increase_per, 2, 1136 "Rack timely increase perentage of our GP multiplication factor"); 1137 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1138 SYSCTL_CHILDREN(rack_timely), 1139 OID_AUTO, "lowerbound", CTLFLAG_RW, 1140 &rack_per_lower_bound, 50, 1141 "Rack timely lowest percentage we allow GP multiplier to fall to"); 1142 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1143 SYSCTL_CHILDREN(rack_timely), 1144 OID_AUTO, "upperboundss", CTLFLAG_RW, 1145 &rack_per_upper_bound_ss, 0, 1146 "Rack timely higest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)"); 1147 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1148 SYSCTL_CHILDREN(rack_timely), 1149 OID_AUTO, "upperboundca", CTLFLAG_RW, 1150 &rack_per_upper_bound_ca, 0, 1151 "Rack timely higest percentage we allow GP multiplier to CA raise to (0 is no upperbound)"); 1152 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1153 SYSCTL_CHILDREN(rack_timely), 1154 OID_AUTO, "dynamicgp", CTLFLAG_RW, 1155 &rack_do_dyn_mul, 0, 1156 "Rack timely do we enable dynmaic timely goodput by default"); 1157 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1158 SYSCTL_CHILDREN(rack_timely), 1159 OID_AUTO, "no_rec_red", CTLFLAG_RW, 1160 &rack_gp_no_rec_chg, 1, 1161 "Rack timely do we prohibit the recovery multiplier from being lowered"); 1162 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1163 SYSCTL_CHILDREN(rack_timely), 1164 OID_AUTO, "red_clear_cnt", CTLFLAG_RW, 1165 &rack_timely_dec_clear, 6, 1166 "Rack timely what threshold do we count to before another boost during b/w decent"); 1167 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1168 SYSCTL_CHILDREN(rack_timely), 1169 OID_AUTO, "max_push_rise", CTLFLAG_RW, 1170 &rack_timely_max_push_rise, 3, 1171 "Rack timely how many times do we push up with b/w increase"); 1172 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1173 SYSCTL_CHILDREN(rack_timely), 1174 OID_AUTO, "max_push_drop", CTLFLAG_RW, 1175 &rack_timely_max_push_drop, 3, 1176 "Rack timely how many times do we push back on b/w decent"); 1177 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1178 SYSCTL_CHILDREN(rack_timely), 1179 OID_AUTO, "min_segs", CTLFLAG_RW, 1180 &rack_timely_min_segs, 4, 1181 "Rack timely when setting the cwnd what is the min num segments"); 1182 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1183 SYSCTL_CHILDREN(rack_timely), 1184 OID_AUTO, "noback_max", CTLFLAG_RW, 1185 &rack_use_max_for_nobackoff, 0, 1186 "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min"); 1187 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1188 SYSCTL_CHILDREN(rack_timely), 1189 OID_AUTO, "interim_timely_only", CTLFLAG_RW, 1190 &rack_timely_int_timely_only, 0, 1191 "Rack timely when doing interim timely's do we only do timely (no b/w consideration)"); 1192 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1193 SYSCTL_CHILDREN(rack_timely), 1194 OID_AUTO, "nonstop", CTLFLAG_RW, 1195 &rack_timely_no_stopping, 0, 1196 "Rack timely don't stop increase"); 1197 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1198 SYSCTL_CHILDREN(rack_timely), 1199 OID_AUTO, "dec_raise_thresh", CTLFLAG_RW, 1200 &rack_down_raise_thresh, 100, 1201 "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)"); 1202 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1203 SYSCTL_CHILDREN(rack_timely), 1204 OID_AUTO, "bottom_drag_segs", CTLFLAG_RW, 1205 &rack_req_segs, 1, 1206 "Bottom dragging if not these many segments outstanding and room"); 1207 1208 /* TLP and Rack related parameters */ 1209 rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1210 SYSCTL_CHILDREN(rack_sysctl_root), 1211 OID_AUTO, 1212 "tlp", 1213 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1214 "TLP and Rack related Controls"); 1215 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1216 SYSCTL_CHILDREN(rack_tlp), 1217 OID_AUTO, "use_rrr", CTLFLAG_RW, 1218 &use_rack_rr, 1, 1219 "Do we use Rack Rapid Recovery"); 1220 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1221 SYSCTL_CHILDREN(rack_tlp), 1222 OID_AUTO, "post_rec_labc", CTLFLAG_RW, 1223 &rack_max_abc_post_recovery, 2, 1224 "Since we do early recovery, do we override the l_abc to a value, if so what?"); 1225 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1226 SYSCTL_CHILDREN(rack_tlp), 1227 OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW, 1228 &rack_non_rxt_use_cr, 0, 1229 "Do we use ss/ca rate if in recovery we are transmitting a new data chunk"); 1230 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1231 SYSCTL_CHILDREN(rack_tlp), 1232 OID_AUTO, "tlpmethod", CTLFLAG_RW, 1233 &rack_tlp_threshold_use, TLP_USE_TWO_ONE, 1234 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2"); 1235 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1236 SYSCTL_CHILDREN(rack_tlp), 1237 OID_AUTO, "limit", CTLFLAG_RW, 1238 &rack_tlp_limit, 2, 1239 "How many TLP's can be sent without sending new data"); 1240 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1241 SYSCTL_CHILDREN(rack_tlp), 1242 OID_AUTO, "use_greater", CTLFLAG_RW, 1243 &rack_tlp_use_greater, 1, 1244 "Should we use the rack_rtt time if its greater than srtt"); 1245 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1246 SYSCTL_CHILDREN(rack_tlp), 1247 OID_AUTO, "tlpminto", CTLFLAG_RW, 1248 &rack_tlp_min, 10000, 1249 "TLP minimum timeout per the specification (in microseconds)"); 1250 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1251 SYSCTL_CHILDREN(rack_tlp), 1252 OID_AUTO, "send_oldest", CTLFLAG_RW, 1253 &rack_always_send_oldest, 0, 1254 "Should we always send the oldest TLP and RACK-TLP"); 1255 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1256 SYSCTL_CHILDREN(rack_tlp), 1257 OID_AUTO, "rack_tlimit", CTLFLAG_RW, 1258 &rack_limited_retran, 0, 1259 "How many times can a rack timeout drive out sends"); 1260 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1261 SYSCTL_CHILDREN(rack_tlp), 1262 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW, 1263 &rack_lower_cwnd_at_tlp, 0, 1264 "When a TLP completes a retran should we enter recovery"); 1265 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1266 SYSCTL_CHILDREN(rack_tlp), 1267 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1268 &rack_reorder_thresh, 2, 1269 "What factor for rack will be added when seeing reordering (shift right)"); 1270 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1271 SYSCTL_CHILDREN(rack_tlp), 1272 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1273 &rack_tlp_thresh, 1, 1274 "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1275 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1276 SYSCTL_CHILDREN(rack_tlp), 1277 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1278 &rack_reorder_fade, 60000000, 1279 "Does reorder detection fade, if so how many microseconds (0 means never)"); 1280 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1281 SYSCTL_CHILDREN(rack_tlp), 1282 OID_AUTO, "pktdelay", CTLFLAG_RW, 1283 &rack_pkt_delay, 1000, 1284 "Extra RACK time (in microseconds) besides reordering thresh"); 1285 1286 /* Timer related controls */ 1287 rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1288 SYSCTL_CHILDREN(rack_sysctl_root), 1289 OID_AUTO, 1290 "timers", 1291 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1292 "Timer related controls"); 1293 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1294 SYSCTL_CHILDREN(rack_timers), 1295 OID_AUTO, "persmin", CTLFLAG_RW, 1296 &rack_persist_min, 250000, 1297 "What is the minimum time in microseconds between persists"); 1298 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1299 SYSCTL_CHILDREN(rack_timers), 1300 OID_AUTO, "persmax", CTLFLAG_RW, 1301 &rack_persist_max, 2000000, 1302 "What is the largest delay in microseconds between persists"); 1303 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1304 SYSCTL_CHILDREN(rack_timers), 1305 OID_AUTO, "delayed_ack", CTLFLAG_RW, 1306 &rack_delayed_ack_time, 40000, 1307 "Delayed ack time (40ms in microseconds)"); 1308 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1309 SYSCTL_CHILDREN(rack_timers), 1310 OID_AUTO, "minrto", CTLFLAG_RW, 1311 &rack_rto_min, 30000, 1312 "Minimum RTO in microseconds -- set with caution below 1000 due to TLP"); 1313 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1314 SYSCTL_CHILDREN(rack_timers), 1315 OID_AUTO, "maxrto", CTLFLAG_RW, 1316 &rack_rto_max, 4000000, 1317 "Maxiumum RTO in microseconds -- should be at least as large as min_rto"); 1318 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1319 SYSCTL_CHILDREN(rack_timers), 1320 OID_AUTO, "minto", CTLFLAG_RW, 1321 &rack_min_to, 1000, 1322 "Minimum rack timeout in microseconds"); 1323 /* Measure controls */ 1324 rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1325 SYSCTL_CHILDREN(rack_sysctl_root), 1326 OID_AUTO, 1327 "measure", 1328 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1329 "Measure related controls"); 1330 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1331 SYSCTL_CHILDREN(rack_measure), 1332 OID_AUTO, "wma_divisor", CTLFLAG_RW, 1333 &rack_wma_divisor, 8, 1334 "When doing b/w calculation what is the divisor for the WMA"); 1335 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1336 SYSCTL_CHILDREN(rack_measure), 1337 OID_AUTO, "end_cwnd", CTLFLAG_RW, 1338 &rack_cwnd_block_ends_measure, 0, 1339 "Does a cwnd just-return end the measurement window (app limited)"); 1340 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1341 SYSCTL_CHILDREN(rack_measure), 1342 OID_AUTO, "end_rwnd", CTLFLAG_RW, 1343 &rack_rwnd_block_ends_measure, 0, 1344 "Does an rwnd just-return end the measurement window (app limited -- not persists)"); 1345 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1346 SYSCTL_CHILDREN(rack_measure), 1347 OID_AUTO, "min_target", CTLFLAG_RW, 1348 &rack_def_data_window, 20, 1349 "What is the minimum target window (in mss) for a GP measurements"); 1350 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1351 SYSCTL_CHILDREN(rack_measure), 1352 OID_AUTO, "goal_bdp", CTLFLAG_RW, 1353 &rack_goal_bdp, 2, 1354 "What is the goal BDP to measure"); 1355 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1356 SYSCTL_CHILDREN(rack_measure), 1357 OID_AUTO, "min_srtts", CTLFLAG_RW, 1358 &rack_min_srtts, 1, 1359 "What is the goal BDP to measure"); 1360 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1361 SYSCTL_CHILDREN(rack_measure), 1362 OID_AUTO, "min_measure_tim", CTLFLAG_RW, 1363 &rack_min_measure_usec, 0, 1364 "What is the Minimum time time for a measurement if 0, this is off"); 1365 /* Misc rack controls */ 1366 rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1367 SYSCTL_CHILDREN(rack_sysctl_root), 1368 OID_AUTO, 1369 "misc", 1370 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1371 "Misc related controls"); 1372 #ifdef TCP_ACCOUNTING 1373 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1374 SYSCTL_CHILDREN(rack_misc), 1375 OID_AUTO, "tcp_acct", CTLFLAG_RW, 1376 &rack_tcp_accounting, 0, 1377 "Should we turn on TCP accounting for all rack sessions?"); 1378 #endif 1379 1380 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1381 SYSCTL_CHILDREN(rack_misc), 1382 OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW, 1383 &rack_dsack_std_based, 3, 1384 "How do we process dsack with respect to rack timers, bit field, 3 is standards based?"); 1385 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1386 SYSCTL_CHILDREN(rack_misc), 1387 OID_AUTO, "prr_addback_max", CTLFLAG_RW, 1388 &rack_prr_addbackmax, 2, 1389 "What is the maximum number of MSS we allow to be added back if prr can't send all its data?"); 1390 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1391 SYSCTL_CHILDREN(rack_misc), 1392 OID_AUTO, "stats_gets_ms", CTLFLAG_RW, 1393 &rack_stats_gets_ms_rtt, 1, 1394 "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?"); 1395 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1396 SYSCTL_CHILDREN(rack_misc), 1397 OID_AUTO, "clientlowbuf", CTLFLAG_RW, 1398 &rack_client_low_buf, 0, 1399 "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?"); 1400 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1401 SYSCTL_CHILDREN(rack_misc), 1402 OID_AUTO, "defprofile", CTLFLAG_RW, 1403 &rack_def_profile, 0, 1404 "Should RACK use a default profile (0=no, num == profile num)?"); 1405 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1406 SYSCTL_CHILDREN(rack_misc), 1407 OID_AUTO, "cmpack", CTLFLAG_RW, 1408 &rack_use_cmp_acks, 1, 1409 "Should RACK have LRO send compressed acks"); 1410 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1411 SYSCTL_CHILDREN(rack_misc), 1412 OID_AUTO, "fsb", CTLFLAG_RW, 1413 &rack_use_fsb, 1, 1414 "Should RACK use the fast send block?"); 1415 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1416 SYSCTL_CHILDREN(rack_misc), 1417 OID_AUTO, "rfo", CTLFLAG_RW, 1418 &rack_use_rfo, 1, 1419 "Should RACK use rack_fast_output()?"); 1420 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1421 SYSCTL_CHILDREN(rack_misc), 1422 OID_AUTO, "rsmrfo", CTLFLAG_RW, 1423 &rack_use_rsm_rfo, 1, 1424 "Should RACK use rack_fast_rsm_output()?"); 1425 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1426 SYSCTL_CHILDREN(rack_misc), 1427 OID_AUTO, "shared_cwnd", CTLFLAG_RW, 1428 &rack_enable_shared_cwnd, 1, 1429 "Should RACK try to use the shared cwnd on connections where allowed"); 1430 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1431 SYSCTL_CHILDREN(rack_misc), 1432 OID_AUTO, "limits_on_scwnd", CTLFLAG_RW, 1433 &rack_limits_scwnd, 1, 1434 "Should RACK place low end time limits on the shared cwnd feature"); 1435 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1436 SYSCTL_CHILDREN(rack_misc), 1437 OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW, 1438 &rack_enable_mqueue_for_nonpaced, 0, 1439 "Should RACK use mbuf queuing for non-paced connections"); 1440 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1441 SYSCTL_CHILDREN(rack_misc), 1442 OID_AUTO, "iMac_dack", CTLFLAG_RW, 1443 &rack_use_imac_dack, 0, 1444 "Should RACK try to emulate iMac delayed ack"); 1445 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1446 SYSCTL_CHILDREN(rack_misc), 1447 OID_AUTO, "no_prr", CTLFLAG_RW, 1448 &rack_disable_prr, 0, 1449 "Should RACK not use prr and only pace (must have pacing on)"); 1450 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1451 SYSCTL_CHILDREN(rack_misc), 1452 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1453 &rack_verbose_logging, 0, 1454 "Should RACK black box logging be verbose"); 1455 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1456 SYSCTL_CHILDREN(rack_misc), 1457 OID_AUTO, "data_after_close", CTLFLAG_RW, 1458 &rack_ignore_data_after_close, 1, 1459 "Do we hold off sending a RST until all pending data is ack'd"); 1460 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1461 SYSCTL_CHILDREN(rack_misc), 1462 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1463 &rack_sack_not_required, 1, 1464 "Do we allow rack to run on connections not supporting SACK"); 1465 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1466 SYSCTL_CHILDREN(rack_misc), 1467 OID_AUTO, "prr_sendalot", CTLFLAG_RW, 1468 &rack_send_a_lot_in_prr, 1, 1469 "Send a lot in prr"); 1470 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1471 SYSCTL_CHILDREN(rack_misc), 1472 OID_AUTO, "autoscale", CTLFLAG_RW, 1473 &rack_autosndbuf_inc, 20, 1474 "What percentage should rack scale up its snd buffer by?"); 1475 /* Sack Attacker detection stuff */ 1476 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1477 SYSCTL_CHILDREN(rack_attack), 1478 OID_AUTO, "detect_highsackratio", CTLFLAG_RW, 1479 &rack_highest_sack_thresh_seen, 0, 1480 "Highest sack to ack ratio seen"); 1481 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1482 SYSCTL_CHILDREN(rack_attack), 1483 OID_AUTO, "detect_highmoveratio", CTLFLAG_RW, 1484 &rack_highest_move_thresh_seen, 0, 1485 "Highest move to non-move ratio seen"); 1486 rack_ack_total = counter_u64_alloc(M_WAITOK); 1487 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1488 SYSCTL_CHILDREN(rack_attack), 1489 OID_AUTO, "acktotal", CTLFLAG_RD, 1490 &rack_ack_total, 1491 "Total number of Ack's"); 1492 rack_express_sack = counter_u64_alloc(M_WAITOK); 1493 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1494 SYSCTL_CHILDREN(rack_attack), 1495 OID_AUTO, "exp_sacktotal", CTLFLAG_RD, 1496 &rack_express_sack, 1497 "Total expresss number of Sack's"); 1498 rack_sack_total = counter_u64_alloc(M_WAITOK); 1499 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1500 SYSCTL_CHILDREN(rack_attack), 1501 OID_AUTO, "sacktotal", CTLFLAG_RD, 1502 &rack_sack_total, 1503 "Total number of SACKs"); 1504 rack_move_none = counter_u64_alloc(M_WAITOK); 1505 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1506 SYSCTL_CHILDREN(rack_attack), 1507 OID_AUTO, "move_none", CTLFLAG_RD, 1508 &rack_move_none, 1509 "Total number of SACK index reuse of postions under threshold"); 1510 rack_move_some = counter_u64_alloc(M_WAITOK); 1511 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1512 SYSCTL_CHILDREN(rack_attack), 1513 OID_AUTO, "move_some", CTLFLAG_RD, 1514 &rack_move_some, 1515 "Total number of SACK index reuse of postions over threshold"); 1516 rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK); 1517 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1518 SYSCTL_CHILDREN(rack_attack), 1519 OID_AUTO, "attacks", CTLFLAG_RD, 1520 &rack_sack_attacks_detected, 1521 "Total number of SACK attackers that had sack disabled"); 1522 rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK); 1523 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1524 SYSCTL_CHILDREN(rack_attack), 1525 OID_AUTO, "reversed", CTLFLAG_RD, 1526 &rack_sack_attacks_reversed, 1527 "Total number of SACK attackers that were later determined false positive"); 1528 rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK); 1529 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1530 SYSCTL_CHILDREN(rack_attack), 1531 OID_AUTO, "nextmerge", CTLFLAG_RD, 1532 &rack_sack_used_next_merge, 1533 "Total number of times we used the next merge"); 1534 rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK); 1535 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1536 SYSCTL_CHILDREN(rack_attack), 1537 OID_AUTO, "prevmerge", CTLFLAG_RD, 1538 &rack_sack_used_prev_merge, 1539 "Total number of times we used the prev merge"); 1540 /* Counters */ 1541 rack_fto_send = counter_u64_alloc(M_WAITOK); 1542 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1543 SYSCTL_CHILDREN(rack_counters), 1544 OID_AUTO, "fto_send", CTLFLAG_RD, 1545 &rack_fto_send, "Total number of rack_fast_output sends"); 1546 rack_fto_rsm_send = counter_u64_alloc(M_WAITOK); 1547 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1548 SYSCTL_CHILDREN(rack_counters), 1549 OID_AUTO, "fto_rsm_send", CTLFLAG_RD, 1550 &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends"); 1551 rack_nfto_resend = counter_u64_alloc(M_WAITOK); 1552 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1553 SYSCTL_CHILDREN(rack_counters), 1554 OID_AUTO, "nfto_resend", CTLFLAG_RD, 1555 &rack_nfto_resend, "Total number of rack_output retransmissions"); 1556 rack_non_fto_send = counter_u64_alloc(M_WAITOK); 1557 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1558 SYSCTL_CHILDREN(rack_counters), 1559 OID_AUTO, "nfto_send", CTLFLAG_RD, 1560 &rack_non_fto_send, "Total number of rack_output first sends"); 1561 rack_extended_rfo = counter_u64_alloc(M_WAITOK); 1562 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1563 SYSCTL_CHILDREN(rack_counters), 1564 OID_AUTO, "rfo_extended", CTLFLAG_RD, 1565 &rack_extended_rfo, "Total number of times we extended rfo"); 1566 1567 rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK); 1568 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1569 SYSCTL_CHILDREN(rack_counters), 1570 OID_AUTO, "hwpace_init_fail", CTLFLAG_RD, 1571 &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing"); 1572 rack_hw_pace_lost = counter_u64_alloc(M_WAITOK); 1573 1574 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1575 SYSCTL_CHILDREN(rack_counters), 1576 OID_AUTO, "hwpace_lost", CTLFLAG_RD, 1577 &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing"); 1578 rack_badfr = counter_u64_alloc(M_WAITOK); 1579 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1580 SYSCTL_CHILDREN(rack_counters), 1581 OID_AUTO, "badfr", CTLFLAG_RD, 1582 &rack_badfr, "Total number of bad FRs"); 1583 rack_badfr_bytes = counter_u64_alloc(M_WAITOK); 1584 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1585 SYSCTL_CHILDREN(rack_counters), 1586 OID_AUTO, "badfr_bytes", CTLFLAG_RD, 1587 &rack_badfr_bytes, "Total number of bad FRs"); 1588 rack_rtm_prr_retran = counter_u64_alloc(M_WAITOK); 1589 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1590 SYSCTL_CHILDREN(rack_counters), 1591 OID_AUTO, "prrsndret", CTLFLAG_RD, 1592 &rack_rtm_prr_retran, 1593 "Total number of prr based retransmits"); 1594 rack_rtm_prr_newdata = counter_u64_alloc(M_WAITOK); 1595 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1596 SYSCTL_CHILDREN(rack_counters), 1597 OID_AUTO, "prrsndnew", CTLFLAG_RD, 1598 &rack_rtm_prr_newdata, 1599 "Total number of prr based new transmits"); 1600 rack_timestamp_mismatch = counter_u64_alloc(M_WAITOK); 1601 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1602 SYSCTL_CHILDREN(rack_counters), 1603 OID_AUTO, "tsnf", CTLFLAG_RD, 1604 &rack_timestamp_mismatch, 1605 "Total number of timestamps that we could not find the reported ts"); 1606 rack_find_high = counter_u64_alloc(M_WAITOK); 1607 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1608 SYSCTL_CHILDREN(rack_counters), 1609 OID_AUTO, "findhigh", CTLFLAG_RD, 1610 &rack_find_high, 1611 "Total number of FIN causing find-high"); 1612 rack_reorder_seen = counter_u64_alloc(M_WAITOK); 1613 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1614 SYSCTL_CHILDREN(rack_counters), 1615 OID_AUTO, "reordering", CTLFLAG_RD, 1616 &rack_reorder_seen, 1617 "Total number of times we added delay due to reordering"); 1618 rack_tlp_tot = counter_u64_alloc(M_WAITOK); 1619 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1620 SYSCTL_CHILDREN(rack_counters), 1621 OID_AUTO, "tlp_to_total", CTLFLAG_RD, 1622 &rack_tlp_tot, 1623 "Total number of tail loss probe expirations"); 1624 rack_tlp_newdata = counter_u64_alloc(M_WAITOK); 1625 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1626 SYSCTL_CHILDREN(rack_counters), 1627 OID_AUTO, "tlp_new", CTLFLAG_RD, 1628 &rack_tlp_newdata, 1629 "Total number of tail loss probe sending new data"); 1630 rack_tlp_retran = counter_u64_alloc(M_WAITOK); 1631 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1632 SYSCTL_CHILDREN(rack_counters), 1633 OID_AUTO, "tlp_retran", CTLFLAG_RD, 1634 &rack_tlp_retran, 1635 "Total number of tail loss probe sending retransmitted data"); 1636 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK); 1637 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1638 SYSCTL_CHILDREN(rack_counters), 1639 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD, 1640 &rack_tlp_retran_bytes, 1641 "Total bytes of tail loss probe sending retransmitted data"); 1642 rack_tlp_retran_fail = counter_u64_alloc(M_WAITOK); 1643 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1644 SYSCTL_CHILDREN(rack_counters), 1645 OID_AUTO, "tlp_retran_fail", CTLFLAG_RD, 1646 &rack_tlp_retran_fail, 1647 "Total number of tail loss probe sending retransmitted data that failed (wait for t3)"); 1648 rack_to_tot = counter_u64_alloc(M_WAITOK); 1649 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1650 SYSCTL_CHILDREN(rack_counters), 1651 OID_AUTO, "rack_to_tot", CTLFLAG_RD, 1652 &rack_to_tot, 1653 "Total number of times the rack to expired"); 1654 rack_to_arm_rack = counter_u64_alloc(M_WAITOK); 1655 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1656 SYSCTL_CHILDREN(rack_counters), 1657 OID_AUTO, "arm_rack", CTLFLAG_RD, 1658 &rack_to_arm_rack, 1659 "Total number of times the rack timer armed"); 1660 rack_to_arm_tlp = counter_u64_alloc(M_WAITOK); 1661 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1662 SYSCTL_CHILDREN(rack_counters), 1663 OID_AUTO, "arm_tlp", CTLFLAG_RD, 1664 &rack_to_arm_tlp, 1665 "Total number of times the tlp timer armed"); 1666 rack_calc_zero = counter_u64_alloc(M_WAITOK); 1667 rack_calc_nonzero = counter_u64_alloc(M_WAITOK); 1668 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1669 SYSCTL_CHILDREN(rack_counters), 1670 OID_AUTO, "calc_zero", CTLFLAG_RD, 1671 &rack_calc_zero, 1672 "Total number of times pacing time worked out to zero"); 1673 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1674 SYSCTL_CHILDREN(rack_counters), 1675 OID_AUTO, "calc_nonzero", CTLFLAG_RD, 1676 &rack_calc_nonzero, 1677 "Total number of times pacing time worked out to non-zero"); 1678 rack_paced_segments = counter_u64_alloc(M_WAITOK); 1679 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1680 SYSCTL_CHILDREN(rack_counters), 1681 OID_AUTO, "paced", CTLFLAG_RD, 1682 &rack_paced_segments, 1683 "Total number of times a segment send caused hptsi"); 1684 rack_unpaced_segments = counter_u64_alloc(M_WAITOK); 1685 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1686 SYSCTL_CHILDREN(rack_counters), 1687 OID_AUTO, "unpaced", CTLFLAG_RD, 1688 &rack_unpaced_segments, 1689 "Total number of times a segment did not cause hptsi"); 1690 rack_saw_enobuf = counter_u64_alloc(M_WAITOK); 1691 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1692 SYSCTL_CHILDREN(rack_counters), 1693 OID_AUTO, "saw_enobufs", CTLFLAG_RD, 1694 &rack_saw_enobuf, 1695 "Total number of times a sends returned enobuf for non-hdwr paced connections"); 1696 rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK); 1697 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1698 SYSCTL_CHILDREN(rack_counters), 1699 OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD, 1700 &rack_saw_enobuf_hw, 1701 "Total number of times a send returned enobuf for hdwr paced connections"); 1702 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK); 1703 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1704 SYSCTL_CHILDREN(rack_counters), 1705 OID_AUTO, "saw_enetunreach", CTLFLAG_RD, 1706 &rack_saw_enetunreach, 1707 "Total number of times a send received a enetunreachable"); 1708 rack_hot_alloc = counter_u64_alloc(M_WAITOK); 1709 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1710 SYSCTL_CHILDREN(rack_counters), 1711 OID_AUTO, "alloc_hot", CTLFLAG_RD, 1712 &rack_hot_alloc, 1713 "Total allocations from the top of our list"); 1714 rack_to_alloc = counter_u64_alloc(M_WAITOK); 1715 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1716 SYSCTL_CHILDREN(rack_counters), 1717 OID_AUTO, "allocs", CTLFLAG_RD, 1718 &rack_to_alloc, 1719 "Total allocations of tracking structures"); 1720 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK); 1721 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1722 SYSCTL_CHILDREN(rack_counters), 1723 OID_AUTO, "allochard", CTLFLAG_RD, 1724 &rack_to_alloc_hard, 1725 "Total allocations done with sleeping the hard way"); 1726 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK); 1727 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1728 SYSCTL_CHILDREN(rack_counters), 1729 OID_AUTO, "allocemerg", CTLFLAG_RD, 1730 &rack_to_alloc_emerg, 1731 "Total allocations done from emergency cache"); 1732 rack_to_alloc_limited = counter_u64_alloc(M_WAITOK); 1733 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1734 SYSCTL_CHILDREN(rack_counters), 1735 OID_AUTO, "alloc_limited", CTLFLAG_RD, 1736 &rack_to_alloc_limited, 1737 "Total allocations dropped due to limit"); 1738 rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK); 1739 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1740 SYSCTL_CHILDREN(rack_counters), 1741 OID_AUTO, "alloc_limited_conns", CTLFLAG_RD, 1742 &rack_alloc_limited_conns, 1743 "Connections with allocations dropped due to limit"); 1744 rack_split_limited = counter_u64_alloc(M_WAITOK); 1745 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1746 SYSCTL_CHILDREN(rack_counters), 1747 OID_AUTO, "split_limited", CTLFLAG_RD, 1748 &rack_split_limited, 1749 "Split allocations dropped due to limit"); 1750 1751 for (i = 0; i < MAX_NUM_OF_CNTS; i++) { 1752 char name[32]; 1753 sprintf(name, "cmp_ack_cnt_%d", i); 1754 rack_proc_comp_ack[i] = counter_u64_alloc(M_WAITOK); 1755 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1756 SYSCTL_CHILDREN(rack_counters), 1757 OID_AUTO, name, CTLFLAG_RD, 1758 &rack_proc_comp_ack[i], 1759 "Number of compressed acks we processed"); 1760 } 1761 rack_large_ackcmp = counter_u64_alloc(M_WAITOK); 1762 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1763 SYSCTL_CHILDREN(rack_counters), 1764 OID_AUTO, "cmp_large_mbufs", CTLFLAG_RD, 1765 &rack_large_ackcmp, 1766 "Number of TCP connections with large mbuf's for compressed acks"); 1767 rack_small_ackcmp = counter_u64_alloc(M_WAITOK); 1768 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1769 SYSCTL_CHILDREN(rack_counters), 1770 OID_AUTO, "cmp_small_mbufs", CTLFLAG_RD, 1771 &rack_small_ackcmp, 1772 "Number of TCP connections with small mbuf's for compressed acks"); 1773 #ifdef INVARIANTS 1774 rack_adjust_map_bw = counter_u64_alloc(M_WAITOK); 1775 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1776 SYSCTL_CHILDREN(rack_counters), 1777 OID_AUTO, "map_adjust_req", CTLFLAG_RD, 1778 &rack_adjust_map_bw, 1779 "Number of times we hit the case where the sb went up and down on a sendmap entry"); 1780 #endif 1781 rack_multi_single_eq = counter_u64_alloc(M_WAITOK); 1782 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1783 SYSCTL_CHILDREN(rack_counters), 1784 OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD, 1785 &rack_multi_single_eq, 1786 "Number of compressed acks total represented"); 1787 rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK); 1788 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1789 SYSCTL_CHILDREN(rack_counters), 1790 OID_AUTO, "cmp_ack_not", CTLFLAG_RD, 1791 &rack_proc_non_comp_ack, 1792 "Number of non compresseds acks that we processed"); 1793 1794 1795 rack_sack_proc_all = counter_u64_alloc(M_WAITOK); 1796 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1797 SYSCTL_CHILDREN(rack_counters), 1798 OID_AUTO, "sack_long", CTLFLAG_RD, 1799 &rack_sack_proc_all, 1800 "Total times we had to walk whole list for sack processing"); 1801 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK); 1802 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1803 SYSCTL_CHILDREN(rack_counters), 1804 OID_AUTO, "sack_restart", CTLFLAG_RD, 1805 &rack_sack_proc_restart, 1806 "Total times we had to walk whole list due to a restart"); 1807 rack_sack_proc_short = counter_u64_alloc(M_WAITOK); 1808 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1809 SYSCTL_CHILDREN(rack_counters), 1810 OID_AUTO, "sack_short", CTLFLAG_RD, 1811 &rack_sack_proc_short, 1812 "Total times we took shortcut for sack processing"); 1813 rack_enter_tlp_calc = counter_u64_alloc(M_WAITOK); 1814 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1815 SYSCTL_CHILDREN(rack_counters), 1816 OID_AUTO, "tlp_calc_entered", CTLFLAG_RD, 1817 &rack_enter_tlp_calc, 1818 "Total times we called calc-tlp"); 1819 rack_used_tlpmethod = counter_u64_alloc(M_WAITOK); 1820 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1821 SYSCTL_CHILDREN(rack_counters), 1822 OID_AUTO, "hit_tlp_method", CTLFLAG_RD, 1823 &rack_used_tlpmethod, 1824 "Total number of runt sacks"); 1825 rack_used_tlpmethod2 = counter_u64_alloc(M_WAITOK); 1826 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1827 SYSCTL_CHILDREN(rack_counters), 1828 OID_AUTO, "hit_tlp_method2", CTLFLAG_RD, 1829 &rack_used_tlpmethod2, 1830 "Total number of times we hit TLP method 2"); 1831 rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK); 1832 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1833 SYSCTL_CHILDREN(rack_attack), 1834 OID_AUTO, "skipacked", CTLFLAG_RD, 1835 &rack_sack_skipped_acked, 1836 "Total number of times we skipped previously sacked"); 1837 rack_sack_splits = counter_u64_alloc(M_WAITOK); 1838 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1839 SYSCTL_CHILDREN(rack_attack), 1840 OID_AUTO, "ofsplit", CTLFLAG_RD, 1841 &rack_sack_splits, 1842 "Total number of times we did the old fashion tree split"); 1843 rack_progress_drops = counter_u64_alloc(M_WAITOK); 1844 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1845 SYSCTL_CHILDREN(rack_counters), 1846 OID_AUTO, "prog_drops", CTLFLAG_RD, 1847 &rack_progress_drops, 1848 "Total number of progress drops"); 1849 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK); 1850 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1851 SYSCTL_CHILDREN(rack_counters), 1852 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD, 1853 &rack_input_idle_reduces, 1854 "Total number of idle reductions on input"); 1855 rack_collapsed_win = counter_u64_alloc(M_WAITOK); 1856 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1857 SYSCTL_CHILDREN(rack_counters), 1858 OID_AUTO, "collapsed_win", CTLFLAG_RD, 1859 &rack_collapsed_win, 1860 "Total number of collapsed windows"); 1861 rack_tlp_does_nada = counter_u64_alloc(M_WAITOK); 1862 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1863 SYSCTL_CHILDREN(rack_counters), 1864 OID_AUTO, "tlp_nada", CTLFLAG_RD, 1865 &rack_tlp_does_nada, 1866 "Total number of nada tlp calls"); 1867 rack_try_scwnd = counter_u64_alloc(M_WAITOK); 1868 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1869 SYSCTL_CHILDREN(rack_counters), 1870 OID_AUTO, "tried_scwnd", CTLFLAG_RD, 1871 &rack_try_scwnd, 1872 "Total number of scwnd attempts"); 1873 1874 rack_per_timer_hole = counter_u64_alloc(M_WAITOK); 1875 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1876 SYSCTL_CHILDREN(rack_counters), 1877 OID_AUTO, "timer_hole", CTLFLAG_RD, 1878 &rack_per_timer_hole, 1879 "Total persists start in timer hole"); 1880 1881 rack_sbsndptr_wrong = counter_u64_alloc(M_WAITOK); 1882 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1883 SYSCTL_CHILDREN(rack_counters), 1884 OID_AUTO, "sndptr_wrong", CTLFLAG_RD, 1885 &rack_sbsndptr_wrong, "Total number of times the saved sbsndptr was incorret"); 1886 rack_sbsndptr_right = counter_u64_alloc(M_WAITOK); 1887 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1888 SYSCTL_CHILDREN(rack_counters), 1889 OID_AUTO, "sndptr_right", CTLFLAG_RD, 1890 &rack_sbsndptr_right, "Total number of times the saved sbsndptr was corret"); 1891 1892 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1893 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 1894 OID_AUTO, "outsize", CTLFLAG_RD, 1895 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes"); 1896 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK); 1897 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 1898 OID_AUTO, "opts", CTLFLAG_RD, 1899 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats"); 1900 SYSCTL_ADD_PROC(&rack_sysctl_ctx, 1901 SYSCTL_CHILDREN(rack_sysctl_root), 1902 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1903 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters"); 1904 } 1905 1906 static __inline int 1907 rb_map_cmp(struct rack_sendmap *b, struct rack_sendmap *a) 1908 { 1909 if (SEQ_GEQ(b->r_start, a->r_start) && 1910 SEQ_LT(b->r_start, a->r_end)) { 1911 /* 1912 * The entry b is within the 1913 * block a. i.e.: 1914 * a -- |-------------| 1915 * b -- |----| 1916 * <or> 1917 * b -- |------| 1918 * <or> 1919 * b -- |-----------| 1920 */ 1921 return (0); 1922 } else if (SEQ_GEQ(b->r_start, a->r_end)) { 1923 /* 1924 * b falls as either the next 1925 * sequence block after a so a 1926 * is said to be smaller than b. 1927 * i.e: 1928 * a -- |------| 1929 * b -- |--------| 1930 * or 1931 * b -- |-----| 1932 */ 1933 return (1); 1934 } 1935 /* 1936 * Whats left is where a is 1937 * larger than b. i.e: 1938 * a -- |-------| 1939 * b -- |---| 1940 * or even possibly 1941 * b -- |--------------| 1942 */ 1943 return (-1); 1944 } 1945 1946 RB_PROTOTYPE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp); 1947 RB_GENERATE(rack_rb_tree_head, rack_sendmap, r_next, rb_map_cmp); 1948 1949 static uint32_t 1950 rc_init_window(struct tcp_rack *rack) 1951 { 1952 uint32_t win; 1953 1954 if (rack->rc_init_win == 0) { 1955 /* 1956 * Nothing set by the user, use the system stack 1957 * default. 1958 */ 1959 return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp))); 1960 } 1961 win = ctf_fixed_maxseg(rack->rc_tp) * rack->rc_init_win; 1962 return (win); 1963 } 1964 1965 static uint64_t 1966 rack_get_fixed_pacing_bw(struct tcp_rack *rack) 1967 { 1968 if (IN_FASTRECOVERY(rack->rc_tp->t_flags)) 1969 return (rack->r_ctl.rc_fixed_pacing_rate_rec); 1970 else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) 1971 return (rack->r_ctl.rc_fixed_pacing_rate_ss); 1972 else 1973 return (rack->r_ctl.rc_fixed_pacing_rate_ca); 1974 } 1975 1976 static uint64_t 1977 rack_get_bw(struct tcp_rack *rack) 1978 { 1979 if (rack->use_fixed_rate) { 1980 /* Return the fixed pacing rate */ 1981 return (rack_get_fixed_pacing_bw(rack)); 1982 } 1983 if (rack->r_ctl.gp_bw == 0) { 1984 /* 1985 * We have yet no b/w measurement, 1986 * if we have a user set initial bw 1987 * return it. If we don't have that and 1988 * we have an srtt, use the tcp IW (10) to 1989 * calculate a fictional b/w over the SRTT 1990 * which is more or less a guess. Note 1991 * we don't use our IW from rack on purpose 1992 * so if we have like IW=30, we are not 1993 * calculating a "huge" b/w. 1994 */ 1995 uint64_t bw, srtt; 1996 if (rack->r_ctl.init_rate) 1997 return (rack->r_ctl.init_rate); 1998 1999 /* Has the user set a max peak rate? */ 2000 #ifdef NETFLIX_PEAKRATE 2001 if (rack->rc_tp->t_maxpeakrate) 2002 return (rack->rc_tp->t_maxpeakrate); 2003 #endif 2004 /* Ok lets come up with the IW guess, if we have a srtt */ 2005 if (rack->rc_tp->t_srtt == 0) { 2006 /* 2007 * Go with old pacing method 2008 * i.e. burst mitigation only. 2009 */ 2010 return (0); 2011 } 2012 /* Ok lets get the initial TCP win (not racks) */ 2013 bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)); 2014 srtt = (uint64_t)rack->rc_tp->t_srtt; 2015 bw *= (uint64_t)USECS_IN_SECOND; 2016 bw /= srtt; 2017 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap)) 2018 bw = rack->r_ctl.bw_rate_cap; 2019 return (bw); 2020 } else { 2021 uint64_t bw; 2022 2023 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) { 2024 /* Averaging is done, we can return the value */ 2025 bw = rack->r_ctl.gp_bw; 2026 } else { 2027 /* Still doing initial average must calculate */ 2028 bw = rack->r_ctl.gp_bw / rack->r_ctl.num_measurements; 2029 } 2030 #ifdef NETFLIX_PEAKRATE 2031 if ((rack->rc_tp->t_maxpeakrate) && 2032 (bw > rack->rc_tp->t_maxpeakrate)) { 2033 /* The user has set a peak rate to pace at 2034 * don't allow us to pace faster than that. 2035 */ 2036 return (rack->rc_tp->t_maxpeakrate); 2037 } 2038 #endif 2039 if (rack->r_ctl.bw_rate_cap && (bw > rack->r_ctl.bw_rate_cap)) 2040 bw = rack->r_ctl.bw_rate_cap; 2041 return (bw); 2042 } 2043 } 2044 2045 static uint16_t 2046 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm) 2047 { 2048 if (rack->use_fixed_rate) { 2049 return (100); 2050 } else if (rack->in_probe_rtt && (rsm == NULL)) 2051 return (rack->r_ctl.rack_per_of_gp_probertt); 2052 else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) && 2053 rack->r_ctl.rack_per_of_gp_rec)) { 2054 if (rsm) { 2055 /* a retransmission always use the recovery rate */ 2056 return (rack->r_ctl.rack_per_of_gp_rec); 2057 } else if (rack->rack_rec_nonrxt_use_cr) { 2058 /* Directed to use the configured rate */ 2059 goto configured_rate; 2060 } else if (rack->rack_no_prr && 2061 (rack->r_ctl.rack_per_of_gp_rec > 100)) { 2062 /* No PRR, lets just use the b/w estimate only */ 2063 return (100); 2064 } else { 2065 /* 2066 * Here we may have a non-retransmit but we 2067 * have no overrides, so just use the recovery 2068 * rate (prr is in effect). 2069 */ 2070 return (rack->r_ctl.rack_per_of_gp_rec); 2071 } 2072 } 2073 configured_rate: 2074 /* For the configured rate we look at our cwnd vs the ssthresh */ 2075 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) 2076 return (rack->r_ctl.rack_per_of_gp_ss); 2077 else 2078 return (rack->r_ctl.rack_per_of_gp_ca); 2079 } 2080 2081 static void 2082 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6) 2083 { 2084 /* 2085 * Types of logs (mod value) 2086 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit. 2087 * 2 = a dsack round begins, persist is reset to 16. 2088 * 3 = a dsack round ends 2089 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh 2090 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack 2091 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh. 2092 */ 2093 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2094 union tcp_log_stackspecific log; 2095 struct timeval tv; 2096 2097 memset(&log, 0, sizeof(log)); 2098 log.u_bbr.flex1 = rack->rc_rack_tmr_std_based; 2099 log.u_bbr.flex1 <<= 1; 2100 log.u_bbr.flex1 |= rack->rc_rack_use_dsack; 2101 log.u_bbr.flex1 <<= 1; 2102 log.u_bbr.flex1 |= rack->rc_dsack_round_seen; 2103 log.u_bbr.flex2 = rack->r_ctl.dsack_round_end; 2104 log.u_bbr.flex3 = rack->r_ctl.num_dsack; 2105 log.u_bbr.flex4 = flex4; 2106 log.u_bbr.flex5 = flex5; 2107 log.u_bbr.flex6 = flex6; 2108 log.u_bbr.flex7 = rack->r_ctl.dsack_persist; 2109 log.u_bbr.flex8 = mod; 2110 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2111 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2112 &rack->rc_inp->inp_socket->so_rcv, 2113 &rack->rc_inp->inp_socket->so_snd, 2114 RACK_DSACK_HANDLING, 0, 2115 0, &log, false, &tv); 2116 } 2117 } 2118 2119 static void 2120 rack_log_hdwr_pacing(struct tcp_rack *rack, 2121 uint64_t rate, uint64_t hw_rate, int line, 2122 int error, uint16_t mod) 2123 { 2124 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2125 union tcp_log_stackspecific log; 2126 struct timeval tv; 2127 const struct ifnet *ifp; 2128 2129 memset(&log, 0, sizeof(log)); 2130 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2131 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2132 if (rack->r_ctl.crte) { 2133 ifp = rack->r_ctl.crte->ptbl->rs_ifp; 2134 } else if (rack->rc_inp->inp_route.ro_nh && 2135 rack->rc_inp->inp_route.ro_nh->nh_ifp) { 2136 ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp; 2137 } else 2138 ifp = NULL; 2139 if (ifp) { 2140 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff); 2141 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff); 2142 } 2143 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2144 log.u_bbr.bw_inuse = rate; 2145 log.u_bbr.flex5 = line; 2146 log.u_bbr.flex6 = error; 2147 log.u_bbr.flex7 = mod; 2148 log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs; 2149 log.u_bbr.flex8 = rack->use_fixed_rate; 2150 log.u_bbr.flex8 <<= 1; 2151 log.u_bbr.flex8 |= rack->rack_hdrw_pacing; 2152 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg; 2153 log.u_bbr.delRate = rack->r_ctl.crte_prev_rate; 2154 if (rack->r_ctl.crte) 2155 log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate; 2156 else 2157 log.u_bbr.cur_del_rate = 0; 2158 log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req; 2159 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2160 &rack->rc_inp->inp_socket->so_rcv, 2161 &rack->rc_inp->inp_socket->so_snd, 2162 BBR_LOG_HDWR_PACE, 0, 2163 0, &log, false, &tv); 2164 } 2165 } 2166 2167 static uint64_t 2168 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped) 2169 { 2170 /* 2171 * We allow rack_per_of_gp_xx to dictate our bw rate we want. 2172 */ 2173 uint64_t bw_est, high_rate; 2174 uint64_t gain; 2175 2176 gain = (uint64_t)rack_get_output_gain(rack, rsm); 2177 bw_est = bw * gain; 2178 bw_est /= (uint64_t)100; 2179 /* Never fall below the minimum (def 64kbps) */ 2180 if (bw_est < RACK_MIN_BW) 2181 bw_est = RACK_MIN_BW; 2182 if (rack->r_rack_hw_rate_caps) { 2183 /* Rate caps are in place */ 2184 if (rack->r_ctl.crte != NULL) { 2185 /* We have a hdwr rate already */ 2186 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte); 2187 if (bw_est >= high_rate) { 2188 /* We are capping bw at the highest rate table entry */ 2189 rack_log_hdwr_pacing(rack, 2190 bw_est, high_rate, __LINE__, 2191 0, 3); 2192 bw_est = high_rate; 2193 if (capped) 2194 *capped = 1; 2195 } 2196 } else if ((rack->rack_hdrw_pacing == 0) && 2197 (rack->rack_hdw_pace_ena) && 2198 (rack->rack_attempt_hdwr_pace == 0) && 2199 (rack->rc_inp->inp_route.ro_nh != NULL) && 2200 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 2201 /* 2202 * Special case, we have not yet attempted hardware 2203 * pacing, and yet we may, when we do, find out if we are 2204 * above the highest rate. We need to know the maxbw for the interface 2205 * in question (if it supports ratelimiting). We get back 2206 * a 0, if the interface is not found in the RL lists. 2207 */ 2208 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp); 2209 if (high_rate) { 2210 /* Yep, we have a rate is it above this rate? */ 2211 if (bw_est > high_rate) { 2212 bw_est = high_rate; 2213 if (capped) 2214 *capped = 1; 2215 } 2216 } 2217 } 2218 } 2219 return (bw_est); 2220 } 2221 2222 static void 2223 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod) 2224 { 2225 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2226 union tcp_log_stackspecific log; 2227 struct timeval tv; 2228 2229 if ((mod != 1) && (rack_verbose_logging == 0)) { 2230 /* 2231 * We get 3 values currently for mod 2232 * 1 - We are retransmitting and this tells the reason. 2233 * 2 - We are clearing a dup-ack count. 2234 * 3 - We are incrementing a dup-ack count. 2235 * 2236 * The clear/increment are only logged 2237 * if you have BBverbose on. 2238 */ 2239 return; 2240 } 2241 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2242 log.u_bbr.flex1 = tsused; 2243 log.u_bbr.flex2 = thresh; 2244 log.u_bbr.flex3 = rsm->r_flags; 2245 log.u_bbr.flex4 = rsm->r_dupack; 2246 log.u_bbr.flex5 = rsm->r_start; 2247 log.u_bbr.flex6 = rsm->r_end; 2248 log.u_bbr.flex8 = mod; 2249 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2250 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2251 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2252 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2253 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2254 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2255 log.u_bbr.pacing_gain = rack->r_must_retran; 2256 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2257 &rack->rc_inp->inp_socket->so_rcv, 2258 &rack->rc_inp->inp_socket->so_snd, 2259 BBR_LOG_SETTINGS_CHG, 0, 2260 0, &log, false, &tv); 2261 } 2262 } 2263 2264 static void 2265 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2266 { 2267 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2268 union tcp_log_stackspecific log; 2269 struct timeval tv; 2270 2271 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2272 log.u_bbr.flex1 = rack->rc_tp->t_srtt; 2273 log.u_bbr.flex2 = to; 2274 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags; 2275 log.u_bbr.flex4 = slot; 2276 log.u_bbr.flex5 = rack->rc_inp->inp_hptsslot; 2277 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 2278 log.u_bbr.flex7 = rack->rc_in_persist; 2279 log.u_bbr.flex8 = which; 2280 if (rack->rack_no_prr) 2281 log.u_bbr.pkts_out = 0; 2282 else 2283 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 2284 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2285 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2286 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2287 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2288 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2289 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2290 log.u_bbr.pacing_gain = rack->r_must_retran; 2291 log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift; 2292 log.u_bbr.lost = rack_rto_min; 2293 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2294 &rack->rc_inp->inp_socket->so_rcv, 2295 &rack->rc_inp->inp_socket->so_snd, 2296 BBR_LOG_TIMERSTAR, 0, 2297 0, &log, false, &tv); 2298 } 2299 } 2300 2301 static void 2302 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm) 2303 { 2304 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2305 union tcp_log_stackspecific log; 2306 struct timeval tv; 2307 2308 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2309 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2310 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2311 log.u_bbr.flex8 = to_num; 2312 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt; 2313 log.u_bbr.flex2 = rack->rc_rack_rtt; 2314 if (rsm == NULL) 2315 log.u_bbr.flex3 = 0; 2316 else 2317 log.u_bbr.flex3 = rsm->r_end - rsm->r_start; 2318 if (rack->rack_no_prr) 2319 log.u_bbr.flex5 = 0; 2320 else 2321 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 2322 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2323 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2324 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2325 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2326 log.u_bbr.pacing_gain = rack->r_must_retran; 2327 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2328 &rack->rc_inp->inp_socket->so_rcv, 2329 &rack->rc_inp->inp_socket->so_snd, 2330 BBR_LOG_RTO, 0, 2331 0, &log, false, &tv); 2332 } 2333 } 2334 2335 static void 2336 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack, 2337 struct rack_sendmap *prev, 2338 struct rack_sendmap *rsm, 2339 struct rack_sendmap *next, 2340 int flag, uint32_t th_ack, int line) 2341 { 2342 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) { 2343 union tcp_log_stackspecific log; 2344 struct timeval tv; 2345 2346 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2347 log.u_bbr.flex8 = flag; 2348 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2349 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2350 log.u_bbr.cur_del_rate = (uint64_t)prev; 2351 log.u_bbr.delRate = (uint64_t)rsm; 2352 log.u_bbr.rttProp = (uint64_t)next; 2353 log.u_bbr.flex7 = 0; 2354 if (prev) { 2355 log.u_bbr.flex1 = prev->r_start; 2356 log.u_bbr.flex2 = prev->r_end; 2357 log.u_bbr.flex7 |= 0x4; 2358 } 2359 if (rsm) { 2360 log.u_bbr.flex3 = rsm->r_start; 2361 log.u_bbr.flex4 = rsm->r_end; 2362 log.u_bbr.flex7 |= 0x2; 2363 } 2364 if (next) { 2365 log.u_bbr.flex5 = next->r_start; 2366 log.u_bbr.flex6 = next->r_end; 2367 log.u_bbr.flex7 |= 0x1; 2368 } 2369 log.u_bbr.applimited = line; 2370 log.u_bbr.pkts_out = th_ack; 2371 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2372 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2373 if (rack->rack_no_prr) 2374 log.u_bbr.lost = 0; 2375 else 2376 log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt; 2377 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2378 &rack->rc_inp->inp_socket->so_rcv, 2379 &rack->rc_inp->inp_socket->so_snd, 2380 TCP_LOG_MAPCHG, 0, 2381 0, &log, false, &tv); 2382 } 2383 } 2384 2385 static void 2386 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len, 2387 struct rack_sendmap *rsm, int conf) 2388 { 2389 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 2390 union tcp_log_stackspecific log; 2391 struct timeval tv; 2392 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2393 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2394 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2395 log.u_bbr.flex1 = t; 2396 log.u_bbr.flex2 = len; 2397 log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt; 2398 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest; 2399 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest; 2400 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt; 2401 log.u_bbr.flex7 = conf; 2402 log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot; 2403 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method; 2404 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2405 log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt; 2406 log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags; 2407 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2408 if (rsm) { 2409 log.u_bbr.pkt_epoch = rsm->r_start; 2410 log.u_bbr.lost = rsm->r_end; 2411 log.u_bbr.cwnd_gain = rsm->r_rtr_cnt; 2412 log.u_bbr.pacing_gain = rsm->r_flags; 2413 } else { 2414 /* Its a SYN */ 2415 log.u_bbr.pkt_epoch = rack->rc_tp->iss; 2416 log.u_bbr.lost = 0; 2417 log.u_bbr.cwnd_gain = 0; 2418 log.u_bbr.pacing_gain = 0; 2419 } 2420 /* Write out general bits of interest rrs here */ 2421 log.u_bbr.use_lt_bw = rack->rc_highly_buffered; 2422 log.u_bbr.use_lt_bw <<= 1; 2423 log.u_bbr.use_lt_bw |= rack->forced_ack; 2424 log.u_bbr.use_lt_bw <<= 1; 2425 log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul; 2426 log.u_bbr.use_lt_bw <<= 1; 2427 log.u_bbr.use_lt_bw |= rack->in_probe_rtt; 2428 log.u_bbr.use_lt_bw <<= 1; 2429 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt; 2430 log.u_bbr.use_lt_bw <<= 1; 2431 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set; 2432 log.u_bbr.use_lt_bw <<= 1; 2433 log.u_bbr.use_lt_bw |= rack->rc_gp_filled; 2434 log.u_bbr.use_lt_bw <<= 1; 2435 log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom; 2436 log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight; 2437 log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts; 2438 log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered; 2439 log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts; 2440 log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt; 2441 log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 2442 log.u_bbr.bw_inuse <<= 32; 2443 if (rsm) 2444 log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 2445 TCP_LOG_EVENTP(tp, NULL, 2446 &rack->rc_inp->inp_socket->so_rcv, 2447 &rack->rc_inp->inp_socket->so_snd, 2448 BBR_LOG_BBRRTT, 0, 2449 0, &log, false, &tv); 2450 2451 2452 } 2453 } 2454 2455 static void 2456 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt) 2457 { 2458 /* 2459 * Log the rtt sample we are 2460 * applying to the srtt algorithm in 2461 * useconds. 2462 */ 2463 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2464 union tcp_log_stackspecific log; 2465 struct timeval tv; 2466 2467 /* Convert our ms to a microsecond */ 2468 memset(&log, 0, sizeof(log)); 2469 log.u_bbr.flex1 = rtt; 2470 log.u_bbr.flex2 = rack->r_ctl.ack_count; 2471 log.u_bbr.flex3 = rack->r_ctl.sack_count; 2472 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move; 2473 log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra; 2474 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 2475 log.u_bbr.flex7 = 1; 2476 log.u_bbr.flex8 = rack->sack_attack_disable; 2477 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2478 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2479 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2480 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2481 log.u_bbr.pacing_gain = rack->r_must_retran; 2482 /* 2483 * We capture in delRate the upper 32 bits as 2484 * the confidence level we had declared, and the 2485 * lower 32 bits as the actual RTT using the arrival 2486 * timestamp. 2487 */ 2488 log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence; 2489 log.u_bbr.delRate <<= 32; 2490 log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt; 2491 /* Lets capture all the things that make up t_rtxcur */ 2492 log.u_bbr.applimited = rack_rto_min; 2493 log.u_bbr.epoch = rack_rto_max; 2494 log.u_bbr.lt_epoch = rack->r_ctl.timer_slop; 2495 log.u_bbr.lost = rack_rto_min; 2496 log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop); 2497 log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp); 2498 log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec; 2499 log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC; 2500 log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec; 2501 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2502 &rack->rc_inp->inp_socket->so_rcv, 2503 &rack->rc_inp->inp_socket->so_snd, 2504 TCP_LOG_RTT, 0, 2505 0, &log, false, &tv); 2506 } 2507 } 2508 2509 static void 2510 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where) 2511 { 2512 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) { 2513 union tcp_log_stackspecific log; 2514 struct timeval tv; 2515 2516 /* Convert our ms to a microsecond */ 2517 memset(&log, 0, sizeof(log)); 2518 log.u_bbr.flex1 = rtt; 2519 log.u_bbr.flex2 = send_time; 2520 log.u_bbr.flex3 = ack_time; 2521 log.u_bbr.flex4 = where; 2522 log.u_bbr.flex7 = 2; 2523 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2524 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2525 &rack->rc_inp->inp_socket->so_rcv, 2526 &rack->rc_inp->inp_socket->so_snd, 2527 TCP_LOG_RTT, 0, 2528 0, &log, false, &tv); 2529 } 2530 } 2531 2532 2533 2534 static inline void 2535 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line) 2536 { 2537 if (rack_verbose_logging && (tp->t_logstate != TCP_LOG_STATE_OFF)) { 2538 union tcp_log_stackspecific log; 2539 struct timeval tv; 2540 2541 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2542 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2543 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2544 log.u_bbr.flex1 = line; 2545 log.u_bbr.flex2 = tick; 2546 log.u_bbr.flex3 = tp->t_maxunacktime; 2547 log.u_bbr.flex4 = tp->t_acktime; 2548 log.u_bbr.flex8 = event; 2549 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2550 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2551 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2552 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2553 log.u_bbr.pacing_gain = rack->r_must_retran; 2554 TCP_LOG_EVENTP(tp, NULL, 2555 &rack->rc_inp->inp_socket->so_rcv, 2556 &rack->rc_inp->inp_socket->so_snd, 2557 BBR_LOG_PROGRESS, 0, 2558 0, &log, false, &tv); 2559 } 2560 } 2561 2562 static void 2563 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv) 2564 { 2565 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2566 union tcp_log_stackspecific log; 2567 2568 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2569 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2570 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2571 log.u_bbr.flex1 = slot; 2572 if (rack->rack_no_prr) 2573 log.u_bbr.flex2 = 0; 2574 else 2575 log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt; 2576 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags); 2577 log.u_bbr.flex8 = rack->rc_in_persist; 2578 log.u_bbr.timeStamp = cts; 2579 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2580 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2581 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2582 log.u_bbr.pacing_gain = rack->r_must_retran; 2583 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2584 &rack->rc_inp->inp_socket->so_rcv, 2585 &rack->rc_inp->inp_socket->so_snd, 2586 BBR_LOG_BBRSND, 0, 2587 0, &log, false, tv); 2588 } 2589 } 2590 2591 static void 2592 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs) 2593 { 2594 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2595 union tcp_log_stackspecific log; 2596 struct timeval tv; 2597 2598 memset(&log, 0, sizeof(log)); 2599 log.u_bbr.flex1 = did_out; 2600 log.u_bbr.flex2 = nxt_pkt; 2601 log.u_bbr.flex3 = way_out; 2602 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 2603 if (rack->rack_no_prr) 2604 log.u_bbr.flex5 = 0; 2605 else 2606 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 2607 log.u_bbr.flex6 = nsegs; 2608 log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs; 2609 log.u_bbr.flex7 = rack->rc_ack_can_sendout_data; /* Do we have ack-can-send set */ 2610 log.u_bbr.flex7 <<= 1; 2611 log.u_bbr.flex7 |= rack->r_fast_output; /* is fast output primed */ 2612 log.u_bbr.flex7 <<= 1; 2613 log.u_bbr.flex7 |= rack->r_wanted_output; /* Do we want output */ 2614 log.u_bbr.flex8 = rack->rc_in_persist; 2615 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2616 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2617 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2618 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 2619 log.u_bbr.use_lt_bw <<= 1; 2620 log.u_bbr.use_lt_bw |= rack->r_might_revert; 2621 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2622 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2623 log.u_bbr.pacing_gain = rack->r_must_retran; 2624 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2625 &rack->rc_inp->inp_socket->so_rcv, 2626 &rack->rc_inp->inp_socket->so_snd, 2627 BBR_LOG_DOSEG_DONE, 0, 2628 0, &log, false, &tv); 2629 } 2630 } 2631 2632 static void 2633 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm) 2634 { 2635 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 2636 union tcp_log_stackspecific log; 2637 struct timeval tv; 2638 uint32_t cts; 2639 2640 memset(&log, 0, sizeof(log)); 2641 cts = tcp_get_usecs(&tv); 2642 log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs; 2643 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 2644 log.u_bbr.flex4 = arg1; 2645 log.u_bbr.flex5 = arg2; 2646 log.u_bbr.flex6 = arg3; 2647 log.u_bbr.flex8 = frm; 2648 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2649 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2650 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2651 log.u_bbr.applimited = rack->r_ctl.rc_sacked; 2652 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2653 log.u_bbr.pacing_gain = rack->r_must_retran; 2654 TCP_LOG_EVENTP(tp, NULL, 2655 &tp->t_inpcb->inp_socket->so_rcv, 2656 &tp->t_inpcb->inp_socket->so_snd, 2657 TCP_HDWR_PACE_SIZE, 0, 2658 0, &log, false, &tv); 2659 } 2660 } 2661 2662 static void 2663 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot, 2664 uint8_t hpts_calling, int reason, uint32_t cwnd_to_use) 2665 { 2666 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2667 union tcp_log_stackspecific log; 2668 struct timeval tv; 2669 2670 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2671 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2672 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2673 log.u_bbr.flex1 = slot; 2674 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags; 2675 log.u_bbr.flex4 = reason; 2676 if (rack->rack_no_prr) 2677 log.u_bbr.flex5 = 0; 2678 else 2679 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 2680 log.u_bbr.flex7 = hpts_calling; 2681 log.u_bbr.flex8 = rack->rc_in_persist; 2682 log.u_bbr.lt_epoch = cwnd_to_use; 2683 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2684 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2685 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2686 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2687 log.u_bbr.pacing_gain = rack->r_must_retran; 2688 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2689 &rack->rc_inp->inp_socket->so_rcv, 2690 &rack->rc_inp->inp_socket->so_snd, 2691 BBR_LOG_JUSTRET, 0, 2692 tlen, &log, false, &tv); 2693 } 2694 } 2695 2696 static void 2697 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts, 2698 struct timeval *tv, uint32_t flags_on_entry) 2699 { 2700 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2701 union tcp_log_stackspecific log; 2702 2703 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2704 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 2705 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 2706 log.u_bbr.flex1 = line; 2707 log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to; 2708 log.u_bbr.flex3 = flags_on_entry; 2709 log.u_bbr.flex4 = us_cts; 2710 if (rack->rack_no_prr) 2711 log.u_bbr.flex5 = 0; 2712 else 2713 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 2714 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 2715 log.u_bbr.flex7 = hpts_removed; 2716 log.u_bbr.flex8 = 1; 2717 log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags; 2718 log.u_bbr.timeStamp = us_cts; 2719 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2720 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2721 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2722 log.u_bbr.pacing_gain = rack->r_must_retran; 2723 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2724 &rack->rc_inp->inp_socket->so_rcv, 2725 &rack->rc_inp->inp_socket->so_snd, 2726 BBR_LOG_TIMERCANC, 0, 2727 0, &log, false, tv); 2728 } 2729 } 2730 2731 static void 2732 rack_log_alt_to_to_cancel(struct tcp_rack *rack, 2733 uint32_t flex1, uint32_t flex2, 2734 uint32_t flex3, uint32_t flex4, 2735 uint32_t flex5, uint32_t flex6, 2736 uint16_t flex7, uint8_t mod) 2737 { 2738 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2739 union tcp_log_stackspecific log; 2740 struct timeval tv; 2741 2742 if (mod == 1) { 2743 /* No you can't use 1, its for the real to cancel */ 2744 return; 2745 } 2746 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2747 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2748 log.u_bbr.flex1 = flex1; 2749 log.u_bbr.flex2 = flex2; 2750 log.u_bbr.flex3 = flex3; 2751 log.u_bbr.flex4 = flex4; 2752 log.u_bbr.flex5 = flex5; 2753 log.u_bbr.flex6 = flex6; 2754 log.u_bbr.flex7 = flex7; 2755 log.u_bbr.flex8 = mod; 2756 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2757 &rack->rc_inp->inp_socket->so_rcv, 2758 &rack->rc_inp->inp_socket->so_snd, 2759 BBR_LOG_TIMERCANC, 0, 2760 0, &log, false, &tv); 2761 } 2762 } 2763 2764 static void 2765 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers) 2766 { 2767 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2768 union tcp_log_stackspecific log; 2769 struct timeval tv; 2770 2771 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2772 log.u_bbr.flex1 = timers; 2773 log.u_bbr.flex2 = ret; 2774 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp; 2775 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 2776 log.u_bbr.flex5 = cts; 2777 if (rack->rack_no_prr) 2778 log.u_bbr.flex6 = 0; 2779 else 2780 log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt; 2781 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2782 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2783 log.u_bbr.pacing_gain = rack->r_must_retran; 2784 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2785 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2786 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2787 &rack->rc_inp->inp_socket->so_rcv, 2788 &rack->rc_inp->inp_socket->so_snd, 2789 BBR_LOG_TO_PROCESS, 0, 2790 0, &log, false, &tv); 2791 } 2792 } 2793 2794 static void 2795 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd) 2796 { 2797 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2798 union tcp_log_stackspecific log; 2799 struct timeval tv; 2800 2801 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2802 log.u_bbr.flex1 = rack->r_ctl.rc_prr_out; 2803 log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs; 2804 if (rack->rack_no_prr) 2805 log.u_bbr.flex3 = 0; 2806 else 2807 log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt; 2808 log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered; 2809 log.u_bbr.flex5 = rack->r_ctl.rc_sacked; 2810 log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt; 2811 log.u_bbr.flex8 = frm; 2812 log.u_bbr.pkts_out = orig_cwnd; 2813 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2814 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2815 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 2816 log.u_bbr.use_lt_bw <<= 1; 2817 log.u_bbr.use_lt_bw |= rack->r_might_revert; 2818 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2819 &rack->rc_inp->inp_socket->so_rcv, 2820 &rack->rc_inp->inp_socket->so_snd, 2821 BBR_LOG_BBRUPD, 0, 2822 0, &log, false, &tv); 2823 } 2824 } 2825 2826 #ifdef NETFLIX_EXP_DETECTION 2827 static void 2828 rack_log_sad(struct tcp_rack *rack, int event) 2829 { 2830 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 2831 union tcp_log_stackspecific log; 2832 struct timeval tv; 2833 2834 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2835 log.u_bbr.flex1 = rack->r_ctl.sack_count; 2836 log.u_bbr.flex2 = rack->r_ctl.ack_count; 2837 log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra; 2838 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move; 2839 log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced; 2840 log.u_bbr.flex6 = tcp_sack_to_ack_thresh; 2841 log.u_bbr.pkts_out = tcp_sack_to_move_thresh; 2842 log.u_bbr.lt_epoch = (tcp_force_detection << 8); 2843 log.u_bbr.lt_epoch |= rack->do_detection; 2844 log.u_bbr.applimited = tcp_map_minimum; 2845 log.u_bbr.flex7 = rack->sack_attack_disable; 2846 log.u_bbr.flex8 = event; 2847 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2848 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2849 log.u_bbr.delivered = tcp_sad_decay_val; 2850 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2851 &rack->rc_inp->inp_socket->so_rcv, 2852 &rack->rc_inp->inp_socket->so_snd, 2853 TCP_SAD_DETECTION, 0, 2854 0, &log, false, &tv); 2855 } 2856 } 2857 #endif 2858 2859 static void 2860 rack_counter_destroy(void) 2861 { 2862 int i; 2863 2864 counter_u64_free(rack_fto_send); 2865 counter_u64_free(rack_fto_rsm_send); 2866 counter_u64_free(rack_nfto_resend); 2867 counter_u64_free(rack_hw_pace_init_fail); 2868 counter_u64_free(rack_hw_pace_lost); 2869 counter_u64_free(rack_non_fto_send); 2870 counter_u64_free(rack_extended_rfo); 2871 counter_u64_free(rack_ack_total); 2872 counter_u64_free(rack_express_sack); 2873 counter_u64_free(rack_sack_total); 2874 counter_u64_free(rack_move_none); 2875 counter_u64_free(rack_move_some); 2876 counter_u64_free(rack_sack_attacks_detected); 2877 counter_u64_free(rack_sack_attacks_reversed); 2878 counter_u64_free(rack_sack_used_next_merge); 2879 counter_u64_free(rack_sack_used_prev_merge); 2880 counter_u64_free(rack_badfr); 2881 counter_u64_free(rack_badfr_bytes); 2882 counter_u64_free(rack_rtm_prr_retran); 2883 counter_u64_free(rack_rtm_prr_newdata); 2884 counter_u64_free(rack_timestamp_mismatch); 2885 counter_u64_free(rack_find_high); 2886 counter_u64_free(rack_reorder_seen); 2887 counter_u64_free(rack_tlp_tot); 2888 counter_u64_free(rack_tlp_newdata); 2889 counter_u64_free(rack_tlp_retran); 2890 counter_u64_free(rack_tlp_retran_bytes); 2891 counter_u64_free(rack_tlp_retran_fail); 2892 counter_u64_free(rack_to_tot); 2893 counter_u64_free(rack_to_arm_rack); 2894 counter_u64_free(rack_to_arm_tlp); 2895 counter_u64_free(rack_calc_zero); 2896 counter_u64_free(rack_calc_nonzero); 2897 counter_u64_free(rack_paced_segments); 2898 counter_u64_free(rack_unpaced_segments); 2899 counter_u64_free(rack_saw_enobuf); 2900 counter_u64_free(rack_saw_enobuf_hw); 2901 counter_u64_free(rack_saw_enetunreach); 2902 counter_u64_free(rack_hot_alloc); 2903 counter_u64_free(rack_to_alloc); 2904 counter_u64_free(rack_to_alloc_hard); 2905 counter_u64_free(rack_to_alloc_emerg); 2906 counter_u64_free(rack_to_alloc_limited); 2907 counter_u64_free(rack_alloc_limited_conns); 2908 counter_u64_free(rack_split_limited); 2909 for (i = 0; i < MAX_NUM_OF_CNTS; i++) { 2910 counter_u64_free(rack_proc_comp_ack[i]); 2911 } 2912 counter_u64_free(rack_multi_single_eq); 2913 counter_u64_free(rack_proc_non_comp_ack); 2914 counter_u64_free(rack_sack_proc_all); 2915 counter_u64_free(rack_sack_proc_restart); 2916 counter_u64_free(rack_sack_proc_short); 2917 counter_u64_free(rack_enter_tlp_calc); 2918 counter_u64_free(rack_used_tlpmethod); 2919 counter_u64_free(rack_used_tlpmethod2); 2920 counter_u64_free(rack_sack_skipped_acked); 2921 counter_u64_free(rack_sack_splits); 2922 counter_u64_free(rack_progress_drops); 2923 counter_u64_free(rack_input_idle_reduces); 2924 counter_u64_free(rack_collapsed_win); 2925 counter_u64_free(rack_tlp_does_nada); 2926 counter_u64_free(rack_try_scwnd); 2927 counter_u64_free(rack_per_timer_hole); 2928 counter_u64_free(rack_large_ackcmp); 2929 counter_u64_free(rack_small_ackcmp); 2930 #ifdef INVARIANTS 2931 counter_u64_free(rack_adjust_map_bw); 2932 #endif 2933 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE); 2934 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE); 2935 } 2936 2937 static struct rack_sendmap * 2938 rack_alloc(struct tcp_rack *rack) 2939 { 2940 struct rack_sendmap *rsm; 2941 2942 /* 2943 * First get the top of the list it in 2944 * theory is the "hottest" rsm we have, 2945 * possibly just freed by ack processing. 2946 */ 2947 if (rack->rc_free_cnt > rack_free_cache) { 2948 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 2949 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 2950 counter_u64_add(rack_hot_alloc, 1); 2951 rack->rc_free_cnt--; 2952 return (rsm); 2953 } 2954 /* 2955 * Once we get under our free cache we probably 2956 * no longer have a "hot" one available. Lets 2957 * get one from UMA. 2958 */ 2959 rsm = uma_zalloc(rack_zone, M_NOWAIT); 2960 if (rsm) { 2961 rack->r_ctl.rc_num_maps_alloced++; 2962 counter_u64_add(rack_to_alloc, 1); 2963 return (rsm); 2964 } 2965 /* 2966 * Dig in to our aux rsm's (the last two) since 2967 * UMA failed to get us one. 2968 */ 2969 if (rack->rc_free_cnt) { 2970 counter_u64_add(rack_to_alloc_emerg, 1); 2971 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 2972 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 2973 rack->rc_free_cnt--; 2974 return (rsm); 2975 } 2976 return (NULL); 2977 } 2978 2979 static struct rack_sendmap * 2980 rack_alloc_full_limit(struct tcp_rack *rack) 2981 { 2982 if ((V_tcp_map_entries_limit > 0) && 2983 (rack->do_detection == 0) && 2984 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 2985 counter_u64_add(rack_to_alloc_limited, 1); 2986 if (!rack->alloc_limit_reported) { 2987 rack->alloc_limit_reported = 1; 2988 counter_u64_add(rack_alloc_limited_conns, 1); 2989 } 2990 return (NULL); 2991 } 2992 return (rack_alloc(rack)); 2993 } 2994 2995 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 2996 static struct rack_sendmap * 2997 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type) 2998 { 2999 struct rack_sendmap *rsm; 3000 3001 if (limit_type) { 3002 /* currently there is only one limit type */ 3003 if (V_tcp_map_split_limit > 0 && 3004 (rack->do_detection == 0) && 3005 rack->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3006 counter_u64_add(rack_split_limited, 1); 3007 if (!rack->alloc_limit_reported) { 3008 rack->alloc_limit_reported = 1; 3009 counter_u64_add(rack_alloc_limited_conns, 1); 3010 } 3011 return (NULL); 3012 } 3013 } 3014 3015 /* allocate and mark in the limit type, if set */ 3016 rsm = rack_alloc(rack); 3017 if (rsm != NULL && limit_type) { 3018 rsm->r_limit_type = limit_type; 3019 rack->r_ctl.rc_num_split_allocs++; 3020 } 3021 return (rsm); 3022 } 3023 3024 static void 3025 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm) 3026 { 3027 if (rsm->r_flags & RACK_APP_LIMITED) { 3028 if (rack->r_ctl.rc_app_limited_cnt > 0) { 3029 rack->r_ctl.rc_app_limited_cnt--; 3030 } 3031 } 3032 if (rsm->r_limit_type) { 3033 /* currently there is only one limit type */ 3034 rack->r_ctl.rc_num_split_allocs--; 3035 } 3036 if (rsm == rack->r_ctl.rc_first_appl) { 3037 if (rack->r_ctl.rc_app_limited_cnt == 0) 3038 rack->r_ctl.rc_first_appl = NULL; 3039 else { 3040 /* Follow the next one out */ 3041 struct rack_sendmap fe; 3042 3043 fe.r_start = rsm->r_nseq_appl; 3044 rack->r_ctl.rc_first_appl = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 3045 } 3046 } 3047 if (rsm == rack->r_ctl.rc_resend) 3048 rack->r_ctl.rc_resend = NULL; 3049 if (rsm == rack->r_ctl.rc_rsm_at_retran) 3050 rack->r_ctl.rc_rsm_at_retran = NULL; 3051 if (rsm == rack->r_ctl.rc_end_appl) 3052 rack->r_ctl.rc_end_appl = NULL; 3053 if (rack->r_ctl.rc_tlpsend == rsm) 3054 rack->r_ctl.rc_tlpsend = NULL; 3055 if (rack->r_ctl.rc_sacklast == rsm) 3056 rack->r_ctl.rc_sacklast = NULL; 3057 memset(rsm, 0, sizeof(struct rack_sendmap)); 3058 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext); 3059 rack->rc_free_cnt++; 3060 } 3061 3062 static void 3063 rack_free_trim(struct tcp_rack *rack) 3064 { 3065 struct rack_sendmap *rsm; 3066 3067 /* 3068 * Free up all the tail entries until 3069 * we get our list down to the limit. 3070 */ 3071 while (rack->rc_free_cnt > rack_free_cache) { 3072 rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head); 3073 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 3074 rack->rc_free_cnt--; 3075 uma_zfree(rack_zone, rsm); 3076 } 3077 } 3078 3079 3080 static uint32_t 3081 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack) 3082 { 3083 uint64_t srtt, bw, len, tim; 3084 uint32_t segsiz, def_len, minl; 3085 3086 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 3087 def_len = rack_def_data_window * segsiz; 3088 if (rack->rc_gp_filled == 0) { 3089 /* 3090 * We have no measurement (IW is in flight?) so 3091 * we can only guess using our data_window sysctl 3092 * value (usually 20MSS). 3093 */ 3094 return (def_len); 3095 } 3096 /* 3097 * Now we have a number of factors to consider. 3098 * 3099 * 1) We have a desired BDP which is usually 3100 * at least 2. 3101 * 2) We have a minimum number of rtt's usually 1 SRTT 3102 * but we allow it too to be more. 3103 * 3) We want to make sure a measurement last N useconds (if 3104 * we have set rack_min_measure_usec. 3105 * 3106 * We handle the first concern here by trying to create a data 3107 * window of max(rack_def_data_window, DesiredBDP). The 3108 * second concern we handle in not letting the measurement 3109 * window end normally until at least the required SRTT's 3110 * have gone by which is done further below in 3111 * rack_enough_for_measurement(). Finally the third concern 3112 * we also handle here by calculating how long that time 3113 * would take at the current BW and then return the 3114 * max of our first calculation and that length. Note 3115 * that if rack_min_measure_usec is 0, we don't deal 3116 * with concern 3. Also for both Concern 1 and 3 an 3117 * application limited period could end the measurement 3118 * earlier. 3119 * 3120 * So lets calculate the BDP with the "known" b/w using 3121 * the SRTT has our rtt and then multiply it by the 3122 * goal. 3123 */ 3124 bw = rack_get_bw(rack); 3125 srtt = (uint64_t)tp->t_srtt; 3126 len = bw * srtt; 3127 len /= (uint64_t)HPTS_USEC_IN_SEC; 3128 len *= max(1, rack_goal_bdp); 3129 /* Now we need to round up to the nearest MSS */ 3130 len = roundup(len, segsiz); 3131 if (rack_min_measure_usec) { 3132 /* Now calculate our min length for this b/w */ 3133 tim = rack_min_measure_usec; 3134 minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC; 3135 if (minl == 0) 3136 minl = 1; 3137 minl = roundup(minl, segsiz); 3138 if (len < minl) 3139 len = minl; 3140 } 3141 /* 3142 * Now if we have a very small window we want 3143 * to attempt to get the window that is 3144 * as small as possible. This happens on 3145 * low b/w connections and we don't want to 3146 * span huge numbers of rtt's between measurements. 3147 * 3148 * We basically include 2 over our "MIN window" so 3149 * that the measurement can be shortened (possibly) by 3150 * an ack'ed packet. 3151 */ 3152 if (len < def_len) 3153 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz))); 3154 else 3155 return (max((uint32_t)len, def_len)); 3156 3157 } 3158 3159 static int 3160 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality) 3161 { 3162 uint32_t tim, srtts, segsiz; 3163 3164 /* 3165 * Has enough time passed for the GP measurement to be valid? 3166 */ 3167 if ((tp->snd_max == tp->snd_una) || 3168 (th_ack == tp->snd_max)){ 3169 /* All is acked */ 3170 *quality = RACK_QUALITY_ALLACKED; 3171 return (1); 3172 } 3173 if (SEQ_LT(th_ack, tp->gput_seq)) { 3174 /* Not enough bytes yet */ 3175 return (0); 3176 } 3177 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 3178 if (SEQ_LT(th_ack, tp->gput_ack) && 3179 ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) { 3180 /* Not enough bytes yet */ 3181 return (0); 3182 } 3183 if (rack->r_ctl.rc_first_appl && 3184 (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) { 3185 /* 3186 * We are up to the app limited send point 3187 * we have to measure irrespective of the time.. 3188 */ 3189 *quality = RACK_QUALITY_APPLIMITED; 3190 return (1); 3191 } 3192 /* Now what about time? */ 3193 srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts); 3194 tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts; 3195 if (tim >= srtts) { 3196 *quality = RACK_QUALITY_HIGH; 3197 return (1); 3198 } 3199 /* Nope not even a full SRTT has passed */ 3200 return (0); 3201 } 3202 3203 static void 3204 rack_log_timely(struct tcp_rack *rack, 3205 uint32_t logged, uint64_t cur_bw, uint64_t low_bnd, 3206 uint64_t up_bnd, int line, uint8_t method) 3207 { 3208 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 3209 union tcp_log_stackspecific log; 3210 struct timeval tv; 3211 3212 memset(&log, 0, sizeof(log)); 3213 log.u_bbr.flex1 = logged; 3214 log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt; 3215 log.u_bbr.flex2 <<= 4; 3216 log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt; 3217 log.u_bbr.flex2 <<= 4; 3218 log.u_bbr.flex2 |= rack->rc_gp_incr; 3219 log.u_bbr.flex2 <<= 4; 3220 log.u_bbr.flex2 |= rack->rc_gp_bwred; 3221 log.u_bbr.flex3 = rack->rc_gp_incr; 3222 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss; 3223 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca; 3224 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec; 3225 log.u_bbr.flex7 = rack->rc_gp_bwred; 3226 log.u_bbr.flex8 = method; 3227 log.u_bbr.cur_del_rate = cur_bw; 3228 log.u_bbr.delRate = low_bnd; 3229 log.u_bbr.bw_inuse = up_bnd; 3230 log.u_bbr.rttProp = rack_get_bw(rack); 3231 log.u_bbr.pkt_epoch = line; 3232 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff; 3233 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3234 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3235 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt; 3236 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt; 3237 log.u_bbr.cwnd_gain = rack->rc_dragged_bottom; 3238 log.u_bbr.cwnd_gain <<= 1; 3239 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec; 3240 log.u_bbr.cwnd_gain <<= 1; 3241 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss; 3242 log.u_bbr.cwnd_gain <<= 1; 3243 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca; 3244 log.u_bbr.lost = rack->r_ctl.rc_loss_count; 3245 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3246 &rack->rc_inp->inp_socket->so_rcv, 3247 &rack->rc_inp->inp_socket->so_snd, 3248 TCP_TIMELY_WORK, 0, 3249 0, &log, false, &tv); 3250 } 3251 } 3252 3253 static int 3254 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult) 3255 { 3256 /* 3257 * Before we increase we need to know if 3258 * the estimate just made was less than 3259 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est) 3260 * 3261 * If we already are pacing at a fast enough 3262 * rate to push us faster there is no sense of 3263 * increasing. 3264 * 3265 * We first caculate our actual pacing rate (ss or ca multipler 3266 * times our cur_bw). 3267 * 3268 * Then we take the last measured rate and multipy by our 3269 * maximum pacing overage to give us a max allowable rate. 3270 * 3271 * If our act_rate is smaller than our max_allowable rate 3272 * then we should increase. Else we should hold steady. 3273 * 3274 */ 3275 uint64_t act_rate, max_allow_rate; 3276 3277 if (rack_timely_no_stopping) 3278 return (1); 3279 3280 if ((cur_bw == 0) || (last_bw_est == 0)) { 3281 /* 3282 * Initial startup case or 3283 * everything is acked case. 3284 */ 3285 rack_log_timely(rack, mult, cur_bw, 0, 0, 3286 __LINE__, 9); 3287 return (1); 3288 } 3289 if (mult <= 100) { 3290 /* 3291 * We can always pace at or slightly above our rate. 3292 */ 3293 rack_log_timely(rack, mult, cur_bw, 0, 0, 3294 __LINE__, 9); 3295 return (1); 3296 } 3297 act_rate = cur_bw * (uint64_t)mult; 3298 act_rate /= 100; 3299 max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100); 3300 max_allow_rate /= 100; 3301 if (act_rate < max_allow_rate) { 3302 /* 3303 * Here the rate we are actually pacing at 3304 * is smaller than 10% above our last measurement. 3305 * This means we are pacing below what we would 3306 * like to try to achieve (plus some wiggle room). 3307 */ 3308 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate, 3309 __LINE__, 9); 3310 return (1); 3311 } else { 3312 /* 3313 * Here we are already pacing at least rack_max_per_above(10%) 3314 * what we are getting back. This indicates most likely 3315 * that we are being limited (cwnd/rwnd/app) and can't 3316 * get any more b/w. There is no sense of trying to 3317 * raise up the pacing rate its not speeding us up 3318 * and we already are pacing faster than we are getting. 3319 */ 3320 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate, 3321 __LINE__, 8); 3322 return (0); 3323 } 3324 } 3325 3326 static void 3327 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack) 3328 { 3329 /* 3330 * When we drag bottom, we want to assure 3331 * that no multiplier is below 1.0, if so 3332 * we want to restore it to at least that. 3333 */ 3334 if (rack->r_ctl.rack_per_of_gp_rec < 100) { 3335 /* This is unlikely we usually do not touch recovery */ 3336 rack->r_ctl.rack_per_of_gp_rec = 100; 3337 } 3338 if (rack->r_ctl.rack_per_of_gp_ca < 100) { 3339 rack->r_ctl.rack_per_of_gp_ca = 100; 3340 } 3341 if (rack->r_ctl.rack_per_of_gp_ss < 100) { 3342 rack->r_ctl.rack_per_of_gp_ss = 100; 3343 } 3344 } 3345 3346 static void 3347 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack) 3348 { 3349 if (rack->r_ctl.rack_per_of_gp_ca > 100) { 3350 rack->r_ctl.rack_per_of_gp_ca = 100; 3351 } 3352 if (rack->r_ctl.rack_per_of_gp_ss > 100) { 3353 rack->r_ctl.rack_per_of_gp_ss = 100; 3354 } 3355 } 3356 3357 static void 3358 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override) 3359 { 3360 int32_t calc, logged, plus; 3361 3362 logged = 0; 3363 3364 if (override) { 3365 /* 3366 * override is passed when we are 3367 * loosing b/w and making one last 3368 * gasp at trying to not loose out 3369 * to a new-reno flow. 3370 */ 3371 goto extra_boost; 3372 } 3373 /* In classic timely we boost by 5x if we have 5 increases in a row, lets not */ 3374 if (rack->rc_gp_incr && 3375 ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) { 3376 /* 3377 * Reset and get 5 strokes more before the boost. Note 3378 * that the count is 0 based so we have to add one. 3379 */ 3380 extra_boost: 3381 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST; 3382 rack->rc_gp_timely_inc_cnt = 0; 3383 } else 3384 plus = (uint32_t)rack_gp_increase_per; 3385 /* Must be at least 1% increase for true timely increases */ 3386 if ((plus < 1) && 3387 ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0))) 3388 plus = 1; 3389 if (rack->rc_gp_saw_rec && 3390 (rack->rc_gp_no_rec_chg == 0) && 3391 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 3392 rack->r_ctl.rack_per_of_gp_rec)) { 3393 /* We have been in recovery ding it too */ 3394 calc = rack->r_ctl.rack_per_of_gp_rec + plus; 3395 if (calc > 0xffff) 3396 calc = 0xffff; 3397 logged |= 1; 3398 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc; 3399 if (rack_per_upper_bound_ss && 3400 (rack->rc_dragged_bottom == 0) && 3401 (rack->r_ctl.rack_per_of_gp_rec > rack_per_upper_bound_ss)) 3402 rack->r_ctl.rack_per_of_gp_rec = rack_per_upper_bound_ss; 3403 } 3404 if (rack->rc_gp_saw_ca && 3405 (rack->rc_gp_saw_ss == 0) && 3406 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 3407 rack->r_ctl.rack_per_of_gp_ca)) { 3408 /* In CA */ 3409 calc = rack->r_ctl.rack_per_of_gp_ca + plus; 3410 if (calc > 0xffff) 3411 calc = 0xffff; 3412 logged |= 2; 3413 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc; 3414 if (rack_per_upper_bound_ca && 3415 (rack->rc_dragged_bottom == 0) && 3416 (rack->r_ctl.rack_per_of_gp_ca > rack_per_upper_bound_ca)) 3417 rack->r_ctl.rack_per_of_gp_ca = rack_per_upper_bound_ca; 3418 } 3419 if (rack->rc_gp_saw_ss && 3420 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 3421 rack->r_ctl.rack_per_of_gp_ss)) { 3422 /* In SS */ 3423 calc = rack->r_ctl.rack_per_of_gp_ss + plus; 3424 if (calc > 0xffff) 3425 calc = 0xffff; 3426 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc; 3427 if (rack_per_upper_bound_ss && 3428 (rack->rc_dragged_bottom == 0) && 3429 (rack->r_ctl.rack_per_of_gp_ss > rack_per_upper_bound_ss)) 3430 rack->r_ctl.rack_per_of_gp_ss = rack_per_upper_bound_ss; 3431 logged |= 4; 3432 } 3433 if (logged && 3434 (rack->rc_gp_incr == 0)){ 3435 /* Go into increment mode */ 3436 rack->rc_gp_incr = 1; 3437 rack->rc_gp_timely_inc_cnt = 0; 3438 } 3439 if (rack->rc_gp_incr && 3440 logged && 3441 (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) { 3442 rack->rc_gp_timely_inc_cnt++; 3443 } 3444 rack_log_timely(rack, logged, plus, 0, 0, 3445 __LINE__, 1); 3446 } 3447 3448 static uint32_t 3449 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff) 3450 { 3451 /* 3452 * norm_grad = rtt_diff / minrtt; 3453 * new_per = curper * (1 - B * norm_grad) 3454 * 3455 * B = rack_gp_decrease_per (default 10%) 3456 * rtt_dif = input var current rtt-diff 3457 * curper = input var current percentage 3458 * minrtt = from rack filter 3459 * 3460 */ 3461 uint64_t perf; 3462 3463 perf = (((uint64_t)curper * ((uint64_t)1000000 - 3464 ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 * 3465 (((uint64_t)rtt_diff * (uint64_t)1000000)/ 3466 (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/ 3467 (uint64_t)1000000)) / 3468 (uint64_t)1000000); 3469 if (perf > curper) { 3470 /* TSNH */ 3471 perf = curper - 1; 3472 } 3473 return ((uint32_t)perf); 3474 } 3475 3476 static uint32_t 3477 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt) 3478 { 3479 /* 3480 * highrttthresh 3481 * result = curper * (1 - (B * ( 1 - ------ )) 3482 * gp_srtt 3483 * 3484 * B = rack_gp_decrease_per (default 10%) 3485 * highrttthresh = filter_min * rack_gp_rtt_maxmul 3486 */ 3487 uint64_t perf; 3488 uint32_t highrttthresh; 3489 3490 highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul; 3491 3492 perf = (((uint64_t)curper * ((uint64_t)1000000 - 3493 ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 - 3494 ((uint64_t)highrttthresh * (uint64_t)1000000) / 3495 (uint64_t)rtt)) / 100)) /(uint64_t)1000000); 3496 return (perf); 3497 } 3498 3499 static void 3500 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff) 3501 { 3502 uint64_t logvar, logvar2, logvar3; 3503 uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val; 3504 3505 if (rack->rc_gp_incr) { 3506 /* Turn off increment counting */ 3507 rack->rc_gp_incr = 0; 3508 rack->rc_gp_timely_inc_cnt = 0; 3509 } 3510 ss_red = ca_red = rec_red = 0; 3511 logged = 0; 3512 /* Calculate the reduction value */ 3513 if (rtt_diff < 0) { 3514 rtt_diff *= -1; 3515 } 3516 /* Must be at least 1% reduction */ 3517 if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) { 3518 /* We have been in recovery ding it too */ 3519 if (timely_says == 2) { 3520 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt); 3521 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 3522 if (alt < new_per) 3523 val = alt; 3524 else 3525 val = new_per; 3526 } else 3527 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 3528 if (rack->r_ctl.rack_per_of_gp_rec > val) { 3529 rec_red = (rack->r_ctl.rack_per_of_gp_rec - val); 3530 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val; 3531 } else { 3532 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound; 3533 rec_red = 0; 3534 } 3535 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec) 3536 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound; 3537 logged |= 1; 3538 } 3539 if (rack->rc_gp_saw_ss) { 3540 /* Sent in SS */ 3541 if (timely_says == 2) { 3542 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt); 3543 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 3544 if (alt < new_per) 3545 val = alt; 3546 else 3547 val = new_per; 3548 } else 3549 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff); 3550 if (rack->r_ctl.rack_per_of_gp_ss > new_per) { 3551 ss_red = rack->r_ctl.rack_per_of_gp_ss - val; 3552 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val; 3553 } else { 3554 ss_red = new_per; 3555 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound; 3556 logvar = new_per; 3557 logvar <<= 32; 3558 logvar |= alt; 3559 logvar2 = (uint32_t)rtt; 3560 logvar2 <<= 32; 3561 logvar2 |= (uint32_t)rtt_diff; 3562 logvar3 = rack_gp_rtt_maxmul; 3563 logvar3 <<= 32; 3564 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 3565 rack_log_timely(rack, timely_says, 3566 logvar2, logvar3, 3567 logvar, __LINE__, 10); 3568 } 3569 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss) 3570 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound; 3571 logged |= 4; 3572 } else if (rack->rc_gp_saw_ca) { 3573 /* Sent in CA */ 3574 if (timely_says == 2) { 3575 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt); 3576 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 3577 if (alt < new_per) 3578 val = alt; 3579 else 3580 val = new_per; 3581 } else 3582 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff); 3583 if (rack->r_ctl.rack_per_of_gp_ca > val) { 3584 ca_red = rack->r_ctl.rack_per_of_gp_ca - val; 3585 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val; 3586 } else { 3587 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound; 3588 ca_red = 0; 3589 logvar = new_per; 3590 logvar <<= 32; 3591 logvar |= alt; 3592 logvar2 = (uint32_t)rtt; 3593 logvar2 <<= 32; 3594 logvar2 |= (uint32_t)rtt_diff; 3595 logvar3 = rack_gp_rtt_maxmul; 3596 logvar3 <<= 32; 3597 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 3598 rack_log_timely(rack, timely_says, 3599 logvar2, logvar3, 3600 logvar, __LINE__, 10); 3601 } 3602 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca) 3603 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound; 3604 logged |= 2; 3605 } 3606 if (rack->rc_gp_timely_dec_cnt < 0x7) { 3607 rack->rc_gp_timely_dec_cnt++; 3608 if (rack_timely_dec_clear && 3609 (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear)) 3610 rack->rc_gp_timely_dec_cnt = 0; 3611 } 3612 logvar = ss_red; 3613 logvar <<= 32; 3614 logvar |= ca_red; 3615 rack_log_timely(rack, logged, rec_red, rack_per_lower_bound, logvar, 3616 __LINE__, 2); 3617 } 3618 3619 static void 3620 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts, 3621 uint32_t rtt, uint32_t line, uint8_t reas) 3622 { 3623 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 3624 union tcp_log_stackspecific log; 3625 struct timeval tv; 3626 3627 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3628 log.u_bbr.flex1 = line; 3629 log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts; 3630 log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts; 3631 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss; 3632 log.u_bbr.flex5 = rtt; 3633 log.u_bbr.flex6 = rack->rc_highly_buffered; 3634 log.u_bbr.flex6 <<= 1; 3635 log.u_bbr.flex6 |= rack->forced_ack; 3636 log.u_bbr.flex6 <<= 1; 3637 log.u_bbr.flex6 |= rack->rc_gp_dyn_mul; 3638 log.u_bbr.flex6 <<= 1; 3639 log.u_bbr.flex6 |= rack->in_probe_rtt; 3640 log.u_bbr.flex6 <<= 1; 3641 log.u_bbr.flex6 |= rack->measure_saw_probe_rtt; 3642 log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt; 3643 log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca; 3644 log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec; 3645 log.u_bbr.flex8 = reas; 3646 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3647 log.u_bbr.delRate = rack_get_bw(rack); 3648 log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt; 3649 log.u_bbr.cur_del_rate <<= 32; 3650 log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt; 3651 log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered; 3652 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff; 3653 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3654 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt; 3655 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt; 3656 log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts; 3657 log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight; 3658 log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 3659 log.u_bbr.rttProp = us_cts; 3660 log.u_bbr.rttProp <<= 32; 3661 log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt; 3662 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3663 &rack->rc_inp->inp_socket->so_rcv, 3664 &rack->rc_inp->inp_socket->so_snd, 3665 BBR_LOG_RTT_SHRINKS, 0, 3666 0, &log, false, &rack->r_ctl.act_rcv_time); 3667 } 3668 } 3669 3670 static void 3671 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt) 3672 { 3673 uint64_t bwdp; 3674 3675 bwdp = rack_get_bw(rack); 3676 bwdp *= (uint64_t)rtt; 3677 bwdp /= (uint64_t)HPTS_USEC_IN_SEC; 3678 rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz); 3679 if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) { 3680 /* 3681 * A window protocol must be able to have 4 packets 3682 * outstanding as the floor in order to function 3683 * (especially considering delayed ack :D). 3684 */ 3685 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs); 3686 } 3687 } 3688 3689 static void 3690 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts) 3691 { 3692 /** 3693 * ProbeRTT is a bit different in rack_pacing than in 3694 * BBR. It is like BBR in that it uses the lowering of 3695 * the RTT as a signal that we saw something new and 3696 * counts from there for how long between. But it is 3697 * different in that its quite simple. It does not 3698 * play with the cwnd and wait until we get down 3699 * to N segments outstanding and hold that for 3700 * 200ms. Instead it just sets the pacing reduction 3701 * rate to a set percentage (70 by default) and hold 3702 * that for a number of recent GP Srtt's. 3703 */ 3704 uint32_t segsiz; 3705 3706 if (rack->rc_gp_dyn_mul == 0) 3707 return; 3708 3709 if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) { 3710 /* We are idle */ 3711 return; 3712 } 3713 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) && 3714 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) { 3715 /* 3716 * Stop the goodput now, the idea here is 3717 * that future measurements with in_probe_rtt 3718 * won't register if they are not greater so 3719 * we want to get what info (if any) is available 3720 * now. 3721 */ 3722 rack_do_goodput_measurement(rack->rc_tp, rack, 3723 rack->rc_tp->snd_una, __LINE__, 3724 RACK_QUALITY_PROBERTT); 3725 } 3726 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 3727 rack->r_ctl.rc_time_probertt_entered = us_cts; 3728 segsiz = min(ctf_fixed_maxseg(rack->rc_tp), 3729 rack->r_ctl.rc_pace_min_segs); 3730 rack->in_probe_rtt = 1; 3731 rack->measure_saw_probe_rtt = 1; 3732 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 3733 rack->r_ctl.rc_time_probertt_starts = 0; 3734 rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt; 3735 if (rack_probertt_use_min_rtt_entry) 3736 rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)); 3737 else 3738 rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt); 3739 rack_log_rtt_shrinks(rack, us_cts, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 3740 __LINE__, RACK_RTTS_ENTERPROBE); 3741 } 3742 3743 static void 3744 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts) 3745 { 3746 struct rack_sendmap *rsm; 3747 uint32_t segsiz; 3748 3749 segsiz = min(ctf_fixed_maxseg(rack->rc_tp), 3750 rack->r_ctl.rc_pace_min_segs); 3751 rack->in_probe_rtt = 0; 3752 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) && 3753 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) { 3754 /* 3755 * Stop the goodput now, the idea here is 3756 * that future measurements with in_probe_rtt 3757 * won't register if they are not greater so 3758 * we want to get what info (if any) is available 3759 * now. 3760 */ 3761 rack_do_goodput_measurement(rack->rc_tp, rack, 3762 rack->rc_tp->snd_una, __LINE__, 3763 RACK_QUALITY_PROBERTT); 3764 } else if (rack->rc_tp->t_flags & TF_GPUTINPROG) { 3765 /* 3766 * We don't have enough data to make a measurement. 3767 * So lets just stop and start here after exiting 3768 * probe-rtt. We probably are not interested in 3769 * the results anyway. 3770 */ 3771 rack->rc_tp->t_flags &= ~TF_GPUTINPROG; 3772 } 3773 /* 3774 * Measurements through the current snd_max are going 3775 * to be limited by the slower pacing rate. 3776 * 3777 * We need to mark these as app-limited so we 3778 * don't collapse the b/w. 3779 */ 3780 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 3781 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) { 3782 if (rack->r_ctl.rc_app_limited_cnt == 0) 3783 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm; 3784 else { 3785 /* 3786 * Go out to the end app limited and mark 3787 * this new one as next and move the end_appl up 3788 * to this guy. 3789 */ 3790 if (rack->r_ctl.rc_end_appl) 3791 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start; 3792 rack->r_ctl.rc_end_appl = rsm; 3793 } 3794 rsm->r_flags |= RACK_APP_LIMITED; 3795 rack->r_ctl.rc_app_limited_cnt++; 3796 } 3797 /* 3798 * Now, we need to examine our pacing rate multipliers. 3799 * If its under 100%, we need to kick it back up to 3800 * 100%. We also don't let it be over our "max" above 3801 * the actual rate i.e. 100% + rack_clamp_atexit_prtt. 3802 * Note setting clamp_atexit_prtt to 0 has the effect 3803 * of setting CA/SS to 100% always at exit (which is 3804 * the default behavior). 3805 */ 3806 if (rack_probertt_clear_is) { 3807 rack->rc_gp_incr = 0; 3808 rack->rc_gp_bwred = 0; 3809 rack->rc_gp_timely_inc_cnt = 0; 3810 rack->rc_gp_timely_dec_cnt = 0; 3811 } 3812 /* Do we do any clamping at exit? */ 3813 if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) { 3814 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp; 3815 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp; 3816 } 3817 if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) { 3818 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt; 3819 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt; 3820 } 3821 /* 3822 * Lets set rtt_diff to 0, so that we will get a "boost" 3823 * after exiting. 3824 */ 3825 rack->r_ctl.rc_rtt_diff = 0; 3826 3827 /* Clear all flags so we start fresh */ 3828 rack->rc_tp->t_bytes_acked = 0; 3829 rack->rc_tp->ccv->flags &= ~CCF_ABC_SENTAWND; 3830 /* 3831 * If configured to, set the cwnd and ssthresh to 3832 * our targets. 3833 */ 3834 if (rack_probe_rtt_sets_cwnd) { 3835 uint64_t ebdp; 3836 uint32_t setto; 3837 3838 /* Set ssthresh so we get into CA once we hit our target */ 3839 if (rack_probertt_use_min_rtt_exit == 1) { 3840 /* Set to min rtt */ 3841 rack_set_prtt_target(rack, segsiz, 3842 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)); 3843 } else if (rack_probertt_use_min_rtt_exit == 2) { 3844 /* Set to current gp rtt */ 3845 rack_set_prtt_target(rack, segsiz, 3846 rack->r_ctl.rc_gp_srtt); 3847 } else if (rack_probertt_use_min_rtt_exit == 3) { 3848 /* Set to entry gp rtt */ 3849 rack_set_prtt_target(rack, segsiz, 3850 rack->r_ctl.rc_entry_gp_rtt); 3851 } else { 3852 uint64_t sum; 3853 uint32_t setval; 3854 3855 sum = rack->r_ctl.rc_entry_gp_rtt; 3856 sum *= 10; 3857 sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt)); 3858 if (sum >= 20) { 3859 /* 3860 * A highly buffered path needs 3861 * cwnd space for timely to work. 3862 * Lets set things up as if 3863 * we are heading back here again. 3864 */ 3865 setval = rack->r_ctl.rc_entry_gp_rtt; 3866 } else if (sum >= 15) { 3867 /* 3868 * Lets take the smaller of the 3869 * two since we are just somewhat 3870 * buffered. 3871 */ 3872 setval = rack->r_ctl.rc_gp_srtt; 3873 if (setval > rack->r_ctl.rc_entry_gp_rtt) 3874 setval = rack->r_ctl.rc_entry_gp_rtt; 3875 } else { 3876 /* 3877 * Here we are not highly buffered 3878 * and should pick the min we can to 3879 * keep from causing loss. 3880 */ 3881 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 3882 } 3883 rack_set_prtt_target(rack, segsiz, 3884 setval); 3885 } 3886 if (rack_probe_rtt_sets_cwnd > 1) { 3887 /* There is a percentage here to boost */ 3888 ebdp = rack->r_ctl.rc_target_probertt_flight; 3889 ebdp *= rack_probe_rtt_sets_cwnd; 3890 ebdp /= 100; 3891 setto = rack->r_ctl.rc_target_probertt_flight + ebdp; 3892 } else 3893 setto = rack->r_ctl.rc_target_probertt_flight; 3894 rack->rc_tp->snd_cwnd = roundup(setto, segsiz); 3895 if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) { 3896 /* Enforce a min */ 3897 rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs; 3898 } 3899 /* If we set in the cwnd also set the ssthresh point so we are in CA */ 3900 rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1); 3901 } 3902 rack_log_rtt_shrinks(rack, us_cts, 3903 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 3904 __LINE__, RACK_RTTS_EXITPROBE); 3905 /* Clear times last so log has all the info */ 3906 rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max; 3907 rack->r_ctl.rc_time_probertt_entered = us_cts; 3908 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 3909 rack->r_ctl.rc_time_of_last_probertt = us_cts; 3910 } 3911 3912 static void 3913 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts) 3914 { 3915 /* Check in on probe-rtt */ 3916 if (rack->rc_gp_filled == 0) { 3917 /* We do not do p-rtt unless we have gp measurements */ 3918 return; 3919 } 3920 if (rack->in_probe_rtt) { 3921 uint64_t no_overflow; 3922 uint32_t endtime, must_stay; 3923 3924 if (rack->r_ctl.rc_went_idle_time && 3925 ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) { 3926 /* 3927 * We went idle during prtt, just exit now. 3928 */ 3929 rack_exit_probertt(rack, us_cts); 3930 } else if (rack_probe_rtt_safety_val && 3931 TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) && 3932 ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) { 3933 /* 3934 * Probe RTT safety value triggered! 3935 */ 3936 rack_log_rtt_shrinks(rack, us_cts, 3937 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 3938 __LINE__, RACK_RTTS_SAFETY); 3939 rack_exit_probertt(rack, us_cts); 3940 } 3941 /* Calculate the max we will wait */ 3942 endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait); 3943 if (rack->rc_highly_buffered) 3944 endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp); 3945 /* Calculate the min we must wait */ 3946 must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain); 3947 if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) && 3948 TSTMP_LT(us_cts, endtime)) { 3949 uint32_t calc; 3950 /* Do we lower more? */ 3951 no_exit: 3952 if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered)) 3953 calc = us_cts - rack->r_ctl.rc_time_probertt_entered; 3954 else 3955 calc = 0; 3956 calc /= max(rack->r_ctl.rc_gp_srtt, 1); 3957 if (calc) { 3958 /* Maybe */ 3959 calc *= rack_per_of_gp_probertt_reduce; 3960 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc; 3961 /* Limit it too */ 3962 if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh) 3963 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh; 3964 } 3965 /* We must reach target or the time set */ 3966 return; 3967 } 3968 if (rack->r_ctl.rc_time_probertt_starts == 0) { 3969 if ((TSTMP_LT(us_cts, must_stay) && 3970 rack->rc_highly_buffered) || 3971 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > 3972 rack->r_ctl.rc_target_probertt_flight)) { 3973 /* We are not past the must_stay time */ 3974 goto no_exit; 3975 } 3976 rack_log_rtt_shrinks(rack, us_cts, 3977 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 3978 __LINE__, RACK_RTTS_REACHTARGET); 3979 rack->r_ctl.rc_time_probertt_starts = us_cts; 3980 if (rack->r_ctl.rc_time_probertt_starts == 0) 3981 rack->r_ctl.rc_time_probertt_starts = 1; 3982 /* Restore back to our rate we want to pace at in prtt */ 3983 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 3984 } 3985 /* 3986 * Setup our end time, some number of gp_srtts plus 200ms. 3987 */ 3988 no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt * 3989 (uint64_t)rack_probertt_gpsrtt_cnt_mul); 3990 if (rack_probertt_gpsrtt_cnt_div) 3991 endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div); 3992 else 3993 endtime = 0; 3994 endtime += rack_min_probertt_hold; 3995 endtime += rack->r_ctl.rc_time_probertt_starts; 3996 if (TSTMP_GEQ(us_cts, endtime)) { 3997 /* yes, exit probertt */ 3998 rack_exit_probertt(rack, us_cts); 3999 } 4000 4001 } else if ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt) { 4002 /* Go into probertt, its been too long since we went lower */ 4003 rack_enter_probertt(rack, us_cts); 4004 } 4005 } 4006 4007 static void 4008 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est, 4009 uint32_t rtt, int32_t rtt_diff) 4010 { 4011 uint64_t cur_bw, up_bnd, low_bnd, subfr; 4012 uint32_t losses; 4013 4014 if ((rack->rc_gp_dyn_mul == 0) || 4015 (rack->use_fixed_rate) || 4016 (rack->in_probe_rtt) || 4017 (rack->rc_always_pace == 0)) { 4018 /* No dynamic GP multipler in play */ 4019 return; 4020 } 4021 losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start; 4022 cur_bw = rack_get_bw(rack); 4023 /* Calculate our up and down range */ 4024 up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up; 4025 up_bnd /= 100; 4026 up_bnd += rack->r_ctl.last_gp_comp_bw; 4027 4028 subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down; 4029 subfr /= 100; 4030 low_bnd = rack->r_ctl.last_gp_comp_bw - subfr; 4031 if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) { 4032 /* 4033 * This is the case where our RTT is above 4034 * the max target and we have been configured 4035 * to just do timely no bonus up stuff in that case. 4036 * 4037 * There are two configurations, set to 1, and we 4038 * just do timely if we are over our max. If its 4039 * set above 1 then we slam the multipliers down 4040 * to 100 and then decrement per timely. 4041 */ 4042 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4043 __LINE__, 3); 4044 if (rack->r_ctl.rc_no_push_at_mrtt > 1) 4045 rack_validate_multipliers_at_or_below_100(rack); 4046 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff); 4047 } else if ((last_bw_est < low_bnd) && !losses) { 4048 /* 4049 * We are decreasing this is a bit complicated this 4050 * means we are loosing ground. This could be 4051 * because another flow entered and we are competing 4052 * for b/w with it. This will push the RTT up which 4053 * makes timely unusable unless we want to get shoved 4054 * into a corner and just be backed off (the age 4055 * old problem with delay based CC). 4056 * 4057 * On the other hand if it was a route change we 4058 * would like to stay somewhat contained and not 4059 * blow out the buffers. 4060 */ 4061 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4062 __LINE__, 3); 4063 rack->r_ctl.last_gp_comp_bw = cur_bw; 4064 if (rack->rc_gp_bwred == 0) { 4065 /* Go into reduction counting */ 4066 rack->rc_gp_bwred = 1; 4067 rack->rc_gp_timely_dec_cnt = 0; 4068 } 4069 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) || 4070 (timely_says == 0)) { 4071 /* 4072 * Push another time with a faster pacing 4073 * to try to gain back (we include override to 4074 * get a full raise factor). 4075 */ 4076 if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) || 4077 (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) || 4078 (timely_says == 0) || 4079 (rack_down_raise_thresh == 0)) { 4080 /* 4081 * Do an override up in b/w if we were 4082 * below the threshold or if the threshold 4083 * is zero we always do the raise. 4084 */ 4085 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1); 4086 } else { 4087 /* Log it stays the same */ 4088 rack_log_timely(rack, 0, last_bw_est, low_bnd, 0, 4089 __LINE__, 11); 4090 } 4091 rack->rc_gp_timely_dec_cnt++; 4092 /* We are not incrementing really no-count */ 4093 rack->rc_gp_incr = 0; 4094 rack->rc_gp_timely_inc_cnt = 0; 4095 } else { 4096 /* 4097 * Lets just use the RTT 4098 * information and give up 4099 * pushing. 4100 */ 4101 goto use_timely; 4102 } 4103 } else if ((timely_says != 2) && 4104 !losses && 4105 (last_bw_est > up_bnd)) { 4106 /* 4107 * We are increasing b/w lets keep going, updating 4108 * our b/w and ignoring any timely input, unless 4109 * of course we are at our max raise (if there is one). 4110 */ 4111 4112 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4113 __LINE__, 3); 4114 rack->r_ctl.last_gp_comp_bw = cur_bw; 4115 if (rack->rc_gp_saw_ss && 4116 rack_per_upper_bound_ss && 4117 (rack->r_ctl.rack_per_of_gp_ss == rack_per_upper_bound_ss)) { 4118 /* 4119 * In cases where we can't go higher 4120 * we should just use timely. 4121 */ 4122 goto use_timely; 4123 } 4124 if (rack->rc_gp_saw_ca && 4125 rack_per_upper_bound_ca && 4126 (rack->r_ctl.rack_per_of_gp_ca == rack_per_upper_bound_ca)) { 4127 /* 4128 * In cases where we can't go higher 4129 * we should just use timely. 4130 */ 4131 goto use_timely; 4132 } 4133 rack->rc_gp_bwred = 0; 4134 rack->rc_gp_timely_dec_cnt = 0; 4135 /* You get a set number of pushes if timely is trying to reduce */ 4136 if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) { 4137 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4138 } else { 4139 /* Log it stays the same */ 4140 rack_log_timely(rack, 0, last_bw_est, up_bnd, 0, 4141 __LINE__, 12); 4142 } 4143 return; 4144 } else { 4145 /* 4146 * We are staying between the lower and upper range bounds 4147 * so use timely to decide. 4148 */ 4149 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4150 __LINE__, 3); 4151 use_timely: 4152 if (timely_says) { 4153 rack->rc_gp_incr = 0; 4154 rack->rc_gp_timely_inc_cnt = 0; 4155 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) && 4156 !losses && 4157 (last_bw_est < low_bnd)) { 4158 /* We are loosing ground */ 4159 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4160 rack->rc_gp_timely_dec_cnt++; 4161 /* We are not incrementing really no-count */ 4162 rack->rc_gp_incr = 0; 4163 rack->rc_gp_timely_inc_cnt = 0; 4164 } else 4165 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff); 4166 } else { 4167 rack->rc_gp_bwred = 0; 4168 rack->rc_gp_timely_dec_cnt = 0; 4169 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4170 } 4171 } 4172 } 4173 4174 static int32_t 4175 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt) 4176 { 4177 int32_t timely_says; 4178 uint64_t log_mult, log_rtt_a_diff; 4179 4180 log_rtt_a_diff = rtt; 4181 log_rtt_a_diff <<= 32; 4182 log_rtt_a_diff |= (uint32_t)rtt_diff; 4183 if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * 4184 rack_gp_rtt_maxmul)) { 4185 /* Reduce the b/w multipler */ 4186 timely_says = 2; 4187 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul; 4188 log_mult <<= 32; 4189 log_mult |= prev_rtt; 4190 rack_log_timely(rack, timely_says, log_mult, 4191 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4192 log_rtt_a_diff, __LINE__, 4); 4193 } else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) + 4194 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) / 4195 max(rack_gp_rtt_mindiv , 1)))) { 4196 /* Increase the b/w multipler */ 4197 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) + 4198 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) / 4199 max(rack_gp_rtt_mindiv , 1)); 4200 log_mult <<= 32; 4201 log_mult |= prev_rtt; 4202 timely_says = 0; 4203 rack_log_timely(rack, timely_says, log_mult , 4204 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4205 log_rtt_a_diff, __LINE__, 5); 4206 } else { 4207 /* 4208 * Use a gradient to find it the timely gradient 4209 * is: 4210 * grad = rc_rtt_diff / min_rtt; 4211 * 4212 * anything below or equal to 0 will be 4213 * a increase indication. Anything above 4214 * zero is a decrease. Note we take care 4215 * of the actual gradient calculation 4216 * in the reduction (its not needed for 4217 * increase). 4218 */ 4219 log_mult = prev_rtt; 4220 if (rtt_diff <= 0) { 4221 /* 4222 * Rttdiff is less than zero, increase the 4223 * b/w multipler (its 0 or negative) 4224 */ 4225 timely_says = 0; 4226 rack_log_timely(rack, timely_says, log_mult, 4227 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6); 4228 } else { 4229 /* Reduce the b/w multipler */ 4230 timely_says = 1; 4231 rack_log_timely(rack, timely_says, log_mult, 4232 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7); 4233 } 4234 } 4235 return (timely_says); 4236 } 4237 4238 static void 4239 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack, 4240 tcp_seq th_ack, int line, uint8_t quality) 4241 { 4242 uint64_t tim, bytes_ps, ltim, stim, utim; 4243 uint32_t segsiz, bytes, reqbytes, us_cts; 4244 int32_t gput, new_rtt_diff, timely_says; 4245 uint64_t resid_bw, subpart = 0, addpart = 0, srtt; 4246 int did_add = 0; 4247 4248 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 4249 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 4250 if (TSTMP_GEQ(us_cts, tp->gput_ts)) 4251 tim = us_cts - tp->gput_ts; 4252 else 4253 tim = 0; 4254 if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts) 4255 stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts; 4256 else 4257 stim = 0; 4258 /* 4259 * Use the larger of the send time or ack time. This prevents us 4260 * from being influenced by ack artifacts to come up with too 4261 * high of measurement. Note that since we are spanning over many more 4262 * bytes in most of our measurements hopefully that is less likely to 4263 * occur. 4264 */ 4265 if (tim > stim) 4266 utim = max(tim, 1); 4267 else 4268 utim = max(stim, 1); 4269 /* Lets get a msec time ltim too for the old stuff */ 4270 ltim = max(1, (utim / HPTS_USEC_IN_MSEC)); 4271 gput = (((uint64_t) (th_ack - tp->gput_seq)) << 3) / ltim; 4272 reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz)); 4273 if ((tim == 0) && (stim == 0)) { 4274 /* 4275 * Invalid measurement time, maybe 4276 * all on one ack/one send? 4277 */ 4278 bytes = 0; 4279 bytes_ps = 0; 4280 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 4281 0, 0, 0, 10, __LINE__, NULL, quality); 4282 goto skip_measurement; 4283 } 4284 if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) { 4285 /* We never made a us_rtt measurement? */ 4286 bytes = 0; 4287 bytes_ps = 0; 4288 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 4289 0, 0, 0, 10, __LINE__, NULL, quality); 4290 goto skip_measurement; 4291 } 4292 /* 4293 * Calculate the maximum possible b/w this connection 4294 * could have. We base our calculation on the lowest 4295 * rtt we have seen during the measurement and the 4296 * largest rwnd the client has given us in that time. This 4297 * forms a BDP that is the maximum that we could ever 4298 * get to the client. Anything larger is not valid. 4299 * 4300 * I originally had code here that rejected measurements 4301 * where the time was less than 1/2 the latest us_rtt. 4302 * But after thinking on that I realized its wrong since 4303 * say you had a 150Mbps or even 1Gbps link, and you 4304 * were a long way away.. example I am in Europe (100ms rtt) 4305 * talking to my 1Gbps link in S.C. Now measuring say 150,000 4306 * bytes my time would be 1.2ms, and yet my rtt would say 4307 * the measurement was invalid the time was < 50ms. The 4308 * same thing is true for 150Mb (8ms of time). 4309 * 4310 * A better way I realized is to look at what the maximum 4311 * the connection could possibly do. This is gated on 4312 * the lowest RTT we have seen and the highest rwnd. 4313 * We should in theory never exceed that, if we are 4314 * then something on the path is storing up packets 4315 * and then feeding them all at once to our endpoint 4316 * messing up our measurement. 4317 */ 4318 rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd; 4319 rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC; 4320 rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt; 4321 if (SEQ_LT(th_ack, tp->gput_seq)) { 4322 /* No measurement can be made */ 4323 bytes = 0; 4324 bytes_ps = 0; 4325 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 4326 0, 0, 0, 10, __LINE__, NULL, quality); 4327 goto skip_measurement; 4328 } else 4329 bytes = (th_ack - tp->gput_seq); 4330 bytes_ps = (uint64_t)bytes; 4331 /* 4332 * Don't measure a b/w for pacing unless we have gotten at least 4333 * an initial windows worth of data in this measurement interval. 4334 * 4335 * Small numbers of bytes get badly influenced by delayed ack and 4336 * other artifacts. Note we take the initial window or our 4337 * defined minimum GP (defaulting to 10 which hopefully is the 4338 * IW). 4339 */ 4340 if (rack->rc_gp_filled == 0) { 4341 /* 4342 * The initial estimate is special. We 4343 * have blasted out an IW worth of packets 4344 * without a real valid ack ts results. We 4345 * then setup the app_limited_needs_set flag, 4346 * this should get the first ack in (probably 2 4347 * MSS worth) to be recorded as the timestamp. 4348 * We thus allow a smaller number of bytes i.e. 4349 * IW - 2MSS. 4350 */ 4351 reqbytes -= (2 * segsiz); 4352 /* Also lets fill previous for our first measurement to be neutral */ 4353 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt; 4354 } 4355 if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) { 4356 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 4357 rack->r_ctl.rc_app_limited_cnt, 4358 0, 0, 10, __LINE__, NULL, quality); 4359 goto skip_measurement; 4360 } 4361 /* 4362 * We now need to calculate the Timely like status so 4363 * we can update (possibly) the b/w multipliers. 4364 */ 4365 new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt; 4366 if (rack->rc_gp_filled == 0) { 4367 /* No previous reading */ 4368 rack->r_ctl.rc_rtt_diff = new_rtt_diff; 4369 } else { 4370 if (rack->measure_saw_probe_rtt == 0) { 4371 /* 4372 * We don't want a probertt to be counted 4373 * since it will be negative incorrectly. We 4374 * expect to be reducing the RTT when we 4375 * pace at a slower rate. 4376 */ 4377 rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8); 4378 rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8); 4379 } 4380 } 4381 timely_says = rack_make_timely_judgement(rack, 4382 rack->r_ctl.rc_gp_srtt, 4383 rack->r_ctl.rc_rtt_diff, 4384 rack->r_ctl.rc_prev_gp_srtt 4385 ); 4386 bytes_ps *= HPTS_USEC_IN_SEC; 4387 bytes_ps /= utim; 4388 if (bytes_ps > rack->r_ctl.last_max_bw) { 4389 /* 4390 * Something is on path playing 4391 * since this b/w is not possible based 4392 * on our BDP (highest rwnd and lowest rtt 4393 * we saw in the measurement window). 4394 * 4395 * Another option here would be to 4396 * instead skip the measurement. 4397 */ 4398 rack_log_pacing_delay_calc(rack, bytes, reqbytes, 4399 bytes_ps, rack->r_ctl.last_max_bw, 0, 4400 11, __LINE__, NULL, quality); 4401 bytes_ps = rack->r_ctl.last_max_bw; 4402 } 4403 /* We store gp for b/w in bytes per second */ 4404 if (rack->rc_gp_filled == 0) { 4405 /* Initial measurment */ 4406 if (bytes_ps) { 4407 rack->r_ctl.gp_bw = bytes_ps; 4408 rack->rc_gp_filled = 1; 4409 rack->r_ctl.num_measurements = 1; 4410 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 4411 } else { 4412 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 4413 rack->r_ctl.rc_app_limited_cnt, 4414 0, 0, 10, __LINE__, NULL, quality); 4415 } 4416 if (rack->rc_inp->inp_in_hpts && 4417 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 4418 /* 4419 * Ok we can't trust the pacer in this case 4420 * where we transition from un-paced to paced. 4421 * Or for that matter when the burst mitigation 4422 * was making a wild guess and got it wrong. 4423 * Stop the pacer and clear up all the aggregate 4424 * delays etc. 4425 */ 4426 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 4427 rack->r_ctl.rc_hpts_flags = 0; 4428 rack->r_ctl.rc_last_output_to = 0; 4429 } 4430 did_add = 2; 4431 } else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) { 4432 /* Still a small number run an average */ 4433 rack->r_ctl.gp_bw += bytes_ps; 4434 addpart = rack->r_ctl.num_measurements; 4435 rack->r_ctl.num_measurements++; 4436 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) { 4437 /* We have collected enought to move forward */ 4438 rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements; 4439 } 4440 did_add = 3; 4441 } else { 4442 /* 4443 * We want to take 1/wma of the goodput and add in to 7/8th 4444 * of the old value weighted by the srtt. So if your measurement 4445 * period is say 2 SRTT's long you would get 1/4 as the 4446 * value, if it was like 1/2 SRTT then you would get 1/16th. 4447 * 4448 * But we must be careful not to take too much i.e. if the 4449 * srtt is say 20ms and the measurement is taken over 4450 * 400ms our weight would be 400/20 i.e. 20. On the 4451 * other hand if we get a measurement over 1ms with a 4452 * 10ms rtt we only want to take a much smaller portion. 4453 */ 4454 if (rack->r_ctl.num_measurements < 0xff) { 4455 rack->r_ctl.num_measurements++; 4456 } 4457 srtt = (uint64_t)tp->t_srtt; 4458 if (srtt == 0) { 4459 /* 4460 * Strange why did t_srtt go back to zero? 4461 */ 4462 if (rack->r_ctl.rc_rack_min_rtt) 4463 srtt = rack->r_ctl.rc_rack_min_rtt; 4464 else 4465 srtt = HPTS_USEC_IN_MSEC; 4466 } 4467 /* 4468 * XXXrrs: Note for reviewers, in playing with 4469 * dynamic pacing I discovered this GP calculation 4470 * as done originally leads to some undesired results. 4471 * Basically you can get longer measurements contributing 4472 * too much to the WMA. Thus I changed it if you are doing 4473 * dynamic adjustments to only do the aportioned adjustment 4474 * if we have a very small (time wise) measurement. Longer 4475 * measurements just get there weight (defaulting to 1/8) 4476 * add to the WMA. We may want to think about changing 4477 * this to always do that for both sides i.e. dynamic 4478 * and non-dynamic... but considering lots of folks 4479 * were playing with this I did not want to change the 4480 * calculation per.se. without your thoughts.. Lawerence? 4481 * Peter?? 4482 */ 4483 if (rack->rc_gp_dyn_mul == 0) { 4484 subpart = rack->r_ctl.gp_bw * utim; 4485 subpart /= (srtt * 8); 4486 if (subpart < (rack->r_ctl.gp_bw / 2)) { 4487 /* 4488 * The b/w update takes no more 4489 * away then 1/2 our running total 4490 * so factor it in. 4491 */ 4492 addpart = bytes_ps * utim; 4493 addpart /= (srtt * 8); 4494 } else { 4495 /* 4496 * Don't allow a single measurement 4497 * to account for more than 1/2 of the 4498 * WMA. This could happen on a retransmission 4499 * where utim becomes huge compared to 4500 * srtt (multiple retransmissions when using 4501 * the sending rate which factors in all the 4502 * transmissions from the first one). 4503 */ 4504 subpart = rack->r_ctl.gp_bw / 2; 4505 addpart = bytes_ps / 2; 4506 } 4507 resid_bw = rack->r_ctl.gp_bw - subpart; 4508 rack->r_ctl.gp_bw = resid_bw + addpart; 4509 did_add = 1; 4510 } else { 4511 if ((utim / srtt) <= 1) { 4512 /* 4513 * The b/w update was over a small period 4514 * of time. The idea here is to prevent a small 4515 * measurement time period from counting 4516 * too much. So we scale it based on the 4517 * time so it attributes less than 1/rack_wma_divisor 4518 * of its measurement. 4519 */ 4520 subpart = rack->r_ctl.gp_bw * utim; 4521 subpart /= (srtt * rack_wma_divisor); 4522 addpart = bytes_ps * utim; 4523 addpart /= (srtt * rack_wma_divisor); 4524 } else { 4525 /* 4526 * The scaled measurement was long 4527 * enough so lets just add in the 4528 * portion of the measurment i.e. 1/rack_wma_divisor 4529 */ 4530 subpart = rack->r_ctl.gp_bw / rack_wma_divisor; 4531 addpart = bytes_ps / rack_wma_divisor; 4532 } 4533 if ((rack->measure_saw_probe_rtt == 0) || 4534 (bytes_ps > rack->r_ctl.gp_bw)) { 4535 /* 4536 * For probe-rtt we only add it in 4537 * if its larger, all others we just 4538 * add in. 4539 */ 4540 did_add = 1; 4541 resid_bw = rack->r_ctl.gp_bw - subpart; 4542 rack->r_ctl.gp_bw = resid_bw + addpart; 4543 } 4544 } 4545 } 4546 if ((rack->gp_ready == 0) && 4547 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) { 4548 /* We have enough measurements now */ 4549 rack->gp_ready = 1; 4550 rack_set_cc_pacing(rack); 4551 if (rack->defer_options) 4552 rack_apply_deferred_options(rack); 4553 } 4554 rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim, 4555 rack_get_bw(rack), 22, did_add, NULL, quality); 4556 /* We do not update any multipliers if we are in or have seen a probe-rtt */ 4557 if ((rack->measure_saw_probe_rtt == 0) && rack->rc_gp_rtt_set) 4558 rack_update_multiplier(rack, timely_says, bytes_ps, 4559 rack->r_ctl.rc_gp_srtt, 4560 rack->r_ctl.rc_rtt_diff); 4561 rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim, 4562 rack_get_bw(rack), 3, line, NULL, quality); 4563 /* reset the gp srtt and setup the new prev */ 4564 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt; 4565 /* Record the lost count for the next measurement */ 4566 rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count; 4567 /* 4568 * We restart our diffs based on the gpsrtt in the 4569 * measurement window. 4570 */ 4571 rack->rc_gp_rtt_set = 0; 4572 rack->rc_gp_saw_rec = 0; 4573 rack->rc_gp_saw_ca = 0; 4574 rack->rc_gp_saw_ss = 0; 4575 rack->rc_dragged_bottom = 0; 4576 skip_measurement: 4577 4578 #ifdef STATS 4579 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 4580 gput); 4581 /* 4582 * XXXLAS: This is a temporary hack, and should be 4583 * chained off VOI_TCP_GPUT when stats(9) grows an 4584 * API to deal with chained VOIs. 4585 */ 4586 if (tp->t_stats_gput_prev > 0) 4587 stats_voi_update_abs_s32(tp->t_stats, 4588 VOI_TCP_GPUT_ND, 4589 ((gput - tp->t_stats_gput_prev) * 100) / 4590 tp->t_stats_gput_prev); 4591 #endif 4592 tp->t_flags &= ~TF_GPUTINPROG; 4593 tp->t_stats_gput_prev = gput; 4594 /* 4595 * Now are we app limited now and there is space from where we 4596 * were to where we want to go? 4597 * 4598 * We don't do the other case i.e. non-applimited here since 4599 * the next send will trigger us picking up the missing data. 4600 */ 4601 if (rack->r_ctl.rc_first_appl && 4602 TCPS_HAVEESTABLISHED(tp->t_state) && 4603 rack->r_ctl.rc_app_limited_cnt && 4604 (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) && 4605 ((rack->r_ctl.rc_first_appl->r_end - th_ack) > 4606 max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) { 4607 /* 4608 * Yep there is enough outstanding to make a measurement here. 4609 */ 4610 struct rack_sendmap *rsm, fe; 4611 4612 rack->r_ctl.rc_gp_lowrtt = 0xffffffff; 4613 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 4614 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 4615 rack->app_limited_needs_set = 0; 4616 tp->gput_seq = th_ack; 4617 if (rack->in_probe_rtt) 4618 rack->measure_saw_probe_rtt = 1; 4619 else if ((rack->measure_saw_probe_rtt) && 4620 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 4621 rack->measure_saw_probe_rtt = 0; 4622 if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) { 4623 /* There is a full window to gain info from */ 4624 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 4625 } else { 4626 /* We can only measure up to the applimited point */ 4627 tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack); 4628 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) { 4629 /* 4630 * We don't have enough to make a measurement. 4631 */ 4632 tp->t_flags &= ~TF_GPUTINPROG; 4633 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq, 4634 0, 0, 0, 6, __LINE__, NULL, quality); 4635 return; 4636 } 4637 } 4638 if (tp->t_state >= TCPS_FIN_WAIT_1) { 4639 /* 4640 * We will get no more data into the SB 4641 * this means we need to have the data available 4642 * before we start a measurement. 4643 */ 4644 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < (tp->gput_ack - tp->gput_seq)) { 4645 /* Nope not enough data. */ 4646 return; 4647 } 4648 } 4649 tp->t_flags |= TF_GPUTINPROG; 4650 /* 4651 * Now we need to find the timestamp of the send at tp->gput_seq 4652 * for the send based measurement. 4653 */ 4654 fe.r_start = tp->gput_seq; 4655 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 4656 if (rsm) { 4657 /* Ok send-based limit is set */ 4658 if (SEQ_LT(rsm->r_start, tp->gput_seq)) { 4659 /* 4660 * Move back to include the earlier part 4661 * so our ack time lines up right (this may 4662 * make an overlapping measurement but thats 4663 * ok). 4664 */ 4665 tp->gput_seq = rsm->r_start; 4666 } 4667 if (rsm->r_flags & RACK_ACKED) 4668 tp->gput_ts = (uint32_t)rsm->r_ack_arrival; 4669 else 4670 rack->app_limited_needs_set = 1; 4671 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 4672 } else { 4673 /* 4674 * If we don't find the rsm due to some 4675 * send-limit set the current time, which 4676 * basically disables the send-limit. 4677 */ 4678 struct timeval tv; 4679 4680 microuptime(&tv); 4681 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 4682 } 4683 rack_log_pacing_delay_calc(rack, 4684 tp->gput_seq, 4685 tp->gput_ack, 4686 (uint64_t)rsm, 4687 tp->gput_ts, 4688 rack->r_ctl.rc_app_limited_cnt, 4689 9, 4690 __LINE__, NULL, quality); 4691 } 4692 } 4693 4694 /* 4695 * CC wrapper hook functions 4696 */ 4697 static void 4698 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs, 4699 uint16_t type, int32_t recovery) 4700 { 4701 uint32_t prior_cwnd, acked; 4702 struct tcp_log_buffer *lgb = NULL; 4703 uint8_t labc_to_use, quality; 4704 4705 INP_WLOCK_ASSERT(tp->t_inpcb); 4706 tp->ccv->nsegs = nsegs; 4707 acked = tp->ccv->bytes_this_ack = (th_ack - tp->snd_una); 4708 if ((recovery) && (rack->r_ctl.rc_early_recovery_segs)) { 4709 uint32_t max; 4710 4711 max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp); 4712 if (tp->ccv->bytes_this_ack > max) { 4713 tp->ccv->bytes_this_ack = max; 4714 } 4715 } 4716 #ifdef STATS 4717 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 4718 ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd); 4719 #endif 4720 quality = RACK_QUALITY_NONE; 4721 if ((tp->t_flags & TF_GPUTINPROG) && 4722 rack_enough_for_measurement(tp, rack, th_ack, &quality)) { 4723 /* Measure the Goodput */ 4724 rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality); 4725 #ifdef NETFLIX_PEAKRATE 4726 if ((type == CC_ACK) && 4727 (tp->t_maxpeakrate)) { 4728 /* 4729 * We update t_peakrate_thr. This gives us roughly 4730 * one update per round trip time. Note 4731 * it will only be used if pace_always is off i.e 4732 * we don't do this for paced flows. 4733 */ 4734 rack_update_peakrate_thr(tp); 4735 } 4736 #endif 4737 } 4738 /* Which way our we limited, if not cwnd limited no advance in CA */ 4739 if (tp->snd_cwnd <= tp->snd_wnd) 4740 tp->ccv->flags |= CCF_CWND_LIMITED; 4741 else 4742 tp->ccv->flags &= ~CCF_CWND_LIMITED; 4743 if (tp->snd_cwnd > tp->snd_ssthresh) { 4744 tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 4745 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp)); 4746 /* For the setting of a window past use the actual scwnd we are using */ 4747 if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) { 4748 tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use; 4749 tp->ccv->flags |= CCF_ABC_SENTAWND; 4750 } 4751 } else { 4752 tp->ccv->flags &= ~CCF_ABC_SENTAWND; 4753 tp->t_bytes_acked = 0; 4754 } 4755 prior_cwnd = tp->snd_cwnd; 4756 if ((recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec || 4757 (rack_client_low_buf && (rack->client_bufferlvl < rack_client_low_buf))) 4758 labc_to_use = rack->rc_labc; 4759 else 4760 labc_to_use = rack_max_abc_post_recovery; 4761 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) { 4762 union tcp_log_stackspecific log; 4763 struct timeval tv; 4764 4765 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 4766 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 4767 log.u_bbr.flex1 = th_ack; 4768 log.u_bbr.flex2 = tp->ccv->flags; 4769 log.u_bbr.flex3 = tp->ccv->bytes_this_ack; 4770 log.u_bbr.flex4 = tp->ccv->nsegs; 4771 log.u_bbr.flex5 = labc_to_use; 4772 log.u_bbr.flex6 = prior_cwnd; 4773 log.u_bbr.flex7 = V_tcp_do_newsack; 4774 log.u_bbr.flex8 = 1; 4775 lgb = tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 4776 0, &log, false, NULL, NULL, 0, &tv); 4777 } 4778 if (CC_ALGO(tp)->ack_received != NULL) { 4779 /* XXXLAS: Find a way to live without this */ 4780 tp->ccv->curack = th_ack; 4781 tp->ccv->labc = labc_to_use; 4782 tp->ccv->flags |= CCF_USE_LOCAL_ABC; 4783 CC_ALGO(tp)->ack_received(tp->ccv, type); 4784 } 4785 if (lgb) { 4786 lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd; 4787 } 4788 if (rack->r_must_retran) { 4789 if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) { 4790 /* 4791 * We now are beyond the rxt point so lets disable 4792 * the flag. 4793 */ 4794 rack->r_ctl.rc_out_at_rto = 0; 4795 rack->r_must_retran = 0; 4796 } else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) { 4797 /* 4798 * Only decrement the rc_out_at_rto if the cwnd advances 4799 * at least a whole segment. Otherwise next time the peer 4800 * acks, we won't be able to send this generaly happens 4801 * when we are in Congestion Avoidance. 4802 */ 4803 if (acked <= rack->r_ctl.rc_out_at_rto){ 4804 rack->r_ctl.rc_out_at_rto -= acked; 4805 } else { 4806 rack->r_ctl.rc_out_at_rto = 0; 4807 } 4808 } 4809 } 4810 #ifdef STATS 4811 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use); 4812 #endif 4813 if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) { 4814 rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use; 4815 } 4816 #ifdef NETFLIX_PEAKRATE 4817 /* we enforce max peak rate if it is set and we are not pacing */ 4818 if ((rack->rc_always_pace == 0) && 4819 tp->t_peakrate_thr && 4820 (tp->snd_cwnd > tp->t_peakrate_thr)) { 4821 tp->snd_cwnd = tp->t_peakrate_thr; 4822 } 4823 #endif 4824 } 4825 4826 static void 4827 tcp_rack_partialack(struct tcpcb *tp) 4828 { 4829 struct tcp_rack *rack; 4830 4831 rack = (struct tcp_rack *)tp->t_fb_ptr; 4832 INP_WLOCK_ASSERT(tp->t_inpcb); 4833 /* 4834 * If we are doing PRR and have enough 4835 * room to send <or> we are pacing and prr 4836 * is disabled we will want to see if we 4837 * can send data (by setting r_wanted_output to 4838 * true). 4839 */ 4840 if ((rack->r_ctl.rc_prr_sndcnt > 0) || 4841 rack->rack_no_prr) 4842 rack->r_wanted_output = 1; 4843 } 4844 4845 static void 4846 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack) 4847 { 4848 struct tcp_rack *rack; 4849 uint32_t orig_cwnd; 4850 4851 orig_cwnd = tp->snd_cwnd; 4852 INP_WLOCK_ASSERT(tp->t_inpcb); 4853 rack = (struct tcp_rack *)tp->t_fb_ptr; 4854 /* only alert CC if we alerted when we entered */ 4855 if (CC_ALGO(tp)->post_recovery != NULL) { 4856 tp->ccv->curack = th_ack; 4857 CC_ALGO(tp)->post_recovery(tp->ccv); 4858 if (tp->snd_cwnd < tp->snd_ssthresh) { 4859 /* 4860 * Rack has burst control and pacing 4861 * so lets not set this any lower than 4862 * snd_ssthresh per RFC-6582 (option 2). 4863 */ 4864 tp->snd_cwnd = tp->snd_ssthresh; 4865 } 4866 } 4867 if (rack_verbose_logging && (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF)) { 4868 union tcp_log_stackspecific log; 4869 struct timeval tv; 4870 4871 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 4872 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 4873 log.u_bbr.flex1 = th_ack; 4874 log.u_bbr.flex2 = tp->ccv->flags; 4875 log.u_bbr.flex3 = tp->ccv->bytes_this_ack; 4876 log.u_bbr.flex4 = tp->ccv->nsegs; 4877 log.u_bbr.flex5 = V_tcp_abc_l_var; 4878 log.u_bbr.flex6 = orig_cwnd; 4879 log.u_bbr.flex7 = V_tcp_do_newsack; 4880 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 4881 log.u_bbr.flex8 = 2; 4882 tcp_log_event_(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 4883 0, &log, false, NULL, NULL, 0, &tv); 4884 } 4885 if ((rack->rack_no_prr == 0) && 4886 (rack->no_prr_addback == 0) && 4887 (rack->r_ctl.rc_prr_sndcnt > 0)) { 4888 /* 4889 * Suck the next prr cnt back into cwnd, but 4890 * only do that if we are not application limited. 4891 */ 4892 if (ctf_outstanding(tp) <= sbavail(&(tp->t_inpcb->inp_socket->so_snd))) { 4893 /* 4894 * We are allowed to add back to the cwnd the amount we did 4895 * not get out if: 4896 * a) no_prr_addback is off. 4897 * b) we are not app limited 4898 * c) we are doing prr 4899 * <and> 4900 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none). 4901 */ 4902 tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax), 4903 rack->r_ctl.rc_prr_sndcnt); 4904 } 4905 rack->r_ctl.rc_prr_sndcnt = 0; 4906 rack_log_to_prr(rack, 1, 0); 4907 } 4908 rack_log_to_prr(rack, 14, orig_cwnd); 4909 tp->snd_recover = tp->snd_una; 4910 if (rack->r_ctl.dsack_persist) { 4911 rack->r_ctl.dsack_persist--; 4912 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 4913 rack->r_ctl.num_dsack = 0; 4914 } 4915 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 4916 } 4917 EXIT_RECOVERY(tp->t_flags); 4918 } 4919 4920 static void 4921 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack) 4922 { 4923 struct tcp_rack *rack; 4924 uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd; 4925 4926 INP_WLOCK_ASSERT(tp->t_inpcb); 4927 #ifdef STATS 4928 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 4929 #endif 4930 if (IN_RECOVERY(tp->t_flags) == 0) { 4931 in_rec_at_entry = 0; 4932 ssthresh_enter = tp->snd_ssthresh; 4933 cwnd_enter = tp->snd_cwnd; 4934 } else 4935 in_rec_at_entry = 1; 4936 rack = (struct tcp_rack *)tp->t_fb_ptr; 4937 switch (type) { 4938 case CC_NDUPACK: 4939 tp->t_flags &= ~TF_WASFRECOVERY; 4940 tp->t_flags &= ~TF_WASCRECOVERY; 4941 if (!IN_FASTRECOVERY(tp->t_flags)) { 4942 rack->r_ctl.rc_prr_delivered = 0; 4943 rack->r_ctl.rc_prr_out = 0; 4944 if (rack->rack_no_prr == 0) { 4945 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp); 4946 rack_log_to_prr(rack, 2, in_rec_at_entry); 4947 } 4948 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una; 4949 tp->snd_recover = tp->snd_max; 4950 if (tp->t_flags2 & TF2_ECN_PERMIT) 4951 tp->t_flags2 |= TF2_ECN_SND_CWR; 4952 } 4953 break; 4954 case CC_ECN: 4955 if (!IN_CONGRECOVERY(tp->t_flags) || 4956 /* 4957 * Allow ECN reaction on ACK to CWR, if 4958 * that data segment was also CE marked. 4959 */ 4960 SEQ_GEQ(ack, tp->snd_recover)) { 4961 EXIT_CONGRECOVERY(tp->t_flags); 4962 KMOD_TCPSTAT_INC(tcps_ecn_rcwnd); 4963 tp->snd_recover = tp->snd_max + 1; 4964 if (tp->t_flags2 & TF2_ECN_PERMIT) 4965 tp->t_flags2 |= TF2_ECN_SND_CWR; 4966 } 4967 break; 4968 case CC_RTO: 4969 tp->t_dupacks = 0; 4970 tp->t_bytes_acked = 0; 4971 EXIT_RECOVERY(tp->t_flags); 4972 tp->snd_ssthresh = max(2, min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 / 4973 ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp); 4974 orig_cwnd = tp->snd_cwnd; 4975 tp->snd_cwnd = ctf_fixed_maxseg(tp); 4976 rack_log_to_prr(rack, 16, orig_cwnd); 4977 if (tp->t_flags2 & TF2_ECN_PERMIT) 4978 tp->t_flags2 |= TF2_ECN_SND_CWR; 4979 break; 4980 case CC_RTO_ERR: 4981 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 4982 /* RTO was unnecessary, so reset everything. */ 4983 tp->snd_cwnd = tp->snd_cwnd_prev; 4984 tp->snd_ssthresh = tp->snd_ssthresh_prev; 4985 tp->snd_recover = tp->snd_recover_prev; 4986 if (tp->t_flags & TF_WASFRECOVERY) { 4987 ENTER_FASTRECOVERY(tp->t_flags); 4988 tp->t_flags &= ~TF_WASFRECOVERY; 4989 } 4990 if (tp->t_flags & TF_WASCRECOVERY) { 4991 ENTER_CONGRECOVERY(tp->t_flags); 4992 tp->t_flags &= ~TF_WASCRECOVERY; 4993 } 4994 tp->snd_nxt = tp->snd_max; 4995 tp->t_badrxtwin = 0; 4996 break; 4997 } 4998 if ((CC_ALGO(tp)->cong_signal != NULL) && 4999 (type != CC_RTO)){ 5000 tp->ccv->curack = ack; 5001 CC_ALGO(tp)->cong_signal(tp->ccv, type); 5002 } 5003 if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) { 5004 rack_log_to_prr(rack, 15, cwnd_enter); 5005 rack->r_ctl.dsack_byte_cnt = 0; 5006 rack->r_ctl.retran_during_recovery = 0; 5007 rack->r_ctl.rc_cwnd_at_erec = cwnd_enter; 5008 rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter; 5009 rack->r_ent_rec_ns = 1; 5010 } 5011 } 5012 5013 static inline void 5014 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp) 5015 { 5016 uint32_t i_cwnd; 5017 5018 INP_WLOCK_ASSERT(tp->t_inpcb); 5019 5020 #ifdef NETFLIX_STATS 5021 KMOD_TCPSTAT_INC(tcps_idle_restarts); 5022 if (tp->t_state == TCPS_ESTABLISHED) 5023 KMOD_TCPSTAT_INC(tcps_idle_estrestarts); 5024 #endif 5025 if (CC_ALGO(tp)->after_idle != NULL) 5026 CC_ALGO(tp)->after_idle(tp->ccv); 5027 5028 if (tp->snd_cwnd == 1) 5029 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 5030 else 5031 i_cwnd = rc_init_window(rack); 5032 5033 /* 5034 * Being idle is no differnt than the initial window. If the cc 5035 * clamps it down below the initial window raise it to the initial 5036 * window. 5037 */ 5038 if (tp->snd_cwnd < i_cwnd) { 5039 tp->snd_cwnd = i_cwnd; 5040 } 5041 } 5042 5043 /* 5044 * Indicate whether this ack should be delayed. We can delay the ack if 5045 * following conditions are met: 5046 * - There is no delayed ack timer in progress. 5047 * - Our last ack wasn't a 0-sized window. We never want to delay 5048 * the ack that opens up a 0-sized window. 5049 * - LRO wasn't used for this segment. We make sure by checking that the 5050 * segment size is not larger than the MSS. 5051 * - Delayed acks are enabled or this is a half-synchronized T/TCP 5052 * connection. 5053 */ 5054 #define DELAY_ACK(tp, tlen) \ 5055 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 5056 ((tp->t_flags & TF_DELACK) == 0) && \ 5057 (tlen <= tp->t_maxseg) && \ 5058 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 5059 5060 static struct rack_sendmap * 5061 rack_find_lowest_rsm(struct tcp_rack *rack) 5062 { 5063 struct rack_sendmap *rsm; 5064 5065 /* 5066 * Walk the time-order transmitted list looking for an rsm that is 5067 * not acked. This will be the one that was sent the longest time 5068 * ago that is still outstanding. 5069 */ 5070 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 5071 if (rsm->r_flags & RACK_ACKED) { 5072 continue; 5073 } 5074 goto finish; 5075 } 5076 finish: 5077 return (rsm); 5078 } 5079 5080 static struct rack_sendmap * 5081 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm) 5082 { 5083 struct rack_sendmap *prsm; 5084 5085 /* 5086 * Walk the sequence order list backward until we hit and arrive at 5087 * the highest seq not acked. In theory when this is called it 5088 * should be the last segment (which it was not). 5089 */ 5090 counter_u64_add(rack_find_high, 1); 5091 prsm = rsm; 5092 RB_FOREACH_REVERSE_FROM(prsm, rack_rb_tree_head, rsm) { 5093 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) { 5094 continue; 5095 } 5096 return (prsm); 5097 } 5098 return (NULL); 5099 } 5100 5101 static uint32_t 5102 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts) 5103 { 5104 int32_t lro; 5105 uint32_t thresh; 5106 5107 /* 5108 * lro is the flag we use to determine if we have seen reordering. 5109 * If it gets set we have seen reordering. The reorder logic either 5110 * works in one of two ways: 5111 * 5112 * If reorder-fade is configured, then we track the last time we saw 5113 * re-ordering occur. If we reach the point where enough time as 5114 * passed we no longer consider reordering has occuring. 5115 * 5116 * Or if reorder-face is 0, then once we see reordering we consider 5117 * the connection to alway be subject to reordering and just set lro 5118 * to 1. 5119 * 5120 * In the end if lro is non-zero we add the extra time for 5121 * reordering in. 5122 */ 5123 if (srtt == 0) 5124 srtt = 1; 5125 if (rack->r_ctl.rc_reorder_ts) { 5126 if (rack->r_ctl.rc_reorder_fade) { 5127 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) { 5128 lro = cts - rack->r_ctl.rc_reorder_ts; 5129 if (lro == 0) { 5130 /* 5131 * No time as passed since the last 5132 * reorder, mark it as reordering. 5133 */ 5134 lro = 1; 5135 } 5136 } else { 5137 /* Negative time? */ 5138 lro = 0; 5139 } 5140 if (lro > rack->r_ctl.rc_reorder_fade) { 5141 /* Turn off reordering seen too */ 5142 rack->r_ctl.rc_reorder_ts = 0; 5143 lro = 0; 5144 } 5145 } else { 5146 /* Reodering does not fade */ 5147 lro = 1; 5148 } 5149 } else { 5150 lro = 0; 5151 } 5152 if (rack->rc_rack_tmr_std_based == 0) { 5153 thresh = srtt + rack->r_ctl.rc_pkt_delay; 5154 } else { 5155 /* Standards based pkt-delay is 1/4 srtt */ 5156 thresh = srtt + (srtt >> 2); 5157 } 5158 if (lro && (rack->rc_rack_tmr_std_based == 0)) { 5159 /* It must be set, if not you get 1/4 rtt */ 5160 if (rack->r_ctl.rc_reorder_shift) 5161 thresh += (srtt >> rack->r_ctl.rc_reorder_shift); 5162 else 5163 thresh += (srtt >> 2); 5164 } 5165 if (rack->rc_rack_use_dsack && 5166 lro && 5167 (rack->r_ctl.num_dsack > 0)) { 5168 /* 5169 * We only increase the reordering window if we 5170 * have seen reordering <and> we have a DSACK count. 5171 */ 5172 thresh += rack->r_ctl.num_dsack * (srtt >> 2); 5173 rack_log_dsack_event(rack, 4, __LINE__, srtt, thresh); 5174 } 5175 /* SRTT * 2 is the ceiling */ 5176 if (thresh > (srtt * 2)) { 5177 thresh = srtt * 2; 5178 } 5179 /* And we don't want it above the RTO max either */ 5180 if (thresh > rack_rto_max) { 5181 thresh = rack_rto_max; 5182 } 5183 rack_log_dsack_event(rack, 6, __LINE__, srtt, thresh); 5184 return (thresh); 5185 } 5186 5187 static uint32_t 5188 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack, 5189 struct rack_sendmap *rsm, uint32_t srtt) 5190 { 5191 struct rack_sendmap *prsm; 5192 uint32_t thresh, len; 5193 int segsiz; 5194 5195 if (srtt == 0) 5196 srtt = 1; 5197 if (rack->r_ctl.rc_tlp_threshold) 5198 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold); 5199 else 5200 thresh = (srtt * 2); 5201 5202 /* Get the previous sent packet, if any */ 5203 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 5204 counter_u64_add(rack_enter_tlp_calc, 1); 5205 len = rsm->r_end - rsm->r_start; 5206 if (rack->rack_tlp_threshold_use == TLP_USE_ID) { 5207 /* Exactly like the ID */ 5208 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) { 5209 uint32_t alt_thresh; 5210 /* 5211 * Compensate for delayed-ack with the d-ack time. 5212 */ 5213 counter_u64_add(rack_used_tlpmethod, 1); 5214 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 5215 if (alt_thresh > thresh) 5216 thresh = alt_thresh; 5217 } 5218 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) { 5219 /* 2.1 behavior */ 5220 prsm = TAILQ_PREV(rsm, rack_head, r_tnext); 5221 if (prsm && (len <= segsiz)) { 5222 /* 5223 * Two packets outstanding, thresh should be (2*srtt) + 5224 * possible inter-packet delay (if any). 5225 */ 5226 uint32_t inter_gap = 0; 5227 int idx, nidx; 5228 5229 counter_u64_add(rack_used_tlpmethod, 1); 5230 idx = rsm->r_rtr_cnt - 1; 5231 nidx = prsm->r_rtr_cnt - 1; 5232 if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) { 5233 /* Yes it was sent later (or at the same time) */ 5234 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 5235 } 5236 thresh += inter_gap; 5237 } else if (len <= segsiz) { 5238 /* 5239 * Possibly compensate for delayed-ack. 5240 */ 5241 uint32_t alt_thresh; 5242 5243 counter_u64_add(rack_used_tlpmethod2, 1); 5244 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 5245 if (alt_thresh > thresh) 5246 thresh = alt_thresh; 5247 } 5248 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) { 5249 /* 2.2 behavior */ 5250 if (len <= segsiz) { 5251 uint32_t alt_thresh; 5252 /* 5253 * Compensate for delayed-ack with the d-ack time. 5254 */ 5255 counter_u64_add(rack_used_tlpmethod, 1); 5256 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 5257 if (alt_thresh > thresh) 5258 thresh = alt_thresh; 5259 } 5260 } 5261 /* Not above an RTO */ 5262 if (thresh > tp->t_rxtcur) { 5263 thresh = tp->t_rxtcur; 5264 } 5265 /* Not above a RTO max */ 5266 if (thresh > rack_rto_max) { 5267 thresh = rack_rto_max; 5268 } 5269 /* Apply user supplied min TLP */ 5270 if (thresh < rack_tlp_min) { 5271 thresh = rack_tlp_min; 5272 } 5273 return (thresh); 5274 } 5275 5276 static uint32_t 5277 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack) 5278 { 5279 /* 5280 * We want the rack_rtt which is the 5281 * last rtt we measured. However if that 5282 * does not exist we fallback to the srtt (which 5283 * we probably will never do) and then as a last 5284 * resort we use RACK_INITIAL_RTO if no srtt is 5285 * yet set. 5286 */ 5287 if (rack->rc_rack_rtt) 5288 return (rack->rc_rack_rtt); 5289 else if (tp->t_srtt == 0) 5290 return (RACK_INITIAL_RTO); 5291 return (tp->t_srtt); 5292 } 5293 5294 static struct rack_sendmap * 5295 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused) 5296 { 5297 /* 5298 * Check to see that we don't need to fall into recovery. We will 5299 * need to do so if our oldest transmit is past the time we should 5300 * have had an ack. 5301 */ 5302 struct tcp_rack *rack; 5303 struct rack_sendmap *rsm; 5304 int32_t idx; 5305 uint32_t srtt, thresh; 5306 5307 rack = (struct tcp_rack *)tp->t_fb_ptr; 5308 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) { 5309 return (NULL); 5310 } 5311 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 5312 if (rsm == NULL) 5313 return (NULL); 5314 5315 if (rsm->r_flags & RACK_ACKED) { 5316 rsm = rack_find_lowest_rsm(rack); 5317 if (rsm == NULL) 5318 return (NULL); 5319 } 5320 idx = rsm->r_rtr_cnt - 1; 5321 srtt = rack_grab_rtt(tp, rack); 5322 thresh = rack_calc_thresh_rack(rack, srtt, tsused); 5323 if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) { 5324 return (NULL); 5325 } 5326 if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) { 5327 return (NULL); 5328 } 5329 /* Ok if we reach here we are over-due and this guy can be sent */ 5330 if (IN_RECOVERY(tp->t_flags) == 0) { 5331 /* 5332 * For the one that enters us into recovery record undo 5333 * info. 5334 */ 5335 rack->r_ctl.rc_rsm_start = rsm->r_start; 5336 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 5337 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 5338 } 5339 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una); 5340 return (rsm); 5341 } 5342 5343 static uint32_t 5344 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack) 5345 { 5346 int32_t t; 5347 int32_t tt; 5348 uint32_t ret_val; 5349 5350 t = (tp->t_srtt + (tp->t_rttvar << 2)); 5351 RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 5352 rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop); 5353 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 5354 tp->t_rxtshift++; 5355 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 5356 ret_val = (uint32_t)tt; 5357 return (ret_val); 5358 } 5359 5360 static uint32_t 5361 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack) 5362 { 5363 /* 5364 * Start the FR timer, we do this based on getting the first one in 5365 * the rc_tmap. Note that if its NULL we must stop the timer. in all 5366 * events we need to stop the running timer (if its running) before 5367 * starting the new one. 5368 */ 5369 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 5370 uint32_t srtt_cur; 5371 int32_t idx; 5372 int32_t is_tlp_timer = 0; 5373 struct rack_sendmap *rsm; 5374 5375 if (rack->t_timers_stopped) { 5376 /* All timers have been stopped none are to run */ 5377 return (0); 5378 } 5379 if (rack->rc_in_persist) { 5380 /* We can't start any timer in persists */ 5381 return (rack_get_persists_timer_val(tp, rack)); 5382 } 5383 rack->rc_on_min_to = 0; 5384 if ((tp->t_state < TCPS_ESTABLISHED) || 5385 ((tp->t_flags & TF_SACK_PERMIT) == 0)) { 5386 goto activate_rxt; 5387 } 5388 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 5389 if ((rsm == NULL) || sup_rack) { 5390 /* Nothing on the send map or no rack */ 5391 activate_rxt: 5392 time_since_sent = 0; 5393 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 5394 if (rsm) { 5395 /* 5396 * Should we discount the RTX timer any? 5397 * 5398 * We want to discount it the smallest amount. 5399 * If a timer (Rack/TLP or RXT) has gone off more 5400 * recently thats the discount we want to use (now - timer time). 5401 * If the retransmit of the oldest packet was more recent then 5402 * we want to use that (now - oldest-packet-last_transmit_time). 5403 * 5404 */ 5405 idx = rsm->r_rtr_cnt - 1; 5406 if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx]))) 5407 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time; 5408 else 5409 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx]; 5410 if (TSTMP_GT(cts, tstmp_touse)) 5411 time_since_sent = cts - tstmp_touse; 5412 } 5413 if (SEQ_LT(tp->snd_una, tp->snd_max) || sbavail(&(tp->t_inpcb->inp_socket->so_snd))) { 5414 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 5415 to = tp->t_rxtcur; 5416 if (to > time_since_sent) 5417 to -= time_since_sent; 5418 else 5419 to = rack->r_ctl.rc_min_to; 5420 if (to == 0) 5421 to = 1; 5422 /* Special case for KEEPINIT */ 5423 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) && 5424 (TP_KEEPINIT(tp) != 0) && 5425 rsm) { 5426 /* 5427 * We have to put a ceiling on the rxt timer 5428 * of the keep-init timeout. 5429 */ 5430 uint32_t max_time, red; 5431 5432 max_time = TICKS_2_USEC(TP_KEEPINIT(tp)); 5433 if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) { 5434 red = (cts - (uint32_t)rsm->r_tim_lastsent[0]); 5435 if (red < max_time) 5436 max_time -= red; 5437 else 5438 max_time = 1; 5439 } 5440 /* Reduce timeout to the keep value if needed */ 5441 if (max_time < to) 5442 to = max_time; 5443 } 5444 return (to); 5445 } 5446 return (0); 5447 } 5448 if (rsm->r_flags & RACK_ACKED) { 5449 rsm = rack_find_lowest_rsm(rack); 5450 if (rsm == NULL) { 5451 /* No lowest? */ 5452 goto activate_rxt; 5453 } 5454 } 5455 if (rack->sack_attack_disable) { 5456 /* 5457 * We don't want to do 5458 * any TLP's if you are an attacker. 5459 * Though if you are doing what 5460 * is expected you may still have 5461 * SACK-PASSED marks. 5462 */ 5463 goto activate_rxt; 5464 } 5465 /* Convert from ms to usecs */ 5466 if ((rsm->r_flags & RACK_SACK_PASSED) || (rsm->r_dupack >= DUP_ACK_THRESHOLD)) { 5467 if ((tp->t_flags & TF_SENTFIN) && 5468 ((tp->snd_max - tp->snd_una) == 1) && 5469 (rsm->r_flags & RACK_HAS_FIN)) { 5470 /* 5471 * We don't start a rack timer if all we have is a 5472 * FIN outstanding. 5473 */ 5474 goto activate_rxt; 5475 } 5476 if ((rack->use_rack_rr == 0) && 5477 (IN_FASTRECOVERY(tp->t_flags)) && 5478 (rack->rack_no_prr == 0) && 5479 (rack->r_ctl.rc_prr_sndcnt < ctf_fixed_maxseg(tp))) { 5480 /* 5481 * We are not cheating, in recovery and 5482 * not enough ack's to yet get our next 5483 * retransmission out. 5484 * 5485 * Note that classified attackers do not 5486 * get to use the rack-cheat. 5487 */ 5488 goto activate_tlp; 5489 } 5490 srtt = rack_grab_rtt(tp, rack); 5491 thresh = rack_calc_thresh_rack(rack, srtt, cts); 5492 idx = rsm->r_rtr_cnt - 1; 5493 exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh; 5494 if (SEQ_GEQ(exp, cts)) { 5495 to = exp - cts; 5496 if (to < rack->r_ctl.rc_min_to) { 5497 to = rack->r_ctl.rc_min_to; 5498 if (rack->r_rr_config == 3) 5499 rack->rc_on_min_to = 1; 5500 } 5501 } else { 5502 to = rack->r_ctl.rc_min_to; 5503 if (rack->r_rr_config == 3) 5504 rack->rc_on_min_to = 1; 5505 } 5506 } else { 5507 /* Ok we need to do a TLP not RACK */ 5508 activate_tlp: 5509 if ((rack->rc_tlp_in_progress != 0) && 5510 (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) { 5511 /* 5512 * The previous send was a TLP and we have sent 5513 * N TLP's without sending new data. 5514 */ 5515 goto activate_rxt; 5516 } 5517 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 5518 if (rsm == NULL) { 5519 /* We found no rsm to TLP with. */ 5520 goto activate_rxt; 5521 } 5522 if (rsm->r_flags & RACK_HAS_FIN) { 5523 /* If its a FIN we dont do TLP */ 5524 rsm = NULL; 5525 goto activate_rxt; 5526 } 5527 idx = rsm->r_rtr_cnt - 1; 5528 time_since_sent = 0; 5529 if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time)) 5530 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx]; 5531 else 5532 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time; 5533 if (TSTMP_GT(cts, tstmp_touse)) 5534 time_since_sent = cts - tstmp_touse; 5535 is_tlp_timer = 1; 5536 if (tp->t_srtt) { 5537 if ((rack->rc_srtt_measure_made == 0) && 5538 (tp->t_srtt == 1)) { 5539 /* 5540 * If another stack as run and set srtt to 1, 5541 * then the srtt was 0, so lets use the initial. 5542 */ 5543 srtt = RACK_INITIAL_RTO; 5544 } else { 5545 srtt_cur = tp->t_srtt; 5546 srtt = srtt_cur; 5547 } 5548 } else 5549 srtt = RACK_INITIAL_RTO; 5550 /* 5551 * If the SRTT is not keeping up and the 5552 * rack RTT has spiked we want to use 5553 * the last RTT not the smoothed one. 5554 */ 5555 if (rack_tlp_use_greater && 5556 tp->t_srtt && 5557 (srtt < rack_grab_rtt(tp, rack))) { 5558 srtt = rack_grab_rtt(tp, rack); 5559 } 5560 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt); 5561 if (thresh > time_since_sent) { 5562 to = thresh - time_since_sent; 5563 } else { 5564 to = rack->r_ctl.rc_min_to; 5565 rack_log_alt_to_to_cancel(rack, 5566 thresh, /* flex1 */ 5567 time_since_sent, /* flex2 */ 5568 tstmp_touse, /* flex3 */ 5569 rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */ 5570 (uint32_t)rsm->r_tim_lastsent[idx], 5571 srtt, 5572 idx, 99); 5573 } 5574 if (to < rack_tlp_min) { 5575 to = rack_tlp_min; 5576 } 5577 if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) { 5578 /* 5579 * If the TLP time works out to larger than the max 5580 * RTO lets not do TLP.. just RTO. 5581 */ 5582 goto activate_rxt; 5583 } 5584 } 5585 if (is_tlp_timer == 0) { 5586 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 5587 } else { 5588 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 5589 } 5590 if (to == 0) 5591 to = 1; 5592 return (to); 5593 } 5594 5595 static void 5596 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 5597 { 5598 if (rack->rc_in_persist == 0) { 5599 if (tp->t_flags & TF_GPUTINPROG) { 5600 /* 5601 * Stop the goodput now, the calling of the 5602 * measurement function clears the flag. 5603 */ 5604 rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__, 5605 RACK_QUALITY_PERSIST); 5606 } 5607 #ifdef NETFLIX_SHARED_CWND 5608 if (rack->r_ctl.rc_scw) { 5609 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 5610 rack->rack_scwnd_is_idle = 1; 5611 } 5612 #endif 5613 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 5614 if (rack->r_ctl.rc_went_idle_time == 0) 5615 rack->r_ctl.rc_went_idle_time = 1; 5616 rack_timer_cancel(tp, rack, cts, __LINE__); 5617 tp->t_rxtshift = 0; 5618 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 5619 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 5620 rack->rc_in_persist = 1; 5621 } 5622 } 5623 5624 static void 5625 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 5626 { 5627 if (rack->rc_inp->inp_in_hpts) { 5628 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 5629 rack->r_ctl.rc_hpts_flags = 0; 5630 } 5631 #ifdef NETFLIX_SHARED_CWND 5632 if (rack->r_ctl.rc_scw) { 5633 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 5634 rack->rack_scwnd_is_idle = 0; 5635 } 5636 #endif 5637 if (rack->rc_gp_dyn_mul && 5638 (rack->use_fixed_rate == 0) && 5639 (rack->rc_always_pace)) { 5640 /* 5641 * Do we count this as if a probe-rtt just 5642 * finished? 5643 */ 5644 uint32_t time_idle, idle_min; 5645 5646 time_idle = tcp_get_usecs(NULL) - rack->r_ctl.rc_went_idle_time; 5647 idle_min = rack_min_probertt_hold; 5648 if (rack_probertt_gpsrtt_cnt_div) { 5649 uint64_t extra; 5650 extra = (uint64_t)rack->r_ctl.rc_gp_srtt * 5651 (uint64_t)rack_probertt_gpsrtt_cnt_mul; 5652 extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div; 5653 idle_min += (uint32_t)extra; 5654 } 5655 if (time_idle >= idle_min) { 5656 /* Yes, we count it as a probe-rtt. */ 5657 uint32_t us_cts; 5658 5659 us_cts = tcp_get_usecs(NULL); 5660 if (rack->in_probe_rtt == 0) { 5661 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 5662 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts; 5663 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts; 5664 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts; 5665 } else { 5666 rack_exit_probertt(rack, us_cts); 5667 } 5668 } 5669 } 5670 rack->rc_in_persist = 0; 5671 rack->r_ctl.rc_went_idle_time = 0; 5672 tp->t_rxtshift = 0; 5673 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 5674 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 5675 rack->r_ctl.rc_agg_delayed = 0; 5676 rack->r_early = 0; 5677 rack->r_late = 0; 5678 rack->r_ctl.rc_agg_early = 0; 5679 } 5680 5681 static void 5682 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts, 5683 struct hpts_diag *diag, struct timeval *tv) 5684 { 5685 if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 5686 union tcp_log_stackspecific log; 5687 5688 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5689 log.u_bbr.flex1 = diag->p_nxt_slot; 5690 log.u_bbr.flex2 = diag->p_cur_slot; 5691 log.u_bbr.flex3 = diag->slot_req; 5692 log.u_bbr.flex4 = diag->inp_hptsslot; 5693 log.u_bbr.flex5 = diag->slot_remaining; 5694 log.u_bbr.flex6 = diag->need_new_to; 5695 log.u_bbr.flex7 = diag->p_hpts_active; 5696 log.u_bbr.flex8 = diag->p_on_min_sleep; 5697 /* Hijack other fields as needed */ 5698 log.u_bbr.epoch = diag->have_slept; 5699 log.u_bbr.lt_epoch = diag->yet_to_sleep; 5700 log.u_bbr.pkts_out = diag->co_ret; 5701 log.u_bbr.applimited = diag->hpts_sleep_time; 5702 log.u_bbr.delivered = diag->p_prev_slot; 5703 log.u_bbr.inflight = diag->p_runningslot; 5704 log.u_bbr.bw_inuse = diag->wheel_slot; 5705 log.u_bbr.rttProp = diag->wheel_cts; 5706 log.u_bbr.timeStamp = cts; 5707 log.u_bbr.delRate = diag->maxslots; 5708 log.u_bbr.cur_del_rate = diag->p_curtick; 5709 log.u_bbr.cur_del_rate <<= 32; 5710 log.u_bbr.cur_del_rate |= diag->p_lasttick; 5711 TCP_LOG_EVENTP(rack->rc_tp, NULL, 5712 &rack->rc_inp->inp_socket->so_rcv, 5713 &rack->rc_inp->inp_socket->so_snd, 5714 BBR_LOG_HPTSDIAG, 0, 5715 0, &log, false, tv); 5716 } 5717 5718 } 5719 5720 static void 5721 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type) 5722 { 5723 if (rack_verbose_logging && rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 5724 union tcp_log_stackspecific log; 5725 struct timeval tv; 5726 5727 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5728 log.u_bbr.flex1 = sb->sb_flags; 5729 log.u_bbr.flex2 = len; 5730 log.u_bbr.flex3 = sb->sb_state; 5731 log.u_bbr.flex8 = type; 5732 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5733 TCP_LOG_EVENTP(rack->rc_tp, NULL, 5734 &rack->rc_inp->inp_socket->so_rcv, 5735 &rack->rc_inp->inp_socket->so_snd, 5736 TCP_LOG_SB_WAKE, 0, 5737 len, &log, false, &tv); 5738 } 5739 } 5740 5741 static void 5742 rack_start_hpts_timer(struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts, 5743 int32_t slot, uint32_t tot_len_this_send, int sup_rack) 5744 { 5745 struct hpts_diag diag; 5746 struct inpcb *inp; 5747 struct timeval tv; 5748 uint32_t delayed_ack = 0; 5749 uint32_t hpts_timeout; 5750 uint32_t entry_slot = slot; 5751 uint8_t stopped; 5752 uint32_t left = 0; 5753 uint32_t us_cts; 5754 5755 inp = tp->t_inpcb; 5756 if ((tp->t_state == TCPS_CLOSED) || 5757 (tp->t_state == TCPS_LISTEN)) { 5758 return; 5759 } 5760 if (inp->inp_in_hpts) { 5761 /* Already on the pacer */ 5762 return; 5763 } 5764 stopped = rack->rc_tmr_stopped; 5765 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) { 5766 left = rack->r_ctl.rc_timer_exp - cts; 5767 } 5768 rack->r_ctl.rc_timer_exp = 0; 5769 rack->r_ctl.rc_hpts_flags = 0; 5770 us_cts = tcp_get_usecs(&tv); 5771 /* Now early/late accounting */ 5772 rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0); 5773 if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) { 5774 /* 5775 * We have a early carry over set, 5776 * we can always add more time so we 5777 * can always make this compensation. 5778 * 5779 * Note if ack's are allowed to wake us do not 5780 * penalize the next timer for being awoke 5781 * by an ack aka the rc_agg_early (non-paced mode). 5782 */ 5783 slot += rack->r_ctl.rc_agg_early; 5784 rack->r_early = 0; 5785 rack->r_ctl.rc_agg_early = 0; 5786 } 5787 if (rack->r_late) { 5788 /* 5789 * This is harder, we can 5790 * compensate some but it 5791 * really depends on what 5792 * the current pacing time is. 5793 */ 5794 if (rack->r_ctl.rc_agg_delayed >= slot) { 5795 /* 5796 * We can't compensate for it all. 5797 * And we have to have some time 5798 * on the clock. We always have a min 5799 * 10 slots (10 x 10 i.e. 100 usecs). 5800 */ 5801 if (slot <= HPTS_TICKS_PER_SLOT) { 5802 /* We gain delay */ 5803 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot); 5804 slot = HPTS_TICKS_PER_SLOT; 5805 } else { 5806 /* We take off some */ 5807 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT); 5808 slot = HPTS_TICKS_PER_SLOT; 5809 } 5810 } else { 5811 slot -= rack->r_ctl.rc_agg_delayed; 5812 rack->r_ctl.rc_agg_delayed = 0; 5813 /* Make sure we have 100 useconds at minimum */ 5814 if (slot < HPTS_TICKS_PER_SLOT) { 5815 rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot; 5816 slot = HPTS_TICKS_PER_SLOT; 5817 } 5818 if (rack->r_ctl.rc_agg_delayed == 0) 5819 rack->r_late = 0; 5820 } 5821 } 5822 if (slot) { 5823 /* We are pacing too */ 5824 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 5825 } 5826 hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack); 5827 #ifdef NETFLIX_EXP_DETECTION 5828 if (rack->sack_attack_disable && 5829 (slot < tcp_sad_pacing_interval)) { 5830 /* 5831 * We have a potential attacker on 5832 * the line. We have possibly some 5833 * (or now) pacing time set. We want to 5834 * slow down the processing of sacks by some 5835 * amount (if it is an attacker). Set the default 5836 * slot for attackers in place (unless the orginal 5837 * interval is longer). Its stored in 5838 * micro-seconds, so lets convert to msecs. 5839 */ 5840 slot = tcp_sad_pacing_interval; 5841 } 5842 #endif 5843 if (tp->t_flags & TF_DELACK) { 5844 delayed_ack = TICKS_2_USEC(tcp_delacktime); 5845 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK; 5846 } 5847 if (delayed_ack && ((hpts_timeout == 0) || 5848 (delayed_ack < hpts_timeout))) 5849 hpts_timeout = delayed_ack; 5850 else 5851 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 5852 /* 5853 * If no timers are going to run and we will fall off the hptsi 5854 * wheel, we resort to a keep-alive timer if its configured. 5855 */ 5856 if ((hpts_timeout == 0) && 5857 (slot == 0)) { 5858 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 5859 (tp->t_state <= TCPS_CLOSING)) { 5860 /* 5861 * Ok we have no timer (persists, rack, tlp, rxt or 5862 * del-ack), we don't have segments being paced. So 5863 * all that is left is the keepalive timer. 5864 */ 5865 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 5866 /* Get the established keep-alive time */ 5867 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 5868 } else { 5869 /* 5870 * Get the initial setup keep-alive time, 5871 * note that this is probably not going to 5872 * happen, since rack will be running a rxt timer 5873 * if a SYN of some sort is outstanding. It is 5874 * actually handled in rack_timeout_rxt(). 5875 */ 5876 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 5877 } 5878 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 5879 if (rack->in_probe_rtt) { 5880 /* 5881 * We want to instead not wake up a long time from 5882 * now but to wake up about the time we would 5883 * exit probe-rtt and initiate a keep-alive ack. 5884 * This will get us out of probe-rtt and update 5885 * our min-rtt. 5886 */ 5887 hpts_timeout = rack_min_probertt_hold; 5888 } 5889 } 5890 } 5891 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 5892 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 5893 /* 5894 * RACK, TLP, persists and RXT timers all are restartable 5895 * based on actions input .. i.e we received a packet (ack 5896 * or sack) and that changes things (rw, or snd_una etc). 5897 * Thus we can restart them with a new value. For 5898 * keep-alive, delayed_ack we keep track of what was left 5899 * and restart the timer with a smaller value. 5900 */ 5901 if (left < hpts_timeout) 5902 hpts_timeout = left; 5903 } 5904 if (hpts_timeout) { 5905 /* 5906 * Hack alert for now we can't time-out over 2,147,483 5907 * seconds (a bit more than 596 hours), which is probably ok 5908 * :). 5909 */ 5910 if (hpts_timeout > 0x7ffffffe) 5911 hpts_timeout = 0x7ffffffe; 5912 rack->r_ctl.rc_timer_exp = cts + hpts_timeout; 5913 } 5914 rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0); 5915 if ((rack->gp_ready == 0) && 5916 (rack->use_fixed_rate == 0) && 5917 (hpts_timeout < slot) && 5918 (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 5919 /* 5920 * We have no good estimate yet for the 5921 * old clunky burst mitigation or the 5922 * real pacing. And the tlp or rxt is smaller 5923 * than the pacing calculation. Lets not 5924 * pace that long since we know the calculation 5925 * so far is not accurate. 5926 */ 5927 slot = hpts_timeout; 5928 } 5929 rack->r_ctl.last_pacing_time = slot; 5930 /** 5931 * Turn off all the flags for queuing by default. The 5932 * flags have important meanings to what happens when 5933 * LRO interacts with the transport. Most likely (by default now) 5934 * mbuf_queueing and ack compression are on. So the transport 5935 * has a couple of flags that control what happens (if those 5936 * are not on then these flags won't have any effect since it 5937 * won't go through the queuing LRO path). 5938 * 5939 * INP_MBUF_QUEUE_READY - This flags says that I am busy 5940 * pacing output, so don't disturb. But 5941 * it also means LRO can wake me if there 5942 * is a SACK arrival. 5943 * 5944 * INP_DONT_SACK_QUEUE - This flag is used in conjunction 5945 * with the above flag (QUEUE_READY) and 5946 * when present it says don't even wake me 5947 * if a SACK arrives. 5948 * 5949 * The idea behind these flags is that if we are pacing we 5950 * set the MBUF_QUEUE_READY and only get woken up if 5951 * a SACK arrives (which could change things) or if 5952 * our pacing timer expires. If, however, we have a rack 5953 * timer running, then we don't even want a sack to wake 5954 * us since the rack timer has to expire before we can send. 5955 * 5956 * Other cases should usually have none of the flags set 5957 * so LRO can call into us. 5958 */ 5959 inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY); 5960 if (slot) { 5961 rack->r_ctl.rc_last_output_to = us_cts + slot; 5962 /* 5963 * A pacing timer (slot) is being set, in 5964 * such a case we cannot send (we are blocked by 5965 * the timer). So lets tell LRO that it should not 5966 * wake us unless there is a SACK. Note this only 5967 * will be effective if mbuf queueing is on or 5968 * compressed acks are being processed. 5969 */ 5970 inp->inp_flags2 |= INP_MBUF_QUEUE_READY; 5971 /* 5972 * But wait if we have a Rack timer running 5973 * even a SACK should not disturb us (with 5974 * the exception of r_rr_config 3). 5975 */ 5976 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 5977 (rack->r_rr_config != 3)) 5978 inp->inp_flags2 |= INP_DONT_SACK_QUEUE; 5979 if (rack->rc_ack_can_sendout_data) { 5980 /* 5981 * Ahh but wait, this is that special case 5982 * where the pacing timer can be disturbed 5983 * backout the changes (used for non-paced 5984 * burst limiting). 5985 */ 5986 inp->inp_flags2 &= ~(INP_DONT_SACK_QUEUE|INP_MBUF_QUEUE_READY); 5987 } 5988 if ((rack->use_rack_rr) && 5989 (rack->r_rr_config < 2) && 5990 ((hpts_timeout) && (hpts_timeout < slot))) { 5991 /* 5992 * Arrange for the hpts to kick back in after the 5993 * t-o if the t-o does not cause a send. 5994 */ 5995 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout), 5996 __LINE__, &diag); 5997 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 5998 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 5999 } else { 6000 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(slot), 6001 __LINE__, &diag); 6002 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 6003 rack_log_to_start(rack, cts, hpts_timeout, slot, 1); 6004 } 6005 } else if (hpts_timeout) { 6006 /* 6007 * With respect to inp_flags2 here, lets let any new acks wake 6008 * us up here. Since we are not pacing (no pacing timer), output 6009 * can happen so we should let it. If its a Rack timer, then any inbound 6010 * packet probably won't change the sending (we will be blocked) 6011 * but it may change the prr stats so letting it in (the set defaults 6012 * at the start of this block) are good enough. 6013 */ 6014 (void)tcp_hpts_insert_diag(tp->t_inpcb, HPTS_USEC_TO_SLOTS(hpts_timeout), 6015 __LINE__, &diag); 6016 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 6017 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 6018 } else { 6019 /* No timer starting */ 6020 #ifdef INVARIANTS 6021 if (SEQ_GT(tp->snd_max, tp->snd_una)) { 6022 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?", 6023 tp, rack, tot_len_this_send, cts, slot, hpts_timeout); 6024 } 6025 #endif 6026 } 6027 rack->rc_tmr_stopped = 0; 6028 if (slot) 6029 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv); 6030 } 6031 6032 /* 6033 * RACK Timer, here we simply do logging and house keeping. 6034 * the normal rack_output() function will call the 6035 * appropriate thing to check if we need to do a RACK retransmit. 6036 * We return 1, saying don't proceed with rack_output only 6037 * when all timers have been stopped (destroyed PCB?). 6038 */ 6039 static int 6040 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 6041 { 6042 /* 6043 * This timer simply provides an internal trigger to send out data. 6044 * The check_recovery_mode call will see if there are needed 6045 * retransmissions, if so we will enter fast-recovery. The output 6046 * call may or may not do the same thing depending on sysctl 6047 * settings. 6048 */ 6049 struct rack_sendmap *rsm; 6050 6051 if (tp->t_timers->tt_flags & TT_STOPPED) { 6052 return (1); 6053 } 6054 counter_u64_add(rack_to_tot, 1); 6055 if (rack->r_state && (rack->r_state != tp->t_state)) 6056 rack_set_state(tp, rack); 6057 rack->rc_on_min_to = 0; 6058 rsm = rack_check_recovery_mode(tp, cts); 6059 rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm); 6060 if (rsm) { 6061 rack->r_ctl.rc_resend = rsm; 6062 rack->r_timer_override = 1; 6063 if (rack->use_rack_rr) { 6064 /* 6065 * Don't accumulate extra pacing delay 6066 * we are allowing the rack timer to 6067 * over-ride pacing i.e. rrr takes precedence 6068 * if the pacing interval is longer than the rrr 6069 * time (in other words we get the min pacing 6070 * time versus rrr pacing time). 6071 */ 6072 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 6073 } 6074 } 6075 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 6076 if (rsm == NULL) { 6077 /* restart a timer and return 1 */ 6078 rack_start_hpts_timer(rack, tp, cts, 6079 0, 0, 0); 6080 return (1); 6081 } 6082 return (0); 6083 } 6084 6085 static void 6086 rack_adjust_orig_mlen(struct rack_sendmap *rsm) 6087 { 6088 if (rsm->m->m_len > rsm->orig_m_len) { 6089 /* 6090 * Mbuf grew, caused by sbcompress, our offset does 6091 * not change. 6092 */ 6093 rsm->orig_m_len = rsm->m->m_len; 6094 } else if (rsm->m->m_len < rsm->orig_m_len) { 6095 /* 6096 * Mbuf shrank, trimmed off the top by an ack, our 6097 * offset changes. 6098 */ 6099 rsm->soff -= (rsm->orig_m_len - rsm->m->m_len); 6100 rsm->orig_m_len = rsm->m->m_len; 6101 } 6102 } 6103 6104 static void 6105 rack_setup_offset_for_rsm(struct rack_sendmap *src_rsm, struct rack_sendmap *rsm) 6106 { 6107 struct mbuf *m; 6108 uint32_t soff; 6109 6110 if (src_rsm->m && (src_rsm->orig_m_len != src_rsm->m->m_len)) { 6111 /* Fix up the orig_m_len and possibly the mbuf offset */ 6112 rack_adjust_orig_mlen(src_rsm); 6113 } 6114 m = src_rsm->m; 6115 soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start); 6116 while (soff >= m->m_len) { 6117 /* Move out past this mbuf */ 6118 soff -= m->m_len; 6119 m = m->m_next; 6120 KASSERT((m != NULL), 6121 ("rsm:%p nrsm:%p hit at soff:%u null m", 6122 src_rsm, rsm, soff)); 6123 } 6124 rsm->m = m; 6125 rsm->soff = soff; 6126 rsm->orig_m_len = m->m_len; 6127 } 6128 6129 static __inline void 6130 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm, 6131 struct rack_sendmap *rsm, uint32_t start) 6132 { 6133 int idx; 6134 6135 nrsm->r_start = start; 6136 nrsm->r_end = rsm->r_end; 6137 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 6138 nrsm->r_flags = rsm->r_flags; 6139 nrsm->r_dupack = rsm->r_dupack; 6140 nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed; 6141 nrsm->r_rtr_bytes = 0; 6142 rsm->r_end = nrsm->r_start; 6143 nrsm->r_just_ret = rsm->r_just_ret; 6144 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 6145 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 6146 } 6147 /* Now if we have SYN flag we keep it on the left edge */ 6148 if (nrsm->r_flags & RACK_HAS_SYN) 6149 nrsm->r_flags &= ~RACK_HAS_SYN; 6150 /* Now if we have a FIN flag we keep it on the right edge */ 6151 if (rsm->r_flags & RACK_HAS_FIN) 6152 rsm->r_flags &= ~RACK_HAS_FIN; 6153 /* Push bit must go to the right edge as well */ 6154 if (rsm->r_flags & RACK_HAD_PUSH) 6155 rsm->r_flags &= ~RACK_HAD_PUSH; 6156 /* Clone over the state of the hw_tls flag */ 6157 nrsm->r_hw_tls = rsm->r_hw_tls; 6158 /* 6159 * Now we need to find nrsm's new location in the mbuf chain 6160 * we basically calculate a new offset, which is soff + 6161 * how much is left in original rsm. Then we walk out the mbuf 6162 * chain to find the righ postion, it may be the same mbuf 6163 * or maybe not. 6164 */ 6165 KASSERT(((rsm->m != NULL) || 6166 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))), 6167 ("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack)); 6168 if (rsm->m) 6169 rack_setup_offset_for_rsm(rsm, nrsm); 6170 } 6171 6172 static struct rack_sendmap * 6173 rack_merge_rsm(struct tcp_rack *rack, 6174 struct rack_sendmap *l_rsm, 6175 struct rack_sendmap *r_rsm) 6176 { 6177 /* 6178 * We are merging two ack'd RSM's, 6179 * the l_rsm is on the left (lower seq 6180 * values) and the r_rsm is on the right 6181 * (higher seq value). The simplest way 6182 * to merge these is to move the right 6183 * one into the left. I don't think there 6184 * is any reason we need to try to find 6185 * the oldest (or last oldest retransmitted). 6186 */ 6187 struct rack_sendmap *rm; 6188 6189 rack_log_map_chg(rack->rc_tp, rack, NULL, 6190 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__); 6191 l_rsm->r_end = r_rsm->r_end; 6192 if (l_rsm->r_dupack < r_rsm->r_dupack) 6193 l_rsm->r_dupack = r_rsm->r_dupack; 6194 if (r_rsm->r_rtr_bytes) 6195 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 6196 if (r_rsm->r_in_tmap) { 6197 /* This really should not happen */ 6198 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext); 6199 r_rsm->r_in_tmap = 0; 6200 } 6201 6202 /* Now the flags */ 6203 if (r_rsm->r_flags & RACK_HAS_FIN) 6204 l_rsm->r_flags |= RACK_HAS_FIN; 6205 if (r_rsm->r_flags & RACK_TLP) 6206 l_rsm->r_flags |= RACK_TLP; 6207 if (r_rsm->r_flags & RACK_RWND_COLLAPSED) 6208 l_rsm->r_flags |= RACK_RWND_COLLAPSED; 6209 if ((r_rsm->r_flags & RACK_APP_LIMITED) && 6210 ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) { 6211 /* 6212 * If both are app-limited then let the 6213 * free lower the count. If right is app 6214 * limited and left is not, transfer. 6215 */ 6216 l_rsm->r_flags |= RACK_APP_LIMITED; 6217 r_rsm->r_flags &= ~RACK_APP_LIMITED; 6218 if (r_rsm == rack->r_ctl.rc_first_appl) 6219 rack->r_ctl.rc_first_appl = l_rsm; 6220 } 6221 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, r_rsm); 6222 #ifdef INVARIANTS 6223 if (rm != r_rsm) { 6224 panic("removing head in rack:%p rsm:%p rm:%p", 6225 rack, r_rsm, rm); 6226 } 6227 #endif 6228 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 6229 /* Transfer the split limit to the map we free */ 6230 r_rsm->r_limit_type = l_rsm->r_limit_type; 6231 l_rsm->r_limit_type = 0; 6232 } 6233 rack_free(rack, r_rsm); 6234 return (l_rsm); 6235 } 6236 6237 /* 6238 * TLP Timer, here we simply setup what segment we want to 6239 * have the TLP expire on, the normal rack_output() will then 6240 * send it out. 6241 * 6242 * We return 1, saying don't proceed with rack_output only 6243 * when all timers have been stopped (destroyed PCB?). 6244 */ 6245 static int 6246 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp) 6247 { 6248 /* 6249 * Tail Loss Probe. 6250 */ 6251 struct rack_sendmap *rsm = NULL; 6252 struct rack_sendmap *insret; 6253 struct socket *so; 6254 uint32_t amm; 6255 uint32_t out, avail; 6256 int collapsed_win = 0; 6257 6258 if (tp->t_timers->tt_flags & TT_STOPPED) { 6259 return (1); 6260 } 6261 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 6262 /* Its not time yet */ 6263 return (0); 6264 } 6265 if (ctf_progress_timeout_check(tp, true)) { 6266 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 6267 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 6268 return (1); 6269 } 6270 /* 6271 * A TLP timer has expired. We have been idle for 2 rtts. So we now 6272 * need to figure out how to force a full MSS segment out. 6273 */ 6274 rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL); 6275 rack->r_ctl.retran_during_recovery = 0; 6276 rack->r_ctl.dsack_byte_cnt = 0; 6277 counter_u64_add(rack_tlp_tot, 1); 6278 if (rack->r_state && (rack->r_state != tp->t_state)) 6279 rack_set_state(tp, rack); 6280 so = tp->t_inpcb->inp_socket; 6281 avail = sbavail(&so->so_snd); 6282 out = tp->snd_max - tp->snd_una; 6283 if (out > tp->snd_wnd) { 6284 /* special case, we need a retransmission */ 6285 collapsed_win = 1; 6286 goto need_retran; 6287 } 6288 if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) { 6289 rack->r_ctl.dsack_persist--; 6290 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 6291 rack->r_ctl.num_dsack = 0; 6292 } 6293 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 6294 } 6295 if ((tp->t_flags & TF_GPUTINPROG) && 6296 (rack->r_ctl.rc_tlp_cnt_out == 1)) { 6297 /* 6298 * If this is the second in a row 6299 * TLP and we are doing a measurement 6300 * its time to abandon the measurement. 6301 * Something is likely broken on 6302 * the clients network and measuring a 6303 * broken network does us no good. 6304 */ 6305 tp->t_flags &= ~TF_GPUTINPROG; 6306 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 6307 rack->r_ctl.rc_gp_srtt /*flex1*/, 6308 tp->gput_seq, 6309 0, 0, 18, __LINE__, NULL, 0); 6310 } 6311 /* 6312 * Check our send oldest always settings, and if 6313 * there is an oldest to send jump to the need_retran. 6314 */ 6315 if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0)) 6316 goto need_retran; 6317 6318 if (avail > out) { 6319 /* New data is available */ 6320 amm = avail - out; 6321 if (amm > ctf_fixed_maxseg(tp)) { 6322 amm = ctf_fixed_maxseg(tp); 6323 if ((amm + out) > tp->snd_wnd) { 6324 /* We are rwnd limited */ 6325 goto need_retran; 6326 } 6327 } else if (amm < ctf_fixed_maxseg(tp)) { 6328 /* not enough to fill a MTU */ 6329 goto need_retran; 6330 } 6331 if (IN_FASTRECOVERY(tp->t_flags)) { 6332 /* Unlikely */ 6333 if (rack->rack_no_prr == 0) { 6334 if (out + amm <= tp->snd_wnd) { 6335 rack->r_ctl.rc_prr_sndcnt = amm; 6336 rack->r_ctl.rc_tlp_new_data = amm; 6337 rack_log_to_prr(rack, 4, 0); 6338 } 6339 } else 6340 goto need_retran; 6341 } else { 6342 /* Set the send-new override */ 6343 if (out + amm <= tp->snd_wnd) 6344 rack->r_ctl.rc_tlp_new_data = amm; 6345 else 6346 goto need_retran; 6347 } 6348 rack->r_ctl.rc_tlpsend = NULL; 6349 counter_u64_add(rack_tlp_newdata, 1); 6350 goto send; 6351 } 6352 need_retran: 6353 /* 6354 * Ok we need to arrange the last un-acked segment to be re-sent, or 6355 * optionally the first un-acked segment. 6356 */ 6357 if (collapsed_win == 0) { 6358 if (rack_always_send_oldest) 6359 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6360 else { 6361 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 6362 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) { 6363 rsm = rack_find_high_nonack(rack, rsm); 6364 } 6365 } 6366 if (rsm == NULL) { 6367 counter_u64_add(rack_tlp_does_nada, 1); 6368 #ifdef TCP_BLACKBOX 6369 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true); 6370 #endif 6371 goto out; 6372 } 6373 } else { 6374 /* 6375 * We must find the last segment 6376 * that was acceptable by the client. 6377 */ 6378 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) { 6379 if ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0) { 6380 /* Found one */ 6381 break; 6382 } 6383 } 6384 if (rsm == NULL) { 6385 /* None? if so send the first */ 6386 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 6387 if (rsm == NULL) { 6388 counter_u64_add(rack_tlp_does_nada, 1); 6389 #ifdef TCP_BLACKBOX 6390 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true); 6391 #endif 6392 goto out; 6393 } 6394 } 6395 } 6396 if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) { 6397 /* 6398 * We need to split this the last segment in two. 6399 */ 6400 struct rack_sendmap *nrsm; 6401 6402 nrsm = rack_alloc_full_limit(rack); 6403 if (nrsm == NULL) { 6404 /* 6405 * No memory to split, we will just exit and punt 6406 * off to the RXT timer. 6407 */ 6408 counter_u64_add(rack_tlp_does_nada, 1); 6409 goto out; 6410 } 6411 rack_clone_rsm(rack, nrsm, rsm, 6412 (rsm->r_end - ctf_fixed_maxseg(tp))); 6413 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 6414 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 6415 #ifdef INVARIANTS 6416 if (insret != NULL) { 6417 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 6418 nrsm, insret, rack, rsm); 6419 } 6420 #endif 6421 if (rsm->r_in_tmap) { 6422 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6423 nrsm->r_in_tmap = 1; 6424 } 6425 rsm = nrsm; 6426 } 6427 rack->r_ctl.rc_tlpsend = rsm; 6428 send: 6429 /* Make sure output path knows we are doing a TLP */ 6430 *doing_tlp = 1; 6431 rack->r_timer_override = 1; 6432 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 6433 return (0); 6434 out: 6435 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 6436 return (0); 6437 } 6438 6439 /* 6440 * Delayed ack Timer, here we simply need to setup the 6441 * ACK_NOW flag and remove the DELACK flag. From there 6442 * the output routine will send the ack out. 6443 * 6444 * We only return 1, saying don't proceed, if all timers 6445 * are stopped (destroyed PCB?). 6446 */ 6447 static int 6448 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 6449 { 6450 if (tp->t_timers->tt_flags & TT_STOPPED) { 6451 return (1); 6452 } 6453 rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL); 6454 tp->t_flags &= ~TF_DELACK; 6455 tp->t_flags |= TF_ACKNOW; 6456 KMOD_TCPSTAT_INC(tcps_delack); 6457 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 6458 return (0); 6459 } 6460 6461 /* 6462 * Persists timer, here we simply send the 6463 * same thing as a keepalive will. 6464 * the one byte send. 6465 * 6466 * We only return 1, saying don't proceed, if all timers 6467 * are stopped (destroyed PCB?). 6468 */ 6469 static int 6470 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 6471 { 6472 struct tcptemp *t_template; 6473 struct inpcb *inp; 6474 int32_t retval = 1; 6475 6476 inp = tp->t_inpcb; 6477 6478 if (tp->t_timers->tt_flags & TT_STOPPED) { 6479 return (1); 6480 } 6481 if (rack->rc_in_persist == 0) 6482 return (0); 6483 if (ctf_progress_timeout_check(tp, false)) { 6484 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 6485 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 6486 tcp_set_inp_to_drop(inp, ETIMEDOUT); 6487 return (1); 6488 } 6489 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 6490 /* 6491 * Persistence timer into zero window. Force a byte to be output, if 6492 * possible. 6493 */ 6494 KMOD_TCPSTAT_INC(tcps_persisttimeo); 6495 /* 6496 * Hack: if the peer is dead/unreachable, we do not time out if the 6497 * window is closed. After a full backoff, drop the connection if 6498 * the idle time (no responses to probes) reaches the maximum 6499 * backoff that we would use if retransmitting. 6500 */ 6501 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 6502 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 6503 TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) { 6504 KMOD_TCPSTAT_INC(tcps_persistdrop); 6505 retval = 1; 6506 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 6507 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 6508 goto out; 6509 } 6510 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) && 6511 tp->snd_una == tp->snd_max) 6512 rack_exit_persist(tp, rack, cts); 6513 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 6514 /* 6515 * If the user has closed the socket then drop a persisting 6516 * connection after a much reduced timeout. 6517 */ 6518 if (tp->t_state > TCPS_CLOSE_WAIT && 6519 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 6520 retval = 1; 6521 KMOD_TCPSTAT_INC(tcps_persistdrop); 6522 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 6523 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 6524 goto out; 6525 } 6526 t_template = tcpip_maketemplate(rack->rc_inp); 6527 if (t_template) { 6528 /* only set it if we were answered */ 6529 if (rack->forced_ack == 0) { 6530 rack->forced_ack = 1; 6531 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL); 6532 } 6533 tcp_respond(tp, t_template->tt_ipgen, 6534 &t_template->tt_t, (struct mbuf *)NULL, 6535 tp->rcv_nxt, tp->snd_una - 1, 0); 6536 /* This sends an ack */ 6537 if (tp->t_flags & TF_DELACK) 6538 tp->t_flags &= ~TF_DELACK; 6539 free(t_template, M_TEMP); 6540 } 6541 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 6542 tp->t_rxtshift++; 6543 out: 6544 rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL); 6545 rack_start_hpts_timer(rack, tp, cts, 6546 0, 0, 0); 6547 return (retval); 6548 } 6549 6550 /* 6551 * If a keepalive goes off, we had no other timers 6552 * happening. We always return 1 here since this 6553 * routine either drops the connection or sends 6554 * out a segment with respond. 6555 */ 6556 static int 6557 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 6558 { 6559 struct tcptemp *t_template; 6560 struct inpcb *inp; 6561 6562 if (tp->t_timers->tt_flags & TT_STOPPED) { 6563 return (1); 6564 } 6565 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 6566 inp = tp->t_inpcb; 6567 rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL); 6568 /* 6569 * Keep-alive timer went off; send something or drop connection if 6570 * idle for too long. 6571 */ 6572 KMOD_TCPSTAT_INC(tcps_keeptimeo); 6573 if (tp->t_state < TCPS_ESTABLISHED) 6574 goto dropit; 6575 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 6576 tp->t_state <= TCPS_CLOSING) { 6577 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 6578 goto dropit; 6579 /* 6580 * Send a packet designed to force a response if the peer is 6581 * up and reachable: either an ACK if the connection is 6582 * still alive, or an RST if the peer has closed the 6583 * connection due to timeout or reboot. Using sequence 6584 * number tp->snd_una-1 causes the transmitted zero-length 6585 * segment to lie outside the receive window; by the 6586 * protocol spec, this requires the correspondent TCP to 6587 * respond. 6588 */ 6589 KMOD_TCPSTAT_INC(tcps_keepprobe); 6590 t_template = tcpip_maketemplate(inp); 6591 if (t_template) { 6592 if (rack->forced_ack == 0) { 6593 rack->forced_ack = 1; 6594 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL); 6595 } 6596 tcp_respond(tp, t_template->tt_ipgen, 6597 &t_template->tt_t, (struct mbuf *)NULL, 6598 tp->rcv_nxt, tp->snd_una - 1, 0); 6599 free(t_template, M_TEMP); 6600 } 6601 } 6602 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 6603 return (1); 6604 dropit: 6605 KMOD_TCPSTAT_INC(tcps_keepdrops); 6606 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 6607 tcp_set_inp_to_drop(rack->rc_inp, ETIMEDOUT); 6608 return (1); 6609 } 6610 6611 /* 6612 * Retransmit helper function, clear up all the ack 6613 * flags and take care of important book keeping. 6614 */ 6615 static void 6616 rack_remxt_tmr(struct tcpcb *tp) 6617 { 6618 /* 6619 * The retransmit timer went off, all sack'd blocks must be 6620 * un-acked. 6621 */ 6622 struct rack_sendmap *rsm, *trsm = NULL; 6623 struct tcp_rack *rack; 6624 6625 rack = (struct tcp_rack *)tp->t_fb_ptr; 6626 rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__); 6627 rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL); 6628 if (rack->r_state && (rack->r_state != tp->t_state)) 6629 rack_set_state(tp, rack); 6630 /* 6631 * Ideally we would like to be able to 6632 * mark SACK-PASS on anything not acked here. 6633 * 6634 * However, if we do that we would burst out 6635 * all that data 1ms apart. This would be unwise, 6636 * so for now we will just let the normal rxt timer 6637 * and tlp timer take care of it. 6638 * 6639 * Also we really need to stick them back in sequence 6640 * order. This way we send in the proper order and any 6641 * sacks that come floating in will "re-ack" the data. 6642 * To do this we zap the tmap with an INIT and then 6643 * walk through and place every rsm in the RB tree 6644 * back in its seq ordered place. 6645 */ 6646 TAILQ_INIT(&rack->r_ctl.rc_tmap); 6647 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) { 6648 rsm->r_dupack = 0; 6649 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 6650 /* We must re-add it back to the tlist */ 6651 if (trsm == NULL) { 6652 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 6653 } else { 6654 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext); 6655 } 6656 rsm->r_in_tmap = 1; 6657 trsm = rsm; 6658 if (rsm->r_flags & RACK_ACKED) 6659 rsm->r_flags |= RACK_WAS_ACKED; 6660 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS); 6661 } 6662 /* Clear the count (we just un-acked them) */ 6663 rack->r_ctl.rc_last_timeout_snduna = tp->snd_una; 6664 rack->r_ctl.rc_sacked = 0; 6665 rack->r_ctl.rc_sacklast = NULL; 6666 rack->r_ctl.rc_agg_delayed = 0; 6667 rack->r_early = 0; 6668 rack->r_ctl.rc_agg_early = 0; 6669 rack->r_late = 0; 6670 /* Clear the tlp rtx mark */ 6671 rack->r_ctl.rc_resend = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 6672 if (rack->r_ctl.rc_resend != NULL) 6673 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT; 6674 rack->r_ctl.rc_prr_sndcnt = 0; 6675 rack_log_to_prr(rack, 6, 0); 6676 rack->r_timer_override = 1; 6677 if ((((tp->t_flags & TF_SACK_PERMIT) == 0) 6678 #ifdef NETFLIX_EXP_DETECTION 6679 || (rack->sack_attack_disable != 0) 6680 #endif 6681 ) && ((tp->t_flags & TF_SENTFIN) == 0)) { 6682 /* 6683 * For non-sack customers new data 6684 * needs to go out as retransmits until 6685 * we retransmit up to snd_max. 6686 */ 6687 rack->r_must_retran = 1; 6688 rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp, 6689 rack->r_ctl.rc_sacked); 6690 } 6691 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max; 6692 } 6693 6694 static void 6695 rack_convert_rtts(struct tcpcb *tp) 6696 { 6697 if (tp->t_srtt > 1) { 6698 uint32_t val, frac; 6699 6700 val = tp->t_srtt >> TCP_RTT_SHIFT; 6701 frac = tp->t_srtt & 0x1f; 6702 tp->t_srtt = TICKS_2_USEC(val); 6703 /* 6704 * frac is the fractional part of the srtt (if any) 6705 * but its in ticks and every bit represents 6706 * 1/32nd of a hz. 6707 */ 6708 if (frac) { 6709 if (hz == 1000) { 6710 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE); 6711 } else { 6712 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE)); 6713 } 6714 tp->t_srtt += frac; 6715 } 6716 } 6717 if (tp->t_rttvar) { 6718 uint32_t val, frac; 6719 6720 val = tp->t_rttvar >> TCP_RTTVAR_SHIFT; 6721 frac = tp->t_rttvar & 0x1f; 6722 tp->t_rttvar = TICKS_2_USEC(val); 6723 /* 6724 * frac is the fractional part of the srtt (if any) 6725 * but its in ticks and every bit represents 6726 * 1/32nd of a hz. 6727 */ 6728 if (frac) { 6729 if (hz == 1000) { 6730 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE); 6731 } else { 6732 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE)); 6733 } 6734 tp->t_rttvar += frac; 6735 } 6736 } 6737 tp->t_rxtcur = RACK_REXMTVAL(tp); 6738 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 6739 tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop); 6740 } 6741 if (tp->t_rxtcur > rack_rto_max) { 6742 tp->t_rxtcur = rack_rto_max; 6743 } 6744 } 6745 6746 static void 6747 rack_cc_conn_init(struct tcpcb *tp) 6748 { 6749 struct tcp_rack *rack; 6750 uint32_t srtt; 6751 6752 rack = (struct tcp_rack *)tp->t_fb_ptr; 6753 srtt = tp->t_srtt; 6754 cc_conn_init(tp); 6755 /* 6756 * Now convert to rack's internal format, 6757 * if required. 6758 */ 6759 if ((srtt == 0) && (tp->t_srtt != 0)) 6760 rack_convert_rtts(tp); 6761 /* 6762 * We want a chance to stay in slowstart as 6763 * we create a connection. TCP spec says that 6764 * initially ssthresh is infinite. For our 6765 * purposes that is the snd_wnd. 6766 */ 6767 if (tp->snd_ssthresh < tp->snd_wnd) { 6768 tp->snd_ssthresh = tp->snd_wnd; 6769 } 6770 /* 6771 * We also want to assure a IW worth of 6772 * data can get inflight. 6773 */ 6774 if (rc_init_window(rack) < tp->snd_cwnd) 6775 tp->snd_cwnd = rc_init_window(rack); 6776 } 6777 6778 /* 6779 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 6780 * we will setup to retransmit the lowest seq number outstanding. 6781 */ 6782 static int 6783 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 6784 { 6785 int32_t rexmt; 6786 struct inpcb *inp; 6787 int32_t retval = 0; 6788 bool isipv6; 6789 6790 inp = tp->t_inpcb; 6791 if (tp->t_timers->tt_flags & TT_STOPPED) { 6792 return (1); 6793 } 6794 if ((tp->t_flags & TF_GPUTINPROG) && 6795 (tp->t_rxtshift)) { 6796 /* 6797 * We have had a second timeout 6798 * measurements on successive rxt's are not profitable. 6799 * It is unlikely to be of any use (the network is 6800 * broken or the client went away). 6801 */ 6802 tp->t_flags &= ~TF_GPUTINPROG; 6803 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 6804 rack->r_ctl.rc_gp_srtt /*flex1*/, 6805 tp->gput_seq, 6806 0, 0, 18, __LINE__, NULL, 0); 6807 } 6808 if (ctf_progress_timeout_check(tp, false)) { 6809 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 6810 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 6811 tcp_set_inp_to_drop(inp, ETIMEDOUT); 6812 return (1); 6813 } 6814 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 6815 rack->r_ctl.retran_during_recovery = 0; 6816 rack->r_ctl.dsack_byte_cnt = 0; 6817 if (IN_FASTRECOVERY(tp->t_flags)) 6818 tp->t_flags |= TF_WASFRECOVERY; 6819 else 6820 tp->t_flags &= ~TF_WASFRECOVERY; 6821 if (IN_CONGRECOVERY(tp->t_flags)) 6822 tp->t_flags |= TF_WASCRECOVERY; 6823 else 6824 tp->t_flags &= ~TF_WASCRECOVERY; 6825 if (TCPS_HAVEESTABLISHED(tp->t_state) && 6826 (tp->snd_una == tp->snd_max)) { 6827 /* Nothing outstanding .. nothing to do */ 6828 return (0); 6829 } 6830 if (rack->r_ctl.dsack_persist) { 6831 rack->r_ctl.dsack_persist--; 6832 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 6833 rack->r_ctl.num_dsack = 0; 6834 } 6835 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 6836 } 6837 /* 6838 * Rack can only run one timer at a time, so we cannot 6839 * run a KEEPINIT (gating SYN sending) and a retransmit 6840 * timer for the SYN. So if we are in a front state and 6841 * have a KEEPINIT timer we need to check the first transmit 6842 * against now to see if we have exceeded the KEEPINIT time 6843 * (if one is set). 6844 */ 6845 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) && 6846 (TP_KEEPINIT(tp) != 0)) { 6847 struct rack_sendmap *rsm; 6848 6849 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 6850 if (rsm) { 6851 /* Ok we have something outstanding to test keepinit with */ 6852 if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) && 6853 ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) { 6854 /* We have exceeded the KEEPINIT time */ 6855 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 6856 goto drop_it; 6857 } 6858 } 6859 } 6860 /* 6861 * Retransmission timer went off. Message has not been acked within 6862 * retransmit interval. Back off to a longer retransmit interval 6863 * and retransmit one segment. 6864 */ 6865 rack_remxt_tmr(tp); 6866 if ((rack->r_ctl.rc_resend == NULL) || 6867 ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) { 6868 /* 6869 * If the rwnd collapsed on 6870 * the one we are retransmitting 6871 * it does not count against the 6872 * rxt count. 6873 */ 6874 tp->t_rxtshift++; 6875 } 6876 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) { 6877 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 6878 drop_it: 6879 tp->t_rxtshift = TCP_MAXRXTSHIFT; 6880 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 6881 retval = 1; 6882 tcp_set_inp_to_drop(rack->rc_inp, 6883 (tp->t_softerror ? (uint16_t) tp->t_softerror : ETIMEDOUT)); 6884 goto out; 6885 } 6886 if (tp->t_state == TCPS_SYN_SENT) { 6887 /* 6888 * If the SYN was retransmitted, indicate CWND to be limited 6889 * to 1 segment in cc_conn_init(). 6890 */ 6891 tp->snd_cwnd = 1; 6892 } else if (tp->t_rxtshift == 1) { 6893 /* 6894 * first retransmit; record ssthresh and cwnd so they can be 6895 * recovered if this turns out to be a "bad" retransmit. A 6896 * retransmit is considered "bad" if an ACK for this segment 6897 * is received within RTT/2 interval; the assumption here is 6898 * that the ACK was already in flight. See "On Estimating 6899 * End-to-End Network Path Properties" by Allman and Paxson 6900 * for more details. 6901 */ 6902 tp->snd_cwnd_prev = tp->snd_cwnd; 6903 tp->snd_ssthresh_prev = tp->snd_ssthresh; 6904 tp->snd_recover_prev = tp->snd_recover; 6905 tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2); 6906 tp->t_flags |= TF_PREVVALID; 6907 } else if ((tp->t_flags & TF_RCVD_TSTMP) == 0) 6908 tp->t_flags &= ~TF_PREVVALID; 6909 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 6910 if ((tp->t_state == TCPS_SYN_SENT) || 6911 (tp->t_state == TCPS_SYN_RECEIVED)) 6912 rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift]; 6913 else 6914 rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift]; 6915 6916 RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt, 6917 max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop); 6918 /* 6919 * We enter the path for PLMTUD if connection is established or, if 6920 * connection is FIN_WAIT_1 status, reason for the last is that if 6921 * amount of data we send is very small, we could send it in couple 6922 * of packets and process straight to FIN. In that case we won't 6923 * catch ESTABLISHED state. 6924 */ 6925 #ifdef INET6 6926 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false; 6927 #else 6928 isipv6 = false; 6929 #endif 6930 if (((V_tcp_pmtud_blackhole_detect == 1) || 6931 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 6932 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 6933 ((tp->t_state == TCPS_ESTABLISHED) || 6934 (tp->t_state == TCPS_FIN_WAIT_1))) { 6935 /* 6936 * Idea here is that at each stage of mtu probe (usually, 6937 * 1448 -> 1188 -> 524) should be given 2 chances to recover 6938 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 6939 * should take care of that. 6940 */ 6941 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 6942 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 6943 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 6944 tp->t_rxtshift % 2 == 0)) { 6945 /* 6946 * Enter Path MTU Black-hole Detection mechanism: - 6947 * Disable Path MTU Discovery (IP "DF" bit). - 6948 * Reduce MTU to lower value than what we negotiated 6949 * with peer. 6950 */ 6951 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 6952 /* Record that we may have found a black hole. */ 6953 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 6954 /* Keep track of previous MSS. */ 6955 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 6956 } 6957 6958 /* 6959 * Reduce the MSS to blackhole value or to the 6960 * default in an attempt to retransmit. 6961 */ 6962 #ifdef INET6 6963 if (isipv6 && 6964 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 6965 /* Use the sysctl tuneable blackhole MSS. */ 6966 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 6967 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 6968 } else if (isipv6) { 6969 /* Use the default MSS. */ 6970 tp->t_maxseg = V_tcp_v6mssdflt; 6971 /* 6972 * Disable Path MTU Discovery when we switch 6973 * to minmss. 6974 */ 6975 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 6976 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 6977 } 6978 #endif 6979 #if defined(INET6) && defined(INET) 6980 else 6981 #endif 6982 #ifdef INET 6983 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 6984 /* Use the sysctl tuneable blackhole MSS. */ 6985 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 6986 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 6987 } else { 6988 /* Use the default MSS. */ 6989 tp->t_maxseg = V_tcp_mssdflt; 6990 /* 6991 * Disable Path MTU Discovery when we switch 6992 * to minmss. 6993 */ 6994 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 6995 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 6996 } 6997 #endif 6998 } else { 6999 /* 7000 * If further retransmissions are still unsuccessful 7001 * with a lowered MTU, maybe this isn't a blackhole 7002 * and we restore the previous MSS and blackhole 7003 * detection flags. The limit '6' is determined by 7004 * giving each probe stage (1448, 1188, 524) 2 7005 * chances to recover. 7006 */ 7007 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 7008 (tp->t_rxtshift >= 6)) { 7009 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 7010 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 7011 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 7012 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 7013 } 7014 } 7015 } 7016 /* 7017 * Disable RFC1323 and SACK if we haven't got any response to 7018 * our third SYN to work-around some broken terminal servers 7019 * (most of which have hopefully been retired) that have bad VJ 7020 * header compression code which trashes TCP segments containing 7021 * unknown-to-them TCP options. 7022 */ 7023 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 7024 (tp->t_rxtshift == 3)) 7025 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT); 7026 /* 7027 * If we backed off this far, our srtt estimate is probably bogus. 7028 * Clobber it so we'll take the next rtt measurement as our srtt; 7029 * move the current srtt into rttvar to keep the current retransmit 7030 * times until then. 7031 */ 7032 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 7033 #ifdef INET6 7034 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 7035 in6_losing(tp->t_inpcb); 7036 else 7037 #endif 7038 in_losing(tp->t_inpcb); 7039 tp->t_rttvar += tp->t_srtt; 7040 tp->t_srtt = 0; 7041 } 7042 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 7043 tp->snd_recover = tp->snd_max; 7044 tp->t_flags |= TF_ACKNOW; 7045 tp->t_rtttime = 0; 7046 rack_cong_signal(tp, CC_RTO, tp->snd_una); 7047 out: 7048 return (retval); 7049 } 7050 7051 static int 7052 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp) 7053 { 7054 int32_t ret = 0; 7055 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 7056 7057 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7058 (tp->t_flags & TF_GPUTINPROG)) { 7059 /* 7060 * We have a goodput in progress 7061 * and we have entered a late state. 7062 * Do we have enough data in the sb 7063 * to handle the GPUT request? 7064 */ 7065 uint32_t bytes; 7066 7067 bytes = tp->gput_ack - tp->gput_seq; 7068 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 7069 bytes += tp->gput_seq - tp->snd_una; 7070 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) { 7071 /* 7072 * There are not enough bytes in the socket 7073 * buffer that have been sent to cover this 7074 * measurement. Cancel it. 7075 */ 7076 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 7077 rack->r_ctl.rc_gp_srtt /*flex1*/, 7078 tp->gput_seq, 7079 0, 0, 18, __LINE__, NULL, 0); 7080 tp->t_flags &= ~TF_GPUTINPROG; 7081 } 7082 } 7083 if (timers == 0) { 7084 return (0); 7085 } 7086 if (tp->t_state == TCPS_LISTEN) { 7087 /* no timers on listen sockets */ 7088 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 7089 return (0); 7090 return (1); 7091 } 7092 if ((timers & PACE_TMR_RACK) && 7093 rack->rc_on_min_to) { 7094 /* 7095 * For the rack timer when we 7096 * are on a min-timeout (which means rrr_conf = 3) 7097 * we don't want to check the timer. It may 7098 * be going off for a pace and thats ok we 7099 * want to send the retransmit (if its ready). 7100 * 7101 * If its on a normal rack timer (non-min) then 7102 * we will check if its expired. 7103 */ 7104 goto skip_time_check; 7105 } 7106 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 7107 uint32_t left; 7108 7109 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 7110 ret = -1; 7111 rack_log_to_processing(rack, cts, ret, 0); 7112 return (0); 7113 } 7114 if (hpts_calling == 0) { 7115 /* 7116 * A user send or queued mbuf (sack) has called us? We 7117 * return 0 and let the pacing guards 7118 * deal with it if they should or 7119 * should not cause a send. 7120 */ 7121 ret = -2; 7122 rack_log_to_processing(rack, cts, ret, 0); 7123 return (0); 7124 } 7125 /* 7126 * Ok our timer went off early and we are not paced false 7127 * alarm, go back to sleep. 7128 */ 7129 ret = -3; 7130 left = rack->r_ctl.rc_timer_exp - cts; 7131 tcp_hpts_insert(tp->t_inpcb, HPTS_MS_TO_SLOTS(left)); 7132 rack_log_to_processing(rack, cts, ret, left); 7133 return (1); 7134 } 7135 skip_time_check: 7136 rack->rc_tmr_stopped = 0; 7137 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 7138 if (timers & PACE_TMR_DELACK) { 7139 ret = rack_timeout_delack(tp, rack, cts); 7140 } else if (timers & PACE_TMR_RACK) { 7141 rack->r_ctl.rc_tlp_rxt_last_time = cts; 7142 rack->r_fast_output = 0; 7143 ret = rack_timeout_rack(tp, rack, cts); 7144 } else if (timers & PACE_TMR_TLP) { 7145 rack->r_ctl.rc_tlp_rxt_last_time = cts; 7146 ret = rack_timeout_tlp(tp, rack, cts, doing_tlp); 7147 } else if (timers & PACE_TMR_RXT) { 7148 rack->r_ctl.rc_tlp_rxt_last_time = cts; 7149 rack->r_fast_output = 0; 7150 ret = rack_timeout_rxt(tp, rack, cts); 7151 } else if (timers & PACE_TMR_PERSIT) { 7152 ret = rack_timeout_persist(tp, rack, cts); 7153 } else if (timers & PACE_TMR_KEEP) { 7154 ret = rack_timeout_keepalive(tp, rack, cts); 7155 } 7156 rack_log_to_processing(rack, cts, ret, timers); 7157 return (ret); 7158 } 7159 7160 static void 7161 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line) 7162 { 7163 struct timeval tv; 7164 uint32_t us_cts, flags_on_entry; 7165 uint8_t hpts_removed = 0; 7166 7167 flags_on_entry = rack->r_ctl.rc_hpts_flags; 7168 us_cts = tcp_get_usecs(&tv); 7169 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 7170 ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) || 7171 ((tp->snd_max - tp->snd_una) == 0))) { 7172 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 7173 hpts_removed = 1; 7174 /* If we were not delayed cancel out the flag. */ 7175 if ((tp->snd_max - tp->snd_una) == 0) 7176 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 7177 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry); 7178 } 7179 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 7180 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 7181 if (rack->rc_inp->inp_in_hpts && 7182 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) { 7183 /* 7184 * Canceling timer's when we have no output being 7185 * paced. We also must remove ourselves from the 7186 * hpts. 7187 */ 7188 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 7189 hpts_removed = 1; 7190 } 7191 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 7192 } 7193 if (hpts_removed == 0) 7194 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry); 7195 } 7196 7197 static void 7198 rack_timer_stop(struct tcpcb *tp, uint32_t timer_type) 7199 { 7200 return; 7201 } 7202 7203 static int 7204 rack_stopall(struct tcpcb *tp) 7205 { 7206 struct tcp_rack *rack; 7207 rack = (struct tcp_rack *)tp->t_fb_ptr; 7208 rack->t_timers_stopped = 1; 7209 return (0); 7210 } 7211 7212 static void 7213 rack_timer_activate(struct tcpcb *tp, uint32_t timer_type, uint32_t delta) 7214 { 7215 return; 7216 } 7217 7218 static int 7219 rack_timer_active(struct tcpcb *tp, uint32_t timer_type) 7220 { 7221 return (0); 7222 } 7223 7224 static void 7225 rack_stop_all_timers(struct tcpcb *tp) 7226 { 7227 struct tcp_rack *rack; 7228 7229 /* 7230 * Assure no timers are running. 7231 */ 7232 if (tcp_timer_active(tp, TT_PERSIST)) { 7233 /* We enter in persists, set the flag appropriately */ 7234 rack = (struct tcp_rack *)tp->t_fb_ptr; 7235 rack->rc_in_persist = 1; 7236 } 7237 tcp_timer_suspend(tp, TT_PERSIST); 7238 tcp_timer_suspend(tp, TT_REXMT); 7239 tcp_timer_suspend(tp, TT_KEEP); 7240 tcp_timer_suspend(tp, TT_DELACK); 7241 } 7242 7243 static void 7244 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 7245 struct rack_sendmap *rsm, uint64_t ts, uint16_t add_flag) 7246 { 7247 int32_t idx; 7248 uint16_t stripped_flags; 7249 7250 rsm->r_rtr_cnt++; 7251 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 7252 rsm->r_dupack = 0; 7253 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) { 7254 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS; 7255 rsm->r_flags |= RACK_OVERMAX; 7256 } 7257 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) { 7258 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 7259 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 7260 } 7261 idx = rsm->r_rtr_cnt - 1; 7262 rsm->r_tim_lastsent[idx] = ts; 7263 stripped_flags = rsm->r_flags & ~(RACK_SENT_SP|RACK_SENT_FP); 7264 if (rsm->r_flags & RACK_ACKED) { 7265 /* Problably MTU discovery messing with us */ 7266 rsm->r_flags &= ~RACK_ACKED; 7267 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7268 } 7269 if (rsm->r_in_tmap) { 7270 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 7271 rsm->r_in_tmap = 0; 7272 } 7273 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 7274 rsm->r_in_tmap = 1; 7275 if (rsm->r_flags & RACK_SACK_PASSED) { 7276 /* We have retransmitted due to the SACK pass */ 7277 rsm->r_flags &= ~RACK_SACK_PASSED; 7278 rsm->r_flags |= RACK_WAS_SACKPASS; 7279 } 7280 } 7281 7282 static uint32_t 7283 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 7284 struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint16_t add_flag) 7285 { 7286 /* 7287 * We (re-)transmitted starting at rsm->r_start for some length 7288 * (possibly less than r_end. 7289 */ 7290 struct rack_sendmap *nrsm, *insret; 7291 uint32_t c_end; 7292 int32_t len; 7293 7294 len = *lenp; 7295 c_end = rsm->r_start + len; 7296 if (SEQ_GEQ(c_end, rsm->r_end)) { 7297 /* 7298 * We retransmitted the whole piece or more than the whole 7299 * slopping into the next rsm. 7300 */ 7301 rack_update_rsm(tp, rack, rsm, ts, add_flag); 7302 if (c_end == rsm->r_end) { 7303 *lenp = 0; 7304 return (0); 7305 } else { 7306 int32_t act_len; 7307 7308 /* Hangs over the end return whats left */ 7309 act_len = rsm->r_end - rsm->r_start; 7310 *lenp = (len - act_len); 7311 return (rsm->r_end); 7312 } 7313 /* We don't get out of this block. */ 7314 } 7315 /* 7316 * Here we retransmitted less than the whole thing which means we 7317 * have to split this into what was transmitted and what was not. 7318 */ 7319 nrsm = rack_alloc_full_limit(rack); 7320 if (nrsm == NULL) { 7321 /* 7322 * We can't get memory, so lets not proceed. 7323 */ 7324 *lenp = 0; 7325 return (0); 7326 } 7327 /* 7328 * So here we are going to take the original rsm and make it what we 7329 * retransmitted. nrsm will be the tail portion we did not 7330 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 7331 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 7332 * 1, 6 and the new piece will be 6, 11. 7333 */ 7334 rack_clone_rsm(rack, nrsm, rsm, c_end); 7335 nrsm->r_dupack = 0; 7336 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2); 7337 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 7338 #ifdef INVARIANTS 7339 if (insret != NULL) { 7340 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 7341 nrsm, insret, rack, rsm); 7342 } 7343 #endif 7344 if (rsm->r_in_tmap) { 7345 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7346 nrsm->r_in_tmap = 1; 7347 } 7348 rsm->r_flags &= (~RACK_HAS_FIN); 7349 rack_update_rsm(tp, rack, rsm, ts, add_flag); 7350 /* Log a split of rsm into rsm and nrsm */ 7351 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 7352 *lenp = 0; 7353 return (0); 7354 } 7355 7356 static void 7357 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 7358 uint32_t seq_out, uint8_t th_flags, int32_t err, uint64_t cts, 7359 struct rack_sendmap *hintrsm, uint16_t add_flag, struct mbuf *s_mb, uint32_t s_moff, int hw_tls) 7360 { 7361 struct tcp_rack *rack; 7362 struct rack_sendmap *rsm, *nrsm, *insret, fe; 7363 register uint32_t snd_max, snd_una; 7364 7365 /* 7366 * Add to the RACK log of packets in flight or retransmitted. If 7367 * there is a TS option we will use the TS echoed, if not we will 7368 * grab a TS. 7369 * 7370 * Retransmissions will increment the count and move the ts to its 7371 * proper place. Note that if options do not include TS's then we 7372 * won't be able to effectively use the ACK for an RTT on a retran. 7373 * 7374 * Notes about r_start and r_end. Lets consider a send starting at 7375 * sequence 1 for 10 bytes. In such an example the r_start would be 7376 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 7377 * This means that r_end is actually the first sequence for the next 7378 * slot (11). 7379 * 7380 */ 7381 /* 7382 * If err is set what do we do XXXrrs? should we not add the thing? 7383 * -- i.e. return if err != 0 or should we pretend we sent it? -- 7384 * i.e. proceed with add ** do this for now. 7385 */ 7386 INP_WLOCK_ASSERT(tp->t_inpcb); 7387 if (err) 7388 /* 7389 * We don't log errors -- we could but snd_max does not 7390 * advance in this case either. 7391 */ 7392 return; 7393 7394 if (th_flags & TH_RST) { 7395 /* 7396 * We don't log resets and we return immediately from 7397 * sending 7398 */ 7399 return; 7400 } 7401 rack = (struct tcp_rack *)tp->t_fb_ptr; 7402 snd_una = tp->snd_una; 7403 snd_max = tp->snd_max; 7404 if (th_flags & (TH_SYN | TH_FIN)) { 7405 /* 7406 * The call to rack_log_output is made before bumping 7407 * snd_max. This means we can record one extra byte on a SYN 7408 * or FIN if seq_out is adding more on and a FIN is present 7409 * (and we are not resending). 7410 */ 7411 if ((th_flags & TH_SYN) && (seq_out == tp->iss)) 7412 len++; 7413 if (th_flags & TH_FIN) 7414 len++; 7415 if (SEQ_LT(snd_max, tp->snd_nxt)) { 7416 /* 7417 * The add/update as not been done for the FIN/SYN 7418 * yet. 7419 */ 7420 snd_max = tp->snd_nxt; 7421 } 7422 } 7423 if (SEQ_LEQ((seq_out + len), snd_una)) { 7424 /* Are sending an old segment to induce an ack (keep-alive)? */ 7425 return; 7426 } 7427 if (SEQ_LT(seq_out, snd_una)) { 7428 /* huh? should we panic? */ 7429 uint32_t end; 7430 7431 end = seq_out + len; 7432 seq_out = snd_una; 7433 if (SEQ_GEQ(end, seq_out)) 7434 len = end - seq_out; 7435 else 7436 len = 0; 7437 } 7438 if (len == 0) { 7439 /* We don't log zero window probes */ 7440 return; 7441 } 7442 rack->r_ctl.rc_time_last_sent = cts; 7443 if (IN_FASTRECOVERY(tp->t_flags)) { 7444 rack->r_ctl.rc_prr_out += len; 7445 } 7446 /* First question is it a retransmission or new? */ 7447 if (seq_out == snd_max) { 7448 /* Its new */ 7449 again: 7450 rsm = rack_alloc(rack); 7451 if (rsm == NULL) { 7452 /* 7453 * Hmm out of memory and the tcb got destroyed while 7454 * we tried to wait. 7455 */ 7456 return; 7457 } 7458 if (th_flags & TH_FIN) { 7459 rsm->r_flags = RACK_HAS_FIN|add_flag; 7460 } else { 7461 rsm->r_flags = add_flag; 7462 } 7463 if (hw_tls) 7464 rsm->r_hw_tls = 1; 7465 rsm->r_tim_lastsent[0] = cts; 7466 rsm->r_rtr_cnt = 1; 7467 rsm->r_rtr_bytes = 0; 7468 if (th_flags & TH_SYN) { 7469 /* The data space is one beyond snd_una */ 7470 rsm->r_flags |= RACK_HAS_SYN; 7471 } 7472 rsm->r_start = seq_out; 7473 rsm->r_end = rsm->r_start + len; 7474 rsm->r_dupack = 0; 7475 /* 7476 * save off the mbuf location that 7477 * sndmbuf_noadv returned (which is 7478 * where we started copying from).. 7479 */ 7480 rsm->m = s_mb; 7481 rsm->soff = s_moff; 7482 /* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */ 7483 if (rsm->m) { 7484 if (rsm->m->m_len <= rsm->soff) { 7485 /* 7486 * XXXrrs Question, will this happen? 7487 * 7488 * If sbsndptr is set at the correct place 7489 * then s_moff should always be somewhere 7490 * within rsm->m. But if the sbsndptr was 7491 * off then that won't be true. If it occurs 7492 * we need to walkout to the correct location. 7493 */ 7494 struct mbuf *lm; 7495 7496 lm = rsm->m; 7497 while (lm->m_len <= rsm->soff) { 7498 rsm->soff -= lm->m_len; 7499 lm = lm->m_next; 7500 KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u", 7501 __func__, rack, s_moff, s_mb, rsm->soff)); 7502 } 7503 rsm->m = lm; 7504 counter_u64_add(rack_sbsndptr_wrong, 1); 7505 } else 7506 counter_u64_add(rack_sbsndptr_right, 1); 7507 rsm->orig_m_len = rsm->m->m_len; 7508 } else 7509 rsm->orig_m_len = 0; 7510 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 7511 /* Log a new rsm */ 7512 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__); 7513 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 7514 #ifdef INVARIANTS 7515 if (insret != NULL) { 7516 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 7517 nrsm, insret, rack, rsm); 7518 } 7519 #endif 7520 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 7521 rsm->r_in_tmap = 1; 7522 /* 7523 * Special case detection, is there just a single 7524 * packet outstanding when we are not in recovery? 7525 * 7526 * If this is true mark it so. 7527 */ 7528 if ((IN_FASTRECOVERY(tp->t_flags) == 0) && 7529 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) { 7530 struct rack_sendmap *prsm; 7531 7532 prsm = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 7533 if (prsm) 7534 prsm->r_one_out_nr = 1; 7535 } 7536 return; 7537 } 7538 /* 7539 * If we reach here its a retransmission and we need to find it. 7540 */ 7541 memset(&fe, 0, sizeof(fe)); 7542 more: 7543 if (hintrsm && (hintrsm->r_start == seq_out)) { 7544 rsm = hintrsm; 7545 hintrsm = NULL; 7546 } else { 7547 /* No hints sorry */ 7548 rsm = NULL; 7549 } 7550 if ((rsm) && (rsm->r_start == seq_out)) { 7551 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag); 7552 if (len == 0) { 7553 return; 7554 } else { 7555 goto more; 7556 } 7557 } 7558 /* Ok it was not the last pointer go through it the hard way. */ 7559 refind: 7560 fe.r_start = seq_out; 7561 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 7562 if (rsm) { 7563 if (rsm->r_start == seq_out) { 7564 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag); 7565 if (len == 0) { 7566 return; 7567 } else { 7568 goto refind; 7569 } 7570 } 7571 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 7572 /* Transmitted within this piece */ 7573 /* 7574 * Ok we must split off the front and then let the 7575 * update do the rest 7576 */ 7577 nrsm = rack_alloc_full_limit(rack); 7578 if (nrsm == NULL) { 7579 rack_update_rsm(tp, rack, rsm, cts, add_flag); 7580 return; 7581 } 7582 /* 7583 * copy rsm to nrsm and then trim the front of rsm 7584 * to not include this part. 7585 */ 7586 rack_clone_rsm(rack, nrsm, rsm, seq_out); 7587 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 7588 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 7589 #ifdef INVARIANTS 7590 if (insret != NULL) { 7591 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 7592 nrsm, insret, rack, rsm); 7593 } 7594 #endif 7595 if (rsm->r_in_tmap) { 7596 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7597 nrsm->r_in_tmap = 1; 7598 } 7599 rsm->r_flags &= (~RACK_HAS_FIN); 7600 seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag); 7601 if (len == 0) { 7602 return; 7603 } else if (len > 0) 7604 goto refind; 7605 } 7606 } 7607 /* 7608 * Hmm not found in map did they retransmit both old and on into the 7609 * new? 7610 */ 7611 if (seq_out == tp->snd_max) { 7612 goto again; 7613 } else if (SEQ_LT(seq_out, tp->snd_max)) { 7614 #ifdef INVARIANTS 7615 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 7616 seq_out, len, tp->snd_una, tp->snd_max); 7617 printf("Starting Dump of all rack entries\n"); 7618 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) { 7619 printf("rsm:%p start:%u end:%u\n", 7620 rsm, rsm->r_start, rsm->r_end); 7621 } 7622 printf("Dump complete\n"); 7623 panic("seq_out not found rack:%p tp:%p", 7624 rack, tp); 7625 #endif 7626 } else { 7627 #ifdef INVARIANTS 7628 /* 7629 * Hmm beyond sndmax? (only if we are using the new rtt-pack 7630 * flag) 7631 */ 7632 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 7633 seq_out, len, tp->snd_max, tp); 7634 #endif 7635 } 7636 } 7637 7638 /* 7639 * Record one of the RTT updates from an ack into 7640 * our sample structure. 7641 */ 7642 7643 static void 7644 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt, 7645 int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt) 7646 { 7647 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 7648 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) { 7649 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt; 7650 } 7651 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 7652 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) { 7653 rack->r_ctl.rack_rs.rs_rtt_highest = rtt; 7654 } 7655 if (rack->rc_tp->t_flags & TF_GPUTINPROG) { 7656 if (us_rtt < rack->r_ctl.rc_gp_lowrtt) 7657 rack->r_ctl.rc_gp_lowrtt = us_rtt; 7658 if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd) 7659 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 7660 } 7661 if ((confidence == 1) && 7662 ((rsm == NULL) || 7663 (rsm->r_just_ret) || 7664 (rsm->r_one_out_nr && 7665 len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) { 7666 /* 7667 * If the rsm had a just return 7668 * hit it then we can't trust the 7669 * rtt measurement for buffer deterimination 7670 * Note that a confidence of 2, indicates 7671 * SACK'd which overrides the r_just_ret or 7672 * the r_one_out_nr. If it was a CUM-ACK and 7673 * we had only two outstanding, but get an 7674 * ack for only 1. Then that also lowers our 7675 * confidence. 7676 */ 7677 confidence = 0; 7678 } 7679 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 7680 (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) { 7681 if (rack->r_ctl.rack_rs.confidence == 0) { 7682 /* 7683 * We take anything with no current confidence 7684 * saved. 7685 */ 7686 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt; 7687 rack->r_ctl.rack_rs.confidence = confidence; 7688 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt; 7689 } else if (confidence || rack->r_ctl.rack_rs.confidence) { 7690 /* 7691 * Once we have a confident number, 7692 * we can update it with a smaller 7693 * value since this confident number 7694 * may include the DSACK time until 7695 * the next segment (the second one) arrived. 7696 */ 7697 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt; 7698 rack->r_ctl.rack_rs.confidence = confidence; 7699 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt; 7700 } 7701 } 7702 rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence); 7703 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID; 7704 rack->r_ctl.rack_rs.rs_rtt_tot += rtt; 7705 rack->r_ctl.rack_rs.rs_rtt_cnt++; 7706 } 7707 7708 /* 7709 * Collect new round-trip time estimate 7710 * and update averages and current timeout. 7711 */ 7712 static void 7713 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp) 7714 { 7715 int32_t delta; 7716 uint32_t o_srtt, o_var; 7717 int32_t hrtt_up = 0; 7718 int32_t rtt; 7719 7720 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) 7721 /* No valid sample */ 7722 return; 7723 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) { 7724 /* We are to use the lowest RTT seen in a single ack */ 7725 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest; 7726 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) { 7727 /* We are to use the highest RTT seen in a single ack */ 7728 rtt = rack->r_ctl.rack_rs.rs_rtt_highest; 7729 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) { 7730 /* We are to use the average RTT seen in a single ack */ 7731 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot / 7732 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt); 7733 } else { 7734 #ifdef INVARIANTS 7735 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method); 7736 #endif 7737 return; 7738 } 7739 if (rtt == 0) 7740 rtt = 1; 7741 if (rack->rc_gp_rtt_set == 0) { 7742 /* 7743 * With no RTT we have to accept 7744 * even one we are not confident of. 7745 */ 7746 rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt; 7747 rack->rc_gp_rtt_set = 1; 7748 } else if (rack->r_ctl.rack_rs.confidence) { 7749 /* update the running gp srtt */ 7750 rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8); 7751 rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8; 7752 } 7753 if (rack->r_ctl.rack_rs.confidence) { 7754 /* 7755 * record the low and high for highly buffered path computation, 7756 * we only do this if we are confident (not a retransmission). 7757 */ 7758 if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) { 7759 rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 7760 hrtt_up = 1; 7761 } 7762 if (rack->rc_highly_buffered == 0) { 7763 /* 7764 * Currently once we declare a path has 7765 * highly buffered there is no going 7766 * back, which may be a problem... 7767 */ 7768 if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) { 7769 rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt, 7770 rack->r_ctl.rc_highest_us_rtt, 7771 rack->r_ctl.rc_lowest_us_rtt, 7772 RACK_RTTS_SEEHBP); 7773 rack->rc_highly_buffered = 1; 7774 } 7775 } 7776 } 7777 if ((rack->r_ctl.rack_rs.confidence) || 7778 (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) { 7779 /* 7780 * If we are highly confident of it <or> it was 7781 * never retransmitted we accept it as the last us_rtt. 7782 */ 7783 rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 7784 /* The lowest rtt can be set if its was not retransmited */ 7785 if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) { 7786 rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 7787 if (rack->r_ctl.rc_lowest_us_rtt == 0) 7788 rack->r_ctl.rc_lowest_us_rtt = 1; 7789 } 7790 } 7791 o_srtt = tp->t_srtt; 7792 o_var = tp->t_rttvar; 7793 rack = (struct tcp_rack *)tp->t_fb_ptr; 7794 if (tp->t_srtt != 0) { 7795 /* 7796 * We keep a simple srtt in microseconds, like our rtt 7797 * measurement. We don't need to do any tricks with shifting 7798 * etc. Instead we just add in 1/8th of the new measurement 7799 * and subtract out 1/8 of the old srtt. We do the same with 7800 * the variance after finding the absolute value of the 7801 * difference between this sample and the current srtt. 7802 */ 7803 delta = tp->t_srtt - rtt; 7804 /* Take off 1/8th of the current sRTT */ 7805 tp->t_srtt -= (tp->t_srtt >> 3); 7806 /* Add in 1/8th of the new RTT just measured */ 7807 tp->t_srtt += (rtt >> 3); 7808 if (tp->t_srtt <= 0) 7809 tp->t_srtt = 1; 7810 /* Now lets make the absolute value of the variance */ 7811 if (delta < 0) 7812 delta = -delta; 7813 /* Subtract out 1/8th */ 7814 tp->t_rttvar -= (tp->t_rttvar >> 3); 7815 /* Add in 1/8th of the new variance we just saw */ 7816 tp->t_rttvar += (delta >> 3); 7817 if (tp->t_rttvar <= 0) 7818 tp->t_rttvar = 1; 7819 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 7820 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 7821 } else { 7822 /* 7823 * No rtt measurement yet - use the unsmoothed rtt. Set the 7824 * variance to half the rtt (so our first retransmit happens 7825 * at 3*rtt). 7826 */ 7827 tp->t_srtt = rtt; 7828 tp->t_rttvar = rtt >> 1; 7829 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 7830 } 7831 rack->rc_srtt_measure_made = 1; 7832 KMOD_TCPSTAT_INC(tcps_rttupdated); 7833 tp->t_rttupdated++; 7834 #ifdef STATS 7835 if (rack_stats_gets_ms_rtt == 0) { 7836 /* Send in the microsecond rtt used for rxt timeout purposes */ 7837 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt)); 7838 } else if (rack_stats_gets_ms_rtt == 1) { 7839 /* Send in the millisecond rtt used for rxt timeout purposes */ 7840 int32_t ms_rtt; 7841 7842 /* Round up */ 7843 ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC; 7844 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt)); 7845 } else if (rack_stats_gets_ms_rtt == 2) { 7846 /* Send in the millisecond rtt has close to the path RTT as we can get */ 7847 int32_t ms_rtt; 7848 7849 /* Round up */ 7850 ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC; 7851 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt)); 7852 } else { 7853 /* Send in the microsecond rtt has close to the path RTT as we can get */ 7854 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt)); 7855 } 7856 7857 #endif 7858 /* 7859 * the retransmit should happen at rtt + 4 * rttvar. Because of the 7860 * way we do the smoothing, srtt and rttvar will each average +1/2 7861 * tick of bias. When we compute the retransmit timer, we want 1/2 7862 * tick of rounding and 1 extra tick because of +-1/2 tick 7863 * uncertainty in the firing of the timer. The bias will give us 7864 * exactly the 1.5 tick we need. But, because the bias is 7865 * statistical, we have to test that we don't drop below the minimum 7866 * feasible timer (which is 2 ticks). 7867 */ 7868 tp->t_rxtshift = 0; 7869 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 7870 max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop); 7871 rack_log_rtt_sample(rack, rtt); 7872 tp->t_softerror = 0; 7873 } 7874 7875 7876 static void 7877 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts) 7878 { 7879 /* 7880 * Apply to filter the inbound us-rtt at us_cts. 7881 */ 7882 uint32_t old_rtt; 7883 7884 old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 7885 apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt, 7886 us_rtt, us_cts); 7887 if (rack->r_ctl.last_pacing_time && 7888 rack->rc_gp_dyn_mul && 7889 (rack->r_ctl.last_pacing_time > us_rtt)) 7890 rack->pacing_longer_than_rtt = 1; 7891 else 7892 rack->pacing_longer_than_rtt = 0; 7893 if (old_rtt > us_rtt) { 7894 /* We just hit a new lower rtt time */ 7895 rack_log_rtt_shrinks(rack, us_cts, old_rtt, 7896 __LINE__, RACK_RTTS_NEWRTT); 7897 /* 7898 * Only count it if its lower than what we saw within our 7899 * calculated range. 7900 */ 7901 if ((old_rtt - us_rtt) > rack_min_rtt_movement) { 7902 if (rack_probertt_lower_within && 7903 rack->rc_gp_dyn_mul && 7904 (rack->use_fixed_rate == 0) && 7905 (rack->rc_always_pace)) { 7906 /* 7907 * We are seeing a new lower rtt very close 7908 * to the time that we would have entered probe-rtt. 7909 * This is probably due to the fact that a peer flow 7910 * has entered probe-rtt. Lets go in now too. 7911 */ 7912 uint32_t val; 7913 7914 val = rack_probertt_lower_within * rack_time_between_probertt; 7915 val /= 100; 7916 if ((rack->in_probe_rtt == 0) && 7917 ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val))) { 7918 rack_enter_probertt(rack, us_cts); 7919 } 7920 } 7921 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 7922 } 7923 } 7924 } 7925 7926 static int 7927 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 7928 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack) 7929 { 7930 int32_t i, all; 7931 uint32_t t, len_acked; 7932 7933 if ((rsm->r_flags & RACK_ACKED) || 7934 (rsm->r_flags & RACK_WAS_ACKED)) 7935 /* Already done */ 7936 return (0); 7937 if (rsm->r_no_rtt_allowed) { 7938 /* Not allowed */ 7939 return (0); 7940 } 7941 if (ack_type == CUM_ACKED) { 7942 if (SEQ_GT(th_ack, rsm->r_end)) { 7943 len_acked = rsm->r_end - rsm->r_start; 7944 all = 1; 7945 } else { 7946 len_acked = th_ack - rsm->r_start; 7947 all = 0; 7948 } 7949 } else { 7950 len_acked = rsm->r_end - rsm->r_start; 7951 all = 0; 7952 } 7953 if (rsm->r_rtr_cnt == 1) { 7954 uint32_t us_rtt; 7955 7956 t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 7957 if ((int)t <= 0) 7958 t = 1; 7959 if (!tp->t_rttlow || tp->t_rttlow > t) 7960 tp->t_rttlow = t; 7961 if (!rack->r_ctl.rc_rack_min_rtt || 7962 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 7963 rack->r_ctl.rc_rack_min_rtt = t; 7964 if (rack->r_ctl.rc_rack_min_rtt == 0) { 7965 rack->r_ctl.rc_rack_min_rtt = 1; 7966 } 7967 } 7968 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) 7969 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 7970 else 7971 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 7972 if (us_rtt == 0) 7973 us_rtt = 1; 7974 rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time)); 7975 if (ack_type == SACKED) { 7976 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1); 7977 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt); 7978 } else { 7979 /* 7980 * We need to setup what our confidence 7981 * is in this ack. 7982 * 7983 * If the rsm was app limited and it is 7984 * less than a mss in length (the end 7985 * of the send) then we have a gap. If we 7986 * were app limited but say we were sending 7987 * multiple MSS's then we are more confident 7988 * int it. 7989 * 7990 * When we are not app-limited then we see if 7991 * the rsm is being included in the current 7992 * measurement, we tell this by the app_limited_needs_set 7993 * flag. 7994 * 7995 * Note that being cwnd blocked is not applimited 7996 * as well as the pacing delay between packets which 7997 * are sending only 1 or 2 MSS's also will show up 7998 * in the RTT. We probably need to examine this algorithm 7999 * a bit more and enhance it to account for the delay 8000 * between rsm's. We could do that by saving off the 8001 * pacing delay of each rsm (in an rsm) and then 8002 * factoring that in somehow though for now I am 8003 * not sure how :) 8004 */ 8005 int calc_conf = 0; 8006 8007 if (rsm->r_flags & RACK_APP_LIMITED) { 8008 if (all && (len_acked <= ctf_fixed_maxseg(tp))) 8009 calc_conf = 0; 8010 else 8011 calc_conf = 1; 8012 } else if (rack->app_limited_needs_set == 0) { 8013 calc_conf = 1; 8014 } else { 8015 calc_conf = 0; 8016 } 8017 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2); 8018 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 8019 calc_conf, rsm, rsm->r_rtr_cnt); 8020 } 8021 if ((rsm->r_flags & RACK_TLP) && 8022 (!IN_FASTRECOVERY(tp->t_flags))) { 8023 /* Segment was a TLP and our retrans matched */ 8024 if (rack->r_ctl.rc_tlp_cwnd_reduce) { 8025 rack->r_ctl.rc_rsm_start = tp->snd_max; 8026 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 8027 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 8028 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una); 8029 } 8030 } 8031 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) { 8032 /* New more recent rack_tmit_time */ 8033 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 8034 rack->rc_rack_rtt = t; 8035 } 8036 return (1); 8037 } 8038 /* 8039 * We clear the soft/rxtshift since we got an ack. 8040 * There is no assurance we will call the commit() function 8041 * so we need to clear these to avoid incorrect handling. 8042 */ 8043 tp->t_rxtshift = 0; 8044 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 8045 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 8046 tp->t_softerror = 0; 8047 if (to && (to->to_flags & TOF_TS) && 8048 (ack_type == CUM_ACKED) && 8049 (to->to_tsecr) && 8050 ((rsm->r_flags & RACK_OVERMAX) == 0)) { 8051 /* 8052 * Now which timestamp does it match? In this block the ACK 8053 * must be coming from a previous transmission. 8054 */ 8055 for (i = 0; i < rsm->r_rtr_cnt; i++) { 8056 if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) { 8057 t = cts - (uint32_t)rsm->r_tim_lastsent[i]; 8058 if ((int)t <= 0) 8059 t = 1; 8060 if ((i + 1) < rsm->r_rtr_cnt) { 8061 /* 8062 * The peer ack'd from our previous 8063 * transmission. We have a spurious 8064 * retransmission and thus we dont 8065 * want to update our rack_rtt. 8066 */ 8067 return (0); 8068 } 8069 if (!tp->t_rttlow || tp->t_rttlow > t) 8070 tp->t_rttlow = t; 8071 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 8072 rack->r_ctl.rc_rack_min_rtt = t; 8073 if (rack->r_ctl.rc_rack_min_rtt == 0) { 8074 rack->r_ctl.rc_rack_min_rtt = 1; 8075 } 8076 } 8077 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, 8078 (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)])) { 8079 /* New more recent rack_tmit_time */ 8080 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 8081 rack->rc_rack_rtt = t; 8082 } 8083 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3); 8084 tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm, 8085 rsm->r_rtr_cnt); 8086 return (1); 8087 } 8088 } 8089 goto ts_not_found; 8090 } else { 8091 /* 8092 * Ok its a SACK block that we retransmitted. or a windows 8093 * machine without timestamps. We can tell nothing from the 8094 * time-stamp since its not there or the time the peer last 8095 * recieved a segment that moved forward its cum-ack point. 8096 */ 8097 ts_not_found: 8098 i = rsm->r_rtr_cnt - 1; 8099 t = cts - (uint32_t)rsm->r_tim_lastsent[i]; 8100 if ((int)t <= 0) 8101 t = 1; 8102 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 8103 /* 8104 * We retransmitted and the ack came back in less 8105 * than the smallest rtt we have observed. We most 8106 * likely did an improper retransmit as outlined in 8107 * 6.2 Step 2 point 2 in the rack-draft so we 8108 * don't want to update our rack_rtt. We in 8109 * theory (in future) might want to think about reverting our 8110 * cwnd state but we won't for now. 8111 */ 8112 return (0); 8113 } else if (rack->r_ctl.rc_rack_min_rtt) { 8114 /* 8115 * We retransmitted it and the retransmit did the 8116 * job. 8117 */ 8118 if (!rack->r_ctl.rc_rack_min_rtt || 8119 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 8120 rack->r_ctl.rc_rack_min_rtt = t; 8121 if (rack->r_ctl.rc_rack_min_rtt == 0) { 8122 rack->r_ctl.rc_rack_min_rtt = 1; 8123 } 8124 } 8125 if (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, (uint32_t)rsm->r_tim_lastsent[i])) { 8126 /* New more recent rack_tmit_time */ 8127 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i]; 8128 rack->rc_rack_rtt = t; 8129 } 8130 return (1); 8131 } 8132 } 8133 return (0); 8134 } 8135 8136 /* 8137 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 8138 */ 8139 static void 8140 rack_log_sack_passed(struct tcpcb *tp, 8141 struct tcp_rack *rack, struct rack_sendmap *rsm) 8142 { 8143 struct rack_sendmap *nrsm; 8144 8145 nrsm = rsm; 8146 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap, 8147 rack_head, r_tnext) { 8148 if (nrsm == rsm) { 8149 /* Skip orginal segment he is acked */ 8150 continue; 8151 } 8152 if (nrsm->r_flags & RACK_ACKED) { 8153 /* 8154 * Skip ack'd segments, though we 8155 * should not see these, since tmap 8156 * should not have ack'd segments. 8157 */ 8158 continue; 8159 } 8160 if (nrsm->r_flags & RACK_SACK_PASSED) { 8161 /* 8162 * We found one that is already marked 8163 * passed, we have been here before and 8164 * so all others below this are marked. 8165 */ 8166 break; 8167 } 8168 nrsm->r_flags |= RACK_SACK_PASSED; 8169 nrsm->r_flags &= ~RACK_WAS_SACKPASS; 8170 } 8171 } 8172 8173 static void 8174 rack_need_set_test(struct tcpcb *tp, 8175 struct tcp_rack *rack, 8176 struct rack_sendmap *rsm, 8177 tcp_seq th_ack, 8178 int line, 8179 int use_which) 8180 { 8181 8182 if ((tp->t_flags & TF_GPUTINPROG) && 8183 SEQ_GEQ(rsm->r_end, tp->gput_seq)) { 8184 /* 8185 * We were app limited, and this ack 8186 * butts up or goes beyond the point where we want 8187 * to start our next measurement. We need 8188 * to record the new gput_ts as here and 8189 * possibly update the start sequence. 8190 */ 8191 uint32_t seq, ts; 8192 8193 if (rsm->r_rtr_cnt > 1) { 8194 /* 8195 * This is a retransmit, can we 8196 * really make any assessment at this 8197 * point? We are not really sure of 8198 * the timestamp, is it this or the 8199 * previous transmission? 8200 * 8201 * Lets wait for something better that 8202 * is not retransmitted. 8203 */ 8204 return; 8205 } 8206 seq = tp->gput_seq; 8207 ts = tp->gput_ts; 8208 rack->app_limited_needs_set = 0; 8209 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 8210 /* Do we start at a new end? */ 8211 if ((use_which == RACK_USE_BEG) && 8212 SEQ_GEQ(rsm->r_start, tp->gput_seq)) { 8213 /* 8214 * When we get an ACK that just eats 8215 * up some of the rsm, we set RACK_USE_BEG 8216 * since whats at r_start (i.e. th_ack) 8217 * is left unacked and thats where the 8218 * measurement not starts. 8219 */ 8220 tp->gput_seq = rsm->r_start; 8221 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 8222 } 8223 if ((use_which == RACK_USE_END) && 8224 SEQ_GEQ(rsm->r_end, tp->gput_seq)) { 8225 /* 8226 * We use the end when the cumack 8227 * is moving forward and completely 8228 * deleting the rsm passed so basically 8229 * r_end holds th_ack. 8230 * 8231 * For SACK's we also want to use the end 8232 * since this piece just got sacked and 8233 * we want to target anything after that 8234 * in our measurement. 8235 */ 8236 tp->gput_seq = rsm->r_end; 8237 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 8238 } 8239 if (use_which == RACK_USE_END_OR_THACK) { 8240 /* 8241 * special case for ack moving forward, 8242 * not a sack, we need to move all the 8243 * way up to where this ack cum-ack moves 8244 * to. 8245 */ 8246 if (SEQ_GT(th_ack, rsm->r_end)) 8247 tp->gput_seq = th_ack; 8248 else 8249 tp->gput_seq = rsm->r_end; 8250 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 8251 } 8252 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) { 8253 /* 8254 * We moved beyond this guy's range, re-calculate 8255 * the new end point. 8256 */ 8257 if (rack->rc_gp_filled == 0) { 8258 tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp))); 8259 } else { 8260 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 8261 } 8262 } 8263 /* 8264 * We are moving the goal post, we may be able to clear the 8265 * measure_saw_probe_rtt flag. 8266 */ 8267 if ((rack->in_probe_rtt == 0) && 8268 (rack->measure_saw_probe_rtt) && 8269 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 8270 rack->measure_saw_probe_rtt = 0; 8271 rack_log_pacing_delay_calc(rack, ts, tp->gput_ts, 8272 seq, tp->gput_seq, 0, 5, line, NULL, 0); 8273 if (rack->rc_gp_filled && 8274 ((tp->gput_ack - tp->gput_seq) < 8275 max(rc_init_window(rack), (MIN_GP_WIN * 8276 ctf_fixed_maxseg(tp))))) { 8277 uint32_t ideal_amount; 8278 8279 ideal_amount = rack_get_measure_window(tp, rack); 8280 if (ideal_amount > sbavail(&tp->t_inpcb->inp_socket->so_snd)) { 8281 /* 8282 * There is no sense of continuing this measurement 8283 * because its too small to gain us anything we 8284 * trust. Skip it and that way we can start a new 8285 * measurement quicker. 8286 */ 8287 tp->t_flags &= ~TF_GPUTINPROG; 8288 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq, 8289 0, 0, 0, 6, __LINE__, NULL, 0); 8290 } else { 8291 /* 8292 * Reset the window further out. 8293 */ 8294 tp->gput_ack = tp->gput_seq + ideal_amount; 8295 } 8296 } 8297 } 8298 } 8299 8300 static inline int 8301 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm) 8302 { 8303 if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) { 8304 /* Behind our TLP definition or right at */ 8305 return (0); 8306 } 8307 if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) { 8308 /* The start is beyond or right at our end of TLP definition */ 8309 return (0); 8310 } 8311 /* It has to be a sub-part of the original TLP recorded */ 8312 return (1); 8313 } 8314 8315 8316 static uint32_t 8317 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack, 8318 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, int *moved_two) 8319 { 8320 uint32_t start, end, changed = 0; 8321 struct rack_sendmap stack_map; 8322 struct rack_sendmap *rsm, *nrsm, fe, *insret, *prev, *next; 8323 int32_t used_ref = 1; 8324 int moved = 0; 8325 8326 start = sack->start; 8327 end = sack->end; 8328 rsm = *prsm; 8329 memset(&fe, 0, sizeof(fe)); 8330 do_rest_ofb: 8331 if ((rsm == NULL) || 8332 (SEQ_LT(end, rsm->r_start)) || 8333 (SEQ_GEQ(start, rsm->r_end)) || 8334 (SEQ_LT(start, rsm->r_start))) { 8335 /* 8336 * We are not in the right spot, 8337 * find the correct spot in the tree. 8338 */ 8339 used_ref = 0; 8340 fe.r_start = start; 8341 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 8342 moved++; 8343 } 8344 if (rsm == NULL) { 8345 /* TSNH */ 8346 goto out; 8347 } 8348 /* Ok we have an ACK for some piece of this rsm */ 8349 if (rsm->r_start != start) { 8350 if ((rsm->r_flags & RACK_ACKED) == 0) { 8351 /* 8352 * Before any splitting or hookery is 8353 * done is it a TLP of interest i.e. rxt? 8354 */ 8355 if ((rsm->r_flags & RACK_TLP) && 8356 (rsm->r_rtr_cnt > 1)) { 8357 /* 8358 * We are splitting a rxt TLP, check 8359 * if we need to save off the start/end 8360 */ 8361 if (rack->rc_last_tlp_acked_set && 8362 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 8363 /* 8364 * We already turned this on since we are inside 8365 * the previous one was a partially sack now we 8366 * are getting another one (maybe all of it). 8367 * 8368 */ 8369 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 8370 /* 8371 * Lets make sure we have all of it though. 8372 */ 8373 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 8374 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8375 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8376 rack->r_ctl.last_tlp_acked_end); 8377 } 8378 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 8379 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8380 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8381 rack->r_ctl.last_tlp_acked_end); 8382 } 8383 } else { 8384 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8385 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8386 rack->rc_last_tlp_past_cumack = 0; 8387 rack->rc_last_tlp_acked_set = 1; 8388 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 8389 } 8390 } 8391 /** 8392 * Need to split this in two pieces the before and after, 8393 * the before remains in the map, the after must be 8394 * added. In other words we have: 8395 * rsm |--------------| 8396 * sackblk |-------> 8397 * rsm will become 8398 * rsm |---| 8399 * and nrsm will be the sacked piece 8400 * nrsm |----------| 8401 * 8402 * But before we start down that path lets 8403 * see if the sack spans over on top of 8404 * the next guy and it is already sacked. 8405 * 8406 */ 8407 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8408 if (next && (next->r_flags & RACK_ACKED) && 8409 SEQ_GEQ(end, next->r_start)) { 8410 /** 8411 * So the next one is already acked, and 8412 * we can thus by hookery use our stack_map 8413 * to reflect the piece being sacked and 8414 * then adjust the two tree entries moving 8415 * the start and ends around. So we start like: 8416 * rsm |------------| (not-acked) 8417 * next |-----------| (acked) 8418 * sackblk |--------> 8419 * We want to end like so: 8420 * rsm |------| (not-acked) 8421 * next |-----------------| (acked) 8422 * nrsm |-----| 8423 * Where nrsm is a temporary stack piece we 8424 * use to update all the gizmos. 8425 */ 8426 /* Copy up our fudge block */ 8427 nrsm = &stack_map; 8428 memcpy(nrsm, rsm, sizeof(struct rack_sendmap)); 8429 /* Now adjust our tree blocks */ 8430 rsm->r_end = start; 8431 next->r_start = start; 8432 /* Now we must adjust back where next->m is */ 8433 rack_setup_offset_for_rsm(rsm, next); 8434 8435 /* We don't need to adjust rsm, it did not change */ 8436 /* Clear out the dup ack count of the remainder */ 8437 rsm->r_dupack = 0; 8438 rsm->r_just_ret = 0; 8439 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 8440 /* Now lets make sure our fudge block is right */ 8441 nrsm->r_start = start; 8442 /* Now lets update all the stats and such */ 8443 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0); 8444 if (rack->app_limited_needs_set) 8445 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END); 8446 changed += (nrsm->r_end - nrsm->r_start); 8447 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start); 8448 if (nrsm->r_flags & RACK_SACK_PASSED) { 8449 counter_u64_add(rack_reorder_seen, 1); 8450 rack->r_ctl.rc_reorder_ts = cts; 8451 } 8452 /* 8453 * Now we want to go up from rsm (the 8454 * one left un-acked) to the next one 8455 * in the tmap. We do this so when 8456 * we walk backwards we include marking 8457 * sack-passed on rsm (The one passed in 8458 * is skipped since it is generally called 8459 * on something sacked before removing it 8460 * from the tmap). 8461 */ 8462 if (rsm->r_in_tmap) { 8463 nrsm = TAILQ_NEXT(rsm, r_tnext); 8464 /* 8465 * Now that we have the next 8466 * one walk backwards from there. 8467 */ 8468 if (nrsm && nrsm->r_in_tmap) 8469 rack_log_sack_passed(tp, rack, nrsm); 8470 } 8471 /* Now are we done? */ 8472 if (SEQ_LT(end, next->r_end) || 8473 (end == next->r_end)) { 8474 /* Done with block */ 8475 goto out; 8476 } 8477 rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__); 8478 counter_u64_add(rack_sack_used_next_merge, 1); 8479 /* Postion for the next block */ 8480 start = next->r_end; 8481 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, next); 8482 if (rsm == NULL) 8483 goto out; 8484 } else { 8485 /** 8486 * We can't use any hookery here, so we 8487 * need to split the map. We enter like 8488 * so: 8489 * rsm |--------| 8490 * sackblk |-----> 8491 * We will add the new block nrsm and 8492 * that will be the new portion, and then 8493 * fall through after reseting rsm. So we 8494 * split and look like this: 8495 * rsm |----| 8496 * sackblk |-----> 8497 * nrsm |---| 8498 * We then fall through reseting 8499 * rsm to nrsm, so the next block 8500 * picks it up. 8501 */ 8502 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 8503 if (nrsm == NULL) { 8504 /* 8505 * failed XXXrrs what can we do but loose the sack 8506 * info? 8507 */ 8508 goto out; 8509 } 8510 counter_u64_add(rack_sack_splits, 1); 8511 rack_clone_rsm(rack, nrsm, rsm, start); 8512 rsm->r_just_ret = 0; 8513 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 8514 #ifdef INVARIANTS 8515 if (insret != NULL) { 8516 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 8517 nrsm, insret, rack, rsm); 8518 } 8519 #endif 8520 if (rsm->r_in_tmap) { 8521 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8522 nrsm->r_in_tmap = 1; 8523 } 8524 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__); 8525 rsm->r_flags &= (~RACK_HAS_FIN); 8526 /* Position us to point to the new nrsm that starts the sack blk */ 8527 rsm = nrsm; 8528 } 8529 } else { 8530 /* Already sacked this piece */ 8531 counter_u64_add(rack_sack_skipped_acked, 1); 8532 moved++; 8533 if (end == rsm->r_end) { 8534 /* Done with block */ 8535 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8536 goto out; 8537 } else if (SEQ_LT(end, rsm->r_end)) { 8538 /* A partial sack to a already sacked block */ 8539 moved++; 8540 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8541 goto out; 8542 } else { 8543 /* 8544 * The end goes beyond this guy 8545 * repostion the start to the 8546 * next block. 8547 */ 8548 start = rsm->r_end; 8549 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8550 if (rsm == NULL) 8551 goto out; 8552 } 8553 } 8554 } 8555 if (SEQ_GEQ(end, rsm->r_end)) { 8556 /** 8557 * The end of this block is either beyond this guy or right 8558 * at this guy. I.e.: 8559 * rsm --- |-----| 8560 * end |-----| 8561 * <or> 8562 * end |---------| 8563 */ 8564 if ((rsm->r_flags & RACK_ACKED) == 0) { 8565 /* 8566 * Is it a TLP of interest? 8567 */ 8568 if ((rsm->r_flags & RACK_TLP) && 8569 (rsm->r_rtr_cnt > 1)) { 8570 /* 8571 * We are splitting a rxt TLP, check 8572 * if we need to save off the start/end 8573 */ 8574 if (rack->rc_last_tlp_acked_set && 8575 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 8576 /* 8577 * We already turned this on since we are inside 8578 * the previous one was a partially sack now we 8579 * are getting another one (maybe all of it). 8580 */ 8581 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 8582 /* 8583 * Lets make sure we have all of it though. 8584 */ 8585 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 8586 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8587 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8588 rack->r_ctl.last_tlp_acked_end); 8589 } 8590 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 8591 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8592 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8593 rack->r_ctl.last_tlp_acked_end); 8594 } 8595 } else { 8596 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8597 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8598 rack->rc_last_tlp_past_cumack = 0; 8599 rack->rc_last_tlp_acked_set = 1; 8600 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 8601 } 8602 } 8603 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0); 8604 changed += (rsm->r_end - rsm->r_start); 8605 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 8606 if (rsm->r_in_tmap) /* should be true */ 8607 rack_log_sack_passed(tp, rack, rsm); 8608 /* Is Reordering occuring? */ 8609 if (rsm->r_flags & RACK_SACK_PASSED) { 8610 rsm->r_flags &= ~RACK_SACK_PASSED; 8611 counter_u64_add(rack_reorder_seen, 1); 8612 rack->r_ctl.rc_reorder_ts = cts; 8613 } 8614 if (rack->app_limited_needs_set) 8615 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END); 8616 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 8617 rsm->r_flags |= RACK_ACKED; 8618 if (rsm->r_in_tmap) { 8619 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8620 rsm->r_in_tmap = 0; 8621 } 8622 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__); 8623 } else { 8624 counter_u64_add(rack_sack_skipped_acked, 1); 8625 moved++; 8626 } 8627 if (end == rsm->r_end) { 8628 /* This block only - done, setup for next */ 8629 goto out; 8630 } 8631 /* 8632 * There is more not coverend by this rsm move on 8633 * to the next block in the RB tree. 8634 */ 8635 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8636 start = rsm->r_end; 8637 rsm = nrsm; 8638 if (rsm == NULL) 8639 goto out; 8640 goto do_rest_ofb; 8641 } 8642 /** 8643 * The end of this sack block is smaller than 8644 * our rsm i.e.: 8645 * rsm --- |-----| 8646 * end |--| 8647 */ 8648 if ((rsm->r_flags & RACK_ACKED) == 0) { 8649 /* 8650 * Is it a TLP of interest? 8651 */ 8652 if ((rsm->r_flags & RACK_TLP) && 8653 (rsm->r_rtr_cnt > 1)) { 8654 /* 8655 * We are splitting a rxt TLP, check 8656 * if we need to save off the start/end 8657 */ 8658 if (rack->rc_last_tlp_acked_set && 8659 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 8660 /* 8661 * We already turned this on since we are inside 8662 * the previous one was a partially sack now we 8663 * are getting another one (maybe all of it). 8664 */ 8665 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 8666 /* 8667 * Lets make sure we have all of it though. 8668 */ 8669 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 8670 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8671 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8672 rack->r_ctl.last_tlp_acked_end); 8673 } 8674 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 8675 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8676 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8677 rack->r_ctl.last_tlp_acked_end); 8678 } 8679 } else { 8680 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8681 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8682 rack->rc_last_tlp_past_cumack = 0; 8683 rack->rc_last_tlp_acked_set = 1; 8684 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 8685 } 8686 } 8687 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8688 if (prev && 8689 (prev->r_flags & RACK_ACKED)) { 8690 /** 8691 * Goal, we want the right remainder of rsm to shrink 8692 * in place and span from (rsm->r_start = end) to rsm->r_end. 8693 * We want to expand prev to go all the way 8694 * to prev->r_end <- end. 8695 * so in the tree we have before: 8696 * prev |--------| (acked) 8697 * rsm |-------| (non-acked) 8698 * sackblk |-| 8699 * We churn it so we end up with 8700 * prev |----------| (acked) 8701 * rsm |-----| (non-acked) 8702 * nrsm |-| (temporary) 8703 * 8704 * Note if either prev/rsm is a TLP we don't 8705 * do this. 8706 */ 8707 nrsm = &stack_map; 8708 memcpy(nrsm, rsm, sizeof(struct rack_sendmap)); 8709 prev->r_end = end; 8710 rsm->r_start = end; 8711 /* Now adjust nrsm (stack copy) to be 8712 * the one that is the small 8713 * piece that was "sacked". 8714 */ 8715 nrsm->r_end = end; 8716 rsm->r_dupack = 0; 8717 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 8718 /* 8719 * Now that the rsm has had its start moved forward 8720 * lets go ahead and get its new place in the world. 8721 */ 8722 rack_setup_offset_for_rsm(prev, rsm); 8723 /* 8724 * Now nrsm is our new little piece 8725 * that is acked (which was merged 8726 * to prev). Update the rtt and changed 8727 * based on that. Also check for reordering. 8728 */ 8729 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0); 8730 if (rack->app_limited_needs_set) 8731 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END); 8732 changed += (nrsm->r_end - nrsm->r_start); 8733 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start); 8734 if (nrsm->r_flags & RACK_SACK_PASSED) { 8735 counter_u64_add(rack_reorder_seen, 1); 8736 rack->r_ctl.rc_reorder_ts = cts; 8737 } 8738 rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__); 8739 rsm = prev; 8740 counter_u64_add(rack_sack_used_prev_merge, 1); 8741 } else { 8742 /** 8743 * This is the case where our previous 8744 * block is not acked either, so we must 8745 * split the block in two. 8746 */ 8747 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 8748 if (nrsm == NULL) { 8749 /* failed rrs what can we do but loose the sack info? */ 8750 goto out; 8751 } 8752 if ((rsm->r_flags & RACK_TLP) && 8753 (rsm->r_rtr_cnt > 1)) { 8754 /* 8755 * We are splitting a rxt TLP, check 8756 * if we need to save off the start/end 8757 */ 8758 if (rack->rc_last_tlp_acked_set && 8759 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 8760 /* 8761 * We already turned this on since this block is inside 8762 * the previous one was a partially sack now we 8763 * are getting another one (maybe all of it). 8764 */ 8765 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 8766 /* 8767 * Lets make sure we have all of it though. 8768 */ 8769 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 8770 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8771 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8772 rack->r_ctl.last_tlp_acked_end); 8773 } 8774 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 8775 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8776 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 8777 rack->r_ctl.last_tlp_acked_end); 8778 } 8779 } else { 8780 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 8781 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 8782 rack->rc_last_tlp_acked_set = 1; 8783 rack->rc_last_tlp_past_cumack = 0; 8784 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 8785 } 8786 } 8787 /** 8788 * In this case nrsm becomes 8789 * nrsm->r_start = end; 8790 * nrsm->r_end = rsm->r_end; 8791 * which is un-acked. 8792 * <and> 8793 * rsm->r_end = nrsm->r_start; 8794 * i.e. the remaining un-acked 8795 * piece is left on the left 8796 * hand side. 8797 * 8798 * So we start like this 8799 * rsm |----------| (not acked) 8800 * sackblk |---| 8801 * build it so we have 8802 * rsm |---| (acked) 8803 * nrsm |------| (not acked) 8804 */ 8805 counter_u64_add(rack_sack_splits, 1); 8806 rack_clone_rsm(rack, nrsm, rsm, end); 8807 rsm->r_flags &= (~RACK_HAS_FIN); 8808 rsm->r_just_ret = 0; 8809 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 8810 #ifdef INVARIANTS 8811 if (insret != NULL) { 8812 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 8813 nrsm, insret, rack, rsm); 8814 } 8815 #endif 8816 if (rsm->r_in_tmap) { 8817 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8818 nrsm->r_in_tmap = 1; 8819 } 8820 nrsm->r_dupack = 0; 8821 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2); 8822 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0); 8823 changed += (rsm->r_end - rsm->r_start); 8824 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 8825 if (rsm->r_in_tmap) /* should be true */ 8826 rack_log_sack_passed(tp, rack, rsm); 8827 /* Is Reordering occuring? */ 8828 if (rsm->r_flags & RACK_SACK_PASSED) { 8829 rsm->r_flags &= ~RACK_SACK_PASSED; 8830 counter_u64_add(rack_reorder_seen, 1); 8831 rack->r_ctl.rc_reorder_ts = cts; 8832 } 8833 if (rack->app_limited_needs_set) 8834 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END); 8835 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 8836 rsm->r_flags |= RACK_ACKED; 8837 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__); 8838 if (rsm->r_in_tmap) { 8839 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8840 rsm->r_in_tmap = 0; 8841 } 8842 } 8843 } else if (start != end){ 8844 /* 8845 * The block was already acked. 8846 */ 8847 counter_u64_add(rack_sack_skipped_acked, 1); 8848 moved++; 8849 } 8850 out: 8851 if (rsm && 8852 ((rsm->r_flags & RACK_TLP) == 0) && 8853 (rsm->r_flags & RACK_ACKED)) { 8854 /* 8855 * Now can we merge where we worked 8856 * with either the previous or 8857 * next block? 8858 */ 8859 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8860 while (next) { 8861 if (next->r_flags & RACK_TLP) 8862 break; 8863 if (next->r_flags & RACK_ACKED) { 8864 /* yep this and next can be merged */ 8865 rsm = rack_merge_rsm(rack, rsm, next); 8866 next = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8867 } else 8868 break; 8869 } 8870 /* Now what about the previous? */ 8871 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8872 while (prev) { 8873 if (prev->r_flags & RACK_TLP) 8874 break; 8875 if (prev->r_flags & RACK_ACKED) { 8876 /* yep the previous and this can be merged */ 8877 rsm = rack_merge_rsm(rack, prev, rsm); 8878 prev = RB_PREV(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8879 } else 8880 break; 8881 } 8882 } 8883 if (used_ref == 0) { 8884 counter_u64_add(rack_sack_proc_all, 1); 8885 } else { 8886 counter_u64_add(rack_sack_proc_short, 1); 8887 } 8888 /* Save off the next one for quick reference. */ 8889 if (rsm) 8890 nrsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8891 else 8892 nrsm = NULL; 8893 *prsm = rack->r_ctl.rc_sacklast = nrsm; 8894 /* Pass back the moved. */ 8895 *moved_two = moved; 8896 return (changed); 8897 } 8898 8899 static void inline 8900 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack) 8901 { 8902 struct rack_sendmap *tmap; 8903 8904 tmap = NULL; 8905 while (rsm && (rsm->r_flags & RACK_ACKED)) { 8906 /* Its no longer sacked, mark it so */ 8907 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 8908 #ifdef INVARIANTS 8909 if (rsm->r_in_tmap) { 8910 panic("rack:%p rsm:%p flags:0x%x in tmap?", 8911 rack, rsm, rsm->r_flags); 8912 } 8913 #endif 8914 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS); 8915 /* Rebuild it into our tmap */ 8916 if (tmap == NULL) { 8917 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8918 tmap = rsm; 8919 } else { 8920 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext); 8921 tmap = rsm; 8922 } 8923 tmap->r_in_tmap = 1; 8924 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 8925 } 8926 /* 8927 * Now lets possibly clear the sack filter so we start 8928 * recognizing sacks that cover this area. 8929 */ 8930 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack); 8931 8932 } 8933 8934 static void 8935 rack_do_decay(struct tcp_rack *rack) 8936 { 8937 struct timeval res; 8938 8939 #define timersub(tvp, uvp, vvp) \ 8940 do { \ 8941 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 8942 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 8943 if ((vvp)->tv_usec < 0) { \ 8944 (vvp)->tv_sec--; \ 8945 (vvp)->tv_usec += 1000000; \ 8946 } \ 8947 } while (0) 8948 8949 timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res); 8950 #undef timersub 8951 8952 rack->r_ctl.input_pkt++; 8953 if ((rack->rc_in_persist) || 8954 (res.tv_sec >= 1) || 8955 (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) { 8956 /* 8957 * Check for decay of non-SAD, 8958 * we want all SAD detection metrics to 8959 * decay 1/4 per second (or more) passed. 8960 */ 8961 uint32_t pkt_delta; 8962 8963 pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt; 8964 /* Update our saved tracking values */ 8965 rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt; 8966 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time; 8967 /* Now do we escape without decay? */ 8968 #ifdef NETFLIX_EXP_DETECTION 8969 if (rack->rc_in_persist || 8970 (rack->rc_tp->snd_max == rack->rc_tp->snd_una) || 8971 (pkt_delta < tcp_sad_low_pps)){ 8972 /* 8973 * We don't decay idle connections 8974 * or ones that have a low input pps. 8975 */ 8976 return; 8977 } 8978 /* Decay the counters */ 8979 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count, 8980 tcp_sad_decay_val); 8981 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count, 8982 tcp_sad_decay_val); 8983 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra, 8984 tcp_sad_decay_val); 8985 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move, 8986 tcp_sad_decay_val); 8987 #endif 8988 } 8989 } 8990 8991 static void 8992 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to) 8993 { 8994 struct rack_sendmap *rsm, *rm; 8995 8996 /* 8997 * The ACK point is advancing to th_ack, we must drop off 8998 * the packets in the rack log and calculate any eligble 8999 * RTT's. 9000 */ 9001 rack->r_wanted_output = 1; 9002 9003 /* Tend any TLP that has been marked for 1/2 the seq space (its old) */ 9004 if ((rack->rc_last_tlp_acked_set == 1)&& 9005 (rack->rc_last_tlp_past_cumack == 1) && 9006 (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) { 9007 /* 9008 * We have reached the point where our last rack 9009 * tlp retransmit sequence is ahead of the cum-ack. 9010 * This can only happen when the cum-ack moves all 9011 * the way around (its been a full 2^^31+1 bytes 9012 * or more since we sent a retransmitted TLP). Lets 9013 * turn off the valid flag since its not really valid. 9014 * 9015 * Note since sack's also turn on this event we have 9016 * a complication, we have to wait to age it out until 9017 * the cum-ack is by the TLP before checking which is 9018 * what the next else clause does. 9019 */ 9020 rack_log_dsack_event(rack, 9, __LINE__, 9021 rack->r_ctl.last_tlp_acked_start, 9022 rack->r_ctl.last_tlp_acked_end); 9023 rack->rc_last_tlp_acked_set = 0; 9024 rack->rc_last_tlp_past_cumack = 0; 9025 } else if ((rack->rc_last_tlp_acked_set == 1) && 9026 (rack->rc_last_tlp_past_cumack == 0) && 9027 (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) { 9028 /* 9029 * It is safe to start aging TLP's out. 9030 */ 9031 rack->rc_last_tlp_past_cumack = 1; 9032 } 9033 /* We do the same for the tlp send seq as well */ 9034 if ((rack->rc_last_sent_tlp_seq_valid == 1) && 9035 (rack->rc_last_sent_tlp_past_cumack == 1) && 9036 (SEQ_GT(rack->r_ctl.last_sent_tlp_seq, th_ack))) { 9037 rack_log_dsack_event(rack, 9, __LINE__, 9038 rack->r_ctl.last_sent_tlp_seq, 9039 (rack->r_ctl.last_sent_tlp_seq + 9040 rack->r_ctl.last_sent_tlp_len)); 9041 rack->rc_last_sent_tlp_seq_valid = 0; 9042 rack->rc_last_sent_tlp_past_cumack = 0; 9043 } else if ((rack->rc_last_sent_tlp_seq_valid == 1) && 9044 (rack->rc_last_sent_tlp_past_cumack == 0) && 9045 (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) { 9046 /* 9047 * It is safe to start aging TLP's send. 9048 */ 9049 rack->rc_last_sent_tlp_past_cumack = 1; 9050 } 9051 more: 9052 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 9053 if (rsm == NULL) { 9054 if ((th_ack - 1) == tp->iss) { 9055 /* 9056 * For the SYN incoming case we will not 9057 * have called tcp_output for the sending of 9058 * the SYN, so there will be no map. All 9059 * other cases should probably be a panic. 9060 */ 9061 return; 9062 } 9063 if (tp->t_flags & TF_SENTFIN) { 9064 /* if we sent a FIN we often will not have map */ 9065 return; 9066 } 9067 #ifdef INVARIANTS 9068 panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u snd_nxt:%u\n", 9069 tp, 9070 tp->t_state, th_ack, rack, 9071 tp->snd_una, tp->snd_max, tp->snd_nxt); 9072 #endif 9073 return; 9074 } 9075 if (SEQ_LT(th_ack, rsm->r_start)) { 9076 /* Huh map is missing this */ 9077 #ifdef INVARIANTS 9078 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n", 9079 rsm->r_start, 9080 th_ack, tp->t_state, rack->r_state); 9081 #endif 9082 return; 9083 } 9084 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack); 9085 9086 /* Now was it a retransmitted TLP? */ 9087 if ((rsm->r_flags & RACK_TLP) && 9088 (rsm->r_rtr_cnt > 1)) { 9089 /* 9090 * Yes, this rsm was a TLP and retransmitted, remember that 9091 * since if a DSACK comes back on this we don't want 9092 * to think of it as a reordered segment. This may 9093 * get updated again with possibly even other TLPs 9094 * in flight, but thats ok. Only when we don't send 9095 * a retransmitted TLP for 1/2 the sequences space 9096 * will it get turned off (above). 9097 */ 9098 if (rack->rc_last_tlp_acked_set && 9099 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 9100 /* 9101 * We already turned this on since the end matches, 9102 * the previous one was a partially ack now we 9103 * are getting another one (maybe all of it). 9104 */ 9105 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 9106 /* 9107 * Lets make sure we have all of it though. 9108 */ 9109 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 9110 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 9111 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 9112 rack->r_ctl.last_tlp_acked_end); 9113 } 9114 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 9115 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 9116 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 9117 rack->r_ctl.last_tlp_acked_end); 9118 } 9119 } else { 9120 rack->rc_last_tlp_past_cumack = 1; 9121 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 9122 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 9123 rack->rc_last_tlp_acked_set = 1; 9124 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 9125 } 9126 } 9127 /* Now do we consume the whole thing? */ 9128 if (SEQ_GEQ(th_ack, rsm->r_end)) { 9129 /* Its all consumed. */ 9130 uint32_t left; 9131 uint8_t newly_acked; 9132 9133 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__); 9134 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 9135 rsm->r_rtr_bytes = 0; 9136 /* Record the time of highest cumack sent */ 9137 rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 9138 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 9139 #ifdef INVARIANTS 9140 if (rm != rsm) { 9141 panic("removing head in rack:%p rsm:%p rm:%p", 9142 rack, rsm, rm); 9143 } 9144 #endif 9145 if (rsm->r_in_tmap) { 9146 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 9147 rsm->r_in_tmap = 0; 9148 } 9149 newly_acked = 1; 9150 if (rsm->r_flags & RACK_ACKED) { 9151 /* 9152 * It was acked on the scoreboard -- remove 9153 * it from total 9154 */ 9155 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 9156 newly_acked = 0; 9157 } else if (rsm->r_flags & RACK_SACK_PASSED) { 9158 /* 9159 * There are segments ACKED on the 9160 * scoreboard further up. We are seeing 9161 * reordering. 9162 */ 9163 rsm->r_flags &= ~RACK_SACK_PASSED; 9164 counter_u64_add(rack_reorder_seen, 1); 9165 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 9166 rsm->r_flags |= RACK_ACKED; 9167 rack->r_ctl.rc_reorder_ts = cts; 9168 if (rack->r_ent_rec_ns) { 9169 /* 9170 * We have sent no more, and we saw an sack 9171 * then ack arrive. 9172 */ 9173 rack->r_might_revert = 1; 9174 } 9175 } 9176 if ((rsm->r_flags & RACK_TO_REXT) && 9177 (tp->t_flags & TF_RCVD_TSTMP) && 9178 (to->to_flags & TOF_TS) && 9179 (tp->t_flags & TF_PREVVALID)) { 9180 /* 9181 * We can use the timestamp to see 9182 * if this retransmission was from the 9183 * first transmit. If so we made a mistake. 9184 */ 9185 tp->t_flags &= ~TF_PREVVALID; 9186 if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) { 9187 /* The first transmit is what this ack is for */ 9188 rack_cong_signal(tp, CC_RTO_ERR, th_ack); 9189 } 9190 } 9191 left = th_ack - rsm->r_end; 9192 if (rack->app_limited_needs_set && newly_acked) 9193 rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK); 9194 /* Free back to zone */ 9195 rack_free(rack, rsm); 9196 if (left) { 9197 goto more; 9198 } 9199 /* Check for reneging */ 9200 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 9201 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) { 9202 /* 9203 * The peer has moved snd_una up to 9204 * the edge of this send, i.e. one 9205 * that it had previously acked. The only 9206 * way that can be true if the peer threw 9207 * away data (space issues) that it had 9208 * previously sacked (else it would have 9209 * given us snd_una up to (rsm->r_end). 9210 * We need to undo the acked markings here. 9211 * 9212 * Note we have to look to make sure th_ack is 9213 * our rsm->r_start in case we get an old ack 9214 * where th_ack is behind snd_una. 9215 */ 9216 rack_peer_reneges(rack, rsm, th_ack); 9217 } 9218 return; 9219 } 9220 if (rsm->r_flags & RACK_ACKED) { 9221 /* 9222 * It was acked on the scoreboard -- remove it from 9223 * total for the part being cum-acked. 9224 */ 9225 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 9226 } 9227 /* 9228 * Clear the dup ack count for 9229 * the piece that remains. 9230 */ 9231 rsm->r_dupack = 0; 9232 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 9233 if (rsm->r_rtr_bytes) { 9234 /* 9235 * It was retransmitted adjust the 9236 * sack holes for what was acked. 9237 */ 9238 int ack_am; 9239 9240 ack_am = (th_ack - rsm->r_start); 9241 if (ack_am >= rsm->r_rtr_bytes) { 9242 rack->r_ctl.rc_holes_rxt -= ack_am; 9243 rsm->r_rtr_bytes -= ack_am; 9244 } 9245 } 9246 /* 9247 * Update where the piece starts and record 9248 * the time of send of highest cumack sent. 9249 */ 9250 rack->r_ctl.rc_gp_cumack_ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 9251 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__); 9252 /* Now we need to move our offset forward too */ 9253 if (rsm->m && (rsm->orig_m_len != rsm->m->m_len)) { 9254 /* Fix up the orig_m_len and possibly the mbuf offset */ 9255 rack_adjust_orig_mlen(rsm); 9256 } 9257 rsm->soff += (th_ack - rsm->r_start); 9258 rsm->r_start = th_ack; 9259 /* Now do we need to move the mbuf fwd too? */ 9260 if (rsm->m) { 9261 while (rsm->soff >= rsm->m->m_len) { 9262 rsm->soff -= rsm->m->m_len; 9263 rsm->m = rsm->m->m_next; 9264 KASSERT((rsm->m != NULL), 9265 (" nrsm:%p hit at soff:%u null m", 9266 rsm, rsm->soff)); 9267 } 9268 rsm->orig_m_len = rsm->m->m_len; 9269 } 9270 if (rack->app_limited_needs_set) 9271 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG); 9272 } 9273 9274 static void 9275 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack) 9276 { 9277 struct rack_sendmap *rsm; 9278 int sack_pass_fnd = 0; 9279 9280 if (rack->r_might_revert) { 9281 /* 9282 * Ok we have reordering, have not sent anything, we 9283 * might want to revert the congestion state if nothing 9284 * further has SACK_PASSED on it. Lets check. 9285 * 9286 * We also get here when we have DSACKs come in for 9287 * all the data that we FR'd. Note that a rxt or tlp 9288 * timer clears this from happening. 9289 */ 9290 9291 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 9292 if (rsm->r_flags & RACK_SACK_PASSED) { 9293 sack_pass_fnd = 1; 9294 break; 9295 } 9296 } 9297 if (sack_pass_fnd == 0) { 9298 /* 9299 * We went into recovery 9300 * incorrectly due to reordering! 9301 */ 9302 int orig_cwnd; 9303 9304 rack->r_ent_rec_ns = 0; 9305 orig_cwnd = tp->snd_cwnd; 9306 tp->snd_cwnd = rack->r_ctl.rc_cwnd_at_erec; 9307 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec; 9308 tp->snd_recover = tp->snd_una; 9309 rack_log_to_prr(rack, 14, orig_cwnd); 9310 EXIT_RECOVERY(tp->t_flags); 9311 } 9312 rack->r_might_revert = 0; 9313 } 9314 } 9315 9316 #ifdef NETFLIX_EXP_DETECTION 9317 static void 9318 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack, uint32_t bytes_this_ack, uint32_t segsiz) 9319 { 9320 if ((rack->do_detection || tcp_force_detection) && 9321 tcp_sack_to_ack_thresh && 9322 tcp_sack_to_move_thresh && 9323 ((rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum) || rack->sack_attack_disable)) { 9324 /* 9325 * We have thresholds set to find 9326 * possible attackers and disable sack. 9327 * Check them. 9328 */ 9329 uint64_t ackratio, moveratio, movetotal; 9330 9331 /* Log detecting */ 9332 rack_log_sad(rack, 1); 9333 ackratio = (uint64_t)(rack->r_ctl.sack_count); 9334 ackratio *= (uint64_t)(1000); 9335 if (rack->r_ctl.ack_count) 9336 ackratio /= (uint64_t)(rack->r_ctl.ack_count); 9337 else { 9338 /* We really should not hit here */ 9339 ackratio = 1000; 9340 } 9341 if ((rack->sack_attack_disable == 0) && 9342 (ackratio > rack_highest_sack_thresh_seen)) 9343 rack_highest_sack_thresh_seen = (uint32_t)ackratio; 9344 movetotal = rack->r_ctl.sack_moved_extra; 9345 movetotal += rack->r_ctl.sack_noextra_move; 9346 moveratio = rack->r_ctl.sack_moved_extra; 9347 moveratio *= (uint64_t)1000; 9348 if (movetotal) 9349 moveratio /= movetotal; 9350 else { 9351 /* No moves, thats pretty good */ 9352 moveratio = 0; 9353 } 9354 if ((rack->sack_attack_disable == 0) && 9355 (moveratio > rack_highest_move_thresh_seen)) 9356 rack_highest_move_thresh_seen = (uint32_t)moveratio; 9357 if (rack->sack_attack_disable == 0) { 9358 if ((ackratio > tcp_sack_to_ack_thresh) && 9359 (moveratio > tcp_sack_to_move_thresh)) { 9360 /* Disable sack processing */ 9361 rack->sack_attack_disable = 1; 9362 if (rack->r_rep_attack == 0) { 9363 rack->r_rep_attack = 1; 9364 counter_u64_add(rack_sack_attacks_detected, 1); 9365 } 9366 if (tcp_attack_on_turns_on_logging) { 9367 /* 9368 * Turn on logging, used for debugging 9369 * false positives. 9370 */ 9371 rack->rc_tp->t_logstate = tcp_attack_on_turns_on_logging; 9372 } 9373 /* Clamp the cwnd at flight size */ 9374 rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd; 9375 rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 9376 rack_log_sad(rack, 2); 9377 } 9378 } else { 9379 /* We are sack-disabled check for false positives */ 9380 if ((ackratio <= tcp_restoral_thresh) || 9381 (rack->r_ctl.rc_num_maps_alloced < tcp_map_minimum)) { 9382 rack->sack_attack_disable = 0; 9383 rack_log_sad(rack, 3); 9384 /* Restart counting */ 9385 rack->r_ctl.sack_count = 0; 9386 rack->r_ctl.sack_moved_extra = 0; 9387 rack->r_ctl.sack_noextra_move = 1; 9388 rack->r_ctl.ack_count = max(1, 9389 (bytes_this_ack / segsiz)); 9390 9391 if (rack->r_rep_reverse == 0) { 9392 rack->r_rep_reverse = 1; 9393 counter_u64_add(rack_sack_attacks_reversed, 1); 9394 } 9395 /* Restore the cwnd */ 9396 if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd) 9397 rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd; 9398 } 9399 } 9400 } 9401 } 9402 #endif 9403 9404 static void 9405 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end) 9406 { 9407 9408 uint32_t am, l_end; 9409 9410 if (SEQ_GT(end, start)) 9411 am = end - start; 9412 else 9413 am = 0; 9414 if ((rack->rc_last_tlp_acked_set ) && 9415 (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) && 9416 (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) { 9417 /* 9418 * The DSACK is because of a TLP which we don't 9419 * do anything with the reordering window over since 9420 * it was not reordering that caused the DSACK but 9421 * our previous retransmit TLP. 9422 */ 9423 rack_log_dsack_event(rack, 7, __LINE__, start, end); 9424 goto skip_dsack_round; 9425 } 9426 if (rack->rc_last_sent_tlp_seq_valid) { 9427 l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len; 9428 if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) && 9429 (SEQ_LEQ(end, l_end))) { 9430 /* 9431 * This dsack is from the last sent TLP, ignore it 9432 * for reordering purposes. 9433 */ 9434 rack_log_dsack_event(rack, 7, __LINE__, start, end); 9435 goto skip_dsack_round; 9436 } 9437 } 9438 if (rack->rc_dsack_round_seen == 0) { 9439 rack->rc_dsack_round_seen = 1; 9440 rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max; 9441 rack->r_ctl.num_dsack++; 9442 rack->r_ctl.dsack_persist = 16; /* 16 is from the standard */ 9443 rack_log_dsack_event(rack, 2, __LINE__, 0, 0); 9444 } 9445 skip_dsack_round: 9446 /* 9447 * We keep track of how many DSACK blocks we get 9448 * after a recovery incident. 9449 */ 9450 rack->r_ctl.dsack_byte_cnt += am; 9451 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) && 9452 rack->r_ctl.retran_during_recovery && 9453 (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) { 9454 /* 9455 * False recovery most likely culprit is reordering. If 9456 * nothing else is missing we need to revert. 9457 */ 9458 rack->r_might_revert = 1; 9459 rack_handle_might_revert(rack->rc_tp, rack); 9460 rack->r_might_revert = 0; 9461 rack->r_ctl.retran_during_recovery = 0; 9462 rack->r_ctl.dsack_byte_cnt = 0; 9463 } 9464 } 9465 9466 static void 9467 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack) 9468 { 9469 /* Deal with changed and PRR here (in recovery only) */ 9470 uint32_t pipe, snd_una; 9471 9472 rack->r_ctl.rc_prr_delivered += changed; 9473 9474 if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) { 9475 /* 9476 * It is all outstanding, we are application limited 9477 * and thus we don't need more room to send anything. 9478 * Note we use tp->snd_una here and not th_ack because 9479 * the data as yet not been cut from the sb. 9480 */ 9481 rack->r_ctl.rc_prr_sndcnt = 0; 9482 return; 9483 } 9484 /* Compute prr_sndcnt */ 9485 if (SEQ_GT(tp->snd_una, th_ack)) { 9486 snd_una = tp->snd_una; 9487 } else { 9488 snd_una = th_ack; 9489 } 9490 pipe = ((tp->snd_max - snd_una) - rack->r_ctl.rc_sacked) + rack->r_ctl.rc_holes_rxt; 9491 if (pipe > tp->snd_ssthresh) { 9492 long sndcnt; 9493 9494 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh; 9495 if (rack->r_ctl.rc_prr_recovery_fs > 0) 9496 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs; 9497 else { 9498 rack->r_ctl.rc_prr_sndcnt = 0; 9499 rack_log_to_prr(rack, 9, 0); 9500 sndcnt = 0; 9501 } 9502 sndcnt++; 9503 if (sndcnt > (long)rack->r_ctl.rc_prr_out) 9504 sndcnt -= rack->r_ctl.rc_prr_out; 9505 else 9506 sndcnt = 0; 9507 rack->r_ctl.rc_prr_sndcnt = sndcnt; 9508 rack_log_to_prr(rack, 10, 0); 9509 } else { 9510 uint32_t limit; 9511 9512 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out) 9513 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out); 9514 else 9515 limit = 0; 9516 if (changed > limit) 9517 limit = changed; 9518 limit += ctf_fixed_maxseg(tp); 9519 if (tp->snd_ssthresh > pipe) { 9520 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit); 9521 rack_log_to_prr(rack, 11, 0); 9522 } else { 9523 rack->r_ctl.rc_prr_sndcnt = min(0, limit); 9524 rack_log_to_prr(rack, 12, 0); 9525 } 9526 } 9527 } 9528 9529 static void 9530 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck) 9531 { 9532 uint32_t changed; 9533 struct tcp_rack *rack; 9534 struct rack_sendmap *rsm; 9535 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 9536 register uint32_t th_ack; 9537 int32_t i, j, k, num_sack_blks = 0; 9538 uint32_t cts, acked, ack_point, sack_changed = 0; 9539 int loop_start = 0, moved_two = 0; 9540 uint32_t tsused; 9541 9542 9543 INP_WLOCK_ASSERT(tp->t_inpcb); 9544 if (th->th_flags & TH_RST) { 9545 /* We don't log resets */ 9546 return; 9547 } 9548 rack = (struct tcp_rack *)tp->t_fb_ptr; 9549 cts = tcp_get_usecs(NULL); 9550 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 9551 changed = 0; 9552 th_ack = th->th_ack; 9553 if (rack->sack_attack_disable == 0) 9554 rack_do_decay(rack); 9555 if (BYTES_THIS_ACK(tp, th) >= ctf_fixed_maxseg(rack->rc_tp)) { 9556 /* 9557 * You only get credit for 9558 * MSS and greater (and you get extra 9559 * credit for larger cum-ack moves). 9560 */ 9561 int ac; 9562 9563 ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp); 9564 rack->r_ctl.ack_count += ac; 9565 counter_u64_add(rack_ack_total, ac); 9566 } 9567 if (rack->r_ctl.ack_count > 0xfff00000) { 9568 /* 9569 * reduce the number to keep us under 9570 * a uint32_t. 9571 */ 9572 rack->r_ctl.ack_count /= 2; 9573 rack->r_ctl.sack_count /= 2; 9574 } 9575 if (SEQ_GT(th_ack, tp->snd_una)) { 9576 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__); 9577 tp->t_acktime = ticks; 9578 } 9579 if (rsm && SEQ_GT(th_ack, rsm->r_start)) 9580 changed = th_ack - rsm->r_start; 9581 if (changed) { 9582 rack_process_to_cumack(tp, rack, th_ack, cts, to); 9583 } 9584 if ((to->to_flags & TOF_SACK) == 0) { 9585 /* We are done nothing left and no sack. */ 9586 rack_handle_might_revert(tp, rack); 9587 /* 9588 * For cases where we struck a dup-ack 9589 * with no SACK, add to the changes so 9590 * PRR will work right. 9591 */ 9592 if (dup_ack_struck && (changed == 0)) { 9593 changed += ctf_fixed_maxseg(rack->rc_tp); 9594 } 9595 goto out; 9596 } 9597 /* Sack block processing */ 9598 if (SEQ_GT(th_ack, tp->snd_una)) 9599 ack_point = th_ack; 9600 else 9601 ack_point = tp->snd_una; 9602 for (i = 0; i < to->to_nsacks; i++) { 9603 bcopy((to->to_sacks + i * TCPOLEN_SACK), 9604 &sack, sizeof(sack)); 9605 sack.start = ntohl(sack.start); 9606 sack.end = ntohl(sack.end); 9607 if (SEQ_GT(sack.end, sack.start) && 9608 SEQ_GT(sack.start, ack_point) && 9609 SEQ_LT(sack.start, tp->snd_max) && 9610 SEQ_GT(sack.end, ack_point) && 9611 SEQ_LEQ(sack.end, tp->snd_max)) { 9612 sack_blocks[num_sack_blks] = sack; 9613 num_sack_blks++; 9614 } else if (SEQ_LEQ(sack.start, th_ack) && 9615 SEQ_LEQ(sack.end, th_ack)) { 9616 #ifdef NETFLIX_STATS 9617 /* 9618 * Its a D-SACK block. 9619 */ 9620 tcp_record_dsack(sack.start, sack.end); 9621 #endif 9622 rack_note_dsack(rack, sack.start, sack.end); 9623 } 9624 } 9625 if (rack->rc_dsack_round_seen) { 9626 /* Is the dsack roound over? */ 9627 if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) { 9628 /* Yes it is */ 9629 rack->rc_dsack_round_seen = 0; 9630 rack_log_dsack_event(rack, 3, __LINE__, 0, 0); 9631 } 9632 } 9633 /* 9634 * Sort the SACK blocks so we can update the rack scoreboard with 9635 * just one pass. 9636 */ 9637 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks, 9638 num_sack_blks, th->th_ack); 9639 ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks); 9640 if (num_sack_blks == 0) { 9641 /* Nothing to sack (DSACKs?) */ 9642 goto out_with_totals; 9643 } 9644 if (num_sack_blks < 2) { 9645 /* Only one, we don't need to sort */ 9646 goto do_sack_work; 9647 } 9648 /* Sort the sacks */ 9649 for (i = 0; i < num_sack_blks; i++) { 9650 for (j = i + 1; j < num_sack_blks; j++) { 9651 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 9652 sack = sack_blocks[i]; 9653 sack_blocks[i] = sack_blocks[j]; 9654 sack_blocks[j] = sack; 9655 } 9656 } 9657 } 9658 /* 9659 * Now are any of the sack block ends the same (yes some 9660 * implementations send these)? 9661 */ 9662 again: 9663 if (num_sack_blks == 0) 9664 goto out_with_totals; 9665 if (num_sack_blks > 1) { 9666 for (i = 0; i < num_sack_blks; i++) { 9667 for (j = i + 1; j < num_sack_blks; j++) { 9668 if (sack_blocks[i].end == sack_blocks[j].end) { 9669 /* 9670 * Ok these two have the same end we 9671 * want the smallest end and then 9672 * throw away the larger and start 9673 * again. 9674 */ 9675 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 9676 /* 9677 * The second block covers 9678 * more area use that 9679 */ 9680 sack_blocks[i].start = sack_blocks[j].start; 9681 } 9682 /* 9683 * Now collapse out the dup-sack and 9684 * lower the count 9685 */ 9686 for (k = (j + 1); k < num_sack_blks; k++) { 9687 sack_blocks[j].start = sack_blocks[k].start; 9688 sack_blocks[j].end = sack_blocks[k].end; 9689 j++; 9690 } 9691 num_sack_blks--; 9692 goto again; 9693 } 9694 } 9695 } 9696 } 9697 do_sack_work: 9698 /* 9699 * First lets look to see if 9700 * we have retransmitted and 9701 * can use the transmit next? 9702 */ 9703 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 9704 if (rsm && 9705 SEQ_GT(sack_blocks[0].end, rsm->r_start) && 9706 SEQ_LT(sack_blocks[0].start, rsm->r_end)) { 9707 /* 9708 * We probably did the FR and the next 9709 * SACK in continues as we would expect. 9710 */ 9711 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &moved_two); 9712 if (acked) { 9713 rack->r_wanted_output = 1; 9714 changed += acked; 9715 sack_changed += acked; 9716 } 9717 if (num_sack_blks == 1) { 9718 /* 9719 * This is what we would expect from 9720 * a normal implementation to happen 9721 * after we have retransmitted the FR, 9722 * i.e the sack-filter pushes down 9723 * to 1 block and the next to be retransmitted 9724 * is the sequence in the sack block (has more 9725 * are acked). Count this as ACK'd data to boost 9726 * up the chances of recovering any false positives. 9727 */ 9728 rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp)); 9729 counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp))); 9730 counter_u64_add(rack_express_sack, 1); 9731 if (rack->r_ctl.ack_count > 0xfff00000) { 9732 /* 9733 * reduce the number to keep us under 9734 * a uint32_t. 9735 */ 9736 rack->r_ctl.ack_count /= 2; 9737 rack->r_ctl.sack_count /= 2; 9738 } 9739 goto out_with_totals; 9740 } else { 9741 /* 9742 * Start the loop through the 9743 * rest of blocks, past the first block. 9744 */ 9745 moved_two = 0; 9746 loop_start = 1; 9747 } 9748 } 9749 /* Its a sack of some sort */ 9750 rack->r_ctl.sack_count++; 9751 if (rack->r_ctl.sack_count > 0xfff00000) { 9752 /* 9753 * reduce the number to keep us under 9754 * a uint32_t. 9755 */ 9756 rack->r_ctl.ack_count /= 2; 9757 rack->r_ctl.sack_count /= 2; 9758 } 9759 counter_u64_add(rack_sack_total, 1); 9760 if (rack->sack_attack_disable) { 9761 /* An attacker disablement is in place */ 9762 if (num_sack_blks > 1) { 9763 rack->r_ctl.sack_count += (num_sack_blks - 1); 9764 rack->r_ctl.sack_moved_extra++; 9765 counter_u64_add(rack_move_some, 1); 9766 if (rack->r_ctl.sack_moved_extra > 0xfff00000) { 9767 rack->r_ctl.sack_moved_extra /= 2; 9768 rack->r_ctl.sack_noextra_move /= 2; 9769 } 9770 } 9771 goto out; 9772 } 9773 rsm = rack->r_ctl.rc_sacklast; 9774 for (i = loop_start; i < num_sack_blks; i++) { 9775 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &moved_two); 9776 if (acked) { 9777 rack->r_wanted_output = 1; 9778 changed += acked; 9779 sack_changed += acked; 9780 } 9781 if (moved_two) { 9782 /* 9783 * If we did not get a SACK for at least a MSS and 9784 * had to move at all, or if we moved more than our 9785 * threshold, it counts against the "extra" move. 9786 */ 9787 rack->r_ctl.sack_moved_extra += moved_two; 9788 counter_u64_add(rack_move_some, 1); 9789 } else { 9790 /* 9791 * else we did not have to move 9792 * any more than we would expect. 9793 */ 9794 rack->r_ctl.sack_noextra_move++; 9795 counter_u64_add(rack_move_none, 1); 9796 } 9797 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) { 9798 /* 9799 * If the SACK was not a full MSS then 9800 * we add to sack_count the number of 9801 * MSS's (or possibly more than 9802 * a MSS if its a TSO send) we had to skip by. 9803 */ 9804 rack->r_ctl.sack_count += moved_two; 9805 counter_u64_add(rack_sack_total, moved_two); 9806 } 9807 /* 9808 * Now we need to setup for the next 9809 * round. First we make sure we won't 9810 * exceed the size of our uint32_t on 9811 * the various counts, and then clear out 9812 * moved_two. 9813 */ 9814 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) || 9815 (rack->r_ctl.sack_noextra_move > 0xfff00000)) { 9816 rack->r_ctl.sack_moved_extra /= 2; 9817 rack->r_ctl.sack_noextra_move /= 2; 9818 } 9819 if (rack->r_ctl.sack_count > 0xfff00000) { 9820 rack->r_ctl.ack_count /= 2; 9821 rack->r_ctl.sack_count /= 2; 9822 } 9823 moved_two = 0; 9824 } 9825 out_with_totals: 9826 if (num_sack_blks > 1) { 9827 /* 9828 * You get an extra stroke if 9829 * you have more than one sack-blk, this 9830 * could be where we are skipping forward 9831 * and the sack-filter is still working, or 9832 * it could be an attacker constantly 9833 * moving us. 9834 */ 9835 rack->r_ctl.sack_moved_extra++; 9836 counter_u64_add(rack_move_some, 1); 9837 } 9838 out: 9839 #ifdef NETFLIX_EXP_DETECTION 9840 rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp)); 9841 #endif 9842 if (changed) { 9843 /* Something changed cancel the rack timer */ 9844 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 9845 } 9846 tsused = tcp_get_usecs(NULL); 9847 rsm = tcp_rack_output(tp, rack, tsused); 9848 if ((!IN_FASTRECOVERY(tp->t_flags)) && 9849 rsm) { 9850 /* Enter recovery */ 9851 rack->r_ctl.rc_rsm_start = rsm->r_start; 9852 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 9853 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 9854 entered_recovery = 1; 9855 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una); 9856 /* 9857 * When we enter recovery we need to assure we send 9858 * one packet. 9859 */ 9860 if (rack->rack_no_prr == 0) { 9861 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp); 9862 rack_log_to_prr(rack, 8, 0); 9863 } 9864 rack->r_timer_override = 1; 9865 rack->r_early = 0; 9866 rack->r_ctl.rc_agg_early = 0; 9867 } else if (IN_FASTRECOVERY(tp->t_flags) && 9868 rsm && 9869 (rack->r_rr_config == 3)) { 9870 /* 9871 * Assure we can output and we get no 9872 * remembered pace time except the retransmit. 9873 */ 9874 rack->r_timer_override = 1; 9875 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 9876 rack->r_ctl.rc_resend = rsm; 9877 } 9878 if (IN_FASTRECOVERY(tp->t_flags) && 9879 (rack->rack_no_prr == 0) && 9880 (entered_recovery == 0)) { 9881 rack_update_prr(tp, rack, changed, th_ack); 9882 if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) && 9883 ((rack->rc_inp->inp_in_hpts == 0) && 9884 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) { 9885 /* 9886 * If you are pacing output you don't want 9887 * to override. 9888 */ 9889 rack->r_early = 0; 9890 rack->r_ctl.rc_agg_early = 0; 9891 rack->r_timer_override = 1; 9892 } 9893 } 9894 } 9895 9896 static void 9897 rack_strike_dupack(struct tcp_rack *rack) 9898 { 9899 struct rack_sendmap *rsm; 9900 9901 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 9902 while (rsm && (rsm->r_dupack >= DUP_ACK_THRESHOLD)) { 9903 rsm = TAILQ_NEXT(rsm, r_tnext); 9904 } 9905 if (rsm && (rsm->r_dupack < 0xff)) { 9906 rsm->r_dupack++; 9907 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) { 9908 struct timeval tv; 9909 uint32_t cts; 9910 /* 9911 * Here we see if we need to retransmit. For 9912 * a SACK type connection if enough time has passed 9913 * we will get a return of the rsm. For a non-sack 9914 * connection we will get the rsm returned if the 9915 * dupack value is 3 or more. 9916 */ 9917 cts = tcp_get_usecs(&tv); 9918 rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts); 9919 if (rack->r_ctl.rc_resend != NULL) { 9920 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) { 9921 rack_cong_signal(rack->rc_tp, CC_NDUPACK, 9922 rack->rc_tp->snd_una); 9923 } 9924 rack->r_wanted_output = 1; 9925 rack->r_timer_override = 1; 9926 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3); 9927 } 9928 } else { 9929 rack_log_retran_reason(rack, rsm, __LINE__, 0, 3); 9930 } 9931 } 9932 } 9933 9934 static void 9935 rack_check_bottom_drag(struct tcpcb *tp, 9936 struct tcp_rack *rack, 9937 struct socket *so, int32_t acked) 9938 { 9939 uint32_t segsiz, minseg; 9940 9941 segsiz = ctf_fixed_maxseg(tp); 9942 minseg = segsiz; 9943 9944 if (tp->snd_max == tp->snd_una) { 9945 /* 9946 * We are doing dynamic pacing and we are way 9947 * under. Basically everything got acked while 9948 * we were still waiting on the pacer to expire. 9949 * 9950 * This means we need to boost the b/w in 9951 * addition to any earlier boosting of 9952 * the multipler. 9953 */ 9954 rack->rc_dragged_bottom = 1; 9955 rack_validate_multipliers_at_or_above100(rack); 9956 /* 9957 * Lets use the segment bytes acked plus 9958 * the lowest RTT seen as the basis to 9959 * form a b/w estimate. This will be off 9960 * due to the fact that the true estimate 9961 * should be around 1/2 the time of the RTT 9962 * but we can settle for that. 9963 */ 9964 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) && 9965 acked) { 9966 uint64_t bw, calc_bw, rtt; 9967 9968 rtt = rack->r_ctl.rack_rs.rs_us_rtt; 9969 if (rtt == 0) { 9970 /* no us sample is there a ms one? */ 9971 if (rack->r_ctl.rack_rs.rs_rtt_lowest) { 9972 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest; 9973 } else { 9974 goto no_measurement; 9975 } 9976 } 9977 bw = acked; 9978 calc_bw = bw * 1000000; 9979 calc_bw /= rtt; 9980 if (rack->r_ctl.last_max_bw && 9981 (rack->r_ctl.last_max_bw < calc_bw)) { 9982 /* 9983 * If we have a last calculated max bw 9984 * enforce it. 9985 */ 9986 calc_bw = rack->r_ctl.last_max_bw; 9987 } 9988 /* now plop it in */ 9989 if (rack->rc_gp_filled == 0) { 9990 if (calc_bw > ONE_POINT_TWO_MEG) { 9991 /* 9992 * If we have no measurement 9993 * don't let us set in more than 9994 * 1.2Mbps. If we are still too 9995 * low after pacing with this we 9996 * will hopefully have a max b/w 9997 * available to sanity check things. 9998 */ 9999 calc_bw = ONE_POINT_TWO_MEG; 10000 } 10001 rack->r_ctl.rc_rtt_diff = 0; 10002 rack->r_ctl.gp_bw = calc_bw; 10003 rack->rc_gp_filled = 1; 10004 if (rack->r_ctl.num_measurements < RACK_REQ_AVG) 10005 rack->r_ctl.num_measurements = RACK_REQ_AVG; 10006 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 10007 } else if (calc_bw > rack->r_ctl.gp_bw) { 10008 rack->r_ctl.rc_rtt_diff = 0; 10009 if (rack->r_ctl.num_measurements < RACK_REQ_AVG) 10010 rack->r_ctl.num_measurements = RACK_REQ_AVG; 10011 rack->r_ctl.gp_bw = calc_bw; 10012 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 10013 } else 10014 rack_increase_bw_mul(rack, -1, 0, 0, 1); 10015 if ((rack->gp_ready == 0) && 10016 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) { 10017 /* We have enough measurements now */ 10018 rack->gp_ready = 1; 10019 rack_set_cc_pacing(rack); 10020 if (rack->defer_options) 10021 rack_apply_deferred_options(rack); 10022 } 10023 /* 10024 * For acks over 1mss we do a extra boost to simulate 10025 * where we would get 2 acks (we want 110 for the mul). 10026 */ 10027 if (acked > segsiz) 10028 rack_increase_bw_mul(rack, -1, 0, 0, 1); 10029 } else { 10030 /* 10031 * zero rtt possibly?, settle for just an old increase. 10032 */ 10033 no_measurement: 10034 rack_increase_bw_mul(rack, -1, 0, 0, 1); 10035 } 10036 } else if ((IN_FASTRECOVERY(tp->t_flags) == 0) && 10037 (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)), 10038 minseg)) && 10039 (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) && 10040 (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) && 10041 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <= 10042 (segsiz * rack_req_segs))) { 10043 /* 10044 * We are doing dynamic GP pacing and 10045 * we have everything except 1MSS or less 10046 * bytes left out. We are still pacing away. 10047 * And there is data that could be sent, This 10048 * means we are inserting delayed ack time in 10049 * our measurements because we are pacing too slow. 10050 */ 10051 rack_validate_multipliers_at_or_above100(rack); 10052 rack->rc_dragged_bottom = 1; 10053 rack_increase_bw_mul(rack, -1, 0, 0, 1); 10054 } 10055 } 10056 10057 10058 10059 static void 10060 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount) 10061 { 10062 /* 10063 * The fast output path is enabled and we 10064 * have moved the cumack forward. Lets see if 10065 * we can expand forward the fast path length by 10066 * that amount. What we would ideally like to 10067 * do is increase the number of bytes in the 10068 * fast path block (left_to_send) by the 10069 * acked amount. However we have to gate that 10070 * by two factors: 10071 * 1) The amount outstanding and the rwnd of the peer 10072 * (i.e. we don't want to exceed the rwnd of the peer). 10073 * <and> 10074 * 2) The amount of data left in the socket buffer (i.e. 10075 * we can't send beyond what is in the buffer). 10076 * 10077 * Note that this does not take into account any increase 10078 * in the cwnd. We will only extend the fast path by 10079 * what was acked. 10080 */ 10081 uint32_t new_total, gating_val; 10082 10083 new_total = acked_amount + rack->r_ctl.fsb.left_to_send; 10084 gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)), 10085 (tp->snd_wnd - (tp->snd_max - tp->snd_una))); 10086 if (new_total <= gating_val) { 10087 /* We can increase left_to_send by the acked amount */ 10088 counter_u64_add(rack_extended_rfo, 1); 10089 rack->r_ctl.fsb.left_to_send = new_total; 10090 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))), 10091 ("rack:%p left_to_send:%u sbavail:%u out:%u", 10092 rack, rack->r_ctl.fsb.left_to_send, 10093 sbavail(&rack->rc_inp->inp_socket->so_snd), 10094 (tp->snd_max - tp->snd_una))); 10095 10096 } 10097 } 10098 10099 static void 10100 rack_adjust_sendmap(struct tcp_rack *rack, struct sockbuf *sb, tcp_seq snd_una) 10101 { 10102 /* 10103 * Here any sendmap entry that points to the 10104 * beginning mbuf must be adjusted to the correct 10105 * offset. This must be called with: 10106 * 1) The socket buffer locked 10107 * 2) snd_una adjusted to its new postion. 10108 * 10109 * Note that (2) implies rack_ack_received has also 10110 * been called. 10111 * 10112 * We grab the first mbuf in the socket buffer and 10113 * then go through the front of the sendmap, recalculating 10114 * the stored offset for any sendmap entry that has 10115 * that mbuf. We must use the sb functions to do this 10116 * since its possible an add was done has well as 10117 * the subtraction we may have just completed. This should 10118 * not be a penalty though, since we just referenced the sb 10119 * to go in and trim off the mbufs that we freed (of course 10120 * there will be a penalty for the sendmap references though). 10121 */ 10122 struct mbuf *m; 10123 struct rack_sendmap *rsm; 10124 10125 SOCKBUF_LOCK_ASSERT(sb); 10126 m = sb->sb_mb; 10127 rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 10128 if ((rsm == NULL) || (m == NULL)) { 10129 /* Nothing outstanding */ 10130 return; 10131 } 10132 while (rsm->m && (rsm->m == m)) { 10133 /* one to adjust */ 10134 #ifdef INVARIANTS 10135 struct mbuf *tm; 10136 uint32_t soff; 10137 10138 tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff); 10139 if (rsm->orig_m_len != m->m_len) { 10140 rack_adjust_orig_mlen(rsm); 10141 } 10142 if (rsm->soff != soff) { 10143 /* 10144 * This is not a fatal error, we anticipate it 10145 * might happen (the else code), so we count it here 10146 * so that under invariant we can see that it really 10147 * does happen. 10148 */ 10149 counter_u64_add(rack_adjust_map_bw, 1); 10150 } 10151 rsm->m = tm; 10152 rsm->soff = soff; 10153 if (tm) 10154 rsm->orig_m_len = rsm->m->m_len; 10155 else 10156 rsm->orig_m_len = 0; 10157 #else 10158 rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff); 10159 if (rsm->m) 10160 rsm->orig_m_len = rsm->m->m_len; 10161 else 10162 rsm->orig_m_len = 0; 10163 #endif 10164 rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, 10165 rsm); 10166 if (rsm == NULL) 10167 break; 10168 } 10169 } 10170 10171 /* 10172 * Return value of 1, we do not need to call rack_process_data(). 10173 * return value of 0, rack_process_data can be called. 10174 * For ret_val if its 0 the TCP is locked, if its non-zero 10175 * its unlocked and probably unsafe to touch the TCB. 10176 */ 10177 static int 10178 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 10179 struct tcpcb *tp, struct tcpopt *to, 10180 uint32_t tiwin, int32_t tlen, 10181 int32_t * ofia, int32_t thflags, int32_t *ret_val) 10182 { 10183 int32_t ourfinisacked = 0; 10184 int32_t nsegs, acked_amount; 10185 int32_t acked; 10186 struct mbuf *mfree; 10187 struct tcp_rack *rack; 10188 int32_t under_pacing = 0; 10189 int32_t recovery = 0; 10190 10191 rack = (struct tcp_rack *)tp->t_fb_ptr; 10192 if (SEQ_GT(th->th_ack, tp->snd_max)) { 10193 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val, 10194 &rack->r_ctl.challenge_ack_ts, 10195 &rack->r_ctl.challenge_ack_cnt); 10196 rack->r_wanted_output = 1; 10197 return (1); 10198 } 10199 if (rack->gp_ready && 10200 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 10201 under_pacing = 1; 10202 } 10203 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 10204 int in_rec, dup_ack_struck = 0; 10205 10206 in_rec = IN_FASTRECOVERY(tp->t_flags); 10207 if (rack->rc_in_persist) { 10208 tp->t_rxtshift = 0; 10209 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 10210 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 10211 } 10212 if ((th->th_ack == tp->snd_una) && 10213 (tiwin == tp->snd_wnd) && 10214 ((to->to_flags & TOF_SACK) == 0)) { 10215 rack_strike_dupack(rack); 10216 dup_ack_struck = 1; 10217 } 10218 rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), dup_ack_struck); 10219 } 10220 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 10221 /* 10222 * Old ack, behind (or duplicate to) the last one rcv'd 10223 * Note: We mark reordering is occuring if its 10224 * less than and we have not closed our window. 10225 */ 10226 if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) { 10227 counter_u64_add(rack_reorder_seen, 1); 10228 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 10229 } 10230 return (0); 10231 } 10232 /* 10233 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 10234 * something we sent. 10235 */ 10236 if (tp->t_flags & TF_NEEDSYN) { 10237 /* 10238 * T/TCP: Connection was half-synchronized, and our SYN has 10239 * been ACK'd (so connection is now fully synchronized). Go 10240 * to non-starred state, increment snd_una for ACK of SYN, 10241 * and check if we can do window scaling. 10242 */ 10243 tp->t_flags &= ~TF_NEEDSYN; 10244 tp->snd_una++; 10245 /* Do window scaling? */ 10246 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 10247 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 10248 tp->rcv_scale = tp->request_r_scale; 10249 /* Send window already scaled. */ 10250 } 10251 } 10252 nsegs = max(1, m->m_pkthdr.lro_nsegs); 10253 INP_WLOCK_ASSERT(tp->t_inpcb); 10254 10255 acked = BYTES_THIS_ACK(tp, th); 10256 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs); 10257 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 10258 /* 10259 * If we just performed our first retransmit, and the ACK arrives 10260 * within our recovery window, then it was a mistake to do the 10261 * retransmit in the first place. Recover our original cwnd and 10262 * ssthresh, and proceed to transmit where we left off. 10263 */ 10264 if ((tp->t_flags & TF_PREVVALID) && 10265 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 10266 tp->t_flags &= ~TF_PREVVALID; 10267 if (tp->t_rxtshift == 1 && 10268 (int)(ticks - tp->t_badrxtwin) < 0) 10269 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack); 10270 } 10271 if (acked) { 10272 /* assure we are not backed off */ 10273 tp->t_rxtshift = 0; 10274 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 10275 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 10276 rack->rc_tlp_in_progress = 0; 10277 rack->r_ctl.rc_tlp_cnt_out = 0; 10278 /* 10279 * If it is the RXT timer we want to 10280 * stop it, so we can restart a TLP. 10281 */ 10282 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 10283 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 10284 #ifdef NETFLIX_HTTP_LOGGING 10285 tcp_http_check_for_comp(rack->rc_tp, th->th_ack); 10286 #endif 10287 } 10288 /* 10289 * If we have a timestamp reply, update smoothed round trip time. If 10290 * no timestamp is present but transmit timer is running and timed 10291 * sequence number was acked, update smoothed round trip time. Since 10292 * we now have an rtt measurement, cancel the timer backoff (cf., 10293 * Phil Karn's retransmit alg.). Recompute the initial retransmit 10294 * timer. 10295 * 10296 * Some boxes send broken timestamp replies during the SYN+ACK 10297 * phase, ignore timestamps of 0 or we could calculate a huge RTT 10298 * and blow up the retransmit timer. 10299 */ 10300 /* 10301 * If all outstanding data is acked, stop retransmit timer and 10302 * remember to restart (more output or persist). If there is more 10303 * data to be acked, restart retransmit timer, using current 10304 * (possibly backed-off) value. 10305 */ 10306 if (acked == 0) { 10307 if (ofia) 10308 *ofia = ourfinisacked; 10309 return (0); 10310 } 10311 if (IN_RECOVERY(tp->t_flags)) { 10312 if (SEQ_LT(th->th_ack, tp->snd_recover) && 10313 (SEQ_LT(th->th_ack, tp->snd_max))) { 10314 tcp_rack_partialack(tp); 10315 } else { 10316 rack_post_recovery(tp, th->th_ack); 10317 recovery = 1; 10318 } 10319 } 10320 /* 10321 * Let the congestion control algorithm update congestion control 10322 * related information. This typically means increasing the 10323 * congestion window. 10324 */ 10325 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, recovery); 10326 SOCKBUF_LOCK(&so->so_snd); 10327 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 10328 tp->snd_wnd -= acked_amount; 10329 mfree = sbcut_locked(&so->so_snd, acked_amount); 10330 if ((sbused(&so->so_snd) == 0) && 10331 (acked > acked_amount) && 10332 (tp->t_state >= TCPS_FIN_WAIT_1) && 10333 (tp->t_flags & TF_SENTFIN)) { 10334 /* 10335 * We must be sure our fin 10336 * was sent and acked (we can be 10337 * in FIN_WAIT_1 without having 10338 * sent the fin). 10339 */ 10340 ourfinisacked = 1; 10341 } 10342 tp->snd_una = th->th_ack; 10343 if (acked_amount && sbavail(&so->so_snd)) 10344 rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una); 10345 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 10346 /* NB: sowwakeup_locked() does an implicit unlock. */ 10347 sowwakeup_locked(so); 10348 m_freem(mfree); 10349 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 10350 tp->snd_recover = tp->snd_una; 10351 10352 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) { 10353 tp->snd_nxt = tp->snd_una; 10354 } 10355 if (under_pacing && 10356 (rack->use_fixed_rate == 0) && 10357 (rack->in_probe_rtt == 0) && 10358 rack->rc_gp_dyn_mul && 10359 rack->rc_always_pace) { 10360 /* Check if we are dragging bottom */ 10361 rack_check_bottom_drag(tp, rack, so, acked); 10362 } 10363 if (tp->snd_una == tp->snd_max) { 10364 /* Nothing left outstanding */ 10365 tp->t_flags &= ~TF_PREVVALID; 10366 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 10367 rack->r_ctl.retran_during_recovery = 0; 10368 rack->r_ctl.dsack_byte_cnt = 0; 10369 if (rack->r_ctl.rc_went_idle_time == 0) 10370 rack->r_ctl.rc_went_idle_time = 1; 10371 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 10372 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0) 10373 tp->t_acktime = 0; 10374 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 10375 /* Set need output so persist might get set */ 10376 rack->r_wanted_output = 1; 10377 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 10378 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 10379 (sbavail(&so->so_snd) == 0) && 10380 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 10381 /* 10382 * The socket was gone and the 10383 * peer sent data (now or in the past), time to 10384 * reset him. 10385 */ 10386 *ret_val = 1; 10387 /* tcp_close will kill the inp pre-log the Reset */ 10388 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 10389 tp = tcp_close(tp); 10390 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 10391 return (1); 10392 } 10393 } 10394 if (ofia) 10395 *ofia = ourfinisacked; 10396 return (0); 10397 } 10398 10399 static void 10400 rack_collapsed_window(struct tcp_rack *rack) 10401 { 10402 /* 10403 * Now we must walk the 10404 * send map and divide the 10405 * ones left stranded. These 10406 * guys can't cause us to abort 10407 * the connection and are really 10408 * "unsent". However if a buggy 10409 * client actually did keep some 10410 * of the data i.e. collapsed the win 10411 * and refused to ack and then opened 10412 * the win and acked that data. We would 10413 * get into an ack war, the simplier 10414 * method then of just pretending we 10415 * did not send those segments something 10416 * won't work. 10417 */ 10418 struct rack_sendmap *rsm, *nrsm, fe, *insret; 10419 tcp_seq max_seq; 10420 10421 max_seq = rack->rc_tp->snd_una + rack->rc_tp->snd_wnd; 10422 memset(&fe, 0, sizeof(fe)); 10423 fe.r_start = max_seq; 10424 /* Find the first seq past or at maxseq */ 10425 rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 10426 if (rsm == NULL) { 10427 /* Nothing to do strange */ 10428 rack->rc_has_collapsed = 0; 10429 return; 10430 } 10431 /* 10432 * Now do we need to split at 10433 * the collapse point? 10434 */ 10435 if (SEQ_GT(max_seq, rsm->r_start)) { 10436 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 10437 if (nrsm == NULL) { 10438 /* We can't get a rsm, mark all? */ 10439 nrsm = rsm; 10440 goto no_split; 10441 } 10442 /* Clone it */ 10443 rack_clone_rsm(rack, nrsm, rsm, max_seq); 10444 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm); 10445 #ifdef INVARIANTS 10446 if (insret != NULL) { 10447 panic("Insert in rb tree of %p fails ret:%p rack:%p rsm:%p", 10448 nrsm, insret, rack, rsm); 10449 } 10450 #endif 10451 rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT, max_seq, __LINE__); 10452 if (rsm->r_in_tmap) { 10453 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 10454 nrsm->r_in_tmap = 1; 10455 } 10456 /* 10457 * Set in the new RSM as the 10458 * collapsed starting point 10459 */ 10460 rsm = nrsm; 10461 } 10462 no_split: 10463 counter_u64_add(rack_collapsed_win, 1); 10464 RB_FOREACH_FROM(nrsm, rack_rb_tree_head, rsm) { 10465 nrsm->r_flags |= RACK_RWND_COLLAPSED; 10466 } 10467 rack->rc_has_collapsed = 1; 10468 } 10469 10470 static void 10471 rack_un_collapse_window(struct tcp_rack *rack) 10472 { 10473 struct rack_sendmap *rsm; 10474 10475 RB_FOREACH_REVERSE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) { 10476 if (rsm->r_flags & RACK_RWND_COLLAPSED) 10477 rsm->r_flags &= ~RACK_RWND_COLLAPSED; 10478 else 10479 break; 10480 } 10481 rack->rc_has_collapsed = 0; 10482 } 10483 10484 static void 10485 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack, 10486 int32_t tlen, int32_t tfo_syn) 10487 { 10488 if (DELAY_ACK(tp, tlen) || tfo_syn) { 10489 if (rack->rc_dack_mode && 10490 (tlen > 500) && 10491 (rack->rc_dack_toggle == 1)) { 10492 goto no_delayed_ack; 10493 } 10494 rack_timer_cancel(tp, rack, 10495 rack->r_ctl.rc_rcvtime, __LINE__); 10496 tp->t_flags |= TF_DELACK; 10497 } else { 10498 no_delayed_ack: 10499 rack->r_wanted_output = 1; 10500 tp->t_flags |= TF_ACKNOW; 10501 if (rack->rc_dack_mode) { 10502 if (tp->t_flags & TF_DELACK) 10503 rack->rc_dack_toggle = 1; 10504 else 10505 rack->rc_dack_toggle = 0; 10506 } 10507 } 10508 } 10509 10510 static void 10511 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack) 10512 { 10513 /* 10514 * If fast output is in progress, lets validate that 10515 * the new window did not shrink on us and make it 10516 * so fast output should end. 10517 */ 10518 if (rack->r_fast_output) { 10519 uint32_t out; 10520 10521 /* 10522 * Calculate what we will send if left as is 10523 * and compare that to our send window. 10524 */ 10525 out = ctf_outstanding(tp); 10526 if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) { 10527 /* ok we have an issue */ 10528 if (out >= tp->snd_wnd) { 10529 /* Turn off fast output the window is met or collapsed */ 10530 rack->r_fast_output = 0; 10531 } else { 10532 /* we have some room left */ 10533 rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out; 10534 if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) { 10535 /* If not at least 1 full segment never mind */ 10536 rack->r_fast_output = 0; 10537 } 10538 } 10539 } 10540 } 10541 } 10542 10543 10544 /* 10545 * Return value of 1, the TCB is unlocked and most 10546 * likely gone, return value of 0, the TCP is still 10547 * locked. 10548 */ 10549 static int 10550 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 10551 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 10552 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 10553 { 10554 /* 10555 * Update window information. Don't look at window if no ACK: TAC's 10556 * send garbage on first SYN. 10557 */ 10558 int32_t nsegs; 10559 int32_t tfo_syn; 10560 struct tcp_rack *rack; 10561 10562 rack = (struct tcp_rack *)tp->t_fb_ptr; 10563 INP_WLOCK_ASSERT(tp->t_inpcb); 10564 nsegs = max(1, m->m_pkthdr.lro_nsegs); 10565 if ((thflags & TH_ACK) && 10566 (SEQ_LT(tp->snd_wl1, th->th_seq) || 10567 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 10568 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 10569 /* keep track of pure window updates */ 10570 if (tlen == 0 && 10571 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 10572 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 10573 tp->snd_wnd = tiwin; 10574 rack_validate_fo_sendwin_up(tp, rack); 10575 tp->snd_wl1 = th->th_seq; 10576 tp->snd_wl2 = th->th_ack; 10577 if (tp->snd_wnd > tp->max_sndwnd) 10578 tp->max_sndwnd = tp->snd_wnd; 10579 rack->r_wanted_output = 1; 10580 } else if (thflags & TH_ACK) { 10581 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 10582 tp->snd_wnd = tiwin; 10583 rack_validate_fo_sendwin_up(tp, rack); 10584 tp->snd_wl1 = th->th_seq; 10585 tp->snd_wl2 = th->th_ack; 10586 } 10587 } 10588 if (tp->snd_wnd < ctf_outstanding(tp)) 10589 /* The peer collapsed the window */ 10590 rack_collapsed_window(rack); 10591 else if (rack->rc_has_collapsed) 10592 rack_un_collapse_window(rack); 10593 /* Was persist timer active and now we have window space? */ 10594 if ((rack->rc_in_persist != 0) && 10595 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 10596 rack->r_ctl.rc_pace_min_segs))) { 10597 rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime); 10598 tp->snd_nxt = tp->snd_max; 10599 /* Make sure we output to start the timer */ 10600 rack->r_wanted_output = 1; 10601 } 10602 /* Do we enter persists? */ 10603 if ((rack->rc_in_persist == 0) && 10604 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 10605 TCPS_HAVEESTABLISHED(tp->t_state) && 10606 (tp->snd_max == tp->snd_una) && 10607 sbavail(&tp->t_inpcb->inp_socket->so_snd) && 10608 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) { 10609 /* 10610 * Here the rwnd is less than 10611 * the pacing size, we are established, 10612 * nothing is outstanding, and there is 10613 * data to send. Enter persists. 10614 */ 10615 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime); 10616 } 10617 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 10618 m_freem(m); 10619 return (0); 10620 } 10621 /* 10622 * don't process the URG bit, ignore them drag 10623 * along the up. 10624 */ 10625 tp->rcv_up = tp->rcv_nxt; 10626 INP_WLOCK_ASSERT(tp->t_inpcb); 10627 10628 /* 10629 * Process the segment text, merging it into the TCP sequencing 10630 * queue, and arranging for acknowledgment of receipt if necessary. 10631 * This process logically involves adjusting tp->rcv_wnd as data is 10632 * presented to the user (this happens in tcp_usrreq.c, case 10633 * PRU_RCVD). If a FIN has already been received on this connection 10634 * then we just ignore the text. 10635 */ 10636 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 10637 IS_FASTOPEN(tp->t_flags)); 10638 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 10639 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10640 tcp_seq save_start = th->th_seq; 10641 tcp_seq save_rnxt = tp->rcv_nxt; 10642 int save_tlen = tlen; 10643 10644 m_adj(m, drop_hdrlen); /* delayed header drop */ 10645 /* 10646 * Insert segment which includes th into TCP reassembly 10647 * queue with control block tp. Set thflags to whether 10648 * reassembly now includes a segment with FIN. This handles 10649 * the common case inline (segment is the next to be 10650 * received on an established connection, and the queue is 10651 * empty), avoiding linkage into and removal from the queue 10652 * and repetition of various conversions. Set DELACK for 10653 * segments received in order, but ack immediately when 10654 * segments are out of order (so fast retransmit can work). 10655 */ 10656 if (th->th_seq == tp->rcv_nxt && 10657 SEGQ_EMPTY(tp) && 10658 (TCPS_HAVEESTABLISHED(tp->t_state) || 10659 tfo_syn)) { 10660 #ifdef NETFLIX_SB_LIMITS 10661 u_int mcnt, appended; 10662 10663 if (so->so_rcv.sb_shlim) { 10664 mcnt = m_memcnt(m); 10665 appended = 0; 10666 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 10667 CFO_NOSLEEP, NULL) == false) { 10668 counter_u64_add(tcp_sb_shlim_fails, 1); 10669 m_freem(m); 10670 return (0); 10671 } 10672 } 10673 #endif 10674 rack_handle_delayed_ack(tp, rack, tlen, tfo_syn); 10675 tp->rcv_nxt += tlen; 10676 if (tlen && 10677 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 10678 (tp->t_fbyte_in == 0)) { 10679 tp->t_fbyte_in = ticks; 10680 if (tp->t_fbyte_in == 0) 10681 tp->t_fbyte_in = 1; 10682 if (tp->t_fbyte_out && tp->t_fbyte_in) 10683 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 10684 } 10685 thflags = th->th_flags & TH_FIN; 10686 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs); 10687 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 10688 SOCKBUF_LOCK(&so->so_rcv); 10689 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 10690 m_freem(m); 10691 } else 10692 #ifdef NETFLIX_SB_LIMITS 10693 appended = 10694 #endif 10695 sbappendstream_locked(&so->so_rcv, m, 0); 10696 10697 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1); 10698 /* NB: sorwakeup_locked() does an implicit unlock. */ 10699 sorwakeup_locked(so); 10700 #ifdef NETFLIX_SB_LIMITS 10701 if (so->so_rcv.sb_shlim && appended != mcnt) 10702 counter_fo_release(so->so_rcv.sb_shlim, 10703 mcnt - appended); 10704 #endif 10705 } else { 10706 /* 10707 * XXX: Due to the header drop above "th" is 10708 * theoretically invalid by now. Fortunately 10709 * m_adj() doesn't actually frees any mbufs when 10710 * trimming from the head. 10711 */ 10712 tcp_seq temp = save_start; 10713 10714 thflags = tcp_reass(tp, th, &temp, &tlen, m); 10715 tp->t_flags |= TF_ACKNOW; 10716 if (tp->t_flags & TF_WAKESOR) { 10717 tp->t_flags &= ~TF_WAKESOR; 10718 /* NB: sorwakeup_locked() does an implicit unlock. */ 10719 sorwakeup_locked(so); 10720 } 10721 } 10722 if ((tp->t_flags & TF_SACK_PERMIT) && 10723 (save_tlen > 0) && 10724 TCPS_HAVEESTABLISHED(tp->t_state)) { 10725 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 10726 /* 10727 * DSACK actually handled in the fastpath 10728 * above. 10729 */ 10730 RACK_OPTS_INC(tcp_sack_path_1); 10731 tcp_update_sack_list(tp, save_start, 10732 save_start + save_tlen); 10733 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 10734 if ((tp->rcv_numsacks >= 1) && 10735 (tp->sackblks[0].end == save_start)) { 10736 /* 10737 * Partial overlap, recorded at todrop 10738 * above. 10739 */ 10740 RACK_OPTS_INC(tcp_sack_path_2a); 10741 tcp_update_sack_list(tp, 10742 tp->sackblks[0].start, 10743 tp->sackblks[0].end); 10744 } else { 10745 RACK_OPTS_INC(tcp_sack_path_2b); 10746 tcp_update_dsack_list(tp, save_start, 10747 save_start + save_tlen); 10748 } 10749 } else if (tlen >= save_tlen) { 10750 /* Update of sackblks. */ 10751 RACK_OPTS_INC(tcp_sack_path_3); 10752 tcp_update_dsack_list(tp, save_start, 10753 save_start + save_tlen); 10754 } else if (tlen > 0) { 10755 RACK_OPTS_INC(tcp_sack_path_4); 10756 tcp_update_dsack_list(tp, save_start, 10757 save_start + tlen); 10758 } 10759 } 10760 } else { 10761 m_freem(m); 10762 thflags &= ~TH_FIN; 10763 } 10764 10765 /* 10766 * If FIN is received ACK the FIN and let the user know that the 10767 * connection is closing. 10768 */ 10769 if (thflags & TH_FIN) { 10770 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10771 /* The socket upcall is handled by socantrcvmore. */ 10772 socantrcvmore(so); 10773 /* 10774 * If connection is half-synchronized (ie NEEDSYN 10775 * flag on) then delay ACK, so it may be piggybacked 10776 * when SYN is sent. Otherwise, since we received a 10777 * FIN then no more input can be expected, send ACK 10778 * now. 10779 */ 10780 if (tp->t_flags & TF_NEEDSYN) { 10781 rack_timer_cancel(tp, rack, 10782 rack->r_ctl.rc_rcvtime, __LINE__); 10783 tp->t_flags |= TF_DELACK; 10784 } else { 10785 tp->t_flags |= TF_ACKNOW; 10786 } 10787 tp->rcv_nxt++; 10788 } 10789 switch (tp->t_state) { 10790 /* 10791 * In SYN_RECEIVED and ESTABLISHED STATES enter the 10792 * CLOSE_WAIT state. 10793 */ 10794 case TCPS_SYN_RECEIVED: 10795 tp->t_starttime = ticks; 10796 /* FALLTHROUGH */ 10797 case TCPS_ESTABLISHED: 10798 rack_timer_cancel(tp, rack, 10799 rack->r_ctl.rc_rcvtime, __LINE__); 10800 tcp_state_change(tp, TCPS_CLOSE_WAIT); 10801 break; 10802 10803 /* 10804 * If still in FIN_WAIT_1 STATE FIN has not been 10805 * acked so enter the CLOSING state. 10806 */ 10807 case TCPS_FIN_WAIT_1: 10808 rack_timer_cancel(tp, rack, 10809 rack->r_ctl.rc_rcvtime, __LINE__); 10810 tcp_state_change(tp, TCPS_CLOSING); 10811 break; 10812 10813 /* 10814 * In FIN_WAIT_2 state enter the TIME_WAIT state, 10815 * starting the time-wait timer, turning off the 10816 * other standard timers. 10817 */ 10818 case TCPS_FIN_WAIT_2: 10819 rack_timer_cancel(tp, rack, 10820 rack->r_ctl.rc_rcvtime, __LINE__); 10821 tcp_twstart(tp); 10822 return (1); 10823 } 10824 } 10825 /* 10826 * Return any desired output. 10827 */ 10828 if ((tp->t_flags & TF_ACKNOW) || 10829 (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) { 10830 rack->r_wanted_output = 1; 10831 } 10832 INP_WLOCK_ASSERT(tp->t_inpcb); 10833 return (0); 10834 } 10835 10836 /* 10837 * Here nothing is really faster, its just that we 10838 * have broken out the fast-data path also just like 10839 * the fast-ack. 10840 */ 10841 static int 10842 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 10843 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 10844 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 10845 { 10846 int32_t nsegs; 10847 int32_t newsize = 0; /* automatic sockbuf scaling */ 10848 struct tcp_rack *rack; 10849 #ifdef NETFLIX_SB_LIMITS 10850 u_int mcnt, appended; 10851 #endif 10852 #ifdef TCPDEBUG 10853 /* 10854 * The size of tcp_saveipgen must be the size of the max ip header, 10855 * now IPv6. 10856 */ 10857 u_char tcp_saveipgen[IP6_HDR_LEN]; 10858 struct tcphdr tcp_savetcp; 10859 short ostate = 0; 10860 10861 #endif 10862 /* 10863 * If last ACK falls within this segment's sequence numbers, record 10864 * the timestamp. NOTE that the test is modified according to the 10865 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 10866 */ 10867 if (__predict_false(th->th_seq != tp->rcv_nxt)) { 10868 return (0); 10869 } 10870 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 10871 return (0); 10872 } 10873 if (tiwin && tiwin != tp->snd_wnd) { 10874 return (0); 10875 } 10876 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 10877 return (0); 10878 } 10879 if (__predict_false((to->to_flags & TOF_TS) && 10880 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 10881 return (0); 10882 } 10883 if (__predict_false((th->th_ack != tp->snd_una))) { 10884 return (0); 10885 } 10886 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 10887 return (0); 10888 } 10889 if ((to->to_flags & TOF_TS) != 0 && 10890 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 10891 tp->ts_recent_age = tcp_ts_getticks(); 10892 tp->ts_recent = to->to_tsval; 10893 } 10894 rack = (struct tcp_rack *)tp->t_fb_ptr; 10895 /* 10896 * This is a pure, in-sequence data packet with nothing on the 10897 * reassembly queue and we have enough buffer space to take it. 10898 */ 10899 nsegs = max(1, m->m_pkthdr.lro_nsegs); 10900 10901 #ifdef NETFLIX_SB_LIMITS 10902 if (so->so_rcv.sb_shlim) { 10903 mcnt = m_memcnt(m); 10904 appended = 0; 10905 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 10906 CFO_NOSLEEP, NULL) == false) { 10907 counter_u64_add(tcp_sb_shlim_fails, 1); 10908 m_freem(m); 10909 return (1); 10910 } 10911 } 10912 #endif 10913 /* Clean receiver SACK report if present */ 10914 if (tp->rcv_numsacks) 10915 tcp_clean_sackreport(tp); 10916 KMOD_TCPSTAT_INC(tcps_preddat); 10917 tp->rcv_nxt += tlen; 10918 if (tlen && 10919 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 10920 (tp->t_fbyte_in == 0)) { 10921 tp->t_fbyte_in = ticks; 10922 if (tp->t_fbyte_in == 0) 10923 tp->t_fbyte_in = 1; 10924 if (tp->t_fbyte_out && tp->t_fbyte_in) 10925 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 10926 } 10927 /* 10928 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 10929 */ 10930 tp->snd_wl1 = th->th_seq; 10931 /* 10932 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 10933 */ 10934 tp->rcv_up = tp->rcv_nxt; 10935 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs); 10936 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 10937 #ifdef TCPDEBUG 10938 if (so->so_options & SO_DEBUG) 10939 tcp_trace(TA_INPUT, ostate, tp, 10940 (void *)tcp_saveipgen, &tcp_savetcp, 0); 10941 #endif 10942 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 10943 10944 /* Add data to socket buffer. */ 10945 SOCKBUF_LOCK(&so->so_rcv); 10946 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 10947 m_freem(m); 10948 } else { 10949 /* 10950 * Set new socket buffer size. Give up when limit is 10951 * reached. 10952 */ 10953 if (newsize) 10954 if (!sbreserve_locked(&so->so_rcv, 10955 newsize, so, NULL)) 10956 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 10957 m_adj(m, drop_hdrlen); /* delayed header drop */ 10958 #ifdef NETFLIX_SB_LIMITS 10959 appended = 10960 #endif 10961 sbappendstream_locked(&so->so_rcv, m, 0); 10962 ctf_calc_rwin(so, tp); 10963 } 10964 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1); 10965 /* NB: sorwakeup_locked() does an implicit unlock. */ 10966 sorwakeup_locked(so); 10967 #ifdef NETFLIX_SB_LIMITS 10968 if (so->so_rcv.sb_shlim && mcnt != appended) 10969 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 10970 #endif 10971 rack_handle_delayed_ack(tp, rack, tlen, 0); 10972 if (tp->snd_una == tp->snd_max) 10973 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 10974 return (1); 10975 } 10976 10977 /* 10978 * This subfunction is used to try to highly optimize the 10979 * fast path. We again allow window updates that are 10980 * in sequence to remain in the fast-path. We also add 10981 * in the __predict's to attempt to help the compiler. 10982 * Note that if we return a 0, then we can *not* process 10983 * it and the caller should push the packet into the 10984 * slow-path. 10985 */ 10986 static int 10987 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 10988 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 10989 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts) 10990 { 10991 int32_t acked; 10992 int32_t nsegs; 10993 #ifdef TCPDEBUG 10994 /* 10995 * The size of tcp_saveipgen must be the size of the max ip header, 10996 * now IPv6. 10997 */ 10998 u_char tcp_saveipgen[IP6_HDR_LEN]; 10999 struct tcphdr tcp_savetcp; 11000 short ostate = 0; 11001 #endif 11002 int32_t under_pacing = 0; 11003 struct tcp_rack *rack; 11004 11005 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 11006 /* Old ack, behind (or duplicate to) the last one rcv'd */ 11007 return (0); 11008 } 11009 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 11010 /* Above what we have sent? */ 11011 return (0); 11012 } 11013 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 11014 /* We are retransmitting */ 11015 return (0); 11016 } 11017 if (__predict_false(tiwin == 0)) { 11018 /* zero window */ 11019 return (0); 11020 } 11021 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 11022 /* We need a SYN or a FIN, unlikely.. */ 11023 return (0); 11024 } 11025 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 11026 /* Timestamp is behind .. old ack with seq wrap? */ 11027 return (0); 11028 } 11029 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 11030 /* Still recovering */ 11031 return (0); 11032 } 11033 rack = (struct tcp_rack *)tp->t_fb_ptr; 11034 if (rack->r_ctl.rc_sacked) { 11035 /* We have sack holes on our scoreboard */ 11036 return (0); 11037 } 11038 /* Ok if we reach here, we can process a fast-ack */ 11039 if (rack->gp_ready && 11040 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11041 under_pacing = 1; 11042 } 11043 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11044 rack_log_ack(tp, to, th, 0, 0); 11045 /* Did the window get updated? */ 11046 if (tiwin != tp->snd_wnd) { 11047 tp->snd_wnd = tiwin; 11048 rack_validate_fo_sendwin_up(tp, rack); 11049 tp->snd_wl1 = th->th_seq; 11050 if (tp->snd_wnd > tp->max_sndwnd) 11051 tp->max_sndwnd = tp->snd_wnd; 11052 } 11053 /* Do we exit persists? */ 11054 if ((rack->rc_in_persist != 0) && 11055 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 11056 rack->r_ctl.rc_pace_min_segs))) { 11057 rack_exit_persist(tp, rack, cts); 11058 } 11059 /* Do we enter persists? */ 11060 if ((rack->rc_in_persist == 0) && 11061 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 11062 TCPS_HAVEESTABLISHED(tp->t_state) && 11063 (tp->snd_max == tp->snd_una) && 11064 sbavail(&tp->t_inpcb->inp_socket->so_snd) && 11065 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) { 11066 /* 11067 * Here the rwnd is less than 11068 * the pacing size, we are established, 11069 * nothing is outstanding, and there is 11070 * data to send. Enter persists. 11071 */ 11072 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime); 11073 } 11074 /* 11075 * If last ACK falls within this segment's sequence numbers, record 11076 * the timestamp. NOTE that the test is modified according to the 11077 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 11078 */ 11079 if ((to->to_flags & TOF_TS) != 0 && 11080 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 11081 tp->ts_recent_age = tcp_ts_getticks(); 11082 tp->ts_recent = to->to_tsval; 11083 } 11084 /* 11085 * This is a pure ack for outstanding data. 11086 */ 11087 KMOD_TCPSTAT_INC(tcps_predack); 11088 11089 /* 11090 * "bad retransmit" recovery. 11091 */ 11092 if ((tp->t_flags & TF_PREVVALID) && 11093 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 11094 tp->t_flags &= ~TF_PREVVALID; 11095 if (tp->t_rxtshift == 1 && 11096 (int)(ticks - tp->t_badrxtwin) < 0) 11097 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack); 11098 } 11099 /* 11100 * Recalculate the transmit timer / rtt. 11101 * 11102 * Some boxes send broken timestamp replies during the SYN+ACK 11103 * phase, ignore timestamps of 0 or we could calculate a huge RTT 11104 * and blow up the retransmit timer. 11105 */ 11106 acked = BYTES_THIS_ACK(tp, th); 11107 11108 #ifdef TCP_HHOOK 11109 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 11110 hhook_run_tcp_est_in(tp, th, to); 11111 #endif 11112 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs); 11113 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 11114 if (acked) { 11115 struct mbuf *mfree; 11116 11117 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0); 11118 SOCKBUF_LOCK(&so->so_snd); 11119 mfree = sbcut_locked(&so->so_snd, acked); 11120 tp->snd_una = th->th_ack; 11121 /* Note we want to hold the sb lock through the sendmap adjust */ 11122 rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una); 11123 /* Wake up the socket if we have room to write more */ 11124 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 11125 sowwakeup_locked(so); 11126 m_freem(mfree); 11127 tp->t_rxtshift = 0; 11128 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 11129 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 11130 rack->rc_tlp_in_progress = 0; 11131 rack->r_ctl.rc_tlp_cnt_out = 0; 11132 /* 11133 * If it is the RXT timer we want to 11134 * stop it, so we can restart a TLP. 11135 */ 11136 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 11137 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 11138 #ifdef NETFLIX_HTTP_LOGGING 11139 tcp_http_check_for_comp(rack->rc_tp, th->th_ack); 11140 #endif 11141 } 11142 /* 11143 * Let the congestion control algorithm update congestion control 11144 * related information. This typically means increasing the 11145 * congestion window. 11146 */ 11147 if (tp->snd_wnd < ctf_outstanding(tp)) { 11148 /* The peer collapsed the window */ 11149 rack_collapsed_window(rack); 11150 } else if (rack->rc_has_collapsed) 11151 rack_un_collapse_window(rack); 11152 11153 /* 11154 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 11155 */ 11156 tp->snd_wl2 = th->th_ack; 11157 tp->t_dupacks = 0; 11158 m_freem(m); 11159 /* ND6_HINT(tp); *//* Some progress has been made. */ 11160 11161 /* 11162 * If all outstanding data are acked, stop retransmit timer, 11163 * otherwise restart timer using current (possibly backed-off) 11164 * value. If process is waiting for space, wakeup/selwakeup/signal. 11165 * If data are ready to send, let tcp_output decide between more 11166 * output or persist. 11167 */ 11168 #ifdef TCPDEBUG 11169 if (so->so_options & SO_DEBUG) 11170 tcp_trace(TA_INPUT, ostate, tp, 11171 (void *)tcp_saveipgen, 11172 &tcp_savetcp, 0); 11173 #endif 11174 if (under_pacing && 11175 (rack->use_fixed_rate == 0) && 11176 (rack->in_probe_rtt == 0) && 11177 rack->rc_gp_dyn_mul && 11178 rack->rc_always_pace) { 11179 /* Check if we are dragging bottom */ 11180 rack_check_bottom_drag(tp, rack, so, acked); 11181 } 11182 if (tp->snd_una == tp->snd_max) { 11183 tp->t_flags &= ~TF_PREVVALID; 11184 rack->r_ctl.retran_during_recovery = 0; 11185 rack->r_ctl.dsack_byte_cnt = 0; 11186 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 11187 if (rack->r_ctl.rc_went_idle_time == 0) 11188 rack->r_ctl.rc_went_idle_time = 1; 11189 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 11190 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0) 11191 tp->t_acktime = 0; 11192 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 11193 } 11194 if (acked && rack->r_fast_output) 11195 rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked); 11196 if (sbavail(&so->so_snd)) { 11197 rack->r_wanted_output = 1; 11198 } 11199 return (1); 11200 } 11201 11202 /* 11203 * Return value of 1, the TCB is unlocked and most 11204 * likely gone, return value of 0, the TCP is still 11205 * locked. 11206 */ 11207 static int 11208 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 11209 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 11210 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 11211 { 11212 int32_t ret_val = 0; 11213 int32_t todrop; 11214 int32_t ourfinisacked = 0; 11215 struct tcp_rack *rack; 11216 11217 ctf_calc_rwin(so, tp); 11218 /* 11219 * If the state is SYN_SENT: if seg contains an ACK, but not for our 11220 * SYN, drop the input. if seg contains a RST, then drop the 11221 * connection. if seg does not contain SYN, then drop it. Otherwise 11222 * this is an acceptable SYN segment initialize tp->rcv_nxt and 11223 * tp->irs if seg contains ack then advance tp->snd_una if seg 11224 * contains an ECE and ECN support is enabled, the stream is ECN 11225 * capable. if SYN has been acked change to ESTABLISHED else 11226 * SYN_RCVD state arrange for segment to be acked (eventually) 11227 * continue processing rest of data/controls. 11228 */ 11229 if ((thflags & TH_ACK) && 11230 (SEQ_LEQ(th->th_ack, tp->iss) || 11231 SEQ_GT(th->th_ack, tp->snd_max))) { 11232 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11233 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11234 return (1); 11235 } 11236 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 11237 TCP_PROBE5(connect__refused, NULL, tp, 11238 mtod(m, const char *), tp, th); 11239 tp = tcp_drop(tp, ECONNREFUSED); 11240 ctf_do_drop(m, tp); 11241 return (1); 11242 } 11243 if (thflags & TH_RST) { 11244 ctf_do_drop(m, tp); 11245 return (1); 11246 } 11247 if (!(thflags & TH_SYN)) { 11248 ctf_do_drop(m, tp); 11249 return (1); 11250 } 11251 tp->irs = th->th_seq; 11252 tcp_rcvseqinit(tp); 11253 rack = (struct tcp_rack *)tp->t_fb_ptr; 11254 if (thflags & TH_ACK) { 11255 int tfo_partial = 0; 11256 11257 KMOD_TCPSTAT_INC(tcps_connects); 11258 soisconnected(so); 11259 #ifdef MAC 11260 mac_socketpeer_set_from_mbuf(m, so); 11261 #endif 11262 /* Do window scaling on this connection? */ 11263 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 11264 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 11265 tp->rcv_scale = tp->request_r_scale; 11266 } 11267 tp->rcv_adv += min(tp->rcv_wnd, 11268 TCP_MAXWIN << tp->rcv_scale); 11269 /* 11270 * If not all the data that was sent in the TFO SYN 11271 * has been acked, resend the remainder right away. 11272 */ 11273 if (IS_FASTOPEN(tp->t_flags) && 11274 (tp->snd_una != tp->snd_max)) { 11275 tp->snd_nxt = th->th_ack; 11276 tfo_partial = 1; 11277 } 11278 /* 11279 * If there's data, delay ACK; if there's also a FIN ACKNOW 11280 * will be turned on later. 11281 */ 11282 if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) { 11283 rack_timer_cancel(tp, rack, 11284 rack->r_ctl.rc_rcvtime, __LINE__); 11285 tp->t_flags |= TF_DELACK; 11286 } else { 11287 rack->r_wanted_output = 1; 11288 tp->t_flags |= TF_ACKNOW; 11289 rack->rc_dack_toggle = 0; 11290 } 11291 if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 11292 (V_tcp_do_ecn == 1)) { 11293 tp->t_flags2 |= TF2_ECN_PERMIT; 11294 KMOD_TCPSTAT_INC(tcps_ecn_shs); 11295 } 11296 if (SEQ_GT(th->th_ack, tp->snd_una)) { 11297 /* 11298 * We advance snd_una for the 11299 * fast open case. If th_ack is 11300 * acknowledging data beyond 11301 * snd_una we can't just call 11302 * ack-processing since the 11303 * data stream in our send-map 11304 * will start at snd_una + 1 (one 11305 * beyond the SYN). If its just 11306 * equal we don't need to do that 11307 * and there is no send_map. 11308 */ 11309 tp->snd_una++; 11310 } 11311 /* 11312 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 11313 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 11314 */ 11315 tp->t_starttime = ticks; 11316 if (tp->t_flags & TF_NEEDFIN) { 11317 tcp_state_change(tp, TCPS_FIN_WAIT_1); 11318 tp->t_flags &= ~TF_NEEDFIN; 11319 thflags &= ~TH_SYN; 11320 } else { 11321 tcp_state_change(tp, TCPS_ESTABLISHED); 11322 TCP_PROBE5(connect__established, NULL, tp, 11323 mtod(m, const char *), tp, th); 11324 rack_cc_conn_init(tp); 11325 } 11326 } else { 11327 /* 11328 * Received initial SYN in SYN-SENT[*] state => simultaneous 11329 * open. If segment contains CC option and there is a 11330 * cached CC, apply TAO test. If it succeeds, connection is * 11331 * half-synchronized. Otherwise, do 3-way handshake: 11332 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 11333 * there was no CC option, clear cached CC value. 11334 */ 11335 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 11336 tcp_state_change(tp, TCPS_SYN_RECEIVED); 11337 } 11338 INP_WLOCK_ASSERT(tp->t_inpcb); 11339 /* 11340 * Advance th->th_seq to correspond to first data byte. If data, 11341 * trim to stay within window, dropping FIN if necessary. 11342 */ 11343 th->th_seq++; 11344 if (tlen > tp->rcv_wnd) { 11345 todrop = tlen - tp->rcv_wnd; 11346 m_adj(m, -todrop); 11347 tlen = tp->rcv_wnd; 11348 thflags &= ~TH_FIN; 11349 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 11350 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 11351 } 11352 tp->snd_wl1 = th->th_seq - 1; 11353 tp->rcv_up = th->th_seq; 11354 /* 11355 * Client side of transaction: already sent SYN and data. If the 11356 * remote host used T/TCP to validate the SYN, our data will be 11357 * ACK'd; if so, enter normal data segment processing in the middle 11358 * of step 5, ack processing. Otherwise, goto step 6. 11359 */ 11360 if (thflags & TH_ACK) { 11361 /* For syn-sent we need to possibly update the rtt */ 11362 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 11363 uint32_t t, mcts; 11364 11365 mcts = tcp_ts_getticks(); 11366 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC; 11367 if (!tp->t_rttlow || tp->t_rttlow > t) 11368 tp->t_rttlow = t; 11369 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4); 11370 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2); 11371 tcp_rack_xmit_timer_commit(rack, tp); 11372 } 11373 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 11374 return (ret_val); 11375 /* We may have changed to FIN_WAIT_1 above */ 11376 if (tp->t_state == TCPS_FIN_WAIT_1) { 11377 /* 11378 * In FIN_WAIT_1 STATE in addition to the processing 11379 * for the ESTABLISHED state if our FIN is now 11380 * acknowledged then enter FIN_WAIT_2. 11381 */ 11382 if (ourfinisacked) { 11383 /* 11384 * If we can't receive any more data, then 11385 * closing user can proceed. Starting the 11386 * timer is contrary to the specification, 11387 * but if we don't get a FIN we'll hang 11388 * forever. 11389 * 11390 * XXXjl: we should release the tp also, and 11391 * use a compressed state. 11392 */ 11393 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 11394 soisdisconnected(so); 11395 tcp_timer_activate(tp, TT_2MSL, 11396 (tcp_fast_finwait2_recycle ? 11397 tcp_finwait2_timeout : 11398 TP_MAXIDLE(tp))); 11399 } 11400 tcp_state_change(tp, TCPS_FIN_WAIT_2); 11401 } 11402 } 11403 } 11404 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11405 tiwin, thflags, nxt_pkt)); 11406 } 11407 11408 /* 11409 * Return value of 1, the TCB is unlocked and most 11410 * likely gone, return value of 0, the TCP is still 11411 * locked. 11412 */ 11413 static int 11414 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 11415 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 11416 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 11417 { 11418 struct tcp_rack *rack; 11419 int32_t ret_val = 0; 11420 int32_t ourfinisacked = 0; 11421 11422 ctf_calc_rwin(so, tp); 11423 if ((thflags & TH_ACK) && 11424 (SEQ_LEQ(th->th_ack, tp->snd_una) || 11425 SEQ_GT(th->th_ack, tp->snd_max))) { 11426 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11427 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11428 return (1); 11429 } 11430 rack = (struct tcp_rack *)tp->t_fb_ptr; 11431 if (IS_FASTOPEN(tp->t_flags)) { 11432 /* 11433 * When a TFO connection is in SYN_RECEIVED, the 11434 * only valid packets are the initial SYN, a 11435 * retransmit/copy of the initial SYN (possibly with 11436 * a subset of the original data), a valid ACK, a 11437 * FIN, or a RST. 11438 */ 11439 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 11440 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11441 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11442 return (1); 11443 } else if (thflags & TH_SYN) { 11444 /* non-initial SYN is ignored */ 11445 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 11446 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 11447 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 11448 ctf_do_drop(m, NULL); 11449 return (0); 11450 } 11451 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 11452 ctf_do_drop(m, NULL); 11453 return (0); 11454 } 11455 } 11456 if ((thflags & TH_RST) || 11457 (tp->t_fin_is_rst && (thflags & TH_FIN))) 11458 return (ctf_process_rst(m, th, so, tp)); 11459 /* 11460 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 11461 * it's less than ts_recent, drop it. 11462 */ 11463 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 11464 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 11465 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 11466 return (ret_val); 11467 } 11468 /* 11469 * In the SYN-RECEIVED state, validate that the packet belongs to 11470 * this connection before trimming the data to fit the receive 11471 * window. Check the sequence number versus IRS since we know the 11472 * sequence numbers haven't wrapped. This is a partial fix for the 11473 * "LAND" DoS attack. 11474 */ 11475 if (SEQ_LT(th->th_seq, tp->irs)) { 11476 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11477 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11478 return (1); 11479 } 11480 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 11481 &rack->r_ctl.challenge_ack_ts, 11482 &rack->r_ctl.challenge_ack_cnt)) { 11483 return (ret_val); 11484 } 11485 /* 11486 * If last ACK falls within this segment's sequence numbers, record 11487 * its timestamp. NOTE: 1) That the test incorporates suggestions 11488 * from the latest proposal of the tcplw@cray.com list (Braden 11489 * 1993/04/26). 2) That updating only on newer timestamps interferes 11490 * with our earlier PAWS tests, so this check should be solely 11491 * predicated on the sequence space of this segment. 3) That we 11492 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 11493 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 11494 * SEG.Len, This modified check allows us to overcome RFC1323's 11495 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 11496 * p.869. In such cases, we can still calculate the RTT correctly 11497 * when RCV.NXT == Last.ACK.Sent. 11498 */ 11499 if ((to->to_flags & TOF_TS) != 0 && 11500 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 11501 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 11502 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 11503 tp->ts_recent_age = tcp_ts_getticks(); 11504 tp->ts_recent = to->to_tsval; 11505 } 11506 tp->snd_wnd = tiwin; 11507 rack_validate_fo_sendwin_up(tp, rack); 11508 /* 11509 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 11510 * is on (half-synchronized state), then queue data for later 11511 * processing; else drop segment and return. 11512 */ 11513 if ((thflags & TH_ACK) == 0) { 11514 if (IS_FASTOPEN(tp->t_flags)) { 11515 rack_cc_conn_init(tp); 11516 } 11517 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11518 tiwin, thflags, nxt_pkt)); 11519 } 11520 KMOD_TCPSTAT_INC(tcps_connects); 11521 soisconnected(so); 11522 /* Do window scaling? */ 11523 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 11524 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 11525 tp->rcv_scale = tp->request_r_scale; 11526 } 11527 /* 11528 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 11529 * FIN-WAIT-1 11530 */ 11531 tp->t_starttime = ticks; 11532 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 11533 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 11534 tp->t_tfo_pending = NULL; 11535 } 11536 if (tp->t_flags & TF_NEEDFIN) { 11537 tcp_state_change(tp, TCPS_FIN_WAIT_1); 11538 tp->t_flags &= ~TF_NEEDFIN; 11539 } else { 11540 tcp_state_change(tp, TCPS_ESTABLISHED); 11541 TCP_PROBE5(accept__established, NULL, tp, 11542 mtod(m, const char *), tp, th); 11543 /* 11544 * TFO connections call cc_conn_init() during SYN 11545 * processing. Calling it again here for such connections 11546 * is not harmless as it would undo the snd_cwnd reduction 11547 * that occurs when a TFO SYN|ACK is retransmitted. 11548 */ 11549 if (!IS_FASTOPEN(tp->t_flags)) 11550 rack_cc_conn_init(tp); 11551 } 11552 /* 11553 * Account for the ACK of our SYN prior to 11554 * regular ACK processing below, except for 11555 * simultaneous SYN, which is handled later. 11556 */ 11557 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 11558 tp->snd_una++; 11559 /* 11560 * If segment contains data or ACK, will call tcp_reass() later; if 11561 * not, do so now to pass queued data to user. 11562 */ 11563 if (tlen == 0 && (thflags & TH_FIN) == 0) { 11564 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 11565 (struct mbuf *)0); 11566 if (tp->t_flags & TF_WAKESOR) { 11567 tp->t_flags &= ~TF_WAKESOR; 11568 /* NB: sorwakeup_locked() does an implicit unlock. */ 11569 sorwakeup_locked(so); 11570 } 11571 } 11572 tp->snd_wl1 = th->th_seq - 1; 11573 /* For syn-recv we need to possibly update the rtt */ 11574 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 11575 uint32_t t, mcts; 11576 11577 mcts = tcp_ts_getticks(); 11578 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC; 11579 if (!tp->t_rttlow || tp->t_rttlow > t) 11580 tp->t_rttlow = t; 11581 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5); 11582 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2); 11583 tcp_rack_xmit_timer_commit(rack, tp); 11584 } 11585 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 11586 return (ret_val); 11587 } 11588 if (tp->t_state == TCPS_FIN_WAIT_1) { 11589 /* We could have went to FIN_WAIT_1 (or EST) above */ 11590 /* 11591 * In FIN_WAIT_1 STATE in addition to the processing for the 11592 * ESTABLISHED state if our FIN is now acknowledged then 11593 * enter FIN_WAIT_2. 11594 */ 11595 if (ourfinisacked) { 11596 /* 11597 * If we can't receive any more data, then closing 11598 * user can proceed. Starting the timer is contrary 11599 * to the specification, but if we don't get a FIN 11600 * we'll hang forever. 11601 * 11602 * XXXjl: we should release the tp also, and use a 11603 * compressed state. 11604 */ 11605 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 11606 soisdisconnected(so); 11607 tcp_timer_activate(tp, TT_2MSL, 11608 (tcp_fast_finwait2_recycle ? 11609 tcp_finwait2_timeout : 11610 TP_MAXIDLE(tp))); 11611 } 11612 tcp_state_change(tp, TCPS_FIN_WAIT_2); 11613 } 11614 } 11615 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11616 tiwin, thflags, nxt_pkt)); 11617 } 11618 11619 /* 11620 * Return value of 1, the TCB is unlocked and most 11621 * likely gone, return value of 0, the TCP is still 11622 * locked. 11623 */ 11624 static int 11625 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 11626 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 11627 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 11628 { 11629 int32_t ret_val = 0; 11630 struct tcp_rack *rack; 11631 11632 /* 11633 * Header prediction: check for the two common cases of a 11634 * uni-directional data xfer. If the packet has no control flags, 11635 * is in-sequence, the window didn't change and we're not 11636 * retransmitting, it's a candidate. If the length is zero and the 11637 * ack moved forward, we're the sender side of the xfer. Just free 11638 * the data acked & wake any higher level process that was blocked 11639 * waiting for space. If the length is non-zero and the ack didn't 11640 * move, we're the receiver side. If we're getting packets in-order 11641 * (the reassembly queue is empty), add the data toc The socket 11642 * buffer and note that we need a delayed ack. Make sure that the 11643 * hidden state-flags are also off. Since we check for 11644 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 11645 */ 11646 rack = (struct tcp_rack *)tp->t_fb_ptr; 11647 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 11648 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) && 11649 __predict_true(SEGQ_EMPTY(tp)) && 11650 __predict_true(th->th_seq == tp->rcv_nxt)) { 11651 if (tlen == 0) { 11652 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 11653 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) { 11654 return (0); 11655 } 11656 } else { 11657 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 11658 tiwin, nxt_pkt, iptos)) { 11659 return (0); 11660 } 11661 } 11662 } 11663 ctf_calc_rwin(so, tp); 11664 11665 if ((thflags & TH_RST) || 11666 (tp->t_fin_is_rst && (thflags & TH_FIN))) 11667 return (ctf_process_rst(m, th, so, tp)); 11668 11669 /* 11670 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 11671 * synchronized state. 11672 */ 11673 if (thflags & TH_SYN) { 11674 ctf_challenge_ack(m, th, tp, &ret_val); 11675 return (ret_val); 11676 } 11677 /* 11678 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 11679 * it's less than ts_recent, drop it. 11680 */ 11681 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 11682 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 11683 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 11684 return (ret_val); 11685 } 11686 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 11687 &rack->r_ctl.challenge_ack_ts, 11688 &rack->r_ctl.challenge_ack_cnt)) { 11689 return (ret_val); 11690 } 11691 /* 11692 * If last ACK falls within this segment's sequence numbers, record 11693 * its timestamp. NOTE: 1) That the test incorporates suggestions 11694 * from the latest proposal of the tcplw@cray.com list (Braden 11695 * 1993/04/26). 2) That updating only on newer timestamps interferes 11696 * with our earlier PAWS tests, so this check should be solely 11697 * predicated on the sequence space of this segment. 3) That we 11698 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 11699 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 11700 * SEG.Len, This modified check allows us to overcome RFC1323's 11701 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 11702 * p.869. In such cases, we can still calculate the RTT correctly 11703 * when RCV.NXT == Last.ACK.Sent. 11704 */ 11705 if ((to->to_flags & TOF_TS) != 0 && 11706 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 11707 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 11708 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 11709 tp->ts_recent_age = tcp_ts_getticks(); 11710 tp->ts_recent = to->to_tsval; 11711 } 11712 /* 11713 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 11714 * is on (half-synchronized state), then queue data for later 11715 * processing; else drop segment and return. 11716 */ 11717 if ((thflags & TH_ACK) == 0) { 11718 if (tp->t_flags & TF_NEEDSYN) { 11719 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11720 tiwin, thflags, nxt_pkt)); 11721 11722 } else if (tp->t_flags & TF_ACKNOW) { 11723 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 11724 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 11725 return (ret_val); 11726 } else { 11727 ctf_do_drop(m, NULL); 11728 return (0); 11729 } 11730 } 11731 /* 11732 * Ack processing. 11733 */ 11734 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 11735 return (ret_val); 11736 } 11737 if (sbavail(&so->so_snd)) { 11738 if (ctf_progress_timeout_check(tp, true)) { 11739 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 11740 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 11741 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11742 return (1); 11743 } 11744 } 11745 /* State changes only happen in rack_process_data() */ 11746 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11747 tiwin, thflags, nxt_pkt)); 11748 } 11749 11750 /* 11751 * Return value of 1, the TCB is unlocked and most 11752 * likely gone, return value of 0, the TCP is still 11753 * locked. 11754 */ 11755 static int 11756 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 11757 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 11758 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 11759 { 11760 int32_t ret_val = 0; 11761 struct tcp_rack *rack; 11762 11763 rack = (struct tcp_rack *)tp->t_fb_ptr; 11764 ctf_calc_rwin(so, tp); 11765 if ((thflags & TH_RST) || 11766 (tp->t_fin_is_rst && (thflags & TH_FIN))) 11767 return (ctf_process_rst(m, th, so, tp)); 11768 /* 11769 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 11770 * synchronized state. 11771 */ 11772 if (thflags & TH_SYN) { 11773 ctf_challenge_ack(m, th, tp, &ret_val); 11774 return (ret_val); 11775 } 11776 /* 11777 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 11778 * it's less than ts_recent, drop it. 11779 */ 11780 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 11781 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 11782 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 11783 return (ret_val); 11784 } 11785 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 11786 &rack->r_ctl.challenge_ack_ts, 11787 &rack->r_ctl.challenge_ack_cnt)) { 11788 return (ret_val); 11789 } 11790 /* 11791 * If last ACK falls within this segment's sequence numbers, record 11792 * its timestamp. NOTE: 1) That the test incorporates suggestions 11793 * from the latest proposal of the tcplw@cray.com list (Braden 11794 * 1993/04/26). 2) That updating only on newer timestamps interferes 11795 * with our earlier PAWS tests, so this check should be solely 11796 * predicated on the sequence space of this segment. 3) That we 11797 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 11798 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 11799 * SEG.Len, This modified check allows us to overcome RFC1323's 11800 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 11801 * p.869. In such cases, we can still calculate the RTT correctly 11802 * when RCV.NXT == Last.ACK.Sent. 11803 */ 11804 if ((to->to_flags & TOF_TS) != 0 && 11805 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 11806 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 11807 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 11808 tp->ts_recent_age = tcp_ts_getticks(); 11809 tp->ts_recent = to->to_tsval; 11810 } 11811 /* 11812 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 11813 * is on (half-synchronized state), then queue data for later 11814 * processing; else drop segment and return. 11815 */ 11816 if ((thflags & TH_ACK) == 0) { 11817 if (tp->t_flags & TF_NEEDSYN) { 11818 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11819 tiwin, thflags, nxt_pkt)); 11820 11821 } else if (tp->t_flags & TF_ACKNOW) { 11822 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 11823 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 11824 return (ret_val); 11825 } else { 11826 ctf_do_drop(m, NULL); 11827 return (0); 11828 } 11829 } 11830 /* 11831 * Ack processing. 11832 */ 11833 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 11834 return (ret_val); 11835 } 11836 if (sbavail(&so->so_snd)) { 11837 if (ctf_progress_timeout_check(tp, true)) { 11838 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 11839 tp, tick, PROGRESS_DROP, __LINE__); 11840 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 11841 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11842 return (1); 11843 } 11844 } 11845 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11846 tiwin, thflags, nxt_pkt)); 11847 } 11848 11849 static int 11850 rack_check_data_after_close(struct mbuf *m, 11851 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so) 11852 { 11853 struct tcp_rack *rack; 11854 11855 rack = (struct tcp_rack *)tp->t_fb_ptr; 11856 if (rack->rc_allow_data_af_clo == 0) { 11857 close_now: 11858 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 11859 /* tcp_close will kill the inp pre-log the Reset */ 11860 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 11861 tp = tcp_close(tp); 11862 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 11863 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 11864 return (1); 11865 } 11866 if (sbavail(&so->so_snd) == 0) 11867 goto close_now; 11868 /* Ok we allow data that is ignored and a followup reset */ 11869 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 11870 tp->rcv_nxt = th->th_seq + *tlen; 11871 tp->t_flags2 |= TF2_DROP_AF_DATA; 11872 rack->r_wanted_output = 1; 11873 *tlen = 0; 11874 return (0); 11875 } 11876 11877 /* 11878 * Return value of 1, the TCB is unlocked and most 11879 * likely gone, return value of 0, the TCP is still 11880 * locked. 11881 */ 11882 static int 11883 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 11884 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 11885 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 11886 { 11887 int32_t ret_val = 0; 11888 int32_t ourfinisacked = 0; 11889 struct tcp_rack *rack; 11890 11891 rack = (struct tcp_rack *)tp->t_fb_ptr; 11892 ctf_calc_rwin(so, tp); 11893 11894 if ((thflags & TH_RST) || 11895 (tp->t_fin_is_rst && (thflags & TH_FIN))) 11896 return (ctf_process_rst(m, th, so, tp)); 11897 /* 11898 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 11899 * synchronized state. 11900 */ 11901 if (thflags & TH_SYN) { 11902 ctf_challenge_ack(m, th, tp, &ret_val); 11903 return (ret_val); 11904 } 11905 /* 11906 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 11907 * it's less than ts_recent, drop it. 11908 */ 11909 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 11910 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 11911 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 11912 return (ret_val); 11913 } 11914 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 11915 &rack->r_ctl.challenge_ack_ts, 11916 &rack->r_ctl.challenge_ack_cnt)) { 11917 return (ret_val); 11918 } 11919 /* 11920 * If new data are received on a connection after the user processes 11921 * are gone, then RST the other end. 11922 */ 11923 if ((so->so_state & SS_NOFDREF) && tlen) { 11924 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 11925 return (1); 11926 } 11927 /* 11928 * If last ACK falls within this segment's sequence numbers, record 11929 * its timestamp. NOTE: 1) That the test incorporates suggestions 11930 * from the latest proposal of the tcplw@cray.com list (Braden 11931 * 1993/04/26). 2) That updating only on newer timestamps interferes 11932 * with our earlier PAWS tests, so this check should be solely 11933 * predicated on the sequence space of this segment. 3) That we 11934 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 11935 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 11936 * SEG.Len, This modified check allows us to overcome RFC1323's 11937 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 11938 * p.869. In such cases, we can still calculate the RTT correctly 11939 * when RCV.NXT == Last.ACK.Sent. 11940 */ 11941 if ((to->to_flags & TOF_TS) != 0 && 11942 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 11943 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 11944 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 11945 tp->ts_recent_age = tcp_ts_getticks(); 11946 tp->ts_recent = to->to_tsval; 11947 } 11948 /* 11949 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 11950 * is on (half-synchronized state), then queue data for later 11951 * processing; else drop segment and return. 11952 */ 11953 if ((thflags & TH_ACK) == 0) { 11954 if (tp->t_flags & TF_NEEDSYN) { 11955 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 11956 tiwin, thflags, nxt_pkt)); 11957 } else if (tp->t_flags & TF_ACKNOW) { 11958 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 11959 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 11960 return (ret_val); 11961 } else { 11962 ctf_do_drop(m, NULL); 11963 return (0); 11964 } 11965 } 11966 /* 11967 * Ack processing. 11968 */ 11969 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 11970 return (ret_val); 11971 } 11972 if (ourfinisacked) { 11973 /* 11974 * If we can't receive any more data, then closing user can 11975 * proceed. Starting the timer is contrary to the 11976 * specification, but if we don't get a FIN we'll hang 11977 * forever. 11978 * 11979 * XXXjl: we should release the tp also, and use a 11980 * compressed state. 11981 */ 11982 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 11983 soisdisconnected(so); 11984 tcp_timer_activate(tp, TT_2MSL, 11985 (tcp_fast_finwait2_recycle ? 11986 tcp_finwait2_timeout : 11987 TP_MAXIDLE(tp))); 11988 } 11989 tcp_state_change(tp, TCPS_FIN_WAIT_2); 11990 } 11991 if (sbavail(&so->so_snd)) { 11992 if (ctf_progress_timeout_check(tp, true)) { 11993 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 11994 tp, tick, PROGRESS_DROP, __LINE__); 11995 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 11996 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11997 return (1); 11998 } 11999 } 12000 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12001 tiwin, thflags, nxt_pkt)); 12002 } 12003 12004 /* 12005 * Return value of 1, the TCB is unlocked and most 12006 * likely gone, return value of 0, the TCP is still 12007 * locked. 12008 */ 12009 static int 12010 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 12011 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 12012 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 12013 { 12014 int32_t ret_val = 0; 12015 int32_t ourfinisacked = 0; 12016 struct tcp_rack *rack; 12017 12018 rack = (struct tcp_rack *)tp->t_fb_ptr; 12019 ctf_calc_rwin(so, tp); 12020 12021 if ((thflags & TH_RST) || 12022 (tp->t_fin_is_rst && (thflags & TH_FIN))) 12023 return (ctf_process_rst(m, th, so, tp)); 12024 /* 12025 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 12026 * synchronized state. 12027 */ 12028 if (thflags & TH_SYN) { 12029 ctf_challenge_ack(m, th, tp, &ret_val); 12030 return (ret_val); 12031 } 12032 /* 12033 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 12034 * it's less than ts_recent, drop it. 12035 */ 12036 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 12037 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 12038 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 12039 return (ret_val); 12040 } 12041 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 12042 &rack->r_ctl.challenge_ack_ts, 12043 &rack->r_ctl.challenge_ack_cnt)) { 12044 return (ret_val); 12045 } 12046 /* 12047 * If new data are received on a connection after the user processes 12048 * are gone, then RST the other end. 12049 */ 12050 if ((so->so_state & SS_NOFDREF) && tlen) { 12051 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 12052 return (1); 12053 } 12054 /* 12055 * If last ACK falls within this segment's sequence numbers, record 12056 * its timestamp. NOTE: 1) That the test incorporates suggestions 12057 * from the latest proposal of the tcplw@cray.com list (Braden 12058 * 1993/04/26). 2) That updating only on newer timestamps interferes 12059 * with our earlier PAWS tests, so this check should be solely 12060 * predicated on the sequence space of this segment. 3) That we 12061 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 12062 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 12063 * SEG.Len, This modified check allows us to overcome RFC1323's 12064 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 12065 * p.869. In such cases, we can still calculate the RTT correctly 12066 * when RCV.NXT == Last.ACK.Sent. 12067 */ 12068 if ((to->to_flags & TOF_TS) != 0 && 12069 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 12070 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 12071 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 12072 tp->ts_recent_age = tcp_ts_getticks(); 12073 tp->ts_recent = to->to_tsval; 12074 } 12075 /* 12076 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 12077 * is on (half-synchronized state), then queue data for later 12078 * processing; else drop segment and return. 12079 */ 12080 if ((thflags & TH_ACK) == 0) { 12081 if (tp->t_flags & TF_NEEDSYN) { 12082 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12083 tiwin, thflags, nxt_pkt)); 12084 } else if (tp->t_flags & TF_ACKNOW) { 12085 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 12086 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 12087 return (ret_val); 12088 } else { 12089 ctf_do_drop(m, NULL); 12090 return (0); 12091 } 12092 } 12093 /* 12094 * Ack processing. 12095 */ 12096 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 12097 return (ret_val); 12098 } 12099 if (ourfinisacked) { 12100 tcp_twstart(tp); 12101 m_freem(m); 12102 return (1); 12103 } 12104 if (sbavail(&so->so_snd)) { 12105 if (ctf_progress_timeout_check(tp, true)) { 12106 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 12107 tp, tick, PROGRESS_DROP, __LINE__); 12108 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 12109 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 12110 return (1); 12111 } 12112 } 12113 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12114 tiwin, thflags, nxt_pkt)); 12115 } 12116 12117 /* 12118 * Return value of 1, the TCB is unlocked and most 12119 * likely gone, return value of 0, the TCP is still 12120 * locked. 12121 */ 12122 static int 12123 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 12124 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 12125 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 12126 { 12127 int32_t ret_val = 0; 12128 int32_t ourfinisacked = 0; 12129 struct tcp_rack *rack; 12130 12131 rack = (struct tcp_rack *)tp->t_fb_ptr; 12132 ctf_calc_rwin(so, tp); 12133 12134 if ((thflags & TH_RST) || 12135 (tp->t_fin_is_rst && (thflags & TH_FIN))) 12136 return (ctf_process_rst(m, th, so, tp)); 12137 /* 12138 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 12139 * synchronized state. 12140 */ 12141 if (thflags & TH_SYN) { 12142 ctf_challenge_ack(m, th, tp, &ret_val); 12143 return (ret_val); 12144 } 12145 /* 12146 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 12147 * it's less than ts_recent, drop it. 12148 */ 12149 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 12150 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 12151 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 12152 return (ret_val); 12153 } 12154 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 12155 &rack->r_ctl.challenge_ack_ts, 12156 &rack->r_ctl.challenge_ack_cnt)) { 12157 return (ret_val); 12158 } 12159 /* 12160 * If new data are received on a connection after the user processes 12161 * are gone, then RST the other end. 12162 */ 12163 if ((so->so_state & SS_NOFDREF) && tlen) { 12164 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 12165 return (1); 12166 } 12167 /* 12168 * If last ACK falls within this segment's sequence numbers, record 12169 * its timestamp. NOTE: 1) That the test incorporates suggestions 12170 * from the latest proposal of the tcplw@cray.com list (Braden 12171 * 1993/04/26). 2) That updating only on newer timestamps interferes 12172 * with our earlier PAWS tests, so this check should be solely 12173 * predicated on the sequence space of this segment. 3) That we 12174 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 12175 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 12176 * SEG.Len, This modified check allows us to overcome RFC1323's 12177 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 12178 * p.869. In such cases, we can still calculate the RTT correctly 12179 * when RCV.NXT == Last.ACK.Sent. 12180 */ 12181 if ((to->to_flags & TOF_TS) != 0 && 12182 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 12183 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 12184 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 12185 tp->ts_recent_age = tcp_ts_getticks(); 12186 tp->ts_recent = to->to_tsval; 12187 } 12188 /* 12189 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 12190 * is on (half-synchronized state), then queue data for later 12191 * processing; else drop segment and return. 12192 */ 12193 if ((thflags & TH_ACK) == 0) { 12194 if (tp->t_flags & TF_NEEDSYN) { 12195 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12196 tiwin, thflags, nxt_pkt)); 12197 } else if (tp->t_flags & TF_ACKNOW) { 12198 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 12199 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 12200 return (ret_val); 12201 } else { 12202 ctf_do_drop(m, NULL); 12203 return (0); 12204 } 12205 } 12206 /* 12207 * case TCPS_LAST_ACK: Ack processing. 12208 */ 12209 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 12210 return (ret_val); 12211 } 12212 if (ourfinisacked) { 12213 tp = tcp_close(tp); 12214 ctf_do_drop(m, tp); 12215 return (1); 12216 } 12217 if (sbavail(&so->so_snd)) { 12218 if (ctf_progress_timeout_check(tp, true)) { 12219 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 12220 tp, tick, PROGRESS_DROP, __LINE__); 12221 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 12222 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 12223 return (1); 12224 } 12225 } 12226 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12227 tiwin, thflags, nxt_pkt)); 12228 } 12229 12230 /* 12231 * Return value of 1, the TCB is unlocked and most 12232 * likely gone, return value of 0, the TCP is still 12233 * locked. 12234 */ 12235 static int 12236 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 12237 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 12238 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 12239 { 12240 int32_t ret_val = 0; 12241 int32_t ourfinisacked = 0; 12242 struct tcp_rack *rack; 12243 12244 rack = (struct tcp_rack *)tp->t_fb_ptr; 12245 ctf_calc_rwin(so, tp); 12246 12247 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 12248 if ((thflags & TH_RST) || 12249 (tp->t_fin_is_rst && (thflags & TH_FIN))) 12250 return (ctf_process_rst(m, th, so, tp)); 12251 /* 12252 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 12253 * synchronized state. 12254 */ 12255 if (thflags & TH_SYN) { 12256 ctf_challenge_ack(m, th, tp, &ret_val); 12257 return (ret_val); 12258 } 12259 /* 12260 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 12261 * it's less than ts_recent, drop it. 12262 */ 12263 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 12264 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 12265 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 12266 return (ret_val); 12267 } 12268 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 12269 &rack->r_ctl.challenge_ack_ts, 12270 &rack->r_ctl.challenge_ack_cnt)) { 12271 return (ret_val); 12272 } 12273 /* 12274 * If new data are received on a connection after the user processes 12275 * are gone, then RST the other end. 12276 */ 12277 if ((so->so_state & SS_NOFDREF) && 12278 tlen) { 12279 if (rack_check_data_after_close(m, tp, &tlen, th, so)) 12280 return (1); 12281 } 12282 /* 12283 * If last ACK falls within this segment's sequence numbers, record 12284 * its timestamp. NOTE: 1) That the test incorporates suggestions 12285 * from the latest proposal of the tcplw@cray.com list (Braden 12286 * 1993/04/26). 2) That updating only on newer timestamps interferes 12287 * with our earlier PAWS tests, so this check should be solely 12288 * predicated on the sequence space of this segment. 3) That we 12289 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 12290 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 12291 * SEG.Len, This modified check allows us to overcome RFC1323's 12292 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 12293 * p.869. In such cases, we can still calculate the RTT correctly 12294 * when RCV.NXT == Last.ACK.Sent. 12295 */ 12296 if ((to->to_flags & TOF_TS) != 0 && 12297 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 12298 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 12299 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 12300 tp->ts_recent_age = tcp_ts_getticks(); 12301 tp->ts_recent = to->to_tsval; 12302 } 12303 /* 12304 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 12305 * is on (half-synchronized state), then queue data for later 12306 * processing; else drop segment and return. 12307 */ 12308 if ((thflags & TH_ACK) == 0) { 12309 if (tp->t_flags & TF_NEEDSYN) { 12310 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12311 tiwin, thflags, nxt_pkt)); 12312 } else if (tp->t_flags & TF_ACKNOW) { 12313 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 12314 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 12315 return (ret_val); 12316 } else { 12317 ctf_do_drop(m, NULL); 12318 return (0); 12319 } 12320 } 12321 /* 12322 * Ack processing. 12323 */ 12324 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 12325 return (ret_val); 12326 } 12327 if (sbavail(&so->so_snd)) { 12328 if (ctf_progress_timeout_check(tp, true)) { 12329 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 12330 tp, tick, PROGRESS_DROP, __LINE__); 12331 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 12332 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 12333 return (1); 12334 } 12335 } 12336 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 12337 tiwin, thflags, nxt_pkt)); 12338 } 12339 12340 static void inline 12341 rack_clear_rate_sample(struct tcp_rack *rack) 12342 { 12343 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY; 12344 rack->r_ctl.rack_rs.rs_rtt_cnt = 0; 12345 rack->r_ctl.rack_rs.rs_rtt_tot = 0; 12346 } 12347 12348 static void 12349 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override) 12350 { 12351 uint64_t bw_est, rate_wanted; 12352 int chged = 0; 12353 uint32_t user_max, orig_min, orig_max; 12354 12355 orig_min = rack->r_ctl.rc_pace_min_segs; 12356 orig_max = rack->r_ctl.rc_pace_max_segs; 12357 user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs; 12358 if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs) 12359 chged = 1; 12360 rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp); 12361 if (rack->use_fixed_rate || rack->rc_force_max_seg) { 12362 if (user_max != rack->r_ctl.rc_pace_max_segs) 12363 chged = 1; 12364 } 12365 if (rack->rc_force_max_seg) { 12366 rack->r_ctl.rc_pace_max_segs = user_max; 12367 } else if (rack->use_fixed_rate) { 12368 bw_est = rack_get_bw(rack); 12369 if ((rack->r_ctl.crte == NULL) || 12370 (bw_est != rack->r_ctl.crte->rate)) { 12371 rack->r_ctl.rc_pace_max_segs = user_max; 12372 } else { 12373 /* We are pacing right at the hardware rate */ 12374 uint32_t segsiz; 12375 12376 segsiz = min(ctf_fixed_maxseg(tp), 12377 rack->r_ctl.rc_pace_min_segs); 12378 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size( 12379 tp, bw_est, segsiz, 0, 12380 rack->r_ctl.crte, NULL); 12381 } 12382 } else if (rack->rc_always_pace) { 12383 if (rack->r_ctl.gp_bw || 12384 #ifdef NETFLIX_PEAKRATE 12385 rack->rc_tp->t_maxpeakrate || 12386 #endif 12387 rack->r_ctl.init_rate) { 12388 /* We have a rate of some sort set */ 12389 uint32_t orig; 12390 12391 bw_est = rack_get_bw(rack); 12392 orig = rack->r_ctl.rc_pace_max_segs; 12393 if (fill_override) 12394 rate_wanted = *fill_override; 12395 else 12396 rate_wanted = rack_get_output_bw(rack, bw_est, NULL, NULL); 12397 if (rate_wanted) { 12398 /* We have something */ 12399 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, 12400 rate_wanted, 12401 ctf_fixed_maxseg(rack->rc_tp)); 12402 } else 12403 rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs; 12404 if (orig != rack->r_ctl.rc_pace_max_segs) 12405 chged = 1; 12406 } else if ((rack->r_ctl.gp_bw == 0) && 12407 (rack->r_ctl.rc_pace_max_segs == 0)) { 12408 /* 12409 * If we have nothing limit us to bursting 12410 * out IW sized pieces. 12411 */ 12412 chged = 1; 12413 rack->r_ctl.rc_pace_max_segs = rc_init_window(rack); 12414 } 12415 } 12416 if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) { 12417 chged = 1; 12418 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES; 12419 } 12420 if (chged) 12421 rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2); 12422 } 12423 12424 12425 static void 12426 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack) 12427 { 12428 #ifdef INET6 12429 struct ip6_hdr *ip6 = NULL; 12430 #endif 12431 #ifdef INET 12432 struct ip *ip = NULL; 12433 #endif 12434 struct udphdr *udp = NULL; 12435 12436 /* Ok lets fill in the fast block, it can only be used with no IP options! */ 12437 #ifdef INET6 12438 if (rack->r_is_v6) { 12439 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12440 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 12441 if (tp->t_port) { 12442 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr); 12443 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 12444 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 12445 udp->uh_dport = tp->t_port; 12446 rack->r_ctl.fsb.udp = udp; 12447 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1); 12448 } else 12449 { 12450 rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1); 12451 rack->r_ctl.fsb.udp = NULL; 12452 } 12453 tcpip_fillheaders(rack->rc_inp, 12454 tp->t_port, 12455 ip6, rack->r_ctl.fsb.th); 12456 } else 12457 #endif /* INET6 */ 12458 { 12459 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr); 12460 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 12461 if (tp->t_port) { 12462 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr); 12463 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 12464 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 12465 udp->uh_dport = tp->t_port; 12466 rack->r_ctl.fsb.udp = udp; 12467 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1); 12468 } else 12469 { 12470 rack->r_ctl.fsb.udp = NULL; 12471 rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1); 12472 } 12473 tcpip_fillheaders(rack->rc_inp, 12474 tp->t_port, 12475 ip, rack->r_ctl.fsb.th); 12476 } 12477 rack->r_fsb_inited = 1; 12478 } 12479 12480 static int 12481 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack) 12482 { 12483 /* 12484 * Allocate the larger of spaces V6 if available else just 12485 * V4 and include udphdr (overbook) 12486 */ 12487 #ifdef INET6 12488 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr); 12489 #else 12490 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr); 12491 #endif 12492 rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len, 12493 M_TCPFSB, M_NOWAIT|M_ZERO); 12494 if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) { 12495 return (ENOMEM); 12496 } 12497 rack->r_fsb_inited = 0; 12498 return (0); 12499 } 12500 12501 static int 12502 rack_init(struct tcpcb *tp) 12503 { 12504 struct tcp_rack *rack = NULL; 12505 struct rack_sendmap *insret; 12506 uint32_t iwin, snt, us_cts; 12507 int err; 12508 12509 tp->t_fb_ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT); 12510 if (tp->t_fb_ptr == NULL) { 12511 /* 12512 * We need to allocate memory but cant. The INP and INP_INFO 12513 * locks and they are recusive (happens during setup. So a 12514 * scheme to drop the locks fails :( 12515 * 12516 */ 12517 return (ENOMEM); 12518 } 12519 memset(tp->t_fb_ptr, 0, sizeof(struct tcp_rack)); 12520 12521 rack = (struct tcp_rack *)tp->t_fb_ptr; 12522 RB_INIT(&rack->r_ctl.rc_mtree); 12523 TAILQ_INIT(&rack->r_ctl.rc_free); 12524 TAILQ_INIT(&rack->r_ctl.rc_tmap); 12525 rack->rc_tp = tp; 12526 rack->rc_inp = tp->t_inpcb; 12527 /* Set the flag */ 12528 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 12529 /* Probably not needed but lets be sure */ 12530 rack_clear_rate_sample(rack); 12531 /* 12532 * Save off the default values, socket options will poke 12533 * at these if pacing is not on or we have not yet 12534 * reached where pacing is on (gp_ready/fixed enabled). 12535 * When they get set into the CC module (when gp_ready 12536 * is enabled or we enable fixed) then we will set these 12537 * values into the CC and place in here the old values 12538 * so we have a restoral. Then we will set the flag 12539 * rc_pacing_cc_set. That way whenever we turn off pacing 12540 * or switch off this stack, we will know to go restore 12541 * the saved values. 12542 */ 12543 rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn; 12544 rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn; 12545 /* We want abe like behavior as well */ 12546 rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN; 12547 rack->r_ctl.rc_reorder_fade = rack_reorder_fade; 12548 rack->rc_allow_data_af_clo = rack_ignore_data_after_close; 12549 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh; 12550 if (use_rack_rr) 12551 rack->use_rack_rr = 1; 12552 if (V_tcp_delack_enabled) 12553 tp->t_delayed_ack = 1; 12554 else 12555 tp->t_delayed_ack = 0; 12556 #ifdef TCP_ACCOUNTING 12557 if (rack_tcp_accounting) { 12558 tp->t_flags2 |= TF2_TCP_ACCOUNTING; 12559 } 12560 #endif 12561 if (rack_enable_shared_cwnd) 12562 rack->rack_enable_scwnd = 1; 12563 rack->rc_user_set_max_segs = rack_hptsi_segments; 12564 rack->rc_force_max_seg = 0; 12565 if (rack_use_imac_dack) 12566 rack->rc_dack_mode = 1; 12567 TAILQ_INIT(&rack->r_ctl.opt_list); 12568 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh; 12569 rack->r_ctl.rc_pkt_delay = rack_pkt_delay; 12570 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp; 12571 rack->r_ctl.rc_lowest_us_rtt = 0xffffffff; 12572 rack->r_ctl.rc_highest_us_rtt = 0; 12573 rack->r_ctl.bw_rate_cap = rack_bw_rate_cap; 12574 rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop); 12575 if (rack_use_cmp_acks) 12576 rack->r_use_cmp_ack = 1; 12577 if (rack_disable_prr) 12578 rack->rack_no_prr = 1; 12579 if (rack_gp_no_rec_chg) 12580 rack->rc_gp_no_rec_chg = 1; 12581 if (rack_pace_every_seg && tcp_can_enable_pacing()) { 12582 rack->rc_always_pace = 1; 12583 if (rack->use_fixed_rate || rack->gp_ready) 12584 rack_set_cc_pacing(rack); 12585 } else 12586 rack->rc_always_pace = 0; 12587 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) 12588 rack->r_mbuf_queue = 1; 12589 else 12590 rack->r_mbuf_queue = 0; 12591 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 12592 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ; 12593 else 12594 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ; 12595 rack_set_pace_segments(tp, rack, __LINE__, NULL); 12596 if (rack_limits_scwnd) 12597 rack->r_limit_scw = 1; 12598 else 12599 rack->r_limit_scw = 0; 12600 rack->rc_labc = V_tcp_abc_l_var; 12601 rack->r_ctl.rc_high_rwnd = tp->snd_wnd; 12602 rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 12603 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method; 12604 rack->rack_tlp_threshold_use = rack_tlp_threshold_use; 12605 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr; 12606 rack->r_ctl.rc_min_to = rack_min_to; 12607 microuptime(&rack->r_ctl.act_rcv_time); 12608 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time; 12609 rack->r_running_late = 0; 12610 rack->r_running_early = 0; 12611 rack->rc_init_win = rack_default_init_window; 12612 rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss; 12613 if (rack_hw_up_only) 12614 rack->r_up_only = 1; 12615 if (rack_do_dyn_mul) { 12616 /* When dynamic adjustment is on CA needs to start at 100% */ 12617 rack->rc_gp_dyn_mul = 1; 12618 if (rack_do_dyn_mul >= 100) 12619 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul; 12620 } else 12621 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca; 12622 rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec; 12623 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 12624 rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time); 12625 setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN, 12626 rack_probertt_filter_life); 12627 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 12628 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 12629 rack->r_ctl.rc_time_of_last_probertt = us_cts; 12630 rack->r_ctl.challenge_ack_ts = tcp_ts_getticks(); 12631 rack->r_ctl.rc_time_probertt_starts = 0; 12632 if (rack_dsack_std_based & 0x1) { 12633 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */ 12634 rack->rc_rack_tmr_std_based = 1; 12635 } 12636 if (rack_dsack_std_based & 0x2) { 12637 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */ 12638 rack->rc_rack_use_dsack = 1; 12639 } 12640 /* We require at least one measurement, even if the sysctl is 0 */ 12641 if (rack_req_measurements) 12642 rack->r_ctl.req_measurements = rack_req_measurements; 12643 else 12644 rack->r_ctl.req_measurements = 1; 12645 if (rack_enable_hw_pacing) 12646 rack->rack_hdw_pace_ena = 1; 12647 if (rack_hw_rate_caps) 12648 rack->r_rack_hw_rate_caps = 1; 12649 /* Do we force on detection? */ 12650 #ifdef NETFLIX_EXP_DETECTION 12651 if (tcp_force_detection) 12652 rack->do_detection = 1; 12653 else 12654 #endif 12655 rack->do_detection = 0; 12656 if (rack_non_rxt_use_cr) 12657 rack->rack_rec_nonrxt_use_cr = 1; 12658 err = rack_init_fsb(tp, rack); 12659 if (err) { 12660 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 12661 tp->t_fb_ptr = NULL; 12662 return (err); 12663 } 12664 if (tp->snd_una != tp->snd_max) { 12665 /* Create a send map for the current outstanding data */ 12666 struct rack_sendmap *rsm; 12667 12668 rsm = rack_alloc(rack); 12669 if (rsm == NULL) { 12670 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 12671 tp->t_fb_ptr = NULL; 12672 return (ENOMEM); 12673 } 12674 rsm->r_no_rtt_allowed = 1; 12675 rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 12676 rsm->r_rtr_cnt = 1; 12677 rsm->r_rtr_bytes = 0; 12678 if (tp->t_flags & TF_SENTFIN) { 12679 rsm->r_end = tp->snd_max - 1; 12680 rsm->r_flags |= RACK_HAS_FIN; 12681 } else { 12682 rsm->r_end = tp->snd_max; 12683 } 12684 if (tp->snd_una == tp->iss) { 12685 /* The data space is one beyond snd_una */ 12686 rsm->r_flags |= RACK_HAS_SYN; 12687 rsm->r_start = tp->iss; 12688 rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una); 12689 } else 12690 rsm->r_start = tp->snd_una; 12691 rsm->r_dupack = 0; 12692 if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) { 12693 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff); 12694 if (rsm->m) 12695 rsm->orig_m_len = rsm->m->m_len; 12696 else 12697 rsm->orig_m_len = 0; 12698 } else { 12699 /* 12700 * This can happen if we have a stand-alone FIN or 12701 * SYN. 12702 */ 12703 rsm->m = NULL; 12704 rsm->orig_m_len = 0; 12705 rsm->soff = 0; 12706 } 12707 insret = RB_INSERT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 12708 #ifdef INVARIANTS 12709 if (insret != NULL) { 12710 panic("Insert in rb tree fails ret:%p rack:%p rsm:%p", 12711 insret, rack, rsm); 12712 } 12713 #endif 12714 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 12715 rsm->r_in_tmap = 1; 12716 } 12717 /* 12718 * Timers in Rack are kept in microseconds so lets 12719 * convert any initial incoming variables 12720 * from ticks into usecs. Note that we 12721 * also change the values of t_srtt and t_rttvar, if 12722 * they are non-zero. They are kept with a 5 12723 * bit decimal so we have to carefully convert 12724 * these to get the full precision. 12725 */ 12726 rack_convert_rtts(tp); 12727 tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow); 12728 if (rack_def_profile) 12729 rack_set_profile(rack, rack_def_profile); 12730 /* Cancel the GP measurement in progress */ 12731 tp->t_flags &= ~TF_GPUTINPROG; 12732 if (SEQ_GT(tp->snd_max, tp->iss)) 12733 snt = tp->snd_max - tp->iss; 12734 else 12735 snt = 0; 12736 iwin = rc_init_window(rack); 12737 if (snt < iwin) { 12738 /* We are not past the initial window 12739 * so we need to make sure cwnd is 12740 * correct. 12741 */ 12742 if (tp->snd_cwnd < iwin) 12743 tp->snd_cwnd = iwin; 12744 /* 12745 * If we are within the initial window 12746 * we want ssthresh to be unlimited. Setting 12747 * it to the rwnd (which the default stack does 12748 * and older racks) is not really a good idea 12749 * since we want to be in SS and grow both the 12750 * cwnd and the rwnd (via dynamic rwnd growth). If 12751 * we set it to the rwnd then as the peer grows its 12752 * rwnd we will be stuck in CA and never hit SS. 12753 * 12754 * Its far better to raise it up high (this takes the 12755 * risk that there as been a loss already, probably 12756 * we should have an indicator in all stacks of loss 12757 * but we don't), but considering the normal use this 12758 * is a risk worth taking. The consequences of not 12759 * hitting SS are far worse than going one more time 12760 * into it early on (before we have sent even a IW). 12761 * It is highly unlikely that we will have had a loss 12762 * before getting the IW out. 12763 */ 12764 tp->snd_ssthresh = 0xffffffff; 12765 } 12766 rack_stop_all_timers(tp); 12767 /* Lets setup the fsb block */ 12768 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 12769 rack_log_rtt_shrinks(rack, us_cts, tp->t_rxtcur, 12770 __LINE__, RACK_RTTS_INIT); 12771 return (0); 12772 } 12773 12774 static int 12775 rack_handoff_ok(struct tcpcb *tp) 12776 { 12777 if ((tp->t_state == TCPS_CLOSED) || 12778 (tp->t_state == TCPS_LISTEN)) { 12779 /* Sure no problem though it may not stick */ 12780 return (0); 12781 } 12782 if ((tp->t_state == TCPS_SYN_SENT) || 12783 (tp->t_state == TCPS_SYN_RECEIVED)) { 12784 /* 12785 * We really don't know if you support sack, 12786 * you have to get to ESTAB or beyond to tell. 12787 */ 12788 return (EAGAIN); 12789 } 12790 if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) { 12791 /* 12792 * Rack will only send a FIN after all data is acknowledged. 12793 * So in this case we have more data outstanding. We can't 12794 * switch stacks until either all data and only the FIN 12795 * is left (in which case rack_init() now knows how 12796 * to deal with that) <or> all is acknowledged and we 12797 * are only left with incoming data, though why you 12798 * would want to switch to rack after all data is acknowledged 12799 * I have no idea (rrs)! 12800 */ 12801 return (EAGAIN); 12802 } 12803 if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){ 12804 return (0); 12805 } 12806 /* 12807 * If we reach here we don't do SACK on this connection so we can 12808 * never do rack. 12809 */ 12810 return (EINVAL); 12811 } 12812 12813 12814 static void 12815 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged) 12816 { 12817 int ack_cmp = 0; 12818 12819 if (tp->t_fb_ptr) { 12820 struct tcp_rack *rack; 12821 struct rack_sendmap *rsm, *nrsm, *rm; 12822 12823 rack = (struct tcp_rack *)tp->t_fb_ptr; 12824 if (tp->t_in_pkt) { 12825 /* 12826 * It is unsafe to process the packets since a 12827 * reset may be lurking in them (its rare but it 12828 * can occur). If we were to find a RST, then we 12829 * would end up dropping the connection and the 12830 * INP lock, so when we return the caller (tcp_usrreq) 12831 * will blow up when it trys to unlock the inp. 12832 */ 12833 struct mbuf *save, *m; 12834 12835 m = tp->t_in_pkt; 12836 tp->t_in_pkt = NULL; 12837 tp->t_tail_pkt = NULL; 12838 while (m) { 12839 save = m->m_nextpkt; 12840 m->m_nextpkt = NULL; 12841 m_freem(m); 12842 m = save; 12843 } 12844 if ((tp->t_inpcb) && 12845 (tp->t_inpcb->inp_flags2 & INP_MBUF_ACKCMP)) 12846 ack_cmp = 1; 12847 if (ack_cmp) { 12848 /* Total if we used large or small (if ack-cmp was used). */ 12849 if (rack->rc_inp->inp_flags2 & INP_MBUF_L_ACKS) 12850 counter_u64_add(rack_large_ackcmp, 1); 12851 else 12852 counter_u64_add(rack_small_ackcmp, 1); 12853 } 12854 } 12855 tp->t_flags &= ~TF_FORCEDATA; 12856 #ifdef NETFLIX_SHARED_CWND 12857 if (rack->r_ctl.rc_scw) { 12858 uint32_t limit; 12859 12860 if (rack->r_limit_scw) 12861 limit = max(1, rack->r_ctl.rc_lowest_us_rtt); 12862 else 12863 limit = 0; 12864 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw, 12865 rack->r_ctl.rc_scw_index, 12866 limit); 12867 rack->r_ctl.rc_scw = NULL; 12868 } 12869 #endif 12870 if (rack->r_ctl.fsb.tcp_ip_hdr) { 12871 free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB); 12872 rack->r_ctl.fsb.tcp_ip_hdr = NULL; 12873 rack->r_ctl.fsb.th = NULL; 12874 } 12875 /* Convert back to ticks, with */ 12876 if (tp->t_srtt > 1) { 12877 uint32_t val, frac; 12878 12879 val = USEC_2_TICKS(tp->t_srtt); 12880 frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz); 12881 tp->t_srtt = val << TCP_RTT_SHIFT; 12882 /* 12883 * frac is the fractional part here is left 12884 * over from converting to hz and shifting. 12885 * We need to convert this to the 5 bit 12886 * remainder. 12887 */ 12888 if (frac) { 12889 if (hz == 1000) { 12890 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC); 12891 } else { 12892 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC); 12893 } 12894 tp->t_srtt += frac; 12895 } 12896 } 12897 if (tp->t_rttvar) { 12898 uint32_t val, frac; 12899 12900 val = USEC_2_TICKS(tp->t_rttvar); 12901 frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz); 12902 tp->t_rttvar = val << TCP_RTTVAR_SHIFT; 12903 /* 12904 * frac is the fractional part here is left 12905 * over from converting to hz and shifting. 12906 * We need to convert this to the 5 bit 12907 * remainder. 12908 */ 12909 if (frac) { 12910 if (hz == 1000) { 12911 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC); 12912 } else { 12913 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC); 12914 } 12915 tp->t_rttvar += frac; 12916 } 12917 } 12918 tp->t_rxtcur = USEC_2_TICKS(tp->t_rxtcur); 12919 tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow); 12920 if (rack->rc_always_pace) { 12921 tcp_decrement_paced_conn(); 12922 rack_undo_cc_pacing(rack); 12923 rack->rc_always_pace = 0; 12924 } 12925 /* Clean up any options if they were not applied */ 12926 while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) { 12927 struct deferred_opt_list *dol; 12928 12929 dol = TAILQ_FIRST(&rack->r_ctl.opt_list); 12930 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next); 12931 free(dol, M_TCPDO); 12932 } 12933 /* rack does not use force data but other stacks may clear it */ 12934 if (rack->r_ctl.crte != NULL) { 12935 tcp_rel_pacing_rate(rack->r_ctl.crte, tp); 12936 rack->rack_hdrw_pacing = 0; 12937 rack->r_ctl.crte = NULL; 12938 } 12939 #ifdef TCP_BLACKBOX 12940 tcp_log_flowend(tp); 12941 #endif 12942 RB_FOREACH_SAFE(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree, nrsm) { 12943 rm = RB_REMOVE(rack_rb_tree_head, &rack->r_ctl.rc_mtree, rsm); 12944 #ifdef INVARIANTS 12945 if (rm != rsm) { 12946 panic("At fini, rack:%p rsm:%p rm:%p", 12947 rack, rsm, rm); 12948 } 12949 #endif 12950 uma_zfree(rack_zone, rsm); 12951 } 12952 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 12953 while (rsm) { 12954 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 12955 uma_zfree(rack_zone, rsm); 12956 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 12957 } 12958 rack->rc_free_cnt = 0; 12959 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 12960 tp->t_fb_ptr = NULL; 12961 } 12962 if (tp->t_inpcb) { 12963 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ; 12964 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_QUEUE_READY; 12965 tp->t_inpcb->inp_flags2 &= ~INP_DONT_SACK_QUEUE; 12966 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_ACKCMP; 12967 /* Cancel the GP measurement in progress */ 12968 tp->t_flags &= ~TF_GPUTINPROG; 12969 tp->t_inpcb->inp_flags2 &= ~INP_MBUF_L_ACKS; 12970 } 12971 /* Make sure snd_nxt is correctly set */ 12972 tp->snd_nxt = tp->snd_max; 12973 } 12974 12975 static void 12976 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack) 12977 { 12978 if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) { 12979 rack->r_is_v6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 12980 } 12981 switch (tp->t_state) { 12982 case TCPS_SYN_SENT: 12983 rack->r_state = TCPS_SYN_SENT; 12984 rack->r_substate = rack_do_syn_sent; 12985 break; 12986 case TCPS_SYN_RECEIVED: 12987 rack->r_state = TCPS_SYN_RECEIVED; 12988 rack->r_substate = rack_do_syn_recv; 12989 break; 12990 case TCPS_ESTABLISHED: 12991 rack_set_pace_segments(tp, rack, __LINE__, NULL); 12992 rack->r_state = TCPS_ESTABLISHED; 12993 rack->r_substate = rack_do_established; 12994 break; 12995 case TCPS_CLOSE_WAIT: 12996 rack_set_pace_segments(tp, rack, __LINE__, NULL); 12997 rack->r_state = TCPS_CLOSE_WAIT; 12998 rack->r_substate = rack_do_close_wait; 12999 break; 13000 case TCPS_FIN_WAIT_1: 13001 rack_set_pace_segments(tp, rack, __LINE__, NULL); 13002 rack->r_state = TCPS_FIN_WAIT_1; 13003 rack->r_substate = rack_do_fin_wait_1; 13004 break; 13005 case TCPS_CLOSING: 13006 rack_set_pace_segments(tp, rack, __LINE__, NULL); 13007 rack->r_state = TCPS_CLOSING; 13008 rack->r_substate = rack_do_closing; 13009 break; 13010 case TCPS_LAST_ACK: 13011 rack_set_pace_segments(tp, rack, __LINE__, NULL); 13012 rack->r_state = TCPS_LAST_ACK; 13013 rack->r_substate = rack_do_lastack; 13014 break; 13015 case TCPS_FIN_WAIT_2: 13016 rack_set_pace_segments(tp, rack, __LINE__, NULL); 13017 rack->r_state = TCPS_FIN_WAIT_2; 13018 rack->r_substate = rack_do_fin_wait_2; 13019 break; 13020 case TCPS_LISTEN: 13021 case TCPS_CLOSED: 13022 case TCPS_TIME_WAIT: 13023 default: 13024 break; 13025 }; 13026 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 13027 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 13028 13029 } 13030 13031 static void 13032 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb) 13033 { 13034 /* 13035 * We received an ack, and then did not 13036 * call send or were bounced out due to the 13037 * hpts was running. Now a timer is up as well, is 13038 * it the right timer? 13039 */ 13040 struct rack_sendmap *rsm; 13041 int tmr_up; 13042 13043 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 13044 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 13045 return; 13046 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 13047 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 13048 (tmr_up == PACE_TMR_RXT)) { 13049 /* Should be an RXT */ 13050 return; 13051 } 13052 if (rsm == NULL) { 13053 /* Nothing outstanding? */ 13054 if (tp->t_flags & TF_DELACK) { 13055 if (tmr_up == PACE_TMR_DELACK) 13056 /* We are supposed to have delayed ack up and we do */ 13057 return; 13058 } else if (sbavail(&tp->t_inpcb->inp_socket->so_snd) && (tmr_up == PACE_TMR_RXT)) { 13059 /* 13060 * if we hit enobufs then we would expect the possiblity 13061 * of nothing outstanding and the RXT up (and the hptsi timer). 13062 */ 13063 return; 13064 } else if (((V_tcp_always_keepalive || 13065 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 13066 (tp->t_state <= TCPS_CLOSING)) && 13067 (tmr_up == PACE_TMR_KEEP) && 13068 (tp->snd_max == tp->snd_una)) { 13069 /* We should have keep alive up and we do */ 13070 return; 13071 } 13072 } 13073 if (SEQ_GT(tp->snd_max, tp->snd_una) && 13074 ((tmr_up == PACE_TMR_TLP) || 13075 (tmr_up == PACE_TMR_RACK) || 13076 (tmr_up == PACE_TMR_RXT))) { 13077 /* 13078 * Either a Rack, TLP or RXT is fine if we 13079 * have outstanding data. 13080 */ 13081 return; 13082 } else if (tmr_up == PACE_TMR_DELACK) { 13083 /* 13084 * If the delayed ack was going to go off 13085 * before the rtx/tlp/rack timer were going to 13086 * expire, then that would be the timer in control. 13087 * Note we don't check the time here trusting the 13088 * code is correct. 13089 */ 13090 return; 13091 } 13092 /* 13093 * Ok the timer originally started is not what we want now. 13094 * We will force the hpts to be stopped if any, and restart 13095 * with the slot set to what was in the saved slot. 13096 */ 13097 if (rack->rc_inp->inp_in_hpts) { 13098 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 13099 uint32_t us_cts; 13100 13101 us_cts = tcp_get_usecs(NULL); 13102 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) { 13103 rack->r_early = 1; 13104 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts); 13105 } 13106 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 13107 } 13108 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT); 13109 } 13110 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13111 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 13112 } 13113 13114 13115 static void 13116 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts, uint32_t high_seq) 13117 { 13118 tp->snd_wnd = tiwin; 13119 rack_validate_fo_sendwin_up(tp, rack); 13120 tp->snd_wl1 = seq; 13121 tp->snd_wl2 = ack; 13122 if (tp->snd_wnd > tp->max_sndwnd) 13123 tp->max_sndwnd = tp->snd_wnd; 13124 if (tp->snd_wnd < (tp->snd_max - high_seq)) { 13125 /* The peer collapsed the window */ 13126 rack_collapsed_window(rack); 13127 } else if (rack->rc_has_collapsed) 13128 rack_un_collapse_window(rack); 13129 /* Do we exit persists? */ 13130 if ((rack->rc_in_persist != 0) && 13131 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 13132 rack->r_ctl.rc_pace_min_segs))) { 13133 rack_exit_persist(tp, rack, cts); 13134 } 13135 /* Do we enter persists? */ 13136 if ((rack->rc_in_persist == 0) && 13137 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 13138 TCPS_HAVEESTABLISHED(tp->t_state) && 13139 (tp->snd_max == tp->snd_una) && 13140 sbavail(&tp->t_inpcb->inp_socket->so_snd) && 13141 (sbavail(&tp->t_inpcb->inp_socket->so_snd) > tp->snd_wnd)) { 13142 /* 13143 * Here the rwnd is less than 13144 * the pacing size, we are established, 13145 * nothing is outstanding, and there is 13146 * data to send. Enter persists. 13147 */ 13148 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime); 13149 } 13150 } 13151 13152 static void 13153 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq) 13154 { 13155 13156 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 13157 union tcp_log_stackspecific log; 13158 struct timeval ltv; 13159 char tcp_hdr_buf[60]; 13160 struct tcphdr *th; 13161 struct timespec ts; 13162 uint32_t orig_snd_una; 13163 uint8_t xx = 0; 13164 13165 #ifdef NETFLIX_HTTP_LOGGING 13166 struct http_sendfile_track *http_req; 13167 13168 if (SEQ_GT(ae->ack, tp->snd_una)) { 13169 http_req = tcp_http_find_req_for_seq(tp, (ae->ack-1)); 13170 } else { 13171 http_req = tcp_http_find_req_for_seq(tp, ae->ack); 13172 } 13173 #endif 13174 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 13175 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 13176 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 13177 if (rack->rack_no_prr == 0) 13178 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 13179 else 13180 log.u_bbr.flex1 = 0; 13181 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 13182 log.u_bbr.use_lt_bw <<= 1; 13183 log.u_bbr.use_lt_bw |= rack->r_might_revert; 13184 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced; 13185 log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked); 13186 log.u_bbr.pkts_out = tp->t_maxseg; 13187 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 13188 log.u_bbr.flex7 = 1; 13189 log.u_bbr.lost = ae->flags; 13190 log.u_bbr.cwnd_gain = ackval; 13191 log.u_bbr.pacing_gain = 0x2; 13192 if (ae->flags & TSTMP_HDWR) { 13193 /* Record the hardware timestamp if present */ 13194 log.u_bbr.flex3 = M_TSTMP; 13195 ts.tv_sec = ae->timestamp / 1000000000; 13196 ts.tv_nsec = ae->timestamp % 1000000000; 13197 ltv.tv_sec = ts.tv_sec; 13198 ltv.tv_usec = ts.tv_nsec / 1000; 13199 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v); 13200 } else if (ae->flags & TSTMP_LRO) { 13201 /* Record the LRO the arrival timestamp */ 13202 log.u_bbr.flex3 = M_TSTMP_LRO; 13203 ts.tv_sec = ae->timestamp / 1000000000; 13204 ts.tv_nsec = ae->timestamp % 1000000000; 13205 ltv.tv_sec = ts.tv_sec; 13206 ltv.tv_usec = ts.tv_nsec / 1000; 13207 log.u_bbr.flex5 = tcp_tv_to_usectick(<v); 13208 } 13209 log.u_bbr.timeStamp = tcp_get_usecs(<v); 13210 /* Log the rcv time */ 13211 log.u_bbr.delRate = ae->timestamp; 13212 #ifdef NETFLIX_HTTP_LOGGING 13213 log.u_bbr.applimited = tp->t_http_closed; 13214 log.u_bbr.applimited <<= 8; 13215 log.u_bbr.applimited |= tp->t_http_open; 13216 log.u_bbr.applimited <<= 8; 13217 log.u_bbr.applimited |= tp->t_http_req; 13218 if (http_req) { 13219 /* Copy out any client req info */ 13220 /* seconds */ 13221 log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC); 13222 /* useconds */ 13223 log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC); 13224 log.u_bbr.rttProp = http_req->timestamp; 13225 log.u_bbr.cur_del_rate = http_req->start; 13226 if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) { 13227 log.u_bbr.flex8 |= 1; 13228 } else { 13229 log.u_bbr.flex8 |= 2; 13230 log.u_bbr.bw_inuse = http_req->end; 13231 } 13232 log.u_bbr.flex6 = http_req->start_seq; 13233 if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) { 13234 log.u_bbr.flex8 |= 4; 13235 log.u_bbr.epoch = http_req->end_seq; 13236 } 13237 } 13238 #endif 13239 memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf)); 13240 th = (struct tcphdr *)tcp_hdr_buf; 13241 th->th_seq = ae->seq; 13242 th->th_ack = ae->ack; 13243 th->th_win = ae->win; 13244 /* Now fill in the ports */ 13245 th->th_sport = tp->t_inpcb->inp_fport; 13246 th->th_dport = tp->t_inpcb->inp_lport; 13247 th->th_flags = ae->flags & 0xff; 13248 /* Now do we have a timestamp option? */ 13249 if (ae->flags & HAS_TSTMP) { 13250 u_char *cp; 13251 uint32_t val; 13252 13253 th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2); 13254 cp = (u_char *)(th + 1); 13255 *cp = TCPOPT_NOP; 13256 cp++; 13257 *cp = TCPOPT_NOP; 13258 cp++; 13259 *cp = TCPOPT_TIMESTAMP; 13260 cp++; 13261 *cp = TCPOLEN_TIMESTAMP; 13262 cp++; 13263 val = htonl(ae->ts_value); 13264 bcopy((char *)&val, 13265 (char *)cp, sizeof(uint32_t)); 13266 val = htonl(ae->ts_echo); 13267 bcopy((char *)&val, 13268 (char *)(cp + 4), sizeof(uint32_t)); 13269 } else 13270 th->th_off = (sizeof(struct tcphdr) >> 2); 13271 13272 /* 13273 * For sane logging we need to play a little trick. 13274 * If the ack were fully processed we would have moved 13275 * snd_una to high_seq, but since compressed acks are 13276 * processed in two phases, at this point (logging) snd_una 13277 * won't be advanced. So we would see multiple acks showing 13278 * the advancement. We can prevent that by "pretending" that 13279 * snd_una was advanced and then un-advancing it so that the 13280 * logging code has the right value for tlb_snd_una. 13281 */ 13282 if (tp->snd_una != high_seq) { 13283 orig_snd_una = tp->snd_una; 13284 tp->snd_una = high_seq; 13285 xx = 1; 13286 } else 13287 xx = 0; 13288 TCP_LOG_EVENTP(tp, th, 13289 &tp->t_inpcb->inp_socket->so_rcv, 13290 &tp->t_inpcb->inp_socket->so_snd, TCP_LOG_IN, 0, 13291 0, &log, true, <v); 13292 if (xx) { 13293 tp->snd_una = orig_snd_una; 13294 } 13295 } 13296 13297 } 13298 13299 static int 13300 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv) 13301 { 13302 /* 13303 * Handle a "special" compressed ack mbuf. Each incoming 13304 * ack has only four possible dispositions: 13305 * 13306 * A) It moves the cum-ack forward 13307 * B) It is behind the cum-ack. 13308 * C) It is a window-update ack. 13309 * D) It is a dup-ack. 13310 * 13311 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES 13312 * in the incoming mbuf. We also need to still pay attention 13313 * to nxt_pkt since there may be another packet after this 13314 * one. 13315 */ 13316 #ifdef TCP_ACCOUNTING 13317 uint64_t ts_val; 13318 uint64_t rdstc; 13319 #endif 13320 int segsiz; 13321 struct timespec ts; 13322 struct tcp_rack *rack; 13323 struct tcp_ackent *ae; 13324 uint32_t tiwin, us_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack; 13325 int cnt, i, did_out, ourfinisacked = 0; 13326 int win_up_req = 0; 13327 struct tcpopt to_holder, *to = NULL; 13328 int nsegs = 0; 13329 int under_pacing = 1; 13330 int recovery = 0; 13331 int idx; 13332 #ifdef TCP_ACCOUNTING 13333 sched_pin(); 13334 #endif 13335 rack = (struct tcp_rack *)tp->t_fb_ptr; 13336 if (rack->gp_ready && 13337 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) 13338 under_pacing = 0; 13339 else 13340 under_pacing = 1; 13341 13342 if (rack->r_state != tp->t_state) 13343 rack_set_state(tp, rack); 13344 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 13345 (tp->t_flags & TF_GPUTINPROG)) { 13346 /* 13347 * We have a goodput in progress 13348 * and we have entered a late state. 13349 * Do we have enough data in the sb 13350 * to handle the GPUT request? 13351 */ 13352 uint32_t bytes; 13353 13354 bytes = tp->gput_ack - tp->gput_seq; 13355 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 13356 bytes += tp->gput_seq - tp->snd_una; 13357 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) { 13358 /* 13359 * There are not enough bytes in the socket 13360 * buffer that have been sent to cover this 13361 * measurement. Cancel it. 13362 */ 13363 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 13364 rack->r_ctl.rc_gp_srtt /*flex1*/, 13365 tp->gput_seq, 13366 0, 0, 18, __LINE__, NULL, 0); 13367 tp->t_flags &= ~TF_GPUTINPROG; 13368 } 13369 } 13370 to = &to_holder; 13371 to->to_flags = 0; 13372 KASSERT((m->m_len >= sizeof(struct tcp_ackent)), 13373 ("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len)); 13374 cnt = m->m_len / sizeof(struct tcp_ackent); 13375 idx = cnt / 5; 13376 if (idx >= MAX_NUM_OF_CNTS) 13377 idx = MAX_NUM_OF_CNTS - 1; 13378 counter_u64_add(rack_proc_comp_ack[idx], 1); 13379 counter_u64_add(rack_multi_single_eq, cnt); 13380 high_seq = tp->snd_una; 13381 the_win = tp->snd_wnd; 13382 win_seq = tp->snd_wl1; 13383 win_upd_ack = tp->snd_wl2; 13384 cts = us_cts = tcp_tv_to_usectick(tv); 13385 segsiz = ctf_fixed_maxseg(tp); 13386 if ((rack->rc_gp_dyn_mul) && 13387 (rack->use_fixed_rate == 0) && 13388 (rack->rc_always_pace)) { 13389 /* Check in on probertt */ 13390 rack_check_probe_rtt(rack, us_cts); 13391 } 13392 for (i = 0; i < cnt; i++) { 13393 #ifdef TCP_ACCOUNTING 13394 ts_val = get_cyclecount(); 13395 #endif 13396 rack_clear_rate_sample(rack); 13397 ae = ((mtod(m, struct tcp_ackent *)) + i); 13398 /* Setup the window */ 13399 tiwin = ae->win << tp->snd_scale; 13400 /* figure out the type of ack */ 13401 if (SEQ_LT(ae->ack, high_seq)) { 13402 /* Case B*/ 13403 ae->ack_val_set = ACK_BEHIND; 13404 } else if (SEQ_GT(ae->ack, high_seq)) { 13405 /* Case A */ 13406 ae->ack_val_set = ACK_CUMACK; 13407 } else if (tiwin == the_win) { 13408 /* Case D */ 13409 ae->ack_val_set = ACK_DUPACK; 13410 } else { 13411 /* Case C */ 13412 ae->ack_val_set = ACK_RWND; 13413 } 13414 rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq); 13415 /* Validate timestamp */ 13416 if (ae->flags & HAS_TSTMP) { 13417 /* Setup for a timestamp */ 13418 to->to_flags = TOF_TS; 13419 ae->ts_echo -= tp->ts_offset; 13420 to->to_tsecr = ae->ts_echo; 13421 to->to_tsval = ae->ts_value; 13422 /* 13423 * If echoed timestamp is later than the current time, fall back to 13424 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 13425 * were used when this connection was established. 13426 */ 13427 if (TSTMP_GT(ae->ts_echo, cts)) 13428 ae->ts_echo = 0; 13429 if (tp->ts_recent && 13430 TSTMP_LT(ae->ts_value, tp->ts_recent)) { 13431 if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) { 13432 #ifdef TCP_ACCOUNTING 13433 rdstc = get_cyclecount(); 13434 if (rdstc > ts_val) { 13435 counter_u64_add(tcp_proc_time[ae->ack_val_set] , 13436 (rdstc - ts_val)); 13437 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13438 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val); 13439 } 13440 } 13441 #endif 13442 continue; 13443 } 13444 } 13445 if (SEQ_LEQ(ae->seq, tp->last_ack_sent) && 13446 SEQ_LEQ(tp->last_ack_sent, ae->seq)) { 13447 tp->ts_recent_age = tcp_ts_getticks(); 13448 tp->ts_recent = ae->ts_value; 13449 } 13450 } else { 13451 /* Setup for a no options */ 13452 to->to_flags = 0; 13453 } 13454 /* Update the rcv time and perform idle reduction possibly */ 13455 if (tp->t_idle_reduce && 13456 (tp->snd_max == tp->snd_una) && 13457 ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) { 13458 counter_u64_add(rack_input_idle_reduces, 1); 13459 rack_cc_after_idle(rack, tp); 13460 } 13461 tp->t_rcvtime = ticks; 13462 /* Now what about ECN? */ 13463 if (tp->t_flags2 & TF2_ECN_PERMIT) { 13464 if (ae->flags & TH_CWR) { 13465 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 13466 tp->t_flags |= TF_ACKNOW; 13467 } 13468 switch (ae->codepoint & IPTOS_ECN_MASK) { 13469 case IPTOS_ECN_CE: 13470 tp->t_flags2 |= TF2_ECN_SND_ECE; 13471 KMOD_TCPSTAT_INC(tcps_ecn_ce); 13472 break; 13473 case IPTOS_ECN_ECT0: 13474 KMOD_TCPSTAT_INC(tcps_ecn_ect0); 13475 break; 13476 case IPTOS_ECN_ECT1: 13477 KMOD_TCPSTAT_INC(tcps_ecn_ect1); 13478 break; 13479 } 13480 13481 /* Process a packet differently from RFC3168. */ 13482 cc_ecnpkt_handler_flags(tp, ae->flags, ae->codepoint); 13483 /* Congestion experienced. */ 13484 if (ae->flags & TH_ECE) { 13485 rack_cong_signal(tp, CC_ECN, ae->ack); 13486 } 13487 } 13488 #ifdef TCP_ACCOUNTING 13489 /* Count for the specific type of ack in */ 13490 counter_u64_add(tcp_cnt_counters[ae->ack_val_set], 1); 13491 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13492 tp->tcp_cnt_counters[ae->ack_val_set]++; 13493 } 13494 #endif 13495 /* 13496 * Note how we could move up these in the determination 13497 * above, but we don't so that way the timestamp checks (and ECN) 13498 * is done first before we do any processing on the ACK. 13499 * The non-compressed path through the code has this 13500 * weakness (noted by @jtl) that it actually does some 13501 * processing before verifying the timestamp information. 13502 * We don't take that path here which is why we set 13503 * the ack_val_set first, do the timestamp and ecn 13504 * processing, and then look at what we have setup. 13505 */ 13506 if (ae->ack_val_set == ACK_BEHIND) { 13507 /* 13508 * Case B flag reordering, if window is not closed 13509 * or it could be a keep-alive or persists 13510 */ 13511 if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) { 13512 counter_u64_add(rack_reorder_seen, 1); 13513 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 13514 } 13515 } else if (ae->ack_val_set == ACK_DUPACK) { 13516 /* Case D */ 13517 rack_strike_dupack(rack); 13518 } else if (ae->ack_val_set == ACK_RWND) { 13519 /* Case C */ 13520 13521 win_up_req = 1; 13522 win_upd_ack = ae->ack; 13523 win_seq = ae->seq; 13524 the_win = tiwin; 13525 } else { 13526 /* Case A */ 13527 13528 if (SEQ_GT(ae->ack, tp->snd_max)) { 13529 /* 13530 * We just send an ack since the incoming 13531 * ack is beyond the largest seq we sent. 13532 */ 13533 if ((tp->t_flags & TF_ACKNOW) == 0) { 13534 ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt); 13535 if (tp->t_flags && TF_ACKNOW) 13536 rack->r_wanted_output = 1; 13537 } 13538 } else { 13539 nsegs++; 13540 /* If the window changed setup to update */ 13541 if (tiwin != tp->snd_wnd) { 13542 win_up_req = 1; 13543 win_upd_ack = ae->ack; 13544 win_seq = ae->seq; 13545 the_win = tiwin; 13546 } 13547 #ifdef TCP_ACCOUNTING 13548 /* Account for the acks */ 13549 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13550 tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz); 13551 } 13552 counter_u64_add(tcp_cnt_counters[CNT_OF_ACKS_IN], 13553 (((ae->ack - high_seq) + segsiz - 1) / segsiz)); 13554 #endif 13555 high_seq = ae->ack; 13556 /* Setup our act_rcv_time */ 13557 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) { 13558 ts.tv_sec = ae->timestamp / 1000000000; 13559 ts.tv_nsec = ae->timestamp % 1000000000; 13560 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec; 13561 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000; 13562 } else { 13563 rack->r_ctl.act_rcv_time = *tv; 13564 } 13565 rack_process_to_cumack(tp, rack, ae->ack, cts, to); 13566 if (rack->rc_dsack_round_seen) { 13567 /* Is the dsack roound over? */ 13568 if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) { 13569 /* Yes it is */ 13570 rack->rc_dsack_round_seen = 0; 13571 rack_log_dsack_event(rack, 3, __LINE__, 0, 0); 13572 } 13573 } 13574 } 13575 } 13576 /* And lets be sure to commit the rtt measurements for this ack */ 13577 tcp_rack_xmit_timer_commit(rack, tp); 13578 #ifdef TCP_ACCOUNTING 13579 rdstc = get_cyclecount(); 13580 if (rdstc > ts_val) { 13581 counter_u64_add(tcp_proc_time[ae->ack_val_set] , (rdstc - ts_val)); 13582 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13583 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val); 13584 if (ae->ack_val_set == ACK_CUMACK) 13585 tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val); 13586 } 13587 } 13588 #endif 13589 } 13590 #ifdef TCP_ACCOUNTING 13591 ts_val = get_cyclecount(); 13592 #endif 13593 acked_amount = acked = (high_seq - tp->snd_una); 13594 if (win_up_req) { 13595 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts, high_seq); 13596 } 13597 if (acked) { 13598 if (rack->sack_attack_disable == 0) 13599 rack_do_decay(rack); 13600 if (acked >= segsiz) { 13601 /* 13602 * You only get credit for 13603 * MSS and greater (and you get extra 13604 * credit for larger cum-ack moves). 13605 */ 13606 int ac; 13607 13608 ac = acked / segsiz; 13609 rack->r_ctl.ack_count += ac; 13610 counter_u64_add(rack_ack_total, ac); 13611 } 13612 if (rack->r_ctl.ack_count > 0xfff00000) { 13613 /* 13614 * reduce the number to keep us under 13615 * a uint32_t. 13616 */ 13617 rack->r_ctl.ack_count /= 2; 13618 rack->r_ctl.sack_count /= 2; 13619 } 13620 if (tp->t_flags & TF_NEEDSYN) { 13621 /* 13622 * T/TCP: Connection was half-synchronized, and our SYN has 13623 * been ACK'd (so connection is now fully synchronized). Go 13624 * to non-starred state, increment snd_una for ACK of SYN, 13625 * and check if we can do window scaling. 13626 */ 13627 tp->t_flags &= ~TF_NEEDSYN; 13628 tp->snd_una++; 13629 acked_amount = acked = (high_seq - tp->snd_una); 13630 } 13631 if (acked > sbavail(&so->so_snd)) 13632 acked_amount = sbavail(&so->so_snd); 13633 #ifdef NETFLIX_EXP_DETECTION 13634 /* 13635 * We only care on a cum-ack move if we are in a sack-disabled 13636 * state. We have already added in to the ack_count, and we never 13637 * would disable on a cum-ack move, so we only care to do the 13638 * detection if it may "undo" it, i.e. we were in disabled already. 13639 */ 13640 if (rack->sack_attack_disable) 13641 rack_do_detection(tp, rack, acked_amount, segsiz); 13642 #endif 13643 if (IN_FASTRECOVERY(tp->t_flags) && 13644 (rack->rack_no_prr == 0)) 13645 rack_update_prr(tp, rack, acked_amount, high_seq); 13646 if (IN_RECOVERY(tp->t_flags)) { 13647 if (SEQ_LT(high_seq, tp->snd_recover) && 13648 (SEQ_LT(high_seq, tp->snd_max))) { 13649 tcp_rack_partialack(tp); 13650 } else { 13651 rack_post_recovery(tp, high_seq); 13652 recovery = 1; 13653 } 13654 } 13655 /* Handle the rack-log-ack part (sendmap) */ 13656 if ((sbused(&so->so_snd) == 0) && 13657 (acked > acked_amount) && 13658 (tp->t_state >= TCPS_FIN_WAIT_1) && 13659 (tp->t_flags & TF_SENTFIN)) { 13660 /* 13661 * We must be sure our fin 13662 * was sent and acked (we can be 13663 * in FIN_WAIT_1 without having 13664 * sent the fin). 13665 */ 13666 ourfinisacked = 1; 13667 /* 13668 * Lets make sure snd_una is updated 13669 * since most likely acked_amount = 0 (it 13670 * should be). 13671 */ 13672 tp->snd_una = high_seq; 13673 } 13674 /* Did we make a RTO error? */ 13675 if ((tp->t_flags & TF_PREVVALID) && 13676 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 13677 tp->t_flags &= ~TF_PREVVALID; 13678 if (tp->t_rxtshift == 1 && 13679 (int)(ticks - tp->t_badrxtwin) < 0) 13680 rack_cong_signal(tp, CC_RTO_ERR, high_seq); 13681 } 13682 /* Handle the data in the socket buffer */ 13683 KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1); 13684 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 13685 if (acked_amount > 0) { 13686 struct mbuf *mfree; 13687 13688 rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, recovery); 13689 SOCKBUF_LOCK(&so->so_snd); 13690 mfree = sbcut_locked(&so->so_snd, acked); 13691 tp->snd_una = high_seq; 13692 /* Note we want to hold the sb lock through the sendmap adjust */ 13693 rack_adjust_sendmap(rack, &so->so_snd, tp->snd_una); 13694 /* Wake up the socket if we have room to write more */ 13695 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 13696 sowwakeup_locked(so); 13697 m_freem(mfree); 13698 } 13699 /* update progress */ 13700 tp->t_acktime = ticks; 13701 rack_log_progress_event(rack, tp, tp->t_acktime, 13702 PROGRESS_UPDATE, __LINE__); 13703 /* Clear out shifts and such */ 13704 tp->t_rxtshift = 0; 13705 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 13706 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 13707 rack->rc_tlp_in_progress = 0; 13708 rack->r_ctl.rc_tlp_cnt_out = 0; 13709 /* Send recover and snd_nxt must be dragged along */ 13710 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 13711 tp->snd_recover = tp->snd_una; 13712 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 13713 tp->snd_nxt = tp->snd_una; 13714 /* 13715 * If the RXT timer is running we want to 13716 * stop it, so we can restart a TLP (or new RXT). 13717 */ 13718 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 13719 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13720 #ifdef NETFLIX_HTTP_LOGGING 13721 tcp_http_check_for_comp(rack->rc_tp, high_seq); 13722 #endif 13723 tp->snd_wl2 = high_seq; 13724 tp->t_dupacks = 0; 13725 if (under_pacing && 13726 (rack->use_fixed_rate == 0) && 13727 (rack->in_probe_rtt == 0) && 13728 rack->rc_gp_dyn_mul && 13729 rack->rc_always_pace) { 13730 /* Check if we are dragging bottom */ 13731 rack_check_bottom_drag(tp, rack, so, acked); 13732 } 13733 if (tp->snd_una == tp->snd_max) { 13734 tp->t_flags &= ~TF_PREVVALID; 13735 rack->r_ctl.retran_during_recovery = 0; 13736 rack->r_ctl.dsack_byte_cnt = 0; 13737 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 13738 if (rack->r_ctl.rc_went_idle_time == 0) 13739 rack->r_ctl.rc_went_idle_time = 1; 13740 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 13741 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) == 0) 13742 tp->t_acktime = 0; 13743 /* Set so we might enter persists... */ 13744 rack->r_wanted_output = 1; 13745 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13746 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 13747 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 13748 (sbavail(&so->so_snd) == 0) && 13749 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 13750 /* 13751 * The socket was gone and the 13752 * peer sent data (not now in the past), time to 13753 * reset him. 13754 */ 13755 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13756 /* tcp_close will kill the inp pre-log the Reset */ 13757 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 13758 #ifdef TCP_ACCOUNTING 13759 rdstc = get_cyclecount(); 13760 if (rdstc > ts_val) { 13761 counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val)); 13762 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13763 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13764 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13765 } 13766 } 13767 #endif 13768 m_freem(m); 13769 tp = tcp_close(tp); 13770 if (tp == NULL) { 13771 #ifdef TCP_ACCOUNTING 13772 sched_unpin(); 13773 #endif 13774 return (1); 13775 } 13776 /* 13777 * We would normally do drop-with-reset which would 13778 * send back a reset. We can't since we don't have 13779 * all the needed bits. Instead lets arrange for 13780 * a call to tcp_output(). That way since we 13781 * are in the closed state we will generate a reset. 13782 * 13783 * Note if tcp_accounting is on we don't unpin since 13784 * we do that after the goto label. 13785 */ 13786 goto send_out_a_rst; 13787 } 13788 if ((sbused(&so->so_snd) == 0) && 13789 (tp->t_state >= TCPS_FIN_WAIT_1) && 13790 (tp->t_flags & TF_SENTFIN)) { 13791 /* 13792 * If we can't receive any more data, then closing user can 13793 * proceed. Starting the timer is contrary to the 13794 * specification, but if we don't get a FIN we'll hang 13795 * forever. 13796 * 13797 */ 13798 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 13799 soisdisconnected(so); 13800 tcp_timer_activate(tp, TT_2MSL, 13801 (tcp_fast_finwait2_recycle ? 13802 tcp_finwait2_timeout : 13803 TP_MAXIDLE(tp))); 13804 } 13805 if (ourfinisacked == 0) { 13806 /* 13807 * We don't change to fin-wait-2 if we have our fin acked 13808 * which means we are probably in TCPS_CLOSING. 13809 */ 13810 tcp_state_change(tp, TCPS_FIN_WAIT_2); 13811 } 13812 } 13813 } 13814 /* Wake up the socket if we have room to write more */ 13815 if (sbavail(&so->so_snd)) { 13816 rack->r_wanted_output = 1; 13817 if (ctf_progress_timeout_check(tp, true)) { 13818 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 13819 tp, tick, PROGRESS_DROP, __LINE__); 13820 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT); 13821 /* 13822 * We cheat here and don't send a RST, we should send one 13823 * when the pacer drops the connection. 13824 */ 13825 #ifdef TCP_ACCOUNTING 13826 rdstc = get_cyclecount(); 13827 if (rdstc > ts_val) { 13828 counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val)); 13829 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13830 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13831 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13832 } 13833 } 13834 sched_unpin(); 13835 #endif 13836 INP_WUNLOCK(rack->rc_inp); 13837 m_freem(m); 13838 return (1); 13839 } 13840 } 13841 if (ourfinisacked) { 13842 switch(tp->t_state) { 13843 case TCPS_CLOSING: 13844 #ifdef TCP_ACCOUNTING 13845 rdstc = get_cyclecount(); 13846 if (rdstc > ts_val) { 13847 counter_u64_add(tcp_proc_time[ACK_CUMACK] , 13848 (rdstc - ts_val)); 13849 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13850 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13851 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13852 } 13853 } 13854 sched_unpin(); 13855 #endif 13856 tcp_twstart(tp); 13857 m_freem(m); 13858 return (1); 13859 break; 13860 case TCPS_LAST_ACK: 13861 #ifdef TCP_ACCOUNTING 13862 rdstc = get_cyclecount(); 13863 if (rdstc > ts_val) { 13864 counter_u64_add(tcp_proc_time[ACK_CUMACK] , 13865 (rdstc - ts_val)); 13866 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13867 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13868 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13869 } 13870 } 13871 sched_unpin(); 13872 #endif 13873 tp = tcp_close(tp); 13874 ctf_do_drop(m, tp); 13875 return (1); 13876 break; 13877 case TCPS_FIN_WAIT_1: 13878 #ifdef TCP_ACCOUNTING 13879 rdstc = get_cyclecount(); 13880 if (rdstc > ts_val) { 13881 counter_u64_add(tcp_proc_time[ACK_CUMACK] , 13882 (rdstc - ts_val)); 13883 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13884 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13885 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13886 } 13887 } 13888 #endif 13889 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 13890 soisdisconnected(so); 13891 tcp_timer_activate(tp, TT_2MSL, 13892 (tcp_fast_finwait2_recycle ? 13893 tcp_finwait2_timeout : 13894 TP_MAXIDLE(tp))); 13895 } 13896 tcp_state_change(tp, TCPS_FIN_WAIT_2); 13897 break; 13898 default: 13899 break; 13900 } 13901 } 13902 if (rack->r_fast_output) { 13903 /* 13904 * We re doing fast output.. can we expand that? 13905 */ 13906 rack_gain_for_fastoutput(rack, tp, so, acked_amount); 13907 } 13908 #ifdef TCP_ACCOUNTING 13909 rdstc = get_cyclecount(); 13910 if (rdstc > ts_val) { 13911 counter_u64_add(tcp_proc_time[ACK_CUMACK] , (rdstc - ts_val)); 13912 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13913 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 13914 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 13915 } 13916 } 13917 13918 } else if (win_up_req) { 13919 rdstc = get_cyclecount(); 13920 if (rdstc > ts_val) { 13921 counter_u64_add(tcp_proc_time[ACK_RWND] , (rdstc - ts_val)); 13922 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 13923 tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val); 13924 } 13925 } 13926 #endif 13927 } 13928 /* Now is there a next packet, if so we are done */ 13929 m_freem(m); 13930 did_out = 0; 13931 if (nxt_pkt) { 13932 #ifdef TCP_ACCOUNTING 13933 sched_unpin(); 13934 #endif 13935 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs); 13936 return (0); 13937 } 13938 rack_handle_might_revert(tp, rack); 13939 ctf_calc_rwin(so, tp); 13940 if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) { 13941 send_out_a_rst: 13942 (void)tp->t_fb->tfb_tcp_output(tp); 13943 did_out = 1; 13944 } 13945 rack_free_trim(rack); 13946 #ifdef TCP_ACCOUNTING 13947 sched_unpin(); 13948 #endif 13949 rack_timer_audit(tp, rack, &so->so_snd); 13950 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs); 13951 return (0); 13952 } 13953 13954 13955 static int 13956 rack_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so, 13957 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, 13958 int32_t nxt_pkt, struct timeval *tv) 13959 { 13960 #ifdef TCP_ACCOUNTING 13961 uint64_t ts_val; 13962 #endif 13963 int32_t thflags, retval, did_out = 0; 13964 int32_t way_out = 0; 13965 uint32_t cts; 13966 uint32_t tiwin; 13967 struct timespec ts; 13968 struct tcpopt to; 13969 struct tcp_rack *rack; 13970 struct rack_sendmap *rsm; 13971 int32_t prev_state = 0; 13972 #ifdef TCP_ACCOUNTING 13973 int ack_val_set = 0xf; 13974 #endif 13975 int nsegs; 13976 uint32_t us_cts; 13977 /* 13978 * tv passed from common code is from either M_TSTMP_LRO or 13979 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present. 13980 */ 13981 rack = (struct tcp_rack *)tp->t_fb_ptr; 13982 cts = tcp_tv_to_usectick(tv); 13983 if (m->m_flags & M_ACKCMP) { 13984 return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv)); 13985 } 13986 if (m->m_flags & M_ACKCMP) { 13987 panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp); 13988 } 13989 nsegs = m->m_pkthdr.lro_nsegs; 13990 counter_u64_add(rack_proc_non_comp_ack, 1); 13991 thflags = th->th_flags; 13992 #ifdef TCP_ACCOUNTING 13993 sched_pin(); 13994 if (thflags & TH_ACK) 13995 ts_val = get_cyclecount(); 13996 #endif 13997 if ((m->m_flags & M_TSTMP) || 13998 (m->m_flags & M_TSTMP_LRO)) { 13999 mbuf_tstmp2timespec(m, &ts); 14000 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec; 14001 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000; 14002 } else 14003 rack->r_ctl.act_rcv_time = *tv; 14004 kern_prefetch(rack, &prev_state); 14005 prev_state = 0; 14006 /* 14007 * Unscale the window into a 32-bit value. For the SYN_SENT state 14008 * the scale is zero. 14009 */ 14010 tiwin = th->th_win << tp->snd_scale; 14011 #ifdef TCP_ACCOUNTING 14012 if (thflags & TH_ACK) { 14013 /* 14014 * We have a tradeoff here. We can either do what we are 14015 * doing i.e. pinning to this CPU and then doing the accounting 14016 * <or> we could do a critical enter, setup the rdtsc and cpu 14017 * as in below, and then validate we are on the same CPU on 14018 * exit. I have choosen to not do the critical enter since 14019 * that often will gain you a context switch, and instead lock 14020 * us (line above this if) to the same CPU with sched_pin(). This 14021 * means we may be context switched out for a higher priority 14022 * interupt but we won't be moved to another CPU. 14023 * 14024 * If this occurs (which it won't very often since we most likely 14025 * are running this code in interupt context and only a higher 14026 * priority will bump us ... clock?) we will falsely add in 14027 * to the time the interupt processing time plus the ack processing 14028 * time. This is ok since its a rare event. 14029 */ 14030 ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin, 14031 ctf_fixed_maxseg(tp)); 14032 } 14033 #endif 14034 /* 14035 * Parse options on any incoming segment. 14036 */ 14037 memset(&to, 0, sizeof(to)); 14038 tcp_dooptions(&to, (u_char *)(th + 1), 14039 (th->th_off << 2) - sizeof(struct tcphdr), 14040 (thflags & TH_SYN) ? TO_SYN : 0); 14041 NET_EPOCH_ASSERT(); 14042 INP_WLOCK_ASSERT(tp->t_inpcb); 14043 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 14044 __func__)); 14045 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 14046 __func__)); 14047 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 14048 (tp->t_flags & TF_GPUTINPROG)) { 14049 /* 14050 * We have a goodput in progress 14051 * and we have entered a late state. 14052 * Do we have enough data in the sb 14053 * to handle the GPUT request? 14054 */ 14055 uint32_t bytes; 14056 14057 bytes = tp->gput_ack - tp->gput_seq; 14058 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 14059 bytes += tp->gput_seq - tp->snd_una; 14060 if (bytes > sbavail(&tp->t_inpcb->inp_socket->so_snd)) { 14061 /* 14062 * There are not enough bytes in the socket 14063 * buffer that have been sent to cover this 14064 * measurement. Cancel it. 14065 */ 14066 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 14067 rack->r_ctl.rc_gp_srtt /*flex1*/, 14068 tp->gput_seq, 14069 0, 0, 18, __LINE__, NULL, 0); 14070 tp->t_flags &= ~TF_GPUTINPROG; 14071 } 14072 } 14073 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 14074 union tcp_log_stackspecific log; 14075 struct timeval ltv; 14076 #ifdef NETFLIX_HTTP_LOGGING 14077 struct http_sendfile_track *http_req; 14078 14079 if (SEQ_GT(th->th_ack, tp->snd_una)) { 14080 http_req = tcp_http_find_req_for_seq(tp, (th->th_ack-1)); 14081 } else { 14082 http_req = tcp_http_find_req_for_seq(tp, th->th_ack); 14083 } 14084 #endif 14085 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 14086 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 14087 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 14088 if (rack->rack_no_prr == 0) 14089 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 14090 else 14091 log.u_bbr.flex1 = 0; 14092 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 14093 log.u_bbr.use_lt_bw <<= 1; 14094 log.u_bbr.use_lt_bw |= rack->r_might_revert; 14095 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced; 14096 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 14097 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg; 14098 log.u_bbr.flex3 = m->m_flags; 14099 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 14100 log.u_bbr.lost = thflags; 14101 log.u_bbr.pacing_gain = 0x1; 14102 #ifdef TCP_ACCOUNTING 14103 log.u_bbr.cwnd_gain = ack_val_set; 14104 #endif 14105 log.u_bbr.flex7 = 2; 14106 if (m->m_flags & M_TSTMP) { 14107 /* Record the hardware timestamp if present */ 14108 mbuf_tstmp2timespec(m, &ts); 14109 ltv.tv_sec = ts.tv_sec; 14110 ltv.tv_usec = ts.tv_nsec / 1000; 14111 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v); 14112 } else if (m->m_flags & M_TSTMP_LRO) { 14113 /* Record the LRO the arrival timestamp */ 14114 mbuf_tstmp2timespec(m, &ts); 14115 ltv.tv_sec = ts.tv_sec; 14116 ltv.tv_usec = ts.tv_nsec / 1000; 14117 log.u_bbr.flex5 = tcp_tv_to_usectick(<v); 14118 } 14119 log.u_bbr.timeStamp = tcp_get_usecs(<v); 14120 /* Log the rcv time */ 14121 log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp; 14122 #ifdef NETFLIX_HTTP_LOGGING 14123 log.u_bbr.applimited = tp->t_http_closed; 14124 log.u_bbr.applimited <<= 8; 14125 log.u_bbr.applimited |= tp->t_http_open; 14126 log.u_bbr.applimited <<= 8; 14127 log.u_bbr.applimited |= tp->t_http_req; 14128 if (http_req) { 14129 /* Copy out any client req info */ 14130 /* seconds */ 14131 log.u_bbr.pkt_epoch = (http_req->localtime / HPTS_USEC_IN_SEC); 14132 /* useconds */ 14133 log.u_bbr.delivered = (http_req->localtime % HPTS_USEC_IN_SEC); 14134 log.u_bbr.rttProp = http_req->timestamp; 14135 log.u_bbr.cur_del_rate = http_req->start; 14136 if (http_req->flags & TCP_HTTP_TRACK_FLG_OPEN) { 14137 log.u_bbr.flex8 |= 1; 14138 } else { 14139 log.u_bbr.flex8 |= 2; 14140 log.u_bbr.bw_inuse = http_req->end; 14141 } 14142 log.u_bbr.flex6 = http_req->start_seq; 14143 if (http_req->flags & TCP_HTTP_TRACK_FLG_COMP) { 14144 log.u_bbr.flex8 |= 4; 14145 log.u_bbr.epoch = http_req->end_seq; 14146 } 14147 } 14148 #endif 14149 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 14150 tlen, &log, true, <v); 14151 } 14152 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 14153 way_out = 4; 14154 retval = 0; 14155 m_freem(m); 14156 goto done_with_input; 14157 } 14158 /* 14159 * If a segment with the ACK-bit set arrives in the SYN-SENT state 14160 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 14161 */ 14162 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 14163 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 14164 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 14165 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14166 #ifdef TCP_ACCOUNTING 14167 sched_unpin(); 14168 #endif 14169 return (1); 14170 } 14171 /* 14172 * If timestamps were negotiated during SYN/ACK and a 14173 * segment without a timestamp is received, silently drop 14174 * the segment, unless it is a RST segment or missing timestamps are 14175 * tolerated. 14176 * See section 3.2 of RFC 7323. 14177 */ 14178 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 14179 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 14180 way_out = 5; 14181 retval = 0; 14182 m_freem(m); 14183 goto done_with_input; 14184 } 14185 14186 /* 14187 * Segment received on connection. Reset idle time and keep-alive 14188 * timer. XXX: This should be done after segment validation to 14189 * ignore broken/spoofed segs. 14190 */ 14191 if (tp->t_idle_reduce && 14192 (tp->snd_max == tp->snd_una) && 14193 ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) { 14194 counter_u64_add(rack_input_idle_reduces, 1); 14195 rack_cc_after_idle(rack, tp); 14196 } 14197 tp->t_rcvtime = ticks; 14198 #ifdef STATS 14199 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 14200 #endif 14201 if (tiwin > rack->r_ctl.rc_high_rwnd) 14202 rack->r_ctl.rc_high_rwnd = tiwin; 14203 /* 14204 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move 14205 * this to occur after we've validated the segment. 14206 */ 14207 if (tp->t_flags2 & TF2_ECN_PERMIT) { 14208 if (thflags & TH_CWR) { 14209 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 14210 tp->t_flags |= TF_ACKNOW; 14211 } 14212 switch (iptos & IPTOS_ECN_MASK) { 14213 case IPTOS_ECN_CE: 14214 tp->t_flags2 |= TF2_ECN_SND_ECE; 14215 KMOD_TCPSTAT_INC(tcps_ecn_ce); 14216 break; 14217 case IPTOS_ECN_ECT0: 14218 KMOD_TCPSTAT_INC(tcps_ecn_ect0); 14219 break; 14220 case IPTOS_ECN_ECT1: 14221 KMOD_TCPSTAT_INC(tcps_ecn_ect1); 14222 break; 14223 } 14224 14225 /* Process a packet differently from RFC3168. */ 14226 cc_ecnpkt_handler(tp, th, iptos); 14227 14228 /* Congestion experienced. */ 14229 if (thflags & TH_ECE) { 14230 rack_cong_signal(tp, CC_ECN, th->th_ack); 14231 } 14232 } 14233 14234 /* 14235 * If echoed timestamp is later than the current time, fall back to 14236 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 14237 * were used when this connection was established. 14238 */ 14239 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 14240 to.to_tsecr -= tp->ts_offset; 14241 if (TSTMP_GT(to.to_tsecr, cts)) 14242 to.to_tsecr = 0; 14243 } 14244 14245 /* 14246 * If its the first time in we need to take care of options and 14247 * verify we can do SACK for rack! 14248 */ 14249 if (rack->r_state == 0) { 14250 /* Should be init'd by rack_init() */ 14251 KASSERT(rack->rc_inp != NULL, 14252 ("%s: rack->rc_inp unexpectedly NULL", __func__)); 14253 if (rack->rc_inp == NULL) { 14254 rack->rc_inp = tp->t_inpcb; 14255 } 14256 14257 /* 14258 * Process options only when we get SYN/ACK back. The SYN 14259 * case for incoming connections is handled in tcp_syncache. 14260 * According to RFC1323 the window field in a SYN (i.e., a 14261 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 14262 * this is traditional behavior, may need to be cleaned up. 14263 */ 14264 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 14265 /* Handle parallel SYN for ECN */ 14266 if (!(thflags & TH_ACK) && 14267 ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) && 14268 ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) { 14269 tp->t_flags2 |= TF2_ECN_PERMIT; 14270 tp->t_flags2 |= TF2_ECN_SND_ECE; 14271 TCPSTAT_INC(tcps_ecn_shs); 14272 } 14273 if ((to.to_flags & TOF_SCALE) && 14274 (tp->t_flags & TF_REQ_SCALE)) { 14275 tp->t_flags |= TF_RCVD_SCALE; 14276 tp->snd_scale = to.to_wscale; 14277 } else 14278 tp->t_flags &= ~TF_REQ_SCALE; 14279 /* 14280 * Initial send window. It will be updated with the 14281 * next incoming segment to the scaled value. 14282 */ 14283 tp->snd_wnd = th->th_win; 14284 rack_validate_fo_sendwin_up(tp, rack); 14285 if ((to.to_flags & TOF_TS) && 14286 (tp->t_flags & TF_REQ_TSTMP)) { 14287 tp->t_flags |= TF_RCVD_TSTMP; 14288 tp->ts_recent = to.to_tsval; 14289 tp->ts_recent_age = cts; 14290 } else 14291 tp->t_flags &= ~TF_REQ_TSTMP; 14292 if (to.to_flags & TOF_MSS) { 14293 tcp_mss(tp, to.to_mss); 14294 } 14295 if ((tp->t_flags & TF_SACK_PERMIT) && 14296 (to.to_flags & TOF_SACKPERM) == 0) 14297 tp->t_flags &= ~TF_SACK_PERMIT; 14298 if (IS_FASTOPEN(tp->t_flags)) { 14299 if (to.to_flags & TOF_FASTOPEN) { 14300 uint16_t mss; 14301 14302 if (to.to_flags & TOF_MSS) 14303 mss = to.to_mss; 14304 else 14305 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 14306 mss = TCP6_MSS; 14307 else 14308 mss = TCP_MSS; 14309 tcp_fastopen_update_cache(tp, mss, 14310 to.to_tfo_len, to.to_tfo_cookie); 14311 } else 14312 tcp_fastopen_disable_path(tp); 14313 } 14314 } 14315 /* 14316 * At this point we are at the initial call. Here we decide 14317 * if we are doing RACK or not. We do this by seeing if 14318 * TF_SACK_PERMIT is set and the sack-not-required is clear. 14319 * The code now does do dup-ack counting so if you don't 14320 * switch back you won't get rack & TLP, but you will still 14321 * get this stack. 14322 */ 14323 14324 if ((rack_sack_not_required == 0) && 14325 ((tp->t_flags & TF_SACK_PERMIT) == 0)) { 14326 tcp_switch_back_to_default(tp); 14327 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen, 14328 tlen, iptos); 14329 #ifdef TCP_ACCOUNTING 14330 sched_unpin(); 14331 #endif 14332 return (1); 14333 } 14334 tcp_set_hpts(tp->t_inpcb); 14335 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack); 14336 } 14337 if (thflags & TH_FIN) 14338 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 14339 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 14340 if ((rack->rc_gp_dyn_mul) && 14341 (rack->use_fixed_rate == 0) && 14342 (rack->rc_always_pace)) { 14343 /* Check in on probertt */ 14344 rack_check_probe_rtt(rack, us_cts); 14345 } 14346 rack_clear_rate_sample(rack); 14347 if (rack->forced_ack) { 14348 uint32_t us_rtt; 14349 14350 /* 14351 * A persist or keep-alive was forced out, update our 14352 * min rtt time. Note we do not worry about lost 14353 * retransmissions since KEEP-ALIVES and persists 14354 * are usually way long on times of sending (though 14355 * if we were really paranoid or worried we could 14356 * at least use timestamps if available to validate). 14357 */ 14358 rack->forced_ack = 0; 14359 us_rtt = us_cts - rack->r_ctl.forced_ack_ts; 14360 if (us_rtt == 0) 14361 us_rtt = 1; 14362 rack_apply_updated_usrtt(rack, us_rtt, us_cts); 14363 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1); 14364 } 14365 /* 14366 * This is the one exception case where we set the rack state 14367 * always. All other times (timers etc) we must have a rack-state 14368 * set (so we assure we have done the checks above for SACK). 14369 */ 14370 rack->r_ctl.rc_rcvtime = cts; 14371 if (rack->r_state != tp->t_state) 14372 rack_set_state(tp, rack); 14373 if (SEQ_GT(th->th_ack, tp->snd_una) && 14374 (rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree)) != NULL) 14375 kern_prefetch(rsm, &prev_state); 14376 prev_state = rack->r_state; 14377 retval = (*rack->r_substate) (m, th, so, 14378 tp, &to, drop_hdrlen, 14379 tlen, tiwin, thflags, nxt_pkt, iptos); 14380 #ifdef INVARIANTS 14381 if ((retval == 0) && 14382 (tp->t_inpcb == NULL)) { 14383 panic("retval:%d tp:%p t_inpcb:NULL state:%d", 14384 retval, tp, prev_state); 14385 } 14386 #endif 14387 if (retval == 0) { 14388 /* 14389 * If retval is 1 the tcb is unlocked and most likely the tp 14390 * is gone. 14391 */ 14392 INP_WLOCK_ASSERT(tp->t_inpcb); 14393 if ((rack->rc_gp_dyn_mul) && 14394 (rack->rc_always_pace) && 14395 (rack->use_fixed_rate == 0) && 14396 rack->in_probe_rtt && 14397 (rack->r_ctl.rc_time_probertt_starts == 0)) { 14398 /* 14399 * If we are going for target, lets recheck before 14400 * we output. 14401 */ 14402 rack_check_probe_rtt(rack, us_cts); 14403 } 14404 if (rack->set_pacing_done_a_iw == 0) { 14405 /* How much has been acked? */ 14406 if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) { 14407 /* We have enough to set in the pacing segment size */ 14408 rack->set_pacing_done_a_iw = 1; 14409 rack_set_pace_segments(tp, rack, __LINE__, NULL); 14410 } 14411 } 14412 tcp_rack_xmit_timer_commit(rack, tp); 14413 #ifdef TCP_ACCOUNTING 14414 /* 14415 * If we set the ack_val_se to what ack processing we are doing 14416 * we also want to track how many cycles we burned. Note 14417 * the bits after tcp_output we let be "free". This is because 14418 * we are also tracking the tcp_output times as well. Note the 14419 * use of 0xf here since we only have 11 counter (0 - 0xa) and 14420 * 0xf cannot be returned and is what we initialize it too to 14421 * indicate we are not doing the tabulations. 14422 */ 14423 if (ack_val_set != 0xf) { 14424 uint64_t crtsc; 14425 14426 crtsc = get_cyclecount(); 14427 counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val)); 14428 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 14429 tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val); 14430 } 14431 } 14432 #endif 14433 if (nxt_pkt == 0) { 14434 if ((rack->r_wanted_output != 0) || (rack->r_fast_output != 0)) { 14435 do_output_now: 14436 did_out = 1; 14437 (void)tp->t_fb->tfb_tcp_output(tp); 14438 } 14439 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 14440 rack_free_trim(rack); 14441 } 14442 if ((nxt_pkt == 0) && 14443 ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 14444 (SEQ_GT(tp->snd_max, tp->snd_una) || 14445 (tp->t_flags & TF_DELACK) || 14446 ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 14447 (tp->t_state <= TCPS_CLOSING)))) { 14448 /* We could not send (probably in the hpts but stopped the timer earlier)? */ 14449 if ((tp->snd_max == tp->snd_una) && 14450 ((tp->t_flags & TF_DELACK) == 0) && 14451 (rack->rc_inp->inp_in_hpts) && 14452 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 14453 /* keep alive not needed if we are hptsi output yet */ 14454 ; 14455 } else { 14456 int late = 0; 14457 if (rack->rc_inp->inp_in_hpts) { 14458 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14459 us_cts = tcp_get_usecs(NULL); 14460 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) { 14461 rack->r_early = 1; 14462 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts); 14463 } else 14464 late = 1; 14465 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 14466 } 14467 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_OUTPUT); 14468 } 14469 if (late && (did_out == 0)) { 14470 /* 14471 * We are late in the sending 14472 * and we did not call the output 14473 * (this probably should not happen). 14474 */ 14475 goto do_output_now; 14476 } 14477 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 14478 } 14479 way_out = 1; 14480 } else if (nxt_pkt == 0) { 14481 /* Do we have the correct timer running? */ 14482 rack_timer_audit(tp, rack, &so->so_snd); 14483 way_out = 2; 14484 } 14485 done_with_input: 14486 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs)); 14487 if (did_out) 14488 rack->r_wanted_output = 0; 14489 #ifdef INVARIANTS 14490 if (tp->t_inpcb == NULL) { 14491 panic("OP:%d retval:%d tp:%p t_inpcb:NULL state:%d", 14492 did_out, 14493 retval, tp, prev_state); 14494 } 14495 #endif 14496 #ifdef TCP_ACCOUNTING 14497 } else { 14498 /* 14499 * Track the time (see above). 14500 */ 14501 if (ack_val_set != 0xf) { 14502 uint64_t crtsc; 14503 14504 crtsc = get_cyclecount(); 14505 counter_u64_add(tcp_proc_time[ack_val_set] , (crtsc - ts_val)); 14506 /* 14507 * Note we *DO NOT* increment the per-tcb counters since 14508 * in the else the TP may be gone!! 14509 */ 14510 } 14511 #endif 14512 } 14513 #ifdef TCP_ACCOUNTING 14514 sched_unpin(); 14515 #endif 14516 return (retval); 14517 } 14518 14519 void 14520 rack_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 14521 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 14522 { 14523 struct timeval tv; 14524 14525 /* First lets see if we have old packets */ 14526 if (tp->t_in_pkt) { 14527 if (ctf_do_queued_segments(so, tp, 1)) { 14528 m_freem(m); 14529 return; 14530 } 14531 } 14532 if (m->m_flags & M_TSTMP_LRO) { 14533 tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000; 14534 tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000; 14535 } else { 14536 /* Should not be should we kassert instead? */ 14537 tcp_get_usecs(&tv); 14538 } 14539 if (rack_do_segment_nounlock(m, th, so, tp, 14540 drop_hdrlen, tlen, iptos, 0, &tv) == 0) { 14541 INP_WUNLOCK(tp->t_inpcb); 14542 } 14543 } 14544 14545 struct rack_sendmap * 14546 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused) 14547 { 14548 struct rack_sendmap *rsm = NULL; 14549 int32_t idx; 14550 uint32_t srtt = 0, thresh = 0, ts_low = 0; 14551 14552 /* Return the next guy to be re-transmitted */ 14553 if (RB_EMPTY(&rack->r_ctl.rc_mtree)) { 14554 return (NULL); 14555 } 14556 if (tp->t_flags & TF_SENTFIN) { 14557 /* retran the end FIN? */ 14558 return (NULL); 14559 } 14560 /* ok lets look at this one */ 14561 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 14562 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) { 14563 goto check_it; 14564 } 14565 rsm = rack_find_lowest_rsm(rack); 14566 if (rsm == NULL) { 14567 return (NULL); 14568 } 14569 check_it: 14570 if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) && 14571 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) { 14572 /* 14573 * No sack so we automatically do the 3 strikes and 14574 * retransmit (no rack timer would be started). 14575 */ 14576 14577 return (rsm); 14578 } 14579 if (rsm->r_flags & RACK_ACKED) { 14580 return (NULL); 14581 } 14582 if (((rsm->r_flags & RACK_SACK_PASSED) == 0) && 14583 (rsm->r_dupack < DUP_ACK_THRESHOLD)) { 14584 /* Its not yet ready */ 14585 return (NULL); 14586 } 14587 srtt = rack_grab_rtt(tp, rack); 14588 idx = rsm->r_rtr_cnt - 1; 14589 ts_low = (uint32_t)rsm->r_tim_lastsent[idx]; 14590 thresh = rack_calc_thresh_rack(rack, srtt, tsused); 14591 if ((tsused == ts_low) || 14592 (TSTMP_LT(tsused, ts_low))) { 14593 /* No time since sending */ 14594 return (NULL); 14595 } 14596 if ((tsused - ts_low) < thresh) { 14597 /* It has not been long enough yet */ 14598 return (NULL); 14599 } 14600 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 14601 ((rsm->r_flags & RACK_SACK_PASSED) && 14602 (rack->sack_attack_disable == 0))) { 14603 /* 14604 * We have passed the dup-ack threshold <or> 14605 * a SACK has indicated this is missing. 14606 * Note that if you are a declared attacker 14607 * it is only the dup-ack threshold that 14608 * will cause retransmits. 14609 */ 14610 /* log retransmit reason */ 14611 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1); 14612 rack->r_fast_output = 0; 14613 return (rsm); 14614 } 14615 return (NULL); 14616 } 14617 14618 static void 14619 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot, 14620 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, 14621 int line, struct rack_sendmap *rsm, uint8_t quality) 14622 { 14623 if (rack->rc_tp->t_logstate != TCP_LOG_STATE_OFF) { 14624 union tcp_log_stackspecific log; 14625 struct timeval tv; 14626 14627 memset(&log, 0, sizeof(log)); 14628 log.u_bbr.flex1 = slot; 14629 log.u_bbr.flex2 = len; 14630 log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs; 14631 log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs; 14632 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss; 14633 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca; 14634 log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data; 14635 log.u_bbr.use_lt_bw <<= 1; 14636 log.u_bbr.use_lt_bw |= rack->r_late; 14637 log.u_bbr.use_lt_bw <<= 1; 14638 log.u_bbr.use_lt_bw |= rack->r_early; 14639 log.u_bbr.use_lt_bw <<= 1; 14640 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set; 14641 log.u_bbr.use_lt_bw <<= 1; 14642 log.u_bbr.use_lt_bw |= rack->rc_gp_filled; 14643 log.u_bbr.use_lt_bw <<= 1; 14644 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt; 14645 log.u_bbr.use_lt_bw <<= 1; 14646 log.u_bbr.use_lt_bw |= rack->in_probe_rtt; 14647 log.u_bbr.use_lt_bw <<= 1; 14648 log.u_bbr.use_lt_bw |= rack->gp_ready; 14649 log.u_bbr.pkt_epoch = line; 14650 log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed; 14651 log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early; 14652 log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec; 14653 log.u_bbr.bw_inuse = bw_est; 14654 log.u_bbr.delRate = bw; 14655 if (rack->r_ctl.gp_bw == 0) 14656 log.u_bbr.cur_del_rate = 0; 14657 else 14658 log.u_bbr.cur_del_rate = rack_get_bw(rack); 14659 log.u_bbr.rttProp = len_time; 14660 log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt; 14661 log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit; 14662 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm); 14663 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) { 14664 /* We are in slow start */ 14665 log.u_bbr.flex7 = 1; 14666 } else { 14667 /* we are on congestion avoidance */ 14668 log.u_bbr.flex7 = 0; 14669 } 14670 log.u_bbr.flex8 = method; 14671 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 14672 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 14673 log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec; 14674 log.u_bbr.cwnd_gain <<= 1; 14675 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss; 14676 log.u_bbr.cwnd_gain <<= 1; 14677 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca; 14678 log.u_bbr.bbr_substate = quality; 14679 TCP_LOG_EVENTP(rack->rc_tp, NULL, 14680 &rack->rc_inp->inp_socket->so_rcv, 14681 &rack->rc_inp->inp_socket->so_snd, 14682 BBR_LOG_HPTSI_CALC, 0, 14683 0, &log, false, &tv); 14684 } 14685 } 14686 14687 static uint32_t 14688 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss) 14689 { 14690 uint32_t new_tso, user_max; 14691 14692 user_max = rack->rc_user_set_max_segs * mss; 14693 if (rack->rc_force_max_seg) { 14694 return (user_max); 14695 } 14696 if (rack->use_fixed_rate && 14697 ((rack->r_ctl.crte == NULL) || 14698 (bw != rack->r_ctl.crte->rate))) { 14699 /* Use the user mss since we are not exactly matched */ 14700 return (user_max); 14701 } 14702 new_tso = tcp_get_pacing_burst_size(rack->rc_tp, bw, mss, rack_pace_one_seg, rack->r_ctl.crte, NULL); 14703 if (new_tso > user_max) 14704 new_tso = user_max; 14705 return (new_tso); 14706 } 14707 14708 static int32_t 14709 pace_to_fill_cwnd(struct tcp_rack *rack, int32_t slot, uint32_t len, uint32_t segsiz, int *capped, uint64_t *rate_wanted, uint8_t non_paced) 14710 { 14711 uint64_t lentim, fill_bw; 14712 14713 /* Lets first see if we are full, if so continue with normal rate */ 14714 rack->r_via_fill_cw = 0; 14715 if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use) 14716 return (slot); 14717 if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd) 14718 return (slot); 14719 if (rack->r_ctl.rc_last_us_rtt == 0) 14720 return (slot); 14721 if (rack->rc_pace_fill_if_rttin_range && 14722 (rack->r_ctl.rc_last_us_rtt >= 14723 (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) { 14724 /* The rtt is huge, N * smallest, lets not fill */ 14725 return (slot); 14726 } 14727 /* 14728 * first lets calculate the b/w based on the last us-rtt 14729 * and the sndwnd. 14730 */ 14731 fill_bw = rack->r_ctl.cwnd_to_use; 14732 /* Take the rwnd if its smaller */ 14733 if (fill_bw > rack->rc_tp->snd_wnd) 14734 fill_bw = rack->rc_tp->snd_wnd; 14735 if (rack->r_fill_less_agg) { 14736 /* 14737 * Now take away the inflight (this will reduce our 14738 * aggressiveness and yeah, if we get that much out in 1RTT 14739 * we will have had acks come back and still be behind). 14740 */ 14741 fill_bw -= ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 14742 } 14743 /* Now lets make it into a b/w */ 14744 fill_bw *= (uint64_t)HPTS_USEC_IN_SEC; 14745 fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt; 14746 /* We are below the min b/w */ 14747 if (non_paced) 14748 *rate_wanted = fill_bw; 14749 if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted)) 14750 return (slot); 14751 if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) 14752 fill_bw = rack->r_ctl.bw_rate_cap; 14753 rack->r_via_fill_cw = 1; 14754 if (rack->r_rack_hw_rate_caps && 14755 (rack->r_ctl.crte != NULL)) { 14756 uint64_t high_rate; 14757 14758 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte); 14759 if (fill_bw > high_rate) { 14760 /* We are capping bw at the highest rate table entry */ 14761 if (*rate_wanted > high_rate) { 14762 /* The original rate was also capped */ 14763 rack->r_via_fill_cw = 0; 14764 } 14765 rack_log_hdwr_pacing(rack, 14766 fill_bw, high_rate, __LINE__, 14767 0, 3); 14768 fill_bw = high_rate; 14769 if (capped) 14770 *capped = 1; 14771 } 14772 } else if ((rack->r_ctl.crte == NULL) && 14773 (rack->rack_hdrw_pacing == 0) && 14774 (rack->rack_hdw_pace_ena) && 14775 rack->r_rack_hw_rate_caps && 14776 (rack->rack_attempt_hdwr_pace == 0) && 14777 (rack->rc_inp->inp_route.ro_nh != NULL) && 14778 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 14779 /* 14780 * Ok we may have a first attempt that is greater than our top rate 14781 * lets check. 14782 */ 14783 uint64_t high_rate; 14784 14785 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp); 14786 if (high_rate) { 14787 if (fill_bw > high_rate) { 14788 fill_bw = high_rate; 14789 if (capped) 14790 *capped = 1; 14791 } 14792 } 14793 } 14794 /* 14795 * Ok fill_bw holds our mythical b/w to fill the cwnd 14796 * in a rtt, what does that time wise equate too? 14797 */ 14798 lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC; 14799 lentim /= fill_bw; 14800 *rate_wanted = fill_bw; 14801 if (non_paced || (lentim < slot)) { 14802 rack_log_pacing_delay_calc(rack, len, slot, fill_bw, 14803 0, lentim, 12, __LINE__, NULL, 0); 14804 return ((int32_t)lentim); 14805 } else 14806 return (slot); 14807 } 14808 14809 static int32_t 14810 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz) 14811 { 14812 int32_t slot = 0; 14813 int can_start_hw_pacing = 1; 14814 int err; 14815 14816 if (rack->rc_always_pace == 0) { 14817 /* 14818 * We use the most optimistic possible cwnd/srtt for 14819 * sending calculations. This will make our 14820 * calculation anticipate getting more through 14821 * quicker then possible. But thats ok we don't want 14822 * the peer to have a gap in data sending. 14823 */ 14824 uint32_t srtt, cwnd, tr_perms = 0; 14825 int32_t reduce = 0; 14826 14827 old_method: 14828 /* 14829 * We keep no precise pacing with the old method 14830 * instead we use the pacer to mitigate bursts. 14831 */ 14832 if (rack->r_ctl.rc_rack_min_rtt) 14833 srtt = rack->r_ctl.rc_rack_min_rtt; 14834 else 14835 srtt = max(tp->t_srtt, 1); 14836 if (rack->r_ctl.rc_rack_largest_cwnd) 14837 cwnd = rack->r_ctl.rc_rack_largest_cwnd; 14838 else 14839 cwnd = rack->r_ctl.cwnd_to_use; 14840 /* Inflate cwnd by 1000 so srtt of usecs is in ms */ 14841 tr_perms = (cwnd * 1000) / srtt; 14842 if (tr_perms == 0) { 14843 tr_perms = ctf_fixed_maxseg(tp); 14844 } 14845 /* 14846 * Calculate how long this will take to drain, if 14847 * the calculation comes out to zero, thats ok we 14848 * will use send_a_lot to possibly spin around for 14849 * more increasing tot_len_this_send to the point 14850 * that its going to require a pace, or we hit the 14851 * cwnd. Which in that case we are just waiting for 14852 * a ACK. 14853 */ 14854 slot = len / tr_perms; 14855 /* Now do we reduce the time so we don't run dry? */ 14856 if (slot && rack_slot_reduction) { 14857 reduce = (slot / rack_slot_reduction); 14858 if (reduce < slot) { 14859 slot -= reduce; 14860 } else 14861 slot = 0; 14862 } 14863 slot *= HPTS_USEC_IN_MSEC; 14864 if (rack->rc_pace_to_cwnd) { 14865 uint64_t rate_wanted = 0; 14866 14867 slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1); 14868 rack->rc_ack_can_sendout_data = 1; 14869 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0); 14870 } else 14871 rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0); 14872 } else { 14873 uint64_t bw_est, res, lentim, rate_wanted; 14874 uint32_t orig_val, srtt, segs, oh; 14875 int capped = 0; 14876 int prev_fill; 14877 14878 if ((rack->r_rr_config == 1) && rsm) { 14879 return (rack->r_ctl.rc_min_to); 14880 } 14881 if (rack->use_fixed_rate) { 14882 rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack); 14883 } else if ((rack->r_ctl.init_rate == 0) && 14884 #ifdef NETFLIX_PEAKRATE 14885 (rack->rc_tp->t_maxpeakrate == 0) && 14886 #endif 14887 (rack->r_ctl.gp_bw == 0)) { 14888 /* no way to yet do an estimate */ 14889 bw_est = rate_wanted = 0; 14890 } else { 14891 bw_est = rack_get_bw(rack); 14892 rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped); 14893 } 14894 if ((bw_est == 0) || (rate_wanted == 0) || 14895 ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) { 14896 /* 14897 * No way yet to make a b/w estimate or 14898 * our raise is set incorrectly. 14899 */ 14900 goto old_method; 14901 } 14902 /* We need to account for all the overheads */ 14903 segs = (len + segsiz - 1) / segsiz; 14904 /* 14905 * We need the diff between 1514 bytes (e-mtu with e-hdr) 14906 * and how much data we put in each packet. Yes this 14907 * means we may be off if we are larger than 1500 bytes 14908 * or smaller. But this just makes us more conservative. 14909 */ 14910 if (rack_hw_rate_min && 14911 (bw_est < rack_hw_rate_min)) 14912 can_start_hw_pacing = 0; 14913 if (ETHERNET_SEGMENT_SIZE > segsiz) 14914 oh = ETHERNET_SEGMENT_SIZE - segsiz; 14915 else 14916 oh = 0; 14917 segs *= oh; 14918 lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC; 14919 res = lentim / rate_wanted; 14920 slot = (uint32_t)res; 14921 orig_val = rack->r_ctl.rc_pace_max_segs; 14922 if (rack->r_ctl.crte == NULL) { 14923 /* 14924 * Only do this if we are not hardware pacing 14925 * since if we are doing hw-pacing below we will 14926 * set make a call after setting up or changing 14927 * the rate. 14928 */ 14929 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 14930 } else if (rack->rc_inp->inp_snd_tag == NULL) { 14931 /* 14932 * We lost our rate somehow, this can happen 14933 * if the interface changed underneath us. 14934 */ 14935 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 14936 rack->r_ctl.crte = NULL; 14937 /* Lets re-allow attempting to setup pacing */ 14938 rack->rack_hdrw_pacing = 0; 14939 rack->rack_attempt_hdwr_pace = 0; 14940 rack_log_hdwr_pacing(rack, 14941 rate_wanted, bw_est, __LINE__, 14942 0, 6); 14943 } 14944 /* Did we change the TSO size, if so log it */ 14945 if (rack->r_ctl.rc_pace_max_segs != orig_val) 14946 rack_log_pacing_delay_calc(rack, len, slot, orig_val, 0, 0, 15, __LINE__, NULL, 0); 14947 prev_fill = rack->r_via_fill_cw; 14948 if ((rack->rc_pace_to_cwnd) && 14949 (capped == 0) && 14950 (rack->use_fixed_rate == 0) && 14951 (rack->in_probe_rtt == 0) && 14952 (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) { 14953 /* 14954 * We want to pace at our rate *or* faster to 14955 * fill the cwnd to the max if its not full. 14956 */ 14957 slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0); 14958 } 14959 if ((rack->rc_inp->inp_route.ro_nh != NULL) && 14960 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 14961 if ((rack->rack_hdw_pace_ena) && 14962 (can_start_hw_pacing > 0) && 14963 (rack->rack_hdrw_pacing == 0) && 14964 (rack->rack_attempt_hdwr_pace == 0)) { 14965 /* 14966 * Lets attempt to turn on hardware pacing 14967 * if we can. 14968 */ 14969 rack->rack_attempt_hdwr_pace = 1; 14970 rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp, 14971 rack->rc_inp->inp_route.ro_nh->nh_ifp, 14972 rate_wanted, 14973 RS_PACING_GEQ, 14974 &err, &rack->r_ctl.crte_prev_rate); 14975 if (rack->r_ctl.crte) { 14976 rack->rack_hdrw_pacing = 1; 14977 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted, segsiz, 14978 0, rack->r_ctl.crte, 14979 NULL); 14980 rack_log_hdwr_pacing(rack, 14981 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 14982 err, 0); 14983 rack->r_ctl.last_hw_bw_req = rate_wanted; 14984 } else { 14985 counter_u64_add(rack_hw_pace_init_fail, 1); 14986 } 14987 } else if (rack->rack_hdrw_pacing && 14988 (rack->r_ctl.last_hw_bw_req != rate_wanted)) { 14989 /* Do we need to adjust our rate? */ 14990 const struct tcp_hwrate_limit_table *nrte; 14991 14992 if (rack->r_up_only && 14993 (rate_wanted < rack->r_ctl.crte->rate)) { 14994 /** 14995 * We have four possible states here 14996 * having to do with the previous time 14997 * and this time. 14998 * previous | this-time 14999 * A) 0 | 0 -- fill_cw not in the picture 15000 * B) 1 | 0 -- we were doing a fill-cw but now are not 15001 * C) 1 | 1 -- all rates from fill_cw 15002 * D) 0 | 1 -- we were doing non-fill and now we are filling 15003 * 15004 * For case A, C and D we don't allow a drop. But for 15005 * case B where we now our on our steady rate we do 15006 * allow a drop. 15007 * 15008 */ 15009 if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0))) 15010 goto done_w_hdwr; 15011 } 15012 if ((rate_wanted > rack->r_ctl.crte->rate) || 15013 (rate_wanted <= rack->r_ctl.crte_prev_rate)) { 15014 if (rack_hw_rate_to_low && 15015 (bw_est < rack_hw_rate_to_low)) { 15016 /* 15017 * The pacing rate is too low for hardware, but 15018 * do allow hardware pacing to be restarted. 15019 */ 15020 rack_log_hdwr_pacing(rack, 15021 bw_est, rack->r_ctl.crte->rate, __LINE__, 15022 0, 5); 15023 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 15024 rack->r_ctl.crte = NULL; 15025 rack->rack_attempt_hdwr_pace = 0; 15026 rack->rack_hdrw_pacing = 0; 15027 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 15028 goto done_w_hdwr; 15029 } 15030 nrte = tcp_chg_pacing_rate(rack->r_ctl.crte, 15031 rack->rc_tp, 15032 rack->rc_inp->inp_route.ro_nh->nh_ifp, 15033 rate_wanted, 15034 RS_PACING_GEQ, 15035 &err, &rack->r_ctl.crte_prev_rate); 15036 if (nrte == NULL) { 15037 /* Lost the rate */ 15038 rack->rack_hdrw_pacing = 0; 15039 rack->r_ctl.crte = NULL; 15040 rack_log_hdwr_pacing(rack, 15041 rate_wanted, 0, __LINE__, 15042 err, 1); 15043 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 15044 counter_u64_add(rack_hw_pace_lost, 1); 15045 } else if (nrte != rack->r_ctl.crte) { 15046 rack->r_ctl.crte = nrte; 15047 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size(tp, rate_wanted, 15048 segsiz, 0, 15049 rack->r_ctl.crte, 15050 NULL); 15051 rack_log_hdwr_pacing(rack, 15052 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 15053 err, 2); 15054 rack->r_ctl.last_hw_bw_req = rate_wanted; 15055 } 15056 } else { 15057 /* We just need to adjust the segment size */ 15058 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 15059 rack_log_hdwr_pacing(rack, 15060 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 15061 0, 4); 15062 rack->r_ctl.last_hw_bw_req = rate_wanted; 15063 } 15064 } 15065 } 15066 if ((rack->r_ctl.crte != NULL) && 15067 (rack->r_ctl.crte->rate == rate_wanted)) { 15068 /* 15069 * We need to add a extra if the rates 15070 * are exactly matched. The idea is 15071 * we want the software to make sure the 15072 * queue is empty before adding more, this 15073 * gives us N MSS extra pace times where 15074 * N is our sysctl 15075 */ 15076 slot += (rack->r_ctl.crte->time_between * rack_hw_pace_extra_slots); 15077 } 15078 done_w_hdwr: 15079 if (rack_limit_time_with_srtt && 15080 (rack->use_fixed_rate == 0) && 15081 #ifdef NETFLIX_PEAKRATE 15082 (rack->rc_tp->t_maxpeakrate == 0) && 15083 #endif 15084 (rack->rack_hdrw_pacing == 0)) { 15085 /* 15086 * Sanity check, we do not allow the pacing delay 15087 * to be longer than the SRTT of the path. If it is 15088 * a slow path, then adding a packet should increase 15089 * the RTT and compensate for this i.e. the srtt will 15090 * be greater so the allowed pacing time will be greater. 15091 * 15092 * Note this restriction is not for where a peak rate 15093 * is set, we are doing fixed pacing or hardware pacing. 15094 */ 15095 if (rack->rc_tp->t_srtt) 15096 srtt = rack->rc_tp->t_srtt; 15097 else 15098 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC; /* its in ms convert */ 15099 if (srtt < slot) { 15100 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0); 15101 slot = srtt; 15102 } 15103 } 15104 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0); 15105 } 15106 if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) { 15107 /* 15108 * If this rate is seeing enobufs when it 15109 * goes to send then either the nic is out 15110 * of gas or we are mis-estimating the time 15111 * somehow and not letting the queue empty 15112 * completely. Lets add to the pacing time. 15113 */ 15114 int hw_boost_delay; 15115 15116 hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult; 15117 if (hw_boost_delay > rack_enobuf_hw_max) 15118 hw_boost_delay = rack_enobuf_hw_max; 15119 else if (hw_boost_delay < rack_enobuf_hw_min) 15120 hw_boost_delay = rack_enobuf_hw_min; 15121 slot += hw_boost_delay; 15122 } 15123 if (slot) 15124 counter_u64_add(rack_calc_nonzero, 1); 15125 else 15126 counter_u64_add(rack_calc_zero, 1); 15127 return (slot); 15128 } 15129 15130 static void 15131 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack, 15132 tcp_seq startseq, uint32_t sb_offset) 15133 { 15134 struct rack_sendmap *my_rsm = NULL; 15135 struct rack_sendmap fe; 15136 15137 if (tp->t_state < TCPS_ESTABLISHED) { 15138 /* 15139 * We don't start any measurements if we are 15140 * not at least established. 15141 */ 15142 return; 15143 } 15144 if (tp->t_state >= TCPS_FIN_WAIT_1) { 15145 /* 15146 * We will get no more data into the SB 15147 * this means we need to have the data available 15148 * before we start a measurement. 15149 */ 15150 15151 if (sbavail(&tp->t_inpcb->inp_socket->so_snd) < 15152 max(rc_init_window(rack), 15153 (MIN_GP_WIN * ctf_fixed_maxseg(tp)))) { 15154 /* Nope not enough data */ 15155 return; 15156 } 15157 } 15158 tp->t_flags |= TF_GPUTINPROG; 15159 rack->r_ctl.rc_gp_lowrtt = 0xffffffff; 15160 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 15161 tp->gput_seq = startseq; 15162 rack->app_limited_needs_set = 0; 15163 if (rack->in_probe_rtt) 15164 rack->measure_saw_probe_rtt = 1; 15165 else if ((rack->measure_saw_probe_rtt) && 15166 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 15167 rack->measure_saw_probe_rtt = 0; 15168 if (rack->rc_gp_filled) 15169 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 15170 else { 15171 /* Special case initial measurement */ 15172 struct timeval tv; 15173 15174 tp->gput_ts = tcp_get_usecs(&tv); 15175 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 15176 } 15177 /* 15178 * We take a guess out into the future, 15179 * if we have no measurement and no 15180 * initial rate, we measure the first 15181 * initial-windows worth of data to 15182 * speed up getting some GP measurement and 15183 * thus start pacing. 15184 */ 15185 if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) { 15186 rack->app_limited_needs_set = 1; 15187 tp->gput_ack = startseq + max(rc_init_window(rack), 15188 (MIN_GP_WIN * ctf_fixed_maxseg(tp))); 15189 rack_log_pacing_delay_calc(rack, 15190 tp->gput_seq, 15191 tp->gput_ack, 15192 0, 15193 tp->gput_ts, 15194 rack->r_ctl.rc_app_limited_cnt, 15195 9, 15196 __LINE__, NULL, 0); 15197 return; 15198 } 15199 if (sb_offset) { 15200 /* 15201 * We are out somewhere in the sb 15202 * can we use the already outstanding data? 15203 */ 15204 if (rack->r_ctl.rc_app_limited_cnt == 0) { 15205 /* 15206 * Yes first one is good and in this case 15207 * the tp->gput_ts is correctly set based on 15208 * the last ack that arrived (no need to 15209 * set things up when an ack comes in). 15210 */ 15211 my_rsm = RB_MIN(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 15212 if ((my_rsm == NULL) || 15213 (my_rsm->r_rtr_cnt != 1)) { 15214 /* retransmission? */ 15215 goto use_latest; 15216 } 15217 } else { 15218 if (rack->r_ctl.rc_first_appl == NULL) { 15219 /* 15220 * If rc_first_appl is NULL 15221 * then the cnt should be 0. 15222 * This is probably an error, maybe 15223 * a KASSERT would be approprate. 15224 */ 15225 goto use_latest; 15226 } 15227 /* 15228 * If we have a marker pointer to the last one that is 15229 * app limited we can use that, but we need to set 15230 * things up so that when it gets ack'ed we record 15231 * the ack time (if its not already acked). 15232 */ 15233 rack->app_limited_needs_set = 1; 15234 /* 15235 * We want to get to the rsm that is either 15236 * next with space i.e. over 1 MSS or the one 15237 * after that (after the app-limited). 15238 */ 15239 my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, 15240 rack->r_ctl.rc_first_appl); 15241 if (my_rsm) { 15242 if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp)) 15243 /* Have to use the next one */ 15244 my_rsm = RB_NEXT(rack_rb_tree_head, &rack->r_ctl.rc_mtree, 15245 my_rsm); 15246 else { 15247 /* Use after the first MSS of it is acked */ 15248 tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp); 15249 goto start_set; 15250 } 15251 } 15252 if ((my_rsm == NULL) || 15253 (my_rsm->r_rtr_cnt != 1)) { 15254 /* 15255 * Either its a retransmit or 15256 * the last is the app-limited one. 15257 */ 15258 goto use_latest; 15259 } 15260 } 15261 tp->gput_seq = my_rsm->r_start; 15262 start_set: 15263 if (my_rsm->r_flags & RACK_ACKED) { 15264 /* 15265 * This one has been acked use the arrival ack time 15266 */ 15267 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival; 15268 rack->app_limited_needs_set = 0; 15269 } 15270 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)]; 15271 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 15272 rack_log_pacing_delay_calc(rack, 15273 tp->gput_seq, 15274 tp->gput_ack, 15275 (uint64_t)my_rsm, 15276 tp->gput_ts, 15277 rack->r_ctl.rc_app_limited_cnt, 15278 9, 15279 __LINE__, NULL, 0); 15280 return; 15281 } 15282 15283 use_latest: 15284 /* 15285 * We don't know how long we may have been 15286 * idle or if this is the first-send. Lets 15287 * setup the flag so we will trim off 15288 * the first ack'd data so we get a true 15289 * measurement. 15290 */ 15291 rack->app_limited_needs_set = 1; 15292 tp->gput_ack = startseq + rack_get_measure_window(tp, rack); 15293 /* Find this guy so we can pull the send time */ 15294 fe.r_start = startseq; 15295 my_rsm = RB_FIND(rack_rb_tree_head, &rack->r_ctl.rc_mtree, &fe); 15296 if (my_rsm) { 15297 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[(my_rsm->r_rtr_cnt-1)]; 15298 if (my_rsm->r_flags & RACK_ACKED) { 15299 /* 15300 * Unlikely since its probably what was 15301 * just transmitted (but I am paranoid). 15302 */ 15303 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival; 15304 rack->app_limited_needs_set = 0; 15305 } 15306 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) { 15307 /* This also is unlikely */ 15308 tp->gput_seq = my_rsm->r_start; 15309 } 15310 } else { 15311 /* 15312 * TSNH unless we have some send-map limit, 15313 * and even at that it should not be hitting 15314 * that limit (we should have stopped sending). 15315 */ 15316 struct timeval tv; 15317 15318 microuptime(&tv); 15319 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 15320 } 15321 rack_log_pacing_delay_calc(rack, 15322 tp->gput_seq, 15323 tp->gput_ack, 15324 (uint64_t)my_rsm, 15325 tp->gput_ts, 15326 rack->r_ctl.rc_app_limited_cnt, 15327 9, __LINE__, NULL, 0); 15328 } 15329 15330 static inline uint32_t 15331 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cwnd_to_use, 15332 uint32_t avail, int32_t sb_offset) 15333 { 15334 uint32_t len; 15335 uint32_t sendwin; 15336 15337 if (tp->snd_wnd > cwnd_to_use) 15338 sendwin = cwnd_to_use; 15339 else 15340 sendwin = tp->snd_wnd; 15341 if (ctf_outstanding(tp) >= tp->snd_wnd) { 15342 /* We never want to go over our peers rcv-window */ 15343 len = 0; 15344 } else { 15345 uint32_t flight; 15346 15347 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked); 15348 if (flight >= sendwin) { 15349 /* 15350 * We have in flight what we are allowed by cwnd (if 15351 * it was rwnd blocking it would have hit above out 15352 * >= tp->snd_wnd). 15353 */ 15354 return (0); 15355 } 15356 len = sendwin - flight; 15357 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 15358 /* We would send too much (beyond the rwnd) */ 15359 len = tp->snd_wnd - ctf_outstanding(tp); 15360 } 15361 if ((len + sb_offset) > avail) { 15362 /* 15363 * We don't have that much in the SB, how much is 15364 * there? 15365 */ 15366 len = avail - sb_offset; 15367 } 15368 } 15369 return (len); 15370 } 15371 15372 static void 15373 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags, 15374 unsigned ipoptlen, int32_t orig_len, int32_t len, int error, 15375 int rsm_is_null, int optlen, int line, uint16_t mode) 15376 { 15377 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 15378 union tcp_log_stackspecific log; 15379 struct timeval tv; 15380 15381 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 15382 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 15383 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 15384 log.u_bbr.flex1 = error; 15385 log.u_bbr.flex2 = flags; 15386 log.u_bbr.flex3 = rsm_is_null; 15387 log.u_bbr.flex4 = ipoptlen; 15388 log.u_bbr.flex5 = tp->rcv_numsacks; 15389 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 15390 log.u_bbr.flex7 = optlen; 15391 log.u_bbr.flex8 = rack->r_fsb_inited; 15392 log.u_bbr.applimited = rack->r_fast_output; 15393 log.u_bbr.bw_inuse = rack_get_bw(rack); 15394 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 15395 log.u_bbr.cwnd_gain = mode; 15396 log.u_bbr.pkts_out = orig_len; 15397 log.u_bbr.lt_epoch = len; 15398 log.u_bbr.delivered = line; 15399 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 15400 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 15401 tcp_log_event_(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0, 15402 len, &log, false, NULL, NULL, 0, &tv); 15403 } 15404 } 15405 15406 15407 static struct mbuf * 15408 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen, 15409 struct rack_fast_send_blk *fsb, 15410 int32_t seglimit, int32_t segsize, int hw_tls) 15411 { 15412 #ifdef KERN_TLS 15413 struct ktls_session *tls, *ntls; 15414 struct mbuf *start; 15415 #endif 15416 struct mbuf *m, *n, **np, *smb; 15417 struct mbuf *top; 15418 int32_t off, soff; 15419 int32_t len = *plen; 15420 int32_t fragsize; 15421 int32_t len_cp = 0; 15422 uint32_t mlen, frags; 15423 15424 soff = off = the_off; 15425 smb = m = the_m; 15426 np = ⊤ 15427 top = NULL; 15428 #ifdef KERN_TLS 15429 if (hw_tls && (m->m_flags & M_EXTPG)) 15430 tls = m->m_epg_tls; 15431 else 15432 tls = NULL; 15433 start = m; 15434 #endif 15435 while (len > 0) { 15436 if (m == NULL) { 15437 *plen = len_cp; 15438 break; 15439 } 15440 #ifdef KERN_TLS 15441 if (hw_tls) { 15442 if (m->m_flags & M_EXTPG) 15443 ntls = m->m_epg_tls; 15444 else 15445 ntls = NULL; 15446 15447 /* 15448 * Avoid mixing TLS records with handshake 15449 * data or TLS records from different 15450 * sessions. 15451 */ 15452 if (tls != ntls) { 15453 MPASS(m != start); 15454 *plen = len_cp; 15455 break; 15456 } 15457 } 15458 #endif 15459 mlen = min(len, m->m_len - off); 15460 if (seglimit) { 15461 /* 15462 * For M_EXTPG mbufs, add 3 segments 15463 * + 1 in case we are crossing page boundaries 15464 * + 2 in case the TLS hdr/trailer are used 15465 * It is cheaper to just add the segments 15466 * than it is to take the cache miss to look 15467 * at the mbuf ext_pgs state in detail. 15468 */ 15469 if (m->m_flags & M_EXTPG) { 15470 fragsize = min(segsize, PAGE_SIZE); 15471 frags = 3; 15472 } else { 15473 fragsize = segsize; 15474 frags = 0; 15475 } 15476 15477 /* Break if we really can't fit anymore. */ 15478 if ((frags + 1) >= seglimit) { 15479 *plen = len_cp; 15480 break; 15481 } 15482 15483 /* 15484 * Reduce size if you can't copy the whole 15485 * mbuf. If we can't copy the whole mbuf, also 15486 * adjust len so the loop will end after this 15487 * mbuf. 15488 */ 15489 if ((frags + howmany(mlen, fragsize)) >= seglimit) { 15490 mlen = (seglimit - frags - 1) * fragsize; 15491 len = mlen; 15492 *plen = len_cp + len; 15493 } 15494 frags += howmany(mlen, fragsize); 15495 if (frags == 0) 15496 frags++; 15497 seglimit -= frags; 15498 KASSERT(seglimit > 0, 15499 ("%s: seglimit went too low", __func__)); 15500 } 15501 n = m_get(M_NOWAIT, m->m_type); 15502 *np = n; 15503 if (n == NULL) 15504 goto nospace; 15505 n->m_len = mlen; 15506 soff += mlen; 15507 len_cp += n->m_len; 15508 if (m->m_flags & (M_EXT|M_EXTPG)) { 15509 n->m_data = m->m_data + off; 15510 mb_dupcl(n, m); 15511 } else { 15512 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 15513 (u_int)n->m_len); 15514 } 15515 len -= n->m_len; 15516 off = 0; 15517 m = m->m_next; 15518 np = &n->m_next; 15519 if (len || (soff == smb->m_len)) { 15520 /* 15521 * We have more so we move forward or 15522 * we have consumed the entire mbuf and 15523 * len has fell to 0. 15524 */ 15525 soff = 0; 15526 smb = m; 15527 } 15528 15529 } 15530 if (fsb != NULL) { 15531 fsb->m = smb; 15532 fsb->off = soff; 15533 if (smb) { 15534 /* 15535 * Save off the size of the mbuf. We do 15536 * this so that we can recognize when it 15537 * has been trimmed by sbcut() as acks 15538 * come in. 15539 */ 15540 fsb->o_m_len = smb->m_len; 15541 } else { 15542 /* 15543 * This is the case where the next mbuf went to NULL. This 15544 * means with this copy we have sent everything in the sb. 15545 * In theory we could clear the fast_output flag, but lets 15546 * not since its possible that we could get more added 15547 * and acks that call the extend function which would let 15548 * us send more. 15549 */ 15550 fsb->o_m_len = 0; 15551 } 15552 } 15553 return (top); 15554 nospace: 15555 if (top) 15556 m_freem(top); 15557 return (NULL); 15558 15559 } 15560 15561 /* 15562 * This is a copy of m_copym(), taking the TSO segment size/limit 15563 * constraints into account, and advancing the sndptr as it goes. 15564 */ 15565 static struct mbuf * 15566 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen, 15567 int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff) 15568 { 15569 struct mbuf *m, *n; 15570 int32_t soff; 15571 15572 soff = rack->r_ctl.fsb.off; 15573 m = rack->r_ctl.fsb.m; 15574 if (rack->r_ctl.fsb.o_m_len != m->m_len) { 15575 /* 15576 * The mbuf had the front of it chopped off by an ack 15577 * we need to adjust the soff/off by that difference. 15578 */ 15579 uint32_t delta; 15580 15581 delta = rack->r_ctl.fsb.o_m_len - m->m_len; 15582 soff -= delta; 15583 } 15584 KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff)); 15585 KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen)); 15586 KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?", 15587 __FUNCTION__, 15588 rack, *plen, m, m->m_len)); 15589 /* Save off the right location before we copy and advance */ 15590 *s_soff = soff; 15591 *s_mb = rack->r_ctl.fsb.m; 15592 n = rack_fo_base_copym(m, soff, plen, 15593 &rack->r_ctl.fsb, 15594 seglimit, segsize, rack->r_ctl.fsb.hw_tls); 15595 return (n); 15596 } 15597 15598 static int 15599 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm, 15600 uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp) 15601 { 15602 /* 15603 * Enter the fast retransmit path. We are given that a sched_pin is 15604 * in place (if accounting is compliled in) and the cycle count taken 15605 * at the entry is in the ts_val. The concept her is that the rsm 15606 * now holds the mbuf offsets and such so we can directly transmit 15607 * without a lot of overhead, the len field is already set for 15608 * us to prohibit us from sending too much (usually its 1MSS). 15609 */ 15610 struct ip *ip = NULL; 15611 struct udphdr *udp = NULL; 15612 struct tcphdr *th = NULL; 15613 struct mbuf *m = NULL; 15614 struct inpcb *inp; 15615 uint8_t *cpto; 15616 struct tcp_log_buffer *lgb; 15617 #ifdef TCP_ACCOUNTING 15618 uint64_t crtsc; 15619 int cnt_thru = 1; 15620 #endif 15621 struct tcpopt to; 15622 u_char opt[TCP_MAXOLEN]; 15623 uint32_t hdrlen, optlen; 15624 int32_t slot, segsiz, max_val, tso = 0, error, flags, ulen = 0; 15625 uint32_t us_cts; 15626 uint32_t if_hw_tsomaxsegcount = 0, startseq; 15627 uint32_t if_hw_tsomaxsegsize; 15628 15629 #ifdef INET6 15630 struct ip6_hdr *ip6 = NULL; 15631 15632 if (rack->r_is_v6) { 15633 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 15634 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 15635 } else 15636 #endif /* INET6 */ 15637 { 15638 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 15639 hdrlen = sizeof(struct tcpiphdr); 15640 } 15641 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) { 15642 goto failed; 15643 } 15644 if (doing_tlp) { 15645 /* Its a TLP add the flag, it may already be there but be sure */ 15646 rsm->r_flags |= RACK_TLP; 15647 } else { 15648 /* If it was a TLP it is not not on this retransmit */ 15649 rsm->r_flags &= ~RACK_TLP; 15650 } 15651 startseq = rsm->r_start; 15652 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 15653 inp = rack->rc_inp; 15654 to.to_flags = 0; 15655 flags = tcp_outflags[tp->t_state]; 15656 if (flags & (TH_SYN|TH_RST)) { 15657 goto failed; 15658 } 15659 if (rsm->r_flags & RACK_HAS_FIN) { 15660 /* We can't send a FIN here */ 15661 goto failed; 15662 } 15663 if (flags & TH_FIN) { 15664 /* We never send a FIN */ 15665 flags &= ~TH_FIN; 15666 } 15667 if (tp->t_flags & TF_RCVD_TSTMP) { 15668 to.to_tsval = ms_cts + tp->ts_offset; 15669 to.to_tsecr = tp->ts_recent; 15670 to.to_flags = TOF_TS; 15671 } 15672 optlen = tcp_addoptions(&to, opt); 15673 hdrlen += optlen; 15674 udp = rack->r_ctl.fsb.udp; 15675 if (udp) 15676 hdrlen += sizeof(struct udphdr); 15677 if (rack->r_ctl.rc_pace_max_segs) 15678 max_val = rack->r_ctl.rc_pace_max_segs; 15679 else if (rack->rc_user_set_max_segs) 15680 max_val = rack->rc_user_set_max_segs * segsiz; 15681 else 15682 max_val = len; 15683 if ((tp->t_flags & TF_TSO) && 15684 V_tcp_do_tso && 15685 (len > segsiz) && 15686 (tp->t_port == 0)) 15687 tso = 1; 15688 #ifdef INET6 15689 if (MHLEN < hdrlen + max_linkhdr) 15690 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 15691 else 15692 #endif 15693 m = m_gethdr(M_NOWAIT, MT_DATA); 15694 if (m == NULL) 15695 goto failed; 15696 m->m_data += max_linkhdr; 15697 m->m_len = hdrlen; 15698 th = rack->r_ctl.fsb.th; 15699 /* Establish the len to send */ 15700 if (len > max_val) 15701 len = max_val; 15702 if ((tso) && (len + optlen > tp->t_maxseg)) { 15703 uint32_t if_hw_tsomax; 15704 int32_t max_len; 15705 15706 /* extract TSO information */ 15707 if_hw_tsomax = tp->t_tsomax; 15708 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 15709 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 15710 /* 15711 * Check if we should limit by maximum payload 15712 * length: 15713 */ 15714 if (if_hw_tsomax != 0) { 15715 /* compute maximum TSO length */ 15716 max_len = (if_hw_tsomax - hdrlen - 15717 max_linkhdr); 15718 if (max_len <= 0) { 15719 goto failed; 15720 } else if (len > max_len) { 15721 len = max_len; 15722 } 15723 } 15724 if (len <= segsiz) { 15725 /* 15726 * In case there are too many small fragments don't 15727 * use TSO: 15728 */ 15729 tso = 0; 15730 } 15731 } else { 15732 tso = 0; 15733 } 15734 if ((tso == 0) && (len > segsiz)) 15735 len = segsiz; 15736 us_cts = tcp_get_usecs(tv); 15737 if ((len == 0) || 15738 (len <= MHLEN - hdrlen - max_linkhdr)) { 15739 goto failed; 15740 } 15741 th->th_seq = htonl(rsm->r_start); 15742 th->th_ack = htonl(tp->rcv_nxt); 15743 /* 15744 * The PUSH bit should only be applied 15745 * if the full retransmission is made. If 15746 * we are sending less than this is the 15747 * left hand edge and should not have 15748 * the PUSH bit. 15749 */ 15750 if ((rsm->r_flags & RACK_HAD_PUSH) && 15751 (len == (rsm->r_end - rsm->r_start))) 15752 flags |= TH_PUSH; 15753 th->th_flags = flags; 15754 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale)); 15755 if (th->th_win == 0) { 15756 tp->t_sndzerowin++; 15757 tp->t_flags |= TF_RXWIN0SENT; 15758 } else 15759 tp->t_flags &= ~TF_RXWIN0SENT; 15760 if (rsm->r_flags & RACK_TLP) { 15761 /* 15762 * TLP should not count in retran count, but 15763 * in its own bin 15764 */ 15765 counter_u64_add(rack_tlp_retran, 1); 15766 counter_u64_add(rack_tlp_retran_bytes, len); 15767 } else { 15768 tp->t_sndrexmitpack++; 15769 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 15770 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 15771 } 15772 #ifdef STATS 15773 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 15774 len); 15775 #endif 15776 if (rsm->m == NULL) 15777 goto failed; 15778 if (rsm->orig_m_len != rsm->m->m_len) { 15779 /* Fix up the orig_m_len and possibly the mbuf offset */ 15780 rack_adjust_orig_mlen(rsm); 15781 } 15782 m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls); 15783 if (len <= segsiz) { 15784 /* 15785 * Must have ran out of mbufs for the copy 15786 * shorten it to no longer need tso. Lets 15787 * not put on sendalot since we are low on 15788 * mbufs. 15789 */ 15790 tso = 0; 15791 } 15792 if ((m->m_next == NULL) || (len <= 0)){ 15793 goto failed; 15794 } 15795 if (udp) { 15796 if (rack->r_is_v6) 15797 ulen = hdrlen + len - sizeof(struct ip6_hdr); 15798 else 15799 ulen = hdrlen + len - sizeof(struct ip); 15800 udp->uh_ulen = htons(ulen); 15801 } 15802 m->m_pkthdr.rcvif = (struct ifnet *)0; 15803 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 15804 #ifdef INET6 15805 if (rack->r_is_v6) { 15806 if (tp->t_port) { 15807 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 15808 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 15809 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 15810 th->th_sum = htons(0); 15811 UDPSTAT_INC(udps_opackets); 15812 } else { 15813 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 15814 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 15815 th->th_sum = in6_cksum_pseudo(ip6, 15816 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 15817 0); 15818 } 15819 } 15820 #endif 15821 #if defined(INET6) && defined(INET) 15822 else 15823 #endif 15824 #ifdef INET 15825 { 15826 if (tp->t_port) { 15827 m->m_pkthdr.csum_flags = CSUM_UDP; 15828 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 15829 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 15830 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 15831 th->th_sum = htons(0); 15832 UDPSTAT_INC(udps_opackets); 15833 } else { 15834 m->m_pkthdr.csum_flags = CSUM_TCP; 15835 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 15836 th->th_sum = in_pseudo(ip->ip_src.s_addr, 15837 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 15838 IPPROTO_TCP + len + optlen)); 15839 } 15840 /* IP version must be set here for ipv4/ipv6 checking later */ 15841 KASSERT(ip->ip_v == IPVERSION, 15842 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 15843 } 15844 #endif 15845 if (tso) { 15846 KASSERT(len > tp->t_maxseg - optlen, 15847 ("%s: len <= tso_segsz tp:%p", __func__, tp)); 15848 m->m_pkthdr.csum_flags |= CSUM_TSO; 15849 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 15850 } 15851 #ifdef INET6 15852 if (rack->r_is_v6) { 15853 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit; 15854 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 15855 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 15856 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 15857 else 15858 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 15859 } 15860 #endif 15861 #if defined(INET) && defined(INET6) 15862 else 15863 #endif 15864 #ifdef INET 15865 { 15866 ip->ip_len = htons(m->m_pkthdr.len); 15867 ip->ip_ttl = rack->r_ctl.fsb.hoplimit; 15868 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 15869 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 15870 if (tp->t_port == 0 || len < V_tcp_minmss) { 15871 ip->ip_off |= htons(IP_DF); 15872 } 15873 } else { 15874 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 15875 } 15876 } 15877 #endif 15878 /* Time to copy in our header */ 15879 cpto = mtod(m, uint8_t *); 15880 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 15881 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 15882 if (optlen) { 15883 bcopy(opt, th + 1, optlen); 15884 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 15885 } else { 15886 th->th_off = sizeof(struct tcphdr) >> 2; 15887 } 15888 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 15889 union tcp_log_stackspecific log; 15890 15891 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 15892 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 15893 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 15894 if (rack->rack_no_prr) 15895 log.u_bbr.flex1 = 0; 15896 else 15897 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 15898 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 15899 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 15900 log.u_bbr.flex4 = max_val; 15901 log.u_bbr.flex5 = 0; 15902 /* Save off the early/late values */ 15903 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 15904 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 15905 log.u_bbr.bw_inuse = rack_get_bw(rack); 15906 if (doing_tlp == 0) 15907 log.u_bbr.flex8 = 1; 15908 else 15909 log.u_bbr.flex8 = 2; 15910 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 15911 log.u_bbr.flex7 = 55; 15912 log.u_bbr.pkts_out = tp->t_maxseg; 15913 log.u_bbr.timeStamp = cts; 15914 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 15915 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use; 15916 log.u_bbr.delivered = 0; 15917 lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, 15918 len, &log, false, NULL, NULL, 0, tv); 15919 } else 15920 lgb = NULL; 15921 #ifdef INET6 15922 if (rack->r_is_v6) { 15923 error = ip6_output(m, NULL, 15924 &inp->inp_route6, 15925 0, NULL, NULL, inp); 15926 } 15927 #endif 15928 #if defined(INET) && defined(INET6) 15929 else 15930 #endif 15931 #ifdef INET 15932 { 15933 error = ip_output(m, NULL, 15934 &inp->inp_route, 15935 0, 0, inp); 15936 } 15937 #endif 15938 m = NULL; 15939 if (lgb) { 15940 lgb->tlb_errno = error; 15941 lgb = NULL; 15942 } 15943 if (error) { 15944 goto failed; 15945 } 15946 rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv), 15947 rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls); 15948 if (doing_tlp && (rack->fast_rsm_hack == 0)) { 15949 rack->rc_tlp_in_progress = 1; 15950 rack->r_ctl.rc_tlp_cnt_out++; 15951 } 15952 if (error == 0) { 15953 tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls); 15954 if (doing_tlp) { 15955 rack->rc_last_sent_tlp_past_cumack = 0; 15956 rack->rc_last_sent_tlp_seq_valid = 1; 15957 rack->r_ctl.last_sent_tlp_seq = rsm->r_start; 15958 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start; 15959 } 15960 } 15961 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 15962 rack->forced_ack = 0; /* If we send something zap the FA flag */ 15963 if (IN_FASTRECOVERY(tp->t_flags) && rsm) 15964 rack->r_ctl.retran_during_recovery += len; 15965 { 15966 int idx; 15967 15968 idx = (len / segsiz) + 3; 15969 if (idx >= TCP_MSS_ACCT_ATIMER) 15970 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 15971 else 15972 counter_u64_add(rack_out_size[idx], 1); 15973 } 15974 if (tp->t_rtttime == 0) { 15975 tp->t_rtttime = ticks; 15976 tp->t_rtseq = startseq; 15977 KMOD_TCPSTAT_INC(tcps_segstimed); 15978 } 15979 counter_u64_add(rack_fto_rsm_send, 1); 15980 if (error && (error == ENOBUFS)) { 15981 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC); 15982 if (rack->rc_enobuf < 0x7f) 15983 rack->rc_enobuf++; 15984 if (slot < (10 * HPTS_USEC_IN_MSEC)) 15985 slot = 10 * HPTS_USEC_IN_MSEC; 15986 } else 15987 slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz); 15988 if ((slot == 0) || 15989 (rack->rc_always_pace == 0) || 15990 (rack->r_rr_config == 1)) { 15991 /* 15992 * We have no pacing set or we 15993 * are using old-style rack or 15994 * we are overriden to use the old 1ms pacing. 15995 */ 15996 slot = rack->r_ctl.rc_min_to; 15997 } 15998 rack_start_hpts_timer(rack, tp, cts, slot, len, 0); 15999 if (rack->r_must_retran) { 16000 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start); 16001 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) { 16002 /* 16003 * We have retransmitted all we need. 16004 */ 16005 rack->r_must_retran = 0; 16006 rack->r_ctl.rc_out_at_rto = 0; 16007 } 16008 } 16009 #ifdef TCP_ACCOUNTING 16010 crtsc = get_cyclecount(); 16011 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16012 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru; 16013 } 16014 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru); 16015 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16016 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 16017 } 16018 counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val)); 16019 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16020 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz); 16021 } 16022 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((len + segsiz - 1) / segsiz)); 16023 sched_unpin(); 16024 #endif 16025 return (0); 16026 failed: 16027 if (m) 16028 m_free(m); 16029 return (-1); 16030 } 16031 16032 static void 16033 rack_sndbuf_autoscale(struct tcp_rack *rack) 16034 { 16035 /* 16036 * Automatic sizing of send socket buffer. Often the send buffer 16037 * size is not optimally adjusted to the actual network conditions 16038 * at hand (delay bandwidth product). Setting the buffer size too 16039 * small limits throughput on links with high bandwidth and high 16040 * delay (eg. trans-continental/oceanic links). Setting the 16041 * buffer size too big consumes too much real kernel memory, 16042 * especially with many connections on busy servers. 16043 * 16044 * The criteria to step up the send buffer one notch are: 16045 * 1. receive window of remote host is larger than send buffer 16046 * (with a fudge factor of 5/4th); 16047 * 2. send buffer is filled to 7/8th with data (so we actually 16048 * have data to make use of it); 16049 * 3. send buffer fill has not hit maximal automatic size; 16050 * 4. our send window (slow start and cogestion controlled) is 16051 * larger than sent but unacknowledged data in send buffer. 16052 * 16053 * Note that the rack version moves things much faster since 16054 * we want to avoid hitting cache lines in the rack_fast_output() 16055 * path so this is called much less often and thus moves 16056 * the SB forward by a percentage. 16057 */ 16058 struct socket *so; 16059 struct tcpcb *tp; 16060 uint32_t sendwin, scaleup; 16061 16062 tp = rack->rc_tp; 16063 so = rack->rc_inp->inp_socket; 16064 sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd); 16065 if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 16066 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 16067 sbused(&so->so_snd) >= 16068 (so->so_snd.sb_hiwat / 8 * 7) && 16069 sbused(&so->so_snd) < V_tcp_autosndbuf_max && 16070 sendwin >= (sbused(&so->so_snd) - 16071 (tp->snd_nxt - tp->snd_una))) { 16072 if (rack_autosndbuf_inc) 16073 scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100; 16074 else 16075 scaleup = V_tcp_autosndbuf_inc; 16076 if (scaleup < V_tcp_autosndbuf_inc) 16077 scaleup = V_tcp_autosndbuf_inc; 16078 scaleup += so->so_snd.sb_hiwat; 16079 if (scaleup > V_tcp_autosndbuf_max) 16080 scaleup = V_tcp_autosndbuf_max; 16081 if (!sbreserve_locked(&so->so_snd, scaleup, so, curthread)) 16082 so->so_snd.sb_flags &= ~SB_AUTOSIZE; 16083 } 16084 } 16085 } 16086 16087 static int 16088 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val, 16089 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err) 16090 { 16091 /* 16092 * Enter to do fast output. We are given that the sched_pin is 16093 * in place (if accounting is compiled in) and the cycle count taken 16094 * at entry is in place in ts_val. The idea here is that 16095 * we know how many more bytes needs to be sent (presumably either 16096 * during pacing or to fill the cwnd and that was greater than 16097 * the max-burst). We have how much to send and all the info we 16098 * need to just send. 16099 */ 16100 struct ip *ip = NULL; 16101 struct udphdr *udp = NULL; 16102 struct tcphdr *th = NULL; 16103 struct mbuf *m, *s_mb; 16104 struct inpcb *inp; 16105 uint8_t *cpto; 16106 struct tcp_log_buffer *lgb; 16107 #ifdef TCP_ACCOUNTING 16108 uint64_t crtsc; 16109 #endif 16110 struct tcpopt to; 16111 u_char opt[TCP_MAXOLEN]; 16112 uint32_t hdrlen, optlen; 16113 int cnt_thru = 1; 16114 int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, flags, ulen = 0; 16115 uint32_t us_cts, s_soff; 16116 uint32_t if_hw_tsomaxsegcount = 0, startseq; 16117 uint32_t if_hw_tsomaxsegsize; 16118 uint16_t add_flag = RACK_SENT_FP; 16119 #ifdef INET6 16120 struct ip6_hdr *ip6 = NULL; 16121 16122 if (rack->r_is_v6) { 16123 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 16124 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 16125 } else 16126 #endif /* INET6 */ 16127 { 16128 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 16129 hdrlen = sizeof(struct tcpiphdr); 16130 } 16131 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) { 16132 m = NULL; 16133 goto failed; 16134 } 16135 startseq = tp->snd_max; 16136 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 16137 inp = rack->rc_inp; 16138 len = rack->r_ctl.fsb.left_to_send; 16139 to.to_flags = 0; 16140 flags = rack->r_ctl.fsb.tcp_flags; 16141 if (tp->t_flags & TF_RCVD_TSTMP) { 16142 to.to_tsval = ms_cts + tp->ts_offset; 16143 to.to_tsecr = tp->ts_recent; 16144 to.to_flags = TOF_TS; 16145 } 16146 optlen = tcp_addoptions(&to, opt); 16147 hdrlen += optlen; 16148 udp = rack->r_ctl.fsb.udp; 16149 if (udp) 16150 hdrlen += sizeof(struct udphdr); 16151 if (rack->r_ctl.rc_pace_max_segs) 16152 max_val = rack->r_ctl.rc_pace_max_segs; 16153 else if (rack->rc_user_set_max_segs) 16154 max_val = rack->rc_user_set_max_segs * segsiz; 16155 else 16156 max_val = len; 16157 if ((tp->t_flags & TF_TSO) && 16158 V_tcp_do_tso && 16159 (len > segsiz) && 16160 (tp->t_port == 0)) 16161 tso = 1; 16162 again: 16163 #ifdef INET6 16164 if (MHLEN < hdrlen + max_linkhdr) 16165 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 16166 else 16167 #endif 16168 m = m_gethdr(M_NOWAIT, MT_DATA); 16169 if (m == NULL) 16170 goto failed; 16171 m->m_data += max_linkhdr; 16172 m->m_len = hdrlen; 16173 th = rack->r_ctl.fsb.th; 16174 /* Establish the len to send */ 16175 if (len > max_val) 16176 len = max_val; 16177 if ((tso) && (len + optlen > tp->t_maxseg)) { 16178 uint32_t if_hw_tsomax; 16179 int32_t max_len; 16180 16181 /* extract TSO information */ 16182 if_hw_tsomax = tp->t_tsomax; 16183 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 16184 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 16185 /* 16186 * Check if we should limit by maximum payload 16187 * length: 16188 */ 16189 if (if_hw_tsomax != 0) { 16190 /* compute maximum TSO length */ 16191 max_len = (if_hw_tsomax - hdrlen - 16192 max_linkhdr); 16193 if (max_len <= 0) { 16194 goto failed; 16195 } else if (len > max_len) { 16196 len = max_len; 16197 } 16198 } 16199 if (len <= segsiz) { 16200 /* 16201 * In case there are too many small fragments don't 16202 * use TSO: 16203 */ 16204 tso = 0; 16205 } 16206 } else { 16207 tso = 0; 16208 } 16209 if ((tso == 0) && (len > segsiz)) 16210 len = segsiz; 16211 us_cts = tcp_get_usecs(tv); 16212 if ((len == 0) || 16213 (len <= MHLEN - hdrlen - max_linkhdr)) { 16214 goto failed; 16215 } 16216 sb_offset = tp->snd_max - tp->snd_una; 16217 th->th_seq = htonl(tp->snd_max); 16218 th->th_ack = htonl(tp->rcv_nxt); 16219 th->th_flags = flags; 16220 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale)); 16221 if (th->th_win == 0) { 16222 tp->t_sndzerowin++; 16223 tp->t_flags |= TF_RXWIN0SENT; 16224 } else 16225 tp->t_flags &= ~TF_RXWIN0SENT; 16226 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */ 16227 KMOD_TCPSTAT_INC(tcps_sndpack); 16228 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 16229 #ifdef STATS 16230 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 16231 len); 16232 #endif 16233 if (rack->r_ctl.fsb.m == NULL) 16234 goto failed; 16235 16236 /* s_mb and s_soff are saved for rack_log_output */ 16237 m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, 16238 &s_mb, &s_soff); 16239 if (len <= segsiz) { 16240 /* 16241 * Must have ran out of mbufs for the copy 16242 * shorten it to no longer need tso. Lets 16243 * not put on sendalot since we are low on 16244 * mbufs. 16245 */ 16246 tso = 0; 16247 } 16248 if (rack->r_ctl.fsb.rfo_apply_push && 16249 (len == rack->r_ctl.fsb.left_to_send)) { 16250 th->th_flags |= TH_PUSH; 16251 add_flag |= RACK_HAD_PUSH; 16252 } 16253 if ((m->m_next == NULL) || (len <= 0)){ 16254 goto failed; 16255 } 16256 if (udp) { 16257 if (rack->r_is_v6) 16258 ulen = hdrlen + len - sizeof(struct ip6_hdr); 16259 else 16260 ulen = hdrlen + len - sizeof(struct ip); 16261 udp->uh_ulen = htons(ulen); 16262 } 16263 m->m_pkthdr.rcvif = (struct ifnet *)0; 16264 if (tp->t_state == TCPS_ESTABLISHED && 16265 (tp->t_flags2 & TF2_ECN_PERMIT)) { 16266 /* 16267 * If the peer has ECN, mark data packets with ECN capable 16268 * transmission (ECT). Ignore pure ack packets, 16269 * retransmissions. 16270 */ 16271 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max)) { 16272 #ifdef INET6 16273 if (rack->r_is_v6) 16274 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 16275 else 16276 #endif 16277 ip->ip_tos |= IPTOS_ECN_ECT0; 16278 KMOD_TCPSTAT_INC(tcps_ecn_ect0); 16279 /* 16280 * Reply with proper ECN notifications. 16281 * Only set CWR on new data segments. 16282 */ 16283 if (tp->t_flags2 & TF2_ECN_SND_CWR) { 16284 flags |= TH_CWR; 16285 tp->t_flags2 &= ~TF2_ECN_SND_CWR; 16286 } 16287 } 16288 if (tp->t_flags2 & TF2_ECN_SND_ECE) 16289 flags |= TH_ECE; 16290 } 16291 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 16292 #ifdef INET6 16293 if (rack->r_is_v6) { 16294 if (tp->t_port) { 16295 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 16296 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 16297 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 16298 th->th_sum = htons(0); 16299 UDPSTAT_INC(udps_opackets); 16300 } else { 16301 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 16302 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 16303 th->th_sum = in6_cksum_pseudo(ip6, 16304 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 16305 0); 16306 } 16307 } 16308 #endif 16309 #if defined(INET6) && defined(INET) 16310 else 16311 #endif 16312 #ifdef INET 16313 { 16314 if (tp->t_port) { 16315 m->m_pkthdr.csum_flags = CSUM_UDP; 16316 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 16317 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 16318 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 16319 th->th_sum = htons(0); 16320 UDPSTAT_INC(udps_opackets); 16321 } else { 16322 m->m_pkthdr.csum_flags = CSUM_TCP; 16323 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 16324 th->th_sum = in_pseudo(ip->ip_src.s_addr, 16325 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 16326 IPPROTO_TCP + len + optlen)); 16327 } 16328 /* IP version must be set here for ipv4/ipv6 checking later */ 16329 KASSERT(ip->ip_v == IPVERSION, 16330 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 16331 } 16332 #endif 16333 if (tso) { 16334 KASSERT(len > tp->t_maxseg - optlen, 16335 ("%s: len <= tso_segsz tp:%p", __func__, tp)); 16336 m->m_pkthdr.csum_flags |= CSUM_TSO; 16337 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 16338 } 16339 #ifdef INET6 16340 if (rack->r_is_v6) { 16341 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit; 16342 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 16343 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 16344 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 16345 else 16346 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 16347 } 16348 #endif 16349 #if defined(INET) && defined(INET6) 16350 else 16351 #endif 16352 #ifdef INET 16353 { 16354 ip->ip_len = htons(m->m_pkthdr.len); 16355 ip->ip_ttl = rack->r_ctl.fsb.hoplimit; 16356 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 16357 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 16358 if (tp->t_port == 0 || len < V_tcp_minmss) { 16359 ip->ip_off |= htons(IP_DF); 16360 } 16361 } else { 16362 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 16363 } 16364 } 16365 #endif 16366 /* Time to copy in our header */ 16367 cpto = mtod(m, uint8_t *); 16368 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 16369 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 16370 if (optlen) { 16371 bcopy(opt, th + 1, optlen); 16372 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 16373 } else { 16374 th->th_off = sizeof(struct tcphdr) >> 2; 16375 } 16376 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 16377 union tcp_log_stackspecific log; 16378 16379 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 16380 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 16381 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 16382 if (rack->rack_no_prr) 16383 log.u_bbr.flex1 = 0; 16384 else 16385 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 16386 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 16387 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 16388 log.u_bbr.flex4 = max_val; 16389 log.u_bbr.flex5 = 0; 16390 /* Save off the early/late values */ 16391 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 16392 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 16393 log.u_bbr.bw_inuse = rack_get_bw(rack); 16394 log.u_bbr.flex8 = 0; 16395 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 16396 log.u_bbr.flex7 = 44; 16397 log.u_bbr.pkts_out = tp->t_maxseg; 16398 log.u_bbr.timeStamp = cts; 16399 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 16400 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use; 16401 log.u_bbr.delivered = 0; 16402 lgb = tcp_log_event_(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, 16403 len, &log, false, NULL, NULL, 0, tv); 16404 } else 16405 lgb = NULL; 16406 #ifdef INET6 16407 if (rack->r_is_v6) { 16408 error = ip6_output(m, NULL, 16409 &inp->inp_route6, 16410 0, NULL, NULL, inp); 16411 } 16412 #endif 16413 #if defined(INET) && defined(INET6) 16414 else 16415 #endif 16416 #ifdef INET 16417 { 16418 error = ip_output(m, NULL, 16419 &inp->inp_route, 16420 0, 0, inp); 16421 } 16422 #endif 16423 if (lgb) { 16424 lgb->tlb_errno = error; 16425 lgb = NULL; 16426 } 16427 if (error) { 16428 *send_err = error; 16429 m = NULL; 16430 goto failed; 16431 } 16432 rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv), 16433 NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls); 16434 m = NULL; 16435 if (tp->snd_una == tp->snd_max) { 16436 rack->r_ctl.rc_tlp_rxt_last_time = cts; 16437 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 16438 tp->t_acktime = ticks; 16439 } 16440 if (error == 0) 16441 tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls); 16442 16443 rack->forced_ack = 0; /* If we send something zap the FA flag */ 16444 tot_len += len; 16445 if ((tp->t_flags & TF_GPUTINPROG) == 0) 16446 rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset); 16447 tp->snd_max += len; 16448 tp->snd_nxt = tp->snd_max; 16449 { 16450 int idx; 16451 16452 idx = (len / segsiz) + 3; 16453 if (idx >= TCP_MSS_ACCT_ATIMER) 16454 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 16455 else 16456 counter_u64_add(rack_out_size[idx], 1); 16457 } 16458 if (len <= rack->r_ctl.fsb.left_to_send) 16459 rack->r_ctl.fsb.left_to_send -= len; 16460 else 16461 rack->r_ctl.fsb.left_to_send = 0; 16462 if (rack->r_ctl.fsb.left_to_send < segsiz) { 16463 rack->r_fast_output = 0; 16464 rack->r_ctl.fsb.left_to_send = 0; 16465 /* At the end of fast_output scale up the sb */ 16466 SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd); 16467 rack_sndbuf_autoscale(rack); 16468 SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd); 16469 } 16470 if (tp->t_rtttime == 0) { 16471 tp->t_rtttime = ticks; 16472 tp->t_rtseq = startseq; 16473 KMOD_TCPSTAT_INC(tcps_segstimed); 16474 } 16475 if ((rack->r_ctl.fsb.left_to_send >= segsiz) && 16476 (max_val > len) && 16477 (tso == 0)) { 16478 max_val -= len; 16479 len = segsiz; 16480 th = rack->r_ctl.fsb.th; 16481 cnt_thru++; 16482 goto again; 16483 } 16484 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 16485 counter_u64_add(rack_fto_send, 1); 16486 slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz); 16487 rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0); 16488 #ifdef TCP_ACCOUNTING 16489 crtsc = get_cyclecount(); 16490 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16491 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru; 16492 } 16493 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], cnt_thru); 16494 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16495 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 16496 } 16497 counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val)); 16498 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16499 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz); 16500 } 16501 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len + segsiz - 1) / segsiz)); 16502 sched_unpin(); 16503 #endif 16504 return (0); 16505 failed: 16506 if (m) 16507 m_free(m); 16508 rack->r_fast_output = 0; 16509 return (-1); 16510 } 16511 16512 static int 16513 rack_output(struct tcpcb *tp) 16514 { 16515 struct socket *so; 16516 uint32_t recwin; 16517 uint32_t sb_offset, s_moff = 0; 16518 int32_t len, flags, error = 0; 16519 struct mbuf *m, *s_mb = NULL; 16520 struct mbuf *mb; 16521 uint32_t if_hw_tsomaxsegcount = 0; 16522 uint32_t if_hw_tsomaxsegsize; 16523 int32_t segsiz, minseg; 16524 long tot_len_this_send = 0; 16525 #ifdef INET 16526 struct ip *ip = NULL; 16527 #endif 16528 #ifdef TCPDEBUG 16529 struct ipovly *ipov = NULL; 16530 #endif 16531 struct udphdr *udp = NULL; 16532 struct tcp_rack *rack; 16533 struct tcphdr *th; 16534 uint8_t pass = 0; 16535 uint8_t mark = 0; 16536 uint8_t wanted_cookie = 0; 16537 u_char opt[TCP_MAXOLEN]; 16538 unsigned ipoptlen, optlen, hdrlen, ulen=0; 16539 uint32_t rack_seq; 16540 16541 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 16542 unsigned ipsec_optlen = 0; 16543 16544 #endif 16545 int32_t idle, sendalot; 16546 int32_t sub_from_prr = 0; 16547 volatile int32_t sack_rxmit; 16548 struct rack_sendmap *rsm = NULL; 16549 int32_t tso, mtu; 16550 struct tcpopt to; 16551 int32_t slot = 0; 16552 int32_t sup_rack = 0; 16553 uint32_t cts, ms_cts, delayed, early; 16554 uint16_t add_flag = RACK_SENT_SP; 16555 /* The doing_tlp flag will be set by the actual rack_timeout_tlp() */ 16556 uint8_t hpts_calling, doing_tlp = 0; 16557 uint32_t cwnd_to_use, pace_max_seg; 16558 int32_t do_a_prefetch = 0; 16559 int32_t prefetch_rsm = 0; 16560 int32_t orig_len = 0; 16561 struct timeval tv; 16562 int32_t prefetch_so_done = 0; 16563 struct tcp_log_buffer *lgb; 16564 struct inpcb *inp; 16565 struct sockbuf *sb; 16566 uint64_t ts_val = 0; 16567 #ifdef TCP_ACCOUNTING 16568 uint64_t crtsc; 16569 #endif 16570 #ifdef INET6 16571 struct ip6_hdr *ip6 = NULL; 16572 int32_t isipv6; 16573 #endif 16574 uint8_t filled_all = 0; 16575 bool hw_tls = false; 16576 16577 /* setup and take the cache hits here */ 16578 rack = (struct tcp_rack *)tp->t_fb_ptr; 16579 #ifdef TCP_ACCOUNTING 16580 sched_pin(); 16581 ts_val = get_cyclecount(); 16582 #endif 16583 hpts_calling = rack->rc_inp->inp_hpts_calls; 16584 NET_EPOCH_ASSERT(); 16585 INP_WLOCK_ASSERT(rack->rc_inp); 16586 #ifdef TCP_OFFLOAD 16587 if (tp->t_flags & TF_TOE) { 16588 #ifdef TCP_ACCOUNTING 16589 sched_unpin(); 16590 #endif 16591 return (tcp_offload_output(tp)); 16592 } 16593 #endif 16594 /* 16595 * For TFO connections in SYN_RECEIVED, only allow the initial 16596 * SYN|ACK and those sent by the retransmit timer. 16597 */ 16598 if (IS_FASTOPEN(tp->t_flags) && 16599 (tp->t_state == TCPS_SYN_RECEIVED) && 16600 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN|ACK sent */ 16601 (rack->r_ctl.rc_resend == NULL)) { /* not a retransmit */ 16602 #ifdef TCP_ACCOUNTING 16603 sched_unpin(); 16604 #endif 16605 return (0); 16606 } 16607 #ifdef INET6 16608 if (rack->r_state) { 16609 /* Use the cache line loaded if possible */ 16610 isipv6 = rack->r_is_v6; 16611 } else { 16612 isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0; 16613 } 16614 #endif 16615 early = 0; 16616 cts = tcp_get_usecs(&tv); 16617 ms_cts = tcp_tv_to_mssectick(&tv); 16618 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 16619 rack->rc_inp->inp_in_hpts) { 16620 /* 16621 * We are on the hpts for some timer but not hptsi output. 16622 * Remove from the hpts unconditionally. 16623 */ 16624 rack_timer_cancel(tp, rack, cts, __LINE__); 16625 } 16626 /* Are we pacing and late? */ 16627 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 16628 TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) { 16629 /* We are delayed */ 16630 delayed = cts - rack->r_ctl.rc_last_output_to; 16631 } else { 16632 delayed = 0; 16633 } 16634 /* Do the timers, which may override the pacer */ 16635 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 16636 if (rack_process_timers(tp, rack, cts, hpts_calling, &doing_tlp)) { 16637 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1); 16638 #ifdef TCP_ACCOUNTING 16639 sched_unpin(); 16640 #endif 16641 return (0); 16642 } 16643 } 16644 if (rack->rc_in_persist) { 16645 if (rack->rc_inp->inp_in_hpts == 0) { 16646 /* Timer is not running */ 16647 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 16648 } 16649 #ifdef TCP_ACCOUNTING 16650 sched_unpin(); 16651 #endif 16652 return (0); 16653 } 16654 if ((rack->r_timer_override) || 16655 (rack->rc_ack_can_sendout_data) || 16656 (delayed) || 16657 (tp->t_state < TCPS_ESTABLISHED)) { 16658 rack->rc_ack_can_sendout_data = 0; 16659 if (rack->rc_inp->inp_in_hpts) 16660 tcp_hpts_remove(rack->rc_inp, HPTS_REMOVE_OUTPUT); 16661 } else if (rack->rc_inp->inp_in_hpts) { 16662 /* 16663 * On the hpts you can't pass even if ACKNOW is on, we will 16664 * when the hpts fires. 16665 */ 16666 #ifdef TCP_ACCOUNTING 16667 crtsc = get_cyclecount(); 16668 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16669 tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val); 16670 } 16671 counter_u64_add(tcp_proc_time[SND_BLOCKED], (crtsc - ts_val)); 16672 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 16673 tp->tcp_cnt_counters[SND_BLOCKED]++; 16674 } 16675 counter_u64_add(tcp_cnt_counters[SND_BLOCKED], 1); 16676 sched_unpin(); 16677 #endif 16678 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1); 16679 return (0); 16680 } 16681 rack->rc_inp->inp_hpts_calls = 0; 16682 /* Finish out both pacing early and late accounting */ 16683 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 16684 TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) { 16685 early = rack->r_ctl.rc_last_output_to - cts; 16686 } else 16687 early = 0; 16688 if (delayed) { 16689 rack->r_ctl.rc_agg_delayed += delayed; 16690 rack->r_late = 1; 16691 } else if (early) { 16692 rack->r_ctl.rc_agg_early += early; 16693 rack->r_early = 1; 16694 } 16695 /* Now that early/late accounting is done turn off the flag */ 16696 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 16697 rack->r_wanted_output = 0; 16698 rack->r_timer_override = 0; 16699 if ((tp->t_state != rack->r_state) && 16700 TCPS_HAVEESTABLISHED(tp->t_state)) { 16701 rack_set_state(tp, rack); 16702 } 16703 if ((rack->r_fast_output) && 16704 (doing_tlp == 0) && 16705 (tp->rcv_numsacks == 0)) { 16706 int ret; 16707 16708 error = 0; 16709 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error); 16710 if (ret >= 0) 16711 return(ret); 16712 else if (error) { 16713 inp = rack->rc_inp; 16714 so = inp->inp_socket; 16715 sb = &so->so_snd; 16716 goto nomore; 16717 } 16718 } 16719 inp = rack->rc_inp; 16720 /* 16721 * For TFO connections in SYN_SENT or SYN_RECEIVED, 16722 * only allow the initial SYN or SYN|ACK and those sent 16723 * by the retransmit timer. 16724 */ 16725 if (IS_FASTOPEN(tp->t_flags) && 16726 ((tp->t_state == TCPS_SYN_RECEIVED) || 16727 (tp->t_state == TCPS_SYN_SENT)) && 16728 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 16729 (tp->t_rxtshift == 0)) { /* not a retransmit */ 16730 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 16731 so = inp->inp_socket; 16732 sb = &so->so_snd; 16733 goto just_return_nolock; 16734 } 16735 /* 16736 * Determine length of data that should be transmitted, and flags 16737 * that will be used. If there is some data or critical controls 16738 * (SYN, RST) to send, then transmit; otherwise, investigate 16739 * further. 16740 */ 16741 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 16742 if (tp->t_idle_reduce) { 16743 if (idle && ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) 16744 rack_cc_after_idle(rack, tp); 16745 } 16746 tp->t_flags &= ~TF_LASTIDLE; 16747 if (idle) { 16748 if (tp->t_flags & TF_MORETOCOME) { 16749 tp->t_flags |= TF_LASTIDLE; 16750 idle = 0; 16751 } 16752 } 16753 if ((tp->snd_una == tp->snd_max) && 16754 rack->r_ctl.rc_went_idle_time && 16755 TSTMP_GT(cts, rack->r_ctl.rc_went_idle_time)) { 16756 idle = cts - rack->r_ctl.rc_went_idle_time; 16757 if (idle > rack_min_probertt_hold) { 16758 /* Count as a probe rtt */ 16759 if (rack->in_probe_rtt == 0) { 16760 rack->r_ctl.rc_lower_rtt_us_cts = cts; 16761 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts; 16762 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts; 16763 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts; 16764 } else { 16765 rack_exit_probertt(rack, cts); 16766 } 16767 } 16768 idle = 0; 16769 } 16770 if (rack_use_fsb && (rack->r_fsb_inited == 0) && (rack->r_state != TCPS_CLOSED)) 16771 rack_init_fsb_block(tp, rack); 16772 again: 16773 /* 16774 * If we've recently taken a timeout, snd_max will be greater than 16775 * snd_nxt. There may be SACK information that allows us to avoid 16776 * resending already delivered data. Adjust snd_nxt accordingly. 16777 */ 16778 sendalot = 0; 16779 cts = tcp_get_usecs(&tv); 16780 ms_cts = tcp_tv_to_mssectick(&tv); 16781 tso = 0; 16782 mtu = 0; 16783 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 16784 minseg = segsiz; 16785 if (rack->r_ctl.rc_pace_max_segs == 0) 16786 pace_max_seg = rack->rc_user_set_max_segs * segsiz; 16787 else 16788 pace_max_seg = rack->r_ctl.rc_pace_max_segs; 16789 sb_offset = tp->snd_max - tp->snd_una; 16790 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 16791 flags = tcp_outflags[tp->t_state]; 16792 while (rack->rc_free_cnt < rack_free_cache) { 16793 rsm = rack_alloc(rack); 16794 if (rsm == NULL) { 16795 if (inp->inp_hpts_calls) 16796 /* Retry in a ms */ 16797 slot = (1 * HPTS_USEC_IN_MSEC); 16798 so = inp->inp_socket; 16799 sb = &so->so_snd; 16800 goto just_return_nolock; 16801 } 16802 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext); 16803 rack->rc_free_cnt++; 16804 rsm = NULL; 16805 } 16806 if (inp->inp_hpts_calls) 16807 inp->inp_hpts_calls = 0; 16808 sack_rxmit = 0; 16809 len = 0; 16810 rsm = NULL; 16811 if (flags & TH_RST) { 16812 SOCKBUF_LOCK(&inp->inp_socket->so_snd); 16813 so = inp->inp_socket; 16814 sb = &so->so_snd; 16815 goto send; 16816 } 16817 if (rack->r_ctl.rc_resend) { 16818 /* Retransmit timer */ 16819 rsm = rack->r_ctl.rc_resend; 16820 rack->r_ctl.rc_resend = NULL; 16821 len = rsm->r_end - rsm->r_start; 16822 sack_rxmit = 1; 16823 sendalot = 0; 16824 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 16825 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 16826 __func__, __LINE__, 16827 rsm->r_start, tp->snd_una, tp, rack, rsm)); 16828 sb_offset = rsm->r_start - tp->snd_una; 16829 if (len >= segsiz) 16830 len = segsiz; 16831 } else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) { 16832 /* We have a retransmit that takes precedence */ 16833 if ((!IN_FASTRECOVERY(tp->t_flags)) && 16834 ((tp->t_flags & TF_WASFRECOVERY) == 0)) { 16835 /* Enter recovery if not induced by a time-out */ 16836 rack->r_ctl.rc_rsm_start = rsm->r_start; 16837 rack->r_ctl.rc_cwnd_at = tp->snd_cwnd; 16838 rack->r_ctl.rc_ssthresh_at = tp->snd_ssthresh; 16839 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una); 16840 } 16841 #ifdef INVARIANTS 16842 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 16843 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n", 16844 tp, rack, rsm, rsm->r_start, tp->snd_una); 16845 } 16846 #endif 16847 len = rsm->r_end - rsm->r_start; 16848 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 16849 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 16850 __func__, __LINE__, 16851 rsm->r_start, tp->snd_una, tp, rack, rsm)); 16852 sb_offset = rsm->r_start - tp->snd_una; 16853 sendalot = 0; 16854 if (len >= segsiz) 16855 len = segsiz; 16856 if (len > 0) { 16857 sack_rxmit = 1; 16858 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 16859 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 16860 min(len, segsiz)); 16861 counter_u64_add(rack_rtm_prr_retran, 1); 16862 } 16863 } else if (rack->r_ctl.rc_tlpsend) { 16864 /* Tail loss probe */ 16865 long cwin; 16866 long tlen; 16867 16868 /* 16869 * Check if we can do a TLP with a RACK'd packet 16870 * this can happen if we are not doing the rack 16871 * cheat and we skipped to a TLP and it 16872 * went off. 16873 */ 16874 rsm = rack->r_ctl.rc_tlpsend; 16875 /* We are doing a TLP make sure the flag is preent */ 16876 rsm->r_flags |= RACK_TLP; 16877 rack->r_ctl.rc_tlpsend = NULL; 16878 sack_rxmit = 1; 16879 tlen = rsm->r_end - rsm->r_start; 16880 if (tlen > segsiz) 16881 tlen = segsiz; 16882 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 16883 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 16884 __func__, __LINE__, 16885 rsm->r_start, tp->snd_una, tp, rack, rsm)); 16886 sb_offset = rsm->r_start - tp->snd_una; 16887 cwin = min(tp->snd_wnd, tlen); 16888 len = cwin; 16889 } 16890 if (rack->r_must_retran && 16891 (rsm == NULL)) { 16892 /* 16893 * Non-Sack and we had a RTO or MTU change, we 16894 * need to retransmit until we reach 16895 * the former snd_max (rack->r_ctl.rc_snd_max_at_rto). 16896 */ 16897 if (SEQ_GT(tp->snd_max, tp->snd_una)) { 16898 int sendwin, flight; 16899 16900 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 16901 flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto); 16902 if (flight >= sendwin) { 16903 so = inp->inp_socket; 16904 sb = &so->so_snd; 16905 goto just_return_nolock; 16906 } 16907 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 16908 KASSERT(rsm != NULL, ("rsm is NULL rack:%p r_must_retran set", rack)); 16909 if (rsm == NULL) { 16910 /* TSNH */ 16911 rack->r_must_retran = 0; 16912 rack->r_ctl.rc_out_at_rto = 0; 16913 rack->r_must_retran = 0; 16914 so = inp->inp_socket; 16915 sb = &so->so_snd; 16916 goto just_return_nolock; 16917 } 16918 sack_rxmit = 1; 16919 len = rsm->r_end - rsm->r_start; 16920 sendalot = 0; 16921 sb_offset = rsm->r_start - tp->snd_una; 16922 if (len >= segsiz) 16923 len = segsiz; 16924 } else { 16925 /* We must be done if there is nothing outstanding */ 16926 rack->r_must_retran = 0; 16927 rack->r_ctl.rc_out_at_rto = 0; 16928 } 16929 } 16930 /* 16931 * Enforce a connection sendmap count limit if set 16932 * as long as we are not retransmiting. 16933 */ 16934 if ((rsm == NULL) && 16935 (rack->do_detection == 0) && 16936 (V_tcp_map_entries_limit > 0) && 16937 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 16938 counter_u64_add(rack_to_alloc_limited, 1); 16939 if (!rack->alloc_limit_reported) { 16940 rack->alloc_limit_reported = 1; 16941 counter_u64_add(rack_alloc_limited_conns, 1); 16942 } 16943 so = inp->inp_socket; 16944 sb = &so->so_snd; 16945 goto just_return_nolock; 16946 } 16947 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) { 16948 /* we are retransmitting the fin */ 16949 len--; 16950 if (len) { 16951 /* 16952 * When retransmitting data do *not* include the 16953 * FIN. This could happen from a TLP probe. 16954 */ 16955 flags &= ~TH_FIN; 16956 } 16957 } 16958 #ifdef INVARIANTS 16959 /* For debugging */ 16960 rack->r_ctl.rc_rsm_at_retran = rsm; 16961 #endif 16962 if (rsm && rack->r_fsb_inited && rack_use_rsm_rfo && 16963 ((rsm->r_flags & RACK_HAS_FIN) == 0)) { 16964 int ret; 16965 16966 ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp); 16967 if (ret == 0) 16968 return (0); 16969 } 16970 so = inp->inp_socket; 16971 sb = &so->so_snd; 16972 if (do_a_prefetch == 0) { 16973 kern_prefetch(sb, &do_a_prefetch); 16974 do_a_prefetch = 1; 16975 } 16976 #ifdef NETFLIX_SHARED_CWND 16977 if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) && 16978 rack->rack_enable_scwnd) { 16979 /* We are doing cwnd sharing */ 16980 if (rack->gp_ready && 16981 (rack->rack_attempted_scwnd == 0) && 16982 (rack->r_ctl.rc_scw == NULL) && 16983 tp->t_lib) { 16984 /* The pcbid is in, lets make an attempt */ 16985 counter_u64_add(rack_try_scwnd, 1); 16986 rack->rack_attempted_scwnd = 1; 16987 rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp, 16988 &rack->r_ctl.rc_scw_index, 16989 segsiz); 16990 } 16991 if (rack->r_ctl.rc_scw && 16992 (rack->rack_scwnd_is_idle == 1) && 16993 sbavail(&so->so_snd)) { 16994 /* we are no longer out of data */ 16995 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 16996 rack->rack_scwnd_is_idle = 0; 16997 } 16998 if (rack->r_ctl.rc_scw) { 16999 /* First lets update and get the cwnd */ 17000 rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw, 17001 rack->r_ctl.rc_scw_index, 17002 tp->snd_cwnd, tp->snd_wnd, segsiz); 17003 } 17004 } 17005 #endif 17006 /* 17007 * Get standard flags, and add SYN or FIN if requested by 'hidden' 17008 * state flags. 17009 */ 17010 if (tp->t_flags & TF_NEEDFIN) 17011 flags |= TH_FIN; 17012 if (tp->t_flags & TF_NEEDSYN) 17013 flags |= TH_SYN; 17014 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 17015 void *end_rsm; 17016 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 17017 if (end_rsm) 17018 kern_prefetch(end_rsm, &prefetch_rsm); 17019 prefetch_rsm = 1; 17020 } 17021 SOCKBUF_LOCK(sb); 17022 /* 17023 * If snd_nxt == snd_max and we have transmitted a FIN, the 17024 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 17025 * negative length. This can also occur when TCP opens up its 17026 * congestion window while receiving additional duplicate acks after 17027 * fast-retransmit because TCP will reset snd_nxt to snd_max after 17028 * the fast-retransmit. 17029 * 17030 * In the normal retransmit-FIN-only case, however, snd_nxt will be 17031 * set to snd_una, the sb_offset will be 0, and the length may wind 17032 * up 0. 17033 * 17034 * If sack_rxmit is true we are retransmitting from the scoreboard 17035 * in which case len is already set. 17036 */ 17037 if ((sack_rxmit == 0) && 17038 (TCPS_HAVEESTABLISHED(tp->t_state) || IS_FASTOPEN(tp->t_flags))) { 17039 uint32_t avail; 17040 17041 avail = sbavail(sb); 17042 if (SEQ_GT(tp->snd_nxt, tp->snd_una) && avail) 17043 sb_offset = tp->snd_nxt - tp->snd_una; 17044 else 17045 sb_offset = 0; 17046 if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) { 17047 if (rack->r_ctl.rc_tlp_new_data) { 17048 /* TLP is forcing out new data */ 17049 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) { 17050 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset); 17051 } 17052 if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) { 17053 if (tp->snd_wnd > sb_offset) 17054 len = tp->snd_wnd - sb_offset; 17055 else 17056 len = 0; 17057 } else { 17058 len = rack->r_ctl.rc_tlp_new_data; 17059 } 17060 } else { 17061 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset); 17062 } 17063 if ((rack->r_ctl.crte == NULL) && IN_FASTRECOVERY(tp->t_flags) && (len > segsiz)) { 17064 /* 17065 * For prr=off, we need to send only 1 MSS 17066 * at a time. We do this because another sack could 17067 * be arriving that causes us to send retransmits and 17068 * we don't want to be on a long pace due to a larger send 17069 * that keeps us from sending out the retransmit. 17070 */ 17071 len = segsiz; 17072 } 17073 } else { 17074 uint32_t outstanding; 17075 /* 17076 * We are inside of a Fast recovery episode, this 17077 * is caused by a SACK or 3 dup acks. At this point 17078 * we have sent all the retransmissions and we rely 17079 * on PRR to dictate what we will send in the form of 17080 * new data. 17081 */ 17082 17083 outstanding = tp->snd_max - tp->snd_una; 17084 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) { 17085 if (tp->snd_wnd > outstanding) { 17086 len = tp->snd_wnd - outstanding; 17087 /* Check to see if we have the data */ 17088 if ((sb_offset + len) > avail) { 17089 /* It does not all fit */ 17090 if (avail > sb_offset) 17091 len = avail - sb_offset; 17092 else 17093 len = 0; 17094 } 17095 } else { 17096 len = 0; 17097 } 17098 } else if (avail > sb_offset) { 17099 len = avail - sb_offset; 17100 } else { 17101 len = 0; 17102 } 17103 if (len > 0) { 17104 if (len > rack->r_ctl.rc_prr_sndcnt) { 17105 len = rack->r_ctl.rc_prr_sndcnt; 17106 } 17107 if (len > 0) { 17108 sub_from_prr = 1; 17109 counter_u64_add(rack_rtm_prr_newdata, 1); 17110 } 17111 } 17112 if (len > segsiz) { 17113 /* 17114 * We should never send more than a MSS when 17115 * retransmitting or sending new data in prr 17116 * mode unless the override flag is on. Most 17117 * likely the PRR algorithm is not going to 17118 * let us send a lot as well :-) 17119 */ 17120 if (rack->r_ctl.rc_prr_sendalot == 0) { 17121 len = segsiz; 17122 } 17123 } else if (len < segsiz) { 17124 /* 17125 * Do we send any? The idea here is if the 17126 * send empty's the socket buffer we want to 17127 * do it. However if not then lets just wait 17128 * for our prr_sndcnt to get bigger. 17129 */ 17130 long leftinsb; 17131 17132 leftinsb = sbavail(sb) - sb_offset; 17133 if (leftinsb > len) { 17134 /* This send does not empty the sb */ 17135 len = 0; 17136 } 17137 } 17138 } 17139 } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) { 17140 /* 17141 * If you have not established 17142 * and are not doing FAST OPEN 17143 * no data please. 17144 */ 17145 if ((sack_rxmit == 0) && 17146 (!IS_FASTOPEN(tp->t_flags))){ 17147 len = 0; 17148 sb_offset = 0; 17149 } 17150 } 17151 if (prefetch_so_done == 0) { 17152 kern_prefetch(so, &prefetch_so_done); 17153 prefetch_so_done = 1; 17154 } 17155 /* 17156 * Lop off SYN bit if it has already been sent. However, if this is 17157 * SYN-SENT state and if segment contains data and if we don't know 17158 * that foreign host supports TAO, suppress sending segment. 17159 */ 17160 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una) && 17161 ((sack_rxmit == 0) && (tp->t_rxtshift == 0))) { 17162 /* 17163 * When sending additional segments following a TFO SYN|ACK, 17164 * do not include the SYN bit. 17165 */ 17166 if (IS_FASTOPEN(tp->t_flags) && 17167 (tp->t_state == TCPS_SYN_RECEIVED)) 17168 flags &= ~TH_SYN; 17169 } 17170 /* 17171 * Be careful not to send data and/or FIN on SYN segments. This 17172 * measure is needed to prevent interoperability problems with not 17173 * fully conformant TCP implementations. 17174 */ 17175 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 17176 len = 0; 17177 flags &= ~TH_FIN; 17178 } 17179 /* 17180 * On TFO sockets, ensure no data is sent in the following cases: 17181 * 17182 * - When retransmitting SYN|ACK on a passively-created socket 17183 * 17184 * - When retransmitting SYN on an actively created socket 17185 * 17186 * - When sending a zero-length cookie (cookie request) on an 17187 * actively created socket 17188 * 17189 * - When the socket is in the CLOSED state (RST is being sent) 17190 */ 17191 if (IS_FASTOPEN(tp->t_flags) && 17192 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 17193 ((tp->t_state == TCPS_SYN_SENT) && 17194 (tp->t_tfo_client_cookie_len == 0)) || 17195 (flags & TH_RST))) { 17196 sack_rxmit = 0; 17197 len = 0; 17198 } 17199 /* Without fast-open there should never be data sent on a SYN */ 17200 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) { 17201 tp->snd_nxt = tp->iss; 17202 len = 0; 17203 } 17204 if ((len > segsiz) && (tcp_dsack_block_exists(tp))) { 17205 /* We only send 1 MSS if we have a DSACK block */ 17206 add_flag |= RACK_SENT_W_DSACK; 17207 len = segsiz; 17208 } 17209 orig_len = len; 17210 if (len <= 0) { 17211 /* 17212 * If FIN has been sent but not acked, but we haven't been 17213 * called to retransmit, len will be < 0. Otherwise, window 17214 * shrank after we sent into it. If window shrank to 0, 17215 * cancel pending retransmit, pull snd_nxt back to (closed) 17216 * window, and set the persist timer if it isn't already 17217 * going. If the window didn't close completely, just wait 17218 * for an ACK. 17219 * 17220 * We also do a general check here to ensure that we will 17221 * set the persist timer when we have data to send, but a 17222 * 0-byte window. This makes sure the persist timer is set 17223 * even if the packet hits one of the "goto send" lines 17224 * below. 17225 */ 17226 len = 0; 17227 if ((tp->snd_wnd == 0) && 17228 (TCPS_HAVEESTABLISHED(tp->t_state)) && 17229 (tp->snd_una == tp->snd_max) && 17230 (sb_offset < (int)sbavail(sb))) { 17231 rack_enter_persist(tp, rack, cts); 17232 } 17233 } else if ((rsm == NULL) && 17234 (doing_tlp == 0) && 17235 (len < pace_max_seg)) { 17236 /* 17237 * We are not sending a maximum sized segment for 17238 * some reason. Should we not send anything (think 17239 * sws or persists)? 17240 */ 17241 if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) && 17242 (TCPS_HAVEESTABLISHED(tp->t_state)) && 17243 (len < minseg) && 17244 (len < (int)(sbavail(sb) - sb_offset))) { 17245 /* 17246 * Here the rwnd is less than 17247 * the minimum pacing size, this is not a retransmit, 17248 * we are established and 17249 * the send is not the last in the socket buffer 17250 * we send nothing, and we may enter persists 17251 * if nothing is outstanding. 17252 */ 17253 len = 0; 17254 if (tp->snd_max == tp->snd_una) { 17255 /* 17256 * Nothing out we can 17257 * go into persists. 17258 */ 17259 rack_enter_persist(tp, rack, cts); 17260 } 17261 } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) && 17262 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) && 17263 (len < (int)(sbavail(sb) - sb_offset)) && 17264 (len < minseg)) { 17265 /* 17266 * Here we are not retransmitting, and 17267 * the cwnd is not so small that we could 17268 * not send at least a min size (rxt timer 17269 * not having gone off), We have 2 segments or 17270 * more already in flight, its not the tail end 17271 * of the socket buffer and the cwnd is blocking 17272 * us from sending out a minimum pacing segment size. 17273 * Lets not send anything. 17274 */ 17275 len = 0; 17276 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 17277 min((rack->r_ctl.rc_high_rwnd/2), minseg)) && 17278 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) && 17279 (len < (int)(sbavail(sb) - sb_offset)) && 17280 (TCPS_HAVEESTABLISHED(tp->t_state))) { 17281 /* 17282 * Here we have a send window but we have 17283 * filled it up and we can't send another pacing segment. 17284 * We also have in flight more than 2 segments 17285 * and we are not completing the sb i.e. we allow 17286 * the last bytes of the sb to go out even if 17287 * its not a full pacing segment. 17288 */ 17289 len = 0; 17290 } else if ((rack->r_ctl.crte != NULL) && 17291 (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) && 17292 (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) && 17293 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) && 17294 (len < (int)(sbavail(sb) - sb_offset))) { 17295 /* 17296 * Here we are doing hardware pacing, this is not a TLP, 17297 * we are not sending a pace max segment size, there is rwnd 17298 * room to send at least N pace_max_seg, the cwnd is greater 17299 * than or equal to a full pacing segments plus 4 mss and we have 2 or 17300 * more segments in flight and its not the tail of the socket buffer. 17301 * 17302 * We don't want to send instead we need to get more ack's in to 17303 * allow us to send a full pacing segment. Normally, if we are pacing 17304 * about the right speed, we should have finished our pacing 17305 * send as most of the acks have come back if we are at the 17306 * right rate. This is a bit fuzzy since return path delay 17307 * can delay the acks, which is why we want to make sure we 17308 * have cwnd space to have a bit more than a max pace segments in flight. 17309 * 17310 * If we have not gotten our acks back we are pacing at too high a 17311 * rate delaying will not hurt and will bring our GP estimate down by 17312 * injecting the delay. If we don't do this we will send 17313 * 2 MSS out in response to the acks being clocked in which 17314 * defeats the point of hw-pacing (i.e. to help us get 17315 * larger TSO's out). 17316 */ 17317 len = 0; 17318 17319 } 17320 17321 } 17322 /* len will be >= 0 after this point. */ 17323 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 17324 rack_sndbuf_autoscale(rack); 17325 /* 17326 * Decide if we can use TCP Segmentation Offloading (if supported by 17327 * hardware). 17328 * 17329 * TSO may only be used if we are in a pure bulk sending state. The 17330 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 17331 * options prevent using TSO. With TSO the TCP header is the same 17332 * (except for the sequence number) for all generated packets. This 17333 * makes it impossible to transmit any options which vary per 17334 * generated segment or packet. 17335 * 17336 * IPv4 handling has a clear separation of ip options and ip header 17337 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 17338 * the right thing below to provide length of just ip options and thus 17339 * checking for ipoptlen is enough to decide if ip options are present. 17340 */ 17341 ipoptlen = 0; 17342 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 17343 /* 17344 * Pre-calculate here as we save another lookup into the darknesses 17345 * of IPsec that way and can actually decide if TSO is ok. 17346 */ 17347 #ifdef INET6 17348 if (isipv6 && IPSEC_ENABLED(ipv6)) 17349 ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 17350 #ifdef INET 17351 else 17352 #endif 17353 #endif /* INET6 */ 17354 #ifdef INET 17355 if (IPSEC_ENABLED(ipv4)) 17356 ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 17357 #endif /* INET */ 17358 #endif 17359 17360 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 17361 ipoptlen += ipsec_optlen; 17362 #endif 17363 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz && 17364 (tp->t_port == 0) && 17365 ((tp->t_flags & TF_SIGNATURE) == 0) && 17366 tp->rcv_numsacks == 0 && sack_rxmit == 0 && 17367 ipoptlen == 0) 17368 tso = 1; 17369 { 17370 uint32_t outstanding; 17371 17372 outstanding = tp->snd_max - tp->snd_una; 17373 if (tp->t_flags & TF_SENTFIN) { 17374 /* 17375 * If we sent a fin, snd_max is 1 higher than 17376 * snd_una 17377 */ 17378 outstanding--; 17379 } 17380 if (sack_rxmit) { 17381 if ((rsm->r_flags & RACK_HAS_FIN) == 0) 17382 flags &= ~TH_FIN; 17383 } else { 17384 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 17385 sbused(sb))) 17386 flags &= ~TH_FIN; 17387 } 17388 } 17389 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 17390 (long)TCP_MAXWIN << tp->rcv_scale); 17391 17392 /* 17393 * Sender silly window avoidance. We transmit under the following 17394 * conditions when len is non-zero: 17395 * 17396 * - We have a full segment (or more with TSO) - This is the last 17397 * buffer in a write()/send() and we are either idle or running 17398 * NODELAY - we've timed out (e.g. persist timer) - we have more 17399 * then 1/2 the maximum send window's worth of data (receiver may be 17400 * limited the window size) - we need to retransmit 17401 */ 17402 if (len) { 17403 if (len >= segsiz) { 17404 goto send; 17405 } 17406 /* 17407 * NOTE! on localhost connections an 'ack' from the remote 17408 * end may occur synchronously with the output and cause us 17409 * to flush a buffer queued with moretocome. XXX 17410 * 17411 */ 17412 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 17413 (idle || (tp->t_flags & TF_NODELAY)) && 17414 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) && 17415 (tp->t_flags & TF_NOPUSH) == 0) { 17416 pass = 2; 17417 goto send; 17418 } 17419 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 17420 pass = 22; 17421 goto send; 17422 } 17423 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 17424 pass = 4; 17425 goto send; 17426 } 17427 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { /* retransmit case */ 17428 pass = 5; 17429 goto send; 17430 } 17431 if (sack_rxmit) { 17432 pass = 6; 17433 goto send; 17434 } 17435 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) && 17436 (ctf_outstanding(tp) < (segsiz * 2))) { 17437 /* 17438 * We have less than two MSS outstanding (delayed ack) 17439 * and our rwnd will not let us send a full sized 17440 * MSS. Lets go ahead and let this small segment 17441 * out because we want to try to have at least two 17442 * packets inflight to not be caught by delayed ack. 17443 */ 17444 pass = 12; 17445 goto send; 17446 } 17447 } 17448 /* 17449 * Sending of standalone window updates. 17450 * 17451 * Window updates are important when we close our window due to a 17452 * full socket buffer and are opening it again after the application 17453 * reads data from it. Once the window has opened again and the 17454 * remote end starts to send again the ACK clock takes over and 17455 * provides the most current window information. 17456 * 17457 * We must avoid the silly window syndrome whereas every read from 17458 * the receive buffer, no matter how small, causes a window update 17459 * to be sent. We also should avoid sending a flurry of window 17460 * updates when the socket buffer had queued a lot of data and the 17461 * application is doing small reads. 17462 * 17463 * Prevent a flurry of pointless window updates by only sending an 17464 * update when we can increase the advertized window by more than 17465 * 1/4th of the socket buffer capacity. When the buffer is getting 17466 * full or is very small be more aggressive and send an update 17467 * whenever we can increase by two mss sized segments. In all other 17468 * situations the ACK's to new incoming data will carry further 17469 * window increases. 17470 * 17471 * Don't send an independent window update if a delayed ACK is 17472 * pending (it will get piggy-backed on it) or the remote side 17473 * already has done a half-close and won't send more data. Skip 17474 * this if the connection is in T/TCP half-open state. 17475 */ 17476 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 17477 !(tp->t_flags & TF_DELACK) && 17478 !TCPS_HAVERCVDFIN(tp->t_state)) { 17479 /* 17480 * "adv" is the amount we could increase the window, taking 17481 * into account that we are limited by TCP_MAXWIN << 17482 * tp->rcv_scale. 17483 */ 17484 int32_t adv; 17485 int oldwin; 17486 17487 adv = recwin; 17488 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 17489 oldwin = (tp->rcv_adv - tp->rcv_nxt); 17490 if (adv > oldwin) 17491 adv -= oldwin; 17492 else { 17493 /* We can't increase the window */ 17494 adv = 0; 17495 } 17496 } else 17497 oldwin = 0; 17498 17499 /* 17500 * If the new window size ends up being the same as or less 17501 * than the old size when it is scaled, then don't force 17502 * a window update. 17503 */ 17504 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 17505 goto dontupdate; 17506 17507 if (adv >= (int32_t)(2 * segsiz) && 17508 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 17509 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) || 17510 so->so_rcv.sb_hiwat <= 8 * segsiz)) { 17511 pass = 7; 17512 goto send; 17513 } 17514 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) { 17515 pass = 23; 17516 goto send; 17517 } 17518 } 17519 dontupdate: 17520 17521 /* 17522 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 17523 * is also a catch-all for the retransmit timer timeout case. 17524 */ 17525 if (tp->t_flags & TF_ACKNOW) { 17526 pass = 8; 17527 goto send; 17528 } 17529 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) { 17530 pass = 9; 17531 goto send; 17532 } 17533 /* 17534 * If our state indicates that FIN should be sent and we have not 17535 * yet done so, then we need to send. 17536 */ 17537 if ((flags & TH_FIN) && 17538 (tp->snd_nxt == tp->snd_una)) { 17539 pass = 11; 17540 goto send; 17541 } 17542 /* 17543 * No reason to send a segment, just return. 17544 */ 17545 just_return: 17546 SOCKBUF_UNLOCK(sb); 17547 just_return_nolock: 17548 { 17549 int app_limited = CTF_JR_SENT_DATA; 17550 17551 if (tot_len_this_send > 0) { 17552 /* Make sure snd_nxt is up to max */ 17553 rack->r_ctl.fsb.recwin = recwin; 17554 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz); 17555 if ((error == 0) && 17556 rack_use_rfo && 17557 ((flags & (TH_SYN|TH_FIN)) == 0) && 17558 (ipoptlen == 0) && 17559 (tp->snd_nxt == tp->snd_max) && 17560 (tp->rcv_numsacks == 0) && 17561 rack->r_fsb_inited && 17562 TCPS_HAVEESTABLISHED(tp->t_state) && 17563 (rack->r_must_retran == 0) && 17564 ((tp->t_flags & TF_NEEDFIN) == 0) && 17565 (len > 0) && (orig_len > 0) && 17566 (orig_len > len) && 17567 ((orig_len - len) >= segsiz) && 17568 ((optlen == 0) || 17569 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 17570 /* We can send at least one more MSS using our fsb */ 17571 17572 rack->r_fast_output = 1; 17573 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 17574 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 17575 rack->r_ctl.fsb.tcp_flags = flags; 17576 rack->r_ctl.fsb.left_to_send = orig_len - len; 17577 if (hw_tls) 17578 rack->r_ctl.fsb.hw_tls = 1; 17579 else 17580 rack->r_ctl.fsb.hw_tls = 0; 17581 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))), 17582 ("rack:%p left_to_send:%u sbavail:%u out:%u", 17583 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb), 17584 (tp->snd_max - tp->snd_una))); 17585 if (rack->r_ctl.fsb.left_to_send < segsiz) 17586 rack->r_fast_output = 0; 17587 else { 17588 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una))) 17589 rack->r_ctl.fsb.rfo_apply_push = 1; 17590 else 17591 rack->r_ctl.fsb.rfo_apply_push = 0; 17592 } 17593 } else 17594 rack->r_fast_output = 0; 17595 17596 17597 rack_log_fsb(rack, tp, so, flags, 17598 ipoptlen, orig_len, len, 0, 17599 1, optlen, __LINE__, 1); 17600 if (SEQ_GT(tp->snd_max, tp->snd_nxt)) 17601 tp->snd_nxt = tp->snd_max; 17602 } else { 17603 int end_window = 0; 17604 uint32_t seq = tp->gput_ack; 17605 17606 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 17607 if (rsm) { 17608 /* 17609 * Mark the last sent that we just-returned (hinting 17610 * that delayed ack may play a role in any rtt measurement). 17611 */ 17612 rsm->r_just_ret = 1; 17613 } 17614 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1); 17615 rack->r_ctl.rc_agg_delayed = 0; 17616 rack->r_early = 0; 17617 rack->r_late = 0; 17618 rack->r_ctl.rc_agg_early = 0; 17619 if ((ctf_outstanding(tp) + 17620 min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)), 17621 minseg)) >= tp->snd_wnd) { 17622 /* We are limited by the rwnd */ 17623 app_limited = CTF_JR_RWND_LIMITED; 17624 if (IN_FASTRECOVERY(tp->t_flags)) 17625 rack->r_ctl.rc_prr_sndcnt = 0; 17626 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 17627 /* We are limited by whats available -- app limited */ 17628 app_limited = CTF_JR_APP_LIMITED; 17629 if (IN_FASTRECOVERY(tp->t_flags)) 17630 rack->r_ctl.rc_prr_sndcnt = 0; 17631 } else if ((idle == 0) && 17632 ((tp->t_flags & TF_NODELAY) == 0) && 17633 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) && 17634 (len < segsiz)) { 17635 /* 17636 * No delay is not on and the 17637 * user is sending less than 1MSS. This 17638 * brings out SWS avoidance so we 17639 * don't send. Another app-limited case. 17640 */ 17641 app_limited = CTF_JR_APP_LIMITED; 17642 } else if (tp->t_flags & TF_NOPUSH) { 17643 /* 17644 * The user has requested no push of 17645 * the last segment and we are 17646 * at the last segment. Another app 17647 * limited case. 17648 */ 17649 app_limited = CTF_JR_APP_LIMITED; 17650 } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) { 17651 /* Its the cwnd */ 17652 app_limited = CTF_JR_CWND_LIMITED; 17653 } else if (IN_FASTRECOVERY(tp->t_flags) && 17654 (rack->rack_no_prr == 0) && 17655 (rack->r_ctl.rc_prr_sndcnt < segsiz)) { 17656 app_limited = CTF_JR_PRR; 17657 } else { 17658 /* Now why here are we not sending? */ 17659 #ifdef NOW 17660 #ifdef INVARIANTS 17661 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use); 17662 #endif 17663 #endif 17664 app_limited = CTF_JR_ASSESSING; 17665 } 17666 /* 17667 * App limited in some fashion, for our pacing GP 17668 * measurements we don't want any gap (even cwnd). 17669 * Close down the measurement window. 17670 */ 17671 if (rack_cwnd_block_ends_measure && 17672 ((app_limited == CTF_JR_CWND_LIMITED) || 17673 (app_limited == CTF_JR_PRR))) { 17674 /* 17675 * The reason we are not sending is 17676 * the cwnd (or prr). We have been configured 17677 * to end the measurement window in 17678 * this case. 17679 */ 17680 end_window = 1; 17681 } else if (rack_rwnd_block_ends_measure && 17682 (app_limited == CTF_JR_RWND_LIMITED)) { 17683 /* 17684 * We are rwnd limited and have been 17685 * configured to end the measurement 17686 * window in this case. 17687 */ 17688 end_window = 1; 17689 } else if (app_limited == CTF_JR_APP_LIMITED) { 17690 /* 17691 * A true application limited period, we have 17692 * ran out of data. 17693 */ 17694 end_window = 1; 17695 } else if (app_limited == CTF_JR_ASSESSING) { 17696 /* 17697 * In the assessing case we hit the end of 17698 * the if/else and had no known reason 17699 * This will panic us under invariants.. 17700 * 17701 * If we get this out in logs we need to 17702 * investagate which reason we missed. 17703 */ 17704 end_window = 1; 17705 } 17706 if (end_window) { 17707 uint8_t log = 0; 17708 17709 /* Adjust the Gput measurement */ 17710 if ((tp->t_flags & TF_GPUTINPROG) && 17711 SEQ_GT(tp->gput_ack, tp->snd_max)) { 17712 tp->gput_ack = tp->snd_max; 17713 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) { 17714 /* 17715 * There is not enough to measure. 17716 */ 17717 tp->t_flags &= ~TF_GPUTINPROG; 17718 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 17719 rack->r_ctl.rc_gp_srtt /*flex1*/, 17720 tp->gput_seq, 17721 0, 0, 18, __LINE__, NULL, 0); 17722 } else 17723 log = 1; 17724 } 17725 /* Mark the last packet has app limited */ 17726 rsm = RB_MAX(rack_rb_tree_head, &rack->r_ctl.rc_mtree); 17727 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) { 17728 if (rack->r_ctl.rc_app_limited_cnt == 0) 17729 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm; 17730 else { 17731 /* 17732 * Go out to the end app limited and mark 17733 * this new one as next and move the end_appl up 17734 * to this guy. 17735 */ 17736 if (rack->r_ctl.rc_end_appl) 17737 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start; 17738 rack->r_ctl.rc_end_appl = rsm; 17739 } 17740 rsm->r_flags |= RACK_APP_LIMITED; 17741 rack->r_ctl.rc_app_limited_cnt++; 17742 } 17743 if (log) 17744 rack_log_pacing_delay_calc(rack, 17745 rack->r_ctl.rc_app_limited_cnt, seq, 17746 tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0); 17747 } 17748 } 17749 if (slot) { 17750 /* set the rack tcb into the slot N */ 17751 counter_u64_add(rack_paced_segments, 1); 17752 } else if (tot_len_this_send) { 17753 counter_u64_add(rack_unpaced_segments, 1); 17754 } 17755 /* Check if we need to go into persists or not */ 17756 if ((tp->snd_max == tp->snd_una) && 17757 TCPS_HAVEESTABLISHED(tp->t_state) && 17758 sbavail(sb) && 17759 (sbavail(sb) > tp->snd_wnd) && 17760 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) { 17761 /* Yes lets make sure to move to persist before timer-start */ 17762 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime); 17763 } 17764 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack); 17765 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use); 17766 } 17767 #ifdef NETFLIX_SHARED_CWND 17768 if ((sbavail(sb) == 0) && 17769 rack->r_ctl.rc_scw) { 17770 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 17771 rack->rack_scwnd_is_idle = 1; 17772 } 17773 #endif 17774 #ifdef TCP_ACCOUNTING 17775 if (tot_len_this_send > 0) { 17776 crtsc = get_cyclecount(); 17777 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17778 tp->tcp_cnt_counters[SND_OUT_DATA]++; 17779 } 17780 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1); 17781 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17782 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 17783 } 17784 counter_u64_add(tcp_proc_time[SND_OUT_DATA], (crtsc - ts_val)); 17785 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17786 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz); 17787 } 17788 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) / segsiz)); 17789 } else { 17790 crtsc = get_cyclecount(); 17791 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17792 tp->tcp_cnt_counters[SND_LIMITED]++; 17793 } 17794 counter_u64_add(tcp_cnt_counters[SND_LIMITED], 1); 17795 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17796 tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val); 17797 } 17798 counter_u64_add(tcp_proc_time[SND_LIMITED], (crtsc - ts_val)); 17799 } 17800 sched_unpin(); 17801 #endif 17802 return (0); 17803 17804 send: 17805 if (rsm || sack_rxmit) 17806 counter_u64_add(rack_nfto_resend, 1); 17807 else 17808 counter_u64_add(rack_non_fto_send, 1); 17809 if ((flags & TH_FIN) && 17810 sbavail(sb)) { 17811 /* 17812 * We do not transmit a FIN 17813 * with data outstanding. We 17814 * need to make it so all data 17815 * is acked first. 17816 */ 17817 flags &= ~TH_FIN; 17818 } 17819 /* Enforce stack imposed max seg size if we have one */ 17820 if (rack->r_ctl.rc_pace_max_segs && 17821 (len > rack->r_ctl.rc_pace_max_segs)) { 17822 mark = 1; 17823 len = rack->r_ctl.rc_pace_max_segs; 17824 } 17825 SOCKBUF_LOCK_ASSERT(sb); 17826 if (len > 0) { 17827 if (len >= segsiz) 17828 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 17829 else 17830 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 17831 } 17832 /* 17833 * Before ESTABLISHED, force sending of initial options unless TCP 17834 * set not to do any options. NOTE: we assume that the IP/TCP header 17835 * plus TCP options always fit in a single mbuf, leaving room for a 17836 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 17837 * + optlen <= MCLBYTES 17838 */ 17839 optlen = 0; 17840 #ifdef INET6 17841 if (isipv6) 17842 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 17843 else 17844 #endif 17845 hdrlen = sizeof(struct tcpiphdr); 17846 17847 /* 17848 * Compute options for segment. We only have to care about SYN and 17849 * established connection segments. Options for SYN-ACK segments 17850 * are handled in TCP syncache. 17851 */ 17852 to.to_flags = 0; 17853 if ((tp->t_flags & TF_NOOPT) == 0) { 17854 /* Maximum segment size. */ 17855 if (flags & TH_SYN) { 17856 tp->snd_nxt = tp->iss; 17857 to.to_mss = tcp_mssopt(&inp->inp_inc); 17858 if (tp->t_port) 17859 to.to_mss -= V_tcp_udp_tunneling_overhead; 17860 to.to_flags |= TOF_MSS; 17861 17862 /* 17863 * On SYN or SYN|ACK transmits on TFO connections, 17864 * only include the TFO option if it is not a 17865 * retransmit, as the presence of the TFO option may 17866 * have caused the original SYN or SYN|ACK to have 17867 * been dropped by a middlebox. 17868 */ 17869 if (IS_FASTOPEN(tp->t_flags) && 17870 (tp->t_rxtshift == 0)) { 17871 if (tp->t_state == TCPS_SYN_RECEIVED) { 17872 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 17873 to.to_tfo_cookie = 17874 (u_int8_t *)&tp->t_tfo_cookie.server; 17875 to.to_flags |= TOF_FASTOPEN; 17876 wanted_cookie = 1; 17877 } else if (tp->t_state == TCPS_SYN_SENT) { 17878 to.to_tfo_len = 17879 tp->t_tfo_client_cookie_len; 17880 to.to_tfo_cookie = 17881 tp->t_tfo_cookie.client; 17882 to.to_flags |= TOF_FASTOPEN; 17883 wanted_cookie = 1; 17884 /* 17885 * If we wind up having more data to 17886 * send with the SYN than can fit in 17887 * one segment, don't send any more 17888 * until the SYN|ACK comes back from 17889 * the other end. 17890 */ 17891 sendalot = 0; 17892 } 17893 } 17894 } 17895 /* Window scaling. */ 17896 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 17897 to.to_wscale = tp->request_r_scale; 17898 to.to_flags |= TOF_SCALE; 17899 } 17900 /* Timestamps. */ 17901 if ((tp->t_flags & TF_RCVD_TSTMP) || 17902 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 17903 to.to_tsval = ms_cts + tp->ts_offset; 17904 to.to_tsecr = tp->ts_recent; 17905 to.to_flags |= TOF_TS; 17906 } 17907 /* Set receive buffer autosizing timestamp. */ 17908 if (tp->rfbuf_ts == 0 && 17909 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 17910 tp->rfbuf_ts = tcp_ts_getticks(); 17911 /* Selective ACK's. */ 17912 if (tp->t_flags & TF_SACK_PERMIT) { 17913 if (flags & TH_SYN) 17914 to.to_flags |= TOF_SACKPERM; 17915 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 17916 tp->rcv_numsacks > 0) { 17917 to.to_flags |= TOF_SACK; 17918 to.to_nsacks = tp->rcv_numsacks; 17919 to.to_sacks = (u_char *)tp->sackblks; 17920 } 17921 } 17922 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 17923 /* TCP-MD5 (RFC2385). */ 17924 if (tp->t_flags & TF_SIGNATURE) 17925 to.to_flags |= TOF_SIGNATURE; 17926 #endif /* TCP_SIGNATURE */ 17927 17928 /* Processing the options. */ 17929 hdrlen += optlen = tcp_addoptions(&to, opt); 17930 /* 17931 * If we wanted a TFO option to be added, but it was unable 17932 * to fit, ensure no data is sent. 17933 */ 17934 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 17935 !(to.to_flags & TOF_FASTOPEN)) 17936 len = 0; 17937 } 17938 if (tp->t_port) { 17939 if (V_tcp_udp_tunneling_port == 0) { 17940 /* The port was removed?? */ 17941 SOCKBUF_UNLOCK(&so->so_snd); 17942 #ifdef TCP_ACCOUNTING 17943 crtsc = get_cyclecount(); 17944 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17945 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 17946 } 17947 counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1); 17948 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17949 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 17950 } 17951 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val)); 17952 sched_unpin(); 17953 #endif 17954 return (EHOSTUNREACH); 17955 } 17956 hdrlen += sizeof(struct udphdr); 17957 } 17958 #ifdef INET6 17959 if (isipv6) 17960 ipoptlen = ip6_optlen(tp->t_inpcb); 17961 else 17962 #endif 17963 if (tp->t_inpcb->inp_options) 17964 ipoptlen = tp->t_inpcb->inp_options->m_len - 17965 offsetof(struct ipoption, ipopt_list); 17966 else 17967 ipoptlen = 0; 17968 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 17969 ipoptlen += ipsec_optlen; 17970 #endif 17971 17972 /* 17973 * Adjust data length if insertion of options will bump the packet 17974 * length beyond the t_maxseg length. Clear the FIN bit because we 17975 * cut off the tail of the segment. 17976 */ 17977 if (len + optlen + ipoptlen > tp->t_maxseg) { 17978 if (tso) { 17979 uint32_t if_hw_tsomax; 17980 uint32_t moff; 17981 int32_t max_len; 17982 17983 /* extract TSO information */ 17984 if_hw_tsomax = tp->t_tsomax; 17985 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 17986 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 17987 KASSERT(ipoptlen == 0, 17988 ("%s: TSO can't do IP options", __func__)); 17989 17990 /* 17991 * Check if we should limit by maximum payload 17992 * length: 17993 */ 17994 if (if_hw_tsomax != 0) { 17995 /* compute maximum TSO length */ 17996 max_len = (if_hw_tsomax - hdrlen - 17997 max_linkhdr); 17998 if (max_len <= 0) { 17999 len = 0; 18000 } else if (len > max_len) { 18001 sendalot = 1; 18002 len = max_len; 18003 mark = 2; 18004 } 18005 } 18006 /* 18007 * Prevent the last segment from being fractional 18008 * unless the send sockbuf can be emptied: 18009 */ 18010 max_len = (tp->t_maxseg - optlen); 18011 if ((sb_offset + len) < sbavail(sb)) { 18012 moff = len % (u_int)max_len; 18013 if (moff != 0) { 18014 mark = 3; 18015 len -= moff; 18016 } 18017 } 18018 /* 18019 * In case there are too many small fragments don't 18020 * use TSO: 18021 */ 18022 if (len <= segsiz) { 18023 mark = 4; 18024 tso = 0; 18025 } 18026 /* 18027 * Send the FIN in a separate segment after the bulk 18028 * sending is done. We don't trust the TSO 18029 * implementations to clear the FIN flag on all but 18030 * the last segment. 18031 */ 18032 if (tp->t_flags & TF_NEEDFIN) { 18033 sendalot = 4; 18034 } 18035 } else { 18036 mark = 5; 18037 if (optlen + ipoptlen >= tp->t_maxseg) { 18038 /* 18039 * Since we don't have enough space to put 18040 * the IP header chain and the TCP header in 18041 * one packet as required by RFC 7112, don't 18042 * send it. Also ensure that at least one 18043 * byte of the payload can be put into the 18044 * TCP segment. 18045 */ 18046 SOCKBUF_UNLOCK(&so->so_snd); 18047 error = EMSGSIZE; 18048 sack_rxmit = 0; 18049 goto out; 18050 } 18051 len = tp->t_maxseg - optlen - ipoptlen; 18052 sendalot = 5; 18053 } 18054 } else { 18055 tso = 0; 18056 mark = 6; 18057 } 18058 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 18059 ("%s: len > IP_MAXPACKET", __func__)); 18060 #ifdef DIAGNOSTIC 18061 #ifdef INET6 18062 if (max_linkhdr + hdrlen > MCLBYTES) 18063 #else 18064 if (max_linkhdr + hdrlen > MHLEN) 18065 #endif 18066 panic("tcphdr too big"); 18067 #endif 18068 18069 /* 18070 * This KASSERT is here to catch edge cases at a well defined place. 18071 * Before, those had triggered (random) panic conditions further 18072 * down. 18073 */ 18074 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 18075 if ((len == 0) && 18076 (flags & TH_FIN) && 18077 (sbused(sb))) { 18078 /* 18079 * We have outstanding data, don't send a fin by itself!. 18080 */ 18081 goto just_return; 18082 } 18083 /* 18084 * Grab a header mbuf, attaching a copy of data to be transmitted, 18085 * and initialize the header from the template for sends on this 18086 * connection. 18087 */ 18088 hw_tls = (sb->sb_flags & SB_TLS_IFNET) != 0; 18089 if (len) { 18090 uint32_t max_val; 18091 uint32_t moff; 18092 18093 if (rack->r_ctl.rc_pace_max_segs) 18094 max_val = rack->r_ctl.rc_pace_max_segs; 18095 else if (rack->rc_user_set_max_segs) 18096 max_val = rack->rc_user_set_max_segs * segsiz; 18097 else 18098 max_val = len; 18099 /* 18100 * We allow a limit on sending with hptsi. 18101 */ 18102 if (len > max_val) { 18103 mark = 7; 18104 len = max_val; 18105 } 18106 #ifdef INET6 18107 if (MHLEN < hdrlen + max_linkhdr) 18108 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 18109 else 18110 #endif 18111 m = m_gethdr(M_NOWAIT, MT_DATA); 18112 18113 if (m == NULL) { 18114 SOCKBUF_UNLOCK(sb); 18115 error = ENOBUFS; 18116 sack_rxmit = 0; 18117 goto out; 18118 } 18119 m->m_data += max_linkhdr; 18120 m->m_len = hdrlen; 18121 18122 /* 18123 * Start the m_copy functions from the closest mbuf to the 18124 * sb_offset in the socket buffer chain. 18125 */ 18126 mb = sbsndptr_noadv(sb, sb_offset, &moff); 18127 s_mb = mb; 18128 s_moff = moff; 18129 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 18130 m_copydata(mb, moff, (int)len, 18131 mtod(m, caddr_t)+hdrlen); 18132 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 18133 sbsndptr_adv(sb, mb, len); 18134 m->m_len += len; 18135 } else { 18136 struct sockbuf *msb; 18137 18138 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 18139 msb = NULL; 18140 else 18141 msb = sb; 18142 m->m_next = tcp_m_copym( 18143 mb, moff, &len, 18144 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb, 18145 ((rsm == NULL) ? hw_tls : 0) 18146 #ifdef NETFLIX_COPY_ARGS 18147 , &filled_all 18148 #endif 18149 ); 18150 if (len <= (tp->t_maxseg - optlen)) { 18151 /* 18152 * Must have ran out of mbufs for the copy 18153 * shorten it to no longer need tso. Lets 18154 * not put on sendalot since we are low on 18155 * mbufs. 18156 */ 18157 tso = 0; 18158 } 18159 if (m->m_next == NULL) { 18160 SOCKBUF_UNLOCK(sb); 18161 (void)m_free(m); 18162 error = ENOBUFS; 18163 sack_rxmit = 0; 18164 goto out; 18165 } 18166 } 18167 if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 18168 if (rsm && (rsm->r_flags & RACK_TLP)) { 18169 /* 18170 * TLP should not count in retran count, but 18171 * in its own bin 18172 */ 18173 counter_u64_add(rack_tlp_retran, 1); 18174 counter_u64_add(rack_tlp_retran_bytes, len); 18175 } else { 18176 tp->t_sndrexmitpack++; 18177 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 18178 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 18179 } 18180 #ifdef STATS 18181 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 18182 len); 18183 #endif 18184 } else { 18185 KMOD_TCPSTAT_INC(tcps_sndpack); 18186 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 18187 #ifdef STATS 18188 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 18189 len); 18190 #endif 18191 } 18192 /* 18193 * If we're sending everything we've got, set PUSH. (This 18194 * will keep happy those implementations which only give 18195 * data to the user when a buffer fills or a PUSH comes in.) 18196 */ 18197 if (sb_offset + len == sbused(sb) && 18198 sbused(sb) && 18199 !(flags & TH_SYN)) { 18200 flags |= TH_PUSH; 18201 add_flag |= RACK_HAD_PUSH; 18202 } 18203 18204 SOCKBUF_UNLOCK(sb); 18205 } else { 18206 SOCKBUF_UNLOCK(sb); 18207 if (tp->t_flags & TF_ACKNOW) 18208 KMOD_TCPSTAT_INC(tcps_sndacks); 18209 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 18210 KMOD_TCPSTAT_INC(tcps_sndctrl); 18211 else 18212 KMOD_TCPSTAT_INC(tcps_sndwinup); 18213 18214 m = m_gethdr(M_NOWAIT, MT_DATA); 18215 if (m == NULL) { 18216 error = ENOBUFS; 18217 sack_rxmit = 0; 18218 goto out; 18219 } 18220 #ifdef INET6 18221 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 18222 MHLEN >= hdrlen) { 18223 M_ALIGN(m, hdrlen); 18224 } else 18225 #endif 18226 m->m_data += max_linkhdr; 18227 m->m_len = hdrlen; 18228 } 18229 SOCKBUF_UNLOCK_ASSERT(sb); 18230 m->m_pkthdr.rcvif = (struct ifnet *)0; 18231 #ifdef MAC 18232 mac_inpcb_create_mbuf(inp, m); 18233 #endif 18234 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) { 18235 #ifdef INET6 18236 if (isipv6) 18237 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 18238 else 18239 #endif /* INET6 */ 18240 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 18241 th = rack->r_ctl.fsb.th; 18242 udp = rack->r_ctl.fsb.udp; 18243 if (udp) { 18244 #ifdef INET6 18245 if (isipv6) 18246 ulen = hdrlen + len - sizeof(struct ip6_hdr); 18247 else 18248 #endif /* INET6 */ 18249 ulen = hdrlen + len - sizeof(struct ip); 18250 udp->uh_ulen = htons(ulen); 18251 } 18252 } else { 18253 #ifdef INET6 18254 if (isipv6) { 18255 ip6 = mtod(m, struct ip6_hdr *); 18256 if (tp->t_port) { 18257 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 18258 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 18259 udp->uh_dport = tp->t_port; 18260 ulen = hdrlen + len - sizeof(struct ip6_hdr); 18261 udp->uh_ulen = htons(ulen); 18262 th = (struct tcphdr *)(udp + 1); 18263 } else 18264 th = (struct tcphdr *)(ip6 + 1); 18265 tcpip_fillheaders(inp, tp->t_port, ip6, th); 18266 } else 18267 #endif /* INET6 */ 18268 { 18269 ip = mtod(m, struct ip *); 18270 #ifdef TCPDEBUG 18271 ipov = (struct ipovly *)ip; 18272 #endif 18273 if (tp->t_port) { 18274 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 18275 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 18276 udp->uh_dport = tp->t_port; 18277 ulen = hdrlen + len - sizeof(struct ip); 18278 udp->uh_ulen = htons(ulen); 18279 th = (struct tcphdr *)(udp + 1); 18280 } else 18281 th = (struct tcphdr *)(ip + 1); 18282 tcpip_fillheaders(inp, tp->t_port, ip, th); 18283 } 18284 } 18285 /* 18286 * Fill in fields, remembering maximum advertised window for use in 18287 * delaying messages about window sizes. If resending a FIN, be sure 18288 * not to use a new sequence number. 18289 */ 18290 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 18291 tp->snd_nxt == tp->snd_max) 18292 tp->snd_nxt--; 18293 /* 18294 * If we are starting a connection, send ECN setup SYN packet. If we 18295 * are on a retransmit, we may resend those bits a number of times 18296 * as per RFC 3168. 18297 */ 18298 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) { 18299 if (tp->t_rxtshift >= 1) { 18300 if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 18301 flags |= TH_ECE | TH_CWR; 18302 } else 18303 flags |= TH_ECE | TH_CWR; 18304 } 18305 /* Handle parallel SYN for ECN */ 18306 if ((tp->t_state == TCPS_SYN_RECEIVED) && 18307 (tp->t_flags2 & TF2_ECN_SND_ECE)) { 18308 flags |= TH_ECE; 18309 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 18310 } 18311 if (TCPS_HAVEESTABLISHED(tp->t_state) && 18312 (tp->t_flags2 & TF2_ECN_PERMIT)) { 18313 /* 18314 * If the peer has ECN, mark data packets with ECN capable 18315 * transmission (ECT). Ignore pure ack packets, 18316 * retransmissions. 18317 */ 18318 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 18319 (sack_rxmit == 0)) { 18320 #ifdef INET6 18321 if (isipv6) 18322 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 18323 else 18324 #endif 18325 ip->ip_tos |= IPTOS_ECN_ECT0; 18326 KMOD_TCPSTAT_INC(tcps_ecn_ect0); 18327 /* 18328 * Reply with proper ECN notifications. 18329 * Only set CWR on new data segments. 18330 */ 18331 if (tp->t_flags2 & TF2_ECN_SND_CWR) { 18332 flags |= TH_CWR; 18333 tp->t_flags2 &= ~TF2_ECN_SND_CWR; 18334 } 18335 } 18336 if (tp->t_flags2 & TF2_ECN_SND_ECE) 18337 flags |= TH_ECE; 18338 } 18339 /* 18340 * If we are doing retransmissions, then snd_nxt will not reflect 18341 * the first unsent octet. For ACK only packets, we do not want the 18342 * sequence number of the retransmitted packet, we want the sequence 18343 * number of the next unsent octet. So, if there is no data (and no 18344 * SYN or FIN), use snd_max instead of snd_nxt when filling in 18345 * ti_seq. But if we are in persist state, snd_max might reflect 18346 * one byte beyond the right edge of the window, so use snd_nxt in 18347 * that case, since we know we aren't doing a retransmission. 18348 * (retransmit and persist are mutually exclusive...) 18349 */ 18350 if (sack_rxmit == 0) { 18351 if (len || (flags & (TH_SYN | TH_FIN))) { 18352 th->th_seq = htonl(tp->snd_nxt); 18353 rack_seq = tp->snd_nxt; 18354 } else { 18355 th->th_seq = htonl(tp->snd_max); 18356 rack_seq = tp->snd_max; 18357 } 18358 } else { 18359 th->th_seq = htonl(rsm->r_start); 18360 rack_seq = rsm->r_start; 18361 } 18362 th->th_ack = htonl(tp->rcv_nxt); 18363 th->th_flags = flags; 18364 /* 18365 * Calculate receive window. Don't shrink window, but avoid silly 18366 * window syndrome. 18367 * If a RST segment is sent, advertise a window of zero. 18368 */ 18369 if (flags & TH_RST) { 18370 recwin = 0; 18371 } else { 18372 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 18373 recwin < (long)segsiz) { 18374 recwin = 0; 18375 } 18376 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 18377 recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 18378 recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 18379 } 18380 18381 /* 18382 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 18383 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 18384 * handled in syncache. 18385 */ 18386 if (flags & TH_SYN) 18387 th->th_win = htons((u_short) 18388 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 18389 else { 18390 /* Avoid shrinking window with window scaling. */ 18391 recwin = roundup2(recwin, 1 << tp->rcv_scale); 18392 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 18393 } 18394 /* 18395 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 18396 * window. This may cause the remote transmitter to stall. This 18397 * flag tells soreceive() to disable delayed acknowledgements when 18398 * draining the buffer. This can occur if the receiver is 18399 * attempting to read more data than can be buffered prior to 18400 * transmitting on the connection. 18401 */ 18402 if (th->th_win == 0) { 18403 tp->t_sndzerowin++; 18404 tp->t_flags |= TF_RXWIN0SENT; 18405 } else 18406 tp->t_flags &= ~TF_RXWIN0SENT; 18407 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */ 18408 /* Now are we using fsb?, if so copy the template data to the mbuf */ 18409 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) { 18410 uint8_t *cpto; 18411 18412 cpto = mtod(m, uint8_t *); 18413 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 18414 /* 18415 * We have just copied in: 18416 * IP/IP6 18417 * <optional udphdr> 18418 * tcphdr (no options) 18419 * 18420 * We need to grab the correct pointers into the mbuf 18421 * for both the tcp header, and possibly the udp header (if tunneling). 18422 * We do this by using the offset in the copy buffer and adding it 18423 * to the mbuf base pointer (cpto). 18424 */ 18425 #ifdef INET6 18426 if (isipv6) 18427 ip6 = mtod(m, struct ip6_hdr *); 18428 else 18429 #endif /* INET6 */ 18430 ip = mtod(m, struct ip *); 18431 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 18432 /* If we have a udp header lets set it into the mbuf as well */ 18433 if (udp) 18434 udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr)); 18435 } 18436 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 18437 if (to.to_flags & TOF_SIGNATURE) { 18438 /* 18439 * Calculate MD5 signature and put it into the place 18440 * determined before. 18441 * NOTE: since TCP options buffer doesn't point into 18442 * mbuf's data, calculate offset and use it. 18443 */ 18444 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 18445 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 18446 /* 18447 * Do not send segment if the calculation of MD5 18448 * digest has failed. 18449 */ 18450 goto out; 18451 } 18452 } 18453 #endif 18454 if (optlen) { 18455 bcopy(opt, th + 1, optlen); 18456 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 18457 } 18458 /* 18459 * Put TCP length in extended header, and then checksum extended 18460 * header and data. 18461 */ 18462 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 18463 #ifdef INET6 18464 if (isipv6) { 18465 /* 18466 * ip6_plen is not need to be filled now, and will be filled 18467 * in ip6_output. 18468 */ 18469 if (tp->t_port) { 18470 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 18471 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 18472 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 18473 th->th_sum = htons(0); 18474 UDPSTAT_INC(udps_opackets); 18475 } else { 18476 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 18477 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 18478 th->th_sum = in6_cksum_pseudo(ip6, 18479 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 18480 0); 18481 } 18482 } 18483 #endif 18484 #if defined(INET6) && defined(INET) 18485 else 18486 #endif 18487 #ifdef INET 18488 { 18489 if (tp->t_port) { 18490 m->m_pkthdr.csum_flags = CSUM_UDP; 18491 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 18492 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 18493 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 18494 th->th_sum = htons(0); 18495 UDPSTAT_INC(udps_opackets); 18496 } else { 18497 m->m_pkthdr.csum_flags = CSUM_TCP; 18498 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 18499 th->th_sum = in_pseudo(ip->ip_src.s_addr, 18500 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 18501 IPPROTO_TCP + len + optlen)); 18502 } 18503 /* IP version must be set here for ipv4/ipv6 checking later */ 18504 KASSERT(ip->ip_v == IPVERSION, 18505 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 18506 } 18507 #endif 18508 /* 18509 * Enable TSO and specify the size of the segments. The TCP pseudo 18510 * header checksum is always provided. XXX: Fixme: This is currently 18511 * not the case for IPv6. 18512 */ 18513 if (tso) { 18514 KASSERT(len > tp->t_maxseg - optlen, 18515 ("%s: len <= tso_segsz", __func__)); 18516 m->m_pkthdr.csum_flags |= CSUM_TSO; 18517 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 18518 } 18519 KASSERT(len + hdrlen == m_length(m, NULL), 18520 ("%s: mbuf chain different than expected: %d + %u != %u", 18521 __func__, len, hdrlen, m_length(m, NULL))); 18522 18523 #ifdef TCP_HHOOK 18524 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 18525 hhook_run_tcp_est_out(tp, th, &to, len, tso); 18526 #endif 18527 /* We're getting ready to send; log now. */ 18528 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 18529 union tcp_log_stackspecific log; 18530 18531 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 18532 log.u_bbr.inhpts = rack->rc_inp->inp_in_hpts; 18533 log.u_bbr.ininput = rack->rc_inp->inp_in_input; 18534 if (rack->rack_no_prr) 18535 log.u_bbr.flex1 = 0; 18536 else 18537 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 18538 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 18539 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 18540 log.u_bbr.flex4 = orig_len; 18541 if (filled_all) 18542 log.u_bbr.flex5 = 0x80000000; 18543 else 18544 log.u_bbr.flex5 = 0; 18545 /* Save off the early/late values */ 18546 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 18547 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 18548 log.u_bbr.bw_inuse = rack_get_bw(rack); 18549 if (rsm || sack_rxmit) { 18550 if (doing_tlp) 18551 log.u_bbr.flex8 = 2; 18552 else 18553 log.u_bbr.flex8 = 1; 18554 } else { 18555 if (doing_tlp) 18556 log.u_bbr.flex8 = 3; 18557 else 18558 log.u_bbr.flex8 = 0; 18559 } 18560 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm); 18561 log.u_bbr.flex7 = mark; 18562 log.u_bbr.flex7 <<= 8; 18563 log.u_bbr.flex7 |= pass; 18564 log.u_bbr.pkts_out = tp->t_maxseg; 18565 log.u_bbr.timeStamp = cts; 18566 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 18567 log.u_bbr.lt_epoch = cwnd_to_use; 18568 log.u_bbr.delivered = sendalot; 18569 lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 18570 len, &log, false, NULL, NULL, 0, &tv); 18571 } else 18572 lgb = NULL; 18573 18574 /* 18575 * Fill in IP length and desired time to live and send to IP level. 18576 * There should be a better way to handle ttl and tos; we could keep 18577 * them in the template, but need a way to checksum without them. 18578 */ 18579 /* 18580 * m->m_pkthdr.len should have been set before cksum calcuration, 18581 * because in6_cksum() need it. 18582 */ 18583 #ifdef INET6 18584 if (isipv6) { 18585 /* 18586 * we separately set hoplimit for every segment, since the 18587 * user might want to change the value via setsockopt. Also, 18588 * desired default hop limit might be changed via Neighbor 18589 * Discovery. 18590 */ 18591 rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL); 18592 18593 /* 18594 * Set the packet size here for the benefit of DTrace 18595 * probes. ip6_output() will set it properly; it's supposed 18596 * to include the option header lengths as well. 18597 */ 18598 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 18599 18600 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 18601 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 18602 else 18603 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 18604 18605 if (tp->t_state == TCPS_SYN_SENT) 18606 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 18607 18608 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 18609 /* TODO: IPv6 IP6TOS_ECT bit on */ 18610 error = ip6_output(m, 18611 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 18612 inp->in6p_outputopts, 18613 #else 18614 NULL, 18615 #endif 18616 &inp->inp_route6, 18617 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 18618 NULL, NULL, inp); 18619 18620 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 18621 mtu = inp->inp_route6.ro_nh->nh_mtu; 18622 } 18623 #endif /* INET6 */ 18624 #if defined(INET) && defined(INET6) 18625 else 18626 #endif 18627 #ifdef INET 18628 { 18629 ip->ip_len = htons(m->m_pkthdr.len); 18630 #ifdef INET6 18631 if (inp->inp_vflag & INP_IPV6PROTO) 18632 ip->ip_ttl = in6_selecthlim(inp, NULL); 18633 #endif /* INET6 */ 18634 rack->r_ctl.fsb.hoplimit = ip->ip_ttl; 18635 /* 18636 * If we do path MTU discovery, then we set DF on every 18637 * packet. This might not be the best thing to do according 18638 * to RFC3390 Section 2. However the tcp hostcache migitates 18639 * the problem so it affects only the first tcp connection 18640 * with a host. 18641 * 18642 * NB: Don't set DF on small MTU/MSS to have a safe 18643 * fallback. 18644 */ 18645 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 18646 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 18647 if (tp->t_port == 0 || len < V_tcp_minmss) { 18648 ip->ip_off |= htons(IP_DF); 18649 } 18650 } else { 18651 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 18652 } 18653 18654 if (tp->t_state == TCPS_SYN_SENT) 18655 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 18656 18657 TCP_PROBE5(send, NULL, tp, ip, tp, th); 18658 18659 error = ip_output(m, 18660 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 18661 inp->inp_options, 18662 #else 18663 NULL, 18664 #endif 18665 &inp->inp_route, 18666 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 18667 inp); 18668 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 18669 mtu = inp->inp_route.ro_nh->nh_mtu; 18670 } 18671 #endif /* INET */ 18672 18673 out: 18674 if (lgb) { 18675 lgb->tlb_errno = error; 18676 lgb = NULL; 18677 } 18678 /* 18679 * In transmit state, time the transmission and arrange for the 18680 * retransmit. In persist state, just set snd_max. 18681 */ 18682 if (error == 0) { 18683 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 18684 if (rsm && doing_tlp) { 18685 rack->rc_last_sent_tlp_past_cumack = 0; 18686 rack->rc_last_sent_tlp_seq_valid = 1; 18687 rack->r_ctl.last_sent_tlp_seq = rsm->r_start; 18688 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start; 18689 } 18690 rack->forced_ack = 0; /* If we send something zap the FA flag */ 18691 if (rsm && (doing_tlp == 0)) { 18692 /* Set we retransmitted */ 18693 rack->rc_gp_saw_rec = 1; 18694 } else { 18695 if (cwnd_to_use > tp->snd_ssthresh) { 18696 /* Set we sent in CA */ 18697 rack->rc_gp_saw_ca = 1; 18698 } else { 18699 /* Set we sent in SS */ 18700 rack->rc_gp_saw_ss = 1; 18701 } 18702 } 18703 if (doing_tlp && (rsm == NULL)) { 18704 /* Make sure new data TLP cnt is clear */ 18705 rack->r_ctl.rc_tlp_new_data = 0; 18706 } 18707 if (TCPS_HAVEESTABLISHED(tp->t_state) && 18708 (tp->t_flags & TF_SACK_PERMIT) && 18709 tp->rcv_numsacks > 0) 18710 tcp_clean_dsack_blocks(tp); 18711 tot_len_this_send += len; 18712 if (len == 0) 18713 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1); 18714 else if (len == 1) { 18715 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1); 18716 } else if (len > 1) { 18717 int idx; 18718 18719 idx = (len / segsiz) + 3; 18720 if (idx >= TCP_MSS_ACCT_ATIMER) 18721 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 18722 else 18723 counter_u64_add(rack_out_size[idx], 1); 18724 } 18725 } 18726 if ((rack->rack_no_prr == 0) && 18727 sub_from_prr && 18728 (error == 0)) { 18729 if (rack->r_ctl.rc_prr_sndcnt >= len) 18730 rack->r_ctl.rc_prr_sndcnt -= len; 18731 else 18732 rack->r_ctl.rc_prr_sndcnt = 0; 18733 } 18734 sub_from_prr = 0; 18735 if (doing_tlp) { 18736 /* Make sure the TLP is added */ 18737 add_flag |= RACK_TLP; 18738 } else if (rsm) { 18739 /* If its a resend without TLP then it must not have the flag */ 18740 rsm->r_flags &= ~RACK_TLP; 18741 } 18742 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, 18743 rack_to_usec_ts(&tv), 18744 rsm, add_flag, s_mb, s_moff, hw_tls); 18745 18746 18747 if ((error == 0) && 18748 (len > 0) && 18749 (tp->snd_una == tp->snd_max)) 18750 rack->r_ctl.rc_tlp_rxt_last_time = cts; 18751 { 18752 tcp_seq startseq = tp->snd_nxt; 18753 18754 /* Track our lost count */ 18755 if (rsm && (doing_tlp == 0)) 18756 rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start; 18757 /* 18758 * Advance snd_nxt over sequence space of this segment. 18759 */ 18760 if (error) 18761 /* We don't log or do anything with errors */ 18762 goto nomore; 18763 if (doing_tlp == 0) { 18764 if (rsm == NULL) { 18765 /* 18766 * Not a retransmission of some 18767 * sort, new data is going out so 18768 * clear our TLP count and flag. 18769 */ 18770 rack->rc_tlp_in_progress = 0; 18771 rack->r_ctl.rc_tlp_cnt_out = 0; 18772 } 18773 } else { 18774 /* 18775 * We have just sent a TLP, mark that it is true 18776 * and make sure our in progress is set so we 18777 * continue to check the count. 18778 */ 18779 rack->rc_tlp_in_progress = 1; 18780 rack->r_ctl.rc_tlp_cnt_out++; 18781 } 18782 if (flags & (TH_SYN | TH_FIN)) { 18783 if (flags & TH_SYN) 18784 tp->snd_nxt++; 18785 if (flags & TH_FIN) { 18786 tp->snd_nxt++; 18787 tp->t_flags |= TF_SENTFIN; 18788 } 18789 } 18790 /* In the ENOBUFS case we do *not* update snd_max */ 18791 if (sack_rxmit) 18792 goto nomore; 18793 18794 tp->snd_nxt += len; 18795 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 18796 if (tp->snd_una == tp->snd_max) { 18797 /* 18798 * Update the time we just added data since 18799 * none was outstanding. 18800 */ 18801 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 18802 tp->t_acktime = ticks; 18803 } 18804 tp->snd_max = tp->snd_nxt; 18805 /* 18806 * Time this transmission if not a retransmission and 18807 * not currently timing anything. 18808 * This is only relevant in case of switching back to 18809 * the base stack. 18810 */ 18811 if (tp->t_rtttime == 0) { 18812 tp->t_rtttime = ticks; 18813 tp->t_rtseq = startseq; 18814 KMOD_TCPSTAT_INC(tcps_segstimed); 18815 } 18816 if (len && 18817 ((tp->t_flags & TF_GPUTINPROG) == 0)) 18818 rack_start_gp_measurement(tp, rack, startseq, sb_offset); 18819 } 18820 /* 18821 * If we are doing FO we need to update the mbuf position and subtract 18822 * this happens when the peer sends us duplicate information and 18823 * we thus want to send a DSACK. 18824 * 18825 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO 18826 * turned off? If not then we are going to echo multiple DSACK blocks 18827 * out (with the TSO), which we should not be doing. 18828 */ 18829 if (rack->r_fast_output && len) { 18830 if (rack->r_ctl.fsb.left_to_send > len) 18831 rack->r_ctl.fsb.left_to_send -= len; 18832 else 18833 rack->r_ctl.fsb.left_to_send = 0; 18834 if (rack->r_ctl.fsb.left_to_send < segsiz) 18835 rack->r_fast_output = 0; 18836 if (rack->r_fast_output) { 18837 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 18838 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 18839 } 18840 } 18841 } 18842 nomore: 18843 if (error) { 18844 rack->r_ctl.rc_agg_delayed = 0; 18845 rack->r_early = 0; 18846 rack->r_late = 0; 18847 rack->r_ctl.rc_agg_early = 0; 18848 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */ 18849 /* 18850 * Failures do not advance the seq counter above. For the 18851 * case of ENOBUFS we will fall out and retry in 1ms with 18852 * the hpts. Everything else will just have to retransmit 18853 * with the timer. 18854 * 18855 * In any case, we do not want to loop around for another 18856 * send without a good reason. 18857 */ 18858 sendalot = 0; 18859 switch (error) { 18860 case EPERM: 18861 tp->t_softerror = error; 18862 #ifdef TCP_ACCOUNTING 18863 crtsc = get_cyclecount(); 18864 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18865 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 18866 } 18867 counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1); 18868 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18869 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 18870 } 18871 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val)); 18872 sched_unpin(); 18873 #endif 18874 return (error); 18875 case ENOBUFS: 18876 /* 18877 * Pace us right away to retry in a some 18878 * time 18879 */ 18880 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC); 18881 if (rack->rc_enobuf < 0x7f) 18882 rack->rc_enobuf++; 18883 if (slot < (10 * HPTS_USEC_IN_MSEC)) 18884 slot = 10 * HPTS_USEC_IN_MSEC; 18885 if (rack->r_ctl.crte != NULL) { 18886 counter_u64_add(rack_saw_enobuf_hw, 1); 18887 tcp_rl_log_enobuf(rack->r_ctl.crte); 18888 } 18889 counter_u64_add(rack_saw_enobuf, 1); 18890 goto enobufs; 18891 case EMSGSIZE: 18892 /* 18893 * For some reason the interface we used initially 18894 * to send segments changed to another or lowered 18895 * its MTU. If TSO was active we either got an 18896 * interface without TSO capabilits or TSO was 18897 * turned off. If we obtained mtu from ip_output() 18898 * then update it and try again. 18899 */ 18900 if (tso) 18901 tp->t_flags &= ~TF_TSO; 18902 if (mtu != 0) { 18903 tcp_mss_update(tp, -1, mtu, NULL, NULL); 18904 goto again; 18905 } 18906 slot = 10 * HPTS_USEC_IN_MSEC; 18907 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0); 18908 #ifdef TCP_ACCOUNTING 18909 crtsc = get_cyclecount(); 18910 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18911 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 18912 } 18913 counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1); 18914 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18915 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 18916 } 18917 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val)); 18918 sched_unpin(); 18919 #endif 18920 return (error); 18921 case ENETUNREACH: 18922 counter_u64_add(rack_saw_enetunreach, 1); 18923 case EHOSTDOWN: 18924 case EHOSTUNREACH: 18925 case ENETDOWN: 18926 if (TCPS_HAVERCVDSYN(tp->t_state)) { 18927 tp->t_softerror = error; 18928 } 18929 /* FALLTHROUGH */ 18930 default: 18931 slot = 10 * HPTS_USEC_IN_MSEC; 18932 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0); 18933 #ifdef TCP_ACCOUNTING 18934 crtsc = get_cyclecount(); 18935 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18936 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 18937 } 18938 counter_u64_add(tcp_cnt_counters[SND_OUT_FAIL], 1); 18939 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18940 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 18941 } 18942 counter_u64_add(tcp_proc_time[SND_OUT_FAIL], (crtsc - ts_val)); 18943 sched_unpin(); 18944 #endif 18945 return (error); 18946 } 18947 } else { 18948 rack->rc_enobuf = 0; 18949 if (IN_FASTRECOVERY(tp->t_flags) && rsm) 18950 rack->r_ctl.retran_during_recovery += len; 18951 } 18952 KMOD_TCPSTAT_INC(tcps_sndtotal); 18953 18954 /* 18955 * Data sent (as far as we can tell). If this advertises a larger 18956 * window than any other segment, then remember the size of the 18957 * advertised window. Any pending ACK has now been sent. 18958 */ 18959 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 18960 tp->rcv_adv = tp->rcv_nxt + recwin; 18961 18962 tp->last_ack_sent = tp->rcv_nxt; 18963 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 18964 enobufs: 18965 if (sendalot) { 18966 /* Do we need to turn off sendalot? */ 18967 if (rack->r_ctl.rc_pace_max_segs && 18968 (tot_len_this_send >= rack->r_ctl.rc_pace_max_segs)) { 18969 /* We hit our max. */ 18970 sendalot = 0; 18971 } else if ((rack->rc_user_set_max_segs) && 18972 (tot_len_this_send >= (rack->rc_user_set_max_segs * segsiz))) { 18973 /* We hit the user defined max */ 18974 sendalot = 0; 18975 } 18976 } 18977 if ((error == 0) && (flags & TH_FIN)) 18978 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 18979 if (flags & TH_RST) { 18980 /* 18981 * We don't send again after sending a RST. 18982 */ 18983 slot = 0; 18984 sendalot = 0; 18985 if (error == 0) 18986 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 18987 } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) { 18988 /* 18989 * Get our pacing rate, if an error 18990 * occurred in sending (ENOBUF) we would 18991 * hit the else if with slot preset. Other 18992 * errors return. 18993 */ 18994 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz); 18995 } 18996 if (rsm && 18997 (rsm->r_flags & RACK_HAS_SYN) == 0 && 18998 rack->use_rack_rr) { 18999 /* Its a retransmit and we use the rack cheat? */ 19000 if ((slot == 0) || 19001 (rack->rc_always_pace == 0) || 19002 (rack->r_rr_config == 1)) { 19003 /* 19004 * We have no pacing set or we 19005 * are using old-style rack or 19006 * we are overriden to use the old 1ms pacing. 19007 */ 19008 slot = rack->r_ctl.rc_min_to; 19009 } 19010 } 19011 /* We have sent clear the flag */ 19012 rack->r_ent_rec_ns = 0; 19013 if (rack->r_must_retran) { 19014 if (rsm) { 19015 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start); 19016 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) { 19017 /* 19018 * We have retransmitted all. 19019 */ 19020 rack->r_must_retran = 0; 19021 rack->r_ctl.rc_out_at_rto = 0; 19022 } 19023 } else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) { 19024 /* 19025 * Sending new data will also kill 19026 * the loop. 19027 */ 19028 rack->r_must_retran = 0; 19029 rack->r_ctl.rc_out_at_rto = 0; 19030 } 19031 } 19032 rack->r_ctl.fsb.recwin = recwin; 19033 if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) && 19034 SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) { 19035 /* 19036 * We hit an RTO and now have past snd_max at the RTO 19037 * clear all the WAS flags. 19038 */ 19039 tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY); 19040 } 19041 if (slot) { 19042 /* set the rack tcb into the slot N */ 19043 counter_u64_add(rack_paced_segments, 1); 19044 if ((error == 0) && 19045 rack_use_rfo && 19046 ((flags & (TH_SYN|TH_FIN)) == 0) && 19047 (rsm == NULL) && 19048 (tp->snd_nxt == tp->snd_max) && 19049 (ipoptlen == 0) && 19050 (tp->rcv_numsacks == 0) && 19051 rack->r_fsb_inited && 19052 TCPS_HAVEESTABLISHED(tp->t_state) && 19053 (rack->r_must_retran == 0) && 19054 ((tp->t_flags & TF_NEEDFIN) == 0) && 19055 (len > 0) && (orig_len > 0) && 19056 (orig_len > len) && 19057 ((orig_len - len) >= segsiz) && 19058 ((optlen == 0) || 19059 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 19060 /* We can send at least one more MSS using our fsb */ 19061 19062 rack->r_fast_output = 1; 19063 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 19064 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 19065 rack->r_ctl.fsb.tcp_flags = flags; 19066 rack->r_ctl.fsb.left_to_send = orig_len - len; 19067 if (hw_tls) 19068 rack->r_ctl.fsb.hw_tls = 1; 19069 else 19070 rack->r_ctl.fsb.hw_tls = 0; 19071 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))), 19072 ("rack:%p left_to_send:%u sbavail:%u out:%u", 19073 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb), 19074 (tp->snd_max - tp->snd_una))); 19075 if (rack->r_ctl.fsb.left_to_send < segsiz) 19076 rack->r_fast_output = 0; 19077 else { 19078 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una))) 19079 rack->r_ctl.fsb.rfo_apply_push = 1; 19080 else 19081 rack->r_ctl.fsb.rfo_apply_push = 0; 19082 } 19083 } else 19084 rack->r_fast_output = 0; 19085 rack_log_fsb(rack, tp, so, flags, 19086 ipoptlen, orig_len, len, error, 19087 (rsm == NULL), optlen, __LINE__, 2); 19088 } else if (sendalot) { 19089 int ret; 19090 19091 if (len) 19092 counter_u64_add(rack_unpaced_segments, 1); 19093 sack_rxmit = 0; 19094 if ((error == 0) && 19095 rack_use_rfo && 19096 ((flags & (TH_SYN|TH_FIN)) == 0) && 19097 (rsm == NULL) && 19098 (ipoptlen == 0) && 19099 (tp->rcv_numsacks == 0) && 19100 (tp->snd_nxt == tp->snd_max) && 19101 (rack->r_must_retran == 0) && 19102 rack->r_fsb_inited && 19103 TCPS_HAVEESTABLISHED(tp->t_state) && 19104 ((tp->t_flags & TF_NEEDFIN) == 0) && 19105 (len > 0) && (orig_len > 0) && 19106 (orig_len > len) && 19107 ((orig_len - len) >= segsiz) && 19108 ((optlen == 0) || 19109 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 19110 /* we can use fast_output for more */ 19111 19112 rack->r_fast_output = 1; 19113 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 19114 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 19115 rack->r_ctl.fsb.tcp_flags = flags; 19116 rack->r_ctl.fsb.left_to_send = orig_len - len; 19117 if (hw_tls) 19118 rack->r_ctl.fsb.hw_tls = 1; 19119 else 19120 rack->r_ctl.fsb.hw_tls = 0; 19121 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))), 19122 ("rack:%p left_to_send:%u sbavail:%u out:%u", 19123 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb), 19124 (tp->snd_max - tp->snd_una))); 19125 if (rack->r_ctl.fsb.left_to_send < segsiz) { 19126 rack->r_fast_output = 0; 19127 } 19128 if (rack->r_fast_output) { 19129 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una))) 19130 rack->r_ctl.fsb.rfo_apply_push = 1; 19131 else 19132 rack->r_ctl.fsb.rfo_apply_push = 0; 19133 rack_log_fsb(rack, tp, so, flags, 19134 ipoptlen, orig_len, len, error, 19135 (rsm == NULL), optlen, __LINE__, 3); 19136 error = 0; 19137 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error); 19138 if (ret >= 0) 19139 return (ret); 19140 else if (error) 19141 goto nomore; 19142 19143 } 19144 } 19145 goto again; 19146 } else if (len) { 19147 counter_u64_add(rack_unpaced_segments, 1); 19148 } 19149 /* Assure when we leave that snd_nxt will point to top */ 19150 if (SEQ_GT(tp->snd_max, tp->snd_nxt)) 19151 tp->snd_nxt = tp->snd_max; 19152 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0); 19153 #ifdef TCP_ACCOUNTING 19154 crtsc = get_cyclecount() - ts_val; 19155 if (tot_len_this_send) { 19156 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 19157 tp->tcp_cnt_counters[SND_OUT_DATA]++; 19158 } 19159 counter_u64_add(tcp_cnt_counters[SND_OUT_DATA], 1); 19160 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 19161 tp->tcp_proc_time[SND_OUT_DATA] += crtsc; 19162 } 19163 counter_u64_add(tcp_proc_time[SND_OUT_DATA], crtsc); 19164 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 19165 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz); 19166 } 19167 counter_u64_add(tcp_cnt_counters[CNT_OF_MSS_OUT], ((tot_len_this_send + segsiz - 1) /segsiz)); 19168 } else { 19169 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 19170 tp->tcp_cnt_counters[SND_OUT_ACK]++; 19171 } 19172 counter_u64_add(tcp_cnt_counters[SND_OUT_ACK], 1); 19173 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 19174 tp->tcp_proc_time[SND_OUT_ACK] += crtsc; 19175 } 19176 counter_u64_add(tcp_proc_time[SND_OUT_ACK], crtsc); 19177 } 19178 sched_unpin(); 19179 #endif 19180 if (error == ENOBUFS) 19181 error = 0; 19182 return (error); 19183 } 19184 19185 static void 19186 rack_update_seg(struct tcp_rack *rack) 19187 { 19188 uint32_t orig_val; 19189 19190 orig_val = rack->r_ctl.rc_pace_max_segs; 19191 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 19192 if (orig_val != rack->r_ctl.rc_pace_max_segs) 19193 rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0); 19194 } 19195 19196 static void 19197 rack_mtu_change(struct tcpcb *tp) 19198 { 19199 /* 19200 * The MSS may have changed 19201 */ 19202 struct tcp_rack *rack; 19203 19204 rack = (struct tcp_rack *)tp->t_fb_ptr; 19205 if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) { 19206 /* 19207 * The MTU has changed we need to resend everything 19208 * since all we have sent is lost. We first fix 19209 * up the mtu though. 19210 */ 19211 rack_set_pace_segments(tp, rack, __LINE__, NULL); 19212 /* We treat this like a full retransmit timeout without the cwnd adjustment */ 19213 rack_remxt_tmr(tp); 19214 rack->r_fast_output = 0; 19215 rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp, 19216 rack->r_ctl.rc_sacked); 19217 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max; 19218 rack->r_must_retran = 1; 19219 19220 } 19221 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 19222 /* We don't use snd_nxt to retransmit */ 19223 tp->snd_nxt = tp->snd_max; 19224 } 19225 19226 static int 19227 rack_set_profile(struct tcp_rack *rack, int prof) 19228 { 19229 int err = EINVAL; 19230 if (prof == 1) { 19231 /* pace_always=1 */ 19232 if (rack->rc_always_pace == 0) { 19233 if (tcp_can_enable_pacing() == 0) 19234 return (EBUSY); 19235 } 19236 rack->rc_always_pace = 1; 19237 if (rack->use_fixed_rate || rack->gp_ready) 19238 rack_set_cc_pacing(rack); 19239 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19240 rack->rack_attempt_hdwr_pace = 0; 19241 /* cmpack=1 */ 19242 if (rack_use_cmp_acks) 19243 rack->r_use_cmp_ack = 1; 19244 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) && 19245 rack->r_use_cmp_ack) 19246 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 19247 /* scwnd=1 */ 19248 rack->rack_enable_scwnd = 1; 19249 /* dynamic=100 */ 19250 rack->rc_gp_dyn_mul = 1; 19251 /* gp_inc_ca */ 19252 rack->r_ctl.rack_per_of_gp_ca = 100; 19253 /* rrr_conf=3 */ 19254 rack->r_rr_config = 3; 19255 /* npush=2 */ 19256 rack->r_ctl.rc_no_push_at_mrtt = 2; 19257 /* fillcw=1 */ 19258 rack->rc_pace_to_cwnd = 1; 19259 rack->rc_pace_fill_if_rttin_range = 0; 19260 rack->rtt_limit_mul = 0; 19261 /* noprr=1 */ 19262 rack->rack_no_prr = 1; 19263 /* lscwnd=1 */ 19264 rack->r_limit_scw = 1; 19265 /* gp_inc_rec */ 19266 rack->r_ctl.rack_per_of_gp_rec = 90; 19267 err = 0; 19268 19269 } else if (prof == 3) { 19270 /* Same as profile one execept fill_cw becomes 2 (less aggressive set) */ 19271 /* pace_always=1 */ 19272 if (rack->rc_always_pace == 0) { 19273 if (tcp_can_enable_pacing() == 0) 19274 return (EBUSY); 19275 } 19276 rack->rc_always_pace = 1; 19277 if (rack->use_fixed_rate || rack->gp_ready) 19278 rack_set_cc_pacing(rack); 19279 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19280 rack->rack_attempt_hdwr_pace = 0; 19281 /* cmpack=1 */ 19282 if (rack_use_cmp_acks) 19283 rack->r_use_cmp_ack = 1; 19284 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) && 19285 rack->r_use_cmp_ack) 19286 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 19287 /* scwnd=1 */ 19288 rack->rack_enable_scwnd = 1; 19289 /* dynamic=100 */ 19290 rack->rc_gp_dyn_mul = 1; 19291 /* gp_inc_ca */ 19292 rack->r_ctl.rack_per_of_gp_ca = 100; 19293 /* rrr_conf=3 */ 19294 rack->r_rr_config = 3; 19295 /* npush=2 */ 19296 rack->r_ctl.rc_no_push_at_mrtt = 2; 19297 /* fillcw=2 */ 19298 rack->rc_pace_to_cwnd = 1; 19299 rack->r_fill_less_agg = 1; 19300 rack->rc_pace_fill_if_rttin_range = 0; 19301 rack->rtt_limit_mul = 0; 19302 /* noprr=1 */ 19303 rack->rack_no_prr = 1; 19304 /* lscwnd=1 */ 19305 rack->r_limit_scw = 1; 19306 /* gp_inc_rec */ 19307 rack->r_ctl.rack_per_of_gp_rec = 90; 19308 err = 0; 19309 19310 19311 } else if (prof == 2) { 19312 /* cmpack=1 */ 19313 if (rack->rc_always_pace == 0) { 19314 if (tcp_can_enable_pacing() == 0) 19315 return (EBUSY); 19316 } 19317 rack->rc_always_pace = 1; 19318 if (rack->use_fixed_rate || rack->gp_ready) 19319 rack_set_cc_pacing(rack); 19320 rack->r_use_cmp_ack = 1; 19321 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state)) 19322 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 19323 /* pace_always=1 */ 19324 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19325 /* scwnd=1 */ 19326 rack->rack_enable_scwnd = 1; 19327 /* dynamic=100 */ 19328 rack->rc_gp_dyn_mul = 1; 19329 rack->r_ctl.rack_per_of_gp_ca = 100; 19330 /* rrr_conf=3 */ 19331 rack->r_rr_config = 3; 19332 /* npush=2 */ 19333 rack->r_ctl.rc_no_push_at_mrtt = 2; 19334 /* fillcw=1 */ 19335 rack->rc_pace_to_cwnd = 1; 19336 rack->rc_pace_fill_if_rttin_range = 0; 19337 rack->rtt_limit_mul = 0; 19338 /* noprr=1 */ 19339 rack->rack_no_prr = 1; 19340 /* lscwnd=0 */ 19341 rack->r_limit_scw = 0; 19342 err = 0; 19343 } else if (prof == 0) { 19344 /* This changes things back to the default settings */ 19345 err = 0; 19346 if (rack->rc_always_pace) { 19347 tcp_decrement_paced_conn(); 19348 rack_undo_cc_pacing(rack); 19349 rack->rc_always_pace = 0; 19350 } 19351 if (rack_pace_every_seg && tcp_can_enable_pacing()) { 19352 rack->rc_always_pace = 1; 19353 if (rack->use_fixed_rate || rack->gp_ready) 19354 rack_set_cc_pacing(rack); 19355 } else 19356 rack->rc_always_pace = 0; 19357 if (rack_dsack_std_based & 0x1) { 19358 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */ 19359 rack->rc_rack_tmr_std_based = 1; 19360 } 19361 if (rack_dsack_std_based & 0x2) { 19362 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */ 19363 rack->rc_rack_use_dsack = 1; 19364 } 19365 if (rack_use_cmp_acks) 19366 rack->r_use_cmp_ack = 1; 19367 else 19368 rack->r_use_cmp_ack = 0; 19369 if (rack_disable_prr) 19370 rack->rack_no_prr = 1; 19371 else 19372 rack->rack_no_prr = 0; 19373 if (rack_gp_no_rec_chg) 19374 rack->rc_gp_no_rec_chg = 1; 19375 else 19376 rack->rc_gp_no_rec_chg = 0; 19377 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) { 19378 rack->r_mbuf_queue = 1; 19379 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state)) 19380 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 19381 rack->rc_inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19382 } else { 19383 rack->r_mbuf_queue = 0; 19384 rack->rc_inp->inp_flags2 &= ~INP_SUPPORTS_MBUFQ; 19385 } 19386 if (rack_enable_shared_cwnd) 19387 rack->rack_enable_scwnd = 1; 19388 else 19389 rack->rack_enable_scwnd = 0; 19390 if (rack_do_dyn_mul) { 19391 /* When dynamic adjustment is on CA needs to start at 100% */ 19392 rack->rc_gp_dyn_mul = 1; 19393 if (rack_do_dyn_mul >= 100) 19394 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul; 19395 } else { 19396 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca; 19397 rack->rc_gp_dyn_mul = 0; 19398 } 19399 rack->r_rr_config = 0; 19400 rack->r_ctl.rc_no_push_at_mrtt = 0; 19401 rack->rc_pace_to_cwnd = 0; 19402 rack->rc_pace_fill_if_rttin_range = 0; 19403 rack->rtt_limit_mul = 0; 19404 19405 if (rack_enable_hw_pacing) 19406 rack->rack_hdw_pace_ena = 1; 19407 else 19408 rack->rack_hdw_pace_ena = 0; 19409 if (rack_disable_prr) 19410 rack->rack_no_prr = 1; 19411 else 19412 rack->rack_no_prr = 0; 19413 if (rack_limits_scwnd) 19414 rack->r_limit_scw = 1; 19415 else 19416 rack->r_limit_scw = 0; 19417 err = 0; 19418 } 19419 return (err); 19420 } 19421 19422 static int 19423 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval) 19424 { 19425 struct deferred_opt_list *dol; 19426 19427 dol = malloc(sizeof(struct deferred_opt_list), 19428 M_TCPFSB, M_NOWAIT|M_ZERO); 19429 if (dol == NULL) { 19430 /* 19431 * No space yikes -- fail out.. 19432 */ 19433 return (0); 19434 } 19435 dol->optname = sopt_name; 19436 dol->optval = loptval; 19437 TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next); 19438 return (1); 19439 } 19440 19441 static int 19442 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name, 19443 uint32_t optval, uint64_t loptval) 19444 { 19445 struct epoch_tracker et; 19446 struct sockopt sopt; 19447 struct cc_newreno_opts opt; 19448 uint64_t val; 19449 int error = 0; 19450 uint16_t ca, ss; 19451 19452 switch (sopt_name) { 19453 19454 case TCP_RACK_DSACK_OPT: 19455 RACK_OPTS_INC(tcp_rack_dsack_opt); 19456 if (optval & 0x1) { 19457 rack->rc_rack_tmr_std_based = 1; 19458 } else { 19459 rack->rc_rack_tmr_std_based = 0; 19460 } 19461 if (optval & 0x2) { 19462 rack->rc_rack_use_dsack = 1; 19463 } else { 19464 rack->rc_rack_use_dsack = 0; 19465 } 19466 rack_log_dsack_event(rack, 5, __LINE__, 0, 0); 19467 break; 19468 case TCP_RACK_PACING_BETA: 19469 RACK_OPTS_INC(tcp_rack_beta); 19470 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) { 19471 /* This only works for newreno. */ 19472 error = EINVAL; 19473 break; 19474 } 19475 if (rack->rc_pacing_cc_set) { 19476 /* 19477 * Set them into the real CC module 19478 * whats in the rack pcb is the old values 19479 * to be used on restoral/ 19480 */ 19481 sopt.sopt_dir = SOPT_SET; 19482 opt.name = CC_NEWRENO_BETA; 19483 opt.val = optval; 19484 if (CC_ALGO(tp)->ctl_output != NULL) 19485 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt); 19486 else { 19487 error = ENOENT; 19488 break; 19489 } 19490 } else { 19491 /* 19492 * Not pacing yet so set it into our local 19493 * rack pcb storage. 19494 */ 19495 rack->r_ctl.rc_saved_beta.beta = optval; 19496 } 19497 break; 19498 case TCP_RACK_TIMER_SLOP: 19499 RACK_OPTS_INC(tcp_rack_timer_slop); 19500 rack->r_ctl.timer_slop = optval; 19501 if (rack->rc_tp->t_srtt) { 19502 /* 19503 * If we have an SRTT lets update t_rxtcur 19504 * to have the new slop. 19505 */ 19506 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 19507 rack_rto_min, rack_rto_max, 19508 rack->r_ctl.timer_slop); 19509 } 19510 break; 19511 case TCP_RACK_PACING_BETA_ECN: 19512 RACK_OPTS_INC(tcp_rack_beta_ecn); 19513 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) { 19514 /* This only works for newreno. */ 19515 error = EINVAL; 19516 break; 19517 } 19518 if (rack->rc_pacing_cc_set) { 19519 /* 19520 * Set them into the real CC module 19521 * whats in the rack pcb is the old values 19522 * to be used on restoral/ 19523 */ 19524 sopt.sopt_dir = SOPT_SET; 19525 opt.name = CC_NEWRENO_BETA_ECN; 19526 opt.val = optval; 19527 if (CC_ALGO(tp)->ctl_output != NULL) 19528 error = CC_ALGO(tp)->ctl_output(tp->ccv, &sopt, &opt); 19529 else 19530 error = ENOENT; 19531 } else { 19532 /* 19533 * Not pacing yet so set it into our local 19534 * rack pcb storage. 19535 */ 19536 rack->r_ctl.rc_saved_beta.beta_ecn = optval; 19537 rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN; 19538 } 19539 break; 19540 case TCP_DEFER_OPTIONS: 19541 RACK_OPTS_INC(tcp_defer_opt); 19542 if (optval) { 19543 if (rack->gp_ready) { 19544 /* Too late */ 19545 error = EINVAL; 19546 break; 19547 } 19548 rack->defer_options = 1; 19549 } else 19550 rack->defer_options = 0; 19551 break; 19552 case TCP_RACK_MEASURE_CNT: 19553 RACK_OPTS_INC(tcp_rack_measure_cnt); 19554 if (optval && (optval <= 0xff)) { 19555 rack->r_ctl.req_measurements = optval; 19556 } else 19557 error = EINVAL; 19558 break; 19559 case TCP_REC_ABC_VAL: 19560 RACK_OPTS_INC(tcp_rec_abc_val); 19561 if (optval > 0) 19562 rack->r_use_labc_for_rec = 1; 19563 else 19564 rack->r_use_labc_for_rec = 0; 19565 break; 19566 case TCP_RACK_ABC_VAL: 19567 RACK_OPTS_INC(tcp_rack_abc_val); 19568 if ((optval > 0) && (optval < 255)) 19569 rack->rc_labc = optval; 19570 else 19571 error = EINVAL; 19572 break; 19573 case TCP_HDWR_UP_ONLY: 19574 RACK_OPTS_INC(tcp_pacing_up_only); 19575 if (optval) 19576 rack->r_up_only = 1; 19577 else 19578 rack->r_up_only = 0; 19579 break; 19580 case TCP_PACING_RATE_CAP: 19581 RACK_OPTS_INC(tcp_pacing_rate_cap); 19582 rack->r_ctl.bw_rate_cap = loptval; 19583 break; 19584 case TCP_RACK_PROFILE: 19585 RACK_OPTS_INC(tcp_profile); 19586 error = rack_set_profile(rack, optval); 19587 break; 19588 case TCP_USE_CMP_ACKS: 19589 RACK_OPTS_INC(tcp_use_cmp_acks); 19590 if ((optval == 0) && (rack->rc_inp->inp_flags2 & INP_MBUF_ACKCMP)) { 19591 /* You can't turn it off once its on! */ 19592 error = EINVAL; 19593 } else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) { 19594 rack->r_use_cmp_ack = 1; 19595 rack->r_mbuf_queue = 1; 19596 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19597 } 19598 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 19599 rack->rc_inp->inp_flags2 |= INP_MBUF_ACKCMP; 19600 break; 19601 case TCP_SHARED_CWND_TIME_LIMIT: 19602 RACK_OPTS_INC(tcp_lscwnd); 19603 if (optval) 19604 rack->r_limit_scw = 1; 19605 else 19606 rack->r_limit_scw = 0; 19607 break; 19608 case TCP_RACK_PACE_TO_FILL: 19609 RACK_OPTS_INC(tcp_fillcw); 19610 if (optval == 0) 19611 rack->rc_pace_to_cwnd = 0; 19612 else { 19613 rack->rc_pace_to_cwnd = 1; 19614 if (optval > 1) 19615 rack->r_fill_less_agg = 1; 19616 } 19617 if ((optval >= rack_gp_rtt_maxmul) && 19618 rack_gp_rtt_maxmul && 19619 (optval < 0xf)) { 19620 rack->rc_pace_fill_if_rttin_range = 1; 19621 rack->rtt_limit_mul = optval; 19622 } else { 19623 rack->rc_pace_fill_if_rttin_range = 0; 19624 rack->rtt_limit_mul = 0; 19625 } 19626 break; 19627 case TCP_RACK_NO_PUSH_AT_MAX: 19628 RACK_OPTS_INC(tcp_npush); 19629 if (optval == 0) 19630 rack->r_ctl.rc_no_push_at_mrtt = 0; 19631 else if (optval < 0xff) 19632 rack->r_ctl.rc_no_push_at_mrtt = optval; 19633 else 19634 error = EINVAL; 19635 break; 19636 case TCP_SHARED_CWND_ENABLE: 19637 RACK_OPTS_INC(tcp_rack_scwnd); 19638 if (optval == 0) 19639 rack->rack_enable_scwnd = 0; 19640 else 19641 rack->rack_enable_scwnd = 1; 19642 break; 19643 case TCP_RACK_MBUF_QUEUE: 19644 /* Now do we use the LRO mbuf-queue feature */ 19645 RACK_OPTS_INC(tcp_rack_mbufq); 19646 if (optval || rack->r_use_cmp_ack) 19647 rack->r_mbuf_queue = 1; 19648 else 19649 rack->r_mbuf_queue = 0; 19650 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 19651 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19652 else 19653 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ; 19654 break; 19655 case TCP_RACK_NONRXT_CFG_RATE: 19656 RACK_OPTS_INC(tcp_rack_cfg_rate); 19657 if (optval == 0) 19658 rack->rack_rec_nonrxt_use_cr = 0; 19659 else 19660 rack->rack_rec_nonrxt_use_cr = 1; 19661 break; 19662 case TCP_NO_PRR: 19663 RACK_OPTS_INC(tcp_rack_noprr); 19664 if (optval == 0) 19665 rack->rack_no_prr = 0; 19666 else if (optval == 1) 19667 rack->rack_no_prr = 1; 19668 else if (optval == 2) 19669 rack->no_prr_addback = 1; 19670 else 19671 error = EINVAL; 19672 break; 19673 case TCP_TIMELY_DYN_ADJ: 19674 RACK_OPTS_INC(tcp_timely_dyn); 19675 if (optval == 0) 19676 rack->rc_gp_dyn_mul = 0; 19677 else { 19678 rack->rc_gp_dyn_mul = 1; 19679 if (optval >= 100) { 19680 /* 19681 * If the user sets something 100 or more 19682 * its the gp_ca value. 19683 */ 19684 rack->r_ctl.rack_per_of_gp_ca = optval; 19685 } 19686 } 19687 break; 19688 case TCP_RACK_DO_DETECTION: 19689 RACK_OPTS_INC(tcp_rack_do_detection); 19690 if (optval == 0) 19691 rack->do_detection = 0; 19692 else 19693 rack->do_detection = 1; 19694 break; 19695 case TCP_RACK_TLP_USE: 19696 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) { 19697 error = EINVAL; 19698 break; 19699 } 19700 RACK_OPTS_INC(tcp_tlp_use); 19701 rack->rack_tlp_threshold_use = optval; 19702 break; 19703 case TCP_RACK_TLP_REDUCE: 19704 /* RACK TLP cwnd reduction (bool) */ 19705 RACK_OPTS_INC(tcp_rack_tlp_reduce); 19706 rack->r_ctl.rc_tlp_cwnd_reduce = optval; 19707 break; 19708 /* Pacing related ones */ 19709 case TCP_RACK_PACE_ALWAYS: 19710 /* 19711 * zero is old rack method, 1 is new 19712 * method using a pacing rate. 19713 */ 19714 RACK_OPTS_INC(tcp_rack_pace_always); 19715 if (optval > 0) { 19716 if (rack->rc_always_pace) { 19717 error = EALREADY; 19718 break; 19719 } else if (tcp_can_enable_pacing()) { 19720 rack->rc_always_pace = 1; 19721 if (rack->use_fixed_rate || rack->gp_ready) 19722 rack_set_cc_pacing(rack); 19723 } 19724 else { 19725 error = ENOSPC; 19726 break; 19727 } 19728 } else { 19729 if (rack->rc_always_pace) { 19730 tcp_decrement_paced_conn(); 19731 rack->rc_always_pace = 0; 19732 rack_undo_cc_pacing(rack); 19733 } 19734 } 19735 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 19736 tp->t_inpcb->inp_flags2 |= INP_SUPPORTS_MBUFQ; 19737 else 19738 tp->t_inpcb->inp_flags2 &= ~INP_SUPPORTS_MBUFQ; 19739 /* A rate may be set irate or other, if so set seg size */ 19740 rack_update_seg(rack); 19741 break; 19742 case TCP_BBR_RACK_INIT_RATE: 19743 RACK_OPTS_INC(tcp_initial_rate); 19744 val = optval; 19745 /* Change from kbits per second to bytes per second */ 19746 val *= 1000; 19747 val /= 8; 19748 rack->r_ctl.init_rate = val; 19749 if (rack->rc_init_win != rack_default_init_window) { 19750 uint32_t win, snt; 19751 19752 /* 19753 * Options don't always get applied 19754 * in the order you think. So in order 19755 * to assure we update a cwnd we need 19756 * to check and see if we are still 19757 * where we should raise the cwnd. 19758 */ 19759 win = rc_init_window(rack); 19760 if (SEQ_GT(tp->snd_max, tp->iss)) 19761 snt = tp->snd_max - tp->iss; 19762 else 19763 snt = 0; 19764 if ((snt < win) && 19765 (tp->snd_cwnd < win)) 19766 tp->snd_cwnd = win; 19767 } 19768 if (rack->rc_always_pace) 19769 rack_update_seg(rack); 19770 break; 19771 case TCP_BBR_IWINTSO: 19772 RACK_OPTS_INC(tcp_initial_win); 19773 if (optval && (optval <= 0xff)) { 19774 uint32_t win, snt; 19775 19776 rack->rc_init_win = optval; 19777 win = rc_init_window(rack); 19778 if (SEQ_GT(tp->snd_max, tp->iss)) 19779 snt = tp->snd_max - tp->iss; 19780 else 19781 snt = 0; 19782 if ((snt < win) && 19783 (tp->t_srtt | 19784 #ifdef NETFLIX_PEAKRATE 19785 tp->t_maxpeakrate | 19786 #endif 19787 rack->r_ctl.init_rate)) { 19788 /* 19789 * We are not past the initial window 19790 * and we have some bases for pacing, 19791 * so we need to possibly adjust up 19792 * the cwnd. Note even if we don't set 19793 * the cwnd, its still ok to raise the rc_init_win 19794 * which can be used coming out of idle when we 19795 * would have a rate. 19796 */ 19797 if (tp->snd_cwnd < win) 19798 tp->snd_cwnd = win; 19799 } 19800 if (rack->rc_always_pace) 19801 rack_update_seg(rack); 19802 } else 19803 error = EINVAL; 19804 break; 19805 case TCP_RACK_FORCE_MSEG: 19806 RACK_OPTS_INC(tcp_rack_force_max_seg); 19807 if (optval) 19808 rack->rc_force_max_seg = 1; 19809 else 19810 rack->rc_force_max_seg = 0; 19811 break; 19812 case TCP_RACK_PACE_MAX_SEG: 19813 /* Max segments size in a pace in bytes */ 19814 RACK_OPTS_INC(tcp_rack_max_seg); 19815 rack->rc_user_set_max_segs = optval; 19816 rack_set_pace_segments(tp, rack, __LINE__, NULL); 19817 break; 19818 case TCP_RACK_PACE_RATE_REC: 19819 /* Set the fixed pacing rate in Bytes per second ca */ 19820 RACK_OPTS_INC(tcp_rack_pace_rate_rec); 19821 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 19822 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0) 19823 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 19824 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0) 19825 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 19826 rack->use_fixed_rate = 1; 19827 if (rack->rc_always_pace) 19828 rack_set_cc_pacing(rack); 19829 rack_log_pacing_delay_calc(rack, 19830 rack->r_ctl.rc_fixed_pacing_rate_ss, 19831 rack->r_ctl.rc_fixed_pacing_rate_ca, 19832 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 19833 __LINE__, NULL,0); 19834 break; 19835 19836 case TCP_RACK_PACE_RATE_SS: 19837 /* Set the fixed pacing rate in Bytes per second ca */ 19838 RACK_OPTS_INC(tcp_rack_pace_rate_ss); 19839 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 19840 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0) 19841 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 19842 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0) 19843 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 19844 rack->use_fixed_rate = 1; 19845 if (rack->rc_always_pace) 19846 rack_set_cc_pacing(rack); 19847 rack_log_pacing_delay_calc(rack, 19848 rack->r_ctl.rc_fixed_pacing_rate_ss, 19849 rack->r_ctl.rc_fixed_pacing_rate_ca, 19850 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 19851 __LINE__, NULL, 0); 19852 break; 19853 19854 case TCP_RACK_PACE_RATE_CA: 19855 /* Set the fixed pacing rate in Bytes per second ca */ 19856 RACK_OPTS_INC(tcp_rack_pace_rate_ca); 19857 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 19858 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0) 19859 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 19860 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0) 19861 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 19862 rack->use_fixed_rate = 1; 19863 if (rack->rc_always_pace) 19864 rack_set_cc_pacing(rack); 19865 rack_log_pacing_delay_calc(rack, 19866 rack->r_ctl.rc_fixed_pacing_rate_ss, 19867 rack->r_ctl.rc_fixed_pacing_rate_ca, 19868 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 19869 __LINE__, NULL, 0); 19870 break; 19871 case TCP_RACK_GP_INCREASE_REC: 19872 RACK_OPTS_INC(tcp_gp_inc_rec); 19873 rack->r_ctl.rack_per_of_gp_rec = optval; 19874 rack_log_pacing_delay_calc(rack, 19875 rack->r_ctl.rack_per_of_gp_ss, 19876 rack->r_ctl.rack_per_of_gp_ca, 19877 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 19878 __LINE__, NULL, 0); 19879 break; 19880 case TCP_RACK_GP_INCREASE_CA: 19881 RACK_OPTS_INC(tcp_gp_inc_ca); 19882 ca = optval; 19883 if (ca < 100) { 19884 /* 19885 * We don't allow any reduction 19886 * over the GP b/w. 19887 */ 19888 error = EINVAL; 19889 break; 19890 } 19891 rack->r_ctl.rack_per_of_gp_ca = ca; 19892 rack_log_pacing_delay_calc(rack, 19893 rack->r_ctl.rack_per_of_gp_ss, 19894 rack->r_ctl.rack_per_of_gp_ca, 19895 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 19896 __LINE__, NULL, 0); 19897 break; 19898 case TCP_RACK_GP_INCREASE_SS: 19899 RACK_OPTS_INC(tcp_gp_inc_ss); 19900 ss = optval; 19901 if (ss < 100) { 19902 /* 19903 * We don't allow any reduction 19904 * over the GP b/w. 19905 */ 19906 error = EINVAL; 19907 break; 19908 } 19909 rack->r_ctl.rack_per_of_gp_ss = ss; 19910 rack_log_pacing_delay_calc(rack, 19911 rack->r_ctl.rack_per_of_gp_ss, 19912 rack->r_ctl.rack_per_of_gp_ca, 19913 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 19914 __LINE__, NULL, 0); 19915 break; 19916 case TCP_RACK_RR_CONF: 19917 RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate); 19918 if (optval && optval <= 3) 19919 rack->r_rr_config = optval; 19920 else 19921 rack->r_rr_config = 0; 19922 break; 19923 case TCP_HDWR_RATE_CAP: 19924 RACK_OPTS_INC(tcp_hdwr_rate_cap); 19925 if (optval) { 19926 if (rack->r_rack_hw_rate_caps == 0) 19927 rack->r_rack_hw_rate_caps = 1; 19928 else 19929 error = EALREADY; 19930 } else { 19931 rack->r_rack_hw_rate_caps = 0; 19932 } 19933 break; 19934 case TCP_BBR_HDWR_PACE: 19935 RACK_OPTS_INC(tcp_hdwr_pacing); 19936 if (optval){ 19937 if (rack->rack_hdrw_pacing == 0) { 19938 rack->rack_hdw_pace_ena = 1; 19939 rack->rack_attempt_hdwr_pace = 0; 19940 } else 19941 error = EALREADY; 19942 } else { 19943 rack->rack_hdw_pace_ena = 0; 19944 #ifdef RATELIMIT 19945 if (rack->r_ctl.crte != NULL) { 19946 rack->rack_hdrw_pacing = 0; 19947 rack->rack_attempt_hdwr_pace = 0; 19948 tcp_rel_pacing_rate(rack->r_ctl.crte, tp); 19949 rack->r_ctl.crte = NULL; 19950 } 19951 #endif 19952 } 19953 break; 19954 /* End Pacing related ones */ 19955 case TCP_RACK_PRR_SENDALOT: 19956 /* Allow PRR to send more than one seg */ 19957 RACK_OPTS_INC(tcp_rack_prr_sendalot); 19958 rack->r_ctl.rc_prr_sendalot = optval; 19959 break; 19960 case TCP_RACK_MIN_TO: 19961 /* Minimum time between rack t-o's in ms */ 19962 RACK_OPTS_INC(tcp_rack_min_to); 19963 rack->r_ctl.rc_min_to = optval; 19964 break; 19965 case TCP_RACK_EARLY_SEG: 19966 /* If early recovery max segments */ 19967 RACK_OPTS_INC(tcp_rack_early_seg); 19968 rack->r_ctl.rc_early_recovery_segs = optval; 19969 break; 19970 case TCP_RACK_REORD_THRESH: 19971 /* RACK reorder threshold (shift amount) */ 19972 RACK_OPTS_INC(tcp_rack_reord_thresh); 19973 if ((optval > 0) && (optval < 31)) 19974 rack->r_ctl.rc_reorder_shift = optval; 19975 else 19976 error = EINVAL; 19977 break; 19978 case TCP_RACK_REORD_FADE: 19979 /* Does reordering fade after ms time */ 19980 RACK_OPTS_INC(tcp_rack_reord_fade); 19981 rack->r_ctl.rc_reorder_fade = optval; 19982 break; 19983 case TCP_RACK_TLP_THRESH: 19984 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 19985 RACK_OPTS_INC(tcp_rack_tlp_thresh); 19986 if (optval) 19987 rack->r_ctl.rc_tlp_threshold = optval; 19988 else 19989 error = EINVAL; 19990 break; 19991 case TCP_BBR_USE_RACK_RR: 19992 RACK_OPTS_INC(tcp_rack_rr); 19993 if (optval) 19994 rack->use_rack_rr = 1; 19995 else 19996 rack->use_rack_rr = 0; 19997 break; 19998 case TCP_FAST_RSM_HACK: 19999 RACK_OPTS_INC(tcp_rack_fastrsm_hack); 20000 if (optval) 20001 rack->fast_rsm_hack = 1; 20002 else 20003 rack->fast_rsm_hack = 0; 20004 break; 20005 case TCP_RACK_PKT_DELAY: 20006 /* RACK added ms i.e. rack-rtt + reord + N */ 20007 RACK_OPTS_INC(tcp_rack_pkt_delay); 20008 rack->r_ctl.rc_pkt_delay = optval; 20009 break; 20010 case TCP_DELACK: 20011 RACK_OPTS_INC(tcp_rack_delayed_ack); 20012 if (optval == 0) 20013 tp->t_delayed_ack = 0; 20014 else 20015 tp->t_delayed_ack = 1; 20016 if (tp->t_flags & TF_DELACK) { 20017 tp->t_flags &= ~TF_DELACK; 20018 tp->t_flags |= TF_ACKNOW; 20019 NET_EPOCH_ENTER(et); 20020 rack_output(tp); 20021 NET_EPOCH_EXIT(et); 20022 } 20023 break; 20024 20025 case TCP_BBR_RACK_RTT_USE: 20026 RACK_OPTS_INC(tcp_rack_rtt_use); 20027 if ((optval != USE_RTT_HIGH) && 20028 (optval != USE_RTT_LOW) && 20029 (optval != USE_RTT_AVG)) 20030 error = EINVAL; 20031 else 20032 rack->r_ctl.rc_rate_sample_method = optval; 20033 break; 20034 case TCP_DATA_AFTER_CLOSE: 20035 RACK_OPTS_INC(tcp_data_after_close); 20036 if (optval) 20037 rack->rc_allow_data_af_clo = 1; 20038 else 20039 rack->rc_allow_data_af_clo = 0; 20040 break; 20041 default: 20042 break; 20043 } 20044 #ifdef NETFLIX_STATS 20045 tcp_log_socket_option(tp, sopt_name, optval, error); 20046 #endif 20047 return (error); 20048 } 20049 20050 20051 static void 20052 rack_apply_deferred_options(struct tcp_rack *rack) 20053 { 20054 struct deferred_opt_list *dol, *sdol; 20055 uint32_t s_optval; 20056 20057 TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) { 20058 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next); 20059 /* Disadvantage of deferal is you loose the error return */ 20060 s_optval = (uint32_t)dol->optval; 20061 (void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval); 20062 free(dol, M_TCPDO); 20063 } 20064 } 20065 20066 static void 20067 rack_hw_tls_change(struct tcpcb *tp, int chg) 20068 { 20069 /* 20070 * HW tls state has changed.. fix all 20071 * rsm's in flight. 20072 */ 20073 struct tcp_rack *rack; 20074 struct rack_sendmap *rsm; 20075 20076 rack = (struct tcp_rack *)tp->t_fb_ptr; 20077 RB_FOREACH(rsm, rack_rb_tree_head, &rack->r_ctl.rc_mtree) { 20078 if (chg) 20079 rsm->r_hw_tls = 1; 20080 else 20081 rsm->r_hw_tls = 0; 20082 } 20083 if (chg) 20084 rack->r_ctl.fsb.hw_tls = 1; 20085 else 20086 rack->r_ctl.fsb.hw_tls = 0; 20087 } 20088 20089 static int 20090 rack_pru_options(struct tcpcb *tp, int flags) 20091 { 20092 if (flags & PRUS_OOB) 20093 return (EOPNOTSUPP); 20094 return (0); 20095 } 20096 20097 static struct tcp_function_block __tcp_rack = { 20098 .tfb_tcp_block_name = __XSTRING(STACKNAME), 20099 .tfb_tcp_output = rack_output, 20100 .tfb_do_queued_segments = ctf_do_queued_segments, 20101 .tfb_do_segment_nounlock = rack_do_segment_nounlock, 20102 .tfb_tcp_do_segment = rack_do_segment, 20103 .tfb_tcp_ctloutput = rack_ctloutput, 20104 .tfb_tcp_fb_init = rack_init, 20105 .tfb_tcp_fb_fini = rack_fini, 20106 .tfb_tcp_timer_stop_all = rack_stopall, 20107 .tfb_tcp_timer_activate = rack_timer_activate, 20108 .tfb_tcp_timer_active = rack_timer_active, 20109 .tfb_tcp_timer_stop = rack_timer_stop, 20110 .tfb_tcp_rexmit_tmr = rack_remxt_tmr, 20111 .tfb_tcp_handoff_ok = rack_handoff_ok, 20112 .tfb_tcp_mtu_chg = rack_mtu_change, 20113 .tfb_pru_options = rack_pru_options, 20114 .tfb_hwtls_change = rack_hw_tls_change, 20115 }; 20116 20117 /* 20118 * rack_ctloutput() must drop the inpcb lock before performing copyin on 20119 * socket option arguments. When it re-acquires the lock after the copy, it 20120 * has to revalidate that the connection is still valid for the socket 20121 * option. 20122 */ 20123 static int 20124 rack_set_sockopt(struct socket *so, struct sockopt *sopt, 20125 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 20126 { 20127 uint64_t loptval; 20128 int32_t error = 0, optval; 20129 20130 switch (sopt->sopt_name) { 20131 case TCP_RACK_TLP_REDUCE: /* URL:tlp_reduce */ 20132 /* Pacing related ones */ 20133 case TCP_RACK_PACE_ALWAYS: /* URL:pace_always */ 20134 case TCP_BBR_RACK_INIT_RATE: /* URL:irate */ 20135 case TCP_BBR_IWINTSO: /* URL:tso_iwin */ 20136 case TCP_RACK_PACE_MAX_SEG: /* URL:pace_max_seg */ 20137 case TCP_RACK_FORCE_MSEG: /* URL:force_max_seg */ 20138 case TCP_RACK_PACE_RATE_CA: /* URL:pr_ca */ 20139 case TCP_RACK_PACE_RATE_SS: /* URL:pr_ss*/ 20140 case TCP_RACK_PACE_RATE_REC: /* URL:pr_rec */ 20141 case TCP_RACK_GP_INCREASE_CA: /* URL:gp_inc_ca */ 20142 case TCP_RACK_GP_INCREASE_SS: /* URL:gp_inc_ss */ 20143 case TCP_RACK_GP_INCREASE_REC: /* URL:gp_inc_rec */ 20144 case TCP_RACK_RR_CONF: /* URL:rrr_conf */ 20145 case TCP_BBR_HDWR_PACE: /* URL:hdwrpace */ 20146 case TCP_HDWR_RATE_CAP: /* URL:hdwrcap boolean */ 20147 case TCP_PACING_RATE_CAP: /* URL:cap -- used by side-channel */ 20148 case TCP_HDWR_UP_ONLY: /* URL:uponly -- hardware pacing boolean */ 20149 /* End pacing related */ 20150 case TCP_FAST_RSM_HACK: /* URL:frsm_hack */ 20151 case TCP_DELACK: /* URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */ 20152 case TCP_RACK_PRR_SENDALOT: /* URL:prr_sendalot */ 20153 case TCP_RACK_MIN_TO: /* URL:min_to */ 20154 case TCP_RACK_EARLY_SEG: /* URL:early_seg */ 20155 case TCP_RACK_REORD_THRESH: /* URL:reord_thresh */ 20156 case TCP_RACK_REORD_FADE: /* URL:reord_fade */ 20157 case TCP_RACK_TLP_THRESH: /* URL:tlp_thresh */ 20158 case TCP_RACK_PKT_DELAY: /* URL:pkt_delay */ 20159 case TCP_RACK_TLP_USE: /* URL:tlp_use */ 20160 case TCP_BBR_RACK_RTT_USE: /* URL:rttuse */ 20161 case TCP_BBR_USE_RACK_RR: /* URL:rackrr */ 20162 case TCP_RACK_DO_DETECTION: /* URL:detect */ 20163 case TCP_NO_PRR: /* URL:noprr */ 20164 case TCP_TIMELY_DYN_ADJ: /* URL:dynamic */ 20165 case TCP_DATA_AFTER_CLOSE: /* no URL */ 20166 case TCP_RACK_NONRXT_CFG_RATE: /* URL:nonrxtcr */ 20167 case TCP_SHARED_CWND_ENABLE: /* URL:scwnd */ 20168 case TCP_RACK_MBUF_QUEUE: /* URL:mqueue */ 20169 case TCP_RACK_NO_PUSH_AT_MAX: /* URL:npush */ 20170 case TCP_RACK_PACE_TO_FILL: /* URL:fillcw */ 20171 case TCP_SHARED_CWND_TIME_LIMIT: /* URL:lscwnd */ 20172 case TCP_RACK_PROFILE: /* URL:profile */ 20173 case TCP_USE_CMP_ACKS: /* URL:cmpack */ 20174 case TCP_RACK_ABC_VAL: /* URL:labc */ 20175 case TCP_REC_ABC_VAL: /* URL:reclabc */ 20176 case TCP_RACK_MEASURE_CNT: /* URL:measurecnt */ 20177 case TCP_DEFER_OPTIONS: /* URL:defer */ 20178 case TCP_RACK_DSACK_OPT: /* URL:dsack */ 20179 case TCP_RACK_PACING_BETA: /* URL:pacing_beta */ 20180 case TCP_RACK_PACING_BETA_ECN: /* URL:pacing_beta_ecn */ 20181 case TCP_RACK_TIMER_SLOP: /* URL:timer_slop */ 20182 break; 20183 default: 20184 /* Filter off all unknown options to the base stack */ 20185 return (tcp_default_ctloutput(so, sopt, inp, tp)); 20186 break; 20187 } 20188 INP_WUNLOCK(inp); 20189 if (sopt->sopt_name == TCP_PACING_RATE_CAP) { 20190 error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval)); 20191 /* 20192 * We truncate it down to 32 bits for the socket-option trace this 20193 * means rates > 34Gbps won't show right, but thats probably ok. 20194 */ 20195 optval = (uint32_t)loptval; 20196 } else { 20197 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 20198 /* Save it in 64 bit form too */ 20199 loptval = optval; 20200 } 20201 if (error) 20202 return (error); 20203 INP_WLOCK(inp); 20204 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 20205 INP_WUNLOCK(inp); 20206 return (ECONNRESET); 20207 } 20208 if (tp->t_fb != &__tcp_rack) { 20209 INP_WUNLOCK(inp); 20210 return (ENOPROTOOPT); 20211 } 20212 if (rack->defer_options && (rack->gp_ready == 0) && 20213 (sopt->sopt_name != TCP_DEFER_OPTIONS) && 20214 (sopt->sopt_name != TCP_RACK_PACING_BETA) && 20215 (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) && 20216 (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) { 20217 /* Options are beind deferred */ 20218 if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) { 20219 INP_WUNLOCK(inp); 20220 return (0); 20221 } else { 20222 /* No memory to defer, fail */ 20223 INP_WUNLOCK(inp); 20224 return (ENOMEM); 20225 } 20226 } 20227 error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval); 20228 INP_WUNLOCK(inp); 20229 return (error); 20230 } 20231 20232 static void 20233 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti) 20234 { 20235 20236 INP_WLOCK_ASSERT(tp->t_inpcb); 20237 bzero(ti, sizeof(*ti)); 20238 20239 ti->tcpi_state = tp->t_state; 20240 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 20241 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 20242 if (tp->t_flags & TF_SACK_PERMIT) 20243 ti->tcpi_options |= TCPI_OPT_SACK; 20244 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 20245 ti->tcpi_options |= TCPI_OPT_WSCALE; 20246 ti->tcpi_snd_wscale = tp->snd_scale; 20247 ti->tcpi_rcv_wscale = tp->rcv_scale; 20248 } 20249 if (tp->t_flags2 & TF2_ECN_PERMIT) 20250 ti->tcpi_options |= TCPI_OPT_ECN; 20251 if (tp->t_flags & TF_FASTOPEN) 20252 ti->tcpi_options |= TCPI_OPT_TFO; 20253 /* still kept in ticks is t_rcvtime */ 20254 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 20255 /* Since we hold everything in precise useconds this is easy */ 20256 ti->tcpi_rtt = tp->t_srtt; 20257 ti->tcpi_rttvar = tp->t_rttvar; 20258 ti->tcpi_rto = tp->t_rxtcur; 20259 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 20260 ti->tcpi_snd_cwnd = tp->snd_cwnd; 20261 /* 20262 * FreeBSD-specific extension fields for tcp_info. 20263 */ 20264 ti->tcpi_rcv_space = tp->rcv_wnd; 20265 ti->tcpi_rcv_nxt = tp->rcv_nxt; 20266 ti->tcpi_snd_wnd = tp->snd_wnd; 20267 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 20268 ti->tcpi_snd_nxt = tp->snd_nxt; 20269 ti->tcpi_snd_mss = tp->t_maxseg; 20270 ti->tcpi_rcv_mss = tp->t_maxseg; 20271 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 20272 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 20273 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 20274 #ifdef NETFLIX_STATS 20275 ti->tcpi_total_tlp = tp->t_sndtlppack; 20276 ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte; 20277 memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo)); 20278 #endif 20279 #ifdef TCP_OFFLOAD 20280 if (tp->t_flags & TF_TOE) { 20281 ti->tcpi_options |= TCPI_OPT_TOE; 20282 tcp_offload_tcp_info(tp, ti); 20283 } 20284 #endif 20285 } 20286 20287 static int 20288 rack_get_sockopt(struct socket *so, struct sockopt *sopt, 20289 struct inpcb *inp, struct tcpcb *tp, struct tcp_rack *rack) 20290 { 20291 int32_t error, optval; 20292 uint64_t val, loptval; 20293 struct tcp_info ti; 20294 /* 20295 * Because all our options are either boolean or an int, we can just 20296 * pull everything into optval and then unlock and copy. If we ever 20297 * add a option that is not a int, then this will have quite an 20298 * impact to this routine. 20299 */ 20300 error = 0; 20301 switch (sopt->sopt_name) { 20302 case TCP_INFO: 20303 /* First get the info filled */ 20304 rack_fill_info(tp, &ti); 20305 /* Fix up the rtt related fields if needed */ 20306 INP_WUNLOCK(inp); 20307 error = sooptcopyout(sopt, &ti, sizeof ti); 20308 return (error); 20309 /* 20310 * Beta is the congestion control value for NewReno that influences how 20311 * much of a backoff happens when loss is detected. It is normally set 20312 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value 20313 * when you exit recovery. 20314 */ 20315 case TCP_RACK_PACING_BETA: 20316 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) 20317 error = EINVAL; 20318 else if (rack->rc_pacing_cc_set == 0) 20319 optval = rack->r_ctl.rc_saved_beta.beta; 20320 else { 20321 /* 20322 * Reach out into the CC data and report back what 20323 * I have previously set. Yeah it looks hackish but 20324 * we don't want to report the saved values. 20325 */ 20326 if (tp->ccv->cc_data) 20327 optval = ((struct newreno *)tp->ccv->cc_data)->beta; 20328 else 20329 error = EINVAL; 20330 } 20331 break; 20332 /* 20333 * Beta_ecn is the congestion control value for NewReno that influences how 20334 * much of a backoff happens when a ECN mark is detected. It is normally set 20335 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when 20336 * you exit recovery. Note that classic ECN has a beta of 50, it is only 20337 * ABE Ecn that uses this "less" value, but we do too with pacing :) 20338 */ 20339 20340 case TCP_RACK_PACING_BETA_ECN: 20341 if (strcmp(tp->cc_algo->name, CCALGONAME_NEWRENO) != 0) 20342 error = EINVAL; 20343 else if (rack->rc_pacing_cc_set == 0) 20344 optval = rack->r_ctl.rc_saved_beta.beta_ecn; 20345 else { 20346 /* 20347 * Reach out into the CC data and report back what 20348 * I have previously set. Yeah it looks hackish but 20349 * we don't want to report the saved values. 20350 */ 20351 if (tp->ccv->cc_data) 20352 optval = ((struct newreno *)tp->ccv->cc_data)->beta_ecn; 20353 else 20354 error = EINVAL; 20355 } 20356 break; 20357 case TCP_RACK_DSACK_OPT: 20358 optval = 0; 20359 if (rack->rc_rack_tmr_std_based) { 20360 optval |= 1; 20361 } 20362 if (rack->rc_rack_use_dsack) { 20363 optval |= 2; 20364 } 20365 break; 20366 case TCP_FAST_RSM_HACK: 20367 optval = rack->fast_rsm_hack; 20368 break; 20369 case TCP_DEFER_OPTIONS: 20370 optval = rack->defer_options; 20371 break; 20372 case TCP_RACK_MEASURE_CNT: 20373 optval = rack->r_ctl.req_measurements; 20374 break; 20375 case TCP_REC_ABC_VAL: 20376 optval = rack->r_use_labc_for_rec; 20377 break; 20378 case TCP_RACK_ABC_VAL: 20379 optval = rack->rc_labc; 20380 break; 20381 case TCP_HDWR_UP_ONLY: 20382 optval= rack->r_up_only; 20383 break; 20384 case TCP_PACING_RATE_CAP: 20385 loptval = rack->r_ctl.bw_rate_cap; 20386 break; 20387 case TCP_RACK_PROFILE: 20388 /* You cannot retrieve a profile, its write only */ 20389 error = EINVAL; 20390 break; 20391 case TCP_USE_CMP_ACKS: 20392 optval = rack->r_use_cmp_ack; 20393 break; 20394 case TCP_RACK_PACE_TO_FILL: 20395 optval = rack->rc_pace_to_cwnd; 20396 if (optval && rack->r_fill_less_agg) 20397 optval++; 20398 break; 20399 case TCP_RACK_NO_PUSH_AT_MAX: 20400 optval = rack->r_ctl.rc_no_push_at_mrtt; 20401 break; 20402 case TCP_SHARED_CWND_ENABLE: 20403 optval = rack->rack_enable_scwnd; 20404 break; 20405 case TCP_RACK_NONRXT_CFG_RATE: 20406 optval = rack->rack_rec_nonrxt_use_cr; 20407 break; 20408 case TCP_NO_PRR: 20409 if (rack->rack_no_prr == 1) 20410 optval = 1; 20411 else if (rack->no_prr_addback == 1) 20412 optval = 2; 20413 else 20414 optval = 0; 20415 break; 20416 case TCP_RACK_DO_DETECTION: 20417 optval = rack->do_detection; 20418 break; 20419 case TCP_RACK_MBUF_QUEUE: 20420 /* Now do we use the LRO mbuf-queue feature */ 20421 optval = rack->r_mbuf_queue; 20422 break; 20423 case TCP_TIMELY_DYN_ADJ: 20424 optval = rack->rc_gp_dyn_mul; 20425 break; 20426 case TCP_BBR_IWINTSO: 20427 optval = rack->rc_init_win; 20428 break; 20429 case TCP_RACK_TLP_REDUCE: 20430 /* RACK TLP cwnd reduction (bool) */ 20431 optval = rack->r_ctl.rc_tlp_cwnd_reduce; 20432 break; 20433 case TCP_BBR_RACK_INIT_RATE: 20434 val = rack->r_ctl.init_rate; 20435 /* convert to kbits per sec */ 20436 val *= 8; 20437 val /= 1000; 20438 optval = (uint32_t)val; 20439 break; 20440 case TCP_RACK_FORCE_MSEG: 20441 optval = rack->rc_force_max_seg; 20442 break; 20443 case TCP_RACK_PACE_MAX_SEG: 20444 /* Max segments in a pace */ 20445 optval = rack->rc_user_set_max_segs; 20446 break; 20447 case TCP_RACK_PACE_ALWAYS: 20448 /* Use the always pace method */ 20449 optval = rack->rc_always_pace; 20450 break; 20451 case TCP_RACK_PRR_SENDALOT: 20452 /* Allow PRR to send more than one seg */ 20453 optval = rack->r_ctl.rc_prr_sendalot; 20454 break; 20455 case TCP_RACK_MIN_TO: 20456 /* Minimum time between rack t-o's in ms */ 20457 optval = rack->r_ctl.rc_min_to; 20458 break; 20459 case TCP_RACK_EARLY_SEG: 20460 /* If early recovery max segments */ 20461 optval = rack->r_ctl.rc_early_recovery_segs; 20462 break; 20463 case TCP_RACK_REORD_THRESH: 20464 /* RACK reorder threshold (shift amount) */ 20465 optval = rack->r_ctl.rc_reorder_shift; 20466 break; 20467 case TCP_RACK_REORD_FADE: 20468 /* Does reordering fade after ms time */ 20469 optval = rack->r_ctl.rc_reorder_fade; 20470 break; 20471 case TCP_BBR_USE_RACK_RR: 20472 /* Do we use the rack cheat for rxt */ 20473 optval = rack->use_rack_rr; 20474 break; 20475 case TCP_RACK_RR_CONF: 20476 optval = rack->r_rr_config; 20477 break; 20478 case TCP_HDWR_RATE_CAP: 20479 optval = rack->r_rack_hw_rate_caps; 20480 break; 20481 case TCP_BBR_HDWR_PACE: 20482 optval = rack->rack_hdw_pace_ena; 20483 break; 20484 case TCP_RACK_TLP_THRESH: 20485 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 20486 optval = rack->r_ctl.rc_tlp_threshold; 20487 break; 20488 case TCP_RACK_PKT_DELAY: 20489 /* RACK added ms i.e. rack-rtt + reord + N */ 20490 optval = rack->r_ctl.rc_pkt_delay; 20491 break; 20492 case TCP_RACK_TLP_USE: 20493 optval = rack->rack_tlp_threshold_use; 20494 break; 20495 case TCP_RACK_PACE_RATE_CA: 20496 optval = rack->r_ctl.rc_fixed_pacing_rate_ca; 20497 break; 20498 case TCP_RACK_PACE_RATE_SS: 20499 optval = rack->r_ctl.rc_fixed_pacing_rate_ss; 20500 break; 20501 case TCP_RACK_PACE_RATE_REC: 20502 optval = rack->r_ctl.rc_fixed_pacing_rate_rec; 20503 break; 20504 case TCP_RACK_GP_INCREASE_SS: 20505 optval = rack->r_ctl.rack_per_of_gp_ca; 20506 break; 20507 case TCP_RACK_GP_INCREASE_CA: 20508 optval = rack->r_ctl.rack_per_of_gp_ss; 20509 break; 20510 case TCP_BBR_RACK_RTT_USE: 20511 optval = rack->r_ctl.rc_rate_sample_method; 20512 break; 20513 case TCP_DELACK: 20514 optval = tp->t_delayed_ack; 20515 break; 20516 case TCP_DATA_AFTER_CLOSE: 20517 optval = rack->rc_allow_data_af_clo; 20518 break; 20519 case TCP_SHARED_CWND_TIME_LIMIT: 20520 optval = rack->r_limit_scw; 20521 break; 20522 case TCP_RACK_TIMER_SLOP: 20523 optval = rack->r_ctl.timer_slop; 20524 break; 20525 default: 20526 return (tcp_default_ctloutput(so, sopt, inp, tp)); 20527 break; 20528 } 20529 INP_WUNLOCK(inp); 20530 if (error == 0) { 20531 if (TCP_PACING_RATE_CAP) 20532 error = sooptcopyout(sopt, &loptval, sizeof loptval); 20533 else 20534 error = sooptcopyout(sopt, &optval, sizeof optval); 20535 } 20536 return (error); 20537 } 20538 20539 static int 20540 rack_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 20541 { 20542 int32_t error = EINVAL; 20543 struct tcp_rack *rack; 20544 20545 rack = (struct tcp_rack *)tp->t_fb_ptr; 20546 if (rack == NULL) { 20547 /* Huh? */ 20548 goto out; 20549 } 20550 if (sopt->sopt_dir == SOPT_SET) { 20551 return (rack_set_sockopt(so, sopt, inp, tp, rack)); 20552 } else if (sopt->sopt_dir == SOPT_GET) { 20553 return (rack_get_sockopt(so, sopt, inp, tp, rack)); 20554 } 20555 out: 20556 INP_WUNLOCK(inp); 20557 return (error); 20558 } 20559 20560 static const char *rack_stack_names[] = { 20561 __XSTRING(STACKNAME), 20562 #ifdef STACKALIAS 20563 __XSTRING(STACKALIAS), 20564 #endif 20565 }; 20566 20567 static int 20568 rack_ctor(void *mem, int32_t size, void *arg, int32_t how) 20569 { 20570 memset(mem, 0, size); 20571 return (0); 20572 } 20573 20574 static void 20575 rack_dtor(void *mem, int32_t size, void *arg) 20576 { 20577 20578 } 20579 20580 static bool rack_mod_inited = false; 20581 20582 static int 20583 tcp_addrack(module_t mod, int32_t type, void *data) 20584 { 20585 int32_t err = 0; 20586 int num_stacks; 20587 20588 switch (type) { 20589 case MOD_LOAD: 20590 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 20591 sizeof(struct rack_sendmap), 20592 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 20593 20594 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 20595 sizeof(struct tcp_rack), 20596 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 20597 20598 sysctl_ctx_init(&rack_sysctl_ctx); 20599 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 20600 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 20601 OID_AUTO, 20602 #ifdef STACKALIAS 20603 __XSTRING(STACKALIAS), 20604 #else 20605 __XSTRING(STACKNAME), 20606 #endif 20607 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 20608 ""); 20609 if (rack_sysctl_root == NULL) { 20610 printf("Failed to add sysctl node\n"); 20611 err = EFAULT; 20612 goto free_uma; 20613 } 20614 rack_init_sysctls(); 20615 num_stacks = nitems(rack_stack_names); 20616 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK, 20617 rack_stack_names, &num_stacks); 20618 if (err) { 20619 printf("Failed to register %s stack name for " 20620 "%s module\n", rack_stack_names[num_stacks], 20621 __XSTRING(MODNAME)); 20622 sysctl_ctx_free(&rack_sysctl_ctx); 20623 free_uma: 20624 uma_zdestroy(rack_zone); 20625 uma_zdestroy(rack_pcb_zone); 20626 rack_counter_destroy(); 20627 printf("Failed to register rack module -- err:%d\n", err); 20628 return (err); 20629 } 20630 tcp_lro_reg_mbufq(); 20631 rack_mod_inited = true; 20632 break; 20633 case MOD_QUIESCE: 20634 err = deregister_tcp_functions(&__tcp_rack, true, false); 20635 break; 20636 case MOD_UNLOAD: 20637 err = deregister_tcp_functions(&__tcp_rack, false, true); 20638 if (err == EBUSY) 20639 break; 20640 if (rack_mod_inited) { 20641 uma_zdestroy(rack_zone); 20642 uma_zdestroy(rack_pcb_zone); 20643 sysctl_ctx_free(&rack_sysctl_ctx); 20644 rack_counter_destroy(); 20645 rack_mod_inited = false; 20646 } 20647 tcp_lro_dereg_mbufq(); 20648 err = 0; 20649 break; 20650 default: 20651 return (EOPNOTSUPP); 20652 } 20653 return (err); 20654 } 20655 20656 static moduledata_t tcp_rack = { 20657 .name = __XSTRING(MODNAME), 20658 .evhand = tcp_addrack, 20659 .priv = 0 20660 }; 20661 20662 MODULE_VERSION(MODNAME, 1); 20663 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 20664 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 20665