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 #include "opt_inet.h" 29 #include "opt_inet6.h" 30 #include "opt_ipsec.h" 31 #include "opt_ratelimit.h" 32 #include "opt_kern_tls.h" 33 #if defined(INET) || defined(INET6) 34 #include <sys/param.h> 35 #include <sys/arb.h> 36 #include <sys/module.h> 37 #include <sys/kernel.h> 38 #ifdef TCP_HHOOK 39 #include <sys/hhook.h> 40 #endif 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/mbuf.h> 46 #include <sys/proc.h> /* for proc0 declaration */ 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 #ifdef STATS 52 #include <sys/qmath.h> 53 #include <sys/tree.h> 54 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 55 #else 56 #include <sys/tree.h> 57 #endif 58 #include <sys/refcount.h> 59 #include <sys/queue.h> 60 #include <sys/tim_filter.h> 61 #include <sys/smp.h> 62 #include <sys/kthread.h> 63 #include <sys/kern_prefetch.h> 64 #include <sys/protosw.h> 65 #ifdef TCP_ACCOUNTING 66 #include <sys/sched.h> 67 #include <machine/cpu.h> 68 #endif 69 #include <vm/uma.h> 70 71 #include <net/route.h> 72 #include <net/route/nhop.h> 73 #include <net/vnet.h> 74 75 #define TCPSTATES /* for logging */ 76 77 #include <netinet/in.h> 78 #include <netinet/in_kdtrace.h> 79 #include <netinet/in_pcb.h> 80 #include <netinet/ip.h> 81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 83 #include <netinet/ip_var.h> 84 #include <netinet/ip6.h> 85 #include <netinet6/in6_pcb.h> 86 #include <netinet6/ip6_var.h> 87 #include <netinet/tcp.h> 88 #define TCPOUTFLAGS 89 #include <netinet/tcp_fsm.h> 90 #include <netinet/tcp_seq.h> 91 #include <netinet/tcp_timer.h> 92 #include <netinet/tcp_var.h> 93 #include <netinet/tcp_log_buf.h> 94 #include <netinet/tcp_syncache.h> 95 #include <netinet/tcp_hpts.h> 96 #include <netinet/tcp_ratelimit.h> 97 #include <netinet/tcp_accounting.h> 98 #include <netinet/tcpip.h> 99 #include <netinet/cc/cc.h> 100 #include <netinet/cc/cc_newreno.h> 101 #include <netinet/tcp_fastopen.h> 102 #include <netinet/tcp_lro.h> 103 #ifdef NETFLIX_SHARED_CWND 104 #include <netinet/tcp_shared_cwnd.h> 105 #endif 106 #ifdef TCP_OFFLOAD 107 #include <netinet/tcp_offload.h> 108 #endif 109 #ifdef INET6 110 #include <netinet6/tcp6_var.h> 111 #endif 112 #include <netinet/tcp_ecn.h> 113 114 #include <netipsec/ipsec_support.h> 115 116 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 117 #include <netipsec/ipsec.h> 118 #include <netipsec/ipsec6.h> 119 #endif /* IPSEC */ 120 121 #include <netinet/udp.h> 122 #include <netinet/udp_var.h> 123 #include <machine/in_cksum.h> 124 125 #ifdef MAC 126 #include <security/mac/mac_framework.h> 127 #endif 128 #include "sack_filter.h" 129 #include "tcp_rack.h" 130 #include "tailq_hash.h" 131 #include "rack_bbr_common.h" 132 133 uma_zone_t rack_zone; 134 uma_zone_t rack_pcb_zone; 135 136 #ifndef TICKS2SBT 137 #define TICKS2SBT(__t) (tick_sbt * ((sbintime_t)(__t))) 138 #endif 139 140 VNET_DECLARE(uint32_t, newreno_beta); 141 VNET_DECLARE(uint32_t, newreno_beta_ecn); 142 #define V_newreno_beta VNET(newreno_beta) 143 #define V_newreno_beta_ecn VNET(newreno_beta_ecn) 144 145 #define M_TCPFSB __CONCAT(M_TCPFSB, STACKNAME) 146 #define M_TCPDO __CONCAT(M_TCPDO, STACKNAME) 147 148 MALLOC_DEFINE(M_TCPFSB, "tcp_fsb_" __XSTRING(STACKNAME), "TCP fast send block"); 149 MALLOC_DEFINE(M_TCPDO, "tcp_do_" __XSTRING(STACKNAME), "TCP deferred options"); 150 MALLOC_DEFINE(M_TCPPCM, "tcp_pcm_" __XSTRING(STACKNAME), "TCP PCM measurement information"); 151 152 struct sysctl_ctx_list rack_sysctl_ctx; 153 struct sysctl_oid *rack_sysctl_root; 154 155 #define CUM_ACKED 1 156 #define SACKED 2 157 158 /* 159 * The RACK module incorporates a number of 160 * TCP ideas that have been put out into the IETF 161 * over the last few years: 162 * - Matt Mathis's Rate Halving which slowly drops 163 * the congestion window so that the ack clock can 164 * be maintained during a recovery. 165 * - Yuchung Cheng's RACK TCP (for which its named) that 166 * will stop us using the number of dup acks and instead 167 * use time as the gage of when we retransmit. 168 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 169 * of Dukkipati et.al. 170 * RACK depends on SACK, so if an endpoint arrives that 171 * cannot do SACK the state machine below will shuttle the 172 * connection back to using the "default" TCP stack that is 173 * in FreeBSD. 174 * 175 * To implement RACK the original TCP stack was first decomposed 176 * into a functional state machine with individual states 177 * for each of the possible TCP connection states. The do_segment 178 * functions role in life is to mandate the connection supports SACK 179 * initially and then assure that the RACK state matches the conenction 180 * state before calling the states do_segment function. Each 181 * state is simplified due to the fact that the original do_segment 182 * has been decomposed and we *know* what state we are in (no 183 * switches on the state) and all tests for SACK are gone. This 184 * greatly simplifies what each state does. 185 * 186 * TCP output is also over-written with a new version since it 187 * must maintain the new rack scoreboard. 188 * 189 */ 190 static int32_t rack_tlp_thresh = 1; 191 static int32_t rack_tlp_limit = 2; /* No more than 2 TLPs w-out new data */ 192 static int32_t rack_tlp_use_greater = 1; 193 static int32_t rack_reorder_thresh = 2; 194 static int32_t rack_reorder_fade = 60000000; /* 0 - never fade, def 60,000,000 195 * - 60 seconds */ 196 static uint16_t rack_policer_rxt_thresh= 0; /* 499 = 49.9%, 0 is off */ 197 static uint8_t rack_policer_avg_thresh = 0; /* 3.2 */ 198 static uint8_t rack_policer_med_thresh = 0; /* 1 - 16 */ 199 static uint16_t rack_policer_bucket_reserve = 20; /* How much % is reserved in the bucket */ 200 static uint64_t rack_pol_min_bw = 125000; /* 1mbps in Bytes per sec */ 201 static uint32_t rack_policer_data_thresh = 64000; /* 64,000 bytes must be sent before we engage */ 202 static uint32_t rack_policing_do_bw_comp = 1; 203 static uint32_t rack_pcm_every_n_rounds = 100; 204 static uint32_t rack_pcm_blast = 0; 205 static uint32_t rack_pcm_is_enabled = 1; 206 static uint8_t rack_req_del_mss = 18; /* How many segments need to be sent in a recovery episode to do policer_detection */ 207 static uint8_t rack_ssthresh_rest_rto_rec = 0; /* Do we restore ssthresh when we have rec -> rto -> rec */ 208 209 static uint32_t rack_gp_gain_req = 1200; /* Amount percent wise required to gain to record a round has "gaining" */ 210 static uint32_t rack_rnd_cnt_req = 0x10005; /* Default number of rounds if we are below rack_gp_gain_req where we exit ss */ 211 212 213 static int32_t rack_rxt_scoreboard_clear_thresh = 2; 214 static int32_t rack_dnd_default = 0; /* For rr_conf = 3, what is the default for dnd */ 215 static int32_t rack_rxt_controls = 0; 216 static int32_t rack_fill_cw_state = 0; 217 static uint8_t rack_req_measurements = 1; 218 /* Attack threshold detections */ 219 static uint32_t rack_highest_sack_thresh_seen = 0; 220 static uint32_t rack_highest_move_thresh_seen = 0; 221 static uint32_t rack_merge_out_sacks_on_attack = 0; 222 static int32_t rack_enable_hw_pacing = 0; /* Due to CCSP keep it off by default */ 223 static int32_t rack_hw_pace_extra_slots = 0; /* 2 extra MSS time betweens */ 224 static int32_t rack_hw_rate_caps = 0; /* 1; */ 225 static int32_t rack_hw_rate_cap_per = 0; /* 0 -- off */ 226 static int32_t rack_hw_rate_min = 0; /* 1500000;*/ 227 static int32_t rack_hw_rate_to_low = 0; /* 1200000; */ 228 static int32_t rack_hw_up_only = 0; 229 static int32_t rack_stats_gets_ms_rtt = 1; 230 static int32_t rack_prr_addbackmax = 2; 231 static int32_t rack_do_hystart = 0; 232 static int32_t rack_apply_rtt_with_reduced_conf = 0; 233 static int32_t rack_hibeta_setting = 0; 234 static int32_t rack_default_pacing_divisor = 250; 235 static uint16_t rack_pacing_min_seg = 0; 236 static int32_t rack_timely_off = 0; 237 238 static uint32_t sad_seg_size_per = 800; /* 80.0 % */ 239 static int32_t rack_pkt_delay = 1000; 240 static int32_t rack_send_a_lot_in_prr = 1; 241 static int32_t rack_min_to = 1000; /* Number of microsecond min timeout */ 242 static int32_t rack_verbose_logging = 0; 243 static int32_t rack_ignore_data_after_close = 1; 244 static int32_t rack_enable_shared_cwnd = 1; 245 static int32_t rack_use_cmp_acks = 1; 246 static int32_t rack_use_fsb = 1; 247 static int32_t rack_use_rfo = 1; 248 static int32_t rack_use_rsm_rfo = 1; 249 static int32_t rack_max_abc_post_recovery = 2; 250 static int32_t rack_client_low_buf = 0; 251 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 */ 252 static int32_t rack_bw_multipler = 0; /* Limit on fill cw's jump up to be this x gp_est */ 253 #ifdef TCP_ACCOUNTING 254 static int32_t rack_tcp_accounting = 0; 255 #endif 256 static int32_t rack_limits_scwnd = 1; 257 static int32_t rack_enable_mqueue_for_nonpaced = 0; 258 static int32_t rack_hybrid_allow_set_maxseg = 0; 259 static int32_t rack_disable_prr = 0; 260 static int32_t use_rack_rr = 1; 261 static int32_t rack_non_rxt_use_cr = 0; /* does a non-rxt in recovery use the configured rate (ss/ca)? */ 262 static int32_t rack_persist_min = 250000; /* 250usec */ 263 static int32_t rack_persist_max = 2000000; /* 2 Second in usec's */ 264 static int32_t rack_honors_hpts_min_to = 1; /* Do we honor the hpts minimum time out for pacing timers */ 265 static uint32_t rack_max_reduce = 10; /* Percent we can reduce slot by */ 266 static int32_t rack_sack_not_required = 1; /* set to one to allow non-sack to use rack */ 267 static int32_t rack_limit_time_with_srtt = 0; 268 static int32_t rack_autosndbuf_inc = 20; /* In percentage form */ 269 static int32_t rack_enobuf_hw_boost_mult = 0; /* How many times the hw rate we boost slot using time_between */ 270 static int32_t rack_enobuf_hw_max = 12000; /* 12 ms in usecs */ 271 static int32_t rack_enobuf_hw_min = 10000; /* 10 ms in usecs */ 272 static int32_t rack_hw_rwnd_factor = 2; /* How many max_segs the rwnd must be before we hold off sending */ 273 static int32_t rack_hw_check_queue = 0; /* Do we always pre-check queue depth of a hw queue */ 274 static int32_t rack_full_buffer_discount = 10; 275 /* 276 * Currently regular tcp has a rto_min of 30ms 277 * the backoff goes 12 times so that ends up 278 * being a total of 122.850 seconds before a 279 * connection is killed. 280 */ 281 static uint32_t rack_def_data_window = 20; 282 static uint32_t rack_goal_bdp = 2; 283 static uint32_t rack_min_srtts = 1; 284 static uint32_t rack_min_measure_usec = 0; 285 static int32_t rack_tlp_min = 10000; /* 10ms */ 286 static int32_t rack_rto_min = 30000; /* 30,000 usec same as main freebsd */ 287 static int32_t rack_rto_max = 4000000; /* 4 seconds in usec's */ 288 static const int32_t rack_free_cache = 2; 289 static int32_t rack_hptsi_segments = 40; 290 static int32_t rack_rate_sample_method = USE_RTT_LOW; 291 static int32_t rack_pace_every_seg = 0; 292 static int32_t rack_delayed_ack_time = 40000; /* 40ms in usecs */ 293 static int32_t rack_slot_reduction = 4; 294 static int32_t rack_wma_divisor = 8; /* For WMA calculation */ 295 static int32_t rack_cwnd_block_ends_measure = 0; 296 static int32_t rack_rwnd_block_ends_measure = 0; 297 static int32_t rack_def_profile = 0; 298 299 static int32_t rack_lower_cwnd_at_tlp = 0; 300 static int32_t rack_always_send_oldest = 0; 301 static int32_t rack_tlp_threshold_use = TLP_USE_TWO_ONE; 302 303 static uint16_t rack_per_of_gp_ss = 250; /* 250 % slow-start */ 304 static uint16_t rack_per_of_gp_ca = 200; /* 200 % congestion-avoidance */ 305 static uint16_t rack_per_of_gp_rec = 200; /* 200 % of bw */ 306 307 /* Probertt */ 308 static uint16_t rack_per_of_gp_probertt = 60; /* 60% of bw */ 309 static uint16_t rack_per_of_gp_lowthresh = 40; /* 40% is bottom */ 310 static uint16_t rack_per_of_gp_probertt_reduce = 10; /* 10% reduction */ 311 static uint16_t rack_atexit_prtt_hbp = 130; /* Clamp to 130% on exit prtt if highly buffered path */ 312 static uint16_t rack_atexit_prtt = 130; /* Clamp to 100% on exit prtt if non highly buffered path */ 313 314 static uint32_t rack_max_drain_wait = 2; /* How man gp srtt's before we give up draining */ 315 static uint32_t rack_must_drain = 1; /* How many GP srtt's we *must* wait */ 316 static uint32_t rack_probertt_use_min_rtt_entry = 1; /* Use the min to calculate the goal else gp_srtt */ 317 static uint32_t rack_probertt_use_min_rtt_exit = 0; 318 static uint32_t rack_probe_rtt_sets_cwnd = 0; 319 static uint32_t rack_probe_rtt_safety_val = 2000000; /* No more than 2 sec in probe-rtt */ 320 static uint32_t rack_time_between_probertt = 9600000; /* 9.6 sec in usecs */ 321 static uint32_t rack_probertt_gpsrtt_cnt_mul = 0; /* How many srtt periods does probe-rtt last top fraction */ 322 static uint32_t rack_probertt_gpsrtt_cnt_div = 0; /* How many srtt periods does probe-rtt last bottom fraction */ 323 static uint32_t rack_min_probertt_hold = 40000; /* Equal to delayed ack time */ 324 static uint32_t rack_probertt_filter_life = 10000000; 325 static uint32_t rack_probertt_lower_within = 10; 326 static uint32_t rack_min_rtt_movement = 250000; /* Must move at least 250ms (in microseconds) to count as a lowering */ 327 static int32_t rack_pace_one_seg = 0; /* Shall we pace for less than 1.4Meg 1MSS at a time */ 328 static int32_t rack_probertt_clear_is = 1; 329 static int32_t rack_max_drain_hbp = 1; /* Extra drain times gpsrtt for highly buffered paths */ 330 static int32_t rack_hbp_thresh = 3; /* what is the divisor max_rtt/min_rtt to decided a hbp */ 331 332 /* Part of pacing */ 333 static int32_t rack_max_per_above = 30; /* When we go to increment stop if above 100+this% */ 334 335 /* Timely information: 336 * 337 * Here we have various control parameters on how 338 * timely may change the multiplier. rack_gain_p5_ub 339 * is associated with timely but not directly influencing 340 * the rate decision like the other variables. It controls 341 * the way fill-cw interacts with timely and caps how much 342 * timely can boost the fill-cw b/w. 343 * 344 * The other values are various boost/shrink numbers as well 345 * as potential caps when adjustments are made to the timely 346 * gain (returned by rack_get_output_gain(). Remember too that 347 * the gain returned can be overriden by other factors such as 348 * probeRTT as well as fixed-rate-pacing. 349 */ 350 static int32_t rack_gain_p5_ub = 250; 351 static int32_t rack_gp_per_bw_mul_up = 2; /* 2% */ 352 static int32_t rack_gp_per_bw_mul_down = 4; /* 4% */ 353 static int32_t rack_gp_rtt_maxmul = 3; /* 3 x maxmin */ 354 static int32_t rack_gp_rtt_minmul = 1; /* minrtt + (minrtt/mindiv) is lower rtt */ 355 static int32_t rack_gp_rtt_mindiv = 4; /* minrtt + (minrtt * minmul/mindiv) is lower rtt */ 356 static int32_t rack_gp_decrease_per = 80; /* Beta value of timely decrease (.8) = 80 */ 357 static int32_t rack_gp_increase_per = 2; /* 2% increase in multiplier */ 358 static int32_t rack_per_lower_bound = 50; /* Don't allow to drop below this multiplier */ 359 static int32_t rack_per_upper_bound_ss = 0; /* Don't allow SS to grow above this */ 360 static int32_t rack_per_upper_bound_ca = 0; /* Don't allow CA to grow above this */ 361 static int32_t rack_do_dyn_mul = 0; /* Are the rack gp multipliers dynamic */ 362 static int32_t rack_gp_no_rec_chg = 1; /* Prohibit recovery from reducing it's multiplier */ 363 static int32_t rack_timely_dec_clear = 6; /* Do we clear decrement count at a value (6)? */ 364 static int32_t rack_timely_max_push_rise = 3; /* One round of pushing */ 365 static int32_t rack_timely_max_push_drop = 3; /* Three round of pushing */ 366 static int32_t rack_timely_min_segs = 4; /* 4 segment minimum */ 367 static int32_t rack_use_max_for_nobackoff = 0; 368 static int32_t rack_timely_int_timely_only = 0; /* do interim timely's only use the timely algo (no b/w changes)? */ 369 static int32_t rack_timely_no_stopping = 0; 370 static int32_t rack_down_raise_thresh = 100; 371 static int32_t rack_req_segs = 1; 372 static uint64_t rack_bw_rate_cap = 0; 373 static uint64_t rack_fillcw_bw_cap = 3750000; /* Cap fillcw at 30Mbps */ 374 375 376 /* Rack specific counters */ 377 counter_u64_t rack_saw_enobuf; 378 counter_u64_t rack_saw_enobuf_hw; 379 counter_u64_t rack_saw_enetunreach; 380 counter_u64_t rack_persists_sends; 381 counter_u64_t rack_persists_acks; 382 counter_u64_t rack_persists_loss; 383 counter_u64_t rack_persists_lost_ends; 384 counter_u64_t rack_total_bytes; 385 #ifdef INVARIANTS 386 counter_u64_t rack_adjust_map_bw; 387 #endif 388 /* Tail loss probe counters */ 389 counter_u64_t rack_tlp_tot; 390 counter_u64_t rack_tlp_newdata; 391 counter_u64_t rack_tlp_retran; 392 counter_u64_t rack_tlp_retran_bytes; 393 counter_u64_t rack_to_tot; 394 counter_u64_t rack_hot_alloc; 395 counter_u64_t tcp_policer_detected; 396 counter_u64_t rack_to_alloc; 397 counter_u64_t rack_to_alloc_hard; 398 counter_u64_t rack_to_alloc_emerg; 399 counter_u64_t rack_to_alloc_limited; 400 counter_u64_t rack_alloc_limited_conns; 401 counter_u64_t rack_split_limited; 402 counter_u64_t rack_rxt_clamps_cwnd; 403 counter_u64_t rack_rxt_clamps_cwnd_uniq; 404 405 counter_u64_t rack_multi_single_eq; 406 counter_u64_t rack_proc_non_comp_ack; 407 408 counter_u64_t rack_fto_send; 409 counter_u64_t rack_fto_rsm_send; 410 counter_u64_t rack_nfto_resend; 411 counter_u64_t rack_non_fto_send; 412 counter_u64_t rack_extended_rfo; 413 414 counter_u64_t rack_sack_proc_all; 415 counter_u64_t rack_sack_proc_short; 416 counter_u64_t rack_sack_proc_restart; 417 counter_u64_t rack_sack_attacks_detected; 418 counter_u64_t rack_sack_attacks_reversed; 419 counter_u64_t rack_sack_attacks_suspect; 420 counter_u64_t rack_sack_used_next_merge; 421 counter_u64_t rack_sack_splits; 422 counter_u64_t rack_sack_used_prev_merge; 423 counter_u64_t rack_sack_skipped_acked; 424 counter_u64_t rack_ack_total; 425 counter_u64_t rack_express_sack; 426 counter_u64_t rack_sack_total; 427 counter_u64_t rack_move_none; 428 counter_u64_t rack_move_some; 429 430 counter_u64_t rack_input_idle_reduces; 431 counter_u64_t rack_collapsed_win; 432 counter_u64_t rack_collapsed_win_seen; 433 counter_u64_t rack_collapsed_win_rxt; 434 counter_u64_t rack_collapsed_win_rxt_bytes; 435 counter_u64_t rack_try_scwnd; 436 counter_u64_t rack_hw_pace_init_fail; 437 counter_u64_t rack_hw_pace_lost; 438 439 counter_u64_t rack_out_size[TCP_MSS_ACCT_SIZE]; 440 counter_u64_t rack_opts_arry[RACK_OPTS_SIZE]; 441 442 443 #define RACK_REXMTVAL(tp) max(rack_rto_min, ((tp)->t_srtt + ((tp)->t_rttvar << 2))) 444 445 #define RACK_TCPT_RANGESET(tv, value, tvmin, tvmax, slop) do { \ 446 (tv) = (value) + slop; \ 447 if ((u_long)(tv) < (u_long)(tvmin)) \ 448 (tv) = (tvmin); \ 449 if ((u_long)(tv) > (u_long)(tvmax)) \ 450 (tv) = (tvmax); \ 451 } while (0) 452 453 static void 454 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line); 455 456 static int 457 rack_process_ack(struct mbuf *m, struct tcphdr *th, 458 struct socket *so, struct tcpcb *tp, struct tcpopt *to, 459 uint32_t tiwin, int32_t tlen, int32_t * ofia, int32_t thflags, int32_t * ret_val, int32_t orig_tlen); 460 static int 461 rack_process_data(struct mbuf *m, struct tcphdr *th, 462 struct socket *so, struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 463 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt); 464 static void 465 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, 466 uint32_t th_ack, uint16_t nsegs, uint16_t type, int32_t recovery); 467 static struct rack_sendmap *rack_alloc(struct tcp_rack *rack); 468 static struct rack_sendmap *rack_alloc_limit(struct tcp_rack *rack, 469 uint8_t limit_type); 470 static struct rack_sendmap * 471 rack_check_recovery_mode(struct tcpcb *tp, 472 uint32_t tsused); 473 static uint32_t 474 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack); 475 static void 476 rack_cong_signal(struct tcpcb *tp, 477 uint32_t type, uint32_t ack, int ); 478 static void rack_counter_destroy(void); 479 static int 480 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt); 481 static int32_t rack_ctor(void *mem, int32_t size, void *arg, int32_t how); 482 static void 483 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override); 484 static void 485 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 486 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos); 487 static void rack_dtor(void *mem, int32_t size, void *arg); 488 static void 489 rack_log_alt_to_to_cancel(struct tcp_rack *rack, 490 uint32_t flex1, uint32_t flex2, 491 uint32_t flex3, uint32_t flex4, 492 uint32_t flex5, uint32_t flex6, 493 uint16_t flex7, uint8_t mod); 494 495 static void 496 rack_log_pacing_delay_calc(struct tcp_rack *rack, uint32_t len, uint32_t slot, 497 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, int line, 498 struct rack_sendmap *rsm, uint8_t quality); 499 static struct rack_sendmap * 500 rack_find_high_nonack(struct tcp_rack *rack, 501 struct rack_sendmap *rsm); 502 static struct rack_sendmap *rack_find_lowest_rsm(struct tcp_rack *rack); 503 static void rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm); 504 static void rack_fini(struct tcpcb *tp, int32_t tcb_is_purged); 505 static int rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt); 506 static void 507 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack, 508 tcp_seq th_ack, int line, uint8_t quality); 509 static void 510 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm); 511 512 static uint32_t 513 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss); 514 static int32_t rack_handoff_ok(struct tcpcb *tp); 515 static int32_t rack_init(struct tcpcb *tp, void **ptr); 516 static void rack_init_sysctls(void); 517 518 static void 519 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, 520 struct tcphdr *th, int entered_rec, int dup_ack_struck, 521 int *dsack_seen, int *sacks_seen); 522 static void 523 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 524 uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t ts, 525 struct rack_sendmap *hintrsm, uint32_t add_flags, struct mbuf *s_mb, uint32_t s_moff, int hw_tls, int segsiz); 526 527 static uint64_t rack_get_gp_est(struct tcp_rack *rack); 528 529 530 static void 531 rack_log_sack_passed(struct tcpcb *tp, struct tcp_rack *rack, 532 struct rack_sendmap *rsm, uint32_t cts); 533 static void rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm); 534 static int32_t rack_output(struct tcpcb *tp); 535 536 static uint32_t 537 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, 538 struct sackblk *sack, struct tcpopt *to, struct rack_sendmap **prsm, 539 uint32_t cts, int *no_extra, int *moved_two, uint32_t segsiz); 540 static void rack_post_recovery(struct tcpcb *tp, uint32_t th_seq); 541 static void rack_remxt_tmr(struct tcpcb *tp); 542 static int rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt); 543 static void rack_set_state(struct tcpcb *tp, struct tcp_rack *rack); 544 static int32_t rack_stopall(struct tcpcb *tp); 545 static void rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line); 546 static uint32_t 547 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 548 struct rack_sendmap *rsm, uint64_t ts, int32_t * lenp, uint32_t add_flag, int segsiz); 549 static void 550 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 551 struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz); 552 static int 553 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 554 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack); 555 static int32_t tcp_addrack(module_t mod, int32_t type, void *data); 556 static int 557 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, 558 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 559 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 560 561 static void 562 rack_peg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz); 563 564 static int 565 rack_do_closing(struct mbuf *m, struct tcphdr *th, 566 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 567 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 568 static int 569 rack_do_established(struct mbuf *m, struct tcphdr *th, 570 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 571 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 572 static int 573 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, 574 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 575 int32_t tlen, uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos); 576 static int 577 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, 578 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 579 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 580 static int 581 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, 582 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 583 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 584 static int 585 rack_do_lastack(struct mbuf *m, struct tcphdr *th, 586 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 587 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 588 static int 589 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, 590 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 591 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 592 static int 593 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, 594 struct socket *so, struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, 595 int32_t tlen, uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos); 596 static void rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts); 597 struct rack_sendmap * 598 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, 599 uint32_t tsused); 600 static void tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, 601 uint32_t len, uint32_t us_tim, int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt); 602 static void 603 tcp_rack_partialack(struct tcpcb *tp); 604 static int 605 rack_set_profile(struct tcp_rack *rack, int prof); 606 static void 607 rack_apply_deferred_options(struct tcp_rack *rack); 608 609 int32_t rack_clear_counter=0; 610 611 static uint64_t 612 rack_get_lt_bw(struct tcp_rack *rack) 613 { 614 struct timeval tv; 615 uint64_t tim, bytes; 616 617 tim = rack->r_ctl.lt_bw_time; 618 bytes = rack->r_ctl.lt_bw_bytes; 619 if (rack->lt_bw_up) { 620 /* Include all the current bytes too */ 621 microuptime(&tv); 622 bytes += (rack->rc_tp->snd_una - rack->r_ctl.lt_seq); 623 tim += (tcp_tv_to_lusectick(&tv) - rack->r_ctl.lt_timemark); 624 } 625 if ((bytes != 0) && (tim != 0)) 626 return ((bytes * (uint64_t)1000000) / tim); 627 else 628 return (0); 629 } 630 631 static void 632 rack_swap_beta_values(struct tcp_rack *rack, uint8_t flex8) 633 { 634 struct sockopt sopt; 635 struct cc_newreno_opts opt; 636 struct newreno old; 637 struct tcpcb *tp; 638 int error, failed = 0; 639 640 tp = rack->rc_tp; 641 if (tp->t_cc == NULL) { 642 /* Tcb is leaving */ 643 return; 644 } 645 rack->rc_pacing_cc_set = 1; 646 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) { 647 /* Not new-reno we can't play games with beta! */ 648 failed = 1; 649 goto out; 650 651 } 652 if (CC_ALGO(tp)->ctl_output == NULL) { 653 /* Huh, not using new-reno so no swaps.? */ 654 failed = 2; 655 goto out; 656 } 657 /* Get the current values out */ 658 sopt.sopt_valsize = sizeof(struct cc_newreno_opts); 659 sopt.sopt_dir = SOPT_GET; 660 opt.name = CC_NEWRENO_BETA; 661 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt); 662 if (error) { 663 failed = 3; 664 goto out; 665 } 666 old.beta = opt.val; 667 opt.name = CC_NEWRENO_BETA_ECN; 668 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt); 669 if (error) { 670 failed = 4; 671 goto out; 672 } 673 old.beta_ecn = opt.val; 674 675 /* Now lets set in the values we have stored */ 676 sopt.sopt_dir = SOPT_SET; 677 opt.name = CC_NEWRENO_BETA; 678 opt.val = rack->r_ctl.rc_saved_beta.beta; 679 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt); 680 if (error) { 681 failed = 5; 682 goto out; 683 } 684 opt.name = CC_NEWRENO_BETA_ECN; 685 opt.val = rack->r_ctl.rc_saved_beta.beta_ecn; 686 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt); 687 if (error) { 688 failed = 6; 689 goto out; 690 } 691 /* Save off the values for restoral */ 692 memcpy(&rack->r_ctl.rc_saved_beta, &old, sizeof(struct newreno)); 693 out: 694 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 695 union tcp_log_stackspecific log; 696 struct timeval tv; 697 struct newreno *ptr; 698 699 ptr = ((struct newreno *)tp->t_ccv.cc_data); 700 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 701 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 702 log.u_bbr.flex1 = ptr->beta; 703 log.u_bbr.flex2 = ptr->beta_ecn; 704 log.u_bbr.flex3 = ptr->newreno_flags; 705 log.u_bbr.flex4 = rack->r_ctl.rc_saved_beta.beta; 706 log.u_bbr.flex5 = rack->r_ctl.rc_saved_beta.beta_ecn; 707 log.u_bbr.flex6 = failed; 708 log.u_bbr.flex7 = rack->gp_ready; 709 log.u_bbr.flex7 <<= 1; 710 log.u_bbr.flex7 |= rack->use_fixed_rate; 711 log.u_bbr.flex7 <<= 1; 712 log.u_bbr.flex7 |= rack->rc_pacing_cc_set; 713 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 714 log.u_bbr.flex8 = flex8; 715 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, error, 716 0, &log, false, NULL, NULL, 0, &tv); 717 } 718 } 719 720 static void 721 rack_set_cc_pacing(struct tcp_rack *rack) 722 { 723 if (rack->rc_pacing_cc_set) 724 return; 725 /* 726 * Use the swap utility placing in 3 for flex8 to id a 727 * set of a new set of values. 728 */ 729 rack->rc_pacing_cc_set = 1; 730 rack_swap_beta_values(rack, 3); 731 } 732 733 static void 734 rack_undo_cc_pacing(struct tcp_rack *rack) 735 { 736 if (rack->rc_pacing_cc_set == 0) 737 return; 738 /* 739 * Use the swap utility placing in 4 for flex8 to id a 740 * restoral of the old values. 741 */ 742 rack->rc_pacing_cc_set = 0; 743 rack_swap_beta_values(rack, 4); 744 } 745 746 static void 747 rack_remove_pacing(struct tcp_rack *rack) 748 { 749 if (rack->rc_pacing_cc_set) 750 rack_undo_cc_pacing(rack); 751 if (rack->r_ctl.pacing_method & RACK_REG_PACING) 752 tcp_decrement_paced_conn(); 753 if (rack->r_ctl.pacing_method & RACK_DGP_PACING) 754 tcp_dec_dgp_pacing_cnt(); 755 rack->rc_always_pace = 0; 756 rack->r_ctl.pacing_method = RACK_PACING_NONE; 757 rack->dgp_on = 0; 758 rack->rc_hybrid_mode = 0; 759 rack->use_fixed_rate = 0; 760 } 761 762 static void 763 rack_log_gpset(struct tcp_rack *rack, uint32_t seq_end, uint32_t ack_end_t, 764 uint32_t send_end_t, int line, uint8_t mode, struct rack_sendmap *rsm) 765 { 766 if (tcp_bblogging_on(rack->rc_tp) && (rack_verbose_logging != 0)) { 767 union tcp_log_stackspecific log; 768 struct timeval tv; 769 770 memset(&log, 0, sizeof(log)); 771 log.u_bbr.flex1 = seq_end; 772 log.u_bbr.flex2 = rack->rc_tp->gput_seq; 773 log.u_bbr.flex3 = ack_end_t; 774 log.u_bbr.flex4 = rack->rc_tp->gput_ts; 775 log.u_bbr.flex5 = send_end_t; 776 log.u_bbr.flex6 = rack->rc_tp->gput_ack; 777 log.u_bbr.flex7 = mode; 778 log.u_bbr.flex8 = 69; 779 log.u_bbr.rttProp = rack->r_ctl.rc_gp_cumack_ts; 780 log.u_bbr.delRate = rack->r_ctl.rc_gp_output_ts; 781 log.u_bbr.pkts_out = line; 782 log.u_bbr.cwnd_gain = rack->app_limited_needs_set; 783 log.u_bbr.pkt_epoch = rack->r_ctl.rc_app_limited_cnt; 784 log.u_bbr.epoch = rack->r_ctl.current_round; 785 log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost; 786 if (rsm != NULL) { 787 log.u_bbr.applimited = rsm->r_start; 788 log.u_bbr.delivered = rsm->r_end; 789 log.u_bbr.epoch = rsm->r_flags; 790 } 791 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 792 TCP_LOG_EVENTP(rack->rc_tp, NULL, 793 &rack->rc_inp->inp_socket->so_rcv, 794 &rack->rc_inp->inp_socket->so_snd, 795 BBR_LOG_HPTSI_CALC, 0, 796 0, &log, false, &tv); 797 } 798 } 799 800 static int 801 sysctl_rack_clear(SYSCTL_HANDLER_ARGS) 802 { 803 uint32_t stat; 804 int32_t error; 805 806 error = SYSCTL_OUT(req, &rack_clear_counter, sizeof(uint32_t)); 807 if (error || req->newptr == NULL) 808 return error; 809 810 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 811 if (error) 812 return (error); 813 if (stat == 1) { 814 #ifdef INVARIANTS 815 printf("Clearing RACK counters\n"); 816 #endif 817 counter_u64_zero(rack_tlp_tot); 818 counter_u64_zero(rack_tlp_newdata); 819 counter_u64_zero(rack_tlp_retran); 820 counter_u64_zero(rack_tlp_retran_bytes); 821 counter_u64_zero(rack_to_tot); 822 counter_u64_zero(rack_saw_enobuf); 823 counter_u64_zero(rack_saw_enobuf_hw); 824 counter_u64_zero(rack_saw_enetunreach); 825 counter_u64_zero(rack_persists_sends); 826 counter_u64_zero(rack_total_bytes); 827 counter_u64_zero(rack_persists_acks); 828 counter_u64_zero(rack_persists_loss); 829 counter_u64_zero(rack_persists_lost_ends); 830 #ifdef INVARIANTS 831 counter_u64_zero(rack_adjust_map_bw); 832 #endif 833 counter_u64_zero(rack_to_alloc_hard); 834 counter_u64_zero(rack_to_alloc_emerg); 835 counter_u64_zero(rack_sack_proc_all); 836 counter_u64_zero(rack_fto_send); 837 counter_u64_zero(rack_fto_rsm_send); 838 counter_u64_zero(rack_extended_rfo); 839 counter_u64_zero(rack_hw_pace_init_fail); 840 counter_u64_zero(rack_hw_pace_lost); 841 counter_u64_zero(rack_non_fto_send); 842 counter_u64_zero(rack_nfto_resend); 843 counter_u64_zero(rack_sack_proc_short); 844 counter_u64_zero(rack_sack_proc_restart); 845 counter_u64_zero(rack_to_alloc); 846 counter_u64_zero(rack_to_alloc_limited); 847 counter_u64_zero(rack_alloc_limited_conns); 848 counter_u64_zero(rack_split_limited); 849 counter_u64_zero(rack_rxt_clamps_cwnd); 850 counter_u64_zero(rack_rxt_clamps_cwnd_uniq); 851 counter_u64_zero(rack_multi_single_eq); 852 counter_u64_zero(rack_proc_non_comp_ack); 853 counter_u64_zero(rack_sack_attacks_detected); 854 counter_u64_zero(rack_sack_attacks_reversed); 855 counter_u64_zero(rack_sack_attacks_suspect); 856 counter_u64_zero(rack_sack_used_next_merge); 857 counter_u64_zero(rack_sack_used_prev_merge); 858 counter_u64_zero(rack_sack_splits); 859 counter_u64_zero(rack_sack_skipped_acked); 860 counter_u64_zero(rack_ack_total); 861 counter_u64_zero(rack_express_sack); 862 counter_u64_zero(rack_sack_total); 863 counter_u64_zero(rack_move_none); 864 counter_u64_zero(rack_move_some); 865 counter_u64_zero(rack_try_scwnd); 866 counter_u64_zero(rack_collapsed_win); 867 counter_u64_zero(rack_collapsed_win_rxt); 868 counter_u64_zero(rack_collapsed_win_seen); 869 counter_u64_zero(rack_collapsed_win_rxt_bytes); 870 } else if (stat == 2) { 871 #ifdef INVARIANTS 872 printf("Clearing RACK option array\n"); 873 #endif 874 COUNTER_ARRAY_ZERO(rack_opts_arry, RACK_OPTS_SIZE); 875 } else if (stat == 3) { 876 printf("Rack has no stats counters to clear (use 1 to clear all stats in sysctl node)\n"); 877 } else if (stat == 4) { 878 #ifdef INVARIANTS 879 printf("Clearing RACK out size array\n"); 880 #endif 881 COUNTER_ARRAY_ZERO(rack_out_size, TCP_MSS_ACCT_SIZE); 882 } 883 rack_clear_counter = 0; 884 return (0); 885 } 886 887 static void 888 rack_init_sysctls(void) 889 { 890 struct sysctl_oid *rack_counters; 891 struct sysctl_oid *rack_attack; 892 struct sysctl_oid *rack_pacing; 893 struct sysctl_oid *rack_timely; 894 struct sysctl_oid *rack_timers; 895 struct sysctl_oid *rack_tlp; 896 struct sysctl_oid *rack_misc; 897 struct sysctl_oid *rack_features; 898 struct sysctl_oid *rack_measure; 899 struct sysctl_oid *rack_probertt; 900 struct sysctl_oid *rack_hw_pacing; 901 struct sysctl_oid *rack_policing; 902 903 rack_attack = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 904 SYSCTL_CHILDREN(rack_sysctl_root), 905 OID_AUTO, 906 "sack_attack", 907 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 908 "Rack Sack Attack Counters and Controls"); 909 rack_counters = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 910 SYSCTL_CHILDREN(rack_sysctl_root), 911 OID_AUTO, 912 "stats", 913 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 914 "Rack Counters"); 915 SYSCTL_ADD_S32(&rack_sysctl_ctx, 916 SYSCTL_CHILDREN(rack_sysctl_root), 917 OID_AUTO, "rate_sample_method", CTLFLAG_RW, 918 &rack_rate_sample_method , USE_RTT_LOW, 919 "What method should we use for rate sampling 0=high, 1=low "); 920 /* Probe rtt related controls */ 921 rack_probertt = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 922 SYSCTL_CHILDREN(rack_sysctl_root), 923 OID_AUTO, 924 "probertt", 925 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 926 "ProbeRTT related Controls"); 927 SYSCTL_ADD_U16(&rack_sysctl_ctx, 928 SYSCTL_CHILDREN(rack_probertt), 929 OID_AUTO, "exit_per_hpb", CTLFLAG_RW, 930 &rack_atexit_prtt_hbp, 130, 931 "What percentage above goodput do we clamp CA/SS to at exit on high-BDP path 110%"); 932 SYSCTL_ADD_U16(&rack_sysctl_ctx, 933 SYSCTL_CHILDREN(rack_probertt), 934 OID_AUTO, "exit_per_nonhpb", CTLFLAG_RW, 935 &rack_atexit_prtt, 130, 936 "What percentage above goodput do we clamp CA/SS to at exit on a non high-BDP path 100%"); 937 SYSCTL_ADD_U16(&rack_sysctl_ctx, 938 SYSCTL_CHILDREN(rack_probertt), 939 OID_AUTO, "gp_per_mul", CTLFLAG_RW, 940 &rack_per_of_gp_probertt, 60, 941 "What percentage of goodput do we pace at in probertt"); 942 SYSCTL_ADD_U16(&rack_sysctl_ctx, 943 SYSCTL_CHILDREN(rack_probertt), 944 OID_AUTO, "gp_per_reduce", CTLFLAG_RW, 945 &rack_per_of_gp_probertt_reduce, 10, 946 "What percentage of goodput do we reduce every gp_srtt"); 947 SYSCTL_ADD_U16(&rack_sysctl_ctx, 948 SYSCTL_CHILDREN(rack_probertt), 949 OID_AUTO, "gp_per_low", CTLFLAG_RW, 950 &rack_per_of_gp_lowthresh, 40, 951 "What percentage of goodput do we allow the multiplier to fall to"); 952 SYSCTL_ADD_U32(&rack_sysctl_ctx, 953 SYSCTL_CHILDREN(rack_probertt), 954 OID_AUTO, "time_between", CTLFLAG_RW, 955 & rack_time_between_probertt, 96000000, 956 "How many useconds between the lowest rtt falling must past before we enter probertt"); 957 SYSCTL_ADD_U32(&rack_sysctl_ctx, 958 SYSCTL_CHILDREN(rack_probertt), 959 OID_AUTO, "safety", CTLFLAG_RW, 960 &rack_probe_rtt_safety_val, 2000000, 961 "If not zero, provides a maximum usecond that you can stay in probertt (2sec = 2000000)"); 962 SYSCTL_ADD_U32(&rack_sysctl_ctx, 963 SYSCTL_CHILDREN(rack_probertt), 964 OID_AUTO, "sets_cwnd", CTLFLAG_RW, 965 &rack_probe_rtt_sets_cwnd, 0, 966 "Do we set the cwnd too (if always_lower is on)"); 967 SYSCTL_ADD_U32(&rack_sysctl_ctx, 968 SYSCTL_CHILDREN(rack_probertt), 969 OID_AUTO, "maxdrainsrtts", CTLFLAG_RW, 970 &rack_max_drain_wait, 2, 971 "Maximum number of gp_srtt's to hold in drain waiting for flight to reach goal"); 972 SYSCTL_ADD_U32(&rack_sysctl_ctx, 973 SYSCTL_CHILDREN(rack_probertt), 974 OID_AUTO, "mustdrainsrtts", CTLFLAG_RW, 975 &rack_must_drain, 1, 976 "We must drain this many gp_srtt's waiting for flight to reach goal"); 977 SYSCTL_ADD_U32(&rack_sysctl_ctx, 978 SYSCTL_CHILDREN(rack_probertt), 979 OID_AUTO, "goal_use_min_entry", CTLFLAG_RW, 980 &rack_probertt_use_min_rtt_entry, 1, 981 "Should we use the min-rtt to calculate the goal rtt (else gp_srtt) at entry"); 982 SYSCTL_ADD_U32(&rack_sysctl_ctx, 983 SYSCTL_CHILDREN(rack_probertt), 984 OID_AUTO, "goal_use_min_exit", CTLFLAG_RW, 985 &rack_probertt_use_min_rtt_exit, 0, 986 "How to set cwnd at exit, 0 - dynamic, 1 - use min-rtt, 2 - use curgprtt, 3 - entry gp-rtt"); 987 SYSCTL_ADD_U32(&rack_sysctl_ctx, 988 SYSCTL_CHILDREN(rack_probertt), 989 OID_AUTO, "length_div", CTLFLAG_RW, 990 &rack_probertt_gpsrtt_cnt_div, 0, 991 "How many recent goodput srtt periods plus hold tim does probertt last (bottom of fraction)"); 992 SYSCTL_ADD_U32(&rack_sysctl_ctx, 993 SYSCTL_CHILDREN(rack_probertt), 994 OID_AUTO, "length_mul", CTLFLAG_RW, 995 &rack_probertt_gpsrtt_cnt_mul, 0, 996 "How many recent goodput srtt periods plus hold tim does probertt last (top of fraction)"); 997 SYSCTL_ADD_U32(&rack_sysctl_ctx, 998 SYSCTL_CHILDREN(rack_probertt), 999 OID_AUTO, "holdtim_at_target", CTLFLAG_RW, 1000 &rack_min_probertt_hold, 200000, 1001 "What is the minimum time we hold probertt at target"); 1002 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1003 SYSCTL_CHILDREN(rack_probertt), 1004 OID_AUTO, "filter_life", CTLFLAG_RW, 1005 &rack_probertt_filter_life, 10000000, 1006 "What is the time for the filters life in useconds"); 1007 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1008 SYSCTL_CHILDREN(rack_probertt), 1009 OID_AUTO, "lower_within", CTLFLAG_RW, 1010 &rack_probertt_lower_within, 10, 1011 "If the rtt goes lower within this percentage of the time, go into probe-rtt"); 1012 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1013 SYSCTL_CHILDREN(rack_probertt), 1014 OID_AUTO, "must_move", CTLFLAG_RW, 1015 &rack_min_rtt_movement, 250, 1016 "How much is the minimum movement in rtt to count as a drop for probertt purposes"); 1017 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1018 SYSCTL_CHILDREN(rack_probertt), 1019 OID_AUTO, "clear_is_cnts", CTLFLAG_RW, 1020 &rack_probertt_clear_is, 1, 1021 "Do we clear I/S counts on exiting probe-rtt"); 1022 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1023 SYSCTL_CHILDREN(rack_probertt), 1024 OID_AUTO, "hbp_extra_drain", CTLFLAG_RW, 1025 &rack_max_drain_hbp, 1, 1026 "How many extra drain gpsrtt's do we get in highly buffered paths"); 1027 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1028 SYSCTL_CHILDREN(rack_probertt), 1029 OID_AUTO, "hbp_threshold", CTLFLAG_RW, 1030 &rack_hbp_thresh, 3, 1031 "We are highly buffered if min_rtt_seen / max_rtt_seen > this-threshold"); 1032 /* Pacing related sysctls */ 1033 rack_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1034 SYSCTL_CHILDREN(rack_sysctl_root), 1035 OID_AUTO, 1036 "pacing", 1037 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1038 "Pacing related Controls"); 1039 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1040 SYSCTL_CHILDREN(rack_pacing), 1041 OID_AUTO, "pcm_enabled", CTLFLAG_RW, 1042 &rack_pcm_is_enabled, 1, 1043 "Do we by default do PCM measurements?"); 1044 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1045 SYSCTL_CHILDREN(rack_pacing), 1046 OID_AUTO, "pcm_rnds", CTLFLAG_RW, 1047 &rack_pcm_every_n_rounds, 100, 1048 "How many rounds before we need to do a PCM measurement"); 1049 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1050 SYSCTL_CHILDREN(rack_pacing), 1051 OID_AUTO, "pcm_blast", CTLFLAG_RW, 1052 &rack_pcm_blast, 0, 1053 "Blast out the full cwnd/rwnd when doing a PCM measurement"); 1054 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1055 SYSCTL_CHILDREN(rack_pacing), 1056 OID_AUTO, "rnd_gp_gain", CTLFLAG_RW, 1057 &rack_gp_gain_req, 1200, 1058 "How much do we have to increase the GP to record the round 1200 = 120.0"); 1059 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1060 SYSCTL_CHILDREN(rack_pacing), 1061 OID_AUTO, "dgp_out_of_ss_at", CTLFLAG_RW, 1062 &rack_rnd_cnt_req, 0x10005, 1063 "How many rounds less than rnd_gp_gain will drop us out of SS"); 1064 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1065 SYSCTL_CHILDREN(rack_pacing), 1066 OID_AUTO, "no_timely", CTLFLAG_RW, 1067 &rack_timely_off, 0, 1068 "Do we not use timely in DGP?"); 1069 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1070 SYSCTL_CHILDREN(rack_pacing), 1071 OID_AUTO, "fullbufdisc", CTLFLAG_RW, 1072 &rack_full_buffer_discount, 10, 1073 "What percentage b/w reduction over the GP estimate for a full buffer (default=0 off)?"); 1074 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1075 SYSCTL_CHILDREN(rack_pacing), 1076 OID_AUTO, "fillcw", CTLFLAG_RW, 1077 &rack_fill_cw_state, 0, 1078 "Enable fillcw on new connections (default=0 off)?"); 1079 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1080 SYSCTL_CHILDREN(rack_pacing), 1081 OID_AUTO, "min_burst", CTLFLAG_RW, 1082 &rack_pacing_min_seg, 0, 1083 "What is the min burst size for pacing (0 disables)?"); 1084 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1085 SYSCTL_CHILDREN(rack_pacing), 1086 OID_AUTO, "divisor", CTLFLAG_RW, 1087 &rack_default_pacing_divisor, 250, 1088 "What is the default divisor given to the rl code?"); 1089 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1090 SYSCTL_CHILDREN(rack_pacing), 1091 OID_AUTO, "fillcw_max_mult", CTLFLAG_RW, 1092 &rack_bw_multipler, 0, 1093 "What is the limit multiplier of the current gp_est that fillcw can increase the b/w too, 200 == 200% (0 = off)?"); 1094 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1095 SYSCTL_CHILDREN(rack_pacing), 1096 OID_AUTO, "max_pace_over", CTLFLAG_RW, 1097 &rack_max_per_above, 30, 1098 "What is the maximum allowable percentage that we can pace above (so 30 = 130% of our goal)"); 1099 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1100 SYSCTL_CHILDREN(rack_pacing), 1101 OID_AUTO, "allow1mss", CTLFLAG_RW, 1102 &rack_pace_one_seg, 0, 1103 "Do we allow low b/w pacing of 1MSS instead of two (1.2Meg and less)?"); 1104 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1105 SYSCTL_CHILDREN(rack_pacing), 1106 OID_AUTO, "limit_wsrtt", CTLFLAG_RW, 1107 &rack_limit_time_with_srtt, 0, 1108 "Do we limit pacing time based on srtt"); 1109 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1110 SYSCTL_CHILDREN(rack_pacing), 1111 OID_AUTO, "gp_per_ss", CTLFLAG_RW, 1112 &rack_per_of_gp_ss, 250, 1113 "If non zero, what percentage of goodput to pace at in slow start"); 1114 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1115 SYSCTL_CHILDREN(rack_pacing), 1116 OID_AUTO, "gp_per_ca", CTLFLAG_RW, 1117 &rack_per_of_gp_ca, 150, 1118 "If non zero, what percentage of goodput to pace at in congestion avoidance"); 1119 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1120 SYSCTL_CHILDREN(rack_pacing), 1121 OID_AUTO, "gp_per_rec", CTLFLAG_RW, 1122 &rack_per_of_gp_rec, 200, 1123 "If non zero, what percentage of goodput to pace at in recovery"); 1124 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1125 SYSCTL_CHILDREN(rack_pacing), 1126 OID_AUTO, "pace_max_seg", CTLFLAG_RW, 1127 &rack_hptsi_segments, 40, 1128 "What size is the max for TSO segments in pacing and burst mitigation"); 1129 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1130 SYSCTL_CHILDREN(rack_pacing), 1131 OID_AUTO, "burst_reduces", CTLFLAG_RW, 1132 &rack_slot_reduction, 4, 1133 "When doing only burst mitigation what is the reduce divisor"); 1134 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1135 SYSCTL_CHILDREN(rack_sysctl_root), 1136 OID_AUTO, "use_pacing", CTLFLAG_RW, 1137 &rack_pace_every_seg, 0, 1138 "If set we use pacing, if clear we use only the original burst mitigation"); 1139 SYSCTL_ADD_U64(&rack_sysctl_ctx, 1140 SYSCTL_CHILDREN(rack_pacing), 1141 OID_AUTO, "rate_cap", CTLFLAG_RW, 1142 &rack_bw_rate_cap, 0, 1143 "If set we apply this value to the absolute rate cap used by pacing"); 1144 SYSCTL_ADD_U64(&rack_sysctl_ctx, 1145 SYSCTL_CHILDREN(rack_pacing), 1146 OID_AUTO, "fillcw_cap", CTLFLAG_RW, 1147 &rack_fillcw_bw_cap, 3750000, 1148 "Do we have an absolute cap on the amount of b/w fillcw can specify (0 = no)?"); 1149 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1150 SYSCTL_CHILDREN(rack_sysctl_root), 1151 OID_AUTO, "req_measure_cnt", CTLFLAG_RW, 1152 &rack_req_measurements, 1, 1153 "If doing dynamic pacing, how many measurements must be in before we start pacing?"); 1154 /* Hardware pacing */ 1155 rack_hw_pacing = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1156 SYSCTL_CHILDREN(rack_sysctl_root), 1157 OID_AUTO, 1158 "hdwr_pacing", 1159 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1160 "Pacing related Controls"); 1161 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1162 SYSCTL_CHILDREN(rack_hw_pacing), 1163 OID_AUTO, "rwnd_factor", CTLFLAG_RW, 1164 &rack_hw_rwnd_factor, 2, 1165 "How many times does snd_wnd need to be bigger than pace_max_seg so we will hold off and get more acks?"); 1166 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1167 SYSCTL_CHILDREN(rack_hw_pacing), 1168 OID_AUTO, "precheck", CTLFLAG_RW, 1169 &rack_hw_check_queue, 0, 1170 "Do we always precheck the hdwr pacing queue to avoid ENOBUF's?"); 1171 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1172 SYSCTL_CHILDREN(rack_hw_pacing), 1173 OID_AUTO, "pace_enobuf_mult", CTLFLAG_RW, 1174 &rack_enobuf_hw_boost_mult, 0, 1175 "By how many time_betweens should we boost the pacing time if we see a ENOBUFS?"); 1176 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1177 SYSCTL_CHILDREN(rack_hw_pacing), 1178 OID_AUTO, "pace_enobuf_max", CTLFLAG_RW, 1179 &rack_enobuf_hw_max, 2, 1180 "What is the max boost the pacing time if we see a ENOBUFS?"); 1181 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1182 SYSCTL_CHILDREN(rack_hw_pacing), 1183 OID_AUTO, "pace_enobuf_min", CTLFLAG_RW, 1184 &rack_enobuf_hw_min, 2, 1185 "What is the min boost the pacing time if we see a ENOBUFS?"); 1186 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1187 SYSCTL_CHILDREN(rack_hw_pacing), 1188 OID_AUTO, "enable", CTLFLAG_RW, 1189 &rack_enable_hw_pacing, 0, 1190 "Should RACK attempt to use hw pacing?"); 1191 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1192 SYSCTL_CHILDREN(rack_hw_pacing), 1193 OID_AUTO, "rate_cap", CTLFLAG_RW, 1194 &rack_hw_rate_caps, 0, 1195 "Does the highest hardware pacing rate cap the rate we will send at??"); 1196 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1197 SYSCTL_CHILDREN(rack_hw_pacing), 1198 OID_AUTO, "uncap_per", CTLFLAG_RW, 1199 &rack_hw_rate_cap_per, 0, 1200 "If you go over b/w by this amount you will be uncapped (0 = never)"); 1201 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1202 SYSCTL_CHILDREN(rack_hw_pacing), 1203 OID_AUTO, "rate_min", CTLFLAG_RW, 1204 &rack_hw_rate_min, 0, 1205 "Do we need a minimum estimate of this many bytes per second in order to engage hw pacing?"); 1206 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1207 SYSCTL_CHILDREN(rack_hw_pacing), 1208 OID_AUTO, "rate_to_low", CTLFLAG_RW, 1209 &rack_hw_rate_to_low, 0, 1210 "If we fall below this rate, dis-engage hw pacing?"); 1211 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1212 SYSCTL_CHILDREN(rack_hw_pacing), 1213 OID_AUTO, "up_only", CTLFLAG_RW, 1214 &rack_hw_up_only, 0, 1215 "Do we allow hw pacing to lower the rate selected?"); 1216 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1217 SYSCTL_CHILDREN(rack_hw_pacing), 1218 OID_AUTO, "extra_mss_precise", CTLFLAG_RW, 1219 &rack_hw_pace_extra_slots, 0, 1220 "If the rates between software and hardware match precisely how many extra time_betweens do we get?"); 1221 rack_timely = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1222 SYSCTL_CHILDREN(rack_sysctl_root), 1223 OID_AUTO, 1224 "timely", 1225 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1226 "Rack Timely RTT Controls"); 1227 /* Timely based GP dynmics */ 1228 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1229 SYSCTL_CHILDREN(rack_timely), 1230 OID_AUTO, "upper", CTLFLAG_RW, 1231 &rack_gp_per_bw_mul_up, 2, 1232 "Rack timely upper range for equal b/w (in percentage)"); 1233 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1234 SYSCTL_CHILDREN(rack_timely), 1235 OID_AUTO, "lower", CTLFLAG_RW, 1236 &rack_gp_per_bw_mul_down, 4, 1237 "Rack timely lower range for equal b/w (in percentage)"); 1238 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1239 SYSCTL_CHILDREN(rack_timely), 1240 OID_AUTO, "rtt_max_mul", CTLFLAG_RW, 1241 &rack_gp_rtt_maxmul, 3, 1242 "Rack timely multiplier of lowest rtt for rtt_max"); 1243 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1244 SYSCTL_CHILDREN(rack_timely), 1245 OID_AUTO, "rtt_min_div", CTLFLAG_RW, 1246 &rack_gp_rtt_mindiv, 4, 1247 "Rack timely divisor used for rtt + (rtt * mul/divisor) for check for lower rtt"); 1248 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1249 SYSCTL_CHILDREN(rack_timely), 1250 OID_AUTO, "rtt_min_mul", CTLFLAG_RW, 1251 &rack_gp_rtt_minmul, 1, 1252 "Rack timely multiplier used for rtt + (rtt * mul/divisor) for check for lower rtt"); 1253 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1254 SYSCTL_CHILDREN(rack_timely), 1255 OID_AUTO, "decrease", CTLFLAG_RW, 1256 &rack_gp_decrease_per, 80, 1257 "Rack timely Beta value 80 = .8 (scaled by 100)"); 1258 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1259 SYSCTL_CHILDREN(rack_timely), 1260 OID_AUTO, "increase", CTLFLAG_RW, 1261 &rack_gp_increase_per, 2, 1262 "Rack timely increase perentage of our GP multiplication factor"); 1263 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1264 SYSCTL_CHILDREN(rack_timely), 1265 OID_AUTO, "lowerbound", CTLFLAG_RW, 1266 &rack_per_lower_bound, 50, 1267 "Rack timely lowest percentage we allow GP multiplier to fall to"); 1268 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1269 SYSCTL_CHILDREN(rack_timely), 1270 OID_AUTO, "p5_upper", CTLFLAG_RW, 1271 &rack_gain_p5_ub, 250, 1272 "Profile 5 upper bound to timely gain"); 1273 1274 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1275 SYSCTL_CHILDREN(rack_timely), 1276 OID_AUTO, "upperboundss", CTLFLAG_RW, 1277 &rack_per_upper_bound_ss, 0, 1278 "Rack timely highest percentage we allow GP multiplier in SS to raise to (0 is no upperbound)"); 1279 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1280 SYSCTL_CHILDREN(rack_timely), 1281 OID_AUTO, "upperboundca", CTLFLAG_RW, 1282 &rack_per_upper_bound_ca, 0, 1283 "Rack timely highest percentage we allow GP multiplier to CA raise to (0 is no upperbound)"); 1284 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1285 SYSCTL_CHILDREN(rack_timely), 1286 OID_AUTO, "dynamicgp", CTLFLAG_RW, 1287 &rack_do_dyn_mul, 0, 1288 "Rack timely do we enable dynmaic timely goodput by default"); 1289 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1290 SYSCTL_CHILDREN(rack_timely), 1291 OID_AUTO, "no_rec_red", CTLFLAG_RW, 1292 &rack_gp_no_rec_chg, 1, 1293 "Rack timely do we prohibit the recovery multiplier from being lowered"); 1294 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1295 SYSCTL_CHILDREN(rack_timely), 1296 OID_AUTO, "red_clear_cnt", CTLFLAG_RW, 1297 &rack_timely_dec_clear, 6, 1298 "Rack timely what threshold do we count to before another boost during b/w decent"); 1299 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1300 SYSCTL_CHILDREN(rack_timely), 1301 OID_AUTO, "max_push_rise", CTLFLAG_RW, 1302 &rack_timely_max_push_rise, 3, 1303 "Rack timely how many times do we push up with b/w increase"); 1304 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1305 SYSCTL_CHILDREN(rack_timely), 1306 OID_AUTO, "max_push_drop", CTLFLAG_RW, 1307 &rack_timely_max_push_drop, 3, 1308 "Rack timely how many times do we push back on b/w decent"); 1309 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1310 SYSCTL_CHILDREN(rack_timely), 1311 OID_AUTO, "min_segs", CTLFLAG_RW, 1312 &rack_timely_min_segs, 4, 1313 "Rack timely when setting the cwnd what is the min num segments"); 1314 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1315 SYSCTL_CHILDREN(rack_timely), 1316 OID_AUTO, "noback_max", CTLFLAG_RW, 1317 &rack_use_max_for_nobackoff, 0, 1318 "Rack timely when deciding if to backoff on a loss, do we use under max rtt else min"); 1319 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1320 SYSCTL_CHILDREN(rack_timely), 1321 OID_AUTO, "interim_timely_only", CTLFLAG_RW, 1322 &rack_timely_int_timely_only, 0, 1323 "Rack timely when doing interim timely's do we only do timely (no b/w consideration)"); 1324 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1325 SYSCTL_CHILDREN(rack_timely), 1326 OID_AUTO, "nonstop", CTLFLAG_RW, 1327 &rack_timely_no_stopping, 0, 1328 "Rack timely don't stop increase"); 1329 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1330 SYSCTL_CHILDREN(rack_timely), 1331 OID_AUTO, "dec_raise_thresh", CTLFLAG_RW, 1332 &rack_down_raise_thresh, 100, 1333 "If the CA or SS is below this threshold raise on the first 3 b/w lowers (0=always)"); 1334 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1335 SYSCTL_CHILDREN(rack_timely), 1336 OID_AUTO, "bottom_drag_segs", CTLFLAG_RW, 1337 &rack_req_segs, 1, 1338 "Bottom dragging if not these many segments outstanding and room"); 1339 1340 /* TLP and Rack related parameters */ 1341 rack_tlp = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1342 SYSCTL_CHILDREN(rack_sysctl_root), 1343 OID_AUTO, 1344 "tlp", 1345 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1346 "TLP and Rack related Controls"); 1347 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1348 SYSCTL_CHILDREN(rack_tlp), 1349 OID_AUTO, "use_rrr", CTLFLAG_RW, 1350 &use_rack_rr, 1, 1351 "Do we use Rack Rapid Recovery"); 1352 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1353 SYSCTL_CHILDREN(rack_tlp), 1354 OID_AUTO, "post_rec_labc", CTLFLAG_RW, 1355 &rack_max_abc_post_recovery, 2, 1356 "Since we do early recovery, do we override the l_abc to a value, if so what?"); 1357 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1358 SYSCTL_CHILDREN(rack_tlp), 1359 OID_AUTO, "nonrxt_use_cr", CTLFLAG_RW, 1360 &rack_non_rxt_use_cr, 0, 1361 "Do we use ss/ca rate if in recovery we are transmitting a new data chunk"); 1362 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1363 SYSCTL_CHILDREN(rack_tlp), 1364 OID_AUTO, "tlpmethod", CTLFLAG_RW, 1365 &rack_tlp_threshold_use, TLP_USE_TWO_ONE, 1366 "What method do we do for TLP time calc 0=no-de-ack-comp, 1=ID, 2=2.1, 3=2.2"); 1367 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1368 SYSCTL_CHILDREN(rack_tlp), 1369 OID_AUTO, "limit", CTLFLAG_RW, 1370 &rack_tlp_limit, 2, 1371 "How many TLP's can be sent without sending new data"); 1372 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1373 SYSCTL_CHILDREN(rack_tlp), 1374 OID_AUTO, "use_greater", CTLFLAG_RW, 1375 &rack_tlp_use_greater, 1, 1376 "Should we use the rack_rtt time if its greater than srtt"); 1377 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1378 SYSCTL_CHILDREN(rack_tlp), 1379 OID_AUTO, "tlpminto", CTLFLAG_RW, 1380 &rack_tlp_min, 10000, 1381 "TLP minimum timeout per the specification (in microseconds)"); 1382 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1383 SYSCTL_CHILDREN(rack_tlp), 1384 OID_AUTO, "send_oldest", CTLFLAG_RW, 1385 &rack_always_send_oldest, 0, 1386 "Should we always send the oldest TLP and RACK-TLP"); 1387 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1388 SYSCTL_CHILDREN(rack_tlp), 1389 OID_AUTO, "tlp_cwnd_flag", CTLFLAG_RW, 1390 &rack_lower_cwnd_at_tlp, 0, 1391 "When a TLP completes a retran should we enter recovery"); 1392 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1393 SYSCTL_CHILDREN(rack_tlp), 1394 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1395 &rack_reorder_thresh, 2, 1396 "What factor for rack will be added when seeing reordering (shift right)"); 1397 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1398 SYSCTL_CHILDREN(rack_tlp), 1399 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1400 &rack_tlp_thresh, 1, 1401 "What divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1402 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1403 SYSCTL_CHILDREN(rack_tlp), 1404 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1405 &rack_reorder_fade, 60000000, 1406 "Does reorder detection fade, if so how many microseconds (0 means never)"); 1407 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1408 SYSCTL_CHILDREN(rack_tlp), 1409 OID_AUTO, "pktdelay", CTLFLAG_RW, 1410 &rack_pkt_delay, 1000, 1411 "Extra RACK time (in microseconds) besides reordering thresh"); 1412 1413 /* Timer related controls */ 1414 rack_timers = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1415 SYSCTL_CHILDREN(rack_sysctl_root), 1416 OID_AUTO, 1417 "timers", 1418 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1419 "Timer related controls"); 1420 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1421 SYSCTL_CHILDREN(rack_timers), 1422 OID_AUTO, "reset_ssth_rec_rto", CTLFLAG_RW, 1423 &rack_ssthresh_rest_rto_rec, 0, 1424 "When doing recovery -> rto -> recovery do we reset SSthresh?"); 1425 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1426 SYSCTL_CHILDREN(rack_timers), 1427 OID_AUTO, "scoreboard_thresh", CTLFLAG_RW, 1428 &rack_rxt_scoreboard_clear_thresh, 2, 1429 "How many RTO's are allowed before we clear the scoreboard"); 1430 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1431 SYSCTL_CHILDREN(rack_timers), 1432 OID_AUTO, "honor_hpts_min", CTLFLAG_RW, 1433 &rack_honors_hpts_min_to, 1, 1434 "Do rack pacing timers honor hpts min timeout"); 1435 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1436 SYSCTL_CHILDREN(rack_timers), 1437 OID_AUTO, "hpts_max_reduce", CTLFLAG_RW, 1438 &rack_max_reduce, 10, 1439 "Max percentage we will reduce slot by for pacing when we are behind"); 1440 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1441 SYSCTL_CHILDREN(rack_timers), 1442 OID_AUTO, "persmin", CTLFLAG_RW, 1443 &rack_persist_min, 250000, 1444 "What is the minimum time in microseconds between persists"); 1445 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1446 SYSCTL_CHILDREN(rack_timers), 1447 OID_AUTO, "persmax", CTLFLAG_RW, 1448 &rack_persist_max, 2000000, 1449 "What is the largest delay in microseconds between persists"); 1450 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1451 SYSCTL_CHILDREN(rack_timers), 1452 OID_AUTO, "delayed_ack", CTLFLAG_RW, 1453 &rack_delayed_ack_time, 40000, 1454 "Delayed ack time (40ms in microseconds)"); 1455 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1456 SYSCTL_CHILDREN(rack_timers), 1457 OID_AUTO, "minrto", CTLFLAG_RW, 1458 &rack_rto_min, 30000, 1459 "Minimum RTO in microseconds -- set with caution below 1000 due to TLP"); 1460 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1461 SYSCTL_CHILDREN(rack_timers), 1462 OID_AUTO, "maxrto", CTLFLAG_RW, 1463 &rack_rto_max, 4000000, 1464 "Maximum RTO in microseconds -- should be at least as large as min_rto"); 1465 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1466 SYSCTL_CHILDREN(rack_timers), 1467 OID_AUTO, "minto", CTLFLAG_RW, 1468 &rack_min_to, 1000, 1469 "Minimum rack timeout in microseconds"); 1470 /* Measure controls */ 1471 rack_measure = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1472 SYSCTL_CHILDREN(rack_sysctl_root), 1473 OID_AUTO, 1474 "measure", 1475 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1476 "Measure related controls"); 1477 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1478 SYSCTL_CHILDREN(rack_measure), 1479 OID_AUTO, "wma_divisor", CTLFLAG_RW, 1480 &rack_wma_divisor, 8, 1481 "When doing b/w calculation what is the divisor for the WMA"); 1482 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1483 SYSCTL_CHILDREN(rack_measure), 1484 OID_AUTO, "end_cwnd", CTLFLAG_RW, 1485 &rack_cwnd_block_ends_measure, 0, 1486 "Does a cwnd just-return end the measurement window (app limited)"); 1487 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1488 SYSCTL_CHILDREN(rack_measure), 1489 OID_AUTO, "end_rwnd", CTLFLAG_RW, 1490 &rack_rwnd_block_ends_measure, 0, 1491 "Does an rwnd just-return end the measurement window (app limited -- not persists)"); 1492 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1493 SYSCTL_CHILDREN(rack_measure), 1494 OID_AUTO, "min_target", CTLFLAG_RW, 1495 &rack_def_data_window, 20, 1496 "What is the minimum target window (in mss) for a GP measurements"); 1497 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1498 SYSCTL_CHILDREN(rack_measure), 1499 OID_AUTO, "goal_bdp", CTLFLAG_RW, 1500 &rack_goal_bdp, 2, 1501 "What is the goal BDP to measure"); 1502 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1503 SYSCTL_CHILDREN(rack_measure), 1504 OID_AUTO, "min_srtts", CTLFLAG_RW, 1505 &rack_min_srtts, 1, 1506 "What is the goal BDP to measure"); 1507 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1508 SYSCTL_CHILDREN(rack_measure), 1509 OID_AUTO, "min_measure_tim", CTLFLAG_RW, 1510 &rack_min_measure_usec, 0, 1511 "What is the Minimum time time for a measurement if 0, this is off"); 1512 /* Features */ 1513 rack_features = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1514 SYSCTL_CHILDREN(rack_sysctl_root), 1515 OID_AUTO, 1516 "features", 1517 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1518 "Feature controls"); 1519 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1520 SYSCTL_CHILDREN(rack_features), 1521 OID_AUTO, "hybrid_set_maxseg", CTLFLAG_RW, 1522 &rack_hybrid_allow_set_maxseg, 0, 1523 "Should hybrid pacing allow the setmss command"); 1524 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1525 SYSCTL_CHILDREN(rack_features), 1526 OID_AUTO, "cmpack", CTLFLAG_RW, 1527 &rack_use_cmp_acks, 1, 1528 "Should RACK have LRO send compressed acks"); 1529 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1530 SYSCTL_CHILDREN(rack_features), 1531 OID_AUTO, "fsb", CTLFLAG_RW, 1532 &rack_use_fsb, 1, 1533 "Should RACK use the fast send block?"); 1534 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1535 SYSCTL_CHILDREN(rack_features), 1536 OID_AUTO, "rfo", CTLFLAG_RW, 1537 &rack_use_rfo, 1, 1538 "Should RACK use rack_fast_output()?"); 1539 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1540 SYSCTL_CHILDREN(rack_features), 1541 OID_AUTO, "rsmrfo", CTLFLAG_RW, 1542 &rack_use_rsm_rfo, 1, 1543 "Should RACK use rack_fast_rsm_output()?"); 1544 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1545 SYSCTL_CHILDREN(rack_features), 1546 OID_AUTO, "non_paced_lro_queue", CTLFLAG_RW, 1547 &rack_enable_mqueue_for_nonpaced, 0, 1548 "Should RACK use mbuf queuing for non-paced connections"); 1549 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1550 SYSCTL_CHILDREN(rack_features), 1551 OID_AUTO, "hystartplusplus", CTLFLAG_RW, 1552 &rack_do_hystart, 0, 1553 "Should RACK enable HyStart++ on connections?"); 1554 /* Policer detection */ 1555 rack_policing = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1556 SYSCTL_CHILDREN(rack_sysctl_root), 1557 OID_AUTO, 1558 "policing", 1559 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1560 "policer detection"); 1561 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1562 SYSCTL_CHILDREN(rack_policing), 1563 OID_AUTO, "rxt_thresh", CTLFLAG_RW, 1564 &rack_policer_rxt_thresh, 0, 1565 "Percentage of retransmits we need to be a possible policer (499 = 49.9 percent)"); 1566 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1567 SYSCTL_CHILDREN(rack_policing), 1568 OID_AUTO, "avg_thresh", CTLFLAG_RW, 1569 &rack_policer_avg_thresh, 0, 1570 "What threshold of average retransmits needed to recover a lost packet (1 - 169 aka 21 = 2.1)?"); 1571 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1572 SYSCTL_CHILDREN(rack_policing), 1573 OID_AUTO, "med_thresh", CTLFLAG_RW, 1574 &rack_policer_med_thresh, 0, 1575 "What threshold of Median retransmits needed to recover a lost packet (1 - 16)?"); 1576 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1577 SYSCTL_CHILDREN(rack_policing), 1578 OID_AUTO, "data_thresh", CTLFLAG_RW, 1579 &rack_policer_data_thresh, 64000, 1580 "How many bytes must have gotten through before we can start doing policer detection?"); 1581 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1582 SYSCTL_CHILDREN(rack_policing), 1583 OID_AUTO, "bwcomp", CTLFLAG_RW, 1584 &rack_policing_do_bw_comp, 1, 1585 "Do we raise up low b/w so that at least pace_max_seg can be sent in the srtt?"); 1586 SYSCTL_ADD_U8(&rack_sysctl_ctx, 1587 SYSCTL_CHILDREN(rack_policing), 1588 OID_AUTO, "recmss", CTLFLAG_RW, 1589 &rack_req_del_mss, 18, 1590 "How many MSS must be delivered during recovery to engage policer detection?"); 1591 SYSCTL_ADD_U16(&rack_sysctl_ctx, 1592 SYSCTL_CHILDREN(rack_policing), 1593 OID_AUTO, "res_div", CTLFLAG_RW, 1594 &rack_policer_bucket_reserve, 20, 1595 "What percentage is reserved in the policer bucket?"); 1596 SYSCTL_ADD_U64(&rack_sysctl_ctx, 1597 SYSCTL_CHILDREN(rack_policing), 1598 OID_AUTO, "min_comp_bw", CTLFLAG_RW, 1599 &rack_pol_min_bw, 125000, 1600 "Do we have a min b/w for b/w compensation (0 = no)?"); 1601 /* Misc rack controls */ 1602 rack_misc = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 1603 SYSCTL_CHILDREN(rack_sysctl_root), 1604 OID_AUTO, 1605 "misc", 1606 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1607 "Misc related controls"); 1608 #ifdef TCP_ACCOUNTING 1609 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1610 SYSCTL_CHILDREN(rack_misc), 1611 OID_AUTO, "tcp_acct", CTLFLAG_RW, 1612 &rack_tcp_accounting, 0, 1613 "Should we turn on TCP accounting for all rack sessions?"); 1614 #endif 1615 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1616 SYSCTL_CHILDREN(rack_misc), 1617 OID_AUTO, "dnd", CTLFLAG_RW, 1618 &rack_dnd_default, 0, 1619 "Do not disturb default for rack_rrr = 3"); 1620 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1621 SYSCTL_CHILDREN(rack_misc), 1622 OID_AUTO, "sad_seg_per", CTLFLAG_RW, 1623 &sad_seg_size_per, 800, 1624 "Percentage of segment size needed in a sack 800 = 80.0?"); 1625 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1626 SYSCTL_CHILDREN(rack_misc), 1627 OID_AUTO, "rxt_controls", CTLFLAG_RW, 1628 &rack_rxt_controls, 0, 1629 "Retransmit sending size controls (valid values 0, 1, 2 default=1)?"); 1630 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1631 SYSCTL_CHILDREN(rack_misc), 1632 OID_AUTO, "rack_hibeta", CTLFLAG_RW, 1633 &rack_hibeta_setting, 0, 1634 "Do we ue a high beta (80 instead of 50)?"); 1635 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1636 SYSCTL_CHILDREN(rack_misc), 1637 OID_AUTO, "apply_rtt_with_low_conf", CTLFLAG_RW, 1638 &rack_apply_rtt_with_reduced_conf, 0, 1639 "When a persist or keep-alive probe is not answered do we calculate rtt on subsequent answers?"); 1640 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1641 SYSCTL_CHILDREN(rack_misc), 1642 OID_AUTO, "rack_dsack_ctl", CTLFLAG_RW, 1643 &rack_dsack_std_based, 3, 1644 "How do we process dsack with respect to rack timers, bit field, 3 is standards based?"); 1645 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1646 SYSCTL_CHILDREN(rack_misc), 1647 OID_AUTO, "prr_addback_max", CTLFLAG_RW, 1648 &rack_prr_addbackmax, 2, 1649 "What is the maximum number of MSS we allow to be added back if prr can't send all its data?"); 1650 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1651 SYSCTL_CHILDREN(rack_misc), 1652 OID_AUTO, "stats_gets_ms", CTLFLAG_RW, 1653 &rack_stats_gets_ms_rtt, 1, 1654 "What do we feed the stats framework (1 = ms_rtt, 0 = us_rtt, 2 = ms_rtt from hdwr, > 2 usec rtt from hdwr)?"); 1655 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1656 SYSCTL_CHILDREN(rack_misc), 1657 OID_AUTO, "clientlowbuf", CTLFLAG_RW, 1658 &rack_client_low_buf, 0, 1659 "Client low buffer level (below this we are more aggressive in DGP exiting recovery (0 = off)?"); 1660 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1661 SYSCTL_CHILDREN(rack_misc), 1662 OID_AUTO, "defprofile", CTLFLAG_RW, 1663 &rack_def_profile, 0, 1664 "Should RACK use a default profile (0=no, num == profile num)?"); 1665 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1666 SYSCTL_CHILDREN(rack_misc), 1667 OID_AUTO, "shared_cwnd", CTLFLAG_RW, 1668 &rack_enable_shared_cwnd, 1, 1669 "Should RACK try to use the shared cwnd on connections where allowed"); 1670 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1671 SYSCTL_CHILDREN(rack_misc), 1672 OID_AUTO, "limits_on_scwnd", CTLFLAG_RW, 1673 &rack_limits_scwnd, 1, 1674 "Should RACK place low end time limits on the shared cwnd feature"); 1675 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1676 SYSCTL_CHILDREN(rack_misc), 1677 OID_AUTO, "no_prr", CTLFLAG_RW, 1678 &rack_disable_prr, 0, 1679 "Should RACK not use prr and only pace (must have pacing on)"); 1680 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1681 SYSCTL_CHILDREN(rack_misc), 1682 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1683 &rack_verbose_logging, 0, 1684 "Should RACK black box logging be verbose"); 1685 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1686 SYSCTL_CHILDREN(rack_misc), 1687 OID_AUTO, "data_after_close", CTLFLAG_RW, 1688 &rack_ignore_data_after_close, 1, 1689 "Do we hold off sending a RST until all pending data is ack'd"); 1690 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1691 SYSCTL_CHILDREN(rack_misc), 1692 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1693 &rack_sack_not_required, 1, 1694 "Do we allow rack to run on connections not supporting SACK"); 1695 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1696 SYSCTL_CHILDREN(rack_misc), 1697 OID_AUTO, "prr_sendalot", CTLFLAG_RW, 1698 &rack_send_a_lot_in_prr, 1, 1699 "Send a lot in prr"); 1700 SYSCTL_ADD_S32(&rack_sysctl_ctx, 1701 SYSCTL_CHILDREN(rack_misc), 1702 OID_AUTO, "autoscale", CTLFLAG_RW, 1703 &rack_autosndbuf_inc, 20, 1704 "What percentage should rack scale up its snd buffer by?"); 1705 1706 1707 /* Sack Attacker detection stuff */ 1708 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1709 SYSCTL_CHILDREN(rack_attack), 1710 OID_AUTO, "merge_out", CTLFLAG_RW, 1711 &rack_merge_out_sacks_on_attack, 0, 1712 "Do we merge the sendmap when we decide we are being attacked?"); 1713 1714 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1715 SYSCTL_CHILDREN(rack_attack), 1716 OID_AUTO, "detect_highsackratio", CTLFLAG_RW, 1717 &rack_highest_sack_thresh_seen, 0, 1718 "Highest sack to ack ratio seen"); 1719 SYSCTL_ADD_U32(&rack_sysctl_ctx, 1720 SYSCTL_CHILDREN(rack_attack), 1721 OID_AUTO, "detect_highmoveratio", CTLFLAG_RW, 1722 &rack_highest_move_thresh_seen, 0, 1723 "Highest move to non-move ratio seen"); 1724 rack_ack_total = counter_u64_alloc(M_WAITOK); 1725 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1726 SYSCTL_CHILDREN(rack_attack), 1727 OID_AUTO, "acktotal", CTLFLAG_RD, 1728 &rack_ack_total, 1729 "Total number of Ack's"); 1730 rack_express_sack = counter_u64_alloc(M_WAITOK); 1731 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1732 SYSCTL_CHILDREN(rack_attack), 1733 OID_AUTO, "exp_sacktotal", CTLFLAG_RD, 1734 &rack_express_sack, 1735 "Total expresss number of Sack's"); 1736 rack_sack_total = counter_u64_alloc(M_WAITOK); 1737 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1738 SYSCTL_CHILDREN(rack_attack), 1739 OID_AUTO, "sacktotal", CTLFLAG_RD, 1740 &rack_sack_total, 1741 "Total number of SACKs"); 1742 rack_move_none = counter_u64_alloc(M_WAITOK); 1743 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1744 SYSCTL_CHILDREN(rack_attack), 1745 OID_AUTO, "move_none", CTLFLAG_RD, 1746 &rack_move_none, 1747 "Total number of SACK index reuse of positions under threshold"); 1748 rack_move_some = counter_u64_alloc(M_WAITOK); 1749 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1750 SYSCTL_CHILDREN(rack_attack), 1751 OID_AUTO, "move_some", CTLFLAG_RD, 1752 &rack_move_some, 1753 "Total number of SACK index reuse of positions over threshold"); 1754 rack_sack_attacks_detected = counter_u64_alloc(M_WAITOK); 1755 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1756 SYSCTL_CHILDREN(rack_attack), 1757 OID_AUTO, "attacks", CTLFLAG_RD, 1758 &rack_sack_attacks_detected, 1759 "Total number of SACK attackers that had sack disabled"); 1760 rack_sack_attacks_reversed = counter_u64_alloc(M_WAITOK); 1761 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1762 SYSCTL_CHILDREN(rack_attack), 1763 OID_AUTO, "reversed", CTLFLAG_RD, 1764 &rack_sack_attacks_reversed, 1765 "Total number of SACK attackers that were later determined false positive"); 1766 rack_sack_attacks_suspect = counter_u64_alloc(M_WAITOK); 1767 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1768 SYSCTL_CHILDREN(rack_attack), 1769 OID_AUTO, "suspect", CTLFLAG_RD, 1770 &rack_sack_attacks_suspect, 1771 "Total number of SACKs that triggered early detection"); 1772 1773 rack_sack_used_next_merge = counter_u64_alloc(M_WAITOK); 1774 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1775 SYSCTL_CHILDREN(rack_attack), 1776 OID_AUTO, "nextmerge", CTLFLAG_RD, 1777 &rack_sack_used_next_merge, 1778 "Total number of times we used the next merge"); 1779 rack_sack_used_prev_merge = counter_u64_alloc(M_WAITOK); 1780 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1781 SYSCTL_CHILDREN(rack_attack), 1782 OID_AUTO, "prevmerge", CTLFLAG_RD, 1783 &rack_sack_used_prev_merge, 1784 "Total number of times we used the prev merge"); 1785 /* Counters */ 1786 rack_total_bytes = counter_u64_alloc(M_WAITOK); 1787 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1788 SYSCTL_CHILDREN(rack_counters), 1789 OID_AUTO, "totalbytes", CTLFLAG_RD, 1790 &rack_total_bytes, 1791 "Total number of bytes sent"); 1792 rack_fto_send = counter_u64_alloc(M_WAITOK); 1793 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1794 SYSCTL_CHILDREN(rack_counters), 1795 OID_AUTO, "fto_send", CTLFLAG_RD, 1796 &rack_fto_send, "Total number of rack_fast_output sends"); 1797 rack_fto_rsm_send = counter_u64_alloc(M_WAITOK); 1798 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1799 SYSCTL_CHILDREN(rack_counters), 1800 OID_AUTO, "fto_rsm_send", CTLFLAG_RD, 1801 &rack_fto_rsm_send, "Total number of rack_fast_rsm_output sends"); 1802 rack_nfto_resend = counter_u64_alloc(M_WAITOK); 1803 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1804 SYSCTL_CHILDREN(rack_counters), 1805 OID_AUTO, "nfto_resend", CTLFLAG_RD, 1806 &rack_nfto_resend, "Total number of rack_output retransmissions"); 1807 rack_non_fto_send = counter_u64_alloc(M_WAITOK); 1808 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1809 SYSCTL_CHILDREN(rack_counters), 1810 OID_AUTO, "nfto_send", CTLFLAG_RD, 1811 &rack_non_fto_send, "Total number of rack_output first sends"); 1812 rack_extended_rfo = counter_u64_alloc(M_WAITOK); 1813 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1814 SYSCTL_CHILDREN(rack_counters), 1815 OID_AUTO, "rfo_extended", CTLFLAG_RD, 1816 &rack_extended_rfo, "Total number of times we extended rfo"); 1817 1818 rack_hw_pace_init_fail = counter_u64_alloc(M_WAITOK); 1819 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1820 SYSCTL_CHILDREN(rack_counters), 1821 OID_AUTO, "hwpace_init_fail", CTLFLAG_RD, 1822 &rack_hw_pace_init_fail, "Total number of times we failed to initialize hw pacing"); 1823 rack_hw_pace_lost = counter_u64_alloc(M_WAITOK); 1824 1825 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1826 SYSCTL_CHILDREN(rack_counters), 1827 OID_AUTO, "hwpace_lost", CTLFLAG_RD, 1828 &rack_hw_pace_lost, "Total number of times we failed to initialize hw pacing"); 1829 rack_tlp_tot = counter_u64_alloc(M_WAITOK); 1830 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1831 SYSCTL_CHILDREN(rack_counters), 1832 OID_AUTO, "tlp_to_total", CTLFLAG_RD, 1833 &rack_tlp_tot, 1834 "Total number of tail loss probe expirations"); 1835 rack_tlp_newdata = counter_u64_alloc(M_WAITOK); 1836 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1837 SYSCTL_CHILDREN(rack_counters), 1838 OID_AUTO, "tlp_new", CTLFLAG_RD, 1839 &rack_tlp_newdata, 1840 "Total number of tail loss probe sending new data"); 1841 rack_tlp_retran = counter_u64_alloc(M_WAITOK); 1842 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1843 SYSCTL_CHILDREN(rack_counters), 1844 OID_AUTO, "tlp_retran", CTLFLAG_RD, 1845 &rack_tlp_retran, 1846 "Total number of tail loss probe sending retransmitted data"); 1847 rack_tlp_retran_bytes = counter_u64_alloc(M_WAITOK); 1848 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1849 SYSCTL_CHILDREN(rack_counters), 1850 OID_AUTO, "tlp_retran_bytes", CTLFLAG_RD, 1851 &rack_tlp_retran_bytes, 1852 "Total bytes of tail loss probe sending retransmitted data"); 1853 rack_to_tot = counter_u64_alloc(M_WAITOK); 1854 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1855 SYSCTL_CHILDREN(rack_counters), 1856 OID_AUTO, "rack_to_tot", CTLFLAG_RD, 1857 &rack_to_tot, 1858 "Total number of times the rack to expired"); 1859 rack_saw_enobuf = counter_u64_alloc(M_WAITOK); 1860 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1861 SYSCTL_CHILDREN(rack_counters), 1862 OID_AUTO, "saw_enobufs", CTLFLAG_RD, 1863 &rack_saw_enobuf, 1864 "Total number of times a sends returned enobuf for non-hdwr paced connections"); 1865 rack_saw_enobuf_hw = counter_u64_alloc(M_WAITOK); 1866 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1867 SYSCTL_CHILDREN(rack_counters), 1868 OID_AUTO, "saw_enobufs_hw", CTLFLAG_RD, 1869 &rack_saw_enobuf_hw, 1870 "Total number of times a send returned enobuf for hdwr paced connections"); 1871 rack_saw_enetunreach = counter_u64_alloc(M_WAITOK); 1872 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1873 SYSCTL_CHILDREN(rack_counters), 1874 OID_AUTO, "saw_enetunreach", CTLFLAG_RD, 1875 &rack_saw_enetunreach, 1876 "Total number of times a send received a enetunreachable"); 1877 rack_hot_alloc = counter_u64_alloc(M_WAITOK); 1878 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1879 SYSCTL_CHILDREN(rack_counters), 1880 OID_AUTO, "alloc_hot", CTLFLAG_RD, 1881 &rack_hot_alloc, 1882 "Total allocations from the top of our list"); 1883 tcp_policer_detected = counter_u64_alloc(M_WAITOK); 1884 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1885 SYSCTL_CHILDREN(rack_counters), 1886 OID_AUTO, "policer_detected", CTLFLAG_RD, 1887 &tcp_policer_detected, 1888 "Total policer_detections"); 1889 1890 rack_to_alloc = counter_u64_alloc(M_WAITOK); 1891 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1892 SYSCTL_CHILDREN(rack_counters), 1893 OID_AUTO, "allocs", CTLFLAG_RD, 1894 &rack_to_alloc, 1895 "Total allocations of tracking structures"); 1896 rack_to_alloc_hard = counter_u64_alloc(M_WAITOK); 1897 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1898 SYSCTL_CHILDREN(rack_counters), 1899 OID_AUTO, "allochard", CTLFLAG_RD, 1900 &rack_to_alloc_hard, 1901 "Total allocations done with sleeping the hard way"); 1902 rack_to_alloc_emerg = counter_u64_alloc(M_WAITOK); 1903 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1904 SYSCTL_CHILDREN(rack_counters), 1905 OID_AUTO, "allocemerg", CTLFLAG_RD, 1906 &rack_to_alloc_emerg, 1907 "Total allocations done from emergency cache"); 1908 rack_to_alloc_limited = counter_u64_alloc(M_WAITOK); 1909 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1910 SYSCTL_CHILDREN(rack_counters), 1911 OID_AUTO, "alloc_limited", CTLFLAG_RD, 1912 &rack_to_alloc_limited, 1913 "Total allocations dropped due to limit"); 1914 rack_alloc_limited_conns = counter_u64_alloc(M_WAITOK); 1915 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1916 SYSCTL_CHILDREN(rack_counters), 1917 OID_AUTO, "alloc_limited_conns", CTLFLAG_RD, 1918 &rack_alloc_limited_conns, 1919 "Connections with allocations dropped due to limit"); 1920 rack_split_limited = counter_u64_alloc(M_WAITOK); 1921 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1922 SYSCTL_CHILDREN(rack_counters), 1923 OID_AUTO, "split_limited", CTLFLAG_RD, 1924 &rack_split_limited, 1925 "Split allocations dropped due to limit"); 1926 rack_rxt_clamps_cwnd = counter_u64_alloc(M_WAITOK); 1927 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1928 SYSCTL_CHILDREN(rack_counters), 1929 OID_AUTO, "rxt_clamps_cwnd", CTLFLAG_RD, 1930 &rack_rxt_clamps_cwnd, 1931 "Number of times that excessive rxt clamped the cwnd down"); 1932 rack_rxt_clamps_cwnd_uniq = counter_u64_alloc(M_WAITOK); 1933 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1934 SYSCTL_CHILDREN(rack_counters), 1935 OID_AUTO, "rxt_clamps_cwnd_uniq", CTLFLAG_RD, 1936 &rack_rxt_clamps_cwnd_uniq, 1937 "Number of connections that have had excessive rxt clamped the cwnd down"); 1938 rack_persists_sends = counter_u64_alloc(M_WAITOK); 1939 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1940 SYSCTL_CHILDREN(rack_counters), 1941 OID_AUTO, "persist_sends", CTLFLAG_RD, 1942 &rack_persists_sends, 1943 "Number of times we sent a persist probe"); 1944 rack_persists_acks = counter_u64_alloc(M_WAITOK); 1945 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1946 SYSCTL_CHILDREN(rack_counters), 1947 OID_AUTO, "persist_acks", CTLFLAG_RD, 1948 &rack_persists_acks, 1949 "Number of times a persist probe was acked"); 1950 rack_persists_loss = counter_u64_alloc(M_WAITOK); 1951 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1952 SYSCTL_CHILDREN(rack_counters), 1953 OID_AUTO, "persist_loss", CTLFLAG_RD, 1954 &rack_persists_loss, 1955 "Number of times we detected a lost persist probe (no ack)"); 1956 rack_persists_lost_ends = counter_u64_alloc(M_WAITOK); 1957 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1958 SYSCTL_CHILDREN(rack_counters), 1959 OID_AUTO, "persist_loss_ends", CTLFLAG_RD, 1960 &rack_persists_lost_ends, 1961 "Number of lost persist probe (no ack) that the run ended with a PERSIST abort"); 1962 #ifdef INVARIANTS 1963 rack_adjust_map_bw = counter_u64_alloc(M_WAITOK); 1964 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1965 SYSCTL_CHILDREN(rack_counters), 1966 OID_AUTO, "map_adjust_req", CTLFLAG_RD, 1967 &rack_adjust_map_bw, 1968 "Number of times we hit the case where the sb went up and down on a sendmap entry"); 1969 #endif 1970 rack_multi_single_eq = counter_u64_alloc(M_WAITOK); 1971 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1972 SYSCTL_CHILDREN(rack_counters), 1973 OID_AUTO, "cmp_ack_equiv", CTLFLAG_RD, 1974 &rack_multi_single_eq, 1975 "Number of compressed acks total represented"); 1976 rack_proc_non_comp_ack = counter_u64_alloc(M_WAITOK); 1977 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1978 SYSCTL_CHILDREN(rack_counters), 1979 OID_AUTO, "cmp_ack_not", CTLFLAG_RD, 1980 &rack_proc_non_comp_ack, 1981 "Number of non compresseds acks that we processed"); 1982 1983 1984 rack_sack_proc_all = counter_u64_alloc(M_WAITOK); 1985 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1986 SYSCTL_CHILDREN(rack_counters), 1987 OID_AUTO, "sack_long", CTLFLAG_RD, 1988 &rack_sack_proc_all, 1989 "Total times we had to walk whole list for sack processing"); 1990 rack_sack_proc_restart = counter_u64_alloc(M_WAITOK); 1991 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1992 SYSCTL_CHILDREN(rack_counters), 1993 OID_AUTO, "sack_restart", CTLFLAG_RD, 1994 &rack_sack_proc_restart, 1995 "Total times we had to walk whole list due to a restart"); 1996 rack_sack_proc_short = counter_u64_alloc(M_WAITOK); 1997 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 1998 SYSCTL_CHILDREN(rack_counters), 1999 OID_AUTO, "sack_short", CTLFLAG_RD, 2000 &rack_sack_proc_short, 2001 "Total times we took shortcut for sack processing"); 2002 rack_sack_skipped_acked = counter_u64_alloc(M_WAITOK); 2003 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2004 SYSCTL_CHILDREN(rack_attack), 2005 OID_AUTO, "skipacked", CTLFLAG_RD, 2006 &rack_sack_skipped_acked, 2007 "Total number of times we skipped previously sacked"); 2008 rack_sack_splits = counter_u64_alloc(M_WAITOK); 2009 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2010 SYSCTL_CHILDREN(rack_attack), 2011 OID_AUTO, "ofsplit", CTLFLAG_RD, 2012 &rack_sack_splits, 2013 "Total number of times we did the old fashion tree split"); 2014 rack_input_idle_reduces = counter_u64_alloc(M_WAITOK); 2015 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2016 SYSCTL_CHILDREN(rack_counters), 2017 OID_AUTO, "idle_reduce_oninput", CTLFLAG_RD, 2018 &rack_input_idle_reduces, 2019 "Total number of idle reductions on input"); 2020 rack_collapsed_win_seen = counter_u64_alloc(M_WAITOK); 2021 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2022 SYSCTL_CHILDREN(rack_counters), 2023 OID_AUTO, "collapsed_win_seen", CTLFLAG_RD, 2024 &rack_collapsed_win_seen, 2025 "Total number of collapsed window events seen (where our window shrinks)"); 2026 2027 rack_collapsed_win = counter_u64_alloc(M_WAITOK); 2028 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2029 SYSCTL_CHILDREN(rack_counters), 2030 OID_AUTO, "collapsed_win", CTLFLAG_RD, 2031 &rack_collapsed_win, 2032 "Total number of collapsed window events where we mark packets"); 2033 rack_collapsed_win_rxt = counter_u64_alloc(M_WAITOK); 2034 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2035 SYSCTL_CHILDREN(rack_counters), 2036 OID_AUTO, "collapsed_win_rxt", CTLFLAG_RD, 2037 &rack_collapsed_win_rxt, 2038 "Total number of packets that were retransmitted"); 2039 rack_collapsed_win_rxt_bytes = counter_u64_alloc(M_WAITOK); 2040 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2041 SYSCTL_CHILDREN(rack_counters), 2042 OID_AUTO, "collapsed_win_bytes", CTLFLAG_RD, 2043 &rack_collapsed_win_rxt_bytes, 2044 "Total number of bytes that were retransmitted"); 2045 rack_try_scwnd = counter_u64_alloc(M_WAITOK); 2046 SYSCTL_ADD_COUNTER_U64(&rack_sysctl_ctx, 2047 SYSCTL_CHILDREN(rack_counters), 2048 OID_AUTO, "tried_scwnd", CTLFLAG_RD, 2049 &rack_try_scwnd, 2050 "Total number of scwnd attempts"); 2051 COUNTER_ARRAY_ALLOC(rack_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 2052 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 2053 OID_AUTO, "outsize", CTLFLAG_RD, 2054 rack_out_size, TCP_MSS_ACCT_SIZE, "MSS send sizes"); 2055 COUNTER_ARRAY_ALLOC(rack_opts_arry, RACK_OPTS_SIZE, M_WAITOK); 2056 SYSCTL_ADD_COUNTER_U64_ARRAY(&rack_sysctl_ctx, SYSCTL_CHILDREN(rack_sysctl_root), 2057 OID_AUTO, "opts", CTLFLAG_RD, 2058 rack_opts_arry, RACK_OPTS_SIZE, "RACK Option Stats"); 2059 SYSCTL_ADD_PROC(&rack_sysctl_ctx, 2060 SYSCTL_CHILDREN(rack_sysctl_root), 2061 OID_AUTO, "clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2062 &rack_clear_counter, 0, sysctl_rack_clear, "IU", "Clear counters"); 2063 } 2064 2065 static uint32_t 2066 rc_init_window(struct tcp_rack *rack) 2067 { 2068 return (tcp_compute_initwnd(tcp_maxseg(rack->rc_tp))); 2069 2070 } 2071 2072 static uint64_t 2073 rack_get_fixed_pacing_bw(struct tcp_rack *rack) 2074 { 2075 if (IN_FASTRECOVERY(rack->rc_tp->t_flags)) 2076 return (rack->r_ctl.rc_fixed_pacing_rate_rec); 2077 else if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) 2078 return (rack->r_ctl.rc_fixed_pacing_rate_ss); 2079 else 2080 return (rack->r_ctl.rc_fixed_pacing_rate_ca); 2081 } 2082 2083 static void 2084 rack_log_hybrid_bw(struct tcp_rack *rack, uint32_t seq, uint64_t cbw, uint64_t tim, 2085 uint64_t data, uint8_t mod, uint16_t aux, 2086 struct tcp_sendfile_track *cur, int line) 2087 { 2088 #ifdef TCP_REQUEST_TRK 2089 int do_log = 0; 2090 2091 /* 2092 * The rate cap one is noisy and only should come out when normal BB logging 2093 * is enabled, the other logs (not RATE_CAP and NOT CAP_CALC) only come out 2094 * once per chunk and make up the BBpoint that can be turned on by the client. 2095 */ 2096 if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) { 2097 /* 2098 * The very noisy two need to only come out when 2099 * we have verbose logging on. 2100 */ 2101 if (rack_verbose_logging != 0) 2102 do_log = tcp_bblogging_on(rack->rc_tp); 2103 else 2104 do_log = 0; 2105 } else if (mod != HYBRID_LOG_BW_MEASURE) { 2106 /* 2107 * All other less noisy logs here except the measure which 2108 * also needs to come out on the point and the log. 2109 */ 2110 do_log = tcp_bblogging_on(rack->rc_tp); 2111 } else { 2112 do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING); 2113 } 2114 2115 if (do_log) { 2116 union tcp_log_stackspecific log; 2117 struct timeval tv; 2118 uint64_t lt_bw; 2119 2120 /* Convert our ms to a microsecond */ 2121 memset(&log, 0, sizeof(log)); 2122 2123 log.u_bbr.cwnd_gain = line; 2124 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2125 log.u_bbr.rttProp = tim; 2126 log.u_bbr.bw_inuse = cbw; 2127 log.u_bbr.delRate = rack_get_gp_est(rack); 2128 lt_bw = rack_get_lt_bw(rack); 2129 log.u_bbr.flex1 = seq; 2130 log.u_bbr.pacing_gain = aux; 2131 /* lt_bw = < flex3 | flex2 > */ 2132 log.u_bbr.flex2 = (uint32_t)(lt_bw & 0x00000000ffffffff); 2133 log.u_bbr.flex3 = (uint32_t)((lt_bw >> 32) & 0x00000000ffffffff); 2134 /* Record the last obtained us rtt in inflight */ 2135 if (cur == NULL) { 2136 /* Make sure we are looking at the right log if an overide comes in */ 2137 cur = rack->r_ctl.rc_last_sft; 2138 } 2139 if (rack->r_ctl.rack_rs.rs_flags != RACK_RTT_EMPTY) 2140 log.u_bbr.inflight = rack->r_ctl.rack_rs.rs_us_rtt; 2141 else { 2142 /* Use the last known rtt i.e. the rack-rtt */ 2143 log.u_bbr.inflight = rack->rc_rack_rtt; 2144 } 2145 if (cur != NULL) { 2146 uint64_t off; 2147 2148 log.u_bbr.cur_del_rate = cur->deadline; 2149 if ((mod == HYBRID_LOG_RATE_CAP) || (mod == HYBRID_LOG_CAP_CALC)) { 2150 /* start = < lost | pkt_epoch > */ 2151 log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff); 2152 log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff); 2153 log.u_bbr.flex6 = cur->start_seq; 2154 log.u_bbr.pkts_out = cur->end_seq; 2155 } else { 2156 /* start = < lost | pkt_epoch > */ 2157 log.u_bbr.pkt_epoch = (uint32_t)(cur->start & 0x00000000ffffffff); 2158 log.u_bbr.lost = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff); 2159 /* end = < pkts_out | flex6 > */ 2160 log.u_bbr.flex6 = (uint32_t)(cur->end & 0x00000000ffffffff); 2161 log.u_bbr.pkts_out = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff); 2162 } 2163 /* first_send = <lt_epoch | epoch> */ 2164 log.u_bbr.epoch = (uint32_t)(cur->first_send & 0x00000000ffffffff); 2165 log.u_bbr.lt_epoch = (uint32_t)((cur->first_send >> 32) & 0x00000000ffffffff); 2166 /* localtime = <delivered | applimited>*/ 2167 log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff); 2168 log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff); 2169 #ifdef TCP_REQUEST_TRK 2170 off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]); 2171 log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track)); 2172 #endif 2173 log.u_bbr.inhpts = 1; 2174 log.u_bbr.flex4 = (uint32_t)(rack->rc_tp->t_sndbytes - cur->sent_at_fs); 2175 log.u_bbr.flex5 = (uint32_t)(rack->rc_tp->t_snd_rxt_bytes - cur->rxt_at_fs); 2176 log.u_bbr.flex7 = (uint16_t)cur->hybrid_flags; 2177 } else { 2178 log.u_bbr.flex7 = 0xffff; 2179 log.u_bbr.cur_del_rate = 0xffffffffffffffff; 2180 } 2181 /* 2182 * Compose bbr_state to be a bit wise 0000ADHF 2183 * where A is the always_pace flag 2184 * where D is the dgp_on flag 2185 * where H is the hybrid_mode on flag 2186 * where F is the use_fixed_rate flag. 2187 */ 2188 log.u_bbr.bbr_state = rack->rc_always_pace; 2189 log.u_bbr.bbr_state <<= 1; 2190 log.u_bbr.bbr_state |= rack->dgp_on; 2191 log.u_bbr.bbr_state <<= 1; 2192 log.u_bbr.bbr_state |= rack->rc_hybrid_mode; 2193 log.u_bbr.bbr_state <<= 1; 2194 log.u_bbr.bbr_state |= rack->use_fixed_rate; 2195 log.u_bbr.flex8 = mod; 2196 tcp_log_event(rack->rc_tp, NULL, 2197 &rack->rc_inp->inp_socket->so_rcv, 2198 &rack->rc_inp->inp_socket->so_snd, 2199 TCP_HYBRID_PACING_LOG, 0, 2200 0, &log, false, NULL, __func__, __LINE__, &tv); 2201 2202 } 2203 #endif 2204 } 2205 2206 #ifdef TCP_REQUEST_TRK 2207 static void 2208 rack_log_hybrid_sends(struct tcp_rack *rack, struct tcp_sendfile_track *cur, int line) 2209 { 2210 if (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING)) { 2211 union tcp_log_stackspecific log; 2212 struct timeval tv; 2213 uint64_t off; 2214 2215 /* Convert our ms to a microsecond */ 2216 memset(&log, 0, sizeof(log)); 2217 2218 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2219 log.u_bbr.delRate = cur->sent_at_fs; 2220 2221 if ((cur->flags & TCP_TRK_TRACK_FLG_LSND) == 0) { 2222 /* 2223 * We did not get a new Rules Applied to set so 2224 * no overlapping send occured, this means the 2225 * current byte counts are correct. 2226 */ 2227 log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes; 2228 log.u_bbr.rttProp = rack->rc_tp->t_snd_rxt_bytes; 2229 } else { 2230 /* 2231 * Overlapping send case, we switched to a new 2232 * send and did a rules applied. 2233 */ 2234 log.u_bbr.cur_del_rate = cur->sent_at_ls; 2235 log.u_bbr.rttProp = cur->rxt_at_ls; 2236 } 2237 log.u_bbr.bw_inuse = cur->rxt_at_fs; 2238 log.u_bbr.cwnd_gain = line; 2239 off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]); 2240 log.u_bbr.bbr_substate = (uint8_t)(off / sizeof(struct tcp_sendfile_track)); 2241 /* start = < flex1 | flex2 > */ 2242 log.u_bbr.flex2 = (uint32_t)(cur->start & 0x00000000ffffffff); 2243 log.u_bbr.flex1 = (uint32_t)((cur->start >> 32) & 0x00000000ffffffff); 2244 /* end = < flex3 | flex4 > */ 2245 log.u_bbr.flex4 = (uint32_t)(cur->end & 0x00000000ffffffff); 2246 log.u_bbr.flex3 = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff); 2247 2248 /* localtime = <delivered | applimited>*/ 2249 log.u_bbr.applimited = (uint32_t)(cur->localtime & 0x00000000ffffffff); 2250 log.u_bbr.delivered = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff); 2251 /* client timestamp = <lt_epoch | epoch>*/ 2252 log.u_bbr.epoch = (uint32_t)(cur->timestamp & 0x00000000ffffffff); 2253 log.u_bbr.lt_epoch = (uint32_t)((cur->timestamp >> 32) & 0x00000000ffffffff); 2254 /* now set all the flags in */ 2255 log.u_bbr.pkts_out = cur->hybrid_flags; 2256 log.u_bbr.lost = cur->playout_ms; 2257 log.u_bbr.flex6 = cur->flags; 2258 /* 2259 * Last send time = <flex5 | pkt_epoch> note we do not distinguish cases 2260 * where a false retransmit occurred so first_send <-> lastsend may 2261 * include longer time then it actually took if we have a false rxt. 2262 */ 2263 log.u_bbr.pkt_epoch = (uint32_t)(rack->r_ctl.last_tmit_time_acked & 0x00000000ffffffff); 2264 log.u_bbr.flex5 = (uint32_t)((rack->r_ctl.last_tmit_time_acked >> 32) & 0x00000000ffffffff); 2265 /* 2266 * Compose bbr_state to be a bit wise 0000ADHF 2267 * where A is the always_pace flag 2268 * where D is the dgp_on flag 2269 * where H is the hybrid_mode on flag 2270 * where F is the use_fixed_rate flag. 2271 */ 2272 log.u_bbr.bbr_state = rack->rc_always_pace; 2273 log.u_bbr.bbr_state <<= 1; 2274 log.u_bbr.bbr_state |= rack->dgp_on; 2275 log.u_bbr.bbr_state <<= 1; 2276 log.u_bbr.bbr_state |= rack->rc_hybrid_mode; 2277 log.u_bbr.bbr_state <<= 1; 2278 log.u_bbr.bbr_state |= rack->use_fixed_rate; 2279 2280 log.u_bbr.flex8 = HYBRID_LOG_SENT_LOST; 2281 tcp_log_event(rack->rc_tp, NULL, 2282 &rack->rc_inp->inp_socket->so_rcv, 2283 &rack->rc_inp->inp_socket->so_snd, 2284 TCP_HYBRID_PACING_LOG, 0, 2285 0, &log, false, NULL, __func__, __LINE__, &tv); 2286 } 2287 } 2288 #endif 2289 2290 static inline uint64_t 2291 rack_compensate_for_linerate(struct tcp_rack *rack, uint64_t bw) 2292 { 2293 uint64_t ret_bw, ether; 2294 uint64_t u_segsiz; 2295 2296 ether = rack->rc_tp->t_maxseg + sizeof(struct tcphdr); 2297 if (rack->r_is_v6){ 2298 #ifdef INET6 2299 ether += sizeof(struct ip6_hdr); 2300 #endif 2301 ether += 14; /* eheader size 6+6+2 */ 2302 } else { 2303 #ifdef INET 2304 ether += sizeof(struct ip); 2305 #endif 2306 ether += 14; /* eheader size 6+6+2 */ 2307 } 2308 u_segsiz = (uint64_t)min(ctf_fixed_maxseg(rack->rc_tp), rack->r_ctl.rc_pace_min_segs); 2309 ret_bw = bw; 2310 ret_bw *= ether; 2311 ret_bw /= u_segsiz; 2312 return (ret_bw); 2313 } 2314 2315 static void 2316 rack_rate_cap_bw(struct tcp_rack *rack, uint64_t *bw, int *capped) 2317 { 2318 #ifdef TCP_REQUEST_TRK 2319 struct timeval tv; 2320 uint64_t timenow, timeleft, lenleft, lengone, calcbw; 2321 #endif 2322 2323 if (rack->r_ctl.bw_rate_cap == 0) 2324 return; 2325 #ifdef TCP_REQUEST_TRK 2326 if (rack->rc_catch_up && rack->rc_hybrid_mode && 2327 (rack->r_ctl.rc_last_sft != NULL)) { 2328 /* 2329 * We have a dynamic cap. The original target 2330 * is in bw_rate_cap, but we need to look at 2331 * how long it is until we hit the deadline. 2332 */ 2333 struct tcp_sendfile_track *ent; 2334 2335 ent = rack->r_ctl.rc_last_sft; 2336 microuptime(&tv); 2337 timenow = tcp_tv_to_lusectick(&tv); 2338 if (timenow >= ent->deadline) { 2339 /* No time left we do DGP only */ 2340 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2341 0, 0, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__); 2342 rack->r_ctl.bw_rate_cap = 0; 2343 return; 2344 } 2345 /* We have the time */ 2346 timeleft = rack->r_ctl.rc_last_sft->deadline - timenow; 2347 if (timeleft < HPTS_MSEC_IN_SEC) { 2348 /* If there is less than a ms left just use DGPs rate */ 2349 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2350 0, timeleft, 0, HYBRID_LOG_OUTOFTIME, 0, ent, __LINE__); 2351 rack->r_ctl.bw_rate_cap = 0; 2352 return; 2353 } 2354 /* 2355 * Now lets find the amount of data left to send. 2356 * 2357 * Now ideally we want to use the end_seq to figure out how much more 2358 * but it might not be possible (only if we have the TRACK_FG_COMP on the entry.. 2359 */ 2360 if (ent->flags & TCP_TRK_TRACK_FLG_COMP) { 2361 if (SEQ_GT(ent->end_seq, rack->rc_tp->snd_una)) 2362 lenleft = ent->end_seq - rack->rc_tp->snd_una; 2363 else { 2364 /* TSNH, we should catch it at the send */ 2365 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2366 0, timeleft, 0, HYBRID_LOG_CAPERROR, 0, ent, __LINE__); 2367 rack->r_ctl.bw_rate_cap = 0; 2368 return; 2369 } 2370 } else { 2371 /* 2372 * The hard way, figure out how much is gone and then 2373 * take that away from the total the client asked for 2374 * (thats off by tls overhead if this is tls). 2375 */ 2376 if (SEQ_GT(rack->rc_tp->snd_una, ent->start_seq)) 2377 lengone = rack->rc_tp->snd_una - ent->start_seq; 2378 else 2379 lengone = 0; 2380 if (lengone < (ent->end - ent->start)) 2381 lenleft = (ent->end - ent->start) - lengone; 2382 else { 2383 /* TSNH, we should catch it at the send */ 2384 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2385 0, timeleft, lengone, HYBRID_LOG_CAPERROR, 0, ent, __LINE__); 2386 rack->r_ctl.bw_rate_cap = 0; 2387 return; 2388 } 2389 } 2390 if (lenleft == 0) { 2391 /* We have it all sent */ 2392 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2393 0, timeleft, lenleft, HYBRID_LOG_ALLSENT, 0, ent, __LINE__); 2394 if (rack->r_ctl.bw_rate_cap) 2395 goto normal_ratecap; 2396 else 2397 return; 2398 } 2399 calcbw = lenleft * HPTS_USEC_IN_SEC; 2400 calcbw /= timeleft; 2401 /* Now we must compensate for IP/TCP overhead */ 2402 calcbw = rack_compensate_for_linerate(rack, calcbw); 2403 /* Update the bit rate cap */ 2404 rack->r_ctl.bw_rate_cap = calcbw; 2405 if ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) && 2406 (rack_hybrid_allow_set_maxseg == 1) && 2407 ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) { 2408 /* Lets set in a smaller mss possibly here to match our rate-cap */ 2409 uint32_t orig_max; 2410 2411 orig_max = rack->r_ctl.rc_pace_max_segs; 2412 rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS; 2413 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, calcbw, ctf_fixed_maxseg(rack->rc_tp)); 2414 rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5); 2415 } 2416 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2417 calcbw, timeleft, lenleft, HYBRID_LOG_CAP_CALC, 0, ent, __LINE__); 2418 if ((calcbw > 0) && (*bw > calcbw)) { 2419 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2420 *bw, ent->deadline, lenleft, HYBRID_LOG_RATE_CAP, 0, ent, __LINE__); 2421 *capped = 1; 2422 *bw = calcbw; 2423 } 2424 return; 2425 } 2426 normal_ratecap: 2427 #endif 2428 if ((rack->r_ctl.bw_rate_cap > 0) && (*bw > rack->r_ctl.bw_rate_cap)) { 2429 #ifdef TCP_REQUEST_TRK 2430 if (rack->rc_hybrid_mode && 2431 rack->rc_catch_up && 2432 (rack->r_ctl.rc_last_sft != NULL) && 2433 (rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_S_MSS) && 2434 (rack_hybrid_allow_set_maxseg == 1) && 2435 ((rack->r_ctl.rc_last_sft->hybrid_flags & TCP_HYBRID_PACING_SETMSS) == 0)) { 2436 /* Lets set in a smaller mss possibly here to match our rate-cap */ 2437 uint32_t orig_max; 2438 2439 orig_max = rack->r_ctl.rc_pace_max_segs; 2440 rack->r_ctl.rc_last_sft->hybrid_flags |= TCP_HYBRID_PACING_SETMSS; 2441 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, rack->r_ctl.bw_rate_cap, ctf_fixed_maxseg(rack->rc_tp)); 2442 rack_log_type_pacing_sizes(rack->rc_tp, rack, rack->r_ctl.client_suggested_maxseg, orig_max, __LINE__, 5); 2443 } 2444 #endif 2445 *capped = 1; 2446 *bw = rack->r_ctl.bw_rate_cap; 2447 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 2448 *bw, 0, 0, 2449 HYBRID_LOG_RATE_CAP, 1, NULL, __LINE__); 2450 } 2451 } 2452 2453 static uint64_t 2454 rack_get_gp_est(struct tcp_rack *rack) 2455 { 2456 uint64_t bw, lt_bw, ret_bw; 2457 2458 if (rack->rc_gp_filled == 0) { 2459 /* 2460 * We have yet no b/w measurement, 2461 * if we have a user set initial bw 2462 * return it. If we don't have that and 2463 * we have an srtt, use the tcp IW (10) to 2464 * calculate a fictional b/w over the SRTT 2465 * which is more or less a guess. Note 2466 * we don't use our IW from rack on purpose 2467 * so if we have like IW=30, we are not 2468 * calculating a "huge" b/w. 2469 */ 2470 uint64_t srtt; 2471 2472 if (rack->dis_lt_bw == 1) 2473 lt_bw = 0; 2474 else 2475 lt_bw = rack_get_lt_bw(rack); 2476 if (lt_bw) { 2477 /* 2478 * No goodput bw but a long-term b/w does exist 2479 * lets use that. 2480 */ 2481 ret_bw = lt_bw; 2482 goto compensate; 2483 } 2484 if (rack->r_ctl.init_rate) 2485 return (rack->r_ctl.init_rate); 2486 2487 /* Ok lets come up with the IW guess, if we have a srtt */ 2488 if (rack->rc_tp->t_srtt == 0) { 2489 /* 2490 * Go with old pacing method 2491 * i.e. burst mitigation only. 2492 */ 2493 return (0); 2494 } 2495 /* Ok lets get the initial TCP win (not racks) */ 2496 bw = tcp_compute_initwnd(tcp_maxseg(rack->rc_tp)); 2497 srtt = (uint64_t)rack->rc_tp->t_srtt; 2498 bw *= (uint64_t)USECS_IN_SECOND; 2499 bw /= srtt; 2500 ret_bw = bw; 2501 goto compensate; 2502 2503 } 2504 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) { 2505 /* Averaging is done, we can return the value */ 2506 bw = rack->r_ctl.gp_bw; 2507 } else { 2508 /* Still doing initial average must calculate */ 2509 bw = rack->r_ctl.gp_bw / max(rack->r_ctl.num_measurements, 1); 2510 } 2511 if (rack->dis_lt_bw) { 2512 /* We are not using lt-bw */ 2513 ret_bw = bw; 2514 goto compensate; 2515 } 2516 lt_bw = rack_get_lt_bw(rack); 2517 if (lt_bw == 0) { 2518 /* If we don't have one then equate it to the gp_bw */ 2519 lt_bw = rack->r_ctl.gp_bw; 2520 } 2521 if (rack->use_lesser_lt_bw) { 2522 if (lt_bw < bw) 2523 ret_bw = lt_bw; 2524 else 2525 ret_bw = bw; 2526 } else { 2527 if (lt_bw > bw) 2528 ret_bw = lt_bw; 2529 else 2530 ret_bw = bw; 2531 } 2532 /* 2533 * Now lets compensate based on the TCP/IP overhead. Our 2534 * Goodput estimate does not include this so we must pace out 2535 * a bit faster since our pacing calculations do. The pacing 2536 * calculations use the base ETHERNET_SEGMENT_SIZE and the segsiz 2537 * we are using to do this, so we do that here in the opposite 2538 * direction as well. This means that if we are tunneled and the 2539 * segsiz is say 1200 bytes we will get quite a boost, but its 2540 * compensated for in the pacing time the opposite way. 2541 */ 2542 compensate: 2543 ret_bw = rack_compensate_for_linerate(rack, ret_bw); 2544 return(ret_bw); 2545 } 2546 2547 2548 static uint64_t 2549 rack_get_bw(struct tcp_rack *rack) 2550 { 2551 uint64_t bw; 2552 2553 if (rack->use_fixed_rate) { 2554 /* Return the fixed pacing rate */ 2555 return (rack_get_fixed_pacing_bw(rack)); 2556 } 2557 bw = rack_get_gp_est(rack); 2558 return (bw); 2559 } 2560 2561 static uint16_t 2562 rack_get_output_gain(struct tcp_rack *rack, struct rack_sendmap *rsm) 2563 { 2564 if (rack->use_fixed_rate) { 2565 return (100); 2566 } else if (rack->in_probe_rtt && (rsm == NULL)) 2567 return (rack->r_ctl.rack_per_of_gp_probertt); 2568 else if ((IN_FASTRECOVERY(rack->rc_tp->t_flags) && 2569 rack->r_ctl.rack_per_of_gp_rec)) { 2570 if (rsm) { 2571 /* a retransmission always use the recovery rate */ 2572 return (rack->r_ctl.rack_per_of_gp_rec); 2573 } else if (rack->rack_rec_nonrxt_use_cr) { 2574 /* Directed to use the configured rate */ 2575 goto configured_rate; 2576 } else if (rack->rack_no_prr && 2577 (rack->r_ctl.rack_per_of_gp_rec > 100)) { 2578 /* No PRR, lets just use the b/w estimate only */ 2579 return (100); 2580 } else { 2581 /* 2582 * Here we may have a non-retransmit but we 2583 * have no overrides, so just use the recovery 2584 * rate (prr is in effect). 2585 */ 2586 return (rack->r_ctl.rack_per_of_gp_rec); 2587 } 2588 } 2589 configured_rate: 2590 /* For the configured rate we look at our cwnd vs the ssthresh */ 2591 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) 2592 return (rack->r_ctl.rack_per_of_gp_ss); 2593 else 2594 return (rack->r_ctl.rack_per_of_gp_ca); 2595 } 2596 2597 static void 2598 rack_log_dsack_event(struct tcp_rack *rack, uint8_t mod, uint32_t flex4, uint32_t flex5, uint32_t flex6) 2599 { 2600 /* 2601 * Types of logs (mod value) 2602 * 1 = dsack_persists reduced by 1 via T-O or fast recovery exit. 2603 * 2 = a dsack round begins, persist is reset to 16. 2604 * 3 = a dsack round ends 2605 * 4 = Dsack option increases rack rtt flex5 is the srtt input, flex6 is thresh 2606 * 5 = Socket option set changing the control flags rc_rack_tmr_std_based, rc_rack_use_dsack 2607 * 6 = Final rack rtt, flex4 is srtt and flex6 is final limited thresh. 2608 */ 2609 if (tcp_bblogging_on(rack->rc_tp)) { 2610 union tcp_log_stackspecific log; 2611 struct timeval tv; 2612 2613 memset(&log, 0, sizeof(log)); 2614 log.u_bbr.flex1 = rack->rc_rack_tmr_std_based; 2615 log.u_bbr.flex1 <<= 1; 2616 log.u_bbr.flex1 |= rack->rc_rack_use_dsack; 2617 log.u_bbr.flex1 <<= 1; 2618 log.u_bbr.flex1 |= rack->rc_dsack_round_seen; 2619 log.u_bbr.flex2 = rack->r_ctl.dsack_round_end; 2620 log.u_bbr.flex3 = rack->r_ctl.num_dsack; 2621 log.u_bbr.flex4 = flex4; 2622 log.u_bbr.flex5 = flex5; 2623 log.u_bbr.flex6 = flex6; 2624 log.u_bbr.flex7 = rack->r_ctl.dsack_persist; 2625 log.u_bbr.flex8 = mod; 2626 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2627 log.u_bbr.epoch = rack->r_ctl.current_round; 2628 log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost; 2629 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2630 &rack->rc_inp->inp_socket->so_rcv, 2631 &rack->rc_inp->inp_socket->so_snd, 2632 RACK_DSACK_HANDLING, 0, 2633 0, &log, false, &tv); 2634 } 2635 } 2636 2637 static void 2638 rack_log_hdwr_pacing(struct tcp_rack *rack, 2639 uint64_t rate, uint64_t hw_rate, int line, 2640 int error, uint16_t mod) 2641 { 2642 if (tcp_bblogging_on(rack->rc_tp)) { 2643 union tcp_log_stackspecific log; 2644 struct timeval tv; 2645 const struct ifnet *ifp; 2646 2647 memset(&log, 0, sizeof(log)); 2648 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2649 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2650 if (rack->r_ctl.crte) { 2651 ifp = rack->r_ctl.crte->ptbl->rs_ifp; 2652 } else if (rack->rc_inp->inp_route.ro_nh && 2653 rack->rc_inp->inp_route.ro_nh->nh_ifp) { 2654 ifp = rack->rc_inp->inp_route.ro_nh->nh_ifp; 2655 } else 2656 ifp = NULL; 2657 if (ifp) { 2658 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff); 2659 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff); 2660 } 2661 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2662 log.u_bbr.bw_inuse = rate; 2663 log.u_bbr.flex5 = line; 2664 log.u_bbr.flex6 = error; 2665 log.u_bbr.flex7 = mod; 2666 log.u_bbr.applimited = rack->r_ctl.rc_pace_max_segs; 2667 log.u_bbr.flex8 = rack->use_fixed_rate; 2668 log.u_bbr.flex8 <<= 1; 2669 log.u_bbr.flex8 |= rack->rack_hdrw_pacing; 2670 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg; 2671 log.u_bbr.delRate = rack->r_ctl.crte_prev_rate; 2672 if (rack->r_ctl.crte) 2673 log.u_bbr.cur_del_rate = rack->r_ctl.crte->rate; 2674 else 2675 log.u_bbr.cur_del_rate = 0; 2676 log.u_bbr.rttProp = rack->r_ctl.last_hw_bw_req; 2677 log.u_bbr.epoch = rack->r_ctl.current_round; 2678 log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost; 2679 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2680 &rack->rc_inp->inp_socket->so_rcv, 2681 &rack->rc_inp->inp_socket->so_snd, 2682 BBR_LOG_HDWR_PACE, 0, 2683 0, &log, false, &tv); 2684 } 2685 } 2686 2687 static uint64_t 2688 rack_get_output_bw(struct tcp_rack *rack, uint64_t bw, struct rack_sendmap *rsm, int *capped) 2689 { 2690 /* 2691 * We allow rack_per_of_gp_xx to dictate our bw rate we want. 2692 */ 2693 uint64_t bw_est, high_rate; 2694 uint64_t gain; 2695 2696 gain = (uint64_t)rack_get_output_gain(rack, rsm); 2697 bw_est = bw * gain; 2698 bw_est /= (uint64_t)100; 2699 /* Never fall below the minimum (def 64kbps) */ 2700 if (bw_est < RACK_MIN_BW) 2701 bw_est = RACK_MIN_BW; 2702 if (rack->r_rack_hw_rate_caps) { 2703 /* Rate caps are in place */ 2704 if (rack->r_ctl.crte != NULL) { 2705 /* We have a hdwr rate already */ 2706 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte); 2707 if (bw_est >= high_rate) { 2708 /* We are capping bw at the highest rate table entry */ 2709 if (rack_hw_rate_cap_per && 2710 (((high_rate * (100 + rack_hw_rate_cap_per)) / 100) < bw_est)) { 2711 rack->r_rack_hw_rate_caps = 0; 2712 goto done; 2713 } 2714 rack_log_hdwr_pacing(rack, 2715 bw_est, high_rate, __LINE__, 2716 0, 3); 2717 bw_est = high_rate; 2718 if (capped) 2719 *capped = 1; 2720 } 2721 } else if ((rack->rack_hdrw_pacing == 0) && 2722 (rack->rack_hdw_pace_ena) && 2723 (rack->rack_attempt_hdwr_pace == 0) && 2724 (rack->rc_inp->inp_route.ro_nh != NULL) && 2725 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 2726 /* 2727 * Special case, we have not yet attempted hardware 2728 * pacing, and yet we may, when we do, find out if we are 2729 * above the highest rate. We need to know the maxbw for the interface 2730 * in question (if it supports ratelimiting). We get back 2731 * a 0, if the interface is not found in the RL lists. 2732 */ 2733 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp); 2734 if (high_rate) { 2735 /* Yep, we have a rate is it above this rate? */ 2736 if (bw_est > high_rate) { 2737 bw_est = high_rate; 2738 if (capped) 2739 *capped = 1; 2740 } 2741 } 2742 } 2743 } 2744 done: 2745 return (bw_est); 2746 } 2747 2748 static void 2749 rack_log_retran_reason(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t tsused, uint32_t thresh, int mod) 2750 { 2751 if (tcp_bblogging_on(rack->rc_tp)) { 2752 union tcp_log_stackspecific log; 2753 struct timeval tv; 2754 2755 if (rack->sack_attack_disable > 0) 2756 goto log_anyway; 2757 if ((mod != 1) && (rack_verbose_logging == 0)) { 2758 /* 2759 * We get 3 values currently for mod 2760 * 1 - We are retransmitting and this tells the reason. 2761 * 2 - We are clearing a dup-ack count. 2762 * 3 - We are incrementing a dup-ack count. 2763 * 2764 * The clear/increment are only logged 2765 * if you have BBverbose on. 2766 */ 2767 return; 2768 } 2769 log_anyway: 2770 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2771 log.u_bbr.flex1 = tsused; 2772 log.u_bbr.flex2 = thresh; 2773 log.u_bbr.flex3 = rsm->r_flags; 2774 log.u_bbr.flex4 = rsm->r_dupack; 2775 log.u_bbr.flex5 = rsm->r_start; 2776 log.u_bbr.flex6 = rsm->r_end; 2777 log.u_bbr.flex8 = mod; 2778 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 2779 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2780 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 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.epoch = rack->r_ctl.current_round; 2785 log.u_bbr.lt_epoch = rack->r_ctl.rc_considered_lost; 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_SETTINGS_CHG, 0, 2790 0, &log, false, &tv); 2791 } 2792 } 2793 2794 static void 2795 rack_log_to_start(struct tcp_rack *rack, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2796 { 2797 if (tcp_bblogging_on(rack->rc_tp)) { 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->rc_tp->t_srtt; 2803 log.u_bbr.flex2 = to; 2804 log.u_bbr.flex3 = rack->r_ctl.rc_hpts_flags; 2805 log.u_bbr.flex4 = slot; 2806 log.u_bbr.flex5 = rack->rc_tp->t_hpts_slot; 2807 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 2808 log.u_bbr.flex7 = rack->rc_in_persist; 2809 log.u_bbr.flex8 = which; 2810 if (rack->rack_no_prr) 2811 log.u_bbr.pkts_out = 0; 2812 else 2813 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 2814 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 2815 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2816 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2817 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2818 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2819 log.u_bbr.pacing_gain = rack->r_must_retran; 2820 log.u_bbr.cwnd_gain = rack->rack_deferred_inited; 2821 log.u_bbr.pkt_epoch = rack->rc_has_collapsed; 2822 log.u_bbr.lt_epoch = rack->rc_tp->t_rxtshift; 2823 log.u_bbr.lost = rack_rto_min; 2824 log.u_bbr.epoch = rack->r_ctl.roundends; 2825 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 2826 log.u_bbr.bw_inuse <<= 32; 2827 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 2828 log.u_bbr.applimited = rack->rc_tp->t_flags2; 2829 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2830 &rack->rc_inp->inp_socket->so_rcv, 2831 &rack->rc_inp->inp_socket->so_snd, 2832 BBR_LOG_TIMERSTAR, 0, 2833 0, &log, false, &tv); 2834 } 2835 } 2836 2837 static void 2838 rack_log_to_event(struct tcp_rack *rack, int32_t to_num, struct rack_sendmap *rsm) 2839 { 2840 if (tcp_bblogging_on(rack->rc_tp)) { 2841 union tcp_log_stackspecific log; 2842 struct timeval tv; 2843 2844 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2845 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 2846 log.u_bbr.flex8 = to_num; 2847 log.u_bbr.flex1 = rack->r_ctl.rc_rack_min_rtt; 2848 log.u_bbr.flex2 = rack->rc_rack_rtt; 2849 if (rsm == NULL) 2850 log.u_bbr.flex3 = 0; 2851 else 2852 log.u_bbr.flex3 = rsm->r_end - rsm->r_start; 2853 if (rack->rack_no_prr) 2854 log.u_bbr.flex5 = 0; 2855 else 2856 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 2857 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2858 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2859 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 2860 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 2861 log.u_bbr.pacing_gain = rack->r_must_retran; 2862 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 2863 log.u_bbr.bw_inuse <<= 32; 2864 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 2865 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2866 &rack->rc_inp->inp_socket->so_rcv, 2867 &rack->rc_inp->inp_socket->so_snd, 2868 BBR_LOG_RTO, 0, 2869 0, &log, false, &tv); 2870 } 2871 } 2872 2873 static void 2874 rack_log_map_chg(struct tcpcb *tp, struct tcp_rack *rack, 2875 struct rack_sendmap *prev, 2876 struct rack_sendmap *rsm, 2877 struct rack_sendmap *next, 2878 int flag, uint32_t th_ack, int line) 2879 { 2880 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 2881 union tcp_log_stackspecific log; 2882 struct timeval tv; 2883 2884 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2885 log.u_bbr.flex8 = flag; 2886 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 2887 log.u_bbr.cur_del_rate = (uint64_t)prev; 2888 log.u_bbr.delRate = (uint64_t)rsm; 2889 log.u_bbr.rttProp = (uint64_t)next; 2890 log.u_bbr.flex7 = 0; 2891 if (prev) { 2892 log.u_bbr.flex1 = prev->r_start; 2893 log.u_bbr.flex2 = prev->r_end; 2894 log.u_bbr.flex7 |= 0x4; 2895 } 2896 if (rsm) { 2897 log.u_bbr.flex3 = rsm->r_start; 2898 log.u_bbr.flex4 = rsm->r_end; 2899 log.u_bbr.flex7 |= 0x2; 2900 } 2901 if (next) { 2902 log.u_bbr.flex5 = next->r_start; 2903 log.u_bbr.flex6 = next->r_end; 2904 log.u_bbr.flex7 |= 0x1; 2905 } 2906 log.u_bbr.applimited = line; 2907 log.u_bbr.pkts_out = th_ack; 2908 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2909 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2910 if (rack->rack_no_prr) 2911 log.u_bbr.lost = 0; 2912 else 2913 log.u_bbr.lost = rack->r_ctl.rc_prr_sndcnt; 2914 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 2915 log.u_bbr.bw_inuse <<= 32; 2916 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 2917 TCP_LOG_EVENTP(rack->rc_tp, NULL, 2918 &rack->rc_inp->inp_socket->so_rcv, 2919 &rack->rc_inp->inp_socket->so_snd, 2920 TCP_LOG_MAPCHG, 0, 2921 0, &log, false, &tv); 2922 } 2923 } 2924 2925 static void 2926 rack_log_rtt_upd(struct tcpcb *tp, struct tcp_rack *rack, uint32_t t, uint32_t len, 2927 struct rack_sendmap *rsm, int conf) 2928 { 2929 if (tcp_bblogging_on(tp)) { 2930 union tcp_log_stackspecific log; 2931 struct timeval tv; 2932 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2933 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 2934 log.u_bbr.flex1 = t; 2935 log.u_bbr.flex2 = len; 2936 log.u_bbr.flex3 = rack->r_ctl.rc_rack_min_rtt; 2937 log.u_bbr.flex4 = rack->r_ctl.rack_rs.rs_rtt_lowest; 2938 log.u_bbr.flex5 = rack->r_ctl.rack_rs.rs_rtt_highest; 2939 log.u_bbr.flex6 = rack->r_ctl.rack_rs.rs_us_rtrcnt; 2940 log.u_bbr.flex7 = conf; 2941 log.u_bbr.rttProp = (uint64_t)rack->r_ctl.rack_rs.rs_rtt_tot; 2942 log.u_bbr.flex8 = rack->r_ctl.rc_rate_sample_method; 2943 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2944 log.u_bbr.delivered = rack->r_ctl.rack_rs.rs_us_rtrcnt; 2945 log.u_bbr.pkts_out = rack->r_ctl.rack_rs.rs_flags; 2946 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 2947 if (rsm) { 2948 log.u_bbr.pkt_epoch = rsm->r_start; 2949 log.u_bbr.lost = rsm->r_end; 2950 log.u_bbr.cwnd_gain = rsm->r_rtr_cnt; 2951 /* We loose any upper of the 24 bits */ 2952 log.u_bbr.pacing_gain = (uint16_t)rsm->r_flags; 2953 } else { 2954 /* Its a SYN */ 2955 log.u_bbr.pkt_epoch = rack->rc_tp->iss; 2956 log.u_bbr.lost = 0; 2957 log.u_bbr.cwnd_gain = 0; 2958 log.u_bbr.pacing_gain = 0; 2959 } 2960 /* Write out general bits of interest rrs here */ 2961 log.u_bbr.use_lt_bw = rack->rc_highly_buffered; 2962 log.u_bbr.use_lt_bw <<= 1; 2963 log.u_bbr.use_lt_bw |= rack->forced_ack; 2964 log.u_bbr.use_lt_bw <<= 1; 2965 log.u_bbr.use_lt_bw |= rack->rc_gp_dyn_mul; 2966 log.u_bbr.use_lt_bw <<= 1; 2967 log.u_bbr.use_lt_bw |= rack->in_probe_rtt; 2968 log.u_bbr.use_lt_bw <<= 1; 2969 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt; 2970 log.u_bbr.use_lt_bw <<= 1; 2971 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set; 2972 log.u_bbr.use_lt_bw <<= 1; 2973 log.u_bbr.use_lt_bw |= rack->rc_gp_filled; 2974 log.u_bbr.use_lt_bw <<= 1; 2975 log.u_bbr.use_lt_bw |= rack->rc_dragged_bottom; 2976 log.u_bbr.applimited = rack->r_ctl.rc_target_probertt_flight; 2977 log.u_bbr.epoch = rack->r_ctl.rc_time_probertt_starts; 2978 log.u_bbr.lt_epoch = rack->r_ctl.rc_time_probertt_entered; 2979 log.u_bbr.cur_del_rate = rack->r_ctl.rc_lower_rtt_us_cts; 2980 log.u_bbr.delRate = rack->r_ctl.rc_gp_srtt; 2981 log.u_bbr.bw_inuse = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 2982 log.u_bbr.bw_inuse <<= 32; 2983 if (rsm) 2984 log.u_bbr.bw_inuse |= ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 2985 TCP_LOG_EVENTP(tp, NULL, 2986 &rack->rc_inp->inp_socket->so_rcv, 2987 &rack->rc_inp->inp_socket->so_snd, 2988 BBR_LOG_BBRRTT, 0, 2989 0, &log, false, &tv); 2990 2991 2992 } 2993 } 2994 2995 static void 2996 rack_log_rtt_sample(struct tcp_rack *rack, uint32_t rtt) 2997 { 2998 /* 2999 * Log the rtt sample we are 3000 * applying to the srtt algorithm in 3001 * useconds. 3002 */ 3003 if (tcp_bblogging_on(rack->rc_tp)) { 3004 union tcp_log_stackspecific log; 3005 struct timeval tv; 3006 3007 /* Convert our ms to a microsecond */ 3008 memset(&log, 0, sizeof(log)); 3009 log.u_bbr.flex1 = rtt; 3010 log.u_bbr.flex2 = rack->r_ctl.ack_count; 3011 log.u_bbr.flex3 = rack->r_ctl.sack_count; 3012 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move; 3013 log.u_bbr.flex5 = rack->r_ctl.sack_moved_extra; 3014 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 3015 log.u_bbr.flex7 = 1; 3016 log.u_bbr.flex8 = rack->sack_attack_disable; 3017 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3018 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3019 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3020 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3021 log.u_bbr.pacing_gain = rack->r_must_retran; 3022 /* 3023 * We capture in delRate the upper 32 bits as 3024 * the confidence level we had declared, and the 3025 * lower 32 bits as the actual RTT using the arrival 3026 * timestamp. 3027 */ 3028 log.u_bbr.delRate = rack->r_ctl.rack_rs.confidence; 3029 log.u_bbr.delRate <<= 32; 3030 log.u_bbr.delRate |= rack->r_ctl.rack_rs.rs_us_rtt; 3031 /* Lets capture all the things that make up t_rtxcur */ 3032 log.u_bbr.applimited = rack_rto_min; 3033 log.u_bbr.epoch = rack_rto_max; 3034 log.u_bbr.lt_epoch = rack->r_ctl.timer_slop; 3035 log.u_bbr.lost = rack_rto_min; 3036 log.u_bbr.pkt_epoch = TICKS_2_USEC(tcp_rexmit_slop); 3037 log.u_bbr.rttProp = RACK_REXMTVAL(rack->rc_tp); 3038 log.u_bbr.bw_inuse = rack->r_ctl.act_rcv_time.tv_sec; 3039 log.u_bbr.bw_inuse *= HPTS_USEC_IN_SEC; 3040 log.u_bbr.bw_inuse += rack->r_ctl.act_rcv_time.tv_usec; 3041 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3042 &rack->rc_inp->inp_socket->so_rcv, 3043 &rack->rc_inp->inp_socket->so_snd, 3044 TCP_LOG_RTT, 0, 3045 0, &log, false, &tv); 3046 } 3047 } 3048 3049 static void 3050 rack_log_rtt_sample_calc(struct tcp_rack *rack, uint32_t rtt, uint32_t send_time, uint32_t ack_time, int where) 3051 { 3052 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 3053 union tcp_log_stackspecific log; 3054 struct timeval tv; 3055 3056 /* Convert our ms to a microsecond */ 3057 memset(&log, 0, sizeof(log)); 3058 log.u_bbr.flex1 = rtt; 3059 log.u_bbr.flex2 = send_time; 3060 log.u_bbr.flex3 = ack_time; 3061 log.u_bbr.flex4 = where; 3062 log.u_bbr.flex7 = 2; 3063 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3064 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3065 log.u_bbr.bw_inuse <<= 32; 3066 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3067 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3068 &rack->rc_inp->inp_socket->so_rcv, 3069 &rack->rc_inp->inp_socket->so_snd, 3070 TCP_LOG_RTT, 0, 3071 0, &log, false, &tv); 3072 } 3073 } 3074 3075 3076 static void 3077 rack_log_rtt_sendmap(struct tcp_rack *rack, uint32_t idx, uint64_t tsv, uint32_t tsecho) 3078 { 3079 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 3080 union tcp_log_stackspecific log; 3081 struct timeval tv; 3082 3083 /* Convert our ms to a microsecond */ 3084 memset(&log, 0, sizeof(log)); 3085 log.u_bbr.flex1 = idx; 3086 log.u_bbr.flex2 = rack_ts_to_msec(tsv); 3087 log.u_bbr.flex3 = tsecho; 3088 log.u_bbr.flex7 = 3; 3089 log.u_bbr.rttProp = tsv; 3090 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3091 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3092 log.u_bbr.bw_inuse <<= 32; 3093 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3094 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3095 &rack->rc_inp->inp_socket->so_rcv, 3096 &rack->rc_inp->inp_socket->so_snd, 3097 TCP_LOG_RTT, 0, 3098 0, &log, false, &tv); 3099 } 3100 } 3101 3102 3103 static inline void 3104 rack_log_progress_event(struct tcp_rack *rack, struct tcpcb *tp, uint32_t tick, int event, int line) 3105 { 3106 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 3107 union tcp_log_stackspecific log; 3108 struct timeval tv; 3109 3110 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3111 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 3112 log.u_bbr.flex1 = line; 3113 log.u_bbr.flex2 = tick; 3114 log.u_bbr.flex3 = tp->t_maxunacktime; 3115 log.u_bbr.flex4 = tp->t_acktime; 3116 log.u_bbr.flex8 = event; 3117 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3118 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3119 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3120 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3121 log.u_bbr.pacing_gain = rack->r_must_retran; 3122 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3123 log.u_bbr.bw_inuse <<= 32; 3124 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3125 TCP_LOG_EVENTP(tp, NULL, 3126 &rack->rc_inp->inp_socket->so_rcv, 3127 &rack->rc_inp->inp_socket->so_snd, 3128 BBR_LOG_PROGRESS, 0, 3129 0, &log, false, &tv); 3130 } 3131 } 3132 3133 static void 3134 rack_log_type_bbrsnd(struct tcp_rack *rack, uint32_t len, uint32_t slot, uint32_t cts, struct timeval *tv, int line) 3135 { 3136 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 3137 union tcp_log_stackspecific log; 3138 3139 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3140 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 3141 log.u_bbr.flex1 = slot; 3142 if (rack->rack_no_prr) 3143 log.u_bbr.flex2 = 0; 3144 else 3145 log.u_bbr.flex2 = rack->r_ctl.rc_prr_sndcnt; 3146 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 3147 log.u_bbr.flex5 = rack->r_ctl.ack_during_sd; 3148 log.u_bbr.flex6 = line; 3149 log.u_bbr.flex7 = (0x0000ffff & rack->r_ctl.rc_hpts_flags); 3150 log.u_bbr.flex8 = rack->rc_in_persist; 3151 log.u_bbr.timeStamp = cts; 3152 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3153 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3154 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3155 log.u_bbr.pacing_gain = rack->r_must_retran; 3156 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3157 &rack->rc_inp->inp_socket->so_rcv, 3158 &rack->rc_inp->inp_socket->so_snd, 3159 BBR_LOG_BBRSND, 0, 3160 0, &log, false, tv); 3161 } 3162 } 3163 3164 static void 3165 rack_log_doseg_done(struct tcp_rack *rack, uint32_t cts, int32_t nxt_pkt, int32_t did_out, int way_out, int nsegs) 3166 { 3167 if (tcp_bblogging_on(rack->rc_tp)) { 3168 union tcp_log_stackspecific log; 3169 struct timeval tv; 3170 3171 memset(&log, 0, sizeof(log)); 3172 log.u_bbr.flex1 = did_out; 3173 log.u_bbr.flex2 = nxt_pkt; 3174 log.u_bbr.flex3 = way_out; 3175 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 3176 if (rack->rack_no_prr) 3177 log.u_bbr.flex5 = 0; 3178 else 3179 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 3180 log.u_bbr.flex6 = nsegs; 3181 log.u_bbr.applimited = rack->r_ctl.rc_pace_min_segs; 3182 log.u_bbr.flex7 = rack->rc_ack_can_sendout_data; /* Do we have ack-can-send set */ 3183 log.u_bbr.flex7 <<= 1; 3184 log.u_bbr.flex7 |= rack->r_fast_output; /* is fast output primed */ 3185 log.u_bbr.flex7 <<= 1; 3186 log.u_bbr.flex7 |= rack->r_wanted_output; /* Do we want output */ 3187 log.u_bbr.flex8 = rack->rc_in_persist; 3188 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 3189 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3190 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3191 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 3192 log.u_bbr.use_lt_bw <<= 1; 3193 log.u_bbr.use_lt_bw |= rack->r_might_revert; 3194 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3195 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3196 log.u_bbr.pacing_gain = rack->r_must_retran; 3197 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3198 log.u_bbr.bw_inuse <<= 32; 3199 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3200 log.u_bbr.epoch = rack->rc_inp->inp_socket->so_snd.sb_hiwat; 3201 log.u_bbr.lt_epoch = rack->rc_inp->inp_socket->so_rcv.sb_hiwat; 3202 log.u_bbr.lost = rack->rc_tp->t_srtt; 3203 log.u_bbr.pkt_epoch = rack->rc_tp->rfbuf_cnt; 3204 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3205 &rack->rc_inp->inp_socket->so_rcv, 3206 &rack->rc_inp->inp_socket->so_snd, 3207 BBR_LOG_DOSEG_DONE, 0, 3208 0, &log, false, &tv); 3209 } 3210 } 3211 3212 static void 3213 rack_log_type_pacing_sizes(struct tcpcb *tp, struct tcp_rack *rack, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint8_t frm) 3214 { 3215 if (tcp_bblogging_on(rack->rc_tp)) { 3216 union tcp_log_stackspecific log; 3217 struct timeval tv; 3218 3219 memset(&log, 0, sizeof(log)); 3220 log.u_bbr.flex1 = rack->r_ctl.rc_pace_min_segs; 3221 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 3222 log.u_bbr.flex4 = arg1; 3223 log.u_bbr.flex5 = arg2; 3224 log.u_bbr.flex7 = rack->r_ctl.rc_user_set_min_segs; 3225 log.u_bbr.flex6 = arg3; 3226 log.u_bbr.flex8 = frm; 3227 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3228 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3229 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3230 log.u_bbr.applimited = rack->r_ctl.rc_sacked; 3231 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3232 log.u_bbr.pacing_gain = rack->r_must_retran; 3233 TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv, 3234 &tptosocket(tp)->so_snd, 3235 TCP_HDWR_PACE_SIZE, 0, 0, &log, false, &tv); 3236 } 3237 } 3238 3239 static void 3240 rack_log_type_just_return(struct tcp_rack *rack, uint32_t cts, uint32_t tlen, uint32_t slot, 3241 uint8_t hpts_calling, int reason, uint32_t cwnd_to_use) 3242 { 3243 if (tcp_bblogging_on(rack->rc_tp)) { 3244 union tcp_log_stackspecific log; 3245 struct timeval tv; 3246 3247 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3248 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 3249 log.u_bbr.flex1 = slot; 3250 log.u_bbr.flex2 = rack->r_ctl.rc_hpts_flags; 3251 log.u_bbr.flex4 = reason; 3252 if (rack->rack_no_prr) 3253 log.u_bbr.flex5 = 0; 3254 else 3255 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 3256 log.u_bbr.flex7 = hpts_calling; 3257 log.u_bbr.flex8 = rack->rc_in_persist; 3258 log.u_bbr.lt_epoch = cwnd_to_use; 3259 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3260 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3261 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3262 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3263 log.u_bbr.pacing_gain = rack->r_must_retran; 3264 log.u_bbr.cwnd_gain = rack->rc_has_collapsed; 3265 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3266 log.u_bbr.bw_inuse <<= 32; 3267 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3268 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3269 &rack->rc_inp->inp_socket->so_rcv, 3270 &rack->rc_inp->inp_socket->so_snd, 3271 BBR_LOG_JUSTRET, 0, 3272 tlen, &log, false, &tv); 3273 } 3274 } 3275 3276 static void 3277 rack_log_to_cancel(struct tcp_rack *rack, int32_t hpts_removed, int line, uint32_t us_cts, 3278 struct timeval *tv, uint32_t flags_on_entry) 3279 { 3280 if (tcp_bblogging_on(rack->rc_tp)) { 3281 union tcp_log_stackspecific log; 3282 3283 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3284 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 3285 log.u_bbr.flex1 = line; 3286 log.u_bbr.flex2 = rack->r_ctl.rc_last_output_to; 3287 log.u_bbr.flex3 = flags_on_entry; 3288 log.u_bbr.flex4 = us_cts; 3289 if (rack->rack_no_prr) 3290 log.u_bbr.flex5 = 0; 3291 else 3292 log.u_bbr.flex5 = rack->r_ctl.rc_prr_sndcnt; 3293 log.u_bbr.flex6 = rack->rc_tp->t_rxtcur; 3294 log.u_bbr.flex7 = hpts_removed; 3295 log.u_bbr.flex8 = 1; 3296 log.u_bbr.applimited = rack->r_ctl.rc_hpts_flags; 3297 log.u_bbr.timeStamp = us_cts; 3298 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3299 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3300 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3301 log.u_bbr.pacing_gain = rack->r_must_retran; 3302 log.u_bbr.bw_inuse = rack->r_ctl.current_round; 3303 log.u_bbr.bw_inuse <<= 32; 3304 log.u_bbr.bw_inuse |= rack->r_ctl.rc_considered_lost; 3305 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3306 &rack->rc_inp->inp_socket->so_rcv, 3307 &rack->rc_inp->inp_socket->so_snd, 3308 BBR_LOG_TIMERCANC, 0, 3309 0, &log, false, tv); 3310 } 3311 } 3312 3313 static void 3314 rack_log_alt_to_to_cancel(struct tcp_rack *rack, 3315 uint32_t flex1, uint32_t flex2, 3316 uint32_t flex3, uint32_t flex4, 3317 uint32_t flex5, uint32_t flex6, 3318 uint16_t flex7, uint8_t mod) 3319 { 3320 if (tcp_bblogging_on(rack->rc_tp)) { 3321 union tcp_log_stackspecific log; 3322 struct timeval tv; 3323 3324 if (mod == 1) { 3325 /* No you can't use 1, its for the real to cancel */ 3326 return; 3327 } 3328 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3329 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3330 log.u_bbr.flex1 = flex1; 3331 log.u_bbr.flex2 = flex2; 3332 log.u_bbr.flex3 = flex3; 3333 log.u_bbr.flex4 = flex4; 3334 log.u_bbr.flex5 = flex5; 3335 log.u_bbr.flex6 = flex6; 3336 log.u_bbr.flex7 = flex7; 3337 log.u_bbr.flex8 = mod; 3338 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3339 &rack->rc_inp->inp_socket->so_rcv, 3340 &rack->rc_inp->inp_socket->so_snd, 3341 BBR_LOG_TIMERCANC, 0, 3342 0, &log, false, &tv); 3343 } 3344 } 3345 3346 static void 3347 rack_log_to_processing(struct tcp_rack *rack, uint32_t cts, int32_t ret, int32_t timers) 3348 { 3349 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 3350 union tcp_log_stackspecific log; 3351 struct timeval tv; 3352 3353 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3354 log.u_bbr.flex1 = timers; 3355 log.u_bbr.flex2 = ret; 3356 log.u_bbr.flex3 = rack->r_ctl.rc_timer_exp; 3357 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 3358 log.u_bbr.flex5 = cts; 3359 if (rack->rack_no_prr) 3360 log.u_bbr.flex6 = 0; 3361 else 3362 log.u_bbr.flex6 = rack->r_ctl.rc_prr_sndcnt; 3363 log.u_bbr.pkts_out = rack->r_ctl.rc_out_at_rto; 3364 log.u_bbr.delivered = rack->r_ctl.rc_snd_max_at_rto; 3365 log.u_bbr.pacing_gain = rack->r_must_retran; 3366 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3367 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3368 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3369 &rack->rc_inp->inp_socket->so_rcv, 3370 &rack->rc_inp->inp_socket->so_snd, 3371 BBR_LOG_TO_PROCESS, 0, 3372 0, &log, false, &tv); 3373 } 3374 } 3375 3376 static void 3377 rack_log_to_prr(struct tcp_rack *rack, int frm, int orig_cwnd, int line) 3378 { 3379 if (tcp_bblogging_on(rack->rc_tp)) { 3380 union tcp_log_stackspecific log; 3381 struct timeval tv; 3382 3383 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3384 log.u_bbr.flex1 = rack->r_ctl.rc_prr_out; 3385 log.u_bbr.flex2 = rack->r_ctl.rc_prr_recovery_fs; 3386 if (rack->rack_no_prr) 3387 log.u_bbr.flex3 = 0; 3388 else 3389 log.u_bbr.flex3 = rack->r_ctl.rc_prr_sndcnt; 3390 log.u_bbr.flex4 = rack->r_ctl.rc_prr_delivered; 3391 log.u_bbr.flex5 = rack->r_ctl.rc_sacked; 3392 log.u_bbr.flex6 = rack->r_ctl.rc_holes_rxt; 3393 log.u_bbr.flex7 = line; 3394 log.u_bbr.flex8 = frm; 3395 log.u_bbr.pkts_out = orig_cwnd; 3396 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3397 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3398 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 3399 log.u_bbr.use_lt_bw <<= 1; 3400 log.u_bbr.use_lt_bw |= rack->r_might_revert; 3401 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3402 &rack->rc_inp->inp_socket->so_rcv, 3403 &rack->rc_inp->inp_socket->so_snd, 3404 BBR_LOG_BBRUPD, 0, 3405 0, &log, false, &tv); 3406 } 3407 } 3408 3409 #ifdef TCP_SAD_DETECTION 3410 static void 3411 rack_log_sad(struct tcp_rack *rack, int event) 3412 { 3413 if (tcp_bblogging_on(rack->rc_tp)) { 3414 union tcp_log_stackspecific log; 3415 struct timeval tv; 3416 3417 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 3418 log.u_bbr.flex1 = rack->r_ctl.sack_count; 3419 log.u_bbr.flex2 = rack->r_ctl.ack_count; 3420 log.u_bbr.flex3 = rack->r_ctl.sack_moved_extra; 3421 log.u_bbr.flex4 = rack->r_ctl.sack_noextra_move; 3422 log.u_bbr.flex5 = rack->r_ctl.rc_num_maps_alloced; 3423 log.u_bbr.flex6 = tcp_sack_to_ack_thresh; 3424 log.u_bbr.pkts_out = tcp_sack_to_move_thresh; 3425 log.u_bbr.lt_epoch = (tcp_force_detection << 8); 3426 log.u_bbr.lt_epoch |= rack->do_detection; 3427 log.u_bbr.applimited = tcp_map_minimum; 3428 log.u_bbr.flex7 = rack->sack_attack_disable; 3429 log.u_bbr.flex8 = event; 3430 log.u_bbr.bbr_state = rack->rc_suspicious; 3431 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3432 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3433 log.u_bbr.delivered = tcp_sad_decay_val; 3434 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3435 &rack->rc_inp->inp_socket->so_rcv, 3436 &rack->rc_inp->inp_socket->so_snd, 3437 TCP_SAD_DETECT, 0, 3438 0, &log, false, &tv); 3439 } 3440 } 3441 #endif 3442 3443 static void 3444 rack_counter_destroy(void) 3445 { 3446 counter_u64_free(rack_total_bytes); 3447 counter_u64_free(rack_fto_send); 3448 counter_u64_free(rack_fto_rsm_send); 3449 counter_u64_free(rack_nfto_resend); 3450 counter_u64_free(rack_hw_pace_init_fail); 3451 counter_u64_free(rack_hw_pace_lost); 3452 counter_u64_free(rack_non_fto_send); 3453 counter_u64_free(rack_extended_rfo); 3454 counter_u64_free(rack_ack_total); 3455 counter_u64_free(rack_express_sack); 3456 counter_u64_free(rack_sack_total); 3457 counter_u64_free(rack_move_none); 3458 counter_u64_free(rack_move_some); 3459 counter_u64_free(rack_sack_attacks_detected); 3460 counter_u64_free(rack_sack_attacks_reversed); 3461 counter_u64_free(rack_sack_attacks_suspect); 3462 counter_u64_free(rack_sack_used_next_merge); 3463 counter_u64_free(rack_sack_used_prev_merge); 3464 counter_u64_free(rack_tlp_tot); 3465 counter_u64_free(rack_tlp_newdata); 3466 counter_u64_free(rack_tlp_retran); 3467 counter_u64_free(rack_tlp_retran_bytes); 3468 counter_u64_free(rack_to_tot); 3469 counter_u64_free(rack_saw_enobuf); 3470 counter_u64_free(rack_saw_enobuf_hw); 3471 counter_u64_free(rack_saw_enetunreach); 3472 counter_u64_free(rack_hot_alloc); 3473 counter_u64_free(tcp_policer_detected); 3474 counter_u64_free(rack_to_alloc); 3475 counter_u64_free(rack_to_alloc_hard); 3476 counter_u64_free(rack_to_alloc_emerg); 3477 counter_u64_free(rack_to_alloc_limited); 3478 counter_u64_free(rack_alloc_limited_conns); 3479 counter_u64_free(rack_split_limited); 3480 counter_u64_free(rack_multi_single_eq); 3481 counter_u64_free(rack_rxt_clamps_cwnd); 3482 counter_u64_free(rack_rxt_clamps_cwnd_uniq); 3483 counter_u64_free(rack_proc_non_comp_ack); 3484 counter_u64_free(rack_sack_proc_all); 3485 counter_u64_free(rack_sack_proc_restart); 3486 counter_u64_free(rack_sack_proc_short); 3487 counter_u64_free(rack_sack_skipped_acked); 3488 counter_u64_free(rack_sack_splits); 3489 counter_u64_free(rack_input_idle_reduces); 3490 counter_u64_free(rack_collapsed_win); 3491 counter_u64_free(rack_collapsed_win_rxt); 3492 counter_u64_free(rack_collapsed_win_rxt_bytes); 3493 counter_u64_free(rack_collapsed_win_seen); 3494 counter_u64_free(rack_try_scwnd); 3495 counter_u64_free(rack_persists_sends); 3496 counter_u64_free(rack_persists_acks); 3497 counter_u64_free(rack_persists_loss); 3498 counter_u64_free(rack_persists_lost_ends); 3499 #ifdef INVARIANTS 3500 counter_u64_free(rack_adjust_map_bw); 3501 #endif 3502 COUNTER_ARRAY_FREE(rack_out_size, TCP_MSS_ACCT_SIZE); 3503 COUNTER_ARRAY_FREE(rack_opts_arry, RACK_OPTS_SIZE); 3504 } 3505 3506 static struct rack_sendmap * 3507 rack_alloc(struct tcp_rack *rack) 3508 { 3509 struct rack_sendmap *rsm; 3510 3511 /* 3512 * First get the top of the list it in 3513 * theory is the "hottest" rsm we have, 3514 * possibly just freed by ack processing. 3515 */ 3516 if (rack->rc_free_cnt > rack_free_cache) { 3517 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 3518 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 3519 counter_u64_add(rack_hot_alloc, 1); 3520 rack->rc_free_cnt--; 3521 return (rsm); 3522 } 3523 /* 3524 * Once we get under our free cache we probably 3525 * no longer have a "hot" one available. Lets 3526 * get one from UMA. 3527 */ 3528 rsm = uma_zalloc(rack_zone, M_NOWAIT); 3529 if (rsm) { 3530 rack->r_ctl.rc_num_maps_alloced++; 3531 counter_u64_add(rack_to_alloc, 1); 3532 return (rsm); 3533 } 3534 /* 3535 * Dig in to our aux rsm's (the last two) since 3536 * UMA failed to get us one. 3537 */ 3538 if (rack->rc_free_cnt) { 3539 counter_u64_add(rack_to_alloc_emerg, 1); 3540 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 3541 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 3542 rack->rc_free_cnt--; 3543 return (rsm); 3544 } 3545 return (NULL); 3546 } 3547 3548 static struct rack_sendmap * 3549 rack_alloc_full_limit(struct tcp_rack *rack) 3550 { 3551 if ((V_tcp_map_entries_limit > 0) && 3552 (rack->do_detection == 0) && 3553 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3554 counter_u64_add(rack_to_alloc_limited, 1); 3555 if (!rack->alloc_limit_reported) { 3556 rack->alloc_limit_reported = 1; 3557 counter_u64_add(rack_alloc_limited_conns, 1); 3558 } 3559 return (NULL); 3560 } 3561 return (rack_alloc(rack)); 3562 } 3563 3564 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3565 static struct rack_sendmap * 3566 rack_alloc_limit(struct tcp_rack *rack, uint8_t limit_type) 3567 { 3568 struct rack_sendmap *rsm; 3569 3570 if (limit_type) { 3571 /* currently there is only one limit type */ 3572 if (rack->r_ctl.rc_split_limit > 0 && 3573 (rack->do_detection == 0) && 3574 rack->r_ctl.rc_num_split_allocs >= rack->r_ctl.rc_split_limit) { 3575 counter_u64_add(rack_split_limited, 1); 3576 if (!rack->alloc_limit_reported) { 3577 rack->alloc_limit_reported = 1; 3578 counter_u64_add(rack_alloc_limited_conns, 1); 3579 } 3580 return (NULL); 3581 #ifdef TCP_SAD_DETECTION 3582 } else if ((tcp_sad_limit != 0) && 3583 (rack->do_detection == 1) && 3584 (rack->r_ctl.rc_num_split_allocs >= tcp_sad_limit)) { 3585 counter_u64_add(rack_split_limited, 1); 3586 if (!rack->alloc_limit_reported) { 3587 rack->alloc_limit_reported = 1; 3588 counter_u64_add(rack_alloc_limited_conns, 1); 3589 } 3590 return (NULL); 3591 #endif 3592 } 3593 } 3594 3595 /* allocate and mark in the limit type, if set */ 3596 rsm = rack_alloc(rack); 3597 if (rsm != NULL && limit_type) { 3598 rsm->r_limit_type = limit_type; 3599 rack->r_ctl.rc_num_split_allocs++; 3600 } 3601 return (rsm); 3602 } 3603 3604 static void 3605 rack_free_trim(struct tcp_rack *rack) 3606 { 3607 struct rack_sendmap *rsm; 3608 3609 /* 3610 * Free up all the tail entries until 3611 * we get our list down to the limit. 3612 */ 3613 while (rack->rc_free_cnt > rack_free_cache) { 3614 rsm = TAILQ_LAST(&rack->r_ctl.rc_free, rack_head); 3615 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 3616 rack->rc_free_cnt--; 3617 rack->r_ctl.rc_num_maps_alloced--; 3618 uma_zfree(rack_zone, rsm); 3619 } 3620 } 3621 3622 static void 3623 rack_free(struct tcp_rack *rack, struct rack_sendmap *rsm) 3624 { 3625 if (rsm->r_flags & RACK_APP_LIMITED) { 3626 if (rack->r_ctl.rc_app_limited_cnt > 0) { 3627 rack->r_ctl.rc_app_limited_cnt--; 3628 } 3629 } 3630 if (rsm->r_limit_type) { 3631 /* currently there is only one limit type */ 3632 rack->r_ctl.rc_num_split_allocs--; 3633 } 3634 if (rsm == rack->r_ctl.rc_first_appl) { 3635 rack->r_ctl.cleared_app_ack_seq = rsm->r_start + (rsm->r_end - rsm->r_start); 3636 rack->r_ctl.cleared_app_ack = 1; 3637 if (rack->r_ctl.rc_app_limited_cnt == 0) 3638 rack->r_ctl.rc_first_appl = NULL; 3639 else 3640 rack->r_ctl.rc_first_appl = tqhash_find(rack->r_ctl.tqh, rsm->r_nseq_appl); 3641 } 3642 if (rsm == rack->r_ctl.rc_resend) 3643 rack->r_ctl.rc_resend = NULL; 3644 if (rsm == rack->r_ctl.rc_end_appl) 3645 rack->r_ctl.rc_end_appl = NULL; 3646 if (rack->r_ctl.rc_tlpsend == rsm) 3647 rack->r_ctl.rc_tlpsend = NULL; 3648 if (rack->r_ctl.rc_sacklast == rsm) 3649 rack->r_ctl.rc_sacklast = NULL; 3650 memset(rsm, 0, sizeof(struct rack_sendmap)); 3651 /* Make sure we are not going to overrun our count limit of 0xff */ 3652 if ((rack->rc_free_cnt + 1) > RACK_FREE_CNT_MAX) { 3653 rack_free_trim(rack); 3654 } 3655 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_free, rsm, r_tnext); 3656 rack->rc_free_cnt++; 3657 } 3658 3659 static uint32_t 3660 rack_get_measure_window(struct tcpcb *tp, struct tcp_rack *rack) 3661 { 3662 uint64_t srtt, bw, len, tim; 3663 uint32_t segsiz, def_len, minl; 3664 3665 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 3666 def_len = rack_def_data_window * segsiz; 3667 if (rack->rc_gp_filled == 0) { 3668 /* 3669 * We have no measurement (IW is in flight?) so 3670 * we can only guess using our data_window sysctl 3671 * value (usually 20MSS). 3672 */ 3673 return (def_len); 3674 } 3675 /* 3676 * Now we have a number of factors to consider. 3677 * 3678 * 1) We have a desired BDP which is usually 3679 * at least 2. 3680 * 2) We have a minimum number of rtt's usually 1 SRTT 3681 * but we allow it too to be more. 3682 * 3) We want to make sure a measurement last N useconds (if 3683 * we have set rack_min_measure_usec. 3684 * 3685 * We handle the first concern here by trying to create a data 3686 * window of max(rack_def_data_window, DesiredBDP). The 3687 * second concern we handle in not letting the measurement 3688 * window end normally until at least the required SRTT's 3689 * have gone by which is done further below in 3690 * rack_enough_for_measurement(). Finally the third concern 3691 * we also handle here by calculating how long that time 3692 * would take at the current BW and then return the 3693 * max of our first calculation and that length. Note 3694 * that if rack_min_measure_usec is 0, we don't deal 3695 * with concern 3. Also for both Concern 1 and 3 an 3696 * application limited period could end the measurement 3697 * earlier. 3698 * 3699 * So lets calculate the BDP with the "known" b/w using 3700 * the SRTT has our rtt and then multiply it by the 3701 * goal. 3702 */ 3703 bw = rack_get_bw(rack); 3704 srtt = (uint64_t)tp->t_srtt; 3705 len = bw * srtt; 3706 len /= (uint64_t)HPTS_USEC_IN_SEC; 3707 len *= max(1, rack_goal_bdp); 3708 /* Now we need to round up to the nearest MSS */ 3709 len = roundup(len, segsiz); 3710 if (rack_min_measure_usec) { 3711 /* Now calculate our min length for this b/w */ 3712 tim = rack_min_measure_usec; 3713 minl = (tim * bw) / (uint64_t)HPTS_USEC_IN_SEC; 3714 if (minl == 0) 3715 minl = 1; 3716 minl = roundup(minl, segsiz); 3717 if (len < minl) 3718 len = minl; 3719 } 3720 /* 3721 * Now if we have a very small window we want 3722 * to attempt to get the window that is 3723 * as small as possible. This happens on 3724 * low b/w connections and we don't want to 3725 * span huge numbers of rtt's between measurements. 3726 * 3727 * We basically include 2 over our "MIN window" so 3728 * that the measurement can be shortened (possibly) by 3729 * an ack'ed packet. 3730 */ 3731 if (len < def_len) 3732 return (max((uint32_t)len, ((MIN_GP_WIN+2) * segsiz))); 3733 else 3734 return (max((uint32_t)len, def_len)); 3735 3736 } 3737 3738 static int 3739 rack_enough_for_measurement(struct tcpcb *tp, struct tcp_rack *rack, tcp_seq th_ack, uint8_t *quality) 3740 { 3741 uint32_t tim, srtts, segsiz; 3742 3743 /* 3744 * Has enough time passed for the GP measurement to be valid? 3745 */ 3746 if (SEQ_LT(th_ack, tp->gput_seq)) { 3747 /* Not enough bytes yet */ 3748 return (0); 3749 } 3750 if ((tp->snd_max == tp->snd_una) || 3751 (th_ack == tp->snd_max)){ 3752 /* 3753 * All is acked quality of all acked is 3754 * usually low or medium, but we in theory could split 3755 * all acked into two cases, where you got 3756 * a signifigant amount of your window and 3757 * where you did not. For now we leave it 3758 * but it is something to contemplate in the 3759 * future. The danger here is that delayed ack 3760 * is effecting the last byte (which is a 50:50 chance). 3761 */ 3762 *quality = RACK_QUALITY_ALLACKED; 3763 return (1); 3764 } 3765 if (SEQ_GEQ(th_ack, tp->gput_ack)) { 3766 /* 3767 * We obtained our entire window of data we wanted 3768 * no matter if we are in recovery or not then 3769 * its ok since expanding the window does not 3770 * make things fuzzy (or at least not as much). 3771 */ 3772 *quality = RACK_QUALITY_HIGH; 3773 return (1); 3774 } 3775 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 3776 if (SEQ_LT(th_ack, tp->gput_ack) && 3777 ((th_ack - tp->gput_seq) < max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) { 3778 /* Not enough bytes yet */ 3779 return (0); 3780 } 3781 if (rack->r_ctl.rc_first_appl && 3782 (SEQ_GEQ(th_ack, rack->r_ctl.rc_first_appl->r_end))) { 3783 /* 3784 * We are up to the app limited send point 3785 * we have to measure irrespective of the time.. 3786 */ 3787 *quality = RACK_QUALITY_APPLIMITED; 3788 return (1); 3789 } 3790 /* Now what about time? */ 3791 srtts = (rack->r_ctl.rc_gp_srtt * rack_min_srtts); 3792 tim = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - tp->gput_ts; 3793 if ((tim >= srtts) && (IN_RECOVERY(rack->rc_tp->t_flags) == 0)) { 3794 /* 3795 * We do not allow a measurement if we are in recovery 3796 * that would shrink the goodput window we wanted. 3797 * This is to prevent cloudyness of when the last send 3798 * was actually made. 3799 */ 3800 *quality = RACK_QUALITY_HIGH; 3801 return (1); 3802 } 3803 /* Nope not even a full SRTT has passed */ 3804 return (0); 3805 } 3806 3807 static void 3808 rack_log_timely(struct tcp_rack *rack, 3809 uint32_t logged, uint64_t cur_bw, uint64_t low_bnd, 3810 uint64_t up_bnd, int line, uint8_t method) 3811 { 3812 if (tcp_bblogging_on(rack->rc_tp)) { 3813 union tcp_log_stackspecific log; 3814 struct timeval tv; 3815 3816 memset(&log, 0, sizeof(log)); 3817 log.u_bbr.flex1 = logged; 3818 log.u_bbr.flex2 = rack->rc_gp_timely_inc_cnt; 3819 log.u_bbr.flex2 <<= 4; 3820 log.u_bbr.flex2 |= rack->rc_gp_timely_dec_cnt; 3821 log.u_bbr.flex2 <<= 4; 3822 log.u_bbr.flex2 |= rack->rc_gp_incr; 3823 log.u_bbr.flex2 <<= 4; 3824 log.u_bbr.flex2 |= rack->rc_gp_bwred; 3825 log.u_bbr.flex3 = rack->rc_gp_incr; 3826 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss; 3827 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ca; 3828 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_rec; 3829 log.u_bbr.flex7 = rack->rc_gp_bwred; 3830 log.u_bbr.flex8 = method; 3831 log.u_bbr.cur_del_rate = cur_bw; 3832 log.u_bbr.delRate = low_bnd; 3833 log.u_bbr.bw_inuse = up_bnd; 3834 log.u_bbr.rttProp = rack_get_bw(rack); 3835 log.u_bbr.pkt_epoch = line; 3836 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff; 3837 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 3838 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 3839 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt; 3840 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt; 3841 log.u_bbr.cwnd_gain = rack->rc_dragged_bottom; 3842 log.u_bbr.cwnd_gain <<= 1; 3843 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_rec; 3844 log.u_bbr.cwnd_gain <<= 1; 3845 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss; 3846 log.u_bbr.cwnd_gain <<= 1; 3847 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca; 3848 log.u_bbr.lost = rack->r_ctl.rc_loss_count; 3849 TCP_LOG_EVENTP(rack->rc_tp, NULL, 3850 &rack->rc_inp->inp_socket->so_rcv, 3851 &rack->rc_inp->inp_socket->so_snd, 3852 TCP_TIMELY_WORK, 0, 3853 0, &log, false, &tv); 3854 } 3855 } 3856 3857 static int 3858 rack_bw_can_be_raised(struct tcp_rack *rack, uint64_t cur_bw, uint64_t last_bw_est, uint16_t mult) 3859 { 3860 /* 3861 * Before we increase we need to know if 3862 * the estimate just made was less than 3863 * our pacing goal (i.e. (cur_bw * mult) > last_bw_est) 3864 * 3865 * If we already are pacing at a fast enough 3866 * rate to push us faster there is no sense of 3867 * increasing. 3868 * 3869 * We first caculate our actual pacing rate (ss or ca multiplier 3870 * times our cur_bw). 3871 * 3872 * Then we take the last measured rate and multipy by our 3873 * maximum pacing overage to give us a max allowable rate. 3874 * 3875 * If our act_rate is smaller than our max_allowable rate 3876 * then we should increase. Else we should hold steady. 3877 * 3878 */ 3879 uint64_t act_rate, max_allow_rate; 3880 3881 if (rack_timely_no_stopping) 3882 return (1); 3883 3884 if ((cur_bw == 0) || (last_bw_est == 0)) { 3885 /* 3886 * Initial startup case or 3887 * everything is acked case. 3888 */ 3889 rack_log_timely(rack, mult, cur_bw, 0, 0, 3890 __LINE__, 9); 3891 return (1); 3892 } 3893 if (mult <= 100) { 3894 /* 3895 * We can always pace at or slightly above our rate. 3896 */ 3897 rack_log_timely(rack, mult, cur_bw, 0, 0, 3898 __LINE__, 9); 3899 return (1); 3900 } 3901 act_rate = cur_bw * (uint64_t)mult; 3902 act_rate /= 100; 3903 max_allow_rate = last_bw_est * ((uint64_t)rack_max_per_above + (uint64_t)100); 3904 max_allow_rate /= 100; 3905 if (act_rate < max_allow_rate) { 3906 /* 3907 * Here the rate we are actually pacing at 3908 * is smaller than 10% above our last measurement. 3909 * This means we are pacing below what we would 3910 * like to try to achieve (plus some wiggle room). 3911 */ 3912 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate, 3913 __LINE__, 9); 3914 return (1); 3915 } else { 3916 /* 3917 * Here we are already pacing at least rack_max_per_above(10%) 3918 * what we are getting back. This indicates most likely 3919 * that we are being limited (cwnd/rwnd/app) and can't 3920 * get any more b/w. There is no sense of trying to 3921 * raise up the pacing rate its not speeding us up 3922 * and we already are pacing faster than we are getting. 3923 */ 3924 rack_log_timely(rack, mult, cur_bw, act_rate, max_allow_rate, 3925 __LINE__, 8); 3926 return (0); 3927 } 3928 } 3929 3930 static void 3931 rack_validate_multipliers_at_or_above100(struct tcp_rack *rack) 3932 { 3933 /* 3934 * When we drag bottom, we want to assure 3935 * that no multiplier is below 1.0, if so 3936 * we want to restore it to at least that. 3937 */ 3938 if (rack->r_ctl.rack_per_of_gp_rec < 100) { 3939 /* This is unlikely we usually do not touch recovery */ 3940 rack->r_ctl.rack_per_of_gp_rec = 100; 3941 } 3942 if (rack->r_ctl.rack_per_of_gp_ca < 100) { 3943 rack->r_ctl.rack_per_of_gp_ca = 100; 3944 } 3945 if (rack->r_ctl.rack_per_of_gp_ss < 100) { 3946 rack->r_ctl.rack_per_of_gp_ss = 100; 3947 } 3948 } 3949 3950 static void 3951 rack_validate_multipliers_at_or_below_100(struct tcp_rack *rack) 3952 { 3953 if (rack->r_ctl.rack_per_of_gp_ca > 100) { 3954 rack->r_ctl.rack_per_of_gp_ca = 100; 3955 } 3956 if (rack->r_ctl.rack_per_of_gp_ss > 100) { 3957 rack->r_ctl.rack_per_of_gp_ss = 100; 3958 } 3959 } 3960 3961 static void 3962 rack_increase_bw_mul(struct tcp_rack *rack, int timely_says, uint64_t cur_bw, uint64_t last_bw_est, int override) 3963 { 3964 int32_t calc, logged, plus; 3965 3966 logged = 0; 3967 3968 if (rack->rc_skip_timely) 3969 return; 3970 if (override) { 3971 /* 3972 * override is passed when we are 3973 * loosing b/w and making one last 3974 * gasp at trying to not loose out 3975 * to a new-reno flow. 3976 */ 3977 goto extra_boost; 3978 } 3979 /* In classic timely we boost by 5x if we have 5 increases in a row, lets not */ 3980 if (rack->rc_gp_incr && 3981 ((rack->rc_gp_timely_inc_cnt + 1) >= RACK_TIMELY_CNT_BOOST)) { 3982 /* 3983 * Reset and get 5 strokes more before the boost. Note 3984 * that the count is 0 based so we have to add one. 3985 */ 3986 extra_boost: 3987 plus = (uint32_t)rack_gp_increase_per * RACK_TIMELY_CNT_BOOST; 3988 rack->rc_gp_timely_inc_cnt = 0; 3989 } else 3990 plus = (uint32_t)rack_gp_increase_per; 3991 /* Must be at least 1% increase for true timely increases */ 3992 if ((plus < 1) && 3993 ((rack->r_ctl.rc_rtt_diff <= 0) || (timely_says <= 0))) 3994 plus = 1; 3995 if (rack->rc_gp_saw_rec && 3996 (rack->rc_gp_no_rec_chg == 0) && 3997 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 3998 rack->r_ctl.rack_per_of_gp_rec)) { 3999 /* We have been in recovery ding it too */ 4000 calc = rack->r_ctl.rack_per_of_gp_rec + plus; 4001 if (calc > 0xffff) 4002 calc = 0xffff; 4003 logged |= 1; 4004 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)calc; 4005 if (rack->r_ctl.rack_per_upper_bound_ca && 4006 (rack->rc_dragged_bottom == 0) && 4007 (rack->r_ctl.rack_per_of_gp_rec > rack->r_ctl.rack_per_upper_bound_ca)) 4008 rack->r_ctl.rack_per_of_gp_rec = rack->r_ctl.rack_per_upper_bound_ca; 4009 } 4010 if (rack->rc_gp_saw_ca && 4011 (rack->rc_gp_saw_ss == 0) && 4012 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 4013 rack->r_ctl.rack_per_of_gp_ca)) { 4014 /* In CA */ 4015 calc = rack->r_ctl.rack_per_of_gp_ca + plus; 4016 if (calc > 0xffff) 4017 calc = 0xffff; 4018 logged |= 2; 4019 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)calc; 4020 if (rack->r_ctl.rack_per_upper_bound_ca && 4021 (rack->rc_dragged_bottom == 0) && 4022 (rack->r_ctl.rack_per_of_gp_ca > rack->r_ctl.rack_per_upper_bound_ca)) 4023 rack->r_ctl.rack_per_of_gp_ca = rack->r_ctl.rack_per_upper_bound_ca; 4024 } 4025 if (rack->rc_gp_saw_ss && 4026 rack_bw_can_be_raised(rack, cur_bw, last_bw_est, 4027 rack->r_ctl.rack_per_of_gp_ss)) { 4028 /* In SS */ 4029 calc = rack->r_ctl.rack_per_of_gp_ss + plus; 4030 if (calc > 0xffff) 4031 calc = 0xffff; 4032 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)calc; 4033 if (rack->r_ctl.rack_per_upper_bound_ss && 4034 (rack->rc_dragged_bottom == 0) && 4035 (rack->r_ctl.rack_per_of_gp_ss > rack->r_ctl.rack_per_upper_bound_ss)) 4036 rack->r_ctl.rack_per_of_gp_ss = rack->r_ctl.rack_per_upper_bound_ss; 4037 logged |= 4; 4038 } 4039 if (logged && 4040 (rack->rc_gp_incr == 0)){ 4041 /* Go into increment mode */ 4042 rack->rc_gp_incr = 1; 4043 rack->rc_gp_timely_inc_cnt = 0; 4044 } 4045 if (rack->rc_gp_incr && 4046 logged && 4047 (rack->rc_gp_timely_inc_cnt < RACK_TIMELY_CNT_BOOST)) { 4048 rack->rc_gp_timely_inc_cnt++; 4049 } 4050 rack_log_timely(rack, logged, plus, 0, 0, 4051 __LINE__, 1); 4052 } 4053 4054 static uint32_t 4055 rack_get_decrease(struct tcp_rack *rack, uint32_t curper, int32_t rtt_diff) 4056 { 4057 /*- 4058 * norm_grad = rtt_diff / minrtt; 4059 * new_per = curper * (1 - B * norm_grad) 4060 * 4061 * B = rack_gp_decrease_per (default 80%) 4062 * rtt_dif = input var current rtt-diff 4063 * curper = input var current percentage 4064 * minrtt = from rack filter 4065 * 4066 * In order to do the floating point calculations above we 4067 * do an integer conversion. The code looks confusing so let me 4068 * translate it into something that use more variables and 4069 * is clearer for us humans :) 4070 * 4071 * uint64_t norm_grad, inverse, reduce_by, final_result; 4072 * uint32_t perf; 4073 * 4074 * norm_grad = (((uint64_t)rtt_diff * 1000000) / 4075 * (uint64_t)get_filter_small(&rack->r_ctl.rc_gp_min_rtt)); 4076 * inverse = ((uint64_t)rack_gp_decrease * (uint64_t)1000000) * norm_grad; 4077 * inverse /= 1000000; 4078 * reduce_by = (1000000 - inverse); 4079 * final_result = (cur_per * reduce_by) / 1000000; 4080 * perf = (uint32_t)final_result; 4081 */ 4082 uint64_t perf; 4083 4084 perf = (((uint64_t)curper * ((uint64_t)1000000 - 4085 ((uint64_t)rack_gp_decrease_per * (uint64_t)10000 * 4086 (((uint64_t)rtt_diff * (uint64_t)1000000)/ 4087 (uint64_t)get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)))/ 4088 (uint64_t)1000000)) / 4089 (uint64_t)1000000); 4090 if (perf > curper) { 4091 /* TSNH */ 4092 perf = curper - 1; 4093 } 4094 return ((uint32_t)perf); 4095 } 4096 4097 static uint32_t 4098 rack_decrease_highrtt(struct tcp_rack *rack, uint32_t curper, uint32_t rtt) 4099 { 4100 /* 4101 * highrttthresh 4102 * result = curper * (1 - (B * ( 1 - ------ )) 4103 * gp_srtt 4104 * 4105 * B = rack_gp_decrease_per (default .8 i.e. 80) 4106 * highrttthresh = filter_min * rack_gp_rtt_maxmul 4107 */ 4108 uint64_t perf; 4109 uint32_t highrttthresh; 4110 4111 highrttthresh = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul; 4112 4113 perf = (((uint64_t)curper * ((uint64_t)1000000 - 4114 ((uint64_t)rack_gp_decrease_per * ((uint64_t)1000000 - 4115 ((uint64_t)highrttthresh * (uint64_t)1000000) / 4116 (uint64_t)rtt)) / 100)) /(uint64_t)1000000); 4117 if (tcp_bblogging_on(rack->rc_tp)) { 4118 uint64_t log1; 4119 4120 log1 = rtt; 4121 log1 <<= 32; 4122 log1 |= highrttthresh; 4123 rack_log_timely(rack, 4124 rack_gp_decrease_per, 4125 (uint64_t)curper, 4126 log1, 4127 perf, 4128 __LINE__, 4129 15); 4130 } 4131 return (perf); 4132 } 4133 4134 static void 4135 rack_decrease_bw_mul(struct tcp_rack *rack, int timely_says, uint32_t rtt, int32_t rtt_diff) 4136 { 4137 uint64_t logvar, logvar2, logvar3; 4138 uint32_t logged, new_per, ss_red, ca_red, rec_red, alt, val; 4139 4140 if (rack->rc_skip_timely) 4141 return; 4142 if (rack->rc_gp_incr) { 4143 /* Turn off increment counting */ 4144 rack->rc_gp_incr = 0; 4145 rack->rc_gp_timely_inc_cnt = 0; 4146 } 4147 ss_red = ca_red = rec_red = 0; 4148 logged = 0; 4149 /* Calculate the reduction value */ 4150 if (rtt_diff < 0) { 4151 rtt_diff *= -1; 4152 } 4153 /* Must be at least 1% reduction */ 4154 if (rack->rc_gp_saw_rec && (rack->rc_gp_no_rec_chg == 0)) { 4155 /* We have been in recovery ding it too */ 4156 if (timely_says == 2) { 4157 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_rec, rtt); 4158 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 4159 if (alt < new_per) 4160 val = alt; 4161 else 4162 val = new_per; 4163 } else 4164 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_rec, rtt_diff); 4165 if (rack->r_ctl.rack_per_of_gp_rec > val) { 4166 rec_red = (rack->r_ctl.rack_per_of_gp_rec - val); 4167 rack->r_ctl.rack_per_of_gp_rec = (uint16_t)val; 4168 } else { 4169 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound; 4170 rec_red = 0; 4171 } 4172 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_rec) 4173 rack->r_ctl.rack_per_of_gp_rec = rack_per_lower_bound; 4174 logged |= 1; 4175 } 4176 if (rack->rc_gp_saw_ss) { 4177 /* Sent in SS */ 4178 if (timely_says == 2) { 4179 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ss, rtt); 4180 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff); 4181 if (alt < new_per) 4182 val = alt; 4183 else 4184 val = new_per; 4185 } else 4186 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ss, rtt_diff); 4187 if (rack->r_ctl.rack_per_of_gp_ss > new_per) { 4188 ss_red = rack->r_ctl.rack_per_of_gp_ss - val; 4189 rack->r_ctl.rack_per_of_gp_ss = (uint16_t)val; 4190 } else { 4191 ss_red = new_per; 4192 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound; 4193 logvar = new_per; 4194 logvar <<= 32; 4195 logvar |= alt; 4196 logvar2 = (uint32_t)rtt; 4197 logvar2 <<= 32; 4198 logvar2 |= (uint32_t)rtt_diff; 4199 logvar3 = rack_gp_rtt_maxmul; 4200 logvar3 <<= 32; 4201 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 4202 rack_log_timely(rack, timely_says, 4203 logvar2, logvar3, 4204 logvar, __LINE__, 10); 4205 } 4206 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ss) 4207 rack->r_ctl.rack_per_of_gp_ss = rack_per_lower_bound; 4208 logged |= 4; 4209 } else if (rack->rc_gp_saw_ca) { 4210 /* Sent in CA */ 4211 if (timely_says == 2) { 4212 new_per = rack_decrease_highrtt(rack, rack->r_ctl.rack_per_of_gp_ca, rtt); 4213 alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff); 4214 if (alt < new_per) 4215 val = alt; 4216 else 4217 val = new_per; 4218 } else 4219 val = new_per = alt = rack_get_decrease(rack, rack->r_ctl.rack_per_of_gp_ca, rtt_diff); 4220 if (rack->r_ctl.rack_per_of_gp_ca > val) { 4221 ca_red = rack->r_ctl.rack_per_of_gp_ca - val; 4222 rack->r_ctl.rack_per_of_gp_ca = (uint16_t)val; 4223 } else { 4224 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound; 4225 ca_red = 0; 4226 logvar = new_per; 4227 logvar <<= 32; 4228 logvar |= alt; 4229 logvar2 = (uint32_t)rtt; 4230 logvar2 <<= 32; 4231 logvar2 |= (uint32_t)rtt_diff; 4232 logvar3 = rack_gp_rtt_maxmul; 4233 logvar3 <<= 32; 4234 logvar3 |= get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 4235 rack_log_timely(rack, timely_says, 4236 logvar2, logvar3, 4237 logvar, __LINE__, 10); 4238 } 4239 if (rack_per_lower_bound > rack->r_ctl.rack_per_of_gp_ca) 4240 rack->r_ctl.rack_per_of_gp_ca = rack_per_lower_bound; 4241 logged |= 2; 4242 } 4243 if (rack->rc_gp_timely_dec_cnt < 0x7) { 4244 rack->rc_gp_timely_dec_cnt++; 4245 if (rack_timely_dec_clear && 4246 (rack->rc_gp_timely_dec_cnt == rack_timely_dec_clear)) 4247 rack->rc_gp_timely_dec_cnt = 0; 4248 } 4249 logvar = ss_red; 4250 logvar <<= 32; 4251 logvar |= ca_red; 4252 rack_log_timely(rack, logged, rec_red, rack_per_lower_bound, logvar, 4253 __LINE__, 2); 4254 } 4255 4256 static void 4257 rack_log_rtt_shrinks(struct tcp_rack *rack, uint32_t us_cts, 4258 uint32_t rtt, uint32_t line, uint8_t reas) 4259 { 4260 if (tcp_bblogging_on(rack->rc_tp)) { 4261 union tcp_log_stackspecific log; 4262 struct timeval tv; 4263 4264 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 4265 log.u_bbr.flex1 = line; 4266 log.u_bbr.flex2 = rack->r_ctl.rc_time_probertt_starts; 4267 log.u_bbr.flex3 = rack->r_ctl.rc_lower_rtt_us_cts; 4268 log.u_bbr.flex4 = rack->r_ctl.rack_per_of_gp_ss; 4269 log.u_bbr.flex5 = rtt; 4270 log.u_bbr.flex6 = rack->rc_highly_buffered; 4271 log.u_bbr.flex6 <<= 1; 4272 log.u_bbr.flex6 |= rack->forced_ack; 4273 log.u_bbr.flex6 <<= 1; 4274 log.u_bbr.flex6 |= rack->rc_gp_dyn_mul; 4275 log.u_bbr.flex6 <<= 1; 4276 log.u_bbr.flex6 |= rack->in_probe_rtt; 4277 log.u_bbr.flex6 <<= 1; 4278 log.u_bbr.flex6 |= rack->measure_saw_probe_rtt; 4279 log.u_bbr.flex7 = rack->r_ctl.rack_per_of_gp_probertt; 4280 log.u_bbr.pacing_gain = rack->r_ctl.rack_per_of_gp_ca; 4281 log.u_bbr.cwnd_gain = rack->r_ctl.rack_per_of_gp_rec; 4282 log.u_bbr.flex8 = reas; 4283 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 4284 log.u_bbr.delRate = rack_get_bw(rack); 4285 log.u_bbr.cur_del_rate = rack->r_ctl.rc_highest_us_rtt; 4286 log.u_bbr.cur_del_rate <<= 32; 4287 log.u_bbr.cur_del_rate |= rack->r_ctl.rc_lowest_us_rtt; 4288 log.u_bbr.applimited = rack->r_ctl.rc_time_probertt_entered; 4289 log.u_bbr.pkts_out = rack->r_ctl.rc_rtt_diff; 4290 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 4291 log.u_bbr.epoch = rack->r_ctl.rc_gp_srtt; 4292 log.u_bbr.lt_epoch = rack->r_ctl.rc_prev_gp_srtt; 4293 log.u_bbr.pkt_epoch = rack->r_ctl.rc_lower_rtt_us_cts; 4294 log.u_bbr.delivered = rack->r_ctl.rc_target_probertt_flight; 4295 log.u_bbr.lost = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 4296 log.u_bbr.rttProp = us_cts; 4297 log.u_bbr.rttProp <<= 32; 4298 log.u_bbr.rttProp |= rack->r_ctl.rc_entry_gp_rtt; 4299 TCP_LOG_EVENTP(rack->rc_tp, NULL, 4300 &rack->rc_inp->inp_socket->so_rcv, 4301 &rack->rc_inp->inp_socket->so_snd, 4302 BBR_LOG_RTT_SHRINKS, 0, 4303 0, &log, false, &rack->r_ctl.act_rcv_time); 4304 } 4305 } 4306 4307 static void 4308 rack_set_prtt_target(struct tcp_rack *rack, uint32_t segsiz, uint32_t rtt) 4309 { 4310 uint64_t bwdp; 4311 4312 bwdp = rack_get_bw(rack); 4313 bwdp *= (uint64_t)rtt; 4314 bwdp /= (uint64_t)HPTS_USEC_IN_SEC; 4315 rack->r_ctl.rc_target_probertt_flight = roundup((uint32_t)bwdp, segsiz); 4316 if (rack->r_ctl.rc_target_probertt_flight < (segsiz * rack_timely_min_segs)) { 4317 /* 4318 * A window protocol must be able to have 4 packets 4319 * outstanding as the floor in order to function 4320 * (especially considering delayed ack :D). 4321 */ 4322 rack->r_ctl.rc_target_probertt_flight = (segsiz * rack_timely_min_segs); 4323 } 4324 } 4325 4326 static void 4327 rack_enter_probertt(struct tcp_rack *rack, uint32_t us_cts) 4328 { 4329 /** 4330 * ProbeRTT is a bit different in rack_pacing than in 4331 * BBR. It is like BBR in that it uses the lowering of 4332 * the RTT as a signal that we saw something new and 4333 * counts from there for how long between. But it is 4334 * different in that its quite simple. It does not 4335 * play with the cwnd and wait until we get down 4336 * to N segments outstanding and hold that for 4337 * 200ms. Instead it just sets the pacing reduction 4338 * rate to a set percentage (70 by default) and hold 4339 * that for a number of recent GP Srtt's. 4340 */ 4341 uint32_t segsiz; 4342 4343 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 4344 if (rack->rc_gp_dyn_mul == 0) 4345 return; 4346 4347 if (rack->rc_tp->snd_max == rack->rc_tp->snd_una) { 4348 /* We are idle */ 4349 return; 4350 } 4351 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) && 4352 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) { 4353 /* 4354 * Stop the goodput now, the idea here is 4355 * that future measurements with in_probe_rtt 4356 * won't register if they are not greater so 4357 * we want to get what info (if any) is available 4358 * now. 4359 */ 4360 rack_do_goodput_measurement(rack->rc_tp, rack, 4361 rack->rc_tp->snd_una, __LINE__, 4362 RACK_QUALITY_PROBERTT); 4363 } 4364 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 4365 rack->r_ctl.rc_time_probertt_entered = us_cts; 4366 segsiz = min(ctf_fixed_maxseg(rack->rc_tp), 4367 rack->r_ctl.rc_pace_min_segs); 4368 rack->in_probe_rtt = 1; 4369 rack->measure_saw_probe_rtt = 1; 4370 rack->r_ctl.rc_time_probertt_starts = 0; 4371 rack->r_ctl.rc_entry_gp_rtt = rack->r_ctl.rc_gp_srtt; 4372 if (rack_probertt_use_min_rtt_entry) 4373 rack_set_prtt_target(rack, segsiz, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)); 4374 else 4375 rack_set_prtt_target(rack, segsiz, rack->r_ctl.rc_gp_srtt); 4376 rack_log_rtt_shrinks(rack, us_cts, get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4377 __LINE__, RACK_RTTS_ENTERPROBE); 4378 } 4379 4380 static void 4381 rack_exit_probertt(struct tcp_rack *rack, uint32_t us_cts) 4382 { 4383 struct rack_sendmap *rsm; 4384 uint32_t segsiz; 4385 4386 segsiz = min(ctf_fixed_maxseg(rack->rc_tp), 4387 rack->r_ctl.rc_pace_min_segs); 4388 rack->in_probe_rtt = 0; 4389 if ((rack->rc_tp->t_flags & TF_GPUTINPROG) && 4390 SEQ_GT(rack->rc_tp->snd_una, rack->rc_tp->gput_seq)) { 4391 /* 4392 * Stop the goodput now, the idea here is 4393 * that future measurements with in_probe_rtt 4394 * won't register if they are not greater so 4395 * we want to get what info (if any) is available 4396 * now. 4397 */ 4398 rack_do_goodput_measurement(rack->rc_tp, rack, 4399 rack->rc_tp->snd_una, __LINE__, 4400 RACK_QUALITY_PROBERTT); 4401 } else if (rack->rc_tp->t_flags & TF_GPUTINPROG) { 4402 /* 4403 * We don't have enough data to make a measurement. 4404 * So lets just stop and start here after exiting 4405 * probe-rtt. We probably are not interested in 4406 * the results anyway. 4407 */ 4408 rack->rc_tp->t_flags &= ~TF_GPUTINPROG; 4409 } 4410 /* 4411 * Measurements through the current snd_max are going 4412 * to be limited by the slower pacing rate. 4413 * 4414 * We need to mark these as app-limited so we 4415 * don't collapse the b/w. 4416 */ 4417 rsm = tqhash_max(rack->r_ctl.tqh); 4418 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) { 4419 if (rack->r_ctl.rc_app_limited_cnt == 0) 4420 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm; 4421 else { 4422 /* 4423 * Go out to the end app limited and mark 4424 * this new one as next and move the end_appl up 4425 * to this guy. 4426 */ 4427 if (rack->r_ctl.rc_end_appl) 4428 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start; 4429 rack->r_ctl.rc_end_appl = rsm; 4430 } 4431 rsm->r_flags |= RACK_APP_LIMITED; 4432 rack->r_ctl.rc_app_limited_cnt++; 4433 } 4434 /* 4435 * Now, we need to examine our pacing rate multipliers. 4436 * If its under 100%, we need to kick it back up to 4437 * 100%. We also don't let it be over our "max" above 4438 * the actual rate i.e. 100% + rack_clamp_atexit_prtt. 4439 * Note setting clamp_atexit_prtt to 0 has the effect 4440 * of setting CA/SS to 100% always at exit (which is 4441 * the default behavior). 4442 */ 4443 if (rack_probertt_clear_is) { 4444 rack->rc_gp_incr = 0; 4445 rack->rc_gp_bwred = 0; 4446 rack->rc_gp_timely_inc_cnt = 0; 4447 rack->rc_gp_timely_dec_cnt = 0; 4448 } 4449 /* Do we do any clamping at exit? */ 4450 if (rack->rc_highly_buffered && rack_atexit_prtt_hbp) { 4451 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt_hbp; 4452 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt_hbp; 4453 } 4454 if ((rack->rc_highly_buffered == 0) && rack_atexit_prtt) { 4455 rack->r_ctl.rack_per_of_gp_ca = rack_atexit_prtt; 4456 rack->r_ctl.rack_per_of_gp_ss = rack_atexit_prtt; 4457 } 4458 /* 4459 * Lets set rtt_diff to 0, so that we will get a "boost" 4460 * after exiting. 4461 */ 4462 rack->r_ctl.rc_rtt_diff = 0; 4463 4464 /* Clear all flags so we start fresh */ 4465 rack->rc_tp->t_bytes_acked = 0; 4466 rack->rc_tp->t_ccv.flags &= ~CCF_ABC_SENTAWND; 4467 /* 4468 * If configured to, set the cwnd and ssthresh to 4469 * our targets. 4470 */ 4471 if (rack_probe_rtt_sets_cwnd) { 4472 uint64_t ebdp; 4473 uint32_t setto; 4474 4475 /* Set ssthresh so we get into CA once we hit our target */ 4476 if (rack_probertt_use_min_rtt_exit == 1) { 4477 /* Set to min rtt */ 4478 rack_set_prtt_target(rack, segsiz, 4479 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt)); 4480 } else if (rack_probertt_use_min_rtt_exit == 2) { 4481 /* Set to current gp rtt */ 4482 rack_set_prtt_target(rack, segsiz, 4483 rack->r_ctl.rc_gp_srtt); 4484 } else if (rack_probertt_use_min_rtt_exit == 3) { 4485 /* Set to entry gp rtt */ 4486 rack_set_prtt_target(rack, segsiz, 4487 rack->r_ctl.rc_entry_gp_rtt); 4488 } else { 4489 uint64_t sum; 4490 uint32_t setval; 4491 4492 sum = rack->r_ctl.rc_entry_gp_rtt; 4493 sum *= 10; 4494 sum /= (uint64_t)(max(1, rack->r_ctl.rc_gp_srtt)); 4495 if (sum >= 20) { 4496 /* 4497 * A highly buffered path needs 4498 * cwnd space for timely to work. 4499 * Lets set things up as if 4500 * we are heading back here again. 4501 */ 4502 setval = rack->r_ctl.rc_entry_gp_rtt; 4503 } else if (sum >= 15) { 4504 /* 4505 * Lets take the smaller of the 4506 * two since we are just somewhat 4507 * buffered. 4508 */ 4509 setval = rack->r_ctl.rc_gp_srtt; 4510 if (setval > rack->r_ctl.rc_entry_gp_rtt) 4511 setval = rack->r_ctl.rc_entry_gp_rtt; 4512 } else { 4513 /* 4514 * Here we are not highly buffered 4515 * and should pick the min we can to 4516 * keep from causing loss. 4517 */ 4518 setval = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 4519 } 4520 rack_set_prtt_target(rack, segsiz, 4521 setval); 4522 } 4523 if (rack_probe_rtt_sets_cwnd > 1) { 4524 /* There is a percentage here to boost */ 4525 ebdp = rack->r_ctl.rc_target_probertt_flight; 4526 ebdp *= rack_probe_rtt_sets_cwnd; 4527 ebdp /= 100; 4528 setto = rack->r_ctl.rc_target_probertt_flight + ebdp; 4529 } else 4530 setto = rack->r_ctl.rc_target_probertt_flight; 4531 rack->rc_tp->snd_cwnd = roundup(setto, segsiz); 4532 if (rack->rc_tp->snd_cwnd < (segsiz * rack_timely_min_segs)) { 4533 /* Enforce a min */ 4534 rack->rc_tp->snd_cwnd = segsiz * rack_timely_min_segs; 4535 } 4536 /* If we set in the cwnd also set the ssthresh point so we are in CA */ 4537 rack->rc_tp->snd_ssthresh = (rack->rc_tp->snd_cwnd - 1); 4538 } 4539 rack_log_rtt_shrinks(rack, us_cts, 4540 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4541 __LINE__, RACK_RTTS_EXITPROBE); 4542 /* Clear times last so log has all the info */ 4543 rack->r_ctl.rc_probertt_sndmax_atexit = rack->rc_tp->snd_max; 4544 rack->r_ctl.rc_time_probertt_entered = us_cts; 4545 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 4546 rack->r_ctl.rc_time_of_last_probertt = us_cts; 4547 } 4548 4549 static void 4550 rack_check_probe_rtt(struct tcp_rack *rack, uint32_t us_cts) 4551 { 4552 /* Check in on probe-rtt */ 4553 4554 if (rack->rc_gp_filled == 0) { 4555 /* We do not do p-rtt unless we have gp measurements */ 4556 return; 4557 } 4558 if (rack->in_probe_rtt) { 4559 uint64_t no_overflow; 4560 uint32_t endtime, must_stay; 4561 4562 if (rack->r_ctl.rc_went_idle_time && 4563 ((us_cts - rack->r_ctl.rc_went_idle_time) > rack_min_probertt_hold)) { 4564 /* 4565 * We went idle during prtt, just exit now. 4566 */ 4567 rack_exit_probertt(rack, us_cts); 4568 } else if (rack_probe_rtt_safety_val && 4569 TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered) && 4570 ((us_cts - rack->r_ctl.rc_time_probertt_entered) > rack_probe_rtt_safety_val)) { 4571 /* 4572 * Probe RTT safety value triggered! 4573 */ 4574 rack_log_rtt_shrinks(rack, us_cts, 4575 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4576 __LINE__, RACK_RTTS_SAFETY); 4577 rack_exit_probertt(rack, us_cts); 4578 } 4579 /* Calculate the max we will wait */ 4580 endtime = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_max_drain_wait); 4581 if (rack->rc_highly_buffered) 4582 endtime += (rack->r_ctl.rc_gp_srtt * rack_max_drain_hbp); 4583 /* Calculate the min we must wait */ 4584 must_stay = rack->r_ctl.rc_time_probertt_entered + (rack->r_ctl.rc_gp_srtt * rack_must_drain); 4585 if ((ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.rc_target_probertt_flight) && 4586 TSTMP_LT(us_cts, endtime)) { 4587 uint32_t calc; 4588 /* Do we lower more? */ 4589 no_exit: 4590 if (TSTMP_GT(us_cts, rack->r_ctl.rc_time_probertt_entered)) 4591 calc = us_cts - rack->r_ctl.rc_time_probertt_entered; 4592 else 4593 calc = 0; 4594 calc /= max(rack->r_ctl.rc_gp_srtt, 1); 4595 if (calc) { 4596 /* Maybe */ 4597 calc *= rack_per_of_gp_probertt_reduce; 4598 if (calc > rack_per_of_gp_probertt) 4599 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh; 4600 else 4601 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt - calc; 4602 /* Limit it too */ 4603 if (rack->r_ctl.rack_per_of_gp_probertt < rack_per_of_gp_lowthresh) 4604 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_lowthresh; 4605 } 4606 /* We must reach target or the time set */ 4607 return; 4608 } 4609 if (rack->r_ctl.rc_time_probertt_starts == 0) { 4610 if ((TSTMP_LT(us_cts, must_stay) && 4611 rack->rc_highly_buffered) || 4612 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > 4613 rack->r_ctl.rc_target_probertt_flight)) { 4614 /* We are not past the must_stay time */ 4615 goto no_exit; 4616 } 4617 rack_log_rtt_shrinks(rack, us_cts, 4618 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4619 __LINE__, RACK_RTTS_REACHTARGET); 4620 rack->r_ctl.rc_time_probertt_starts = us_cts; 4621 if (rack->r_ctl.rc_time_probertt_starts == 0) 4622 rack->r_ctl.rc_time_probertt_starts = 1; 4623 /* Restore back to our rate we want to pace at in prtt */ 4624 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 4625 } 4626 /* 4627 * Setup our end time, some number of gp_srtts plus 200ms. 4628 */ 4629 no_overflow = ((uint64_t)rack->r_ctl.rc_gp_srtt * 4630 (uint64_t)rack_probertt_gpsrtt_cnt_mul); 4631 if (rack_probertt_gpsrtt_cnt_div) 4632 endtime = (uint32_t)(no_overflow / (uint64_t)rack_probertt_gpsrtt_cnt_div); 4633 else 4634 endtime = 0; 4635 endtime += rack_min_probertt_hold; 4636 endtime += rack->r_ctl.rc_time_probertt_starts; 4637 if (TSTMP_GEQ(us_cts, endtime)) { 4638 /* yes, exit probertt */ 4639 rack_exit_probertt(rack, us_cts); 4640 } 4641 4642 } else if ((rack->rc_skip_timely == 0) && 4643 (TSTMP_GT(us_cts, rack->r_ctl.rc_lower_rtt_us_cts)) && 4644 ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= rack_time_between_probertt)) { 4645 /* Go into probertt, its been too long since we went lower */ 4646 rack_enter_probertt(rack, us_cts); 4647 } 4648 } 4649 4650 static void 4651 rack_update_multiplier(struct tcp_rack *rack, int32_t timely_says, uint64_t last_bw_est, 4652 uint32_t rtt, int32_t rtt_diff) 4653 { 4654 uint64_t cur_bw, up_bnd, low_bnd, subfr; 4655 uint32_t losses; 4656 4657 if ((rack->rc_gp_dyn_mul == 0) || 4658 (rack->use_fixed_rate) || 4659 (rack->in_probe_rtt) || 4660 (rack->rc_always_pace == 0)) { 4661 /* No dynamic GP multiplier in play */ 4662 return; 4663 } 4664 losses = rack->r_ctl.rc_loss_count - rack->r_ctl.rc_loss_at_start; 4665 cur_bw = rack_get_bw(rack); 4666 /* Calculate our up and down range */ 4667 up_bnd = rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_up; 4668 up_bnd /= 100; 4669 up_bnd += rack->r_ctl.last_gp_comp_bw; 4670 4671 subfr = (uint64_t)rack->r_ctl.last_gp_comp_bw * (uint64_t)rack_gp_per_bw_mul_down; 4672 subfr /= 100; 4673 low_bnd = rack->r_ctl.last_gp_comp_bw - subfr; 4674 if ((timely_says == 2) && (rack->r_ctl.rc_no_push_at_mrtt)) { 4675 /* 4676 * This is the case where our RTT is above 4677 * the max target and we have been configured 4678 * to just do timely no bonus up stuff in that case. 4679 * 4680 * There are two configurations, set to 1, and we 4681 * just do timely if we are over our max. If its 4682 * set above 1 then we slam the multipliers down 4683 * to 100 and then decrement per timely. 4684 */ 4685 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4686 __LINE__, 3); 4687 if (rack->r_ctl.rc_no_push_at_mrtt > 1) 4688 rack_validate_multipliers_at_or_below_100(rack); 4689 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff); 4690 } else if ((timely_says != 0) && (last_bw_est < low_bnd) && !losses) { 4691 /* 4692 * We are decreasing this is a bit complicated this 4693 * means we are loosing ground. This could be 4694 * because another flow entered and we are competing 4695 * for b/w with it. This will push the RTT up which 4696 * makes timely unusable unless we want to get shoved 4697 * into a corner and just be backed off (the age 4698 * old problem with delay based CC). 4699 * 4700 * On the other hand if it was a route change we 4701 * would like to stay somewhat contained and not 4702 * blow out the buffers. 4703 */ 4704 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4705 __LINE__, 3); 4706 rack->r_ctl.last_gp_comp_bw = cur_bw; 4707 if (rack->rc_gp_bwred == 0) { 4708 /* Go into reduction counting */ 4709 rack->rc_gp_bwred = 1; 4710 rack->rc_gp_timely_dec_cnt = 0; 4711 } 4712 if (rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) { 4713 /* 4714 * Push another time with a faster pacing 4715 * to try to gain back (we include override to 4716 * get a full raise factor). 4717 */ 4718 if ((rack->rc_gp_saw_ca && rack->r_ctl.rack_per_of_gp_ca <= rack_down_raise_thresh) || 4719 (rack->rc_gp_saw_ss && rack->r_ctl.rack_per_of_gp_ss <= rack_down_raise_thresh) || 4720 (timely_says == 0) || 4721 (rack_down_raise_thresh == 0)) { 4722 /* 4723 * Do an override up in b/w if we were 4724 * below the threshold or if the threshold 4725 * is zero we always do the raise. 4726 */ 4727 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 1); 4728 } else { 4729 /* Log it stays the same */ 4730 rack_log_timely(rack, 0, last_bw_est, low_bnd, 0, 4731 __LINE__, 11); 4732 } 4733 rack->rc_gp_timely_dec_cnt++; 4734 /* We are not incrementing really no-count */ 4735 rack->rc_gp_incr = 0; 4736 rack->rc_gp_timely_inc_cnt = 0; 4737 } else { 4738 /* 4739 * Lets just use the RTT 4740 * information and give up 4741 * pushing. 4742 */ 4743 goto use_timely; 4744 } 4745 } else if ((timely_says != 2) && 4746 !losses && 4747 (last_bw_est > up_bnd)) { 4748 /* 4749 * We are increasing b/w lets keep going, updating 4750 * our b/w and ignoring any timely input, unless 4751 * of course we are at our max raise (if there is one). 4752 */ 4753 4754 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4755 __LINE__, 3); 4756 rack->r_ctl.last_gp_comp_bw = cur_bw; 4757 if (rack->rc_gp_saw_ss && 4758 rack->r_ctl.rack_per_upper_bound_ss && 4759 (rack->r_ctl.rack_per_of_gp_ss == rack->r_ctl.rack_per_upper_bound_ss)) { 4760 /* 4761 * In cases where we can't go higher 4762 * we should just use timely. 4763 */ 4764 goto use_timely; 4765 } 4766 if (rack->rc_gp_saw_ca && 4767 rack->r_ctl.rack_per_upper_bound_ca && 4768 (rack->r_ctl.rack_per_of_gp_ca == rack->r_ctl.rack_per_upper_bound_ca)) { 4769 /* 4770 * In cases where we can't go higher 4771 * we should just use timely. 4772 */ 4773 goto use_timely; 4774 } 4775 rack->rc_gp_bwred = 0; 4776 rack->rc_gp_timely_dec_cnt = 0; 4777 /* You get a set number of pushes if timely is trying to reduce */ 4778 if ((rack->rc_gp_incr < rack_timely_max_push_rise) || (timely_says == 0)) { 4779 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4780 } else { 4781 /* Log it stays the same */ 4782 rack_log_timely(rack, 0, last_bw_est, up_bnd, 0, 4783 __LINE__, 12); 4784 } 4785 return; 4786 } else { 4787 /* 4788 * We are staying between the lower and upper range bounds 4789 * so use timely to decide. 4790 */ 4791 rack_log_timely(rack, timely_says, cur_bw, low_bnd, up_bnd, 4792 __LINE__, 3); 4793 use_timely: 4794 if (timely_says) { 4795 rack->rc_gp_incr = 0; 4796 rack->rc_gp_timely_inc_cnt = 0; 4797 if ((rack->rc_gp_timely_dec_cnt < rack_timely_max_push_drop) && 4798 !losses && 4799 (last_bw_est < low_bnd)) { 4800 /* We are loosing ground */ 4801 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4802 rack->rc_gp_timely_dec_cnt++; 4803 /* We are not incrementing really no-count */ 4804 rack->rc_gp_incr = 0; 4805 rack->rc_gp_timely_inc_cnt = 0; 4806 } else 4807 rack_decrease_bw_mul(rack, timely_says, rtt, rtt_diff); 4808 } else { 4809 rack->rc_gp_bwred = 0; 4810 rack->rc_gp_timely_dec_cnt = 0; 4811 rack_increase_bw_mul(rack, timely_says, cur_bw, last_bw_est, 0); 4812 } 4813 } 4814 } 4815 4816 static int32_t 4817 rack_make_timely_judgement(struct tcp_rack *rack, uint32_t rtt, int32_t rtt_diff, uint32_t prev_rtt) 4818 { 4819 int32_t timely_says; 4820 uint64_t log_mult, log_rtt_a_diff; 4821 4822 log_rtt_a_diff = rtt; 4823 log_rtt_a_diff <<= 32; 4824 log_rtt_a_diff |= (uint32_t)rtt_diff; 4825 if (rtt >= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * 4826 rack_gp_rtt_maxmul)) { 4827 /* Reduce the b/w multiplier */ 4828 timely_says = 2; 4829 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_maxmul; 4830 log_mult <<= 32; 4831 log_mult |= prev_rtt; 4832 rack_log_timely(rack, timely_says, log_mult, 4833 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4834 log_rtt_a_diff, __LINE__, 4); 4835 } else if (rtt <= (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) + 4836 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) / 4837 max(rack_gp_rtt_mindiv , 1)))) { 4838 /* Increase the b/w multiplier */ 4839 log_mult = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) + 4840 ((get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack_gp_rtt_minmul) / 4841 max(rack_gp_rtt_mindiv , 1)); 4842 log_mult <<= 32; 4843 log_mult |= prev_rtt; 4844 timely_says = 0; 4845 rack_log_timely(rack, timely_says, log_mult , 4846 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), 4847 log_rtt_a_diff, __LINE__, 5); 4848 } else { 4849 /* 4850 * Use a gradient to find it the timely gradient 4851 * is: 4852 * grad = rc_rtt_diff / min_rtt; 4853 * 4854 * anything below or equal to 0 will be 4855 * a increase indication. Anything above 4856 * zero is a decrease. Note we take care 4857 * of the actual gradient calculation 4858 * in the reduction (its not needed for 4859 * increase). 4860 */ 4861 log_mult = prev_rtt; 4862 if (rtt_diff <= 0) { 4863 /* 4864 * Rttdiff is less than zero, increase the 4865 * b/w multiplier (its 0 or negative) 4866 */ 4867 timely_says = 0; 4868 rack_log_timely(rack, timely_says, log_mult, 4869 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 6); 4870 } else { 4871 /* Reduce the b/w multiplier */ 4872 timely_says = 1; 4873 rack_log_timely(rack, timely_says, log_mult, 4874 get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt), log_rtt_a_diff, __LINE__, 7); 4875 } 4876 } 4877 return (timely_says); 4878 } 4879 4880 static __inline int 4881 rack_in_gp_window(struct tcpcb *tp, struct rack_sendmap *rsm) 4882 { 4883 if (SEQ_GEQ(rsm->r_start, tp->gput_seq) && 4884 SEQ_LEQ(rsm->r_end, tp->gput_ack)) { 4885 /** 4886 * This covers the case that the 4887 * resent is completely inside 4888 * the gp range or up to it. 4889 * |----------------| 4890 * |-----| <or> 4891 * |----| 4892 * <or> |---| 4893 */ 4894 return (1); 4895 } else if (SEQ_LT(rsm->r_start, tp->gput_seq) && 4896 SEQ_GT(rsm->r_end, tp->gput_seq)){ 4897 /** 4898 * This covers the case of 4899 * |--------------| 4900 * |-------->| 4901 */ 4902 return (1); 4903 } else if (SEQ_GEQ(rsm->r_start, tp->gput_seq) && 4904 SEQ_LT(rsm->r_start, tp->gput_ack) && 4905 SEQ_GEQ(rsm->r_end, tp->gput_ack)) { 4906 4907 /** 4908 * This covers the case of 4909 * |--------------| 4910 * |-------->| 4911 */ 4912 return (1); 4913 } 4914 return (0); 4915 } 4916 4917 static __inline void 4918 rack_mark_in_gp_win(struct tcpcb *tp, struct rack_sendmap *rsm) 4919 { 4920 4921 if ((tp->t_flags & TF_GPUTINPROG) == 0) 4922 return; 4923 /* 4924 * We have a Goodput measurement in progress. Mark 4925 * the send if its within the window. If its not 4926 * in the window make sure it does not have the mark. 4927 */ 4928 if (rack_in_gp_window(tp, rsm)) 4929 rsm->r_flags |= RACK_IN_GP_WIN; 4930 else 4931 rsm->r_flags &= ~RACK_IN_GP_WIN; 4932 } 4933 4934 static __inline void 4935 rack_clear_gp_marks(struct tcpcb *tp, struct tcp_rack *rack) 4936 { 4937 /* A GP measurement is ending, clear all marks on the send map*/ 4938 struct rack_sendmap *rsm = NULL; 4939 4940 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq); 4941 if (rsm == NULL) { 4942 rsm = tqhash_min(rack->r_ctl.tqh); 4943 } 4944 /* Nothing left? */ 4945 while ((rsm != NULL) && (SEQ_GEQ(tp->gput_ack, rsm->r_start))){ 4946 rsm->r_flags &= ~RACK_IN_GP_WIN; 4947 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 4948 } 4949 } 4950 4951 4952 static __inline void 4953 rack_tend_gp_marks(struct tcpcb *tp, struct tcp_rack *rack) 4954 { 4955 struct rack_sendmap *rsm = NULL; 4956 4957 if (tp->snd_una == tp->snd_max) { 4958 /* Nothing outstanding yet, nothing to do here */ 4959 return; 4960 } 4961 if (SEQ_GT(tp->gput_seq, tp->snd_una)) { 4962 /* 4963 * We are measuring ahead of some outstanding 4964 * data. We need to walk through up until we get 4965 * to gp_seq marking so that no rsm is set incorrectly 4966 * with RACK_IN_GP_WIN. 4967 */ 4968 rsm = tqhash_min(rack->r_ctl.tqh); 4969 while (rsm != NULL) { 4970 rack_mark_in_gp_win(tp, rsm); 4971 if (SEQ_GEQ(rsm->r_end, tp->gput_seq)) 4972 break; 4973 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 4974 } 4975 } 4976 if (rsm == NULL) { 4977 /* 4978 * Need to find the GP seq, if rsm is 4979 * set we stopped as we hit it. 4980 */ 4981 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq); 4982 if (rsm == NULL) 4983 return; 4984 rack_mark_in_gp_win(tp, rsm); 4985 } 4986 /* 4987 * Now we may need to mark already sent rsm, ahead of 4988 * gput_seq in the window since they may have been sent 4989 * *before* we started our measurment. The rsm, if non-null 4990 * has been marked (note if rsm would have been NULL we would have 4991 * returned in the previous block). So we go to the next, and continue 4992 * until we run out of entries or we exceed the gp_ack value. 4993 */ 4994 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 4995 while (rsm) { 4996 rack_mark_in_gp_win(tp, rsm); 4997 if (SEQ_GT(rsm->r_end, tp->gput_ack)) 4998 break; 4999 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 5000 } 5001 } 5002 5003 static void 5004 rack_log_gp_calc(struct tcp_rack *rack, uint32_t add_part, uint32_t sub_part, uint32_t srtt, uint64_t meas_bw, uint64_t utim, uint8_t meth, uint32_t line) 5005 { 5006 if (tcp_bblogging_on(rack->rc_tp)) { 5007 union tcp_log_stackspecific log; 5008 struct timeval tv; 5009 5010 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5011 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5012 log.u_bbr.flex1 = add_part; 5013 log.u_bbr.flex2 = sub_part; 5014 log.u_bbr.flex3 = rack_wma_divisor; 5015 log.u_bbr.flex4 = srtt; 5016 log.u_bbr.flex7 = (uint16_t)line; 5017 log.u_bbr.flex8 = meth; 5018 log.u_bbr.delRate = rack->r_ctl.gp_bw; 5019 log.u_bbr.cur_del_rate = meas_bw; 5020 log.u_bbr.rttProp = utim; 5021 TCP_LOG_EVENTP(rack->rc_tp, NULL, 5022 &rack->rc_inp->inp_socket->so_rcv, 5023 &rack->rc_inp->inp_socket->so_snd, 5024 BBR_LOG_THRESH_CALC, 0, 5025 0, &log, false, &rack->r_ctl.act_rcv_time); 5026 } 5027 } 5028 5029 static void 5030 rack_do_goodput_measurement(struct tcpcb *tp, struct tcp_rack *rack, 5031 tcp_seq th_ack, int line, uint8_t quality) 5032 { 5033 uint64_t tim, bytes_ps, stim, utim; 5034 uint32_t segsiz, bytes, reqbytes, us_cts; 5035 int32_t gput, new_rtt_diff, timely_says; 5036 uint64_t resid_bw, subpart = 0, addpart = 0, srtt; 5037 int did_add = 0; 5038 5039 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 5040 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 5041 if (TSTMP_GEQ(us_cts, tp->gput_ts)) 5042 tim = us_cts - tp->gput_ts; 5043 else 5044 tim = 0; 5045 if (rack->r_ctl.rc_gp_cumack_ts > rack->r_ctl.rc_gp_output_ts) 5046 stim = rack->r_ctl.rc_gp_cumack_ts - rack->r_ctl.rc_gp_output_ts; 5047 else 5048 stim = 0; 5049 /* 5050 * Use the larger of the send time or ack time. This prevents us 5051 * from being influenced by ack artifacts to come up with too 5052 * high of measurement. Note that since we are spanning over many more 5053 * bytes in most of our measurements hopefully that is less likely to 5054 * occur. 5055 */ 5056 if (tim > stim) 5057 utim = max(tim, 1); 5058 else 5059 utim = max(stim, 1); 5060 reqbytes = min(rc_init_window(rack), (MIN_GP_WIN * segsiz)); 5061 rack_log_gpset(rack, th_ack, us_cts, rack->r_ctl.rc_gp_cumack_ts, __LINE__, 3, NULL); 5062 if ((tim == 0) && (stim == 0)) { 5063 /* 5064 * Invalid measurement time, maybe 5065 * all on one ack/one send? 5066 */ 5067 bytes = 0; 5068 bytes_ps = 0; 5069 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 5070 0, 0, 0, 10, __LINE__, NULL, quality); 5071 goto skip_measurement; 5072 } 5073 if (rack->r_ctl.rc_gp_lowrtt == 0xffffffff) { 5074 /* We never made a us_rtt measurement? */ 5075 bytes = 0; 5076 bytes_ps = 0; 5077 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 5078 0, 0, 0, 10, __LINE__, NULL, quality); 5079 goto skip_measurement; 5080 } 5081 /* 5082 * Calculate the maximum possible b/w this connection 5083 * could have. We base our calculation on the lowest 5084 * rtt we have seen during the measurement and the 5085 * largest rwnd the client has given us in that time. This 5086 * forms a BDP that is the maximum that we could ever 5087 * get to the client. Anything larger is not valid. 5088 * 5089 * I originally had code here that rejected measurements 5090 * where the time was less than 1/2 the latest us_rtt. 5091 * But after thinking on that I realized its wrong since 5092 * say you had a 150Mbps or even 1Gbps link, and you 5093 * were a long way away.. example I am in Europe (100ms rtt) 5094 * talking to my 1Gbps link in S.C. Now measuring say 150,000 5095 * bytes my time would be 1.2ms, and yet my rtt would say 5096 * the measurement was invalid the time was < 50ms. The 5097 * same thing is true for 150Mb (8ms of time). 5098 * 5099 * A better way I realized is to look at what the maximum 5100 * the connection could possibly do. This is gated on 5101 * the lowest RTT we have seen and the highest rwnd. 5102 * We should in theory never exceed that, if we are 5103 * then something on the path is storing up packets 5104 * and then feeding them all at once to our endpoint 5105 * messing up our measurement. 5106 */ 5107 rack->r_ctl.last_max_bw = rack->r_ctl.rc_gp_high_rwnd; 5108 rack->r_ctl.last_max_bw *= HPTS_USEC_IN_SEC; 5109 rack->r_ctl.last_max_bw /= rack->r_ctl.rc_gp_lowrtt; 5110 if (SEQ_LT(th_ack, tp->gput_seq)) { 5111 /* No measurement can be made */ 5112 bytes = 0; 5113 bytes_ps = 0; 5114 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 5115 0, 0, 0, 10, __LINE__, NULL, quality); 5116 goto skip_measurement; 5117 } else 5118 bytes = (th_ack - tp->gput_seq); 5119 bytes_ps = (uint64_t)bytes; 5120 /* 5121 * Don't measure a b/w for pacing unless we have gotten at least 5122 * an initial windows worth of data in this measurement interval. 5123 * 5124 * Small numbers of bytes get badly influenced by delayed ack and 5125 * other artifacts. Note we take the initial window or our 5126 * defined minimum GP (defaulting to 10 which hopefully is the 5127 * IW). 5128 */ 5129 if (rack->rc_gp_filled == 0) { 5130 /* 5131 * The initial estimate is special. We 5132 * have blasted out an IW worth of packets 5133 * without a real valid ack ts results. We 5134 * then setup the app_limited_needs_set flag, 5135 * this should get the first ack in (probably 2 5136 * MSS worth) to be recorded as the timestamp. 5137 * We thus allow a smaller number of bytes i.e. 5138 * IW - 2MSS. 5139 */ 5140 reqbytes -= (2 * segsiz); 5141 /* Also lets fill previous for our first measurement to be neutral */ 5142 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt; 5143 } 5144 if ((bytes_ps < reqbytes) || rack->app_limited_needs_set) { 5145 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 5146 rack->r_ctl.rc_app_limited_cnt, 5147 0, 0, 10, __LINE__, NULL, quality); 5148 goto skip_measurement; 5149 } 5150 /* 5151 * We now need to calculate the Timely like status so 5152 * we can update (possibly) the b/w multipliers. 5153 */ 5154 new_rtt_diff = (int32_t)rack->r_ctl.rc_gp_srtt - (int32_t)rack->r_ctl.rc_prev_gp_srtt; 5155 if (rack->rc_gp_filled == 0) { 5156 /* No previous reading */ 5157 rack->r_ctl.rc_rtt_diff = new_rtt_diff; 5158 } else { 5159 if (rack->measure_saw_probe_rtt == 0) { 5160 /* 5161 * We don't want a probertt to be counted 5162 * since it will be negative incorrectly. We 5163 * expect to be reducing the RTT when we 5164 * pace at a slower rate. 5165 */ 5166 rack->r_ctl.rc_rtt_diff -= (rack->r_ctl.rc_rtt_diff / 8); 5167 rack->r_ctl.rc_rtt_diff += (new_rtt_diff / 8); 5168 } 5169 } 5170 timely_says = rack_make_timely_judgement(rack, 5171 rack->r_ctl.rc_gp_srtt, 5172 rack->r_ctl.rc_rtt_diff, 5173 rack->r_ctl.rc_prev_gp_srtt 5174 ); 5175 bytes_ps *= HPTS_USEC_IN_SEC; 5176 bytes_ps /= utim; 5177 if (bytes_ps > rack->r_ctl.last_max_bw) { 5178 /* 5179 * Something is on path playing 5180 * since this b/w is not possible based 5181 * on our BDP (highest rwnd and lowest rtt 5182 * we saw in the measurement window). 5183 * 5184 * Another option here would be to 5185 * instead skip the measurement. 5186 */ 5187 rack_log_pacing_delay_calc(rack, bytes, reqbytes, 5188 bytes_ps, rack->r_ctl.last_max_bw, 0, 5189 11, __LINE__, NULL, quality); 5190 bytes_ps = rack->r_ctl.last_max_bw; 5191 } 5192 /* We store gp for b/w in bytes per second */ 5193 if (rack->rc_gp_filled == 0) { 5194 /* Initial measurement */ 5195 if (bytes_ps) { 5196 rack->r_ctl.gp_bw = bytes_ps; 5197 rack->rc_gp_filled = 1; 5198 rack->r_ctl.num_measurements = 1; 5199 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 5200 } else { 5201 rack_log_pacing_delay_calc(rack, bytes_ps, reqbytes, 5202 rack->r_ctl.rc_app_limited_cnt, 5203 0, 0, 10, __LINE__, NULL, quality); 5204 } 5205 if (tcp_in_hpts(rack->rc_tp) && 5206 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 5207 /* 5208 * Ok we can't trust the pacer in this case 5209 * where we transition from un-paced to paced. 5210 * Or for that matter when the burst mitigation 5211 * was making a wild guess and got it wrong. 5212 * Stop the pacer and clear up all the aggregate 5213 * delays etc. 5214 */ 5215 tcp_hpts_remove(rack->rc_tp); 5216 rack->r_ctl.rc_hpts_flags = 0; 5217 rack->r_ctl.rc_last_output_to = 0; 5218 } 5219 did_add = 2; 5220 } else if (rack->r_ctl.num_measurements < RACK_REQ_AVG) { 5221 /* Still a small number run an average */ 5222 rack->r_ctl.gp_bw += bytes_ps; 5223 addpart = rack->r_ctl.num_measurements; 5224 rack->r_ctl.num_measurements++; 5225 if (rack->r_ctl.num_measurements >= RACK_REQ_AVG) { 5226 /* We have collected enough to move forward */ 5227 rack->r_ctl.gp_bw /= (uint64_t)rack->r_ctl.num_measurements; 5228 } 5229 rack_set_pace_segments(tp, rack, __LINE__, NULL); 5230 did_add = 3; 5231 } else { 5232 /* 5233 * We want to take 1/wma of the goodput and add in to 7/8th 5234 * of the old value weighted by the srtt. So if your measurement 5235 * period is say 2 SRTT's long you would get 1/4 as the 5236 * value, if it was like 1/2 SRTT then you would get 1/16th. 5237 * 5238 * But we must be careful not to take too much i.e. if the 5239 * srtt is say 20ms and the measurement is taken over 5240 * 400ms our weight would be 400/20 i.e. 20. On the 5241 * other hand if we get a measurement over 1ms with a 5242 * 10ms rtt we only want to take a much smaller portion. 5243 */ 5244 uint8_t meth; 5245 5246 if (rack->r_ctl.num_measurements < 0xff) { 5247 rack->r_ctl.num_measurements++; 5248 } 5249 srtt = (uint64_t)tp->t_srtt; 5250 if (srtt == 0) { 5251 /* 5252 * Strange why did t_srtt go back to zero? 5253 */ 5254 if (rack->r_ctl.rc_rack_min_rtt) 5255 srtt = rack->r_ctl.rc_rack_min_rtt; 5256 else 5257 srtt = HPTS_USEC_IN_MSEC; 5258 } 5259 /* 5260 * XXXrrs: Note for reviewers, in playing with 5261 * dynamic pacing I discovered this GP calculation 5262 * as done originally leads to some undesired results. 5263 * Basically you can get longer measurements contributing 5264 * too much to the WMA. Thus I changed it if you are doing 5265 * dynamic adjustments to only do the aportioned adjustment 5266 * if we have a very small (time wise) measurement. Longer 5267 * measurements just get there weight (defaulting to 1/8) 5268 * add to the WMA. We may want to think about changing 5269 * this to always do that for both sides i.e. dynamic 5270 * and non-dynamic... but considering lots of folks 5271 * were playing with this I did not want to change the 5272 * calculation per.se. without your thoughts.. Lawerence? 5273 * Peter?? 5274 */ 5275 if (rack->rc_gp_dyn_mul == 0) { 5276 subpart = rack->r_ctl.gp_bw * utim; 5277 subpart /= (srtt * 8); 5278 if (subpart < (rack->r_ctl.gp_bw / 2)) { 5279 /* 5280 * The b/w update takes no more 5281 * away then 1/2 our running total 5282 * so factor it in. 5283 */ 5284 addpart = bytes_ps * utim; 5285 addpart /= (srtt * 8); 5286 meth = 1; 5287 } else { 5288 /* 5289 * Don't allow a single measurement 5290 * to account for more than 1/2 of the 5291 * WMA. This could happen on a retransmission 5292 * where utim becomes huge compared to 5293 * srtt (multiple retransmissions when using 5294 * the sending rate which factors in all the 5295 * transmissions from the first one). 5296 */ 5297 subpart = rack->r_ctl.gp_bw / 2; 5298 addpart = bytes_ps / 2; 5299 meth = 2; 5300 } 5301 rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__); 5302 resid_bw = rack->r_ctl.gp_bw - subpart; 5303 rack->r_ctl.gp_bw = resid_bw + addpart; 5304 did_add = 1; 5305 } else { 5306 if ((utim / srtt) <= 1) { 5307 /* 5308 * The b/w update was over a small period 5309 * of time. The idea here is to prevent a small 5310 * measurement time period from counting 5311 * too much. So we scale it based on the 5312 * time so it attributes less than 1/rack_wma_divisor 5313 * of its measurement. 5314 */ 5315 subpart = rack->r_ctl.gp_bw * utim; 5316 subpart /= (srtt * rack_wma_divisor); 5317 addpart = bytes_ps * utim; 5318 addpart /= (srtt * rack_wma_divisor); 5319 meth = 3; 5320 } else { 5321 /* 5322 * The scaled measurement was long 5323 * enough so lets just add in the 5324 * portion of the measurement i.e. 1/rack_wma_divisor 5325 */ 5326 subpart = rack->r_ctl.gp_bw / rack_wma_divisor; 5327 addpart = bytes_ps / rack_wma_divisor; 5328 meth = 4; 5329 } 5330 if ((rack->measure_saw_probe_rtt == 0) || 5331 (bytes_ps > rack->r_ctl.gp_bw)) { 5332 /* 5333 * For probe-rtt we only add it in 5334 * if its larger, all others we just 5335 * add in. 5336 */ 5337 did_add = 1; 5338 rack_log_gp_calc(rack, addpart, subpart, srtt, bytes_ps, utim, meth, __LINE__); 5339 resid_bw = rack->r_ctl.gp_bw - subpart; 5340 rack->r_ctl.gp_bw = resid_bw + addpart; 5341 } 5342 } 5343 rack_set_pace_segments(tp, rack, __LINE__, NULL); 5344 } 5345 /* 5346 * We only watch the growth of the GP during the initial startup 5347 * or first-slowstart that ensues. If we ever needed to watch 5348 * growth of gp outside of that period all we need to do is 5349 * remove the first clause of this if (rc_initial_ss_comp). 5350 */ 5351 if ((rack->rc_initial_ss_comp == 0) && 5352 (rack->r_ctl.num_measurements >= RACK_REQ_AVG)) { 5353 uint64_t gp_est; 5354 5355 gp_est = bytes_ps; 5356 if (tcp_bblogging_on(rack->rc_tp)) { 5357 union tcp_log_stackspecific log; 5358 struct timeval tv; 5359 5360 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5361 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5362 log.u_bbr.flex1 = rack->r_ctl.current_round; 5363 log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise; 5364 log.u_bbr.delRate = gp_est; 5365 log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest; 5366 log.u_bbr.flex8 = 41; 5367 (void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 5368 0, &log, false, NULL, __func__, __LINE__,&tv); 5369 } 5370 if ((rack->r_ctl.num_measurements == RACK_REQ_AVG) || 5371 (rack->r_ctl.last_gpest == 0)) { 5372 /* 5373 * The round we get our measurement averaging going 5374 * is the base round so it always is the source point 5375 * for when we had our first increment. From there on 5376 * we only record the round that had a rise. 5377 */ 5378 rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round; 5379 rack->r_ctl.last_gpest = rack->r_ctl.gp_bw; 5380 } else if (gp_est >= rack->r_ctl.last_gpest) { 5381 /* 5382 * Test to see if its gone up enough 5383 * to set the round count up to now. Note 5384 * that on the seeding of the 4th measurement we 5385 */ 5386 gp_est *= 1000; 5387 gp_est /= rack->r_ctl.last_gpest; 5388 if ((uint32_t)gp_est > rack->r_ctl.gp_gain_req) { 5389 /* 5390 * We went up enough to record the round. 5391 */ 5392 if (tcp_bblogging_on(rack->rc_tp)) { 5393 union tcp_log_stackspecific log; 5394 struct timeval tv; 5395 5396 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5397 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5398 log.u_bbr.flex1 = rack->r_ctl.current_round; 5399 log.u_bbr.flex2 = (uint32_t)gp_est; 5400 log.u_bbr.flex3 = rack->r_ctl.gp_gain_req; 5401 log.u_bbr.delRate = gp_est; 5402 log.u_bbr.cur_del_rate = rack->r_ctl.last_gpest; 5403 log.u_bbr.flex8 = 42; 5404 (void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 5405 0, &log, false, NULL, __func__, __LINE__,&tv); 5406 } 5407 rack->r_ctl.last_rnd_of_gp_rise = rack->r_ctl.current_round; 5408 if (rack->r_ctl.use_gp_not_last == 1) 5409 rack->r_ctl.last_gpest = rack->r_ctl.gp_bw; 5410 else 5411 rack->r_ctl.last_gpest = bytes_ps; 5412 } 5413 } 5414 } 5415 if ((rack->gp_ready == 0) && 5416 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) { 5417 /* We have enough measurements now */ 5418 rack->gp_ready = 1; 5419 if (rack->dgp_on || 5420 rack->rack_hibeta) 5421 rack_set_cc_pacing(rack); 5422 if (rack->defer_options) 5423 rack_apply_deferred_options(rack); 5424 } 5425 rack_log_pacing_delay_calc(rack, subpart, addpart, bytes_ps, stim, 5426 rack_get_bw(rack), 22, did_add, NULL, quality); 5427 /* We do not update any multipliers if we are in or have seen a probe-rtt */ 5428 5429 if ((rack->measure_saw_probe_rtt == 0) && 5430 rack->rc_gp_rtt_set) { 5431 if (rack->rc_skip_timely == 0) { 5432 rack_update_multiplier(rack, timely_says, bytes_ps, 5433 rack->r_ctl.rc_gp_srtt, 5434 rack->r_ctl.rc_rtt_diff); 5435 } 5436 } 5437 rack_log_pacing_delay_calc(rack, bytes, tim, bytes_ps, stim, 5438 rack_get_bw(rack), 3, line, NULL, quality); 5439 rack_log_pacing_delay_calc(rack, 5440 bytes, /* flex2 */ 5441 tim, /* flex1 */ 5442 bytes_ps, /* bw_inuse */ 5443 rack->r_ctl.gp_bw, /* delRate */ 5444 rack_get_lt_bw(rack), /* rttProp */ 5445 20, line, NULL, 0); 5446 /* reset the gp srtt and setup the new prev */ 5447 rack->r_ctl.rc_prev_gp_srtt = rack->r_ctl.rc_gp_srtt; 5448 /* Record the lost count for the next measurement */ 5449 rack->r_ctl.rc_loss_at_start = rack->r_ctl.rc_loss_count; 5450 skip_measurement: 5451 /* 5452 * We restart our diffs based on the gpsrtt in the 5453 * measurement window. 5454 */ 5455 rack->rc_gp_rtt_set = 0; 5456 rack->rc_gp_saw_rec = 0; 5457 rack->rc_gp_saw_ca = 0; 5458 rack->rc_gp_saw_ss = 0; 5459 rack->rc_dragged_bottom = 0; 5460 if (quality == RACK_QUALITY_HIGH) { 5461 /* 5462 * Gput in the stats world is in kbps where bytes_ps is 5463 * bytes per second so we do ((x * 8)/ 1000). 5464 */ 5465 gput = (int32_t)((bytes_ps << 3) / (uint64_t)1000); 5466 #ifdef STATS 5467 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 5468 gput); 5469 /* 5470 * XXXLAS: This is a temporary hack, and should be 5471 * chained off VOI_TCP_GPUT when stats(9) grows an 5472 * API to deal with chained VOIs. 5473 */ 5474 if (tp->t_stats_gput_prev > 0) 5475 stats_voi_update_abs_s32(tp->t_stats, 5476 VOI_TCP_GPUT_ND, 5477 ((gput - tp->t_stats_gput_prev) * 100) / 5478 tp->t_stats_gput_prev); 5479 #endif 5480 tp->t_stats_gput_prev = gput; 5481 } 5482 tp->t_flags &= ~TF_GPUTINPROG; 5483 /* 5484 * Now are we app limited now and there is space from where we 5485 * were to where we want to go? 5486 * 5487 * We don't do the other case i.e. non-applimited here since 5488 * the next send will trigger us picking up the missing data. 5489 */ 5490 if (rack->r_ctl.rc_first_appl && 5491 TCPS_HAVEESTABLISHED(tp->t_state) && 5492 rack->r_ctl.rc_app_limited_cnt && 5493 (SEQ_GT(rack->r_ctl.rc_first_appl->r_start, th_ack)) && 5494 ((rack->r_ctl.rc_first_appl->r_end - th_ack) > 5495 max(rc_init_window(rack), (MIN_GP_WIN * segsiz)))) { 5496 /* 5497 * Yep there is enough outstanding to make a measurement here. 5498 */ 5499 struct rack_sendmap *rsm; 5500 5501 rack->r_ctl.rc_gp_lowrtt = 0xffffffff; 5502 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 5503 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 5504 rack->app_limited_needs_set = 0; 5505 tp->gput_seq = th_ack; 5506 if (rack->in_probe_rtt) 5507 rack->measure_saw_probe_rtt = 1; 5508 else if ((rack->measure_saw_probe_rtt) && 5509 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 5510 rack->measure_saw_probe_rtt = 0; 5511 if ((rack->r_ctl.rc_first_appl->r_end - th_ack) >= rack_get_measure_window(tp, rack)) { 5512 /* There is a full window to gain info from */ 5513 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 5514 } else { 5515 /* We can only measure up to the applimited point */ 5516 tp->gput_ack = tp->gput_seq + (rack->r_ctl.rc_first_appl->r_end - th_ack); 5517 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) { 5518 /* 5519 * We don't have enough to make a measurement. 5520 */ 5521 tp->t_flags &= ~TF_GPUTINPROG; 5522 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq, 5523 0, 0, 0, 6, __LINE__, NULL, quality); 5524 return; 5525 } 5526 } 5527 if (tp->t_state >= TCPS_FIN_WAIT_1) { 5528 /* 5529 * We will get no more data into the SB 5530 * this means we need to have the data available 5531 * before we start a measurement. 5532 */ 5533 if (sbavail(&tptosocket(tp)->so_snd) < (tp->gput_ack - tp->gput_seq)) { 5534 /* Nope not enough data. */ 5535 return; 5536 } 5537 } 5538 tp->t_flags |= TF_GPUTINPROG; 5539 /* 5540 * Now we need to find the timestamp of the send at tp->gput_seq 5541 * for the send based measurement. 5542 */ 5543 rack->r_ctl.rc_gp_cumack_ts = 0; 5544 rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq); 5545 if (rsm) { 5546 /* Ok send-based limit is set */ 5547 if (SEQ_LT(rsm->r_start, tp->gput_seq)) { 5548 /* 5549 * Move back to include the earlier part 5550 * so our ack time lines up right (this may 5551 * make an overlapping measurement but thats 5552 * ok). 5553 */ 5554 tp->gput_seq = rsm->r_start; 5555 } 5556 if (rsm->r_flags & RACK_ACKED) { 5557 struct rack_sendmap *nrsm; 5558 5559 tp->gput_ts = (uint32_t)rsm->r_ack_arrival; 5560 tp->gput_seq = rsm->r_end; 5561 nrsm = tqhash_next(rack->r_ctl.tqh, rsm); 5562 if (nrsm) 5563 rsm = nrsm; 5564 else { 5565 rack->app_limited_needs_set = 1; 5566 } 5567 } else 5568 rack->app_limited_needs_set = 1; 5569 /* We always go from the first send */ 5570 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0]; 5571 } else { 5572 /* 5573 * If we don't find the rsm due to some 5574 * send-limit set the current time, which 5575 * basically disables the send-limit. 5576 */ 5577 struct timeval tv; 5578 5579 microuptime(&tv); 5580 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 5581 } 5582 rack_tend_gp_marks(tp, rack); 5583 rack_log_pacing_delay_calc(rack, 5584 tp->gput_seq, 5585 tp->gput_ack, 5586 (uint64_t)rsm, 5587 tp->gput_ts, 5588 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts), 5589 9, 5590 __LINE__, rsm, quality); 5591 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL); 5592 } else { 5593 /* 5594 * To make sure proper timestamp merging occurs, we need to clear 5595 * all GP marks if we don't start a measurement. 5596 */ 5597 rack_clear_gp_marks(tp, rack); 5598 } 5599 } 5600 5601 /* 5602 * CC wrapper hook functions 5603 */ 5604 static void 5605 rack_ack_received(struct tcpcb *tp, struct tcp_rack *rack, uint32_t th_ack, uint16_t nsegs, 5606 uint16_t type, int32_t post_recovery) 5607 { 5608 uint32_t prior_cwnd, acked; 5609 struct tcp_log_buffer *lgb = NULL; 5610 uint8_t labc_to_use, quality; 5611 5612 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5613 tp->t_ccv.nsegs = nsegs; 5614 acked = tp->t_ccv.bytes_this_ack = (th_ack - tp->snd_una); 5615 if ((post_recovery) && (rack->r_ctl.rc_early_recovery_segs)) { 5616 uint32_t max; 5617 5618 max = rack->r_ctl.rc_early_recovery_segs * ctf_fixed_maxseg(tp); 5619 if (tp->t_ccv.bytes_this_ack > max) { 5620 tp->t_ccv.bytes_this_ack = max; 5621 } 5622 } 5623 #ifdef STATS 5624 stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 5625 ((int32_t)rack->r_ctl.cwnd_to_use) - tp->snd_wnd); 5626 #endif 5627 if ((th_ack == tp->snd_max) && rack->lt_bw_up) { 5628 /* 5629 * We will ack all the data, time to end any 5630 * lt_bw_up we have running until something 5631 * new is sent. Note we need to use the actual 5632 * ack_rcv_time which with pacing may be different. 5633 */ 5634 uint64_t tmark; 5635 5636 rack->r_ctl.lt_bw_bytes += (tp->snd_max - rack->r_ctl.lt_seq); 5637 rack->r_ctl.lt_seq = tp->snd_max; 5638 tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time); 5639 if (tmark >= rack->r_ctl.lt_timemark) { 5640 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark); 5641 } 5642 rack->r_ctl.lt_timemark = tmark; 5643 rack->lt_bw_up = 0; 5644 } 5645 quality = RACK_QUALITY_NONE; 5646 if ((tp->t_flags & TF_GPUTINPROG) && 5647 rack_enough_for_measurement(tp, rack, th_ack, &quality)) { 5648 /* Measure the Goodput */ 5649 rack_do_goodput_measurement(tp, rack, th_ack, __LINE__, quality); 5650 } 5651 /* Which way our we limited, if not cwnd limited no advance in CA */ 5652 if (tp->snd_cwnd <= tp->snd_wnd) 5653 tp->t_ccv.flags |= CCF_CWND_LIMITED; 5654 else 5655 tp->t_ccv.flags &= ~CCF_CWND_LIMITED; 5656 if (tp->snd_cwnd > tp->snd_ssthresh) { 5657 tp->t_bytes_acked += min(tp->t_ccv.bytes_this_ack, 5658 nsegs * V_tcp_abc_l_var * ctf_fixed_maxseg(tp)); 5659 /* For the setting of a window past use the actual scwnd we are using */ 5660 if (tp->t_bytes_acked >= rack->r_ctl.cwnd_to_use) { 5661 tp->t_bytes_acked -= rack->r_ctl.cwnd_to_use; 5662 tp->t_ccv.flags |= CCF_ABC_SENTAWND; 5663 } 5664 } else { 5665 tp->t_ccv.flags &= ~CCF_ABC_SENTAWND; 5666 tp->t_bytes_acked = 0; 5667 } 5668 prior_cwnd = tp->snd_cwnd; 5669 if ((post_recovery == 0) || (rack_max_abc_post_recovery == 0) || rack->r_use_labc_for_rec || 5670 (rack_client_low_buf && rack->client_bufferlvl && 5671 (rack->client_bufferlvl < rack_client_low_buf))) 5672 labc_to_use = rack->rc_labc; 5673 else 5674 labc_to_use = rack_max_abc_post_recovery; 5675 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 5676 union tcp_log_stackspecific log; 5677 struct timeval tv; 5678 5679 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5680 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5681 log.u_bbr.flex1 = th_ack; 5682 log.u_bbr.flex2 = tp->t_ccv.flags; 5683 log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack; 5684 log.u_bbr.flex4 = tp->t_ccv.nsegs; 5685 log.u_bbr.flex5 = labc_to_use; 5686 log.u_bbr.flex6 = prior_cwnd; 5687 log.u_bbr.flex7 = V_tcp_do_newsack; 5688 log.u_bbr.flex8 = 1; 5689 lgb = tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 5690 0, &log, false, NULL, __func__, __LINE__,&tv); 5691 } 5692 if (CC_ALGO(tp)->ack_received != NULL) { 5693 /* XXXLAS: Find a way to live without this */ 5694 tp->t_ccv.curack = th_ack; 5695 tp->t_ccv.labc = labc_to_use; 5696 tp->t_ccv.flags |= CCF_USE_LOCAL_ABC; 5697 CC_ALGO(tp)->ack_received(&tp->t_ccv, type); 5698 } 5699 if (lgb) { 5700 lgb->tlb_stackinfo.u_bbr.flex6 = tp->snd_cwnd; 5701 } 5702 if (rack->r_must_retran) { 5703 if (SEQ_GEQ(th_ack, rack->r_ctl.rc_snd_max_at_rto)) { 5704 /* 5705 * We now are beyond the rxt point so lets disable 5706 * the flag. 5707 */ 5708 rack->r_ctl.rc_out_at_rto = 0; 5709 rack->r_must_retran = 0; 5710 } else if ((prior_cwnd + ctf_fixed_maxseg(tp)) <= tp->snd_cwnd) { 5711 /* 5712 * Only decrement the rc_out_at_rto if the cwnd advances 5713 * at least a whole segment. Otherwise next time the peer 5714 * acks, we won't be able to send this generaly happens 5715 * when we are in Congestion Avoidance. 5716 */ 5717 if (acked <= rack->r_ctl.rc_out_at_rto){ 5718 rack->r_ctl.rc_out_at_rto -= acked; 5719 } else { 5720 rack->r_ctl.rc_out_at_rto = 0; 5721 } 5722 } 5723 } 5724 #ifdef STATS 5725 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, rack->r_ctl.cwnd_to_use); 5726 #endif 5727 if (rack->r_ctl.rc_rack_largest_cwnd < rack->r_ctl.cwnd_to_use) { 5728 rack->r_ctl.rc_rack_largest_cwnd = rack->r_ctl.cwnd_to_use; 5729 } 5730 if ((rack->rc_initial_ss_comp == 0) && 5731 (tp->snd_cwnd >= tp->snd_ssthresh)) { 5732 /* 5733 * The cwnd has grown beyond ssthresh we have 5734 * entered ca and completed our first Slowstart. 5735 */ 5736 rack->rc_initial_ss_comp = 1; 5737 } 5738 } 5739 5740 static void 5741 tcp_rack_partialack(struct tcpcb *tp) 5742 { 5743 struct tcp_rack *rack; 5744 5745 rack = (struct tcp_rack *)tp->t_fb_ptr; 5746 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5747 /* 5748 * If we are doing PRR and have enough 5749 * room to send <or> we are pacing and prr 5750 * is disabled we will want to see if we 5751 * can send data (by setting r_wanted_output to 5752 * true). 5753 */ 5754 if ((rack->r_ctl.rc_prr_sndcnt > 0) || 5755 rack->rack_no_prr) 5756 rack->r_wanted_output = 1; 5757 } 5758 5759 static inline uint64_t 5760 rack_get_rxt_per(uint64_t snds, uint64_t rxts) 5761 { 5762 uint64_t rxt_per; 5763 5764 if (snds > 0) { 5765 rxt_per = rxts * 1000; 5766 rxt_per /= snds; 5767 } else { 5768 /* This is an unlikely path */ 5769 if (rxts) { 5770 /* Its the max it was all re-transmits */ 5771 rxt_per = 0xffffffffffffffff; 5772 } else { 5773 rxt_per = 0; 5774 } 5775 } 5776 return (rxt_per); 5777 } 5778 5779 static void 5780 policer_detection_log(struct tcp_rack *rack, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint32_t flex4, uint8_t flex8) 5781 { 5782 if (tcp_bblogging_on(rack->rc_tp)) { 5783 union tcp_log_stackspecific log; 5784 struct timeval tv; 5785 5786 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5787 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5788 log.u_bbr.flex1 = flex1; 5789 log.u_bbr.flex2 = flex2; 5790 log.u_bbr.flex3 = flex3; 5791 log.u_bbr.flex4 = flex4; 5792 log.u_bbr.flex5 = rack->r_ctl.current_policer_bucket; 5793 log.u_bbr.flex6 = rack->r_ctl.policer_bucket_size; 5794 log.u_bbr.flex7 = 0; 5795 log.u_bbr.flex8 = flex8; 5796 log.u_bbr.bw_inuse = rack->r_ctl.policer_bw; 5797 log.u_bbr.applimited = rack->r_ctl.current_round; 5798 log.u_bbr.epoch = rack->r_ctl.policer_max_seg; 5799 log.u_bbr.delivered = (uint32_t)rack->r_ctl.bytes_acked_in_recovery; 5800 log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes; 5801 log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes; 5802 log.u_bbr.rttProp = rack->r_ctl.gp_bw; 5803 log.u_bbr.bbr_state = rack->rc_policer_detected; 5804 log.u_bbr.bbr_substate = 0; 5805 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 5806 log.u_bbr.use_lt_bw = rack->policer_detect_on; 5807 log.u_bbr.lt_epoch = 0; 5808 log.u_bbr.pkts_out = 0; 5809 tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_POLICER_DET, 0, 5810 0, &log, false, NULL, NULL, 0, &tv); 5811 } 5812 5813 } 5814 5815 static void 5816 policer_detection(struct tcpcb *tp, struct tcp_rack *rack, int post_recovery) 5817 { 5818 /* 5819 * Rack excess rxt accounting is turned on. If we 5820 * are above a threshold of rxt's in at least N 5821 * rounds, then back off the cwnd and ssthresh 5822 * to fit into the long-term b/w. 5823 */ 5824 5825 uint32_t pkts, mid, med, alt_med, avg, segsiz, tot_retran_pkt_count = 0; 5826 uint32_t cnt_of_mape_rxt = 0; 5827 uint64_t snds, rxts, rxt_per, tim, del, del_bw; 5828 int i; 5829 struct timeval tv; 5830 5831 5832 /* 5833 * First is there enough packets delivered during recovery to make 5834 * a determiniation of b/w? 5835 */ 5836 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 5837 if ((rack->rc_policer_detected == 0) && 5838 (rack->r_ctl.policer_del_mss > 0) && 5839 ((uint32_t)rack->r_ctl.policer_del_mss > ((rack->r_ctl.bytes_acked_in_recovery + segsiz - 1)/segsiz))) { 5840 /* 5841 * Not enough data sent in recovery for initial detection. Once 5842 * we have deteced a policer we allow less than the threshold (polcer_del_mss) 5843 * amount of data in a recovery to let us fall through and double check 5844 * our policer settings and possibly expand or collapse the bucket size and 5845 * the polcier b/w. 5846 * 5847 * Once you are declared to be policed. this block of code cannot be 5848 * reached, instead blocks further down will re-check the policer detection 5849 * triggers and possibly reset the measurements if somehow we have let the 5850 * policer bucket size grow too large. 5851 */ 5852 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 5853 policer_detection_log(rack, rack->r_ctl.policer_del_mss, 5854 ((rack->r_ctl.bytes_acked_in_recovery + segsiz - 1)/segsiz), 5855 rack->r_ctl.bytes_acked_in_recovery, segsiz, 18); 5856 } 5857 return; 5858 } 5859 tcp_get_usecs(&tv); 5860 tim = tcp_tv_to_lusectick(&tv) - rack->r_ctl.time_entered_recovery; 5861 del = rack->r_ctl.bytes_acked_in_recovery; 5862 if (tim > 0) 5863 del_bw = (del * (uint64_t)1000000) / tim; 5864 else 5865 del_bw = 0; 5866 /* B/W compensation? */ 5867 5868 if (rack->r_ctl.pol_bw_comp && ((rack->r_ctl.policer_bw > 0) || 5869 (del_bw > 0))) { 5870 /* 5871 * Sanity check now that the data is in. How long does it 5872 * take for us to pace out two of our policer_max_seg's? 5873 * 5874 * If it is longer than the RTT then we are set 5875 * too slow, maybe because of not enough data 5876 * sent during recovery. 5877 */ 5878 uint64_t lentime, res, srtt, max_delbw, alt_bw; 5879 5880 srtt = (uint64_t)rack_grab_rtt(tp, rack); 5881 if ((tp->t_srtt > 0) && (srtt > tp->t_srtt)) 5882 srtt = tp->t_srtt; 5883 lentime = rack->r_ctl.policer_max_seg * (uint64_t)HPTS_USEC_IN_SEC * 2; 5884 if (del_bw > rack->r_ctl.policer_bw) { 5885 max_delbw = del_bw; 5886 } else { 5887 max_delbw = rack->r_ctl.policer_bw; 5888 } 5889 res = lentime / max_delbw; 5890 if ((srtt > 0) && (res > srtt)) { 5891 /* 5892 * At this rate we can not get two policer_maxsegs 5893 * out before the ack arrives back. 5894 * 5895 * Lets at least get it raised up so that 5896 * we can be a bit faster than that if possible. 5897 */ 5898 lentime = (rack->r_ctl.policer_max_seg * 2); 5899 tim = srtt; 5900 alt_bw = (lentime * (uint64_t)HPTS_USEC_IN_SEC) / tim; 5901 if (alt_bw > max_delbw) { 5902 uint64_t cap_alt_bw; 5903 5904 cap_alt_bw = (max_delbw + (max_delbw * rack->r_ctl.pol_bw_comp)); 5905 if ((rack_pol_min_bw > 0) && (cap_alt_bw < rack_pol_min_bw)) { 5906 /* We place a min on the cap which defaults to 1Mbps */ 5907 cap_alt_bw = rack_pol_min_bw; 5908 } 5909 if (alt_bw <= cap_alt_bw) { 5910 /* It should be */ 5911 del_bw = alt_bw; 5912 policer_detection_log(rack, 5913 (uint32_t)tim, 5914 rack->r_ctl.policer_max_seg, 5915 0, 5916 0, 5917 16); 5918 } else { 5919 /* 5920 * This is an odd case where likely the RTT is very very 5921 * low. And yet it is still being policed. We don't want 5922 * to get more than (rack_policing_do_bw_comp+1) x del-rate 5923 * where del-rate is what we got in recovery for either the 5924 * first Policer Detection(PD) or this PD we are on now. 5925 */ 5926 del_bw = cap_alt_bw; 5927 policer_detection_log(rack, 5928 (uint32_t)tim, 5929 rack->r_ctl.policer_max_seg, 5930 (uint32_t)max_delbw, 5931 (rack->r_ctl.pol_bw_comp + 1), 5932 16); 5933 } 5934 } 5935 } 5936 } 5937 snds = tp->t_sndbytes - rack->r_ctl.last_policer_sndbytes; 5938 rxts = tp->t_snd_rxt_bytes - rack->r_ctl.last_policer_snd_rxt_bytes; 5939 rxt_per = rack_get_rxt_per(snds, rxts); 5940 /* Figure up the average and median */ 5941 for(i = 0; i < RETRAN_CNT_SIZE; i++) { 5942 if (rack->r_ctl.rc_cnt_of_retran[i] > 0) { 5943 tot_retran_pkt_count += (i + 1) * rack->r_ctl.rc_cnt_of_retran[i]; 5944 cnt_of_mape_rxt += rack->r_ctl.rc_cnt_of_retran[i]; 5945 } 5946 } 5947 if (cnt_of_mape_rxt) 5948 avg = (tot_retran_pkt_count * 10)/cnt_of_mape_rxt; 5949 else 5950 avg = 0; 5951 alt_med = med = 0; 5952 mid = tot_retran_pkt_count/2; 5953 for(i = 0; i < RETRAN_CNT_SIZE; i++) { 5954 pkts = (i + 1) * rack->r_ctl.rc_cnt_of_retran[i]; 5955 if (mid > pkts) { 5956 mid -= pkts; 5957 continue; 5958 } 5959 med = (i + 1); 5960 break; 5961 } 5962 mid = cnt_of_mape_rxt / 2; 5963 for(i = 0; i < RETRAN_CNT_SIZE; i++) { 5964 if (mid > rack->r_ctl.rc_cnt_of_retran[i]) { 5965 mid -= rack->r_ctl.rc_cnt_of_retran[i]; 5966 continue; 5967 } 5968 alt_med = (i + 1); 5969 break; 5970 } 5971 if (rack->r_ctl.policer_alt_median) { 5972 /* Swap the medians */ 5973 uint32_t swap; 5974 5975 swap = med; 5976 med = alt_med; 5977 alt_med = swap; 5978 } 5979 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 5980 union tcp_log_stackspecific log; 5981 struct timeval tv; 5982 5983 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 5984 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 5985 log.u_bbr.flex1 = avg; 5986 log.u_bbr.flex2 = med; 5987 log.u_bbr.flex3 = (uint32_t)rxt_per; 5988 log.u_bbr.flex4 = rack->r_ctl.policer_avg_threshold; 5989 log.u_bbr.flex5 = rack->r_ctl.policer_med_threshold; 5990 log.u_bbr.flex6 = rack->r_ctl.policer_rxt_threshold; 5991 log.u_bbr.flex7 = rack->r_ctl.policer_alt_median; 5992 log.u_bbr.flex8 = 1; 5993 log.u_bbr.delivered = rack->r_ctl.policer_bucket_size; 5994 log.u_bbr.applimited = rack->r_ctl.current_round; 5995 log.u_bbr.epoch = rack->r_ctl.policer_max_seg; 5996 log.u_bbr.bw_inuse = del_bw; 5997 log.u_bbr.cur_del_rate = rxts; 5998 log.u_bbr.delRate = snds; 5999 log.u_bbr.rttProp = rack->r_ctl.gp_bw; 6000 log.u_bbr.bbr_state = rack->rc_policer_detected; 6001 log.u_bbr.bbr_substate = 0; 6002 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 6003 log.u_bbr.use_lt_bw = rack->policer_detect_on; 6004 log.u_bbr.lt_epoch = (uint32_t)tim; 6005 log.u_bbr.pkts_out = rack->r_ctl.bytes_acked_in_recovery; 6006 tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0, 6007 0, &log, false, NULL, NULL, 0, &tv); 6008 } 6009 if (med == RETRAN_CNT_SIZE) { 6010 /* 6011 * If the median is the maximum, then what we 6012 * likely have here is a network breakage. Either that 6013 * or we are so unlucky that all of our traffic is being 6014 * dropped and having to be retransmitted the maximum times 6015 * and this just is not how a policer works. 6016 * 6017 * If it is truely a policer eventually we will come 6018 * through and it won't be the maximum. 6019 */ 6020 return; 6021 } 6022 /* Has enough rounds progressed for us to re-measure? */ 6023 if ((rxt_per >= (uint64_t)rack->r_ctl.policer_rxt_threshold) && 6024 (avg >= rack->r_ctl.policer_avg_threshold) && 6025 (med >= rack->r_ctl.policer_med_threshold)) { 6026 /* 6027 * We hit all thresholds that indicate we are 6028 * being policed. Now we may be doing this from a rack timeout 6029 * which then means the rest of recovery will hopefully go 6030 * smoother as we pace. At the end of recovery we will 6031 * fall back in here and reset the values using the 6032 * results of the entire recovery episode (we could also 6033 * hit this as we exit recovery as well which means only 6034 * one time in here). 6035 * 6036 * This is done explicitly that if we hit the thresholds 6037 * again in a second recovery we overwrite the values. We do 6038 * that because over time, as we pace the policer_bucket_size may 6039 * continue to grow. This then provides more and more times when 6040 * we are not pacing to the policer rate. This lets us compensate 6041 * for when we hit a false positive and those flows continue to 6042 * increase. However if its a real policer we will then get over its 6043 * limit, over time, again and thus end up back here hitting the 6044 * thresholds again. 6045 * 6046 * The alternative to this is to instead whenever we pace due to 6047 * policing in rack_policed_sending we could add the amount len paced to the 6048 * idle_snd_una value (which decreases the amount in last_amount_before_rec 6049 * since that is always [th_ack - idle_snd_una]). This would then prevent 6050 * the polcier_bucket_size from growing in additional recovery episodes 6051 * Which would then mean false postives would be pretty much stuck 6052 * after things got back to normal (assuming that what caused the 6053 * false positive was a small network outage). 6054 * 6055 */ 6056 tcp_trace_point(rack->rc_tp, TCP_TP_POLICER_DET); 6057 if (rack->rc_policer_detected == 0) { 6058 /* 6059 * Increment the stat that tells us we identified 6060 * a policer only once. Note that if we ever allow 6061 * the flag to be cleared (reverted) then we need 6062 * to adjust this to not do multi-counting. 6063 */ 6064 counter_u64_add(tcp_policer_detected, 1); 6065 } 6066 rack->r_ctl.last_policer_sndbytes = tp->t_sndbytes; 6067 rack->r_ctl.last_policer_snd_rxt_bytes = tp->t_snd_rxt_bytes; 6068 rack->r_ctl.policer_bw = del_bw; 6069 rack->r_ctl.policer_max_seg = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, 6070 rack->r_ctl.policer_bw, 6071 min(ctf_fixed_maxseg(rack->rc_tp), 6072 rack->r_ctl.rc_pace_min_segs), 6073 0, NULL, 6074 NULL, rack->r_ctl.pace_len_divisor); 6075 /* Now what about the policer bucket size */ 6076 rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec; 6077 if (rack->r_ctl.policer_bucket_size < rack->r_ctl.policer_max_seg) { 6078 /* We must be able to send our max-seg or else chaos ensues */ 6079 rack->r_ctl.policer_bucket_size = rack->r_ctl.policer_max_seg * 2; 6080 } 6081 if (rack->rc_policer_detected == 0) 6082 rack->r_ctl.current_policer_bucket = 0; 6083 if (tcp_bblogging_on(rack->rc_tp)) { 6084 union tcp_log_stackspecific log; 6085 struct timeval tv; 6086 6087 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 6088 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 6089 log.u_bbr.flex1 = avg; 6090 log.u_bbr.flex2 = med; 6091 log.u_bbr.flex3 = rxt_per; 6092 log.u_bbr.flex4 = rack->r_ctl.policer_avg_threshold; 6093 log.u_bbr.flex5 = rack->r_ctl.policer_med_threshold; 6094 log.u_bbr.flex6 = rack->r_ctl.policer_rxt_threshold; 6095 log.u_bbr.flex7 = rack->r_ctl.policer_alt_median; 6096 log.u_bbr.flex8 = 2; 6097 log.u_bbr.applimited = rack->r_ctl.current_round; 6098 log.u_bbr.bw_inuse = del_bw; 6099 log.u_bbr.delivered = rack->r_ctl.policer_bucket_size; 6100 log.u_bbr.cur_del_rate = rxts; 6101 log.u_bbr.delRate = snds; 6102 log.u_bbr.rttProp = rack->r_ctl.gp_bw; 6103 log.u_bbr.bbr_state = rack->rc_policer_detected; 6104 log.u_bbr.bbr_substate = 0; 6105 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 6106 log.u_bbr.use_lt_bw = rack->policer_detect_on; 6107 log.u_bbr.epoch = rack->r_ctl.policer_max_seg; 6108 log.u_bbr.lt_epoch = (uint32_t)tim; 6109 log.u_bbr.pkts_out = rack->r_ctl.bytes_acked_in_recovery; 6110 tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0, 6111 0, &log, false, NULL, NULL, 0, &tv); 6112 /* 6113 * Put out an added log, 19, for the sole purpose 6114 * of getting the txt/rxt so that we can benchmark 6115 * in read-bbrlog the ongoing rxt rate after our 6116 * policer invocation in the HYSTART announcments. 6117 */ 6118 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 6119 log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv); 6120 log.u_bbr.flex1 = alt_med; 6121 log.u_bbr.flex8 = 19; 6122 log.u_bbr.cur_del_rate = tp->t_sndbytes; 6123 log.u_bbr.delRate = tp->t_snd_rxt_bytes; 6124 tcp_log_event(tp, NULL, NULL, NULL, TCP_POLICER_DET, 0, 6125 0, &log, false, NULL, NULL, 0, &tv); 6126 } 6127 /* Turn off any fast output, thats ended */ 6128 rack->r_fast_output = 0; 6129 /* Mark the time for credits */ 6130 rack->r_ctl.last_sendtime = tcp_get_u64_usecs(NULL); 6131 if (rack->r_rr_config < 2) { 6132 /* 6133 * We need to be stricter on the RR config so 6134 * the pacing has priority. 6135 */ 6136 rack->r_rr_config = 2; 6137 } 6138 policer_detection_log(rack, 6139 rack->r_ctl.idle_snd_una, 6140 rack->r_ctl.ack_for_idle, 6141 0, 6142 (uint32_t)tim, 6143 14); 6144 rack->rc_policer_detected = 1; 6145 } else if ((rack->rc_policer_detected == 1) && 6146 (post_recovery == 1)) { 6147 /* 6148 * If we are exiting recovery and have already detected 6149 * we need to possibly update the values. 6150 * 6151 * First: Update the idle -> recovery sent value. 6152 */ 6153 uint32_t srtt; 6154 6155 if (rack->r_ctl.last_amount_before_rec > rack->r_ctl.policer_bucket_size) { 6156 rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec; 6157 } 6158 srtt = (uint64_t)rack_grab_rtt(tp, rack); 6159 if ((tp->t_srtt > 0) && (srtt > tp->t_srtt)) 6160 srtt = tp->t_srtt; 6161 if ((srtt != 0) && 6162 (tim < (uint64_t)srtt)) { 6163 /* 6164 * Not long enough. 6165 */ 6166 if (rack_verbose_logging) 6167 policer_detection_log(rack, 6168 (uint32_t)tim, 6169 0, 6170 0, 6171 0, 6172 15); 6173 return; 6174 } 6175 /* 6176 * Finally update the b/w if its grown. 6177 */ 6178 if (del_bw > rack->r_ctl.policer_bw) { 6179 rack->r_ctl.policer_bw = del_bw; 6180 rack->r_ctl.policer_max_seg = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, 6181 rack->r_ctl.policer_bw, 6182 min(ctf_fixed_maxseg(rack->rc_tp), 6183 rack->r_ctl.rc_pace_min_segs), 6184 0, NULL, 6185 NULL, rack->r_ctl.pace_len_divisor); 6186 if (rack->r_ctl.policer_bucket_size < rack->r_ctl.policer_max_seg) { 6187 /* We must be able to send our max-seg or else chaos ensues */ 6188 rack->r_ctl.policer_bucket_size = rack->r_ctl.policer_max_seg * 2; 6189 } 6190 } 6191 policer_detection_log(rack, 6192 rack->r_ctl.idle_snd_una, 6193 rack->r_ctl.ack_for_idle, 6194 0, 6195 (uint32_t)tim, 6196 3); 6197 } 6198 } 6199 6200 static void 6201 rack_exit_recovery(struct tcpcb *tp, struct tcp_rack *rack, int how) 6202 { 6203 /* now check with the policer if on */ 6204 if (rack->policer_detect_on == 1) { 6205 policer_detection(tp, rack, 1); 6206 } 6207 /* 6208 * Now exit recovery, note we must do the idle set after the policer_detection 6209 * to get the amount acked prior to recovery correct. 6210 */ 6211 rack->r_ctl.idle_snd_una = tp->snd_una; 6212 EXIT_RECOVERY(tp->t_flags); 6213 } 6214 6215 static void 6216 rack_post_recovery(struct tcpcb *tp, uint32_t th_ack) 6217 { 6218 struct tcp_rack *rack; 6219 uint32_t orig_cwnd; 6220 6221 orig_cwnd = tp->snd_cwnd; 6222 INP_WLOCK_ASSERT(tptoinpcb(tp)); 6223 rack = (struct tcp_rack *)tp->t_fb_ptr; 6224 /* only alert CC if we alerted when we entered */ 6225 if (CC_ALGO(tp)->post_recovery != NULL) { 6226 tp->t_ccv.curack = th_ack; 6227 CC_ALGO(tp)->post_recovery(&tp->t_ccv); 6228 if (tp->snd_cwnd < tp->snd_ssthresh) { 6229 /* 6230 * Rack has burst control and pacing 6231 * so lets not set this any lower than 6232 * snd_ssthresh per RFC-6582 (option 2). 6233 */ 6234 tp->snd_cwnd = tp->snd_ssthresh; 6235 } 6236 } 6237 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 6238 union tcp_log_stackspecific log; 6239 struct timeval tv; 6240 6241 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 6242 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 6243 log.u_bbr.flex1 = th_ack; 6244 log.u_bbr.flex2 = tp->t_ccv.flags; 6245 log.u_bbr.flex3 = tp->t_ccv.bytes_this_ack; 6246 log.u_bbr.flex4 = tp->t_ccv.nsegs; 6247 log.u_bbr.flex5 = V_tcp_abc_l_var; 6248 log.u_bbr.flex6 = orig_cwnd; 6249 log.u_bbr.flex7 = V_tcp_do_newsack; 6250 log.u_bbr.pkts_out = rack->r_ctl.rc_prr_sndcnt; 6251 log.u_bbr.flex8 = 2; 6252 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 6253 0, &log, false, NULL, __func__, __LINE__, &tv); 6254 } 6255 if ((rack->rack_no_prr == 0) && 6256 (rack->no_prr_addback == 0) && 6257 (rack->r_ctl.rc_prr_sndcnt > 0)) { 6258 /* 6259 * Suck the next prr cnt back into cwnd, but 6260 * only do that if we are not application limited. 6261 */ 6262 if (ctf_outstanding(tp) <= sbavail(&tptosocket(tp)->so_snd)) { 6263 /* 6264 * We are allowed to add back to the cwnd the amount we did 6265 * not get out if: 6266 * a) no_prr_addback is off. 6267 * b) we are not app limited 6268 * c) we are doing prr 6269 * <and> 6270 * d) it is bounded by rack_prr_addbackmax (if addback is 0, then none). 6271 */ 6272 tp->snd_cwnd += min((ctf_fixed_maxseg(tp) * rack_prr_addbackmax), 6273 rack->r_ctl.rc_prr_sndcnt); 6274 } 6275 rack->r_ctl.rc_prr_sndcnt = 0; 6276 rack_log_to_prr(rack, 1, 0, __LINE__); 6277 } 6278 rack_log_to_prr(rack, 14, orig_cwnd, __LINE__); 6279 tp->snd_recover = tp->snd_una; 6280 if (rack->r_ctl.dsack_persist) { 6281 rack->r_ctl.dsack_persist--; 6282 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 6283 rack->r_ctl.num_dsack = 0; 6284 } 6285 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 6286 } 6287 if (rack->rto_from_rec == 1) { 6288 rack->rto_from_rec = 0; 6289 if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh) 6290 tp->snd_ssthresh = rack->r_ctl.rto_ssthresh; 6291 } 6292 rack_exit_recovery(tp, rack, 1); 6293 } 6294 6295 static void 6296 rack_cong_signal(struct tcpcb *tp, uint32_t type, uint32_t ack, int line) 6297 { 6298 struct tcp_rack *rack; 6299 uint32_t ssthresh_enter, cwnd_enter, in_rec_at_entry, orig_cwnd; 6300 6301 INP_WLOCK_ASSERT(tptoinpcb(tp)); 6302 #ifdef STATS 6303 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 6304 #endif 6305 if (IN_RECOVERY(tp->t_flags) == 0) { 6306 in_rec_at_entry = 0; 6307 ssthresh_enter = tp->snd_ssthresh; 6308 cwnd_enter = tp->snd_cwnd; 6309 } else 6310 in_rec_at_entry = 1; 6311 rack = (struct tcp_rack *)tp->t_fb_ptr; 6312 switch (type) { 6313 case CC_NDUPACK: 6314 tp->t_flags &= ~TF_WASFRECOVERY; 6315 tp->t_flags &= ~TF_WASCRECOVERY; 6316 if (!IN_FASTRECOVERY(tp->t_flags)) { 6317 struct rack_sendmap *rsm; 6318 struct timeval tv; 6319 uint32_t segsiz; 6320 6321 /* Check if this is the end of the initial Start-up i.e. initial slow-start */ 6322 if (rack->rc_initial_ss_comp == 0) { 6323 /* Yep it is the end of the initial slowstart */ 6324 rack->rc_initial_ss_comp = 1; 6325 } 6326 microuptime(&tv); 6327 rack->r_ctl.time_entered_recovery = tcp_tv_to_lusectick(&tv); 6328 if (SEQ_GEQ(ack, tp->snd_una)) { 6329 /* 6330 * The ack is above snd_una. Lets see 6331 * if we can establish a postive distance from 6332 * our idle mark. 6333 */ 6334 rack->r_ctl.ack_for_idle = ack; 6335 if (SEQ_GT(ack, rack->r_ctl.idle_snd_una)) { 6336 rack->r_ctl.last_amount_before_rec = ack - rack->r_ctl.idle_snd_una; 6337 } else { 6338 /* No data thru yet */ 6339 rack->r_ctl.last_amount_before_rec = 0; 6340 } 6341 } else if (SEQ_GT(tp->snd_una, rack->r_ctl.idle_snd_una)) { 6342 /* 6343 * The ack is out of order and behind the snd_una. It may 6344 * have contained SACK information which we processed else 6345 * we would have rejected it. 6346 */ 6347 rack->r_ctl.ack_for_idle = tp->snd_una; 6348 rack->r_ctl.last_amount_before_rec = tp->snd_una - rack->r_ctl.idle_snd_una; 6349 } else { 6350 rack->r_ctl.ack_for_idle = ack; 6351 rack->r_ctl.last_amount_before_rec = 0; 6352 } 6353 if (rack->rc_policer_detected) { 6354 /* 6355 * If we are being policed and we have a loss, it 6356 * means our bucket is now empty. This can happen 6357 * where some other flow on the same host sends 6358 * that this connection is not aware of. 6359 */ 6360 rack->r_ctl.current_policer_bucket = 0; 6361 if (rack_verbose_logging) 6362 policer_detection_log(rack, rack->r_ctl.last_amount_before_rec, 0, 0, 0, 4); 6363 if (rack->r_ctl.last_amount_before_rec > rack->r_ctl.policer_bucket_size) { 6364 rack->r_ctl.policer_bucket_size = rack->r_ctl.last_amount_before_rec; 6365 } 6366 } 6367 memset(rack->r_ctl.rc_cnt_of_retran, 0, sizeof(rack->r_ctl.rc_cnt_of_retran)); 6368 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 6369 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 6370 /* 6371 * Go through the outstanding and re-peg 6372 * any that should have been left in the 6373 * retransmit list (on a double recovery). 6374 */ 6375 if (rsm->r_act_rxt_cnt > 0) { 6376 rack_peg_rxt(rack, rsm, segsiz); 6377 } 6378 } 6379 rack->r_ctl.bytes_acked_in_recovery = 0; 6380 rack->r_ctl.rc_prr_delivered = 0; 6381 rack->r_ctl.rc_prr_out = 0; 6382 rack->r_fast_output = 0; 6383 if (rack->rack_no_prr == 0) { 6384 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp); 6385 rack_log_to_prr(rack, 2, in_rec_at_entry, line); 6386 } 6387 rack->r_ctl.rc_prr_recovery_fs = tp->snd_max - tp->snd_una; 6388 tp->snd_recover = tp->snd_max; 6389 if (tp->t_flags2 & TF2_ECN_PERMIT) 6390 tp->t_flags2 |= TF2_ECN_SND_CWR; 6391 } 6392 break; 6393 case CC_ECN: 6394 if (!IN_CONGRECOVERY(tp->t_flags) || 6395 /* 6396 * Allow ECN reaction on ACK to CWR, if 6397 * that data segment was also CE marked. 6398 */ 6399 SEQ_GEQ(ack, tp->snd_recover)) { 6400 EXIT_CONGRECOVERY(tp->t_flags); 6401 KMOD_TCPSTAT_INC(tcps_ecn_rcwnd); 6402 rack->r_fast_output = 0; 6403 tp->snd_recover = tp->snd_max + 1; 6404 if (tp->t_flags2 & TF2_ECN_PERMIT) 6405 tp->t_flags2 |= TF2_ECN_SND_CWR; 6406 } 6407 break; 6408 case CC_RTO: 6409 tp->t_dupacks = 0; 6410 tp->t_bytes_acked = 0; 6411 rack->r_fast_output = 0; 6412 if (IN_RECOVERY(tp->t_flags)) 6413 rack_exit_recovery(tp, rack, 2); 6414 rack->r_ctl.bytes_acked_in_recovery = 0; 6415 rack->r_ctl.time_entered_recovery = 0; 6416 orig_cwnd = tp->snd_cwnd; 6417 rack_log_to_prr(rack, 16, orig_cwnd, line); 6418 if (CC_ALGO(tp)->cong_signal == NULL) { 6419 /* TSNH */ 6420 tp->snd_ssthresh = max(2, 6421 min(tp->snd_wnd, rack->r_ctl.cwnd_to_use) / 2 / 6422 ctf_fixed_maxseg(tp)) * ctf_fixed_maxseg(tp); 6423 tp->snd_cwnd = ctf_fixed_maxseg(tp); 6424 } 6425 if (tp->t_flags2 & TF2_ECN_PERMIT) 6426 tp->t_flags2 |= TF2_ECN_SND_CWR; 6427 break; 6428 case CC_RTO_ERR: 6429 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 6430 /* RTO was unnecessary, so reset everything. */ 6431 tp->snd_cwnd = tp->snd_cwnd_prev; 6432 tp->snd_ssthresh = tp->snd_ssthresh_prev; 6433 tp->snd_recover = tp->snd_recover_prev; 6434 if (tp->t_flags & TF_WASFRECOVERY) { 6435 ENTER_FASTRECOVERY(tp->t_flags); 6436 tp->t_flags &= ~TF_WASFRECOVERY; 6437 } 6438 if (tp->t_flags & TF_WASCRECOVERY) { 6439 ENTER_CONGRECOVERY(tp->t_flags); 6440 tp->t_flags &= ~TF_WASCRECOVERY; 6441 } 6442 tp->snd_nxt = tp->snd_max; 6443 tp->t_badrxtwin = 0; 6444 break; 6445 } 6446 if ((CC_ALGO(tp)->cong_signal != NULL) && 6447 (type != CC_RTO)){ 6448 tp->t_ccv.curack = ack; 6449 CC_ALGO(tp)->cong_signal(&tp->t_ccv, type); 6450 } 6451 if ((in_rec_at_entry == 0) && IN_RECOVERY(tp->t_flags)) { 6452 rack_log_to_prr(rack, 15, cwnd_enter, line); 6453 rack->r_ctl.dsack_byte_cnt = 0; 6454 rack->r_ctl.retran_during_recovery = 0; 6455 rack->r_ctl.rc_cwnd_at_erec = cwnd_enter; 6456 rack->r_ctl.rc_ssthresh_at_erec = ssthresh_enter; 6457 rack->r_ent_rec_ns = 1; 6458 } 6459 } 6460 6461 static inline void 6462 rack_cc_after_idle(struct tcp_rack *rack, struct tcpcb *tp) 6463 { 6464 uint32_t i_cwnd; 6465 6466 INP_WLOCK_ASSERT(tptoinpcb(tp)); 6467 6468 if (CC_ALGO(tp)->after_idle != NULL) 6469 CC_ALGO(tp)->after_idle(&tp->t_ccv); 6470 6471 if (tp->snd_cwnd == 1) 6472 i_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 6473 else 6474 i_cwnd = rc_init_window(rack); 6475 6476 /* 6477 * Being idle is no different than the initial window. If the cc 6478 * clamps it down below the initial window raise it to the initial 6479 * window. 6480 */ 6481 if (tp->snd_cwnd < i_cwnd) { 6482 tp->snd_cwnd = i_cwnd; 6483 } 6484 } 6485 6486 /* 6487 * Indicate whether this ack should be delayed. We can delay the ack if 6488 * following conditions are met: 6489 * - There is no delayed ack timer in progress. 6490 * - Our last ack wasn't a 0-sized window. We never want to delay 6491 * the ack that opens up a 0-sized window. 6492 * - LRO wasn't used for this segment. We make sure by checking that the 6493 * segment size is not larger than the MSS. 6494 * - Delayed acks are enabled or this is a half-synchronized T/TCP 6495 * connection. 6496 */ 6497 #define DELAY_ACK(tp, tlen) \ 6498 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 6499 ((tp->t_flags & TF_DELACK) == 0) && \ 6500 (tlen <= tp->t_maxseg) && \ 6501 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 6502 6503 static struct rack_sendmap * 6504 rack_find_lowest_rsm(struct tcp_rack *rack) 6505 { 6506 struct rack_sendmap *rsm; 6507 6508 /* 6509 * Walk the time-order transmitted list looking for an rsm that is 6510 * not acked. This will be the one that was sent the longest time 6511 * ago that is still outstanding. 6512 */ 6513 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 6514 if (rsm->r_flags & RACK_ACKED) { 6515 continue; 6516 } 6517 goto finish; 6518 } 6519 finish: 6520 return (rsm); 6521 } 6522 6523 static struct rack_sendmap * 6524 rack_find_high_nonack(struct tcp_rack *rack, struct rack_sendmap *rsm) 6525 { 6526 struct rack_sendmap *prsm; 6527 6528 /* 6529 * Walk the sequence order list backward until we hit and arrive at 6530 * the highest seq not acked. In theory when this is called it 6531 * should be the last segment (which it was not). 6532 */ 6533 prsm = rsm; 6534 6535 TQHASH_FOREACH_REVERSE_FROM(prsm, rack->r_ctl.tqh) { 6536 if (prsm->r_flags & (RACK_ACKED | RACK_HAS_FIN)) { 6537 continue; 6538 } 6539 return (prsm); 6540 } 6541 return (NULL); 6542 } 6543 6544 static uint32_t 6545 rack_calc_thresh_rack(struct tcp_rack *rack, uint32_t srtt, uint32_t cts, int line, int log_allowed) 6546 { 6547 int32_t lro; 6548 uint32_t thresh; 6549 6550 /* 6551 * lro is the flag we use to determine if we have seen reordering. 6552 * If it gets set we have seen reordering. The reorder logic either 6553 * works in one of two ways: 6554 * 6555 * If reorder-fade is configured, then we track the last time we saw 6556 * re-ordering occur. If we reach the point where enough time as 6557 * passed we no longer consider reordering has occuring. 6558 * 6559 * Or if reorder-face is 0, then once we see reordering we consider 6560 * the connection to alway be subject to reordering and just set lro 6561 * to 1. 6562 * 6563 * In the end if lro is non-zero we add the extra time for 6564 * reordering in. 6565 */ 6566 if (srtt == 0) 6567 srtt = 1; 6568 if (rack->r_ctl.rc_reorder_ts) { 6569 if (rack->r_ctl.rc_reorder_fade) { 6570 if (SEQ_GEQ(cts, rack->r_ctl.rc_reorder_ts)) { 6571 lro = cts - rack->r_ctl.rc_reorder_ts; 6572 if (lro == 0) { 6573 /* 6574 * No time as passed since the last 6575 * reorder, mark it as reordering. 6576 */ 6577 lro = 1; 6578 } 6579 } else { 6580 /* Negative time? */ 6581 lro = 0; 6582 } 6583 if (lro > rack->r_ctl.rc_reorder_fade) { 6584 /* Turn off reordering seen too */ 6585 rack->r_ctl.rc_reorder_ts = 0; 6586 lro = 0; 6587 } 6588 } else { 6589 /* Reodering does not fade */ 6590 lro = 1; 6591 } 6592 } else { 6593 lro = 0; 6594 } 6595 if (rack->rc_rack_tmr_std_based == 0) { 6596 thresh = srtt + rack->r_ctl.rc_pkt_delay; 6597 } else { 6598 /* Standards based pkt-delay is 1/4 srtt */ 6599 thresh = srtt + (srtt >> 2); 6600 } 6601 if (lro && (rack->rc_rack_tmr_std_based == 0)) { 6602 /* It must be set, if not you get 1/4 rtt */ 6603 if (rack->r_ctl.rc_reorder_shift) 6604 thresh += (srtt >> rack->r_ctl.rc_reorder_shift); 6605 else 6606 thresh += (srtt >> 2); 6607 } 6608 if (rack->rc_rack_use_dsack && 6609 lro && 6610 (rack->r_ctl.num_dsack > 0)) { 6611 /* 6612 * We only increase the reordering window if we 6613 * have seen reordering <and> we have a DSACK count. 6614 */ 6615 thresh += rack->r_ctl.num_dsack * (srtt >> 2); 6616 if (log_allowed) 6617 rack_log_dsack_event(rack, 4, line, srtt, thresh); 6618 } 6619 /* SRTT * 2 is the ceiling */ 6620 if (thresh > (srtt * 2)) { 6621 thresh = srtt * 2; 6622 } 6623 /* And we don't want it above the RTO max either */ 6624 if (thresh > rack_rto_max) { 6625 thresh = rack_rto_max; 6626 } 6627 if (log_allowed) 6628 rack_log_dsack_event(rack, 6, line, srtt, thresh); 6629 return (thresh); 6630 } 6631 6632 static uint32_t 6633 rack_calc_thresh_tlp(struct tcpcb *tp, struct tcp_rack *rack, 6634 struct rack_sendmap *rsm, uint32_t srtt) 6635 { 6636 struct rack_sendmap *prsm; 6637 uint32_t thresh, len; 6638 int segsiz; 6639 6640 if (srtt == 0) 6641 srtt = 1; 6642 if (rack->r_ctl.rc_tlp_threshold) 6643 thresh = srtt + (srtt / rack->r_ctl.rc_tlp_threshold); 6644 else 6645 thresh = (srtt * 2); 6646 6647 /* Get the previous sent packet, if any */ 6648 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 6649 len = rsm->r_end - rsm->r_start; 6650 if (rack->rack_tlp_threshold_use == TLP_USE_ID) { 6651 /* Exactly like the ID */ 6652 if (((tp->snd_max - tp->snd_una) - rack->r_ctl.rc_sacked + rack->r_ctl.rc_holes_rxt) <= segsiz) { 6653 uint32_t alt_thresh; 6654 /* 6655 * Compensate for delayed-ack with the d-ack time. 6656 */ 6657 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 6658 if (alt_thresh > thresh) 6659 thresh = alt_thresh; 6660 } 6661 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_ONE) { 6662 /* 2.1 behavior */ 6663 prsm = TAILQ_PREV(rsm, rack_head, r_tnext); 6664 if (prsm && (len <= segsiz)) { 6665 /* 6666 * Two packets outstanding, thresh should be (2*srtt) + 6667 * possible inter-packet delay (if any). 6668 */ 6669 uint32_t inter_gap = 0; 6670 int idx, nidx; 6671 6672 idx = rsm->r_rtr_cnt - 1; 6673 nidx = prsm->r_rtr_cnt - 1; 6674 if (rsm->r_tim_lastsent[nidx] >= prsm->r_tim_lastsent[idx]) { 6675 /* Yes it was sent later (or at the same time) */ 6676 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 6677 } 6678 thresh += inter_gap; 6679 } else if (len <= segsiz) { 6680 /* 6681 * Possibly compensate for delayed-ack. 6682 */ 6683 uint32_t alt_thresh; 6684 6685 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 6686 if (alt_thresh > thresh) 6687 thresh = alt_thresh; 6688 } 6689 } else if (rack->rack_tlp_threshold_use == TLP_USE_TWO_TWO) { 6690 /* 2.2 behavior */ 6691 if (len <= segsiz) { 6692 uint32_t alt_thresh; 6693 /* 6694 * Compensate for delayed-ack with the d-ack time. 6695 */ 6696 alt_thresh = srtt + (srtt / 2) + rack_delayed_ack_time; 6697 if (alt_thresh > thresh) 6698 thresh = alt_thresh; 6699 } 6700 } 6701 /* Not above an RTO */ 6702 if (thresh > tp->t_rxtcur) { 6703 thresh = tp->t_rxtcur; 6704 } 6705 /* Not above a RTO max */ 6706 if (thresh > rack_rto_max) { 6707 thresh = rack_rto_max; 6708 } 6709 /* Apply user supplied min TLP */ 6710 if (thresh < rack_tlp_min) { 6711 thresh = rack_tlp_min; 6712 } 6713 return (thresh); 6714 } 6715 6716 static uint32_t 6717 rack_grab_rtt(struct tcpcb *tp, struct tcp_rack *rack) 6718 { 6719 /* 6720 * We want the rack_rtt which is the 6721 * last rtt we measured. However if that 6722 * does not exist we fallback to the srtt (which 6723 * we probably will never do) and then as a last 6724 * resort we use RACK_INITIAL_RTO if no srtt is 6725 * yet set. 6726 */ 6727 if (rack->rc_rack_rtt) 6728 return (rack->rc_rack_rtt); 6729 else if (tp->t_srtt == 0) 6730 return (RACK_INITIAL_RTO); 6731 return (tp->t_srtt); 6732 } 6733 6734 static struct rack_sendmap * 6735 rack_check_recovery_mode(struct tcpcb *tp, uint32_t tsused) 6736 { 6737 /* 6738 * Check to see that we don't need to fall into recovery. We will 6739 * need to do so if our oldest transmit is past the time we should 6740 * have had an ack. 6741 */ 6742 struct tcp_rack *rack; 6743 struct rack_sendmap *rsm; 6744 int32_t idx; 6745 uint32_t srtt, thresh; 6746 6747 rack = (struct tcp_rack *)tp->t_fb_ptr; 6748 if (tqhash_empty(rack->r_ctl.tqh)) { 6749 return (NULL); 6750 } 6751 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6752 if (rsm == NULL) 6753 return (NULL); 6754 6755 6756 if (rsm->r_flags & RACK_ACKED) { 6757 rsm = rack_find_lowest_rsm(rack); 6758 if (rsm == NULL) 6759 return (NULL); 6760 } 6761 idx = rsm->r_rtr_cnt - 1; 6762 srtt = rack_grab_rtt(tp, rack); 6763 thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1); 6764 if (TSTMP_LT(tsused, ((uint32_t)rsm->r_tim_lastsent[idx]))) { 6765 return (NULL); 6766 } 6767 if ((tsused - ((uint32_t)rsm->r_tim_lastsent[idx])) < thresh) { 6768 return (NULL); 6769 } 6770 /* Ok if we reach here we are over-due and this guy can be sent */ 6771 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__); 6772 return (rsm); 6773 } 6774 6775 static uint32_t 6776 rack_get_persists_timer_val(struct tcpcb *tp, struct tcp_rack *rack) 6777 { 6778 int32_t t; 6779 int32_t tt; 6780 uint32_t ret_val; 6781 6782 t = (tp->t_srtt + (tp->t_rttvar << 2)); 6783 RACK_TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 6784 rack_persist_min, rack_persist_max, rack->r_ctl.timer_slop); 6785 rack->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 6786 ret_val = (uint32_t)tt; 6787 return (ret_val); 6788 } 6789 6790 static uint32_t 6791 rack_timer_start(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int sup_rack) 6792 { 6793 /* 6794 * Start the FR timer, we do this based on getting the first one in 6795 * the rc_tmap. Note that if its NULL we must stop the timer. in all 6796 * events we need to stop the running timer (if its running) before 6797 * starting the new one. 6798 */ 6799 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 6800 uint32_t srtt_cur; 6801 int32_t idx; 6802 int32_t is_tlp_timer = 0; 6803 struct rack_sendmap *rsm; 6804 6805 if (rack->t_timers_stopped) { 6806 /* All timers have been stopped none are to run */ 6807 return (0); 6808 } 6809 if (rack->rc_in_persist) { 6810 /* We can't start any timer in persists */ 6811 return (rack_get_persists_timer_val(tp, rack)); 6812 } 6813 rack->rc_on_min_to = 0; 6814 if ((tp->t_state < TCPS_ESTABLISHED) || 6815 (rack->sack_attack_disable > 0) || 6816 ((tp->t_flags & TF_SACK_PERMIT) == 0)) { 6817 goto activate_rxt; 6818 } 6819 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6820 if ((rsm == NULL) || sup_rack) { 6821 /* Nothing on the send map or no rack */ 6822 activate_rxt: 6823 time_since_sent = 0; 6824 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 6825 if (rsm) { 6826 /* 6827 * Should we discount the RTX timer any? 6828 * 6829 * We want to discount it the smallest amount. 6830 * If a timer (Rack/TLP or RXT) has gone off more 6831 * recently thats the discount we want to use (now - timer time). 6832 * If the retransmit of the oldest packet was more recent then 6833 * we want to use that (now - oldest-packet-last_transmit_time). 6834 * 6835 */ 6836 idx = rsm->r_rtr_cnt - 1; 6837 if (TSTMP_GEQ(rack->r_ctl.rc_tlp_rxt_last_time, ((uint32_t)rsm->r_tim_lastsent[idx]))) 6838 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time; 6839 else 6840 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx]; 6841 if (TSTMP_GT(cts, tstmp_touse)) 6842 time_since_sent = cts - tstmp_touse; 6843 } 6844 if (SEQ_LT(tp->snd_una, tp->snd_max) || 6845 sbavail(&tptosocket(tp)->so_snd)) { 6846 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 6847 to = tp->t_rxtcur; 6848 if (to > time_since_sent) 6849 to -= time_since_sent; 6850 else 6851 to = rack->r_ctl.rc_min_to; 6852 if (to == 0) 6853 to = 1; 6854 /* Special case for KEEPINIT */ 6855 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) && 6856 (TP_KEEPINIT(tp) != 0) && 6857 rsm) { 6858 /* 6859 * We have to put a ceiling on the rxt timer 6860 * of the keep-init timeout. 6861 */ 6862 uint32_t max_time, red; 6863 6864 max_time = TICKS_2_USEC(TP_KEEPINIT(tp)); 6865 if (TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) { 6866 red = (cts - (uint32_t)rsm->r_tim_lastsent[0]); 6867 if (red < max_time) 6868 max_time -= red; 6869 else 6870 max_time = 1; 6871 } 6872 /* Reduce timeout to the keep value if needed */ 6873 if (max_time < to) 6874 to = max_time; 6875 } 6876 return (to); 6877 } 6878 return (0); 6879 } 6880 if (rsm->r_flags & RACK_ACKED) { 6881 rsm = rack_find_lowest_rsm(rack); 6882 if (rsm == NULL) { 6883 /* No lowest? */ 6884 goto activate_rxt; 6885 } 6886 } 6887 if (rack->sack_attack_disable) { 6888 /* 6889 * We don't want to do 6890 * any TLP's if you are an attacker. 6891 * Though if you are doing what 6892 * is expected you may still have 6893 * SACK-PASSED marks. 6894 */ 6895 goto activate_rxt; 6896 } 6897 /* Convert from ms to usecs */ 6898 if ((rsm->r_flags & RACK_SACK_PASSED) || 6899 (rsm->r_flags & RACK_RWND_COLLAPSED) || 6900 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) { 6901 if ((tp->t_flags & TF_SENTFIN) && 6902 ((tp->snd_max - tp->snd_una) == 1) && 6903 (rsm->r_flags & RACK_HAS_FIN)) { 6904 /* 6905 * We don't start a rack timer if all we have is a 6906 * FIN outstanding. 6907 */ 6908 goto activate_rxt; 6909 } 6910 if ((rack->use_rack_rr == 0) && 6911 (IN_FASTRECOVERY(tp->t_flags)) && 6912 (rack->rack_no_prr == 0) && 6913 (rack->r_ctl.rc_prr_sndcnt < ctf_fixed_maxseg(tp))) { 6914 /* 6915 * We are not cheating, in recovery and 6916 * not enough ack's to yet get our next 6917 * retransmission out. 6918 * 6919 * Note that classified attackers do not 6920 * get to use the rack-cheat. 6921 */ 6922 goto activate_tlp; 6923 } 6924 srtt = rack_grab_rtt(tp, rack); 6925 thresh = rack_calc_thresh_rack(rack, srtt, cts, __LINE__, 1); 6926 idx = rsm->r_rtr_cnt - 1; 6927 exp = ((uint32_t)rsm->r_tim_lastsent[idx]) + thresh; 6928 if (SEQ_GEQ(exp, cts)) { 6929 to = exp - cts; 6930 if (to < rack->r_ctl.rc_min_to) { 6931 to = rack->r_ctl.rc_min_to; 6932 if (rack->r_rr_config == 3) 6933 rack->rc_on_min_to = 1; 6934 } 6935 } else { 6936 to = rack->r_ctl.rc_min_to; 6937 if (rack->r_rr_config == 3) 6938 rack->rc_on_min_to = 1; 6939 } 6940 } else { 6941 /* Ok we need to do a TLP not RACK */ 6942 activate_tlp: 6943 if ((rack->rc_tlp_in_progress != 0) && 6944 (rack->r_ctl.rc_tlp_cnt_out >= rack_tlp_limit)) { 6945 /* 6946 * The previous send was a TLP and we have sent 6947 * N TLP's without sending new data. 6948 */ 6949 goto activate_rxt; 6950 } 6951 rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 6952 if (rsm == NULL) { 6953 /* We found no rsm to TLP with. */ 6954 goto activate_rxt; 6955 } 6956 if (rsm->r_flags & RACK_HAS_FIN) { 6957 /* If its a FIN we dont do TLP */ 6958 rsm = NULL; 6959 goto activate_rxt; 6960 } 6961 idx = rsm->r_rtr_cnt - 1; 6962 time_since_sent = 0; 6963 if (TSTMP_GEQ(((uint32_t)rsm->r_tim_lastsent[idx]), rack->r_ctl.rc_tlp_rxt_last_time)) 6964 tstmp_touse = (uint32_t)rsm->r_tim_lastsent[idx]; 6965 else 6966 tstmp_touse = (uint32_t)rack->r_ctl.rc_tlp_rxt_last_time; 6967 if (TSTMP_GT(cts, tstmp_touse)) 6968 time_since_sent = cts - tstmp_touse; 6969 is_tlp_timer = 1; 6970 if (tp->t_srtt) { 6971 if ((rack->rc_srtt_measure_made == 0) && 6972 (tp->t_srtt == 1)) { 6973 /* 6974 * If another stack as run and set srtt to 1, 6975 * then the srtt was 0, so lets use the initial. 6976 */ 6977 srtt = RACK_INITIAL_RTO; 6978 } else { 6979 srtt_cur = tp->t_srtt; 6980 srtt = srtt_cur; 6981 } 6982 } else 6983 srtt = RACK_INITIAL_RTO; 6984 /* 6985 * If the SRTT is not keeping up and the 6986 * rack RTT has spiked we want to use 6987 * the last RTT not the smoothed one. 6988 */ 6989 if (rack_tlp_use_greater && 6990 tp->t_srtt && 6991 (srtt < rack_grab_rtt(tp, rack))) { 6992 srtt = rack_grab_rtt(tp, rack); 6993 } 6994 thresh = rack_calc_thresh_tlp(tp, rack, rsm, srtt); 6995 if (thresh > time_since_sent) { 6996 to = thresh - time_since_sent; 6997 } else { 6998 to = rack->r_ctl.rc_min_to; 6999 rack_log_alt_to_to_cancel(rack, 7000 thresh, /* flex1 */ 7001 time_since_sent, /* flex2 */ 7002 tstmp_touse, /* flex3 */ 7003 rack->r_ctl.rc_tlp_rxt_last_time, /* flex4 */ 7004 (uint32_t)rsm->r_tim_lastsent[idx], 7005 srtt, 7006 idx, 99); 7007 } 7008 if (to < rack_tlp_min) { 7009 to = rack_tlp_min; 7010 } 7011 if (to > TICKS_2_USEC(TCPTV_REXMTMAX)) { 7012 /* 7013 * If the TLP time works out to larger than the max 7014 * RTO lets not do TLP.. just RTO. 7015 */ 7016 goto activate_rxt; 7017 } 7018 } 7019 if (is_tlp_timer == 0) { 7020 rack->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 7021 } else { 7022 rack->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 7023 } 7024 if (to == 0) 7025 to = 1; 7026 return (to); 7027 } 7028 7029 static void 7030 rack_enter_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, tcp_seq snd_una) 7031 { 7032 if (rack->rc_in_persist == 0) { 7033 if (tp->t_flags & TF_GPUTINPROG) { 7034 /* 7035 * Stop the goodput now, the calling of the 7036 * measurement function clears the flag. 7037 */ 7038 rack_do_goodput_measurement(tp, rack, tp->snd_una, __LINE__, 7039 RACK_QUALITY_PERSIST); 7040 } 7041 #ifdef NETFLIX_SHARED_CWND 7042 if (rack->r_ctl.rc_scw) { 7043 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 7044 rack->rack_scwnd_is_idle = 1; 7045 } 7046 #endif 7047 rack->r_ctl.rc_went_idle_time = cts; 7048 if (rack->r_ctl.rc_went_idle_time == 0) 7049 rack->r_ctl.rc_went_idle_time = 1; 7050 if (rack->lt_bw_up) { 7051 /* Suspend our LT BW measurement */ 7052 uint64_t tmark; 7053 7054 rack->r_ctl.lt_bw_bytes += (snd_una - rack->r_ctl.lt_seq); 7055 rack->r_ctl.lt_seq = snd_una; 7056 tmark = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time); 7057 if (tmark >= rack->r_ctl.lt_timemark) { 7058 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark); 7059 } 7060 rack->r_ctl.lt_timemark = tmark; 7061 rack->lt_bw_up = 0; 7062 rack->r_persist_lt_bw_off = 1; 7063 } 7064 rack_timer_cancel(tp, rack, cts, __LINE__); 7065 rack->r_ctl.persist_lost_ends = 0; 7066 rack->probe_not_answered = 0; 7067 rack->forced_ack = 0; 7068 tp->t_rxtshift = 0; 7069 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 7070 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 7071 rack->rc_in_persist = 1; 7072 } 7073 } 7074 7075 static void 7076 rack_exit_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 7077 { 7078 if (tcp_in_hpts(rack->rc_tp)) { 7079 tcp_hpts_remove(rack->rc_tp); 7080 rack->r_ctl.rc_hpts_flags = 0; 7081 } 7082 #ifdef NETFLIX_SHARED_CWND 7083 if (rack->r_ctl.rc_scw) { 7084 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 7085 rack->rack_scwnd_is_idle = 0; 7086 } 7087 #endif 7088 if (rack->rc_gp_dyn_mul && 7089 (rack->use_fixed_rate == 0) && 7090 (rack->rc_always_pace)) { 7091 /* 7092 * Do we count this as if a probe-rtt just 7093 * finished? 7094 */ 7095 uint32_t time_idle, idle_min; 7096 7097 time_idle = cts - rack->r_ctl.rc_went_idle_time; 7098 idle_min = rack_min_probertt_hold; 7099 if (rack_probertt_gpsrtt_cnt_div) { 7100 uint64_t extra; 7101 extra = (uint64_t)rack->r_ctl.rc_gp_srtt * 7102 (uint64_t)rack_probertt_gpsrtt_cnt_mul; 7103 extra /= (uint64_t)rack_probertt_gpsrtt_cnt_div; 7104 idle_min += (uint32_t)extra; 7105 } 7106 if (time_idle >= idle_min) { 7107 /* Yes, we count it as a probe-rtt. */ 7108 uint32_t us_cts; 7109 7110 us_cts = tcp_get_usecs(NULL); 7111 if (rack->in_probe_rtt == 0) { 7112 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 7113 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts; 7114 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts; 7115 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts; 7116 } else { 7117 rack_exit_probertt(rack, us_cts); 7118 } 7119 } 7120 } 7121 if (rack->r_persist_lt_bw_off) { 7122 /* Continue where we left off */ 7123 rack->r_ctl.lt_timemark = tcp_get_u64_usecs(NULL); 7124 rack->lt_bw_up = 1; 7125 rack->r_persist_lt_bw_off = 0; 7126 } 7127 rack->r_ctl.idle_snd_una = tp->snd_una; 7128 rack->rc_in_persist = 0; 7129 rack->r_ctl.rc_went_idle_time = 0; 7130 tp->t_rxtshift = 0; 7131 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 7132 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 7133 rack->r_ctl.rc_agg_delayed = 0; 7134 rack->r_early = 0; 7135 rack->r_late = 0; 7136 rack->r_ctl.rc_agg_early = 0; 7137 } 7138 7139 static void 7140 rack_log_hpts_diag(struct tcp_rack *rack, uint32_t cts, 7141 struct hpts_diag *diag, struct timeval *tv) 7142 { 7143 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 7144 union tcp_log_stackspecific log; 7145 7146 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 7147 log.u_bbr.flex1 = diag->p_nxt_slot; 7148 log.u_bbr.flex2 = diag->p_cur_slot; 7149 log.u_bbr.flex3 = diag->slot_req; 7150 log.u_bbr.flex4 = diag->inp_hptsslot; 7151 log.u_bbr.flex5 = diag->slot_remaining; 7152 log.u_bbr.flex6 = diag->need_new_to; 7153 log.u_bbr.flex7 = diag->p_hpts_active; 7154 log.u_bbr.flex8 = diag->p_on_min_sleep; 7155 /* Hijack other fields as needed */ 7156 log.u_bbr.epoch = diag->have_slept; 7157 log.u_bbr.lt_epoch = diag->yet_to_sleep; 7158 log.u_bbr.pkts_out = diag->co_ret; 7159 log.u_bbr.applimited = diag->hpts_sleep_time; 7160 log.u_bbr.delivered = diag->p_prev_slot; 7161 log.u_bbr.inflight = diag->p_runningslot; 7162 log.u_bbr.bw_inuse = diag->wheel_slot; 7163 log.u_bbr.rttProp = diag->wheel_cts; 7164 log.u_bbr.timeStamp = cts; 7165 log.u_bbr.delRate = diag->maxslots; 7166 log.u_bbr.cur_del_rate = diag->p_curtick; 7167 log.u_bbr.cur_del_rate <<= 32; 7168 log.u_bbr.cur_del_rate |= diag->p_lasttick; 7169 TCP_LOG_EVENTP(rack->rc_tp, NULL, 7170 &rack->rc_inp->inp_socket->so_rcv, 7171 &rack->rc_inp->inp_socket->so_snd, 7172 BBR_LOG_HPTSDIAG, 0, 7173 0, &log, false, tv); 7174 } 7175 7176 } 7177 7178 static void 7179 rack_log_wakeup(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb, uint32_t len, int type) 7180 { 7181 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 7182 union tcp_log_stackspecific log; 7183 struct timeval tv; 7184 7185 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 7186 log.u_bbr.flex1 = sb->sb_flags; 7187 log.u_bbr.flex2 = len; 7188 log.u_bbr.flex3 = sb->sb_state; 7189 log.u_bbr.flex8 = type; 7190 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 7191 TCP_LOG_EVENTP(rack->rc_tp, NULL, 7192 &rack->rc_inp->inp_socket->so_rcv, 7193 &rack->rc_inp->inp_socket->so_snd, 7194 TCP_LOG_SB_WAKE, 0, 7195 len, &log, false, &tv); 7196 } 7197 } 7198 7199 static void 7200 rack_start_hpts_timer (struct tcp_rack *rack, struct tcpcb *tp, uint32_t cts, 7201 int32_t slot, uint32_t tot_len_this_send, int sup_rack) 7202 { 7203 struct hpts_diag diag; 7204 struct inpcb *inp = tptoinpcb(tp); 7205 struct timeval tv; 7206 uint32_t delayed_ack = 0; 7207 uint32_t hpts_timeout; 7208 uint32_t entry_slot = slot; 7209 uint8_t stopped; 7210 uint32_t left = 0; 7211 uint32_t us_cts; 7212 7213 if ((tp->t_state == TCPS_CLOSED) || 7214 (tp->t_state == TCPS_LISTEN)) { 7215 return; 7216 } 7217 if (tcp_in_hpts(tp)) { 7218 /* Already on the pacer */ 7219 return; 7220 } 7221 stopped = rack->rc_tmr_stopped; 7222 if (stopped && TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) { 7223 left = rack->r_ctl.rc_timer_exp - cts; 7224 } 7225 rack->r_ctl.rc_timer_exp = 0; 7226 rack->r_ctl.rc_hpts_flags = 0; 7227 us_cts = tcp_get_usecs(&tv); 7228 /* Now early/late accounting */ 7229 rack_log_pacing_delay_calc(rack, entry_slot, slot, 0, 0, 0, 26, __LINE__, NULL, 0); 7230 if (rack->r_early && (rack->rc_ack_can_sendout_data == 0)) { 7231 /* 7232 * We have a early carry over set, 7233 * we can always add more time so we 7234 * can always make this compensation. 7235 * 7236 * Note if ack's are allowed to wake us do not 7237 * penalize the next timer for being awoke 7238 * by an ack aka the rc_agg_early (non-paced mode). 7239 */ 7240 slot += rack->r_ctl.rc_agg_early; 7241 rack->r_early = 0; 7242 rack->r_ctl.rc_agg_early = 0; 7243 } 7244 if ((rack->r_late) && 7245 ((rack->r_use_hpts_min == 0) || (rack->dgp_on == 0))) { 7246 /* 7247 * This is harder, we can 7248 * compensate some but it 7249 * really depends on what 7250 * the current pacing time is. 7251 */ 7252 if (rack->r_ctl.rc_agg_delayed >= slot) { 7253 /* 7254 * We can't compensate for it all. 7255 * And we have to have some time 7256 * on the clock. We always have a min 7257 * 10 slots (10 x 10 i.e. 100 usecs). 7258 */ 7259 if (slot <= HPTS_TICKS_PER_SLOT) { 7260 /* We gain delay */ 7261 rack->r_ctl.rc_agg_delayed += (HPTS_TICKS_PER_SLOT - slot); 7262 slot = HPTS_TICKS_PER_SLOT; 7263 } else { 7264 /* We take off some */ 7265 rack->r_ctl.rc_agg_delayed -= (slot - HPTS_TICKS_PER_SLOT); 7266 slot = HPTS_TICKS_PER_SLOT; 7267 } 7268 } else { 7269 slot -= rack->r_ctl.rc_agg_delayed; 7270 rack->r_ctl.rc_agg_delayed = 0; 7271 /* Make sure we have 100 useconds at minimum */ 7272 if (slot < HPTS_TICKS_PER_SLOT) { 7273 rack->r_ctl.rc_agg_delayed = HPTS_TICKS_PER_SLOT - slot; 7274 slot = HPTS_TICKS_PER_SLOT; 7275 } 7276 if (rack->r_ctl.rc_agg_delayed == 0) 7277 rack->r_late = 0; 7278 } 7279 } else if (rack->r_late) { 7280 /* r_use_hpts_min is on and so is DGP */ 7281 uint32_t max_red; 7282 7283 max_red = (slot * rack->r_ctl.max_reduction) / 100; 7284 if (max_red >= rack->r_ctl.rc_agg_delayed) { 7285 slot -= rack->r_ctl.rc_agg_delayed; 7286 rack->r_ctl.rc_agg_delayed = 0; 7287 } else { 7288 slot -= max_red; 7289 rack->r_ctl.rc_agg_delayed -= max_red; 7290 } 7291 } 7292 if ((rack->r_use_hpts_min == 1) && 7293 (slot > 0) && 7294 (rack->dgp_on == 1)) { 7295 /* 7296 * We are enforcing a min pacing timer 7297 * based on our hpts min timeout. 7298 */ 7299 uint32_t min; 7300 7301 min = get_hpts_min_sleep_time(); 7302 if (min > slot) { 7303 slot = min; 7304 } 7305 } 7306 hpts_timeout = rack_timer_start(tp, rack, cts, sup_rack); 7307 #ifdef TCP_SAD_DETECTION 7308 if (rack->sack_attack_disable && 7309 (rack->r_ctl.ack_during_sd > 0) && 7310 (slot < tcp_sad_pacing_interval)) { 7311 /* 7312 * We have a potential attacker on 7313 * the line. We have possibly some 7314 * (or now) pacing time set. We want to 7315 * slow down the processing of sacks by some 7316 * amount (if it is an attacker). Set the default 7317 * slot for attackers in place (unless the original 7318 * interval is longer). Its stored in 7319 * micro-seconds, so lets convert to msecs. 7320 */ 7321 slot = tcp_sad_pacing_interval; 7322 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__); 7323 rack->r_ctl.ack_during_sd = 0; 7324 } 7325 #endif 7326 if (tp->t_flags & TF_DELACK) { 7327 delayed_ack = TICKS_2_USEC(tcp_delacktime); 7328 rack->r_ctl.rc_hpts_flags |= PACE_TMR_DELACK; 7329 } 7330 if (delayed_ack && ((hpts_timeout == 0) || 7331 (delayed_ack < hpts_timeout))) 7332 hpts_timeout = delayed_ack; 7333 else 7334 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 7335 /* 7336 * If no timers are going to run and we will fall off the hptsi 7337 * wheel, we resort to a keep-alive timer if its configured. 7338 */ 7339 if ((hpts_timeout == 0) && 7340 (slot == 0)) { 7341 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 7342 (tp->t_state <= TCPS_CLOSING)) { 7343 /* 7344 * Ok we have no timer (persists, rack, tlp, rxt or 7345 * del-ack), we don't have segments being paced. So 7346 * all that is left is the keepalive timer. 7347 */ 7348 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 7349 /* Get the established keep-alive time */ 7350 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 7351 } else { 7352 /* 7353 * Get the initial setup keep-alive time, 7354 * note that this is probably not going to 7355 * happen, since rack will be running a rxt timer 7356 * if a SYN of some sort is outstanding. It is 7357 * actually handled in rack_timeout_rxt(). 7358 */ 7359 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 7360 } 7361 rack->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 7362 if (rack->in_probe_rtt) { 7363 /* 7364 * We want to instead not wake up a long time from 7365 * now but to wake up about the time we would 7366 * exit probe-rtt and initiate a keep-alive ack. 7367 * This will get us out of probe-rtt and update 7368 * our min-rtt. 7369 */ 7370 hpts_timeout = rack_min_probertt_hold; 7371 } 7372 } 7373 } 7374 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 7375 (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 7376 /* 7377 * RACK, TLP, persists and RXT timers all are restartable 7378 * based on actions input .. i.e we received a packet (ack 7379 * or sack) and that changes things (rw, or snd_una etc). 7380 * Thus we can restart them with a new value. For 7381 * keep-alive, delayed_ack we keep track of what was left 7382 * and restart the timer with a smaller value. 7383 */ 7384 if (left < hpts_timeout) 7385 hpts_timeout = left; 7386 } 7387 if (hpts_timeout) { 7388 /* 7389 * Hack alert for now we can't time-out over 2,147,483 7390 * seconds (a bit more than 596 hours), which is probably ok 7391 * :). 7392 */ 7393 if (hpts_timeout > 0x7ffffffe) 7394 hpts_timeout = 0x7ffffffe; 7395 rack->r_ctl.rc_timer_exp = cts + hpts_timeout; 7396 } 7397 rack_log_pacing_delay_calc(rack, entry_slot, slot, hpts_timeout, 0, 0, 27, __LINE__, NULL, 0); 7398 if ((rack->gp_ready == 0) && 7399 (rack->use_fixed_rate == 0) && 7400 (hpts_timeout < slot) && 7401 (rack->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 7402 /* 7403 * We have no good estimate yet for the 7404 * old clunky burst mitigation or the 7405 * real pacing. And the tlp or rxt is smaller 7406 * than the pacing calculation. Lets not 7407 * pace that long since we know the calculation 7408 * so far is not accurate. 7409 */ 7410 slot = hpts_timeout; 7411 } 7412 /** 7413 * Turn off all the flags for queuing by default. The 7414 * flags have important meanings to what happens when 7415 * LRO interacts with the transport. Most likely (by default now) 7416 * mbuf_queueing and ack compression are on. So the transport 7417 * has a couple of flags that control what happens (if those 7418 * are not on then these flags won't have any effect since it 7419 * won't go through the queuing LRO path). 7420 * 7421 * TF2_MBUF_QUEUE_READY - This flags says that I am busy 7422 * pacing output, so don't disturb. But 7423 * it also means LRO can wake me if there 7424 * is a SACK arrival. 7425 * 7426 * TF2_DONT_SACK_QUEUE - This flag is used in conjunction 7427 * with the above flag (QUEUE_READY) and 7428 * when present it says don't even wake me 7429 * if a SACK arrives. 7430 * 7431 * The idea behind these flags is that if we are pacing we 7432 * set the MBUF_QUEUE_READY and only get woken up if 7433 * a SACK arrives (which could change things) or if 7434 * our pacing timer expires. If, however, we have a rack 7435 * timer running, then we don't even want a sack to wake 7436 * us since the rack timer has to expire before we can send. 7437 * 7438 * Other cases should usually have none of the flags set 7439 * so LRO can call into us. 7440 */ 7441 tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE|TF2_MBUF_QUEUE_READY); 7442 if (slot) { 7443 rack->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 7444 rack->r_ctl.rc_last_output_to = us_cts + slot; 7445 /* 7446 * A pacing timer (slot) is being set, in 7447 * such a case we cannot send (we are blocked by 7448 * the timer). So lets tell LRO that it should not 7449 * wake us unless there is a SACK. Note this only 7450 * will be effective if mbuf queueing is on or 7451 * compressed acks are being processed. 7452 */ 7453 tp->t_flags2 |= TF2_MBUF_QUEUE_READY; 7454 /* 7455 * But wait if we have a Rack timer running 7456 * even a SACK should not disturb us (with 7457 * the exception of r_rr_config 3). 7458 */ 7459 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK) || 7460 (IN_RECOVERY(tp->t_flags))) { 7461 if (rack->r_rr_config != 3) 7462 tp->t_flags2 |= TF2_DONT_SACK_QUEUE; 7463 else if (rack->rc_pace_dnd) { 7464 /* 7465 * When DND is on, we only let a sack 7466 * interrupt us if we are not in recovery. 7467 * 7468 * If DND is off, then we never hit here 7469 * and let all sacks wake us up. 7470 * 7471 */ 7472 tp->t_flags2 |= TF2_DONT_SACK_QUEUE; 7473 } 7474 } 7475 /* For sack attackers we want to ignore sack */ 7476 if (rack->sack_attack_disable == 1) { 7477 tp->t_flags2 |= (TF2_DONT_SACK_QUEUE | 7478 TF2_MBUF_QUEUE_READY); 7479 } else if (rack->rc_ack_can_sendout_data) { 7480 /* 7481 * Ahh but wait, this is that special case 7482 * where the pacing timer can be disturbed 7483 * backout the changes (used for non-paced 7484 * burst limiting). 7485 */ 7486 tp->t_flags2 &= ~(TF2_DONT_SACK_QUEUE | 7487 TF2_MBUF_QUEUE_READY); 7488 } 7489 if ((rack->use_rack_rr) && 7490 (rack->r_rr_config < 2) && 7491 ((hpts_timeout) && (hpts_timeout < slot))) { 7492 /* 7493 * Arrange for the hpts to kick back in after the 7494 * t-o if the t-o does not cause a send. 7495 */ 7496 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout), 7497 __LINE__, &diag); 7498 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 7499 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 7500 } else { 7501 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot), 7502 __LINE__, &diag); 7503 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 7504 rack_log_to_start(rack, cts, hpts_timeout, slot, 1); 7505 } 7506 } else if (hpts_timeout) { 7507 /* 7508 * With respect to t_flags2(?) here, lets let any new acks wake 7509 * us up here. Since we are not pacing (no pacing timer), output 7510 * can happen so we should let it. If its a Rack timer, then any inbound 7511 * packet probably won't change the sending (we will be blocked) 7512 * but it may change the prr stats so letting it in (the set defaults 7513 * at the start of this block) are good enough. 7514 */ 7515 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 7516 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout), 7517 __LINE__, &diag); 7518 rack_log_hpts_diag(rack, us_cts, &diag, &tv); 7519 rack_log_to_start(rack, cts, hpts_timeout, slot, 0); 7520 } else { 7521 /* No timer starting */ 7522 #ifdef INVARIANTS 7523 if (SEQ_GT(tp->snd_max, tp->snd_una)) { 7524 panic("tp:%p rack:%p tlts:%d cts:%u slot:%u pto:%u -- no timer started?", 7525 tp, rack, tot_len_this_send, cts, slot, hpts_timeout); 7526 } 7527 #endif 7528 } 7529 rack->rc_tmr_stopped = 0; 7530 if (slot) 7531 rack_log_type_bbrsnd(rack, tot_len_this_send, slot, us_cts, &tv, __LINE__); 7532 } 7533 7534 static void 7535 rack_mark_lost(struct tcpcb *tp, 7536 struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts) 7537 { 7538 struct rack_sendmap *nrsm; 7539 uint32_t thresh, exp; 7540 7541 thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0); 7542 nrsm = rsm; 7543 TAILQ_FOREACH_FROM(nrsm, &rack->r_ctl.rc_tmap, r_tnext) { 7544 if ((nrsm->r_flags & RACK_SACK_PASSED) == 0) { 7545 /* Got up to all that were marked sack-passed */ 7546 break; 7547 } 7548 if ((nrsm->r_flags & RACK_WAS_LOST) == 0) { 7549 exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh; 7550 if (TSTMP_LT(exp, cts) || (exp == cts)) { 7551 /* We now consider it lost */ 7552 nrsm->r_flags |= RACK_WAS_LOST; 7553 rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start; 7554 } else { 7555 /* Past here it won't be lost so stop */ 7556 break; 7557 } 7558 } 7559 } 7560 } 7561 7562 /* 7563 * RACK Timer, here we simply do logging and house keeping. 7564 * the normal rack_output() function will call the 7565 * appropriate thing to check if we need to do a RACK retransmit. 7566 * We return 1, saying don't proceed with rack_output only 7567 * when all timers have been stopped (destroyed PCB?). 7568 */ 7569 static int 7570 rack_timeout_rack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 7571 { 7572 /* 7573 * This timer simply provides an internal trigger to send out data. 7574 * The check_recovery_mode call will see if there are needed 7575 * retransmissions, if so we will enter fast-recovery. The output 7576 * call may or may not do the same thing depending on sysctl 7577 * settings. 7578 */ 7579 struct rack_sendmap *rsm; 7580 7581 counter_u64_add(rack_to_tot, 1); 7582 if (rack->r_state && (rack->r_state != tp->t_state)) 7583 rack_set_state(tp, rack); 7584 rack->rc_on_min_to = 0; 7585 rsm = rack_check_recovery_mode(tp, cts); 7586 rack_log_to_event(rack, RACK_TO_FRM_RACK, rsm); 7587 if (rsm) { 7588 /* We need to stroke any lost that are now declared as lost */ 7589 rack_mark_lost(tp, rack, rsm, cts); 7590 rack->r_ctl.rc_resend = rsm; 7591 rack->r_timer_override = 1; 7592 if (rack->use_rack_rr) { 7593 /* 7594 * Don't accumulate extra pacing delay 7595 * we are allowing the rack timer to 7596 * over-ride pacing i.e. rrr takes precedence 7597 * if the pacing interval is longer than the rrr 7598 * time (in other words we get the min pacing 7599 * time versus rrr pacing time). 7600 */ 7601 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 7602 } 7603 } 7604 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 7605 if (rsm == NULL) { 7606 /* restart a timer and return 1 */ 7607 rack_start_hpts_timer(rack, tp, cts, 7608 0, 0, 0); 7609 return (1); 7610 } 7611 if ((rack->policer_detect_on == 1) && 7612 (rack->rc_policer_detected == 0)) { 7613 /* 7614 * We do this early if we have not 7615 * deteceted to attempt to detect 7616 * quicker. Normally we want to do this 7617 * as recovery exits (and we will again). 7618 */ 7619 policer_detection(tp, rack, 0); 7620 } 7621 return (0); 7622 } 7623 7624 7625 7626 static void 7627 rack_adjust_orig_mlen(struct rack_sendmap *rsm) 7628 { 7629 7630 if ((M_TRAILINGROOM(rsm->m) != rsm->orig_t_space)) { 7631 /* 7632 * The trailing space changed, mbufs can grow 7633 * at the tail but they can't shrink from 7634 * it, KASSERT that. Adjust the orig_m_len to 7635 * compensate for this change. 7636 */ 7637 KASSERT((rsm->orig_t_space > M_TRAILINGROOM(rsm->m)), 7638 ("mbuf:%p rsm:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n", 7639 rsm->m, 7640 rsm, 7641 (intmax_t)M_TRAILINGROOM(rsm->m), 7642 rsm->orig_t_space, 7643 rsm->orig_m_len, 7644 rsm->m->m_len)); 7645 rsm->orig_m_len += (rsm->orig_t_space - M_TRAILINGROOM(rsm->m)); 7646 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 7647 } 7648 if (rsm->m->m_len < rsm->orig_m_len) { 7649 /* 7650 * Mbuf shrank, trimmed off the top by an ack, our 7651 * offset changes. 7652 */ 7653 KASSERT((rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)), 7654 ("mbuf:%p len:%u rsm:%p oml:%u soff:%u\n", 7655 rsm->m, rsm->m->m_len, 7656 rsm, rsm->orig_m_len, 7657 rsm->soff)); 7658 if (rsm->soff >= (rsm->orig_m_len - rsm->m->m_len)) 7659 rsm->soff -= (rsm->orig_m_len - rsm->m->m_len); 7660 else 7661 rsm->soff = 0; 7662 rsm->orig_m_len = rsm->m->m_len; 7663 #ifdef INVARIANTS 7664 } else if (rsm->m->m_len > rsm->orig_m_len) { 7665 panic("rsm:%p m:%p m_len grew outside of t_space compensation", 7666 rsm, rsm->m); 7667 #endif 7668 } 7669 } 7670 7671 static void 7672 rack_setup_offset_for_rsm(struct tcp_rack *rack, struct rack_sendmap *src_rsm, struct rack_sendmap *rsm) 7673 { 7674 struct mbuf *m; 7675 uint32_t soff; 7676 7677 if (src_rsm->m && 7678 ((src_rsm->orig_m_len != src_rsm->m->m_len) || 7679 (M_TRAILINGROOM(src_rsm->m) != src_rsm->orig_t_space))) { 7680 /* Fix up the orig_m_len and possibly the mbuf offset */ 7681 rack_adjust_orig_mlen(src_rsm); 7682 } 7683 m = src_rsm->m; 7684 soff = src_rsm->soff + (src_rsm->r_end - src_rsm->r_start); 7685 while (soff >= m->m_len) { 7686 /* Move out past this mbuf */ 7687 soff -= m->m_len; 7688 m = m->m_next; 7689 KASSERT((m != NULL), 7690 ("rsm:%p nrsm:%p hit at soff:%u null m", 7691 src_rsm, rsm, soff)); 7692 if (m == NULL) { 7693 /* This should *not* happen which is why there is a kassert */ 7694 src_rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 7695 (src_rsm->r_start - rack->rc_tp->snd_una), 7696 &src_rsm->soff); 7697 src_rsm->orig_m_len = src_rsm->m->m_len; 7698 src_rsm->orig_t_space = M_TRAILINGROOM(src_rsm->m); 7699 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 7700 (rsm->r_start - rack->rc_tp->snd_una), 7701 &rsm->soff); 7702 rsm->orig_m_len = rsm->m->m_len; 7703 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 7704 return; 7705 } 7706 } 7707 rsm->m = m; 7708 rsm->soff = soff; 7709 rsm->orig_m_len = m->m_len; 7710 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 7711 } 7712 7713 static __inline void 7714 rack_clone_rsm(struct tcp_rack *rack, struct rack_sendmap *nrsm, 7715 struct rack_sendmap *rsm, uint32_t start) 7716 { 7717 int idx; 7718 7719 nrsm->r_start = start; 7720 nrsm->r_end = rsm->r_end; 7721 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 7722 nrsm->r_act_rxt_cnt = rsm->r_act_rxt_cnt; 7723 nrsm->r_flags = rsm->r_flags; 7724 nrsm->r_dupack = rsm->r_dupack; 7725 nrsm->r_no_rtt_allowed = rsm->r_no_rtt_allowed; 7726 nrsm->r_rtr_bytes = 0; 7727 nrsm->r_fas = rsm->r_fas; 7728 nrsm->r_bas = rsm->r_bas; 7729 tqhash_update_end(rack->r_ctl.tqh, rsm, nrsm->r_start); 7730 nrsm->r_just_ret = rsm->r_just_ret; 7731 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 7732 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 7733 } 7734 /* Now if we have SYN flag we keep it on the left edge */ 7735 if (nrsm->r_flags & RACK_HAS_SYN) 7736 nrsm->r_flags &= ~RACK_HAS_SYN; 7737 /* Now if we have a FIN flag we keep it on the right edge */ 7738 if (rsm->r_flags & RACK_HAS_FIN) 7739 rsm->r_flags &= ~RACK_HAS_FIN; 7740 /* Push bit must go to the right edge as well */ 7741 if (rsm->r_flags & RACK_HAD_PUSH) 7742 rsm->r_flags &= ~RACK_HAD_PUSH; 7743 /* Clone over the state of the hw_tls flag */ 7744 nrsm->r_hw_tls = rsm->r_hw_tls; 7745 /* 7746 * Now we need to find nrsm's new location in the mbuf chain 7747 * we basically calculate a new offset, which is soff + 7748 * how much is left in original rsm. Then we walk out the mbuf 7749 * chain to find the righ position, it may be the same mbuf 7750 * or maybe not. 7751 */ 7752 KASSERT(((rsm->m != NULL) || 7753 (rsm->r_flags & (RACK_HAS_SYN|RACK_HAS_FIN))), 7754 ("rsm:%p nrsm:%p rack:%p -- rsm->m is NULL?", rsm, nrsm, rack)); 7755 if (rsm->m) 7756 rack_setup_offset_for_rsm(rack, rsm, nrsm); 7757 } 7758 7759 static struct rack_sendmap * 7760 rack_merge_rsm(struct tcp_rack *rack, 7761 struct rack_sendmap *l_rsm, 7762 struct rack_sendmap *r_rsm) 7763 { 7764 /* 7765 * We are merging two ack'd RSM's, 7766 * the l_rsm is on the left (lower seq 7767 * values) and the r_rsm is on the right 7768 * (higher seq value). The simplest way 7769 * to merge these is to move the right 7770 * one into the left. I don't think there 7771 * is any reason we need to try to find 7772 * the oldest (or last oldest retransmitted). 7773 */ 7774 rack_log_map_chg(rack->rc_tp, rack, NULL, 7775 l_rsm, r_rsm, MAP_MERGE, r_rsm->r_end, __LINE__); 7776 tqhash_update_end(rack->r_ctl.tqh, l_rsm, r_rsm->r_end); 7777 if (l_rsm->r_dupack < r_rsm->r_dupack) 7778 l_rsm->r_dupack = r_rsm->r_dupack; 7779 if (r_rsm->r_rtr_bytes) 7780 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 7781 if (r_rsm->r_in_tmap) { 7782 /* This really should not happen */ 7783 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, r_rsm, r_tnext); 7784 r_rsm->r_in_tmap = 0; 7785 } 7786 7787 /* Now the flags */ 7788 if (r_rsm->r_flags & RACK_HAS_FIN) 7789 l_rsm->r_flags |= RACK_HAS_FIN; 7790 if (r_rsm->r_flags & RACK_TLP) 7791 l_rsm->r_flags |= RACK_TLP; 7792 if (r_rsm->r_flags & RACK_RWND_COLLAPSED) 7793 l_rsm->r_flags |= RACK_RWND_COLLAPSED; 7794 if ((r_rsm->r_flags & RACK_APP_LIMITED) && 7795 ((l_rsm->r_flags & RACK_APP_LIMITED) == 0)) { 7796 /* 7797 * If both are app-limited then let the 7798 * free lower the count. If right is app 7799 * limited and left is not, transfer. 7800 */ 7801 l_rsm->r_flags |= RACK_APP_LIMITED; 7802 r_rsm->r_flags &= ~RACK_APP_LIMITED; 7803 if (r_rsm == rack->r_ctl.rc_first_appl) 7804 rack->r_ctl.rc_first_appl = l_rsm; 7805 } 7806 tqhash_remove(rack->r_ctl.tqh, r_rsm, REMOVE_TYPE_MERGE); 7807 /* 7808 * We keep the largest value, which is the newest 7809 * send. We do this in case a segment that is 7810 * joined together and not part of a GP estimate 7811 * later gets expanded into the GP estimate. 7812 * 7813 * We prohibit the merging of unlike kinds i.e. 7814 * all pieces that are in the GP estimate can be 7815 * merged and all pieces that are not in a GP estimate 7816 * can be merged, but not disimilar pieces. Combine 7817 * this with taking the highest here and we should 7818 * be ok unless of course the client reneges. Then 7819 * all bets are off. 7820 */ 7821 if(l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] < 7822 r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]) { 7823 l_rsm->r_tim_lastsent[(l_rsm->r_rtr_cnt-1)] = r_rsm->r_tim_lastsent[(r_rsm->r_rtr_cnt-1)]; 7824 } 7825 /* 7826 * When merging two RSM's we also need to consider the ack time and keep 7827 * newest. If the ack gets merged into a measurement then that is the 7828 * one we will want to be using. 7829 */ 7830 if(l_rsm->r_ack_arrival < r_rsm->r_ack_arrival) 7831 l_rsm->r_ack_arrival = r_rsm->r_ack_arrival; 7832 7833 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 7834 /* Transfer the split limit to the map we free */ 7835 r_rsm->r_limit_type = l_rsm->r_limit_type; 7836 l_rsm->r_limit_type = 0; 7837 } 7838 rack_free(rack, r_rsm); 7839 l_rsm->r_flags |= RACK_MERGED; 7840 return (l_rsm); 7841 } 7842 7843 /* 7844 * TLP Timer, here we simply setup what segment we want to 7845 * have the TLP expire on, the normal rack_output() will then 7846 * send it out. 7847 * 7848 * We return 1, saying don't proceed with rack_output only 7849 * when all timers have been stopped (destroyed PCB?). 7850 */ 7851 static int 7852 rack_timeout_tlp(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t *doing_tlp) 7853 { 7854 /* 7855 * Tail Loss Probe. 7856 */ 7857 struct rack_sendmap *rsm = NULL; 7858 int insret __diagused; 7859 struct socket *so = tptosocket(tp); 7860 uint32_t amm; 7861 uint32_t out, avail; 7862 int collapsed_win = 0; 7863 7864 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 7865 /* Its not time yet */ 7866 return (0); 7867 } 7868 if (ctf_progress_timeout_check(tp, true)) { 7869 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 7870 return (-ETIMEDOUT); /* tcp_drop() */ 7871 } 7872 /* 7873 * A TLP timer has expired. We have been idle for 2 rtts. So we now 7874 * need to figure out how to force a full MSS segment out. 7875 */ 7876 rack_log_to_event(rack, RACK_TO_FRM_TLP, NULL); 7877 rack->r_ctl.retran_during_recovery = 0; 7878 rack->r_might_revert = 0; 7879 rack->r_ctl.dsack_byte_cnt = 0; 7880 counter_u64_add(rack_tlp_tot, 1); 7881 if (rack->r_state && (rack->r_state != tp->t_state)) 7882 rack_set_state(tp, rack); 7883 avail = sbavail(&so->so_snd); 7884 out = tp->snd_max - tp->snd_una; 7885 if ((out > tp->snd_wnd) || rack->rc_has_collapsed) { 7886 /* special case, we need a retransmission */ 7887 collapsed_win = 1; 7888 goto need_retran; 7889 } 7890 if (rack->r_ctl.dsack_persist && (rack->r_ctl.rc_tlp_cnt_out >= 1)) { 7891 rack->r_ctl.dsack_persist--; 7892 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 7893 rack->r_ctl.num_dsack = 0; 7894 } 7895 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 7896 } 7897 if ((tp->t_flags & TF_GPUTINPROG) && 7898 (rack->r_ctl.rc_tlp_cnt_out == 1)) { 7899 /* 7900 * If this is the second in a row 7901 * TLP and we are doing a measurement 7902 * its time to abandon the measurement. 7903 * Something is likely broken on 7904 * the clients network and measuring a 7905 * broken network does us no good. 7906 */ 7907 tp->t_flags &= ~TF_GPUTINPROG; 7908 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 7909 rack->r_ctl.rc_gp_srtt /*flex1*/, 7910 tp->gput_seq, 7911 0, 0, 18, __LINE__, NULL, 0); 7912 } 7913 /* 7914 * Check our send oldest always settings, and if 7915 * there is an oldest to send jump to the need_retran. 7916 */ 7917 if (rack_always_send_oldest && (TAILQ_EMPTY(&rack->r_ctl.rc_tmap) == 0)) 7918 goto need_retran; 7919 7920 if (avail > out) { 7921 /* New data is available */ 7922 amm = avail - out; 7923 if (amm > ctf_fixed_maxseg(tp)) { 7924 amm = ctf_fixed_maxseg(tp); 7925 if ((amm + out) > tp->snd_wnd) { 7926 /* We are rwnd limited */ 7927 goto need_retran; 7928 } 7929 } else if (amm < ctf_fixed_maxseg(tp)) { 7930 /* not enough to fill a MTU */ 7931 goto need_retran; 7932 } 7933 if (IN_FASTRECOVERY(tp->t_flags)) { 7934 /* Unlikely */ 7935 if (rack->rack_no_prr == 0) { 7936 if (out + amm <= tp->snd_wnd) { 7937 rack->r_ctl.rc_prr_sndcnt = amm; 7938 rack->r_ctl.rc_tlp_new_data = amm; 7939 rack_log_to_prr(rack, 4, 0, __LINE__); 7940 } 7941 } else 7942 goto need_retran; 7943 } else { 7944 /* Set the send-new override */ 7945 if (out + amm <= tp->snd_wnd) 7946 rack->r_ctl.rc_tlp_new_data = amm; 7947 else 7948 goto need_retran; 7949 } 7950 rack->r_ctl.rc_tlpsend = NULL; 7951 counter_u64_add(rack_tlp_newdata, 1); 7952 goto send; 7953 } 7954 need_retran: 7955 /* 7956 * Ok we need to arrange the last un-acked segment to be re-sent, or 7957 * optionally the first un-acked segment. 7958 */ 7959 if (collapsed_win == 0) { 7960 if (rack_always_send_oldest) 7961 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 7962 else { 7963 rsm = tqhash_max(rack->r_ctl.tqh); 7964 if (rsm && (rsm->r_flags & (RACK_ACKED | RACK_HAS_FIN))) { 7965 rsm = rack_find_high_nonack(rack, rsm); 7966 } 7967 } 7968 if (rsm == NULL) { 7969 #ifdef TCP_BLACKBOX 7970 tcp_log_dump_tp_logbuf(tp, "nada counter trips", M_NOWAIT, true); 7971 #endif 7972 goto out; 7973 } 7974 } else { 7975 /* 7976 * We had a collapsed window, lets find 7977 * the point before the collapse. 7978 */ 7979 if (SEQ_GT((rack->r_ctl.last_collapse_point - 1), rack->rc_tp->snd_una)) 7980 rsm = tqhash_find(rack->r_ctl.tqh, (rack->r_ctl.last_collapse_point - 1)); 7981 else { 7982 rsm = tqhash_min(rack->r_ctl.tqh); 7983 } 7984 if (rsm == NULL) { 7985 /* Huh */ 7986 goto out; 7987 } 7988 } 7989 if ((rsm->r_end - rsm->r_start) > ctf_fixed_maxseg(tp)) { 7990 /* 7991 * We need to split this the last segment in two. 7992 */ 7993 struct rack_sendmap *nrsm; 7994 7995 nrsm = rack_alloc_full_limit(rack); 7996 if (nrsm == NULL) { 7997 /* 7998 * No memory to split, we will just exit and punt 7999 * off to the RXT timer. 8000 */ 8001 goto out; 8002 } 8003 rack_clone_rsm(rack, nrsm, rsm, 8004 (rsm->r_end - ctf_fixed_maxseg(tp))); 8005 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 8006 #ifndef INVARIANTS 8007 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 8008 #else 8009 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 8010 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 8011 nrsm, insret, rack, rsm); 8012 } 8013 #endif 8014 if (rsm->r_in_tmap) { 8015 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8016 nrsm->r_in_tmap = 1; 8017 } 8018 rsm = nrsm; 8019 } 8020 rack->r_ctl.rc_tlpsend = rsm; 8021 send: 8022 /* Make sure output path knows we are doing a TLP */ 8023 *doing_tlp = 1; 8024 rack->r_timer_override = 1; 8025 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 8026 return (0); 8027 out: 8028 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 8029 return (0); 8030 } 8031 8032 /* 8033 * Delayed ack Timer, here we simply need to setup the 8034 * ACK_NOW flag and remove the DELACK flag. From there 8035 * the output routine will send the ack out. 8036 * 8037 * We only return 1, saying don't proceed, if all timers 8038 * are stopped (destroyed PCB?). 8039 */ 8040 static int 8041 rack_timeout_delack(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 8042 { 8043 8044 rack_log_to_event(rack, RACK_TO_FRM_DELACK, NULL); 8045 tp->t_flags &= ~TF_DELACK; 8046 tp->t_flags |= TF_ACKNOW; 8047 KMOD_TCPSTAT_INC(tcps_delack); 8048 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 8049 return (0); 8050 } 8051 8052 static inline int 8053 rack_send_ack_challange(struct tcp_rack *rack) 8054 { 8055 struct tcptemp *t_template; 8056 8057 t_template = tcpip_maketemplate(rack->rc_inp); 8058 if (t_template) { 8059 if (rack->forced_ack == 0) { 8060 rack->forced_ack = 1; 8061 rack->r_ctl.forced_ack_ts = tcp_get_usecs(NULL); 8062 } else { 8063 rack->probe_not_answered = 1; 8064 } 8065 tcp_respond(rack->rc_tp, t_template->tt_ipgen, 8066 &t_template->tt_t, (struct mbuf *)NULL, 8067 rack->rc_tp->rcv_nxt, rack->rc_tp->snd_una - 1, 0); 8068 free(t_template, M_TEMP); 8069 /* This does send an ack so kill any D-ack timer */ 8070 if (rack->rc_tp->t_flags & TF_DELACK) 8071 rack->rc_tp->t_flags &= ~TF_DELACK; 8072 return(1); 8073 } else 8074 return (0); 8075 8076 } 8077 8078 /* 8079 * Persists timer, here we simply send the 8080 * same thing as a keepalive will. 8081 * the one byte send. 8082 * 8083 * We only return 1, saying don't proceed, if all timers 8084 * are stopped (destroyed PCB?). 8085 */ 8086 static int 8087 rack_timeout_persist(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 8088 { 8089 int32_t retval = 1; 8090 8091 if (rack->rc_in_persist == 0) 8092 return (0); 8093 if (ctf_progress_timeout_check(tp, false)) { 8094 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 8095 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 8096 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends); 8097 return (-ETIMEDOUT); /* tcp_drop() */ 8098 } 8099 /* 8100 * Persistence timer into zero window. Force a byte to be output, if 8101 * possible. 8102 */ 8103 KMOD_TCPSTAT_INC(tcps_persisttimeo); 8104 /* 8105 * Hack: if the peer is dead/unreachable, we do not time out if the 8106 * window is closed. After a full backoff, drop the connection if 8107 * the idle time (no responses to probes) reaches the maximum 8108 * backoff that we would use if retransmitting. 8109 */ 8110 if (tp->t_rxtshift >= V_tcp_retries && 8111 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 8112 TICKS_2_USEC(ticks - tp->t_rcvtime) >= RACK_REXMTVAL(tp) * tcp_totbackoff)) { 8113 KMOD_TCPSTAT_INC(tcps_persistdrop); 8114 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 8115 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends); 8116 retval = -ETIMEDOUT; /* tcp_drop() */ 8117 goto out; 8118 } 8119 if ((sbavail(&rack->rc_inp->inp_socket->so_snd) == 0) && 8120 tp->snd_una == tp->snd_max) 8121 rack_exit_persist(tp, rack, cts); 8122 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 8123 /* 8124 * If the user has closed the socket then drop a persisting 8125 * connection after a much reduced timeout. 8126 */ 8127 if (tp->t_state > TCPS_CLOSE_WAIT && 8128 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 8129 KMOD_TCPSTAT_INC(tcps_persistdrop); 8130 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 8131 counter_u64_add(rack_persists_lost_ends, rack->r_ctl.persist_lost_ends); 8132 retval = -ETIMEDOUT; /* tcp_drop() */ 8133 goto out; 8134 } 8135 if (rack_send_ack_challange(rack)) { 8136 /* only set it if we were answered */ 8137 if (rack->probe_not_answered) { 8138 counter_u64_add(rack_persists_loss, 1); 8139 rack->r_ctl.persist_lost_ends++; 8140 } 8141 counter_u64_add(rack_persists_sends, 1); 8142 counter_u64_add(rack_out_size[TCP_MSS_ACCT_PERSIST], 1); 8143 } 8144 if (tp->t_rxtshift < V_tcp_retries) 8145 tp->t_rxtshift++; 8146 out: 8147 rack_log_to_event(rack, RACK_TO_FRM_PERSIST, NULL); 8148 rack_start_hpts_timer(rack, tp, cts, 8149 0, 0, 0); 8150 return (retval); 8151 } 8152 8153 /* 8154 * If a keepalive goes off, we had no other timers 8155 * happening. We always return 1 here since this 8156 * routine either drops the connection or sends 8157 * out a segment with respond. 8158 */ 8159 static int 8160 rack_timeout_keepalive(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 8161 { 8162 struct inpcb *inp = tptoinpcb(tp); 8163 8164 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 8165 rack_log_to_event(rack, RACK_TO_FRM_KEEP, NULL); 8166 /* 8167 * Keep-alive timer went off; send something or drop connection if 8168 * idle for too long. 8169 */ 8170 KMOD_TCPSTAT_INC(tcps_keeptimeo); 8171 if (tp->t_state < TCPS_ESTABLISHED) 8172 goto dropit; 8173 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 8174 tp->t_state <= TCPS_CLOSING) { 8175 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 8176 goto dropit; 8177 /* 8178 * Send a packet designed to force a response if the peer is 8179 * up and reachable: either an ACK if the connection is 8180 * still alive, or an RST if the peer has closed the 8181 * connection due to timeout or reboot. Using sequence 8182 * number tp->snd_una-1 causes the transmitted zero-length 8183 * segment to lie outside the receive window; by the 8184 * protocol spec, this requires the correspondent TCP to 8185 * respond. 8186 */ 8187 KMOD_TCPSTAT_INC(tcps_keepprobe); 8188 rack_send_ack_challange(rack); 8189 } 8190 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 8191 return (1); 8192 dropit: 8193 KMOD_TCPSTAT_INC(tcps_keepdrops); 8194 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 8195 return (-ETIMEDOUT); /* tcp_drop() */ 8196 } 8197 8198 /* 8199 * Retransmit helper function, clear up all the ack 8200 * flags and take care of important book keeping. 8201 */ 8202 static void 8203 rack_remxt_tmr(struct tcpcb *tp) 8204 { 8205 /* 8206 * The retransmit timer went off, all sack'd blocks must be 8207 * un-acked. 8208 */ 8209 struct rack_sendmap *rsm, *trsm = NULL; 8210 struct tcp_rack *rack; 8211 8212 rack = (struct tcp_rack *)tp->t_fb_ptr; 8213 rack_timer_cancel(tp, rack, tcp_get_usecs(NULL), __LINE__); 8214 rack_log_to_event(rack, RACK_TO_FRM_TMR, NULL); 8215 rack->r_timer_override = 1; 8216 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max; 8217 rack->r_ctl.rc_last_timeout_snduna = tp->snd_una; 8218 rack->r_late = 0; 8219 rack->r_early = 0; 8220 rack->r_ctl.rc_agg_delayed = 0; 8221 rack->r_ctl.rc_agg_early = 0; 8222 if (rack->r_state && (rack->r_state != tp->t_state)) 8223 rack_set_state(tp, rack); 8224 if (tp->t_rxtshift <= rack_rxt_scoreboard_clear_thresh) { 8225 /* 8226 * We do not clear the scoreboard until we have had 8227 * more than rack_rxt_scoreboard_clear_thresh time-outs. 8228 */ 8229 rack->r_ctl.rc_resend = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 8230 if (rack->r_ctl.rc_resend != NULL) 8231 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT; 8232 8233 return; 8234 } 8235 /* 8236 * Ideally we would like to be able to 8237 * mark SACK-PASS on anything not acked here. 8238 * 8239 * However, if we do that we would burst out 8240 * all that data 1ms apart. This would be unwise, 8241 * so for now we will just let the normal rxt timer 8242 * and tlp timer take care of it. 8243 * 8244 * Also we really need to stick them back in sequence 8245 * order. This way we send in the proper order and any 8246 * sacks that come floating in will "re-ack" the data. 8247 * To do this we zap the tmap with an INIT and then 8248 * walk through and place every rsm in the tail queue 8249 * hash table back in its seq ordered place. 8250 */ 8251 TAILQ_INIT(&rack->r_ctl.rc_tmap); 8252 8253 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) { 8254 rsm->r_dupack = 0; 8255 if (rack_verbose_logging) 8256 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 8257 /* We must re-add it back to the tlist */ 8258 if (trsm == NULL) { 8259 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8260 } else { 8261 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext); 8262 } 8263 rsm->r_in_tmap = 1; 8264 trsm = rsm; 8265 if (rsm->r_flags & RACK_ACKED) 8266 rsm->r_flags |= RACK_WAS_ACKED; 8267 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED | RACK_WAS_LOST); 8268 rsm->r_flags |= RACK_MUST_RXT; 8269 } 8270 /* zero the lost since it's all gone */ 8271 rack->r_ctl.rc_considered_lost = 0; 8272 /* Clear the count (we just un-acked them) */ 8273 rack->r_ctl.rc_sacked = 0; 8274 rack->r_ctl.rc_sacklast = NULL; 8275 /* Clear the tlp rtx mark */ 8276 rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh); 8277 if (rack->r_ctl.rc_resend != NULL) 8278 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT; 8279 rack->r_ctl.rc_prr_sndcnt = 0; 8280 rack_log_to_prr(rack, 6, 0, __LINE__); 8281 rack->r_ctl.rc_resend = tqhash_min(rack->r_ctl.tqh); 8282 if (rack->r_ctl.rc_resend != NULL) 8283 rack->r_ctl.rc_resend->r_flags |= RACK_TO_REXT; 8284 if ((((tp->t_flags & TF_SACK_PERMIT) == 0) 8285 #ifdef TCP_SAD_DETECTION 8286 || (rack->sack_attack_disable != 0) 8287 #endif 8288 ) && ((tp->t_flags & TF_SENTFIN) == 0)) { 8289 /* 8290 * For non-sack customers new data 8291 * needs to go out as retransmits until 8292 * we retransmit up to snd_max. 8293 */ 8294 rack->r_must_retran = 1; 8295 rack->r_ctl.rc_out_at_rto = ctf_flight_size(rack->rc_tp, 8296 rack->r_ctl.rc_sacked); 8297 } 8298 } 8299 8300 static void 8301 rack_convert_rtts(struct tcpcb *tp) 8302 { 8303 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC); 8304 tp->t_rxtcur = RACK_REXMTVAL(tp); 8305 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 8306 tp->t_rxtcur += TICKS_2_USEC(tcp_rexmit_slop); 8307 } 8308 if (tp->t_rxtcur > rack_rto_max) { 8309 tp->t_rxtcur = rack_rto_max; 8310 } 8311 } 8312 8313 static void 8314 rack_cc_conn_init(struct tcpcb *tp) 8315 { 8316 struct tcp_rack *rack; 8317 uint32_t srtt; 8318 8319 rack = (struct tcp_rack *)tp->t_fb_ptr; 8320 srtt = tp->t_srtt; 8321 cc_conn_init(tp); 8322 /* 8323 * Now convert to rack's internal format, 8324 * if required. 8325 */ 8326 if ((srtt == 0) && (tp->t_srtt != 0)) 8327 rack_convert_rtts(tp); 8328 /* 8329 * We want a chance to stay in slowstart as 8330 * we create a connection. TCP spec says that 8331 * initially ssthresh is infinite. For our 8332 * purposes that is the snd_wnd. 8333 */ 8334 if (tp->snd_ssthresh < tp->snd_wnd) { 8335 tp->snd_ssthresh = tp->snd_wnd; 8336 } 8337 /* 8338 * We also want to assure a IW worth of 8339 * data can get inflight. 8340 */ 8341 if (rc_init_window(rack) < tp->snd_cwnd) 8342 tp->snd_cwnd = rc_init_window(rack); 8343 } 8344 8345 /* 8346 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 8347 * we will setup to retransmit the lowest seq number outstanding. 8348 */ 8349 static int 8350 rack_timeout_rxt(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts) 8351 { 8352 struct inpcb *inp = tptoinpcb(tp); 8353 int32_t rexmt; 8354 int32_t retval = 0; 8355 bool isipv6; 8356 8357 if ((tp->t_flags & TF_GPUTINPROG) && 8358 (tp->t_rxtshift)) { 8359 /* 8360 * We have had a second timeout 8361 * measurements on successive rxt's are not profitable. 8362 * It is unlikely to be of any use (the network is 8363 * broken or the client went away). 8364 */ 8365 tp->t_flags &= ~TF_GPUTINPROG; 8366 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 8367 rack->r_ctl.rc_gp_srtt /*flex1*/, 8368 tp->gput_seq, 8369 0, 0, 18, __LINE__, NULL, 0); 8370 } 8371 if (ctf_progress_timeout_check(tp, false)) { 8372 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 8373 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 8374 return (-ETIMEDOUT); /* tcp_drop() */ 8375 } 8376 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 8377 rack->r_ctl.retran_during_recovery = 0; 8378 rack->rc_ack_required = 1; 8379 rack->r_ctl.dsack_byte_cnt = 0; 8380 if (IN_RECOVERY(tp->t_flags) && 8381 (rack->rto_from_rec == 0)) { 8382 /* 8383 * Mark that we had a rto while in recovery 8384 * and save the ssthresh so if we go back 8385 * into recovery we will have a chance 8386 * to slowstart back to the level. 8387 */ 8388 rack->rto_from_rec = 1; 8389 rack->r_ctl.rto_ssthresh = tp->snd_ssthresh; 8390 } 8391 if (IN_FASTRECOVERY(tp->t_flags)) 8392 tp->t_flags |= TF_WASFRECOVERY; 8393 else 8394 tp->t_flags &= ~TF_WASFRECOVERY; 8395 if (IN_CONGRECOVERY(tp->t_flags)) 8396 tp->t_flags |= TF_WASCRECOVERY; 8397 else 8398 tp->t_flags &= ~TF_WASCRECOVERY; 8399 if (TCPS_HAVEESTABLISHED(tp->t_state) && 8400 (tp->snd_una == tp->snd_max)) { 8401 /* Nothing outstanding .. nothing to do */ 8402 return (0); 8403 } 8404 if (rack->r_ctl.dsack_persist) { 8405 rack->r_ctl.dsack_persist--; 8406 if (rack->r_ctl.num_dsack && (rack->r_ctl.dsack_persist == 0)) { 8407 rack->r_ctl.num_dsack = 0; 8408 } 8409 rack_log_dsack_event(rack, 1, __LINE__, 0, 0); 8410 } 8411 /* 8412 * Rack can only run one timer at a time, so we cannot 8413 * run a KEEPINIT (gating SYN sending) and a retransmit 8414 * timer for the SYN. So if we are in a front state and 8415 * have a KEEPINIT timer we need to check the first transmit 8416 * against now to see if we have exceeded the KEEPINIT time 8417 * (if one is set). 8418 */ 8419 if ((TCPS_HAVEESTABLISHED(tp->t_state) == 0) && 8420 (TP_KEEPINIT(tp) != 0)) { 8421 struct rack_sendmap *rsm; 8422 8423 rsm = tqhash_min(rack->r_ctl.tqh); 8424 if (rsm) { 8425 /* Ok we have something outstanding to test keepinit with */ 8426 if ((TSTMP_GT(cts, (uint32_t)rsm->r_tim_lastsent[0])) && 8427 ((cts - (uint32_t)rsm->r_tim_lastsent[0]) >= TICKS_2_USEC(TP_KEEPINIT(tp)))) { 8428 /* We have exceeded the KEEPINIT time */ 8429 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 8430 goto drop_it; 8431 } 8432 } 8433 } 8434 /* 8435 * Retransmission timer went off. Message has not been acked within 8436 * retransmit interval. Back off to a longer retransmit interval 8437 * and retransmit one segment. 8438 */ 8439 if ((rack->r_ctl.rc_resend == NULL) || 8440 ((rack->r_ctl.rc_resend->r_flags & RACK_RWND_COLLAPSED) == 0)) { 8441 /* 8442 * If the rwnd collapsed on 8443 * the one we are retransmitting 8444 * it does not count against the 8445 * rxt count. 8446 */ 8447 tp->t_rxtshift++; 8448 } 8449 rack_remxt_tmr(tp); 8450 if (tp->t_rxtshift > V_tcp_retries) { 8451 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 8452 drop_it: 8453 tp->t_rxtshift = V_tcp_retries; 8454 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 8455 /* XXXGL: previously t_softerror was casted to uint16_t */ 8456 MPASS(tp->t_softerror >= 0); 8457 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 8458 goto out; /* tcp_drop() */ 8459 } 8460 if (tp->t_state == TCPS_SYN_SENT) { 8461 /* 8462 * If the SYN was retransmitted, indicate CWND to be limited 8463 * to 1 segment in cc_conn_init(). 8464 */ 8465 tp->snd_cwnd = 1; 8466 } else if (tp->t_rxtshift == 1) { 8467 /* 8468 * first retransmit; record ssthresh and cwnd so they can be 8469 * recovered if this turns out to be a "bad" retransmit. A 8470 * retransmit is considered "bad" if an ACK for this segment 8471 * is received within RTT/2 interval; the assumption here is 8472 * that the ACK was already in flight. See "On Estimating 8473 * End-to-End Network Path Properties" by Allman and Paxson 8474 * for more details. 8475 */ 8476 tp->snd_cwnd_prev = tp->snd_cwnd; 8477 tp->snd_ssthresh_prev = tp->snd_ssthresh; 8478 tp->snd_recover_prev = tp->snd_recover; 8479 tp->t_badrxtwin = ticks + (USEC_2_TICKS(tp->t_srtt)/2); 8480 tp->t_flags |= TF_PREVVALID; 8481 } else if ((tp->t_flags & TF_RCVD_TSTMP) == 0) 8482 tp->t_flags &= ~TF_PREVVALID; 8483 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 8484 if ((tp->t_state == TCPS_SYN_SENT) || 8485 (tp->t_state == TCPS_SYN_RECEIVED)) 8486 rexmt = RACK_INITIAL_RTO * tcp_backoff[tp->t_rxtshift]; 8487 else 8488 rexmt = max(rack_rto_min, (tp->t_srtt + (tp->t_rttvar << 2))) * tcp_backoff[tp->t_rxtshift]; 8489 8490 RACK_TCPT_RANGESET(tp->t_rxtcur, rexmt, 8491 max(rack_rto_min, rexmt), rack_rto_max, rack->r_ctl.timer_slop); 8492 /* 8493 * We enter the path for PLMTUD if connection is established or, if 8494 * connection is FIN_WAIT_1 status, reason for the last is that if 8495 * amount of data we send is very small, we could send it in couple 8496 * of packets and process straight to FIN. In that case we won't 8497 * catch ESTABLISHED state. 8498 */ 8499 #ifdef INET6 8500 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 8501 #else 8502 isipv6 = false; 8503 #endif 8504 if (((V_tcp_pmtud_blackhole_detect == 1) || 8505 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 8506 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 8507 ((tp->t_state == TCPS_ESTABLISHED) || 8508 (tp->t_state == TCPS_FIN_WAIT_1))) { 8509 /* 8510 * Idea here is that at each stage of mtu probe (usually, 8511 * 1448 -> 1188 -> 524) should be given 2 chances to recover 8512 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 8513 * should take care of that. 8514 */ 8515 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 8516 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 8517 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 8518 tp->t_rxtshift % 2 == 0)) { 8519 /* 8520 * Enter Path MTU Black-hole Detection mechanism: - 8521 * Disable Path MTU Discovery (IP "DF" bit). - 8522 * Reduce MTU to lower value than what we negotiated 8523 * with peer. 8524 */ 8525 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 8526 /* Record that we may have found a black hole. */ 8527 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 8528 /* Keep track of previous MSS. */ 8529 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 8530 } 8531 8532 /* 8533 * Reduce the MSS to blackhole value or to the 8534 * default in an attempt to retransmit. 8535 */ 8536 #ifdef INET6 8537 if (isipv6 && 8538 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 8539 /* Use the sysctl tuneable blackhole MSS. */ 8540 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 8541 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 8542 } else if (isipv6) { 8543 /* Use the default MSS. */ 8544 tp->t_maxseg = V_tcp_v6mssdflt; 8545 /* 8546 * Disable Path MTU Discovery when we switch 8547 * to minmss. 8548 */ 8549 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8550 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 8551 } 8552 #endif 8553 #if defined(INET6) && defined(INET) 8554 else 8555 #endif 8556 #ifdef INET 8557 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 8558 /* Use the sysctl tuneable blackhole MSS. */ 8559 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 8560 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 8561 } else { 8562 /* Use the default MSS. */ 8563 tp->t_maxseg = V_tcp_mssdflt; 8564 /* 8565 * Disable Path MTU Discovery when we switch 8566 * to minmss. 8567 */ 8568 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 8569 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 8570 } 8571 #endif 8572 } else { 8573 /* 8574 * If further retransmissions are still unsuccessful 8575 * with a lowered MTU, maybe this isn't a blackhole 8576 * and we restore the previous MSS and blackhole 8577 * detection flags. The limit '6' is determined by 8578 * giving each probe stage (1448, 1188, 524) 2 8579 * chances to recover. 8580 */ 8581 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 8582 (tp->t_rxtshift >= 6)) { 8583 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 8584 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 8585 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 8586 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 8587 } 8588 } 8589 } 8590 /* 8591 * Disable RFC1323 and SACK if we haven't got any response to 8592 * our third SYN to work-around some broken terminal servers 8593 * (most of which have hopefully been retired) that have bad VJ 8594 * header compression code which trashes TCP segments containing 8595 * unknown-to-them TCP options. 8596 */ 8597 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 8598 (tp->t_rxtshift == 3)) 8599 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT); 8600 /* 8601 * If we backed off this far, our srtt estimate is probably bogus. 8602 * Clobber it so we'll take the next rtt measurement as our srtt; 8603 * move the current srtt into rttvar to keep the current retransmit 8604 * times until then. 8605 */ 8606 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 8607 #ifdef INET6 8608 if ((inp->inp_vflag & INP_IPV6) != 0) 8609 in6_losing(inp); 8610 else 8611 #endif 8612 in_losing(inp); 8613 tp->t_rttvar += tp->t_srtt; 8614 tp->t_srtt = 0; 8615 } 8616 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 8617 tp->snd_recover = tp->snd_max; 8618 tp->t_flags |= TF_ACKNOW; 8619 tp->t_rtttime = 0; 8620 rack_cong_signal(tp, CC_RTO, tp->snd_una, __LINE__); 8621 out: 8622 return (retval); 8623 } 8624 8625 static int 8626 rack_process_timers(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, uint8_t hpts_calling, uint8_t *doing_tlp) 8627 { 8628 int32_t ret = 0; 8629 int32_t timers = (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 8630 8631 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 8632 (tp->t_flags & TF_GPUTINPROG)) { 8633 /* 8634 * We have a goodput in progress 8635 * and we have entered a late state. 8636 * Do we have enough data in the sb 8637 * to handle the GPUT request? 8638 */ 8639 uint32_t bytes; 8640 8641 bytes = tp->gput_ack - tp->gput_seq; 8642 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 8643 bytes += tp->gput_seq - tp->snd_una; 8644 if (bytes > sbavail(&tptosocket(tp)->so_snd)) { 8645 /* 8646 * There are not enough bytes in the socket 8647 * buffer that have been sent to cover this 8648 * measurement. Cancel it. 8649 */ 8650 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 8651 rack->r_ctl.rc_gp_srtt /*flex1*/, 8652 tp->gput_seq, 8653 0, 0, 18, __LINE__, NULL, 0); 8654 tp->t_flags &= ~TF_GPUTINPROG; 8655 } 8656 } 8657 if (timers == 0) { 8658 return (0); 8659 } 8660 if (tp->t_state == TCPS_LISTEN) { 8661 /* no timers on listen sockets */ 8662 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 8663 return (0); 8664 return (1); 8665 } 8666 if ((timers & PACE_TMR_RACK) && 8667 rack->rc_on_min_to) { 8668 /* 8669 * For the rack timer when we 8670 * are on a min-timeout (which means rrr_conf = 3) 8671 * we don't want to check the timer. It may 8672 * be going off for a pace and thats ok we 8673 * want to send the retransmit (if its ready). 8674 * 8675 * If its on a normal rack timer (non-min) then 8676 * we will check if its expired. 8677 */ 8678 goto skip_time_check; 8679 } 8680 if (TSTMP_LT(cts, rack->r_ctl.rc_timer_exp)) { 8681 uint32_t left; 8682 8683 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 8684 ret = -1; 8685 rack_log_to_processing(rack, cts, ret, 0); 8686 return (0); 8687 } 8688 if (hpts_calling == 0) { 8689 /* 8690 * A user send or queued mbuf (sack) has called us? We 8691 * return 0 and let the pacing guards 8692 * deal with it if they should or 8693 * should not cause a send. 8694 */ 8695 ret = -2; 8696 rack_log_to_processing(rack, cts, ret, 0); 8697 return (0); 8698 } 8699 /* 8700 * Ok our timer went off early and we are not paced false 8701 * alarm, go back to sleep. We make sure we don't have 8702 * no-sack wakeup on since we no longer have a PKT_OUTPUT 8703 * flag in place. 8704 */ 8705 rack->rc_tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 8706 ret = -3; 8707 left = rack->r_ctl.rc_timer_exp - cts; 8708 tcp_hpts_insert(tp, HPTS_MS_TO_SLOTS(left)); 8709 rack_log_to_processing(rack, cts, ret, left); 8710 return (1); 8711 } 8712 skip_time_check: 8713 rack->rc_tmr_stopped = 0; 8714 rack->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 8715 if (timers & PACE_TMR_DELACK) { 8716 ret = rack_timeout_delack(tp, rack, cts); 8717 } else if (timers & PACE_TMR_RACK) { 8718 rack->r_ctl.rc_tlp_rxt_last_time = cts; 8719 rack->r_fast_output = 0; 8720 ret = rack_timeout_rack(tp, rack, cts); 8721 } else if (timers & PACE_TMR_TLP) { 8722 rack->r_ctl.rc_tlp_rxt_last_time = cts; 8723 ret = rack_timeout_tlp(tp, rack, cts, doing_tlp); 8724 } else if (timers & PACE_TMR_RXT) { 8725 rack->r_ctl.rc_tlp_rxt_last_time = cts; 8726 rack->r_fast_output = 0; 8727 ret = rack_timeout_rxt(tp, rack, cts); 8728 } else if (timers & PACE_TMR_PERSIT) { 8729 ret = rack_timeout_persist(tp, rack, cts); 8730 } else if (timers & PACE_TMR_KEEP) { 8731 ret = rack_timeout_keepalive(tp, rack, cts); 8732 } 8733 rack_log_to_processing(rack, cts, ret, timers); 8734 return (ret); 8735 } 8736 8737 static void 8738 rack_timer_cancel(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cts, int line) 8739 { 8740 struct timeval tv; 8741 uint32_t us_cts, flags_on_entry; 8742 uint8_t hpts_removed = 0; 8743 8744 flags_on_entry = rack->r_ctl.rc_hpts_flags; 8745 us_cts = tcp_get_usecs(&tv); 8746 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 8747 ((TSTMP_GEQ(us_cts, rack->r_ctl.rc_last_output_to)) || 8748 ((tp->snd_max - tp->snd_una) == 0))) { 8749 tcp_hpts_remove(rack->rc_tp); 8750 hpts_removed = 1; 8751 /* If we were not delayed cancel out the flag. */ 8752 if ((tp->snd_max - tp->snd_una) == 0) 8753 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 8754 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry); 8755 } 8756 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 8757 rack->rc_tmr_stopped = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 8758 if (tcp_in_hpts(rack->rc_tp) && 8759 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)) { 8760 /* 8761 * Canceling timer's when we have no output being 8762 * paced. We also must remove ourselves from the 8763 * hpts. 8764 */ 8765 tcp_hpts_remove(rack->rc_tp); 8766 hpts_removed = 1; 8767 } 8768 rack->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 8769 } 8770 if (hpts_removed == 0) 8771 rack_log_to_cancel(rack, hpts_removed, line, us_cts, &tv, flags_on_entry); 8772 } 8773 8774 static int 8775 rack_stopall(struct tcpcb *tp) 8776 { 8777 struct tcp_rack *rack; 8778 8779 rack = (struct tcp_rack *)tp->t_fb_ptr; 8780 rack->t_timers_stopped = 1; 8781 8782 tcp_hpts_remove(tp); 8783 8784 return (0); 8785 } 8786 8787 static void 8788 rack_stop_all_timers(struct tcpcb *tp, struct tcp_rack *rack) 8789 { 8790 /* 8791 * Assure no timers are running. 8792 */ 8793 if (tcp_timer_active(tp, TT_PERSIST)) { 8794 /* We enter in persists, set the flag appropriately */ 8795 rack->rc_in_persist = 1; 8796 } 8797 if (tcp_in_hpts(rack->rc_tp)) { 8798 tcp_hpts_remove(rack->rc_tp); 8799 } 8800 } 8801 8802 /* 8803 * We maintain an array fo 16 (RETRAN_CNT_SIZE) entries. This 8804 * array is zeroed at the start of recovery. Each time a segment 8805 * is retransmitted, we translate that into a number of packets 8806 * (based on segsiz) and based on how many times its been retransmitted 8807 * increment by the number of packets the counter that represents 8808 * retansmitted N times. Index 0 is retransmitted 1 time, index 1 8809 * is retransmitted 2 times etc. 8810 * 8811 * So for example when we send a 4344 byte transmission with a 1448 8812 * byte segsize, and its the third time we have retransmitted this 8813 * segment, we would add to the rc_cnt_of_retran[2] the value of 8814 * 3. That represents 3 MSS were retransmitted 3 times (index is 8815 * the number of times retranmitted minus 1). 8816 */ 8817 static void 8818 rack_peg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz) 8819 { 8820 int idx; 8821 uint32_t peg; 8822 8823 peg = ((rsm->r_end - rsm->r_start) + segsiz) - 1; 8824 peg /= segsiz; 8825 idx = rsm->r_act_rxt_cnt - 1; 8826 if (idx >= RETRAN_CNT_SIZE) 8827 idx = RETRAN_CNT_SIZE - 1; 8828 /* Max of a uint16_t retransmits in a bucket */ 8829 if ((rack->r_ctl.rc_cnt_of_retran[idx] + peg) < 0xffff) 8830 rack->r_ctl.rc_cnt_of_retran[idx] += peg; 8831 else 8832 rack->r_ctl.rc_cnt_of_retran[idx] = 0xffff; 8833 } 8834 8835 /* 8836 * We maintain an array fo 16 (RETRAN_CNT_SIZE) entries. This 8837 * array is zeroed at the start of recovery. Each time a segment 8838 * is retransmitted, we translate that into a number of packets 8839 * (based on segsiz) and based on how many times its been retransmitted 8840 * increment by the number of packets the counter that represents 8841 * retansmitted N times. Index 0 is retransmitted 1 time, index 1 8842 * is retransmitted 2 times etc. 8843 * 8844 * The rack_unpeg_rxt is used when we go to retransmit a segment 8845 * again. Basically if the segment had previously been retransmitted 8846 * say 3 times (as our previous example illustrated in the comment 8847 * above rack_peg_rxt() prior to calling that and incrementing 8848 * r_ack_rxt_cnt we would have called rack_unpeg_rxt() that would 8849 * subtract back the previous add from its last rxt (in this 8850 * example r_act_cnt would have been 2 for 2 retransmissions. So 8851 * we would have subtracted 3 from rc_cnt_of_reetran[1] to remove 8852 * those 3 segments. You will see this in the rack_update_rsm() 8853 * below where we do: 8854 * if (rsm->r_act_rxt_cnt > 0) { 8855 * rack_unpeg_rxt(rack, rsm, segsiz); 8856 * } 8857 * rsm->r_act_rxt_cnt++; 8858 * rack_peg_rxt(rack, rsm, segsiz); 8859 * 8860 * This effectively moves the count from rc_cnt_of_retran[1] to 8861 * rc_cnt_of_retran[2]. 8862 */ 8863 static void 8864 rack_unpeg_rxt(struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t segsiz) 8865 { 8866 int idx; 8867 uint32_t peg; 8868 8869 idx = rsm->r_act_rxt_cnt - 1; 8870 if (idx >= RETRAN_CNT_SIZE) 8871 idx = RETRAN_CNT_SIZE - 1; 8872 peg = ((rsm->r_end - rsm->r_start) + segsiz) - 1; 8873 peg /= segsiz; 8874 if (peg < rack->r_ctl.rc_cnt_of_retran[idx]) 8875 rack->r_ctl.rc_cnt_of_retran[idx] -= peg; 8876 else { 8877 /* TSNH */ 8878 rack->r_ctl.rc_cnt_of_retran[idx] = 0; 8879 } 8880 } 8881 8882 static void 8883 rack_update_rsm(struct tcpcb *tp, struct tcp_rack *rack, 8884 struct rack_sendmap *rsm, uint64_t ts, uint32_t add_flag, int segsiz) 8885 { 8886 int32_t idx; 8887 8888 rsm->r_rtr_cnt++; 8889 if (rsm->r_rtr_cnt > RACK_NUM_OF_RETRANS) { 8890 rsm->r_rtr_cnt = RACK_NUM_OF_RETRANS; 8891 rsm->r_flags |= RACK_OVERMAX; 8892 } 8893 if (rsm->r_act_rxt_cnt > 0) { 8894 /* Drop the count back for this, its retransmitting again */ 8895 rack_unpeg_rxt(rack, rsm, segsiz); 8896 } 8897 rsm->r_act_rxt_cnt++; 8898 /* Peg the count/index */ 8899 rack_peg_rxt(rack, rsm, segsiz); 8900 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 8901 rsm->r_dupack = 0; 8902 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & RACK_TLP) == 0)) { 8903 rack->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 8904 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 8905 } 8906 if (rsm->r_flags & RACK_WAS_LOST) { 8907 /* 8908 * We retransmitted it putting it back in flight 8909 * remove the lost desgination and reduce the 8910 * bytes considered lost. 8911 */ 8912 rsm->r_flags &= ~RACK_WAS_LOST; 8913 KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)), 8914 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 8915 if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)) 8916 rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start; 8917 else 8918 rack->r_ctl.rc_considered_lost = 0; 8919 } 8920 idx = rsm->r_rtr_cnt - 1; 8921 rsm->r_tim_lastsent[idx] = ts; 8922 /* 8923 * Here we don't add in the len of send, since its already 8924 * in snduna <->snd_max. 8925 */ 8926 rsm->r_fas = ctf_flight_size(rack->rc_tp, 8927 rack->r_ctl.rc_sacked); 8928 if (rsm->r_flags & RACK_ACKED) { 8929 /* Problably MTU discovery messing with us */ 8930 rsm->r_flags &= ~RACK_ACKED; 8931 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 8932 } 8933 if (rsm->r_in_tmap) { 8934 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8935 rsm->r_in_tmap = 0; 8936 } 8937 /* Lets make sure it really is in or not the GP window */ 8938 rack_mark_in_gp_win(tp, rsm); 8939 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 8940 rsm->r_in_tmap = 1; 8941 rsm->r_bas = (uint8_t)(((rsm->r_end - rsm->r_start) + segsiz - 1) / segsiz); 8942 /* Take off the must retransmit flag, if its on */ 8943 if (rsm->r_flags & RACK_MUST_RXT) { 8944 if (rack->r_must_retran) 8945 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start); 8946 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) { 8947 /* 8948 * We have retransmitted all we need. Clear 8949 * any must retransmit flags. 8950 */ 8951 rack->r_must_retran = 0; 8952 rack->r_ctl.rc_out_at_rto = 0; 8953 } 8954 rsm->r_flags &= ~RACK_MUST_RXT; 8955 } 8956 /* Remove any collapsed flag */ 8957 rsm->r_flags &= ~RACK_RWND_COLLAPSED; 8958 if (rsm->r_flags & RACK_SACK_PASSED) { 8959 /* We have retransmitted due to the SACK pass */ 8960 rsm->r_flags &= ~RACK_SACK_PASSED; 8961 rsm->r_flags |= RACK_WAS_SACKPASS; 8962 } 8963 } 8964 8965 static uint32_t 8966 rack_update_entry(struct tcpcb *tp, struct tcp_rack *rack, 8967 struct rack_sendmap *rsm, uint64_t ts, int32_t *lenp, uint32_t add_flag, int segsiz) 8968 { 8969 /* 8970 * We (re-)transmitted starting at rsm->r_start for some length 8971 * (possibly less than r_end. 8972 */ 8973 struct rack_sendmap *nrsm; 8974 int insret __diagused; 8975 uint32_t c_end; 8976 int32_t len; 8977 8978 len = *lenp; 8979 c_end = rsm->r_start + len; 8980 if (SEQ_GEQ(c_end, rsm->r_end)) { 8981 /* 8982 * We retransmitted the whole piece or more than the whole 8983 * slopping into the next rsm. 8984 */ 8985 rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz); 8986 if (c_end == rsm->r_end) { 8987 *lenp = 0; 8988 return (0); 8989 } else { 8990 int32_t act_len; 8991 8992 /* Hangs over the end return whats left */ 8993 act_len = rsm->r_end - rsm->r_start; 8994 *lenp = (len - act_len); 8995 return (rsm->r_end); 8996 } 8997 /* We don't get out of this block. */ 8998 } 8999 /* 9000 * Here we retransmitted less than the whole thing which means we 9001 * have to split this into what was transmitted and what was not. 9002 */ 9003 nrsm = rack_alloc_full_limit(rack); 9004 if (nrsm == NULL) { 9005 /* 9006 * We can't get memory, so lets not proceed. 9007 */ 9008 *lenp = 0; 9009 return (0); 9010 } 9011 /* 9012 * So here we are going to take the original rsm and make it what we 9013 * retransmitted. nrsm will be the tail portion we did not 9014 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 9015 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 9016 * 1, 6 and the new piece will be 6, 11. 9017 */ 9018 rack_clone_rsm(rack, nrsm, rsm, c_end); 9019 nrsm->r_dupack = 0; 9020 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2); 9021 #ifndef INVARIANTS 9022 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 9023 #else 9024 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 9025 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 9026 nrsm, insret, rack, rsm); 9027 } 9028 #endif 9029 if (rsm->r_in_tmap) { 9030 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 9031 nrsm->r_in_tmap = 1; 9032 } 9033 rsm->r_flags &= (~RACK_HAS_FIN); 9034 rack_update_rsm(tp, rack, rsm, ts, add_flag, segsiz); 9035 /* Log a split of rsm into rsm and nrsm */ 9036 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 9037 *lenp = 0; 9038 return (0); 9039 } 9040 9041 static void 9042 rack_log_output(struct tcpcb *tp, struct tcpopt *to, int32_t len, 9043 uint32_t seq_out, uint16_t th_flags, int32_t err, uint64_t cts, 9044 struct rack_sendmap *hintrsm, uint32_t add_flag, struct mbuf *s_mb, 9045 uint32_t s_moff, int hw_tls, int segsiz) 9046 { 9047 struct tcp_rack *rack; 9048 struct rack_sendmap *rsm, *nrsm; 9049 int insret __diagused; 9050 9051 register uint32_t snd_max, snd_una; 9052 9053 /* 9054 * Add to the RACK log of packets in flight or retransmitted. If 9055 * there is a TS option we will use the TS echoed, if not we will 9056 * grab a TS. 9057 * 9058 * Retransmissions will increment the count and move the ts to its 9059 * proper place. Note that if options do not include TS's then we 9060 * won't be able to effectively use the ACK for an RTT on a retran. 9061 * 9062 * Notes about r_start and r_end. Lets consider a send starting at 9063 * sequence 1 for 10 bytes. In such an example the r_start would be 9064 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 9065 * This means that r_end is actually the first sequence for the next 9066 * slot (11). 9067 * 9068 */ 9069 /* 9070 * If err is set what do we do XXXrrs? should we not add the thing? 9071 * -- i.e. return if err != 0 or should we pretend we sent it? -- 9072 * i.e. proceed with add ** do this for now. 9073 */ 9074 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9075 if (err) 9076 /* 9077 * We don't log errors -- we could but snd_max does not 9078 * advance in this case either. 9079 */ 9080 return; 9081 9082 if (th_flags & TH_RST) { 9083 /* 9084 * We don't log resets and we return immediately from 9085 * sending 9086 */ 9087 return; 9088 } 9089 rack = (struct tcp_rack *)tp->t_fb_ptr; 9090 snd_una = tp->snd_una; 9091 snd_max = tp->snd_max; 9092 if (th_flags & (TH_SYN | TH_FIN)) { 9093 /* 9094 * The call to rack_log_output is made before bumping 9095 * snd_max. This means we can record one extra byte on a SYN 9096 * or FIN if seq_out is adding more on and a FIN is present 9097 * (and we are not resending). 9098 */ 9099 if ((th_flags & TH_SYN) && (seq_out == tp->iss)) 9100 len++; 9101 if (th_flags & TH_FIN) 9102 len++; 9103 } 9104 if (SEQ_LEQ((seq_out + len), snd_una)) { 9105 /* Are sending an old segment to induce an ack (keep-alive)? */ 9106 return; 9107 } 9108 if (SEQ_LT(seq_out, snd_una)) { 9109 /* huh? should we panic? */ 9110 uint32_t end; 9111 9112 end = seq_out + len; 9113 seq_out = snd_una; 9114 if (SEQ_GEQ(end, seq_out)) 9115 len = end - seq_out; 9116 else 9117 len = 0; 9118 } 9119 if (len == 0) { 9120 /* We don't log zero window probes */ 9121 return; 9122 } 9123 if (IN_FASTRECOVERY(tp->t_flags)) { 9124 rack->r_ctl.rc_prr_out += len; 9125 } 9126 /* First question is it a retransmission or new? */ 9127 if (seq_out == snd_max) { 9128 /* Its new */ 9129 rack_chk_req_and_hybrid_on_out(rack, seq_out, len, cts); 9130 again: 9131 rsm = rack_alloc(rack); 9132 if (rsm == NULL) { 9133 /* 9134 * Hmm out of memory and the tcb got destroyed while 9135 * we tried to wait. 9136 */ 9137 return; 9138 } 9139 if (th_flags & TH_FIN) { 9140 rsm->r_flags = RACK_HAS_FIN|add_flag; 9141 } else { 9142 rsm->r_flags = add_flag; 9143 } 9144 if (hw_tls) 9145 rsm->r_hw_tls = 1; 9146 rsm->r_tim_lastsent[0] = cts; 9147 rsm->r_rtr_cnt = 1; 9148 rsm->r_act_rxt_cnt = 0; 9149 rsm->r_rtr_bytes = 0; 9150 if (th_flags & TH_SYN) { 9151 /* The data space is one beyond snd_una */ 9152 rsm->r_flags |= RACK_HAS_SYN; 9153 } 9154 rsm->r_start = seq_out; 9155 rsm->r_end = rsm->r_start + len; 9156 rack_mark_in_gp_win(tp, rsm); 9157 rsm->r_dupack = 0; 9158 /* 9159 * save off the mbuf location that 9160 * sndmbuf_noadv returned (which is 9161 * where we started copying from).. 9162 */ 9163 rsm->m = s_mb; 9164 rsm->soff = s_moff; 9165 /* 9166 * Here we do add in the len of send, since its not yet 9167 * reflected in in snduna <->snd_max 9168 */ 9169 rsm->r_fas = (ctf_flight_size(rack->rc_tp, 9170 rack->r_ctl.rc_sacked) + 9171 (rsm->r_end - rsm->r_start)); 9172 if ((rack->rc_initial_ss_comp == 0) && 9173 (rack->r_ctl.ss_hi_fs < rsm->r_fas)) { 9174 rack->r_ctl.ss_hi_fs = rsm->r_fas; 9175 } 9176 /* rsm->m will be NULL if RACK_HAS_SYN or RACK_HAS_FIN is set */ 9177 if (rsm->m) { 9178 if (rsm->m->m_len <= rsm->soff) { 9179 /* 9180 * XXXrrs Question, will this happen? 9181 * 9182 * If sbsndptr is set at the correct place 9183 * then s_moff should always be somewhere 9184 * within rsm->m. But if the sbsndptr was 9185 * off then that won't be true. If it occurs 9186 * we need to walkout to the correct location. 9187 */ 9188 struct mbuf *lm; 9189 9190 lm = rsm->m; 9191 while (lm->m_len <= rsm->soff) { 9192 rsm->soff -= lm->m_len; 9193 lm = lm->m_next; 9194 KASSERT(lm != NULL, ("%s rack:%p lm goes null orig_off:%u origmb:%p rsm->soff:%u", 9195 __func__, rack, s_moff, s_mb, rsm->soff)); 9196 } 9197 rsm->m = lm; 9198 } 9199 rsm->orig_m_len = rsm->m->m_len; 9200 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 9201 } else { 9202 rsm->orig_m_len = 0; 9203 rsm->orig_t_space = 0; 9204 } 9205 rsm->r_bas = (uint8_t)((len + segsiz - 1) / segsiz); 9206 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 9207 /* Log a new rsm */ 9208 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_NEW, 0, __LINE__); 9209 #ifndef INVARIANTS 9210 (void)tqhash_insert(rack->r_ctl.tqh, rsm); 9211 #else 9212 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) { 9213 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 9214 nrsm, insret, rack, rsm); 9215 } 9216 #endif 9217 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 9218 rsm->r_in_tmap = 1; 9219 if (rsm->r_flags & RACK_IS_PCM) { 9220 rack->r_ctl.pcm_i.send_time = cts; 9221 rack->r_ctl.pcm_i.eseq = rsm->r_end; 9222 /* First time through we set the start too */ 9223 if (rack->pcm_in_progress == 0) 9224 rack->r_ctl.pcm_i.sseq = rsm->r_start; 9225 } 9226 /* 9227 * Special case detection, is there just a single 9228 * packet outstanding when we are not in recovery? 9229 * 9230 * If this is true mark it so. 9231 */ 9232 if ((IN_FASTRECOVERY(tp->t_flags) == 0) && 9233 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) == ctf_fixed_maxseg(tp))) { 9234 struct rack_sendmap *prsm; 9235 9236 prsm = tqhash_prev(rack->r_ctl.tqh, rsm); 9237 if (prsm) 9238 prsm->r_one_out_nr = 1; 9239 } 9240 return; 9241 } 9242 /* 9243 * If we reach here its a retransmission and we need to find it. 9244 */ 9245 more: 9246 if (hintrsm && (hintrsm->r_start == seq_out)) { 9247 rsm = hintrsm; 9248 hintrsm = NULL; 9249 } else { 9250 /* No hints sorry */ 9251 rsm = NULL; 9252 } 9253 if ((rsm) && (rsm->r_start == seq_out)) { 9254 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz); 9255 if (len == 0) { 9256 return; 9257 } else { 9258 goto more; 9259 } 9260 } 9261 /* Ok it was not the last pointer go through it the hard way. */ 9262 refind: 9263 rsm = tqhash_find(rack->r_ctl.tqh, seq_out); 9264 if (rsm) { 9265 if (rsm->r_start == seq_out) { 9266 seq_out = rack_update_entry(tp, rack, rsm, cts, &len, add_flag, segsiz); 9267 if (len == 0) { 9268 return; 9269 } else { 9270 goto refind; 9271 } 9272 } 9273 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 9274 /* Transmitted within this piece */ 9275 /* 9276 * Ok we must split off the front and then let the 9277 * update do the rest 9278 */ 9279 nrsm = rack_alloc_full_limit(rack); 9280 if (nrsm == NULL) { 9281 rack_update_rsm(tp, rack, rsm, cts, add_flag, segsiz); 9282 return; 9283 } 9284 /* 9285 * copy rsm to nrsm and then trim the front of rsm 9286 * to not include this part. 9287 */ 9288 rack_clone_rsm(rack, nrsm, rsm, seq_out); 9289 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 0, __LINE__); 9290 #ifndef INVARIANTS 9291 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 9292 #else 9293 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 9294 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 9295 nrsm, insret, rack, rsm); 9296 } 9297 #endif 9298 if (rsm->r_in_tmap) { 9299 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 9300 nrsm->r_in_tmap = 1; 9301 } 9302 rsm->r_flags &= (~RACK_HAS_FIN); 9303 seq_out = rack_update_entry(tp, rack, nrsm, cts, &len, add_flag, segsiz); 9304 if (len == 0) { 9305 return; 9306 } else if (len > 0) 9307 goto refind; 9308 } 9309 } 9310 /* 9311 * Hmm not found in map did they retransmit both old and on into the 9312 * new? 9313 */ 9314 if (seq_out == tp->snd_max) { 9315 goto again; 9316 } else if (SEQ_LT(seq_out, tp->snd_max)) { 9317 #ifdef INVARIANTS 9318 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 9319 seq_out, len, tp->snd_una, tp->snd_max); 9320 printf("Starting Dump of all rack entries\n"); 9321 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) { 9322 printf("rsm:%p start:%u end:%u\n", 9323 rsm, rsm->r_start, rsm->r_end); 9324 } 9325 printf("Dump complete\n"); 9326 panic("seq_out not found rack:%p tp:%p", 9327 rack, tp); 9328 #endif 9329 } else { 9330 #ifdef INVARIANTS 9331 /* 9332 * Hmm beyond sndmax? (only if we are using the new rtt-pack 9333 * flag) 9334 */ 9335 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 9336 seq_out, len, tp->snd_max, tp); 9337 #endif 9338 } 9339 } 9340 9341 /* 9342 * Record one of the RTT updates from an ack into 9343 * our sample structure. 9344 */ 9345 9346 static void 9347 tcp_rack_xmit_timer(struct tcp_rack *rack, int32_t rtt, uint32_t len, uint32_t us_rtt, 9348 int confidence, struct rack_sendmap *rsm, uint16_t rtrcnt) 9349 { 9350 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 9351 (rack->r_ctl.rack_rs.rs_rtt_lowest > rtt)) { 9352 rack->r_ctl.rack_rs.rs_rtt_lowest = rtt; 9353 } 9354 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 9355 (rack->r_ctl.rack_rs.rs_rtt_highest < rtt)) { 9356 rack->r_ctl.rack_rs.rs_rtt_highest = rtt; 9357 } 9358 if (rack->rc_tp->t_flags & TF_GPUTINPROG) { 9359 if (us_rtt < rack->r_ctl.rc_gp_lowrtt) 9360 rack->r_ctl.rc_gp_lowrtt = us_rtt; 9361 if (rack->rc_tp->snd_wnd > rack->r_ctl.rc_gp_high_rwnd) 9362 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 9363 } 9364 if ((confidence == 1) && 9365 ((rsm == NULL) || 9366 (rsm->r_just_ret) || 9367 (rsm->r_one_out_nr && 9368 len < (ctf_fixed_maxseg(rack->rc_tp) * 2)))) { 9369 /* 9370 * If the rsm had a just return 9371 * hit it then we can't trust the 9372 * rtt measurement for buffer deterimination 9373 * Note that a confidence of 2, indicates 9374 * SACK'd which overrides the r_just_ret or 9375 * the r_one_out_nr. If it was a CUM-ACK and 9376 * we had only two outstanding, but get an 9377 * ack for only 1. Then that also lowers our 9378 * confidence. 9379 */ 9380 confidence = 0; 9381 } 9382 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) || 9383 (rack->r_ctl.rack_rs.rs_us_rtt > us_rtt)) { 9384 if (rack->r_ctl.rack_rs.confidence == 0) { 9385 /* 9386 * We take anything with no current confidence 9387 * saved. 9388 */ 9389 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt; 9390 rack->r_ctl.rack_rs.confidence = confidence; 9391 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt; 9392 } else if (confidence != 0) { 9393 /* 9394 * Once we have a confident number, 9395 * we can update it with a smaller 9396 * value since this confident number 9397 * may include the DSACK time until 9398 * the next segment (the second one) arrived. 9399 */ 9400 rack->r_ctl.rack_rs.rs_us_rtt = us_rtt; 9401 rack->r_ctl.rack_rs.confidence = confidence; 9402 rack->r_ctl.rack_rs.rs_us_rtrcnt = rtrcnt; 9403 } 9404 } 9405 rack_log_rtt_upd(rack->rc_tp, rack, us_rtt, len, rsm, confidence); 9406 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_VALID; 9407 rack->r_ctl.rack_rs.rs_rtt_tot += rtt; 9408 rack->r_ctl.rack_rs.rs_rtt_cnt++; 9409 } 9410 9411 /* 9412 * Collect new round-trip time estimate 9413 * and update averages and current timeout. 9414 */ 9415 static void 9416 tcp_rack_xmit_timer_commit(struct tcp_rack *rack, struct tcpcb *tp) 9417 { 9418 int32_t delta; 9419 int32_t rtt; 9420 9421 if (rack->r_ctl.rack_rs.rs_flags & RACK_RTT_EMPTY) 9422 /* No valid sample */ 9423 return; 9424 if (rack->r_ctl.rc_rate_sample_method == USE_RTT_LOW) { 9425 /* We are to use the lowest RTT seen in a single ack */ 9426 rtt = rack->r_ctl.rack_rs.rs_rtt_lowest; 9427 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_HIGH) { 9428 /* We are to use the highest RTT seen in a single ack */ 9429 rtt = rack->r_ctl.rack_rs.rs_rtt_highest; 9430 } else if (rack->r_ctl.rc_rate_sample_method == USE_RTT_AVG) { 9431 /* We are to use the average RTT seen in a single ack */ 9432 rtt = (int32_t)(rack->r_ctl.rack_rs.rs_rtt_tot / 9433 (uint64_t)rack->r_ctl.rack_rs.rs_rtt_cnt); 9434 } else { 9435 #ifdef INVARIANTS 9436 panic("Unknown rtt variant %d", rack->r_ctl.rc_rate_sample_method); 9437 #endif 9438 return; 9439 } 9440 if (rtt == 0) 9441 rtt = 1; 9442 if (rack->rc_gp_rtt_set == 0) { 9443 /* 9444 * With no RTT we have to accept 9445 * even one we are not confident of. 9446 */ 9447 rack->r_ctl.rc_gp_srtt = rack->r_ctl.rack_rs.rs_us_rtt; 9448 rack->rc_gp_rtt_set = 1; 9449 } else if (rack->r_ctl.rack_rs.confidence) { 9450 /* update the running gp srtt */ 9451 rack->r_ctl.rc_gp_srtt -= (rack->r_ctl.rc_gp_srtt/8); 9452 rack->r_ctl.rc_gp_srtt += rack->r_ctl.rack_rs.rs_us_rtt / 8; 9453 } 9454 if (rack->r_ctl.rack_rs.confidence) { 9455 /* 9456 * record the low and high for highly buffered path computation, 9457 * we only do this if we are confident (not a retransmission). 9458 */ 9459 if (rack->r_ctl.rc_highest_us_rtt < rack->r_ctl.rack_rs.rs_us_rtt) { 9460 rack->r_ctl.rc_highest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 9461 } 9462 if (rack->rc_highly_buffered == 0) { 9463 /* 9464 * Currently once we declare a path has 9465 * highly buffered there is no going 9466 * back, which may be a problem... 9467 */ 9468 if ((rack->r_ctl.rc_highest_us_rtt / rack->r_ctl.rc_lowest_us_rtt) > rack_hbp_thresh) { 9469 rack_log_rtt_shrinks(rack, rack->r_ctl.rack_rs.rs_us_rtt, 9470 rack->r_ctl.rc_highest_us_rtt, 9471 rack->r_ctl.rc_lowest_us_rtt, 9472 RACK_RTTS_SEEHBP); 9473 rack->rc_highly_buffered = 1; 9474 } 9475 } 9476 } 9477 if ((rack->r_ctl.rack_rs.confidence) || 9478 (rack->r_ctl.rack_rs.rs_us_rtrcnt == 1)) { 9479 /* 9480 * If we are highly confident of it <or> it was 9481 * never retransmitted we accept it as the last us_rtt. 9482 */ 9483 rack->r_ctl.rc_last_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 9484 /* The lowest rtt can be set if its was not retransmited */ 9485 if (rack->r_ctl.rc_lowest_us_rtt > rack->r_ctl.rack_rs.rs_us_rtt) { 9486 rack->r_ctl.rc_lowest_us_rtt = rack->r_ctl.rack_rs.rs_us_rtt; 9487 if (rack->r_ctl.rc_lowest_us_rtt == 0) 9488 rack->r_ctl.rc_lowest_us_rtt = 1; 9489 } 9490 } 9491 rack = (struct tcp_rack *)tp->t_fb_ptr; 9492 if (tp->t_srtt != 0) { 9493 /* 9494 * We keep a simple srtt in microseconds, like our rtt 9495 * measurement. We don't need to do any tricks with shifting 9496 * etc. Instead we just add in 1/8th of the new measurement 9497 * and subtract out 1/8 of the old srtt. We do the same with 9498 * the variance after finding the absolute value of the 9499 * difference between this sample and the current srtt. 9500 */ 9501 delta = tp->t_srtt - rtt; 9502 /* Take off 1/8th of the current sRTT */ 9503 tp->t_srtt -= (tp->t_srtt >> 3); 9504 /* Add in 1/8th of the new RTT just measured */ 9505 tp->t_srtt += (rtt >> 3); 9506 if (tp->t_srtt <= 0) 9507 tp->t_srtt = 1; 9508 /* Now lets make the absolute value of the variance */ 9509 if (delta < 0) 9510 delta = -delta; 9511 /* Subtract out 1/8th */ 9512 tp->t_rttvar -= (tp->t_rttvar >> 3); 9513 /* Add in 1/8th of the new variance we just saw */ 9514 tp->t_rttvar += (delta >> 3); 9515 if (tp->t_rttvar <= 0) 9516 tp->t_rttvar = 1; 9517 } else { 9518 /* 9519 * No rtt measurement yet - use the unsmoothed rtt. Set the 9520 * variance to half the rtt (so our first retransmit happens 9521 * at 3*rtt). 9522 */ 9523 tp->t_srtt = rtt; 9524 tp->t_rttvar = rtt >> 1; 9525 } 9526 rack->rc_srtt_measure_made = 1; 9527 KMOD_TCPSTAT_INC(tcps_rttupdated); 9528 if (tp->t_rttupdated < UCHAR_MAX) 9529 tp->t_rttupdated++; 9530 #ifdef STATS 9531 if (rack_stats_gets_ms_rtt == 0) { 9532 /* Send in the microsecond rtt used for rxt timeout purposes */ 9533 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt)); 9534 } else if (rack_stats_gets_ms_rtt == 1) { 9535 /* Send in the millisecond rtt used for rxt timeout purposes */ 9536 int32_t ms_rtt; 9537 9538 /* Round up */ 9539 ms_rtt = (rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC; 9540 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt)); 9541 } else if (rack_stats_gets_ms_rtt == 2) { 9542 /* Send in the millisecond rtt has close to the path RTT as we can get */ 9543 int32_t ms_rtt; 9544 9545 /* Round up */ 9546 ms_rtt = (rack->r_ctl.rack_rs.rs_us_rtt + HPTS_USEC_IN_MSEC - 1) / HPTS_USEC_IN_MSEC; 9547 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, ms_rtt)); 9548 } else { 9549 /* Send in the microsecond rtt has close to the path RTT as we can get */ 9550 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt)); 9551 } 9552 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rack->r_ctl.rack_rs.rs_us_rtt)); 9553 #endif 9554 rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time); 9555 /* 9556 * the retransmit should happen at rtt + 4 * rttvar. Because of the 9557 * way we do the smoothing, srtt and rttvar will each average +1/2 9558 * tick of bias. When we compute the retransmit timer, we want 1/2 9559 * tick of rounding and 1 extra tick because of +-1/2 tick 9560 * uncertainty in the firing of the timer. The bias will give us 9561 * exactly the 1.5 tick we need. But, because the bias is 9562 * statistical, we have to test that we don't drop below the minimum 9563 * feasible timer (which is 2 ticks). 9564 */ 9565 tp->t_rxtshift = 0; 9566 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 9567 max(rack_rto_min, rtt + 2), rack_rto_max, rack->r_ctl.timer_slop); 9568 rack_log_rtt_sample(rack, rtt); 9569 tp->t_softerror = 0; 9570 } 9571 9572 9573 static void 9574 rack_apply_updated_usrtt(struct tcp_rack *rack, uint32_t us_rtt, uint32_t us_cts) 9575 { 9576 /* 9577 * Apply to filter the inbound us-rtt at us_cts. 9578 */ 9579 uint32_t old_rtt; 9580 9581 old_rtt = get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt); 9582 apply_filter_min_small(&rack->r_ctl.rc_gp_min_rtt, 9583 us_rtt, us_cts); 9584 if (old_rtt > us_rtt) { 9585 /* We just hit a new lower rtt time */ 9586 rack_log_rtt_shrinks(rack, us_cts, old_rtt, 9587 __LINE__, RACK_RTTS_NEWRTT); 9588 /* 9589 * Only count it if its lower than what we saw within our 9590 * calculated range. 9591 */ 9592 if ((old_rtt - us_rtt) > rack_min_rtt_movement) { 9593 if (rack_probertt_lower_within && 9594 rack->rc_gp_dyn_mul && 9595 (rack->use_fixed_rate == 0) && 9596 (rack->rc_always_pace)) { 9597 /* 9598 * We are seeing a new lower rtt very close 9599 * to the time that we would have entered probe-rtt. 9600 * This is probably due to the fact that a peer flow 9601 * has entered probe-rtt. Lets go in now too. 9602 */ 9603 uint32_t val; 9604 9605 val = rack_probertt_lower_within * rack_time_between_probertt; 9606 val /= 100; 9607 if ((rack->in_probe_rtt == 0) && 9608 (rack->rc_skip_timely == 0) && 9609 ((us_cts - rack->r_ctl.rc_lower_rtt_us_cts) >= (rack_time_between_probertt - val))) { 9610 rack_enter_probertt(rack, us_cts); 9611 } 9612 } 9613 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 9614 } 9615 } 9616 } 9617 9618 static int 9619 rack_update_rtt(struct tcpcb *tp, struct tcp_rack *rack, 9620 struct rack_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, tcp_seq th_ack) 9621 { 9622 uint32_t us_rtt; 9623 int32_t i, all; 9624 uint32_t t, len_acked; 9625 9626 if ((rsm->r_flags & RACK_ACKED) || 9627 (rsm->r_flags & RACK_WAS_ACKED)) 9628 /* Already done */ 9629 return (0); 9630 if (rsm->r_no_rtt_allowed) { 9631 /* Not allowed */ 9632 return (0); 9633 } 9634 if (ack_type == CUM_ACKED) { 9635 if (SEQ_GT(th_ack, rsm->r_end)) { 9636 len_acked = rsm->r_end - rsm->r_start; 9637 all = 1; 9638 } else { 9639 len_acked = th_ack - rsm->r_start; 9640 all = 0; 9641 } 9642 } else { 9643 len_acked = rsm->r_end - rsm->r_start; 9644 all = 0; 9645 } 9646 if (rsm->r_rtr_cnt == 1) { 9647 9648 t = cts - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 9649 if ((int)t <= 0) 9650 t = 1; 9651 if (!tp->t_rttlow || tp->t_rttlow > t) 9652 tp->t_rttlow = t; 9653 if (!rack->r_ctl.rc_rack_min_rtt || 9654 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 9655 rack->r_ctl.rc_rack_min_rtt = t; 9656 if (rack->r_ctl.rc_rack_min_rtt == 0) { 9657 rack->r_ctl.rc_rack_min_rtt = 1; 9658 } 9659 } 9660 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) 9661 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 9662 else 9663 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 9664 if (us_rtt == 0) 9665 us_rtt = 1; 9666 if (CC_ALGO(tp)->rttsample != NULL) { 9667 /* Kick the RTT to the CC */ 9668 CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas); 9669 } 9670 rack_apply_updated_usrtt(rack, us_rtt, tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time)); 9671 if (ack_type == SACKED) { 9672 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 1); 9673 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 2 , rsm, rsm->r_rtr_cnt); 9674 } else { 9675 /* 9676 * We need to setup what our confidence 9677 * is in this ack. 9678 * 9679 * If the rsm was app limited and it is 9680 * less than a mss in length (the end 9681 * of the send) then we have a gap. If we 9682 * were app limited but say we were sending 9683 * multiple MSS's then we are more confident 9684 * int it. 9685 * 9686 * When we are not app-limited then we see if 9687 * the rsm is being included in the current 9688 * measurement, we tell this by the app_limited_needs_set 9689 * flag. 9690 * 9691 * Note that being cwnd blocked is not applimited 9692 * as well as the pacing delay between packets which 9693 * are sending only 1 or 2 MSS's also will show up 9694 * in the RTT. We probably need to examine this algorithm 9695 * a bit more and enhance it to account for the delay 9696 * between rsm's. We could do that by saving off the 9697 * pacing delay of each rsm (in an rsm) and then 9698 * factoring that in somehow though for now I am 9699 * not sure how :) 9700 */ 9701 int calc_conf = 0; 9702 9703 if (rsm->r_flags & RACK_APP_LIMITED) { 9704 if (all && (len_acked <= ctf_fixed_maxseg(tp))) 9705 calc_conf = 0; 9706 else 9707 calc_conf = 1; 9708 } else if (rack->app_limited_needs_set == 0) { 9709 calc_conf = 1; 9710 } else { 9711 calc_conf = 0; 9712 } 9713 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)], cts, 2); 9714 tcp_rack_xmit_timer(rack, t + 1, len_acked, us_rtt, 9715 calc_conf, rsm, rsm->r_rtr_cnt); 9716 } 9717 if ((rsm->r_flags & RACK_TLP) && 9718 (!IN_FASTRECOVERY(tp->t_flags))) { 9719 /* Segment was a TLP and our retrans matched */ 9720 if (rack->r_ctl.rc_tlp_cwnd_reduce) { 9721 rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__); 9722 } 9723 } 9724 if ((rack->r_ctl.rc_rack_tmit_time == 0) || 9725 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, 9726 (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) { 9727 /* New more recent rack_tmit_time */ 9728 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 9729 if (rack->r_ctl.rc_rack_tmit_time == 0) 9730 rack->r_ctl.rc_rack_tmit_time = 1; 9731 rack->rc_rack_rtt = t; 9732 } 9733 return (1); 9734 } 9735 /* 9736 * We clear the soft/rxtshift since we got an ack. 9737 * There is no assurance we will call the commit() function 9738 * so we need to clear these to avoid incorrect handling. 9739 */ 9740 tp->t_rxtshift = 0; 9741 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 9742 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 9743 tp->t_softerror = 0; 9744 if (to && (to->to_flags & TOF_TS) && 9745 (ack_type == CUM_ACKED) && 9746 (to->to_tsecr) && 9747 ((rsm->r_flags & RACK_OVERMAX) == 0)) { 9748 /* 9749 * Now which timestamp does it match? In this block the ACK 9750 * must be coming from a previous transmission. 9751 */ 9752 for (i = 0; i < rsm->r_rtr_cnt; i++) { 9753 if (rack_ts_to_msec(rsm->r_tim_lastsent[i]) == to->to_tsecr) { 9754 t = cts - (uint32_t)rsm->r_tim_lastsent[i]; 9755 if ((int)t <= 0) 9756 t = 1; 9757 if (CC_ALGO(tp)->rttsample != NULL) { 9758 /* 9759 * Kick the RTT to the CC, here 9760 * we lie a bit in that we know the 9761 * retransmission is correct even though 9762 * we retransmitted. This is because 9763 * we match the timestamps. 9764 */ 9765 if (TSTMP_GT(tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time), rsm->r_tim_lastsent[i])) 9766 us_rtt = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time) - (uint32_t)rsm->r_tim_lastsent[i]; 9767 else 9768 us_rtt = tcp_get_usecs(NULL) - (uint32_t)rsm->r_tim_lastsent[i]; 9769 CC_ALGO(tp)->rttsample(&tp->t_ccv, us_rtt, 1, rsm->r_fas); 9770 } 9771 if ((i + 1) < rsm->r_rtr_cnt) { 9772 /* 9773 * The peer ack'd from our previous 9774 * transmission. We have a spurious 9775 * retransmission and thus we dont 9776 * want to update our rack_rtt. 9777 * 9778 * Hmm should there be a CC revert here? 9779 * 9780 */ 9781 return (0); 9782 } 9783 if (!tp->t_rttlow || tp->t_rttlow > t) 9784 tp->t_rttlow = t; 9785 if (!rack->r_ctl.rc_rack_min_rtt || SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 9786 rack->r_ctl.rc_rack_min_rtt = t; 9787 if (rack->r_ctl.rc_rack_min_rtt == 0) { 9788 rack->r_ctl.rc_rack_min_rtt = 1; 9789 } 9790 } 9791 if ((rack->r_ctl.rc_rack_tmit_time == 0) || 9792 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, 9793 (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]))) { 9794 /* New more recent rack_tmit_time */ 9795 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 9796 if (rack->r_ctl.rc_rack_tmit_time == 0) 9797 rack->r_ctl.rc_rack_tmit_time = 1; 9798 rack->rc_rack_rtt = t; 9799 } 9800 rack_log_rtt_sample_calc(rack, t, (uint32_t)rsm->r_tim_lastsent[i], cts, 3); 9801 tcp_rack_xmit_timer(rack, t + 1, len_acked, t, 0, rsm, 9802 rsm->r_rtr_cnt); 9803 return (1); 9804 } 9805 } 9806 /* If we are logging log out the sendmap */ 9807 if (tcp_bblogging_on(rack->rc_tp)) { 9808 for (i = 0; i < rsm->r_rtr_cnt; i++) { 9809 rack_log_rtt_sendmap(rack, i, rsm->r_tim_lastsent[i], to->to_tsecr); 9810 } 9811 } 9812 goto ts_not_found; 9813 } else { 9814 /* 9815 * Ok its a SACK block that we retransmitted. or a windows 9816 * machine without timestamps. We can tell nothing from the 9817 * time-stamp since its not there or the time the peer last 9818 * received a segment that moved forward its cum-ack point. 9819 */ 9820 ts_not_found: 9821 i = rsm->r_rtr_cnt - 1; 9822 t = cts - (uint32_t)rsm->r_tim_lastsent[i]; 9823 if ((int)t <= 0) 9824 t = 1; 9825 if (rack->r_ctl.rc_rack_min_rtt && SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 9826 /* 9827 * We retransmitted and the ack came back in less 9828 * than the smallest rtt we have observed. We most 9829 * likely did an improper retransmit as outlined in 9830 * 6.2 Step 2 point 2 in the rack-draft so we 9831 * don't want to update our rack_rtt. We in 9832 * theory (in future) might want to think about reverting our 9833 * cwnd state but we won't for now. 9834 */ 9835 return (0); 9836 } else if (rack->r_ctl.rc_rack_min_rtt) { 9837 /* 9838 * We retransmitted it and the retransmit did the 9839 * job. 9840 */ 9841 if (!rack->r_ctl.rc_rack_min_rtt || 9842 SEQ_LT(t, rack->r_ctl.rc_rack_min_rtt)) { 9843 rack->r_ctl.rc_rack_min_rtt = t; 9844 if (rack->r_ctl.rc_rack_min_rtt == 0) { 9845 rack->r_ctl.rc_rack_min_rtt = 1; 9846 } 9847 } 9848 if ((rack->r_ctl.rc_rack_tmit_time == 0) || 9849 (SEQ_LT(rack->r_ctl.rc_rack_tmit_time, 9850 (uint32_t)rsm->r_tim_lastsent[i]))) { 9851 /* New more recent rack_tmit_time */ 9852 rack->r_ctl.rc_rack_tmit_time = (uint32_t)rsm->r_tim_lastsent[i]; 9853 if (rack->r_ctl.rc_rack_tmit_time == 0) 9854 rack->r_ctl.rc_rack_tmit_time = 1; 9855 rack->rc_rack_rtt = t; 9856 } 9857 return (1); 9858 } 9859 } 9860 return (0); 9861 } 9862 9863 /* 9864 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 9865 */ 9866 static void 9867 rack_log_sack_passed(struct tcpcb *tp, 9868 struct tcp_rack *rack, struct rack_sendmap *rsm, uint32_t cts) 9869 { 9870 struct rack_sendmap *nrsm; 9871 uint32_t thresh; 9872 9873 /* Get our rxt threshold for lost consideration */ 9874 thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(tp, rack), cts, __LINE__, 0); 9875 /* Now start looking at rsm's */ 9876 nrsm = rsm; 9877 TAILQ_FOREACH_REVERSE_FROM(nrsm, &rack->r_ctl.rc_tmap, 9878 rack_head, r_tnext) { 9879 if (nrsm == rsm) { 9880 /* Skip original segment he is acked */ 9881 continue; 9882 } 9883 if (nrsm->r_flags & RACK_ACKED) { 9884 /* 9885 * Skip ack'd segments, though we 9886 * should not see these, since tmap 9887 * should not have ack'd segments. 9888 */ 9889 continue; 9890 } 9891 if (nrsm->r_flags & RACK_RWND_COLLAPSED) { 9892 /* 9893 * If the peer dropped the rwnd on 9894 * these then we don't worry about them. 9895 */ 9896 continue; 9897 } 9898 /* Check lost state */ 9899 if ((nrsm->r_flags & RACK_WAS_LOST) == 0) { 9900 uint32_t exp; 9901 9902 exp = ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) + thresh; 9903 if (TSTMP_LT(exp, cts) || (exp == cts)) { 9904 /* We consider it lost */ 9905 nrsm->r_flags |= RACK_WAS_LOST; 9906 rack->r_ctl.rc_considered_lost += nrsm->r_end - nrsm->r_start; 9907 } 9908 } 9909 if (nrsm->r_flags & RACK_SACK_PASSED) { 9910 /* 9911 * We found one that is already marked 9912 * passed, we have been here before and 9913 * so all others below this are marked. 9914 */ 9915 break; 9916 } 9917 nrsm->r_flags |= RACK_SACK_PASSED; 9918 nrsm->r_flags &= ~RACK_WAS_SACKPASS; 9919 } 9920 } 9921 9922 static void 9923 rack_need_set_test(struct tcpcb *tp, 9924 struct tcp_rack *rack, 9925 struct rack_sendmap *rsm, 9926 tcp_seq th_ack, 9927 int line, 9928 int use_which) 9929 { 9930 struct rack_sendmap *s_rsm; 9931 9932 if ((tp->t_flags & TF_GPUTINPROG) && 9933 SEQ_GEQ(rsm->r_end, tp->gput_seq)) { 9934 /* 9935 * We were app limited, and this ack 9936 * butts up or goes beyond the point where we want 9937 * to start our next measurement. We need 9938 * to record the new gput_ts as here and 9939 * possibly update the start sequence. 9940 */ 9941 uint32_t seq, ts; 9942 9943 if (rsm->r_rtr_cnt > 1) { 9944 /* 9945 * This is a retransmit, can we 9946 * really make any assessment at this 9947 * point? We are not really sure of 9948 * the timestamp, is it this or the 9949 * previous transmission? 9950 * 9951 * Lets wait for something better that 9952 * is not retransmitted. 9953 */ 9954 return; 9955 } 9956 seq = tp->gput_seq; 9957 ts = tp->gput_ts; 9958 rack->app_limited_needs_set = 0; 9959 tp->gput_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 9960 /* Do we start at a new end? */ 9961 if ((use_which == RACK_USE_BEG) && 9962 SEQ_GEQ(rsm->r_start, tp->gput_seq)) { 9963 /* 9964 * When we get an ACK that just eats 9965 * up some of the rsm, we set RACK_USE_BEG 9966 * since whats at r_start (i.e. th_ack) 9967 * is left unacked and thats where the 9968 * measurement now starts. 9969 */ 9970 tp->gput_seq = rsm->r_start; 9971 } 9972 if ((use_which == RACK_USE_END) && 9973 SEQ_GEQ(rsm->r_end, tp->gput_seq)) { 9974 /* 9975 * We use the end when the cumack 9976 * is moving forward and completely 9977 * deleting the rsm passed so basically 9978 * r_end holds th_ack. 9979 * 9980 * For SACK's we also want to use the end 9981 * since this piece just got sacked and 9982 * we want to target anything after that 9983 * in our measurement. 9984 */ 9985 tp->gput_seq = rsm->r_end; 9986 } 9987 if (use_which == RACK_USE_END_OR_THACK) { 9988 /* 9989 * special case for ack moving forward, 9990 * not a sack, we need to move all the 9991 * way up to where this ack cum-ack moves 9992 * to. 9993 */ 9994 if (SEQ_GT(th_ack, rsm->r_end)) 9995 tp->gput_seq = th_ack; 9996 else 9997 tp->gput_seq = rsm->r_end; 9998 } 9999 if (SEQ_LT(tp->gput_seq, tp->snd_max)) 10000 s_rsm = tqhash_find(rack->r_ctl.tqh, tp->gput_seq); 10001 else 10002 s_rsm = NULL; 10003 /* 10004 * Pick up the correct send time if we can the rsm passed in 10005 * may be equal to s_rsm if the RACK_USE_BEG was set. For the other 10006 * two cases (RACK_USE_THACK or RACK_USE_END) most likely we will 10007 * find a different seq i.e. the next send up. 10008 * 10009 * If that has not been sent, s_rsm will be NULL and we must 10010 * arrange it so this function will get called again by setting 10011 * app_limited_needs_set. 10012 */ 10013 if (s_rsm) 10014 rack->r_ctl.rc_gp_output_ts = s_rsm->r_tim_lastsent[0]; 10015 else { 10016 /* If we hit here we have to have *not* sent tp->gput_seq */ 10017 rack->r_ctl.rc_gp_output_ts = rsm->r_tim_lastsent[0]; 10018 /* Set it up so we will go through here again */ 10019 rack->app_limited_needs_set = 1; 10020 } 10021 if (SEQ_GT(tp->gput_seq, tp->gput_ack)) { 10022 /* 10023 * We moved beyond this guy's range, re-calculate 10024 * the new end point. 10025 */ 10026 if (rack->rc_gp_filled == 0) { 10027 tp->gput_ack = tp->gput_seq + max(rc_init_window(rack), (MIN_GP_WIN * ctf_fixed_maxseg(tp))); 10028 } else { 10029 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 10030 } 10031 } 10032 /* 10033 * We are moving the goal post, we may be able to clear the 10034 * measure_saw_probe_rtt flag. 10035 */ 10036 if ((rack->in_probe_rtt == 0) && 10037 (rack->measure_saw_probe_rtt) && 10038 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 10039 rack->measure_saw_probe_rtt = 0; 10040 rack_log_pacing_delay_calc(rack, ts, tp->gput_ts, 10041 seq, tp->gput_seq, 10042 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | 10043 (uint64_t)rack->r_ctl.rc_gp_output_ts), 10044 5, line, NULL, 0); 10045 if (rack->rc_gp_filled && 10046 ((tp->gput_ack - tp->gput_seq) < 10047 max(rc_init_window(rack), (MIN_GP_WIN * 10048 ctf_fixed_maxseg(tp))))) { 10049 uint32_t ideal_amount; 10050 10051 ideal_amount = rack_get_measure_window(tp, rack); 10052 if (ideal_amount > sbavail(&tptosocket(tp)->so_snd)) { 10053 /* 10054 * There is no sense of continuing this measurement 10055 * because its too small to gain us anything we 10056 * trust. Skip it and that way we can start a new 10057 * measurement quicker. 10058 */ 10059 tp->t_flags &= ~TF_GPUTINPROG; 10060 rack_log_pacing_delay_calc(rack, tp->gput_ack, tp->gput_seq, 10061 0, 0, 10062 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | 10063 (uint64_t)rack->r_ctl.rc_gp_output_ts), 10064 6, __LINE__, NULL, 0); 10065 } else { 10066 /* 10067 * Reset the window further out. 10068 */ 10069 tp->gput_ack = tp->gput_seq + ideal_amount; 10070 } 10071 } 10072 rack_tend_gp_marks(tp, rack); 10073 rack_log_gpset(rack, tp->gput_ack, 0, 0, line, 2, rsm); 10074 } 10075 } 10076 10077 static inline int 10078 is_rsm_inside_declared_tlp_block(struct tcp_rack *rack, struct rack_sendmap *rsm) 10079 { 10080 if (SEQ_LT(rsm->r_end, rack->r_ctl.last_tlp_acked_start)) { 10081 /* Behind our TLP definition or right at */ 10082 return (0); 10083 } 10084 if (SEQ_GT(rsm->r_start, rack->r_ctl.last_tlp_acked_end)) { 10085 /* The start is beyond or right at our end of TLP definition */ 10086 return (0); 10087 } 10088 /* It has to be a sub-part of the original TLP recorded */ 10089 return (1); 10090 } 10091 10092 static uint32_t 10093 rack_proc_sack_blk(struct tcpcb *tp, struct tcp_rack *rack, struct sackblk *sack, 10094 struct tcpopt *to, struct rack_sendmap **prsm, uint32_t cts, 10095 int *no_extra, 10096 int *moved_two, uint32_t segsiz) 10097 { 10098 uint32_t start, end, changed = 0; 10099 struct rack_sendmap stack_map; 10100 struct rack_sendmap *rsm, *nrsm, *prev, *next; 10101 int insret __diagused; 10102 int32_t used_ref = 1; 10103 int moved = 0; 10104 #ifdef TCP_SAD_DETECTION 10105 int allow_segsiz; 10106 int first_time_through = 1; 10107 #endif 10108 int noextra = 0; 10109 int can_use_hookery = 0; 10110 10111 start = sack->start; 10112 end = sack->end; 10113 rsm = *prsm; 10114 10115 #ifdef TCP_SAD_DETECTION 10116 /* 10117 * There are a strange number of proxys and meddle boxes in the world 10118 * that seem to cut up segments on different boundaries. This gets us 10119 * smaller sacks that are still ok in terms of it being an attacker. 10120 * We use the base segsiz to calculate an allowable smallness but 10121 * also enforce a min on the segsiz in case it is an attacker playing 10122 * games with MSS. So basically if the sack arrives and it is 10123 * larger than a worse case 960 bytes, we don't classify the guy 10124 * as supicious. 10125 */ 10126 allow_segsiz = max(segsiz, 1200) * sad_seg_size_per; 10127 allow_segsiz /= 1000; 10128 #endif 10129 do_rest_ofb: 10130 if ((rsm == NULL) || 10131 (SEQ_LT(end, rsm->r_start)) || 10132 (SEQ_GEQ(start, rsm->r_end)) || 10133 (SEQ_LT(start, rsm->r_start))) { 10134 /* 10135 * We are not in the right spot, 10136 * find the correct spot in the tree. 10137 */ 10138 used_ref = 0; 10139 rsm = tqhash_find(rack->r_ctl.tqh, start); 10140 moved++; 10141 } 10142 if (rsm == NULL) { 10143 /* TSNH */ 10144 goto out; 10145 } 10146 #ifdef TCP_SAD_DETECTION 10147 /* Now we must check for suspicous activity */ 10148 if ((first_time_through == 1) && 10149 ((end - start) < min((rsm->r_end - rsm->r_start), allow_segsiz)) && 10150 ((rsm->r_flags & RACK_PMTU_CHG) == 0) && 10151 ((rsm->r_flags & RACK_TLP) == 0)) { 10152 /* 10153 * Its less than a full MSS or the segment being acked 10154 * this should only happen if the rsm in question had the 10155 * r_just_ret flag set <and> the end matches the end of 10156 * the rsm block. 10157 * 10158 * Note we do not look at segments that have had TLP's on 10159 * them since we can get un-reported rwnd collapses that 10160 * basically we TLP on and then we get back a sack block 10161 * that goes from the start to only a small way. 10162 * 10163 */ 10164 int loss, ok; 10165 10166 ok = 0; 10167 if (SEQ_GEQ(end, rsm->r_end)) { 10168 if (rsm->r_just_ret == 1) { 10169 /* This was at the end of a send which is ok */ 10170 ok = 1; 10171 } else { 10172 /* A bit harder was it the end of our segment */ 10173 int segs, len; 10174 10175 len = (rsm->r_end - rsm->r_start); 10176 segs = len / segsiz; 10177 segs *= segsiz; 10178 if ((segs + (rsm->r_end - start)) == len) { 10179 /* 10180 * So this last bit was the 10181 * end of our send if we cut it 10182 * up into segsiz pieces so its ok. 10183 */ 10184 ok = 1; 10185 } 10186 } 10187 } 10188 if (ok == 0) { 10189 /* 10190 * This guy is doing something suspicious 10191 * lets start detection. 10192 */ 10193 if (rack->rc_suspicious == 0) { 10194 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_SUSPECT); 10195 counter_u64_add(rack_sack_attacks_suspect, 1); 10196 rack->rc_suspicious = 1; 10197 rack_log_sad(rack, 4); 10198 if (tcp_bblogging_on(rack->rc_tp)) { 10199 union tcp_log_stackspecific log; 10200 struct timeval tv; 10201 10202 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 10203 log.u_bbr.flex1 = end; 10204 log.u_bbr.flex2 = start; 10205 log.u_bbr.flex3 = rsm->r_end; 10206 log.u_bbr.flex4 = rsm->r_start; 10207 log.u_bbr.flex5 = segsiz; 10208 log.u_bbr.flex6 = rsm->r_fas; 10209 log.u_bbr.flex7 = rsm->r_bas; 10210 log.u_bbr.flex8 = 5; 10211 log.u_bbr.pkts_out = rsm->r_flags; 10212 log.u_bbr.bbr_state = rack->rc_suspicious; 10213 log.u_bbr.bbr_substate = rsm->r_just_ret; 10214 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 10215 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 10216 TCP_LOG_EVENTP(rack->rc_tp, NULL, 10217 &rack->rc_inp->inp_socket->so_rcv, 10218 &rack->rc_inp->inp_socket->so_snd, 10219 TCP_SAD_DETECTION, 0, 10220 0, &log, false, &tv); 10221 } 10222 } 10223 /* You loose some ack count every time you sack 10224 * a small bit that is not butting to the end of 10225 * what we have sent. This is because we never 10226 * send small bits unless its the end of the sb. 10227 * Anyone sending a sack that is not at the end 10228 * is thus very very suspicious. 10229 */ 10230 loss = (segsiz/2) / (end - start); 10231 if (loss < rack->r_ctl.ack_count) 10232 rack->r_ctl.ack_count -= loss; 10233 else 10234 rack->r_ctl.ack_count = 0; 10235 } 10236 } 10237 first_time_through = 0; 10238 #endif 10239 /* Ok we have an ACK for some piece of this rsm */ 10240 if (rsm->r_start != start) { 10241 if ((rsm->r_flags & RACK_ACKED) == 0) { 10242 /* 10243 * Before any splitting or hookery is 10244 * done is it a TLP of interest i.e. rxt? 10245 */ 10246 if ((rsm->r_flags & RACK_TLP) && 10247 (rsm->r_rtr_cnt > 1)) { 10248 /* 10249 * We are splitting a rxt TLP, check 10250 * if we need to save off the start/end 10251 */ 10252 if (rack->rc_last_tlp_acked_set && 10253 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 10254 /* 10255 * We already turned this on since we are inside 10256 * the previous one was a partially sack now we 10257 * are getting another one (maybe all of it). 10258 * 10259 */ 10260 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 10261 /* 10262 * Lets make sure we have all of it though. 10263 */ 10264 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 10265 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10266 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10267 rack->r_ctl.last_tlp_acked_end); 10268 } 10269 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 10270 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10271 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10272 rack->r_ctl.last_tlp_acked_end); 10273 } 10274 } else { 10275 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10276 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10277 rack->rc_last_tlp_past_cumack = 0; 10278 rack->rc_last_tlp_acked_set = 1; 10279 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 10280 } 10281 } 10282 /** 10283 * Need to split this in two pieces the before and after, 10284 * the before remains in the map, the after must be 10285 * added. In other words we have: 10286 * rsm |--------------| 10287 * sackblk |-------> 10288 * rsm will become 10289 * rsm |---| 10290 * and nrsm will be the sacked piece 10291 * nrsm |----------| 10292 * 10293 * But before we start down that path lets 10294 * see if the sack spans over on top of 10295 * the next guy and it is already sacked. 10296 * 10297 */ 10298 /* 10299 * Hookery can only be used if the two entries 10300 * are in the same bucket and neither one of 10301 * them staddle the bucket line. 10302 */ 10303 next = tqhash_next(rack->r_ctl.tqh, rsm); 10304 if (next && 10305 (rsm->bindex == next->bindex) && 10306 ((rsm->r_flags & RACK_STRADDLE) == 0) && 10307 ((next->r_flags & RACK_STRADDLE) == 0) && 10308 ((rsm->r_flags & RACK_IS_PCM) == 0) && 10309 ((next->r_flags & RACK_IS_PCM) == 0) && 10310 (rsm->r_flags & RACK_IN_GP_WIN) && 10311 (next->r_flags & RACK_IN_GP_WIN)) 10312 can_use_hookery = 1; 10313 else 10314 can_use_hookery = 0; 10315 if (next && can_use_hookery && 10316 (next->r_flags & RACK_ACKED) && 10317 SEQ_GEQ(end, next->r_start)) { 10318 /** 10319 * So the next one is already acked, and 10320 * we can thus by hookery use our stack_map 10321 * to reflect the piece being sacked and 10322 * then adjust the two tree entries moving 10323 * the start and ends around. So we start like: 10324 * rsm |------------| (not-acked) 10325 * next |-----------| (acked) 10326 * sackblk |--------> 10327 * We want to end like so: 10328 * rsm |------| (not-acked) 10329 * next |-----------------| (acked) 10330 * nrsm |-----| 10331 * Where nrsm is a temporary stack piece we 10332 * use to update all the gizmos. 10333 */ 10334 /* Copy up our fudge block */ 10335 noextra++; 10336 nrsm = &stack_map; 10337 memcpy(nrsm, rsm, sizeof(struct rack_sendmap)); 10338 /* Now adjust our tree blocks */ 10339 tqhash_update_end(rack->r_ctl.tqh, rsm, start); 10340 next->r_start = start; 10341 rsm->r_flags |= RACK_SHUFFLED; 10342 next->r_flags |= RACK_SHUFFLED; 10343 /* Now we must adjust back where next->m is */ 10344 rack_setup_offset_for_rsm(rack, rsm, next); 10345 /* 10346 * Which timestamp do we keep? It is rather 10347 * important in GP measurements to have the 10348 * accurate end of the send window. 10349 * 10350 * We keep the largest value, which is the newest 10351 * send. We do this in case a segment that is 10352 * joined together and not part of a GP estimate 10353 * later gets expanded into the GP estimate. 10354 * 10355 * We prohibit the merging of unlike kinds i.e. 10356 * all pieces that are in the GP estimate can be 10357 * merged and all pieces that are not in a GP estimate 10358 * can be merged, but not disimilar pieces. Combine 10359 * this with taking the highest here and we should 10360 * be ok unless of course the client reneges. Then 10361 * all bets are off. 10362 */ 10363 if (next->r_tim_lastsent[(next->r_rtr_cnt-1)] < 10364 nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) 10365 next->r_tim_lastsent[(next->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]; 10366 /* 10367 * And we must keep the newest ack arrival time. 10368 */ 10369 if (next->r_ack_arrival < 10370 rack_to_usec_ts(&rack->r_ctl.act_rcv_time)) 10371 next->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 10372 10373 10374 /* We don't need to adjust rsm, it did not change */ 10375 /* Clear out the dup ack count of the remainder */ 10376 rsm->r_dupack = 0; 10377 rsm->r_just_ret = 0; 10378 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 10379 /* Now lets make sure our fudge block is right */ 10380 nrsm->r_start = start; 10381 /* Now lets update all the stats and such */ 10382 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0); 10383 if (rack->app_limited_needs_set) 10384 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END); 10385 changed += (nrsm->r_end - nrsm->r_start); 10386 /* You get a count for acking a whole segment or more */ 10387 if ((nrsm->r_end - nrsm->r_start) >= segsiz) 10388 rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz); 10389 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start); 10390 if (rsm->r_flags & RACK_WAS_LOST) { 10391 int my_chg; 10392 10393 my_chg = (nrsm->r_end - nrsm->r_start); 10394 KASSERT((rack->r_ctl.rc_considered_lost >= my_chg), 10395 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 10396 if (my_chg <= rack->r_ctl.rc_considered_lost) 10397 rack->r_ctl.rc_considered_lost -= my_chg; 10398 else 10399 rack->r_ctl.rc_considered_lost = 0; 10400 } 10401 if (nrsm->r_flags & RACK_SACK_PASSED) { 10402 rack->r_ctl.rc_reorder_ts = cts; 10403 if (rack->r_ctl.rc_reorder_ts == 0) 10404 rack->r_ctl.rc_reorder_ts = 1; 10405 } 10406 /* 10407 * Now we want to go up from rsm (the 10408 * one left un-acked) to the next one 10409 * in the tmap. We do this so when 10410 * we walk backwards we include marking 10411 * sack-passed on rsm (The one passed in 10412 * is skipped since it is generally called 10413 * on something sacked before removing it 10414 * from the tmap). 10415 */ 10416 if (rsm->r_in_tmap) { 10417 nrsm = TAILQ_NEXT(rsm, r_tnext); 10418 /* 10419 * Now that we have the next 10420 * one walk backwards from there. 10421 */ 10422 if (nrsm && nrsm->r_in_tmap) 10423 rack_log_sack_passed(tp, rack, nrsm, cts); 10424 } 10425 /* Now are we done? */ 10426 if (SEQ_LT(end, next->r_end) || 10427 (end == next->r_end)) { 10428 /* Done with block */ 10429 goto out; 10430 } 10431 rack_log_map_chg(tp, rack, &stack_map, rsm, next, MAP_SACK_M1, end, __LINE__); 10432 counter_u64_add(rack_sack_used_next_merge, 1); 10433 /* Postion for the next block */ 10434 start = next->r_end; 10435 rsm = tqhash_next(rack->r_ctl.tqh, next); 10436 if (rsm == NULL) 10437 goto out; 10438 } else { 10439 /** 10440 * We can't use any hookery here, so we 10441 * need to split the map. We enter like 10442 * so: 10443 * rsm |--------| 10444 * sackblk |-----> 10445 * We will add the new block nrsm and 10446 * that will be the new portion, and then 10447 * fall through after reseting rsm. So we 10448 * split and look like this: 10449 * rsm |----| 10450 * sackblk |-----> 10451 * nrsm |---| 10452 * We then fall through reseting 10453 * rsm to nrsm, so the next block 10454 * picks it up. 10455 */ 10456 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 10457 if (nrsm == NULL) { 10458 /* 10459 * failed XXXrrs what can we do but loose the sack 10460 * info? 10461 */ 10462 goto out; 10463 } 10464 counter_u64_add(rack_sack_splits, 1); 10465 rack_clone_rsm(rack, nrsm, rsm, start); 10466 moved++; 10467 rsm->r_just_ret = 0; 10468 #ifndef INVARIANTS 10469 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 10470 #else 10471 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 10472 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 10473 nrsm, insret, rack, rsm); 10474 } 10475 #endif 10476 if (rsm->r_in_tmap) { 10477 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 10478 nrsm->r_in_tmap = 1; 10479 } 10480 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M2, end, __LINE__); 10481 rsm->r_flags &= (~RACK_HAS_FIN); 10482 /* Position us to point to the new nrsm that starts the sack blk */ 10483 rsm = nrsm; 10484 } 10485 } else { 10486 /* Already sacked this piece */ 10487 counter_u64_add(rack_sack_skipped_acked, 1); 10488 moved++; 10489 if (end == rsm->r_end) { 10490 /* Done with block */ 10491 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 10492 goto out; 10493 } else if (SEQ_LT(end, rsm->r_end)) { 10494 /* A partial sack to a already sacked block */ 10495 moved++; 10496 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 10497 goto out; 10498 } else { 10499 /* 10500 * The end goes beyond this guy 10501 * reposition the start to the 10502 * next block. 10503 */ 10504 start = rsm->r_end; 10505 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 10506 if (rsm == NULL) 10507 goto out; 10508 } 10509 } 10510 } 10511 if (SEQ_GEQ(end, rsm->r_end)) { 10512 /** 10513 * The end of this block is either beyond this guy or right 10514 * at this guy. I.e.: 10515 * rsm --- |-----| 10516 * end |-----| 10517 * <or> 10518 * end |---------| 10519 */ 10520 if ((rsm->r_flags & RACK_ACKED) == 0) { 10521 /* 10522 * Is it a TLP of interest? 10523 */ 10524 if ((rsm->r_flags & RACK_TLP) && 10525 (rsm->r_rtr_cnt > 1)) { 10526 /* 10527 * We are splitting a rxt TLP, check 10528 * if we need to save off the start/end 10529 */ 10530 if (rack->rc_last_tlp_acked_set && 10531 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 10532 /* 10533 * We already turned this on since we are inside 10534 * the previous one was a partially sack now we 10535 * are getting another one (maybe all of it). 10536 */ 10537 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 10538 /* 10539 * Lets make sure we have all of it though. 10540 */ 10541 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 10542 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10543 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10544 rack->r_ctl.last_tlp_acked_end); 10545 } 10546 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 10547 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10548 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10549 rack->r_ctl.last_tlp_acked_end); 10550 } 10551 } else { 10552 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10553 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10554 rack->rc_last_tlp_past_cumack = 0; 10555 rack->rc_last_tlp_acked_set = 1; 10556 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 10557 } 10558 } 10559 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0); 10560 changed += (rsm->r_end - rsm->r_start); 10561 /* You get a count for acking a whole segment or more */ 10562 if ((rsm->r_end - rsm->r_start) >= segsiz) 10563 rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz); 10564 if (rsm->r_flags & RACK_WAS_LOST) { 10565 int my_chg; 10566 10567 my_chg = (rsm->r_end - rsm->r_start); 10568 rsm->r_flags &= ~RACK_WAS_LOST; 10569 KASSERT((rack->r_ctl.rc_considered_lost >= my_chg), 10570 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 10571 if (my_chg <= rack->r_ctl.rc_considered_lost) 10572 rack->r_ctl.rc_considered_lost -= my_chg; 10573 else 10574 rack->r_ctl.rc_considered_lost = 0; 10575 } 10576 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 10577 if (rsm->r_in_tmap) /* should be true */ 10578 rack_log_sack_passed(tp, rack, rsm, cts); 10579 /* Is Reordering occuring? */ 10580 if (rsm->r_flags & RACK_SACK_PASSED) { 10581 rsm->r_flags &= ~RACK_SACK_PASSED; 10582 rack->r_ctl.rc_reorder_ts = cts; 10583 if (rack->r_ctl.rc_reorder_ts == 0) 10584 rack->r_ctl.rc_reorder_ts = 1; 10585 } 10586 if (rack->app_limited_needs_set) 10587 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END); 10588 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 10589 rsm->r_flags |= RACK_ACKED; 10590 rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end); 10591 if (rsm->r_in_tmap) { 10592 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 10593 rsm->r_in_tmap = 0; 10594 } 10595 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_SACK_M3, end, __LINE__); 10596 } else { 10597 counter_u64_add(rack_sack_skipped_acked, 1); 10598 moved++; 10599 } 10600 if (end == rsm->r_end) { 10601 /* This block only - done, setup for next */ 10602 goto out; 10603 } 10604 /* 10605 * There is more not coverend by this rsm move on 10606 * to the next block in the tail queue hash table. 10607 */ 10608 nrsm = tqhash_next(rack->r_ctl.tqh, rsm); 10609 start = rsm->r_end; 10610 rsm = nrsm; 10611 if (rsm == NULL) 10612 goto out; 10613 goto do_rest_ofb; 10614 } 10615 /** 10616 * The end of this sack block is smaller than 10617 * our rsm i.e.: 10618 * rsm --- |-----| 10619 * end |--| 10620 */ 10621 if ((rsm->r_flags & RACK_ACKED) == 0) { 10622 /* 10623 * Is it a TLP of interest? 10624 */ 10625 if ((rsm->r_flags & RACK_TLP) && 10626 (rsm->r_rtr_cnt > 1)) { 10627 /* 10628 * We are splitting a rxt TLP, check 10629 * if we need to save off the start/end 10630 */ 10631 if (rack->rc_last_tlp_acked_set && 10632 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 10633 /* 10634 * We already turned this on since we are inside 10635 * the previous one was a partially sack now we 10636 * are getting another one (maybe all of it). 10637 */ 10638 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 10639 /* 10640 * Lets make sure we have all of it though. 10641 */ 10642 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 10643 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10644 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10645 rack->r_ctl.last_tlp_acked_end); 10646 } 10647 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 10648 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10649 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10650 rack->r_ctl.last_tlp_acked_end); 10651 } 10652 } else { 10653 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10654 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10655 rack->rc_last_tlp_past_cumack = 0; 10656 rack->rc_last_tlp_acked_set = 1; 10657 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 10658 } 10659 } 10660 /* 10661 * Hookery can only be used if the two entries 10662 * are in the same bucket and neither one of 10663 * them staddle the bucket line. 10664 */ 10665 prev = tqhash_prev(rack->r_ctl.tqh, rsm); 10666 if (prev && 10667 (rsm->bindex == prev->bindex) && 10668 ((rsm->r_flags & RACK_STRADDLE) == 0) && 10669 ((prev->r_flags & RACK_STRADDLE) == 0) && 10670 ((rsm->r_flags & RACK_IS_PCM) == 0) && 10671 ((prev->r_flags & RACK_IS_PCM) == 0) && 10672 (rsm->r_flags & RACK_IN_GP_WIN) && 10673 (prev->r_flags & RACK_IN_GP_WIN)) 10674 can_use_hookery = 1; 10675 else 10676 can_use_hookery = 0; 10677 if (prev && can_use_hookery && 10678 (prev->r_flags & RACK_ACKED)) { 10679 /** 10680 * Goal, we want the right remainder of rsm to shrink 10681 * in place and span from (rsm->r_start = end) to rsm->r_end. 10682 * We want to expand prev to go all the way 10683 * to prev->r_end <- end. 10684 * so in the tree we have before: 10685 * prev |--------| (acked) 10686 * rsm |-------| (non-acked) 10687 * sackblk |-| 10688 * We churn it so we end up with 10689 * prev |----------| (acked) 10690 * rsm |-----| (non-acked) 10691 * nrsm |-| (temporary) 10692 * 10693 * Note if either prev/rsm is a TLP we don't 10694 * do this. 10695 */ 10696 noextra++; 10697 nrsm = &stack_map; 10698 memcpy(nrsm, rsm, sizeof(struct rack_sendmap)); 10699 tqhash_update_end(rack->r_ctl.tqh, prev, end); 10700 rsm->r_start = end; 10701 rsm->r_flags |= RACK_SHUFFLED; 10702 prev->r_flags |= RACK_SHUFFLED; 10703 /* Now adjust nrsm (stack copy) to be 10704 * the one that is the small 10705 * piece that was "sacked". 10706 */ 10707 nrsm->r_end = end; 10708 rsm->r_dupack = 0; 10709 /* 10710 * Which timestamp do we keep? It is rather 10711 * important in GP measurements to have the 10712 * accurate end of the send window. 10713 * 10714 * We keep the largest value, which is the newest 10715 * send. We do this in case a segment that is 10716 * joined together and not part of a GP estimate 10717 * later gets expanded into the GP estimate. 10718 * 10719 * We prohibit the merging of unlike kinds i.e. 10720 * all pieces that are in the GP estimate can be 10721 * merged and all pieces that are not in a GP estimate 10722 * can be merged, but not disimilar pieces. Combine 10723 * this with taking the highest here and we should 10724 * be ok unless of course the client reneges. Then 10725 * all bets are off. 10726 */ 10727 if(prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] < 10728 nrsm->r_tim_lastsent[(nrsm->r_rtr_cnt-1)]) { 10729 prev->r_tim_lastsent[(prev->r_rtr_cnt-1)] = nrsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]; 10730 } 10731 /* 10732 * And we must keep the newest ack arrival time. 10733 */ 10734 10735 if(prev->r_ack_arrival < 10736 rack_to_usec_ts(&rack->r_ctl.act_rcv_time)) 10737 prev->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 10738 10739 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 10740 /* 10741 * Now that the rsm has had its start moved forward 10742 * lets go ahead and get its new place in the world. 10743 */ 10744 rack_setup_offset_for_rsm(rack, prev, rsm); 10745 /* 10746 * Now nrsm is our new little piece 10747 * that is acked (which was merged 10748 * to prev). Update the rtt and changed 10749 * based on that. Also check for reordering. 10750 */ 10751 rack_update_rtt(tp, rack, nrsm, to, cts, SACKED, 0); 10752 if (rack->app_limited_needs_set) 10753 rack_need_set_test(tp, rack, nrsm, tp->snd_una, __LINE__, RACK_USE_END); 10754 changed += (nrsm->r_end - nrsm->r_start); 10755 /* You get a count for acking a whole segment or more */ 10756 if ((nrsm->r_end - nrsm->r_start) >= segsiz) 10757 rack->r_ctl.ack_count += ((nrsm->r_end - nrsm->r_start) / segsiz); 10758 10759 rack->r_ctl.rc_sacked += (nrsm->r_end - nrsm->r_start); 10760 if (rsm->r_flags & RACK_WAS_LOST) { 10761 int my_chg; 10762 10763 my_chg = (nrsm->r_end - nrsm->r_start); 10764 KASSERT((rack->r_ctl.rc_considered_lost >= my_chg), 10765 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 10766 if (my_chg <= rack->r_ctl.rc_considered_lost) 10767 rack->r_ctl.rc_considered_lost -= my_chg; 10768 else 10769 rack->r_ctl.rc_considered_lost = 0; 10770 } 10771 if (nrsm->r_flags & RACK_SACK_PASSED) { 10772 rack->r_ctl.rc_reorder_ts = cts; 10773 if (rack->r_ctl.rc_reorder_ts == 0) 10774 rack->r_ctl.rc_reorder_ts = 1; 10775 } 10776 rack_log_map_chg(tp, rack, prev, &stack_map, rsm, MAP_SACK_M4, end, __LINE__); 10777 rsm = prev; 10778 counter_u64_add(rack_sack_used_prev_merge, 1); 10779 } else { 10780 /** 10781 * This is the case where our previous 10782 * block is not acked either, so we must 10783 * split the block in two. 10784 */ 10785 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 10786 if (nrsm == NULL) { 10787 /* failed rrs what can we do but loose the sack info? */ 10788 goto out; 10789 } 10790 if ((rsm->r_flags & RACK_TLP) && 10791 (rsm->r_rtr_cnt > 1)) { 10792 /* 10793 * We are splitting a rxt TLP, check 10794 * if we need to save off the start/end 10795 */ 10796 if (rack->rc_last_tlp_acked_set && 10797 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 10798 /* 10799 * We already turned this on since this block is inside 10800 * the previous one was a partially sack now we 10801 * are getting another one (maybe all of it). 10802 */ 10803 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 10804 /* 10805 * Lets make sure we have all of it though. 10806 */ 10807 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 10808 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10809 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10810 rack->r_ctl.last_tlp_acked_end); 10811 } 10812 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 10813 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10814 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 10815 rack->r_ctl.last_tlp_acked_end); 10816 } 10817 } else { 10818 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 10819 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 10820 rack->rc_last_tlp_acked_set = 1; 10821 rack->rc_last_tlp_past_cumack = 0; 10822 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 10823 } 10824 } 10825 /** 10826 * In this case nrsm becomes 10827 * nrsm->r_start = end; 10828 * nrsm->r_end = rsm->r_end; 10829 * which is un-acked. 10830 * <and> 10831 * rsm->r_end = nrsm->r_start; 10832 * i.e. the remaining un-acked 10833 * piece is left on the left 10834 * hand side. 10835 * 10836 * So we start like this 10837 * rsm |----------| (not acked) 10838 * sackblk |---| 10839 * build it so we have 10840 * rsm |---| (acked) 10841 * nrsm |------| (not acked) 10842 */ 10843 counter_u64_add(rack_sack_splits, 1); 10844 rack_clone_rsm(rack, nrsm, rsm, end); 10845 moved++; 10846 rsm->r_flags &= (~RACK_HAS_FIN); 10847 rsm->r_just_ret = 0; 10848 #ifndef INVARIANTS 10849 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 10850 #else 10851 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 10852 panic("Insert in tailq_hash of %p fails ret:% rack:%p rsm:%p", 10853 nrsm, insret, rack, rsm); 10854 } 10855 #endif 10856 if (rsm->r_in_tmap) { 10857 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 10858 nrsm->r_in_tmap = 1; 10859 } 10860 nrsm->r_dupack = 0; 10861 rack_log_retran_reason(rack, nrsm, __LINE__, 0, 2); 10862 rack_update_rtt(tp, rack, rsm, to, cts, SACKED, 0); 10863 changed += (rsm->r_end - rsm->r_start); 10864 /* You get a count for acking a whole segment or more */ 10865 if ((rsm->r_end - rsm->r_start) >= segsiz) 10866 rack->r_ctl.ack_count += ((rsm->r_end - rsm->r_start) / segsiz); 10867 if (rsm->r_flags & RACK_WAS_LOST) { 10868 int my_chg; 10869 10870 my_chg = (rsm->r_end - rsm->r_start); 10871 rsm->r_flags &= ~RACK_WAS_LOST; 10872 KASSERT((rack->r_ctl.rc_considered_lost >= my_chg), 10873 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 10874 if (my_chg <= rack->r_ctl.rc_considered_lost) 10875 rack->r_ctl.rc_considered_lost -= my_chg; 10876 else 10877 rack->r_ctl.rc_considered_lost = 0; 10878 } 10879 rack->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 10880 10881 if (rsm->r_in_tmap) /* should be true */ 10882 rack_log_sack_passed(tp, rack, rsm, cts); 10883 /* Is Reordering occuring? */ 10884 if (rsm->r_flags & RACK_SACK_PASSED) { 10885 rsm->r_flags &= ~RACK_SACK_PASSED; 10886 rack->r_ctl.rc_reorder_ts = cts; 10887 if (rack->r_ctl.rc_reorder_ts == 0) 10888 rack->r_ctl.rc_reorder_ts = 1; 10889 } 10890 if (rack->app_limited_needs_set) 10891 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_END); 10892 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 10893 rsm->r_flags |= RACK_ACKED; 10894 rack_update_pcm_ack(rack, 0, rsm->r_start, rsm->r_end); 10895 rack_log_map_chg(tp, rack, NULL, rsm, nrsm, MAP_SACK_M5, end, __LINE__); 10896 if (rsm->r_in_tmap) { 10897 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 10898 rsm->r_in_tmap = 0; 10899 } 10900 } 10901 } else if (start != end){ 10902 /* 10903 * The block was already acked. 10904 */ 10905 counter_u64_add(rack_sack_skipped_acked, 1); 10906 moved++; 10907 } 10908 out: 10909 if (rsm && 10910 ((rsm->r_flags & RACK_TLP) == 0) && 10911 (rsm->r_flags & RACK_ACKED)) { 10912 /* 10913 * Now can we merge where we worked 10914 * with either the previous or 10915 * next block? 10916 */ 10917 next = tqhash_next(rack->r_ctl.tqh, rsm); 10918 while (next) { 10919 if (next->r_flags & RACK_TLP) 10920 break; 10921 /* Only allow merges between ones in or out of GP window */ 10922 if ((next->r_flags & RACK_IN_GP_WIN) && 10923 ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) { 10924 break; 10925 } 10926 if ((rsm->r_flags & RACK_IN_GP_WIN) && 10927 ((next->r_flags & RACK_IN_GP_WIN) == 0)) { 10928 break; 10929 } 10930 if (rsm->bindex != next->bindex) 10931 break; 10932 if (rsm->r_flags & RACK_STRADDLE) 10933 break; 10934 if (rsm->r_flags & RACK_IS_PCM) 10935 break; 10936 if (next->r_flags & RACK_STRADDLE) 10937 break; 10938 if (next->r_flags & RACK_IS_PCM) 10939 break; 10940 if (next->r_flags & RACK_ACKED) { 10941 /* yep this and next can be merged */ 10942 rsm = rack_merge_rsm(rack, rsm, next); 10943 noextra++; 10944 next = tqhash_next(rack->r_ctl.tqh, rsm); 10945 } else 10946 break; 10947 } 10948 /* Now what about the previous? */ 10949 prev = tqhash_prev(rack->r_ctl.tqh, rsm); 10950 while (prev) { 10951 if (prev->r_flags & RACK_TLP) 10952 break; 10953 /* Only allow merges between ones in or out of GP window */ 10954 if ((prev->r_flags & RACK_IN_GP_WIN) && 10955 ((rsm->r_flags & RACK_IN_GP_WIN) == 0)) { 10956 break; 10957 } 10958 if ((rsm->r_flags & RACK_IN_GP_WIN) && 10959 ((prev->r_flags & RACK_IN_GP_WIN) == 0)) { 10960 break; 10961 } 10962 if (rsm->bindex != prev->bindex) 10963 break; 10964 if (rsm->r_flags & RACK_STRADDLE) 10965 break; 10966 if (rsm->r_flags & RACK_IS_PCM) 10967 break; 10968 if (prev->r_flags & RACK_STRADDLE) 10969 break; 10970 if (prev->r_flags & RACK_IS_PCM) 10971 break; 10972 if (prev->r_flags & RACK_ACKED) { 10973 /* yep the previous and this can be merged */ 10974 rsm = rack_merge_rsm(rack, prev, rsm); 10975 noextra++; 10976 prev = tqhash_prev(rack->r_ctl.tqh, rsm); 10977 } else 10978 break; 10979 } 10980 } 10981 if (used_ref == 0) { 10982 counter_u64_add(rack_sack_proc_all, 1); 10983 } else { 10984 counter_u64_add(rack_sack_proc_short, 1); 10985 } 10986 /* Save off the next one for quick reference. */ 10987 nrsm = tqhash_find(rack->r_ctl.tqh, end); 10988 *prsm = rack->r_ctl.rc_sacklast = nrsm; 10989 /* Pass back the moved. */ 10990 *moved_two = moved; 10991 *no_extra = noextra; 10992 if (IN_RECOVERY(tp->t_flags)) { 10993 rack->r_ctl.bytes_acked_in_recovery += changed; 10994 } 10995 return (changed); 10996 } 10997 10998 static void inline 10999 rack_peer_reneges(struct tcp_rack *rack, struct rack_sendmap *rsm, tcp_seq th_ack) 11000 { 11001 struct rack_sendmap *tmap; 11002 11003 tmap = NULL; 11004 while (rsm && (rsm->r_flags & RACK_ACKED)) { 11005 /* Its no longer sacked, mark it so */ 11006 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 11007 #ifdef INVARIANTS 11008 if (rsm->r_in_tmap) { 11009 panic("rack:%p rsm:%p flags:0x%x in tmap?", 11010 rack, rsm, rsm->r_flags); 11011 } 11012 #endif 11013 rsm->r_flags &= ~(RACK_ACKED|RACK_SACK_PASSED|RACK_WAS_SACKPASS); 11014 /* Rebuild it into our tmap */ 11015 if (tmap == NULL) { 11016 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 11017 tmap = rsm; 11018 } else { 11019 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, tmap, rsm, r_tnext); 11020 tmap = rsm; 11021 } 11022 tmap->r_in_tmap = 1; 11023 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 11024 } 11025 /* 11026 * Now lets possibly clear the sack filter so we start 11027 * recognizing sacks that cover this area. 11028 */ 11029 sack_filter_clear(&rack->r_ctl.rack_sf, th_ack); 11030 11031 } 11032 11033 static void 11034 rack_do_decay(struct tcp_rack *rack) 11035 { 11036 struct timeval res; 11037 11038 #define timersub(tvp, uvp, vvp) \ 11039 do { \ 11040 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 11041 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 11042 if ((vvp)->tv_usec < 0) { \ 11043 (vvp)->tv_sec--; \ 11044 (vvp)->tv_usec += 1000000; \ 11045 } \ 11046 } while (0) 11047 11048 timersub(&rack->r_ctl.act_rcv_time, &rack->r_ctl.rc_last_time_decay, &res); 11049 #undef timersub 11050 11051 rack->r_ctl.input_pkt++; 11052 if ((rack->rc_in_persist) || 11053 (res.tv_sec >= 1) || 11054 (rack->rc_tp->snd_max == rack->rc_tp->snd_una)) { 11055 /* 11056 * Check for decay of non-SAD, 11057 * we want all SAD detection metrics to 11058 * decay 1/4 per second (or more) passed. 11059 * Current default is 800 so it decays 11060 * 80% every second. 11061 */ 11062 #ifdef TCP_SAD_DETECTION 11063 uint32_t pkt_delta; 11064 11065 pkt_delta = rack->r_ctl.input_pkt - rack->r_ctl.saved_input_pkt; 11066 #endif 11067 /* Update our saved tracking values */ 11068 rack->r_ctl.saved_input_pkt = rack->r_ctl.input_pkt; 11069 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time; 11070 /* Now do we escape without decay? */ 11071 #ifdef TCP_SAD_DETECTION 11072 if (rack->rc_in_persist || 11073 (rack->rc_tp->snd_max == rack->rc_tp->snd_una) || 11074 (pkt_delta < tcp_sad_low_pps)){ 11075 /* 11076 * We don't decay idle connections 11077 * or ones that have a low input pps. 11078 */ 11079 return; 11080 } 11081 /* Decay the counters */ 11082 rack->r_ctl.ack_count = ctf_decay_count(rack->r_ctl.ack_count, 11083 tcp_sad_decay_val); 11084 rack->r_ctl.sack_count = ctf_decay_count(rack->r_ctl.sack_count, 11085 tcp_sad_decay_val); 11086 rack->r_ctl.sack_moved_extra = ctf_decay_count(rack->r_ctl.sack_moved_extra, 11087 tcp_sad_decay_val); 11088 rack->r_ctl.sack_noextra_move = ctf_decay_count(rack->r_ctl.sack_noextra_move, 11089 tcp_sad_decay_val); 11090 #endif 11091 } 11092 } 11093 11094 static void inline 11095 rack_rsm_sender_update(struct tcp_rack *rack, struct tcpcb *tp, struct rack_sendmap *rsm, uint8_t from) 11096 { 11097 /* 11098 * We look at advancing the end send time for our GP 11099 * measurement tracking only as the cumulative acknowledgment 11100 * moves forward. You might wonder about this, why not 11101 * at every transmission or retransmission within the 11102 * GP window update the rc_gp_cumack_ts? Well its rather 11103 * nuanced but basically the GP window *may* expand (as 11104 * it does below) or worse and harder to track it may shrink. 11105 * 11106 * This last makes it impossible to track at the time of 11107 * the send, since you may set forward your rc_gp_cumack_ts 11108 * when you send, because that send *is* in your currently 11109 * "guessed" window, but then it shrinks. Now which was 11110 * the send time of the last bytes in the window, by the 11111 * time you ask that question that part of the sendmap 11112 * is freed. So you don't know and you will have too 11113 * long of send window. Instead by updating the time 11114 * marker only when the cumack advances this assures us 11115 * that we will have only the sends in the window of our 11116 * GP measurement. 11117 * 11118 * Another complication from this is the 11119 * merging of sendmap entries. During SACK processing this 11120 * can happen to conserve the sendmap size. That breaks 11121 * everything down in tracking the send window of the GP 11122 * estimate. So to prevent that and keep it working with 11123 * a tiny bit more limited merging, we only allow like 11124 * types to be merged. I.e. if two sends are in the GP window 11125 * then its ok to merge them together. If two sends are not 11126 * in the GP window its ok to merge them together too. Though 11127 * one send in and one send out cannot be merged. We combine 11128 * this with never allowing the shrinking of the GP window when 11129 * we are in recovery so that we can properly calculate the 11130 * sending times. 11131 * 11132 * This all of course seems complicated, because it is.. :) 11133 * 11134 * The cum-ack is being advanced upon the sendmap. 11135 * If we are not doing a GP estimate don't 11136 * proceed. 11137 */ 11138 uint64_t ts; 11139 11140 if ((tp->t_flags & TF_GPUTINPROG) == 0) 11141 return; 11142 /* 11143 * If this sendmap entry is going 11144 * beyond the measurement window we had picked, 11145 * expand the measurement window by that much. 11146 */ 11147 if (SEQ_GT(rsm->r_end, tp->gput_ack)) { 11148 tp->gput_ack = rsm->r_end; 11149 } 11150 /* 11151 * If we have not setup a ack, then we 11152 * have no idea if the newly acked pieces 11153 * will be "in our seq measurement range". If 11154 * it is when we clear the app_limited_needs_set 11155 * flag the timestamp will be updated. 11156 */ 11157 if (rack->app_limited_needs_set) 11158 return; 11159 /* 11160 * Finally, we grab out the latest timestamp 11161 * that this packet was sent and then see 11162 * if: 11163 * a) The packet touches are newly defined GP range. 11164 * b) The time is greater than (newer) than the 11165 * one we currently have. If so we update 11166 * our sending end time window. 11167 * 11168 * Note we *do not* do this at send time. The reason 11169 * is that if you do you *may* pick up a newer timestamp 11170 * for a range you are not going to measure. We project 11171 * out how far and then sometimes modify that to be 11172 * smaller. If that occurs then you will have a send 11173 * that does not belong to the range included. 11174 */ 11175 if ((ts = rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) <= 11176 rack->r_ctl.rc_gp_cumack_ts) 11177 return; 11178 if (rack_in_gp_window(tp, rsm)) { 11179 rack->r_ctl.rc_gp_cumack_ts = ts; 11180 rack_log_gpset(rack, tp->gput_ack, (uint32_t)ts, rsm->r_end, 11181 __LINE__, from, rsm); 11182 } 11183 } 11184 11185 static void 11186 rack_process_to_cumack(struct tcpcb *tp, struct tcp_rack *rack, register uint32_t th_ack, uint32_t cts, struct tcpopt *to, uint64_t acktime) 11187 { 11188 struct rack_sendmap *rsm; 11189 /* 11190 * The ACK point is advancing to th_ack, we must drop off 11191 * the packets in the rack log and calculate any eligble 11192 * RTT's. 11193 */ 11194 11195 if (sack_filter_blks_used(&rack->r_ctl.rack_sf)) { 11196 /* 11197 * If we have some sack blocks in the filter 11198 * lets prune them out by calling sfb with no blocks. 11199 */ 11200 sack_filter_blks(&rack->r_ctl.rack_sf, NULL, 0, th_ack); 11201 } 11202 if (SEQ_GT(th_ack, tp->snd_una)) { 11203 /* Clear any app ack remembered settings */ 11204 rack->r_ctl.cleared_app_ack = 0; 11205 } 11206 rack->r_wanted_output = 1; 11207 if (SEQ_GT(th_ack, tp->snd_una)) 11208 rack->r_ctl.last_cumack_advance = acktime; 11209 11210 /* Tend any TLP that has been marked for 1/2 the seq space (its old) */ 11211 if ((rack->rc_last_tlp_acked_set == 1)&& 11212 (rack->rc_last_tlp_past_cumack == 1) && 11213 (SEQ_GT(rack->r_ctl.last_tlp_acked_start, th_ack))) { 11214 /* 11215 * We have reached the point where our last rack 11216 * tlp retransmit sequence is ahead of the cum-ack. 11217 * This can only happen when the cum-ack moves all 11218 * the way around (its been a full 2^^31+1 bytes 11219 * or more since we sent a retransmitted TLP). Lets 11220 * turn off the valid flag since its not really valid. 11221 * 11222 * Note since sack's also turn on this event we have 11223 * a complication, we have to wait to age it out until 11224 * the cum-ack is by the TLP before checking which is 11225 * what the next else clause does. 11226 */ 11227 rack_log_dsack_event(rack, 9, __LINE__, 11228 rack->r_ctl.last_tlp_acked_start, 11229 rack->r_ctl.last_tlp_acked_end); 11230 rack->rc_last_tlp_acked_set = 0; 11231 rack->rc_last_tlp_past_cumack = 0; 11232 } else if ((rack->rc_last_tlp_acked_set == 1) && 11233 (rack->rc_last_tlp_past_cumack == 0) && 11234 (SEQ_GEQ(th_ack, rack->r_ctl.last_tlp_acked_end))) { 11235 /* 11236 * It is safe to start aging TLP's out. 11237 */ 11238 rack->rc_last_tlp_past_cumack = 1; 11239 } 11240 /* We do the same for the tlp send seq as well */ 11241 if ((rack->rc_last_sent_tlp_seq_valid == 1) && 11242 (rack->rc_last_sent_tlp_past_cumack == 1) && 11243 (SEQ_GT(rack->r_ctl.last_sent_tlp_seq, th_ack))) { 11244 rack_log_dsack_event(rack, 9, __LINE__, 11245 rack->r_ctl.last_sent_tlp_seq, 11246 (rack->r_ctl.last_sent_tlp_seq + 11247 rack->r_ctl.last_sent_tlp_len)); 11248 rack->rc_last_sent_tlp_seq_valid = 0; 11249 rack->rc_last_sent_tlp_past_cumack = 0; 11250 } else if ((rack->rc_last_sent_tlp_seq_valid == 1) && 11251 (rack->rc_last_sent_tlp_past_cumack == 0) && 11252 (SEQ_GEQ(th_ack, rack->r_ctl.last_sent_tlp_seq))) { 11253 /* 11254 * It is safe to start aging TLP's send. 11255 */ 11256 rack->rc_last_sent_tlp_past_cumack = 1; 11257 } 11258 more: 11259 rsm = tqhash_min(rack->r_ctl.tqh); 11260 if (rsm == NULL) { 11261 if ((th_ack - 1) == tp->iss) { 11262 /* 11263 * For the SYN incoming case we will not 11264 * have called tcp_output for the sending of 11265 * the SYN, so there will be no map. All 11266 * other cases should probably be a panic. 11267 */ 11268 return; 11269 } 11270 if (tp->t_flags & TF_SENTFIN) { 11271 /* if we sent a FIN we often will not have map */ 11272 return; 11273 } 11274 #ifdef INVARIANTS 11275 panic("No rack map tp:%p for state:%d ack:%u rack:%p snd_una:%u snd_max:%u\n", 11276 tp, 11277 tp->t_state, th_ack, rack, 11278 tp->snd_una, tp->snd_max); 11279 #endif 11280 return; 11281 } 11282 if (SEQ_LT(th_ack, rsm->r_start)) { 11283 /* Huh map is missing this */ 11284 #ifdef INVARIANTS 11285 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d\n", 11286 rsm->r_start, 11287 th_ack, tp->t_state, rack->r_state); 11288 #endif 11289 return; 11290 } 11291 rack_update_rtt(tp, rack, rsm, to, cts, CUM_ACKED, th_ack); 11292 11293 /* Now was it a retransmitted TLP? */ 11294 if ((rsm->r_flags & RACK_TLP) && 11295 (rsm->r_rtr_cnt > 1)) { 11296 /* 11297 * Yes, this rsm was a TLP and retransmitted, remember that 11298 * since if a DSACK comes back on this we don't want 11299 * to think of it as a reordered segment. This may 11300 * get updated again with possibly even other TLPs 11301 * in flight, but thats ok. Only when we don't send 11302 * a retransmitted TLP for 1/2 the sequences space 11303 * will it get turned off (above). 11304 */ 11305 if (rack->rc_last_tlp_acked_set && 11306 (is_rsm_inside_declared_tlp_block(rack, rsm))) { 11307 /* 11308 * We already turned this on since the end matches, 11309 * the previous one was a partially ack now we 11310 * are getting another one (maybe all of it). 11311 */ 11312 rack_log_dsack_event(rack, 10, __LINE__, rsm->r_start, rsm->r_end); 11313 /* 11314 * Lets make sure we have all of it though. 11315 */ 11316 if (SEQ_LT(rsm->r_start, rack->r_ctl.last_tlp_acked_start)) { 11317 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 11318 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 11319 rack->r_ctl.last_tlp_acked_end); 11320 } 11321 if (SEQ_GT(rsm->r_end, rack->r_ctl.last_tlp_acked_end)) { 11322 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 11323 rack_log_dsack_event(rack, 11, __LINE__, rack->r_ctl.last_tlp_acked_start, 11324 rack->r_ctl.last_tlp_acked_end); 11325 } 11326 } else { 11327 rack->rc_last_tlp_past_cumack = 1; 11328 rack->r_ctl.last_tlp_acked_start = rsm->r_start; 11329 rack->r_ctl.last_tlp_acked_end = rsm->r_end; 11330 rack->rc_last_tlp_acked_set = 1; 11331 rack_log_dsack_event(rack, 8, __LINE__, rsm->r_start, rsm->r_end); 11332 } 11333 } 11334 /* Now do we consume the whole thing? */ 11335 rack->r_ctl.last_tmit_time_acked = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 11336 if (SEQ_GEQ(th_ack, rsm->r_end)) { 11337 /* Its all consumed. */ 11338 uint32_t left; 11339 uint8_t newly_acked; 11340 11341 if (rsm->r_flags & RACK_WAS_LOST) { 11342 /* 11343 * This can happen when we marked it as lost 11344 * and yet before retransmitting we get an ack 11345 * which can happen due to reordering. 11346 */ 11347 rsm->r_flags &= ~RACK_WAS_LOST; 11348 KASSERT((rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)), 11349 ("rsm:%p rack:%p rc_considered_lost goes negative", rsm, rack)); 11350 if (rack->r_ctl.rc_considered_lost >= (rsm->r_end - rsm->r_start)) 11351 rack->r_ctl.rc_considered_lost -= rsm->r_end - rsm->r_start; 11352 else 11353 rack->r_ctl.rc_considered_lost = 0; 11354 } 11355 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_FREE, rsm->r_end, __LINE__); 11356 rack->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 11357 rsm->r_rtr_bytes = 0; 11358 /* 11359 * Record the time of highest cumack sent if its in our measurement 11360 * window and possibly bump out the end. 11361 */ 11362 rack_rsm_sender_update(rack, tp, rsm, 4); 11363 tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK); 11364 if (rsm->r_in_tmap) { 11365 TAILQ_REMOVE(&rack->r_ctl.rc_tmap, rsm, r_tnext); 11366 rsm->r_in_tmap = 0; 11367 } 11368 newly_acked = 1; 11369 if (((rsm->r_flags & RACK_ACKED) == 0) && 11370 (IN_RECOVERY(tp->t_flags))) { 11371 rack->r_ctl.bytes_acked_in_recovery += (rsm->r_end - rsm->r_start); 11372 } 11373 if (rsm->r_flags & RACK_ACKED) { 11374 /* 11375 * It was acked on the scoreboard -- remove 11376 * it from total 11377 */ 11378 rack->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 11379 newly_acked = 0; 11380 } else if (rsm->r_flags & RACK_SACK_PASSED) { 11381 /* 11382 * There are segments ACKED on the 11383 * scoreboard further up. We are seeing 11384 * reordering. 11385 */ 11386 rsm->r_flags &= ~RACK_SACK_PASSED; 11387 rsm->r_ack_arrival = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 11388 rsm->r_flags |= RACK_ACKED; 11389 rack->r_ctl.rc_reorder_ts = cts; 11390 if (rack->r_ctl.rc_reorder_ts == 0) 11391 rack->r_ctl.rc_reorder_ts = 1; 11392 if (rack->r_ent_rec_ns) { 11393 /* 11394 * We have sent no more, and we saw an sack 11395 * then ack arrive. 11396 */ 11397 rack->r_might_revert = 1; 11398 } 11399 rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end); 11400 } else { 11401 rack_update_pcm_ack(rack, 1, rsm->r_start, rsm->r_end); 11402 } 11403 if ((rsm->r_flags & RACK_TO_REXT) && 11404 (tp->t_flags & TF_RCVD_TSTMP) && 11405 (to->to_flags & TOF_TS) && 11406 (to->to_tsecr != 0) && 11407 (tp->t_flags & TF_PREVVALID)) { 11408 /* 11409 * We can use the timestamp to see 11410 * if this retransmission was from the 11411 * first transmit. If so we made a mistake. 11412 */ 11413 tp->t_flags &= ~TF_PREVVALID; 11414 if (to->to_tsecr == rack_ts_to_msec(rsm->r_tim_lastsent[0])) { 11415 /* The first transmit is what this ack is for */ 11416 rack_cong_signal(tp, CC_RTO_ERR, th_ack, __LINE__); 11417 } 11418 } 11419 left = th_ack - rsm->r_end; 11420 if (rack->app_limited_needs_set && newly_acked) 11421 rack_need_set_test(tp, rack, rsm, th_ack, __LINE__, RACK_USE_END_OR_THACK); 11422 /* Free back to zone */ 11423 rack_free(rack, rsm); 11424 if (left) { 11425 goto more; 11426 } 11427 /* Check for reneging */ 11428 rsm = tqhash_min(rack->r_ctl.tqh); 11429 if (rsm && (rsm->r_flags & RACK_ACKED) && (th_ack == rsm->r_start)) { 11430 /* 11431 * The peer has moved snd_una up to 11432 * the edge of this send, i.e. one 11433 * that it had previously acked. The only 11434 * way that can be true if the peer threw 11435 * away data (space issues) that it had 11436 * previously sacked (else it would have 11437 * given us snd_una up to (rsm->r_end). 11438 * We need to undo the acked markings here. 11439 * 11440 * Note we have to look to make sure th_ack is 11441 * our rsm->r_start in case we get an old ack 11442 * where th_ack is behind snd_una. 11443 */ 11444 rack_peer_reneges(rack, rsm, th_ack); 11445 } 11446 return; 11447 } 11448 if (rsm->r_flags & RACK_ACKED) { 11449 /* 11450 * It was acked on the scoreboard -- remove it from 11451 * total for the part being cum-acked. 11452 */ 11453 rack->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 11454 } else { 11455 if (((rsm->r_flags & RACK_ACKED) == 0) && 11456 (IN_RECOVERY(tp->t_flags))) { 11457 rack->r_ctl.bytes_acked_in_recovery += (th_ack - rsm->r_start); 11458 } 11459 rack_update_pcm_ack(rack, 1, rsm->r_start, th_ack); 11460 } 11461 /* And what about the lost flag? */ 11462 if (rsm->r_flags & RACK_WAS_LOST) { 11463 /* 11464 * This can happen when we marked it as lost 11465 * and yet before retransmitting we get an ack 11466 * which can happen due to reordering. In this 11467 * case its only a partial ack of the send. 11468 */ 11469 KASSERT((rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start)), 11470 ("rsm:%p rack:%p rc_considered_lost goes negative th_ack:%u", rsm, rack, th_ack)); 11471 if (rack->r_ctl.rc_considered_lost >= (th_ack - rsm->r_start)) 11472 rack->r_ctl.rc_considered_lost -= th_ack - rsm->r_start; 11473 else 11474 rack->r_ctl.rc_considered_lost = 0; 11475 } 11476 /* 11477 * Clear the dup ack count for 11478 * the piece that remains. 11479 */ 11480 rsm->r_dupack = 0; 11481 rack_log_retran_reason(rack, rsm, __LINE__, 0, 2); 11482 if (rsm->r_rtr_bytes) { 11483 /* 11484 * It was retransmitted adjust the 11485 * sack holes for what was acked. 11486 */ 11487 int ack_am; 11488 11489 ack_am = (th_ack - rsm->r_start); 11490 if (ack_am >= rsm->r_rtr_bytes) { 11491 rack->r_ctl.rc_holes_rxt -= ack_am; 11492 rsm->r_rtr_bytes -= ack_am; 11493 } 11494 } 11495 /* 11496 * Update where the piece starts and record 11497 * the time of send of highest cumack sent if 11498 * its in our GP range. 11499 */ 11500 rack_log_map_chg(tp, rack, NULL, rsm, NULL, MAP_TRIM_HEAD, th_ack, __LINE__); 11501 /* Now we need to move our offset forward too */ 11502 if (rsm->m && 11503 ((rsm->orig_m_len != rsm->m->m_len) || 11504 (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) { 11505 /* Fix up the orig_m_len and possibly the mbuf offset */ 11506 rack_adjust_orig_mlen(rsm); 11507 } 11508 rsm->soff += (th_ack - rsm->r_start); 11509 rack_rsm_sender_update(rack, tp, rsm, 5); 11510 /* The trim will move th_ack into r_start for us */ 11511 tqhash_trim(rack->r_ctl.tqh, th_ack); 11512 /* Now do we need to move the mbuf fwd too? */ 11513 { 11514 struct mbuf *m; 11515 uint32_t soff; 11516 11517 m = rsm->m; 11518 soff = rsm->soff; 11519 if (m) { 11520 while (soff >= m->m_len) { 11521 soff -= m->m_len; 11522 KASSERT((m->m_next != NULL), 11523 (" rsm:%p off:%u soff:%u m:%p", 11524 rsm, rsm->soff, soff, m)); 11525 m = m->m_next; 11526 if (m == NULL) { 11527 /* 11528 * This is a fall-back that prevents a panic. In reality 11529 * we should be able to walk the mbuf's and find our place. 11530 * At this point snd_una has not been updated with the sbcut() yet 11531 * but tqhash_trim did update rsm->r_start so the offset calcuation 11532 * should work fine. This is undesirable since we will take cache 11533 * hits to access the socket buffer. And even more puzzling is that 11534 * it happens occasionally. It should not :( 11535 */ 11536 m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 11537 (rsm->r_start - tp->snd_una), 11538 &soff); 11539 break; 11540 } 11541 } 11542 /* 11543 * Now save in our updated values. 11544 */ 11545 rsm->m = m; 11546 rsm->soff = soff; 11547 rsm->orig_m_len = rsm->m->m_len; 11548 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 11549 } 11550 } 11551 if (rack->app_limited_needs_set && 11552 SEQ_GEQ(th_ack, tp->gput_seq)) 11553 rack_need_set_test(tp, rack, rsm, tp->snd_una, __LINE__, RACK_USE_BEG); 11554 } 11555 11556 static void 11557 rack_handle_might_revert(struct tcpcb *tp, struct tcp_rack *rack) 11558 { 11559 struct rack_sendmap *rsm; 11560 int sack_pass_fnd = 0; 11561 11562 if (rack->r_might_revert) { 11563 /* 11564 * Ok we have reordering, have not sent anything, we 11565 * might want to revert the congestion state if nothing 11566 * further has SACK_PASSED on it. Lets check. 11567 * 11568 * We also get here when we have DSACKs come in for 11569 * all the data that we FR'd. Note that a rxt or tlp 11570 * timer clears this from happening. 11571 */ 11572 11573 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 11574 if (rsm->r_flags & RACK_SACK_PASSED) { 11575 sack_pass_fnd = 1; 11576 break; 11577 } 11578 } 11579 if (sack_pass_fnd == 0) { 11580 /* 11581 * We went into recovery 11582 * incorrectly due to reordering! 11583 */ 11584 int orig_cwnd; 11585 11586 rack->r_ent_rec_ns = 0; 11587 orig_cwnd = tp->snd_cwnd; 11588 tp->snd_ssthresh = rack->r_ctl.rc_ssthresh_at_erec; 11589 tp->snd_recover = tp->snd_una; 11590 rack_log_to_prr(rack, 14, orig_cwnd, __LINE__); 11591 if (IN_RECOVERY(tp->t_flags)) { 11592 rack_exit_recovery(tp, rack, 3); 11593 if ((rack->rto_from_rec == 1) && (rack_ssthresh_rest_rto_rec != 0) ){ 11594 /* 11595 * We were in recovery, had an RTO 11596 * and then re-entered recovery (more sack's arrived) 11597 * and we have properly recorded the old ssthresh from 11598 * the first recovery. We want to be able to slow-start 11599 * back to this level. The ssthresh from the timeout 11600 * and then back into recovery will end up most likely 11601 * to be min(cwnd=1mss, 2mss). Which makes it basically 11602 * so we get no slow-start after our RTO. 11603 */ 11604 rack->rto_from_rec = 0; 11605 if (rack->r_ctl.rto_ssthresh > tp->snd_ssthresh) 11606 tp->snd_ssthresh = rack->r_ctl.rto_ssthresh; 11607 } 11608 } 11609 rack->r_ctl.bytes_acked_in_recovery = 0; 11610 rack->r_ctl.time_entered_recovery = 0; 11611 } 11612 rack->r_might_revert = 0; 11613 } 11614 } 11615 11616 #ifdef TCP_SAD_DETECTION 11617 11618 static void 11619 rack_merge_out_sacks(struct tcp_rack *rack) 11620 { 11621 struct rack_sendmap *cur, *next, *rsm, *trsm = NULL; 11622 11623 cur = tqhash_min(rack->r_ctl.tqh); 11624 while(cur) { 11625 next = tqhash_next(rack->r_ctl.tqh, cur); 11626 /* 11627 * The idea is to go through all and merge back 11628 * together the pieces sent together, 11629 */ 11630 if ((next != NULL) && 11631 (cur->r_tim_lastsent[0] == next->r_tim_lastsent[0])) { 11632 rack_merge_rsm(rack, cur, next); 11633 } else { 11634 cur = next; 11635 } 11636 } 11637 /* 11638 * now treat it like a rxt event, everything is outstanding 11639 * and sent nothing acvked and dupacks are all zero. If this 11640 * is not an attacker it will have to dupack its way through 11641 * it all. 11642 */ 11643 TAILQ_INIT(&rack->r_ctl.rc_tmap); 11644 TQHASH_FOREACH(rsm, rack->r_ctl.tqh) { 11645 rsm->r_dupack = 0; 11646 /* We must re-add it back to the tlist */ 11647 if (trsm == NULL) { 11648 TAILQ_INSERT_HEAD(&rack->r_ctl.rc_tmap, rsm, r_tnext); 11649 } else { 11650 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, trsm, rsm, r_tnext); 11651 } 11652 rsm->r_in_tmap = 1; 11653 trsm = rsm; 11654 rsm->r_flags &= ~(RACK_ACKED | RACK_SACK_PASSED | RACK_WAS_SACKPASS | RACK_RWND_COLLAPSED); 11655 } 11656 sack_filter_clear(&rack->r_ctl.rack_sf, rack->rc_tp->snd_una); 11657 } 11658 11659 static void 11660 rack_do_detection(struct tcpcb *tp, struct tcp_rack *rack, uint32_t bytes_this_ack, uint32_t segsiz) 11661 { 11662 int do_detection = 0; 11663 11664 if (rack->sack_attack_disable || rack->rc_suspicious) { 11665 /* 11666 * If we have been disabled we must detect 11667 * to possibly reverse it. Or if the guy has 11668 * sent in suspicious sacks we want to do detection too. 11669 */ 11670 do_detection = 1; 11671 11672 } else if ((rack->do_detection || tcp_force_detection) && 11673 (tcp_sack_to_ack_thresh > 0) && 11674 (tcp_sack_to_move_thresh > 0) && 11675 (rack->r_ctl.rc_num_maps_alloced > tcp_map_minimum)) { 11676 /* 11677 * We only detect here if: 11678 * 1) System wide forcing is on <or> do_detection is on 11679 * <and> 11680 * 2) We have thresholds for move and ack (set one to 0 and we are off) 11681 * <and> 11682 * 3) We have maps allocated larger than our min (500). 11683 */ 11684 do_detection = 1; 11685 } 11686 if (do_detection > 0) { 11687 /* 11688 * We have thresholds set to find 11689 * possible attackers and disable sack. 11690 * Check them. 11691 */ 11692 uint64_t ackratio, moveratio, movetotal; 11693 11694 /* Log detecting */ 11695 rack_log_sad(rack, 1); 11696 /* Do we establish a ack ratio */ 11697 if ((rack->r_ctl.sack_count > tcp_map_minimum) || 11698 (rack->rc_suspicious == 1) || 11699 (rack->sack_attack_disable > 0)) { 11700 ackratio = (uint64_t)(rack->r_ctl.sack_count); 11701 ackratio *= (uint64_t)(1000); 11702 if (rack->r_ctl.ack_count) 11703 ackratio /= (uint64_t)(rack->r_ctl.ack_count); 11704 else { 11705 /* We can hit this due to ack totals degregation (via small sacks) */ 11706 ackratio = 1000; 11707 } 11708 } else { 11709 /* 11710 * No ack ratio needed if we have not 11711 * seen more sacks then the number of map entries. 11712 * The exception to that is if we have disabled sack then 11713 * we need to find a ratio. 11714 */ 11715 ackratio = 0; 11716 } 11717 11718 if ((rack->sack_attack_disable == 0) && 11719 (ackratio > rack_highest_sack_thresh_seen)) 11720 rack_highest_sack_thresh_seen = (uint32_t)ackratio; 11721 /* Do we establish a move ratio? */ 11722 if ((rack->r_ctl.sack_moved_extra > tcp_map_minimum) || 11723 (rack->rc_suspicious == 1) || 11724 (rack->sack_attack_disable > 0)) { 11725 /* 11726 * We need to have more sack moves than maps 11727 * allocated to have a move ratio considered. 11728 */ 11729 movetotal = rack->r_ctl.sack_moved_extra; 11730 movetotal += rack->r_ctl.sack_noextra_move; 11731 moveratio = rack->r_ctl.sack_moved_extra; 11732 moveratio *= (uint64_t)1000; 11733 if (movetotal) 11734 moveratio /= movetotal; 11735 else { 11736 /* No moves, thats pretty good */ 11737 moveratio = 0; 11738 } 11739 } else { 11740 /* 11741 * Not enough moves have occured to consider 11742 * if we are out of whack in that ratio. 11743 * The exception to that is if we have disabled sack then 11744 * we need to find a ratio. 11745 */ 11746 moveratio = 0; 11747 } 11748 if ((rack->sack_attack_disable == 0) && 11749 (moveratio > rack_highest_move_thresh_seen)) 11750 rack_highest_move_thresh_seen = (uint32_t)moveratio; 11751 /* Now the tests */ 11752 if (rack->sack_attack_disable == 0) { 11753 /* Not disabled, do we need to disable? */ 11754 if ((ackratio > tcp_sack_to_ack_thresh) && 11755 (moveratio > tcp_sack_to_move_thresh)) { 11756 /* Disable sack processing */ 11757 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED); 11758 rack->sack_attack_disable = 1; 11759 /* set it so we have the built in delay */ 11760 rack->r_ctl.ack_during_sd = 1; 11761 if (rack_merge_out_sacks_on_attack) 11762 rack_merge_out_sacks(rack); 11763 counter_u64_add(rack_sack_attacks_detected, 1); 11764 tcp_trace_point(rack->rc_tp, TCP_TP_SAD_TRIGGERED); 11765 /* Clamp the cwnd at flight size */ 11766 rack->r_ctl.rc_saved_cwnd = rack->rc_tp->snd_cwnd; 11767 rack->rc_tp->snd_cwnd = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 11768 rack_log_sad(rack, 2); 11769 } 11770 } else { 11771 /* We are sack-disabled check for false positives */ 11772 if ((ackratio <= tcp_restoral_thresh) || 11773 ((rack_merge_out_sacks_on_attack == 0) && 11774 (rack->rc_suspicious == 0) && 11775 (rack->r_ctl.rc_num_maps_alloced <= (tcp_map_minimum/2)))) { 11776 rack->sack_attack_disable = 0; 11777 rack_log_sad(rack, 3); 11778 /* Restart counting */ 11779 rack->r_ctl.sack_count = 0; 11780 rack->r_ctl.sack_moved_extra = 0; 11781 rack->r_ctl.sack_noextra_move = 1; 11782 rack->rc_suspicious = 0; 11783 rack->r_ctl.ack_count = max(1, 11784 (bytes_this_ack / segsiz)); 11785 11786 counter_u64_add(rack_sack_attacks_reversed, 1); 11787 /* Restore the cwnd */ 11788 if (rack->r_ctl.rc_saved_cwnd > rack->rc_tp->snd_cwnd) 11789 rack->rc_tp->snd_cwnd = rack->r_ctl.rc_saved_cwnd; 11790 } 11791 } 11792 } 11793 } 11794 #endif 11795 11796 static int 11797 rack_note_dsack(struct tcp_rack *rack, tcp_seq start, tcp_seq end) 11798 { 11799 11800 uint32_t am, l_end; 11801 int was_tlp = 0; 11802 11803 if (SEQ_GT(end, start)) 11804 am = end - start; 11805 else 11806 am = 0; 11807 if ((rack->rc_last_tlp_acked_set ) && 11808 (SEQ_GEQ(start, rack->r_ctl.last_tlp_acked_start)) && 11809 (SEQ_LEQ(end, rack->r_ctl.last_tlp_acked_end))) { 11810 /* 11811 * The DSACK is because of a TLP which we don't 11812 * do anything with the reordering window over since 11813 * it was not reordering that caused the DSACK but 11814 * our previous retransmit TLP. 11815 */ 11816 rack_log_dsack_event(rack, 7, __LINE__, start, end); 11817 was_tlp = 1; 11818 goto skip_dsack_round; 11819 } 11820 if (rack->rc_last_sent_tlp_seq_valid) { 11821 l_end = rack->r_ctl.last_sent_tlp_seq + rack->r_ctl.last_sent_tlp_len; 11822 if (SEQ_GEQ(start, rack->r_ctl.last_sent_tlp_seq) && 11823 (SEQ_LEQ(end, l_end))) { 11824 /* 11825 * This dsack is from the last sent TLP, ignore it 11826 * for reordering purposes. 11827 */ 11828 rack_log_dsack_event(rack, 7, __LINE__, start, end); 11829 was_tlp = 1; 11830 goto skip_dsack_round; 11831 } 11832 } 11833 if (rack->rc_dsack_round_seen == 0) { 11834 rack->rc_dsack_round_seen = 1; 11835 rack->r_ctl.dsack_round_end = rack->rc_tp->snd_max; 11836 rack->r_ctl.num_dsack++; 11837 rack->r_ctl.dsack_persist = 16; /* 16 is from the standard */ 11838 rack_log_dsack_event(rack, 2, __LINE__, 0, 0); 11839 } 11840 skip_dsack_round: 11841 /* 11842 * We keep track of how many DSACK blocks we get 11843 * after a recovery incident. 11844 */ 11845 rack->r_ctl.dsack_byte_cnt += am; 11846 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags) && 11847 rack->r_ctl.retran_during_recovery && 11848 (rack->r_ctl.dsack_byte_cnt >= rack->r_ctl.retran_during_recovery)) { 11849 /* 11850 * False recovery most likely culprit is reordering. If 11851 * nothing else is missing we need to revert. 11852 */ 11853 rack->r_might_revert = 1; 11854 rack_handle_might_revert(rack->rc_tp, rack); 11855 rack->r_might_revert = 0; 11856 rack->r_ctl.retran_during_recovery = 0; 11857 rack->r_ctl.dsack_byte_cnt = 0; 11858 } 11859 return (was_tlp); 11860 } 11861 11862 static uint32_t 11863 do_rack_compute_pipe(struct tcpcb *tp, struct tcp_rack *rack, uint32_t snd_una) 11864 { 11865 return (((tp->snd_max - snd_una) - 11866 (rack->r_ctl.rc_sacked + rack->r_ctl.rc_considered_lost)) + rack->r_ctl.rc_holes_rxt); 11867 } 11868 11869 static int32_t 11870 rack_compute_pipe(struct tcpcb *tp) 11871 { 11872 return ((int32_t)do_rack_compute_pipe(tp, 11873 (struct tcp_rack *)tp->t_fb_ptr, 11874 tp->snd_una)); 11875 } 11876 11877 static void 11878 rack_update_prr(struct tcpcb *tp, struct tcp_rack *rack, uint32_t changed, tcp_seq th_ack) 11879 { 11880 /* Deal with changed and PRR here (in recovery only) */ 11881 uint32_t pipe, snd_una; 11882 11883 rack->r_ctl.rc_prr_delivered += changed; 11884 11885 if (sbavail(&rack->rc_inp->inp_socket->so_snd) <= (tp->snd_max - tp->snd_una)) { 11886 /* 11887 * It is all outstanding, we are application limited 11888 * and thus we don't need more room to send anything. 11889 * Note we use tp->snd_una here and not th_ack because 11890 * the data as yet not been cut from the sb. 11891 */ 11892 rack->r_ctl.rc_prr_sndcnt = 0; 11893 return; 11894 } 11895 /* Compute prr_sndcnt */ 11896 if (SEQ_GT(tp->snd_una, th_ack)) { 11897 snd_una = tp->snd_una; 11898 } else { 11899 snd_una = th_ack; 11900 } 11901 pipe = do_rack_compute_pipe(tp, rack, snd_una); 11902 if (pipe > tp->snd_ssthresh) { 11903 long sndcnt; 11904 11905 sndcnt = rack->r_ctl.rc_prr_delivered * tp->snd_ssthresh; 11906 if (rack->r_ctl.rc_prr_recovery_fs > 0) 11907 sndcnt /= (long)rack->r_ctl.rc_prr_recovery_fs; 11908 else { 11909 rack->r_ctl.rc_prr_sndcnt = 0; 11910 rack_log_to_prr(rack, 9, 0, __LINE__); 11911 sndcnt = 0; 11912 } 11913 sndcnt++; 11914 if (sndcnt > (long)rack->r_ctl.rc_prr_out) 11915 sndcnt -= rack->r_ctl.rc_prr_out; 11916 else 11917 sndcnt = 0; 11918 rack->r_ctl.rc_prr_sndcnt = sndcnt; 11919 rack_log_to_prr(rack, 10, 0, __LINE__); 11920 } else { 11921 uint32_t limit; 11922 11923 if (rack->r_ctl.rc_prr_delivered > rack->r_ctl.rc_prr_out) 11924 limit = (rack->r_ctl.rc_prr_delivered - rack->r_ctl.rc_prr_out); 11925 else 11926 limit = 0; 11927 if (changed > limit) 11928 limit = changed; 11929 limit += ctf_fixed_maxseg(tp); 11930 if (tp->snd_ssthresh > pipe) { 11931 rack->r_ctl.rc_prr_sndcnt = min((tp->snd_ssthresh - pipe), limit); 11932 rack_log_to_prr(rack, 11, 0, __LINE__); 11933 } else { 11934 rack->r_ctl.rc_prr_sndcnt = min(0, limit); 11935 rack_log_to_prr(rack, 12, 0, __LINE__); 11936 } 11937 } 11938 } 11939 11940 static void 11941 rack_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, int entered_recovery, int dup_ack_struck, 11942 int *dsack_seen, int *sacks_seen) 11943 { 11944 uint32_t changed; 11945 struct tcp_rack *rack; 11946 struct rack_sendmap *rsm; 11947 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 11948 register uint32_t th_ack; 11949 int32_t i, j, k, num_sack_blks = 0; 11950 uint32_t cts, acked, ack_point; 11951 int loop_start = 0, moved_two = 0, no_extra = 0; 11952 uint32_t tsused; 11953 uint32_t segsiz, o_cnt; 11954 11955 11956 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11957 if (tcp_get_flags(th) & TH_RST) { 11958 /* We don't log resets */ 11959 return; 11960 } 11961 rack = (struct tcp_rack *)tp->t_fb_ptr; 11962 cts = tcp_get_usecs(NULL); 11963 rsm = tqhash_min(rack->r_ctl.tqh); 11964 changed = 0; 11965 th_ack = th->th_ack; 11966 if (rack->sack_attack_disable == 0) 11967 rack_do_decay(rack); 11968 segsiz = ctf_fixed_maxseg(rack->rc_tp); 11969 if (BYTES_THIS_ACK(tp, th) >= segsiz) { 11970 /* 11971 * You only get credit for 11972 * MSS and greater (and you get extra 11973 * credit for larger cum-ack moves). 11974 */ 11975 int ac; 11976 11977 ac = BYTES_THIS_ACK(tp, th) / ctf_fixed_maxseg(rack->rc_tp); 11978 rack->r_ctl.ack_count += ac; 11979 counter_u64_add(rack_ack_total, ac); 11980 } 11981 if (rack->r_ctl.ack_count > 0xfff00000) { 11982 /* 11983 * reduce the number to keep us under 11984 * a uint32_t. 11985 */ 11986 rack->r_ctl.ack_count /= 2; 11987 rack->r_ctl.sack_count /= 2; 11988 } 11989 if (SEQ_GT(th_ack, tp->snd_una)) { 11990 rack_log_progress_event(rack, tp, ticks, PROGRESS_UPDATE, __LINE__); 11991 tp->t_acktime = ticks; 11992 } 11993 if (rsm && SEQ_GT(th_ack, rsm->r_start)) 11994 changed = th_ack - rsm->r_start; 11995 if (changed) { 11996 rack_process_to_cumack(tp, rack, th_ack, cts, to, 11997 tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time)); 11998 } 11999 if ((to->to_flags & TOF_SACK) == 0) { 12000 /* We are done nothing left and no sack. */ 12001 rack_handle_might_revert(tp, rack); 12002 /* 12003 * For cases where we struck a dup-ack 12004 * with no SACK, add to the changes so 12005 * PRR will work right. 12006 */ 12007 if (dup_ack_struck && (changed == 0)) { 12008 changed += ctf_fixed_maxseg(rack->rc_tp); 12009 } 12010 goto out; 12011 } 12012 /* Sack block processing */ 12013 if (SEQ_GT(th_ack, tp->snd_una)) 12014 ack_point = th_ack; 12015 else 12016 ack_point = tp->snd_una; 12017 for (i = 0; i < to->to_nsacks; i++) { 12018 bcopy((to->to_sacks + i * TCPOLEN_SACK), 12019 &sack, sizeof(sack)); 12020 sack.start = ntohl(sack.start); 12021 sack.end = ntohl(sack.end); 12022 if (SEQ_GT(sack.end, sack.start) && 12023 SEQ_GT(sack.start, ack_point) && 12024 SEQ_LT(sack.start, tp->snd_max) && 12025 SEQ_GT(sack.end, ack_point) && 12026 SEQ_LEQ(sack.end, tp->snd_max)) { 12027 sack_blocks[num_sack_blks] = sack; 12028 num_sack_blks++; 12029 } else if (SEQ_LEQ(sack.start, th_ack) && 12030 SEQ_LEQ(sack.end, th_ack)) { 12031 int was_tlp; 12032 12033 if (dsack_seen != NULL) 12034 *dsack_seen = 1; 12035 was_tlp = rack_note_dsack(rack, sack.start, sack.end); 12036 /* 12037 * Its a D-SACK block. 12038 */ 12039 tcp_record_dsack(tp, sack.start, sack.end, was_tlp); 12040 } 12041 } 12042 if (rack->rc_dsack_round_seen) { 12043 /* Is the dsack roound over? */ 12044 if (SEQ_GEQ(th_ack, rack->r_ctl.dsack_round_end)) { 12045 /* Yes it is */ 12046 rack->rc_dsack_round_seen = 0; 12047 rack_log_dsack_event(rack, 3, __LINE__, 0, 0); 12048 } 12049 } 12050 /* 12051 * Sort the SACK blocks so we can update the rack scoreboard with 12052 * just one pass. 12053 */ 12054 o_cnt = num_sack_blks; 12055 num_sack_blks = sack_filter_blks(&rack->r_ctl.rack_sf, sack_blocks, 12056 num_sack_blks, th->th_ack); 12057 ctf_log_sack_filter(rack->rc_tp, num_sack_blks, sack_blocks); 12058 if (sacks_seen != NULL) 12059 *sacks_seen = num_sack_blks; 12060 if (num_sack_blks == 0) { 12061 /* Nothing to sack, but we need to update counts */ 12062 if ((o_cnt == 1) && 12063 (*dsack_seen != 1)) 12064 rack->r_ctl.sack_count++; 12065 else if (o_cnt > 1) 12066 rack->r_ctl.sack_count++; 12067 goto out_with_totals; 12068 } 12069 if (rack->sack_attack_disable) { 12070 /* 12071 * An attacker disablement is in place, for 12072 * every sack block that is not at least a full MSS 12073 * count up sack_count. 12074 */ 12075 for (i = 0; i < num_sack_blks; i++) { 12076 if ((sack_blocks[i].end - sack_blocks[i].start) < segsiz) { 12077 rack->r_ctl.sack_count++; 12078 } 12079 if (rack->r_ctl.sack_count > 0xfff00000) { 12080 /* 12081 * reduce the number to keep us under 12082 * a uint32_t. 12083 */ 12084 rack->r_ctl.ack_count /= 2; 12085 rack->r_ctl.sack_count /= 2; 12086 } 12087 } 12088 goto out; 12089 } 12090 /* Its a sack of some sort */ 12091 rack->r_ctl.sack_count += num_sack_blks; 12092 if (rack->r_ctl.sack_count > 0xfff00000) { 12093 /* 12094 * reduce the number to keep us under 12095 * a uint32_t. 12096 */ 12097 rack->r_ctl.ack_count /= 2; 12098 rack->r_ctl.sack_count /= 2; 12099 } 12100 if (num_sack_blks < 2) { 12101 /* Only one, we don't need to sort */ 12102 goto do_sack_work; 12103 } 12104 /* Sort the sacks */ 12105 for (i = 0; i < num_sack_blks; i++) { 12106 for (j = i + 1; j < num_sack_blks; j++) { 12107 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 12108 sack = sack_blocks[i]; 12109 sack_blocks[i] = sack_blocks[j]; 12110 sack_blocks[j] = sack; 12111 } 12112 } 12113 } 12114 /* 12115 * Now are any of the sack block ends the same (yes some 12116 * implementations send these)? 12117 */ 12118 again: 12119 if (num_sack_blks == 0) 12120 goto out_with_totals; 12121 if (num_sack_blks > 1) { 12122 for (i = 0; i < num_sack_blks; i++) { 12123 for (j = i + 1; j < num_sack_blks; j++) { 12124 if (sack_blocks[i].end == sack_blocks[j].end) { 12125 /* 12126 * Ok these two have the same end we 12127 * want the smallest end and then 12128 * throw away the larger and start 12129 * again. 12130 */ 12131 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 12132 /* 12133 * The second block covers 12134 * more area use that 12135 */ 12136 sack_blocks[i].start = sack_blocks[j].start; 12137 } 12138 /* 12139 * Now collapse out the dup-sack and 12140 * lower the count 12141 */ 12142 for (k = (j + 1); k < num_sack_blks; k++) { 12143 sack_blocks[j].start = sack_blocks[k].start; 12144 sack_blocks[j].end = sack_blocks[k].end; 12145 j++; 12146 } 12147 num_sack_blks--; 12148 goto again; 12149 } 12150 } 12151 } 12152 } 12153 do_sack_work: 12154 /* 12155 * First lets look to see if 12156 * we have retransmitted and 12157 * can use the transmit next? 12158 */ 12159 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 12160 if (rsm && 12161 SEQ_GT(sack_blocks[0].end, rsm->r_start) && 12162 SEQ_LT(sack_blocks[0].start, rsm->r_end)) { 12163 /* 12164 * We probably did the FR and the next 12165 * SACK in continues as we would expect. 12166 */ 12167 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[0], to, &rsm, cts, &no_extra, &moved_two, segsiz); 12168 if (acked) { 12169 rack->r_wanted_output = 1; 12170 changed += acked; 12171 } 12172 if (num_sack_blks == 1) { 12173 /* 12174 * This is what we would expect from 12175 * a normal implementation to happen 12176 * after we have retransmitted the FR, 12177 * i.e the sack-filter pushes down 12178 * to 1 block and the next to be retransmitted 12179 * is the sequence in the sack block (has more 12180 * are acked). Count this as ACK'd data to boost 12181 * up the chances of recovering any false positives. 12182 */ 12183 rack->r_ctl.ack_count += (acked / ctf_fixed_maxseg(rack->rc_tp)); 12184 counter_u64_add(rack_ack_total, (acked / ctf_fixed_maxseg(rack->rc_tp))); 12185 counter_u64_add(rack_express_sack, 1); 12186 if (rack->r_ctl.ack_count > 0xfff00000) { 12187 /* 12188 * reduce the number to keep us under 12189 * a uint32_t. 12190 */ 12191 rack->r_ctl.ack_count /= 2; 12192 rack->r_ctl.sack_count /= 2; 12193 } 12194 if (moved_two) { 12195 /* 12196 * If we did not get a SACK for at least a MSS and 12197 * had to move at all, or if we moved more than our 12198 * threshold, it counts against the "extra" move. 12199 */ 12200 rack->r_ctl.sack_moved_extra += moved_two; 12201 rack->r_ctl.sack_noextra_move += no_extra; 12202 counter_u64_add(rack_move_some, 1); 12203 } else { 12204 /* 12205 * else we did not have to move 12206 * any more than we would expect. 12207 */ 12208 rack->r_ctl.sack_noextra_move += no_extra; 12209 rack->r_ctl.sack_noextra_move++; 12210 counter_u64_add(rack_move_none, 1); 12211 } 12212 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) || 12213 (rack->r_ctl.sack_noextra_move > 0xfff00000)) { 12214 rack->r_ctl.sack_moved_extra /= 2; 12215 rack->r_ctl.sack_noextra_move /= 2; 12216 } 12217 goto out_with_totals; 12218 } else { 12219 /* 12220 * Start the loop through the 12221 * rest of blocks, past the first block. 12222 */ 12223 loop_start = 1; 12224 } 12225 } 12226 counter_u64_add(rack_sack_total, 1); 12227 rsm = rack->r_ctl.rc_sacklast; 12228 for (i = loop_start; i < num_sack_blks; i++) { 12229 acked = rack_proc_sack_blk(tp, rack, &sack_blocks[i], to, &rsm, cts, &no_extra, &moved_two, segsiz); 12230 if (acked) { 12231 rack->r_wanted_output = 1; 12232 changed += acked; 12233 } 12234 if (moved_two) { 12235 /* 12236 * If we did not get a SACK for at least a MSS and 12237 * had to move at all, or if we moved more than our 12238 * threshold, it counts against the "extra" move. 12239 */ 12240 rack->r_ctl.sack_moved_extra += moved_two; 12241 rack->r_ctl.sack_noextra_move += no_extra; 12242 counter_u64_add(rack_move_some, 1); 12243 } else { 12244 /* 12245 * else we did not have to move 12246 * any more than we would expect. 12247 */ 12248 rack->r_ctl.sack_noextra_move += no_extra; 12249 rack->r_ctl.sack_noextra_move++; 12250 counter_u64_add(rack_move_none, 1); 12251 } 12252 if ((rack->r_ctl.sack_moved_extra > 0xfff00000) || 12253 (rack->r_ctl.sack_noextra_move > 0xfff00000)) { 12254 rack->r_ctl.sack_moved_extra /= 2; 12255 rack->r_ctl.sack_noextra_move /= 2; 12256 } 12257 if (moved_two && (acked < ctf_fixed_maxseg(rack->rc_tp))) { 12258 /* 12259 * If the SACK was not a full MSS then 12260 * we add to sack_count the number of 12261 * MSS's (or possibly more than 12262 * a MSS if its a TSO send) we had to skip by. 12263 */ 12264 rack->r_ctl.sack_count += moved_two; 12265 if (rack->r_ctl.sack_count > 0xfff00000) { 12266 rack->r_ctl.ack_count /= 2; 12267 rack->r_ctl.sack_count /= 2; 12268 } 12269 counter_u64_add(rack_sack_total, moved_two); 12270 } 12271 /* 12272 * Now we need to setup for the next 12273 * round. First we make sure we won't 12274 * exceed the size of our uint32_t on 12275 * the various counts, and then clear out 12276 * moved_two. 12277 */ 12278 moved_two = 0; 12279 no_extra = 0; 12280 } 12281 out_with_totals: 12282 if (num_sack_blks > 1) { 12283 /* 12284 * You get an extra stroke if 12285 * you have more than one sack-blk, this 12286 * could be where we are skipping forward 12287 * and the sack-filter is still working, or 12288 * it could be an attacker constantly 12289 * moving us. 12290 */ 12291 rack->r_ctl.sack_moved_extra++; 12292 counter_u64_add(rack_move_some, 1); 12293 } 12294 out: 12295 #ifdef TCP_SAD_DETECTION 12296 rack_do_detection(tp, rack, BYTES_THIS_ACK(tp, th), ctf_fixed_maxseg(rack->rc_tp)); 12297 #endif 12298 if (changed) { 12299 /* Something changed cancel the rack timer */ 12300 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 12301 } 12302 tsused = tcp_get_usecs(NULL); 12303 rsm = tcp_rack_output(tp, rack, tsused); 12304 if ((!IN_FASTRECOVERY(tp->t_flags)) && 12305 rsm && 12306 ((rsm->r_flags & RACK_MUST_RXT) == 0)) { 12307 /* Enter recovery */ 12308 entered_recovery = 1; 12309 rack_cong_signal(tp, CC_NDUPACK, th_ack, __LINE__); 12310 /* 12311 * When we enter recovery we need to assure we send 12312 * one packet. 12313 */ 12314 if (rack->rack_no_prr == 0) { 12315 rack->r_ctl.rc_prr_sndcnt = ctf_fixed_maxseg(tp); 12316 rack_log_to_prr(rack, 8, 0, __LINE__); 12317 } 12318 rack->r_timer_override = 1; 12319 rack->r_early = 0; 12320 rack->r_ctl.rc_agg_early = 0; 12321 } else if (IN_FASTRECOVERY(tp->t_flags) && 12322 rsm && 12323 (rack->r_rr_config == 3)) { 12324 /* 12325 * Assure we can output and we get no 12326 * remembered pace time except the retransmit. 12327 */ 12328 rack->r_timer_override = 1; 12329 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 12330 rack->r_ctl.rc_resend = rsm; 12331 } 12332 if (IN_FASTRECOVERY(tp->t_flags) && 12333 (rack->rack_no_prr == 0) && 12334 (entered_recovery == 0)) { 12335 rack_update_prr(tp, rack, changed, th_ack); 12336 if ((rsm && (rack->r_ctl.rc_prr_sndcnt >= ctf_fixed_maxseg(tp)) && 12337 ((tcp_in_hpts(rack->rc_tp) == 0) && 12338 ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0)))) { 12339 /* 12340 * If you are pacing output you don't want 12341 * to override. 12342 */ 12343 rack->r_early = 0; 12344 rack->r_ctl.rc_agg_early = 0; 12345 rack->r_timer_override = 1; 12346 } 12347 } 12348 } 12349 12350 static void 12351 rack_strike_dupack(struct tcp_rack *rack, tcp_seq th_ack) 12352 { 12353 struct rack_sendmap *rsm; 12354 12355 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 12356 while (rsm) { 12357 /* 12358 * We need to skip anything already set 12359 * to be retransmitted. 12360 */ 12361 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 12362 (rsm->r_flags & RACK_MUST_RXT)) { 12363 rsm = TAILQ_NEXT(rsm, r_tnext); 12364 continue; 12365 } 12366 break; 12367 } 12368 if (rsm && (rsm->r_dupack < 0xff)) { 12369 rsm->r_dupack++; 12370 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) { 12371 struct timeval tv; 12372 uint32_t cts; 12373 /* 12374 * Here we see if we need to retransmit. For 12375 * a SACK type connection if enough time has passed 12376 * we will get a return of the rsm. For a non-sack 12377 * connection we will get the rsm returned if the 12378 * dupack value is 3 or more. 12379 */ 12380 cts = tcp_get_usecs(&tv); 12381 rack->r_ctl.rc_resend = tcp_rack_output(rack->rc_tp, rack, cts); 12382 if (rack->r_ctl.rc_resend != NULL) { 12383 if (!IN_FASTRECOVERY(rack->rc_tp->t_flags)) { 12384 rack_cong_signal(rack->rc_tp, CC_NDUPACK, 12385 th_ack, __LINE__); 12386 } 12387 rack->r_wanted_output = 1; 12388 rack->r_timer_override = 1; 12389 rack_log_retran_reason(rack, rsm, __LINE__, 1, 3); 12390 } 12391 } else { 12392 rack_log_retran_reason(rack, rsm, __LINE__, 0, 3); 12393 } 12394 } 12395 } 12396 12397 static void 12398 rack_check_bottom_drag(struct tcpcb *tp, 12399 struct tcp_rack *rack, 12400 struct socket *so) 12401 { 12402 /* 12403 * So what is dragging bottom? 12404 * 12405 * Dragging bottom means you were under pacing and had a 12406 * delay in processing inbound acks waiting on our pacing 12407 * timer to expire. While you were waiting all of the acknowledgments 12408 * for the packets you sent have arrived. This means we are pacing 12409 * way underneath the bottleneck to the point where our Goodput 12410 * measurements stop working, since they require more than one 12411 * ack (usually at least 8 packets worth with multiple acks so we can 12412 * gauge the inter-ack times). If that occurs we have a real problem 12413 * since we are stuck in a hole that we can't get out of without 12414 * something speeding us up. 12415 * 12416 * We also check to see if we are widdling down to just one segment 12417 * outstanding. If this occurs and we have room to send in our cwnd/rwnd 12418 * then we are adding the delayed ack interval into our measurments and 12419 * we need to speed up slightly. 12420 */ 12421 uint32_t segsiz, minseg; 12422 12423 segsiz = ctf_fixed_maxseg(tp); 12424 minseg = segsiz; 12425 if (tp->snd_max == tp->snd_una) { 12426 /* 12427 * We are doing dynamic pacing and we are way 12428 * under. Basically everything got acked while 12429 * we were still waiting on the pacer to expire. 12430 * 12431 * This means we need to boost the b/w in 12432 * addition to any earlier boosting of 12433 * the multiplier. 12434 */ 12435 uint64_t lt_bw; 12436 12437 tcp_trace_point(rack->rc_tp, TCP_TP_PACED_BOTTOM); 12438 lt_bw = rack_get_lt_bw(rack); 12439 rack->rc_dragged_bottom = 1; 12440 rack_validate_multipliers_at_or_above100(rack); 12441 if ((rack->r_ctl.rack_rs.rs_flags & RACK_RTT_VALID) && 12442 (rack->dis_lt_bw == 0) && 12443 (rack->use_lesser_lt_bw == 0) && 12444 (lt_bw > 0)) { 12445 /* 12446 * Lets use the long-term b/w we have 12447 * been getting as a base. 12448 */ 12449 if (rack->rc_gp_filled == 0) { 12450 if (lt_bw > ONE_POINT_TWO_MEG) { 12451 /* 12452 * If we have no measurement 12453 * don't let us set in more than 12454 * 1.2Mbps. If we are still too 12455 * low after pacing with this we 12456 * will hopefully have a max b/w 12457 * available to sanity check things. 12458 */ 12459 lt_bw = ONE_POINT_TWO_MEG; 12460 } 12461 rack->r_ctl.rc_rtt_diff = 0; 12462 rack->r_ctl.gp_bw = lt_bw; 12463 rack->rc_gp_filled = 1; 12464 if (rack->r_ctl.num_measurements < RACK_REQ_AVG) 12465 rack->r_ctl.num_measurements = RACK_REQ_AVG; 12466 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 12467 } else if (lt_bw > rack->r_ctl.gp_bw) { 12468 rack->r_ctl.rc_rtt_diff = 0; 12469 if (rack->r_ctl.num_measurements < RACK_REQ_AVG) 12470 rack->r_ctl.num_measurements = RACK_REQ_AVG; 12471 rack->r_ctl.gp_bw = lt_bw; 12472 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 12473 } else 12474 rack_increase_bw_mul(rack, -1, 0, 0, 1); 12475 if ((rack->gp_ready == 0) && 12476 (rack->r_ctl.num_measurements >= rack->r_ctl.req_measurements)) { 12477 /* We have enough measurements now */ 12478 rack->gp_ready = 1; 12479 if (rack->dgp_on || 12480 rack->rack_hibeta) 12481 rack_set_cc_pacing(rack); 12482 if (rack->defer_options) 12483 rack_apply_deferred_options(rack); 12484 } 12485 } else { 12486 /* 12487 * zero rtt possibly?, settle for just an old increase. 12488 */ 12489 rack_increase_bw_mul(rack, -1, 0, 0, 1); 12490 } 12491 } else if ((IN_FASTRECOVERY(tp->t_flags) == 0) && 12492 (sbavail(&so->so_snd) > max((segsiz * (4 + rack_req_segs)), 12493 minseg)) && 12494 (rack->r_ctl.cwnd_to_use > max((segsiz * (rack_req_segs + 2)), minseg)) && 12495 (tp->snd_wnd > max((segsiz * (rack_req_segs + 2)), minseg)) && 12496 (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) <= 12497 (segsiz * rack_req_segs))) { 12498 /* 12499 * We are doing dynamic GP pacing and 12500 * we have everything except 1MSS or less 12501 * bytes left out. We are still pacing away. 12502 * And there is data that could be sent, This 12503 * means we are inserting delayed ack time in 12504 * our measurements because we are pacing too slow. 12505 */ 12506 rack_validate_multipliers_at_or_above100(rack); 12507 rack->rc_dragged_bottom = 1; 12508 rack_increase_bw_mul(rack, -1, 0, 0, 1); 12509 } 12510 } 12511 12512 #ifdef TCP_REQUEST_TRK 12513 static void 12514 rack_log_hybrid(struct tcp_rack *rack, uint32_t seq, 12515 struct tcp_sendfile_track *cur, uint8_t mod, int line, int err) 12516 { 12517 int do_log; 12518 12519 do_log = tcp_bblogging_on(rack->rc_tp); 12520 if (do_log == 0) { 12521 if ((do_log = tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) )== 0) 12522 return; 12523 /* We only allow the three below with point logging on */ 12524 if ((mod != HYBRID_LOG_RULES_APP) && 12525 (mod != HYBRID_LOG_RULES_SET) && 12526 (mod != HYBRID_LOG_REQ_COMP)) 12527 return; 12528 12529 } 12530 if (do_log) { 12531 union tcp_log_stackspecific log; 12532 struct timeval tv; 12533 12534 /* Convert our ms to a microsecond */ 12535 memset(&log, 0, sizeof(log)); 12536 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 12537 log.u_bbr.flex1 = seq; 12538 log.u_bbr.cwnd_gain = line; 12539 if (cur != NULL) { 12540 uint64_t off; 12541 12542 log.u_bbr.flex2 = cur->start_seq; 12543 log.u_bbr.flex3 = cur->end_seq; 12544 log.u_bbr.flex4 = (uint32_t)((cur->localtime >> 32) & 0x00000000ffffffff); 12545 log.u_bbr.flex5 = (uint32_t)(cur->localtime & 0x00000000ffffffff); 12546 log.u_bbr.flex6 = cur->flags; 12547 log.u_bbr.pkts_out = cur->hybrid_flags; 12548 log.u_bbr.rttProp = cur->timestamp; 12549 log.u_bbr.cur_del_rate = cur->cspr; 12550 log.u_bbr.bw_inuse = cur->start; 12551 log.u_bbr.applimited = (uint32_t)(cur->end & 0x00000000ffffffff); 12552 log.u_bbr.delivered = (uint32_t)((cur->end >> 32) & 0x00000000ffffffff) ; 12553 log.u_bbr.epoch = (uint32_t)(cur->deadline & 0x00000000ffffffff); 12554 log.u_bbr.lt_epoch = (uint32_t)((cur->deadline >> 32) & 0x00000000ffffffff) ; 12555 log.u_bbr.inhpts = 1; 12556 #ifdef TCP_REQUEST_TRK 12557 off = (uint64_t)(cur) - (uint64_t)(&rack->rc_tp->t_tcpreq_info[0]); 12558 log.u_bbr.use_lt_bw = (uint8_t)(off / sizeof(struct tcp_sendfile_track)); 12559 #endif 12560 } else { 12561 log.u_bbr.flex2 = err; 12562 } 12563 /* 12564 * Fill in flex7 to be CHD (catchup|hybrid|DGP) 12565 */ 12566 log.u_bbr.flex7 = rack->rc_catch_up; 12567 log.u_bbr.flex7 <<= 1; 12568 log.u_bbr.flex7 |= rack->rc_hybrid_mode; 12569 log.u_bbr.flex7 <<= 1; 12570 log.u_bbr.flex7 |= rack->dgp_on; 12571 /* 12572 * Compose bbr_state to be a bit wise 0000ADHF 12573 * where A is the always_pace flag 12574 * where D is the dgp_on flag 12575 * where H is the hybrid_mode on flag 12576 * where F is the use_fixed_rate flag. 12577 */ 12578 log.u_bbr.bbr_state = rack->rc_always_pace; 12579 log.u_bbr.bbr_state <<= 1; 12580 log.u_bbr.bbr_state |= rack->dgp_on; 12581 log.u_bbr.bbr_state <<= 1; 12582 log.u_bbr.bbr_state |= rack->rc_hybrid_mode; 12583 log.u_bbr.bbr_state <<= 1; 12584 log.u_bbr.bbr_state |= rack->use_fixed_rate; 12585 log.u_bbr.flex8 = mod; 12586 log.u_bbr.delRate = rack->r_ctl.bw_rate_cap; 12587 log.u_bbr.bbr_substate = rack->r_ctl.client_suggested_maxseg; 12588 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 12589 log.u_bbr.pkt_epoch = rack->rc_tp->tcp_hybrid_start; 12590 log.u_bbr.lost = rack->rc_tp->tcp_hybrid_error; 12591 log.u_bbr.pacing_gain = (uint16_t)rack->rc_tp->tcp_hybrid_stop; 12592 tcp_log_event(rack->rc_tp, NULL, 12593 &rack->rc_inp->inp_socket->so_rcv, 12594 &rack->rc_inp->inp_socket->so_snd, 12595 TCP_HYBRID_PACING_LOG, 0, 12596 0, &log, false, NULL, __func__, __LINE__, &tv); 12597 } 12598 } 12599 #endif 12600 12601 #ifdef TCP_REQUEST_TRK 12602 static void 12603 rack_set_dgp_hybrid_mode(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts) 12604 { 12605 struct tcp_sendfile_track *rc_cur, *orig_ent; 12606 struct tcpcb *tp; 12607 int err = 0; 12608 12609 orig_ent = rack->r_ctl.rc_last_sft; 12610 rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, seq); 12611 if (rc_cur == NULL) { 12612 /* If not in the beginning what about the end piece */ 12613 if (rack->rc_hybrid_mode) 12614 rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_RANGE, __LINE__, err); 12615 rc_cur = tcp_req_find_req_for_seq(rack->rc_tp, (seq + len - 1)); 12616 } else { 12617 err = 12345; 12618 } 12619 /* If we find no parameters we are in straight DGP mode */ 12620 if(rc_cur == NULL) { 12621 /* None found for this seq, just DGP for now */ 12622 if (rack->rc_hybrid_mode) { 12623 rack->r_ctl.client_suggested_maxseg = 0; 12624 rack->rc_catch_up = 0; 12625 if (rack->cspr_is_fcc == 0) 12626 rack->r_ctl.bw_rate_cap = 0; 12627 else 12628 rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap; 12629 } 12630 if (rack->rc_hybrid_mode) { 12631 rack_log_hybrid(rack, (seq + len - 1), NULL, HYBRID_LOG_NO_RANGE, __LINE__, err); 12632 } 12633 if (rack->r_ctl.rc_last_sft) { 12634 rack->r_ctl.rc_last_sft = NULL; 12635 } 12636 return; 12637 } 12638 if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_WASSET) == 0) { 12639 /* This entry was never setup for hybrid pacing on/off etc */ 12640 if (rack->rc_hybrid_mode) { 12641 rack->r_ctl.client_suggested_maxseg = 0; 12642 rack->rc_catch_up = 0; 12643 rack->r_ctl.bw_rate_cap = 0; 12644 } 12645 if (rack->r_ctl.rc_last_sft) { 12646 rack->r_ctl.rc_last_sft = NULL; 12647 } 12648 if ((rc_cur->flags & TCP_TRK_TRACK_FLG_FSND) == 0) { 12649 rc_cur->flags |= TCP_TRK_TRACK_FLG_FSND; 12650 rc_cur->first_send = cts; 12651 rc_cur->sent_at_fs = rack->rc_tp->t_sndbytes; 12652 rc_cur->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes; 12653 } 12654 return; 12655 } 12656 /* 12657 * Ok if we have a new entry *or* have never 12658 * set up an entry we need to proceed. If 12659 * we have already set it up this entry we 12660 * just continue along with what we already 12661 * setup. 12662 */ 12663 tp = rack->rc_tp; 12664 if ((rack->r_ctl.rc_last_sft != NULL) && 12665 (rack->r_ctl.rc_last_sft == rc_cur)) { 12666 /* Its already in place */ 12667 if (rack->rc_hybrid_mode) 12668 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_ISSAME, __LINE__, 0); 12669 return; 12670 } 12671 if (rack->rc_hybrid_mode == 0) { 12672 rack->r_ctl.rc_last_sft = rc_cur; 12673 if (orig_ent) { 12674 orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes; 12675 orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes; 12676 orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND; 12677 } 12678 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0); 12679 return; 12680 } 12681 if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CSPR) && rc_cur->cspr){ 12682 /* Compensate for all the header overhead's */ 12683 if (rack->cspr_is_fcc == 0) 12684 rack->r_ctl.bw_rate_cap = rack_compensate_for_linerate(rack, rc_cur->cspr); 12685 else 12686 rack->r_ctl.fillcw_cap = rack_compensate_for_linerate(rack, rc_cur->cspr); 12687 } else { 12688 if (rack->rc_hybrid_mode) { 12689 if (rack->cspr_is_fcc == 0) 12690 rack->r_ctl.bw_rate_cap = 0; 12691 else 12692 rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap; 12693 } 12694 } 12695 if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_H_MS) 12696 rack->r_ctl.client_suggested_maxseg = rc_cur->hint_maxseg; 12697 else 12698 rack->r_ctl.client_suggested_maxseg = 0; 12699 if (rc_cur->timestamp == rack->r_ctl.last_tm_mark) { 12700 /* 12701 * It is the same timestamp as the previous one 12702 * add the hybrid flag that will indicate we use 12703 * sendtime not arrival time for catch-up mode. 12704 */ 12705 rc_cur->hybrid_flags |= TCP_HYBRID_PACING_SENDTIME; 12706 } 12707 if ((rc_cur->hybrid_flags & TCP_HYBRID_PACING_CU) && 12708 (rc_cur->cspr > 0)) { 12709 uint64_t len; 12710 12711 rack->rc_catch_up = 1; 12712 /* 12713 * Calculate the deadline time, first set the 12714 * time to when the request arrived. 12715 */ 12716 if (rc_cur->hybrid_flags & TCP_HYBRID_PACING_SENDTIME) { 12717 /* 12718 * For cases where its a duplicate tm (we received more 12719 * than one request for a tm) we want to use now, the point 12720 * where we are just sending the first bit of the request. 12721 */ 12722 rc_cur->deadline = cts; 12723 } else { 12724 /* 12725 * Here we have a different tm from the last request 12726 * so we want to use arrival time as our base. 12727 */ 12728 rc_cur->deadline = rc_cur->localtime; 12729 } 12730 /* 12731 * Next calculate the length and compensate for 12732 * TLS if need be. 12733 */ 12734 len = rc_cur->end - rc_cur->start; 12735 if (tp->t_inpcb.inp_socket->so_snd.sb_tls_info) { 12736 /* 12737 * This session is doing TLS. Take a swag guess 12738 * at the overhead. 12739 */ 12740 len += tcp_estimate_tls_overhead(tp->t_inpcb.inp_socket, len); 12741 } 12742 /* 12743 * Now considering the size, and the cspr, what is the time that 12744 * would be required at the cspr rate. Here we use the raw 12745 * cspr value since the client only looks at the raw data. We 12746 * do use len which includes TLS overhead, but not the TCP/IP etc. 12747 * That will get made up for in the CU pacing rate set. 12748 */ 12749 len *= HPTS_USEC_IN_SEC; 12750 len /= rc_cur->cspr; 12751 rc_cur->deadline += len; 12752 } else { 12753 rack->rc_catch_up = 0; 12754 rc_cur->deadline = 0; 12755 } 12756 if (rack->r_ctl.client_suggested_maxseg != 0) { 12757 /* 12758 * We need to reset the max pace segs if we have a 12759 * client_suggested_maxseg. 12760 */ 12761 rack_set_pace_segments(tp, rack, __LINE__, NULL); 12762 } 12763 if (orig_ent) { 12764 orig_ent->sent_at_ls = rack->rc_tp->t_sndbytes; 12765 orig_ent->rxt_at_ls = rack->rc_tp->t_snd_rxt_bytes; 12766 orig_ent->flags |= TCP_TRK_TRACK_FLG_LSND; 12767 } 12768 rack_log_hybrid(rack, seq, rc_cur, HYBRID_LOG_RULES_APP, __LINE__, 0); 12769 /* Remember it for next time and for CU mode */ 12770 rack->r_ctl.rc_last_sft = rc_cur; 12771 rack->r_ctl.last_tm_mark = rc_cur->timestamp; 12772 } 12773 #endif 12774 12775 static void 12776 rack_chk_req_and_hybrid_on_out(struct tcp_rack *rack, tcp_seq seq, uint32_t len, uint64_t cts) 12777 { 12778 #ifdef TCP_REQUEST_TRK 12779 struct tcp_sendfile_track *ent; 12780 12781 ent = rack->r_ctl.rc_last_sft; 12782 if ((ent == NULL) || 12783 (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) || 12784 (SEQ_GEQ(seq, ent->end_seq))) { 12785 /* Time to update the track. */ 12786 rack_set_dgp_hybrid_mode(rack, seq, len, cts); 12787 ent = rack->r_ctl.rc_last_sft; 12788 } 12789 /* Out of all */ 12790 if (ent == NULL) { 12791 return; 12792 } 12793 if (SEQ_LT(ent->end_seq, (seq + len))) { 12794 /* 12795 * This is the case where our end_seq guess 12796 * was wrong. This is usually due to TLS having 12797 * more bytes then our guess. It could also be the 12798 * case that the client sent in two requests closely 12799 * and the SB is full of both so we are sending part 12800 * of each (end|beg). In such a case lets move this 12801 * guys end to match the end of this send. That 12802 * way it will complete when all of it is acked. 12803 */ 12804 ent->end_seq = (seq + len); 12805 if (rack->rc_hybrid_mode) 12806 rack_log_hybrid_bw(rack, seq, len, 0, 0, HYBRID_LOG_EXTEND, 0, ent, __LINE__); 12807 } 12808 /* Now validate we have set the send time of this one */ 12809 if ((ent->flags & TCP_TRK_TRACK_FLG_FSND) == 0) { 12810 ent->flags |= TCP_TRK_TRACK_FLG_FSND; 12811 ent->first_send = cts; 12812 ent->sent_at_fs = rack->rc_tp->t_sndbytes; 12813 ent->rxt_at_fs = rack->rc_tp->t_snd_rxt_bytes; 12814 } 12815 #endif 12816 } 12817 12818 static void 12819 rack_gain_for_fastoutput(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t acked_amount) 12820 { 12821 /* 12822 * The fast output path is enabled and we 12823 * have moved the cumack forward. Lets see if 12824 * we can expand forward the fast path length by 12825 * that amount. What we would ideally like to 12826 * do is increase the number of bytes in the 12827 * fast path block (left_to_send) by the 12828 * acked amount. However we have to gate that 12829 * by two factors: 12830 * 1) The amount outstanding and the rwnd of the peer 12831 * (i.e. we don't want to exceed the rwnd of the peer). 12832 * <and> 12833 * 2) The amount of data left in the socket buffer (i.e. 12834 * we can't send beyond what is in the buffer). 12835 * 12836 * Note that this does not take into account any increase 12837 * in the cwnd. We will only extend the fast path by 12838 * what was acked. 12839 */ 12840 uint32_t new_total, gating_val; 12841 12842 new_total = acked_amount + rack->r_ctl.fsb.left_to_send; 12843 gating_val = min((sbavail(&so->so_snd) - (tp->snd_max - tp->snd_una)), 12844 (tp->snd_wnd - (tp->snd_max - tp->snd_una))); 12845 if (new_total <= gating_val) { 12846 /* We can increase left_to_send by the acked amount */ 12847 counter_u64_add(rack_extended_rfo, 1); 12848 rack->r_ctl.fsb.left_to_send = new_total; 12849 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(&rack->rc_inp->inp_socket->so_snd) - (tp->snd_max - tp->snd_una))), 12850 ("rack:%p left_to_send:%u sbavail:%u out:%u", 12851 rack, rack->r_ctl.fsb.left_to_send, 12852 sbavail(&rack->rc_inp->inp_socket->so_snd), 12853 (tp->snd_max - tp->snd_una))); 12854 12855 } 12856 } 12857 12858 static void 12859 rack_adjust_sendmap_head(struct tcp_rack *rack, struct sockbuf *sb) 12860 { 12861 /* 12862 * Here any sendmap entry that points to the 12863 * beginning mbuf must be adjusted to the correct 12864 * offset. This must be called with: 12865 * 1) The socket buffer locked 12866 * 2) snd_una adjusted to its new position. 12867 * 12868 * Note that (2) implies rack_ack_received has also 12869 * been called and all the sbcut's have been done. 12870 * 12871 * We grab the first mbuf in the socket buffer and 12872 * then go through the front of the sendmap, recalculating 12873 * the stored offset for any sendmap entry that has 12874 * that mbuf. We must use the sb functions to do this 12875 * since its possible an add was done has well as 12876 * the subtraction we may have just completed. This should 12877 * not be a penalty though, since we just referenced the sb 12878 * to go in and trim off the mbufs that we freed (of course 12879 * there will be a penalty for the sendmap references though). 12880 * 12881 * Note also with INVARIANT on, we validate with a KASSERT 12882 * that the first sendmap entry has a soff of 0. 12883 * 12884 */ 12885 struct mbuf *m; 12886 struct rack_sendmap *rsm; 12887 tcp_seq snd_una; 12888 #ifdef INVARIANTS 12889 int first_processed = 0; 12890 #endif 12891 12892 snd_una = rack->rc_tp->snd_una; 12893 SOCKBUF_LOCK_ASSERT(sb); 12894 m = sb->sb_mb; 12895 rsm = tqhash_min(rack->r_ctl.tqh); 12896 if ((rsm == NULL) || (m == NULL)) { 12897 /* Nothing outstanding */ 12898 return; 12899 } 12900 /* The very first RSM's mbuf must point to the head mbuf in the sb */ 12901 KASSERT((rsm->m == m), 12902 ("Rack:%p sb:%p rsm:%p -- first rsm mbuf not aligned to sb", 12903 rack, sb, rsm)); 12904 while (rsm->m && (rsm->m == m)) { 12905 /* one to adjust */ 12906 #ifdef INVARIANTS 12907 struct mbuf *tm; 12908 uint32_t soff; 12909 12910 tm = sbsndmbuf(sb, (rsm->r_start - snd_una), &soff); 12911 if ((rsm->orig_m_len != m->m_len) || 12912 (rsm->orig_t_space != M_TRAILINGROOM(m))){ 12913 rack_adjust_orig_mlen(rsm); 12914 } 12915 if (first_processed == 0) { 12916 KASSERT((rsm->soff == 0), 12917 ("Rack:%p rsm:%p -- rsm at head but soff not zero", 12918 rack, rsm)); 12919 first_processed = 1; 12920 } 12921 if ((rsm->soff != soff) || (rsm->m != tm)) { 12922 /* 12923 * This is not a fatal error, we anticipate it 12924 * might happen (the else code), so we count it here 12925 * so that under invariant we can see that it really 12926 * does happen. 12927 */ 12928 counter_u64_add(rack_adjust_map_bw, 1); 12929 } 12930 rsm->m = tm; 12931 rsm->soff = soff; 12932 if (tm) { 12933 rsm->orig_m_len = rsm->m->m_len; 12934 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 12935 } else { 12936 rsm->orig_m_len = 0; 12937 rsm->orig_t_space = 0; 12938 } 12939 #else 12940 rsm->m = sbsndmbuf(sb, (rsm->r_start - snd_una), &rsm->soff); 12941 if (rsm->m) { 12942 rsm->orig_m_len = rsm->m->m_len; 12943 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 12944 } else { 12945 rsm->orig_m_len = 0; 12946 rsm->orig_t_space = 0; 12947 } 12948 #endif 12949 rsm = tqhash_next(rack->r_ctl.tqh, rsm); 12950 if (rsm == NULL) 12951 break; 12952 } 12953 } 12954 12955 #ifdef TCP_REQUEST_TRK 12956 static inline void 12957 rack_req_check_for_comp(struct tcp_rack *rack, tcp_seq th_ack) 12958 { 12959 struct tcp_sendfile_track *ent; 12960 int i; 12961 12962 if ((rack->rc_hybrid_mode == 0) && 12963 (tcp_bblogging_point_on(rack->rc_tp, TCP_BBPOINT_REQ_LEVEL_LOGGING) == 0)) { 12964 /* 12965 * Just do normal completions hybrid pacing is not on 12966 * and CLDL is off as well. 12967 */ 12968 tcp_req_check_for_comp(rack->rc_tp, th_ack); 12969 return; 12970 } 12971 /* 12972 * Originally I was just going to find the th_ack associated 12973 * with an entry. But then I realized a large strech ack could 12974 * in theory ack two or more requests at once. So instead we 12975 * need to find all entries that are completed by th_ack not 12976 * just a single entry and do our logging. 12977 */ 12978 ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i); 12979 while (ent != NULL) { 12980 /* 12981 * We may be doing hybrid pacing or CLDL and need more details possibly 12982 * so we do it manually instead of calling 12983 * tcp_req_check_for_comp() 12984 */ 12985 uint64_t laa, tim, data, cbw, ftim; 12986 12987 /* Ok this ack frees it */ 12988 rack_log_hybrid(rack, th_ack, 12989 ent, HYBRID_LOG_REQ_COMP, __LINE__, 0); 12990 rack_log_hybrid_sends(rack, ent, __LINE__); 12991 /* calculate the time based on the ack arrival */ 12992 data = ent->end - ent->start; 12993 laa = tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time); 12994 if (ent->flags & TCP_TRK_TRACK_FLG_FSND) { 12995 if (ent->first_send > ent->localtime) 12996 ftim = ent->first_send; 12997 else 12998 ftim = ent->localtime; 12999 } else { 13000 /* TSNH */ 13001 ftim = ent->localtime; 13002 } 13003 if (laa > ent->localtime) 13004 tim = laa - ftim; 13005 else 13006 tim = 0; 13007 cbw = data * HPTS_USEC_IN_SEC; 13008 if (tim > 0) 13009 cbw /= tim; 13010 else 13011 cbw = 0; 13012 rack_log_hybrid_bw(rack, th_ack, cbw, tim, data, HYBRID_LOG_BW_MEASURE, 0, ent, __LINE__); 13013 /* 13014 * Check to see if we are freeing what we are pointing to send wise 13015 * if so be sure to NULL the pointer so we know we are no longer 13016 * set to anything. 13017 */ 13018 if (ent == rack->r_ctl.rc_last_sft) { 13019 rack->r_ctl.rc_last_sft = NULL; 13020 if (rack->rc_hybrid_mode) { 13021 rack->rc_catch_up = 0; 13022 if (rack->cspr_is_fcc == 0) 13023 rack->r_ctl.bw_rate_cap = 0; 13024 else 13025 rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap; 13026 rack->r_ctl.client_suggested_maxseg = 0; 13027 } 13028 } 13029 /* Generate the log that the tcp_netflix call would have */ 13030 tcp_req_log_req_info(rack->rc_tp, ent, 13031 i, TCP_TRK_REQ_LOG_FREED, 0, 0); 13032 /* Free it and see if there is another one */ 13033 tcp_req_free_a_slot(rack->rc_tp, ent); 13034 ent = tcp_req_find_a_req_that_is_completed_by(rack->rc_tp, th_ack, &i); 13035 } 13036 } 13037 #endif 13038 13039 13040 /* 13041 * Return value of 1, we do not need to call rack_process_data(). 13042 * return value of 0, rack_process_data can be called. 13043 * For ret_val if its 0 the TCP is locked, if its non-zero 13044 * its unlocked and probably unsafe to touch the TCB. 13045 */ 13046 static int 13047 rack_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 13048 struct tcpcb *tp, struct tcpopt *to, 13049 uint32_t tiwin, int32_t tlen, 13050 int32_t * ofia, int32_t thflags, int32_t *ret_val, int32_t orig_tlen) 13051 { 13052 int32_t ourfinisacked = 0; 13053 int32_t nsegs, acked_amount; 13054 int32_t acked; 13055 struct mbuf *mfree; 13056 struct tcp_rack *rack; 13057 int32_t under_pacing = 0; 13058 int32_t post_recovery = 0; 13059 uint32_t p_cwnd; 13060 13061 INP_WLOCK_ASSERT(tptoinpcb(tp)); 13062 13063 rack = (struct tcp_rack *)tp->t_fb_ptr; 13064 if (SEQ_GT(th->th_ack, tp->snd_max)) { 13065 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val, 13066 &rack->r_ctl.challenge_ack_ts, 13067 &rack->r_ctl.challenge_ack_cnt); 13068 rack->r_wanted_output = 1; 13069 return (1); 13070 } 13071 if (rack->gp_ready && 13072 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 13073 under_pacing = 1; 13074 } 13075 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 13076 int in_rec, dup_ack_struck = 0; 13077 int dsack_seen = 0, sacks_seen = 0; 13078 13079 in_rec = IN_FASTRECOVERY(tp->t_flags); 13080 if (rack->rc_in_persist) { 13081 tp->t_rxtshift = 0; 13082 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 13083 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 13084 } 13085 13086 if ((th->th_ack == tp->snd_una) && 13087 (tiwin == tp->snd_wnd) && 13088 (orig_tlen == 0) && 13089 ((to->to_flags & TOF_SACK) == 0)) { 13090 rack_strike_dupack(rack, th->th_ack); 13091 dup_ack_struck = 1; 13092 } 13093 rack_log_ack(tp, to, th, ((in_rec == 0) && IN_FASTRECOVERY(tp->t_flags)), 13094 dup_ack_struck, &dsack_seen, &sacks_seen); 13095 if ((rack->sack_attack_disable > 0) && 13096 (th->th_ack == tp->snd_una) && 13097 (tiwin == tp->snd_wnd) && 13098 (orig_tlen == 0) && 13099 (dsack_seen == 0) && 13100 (sacks_seen > 0)) { 13101 /* 13102 * If sacks have been disabled we may 13103 * want to strike a dup-ack "ignoring" the 13104 * sack as long as the sack was not a "dsack". Note 13105 * that if no sack is sent (TOF_SACK is off) then the 13106 * normal dsack code above rack_log_ack() would have 13107 * already struck. So this is just to catch the case 13108 * were we are ignoring sacks from this guy due to 13109 * it being a suspected attacker. 13110 */ 13111 rack_strike_dupack(rack, th->th_ack); 13112 } 13113 13114 } 13115 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 13116 /* 13117 * Old ack, behind (or duplicate to) the last one rcv'd 13118 * Note: We mark reordering is occuring if its 13119 * less than and we have not closed our window. 13120 */ 13121 if (SEQ_LT(th->th_ack, tp->snd_una) && (sbspace(&so->so_rcv) > ctf_fixed_maxseg(tp))) { 13122 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 13123 if (rack->r_ctl.rc_reorder_ts == 0) 13124 rack->r_ctl.rc_reorder_ts = 1; 13125 } 13126 return (0); 13127 } 13128 /* 13129 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 13130 * something we sent. 13131 */ 13132 if (tp->t_flags & TF_NEEDSYN) { 13133 /* 13134 * T/TCP: Connection was half-synchronized, and our SYN has 13135 * been ACK'd (so connection is now fully synchronized). Go 13136 * to non-starred state, increment snd_una for ACK of SYN, 13137 * and check if we can do window scaling. 13138 */ 13139 tp->t_flags &= ~TF_NEEDSYN; 13140 tp->snd_una++; 13141 /* Do window scaling? */ 13142 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 13143 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 13144 tp->rcv_scale = tp->request_r_scale; 13145 /* Send window already scaled. */ 13146 } 13147 } 13148 nsegs = max(1, m->m_pkthdr.lro_nsegs); 13149 13150 acked = BYTES_THIS_ACK(tp, th); 13151 if (acked) { 13152 /* 13153 * Any time we move the cum-ack forward clear 13154 * keep-alive tied probe-not-answered. The 13155 * persists clears its own on entry. 13156 */ 13157 rack->probe_not_answered = 0; 13158 } 13159 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs); 13160 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 13161 /* 13162 * If we just performed our first retransmit, and the ACK arrives 13163 * within our recovery window, then it was a mistake to do the 13164 * retransmit in the first place. Recover our original cwnd and 13165 * ssthresh, and proceed to transmit where we left off. 13166 */ 13167 if ((tp->t_flags & TF_PREVVALID) && 13168 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 13169 tp->t_flags &= ~TF_PREVVALID; 13170 if (tp->t_rxtshift == 1 && 13171 (int)(ticks - tp->t_badrxtwin) < 0) 13172 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__); 13173 } 13174 if (acked) { 13175 /* assure we are not backed off */ 13176 tp->t_rxtshift = 0; 13177 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 13178 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 13179 rack->rc_tlp_in_progress = 0; 13180 rack->r_ctl.rc_tlp_cnt_out = 0; 13181 /* 13182 * If it is the RXT timer we want to 13183 * stop it, so we can restart a TLP. 13184 */ 13185 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 13186 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13187 #ifdef TCP_REQUEST_TRK 13188 rack_req_check_for_comp(rack, th->th_ack); 13189 #endif 13190 } 13191 /* 13192 * If we have a timestamp reply, update smoothed round trip time. If 13193 * no timestamp is present but transmit timer is running and timed 13194 * sequence number was acked, update smoothed round trip time. Since 13195 * we now have an rtt measurement, cancel the timer backoff (cf., 13196 * Phil Karn's retransmit alg.). Recompute the initial retransmit 13197 * timer. 13198 * 13199 * Some boxes send broken timestamp replies during the SYN+ACK 13200 * phase, ignore timestamps of 0 or we could calculate a huge RTT 13201 * and blow up the retransmit timer. 13202 */ 13203 /* 13204 * If all outstanding data is acked, stop retransmit timer and 13205 * remember to restart (more output or persist). If there is more 13206 * data to be acked, restart retransmit timer, using current 13207 * (possibly backed-off) value. 13208 */ 13209 if (acked == 0) { 13210 if (ofia) 13211 *ofia = ourfinisacked; 13212 return (0); 13213 } 13214 if (IN_RECOVERY(tp->t_flags)) { 13215 if (SEQ_LT(th->th_ack, tp->snd_recover) && 13216 (SEQ_LT(th->th_ack, tp->snd_max))) { 13217 tcp_rack_partialack(tp); 13218 } else { 13219 rack_post_recovery(tp, th->th_ack); 13220 post_recovery = 1; 13221 /* 13222 * Grab the segsiz, multiply by 2 and add the snd_cwnd 13223 * that is the max the CC should add if we are exiting 13224 * recovery and doing a late add. 13225 */ 13226 p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 13227 p_cwnd <<= 1; 13228 p_cwnd += tp->snd_cwnd; 13229 } 13230 } else if ((rack->rto_from_rec == 1) && 13231 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 13232 /* 13233 * We were in recovery, hit a rxt timeout 13234 * and never re-entered recovery. The timeout(s) 13235 * made up all the lost data. In such a case 13236 * we need to clear the rto_from_rec flag. 13237 */ 13238 rack->rto_from_rec = 0; 13239 } 13240 /* 13241 * Let the congestion control algorithm update congestion control 13242 * related information. This typically means increasing the 13243 * congestion window. 13244 */ 13245 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, post_recovery); 13246 if (post_recovery && 13247 (tp->snd_cwnd > p_cwnd)) { 13248 /* Must be non-newreno (cubic) getting too ahead of itself */ 13249 tp->snd_cwnd = p_cwnd; 13250 } 13251 SOCKBUF_LOCK(&so->so_snd); 13252 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 13253 tp->snd_wnd -= acked_amount; 13254 mfree = sbcut_locked(&so->so_snd, acked_amount); 13255 if ((sbused(&so->so_snd) == 0) && 13256 (acked > acked_amount) && 13257 (tp->t_state >= TCPS_FIN_WAIT_1) && 13258 (tp->t_flags & TF_SENTFIN)) { 13259 /* 13260 * We must be sure our fin 13261 * was sent and acked (we can be 13262 * in FIN_WAIT_1 without having 13263 * sent the fin). 13264 */ 13265 ourfinisacked = 1; 13266 } 13267 tp->snd_una = th->th_ack; 13268 /* wakeups? */ 13269 if (acked_amount && sbavail(&so->so_snd)) 13270 rack_adjust_sendmap_head(rack, &so->so_snd); 13271 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 13272 /* NB: sowwakeup_locked() does an implicit unlock. */ 13273 sowwakeup_locked(so); 13274 m_freem(mfree); 13275 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 13276 tp->snd_recover = tp->snd_una; 13277 13278 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13279 tp->snd_nxt = tp->snd_max; 13280 } 13281 if (under_pacing && 13282 (rack->use_fixed_rate == 0) && 13283 (rack->in_probe_rtt == 0) && 13284 rack->rc_gp_dyn_mul && 13285 rack->rc_always_pace) { 13286 /* Check if we are dragging bottom */ 13287 rack_check_bottom_drag(tp, rack, so); 13288 } 13289 if (tp->snd_una == tp->snd_max) { 13290 /* Nothing left outstanding */ 13291 tp->t_flags &= ~TF_PREVVALID; 13292 rack->r_ctl.idle_snd_una = tp->snd_una; 13293 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 13294 if (rack->r_ctl.rc_went_idle_time == 0) 13295 rack->r_ctl.rc_went_idle_time = 1; 13296 rack->r_ctl.retran_during_recovery = 0; 13297 rack->r_ctl.dsack_byte_cnt = 0; 13298 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 13299 if (sbavail(&tptosocket(tp)->so_snd) == 0) 13300 tp->t_acktime = 0; 13301 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 13302 rack->rc_suspicious = 0; 13303 /* Set need output so persist might get set */ 13304 rack->r_wanted_output = 1; 13305 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 13306 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 13307 (sbavail(&so->so_snd) == 0) && 13308 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 13309 /* 13310 * The socket was gone and the 13311 * peer sent data (now or in the past), time to 13312 * reset him. 13313 */ 13314 *ret_val = 1; 13315 /* tcp_close will kill the inp pre-log the Reset */ 13316 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 13317 tp = tcp_close(tp); 13318 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 13319 return (1); 13320 } 13321 } 13322 if (ofia) 13323 *ofia = ourfinisacked; 13324 return (0); 13325 } 13326 13327 13328 static void 13329 rack_log_collapse(struct tcp_rack *rack, uint32_t cnt, uint32_t split, uint32_t out, int line, 13330 int dir, uint32_t flags, struct rack_sendmap *rsm) 13331 { 13332 if (tcp_bblogging_on(rack->rc_tp)) { 13333 union tcp_log_stackspecific log; 13334 struct timeval tv; 13335 13336 memset(&log, 0, sizeof(log)); 13337 log.u_bbr.flex1 = cnt; 13338 log.u_bbr.flex2 = split; 13339 log.u_bbr.flex3 = out; 13340 log.u_bbr.flex4 = line; 13341 log.u_bbr.flex5 = rack->r_must_retran; 13342 log.u_bbr.flex6 = flags; 13343 log.u_bbr.flex7 = rack->rc_has_collapsed; 13344 log.u_bbr.flex8 = dir; /* 13345 * 1 is collapsed, 0 is uncollapsed, 13346 * 2 is log of a rsm being marked, 3 is a split. 13347 */ 13348 if (rsm == NULL) 13349 log.u_bbr.rttProp = 0; 13350 else 13351 log.u_bbr.rttProp = (uint64_t)rsm; 13352 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 13353 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 13354 TCP_LOG_EVENTP(rack->rc_tp, NULL, 13355 &rack->rc_inp->inp_socket->so_rcv, 13356 &rack->rc_inp->inp_socket->so_snd, 13357 TCP_RACK_LOG_COLLAPSE, 0, 13358 0, &log, false, &tv); 13359 } 13360 } 13361 13362 static void 13363 rack_collapsed_window(struct tcp_rack *rack, uint32_t out, tcp_seq th_ack, int line) 13364 { 13365 /* 13366 * Here all we do is mark the collapsed point and set the flag. 13367 * This may happen again and again, but there is no 13368 * sense splitting our map until we know where the 13369 * peer finally lands in the collapse. 13370 */ 13371 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND); 13372 if ((rack->rc_has_collapsed == 0) || 13373 (rack->r_ctl.last_collapse_point != (th_ack + rack->rc_tp->snd_wnd))) 13374 counter_u64_add(rack_collapsed_win_seen, 1); 13375 rack->r_ctl.last_collapse_point = th_ack + rack->rc_tp->snd_wnd; 13376 rack->r_ctl.high_collapse_point = rack->rc_tp->snd_max; 13377 rack->rc_has_collapsed = 1; 13378 rack->r_collapse_point_valid = 1; 13379 rack_log_collapse(rack, 0, th_ack, rack->r_ctl.last_collapse_point, line, 1, 0, NULL); 13380 } 13381 13382 static void 13383 rack_un_collapse_window(struct tcp_rack *rack, int line) 13384 { 13385 struct rack_sendmap *nrsm, *rsm; 13386 int cnt = 0, split = 0; 13387 int insret __diagused; 13388 13389 13390 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_WND); 13391 rack->rc_has_collapsed = 0; 13392 rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point); 13393 if (rsm == NULL) { 13394 /* Nothing to do maybe the peer ack'ed it all */ 13395 rack_log_collapse(rack, 0, 0, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL); 13396 return; 13397 } 13398 /* Now do we need to split this one? */ 13399 if (SEQ_GT(rack->r_ctl.last_collapse_point, rsm->r_start)) { 13400 rack_log_collapse(rack, rsm->r_start, rsm->r_end, 13401 rack->r_ctl.last_collapse_point, line, 3, rsm->r_flags, rsm); 13402 nrsm = rack_alloc_limit(rack, RACK_LIMIT_TYPE_SPLIT); 13403 if (nrsm == NULL) { 13404 /* We can't get a rsm, mark all? */ 13405 nrsm = rsm; 13406 goto no_split; 13407 } 13408 /* Clone it */ 13409 split = 1; 13410 rack_clone_rsm(rack, nrsm, rsm, rack->r_ctl.last_collapse_point); 13411 #ifndef INVARIANTS 13412 (void)tqhash_insert(rack->r_ctl.tqh, nrsm); 13413 #else 13414 if ((insret = tqhash_insert(rack->r_ctl.tqh, nrsm)) != 0) { 13415 panic("Insert in tailq_hash of %p fails ret:%d rack:%p rsm:%p", 13416 nrsm, insret, rack, rsm); 13417 } 13418 #endif 13419 rack_log_map_chg(rack->rc_tp, rack, NULL, rsm, nrsm, MAP_SPLIT, 13420 rack->r_ctl.last_collapse_point, __LINE__); 13421 if (rsm->r_in_tmap) { 13422 TAILQ_INSERT_AFTER(&rack->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 13423 nrsm->r_in_tmap = 1; 13424 } 13425 /* 13426 * Set in the new RSM as the 13427 * collapsed starting point 13428 */ 13429 rsm = nrsm; 13430 } 13431 13432 no_split: 13433 TQHASH_FOREACH_FROM(nrsm, rack->r_ctl.tqh, rsm) { 13434 cnt++; 13435 nrsm->r_flags |= RACK_RWND_COLLAPSED; 13436 rack_log_collapse(rack, nrsm->r_start, nrsm->r_end, 0, line, 4, nrsm->r_flags, nrsm); 13437 cnt++; 13438 } 13439 if (cnt) { 13440 counter_u64_add(rack_collapsed_win, 1); 13441 } 13442 rack_log_collapse(rack, cnt, split, ctf_outstanding(rack->rc_tp), line, 0, 0, NULL); 13443 } 13444 13445 static void 13446 rack_handle_delayed_ack(struct tcpcb *tp, struct tcp_rack *rack, 13447 int32_t tlen, int32_t tfo_syn) 13448 { 13449 if (DELAY_ACK(tp, tlen) || tfo_syn) { 13450 rack_timer_cancel(tp, rack, 13451 rack->r_ctl.rc_rcvtime, __LINE__); 13452 tp->t_flags |= TF_DELACK; 13453 } else { 13454 rack->r_wanted_output = 1; 13455 tp->t_flags |= TF_ACKNOW; 13456 } 13457 } 13458 13459 static void 13460 rack_validate_fo_sendwin_up(struct tcpcb *tp, struct tcp_rack *rack) 13461 { 13462 /* 13463 * If fast output is in progress, lets validate that 13464 * the new window did not shrink on us and make it 13465 * so fast output should end. 13466 */ 13467 if (rack->r_fast_output) { 13468 uint32_t out; 13469 13470 /* 13471 * Calculate what we will send if left as is 13472 * and compare that to our send window. 13473 */ 13474 out = ctf_outstanding(tp); 13475 if ((out + rack->r_ctl.fsb.left_to_send) > tp->snd_wnd) { 13476 /* ok we have an issue */ 13477 if (out >= tp->snd_wnd) { 13478 /* Turn off fast output the window is met or collapsed */ 13479 rack->r_fast_output = 0; 13480 } else { 13481 /* we have some room left */ 13482 rack->r_ctl.fsb.left_to_send = tp->snd_wnd - out; 13483 if (rack->r_ctl.fsb.left_to_send < ctf_fixed_maxseg(tp)) { 13484 /* If not at least 1 full segment never mind */ 13485 rack->r_fast_output = 0; 13486 } 13487 } 13488 } 13489 } 13490 } 13491 13492 /* 13493 * Return value of 1, the TCB is unlocked and most 13494 * likely gone, return value of 0, the TCP is still 13495 * locked. 13496 */ 13497 static int 13498 rack_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 13499 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 13500 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 13501 { 13502 /* 13503 * Update window information. Don't look at window if no ACK: TAC's 13504 * send garbage on first SYN. 13505 */ 13506 int32_t nsegs; 13507 int32_t tfo_syn; 13508 struct tcp_rack *rack; 13509 13510 INP_WLOCK_ASSERT(tptoinpcb(tp)); 13511 13512 rack = (struct tcp_rack *)tp->t_fb_ptr; 13513 nsegs = max(1, m->m_pkthdr.lro_nsegs); 13514 if ((thflags & TH_ACK) && 13515 (SEQ_LT(tp->snd_wl1, th->th_seq) || 13516 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 13517 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 13518 /* keep track of pure window updates */ 13519 if (tlen == 0 && 13520 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 13521 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 13522 tp->snd_wnd = tiwin; 13523 rack_validate_fo_sendwin_up(tp, rack); 13524 tp->snd_wl1 = th->th_seq; 13525 tp->snd_wl2 = th->th_ack; 13526 if (tp->snd_wnd > tp->max_sndwnd) 13527 tp->max_sndwnd = tp->snd_wnd; 13528 rack->r_wanted_output = 1; 13529 } else if (thflags & TH_ACK) { 13530 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 13531 tp->snd_wnd = tiwin; 13532 rack_validate_fo_sendwin_up(tp, rack); 13533 tp->snd_wl1 = th->th_seq; 13534 tp->snd_wl2 = th->th_ack; 13535 } 13536 } 13537 if (tp->snd_wnd < ctf_outstanding(tp)) 13538 /* The peer collapsed the window */ 13539 rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__); 13540 else if (rack->rc_has_collapsed) 13541 rack_un_collapse_window(rack, __LINE__); 13542 if ((rack->r_collapse_point_valid) && 13543 (SEQ_GT(th->th_ack, rack->r_ctl.high_collapse_point))) 13544 rack->r_collapse_point_valid = 0; 13545 /* Was persist timer active and now we have window space? */ 13546 if ((rack->rc_in_persist != 0) && 13547 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 13548 rack->r_ctl.rc_pace_min_segs))) { 13549 rack_exit_persist(tp, rack, rack->r_ctl.rc_rcvtime); 13550 tp->snd_nxt = tp->snd_max; 13551 /* Make sure we output to start the timer */ 13552 rack->r_wanted_output = 1; 13553 } 13554 /* Do we enter persists? */ 13555 if ((rack->rc_in_persist == 0) && 13556 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 13557 TCPS_HAVEESTABLISHED(tp->t_state) && 13558 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) && 13559 sbavail(&tptosocket(tp)->so_snd) && 13560 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) { 13561 /* 13562 * Here the rwnd is less than 13563 * the pacing size, we are established, 13564 * nothing is outstanding, and there is 13565 * data to send. Enter persists. 13566 */ 13567 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una); 13568 } 13569 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 13570 m_freem(m); 13571 return (0); 13572 } 13573 /* 13574 * don't process the URG bit, ignore them drag 13575 * along the up. 13576 */ 13577 tp->rcv_up = tp->rcv_nxt; 13578 13579 /* 13580 * Process the segment text, merging it into the TCP sequencing 13581 * queue, and arranging for acknowledgment of receipt if necessary. 13582 * This process logically involves adjusting tp->rcv_wnd as data is 13583 * presented to the user (this happens in tcp_usrreq.c, case 13584 * PRU_RCVD). If a FIN has already been received on this connection 13585 * then we just ignore the text. 13586 */ 13587 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 13588 (tp->t_flags & TF_FASTOPEN)); 13589 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 13590 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 13591 tcp_seq save_start = th->th_seq; 13592 tcp_seq save_rnxt = tp->rcv_nxt; 13593 int save_tlen = tlen; 13594 13595 m_adj(m, drop_hdrlen); /* delayed header drop */ 13596 /* 13597 * Insert segment which includes th into TCP reassembly 13598 * queue with control block tp. Set thflags to whether 13599 * reassembly now includes a segment with FIN. This handles 13600 * the common case inline (segment is the next to be 13601 * received on an established connection, and the queue is 13602 * empty), avoiding linkage into and removal from the queue 13603 * and repetition of various conversions. Set DELACK for 13604 * segments received in order, but ack immediately when 13605 * segments are out of order (so fast retransmit can work). 13606 */ 13607 if (th->th_seq == tp->rcv_nxt && 13608 SEGQ_EMPTY(tp) && 13609 (TCPS_HAVEESTABLISHED(tp->t_state) || 13610 tfo_syn)) { 13611 #ifdef NETFLIX_SB_LIMITS 13612 u_int mcnt, appended; 13613 13614 if (so->so_rcv.sb_shlim) { 13615 mcnt = m_memcnt(m); 13616 appended = 0; 13617 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 13618 CFO_NOSLEEP, NULL) == false) { 13619 counter_u64_add(tcp_sb_shlim_fails, 1); 13620 m_freem(m); 13621 return (0); 13622 } 13623 } 13624 #endif 13625 rack_handle_delayed_ack(tp, rack, tlen, tfo_syn); 13626 tp->rcv_nxt += tlen; 13627 if (tlen && 13628 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 13629 (tp->t_fbyte_in == 0)) { 13630 tp->t_fbyte_in = ticks; 13631 if (tp->t_fbyte_in == 0) 13632 tp->t_fbyte_in = 1; 13633 if (tp->t_fbyte_out && tp->t_fbyte_in) 13634 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 13635 } 13636 thflags = tcp_get_flags(th) & TH_FIN; 13637 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs); 13638 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 13639 SOCKBUF_LOCK(&so->so_rcv); 13640 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 13641 m_freem(m); 13642 } else { 13643 int32_t newsize; 13644 13645 if (tlen > 0) { 13646 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 13647 if (newsize) 13648 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 13649 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 13650 } 13651 #ifdef NETFLIX_SB_LIMITS 13652 appended = 13653 #endif 13654 sbappendstream_locked(&so->so_rcv, m, 0); 13655 } 13656 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1); 13657 /* NB: sorwakeup_locked() does an implicit unlock. */ 13658 sorwakeup_locked(so); 13659 #ifdef NETFLIX_SB_LIMITS 13660 if (so->so_rcv.sb_shlim && appended != mcnt) 13661 counter_fo_release(so->so_rcv.sb_shlim, 13662 mcnt - appended); 13663 #endif 13664 } else { 13665 /* 13666 * XXX: Due to the header drop above "th" is 13667 * theoretically invalid by now. Fortunately 13668 * m_adj() doesn't actually frees any mbufs when 13669 * trimming from the head. 13670 */ 13671 tcp_seq temp = save_start; 13672 13673 thflags = tcp_reass(tp, th, &temp, &tlen, m); 13674 tp->t_flags |= TF_ACKNOW; 13675 if (tp->t_flags & TF_WAKESOR) { 13676 tp->t_flags &= ~TF_WAKESOR; 13677 /* NB: sorwakeup_locked() does an implicit unlock. */ 13678 sorwakeup_locked(so); 13679 } 13680 } 13681 if ((tp->t_flags & TF_SACK_PERMIT) && 13682 (save_tlen > 0) && 13683 TCPS_HAVEESTABLISHED(tp->t_state)) { 13684 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 13685 /* 13686 * DSACK actually handled in the fastpath 13687 * above. 13688 */ 13689 tcp_update_sack_list(tp, save_start, 13690 save_start + save_tlen); 13691 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 13692 if ((tp->rcv_numsacks >= 1) && 13693 (tp->sackblks[0].end == save_start)) { 13694 /* 13695 * Partial overlap, recorded at todrop 13696 * above. 13697 */ 13698 tcp_update_sack_list(tp, 13699 tp->sackblks[0].start, 13700 tp->sackblks[0].end); 13701 } else { 13702 tcp_update_dsack_list(tp, save_start, 13703 save_start + save_tlen); 13704 } 13705 } else if (tlen >= save_tlen) { 13706 /* Update of sackblks. */ 13707 tcp_update_dsack_list(tp, save_start, 13708 save_start + save_tlen); 13709 } else if (tlen > 0) { 13710 tcp_update_dsack_list(tp, save_start, 13711 save_start + tlen); 13712 } 13713 } 13714 } else { 13715 m_freem(m); 13716 thflags &= ~TH_FIN; 13717 } 13718 13719 /* 13720 * If FIN is received ACK the FIN and let the user know that the 13721 * connection is closing. 13722 */ 13723 if (thflags & TH_FIN) { 13724 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 13725 /* The socket upcall is handled by socantrcvmore. */ 13726 socantrcvmore(so); 13727 /* 13728 * If connection is half-synchronized (ie NEEDSYN 13729 * flag on) then delay ACK, so it may be piggybacked 13730 * when SYN is sent. Otherwise, since we received a 13731 * FIN then no more input can be expected, send ACK 13732 * now. 13733 */ 13734 if (tp->t_flags & TF_NEEDSYN) { 13735 rack_timer_cancel(tp, rack, 13736 rack->r_ctl.rc_rcvtime, __LINE__); 13737 tp->t_flags |= TF_DELACK; 13738 } else { 13739 tp->t_flags |= TF_ACKNOW; 13740 } 13741 tp->rcv_nxt++; 13742 } 13743 switch (tp->t_state) { 13744 /* 13745 * In SYN_RECEIVED and ESTABLISHED STATES enter the 13746 * CLOSE_WAIT state. 13747 */ 13748 case TCPS_SYN_RECEIVED: 13749 tp->t_starttime = ticks; 13750 /* FALLTHROUGH */ 13751 case TCPS_ESTABLISHED: 13752 rack_timer_cancel(tp, rack, 13753 rack->r_ctl.rc_rcvtime, __LINE__); 13754 tcp_state_change(tp, TCPS_CLOSE_WAIT); 13755 break; 13756 13757 /* 13758 * If still in FIN_WAIT_1 STATE FIN has not been 13759 * acked so enter the CLOSING state. 13760 */ 13761 case TCPS_FIN_WAIT_1: 13762 rack_timer_cancel(tp, rack, 13763 rack->r_ctl.rc_rcvtime, __LINE__); 13764 tcp_state_change(tp, TCPS_CLOSING); 13765 break; 13766 13767 /* 13768 * In FIN_WAIT_2 state enter the TIME_WAIT state, 13769 * starting the time-wait timer, turning off the 13770 * other standard timers. 13771 */ 13772 case TCPS_FIN_WAIT_2: 13773 rack_timer_cancel(tp, rack, 13774 rack->r_ctl.rc_rcvtime, __LINE__); 13775 tcp_twstart(tp); 13776 return (1); 13777 } 13778 } 13779 /* 13780 * Return any desired output. 13781 */ 13782 if ((tp->t_flags & TF_ACKNOW) || 13783 (sbavail(&so->so_snd) > (tp->snd_max - tp->snd_una))) { 13784 rack->r_wanted_output = 1; 13785 } 13786 return (0); 13787 } 13788 13789 /* 13790 * Here nothing is really faster, its just that we 13791 * have broken out the fast-data path also just like 13792 * the fast-ack. 13793 */ 13794 static int 13795 rack_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 13796 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 13797 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 13798 { 13799 int32_t nsegs; 13800 int32_t newsize = 0; /* automatic sockbuf scaling */ 13801 struct tcp_rack *rack; 13802 #ifdef NETFLIX_SB_LIMITS 13803 u_int mcnt, appended; 13804 #endif 13805 13806 /* 13807 * If last ACK falls within this segment's sequence numbers, record 13808 * the timestamp. NOTE that the test is modified according to the 13809 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 13810 */ 13811 if (__predict_false(th->th_seq != tp->rcv_nxt)) { 13812 return (0); 13813 } 13814 if (tiwin && tiwin != tp->snd_wnd) { 13815 return (0); 13816 } 13817 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 13818 return (0); 13819 } 13820 if (__predict_false((to->to_flags & TOF_TS) && 13821 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 13822 return (0); 13823 } 13824 if (__predict_false((th->th_ack != tp->snd_una))) { 13825 return (0); 13826 } 13827 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 13828 return (0); 13829 } 13830 if ((to->to_flags & TOF_TS) != 0 && 13831 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 13832 tp->ts_recent_age = tcp_ts_getticks(); 13833 tp->ts_recent = to->to_tsval; 13834 } 13835 rack = (struct tcp_rack *)tp->t_fb_ptr; 13836 /* 13837 * This is a pure, in-sequence data packet with nothing on the 13838 * reassembly queue and we have enough buffer space to take it. 13839 */ 13840 nsegs = max(1, m->m_pkthdr.lro_nsegs); 13841 13842 #ifdef NETFLIX_SB_LIMITS 13843 if (so->so_rcv.sb_shlim) { 13844 mcnt = m_memcnt(m); 13845 appended = 0; 13846 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 13847 CFO_NOSLEEP, NULL) == false) { 13848 counter_u64_add(tcp_sb_shlim_fails, 1); 13849 m_freem(m); 13850 return (1); 13851 } 13852 } 13853 #endif 13854 /* Clean receiver SACK report if present */ 13855 if (tp->rcv_numsacks) 13856 tcp_clean_sackreport(tp); 13857 KMOD_TCPSTAT_INC(tcps_preddat); 13858 tp->rcv_nxt += tlen; 13859 if (tlen && 13860 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 13861 (tp->t_fbyte_in == 0)) { 13862 tp->t_fbyte_in = ticks; 13863 if (tp->t_fbyte_in == 0) 13864 tp->t_fbyte_in = 1; 13865 if (tp->t_fbyte_out && tp->t_fbyte_in) 13866 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 13867 } 13868 /* 13869 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 13870 */ 13871 tp->snd_wl1 = th->th_seq; 13872 /* 13873 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 13874 */ 13875 tp->rcv_up = tp->rcv_nxt; 13876 KMOD_TCPSTAT_ADD(tcps_rcvpack, nsegs); 13877 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 13878 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 13879 13880 /* Add data to socket buffer. */ 13881 SOCKBUF_LOCK(&so->so_rcv); 13882 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 13883 m_freem(m); 13884 } else { 13885 /* 13886 * Set new socket buffer size. Give up when limit is 13887 * reached. 13888 */ 13889 if (newsize) 13890 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 13891 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 13892 m_adj(m, drop_hdrlen); /* delayed header drop */ 13893 #ifdef NETFLIX_SB_LIMITS 13894 appended = 13895 #endif 13896 sbappendstream_locked(&so->so_rcv, m, 0); 13897 ctf_calc_rwin(so, tp); 13898 } 13899 rack_log_wakeup(tp,rack, &so->so_rcv, tlen, 1); 13900 /* NB: sorwakeup_locked() does an implicit unlock. */ 13901 sorwakeup_locked(so); 13902 #ifdef NETFLIX_SB_LIMITS 13903 if (so->so_rcv.sb_shlim && mcnt != appended) 13904 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 13905 #endif 13906 rack_handle_delayed_ack(tp, rack, tlen, 0); 13907 if (tp->snd_una == tp->snd_max) 13908 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 13909 return (1); 13910 } 13911 13912 /* 13913 * This subfunction is used to try to highly optimize the 13914 * fast path. We again allow window updates that are 13915 * in sequence to remain in the fast-path. We also add 13916 * in the __predict's to attempt to help the compiler. 13917 * Note that if we return a 0, then we can *not* process 13918 * it and the caller should push the packet into the 13919 * slow-path. 13920 */ 13921 static int 13922 rack_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 13923 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 13924 uint32_t tiwin, int32_t nxt_pkt, uint32_t cts) 13925 { 13926 int32_t acked; 13927 int32_t nsegs; 13928 int32_t under_pacing = 0; 13929 struct tcp_rack *rack; 13930 13931 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 13932 /* Old ack, behind (or duplicate to) the last one rcv'd */ 13933 return (0); 13934 } 13935 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 13936 /* Above what we have sent? */ 13937 return (0); 13938 } 13939 if (__predict_false(tiwin == 0)) { 13940 /* zero window */ 13941 return (0); 13942 } 13943 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 13944 /* We need a SYN or a FIN, unlikely.. */ 13945 return (0); 13946 } 13947 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 13948 /* Timestamp is behind .. old ack with seq wrap? */ 13949 return (0); 13950 } 13951 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 13952 /* Still recovering */ 13953 return (0); 13954 } 13955 rack = (struct tcp_rack *)tp->t_fb_ptr; 13956 if (rack->r_ctl.rc_sacked) { 13957 /* We have sack holes on our scoreboard */ 13958 return (0); 13959 } 13960 /* Ok if we reach here, we can process a fast-ack */ 13961 if (rack->gp_ready && 13962 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 13963 under_pacing = 1; 13964 } 13965 nsegs = max(1, m->m_pkthdr.lro_nsegs); 13966 rack_log_ack(tp, to, th, 0, 0, NULL, NULL); 13967 /* Did the window get updated? */ 13968 if (tiwin != tp->snd_wnd) { 13969 tp->snd_wnd = tiwin; 13970 rack_validate_fo_sendwin_up(tp, rack); 13971 tp->snd_wl1 = th->th_seq; 13972 if (tp->snd_wnd > tp->max_sndwnd) 13973 tp->max_sndwnd = tp->snd_wnd; 13974 } 13975 /* Do we exit persists? */ 13976 if ((rack->rc_in_persist != 0) && 13977 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 13978 rack->r_ctl.rc_pace_min_segs))) { 13979 rack_exit_persist(tp, rack, cts); 13980 } 13981 /* Do we enter persists? */ 13982 if ((rack->rc_in_persist == 0) && 13983 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 13984 TCPS_HAVEESTABLISHED(tp->t_state) && 13985 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) && 13986 sbavail(&tptosocket(tp)->so_snd) && 13987 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) { 13988 /* 13989 * Here the rwnd is less than 13990 * the pacing size, we are established, 13991 * nothing is outstanding, and there is 13992 * data to send. Enter persists. 13993 */ 13994 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, th->th_ack); 13995 } 13996 /* 13997 * If last ACK falls within this segment's sequence numbers, record 13998 * the timestamp. NOTE that the test is modified according to the 13999 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 14000 */ 14001 if ((to->to_flags & TOF_TS) != 0 && 14002 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 14003 tp->ts_recent_age = tcp_ts_getticks(); 14004 tp->ts_recent = to->to_tsval; 14005 } 14006 /* 14007 * This is a pure ack for outstanding data. 14008 */ 14009 KMOD_TCPSTAT_INC(tcps_predack); 14010 14011 /* 14012 * "bad retransmit" recovery. 14013 */ 14014 if ((tp->t_flags & TF_PREVVALID) && 14015 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 14016 tp->t_flags &= ~TF_PREVVALID; 14017 if (tp->t_rxtshift == 1 && 14018 (int)(ticks - tp->t_badrxtwin) < 0) 14019 rack_cong_signal(tp, CC_RTO_ERR, th->th_ack, __LINE__); 14020 } 14021 /* 14022 * Recalculate the transmit timer / rtt. 14023 * 14024 * Some boxes send broken timestamp replies during the SYN+ACK 14025 * phase, ignore timestamps of 0 or we could calculate a huge RTT 14026 * and blow up the retransmit timer. 14027 */ 14028 acked = BYTES_THIS_ACK(tp, th); 14029 14030 #ifdef TCP_HHOOK 14031 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 14032 hhook_run_tcp_est_in(tp, th, to); 14033 #endif 14034 KMOD_TCPSTAT_ADD(tcps_rcvackpack, nsegs); 14035 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 14036 if (acked) { 14037 struct mbuf *mfree; 14038 14039 rack_ack_received(tp, rack, th->th_ack, nsegs, CC_ACK, 0); 14040 SOCKBUF_LOCK(&so->so_snd); 14041 mfree = sbcut_locked(&so->so_snd, acked); 14042 tp->snd_una = th->th_ack; 14043 /* Note we want to hold the sb lock through the sendmap adjust */ 14044 rack_adjust_sendmap_head(rack, &so->so_snd); 14045 /* Wake up the socket if we have room to write more */ 14046 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 14047 sowwakeup_locked(so); 14048 m_freem(mfree); 14049 tp->t_rxtshift = 0; 14050 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 14051 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 14052 rack->rc_tlp_in_progress = 0; 14053 rack->r_ctl.rc_tlp_cnt_out = 0; 14054 /* 14055 * If it is the RXT timer we want to 14056 * stop it, so we can restart a TLP. 14057 */ 14058 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 14059 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 14060 14061 #ifdef TCP_REQUEST_TRK 14062 rack_req_check_for_comp(rack, th->th_ack); 14063 #endif 14064 } 14065 /* 14066 * Let the congestion control algorithm update congestion control 14067 * related information. This typically means increasing the 14068 * congestion window. 14069 */ 14070 if (tp->snd_wnd < ctf_outstanding(tp)) { 14071 /* The peer collapsed the window */ 14072 rack_collapsed_window(rack, ctf_outstanding(tp), th->th_ack, __LINE__); 14073 } else if (rack->rc_has_collapsed) 14074 rack_un_collapse_window(rack, __LINE__); 14075 if ((rack->r_collapse_point_valid) && 14076 (SEQ_GT(tp->snd_una, rack->r_ctl.high_collapse_point))) 14077 rack->r_collapse_point_valid = 0; 14078 /* 14079 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 14080 */ 14081 tp->snd_wl2 = th->th_ack; 14082 tp->t_dupacks = 0; 14083 m_freem(m); 14084 /* ND6_HINT(tp); *//* Some progress has been made. */ 14085 14086 /* 14087 * If all outstanding data are acked, stop retransmit timer, 14088 * otherwise restart timer using current (possibly backed-off) 14089 * value. If process is waiting for space, wakeup/selwakeup/signal. 14090 * If data are ready to send, let tcp_output decide between more 14091 * output or persist. 14092 */ 14093 if (under_pacing && 14094 (rack->use_fixed_rate == 0) && 14095 (rack->in_probe_rtt == 0) && 14096 rack->rc_gp_dyn_mul && 14097 rack->rc_always_pace) { 14098 /* Check if we are dragging bottom */ 14099 rack_check_bottom_drag(tp, rack, so); 14100 } 14101 if (tp->snd_una == tp->snd_max) { 14102 tp->t_flags &= ~TF_PREVVALID; 14103 rack->r_ctl.retran_during_recovery = 0; 14104 rack->rc_suspicious = 0; 14105 rack->r_ctl.dsack_byte_cnt = 0; 14106 rack->r_ctl.idle_snd_una = tp->snd_una; 14107 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 14108 if (rack->r_ctl.rc_went_idle_time == 0) 14109 rack->r_ctl.rc_went_idle_time = 1; 14110 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 14111 if (sbavail(&tptosocket(tp)->so_snd) == 0) 14112 tp->t_acktime = 0; 14113 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 14114 } 14115 if (acked && rack->r_fast_output) 14116 rack_gain_for_fastoutput(rack, tp, so, (uint32_t)acked); 14117 if (sbavail(&so->so_snd)) { 14118 rack->r_wanted_output = 1; 14119 } 14120 return (1); 14121 } 14122 14123 /* 14124 * Return value of 1, the TCB is unlocked and most 14125 * likely gone, return value of 0, the TCP is still 14126 * locked. 14127 */ 14128 static int 14129 rack_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 14130 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14131 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14132 { 14133 int32_t ret_val = 0; 14134 int32_t orig_tlen = tlen; 14135 int32_t todrop; 14136 int32_t ourfinisacked = 0; 14137 struct tcp_rack *rack; 14138 14139 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14140 14141 ctf_calc_rwin(so, tp); 14142 /* 14143 * If the state is SYN_SENT: if seg contains an ACK, but not for our 14144 * SYN, drop the input. if seg contains a RST, then drop the 14145 * connection. if seg does not contain SYN, then drop it. Otherwise 14146 * this is an acceptable SYN segment initialize tp->rcv_nxt and 14147 * tp->irs if seg contains ack then advance tp->snd_una if seg 14148 * contains an ECE and ECN support is enabled, the stream is ECN 14149 * capable. if SYN has been acked change to ESTABLISHED else 14150 * SYN_RCVD state arrange for segment to be acked (eventually) 14151 * continue processing rest of data/controls. 14152 */ 14153 if ((thflags & TH_ACK) && 14154 (SEQ_LEQ(th->th_ack, tp->iss) || 14155 SEQ_GT(th->th_ack, tp->snd_max))) { 14156 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 14157 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14158 return (1); 14159 } 14160 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 14161 TCP_PROBE5(connect__refused, NULL, tp, 14162 mtod(m, const char *), tp, th); 14163 tp = tcp_drop(tp, ECONNREFUSED); 14164 ctf_do_drop(m, tp); 14165 return (1); 14166 } 14167 if (thflags & TH_RST) { 14168 ctf_do_drop(m, tp); 14169 return (1); 14170 } 14171 if (!(thflags & TH_SYN)) { 14172 ctf_do_drop(m, tp); 14173 return (1); 14174 } 14175 tp->irs = th->th_seq; 14176 tcp_rcvseqinit(tp); 14177 rack = (struct tcp_rack *)tp->t_fb_ptr; 14178 if (thflags & TH_ACK) { 14179 int tfo_partial = 0; 14180 14181 KMOD_TCPSTAT_INC(tcps_connects); 14182 soisconnected(so); 14183 #ifdef MAC 14184 mac_socketpeer_set_from_mbuf(m, so); 14185 #endif 14186 /* Do window scaling on this connection? */ 14187 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 14188 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 14189 tp->rcv_scale = tp->request_r_scale; 14190 } 14191 tp->rcv_adv += min(tp->rcv_wnd, 14192 TCP_MAXWIN << tp->rcv_scale); 14193 /* 14194 * If not all the data that was sent in the TFO SYN 14195 * has been acked, resend the remainder right away. 14196 */ 14197 if ((tp->t_flags & TF_FASTOPEN) && 14198 (tp->snd_una != tp->snd_max)) { 14199 /* Was it a partial ack? */ 14200 if (SEQ_LT(th->th_ack, tp->snd_max)) 14201 tfo_partial = 1; 14202 } 14203 /* 14204 * If there's data, delay ACK; if there's also a FIN ACKNOW 14205 * will be turned on later. 14206 */ 14207 if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial) { 14208 rack_timer_cancel(tp, rack, 14209 rack->r_ctl.rc_rcvtime, __LINE__); 14210 tp->t_flags |= TF_DELACK; 14211 } else { 14212 rack->r_wanted_output = 1; 14213 tp->t_flags |= TF_ACKNOW; 14214 } 14215 14216 tcp_ecn_input_syn_sent(tp, thflags, iptos); 14217 14218 if (SEQ_GT(th->th_ack, tp->snd_una)) { 14219 /* 14220 * We advance snd_una for the 14221 * fast open case. If th_ack is 14222 * acknowledging data beyond 14223 * snd_una we can't just call 14224 * ack-processing since the 14225 * data stream in our send-map 14226 * will start at snd_una + 1 (one 14227 * beyond the SYN). If its just 14228 * equal we don't need to do that 14229 * and there is no send_map. 14230 */ 14231 tp->snd_una++; 14232 if (tfo_partial && (SEQ_GT(tp->snd_max, tp->snd_una))) { 14233 /* 14234 * We sent a SYN with data, and thus have a 14235 * sendmap entry with a SYN set. Lets find it 14236 * and take off the send bit and the byte and 14237 * set it up to be what we send (send it next). 14238 */ 14239 struct rack_sendmap *rsm; 14240 14241 rsm = tqhash_min(rack->r_ctl.tqh); 14242 if (rsm) { 14243 if (rsm->r_flags & RACK_HAS_SYN) { 14244 rsm->r_flags &= ~RACK_HAS_SYN; 14245 rsm->r_start++; 14246 } 14247 rack->r_ctl.rc_resend = rsm; 14248 } 14249 } 14250 } 14251 /* 14252 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 14253 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 14254 */ 14255 tp->t_starttime = ticks; 14256 if (tp->t_flags & TF_NEEDFIN) { 14257 tcp_state_change(tp, TCPS_FIN_WAIT_1); 14258 tp->t_flags &= ~TF_NEEDFIN; 14259 thflags &= ~TH_SYN; 14260 } else { 14261 tcp_state_change(tp, TCPS_ESTABLISHED); 14262 TCP_PROBE5(connect__established, NULL, tp, 14263 mtod(m, const char *), tp, th); 14264 rack_cc_conn_init(tp); 14265 } 14266 } else { 14267 /* 14268 * Received initial SYN in SYN-SENT[*] state => simultaneous 14269 * open. If segment contains CC option and there is a 14270 * cached CC, apply TAO test. If it succeeds, connection is * 14271 * half-synchronized. Otherwise, do 3-way handshake: 14272 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 14273 * there was no CC option, clear cached CC value. 14274 */ 14275 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 14276 tcp_state_change(tp, TCPS_SYN_RECEIVED); 14277 } 14278 /* 14279 * Advance th->th_seq to correspond to first data byte. If data, 14280 * trim to stay within window, dropping FIN if necessary. 14281 */ 14282 th->th_seq++; 14283 if (tlen > tp->rcv_wnd) { 14284 todrop = tlen - tp->rcv_wnd; 14285 m_adj(m, -todrop); 14286 tlen = tp->rcv_wnd; 14287 thflags &= ~TH_FIN; 14288 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 14289 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 14290 } 14291 tp->snd_wl1 = th->th_seq - 1; 14292 tp->rcv_up = th->th_seq; 14293 /* 14294 * Client side of transaction: already sent SYN and data. If the 14295 * remote host used T/TCP to validate the SYN, our data will be 14296 * ACK'd; if so, enter normal data segment processing in the middle 14297 * of step 5, ack processing. Otherwise, goto step 6. 14298 */ 14299 if (thflags & TH_ACK) { 14300 /* For syn-sent we need to possibly update the rtt */ 14301 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 14302 uint32_t t, mcts; 14303 14304 mcts = tcp_ts_getticks(); 14305 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC; 14306 if (!tp->t_rttlow || tp->t_rttlow > t) 14307 tp->t_rttlow = t; 14308 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 4); 14309 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2); 14310 tcp_rack_xmit_timer_commit(rack, tp); 14311 } 14312 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) 14313 return (ret_val); 14314 /* We may have changed to FIN_WAIT_1 above */ 14315 if (tp->t_state == TCPS_FIN_WAIT_1) { 14316 /* 14317 * In FIN_WAIT_1 STATE in addition to the processing 14318 * for the ESTABLISHED state if our FIN is now 14319 * acknowledged then enter FIN_WAIT_2. 14320 */ 14321 if (ourfinisacked) { 14322 /* 14323 * If we can't receive any more data, then 14324 * closing user can proceed. Starting the 14325 * timer is contrary to the specification, 14326 * but if we don't get a FIN we'll hang 14327 * forever. 14328 * 14329 * XXXjl: we should release the tp also, and 14330 * use a compressed state. 14331 */ 14332 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 14333 soisdisconnected(so); 14334 tcp_timer_activate(tp, TT_2MSL, 14335 (tcp_fast_finwait2_recycle ? 14336 tcp_finwait2_timeout : 14337 TP_MAXIDLE(tp))); 14338 } 14339 tcp_state_change(tp, TCPS_FIN_WAIT_2); 14340 } 14341 } 14342 } 14343 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14344 tiwin, thflags, nxt_pkt)); 14345 } 14346 14347 /* 14348 * Return value of 1, the TCB is unlocked and most 14349 * likely gone, return value of 0, the TCP is still 14350 * locked. 14351 */ 14352 static int 14353 rack_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 14354 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14355 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14356 { 14357 struct tcp_rack *rack; 14358 int32_t orig_tlen = tlen; 14359 int32_t ret_val = 0; 14360 int32_t ourfinisacked = 0; 14361 14362 rack = (struct tcp_rack *)tp->t_fb_ptr; 14363 ctf_calc_rwin(so, tp); 14364 if ((thflags & TH_RST) || 14365 (tp->t_fin_is_rst && (thflags & TH_FIN))) 14366 return (__ctf_process_rst(m, th, so, tp, 14367 &rack->r_ctl.challenge_ack_ts, 14368 &rack->r_ctl.challenge_ack_cnt)); 14369 if ((thflags & TH_ACK) && 14370 (SEQ_LEQ(th->th_ack, tp->snd_una) || 14371 SEQ_GT(th->th_ack, tp->snd_max))) { 14372 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 14373 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14374 return (1); 14375 } 14376 if (tp->t_flags & TF_FASTOPEN) { 14377 /* 14378 * When a TFO connection is in SYN_RECEIVED, the 14379 * only valid packets are the initial SYN, a 14380 * retransmit/copy of the initial SYN (possibly with 14381 * a subset of the original data), a valid ACK, a 14382 * FIN, or a RST. 14383 */ 14384 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 14385 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 14386 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14387 return (1); 14388 } else if (thflags & TH_SYN) { 14389 /* non-initial SYN is ignored */ 14390 if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 14391 (rack->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 14392 (rack->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 14393 ctf_do_drop(m, NULL); 14394 return (0); 14395 } 14396 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 14397 ctf_do_drop(m, NULL); 14398 return (0); 14399 } 14400 } 14401 14402 /* 14403 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 14404 * it's less than ts_recent, drop it. 14405 */ 14406 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 14407 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 14408 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 14409 return (ret_val); 14410 } 14411 /* 14412 * In the SYN-RECEIVED state, validate that the packet belongs to 14413 * this connection before trimming the data to fit the receive 14414 * window. Check the sequence number versus IRS since we know the 14415 * sequence numbers haven't wrapped. This is a partial fix for the 14416 * "LAND" DoS attack. 14417 */ 14418 if (SEQ_LT(th->th_seq, tp->irs)) { 14419 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 14420 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14421 return (1); 14422 } 14423 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 14424 &rack->r_ctl.challenge_ack_ts, 14425 &rack->r_ctl.challenge_ack_cnt)) { 14426 return (ret_val); 14427 } 14428 /* 14429 * If last ACK falls within this segment's sequence numbers, record 14430 * its timestamp. NOTE: 1) That the test incorporates suggestions 14431 * from the latest proposal of the tcplw@cray.com list (Braden 14432 * 1993/04/26). 2) That updating only on newer timestamps interferes 14433 * with our earlier PAWS tests, so this check should be solely 14434 * predicated on the sequence space of this segment. 3) That we 14435 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 14436 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 14437 * SEG.Len, This modified check allows us to overcome RFC1323's 14438 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 14439 * p.869. In such cases, we can still calculate the RTT correctly 14440 * when RCV.NXT == Last.ACK.Sent. 14441 */ 14442 if ((to->to_flags & TOF_TS) != 0 && 14443 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 14444 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 14445 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 14446 tp->ts_recent_age = tcp_ts_getticks(); 14447 tp->ts_recent = to->to_tsval; 14448 } 14449 tp->snd_wnd = tiwin; 14450 rack_validate_fo_sendwin_up(tp, rack); 14451 /* 14452 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 14453 * is on (half-synchronized state), then queue data for later 14454 * processing; else drop segment and return. 14455 */ 14456 if ((thflags & TH_ACK) == 0) { 14457 if (tp->t_flags & TF_FASTOPEN) { 14458 rack_cc_conn_init(tp); 14459 } 14460 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14461 tiwin, thflags, nxt_pkt)); 14462 } 14463 KMOD_TCPSTAT_INC(tcps_connects); 14464 if (tp->t_flags & TF_SONOTCONN) { 14465 tp->t_flags &= ~TF_SONOTCONN; 14466 soisconnected(so); 14467 } 14468 /* Do window scaling? */ 14469 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 14470 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 14471 tp->rcv_scale = tp->request_r_scale; 14472 } 14473 /* 14474 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 14475 * FIN-WAIT-1 14476 */ 14477 tp->t_starttime = ticks; 14478 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 14479 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 14480 tp->t_tfo_pending = NULL; 14481 } 14482 if (tp->t_flags & TF_NEEDFIN) { 14483 tcp_state_change(tp, TCPS_FIN_WAIT_1); 14484 tp->t_flags &= ~TF_NEEDFIN; 14485 } else { 14486 tcp_state_change(tp, TCPS_ESTABLISHED); 14487 TCP_PROBE5(accept__established, NULL, tp, 14488 mtod(m, const char *), tp, th); 14489 /* 14490 * TFO connections call cc_conn_init() during SYN 14491 * processing. Calling it again here for such connections 14492 * is not harmless as it would undo the snd_cwnd reduction 14493 * that occurs when a TFO SYN|ACK is retransmitted. 14494 */ 14495 if (!(tp->t_flags & TF_FASTOPEN)) 14496 rack_cc_conn_init(tp); 14497 } 14498 /* 14499 * Account for the ACK of our SYN prior to 14500 * regular ACK processing below, except for 14501 * simultaneous SYN, which is handled later. 14502 */ 14503 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 14504 tp->snd_una++; 14505 /* 14506 * If segment contains data or ACK, will call tcp_reass() later; if 14507 * not, do so now to pass queued data to user. 14508 */ 14509 if (tlen == 0 && (thflags & TH_FIN) == 0) { 14510 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 14511 (struct mbuf *)0); 14512 if (tp->t_flags & TF_WAKESOR) { 14513 tp->t_flags &= ~TF_WAKESOR; 14514 /* NB: sorwakeup_locked() does an implicit unlock. */ 14515 sorwakeup_locked(so); 14516 } 14517 } 14518 tp->snd_wl1 = th->th_seq - 1; 14519 /* For syn-recv we need to possibly update the rtt */ 14520 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 14521 uint32_t t, mcts; 14522 14523 mcts = tcp_ts_getticks(); 14524 t = (mcts - to->to_tsecr) * HPTS_USEC_IN_MSEC; 14525 if (!tp->t_rttlow || tp->t_rttlow > t) 14526 tp->t_rttlow = t; 14527 rack_log_rtt_sample_calc(rack, t, (to->to_tsecr * 1000), (mcts * 1000), 5); 14528 tcp_rack_xmit_timer(rack, t + 1, 1, t, 0, NULL, 2); 14529 tcp_rack_xmit_timer_commit(rack, tp); 14530 } 14531 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) { 14532 return (ret_val); 14533 } 14534 if (tp->t_state == TCPS_FIN_WAIT_1) { 14535 /* We could have went to FIN_WAIT_1 (or EST) above */ 14536 /* 14537 * In FIN_WAIT_1 STATE in addition to the processing for the 14538 * ESTABLISHED state if our FIN is now acknowledged then 14539 * enter FIN_WAIT_2. 14540 */ 14541 if (ourfinisacked) { 14542 /* 14543 * If we can't receive any more data, then closing 14544 * user can proceed. Starting the timer is contrary 14545 * to the specification, but if we don't get a FIN 14546 * we'll hang forever. 14547 * 14548 * XXXjl: we should release the tp also, and use a 14549 * compressed state. 14550 */ 14551 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 14552 soisdisconnected(so); 14553 tcp_timer_activate(tp, TT_2MSL, 14554 (tcp_fast_finwait2_recycle ? 14555 tcp_finwait2_timeout : 14556 TP_MAXIDLE(tp))); 14557 } 14558 tcp_state_change(tp, TCPS_FIN_WAIT_2); 14559 } 14560 } 14561 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14562 tiwin, thflags, nxt_pkt)); 14563 } 14564 14565 /* 14566 * Return value of 1, the TCB is unlocked and most 14567 * likely gone, return value of 0, the TCP is still 14568 * locked. 14569 */ 14570 static int 14571 rack_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 14572 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14573 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14574 { 14575 int32_t ret_val = 0; 14576 int32_t orig_tlen = tlen; 14577 struct tcp_rack *rack; 14578 14579 /* 14580 * Header prediction: check for the two common cases of a 14581 * uni-directional data xfer. If the packet has no control flags, 14582 * is in-sequence, the window didn't change and we're not 14583 * retransmitting, it's a candidate. If the length is zero and the 14584 * ack moved forward, we're the sender side of the xfer. Just free 14585 * the data acked & wake any higher level process that was blocked 14586 * waiting for space. If the length is non-zero and the ack didn't 14587 * move, we're the receiver side. If we're getting packets in-order 14588 * (the reassembly queue is empty), add the data toc The socket 14589 * buffer and note that we need a delayed ack. Make sure that the 14590 * hidden state-flags are also off. Since we check for 14591 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 14592 */ 14593 rack = (struct tcp_rack *)tp->t_fb_ptr; 14594 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 14595 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) == TH_ACK) && 14596 __predict_true(SEGQ_EMPTY(tp)) && 14597 __predict_true(th->th_seq == tp->rcv_nxt)) { 14598 if (tlen == 0) { 14599 if (rack_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 14600 tiwin, nxt_pkt, rack->r_ctl.rc_rcvtime)) { 14601 return (0); 14602 } 14603 } else { 14604 if (rack_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 14605 tiwin, nxt_pkt, iptos)) { 14606 return (0); 14607 } 14608 } 14609 } 14610 ctf_calc_rwin(so, tp); 14611 14612 if ((thflags & TH_RST) || 14613 (tp->t_fin_is_rst && (thflags & TH_FIN))) 14614 return (__ctf_process_rst(m, th, so, tp, 14615 &rack->r_ctl.challenge_ack_ts, 14616 &rack->r_ctl.challenge_ack_cnt)); 14617 14618 /* 14619 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 14620 * synchronized state. 14621 */ 14622 if (thflags & TH_SYN) { 14623 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 14624 return (ret_val); 14625 } 14626 /* 14627 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 14628 * it's less than ts_recent, drop it. 14629 */ 14630 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 14631 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 14632 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 14633 return (ret_val); 14634 } 14635 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 14636 &rack->r_ctl.challenge_ack_ts, 14637 &rack->r_ctl.challenge_ack_cnt)) { 14638 return (ret_val); 14639 } 14640 /* 14641 * If last ACK falls within this segment's sequence numbers, record 14642 * its timestamp. NOTE: 1) That the test incorporates suggestions 14643 * from the latest proposal of the tcplw@cray.com list (Braden 14644 * 1993/04/26). 2) That updating only on newer timestamps interferes 14645 * with our earlier PAWS tests, so this check should be solely 14646 * predicated on the sequence space of this segment. 3) That we 14647 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 14648 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 14649 * SEG.Len, This modified check allows us to overcome RFC1323's 14650 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 14651 * p.869. In such cases, we can still calculate the RTT correctly 14652 * when RCV.NXT == Last.ACK.Sent. 14653 */ 14654 if ((to->to_flags & TOF_TS) != 0 && 14655 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 14656 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 14657 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 14658 tp->ts_recent_age = tcp_ts_getticks(); 14659 tp->ts_recent = to->to_tsval; 14660 } 14661 /* 14662 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 14663 * is on (half-synchronized state), then queue data for later 14664 * processing; else drop segment and return. 14665 */ 14666 if ((thflags & TH_ACK) == 0) { 14667 if (tp->t_flags & TF_NEEDSYN) { 14668 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14669 tiwin, thflags, nxt_pkt)); 14670 14671 } else if (tp->t_flags & TF_ACKNOW) { 14672 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 14673 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 14674 return (ret_val); 14675 } else { 14676 ctf_do_drop(m, NULL); 14677 return (0); 14678 } 14679 } 14680 /* 14681 * Ack processing. 14682 */ 14683 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) { 14684 return (ret_val); 14685 } 14686 if (sbavail(&so->so_snd)) { 14687 if (ctf_progress_timeout_check(tp, true)) { 14688 rack_log_progress_event(rack, tp, tick, PROGRESS_DROP, __LINE__); 14689 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14690 return (1); 14691 } 14692 } 14693 /* State changes only happen in rack_process_data() */ 14694 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14695 tiwin, thflags, nxt_pkt)); 14696 } 14697 14698 /* 14699 * Return value of 1, the TCB is unlocked and most 14700 * likely gone, return value of 0, the TCP is still 14701 * locked. 14702 */ 14703 static int 14704 rack_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 14705 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14706 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14707 { 14708 int32_t ret_val = 0; 14709 int32_t orig_tlen = tlen; 14710 struct tcp_rack *rack; 14711 14712 rack = (struct tcp_rack *)tp->t_fb_ptr; 14713 ctf_calc_rwin(so, tp); 14714 if ((thflags & TH_RST) || 14715 (tp->t_fin_is_rst && (thflags & TH_FIN))) 14716 return (__ctf_process_rst(m, th, so, tp, 14717 &rack->r_ctl.challenge_ack_ts, 14718 &rack->r_ctl.challenge_ack_cnt)); 14719 /* 14720 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 14721 * synchronized state. 14722 */ 14723 if (thflags & TH_SYN) { 14724 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 14725 return (ret_val); 14726 } 14727 /* 14728 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 14729 * it's less than ts_recent, drop it. 14730 */ 14731 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 14732 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 14733 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 14734 return (ret_val); 14735 } 14736 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 14737 &rack->r_ctl.challenge_ack_ts, 14738 &rack->r_ctl.challenge_ack_cnt)) { 14739 return (ret_val); 14740 } 14741 /* 14742 * If last ACK falls within this segment's sequence numbers, record 14743 * its timestamp. NOTE: 1) That the test incorporates suggestions 14744 * from the latest proposal of the tcplw@cray.com list (Braden 14745 * 1993/04/26). 2) That updating only on newer timestamps interferes 14746 * with our earlier PAWS tests, so this check should be solely 14747 * predicated on the sequence space of this segment. 3) That we 14748 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 14749 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 14750 * SEG.Len, This modified check allows us to overcome RFC1323's 14751 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 14752 * p.869. In such cases, we can still calculate the RTT correctly 14753 * when RCV.NXT == Last.ACK.Sent. 14754 */ 14755 if ((to->to_flags & TOF_TS) != 0 && 14756 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 14757 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 14758 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 14759 tp->ts_recent_age = tcp_ts_getticks(); 14760 tp->ts_recent = to->to_tsval; 14761 } 14762 /* 14763 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 14764 * is on (half-synchronized state), then queue data for later 14765 * processing; else drop segment and return. 14766 */ 14767 if ((thflags & TH_ACK) == 0) { 14768 if (tp->t_flags & TF_NEEDSYN) { 14769 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14770 tiwin, thflags, nxt_pkt)); 14771 14772 } else if (tp->t_flags & TF_ACKNOW) { 14773 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 14774 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 14775 return (ret_val); 14776 } else { 14777 ctf_do_drop(m, NULL); 14778 return (0); 14779 } 14780 } 14781 /* 14782 * Ack processing. 14783 */ 14784 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val, orig_tlen)) { 14785 return (ret_val); 14786 } 14787 if (sbavail(&so->so_snd)) { 14788 if (ctf_progress_timeout_check(tp, true)) { 14789 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 14790 tp, tick, PROGRESS_DROP, __LINE__); 14791 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14792 return (1); 14793 } 14794 } 14795 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14796 tiwin, thflags, nxt_pkt)); 14797 } 14798 14799 static int 14800 rack_check_data_after_close(struct mbuf *m, 14801 struct tcpcb *tp, int32_t *tlen, struct tcphdr *th, struct socket *so) 14802 { 14803 struct tcp_rack *rack; 14804 14805 rack = (struct tcp_rack *)tp->t_fb_ptr; 14806 if (rack->rc_allow_data_af_clo == 0) { 14807 close_now: 14808 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 14809 /* tcp_close will kill the inp pre-log the Reset */ 14810 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 14811 tp = tcp_close(tp); 14812 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 14813 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 14814 return (1); 14815 } 14816 if (sbavail(&so->so_snd) == 0) 14817 goto close_now; 14818 /* Ok we allow data that is ignored and a followup reset */ 14819 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 14820 tp->rcv_nxt = th->th_seq + *tlen; 14821 tp->t_flags2 |= TF2_DROP_AF_DATA; 14822 rack->r_wanted_output = 1; 14823 *tlen = 0; 14824 return (0); 14825 } 14826 14827 /* 14828 * Return value of 1, the TCB is unlocked and most 14829 * likely gone, return value of 0, the TCP is still 14830 * locked. 14831 */ 14832 static int 14833 rack_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 14834 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14835 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14836 { 14837 int32_t ret_val = 0; 14838 int32_t orig_tlen = tlen; 14839 int32_t ourfinisacked = 0; 14840 struct tcp_rack *rack; 14841 14842 rack = (struct tcp_rack *)tp->t_fb_ptr; 14843 ctf_calc_rwin(so, tp); 14844 14845 if ((thflags & TH_RST) || 14846 (tp->t_fin_is_rst && (thflags & TH_FIN))) 14847 return (__ctf_process_rst(m, th, so, tp, 14848 &rack->r_ctl.challenge_ack_ts, 14849 &rack->r_ctl.challenge_ack_cnt)); 14850 /* 14851 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 14852 * synchronized state. 14853 */ 14854 if (thflags & TH_SYN) { 14855 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 14856 return (ret_val); 14857 } 14858 /* 14859 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 14860 * it's less than ts_recent, drop it. 14861 */ 14862 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 14863 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 14864 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 14865 return (ret_val); 14866 } 14867 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 14868 &rack->r_ctl.challenge_ack_ts, 14869 &rack->r_ctl.challenge_ack_cnt)) { 14870 return (ret_val); 14871 } 14872 /* 14873 * If new data are received on a connection after the user processes 14874 * are gone, then RST the other end. 14875 */ 14876 if ((tp->t_flags & TF_CLOSED) && tlen && 14877 rack_check_data_after_close(m, tp, &tlen, th, so)) 14878 return (1); 14879 /* 14880 * If last ACK falls within this segment's sequence numbers, record 14881 * its timestamp. NOTE: 1) That the test incorporates suggestions 14882 * from the latest proposal of the tcplw@cray.com list (Braden 14883 * 1993/04/26). 2) That updating only on newer timestamps interferes 14884 * with our earlier PAWS tests, so this check should be solely 14885 * predicated on the sequence space of this segment. 3) That we 14886 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 14887 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 14888 * SEG.Len, This modified check allows us to overcome RFC1323's 14889 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 14890 * p.869. In such cases, we can still calculate the RTT correctly 14891 * when RCV.NXT == Last.ACK.Sent. 14892 */ 14893 if ((to->to_flags & TOF_TS) != 0 && 14894 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 14895 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 14896 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 14897 tp->ts_recent_age = tcp_ts_getticks(); 14898 tp->ts_recent = to->to_tsval; 14899 } 14900 /* 14901 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 14902 * is on (half-synchronized state), then queue data for later 14903 * processing; else drop segment and return. 14904 */ 14905 if ((thflags & TH_ACK) == 0) { 14906 if (tp->t_flags & TF_NEEDSYN) { 14907 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14908 tiwin, thflags, nxt_pkt)); 14909 } else if (tp->t_flags & TF_ACKNOW) { 14910 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 14911 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 14912 return (ret_val); 14913 } else { 14914 ctf_do_drop(m, NULL); 14915 return (0); 14916 } 14917 } 14918 /* 14919 * Ack processing. 14920 */ 14921 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) { 14922 return (ret_val); 14923 } 14924 if (ourfinisacked) { 14925 /* 14926 * If we can't receive any more data, then closing user can 14927 * proceed. Starting the timer is contrary to the 14928 * specification, but if we don't get a FIN we'll hang 14929 * forever. 14930 * 14931 * XXXjl: we should release the tp also, and use a 14932 * compressed state. 14933 */ 14934 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 14935 soisdisconnected(so); 14936 tcp_timer_activate(tp, TT_2MSL, 14937 (tcp_fast_finwait2_recycle ? 14938 tcp_finwait2_timeout : 14939 TP_MAXIDLE(tp))); 14940 } 14941 tcp_state_change(tp, TCPS_FIN_WAIT_2); 14942 } 14943 if (sbavail(&so->so_snd)) { 14944 if (ctf_progress_timeout_check(tp, true)) { 14945 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 14946 tp, tick, PROGRESS_DROP, __LINE__); 14947 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 14948 return (1); 14949 } 14950 } 14951 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 14952 tiwin, thflags, nxt_pkt)); 14953 } 14954 14955 /* 14956 * Return value of 1, the TCB is unlocked and most 14957 * likely gone, return value of 0, the TCP is still 14958 * locked. 14959 */ 14960 static int 14961 rack_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 14962 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 14963 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 14964 { 14965 int32_t ret_val = 0; 14966 int32_t orig_tlen = tlen; 14967 int32_t ourfinisacked = 0; 14968 struct tcp_rack *rack; 14969 14970 rack = (struct tcp_rack *)tp->t_fb_ptr; 14971 ctf_calc_rwin(so, tp); 14972 14973 if ((thflags & TH_RST) || 14974 (tp->t_fin_is_rst && (thflags & TH_FIN))) 14975 return (__ctf_process_rst(m, th, so, tp, 14976 &rack->r_ctl.challenge_ack_ts, 14977 &rack->r_ctl.challenge_ack_cnt)); 14978 /* 14979 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 14980 * synchronized state. 14981 */ 14982 if (thflags & TH_SYN) { 14983 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 14984 return (ret_val); 14985 } 14986 /* 14987 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 14988 * it's less than ts_recent, drop it. 14989 */ 14990 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 14991 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 14992 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 14993 return (ret_val); 14994 } 14995 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 14996 &rack->r_ctl.challenge_ack_ts, 14997 &rack->r_ctl.challenge_ack_cnt)) { 14998 return (ret_val); 14999 } 15000 /* 15001 * If last ACK falls within this segment's sequence numbers, record 15002 * its timestamp. NOTE: 1) That the test incorporates suggestions 15003 * from the latest proposal of the tcplw@cray.com list (Braden 15004 * 1993/04/26). 2) That updating only on newer timestamps interferes 15005 * with our earlier PAWS tests, so this check should be solely 15006 * predicated on the sequence space of this segment. 3) That we 15007 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 15008 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 15009 * SEG.Len, This modified check allows us to overcome RFC1323's 15010 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 15011 * p.869. In such cases, we can still calculate the RTT correctly 15012 * when RCV.NXT == Last.ACK.Sent. 15013 */ 15014 if ((to->to_flags & TOF_TS) != 0 && 15015 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 15016 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 15017 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 15018 tp->ts_recent_age = tcp_ts_getticks(); 15019 tp->ts_recent = to->to_tsval; 15020 } 15021 /* 15022 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 15023 * is on (half-synchronized state), then queue data for later 15024 * processing; else drop segment and return. 15025 */ 15026 if ((thflags & TH_ACK) == 0) { 15027 if (tp->t_flags & TF_NEEDSYN) { 15028 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15029 tiwin, thflags, nxt_pkt)); 15030 } else if (tp->t_flags & TF_ACKNOW) { 15031 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 15032 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 15033 return (ret_val); 15034 } else { 15035 ctf_do_drop(m, NULL); 15036 return (0); 15037 } 15038 } 15039 /* 15040 * Ack processing. 15041 */ 15042 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) { 15043 return (ret_val); 15044 } 15045 if (ourfinisacked) { 15046 tcp_twstart(tp); 15047 m_freem(m); 15048 return (1); 15049 } 15050 if (sbavail(&so->so_snd)) { 15051 if (ctf_progress_timeout_check(tp, true)) { 15052 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 15053 tp, tick, PROGRESS_DROP, __LINE__); 15054 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 15055 return (1); 15056 } 15057 } 15058 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15059 tiwin, thflags, nxt_pkt)); 15060 } 15061 15062 /* 15063 * Return value of 1, the TCB is unlocked and most 15064 * likely gone, return value of 0, the TCP is still 15065 * locked. 15066 */ 15067 static int 15068 rack_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 15069 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 15070 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 15071 { 15072 int32_t ret_val = 0; 15073 int32_t orig_tlen; 15074 int32_t ourfinisacked = 0; 15075 struct tcp_rack *rack; 15076 15077 rack = (struct tcp_rack *)tp->t_fb_ptr; 15078 ctf_calc_rwin(so, tp); 15079 15080 if ((thflags & TH_RST) || 15081 (tp->t_fin_is_rst && (thflags & TH_FIN))) 15082 return (__ctf_process_rst(m, th, so, tp, 15083 &rack->r_ctl.challenge_ack_ts, 15084 &rack->r_ctl.challenge_ack_cnt)); 15085 /* 15086 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 15087 * synchronized state. 15088 */ 15089 if (thflags & TH_SYN) { 15090 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 15091 return (ret_val); 15092 } 15093 /* 15094 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 15095 * it's less than ts_recent, drop it. 15096 */ 15097 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 15098 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 15099 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 15100 return (ret_val); 15101 } 15102 orig_tlen = tlen; 15103 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 15104 &rack->r_ctl.challenge_ack_ts, 15105 &rack->r_ctl.challenge_ack_cnt)) { 15106 return (ret_val); 15107 } 15108 /* 15109 * If last ACK falls within this segment's sequence numbers, record 15110 * its timestamp. NOTE: 1) That the test incorporates suggestions 15111 * from the latest proposal of the tcplw@cray.com list (Braden 15112 * 1993/04/26). 2) That updating only on newer timestamps interferes 15113 * with our earlier PAWS tests, so this check should be solely 15114 * predicated on the sequence space of this segment. 3) That we 15115 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 15116 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 15117 * SEG.Len, This modified check allows us to overcome RFC1323's 15118 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 15119 * p.869. In such cases, we can still calculate the RTT correctly 15120 * when RCV.NXT == Last.ACK.Sent. 15121 */ 15122 if ((to->to_flags & TOF_TS) != 0 && 15123 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 15124 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 15125 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 15126 tp->ts_recent_age = tcp_ts_getticks(); 15127 tp->ts_recent = to->to_tsval; 15128 } 15129 /* 15130 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 15131 * is on (half-synchronized state), then queue data for later 15132 * processing; else drop segment and return. 15133 */ 15134 if ((thflags & TH_ACK) == 0) { 15135 if (tp->t_flags & TF_NEEDSYN) { 15136 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15137 tiwin, thflags, nxt_pkt)); 15138 } else if (tp->t_flags & TF_ACKNOW) { 15139 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 15140 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 15141 return (ret_val); 15142 } else { 15143 ctf_do_drop(m, NULL); 15144 return (0); 15145 } 15146 } 15147 /* 15148 * case TCPS_LAST_ACK: Ack processing. 15149 */ 15150 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) { 15151 return (ret_val); 15152 } 15153 if (ourfinisacked) { 15154 tp = tcp_close(tp); 15155 ctf_do_drop(m, tp); 15156 return (1); 15157 } 15158 if (sbavail(&so->so_snd)) { 15159 if (ctf_progress_timeout_check(tp, true)) { 15160 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 15161 tp, tick, PROGRESS_DROP, __LINE__); 15162 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 15163 return (1); 15164 } 15165 } 15166 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15167 tiwin, thflags, nxt_pkt)); 15168 } 15169 15170 /* 15171 * Return value of 1, the TCB is unlocked and most 15172 * likely gone, return value of 0, the TCP is still 15173 * locked. 15174 */ 15175 static int 15176 rack_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 15177 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 15178 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 15179 { 15180 int32_t ret_val = 0; 15181 int32_t orig_tlen = tlen; 15182 int32_t ourfinisacked = 0; 15183 struct tcp_rack *rack; 15184 15185 rack = (struct tcp_rack *)tp->t_fb_ptr; 15186 ctf_calc_rwin(so, tp); 15187 15188 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 15189 if ((thflags & TH_RST) || 15190 (tp->t_fin_is_rst && (thflags & TH_FIN))) 15191 return (__ctf_process_rst(m, th, so, tp, 15192 &rack->r_ctl.challenge_ack_ts, 15193 &rack->r_ctl.challenge_ack_cnt)); 15194 /* 15195 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 15196 * synchronized state. 15197 */ 15198 if (thflags & TH_SYN) { 15199 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 15200 return (ret_val); 15201 } 15202 /* 15203 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 15204 * it's less than ts_recent, drop it. 15205 */ 15206 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 15207 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 15208 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 15209 return (ret_val); 15210 } 15211 if (_ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val, 15212 &rack->r_ctl.challenge_ack_ts, 15213 &rack->r_ctl.challenge_ack_cnt)) { 15214 return (ret_val); 15215 } 15216 /* 15217 * If new data are received on a connection after the user processes 15218 * are gone, then RST the other end. 15219 */ 15220 if ((tp->t_flags & TF_CLOSED) && tlen && 15221 rack_check_data_after_close(m, tp, &tlen, th, so)) 15222 return (1); 15223 /* 15224 * If last ACK falls within this segment's sequence numbers, record 15225 * its timestamp. NOTE: 1) That the test incorporates suggestions 15226 * from the latest proposal of the tcplw@cray.com list (Braden 15227 * 1993/04/26). 2) That updating only on newer timestamps interferes 15228 * with our earlier PAWS tests, so this check should be solely 15229 * predicated on the sequence space of this segment. 3) That we 15230 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 15231 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 15232 * SEG.Len, This modified check allows us to overcome RFC1323's 15233 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 15234 * p.869. In such cases, we can still calculate the RTT correctly 15235 * when RCV.NXT == Last.ACK.Sent. 15236 */ 15237 if ((to->to_flags & TOF_TS) != 0 && 15238 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 15239 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 15240 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 15241 tp->ts_recent_age = tcp_ts_getticks(); 15242 tp->ts_recent = to->to_tsval; 15243 } 15244 /* 15245 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 15246 * is on (half-synchronized state), then queue data for later 15247 * processing; else drop segment and return. 15248 */ 15249 if ((thflags & TH_ACK) == 0) { 15250 if (tp->t_flags & TF_NEEDSYN) { 15251 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15252 tiwin, thflags, nxt_pkt)); 15253 } else if (tp->t_flags & TF_ACKNOW) { 15254 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 15255 ((struct tcp_rack *)tp->t_fb_ptr)->r_wanted_output = 1; 15256 return (ret_val); 15257 } else { 15258 ctf_do_drop(m, NULL); 15259 return (0); 15260 } 15261 } 15262 /* 15263 * Ack processing. 15264 */ 15265 if (rack_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val, orig_tlen)) { 15266 return (ret_val); 15267 } 15268 if (sbavail(&so->so_snd)) { 15269 if (ctf_progress_timeout_check(tp, true)) { 15270 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 15271 tp, tick, PROGRESS_DROP, __LINE__); 15272 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 15273 return (1); 15274 } 15275 } 15276 return (rack_process_data(m, th, so, tp, drop_hdrlen, tlen, 15277 tiwin, thflags, nxt_pkt)); 15278 } 15279 15280 static void inline 15281 rack_clear_rate_sample(struct tcp_rack *rack) 15282 { 15283 rack->r_ctl.rack_rs.rs_flags = RACK_RTT_EMPTY; 15284 rack->r_ctl.rack_rs.rs_rtt_cnt = 0; 15285 rack->r_ctl.rack_rs.rs_rtt_tot = 0; 15286 } 15287 15288 static void 15289 rack_set_pace_segments(struct tcpcb *tp, struct tcp_rack *rack, uint32_t line, uint64_t *fill_override) 15290 { 15291 uint64_t bw_est, rate_wanted; 15292 int chged = 0; 15293 uint32_t user_max, orig_min, orig_max; 15294 15295 #ifdef TCP_REQUEST_TRK 15296 if (rack->rc_hybrid_mode && 15297 (rack->r_ctl.rc_pace_max_segs != 0) && 15298 (rack_hybrid_allow_set_maxseg == 1) && 15299 (rack->r_ctl.rc_last_sft != NULL)) { 15300 rack->r_ctl.rc_last_sft->hybrid_flags &= ~TCP_HYBRID_PACING_SETMSS; 15301 return; 15302 } 15303 #endif 15304 orig_min = rack->r_ctl.rc_pace_min_segs; 15305 orig_max = rack->r_ctl.rc_pace_max_segs; 15306 user_max = ctf_fixed_maxseg(tp) * rack->rc_user_set_max_segs; 15307 if (ctf_fixed_maxseg(tp) != rack->r_ctl.rc_pace_min_segs) 15308 chged = 1; 15309 rack->r_ctl.rc_pace_min_segs = ctf_fixed_maxseg(tp); 15310 if (rack->use_fixed_rate || rack->rc_force_max_seg) { 15311 if (user_max != rack->r_ctl.rc_pace_max_segs) 15312 chged = 1; 15313 } 15314 if (rack->rc_force_max_seg) { 15315 rack->r_ctl.rc_pace_max_segs = user_max; 15316 } else if (rack->use_fixed_rate) { 15317 bw_est = rack_get_bw(rack); 15318 if ((rack->r_ctl.crte == NULL) || 15319 (bw_est != rack->r_ctl.crte->rate)) { 15320 rack->r_ctl.rc_pace_max_segs = user_max; 15321 } else { 15322 /* We are pacing right at the hardware rate */ 15323 uint32_t segsiz, pace_one; 15324 15325 if (rack_pace_one_seg || 15326 (rack->r_ctl.rc_user_set_min_segs == 1)) 15327 pace_one = 1; 15328 else 15329 pace_one = 0; 15330 segsiz = min(ctf_fixed_maxseg(tp), 15331 rack->r_ctl.rc_pace_min_segs); 15332 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor( 15333 tp, bw_est, segsiz, pace_one, 15334 rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor); 15335 } 15336 } else if (rack->rc_always_pace) { 15337 if (rack->r_ctl.gp_bw || 15338 rack->r_ctl.init_rate) { 15339 /* We have a rate of some sort set */ 15340 uint32_t orig; 15341 15342 bw_est = rack_get_bw(rack); 15343 orig = rack->r_ctl.rc_pace_max_segs; 15344 if (fill_override) 15345 rate_wanted = *fill_override; 15346 else 15347 rate_wanted = rack_get_gp_est(rack); 15348 if (rate_wanted) { 15349 /* We have something */ 15350 rack->r_ctl.rc_pace_max_segs = rack_get_pacing_len(rack, 15351 rate_wanted, 15352 ctf_fixed_maxseg(rack->rc_tp)); 15353 } else 15354 rack->r_ctl.rc_pace_max_segs = rack->r_ctl.rc_pace_min_segs; 15355 if (orig != rack->r_ctl.rc_pace_max_segs) 15356 chged = 1; 15357 } else if ((rack->r_ctl.gp_bw == 0) && 15358 (rack->r_ctl.rc_pace_max_segs == 0)) { 15359 /* 15360 * If we have nothing limit us to bursting 15361 * out IW sized pieces. 15362 */ 15363 chged = 1; 15364 rack->r_ctl.rc_pace_max_segs = rc_init_window(rack); 15365 } 15366 } 15367 if (rack->r_ctl.rc_pace_max_segs > PACE_MAX_IP_BYTES) { 15368 chged = 1; 15369 rack->r_ctl.rc_pace_max_segs = PACE_MAX_IP_BYTES; 15370 } 15371 if (chged) 15372 rack_log_type_pacing_sizes(tp, rack, orig_min, orig_max, line, 2); 15373 } 15374 15375 15376 static void 15377 rack_init_fsb_block(struct tcpcb *tp, struct tcp_rack *rack, int32_t flags) 15378 { 15379 #ifdef INET6 15380 struct ip6_hdr *ip6 = NULL; 15381 #endif 15382 #ifdef INET 15383 struct ip *ip = NULL; 15384 #endif 15385 struct udphdr *udp = NULL; 15386 15387 /* Ok lets fill in the fast block, it can only be used with no IP options! */ 15388 #ifdef INET6 15389 if (rack->r_is_v6) { 15390 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 15391 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 15392 if (tp->t_port) { 15393 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr); 15394 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 15395 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 15396 udp->uh_dport = tp->t_port; 15397 rack->r_ctl.fsb.udp = udp; 15398 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1); 15399 } else 15400 { 15401 rack->r_ctl.fsb.th = (struct tcphdr *)(ip6 + 1); 15402 rack->r_ctl.fsb.udp = NULL; 15403 } 15404 tcpip_fillheaders(rack->rc_inp, 15405 tp->t_port, 15406 ip6, rack->r_ctl.fsb.th); 15407 rack->r_ctl.fsb.hoplimit = in6_selecthlim(rack->rc_inp, NULL); 15408 } else 15409 #endif /* INET6 */ 15410 #ifdef INET 15411 { 15412 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr); 15413 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 15414 if (tp->t_port) { 15415 rack->r_ctl.fsb.tcp_ip_hdr_len += sizeof(struct udphdr); 15416 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 15417 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 15418 udp->uh_dport = tp->t_port; 15419 rack->r_ctl.fsb.udp = udp; 15420 rack->r_ctl.fsb.th = (struct tcphdr *)(udp + 1); 15421 } else 15422 { 15423 rack->r_ctl.fsb.udp = NULL; 15424 rack->r_ctl.fsb.th = (struct tcphdr *)(ip + 1); 15425 } 15426 tcpip_fillheaders(rack->rc_inp, 15427 tp->t_port, 15428 ip, rack->r_ctl.fsb.th); 15429 rack->r_ctl.fsb.hoplimit = tptoinpcb(tp)->inp_ip_ttl; 15430 } 15431 #endif 15432 rack->r_ctl.fsb.recwin = lmin(lmax(sbspace(&tptosocket(tp)->so_rcv), 0), 15433 (long)TCP_MAXWIN << tp->rcv_scale); 15434 rack->r_fsb_inited = 1; 15435 } 15436 15437 static int 15438 rack_init_fsb(struct tcpcb *tp, struct tcp_rack *rack) 15439 { 15440 /* 15441 * Allocate the larger of spaces V6 if available else just 15442 * V4 and include udphdr (overbook) 15443 */ 15444 #ifdef INET6 15445 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + sizeof(struct udphdr); 15446 #else 15447 rack->r_ctl.fsb.tcp_ip_hdr_len = sizeof(struct tcpiphdr) + sizeof(struct udphdr); 15448 #endif 15449 rack->r_ctl.fsb.tcp_ip_hdr = malloc(rack->r_ctl.fsb.tcp_ip_hdr_len, 15450 M_TCPFSB, M_NOWAIT|M_ZERO); 15451 if (rack->r_ctl.fsb.tcp_ip_hdr == NULL) { 15452 return (ENOMEM); 15453 } 15454 rack->r_fsb_inited = 0; 15455 return (0); 15456 } 15457 15458 static void 15459 rack_log_hystart_event(struct tcp_rack *rack, uint32_t high_seq, uint8_t mod) 15460 { 15461 /* 15462 * Types of logs (mod value) 15463 * 20 - Initial round setup 15464 * 21 - Rack declares a new round. 15465 */ 15466 struct tcpcb *tp; 15467 15468 tp = rack->rc_tp; 15469 if (tcp_bblogging_on(tp)) { 15470 union tcp_log_stackspecific log; 15471 struct timeval tv; 15472 15473 memset(&log, 0, sizeof(log)); 15474 log.u_bbr.flex1 = rack->r_ctl.current_round; 15475 log.u_bbr.flex2 = rack->r_ctl.roundends; 15476 log.u_bbr.flex3 = high_seq; 15477 log.u_bbr.flex4 = tp->snd_max; 15478 log.u_bbr.flex8 = mod; 15479 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 15480 log.u_bbr.cur_del_rate = rack->rc_tp->t_sndbytes; 15481 log.u_bbr.delRate = rack->rc_tp->t_snd_rxt_bytes; 15482 TCP_LOG_EVENTP(tp, NULL, 15483 &tptosocket(tp)->so_rcv, 15484 &tptosocket(tp)->so_snd, 15485 TCP_HYSTART, 0, 15486 0, &log, false, &tv); 15487 } 15488 } 15489 15490 static void 15491 rack_deferred_init(struct tcpcb *tp, struct tcp_rack *rack) 15492 { 15493 rack->rack_deferred_inited = 1; 15494 rack->r_ctl.roundends = tp->snd_max; 15495 rack->r_ctl.rc_high_rwnd = tp->snd_wnd; 15496 rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 15497 } 15498 15499 static void 15500 rack_init_retransmit_value(struct tcp_rack *rack, int ctl) 15501 { 15502 /* Retransmit bit controls. 15503 * 15504 * The setting of these values control one of 15505 * three settings you can have and dictate 15506 * how rack does retransmissions. Note this 15507 * is in *any* mode i.e. pacing on or off DGP 15508 * fixed rate pacing, or just bursting rack. 15509 * 15510 * 1 - Use full sized retransmits i.e. limit 15511 * the size to whatever the pace_max_segments 15512 * size is. 15513 * 15514 * 2 - Use pacer min granularity as a guide to 15515 * the size combined with the current calculated 15516 * goodput b/w measurement. So for example if 15517 * the goodput is measured at 20Mbps we would 15518 * calculate 8125 (pacer minimum 250usec in 15519 * that b/w) and then round it up to the next 15520 * MSS i.e. for 1448 mss 6 MSS or 8688 bytes. 15521 * 15522 * 0 - The rack default 1 MSS (anything not 0/1/2 15523 * fall here too if we are setting via rack_init()). 15524 * 15525 */ 15526 if (ctl == 1) { 15527 rack->full_size_rxt = 1; 15528 rack->shape_rxt_to_pacing_min = 0; 15529 } else if (ctl == 2) { 15530 rack->full_size_rxt = 0; 15531 rack->shape_rxt_to_pacing_min = 1; 15532 } else { 15533 rack->full_size_rxt = 0; 15534 rack->shape_rxt_to_pacing_min = 0; 15535 } 15536 } 15537 15538 static void 15539 rack_log_chg_info(struct tcpcb *tp, struct tcp_rack *rack, uint8_t mod, 15540 uint32_t flex1, 15541 uint32_t flex2, 15542 uint32_t flex3) 15543 { 15544 if (tcp_bblogging_on(rack->rc_tp)) { 15545 union tcp_log_stackspecific log; 15546 struct timeval tv; 15547 15548 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 15549 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 15550 log.u_bbr.flex8 = mod; 15551 log.u_bbr.flex1 = flex1; 15552 log.u_bbr.flex2 = flex2; 15553 log.u_bbr.flex3 = flex3; 15554 tcp_log_event(tp, NULL, NULL, NULL, TCP_CHG_QUERY, 0, 15555 0, &log, false, NULL, __func__, __LINE__, &tv); 15556 } 15557 } 15558 15559 static int 15560 rack_chg_query(struct tcpcb *tp, struct tcp_query_resp *reqr) 15561 { 15562 struct tcp_rack *rack; 15563 struct rack_sendmap *rsm; 15564 int i; 15565 15566 15567 rack = (struct tcp_rack *)tp->t_fb_ptr; 15568 switch (reqr->req) { 15569 case TCP_QUERY_SENDMAP: 15570 if ((reqr->req_param == tp->snd_max) || 15571 (tp->snd_max == tp->snd_una)){ 15572 /* Unlikely */ 15573 return (0); 15574 } 15575 rsm = tqhash_find(rack->r_ctl.tqh, reqr->req_param); 15576 if (rsm == NULL) { 15577 /* Can't find that seq -- unlikely */ 15578 return (0); 15579 } 15580 reqr->sendmap_start = rsm->r_start; 15581 reqr->sendmap_end = rsm->r_end; 15582 reqr->sendmap_send_cnt = rsm->r_rtr_cnt; 15583 reqr->sendmap_fas = rsm->r_fas; 15584 if (reqr->sendmap_send_cnt > SNDMAP_NRTX) 15585 reqr->sendmap_send_cnt = SNDMAP_NRTX; 15586 for(i=0; i<reqr->sendmap_send_cnt; i++) 15587 reqr->sendmap_time[i] = rsm->r_tim_lastsent[i]; 15588 reqr->sendmap_ack_arrival = rsm->r_ack_arrival; 15589 reqr->sendmap_flags = rsm->r_flags & SNDMAP_MASK; 15590 reqr->sendmap_r_rtr_bytes = rsm->r_rtr_bytes; 15591 reqr->sendmap_dupacks = rsm->r_dupack; 15592 rack_log_chg_info(tp, rack, 1, 15593 rsm->r_start, 15594 rsm->r_end, 15595 rsm->r_flags); 15596 return(1); 15597 break; 15598 case TCP_QUERY_TIMERS_UP: 15599 if (rack->r_ctl.rc_hpts_flags == 0) { 15600 /* no timers up */ 15601 return (0); 15602 } 15603 reqr->timer_hpts_flags = rack->r_ctl.rc_hpts_flags; 15604 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 15605 reqr->timer_pacing_to = rack->r_ctl.rc_last_output_to; 15606 } 15607 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 15608 reqr->timer_timer_exp = rack->r_ctl.rc_timer_exp; 15609 } 15610 rack_log_chg_info(tp, rack, 2, 15611 rack->r_ctl.rc_hpts_flags, 15612 rack->r_ctl.rc_last_output_to, 15613 rack->r_ctl.rc_timer_exp); 15614 return (1); 15615 break; 15616 case TCP_QUERY_RACK_TIMES: 15617 /* Reordering items */ 15618 reqr->rack_num_dsacks = rack->r_ctl.num_dsack; 15619 reqr->rack_reorder_ts = rack->r_ctl.rc_reorder_ts; 15620 /* Timerstamps and timers */ 15621 reqr->rack_rxt_last_time = rack->r_ctl.rc_tlp_rxt_last_time; 15622 reqr->rack_min_rtt = rack->r_ctl.rc_rack_min_rtt; 15623 reqr->rack_rtt = rack->rc_rack_rtt; 15624 reqr->rack_tmit_time = rack->r_ctl.rc_rack_tmit_time; 15625 reqr->rack_srtt_measured = rack->rc_srtt_measure_made; 15626 /* PRR data */ 15627 reqr->rack_sacked = rack->r_ctl.rc_sacked; 15628 reqr->rack_holes_rxt = rack->r_ctl.rc_holes_rxt; 15629 reqr->rack_prr_delivered = rack->r_ctl.rc_prr_delivered; 15630 reqr->rack_prr_recovery_fs = rack->r_ctl.rc_prr_recovery_fs; 15631 reqr->rack_prr_sndcnt = rack->r_ctl.rc_prr_sndcnt; 15632 reqr->rack_prr_out = rack->r_ctl.rc_prr_out; 15633 /* TLP and persists info */ 15634 reqr->rack_tlp_out = rack->rc_tlp_in_progress; 15635 reqr->rack_tlp_cnt_out = rack->r_ctl.rc_tlp_cnt_out; 15636 if (rack->rc_in_persist) { 15637 reqr->rack_time_went_idle = rack->r_ctl.rc_went_idle_time; 15638 reqr->rack_in_persist = 1; 15639 } else { 15640 reqr->rack_time_went_idle = 0; 15641 reqr->rack_in_persist = 0; 15642 } 15643 if (rack->r_wanted_output) 15644 reqr->rack_wanted_output = 1; 15645 else 15646 reqr->rack_wanted_output = 0; 15647 return (1); 15648 break; 15649 default: 15650 return (-EINVAL); 15651 } 15652 } 15653 15654 static void 15655 rack_switch_failed(struct tcpcb *tp) 15656 { 15657 /* 15658 * This method gets called if a stack switch was 15659 * attempted and it failed. We are left 15660 * but our hpts timers were stopped and we 15661 * need to validate time units and t_flags2. 15662 */ 15663 struct tcp_rack *rack; 15664 struct timeval tv; 15665 uint32_t cts; 15666 uint32_t toval; 15667 struct hpts_diag diag; 15668 15669 rack = (struct tcp_rack *)tp->t_fb_ptr; 15670 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_USEC); 15671 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 15672 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 15673 else 15674 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 15675 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 15676 tp->t_flags2 |= TF2_MBUF_ACKCMP; 15677 if (tp->t_in_hpts > IHPTS_NONE) { 15678 /* Strange */ 15679 return; 15680 } 15681 cts = tcp_get_usecs(&tv); 15682 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 15683 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) { 15684 toval = rack->r_ctl.rc_last_output_to - cts; 15685 } else { 15686 /* one slot please */ 15687 toval = HPTS_TICKS_PER_SLOT; 15688 } 15689 } else if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 15690 if (TSTMP_GT(rack->r_ctl.rc_timer_exp, cts)) { 15691 toval = rack->r_ctl.rc_timer_exp - cts; 15692 } else { 15693 /* one slot please */ 15694 toval = HPTS_TICKS_PER_SLOT; 15695 } 15696 } else 15697 toval = HPTS_TICKS_PER_SLOT; 15698 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval), 15699 __LINE__, &diag); 15700 rack_log_hpts_diag(rack, cts, &diag, &tv); 15701 } 15702 15703 static int 15704 rack_init_outstanding(struct tcpcb *tp, struct tcp_rack *rack, uint32_t us_cts, void *ptr) 15705 { 15706 struct rack_sendmap *rsm, *ersm; 15707 int insret __diagused; 15708 /* 15709 * When initing outstanding, we must be quite careful 15710 * to not refer to tp->t_fb_ptr. This has the old rack 15711 * pointer in it, not the "new" one (when we are doing 15712 * a stack switch). 15713 */ 15714 15715 15716 if (tp->t_fb->tfb_chg_query == NULL) { 15717 /* Create a send map for the current outstanding data */ 15718 15719 rsm = rack_alloc(rack); 15720 if (rsm == NULL) { 15721 uma_zfree(rack_pcb_zone, ptr); 15722 return (ENOMEM); 15723 } 15724 rsm->r_no_rtt_allowed = 1; 15725 rsm->r_tim_lastsent[0] = rack_to_usec_ts(&rack->r_ctl.act_rcv_time); 15726 rsm->r_rtr_cnt = 1; 15727 rsm->r_rtr_bytes = 0; 15728 if (tp->t_flags & TF_SENTFIN) 15729 rsm->r_flags |= RACK_HAS_FIN; 15730 rsm->r_end = tp->snd_max; 15731 if (tp->snd_una == tp->iss) { 15732 /* The data space is one beyond snd_una */ 15733 rsm->r_flags |= RACK_HAS_SYN; 15734 rsm->r_start = tp->iss; 15735 rsm->r_end = rsm->r_start + (tp->snd_max - tp->snd_una); 15736 } else 15737 rsm->r_start = tp->snd_una; 15738 rsm->r_dupack = 0; 15739 if (rack->rc_inp->inp_socket->so_snd.sb_mb != NULL) { 15740 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 0, &rsm->soff); 15741 if (rsm->m) { 15742 rsm->orig_m_len = rsm->m->m_len; 15743 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 15744 } else { 15745 rsm->orig_m_len = 0; 15746 rsm->orig_t_space = 0; 15747 } 15748 } else { 15749 /* 15750 * This can happen if we have a stand-alone FIN or 15751 * SYN. 15752 */ 15753 rsm->m = NULL; 15754 rsm->orig_m_len = 0; 15755 rsm->orig_t_space = 0; 15756 rsm->soff = 0; 15757 } 15758 #ifdef INVARIANTS 15759 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) { 15760 panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p", 15761 insret, rack, rsm); 15762 } 15763 #else 15764 (void)tqhash_insert(rack->r_ctl.tqh, rsm); 15765 #endif 15766 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 15767 rsm->r_in_tmap = 1; 15768 } else { 15769 /* We have a query mechanism, lets use it */ 15770 struct tcp_query_resp qr; 15771 int i; 15772 tcp_seq at; 15773 15774 at = tp->snd_una; 15775 while (at != tp->snd_max) { 15776 memset(&qr, 0, sizeof(qr)); 15777 qr.req = TCP_QUERY_SENDMAP; 15778 qr.req_param = at; 15779 if ((*tp->t_fb->tfb_chg_query)(tp, &qr) == 0) 15780 break; 15781 /* Move forward */ 15782 at = qr.sendmap_end; 15783 /* Now lets build the entry for this one */ 15784 rsm = rack_alloc(rack); 15785 if (rsm == NULL) { 15786 uma_zfree(rack_pcb_zone, ptr); 15787 return (ENOMEM); 15788 } 15789 memset(rsm, 0, sizeof(struct rack_sendmap)); 15790 /* Now configure the rsm and insert it */ 15791 rsm->r_dupack = qr.sendmap_dupacks; 15792 rsm->r_start = qr.sendmap_start; 15793 rsm->r_end = qr.sendmap_end; 15794 if (qr.sendmap_fas) 15795 rsm->r_fas = qr.sendmap_end; 15796 else 15797 rsm->r_fas = rsm->r_start - tp->snd_una; 15798 /* 15799 * We have carefully aligned the bits 15800 * so that all we have to do is copy over 15801 * the bits with the mask. 15802 */ 15803 rsm->r_flags = qr.sendmap_flags & SNDMAP_MASK; 15804 rsm->r_rtr_bytes = qr.sendmap_r_rtr_bytes; 15805 rsm->r_rtr_cnt = qr.sendmap_send_cnt; 15806 rsm->r_ack_arrival = qr.sendmap_ack_arrival; 15807 for (i=0 ; i<rsm->r_rtr_cnt; i++) 15808 rsm->r_tim_lastsent[i] = qr.sendmap_time[i]; 15809 rsm->m = sbsndmbuf(&rack->rc_inp->inp_socket->so_snd, 15810 (rsm->r_start - tp->snd_una), &rsm->soff); 15811 if (rsm->m) { 15812 rsm->orig_m_len = rsm->m->m_len; 15813 rsm->orig_t_space = M_TRAILINGROOM(rsm->m); 15814 } else { 15815 rsm->orig_m_len = 0; 15816 rsm->orig_t_space = 0; 15817 } 15818 #ifdef INVARIANTS 15819 if ((insret = tqhash_insert(rack->r_ctl.tqh, rsm)) != 0) { 15820 panic("Insert in tailq_hash fails ret:%d rack:%p rsm:%p", 15821 insret, rack, rsm); 15822 } 15823 #else 15824 (void)tqhash_insert(rack->r_ctl.tqh, rsm); 15825 #endif 15826 if ((rsm->r_flags & RACK_ACKED) == 0) { 15827 TAILQ_FOREACH(ersm, &rack->r_ctl.rc_tmap, r_tnext) { 15828 if (ersm->r_tim_lastsent[(ersm->r_rtr_cnt-1)] > 15829 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]) { 15830 /* 15831 * If the existing ersm was sent at 15832 * a later time than the new one, then 15833 * the new one should appear ahead of this 15834 * ersm. 15835 */ 15836 rsm->r_in_tmap = 1; 15837 TAILQ_INSERT_BEFORE(ersm, rsm, r_tnext); 15838 break; 15839 } 15840 } 15841 if (rsm->r_in_tmap == 0) { 15842 /* 15843 * Not found so shove it on the tail. 15844 */ 15845 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_tmap, rsm, r_tnext); 15846 rsm->r_in_tmap = 1; 15847 } 15848 } else { 15849 if ((rack->r_ctl.rc_sacklast == NULL) || 15850 (SEQ_GT(rsm->r_end, rack->r_ctl.rc_sacklast->r_end))) { 15851 rack->r_ctl.rc_sacklast = rsm; 15852 } 15853 } 15854 rack_log_chg_info(tp, rack, 3, 15855 rsm->r_start, 15856 rsm->r_end, 15857 rsm->r_flags); 15858 } 15859 } 15860 return (0); 15861 } 15862 15863 static void 15864 rack_translate_policer_detect(struct tcp_rack *rack, uint32_t optval) 15865 { 15866 /* 15867 * P = Percent of retransmits 499 = 49.9% 15868 * A = Average number 1 (.1%) -> 169 (16.9%) 15869 * M = Median number of retrans 1 - 16 15870 * MMMM MMMM AAAA AAAA PPPP PPPP PPPP PPPP 15871 * 15872 */ 15873 uint16_t per, upp; 15874 15875 per = optval & 0x0000ffff; 15876 rack->r_ctl.policer_rxt_threshold = (uint32_t)(per & 0xffff); 15877 upp = ((optval & 0xffff0000) >> 16); 15878 rack->r_ctl.policer_avg_threshold = (0x00ff & upp); 15879 rack->r_ctl.policer_med_threshold = ((upp >> 8) & 0x00ff); 15880 if ((rack->r_ctl.policer_rxt_threshold > 0) && 15881 (rack->r_ctl.policer_avg_threshold > 0) && 15882 (rack->r_ctl.policer_med_threshold > 0)) { 15883 rack->policer_detect_on = 1; 15884 } else { 15885 rack->policer_detect_on = 0; 15886 } 15887 rack->r_ctl.saved_policer_val = optval; 15888 policer_detection_log(rack, optval, 15889 rack->r_ctl.policer_avg_threshold, 15890 rack->r_ctl.policer_med_threshold, 15891 rack->r_ctl.policer_rxt_threshold, 11); 15892 } 15893 15894 static int32_t 15895 rack_init(struct tcpcb *tp, void **ptr) 15896 { 15897 struct inpcb *inp = tptoinpcb(tp); 15898 struct tcp_rack *rack = NULL; 15899 uint32_t iwin, snt, us_cts; 15900 size_t sz; 15901 int err, no_query; 15902 15903 tcp_hpts_init(tp); 15904 15905 /* 15906 * First are we the initial or are we a switched stack? 15907 * If we are initing via tcp_newtcppcb the ptr passed 15908 * will be tp->t_fb_ptr. If its a stack switch that 15909 * has a previous stack we can query it will be a local 15910 * var that will in the end be set into t_fb_ptr. 15911 */ 15912 if (ptr == &tp->t_fb_ptr) 15913 no_query = 1; 15914 else 15915 no_query = 0; 15916 *ptr = uma_zalloc(rack_pcb_zone, M_NOWAIT); 15917 if (*ptr == NULL) { 15918 /* 15919 * We need to allocate memory but cant. The INP and INP_INFO 15920 * locks and they are recursive (happens during setup. So a 15921 * scheme to drop the locks fails :( 15922 * 15923 */ 15924 return(ENOMEM); 15925 } 15926 memset(*ptr, 0, sizeof(struct tcp_rack)); 15927 rack = (struct tcp_rack *)*ptr; 15928 rack->r_ctl.tqh = malloc(sizeof(struct tailq_hash), M_TCPFSB, M_NOWAIT); 15929 if (rack->r_ctl.tqh == NULL) { 15930 uma_zfree(rack_pcb_zone, rack); 15931 return(ENOMEM); 15932 } 15933 tqhash_init(rack->r_ctl.tqh); 15934 TAILQ_INIT(&rack->r_ctl.rc_free); 15935 TAILQ_INIT(&rack->r_ctl.rc_tmap); 15936 rack->rc_tp = tp; 15937 rack->rc_inp = inp; 15938 /* Set the flag */ 15939 rack->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 15940 /* Probably not needed but lets be sure */ 15941 rack_clear_rate_sample(rack); 15942 /* 15943 * Save off the default values, socket options will poke 15944 * at these if pacing is not on or we have not yet 15945 * reached where pacing is on (gp_ready/fixed enabled). 15946 * When they get set into the CC module (when gp_ready 15947 * is enabled or we enable fixed) then we will set these 15948 * values into the CC and place in here the old values 15949 * so we have a restoral. Then we will set the flag 15950 * rc_pacing_cc_set. That way whenever we turn off pacing 15951 * or switch off this stack, we will know to go restore 15952 * the saved values. 15953 * 15954 * We specifically put into the beta the ecn value for pacing. 15955 */ 15956 rack->rc_new_rnd_needed = 1; 15957 rack->r_ctl.rc_split_limit = V_tcp_map_split_limit; 15958 /* We want abe like behavior as well */ 15959 15960 rack->r_ctl.rc_saved_beta.newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED; 15961 rack->r_ctl.rc_reorder_fade = rack_reorder_fade; 15962 rack->rc_allow_data_af_clo = rack_ignore_data_after_close; 15963 rack->r_ctl.rc_tlp_threshold = rack_tlp_thresh; 15964 rack->r_ctl.policer_del_mss = rack_req_del_mss; 15965 if ((rack_policer_rxt_thresh > 0) && 15966 (rack_policer_avg_thresh > 0) && 15967 (rack_policer_med_thresh > 0)) { 15968 rack->r_ctl.policer_rxt_threshold = rack_policer_rxt_thresh; 15969 rack->r_ctl.policer_avg_threshold = rack_policer_avg_thresh; 15970 rack->r_ctl.policer_med_threshold = rack_policer_med_thresh; 15971 rack->policer_detect_on = 1; 15972 } else { 15973 rack->policer_detect_on = 0; 15974 } 15975 if (rack_fill_cw_state) 15976 rack->rc_pace_to_cwnd = 1; 15977 if (rack_pacing_min_seg) 15978 rack->r_ctl.rc_user_set_min_segs = rack_pacing_min_seg; 15979 if (use_rack_rr) 15980 rack->use_rack_rr = 1; 15981 if (rack_dnd_default) { 15982 rack->rc_pace_dnd = 1; 15983 } 15984 if (V_tcp_delack_enabled) 15985 tp->t_delayed_ack = 1; 15986 else 15987 tp->t_delayed_ack = 0; 15988 #ifdef TCP_ACCOUNTING 15989 if (rack_tcp_accounting) { 15990 tp->t_flags2 |= TF2_TCP_ACCOUNTING; 15991 } 15992 #endif 15993 rack->r_ctl.pcm_i.cnt_alloc = RACK_DEFAULT_PCM_ARRAY; 15994 sz = (sizeof(struct rack_pcm_stats) * rack->r_ctl.pcm_i.cnt_alloc); 15995 rack->r_ctl.pcm_s = malloc(sz,M_TCPPCM, M_NOWAIT); 15996 if (rack->r_ctl.pcm_s == NULL) { 15997 rack->r_ctl.pcm_i.cnt_alloc = 0; 15998 } 15999 #ifdef NETFLIX_STATS 16000 rack->r_ctl.side_chan_dis_mask = tcp_sidechannel_disable_mask; 16001 #endif 16002 rack->r_ctl.rack_per_upper_bound_ss = (uint8_t)rack_per_upper_bound_ss; 16003 rack->r_ctl.rack_per_upper_bound_ca = (uint8_t)rack_per_upper_bound_ca; 16004 if (rack_enable_shared_cwnd) 16005 rack->rack_enable_scwnd = 1; 16006 rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor; 16007 rack->rc_user_set_max_segs = rack_hptsi_segments; 16008 rack->r_ctl.max_reduction = rack_max_reduce; 16009 rack->rc_force_max_seg = 0; 16010 TAILQ_INIT(&rack->r_ctl.opt_list); 16011 rack->r_ctl.rc_saved_beta.beta = V_newreno_beta_ecn; 16012 rack->r_ctl.rc_saved_beta.beta_ecn = V_newreno_beta_ecn; 16013 if (rack_hibeta_setting) { 16014 rack->rack_hibeta = 1; 16015 if ((rack_hibeta_setting >= 50) && 16016 (rack_hibeta_setting <= 100)) { 16017 rack->r_ctl.rc_saved_beta.beta = rack_hibeta_setting; 16018 rack->r_ctl.saved_hibeta = rack_hibeta_setting; 16019 } 16020 } else { 16021 rack->r_ctl.saved_hibeta = 50; 16022 } 16023 /* 16024 * We initialize to all ones so we never match 0 16025 * just in case the client sends in 0, it hopefully 16026 * will never have all 1's in ms :-) 16027 */ 16028 rack->r_ctl.last_tm_mark = 0xffffffffffffffff; 16029 rack->r_ctl.rc_reorder_shift = rack_reorder_thresh; 16030 rack->r_ctl.rc_pkt_delay = rack_pkt_delay; 16031 rack->r_ctl.pol_bw_comp = rack_policing_do_bw_comp; 16032 rack->r_ctl.rc_tlp_cwnd_reduce = rack_lower_cwnd_at_tlp; 16033 rack->r_ctl.rc_lowest_us_rtt = 0xffffffff; 16034 rack->r_ctl.rc_highest_us_rtt = 0; 16035 rack->r_ctl.bw_rate_cap = rack_bw_rate_cap; 16036 rack->pcm_enabled = rack_pcm_is_enabled; 16037 if (rack_fillcw_bw_cap) 16038 rack->r_ctl.fillcw_cap = rack_fillcw_bw_cap; 16039 rack->r_ctl.timer_slop = TICKS_2_USEC(tcp_rexmit_slop); 16040 if (rack_use_cmp_acks) 16041 rack->r_use_cmp_ack = 1; 16042 if (rack_disable_prr) 16043 rack->rack_no_prr = 1; 16044 if (rack_gp_no_rec_chg) 16045 rack->rc_gp_no_rec_chg = 1; 16046 if (rack_pace_every_seg && tcp_can_enable_pacing()) { 16047 rack->r_ctl.pacing_method |= RACK_REG_PACING; 16048 rack->rc_always_pace = 1; 16049 if (rack->rack_hibeta) 16050 rack_set_cc_pacing(rack); 16051 } else 16052 rack->rc_always_pace = 0; 16053 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) 16054 rack->r_mbuf_queue = 1; 16055 else 16056 rack->r_mbuf_queue = 0; 16057 rack_set_pace_segments(tp, rack, __LINE__, NULL); 16058 if (rack_limits_scwnd) 16059 rack->r_limit_scw = 1; 16060 else 16061 rack->r_limit_scw = 0; 16062 rack_init_retransmit_value(rack, rack_rxt_controls); 16063 rack->rc_labc = V_tcp_abc_l_var; 16064 if (rack_honors_hpts_min_to) 16065 rack->r_use_hpts_min = 1; 16066 if (tp->snd_una != 0) { 16067 rack->r_ctl.idle_snd_una = tp->snd_una; 16068 rack->rc_sendvars_notset = 0; 16069 /* 16070 * Make sure any TCP timers are not running. 16071 */ 16072 tcp_timer_stop(tp); 16073 } else { 16074 /* 16075 * Server side, we are called from the 16076 * syn-cache. This means none of the 16077 * snd_una/max are set yet so we have 16078 * to defer this until the first send. 16079 */ 16080 rack->rc_sendvars_notset = 1; 16081 } 16082 16083 rack->r_ctl.rc_rate_sample_method = rack_rate_sample_method; 16084 rack->rack_tlp_threshold_use = rack_tlp_threshold_use; 16085 rack->r_ctl.rc_prr_sendalot = rack_send_a_lot_in_prr; 16086 rack->r_ctl.rc_min_to = rack_min_to; 16087 microuptime(&rack->r_ctl.act_rcv_time); 16088 rack->r_ctl.rc_last_time_decay = rack->r_ctl.act_rcv_time; 16089 rack->r_ctl.rack_per_of_gp_ss = rack_per_of_gp_ss; 16090 if (rack_hw_up_only) 16091 rack->r_up_only = 1; 16092 if (rack_do_dyn_mul) { 16093 /* When dynamic adjustment is on CA needs to start at 100% */ 16094 rack->rc_gp_dyn_mul = 1; 16095 if (rack_do_dyn_mul >= 100) 16096 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul; 16097 } else 16098 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca; 16099 rack->r_ctl.rack_per_of_gp_rec = rack_per_of_gp_rec; 16100 if (rack_timely_off) { 16101 rack->rc_skip_timely = 1; 16102 } 16103 if (rack->rc_skip_timely) { 16104 rack->r_ctl.rack_per_of_gp_rec = 90; 16105 rack->r_ctl.rack_per_of_gp_ca = 100; 16106 rack->r_ctl.rack_per_of_gp_ss = 250; 16107 } 16108 rack->r_ctl.rack_per_of_gp_probertt = rack_per_of_gp_probertt; 16109 rack->r_ctl.rc_tlp_rxt_last_time = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time); 16110 rack->r_ctl.last_rcv_tstmp_for_rtt = tcp_tv_to_mssectick(&rack->r_ctl.act_rcv_time); 16111 16112 setup_time_filter_small(&rack->r_ctl.rc_gp_min_rtt, FILTER_TYPE_MIN, 16113 rack_probertt_filter_life); 16114 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 16115 rack->r_ctl.rc_lower_rtt_us_cts = us_cts; 16116 rack->r_ctl.rc_time_of_last_probertt = us_cts; 16117 rack->r_ctl.rc_went_idle_time = us_cts; 16118 rack->r_ctl.challenge_ack_ts = tcp_ts_getticks() - (tcp_ack_war_time_window + 1); 16119 rack->r_ctl.rc_time_probertt_starts = 0; 16120 16121 rack->r_ctl.gp_rnd_thresh = rack_rnd_cnt_req & 0xff; 16122 if (rack_rnd_cnt_req & 0x10000) 16123 rack->r_ctl.gate_to_fs = 1; 16124 rack->r_ctl.gp_gain_req = rack_gp_gain_req; 16125 if ((rack_rnd_cnt_req & 0x100) > 0) { 16126 16127 } 16128 if (rack_dsack_std_based & 0x1) { 16129 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */ 16130 rack->rc_rack_tmr_std_based = 1; 16131 } 16132 if (rack_dsack_std_based & 0x2) { 16133 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */ 16134 rack->rc_rack_use_dsack = 1; 16135 } 16136 /* We require at least one measurement, even if the sysctl is 0 */ 16137 if (rack_req_measurements) 16138 rack->r_ctl.req_measurements = rack_req_measurements; 16139 else 16140 rack->r_ctl.req_measurements = 1; 16141 if (rack_enable_hw_pacing) 16142 rack->rack_hdw_pace_ena = 1; 16143 if (rack_hw_rate_caps) 16144 rack->r_rack_hw_rate_caps = 1; 16145 #ifdef TCP_SAD_DETECTION 16146 rack->do_detection = 1; 16147 #else 16148 rack->do_detection = 0; 16149 #endif 16150 if (rack_non_rxt_use_cr) 16151 rack->rack_rec_nonrxt_use_cr = 1; 16152 /* Lets setup the fsb block */ 16153 err = rack_init_fsb(tp, rack); 16154 if (err) { 16155 uma_zfree(rack_pcb_zone, *ptr); 16156 *ptr = NULL; 16157 return (err); 16158 } 16159 if (rack_do_hystart) { 16160 tp->t_ccv.flags |= CCF_HYSTART_ALLOWED; 16161 if (rack_do_hystart > 1) 16162 tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND; 16163 if (rack_do_hystart > 2) 16164 tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH; 16165 } 16166 /* Log what we will do with queries */ 16167 rack_log_chg_info(tp, rack, 7, 16168 no_query, 0, 0); 16169 if (rack_def_profile) 16170 rack_set_profile(rack, rack_def_profile); 16171 /* Cancel the GP measurement in progress */ 16172 tp->t_flags &= ~TF_GPUTINPROG; 16173 if ((tp->t_state != TCPS_CLOSED) && 16174 (tp->t_state != TCPS_TIME_WAIT)) { 16175 /* 16176 * We are already open, we may 16177 * need to adjust a few things. 16178 */ 16179 if (SEQ_GT(tp->snd_max, tp->iss)) 16180 snt = tp->snd_max - tp->iss; 16181 else 16182 snt = 0; 16183 iwin = rc_init_window(rack); 16184 if ((snt < iwin) && 16185 (no_query == 1)) { 16186 /* We are not past the initial window 16187 * on the first init (i.e. a stack switch 16188 * has not yet occured) so we need to make 16189 * sure cwnd and ssthresh is correct. 16190 */ 16191 if (tp->snd_cwnd < iwin) 16192 tp->snd_cwnd = iwin; 16193 /* 16194 * If we are within the initial window 16195 * we want ssthresh to be unlimited. Setting 16196 * it to the rwnd (which the default stack does 16197 * and older racks) is not really a good idea 16198 * since we want to be in SS and grow both the 16199 * cwnd and the rwnd (via dynamic rwnd growth). If 16200 * we set it to the rwnd then as the peer grows its 16201 * rwnd we will be stuck in CA and never hit SS. 16202 * 16203 * Its far better to raise it up high (this takes the 16204 * risk that there as been a loss already, probably 16205 * we should have an indicator in all stacks of loss 16206 * but we don't), but considering the normal use this 16207 * is a risk worth taking. The consequences of not 16208 * hitting SS are far worse than going one more time 16209 * into it early on (before we have sent even a IW). 16210 * It is highly unlikely that we will have had a loss 16211 * before getting the IW out. 16212 */ 16213 tp->snd_ssthresh = 0xffffffff; 16214 } 16215 /* 16216 * Any init based on sequence numbers 16217 * should be done in the deferred init path 16218 * since we can be CLOSED and not have them 16219 * inited when rack_init() is called. We 16220 * are not closed so lets call it. 16221 */ 16222 rack_deferred_init(tp, rack); 16223 } 16224 if ((tp->t_state != TCPS_CLOSED) && 16225 (tp->t_state != TCPS_TIME_WAIT) && 16226 (no_query == 0) && 16227 (tp->snd_una != tp->snd_max)) { 16228 err = rack_init_outstanding(tp, rack, us_cts, *ptr); 16229 if (err) { 16230 *ptr = NULL; 16231 return(err); 16232 } 16233 } 16234 rack_stop_all_timers(tp, rack); 16235 /* Setup all the t_flags2 */ 16236 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 16237 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 16238 else 16239 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 16240 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 16241 tp->t_flags2 |= TF2_MBUF_ACKCMP; 16242 /* 16243 * Timers in Rack are kept in microseconds so lets 16244 * convert any initial incoming variables 16245 * from ticks into usecs. Note that we 16246 * also change the values of t_srtt and t_rttvar, if 16247 * they are non-zero. They are kept with a 5 16248 * bit decimal so we have to carefully convert 16249 * these to get the full precision. 16250 */ 16251 rack_convert_rtts(tp); 16252 rack_log_hystart_event(rack, rack->r_ctl.roundends, 20); 16253 if ((tptoinpcb(tp)->inp_flags & INP_DROPPED) == 0) { 16254 /* We do not start any timers on DROPPED connections */ 16255 if (tp->t_fb->tfb_chg_query == NULL) { 16256 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 16257 } else { 16258 struct tcp_query_resp qr; 16259 int ret; 16260 16261 memset(&qr, 0, sizeof(qr)); 16262 16263 /* Get the misc time stamps and such for rack */ 16264 qr.req = TCP_QUERY_RACK_TIMES; 16265 ret = (*tp->t_fb->tfb_chg_query)(tp, &qr); 16266 if (ret == 1) { 16267 rack->r_ctl.rc_reorder_ts = qr.rack_reorder_ts; 16268 rack->r_ctl.num_dsack = qr.rack_num_dsacks; 16269 rack->r_ctl.rc_tlp_rxt_last_time = qr.rack_rxt_last_time; 16270 rack->r_ctl.rc_rack_min_rtt = qr.rack_min_rtt; 16271 rack->rc_rack_rtt = qr.rack_rtt; 16272 rack->r_ctl.rc_rack_tmit_time = qr.rack_tmit_time; 16273 rack->r_ctl.rc_sacked = qr.rack_sacked; 16274 rack->r_ctl.rc_holes_rxt = qr.rack_holes_rxt; 16275 rack->r_ctl.rc_prr_delivered = qr.rack_prr_delivered; 16276 rack->r_ctl.rc_prr_recovery_fs = qr.rack_prr_recovery_fs; 16277 rack->r_ctl.rc_prr_sndcnt = qr.rack_prr_sndcnt; 16278 rack->r_ctl.rc_prr_out = qr.rack_prr_out; 16279 if (qr.rack_tlp_out) { 16280 rack->rc_tlp_in_progress = 1; 16281 rack->r_ctl.rc_tlp_cnt_out = qr.rack_tlp_cnt_out; 16282 } else { 16283 rack->rc_tlp_in_progress = 0; 16284 rack->r_ctl.rc_tlp_cnt_out = 0; 16285 } 16286 if (qr.rack_srtt_measured) 16287 rack->rc_srtt_measure_made = 1; 16288 if (qr.rack_in_persist == 1) { 16289 rack->r_ctl.rc_went_idle_time = qr.rack_time_went_idle; 16290 #ifdef NETFLIX_SHARED_CWND 16291 if (rack->r_ctl.rc_scw) { 16292 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 16293 rack->rack_scwnd_is_idle = 1; 16294 } 16295 #endif 16296 rack->r_ctl.persist_lost_ends = 0; 16297 rack->probe_not_answered = 0; 16298 rack->forced_ack = 0; 16299 tp->t_rxtshift = 0; 16300 rack->rc_in_persist = 1; 16301 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 16302 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 16303 } 16304 if (qr.rack_wanted_output) 16305 rack->r_wanted_output = 1; 16306 rack_log_chg_info(tp, rack, 6, 16307 qr.rack_min_rtt, 16308 qr.rack_rtt, 16309 qr.rack_reorder_ts); 16310 } 16311 /* Get the old stack timers */ 16312 qr.req_param = 0; 16313 qr.req = TCP_QUERY_TIMERS_UP; 16314 ret = (*tp->t_fb->tfb_chg_query)(tp, &qr); 16315 if (ret) { 16316 /* 16317 * non-zero return means we have a timer('s) 16318 * to start. Zero means no timer (no keepalive 16319 * I suppose). 16320 */ 16321 uint32_t tov = 0; 16322 16323 rack->r_ctl.rc_hpts_flags = qr.timer_hpts_flags; 16324 if (qr.timer_hpts_flags & PACE_PKT_OUTPUT) { 16325 rack->r_ctl.rc_last_output_to = qr.timer_pacing_to; 16326 if (TSTMP_GT(qr.timer_pacing_to, us_cts)) 16327 tov = qr.timer_pacing_to - us_cts; 16328 else 16329 tov = HPTS_TICKS_PER_SLOT; 16330 } 16331 if (qr.timer_hpts_flags & PACE_TMR_MASK) { 16332 rack->r_ctl.rc_timer_exp = qr.timer_timer_exp; 16333 if (tov == 0) { 16334 if (TSTMP_GT(qr.timer_timer_exp, us_cts)) 16335 tov = qr.timer_timer_exp - us_cts; 16336 else 16337 tov = HPTS_TICKS_PER_SLOT; 16338 } 16339 } 16340 rack_log_chg_info(tp, rack, 4, 16341 rack->r_ctl.rc_hpts_flags, 16342 rack->r_ctl.rc_last_output_to, 16343 rack->r_ctl.rc_timer_exp); 16344 if (tov) { 16345 struct hpts_diag diag; 16346 16347 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(tov), 16348 __LINE__, &diag); 16349 rack_log_hpts_diag(rack, us_cts, &diag, &rack->r_ctl.act_rcv_time); 16350 } 16351 } 16352 } 16353 rack_log_rtt_shrinks(rack, us_cts, tp->t_rxtcur, 16354 __LINE__, RACK_RTTS_INIT); 16355 } 16356 return (0); 16357 } 16358 16359 static int 16360 rack_handoff_ok(struct tcpcb *tp) 16361 { 16362 if ((tp->t_state == TCPS_CLOSED) || 16363 (tp->t_state == TCPS_LISTEN)) { 16364 /* Sure no problem though it may not stick */ 16365 return (0); 16366 } 16367 if ((tp->t_state == TCPS_SYN_SENT) || 16368 (tp->t_state == TCPS_SYN_RECEIVED)) { 16369 /* 16370 * We really don't know if you support sack, 16371 * you have to get to ESTAB or beyond to tell. 16372 */ 16373 return (EAGAIN); 16374 } 16375 if ((tp->t_flags & TF_SENTFIN) && ((tp->snd_max - tp->snd_una) > 1)) { 16376 /* 16377 * Rack will only send a FIN after all data is acknowledged. 16378 * So in this case we have more data outstanding. We can't 16379 * switch stacks until either all data and only the FIN 16380 * is left (in which case rack_init() now knows how 16381 * to deal with that) <or> all is acknowledged and we 16382 * are only left with incoming data, though why you 16383 * would want to switch to rack after all data is acknowledged 16384 * I have no idea (rrs)! 16385 */ 16386 return (EAGAIN); 16387 } 16388 if ((tp->t_flags & TF_SACK_PERMIT) || rack_sack_not_required){ 16389 return (0); 16390 } 16391 /* 16392 * If we reach here we don't do SACK on this connection so we can 16393 * never do rack. 16394 */ 16395 return (EINVAL); 16396 } 16397 16398 static void 16399 rack_fini(struct tcpcb *tp, int32_t tcb_is_purged) 16400 { 16401 16402 if (tp->t_fb_ptr) { 16403 uint32_t cnt_free = 0; 16404 struct tcp_rack *rack; 16405 struct rack_sendmap *rsm; 16406 16407 tcp_handle_orphaned_packets(tp); 16408 tp->t_flags &= ~TF_FORCEDATA; 16409 rack = (struct tcp_rack *)tp->t_fb_ptr; 16410 rack_log_pacing_delay_calc(rack, 16411 0, 16412 0, 16413 0, 16414 rack_get_gp_est(rack), /* delRate */ 16415 rack_get_lt_bw(rack), /* rttProp */ 16416 20, __LINE__, NULL, 0); 16417 #ifdef NETFLIX_SHARED_CWND 16418 if (rack->r_ctl.rc_scw) { 16419 uint32_t limit; 16420 16421 if (rack->r_limit_scw) 16422 limit = max(1, rack->r_ctl.rc_lowest_us_rtt); 16423 else 16424 limit = 0; 16425 tcp_shared_cwnd_free_full(tp, rack->r_ctl.rc_scw, 16426 rack->r_ctl.rc_scw_index, 16427 limit); 16428 rack->r_ctl.rc_scw = NULL; 16429 } 16430 #endif 16431 if (rack->r_ctl.fsb.tcp_ip_hdr) { 16432 free(rack->r_ctl.fsb.tcp_ip_hdr, M_TCPFSB); 16433 rack->r_ctl.fsb.tcp_ip_hdr = NULL; 16434 rack->r_ctl.fsb.th = NULL; 16435 } 16436 if (rack->rc_always_pace == 1) { 16437 rack_remove_pacing(rack); 16438 } 16439 /* Clean up any options if they were not applied */ 16440 while (!TAILQ_EMPTY(&rack->r_ctl.opt_list)) { 16441 struct deferred_opt_list *dol; 16442 16443 dol = TAILQ_FIRST(&rack->r_ctl.opt_list); 16444 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next); 16445 free(dol, M_TCPDO); 16446 } 16447 /* rack does not use force data but other stacks may clear it */ 16448 if (rack->r_ctl.crte != NULL) { 16449 tcp_rel_pacing_rate(rack->r_ctl.crte, tp); 16450 rack->rack_hdrw_pacing = 0; 16451 rack->r_ctl.crte = NULL; 16452 } 16453 #ifdef TCP_BLACKBOX 16454 tcp_log_flowend(tp); 16455 #endif 16456 /* 16457 * Lets take a different approach to purging just 16458 * get each one and free it like a cum-ack would and 16459 * not use a foreach loop. 16460 */ 16461 rsm = tqhash_min(rack->r_ctl.tqh); 16462 while (rsm) { 16463 tqhash_remove(rack->r_ctl.tqh, rsm, REMOVE_TYPE_CUMACK); 16464 rack->r_ctl.rc_num_maps_alloced--; 16465 uma_zfree(rack_zone, rsm); 16466 rsm = tqhash_min(rack->r_ctl.tqh); 16467 } 16468 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 16469 while (rsm) { 16470 TAILQ_REMOVE(&rack->r_ctl.rc_free, rsm, r_tnext); 16471 rack->r_ctl.rc_num_maps_alloced--; 16472 rack->rc_free_cnt--; 16473 cnt_free++; 16474 uma_zfree(rack_zone, rsm); 16475 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 16476 } 16477 if (rack->r_ctl.pcm_s != NULL) { 16478 free(rack->r_ctl.pcm_s, M_TCPPCM); 16479 rack->r_ctl.pcm_s = NULL; 16480 rack->r_ctl.pcm_i.cnt_alloc = 0; 16481 rack->r_ctl.pcm_i.cnt = 0; 16482 } 16483 if ((rack->r_ctl.rc_num_maps_alloced > 0) && 16484 (tcp_bblogging_on(tp))) { 16485 union tcp_log_stackspecific log; 16486 struct timeval tv; 16487 16488 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 16489 log.u_bbr.flex8 = 10; 16490 log.u_bbr.flex1 = rack->r_ctl.rc_num_maps_alloced; 16491 log.u_bbr.flex2 = rack->rc_free_cnt; 16492 log.u_bbr.flex3 = cnt_free; 16493 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 16494 rsm = tqhash_min(rack->r_ctl.tqh); 16495 log.u_bbr.delRate = (uint64_t)rsm; 16496 rsm = TAILQ_FIRST(&rack->r_ctl.rc_free); 16497 log.u_bbr.cur_del_rate = (uint64_t)rsm; 16498 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 16499 log.u_bbr.pkt_epoch = __LINE__; 16500 (void)tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, 16501 0, &log, false, NULL, NULL, 0, &tv); 16502 } 16503 KASSERT((rack->r_ctl.rc_num_maps_alloced == 0), 16504 ("rack:%p num_aloc:%u after freeing all?", 16505 rack, 16506 rack->r_ctl.rc_num_maps_alloced)); 16507 rack->rc_free_cnt = 0; 16508 free(rack->r_ctl.tqh, M_TCPFSB); 16509 rack->r_ctl.tqh = NULL; 16510 uma_zfree(rack_pcb_zone, tp->t_fb_ptr); 16511 tp->t_fb_ptr = NULL; 16512 } 16513 /* Make sure snd_nxt is correctly set */ 16514 tp->snd_nxt = tp->snd_max; 16515 } 16516 16517 static void 16518 rack_set_state(struct tcpcb *tp, struct tcp_rack *rack) 16519 { 16520 if ((rack->r_state == TCPS_CLOSED) && (tp->t_state != TCPS_CLOSED)) { 16521 rack->r_is_v6 = (tptoinpcb(tp)->inp_vflag & INP_IPV6) != 0; 16522 } 16523 switch (tp->t_state) { 16524 case TCPS_SYN_SENT: 16525 rack->r_state = TCPS_SYN_SENT; 16526 rack->r_substate = rack_do_syn_sent; 16527 break; 16528 case TCPS_SYN_RECEIVED: 16529 rack->r_state = TCPS_SYN_RECEIVED; 16530 rack->r_substate = rack_do_syn_recv; 16531 break; 16532 case TCPS_ESTABLISHED: 16533 rack_set_pace_segments(tp, rack, __LINE__, NULL); 16534 rack->r_state = TCPS_ESTABLISHED; 16535 rack->r_substate = rack_do_established; 16536 break; 16537 case TCPS_CLOSE_WAIT: 16538 rack->r_state = TCPS_CLOSE_WAIT; 16539 rack->r_substate = rack_do_close_wait; 16540 break; 16541 case TCPS_FIN_WAIT_1: 16542 rack_set_pace_segments(tp, rack, __LINE__, NULL); 16543 rack->r_state = TCPS_FIN_WAIT_1; 16544 rack->r_substate = rack_do_fin_wait_1; 16545 break; 16546 case TCPS_CLOSING: 16547 rack_set_pace_segments(tp, rack, __LINE__, NULL); 16548 rack->r_state = TCPS_CLOSING; 16549 rack->r_substate = rack_do_closing; 16550 break; 16551 case TCPS_LAST_ACK: 16552 rack_set_pace_segments(tp, rack, __LINE__, NULL); 16553 rack->r_state = TCPS_LAST_ACK; 16554 rack->r_substate = rack_do_lastack; 16555 break; 16556 case TCPS_FIN_WAIT_2: 16557 rack->r_state = TCPS_FIN_WAIT_2; 16558 rack->r_substate = rack_do_fin_wait_2; 16559 break; 16560 case TCPS_LISTEN: 16561 case TCPS_CLOSED: 16562 case TCPS_TIME_WAIT: 16563 default: 16564 break; 16565 }; 16566 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 16567 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP; 16568 16569 } 16570 16571 static void 16572 rack_timer_audit(struct tcpcb *tp, struct tcp_rack *rack, struct sockbuf *sb) 16573 { 16574 /* 16575 * We received an ack, and then did not 16576 * call send or were bounced out due to the 16577 * hpts was running. Now a timer is up as well, is 16578 * it the right timer? 16579 */ 16580 struct rack_sendmap *rsm; 16581 int tmr_up; 16582 16583 tmr_up = rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 16584 if (tcp_in_hpts(rack->rc_tp) == 0) { 16585 /* 16586 * Ok we probably need some timer up, but no 16587 * matter what the mask we are not in hpts. We 16588 * may have received an old ack and thus did nothing. 16589 */ 16590 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 16591 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 16592 return; 16593 } 16594 if (rack->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 16595 return; 16596 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 16597 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 16598 (tmr_up == PACE_TMR_RXT)) { 16599 /* Should be an RXT */ 16600 return; 16601 } 16602 if (rsm == NULL) { 16603 /* Nothing outstanding? */ 16604 if (tp->t_flags & TF_DELACK) { 16605 if (tmr_up == PACE_TMR_DELACK) 16606 /* We are supposed to have delayed ack up and we do */ 16607 return; 16608 } else if (sbavail(&tptosocket(tp)->so_snd) && (tmr_up == PACE_TMR_RXT)) { 16609 /* 16610 * if we hit enobufs then we would expect the possibility 16611 * of nothing outstanding and the RXT up (and the hptsi timer). 16612 */ 16613 return; 16614 } else if (((V_tcp_always_keepalive || 16615 rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 16616 (tp->t_state <= TCPS_CLOSING)) && 16617 (tmr_up == PACE_TMR_KEEP) && 16618 (tp->snd_max == tp->snd_una)) { 16619 /* We should have keep alive up and we do */ 16620 return; 16621 } 16622 } 16623 if (SEQ_GT(tp->snd_max, tp->snd_una) && 16624 ((tmr_up == PACE_TMR_TLP) || 16625 (tmr_up == PACE_TMR_RACK) || 16626 (tmr_up == PACE_TMR_RXT))) { 16627 /* 16628 * Either a Rack, TLP or RXT is fine if we 16629 * have outstanding data. 16630 */ 16631 return; 16632 } else if (tmr_up == PACE_TMR_DELACK) { 16633 /* 16634 * If the delayed ack was going to go off 16635 * before the rtx/tlp/rack timer were going to 16636 * expire, then that would be the timer in control. 16637 * Note we don't check the time here trusting the 16638 * code is correct. 16639 */ 16640 return; 16641 } 16642 /* 16643 * Ok the timer originally started is not what we want now. 16644 * We will force the hpts to be stopped if any, and restart 16645 * with the slot set to what was in the saved slot. 16646 */ 16647 if (tcp_in_hpts(rack->rc_tp)) { 16648 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 16649 uint32_t us_cts; 16650 16651 us_cts = tcp_get_usecs(NULL); 16652 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) { 16653 rack->r_early = 1; 16654 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts); 16655 } 16656 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 16657 } 16658 tcp_hpts_remove(rack->rc_tp); 16659 } 16660 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 16661 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 16662 } 16663 16664 16665 static void 16666 rack_do_win_updates(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tiwin, uint32_t seq, uint32_t ack, uint32_t cts) 16667 { 16668 if ((SEQ_LT(tp->snd_wl1, seq) || 16669 (tp->snd_wl1 == seq && (SEQ_LT(tp->snd_wl2, ack) || 16670 (tp->snd_wl2 == ack && tiwin > tp->snd_wnd))))) { 16671 /* keep track of pure window updates */ 16672 if ((tp->snd_wl2 == ack) && (tiwin > tp->snd_wnd)) 16673 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 16674 tp->snd_wnd = tiwin; 16675 rack_validate_fo_sendwin_up(tp, rack); 16676 tp->snd_wl1 = seq; 16677 tp->snd_wl2 = ack; 16678 if (tp->snd_wnd > tp->max_sndwnd) 16679 tp->max_sndwnd = tp->snd_wnd; 16680 rack->r_wanted_output = 1; 16681 } else if ((tp->snd_wl2 == ack) && (tiwin < tp->snd_wnd)) { 16682 tp->snd_wnd = tiwin; 16683 rack_validate_fo_sendwin_up(tp, rack); 16684 tp->snd_wl1 = seq; 16685 tp->snd_wl2 = ack; 16686 } else { 16687 /* Not a valid win update */ 16688 return; 16689 } 16690 if (tp->snd_wnd > tp->max_sndwnd) 16691 tp->max_sndwnd = tp->snd_wnd; 16692 /* Do we exit persists? */ 16693 if ((rack->rc_in_persist != 0) && 16694 (tp->snd_wnd >= min((rack->r_ctl.rc_high_rwnd/2), 16695 rack->r_ctl.rc_pace_min_segs))) { 16696 rack_exit_persist(tp, rack, cts); 16697 } 16698 /* Do we enter persists? */ 16699 if ((rack->rc_in_persist == 0) && 16700 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), rack->r_ctl.rc_pace_min_segs)) && 16701 TCPS_HAVEESTABLISHED(tp->t_state) && 16702 ((tp->snd_max == tp->snd_una) || rack->rc_has_collapsed) && 16703 sbavail(&tptosocket(tp)->so_snd) && 16704 (sbavail(&tptosocket(tp)->so_snd) > tp->snd_wnd)) { 16705 /* 16706 * Here the rwnd is less than 16707 * the pacing size, we are established, 16708 * nothing is outstanding, and there is 16709 * data to send. Enter persists. 16710 */ 16711 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, ack); 16712 } 16713 } 16714 16715 static void 16716 rack_log_input_packet(struct tcpcb *tp, struct tcp_rack *rack, struct tcp_ackent *ae, int ackval, uint32_t high_seq) 16717 { 16718 16719 if (tcp_bblogging_on(rack->rc_tp)) { 16720 struct inpcb *inp = tptoinpcb(tp); 16721 union tcp_log_stackspecific log; 16722 struct timeval ltv; 16723 char tcp_hdr_buf[60]; 16724 struct tcphdr *th; 16725 struct timespec ts; 16726 uint32_t orig_snd_una; 16727 uint8_t xx = 0; 16728 16729 #ifdef TCP_REQUEST_TRK 16730 struct tcp_sendfile_track *tcp_req; 16731 16732 if (SEQ_GT(ae->ack, tp->snd_una)) { 16733 tcp_req = tcp_req_find_req_for_seq(tp, (ae->ack-1)); 16734 } else { 16735 tcp_req = tcp_req_find_req_for_seq(tp, ae->ack); 16736 } 16737 #endif 16738 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 16739 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 16740 if (rack->rack_no_prr == 0) 16741 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 16742 else 16743 log.u_bbr.flex1 = 0; 16744 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 16745 log.u_bbr.use_lt_bw <<= 1; 16746 log.u_bbr.use_lt_bw |= rack->r_might_revert; 16747 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced; 16748 log.u_bbr.bbr_state = rack->rc_free_cnt; 16749 log.u_bbr.inflight = ctf_flight_size(tp, rack->r_ctl.rc_sacked); 16750 log.u_bbr.pkts_out = tp->t_maxseg; 16751 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 16752 log.u_bbr.flex7 = 1; 16753 log.u_bbr.lost = ae->flags; 16754 log.u_bbr.cwnd_gain = ackval; 16755 log.u_bbr.pacing_gain = 0x2; 16756 if (ae->flags & TSTMP_HDWR) { 16757 /* Record the hardware timestamp if present */ 16758 log.u_bbr.flex3 = M_TSTMP; 16759 ts.tv_sec = ae->timestamp / 1000000000; 16760 ts.tv_nsec = ae->timestamp % 1000000000; 16761 ltv.tv_sec = ts.tv_sec; 16762 ltv.tv_usec = ts.tv_nsec / 1000; 16763 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v); 16764 } else if (ae->flags & TSTMP_LRO) { 16765 /* Record the LRO the arrival timestamp */ 16766 log.u_bbr.flex3 = M_TSTMP_LRO; 16767 ts.tv_sec = ae->timestamp / 1000000000; 16768 ts.tv_nsec = ae->timestamp % 1000000000; 16769 ltv.tv_sec = ts.tv_sec; 16770 ltv.tv_usec = ts.tv_nsec / 1000; 16771 log.u_bbr.flex5 = tcp_tv_to_usectick(<v); 16772 } 16773 log.u_bbr.timeStamp = tcp_get_usecs(<v); 16774 /* Log the rcv time */ 16775 log.u_bbr.delRate = ae->timestamp; 16776 #ifdef TCP_REQUEST_TRK 16777 log.u_bbr.applimited = tp->t_tcpreq_closed; 16778 log.u_bbr.applimited <<= 8; 16779 log.u_bbr.applimited |= tp->t_tcpreq_open; 16780 log.u_bbr.applimited <<= 8; 16781 log.u_bbr.applimited |= tp->t_tcpreq_req; 16782 if (tcp_req) { 16783 /* Copy out any client req info */ 16784 /* seconds */ 16785 log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC); 16786 /* useconds */ 16787 log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC); 16788 log.u_bbr.rttProp = tcp_req->timestamp; 16789 log.u_bbr.cur_del_rate = tcp_req->start; 16790 if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) { 16791 log.u_bbr.flex8 |= 1; 16792 } else { 16793 log.u_bbr.flex8 |= 2; 16794 log.u_bbr.bw_inuse = tcp_req->end; 16795 } 16796 log.u_bbr.flex6 = tcp_req->start_seq; 16797 if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) { 16798 log.u_bbr.flex8 |= 4; 16799 log.u_bbr.epoch = tcp_req->end_seq; 16800 } 16801 } 16802 #endif 16803 memset(tcp_hdr_buf, 0, sizeof(tcp_hdr_buf)); 16804 th = (struct tcphdr *)tcp_hdr_buf; 16805 th->th_seq = ae->seq; 16806 th->th_ack = ae->ack; 16807 th->th_win = ae->win; 16808 /* Now fill in the ports */ 16809 th->th_sport = inp->inp_fport; 16810 th->th_dport = inp->inp_lport; 16811 tcp_set_flags(th, ae->flags); 16812 /* Now do we have a timestamp option? */ 16813 if (ae->flags & HAS_TSTMP) { 16814 u_char *cp; 16815 uint32_t val; 16816 16817 th->th_off = ((sizeof(struct tcphdr) + TCPOLEN_TSTAMP_APPA) >> 2); 16818 cp = (u_char *)(th + 1); 16819 *cp = TCPOPT_NOP; 16820 cp++; 16821 *cp = TCPOPT_NOP; 16822 cp++; 16823 *cp = TCPOPT_TIMESTAMP; 16824 cp++; 16825 *cp = TCPOLEN_TIMESTAMP; 16826 cp++; 16827 val = htonl(ae->ts_value); 16828 bcopy((char *)&val, 16829 (char *)cp, sizeof(uint32_t)); 16830 val = htonl(ae->ts_echo); 16831 bcopy((char *)&val, 16832 (char *)(cp + 4), sizeof(uint32_t)); 16833 } else 16834 th->th_off = (sizeof(struct tcphdr) >> 2); 16835 16836 /* 16837 * For sane logging we need to play a little trick. 16838 * If the ack were fully processed we would have moved 16839 * snd_una to high_seq, but since compressed acks are 16840 * processed in two phases, at this point (logging) snd_una 16841 * won't be advanced. So we would see multiple acks showing 16842 * the advancement. We can prevent that by "pretending" that 16843 * snd_una was advanced and then un-advancing it so that the 16844 * logging code has the right value for tlb_snd_una. 16845 */ 16846 if (tp->snd_una != high_seq) { 16847 orig_snd_una = tp->snd_una; 16848 tp->snd_una = high_seq; 16849 xx = 1; 16850 } else 16851 xx = 0; 16852 TCP_LOG_EVENTP(tp, th, 16853 &tptosocket(tp)->so_rcv, 16854 &tptosocket(tp)->so_snd, TCP_LOG_IN, 0, 16855 0, &log, true, <v); 16856 if (xx) { 16857 tp->snd_una = orig_snd_una; 16858 } 16859 } 16860 16861 } 16862 16863 static void 16864 rack_handle_probe_response(struct tcp_rack *rack, uint32_t tiwin, uint32_t us_cts) 16865 { 16866 uint32_t us_rtt; 16867 /* 16868 * A persist or keep-alive was forced out, update our 16869 * min rtt time. Note now worry about lost responses. 16870 * When a subsequent keep-alive or persist times out 16871 * and forced_ack is still on, then the last probe 16872 * was not responded to. In such cases we have a 16873 * sysctl that controls the behavior. Either we apply 16874 * the rtt but with reduced confidence (0). Or we just 16875 * plain don't apply the rtt estimate. Having data flow 16876 * will clear the probe_not_answered flag i.e. cum-ack 16877 * move forward <or> exiting and reentering persists. 16878 */ 16879 16880 rack->forced_ack = 0; 16881 rack->rc_tp->t_rxtshift = 0; 16882 if ((rack->rc_in_persist && 16883 (tiwin == rack->rc_tp->snd_wnd)) || 16884 (rack->rc_in_persist == 0)) { 16885 /* 16886 * In persists only apply the RTT update if this is 16887 * a response to our window probe. And that 16888 * means the rwnd sent must match the current 16889 * snd_wnd. If it does not, then we got a 16890 * window update ack instead. For keepalive 16891 * we allow the answer no matter what the window. 16892 * 16893 * Note that if the probe_not_answered is set then 16894 * the forced_ack_ts is the oldest one i.e. the first 16895 * probe sent that might have been lost. This assures 16896 * us that if we do calculate an RTT it is longer not 16897 * some short thing. 16898 */ 16899 if (rack->rc_in_persist) 16900 counter_u64_add(rack_persists_acks, 1); 16901 us_rtt = us_cts - rack->r_ctl.forced_ack_ts; 16902 if (us_rtt == 0) 16903 us_rtt = 1; 16904 if (rack->probe_not_answered == 0) { 16905 rack_apply_updated_usrtt(rack, us_rtt, us_cts); 16906 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 3, NULL, 1); 16907 } else { 16908 /* We have a retransmitted probe here too */ 16909 if (rack_apply_rtt_with_reduced_conf) { 16910 rack_apply_updated_usrtt(rack, us_rtt, us_cts); 16911 tcp_rack_xmit_timer(rack, us_rtt, 0, us_rtt, 0, NULL, 1); 16912 } 16913 } 16914 } 16915 } 16916 16917 static void 16918 rack_new_round_starts(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq) 16919 { 16920 /* 16921 * The next send has occurred mark the end of the round 16922 * as when that data gets acknowledged. We can 16923 * also do common things we might need to do when 16924 * a round begins. 16925 */ 16926 rack->r_ctl.roundends = tp->snd_max; 16927 rack->rc_new_rnd_needed = 0; 16928 rack_log_hystart_event(rack, tp->snd_max, 4); 16929 } 16930 16931 16932 static void 16933 rack_log_pcm(struct tcp_rack *rack, uint8_t mod, uint32_t flex1, uint32_t flex2, 16934 uint32_t flex3) 16935 { 16936 if (tcp_bblogging_on(rack->rc_tp)) { 16937 union tcp_log_stackspecific log; 16938 struct timeval tv; 16939 16940 (void)tcp_get_usecs(&tv); 16941 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 16942 log.u_bbr.timeStamp = tcp_tv_to_usectick(&tv); 16943 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 16944 log.u_bbr.flex8 = mod; 16945 log.u_bbr.flex1 = flex1; 16946 log.u_bbr.flex2 = flex2; 16947 log.u_bbr.flex3 = flex3; 16948 log.u_bbr.flex4 = rack_pcm_every_n_rounds; 16949 log.u_bbr.flex5 = rack->r_ctl.pcm_idle_rounds; 16950 log.u_bbr.bbr_substate = rack->pcm_needed; 16951 log.u_bbr.bbr_substate <<= 1; 16952 log.u_bbr.bbr_substate |= rack->pcm_in_progress; 16953 log.u_bbr.bbr_substate <<= 1; 16954 log.u_bbr.bbr_substate |= rack->pcm_enabled; /* bits are NIE for Needed, Inprogress, Enabled */ 16955 (void)tcp_log_event(rack->rc_tp, NULL, NULL, NULL, TCP_PCM_MEASURE, ERRNO_UNK, 16956 0, &log, false, NULL, NULL, 0, &tv); 16957 } 16958 } 16959 16960 static void 16961 rack_new_round_setup(struct tcpcb *tp, struct tcp_rack *rack, uint32_t high_seq) 16962 { 16963 /* 16964 * The round (current_round) has ended. We now 16965 * setup for the next round by incrementing the 16966 * round numnber and doing any round specific 16967 * things. 16968 */ 16969 rack_log_hystart_event(rack, high_seq, 21); 16970 rack->r_ctl.current_round++; 16971 /* New round (current_round) begins at next send */ 16972 rack->rc_new_rnd_needed = 1; 16973 if ((rack->pcm_enabled == 1) && 16974 (rack->pcm_needed == 0) && 16975 (rack->pcm_in_progress == 0)) { 16976 /* 16977 * If we have enabled PCM, then we need to 16978 * check if the round has adanced to the state 16979 * where one is required. 16980 */ 16981 int rnds; 16982 16983 rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round; 16984 if ((rnds + rack->r_ctl.pcm_idle_rounds) >= rack_pcm_every_n_rounds) { 16985 rack->pcm_needed = 1; 16986 rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round ); 16987 } else if (rack_verbose_logging) { 16988 rack_log_pcm(rack, 3, rack->r_ctl.last_pcm_round, rack_pcm_every_n_rounds, rack->r_ctl.current_round ); 16989 } 16990 } 16991 if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) { 16992 /* We have hystart enabled send the round info in */ 16993 if (CC_ALGO(tp)->newround != NULL) { 16994 CC_ALGO(tp)->newround(&tp->t_ccv, rack->r_ctl.current_round); 16995 } 16996 } 16997 /* 16998 * For DGP an initial startup check. We want to validate 16999 * that we are not just pushing on slow-start and just 17000 * not gaining.. i.e. filling buffers without getting any 17001 * boost in b/w during the inital slow-start. 17002 */ 17003 if (rack->dgp_on && 17004 (rack->rc_initial_ss_comp == 0) && 17005 (tp->snd_cwnd < tp->snd_ssthresh) && 17006 (rack->r_ctl.num_measurements >= RACK_REQ_AVG) && 17007 (rack->r_ctl.gp_rnd_thresh > 0) && 17008 ((rack->r_ctl.current_round - rack->r_ctl.last_rnd_of_gp_rise) >= rack->r_ctl.gp_rnd_thresh)) { 17009 17010 /* 17011 * We are in the initial SS and we have hd rack_rnd_cnt_req rounds(def:5) where 17012 * we have not gained the required amount in the gp_est (120.0% aka 1200). Lets 17013 * exit SS. 17014 * 17015 * Pick up the flight size now as we enter slowstart (not the 17016 * cwnd which may be inflated). 17017 */ 17018 rack->rc_initial_ss_comp = 1; 17019 17020 if (tcp_bblogging_on(rack->rc_tp)) { 17021 union tcp_log_stackspecific log; 17022 struct timeval tv; 17023 17024 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 17025 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 17026 log.u_bbr.flex1 = rack->r_ctl.current_round; 17027 log.u_bbr.flex2 = rack->r_ctl.last_rnd_of_gp_rise; 17028 log.u_bbr.flex3 = rack->r_ctl.gp_rnd_thresh; 17029 log.u_bbr.flex4 = rack->r_ctl.gate_to_fs; 17030 log.u_bbr.flex5 = rack->r_ctl.ss_hi_fs; 17031 log.u_bbr.flex8 = 40; 17032 (void)tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_CWND, 0, 17033 0, &log, false, NULL, __func__, __LINE__,&tv); 17034 } 17035 if ((rack->r_ctl.gate_to_fs == 1) && 17036 (tp->snd_cwnd > rack->r_ctl.ss_hi_fs)) { 17037 tp->snd_cwnd = rack->r_ctl.ss_hi_fs; 17038 } 17039 tp->snd_ssthresh = tp->snd_cwnd - 1; 17040 /* Turn off any fast output running */ 17041 rack->r_fast_output = 0; 17042 } 17043 } 17044 17045 static int 17046 rack_do_compressed_ack_processing(struct tcpcb *tp, struct socket *so, struct mbuf *m, int nxt_pkt, struct timeval *tv) 17047 { 17048 /* 17049 * Handle a "special" compressed ack mbuf. Each incoming 17050 * ack has only four possible dispositions: 17051 * 17052 * A) It moves the cum-ack forward 17053 * B) It is behind the cum-ack. 17054 * C) It is a window-update ack. 17055 * D) It is a dup-ack. 17056 * 17057 * Note that we can have between 1 -> TCP_COMP_ACK_ENTRIES 17058 * in the incoming mbuf. We also need to still pay attention 17059 * to nxt_pkt since there may be another packet after this 17060 * one. 17061 */ 17062 #ifdef TCP_ACCOUNTING 17063 uint64_t ts_val; 17064 uint64_t rdstc; 17065 #endif 17066 int segsiz; 17067 struct timespec ts; 17068 struct tcp_rack *rack; 17069 struct tcp_ackent *ae; 17070 uint32_t tiwin, ms_cts, cts, acked, acked_amount, high_seq, win_seq, the_win, win_upd_ack; 17071 int cnt, i, did_out, ourfinisacked = 0; 17072 struct tcpopt to_holder, *to = NULL; 17073 #ifdef TCP_ACCOUNTING 17074 int win_up_req = 0; 17075 #endif 17076 int nsegs = 0; 17077 int under_pacing = 0; 17078 int post_recovery = 0; 17079 #ifdef TCP_ACCOUNTING 17080 sched_pin(); 17081 #endif 17082 rack = (struct tcp_rack *)tp->t_fb_ptr; 17083 if (rack->gp_ready && 17084 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) 17085 under_pacing = 1; 17086 17087 if (rack->r_state != tp->t_state) 17088 rack_set_state(tp, rack); 17089 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 17090 (tp->t_flags & TF_GPUTINPROG)) { 17091 /* 17092 * We have a goodput in progress 17093 * and we have entered a late state. 17094 * Do we have enough data in the sb 17095 * to handle the GPUT request? 17096 */ 17097 uint32_t bytes; 17098 17099 bytes = tp->gput_ack - tp->gput_seq; 17100 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 17101 bytes += tp->gput_seq - tp->snd_una; 17102 if (bytes > sbavail(&tptosocket(tp)->so_snd)) { 17103 /* 17104 * There are not enough bytes in the socket 17105 * buffer that have been sent to cover this 17106 * measurement. Cancel it. 17107 */ 17108 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 17109 rack->r_ctl.rc_gp_srtt /*flex1*/, 17110 tp->gput_seq, 17111 0, 0, 18, __LINE__, NULL, 0); 17112 tp->t_flags &= ~TF_GPUTINPROG; 17113 } 17114 } 17115 to = &to_holder; 17116 to->to_flags = 0; 17117 KASSERT((m->m_len >= sizeof(struct tcp_ackent)), 17118 ("tp:%p m_cmpack:%p with invalid len:%u", tp, m, m->m_len)); 17119 cnt = m->m_len / sizeof(struct tcp_ackent); 17120 counter_u64_add(rack_multi_single_eq, cnt); 17121 high_seq = tp->snd_una; 17122 the_win = tp->snd_wnd; 17123 win_seq = tp->snd_wl1; 17124 win_upd_ack = tp->snd_wl2; 17125 cts = tcp_tv_to_usectick(tv); 17126 ms_cts = tcp_tv_to_mssectick(tv); 17127 rack->r_ctl.rc_rcvtime = cts; 17128 segsiz = ctf_fixed_maxseg(tp); 17129 if ((rack->rc_gp_dyn_mul) && 17130 (rack->use_fixed_rate == 0) && 17131 (rack->rc_always_pace)) { 17132 /* Check in on probertt */ 17133 rack_check_probe_rtt(rack, cts); 17134 } 17135 for (i = 0; i < cnt; i++) { 17136 #ifdef TCP_ACCOUNTING 17137 ts_val = get_cyclecount(); 17138 #endif 17139 rack_clear_rate_sample(rack); 17140 ae = ((mtod(m, struct tcp_ackent *)) + i); 17141 if (ae->flags & TH_FIN) 17142 rack_log_pacing_delay_calc(rack, 17143 0, 17144 0, 17145 0, 17146 rack_get_gp_est(rack), /* delRate */ 17147 rack_get_lt_bw(rack), /* rttProp */ 17148 20, __LINE__, NULL, 0); 17149 /* Setup the window */ 17150 tiwin = ae->win << tp->snd_scale; 17151 if (tiwin > rack->r_ctl.rc_high_rwnd) 17152 rack->r_ctl.rc_high_rwnd = tiwin; 17153 /* figure out the type of ack */ 17154 if (SEQ_LT(ae->ack, high_seq)) { 17155 /* Case B*/ 17156 ae->ack_val_set = ACK_BEHIND; 17157 } else if (SEQ_GT(ae->ack, high_seq)) { 17158 /* Case A */ 17159 ae->ack_val_set = ACK_CUMACK; 17160 } else if ((tiwin == the_win) && (rack->rc_in_persist == 0)){ 17161 /* Case D */ 17162 ae->ack_val_set = ACK_DUPACK; 17163 } else { 17164 /* Case C */ 17165 ae->ack_val_set = ACK_RWND; 17166 } 17167 if (rack->sack_attack_disable > 0) { 17168 rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__); 17169 rack->r_ctl.ack_during_sd++; 17170 } 17171 rack_log_input_packet(tp, rack, ae, ae->ack_val_set, high_seq); 17172 /* Validate timestamp */ 17173 if (ae->flags & HAS_TSTMP) { 17174 /* Setup for a timestamp */ 17175 to->to_flags = TOF_TS; 17176 ae->ts_echo -= tp->ts_offset; 17177 to->to_tsecr = ae->ts_echo; 17178 to->to_tsval = ae->ts_value; 17179 /* 17180 * If echoed timestamp is later than the current time, fall back to 17181 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 17182 * were used when this connection was established. 17183 */ 17184 if (TSTMP_GT(ae->ts_echo, ms_cts)) 17185 to->to_tsecr = 0; 17186 if (tp->ts_recent && 17187 TSTMP_LT(ae->ts_value, tp->ts_recent)) { 17188 if (ctf_ts_check_ac(tp, (ae->flags & 0xff))) { 17189 #ifdef TCP_ACCOUNTING 17190 rdstc = get_cyclecount(); 17191 if (rdstc > ts_val) { 17192 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17193 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val); 17194 } 17195 } 17196 #endif 17197 continue; 17198 } 17199 } 17200 if (SEQ_LEQ(ae->seq, tp->last_ack_sent) && 17201 SEQ_LEQ(tp->last_ack_sent, ae->seq)) { 17202 tp->ts_recent_age = tcp_ts_getticks(); 17203 tp->ts_recent = ae->ts_value; 17204 } 17205 } else { 17206 /* Setup for a no options */ 17207 to->to_flags = 0; 17208 } 17209 /* Update the rcv time and perform idle reduction possibly */ 17210 if (tp->t_idle_reduce && 17211 (tp->snd_max == tp->snd_una) && 17212 (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) { 17213 counter_u64_add(rack_input_idle_reduces, 1); 17214 rack_cc_after_idle(rack, tp); 17215 } 17216 tp->t_rcvtime = ticks; 17217 /* Now what about ECN of a chain of pure ACKs? */ 17218 if (tcp_ecn_input_segment(tp, ae->flags, 0, 17219 tcp_packets_this_ack(tp, ae->ack), 17220 ae->codepoint)) 17221 rack_cong_signal(tp, CC_ECN, ae->ack, __LINE__); 17222 #ifdef TCP_ACCOUNTING 17223 /* Count for the specific type of ack in */ 17224 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17225 tp->tcp_cnt_counters[ae->ack_val_set]++; 17226 } 17227 #endif 17228 /* 17229 * Note how we could move up these in the determination 17230 * above, but we don't so that way the timestamp checks (and ECN) 17231 * is done first before we do any processing on the ACK. 17232 * The non-compressed path through the code has this 17233 * weakness (noted by @jtl) that it actually does some 17234 * processing before verifying the timestamp information. 17235 * We don't take that path here which is why we set 17236 * the ack_val_set first, do the timestamp and ecn 17237 * processing, and then look at what we have setup. 17238 */ 17239 if (ae->ack_val_set == ACK_BEHIND) { 17240 /* 17241 * Case B flag reordering, if window is not closed 17242 * or it could be a keep-alive or persists 17243 */ 17244 if (SEQ_LT(ae->ack, tp->snd_una) && (sbspace(&so->so_rcv) > segsiz)) { 17245 rack->r_ctl.rc_reorder_ts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 17246 if (rack->r_ctl.rc_reorder_ts == 0) 17247 rack->r_ctl.rc_reorder_ts = 1; 17248 } 17249 } else if (ae->ack_val_set == ACK_DUPACK) { 17250 /* Case D */ 17251 rack_strike_dupack(rack, ae->ack); 17252 } else if (ae->ack_val_set == ACK_RWND) { 17253 /* Case C */ 17254 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) { 17255 ts.tv_sec = ae->timestamp / 1000000000; 17256 ts.tv_nsec = ae->timestamp % 1000000000; 17257 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec; 17258 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000; 17259 } else { 17260 rack->r_ctl.act_rcv_time = *tv; 17261 } 17262 if (rack->forced_ack) { 17263 rack_handle_probe_response(rack, tiwin, 17264 tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time)); 17265 } 17266 #ifdef TCP_ACCOUNTING 17267 win_up_req = 1; 17268 #endif 17269 win_upd_ack = ae->ack; 17270 win_seq = ae->seq; 17271 the_win = tiwin; 17272 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts); 17273 } else { 17274 /* Case A */ 17275 if (SEQ_GT(ae->ack, tp->snd_max)) { 17276 /* 17277 * We just send an ack since the incoming 17278 * ack is beyond the largest seq we sent. 17279 */ 17280 if ((tp->t_flags & TF_ACKNOW) == 0) { 17281 ctf_ack_war_checks(tp, &rack->r_ctl.challenge_ack_ts, &rack->r_ctl.challenge_ack_cnt); 17282 if (tp->t_flags && TF_ACKNOW) 17283 rack->r_wanted_output = 1; 17284 } 17285 } else { 17286 nsegs++; 17287 /* If the window changed setup to update */ 17288 if (tiwin != tp->snd_wnd) { 17289 win_upd_ack = ae->ack; 17290 win_seq = ae->seq; 17291 the_win = tiwin; 17292 rack_do_win_updates(tp, rack, the_win, win_seq, win_upd_ack, cts); 17293 } 17294 #ifdef TCP_ACCOUNTING 17295 /* Account for the acks */ 17296 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17297 tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((ae->ack - high_seq) + segsiz - 1) / segsiz); 17298 } 17299 #endif 17300 high_seq = ae->ack; 17301 /* Setup our act_rcv_time */ 17302 if ((ae->flags & TSTMP_LRO) || (ae->flags & TSTMP_HDWR)) { 17303 ts.tv_sec = ae->timestamp / 1000000000; 17304 ts.tv_nsec = ae->timestamp % 1000000000; 17305 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec; 17306 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000; 17307 } else { 17308 rack->r_ctl.act_rcv_time = *tv; 17309 } 17310 rack_process_to_cumack(tp, rack, ae->ack, cts, to, 17311 tcp_tv_to_lusectick(&rack->r_ctl.act_rcv_time)); 17312 #ifdef TCP_REQUEST_TRK 17313 rack_req_check_for_comp(rack, high_seq); 17314 #endif 17315 if (rack->rc_dsack_round_seen) { 17316 /* Is the dsack round over? */ 17317 if (SEQ_GEQ(ae->ack, rack->r_ctl.dsack_round_end)) { 17318 /* Yes it is */ 17319 rack->rc_dsack_round_seen = 0; 17320 rack_log_dsack_event(rack, 3, __LINE__, 0, 0); 17321 } 17322 } 17323 } 17324 } 17325 /* And lets be sure to commit the rtt measurements for this ack */ 17326 tcp_rack_xmit_timer_commit(rack, tp); 17327 #ifdef TCP_ACCOUNTING 17328 rdstc = get_cyclecount(); 17329 if (rdstc > ts_val) { 17330 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17331 tp->tcp_proc_time[ae->ack_val_set] += (rdstc - ts_val); 17332 if (ae->ack_val_set == ACK_CUMACK) 17333 tp->tcp_proc_time[CYC_HANDLE_MAP] += (rdstc - ts_val); 17334 } 17335 } 17336 #endif 17337 } 17338 #ifdef TCP_ACCOUNTING 17339 ts_val = get_cyclecount(); 17340 #endif 17341 /* Tend to any collapsed window */ 17342 if (SEQ_GT(tp->snd_max, high_seq) && (tp->snd_wnd < (tp->snd_max - high_seq))) { 17343 /* The peer collapsed the window */ 17344 rack_collapsed_window(rack, (tp->snd_max - high_seq), high_seq, __LINE__); 17345 } else if (rack->rc_has_collapsed) 17346 rack_un_collapse_window(rack, __LINE__); 17347 if ((rack->r_collapse_point_valid) && 17348 (SEQ_GT(high_seq, rack->r_ctl.high_collapse_point))) 17349 rack->r_collapse_point_valid = 0; 17350 acked_amount = acked = (high_seq - tp->snd_una); 17351 if (acked) { 17352 /* 17353 * The draft (v3) calls for us to use SEQ_GEQ, but that 17354 * causes issues when we are just going app limited. Lets 17355 * instead use SEQ_GT <or> where its equal but more data 17356 * is outstanding. 17357 * 17358 * Also make sure we are on the last ack of a series. We 17359 * have to have all the ack's processed in queue to know 17360 * if there is something left outstanding. 17361 * 17362 */ 17363 if (SEQ_GEQ(high_seq, rack->r_ctl.roundends) && 17364 (rack->rc_new_rnd_needed == 0) && 17365 (nxt_pkt == 0)) { 17366 /* 17367 * We have crossed into a new round with 17368 * this th_ack value. 17369 */ 17370 rack_new_round_setup(tp, rack, high_seq); 17371 } 17372 /* 17373 * Clear the probe not answered flag 17374 * since cum-ack moved forward. 17375 */ 17376 rack->probe_not_answered = 0; 17377 if (rack->sack_attack_disable == 0) 17378 rack_do_decay(rack); 17379 if (acked >= segsiz) { 17380 /* 17381 * You only get credit for 17382 * MSS and greater (and you get extra 17383 * credit for larger cum-ack moves). 17384 */ 17385 int ac; 17386 17387 ac = acked / segsiz; 17388 rack->r_ctl.ack_count += ac; 17389 counter_u64_add(rack_ack_total, ac); 17390 } 17391 if (rack->r_ctl.ack_count > 0xfff00000) { 17392 /* 17393 * reduce the number to keep us under 17394 * a uint32_t. 17395 */ 17396 rack->r_ctl.ack_count /= 2; 17397 rack->r_ctl.sack_count /= 2; 17398 } 17399 if (tp->t_flags & TF_NEEDSYN) { 17400 /* 17401 * T/TCP: Connection was half-synchronized, and our SYN has 17402 * been ACK'd (so connection is now fully synchronized). Go 17403 * to non-starred state, increment snd_una for ACK of SYN, 17404 * and check if we can do window scaling. 17405 */ 17406 tp->t_flags &= ~TF_NEEDSYN; 17407 tp->snd_una++; 17408 acked_amount = acked = (high_seq - tp->snd_una); 17409 } 17410 if (acked > sbavail(&so->so_snd)) 17411 acked_amount = sbavail(&so->so_snd); 17412 #ifdef TCP_SAD_DETECTION 17413 /* 17414 * We only care on a cum-ack move if we are in a sack-disabled 17415 * state. We have already added in to the ack_count, and we never 17416 * would disable on a cum-ack move, so we only care to do the 17417 * detection if it may "undo" it, i.e. we were in disabled already. 17418 */ 17419 if (rack->sack_attack_disable) 17420 rack_do_detection(tp, rack, acked_amount, segsiz); 17421 #endif 17422 if (IN_FASTRECOVERY(tp->t_flags) && 17423 (rack->rack_no_prr == 0)) 17424 rack_update_prr(tp, rack, acked_amount, high_seq); 17425 if (IN_RECOVERY(tp->t_flags)) { 17426 if (SEQ_LT(high_seq, tp->snd_recover) && 17427 (SEQ_LT(high_seq, tp->snd_max))) { 17428 tcp_rack_partialack(tp); 17429 } else { 17430 rack_post_recovery(tp, high_seq); 17431 post_recovery = 1; 17432 } 17433 } else if ((rack->rto_from_rec == 1) && 17434 SEQ_GEQ(high_seq, tp->snd_recover)) { 17435 /* 17436 * We were in recovery, hit a rxt timeout 17437 * and never re-entered recovery. The timeout(s) 17438 * made up all the lost data. In such a case 17439 * we need to clear the rto_from_rec flag. 17440 */ 17441 rack->rto_from_rec = 0; 17442 } 17443 /* Handle the rack-log-ack part (sendmap) */ 17444 if ((sbused(&so->so_snd) == 0) && 17445 (acked > acked_amount) && 17446 (tp->t_state >= TCPS_FIN_WAIT_1) && 17447 (tp->t_flags & TF_SENTFIN)) { 17448 /* 17449 * We must be sure our fin 17450 * was sent and acked (we can be 17451 * in FIN_WAIT_1 without having 17452 * sent the fin). 17453 */ 17454 ourfinisacked = 1; 17455 /* 17456 * Lets make sure snd_una is updated 17457 * since most likely acked_amount = 0 (it 17458 * should be). 17459 */ 17460 tp->snd_una = high_seq; 17461 } 17462 /* Did we make a RTO error? */ 17463 if ((tp->t_flags & TF_PREVVALID) && 17464 ((tp->t_flags & TF_RCVD_TSTMP) == 0)) { 17465 tp->t_flags &= ~TF_PREVVALID; 17466 if (tp->t_rxtshift == 1 && 17467 (int)(ticks - tp->t_badrxtwin) < 0) 17468 rack_cong_signal(tp, CC_RTO_ERR, high_seq, __LINE__); 17469 } 17470 /* Handle the data in the socket buffer */ 17471 KMOD_TCPSTAT_ADD(tcps_rcvackpack, 1); 17472 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 17473 if (acked_amount > 0) { 17474 uint32_t p_cwnd; 17475 struct mbuf *mfree; 17476 17477 if (post_recovery) { 17478 /* 17479 * Grab the segsiz, multiply by 2 and add the snd_cwnd 17480 * that is the max the CC should add if we are exiting 17481 * recovery and doing a late add. 17482 */ 17483 p_cwnd = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 17484 p_cwnd <<= 1; 17485 p_cwnd += tp->snd_cwnd; 17486 } 17487 rack_ack_received(tp, rack, high_seq, nsegs, CC_ACK, post_recovery); 17488 if (post_recovery && (tp->snd_cwnd > p_cwnd)) { 17489 /* Must be non-newreno (cubic) getting too ahead of itself */ 17490 tp->snd_cwnd = p_cwnd; 17491 } 17492 SOCKBUF_LOCK(&so->so_snd); 17493 mfree = sbcut_locked(&so->so_snd, acked_amount); 17494 tp->snd_una = high_seq; 17495 /* Note we want to hold the sb lock through the sendmap adjust */ 17496 rack_adjust_sendmap_head(rack, &so->so_snd); 17497 /* Wake up the socket if we have room to write more */ 17498 rack_log_wakeup(tp,rack, &so->so_snd, acked, 2); 17499 sowwakeup_locked(so); 17500 m_freem(mfree); 17501 } 17502 /* update progress */ 17503 tp->t_acktime = ticks; 17504 rack_log_progress_event(rack, tp, tp->t_acktime, 17505 PROGRESS_UPDATE, __LINE__); 17506 /* Clear out shifts and such */ 17507 tp->t_rxtshift = 0; 17508 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 17509 rack_rto_min, rack_rto_max, rack->r_ctl.timer_slop); 17510 rack->rc_tlp_in_progress = 0; 17511 rack->r_ctl.rc_tlp_cnt_out = 0; 17512 /* Send recover and snd_nxt must be dragged along */ 17513 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 17514 tp->snd_recover = tp->snd_una; 17515 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 17516 tp->snd_nxt = tp->snd_max; 17517 /* 17518 * If the RXT timer is running we want to 17519 * stop it, so we can restart a TLP (or new RXT). 17520 */ 17521 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_RXT) 17522 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 17523 tp->snd_wl2 = high_seq; 17524 tp->t_dupacks = 0; 17525 if (under_pacing && 17526 (rack->use_fixed_rate == 0) && 17527 (rack->in_probe_rtt == 0) && 17528 rack->rc_gp_dyn_mul && 17529 rack->rc_always_pace) { 17530 /* Check if we are dragging bottom */ 17531 rack_check_bottom_drag(tp, rack, so); 17532 } 17533 if (tp->snd_una == tp->snd_max) { 17534 tp->t_flags &= ~TF_PREVVALID; 17535 rack->r_ctl.retran_during_recovery = 0; 17536 rack->rc_suspicious = 0; 17537 rack->r_ctl.dsack_byte_cnt = 0; 17538 rack->r_ctl.rc_went_idle_time = tcp_get_usecs(NULL); 17539 if (rack->r_ctl.rc_went_idle_time == 0) 17540 rack->r_ctl.rc_went_idle_time = 1; 17541 rack_log_progress_event(rack, tp, 0, PROGRESS_CLEAR, __LINE__); 17542 if (sbavail(&tptosocket(tp)->so_snd) == 0) 17543 tp->t_acktime = 0; 17544 /* Set so we might enter persists... */ 17545 rack->r_wanted_output = 1; 17546 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 17547 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 17548 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 17549 (sbavail(&so->so_snd) == 0) && 17550 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 17551 /* 17552 * The socket was gone and the 17553 * peer sent data (not now in the past), time to 17554 * reset him. 17555 */ 17556 rack_timer_cancel(tp, rack, rack->r_ctl.rc_rcvtime, __LINE__); 17557 /* tcp_close will kill the inp pre-log the Reset */ 17558 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 17559 #ifdef TCP_ACCOUNTING 17560 rdstc = get_cyclecount(); 17561 if (rdstc > ts_val) { 17562 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17563 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17564 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17565 } 17566 } 17567 #endif 17568 m_freem(m); 17569 tp = tcp_close(tp); 17570 if (tp == NULL) { 17571 #ifdef TCP_ACCOUNTING 17572 sched_unpin(); 17573 #endif 17574 return (1); 17575 } 17576 /* 17577 * We would normally do drop-with-reset which would 17578 * send back a reset. We can't since we don't have 17579 * all the needed bits. Instead lets arrange for 17580 * a call to tcp_output(). That way since we 17581 * are in the closed state we will generate a reset. 17582 * 17583 * Note if tcp_accounting is on we don't unpin since 17584 * we do that after the goto label. 17585 */ 17586 goto send_out_a_rst; 17587 } 17588 if ((sbused(&so->so_snd) == 0) && 17589 (tp->t_state >= TCPS_FIN_WAIT_1) && 17590 (tp->t_flags & TF_SENTFIN)) { 17591 /* 17592 * If we can't receive any more data, then closing user can 17593 * proceed. Starting the timer is contrary to the 17594 * specification, but if we don't get a FIN we'll hang 17595 * forever. 17596 * 17597 */ 17598 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 17599 soisdisconnected(so); 17600 tcp_timer_activate(tp, TT_2MSL, 17601 (tcp_fast_finwait2_recycle ? 17602 tcp_finwait2_timeout : 17603 TP_MAXIDLE(tp))); 17604 } 17605 if (ourfinisacked == 0) { 17606 /* 17607 * We don't change to fin-wait-2 if we have our fin acked 17608 * which means we are probably in TCPS_CLOSING. 17609 */ 17610 tcp_state_change(tp, TCPS_FIN_WAIT_2); 17611 } 17612 } 17613 } 17614 /* Wake up the socket if we have room to write more */ 17615 if (sbavail(&so->so_snd)) { 17616 rack->r_wanted_output = 1; 17617 if (ctf_progress_timeout_check(tp, true)) { 17618 rack_log_progress_event((struct tcp_rack *)tp->t_fb_ptr, 17619 tp, tick, PROGRESS_DROP, __LINE__); 17620 /* 17621 * We cheat here and don't send a RST, we should send one 17622 * when the pacer drops the connection. 17623 */ 17624 #ifdef TCP_ACCOUNTING 17625 rdstc = get_cyclecount(); 17626 if (rdstc > ts_val) { 17627 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17628 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17629 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17630 } 17631 } 17632 sched_unpin(); 17633 #endif 17634 (void)tcp_drop(tp, ETIMEDOUT); 17635 m_freem(m); 17636 return (1); 17637 } 17638 } 17639 if (ourfinisacked) { 17640 switch(tp->t_state) { 17641 case TCPS_CLOSING: 17642 #ifdef TCP_ACCOUNTING 17643 rdstc = get_cyclecount(); 17644 if (rdstc > ts_val) { 17645 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17646 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17647 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17648 } 17649 } 17650 sched_unpin(); 17651 #endif 17652 tcp_twstart(tp); 17653 m_freem(m); 17654 return (1); 17655 break; 17656 case TCPS_LAST_ACK: 17657 #ifdef TCP_ACCOUNTING 17658 rdstc = get_cyclecount(); 17659 if (rdstc > ts_val) { 17660 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17661 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17662 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17663 } 17664 } 17665 sched_unpin(); 17666 #endif 17667 tp = tcp_close(tp); 17668 ctf_do_drop(m, tp); 17669 return (1); 17670 break; 17671 case TCPS_FIN_WAIT_1: 17672 #ifdef TCP_ACCOUNTING 17673 rdstc = get_cyclecount(); 17674 if (rdstc > ts_val) { 17675 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17676 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17677 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17678 } 17679 } 17680 #endif 17681 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 17682 soisdisconnected(so); 17683 tcp_timer_activate(tp, TT_2MSL, 17684 (tcp_fast_finwait2_recycle ? 17685 tcp_finwait2_timeout : 17686 TP_MAXIDLE(tp))); 17687 } 17688 tcp_state_change(tp, TCPS_FIN_WAIT_2); 17689 break; 17690 default: 17691 break; 17692 } 17693 } 17694 if (rack->r_fast_output) { 17695 /* 17696 * We re doing fast output.. can we expand that? 17697 */ 17698 rack_gain_for_fastoutput(rack, tp, so, acked_amount); 17699 } 17700 #ifdef TCP_ACCOUNTING 17701 rdstc = get_cyclecount(); 17702 if (rdstc > ts_val) { 17703 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17704 tp->tcp_proc_time[ACK_CUMACK] += (rdstc - ts_val); 17705 tp->tcp_proc_time[CYC_HANDLE_ACK] += (rdstc - ts_val); 17706 } 17707 } 17708 17709 } else if (win_up_req) { 17710 rdstc = get_cyclecount(); 17711 if (rdstc > ts_val) { 17712 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 17713 tp->tcp_proc_time[ACK_RWND] += (rdstc - ts_val); 17714 } 17715 } 17716 #endif 17717 } 17718 /* Now is there a next packet, if so we are done */ 17719 m_freem(m); 17720 did_out = 0; 17721 if (nxt_pkt) { 17722 #ifdef TCP_ACCOUNTING 17723 sched_unpin(); 17724 #endif 17725 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 5, nsegs); 17726 return (0); 17727 } 17728 rack_handle_might_revert(tp, rack); 17729 ctf_calc_rwin(so, tp); 17730 if ((rack->r_wanted_output != 0) || 17731 (rack->r_fast_output != 0) || 17732 (tp->t_flags & TF_ACKNOW )) { 17733 send_out_a_rst: 17734 if (tcp_output(tp) < 0) { 17735 #ifdef TCP_ACCOUNTING 17736 sched_unpin(); 17737 #endif 17738 return (1); 17739 } 17740 did_out = 1; 17741 } 17742 if (tp->t_flags2 & TF2_HPTS_CALLS) 17743 tp->t_flags2 &= ~TF2_HPTS_CALLS; 17744 rack_free_trim(rack); 17745 #ifdef TCP_ACCOUNTING 17746 sched_unpin(); 17747 #endif 17748 rack_timer_audit(tp, rack, &so->so_snd); 17749 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, 6, nsegs); 17750 return (0); 17751 } 17752 17753 #define TCP_LRO_TS_OPTION \ 17754 ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 17755 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP) 17756 17757 static int 17758 rack_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 17759 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt, 17760 struct timeval *tv) 17761 { 17762 struct inpcb *inp = tptoinpcb(tp); 17763 struct socket *so = tptosocket(tp); 17764 #ifdef TCP_ACCOUNTING 17765 uint64_t ts_val; 17766 #endif 17767 int32_t thflags, retval, did_out = 0; 17768 int32_t way_out = 0; 17769 /* 17770 * cts - is the current time from tv (caller gets ts) in microseconds. 17771 * ms_cts - is the current time from tv in milliseconds. 17772 * us_cts - is the time that LRO or hardware actually got the packet in microseconds. 17773 */ 17774 uint32_t cts, us_cts, ms_cts; 17775 uint32_t tiwin; 17776 struct timespec ts; 17777 struct tcpopt to; 17778 struct tcp_rack *rack; 17779 struct rack_sendmap *rsm; 17780 int32_t prev_state = 0; 17781 int no_output = 0; 17782 int slot_remaining = 0; 17783 #ifdef TCP_ACCOUNTING 17784 int ack_val_set = 0xf; 17785 #endif 17786 int nsegs; 17787 17788 NET_EPOCH_ASSERT(); 17789 INP_WLOCK_ASSERT(inp); 17790 17791 /* 17792 * tv passed from common code is from either M_TSTMP_LRO or 17793 * tcp_get_usecs() if no LRO m_pkthdr timestamp is present. 17794 */ 17795 rack = (struct tcp_rack *)tp->t_fb_ptr; 17796 if (rack->rack_deferred_inited == 0) { 17797 /* 17798 * If we are the connecting socket we will 17799 * hit rack_init() when no sequence numbers 17800 * are setup. This makes it so we must defer 17801 * some initialization. Call that now. 17802 */ 17803 rack_deferred_init(tp, rack); 17804 } 17805 /* 17806 * Check to see if we need to skip any output plans. This 17807 * can happen in the non-LRO path where we are pacing and 17808 * must process the ack coming in but need to defer sending 17809 * anything becase a pacing timer is running. 17810 */ 17811 us_cts = tcp_tv_to_usectick(tv); 17812 if (m->m_flags & M_ACKCMP) { 17813 /* 17814 * All compressed ack's are ack's by definition so 17815 * remove any ack required flag and then do the processing. 17816 */ 17817 rack->rc_ack_required = 0; 17818 return (rack_do_compressed_ack_processing(tp, so, m, nxt_pkt, tv)); 17819 } 17820 thflags = tcp_get_flags(th); 17821 if ((rack->rc_always_pace == 1) && 17822 (rack->rc_ack_can_sendout_data == 0) && 17823 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 17824 (TSTMP_LT(us_cts, rack->r_ctl.rc_last_output_to))) { 17825 /* 17826 * Ok conditions are right for queuing the packets 17827 * but we do have to check the flags in the inp, it 17828 * could be, if a sack is present, we want to be awoken and 17829 * so should process the packets. 17830 */ 17831 slot_remaining = rack->r_ctl.rc_last_output_to - us_cts; 17832 if (rack->rc_tp->t_flags2 & TF2_DONT_SACK_QUEUE) { 17833 no_output = 1; 17834 } else { 17835 /* 17836 * If there is no options, or just a 17837 * timestamp option, we will want to queue 17838 * the packets. This is the same that LRO does 17839 * and will need to change with accurate ECN. 17840 */ 17841 uint32_t *ts_ptr; 17842 int optlen; 17843 17844 optlen = (th->th_off << 2) - sizeof(struct tcphdr); 17845 ts_ptr = (uint32_t *)(th + 1); 17846 if ((optlen == 0) || 17847 ((optlen == TCPOLEN_TSTAMP_APPA) && 17848 (*ts_ptr == TCP_LRO_TS_OPTION))) 17849 no_output = 1; 17850 } 17851 if ((no_output == 1) && (slot_remaining < tcp_min_hptsi_time)) { 17852 /* 17853 * It is unrealistic to think we can pace in less than 17854 * the minimum granularity of the pacer (def:250usec). So 17855 * if we have less than that time remaining we should go 17856 * ahead and allow output to be "early". We will attempt to 17857 * make up for it in any pacing time we try to apply on 17858 * the outbound packet. 17859 */ 17860 no_output = 0; 17861 } 17862 } 17863 /* 17864 * If there is a RST or FIN lets dump out the bw 17865 * with a FIN the connection may go on but we 17866 * may not. 17867 */ 17868 if ((thflags & TH_FIN) || (thflags & TH_RST)) 17869 rack_log_pacing_delay_calc(rack, 17870 rack->r_ctl.gp_bw, 17871 0, 17872 0, 17873 rack_get_gp_est(rack), /* delRate */ 17874 rack_get_lt_bw(rack), /* rttProp */ 17875 20, __LINE__, NULL, 0); 17876 if (m->m_flags & M_ACKCMP) { 17877 panic("Impossible reach m has ackcmp? m:%p tp:%p", m, tp); 17878 } 17879 cts = tcp_tv_to_usectick(tv); 17880 ms_cts = tcp_tv_to_mssectick(tv); 17881 nsegs = m->m_pkthdr.lro_nsegs; 17882 counter_u64_add(rack_proc_non_comp_ack, 1); 17883 #ifdef TCP_ACCOUNTING 17884 sched_pin(); 17885 if (thflags & TH_ACK) 17886 ts_val = get_cyclecount(); 17887 #endif 17888 if ((m->m_flags & M_TSTMP) || 17889 (m->m_flags & M_TSTMP_LRO)) { 17890 mbuf_tstmp2timespec(m, &ts); 17891 rack->r_ctl.act_rcv_time.tv_sec = ts.tv_sec; 17892 rack->r_ctl.act_rcv_time.tv_usec = ts.tv_nsec/1000; 17893 } else 17894 rack->r_ctl.act_rcv_time = *tv; 17895 kern_prefetch(rack, &prev_state); 17896 prev_state = 0; 17897 /* 17898 * Unscale the window into a 32-bit value. For the SYN_SENT state 17899 * the scale is zero. 17900 */ 17901 tiwin = th->th_win << tp->snd_scale; 17902 #ifdef TCP_ACCOUNTING 17903 if (thflags & TH_ACK) { 17904 /* 17905 * We have a tradeoff here. We can either do what we are 17906 * doing i.e. pinning to this CPU and then doing the accounting 17907 * <or> we could do a critical enter, setup the rdtsc and cpu 17908 * as in below, and then validate we are on the same CPU on 17909 * exit. I have choosen to not do the critical enter since 17910 * that often will gain you a context switch, and instead lock 17911 * us (line above this if) to the same CPU with sched_pin(). This 17912 * means we may be context switched out for a higher priority 17913 * interupt but we won't be moved to another CPU. 17914 * 17915 * If this occurs (which it won't very often since we most likely 17916 * are running this code in interupt context and only a higher 17917 * priority will bump us ... clock?) we will falsely add in 17918 * to the time the interupt processing time plus the ack processing 17919 * time. This is ok since its a rare event. 17920 */ 17921 ack_val_set = tcp_do_ack_accounting(tp, th, &to, tiwin, 17922 ctf_fixed_maxseg(tp)); 17923 } 17924 #endif 17925 /* 17926 * Parse options on any incoming segment. 17927 */ 17928 memset(&to, 0, sizeof(to)); 17929 tcp_dooptions(&to, (u_char *)(th + 1), 17930 (th->th_off << 2) - sizeof(struct tcphdr), 17931 (thflags & TH_SYN) ? TO_SYN : 0); 17932 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 17933 __func__)); 17934 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 17935 __func__)); 17936 17937 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 17938 (tp->t_flags & TF_GPUTINPROG)) { 17939 /* 17940 * We have a goodput in progress 17941 * and we have entered a late state. 17942 * Do we have enough data in the sb 17943 * to handle the GPUT request? 17944 */ 17945 uint32_t bytes; 17946 17947 bytes = tp->gput_ack - tp->gput_seq; 17948 if (SEQ_GT(tp->gput_seq, tp->snd_una)) 17949 bytes += tp->gput_seq - tp->snd_una; 17950 if (bytes > sbavail(&tptosocket(tp)->so_snd)) { 17951 /* 17952 * There are not enough bytes in the socket 17953 * buffer that have been sent to cover this 17954 * measurement. Cancel it. 17955 */ 17956 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 17957 rack->r_ctl.rc_gp_srtt /*flex1*/, 17958 tp->gput_seq, 17959 0, 0, 18, __LINE__, NULL, 0); 17960 tp->t_flags &= ~TF_GPUTINPROG; 17961 } 17962 } 17963 if (tcp_bblogging_on(rack->rc_tp)) { 17964 union tcp_log_stackspecific log; 17965 struct timeval ltv; 17966 #ifdef TCP_REQUEST_TRK 17967 struct tcp_sendfile_track *tcp_req; 17968 17969 if (SEQ_GT(th->th_ack, tp->snd_una)) { 17970 tcp_req = tcp_req_find_req_for_seq(tp, (th->th_ack-1)); 17971 } else { 17972 tcp_req = tcp_req_find_req_for_seq(tp, th->th_ack); 17973 } 17974 #endif 17975 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 17976 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 17977 if (rack->rack_no_prr == 0) 17978 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 17979 else 17980 log.u_bbr.flex1 = 0; 17981 log.u_bbr.use_lt_bw = rack->r_ent_rec_ns; 17982 log.u_bbr.use_lt_bw <<= 1; 17983 log.u_bbr.use_lt_bw |= rack->r_might_revert; 17984 log.u_bbr.flex2 = rack->r_ctl.rc_num_maps_alloced; 17985 log.u_bbr.bbr_state = rack->rc_free_cnt; 17986 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 17987 log.u_bbr.pkts_out = rack->rc_tp->t_maxseg; 17988 log.u_bbr.flex3 = m->m_flags; 17989 log.u_bbr.flex4 = rack->r_ctl.rc_hpts_flags; 17990 log.u_bbr.lost = thflags; 17991 log.u_bbr.pacing_gain = 0x1; 17992 #ifdef TCP_ACCOUNTING 17993 log.u_bbr.cwnd_gain = ack_val_set; 17994 #endif 17995 log.u_bbr.flex7 = 2; 17996 if (m->m_flags & M_TSTMP) { 17997 /* Record the hardware timestamp if present */ 17998 mbuf_tstmp2timespec(m, &ts); 17999 ltv.tv_sec = ts.tv_sec; 18000 ltv.tv_usec = ts.tv_nsec / 1000; 18001 log.u_bbr.lt_epoch = tcp_tv_to_usectick(<v); 18002 } else if (m->m_flags & M_TSTMP_LRO) { 18003 /* Record the LRO the arrival timestamp */ 18004 mbuf_tstmp2timespec(m, &ts); 18005 ltv.tv_sec = ts.tv_sec; 18006 ltv.tv_usec = ts.tv_nsec / 1000; 18007 log.u_bbr.flex5 = tcp_tv_to_usectick(<v); 18008 } 18009 log.u_bbr.timeStamp = tcp_get_usecs(<v); 18010 /* Log the rcv time */ 18011 log.u_bbr.delRate = m->m_pkthdr.rcv_tstmp; 18012 #ifdef TCP_REQUEST_TRK 18013 log.u_bbr.applimited = tp->t_tcpreq_closed; 18014 log.u_bbr.applimited <<= 8; 18015 log.u_bbr.applimited |= tp->t_tcpreq_open; 18016 log.u_bbr.applimited <<= 8; 18017 log.u_bbr.applimited |= tp->t_tcpreq_req; 18018 if (tcp_req) { 18019 /* Copy out any client req info */ 18020 /* seconds */ 18021 log.u_bbr.pkt_epoch = (tcp_req->localtime / HPTS_USEC_IN_SEC); 18022 /* useconds */ 18023 log.u_bbr.delivered = (tcp_req->localtime % HPTS_USEC_IN_SEC); 18024 log.u_bbr.rttProp = tcp_req->timestamp; 18025 log.u_bbr.cur_del_rate = tcp_req->start; 18026 if (tcp_req->flags & TCP_TRK_TRACK_FLG_OPEN) { 18027 log.u_bbr.flex8 |= 1; 18028 } else { 18029 log.u_bbr.flex8 |= 2; 18030 log.u_bbr.bw_inuse = tcp_req->end; 18031 } 18032 log.u_bbr.flex6 = tcp_req->start_seq; 18033 if (tcp_req->flags & TCP_TRK_TRACK_FLG_COMP) { 18034 log.u_bbr.flex8 |= 4; 18035 log.u_bbr.epoch = tcp_req->end_seq; 18036 } 18037 } 18038 #endif 18039 TCP_LOG_EVENTP(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 18040 tlen, &log, true, <v); 18041 } 18042 /* Remove ack required flag if set, we have one */ 18043 if (thflags & TH_ACK) 18044 rack->rc_ack_required = 0; 18045 if (rack->sack_attack_disable > 0) { 18046 rack->r_ctl.ack_during_sd++; 18047 rack_log_type_bbrsnd(rack, 0, 0, cts, tv, __LINE__); 18048 } 18049 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 18050 way_out = 4; 18051 retval = 0; 18052 m_freem(m); 18053 goto done_with_input; 18054 } 18055 /* 18056 * If a segment with the ACK-bit set arrives in the SYN-SENT state 18057 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 18058 */ 18059 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 18060 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 18061 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 18062 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 18063 #ifdef TCP_ACCOUNTING 18064 sched_unpin(); 18065 #endif 18066 return (1); 18067 } 18068 /* 18069 * If timestamps were negotiated during SYN/ACK and a 18070 * segment without a timestamp is received, silently drop 18071 * the segment, unless it is a RST segment or missing timestamps are 18072 * tolerated. 18073 * See section 3.2 of RFC 7323. 18074 */ 18075 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 18076 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 18077 way_out = 5; 18078 retval = 0; 18079 m_freem(m); 18080 goto done_with_input; 18081 } 18082 /* 18083 * Segment received on connection. Reset idle time and keep-alive 18084 * timer. XXX: This should be done after segment validation to 18085 * ignore broken/spoofed segs. 18086 */ 18087 if (tp->t_idle_reduce && 18088 (tp->snd_max == tp->snd_una) && 18089 (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) { 18090 counter_u64_add(rack_input_idle_reduces, 1); 18091 rack_cc_after_idle(rack, tp); 18092 } 18093 tp->t_rcvtime = ticks; 18094 #ifdef STATS 18095 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 18096 #endif 18097 if (tiwin > rack->r_ctl.rc_high_rwnd) 18098 rack->r_ctl.rc_high_rwnd = tiwin; 18099 /* 18100 * TCP ECN processing. XXXJTL: If we ever use ECN, we need to move 18101 * this to occur after we've validated the segment. 18102 */ 18103 if (tcp_ecn_input_segment(tp, thflags, tlen, 18104 tcp_packets_this_ack(tp, th->th_ack), 18105 iptos)) 18106 rack_cong_signal(tp, CC_ECN, th->th_ack, __LINE__); 18107 18108 /* 18109 * If echoed timestamp is later than the current time, fall back to 18110 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 18111 * were used when this connection was established. 18112 */ 18113 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 18114 to.to_tsecr -= tp->ts_offset; 18115 if (TSTMP_GT(to.to_tsecr, ms_cts)) 18116 to.to_tsecr = 0; 18117 } 18118 if ((rack->r_rcvpath_rtt_up == 1) && 18119 (to.to_flags & TOF_TS) && 18120 (TSTMP_GEQ(to.to_tsecr, rack->r_ctl.last_rcv_tstmp_for_rtt))) { 18121 uint32_t rtt = 0; 18122 18123 /* 18124 * We are receiving only and thus not sending 18125 * data to do an RTT. We set a flag when we first 18126 * sent this TS to the peer. We now have it back 18127 * and have an RTT to share. We log it as a conf 18128 * 4, we are not so sure about it.. since we 18129 * may have lost an ack. 18130 */ 18131 if (TSTMP_GT(cts, rack->r_ctl.last_time_of_arm_rcv)) 18132 rtt = (cts - rack->r_ctl.last_time_of_arm_rcv); 18133 rack->r_rcvpath_rtt_up = 0; 18134 /* Submit and commit the timer */ 18135 if (rtt > 0) { 18136 tcp_rack_xmit_timer(rack, rtt, 0, rtt, 4, NULL, 1); 18137 tcp_rack_xmit_timer_commit(rack, tp); 18138 } 18139 } 18140 /* 18141 * If its the first time in we need to take care of options and 18142 * verify we can do SACK for rack! 18143 */ 18144 if (rack->r_state == 0) { 18145 /* Should be init'd by rack_init() */ 18146 KASSERT(rack->rc_inp != NULL, 18147 ("%s: rack->rc_inp unexpectedly NULL", __func__)); 18148 if (rack->rc_inp == NULL) { 18149 rack->rc_inp = inp; 18150 } 18151 18152 /* 18153 * Process options only when we get SYN/ACK back. The SYN 18154 * case for incoming connections is handled in tcp_syncache. 18155 * According to RFC1323 the window field in a SYN (i.e., a 18156 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 18157 * this is traditional behavior, may need to be cleaned up. 18158 */ 18159 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 18160 /* Handle parallel SYN for ECN */ 18161 tcp_ecn_input_parallel_syn(tp, thflags, iptos); 18162 if ((to.to_flags & TOF_SCALE) && 18163 (tp->t_flags & TF_REQ_SCALE)) { 18164 tp->t_flags |= TF_RCVD_SCALE; 18165 tp->snd_scale = to.to_wscale; 18166 } else 18167 tp->t_flags &= ~TF_REQ_SCALE; 18168 /* 18169 * Initial send window. It will be updated with the 18170 * next incoming segment to the scaled value. 18171 */ 18172 tp->snd_wnd = th->th_win; 18173 rack_validate_fo_sendwin_up(tp, rack); 18174 if ((to.to_flags & TOF_TS) && 18175 (tp->t_flags & TF_REQ_TSTMP)) { 18176 tp->t_flags |= TF_RCVD_TSTMP; 18177 tp->ts_recent = to.to_tsval; 18178 tp->ts_recent_age = cts; 18179 } else 18180 tp->t_flags &= ~TF_REQ_TSTMP; 18181 if (to.to_flags & TOF_MSS) { 18182 tcp_mss(tp, to.to_mss); 18183 } 18184 if ((tp->t_flags & TF_SACK_PERMIT) && 18185 (to.to_flags & TOF_SACKPERM) == 0) 18186 tp->t_flags &= ~TF_SACK_PERMIT; 18187 if (tp->t_flags & TF_FASTOPEN) { 18188 if (to.to_flags & TOF_FASTOPEN) { 18189 uint16_t mss; 18190 18191 if (to.to_flags & TOF_MSS) 18192 mss = to.to_mss; 18193 else 18194 if ((inp->inp_vflag & INP_IPV6) != 0) 18195 mss = TCP6_MSS; 18196 else 18197 mss = TCP_MSS; 18198 tcp_fastopen_update_cache(tp, mss, 18199 to.to_tfo_len, to.to_tfo_cookie); 18200 } else 18201 tcp_fastopen_disable_path(tp); 18202 } 18203 } 18204 /* 18205 * At this point we are at the initial call. Here we decide 18206 * if we are doing RACK or not. We do this by seeing if 18207 * TF_SACK_PERMIT is set and the sack-not-required is clear. 18208 * The code now does do dup-ack counting so if you don't 18209 * switch back you won't get rack & TLP, but you will still 18210 * get this stack. 18211 */ 18212 18213 if ((rack_sack_not_required == 0) && 18214 ((tp->t_flags & TF_SACK_PERMIT) == 0)) { 18215 tcp_switch_back_to_default(tp); 18216 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen, 18217 tlen, iptos); 18218 #ifdef TCP_ACCOUNTING 18219 sched_unpin(); 18220 #endif 18221 return (1); 18222 } 18223 tcp_set_hpts(tp); 18224 sack_filter_clear(&rack->r_ctl.rack_sf, th->th_ack); 18225 } 18226 if (thflags & TH_FIN) 18227 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 18228 us_cts = tcp_tv_to_usectick(&rack->r_ctl.act_rcv_time); 18229 if ((rack->rc_gp_dyn_mul) && 18230 (rack->use_fixed_rate == 0) && 18231 (rack->rc_always_pace)) { 18232 /* Check in on probertt */ 18233 rack_check_probe_rtt(rack, cts); 18234 } 18235 rack_clear_rate_sample(rack); 18236 if ((rack->forced_ack) && 18237 ((tcp_get_flags(th) & TH_RST) == 0)) { 18238 rack_handle_probe_response(rack, tiwin, us_cts); 18239 } 18240 /* 18241 * This is the one exception case where we set the rack state 18242 * always. All other times (timers etc) we must have a rack-state 18243 * set (so we assure we have done the checks above for SACK). 18244 */ 18245 rack->r_ctl.rc_rcvtime = cts; 18246 if (rack->r_state != tp->t_state) 18247 rack_set_state(tp, rack); 18248 if (SEQ_GT(th->th_ack, tp->snd_una) && 18249 (rsm = tqhash_min(rack->r_ctl.tqh)) != NULL) 18250 kern_prefetch(rsm, &prev_state); 18251 prev_state = rack->r_state; 18252 if ((thflags & TH_RST) && 18253 ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 18254 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 18255 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq))) { 18256 /* The connection will be killed by a reset check the tracepoint */ 18257 tcp_trace_point(rack->rc_tp, TCP_TP_RESET_RCV); 18258 } 18259 retval = (*rack->r_substate) (m, th, so, 18260 tp, &to, drop_hdrlen, 18261 tlen, tiwin, thflags, nxt_pkt, iptos); 18262 if (retval == 0) { 18263 /* 18264 * If retval is 1 the tcb is unlocked and most likely the tp 18265 * is gone. 18266 */ 18267 INP_WLOCK_ASSERT(inp); 18268 if ((rack->rc_gp_dyn_mul) && 18269 (rack->rc_always_pace) && 18270 (rack->use_fixed_rate == 0) && 18271 rack->in_probe_rtt && 18272 (rack->r_ctl.rc_time_probertt_starts == 0)) { 18273 /* 18274 * If we are going for target, lets recheck before 18275 * we output. 18276 */ 18277 rack_check_probe_rtt(rack, cts); 18278 } 18279 if (rack->set_pacing_done_a_iw == 0) { 18280 /* How much has been acked? */ 18281 if ((tp->snd_una - tp->iss) > (ctf_fixed_maxseg(tp) * 10)) { 18282 /* We have enough to set in the pacing segment size */ 18283 rack->set_pacing_done_a_iw = 1; 18284 rack_set_pace_segments(tp, rack, __LINE__, NULL); 18285 } 18286 } 18287 tcp_rack_xmit_timer_commit(rack, tp); 18288 #ifdef TCP_ACCOUNTING 18289 /* 18290 * If we set the ack_val_se to what ack processing we are doing 18291 * we also want to track how many cycles we burned. Note 18292 * the bits after tcp_output we let be "free". This is because 18293 * we are also tracking the tcp_output times as well. Note the 18294 * use of 0xf here since we only have 11 counter (0 - 0xa) and 18295 * 0xf cannot be returned and is what we initialize it too to 18296 * indicate we are not doing the tabulations. 18297 */ 18298 if (ack_val_set != 0xf) { 18299 uint64_t crtsc; 18300 18301 crtsc = get_cyclecount(); 18302 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 18303 tp->tcp_proc_time[ack_val_set] += (crtsc - ts_val); 18304 } 18305 } 18306 #endif 18307 if ((nxt_pkt == 0) && (no_output == 0)) { 18308 if ((rack->r_wanted_output != 0) || 18309 (tp->t_flags & TF_ACKNOW) || 18310 (rack->r_fast_output != 0)) { 18311 18312 do_output_now: 18313 if (tcp_output(tp) < 0) { 18314 #ifdef TCP_ACCOUNTING 18315 sched_unpin(); 18316 #endif 18317 return (1); 18318 } 18319 did_out = 1; 18320 } 18321 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 18322 rack_free_trim(rack); 18323 } else if ((nxt_pkt == 0) && (tp->t_flags & TF_ACKNOW)) { 18324 goto do_output_now; 18325 } else if ((no_output == 1) && 18326 (nxt_pkt == 0) && 18327 (tcp_in_hpts(rack->rc_tp) == 0)) { 18328 /* 18329 * We are not in hpts and we had a pacing timer up. Use 18330 * the remaining time (slot_remaining) to restart the timer. 18331 */ 18332 KASSERT ((slot_remaining != 0), ("slot remaining is zero for rack:%p tp:%p", rack, tp)); 18333 rack_start_hpts_timer(rack, tp, cts, slot_remaining, 0, 0); 18334 rack_free_trim(rack); 18335 } 18336 /* Clear the flag, it may have been cleared by output but we may not have */ 18337 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS)) 18338 tp->t_flags2 &= ~TF2_HPTS_CALLS; 18339 /* 18340 * The draft (v3) calls for us to use SEQ_GEQ, but that 18341 * causes issues when we are just going app limited. Lets 18342 * instead use SEQ_GT <or> where its equal but more data 18343 * is outstanding. 18344 * 18345 * Also make sure we are on the last ack of a series. We 18346 * have to have all the ack's processed in queue to know 18347 * if there is something left outstanding. 18348 */ 18349 if (SEQ_GEQ(tp->snd_una, rack->r_ctl.roundends) && 18350 (rack->rc_new_rnd_needed == 0) && 18351 (nxt_pkt == 0)) { 18352 /* 18353 * We have crossed into a new round with 18354 * the new snd_unae. 18355 */ 18356 rack_new_round_setup(tp, rack, tp->snd_una); 18357 } 18358 if ((nxt_pkt == 0) && 18359 ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 18360 (SEQ_GT(tp->snd_max, tp->snd_una) || 18361 (tp->t_flags & TF_DELACK) || 18362 ((V_tcp_always_keepalive || rack->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 18363 (tp->t_state <= TCPS_CLOSING)))) { 18364 /* We could not send (probably in the hpts but stopped the timer earlier)? */ 18365 if ((tp->snd_max == tp->snd_una) && 18366 ((tp->t_flags & TF_DELACK) == 0) && 18367 (tcp_in_hpts(rack->rc_tp)) && 18368 (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 18369 /* keep alive not needed if we are hptsi output yet */ 18370 ; 18371 } else { 18372 int late = 0; 18373 if (tcp_in_hpts(tp)) { 18374 if (rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 18375 us_cts = tcp_get_usecs(NULL); 18376 if (TSTMP_GT(rack->r_ctl.rc_last_output_to, us_cts)) { 18377 rack->r_early = 1; 18378 rack->r_ctl.rc_agg_early += (rack->r_ctl.rc_last_output_to - us_cts); 18379 } else 18380 late = 1; 18381 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 18382 } 18383 tcp_hpts_remove(tp); 18384 } 18385 if (late && (did_out == 0)) { 18386 /* 18387 * We are late in the sending 18388 * and we did not call the output 18389 * (this probably should not happen). 18390 */ 18391 goto do_output_now; 18392 } 18393 rack_start_hpts_timer(rack, tp, tcp_get_usecs(NULL), 0, 0, 0); 18394 } 18395 way_out = 1; 18396 } else if (nxt_pkt == 0) { 18397 /* Do we have the correct timer running? */ 18398 rack_timer_audit(tp, rack, &so->so_snd); 18399 way_out = 2; 18400 } 18401 done_with_input: 18402 rack_log_doseg_done(rack, cts, nxt_pkt, did_out, way_out, max(1, nsegs)); 18403 if (did_out) 18404 rack->r_wanted_output = 0; 18405 } 18406 18407 #ifdef TCP_ACCOUNTING 18408 sched_unpin(); 18409 #endif 18410 return (retval); 18411 } 18412 18413 static void 18414 rack_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 18415 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 18416 { 18417 struct timeval tv; 18418 18419 /* First lets see if we have old packets */ 18420 if (!STAILQ_EMPTY(&tp->t_inqueue)) { 18421 if (ctf_do_queued_segments(tp, 1)) { 18422 m_freem(m); 18423 return; 18424 } 18425 } 18426 if (m->m_flags & M_TSTMP_LRO) { 18427 mbuf_tstmp2timeval(m, &tv); 18428 } else { 18429 /* Should not be should we kassert instead? */ 18430 tcp_get_usecs(&tv); 18431 } 18432 if (rack_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 0, 18433 &tv) == 0) { 18434 INP_WUNLOCK(tptoinpcb(tp)); 18435 } 18436 } 18437 18438 struct rack_sendmap * 18439 tcp_rack_output(struct tcpcb *tp, struct tcp_rack *rack, uint32_t tsused) 18440 { 18441 struct rack_sendmap *rsm = NULL; 18442 int32_t idx; 18443 uint32_t srtt = 0, thresh = 0, ts_low = 0; 18444 int no_sack = 0; 18445 18446 /* Return the next guy to be re-transmitted */ 18447 if (tqhash_empty(rack->r_ctl.tqh)) { 18448 return (NULL); 18449 } 18450 if (tp->t_flags & TF_SENTFIN) { 18451 /* retran the end FIN? */ 18452 return (NULL); 18453 } 18454 /* ok lets look at this one */ 18455 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 18456 if (rack->r_must_retran && rsm && (rsm->r_flags & RACK_MUST_RXT)) { 18457 return (rsm); 18458 } 18459 if (rsm && ((rsm->r_flags & RACK_ACKED) == 0)) { 18460 goto check_it; 18461 } 18462 rsm = rack_find_lowest_rsm(rack); 18463 if (rsm == NULL) { 18464 return (NULL); 18465 } 18466 check_it: 18467 if (((rack->rc_tp->t_flags & TF_SACK_PERMIT) == 0) || 18468 (rack->sack_attack_disable > 0)) { 18469 no_sack = 1; 18470 } 18471 if ((no_sack > 0) && 18472 (rsm->r_dupack >= DUP_ACK_THRESHOLD)) { 18473 /* 18474 * No sack so we automatically do the 3 strikes and 18475 * retransmit (no rack timer would be started). 18476 */ 18477 return (rsm); 18478 } 18479 if (rsm->r_flags & RACK_ACKED) { 18480 return (NULL); 18481 } 18482 if (((rsm->r_flags & RACK_SACK_PASSED) == 0) && 18483 (rsm->r_dupack < DUP_ACK_THRESHOLD)) { 18484 /* Its not yet ready */ 18485 return (NULL); 18486 } 18487 srtt = rack_grab_rtt(tp, rack); 18488 idx = rsm->r_rtr_cnt - 1; 18489 ts_low = (uint32_t)rsm->r_tim_lastsent[idx]; 18490 thresh = rack_calc_thresh_rack(rack, srtt, tsused, __LINE__, 1); 18491 if ((tsused == ts_low) || 18492 (TSTMP_LT(tsused, ts_low))) { 18493 /* No time since sending */ 18494 return (NULL); 18495 } 18496 if ((tsused - ts_low) < thresh) { 18497 /* It has not been long enough yet */ 18498 return (NULL); 18499 } 18500 if ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 18501 ((rsm->r_flags & RACK_SACK_PASSED) && 18502 (rack->sack_attack_disable == 0))) { 18503 /* 18504 * We have passed the dup-ack threshold <or> 18505 * a SACK has indicated this is missing. 18506 * Note that if you are a declared attacker 18507 * it is only the dup-ack threshold that 18508 * will cause retransmits. 18509 */ 18510 /* log retransmit reason */ 18511 rack_log_retran_reason(rack, rsm, (tsused - ts_low), thresh, 1); 18512 rack->r_fast_output = 0; 18513 return (rsm); 18514 } 18515 return (NULL); 18516 } 18517 18518 static void 18519 rack_log_pacing_delay_calc (struct tcp_rack *rack, uint32_t len, uint32_t slot, 18520 uint64_t bw_est, uint64_t bw, uint64_t len_time, int method, 18521 int line, struct rack_sendmap *rsm, uint8_t quality) 18522 { 18523 if (tcp_bblogging_on(rack->rc_tp)) { 18524 union tcp_log_stackspecific log; 18525 struct timeval tv; 18526 18527 if (rack_verbose_logging == 0) { 18528 /* 18529 * We are not verbose screen out all but 18530 * ones we always want. 18531 */ 18532 if ((method != 2) && 18533 (method != 3) && 18534 (method != 7) && 18535 (method != 89) && 18536 (method != 14) && 18537 (method != 20)) { 18538 return; 18539 } 18540 } 18541 memset(&log, 0, sizeof(log)); 18542 log.u_bbr.flex1 = slot; 18543 log.u_bbr.flex2 = len; 18544 log.u_bbr.flex3 = rack->r_ctl.rc_pace_min_segs; 18545 log.u_bbr.flex4 = rack->r_ctl.rc_pace_max_segs; 18546 log.u_bbr.flex5 = rack->r_ctl.rack_per_of_gp_ss; 18547 log.u_bbr.flex6 = rack->r_ctl.rack_per_of_gp_ca; 18548 log.u_bbr.use_lt_bw = rack->rc_ack_can_sendout_data; 18549 log.u_bbr.use_lt_bw <<= 1; 18550 log.u_bbr.use_lt_bw |= rack->r_late; 18551 log.u_bbr.use_lt_bw <<= 1; 18552 log.u_bbr.use_lt_bw |= rack->r_early; 18553 log.u_bbr.use_lt_bw <<= 1; 18554 log.u_bbr.use_lt_bw |= rack->app_limited_needs_set; 18555 log.u_bbr.use_lt_bw <<= 1; 18556 log.u_bbr.use_lt_bw |= rack->rc_gp_filled; 18557 log.u_bbr.use_lt_bw <<= 1; 18558 log.u_bbr.use_lt_bw |= rack->measure_saw_probe_rtt; 18559 log.u_bbr.use_lt_bw <<= 1; 18560 log.u_bbr.use_lt_bw |= rack->in_probe_rtt; 18561 log.u_bbr.use_lt_bw <<= 1; 18562 log.u_bbr.use_lt_bw |= rack->gp_ready; 18563 log.u_bbr.pkt_epoch = line; 18564 log.u_bbr.epoch = rack->r_ctl.rc_agg_delayed; 18565 log.u_bbr.lt_epoch = rack->r_ctl.rc_agg_early; 18566 log.u_bbr.applimited = rack->r_ctl.rack_per_of_gp_rec; 18567 log.u_bbr.bw_inuse = bw_est; 18568 log.u_bbr.delRate = bw; 18569 if (rack->r_ctl.gp_bw == 0) 18570 log.u_bbr.cur_del_rate = 0; 18571 else 18572 log.u_bbr.cur_del_rate = rack_get_bw(rack); 18573 log.u_bbr.rttProp = len_time; 18574 log.u_bbr.pkts_out = rack->r_ctl.rc_rack_min_rtt; 18575 log.u_bbr.lost = rack->r_ctl.rc_probertt_sndmax_atexit; 18576 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm); 18577 if (rack->r_ctl.cwnd_to_use < rack->rc_tp->snd_ssthresh) { 18578 /* We are in slow start */ 18579 log.u_bbr.flex7 = 1; 18580 } else { 18581 /* we are on congestion avoidance */ 18582 log.u_bbr.flex7 = 0; 18583 } 18584 log.u_bbr.flex8 = method; 18585 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 18586 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 18587 log.u_bbr.cwnd_gain = rack->rc_gp_saw_rec; 18588 log.u_bbr.cwnd_gain <<= 1; 18589 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ss; 18590 log.u_bbr.cwnd_gain <<= 1; 18591 log.u_bbr.cwnd_gain |= rack->rc_gp_saw_ca; 18592 log.u_bbr.bbr_substate = quality; 18593 log.u_bbr.bbr_state = rack->dgp_on; 18594 log.u_bbr.bbr_state <<= 1; 18595 log.u_bbr.bbr_state |= rack->rc_pace_to_cwnd; 18596 log.u_bbr.bbr_state <<= 2; 18597 TCP_LOG_EVENTP(rack->rc_tp, NULL, 18598 &rack->rc_inp->inp_socket->so_rcv, 18599 &rack->rc_inp->inp_socket->so_snd, 18600 BBR_LOG_HPTSI_CALC, 0, 18601 0, &log, false, &tv); 18602 } 18603 } 18604 18605 static uint32_t 18606 rack_get_pacing_len(struct tcp_rack *rack, uint64_t bw, uint32_t mss) 18607 { 18608 uint32_t new_tso, user_max, pace_one; 18609 18610 user_max = rack->rc_user_set_max_segs * mss; 18611 if (rack->rc_force_max_seg) { 18612 return (user_max); 18613 } 18614 if (rack->use_fixed_rate && 18615 ((rack->r_ctl.crte == NULL) || 18616 (bw != rack->r_ctl.crte->rate))) { 18617 /* Use the user mss since we are not exactly matched */ 18618 return (user_max); 18619 } 18620 if (rack_pace_one_seg || 18621 (rack->r_ctl.rc_user_set_min_segs == 1)) 18622 pace_one = 1; 18623 else 18624 pace_one = 0; 18625 18626 new_tso = tcp_get_pacing_burst_size_w_divisor(rack->rc_tp, bw, mss, 18627 pace_one, rack->r_ctl.crte, NULL, rack->r_ctl.pace_len_divisor); 18628 if (new_tso > user_max) 18629 new_tso = user_max; 18630 if (rack->rc_hybrid_mode && rack->r_ctl.client_suggested_maxseg) { 18631 if (((uint32_t)rack->r_ctl.client_suggested_maxseg * mss) > new_tso) 18632 new_tso = (uint32_t)rack->r_ctl.client_suggested_maxseg * mss; 18633 } 18634 if (rack->r_ctl.rc_user_set_min_segs && 18635 ((rack->r_ctl.rc_user_set_min_segs * mss) > new_tso)) 18636 new_tso = rack->r_ctl.rc_user_set_min_segs * mss; 18637 return (new_tso); 18638 } 18639 18640 static uint64_t 18641 rack_arrive_at_discounted_rate(struct tcp_rack *rack, uint64_t window_input, uint32_t *rate_set, uint32_t *gain_b) 18642 { 18643 uint64_t reduced_win; 18644 uint32_t gain; 18645 18646 if (window_input < rc_init_window(rack)) { 18647 /* 18648 * The cwnd is collapsed to 18649 * nearly zero, maybe because of a time-out? 18650 * Lets drop back to the lt-bw. 18651 */ 18652 reduced_win = rack_get_lt_bw(rack); 18653 /* Set the flag so the caller knows its a rate and not a reduced window */ 18654 *rate_set = 1; 18655 gain = 100; 18656 } else if (IN_RECOVERY(rack->rc_tp->t_flags)) { 18657 /* 18658 * If we are in recover our cwnd needs to be less for 18659 * our pacing consideration. 18660 */ 18661 if (rack->rack_hibeta == 0) { 18662 reduced_win = window_input / 2; 18663 gain = 50; 18664 } else { 18665 reduced_win = window_input * rack->r_ctl.saved_hibeta; 18666 reduced_win /= 100; 18667 gain = rack->r_ctl.saved_hibeta; 18668 } 18669 } else { 18670 /* 18671 * Apply Timely factor to increase/decrease the 18672 * amount we are pacing at. 18673 */ 18674 gain = rack_get_output_gain(rack, NULL); 18675 if (gain > rack_gain_p5_ub) { 18676 gain = rack_gain_p5_ub; 18677 } 18678 reduced_win = window_input * gain; 18679 reduced_win /= 100; 18680 } 18681 if (gain_b != NULL) 18682 *gain_b = gain; 18683 /* 18684 * What is being returned here is a trimmed down 18685 * window values in all cases where rate_set is left 18686 * at 0. In one case we actually return the rate (lt_bw). 18687 * the "reduced_win" is returned as a slimmed down cwnd that 18688 * is then calculated by the caller into a rate when rate_set 18689 * is 0. 18690 */ 18691 return (reduced_win); 18692 } 18693 18694 static int32_t 18695 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) 18696 { 18697 uint64_t lentim, fill_bw; 18698 18699 rack->r_via_fill_cw = 0; 18700 if (ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked) > rack->r_ctl.cwnd_to_use) 18701 return (slot); 18702 if ((ctf_outstanding(rack->rc_tp) + (segsiz-1)) > rack->rc_tp->snd_wnd) 18703 return (slot); 18704 if (rack->r_ctl.rc_last_us_rtt == 0) 18705 return (slot); 18706 if (rack->rc_pace_fill_if_rttin_range && 18707 (rack->r_ctl.rc_last_us_rtt >= 18708 (get_filter_value_small(&rack->r_ctl.rc_gp_min_rtt) * rack->rtt_limit_mul))) { 18709 /* The rtt is huge, N * smallest, lets not fill */ 18710 return (slot); 18711 } 18712 if (rack->r_ctl.fillcw_cap && *rate_wanted >= rack->r_ctl.fillcw_cap) 18713 return (slot); 18714 /* 18715 * first lets calculate the b/w based on the last us-rtt 18716 * and the the smallest send window. 18717 */ 18718 fill_bw = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use); 18719 if (rack->rc_fillcw_apply_discount) { 18720 uint32_t rate_set = 0; 18721 18722 fill_bw = rack_arrive_at_discounted_rate(rack, fill_bw, &rate_set, NULL); 18723 if (rate_set) { 18724 goto at_lt_bw; 18725 } 18726 } 18727 /* Take the rwnd if its smaller */ 18728 if (fill_bw > rack->rc_tp->snd_wnd) 18729 fill_bw = rack->rc_tp->snd_wnd; 18730 /* Now lets make it into a b/w */ 18731 fill_bw *= (uint64_t)HPTS_USEC_IN_SEC; 18732 fill_bw /= (uint64_t)rack->r_ctl.rc_last_us_rtt; 18733 /* Adjust to any cap */ 18734 if (rack->r_ctl.fillcw_cap && fill_bw >= rack->r_ctl.fillcw_cap) 18735 fill_bw = rack->r_ctl.fillcw_cap; 18736 18737 at_lt_bw: 18738 if (rack_bw_multipler > 0) { 18739 /* 18740 * We want to limit fill-cw to the some multiplier 18741 * of the max(lt_bw, gp_est). The normal default 18742 * is 0 for off, so a sysctl has enabled it. 18743 */ 18744 uint64_t lt_bw, gp, rate; 18745 18746 gp = rack_get_gp_est(rack); 18747 lt_bw = rack_get_lt_bw(rack); 18748 if (lt_bw > gp) 18749 rate = lt_bw; 18750 else 18751 rate = gp; 18752 rate *= rack_bw_multipler; 18753 rate /= 100; 18754 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 18755 union tcp_log_stackspecific log; 18756 struct timeval tv; 18757 18758 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 18759 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 18760 log.u_bbr.flex1 = rack_bw_multipler; 18761 log.u_bbr.flex2 = len; 18762 log.u_bbr.cur_del_rate = gp; 18763 log.u_bbr.delRate = lt_bw; 18764 log.u_bbr.bw_inuse = rate; 18765 log.u_bbr.rttProp = fill_bw; 18766 log.u_bbr.flex8 = 44; 18767 tcp_log_event(rack->rc_tp, NULL, NULL, NULL, 18768 BBR_LOG_CWND, 0, 18769 0, &log, false, NULL, 18770 __func__, __LINE__, &tv); 18771 } 18772 if (fill_bw > rate) 18773 fill_bw = rate; 18774 } 18775 /* We are below the min b/w */ 18776 if (non_paced) 18777 *rate_wanted = fill_bw; 18778 if ((fill_bw < RACK_MIN_BW) || (fill_bw < *rate_wanted)) 18779 return (slot); 18780 rack->r_via_fill_cw = 1; 18781 if (rack->r_rack_hw_rate_caps && 18782 (rack->r_ctl.crte != NULL)) { 18783 uint64_t high_rate; 18784 18785 high_rate = tcp_hw_highest_rate(rack->r_ctl.crte); 18786 if (fill_bw > high_rate) { 18787 /* We are capping bw at the highest rate table entry */ 18788 if (*rate_wanted > high_rate) { 18789 /* The original rate was also capped */ 18790 rack->r_via_fill_cw = 0; 18791 } 18792 rack_log_hdwr_pacing(rack, 18793 fill_bw, high_rate, __LINE__, 18794 0, 3); 18795 fill_bw = high_rate; 18796 if (capped) 18797 *capped = 1; 18798 } 18799 } else if ((rack->r_ctl.crte == NULL) && 18800 (rack->rack_hdrw_pacing == 0) && 18801 (rack->rack_hdw_pace_ena) && 18802 rack->r_rack_hw_rate_caps && 18803 (rack->rack_attempt_hdwr_pace == 0) && 18804 (rack->rc_inp->inp_route.ro_nh != NULL) && 18805 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 18806 /* 18807 * Ok we may have a first attempt that is greater than our top rate 18808 * lets check. 18809 */ 18810 uint64_t high_rate; 18811 18812 high_rate = tcp_hw_highest_rate_ifp(rack->rc_inp->inp_route.ro_nh->nh_ifp, rack->rc_inp); 18813 if (high_rate) { 18814 if (fill_bw > high_rate) { 18815 fill_bw = high_rate; 18816 if (capped) 18817 *capped = 1; 18818 } 18819 } 18820 } 18821 if (rack->r_ctl.bw_rate_cap && (fill_bw > rack->r_ctl.bw_rate_cap)) { 18822 rack_log_hybrid_bw(rack, rack->rc_tp->snd_max, 18823 fill_bw, 0, 0, HYBRID_LOG_RATE_CAP, 2, NULL, __LINE__); 18824 fill_bw = rack->r_ctl.bw_rate_cap; 18825 } 18826 /* 18827 * Ok fill_bw holds our mythical b/w to fill the cwnd 18828 * in an rtt (unless it was capped), what does that 18829 * time wise equate too? 18830 */ 18831 lentim = (uint64_t)(len) * (uint64_t)HPTS_USEC_IN_SEC; 18832 lentim /= fill_bw; 18833 *rate_wanted = fill_bw; 18834 if (non_paced || (lentim < slot)) { 18835 rack_log_pacing_delay_calc(rack, len, slot, fill_bw, 18836 0, lentim, 12, __LINE__, NULL, 0); 18837 return ((int32_t)lentim); 18838 } else 18839 return (slot); 18840 } 18841 18842 static uint32_t 18843 rack_policer_check_send(struct tcp_rack *rack, uint32_t len, uint32_t segsiz, uint32_t *needs) 18844 { 18845 uint64_t calc; 18846 18847 rack->rc_policer_should_pace = 0; 18848 calc = rack_policer_bucket_reserve * rack->r_ctl.policer_bucket_size; 18849 calc /= 100; 18850 /* 18851 * Now lets look at if we want more than is in the bucket <or> 18852 * we want more than is reserved in the bucket. 18853 */ 18854 if (rack_verbose_logging > 0) 18855 policer_detection_log(rack, len, segsiz, calc, rack->r_ctl.current_policer_bucket, 8); 18856 if ((calc > rack->r_ctl.current_policer_bucket) || 18857 (len >= (rack->r_ctl.current_policer_bucket - calc))) { 18858 /* 18859 * We may want to pace depending on if we are going 18860 * into the reserve or not. 18861 */ 18862 uint32_t newlen; 18863 18864 if (calc > rack->r_ctl.current_policer_bucket) { 18865 /* 18866 * This will eat into the reserve if we 18867 * don't have room at all some lines 18868 * below will catch it. 18869 */ 18870 newlen = rack->r_ctl.policer_max_seg; 18871 rack->rc_policer_should_pace = 1; 18872 } else { 18873 /* 18874 * We have all of the reserve plus something in the bucket 18875 * that we can give out. 18876 */ 18877 newlen = rack->r_ctl.current_policer_bucket - calc; 18878 if (newlen < rack->r_ctl.policer_max_seg) { 18879 /* 18880 * Into the reserve to get a full policer_max_seg 18881 * so we set the len to that and eat into 18882 * the reserve. If we go over the code 18883 * below will make us wait. 18884 */ 18885 newlen = rack->r_ctl.policer_max_seg; 18886 rack->rc_policer_should_pace = 1; 18887 } 18888 } 18889 if (newlen > rack->r_ctl.current_policer_bucket) { 18890 /* We have to wait some */ 18891 *needs = newlen - rack->r_ctl.current_policer_bucket; 18892 return (0); 18893 } 18894 if (rack_verbose_logging > 0) 18895 policer_detection_log(rack, len, segsiz, newlen, 0, 9); 18896 len = newlen; 18897 } /* else we have all len available above the reserve */ 18898 if (rack_verbose_logging > 0) 18899 policer_detection_log(rack, len, segsiz, calc, 0, 10); 18900 return (len); 18901 } 18902 18903 static uint32_t 18904 rack_policed_sending(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, uint32_t segsiz, int call_line) 18905 { 18906 /* 18907 * Given a send of len, and a token bucket set at current_policer_bucket_size 18908 * are we close enough to the end of the bucket that we need to pace? If so 18909 * calculate out a time and return it. Otherwise subtract the tokens from 18910 * the bucket. 18911 */ 18912 uint64_t calc; 18913 18914 if ((rack->r_ctl.policer_bw == 0) || 18915 (rack->r_ctl.policer_bucket_size < segsiz)) { 18916 /* 18917 * We should have an estimate here... 18918 */ 18919 return (0); 18920 } 18921 calc = (uint64_t)rack_policer_bucket_reserve * (uint64_t)rack->r_ctl.policer_bucket_size; 18922 calc /= 100; 18923 if ((rack->r_ctl.current_policer_bucket < len) || 18924 (rack->rc_policer_should_pace == 1) || 18925 ((rack->r_ctl.current_policer_bucket - len) <= (uint32_t)calc)) { 18926 /* we need to pace */ 18927 uint64_t lentim, res; 18928 uint32_t slot; 18929 18930 lentim = (uint64_t)len * (uint64_t)HPTS_USEC_IN_SEC; 18931 res = lentim / rack->r_ctl.policer_bw; 18932 slot = (uint32_t)res; 18933 if (rack->r_ctl.current_policer_bucket > len) 18934 rack->r_ctl.current_policer_bucket -= len; 18935 else 18936 rack->r_ctl.current_policer_bucket = 0; 18937 policer_detection_log(rack, len, slot, (uint32_t)rack_policer_bucket_reserve, call_line, 5); 18938 rack->rc_policer_should_pace = 0; 18939 return(slot); 18940 } 18941 /* Just take tokens out of the bucket and let rack do whatever it would have */ 18942 policer_detection_log(rack, len, 0, (uint32_t)rack_policer_bucket_reserve, call_line, 6); 18943 if (len < rack->r_ctl.current_policer_bucket) { 18944 rack->r_ctl.current_policer_bucket -= len; 18945 } else { 18946 rack->r_ctl.current_policer_bucket = 0; 18947 } 18948 return (0); 18949 } 18950 18951 18952 static int32_t 18953 rack_get_pacing_delay(struct tcp_rack *rack, struct tcpcb *tp, uint32_t len, struct rack_sendmap *rsm, uint32_t segsiz, int line) 18954 { 18955 uint64_t srtt; 18956 int32_t slot = 0; 18957 int32_t minslot = 0; 18958 int can_start_hw_pacing = 1; 18959 int err; 18960 int pace_one; 18961 18962 if (rack_pace_one_seg || 18963 (rack->r_ctl.rc_user_set_min_segs == 1)) 18964 pace_one = 1; 18965 else 18966 pace_one = 0; 18967 if (rack->rc_policer_detected == 1) { 18968 /* 18969 * A policer has been detected and we 18970 * have all of our data (policer-bw and 18971 * policer bucket size) calculated. Call 18972 * into the function to find out if we are 18973 * overriding the time. 18974 */ 18975 slot = rack_policed_sending(rack, tp, len, segsiz, line); 18976 if (slot) { 18977 uint64_t logbw; 18978 18979 logbw = rack->r_ctl.current_policer_bucket; 18980 logbw <<= 32; 18981 logbw |= rack->r_ctl.policer_bucket_size; 18982 rack_log_pacing_delay_calc(rack, len, slot, rack->r_ctl.policer_bw, logbw, 0, 89, __LINE__, NULL, 0); 18983 return(slot); 18984 } 18985 } 18986 if (rack->rc_always_pace == 0) { 18987 /* 18988 * We use the most optimistic possible cwnd/srtt for 18989 * sending calculations. This will make our 18990 * calculation anticipate getting more through 18991 * quicker then possible. But thats ok we don't want 18992 * the peer to have a gap in data sending. 18993 */ 18994 uint64_t cwnd, tr_perms = 0; 18995 int32_t reduce = 0; 18996 18997 old_method: 18998 /* 18999 * We keep no precise pacing with the old method 19000 * instead we use the pacer to mitigate bursts. 19001 */ 19002 if (rack->r_ctl.rc_rack_min_rtt) 19003 srtt = rack->r_ctl.rc_rack_min_rtt; 19004 else 19005 srtt = max(tp->t_srtt, 1); 19006 if (rack->r_ctl.rc_rack_largest_cwnd) 19007 cwnd = rack->r_ctl.rc_rack_largest_cwnd; 19008 else 19009 cwnd = rack->r_ctl.cwnd_to_use; 19010 /* Inflate cwnd by 1000 so srtt of usecs is in ms */ 19011 tr_perms = (cwnd * 1000) / srtt; 19012 if (tr_perms == 0) { 19013 tr_perms = ctf_fixed_maxseg(tp); 19014 } 19015 /* 19016 * Calculate how long this will take to drain, if 19017 * the calculation comes out to zero, thats ok we 19018 * will use send_a_lot to possibly spin around for 19019 * more increasing tot_len_this_send to the point 19020 * that its going to require a pace, or we hit the 19021 * cwnd. Which in that case we are just waiting for 19022 * a ACK. 19023 */ 19024 slot = len / tr_perms; 19025 /* Now do we reduce the time so we don't run dry? */ 19026 if (slot && rack_slot_reduction) { 19027 reduce = (slot / rack_slot_reduction); 19028 if (reduce < slot) { 19029 slot -= reduce; 19030 } else 19031 slot = 0; 19032 } 19033 slot *= HPTS_USEC_IN_MSEC; 19034 if (rack->rc_pace_to_cwnd) { 19035 uint64_t rate_wanted = 0; 19036 19037 slot = pace_to_fill_cwnd(rack, slot, len, segsiz, NULL, &rate_wanted, 1); 19038 rack->rc_ack_can_sendout_data = 1; 19039 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, 0, 0, 14, __LINE__, NULL, 0); 19040 } else 19041 rack_log_pacing_delay_calc(rack, len, slot, tr_perms, reduce, 0, 7, __LINE__, NULL, 0); 19042 /*******************************************************/ 19043 /* RRS: We insert non-paced call to stats here for len */ 19044 /*******************************************************/ 19045 } else { 19046 uint64_t bw_est, res, lentim, rate_wanted; 19047 uint32_t segs, oh; 19048 int capped = 0; 19049 int prev_fill; 19050 19051 if ((rack->r_rr_config == 1) && rsm) { 19052 return (rack->r_ctl.rc_min_to); 19053 } 19054 if (rack->use_fixed_rate) { 19055 rate_wanted = bw_est = rack_get_fixed_pacing_bw(rack); 19056 } else if ((rack->r_ctl.init_rate == 0) && 19057 (rack->r_ctl.gp_bw == 0)) { 19058 /* no way to yet do an estimate */ 19059 bw_est = rate_wanted = 0; 19060 } else if (rack->dgp_on) { 19061 bw_est = rack_get_bw(rack); 19062 rate_wanted = rack_get_output_bw(rack, bw_est, rsm, &capped); 19063 } else { 19064 uint32_t gain, rate_set = 0; 19065 19066 rate_wanted = min(rack->rc_tp->snd_cwnd, rack->r_ctl.cwnd_to_use); 19067 rate_wanted = rack_arrive_at_discounted_rate(rack, rate_wanted, &rate_set, &gain); 19068 if (rate_set == 0) { 19069 if (rate_wanted > rack->rc_tp->snd_wnd) 19070 rate_wanted = rack->rc_tp->snd_wnd; 19071 /* Now lets make it into a b/w */ 19072 rate_wanted *= (uint64_t)HPTS_USEC_IN_SEC; 19073 rate_wanted /= (uint64_t)rack->r_ctl.rc_last_us_rtt; 19074 } 19075 bw_est = rate_wanted; 19076 rack_log_pacing_delay_calc(rack, rack->rc_tp->snd_cwnd, 19077 rack->r_ctl.cwnd_to_use, 19078 rate_wanted, bw_est, 19079 rack->r_ctl.rc_last_us_rtt, 19080 88, __LINE__, NULL, gain); 19081 } 19082 if ((bw_est == 0) || (rate_wanted == 0) || 19083 ((rack->gp_ready == 0) && (rack->use_fixed_rate == 0))) { 19084 /* 19085 * No way yet to make a b/w estimate or 19086 * our raise is set incorrectly. 19087 */ 19088 goto old_method; 19089 } 19090 rack_rate_cap_bw(rack, &rate_wanted, &capped); 19091 /* We need to account for all the overheads */ 19092 segs = (len + segsiz - 1) / segsiz; 19093 /* 19094 * We need the diff between 1514 bytes (e-mtu with e-hdr) 19095 * and how much data we put in each packet. Yes this 19096 * means we may be off if we are larger than 1500 bytes 19097 * or smaller. But this just makes us more conservative. 19098 */ 19099 19100 oh = (tp->t_maxseg - segsiz) + sizeof(struct tcphdr); 19101 if (rack->r_is_v6) { 19102 #ifdef INET6 19103 oh += sizeof(struct ip6_hdr); 19104 #endif 19105 } else { 19106 #ifdef INET 19107 oh += sizeof(struct ip); 19108 #endif 19109 } 19110 /* We add a fixed 14 for the ethernet header */ 19111 oh += 14; 19112 segs *= oh; 19113 lentim = (uint64_t)(len + segs) * (uint64_t)HPTS_USEC_IN_SEC; 19114 res = lentim / rate_wanted; 19115 slot = (uint32_t)res; 19116 if (rack_hw_rate_min && 19117 (rate_wanted < rack_hw_rate_min)) { 19118 can_start_hw_pacing = 0; 19119 if (rack->r_ctl.crte) { 19120 /* 19121 * Ok we need to release it, we 19122 * have fallen too low. 19123 */ 19124 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 19125 rack->r_ctl.crte = NULL; 19126 rack->rack_attempt_hdwr_pace = 0; 19127 rack->rack_hdrw_pacing = 0; 19128 } 19129 } 19130 if (rack->r_ctl.crte && 19131 (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) { 19132 /* 19133 * We want more than the hardware can give us, 19134 * don't start any hw pacing. 19135 */ 19136 can_start_hw_pacing = 0; 19137 if (rack->r_rack_hw_rate_caps == 0) { 19138 /* 19139 * Ok we need to release it, we 19140 * want more than the card can give us and 19141 * no rate cap is in place. Set it up so 19142 * when we want less we can retry. 19143 */ 19144 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 19145 rack->r_ctl.crte = NULL; 19146 rack->rack_attempt_hdwr_pace = 0; 19147 rack->rack_hdrw_pacing = 0; 19148 } 19149 } 19150 if ((rack->r_ctl.crte != NULL) && (rack->rc_inp->inp_snd_tag == NULL)) { 19151 /* 19152 * We lost our rate somehow, this can happen 19153 * if the interface changed underneath us. 19154 */ 19155 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 19156 rack->r_ctl.crte = NULL; 19157 /* Lets re-allow attempting to setup pacing */ 19158 rack->rack_hdrw_pacing = 0; 19159 rack->rack_attempt_hdwr_pace = 0; 19160 rack_log_hdwr_pacing(rack, 19161 rate_wanted, bw_est, __LINE__, 19162 0, 6); 19163 } 19164 prev_fill = rack->r_via_fill_cw; 19165 if ((rack->rc_pace_to_cwnd) && 19166 (capped == 0) && 19167 (rack->dgp_on == 1) && 19168 (rack->use_fixed_rate == 0) && 19169 (rack->in_probe_rtt == 0) && 19170 (IN_FASTRECOVERY(rack->rc_tp->t_flags) == 0)) { 19171 /* 19172 * We want to pace at our rate *or* faster to 19173 * fill the cwnd to the max if its not full. 19174 */ 19175 slot = pace_to_fill_cwnd(rack, slot, (len+segs), segsiz, &capped, &rate_wanted, 0); 19176 /* Re-check to make sure we are not exceeding our max b/w */ 19177 if ((rack->r_ctl.crte != NULL) && 19178 (tcp_hw_highest_rate(rack->r_ctl.crte) < rate_wanted)) { 19179 /* 19180 * We want more than the hardware can give us, 19181 * don't start any hw pacing. 19182 */ 19183 can_start_hw_pacing = 0; 19184 if (rack->r_rack_hw_rate_caps == 0) { 19185 /* 19186 * Ok we need to release it, we 19187 * want more than the card can give us and 19188 * no rate cap is in place. Set it up so 19189 * when we want less we can retry. 19190 */ 19191 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 19192 rack->r_ctl.crte = NULL; 19193 rack->rack_attempt_hdwr_pace = 0; 19194 rack->rack_hdrw_pacing = 0; 19195 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 19196 } 19197 } 19198 } 19199 if ((rack->rc_inp->inp_route.ro_nh != NULL) && 19200 (rack->rc_inp->inp_route.ro_nh->nh_ifp != NULL)) { 19201 if ((rack->rack_hdw_pace_ena) && 19202 (can_start_hw_pacing > 0) && 19203 (rack->rack_hdrw_pacing == 0) && 19204 (rack->rack_attempt_hdwr_pace == 0)) { 19205 /* 19206 * Lets attempt to turn on hardware pacing 19207 * if we can. 19208 */ 19209 rack->rack_attempt_hdwr_pace = 1; 19210 rack->r_ctl.crte = tcp_set_pacing_rate(rack->rc_tp, 19211 rack->rc_inp->inp_route.ro_nh->nh_ifp, 19212 rate_wanted, 19213 RS_PACING_GEQ, 19214 &err, &rack->r_ctl.crte_prev_rate); 19215 if (rack->r_ctl.crte) { 19216 rack->rack_hdrw_pacing = 1; 19217 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, segsiz, 19218 pace_one, rack->r_ctl.crte, 19219 NULL, rack->r_ctl.pace_len_divisor); 19220 rack_log_hdwr_pacing(rack, 19221 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 19222 err, 0); 19223 rack->r_ctl.last_hw_bw_req = rate_wanted; 19224 } else { 19225 counter_u64_add(rack_hw_pace_init_fail, 1); 19226 } 19227 } else if (rack->rack_hdrw_pacing && 19228 (rack->r_ctl.last_hw_bw_req != rate_wanted)) { 19229 /* Do we need to adjust our rate? */ 19230 const struct tcp_hwrate_limit_table *nrte; 19231 19232 if (rack->r_up_only && 19233 (rate_wanted < rack->r_ctl.crte->rate)) { 19234 /** 19235 * We have four possible states here 19236 * having to do with the previous time 19237 * and this time. 19238 * previous | this-time 19239 * A) 0 | 0 -- fill_cw not in the picture 19240 * B) 1 | 0 -- we were doing a fill-cw but now are not 19241 * C) 1 | 1 -- all rates from fill_cw 19242 * D) 0 | 1 -- we were doing non-fill and now we are filling 19243 * 19244 * For case A, C and D we don't allow a drop. But for 19245 * case B where we now our on our steady rate we do 19246 * allow a drop. 19247 * 19248 */ 19249 if (!((prev_fill == 1) && (rack->r_via_fill_cw == 0))) 19250 goto done_w_hdwr; 19251 } 19252 if ((rate_wanted > rack->r_ctl.crte->rate) || 19253 (rate_wanted <= rack->r_ctl.crte_prev_rate)) { 19254 if (rack_hw_rate_to_low && 19255 (bw_est < rack_hw_rate_to_low)) { 19256 /* 19257 * The pacing rate is too low for hardware, but 19258 * do allow hardware pacing to be restarted. 19259 */ 19260 rack_log_hdwr_pacing(rack, 19261 bw_est, rack->r_ctl.crte->rate, __LINE__, 19262 0, 5); 19263 tcp_rel_pacing_rate(rack->r_ctl.crte, rack->rc_tp); 19264 rack->r_ctl.crte = NULL; 19265 rack->rack_attempt_hdwr_pace = 0; 19266 rack->rack_hdrw_pacing = 0; 19267 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 19268 goto done_w_hdwr; 19269 } 19270 nrte = tcp_chg_pacing_rate(rack->r_ctl.crte, 19271 rack->rc_tp, 19272 rack->rc_inp->inp_route.ro_nh->nh_ifp, 19273 rate_wanted, 19274 RS_PACING_GEQ, 19275 &err, &rack->r_ctl.crte_prev_rate); 19276 if (nrte == NULL) { 19277 /* 19278 * Lost the rate, lets drop hardware pacing 19279 * period. 19280 */ 19281 rack->rack_hdrw_pacing = 0; 19282 rack->r_ctl.crte = NULL; 19283 rack_log_hdwr_pacing(rack, 19284 rate_wanted, 0, __LINE__, 19285 err, 1); 19286 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 19287 counter_u64_add(rack_hw_pace_lost, 1); 19288 } else if (nrte != rack->r_ctl.crte) { 19289 rack->r_ctl.crte = nrte; 19290 rack->r_ctl.rc_pace_max_segs = tcp_get_pacing_burst_size_w_divisor(tp, rate_wanted, 19291 segsiz, pace_one, rack->r_ctl.crte, 19292 NULL, rack->r_ctl.pace_len_divisor); 19293 rack_log_hdwr_pacing(rack, 19294 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 19295 err, 2); 19296 rack->r_ctl.last_hw_bw_req = rate_wanted; 19297 } 19298 } else { 19299 /* We just need to adjust the segment size */ 19300 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, &rate_wanted); 19301 rack_log_hdwr_pacing(rack, 19302 rate_wanted, rack->r_ctl.crte->rate, __LINE__, 19303 0, 4); 19304 rack->r_ctl.last_hw_bw_req = rate_wanted; 19305 } 19306 } 19307 } 19308 if (minslot && (minslot > slot)) { 19309 rack_log_pacing_delay_calc(rack, minslot, slot, rack->r_ctl.crte->rate, bw_est, lentim, 19310 98, __LINE__, NULL, 0); 19311 slot = minslot; 19312 } 19313 done_w_hdwr: 19314 if (rack_limit_time_with_srtt && 19315 (rack->use_fixed_rate == 0) && 19316 (rack->rack_hdrw_pacing == 0)) { 19317 /* 19318 * Sanity check, we do not allow the pacing delay 19319 * to be longer than the SRTT of the path. If it is 19320 * a slow path, then adding a packet should increase 19321 * the RTT and compensate for this i.e. the srtt will 19322 * be greater so the allowed pacing time will be greater. 19323 * 19324 * Note this restriction is not for where a peak rate 19325 * is set, we are doing fixed pacing or hardware pacing. 19326 */ 19327 if (rack->rc_tp->t_srtt) 19328 srtt = rack->rc_tp->t_srtt; 19329 else 19330 srtt = RACK_INITIAL_RTO * HPTS_USEC_IN_MSEC; /* its in ms convert */ 19331 if (srtt < (uint64_t)slot) { 19332 rack_log_pacing_delay_calc(rack, srtt, slot, rate_wanted, bw_est, lentim, 99, __LINE__, NULL, 0); 19333 slot = srtt; 19334 } 19335 } 19336 /*******************************************************************/ 19337 /* RRS: We insert paced call to stats here for len and rate_wanted */ 19338 /*******************************************************************/ 19339 rack_log_pacing_delay_calc(rack, len, slot, rate_wanted, bw_est, lentim, 2, __LINE__, rsm, 0); 19340 } 19341 if (rack->r_ctl.crte && (rack->r_ctl.crte->rs_num_enobufs > 0)) { 19342 /* 19343 * If this rate is seeing enobufs when it 19344 * goes to send then either the nic is out 19345 * of gas or we are mis-estimating the time 19346 * somehow and not letting the queue empty 19347 * completely. Lets add to the pacing time. 19348 */ 19349 int hw_boost_delay; 19350 19351 hw_boost_delay = rack->r_ctl.crte->time_between * rack_enobuf_hw_boost_mult; 19352 if (hw_boost_delay > rack_enobuf_hw_max) 19353 hw_boost_delay = rack_enobuf_hw_max; 19354 else if (hw_boost_delay < rack_enobuf_hw_min) 19355 hw_boost_delay = rack_enobuf_hw_min; 19356 slot += hw_boost_delay; 19357 } 19358 return (slot); 19359 } 19360 19361 static void 19362 rack_start_gp_measurement(struct tcpcb *tp, struct tcp_rack *rack, 19363 tcp_seq startseq, uint32_t sb_offset) 19364 { 19365 struct rack_sendmap *my_rsm = NULL; 19366 19367 if (tp->t_state < TCPS_ESTABLISHED) { 19368 /* 19369 * We don't start any measurements if we are 19370 * not at least established. 19371 */ 19372 return; 19373 } 19374 if (tp->t_state >= TCPS_FIN_WAIT_1) { 19375 /* 19376 * We will get no more data into the SB 19377 * this means we need to have the data available 19378 * before we start a measurement. 19379 */ 19380 19381 if (sbavail(&tptosocket(tp)->so_snd) < 19382 max(rc_init_window(rack), 19383 (MIN_GP_WIN * ctf_fixed_maxseg(tp)))) { 19384 /* Nope not enough data */ 19385 return; 19386 } 19387 } 19388 tp->t_flags |= TF_GPUTINPROG; 19389 rack->r_ctl.rc_gp_cumack_ts = 0; 19390 rack->r_ctl.rc_gp_lowrtt = 0xffffffff; 19391 rack->r_ctl.rc_gp_high_rwnd = rack->rc_tp->snd_wnd; 19392 tp->gput_seq = startseq; 19393 rack->app_limited_needs_set = 0; 19394 if (rack->in_probe_rtt) 19395 rack->measure_saw_probe_rtt = 1; 19396 else if ((rack->measure_saw_probe_rtt) && 19397 (SEQ_GEQ(tp->gput_seq, rack->r_ctl.rc_probertt_sndmax_atexit))) 19398 rack->measure_saw_probe_rtt = 0; 19399 if (rack->rc_gp_filled) 19400 tp->gput_ts = rack->r_ctl.last_cumack_advance; 19401 else { 19402 /* Special case initial measurement */ 19403 struct timeval tv; 19404 19405 tp->gput_ts = tcp_get_usecs(&tv); 19406 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 19407 } 19408 /* 19409 * We take a guess out into the future, 19410 * if we have no measurement and no 19411 * initial rate, we measure the first 19412 * initial-windows worth of data to 19413 * speed up getting some GP measurement and 19414 * thus start pacing. 19415 */ 19416 if ((rack->rc_gp_filled == 0) && (rack->r_ctl.init_rate == 0)) { 19417 rack->app_limited_needs_set = 1; 19418 tp->gput_ack = startseq + max(rc_init_window(rack), 19419 (MIN_GP_WIN * ctf_fixed_maxseg(tp))); 19420 rack_log_pacing_delay_calc(rack, 19421 tp->gput_seq, 19422 tp->gput_ack, 19423 0, 19424 tp->gput_ts, 19425 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts), 19426 9, 19427 __LINE__, NULL, 0); 19428 rack_tend_gp_marks(tp, rack); 19429 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL); 19430 return; 19431 } 19432 if (sb_offset) { 19433 /* 19434 * We are out somewhere in the sb 19435 * can we use the already outstanding data? 19436 */ 19437 19438 if (rack->r_ctl.rc_app_limited_cnt == 0) { 19439 /* 19440 * Yes first one is good and in this case 19441 * the tp->gput_ts is correctly set based on 19442 * the last ack that arrived (no need to 19443 * set things up when an ack comes in). 19444 */ 19445 my_rsm = tqhash_min(rack->r_ctl.tqh); 19446 if ((my_rsm == NULL) || 19447 (my_rsm->r_rtr_cnt != 1)) { 19448 /* retransmission? */ 19449 goto use_latest; 19450 } 19451 } else { 19452 if (rack->r_ctl.rc_first_appl == NULL) { 19453 /* 19454 * If rc_first_appl is NULL 19455 * then the cnt should be 0. 19456 * This is probably an error, maybe 19457 * a KASSERT would be approprate. 19458 */ 19459 goto use_latest; 19460 } 19461 /* 19462 * If we have a marker pointer to the last one that is 19463 * app limited we can use that, but we need to set 19464 * things up so that when it gets ack'ed we record 19465 * the ack time (if its not already acked). 19466 */ 19467 rack->app_limited_needs_set = 1; 19468 /* 19469 * We want to get to the rsm that is either 19470 * next with space i.e. over 1 MSS or the one 19471 * after that (after the app-limited). 19472 */ 19473 my_rsm = tqhash_next(rack->r_ctl.tqh, rack->r_ctl.rc_first_appl); 19474 if (my_rsm) { 19475 if ((my_rsm->r_end - my_rsm->r_start) <= ctf_fixed_maxseg(tp)) 19476 /* Have to use the next one */ 19477 my_rsm = tqhash_next(rack->r_ctl.tqh, my_rsm); 19478 else { 19479 /* Use after the first MSS of it is acked */ 19480 tp->gput_seq = my_rsm->r_start + ctf_fixed_maxseg(tp); 19481 goto start_set; 19482 } 19483 } 19484 if ((my_rsm == NULL) || 19485 (my_rsm->r_rtr_cnt != 1)) { 19486 /* 19487 * Either its a retransmit or 19488 * the last is the app-limited one. 19489 */ 19490 goto use_latest; 19491 } 19492 } 19493 tp->gput_seq = my_rsm->r_start; 19494 start_set: 19495 if (my_rsm->r_flags & RACK_ACKED) { 19496 /* 19497 * This one has been acked use the arrival ack time 19498 */ 19499 struct rack_sendmap *nrsm; 19500 19501 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival; 19502 rack->app_limited_needs_set = 0; 19503 /* 19504 * Ok in this path we need to use the r_end now 19505 * since this guy is the starting ack. 19506 */ 19507 tp->gput_seq = my_rsm->r_end; 19508 /* 19509 * We also need to adjust up the sendtime 19510 * to the send of the next data after my_rsm. 19511 */ 19512 nrsm = tqhash_next(rack->r_ctl.tqh, my_rsm); 19513 if (nrsm != NULL) 19514 my_rsm = nrsm; 19515 else { 19516 /* 19517 * The next as not been sent, thats the 19518 * case for using the latest. 19519 */ 19520 goto use_latest; 19521 } 19522 } 19523 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0]; 19524 tp->gput_ack = tp->gput_seq + rack_get_measure_window(tp, rack); 19525 rack->r_ctl.rc_gp_cumack_ts = 0; 19526 if ((rack->r_ctl.cleared_app_ack == 1) && 19527 (SEQ_GEQ(rack->r_ctl.cleared_app_ack, tp->gput_seq))) { 19528 /* 19529 * We just cleared an application limited period 19530 * so the next seq out needs to skip the first 19531 * ack. 19532 */ 19533 rack->app_limited_needs_set = 1; 19534 rack->r_ctl.cleared_app_ack = 0; 19535 } 19536 rack_log_pacing_delay_calc(rack, 19537 tp->gput_seq, 19538 tp->gput_ack, 19539 (uint64_t)my_rsm, 19540 tp->gput_ts, 19541 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts), 19542 9, 19543 __LINE__, my_rsm, 0); 19544 /* Now lets make sure all are marked as they should be */ 19545 rack_tend_gp_marks(tp, rack); 19546 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL); 19547 return; 19548 } 19549 19550 use_latest: 19551 /* 19552 * We don't know how long we may have been 19553 * idle or if this is the first-send. Lets 19554 * setup the flag so we will trim off 19555 * the first ack'd data so we get a true 19556 * measurement. 19557 */ 19558 rack->app_limited_needs_set = 1; 19559 tp->gput_ack = startseq + rack_get_measure_window(tp, rack); 19560 rack->r_ctl.rc_gp_cumack_ts = 0; 19561 /* Find this guy so we can pull the send time */ 19562 my_rsm = tqhash_find(rack->r_ctl.tqh, startseq); 19563 if (my_rsm) { 19564 rack->r_ctl.rc_gp_output_ts = my_rsm->r_tim_lastsent[0]; 19565 if (my_rsm->r_flags & RACK_ACKED) { 19566 /* 19567 * Unlikely since its probably what was 19568 * just transmitted (but I am paranoid). 19569 */ 19570 tp->gput_ts = (uint32_t)my_rsm->r_ack_arrival; 19571 rack->app_limited_needs_set = 0; 19572 } 19573 if (SEQ_LT(my_rsm->r_start, tp->gput_seq)) { 19574 /* This also is unlikely */ 19575 tp->gput_seq = my_rsm->r_start; 19576 } 19577 } else { 19578 /* 19579 * TSNH unless we have some send-map limit, 19580 * and even at that it should not be hitting 19581 * that limit (we should have stopped sending). 19582 */ 19583 struct timeval tv; 19584 19585 microuptime(&tv); 19586 rack->r_ctl.rc_gp_output_ts = rack_to_usec_ts(&tv); 19587 } 19588 rack_tend_gp_marks(tp, rack); 19589 rack_log_pacing_delay_calc(rack, 19590 tp->gput_seq, 19591 tp->gput_ack, 19592 (uint64_t)my_rsm, 19593 tp->gput_ts, 19594 (((uint64_t)rack->r_ctl.rc_app_limited_cnt << 32) | (uint64_t)rack->r_ctl.rc_gp_output_ts), 19595 9, __LINE__, NULL, 0); 19596 rack_log_gpset(rack, tp->gput_ack, 0, 0, __LINE__, 1, NULL); 19597 } 19598 19599 static inline uint32_t 19600 rack_what_can_we_send(struct tcpcb *tp, struct tcp_rack *rack, uint32_t cwnd_to_use, 19601 uint32_t avail, int32_t sb_offset) 19602 { 19603 uint32_t len; 19604 uint32_t sendwin; 19605 19606 if (tp->snd_wnd > cwnd_to_use) 19607 sendwin = cwnd_to_use; 19608 else 19609 sendwin = tp->snd_wnd; 19610 if (ctf_outstanding(tp) >= tp->snd_wnd) { 19611 /* We never want to go over our peers rcv-window */ 19612 len = 0; 19613 } else { 19614 uint32_t flight; 19615 19616 flight = ctf_flight_size(tp, rack->r_ctl.rc_sacked); 19617 if (flight >= sendwin) { 19618 /* 19619 * We have in flight what we are allowed by cwnd (if 19620 * it was rwnd blocking it would have hit above out 19621 * >= tp->snd_wnd). 19622 */ 19623 return (0); 19624 } 19625 len = sendwin - flight; 19626 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 19627 /* We would send too much (beyond the rwnd) */ 19628 len = tp->snd_wnd - ctf_outstanding(tp); 19629 } 19630 if ((len + sb_offset) > avail) { 19631 /* 19632 * We don't have that much in the SB, how much is 19633 * there? 19634 */ 19635 len = avail - sb_offset; 19636 } 19637 } 19638 return (len); 19639 } 19640 19641 static void 19642 rack_log_fsb(struct tcp_rack *rack, struct tcpcb *tp, struct socket *so, uint32_t flags, 19643 unsigned ipoptlen, int32_t orig_len, int32_t len, int error, 19644 int rsm_is_null, int optlen, int line, uint16_t mode) 19645 { 19646 if (rack_verbose_logging && tcp_bblogging_on(rack->rc_tp)) { 19647 union tcp_log_stackspecific log; 19648 struct timeval tv; 19649 19650 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 19651 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 19652 log.u_bbr.flex1 = error; 19653 log.u_bbr.flex2 = flags; 19654 log.u_bbr.flex3 = rsm_is_null; 19655 log.u_bbr.flex4 = ipoptlen; 19656 log.u_bbr.flex5 = tp->rcv_numsacks; 19657 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 19658 log.u_bbr.flex7 = optlen; 19659 log.u_bbr.flex8 = rack->r_fsb_inited; 19660 log.u_bbr.applimited = rack->r_fast_output; 19661 log.u_bbr.bw_inuse = rack_get_bw(rack); 19662 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 19663 log.u_bbr.cwnd_gain = mode; 19664 log.u_bbr.pkts_out = orig_len; 19665 log.u_bbr.lt_epoch = len; 19666 log.u_bbr.delivered = line; 19667 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 19668 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 19669 tcp_log_event(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_FSB, 0, 19670 len, &log, false, NULL, __func__, __LINE__, &tv); 19671 } 19672 } 19673 19674 19675 static struct mbuf * 19676 rack_fo_base_copym(struct mbuf *the_m, uint32_t the_off, int32_t *plen, 19677 struct rack_fast_send_blk *fsb, 19678 int32_t seglimit, int32_t segsize, int hw_tls) 19679 { 19680 #ifdef KERN_TLS 19681 struct ktls_session *tls, *ntls; 19682 #ifdef INVARIANTS 19683 struct mbuf *start; 19684 #endif 19685 #endif 19686 struct mbuf *m, *n, **np, *smb; 19687 struct mbuf *top; 19688 int32_t off, soff; 19689 int32_t len = *plen; 19690 int32_t fragsize; 19691 int32_t len_cp = 0; 19692 uint32_t mlen, frags; 19693 19694 soff = off = the_off; 19695 smb = m = the_m; 19696 np = ⊤ 19697 top = NULL; 19698 #ifdef KERN_TLS 19699 if (hw_tls && (m->m_flags & M_EXTPG)) 19700 tls = m->m_epg_tls; 19701 else 19702 tls = NULL; 19703 #ifdef INVARIANTS 19704 start = m; 19705 #endif 19706 #endif 19707 while (len > 0) { 19708 if (m == NULL) { 19709 *plen = len_cp; 19710 break; 19711 } 19712 #ifdef KERN_TLS 19713 if (hw_tls) { 19714 if (m->m_flags & M_EXTPG) 19715 ntls = m->m_epg_tls; 19716 else 19717 ntls = NULL; 19718 19719 /* 19720 * Avoid mixing TLS records with handshake 19721 * data or TLS records from different 19722 * sessions. 19723 */ 19724 if (tls != ntls) { 19725 MPASS(m != start); 19726 *plen = len_cp; 19727 break; 19728 } 19729 } 19730 #endif 19731 mlen = min(len, m->m_len - off); 19732 if (seglimit) { 19733 /* 19734 * For M_EXTPG mbufs, add 3 segments 19735 * + 1 in case we are crossing page boundaries 19736 * + 2 in case the TLS hdr/trailer are used 19737 * It is cheaper to just add the segments 19738 * than it is to take the cache miss to look 19739 * at the mbuf ext_pgs state in detail. 19740 */ 19741 if (m->m_flags & M_EXTPG) { 19742 fragsize = min(segsize, PAGE_SIZE); 19743 frags = 3; 19744 } else { 19745 fragsize = segsize; 19746 frags = 0; 19747 } 19748 19749 /* Break if we really can't fit anymore. */ 19750 if ((frags + 1) >= seglimit) { 19751 *plen = len_cp; 19752 break; 19753 } 19754 19755 /* 19756 * Reduce size if you can't copy the whole 19757 * mbuf. If we can't copy the whole mbuf, also 19758 * adjust len so the loop will end after this 19759 * mbuf. 19760 */ 19761 if ((frags + howmany(mlen, fragsize)) >= seglimit) { 19762 mlen = (seglimit - frags - 1) * fragsize; 19763 len = mlen; 19764 *plen = len_cp + len; 19765 } 19766 frags += howmany(mlen, fragsize); 19767 if (frags == 0) 19768 frags++; 19769 seglimit -= frags; 19770 KASSERT(seglimit > 0, 19771 ("%s: seglimit went too low", __func__)); 19772 } 19773 n = m_get(M_NOWAIT, m->m_type); 19774 *np = n; 19775 if (n == NULL) 19776 goto nospace; 19777 n->m_len = mlen; 19778 soff += mlen; 19779 len_cp += n->m_len; 19780 if (m->m_flags & (M_EXT | M_EXTPG)) { 19781 n->m_data = m->m_data + off; 19782 mb_dupcl(n, m); 19783 } else { 19784 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 19785 (u_int)n->m_len); 19786 } 19787 len -= n->m_len; 19788 off = 0; 19789 m = m->m_next; 19790 np = &n->m_next; 19791 if (len || (soff == smb->m_len)) { 19792 /* 19793 * We have more so we move forward or 19794 * we have consumed the entire mbuf and 19795 * len has fell to 0. 19796 */ 19797 soff = 0; 19798 smb = m; 19799 } 19800 19801 } 19802 if (fsb != NULL) { 19803 fsb->m = smb; 19804 fsb->off = soff; 19805 if (smb) { 19806 /* 19807 * Save off the size of the mbuf. We do 19808 * this so that we can recognize when it 19809 * has been trimmed by sbcut() as acks 19810 * come in. 19811 */ 19812 fsb->o_m_len = smb->m_len; 19813 fsb->o_t_len = M_TRAILINGROOM(smb); 19814 } else { 19815 /* 19816 * This is the case where the next mbuf went to NULL. This 19817 * means with this copy we have sent everything in the sb. 19818 * In theory we could clear the fast_output flag, but lets 19819 * not since its possible that we could get more added 19820 * and acks that call the extend function which would let 19821 * us send more. 19822 */ 19823 fsb->o_m_len = 0; 19824 fsb->o_t_len = 0; 19825 } 19826 } 19827 return (top); 19828 nospace: 19829 if (top) 19830 m_freem(top); 19831 return (NULL); 19832 19833 } 19834 19835 /* 19836 * This is a copy of m_copym(), taking the TSO segment size/limit 19837 * constraints into account, and advancing the sndptr as it goes. 19838 */ 19839 static struct mbuf * 19840 rack_fo_m_copym(struct tcp_rack *rack, int32_t *plen, 19841 int32_t seglimit, int32_t segsize, struct mbuf **s_mb, int *s_soff) 19842 { 19843 struct mbuf *m, *n; 19844 int32_t soff; 19845 19846 m = rack->r_ctl.fsb.m; 19847 if (M_TRAILINGROOM(m) != rack->r_ctl.fsb.o_t_len) { 19848 /* 19849 * The trailing space changed, mbufs can grow 19850 * at the tail but they can't shrink from 19851 * it, KASSERT that. Adjust the orig_m_len to 19852 * compensate for this change. 19853 */ 19854 KASSERT((rack->r_ctl.fsb.o_t_len > M_TRAILINGROOM(m)), 19855 ("mbuf:%p rack:%p trailing_space:%jd ots:%u oml:%u mlen:%u\n", 19856 m, 19857 rack, 19858 (intmax_t)M_TRAILINGROOM(m), 19859 rack->r_ctl.fsb.o_t_len, 19860 rack->r_ctl.fsb.o_m_len, 19861 m->m_len)); 19862 rack->r_ctl.fsb.o_m_len += (rack->r_ctl.fsb.o_t_len - M_TRAILINGROOM(m)); 19863 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(m); 19864 } 19865 if (m->m_len < rack->r_ctl.fsb.o_m_len) { 19866 /* 19867 * Mbuf shrank, trimmed off the top by an ack, our 19868 * offset changes. 19869 */ 19870 KASSERT((rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len - m->m_len)), 19871 ("mbuf:%p len:%u rack:%p oml:%u soff:%u\n", 19872 m, m->m_len, 19873 rack, rack->r_ctl.fsb.o_m_len, 19874 rack->r_ctl.fsb.off)); 19875 19876 if (rack->r_ctl.fsb.off >= (rack->r_ctl.fsb.o_m_len- m->m_len)) 19877 rack->r_ctl.fsb.off -= (rack->r_ctl.fsb.o_m_len - m->m_len); 19878 else 19879 rack->r_ctl.fsb.off = 0; 19880 rack->r_ctl.fsb.o_m_len = m->m_len; 19881 #ifdef INVARIANTS 19882 } else if (m->m_len > rack->r_ctl.fsb.o_m_len) { 19883 panic("rack:%p m:%p m_len grew outside of t_space compensation", 19884 rack, m); 19885 #endif 19886 } 19887 soff = rack->r_ctl.fsb.off; 19888 KASSERT(soff >= 0, ("%s, negative off %d", __FUNCTION__, soff)); 19889 KASSERT(*plen >= 0, ("%s, negative len %d", __FUNCTION__, *plen)); 19890 KASSERT(soff < m->m_len, ("%s rack:%p len:%u m:%p m->m_len:%u < off?", 19891 __FUNCTION__, 19892 rack, *plen, m, m->m_len)); 19893 /* Save off the right location before we copy and advance */ 19894 *s_soff = soff; 19895 *s_mb = rack->r_ctl.fsb.m; 19896 n = rack_fo_base_copym(m, soff, plen, 19897 &rack->r_ctl.fsb, 19898 seglimit, segsize, rack->r_ctl.fsb.hw_tls); 19899 return (n); 19900 } 19901 19902 /* Log the buffer level */ 19903 static void 19904 rack_log_queue_level(struct tcpcb *tp, struct tcp_rack *rack, 19905 int len, struct timeval *tv, 19906 uint32_t cts) 19907 { 19908 uint32_t p_rate = 0, p_queue = 0, err = 0; 19909 union tcp_log_stackspecific log; 19910 19911 #ifdef RATELIMIT 19912 err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue); 19913 err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate); 19914 #endif 19915 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 19916 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 19917 log.u_bbr.flex1 = p_rate; 19918 log.u_bbr.flex2 = p_queue; 19919 log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using; 19920 log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs; 19921 log.u_bbr.flex6 = rack->r_ctl.crte->time_between; 19922 log.u_bbr.flex7 = 99; 19923 log.u_bbr.flex8 = 0; 19924 log.u_bbr.pkts_out = err; 19925 log.u_bbr.delRate = rack->r_ctl.crte->rate; 19926 log.u_bbr.timeStamp = cts; 19927 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 19928 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0, 19929 len, &log, false, NULL, __func__, __LINE__, tv); 19930 19931 } 19932 19933 static uint32_t 19934 rack_check_queue_level(struct tcp_rack *rack, struct tcpcb *tp, 19935 struct timeval *tv, uint32_t cts, int len, uint32_t segsiz) 19936 { 19937 uint64_t lentime = 0; 19938 #ifdef RATELIMIT 19939 uint32_t p_rate = 0, p_queue = 0, err; 19940 union tcp_log_stackspecific log; 19941 uint64_t bw; 19942 19943 err = in_pcbquery_txrlevel(rack->rc_inp, &p_queue); 19944 /* Failed or queue is zero */ 19945 if (err || (p_queue == 0)) { 19946 lentime = 0; 19947 goto out; 19948 } 19949 err = in_pcbquery_txrtlmt(rack->rc_inp, &p_rate); 19950 if (err) { 19951 lentime = 0; 19952 goto out; 19953 } 19954 /* 19955 * If we reach here we have some bytes in 19956 * the queue. The number returned is a value 19957 * between 0 and 0xffff where ffff is full 19958 * and 0 is empty. So how best to make this into 19959 * something usable? 19960 * 19961 * The "safer" way is lets take the b/w gotten 19962 * from the query (which should be our b/w rate) 19963 * and pretend that a full send (our rc_pace_max_segs) 19964 * is outstanding. We factor it so its as if a full 19965 * number of our MSS segment is terms of full 19966 * ethernet segments are outstanding. 19967 */ 19968 bw = p_rate / 8; 19969 if (bw) { 19970 lentime = (rack->r_ctl.rc_pace_max_segs / segsiz); 19971 lentime *= ETHERNET_SEGMENT_SIZE; 19972 lentime *= (uint64_t)HPTS_USEC_IN_SEC; 19973 lentime /= bw; 19974 } else { 19975 /* TSNH -- KASSERT? */ 19976 lentime = 0; 19977 } 19978 out: 19979 if (tcp_bblogging_on(tp)) { 19980 memset(&log, 0, sizeof(log)); 19981 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 19982 log.u_bbr.flex1 = p_rate; 19983 log.u_bbr.flex2 = p_queue; 19984 log.u_bbr.flex4 = (uint32_t)rack->r_ctl.crte->using; 19985 log.u_bbr.flex5 = (uint32_t)rack->r_ctl.crte->rs_num_enobufs; 19986 log.u_bbr.flex6 = rack->r_ctl.crte->time_between; 19987 log.u_bbr.flex7 = 99; 19988 log.u_bbr.flex8 = 0; 19989 log.u_bbr.pkts_out = err; 19990 log.u_bbr.delRate = rack->r_ctl.crte->rate; 19991 log.u_bbr.cur_del_rate = lentime; 19992 log.u_bbr.timeStamp = cts; 19993 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 19994 tcp_log_event(tp, NULL, NULL, NULL, BBR_LOG_HDWR_PACE, 0, 19995 len, &log, false, NULL, __func__, __LINE__,tv); 19996 } 19997 #endif 19998 return ((uint32_t)lentime); 19999 } 20000 20001 static int 20002 rack_fast_rsm_output(struct tcpcb *tp, struct tcp_rack *rack, struct rack_sendmap *rsm, 20003 uint64_t ts_val, uint32_t cts, uint32_t ms_cts, struct timeval *tv, int len, uint8_t doing_tlp) 20004 { 20005 /* 20006 * Enter the fast retransmit path. We are given that a sched_pin is 20007 * in place (if accounting is compliled in) and the cycle count taken 20008 * at the entry is in the ts_val. The concept her is that the rsm 20009 * now holds the mbuf offsets and such so we can directly transmit 20010 * without a lot of overhead, the len field is already set for 20011 * us to prohibit us from sending too much (usually its 1MSS). 20012 */ 20013 struct ip *ip = NULL; 20014 struct udphdr *udp = NULL; 20015 struct tcphdr *th = NULL; 20016 struct mbuf *m = NULL; 20017 struct inpcb *inp; 20018 uint8_t *cpto; 20019 struct tcp_log_buffer *lgb; 20020 #ifdef TCP_ACCOUNTING 20021 uint64_t crtsc; 20022 int cnt_thru = 1; 20023 #endif 20024 struct tcpopt to; 20025 u_char opt[TCP_MAXOLEN]; 20026 uint32_t hdrlen, optlen; 20027 int32_t slot, segsiz, max_val, tso = 0, error = 0, ulen = 0; 20028 uint16_t flags; 20029 uint32_t if_hw_tsomaxsegcount = 0, startseq; 20030 uint32_t if_hw_tsomaxsegsize; 20031 int32_t ip_sendflag = IP_NO_SND_TAG_RL; 20032 20033 #ifdef INET6 20034 struct ip6_hdr *ip6 = NULL; 20035 20036 if (rack->r_is_v6) { 20037 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 20038 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 20039 } else 20040 #endif /* INET6 */ 20041 { 20042 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 20043 hdrlen = sizeof(struct tcpiphdr); 20044 } 20045 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) { 20046 goto failed; 20047 } 20048 if (doing_tlp) { 20049 /* Its a TLP add the flag, it may already be there but be sure */ 20050 rsm->r_flags |= RACK_TLP; 20051 } else { 20052 /* If it was a TLP it is not not on this retransmit */ 20053 rsm->r_flags &= ~RACK_TLP; 20054 } 20055 startseq = rsm->r_start; 20056 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 20057 inp = rack->rc_inp; 20058 to.to_flags = 0; 20059 flags = tcp_outflags[tp->t_state]; 20060 if (flags & (TH_SYN|TH_RST)) { 20061 goto failed; 20062 } 20063 if (rsm->r_flags & RACK_HAS_FIN) { 20064 /* We can't send a FIN here */ 20065 goto failed; 20066 } 20067 if (flags & TH_FIN) { 20068 /* We never send a FIN */ 20069 flags &= ~TH_FIN; 20070 } 20071 if (tp->t_flags & TF_RCVD_TSTMP) { 20072 to.to_tsval = ms_cts + tp->ts_offset; 20073 to.to_tsecr = tp->ts_recent; 20074 to.to_flags = TOF_TS; 20075 } 20076 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 20077 /* TCP-MD5 (RFC2385). */ 20078 if (tp->t_flags & TF_SIGNATURE) 20079 to.to_flags |= TOF_SIGNATURE; 20080 #endif 20081 optlen = tcp_addoptions(&to, opt); 20082 hdrlen += optlen; 20083 udp = rack->r_ctl.fsb.udp; 20084 if (udp) 20085 hdrlen += sizeof(struct udphdr); 20086 if (rack->r_ctl.rc_pace_max_segs) 20087 max_val = rack->r_ctl.rc_pace_max_segs; 20088 else if (rack->rc_user_set_max_segs) 20089 max_val = rack->rc_user_set_max_segs * segsiz; 20090 else 20091 max_val = len; 20092 if ((tp->t_flags & TF_TSO) && 20093 V_tcp_do_tso && 20094 (len > segsiz) && 20095 (tp->t_port == 0)) 20096 tso = 1; 20097 #ifdef INET6 20098 if (MHLEN < hdrlen + max_linkhdr) 20099 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 20100 else 20101 #endif 20102 m = m_gethdr(M_NOWAIT, MT_DATA); 20103 if (m == NULL) 20104 goto failed; 20105 m->m_data += max_linkhdr; 20106 m->m_len = hdrlen; 20107 th = rack->r_ctl.fsb.th; 20108 /* Establish the len to send */ 20109 if (len > max_val) 20110 len = max_val; 20111 if ((tso) && (len + optlen > segsiz)) { 20112 uint32_t if_hw_tsomax; 20113 int32_t max_len; 20114 20115 /* extract TSO information */ 20116 if_hw_tsomax = tp->t_tsomax; 20117 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 20118 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 20119 /* 20120 * Check if we should limit by maximum payload 20121 * length: 20122 */ 20123 if (if_hw_tsomax != 0) { 20124 /* compute maximum TSO length */ 20125 max_len = (if_hw_tsomax - hdrlen - 20126 max_linkhdr); 20127 if (max_len <= 0) { 20128 goto failed; 20129 } else if (len > max_len) { 20130 len = max_len; 20131 } 20132 } 20133 if (len <= segsiz) { 20134 /* 20135 * In case there are too many small fragments don't 20136 * use TSO: 20137 */ 20138 tso = 0; 20139 } 20140 } else { 20141 tso = 0; 20142 } 20143 if ((tso == 0) && (len > segsiz)) 20144 len = segsiz; 20145 (void)tcp_get_usecs(tv); 20146 if ((len == 0) || 20147 (len <= MHLEN - hdrlen - max_linkhdr)) { 20148 goto failed; 20149 } 20150 th->th_seq = htonl(rsm->r_start); 20151 th->th_ack = htonl(tp->rcv_nxt); 20152 /* 20153 * The PUSH bit should only be applied 20154 * if the full retransmission is made. If 20155 * we are sending less than this is the 20156 * left hand edge and should not have 20157 * the PUSH bit. 20158 */ 20159 if ((rsm->r_flags & RACK_HAD_PUSH) && 20160 (len == (rsm->r_end - rsm->r_start))) 20161 flags |= TH_PUSH; 20162 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale)); 20163 if (th->th_win == 0) { 20164 tp->t_sndzerowin++; 20165 tp->t_flags |= TF_RXWIN0SENT; 20166 } else 20167 tp->t_flags &= ~TF_RXWIN0SENT; 20168 if (rsm->r_flags & RACK_TLP) { 20169 /* 20170 * TLP should not count in retran count, but 20171 * in its own bin 20172 */ 20173 counter_u64_add(rack_tlp_retran, 1); 20174 counter_u64_add(rack_tlp_retran_bytes, len); 20175 } else { 20176 tp->t_sndrexmitpack++; 20177 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 20178 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 20179 } 20180 #ifdef STATS 20181 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 20182 len); 20183 #endif 20184 if (rsm->m == NULL) 20185 goto failed; 20186 if (rsm->m && 20187 ((rsm->orig_m_len != rsm->m->m_len) || 20188 (M_TRAILINGROOM(rsm->m) != rsm->orig_t_space))) { 20189 /* Fix up the orig_m_len and possibly the mbuf offset */ 20190 rack_adjust_orig_mlen(rsm); 20191 } 20192 m->m_next = rack_fo_base_copym(rsm->m, rsm->soff, &len, NULL, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, rsm->r_hw_tls); 20193 if (len <= segsiz) { 20194 /* 20195 * Must have ran out of mbufs for the copy 20196 * shorten it to no longer need tso. Lets 20197 * not put on sendalot since we are low on 20198 * mbufs. 20199 */ 20200 tso = 0; 20201 } 20202 if ((m->m_next == NULL) || (len <= 0)){ 20203 goto failed; 20204 } 20205 if (udp) { 20206 if (rack->r_is_v6) 20207 ulen = hdrlen + len - sizeof(struct ip6_hdr); 20208 else 20209 ulen = hdrlen + len - sizeof(struct ip); 20210 udp->uh_ulen = htons(ulen); 20211 } 20212 m->m_pkthdr.rcvif = (struct ifnet *)0; 20213 if (TCPS_HAVERCVDSYN(tp->t_state) && 20214 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 20215 int ect = tcp_ecn_output_established(tp, &flags, len, true); 20216 if ((tp->t_state == TCPS_SYN_RECEIVED) && 20217 (tp->t_flags2 & TF2_ECN_SND_ECE)) 20218 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 20219 #ifdef INET6 20220 if (rack->r_is_v6) { 20221 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20); 20222 ip6->ip6_flow |= htonl(ect << 20); 20223 } 20224 else 20225 #endif 20226 { 20227 ip->ip_tos &= ~IPTOS_ECN_MASK; 20228 ip->ip_tos |= ect; 20229 } 20230 } 20231 if (rack->r_ctl.crte != NULL) { 20232 /* See if we can send via the hw queue */ 20233 slot = rack_check_queue_level(rack, tp, tv, cts, len, segsiz); 20234 /* If there is nothing in queue (no pacing time) we can send via the hw queue */ 20235 if (slot == 0) 20236 ip_sendflag = 0; 20237 } 20238 tcp_set_flags(th, flags); 20239 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 20240 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 20241 if (to.to_flags & TOF_SIGNATURE) { 20242 /* 20243 * Calculate MD5 signature and put it into the place 20244 * determined before. 20245 * NOTE: since TCP options buffer doesn't point into 20246 * mbuf's data, calculate offset and use it. 20247 */ 20248 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 20249 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 20250 /* 20251 * Do not send segment if the calculation of MD5 20252 * digest has failed. 20253 */ 20254 goto failed; 20255 } 20256 } 20257 #endif 20258 #ifdef INET6 20259 if (rack->r_is_v6) { 20260 if (tp->t_port) { 20261 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 20262 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 20263 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 20264 th->th_sum = htons(0); 20265 UDPSTAT_INC(udps_opackets); 20266 } else { 20267 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 20268 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 20269 th->th_sum = in6_cksum_pseudo(ip6, 20270 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 20271 0); 20272 } 20273 } 20274 #endif 20275 #if defined(INET6) && defined(INET) 20276 else 20277 #endif 20278 #ifdef INET 20279 { 20280 if (tp->t_port) { 20281 m->m_pkthdr.csum_flags = CSUM_UDP; 20282 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 20283 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 20284 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 20285 th->th_sum = htons(0); 20286 UDPSTAT_INC(udps_opackets); 20287 } else { 20288 m->m_pkthdr.csum_flags = CSUM_TCP; 20289 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 20290 th->th_sum = in_pseudo(ip->ip_src.s_addr, 20291 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 20292 IPPROTO_TCP + len + optlen)); 20293 } 20294 /* IP version must be set here for ipv4/ipv6 checking later */ 20295 KASSERT(ip->ip_v == IPVERSION, 20296 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 20297 } 20298 #endif 20299 if (tso) { 20300 /* 20301 * Here we use segsiz since we have no added options besides 20302 * any standard timestamp options (no DSACKs or SACKS are sent 20303 * via either fast-path). 20304 */ 20305 KASSERT(len > segsiz, 20306 ("%s: len <= tso_segsz tp:%p", __func__, tp)); 20307 m->m_pkthdr.csum_flags |= CSUM_TSO; 20308 m->m_pkthdr.tso_segsz = segsiz; 20309 } 20310 #ifdef INET6 20311 if (rack->r_is_v6) { 20312 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit; 20313 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 20314 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 20315 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 20316 else 20317 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 20318 } 20319 #endif 20320 #if defined(INET) && defined(INET6) 20321 else 20322 #endif 20323 #ifdef INET 20324 { 20325 ip->ip_len = htons(m->m_pkthdr.len); 20326 ip->ip_ttl = rack->r_ctl.fsb.hoplimit; 20327 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 20328 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 20329 if (tp->t_port == 0 || len < V_tcp_minmss) { 20330 ip->ip_off |= htons(IP_DF); 20331 } 20332 } else { 20333 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 20334 } 20335 } 20336 #endif 20337 if (doing_tlp == 0) { 20338 /* Set we retransmitted */ 20339 rack->rc_gp_saw_rec = 1; 20340 } else { 20341 /* Its a TLP set ca or ss */ 20342 if (tp->snd_cwnd > tp->snd_ssthresh) { 20343 /* Set we sent in CA */ 20344 rack->rc_gp_saw_ca = 1; 20345 } else { 20346 /* Set we sent in SS */ 20347 rack->rc_gp_saw_ss = 1; 20348 } 20349 } 20350 /* Time to copy in our header */ 20351 cpto = mtod(m, uint8_t *); 20352 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 20353 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 20354 if (optlen) { 20355 bcopy(opt, th + 1, optlen); 20356 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 20357 } else { 20358 th->th_off = sizeof(struct tcphdr) >> 2; 20359 } 20360 if (tcp_bblogging_on(rack->rc_tp)) { 20361 union tcp_log_stackspecific log; 20362 20363 if (rsm->r_flags & RACK_RWND_COLLAPSED) { 20364 rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm); 20365 counter_u64_add(rack_collapsed_win_rxt, 1); 20366 counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start)); 20367 } 20368 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 20369 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 20370 if (rack->rack_no_prr) 20371 log.u_bbr.flex1 = 0; 20372 else 20373 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 20374 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 20375 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 20376 log.u_bbr.flex4 = max_val; 20377 /* Save off the early/late values */ 20378 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 20379 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 20380 log.u_bbr.bw_inuse = rack_get_bw(rack); 20381 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw; 20382 if (doing_tlp == 0) 20383 log.u_bbr.flex8 = 1; 20384 else 20385 log.u_bbr.flex8 = 2; 20386 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 20387 log.u_bbr.flex7 = 55; 20388 log.u_bbr.pkts_out = tp->t_maxseg; 20389 log.u_bbr.timeStamp = cts; 20390 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 20391 if (rsm && (rsm->r_rtr_cnt > 0)) { 20392 /* 20393 * When we have a retransmit we want to log the 20394 * burst at send and flight at send from before. 20395 */ 20396 log.u_bbr.flex5 = rsm->r_fas; 20397 log.u_bbr.bbr_substate = rsm->r_bas; 20398 } else { 20399 /* 20400 * This is currently unlikely until we do the 20401 * packet pair probes but I will add it for completeness. 20402 */ 20403 log.u_bbr.flex5 = log.u_bbr.inflight; 20404 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz); 20405 } 20406 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use; 20407 log.u_bbr.delivered = 0; 20408 log.u_bbr.rttProp = (uint64_t)rsm; 20409 log.u_bbr.delRate = rsm->r_flags; 20410 log.u_bbr.delRate <<= 31; 20411 log.u_bbr.delRate |= rack->r_must_retran; 20412 log.u_bbr.delRate <<= 1; 20413 log.u_bbr.delRate |= 1; 20414 log.u_bbr.pkt_epoch = __LINE__; 20415 lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, 20416 len, &log, false, NULL, __func__, __LINE__, tv); 20417 } else 20418 lgb = NULL; 20419 if ((rack->r_ctl.crte != NULL) && 20420 tcp_bblogging_on(tp)) { 20421 rack_log_queue_level(tp, rack, len, tv, cts); 20422 } 20423 #ifdef INET6 20424 if (rack->r_is_v6) { 20425 error = ip6_output(m, inp->in6p_outputopts, 20426 &inp->inp_route6, 20427 ip_sendflag, NULL, NULL, inp); 20428 } 20429 else 20430 #endif 20431 #ifdef INET 20432 { 20433 error = ip_output(m, NULL, 20434 &inp->inp_route, 20435 ip_sendflag, 0, inp); 20436 } 20437 #endif 20438 m = NULL; 20439 if (lgb) { 20440 lgb->tlb_errno = error; 20441 lgb = NULL; 20442 } 20443 /* Move snd_nxt to snd_max so we don't have false retransmissions */ 20444 tp->snd_nxt = tp->snd_max; 20445 if (error) { 20446 goto failed; 20447 } else if (rack->rc_hw_nobuf && (ip_sendflag != IP_NO_SND_TAG_RL)) { 20448 rack->rc_hw_nobuf = 0; 20449 rack->r_ctl.rc_agg_delayed = 0; 20450 rack->r_early = 0; 20451 rack->r_late = 0; 20452 rack->r_ctl.rc_agg_early = 0; 20453 } 20454 rack_log_output(tp, &to, len, rsm->r_start, flags, error, rack_to_usec_ts(tv), 20455 rsm, RACK_SENT_FP, rsm->m, rsm->soff, rsm->r_hw_tls, segsiz); 20456 if (doing_tlp) { 20457 rack->rc_tlp_in_progress = 1; 20458 rack->r_ctl.rc_tlp_cnt_out++; 20459 } 20460 if (error == 0) { 20461 counter_u64_add(rack_total_bytes, len); 20462 tcp_account_for_send(tp, len, 1, doing_tlp, rsm->r_hw_tls); 20463 if (doing_tlp) { 20464 rack->rc_last_sent_tlp_past_cumack = 0; 20465 rack->rc_last_sent_tlp_seq_valid = 1; 20466 rack->r_ctl.last_sent_tlp_seq = rsm->r_start; 20467 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start; 20468 } 20469 if (rack->r_ctl.rc_prr_sndcnt >= len) 20470 rack->r_ctl.rc_prr_sndcnt -= len; 20471 else 20472 rack->r_ctl.rc_prr_sndcnt = 0; 20473 } 20474 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 20475 rack->forced_ack = 0; /* If we send something zap the FA flag */ 20476 if (IN_FASTRECOVERY(tp->t_flags) && rsm) 20477 rack->r_ctl.retran_during_recovery += len; 20478 { 20479 int idx; 20480 20481 idx = (len / segsiz) + 3; 20482 if (idx >= TCP_MSS_ACCT_ATIMER) 20483 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 20484 else 20485 counter_u64_add(rack_out_size[idx], 1); 20486 } 20487 if (tp->t_rtttime == 0) { 20488 tp->t_rtttime = ticks; 20489 tp->t_rtseq = startseq; 20490 KMOD_TCPSTAT_INC(tcps_segstimed); 20491 } 20492 counter_u64_add(rack_fto_rsm_send, 1); 20493 if (error && (error == ENOBUFS)) { 20494 if (rack->r_ctl.crte != NULL) { 20495 tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF); 20496 if (tcp_bblogging_on(rack->rc_tp)) 20497 rack_log_queue_level(tp, rack, len, tv, cts); 20498 } else 20499 tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF); 20500 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC); 20501 if (rack->rc_enobuf < 0x7f) 20502 rack->rc_enobuf++; 20503 if (slot < (10 * HPTS_USEC_IN_MSEC)) 20504 slot = 10 * HPTS_USEC_IN_MSEC; 20505 if (rack->r_ctl.crte != NULL) { 20506 counter_u64_add(rack_saw_enobuf_hw, 1); 20507 tcp_rl_log_enobuf(rack->r_ctl.crte); 20508 } 20509 counter_u64_add(rack_saw_enobuf, 1); 20510 } else { 20511 slot = rack_get_pacing_delay(rack, tp, len, NULL, segsiz, __LINE__); 20512 } 20513 rack_start_hpts_timer(rack, tp, cts, slot, len, 0); 20514 #ifdef TCP_ACCOUNTING 20515 crtsc = get_cyclecount(); 20516 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 20517 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru; 20518 } 20519 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 20520 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 20521 } 20522 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 20523 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((len + segsiz - 1) / segsiz); 20524 } 20525 sched_unpin(); 20526 #endif 20527 return (0); 20528 failed: 20529 if (m) 20530 m_free(m); 20531 return (-1); 20532 } 20533 20534 static void 20535 rack_sndbuf_autoscale(struct tcp_rack *rack) 20536 { 20537 /* 20538 * Automatic sizing of send socket buffer. Often the send buffer 20539 * size is not optimally adjusted to the actual network conditions 20540 * at hand (delay bandwidth product). Setting the buffer size too 20541 * small limits throughput on links with high bandwidth and high 20542 * delay (eg. trans-continental/oceanic links). Setting the 20543 * buffer size too big consumes too much real kernel memory, 20544 * especially with many connections on busy servers. 20545 * 20546 * The criteria to step up the send buffer one notch are: 20547 * 1. receive window of remote host is larger than send buffer 20548 * (with a fudge factor of 5/4th); 20549 * 2. send buffer is filled to 7/8th with data (so we actually 20550 * have data to make use of it); 20551 * 3. send buffer fill has not hit maximal automatic size; 20552 * 4. our send window (slow start and cogestion controlled) is 20553 * larger than sent but unacknowledged data in send buffer. 20554 * 20555 * Note that the rack version moves things much faster since 20556 * we want to avoid hitting cache lines in the rack_fast_output() 20557 * path so this is called much less often and thus moves 20558 * the SB forward by a percentage. 20559 */ 20560 struct socket *so; 20561 struct tcpcb *tp; 20562 uint32_t sendwin, scaleup; 20563 20564 tp = rack->rc_tp; 20565 so = rack->rc_inp->inp_socket; 20566 sendwin = min(rack->r_ctl.cwnd_to_use, tp->snd_wnd); 20567 if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 20568 if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 20569 sbused(&so->so_snd) >= 20570 (so->so_snd.sb_hiwat / 8 * 7) && 20571 sbused(&so->so_snd) < V_tcp_autosndbuf_max && 20572 sendwin >= (sbused(&so->so_snd) - 20573 (tp->snd_max - tp->snd_una))) { 20574 if (rack_autosndbuf_inc) 20575 scaleup = (rack_autosndbuf_inc * so->so_snd.sb_hiwat) / 100; 20576 else 20577 scaleup = V_tcp_autosndbuf_inc; 20578 if (scaleup < V_tcp_autosndbuf_inc) 20579 scaleup = V_tcp_autosndbuf_inc; 20580 scaleup += so->so_snd.sb_hiwat; 20581 if (scaleup > V_tcp_autosndbuf_max) 20582 scaleup = V_tcp_autosndbuf_max; 20583 if (!sbreserve_locked(so, SO_SND, scaleup, curthread)) 20584 so->so_snd.sb_flags &= ~SB_AUTOSIZE; 20585 } 20586 } 20587 } 20588 20589 static int 20590 rack_fast_output(struct tcpcb *tp, struct tcp_rack *rack, uint64_t ts_val, 20591 uint32_t cts, uint32_t ms_cts, struct timeval *tv, long tot_len, int *send_err) 20592 { 20593 /* 20594 * Enter to do fast output. We are given that the sched_pin is 20595 * in place (if accounting is compiled in) and the cycle count taken 20596 * at entry is in place in ts_val. The idea here is that 20597 * we know how many more bytes needs to be sent (presumably either 20598 * during pacing or to fill the cwnd and that was greater than 20599 * the max-burst). We have how much to send and all the info we 20600 * need to just send. 20601 */ 20602 #ifdef INET 20603 struct ip *ip = NULL; 20604 #endif 20605 struct udphdr *udp = NULL; 20606 struct tcphdr *th = NULL; 20607 struct mbuf *m, *s_mb; 20608 struct inpcb *inp; 20609 uint8_t *cpto; 20610 struct tcp_log_buffer *lgb; 20611 #ifdef TCP_ACCOUNTING 20612 uint64_t crtsc; 20613 #endif 20614 struct tcpopt to; 20615 u_char opt[TCP_MAXOLEN]; 20616 uint32_t hdrlen, optlen; 20617 #ifdef TCP_ACCOUNTING 20618 int cnt_thru = 1; 20619 #endif 20620 int32_t slot, segsiz, len, max_val, tso = 0, sb_offset, error, ulen = 0; 20621 uint16_t flags; 20622 uint32_t s_soff; 20623 uint32_t if_hw_tsomaxsegcount = 0, startseq; 20624 uint32_t if_hw_tsomaxsegsize; 20625 uint32_t add_flag = RACK_SENT_FP; 20626 #ifdef INET6 20627 struct ip6_hdr *ip6 = NULL; 20628 20629 if (rack->r_is_v6) { 20630 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 20631 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 20632 } else 20633 #endif /* INET6 */ 20634 { 20635 #ifdef INET 20636 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 20637 hdrlen = sizeof(struct tcpiphdr); 20638 #endif 20639 } 20640 if (tp->t_port && (V_tcp_udp_tunneling_port == 0)) { 20641 m = NULL; 20642 goto failed; 20643 } 20644 rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 20645 startseq = tp->snd_max; 20646 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 20647 inp = rack->rc_inp; 20648 len = rack->r_ctl.fsb.left_to_send; 20649 to.to_flags = 0; 20650 flags = rack->r_ctl.fsb.tcp_flags; 20651 if (tp->t_flags & TF_RCVD_TSTMP) { 20652 to.to_tsval = ms_cts + tp->ts_offset; 20653 to.to_tsecr = tp->ts_recent; 20654 to.to_flags = TOF_TS; 20655 } 20656 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 20657 /* TCP-MD5 (RFC2385). */ 20658 if (tp->t_flags & TF_SIGNATURE) 20659 to.to_flags |= TOF_SIGNATURE; 20660 #endif 20661 optlen = tcp_addoptions(&to, opt); 20662 hdrlen += optlen; 20663 udp = rack->r_ctl.fsb.udp; 20664 if (udp) 20665 hdrlen += sizeof(struct udphdr); 20666 if (rack->r_ctl.rc_pace_max_segs) 20667 max_val = rack->r_ctl.rc_pace_max_segs; 20668 else if (rack->rc_user_set_max_segs) 20669 max_val = rack->rc_user_set_max_segs * segsiz; 20670 else 20671 max_val = len; 20672 if ((tp->t_flags & TF_TSO) && 20673 V_tcp_do_tso && 20674 (len > segsiz) && 20675 (tp->t_port == 0)) 20676 tso = 1; 20677 again: 20678 #ifdef INET6 20679 if (MHLEN < hdrlen + max_linkhdr) 20680 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 20681 else 20682 #endif 20683 m = m_gethdr(M_NOWAIT, MT_DATA); 20684 if (m == NULL) 20685 goto failed; 20686 m->m_data += max_linkhdr; 20687 m->m_len = hdrlen; 20688 th = rack->r_ctl.fsb.th; 20689 /* Establish the len to send */ 20690 if (len > max_val) 20691 len = max_val; 20692 if ((tso) && (len + optlen > segsiz)) { 20693 uint32_t if_hw_tsomax; 20694 int32_t max_len; 20695 20696 /* extract TSO information */ 20697 if_hw_tsomax = tp->t_tsomax; 20698 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 20699 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 20700 /* 20701 * Check if we should limit by maximum payload 20702 * length: 20703 */ 20704 if (if_hw_tsomax != 0) { 20705 /* compute maximum TSO length */ 20706 max_len = (if_hw_tsomax - hdrlen - 20707 max_linkhdr); 20708 if (max_len <= 0) { 20709 goto failed; 20710 } else if (len > max_len) { 20711 len = max_len; 20712 } 20713 } 20714 if (len <= segsiz) { 20715 /* 20716 * In case there are too many small fragments don't 20717 * use TSO: 20718 */ 20719 tso = 0; 20720 } 20721 } else { 20722 tso = 0; 20723 } 20724 if ((tso == 0) && (len > segsiz)) 20725 len = segsiz; 20726 (void)tcp_get_usecs(tv); 20727 if ((len == 0) || 20728 (len <= MHLEN - hdrlen - max_linkhdr)) { 20729 goto failed; 20730 } 20731 sb_offset = tp->snd_max - tp->snd_una; 20732 th->th_seq = htonl(tp->snd_max); 20733 th->th_ack = htonl(tp->rcv_nxt); 20734 th->th_win = htons((u_short)(rack->r_ctl.fsb.recwin >> tp->rcv_scale)); 20735 if (th->th_win == 0) { 20736 tp->t_sndzerowin++; 20737 tp->t_flags |= TF_RXWIN0SENT; 20738 } else 20739 tp->t_flags &= ~TF_RXWIN0SENT; 20740 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */ 20741 KMOD_TCPSTAT_INC(tcps_sndpack); 20742 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 20743 #ifdef STATS 20744 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 20745 len); 20746 #endif 20747 if (rack->r_ctl.fsb.m == NULL) 20748 goto failed; 20749 20750 /* s_mb and s_soff are saved for rack_log_output */ 20751 m->m_next = rack_fo_m_copym(rack, &len, if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, 20752 &s_mb, &s_soff); 20753 if (len <= segsiz) { 20754 /* 20755 * Must have ran out of mbufs for the copy 20756 * shorten it to no longer need tso. Lets 20757 * not put on sendalot since we are low on 20758 * mbufs. 20759 */ 20760 tso = 0; 20761 } 20762 if (rack->r_ctl.fsb.rfo_apply_push && 20763 (len == rack->r_ctl.fsb.left_to_send)) { 20764 tcp_set_flags(th, flags | TH_PUSH); 20765 add_flag |= RACK_HAD_PUSH; 20766 } 20767 if ((m->m_next == NULL) || (len <= 0)){ 20768 goto failed; 20769 } 20770 if (udp) { 20771 if (rack->r_is_v6) 20772 ulen = hdrlen + len - sizeof(struct ip6_hdr); 20773 else 20774 ulen = hdrlen + len - sizeof(struct ip); 20775 udp->uh_ulen = htons(ulen); 20776 } 20777 m->m_pkthdr.rcvif = (struct ifnet *)0; 20778 if (TCPS_HAVERCVDSYN(tp->t_state) && 20779 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 20780 int ect = tcp_ecn_output_established(tp, &flags, len, false); 20781 if ((tp->t_state == TCPS_SYN_RECEIVED) && 20782 (tp->t_flags2 & TF2_ECN_SND_ECE)) 20783 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 20784 #ifdef INET6 20785 if (rack->r_is_v6) { 20786 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20); 20787 ip6->ip6_flow |= htonl(ect << 20); 20788 } 20789 else 20790 #endif 20791 { 20792 #ifdef INET 20793 ip->ip_tos &= ~IPTOS_ECN_MASK; 20794 ip->ip_tos |= ect; 20795 #endif 20796 } 20797 } 20798 tcp_set_flags(th, flags); 20799 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 20800 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 20801 if (to.to_flags & TOF_SIGNATURE) { 20802 /* 20803 * Calculate MD5 signature and put it into the place 20804 * determined before. 20805 * NOTE: since TCP options buffer doesn't point into 20806 * mbuf's data, calculate offset and use it. 20807 */ 20808 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 20809 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 20810 /* 20811 * Do not send segment if the calculation of MD5 20812 * digest has failed. 20813 */ 20814 goto failed; 20815 } 20816 } 20817 #endif 20818 #ifdef INET6 20819 if (rack->r_is_v6) { 20820 if (tp->t_port) { 20821 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 20822 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 20823 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 20824 th->th_sum = htons(0); 20825 UDPSTAT_INC(udps_opackets); 20826 } else { 20827 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 20828 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 20829 th->th_sum = in6_cksum_pseudo(ip6, 20830 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 20831 0); 20832 } 20833 } 20834 #endif 20835 #if defined(INET6) && defined(INET) 20836 else 20837 #endif 20838 #ifdef INET 20839 { 20840 if (tp->t_port) { 20841 m->m_pkthdr.csum_flags = CSUM_UDP; 20842 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 20843 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 20844 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 20845 th->th_sum = htons(0); 20846 UDPSTAT_INC(udps_opackets); 20847 } else { 20848 m->m_pkthdr.csum_flags = CSUM_TCP; 20849 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 20850 th->th_sum = in_pseudo(ip->ip_src.s_addr, 20851 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 20852 IPPROTO_TCP + len + optlen)); 20853 } 20854 /* IP version must be set here for ipv4/ipv6 checking later */ 20855 KASSERT(ip->ip_v == IPVERSION, 20856 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 20857 } 20858 #endif 20859 if (tso) { 20860 /* 20861 * Here we use segsiz since we have no added options besides 20862 * any standard timestamp options (no DSACKs or SACKS are sent 20863 * via either fast-path). 20864 */ 20865 KASSERT(len > segsiz, 20866 ("%s: len <= tso_segsz tp:%p", __func__, tp)); 20867 m->m_pkthdr.csum_flags |= CSUM_TSO; 20868 m->m_pkthdr.tso_segsz = segsiz; 20869 } 20870 #ifdef INET6 20871 if (rack->r_is_v6) { 20872 ip6->ip6_hlim = rack->r_ctl.fsb.hoplimit; 20873 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 20874 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 20875 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 20876 else 20877 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 20878 } 20879 #endif 20880 #if defined(INET) && defined(INET6) 20881 else 20882 #endif 20883 #ifdef INET 20884 { 20885 ip->ip_len = htons(m->m_pkthdr.len); 20886 ip->ip_ttl = rack->r_ctl.fsb.hoplimit; 20887 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 20888 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 20889 if (tp->t_port == 0 || len < V_tcp_minmss) { 20890 ip->ip_off |= htons(IP_DF); 20891 } 20892 } else { 20893 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 20894 } 20895 } 20896 #endif 20897 if (tp->snd_cwnd > tp->snd_ssthresh) { 20898 /* Set we sent in CA */ 20899 rack->rc_gp_saw_ca = 1; 20900 } else { 20901 /* Set we sent in SS */ 20902 rack->rc_gp_saw_ss = 1; 20903 } 20904 /* Time to copy in our header */ 20905 cpto = mtod(m, uint8_t *); 20906 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 20907 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 20908 if (optlen) { 20909 bcopy(opt, th + 1, optlen); 20910 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 20911 } else { 20912 th->th_off = sizeof(struct tcphdr) >> 2; 20913 } 20914 if ((rack->r_ctl.crte != NULL) && 20915 tcp_bblogging_on(tp)) { 20916 rack_log_queue_level(tp, rack, len, tv, cts); 20917 } 20918 if (tcp_bblogging_on(rack->rc_tp)) { 20919 union tcp_log_stackspecific log; 20920 20921 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 20922 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 20923 if (rack->rack_no_prr) 20924 log.u_bbr.flex1 = 0; 20925 else 20926 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 20927 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 20928 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 20929 log.u_bbr.flex4 = max_val; 20930 /* Save off the early/late values */ 20931 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 20932 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 20933 log.u_bbr.bw_inuse = rack_get_bw(rack); 20934 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw; 20935 log.u_bbr.flex8 = 0; 20936 log.u_bbr.pacing_gain = rack_get_output_gain(rack, NULL); 20937 log.u_bbr.flex7 = 44; 20938 log.u_bbr.pkts_out = tp->t_maxseg; 20939 log.u_bbr.timeStamp = cts; 20940 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 20941 log.u_bbr.flex5 = log.u_bbr.inflight; 20942 log.u_bbr.lt_epoch = rack->r_ctl.cwnd_to_use; 20943 log.u_bbr.delivered = 0; 20944 log.u_bbr.rttProp = 0; 20945 log.u_bbr.delRate = rack->r_must_retran; 20946 log.u_bbr.delRate <<= 1; 20947 log.u_bbr.pkt_epoch = __LINE__; 20948 /* For fast output no retrans so just inflight and how many mss we send */ 20949 log.u_bbr.flex5 = log.u_bbr.inflight; 20950 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz); 20951 lgb = tcp_log_event(tp, th, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, 20952 len, &log, false, NULL, __func__, __LINE__, tv); 20953 } else 20954 lgb = NULL; 20955 #ifdef INET6 20956 if (rack->r_is_v6) { 20957 error = ip6_output(m, inp->in6p_outputopts, 20958 &inp->inp_route6, 20959 0, NULL, NULL, inp); 20960 } 20961 #endif 20962 #if defined(INET) && defined(INET6) 20963 else 20964 #endif 20965 #ifdef INET 20966 { 20967 error = ip_output(m, NULL, 20968 &inp->inp_route, 20969 0, 0, inp); 20970 } 20971 #endif 20972 if (lgb) { 20973 lgb->tlb_errno = error; 20974 lgb = NULL; 20975 } 20976 if (error) { 20977 *send_err = error; 20978 m = NULL; 20979 goto failed; 20980 } else if (rack->rc_hw_nobuf) { 20981 rack->rc_hw_nobuf = 0; 20982 rack->r_ctl.rc_agg_delayed = 0; 20983 rack->r_early = 0; 20984 rack->r_late = 0; 20985 rack->r_ctl.rc_agg_early = 0; 20986 } 20987 if ((error == 0) && (rack->lt_bw_up == 0)) { 20988 /* Unlikely */ 20989 rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(tv); 20990 rack->r_ctl.lt_seq = tp->snd_una; 20991 rack->lt_bw_up = 1; 20992 } else if ((error == 0) && 20993 (((tp->snd_max + len) - rack->r_ctl.lt_seq) > 0x7fffffff)) { 20994 /* 20995 * Need to record what we have since we are 20996 * approaching seq wrap. 20997 */ 20998 struct timeval tv; 20999 uint64_t tmark; 21000 21001 rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq); 21002 rack->r_ctl.lt_seq = tp->snd_una; 21003 tmark = tcp_get_u64_usecs(&tv); 21004 if (tmark > rack->r_ctl.lt_timemark) { 21005 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark); 21006 rack->r_ctl.lt_timemark = tmark; 21007 } 21008 } 21009 rack_log_output(tp, &to, len, tp->snd_max, flags, error, rack_to_usec_ts(tv), 21010 NULL, add_flag, s_mb, s_soff, rack->r_ctl.fsb.hw_tls, segsiz); 21011 m = NULL; 21012 if (tp->snd_una == tp->snd_max) { 21013 rack->r_ctl.rc_tlp_rxt_last_time = cts; 21014 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 21015 tp->t_acktime = ticks; 21016 } 21017 counter_u64_add(rack_total_bytes, len); 21018 tcp_account_for_send(tp, len, 0, 0, rack->r_ctl.fsb.hw_tls); 21019 21020 rack->forced_ack = 0; /* If we send something zap the FA flag */ 21021 tot_len += len; 21022 if ((tp->t_flags & TF_GPUTINPROG) == 0) 21023 rack_start_gp_measurement(tp, rack, tp->snd_max, sb_offset); 21024 tp->snd_max += len; 21025 tp->snd_nxt = tp->snd_max; 21026 if (rack->rc_new_rnd_needed) { 21027 rack_new_round_starts(tp, rack, tp->snd_max); 21028 } 21029 { 21030 int idx; 21031 21032 idx = (len / segsiz) + 3; 21033 if (idx >= TCP_MSS_ACCT_ATIMER) 21034 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 21035 else 21036 counter_u64_add(rack_out_size[idx], 1); 21037 } 21038 if (len <= rack->r_ctl.fsb.left_to_send) 21039 rack->r_ctl.fsb.left_to_send -= len; 21040 else 21041 rack->r_ctl.fsb.left_to_send = 0; 21042 if (rack->r_ctl.fsb.left_to_send < segsiz) { 21043 rack->r_fast_output = 0; 21044 rack->r_ctl.fsb.left_to_send = 0; 21045 /* At the end of fast_output scale up the sb */ 21046 SOCKBUF_LOCK(&rack->rc_inp->inp_socket->so_snd); 21047 rack_sndbuf_autoscale(rack); 21048 SOCKBUF_UNLOCK(&rack->rc_inp->inp_socket->so_snd); 21049 } 21050 if (tp->t_rtttime == 0) { 21051 tp->t_rtttime = ticks; 21052 tp->t_rtseq = startseq; 21053 KMOD_TCPSTAT_INC(tcps_segstimed); 21054 } 21055 if ((rack->r_ctl.fsb.left_to_send >= segsiz) && 21056 (max_val > len) && 21057 (tso == 0)) { 21058 max_val -= len; 21059 len = segsiz; 21060 th = rack->r_ctl.fsb.th; 21061 #ifdef TCP_ACCOUNTING 21062 cnt_thru++; 21063 #endif 21064 goto again; 21065 } 21066 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 21067 counter_u64_add(rack_fto_send, 1); 21068 slot = rack_get_pacing_delay(rack, tp, tot_len, NULL, segsiz, __LINE__); 21069 rack_start_hpts_timer(rack, tp, cts, slot, tot_len, 0); 21070 #ifdef TCP_ACCOUNTING 21071 crtsc = get_cyclecount(); 21072 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 21073 tp->tcp_cnt_counters[SND_OUT_DATA] += cnt_thru; 21074 } 21075 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 21076 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 21077 } 21078 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 21079 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len + segsiz - 1) / segsiz); 21080 } 21081 sched_unpin(); 21082 #endif 21083 return (0); 21084 failed: 21085 if (m) 21086 m_free(m); 21087 rack->r_fast_output = 0; 21088 return (-1); 21089 } 21090 21091 static inline void 21092 rack_setup_fast_output(struct tcpcb *tp, struct tcp_rack *rack, 21093 struct sockbuf *sb, 21094 int len, int orig_len, int segsiz, uint32_t pace_max_seg, 21095 bool hw_tls, 21096 uint16_t flags) 21097 { 21098 rack->r_fast_output = 1; 21099 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 21100 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 21101 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m); 21102 rack->r_ctl.fsb.tcp_flags = flags; 21103 rack->r_ctl.fsb.left_to_send = orig_len - len; 21104 if (rack->r_ctl.fsb.left_to_send < pace_max_seg) { 21105 /* Less than a full sized pace, lets not */ 21106 rack->r_fast_output = 0; 21107 return; 21108 } else { 21109 /* Round down to the nearest pace_max_seg */ 21110 rack->r_ctl.fsb.left_to_send = rounddown(rack->r_ctl.fsb.left_to_send, pace_max_seg); 21111 } 21112 if (hw_tls) 21113 rack->r_ctl.fsb.hw_tls = 1; 21114 else 21115 rack->r_ctl.fsb.hw_tls = 0; 21116 KASSERT((rack->r_ctl.fsb.left_to_send <= (sbavail(sb) - (tp->snd_max - tp->snd_una))), 21117 ("rack:%p left_to_send:%u sbavail:%u out:%u", 21118 rack, rack->r_ctl.fsb.left_to_send, sbavail(sb), 21119 (tp->snd_max - tp->snd_una))); 21120 if (rack->r_ctl.fsb.left_to_send < segsiz) 21121 rack->r_fast_output = 0; 21122 else { 21123 if (rack->r_ctl.fsb.left_to_send == (sbavail(sb) - (tp->snd_max - tp->snd_una))) 21124 rack->r_ctl.fsb.rfo_apply_push = 1; 21125 else 21126 rack->r_ctl.fsb.rfo_apply_push = 0; 21127 } 21128 } 21129 21130 static uint32_t 21131 rack_get_hpts_pacing_min_for_bw(struct tcp_rack *rack, int32_t segsiz) 21132 { 21133 uint64_t min_time; 21134 uint32_t maxlen; 21135 21136 min_time = (uint64_t)get_hpts_min_sleep_time(); 21137 maxlen = (uint32_t)((rack->r_ctl.gp_bw * min_time) / (uint64_t)HPTS_USEC_IN_SEC); 21138 maxlen = roundup(maxlen, segsiz); 21139 return (maxlen); 21140 } 21141 21142 static struct rack_sendmap * 21143 rack_check_collapsed(struct tcp_rack *rack, uint32_t cts) 21144 { 21145 struct rack_sendmap *rsm = NULL; 21146 int thresh; 21147 21148 restart: 21149 rsm = tqhash_find(rack->r_ctl.tqh, rack->r_ctl.last_collapse_point); 21150 if ((rsm == NULL) || ((rsm->r_flags & RACK_RWND_COLLAPSED) == 0)) { 21151 /* Nothing, strange turn off validity */ 21152 rack->r_collapse_point_valid = 0; 21153 return (NULL); 21154 } 21155 /* Can we send it yet? */ 21156 if (rsm->r_end > (rack->rc_tp->snd_una + rack->rc_tp->snd_wnd)) { 21157 /* 21158 * Receiver window has not grown enough for 21159 * the segment to be put on the wire. 21160 */ 21161 return (NULL); 21162 } 21163 if (rsm->r_flags & RACK_ACKED) { 21164 /* 21165 * It has been sacked, lets move to the 21166 * next one if possible. 21167 */ 21168 rack->r_ctl.last_collapse_point = rsm->r_end; 21169 /* Are we done? */ 21170 if (SEQ_GEQ(rack->r_ctl.last_collapse_point, 21171 rack->r_ctl.high_collapse_point)) { 21172 rack->r_collapse_point_valid = 0; 21173 return (NULL); 21174 } 21175 goto restart; 21176 } 21177 /* Now has it been long enough ? */ 21178 thresh = rack_calc_thresh_rack(rack, rack_grab_rtt(rack->rc_tp, rack), cts, __LINE__, 1); 21179 if ((cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])) > thresh) { 21180 rack_log_collapse(rack, rsm->r_start, 21181 (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])), 21182 thresh, __LINE__, 6, rsm->r_flags, rsm); 21183 return (rsm); 21184 } 21185 /* Not enough time */ 21186 rack_log_collapse(rack, rsm->r_start, 21187 (cts - ((uint32_t)rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)])), 21188 thresh, __LINE__, 7, rsm->r_flags, rsm); 21189 return (NULL); 21190 } 21191 21192 static void 21193 rack_credit_back_policer_idle_time(struct tcp_rack *rack, uint64_t idle_t, int line) 21194 { 21195 /* 21196 * We were idle some time (idle_t) and so our policer bucket 21197 * needs to grow. It can go no higher than policer_bucket_size. 21198 */ 21199 uint64_t len; 21200 21201 len = idle_t * rack->r_ctl.policer_bw; 21202 len /= HPTS_USEC_IN_SEC; 21203 rack->r_ctl.current_policer_bucket += (uint32_t)len; 21204 if (rack->r_ctl.policer_bucket_size < rack->r_ctl.current_policer_bucket) { 21205 rack->r_ctl.current_policer_bucket = rack->r_ctl.policer_bucket_size; 21206 } 21207 if (rack_verbose_logging > 0) 21208 policer_detection_log(rack, (uint32_t)len, line, (uint32_t)idle_t, 0, 7); 21209 } 21210 21211 static inline void 21212 rack_validate_sizes(struct tcp_rack *rack, int32_t *len, int32_t segsiz, uint32_t pace_max_seg) 21213 { 21214 if ((rack->full_size_rxt == 0) && 21215 (rack->shape_rxt_to_pacing_min == 0) && 21216 (*len >= segsiz)) { 21217 *len = segsiz; 21218 } else if (rack->shape_rxt_to_pacing_min && 21219 rack->gp_ready) { 21220 /* We use pacing min as shaping len req */ 21221 uint32_t maxlen; 21222 21223 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz); 21224 if (*len > maxlen) 21225 *len = maxlen; 21226 } else { 21227 /* 21228 * The else is full_size_rxt is on so send it all 21229 * note we do need to check this for exceeding 21230 * our max segment size due to the fact that 21231 * we do sometimes merge chunks together i.e. 21232 * we cannot just assume that we will never have 21233 * a chunk greater than pace_max_seg 21234 */ 21235 if (*len > pace_max_seg) 21236 *len = pace_max_seg; 21237 } 21238 } 21239 21240 static int 21241 rack_output(struct tcpcb *tp) 21242 { 21243 struct socket *so; 21244 uint32_t recwin; 21245 uint32_t sb_offset, s_moff = 0; 21246 int32_t len, error = 0; 21247 uint16_t flags; 21248 struct mbuf *m, *s_mb = NULL; 21249 struct mbuf *mb; 21250 uint32_t if_hw_tsomaxsegcount = 0; 21251 uint32_t if_hw_tsomaxsegsize; 21252 int32_t segsiz, minseg; 21253 long tot_len_this_send = 0; 21254 #ifdef INET 21255 struct ip *ip = NULL; 21256 #endif 21257 struct udphdr *udp = NULL; 21258 struct tcp_rack *rack; 21259 struct tcphdr *th; 21260 uint8_t pass = 0; 21261 uint8_t mark = 0; 21262 uint8_t check_done = 0; 21263 uint8_t wanted_cookie = 0; 21264 u_char opt[TCP_MAXOLEN]; 21265 unsigned ipoptlen, optlen, hdrlen, ulen=0; 21266 uint32_t rack_seq; 21267 21268 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 21269 unsigned ipsec_optlen = 0; 21270 21271 #endif 21272 int32_t idle, sendalot; 21273 uint32_t tot_idle; 21274 int32_t sub_from_prr = 0; 21275 volatile int32_t sack_rxmit; 21276 struct rack_sendmap *rsm = NULL; 21277 int32_t tso, mtu; 21278 struct tcpopt to; 21279 int32_t slot = 0; 21280 int32_t sup_rack = 0; 21281 uint32_t cts, ms_cts, delayed, early; 21282 uint32_t add_flag = RACK_SENT_SP; 21283 /* The doing_tlp flag will be set by the actual rack_timeout_tlp() */ 21284 uint8_t doing_tlp = 0; 21285 uint32_t cwnd_to_use, pace_max_seg; 21286 int32_t do_a_prefetch = 0; 21287 int32_t prefetch_rsm = 0; 21288 int32_t orig_len = 0; 21289 struct timeval tv; 21290 int32_t prefetch_so_done = 0; 21291 struct tcp_log_buffer *lgb; 21292 struct inpcb *inp = tptoinpcb(tp); 21293 struct sockbuf *sb; 21294 uint64_t ts_val = 0; 21295 #ifdef TCP_ACCOUNTING 21296 uint64_t crtsc; 21297 #endif 21298 #ifdef INET6 21299 struct ip6_hdr *ip6 = NULL; 21300 int32_t isipv6; 21301 #endif 21302 bool hpts_calling, hw_tls = false; 21303 21304 NET_EPOCH_ASSERT(); 21305 INP_WLOCK_ASSERT(inp); 21306 21307 /* setup and take the cache hits here */ 21308 rack = (struct tcp_rack *)tp->t_fb_ptr; 21309 #ifdef TCP_ACCOUNTING 21310 sched_pin(); 21311 ts_val = get_cyclecount(); 21312 #endif 21313 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS); 21314 tp->t_flags2 &= ~TF2_HPTS_CALLS; 21315 #ifdef TCP_OFFLOAD 21316 if (tp->t_flags & TF_TOE) { 21317 #ifdef TCP_ACCOUNTING 21318 sched_unpin(); 21319 #endif 21320 return (tcp_offload_output(tp)); 21321 } 21322 #endif 21323 if (rack->rack_deferred_inited == 0) { 21324 /* 21325 * If we are the connecting socket we will 21326 * hit rack_init() when no sequence numbers 21327 * are setup. This makes it so we must defer 21328 * some initialization. Call that now. 21329 */ 21330 rack_deferred_init(tp, rack); 21331 } 21332 /* 21333 * For TFO connections in SYN_RECEIVED, only allow the initial 21334 * SYN|ACK and those sent by the retransmit timer. 21335 */ 21336 if ((tp->t_flags & TF_FASTOPEN) && 21337 (tp->t_state == TCPS_SYN_RECEIVED) && 21338 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN|ACK sent */ 21339 (rack->r_ctl.rc_resend == NULL)) { /* not a retransmit */ 21340 #ifdef TCP_ACCOUNTING 21341 sched_unpin(); 21342 #endif 21343 return (0); 21344 } 21345 #ifdef INET6 21346 if (rack->r_state) { 21347 /* Use the cache line loaded if possible */ 21348 isipv6 = rack->r_is_v6; 21349 } else { 21350 isipv6 = (rack->rc_inp->inp_vflag & INP_IPV6) != 0; 21351 } 21352 #endif 21353 early = 0; 21354 cts = tcp_get_usecs(&tv); 21355 ms_cts = tcp_tv_to_mssectick(&tv); 21356 if (((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 21357 tcp_in_hpts(rack->rc_tp)) { 21358 /* 21359 * We are on the hpts for some timer but not hptsi output. 21360 * Remove from the hpts unconditionally. 21361 */ 21362 rack_timer_cancel(tp, rack, cts, __LINE__); 21363 } 21364 /* Are we pacing and late? */ 21365 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 21366 TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) { 21367 /* We are delayed */ 21368 delayed = cts - rack->r_ctl.rc_last_output_to; 21369 } else { 21370 delayed = 0; 21371 } 21372 /* Do the timers, which may override the pacer */ 21373 if (rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 21374 int retval; 21375 21376 retval = rack_process_timers(tp, rack, cts, hpts_calling, 21377 &doing_tlp); 21378 if (retval != 0) { 21379 counter_u64_add(rack_out_size[TCP_MSS_ACCT_ATIMER], 1); 21380 #ifdef TCP_ACCOUNTING 21381 sched_unpin(); 21382 #endif 21383 /* 21384 * If timers want tcp_drop(), then pass error out, 21385 * otherwise suppress it. 21386 */ 21387 return (retval < 0 ? retval : 0); 21388 } 21389 } 21390 if (rack->rc_in_persist) { 21391 if (tcp_in_hpts(rack->rc_tp) == 0) { 21392 /* Timer is not running */ 21393 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 21394 } 21395 #ifdef TCP_ACCOUNTING 21396 sched_unpin(); 21397 #endif 21398 return (0); 21399 } 21400 if ((rack->rc_ack_required == 1) && 21401 (rack->r_timer_override == 0)){ 21402 /* A timeout occurred and no ack has arrived */ 21403 if (tcp_in_hpts(rack->rc_tp) == 0) { 21404 /* Timer is not running */ 21405 rack_start_hpts_timer(rack, tp, cts, 0, 0, 0); 21406 } 21407 #ifdef TCP_ACCOUNTING 21408 sched_unpin(); 21409 #endif 21410 return (0); 21411 } 21412 if ((rack->r_timer_override) || 21413 (rack->rc_ack_can_sendout_data) || 21414 (delayed) || 21415 (tp->t_state < TCPS_ESTABLISHED)) { 21416 rack->rc_ack_can_sendout_data = 0; 21417 if (tcp_in_hpts(rack->rc_tp)) 21418 tcp_hpts_remove(rack->rc_tp); 21419 } else if (tcp_in_hpts(rack->rc_tp)) { 21420 /* 21421 * On the hpts you can't pass even if ACKNOW is on, we will 21422 * when the hpts fires. 21423 */ 21424 #ifdef TCP_ACCOUNTING 21425 crtsc = get_cyclecount(); 21426 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 21427 tp->tcp_proc_time[SND_BLOCKED] += (crtsc - ts_val); 21428 } 21429 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 21430 tp->tcp_cnt_counters[SND_BLOCKED]++; 21431 } 21432 sched_unpin(); 21433 #endif 21434 counter_u64_add(rack_out_size[TCP_MSS_ACCT_INPACE], 1); 21435 return (0); 21436 } 21437 /* Finish out both pacing early and late accounting */ 21438 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 21439 TSTMP_GT(rack->r_ctl.rc_last_output_to, cts)) { 21440 early = rack->r_ctl.rc_last_output_to - cts; 21441 } else 21442 early = 0; 21443 if (delayed && (rack->rc_always_pace == 1)) { 21444 rack->r_ctl.rc_agg_delayed += delayed; 21445 rack->r_late = 1; 21446 } else if (early && (rack->rc_always_pace == 1)) { 21447 rack->r_ctl.rc_agg_early += early; 21448 rack->r_early = 1; 21449 } else if (rack->rc_always_pace == 0) { 21450 /* Non-paced we are not late */ 21451 rack->r_ctl.rc_agg_delayed = rack->r_ctl.rc_agg_early = 0; 21452 rack->r_early = rack->r_late = 0; 21453 } 21454 /* Now that early/late accounting is done turn off the flag */ 21455 rack->r_ctl.rc_hpts_flags &= ~PACE_PKT_OUTPUT; 21456 rack->r_wanted_output = 0; 21457 rack->r_timer_override = 0; 21458 if ((tp->t_state != rack->r_state) && 21459 TCPS_HAVEESTABLISHED(tp->t_state)) { 21460 rack_set_state(tp, rack); 21461 } 21462 if ((rack->r_fast_output) && 21463 (doing_tlp == 0) && 21464 (tp->rcv_numsacks == 0)) { 21465 int ret; 21466 21467 error = 0; 21468 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error); 21469 if (ret >= 0) 21470 return(ret); 21471 else if (error) { 21472 inp = rack->rc_inp; 21473 so = inp->inp_socket; 21474 sb = &so->so_snd; 21475 goto nomore; 21476 } 21477 } 21478 inp = rack->rc_inp; 21479 /* 21480 * For TFO connections in SYN_SENT or SYN_RECEIVED, 21481 * only allow the initial SYN or SYN|ACK and those sent 21482 * by the retransmit timer. 21483 */ 21484 if ((tp->t_flags & TF_FASTOPEN) && 21485 ((tp->t_state == TCPS_SYN_RECEIVED) || 21486 (tp->t_state == TCPS_SYN_SENT)) && 21487 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 21488 (tp->t_rxtshift == 0)) { /* not a retransmit */ 21489 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 21490 so = inp->inp_socket; 21491 sb = &so->so_snd; 21492 goto just_return_nolock; 21493 } 21494 /* 21495 * Determine length of data that should be transmitted, and flags 21496 * that will be used. If there is some data or critical controls 21497 * (SYN, RST) to send, then transmit; otherwise, investigate 21498 * further. 21499 */ 21500 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 21501 if (tp->t_idle_reduce) { 21502 if (idle && (TICKS_2_USEC(ticks - tp->t_rcvtime) >= tp->t_rxtcur)) 21503 rack_cc_after_idle(rack, tp); 21504 } 21505 tp->t_flags &= ~TF_LASTIDLE; 21506 if (idle) { 21507 if (tp->t_flags & TF_MORETOCOME) { 21508 tp->t_flags |= TF_LASTIDLE; 21509 idle = 0; 21510 } 21511 } 21512 if ((tp->snd_una == tp->snd_max) && 21513 rack->r_ctl.rc_went_idle_time && 21514 (cts > rack->r_ctl.rc_went_idle_time)) { 21515 tot_idle = (cts - rack->r_ctl.rc_went_idle_time); 21516 if (tot_idle > rack_min_probertt_hold) { 21517 /* Count as a probe rtt */ 21518 if (rack->in_probe_rtt == 0) { 21519 rack->r_ctl.rc_lower_rtt_us_cts = cts; 21520 rack->r_ctl.rc_time_probertt_entered = rack->r_ctl.rc_lower_rtt_us_cts; 21521 rack->r_ctl.rc_time_probertt_starts = rack->r_ctl.rc_lower_rtt_us_cts; 21522 rack->r_ctl.rc_time_of_last_probertt = rack->r_ctl.rc_lower_rtt_us_cts; 21523 } else { 21524 rack_exit_probertt(rack, cts); 21525 } 21526 } 21527 } 21528 if(rack->policer_detect_on) { 21529 /* 21530 * If we are doing policer detetion we at a minium 21531 * record the time but if possible add back to 21532 * the bucket based on the idle time. 21533 */ 21534 uint64_t idle_t, u64_cts; 21535 21536 segsiz = min(ctf_fixed_maxseg(tp), 21537 rack->r_ctl.rc_pace_min_segs); 21538 u64_cts = tcp_tv_to_lusectick(&tv); 21539 if ((rack->rc_policer_detected == 1) && 21540 (rack->r_ctl.policer_bucket_size > segsiz) && 21541 (rack->r_ctl.policer_bw > 0) && 21542 (u64_cts > rack->r_ctl.last_sendtime)) { 21543 /* We are being policed add back the time */ 21544 idle_t = u64_cts - rack->r_ctl.last_sendtime; 21545 rack_credit_back_policer_idle_time(rack, idle_t, __LINE__); 21546 } 21547 rack->r_ctl.last_sendtime = u64_cts; 21548 } 21549 if (rack_use_fsb && 21550 (rack->r_ctl.fsb.tcp_ip_hdr) && 21551 (rack->r_fsb_inited == 0) && 21552 (rack->r_state != TCPS_CLOSED)) 21553 rack_init_fsb_block(tp, rack, tcp_outflags[tp->t_state]); 21554 if (rack->rc_sendvars_notset == 1) { 21555 rack->r_ctl.idle_snd_una = tp->snd_una; 21556 rack->rc_sendvars_notset = 0; 21557 /* 21558 * Make sure any TCP timers (keep-alive) is not running. 21559 */ 21560 tcp_timer_stop(tp); 21561 } 21562 if ((rack->rack_no_prr == 1) && 21563 (rack->rc_always_pace == 0)) { 21564 /* 21565 * Sanity check before sending, if we have 21566 * no-pacing enabled and prr is turned off that 21567 * is a logistics error. Correct this by turnning 21568 * prr back on. A user *must* set some form of 21569 * pacing in order to turn PRR off. We do this 21570 * in the output path so that we can avoid socket 21571 * option ordering issues that would occur if we 21572 * tried to do it while setting rack_no_prr on. 21573 */ 21574 rack->rack_no_prr = 0; 21575 } 21576 if ((rack->pcm_enabled == 1) && 21577 (rack->pcm_needed == 0) && 21578 (tot_idle > 0)) { 21579 /* 21580 * We have been idle some micro seconds. We need 21581 * to factor this in to see if a PCM is needed. 21582 */ 21583 uint32_t rtts_idle, rnds; 21584 21585 if (tp->t_srtt) 21586 rtts_idle = tot_idle / tp->t_srtt; 21587 else 21588 rtts_idle = 0; 21589 rnds = rack->r_ctl.current_round - rack->r_ctl.last_pcm_round; 21590 rack->r_ctl.pcm_idle_rounds += rtts_idle; 21591 if ((rnds + rack->r_ctl.pcm_idle_rounds) >= rack_pcm_every_n_rounds) { 21592 rack->pcm_needed = 1; 21593 rack_log_pcm(rack, 8, rack->r_ctl.last_pcm_round, rtts_idle, rack->r_ctl.current_round ); 21594 } 21595 } 21596 again: 21597 sendalot = 0; 21598 cts = tcp_get_usecs(&tv); 21599 ms_cts = tcp_tv_to_mssectick(&tv); 21600 tso = 0; 21601 mtu = 0; 21602 segsiz = min(ctf_fixed_maxseg(tp), rack->r_ctl.rc_pace_min_segs); 21603 minseg = segsiz; 21604 if (rack->r_ctl.rc_pace_max_segs == 0) 21605 pace_max_seg = rack->rc_user_set_max_segs * segsiz; 21606 else 21607 pace_max_seg = rack->r_ctl.rc_pace_max_segs; 21608 if (TCPS_HAVEESTABLISHED(tp->t_state) && 21609 (rack->r_ctl.pcm_max_seg == 0)) { 21610 /* 21611 * We set in our first send so we know that the ctf_fixed_maxseg 21612 * has been fully set. If we do it in rack_init() we most likely 21613 * see 512 bytes so we end up at 5120, not desirable. 21614 */ 21615 rack->r_ctl.pcm_max_seg = rc_init_window(rack); 21616 if (rack->r_ctl.pcm_max_seg < (ctf_fixed_maxseg(tp) * 10)) { 21617 /* 21618 * Assure our initial PCM probe is at least 10 MSS. 21619 */ 21620 rack->r_ctl.pcm_max_seg = ctf_fixed_maxseg(tp) * 10; 21621 } 21622 } 21623 if ((rack->r_ctl.pcm_max_seg != 0) && (rack->pcm_needed == 1)) { 21624 uint32_t rw_avail, cwa; 21625 21626 if (tp->snd_wnd > ctf_outstanding(tp)) 21627 rw_avail = tp->snd_wnd - ctf_outstanding(tp); 21628 else 21629 rw_avail = 0; 21630 if (tp->snd_cwnd > ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked)) 21631 cwa = tp->snd_cwnd -ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 21632 else 21633 cwa = 0; 21634 if ((cwa >= rack->r_ctl.pcm_max_seg) && 21635 (rw_avail > rack->r_ctl.pcm_max_seg)) { 21636 /* Raise up the max seg for this trip through */ 21637 pace_max_seg = rack->r_ctl.pcm_max_seg; 21638 /* Disable any fast output */ 21639 rack->r_fast_output = 0; 21640 } 21641 if (rack_verbose_logging) { 21642 rack_log_pcm(rack, 4, 21643 cwa, rack->r_ctl.pcm_max_seg, rw_avail); 21644 } 21645 } 21646 sb_offset = tp->snd_max - tp->snd_una; 21647 cwnd_to_use = rack->r_ctl.cwnd_to_use = tp->snd_cwnd; 21648 flags = tcp_outflags[tp->t_state]; 21649 while (rack->rc_free_cnt < rack_free_cache) { 21650 rsm = rack_alloc(rack); 21651 if (rsm == NULL) { 21652 if (hpts_calling) 21653 /* Retry in a ms */ 21654 slot = (1 * HPTS_USEC_IN_MSEC); 21655 so = inp->inp_socket; 21656 sb = &so->so_snd; 21657 goto just_return_nolock; 21658 } 21659 TAILQ_INSERT_TAIL(&rack->r_ctl.rc_free, rsm, r_tnext); 21660 rack->rc_free_cnt++; 21661 rsm = NULL; 21662 } 21663 sack_rxmit = 0; 21664 len = 0; 21665 rsm = NULL; 21666 if (flags & TH_RST) { 21667 SOCKBUF_LOCK(&inp->inp_socket->so_snd); 21668 so = inp->inp_socket; 21669 sb = &so->so_snd; 21670 goto send; 21671 } 21672 if (rack->r_ctl.rc_resend) { 21673 /* Retransmit timer */ 21674 rsm = rack->r_ctl.rc_resend; 21675 rack->r_ctl.rc_resend = NULL; 21676 len = rsm->r_end - rsm->r_start; 21677 sack_rxmit = 1; 21678 sendalot = 0; 21679 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 21680 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 21681 __func__, __LINE__, 21682 rsm->r_start, tp->snd_una, tp, rack, rsm)); 21683 sb_offset = rsm->r_start - tp->snd_una; 21684 rack_validate_sizes(rack, &len, segsiz, pace_max_seg); 21685 } else if (rack->r_collapse_point_valid && 21686 ((rsm = rack_check_collapsed(rack, cts)) != NULL)) { 21687 /* 21688 * If an RSM is returned then enough time has passed 21689 * for us to retransmit it. Move up the collapse point, 21690 * since this rsm has its chance to retransmit now. 21691 */ 21692 tcp_trace_point(rack->rc_tp, TCP_TP_COLLAPSED_RXT); 21693 rack->r_ctl.last_collapse_point = rsm->r_end; 21694 /* Are we done? */ 21695 if (SEQ_GEQ(rack->r_ctl.last_collapse_point, 21696 rack->r_ctl.high_collapse_point)) 21697 rack->r_collapse_point_valid = 0; 21698 sack_rxmit = 1; 21699 /* We are not doing a TLP */ 21700 doing_tlp = 0; 21701 len = rsm->r_end - rsm->r_start; 21702 sb_offset = rsm->r_start - tp->snd_una; 21703 sendalot = 0; 21704 rack_validate_sizes(rack, &len, segsiz, pace_max_seg); 21705 } else if ((rsm = tcp_rack_output(tp, rack, cts)) != NULL) { 21706 /* We have a retransmit that takes precedence */ 21707 if ((!IN_FASTRECOVERY(tp->t_flags)) && 21708 ((rsm->r_flags & RACK_MUST_RXT) == 0) && 21709 ((tp->t_flags & TF_WASFRECOVERY) == 0)) { 21710 /* Enter recovery if not induced by a time-out */ 21711 rack_cong_signal(tp, CC_NDUPACK, tp->snd_una, __LINE__); 21712 } 21713 #ifdef INVARIANTS 21714 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 21715 panic("Huh, tp:%p rack:%p rsm:%p start:%u < snd_una:%u\n", 21716 tp, rack, rsm, rsm->r_start, tp->snd_una); 21717 } 21718 #endif 21719 len = rsm->r_end - rsm->r_start; 21720 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 21721 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 21722 __func__, __LINE__, 21723 rsm->r_start, tp->snd_una, tp, rack, rsm)); 21724 sb_offset = rsm->r_start - tp->snd_una; 21725 sendalot = 0; 21726 rack_validate_sizes(rack, &len, segsiz, pace_max_seg); 21727 if (len > 0) { 21728 sack_rxmit = 1; 21729 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 21730 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 21731 min(len, segsiz)); 21732 } 21733 } else if (rack->r_ctl.rc_tlpsend) { 21734 /* Tail loss probe */ 21735 long cwin; 21736 long tlen; 21737 21738 /* 21739 * Check if we can do a TLP with a RACK'd packet 21740 * this can happen if we are not doing the rack 21741 * cheat and we skipped to a TLP and it 21742 * went off. 21743 */ 21744 rsm = rack->r_ctl.rc_tlpsend; 21745 /* We are doing a TLP make sure the flag is preent */ 21746 rsm->r_flags |= RACK_TLP; 21747 rack->r_ctl.rc_tlpsend = NULL; 21748 sack_rxmit = 1; 21749 tlen = rsm->r_end - rsm->r_start; 21750 if (tlen > segsiz) 21751 tlen = segsiz; 21752 KASSERT(SEQ_LEQ(tp->snd_una, rsm->r_start), 21753 ("%s:%d: r.start:%u < SND.UNA:%u; tp:%p, rack:%p, rsm:%p", 21754 __func__, __LINE__, 21755 rsm->r_start, tp->snd_una, tp, rack, rsm)); 21756 sb_offset = rsm->r_start - tp->snd_una; 21757 cwin = min(tp->snd_wnd, tlen); 21758 len = cwin; 21759 } 21760 if (rack->r_must_retran && 21761 (doing_tlp == 0) && 21762 (SEQ_GT(tp->snd_max, tp->snd_una)) && 21763 (rsm == NULL)) { 21764 /* 21765 * There are two different ways that we 21766 * can get into this block: 21767 * a) This is a non-sack connection, we had a time-out 21768 * and thus r_must_retran was set and everything 21769 * left outstanding as been marked for retransmit. 21770 * b) The MTU of the path shrank, so that everything 21771 * was marked to be retransmitted with the smaller 21772 * mtu and r_must_retran was set. 21773 * 21774 * This means that we expect the sendmap (outstanding) 21775 * to all be marked must. We can use the tmap to 21776 * look at them. 21777 * 21778 */ 21779 int sendwin, flight; 21780 21781 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 21782 flight = ctf_flight_size(tp, rack->r_ctl.rc_out_at_rto); 21783 if (flight >= sendwin) { 21784 /* 21785 * We can't send yet. 21786 */ 21787 so = inp->inp_socket; 21788 sb = &so->so_snd; 21789 goto just_return_nolock; 21790 } 21791 /* 21792 * This is the case a/b mentioned above. All 21793 * outstanding/not-acked should be marked. 21794 * We can use the tmap to find them. 21795 */ 21796 rsm = TAILQ_FIRST(&rack->r_ctl.rc_tmap); 21797 if (rsm == NULL) { 21798 /* TSNH */ 21799 rack->r_must_retran = 0; 21800 rack->r_ctl.rc_out_at_rto = 0; 21801 so = inp->inp_socket; 21802 sb = &so->so_snd; 21803 goto just_return_nolock; 21804 } 21805 if ((rsm->r_flags & RACK_MUST_RXT) == 0) { 21806 /* 21807 * The first one does not have the flag, did we collapse 21808 * further up in our list? 21809 */ 21810 rack->r_must_retran = 0; 21811 rack->r_ctl.rc_out_at_rto = 0; 21812 rsm = NULL; 21813 sack_rxmit = 0; 21814 } else { 21815 sack_rxmit = 1; 21816 len = rsm->r_end - rsm->r_start; 21817 sb_offset = rsm->r_start - tp->snd_una; 21818 sendalot = 0; 21819 if ((rack->full_size_rxt == 0) && 21820 (rack->shape_rxt_to_pacing_min == 0) && 21821 (len >= segsiz)) 21822 len = segsiz; 21823 else if (rack->shape_rxt_to_pacing_min && 21824 rack->gp_ready) { 21825 /* We use pacing min as shaping len req */ 21826 uint32_t maxlen; 21827 21828 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz); 21829 if (len > maxlen) 21830 len = maxlen; 21831 } 21832 /* 21833 * Delay removing the flag RACK_MUST_RXT so 21834 * that the fastpath for retransmit will 21835 * work with this rsm. 21836 */ 21837 } 21838 } 21839 /* 21840 * Enforce a connection sendmap count limit if set 21841 * as long as we are not retransmiting. 21842 */ 21843 if ((rsm == NULL) && 21844 (rack->do_detection == 0) && 21845 (V_tcp_map_entries_limit > 0) && 21846 (rack->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 21847 counter_u64_add(rack_to_alloc_limited, 1); 21848 if (!rack->alloc_limit_reported) { 21849 rack->alloc_limit_reported = 1; 21850 counter_u64_add(rack_alloc_limited_conns, 1); 21851 } 21852 so = inp->inp_socket; 21853 sb = &so->so_snd; 21854 goto just_return_nolock; 21855 } 21856 if (rsm && (rsm->r_flags & RACK_HAS_FIN)) { 21857 /* we are retransmitting the fin */ 21858 len--; 21859 if (len) { 21860 /* 21861 * When retransmitting data do *not* include the 21862 * FIN. This could happen from a TLP probe. 21863 */ 21864 flags &= ~TH_FIN; 21865 } 21866 } 21867 if (rsm && rack->r_fsb_inited && 21868 rack_use_rsm_rfo && 21869 ((rsm->r_flags & RACK_HAS_FIN) == 0)) { 21870 int ret; 21871 21872 if ((rack->rc_policer_detected == 1) && 21873 (rack->r_ctl.policer_bucket_size > segsiz) && 21874 (rack->r_ctl.policer_bw > 0)) { 21875 /* Check to see if there is room */ 21876 if (rack->r_ctl.current_policer_bucket < len) { 21877 goto skip_fast_output; 21878 } 21879 } 21880 ret = rack_fast_rsm_output(tp, rack, rsm, ts_val, cts, ms_cts, &tv, len, doing_tlp); 21881 if (ret == 0) 21882 return (0); 21883 } 21884 skip_fast_output: 21885 so = inp->inp_socket; 21886 sb = &so->so_snd; 21887 if (do_a_prefetch == 0) { 21888 kern_prefetch(sb, &do_a_prefetch); 21889 do_a_prefetch = 1; 21890 } 21891 #ifdef NETFLIX_SHARED_CWND 21892 if ((tp->t_flags2 & TF2_TCP_SCWND_ALLOWED) && 21893 rack->rack_enable_scwnd) { 21894 /* We are doing cwnd sharing */ 21895 if (rack->gp_ready && 21896 (rack->rack_attempted_scwnd == 0) && 21897 (rack->r_ctl.rc_scw == NULL) && 21898 tp->t_lib) { 21899 /* The pcbid is in, lets make an attempt */ 21900 counter_u64_add(rack_try_scwnd, 1); 21901 rack->rack_attempted_scwnd = 1; 21902 rack->r_ctl.rc_scw = tcp_shared_cwnd_alloc(tp, 21903 &rack->r_ctl.rc_scw_index, 21904 segsiz); 21905 } 21906 if (rack->r_ctl.rc_scw && 21907 (rack->rack_scwnd_is_idle == 1) && 21908 sbavail(&so->so_snd)) { 21909 /* we are no longer out of data */ 21910 tcp_shared_cwnd_active(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 21911 rack->rack_scwnd_is_idle = 0; 21912 } 21913 if (rack->r_ctl.rc_scw) { 21914 /* First lets update and get the cwnd */ 21915 rack->r_ctl.cwnd_to_use = cwnd_to_use = tcp_shared_cwnd_update(rack->r_ctl.rc_scw, 21916 rack->r_ctl.rc_scw_index, 21917 tp->snd_cwnd, tp->snd_wnd, segsiz); 21918 } 21919 } 21920 #endif 21921 /* 21922 * Get standard flags, and add SYN or FIN if requested by 'hidden' 21923 * state flags. 21924 */ 21925 if (tp->t_flags & TF_NEEDFIN) 21926 flags |= TH_FIN; 21927 if (tp->t_flags & TF_NEEDSYN) 21928 flags |= TH_SYN; 21929 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 21930 void *end_rsm; 21931 end_rsm = TAILQ_LAST_FAST(&rack->r_ctl.rc_tmap, rack_sendmap, r_tnext); 21932 if (end_rsm) 21933 kern_prefetch(end_rsm, &prefetch_rsm); 21934 prefetch_rsm = 1; 21935 } 21936 SOCKBUF_LOCK(sb); 21937 if ((sack_rxmit == 0) && 21938 (TCPS_HAVEESTABLISHED(tp->t_state) || 21939 (tp->t_flags & TF_FASTOPEN))) { 21940 /* 21941 * We are not retransmitting (sack_rxmit is 0) so we 21942 * are sending new data. This is always based on snd_max. 21943 * Now in theory snd_max may be equal to snd_una, if so 21944 * then nothing is outstanding and the offset would be 0. 21945 */ 21946 uint32_t avail; 21947 21948 avail = sbavail(sb); 21949 if (SEQ_GT(tp->snd_max, tp->snd_una) && avail) 21950 sb_offset = tp->snd_max - tp->snd_una; 21951 else 21952 sb_offset = 0; 21953 if ((IN_FASTRECOVERY(tp->t_flags) == 0) || rack->rack_no_prr) { 21954 if (rack->r_ctl.rc_tlp_new_data) { 21955 /* TLP is forcing out new data */ 21956 if (rack->r_ctl.rc_tlp_new_data > (uint32_t) (avail - sb_offset)) { 21957 rack->r_ctl.rc_tlp_new_data = (uint32_t) (avail - sb_offset); 21958 } 21959 if ((rack->r_ctl.rc_tlp_new_data + sb_offset) > tp->snd_wnd) { 21960 if (tp->snd_wnd > sb_offset) 21961 len = tp->snd_wnd - sb_offset; 21962 else 21963 len = 0; 21964 } else { 21965 len = rack->r_ctl.rc_tlp_new_data; 21966 } 21967 rack->r_ctl.rc_tlp_new_data = 0; 21968 } else { 21969 len = rack_what_can_we_send(tp, rack, cwnd_to_use, avail, sb_offset); 21970 } 21971 if ((rack->r_ctl.crte == NULL) && 21972 IN_FASTRECOVERY(tp->t_flags) && 21973 (rack->full_size_rxt == 0) && 21974 (rack->shape_rxt_to_pacing_min == 0) && 21975 (len > segsiz)) { 21976 /* 21977 * For prr=off, we need to send only 1 MSS 21978 * at a time. We do this because another sack could 21979 * be arriving that causes us to send retransmits and 21980 * we don't want to be on a long pace due to a larger send 21981 * that keeps us from sending out the retransmit. 21982 */ 21983 len = segsiz; 21984 } else if (rack->shape_rxt_to_pacing_min && 21985 rack->gp_ready) { 21986 /* We use pacing min as shaping len req */ 21987 uint32_t maxlen; 21988 21989 maxlen = rack_get_hpts_pacing_min_for_bw(rack, segsiz); 21990 if (len > maxlen) 21991 len = maxlen; 21992 }/* The else is full_size_rxt is on so send it all */ 21993 } else { 21994 uint32_t outstanding; 21995 /* 21996 * We are inside of a Fast recovery episode, this 21997 * is caused by a SACK or 3 dup acks. At this point 21998 * we have sent all the retransmissions and we rely 21999 * on PRR to dictate what we will send in the form of 22000 * new data. 22001 */ 22002 22003 outstanding = tp->snd_max - tp->snd_una; 22004 if ((rack->r_ctl.rc_prr_sndcnt + outstanding) > tp->snd_wnd) { 22005 if (tp->snd_wnd > outstanding) { 22006 len = tp->snd_wnd - outstanding; 22007 /* Check to see if we have the data */ 22008 if ((sb_offset + len) > avail) { 22009 /* It does not all fit */ 22010 if (avail > sb_offset) 22011 len = avail - sb_offset; 22012 else 22013 len = 0; 22014 } 22015 } else { 22016 len = 0; 22017 } 22018 } else if (avail > sb_offset) { 22019 len = avail - sb_offset; 22020 } else { 22021 len = 0; 22022 } 22023 if (len > 0) { 22024 if (len > rack->r_ctl.rc_prr_sndcnt) { 22025 len = rack->r_ctl.rc_prr_sndcnt; 22026 } 22027 if (len > 0) { 22028 sub_from_prr = 1; 22029 } 22030 } 22031 if (len > segsiz) { 22032 /* 22033 * We should never send more than a MSS when 22034 * retransmitting or sending new data in prr 22035 * mode unless the override flag is on. Most 22036 * likely the PRR algorithm is not going to 22037 * let us send a lot as well :-) 22038 */ 22039 if (rack->r_ctl.rc_prr_sendalot == 0) { 22040 len = segsiz; 22041 } 22042 } else if (len < segsiz) { 22043 /* 22044 * Do we send any? The idea here is if the 22045 * send empty's the socket buffer we want to 22046 * do it. However if not then lets just wait 22047 * for our prr_sndcnt to get bigger. 22048 */ 22049 long leftinsb; 22050 22051 leftinsb = sbavail(sb) - sb_offset; 22052 if (leftinsb > len) { 22053 /* This send does not empty the sb */ 22054 len = 0; 22055 } 22056 } 22057 } 22058 } else if (!TCPS_HAVEESTABLISHED(tp->t_state)) { 22059 /* 22060 * If you have not established 22061 * and are not doing FAST OPEN 22062 * no data please. 22063 */ 22064 if ((sack_rxmit == 0) && 22065 !(tp->t_flags & TF_FASTOPEN)) { 22066 len = 0; 22067 sb_offset = 0; 22068 } 22069 } 22070 if (prefetch_so_done == 0) { 22071 kern_prefetch(so, &prefetch_so_done); 22072 prefetch_so_done = 1; 22073 } 22074 orig_len = len; 22075 if ((rack->rc_policer_detected == 1) && 22076 (rack->r_ctl.policer_bucket_size > segsiz) && 22077 (rack->r_ctl.policer_bw > 0) && 22078 (len > 0)) { 22079 /* 22080 * Ok we believe we have a policer watching 22081 * what we send, can we send len? If not can 22082 * we tune it down to a smaller value? 22083 */ 22084 uint32_t plen, buck_needs; 22085 22086 plen = rack_policer_check_send(rack, len, segsiz, &buck_needs); 22087 if (plen == 0) { 22088 /* 22089 * We are not allowed to send. How long 22090 * do we need to pace for i.e. how long 22091 * before len is available to send? 22092 */ 22093 uint64_t lentime; 22094 22095 lentime = buck_needs; 22096 lentime *= HPTS_USEC_IN_SEC; 22097 lentime /= rack->r_ctl.policer_bw; 22098 slot = (uint32_t)lentime; 22099 tot_len_this_send = 0; 22100 SOCKBUF_UNLOCK(sb); 22101 if (rack_verbose_logging > 0) 22102 policer_detection_log(rack, len, slot, buck_needs, 0, 12); 22103 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0); 22104 rack_log_type_just_return(rack, cts, 0, slot, hpts_calling, 0, cwnd_to_use); 22105 goto just_return_clean; 22106 } 22107 if (plen < len) { 22108 sendalot = 0; 22109 len = plen; 22110 } 22111 } 22112 /* 22113 * Lop off SYN bit if it has already been sent. However, if this is 22114 * SYN-SENT state and if segment contains data and if we don't know 22115 * that foreign host supports TAO, suppress sending segment. 22116 */ 22117 if ((flags & TH_SYN) && 22118 SEQ_GT(tp->snd_max, tp->snd_una) && 22119 ((sack_rxmit == 0) && 22120 (tp->t_rxtshift == 0))) { 22121 /* 22122 * When sending additional segments following a TFO SYN|ACK, 22123 * do not include the SYN bit. 22124 */ 22125 if ((tp->t_flags & TF_FASTOPEN) && 22126 (tp->t_state == TCPS_SYN_RECEIVED)) 22127 flags &= ~TH_SYN; 22128 } 22129 /* 22130 * Be careful not to send data and/or FIN on SYN segments. This 22131 * measure is needed to prevent interoperability problems with not 22132 * fully conformant TCP implementations. 22133 */ 22134 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 22135 len = 0; 22136 flags &= ~TH_FIN; 22137 } 22138 /* 22139 * On TFO sockets, ensure no data is sent in the following cases: 22140 * 22141 * - When retransmitting SYN|ACK on a passively-created socket 22142 * 22143 * - When retransmitting SYN on an actively created socket 22144 * 22145 * - When sending a zero-length cookie (cookie request) on an 22146 * actively created socket 22147 * 22148 * - When the socket is in the CLOSED state (RST is being sent) 22149 */ 22150 if ((tp->t_flags & TF_FASTOPEN) && 22151 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 22152 ((tp->t_state == TCPS_SYN_SENT) && 22153 (tp->t_tfo_client_cookie_len == 0)) || 22154 (flags & TH_RST))) { 22155 sack_rxmit = 0; 22156 len = 0; 22157 } 22158 /* Without fast-open there should never be data sent on a SYN */ 22159 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) { 22160 len = 0; 22161 } 22162 if ((len > segsiz) && (tcp_dsack_block_exists(tp))) { 22163 /* We only send 1 MSS if we have a DSACK block */ 22164 add_flag |= RACK_SENT_W_DSACK; 22165 len = segsiz; 22166 } 22167 if (len <= 0) { 22168 /* 22169 * We have nothing to send, or the window shrank, or 22170 * is closed, do we need to go into persists? 22171 */ 22172 len = 0; 22173 if ((tp->snd_wnd == 0) && 22174 (TCPS_HAVEESTABLISHED(tp->t_state)) && 22175 (tp->snd_una == tp->snd_max) && 22176 (sb_offset < (int)sbavail(sb))) { 22177 rack_enter_persist(tp, rack, cts, tp->snd_una); 22178 } 22179 } else if ((rsm == NULL) && 22180 (doing_tlp == 0) && 22181 (len < pace_max_seg)) { 22182 /* 22183 * We are not sending a maximum sized segment for 22184 * some reason. Should we not send anything (think 22185 * sws or persists)? 22186 */ 22187 if ((tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg)) && 22188 (TCPS_HAVEESTABLISHED(tp->t_state)) && 22189 (len < minseg) && 22190 (len < (int)(sbavail(sb) - sb_offset))) { 22191 /* 22192 * Here the rwnd is less than 22193 * the minimum pacing size, this is not a retransmit, 22194 * we are established and 22195 * the send is not the last in the socket buffer 22196 * we send nothing, and we may enter persists 22197 * if nothing is outstanding. 22198 */ 22199 len = 0; 22200 if (tp->snd_max == tp->snd_una) { 22201 /* 22202 * Nothing out we can 22203 * go into persists. 22204 */ 22205 rack_enter_persist(tp, rack, cts, tp->snd_una); 22206 } 22207 } else if ((cwnd_to_use >= max(minseg, (segsiz * 4))) && 22208 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) && 22209 (len < (int)(sbavail(sb) - sb_offset)) && 22210 (len < minseg)) { 22211 /* 22212 * Here we are not retransmitting, and 22213 * the cwnd is not so small that we could 22214 * not send at least a min size (rxt timer 22215 * not having gone off), We have 2 segments or 22216 * more already in flight, its not the tail end 22217 * of the socket buffer and the cwnd is blocking 22218 * us from sending out a minimum pacing segment size. 22219 * Lets not send anything. 22220 */ 22221 len = 0; 22222 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 22223 min((rack->r_ctl.rc_high_rwnd/2), minseg)) && 22224 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) > (2 * segsiz)) && 22225 (len < (int)(sbavail(sb) - sb_offset)) && 22226 (TCPS_HAVEESTABLISHED(tp->t_state))) { 22227 /* 22228 * Here we have a send window but we have 22229 * filled it up and we can't send another pacing segment. 22230 * We also have in flight more than 2 segments 22231 * and we are not completing the sb i.e. we allow 22232 * the last bytes of the sb to go out even if 22233 * its not a full pacing segment. 22234 */ 22235 len = 0; 22236 } else if ((rack->r_ctl.crte != NULL) && 22237 (tp->snd_wnd >= (pace_max_seg * max(1, rack_hw_rwnd_factor))) && 22238 (cwnd_to_use >= (pace_max_seg + (4 * segsiz))) && 22239 (ctf_flight_size(tp, rack->r_ctl.rc_sacked) >= (2 * segsiz)) && 22240 (len < (int)(sbavail(sb) - sb_offset))) { 22241 /* 22242 * Here we are doing hardware pacing, this is not a TLP, 22243 * we are not sending a pace max segment size, there is rwnd 22244 * room to send at least N pace_max_seg, the cwnd is greater 22245 * than or equal to a full pacing segments plus 4 mss and we have 2 or 22246 * more segments in flight and its not the tail of the socket buffer. 22247 * 22248 * We don't want to send instead we need to get more ack's in to 22249 * allow us to send a full pacing segment. Normally, if we are pacing 22250 * about the right speed, we should have finished our pacing 22251 * send as most of the acks have come back if we are at the 22252 * right rate. This is a bit fuzzy since return path delay 22253 * can delay the acks, which is why we want to make sure we 22254 * have cwnd space to have a bit more than a max pace segments in flight. 22255 * 22256 * If we have not gotten our acks back we are pacing at too high a 22257 * rate delaying will not hurt and will bring our GP estimate down by 22258 * injecting the delay. If we don't do this we will send 22259 * 2 MSS out in response to the acks being clocked in which 22260 * defeats the point of hw-pacing (i.e. to help us get 22261 * larger TSO's out). 22262 */ 22263 len = 0; 22264 } 22265 22266 } 22267 /* len will be >= 0 after this point. */ 22268 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 22269 rack_sndbuf_autoscale(rack); 22270 /* 22271 * Decide if we can use TCP Segmentation Offloading (if supported by 22272 * hardware). 22273 * 22274 * TSO may only be used if we are in a pure bulk sending state. The 22275 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 22276 * options prevent using TSO. With TSO the TCP header is the same 22277 * (except for the sequence number) for all generated packets. This 22278 * makes it impossible to transmit any options which vary per 22279 * generated segment or packet. 22280 * 22281 * IPv4 handling has a clear separation of ip options and ip header 22282 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 22283 * the right thing below to provide length of just ip options and thus 22284 * checking for ipoptlen is enough to decide if ip options are present. 22285 */ 22286 ipoptlen = 0; 22287 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 22288 /* 22289 * Pre-calculate here as we save another lookup into the darknesses 22290 * of IPsec that way and can actually decide if TSO is ok. 22291 */ 22292 #ifdef INET6 22293 if (isipv6 && IPSEC_ENABLED(ipv6)) 22294 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 22295 #ifdef INET 22296 else 22297 #endif 22298 #endif /* INET6 */ 22299 #ifdef INET 22300 if (IPSEC_ENABLED(ipv4)) 22301 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 22302 #endif /* INET */ 22303 #endif 22304 22305 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 22306 ipoptlen += ipsec_optlen; 22307 #endif 22308 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > segsiz && 22309 (tp->t_port == 0) && 22310 ((tp->t_flags & TF_SIGNATURE) == 0) && 22311 tp->rcv_numsacks == 0 && sack_rxmit == 0 && 22312 ipoptlen == 0) 22313 tso = 1; 22314 { 22315 uint32_t outstanding __unused; 22316 22317 outstanding = tp->snd_max - tp->snd_una; 22318 if (tp->t_flags & TF_SENTFIN) { 22319 /* 22320 * If we sent a fin, snd_max is 1 higher than 22321 * snd_una 22322 */ 22323 outstanding--; 22324 } 22325 if (sack_rxmit) { 22326 if ((rsm->r_flags & RACK_HAS_FIN) == 0) 22327 flags &= ~TH_FIN; 22328 } 22329 } 22330 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 22331 (long)TCP_MAXWIN << tp->rcv_scale); 22332 22333 /* 22334 * Sender silly window avoidance. We transmit under the following 22335 * conditions when len is non-zero: 22336 * 22337 * - We have a full segment (or more with TSO) - This is the last 22338 * buffer in a write()/send() and we are either idle or running 22339 * NODELAY - we've timed out (e.g. persist timer) - we have more 22340 * then 1/2 the maximum send window's worth of data (receiver may be 22341 * limited the window size) - we need to retransmit 22342 */ 22343 if (len) { 22344 if (len >= segsiz) { 22345 goto send; 22346 } 22347 /* 22348 * NOTE! on localhost connections an 'ack' from the remote 22349 * end may occur synchronously with the output and cause us 22350 * to flush a buffer queued with moretocome. XXX 22351 * 22352 */ 22353 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 22354 (idle || (tp->t_flags & TF_NODELAY)) && 22355 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) && 22356 (tp->t_flags & TF_NOPUSH) == 0) { 22357 pass = 2; 22358 goto send; 22359 } 22360 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 22361 pass = 22; 22362 goto send; 22363 } 22364 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 22365 pass = 4; 22366 goto send; 22367 } 22368 if (sack_rxmit) { 22369 pass = 6; 22370 goto send; 22371 } 22372 if (((tp->snd_wnd - ctf_outstanding(tp)) < segsiz) && 22373 (ctf_outstanding(tp) < (segsiz * 2))) { 22374 /* 22375 * We have less than two MSS outstanding (delayed ack) 22376 * and our rwnd will not let us send a full sized 22377 * MSS. Lets go ahead and let this small segment 22378 * out because we want to try to have at least two 22379 * packets inflight to not be caught by delayed ack. 22380 */ 22381 pass = 12; 22382 goto send; 22383 } 22384 } 22385 /* 22386 * Sending of standalone window updates. 22387 * 22388 * Window updates are important when we close our window due to a 22389 * full socket buffer and are opening it again after the application 22390 * reads data from it. Once the window has opened again and the 22391 * remote end starts to send again the ACK clock takes over and 22392 * provides the most current window information. 22393 * 22394 * We must avoid the silly window syndrome whereas every read from 22395 * the receive buffer, no matter how small, causes a window update 22396 * to be sent. We also should avoid sending a flurry of window 22397 * updates when the socket buffer had queued a lot of data and the 22398 * application is doing small reads. 22399 * 22400 * Prevent a flurry of pointless window updates by only sending an 22401 * update when we can increase the advertized window by more than 22402 * 1/4th of the socket buffer capacity. When the buffer is getting 22403 * full or is very small be more aggressive and send an update 22404 * whenever we can increase by two mss sized segments. In all other 22405 * situations the ACK's to new incoming data will carry further 22406 * window increases. 22407 * 22408 * Don't send an independent window update if a delayed ACK is 22409 * pending (it will get piggy-backed on it) or the remote side 22410 * already has done a half-close and won't send more data. Skip 22411 * this if the connection is in T/TCP half-open state. 22412 */ 22413 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 22414 !(tp->t_flags & TF_DELACK) && 22415 !TCPS_HAVERCVDFIN(tp->t_state)) { 22416 /* 22417 * "adv" is the amount we could increase the window, taking 22418 * into account that we are limited by TCP_MAXWIN << 22419 * tp->rcv_scale. 22420 */ 22421 int32_t adv; 22422 int oldwin; 22423 22424 adv = recwin; 22425 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 22426 oldwin = (tp->rcv_adv - tp->rcv_nxt); 22427 if (adv > oldwin) 22428 adv -= oldwin; 22429 else { 22430 /* We can't increase the window */ 22431 adv = 0; 22432 } 22433 } else 22434 oldwin = 0; 22435 22436 /* 22437 * If the new window size ends up being the same as or less 22438 * than the old size when it is scaled, then don't force 22439 * a window update. 22440 */ 22441 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 22442 goto dontupdate; 22443 22444 if (adv >= (int32_t)(2 * segsiz) && 22445 (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 22446 recwin <= (int32_t)(so->so_rcv.sb_hiwat / 8) || 22447 so->so_rcv.sb_hiwat <= 8 * segsiz)) { 22448 pass = 7; 22449 goto send; 22450 } 22451 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) { 22452 pass = 23; 22453 goto send; 22454 } 22455 } 22456 dontupdate: 22457 22458 /* 22459 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 22460 * is also a catch-all for the retransmit timer timeout case. 22461 */ 22462 if (tp->t_flags & TF_ACKNOW) { 22463 pass = 8; 22464 goto send; 22465 } 22466 if (((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) { 22467 pass = 9; 22468 goto send; 22469 } 22470 /* 22471 * If our state indicates that FIN should be sent and we have not 22472 * yet done so, then we need to send. 22473 */ 22474 if ((flags & TH_FIN) && 22475 (tp->snd_max == tp->snd_una)) { 22476 pass = 11; 22477 goto send; 22478 } 22479 /* 22480 * No reason to send a segment, just return. 22481 */ 22482 just_return: 22483 SOCKBUF_UNLOCK(sb); 22484 just_return_nolock: 22485 { 22486 int app_limited = CTF_JR_SENT_DATA; 22487 22488 if ((tp->t_flags & TF_FASTOPEN) == 0 && 22489 (flags & TH_FIN) && 22490 (len == 0) && 22491 (sbused(sb) == (tp->snd_max - tp->snd_una)) && 22492 ((tp->snd_max - tp->snd_una) <= segsiz)) { 22493 /* 22494 * Ok less than or right at a MSS is 22495 * outstanding. The original FreeBSD stack would 22496 * have sent a FIN, which can speed things up for 22497 * a transactional application doing a MSG_WAITALL. 22498 * To speed things up since we do *not* send a FIN 22499 * if data is outstanding, we send a "challenge ack". 22500 * The idea behind that is instead of having to have 22501 * the peer wait for the delayed-ack timer to run off 22502 * we send an ack that makes the peer send us an ack. 22503 */ 22504 rack_send_ack_challange(rack); 22505 } 22506 if (tot_len_this_send > 0) { 22507 rack->r_ctl.fsb.recwin = recwin; 22508 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, NULL, segsiz, __LINE__); 22509 if ((error == 0) && 22510 (rack->rc_policer_detected == 0) && 22511 rack_use_rfo && 22512 ((flags & (TH_SYN|TH_FIN)) == 0) && 22513 (ipoptlen == 0) && 22514 (tp->rcv_numsacks == 0) && 22515 rack->r_fsb_inited && 22516 TCPS_HAVEESTABLISHED(tp->t_state) && 22517 ((IN_RECOVERY(tp->t_flags)) == 0) && 22518 (rack->r_must_retran == 0) && 22519 ((tp->t_flags & TF_NEEDFIN) == 0) && 22520 (len > 0) && (orig_len > 0) && 22521 (orig_len > len) && 22522 ((orig_len - len) >= segsiz) && 22523 ((optlen == 0) || 22524 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 22525 /* We can send at least one more MSS using our fsb */ 22526 rack_setup_fast_output(tp, rack, sb, len, orig_len, 22527 segsiz, pace_max_seg, hw_tls, flags); 22528 } else 22529 rack->r_fast_output = 0; 22530 rack_log_fsb(rack, tp, so, flags, 22531 ipoptlen, orig_len, len, 0, 22532 1, optlen, __LINE__, 1); 22533 /* Assure when we leave that snd_nxt will point to top */ 22534 if (SEQ_GT(tp->snd_max, tp->snd_nxt)) 22535 tp->snd_nxt = tp->snd_max; 22536 } else { 22537 int end_window = 0; 22538 uint32_t seq = tp->gput_ack; 22539 22540 rsm = tqhash_max(rack->r_ctl.tqh); 22541 if (rsm) { 22542 /* 22543 * Mark the last sent that we just-returned (hinting 22544 * that delayed ack may play a role in any rtt measurement). 22545 */ 22546 rsm->r_just_ret = 1; 22547 } 22548 counter_u64_add(rack_out_size[TCP_MSS_ACCT_JUSTRET], 1); 22549 rack->r_ctl.rc_agg_delayed = 0; 22550 rack->r_early = 0; 22551 rack->r_late = 0; 22552 rack->r_ctl.rc_agg_early = 0; 22553 if ((ctf_outstanding(tp) + 22554 min(max(segsiz, (rack->r_ctl.rc_high_rwnd/2)), 22555 minseg)) >= tp->snd_wnd) { 22556 /* We are limited by the rwnd */ 22557 app_limited = CTF_JR_RWND_LIMITED; 22558 if (IN_FASTRECOVERY(tp->t_flags)) 22559 rack->r_ctl.rc_prr_sndcnt = 0; 22560 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 22561 /* We are limited by whats available -- app limited */ 22562 app_limited = CTF_JR_APP_LIMITED; 22563 if (IN_FASTRECOVERY(tp->t_flags)) 22564 rack->r_ctl.rc_prr_sndcnt = 0; 22565 } else if ((idle == 0) && 22566 ((tp->t_flags & TF_NODELAY) == 0) && 22567 ((uint32_t)len + (uint32_t)sb_offset >= sbavail(sb)) && 22568 (len < segsiz)) { 22569 /* 22570 * No delay is not on and the 22571 * user is sending less than 1MSS. This 22572 * brings out SWS avoidance so we 22573 * don't send. Another app-limited case. 22574 */ 22575 app_limited = CTF_JR_APP_LIMITED; 22576 } else if (tp->t_flags & TF_NOPUSH) { 22577 /* 22578 * The user has requested no push of 22579 * the last segment and we are 22580 * at the last segment. Another app 22581 * limited case. 22582 */ 22583 app_limited = CTF_JR_APP_LIMITED; 22584 } else if ((ctf_outstanding(tp) + minseg) > cwnd_to_use) { 22585 /* Its the cwnd */ 22586 app_limited = CTF_JR_CWND_LIMITED; 22587 } else if (IN_FASTRECOVERY(tp->t_flags) && 22588 (rack->rack_no_prr == 0) && 22589 (rack->r_ctl.rc_prr_sndcnt < segsiz)) { 22590 app_limited = CTF_JR_PRR; 22591 } else { 22592 /* Now why here are we not sending? */ 22593 #ifdef NOW 22594 #ifdef INVARIANTS 22595 panic("rack:%p hit JR_ASSESSING case cwnd_to_use:%u?", rack, cwnd_to_use); 22596 #endif 22597 #endif 22598 app_limited = CTF_JR_ASSESSING; 22599 } 22600 /* 22601 * App limited in some fashion, for our pacing GP 22602 * measurements we don't want any gap (even cwnd). 22603 * Close down the measurement window. 22604 */ 22605 if (rack_cwnd_block_ends_measure && 22606 ((app_limited == CTF_JR_CWND_LIMITED) || 22607 (app_limited == CTF_JR_PRR))) { 22608 /* 22609 * The reason we are not sending is 22610 * the cwnd (or prr). We have been configured 22611 * to end the measurement window in 22612 * this case. 22613 */ 22614 end_window = 1; 22615 } else if (rack_rwnd_block_ends_measure && 22616 (app_limited == CTF_JR_RWND_LIMITED)) { 22617 /* 22618 * We are rwnd limited and have been 22619 * configured to end the measurement 22620 * window in this case. 22621 */ 22622 end_window = 1; 22623 } else if (app_limited == CTF_JR_APP_LIMITED) { 22624 /* 22625 * A true application limited period, we have 22626 * ran out of data. 22627 */ 22628 end_window = 1; 22629 } else if (app_limited == CTF_JR_ASSESSING) { 22630 /* 22631 * In the assessing case we hit the end of 22632 * the if/else and had no known reason 22633 * This will panic us under invariants.. 22634 * 22635 * If we get this out in logs we need to 22636 * investagate which reason we missed. 22637 */ 22638 end_window = 1; 22639 } 22640 if (end_window) { 22641 uint8_t log = 0; 22642 22643 /* Adjust the Gput measurement */ 22644 if ((tp->t_flags & TF_GPUTINPROG) && 22645 SEQ_GT(tp->gput_ack, tp->snd_max)) { 22646 tp->gput_ack = tp->snd_max; 22647 if ((tp->gput_ack - tp->gput_seq) < (MIN_GP_WIN * segsiz)) { 22648 /* 22649 * There is not enough to measure. 22650 */ 22651 tp->t_flags &= ~TF_GPUTINPROG; 22652 rack_log_pacing_delay_calc(rack, (tp->gput_ack - tp->gput_seq) /*flex2*/, 22653 rack->r_ctl.rc_gp_srtt /*flex1*/, 22654 tp->gput_seq, 22655 0, 0, 18, __LINE__, NULL, 0); 22656 } else 22657 log = 1; 22658 } 22659 /* Mark the last packet has app limited */ 22660 rsm = tqhash_max(rack->r_ctl.tqh); 22661 if (rsm && ((rsm->r_flags & RACK_APP_LIMITED) == 0)) { 22662 if (rack->r_ctl.rc_app_limited_cnt == 0) 22663 rack->r_ctl.rc_end_appl = rack->r_ctl.rc_first_appl = rsm; 22664 else { 22665 /* 22666 * Go out to the end app limited and mark 22667 * this new one as next and move the end_appl up 22668 * to this guy. 22669 */ 22670 if (rack->r_ctl.rc_end_appl) 22671 rack->r_ctl.rc_end_appl->r_nseq_appl = rsm->r_start; 22672 rack->r_ctl.rc_end_appl = rsm; 22673 } 22674 rsm->r_flags |= RACK_APP_LIMITED; 22675 rack->r_ctl.rc_app_limited_cnt++; 22676 } 22677 if (log) 22678 rack_log_pacing_delay_calc(rack, 22679 rack->r_ctl.rc_app_limited_cnt, seq, 22680 tp->gput_ack, 0, 0, 4, __LINE__, NULL, 0); 22681 } 22682 } 22683 /* Check if we need to go into persists or not */ 22684 if ((tp->snd_max == tp->snd_una) && 22685 TCPS_HAVEESTABLISHED(tp->t_state) && 22686 sbavail(sb) && 22687 (sbavail(sb) > tp->snd_wnd) && 22688 (tp->snd_wnd < min((rack->r_ctl.rc_high_rwnd/2), minseg))) { 22689 /* Yes lets make sure to move to persist before timer-start */ 22690 rack_enter_persist(tp, rack, rack->r_ctl.rc_rcvtime, tp->snd_una); 22691 } 22692 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, sup_rack); 22693 rack_log_type_just_return(rack, cts, tot_len_this_send, slot, hpts_calling, app_limited, cwnd_to_use); 22694 } 22695 just_return_clean: 22696 #ifdef NETFLIX_SHARED_CWND 22697 if ((sbavail(sb) == 0) && 22698 rack->r_ctl.rc_scw) { 22699 tcp_shared_cwnd_idle(rack->r_ctl.rc_scw, rack->r_ctl.rc_scw_index); 22700 rack->rack_scwnd_is_idle = 1; 22701 } 22702 #endif 22703 #ifdef TCP_ACCOUNTING 22704 if (tot_len_this_send > 0) { 22705 crtsc = get_cyclecount(); 22706 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22707 tp->tcp_cnt_counters[SND_OUT_DATA]++; 22708 } 22709 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22710 tp->tcp_proc_time[SND_OUT_DATA] += (crtsc - ts_val); 22711 } 22712 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22713 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) / segsiz); 22714 } 22715 } else { 22716 crtsc = get_cyclecount(); 22717 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22718 tp->tcp_cnt_counters[SND_LIMITED]++; 22719 } 22720 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22721 tp->tcp_proc_time[SND_LIMITED] += (crtsc - ts_val); 22722 } 22723 } 22724 sched_unpin(); 22725 #endif 22726 return (0); 22727 22728 send: 22729 if ((rack->r_ctl.crte != NULL) && 22730 (rsm == NULL) && 22731 ((rack->rc_hw_nobuf == 1) || 22732 (rack_hw_check_queue && (check_done == 0)))) { 22733 /* 22734 * We only want to do this once with the hw_check_queue, 22735 * for the enobuf case we would only do it once if 22736 * we come around to again, the flag will be clear. 22737 */ 22738 check_done = 1; 22739 slot = rack_check_queue_level(rack, tp, &tv, cts, len, segsiz); 22740 if (slot) { 22741 rack->r_ctl.rc_agg_delayed = 0; 22742 rack->r_ctl.rc_agg_early = 0; 22743 rack->r_early = 0; 22744 rack->r_late = 0; 22745 SOCKBUF_UNLOCK(&so->so_snd); 22746 goto skip_all_send; 22747 } 22748 } 22749 if (rsm || sack_rxmit) 22750 counter_u64_add(rack_nfto_resend, 1); 22751 else 22752 counter_u64_add(rack_non_fto_send, 1); 22753 if ((flags & TH_FIN) && 22754 sbavail(sb)) { 22755 /* 22756 * We do not transmit a FIN 22757 * with data outstanding. We 22758 * need to make it so all data 22759 * is acked first. 22760 */ 22761 flags &= ~TH_FIN; 22762 if ((sbused(sb) == (tp->snd_max - tp->snd_una)) && 22763 ((tp->snd_max - tp->snd_una) <= segsiz)) { 22764 /* 22765 * Ok less than or right at a MSS is 22766 * outstanding. The original FreeBSD stack would 22767 * have sent a FIN, which can speed things up for 22768 * a transactional application doing a MSG_WAITALL. 22769 * To speed things up since we do *not* send a FIN 22770 * if data is outstanding, we send a "challenge ack". 22771 * The idea behind that is instead of having to have 22772 * the peer wait for the delayed-ack timer to run off 22773 * we send an ack that makes the peer send us an ack. 22774 */ 22775 rack_send_ack_challange(rack); 22776 } 22777 } 22778 /* Enforce stack imposed max seg size if we have one */ 22779 if (pace_max_seg && 22780 (len > pace_max_seg)) { 22781 mark = 1; 22782 len = pace_max_seg; 22783 } 22784 if ((rsm == NULL) && 22785 (rack->pcm_in_progress == 0) && 22786 (rack->r_ctl.pcm_max_seg > 0) && 22787 (len >= rack->r_ctl.pcm_max_seg)) { 22788 /* It is large enough for a measurement */ 22789 add_flag |= RACK_IS_PCM; 22790 rack_log_pcm(rack, 5, len, rack->r_ctl.pcm_max_seg, add_flag); 22791 } else if (rack_verbose_logging) { 22792 rack_log_pcm(rack, 6, len, rack->r_ctl.pcm_max_seg, add_flag); 22793 } 22794 22795 SOCKBUF_LOCK_ASSERT(sb); 22796 if (len > 0) { 22797 if (len >= segsiz) 22798 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 22799 else 22800 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 22801 } 22802 /* 22803 * Before ESTABLISHED, force sending of initial options unless TCP 22804 * set not to do any options. NOTE: we assume that the IP/TCP header 22805 * plus TCP options always fit in a single mbuf, leaving room for a 22806 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 22807 * + optlen <= MCLBYTES 22808 */ 22809 optlen = 0; 22810 #ifdef INET6 22811 if (isipv6) 22812 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 22813 else 22814 #endif 22815 hdrlen = sizeof(struct tcpiphdr); 22816 22817 /* 22818 * Ok what seq are we sending from. If we have 22819 * no rsm to use, then we look at various bits, 22820 * if we are putting out a SYN it will be ISS. 22821 * If we are retransmitting a FIN it will 22822 * be snd_max-1 else its snd_max. 22823 */ 22824 if (rsm == NULL) { 22825 if (flags & TH_SYN) 22826 rack_seq = tp->iss; 22827 else if ((flags & TH_FIN) && 22828 (tp->t_flags & TF_SENTFIN)) 22829 rack_seq = tp->snd_max - 1; 22830 else 22831 rack_seq = tp->snd_max; 22832 } else { 22833 rack_seq = rsm->r_start; 22834 } 22835 /* 22836 * Compute options for segment. We only have to care about SYN and 22837 * established connection segments. Options for SYN-ACK segments 22838 * are handled in TCP syncache. 22839 */ 22840 to.to_flags = 0; 22841 if ((tp->t_flags & TF_NOOPT) == 0) { 22842 /* Maximum segment size. */ 22843 if (flags & TH_SYN) { 22844 to.to_mss = tcp_mssopt(&inp->inp_inc); 22845 if (tp->t_port) 22846 to.to_mss -= V_tcp_udp_tunneling_overhead; 22847 to.to_flags |= TOF_MSS; 22848 22849 /* 22850 * On SYN or SYN|ACK transmits on TFO connections, 22851 * only include the TFO option if it is not a 22852 * retransmit, as the presence of the TFO option may 22853 * have caused the original SYN or SYN|ACK to have 22854 * been dropped by a middlebox. 22855 */ 22856 if ((tp->t_flags & TF_FASTOPEN) && 22857 (tp->t_rxtshift == 0)) { 22858 if (tp->t_state == TCPS_SYN_RECEIVED) { 22859 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 22860 to.to_tfo_cookie = 22861 (u_int8_t *)&tp->t_tfo_cookie.server; 22862 to.to_flags |= TOF_FASTOPEN; 22863 wanted_cookie = 1; 22864 } else if (tp->t_state == TCPS_SYN_SENT) { 22865 to.to_tfo_len = 22866 tp->t_tfo_client_cookie_len; 22867 to.to_tfo_cookie = 22868 tp->t_tfo_cookie.client; 22869 to.to_flags |= TOF_FASTOPEN; 22870 wanted_cookie = 1; 22871 /* 22872 * If we wind up having more data to 22873 * send with the SYN than can fit in 22874 * one segment, don't send any more 22875 * until the SYN|ACK comes back from 22876 * the other end. 22877 */ 22878 sendalot = 0; 22879 } 22880 } 22881 } 22882 /* Window scaling. */ 22883 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 22884 to.to_wscale = tp->request_r_scale; 22885 to.to_flags |= TOF_SCALE; 22886 } 22887 /* Timestamps. */ 22888 if ((tp->t_flags & TF_RCVD_TSTMP) || 22889 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 22890 uint32_t ts_to_use; 22891 22892 if ((rack->r_rcvpath_rtt_up == 1) && 22893 (ms_cts == rack->r_ctl.last_rcv_tstmp_for_rtt)) { 22894 /* 22895 * When we are doing a rcv_rtt probe all 22896 * other timestamps use the next msec. This 22897 * is safe since our previous ack is in the 22898 * air and we will just have a few more 22899 * on the next ms. This assures that only 22900 * the one ack has the ms_cts that was on 22901 * our ack-probe. 22902 */ 22903 ts_to_use = ms_cts + 1; 22904 } else { 22905 ts_to_use = ms_cts; 22906 } 22907 to.to_tsval = ts_to_use + tp->ts_offset; 22908 to.to_tsecr = tp->ts_recent; 22909 to.to_flags |= TOF_TS; 22910 if ((len == 0) && 22911 (TCPS_HAVEESTABLISHED(tp->t_state)) && 22912 ((ms_cts - rack->r_ctl.last_rcv_tstmp_for_rtt) > RCV_PATH_RTT_MS) && 22913 (tp->snd_una == tp->snd_max) && 22914 (flags & TH_ACK) && 22915 (sbavail(sb) == 0) && 22916 (rack->r_ctl.current_round != 0) && 22917 ((flags & (TH_SYN|TH_FIN)) == 0) && 22918 (rack->r_rcvpath_rtt_up == 0)) { 22919 rack->r_ctl.last_rcv_tstmp_for_rtt = ms_cts; 22920 rack->r_ctl.last_time_of_arm_rcv = cts; 22921 rack->r_rcvpath_rtt_up = 1; 22922 /* Subtract 1 from seq to force a response */ 22923 rack_seq--; 22924 } 22925 } 22926 /* Set receive buffer autosizing timestamp. */ 22927 if (tp->rfbuf_ts == 0 && 22928 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 22929 tp->rfbuf_ts = ms_cts; 22930 } 22931 /* Selective ACK's. */ 22932 if (tp->t_flags & TF_SACK_PERMIT) { 22933 if (flags & TH_SYN) 22934 to.to_flags |= TOF_SACKPERM; 22935 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 22936 tp->rcv_numsacks > 0) { 22937 to.to_flags |= TOF_SACK; 22938 to.to_nsacks = tp->rcv_numsacks; 22939 to.to_sacks = (u_char *)tp->sackblks; 22940 } 22941 } 22942 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 22943 /* TCP-MD5 (RFC2385). */ 22944 if (tp->t_flags & TF_SIGNATURE) 22945 to.to_flags |= TOF_SIGNATURE; 22946 #endif 22947 22948 /* Processing the options. */ 22949 hdrlen += optlen = tcp_addoptions(&to, opt); 22950 /* 22951 * If we wanted a TFO option to be added, but it was unable 22952 * to fit, ensure no data is sent. 22953 */ 22954 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 22955 !(to.to_flags & TOF_FASTOPEN)) 22956 len = 0; 22957 } 22958 if (tp->t_port) { 22959 if (V_tcp_udp_tunneling_port == 0) { 22960 /* The port was removed?? */ 22961 SOCKBUF_UNLOCK(&so->so_snd); 22962 #ifdef TCP_ACCOUNTING 22963 crtsc = get_cyclecount(); 22964 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22965 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 22966 } 22967 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 22968 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 22969 } 22970 sched_unpin(); 22971 #endif 22972 return (EHOSTUNREACH); 22973 } 22974 hdrlen += sizeof(struct udphdr); 22975 } 22976 #ifdef INET6 22977 if (isipv6) 22978 ipoptlen = ip6_optlen(inp); 22979 else 22980 #endif 22981 if (inp->inp_options) 22982 ipoptlen = inp->inp_options->m_len - 22983 offsetof(struct ipoption, ipopt_list); 22984 else 22985 ipoptlen = 0; 22986 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 22987 ipoptlen += ipsec_optlen; 22988 #endif 22989 22990 /* 22991 * Adjust data length if insertion of options will bump the packet 22992 * length beyond the t_maxseg length. Clear the FIN bit because we 22993 * cut off the tail of the segment. 22994 */ 22995 if (len + optlen + ipoptlen > tp->t_maxseg) { 22996 if (tso) { 22997 uint32_t if_hw_tsomax; 22998 uint32_t moff; 22999 int32_t max_len; 23000 23001 /* extract TSO information */ 23002 if_hw_tsomax = tp->t_tsomax; 23003 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 23004 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 23005 KASSERT(ipoptlen == 0, 23006 ("%s: TSO can't do IP options", __func__)); 23007 23008 /* 23009 * Check if we should limit by maximum payload 23010 * length: 23011 */ 23012 if (if_hw_tsomax != 0) { 23013 /* compute maximum TSO length */ 23014 max_len = (if_hw_tsomax - hdrlen - 23015 max_linkhdr); 23016 if (max_len <= 0) { 23017 len = 0; 23018 } else if (len > max_len) { 23019 sendalot = 1; 23020 len = max_len; 23021 mark = 2; 23022 } 23023 } 23024 /* 23025 * Prevent the last segment from being fractional 23026 * unless the send sockbuf can be emptied: 23027 */ 23028 max_len = (tp->t_maxseg - optlen); 23029 if ((sb_offset + len) < sbavail(sb)) { 23030 moff = len % (u_int)max_len; 23031 if (moff != 0) { 23032 mark = 3; 23033 len -= moff; 23034 } 23035 } 23036 /* 23037 * In case there are too many small fragments don't 23038 * use TSO: 23039 */ 23040 if (len <= max_len) { 23041 mark = 4; 23042 tso = 0; 23043 } 23044 /* 23045 * Send the FIN in a separate segment after the bulk 23046 * sending is done. We don't trust the TSO 23047 * implementations to clear the FIN flag on all but 23048 * the last segment. 23049 */ 23050 if (tp->t_flags & TF_NEEDFIN) { 23051 sendalot = 4; 23052 } 23053 } else { 23054 mark = 5; 23055 if (optlen + ipoptlen >= tp->t_maxseg) { 23056 /* 23057 * Since we don't have enough space to put 23058 * the IP header chain and the TCP header in 23059 * one packet as required by RFC 7112, don't 23060 * send it. Also ensure that at least one 23061 * byte of the payload can be put into the 23062 * TCP segment. 23063 */ 23064 SOCKBUF_UNLOCK(&so->so_snd); 23065 error = EMSGSIZE; 23066 sack_rxmit = 0; 23067 goto out; 23068 } 23069 len = tp->t_maxseg - optlen - ipoptlen; 23070 sendalot = 5; 23071 } 23072 } else { 23073 tso = 0; 23074 mark = 6; 23075 } 23076 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 23077 ("%s: len > IP_MAXPACKET", __func__)); 23078 #ifdef DIAGNOSTIC 23079 #ifdef INET6 23080 if (max_linkhdr + hdrlen > MCLBYTES) 23081 #else 23082 if (max_linkhdr + hdrlen > MHLEN) 23083 #endif 23084 panic("tcphdr too big"); 23085 #endif 23086 23087 /* 23088 * This KASSERT is here to catch edge cases at a well defined place. 23089 * Before, those had triggered (random) panic conditions further 23090 * down. 23091 */ 23092 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 23093 if ((len == 0) && 23094 (flags & TH_FIN) && 23095 (sbused(sb))) { 23096 /* 23097 * We have outstanding data, don't send a fin by itself!. 23098 * 23099 * Check to see if we need to send a challenge ack. 23100 */ 23101 if ((sbused(sb) == (tp->snd_max - tp->snd_una)) && 23102 ((tp->snd_max - tp->snd_una) <= segsiz)) { 23103 /* 23104 * Ok less than or right at a MSS is 23105 * outstanding. The original FreeBSD stack would 23106 * have sent a FIN, which can speed things up for 23107 * a transactional application doing a MSG_WAITALL. 23108 * To speed things up since we do *not* send a FIN 23109 * if data is outstanding, we send a "challenge ack". 23110 * The idea behind that is instead of having to have 23111 * the peer wait for the delayed-ack timer to run off 23112 * we send an ack that makes the peer send us an ack. 23113 */ 23114 rack_send_ack_challange(rack); 23115 } 23116 goto just_return; 23117 } 23118 /* 23119 * Grab a header mbuf, attaching a copy of data to be transmitted, 23120 * and initialize the header from the template for sends on this 23121 * connection. 23122 */ 23123 hw_tls = tp->t_nic_ktls_xmit != 0; 23124 if (len) { 23125 uint32_t max_val; 23126 uint32_t moff; 23127 23128 if (pace_max_seg) 23129 max_val = pace_max_seg; 23130 else 23131 max_val = len; 23132 /* 23133 * We allow a limit on sending with hptsi. 23134 */ 23135 if (len > max_val) { 23136 mark = 7; 23137 len = max_val; 23138 } 23139 #ifdef INET6 23140 if (MHLEN < hdrlen + max_linkhdr) 23141 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 23142 else 23143 #endif 23144 m = m_gethdr(M_NOWAIT, MT_DATA); 23145 23146 if (m == NULL) { 23147 SOCKBUF_UNLOCK(sb); 23148 error = ENOBUFS; 23149 sack_rxmit = 0; 23150 goto out; 23151 } 23152 m->m_data += max_linkhdr; 23153 m->m_len = hdrlen; 23154 23155 /* 23156 * Start the m_copy functions from the closest mbuf to the 23157 * sb_offset in the socket buffer chain. 23158 */ 23159 mb = sbsndptr_noadv(sb, sb_offset, &moff); 23160 s_mb = mb; 23161 s_moff = moff; 23162 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 23163 m_copydata(mb, moff, (int)len, 23164 mtod(m, caddr_t)+hdrlen); 23165 /* 23166 * If we are not retransmitting advance the 23167 * sndptr to help remember the next place in 23168 * the sb. 23169 */ 23170 if (rsm == NULL) 23171 sbsndptr_adv(sb, mb, len); 23172 m->m_len += len; 23173 } else { 23174 struct sockbuf *msb; 23175 23176 /* 23177 * If we are not retransmitting pass in msb so 23178 * the socket buffer can be advanced. Otherwise 23179 * set it to NULL if its a retransmission since 23180 * we don't want to change the sb remembered 23181 * location. 23182 */ 23183 if (rsm == NULL) 23184 msb = sb; 23185 else 23186 msb = NULL; 23187 m->m_next = tcp_m_copym( 23188 mb, moff, &len, 23189 if_hw_tsomaxsegcount, if_hw_tsomaxsegsize, msb, 23190 ((rsm == NULL) ? hw_tls : 0) 23191 #ifdef NETFLIX_COPY_ARGS 23192 , &s_mb, &s_moff 23193 #endif 23194 ); 23195 if (len <= (tp->t_maxseg - optlen)) { 23196 /* 23197 * Must have ran out of mbufs for the copy 23198 * shorten it to no longer need tso. Lets 23199 * not put on sendalot since we are low on 23200 * mbufs. 23201 */ 23202 tso = 0; 23203 } 23204 if (m->m_next == NULL) { 23205 SOCKBUF_UNLOCK(sb); 23206 (void)m_free(m); 23207 error = ENOBUFS; 23208 sack_rxmit = 0; 23209 goto out; 23210 } 23211 } 23212 if (sack_rxmit) { 23213 if (rsm && (rsm->r_flags & RACK_TLP)) { 23214 /* 23215 * TLP should not count in retran count, but 23216 * in its own bin 23217 */ 23218 counter_u64_add(rack_tlp_retran, 1); 23219 counter_u64_add(rack_tlp_retran_bytes, len); 23220 } else { 23221 tp->t_sndrexmitpack++; 23222 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 23223 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 23224 } 23225 #ifdef STATS 23226 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 23227 len); 23228 #endif 23229 } else { 23230 KMOD_TCPSTAT_INC(tcps_sndpack); 23231 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 23232 #ifdef STATS 23233 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 23234 len); 23235 #endif 23236 } 23237 /* 23238 * If we're sending everything we've got, set PUSH. (This 23239 * will keep happy those implementations which only give 23240 * data to the user when a buffer fills or a PUSH comes in.) 23241 */ 23242 if (sb_offset + len == sbused(sb) && 23243 sbused(sb) && 23244 !(flags & TH_SYN)) { 23245 flags |= TH_PUSH; 23246 add_flag |= RACK_HAD_PUSH; 23247 } 23248 23249 SOCKBUF_UNLOCK(sb); 23250 } else { 23251 SOCKBUF_UNLOCK(sb); 23252 if (tp->t_flags & TF_ACKNOW) 23253 KMOD_TCPSTAT_INC(tcps_sndacks); 23254 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 23255 KMOD_TCPSTAT_INC(tcps_sndctrl); 23256 else 23257 KMOD_TCPSTAT_INC(tcps_sndwinup); 23258 23259 m = m_gethdr(M_NOWAIT, MT_DATA); 23260 if (m == NULL) { 23261 error = ENOBUFS; 23262 sack_rxmit = 0; 23263 goto out; 23264 } 23265 #ifdef INET6 23266 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 23267 MHLEN >= hdrlen) { 23268 M_ALIGN(m, hdrlen); 23269 } else 23270 #endif 23271 m->m_data += max_linkhdr; 23272 m->m_len = hdrlen; 23273 } 23274 SOCKBUF_UNLOCK_ASSERT(sb); 23275 m->m_pkthdr.rcvif = (struct ifnet *)0; 23276 #ifdef MAC 23277 mac_inpcb_create_mbuf(inp, m); 23278 #endif 23279 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) { 23280 #ifdef INET6 23281 if (isipv6) 23282 ip6 = (struct ip6_hdr *)rack->r_ctl.fsb.tcp_ip_hdr; 23283 else 23284 #endif /* INET6 */ 23285 #ifdef INET 23286 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 23287 #endif 23288 th = rack->r_ctl.fsb.th; 23289 udp = rack->r_ctl.fsb.udp; 23290 if (udp) { 23291 #ifdef INET6 23292 if (isipv6) 23293 ulen = hdrlen + len - sizeof(struct ip6_hdr); 23294 else 23295 #endif /* INET6 */ 23296 ulen = hdrlen + len - sizeof(struct ip); 23297 udp->uh_ulen = htons(ulen); 23298 } 23299 } else { 23300 #ifdef INET6 23301 if (isipv6) { 23302 ip6 = mtod(m, struct ip6_hdr *); 23303 if (tp->t_port) { 23304 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 23305 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 23306 udp->uh_dport = tp->t_port; 23307 ulen = hdrlen + len - sizeof(struct ip6_hdr); 23308 udp->uh_ulen = htons(ulen); 23309 th = (struct tcphdr *)(udp + 1); 23310 } else 23311 th = (struct tcphdr *)(ip6 + 1); 23312 tcpip_fillheaders(inp, tp->t_port, ip6, th); 23313 } else 23314 #endif /* INET6 */ 23315 { 23316 #ifdef INET 23317 ip = mtod(m, struct ip *); 23318 if (tp->t_port) { 23319 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 23320 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 23321 udp->uh_dport = tp->t_port; 23322 ulen = hdrlen + len - sizeof(struct ip); 23323 udp->uh_ulen = htons(ulen); 23324 th = (struct tcphdr *)(udp + 1); 23325 } else 23326 th = (struct tcphdr *)(ip + 1); 23327 tcpip_fillheaders(inp, tp->t_port, ip, th); 23328 #endif 23329 } 23330 } 23331 /* 23332 * If we are starting a connection, send ECN setup SYN packet. If we 23333 * are on a retransmit, we may resend those bits a number of times 23334 * as per RFC 3168. 23335 */ 23336 if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 23337 flags |= tcp_ecn_output_syn_sent(tp); 23338 } 23339 /* Also handle parallel SYN for ECN */ 23340 if (TCPS_HAVERCVDSYN(tp->t_state) && 23341 (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 23342 int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit); 23343 if ((tp->t_state == TCPS_SYN_RECEIVED) && 23344 (tp->t_flags2 & TF2_ECN_SND_ECE)) 23345 tp->t_flags2 &= ~TF2_ECN_SND_ECE; 23346 #ifdef INET6 23347 if (isipv6) { 23348 ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20); 23349 ip6->ip6_flow |= htonl(ect << 20); 23350 } 23351 else 23352 #endif 23353 { 23354 #ifdef INET 23355 ip->ip_tos &= ~IPTOS_ECN_MASK; 23356 ip->ip_tos |= ect; 23357 #endif 23358 } 23359 } 23360 th->th_seq = htonl(rack_seq); 23361 th->th_ack = htonl(tp->rcv_nxt); 23362 tcp_set_flags(th, flags); 23363 /* 23364 * Calculate receive window. Don't shrink window, but avoid silly 23365 * window syndrome. 23366 * If a RST segment is sent, advertise a window of zero. 23367 */ 23368 if (flags & TH_RST) { 23369 recwin = 0; 23370 } else { 23371 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 23372 recwin < (long)segsiz) { 23373 recwin = 0; 23374 } 23375 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 23376 recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 23377 recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 23378 } 23379 23380 /* 23381 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 23382 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 23383 * handled in syncache. 23384 */ 23385 if (flags & TH_SYN) 23386 th->th_win = htons((u_short) 23387 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 23388 else { 23389 /* Avoid shrinking window with window scaling. */ 23390 recwin = roundup2(recwin, 1 << tp->rcv_scale); 23391 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 23392 } 23393 /* 23394 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 23395 * window. This may cause the remote transmitter to stall. This 23396 * flag tells soreceive() to disable delayed acknowledgements when 23397 * draining the buffer. This can occur if the receiver is 23398 * attempting to read more data than can be buffered prior to 23399 * transmitting on the connection. 23400 */ 23401 if (th->th_win == 0) { 23402 tp->t_sndzerowin++; 23403 tp->t_flags |= TF_RXWIN0SENT; 23404 } else 23405 tp->t_flags &= ~TF_RXWIN0SENT; 23406 tp->snd_up = tp->snd_una; /* drag it along, its deprecated */ 23407 /* Now are we using fsb?, if so copy the template data to the mbuf */ 23408 if ((ipoptlen == 0) && (rack->r_ctl.fsb.tcp_ip_hdr) && rack->r_fsb_inited) { 23409 uint8_t *cpto; 23410 23411 cpto = mtod(m, uint8_t *); 23412 memcpy(cpto, rack->r_ctl.fsb.tcp_ip_hdr, rack->r_ctl.fsb.tcp_ip_hdr_len); 23413 /* 23414 * We have just copied in: 23415 * IP/IP6 23416 * <optional udphdr> 23417 * tcphdr (no options) 23418 * 23419 * We need to grab the correct pointers into the mbuf 23420 * for both the tcp header, and possibly the udp header (if tunneling). 23421 * We do this by using the offset in the copy buffer and adding it 23422 * to the mbuf base pointer (cpto). 23423 */ 23424 #ifdef INET6 23425 if (isipv6) 23426 ip6 = mtod(m, struct ip6_hdr *); 23427 else 23428 #endif /* INET6 */ 23429 #ifdef INET 23430 ip = mtod(m, struct ip *); 23431 #endif 23432 th = (struct tcphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.th - rack->r_ctl.fsb.tcp_ip_hdr)); 23433 /* If we have a udp header lets set it into the mbuf as well */ 23434 if (udp) 23435 udp = (struct udphdr *)(cpto + ((uint8_t *)rack->r_ctl.fsb.udp - rack->r_ctl.fsb.tcp_ip_hdr)); 23436 } 23437 if (optlen) { 23438 bcopy(opt, th + 1, optlen); 23439 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 23440 } 23441 /* 23442 * Put TCP length in extended header, and then checksum extended 23443 * header and data. 23444 */ 23445 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 23446 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 23447 if (to.to_flags & TOF_SIGNATURE) { 23448 /* 23449 * Calculate MD5 signature and put it into the place 23450 * determined before. 23451 * NOTE: since TCP options buffer doesn't point into 23452 * mbuf's data, calculate offset and use it. 23453 */ 23454 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 23455 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 23456 /* 23457 * Do not send segment if the calculation of MD5 23458 * digest has failed. 23459 */ 23460 goto out; 23461 } 23462 } 23463 #endif 23464 #ifdef INET6 23465 if (isipv6) { 23466 /* 23467 * ip6_plen is not need to be filled now, and will be filled 23468 * in ip6_output. 23469 */ 23470 if (tp->t_port) { 23471 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 23472 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 23473 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 23474 th->th_sum = htons(0); 23475 UDPSTAT_INC(udps_opackets); 23476 } else { 23477 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 23478 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 23479 th->th_sum = in6_cksum_pseudo(ip6, 23480 sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 23481 0); 23482 } 23483 } 23484 #endif 23485 #if defined(INET6) && defined(INET) 23486 else 23487 #endif 23488 #ifdef INET 23489 { 23490 if (tp->t_port) { 23491 m->m_pkthdr.csum_flags = CSUM_UDP; 23492 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 23493 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 23494 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 23495 th->th_sum = htons(0); 23496 UDPSTAT_INC(udps_opackets); 23497 } else { 23498 m->m_pkthdr.csum_flags = CSUM_TCP; 23499 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 23500 th->th_sum = in_pseudo(ip->ip_src.s_addr, 23501 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 23502 IPPROTO_TCP + len + optlen)); 23503 } 23504 /* IP version must be set here for ipv4/ipv6 checking later */ 23505 KASSERT(ip->ip_v == IPVERSION, 23506 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 23507 } 23508 #endif 23509 /* 23510 * Enable TSO and specify the size of the segments. The TCP pseudo 23511 * header checksum is always provided. XXX: Fixme: This is currently 23512 * not the case for IPv6. 23513 */ 23514 if (tso) { 23515 /* 23516 * Here we must use t_maxseg and the optlen since 23517 * the optlen may include SACK's (or DSACK). 23518 */ 23519 KASSERT(len > tp->t_maxseg - optlen, 23520 ("%s: len <= tso_segsz", __func__)); 23521 m->m_pkthdr.csum_flags |= CSUM_TSO; 23522 m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 23523 } 23524 KASSERT(len + hdrlen == m_length(m, NULL), 23525 ("%s: mbuf chain different than expected: %d + %u != %u", 23526 __func__, len, hdrlen, m_length(m, NULL))); 23527 23528 #ifdef TCP_HHOOK 23529 /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 23530 hhook_run_tcp_est_out(tp, th, &to, len, tso); 23531 #endif 23532 if ((rack->r_ctl.crte != NULL) && 23533 (rack->rc_hw_nobuf == 0) && 23534 tcp_bblogging_on(tp)) { 23535 rack_log_queue_level(tp, rack, len, &tv, cts); 23536 } 23537 /* We're getting ready to send; log now. */ 23538 if (tcp_bblogging_on(rack->rc_tp)) { 23539 union tcp_log_stackspecific log; 23540 23541 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 23542 log.u_bbr.inhpts = tcp_in_hpts(rack->rc_tp); 23543 if (rack->rack_no_prr) 23544 log.u_bbr.flex1 = 0; 23545 else 23546 log.u_bbr.flex1 = rack->r_ctl.rc_prr_sndcnt; 23547 log.u_bbr.flex2 = rack->r_ctl.rc_pace_min_segs; 23548 log.u_bbr.flex3 = rack->r_ctl.rc_pace_max_segs; 23549 log.u_bbr.flex4 = orig_len; 23550 /* Save off the early/late values */ 23551 log.u_bbr.flex6 = rack->r_ctl.rc_agg_early; 23552 log.u_bbr.applimited = rack->r_ctl.rc_agg_delayed; 23553 log.u_bbr.bw_inuse = rack_get_bw(rack); 23554 log.u_bbr.cur_del_rate = rack->r_ctl.gp_bw; 23555 log.u_bbr.flex8 = 0; 23556 if (rsm) { 23557 if (rsm->r_flags & RACK_RWND_COLLAPSED) { 23558 rack_log_collapse(rack, rsm->r_start, rsm->r_end, 0, __LINE__, 5, rsm->r_flags, rsm); 23559 counter_u64_add(rack_collapsed_win_rxt, 1); 23560 counter_u64_add(rack_collapsed_win_rxt_bytes, (rsm->r_end - rsm->r_start)); 23561 } 23562 if (doing_tlp) 23563 log.u_bbr.flex8 = 2; 23564 else 23565 log.u_bbr.flex8 = 1; 23566 } else { 23567 if (doing_tlp) 23568 log.u_bbr.flex8 = 3; 23569 } 23570 log.u_bbr.pacing_gain = rack_get_output_gain(rack, rsm); 23571 log.u_bbr.flex7 = mark; 23572 log.u_bbr.flex7 <<= 8; 23573 log.u_bbr.flex7 |= pass; 23574 log.u_bbr.pkts_out = tp->t_maxseg; 23575 log.u_bbr.timeStamp = cts; 23576 log.u_bbr.inflight = ctf_flight_size(rack->rc_tp, rack->r_ctl.rc_sacked); 23577 if (rsm && (rsm->r_rtr_cnt > 0)) { 23578 /* 23579 * When we have a retransmit we want to log the 23580 * burst at send and flight at send from before. 23581 */ 23582 log.u_bbr.flex5 = rsm->r_fas; 23583 log.u_bbr.bbr_substate = rsm->r_bas; 23584 } else { 23585 /* 23586 * New transmits we log in flex5 the inflight again as 23587 * well as the number of segments in our send in the 23588 * substate field. 23589 */ 23590 log.u_bbr.flex5 = log.u_bbr.inflight; 23591 log.u_bbr.bbr_substate = (uint8_t)((len + segsiz - 1)/segsiz); 23592 } 23593 log.u_bbr.lt_epoch = cwnd_to_use; 23594 log.u_bbr.delivered = sendalot; 23595 log.u_bbr.rttProp = (uint64_t)rsm; 23596 log.u_bbr.pkt_epoch = __LINE__; 23597 if (rsm) { 23598 log.u_bbr.delRate = rsm->r_flags; 23599 log.u_bbr.delRate <<= 31; 23600 log.u_bbr.delRate |= rack->r_must_retran; 23601 log.u_bbr.delRate <<= 1; 23602 log.u_bbr.delRate |= (sack_rxmit & 0x00000001); 23603 } else { 23604 log.u_bbr.delRate = rack->r_must_retran; 23605 log.u_bbr.delRate <<= 1; 23606 log.u_bbr.delRate |= (sack_rxmit & 0x00000001); 23607 } 23608 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 23609 len, &log, false, NULL, __func__, __LINE__, &tv); 23610 } else 23611 lgb = NULL; 23612 23613 /* 23614 * Fill in IP length and desired time to live and send to IP level. 23615 * There should be a better way to handle ttl and tos; we could keep 23616 * them in the template, but need a way to checksum without them. 23617 */ 23618 /* 23619 * m->m_pkthdr.len should have been set before cksum calcuration, 23620 * because in6_cksum() need it. 23621 */ 23622 #ifdef INET6 23623 if (isipv6) { 23624 /* 23625 * we separately set hoplimit for every segment, since the 23626 * user might want to change the value via setsockopt. Also, 23627 * desired default hop limit might be changed via Neighbor 23628 * Discovery. 23629 */ 23630 rack->r_ctl.fsb.hoplimit = ip6->ip6_hlim = in6_selecthlim(inp, NULL); 23631 23632 /* 23633 * Set the packet size here for the benefit of DTrace 23634 * probes. ip6_output() will set it properly; it's supposed 23635 * to include the option header lengths as well. 23636 */ 23637 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 23638 23639 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 23640 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 23641 else 23642 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 23643 23644 if (tp->t_state == TCPS_SYN_SENT) 23645 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 23646 23647 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 23648 /* TODO: IPv6 IP6TOS_ECT bit on */ 23649 error = ip6_output(m, 23650 inp->in6p_outputopts, 23651 &inp->inp_route6, 23652 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 23653 NULL, NULL, inp); 23654 23655 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 23656 mtu = inp->inp_route6.ro_nh->nh_mtu; 23657 } 23658 #endif /* INET6 */ 23659 #if defined(INET) && defined(INET6) 23660 else 23661 #endif 23662 #ifdef INET 23663 { 23664 ip->ip_len = htons(m->m_pkthdr.len); 23665 #ifdef INET6 23666 if (inp->inp_vflag & INP_IPV6PROTO) 23667 ip->ip_ttl = in6_selecthlim(inp, NULL); 23668 #endif /* INET6 */ 23669 rack->r_ctl.fsb.hoplimit = ip->ip_ttl; 23670 /* 23671 * If we do path MTU discovery, then we set DF on every 23672 * packet. This might not be the best thing to do according 23673 * to RFC3390 Section 2. However the tcp hostcache migitates 23674 * the problem so it affects only the first tcp connection 23675 * with a host. 23676 * 23677 * NB: Don't set DF on small MTU/MSS to have a safe 23678 * fallback. 23679 */ 23680 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 23681 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 23682 if (tp->t_port == 0 || len < V_tcp_minmss) { 23683 ip->ip_off |= htons(IP_DF); 23684 } 23685 } else { 23686 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 23687 } 23688 23689 if (tp->t_state == TCPS_SYN_SENT) 23690 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 23691 23692 TCP_PROBE5(send, NULL, tp, ip, tp, th); 23693 23694 error = ip_output(m, 23695 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 23696 inp->inp_options, 23697 #else 23698 NULL, 23699 #endif 23700 &inp->inp_route, 23701 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 23702 inp); 23703 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 23704 mtu = inp->inp_route.ro_nh->nh_mtu; 23705 } 23706 #endif /* INET */ 23707 if (lgb) { 23708 lgb->tlb_errno = error; 23709 lgb = NULL; 23710 } 23711 23712 out: 23713 /* 23714 * In transmit state, time the transmission and arrange for the 23715 * retransmit. In persist state, just set snd_max. 23716 */ 23717 rack_log_output(tp, &to, len, rack_seq, (uint8_t) flags, error, 23718 rack_to_usec_ts(&tv), 23719 rsm, add_flag, s_mb, s_moff, hw_tls, segsiz); 23720 if (error == 0) { 23721 if (add_flag & RACK_IS_PCM) { 23722 /* We just launched a PCM */ 23723 /* rrs here log */ 23724 rack->pcm_in_progress = 1; 23725 rack->pcm_needed = 0; 23726 rack_log_pcm(rack, 7, len, rack->r_ctl.pcm_max_seg, add_flag); 23727 } 23728 if (rsm == NULL) { 23729 if (rack->lt_bw_up == 0) { 23730 rack->r_ctl.lt_timemark = tcp_tv_to_lusectick(&tv); 23731 rack->r_ctl.lt_seq = tp->snd_una; 23732 rack->lt_bw_up = 1; 23733 } else if (((rack_seq + len) - rack->r_ctl.lt_seq) > 0x7fffffff) { 23734 /* 23735 * Need to record what we have since we are 23736 * approaching seq wrap. 23737 */ 23738 uint64_t tmark; 23739 23740 rack->r_ctl.lt_bw_bytes += (tp->snd_una - rack->r_ctl.lt_seq); 23741 rack->r_ctl.lt_seq = tp->snd_una; 23742 tmark = tcp_get_u64_usecs(&tv); 23743 if (tmark > rack->r_ctl.lt_timemark) { 23744 rack->r_ctl.lt_bw_time += (tmark - rack->r_ctl.lt_timemark); 23745 rack->r_ctl.lt_timemark = tmark; 23746 } 23747 } 23748 } 23749 rack->forced_ack = 0; /* If we send something zap the FA flag */ 23750 counter_u64_add(rack_total_bytes, len); 23751 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 23752 if (rsm && doing_tlp) { 23753 rack->rc_last_sent_tlp_past_cumack = 0; 23754 rack->rc_last_sent_tlp_seq_valid = 1; 23755 rack->r_ctl.last_sent_tlp_seq = rsm->r_start; 23756 rack->r_ctl.last_sent_tlp_len = rsm->r_end - rsm->r_start; 23757 } 23758 if (rack->rc_hw_nobuf) { 23759 rack->rc_hw_nobuf = 0; 23760 rack->r_ctl.rc_agg_delayed = 0; 23761 rack->r_early = 0; 23762 rack->r_late = 0; 23763 rack->r_ctl.rc_agg_early = 0; 23764 } 23765 if (rsm && (doing_tlp == 0)) { 23766 /* Set we retransmitted */ 23767 rack->rc_gp_saw_rec = 1; 23768 } else { 23769 if (cwnd_to_use > tp->snd_ssthresh) { 23770 /* Set we sent in CA */ 23771 rack->rc_gp_saw_ca = 1; 23772 } else { 23773 /* Set we sent in SS */ 23774 rack->rc_gp_saw_ss = 1; 23775 } 23776 } 23777 if (TCPS_HAVEESTABLISHED(tp->t_state) && 23778 (tp->t_flags & TF_SACK_PERMIT) && 23779 tp->rcv_numsacks > 0) 23780 tcp_clean_dsack_blocks(tp); 23781 tot_len_this_send += len; 23782 if (len == 0) { 23783 counter_u64_add(rack_out_size[TCP_MSS_ACCT_SNDACK], 1); 23784 } else { 23785 int idx; 23786 23787 idx = (len / segsiz) + 3; 23788 if (idx >= TCP_MSS_ACCT_ATIMER) 23789 counter_u64_add(rack_out_size[(TCP_MSS_ACCT_ATIMER-1)], 1); 23790 else 23791 counter_u64_add(rack_out_size[idx], 1); 23792 } 23793 } 23794 if ((rack->rack_no_prr == 0) && 23795 sub_from_prr && 23796 (error == 0)) { 23797 if (rack->r_ctl.rc_prr_sndcnt >= len) 23798 rack->r_ctl.rc_prr_sndcnt -= len; 23799 else 23800 rack->r_ctl.rc_prr_sndcnt = 0; 23801 } 23802 sub_from_prr = 0; 23803 if (doing_tlp) { 23804 /* Make sure the TLP is added */ 23805 add_flag |= RACK_TLP; 23806 } else if (rsm) { 23807 /* If its a resend without TLP then it must not have the flag */ 23808 rsm->r_flags &= ~RACK_TLP; 23809 } 23810 23811 23812 if ((error == 0) && 23813 (len > 0) && 23814 (tp->snd_una == tp->snd_max)) 23815 rack->r_ctl.rc_tlp_rxt_last_time = cts; 23816 23817 { 23818 /* 23819 * This block is not associated with the above error == 0 test. 23820 * It is used to advance snd_max if we have a new transmit. 23821 */ 23822 tcp_seq startseq = tp->snd_max; 23823 23824 23825 if (rsm && (doing_tlp == 0)) 23826 rack->r_ctl.rc_loss_count += rsm->r_end - rsm->r_start; 23827 if (error) 23828 /* We don't log or do anything with errors */ 23829 goto nomore; 23830 if (doing_tlp == 0) { 23831 if (rsm == NULL) { 23832 /* 23833 * Not a retransmission of some 23834 * sort, new data is going out so 23835 * clear our TLP count and flag. 23836 */ 23837 rack->rc_tlp_in_progress = 0; 23838 rack->r_ctl.rc_tlp_cnt_out = 0; 23839 } 23840 } else { 23841 /* 23842 * We have just sent a TLP, mark that it is true 23843 * and make sure our in progress is set so we 23844 * continue to check the count. 23845 */ 23846 rack->rc_tlp_in_progress = 1; 23847 rack->r_ctl.rc_tlp_cnt_out++; 23848 } 23849 /* 23850 * If we are retransmitting we are done, snd_max 23851 * does not get updated. 23852 */ 23853 if (sack_rxmit) 23854 goto nomore; 23855 if ((tp->snd_una == tp->snd_max) && (len > 0)) { 23856 /* 23857 * Update the time we just added data since 23858 * nothing was outstanding. 23859 */ 23860 rack_log_progress_event(rack, tp, ticks, PROGRESS_START, __LINE__); 23861 tp->t_acktime = ticks; 23862 } 23863 /* 23864 * Now for special SYN/FIN handling. 23865 */ 23866 if (flags & (TH_SYN | TH_FIN)) { 23867 if ((flags & TH_SYN) && 23868 ((tp->t_flags & TF_SENTSYN) == 0)) { 23869 tp->snd_max++; 23870 tp->t_flags |= TF_SENTSYN; 23871 } 23872 if ((flags & TH_FIN) && 23873 ((tp->t_flags & TF_SENTFIN) == 0)) { 23874 tp->snd_max++; 23875 tp->t_flags |= TF_SENTFIN; 23876 } 23877 } 23878 tp->snd_max += len; 23879 if (rack->rc_new_rnd_needed) { 23880 rack_new_round_starts(tp, rack, tp->snd_max); 23881 } 23882 /* 23883 * Time this transmission if not a retransmission and 23884 * not currently timing anything. 23885 * This is only relevant in case of switching back to 23886 * the base stack. 23887 */ 23888 if (tp->t_rtttime == 0) { 23889 tp->t_rtttime = ticks; 23890 tp->t_rtseq = startseq; 23891 KMOD_TCPSTAT_INC(tcps_segstimed); 23892 } 23893 if (len && 23894 ((tp->t_flags & TF_GPUTINPROG) == 0)) 23895 rack_start_gp_measurement(tp, rack, startseq, sb_offset); 23896 /* 23897 * If we are doing FO we need to update the mbuf position and subtract 23898 * this happens when the peer sends us duplicate information and 23899 * we thus want to send a DSACK. 23900 * 23901 * XXXRRS: This brings to mind a ?, when we send a DSACK block is TSO 23902 * turned off? If not then we are going to echo multiple DSACK blocks 23903 * out (with the TSO), which we should not be doing. 23904 */ 23905 if (rack->r_fast_output && len) { 23906 if (rack->r_ctl.fsb.left_to_send > len) 23907 rack->r_ctl.fsb.left_to_send -= len; 23908 else 23909 rack->r_ctl.fsb.left_to_send = 0; 23910 if (rack->r_ctl.fsb.left_to_send < segsiz) 23911 rack->r_fast_output = 0; 23912 if (rack->r_fast_output) { 23913 rack->r_ctl.fsb.m = sbsndmbuf(sb, (tp->snd_max - tp->snd_una), &rack->r_ctl.fsb.off); 23914 rack->r_ctl.fsb.o_m_len = rack->r_ctl.fsb.m->m_len; 23915 rack->r_ctl.fsb.o_t_len = M_TRAILINGROOM(rack->r_ctl.fsb.m); 23916 } 23917 } 23918 if (rack_pcm_blast == 0) { 23919 if ((orig_len > len) && 23920 (add_flag & RACK_IS_PCM) && 23921 (len < pace_max_seg) && 23922 ((pace_max_seg - len) > segsiz)) { 23923 /* 23924 * We are doing a PCM measurement and we did 23925 * not get enough data in the TSO to meet the 23926 * burst requirement. 23927 */ 23928 uint32_t n_len; 23929 23930 n_len = (orig_len - len); 23931 orig_len -= len; 23932 pace_max_seg -= len; 23933 len = n_len; 23934 sb_offset = tp->snd_max - tp->snd_una; 23935 /* Re-lock for the next spin */ 23936 SOCKBUF_LOCK(sb); 23937 goto send; 23938 } 23939 } else { 23940 if ((orig_len > len) && 23941 (add_flag & RACK_IS_PCM) && 23942 ((orig_len - len) > segsiz)) { 23943 /* 23944 * We are doing a PCM measurement and we did 23945 * not get enough data in the TSO to meet the 23946 * burst requirement. 23947 */ 23948 uint32_t n_len; 23949 23950 n_len = (orig_len - len); 23951 orig_len -= len; 23952 len = n_len; 23953 sb_offset = tp->snd_max - tp->snd_una; 23954 /* Re-lock for the next spin */ 23955 SOCKBUF_LOCK(sb); 23956 goto send; 23957 } 23958 } 23959 } 23960 nomore: 23961 if (error) { 23962 rack->r_ctl.rc_agg_delayed = 0; 23963 rack->r_early = 0; 23964 rack->r_late = 0; 23965 rack->r_ctl.rc_agg_early = 0; 23966 SOCKBUF_UNLOCK_ASSERT(sb); /* Check gotos. */ 23967 /* 23968 * Failures do not advance the seq counter above. For the 23969 * case of ENOBUFS we will fall out and retry in 1ms with 23970 * the hpts. Everything else will just have to retransmit 23971 * with the timer. 23972 * 23973 * In any case, we do not want to loop around for another 23974 * send without a good reason. 23975 */ 23976 sendalot = 0; 23977 switch (error) { 23978 case EPERM: 23979 case EACCES: 23980 tp->t_softerror = error; 23981 #ifdef TCP_ACCOUNTING 23982 crtsc = get_cyclecount(); 23983 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 23984 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 23985 } 23986 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 23987 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 23988 } 23989 sched_unpin(); 23990 #endif 23991 return (error); 23992 case ENOBUFS: 23993 /* 23994 * Pace us right away to retry in a some 23995 * time 23996 */ 23997 if (rack->r_ctl.crte != NULL) { 23998 tcp_trace_point(rack->rc_tp, TCP_TP_HWENOBUF); 23999 if (tcp_bblogging_on(rack->rc_tp)) 24000 rack_log_queue_level(tp, rack, len, &tv, cts); 24001 } else 24002 tcp_trace_point(rack->rc_tp, TCP_TP_ENOBUF); 24003 slot = ((1 + rack->rc_enobuf) * HPTS_USEC_IN_MSEC); 24004 if (rack->rc_enobuf < 0x7f) 24005 rack->rc_enobuf++; 24006 if (slot < (10 * HPTS_USEC_IN_MSEC)) 24007 slot = 10 * HPTS_USEC_IN_MSEC; 24008 if (rack->r_ctl.crte != NULL) { 24009 counter_u64_add(rack_saw_enobuf_hw, 1); 24010 tcp_rl_log_enobuf(rack->r_ctl.crte); 24011 } 24012 counter_u64_add(rack_saw_enobuf, 1); 24013 goto enobufs; 24014 case EMSGSIZE: 24015 /* 24016 * For some reason the interface we used initially 24017 * to send segments changed to another or lowered 24018 * its MTU. If TSO was active we either got an 24019 * interface without TSO capabilits or TSO was 24020 * turned off. If we obtained mtu from ip_output() 24021 * then update it and try again. 24022 */ 24023 if (tso) 24024 tp->t_flags &= ~TF_TSO; 24025 if (mtu != 0) { 24026 int saved_mtu; 24027 24028 saved_mtu = tp->t_maxseg; 24029 tcp_mss_update(tp, -1, mtu, NULL, NULL); 24030 if (saved_mtu > tp->t_maxseg) { 24031 goto again; 24032 } 24033 } 24034 slot = 10 * HPTS_USEC_IN_MSEC; 24035 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0); 24036 #ifdef TCP_ACCOUNTING 24037 crtsc = get_cyclecount(); 24038 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24039 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 24040 } 24041 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24042 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 24043 } 24044 sched_unpin(); 24045 #endif 24046 return (error); 24047 case ENETUNREACH: 24048 counter_u64_add(rack_saw_enetunreach, 1); 24049 case EHOSTDOWN: 24050 case EHOSTUNREACH: 24051 case ENETDOWN: 24052 if (TCPS_HAVERCVDSYN(tp->t_state)) { 24053 tp->t_softerror = error; 24054 } 24055 /* FALLTHROUGH */ 24056 default: 24057 slot = 10 * HPTS_USEC_IN_MSEC; 24058 rack_start_hpts_timer(rack, tp, cts, slot, 0, 0); 24059 #ifdef TCP_ACCOUNTING 24060 crtsc = get_cyclecount(); 24061 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24062 tp->tcp_cnt_counters[SND_OUT_FAIL]++; 24063 } 24064 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24065 tp->tcp_proc_time[SND_OUT_FAIL] += (crtsc - ts_val); 24066 } 24067 sched_unpin(); 24068 #endif 24069 return (error); 24070 } 24071 } else { 24072 rack->rc_enobuf = 0; 24073 if (IN_FASTRECOVERY(tp->t_flags) && rsm) 24074 rack->r_ctl.retran_during_recovery += len; 24075 } 24076 KMOD_TCPSTAT_INC(tcps_sndtotal); 24077 24078 /* 24079 * Data sent (as far as we can tell). If this advertises a larger 24080 * window than any other segment, then remember the size of the 24081 * advertised window. Any pending ACK has now been sent. 24082 */ 24083 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 24084 tp->rcv_adv = tp->rcv_nxt + recwin; 24085 24086 tp->last_ack_sent = tp->rcv_nxt; 24087 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 24088 enobufs: 24089 if (sendalot) { 24090 /* Do we need to turn off sendalot? */ 24091 if (pace_max_seg && 24092 (tot_len_this_send >= pace_max_seg)) { 24093 /* We hit our max. */ 24094 sendalot = 0; 24095 } 24096 } 24097 if ((error == 0) && (flags & TH_FIN)) 24098 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 24099 if (flags & TH_RST) { 24100 /* 24101 * We don't send again after sending a RST. 24102 */ 24103 slot = 0; 24104 sendalot = 0; 24105 if (error == 0) 24106 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 24107 } else if ((slot == 0) && (sendalot == 0) && tot_len_this_send) { 24108 /* 24109 * Get our pacing rate, if an error 24110 * occurred in sending (ENOBUF) we would 24111 * hit the else if with slot preset. Other 24112 * errors return. 24113 */ 24114 slot = rack_get_pacing_delay(rack, tp, tot_len_this_send, rsm, segsiz, __LINE__); 24115 } 24116 /* We have sent clear the flag */ 24117 rack->r_ent_rec_ns = 0; 24118 if (rack->r_must_retran) { 24119 if (rsm) { 24120 rack->r_ctl.rc_out_at_rto -= (rsm->r_end - rsm->r_start); 24121 if (SEQ_GEQ(rsm->r_end, rack->r_ctl.rc_snd_max_at_rto)) { 24122 /* 24123 * We have retransmitted all. 24124 */ 24125 rack->r_must_retran = 0; 24126 rack->r_ctl.rc_out_at_rto = 0; 24127 } 24128 } else if (SEQ_GEQ(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) { 24129 /* 24130 * Sending new data will also kill 24131 * the loop. 24132 */ 24133 rack->r_must_retran = 0; 24134 rack->r_ctl.rc_out_at_rto = 0; 24135 } 24136 } 24137 rack->r_ctl.fsb.recwin = recwin; 24138 if ((tp->t_flags & (TF_WASCRECOVERY|TF_WASFRECOVERY)) && 24139 SEQ_GT(tp->snd_max, rack->r_ctl.rc_snd_max_at_rto)) { 24140 /* 24141 * We hit an RTO and now have past snd_max at the RTO 24142 * clear all the WAS flags. 24143 */ 24144 tp->t_flags &= ~(TF_WASCRECOVERY|TF_WASFRECOVERY); 24145 } 24146 if (slot) { 24147 /* set the rack tcb into the slot N */ 24148 if ((error == 0) && 24149 rack_use_rfo && 24150 ((flags & (TH_SYN|TH_FIN)) == 0) && 24151 (rsm == NULL) && 24152 (ipoptlen == 0) && 24153 (tp->rcv_numsacks == 0) && 24154 (rack->rc_policer_detected == 0) && 24155 rack->r_fsb_inited && 24156 TCPS_HAVEESTABLISHED(tp->t_state) && 24157 ((IN_RECOVERY(tp->t_flags)) == 0) && 24158 (rack->r_must_retran == 0) && 24159 ((tp->t_flags & TF_NEEDFIN) == 0) && 24160 (len > 0) && (orig_len > 0) && 24161 (orig_len > len) && 24162 ((orig_len - len) >= segsiz) && 24163 ((optlen == 0) || 24164 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 24165 /* We can send at least one more MSS using our fsb */ 24166 rack_setup_fast_output(tp, rack, sb, len, orig_len, 24167 segsiz, pace_max_seg, hw_tls, flags); 24168 } else 24169 rack->r_fast_output = 0; 24170 rack_log_fsb(rack, tp, so, flags, 24171 ipoptlen, orig_len, len, error, 24172 (rsm == NULL), optlen, __LINE__, 2); 24173 } else if (sendalot) { 24174 int ret; 24175 24176 sack_rxmit = 0; 24177 if ((error == 0) && 24178 rack_use_rfo && 24179 ((flags & (TH_SYN|TH_FIN)) == 0) && 24180 (rsm == NULL) && 24181 (ipoptlen == 0) && 24182 (tp->rcv_numsacks == 0) && 24183 (rack->r_must_retran == 0) && 24184 rack->r_fsb_inited && 24185 TCPS_HAVEESTABLISHED(tp->t_state) && 24186 ((IN_RECOVERY(tp->t_flags)) == 0) && 24187 ((tp->t_flags & TF_NEEDFIN) == 0) && 24188 (len > 0) && (orig_len > 0) && 24189 (orig_len > len) && 24190 ((orig_len - len) >= segsiz) && 24191 ((optlen == 0) || 24192 ((optlen == TCPOLEN_TSTAMP_APPA) && (to.to_flags & TOF_TS)))) { 24193 /* we can use fast_output for more */ 24194 rack_setup_fast_output(tp, rack, sb, len, orig_len, 24195 segsiz, pace_max_seg, hw_tls, flags); 24196 if (rack->r_fast_output) { 24197 error = 0; 24198 ret = rack_fast_output(tp, rack, ts_val, cts, ms_cts, &tv, tot_len_this_send, &error); 24199 if (ret >= 0) 24200 return (ret); 24201 else if (error) 24202 goto nomore; 24203 24204 } 24205 } 24206 goto again; 24207 } 24208 skip_all_send: 24209 /* Assure when we leave that snd_nxt will point to top */ 24210 if (SEQ_GT(tp->snd_max, tp->snd_nxt)) 24211 tp->snd_nxt = tp->snd_max; 24212 rack_start_hpts_timer(rack, tp, cts, slot, tot_len_this_send, 0); 24213 #ifdef TCP_ACCOUNTING 24214 crtsc = get_cyclecount() - ts_val; 24215 if (tot_len_this_send) { 24216 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24217 tp->tcp_cnt_counters[SND_OUT_DATA]++; 24218 } 24219 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24220 tp->tcp_proc_time[SND_OUT_DATA] += crtsc; 24221 } 24222 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24223 tp->tcp_cnt_counters[CNT_OF_MSS_OUT] += ((tot_len_this_send + segsiz - 1) /segsiz); 24224 } 24225 } else { 24226 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24227 tp->tcp_cnt_counters[SND_OUT_ACK]++; 24228 } 24229 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 24230 tp->tcp_proc_time[SND_OUT_ACK] += crtsc; 24231 } 24232 } 24233 sched_unpin(); 24234 #endif 24235 if (error == ENOBUFS) 24236 error = 0; 24237 return (error); 24238 } 24239 24240 static void 24241 rack_update_seg(struct tcp_rack *rack) 24242 { 24243 uint32_t orig_val; 24244 24245 orig_val = rack->r_ctl.rc_pace_max_segs; 24246 rack_set_pace_segments(rack->rc_tp, rack, __LINE__, NULL); 24247 if (orig_val != rack->r_ctl.rc_pace_max_segs) 24248 rack_log_pacing_delay_calc(rack, 0, 0, orig_val, 0, 0, 15, __LINE__, NULL, 0); 24249 } 24250 24251 static void 24252 rack_mtu_change(struct tcpcb *tp) 24253 { 24254 /* 24255 * The MSS may have changed 24256 */ 24257 struct tcp_rack *rack; 24258 struct rack_sendmap *rsm; 24259 24260 rack = (struct tcp_rack *)tp->t_fb_ptr; 24261 if (rack->r_ctl.rc_pace_min_segs != ctf_fixed_maxseg(tp)) { 24262 /* 24263 * The MTU has changed we need to resend everything 24264 * since all we have sent is lost. We first fix 24265 * up the mtu though. 24266 */ 24267 rack_set_pace_segments(tp, rack, __LINE__, NULL); 24268 /* We treat this like a full retransmit timeout without the cwnd adjustment */ 24269 rack_remxt_tmr(tp); 24270 rack->r_fast_output = 0; 24271 rack->r_ctl.rc_out_at_rto = ctf_flight_size(tp, 24272 rack->r_ctl.rc_sacked); 24273 rack->r_ctl.rc_snd_max_at_rto = tp->snd_max; 24274 rack->r_must_retran = 1; 24275 /* Mark all inflight to needing to be rxt'd */ 24276 TAILQ_FOREACH(rsm, &rack->r_ctl.rc_tmap, r_tnext) { 24277 rsm->r_flags |= (RACK_MUST_RXT|RACK_PMTU_CHG); 24278 } 24279 } 24280 sack_filter_clear(&rack->r_ctl.rack_sf, tp->snd_una); 24281 /* We don't use snd_nxt to retransmit */ 24282 tp->snd_nxt = tp->snd_max; 24283 } 24284 24285 static int 24286 rack_set_dgp(struct tcp_rack *rack) 24287 { 24288 if (rack->dgp_on == 1) 24289 return(0); 24290 if ((rack->use_fixed_rate == 1) && 24291 (rack->rc_always_pace == 1)) { 24292 /* 24293 * We are already pacing another 24294 * way. 24295 */ 24296 return (EBUSY); 24297 } 24298 if (rack->rc_always_pace == 1) { 24299 rack_remove_pacing(rack); 24300 } 24301 if (tcp_incr_dgp_pacing_cnt() == 0) 24302 return (ENOSPC); 24303 rack->r_ctl.pacing_method |= RACK_DGP_PACING; 24304 rack->rc_fillcw_apply_discount = 0; 24305 rack->dgp_on = 1; 24306 rack->rc_always_pace = 1; 24307 rack->rc_pace_dnd = 1; 24308 rack->use_fixed_rate = 0; 24309 if (rack->gp_ready) 24310 rack_set_cc_pacing(rack); 24311 rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 24312 rack->rack_attempt_hdwr_pace = 0; 24313 /* rxt settings */ 24314 rack->full_size_rxt = 1; 24315 rack->shape_rxt_to_pacing_min = 0; 24316 /* cmpack=1 */ 24317 rack->r_use_cmp_ack = 1; 24318 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state) && 24319 rack->r_use_cmp_ack) 24320 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP; 24321 /* scwnd=1 */ 24322 rack->rack_enable_scwnd = 1; 24323 /* dynamic=100 */ 24324 rack->rc_gp_dyn_mul = 1; 24325 /* gp_inc_ca */ 24326 rack->r_ctl.rack_per_of_gp_ca = 100; 24327 /* rrr_conf=3 */ 24328 rack->r_rr_config = 3; 24329 /* npush=2 */ 24330 rack->r_ctl.rc_no_push_at_mrtt = 2; 24331 /* fillcw=1 */ 24332 rack->rc_pace_to_cwnd = 1; 24333 rack->rc_pace_fill_if_rttin_range = 0; 24334 rack->rtt_limit_mul = 0; 24335 /* noprr=1 */ 24336 rack->rack_no_prr = 1; 24337 /* lscwnd=1 */ 24338 rack->r_limit_scw = 1; 24339 /* gp_inc_rec */ 24340 rack->r_ctl.rack_per_of_gp_rec = 90; 24341 return (0); 24342 } 24343 24344 static int 24345 rack_set_profile(struct tcp_rack *rack, int prof) 24346 { 24347 int err = EINVAL; 24348 if (prof == 1) { 24349 /* 24350 * Profile 1 is "standard" DGP. It ignores 24351 * client buffer level. 24352 */ 24353 err = rack_set_dgp(rack); 24354 if (err) 24355 return (err); 24356 } else if (prof == 6) { 24357 err = rack_set_dgp(rack); 24358 if (err) 24359 return (err); 24360 /* 24361 * Profile 6 tweaks DGP so that it will apply to 24362 * fill-cw the same settings that profile5 does 24363 * to replace DGP. It gets then the max(dgp-rate, fillcw(discounted). 24364 */ 24365 rack->rc_fillcw_apply_discount = 1; 24366 } else if (prof == 0) { 24367 /* This changes things back to the default settings */ 24368 if (rack->rc_always_pace == 1) { 24369 rack_remove_pacing(rack); 24370 } else { 24371 /* Make sure any stray flags are off */ 24372 rack->dgp_on = 0; 24373 rack->rc_hybrid_mode = 0; 24374 rack->use_fixed_rate = 0; 24375 } 24376 err = 0; 24377 if (rack_fill_cw_state) 24378 rack->rc_pace_to_cwnd = 1; 24379 else 24380 rack->rc_pace_to_cwnd = 0; 24381 24382 if (rack_pace_every_seg && tcp_can_enable_pacing()) { 24383 rack->r_ctl.pacing_method |= RACK_REG_PACING; 24384 rack->rc_always_pace = 1; 24385 if (rack->rack_hibeta) 24386 rack_set_cc_pacing(rack); 24387 } else 24388 rack->rc_always_pace = 0; 24389 if (rack_dsack_std_based & 0x1) { 24390 /* Basically this means all rack timers are at least (srtt + 1/4 srtt) */ 24391 rack->rc_rack_tmr_std_based = 1; 24392 } 24393 if (rack_dsack_std_based & 0x2) { 24394 /* Basically this means rack timers are extended based on dsack by up to (2 * srtt) */ 24395 rack->rc_rack_use_dsack = 1; 24396 } 24397 if (rack_use_cmp_acks) 24398 rack->r_use_cmp_ack = 1; 24399 else 24400 rack->r_use_cmp_ack = 0; 24401 if (rack_disable_prr) 24402 rack->rack_no_prr = 1; 24403 else 24404 rack->rack_no_prr = 0; 24405 if (rack_gp_no_rec_chg) 24406 rack->rc_gp_no_rec_chg = 1; 24407 else 24408 rack->rc_gp_no_rec_chg = 0; 24409 if (rack_enable_mqueue_for_nonpaced || rack->r_use_cmp_ack) { 24410 rack->r_mbuf_queue = 1; 24411 if (TCPS_HAVEESTABLISHED(rack->rc_tp->t_state)) 24412 rack->rc_tp->t_flags2 |= TF2_MBUF_ACKCMP; 24413 rack->rc_tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 24414 } else { 24415 rack->r_mbuf_queue = 0; 24416 rack->rc_tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 24417 } 24418 if (rack_enable_shared_cwnd) 24419 rack->rack_enable_scwnd = 1; 24420 else 24421 rack->rack_enable_scwnd = 0; 24422 if (rack_do_dyn_mul) { 24423 /* When dynamic adjustment is on CA needs to start at 100% */ 24424 rack->rc_gp_dyn_mul = 1; 24425 if (rack_do_dyn_mul >= 100) 24426 rack->r_ctl.rack_per_of_gp_ca = rack_do_dyn_mul; 24427 } else { 24428 rack->r_ctl.rack_per_of_gp_ca = rack_per_of_gp_ca; 24429 rack->rc_gp_dyn_mul = 0; 24430 } 24431 rack->r_rr_config = 0; 24432 rack->r_ctl.rc_no_push_at_mrtt = 0; 24433 rack->rc_pace_fill_if_rttin_range = 0; 24434 rack->rtt_limit_mul = 0; 24435 24436 if (rack_enable_hw_pacing) 24437 rack->rack_hdw_pace_ena = 1; 24438 else 24439 rack->rack_hdw_pace_ena = 0; 24440 if (rack_disable_prr) 24441 rack->rack_no_prr = 1; 24442 else 24443 rack->rack_no_prr = 0; 24444 if (rack_limits_scwnd) 24445 rack->r_limit_scw = 1; 24446 else 24447 rack->r_limit_scw = 0; 24448 rack_init_retransmit_value(rack, rack_rxt_controls); 24449 err = 0; 24450 } 24451 return (err); 24452 } 24453 24454 static int 24455 rack_add_deferred_option(struct tcp_rack *rack, int sopt_name, uint64_t loptval) 24456 { 24457 struct deferred_opt_list *dol; 24458 24459 dol = malloc(sizeof(struct deferred_opt_list), 24460 M_TCPDO, M_NOWAIT|M_ZERO); 24461 if (dol == NULL) { 24462 /* 24463 * No space yikes -- fail out.. 24464 */ 24465 return (0); 24466 } 24467 dol->optname = sopt_name; 24468 dol->optval = loptval; 24469 TAILQ_INSERT_TAIL(&rack->r_ctl.opt_list, dol, next); 24470 return (1); 24471 } 24472 24473 static int 24474 process_hybrid_pacing(struct tcp_rack *rack, struct tcp_hybrid_req *hybrid) 24475 { 24476 #ifdef TCP_REQUEST_TRK 24477 struct tcp_sendfile_track *sft; 24478 struct timeval tv; 24479 tcp_seq seq; 24480 int err; 24481 24482 microuptime(&tv); 24483 24484 /* Make sure no fixed rate is on */ 24485 rack->use_fixed_rate = 0; 24486 rack->r_ctl.rc_fixed_pacing_rate_rec = 0; 24487 rack->r_ctl.rc_fixed_pacing_rate_ca = 0; 24488 rack->r_ctl.rc_fixed_pacing_rate_ss = 0; 24489 /* Now allocate or find our entry that will have these settings */ 24490 sft = tcp_req_alloc_req_full(rack->rc_tp, &hybrid->req, tcp_tv_to_lusectick(&tv), 0); 24491 if (sft == NULL) { 24492 rack->rc_tp->tcp_hybrid_error++; 24493 /* no space, where would it have gone? */ 24494 seq = rack->rc_tp->snd_una + rack->rc_tp->t_inpcb.inp_socket->so_snd.sb_ccc; 24495 rack_log_hybrid(rack, seq, NULL, HYBRID_LOG_NO_ROOM, __LINE__, 0); 24496 return (ENOSPC); 24497 } 24498 /* mask our internal flags */ 24499 hybrid->hybrid_flags &= TCP_HYBRID_PACING_USER_MASK; 24500 /* The seq will be snd_una + everything in the buffer */ 24501 seq = sft->start_seq; 24502 if ((hybrid->hybrid_flags & TCP_HYBRID_PACING_ENABLE) == 0) { 24503 /* Disabling hybrid pacing */ 24504 if (rack->rc_hybrid_mode) { 24505 rack_set_profile(rack, 0); 24506 rack->rc_tp->tcp_hybrid_stop++; 24507 } 24508 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_TURNED_OFF, __LINE__, 0); 24509 return (0); 24510 } 24511 if (rack->dgp_on == 0) { 24512 /* 24513 * If we have not yet turned DGP on, do so 24514 * now setting pure DGP mode, no buffer level 24515 * response. 24516 */ 24517 if ((err = rack_set_profile(rack, 1)) != 0){ 24518 /* Failed to turn pacing on */ 24519 rack->rc_tp->tcp_hybrid_error++; 24520 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_NO_PACING, __LINE__, 0); 24521 return (err); 24522 } 24523 } 24524 /* 24525 * Now we must switch to hybrid mode as well which also 24526 * means moving to regular pacing. 24527 */ 24528 if (rack->rc_hybrid_mode == 0) { 24529 /* First time */ 24530 if (tcp_can_enable_pacing()) { 24531 rack->r_ctl.pacing_method |= RACK_REG_PACING; 24532 rack->rc_hybrid_mode = 1; 24533 } else { 24534 return (ENOSPC); 24535 } 24536 if (rack->r_ctl.pacing_method & RACK_DGP_PACING) { 24537 /* 24538 * This should be true. 24539 */ 24540 tcp_dec_dgp_pacing_cnt(); 24541 rack->r_ctl.pacing_method &= ~RACK_DGP_PACING; 24542 } 24543 } 24544 /* Now set in our flags */ 24545 sft->hybrid_flags = hybrid->hybrid_flags | TCP_HYBRID_PACING_WASSET; 24546 if (hybrid->hybrid_flags & TCP_HYBRID_PACING_CSPR) 24547 sft->cspr = hybrid->cspr; 24548 else 24549 sft->cspr = 0; 24550 if (hybrid->hybrid_flags & TCP_HYBRID_PACING_H_MS) 24551 sft->hint_maxseg = hybrid->hint_maxseg; 24552 else 24553 sft->hint_maxseg = 0; 24554 rack->rc_tp->tcp_hybrid_start++; 24555 rack_log_hybrid(rack, seq, sft, HYBRID_LOG_RULES_SET, __LINE__,0); 24556 return (0); 24557 #else 24558 return (ENOTSUP); 24559 #endif 24560 } 24561 24562 static int 24563 rack_stack_information(struct tcpcb *tp, struct stack_specific_info *si) 24564 { 24565 /* 24566 * Gather rack specific information. 24567 */ 24568 struct tcp_rack *rack; 24569 24570 rack = (struct tcp_rack *)tp->t_fb_ptr; 24571 /* We pulled a SSI info log out what was there */ 24572 policer_detection_log(rack, rack->rc_highly_buffered, 0, 0, 0, 20); 24573 if (rack->policer_detect_on) { 24574 si->policer_detection_enabled = 1; 24575 if (rack->rc_policer_detected) { 24576 si->policer_detected = 1; 24577 si->policer_bucket_size = rack->r_ctl.policer_bucket_size; 24578 si->policer_last_bw = rack->r_ctl.policer_bw; 24579 } else { 24580 si->policer_detected = 0; 24581 si->policer_bucket_size = 0; 24582 si->policer_last_bw = 0; 24583 } 24584 si->current_round = rack->r_ctl.current_round; 24585 si->highly_buffered = rack->rc_highly_buffered; 24586 } 24587 si->bytes_transmitted = tp->t_sndbytes; 24588 si->bytes_retransmitted = tp->t_snd_rxt_bytes; 24589 return (0); 24590 } 24591 24592 static int 24593 rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name, 24594 uint32_t optval, uint64_t loptval, struct tcp_hybrid_req *hybrid) 24595 24596 { 24597 struct epoch_tracker et; 24598 struct sockopt sopt; 24599 struct cc_newreno_opts opt; 24600 uint64_t val; 24601 int error = 0; 24602 uint16_t ca, ss; 24603 24604 switch (sopt_name) { 24605 case TCP_RACK_SET_RXT_OPTIONS: 24606 if ((optval >= 0) && (optval <= 2)) { 24607 rack_init_retransmit_value(rack, optval); 24608 } else { 24609 /* 24610 * You must send in 0, 1 or 2 all else is 24611 * invalid. 24612 */ 24613 error = EINVAL; 24614 } 24615 break; 24616 case TCP_RACK_DSACK_OPT: 24617 RACK_OPTS_INC(tcp_rack_dsack_opt); 24618 if (optval & 0x1) { 24619 rack->rc_rack_tmr_std_based = 1; 24620 } else { 24621 rack->rc_rack_tmr_std_based = 0; 24622 } 24623 if (optval & 0x2) { 24624 rack->rc_rack_use_dsack = 1; 24625 } else { 24626 rack->rc_rack_use_dsack = 0; 24627 } 24628 rack_log_dsack_event(rack, 5, __LINE__, 0, 0); 24629 break; 24630 case TCP_RACK_PACING_DIVISOR: 24631 RACK_OPTS_INC(tcp_rack_pacing_divisor); 24632 if (optval == 0) { 24633 rack->r_ctl.pace_len_divisor = rack_default_pacing_divisor; 24634 } else { 24635 if (optval < RL_MIN_DIVISOR) 24636 rack->r_ctl.pace_len_divisor = RL_MIN_DIVISOR; 24637 else 24638 rack->r_ctl.pace_len_divisor = optval; 24639 } 24640 break; 24641 case TCP_RACK_HI_BETA: 24642 RACK_OPTS_INC(tcp_rack_hi_beta); 24643 if (optval > 0) { 24644 rack->rack_hibeta = 1; 24645 if ((optval >= 50) && 24646 (optval <= 100)) { 24647 /* 24648 * User wants to set a custom beta. 24649 */ 24650 rack->r_ctl.saved_hibeta = optval; 24651 if (rack->rc_pacing_cc_set) 24652 rack_undo_cc_pacing(rack); 24653 rack->r_ctl.rc_saved_beta.beta = optval; 24654 } 24655 if (rack->rc_pacing_cc_set == 0) 24656 rack_set_cc_pacing(rack); 24657 } else { 24658 rack->rack_hibeta = 0; 24659 if (rack->rc_pacing_cc_set) 24660 rack_undo_cc_pacing(rack); 24661 } 24662 break; 24663 case TCP_RACK_PACING_BETA: 24664 error = EINVAL; 24665 break; 24666 case TCP_RACK_TIMER_SLOP: 24667 RACK_OPTS_INC(tcp_rack_timer_slop); 24668 rack->r_ctl.timer_slop = optval; 24669 if (rack->rc_tp->t_srtt) { 24670 /* 24671 * If we have an SRTT lets update t_rxtcur 24672 * to have the new slop. 24673 */ 24674 RACK_TCPT_RANGESET(tp->t_rxtcur, RACK_REXMTVAL(tp), 24675 rack_rto_min, rack_rto_max, 24676 rack->r_ctl.timer_slop); 24677 } 24678 break; 24679 case TCP_RACK_PACING_BETA_ECN: 24680 RACK_OPTS_INC(tcp_rack_beta_ecn); 24681 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) { 24682 /* This only works for newreno. */ 24683 error = EINVAL; 24684 break; 24685 } 24686 if (rack->rc_pacing_cc_set) { 24687 /* 24688 * Set them into the real CC module 24689 * whats in the rack pcb is the old values 24690 * to be used on restoral/ 24691 */ 24692 sopt.sopt_dir = SOPT_SET; 24693 opt.name = CC_NEWRENO_BETA_ECN; 24694 opt.val = optval; 24695 if (CC_ALGO(tp)->ctl_output != NULL) 24696 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, &sopt, &opt); 24697 else 24698 error = ENOENT; 24699 } else { 24700 /* 24701 * Not pacing yet so set it into our local 24702 * rack pcb storage. 24703 */ 24704 rack->r_ctl.rc_saved_beta.beta_ecn = optval; 24705 rack->r_ctl.rc_saved_beta.newreno_flags = CC_NEWRENO_BETA_ECN_ENABLED; 24706 } 24707 break; 24708 case TCP_DEFER_OPTIONS: 24709 RACK_OPTS_INC(tcp_defer_opt); 24710 if (optval) { 24711 if (rack->gp_ready) { 24712 /* Too late */ 24713 error = EINVAL; 24714 break; 24715 } 24716 rack->defer_options = 1; 24717 } else 24718 rack->defer_options = 0; 24719 break; 24720 case TCP_RACK_MEASURE_CNT: 24721 RACK_OPTS_INC(tcp_rack_measure_cnt); 24722 if (optval && (optval <= 0xff)) { 24723 rack->r_ctl.req_measurements = optval; 24724 } else 24725 error = EINVAL; 24726 break; 24727 case TCP_REC_ABC_VAL: 24728 RACK_OPTS_INC(tcp_rec_abc_val); 24729 if (optval > 0) 24730 rack->r_use_labc_for_rec = 1; 24731 else 24732 rack->r_use_labc_for_rec = 0; 24733 break; 24734 case TCP_RACK_ABC_VAL: 24735 RACK_OPTS_INC(tcp_rack_abc_val); 24736 if ((optval > 0) && (optval < 255)) 24737 rack->rc_labc = optval; 24738 else 24739 error = EINVAL; 24740 break; 24741 case TCP_HDWR_UP_ONLY: 24742 RACK_OPTS_INC(tcp_pacing_up_only); 24743 if (optval) 24744 rack->r_up_only = 1; 24745 else 24746 rack->r_up_only = 0; 24747 break; 24748 case TCP_FILLCW_RATE_CAP: /* URL:fillcw_cap */ 24749 RACK_OPTS_INC(tcp_fillcw_rate_cap); 24750 rack->r_ctl.fillcw_cap = loptval; 24751 break; 24752 case TCP_PACING_RATE_CAP: 24753 RACK_OPTS_INC(tcp_pacing_rate_cap); 24754 if ((rack->dgp_on == 1) && 24755 (rack->r_ctl.pacing_method & RACK_DGP_PACING)) { 24756 /* 24757 * If we are doing DGP we need to switch 24758 * to using the pacing limit. 24759 */ 24760 if (tcp_can_enable_pacing() == 0) { 24761 error = ENOSPC; 24762 break; 24763 } 24764 /* 24765 * Now change up the flags and counts to be correct. 24766 */ 24767 rack->r_ctl.pacing_method |= RACK_REG_PACING; 24768 tcp_dec_dgp_pacing_cnt(); 24769 rack->r_ctl.pacing_method &= ~RACK_DGP_PACING; 24770 } 24771 rack->r_ctl.bw_rate_cap = loptval; 24772 break; 24773 case TCP_HYBRID_PACING: 24774 if (hybrid == NULL) { 24775 error = EINVAL; 24776 break; 24777 } 24778 if (rack->r_ctl.side_chan_dis_mask & HYBRID_DIS_MASK) { 24779 error = EPERM; 24780 break; 24781 } 24782 error = process_hybrid_pacing(rack, hybrid); 24783 break; 24784 case TCP_SIDECHAN_DIS: /* URL:scodm */ 24785 if (optval) 24786 rack->r_ctl.side_chan_dis_mask = optval; 24787 else 24788 rack->r_ctl.side_chan_dis_mask = 0; 24789 break; 24790 case TCP_RACK_PROFILE: 24791 RACK_OPTS_INC(tcp_profile); 24792 error = rack_set_profile(rack, optval); 24793 break; 24794 case TCP_USE_CMP_ACKS: 24795 RACK_OPTS_INC(tcp_use_cmp_acks); 24796 if ((optval == 0) && (tp->t_flags2 & TF2_MBUF_ACKCMP)) { 24797 /* You can't turn it off once its on! */ 24798 error = EINVAL; 24799 } else if ((optval == 1) && (rack->r_use_cmp_ack == 0)) { 24800 rack->r_use_cmp_ack = 1; 24801 rack->r_mbuf_queue = 1; 24802 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 24803 } 24804 if (rack->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) 24805 tp->t_flags2 |= TF2_MBUF_ACKCMP; 24806 break; 24807 case TCP_SHARED_CWND_TIME_LIMIT: 24808 RACK_OPTS_INC(tcp_lscwnd); 24809 if (optval) 24810 rack->r_limit_scw = 1; 24811 else 24812 rack->r_limit_scw = 0; 24813 break; 24814 case TCP_RACK_DGP_IN_REC: 24815 error = EINVAL; 24816 break; 24817 case TCP_POLICER_DETECT: /* URL:pol_det */ 24818 RACK_OPTS_INC(tcp_pol_detect); 24819 rack_translate_policer_detect(rack, optval); 24820 break; 24821 case TCP_POLICER_MSS: 24822 RACK_OPTS_INC(tcp_pol_mss); 24823 rack->r_ctl.policer_del_mss = (uint8_t)optval; 24824 if (optval & 0x00000100) { 24825 /* 24826 * Value is setup like so: 24827 * VVVV VVVV VVVV VVVV VVVV VVAI MMMM MMMM 24828 * Where MMMM MMMM is MSS setting 24829 * I (9th bit) is the Postive value that 24830 * says it is being set (if its 0 then the 24831 * upper bits 11 - 32 have no meaning. 24832 * This allows setting it off with 24833 * 0x000001MM. 24834 * 24835 * The 10th bit is used to turn on the 24836 * alternate median (not the expanded one). 24837 * 24838 */ 24839 rack->r_ctl.pol_bw_comp = (optval >> 10); 24840 } 24841 if (optval & 0x00000200) { 24842 rack->r_ctl.policer_alt_median = 1; 24843 } else { 24844 rack->r_ctl.policer_alt_median = 0; 24845 } 24846 break; 24847 case TCP_RACK_PACE_TO_FILL: 24848 RACK_OPTS_INC(tcp_fillcw); 24849 if (optval == 0) 24850 rack->rc_pace_to_cwnd = 0; 24851 else { 24852 rack->rc_pace_to_cwnd = 1; 24853 } 24854 if ((optval >= rack_gp_rtt_maxmul) && 24855 rack_gp_rtt_maxmul && 24856 (optval < 0xf)) { 24857 rack->rc_pace_fill_if_rttin_range = 1; 24858 rack->rtt_limit_mul = optval; 24859 } else { 24860 rack->rc_pace_fill_if_rttin_range = 0; 24861 rack->rtt_limit_mul = 0; 24862 } 24863 break; 24864 case TCP_RACK_NO_PUSH_AT_MAX: 24865 RACK_OPTS_INC(tcp_npush); 24866 if (optval == 0) 24867 rack->r_ctl.rc_no_push_at_mrtt = 0; 24868 else if (optval < 0xff) 24869 rack->r_ctl.rc_no_push_at_mrtt = optval; 24870 else 24871 error = EINVAL; 24872 break; 24873 case TCP_SHARED_CWND_ENABLE: 24874 RACK_OPTS_INC(tcp_rack_scwnd); 24875 if (optval == 0) 24876 rack->rack_enable_scwnd = 0; 24877 else 24878 rack->rack_enable_scwnd = 1; 24879 break; 24880 case TCP_RACK_MBUF_QUEUE: 24881 /* Now do we use the LRO mbuf-queue feature */ 24882 RACK_OPTS_INC(tcp_rack_mbufq); 24883 if (optval || rack->r_use_cmp_ack) 24884 rack->r_mbuf_queue = 1; 24885 else 24886 rack->r_mbuf_queue = 0; 24887 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 24888 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 24889 else 24890 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 24891 break; 24892 case TCP_RACK_NONRXT_CFG_RATE: 24893 RACK_OPTS_INC(tcp_rack_cfg_rate); 24894 if (optval == 0) 24895 rack->rack_rec_nonrxt_use_cr = 0; 24896 else 24897 rack->rack_rec_nonrxt_use_cr = 1; 24898 break; 24899 case TCP_NO_PRR: 24900 RACK_OPTS_INC(tcp_rack_noprr); 24901 if (optval == 0) 24902 rack->rack_no_prr = 0; 24903 else if (optval == 1) 24904 rack->rack_no_prr = 1; 24905 else if (optval == 2) 24906 rack->no_prr_addback = 1; 24907 else 24908 error = EINVAL; 24909 break; 24910 case RACK_CSPR_IS_FCC: /* URL:csprisfcc */ 24911 if (optval > 0) 24912 rack->cspr_is_fcc = 1; 24913 else 24914 rack->cspr_is_fcc = 0; 24915 break; 24916 case TCP_TIMELY_DYN_ADJ: 24917 RACK_OPTS_INC(tcp_timely_dyn); 24918 if (optval == 0) 24919 rack->rc_gp_dyn_mul = 0; 24920 else { 24921 rack->rc_gp_dyn_mul = 1; 24922 if (optval >= 100) { 24923 /* 24924 * If the user sets something 100 or more 24925 * its the gp_ca value. 24926 */ 24927 rack->r_ctl.rack_per_of_gp_ca = optval; 24928 } 24929 } 24930 break; 24931 case TCP_RACK_DO_DETECTION: 24932 RACK_OPTS_INC(tcp_rack_do_detection); 24933 if (optval == 0) 24934 rack->do_detection = 0; 24935 else 24936 rack->do_detection = 1; 24937 break; 24938 case TCP_RACK_TLP_USE: 24939 if ((optval < TLP_USE_ID) || (optval > TLP_USE_TWO_TWO)) { 24940 error = EINVAL; 24941 break; 24942 } 24943 RACK_OPTS_INC(tcp_tlp_use); 24944 rack->rack_tlp_threshold_use = optval; 24945 break; 24946 case TCP_RACK_TLP_REDUCE: 24947 /* RACK TLP cwnd reduction (bool) */ 24948 RACK_OPTS_INC(tcp_rack_tlp_reduce); 24949 rack->r_ctl.rc_tlp_cwnd_reduce = optval; 24950 break; 24951 /* Pacing related ones */ 24952 case TCP_RACK_PACE_ALWAYS: 24953 /* 24954 * zero is old rack method, 1 is new 24955 * method using a pacing rate. 24956 */ 24957 RACK_OPTS_INC(tcp_rack_pace_always); 24958 if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) { 24959 error = EPERM; 24960 break; 24961 } 24962 if (optval > 0) { 24963 if (rack->rc_always_pace) { 24964 error = EALREADY; 24965 break; 24966 } else if (tcp_can_enable_pacing()) { 24967 rack->r_ctl.pacing_method |= RACK_REG_PACING; 24968 rack->rc_always_pace = 1; 24969 if (rack->rack_hibeta) 24970 rack_set_cc_pacing(rack); 24971 } 24972 else { 24973 error = ENOSPC; 24974 break; 24975 } 24976 } else { 24977 if (rack->rc_always_pace == 1) { 24978 rack_remove_pacing(rack); 24979 } 24980 } 24981 if (rack->r_mbuf_queue || rack->rc_always_pace || rack->r_use_cmp_ack) 24982 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 24983 else 24984 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 24985 /* A rate may be set irate or other, if so set seg size */ 24986 rack_update_seg(rack); 24987 break; 24988 case TCP_BBR_RACK_INIT_RATE: 24989 RACK_OPTS_INC(tcp_initial_rate); 24990 val = optval; 24991 /* Change from kbits per second to bytes per second */ 24992 val *= 1000; 24993 val /= 8; 24994 rack->r_ctl.init_rate = val; 24995 if (rack->rc_always_pace) 24996 rack_update_seg(rack); 24997 break; 24998 case TCP_BBR_IWINTSO: 24999 error = EINVAL; 25000 break; 25001 case TCP_RACK_FORCE_MSEG: 25002 RACK_OPTS_INC(tcp_rack_force_max_seg); 25003 if (optval) 25004 rack->rc_force_max_seg = 1; 25005 else 25006 rack->rc_force_max_seg = 0; 25007 break; 25008 case TCP_RACK_PACE_MIN_SEG: 25009 RACK_OPTS_INC(tcp_rack_min_seg); 25010 rack->r_ctl.rc_user_set_min_segs = (0x0000ffff & optval); 25011 rack_set_pace_segments(tp, rack, __LINE__, NULL); 25012 break; 25013 case TCP_RACK_PACE_MAX_SEG: 25014 /* Max segments size in a pace in bytes */ 25015 RACK_OPTS_INC(tcp_rack_max_seg); 25016 if ((rack->dgp_on == 1) && 25017 (rack->r_ctl.pacing_method & RACK_DGP_PACING)) { 25018 /* 25019 * If we set a max-seg and are doing DGP then 25020 * we now fall under the pacing limits not the 25021 * DGP ones. 25022 */ 25023 if (tcp_can_enable_pacing() == 0) { 25024 error = ENOSPC; 25025 break; 25026 } 25027 /* 25028 * Now change up the flags and counts to be correct. 25029 */ 25030 rack->r_ctl.pacing_method |= RACK_REG_PACING; 25031 tcp_dec_dgp_pacing_cnt(); 25032 rack->r_ctl.pacing_method &= ~RACK_DGP_PACING; 25033 } 25034 if (optval <= MAX_USER_SET_SEG) 25035 rack->rc_user_set_max_segs = optval; 25036 else 25037 rack->rc_user_set_max_segs = MAX_USER_SET_SEG; 25038 rack_set_pace_segments(tp, rack, __LINE__, NULL); 25039 break; 25040 case TCP_RACK_PACE_RATE_REC: 25041 /* Set the fixed pacing rate in Bytes per second ca */ 25042 RACK_OPTS_INC(tcp_rack_pace_rate_rec); 25043 if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) { 25044 error = EPERM; 25045 break; 25046 } 25047 if (rack->dgp_on) { 25048 /* 25049 * We are already pacing another 25050 * way. 25051 */ 25052 error = EBUSY; 25053 break; 25054 } 25055 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 25056 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0) 25057 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 25058 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0) 25059 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 25060 rack->use_fixed_rate = 1; 25061 if (rack->rack_hibeta) 25062 rack_set_cc_pacing(rack); 25063 rack_log_pacing_delay_calc(rack, 25064 rack->r_ctl.rc_fixed_pacing_rate_ss, 25065 rack->r_ctl.rc_fixed_pacing_rate_ca, 25066 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 25067 __LINE__, NULL,0); 25068 break; 25069 25070 case TCP_RACK_PACE_RATE_SS: 25071 /* Set the fixed pacing rate in Bytes per second ca */ 25072 RACK_OPTS_INC(tcp_rack_pace_rate_ss); 25073 if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) { 25074 error = EPERM; 25075 break; 25076 } 25077 if (rack->dgp_on) { 25078 /* 25079 * We are already pacing another 25080 * way. 25081 */ 25082 error = EBUSY; 25083 break; 25084 } 25085 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 25086 if (rack->r_ctl.rc_fixed_pacing_rate_ca == 0) 25087 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 25088 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0) 25089 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 25090 rack->use_fixed_rate = 1; 25091 if (rack->rack_hibeta) 25092 rack_set_cc_pacing(rack); 25093 rack_log_pacing_delay_calc(rack, 25094 rack->r_ctl.rc_fixed_pacing_rate_ss, 25095 rack->r_ctl.rc_fixed_pacing_rate_ca, 25096 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 25097 __LINE__, NULL, 0); 25098 break; 25099 25100 case TCP_RACK_PACE_RATE_CA: 25101 /* Set the fixed pacing rate in Bytes per second ca */ 25102 RACK_OPTS_INC(tcp_rack_pace_rate_ca); 25103 if (rack->r_ctl.side_chan_dis_mask & CCSP_DIS_MASK) { 25104 error = EPERM; 25105 break; 25106 } 25107 if (rack->dgp_on) { 25108 /* 25109 * We are already pacing another 25110 * way. 25111 */ 25112 error = EBUSY; 25113 break; 25114 } 25115 rack->r_ctl.rc_fixed_pacing_rate_ca = optval; 25116 if (rack->r_ctl.rc_fixed_pacing_rate_ss == 0) 25117 rack->r_ctl.rc_fixed_pacing_rate_ss = optval; 25118 if (rack->r_ctl.rc_fixed_pacing_rate_rec == 0) 25119 rack->r_ctl.rc_fixed_pacing_rate_rec = optval; 25120 rack->use_fixed_rate = 1; 25121 if (rack->rack_hibeta) 25122 rack_set_cc_pacing(rack); 25123 rack_log_pacing_delay_calc(rack, 25124 rack->r_ctl.rc_fixed_pacing_rate_ss, 25125 rack->r_ctl.rc_fixed_pacing_rate_ca, 25126 rack->r_ctl.rc_fixed_pacing_rate_rec, 0, 0, 8, 25127 __LINE__, NULL, 0); 25128 break; 25129 case TCP_RACK_GP_INCREASE_REC: 25130 RACK_OPTS_INC(tcp_gp_inc_rec); 25131 rack->r_ctl.rack_per_of_gp_rec = optval; 25132 rack_log_pacing_delay_calc(rack, 25133 rack->r_ctl.rack_per_of_gp_ss, 25134 rack->r_ctl.rack_per_of_gp_ca, 25135 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 25136 __LINE__, NULL, 0); 25137 break; 25138 case TCP_RACK_GP_INCREASE_CA: 25139 RACK_OPTS_INC(tcp_gp_inc_ca); 25140 ca = optval; 25141 if (ca < 100) { 25142 /* 25143 * We don't allow any reduction 25144 * over the GP b/w. 25145 */ 25146 error = EINVAL; 25147 break; 25148 } 25149 rack->r_ctl.rack_per_of_gp_ca = ca; 25150 rack_log_pacing_delay_calc(rack, 25151 rack->r_ctl.rack_per_of_gp_ss, 25152 rack->r_ctl.rack_per_of_gp_ca, 25153 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 25154 __LINE__, NULL, 0); 25155 break; 25156 case TCP_RACK_GP_INCREASE_SS: 25157 RACK_OPTS_INC(tcp_gp_inc_ss); 25158 ss = optval; 25159 if (ss < 100) { 25160 /* 25161 * We don't allow any reduction 25162 * over the GP b/w. 25163 */ 25164 error = EINVAL; 25165 break; 25166 } 25167 rack->r_ctl.rack_per_of_gp_ss = ss; 25168 rack_log_pacing_delay_calc(rack, 25169 rack->r_ctl.rack_per_of_gp_ss, 25170 rack->r_ctl.rack_per_of_gp_ca, 25171 rack->r_ctl.rack_per_of_gp_rec, 0, 0, 1, 25172 __LINE__, NULL, 0); 25173 break; 25174 case TCP_RACK_RR_CONF: 25175 RACK_OPTS_INC(tcp_rack_rrr_no_conf_rate); 25176 if (optval && optval <= 3) 25177 rack->r_rr_config = optval; 25178 else 25179 rack->r_rr_config = 0; 25180 break; 25181 case TCP_PACING_DND: /* URL:dnd */ 25182 if (optval > 0) 25183 rack->rc_pace_dnd = 1; 25184 else 25185 rack->rc_pace_dnd = 0; 25186 break; 25187 case TCP_HDWR_RATE_CAP: 25188 RACK_OPTS_INC(tcp_hdwr_rate_cap); 25189 if (optval) { 25190 if (rack->r_rack_hw_rate_caps == 0) 25191 rack->r_rack_hw_rate_caps = 1; 25192 else 25193 error = EALREADY; 25194 } else { 25195 rack->r_rack_hw_rate_caps = 0; 25196 } 25197 break; 25198 case TCP_DGP_UPPER_BOUNDS: 25199 { 25200 uint8_t val; 25201 val = optval & 0x0000ff; 25202 rack->r_ctl.rack_per_upper_bound_ca = val; 25203 val = (optval >> 16) & 0x0000ff; 25204 rack->r_ctl.rack_per_upper_bound_ss = val; 25205 break; 25206 } 25207 case TCP_SS_EEXIT: /* URL:eexit */ 25208 if (optval > 0) { 25209 rack->r_ctl.gp_rnd_thresh = optval & 0x0ff; 25210 if (optval & 0x10000) { 25211 rack->r_ctl.gate_to_fs = 1; 25212 } else { 25213 rack->r_ctl.gate_to_fs = 0; 25214 } 25215 if (optval & 0x20000) { 25216 rack->r_ctl.use_gp_not_last = 1; 25217 } else { 25218 rack->r_ctl.use_gp_not_last = 0; 25219 } 25220 if (optval & 0xfffc0000) { 25221 uint32_t v; 25222 25223 v = (optval >> 18) & 0x00003fff; 25224 if (v >= 1000) 25225 rack->r_ctl.gp_gain_req = v; 25226 } 25227 } else { 25228 /* We do not do ss early exit at all */ 25229 rack->rc_initial_ss_comp = 1; 25230 rack->r_ctl.gp_rnd_thresh = 0; 25231 } 25232 break; 25233 case TCP_RACK_SPLIT_LIMIT: 25234 RACK_OPTS_INC(tcp_split_limit); 25235 rack->r_ctl.rc_split_limit = optval; 25236 break; 25237 case TCP_BBR_HDWR_PACE: 25238 RACK_OPTS_INC(tcp_hdwr_pacing); 25239 if (optval){ 25240 if (rack->rack_hdrw_pacing == 0) { 25241 rack->rack_hdw_pace_ena = 1; 25242 rack->rack_attempt_hdwr_pace = 0; 25243 } else 25244 error = EALREADY; 25245 } else { 25246 rack->rack_hdw_pace_ena = 0; 25247 #ifdef RATELIMIT 25248 if (rack->r_ctl.crte != NULL) { 25249 rack->rack_hdrw_pacing = 0; 25250 rack->rack_attempt_hdwr_pace = 0; 25251 tcp_rel_pacing_rate(rack->r_ctl.crte, tp); 25252 rack->r_ctl.crte = NULL; 25253 } 25254 #endif 25255 } 25256 break; 25257 /* End Pacing related ones */ 25258 case TCP_RACK_PRR_SENDALOT: 25259 /* Allow PRR to send more than one seg */ 25260 RACK_OPTS_INC(tcp_rack_prr_sendalot); 25261 rack->r_ctl.rc_prr_sendalot = optval; 25262 break; 25263 case TCP_RACK_MIN_TO: 25264 /* Minimum time between rack t-o's in ms */ 25265 RACK_OPTS_INC(tcp_rack_min_to); 25266 rack->r_ctl.rc_min_to = optval; 25267 break; 25268 case TCP_RACK_EARLY_SEG: 25269 /* If early recovery max segments */ 25270 RACK_OPTS_INC(tcp_rack_early_seg); 25271 rack->r_ctl.rc_early_recovery_segs = optval; 25272 break; 25273 case TCP_RACK_ENABLE_HYSTART: 25274 { 25275 if (optval) { 25276 tp->t_ccv.flags |= CCF_HYSTART_ALLOWED; 25277 if (rack_do_hystart > RACK_HYSTART_ON) 25278 tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND; 25279 if (rack_do_hystart > RACK_HYSTART_ON_W_SC) 25280 tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH; 25281 } else { 25282 tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH); 25283 } 25284 } 25285 break; 25286 case TCP_RACK_REORD_THRESH: 25287 /* RACK reorder threshold (shift amount) */ 25288 RACK_OPTS_INC(tcp_rack_reord_thresh); 25289 if ((optval > 0) && (optval < 31)) 25290 rack->r_ctl.rc_reorder_shift = optval; 25291 else 25292 error = EINVAL; 25293 break; 25294 case TCP_RACK_REORD_FADE: 25295 /* Does reordering fade after ms time */ 25296 RACK_OPTS_INC(tcp_rack_reord_fade); 25297 rack->r_ctl.rc_reorder_fade = optval; 25298 break; 25299 case TCP_RACK_TLP_THRESH: 25300 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 25301 RACK_OPTS_INC(tcp_rack_tlp_thresh); 25302 if (optval) 25303 rack->r_ctl.rc_tlp_threshold = optval; 25304 else 25305 error = EINVAL; 25306 break; 25307 case TCP_BBR_USE_RACK_RR: 25308 RACK_OPTS_INC(tcp_rack_rr); 25309 if (optval) 25310 rack->use_rack_rr = 1; 25311 else 25312 rack->use_rack_rr = 0; 25313 break; 25314 case TCP_RACK_PKT_DELAY: 25315 /* RACK added ms i.e. rack-rtt + reord + N */ 25316 RACK_OPTS_INC(tcp_rack_pkt_delay); 25317 rack->r_ctl.rc_pkt_delay = optval; 25318 break; 25319 case TCP_DELACK: 25320 RACK_OPTS_INC(tcp_rack_delayed_ack); 25321 if (optval == 0) 25322 tp->t_delayed_ack = 0; 25323 else 25324 tp->t_delayed_ack = 1; 25325 if (tp->t_flags & TF_DELACK) { 25326 tp->t_flags &= ~TF_DELACK; 25327 tp->t_flags |= TF_ACKNOW; 25328 NET_EPOCH_ENTER(et); 25329 rack_output(tp); 25330 NET_EPOCH_EXIT(et); 25331 } 25332 break; 25333 25334 case TCP_BBR_RACK_RTT_USE: 25335 RACK_OPTS_INC(tcp_rack_rtt_use); 25336 if ((optval != USE_RTT_HIGH) && 25337 (optval != USE_RTT_LOW) && 25338 (optval != USE_RTT_AVG)) 25339 error = EINVAL; 25340 else 25341 rack->r_ctl.rc_rate_sample_method = optval; 25342 break; 25343 case TCP_HONOR_HPTS_MIN: 25344 RACK_OPTS_INC(tcp_honor_hpts); 25345 if (optval) { 25346 rack->r_use_hpts_min = 1; 25347 /* 25348 * Must be between 2 - 80% to be a reduction else 25349 * we keep the default (10%). 25350 */ 25351 if ((optval > 1) && (optval <= 80)) { 25352 rack->r_ctl.max_reduction = optval; 25353 } 25354 } else 25355 rack->r_use_hpts_min = 0; 25356 break; 25357 case TCP_REC_IS_DYN: /* URL:dynrec */ 25358 RACK_OPTS_INC(tcp_dyn_rec); 25359 if (optval) 25360 rack->rc_gp_no_rec_chg = 1; 25361 else 25362 rack->rc_gp_no_rec_chg = 0; 25363 break; 25364 case TCP_NO_TIMELY: 25365 RACK_OPTS_INC(tcp_notimely); 25366 if (optval) { 25367 rack->rc_skip_timely = 1; 25368 rack->r_ctl.rack_per_of_gp_rec = 90; 25369 rack->r_ctl.rack_per_of_gp_ca = 100; 25370 rack->r_ctl.rack_per_of_gp_ss = 250; 25371 } else { 25372 rack->rc_skip_timely = 0; 25373 } 25374 break; 25375 case TCP_GP_USE_LTBW: 25376 if (optval == 0) { 25377 rack->use_lesser_lt_bw = 0; 25378 rack->dis_lt_bw = 1; 25379 } else if (optval == 1) { 25380 rack->use_lesser_lt_bw = 1; 25381 rack->dis_lt_bw = 0; 25382 } else if (optval == 2) { 25383 rack->use_lesser_lt_bw = 0; 25384 rack->dis_lt_bw = 0; 25385 } 25386 break; 25387 case TCP_DATA_AFTER_CLOSE: 25388 RACK_OPTS_INC(tcp_data_after_close); 25389 if (optval) 25390 rack->rc_allow_data_af_clo = 1; 25391 else 25392 rack->rc_allow_data_af_clo = 0; 25393 break; 25394 default: 25395 break; 25396 } 25397 tcp_log_socket_option(tp, sopt_name, optval, error); 25398 return (error); 25399 } 25400 25401 static void 25402 rack_inherit(struct tcpcb *tp, struct inpcb *parent) 25403 { 25404 /* 25405 * A new connection has been created (tp) and 25406 * the parent is the inpcb given. We want to 25407 * apply a read-lock to the parent (we are already 25408 * holding a write lock on the tp) and copy anything 25409 * out of the rack specific data as long as its tfb is 25410 * the same as ours i.e. we are the same stack. Otherwise 25411 * we just return. 25412 */ 25413 struct tcpcb *par; 25414 struct tcp_rack *dest, *src; 25415 int cnt = 0; 25416 25417 par = intotcpcb(parent); 25418 if (par->t_fb != tp->t_fb) { 25419 /* Not the same stack */ 25420 tcp_log_socket_option(tp, 0, 0, 1); 25421 return; 25422 } 25423 /* Ok if we reach here lets setup the two rack pointers */ 25424 dest = (struct tcp_rack *)tp->t_fb_ptr; 25425 src = (struct tcp_rack *)par->t_fb_ptr; 25426 if ((src == NULL) || (dest == NULL)) { 25427 /* Huh? */ 25428 tcp_log_socket_option(tp, 0, 0, 2); 25429 return; 25430 } 25431 /* Now copy out anything we wish to inherit i.e. things in socket-options */ 25432 /* TCP_RACK_PROFILE we can't know but we can set DGP if its on */ 25433 if ((src->dgp_on) && (dest->dgp_on == 0)) { 25434 /* Profile 1 had to be set via sock opt */ 25435 rack_set_dgp(dest); 25436 cnt++; 25437 } 25438 /* TCP_RACK_SET_RXT_OPTIONS */ 25439 if (dest->full_size_rxt != src->full_size_rxt) { 25440 dest->full_size_rxt = src->full_size_rxt; 25441 cnt++; 25442 } 25443 if (dest->shape_rxt_to_pacing_min != src->shape_rxt_to_pacing_min) { 25444 dest->shape_rxt_to_pacing_min = src->shape_rxt_to_pacing_min; 25445 cnt++; 25446 } 25447 /* TCP_RACK_DSACK_OPT */ 25448 if (dest->rc_rack_tmr_std_based != src->rc_rack_tmr_std_based) { 25449 dest->rc_rack_tmr_std_based = src->rc_rack_tmr_std_based; 25450 cnt++; 25451 } 25452 if (dest->rc_rack_use_dsack != src->rc_rack_use_dsack) { 25453 dest->rc_rack_use_dsack = src->rc_rack_use_dsack; 25454 cnt++; 25455 } 25456 /* TCP_RACK_PACING_DIVISOR */ 25457 if (dest->r_ctl.pace_len_divisor != src->r_ctl.pace_len_divisor) { 25458 dest->r_ctl.pace_len_divisor = src->r_ctl.pace_len_divisor; 25459 cnt++; 25460 } 25461 /* TCP_RACK_HI_BETA */ 25462 if (src->rack_hibeta != dest->rack_hibeta) { 25463 cnt++; 25464 if (src->rack_hibeta) { 25465 dest->r_ctl.rc_saved_beta.beta = src->r_ctl.rc_saved_beta.beta; 25466 dest->rack_hibeta = 1; 25467 } else { 25468 dest->rack_hibeta = 0; 25469 } 25470 } 25471 /* TCP_RACK_TIMER_SLOP */ 25472 if (dest->r_ctl.timer_slop != src->r_ctl.timer_slop) { 25473 dest->r_ctl.timer_slop = src->r_ctl.timer_slop; 25474 cnt++; 25475 } 25476 /* TCP_RACK_PACING_BETA_ECN */ 25477 if (dest->r_ctl.rc_saved_beta.beta_ecn != src->r_ctl.rc_saved_beta.beta_ecn) { 25478 dest->r_ctl.rc_saved_beta.beta_ecn = src->r_ctl.rc_saved_beta.beta_ecn; 25479 cnt++; 25480 } 25481 if (dest->r_ctl.rc_saved_beta.newreno_flags != src->r_ctl.rc_saved_beta.newreno_flags) { 25482 dest->r_ctl.rc_saved_beta.newreno_flags = src->r_ctl.rc_saved_beta.newreno_flags; 25483 cnt++; 25484 } 25485 /* We do not do TCP_DEFER_OPTIONS */ 25486 /* TCP_RACK_MEASURE_CNT */ 25487 if (dest->r_ctl.req_measurements != src->r_ctl.req_measurements) { 25488 dest->r_ctl.req_measurements = src->r_ctl.req_measurements; 25489 cnt++; 25490 } 25491 /* TCP_HDWR_UP_ONLY */ 25492 if (dest->r_up_only != src->r_up_only) { 25493 dest->r_up_only = src->r_up_only; 25494 cnt++; 25495 } 25496 /* TCP_FILLCW_RATE_CAP */ 25497 if (dest->r_ctl.fillcw_cap != src->r_ctl.fillcw_cap) { 25498 dest->r_ctl.fillcw_cap = src->r_ctl.fillcw_cap; 25499 cnt++; 25500 } 25501 /* TCP_PACING_RATE_CAP */ 25502 if (dest->r_ctl.bw_rate_cap != src->r_ctl.bw_rate_cap) { 25503 dest->r_ctl.bw_rate_cap = src->r_ctl.bw_rate_cap; 25504 cnt++; 25505 } 25506 /* A listener can't set TCP_HYBRID_PACING */ 25507 /* TCP_SIDECHAN_DIS */ 25508 if (dest->r_ctl.side_chan_dis_mask != src->r_ctl.side_chan_dis_mask) { 25509 dest->r_ctl.side_chan_dis_mask = src->r_ctl.side_chan_dis_mask; 25510 cnt++; 25511 } 25512 /* TCP_SHARED_CWND_TIME_LIMIT */ 25513 if (dest->r_limit_scw != src->r_limit_scw) { 25514 dest->r_limit_scw = src->r_limit_scw; 25515 cnt++; 25516 } 25517 /* TCP_POLICER_DETECT */ 25518 if (dest->r_ctl.policer_rxt_threshold != src->r_ctl.policer_rxt_threshold) { 25519 dest->r_ctl.policer_rxt_threshold = src->r_ctl.policer_rxt_threshold; 25520 cnt++; 25521 } 25522 if (dest->r_ctl.policer_avg_threshold != src->r_ctl.policer_avg_threshold) { 25523 dest->r_ctl.policer_avg_threshold = src->r_ctl.policer_avg_threshold; 25524 cnt++; 25525 } 25526 if (dest->r_ctl.policer_med_threshold != src->r_ctl.policer_med_threshold) { 25527 dest->r_ctl.policer_med_threshold = src->r_ctl.policer_med_threshold; 25528 cnt++; 25529 } 25530 if (dest->policer_detect_on != src->policer_detect_on) { 25531 dest->policer_detect_on = src->policer_detect_on; 25532 cnt++; 25533 } 25534 25535 if (dest->r_ctl.saved_policer_val != src->r_ctl.saved_policer_val) { 25536 dest->r_ctl.saved_policer_val = src->r_ctl.saved_policer_val; 25537 cnt++; 25538 } 25539 /* TCP_POLICER_MSS */ 25540 if (dest->r_ctl.policer_del_mss != src->r_ctl.policer_del_mss) { 25541 dest->r_ctl.policer_del_mss = src->r_ctl.policer_del_mss; 25542 cnt++; 25543 } 25544 25545 if (dest->r_ctl.pol_bw_comp != src->r_ctl.pol_bw_comp) { 25546 dest->r_ctl.pol_bw_comp = src->r_ctl.pol_bw_comp; 25547 cnt++; 25548 } 25549 25550 if (dest->r_ctl.policer_alt_median != src->r_ctl.policer_alt_median) { 25551 dest->r_ctl.policer_alt_median = src->r_ctl.policer_alt_median; 25552 cnt++; 25553 } 25554 /* TCP_RACK_PACE_TO_FILL */ 25555 if (dest->rc_pace_to_cwnd != src->rc_pace_to_cwnd) { 25556 dest->rc_pace_to_cwnd = src->rc_pace_to_cwnd; 25557 cnt++; 25558 } 25559 if (dest->rc_pace_fill_if_rttin_range != src->rc_pace_fill_if_rttin_range) { 25560 dest->rc_pace_fill_if_rttin_range = src->rc_pace_fill_if_rttin_range; 25561 cnt++; 25562 } 25563 if (dest->rtt_limit_mul != src->rtt_limit_mul) { 25564 dest->rtt_limit_mul = src->rtt_limit_mul; 25565 cnt++; 25566 } 25567 /* TCP_RACK_NO_PUSH_AT_MAX */ 25568 if (dest->r_ctl.rc_no_push_at_mrtt != src->r_ctl.rc_no_push_at_mrtt) { 25569 dest->r_ctl.rc_no_push_at_mrtt = src->r_ctl.rc_no_push_at_mrtt; 25570 cnt++; 25571 } 25572 /* TCP_SHARED_CWND_ENABLE */ 25573 if (dest->rack_enable_scwnd != src->rack_enable_scwnd) { 25574 dest->rack_enable_scwnd = src->rack_enable_scwnd; 25575 cnt++; 25576 } 25577 /* TCP_USE_CMP_ACKS */ 25578 if (dest->r_use_cmp_ack != src->r_use_cmp_ack) { 25579 dest->r_use_cmp_ack = src->r_use_cmp_ack; 25580 cnt++; 25581 } 25582 25583 if (dest->r_mbuf_queue != src->r_mbuf_queue) { 25584 dest->r_mbuf_queue = src->r_mbuf_queue; 25585 cnt++; 25586 } 25587 /* TCP_RACK_MBUF_QUEUE */ 25588 if (dest->r_mbuf_queue != src->r_mbuf_queue) { 25589 dest->r_mbuf_queue = src->r_mbuf_queue; 25590 cnt++; 25591 } 25592 if (dest->r_mbuf_queue || dest->rc_always_pace || dest->r_use_cmp_ack) { 25593 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 25594 } else { 25595 tp->t_flags2 &= ~TF2_SUPPORTS_MBUFQ; 25596 } 25597 if (dest->r_use_cmp_ack && TCPS_HAVEESTABLISHED(tp->t_state)) { 25598 tp->t_flags2 |= TF2_MBUF_ACKCMP; 25599 } 25600 /* TCP_RACK_NONRXT_CFG_RATE */ 25601 if (dest->rack_rec_nonrxt_use_cr != src->rack_rec_nonrxt_use_cr) { 25602 dest->rack_rec_nonrxt_use_cr = src->rack_rec_nonrxt_use_cr; 25603 cnt++; 25604 } 25605 /* TCP_NO_PRR */ 25606 if (dest->rack_no_prr != src->rack_no_prr) { 25607 dest->rack_no_prr = src->rack_no_prr; 25608 cnt++; 25609 } 25610 if (dest->no_prr_addback != src->no_prr_addback) { 25611 dest->no_prr_addback = src->no_prr_addback; 25612 cnt++; 25613 } 25614 /* RACK_CSPR_IS_FCC */ 25615 if (dest->cspr_is_fcc != src->cspr_is_fcc) { 25616 dest->cspr_is_fcc = src->cspr_is_fcc; 25617 cnt++; 25618 } 25619 /* TCP_TIMELY_DYN_ADJ */ 25620 if (dest->rc_gp_dyn_mul != src->rc_gp_dyn_mul) { 25621 dest->rc_gp_dyn_mul = src->rc_gp_dyn_mul; 25622 cnt++; 25623 } 25624 if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) { 25625 dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca; 25626 cnt++; 25627 } 25628 /* TCP_RACK_DO_DETECTION */ 25629 if (dest->do_detection != src->do_detection) { 25630 dest->do_detection = src->do_detection; 25631 cnt++; 25632 } 25633 /* TCP_RACK_TLP_USE */ 25634 if (dest->rack_tlp_threshold_use != src->rack_tlp_threshold_use) { 25635 dest->rack_tlp_threshold_use = src->rack_tlp_threshold_use; 25636 cnt++; 25637 } 25638 /* we don't allow inheritence of TCP_RACK_PACE_ALWAYS */ 25639 /* TCP_BBR_RACK_INIT_RATE */ 25640 if (dest->r_ctl.init_rate != src->r_ctl.init_rate) { 25641 dest->r_ctl.init_rate = src->r_ctl.init_rate; 25642 cnt++; 25643 } 25644 /* TCP_RACK_FORCE_MSEG */ 25645 if (dest->rc_force_max_seg != src->rc_force_max_seg) { 25646 dest->rc_force_max_seg = src->rc_force_max_seg; 25647 cnt++; 25648 } 25649 /* TCP_RACK_PACE_MIN_SEG */ 25650 if (dest->r_ctl.rc_user_set_min_segs != src->r_ctl.rc_user_set_min_segs) { 25651 dest->r_ctl.rc_user_set_min_segs = src->r_ctl.rc_user_set_min_segs; 25652 cnt++; 25653 } 25654 /* we don't allow TCP_RACK_PACE_MAX_SEG */ 25655 /* TCP_RACK_PACE_RATE_REC, TCP_RACK_PACE_RATE_SS, TCP_RACK_PACE_RATE_CA */ 25656 if (dest->r_ctl.rc_fixed_pacing_rate_ca != src->r_ctl.rc_fixed_pacing_rate_ca) { 25657 dest->r_ctl.rc_fixed_pacing_rate_ca = src->r_ctl.rc_fixed_pacing_rate_ca; 25658 cnt++; 25659 } 25660 if (dest->r_ctl.rc_fixed_pacing_rate_ss != src->r_ctl.rc_fixed_pacing_rate_ss) { 25661 dest->r_ctl.rc_fixed_pacing_rate_ss = src->r_ctl.rc_fixed_pacing_rate_ss; 25662 cnt++; 25663 } 25664 if (dest->r_ctl.rc_fixed_pacing_rate_rec != src->r_ctl.rc_fixed_pacing_rate_rec) { 25665 dest->r_ctl.rc_fixed_pacing_rate_rec = src->r_ctl.rc_fixed_pacing_rate_rec; 25666 cnt++; 25667 } 25668 /* TCP_RACK_GP_INCREASE_REC, TCP_RACK_GP_INCREASE_CA, TCP_RACK_GP_INCREASE_SS */ 25669 if (dest->r_ctl.rack_per_of_gp_rec != src->r_ctl.rack_per_of_gp_rec) { 25670 dest->r_ctl.rack_per_of_gp_rec = src->r_ctl.rack_per_of_gp_rec; 25671 cnt++; 25672 } 25673 if (dest->r_ctl.rack_per_of_gp_ca != src->r_ctl.rack_per_of_gp_ca) { 25674 dest->r_ctl.rack_per_of_gp_ca = src->r_ctl.rack_per_of_gp_ca; 25675 cnt++; 25676 } 25677 25678 if (dest->r_ctl.rack_per_of_gp_ss != src->r_ctl.rack_per_of_gp_ss) { 25679 dest->r_ctl.rack_per_of_gp_ss = src->r_ctl.rack_per_of_gp_ss; 25680 cnt++; 25681 } 25682 /* TCP_RACK_RR_CONF */ 25683 if (dest->r_rr_config != src->r_rr_config) { 25684 dest->r_rr_config = src->r_rr_config; 25685 cnt++; 25686 } 25687 /* TCP_PACING_DND */ 25688 if (dest->rc_pace_dnd != src->rc_pace_dnd) { 25689 dest->rc_pace_dnd = src->rc_pace_dnd; 25690 cnt++; 25691 } 25692 /* TCP_HDWR_RATE_CAP */ 25693 if (dest->r_rack_hw_rate_caps != src->r_rack_hw_rate_caps) { 25694 dest->r_rack_hw_rate_caps = src->r_rack_hw_rate_caps; 25695 cnt++; 25696 } 25697 /* TCP_DGP_UPPER_BOUNDS */ 25698 if (dest->r_ctl.rack_per_upper_bound_ca != src->r_ctl.rack_per_upper_bound_ca) { 25699 dest->r_ctl.rack_per_upper_bound_ca = src->r_ctl.rack_per_upper_bound_ca; 25700 cnt++; 25701 } 25702 if (dest->r_ctl.rack_per_upper_bound_ss != src->r_ctl.rack_per_upper_bound_ss) { 25703 dest->r_ctl.rack_per_upper_bound_ss = src->r_ctl.rack_per_upper_bound_ss; 25704 cnt++; 25705 } 25706 /* TCP_SS_EEXIT */ 25707 if (dest->r_ctl.gp_rnd_thresh != src->r_ctl.gp_rnd_thresh) { 25708 dest->r_ctl.gp_rnd_thresh = src->r_ctl.gp_rnd_thresh; 25709 cnt++; 25710 } 25711 if (dest->r_ctl.gate_to_fs != src->r_ctl.gate_to_fs) { 25712 dest->r_ctl.gate_to_fs = src->r_ctl.gate_to_fs; 25713 cnt++; 25714 } 25715 if (dest->r_ctl.use_gp_not_last != src->r_ctl.use_gp_not_last) { 25716 dest->r_ctl.use_gp_not_last = src->r_ctl.use_gp_not_last; 25717 cnt++; 25718 } 25719 if (dest->r_ctl.gp_gain_req != src->r_ctl.gp_gain_req) { 25720 dest->r_ctl.gp_gain_req = src->r_ctl.gp_gain_req; 25721 cnt++; 25722 } 25723 /* TCP_BBR_HDWR_PACE */ 25724 if (dest->rack_hdw_pace_ena != src->rack_hdw_pace_ena) { 25725 dest->rack_hdw_pace_ena = src->rack_hdw_pace_ena; 25726 cnt++; 25727 } 25728 if (dest->rack_attempt_hdwr_pace != src->rack_attempt_hdwr_pace) { 25729 dest->rack_attempt_hdwr_pace = src->rack_attempt_hdwr_pace; 25730 cnt++; 25731 } 25732 /* TCP_RACK_PRR_SENDALOT */ 25733 if (dest->r_ctl.rc_prr_sendalot != src->r_ctl.rc_prr_sendalot) { 25734 dest->r_ctl.rc_prr_sendalot = src->r_ctl.rc_prr_sendalot; 25735 cnt++; 25736 } 25737 /* TCP_RACK_MIN_TO */ 25738 if (dest->r_ctl.rc_min_to != src->r_ctl.rc_min_to) { 25739 dest->r_ctl.rc_min_to = src->r_ctl.rc_min_to; 25740 cnt++; 25741 } 25742 /* TCP_RACK_EARLY_SEG */ 25743 if (dest->r_ctl.rc_early_recovery_segs != src->r_ctl.rc_early_recovery_segs) { 25744 dest->r_ctl.rc_early_recovery_segs = src->r_ctl.rc_early_recovery_segs; 25745 cnt++; 25746 } 25747 /* TCP_RACK_ENABLE_HYSTART */ 25748 if (par->t_ccv.flags != tp->t_ccv.flags) { 25749 cnt++; 25750 if (par->t_ccv.flags & CCF_HYSTART_ALLOWED) { 25751 tp->t_ccv.flags |= CCF_HYSTART_ALLOWED; 25752 if (rack_do_hystart > RACK_HYSTART_ON) 25753 tp->t_ccv.flags |= CCF_HYSTART_CAN_SH_CWND; 25754 if (rack_do_hystart > RACK_HYSTART_ON_W_SC) 25755 tp->t_ccv.flags |= CCF_HYSTART_CONS_SSTH; 25756 } else { 25757 tp->t_ccv.flags &= ~(CCF_HYSTART_ALLOWED|CCF_HYSTART_CAN_SH_CWND|CCF_HYSTART_CONS_SSTH); 25758 } 25759 } 25760 /* TCP_RACK_REORD_THRESH */ 25761 if (dest->r_ctl.rc_reorder_shift != src->r_ctl.rc_reorder_shift) { 25762 dest->r_ctl.rc_reorder_shift = src->r_ctl.rc_reorder_shift; 25763 cnt++; 25764 } 25765 /* TCP_RACK_REORD_FADE */ 25766 if (dest->r_ctl.rc_reorder_fade != src->r_ctl.rc_reorder_fade) { 25767 dest->r_ctl.rc_reorder_fade = src->r_ctl.rc_reorder_fade; 25768 cnt++; 25769 } 25770 /* TCP_RACK_TLP_THRESH */ 25771 if (dest->r_ctl.rc_tlp_threshold != src->r_ctl.rc_tlp_threshold) { 25772 dest->r_ctl.rc_tlp_threshold = src->r_ctl.rc_tlp_threshold; 25773 cnt++; 25774 } 25775 /* TCP_BBR_USE_RACK_RR */ 25776 if (dest->use_rack_rr != src->use_rack_rr) { 25777 dest->use_rack_rr = src->use_rack_rr; 25778 cnt++; 25779 } 25780 /* TCP_RACK_PKT_DELAY */ 25781 if (dest->r_ctl.rc_pkt_delay != src->r_ctl.rc_pkt_delay) { 25782 dest->r_ctl.rc_pkt_delay = src->r_ctl.rc_pkt_delay; 25783 cnt++; 25784 } 25785 /* TCP_DELACK will get copied via the main code if applicable */ 25786 /* TCP_BBR_RACK_RTT_USE */ 25787 if (dest->r_ctl.rc_rate_sample_method != src->r_ctl.rc_rate_sample_method) { 25788 dest->r_ctl.rc_rate_sample_method = src->r_ctl.rc_rate_sample_method; 25789 cnt++; 25790 } 25791 /* TCP_HONOR_HPTS_MIN */ 25792 if (dest->r_use_hpts_min != src->r_use_hpts_min) { 25793 dest->r_use_hpts_min = src->r_use_hpts_min; 25794 cnt++; 25795 } 25796 if (dest->r_ctl.max_reduction != src->r_ctl.max_reduction) { 25797 dest->r_ctl.max_reduction = src->r_ctl.max_reduction; 25798 cnt++; 25799 } 25800 /* TCP_REC_IS_DYN */ 25801 if (dest->rc_gp_no_rec_chg != src->rc_gp_no_rec_chg) { 25802 dest->rc_gp_no_rec_chg = src->rc_gp_no_rec_chg; 25803 cnt++; 25804 } 25805 if (dest->rc_skip_timely != src->rc_skip_timely) { 25806 dest->rc_skip_timely = src->rc_skip_timely; 25807 cnt++; 25808 } 25809 /* TCP_DATA_AFTER_CLOSE */ 25810 if (dest->rc_allow_data_af_clo != src->rc_allow_data_af_clo) { 25811 dest->rc_allow_data_af_clo = src->rc_allow_data_af_clo; 25812 cnt++; 25813 } 25814 /* TCP_GP_USE_LTBW */ 25815 if (src->use_lesser_lt_bw != dest->use_lesser_lt_bw) { 25816 dest->use_lesser_lt_bw = src->use_lesser_lt_bw; 25817 cnt++; 25818 } 25819 if (dest->dis_lt_bw != src->dis_lt_bw) { 25820 dest->dis_lt_bw = src->dis_lt_bw; 25821 cnt++; 25822 } 25823 tcp_log_socket_option(tp, 0, cnt, 0); 25824 } 25825 25826 25827 static void 25828 rack_apply_deferred_options(struct tcp_rack *rack) 25829 { 25830 struct deferred_opt_list *dol, *sdol; 25831 uint32_t s_optval; 25832 25833 TAILQ_FOREACH_SAFE(dol, &rack->r_ctl.opt_list, next, sdol) { 25834 TAILQ_REMOVE(&rack->r_ctl.opt_list, dol, next); 25835 /* Disadvantage of deferal is you loose the error return */ 25836 s_optval = (uint32_t)dol->optval; 25837 (void)rack_process_option(rack->rc_tp, rack, dol->optname, s_optval, dol->optval, NULL); 25838 free(dol, M_TCPDO); 25839 } 25840 } 25841 25842 static void 25843 rack_hw_tls_change(struct tcpcb *tp, int chg) 25844 { 25845 /* Update HW tls state */ 25846 struct tcp_rack *rack; 25847 25848 rack = (struct tcp_rack *)tp->t_fb_ptr; 25849 if (chg) 25850 rack->r_ctl.fsb.hw_tls = 1; 25851 else 25852 rack->r_ctl.fsb.hw_tls = 0; 25853 } 25854 25855 static int 25856 rack_pru_options(struct tcpcb *tp, int flags) 25857 { 25858 if (flags & PRUS_OOB) 25859 return (EOPNOTSUPP); 25860 return (0); 25861 } 25862 25863 static bool 25864 rack_wake_check(struct tcpcb *tp) 25865 { 25866 struct tcp_rack *rack; 25867 struct timeval tv; 25868 uint32_t cts; 25869 25870 rack = (struct tcp_rack *)tp->t_fb_ptr; 25871 if (rack->r_ctl.rc_hpts_flags) { 25872 cts = tcp_get_usecs(&tv); 25873 if ((rack->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == PACE_PKT_OUTPUT){ 25874 /* 25875 * Pacing timer is up, check if we are ready. 25876 */ 25877 if (TSTMP_GEQ(cts, rack->r_ctl.rc_last_output_to)) 25878 return (true); 25879 } else if ((rack->r_ctl.rc_hpts_flags & PACE_TMR_MASK) != 0) { 25880 /* 25881 * A timer is up, check if we are ready. 25882 */ 25883 if (TSTMP_GEQ(cts, rack->r_ctl.rc_timer_exp)) 25884 return (true); 25885 } 25886 } 25887 return (false); 25888 } 25889 25890 static struct tcp_function_block __tcp_rack = { 25891 .tfb_tcp_block_name = __XSTRING(STACKNAME), 25892 .tfb_tcp_output = rack_output, 25893 .tfb_do_queued_segments = ctf_do_queued_segments, 25894 .tfb_do_segment_nounlock = rack_do_segment_nounlock, 25895 .tfb_tcp_do_segment = rack_do_segment, 25896 .tfb_tcp_ctloutput = rack_ctloutput, 25897 .tfb_tcp_fb_init = rack_init, 25898 .tfb_tcp_fb_fini = rack_fini, 25899 .tfb_tcp_timer_stop_all = rack_stopall, 25900 .tfb_tcp_rexmit_tmr = rack_remxt_tmr, 25901 .tfb_tcp_handoff_ok = rack_handoff_ok, 25902 .tfb_tcp_mtu_chg = rack_mtu_change, 25903 .tfb_pru_options = rack_pru_options, 25904 .tfb_hwtls_change = rack_hw_tls_change, 25905 .tfb_chg_query = rack_chg_query, 25906 .tfb_switch_failed = rack_switch_failed, 25907 .tfb_early_wake_check = rack_wake_check, 25908 .tfb_compute_pipe = rack_compute_pipe, 25909 .tfb_stack_info = rack_stack_information, 25910 .tfb_inherit = rack_inherit, 25911 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP, 25912 25913 }; 25914 25915 /* 25916 * rack_ctloutput() must drop the inpcb lock before performing copyin on 25917 * socket option arguments. When it re-acquires the lock after the copy, it 25918 * has to revalidate that the connection is still valid for the socket 25919 * option. 25920 */ 25921 static int 25922 rack_set_sockopt(struct tcpcb *tp, struct sockopt *sopt) 25923 { 25924 struct inpcb *inp = tptoinpcb(tp); 25925 #ifdef INET 25926 struct ip *ip; 25927 #endif 25928 struct tcp_rack *rack; 25929 struct tcp_hybrid_req hybrid; 25930 uint64_t loptval; 25931 int32_t error = 0, optval; 25932 25933 rack = (struct tcp_rack *)tp->t_fb_ptr; 25934 if (rack == NULL) { 25935 INP_WUNLOCK(inp); 25936 return (EINVAL); 25937 } 25938 #ifdef INET 25939 ip = (struct ip *)rack->r_ctl.fsb.tcp_ip_hdr; 25940 #endif 25941 25942 switch (sopt->sopt_level) { 25943 #ifdef INET6 25944 case IPPROTO_IPV6: 25945 MPASS(inp->inp_vflag & INP_IPV6PROTO); 25946 switch (sopt->sopt_name) { 25947 case IPV6_USE_MIN_MTU: 25948 tcp6_use_min_mtu(tp); 25949 break; 25950 } 25951 INP_WUNLOCK(inp); 25952 return (0); 25953 #endif 25954 #ifdef INET 25955 case IPPROTO_IP: 25956 switch (sopt->sopt_name) { 25957 case IP_TOS: 25958 /* 25959 * The DSCP codepoint has changed, update the fsb. 25960 */ 25961 ip->ip_tos = rack->rc_inp->inp_ip_tos; 25962 break; 25963 case IP_TTL: 25964 /* 25965 * The TTL has changed, update the fsb. 25966 */ 25967 ip->ip_ttl = rack->rc_inp->inp_ip_ttl; 25968 break; 25969 } 25970 INP_WUNLOCK(inp); 25971 return (0); 25972 #endif 25973 #ifdef SO_PEERPRIO 25974 case SOL_SOCKET: 25975 switch (sopt->sopt_name) { 25976 case SO_PEERPRIO: /* SC-URL:bs */ 25977 /* Already read in and sanity checked in sosetopt(). */ 25978 if (inp->inp_socket) { 25979 rack->client_bufferlvl = inp->inp_socket->so_peerprio; 25980 } 25981 break; 25982 } 25983 INP_WUNLOCK(inp); 25984 return (0); 25985 #endif 25986 case IPPROTO_TCP: 25987 switch (sopt->sopt_name) { 25988 case TCP_RACK_TLP_REDUCE: /* URL:tlp_reduce */ 25989 /* Pacing related ones */ 25990 case TCP_RACK_PACE_ALWAYS: /* URL:pace_always */ 25991 case TCP_BBR_RACK_INIT_RATE: /* URL:irate */ 25992 case TCP_RACK_PACE_MIN_SEG: /* URL:pace_min_seg */ 25993 case TCP_RACK_PACE_MAX_SEG: /* URL:pace_max_seg */ 25994 case TCP_RACK_FORCE_MSEG: /* URL:force_max_seg */ 25995 case TCP_RACK_PACE_RATE_CA: /* URL:pr_ca */ 25996 case TCP_RACK_PACE_RATE_SS: /* URL:pr_ss*/ 25997 case TCP_RACK_PACE_RATE_REC: /* URL:pr_rec */ 25998 case TCP_RACK_GP_INCREASE_CA: /* URL:gp_inc_ca */ 25999 case TCP_RACK_GP_INCREASE_SS: /* URL:gp_inc_ss */ 26000 case TCP_RACK_GP_INCREASE_REC: /* URL:gp_inc_rec */ 26001 case TCP_RACK_RR_CONF: /* URL:rrr_conf */ 26002 case TCP_BBR_HDWR_PACE: /* URL:hdwrpace */ 26003 case TCP_HDWR_RATE_CAP: /* URL:hdwrcap boolean */ 26004 case TCP_PACING_RATE_CAP: /* URL:cap -- used by side-channel */ 26005 case TCP_HDWR_UP_ONLY: /* URL:uponly -- hardware pacing boolean */ 26006 case TCP_FILLCW_RATE_CAP: /* URL:fillcw_cap */ 26007 case TCP_RACK_PACING_BETA_ECN: /* URL:pacing_beta_ecn */ 26008 case TCP_RACK_PACE_TO_FILL: /* URL:fillcw */ 26009 /* End pacing related */ 26010 case TCP_POLICER_DETECT: /* URL:pol_det */ 26011 case TCP_POLICER_MSS: /* URL:pol_mss */ 26012 case TCP_DELACK: /* URL:delack (in base TCP i.e. tcp_hints along with cc etc ) */ 26013 case TCP_RACK_PRR_SENDALOT: /* URL:prr_sendalot */ 26014 case TCP_RACK_MIN_TO: /* URL:min_to */ 26015 case TCP_RACK_EARLY_SEG: /* URL:early_seg */ 26016 case TCP_RACK_REORD_THRESH: /* URL:reord_thresh */ 26017 case TCP_RACK_REORD_FADE: /* URL:reord_fade */ 26018 case TCP_RACK_TLP_THRESH: /* URL:tlp_thresh */ 26019 case TCP_RACK_PKT_DELAY: /* URL:pkt_delay */ 26020 case TCP_RACK_TLP_USE: /* URL:tlp_use */ 26021 case TCP_BBR_RACK_RTT_USE: /* URL:rttuse */ 26022 case TCP_BBR_USE_RACK_RR: /* URL:rackrr */ 26023 case TCP_RACK_DO_DETECTION: /* URL:detect */ 26024 case TCP_NO_PRR: /* URL:noprr */ 26025 case TCP_TIMELY_DYN_ADJ: /* URL:dynamic */ 26026 case TCP_DATA_AFTER_CLOSE: /* no URL */ 26027 case TCP_RACK_NONRXT_CFG_RATE: /* URL:nonrxtcr */ 26028 case TCP_SHARED_CWND_ENABLE: /* URL:scwnd */ 26029 case TCP_RACK_MBUF_QUEUE: /* URL:mqueue */ 26030 case TCP_RACK_NO_PUSH_AT_MAX: /* URL:npush */ 26031 case TCP_SHARED_CWND_TIME_LIMIT: /* URL:lscwnd */ 26032 case TCP_RACK_PROFILE: /* URL:profile */ 26033 case TCP_SIDECHAN_DIS: /* URL:scodm */ 26034 case TCP_HYBRID_PACING: /* URL:pacing=hybrid */ 26035 case TCP_USE_CMP_ACKS: /* URL:cmpack */ 26036 case TCP_RACK_ABC_VAL: /* URL:labc */ 26037 case TCP_REC_ABC_VAL: /* URL:reclabc */ 26038 case TCP_RACK_MEASURE_CNT: /* URL:measurecnt */ 26039 case TCP_DEFER_OPTIONS: /* URL:defer */ 26040 case TCP_RACK_DSACK_OPT: /* URL:dsack */ 26041 case TCP_RACK_TIMER_SLOP: /* URL:timer_slop */ 26042 case TCP_RACK_ENABLE_HYSTART: /* URL:hystart */ 26043 case TCP_RACK_SET_RXT_OPTIONS: /* URL:rxtsz */ 26044 case TCP_RACK_HI_BETA: /* URL:hibeta */ 26045 case TCP_RACK_SPLIT_LIMIT: /* URL:split */ 26046 case TCP_SS_EEXIT: /* URL:eexit */ 26047 case TCP_DGP_UPPER_BOUNDS: /* URL:upper */ 26048 case TCP_RACK_PACING_DIVISOR: /* URL:divisor */ 26049 case TCP_PACING_DND: /* URL:dnd */ 26050 case TCP_NO_TIMELY: /* URL:notimely */ 26051 case RACK_CSPR_IS_FCC: /* URL:csprisfcc */ 26052 case TCP_HONOR_HPTS_MIN: /* URL:hptsmin */ 26053 case TCP_REC_IS_DYN: /* URL:dynrec */ 26054 case TCP_GP_USE_LTBW: /* URL:useltbw */ 26055 goto process_opt; 26056 break; 26057 default: 26058 /* Filter off all unknown options to the base stack */ 26059 return (tcp_default_ctloutput(tp, sopt)); 26060 break; 26061 } 26062 default: 26063 INP_WUNLOCK(inp); 26064 return (0); 26065 } 26066 process_opt: 26067 INP_WUNLOCK(inp); 26068 if ((sopt->sopt_name == TCP_PACING_RATE_CAP) || 26069 (sopt->sopt_name == TCP_FILLCW_RATE_CAP)) { 26070 error = sooptcopyin(sopt, &loptval, sizeof(loptval), sizeof(loptval)); 26071 /* 26072 * We truncate it down to 32 bits for the socket-option trace this 26073 * means rates > 34Gbps won't show right, but thats probably ok. 26074 */ 26075 optval = (uint32_t)loptval; 26076 } else if (sopt->sopt_name == TCP_HYBRID_PACING) { 26077 error = sooptcopyin(sopt, &hybrid, sizeof(hybrid), sizeof(hybrid)); 26078 } else { 26079 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 26080 /* Save it in 64 bit form too */ 26081 loptval = optval; 26082 } 26083 if (error) 26084 return (error); 26085 INP_WLOCK(inp); 26086 if (tp->t_fb != &__tcp_rack) { 26087 INP_WUNLOCK(inp); 26088 return (ENOPROTOOPT); 26089 } 26090 if (rack->defer_options && (rack->gp_ready == 0) && 26091 (sopt->sopt_name != TCP_DEFER_OPTIONS) && 26092 (sopt->sopt_name != TCP_HYBRID_PACING) && 26093 (sopt->sopt_name != TCP_RACK_SET_RXT_OPTIONS) && 26094 (sopt->sopt_name != TCP_RACK_PACING_BETA_ECN) && 26095 (sopt->sopt_name != TCP_RACK_MEASURE_CNT)) { 26096 /* Options are being deferred */ 26097 if (rack_add_deferred_option(rack, sopt->sopt_name, loptval)) { 26098 INP_WUNLOCK(inp); 26099 return (0); 26100 } else { 26101 /* No memory to defer, fail */ 26102 INP_WUNLOCK(inp); 26103 return (ENOMEM); 26104 } 26105 } 26106 error = rack_process_option(tp, rack, sopt->sopt_name, optval, loptval, &hybrid); 26107 INP_WUNLOCK(inp); 26108 return (error); 26109 } 26110 26111 static void 26112 rack_fill_info(struct tcpcb *tp, struct tcp_info *ti) 26113 { 26114 26115 INP_WLOCK_ASSERT(tptoinpcb(tp)); 26116 bzero(ti, sizeof(*ti)); 26117 26118 ti->tcpi_state = tp->t_state; 26119 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 26120 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 26121 if (tp->t_flags & TF_SACK_PERMIT) 26122 ti->tcpi_options |= TCPI_OPT_SACK; 26123 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 26124 ti->tcpi_options |= TCPI_OPT_WSCALE; 26125 ti->tcpi_snd_wscale = tp->snd_scale; 26126 ti->tcpi_rcv_wscale = tp->rcv_scale; 26127 } 26128 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 26129 ti->tcpi_options |= TCPI_OPT_ECN; 26130 if (tp->t_flags & TF_FASTOPEN) 26131 ti->tcpi_options |= TCPI_OPT_TFO; 26132 /* still kept in ticks is t_rcvtime */ 26133 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 26134 /* Since we hold everything in precise useconds this is easy */ 26135 ti->tcpi_rtt = tp->t_srtt; 26136 ti->tcpi_rttvar = tp->t_rttvar; 26137 ti->tcpi_rto = tp->t_rxtcur; 26138 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 26139 ti->tcpi_snd_cwnd = tp->snd_cwnd; 26140 /* 26141 * FreeBSD-specific extension fields for tcp_info. 26142 */ 26143 ti->tcpi_rcv_space = tp->rcv_wnd; 26144 ti->tcpi_rcv_nxt = tp->rcv_nxt; 26145 ti->tcpi_snd_wnd = tp->snd_wnd; 26146 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 26147 ti->tcpi_snd_nxt = tp->snd_nxt; 26148 ti->tcpi_snd_mss = tp->t_maxseg; 26149 ti->tcpi_rcv_mss = tp->t_maxseg; 26150 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 26151 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 26152 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 26153 ti->tcpi_total_tlp = tp->t_sndtlppack; 26154 ti->tcpi_total_tlp_bytes = tp->t_sndtlpbyte; 26155 ti->tcpi_rttmin = tp->t_rttlow; 26156 #ifdef NETFLIX_STATS 26157 memcpy(&ti->tcpi_rxsyninfo, &tp->t_rxsyninfo, sizeof(struct tcpsyninfo)); 26158 #endif 26159 #ifdef TCP_OFFLOAD 26160 if (tp->t_flags & TF_TOE) { 26161 ti->tcpi_options |= TCPI_OPT_TOE; 26162 tcp_offload_tcp_info(tp, ti); 26163 } 26164 #endif 26165 } 26166 26167 static int 26168 rack_get_sockopt(struct tcpcb *tp, struct sockopt *sopt) 26169 { 26170 struct inpcb *inp = tptoinpcb(tp); 26171 struct tcp_rack *rack; 26172 int32_t error, optval; 26173 uint64_t val, loptval; 26174 struct tcp_info ti; 26175 /* 26176 * Because all our options are either boolean or an int, we can just 26177 * pull everything into optval and then unlock and copy. If we ever 26178 * add a option that is not a int, then this will have quite an 26179 * impact to this routine. 26180 */ 26181 error = 0; 26182 rack = (struct tcp_rack *)tp->t_fb_ptr; 26183 if (rack == NULL) { 26184 INP_WUNLOCK(inp); 26185 return (EINVAL); 26186 } 26187 switch (sopt->sopt_name) { 26188 case TCP_INFO: 26189 /* First get the info filled */ 26190 rack_fill_info(tp, &ti); 26191 /* Fix up the rtt related fields if needed */ 26192 INP_WUNLOCK(inp); 26193 error = sooptcopyout(sopt, &ti, sizeof ti); 26194 return (error); 26195 /* 26196 * Beta is the congestion control value for NewReno that influences how 26197 * much of a backoff happens when loss is detected. It is normally set 26198 * to 50 for 50% i.e. the cwnd is reduced to 50% of its previous value 26199 * when you exit recovery. 26200 */ 26201 case TCP_RACK_PACING_BETA: 26202 break; 26203 /* 26204 * Beta_ecn is the congestion control value for NewReno that influences how 26205 * much of a backoff happens when a ECN mark is detected. It is normally set 26206 * to 80 for 80% i.e. the cwnd is reduced by 20% of its previous value when 26207 * you exit recovery. Note that classic ECN has a beta of 50, it is only 26208 * ABE Ecn that uses this "less" value, but we do too with pacing :) 26209 */ 26210 26211 case TCP_RACK_PACING_BETA_ECN: 26212 if (strcmp(tp->t_cc->name, CCALGONAME_NEWRENO) != 0) 26213 error = EINVAL; 26214 else if (rack->rc_pacing_cc_set == 0) 26215 optval = rack->r_ctl.rc_saved_beta.beta_ecn; 26216 else { 26217 /* 26218 * Reach out into the CC data and report back what 26219 * I have previously set. Yeah it looks hackish but 26220 * we don't want to report the saved values. 26221 */ 26222 if (tp->t_ccv.cc_data) 26223 optval = ((struct newreno *)tp->t_ccv.cc_data)->beta_ecn; 26224 else 26225 error = EINVAL; 26226 } 26227 break; 26228 case TCP_RACK_DSACK_OPT: 26229 optval = 0; 26230 if (rack->rc_rack_tmr_std_based) { 26231 optval |= 1; 26232 } 26233 if (rack->rc_rack_use_dsack) { 26234 optval |= 2; 26235 } 26236 break; 26237 case TCP_RACK_ENABLE_HYSTART: 26238 { 26239 if (tp->t_ccv.flags & CCF_HYSTART_ALLOWED) { 26240 optval = RACK_HYSTART_ON; 26241 if (tp->t_ccv.flags & CCF_HYSTART_CAN_SH_CWND) 26242 optval = RACK_HYSTART_ON_W_SC; 26243 if (tp->t_ccv.flags & CCF_HYSTART_CONS_SSTH) 26244 optval = RACK_HYSTART_ON_W_SC_C; 26245 } else { 26246 optval = RACK_HYSTART_OFF; 26247 } 26248 } 26249 break; 26250 case TCP_RACK_DGP_IN_REC: 26251 error = EINVAL; 26252 break; 26253 case TCP_RACK_HI_BETA: 26254 optval = rack->rack_hibeta; 26255 break; 26256 case TCP_POLICER_MSS: 26257 optval = rack->r_ctl.policer_del_mss; 26258 break; 26259 case TCP_POLICER_DETECT: 26260 optval = rack->r_ctl.saved_policer_val; 26261 break; 26262 case TCP_DEFER_OPTIONS: 26263 optval = rack->defer_options; 26264 break; 26265 case TCP_RACK_MEASURE_CNT: 26266 optval = rack->r_ctl.req_measurements; 26267 break; 26268 case TCP_REC_ABC_VAL: 26269 optval = rack->r_use_labc_for_rec; 26270 break; 26271 case TCP_RACK_ABC_VAL: 26272 optval = rack->rc_labc; 26273 break; 26274 case TCP_HDWR_UP_ONLY: 26275 optval= rack->r_up_only; 26276 break; 26277 case TCP_FILLCW_RATE_CAP: 26278 loptval = rack->r_ctl.fillcw_cap; 26279 break; 26280 case TCP_PACING_RATE_CAP: 26281 loptval = rack->r_ctl.bw_rate_cap; 26282 break; 26283 case TCP_RACK_PROFILE: 26284 /* You cannot retrieve a profile, its write only */ 26285 error = EINVAL; 26286 break; 26287 case TCP_SIDECHAN_DIS: 26288 optval = rack->r_ctl.side_chan_dis_mask; 26289 break; 26290 case TCP_HYBRID_PACING: 26291 /* You cannot retrieve hybrid pacing information, its write only */ 26292 error = EINVAL; 26293 break; 26294 case TCP_USE_CMP_ACKS: 26295 optval = rack->r_use_cmp_ack; 26296 break; 26297 case TCP_RACK_PACE_TO_FILL: 26298 optval = rack->rc_pace_to_cwnd; 26299 break; 26300 case TCP_RACK_NO_PUSH_AT_MAX: 26301 optval = rack->r_ctl.rc_no_push_at_mrtt; 26302 break; 26303 case TCP_SHARED_CWND_ENABLE: 26304 optval = rack->rack_enable_scwnd; 26305 break; 26306 case TCP_RACK_NONRXT_CFG_RATE: 26307 optval = rack->rack_rec_nonrxt_use_cr; 26308 break; 26309 case TCP_NO_PRR: 26310 if (rack->rack_no_prr == 1) 26311 optval = 1; 26312 else if (rack->no_prr_addback == 1) 26313 optval = 2; 26314 else 26315 optval = 0; 26316 break; 26317 case TCP_GP_USE_LTBW: 26318 if (rack->dis_lt_bw) { 26319 /* It is not used */ 26320 optval = 0; 26321 } else if (rack->use_lesser_lt_bw) { 26322 /* we use min() */ 26323 optval = 1; 26324 } else { 26325 /* we use max() */ 26326 optval = 2; 26327 } 26328 break; 26329 case TCP_RACK_DO_DETECTION: 26330 optval = rack->do_detection; 26331 break; 26332 case TCP_RACK_MBUF_QUEUE: 26333 /* Now do we use the LRO mbuf-queue feature */ 26334 optval = rack->r_mbuf_queue; 26335 break; 26336 case RACK_CSPR_IS_FCC: 26337 optval = rack->cspr_is_fcc; 26338 break; 26339 case TCP_TIMELY_DYN_ADJ: 26340 optval = rack->rc_gp_dyn_mul; 26341 break; 26342 case TCP_BBR_IWINTSO: 26343 error = EINVAL; 26344 break; 26345 case TCP_RACK_TLP_REDUCE: 26346 /* RACK TLP cwnd reduction (bool) */ 26347 optval = rack->r_ctl.rc_tlp_cwnd_reduce; 26348 break; 26349 case TCP_BBR_RACK_INIT_RATE: 26350 val = rack->r_ctl.init_rate; 26351 /* convert to kbits per sec */ 26352 val *= 8; 26353 val /= 1000; 26354 optval = (uint32_t)val; 26355 break; 26356 case TCP_RACK_FORCE_MSEG: 26357 optval = rack->rc_force_max_seg; 26358 break; 26359 case TCP_RACK_PACE_MIN_SEG: 26360 optval = rack->r_ctl.rc_user_set_min_segs; 26361 break; 26362 case TCP_RACK_PACE_MAX_SEG: 26363 /* Max segments in a pace */ 26364 optval = rack->rc_user_set_max_segs; 26365 break; 26366 case TCP_RACK_PACE_ALWAYS: 26367 /* Use the always pace method */ 26368 optval = rack->rc_always_pace; 26369 break; 26370 case TCP_RACK_PRR_SENDALOT: 26371 /* Allow PRR to send more than one seg */ 26372 optval = rack->r_ctl.rc_prr_sendalot; 26373 break; 26374 case TCP_RACK_MIN_TO: 26375 /* Minimum time between rack t-o's in ms */ 26376 optval = rack->r_ctl.rc_min_to; 26377 break; 26378 case TCP_RACK_SPLIT_LIMIT: 26379 optval = rack->r_ctl.rc_split_limit; 26380 break; 26381 case TCP_RACK_EARLY_SEG: 26382 /* If early recovery max segments */ 26383 optval = rack->r_ctl.rc_early_recovery_segs; 26384 break; 26385 case TCP_RACK_REORD_THRESH: 26386 /* RACK reorder threshold (shift amount) */ 26387 optval = rack->r_ctl.rc_reorder_shift; 26388 break; 26389 case TCP_SS_EEXIT: 26390 if (rack->r_ctl.gp_rnd_thresh) { 26391 uint32_t v; 26392 26393 v = rack->r_ctl.gp_gain_req; 26394 v <<= 17; 26395 optval = v | (rack->r_ctl.gp_rnd_thresh & 0xff); 26396 if (rack->r_ctl.gate_to_fs == 1) 26397 optval |= 0x10000; 26398 } else 26399 optval = 0; 26400 break; 26401 case TCP_RACK_REORD_FADE: 26402 /* Does reordering fade after ms time */ 26403 optval = rack->r_ctl.rc_reorder_fade; 26404 break; 26405 case TCP_BBR_USE_RACK_RR: 26406 /* Do we use the rack cheat for rxt */ 26407 optval = rack->use_rack_rr; 26408 break; 26409 case TCP_RACK_RR_CONF: 26410 optval = rack->r_rr_config; 26411 break; 26412 case TCP_HDWR_RATE_CAP: 26413 optval = rack->r_rack_hw_rate_caps; 26414 break; 26415 case TCP_BBR_HDWR_PACE: 26416 optval = rack->rack_hdw_pace_ena; 26417 break; 26418 case TCP_RACK_TLP_THRESH: 26419 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 26420 optval = rack->r_ctl.rc_tlp_threshold; 26421 break; 26422 case TCP_RACK_PKT_DELAY: 26423 /* RACK added ms i.e. rack-rtt + reord + N */ 26424 optval = rack->r_ctl.rc_pkt_delay; 26425 break; 26426 case TCP_RACK_TLP_USE: 26427 optval = rack->rack_tlp_threshold_use; 26428 break; 26429 case TCP_PACING_DND: 26430 optval = rack->rc_pace_dnd; 26431 break; 26432 case TCP_RACK_PACE_RATE_CA: 26433 optval = rack->r_ctl.rc_fixed_pacing_rate_ca; 26434 break; 26435 case TCP_RACK_PACE_RATE_SS: 26436 optval = rack->r_ctl.rc_fixed_pacing_rate_ss; 26437 break; 26438 case TCP_RACK_PACE_RATE_REC: 26439 optval = rack->r_ctl.rc_fixed_pacing_rate_rec; 26440 break; 26441 case TCP_DGP_UPPER_BOUNDS: 26442 optval = rack->r_ctl.rack_per_upper_bound_ss; 26443 optval <<= 16; 26444 optval |= rack->r_ctl.rack_per_upper_bound_ca; 26445 break; 26446 case TCP_RACK_GP_INCREASE_SS: 26447 optval = rack->r_ctl.rack_per_of_gp_ca; 26448 break; 26449 case TCP_RACK_GP_INCREASE_CA: 26450 optval = rack->r_ctl.rack_per_of_gp_ss; 26451 break; 26452 case TCP_RACK_PACING_DIVISOR: 26453 optval = rack->r_ctl.pace_len_divisor; 26454 break; 26455 case TCP_BBR_RACK_RTT_USE: 26456 optval = rack->r_ctl.rc_rate_sample_method; 26457 break; 26458 case TCP_DELACK: 26459 optval = tp->t_delayed_ack; 26460 break; 26461 case TCP_DATA_AFTER_CLOSE: 26462 optval = rack->rc_allow_data_af_clo; 26463 break; 26464 case TCP_SHARED_CWND_TIME_LIMIT: 26465 optval = rack->r_limit_scw; 26466 break; 26467 case TCP_HONOR_HPTS_MIN: 26468 if (rack->r_use_hpts_min) 26469 optval = rack->r_ctl.max_reduction; 26470 else 26471 optval = 0; 26472 break; 26473 case TCP_REC_IS_DYN: 26474 optval = rack->rc_gp_no_rec_chg; 26475 break; 26476 case TCP_NO_TIMELY: 26477 optval = rack->rc_skip_timely; 26478 break; 26479 case TCP_RACK_TIMER_SLOP: 26480 optval = rack->r_ctl.timer_slop; 26481 break; 26482 default: 26483 return (tcp_default_ctloutput(tp, sopt)); 26484 break; 26485 } 26486 INP_WUNLOCK(inp); 26487 if (error == 0) { 26488 if ((sopt->sopt_name == TCP_PACING_RATE_CAP) || 26489 (sopt->sopt_name == TCP_FILLCW_RATE_CAP)) 26490 error = sooptcopyout(sopt, &loptval, sizeof loptval); 26491 else 26492 error = sooptcopyout(sopt, &optval, sizeof optval); 26493 } 26494 return (error); 26495 } 26496 26497 static int 26498 rack_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 26499 { 26500 if (sopt->sopt_dir == SOPT_SET) { 26501 return (rack_set_sockopt(tp, sopt)); 26502 } else if (sopt->sopt_dir == SOPT_GET) { 26503 return (rack_get_sockopt(tp, sopt)); 26504 } else { 26505 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 26506 } 26507 } 26508 26509 static const char *rack_stack_names[] = { 26510 __XSTRING(STACKNAME), 26511 #ifdef STACKALIAS 26512 __XSTRING(STACKALIAS), 26513 #endif 26514 }; 26515 26516 static int 26517 rack_ctor(void *mem, int32_t size, void *arg, int32_t how) 26518 { 26519 memset(mem, 0, size); 26520 return (0); 26521 } 26522 26523 static void 26524 rack_dtor(void *mem, int32_t size, void *arg) 26525 { 26526 26527 } 26528 26529 static bool rack_mod_inited = false; 26530 26531 static int 26532 tcp_addrack(module_t mod, int32_t type, void *data) 26533 { 26534 int32_t err = 0; 26535 int num_stacks; 26536 26537 switch (type) { 26538 case MOD_LOAD: 26539 rack_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 26540 sizeof(struct rack_sendmap), 26541 rack_ctor, rack_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 26542 26543 rack_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 26544 sizeof(struct tcp_rack), 26545 rack_ctor, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 26546 26547 sysctl_ctx_init(&rack_sysctl_ctx); 26548 rack_sysctl_root = SYSCTL_ADD_NODE(&rack_sysctl_ctx, 26549 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 26550 OID_AUTO, 26551 #ifdef STACKALIAS 26552 __XSTRING(STACKALIAS), 26553 #else 26554 __XSTRING(STACKNAME), 26555 #endif 26556 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 26557 ""); 26558 if (rack_sysctl_root == NULL) { 26559 printf("Failed to add sysctl node\n"); 26560 err = EFAULT; 26561 goto free_uma; 26562 } 26563 rack_init_sysctls(); 26564 num_stacks = nitems(rack_stack_names); 26565 err = register_tcp_functions_as_names(&__tcp_rack, M_WAITOK, 26566 rack_stack_names, &num_stacks); 26567 if (err) { 26568 printf("Failed to register %s stack name for " 26569 "%s module\n", rack_stack_names[num_stacks], 26570 __XSTRING(MODNAME)); 26571 sysctl_ctx_free(&rack_sysctl_ctx); 26572 free_uma: 26573 uma_zdestroy(rack_zone); 26574 uma_zdestroy(rack_pcb_zone); 26575 rack_counter_destroy(); 26576 printf("Failed to register rack module -- err:%d\n", err); 26577 return (err); 26578 } 26579 tcp_lro_reg_mbufq(); 26580 rack_mod_inited = true; 26581 break; 26582 case MOD_QUIESCE: 26583 err = deregister_tcp_functions(&__tcp_rack, true, false); 26584 break; 26585 case MOD_UNLOAD: 26586 err = deregister_tcp_functions(&__tcp_rack, false, true); 26587 if (err == EBUSY) 26588 break; 26589 if (rack_mod_inited) { 26590 uma_zdestroy(rack_zone); 26591 uma_zdestroy(rack_pcb_zone); 26592 sysctl_ctx_free(&rack_sysctl_ctx); 26593 rack_counter_destroy(); 26594 rack_mod_inited = false; 26595 } 26596 tcp_lro_dereg_mbufq(); 26597 err = 0; 26598 break; 26599 default: 26600 return (EOPNOTSUPP); 26601 } 26602 return (err); 26603 } 26604 26605 static moduledata_t tcp_rack = { 26606 .name = __XSTRING(MODNAME), 26607 .evhand = tcp_addrack, 26608 .priv = 0 26609 }; 26610 26611 MODULE_VERSION(MODNAME, 1); 26612 DECLARE_MODULE(MODNAME, tcp_rack, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 26613 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 26614 26615 #endif /* #if !defined(INET) && !defined(INET6) */ 26616