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 * Author: Randall Stewart <rrs@netflix.com> 28 * This work is based on the ACM Queue paper 29 * BBR - Congestion Based Congestion Control 30 * and also numerous discussions with Neal, Yuchung and Van. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 #include "opt_ipsec.h" 39 #include "opt_ratelimit.h" 40 #include <sys/param.h> 41 #include <sys/arb.h> 42 #include <sys/module.h> 43 #include <sys/kernel.h> 44 #include <sys/libkern.h> 45 #ifdef TCP_HHOOK 46 #include <sys/hhook.h> 47 #endif 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/proc.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 #ifdef STATS 56 #include <sys/qmath.h> 57 #include <sys/tree.h> 58 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 59 #endif 60 #include <sys/refcount.h> 61 #include <sys/queue.h> 62 #include <sys/eventhandler.h> 63 #include <sys/smp.h> 64 #include <sys/kthread.h> 65 #include <sys/lock.h> 66 #include <sys/mutex.h> 67 #include <sys/tim_filter.h> 68 #include <sys/time.h> 69 #include <sys/protosw.h> 70 #include <vm/uma.h> 71 #include <sys/kern_prefetch.h> 72 73 #include <net/route.h> 74 #include <net/route/nhop.h> 75 #include <net/vnet.h> 76 77 #define TCPSTATES /* for logging */ 78 79 #include <netinet/in.h> 80 #include <netinet/in_kdtrace.h> 81 #include <netinet/in_pcb.h> 82 #include <netinet/ip.h> 83 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 84 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 85 #include <netinet/ip_var.h> 86 #include <netinet/ip6.h> 87 #include <netinet6/in6_pcb.h> 88 #include <netinet6/ip6_var.h> 89 #define TCPOUTFLAGS 90 #include <netinet/tcp.h> 91 #include <netinet/tcp_fsm.h> 92 #include <netinet/tcp_seq.h> 93 #include <netinet/tcp_timer.h> 94 #include <netinet/tcp_var.h> 95 #include <netinet/tcpip.h> 96 #include <netinet/tcp_hpts.h> 97 #include <netinet/cc/cc.h> 98 #include <netinet/tcp_log_buf.h> 99 #include <netinet/tcp_ratelimit.h> 100 #include <netinet/tcp_lro.h> 101 #ifdef TCP_OFFLOAD 102 #include <netinet/tcp_offload.h> 103 #endif 104 #ifdef INET6 105 #include <netinet6/tcp6_var.h> 106 #endif 107 #include <netinet/tcp_fastopen.h> 108 109 #include <netipsec/ipsec_support.h> 110 #include <net/if.h> 111 #include <net/if_var.h> 112 #include <net/ethernet.h> 113 114 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 115 #include <netipsec/ipsec.h> 116 #include <netipsec/ipsec6.h> 117 #endif /* IPSEC */ 118 119 #include <netinet/udp.h> 120 #include <netinet/udp_var.h> 121 #include <machine/in_cksum.h> 122 123 #ifdef MAC 124 #include <security/mac/mac_framework.h> 125 #endif 126 127 #include "sack_filter.h" 128 #include "tcp_bbr.h" 129 #include "rack_bbr_common.h" 130 uma_zone_t bbr_zone; 131 uma_zone_t bbr_pcb_zone; 132 133 struct sysctl_ctx_list bbr_sysctl_ctx; 134 struct sysctl_oid *bbr_sysctl_root; 135 136 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \ 137 (tv) = (value); \ 138 if ((u_long)(tv) < (u_long)(tvmin)) \ 139 (tv) = (tvmin); \ 140 if ((u_long)(tv) > (u_long)(tvmax)) \ 141 (tv) = (tvmax); \ 142 } while(0) 143 144 /*#define BBR_INVARIANT 1*/ 145 146 /* 147 * initial window 148 */ 149 static uint32_t bbr_def_init_win = 10; 150 static int32_t bbr_persist_min = 250000; /* 250ms */ 151 static int32_t bbr_persist_max = 1000000; /* 1 Second */ 152 static int32_t bbr_cwnd_may_shrink = 0; 153 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP; 154 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT; 155 static int32_t bbr_hardware_pacing_limit = 8000; 156 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */ 157 static int32_t bbr_no_retran = 0; 158 159 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */ 160 static int32_t bbr_max_net_error_cnt = 10; 161 /* Should the following be dynamic too -- loss wise */ 162 static int32_t bbr_rtt_gain_thresh = 0; 163 /* Measurement controls */ 164 static int32_t bbr_use_google_algo = 1; 165 static int32_t bbr_ts_limiting = 1; 166 static int32_t bbr_ts_can_raise = 0; 167 static int32_t bbr_do_red = 600; 168 static int32_t bbr_red_scale = 20000; 169 static int32_t bbr_red_mul = 1; 170 static int32_t bbr_red_div = 2; 171 static int32_t bbr_red_growth_restrict = 1; 172 static int32_t bbr_target_is_bbunit = 0; 173 static int32_t bbr_drop_limit = 0; 174 /* 175 * How much gain do we need to see to 176 * stay in startup? 177 */ 178 static int32_t bbr_marks_rxt_sack_passed = 0; 179 static int32_t bbr_start_exit = 25; 180 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */ 181 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */ 182 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */ 183 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this 184 * if we go back ever to where the pacer 185 * has priority over timers. 186 */ 187 static int32_t bbr_policer_call_from_rack_to = 0; 188 static int32_t bbr_policer_detection_enabled = 1; 189 static int32_t bbr_min_measurements_req = 1; /* We need at least 2 190 * measurements before we are 191 * "good" note that 2 == 1. 192 * This is because we use a > 193 * comparison. This means if 194 * min_measure was 0, it takes 195 * num-measures > min(0) and 196 * you get 1 measurement and 197 * you are good. Set to 1, you 198 * have to have two 199 * measurements (this is done 200 * to prevent it from being ok 201 * to have no measurements). */ 202 static int32_t bbr_no_pacing_until = 4; 203 204 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */ 205 static int32_t bbr_min_peer_delta = 20; /* 20 units */ 206 static int32_t bbr_delta_percent = 150; /* 15.0 % */ 207 208 static int32_t bbr_target_cwnd_mult_limit = 8; 209 /* 210 * bbr_cwnd_min_val is the number of 211 * segments we hold to in the RTT probe 212 * state typically 4. 213 */ 214 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS; 215 216 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS; 217 218 static int32_t bbr_gain_to_target = 1; 219 static int32_t bbr_gain_gets_extra_too = 1; 220 /* 221 * bbr_high_gain is the 2/ln(2) value we need 222 * to double the sending rate in startup. This 223 * is used for both cwnd and hptsi gain's. 224 */ 225 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1; 226 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1; 227 static int32_t bbr_use_lower_gain_in_startup = 1; 228 229 /* thresholds for reduction on drain in sub-states/drain */ 230 static int32_t bbr_drain_rtt = BBR_SRTT; 231 static int32_t bbr_drain_floor = 88; 232 static int32_t google_allow_early_out = 1; 233 static int32_t google_consider_lost = 1; 234 static int32_t bbr_drain_drop_mul = 4; 235 static int32_t bbr_drain_drop_div = 5; 236 static int32_t bbr_rand_ot = 50; 237 static int32_t bbr_can_force_probertt = 0; 238 static int32_t bbr_can_adjust_probertt = 1; 239 static int32_t bbr_probertt_sets_rtt = 0; 240 static int32_t bbr_can_use_ts_for_rtt = 1; 241 static int32_t bbr_is_ratio = 0; 242 static int32_t bbr_sub_drain_app_limit = 1; 243 static int32_t bbr_prtt_slam_cwnd = 1; 244 static int32_t bbr_sub_drain_slam_cwnd = 1; 245 static int32_t bbr_slam_cwnd_in_main_drain = 1; 246 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter 247 * hold */ 248 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4); 249 /* 250 * bbr_drain_gain is the reverse of the high_gain 251 * designed to drain back out the standing queue 252 * that is formed in startup by causing a larger 253 * hptsi gain and thus drainging the packets 254 * in flight. 255 */ 256 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885; 257 static int32_t bbr_rttprobe_gain = 192; 258 259 /* 260 * The cwnd_gain is the default cwnd gain applied when 261 * calculating a target cwnd. Note that the cwnd is 262 * a secondary factor in the way BBR works (see the 263 * paper and think about it, it will take some time). 264 * Basically the hptsi_gain spreads the packets out 265 * so you never get more than BDP to the peer even 266 * if the cwnd is high. In our implemenation that 267 * means in non-recovery/retransmission scenarios 268 * cwnd will never be reached by the flight-size. 269 */ 270 static int32_t bbr_cwnd_gain = BBR_UNIT * 2; 271 static int32_t bbr_tlp_type_to_use = BBR_SRTT; 272 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */ 273 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */ 274 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */ 275 static int32_t bbr_ignore_data_after_close = 1; 276 static int16_t bbr_hptsi_gain[] = { 277 (BBR_UNIT *5 / 4), 278 (BBR_UNIT * 3 / 4), 279 BBR_UNIT, 280 BBR_UNIT, 281 BBR_UNIT, 282 BBR_UNIT, 283 BBR_UNIT, 284 BBR_UNIT 285 }; 286 int32_t bbr_use_rack_resend_cheat = 1; 287 int32_t bbr_sends_full_iwnd = 1; 288 289 #define BBR_HPTSI_GAIN_MAX 8 290 /* 291 * The BBR module incorporates a number of 292 * TCP ideas that have been put out into the IETF 293 * over the last few years: 294 * - Yuchung Cheng's RACK TCP (for which its named) that 295 * will stop us using the number of dup acks and instead 296 * use time as the gage of when we retransmit. 297 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 298 * of Dukkipati et.al. 299 * - Van Jacobson's et.al BBR. 300 * 301 * RACK depends on SACK, so if an endpoint arrives that 302 * cannot do SACK the state machine below will shuttle the 303 * connection back to using the "default" TCP stack that is 304 * in FreeBSD. 305 * 306 * To implement BBR and RACK the original TCP stack was first decomposed 307 * into a functional state machine with individual states 308 * for each of the possible TCP connection states. The do_segment 309 * functions role in life is to mandate the connection supports SACK 310 * initially and then assure that the RACK state matches the conenction 311 * state before calling the states do_segment function. Data processing 312 * of inbound segments also now happens in the hpts_do_segment in general 313 * with only one exception. This is so we can keep the connection on 314 * a single CPU. 315 * 316 * Each state is simplified due to the fact that the original do_segment 317 * has been decomposed and we *know* what state we are in (no 318 * switches on the state) and all tests for SACK are gone. This 319 * greatly simplifies what each state does. 320 * 321 * TCP output is also over-written with a new version since it 322 * must maintain the new rack scoreboard and has had hptsi 323 * integrated as a requirment. Still todo is to eliminate the 324 * use of the callout_() system and use the hpts for all 325 * timers as well. 326 */ 327 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */ 328 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */ 329 static const int32_t bbr_min_req_free = 2; /* The min we must have on the 330 * free list */ 331 static int32_t bbr_tlp_thresh = 1; 332 static int32_t bbr_reorder_thresh = 2; 333 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def 334 * 60,000,000 - 60 seconds */ 335 static int32_t bbr_pkt_delay = 1000; 336 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */ 337 static int32_t bbr_incr_timers = 1; 338 339 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */ 340 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */ 341 static int32_t bbr_exit_startup_at_loss = 1; 342 343 /* 344 * bbr_lt_bw_ratio is 1/8th 345 * bbr_lt_bw_diff is < 4 Kbit/sec 346 */ 347 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */ 348 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */ 349 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use 350 * the lt_bw for */ 351 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure 352 * lt_bw */ 353 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */ 354 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */ 355 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */ 356 357 static int32_t bbr_verbose_logging = 0; 358 /* 359 * Currently regular tcp has a rto_min of 30ms 360 * the backoff goes 12 times so that ends up 361 * being a total of 122.850 seconds before a 362 * connection is killed. 363 */ 364 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */ 365 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */ 366 367 /****************************************************/ 368 /* DEFAULT TSO SIZING (cpu performance impacting) */ 369 /****************************************************/ 370 /* What amount is our formula using to get TSO size */ 371 static int32_t bbr_hptsi_per_second = 1000; 372 373 /* 374 * For hptsi under bbr_cross_over connections what is delay 375 * target 7ms (in usec) combined with a seg_max of 2 376 * gets us close to identical google behavior in 377 * TSO size selection (possibly more 1MSS sends). 378 */ 379 static int32_t bbr_hptsi_segments_delay_tar = 7000; 380 381 /* Does pacing delay include overhead's in its time calculations? */ 382 static int32_t bbr_include_enet_oh = 0; 383 static int32_t bbr_include_ip_oh = 1; 384 static int32_t bbr_include_tcp_oh = 1; 385 static int32_t bbr_google_discount = 10; 386 387 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */ 388 static int32_t bbr_state_is_pkt_epoch = 0; 389 static int32_t bbr_state_drain_2_tar = 1; 390 /* What is the max the 0 - bbr_cross_over MBPS TSO target 391 * can reach using our delay target. Note that this 392 * value becomes the floor for the cross over 393 * algorithm. 394 */ 395 static int32_t bbr_hptsi_segments_max = 2; 396 static int32_t bbr_hptsi_segments_floor = 1; 397 static int32_t bbr_hptsi_utter_max = 0; 398 399 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */ 400 static int32_t bbr_hptsi_bytes_min = 1460; 401 static int32_t bbr_all_get_min = 0; 402 403 /* Cross over point from algo-a to algo-b */ 404 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS; 405 406 /* Do we deal with our restart state? */ 407 static int32_t bbr_uses_idle_restart = 0; 408 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */ 409 410 /* Do we allow hardware pacing? */ 411 static int32_t bbr_allow_hdwr_pacing = 0; 412 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */ 413 static int32_t bbr_hdwr_pace_floor = 1; 414 static int32_t bbr_hdwr_pacing_delay_cnt = 10; 415 416 /****************************************************/ 417 static int32_t bbr_resends_use_tso = 0; 418 static int32_t bbr_tlp_max_resend = 2; 419 static int32_t bbr_sack_block_limit = 128; 420 421 #define BBR_MAX_STAT 19 422 counter_u64_t bbr_state_time[BBR_MAX_STAT]; 423 counter_u64_t bbr_state_lost[BBR_MAX_STAT]; 424 counter_u64_t bbr_state_resend[BBR_MAX_STAT]; 425 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE]; 426 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE]; 427 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE]; 428 counter_u64_t bbr_flows_whdwr_pacing; 429 counter_u64_t bbr_flows_nohdwr_pacing; 430 431 counter_u64_t bbr_nohdwr_pacing_enobuf; 432 counter_u64_t bbr_hdwr_pacing_enobuf; 433 434 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr); 435 436 /* 437 * Static defintions we need for forward declarations. 438 */ 439 static uint32_t 440 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, 441 uint32_t useconds_time, uint64_t bw); 442 static uint32_t 443 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain); 444 static void 445 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win); 446 static void 447 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses); 448 static void 449 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line, 450 int dolog); 451 static uint32_t 452 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain); 453 static void 454 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, 455 int32_t pkt_epoch, uint32_t losses); 456 static uint32_t 457 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, 458 struct bbr_sendmap *rsm); 459 static uint32_t 460 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp); 461 static uint32_t 462 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 463 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts); 464 static void 465 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 466 int32_t line); 467 static void 468 bbr_set_state_target(struct tcp_bbr *bbr, int line); 469 static void 470 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line); 471 static void 472 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, 473 int event, int line); 474 static void 475 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts); 476 static void 477 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts); 478 static void 479 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 480 uint32_t rtt, uint32_t line, uint8_t is_start, 481 uint16_t set); 482 static struct bbr_sendmap * 483 bbr_find_lowest_rsm(struct tcp_bbr *bbr); 484 static __inline uint32_t 485 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type); 486 static void 487 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, 488 uint8_t which); 489 static void 490 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, 491 uint32_t time_since_sent, uint32_t srtt, 492 uint32_t thresh, uint32_t to); 493 static void 494 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag); 495 static void 496 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, 497 uint32_t del_by, uint32_t cts, uint32_t sloton, 498 uint32_t prev_delay); 499 static void 500 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 501 int32_t line); 502 static void 503 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr); 504 static void 505 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts); 506 static void 507 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts); 508 static void 509 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts); 510 static void 511 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 512 uint32_t cts, uint32_t usecs, uint64_t bw, 513 uint32_t override, int mod); 514 static int 515 bbr_ctloutput(struct inpcb *inp, struct sockopt *sopt); 516 517 static inline uint8_t 518 bbr_state_val(struct tcp_bbr *bbr) 519 { 520 return(bbr->rc_bbr_substate); 521 } 522 523 static inline uint32_t 524 get_min_cwnd(struct tcp_bbr *bbr) 525 { 526 int mss; 527 528 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 529 bbr->r_ctl.rc_pace_max_segs); 530 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED) 531 return (bbr_cwnd_min_val_hs * mss); 532 else 533 return (bbr_cwnd_min_val * mss); 534 } 535 536 static uint32_t 537 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr) 538 { 539 uint64_t srtt, var; 540 uint64_t ret_val; 541 542 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 543 if (tp->t_srtt == 0) { 544 srtt = (uint64_t)BBR_INITIAL_RTO; 545 var = 0; 546 } else { 547 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 548 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT); 549 } 550 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]), 551 bbr_persist_min, bbr_persist_max); 552 return ((uint32_t)ret_val); 553 } 554 555 static uint32_t 556 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 557 { 558 /* 559 * Start the FR timer, we do this based on getting the first one in 560 * the rc_tmap. Note that if its NULL we must stop the timer. in all 561 * events we need to stop the running timer (if its running) before 562 * starting the new one. 563 */ 564 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 565 int32_t idx; 566 int32_t is_tlp_timer = 0; 567 struct bbr_sendmap *rsm; 568 569 if (bbr->rc_all_timers_stopped) { 570 /* All timers have been stopped none are to run */ 571 return (0); 572 } 573 if (bbr->rc_in_persist) { 574 /* We can't start any timer in persists */ 575 return (bbr_get_persists_timer_val(tp, bbr)); 576 } 577 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 578 if ((rsm == NULL) || 579 ((tp->t_flags & TF_SACK_PERMIT) == 0) || 580 (tp->t_state < TCPS_ESTABLISHED)) { 581 /* Nothing on the send map */ 582 activate_rxt: 583 if (SEQ_LT(tp->snd_una, tp->snd_max) || 584 sbavail(&tptosocket(tp)->so_snd)) { 585 uint64_t tov; 586 587 time_since_sent = 0; 588 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 589 if (rsm) { 590 idx = rsm->r_rtr_cnt - 1; 591 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 592 tstmp_touse = rsm->r_tim_lastsent[idx]; 593 else 594 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 595 if (TSTMP_GT(tstmp_touse, cts)) 596 time_since_sent = cts - tstmp_touse; 597 } 598 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 599 if (tp->t_srtt == 0) 600 tov = BBR_INITIAL_RTO; 601 else 602 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) + 603 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT); 604 if (tp->t_rxtshift) 605 tov *= tcp_backoff[tp->t_rxtshift]; 606 if (tov > time_since_sent) 607 tov -= time_since_sent; 608 else 609 tov = bbr->r_ctl.rc_min_to; 610 TCPT_RANGESET_NOSLOP(to, tov, 611 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC), 612 (bbr->rc_max_rto_sec * USECS_IN_SECOND)); 613 bbr_log_timer_var(bbr, 2, cts, 0, srtt, 0, to); 614 return (to); 615 } 616 return (0); 617 } 618 if (rsm->r_flags & BBR_ACKED) { 619 rsm = bbr_find_lowest_rsm(bbr); 620 if (rsm == NULL) { 621 /* No lowest? */ 622 goto activate_rxt; 623 } 624 } 625 /* Convert from ms to usecs */ 626 if (rsm->r_flags & BBR_SACK_PASSED) { 627 if ((tp->t_flags & TF_SENTFIN) && 628 ((tp->snd_max - tp->snd_una) == 1) && 629 (rsm->r_flags & BBR_HAS_FIN)) { 630 /* 631 * We don't start a bbr rack timer if all we have is 632 * a FIN outstanding. 633 */ 634 goto activate_rxt; 635 } 636 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK); 637 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm); 638 idx = rsm->r_rtr_cnt - 1; 639 exp = rsm->r_tim_lastsent[idx] + thresh; 640 if (SEQ_GEQ(exp, cts)) { 641 to = exp - cts; 642 if (to < bbr->r_ctl.rc_min_to) { 643 to = bbr->r_ctl.rc_min_to; 644 } 645 } else { 646 to = bbr->r_ctl.rc_min_to; 647 } 648 } else { 649 /* Ok we need to do a TLP not RACK */ 650 if (bbr->rc_tlp_in_progress != 0) { 651 /* 652 * The previous send was a TLP. 653 */ 654 goto activate_rxt; 655 } 656 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 657 if (rsm == NULL) { 658 /* We found no rsm to TLP with. */ 659 goto activate_rxt; 660 } 661 if (rsm->r_flags & BBR_HAS_FIN) { 662 /* If its a FIN we don't do TLP */ 663 rsm = NULL; 664 goto activate_rxt; 665 } 666 time_since_sent = 0; 667 idx = rsm->r_rtr_cnt - 1; 668 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 669 tstmp_touse = rsm->r_tim_lastsent[idx]; 670 else 671 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 672 if (TSTMP_GT(tstmp_touse, cts)) 673 time_since_sent = cts - tstmp_touse; 674 is_tlp_timer = 1; 675 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use); 676 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts); 677 if (thresh > time_since_sent) 678 to = thresh - time_since_sent; 679 else 680 to = bbr->r_ctl.rc_min_to; 681 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 682 /* 683 * If the TLP time works out to larger than the max 684 * RTO lets not do TLP.. just RTO. 685 */ 686 goto activate_rxt; 687 } 688 if ((bbr->rc_tlp_rtx_out == 1) && 689 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) { 690 /* 691 * Second retransmit of the same TLP 692 * lets not. 693 */ 694 bbr->rc_tlp_rtx_out = 0; 695 goto activate_rxt; 696 } 697 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) { 698 /* 699 * The tail is no longer the last one I did a probe 700 * on 701 */ 702 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 703 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 704 } 705 } 706 if (is_tlp_timer == 0) { 707 BBR_STAT_INC(bbr_to_arm_rack); 708 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 709 } else { 710 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to); 711 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 712 /* 713 * We have exceeded how many times we can retran the 714 * current TLP timer, switch to the RTO timer. 715 */ 716 goto activate_rxt; 717 } else { 718 BBR_STAT_INC(bbr_to_arm_tlp); 719 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 720 } 721 } 722 return (to); 723 } 724 725 static inline int32_t 726 bbr_minseg(struct tcp_bbr *bbr) 727 { 728 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options); 729 } 730 731 static void 732 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len) 733 { 734 struct inpcb *inp = tptoinpcb(tp); 735 struct hpts_diag diag; 736 uint32_t delayed_ack = 0; 737 uint32_t left = 0; 738 uint32_t hpts_timeout; 739 uint8_t stopped; 740 int32_t delay_calc = 0; 741 uint32_t prev_delay = 0; 742 743 if (tcp_in_hpts(inp)) { 744 /* A previous call is already set up */ 745 return; 746 } 747 if ((tp->t_state == TCPS_CLOSED) || 748 (tp->t_state == TCPS_LISTEN)) { 749 return; 750 } 751 stopped = bbr->rc_tmr_stopped; 752 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 753 left = bbr->r_ctl.rc_timer_exp - cts; 754 } 755 bbr->r_ctl.rc_hpts_flags = 0; 756 bbr->r_ctl.rc_timer_exp = 0; 757 prev_delay = bbr->r_ctl.rc_last_delay_val; 758 if (bbr->r_ctl.rc_last_delay_val && 759 (slot == 0)) { 760 /* 761 * If a previous pacer delay was in place we 762 * are not coming from the output side (where 763 * we calculate a delay, more likely a timer). 764 */ 765 slot = bbr->r_ctl.rc_last_delay_val; 766 if (TSTMP_GT(cts, bbr->rc_pacer_started)) { 767 /* Compensate for time passed */ 768 delay_calc = cts - bbr->rc_pacer_started; 769 if (delay_calc <= slot) 770 slot -= delay_calc; 771 } 772 } 773 /* Do we have early to make up for by pushing out the pacing time? */ 774 if (bbr->r_agg_early_set) { 775 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2); 776 slot += bbr->r_ctl.rc_agg_early; 777 bbr->r_ctl.rc_agg_early = 0; 778 bbr->r_agg_early_set = 0; 779 } 780 /* Are we running a total debt that needs to be compensated for? */ 781 if (bbr->r_ctl.rc_hptsi_agg_delay) { 782 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) { 783 /* We nuke the delay */ 784 slot -= bbr->r_ctl.rc_hptsi_agg_delay; 785 bbr->r_ctl.rc_hptsi_agg_delay = 0; 786 } else { 787 /* We nuke some of the delay, put in a minimal 100usecs */ 788 bbr->r_ctl.rc_hptsi_agg_delay -= slot; 789 bbr->r_ctl.rc_last_delay_val = slot = 100; 790 } 791 } 792 bbr->r_ctl.rc_last_delay_val = slot; 793 hpts_timeout = bbr_timer_start(tp, bbr, cts); 794 if (tp->t_flags & TF_DELACK) { 795 if (bbr->rc_in_persist == 0) { 796 delayed_ack = bbr_delack_time; 797 } else { 798 /* 799 * We are in persists and have 800 * gotten a new data element. 801 */ 802 if (hpts_timeout > bbr_delack_time) { 803 /* 804 * Lets make the persists timer (which acks) 805 * be the smaller of hpts_timeout and bbr_delack_time. 806 */ 807 hpts_timeout = bbr_delack_time; 808 } 809 } 810 } 811 if (delayed_ack && 812 ((hpts_timeout == 0) || 813 (delayed_ack < hpts_timeout))) { 814 /* We need a Delayed ack timer */ 815 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 816 hpts_timeout = delayed_ack; 817 } 818 if (slot) { 819 /* Mark that we have a pacing timer up */ 820 BBR_STAT_INC(bbr_paced_segments); 821 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 822 } 823 /* 824 * If no timers are going to run and we will fall off thfe hptsi 825 * wheel, we resort to a keep-alive timer if its configured. 826 */ 827 if ((hpts_timeout == 0) && 828 (slot == 0)) { 829 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 830 (tp->t_state <= TCPS_CLOSING)) { 831 /* 832 * Ok we have no timer (persists, rack, tlp, rxt or 833 * del-ack), we don't have segments being paced. So 834 * all that is left is the keepalive timer. 835 */ 836 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 837 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 838 } else { 839 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 840 } 841 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 842 } 843 } 844 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 845 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 846 /* 847 * RACK, TLP, persists and RXT timers all are restartable 848 * based on actions input .. i.e we received a packet (ack 849 * or sack) and that changes things (rw, or snd_una etc). 850 * Thus we can restart them with a new value. For 851 * keep-alive, delayed_ack we keep track of what was left 852 * and restart the timer with a smaller value. 853 */ 854 if (left < hpts_timeout) 855 hpts_timeout = left; 856 } 857 if (bbr->r_ctl.rc_incr_tmrs && slot && 858 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 859 /* 860 * If configured to do so, and the timer is either 861 * the TLP or RXT timer, we need to increase the timeout 862 * by the pacing time. Consider the bottleneck at my 863 * machine as an example, we are sending something 864 * to start a TLP on. The last packet won't be emitted 865 * fully until the pacing time (the bottleneck will hold 866 * the data in place). Once the packet is emitted that 867 * is when we want to start waiting for the TLP. This 868 * is most evident with hardware pacing (where the nic 869 * is holding the packet(s) before emitting). But it 870 * can also show up in the network so we do it for all 871 * cases. Technically we would take off one packet from 872 * this extra delay but this is easier and being more 873 * conservative is probably better. 874 */ 875 hpts_timeout += slot; 876 } 877 if (hpts_timeout) { 878 /* 879 * Hack alert for now we can't time-out over 2147 seconds (a 880 * bit more than 35min) 881 */ 882 if (hpts_timeout > 0x7ffffffe) 883 hpts_timeout = 0x7ffffffe; 884 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 885 } else 886 bbr->r_ctl.rc_timer_exp = 0; 887 if ((slot) && 888 (bbr->rc_use_google || 889 bbr->output_error_seen || 890 (slot <= hpts_timeout)) ) { 891 /* 892 * Tell LRO that it can queue packets while 893 * we pace. 894 */ 895 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY; 896 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 897 (bbr->rc_cwnd_limited == 0)) { 898 /* 899 * If we are not cwnd limited and we 900 * are running a rack timer we put on 901 * the do not disturbe even for sack. 902 */ 903 inp->inp_flags2 |= INP_DONT_SACK_QUEUE; 904 } else 905 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE; 906 bbr->rc_pacer_started = cts; 907 908 (void)tcp_hpts_insert_diag(inp, HPTS_USEC_TO_SLOTS(slot), 909 __LINE__, &diag); 910 bbr->rc_timer_first = 0; 911 bbr->bbr_timer_src = frm; 912 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1); 913 bbr_log_hpts_diag(bbr, cts, &diag); 914 } else if (hpts_timeout) { 915 (void)tcp_hpts_insert_diag(inp, HPTS_USEC_TO_SLOTS(hpts_timeout), 916 __LINE__, &diag); 917 /* 918 * We add the flag here as well if the slot is set, 919 * since hpts will call in to clear the queue first before 920 * calling the output routine (which does our timers). 921 * We don't want to set the flag if its just a timer 922 * else the arrival of data might (that causes us 923 * to send more) might get delayed. Imagine being 924 * on a keep-alive timer and a request comes in for 925 * more data. 926 */ 927 if (slot) 928 bbr->rc_pacer_started = cts; 929 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 930 (bbr->rc_cwnd_limited == 0)) { 931 /* 932 * For a rack timer, don't wake us even 933 * if a sack arrives as long as we are 934 * not cwnd limited. 935 */ 936 bbr->rc_inp->inp_flags2 |= INP_MBUF_QUEUE_READY; 937 inp->inp_flags2 |= INP_DONT_SACK_QUEUE; 938 } else { 939 /* All other timers wake us up */ 940 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY; 941 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE; 942 } 943 bbr->bbr_timer_src = frm; 944 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0); 945 bbr_log_hpts_diag(bbr, cts, &diag); 946 bbr->rc_timer_first = 1; 947 } 948 bbr->rc_tmr_stopped = 0; 949 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay); 950 } 951 952 static void 953 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb) 954 { 955 /* 956 * We received an ack, and then did not call send or were bounced 957 * out due to the hpts was running. Now a timer is up as well, is it 958 * the right timer? 959 */ 960 struct inpcb *inp; 961 struct bbr_sendmap *rsm; 962 uint32_t hpts_timeout; 963 int tmr_up; 964 965 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 966 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 967 return; 968 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 969 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 970 (tmr_up == PACE_TMR_RXT)) { 971 /* Should be an RXT */ 972 return; 973 } 974 inp = bbr->rc_inp; 975 if (rsm == NULL) { 976 /* Nothing outstanding? */ 977 if (tp->t_flags & TF_DELACK) { 978 if (tmr_up == PACE_TMR_DELACK) 979 /* 980 * We are supposed to have delayed ack up 981 * and we do 982 */ 983 return; 984 } else if (sbavail(&inp->inp_socket->so_snd) && 985 (tmr_up == PACE_TMR_RXT)) { 986 /* 987 * if we hit enobufs then we would expect the 988 * possibility of nothing outstanding and the RXT up 989 * (and the hptsi timer). 990 */ 991 return; 992 } else if (((V_tcp_always_keepalive || 993 inp->inp_socket->so_options & SO_KEEPALIVE) && 994 (tp->t_state <= TCPS_CLOSING)) && 995 (tmr_up == PACE_TMR_KEEP) && 996 (tp->snd_max == tp->snd_una)) { 997 /* We should have keep alive up and we do */ 998 return; 999 } 1000 } 1001 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) { 1002 if ((tp->t_flags & TF_SENTFIN) && 1003 ((tp->snd_max - tp->snd_una) == 1) && 1004 (rsm->r_flags & BBR_HAS_FIN)) { 1005 /* needs to be a RXT */ 1006 if (tmr_up == PACE_TMR_RXT) 1007 return; 1008 else 1009 goto wrong_timer; 1010 } else if (tmr_up == PACE_TMR_RACK) 1011 return; 1012 else 1013 goto wrong_timer; 1014 } else if (rsm && (tmr_up == PACE_TMR_RACK)) { 1015 /* Rack timer has priority if we have data out */ 1016 return; 1017 } else if (SEQ_GT(tp->snd_max, tp->snd_una) && 1018 ((tmr_up == PACE_TMR_TLP) || 1019 (tmr_up == PACE_TMR_RXT))) { 1020 /* 1021 * Either a TLP or RXT is fine if no sack-passed is in place 1022 * and data is outstanding. 1023 */ 1024 return; 1025 } else if (tmr_up == PACE_TMR_DELACK) { 1026 /* 1027 * If the delayed ack was going to go off before the 1028 * rtx/tlp/rack timer were going to expire, then that would 1029 * be the timer in control. Note we don't check the time 1030 * here trusting the code is correct. 1031 */ 1032 return; 1033 } 1034 if (SEQ_GT(tp->snd_max, tp->snd_una) && 1035 ((tmr_up == PACE_TMR_RXT) || 1036 (tmr_up == PACE_TMR_TLP) || 1037 (tmr_up == PACE_TMR_RACK))) { 1038 /* 1039 * We have outstanding data and 1040 * we *do* have a RACK, TLP or RXT 1041 * timer running. We won't restart 1042 * anything here since thats probably ok we 1043 * will get called with some timer here shortly. 1044 */ 1045 return; 1046 } 1047 /* 1048 * Ok the timer originally started is not what we want now. We will 1049 * force the hpts to be stopped if any, and restart with the slot 1050 * set to what was in the saved slot. 1051 */ 1052 wrong_timer: 1053 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) { 1054 if (tcp_in_hpts(inp)) 1055 tcp_hpts_remove(inp); 1056 bbr_timer_cancel(bbr, __LINE__, cts); 1057 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val, 1058 0); 1059 } else { 1060 /* 1061 * Output is hptsi so we just need to switch the type of 1062 * timer. We don't bother with keep-alive, since when we 1063 * jump through the output, it will start the keep-alive if 1064 * nothing is sent. 1065 * 1066 * We only need a delayed-ack added and or the hpts_timeout. 1067 */ 1068 hpts_timeout = bbr_timer_start(tp, bbr, cts); 1069 if (tp->t_flags & TF_DELACK) { 1070 if (hpts_timeout == 0) { 1071 hpts_timeout = bbr_delack_time; 1072 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1073 } 1074 else if (hpts_timeout > bbr_delack_time) { 1075 hpts_timeout = bbr_delack_time; 1076 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1077 } 1078 } 1079 if (hpts_timeout) { 1080 if (hpts_timeout > 0x7ffffffe) 1081 hpts_timeout = 0x7ffffffe; 1082 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 1083 } 1084 } 1085 } 1086 1087 int32_t bbr_clear_lost = 0; 1088 1089 /* 1090 * Considers the two time values now (cts) and earlier. 1091 * If cts is smaller than earlier, we could have 1092 * had a sequence wrap (our counter wraps every 1093 * 70 min or so) or it could be just clock skew 1094 * getting us two different time values. Clock skew 1095 * will show up within 10ms or so. So in such 1096 * a case (where cts is behind earlier time by 1097 * less than 10ms) we return 0. Otherwise we 1098 * return the true difference between them. 1099 */ 1100 static inline uint32_t 1101 bbr_calc_time(uint32_t cts, uint32_t earlier_time) { 1102 /* 1103 * Given two timestamps, the current time stamp cts, and some other 1104 * time-stamp taken in theory earlier return the difference. The 1105 * trick is here sometimes locking will get the other timestamp 1106 * after the cts. If this occurs we need to return 0. 1107 */ 1108 if (TSTMP_GEQ(cts, earlier_time)) 1109 return (cts - earlier_time); 1110 /* 1111 * cts is behind earlier_time if its less than 10ms consider it 0. 1112 * If its more than 10ms difference then we had a time wrap. Else 1113 * its just the normal locking foo. I wonder if we should not go to 1114 * 64bit TS and get rid of this issue. 1115 */ 1116 if (TSTMP_GEQ((cts + 10000), earlier_time)) 1117 return (0); 1118 /* 1119 * Ok the time must have wrapped. So we need to answer a large 1120 * amount of time, which the normal subtraction should do. 1121 */ 1122 return (cts - earlier_time); 1123 } 1124 1125 static int 1126 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS) 1127 { 1128 uint32_t stat; 1129 int32_t error; 1130 1131 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t)); 1132 if (error || req->newptr == NULL) 1133 return error; 1134 1135 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 1136 if (error) 1137 return (error); 1138 if (stat == 1) { 1139 #ifdef BBR_INVARIANTS 1140 printf("Clearing BBR lost counters\n"); 1141 #endif 1142 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT); 1143 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT); 1144 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT); 1145 } else if (stat == 2) { 1146 #ifdef BBR_INVARIANTS 1147 printf("Clearing BBR option counters\n"); 1148 #endif 1149 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE); 1150 } else if (stat == 3) { 1151 #ifdef BBR_INVARIANTS 1152 printf("Clearing BBR stats counters\n"); 1153 #endif 1154 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE); 1155 } else if (stat == 4) { 1156 #ifdef BBR_INVARIANTS 1157 printf("Clearing BBR out-size counters\n"); 1158 #endif 1159 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE); 1160 } 1161 bbr_clear_lost = 0; 1162 return (0); 1163 } 1164 1165 static void 1166 bbr_init_sysctls(void) 1167 { 1168 struct sysctl_oid *bbr_probertt; 1169 struct sysctl_oid *bbr_hptsi; 1170 struct sysctl_oid *bbr_measure; 1171 struct sysctl_oid *bbr_cwnd; 1172 struct sysctl_oid *bbr_timeout; 1173 struct sysctl_oid *bbr_states; 1174 struct sysctl_oid *bbr_startup; 1175 struct sysctl_oid *bbr_policer; 1176 1177 /* Probe rtt controls */ 1178 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1179 SYSCTL_CHILDREN(bbr_sysctl_root), 1180 OID_AUTO, 1181 "probertt", 1182 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1183 ""); 1184 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1185 SYSCTL_CHILDREN(bbr_probertt), 1186 OID_AUTO, "gain", CTLFLAG_RW, 1187 &bbr_rttprobe_gain, 192, 1188 "What is the filter gain drop in probe_rtt (0=disable)?"); 1189 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1190 SYSCTL_CHILDREN(bbr_probertt), 1191 OID_AUTO, "cwnd", CTLFLAG_RW, 1192 &bbr_rtt_probe_cwndtarg, 4, 1193 "How many mss's are outstanding during probe-rtt"); 1194 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1195 SYSCTL_CHILDREN(bbr_probertt), 1196 OID_AUTO, "int", CTLFLAG_RW, 1197 &bbr_rtt_probe_limit, 4000000, 1198 "If RTT has not shrank in this many micro-seconds enter probe-rtt"); 1199 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1200 SYSCTL_CHILDREN(bbr_probertt), 1201 OID_AUTO, "mintime", CTLFLAG_RW, 1202 &bbr_rtt_probe_time, 200000, 1203 "How many microseconds in probe-rtt"); 1204 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1205 SYSCTL_CHILDREN(bbr_probertt), 1206 OID_AUTO, "filter_len_sec", CTLFLAG_RW, 1207 &bbr_filter_len_sec, 6, 1208 "How long in seconds does the rttProp filter run?"); 1209 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1210 SYSCTL_CHILDREN(bbr_probertt), 1211 OID_AUTO, "drain_rtt", CTLFLAG_RW, 1212 &bbr_drain_rtt, BBR_SRTT, 1213 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?"); 1214 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1215 SYSCTL_CHILDREN(bbr_probertt), 1216 OID_AUTO, "can_force", CTLFLAG_RW, 1217 &bbr_can_force_probertt, 0, 1218 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??"); 1219 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1220 SYSCTL_CHILDREN(bbr_probertt), 1221 OID_AUTO, "enter_sets_force", CTLFLAG_RW, 1222 &bbr_probertt_sets_rtt, 0, 1223 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?"); 1224 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1225 SYSCTL_CHILDREN(bbr_probertt), 1226 OID_AUTO, "can_adjust", CTLFLAG_RW, 1227 &bbr_can_adjust_probertt, 1, 1228 "Can we dynamically adjust the probe-rtt limits and times?"); 1229 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1230 SYSCTL_CHILDREN(bbr_probertt), 1231 OID_AUTO, "is_ratio", CTLFLAG_RW, 1232 &bbr_is_ratio, 0, 1233 "is the limit to filter a ratio?"); 1234 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1235 SYSCTL_CHILDREN(bbr_probertt), 1236 OID_AUTO, "use_cwnd", CTLFLAG_RW, 1237 &bbr_prtt_slam_cwnd, 0, 1238 "Should we set/recover cwnd?"); 1239 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1240 SYSCTL_CHILDREN(bbr_probertt), 1241 OID_AUTO, "can_use_ts", CTLFLAG_RW, 1242 &bbr_can_use_ts_for_rtt, 1, 1243 "Can we use the ms timestamp if available for retransmistted rtt calculations?"); 1244 1245 /* Pacing controls */ 1246 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1247 SYSCTL_CHILDREN(bbr_sysctl_root), 1248 OID_AUTO, 1249 "pacing", 1250 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1251 ""); 1252 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1253 SYSCTL_CHILDREN(bbr_hptsi), 1254 OID_AUTO, "hw_pacing", CTLFLAG_RW, 1255 &bbr_allow_hdwr_pacing, 1, 1256 "Do we allow hardware pacing?"); 1257 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1258 SYSCTL_CHILDREN(bbr_hptsi), 1259 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW, 1260 &bbr_hardware_pacing_limit, 4000, 1261 "Do we have a limited number of connections for pacing chelsio (0=no limit)?"); 1262 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1263 SYSCTL_CHILDREN(bbr_hptsi), 1264 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW, 1265 &bbr_hdwr_pace_adjust, 2, 1266 "Multiplier to calculated tso size?"); 1267 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1268 SYSCTL_CHILDREN(bbr_hptsi), 1269 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW, 1270 &bbr_hdwr_pace_floor, 1, 1271 "Do we invoke the hardware pacing floor?"); 1272 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1273 SYSCTL_CHILDREN(bbr_hptsi), 1274 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW, 1275 &bbr_hdwr_pacing_delay_cnt, 10, 1276 "How many packets must be sent after hdwr pacing is enabled"); 1277 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1278 SYSCTL_CHILDREN(bbr_hptsi), 1279 OID_AUTO, "bw_cross", CTLFLAG_RW, 1280 &bbr_cross_over, 3000000, 1281 "What is the point where we cross over to linux like TSO size set"); 1282 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1283 SYSCTL_CHILDREN(bbr_hptsi), 1284 OID_AUTO, "seg_deltarg", CTLFLAG_RW, 1285 &bbr_hptsi_segments_delay_tar, 7000, 1286 "What is the worse case delay target for hptsi < 48Mbp connections"); 1287 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1288 SYSCTL_CHILDREN(bbr_hptsi), 1289 OID_AUTO, "enet_oh", CTLFLAG_RW, 1290 &bbr_include_enet_oh, 0, 1291 "Do we include the ethernet overhead in calculating pacing delay?"); 1292 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1293 SYSCTL_CHILDREN(bbr_hptsi), 1294 OID_AUTO, "ip_oh", CTLFLAG_RW, 1295 &bbr_include_ip_oh, 1, 1296 "Do we include the IP overhead in calculating pacing delay?"); 1297 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1298 SYSCTL_CHILDREN(bbr_hptsi), 1299 OID_AUTO, "tcp_oh", CTLFLAG_RW, 1300 &bbr_include_tcp_oh, 0, 1301 "Do we include the TCP overhead in calculating pacing delay?"); 1302 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1303 SYSCTL_CHILDREN(bbr_hptsi), 1304 OID_AUTO, "google_discount", CTLFLAG_RW, 1305 &bbr_google_discount, 10, 1306 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?"); 1307 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1308 SYSCTL_CHILDREN(bbr_hptsi), 1309 OID_AUTO, "all_get_min", CTLFLAG_RW, 1310 &bbr_all_get_min, 0, 1311 "If you are less than a MSS do you just get the min?"); 1312 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1313 SYSCTL_CHILDREN(bbr_hptsi), 1314 OID_AUTO, "tso_min", CTLFLAG_RW, 1315 &bbr_hptsi_bytes_min, 1460, 1316 "For 0 -> 24Mbps what is floor number of segments for TSO"); 1317 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1318 SYSCTL_CHILDREN(bbr_hptsi), 1319 OID_AUTO, "seg_tso_max", CTLFLAG_RW, 1320 &bbr_hptsi_segments_max, 6, 1321 "For 0 -> 24Mbps what is top number of segments for TSO"); 1322 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1323 SYSCTL_CHILDREN(bbr_hptsi), 1324 OID_AUTO, "seg_floor", CTLFLAG_RW, 1325 &bbr_hptsi_segments_floor, 1, 1326 "Minimum TSO size we will fall too in segments"); 1327 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1328 SYSCTL_CHILDREN(bbr_hptsi), 1329 OID_AUTO, "utter_max", CTLFLAG_RW, 1330 &bbr_hptsi_utter_max, 0, 1331 "The absolute maximum that any pacing (outside of hardware) can be"); 1332 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1333 SYSCTL_CHILDREN(bbr_hptsi), 1334 OID_AUTO, "seg_divisor", CTLFLAG_RW, 1335 &bbr_hptsi_per_second, 100, 1336 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps "); 1337 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1338 SYSCTL_CHILDREN(bbr_hptsi), 1339 OID_AUTO, "srtt_mul", CTLFLAG_RW, 1340 &bbr_hptsi_max_mul, 1, 1341 "The multiplier for pace len max"); 1342 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1343 SYSCTL_CHILDREN(bbr_hptsi), 1344 OID_AUTO, "srtt_div", CTLFLAG_RW, 1345 &bbr_hptsi_max_div, 2, 1346 "The divisor for pace len max"); 1347 /* Measurement controls */ 1348 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1349 SYSCTL_CHILDREN(bbr_sysctl_root), 1350 OID_AUTO, 1351 "measure", 1352 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1353 "Measurement controls"); 1354 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1355 SYSCTL_CHILDREN(bbr_measure), 1356 OID_AUTO, "min_i_bw", CTLFLAG_RW, 1357 &bbr_initial_bw_bps, 62500, 1358 "Minimum initial b/w in bytes per second"); 1359 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1360 SYSCTL_CHILDREN(bbr_measure), 1361 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1362 &bbr_sack_not_required, 0, 1363 "Do we allow bbr to run on connections not supporting SACK?"); 1364 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1365 SYSCTL_CHILDREN(bbr_measure), 1366 OID_AUTO, "use_google", CTLFLAG_RW, 1367 &bbr_use_google_algo, 0, 1368 "Use has close to google V1.0 has possible?"); 1369 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1370 SYSCTL_CHILDREN(bbr_measure), 1371 OID_AUTO, "ts_limiting", CTLFLAG_RW, 1372 &bbr_ts_limiting, 1, 1373 "Do we attempt to use the peers timestamp to limit b/w caculations?"); 1374 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1375 SYSCTL_CHILDREN(bbr_measure), 1376 OID_AUTO, "ts_can_raise", CTLFLAG_RW, 1377 &bbr_ts_can_raise, 0, 1378 "Can we raise the b/w via timestamp b/w calculation?"); 1379 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1380 SYSCTL_CHILDREN(bbr_measure), 1381 OID_AUTO, "ts_delta", CTLFLAG_RW, 1382 &bbr_min_usec_delta, 20000, 1383 "How long in usec between ts of our sends in ts validation code?"); 1384 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1385 SYSCTL_CHILDREN(bbr_measure), 1386 OID_AUTO, "ts_peer_delta", CTLFLAG_RW, 1387 &bbr_min_peer_delta, 20, 1388 "What min numerical value should be between the peer deltas?"); 1389 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1390 SYSCTL_CHILDREN(bbr_measure), 1391 OID_AUTO, "ts_delta_percent", CTLFLAG_RW, 1392 &bbr_delta_percent, 150, 1393 "What percentage (150 = 15.0) do we allow variance for?"); 1394 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1395 SYSCTL_CHILDREN(bbr_measure), 1396 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW, 1397 &bbr_min_measurements_req, 1, 1398 "What is the minimum measurement count we need before we switch to our b/w estimate"); 1399 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1400 SYSCTL_CHILDREN(bbr_measure), 1401 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW, 1402 &bbr_no_pacing_until, 4, 1403 "How many pkt-epoch's (0 is off) do we need before pacing is on?"); 1404 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1405 SYSCTL_CHILDREN(bbr_measure), 1406 OID_AUTO, "quanta", CTLFLAG_RW, 1407 &bbr_quanta, 2, 1408 "Extra quanta to add when calculating the target (ID section 4.2.3.2)."); 1409 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1410 SYSCTL_CHILDREN(bbr_measure), 1411 OID_AUTO, "noretran", CTLFLAG_RW, 1412 &bbr_no_retran, 0, 1413 "Should google mode not use retransmission measurements for the b/w estimation?"); 1414 /* State controls */ 1415 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1416 SYSCTL_CHILDREN(bbr_sysctl_root), 1417 OID_AUTO, 1418 "states", 1419 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1420 "State controls"); 1421 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1422 SYSCTL_CHILDREN(bbr_states), 1423 OID_AUTO, "idle_restart", CTLFLAG_RW, 1424 &bbr_uses_idle_restart, 0, 1425 "Do we use a new special idle_restart state to ramp back up quickly?"); 1426 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1427 SYSCTL_CHILDREN(bbr_states), 1428 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW, 1429 &bbr_idle_restart_threshold, 100000, 1430 "How long must we be idle before we restart??"); 1431 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1432 SYSCTL_CHILDREN(bbr_states), 1433 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW, 1434 &bbr_state_is_pkt_epoch, 0, 1435 "Do we use a pkt-epoch for substate if 0 rttProp?"); 1436 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1437 SYSCTL_CHILDREN(bbr_states), 1438 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW, 1439 &bbr_rtt_gain_thresh, 0, 1440 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?"); 1441 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1442 SYSCTL_CHILDREN(bbr_states), 1443 OID_AUTO, "drain_floor", CTLFLAG_RW, 1444 &bbr_drain_floor, 88, 1445 "What is the lowest we can drain (pg) too?"); 1446 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1447 SYSCTL_CHILDREN(bbr_states), 1448 OID_AUTO, "drain_2_target", CTLFLAG_RW, 1449 &bbr_state_drain_2_tar, 1, 1450 "Do we drain to target in drain substate?"); 1451 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1452 SYSCTL_CHILDREN(bbr_states), 1453 OID_AUTO, "gain_2_target", CTLFLAG_RW, 1454 &bbr_gain_to_target, 1, 1455 "Does probe bw gain to target??"); 1456 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1457 SYSCTL_CHILDREN(bbr_states), 1458 OID_AUTO, "gain_extra_time", CTLFLAG_RW, 1459 &bbr_gain_gets_extra_too, 1, 1460 "Does probe bw gain get the extra time too?"); 1461 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1462 SYSCTL_CHILDREN(bbr_states), 1463 OID_AUTO, "ld_div", CTLFLAG_RW, 1464 &bbr_drain_drop_div, 5, 1465 "Long drain drop divider?"); 1466 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1467 SYSCTL_CHILDREN(bbr_states), 1468 OID_AUTO, "ld_mul", CTLFLAG_RW, 1469 &bbr_drain_drop_mul, 4, 1470 "Long drain drop multiplier?"); 1471 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1472 SYSCTL_CHILDREN(bbr_states), 1473 OID_AUTO, "rand_ot_disc", CTLFLAG_RW, 1474 &bbr_rand_ot, 50, 1475 "Random discount of the ot?"); 1476 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1477 SYSCTL_CHILDREN(bbr_states), 1478 OID_AUTO, "dr_filter_life", CTLFLAG_RW, 1479 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT, 1480 "How many packet-epochs does the b/w delivery rate last?"); 1481 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1482 SYSCTL_CHILDREN(bbr_states), 1483 OID_AUTO, "subdrain_applimited", CTLFLAG_RW, 1484 &bbr_sub_drain_app_limit, 0, 1485 "Does our sub-state drain invoke app limited if its long?"); 1486 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1487 SYSCTL_CHILDREN(bbr_states), 1488 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW, 1489 &bbr_sub_drain_slam_cwnd, 0, 1490 "Should we set/recover cwnd for sub-state drain?"); 1491 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1492 SYSCTL_CHILDREN(bbr_states), 1493 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW, 1494 &bbr_slam_cwnd_in_main_drain, 0, 1495 "Should we set/recover cwnd for main-state drain?"); 1496 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1497 SYSCTL_CHILDREN(bbr_states), 1498 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW, 1499 &google_allow_early_out, 1, 1500 "Should we allow google probe-bw/drain to exit early at flight target?"); 1501 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1502 SYSCTL_CHILDREN(bbr_states), 1503 OID_AUTO, "google_exit_loss", CTLFLAG_RW, 1504 &google_consider_lost, 1, 1505 "Should we have losses exit gain of probebw in google mode??"); 1506 /* Startup controls */ 1507 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1508 SYSCTL_CHILDREN(bbr_sysctl_root), 1509 OID_AUTO, 1510 "startup", 1511 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1512 "Startup controls"); 1513 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1514 SYSCTL_CHILDREN(bbr_startup), 1515 OID_AUTO, "cheat_iwnd", CTLFLAG_RW, 1516 &bbr_sends_full_iwnd, 1, 1517 "Do we not pace but burst out initial windows has our TSO size?"); 1518 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1519 SYSCTL_CHILDREN(bbr_startup), 1520 OID_AUTO, "loss_threshold", CTLFLAG_RW, 1521 &bbr_startup_loss_thresh, 2000, 1522 "In startup what is the loss threshold in a pe that will exit us from startup?"); 1523 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1524 SYSCTL_CHILDREN(bbr_startup), 1525 OID_AUTO, "use_lowerpg", CTLFLAG_RW, 1526 &bbr_use_lower_gain_in_startup, 1, 1527 "Should we use a lower hptsi gain if we see loss in startup?"); 1528 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1529 SYSCTL_CHILDREN(bbr_startup), 1530 OID_AUTO, "gain", CTLFLAG_RW, 1531 &bbr_start_exit, 25, 1532 "What gain percent do we need to see to stay in startup??"); 1533 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1534 SYSCTL_CHILDREN(bbr_startup), 1535 OID_AUTO, "low_gain", CTLFLAG_RW, 1536 &bbr_low_start_exit, 15, 1537 "What gain percent do we need to see to stay in the lower gain startup??"); 1538 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1539 SYSCTL_CHILDREN(bbr_startup), 1540 OID_AUTO, "loss_exit", CTLFLAG_RW, 1541 &bbr_exit_startup_at_loss, 1, 1542 "Should we exit startup at loss in an epoch if we are not gaining?"); 1543 /* CWND controls */ 1544 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1545 SYSCTL_CHILDREN(bbr_sysctl_root), 1546 OID_AUTO, 1547 "cwnd", 1548 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1549 "Cwnd controls"); 1550 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1551 SYSCTL_CHILDREN(bbr_cwnd), 1552 OID_AUTO, "tar_rtt", CTLFLAG_RW, 1553 &bbr_cwndtarget_rtt_touse, 0, 1554 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?"); 1555 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1556 SYSCTL_CHILDREN(bbr_cwnd), 1557 OID_AUTO, "may_shrink", CTLFLAG_RW, 1558 &bbr_cwnd_may_shrink, 0, 1559 "Can the cwnd shrink if it would grow to more than the target?"); 1560 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1561 SYSCTL_CHILDREN(bbr_cwnd), 1562 OID_AUTO, "max_target_limit", CTLFLAG_RW, 1563 &bbr_target_cwnd_mult_limit, 8, 1564 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?"); 1565 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1566 SYSCTL_CHILDREN(bbr_cwnd), 1567 OID_AUTO, "highspeed_min", CTLFLAG_RW, 1568 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS, 1569 "What is the high-speed min cwnd (rttProp under 1ms)"); 1570 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1571 SYSCTL_CHILDREN(bbr_cwnd), 1572 OID_AUTO, "lowspeed_min", CTLFLAG_RW, 1573 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS, 1574 "What is the min cwnd (rttProp > 1ms)"); 1575 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1576 SYSCTL_CHILDREN(bbr_cwnd), 1577 OID_AUTO, "initwin", CTLFLAG_RW, 1578 &bbr_def_init_win, 10, 1579 "What is the BBR initial window, if 0 use tcp version"); 1580 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1581 SYSCTL_CHILDREN(bbr_cwnd), 1582 OID_AUTO, "do_loss_red", CTLFLAG_RW, 1583 &bbr_do_red, 600, 1584 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?"); 1585 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1586 SYSCTL_CHILDREN(bbr_cwnd), 1587 OID_AUTO, "red_scale", CTLFLAG_RW, 1588 &bbr_red_scale, 20000, 1589 "What RTT do we scale with?"); 1590 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1591 SYSCTL_CHILDREN(bbr_cwnd), 1592 OID_AUTO, "red_growslow", CTLFLAG_RW, 1593 &bbr_red_growth_restrict, 1, 1594 "Do we restrict cwnd growth for whats in flight?"); 1595 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1596 SYSCTL_CHILDREN(bbr_cwnd), 1597 OID_AUTO, "red_div", CTLFLAG_RW, 1598 &bbr_red_div, 2, 1599 "If we reduce whats the divisor?"); 1600 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1601 SYSCTL_CHILDREN(bbr_cwnd), 1602 OID_AUTO, "red_mul", CTLFLAG_RW, 1603 &bbr_red_mul, 1, 1604 "If we reduce whats the mulitiplier?"); 1605 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1606 SYSCTL_CHILDREN(bbr_cwnd), 1607 OID_AUTO, "target_is_unit", CTLFLAG_RW, 1608 &bbr_target_is_bbunit, 0, 1609 "Is the state target the pacing_gain or BBR_UNIT?"); 1610 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1611 SYSCTL_CHILDREN(bbr_cwnd), 1612 OID_AUTO, "drop_limit", CTLFLAG_RW, 1613 &bbr_drop_limit, 0, 1614 "Number of segments limit for drop (0=use min_cwnd w/flight)?"); 1615 1616 /* Timeout controls */ 1617 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1618 SYSCTL_CHILDREN(bbr_sysctl_root), 1619 OID_AUTO, 1620 "timeout", 1621 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1622 "Time out controls"); 1623 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1624 SYSCTL_CHILDREN(bbr_timeout), 1625 OID_AUTO, "delack", CTLFLAG_RW, 1626 &bbr_delack_time, 100000, 1627 "BBR's delayed ack time"); 1628 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1629 SYSCTL_CHILDREN(bbr_timeout), 1630 OID_AUTO, "tlp_uses", CTLFLAG_RW, 1631 &bbr_tlp_type_to_use, 3, 1632 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt"); 1633 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1634 SYSCTL_CHILDREN(bbr_timeout), 1635 OID_AUTO, "persmin", CTLFLAG_RW, 1636 &bbr_persist_min, 250000, 1637 "What is the minimum time in microseconds between persists"); 1638 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1639 SYSCTL_CHILDREN(bbr_timeout), 1640 OID_AUTO, "persmax", CTLFLAG_RW, 1641 &bbr_persist_max, 1000000, 1642 "What is the largest delay in microseconds between persists"); 1643 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1644 SYSCTL_CHILDREN(bbr_timeout), 1645 OID_AUTO, "tlp_minto", CTLFLAG_RW, 1646 &bbr_tlp_min, 10000, 1647 "TLP Min timeout in usecs"); 1648 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1649 SYSCTL_CHILDREN(bbr_timeout), 1650 OID_AUTO, "tlp_dack_time", CTLFLAG_RW, 1651 &bbr_delayed_ack_time, 200000, 1652 "TLP delayed ack compensation value"); 1653 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1654 SYSCTL_CHILDREN(bbr_sysctl_root), 1655 OID_AUTO, "minrto", CTLFLAG_RW, 1656 &bbr_rto_min_ms, 30, 1657 "Minimum RTO in ms"); 1658 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1659 SYSCTL_CHILDREN(bbr_timeout), 1660 OID_AUTO, "maxrto", CTLFLAG_RW, 1661 &bbr_rto_max_sec, 4, 1662 "Maximum RTO in seconds -- should be at least as large as min_rto"); 1663 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1664 SYSCTL_CHILDREN(bbr_timeout), 1665 OID_AUTO, "tlp_retry", CTLFLAG_RW, 1666 &bbr_tlp_max_resend, 2, 1667 "How many times does TLP retry a single segment or multiple with no ACK"); 1668 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1669 SYSCTL_CHILDREN(bbr_timeout), 1670 OID_AUTO, "minto", CTLFLAG_RW, 1671 &bbr_min_to, 1000, 1672 "Minimum rack timeout in useconds"); 1673 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1674 SYSCTL_CHILDREN(bbr_timeout), 1675 OID_AUTO, "pktdelay", CTLFLAG_RW, 1676 &bbr_pkt_delay, 1000, 1677 "Extra RACK time (in useconds) besides reordering thresh"); 1678 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1679 SYSCTL_CHILDREN(bbr_timeout), 1680 OID_AUTO, "incr_tmrs", CTLFLAG_RW, 1681 &bbr_incr_timers, 1, 1682 "Increase the RXT/TLP timer by the pacing time used?"); 1683 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1684 SYSCTL_CHILDREN(bbr_timeout), 1685 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW, 1686 &bbr_marks_rxt_sack_passed, 0, 1687 "Mark sack passed on all those not ack'd when a RXT hits?"); 1688 /* Policer controls */ 1689 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1690 SYSCTL_CHILDREN(bbr_sysctl_root), 1691 OID_AUTO, 1692 "policer", 1693 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1694 "Policer controls"); 1695 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1696 SYSCTL_CHILDREN(bbr_policer), 1697 OID_AUTO, "detect_enable", CTLFLAG_RW, 1698 &bbr_policer_detection_enabled, 1, 1699 "Is policer detection enabled??"); 1700 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1701 SYSCTL_CHILDREN(bbr_policer), 1702 OID_AUTO, "min_pes", CTLFLAG_RW, 1703 &bbr_lt_intvl_min_rtts, 4, 1704 "Minimum number of PE's?"); 1705 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1706 SYSCTL_CHILDREN(bbr_policer), 1707 OID_AUTO, "bwdiff", CTLFLAG_RW, 1708 &bbr_lt_bw_diff, (4000/8), 1709 "Minimal bw diff?"); 1710 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1711 SYSCTL_CHILDREN(bbr_policer), 1712 OID_AUTO, "bwratio", CTLFLAG_RW, 1713 &bbr_lt_bw_ratio, 8, 1714 "Minimal bw diff?"); 1715 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1716 SYSCTL_CHILDREN(bbr_policer), 1717 OID_AUTO, "from_rack_rxt", CTLFLAG_RW, 1718 &bbr_policer_call_from_rack_to, 0, 1719 "Do we call the policer detection code from a rack-timeout?"); 1720 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1721 SYSCTL_CHILDREN(bbr_policer), 1722 OID_AUTO, "false_postive", CTLFLAG_RW, 1723 &bbr_lt_intvl_fp, 0, 1724 "What packet epoch do we do false-positive detection at (0=no)?"); 1725 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1726 SYSCTL_CHILDREN(bbr_policer), 1727 OID_AUTO, "loss_thresh", CTLFLAG_RW, 1728 &bbr_lt_loss_thresh, 196, 1729 "Loss threshold 196 = 19.6%?"); 1730 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1731 SYSCTL_CHILDREN(bbr_policer), 1732 OID_AUTO, "false_postive_thresh", CTLFLAG_RW, 1733 &bbr_lt_fd_thresh, 100, 1734 "What percentage is the false detection threshold (150=15.0)?"); 1735 /* All the rest */ 1736 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1737 SYSCTL_CHILDREN(bbr_sysctl_root), 1738 OID_AUTO, "cheat_rxt", CTLFLAG_RW, 1739 &bbr_use_rack_resend_cheat, 0, 1740 "Do we burst 1ms between sends on retransmissions (like rack)?"); 1741 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1742 SYSCTL_CHILDREN(bbr_sysctl_root), 1743 OID_AUTO, "error_paceout", CTLFLAG_RW, 1744 &bbr_error_base_paceout, 10000, 1745 "When we hit an error what is the min to pace out in usec's?"); 1746 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1747 SYSCTL_CHILDREN(bbr_sysctl_root), 1748 OID_AUTO, "kill_paceout", CTLFLAG_RW, 1749 &bbr_max_net_error_cnt, 10, 1750 "When we hit this many errors in a row, kill the session?"); 1751 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1752 SYSCTL_CHILDREN(bbr_sysctl_root), 1753 OID_AUTO, "data_after_close", CTLFLAG_RW, 1754 &bbr_ignore_data_after_close, 1, 1755 "Do we hold off sending a RST until all pending data is ack'd"); 1756 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1757 SYSCTL_CHILDREN(bbr_sysctl_root), 1758 OID_AUTO, "resend_use_tso", CTLFLAG_RW, 1759 &bbr_resends_use_tso, 0, 1760 "Can resends use TSO?"); 1761 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1762 SYSCTL_CHILDREN(bbr_sysctl_root), 1763 OID_AUTO, "sblklimit", CTLFLAG_RW, 1764 &bbr_sack_block_limit, 128, 1765 "When do we start ignoring small sack blocks"); 1766 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1767 SYSCTL_CHILDREN(bbr_sysctl_root), 1768 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1769 &bbr_verbose_logging, 0, 1770 "Should BBR black box logging be verbose"); 1771 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1772 SYSCTL_CHILDREN(bbr_sysctl_root), 1773 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1774 &bbr_reorder_thresh, 2, 1775 "What factor for rack will be added when seeing reordering (shift right)"); 1776 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1777 SYSCTL_CHILDREN(bbr_sysctl_root), 1778 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1779 &bbr_reorder_fade, 0, 1780 "Does reorder detection fade, if so how many ms (0 means never)"); 1781 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1782 SYSCTL_CHILDREN(bbr_sysctl_root), 1783 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1784 &bbr_tlp_thresh, 1, 1785 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1786 /* Stats and counters */ 1787 /* The pacing counters for hdwr/software can't be in the array */ 1788 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1789 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1790 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1791 SYSCTL_CHILDREN(bbr_sysctl_root), 1792 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD, 1793 &bbr_hdwr_pacing_enobuf, 1794 "Total number of enobufs for hardware paced flows"); 1795 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1796 SYSCTL_CHILDREN(bbr_sysctl_root), 1797 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD, 1798 &bbr_nohdwr_pacing_enobuf, 1799 "Total number of enobufs for non-hardware paced flows"); 1800 1801 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK); 1802 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1803 SYSCTL_CHILDREN(bbr_sysctl_root), 1804 OID_AUTO, "hdwr_pacing", CTLFLAG_RD, 1805 &bbr_flows_whdwr_pacing, 1806 "Total number of hardware paced flows"); 1807 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK); 1808 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1809 SYSCTL_CHILDREN(bbr_sysctl_root), 1810 OID_AUTO, "software_pacing", CTLFLAG_RD, 1811 &bbr_flows_nohdwr_pacing, 1812 "Total number of software paced flows"); 1813 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK); 1814 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1815 OID_AUTO, "stats", CTLFLAG_RD, 1816 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats"); 1817 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK); 1818 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1819 OID_AUTO, "opts", CTLFLAG_RD, 1820 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats"); 1821 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK); 1822 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1823 OID_AUTO, "lost", CTLFLAG_RD, 1824 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur"); 1825 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK); 1826 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1827 OID_AUTO, "stateresend", CTLFLAG_RD, 1828 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend"); 1829 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK); 1830 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1831 OID_AUTO, "statetime", CTLFLAG_RD, 1832 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states"); 1833 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1834 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1835 OID_AUTO, "outsize", CTLFLAG_RD, 1836 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls"); 1837 SYSCTL_ADD_PROC(&bbr_sysctl_ctx, 1838 SYSCTL_CHILDREN(bbr_sysctl_root), 1839 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1840 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters"); 1841 } 1842 1843 static void 1844 bbr_counter_destroy(void) 1845 { 1846 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE); 1847 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE); 1848 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE); 1849 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT); 1850 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT); 1851 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT); 1852 counter_u64_free(bbr_nohdwr_pacing_enobuf); 1853 counter_u64_free(bbr_hdwr_pacing_enobuf); 1854 counter_u64_free(bbr_flows_whdwr_pacing); 1855 counter_u64_free(bbr_flows_nohdwr_pacing); 1856 1857 } 1858 1859 static __inline void 1860 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts) 1861 { 1862 memset(l, 0, sizeof(union tcp_log_stackspecific)); 1863 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate; 1864 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate); 1865 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 1866 l->bw_inuse = bbr_get_bw(bbr); 1867 l->inflight = ctf_flight_size(bbr->rc_tp, 1868 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 1869 l->applimited = bbr->r_ctl.r_app_limited_until; 1870 l->delivered = bbr->r_ctl.rc_delivered; 1871 l->timeStamp = cts; 1872 l->lost = bbr->r_ctl.rc_lost; 1873 l->bbr_state = bbr->rc_bbr_state; 1874 l->bbr_substate = bbr_state_val(bbr); 1875 l->epoch = bbr->r_ctl.rc_rtt_epoch; 1876 l->lt_epoch = bbr->r_ctl.rc_lt_epoch; 1877 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain; 1878 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain; 1879 l->inhpts = tcp_in_hpts(bbr->rc_inp); 1880 l->use_lt_bw = bbr->rc_lt_use_bw; 1881 l->pkts_out = bbr->r_ctl.rc_flight_at_input; 1882 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch; 1883 } 1884 1885 static void 1886 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason) 1887 { 1888 if (tcp_bblogging_on(bbr->rc_tp)) { 1889 union tcp_log_stackspecific log; 1890 1891 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1892 log.u_bbr.flex1 = 0; 1893 log.u_bbr.flex2 = 0; 1894 log.u_bbr.flex5 = 0; 1895 log.u_bbr.flex3 = 0; 1896 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate; 1897 log.u_bbr.flex7 = reason; 1898 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt; 1899 log.u_bbr.flex8 = 0; 1900 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1901 &bbr->rc_inp->inp_socket->so_rcv, 1902 &bbr->rc_inp->inp_socket->so_snd, 1903 BBR_LOG_BW_RED_EV, 0, 1904 0, &log, false, &bbr->rc_tv); 1905 } 1906 } 1907 1908 static void 1909 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count) 1910 { 1911 if (tcp_bblogging_on(bbr->rc_tp)) { 1912 union tcp_log_stackspecific log; 1913 1914 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1915 log.u_bbr.flex1 = seq; 1916 log.u_bbr.flex2 = count; 1917 log.u_bbr.flex8 = mode; 1918 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1919 &bbr->rc_inp->inp_socket->so_rcv, 1920 &bbr->rc_inp->inp_socket->so_snd, 1921 BBR_LOG_LOWGAIN, 0, 1922 0, &log, false, &bbr->rc_tv); 1923 } 1924 } 1925 1926 static void 1927 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling, 1928 uint8_t reason, uint32_t p_maxseg, int len) 1929 { 1930 if (tcp_bblogging_on(bbr->rc_tp)) { 1931 union tcp_log_stackspecific log; 1932 1933 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1934 log.u_bbr.flex1 = p_maxseg; 1935 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags; 1936 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 1937 log.u_bbr.flex4 = reason; 1938 log.u_bbr.flex5 = bbr->rc_in_persist; 1939 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val; 1940 log.u_bbr.flex7 = p_maxseg; 1941 log.u_bbr.flex8 = bbr->rc_in_persist; 1942 log.u_bbr.pkts_out = 0; 1943 log.u_bbr.applimited = len; 1944 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1945 &bbr->rc_inp->inp_socket->so_rcv, 1946 &bbr->rc_inp->inp_socket->so_snd, 1947 BBR_LOG_JUSTRET, 0, 1948 tlen, &log, false, &bbr->rc_tv); 1949 } 1950 } 1951 1952 static void 1953 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq) 1954 { 1955 if (tcp_bblogging_on(bbr->rc_tp)) { 1956 union tcp_log_stackspecific log; 1957 1958 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1959 log.u_bbr.flex1 = seq; 1960 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 1961 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start; 1962 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1963 &bbr->rc_inp->inp_socket->so_rcv, 1964 &bbr->rc_inp->inp_socket->so_snd, 1965 BBR_LOG_ENTREC, 0, 1966 0, &log, false, &bbr->rc_tv); 1967 } 1968 } 1969 1970 static void 1971 bbr_log_msgsize_fail(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t len, uint32_t maxseg, uint32_t mtu, int32_t csum_flags, int32_t tso, uint32_t cts) 1972 { 1973 if (tcp_bblogging_on(tp)) { 1974 union tcp_log_stackspecific log; 1975 1976 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1977 log.u_bbr.flex1 = tso; 1978 log.u_bbr.flex2 = maxseg; 1979 log.u_bbr.flex3 = mtu; 1980 log.u_bbr.flex4 = csum_flags; 1981 TCP_LOG_EVENTP(tp, NULL, 1982 &bbr->rc_inp->inp_socket->so_rcv, 1983 &bbr->rc_inp->inp_socket->so_snd, 1984 BBR_LOG_MSGSIZE, 0, 1985 0, &log, false, &bbr->rc_tv); 1986 } 1987 } 1988 1989 static void 1990 bbr_log_flowend(struct tcp_bbr *bbr) 1991 { 1992 if (tcp_bblogging_on(bbr->rc_tp)) { 1993 union tcp_log_stackspecific log; 1994 struct sockbuf *r, *s; 1995 struct timeval tv; 1996 1997 if (bbr->rc_inp->inp_socket) { 1998 r = &bbr->rc_inp->inp_socket->so_rcv; 1999 s = &bbr->rc_inp->inp_socket->so_snd; 2000 } else { 2001 r = s = NULL; 2002 } 2003 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv)); 2004 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2005 r, s, 2006 TCP_LOG_FLOWEND, 0, 2007 0, &log, false, &tv); 2008 } 2009 } 2010 2011 static void 2012 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, 2013 uint32_t lost, uint32_t del) 2014 { 2015 if (tcp_bblogging_on(bbr->rc_tp)) { 2016 union tcp_log_stackspecific log; 2017 2018 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2019 log.u_bbr.flex1 = lost; 2020 log.u_bbr.flex2 = del; 2021 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw; 2022 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt; 2023 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2024 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2025 log.u_bbr.flex7 = line; 2026 log.u_bbr.flex8 = 0; 2027 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count; 2028 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2029 &bbr->rc_inp->inp_socket->so_rcv, 2030 &bbr->rc_inp->inp_socket->so_snd, 2031 BBR_LOG_PKT_EPOCH, 0, 2032 0, &log, false, &bbr->rc_tv); 2033 } 2034 } 2035 2036 static void 2037 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time) 2038 { 2039 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2040 union tcp_log_stackspecific log; 2041 2042 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2043 log.u_bbr.flex1 = bbr->r_ctl.rc_lost; 2044 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat; 2045 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat; 2046 log.u_bbr.flex7 = line; 2047 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2048 &bbr->rc_inp->inp_socket->so_rcv, 2049 &bbr->rc_inp->inp_socket->so_snd, 2050 BBR_LOG_TIME_EPOCH, 0, 2051 0, &log, false, &bbr->rc_tv); 2052 } 2053 } 2054 2055 static void 2056 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth) 2057 { 2058 if (tcp_bblogging_on(bbr->rc_tp)) { 2059 union tcp_log_stackspecific log; 2060 2061 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2062 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2063 log.u_bbr.flex2 = new_tar; 2064 log.u_bbr.flex3 = line; 2065 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2066 log.u_bbr.flex5 = bbr_quanta; 2067 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs; 2068 log.u_bbr.flex7 = bbr->rc_last_options; 2069 log.u_bbr.flex8 = meth; 2070 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2071 &bbr->rc_inp->inp_socket->so_rcv, 2072 &bbr->rc_inp->inp_socket->so_snd, 2073 BBR_LOG_STATE_TARGET, 0, 2074 0, &log, false, &bbr->rc_tv); 2075 } 2076 2077 } 2078 2079 static void 2080 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2081 { 2082 if (tcp_bblogging_on(bbr->rc_tp)) { 2083 union tcp_log_stackspecific log; 2084 2085 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2086 log.u_bbr.flex1 = line; 2087 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2088 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2089 if (bbr_state_is_pkt_epoch) 2090 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 2091 else 2092 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP); 2093 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2094 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2095 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000); 2096 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra; 2097 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state; 2098 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2099 &bbr->rc_inp->inp_socket->so_rcv, 2100 &bbr->rc_inp->inp_socket->so_snd, 2101 BBR_LOG_STATE, 0, 2102 0, &log, false, &bbr->rc_tv); 2103 } 2104 } 2105 2106 static void 2107 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 2108 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond) 2109 { 2110 if (tcp_bblogging_on(bbr->rc_tp)) { 2111 union tcp_log_stackspecific log; 2112 2113 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2114 log.u_bbr.flex1 = line; 2115 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2116 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt; 2117 log.u_bbr.flex4 = applied; 2118 log.u_bbr.flex5 = rtt; 2119 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2120 log.u_bbr.flex7 = cond; 2121 log.u_bbr.flex8 = reas; 2122 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2123 &bbr->rc_inp->inp_socket->so_rcv, 2124 &bbr->rc_inp->inp_socket->so_snd, 2125 BBR_LOG_RTT_SHRINKS, 0, 2126 0, &log, false, &bbr->rc_tv); 2127 } 2128 } 2129 2130 static void 2131 bbr_log_type_exit_rec(struct tcp_bbr *bbr) 2132 { 2133 if (tcp_bblogging_on(bbr->rc_tp)) { 2134 union tcp_log_stackspecific log; 2135 2136 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2137 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start; 2138 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 2139 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2140 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2141 &bbr->rc_inp->inp_socket->so_rcv, 2142 &bbr->rc_inp->inp_socket->so_snd, 2143 BBR_LOG_EXITREC, 0, 2144 0, &log, false, &bbr->rc_tv); 2145 } 2146 } 2147 2148 static void 2149 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg, 2150 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line) 2151 { 2152 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2153 union tcp_log_stackspecific log; 2154 2155 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2156 log.u_bbr.flex1 = line; 2157 log.u_bbr.flex2 = prev_acked; 2158 log.u_bbr.flex3 = bytes_this_ack; 2159 log.u_bbr.flex4 = chg; 2160 log.u_bbr.flex5 = th_ack; 2161 log.u_bbr.flex6 = target; 2162 log.u_bbr.flex8 = meth; 2163 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2164 &bbr->rc_inp->inp_socket->so_rcv, 2165 &bbr->rc_inp->inp_socket->so_snd, 2166 BBR_LOG_CWND, 0, 2167 0, &log, false, &bbr->rc_tv); 2168 } 2169 } 2170 2171 static void 2172 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin) 2173 { 2174 /* 2175 * Log the rtt sample we are applying to the srtt algorithm in 2176 * useconds. 2177 */ 2178 if (tcp_bblogging_on(bbr->rc_tp)) { 2179 union tcp_log_stackspecific log; 2180 2181 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2182 log.u_bbr.flex1 = rtt; 2183 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time; 2184 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay; 2185 log.u_bbr.flex4 = bbr->rc_tp->ts_offset; 2186 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2187 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv); 2188 log.u_bbr.flex6 = tsin; 2189 log.u_bbr.flex7 = 0; 2190 log.u_bbr.flex8 = bbr->rc_ack_was_delayed; 2191 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2192 &bbr->rc_inp->inp_socket->so_rcv, 2193 &bbr->rc_inp->inp_socket->so_snd, 2194 TCP_LOG_RTT, 0, 2195 0, &log, false, &bbr->rc_tv); 2196 } 2197 } 2198 2199 static void 2200 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit) 2201 { 2202 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2203 union tcp_log_stackspecific log; 2204 2205 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2206 log.u_bbr.flex1 = time_in; 2207 log.u_bbr.flex2 = line; 2208 log.u_bbr.flex8 = enter_exit; 2209 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2210 &bbr->rc_inp->inp_socket->so_rcv, 2211 &bbr->rc_inp->inp_socket->so_snd, 2212 BBR_LOG_PERSIST, 0, 2213 0, &log, false, &bbr->rc_tv); 2214 } 2215 } 2216 static void 2217 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts) 2218 { 2219 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2220 union tcp_log_stackspecific log; 2221 2222 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2223 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age; 2224 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2225 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2226 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time; 2227 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2228 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2229 &bbr->rc_inp->inp_socket->so_rcv, 2230 &bbr->rc_inp->inp_socket->so_snd, 2231 BBR_LOG_ACKCLEAR, 0, 2232 0, &log, false, &bbr->rc_tv); 2233 } 2234 } 2235 2236 static void 2237 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen, 2238 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m) 2239 { 2240 if (tcp_bblogging_on(bbr->rc_tp)) { 2241 union tcp_log_stackspecific log; 2242 struct timeval tv; 2243 2244 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2245 log.u_bbr.flex1 = nsegs; 2246 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes; 2247 if (m) { 2248 struct timespec ts; 2249 2250 log.u_bbr.flex3 = m->m_flags; 2251 if (m->m_flags & M_TSTMP) { 2252 mbuf_tstmp2timespec(m, &ts); 2253 tv.tv_sec = ts.tv_sec; 2254 tv.tv_usec = ts.tv_nsec / 1000; 2255 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv); 2256 } else { 2257 log.u_bbr.lt_epoch = 0; 2258 } 2259 if (m->m_flags & M_TSTMP_LRO) { 2260 mbuf_tstmp2timeval(m, &tv); 2261 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv); 2262 } else { 2263 /* No arrival timestamp */ 2264 log.u_bbr.flex5 = 0; 2265 } 2266 2267 log.u_bbr.pkts_out = tcp_get_usecs(&tv); 2268 } else { 2269 log.u_bbr.flex3 = 0; 2270 log.u_bbr.flex5 = 0; 2271 log.u_bbr.flex6 = 0; 2272 log.u_bbr.pkts_out = 0; 2273 } 2274 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state; 2275 log.u_bbr.flex7 = bbr->r_wanted_output; 2276 log.u_bbr.flex8 = bbr->rc_in_persist; 2277 TCP_LOG_EVENTP(bbr->rc_tp, th, 2278 &bbr->rc_inp->inp_socket->so_rcv, 2279 &bbr->rc_inp->inp_socket->so_snd, 2280 TCP_LOG_IN, 0, 2281 tlen, &log, true, &bbr->rc_tv); 2282 } 2283 } 2284 2285 static void 2286 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out) 2287 { 2288 if (tcp_bblogging_on(bbr->rc_tp)) { 2289 union tcp_log_stackspecific log; 2290 2291 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2292 log.u_bbr.flex1 = did_out; 2293 log.u_bbr.flex2 = nxt_pkt; 2294 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val; 2295 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2296 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp; 2297 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes; 2298 log.u_bbr.flex7 = bbr->r_wanted_output; 2299 log.u_bbr.flex8 = bbr->rc_in_persist; 2300 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay; 2301 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2302 &bbr->rc_inp->inp_socket->so_rcv, 2303 &bbr->rc_inp->inp_socket->so_snd, 2304 BBR_LOG_DOSEG_DONE, 0, 2305 0, &log, true, &bbr->rc_tv); 2306 } 2307 } 2308 2309 static void 2310 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts, 2311 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz) 2312 { 2313 if (tcp_bblogging_on(bbr->rc_tp)) { 2314 union tcp_log_stackspecific log; 2315 2316 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2317 log.u_bbr.flex1 = line; 2318 log.u_bbr.flex2 = o_len; 2319 log.u_bbr.flex3 = segcnt; 2320 log.u_bbr.flex4 = segsiz; 2321 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2322 &bbr->rc_inp->inp_socket->so_rcv, 2323 &bbr->rc_inp->inp_socket->so_snd, 2324 BBR_LOG_ENOBUF_JMP, ENOBUFS, 2325 len, &log, true, &bbr->rc_tv); 2326 } 2327 } 2328 2329 static void 2330 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling) 2331 { 2332 if (tcp_bblogging_on(bbr->rc_tp)) { 2333 union tcp_log_stackspecific log; 2334 2335 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2336 log.u_bbr.flex1 = timers; 2337 log.u_bbr.flex2 = ret; 2338 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 2339 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2340 log.u_bbr.flex5 = cts; 2341 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2342 log.u_bbr.flex8 = hpts_calling; 2343 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2344 &bbr->rc_inp->inp_socket->so_rcv, 2345 &bbr->rc_inp->inp_socket->so_snd, 2346 BBR_LOG_TO_PROCESS, 0, 2347 0, &log, false, &bbr->rc_tv); 2348 } 2349 } 2350 2351 static void 2352 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num) 2353 { 2354 if (tcp_bblogging_on(bbr->rc_tp)) { 2355 union tcp_log_stackspecific log; 2356 uint64_t ar; 2357 2358 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2359 log.u_bbr.flex1 = bbr->bbr_timer_src; 2360 log.u_bbr.flex2 = 0; 2361 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2362 ar = (uint64_t)(bbr->r_ctl.rc_resend); 2363 ar >>= 32; 2364 ar &= 0x00000000ffffffff; 2365 log.u_bbr.flex4 = (uint32_t)ar; 2366 ar = (uint64_t)bbr->r_ctl.rc_resend; 2367 ar &= 0x00000000ffffffff; 2368 log.u_bbr.flex5 = (uint32_t)ar; 2369 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2370 log.u_bbr.flex8 = to_num; 2371 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2372 &bbr->rc_inp->inp_socket->so_rcv, 2373 &bbr->rc_inp->inp_socket->so_snd, 2374 BBR_LOG_RTO, 0, 2375 0, &log, false, &bbr->rc_tv); 2376 } 2377 } 2378 2379 static void 2380 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason) 2381 { 2382 if (tcp_bblogging_on(bbr->rc_tp)) { 2383 union tcp_log_stackspecific log; 2384 2385 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2386 log.u_bbr.flex1 = flex1; 2387 log.u_bbr.flex2 = flex2; 2388 log.u_bbr.flex3 = flex3; 2389 log.u_bbr.flex4 = 0; 2390 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2391 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2392 log.u_bbr.flex8 = reason; 2393 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw; 2394 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2395 &bbr->rc_inp->inp_socket->so_rcv, 2396 &bbr->rc_inp->inp_socket->so_snd, 2397 BBR_LOG_REDUCE, 0, 2398 0, &log, false, &bbr->rc_tv); 2399 } 2400 } 2401 2402 static void 2403 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag) 2404 { 2405 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2406 union tcp_log_stackspecific log; 2407 2408 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2409 log.u_bbr.flex1 = diag->p_nxt_slot; 2410 log.u_bbr.flex2 = diag->p_cur_slot; 2411 log.u_bbr.flex3 = diag->slot_req; 2412 log.u_bbr.flex4 = diag->inp_hptsslot; 2413 log.u_bbr.flex5 = diag->slot_remaining; 2414 log.u_bbr.flex6 = diag->need_new_to; 2415 log.u_bbr.flex7 = diag->p_hpts_active; 2416 log.u_bbr.flex8 = diag->p_on_min_sleep; 2417 /* Hijack other fields as needed */ 2418 log.u_bbr.epoch = diag->have_slept; 2419 log.u_bbr.lt_epoch = diag->yet_to_sleep; 2420 log.u_bbr.pkts_out = diag->co_ret; 2421 log.u_bbr.applimited = diag->hpts_sleep_time; 2422 log.u_bbr.delivered = diag->p_prev_slot; 2423 log.u_bbr.inflight = diag->p_runningslot; 2424 log.u_bbr.bw_inuse = diag->wheel_slot; 2425 log.u_bbr.rttProp = diag->wheel_cts; 2426 log.u_bbr.delRate = diag->maxslots; 2427 log.u_bbr.cur_del_rate = diag->p_curtick; 2428 log.u_bbr.cur_del_rate <<= 32; 2429 log.u_bbr.cur_del_rate |= diag->p_lasttick; 2430 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2431 &bbr->rc_inp->inp_socket->so_rcv, 2432 &bbr->rc_inp->inp_socket->so_snd, 2433 BBR_LOG_HPTSDIAG, 0, 2434 0, &log, false, &bbr->rc_tv); 2435 } 2436 } 2437 2438 static void 2439 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt, 2440 uint32_t thresh, uint32_t to) 2441 { 2442 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2443 union tcp_log_stackspecific log; 2444 2445 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2446 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar; 2447 log.u_bbr.flex2 = time_since_sent; 2448 log.u_bbr.flex3 = srtt; 2449 log.u_bbr.flex4 = thresh; 2450 log.u_bbr.flex5 = to; 2451 log.u_bbr.flex6 = bbr->rc_tp->t_srtt; 2452 log.u_bbr.flex8 = mode; 2453 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2454 &bbr->rc_inp->inp_socket->so_rcv, 2455 &bbr->rc_inp->inp_socket->so_snd, 2456 BBR_LOG_TIMERPREP, 0, 2457 0, &log, false, &bbr->rc_tv); 2458 } 2459 } 2460 2461 static void 2462 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 2463 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod) 2464 { 2465 if (tcp_bblogging_on(bbr->rc_tp)) { 2466 union tcp_log_stackspecific log; 2467 2468 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2469 log.u_bbr.flex1 = usecs; 2470 log.u_bbr.flex2 = len; 2471 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff); 2472 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff); 2473 if (override) 2474 log.u_bbr.flex5 = (1 << 2); 2475 else 2476 log.u_bbr.flex5 = 0; 2477 log.u_bbr.flex6 = override; 2478 log.u_bbr.flex7 = gain; 2479 log.u_bbr.flex8 = mod; 2480 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2481 &bbr->rc_inp->inp_socket->so_rcv, 2482 &bbr->rc_inp->inp_socket->so_snd, 2483 BBR_LOG_HPTSI_CALC, 0, 2484 len, &log, false, &bbr->rc_tv); 2485 } 2486 } 2487 2488 static void 2489 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2490 { 2491 if (tcp_bblogging_on(bbr->rc_tp)) { 2492 union tcp_log_stackspecific log; 2493 2494 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2495 2496 log.u_bbr.flex1 = bbr->bbr_timer_src; 2497 log.u_bbr.flex2 = to; 2498 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2499 log.u_bbr.flex4 = slot; 2500 log.u_bbr.flex5 = bbr->rc_inp->inp_hptsslot; 2501 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2502 log.u_bbr.pkts_out = bbr->rc_inp->inp_flags2; 2503 log.u_bbr.flex8 = which; 2504 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2505 &bbr->rc_inp->inp_socket->so_rcv, 2506 &bbr->rc_inp->inp_socket->so_snd, 2507 BBR_LOG_TIMERSTAR, 0, 2508 0, &log, false, &bbr->rc_tv); 2509 } 2510 } 2511 2512 static void 2513 bbr_log_thresh_choice(struct tcp_bbr *bbr, uint32_t cts, uint32_t thresh, uint32_t lro, uint32_t srtt, struct bbr_sendmap *rsm, uint8_t frm) 2514 { 2515 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2516 union tcp_log_stackspecific log; 2517 2518 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2519 log.u_bbr.flex1 = thresh; 2520 log.u_bbr.flex2 = lro; 2521 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts; 2522 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 2523 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2524 log.u_bbr.flex6 = srtt; 2525 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift; 2526 log.u_bbr.flex8 = frm; 2527 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2528 &bbr->rc_inp->inp_socket->so_rcv, 2529 &bbr->rc_inp->inp_socket->so_snd, 2530 BBR_LOG_THRESH_CALC, 0, 2531 0, &log, false, &bbr->rc_tv); 2532 } 2533 } 2534 2535 static void 2536 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed) 2537 { 2538 if (tcp_bblogging_on(bbr->rc_tp)) { 2539 union tcp_log_stackspecific log; 2540 2541 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2542 log.u_bbr.flex1 = line; 2543 log.u_bbr.flex2 = bbr->bbr_timer_src; 2544 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2545 log.u_bbr.flex4 = bbr->rc_in_persist; 2546 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2547 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2548 log.u_bbr.flex8 = hpts_removed; 2549 log.u_bbr.pkts_out = bbr->rc_pacer_started; 2550 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2551 &bbr->rc_inp->inp_socket->so_rcv, 2552 &bbr->rc_inp->inp_socket->so_snd, 2553 BBR_LOG_TIMERCANC, 0, 2554 0, &log, false, &bbr->rc_tv); 2555 } 2556 } 2557 2558 static void 2559 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta) 2560 { 2561 if (tcp_bblogging_on(bbr->rc_tp)) { 2562 union tcp_log_stackspecific log; 2563 2564 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2565 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio; 2566 log.u_bbr.flex2 = (peer_delta >> 32); 2567 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff); 2568 log.u_bbr.flex4 = (delta >> 32); 2569 log.u_bbr.flex5 = (delta & 0x00000000ffffffff); 2570 log.u_bbr.flex7 = bbr->rc_ts_clock_set; 2571 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used; 2572 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2573 &bbr->rc_inp->inp_socket->so_rcv, 2574 &bbr->rc_inp->inp_socket->so_snd, 2575 BBR_LOG_TSTMP_VAL, 0, 2576 0, &log, false, &bbr->rc_tv); 2577 } 2578 } 2579 2580 static void 2581 bbr_log_type_tsosize(struct tcp_bbr *bbr, uint32_t cts, uint32_t tsosz, uint32_t tls, uint32_t old_val, uint32_t maxseg, int hdwr) 2582 { 2583 if (tcp_bblogging_on(bbr->rc_tp)) { 2584 union tcp_log_stackspecific log; 2585 2586 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2587 log.u_bbr.flex1 = tsosz; 2588 log.u_bbr.flex2 = tls; 2589 log.u_bbr.flex3 = tcp_min_hptsi_time; 2590 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min; 2591 log.u_bbr.flex5 = old_val; 2592 log.u_bbr.flex6 = maxseg; 2593 log.u_bbr.flex7 = bbr->rc_no_pacing; 2594 log.u_bbr.flex7 <<= 1; 2595 log.u_bbr.flex7 |= bbr->rc_past_init_win; 2596 if (hdwr) 2597 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google; 2598 else 2599 log.u_bbr.flex8 = bbr->rc_use_google; 2600 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2601 &bbr->rc_inp->inp_socket->so_rcv, 2602 &bbr->rc_inp->inp_socket->so_snd, 2603 BBR_LOG_BBRTSO, 0, 2604 0, &log, false, &bbr->rc_tv); 2605 } 2606 } 2607 2608 static void 2609 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, 2610 uint32_t flags, uint32_t line) 2611 { 2612 if (tcp_bblogging_on(bbr->rc_tp)) { 2613 union tcp_log_stackspecific log; 2614 2615 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2616 log.u_bbr.flex1 = line; 2617 log.u_bbr.flex2 = rsm->r_start; 2618 log.u_bbr.flex3 = rsm->r_end; 2619 log.u_bbr.flex4 = rsm->r_delivered; 2620 log.u_bbr.flex5 = rsm->r_rtr_cnt; 2621 log.u_bbr.flex6 = rsm->r_dupack; 2622 log.u_bbr.flex7 = rsm->r_tim_lastsent[0]; 2623 log.u_bbr.flex8 = rsm->r_flags; 2624 /* Hijack the pkts_out fids */ 2625 log.u_bbr.applimited = flags; 2626 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2627 &bbr->rc_inp->inp_socket->so_rcv, 2628 &bbr->rc_inp->inp_socket->so_snd, 2629 BBR_RSM_CLEARED, 0, 2630 0, &log, false, &bbr->rc_tv); 2631 } 2632 } 2633 2634 static void 2635 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts, 2636 uint32_t flex3, uint32_t flex2, uint32_t flex5, 2637 uint32_t flex6, uint32_t pkts_out, int flex7, 2638 uint32_t flex4, uint32_t flex1) 2639 { 2640 2641 if (tcp_bblogging_on(bbr->rc_tp)) { 2642 union tcp_log_stackspecific log; 2643 2644 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2645 log.u_bbr.flex1 = flex1; 2646 log.u_bbr.flex2 = flex2; 2647 log.u_bbr.flex3 = flex3; 2648 log.u_bbr.flex4 = flex4; 2649 log.u_bbr.flex5 = flex5; 2650 log.u_bbr.flex6 = flex6; 2651 log.u_bbr.flex7 = flex7; 2652 /* Hijack the pkts_out fids */ 2653 log.u_bbr.pkts_out = pkts_out; 2654 log.u_bbr.flex8 = flex8; 2655 if (bbr->rc_ack_was_delayed) 2656 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay; 2657 else 2658 log.u_bbr.epoch = 0; 2659 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2660 &bbr->rc_inp->inp_socket->so_rcv, 2661 &bbr->rc_inp->inp_socket->so_snd, 2662 BBR_LOG_BBRUPD, 0, 2663 flex2, &log, false, &bbr->rc_tv); 2664 } 2665 } 2666 2667 static void 2668 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason, 2669 uint32_t newbw, uint32_t obw, uint32_t diff, 2670 uint32_t tim) 2671 { 2672 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) { 2673 union tcp_log_stackspecific log; 2674 2675 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2676 log.u_bbr.flex1 = reason; 2677 log.u_bbr.flex2 = newbw; 2678 log.u_bbr.flex3 = obw; 2679 log.u_bbr.flex4 = diff; 2680 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost; 2681 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del; 2682 log.u_bbr.flex7 = bbr->rc_lt_is_sampling; 2683 log.u_bbr.pkts_out = tim; 2684 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw; 2685 if (bbr->rc_lt_use_bw == 0) 2686 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 2687 else 2688 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 2689 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2690 &bbr->rc_inp->inp_socket->so_rcv, 2691 &bbr->rc_inp->inp_socket->so_snd, 2692 BBR_LOG_BWSAMP, 0, 2693 0, &log, false, &bbr->rc_tv); 2694 } 2695 } 2696 2697 static inline void 2698 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line) 2699 { 2700 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2701 union tcp_log_stackspecific log; 2702 2703 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2704 log.u_bbr.flex1 = line; 2705 log.u_bbr.flex2 = tick; 2706 log.u_bbr.flex3 = tp->t_maxunacktime; 2707 log.u_bbr.flex4 = tp->t_acktime; 2708 log.u_bbr.flex8 = event; 2709 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2710 &bbr->rc_inp->inp_socket->so_rcv, 2711 &bbr->rc_inp->inp_socket->so_snd, 2712 BBR_LOG_PROGRESS, 0, 2713 0, &log, false, &bbr->rc_tv); 2714 } 2715 } 2716 2717 static void 2718 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp, 2719 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts, 2720 int error) 2721 { 2722 if (tcp_bblogging_on(bbr->rc_tp)) { 2723 union tcp_log_stackspecific log; 2724 2725 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2726 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2727 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2728 log.u_bbr.flex3 = (((uint64_t)ifp >> 32) & 0x00000000ffffffff); 2729 log.u_bbr.flex4 = ((uint64_t)ifp & 0x00000000ffffffff); 2730 log.u_bbr.bw_inuse = rate; 2731 log.u_bbr.flex5 = line; 2732 log.u_bbr.flex6 = error; 2733 log.u_bbr.flex8 = bbr->skip_gain; 2734 log.u_bbr.flex8 <<= 1; 2735 log.u_bbr.flex8 |= bbr->gain_is_limited; 2736 log.u_bbr.flex8 <<= 1; 2737 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing; 2738 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 2739 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2740 &bbr->rc_inp->inp_socket->so_rcv, 2741 &bbr->rc_inp->inp_socket->so_snd, 2742 BBR_LOG_HDWR_PACE, 0, 2743 0, &log, false, &bbr->rc_tv); 2744 } 2745 } 2746 2747 static void 2748 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, uint32_t del_by, uint32_t cts, uint32_t line, uint32_t prev_delay) 2749 { 2750 if (tcp_bblogging_on(bbr->rc_tp)) { 2751 union tcp_log_stackspecific log; 2752 2753 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2754 log.u_bbr.flex1 = slot; 2755 log.u_bbr.flex2 = del_by; 2756 log.u_bbr.flex3 = prev_delay; 2757 log.u_bbr.flex4 = line; 2758 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val; 2759 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay; 2760 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags); 2761 log.u_bbr.flex8 = bbr->rc_in_persist; 2762 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2763 &bbr->rc_inp->inp_socket->so_rcv, 2764 &bbr->rc_inp->inp_socket->so_snd, 2765 BBR_LOG_BBRSND, 0, 2766 len, &log, false, &bbr->rc_tv); 2767 } 2768 } 2769 2770 static void 2771 bbr_log_type_bbrrttprop(struct tcp_bbr *bbr, uint32_t t, uint32_t end, uint32_t tsconv, uint32_t cts, int32_t match, uint32_t seq, uint8_t flags) 2772 { 2773 if (tcp_bblogging_on(bbr->rc_tp)) { 2774 union tcp_log_stackspecific log; 2775 2776 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2777 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered; 2778 log.u_bbr.flex2 = 0; 2779 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt; 2780 log.u_bbr.flex4 = end; 2781 log.u_bbr.flex5 = seq; 2782 log.u_bbr.flex6 = t; 2783 log.u_bbr.flex7 = match; 2784 log.u_bbr.flex8 = flags; 2785 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2786 &bbr->rc_inp->inp_socket->so_rcv, 2787 &bbr->rc_inp->inp_socket->so_snd, 2788 BBR_LOG_BBRRTT, 0, 2789 0, &log, false, &bbr->rc_tv); 2790 } 2791 } 2792 2793 static void 2794 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method) 2795 { 2796 if (tcp_bblogging_on(bbr->rc_tp)) { 2797 union tcp_log_stackspecific log; 2798 2799 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2800 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2801 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 2802 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch; 2803 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2804 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs; 2805 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight; 2806 log.u_bbr.flex7 = 0; 2807 log.u_bbr.flex8 = entry_method; 2808 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2809 &bbr->rc_inp->inp_socket->so_rcv, 2810 &bbr->rc_inp->inp_socket->so_snd, 2811 BBR_LOG_EXIT_GAIN, 0, 2812 0, &log, false, &bbr->rc_tv); 2813 } 2814 } 2815 2816 static void 2817 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired) 2818 { 2819 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2820 union tcp_log_stackspecific log; 2821 2822 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2823 /* R-HU */ 2824 log.u_bbr.flex1 = 0; 2825 log.u_bbr.flex2 = 0; 2826 log.u_bbr.flex3 = 0; 2827 log.u_bbr.flex4 = 0; 2828 log.u_bbr.flex7 = 0; 2829 log.u_bbr.flex8 = settings_desired; 2830 2831 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2832 &bbr->rc_inp->inp_socket->so_rcv, 2833 &bbr->rc_inp->inp_socket->so_snd, 2834 BBR_LOG_SETTINGS_CHG, 0, 2835 0, &log, false, &bbr->rc_tv); 2836 } 2837 } 2838 2839 /* 2840 * Returns the bw from the our filter. 2841 */ 2842 static inline uint64_t 2843 bbr_get_full_bw(struct tcp_bbr *bbr) 2844 { 2845 uint64_t bw; 2846 2847 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2848 2849 return (bw); 2850 } 2851 2852 static inline void 2853 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2854 { 2855 uint64_t calclr; 2856 uint32_t lost, del; 2857 2858 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch) 2859 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch; 2860 else 2861 lost = 0; 2862 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del; 2863 if (lost == 0) { 2864 calclr = 0; 2865 } else if (del) { 2866 calclr = lost; 2867 calclr *= (uint64_t)1000; 2868 calclr /= (uint64_t)del; 2869 } else { 2870 /* Nothing delivered? 100.0% loss */ 2871 calclr = 1000; 2872 } 2873 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr; 2874 if (IN_RECOVERY(bbr->rc_tp->t_flags)) 2875 bbr->r_ctl.recovery_lr += (uint32_t)calclr; 2876 bbr->r_ctl.rc_pkt_epoch++; 2877 if (bbr->rc_no_pacing && 2878 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) { 2879 bbr->rc_no_pacing = 0; 2880 tcp_bbr_tso_size_check(bbr, cts); 2881 } 2882 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time); 2883 bbr->r_ctl.rc_pkt_epoch_time = cts; 2884 /* What was our loss rate */ 2885 bbr_log_pkt_epoch(bbr, cts, line, lost, del); 2886 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered; 2887 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost; 2888 } 2889 2890 static inline void 2891 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2892 { 2893 uint32_t epoch_time; 2894 2895 /* Tick the RTT clock */ 2896 bbr->r_ctl.rc_rtt_epoch++; 2897 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start; 2898 bbr_log_time_epoch(bbr, cts, line, epoch_time); 2899 bbr->r_ctl.rc_rcv_epoch_start = cts; 2900 } 2901 2902 static inline void 2903 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked) 2904 { 2905 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) { 2906 bbr->rc_is_pkt_epoch_now = 1; 2907 } 2908 } 2909 2910 /* 2911 * Returns the bw from either the b/w filter 2912 * or from the lt_bw (if the connection is being 2913 * policed). 2914 */ 2915 static inline uint64_t 2916 __bbr_get_bw(struct tcp_bbr *bbr) 2917 { 2918 uint64_t bw, min_bw; 2919 uint64_t rtt; 2920 int gm_measure_cnt = 1; 2921 2922 /* 2923 * For startup we make, like google, a 2924 * minimum b/w. This is generated from the 2925 * IW and the rttProp. We do fall back to srtt 2926 * if for some reason (initial handshake) we don't 2927 * have a rttProp. We, in the worst case, fall back 2928 * to the configured min_bw (rc_initial_hptsi_bw). 2929 */ 2930 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 2931 /* Attempt first to use rttProp */ 2932 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2933 if (rtt && (rtt < 0xffffffff)) { 2934 measure: 2935 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2936 ((uint64_t)1000000); 2937 min_bw /= rtt; 2938 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2939 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2940 } 2941 2942 } else if (bbr->rc_tp->t_srtt != 0) { 2943 /* No rttProp, use srtt? */ 2944 rtt = bbr_get_rtt(bbr, BBR_SRTT); 2945 goto measure; 2946 } else { 2947 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2948 } 2949 } else 2950 min_bw = 0; 2951 2952 if ((bbr->rc_past_init_win == 0) && 2953 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp))) 2954 bbr->rc_past_init_win = 1; 2955 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1)) 2956 gm_measure_cnt = 0; 2957 if (gm_measure_cnt && 2958 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) || 2959 (bbr->rc_past_init_win == 0))) { 2960 /* For google we use our guess rate until we get 1 measurement */ 2961 2962 use_initial_window: 2963 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2964 if (rtt && (rtt < 0xffffffff)) { 2965 /* 2966 * We have an RTT measurement. Use that in 2967 * combination with our initial window to calculate 2968 * a b/w. 2969 */ 2970 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2971 ((uint64_t)1000000); 2972 bw /= rtt; 2973 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2974 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2975 } 2976 } else { 2977 /* Drop back to the 40 and punt to a default */ 2978 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2979 } 2980 if (bw < 1) 2981 /* Probably should panic */ 2982 bw = 1; 2983 if (bw > min_bw) 2984 return (bw); 2985 else 2986 return (min_bw); 2987 } 2988 if (bbr->rc_lt_use_bw) 2989 bw = bbr->r_ctl.rc_lt_bw; 2990 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0)) 2991 bw = bbr->r_ctl.red_bw; 2992 else 2993 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2994 if (bbr->rc_tp->t_peakrate_thr && (bbr->rc_use_google == 0)) { 2995 /* 2996 * Enforce user set rate limit, keep in mind that 2997 * t_peakrate_thr is in B/s already 2998 */ 2999 bw = uqmin((uint64_t)bbr->rc_tp->t_peakrate_thr, bw); 3000 } 3001 if (bw == 0) { 3002 /* We should not be at 0, go to the initial window then */ 3003 goto use_initial_window; 3004 } 3005 if (bw < 1) 3006 /* Probably should panic */ 3007 bw = 1; 3008 if (bw < min_bw) 3009 bw = min_bw; 3010 return (bw); 3011 } 3012 3013 static inline uint64_t 3014 bbr_get_bw(struct tcp_bbr *bbr) 3015 { 3016 uint64_t bw; 3017 3018 bw = __bbr_get_bw(bbr); 3019 return (bw); 3020 } 3021 3022 static inline void 3023 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts) 3024 { 3025 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch; 3026 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time; 3027 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3028 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3029 } 3030 3031 static inline void 3032 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts) 3033 { 3034 bbr->rc_lt_is_sampling = 0; 3035 bbr->rc_lt_use_bw = 0; 3036 bbr->r_ctl.rc_lt_bw = 0; 3037 bbr_reset_lt_bw_interval(bbr, cts); 3038 } 3039 3040 static inline void 3041 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin) 3042 { 3043 uint64_t diff; 3044 3045 /* Do we have a previous sample? */ 3046 if (bbr->r_ctl.rc_lt_bw) { 3047 /* Get the diff in bytes per second */ 3048 if (bbr->r_ctl.rc_lt_bw > bw) 3049 diff = bbr->r_ctl.rc_lt_bw - bw; 3050 else 3051 diff = bw - bbr->r_ctl.rc_lt_bw; 3052 if ((diff <= bbr_lt_bw_diff) || 3053 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) { 3054 /* Consider us policed */ 3055 uint32_t saved_bw; 3056 3057 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw; 3058 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */ 3059 bbr->rc_lt_use_bw = 1; 3060 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 3061 /* 3062 * Use pkt based epoch for measuring length of 3063 * policer up 3064 */ 3065 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch; 3066 /* 3067 * reason 4 is we need to start consider being 3068 * policed 3069 */ 3070 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin); 3071 return; 3072 } 3073 } 3074 bbr->r_ctl.rc_lt_bw = bw; 3075 bbr_reset_lt_bw_interval(bbr, cts); 3076 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin); 3077 } 3078 3079 static void 3080 bbr_randomize_extra_state_time(struct tcp_bbr *bbr) 3081 { 3082 uint32_t ran, deduct; 3083 3084 ran = arc4random_uniform(bbr_rand_ot); 3085 if (ran) { 3086 deduct = bbr->r_ctl.rc_level_state_extra / ran; 3087 bbr->r_ctl.rc_level_state_extra -= deduct; 3088 } 3089 } 3090 /* 3091 * Return randomly the starting state 3092 * to use in probebw. 3093 */ 3094 static uint8_t 3095 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts) 3096 { 3097 uint32_t ran; 3098 uint8_t ret_val; 3099 3100 /* Initialize the offset to 0 */ 3101 bbr->r_ctl.rc_exta_time_gd = 0; 3102 bbr->rc_hit_state_1 = 0; 3103 bbr->r_ctl.rc_level_state_extra = 0; 3104 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1)); 3105 /* 3106 * The math works funny here :) the return value is used to set the 3107 * substate and then the state change is called which increments by 3108 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when 3109 * we fully enter the state. Note that the (8 - 1 - ran) assures that 3110 * we return 1 - 7, so we dont return 0 and end up starting in 3111 * state 1 (DRAIN). 3112 */ 3113 ret_val = BBR_SUBSTATE_COUNT - 1 - ran; 3114 /* Set an epoch */ 3115 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) 3116 bbr_set_epoch(bbr, cts, __LINE__); 3117 3118 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 3119 return (ret_val); 3120 } 3121 3122 static void 3123 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected) 3124 { 3125 uint32_t diff, d_time; 3126 uint64_t del_time, bw, lost, delivered; 3127 3128 if (bbr->r_use_policer == 0) 3129 return; 3130 if (bbr->rc_lt_use_bw) { 3131 /* We are using lt bw do we stop yet? */ 3132 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 3133 if (diff > bbr_lt_bw_max_rtts) { 3134 /* Reset it all */ 3135 reset_all: 3136 bbr_reset_lt_bw_sampling(bbr, cts); 3137 if (bbr->rc_filled_pipe) { 3138 bbr_set_epoch(bbr, cts, __LINE__); 3139 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 3140 bbr_substate_change(bbr, cts, __LINE__, 0); 3141 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 3142 bbr_log_type_statechange(bbr, cts, __LINE__); 3143 } else { 3144 /* 3145 * This should not happen really 3146 * unless we remove the startup/drain 3147 * restrictions above. 3148 */ 3149 bbr->rc_bbr_state = BBR_STATE_STARTUP; 3150 bbr_set_epoch(bbr, cts, __LINE__); 3151 bbr->r_ctl.rc_bbr_state_time = cts; 3152 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 3153 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 3154 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 3155 bbr_set_state_target(bbr, __LINE__); 3156 bbr_log_type_statechange(bbr, cts, __LINE__); 3157 } 3158 /* reason 0 is to stop using lt-bw */ 3159 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0); 3160 return; 3161 } 3162 if (bbr_lt_intvl_fp == 0) { 3163 /* Not doing false-positive detection */ 3164 return; 3165 } 3166 /* False positive detection */ 3167 if (diff == bbr_lt_intvl_fp) { 3168 /* At bbr_lt_intvl_fp we record the lost */ 3169 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3170 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3171 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) { 3172 /* Now is our loss rate still high? */ 3173 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3174 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3175 if ((delivered == 0) || 3176 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) { 3177 /* No still below our threshold */ 3178 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0); 3179 } else { 3180 /* Yikes its still high, it must be a false positive */ 3181 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0); 3182 goto reset_all; 3183 } 3184 } 3185 return; 3186 } 3187 /* 3188 * Wait for the first loss before sampling, to let the policer 3189 * exhaust its tokens and estimate the steady-state rate allowed by 3190 * the policer. Starting samples earlier includes bursts that 3191 * over-estimate the bw. 3192 */ 3193 if (bbr->rc_lt_is_sampling == 0) { 3194 /* reason 1 is to begin doing the sampling */ 3195 if (loss_detected == 0) 3196 return; 3197 bbr_reset_lt_bw_interval(bbr, cts); 3198 bbr->rc_lt_is_sampling = 1; 3199 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0); 3200 return; 3201 } 3202 /* Now how long were we delivering long term last> */ 3203 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time)) 3204 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time; 3205 else 3206 d_time = 0; 3207 3208 /* To avoid underestimates, reset sampling if we run out of data. */ 3209 if (bbr->r_ctl.r_app_limited_until) { 3210 /* Can not measure in app-limited state */ 3211 bbr_reset_lt_bw_sampling(bbr, cts); 3212 /* reason 2 is to reset sampling due to app limits */ 3213 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time); 3214 return; 3215 } 3216 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 3217 if (diff < bbr_lt_intvl_min_rtts) { 3218 /* 3219 * need more samples (we don't 3220 * start on a round like linux so 3221 * we need 1 more). 3222 */ 3223 /* 6 is not_enough time or no-loss */ 3224 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3225 return; 3226 } 3227 if (diff > (4 * bbr_lt_intvl_min_rtts)) { 3228 /* 3229 * For now if we wait too long, reset all sampling. We need 3230 * to do some research here, its possible that we should 3231 * base this on how much loss as occurred.. something like 3232 * if its under 10% (or some thresh) reset all otherwise 3233 * don't. Thats for phase II I guess. 3234 */ 3235 bbr_reset_lt_bw_sampling(bbr, cts); 3236 /* reason 3 is to reset sampling due too long of sampling */ 3237 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3238 return; 3239 } 3240 /* 3241 * End sampling interval when a packet is lost, so we estimate the 3242 * policer tokens were exhausted. Stopping the sampling before the 3243 * tokens are exhausted under-estimates the policed rate. 3244 */ 3245 if (loss_detected == 0) { 3246 /* 6 is not_enough time or no-loss */ 3247 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3248 return; 3249 } 3250 /* Calculate packets lost and delivered in sampling interval. */ 3251 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3252 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3253 if ((delivered == 0) || 3254 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) { 3255 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time); 3256 return; 3257 } 3258 if (d_time < 1000) { 3259 /* Not enough time. wait */ 3260 /* 6 is not_enough time or no-loss */ 3261 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3262 return; 3263 } 3264 if (d_time >= (0xffffffff / USECS_IN_MSEC)) { 3265 /* Too long */ 3266 bbr_reset_lt_bw_sampling(bbr, cts); 3267 /* reason 3 is to reset sampling due too long of sampling */ 3268 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3269 return; 3270 } 3271 del_time = d_time; 3272 bw = delivered; 3273 bw *= (uint64_t)USECS_IN_SECOND; 3274 bw /= del_time; 3275 bbr_lt_bw_samp_done(bbr, bw, cts, d_time); 3276 } 3277 3278 /* 3279 * Allocate a sendmap from our zone. 3280 */ 3281 static struct bbr_sendmap * 3282 bbr_alloc(struct tcp_bbr *bbr) 3283 { 3284 struct bbr_sendmap *rsm; 3285 3286 BBR_STAT_INC(bbr_to_alloc); 3287 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO)); 3288 if (rsm) { 3289 bbr->r_ctl.rc_num_maps_alloced++; 3290 return (rsm); 3291 } 3292 if (bbr->r_ctl.rc_free_cnt) { 3293 BBR_STAT_INC(bbr_to_alloc_emerg); 3294 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 3295 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 3296 bbr->r_ctl.rc_free_cnt--; 3297 return (rsm); 3298 } 3299 BBR_STAT_INC(bbr_to_alloc_failed); 3300 return (NULL); 3301 } 3302 3303 static struct bbr_sendmap * 3304 bbr_alloc_full_limit(struct tcp_bbr *bbr) 3305 { 3306 if ((V_tcp_map_entries_limit > 0) && 3307 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3308 BBR_STAT_INC(bbr_alloc_limited); 3309 if (!bbr->alloc_limit_reported) { 3310 bbr->alloc_limit_reported = 1; 3311 BBR_STAT_INC(bbr_alloc_limited_conns); 3312 } 3313 return (NULL); 3314 } 3315 return (bbr_alloc(bbr)); 3316 } 3317 3318 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3319 static struct bbr_sendmap * 3320 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type) 3321 { 3322 struct bbr_sendmap *rsm; 3323 3324 if (limit_type) { 3325 /* currently there is only one limit type */ 3326 if (V_tcp_map_split_limit > 0 && 3327 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3328 BBR_STAT_INC(bbr_split_limited); 3329 if (!bbr->alloc_limit_reported) { 3330 bbr->alloc_limit_reported = 1; 3331 BBR_STAT_INC(bbr_alloc_limited_conns); 3332 } 3333 return (NULL); 3334 } 3335 } 3336 3337 /* allocate and mark in the limit type, if set */ 3338 rsm = bbr_alloc(bbr); 3339 if (rsm != NULL && limit_type) { 3340 rsm->r_limit_type = limit_type; 3341 bbr->r_ctl.rc_num_split_allocs++; 3342 } 3343 return (rsm); 3344 } 3345 3346 static void 3347 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 3348 { 3349 if (rsm->r_limit_type) { 3350 /* currently there is only one limit type */ 3351 bbr->r_ctl.rc_num_split_allocs--; 3352 } 3353 if (rsm->r_is_smallmap) 3354 bbr->r_ctl.rc_num_small_maps_alloced--; 3355 if (bbr->r_ctl.rc_tlp_send == rsm) 3356 bbr->r_ctl.rc_tlp_send = NULL; 3357 if (bbr->r_ctl.rc_resend == rsm) { 3358 bbr->r_ctl.rc_resend = NULL; 3359 } 3360 if (bbr->r_ctl.rc_next == rsm) 3361 bbr->r_ctl.rc_next = NULL; 3362 if (bbr->r_ctl.rc_sacklast == rsm) 3363 bbr->r_ctl.rc_sacklast = NULL; 3364 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 3365 memset(rsm, 0, sizeof(struct bbr_sendmap)); 3366 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 3367 rsm->r_limit_type = 0; 3368 bbr->r_ctl.rc_free_cnt++; 3369 return; 3370 } 3371 bbr->r_ctl.rc_num_maps_alloced--; 3372 uma_zfree(bbr_zone, rsm); 3373 } 3374 3375 /* 3376 * Returns the BDP. 3377 */ 3378 static uint64_t 3379 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) { 3380 /* 3381 * Calculate the bytes in flight needed given the bw (in bytes per 3382 * second) and the specifyed rtt in useconds. We need to put out the 3383 * returned value per RTT to match that rate. Gain will normally 3384 * raise it up from there. 3385 * 3386 * This should not overflow as long as the bandwidth is below 1 3387 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller 3388 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30). 3389 */ 3390 uint64_t usec_per_sec; 3391 3392 usec_per_sec = USECS_IN_SECOND; 3393 return ((rtt * bw) / usec_per_sec); 3394 } 3395 3396 /* 3397 * Return the initial cwnd. 3398 */ 3399 static uint32_t 3400 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp) 3401 { 3402 uint32_t i_cwnd; 3403 3404 if (bbr->rc_init_win) { 3405 i_cwnd = bbr->rc_init_win * tp->t_maxseg; 3406 } else if (V_tcp_initcwnd_segments) 3407 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg), 3408 max(2 * tp->t_maxseg, 14600)); 3409 else if (V_tcp_do_rfc3390) 3410 i_cwnd = min(4 * tp->t_maxseg, 3411 max(2 * tp->t_maxseg, 4380)); 3412 else { 3413 /* Per RFC5681 Section 3.1 */ 3414 if (tp->t_maxseg > 2190) 3415 i_cwnd = 2 * tp->t_maxseg; 3416 else if (tp->t_maxseg > 1095) 3417 i_cwnd = 3 * tp->t_maxseg; 3418 else 3419 i_cwnd = 4 * tp->t_maxseg; 3420 } 3421 return (i_cwnd); 3422 } 3423 3424 /* 3425 * Given a specified gain, return the target 3426 * cwnd based on that gain. 3427 */ 3428 static uint32_t 3429 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw) 3430 { 3431 uint64_t bdp, rtt; 3432 uint32_t cwnd; 3433 3434 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) || 3435 (bbr_get_full_bw(bbr) == 0)) { 3436 /* No measurements yet */ 3437 return (bbr_initial_cwnd(bbr, bbr->rc_tp)); 3438 } 3439 /* 3440 * Get bytes per RTT needed (rttProp is normally in 3441 * bbr_cwndtarget_rtt_touse) 3442 */ 3443 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse); 3444 /* Get the bdp from the two values */ 3445 bdp = bbr_get_bw_delay_prod(rtt, bw); 3446 /* Now apply the gain */ 3447 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT)); 3448 3449 return (cwnd); 3450 } 3451 3452 static uint32_t 3453 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain) 3454 { 3455 uint32_t cwnd, mss; 3456 3457 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 3458 /* Get the base cwnd with gain rounded to a mss */ 3459 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss); 3460 /* 3461 * Add in N (2 default since we do not have a 3462 * fq layer to trap packets in) quanta's per the I-D 3463 * section 4.2.3.2 quanta adjust. 3464 */ 3465 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs); 3466 if (bbr->rc_use_google) { 3467 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3468 (bbr_state_val(bbr) == BBR_SUB_GAIN)) { 3469 /* 3470 * The linux implementation adds 3471 * an extra 2 x mss in gain cycle which 3472 * is documented no-where except in the code. 3473 * so we add more for Neal undocumented feature 3474 */ 3475 cwnd += 2 * mss; 3476 } 3477 if ((cwnd / mss) & 0x1) { 3478 /* Round up for odd num mss */ 3479 cwnd += mss; 3480 } 3481 } 3482 /* Are we below the min cwnd? */ 3483 if (cwnd < get_min_cwnd(bbr)) 3484 return (get_min_cwnd(bbr)); 3485 return (cwnd); 3486 } 3487 3488 static uint16_t 3489 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain) 3490 { 3491 if (gain < 1) 3492 gain = 1; 3493 return (gain); 3494 } 3495 3496 static uint32_t 3497 bbr_get_header_oh(struct tcp_bbr *bbr) 3498 { 3499 int seg_oh; 3500 3501 seg_oh = 0; 3502 if (bbr->r_ctl.rc_inc_tcp_oh) { 3503 /* Do we include TCP overhead? */ 3504 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr)); 3505 } 3506 if (bbr->r_ctl.rc_inc_ip_oh) { 3507 /* Do we include IP overhead? */ 3508 #ifdef INET6 3509 if (bbr->r_is_v6) { 3510 seg_oh += sizeof(struct ip6_hdr); 3511 } else 3512 #endif 3513 { 3514 3515 #ifdef INET 3516 seg_oh += sizeof(struct ip); 3517 #endif 3518 } 3519 } 3520 if (bbr->r_ctl.rc_inc_enet_oh) { 3521 /* Do we include the ethernet overhead? */ 3522 seg_oh += sizeof(struct ether_header); 3523 } 3524 return(seg_oh); 3525 } 3526 3527 static uint32_t 3528 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw) 3529 { 3530 uint64_t divor, res, tim; 3531 3532 if (useconds_time == 0) 3533 return (0); 3534 gain = bbr_gain_adjust(bbr, gain); 3535 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT; 3536 tim = useconds_time; 3537 res = (tim * bw * gain) / divor; 3538 if (res == 0) 3539 res = 1; 3540 return ((uint32_t)res); 3541 } 3542 3543 /* 3544 * Given a gain and a length return the delay in useconds that 3545 * should be used to evenly space out packets 3546 * on the connection (based on the gain factor). 3547 */ 3548 static uint32_t 3549 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog) 3550 { 3551 uint64_t bw, lentim, res; 3552 uint32_t usecs, srtt, over = 0; 3553 uint32_t seg_oh, num_segs, maxseg; 3554 3555 if (len == 0) 3556 return (0); 3557 3558 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 3559 num_segs = (len + maxseg - 1) / maxseg; 3560 if (bbr->rc_use_google == 0) { 3561 seg_oh = bbr_get_header_oh(bbr); 3562 len += (num_segs * seg_oh); 3563 } 3564 gain = bbr_gain_adjust(bbr, gain); 3565 bw = bbr_get_bw(bbr); 3566 if (bbr->rc_use_google) { 3567 uint64_t cbw; 3568 3569 /* 3570 * Reduce the b/w by the google discount 3571 * factor 10 = 1%. 3572 */ 3573 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount); 3574 cbw /= (uint64_t)1000; 3575 /* We don't apply a discount if it results in 0 */ 3576 if (cbw > 0) 3577 bw = cbw; 3578 } 3579 lentim = ((uint64_t)len * 3580 (uint64_t)USECS_IN_SECOND * 3581 (uint64_t)BBR_UNIT); 3582 res = lentim / ((uint64_t)gain * bw); 3583 if (res == 0) 3584 res = 1; 3585 usecs = (uint32_t)res; 3586 srtt = bbr_get_rtt(bbr, BBR_SRTT); 3587 if (bbr_hptsi_max_mul && bbr_hptsi_max_div && 3588 (bbr->rc_use_google == 0) && 3589 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) { 3590 /* 3591 * We cannot let the delay be more than 1/2 the srtt time. 3592 * Otherwise we cannot pace out or send properly. 3593 */ 3594 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div; 3595 BBR_STAT_INC(bbr_hpts_min_time); 3596 } 3597 if (!nolog) 3598 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1); 3599 return (usecs); 3600 } 3601 3602 static void 3603 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack, 3604 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses) 3605 { 3606 uint64_t bw; 3607 uint32_t cwnd, target_cwnd, saved_bytes, maxseg; 3608 int32_t meth; 3609 3610 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3611 3612 #ifdef STATS 3613 if ((tp->t_flags & TF_GPUTINPROG) && 3614 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 3615 /* 3616 * Strech acks and compressed acks will cause this to 3617 * oscillate but we are doing it the same way as the main 3618 * stack so it will be compariable (though possibly not 3619 * ideal). 3620 */ 3621 int32_t cgput; 3622 int64_t gput, time_stamp; 3623 3624 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8; 3625 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000)); 3626 cgput = gput / time_stamp; 3627 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 3628 cgput); 3629 if (tp->t_stats_gput_prev > 0) 3630 stats_voi_update_abs_s32(tp->t_stats, 3631 VOI_TCP_GPUT_ND, 3632 ((gput - tp->t_stats_gput_prev) * 100) / 3633 tp->t_stats_gput_prev); 3634 tp->t_flags &= ~TF_GPUTINPROG; 3635 tp->t_stats_gput_prev = cgput; 3636 } 3637 #endif 3638 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3639 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 3640 /* We don't change anything in probe-rtt */ 3641 return; 3642 } 3643 maxseg = tp->t_maxseg - bbr->rc_last_options; 3644 saved_bytes = bytes_this_ack; 3645 bytes_this_ack += sack_changed; 3646 if (bytes_this_ack > prev_acked) { 3647 bytes_this_ack -= prev_acked; 3648 /* 3649 * A byte ack'd gives us a full mss 3650 * to be like linux i.e. they count packets. 3651 */ 3652 if ((bytes_this_ack < maxseg) && bbr->rc_use_google) 3653 bytes_this_ack = maxseg; 3654 } else { 3655 /* Unlikely */ 3656 bytes_this_ack = 0; 3657 } 3658 cwnd = tp->snd_cwnd; 3659 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3660 if (bw) 3661 target_cwnd = bbr_get_target_cwnd(bbr, 3662 bw, 3663 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain); 3664 else 3665 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp); 3666 if (IN_RECOVERY(tp->t_flags) && 3667 (bbr->bbr_prev_in_rec == 0)) { 3668 /* 3669 * We are entering recovery and 3670 * thus packet conservation. 3671 */ 3672 bbr->pkt_conservation = 1; 3673 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime; 3674 cwnd = ctf_flight_size(tp, 3675 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3676 bytes_this_ack; 3677 } 3678 if (IN_RECOVERY(tp->t_flags)) { 3679 uint32_t flight; 3680 3681 bbr->bbr_prev_in_rec = 1; 3682 if (cwnd > losses) { 3683 cwnd -= losses; 3684 if (cwnd < maxseg) 3685 cwnd = maxseg; 3686 } else 3687 cwnd = maxseg; 3688 flight = ctf_flight_size(tp, 3689 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3690 bbr_log_type_cwndupd(bbr, flight, 0, 3691 losses, 10, 0, 0, line); 3692 if (bbr->pkt_conservation) { 3693 uint32_t time_in; 3694 3695 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start)) 3696 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start; 3697 else 3698 time_in = 0; 3699 3700 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 3701 /* Clear packet conservation after an rttProp */ 3702 bbr->pkt_conservation = 0; 3703 } else { 3704 if ((flight + bytes_this_ack) > cwnd) 3705 cwnd = flight + bytes_this_ack; 3706 if (cwnd < get_min_cwnd(bbr)) 3707 cwnd = get_min_cwnd(bbr); 3708 tp->snd_cwnd = cwnd; 3709 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, 3710 prev_acked, 1, target_cwnd, th->th_ack, line); 3711 return; 3712 } 3713 } 3714 } else 3715 bbr->bbr_prev_in_rec = 0; 3716 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) { 3717 bbr->r_ctl.restrict_growth--; 3718 if (bytes_this_ack > maxseg) 3719 bytes_this_ack = maxseg; 3720 } 3721 if (bbr->rc_filled_pipe) { 3722 /* 3723 * Here we have exited startup and filled the pipe. We will 3724 * thus allow the cwnd to shrink to the target. We hit here 3725 * mostly. 3726 */ 3727 uint32_t s_cwnd; 3728 3729 meth = 2; 3730 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd); 3731 if (s_cwnd > cwnd) 3732 cwnd = s_cwnd; 3733 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing) 3734 cwnd = s_cwnd; 3735 } else { 3736 /* 3737 * Here we are still in startup, we increase cwnd by what 3738 * has been acked. 3739 */ 3740 if ((cwnd < target_cwnd) || 3741 (bbr->rc_past_init_win == 0)) { 3742 meth = 3; 3743 cwnd += bytes_this_ack; 3744 } else { 3745 /* 3746 * Method 4 means we are at target so no gain in 3747 * startup and past the initial window. 3748 */ 3749 meth = 4; 3750 } 3751 } 3752 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr)); 3753 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line); 3754 } 3755 3756 static void 3757 tcp_bbr_partialack(struct tcpcb *tp) 3758 { 3759 struct tcp_bbr *bbr; 3760 3761 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3762 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3763 if (ctf_flight_size(tp, 3764 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 3765 tp->snd_cwnd) { 3766 bbr->r_wanted_output = 1; 3767 } 3768 } 3769 3770 static void 3771 bbr_post_recovery(struct tcpcb *tp) 3772 { 3773 struct tcp_bbr *bbr; 3774 uint32_t flight; 3775 3776 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3777 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3778 /* 3779 * Here we just exit recovery. 3780 */ 3781 EXIT_RECOVERY(tp->t_flags); 3782 /* Lock in our b/w reduction for the specified number of pkt-epochs */ 3783 bbr->r_recovery_bw = 0; 3784 tp->snd_recover = tp->snd_una; 3785 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3786 bbr->pkt_conservation = 0; 3787 if (bbr->rc_use_google == 0) { 3788 /* 3789 * For non-google mode lets 3790 * go ahead and make sure we clear 3791 * the recovery state so if we 3792 * bounce back in to recovery we 3793 * will do PC. 3794 */ 3795 bbr->bbr_prev_in_rec = 0; 3796 } 3797 bbr_log_type_exit_rec(bbr); 3798 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3799 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3800 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__); 3801 } else { 3802 /* For probe-rtt case lets fix up its saved_cwnd */ 3803 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) { 3804 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent; 3805 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__); 3806 } 3807 } 3808 flight = ctf_flight_size(tp, 3809 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3810 if ((bbr->rc_use_google == 0) && 3811 bbr_do_red) { 3812 uint64_t val, lr2use; 3813 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd; 3814 uint32_t *cwnd_p; 3815 3816 if (bbr_get_rtt(bbr, BBR_SRTT)) { 3817 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000); 3818 val /= bbr_get_rtt(bbr, BBR_SRTT); 3819 ratio = (uint32_t)val; 3820 } else 3821 ratio = 1000; 3822 3823 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, 3824 bbr->r_ctl.recovery_lr, 21, 3825 ratio, 3826 bbr->r_ctl.rc_red_cwnd_pe, 3827 __LINE__); 3828 if ((ratio < bbr_do_red) || (bbr_do_red == 0)) 3829 goto done; 3830 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3831 bbr_prtt_slam_cwnd) || 3832 (bbr_sub_drain_slam_cwnd && 3833 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3834 bbr->rc_hit_state_1 && 3835 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) || 3836 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) && 3837 bbr_slam_cwnd_in_main_drain)) { 3838 /* 3839 * Here we must poke at the saved cwnd 3840 * as well as the cwnd. 3841 */ 3842 cwnd = bbr->r_ctl.rc_saved_cwnd; 3843 cwnd_p = &bbr->r_ctl.rc_saved_cwnd; 3844 } else { 3845 cwnd = tp->snd_cwnd; 3846 cwnd_p = &tp->snd_cwnd; 3847 } 3848 maxseg = tp->t_maxseg - bbr->rc_last_options; 3849 /* Add the overall lr with the recovery lr */ 3850 if (bbr->r_ctl.rc_lost == 0) 3851 lr2use = 0; 3852 else if (bbr->r_ctl.rc_delivered == 0) 3853 lr2use = 1000; 3854 else { 3855 lr2use = bbr->r_ctl.rc_lost * 1000; 3856 lr2use /= bbr->r_ctl.rc_delivered; 3857 } 3858 lr2use += bbr->r_ctl.recovery_lr; 3859 acks_inflight = (flight / (maxseg * 2)); 3860 if (bbr_red_scale) { 3861 lr2use *= bbr_get_rtt(bbr, BBR_SRTT); 3862 lr2use /= bbr_red_scale; 3863 if ((bbr_red_growth_restrict) && 3864 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1)) 3865 bbr->r_ctl.restrict_growth += acks_inflight; 3866 } 3867 if (lr2use) { 3868 val = (uint64_t)cwnd * lr2use; 3869 val /= 1000; 3870 if (cwnd > val) 3871 newcwnd = roundup((cwnd - val), maxseg); 3872 else 3873 newcwnd = maxseg; 3874 } else { 3875 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul; 3876 val /= (uint64_t)bbr_red_div; 3877 newcwnd = roundup((uint32_t)val, maxseg); 3878 } 3879 /* with standard delayed acks how many acks can I expect? */ 3880 if (bbr_drop_limit == 0) { 3881 /* 3882 * Anticpate how much we will 3883 * raise the cwnd based on the acks. 3884 */ 3885 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) { 3886 /* We do enforce the min (with the acks) */ 3887 newcwnd = (get_min_cwnd(bbr) - acks_inflight); 3888 } 3889 } else { 3890 /* 3891 * A strict drop limit of N is inplace 3892 */ 3893 if (newcwnd < (bbr_drop_limit * maxseg)) { 3894 newcwnd = bbr_drop_limit * maxseg; 3895 } 3896 } 3897 /* For the next N acks do we restrict the growth */ 3898 *cwnd_p = newcwnd; 3899 if (tp->snd_cwnd > newcwnd) 3900 tp->snd_cwnd = newcwnd; 3901 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22, 3902 (uint32_t)lr2use, 3903 bbr_get_rtt(bbr, BBR_SRTT), __LINE__); 3904 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch; 3905 } 3906 done: 3907 bbr->r_ctl.recovery_lr = 0; 3908 if (flight <= tp->snd_cwnd) { 3909 bbr->r_wanted_output = 1; 3910 } 3911 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3912 } 3913 3914 static void 3915 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts) 3916 { 3917 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3918 /* Limit the drop in b/w to 1/2 our current filter. */ 3919 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate) 3920 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate; 3921 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2)) 3922 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2; 3923 tcp_bbr_tso_size_check(bbr, cts); 3924 } 3925 3926 static void 3927 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm) 3928 { 3929 struct tcp_bbr *bbr; 3930 3931 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3932 #ifdef STATS 3933 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 3934 #endif 3935 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3936 switch (type) { 3937 case CC_NDUPACK: 3938 if (!IN_RECOVERY(tp->t_flags)) { 3939 tp->snd_recover = tp->snd_max; 3940 /* Start a new epoch */ 3941 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 3942 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) { 3943 /* 3944 * Move forward the lt epoch 3945 * so it won't count the truncated 3946 * epoch. 3947 */ 3948 bbr->r_ctl.rc_lt_epoch++; 3949 } 3950 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 3951 /* 3952 * Just like the policer detection code 3953 * if we are in startup we must push 3954 * forward the last startup epoch 3955 * to hide the truncated PE. 3956 */ 3957 bbr->r_ctl.rc_bbr_last_startup_epoch++; 3958 } 3959 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd; 3960 ENTER_RECOVERY(tp->t_flags); 3961 bbr->rc_tlp_rtx_out = 0; 3962 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate; 3963 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3964 if (tcp_in_hpts(bbr->rc_inp) && 3965 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) { 3966 /* 3967 * When we enter recovery, we need to restart 3968 * any timers. This may mean we gain an agg 3969 * early, which will be made up for at the last 3970 * rxt out. 3971 */ 3972 bbr->rc_timer_first = 1; 3973 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 3974 } 3975 /* 3976 * Calculate a new cwnd based on to the current 3977 * delivery rate with no gain. We get the bdp 3978 * without gaining it up like we normally would and 3979 * we use the last cur_del_rate. 3980 */ 3981 if ((bbr->rc_use_google == 0) && 3982 (bbr->r_ctl.bbr_rttprobe_gain_val || 3983 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) { 3984 tp->snd_cwnd = ctf_flight_size(tp, 3985 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3986 (tp->t_maxseg - bbr->rc_last_options); 3987 if (tp->snd_cwnd < get_min_cwnd(bbr)) { 3988 /* We always gate to min cwnd */ 3989 tp->snd_cwnd = get_min_cwnd(bbr); 3990 } 3991 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__); 3992 } 3993 bbr_log_type_enter_rec(bbr, rsm->r_start); 3994 } 3995 break; 3996 case CC_RTO_ERR: 3997 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 3998 /* RTO was unnecessary, so reset everything. */ 3999 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime); 4000 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 4001 tp->snd_cwnd = tp->snd_cwnd_prev; 4002 tp->snd_ssthresh = tp->snd_ssthresh_prev; 4003 tp->snd_recover = tp->snd_recover_prev; 4004 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 4005 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__); 4006 } 4007 tp->t_badrxtwin = 0; 4008 break; 4009 } 4010 } 4011 4012 /* 4013 * Indicate whether this ack should be delayed. We can delay the ack if 4014 * following conditions are met: 4015 * - There is no delayed ack timer in progress. 4016 * - Our last ack wasn't a 0-sized window. We never want to delay 4017 * the ack that opens up a 0-sized window. 4018 * - LRO wasn't used for this segment. We make sure by checking that the 4019 * segment size is not larger than the MSS. 4020 * - Delayed acks are enabled or this is a half-synchronized T/TCP 4021 * connection. 4022 * - The data being acked is less than a full segment (a stretch ack 4023 * of more than a segment we should ack. 4024 * - nsegs is 1 (if its more than that we received more than 1 ack). 4025 */ 4026 #define DELAY_ACK(tp, bbr, nsegs) \ 4027 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4028 ((tp->t_flags & TF_DELACK) == 0) && \ 4029 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \ 4030 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 4031 4032 /* 4033 * Return the lowest RSM in the map of 4034 * packets still in flight that is not acked. 4035 * This should normally find on the first one 4036 * since we remove packets from the send 4037 * map after they are marked ACKED. 4038 */ 4039 static struct bbr_sendmap * 4040 bbr_find_lowest_rsm(struct tcp_bbr *bbr) 4041 { 4042 struct bbr_sendmap *rsm; 4043 4044 /* 4045 * Walk the time-order transmitted list looking for an rsm that is 4046 * not acked. This will be the one that was sent the longest time 4047 * ago that is still outstanding. 4048 */ 4049 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) { 4050 if (rsm->r_flags & BBR_ACKED) { 4051 continue; 4052 } 4053 goto finish; 4054 } 4055 finish: 4056 return (rsm); 4057 } 4058 4059 static struct bbr_sendmap * 4060 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 4061 { 4062 struct bbr_sendmap *prsm; 4063 4064 /* 4065 * Walk the sequence order list backward until we hit and arrive at 4066 * the highest seq not acked. In theory when this is called it 4067 * should be the last segment (which it was not). 4068 */ 4069 prsm = rsm; 4070 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4071 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) { 4072 continue; 4073 } 4074 return (prsm); 4075 } 4076 return (NULL); 4077 } 4078 4079 /* 4080 * Returns to the caller the number of microseconds that 4081 * the packet can be outstanding before we think we 4082 * should have had an ack returned. 4083 */ 4084 static uint32_t 4085 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm) 4086 { 4087 /* 4088 * lro is the flag we use to determine if we have seen reordering. 4089 * If it gets set we have seen reordering. The reorder logic either 4090 * works in one of two ways: 4091 * 4092 * If reorder-fade is configured, then we track the last time we saw 4093 * re-ordering occur. If we reach the point where enough time as 4094 * passed we no longer consider reordering has occuring. 4095 * 4096 * Or if reorder-face is 0, then once we see reordering we consider 4097 * the connection to alway be subject to reordering and just set lro 4098 * to 1. 4099 * 4100 * In the end if lro is non-zero we add the extra time for 4101 * reordering in. 4102 */ 4103 int32_t lro; 4104 uint32_t thresh, t_rxtcur; 4105 4106 if (srtt == 0) 4107 srtt = 1; 4108 if (bbr->r_ctl.rc_reorder_ts) { 4109 if (bbr->r_ctl.rc_reorder_fade) { 4110 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) { 4111 lro = cts - bbr->r_ctl.rc_reorder_ts; 4112 if (lro == 0) { 4113 /* 4114 * No time as passed since the last 4115 * reorder, mark it as reordering. 4116 */ 4117 lro = 1; 4118 } 4119 } else { 4120 /* Negative time? */ 4121 lro = 0; 4122 } 4123 if (lro > bbr->r_ctl.rc_reorder_fade) { 4124 /* Turn off reordering seen too */ 4125 bbr->r_ctl.rc_reorder_ts = 0; 4126 lro = 0; 4127 } 4128 } else { 4129 /* Reodering does not fade */ 4130 lro = 1; 4131 } 4132 } else { 4133 lro = 0; 4134 } 4135 thresh = srtt + bbr->r_ctl.rc_pkt_delay; 4136 if (lro) { 4137 /* It must be set, if not you get 1/4 rtt */ 4138 if (bbr->r_ctl.rc_reorder_shift) 4139 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift); 4140 else 4141 thresh += (srtt >> 2); 4142 } else { 4143 thresh += 1000; 4144 } 4145 /* We don't let the rack timeout be above a RTO */ 4146 if ((bbr->rc_tp)->t_srtt == 0) 4147 t_rxtcur = BBR_INITIAL_RTO; 4148 else 4149 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 4150 if (thresh > t_rxtcur) { 4151 thresh = t_rxtcur; 4152 } 4153 /* And we don't want it above the RTO max either */ 4154 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4155 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4156 } 4157 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK); 4158 return (thresh); 4159 } 4160 4161 /* 4162 * Return to the caller the amount of time in mico-seconds 4163 * that should be used for the TLP timer from the last 4164 * send time of this packet. 4165 */ 4166 static uint32_t 4167 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 4168 struct bbr_sendmap *rsm, uint32_t srtt, 4169 uint32_t cts) 4170 { 4171 uint32_t thresh, len, maxseg, t_rxtcur; 4172 struct bbr_sendmap *prsm; 4173 4174 if (srtt == 0) 4175 srtt = 1; 4176 if (bbr->rc_tlp_threshold) 4177 thresh = srtt + (srtt / bbr->rc_tlp_threshold); 4178 else 4179 thresh = (srtt * 2); 4180 maxseg = tp->t_maxseg - bbr->rc_last_options; 4181 /* Get the previous sent packet, if any */ 4182 len = rsm->r_end - rsm->r_start; 4183 4184 /* 2.1 behavior */ 4185 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext); 4186 if (prsm && (len <= maxseg)) { 4187 /* 4188 * Two packets outstanding, thresh should be (2*srtt) + 4189 * possible inter-packet delay (if any). 4190 */ 4191 uint32_t inter_gap = 0; 4192 int idx, nidx; 4193 4194 idx = rsm->r_rtr_cnt - 1; 4195 nidx = prsm->r_rtr_cnt - 1; 4196 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 4197 /* Yes it was sent later (or at the same time) */ 4198 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 4199 } 4200 thresh += inter_gap; 4201 } else if (len <= maxseg) { 4202 /* 4203 * Possibly compensate for delayed-ack. 4204 */ 4205 uint32_t alt_thresh; 4206 4207 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time; 4208 if (alt_thresh > thresh) 4209 thresh = alt_thresh; 4210 } 4211 /* Not above the current RTO */ 4212 if (tp->t_srtt == 0) 4213 t_rxtcur = BBR_INITIAL_RTO; 4214 else 4215 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur); 4216 4217 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP); 4218 /* Not above an RTO */ 4219 if (thresh > t_rxtcur) { 4220 thresh = t_rxtcur; 4221 } 4222 /* Not above a RTO max */ 4223 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4224 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4225 } 4226 /* And now apply the user TLP min */ 4227 if (thresh < bbr_tlp_min) { 4228 thresh = bbr_tlp_min; 4229 } 4230 return (thresh); 4231 } 4232 4233 /* 4234 * Return one of three RTTs to use (in microseconds). 4235 */ 4236 static __inline uint32_t 4237 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type) 4238 { 4239 uint32_t f_rtt; 4240 uint32_t srtt; 4241 4242 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 4243 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) { 4244 /* We have no rtt at all */ 4245 if (bbr->rc_tp->t_srtt == 0) 4246 f_rtt = BBR_INITIAL_RTO; 4247 else 4248 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4249 /* 4250 * Since we don't know how good the rtt is apply a 4251 * delayed-ack min 4252 */ 4253 if (f_rtt < bbr_delayed_ack_time) { 4254 f_rtt = bbr_delayed_ack_time; 4255 } 4256 } 4257 /* Take the filter version or last measured pkt-rtt */ 4258 if (rtt_type == BBR_RTT_PROP) { 4259 srtt = f_rtt; 4260 } else if (rtt_type == BBR_RTT_PKTRTT) { 4261 if (bbr->r_ctl.rc_pkt_epoch_rtt) { 4262 srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 4263 } else { 4264 /* No pkt rtt yet */ 4265 srtt = f_rtt; 4266 } 4267 } else if (rtt_type == BBR_RTT_RACK) { 4268 srtt = bbr->r_ctl.rc_last_rtt; 4269 /* We need to add in any internal delay for our timer */ 4270 if (bbr->rc_ack_was_delayed) 4271 srtt += bbr->r_ctl.rc_ack_hdwr_delay; 4272 } else if (rtt_type == BBR_SRTT) { 4273 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4274 } else { 4275 /* TSNH */ 4276 srtt = f_rtt; 4277 #ifdef BBR_INVARIANTS 4278 panic("Unknown rtt request type %d", rtt_type); 4279 #endif 4280 } 4281 return (srtt); 4282 } 4283 4284 static int 4285 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts) 4286 { 4287 uint32_t thresh; 4288 4289 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK), 4290 cts, rsm); 4291 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) { 4292 /* It is lost (past time) */ 4293 return (1); 4294 } 4295 return (0); 4296 } 4297 4298 /* 4299 * Return a sendmap if we need to retransmit something. 4300 */ 4301 static struct bbr_sendmap * 4302 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4303 { 4304 /* 4305 * Check to see that we don't need to fall into recovery. We will 4306 * need to do so if our oldest transmit is past the time we should 4307 * have had an ack. 4308 */ 4309 4310 struct bbr_sendmap *rsm; 4311 int32_t idx; 4312 4313 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) { 4314 /* Nothing outstanding that we know of */ 4315 return (NULL); 4316 } 4317 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 4318 if (rsm == NULL) { 4319 /* Nothing in the transmit map */ 4320 return (NULL); 4321 } 4322 if (tp->t_flags & TF_SENTFIN) { 4323 /* Fin restricted, don't find anything once a fin is sent */ 4324 return (NULL); 4325 } 4326 if (rsm->r_flags & BBR_ACKED) { 4327 /* 4328 * Ok the first one is acked (this really should not happen 4329 * since we remove the from the tmap once they are acked) 4330 */ 4331 rsm = bbr_find_lowest_rsm(bbr); 4332 if (rsm == NULL) 4333 return (NULL); 4334 } 4335 idx = rsm->r_rtr_cnt - 1; 4336 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) { 4337 /* Send timestamp is the same or less? can't be ready */ 4338 return (NULL); 4339 } 4340 /* Get our RTT time */ 4341 if (bbr_is_lost(bbr, rsm, cts) && 4342 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 4343 (rsm->r_flags & BBR_SACK_PASSED))) { 4344 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4345 rsm->r_flags |= BBR_MARKED_LOST; 4346 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4347 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4348 } 4349 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm); 4350 #ifdef BBR_INVARIANTS 4351 if ((rsm->r_end - rsm->r_start) == 0) 4352 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm); 4353 #endif 4354 return (rsm); 4355 } 4356 return (NULL); 4357 } 4358 4359 /* 4360 * RACK Timer, here we simply do logging and house keeping. 4361 * the normal bbr_output_wtime() function will call the 4362 * appropriate thing to check if we need to do a RACK retransmit. 4363 * We return 1, saying don't proceed with bbr_output_wtime only 4364 * when all timers have been stopped (destroyed PCB?). 4365 */ 4366 static int 4367 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4368 { 4369 /* 4370 * This timer simply provides an internal trigger to send out data. 4371 * The check_recovery_mode call will see if there are needed 4372 * retransmissions, if so we will enter fast-recovery. The output 4373 * call may or may not do the same thing depending on sysctl 4374 * settings. 4375 */ 4376 uint32_t lost; 4377 4378 if (bbr->rc_all_timers_stopped) { 4379 return (1); 4380 } 4381 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4382 /* Its not time yet */ 4383 return (0); 4384 } 4385 BBR_STAT_INC(bbr_to_tot); 4386 lost = bbr->r_ctl.rc_lost; 4387 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4388 bbr_set_state(tp, bbr, 0); 4389 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK); 4390 if (bbr->r_ctl.rc_resend == NULL) { 4391 /* Lets do the check here */ 4392 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 4393 } 4394 if (bbr_policer_call_from_rack_to) 4395 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4396 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 4397 return (0); 4398 } 4399 4400 static __inline void 4401 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start) 4402 { 4403 int idx; 4404 4405 nrsm->r_start = start; 4406 nrsm->r_end = rsm->r_end; 4407 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4408 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed; 4409 nrsm->r_flags = rsm->r_flags; 4410 /* We don't transfer forward the SYN flag */ 4411 nrsm->r_flags &= ~BBR_HAS_SYN; 4412 /* We move forward the FIN flag, not that this should happen */ 4413 rsm->r_flags &= ~BBR_HAS_FIN; 4414 nrsm->r_dupack = rsm->r_dupack; 4415 nrsm->r_rtr_bytes = 0; 4416 nrsm->r_is_gain = rsm->r_is_gain; 4417 nrsm->r_is_drain = rsm->r_is_drain; 4418 nrsm->r_delivered = rsm->r_delivered; 4419 nrsm->r_ts_valid = rsm->r_ts_valid; 4420 nrsm->r_del_ack_ts = rsm->r_del_ack_ts; 4421 nrsm->r_del_time = rsm->r_del_time; 4422 nrsm->r_app_limited = rsm->r_app_limited; 4423 nrsm->r_first_sent_time = rsm->r_first_sent_time; 4424 nrsm->r_flight_at_send = rsm->r_flight_at_send; 4425 /* We split a piece the lower section looses any just_ret flag. */ 4426 nrsm->r_bbr_state = rsm->r_bbr_state; 4427 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4428 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4429 } 4430 rsm->r_end = nrsm->r_start; 4431 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 4432 idx /= 8; 4433 /* Check if we got too small */ 4434 if ((rsm->r_is_smallmap == 0) && 4435 ((rsm->r_end - rsm->r_start) <= idx)) { 4436 bbr->r_ctl.rc_num_small_maps_alloced++; 4437 rsm->r_is_smallmap = 1; 4438 } 4439 /* Check the new one as well */ 4440 if ((nrsm->r_end - nrsm->r_start) <= idx) { 4441 bbr->r_ctl.rc_num_small_maps_alloced++; 4442 nrsm->r_is_smallmap = 1; 4443 } 4444 } 4445 4446 static int 4447 bbr_sack_mergable(struct bbr_sendmap *at, 4448 uint32_t start, uint32_t end) 4449 { 4450 /* 4451 * Given a sack block defined by 4452 * start and end, and a current position 4453 * at. Return 1 if either side of at 4454 * would show that the block is mergable 4455 * to that side. A block to be mergable 4456 * must have overlap with the start/end 4457 * and be in the SACK'd state. 4458 */ 4459 struct bbr_sendmap *l_rsm; 4460 struct bbr_sendmap *r_rsm; 4461 4462 /* first get the either side blocks */ 4463 l_rsm = TAILQ_PREV(at, bbr_head, r_next); 4464 r_rsm = TAILQ_NEXT(at, r_next); 4465 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) { 4466 /* Potentially mergeable */ 4467 if ((l_rsm->r_end == start) || 4468 (SEQ_LT(start, l_rsm->r_end) && 4469 SEQ_GT(end, l_rsm->r_end))) { 4470 /* 4471 * map blk |------| 4472 * sack blk |------| 4473 * <or> 4474 * map blk |------| 4475 * sack blk |------| 4476 */ 4477 return (1); 4478 } 4479 } 4480 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) { 4481 /* Potentially mergeable */ 4482 if ((r_rsm->r_start == end) || 4483 (SEQ_LT(start, r_rsm->r_start) && 4484 SEQ_GT(end, r_rsm->r_start))) { 4485 /* 4486 * map blk |---------| 4487 * sack blk |----| 4488 * <or> 4489 * map blk |---------| 4490 * sack blk |-------| 4491 */ 4492 return (1); 4493 } 4494 } 4495 return (0); 4496 } 4497 4498 static struct bbr_sendmap * 4499 bbr_merge_rsm(struct tcp_bbr *bbr, 4500 struct bbr_sendmap *l_rsm, 4501 struct bbr_sendmap *r_rsm) 4502 { 4503 /* 4504 * We are merging two ack'd RSM's, 4505 * the l_rsm is on the left (lower seq 4506 * values) and the r_rsm is on the right 4507 * (higher seq value). The simplest way 4508 * to merge these is to move the right 4509 * one into the left. I don't think there 4510 * is any reason we need to try to find 4511 * the oldest (or last oldest retransmitted). 4512 */ 4513 l_rsm->r_end = r_rsm->r_end; 4514 if (l_rsm->r_dupack < r_rsm->r_dupack) 4515 l_rsm->r_dupack = r_rsm->r_dupack; 4516 if (r_rsm->r_rtr_bytes) 4517 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 4518 if (r_rsm->r_in_tmap) { 4519 /* This really should not happen */ 4520 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext); 4521 } 4522 if (r_rsm->r_app_limited) 4523 l_rsm->r_app_limited = r_rsm->r_app_limited; 4524 /* Now the flags */ 4525 if (r_rsm->r_flags & BBR_HAS_FIN) 4526 l_rsm->r_flags |= BBR_HAS_FIN; 4527 if (r_rsm->r_flags & BBR_TLP) 4528 l_rsm->r_flags |= BBR_TLP; 4529 if (r_rsm->r_flags & BBR_RWND_COLLAPSED) 4530 l_rsm->r_flags |= BBR_RWND_COLLAPSED; 4531 if (r_rsm->r_flags & BBR_MARKED_LOST) { 4532 /* This really should not happen */ 4533 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start; 4534 } 4535 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next); 4536 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 4537 /* Transfer the split limit to the map we free */ 4538 r_rsm->r_limit_type = l_rsm->r_limit_type; 4539 l_rsm->r_limit_type = 0; 4540 } 4541 bbr_free(bbr, r_rsm); 4542 return(l_rsm); 4543 } 4544 4545 /* 4546 * TLP Timer, here we simply setup what segment we want to 4547 * have the TLP expire on, the normal bbr_output_wtime() will then 4548 * send it out. 4549 * 4550 * We return 1, saying don't proceed with bbr_output_wtime only 4551 * when all timers have been stopped (destroyed PCB?). 4552 */ 4553 static int 4554 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4555 { 4556 /* 4557 * Tail Loss Probe. 4558 */ 4559 struct bbr_sendmap *rsm = NULL; 4560 struct socket *so; 4561 uint32_t amm; 4562 uint32_t out, avail; 4563 uint32_t maxseg; 4564 int collapsed_win = 0; 4565 4566 if (bbr->rc_all_timers_stopped) { 4567 return (1); 4568 } 4569 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4570 /* Its not time yet */ 4571 return (0); 4572 } 4573 if (ctf_progress_timeout_check(tp, true)) { 4574 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4575 return (-ETIMEDOUT); /* tcp_drop() */ 4576 } 4577 /* Did we somehow get into persists? */ 4578 if (bbr->rc_in_persist) { 4579 return (0); 4580 } 4581 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4582 bbr_set_state(tp, bbr, 0); 4583 BBR_STAT_INC(bbr_tlp_tot); 4584 maxseg = tp->t_maxseg - bbr->rc_last_options; 4585 /* 4586 * A TLP timer has expired. We have been idle for 2 rtts. So we now 4587 * need to figure out how to force a full MSS segment out. 4588 */ 4589 so = tptosocket(tp); 4590 avail = sbavail(&so->so_snd); 4591 out = ctf_outstanding(tp); 4592 if (out > tp->snd_wnd) { 4593 /* special case, we need a retransmission */ 4594 collapsed_win = 1; 4595 goto need_retran; 4596 } 4597 if (avail > out) { 4598 /* New data is available */ 4599 amm = avail - out; 4600 if (amm > maxseg) { 4601 amm = maxseg; 4602 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 4603 /* not enough to fill a MTU and no-delay is off */ 4604 goto need_retran; 4605 } 4606 /* Set the send-new override */ 4607 if ((out + amm) <= tp->snd_wnd) { 4608 bbr->rc_tlp_new_data = 1; 4609 } else { 4610 goto need_retran; 4611 } 4612 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4613 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max; 4614 bbr->r_ctl.rc_tlp_send = NULL; 4615 /* cap any slots */ 4616 BBR_STAT_INC(bbr_tlp_newdata); 4617 goto send; 4618 } 4619 need_retran: 4620 /* 4621 * Ok we need to arrange the last un-acked segment to be re-sent, or 4622 * optionally the first un-acked segment. 4623 */ 4624 if (collapsed_win == 0) { 4625 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 4626 if (rsm && (BBR_ACKED | BBR_HAS_FIN)) { 4627 rsm = bbr_find_high_nonack(bbr, rsm); 4628 } 4629 if (rsm == NULL) { 4630 goto restore; 4631 } 4632 } else { 4633 /* 4634 * We must find the last segment 4635 * that was acceptable by the client. 4636 */ 4637 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4638 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) { 4639 /* Found one */ 4640 break; 4641 } 4642 } 4643 if (rsm == NULL) { 4644 /* None? if so send the first */ 4645 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4646 if (rsm == NULL) 4647 goto restore; 4648 } 4649 } 4650 if ((rsm->r_end - rsm->r_start) > maxseg) { 4651 /* 4652 * We need to split this the last segment in two. 4653 */ 4654 struct bbr_sendmap *nrsm; 4655 4656 nrsm = bbr_alloc_full_limit(bbr); 4657 if (nrsm == NULL) { 4658 /* 4659 * We can't get memory to split, we can either just 4660 * not split it. Or retransmit the whole piece, lets 4661 * do the large send (BTLP :-) ). 4662 */ 4663 goto go_for_it; 4664 } 4665 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg)); 4666 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 4667 if (rsm->r_in_tmap) { 4668 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4669 nrsm->r_in_tmap = 1; 4670 } 4671 rsm->r_flags &= (~BBR_HAS_FIN); 4672 rsm = nrsm; 4673 } 4674 go_for_it: 4675 bbr->r_ctl.rc_tlp_send = rsm; 4676 bbr->rc_tlp_rtx_out = 1; 4677 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) { 4678 bbr->r_ctl.rc_tlp_seg_send_cnt++; 4679 tp->t_rxtshift++; 4680 } else { 4681 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 4682 bbr->r_ctl.rc_tlp_seg_send_cnt = 1; 4683 } 4684 send: 4685 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 4686 /* 4687 * Can't [re]/transmit a segment we have retransmitted the 4688 * max times. We need the retransmit timer to take over. 4689 */ 4690 restore: 4691 bbr->rc_tlp_new_data = 0; 4692 bbr->r_ctl.rc_tlp_send = NULL; 4693 if (rsm) 4694 rsm->r_flags &= ~BBR_TLP; 4695 BBR_STAT_INC(bbr_tlp_retran_fail); 4696 return (0); 4697 } else if (rsm) { 4698 rsm->r_flags |= BBR_TLP; 4699 } 4700 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) && 4701 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) { 4702 /* 4703 * We have retransmitted to many times for TLP. Switch to 4704 * the regular RTO timer 4705 */ 4706 goto restore; 4707 } 4708 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP); 4709 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 4710 return (0); 4711 } 4712 4713 /* 4714 * Delayed ack Timer, here we simply need to setup the 4715 * ACK_NOW flag and remove the DELACK flag. From there 4716 * the output routine will send the ack out. 4717 * 4718 * We only return 1, saying don't proceed, if all timers 4719 * are stopped (destroyed PCB?). 4720 */ 4721 static int 4722 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4723 { 4724 if (bbr->rc_all_timers_stopped) { 4725 return (1); 4726 } 4727 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK); 4728 tp->t_flags &= ~TF_DELACK; 4729 tp->t_flags |= TF_ACKNOW; 4730 KMOD_TCPSTAT_INC(tcps_delack); 4731 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 4732 return (0); 4733 } 4734 4735 /* 4736 * Here we send a KEEP-ALIVE like probe to the 4737 * peer, we do not send data. 4738 * 4739 * We only return 1, saying don't proceed, if all timers 4740 * are stopped (destroyed PCB?). 4741 */ 4742 static int 4743 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4744 { 4745 struct tcptemp *t_template; 4746 int32_t retval = 1; 4747 4748 if (bbr->rc_all_timers_stopped) { 4749 return (1); 4750 } 4751 if (bbr->rc_in_persist == 0) 4752 return (0); 4753 4754 /* 4755 * Persistence timer into zero window. Force a byte to be output, if 4756 * possible. 4757 */ 4758 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST); 4759 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 4760 KMOD_TCPSTAT_INC(tcps_persisttimeo); 4761 /* 4762 * Have we exceeded the user specified progress time? 4763 */ 4764 if (ctf_progress_timeout_check(tp, true)) { 4765 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4766 return (-ETIMEDOUT); /* tcp_drop() */ 4767 } 4768 /* 4769 * Hack: if the peer is dead/unreachable, we do not time out if the 4770 * window is closed. After a full backoff, drop the connection if 4771 * the idle time (no responses to probes) reaches the maximum 4772 * backoff that we would use if retransmitting. 4773 */ 4774 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 4775 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 4776 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 4777 KMOD_TCPSTAT_INC(tcps_persistdrop); 4778 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4779 return (-ETIMEDOUT); /* tcp_drop() */ 4780 } 4781 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) && 4782 tp->snd_una == tp->snd_max) { 4783 bbr_exit_persist(tp, bbr, cts, __LINE__); 4784 retval = 0; 4785 goto out; 4786 } 4787 /* 4788 * If the user has closed the socket then drop a persisting 4789 * connection after a much reduced timeout. 4790 */ 4791 if (tp->t_state > TCPS_CLOSE_WAIT && 4792 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 4793 KMOD_TCPSTAT_INC(tcps_persistdrop); 4794 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4795 return (-ETIMEDOUT); /* tcp_drop() */ 4796 } 4797 t_template = tcpip_maketemplate(bbr->rc_inp); 4798 if (t_template) { 4799 tcp_respond(tp, t_template->tt_ipgen, 4800 &t_template->tt_t, (struct mbuf *)NULL, 4801 tp->rcv_nxt, tp->snd_una - 1, 0); 4802 /* This sends an ack */ 4803 if (tp->t_flags & TF_DELACK) 4804 tp->t_flags &= ~TF_DELACK; 4805 free(t_template, M_TEMP); 4806 } 4807 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 4808 tp->t_rxtshift++; 4809 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0); 4810 out: 4811 return (retval); 4812 } 4813 4814 /* 4815 * If a keepalive goes off, we had no other timers 4816 * happening. We always return 1 here since this 4817 * routine either drops the connection or sends 4818 * out a segment with respond. 4819 */ 4820 static int 4821 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4822 { 4823 struct tcptemp *t_template; 4824 struct inpcb *inp = tptoinpcb(tp); 4825 4826 if (bbr->rc_all_timers_stopped) { 4827 return (1); 4828 } 4829 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 4830 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP); 4831 /* 4832 * Keep-alive timer went off; send something or drop connection if 4833 * idle for too long. 4834 */ 4835 KMOD_TCPSTAT_INC(tcps_keeptimeo); 4836 if (tp->t_state < TCPS_ESTABLISHED) 4837 goto dropit; 4838 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 4839 tp->t_state <= TCPS_CLOSING) { 4840 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 4841 goto dropit; 4842 /* 4843 * Send a packet designed to force a response if the peer is 4844 * up and reachable: either an ACK if the connection is 4845 * still alive, or an RST if the peer has closed the 4846 * connection due to timeout or reboot. Using sequence 4847 * number tp->snd_una-1 causes the transmitted zero-length 4848 * segment to lie outside the receive window; by the 4849 * protocol spec, this requires the correspondent TCP to 4850 * respond. 4851 */ 4852 KMOD_TCPSTAT_INC(tcps_keepprobe); 4853 t_template = tcpip_maketemplate(inp); 4854 if (t_template) { 4855 tcp_respond(tp, t_template->tt_ipgen, 4856 &t_template->tt_t, (struct mbuf *)NULL, 4857 tp->rcv_nxt, tp->snd_una - 1, 0); 4858 free(t_template, M_TEMP); 4859 } 4860 } 4861 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0); 4862 return (1); 4863 dropit: 4864 KMOD_TCPSTAT_INC(tcps_keepdrops); 4865 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 4866 return (-ETIMEDOUT); /* tcp_drop() */ 4867 } 4868 4869 /* 4870 * Retransmit helper function, clear up all the ack 4871 * flags and take care of important book keeping. 4872 */ 4873 static void 4874 bbr_remxt_tmr(struct tcpcb *tp) 4875 { 4876 /* 4877 * The retransmit timer went off, all sack'd blocks must be 4878 * un-acked. 4879 */ 4880 struct bbr_sendmap *rsm, *trsm = NULL; 4881 struct tcp_bbr *bbr; 4882 uint32_t cts, lost; 4883 4884 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 4885 cts = tcp_get_usecs(&bbr->rc_tv); 4886 lost = bbr->r_ctl.rc_lost; 4887 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4888 bbr_set_state(tp, bbr, 0); 4889 4890 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 4891 if (rsm->r_flags & BBR_ACKED) { 4892 uint32_t old_flags; 4893 4894 rsm->r_dupack = 0; 4895 if (rsm->r_in_tmap == 0) { 4896 /* We must re-add it back to the tlist */ 4897 if (trsm == NULL) { 4898 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 4899 } else { 4900 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext); 4901 } 4902 rsm->r_in_tmap = 1; 4903 } 4904 old_flags = rsm->r_flags; 4905 rsm->r_flags |= BBR_RXT_CLEARED; 4906 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS); 4907 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 4908 } else { 4909 if ((tp->t_state < TCPS_ESTABLISHED) && 4910 (rsm->r_start == tp->snd_una)) { 4911 /* 4912 * Special case for TCP FO. Where 4913 * we sent more data beyond the snd_max. 4914 * We don't mark that as lost and stop here. 4915 */ 4916 break; 4917 } 4918 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4919 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4920 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4921 } 4922 if (bbr_marks_rxt_sack_passed) { 4923 /* 4924 * With this option, we will rack out 4925 * in 1ms increments the rest of the packets. 4926 */ 4927 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST; 4928 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4929 } else { 4930 /* 4931 * With this option we only mark them lost 4932 * and remove all sack'd markings. We will run 4933 * another RXT or a TLP. This will cause 4934 * us to eventually send more based on what 4935 * ack's come in. 4936 */ 4937 rsm->r_flags |= BBR_MARKED_LOST; 4938 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4939 rsm->r_flags &= ~BBR_SACK_PASSED; 4940 } 4941 } 4942 trsm = rsm; 4943 } 4944 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4945 /* Clear the count (we just un-acked them) */ 4946 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR); 4947 bbr->rc_tlp_new_data = 0; 4948 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4949 /* zap the behindness on a rxt */ 4950 bbr->r_ctl.rc_hptsi_agg_delay = 0; 4951 bbr->r_agg_early_set = 0; 4952 bbr->r_ctl.rc_agg_early = 0; 4953 bbr->rc_tlp_rtx_out = 0; 4954 bbr->r_ctl.rc_sacked = 0; 4955 bbr->r_ctl.rc_sacklast = NULL; 4956 bbr->r_timer_override = 1; 4957 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4958 } 4959 4960 /* 4961 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 4962 * we will setup to retransmit the lowest seq number outstanding. 4963 */ 4964 static int 4965 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4966 { 4967 struct inpcb *inp = tptoinpcb(tp); 4968 int32_t rexmt; 4969 int32_t retval = 0; 4970 bool isipv6; 4971 4972 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 4973 if (bbr->rc_all_timers_stopped) { 4974 return (1); 4975 } 4976 if (TCPS_HAVEESTABLISHED(tp->t_state) && 4977 (tp->snd_una == tp->snd_max)) { 4978 /* Nothing outstanding .. nothing to do */ 4979 return (0); 4980 } 4981 /* 4982 * Retransmission timer went off. Message has not been acked within 4983 * retransmit interval. Back off to a longer retransmit interval 4984 * and retransmit one segment. 4985 */ 4986 if (ctf_progress_timeout_check(tp, true)) { 4987 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4988 return (-ETIMEDOUT); /* tcp_drop() */ 4989 } 4990 bbr_remxt_tmr(tp); 4991 if ((bbr->r_ctl.rc_resend == NULL) || 4992 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) { 4993 /* 4994 * If the rwnd collapsed on 4995 * the one we are retransmitting 4996 * it does not count against the 4997 * rxt count. 4998 */ 4999 tp->t_rxtshift++; 5000 } 5001 if (tp->t_rxtshift > TCP_MAXRXTSHIFT) { 5002 tp->t_rxtshift = TCP_MAXRXTSHIFT; 5003 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 5004 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 5005 /* XXXGL: previously t_softerror was casted to uint16_t */ 5006 MPASS(tp->t_softerror >= 0); 5007 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 5008 return (retval); /* tcp_drop() */ 5009 } 5010 if (tp->t_state == TCPS_SYN_SENT) { 5011 /* 5012 * If the SYN was retransmitted, indicate CWND to be limited 5013 * to 1 segment in cc_conn_init(). 5014 */ 5015 tp->snd_cwnd = 1; 5016 } else if (tp->t_rxtshift == 1) { 5017 /* 5018 * first retransmit; record ssthresh and cwnd so they can be 5019 * recovered if this turns out to be a "bad" retransmit. A 5020 * retransmit is considered "bad" if an ACK for this segment 5021 * is received within RTT/2 interval; the assumption here is 5022 * that the ACK was already in flight. See "On Estimating 5023 * End-to-End Network Path Properties" by Allman and Paxson 5024 * for more details. 5025 */ 5026 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5027 if (!IN_RECOVERY(tp->t_flags)) { 5028 tp->snd_cwnd_prev = tp->snd_cwnd; 5029 tp->snd_ssthresh_prev = tp->snd_ssthresh; 5030 tp->snd_recover_prev = tp->snd_recover; 5031 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 5032 tp->t_flags |= TF_PREVVALID; 5033 } else { 5034 tp->t_flags &= ~TF_PREVVALID; 5035 } 5036 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5037 } else { 5038 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5039 tp->t_flags &= ~TF_PREVVALID; 5040 } 5041 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 5042 if ((tp->t_state == TCPS_SYN_SENT) || 5043 (tp->t_state == TCPS_SYN_RECEIVED)) 5044 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift]; 5045 else 5046 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 5047 TCPT_RANGESET(tp->t_rxtcur, rexmt, 5048 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), 5049 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 5050 /* 5051 * We enter the path for PLMTUD if connection is established or, if 5052 * connection is FIN_WAIT_1 status, reason for the last is that if 5053 * amount of data we send is very small, we could send it in couple 5054 * of packets and process straight to FIN. In that case we won't 5055 * catch ESTABLISHED state. 5056 */ 5057 #ifdef INET6 5058 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 5059 #else 5060 isipv6 = false; 5061 #endif 5062 if (((V_tcp_pmtud_blackhole_detect == 1) || 5063 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 5064 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 5065 ((tp->t_state == TCPS_ESTABLISHED) || 5066 (tp->t_state == TCPS_FIN_WAIT_1))) { 5067 /* 5068 * Idea here is that at each stage of mtu probe (usually, 5069 * 1448 -> 1188 -> 524) should be given 2 chances to recover 5070 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 5071 * should take care of that. 5072 */ 5073 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 5074 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 5075 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 5076 tp->t_rxtshift % 2 == 0)) { 5077 /* 5078 * Enter Path MTU Black-hole Detection mechanism: - 5079 * Disable Path MTU Discovery (IP "DF" bit). - 5080 * Reduce MTU to lower value than what we negotiated 5081 * with peer. 5082 */ 5083 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 5084 /* 5085 * Record that we may have found a black 5086 * hole. 5087 */ 5088 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 5089 /* Keep track of previous MSS. */ 5090 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 5091 } 5092 /* 5093 * Reduce the MSS to blackhole value or to the 5094 * default in an attempt to retransmit. 5095 */ 5096 #ifdef INET6 5097 isipv6 = bbr->r_is_v6; 5098 if (isipv6 && 5099 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 5100 /* Use the sysctl tuneable blackhole MSS. */ 5101 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 5102 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5103 } else if (isipv6) { 5104 /* Use the default MSS. */ 5105 tp->t_maxseg = V_tcp_v6mssdflt; 5106 /* 5107 * Disable Path MTU Discovery when we switch 5108 * to minmss. 5109 */ 5110 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5111 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5112 } 5113 #endif 5114 #if defined(INET6) && defined(INET) 5115 else 5116 #endif 5117 #ifdef INET 5118 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 5119 /* Use the sysctl tuneable blackhole MSS. */ 5120 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 5121 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5122 } else { 5123 /* Use the default MSS. */ 5124 tp->t_maxseg = V_tcp_mssdflt; 5125 /* 5126 * Disable Path MTU Discovery when we switch 5127 * to minmss. 5128 */ 5129 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5130 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5131 } 5132 #endif 5133 } else { 5134 /* 5135 * If further retransmissions are still unsuccessful 5136 * with a lowered MTU, maybe this isn't a blackhole 5137 * and we restore the previous MSS and blackhole 5138 * detection flags. The limit '6' is determined by 5139 * giving each probe stage (1448, 1188, 524) 2 5140 * chances to recover. 5141 */ 5142 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 5143 (tp->t_rxtshift >= 6)) { 5144 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 5145 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 5146 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 5147 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 5148 } 5149 } 5150 } 5151 /* 5152 * Disable RFC1323 and SACK if we haven't got any response to our 5153 * third SYN to work-around some broken terminal servers (most of 5154 * which have hopefully been retired) that have bad VJ header 5155 * compression code which trashes TCP segments containing 5156 * unknown-to-them TCP options. 5157 */ 5158 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 5159 (tp->t_rxtshift == 3)) 5160 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 5161 /* 5162 * If we backed off this far, our srtt estimate is probably bogus. 5163 * Clobber it so we'll take the next rtt measurement as our srtt; 5164 * move the current srtt into rttvar to keep the current retransmit 5165 * times until then. 5166 */ 5167 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 5168 #ifdef INET6 5169 if (bbr->r_is_v6) 5170 in6_losing(inp); 5171 else 5172 #endif 5173 in_losing(inp); 5174 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 5175 tp->t_srtt = 0; 5176 } 5177 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 5178 tp->snd_recover = tp->snd_max; 5179 tp->t_flags |= TF_ACKNOW; 5180 tp->t_rtttime = 0; 5181 5182 return (retval); 5183 } 5184 5185 static int 5186 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling) 5187 { 5188 int32_t ret = 0; 5189 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 5190 5191 if (timers == 0) { 5192 return (0); 5193 } 5194 if (tp->t_state == TCPS_LISTEN) { 5195 /* no timers on listen sockets */ 5196 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 5197 return (0); 5198 return (1); 5199 } 5200 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 5201 uint32_t left; 5202 5203 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 5204 ret = -1; 5205 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5206 return (0); 5207 } 5208 if (hpts_calling == 0) { 5209 ret = -2; 5210 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5211 return (0); 5212 } 5213 /* 5214 * Ok our timer went off early and we are not paced false 5215 * alarm, go back to sleep. 5216 */ 5217 left = bbr->r_ctl.rc_timer_exp - cts; 5218 ret = -3; 5219 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling); 5220 tcp_hpts_insert(tptoinpcb(tp), HPTS_USEC_TO_SLOTS(left)); 5221 return (1); 5222 } 5223 bbr->rc_tmr_stopped = 0; 5224 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 5225 if (timers & PACE_TMR_DELACK) { 5226 ret = bbr_timeout_delack(tp, bbr, cts); 5227 } else if (timers & PACE_TMR_PERSIT) { 5228 ret = bbr_timeout_persist(tp, bbr, cts); 5229 } else if (timers & PACE_TMR_RACK) { 5230 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5231 ret = bbr_timeout_rack(tp, bbr, cts); 5232 } else if (timers & PACE_TMR_TLP) { 5233 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5234 ret = bbr_timeout_tlp(tp, bbr, cts); 5235 } else if (timers & PACE_TMR_RXT) { 5236 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5237 ret = bbr_timeout_rxt(tp, bbr, cts); 5238 } else if (timers & PACE_TMR_KEEP) { 5239 ret = bbr_timeout_keepalive(tp, bbr, cts); 5240 } 5241 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling); 5242 return (ret); 5243 } 5244 5245 static void 5246 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts) 5247 { 5248 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 5249 uint8_t hpts_removed = 0; 5250 5251 if (tcp_in_hpts(bbr->rc_inp) && 5252 (bbr->rc_timer_first == 1)) { 5253 /* 5254 * If we are canceling timer's when we have the 5255 * timer ahead of the output being paced. We also 5256 * must remove ourselves from the hpts. 5257 */ 5258 hpts_removed = 1; 5259 tcp_hpts_remove(bbr->rc_inp); 5260 if (bbr->r_ctl.rc_last_delay_val) { 5261 /* Update the last hptsi delay too */ 5262 uint32_t time_since_send; 5263 5264 if (TSTMP_GT(cts, bbr->rc_pacer_started)) 5265 time_since_send = cts - bbr->rc_pacer_started; 5266 else 5267 time_since_send = 0; 5268 if (bbr->r_ctl.rc_last_delay_val > time_since_send) { 5269 /* Cut down our slot time */ 5270 bbr->r_ctl.rc_last_delay_val -= time_since_send; 5271 } else { 5272 bbr->r_ctl.rc_last_delay_val = 0; 5273 } 5274 bbr->rc_pacer_started = cts; 5275 } 5276 } 5277 bbr->rc_timer_first = 0; 5278 bbr_log_to_cancel(bbr, line, cts, hpts_removed); 5279 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 5280 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 5281 } 5282 } 5283 5284 static int 5285 bbr_stopall(struct tcpcb *tp) 5286 { 5287 struct tcp_bbr *bbr; 5288 5289 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 5290 bbr->rc_all_timers_stopped = 1; 5291 return (0); 5292 } 5293 5294 static uint32_t 5295 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts) 5296 { 5297 struct bbr_sendmap *rsm; 5298 5299 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 5300 if ((rsm == NULL) || (u_rsm == rsm)) 5301 return (cts); 5302 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 5303 } 5304 5305 static void 5306 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr, 5307 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time) 5308 { 5309 int32_t idx; 5310 5311 rsm->r_rtr_cnt++; 5312 rsm->r_dupack = 0; 5313 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) { 5314 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS; 5315 rsm->r_flags |= BBR_OVERMAX; 5316 } 5317 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 5318 /* Take off the collapsed flag at rxt */ 5319 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 5320 } 5321 if (rsm->r_flags & BBR_MARKED_LOST) { 5322 /* We have retransmitted, its no longer lost */ 5323 rsm->r_flags &= ~BBR_MARKED_LOST; 5324 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 5325 } 5326 if (rsm->r_flags & BBR_RXT_CLEARED) { 5327 /* 5328 * We hit a RXT timer on it and 5329 * we cleared the "acked" flag. 5330 * We now have it going back into 5331 * flight, we can remove the cleared 5332 * flag and possibly do accounting on 5333 * this piece. 5334 */ 5335 rsm->r_flags &= ~BBR_RXT_CLEARED; 5336 } 5337 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) { 5338 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 5339 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 5340 } 5341 idx = rsm->r_rtr_cnt - 1; 5342 rsm->r_tim_lastsent[idx] = cts; 5343 rsm->r_pacing_delay = pacing_time; 5344 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5345 rsm->r_ts_valid = bbr->rc_ts_valid; 5346 if (bbr->rc_ts_valid) 5347 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5348 if (bbr->r_ctl.r_app_limited_until) 5349 rsm->r_app_limited = 1; 5350 else 5351 rsm->r_app_limited = 0; 5352 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5353 rsm->r_bbr_state = bbr_state_val(bbr); 5354 else 5355 rsm->r_bbr_state = 8; 5356 if (rsm->r_flags & BBR_ACKED) { 5357 /* Problably MTU discovery messing with us */ 5358 uint32_t old_flags; 5359 5360 old_flags = rsm->r_flags; 5361 rsm->r_flags &= ~BBR_ACKED; 5362 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 5363 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 5364 if (bbr->r_ctl.rc_sacked == 0) 5365 bbr->r_ctl.rc_sacklast = NULL; 5366 } 5367 if (rsm->r_in_tmap) { 5368 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5369 } 5370 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5371 rsm->r_in_tmap = 1; 5372 if (rsm->r_flags & BBR_SACK_PASSED) { 5373 /* We have retransmitted due to the SACK pass */ 5374 rsm->r_flags &= ~BBR_SACK_PASSED; 5375 rsm->r_flags |= BBR_WAS_SACKPASS; 5376 } 5377 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5378 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5379 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5380 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 5381 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5382 rsm->r_is_gain = 1; 5383 rsm->r_is_drain = 0; 5384 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5385 rsm->r_is_drain = 1; 5386 rsm->r_is_gain = 0; 5387 } else { 5388 rsm->r_is_drain = 0; 5389 rsm->r_is_gain = 0; 5390 } 5391 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */ 5392 } 5393 5394 /* 5395 * Returns 0, or the sequence where we stopped 5396 * updating. We also update the lenp to be the amount 5397 * of data left. 5398 */ 5399 5400 static uint32_t 5401 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr, 5402 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time) 5403 { 5404 /* 5405 * We (re-)transmitted starting at rsm->r_start for some length 5406 * (possibly less than r_end. 5407 */ 5408 struct bbr_sendmap *nrsm; 5409 uint32_t c_end; 5410 int32_t len; 5411 5412 len = *lenp; 5413 c_end = rsm->r_start + len; 5414 if (SEQ_GEQ(c_end, rsm->r_end)) { 5415 /* 5416 * We retransmitted the whole piece or more than the whole 5417 * slopping into the next rsm. 5418 */ 5419 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5420 if (c_end == rsm->r_end) { 5421 *lenp = 0; 5422 return (0); 5423 } else { 5424 int32_t act_len; 5425 5426 /* Hangs over the end return whats left */ 5427 act_len = rsm->r_end - rsm->r_start; 5428 *lenp = (len - act_len); 5429 return (rsm->r_end); 5430 } 5431 /* We don't get out of this block. */ 5432 } 5433 /* 5434 * Here we retransmitted less than the whole thing which means we 5435 * have to split this into what was transmitted and what was not. 5436 */ 5437 nrsm = bbr_alloc_full_limit(bbr); 5438 if (nrsm == NULL) { 5439 *lenp = 0; 5440 return (0); 5441 } 5442 /* 5443 * So here we are going to take the original rsm and make it what we 5444 * retransmitted. nrsm will be the tail portion we did not 5445 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 5446 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 5447 * 1, 6 and the new piece will be 6, 11. 5448 */ 5449 bbr_clone_rsm(bbr, nrsm, rsm, c_end); 5450 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 5451 nrsm->r_dupack = 0; 5452 if (rsm->r_in_tmap) { 5453 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 5454 nrsm->r_in_tmap = 1; 5455 } 5456 rsm->r_flags &= (~BBR_HAS_FIN); 5457 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5458 *lenp = 0; 5459 return (0); 5460 } 5461 5462 static uint64_t 5463 bbr_get_hardware_rate(struct tcp_bbr *bbr) 5464 { 5465 uint64_t bw; 5466 5467 bw = bbr_get_bw(bbr); 5468 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]; 5469 bw /= (uint64_t)BBR_UNIT; 5470 return(bw); 5471 } 5472 5473 static void 5474 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts, 5475 uint64_t act_rate, uint64_t rate_wanted) 5476 { 5477 /* 5478 * We could not get a full gains worth 5479 * of rate. 5480 */ 5481 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) { 5482 /* we can't even get the real rate */ 5483 uint64_t red; 5484 5485 bbr->skip_gain = 1; 5486 bbr->gain_is_limited = 0; 5487 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate; 5488 if (red) 5489 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts); 5490 } else { 5491 /* We can use a lower gain */ 5492 bbr->skip_gain = 0; 5493 bbr->gain_is_limited = 1; 5494 } 5495 } 5496 5497 static void 5498 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts) 5499 { 5500 const struct tcp_hwrate_limit_table *nrte; 5501 int error, rate = -1; 5502 5503 if (bbr->r_ctl.crte == NULL) 5504 return; 5505 if ((bbr->rc_inp->inp_route.ro_nh == NULL) || 5506 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) { 5507 /* Lost our routes? */ 5508 /* Clear the way for a re-attempt */ 5509 bbr->bbr_attempt_hdwr_pace = 0; 5510 lost_rate: 5511 bbr->gain_is_limited = 0; 5512 bbr->skip_gain = 0; 5513 bbr->bbr_hdrw_pacing = 0; 5514 counter_u64_add(bbr_flows_whdwr_pacing, -1); 5515 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 5516 tcp_bbr_tso_size_check(bbr, cts); 5517 return; 5518 } 5519 rate = bbr_get_hardware_rate(bbr); 5520 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte, 5521 bbr->rc_tp, 5522 bbr->rc_inp->inp_route.ro_nh->nh_ifp, 5523 rate, 5524 (RS_PACING_GEQ|RS_PACING_SUB_OK), 5525 &error, NULL); 5526 if (nrte == NULL) { 5527 goto lost_rate; 5528 } 5529 if (nrte != bbr->r_ctl.crte) { 5530 bbr->r_ctl.crte = nrte; 5531 if (error == 0) { 5532 BBR_STAT_INC(bbr_hdwr_rl_mod_ok); 5533 if (bbr->r_ctl.crte->rate < rate) { 5534 /* We have a problem */ 5535 bbr_setup_less_of_rate(bbr, cts, 5536 bbr->r_ctl.crte->rate, rate); 5537 } else { 5538 /* We are good */ 5539 bbr->gain_is_limited = 0; 5540 bbr->skip_gain = 0; 5541 } 5542 } else { 5543 /* A failure should release the tag */ 5544 BBR_STAT_INC(bbr_hdwr_rl_mod_fail); 5545 bbr->gain_is_limited = 0; 5546 bbr->skip_gain = 0; 5547 bbr->bbr_hdrw_pacing = 0; 5548 } 5549 bbr_type_log_hdwr_pacing(bbr, 5550 bbr->r_ctl.crte->ptbl->rs_ifp, 5551 rate, 5552 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate), 5553 __LINE__, 5554 cts, 5555 error); 5556 } 5557 } 5558 5559 static void 5560 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts) 5561 { 5562 /* 5563 * If we have hardware pacing support 5564 * we need to factor that in for our 5565 * TSO size. 5566 */ 5567 const struct tcp_hwrate_limit_table *rlp; 5568 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay; 5569 5570 if ((bbr->bbr_hdrw_pacing == 0) || 5571 (IN_RECOVERY(bbr->rc_tp->t_flags)) || 5572 (bbr->r_ctl.crte == NULL)) 5573 return; 5574 if (bbr->hw_pacing_set == 0) { 5575 /* Not yet by the hdwr pacing count delay */ 5576 return; 5577 } 5578 if (bbr_hdwr_pace_adjust == 0) { 5579 /* No adjustment */ 5580 return; 5581 } 5582 rlp = bbr->r_ctl.crte; 5583 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) 5584 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5585 else 5586 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5587 /* 5588 * So lets first get the 5589 * time we will take between 5590 * TSO sized sends currently without 5591 * hardware help. 5592 */ 5593 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT, 5594 bbr->r_ctl.rc_pace_max_segs, cts, 1); 5595 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg; 5596 hdwr_delay *= rlp->time_between; 5597 if (cur_delay > hdwr_delay) 5598 delta = cur_delay - hdwr_delay; 5599 else 5600 delta = 0; 5601 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay, 5602 (bbr->r_ctl.rc_pace_max_segs / maxseg), 5603 1); 5604 if (delta && 5605 (delta < (max(rlp->time_between, 5606 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) { 5607 /* 5608 * Now lets divide by the pacing 5609 * time between each segment the 5610 * hardware sends rounding up and 5611 * derive a bytes from that. We multiply 5612 * that by bbr_hdwr_pace_adjust to get 5613 * more bang for our buck. 5614 * 5615 * The goal is to have the software pacer 5616 * waiting no more than an additional 5617 * pacing delay if we can (without the 5618 * compensation i.e. x bbr_hdwr_pace_adjust). 5619 */ 5620 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between), 5621 (bbr->r_ctl.rc_pace_max_segs/maxseg)); 5622 seg_sz *= bbr_hdwr_pace_adjust; 5623 if (bbr_hdwr_pace_floor && 5624 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5625 /* Currently hardware paces 5626 * out rs_min_seg segments at a time. 5627 * We need to make sure we always send at least 5628 * a full burst of bbr_hdwr_pace_floor down. 5629 */ 5630 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5631 } 5632 seg_sz *= maxseg; 5633 } else if (delta == 0) { 5634 /* 5635 * The highest pacing rate is 5636 * above our b/w gained. This means 5637 * we probably are going quite fast at 5638 * the hardware highest rate. Lets just multiply 5639 * the calculated TSO size by the 5640 * multiplier factor (its probably 5641 * 4 segments in the default config for 5642 * mlx). 5643 */ 5644 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust; 5645 if (bbr_hdwr_pace_floor && 5646 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5647 /* Currently hardware paces 5648 * out rs_min_seg segments at a time. 5649 * We need to make sure we always send at least 5650 * a full burst of bbr_hdwr_pace_floor down. 5651 */ 5652 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5653 } 5654 } else { 5655 /* 5656 * The pacing time difference is so 5657 * big that the hardware will 5658 * pace out more rapidly then we 5659 * really want and then we 5660 * will have a long delay. Lets just keep 5661 * the same TSO size so its as if 5662 * we were not using hdwr pacing (we 5663 * just gain a bit of spacing from the 5664 * hardware if seg_sz > 1). 5665 */ 5666 seg_sz = bbr->r_ctl.rc_pace_max_segs; 5667 } 5668 if (seg_sz > bbr->r_ctl.rc_pace_max_segs) 5669 new_tso = seg_sz; 5670 else 5671 new_tso = bbr->r_ctl.rc_pace_max_segs; 5672 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg)) 5673 new_tso = PACE_MAX_IP_BYTES - maxseg; 5674 5675 if (new_tso != bbr->r_ctl.rc_pace_max_segs) { 5676 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0); 5677 bbr->r_ctl.rc_pace_max_segs = new_tso; 5678 } 5679 } 5680 5681 static void 5682 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts) 5683 { 5684 uint64_t bw; 5685 uint32_t old_tso = 0, new_tso; 5686 uint32_t maxseg, bytes; 5687 uint32_t tls_seg=0; 5688 /* 5689 * Google/linux uses the following algorithm to determine 5690 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18): 5691 * 5692 * bytes = bw_in_bytes_per_second / 1000 5693 * bytes = min(bytes, 64k) 5694 * tso_segs = bytes / MSS 5695 * if (bw < 1.2Mbs) 5696 * min_tso_segs = 1 5697 * else 5698 * min_tso_segs = 2 5699 * tso_segs = max(tso_segs, min_tso_segs) 5700 * 5701 * * Note apply a device specific limit (we apply this in the 5702 * tcp_m_copym). 5703 * Note that before the initial measurement is made google bursts out 5704 * a full iwnd just like new-reno/cubic. 5705 * 5706 * We do not use this algorithm. Instead we 5707 * use a two phased approach: 5708 * 5709 * if ( bw <= per-tcb-cross-over) 5710 * goal_tso = calculate how much with this bw we 5711 * can send in goal-time seconds. 5712 * if (goal_tso > mss) 5713 * seg = goal_tso / mss 5714 * tso = seg * mss 5715 * else 5716 * tso = mss 5717 * if (tso > per-tcb-max) 5718 * tso = per-tcb-max 5719 * else if ( bw > 512Mbps) 5720 * tso = max-tso (64k/mss) 5721 * else 5722 * goal_tso = bw / per-tcb-divsor 5723 * seg = (goal_tso + mss-1)/mss 5724 * tso = seg * mss 5725 * 5726 * if (tso < per-tcb-floor) 5727 * tso = per-tcb-floor 5728 * if (tso > per-tcb-utter_max) 5729 * tso = per-tcb-utter_max 5730 * 5731 * Note the default per-tcb-divisor is 1000 (same as google). 5732 * the goal cross over is 30Mbps however. To recreate googles 5733 * algorithm you need to set: 5734 * 5735 * cross-over = 23,168,000 bps 5736 * goal-time = 18000 5737 * per-tcb-max = 2 5738 * per-tcb-divisor = 1000 5739 * per-tcb-floor = 1 5740 * 5741 * This will get you "google bbr" behavior with respect to tso size. 5742 * 5743 * Note we do set anything TSO size until we are past the initial 5744 * window. Before that we gnerally use either a single MSS 5745 * or we use the full IW size (so we burst a IW at a time) 5746 */ 5747 5748 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) { 5749 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5750 } else { 5751 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5752 } 5753 old_tso = bbr->r_ctl.rc_pace_max_segs; 5754 if (bbr->rc_past_init_win == 0) { 5755 /* 5756 * Not enough data has been acknowledged to make a 5757 * judgement. Set up the initial TSO based on if we 5758 * are sending a full IW at once or not. 5759 */ 5760 if (bbr->rc_use_google) 5761 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2); 5762 else if (bbr->bbr_init_win_cheat) 5763 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp); 5764 else 5765 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5766 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg) 5767 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg; 5768 if (bbr->r_ctl.rc_pace_max_segs == 0) { 5769 bbr->r_ctl.rc_pace_max_segs = maxseg; 5770 } 5771 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0); 5772 bbr_adjust_for_hw_pacing(bbr, cts); 5773 return; 5774 } 5775 /** 5776 * Now lets set the TSO goal based on our delivery rate in 5777 * bytes per second. Note we only do this if 5778 * we have acked at least the initial cwnd worth of data. 5779 */ 5780 bw = bbr_get_bw(bbr); 5781 if (IN_RECOVERY(bbr->rc_tp->t_flags) && 5782 (bbr->rc_use_google == 0)) { 5783 /* We clamp to one MSS in recovery */ 5784 new_tso = maxseg; 5785 } else if (bbr->rc_use_google) { 5786 int min_tso_segs; 5787 5788 /* Google considers the gain too */ 5789 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) { 5790 bw *= bbr->r_ctl.rc_bbr_hptsi_gain; 5791 bw /= BBR_UNIT; 5792 } 5793 bytes = bw / 1024; 5794 if (bytes > (64 * 1024)) 5795 bytes = 64 * 1024; 5796 new_tso = bytes / maxseg; 5797 if (bw < ONE_POINT_TWO_MEG) 5798 min_tso_segs = 1; 5799 else 5800 min_tso_segs = 2; 5801 if (new_tso < min_tso_segs) 5802 new_tso = min_tso_segs; 5803 new_tso *= maxseg; 5804 } else if (bbr->rc_no_pacing) { 5805 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg; 5806 } else if (bw <= bbr->r_ctl.bbr_cross_over) { 5807 /* 5808 * Calculate the worse case b/w TSO if we are inserting no 5809 * more than a delay_target number of TSO's. 5810 */ 5811 uint32_t tso_len, min_tso; 5812 5813 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw); 5814 if (tso_len > maxseg) { 5815 new_tso = tso_len / maxseg; 5816 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max) 5817 new_tso = bbr->r_ctl.bbr_hptsi_segments_max; 5818 new_tso *= maxseg; 5819 } else { 5820 /* 5821 * less than a full sized frame yikes.. long rtt or 5822 * low bw? 5823 */ 5824 min_tso = bbr_minseg(bbr); 5825 if ((tso_len > min_tso) && (bbr_all_get_min == 0)) 5826 new_tso = rounddown(tso_len, min_tso); 5827 else 5828 new_tso = min_tso; 5829 } 5830 } else if (bw > FIVETWELVE_MBPS) { 5831 /* 5832 * This guy is so fast b/w wise that we can TSO as large as 5833 * possible of segments that the NIC will allow. 5834 */ 5835 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5836 } else { 5837 /* 5838 * This formula is based on attempting to send a segment or 5839 * more every bbr_hptsi_per_second. The default is 1000 5840 * which means you are targeting what you can send every 1ms 5841 * based on the peers bw. 5842 * 5843 * If the number drops to say 500, then you are looking more 5844 * at 2ms and you will raise how much we send in a single 5845 * TSO thus saving CPU (less bbr_output_wtime() calls). The 5846 * trade off of course is you will send more at once and 5847 * thus tend to clump up the sends into larger "bursts" 5848 * building a queue. 5849 */ 5850 bw /= bbr->r_ctl.bbr_hptsi_per_second; 5851 new_tso = roundup(bw, (uint64_t)maxseg); 5852 /* 5853 * Gate the floor to match what our lower than 48Mbps 5854 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus 5855 * becomes the floor for this calculation. 5856 */ 5857 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg)) 5858 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg); 5859 } 5860 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor))) 5861 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor; 5862 if (new_tso > PACE_MAX_IP_BYTES) 5863 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5864 /* Enforce an utter maximum. */ 5865 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) { 5866 new_tso = bbr->r_ctl.bbr_utter_max * maxseg; 5867 } 5868 if (old_tso != new_tso) { 5869 /* Only log changes */ 5870 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0); 5871 bbr->r_ctl.rc_pace_max_segs = new_tso; 5872 } 5873 /* We have hardware pacing! */ 5874 bbr_adjust_for_hw_pacing(bbr, cts); 5875 } 5876 5877 static void 5878 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len, 5879 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts, 5880 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc, 5881 struct sockbuf *sb) 5882 { 5883 5884 struct bbr_sendmap *rsm, *nrsm; 5885 register uint32_t snd_max, snd_una; 5886 uint32_t pacing_time; 5887 /* 5888 * Add to the RACK log of packets in flight or retransmitted. If 5889 * there is a TS option we will use the TS echoed, if not we will 5890 * grab a TS. 5891 * 5892 * Retransmissions will increment the count and move the ts to its 5893 * proper place. Note that if options do not include TS's then we 5894 * won't be able to effectively use the ACK for an RTT on a retran. 5895 * 5896 * Notes about r_start and r_end. Lets consider a send starting at 5897 * sequence 1 for 10 bytes. In such an example the r_start would be 5898 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 5899 * This means that r_end is actually the first sequence for the next 5900 * slot (11). 5901 * 5902 */ 5903 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5904 if (err) { 5905 /* 5906 * We don't log errors -- we could but snd_max does not 5907 * advance in this case either. 5908 */ 5909 return; 5910 } 5911 if (th_flags & TH_RST) { 5912 /* 5913 * We don't log resets and we return immediately from 5914 * sending 5915 */ 5916 *abandon = 1; 5917 return; 5918 } 5919 snd_una = tp->snd_una; 5920 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) { 5921 /* 5922 * The call to bbr_log_output is made before bumping 5923 * snd_max. This means we can record one extra byte on a SYN 5924 * or FIN if seq_out is adding more on and a FIN is present 5925 * (and we are not resending). 5926 */ 5927 if ((th_flags & TH_SYN) && (tp->iss == seq_out)) 5928 len++; 5929 if (th_flags & TH_FIN) 5930 len++; 5931 } 5932 if (SEQ_LEQ((seq_out + len), snd_una)) { 5933 /* Are sending an old segment to induce an ack (keep-alive)? */ 5934 return; 5935 } 5936 if (SEQ_LT(seq_out, snd_una)) { 5937 /* huh? should we panic? */ 5938 uint32_t end; 5939 5940 end = seq_out + len; 5941 seq_out = snd_una; 5942 len = end - seq_out; 5943 } 5944 snd_max = tp->snd_max; 5945 if (len == 0) { 5946 /* We don't log zero window probes */ 5947 return; 5948 } 5949 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1); 5950 /* First question is it a retransmission? */ 5951 if (seq_out == snd_max) { 5952 again: 5953 rsm = bbr_alloc(bbr); 5954 if (rsm == NULL) { 5955 return; 5956 } 5957 rsm->r_flags = 0; 5958 if (th_flags & TH_SYN) 5959 rsm->r_flags |= BBR_HAS_SYN; 5960 if (th_flags & TH_FIN) 5961 rsm->r_flags |= BBR_HAS_FIN; 5962 rsm->r_tim_lastsent[0] = cts; 5963 rsm->r_rtr_cnt = 1; 5964 rsm->r_rtr_bytes = 0; 5965 rsm->r_start = seq_out; 5966 rsm->r_end = rsm->r_start + len; 5967 rsm->r_dupack = 0; 5968 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5969 rsm->r_pacing_delay = pacing_time; 5970 rsm->r_ts_valid = bbr->rc_ts_valid; 5971 if (bbr->rc_ts_valid) 5972 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5973 rsm->r_del_time = bbr->r_ctl.rc_del_time; 5974 if (bbr->r_ctl.r_app_limited_until) 5975 rsm->r_app_limited = 1; 5976 else 5977 rsm->r_app_limited = 0; 5978 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5979 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5980 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5981 /* 5982 * Here we must also add in this rsm since snd_max 5983 * is updated after we return from a new send. 5984 */ 5985 rsm->r_flight_at_send += len; 5986 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 5987 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5988 rsm->r_in_tmap = 1; 5989 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5990 rsm->r_bbr_state = bbr_state_val(bbr); 5991 else 5992 rsm->r_bbr_state = 8; 5993 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5994 rsm->r_is_gain = 1; 5995 rsm->r_is_drain = 0; 5996 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5997 rsm->r_is_drain = 1; 5998 rsm->r_is_gain = 0; 5999 } else { 6000 rsm->r_is_drain = 0; 6001 rsm->r_is_gain = 0; 6002 } 6003 return; 6004 } 6005 /* 6006 * If we reach here its a retransmission and we need to find it. 6007 */ 6008 more: 6009 if (hintrsm && (hintrsm->r_start == seq_out)) { 6010 rsm = hintrsm; 6011 hintrsm = NULL; 6012 } else if (bbr->r_ctl.rc_next) { 6013 /* We have a hint from a previous run */ 6014 rsm = bbr->r_ctl.rc_next; 6015 } else { 6016 /* No hints sorry */ 6017 rsm = NULL; 6018 } 6019 if ((rsm) && (rsm->r_start == seq_out)) { 6020 /* 6021 * We used rc_next or hintrsm to retransmit, hopefully the 6022 * likely case. 6023 */ 6024 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6025 if (len == 0) { 6026 return; 6027 } else { 6028 goto more; 6029 } 6030 } 6031 /* Ok it was not the last pointer go through it the hard way. */ 6032 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6033 if (rsm->r_start == seq_out) { 6034 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6035 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 6036 if (len == 0) { 6037 return; 6038 } else { 6039 continue; 6040 } 6041 } 6042 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 6043 /* Transmitted within this piece */ 6044 /* 6045 * Ok we must split off the front and then let the 6046 * update do the rest 6047 */ 6048 nrsm = bbr_alloc_full_limit(bbr); 6049 if (nrsm == NULL) { 6050 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 6051 return; 6052 } 6053 /* 6054 * copy rsm to nrsm and then trim the front of rsm 6055 * to not include this part. 6056 */ 6057 bbr_clone_rsm(bbr, nrsm, rsm, seq_out); 6058 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 6059 if (rsm->r_in_tmap) { 6060 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6061 nrsm->r_in_tmap = 1; 6062 } 6063 rsm->r_flags &= (~BBR_HAS_FIN); 6064 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time); 6065 if (len == 0) { 6066 return; 6067 } 6068 } 6069 } 6070 /* 6071 * Hmm not found in map did they retransmit both old and on into the 6072 * new? 6073 */ 6074 if (seq_out == tp->snd_max) { 6075 goto again; 6076 } else if (SEQ_LT(seq_out, tp->snd_max)) { 6077 #ifdef BBR_INVARIANTS 6078 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 6079 seq_out, len, tp->snd_una, tp->snd_max); 6080 printf("Starting Dump of all rack entries\n"); 6081 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6082 printf("rsm:%p start:%u end:%u\n", 6083 rsm, rsm->r_start, rsm->r_end); 6084 } 6085 printf("Dump complete\n"); 6086 panic("seq_out not found rack:%p tp:%p", 6087 bbr, tp); 6088 #endif 6089 } else { 6090 #ifdef BBR_INVARIANTS 6091 /* 6092 * Hmm beyond sndmax? (only if we are using the new rtt-pack 6093 * flag) 6094 */ 6095 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 6096 seq_out, len, tp->snd_max, tp); 6097 #endif 6098 } 6099 } 6100 6101 static void 6102 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt) 6103 { 6104 /* 6105 * Collapse timeout back the cum-ack moved. 6106 */ 6107 tp->t_rxtshift = 0; 6108 tp->t_softerror = 0; 6109 } 6110 6111 static void 6112 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin) 6113 { 6114 bbr->rtt_valid = 1; 6115 bbr->r_ctl.cur_rtt = rtt_usecs; 6116 bbr->r_ctl.ts_in = tsin; 6117 if (rsm_send_time) 6118 bbr->r_ctl.cur_rtt_send_time = rsm_send_time; 6119 } 6120 6121 static void 6122 bbr_make_timestamp_determination(struct tcp_bbr *bbr) 6123 { 6124 /** 6125 * We have in our bbr control: 6126 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp). 6127 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts). 6128 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts) 6129 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time) 6130 * 6131 * Now we can calculate the time between the sends by doing: 6132 * 6133 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts 6134 * 6135 * And the peer's time between receiving them by doing: 6136 * 6137 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp 6138 * 6139 * We want to figure out if the timestamp values are in msec, 10msec or usec. 6140 * We also may find that we can't use the timestamps if say we see 6141 * that the peer_delta indicates that though we may have taken 10ms to 6142 * pace out the data, it only saw 1ms between the two packets. This would 6143 * indicate that somewhere on the path is a batching entity that is giving 6144 * out time-slices of the actual b/w. This would mean we could not use 6145 * reliably the peers timestamps. 6146 * 6147 * We expect delta > peer_delta initially. Until we figure out the 6148 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio. 6149 * If we place 1000 there then its a ms vs our usec. If we place 10000 there 6150 * then its 10ms vs our usec. If the peer is running a usec clock we would 6151 * put a 1 there. If the value is faster then ours, we will disable the 6152 * use of timestamps (though we could revist this later if we find it to be not 6153 * just an isolated one or two flows)). 6154 * 6155 * To detect the batching middle boxes we will come up with our compensation and 6156 * if with it in place, we find the peer is drastically off (by some margin) in 6157 * the smaller direction, then we will assume the worst case and disable use of timestamps. 6158 * 6159 */ 6160 uint64_t delta, peer_delta, delta_up; 6161 6162 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts; 6163 if (delta < bbr_min_usec_delta) { 6164 /* 6165 * Have not seen a min amount of time 6166 * between our send times so we can 6167 * make a determination of the timestamp 6168 * yet. 6169 */ 6170 return; 6171 } 6172 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp; 6173 if (peer_delta < bbr_min_peer_delta) { 6174 /* 6175 * We may have enough in the form of 6176 * our delta but the peers number 6177 * has not changed that much. It could 6178 * be its clock ratio is such that 6179 * we need more data (10ms tick) or 6180 * there may be other compression scenarios 6181 * going on. In any event we need the 6182 * spread to be larger. 6183 */ 6184 return; 6185 } 6186 /* Ok lets first see which way our delta is going */ 6187 if (peer_delta > delta) { 6188 /* Very unlikely, the peer without 6189 * compensation shows that it saw 6190 * the two sends arrive further apart 6191 * then we saw then in micro-seconds. 6192 */ 6193 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) { 6194 /* well it looks like the peer is a micro-second clock. */ 6195 bbr->rc_ts_clock_set = 1; 6196 bbr->r_ctl.bbr_peer_tsratio = 1; 6197 } else { 6198 bbr->rc_ts_cant_be_used = 1; 6199 bbr->rc_ts_clock_set = 1; 6200 } 6201 return; 6202 } 6203 /* Ok we know that the peer_delta is smaller than our send distance */ 6204 bbr->rc_ts_clock_set = 1; 6205 /* First question is it within the percentage that they are using usec time? */ 6206 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent; 6207 if ((peer_delta + delta_up) >= delta) { 6208 /* Its a usec clock */ 6209 bbr->r_ctl.bbr_peer_tsratio = 1; 6210 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6211 return; 6212 } 6213 /* Ok if not usec, what about 10usec (though unlikely)? */ 6214 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent; 6215 if (((peer_delta * 10) + delta_up) >= delta) { 6216 bbr->r_ctl.bbr_peer_tsratio = 10; 6217 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6218 return; 6219 } 6220 /* And what about 100usec (though again unlikely)? */ 6221 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent; 6222 if (((peer_delta * 100) + delta_up) >= delta) { 6223 bbr->r_ctl.bbr_peer_tsratio = 100; 6224 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6225 return; 6226 } 6227 /* And how about 1 msec (the most likely one)? */ 6228 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent; 6229 if (((peer_delta * 1000) + delta_up) >= delta) { 6230 bbr->r_ctl.bbr_peer_tsratio = 1000; 6231 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6232 return; 6233 } 6234 /* Ok if not msec could it be 10 msec? */ 6235 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent; 6236 if (((peer_delta * 10000) + delta_up) >= delta) { 6237 bbr->r_ctl.bbr_peer_tsratio = 10000; 6238 return; 6239 } 6240 /* If we fall down here the clock tick so slowly we can't use it */ 6241 bbr->rc_ts_cant_be_used = 1; 6242 bbr->r_ctl.bbr_peer_tsratio = 0; 6243 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6244 } 6245 6246 /* 6247 * Collect new round-trip time estimate 6248 * and update averages and current timeout. 6249 */ 6250 static void 6251 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts) 6252 { 6253 int32_t delta; 6254 uint32_t rtt, tsin; 6255 int32_t rtt_ticks; 6256 6257 if (bbr->rtt_valid == 0) 6258 /* No valid sample */ 6259 return; 6260 6261 rtt = bbr->r_ctl.cur_rtt; 6262 tsin = bbr->r_ctl.ts_in; 6263 if (bbr->rc_prtt_set_ts) { 6264 /* 6265 * We are to force feed the rttProp filter due 6266 * to an entry into PROBE_RTT. This assures 6267 * that the times are sync'd between when we 6268 * go into PROBE_RTT and the filter expiration. 6269 * 6270 * Google does not use a true filter, so they do 6271 * this implicitly since they only keep one value 6272 * and when they enter probe-rtt they update the 6273 * value to the newest rtt. 6274 */ 6275 uint32_t rtt_prop; 6276 6277 bbr->rc_prtt_set_ts = 0; 6278 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 6279 if (rtt > rtt_prop) 6280 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts); 6281 else 6282 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6283 } 6284 #ifdef STATS 6285 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt)); 6286 #endif 6287 if (bbr->rc_ack_was_delayed) 6288 rtt += bbr->r_ctl.rc_ack_hdwr_delay; 6289 6290 if (rtt < bbr->r_ctl.rc_lowest_rtt) 6291 bbr->r_ctl.rc_lowest_rtt = rtt; 6292 bbr_log_rtt_sample(bbr, rtt, tsin); 6293 if (bbr->r_init_rtt) { 6294 /* 6295 * The initial rtt is not-trusted, nuke it and lets get 6296 * our first valid measurement in. 6297 */ 6298 bbr->r_init_rtt = 0; 6299 tp->t_srtt = 0; 6300 } 6301 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) { 6302 /* 6303 * So we have not yet figured out 6304 * what the peers TSTMP value is 6305 * in (most likely ms). We need a 6306 * series of cum-ack's to determine 6307 * this reliably. 6308 */ 6309 if (bbr->rc_ack_is_cumack) { 6310 if (bbr->rc_ts_data_set) { 6311 /* Lets attempt to determine the timestamp granularity. */ 6312 bbr_make_timestamp_determination(bbr); 6313 } else { 6314 bbr->rc_ts_data_set = 1; 6315 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts; 6316 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time; 6317 } 6318 } else { 6319 /* 6320 * We have to have consecutive acks 6321 * reset any "filled" state to none. 6322 */ 6323 bbr->rc_ts_data_set = 0; 6324 } 6325 } 6326 /* Round it up */ 6327 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1))); 6328 if (rtt_ticks == 0) 6329 rtt_ticks = 1; 6330 if (tp->t_srtt != 0) { 6331 /* 6332 * srtt is stored as fixed point with 5 bits after the 6333 * binary point (i.e., scaled by 8). The following magic is 6334 * equivalent to the smoothing algorithm in rfc793 with an 6335 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 6336 * Adjust rtt to origin 0. 6337 */ 6338 6339 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT) 6340 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 6341 6342 tp->t_srtt += delta; 6343 if (tp->t_srtt <= 0) 6344 tp->t_srtt = 1; 6345 6346 /* 6347 * We accumulate a smoothed rtt variance (actually, a 6348 * smoothed mean difference), then set the retransmit timer 6349 * to smoothed rtt + 4 times the smoothed variance. rttvar 6350 * is stored as fixed point with 4 bits after the binary 6351 * point (scaled by 16). The following is equivalent to 6352 * rfc793 smoothing with an alpha of .75 (rttvar = 6353 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 6354 * wired-in beta. 6355 */ 6356 if (delta < 0) 6357 delta = -delta; 6358 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 6359 tp->t_rttvar += delta; 6360 if (tp->t_rttvar <= 0) 6361 tp->t_rttvar = 1; 6362 } else { 6363 /* 6364 * No rtt measurement yet - use the unsmoothed rtt. Set the 6365 * variance to half the rtt (so our first retransmit happens 6366 * at 3*rtt). 6367 */ 6368 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT; 6369 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1); 6370 } 6371 KMOD_TCPSTAT_INC(tcps_rttupdated); 6372 if (tp->t_rttupdated < UCHAR_MAX) 6373 tp->t_rttupdated++; 6374 #ifdef STATS 6375 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks)); 6376 #endif 6377 /* 6378 * the retransmit should happen at rtt + 4 * rttvar. Because of the 6379 * way we do the smoothing, srtt and rttvar will each average +1/2 6380 * tick of bias. When we compute the retransmit timer, we want 1/2 6381 * tick of rounding and 1 extra tick because of +-1/2 tick 6382 * uncertainty in the firing of the timer. The bias will give us 6383 * exactly the 1.5 tick we need. But, because the bias is 6384 * statistical, we have to test that we don't drop below the minimum 6385 * feasible timer (which is 2 ticks). 6386 */ 6387 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 6388 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2), 6389 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 6390 6391 /* 6392 * We received an ack for a packet that wasn't retransmitted; it is 6393 * probably safe to discard any error indications we've received 6394 * recently. This isn't quite right, but close enough for now (a 6395 * route might have failed after we sent a segment, and the return 6396 * path might not be symmetrical). 6397 */ 6398 tp->t_softerror = 0; 6399 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 6400 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt) 6401 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt; 6402 } 6403 6404 static void 6405 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line) 6406 { 6407 bbr->r_ctl.rc_rtt_shrinks = cts; 6408 if (bbr_can_force_probertt && 6409 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 6410 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 6411 /* 6412 * We should enter probe-rtt its been too long 6413 * since we have been there. 6414 */ 6415 bbr_enter_probe_rtt(bbr, cts, __LINE__); 6416 } else 6417 bbr_check_probe_rtt_limits(bbr, cts); 6418 } 6419 6420 static void 6421 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts) 6422 { 6423 uint64_t orig_bw; 6424 6425 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) { 6426 /* We never apply a zero measurement */ 6427 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0, 6428 0, 0, 0, 0, 0, 0); 6429 return; 6430 } 6431 if (bbr->r_ctl.r_measurement_count < 0xffffffff) 6432 bbr->r_ctl.r_measurement_count++; 6433 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 6434 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch); 6435 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw, 6436 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate), 6437 0, 0, 0, 0, 0, 0); 6438 if (orig_bw && 6439 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) { 6440 if (bbr->bbr_hdrw_pacing) { 6441 /* 6442 * Apply a new rate to the hardware 6443 * possibly. 6444 */ 6445 bbr_update_hardware_pacing_rate(bbr, cts); 6446 } 6447 bbr_set_state_target(bbr, __LINE__); 6448 tcp_bbr_tso_size_check(bbr, cts); 6449 if (bbr->r_recovery_bw) { 6450 bbr_setup_red_bw(bbr, cts); 6451 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW); 6452 } 6453 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate)) 6454 tcp_bbr_tso_size_check(bbr, cts); 6455 } 6456 6457 static void 6458 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6459 { 6460 if (bbr->rc_in_persist == 0) { 6461 /* We log only when not in persist */ 6462 /* Translate to a Bytes Per Second */ 6463 uint64_t tim, bw, ts_diff, ts_bw; 6464 uint32_t delivered; 6465 6466 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6467 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6468 else 6469 tim = 1; 6470 /* 6471 * Now that we have processed the tim (skipping the sample 6472 * or possibly updating the time, go ahead and 6473 * calculate the cdr. 6474 */ 6475 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6476 bw = (uint64_t)delivered; 6477 bw *= (uint64_t)USECS_IN_SECOND; 6478 bw /= tim; 6479 if (bw == 0) { 6480 /* We must have a calculatable amount */ 6481 return; 6482 } 6483 /* 6484 * If we are using this b/w shove it in now so we 6485 * can see in the trace viewer if it gets over-ridden. 6486 */ 6487 if (rsm->r_ts_valid && 6488 bbr->rc_ts_valid && 6489 bbr->rc_ts_clock_set && 6490 (bbr->rc_ts_cant_be_used == 0) && 6491 bbr->rc_use_ts_limit) { 6492 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1); 6493 ts_diff *= bbr->r_ctl.bbr_peer_tsratio; 6494 if ((delivered == 0) || 6495 (rtt < 1000)) { 6496 /* Can't use the ts */ 6497 bbr_log_type_bbrupd(bbr, 61, cts, 6498 ts_diff, 6499 bbr->r_ctl.last_inbound_ts, 6500 rsm->r_del_ack_ts, 0, 6501 0, 0, 0, delivered); 6502 } else { 6503 ts_bw = (uint64_t)delivered; 6504 ts_bw *= (uint64_t)USECS_IN_SECOND; 6505 ts_bw /= ts_diff; 6506 bbr_log_type_bbrupd(bbr, 62, cts, 6507 (ts_bw >> 32), 6508 (ts_bw & 0xffffffff), 0, 0, 6509 0, 0, ts_diff, delivered); 6510 if ((bbr->ts_can_raise) && 6511 (ts_bw > bw)) { 6512 bbr_log_type_bbrupd(bbr, 8, cts, 6513 delivered, 6514 ts_diff, 6515 (bw >> 32), 6516 (bw & 0x00000000ffffffff), 6517 0, 0, 0, 0); 6518 bw = ts_bw; 6519 } else if (ts_bw && (ts_bw < bw)) { 6520 bbr_log_type_bbrupd(bbr, 7, cts, 6521 delivered, 6522 ts_diff, 6523 (bw >> 32), 6524 (bw & 0x00000000ffffffff), 6525 0, 0, 0, 0); 6526 bw = ts_bw; 6527 } 6528 } 6529 } 6530 if (rsm->r_first_sent_time && 6531 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6532 uint64_t sbw, sti; 6533 /* 6534 * We use what was in flight at the time of our 6535 * send and the size of this send to figure 6536 * out what we have been sending at (amount). 6537 * For the time we take from the time of 6538 * the send of the first send outstanding 6539 * until this send plus this sends pacing 6540 * time. This gives us a good calculation 6541 * as to the rate we have been sending at. 6542 */ 6543 6544 sbw = (uint64_t)(rsm->r_flight_at_send); 6545 sbw *= (uint64_t)USECS_IN_SECOND; 6546 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6547 sti += rsm->r_pacing_delay; 6548 sbw /= sti; 6549 if (sbw < bw) { 6550 bbr_log_type_bbrupd(bbr, 6, cts, 6551 delivered, 6552 (uint32_t)sti, 6553 (bw >> 32), 6554 (uint32_t)bw, 6555 rsm->r_first_sent_time, 0, (sbw >> 32), 6556 (uint32_t)sbw); 6557 bw = sbw; 6558 } 6559 } 6560 /* Use the google algorithm for b/w measurements */ 6561 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6562 if ((rsm->r_app_limited == 0) || 6563 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) { 6564 tcp_bbr_commit_bw(bbr, cts); 6565 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6566 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6567 } 6568 } 6569 } 6570 6571 static void 6572 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6573 { 6574 if (bbr->rc_in_persist == 0) { 6575 /* We log only when not in persist */ 6576 /* Translate to a Bytes Per Second */ 6577 uint64_t tim, bw; 6578 uint32_t delivered; 6579 int no_apply = 0; 6580 6581 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6582 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6583 else 6584 tim = 1; 6585 /* 6586 * Now that we have processed the tim (skipping the sample 6587 * or possibly updating the time, go ahead and 6588 * calculate the cdr. 6589 */ 6590 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6591 bw = (uint64_t)delivered; 6592 bw *= (uint64_t)USECS_IN_SECOND; 6593 bw /= tim; 6594 if (tim < bbr->r_ctl.rc_lowest_rtt) { 6595 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6596 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6597 6598 no_apply = 1; 6599 } 6600 /* 6601 * If we are using this b/w shove it in now so we 6602 * can see in the trace viewer if it gets over-ridden. 6603 */ 6604 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6605 /* Gate by the sending rate */ 6606 if (rsm->r_first_sent_time && 6607 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6608 uint64_t sbw, sti; 6609 /* 6610 * We use what was in flight at the time of our 6611 * send and the size of this send to figure 6612 * out what we have been sending at (amount). 6613 * For the time we take from the time of 6614 * the send of the first send outstanding 6615 * until this send plus this sends pacing 6616 * time. This gives us a good calculation 6617 * as to the rate we have been sending at. 6618 */ 6619 6620 sbw = (uint64_t)(rsm->r_flight_at_send); 6621 sbw *= (uint64_t)USECS_IN_SECOND; 6622 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6623 sti += rsm->r_pacing_delay; 6624 sbw /= sti; 6625 if (sbw < bw) { 6626 bbr_log_type_bbrupd(bbr, 6, cts, 6627 delivered, 6628 (uint32_t)sti, 6629 (bw >> 32), 6630 (uint32_t)bw, 6631 rsm->r_first_sent_time, 0, (sbw >> 32), 6632 (uint32_t)sbw); 6633 bw = sbw; 6634 } 6635 if ((sti > tim) && 6636 (sti < bbr->r_ctl.rc_lowest_rtt)) { 6637 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6638 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6639 no_apply = 1; 6640 } else 6641 no_apply = 0; 6642 } 6643 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6644 if ((no_apply == 0) && 6645 ((rsm->r_app_limited == 0) || 6646 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) { 6647 tcp_bbr_commit_bw(bbr, cts); 6648 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6649 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6650 } 6651 } 6652 } 6653 6654 static void 6655 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin, 6656 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to) 6657 { 6658 uint64_t old_rttprop; 6659 6660 /* Update our delivery time and amount */ 6661 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start); 6662 bbr->r_ctl.rc_del_time = cts; 6663 if (rtt == 0) { 6664 /* 6665 * 0 means its a retransmit, for now we don't use these for 6666 * the rest of BBR. 6667 */ 6668 return; 6669 } 6670 if ((bbr->rc_use_google == 0) && 6671 (match != BBR_RTT_BY_EXACTMATCH) && 6672 (match != BBR_RTT_BY_TIMESTAMP)){ 6673 /* 6674 * We get a lot of rtt updates, lets not pay attention to 6675 * any that are not an exact match. That way we don't have 6676 * to worry about timestamps and the whole nonsense of 6677 * unsure if its a retransmission etc (if we ever had the 6678 * timestamp fixed to always have the last thing sent this 6679 * would not be a issue). 6680 */ 6681 return; 6682 } 6683 if ((bbr_no_retran && bbr->rc_use_google) && 6684 (match != BBR_RTT_BY_EXACTMATCH) && 6685 (match != BBR_RTT_BY_TIMESTAMP)){ 6686 /* 6687 * We only do measurements in google mode 6688 * with bbr_no_retran on for sure things. 6689 */ 6690 return; 6691 } 6692 /* Only update srtt if we know by exact match */ 6693 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin); 6694 if (ack_type == BBR_CUM_ACKED) 6695 bbr->rc_ack_is_cumack = 1; 6696 else 6697 bbr->rc_ack_is_cumack = 0; 6698 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP); 6699 /* 6700 * Note the following code differs to the original 6701 * BBR spec. It calls for <= not <. However after a 6702 * long discussion in email with Neal, he acknowledged 6703 * that it should be < than so that we will have flows 6704 * going into probe-rtt (we were seeing cases where that 6705 * did not happen and caused ugly things to occur). We 6706 * have added this agreed upon fix to our code base. 6707 */ 6708 if (rtt < old_rttprop) { 6709 /* Update when we last saw a rtt drop */ 6710 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0); 6711 bbr_set_reduced_rtt(bbr, cts, __LINE__); 6712 } 6713 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts, 6714 match, rsm->r_start, rsm->r_flags); 6715 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6716 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) { 6717 /* 6718 * The RTT-prop moved, reset the target (may be a 6719 * nop for some states). 6720 */ 6721 bbr_set_state_target(bbr, __LINE__); 6722 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) 6723 bbr_log_rtt_shrinks(bbr, cts, 0, 0, 6724 __LINE__, BBR_RTTS_NEW_TARGET, 0); 6725 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP)) 6726 /* It went up */ 6727 bbr_check_probe_rtt_limits(bbr, cts); 6728 } 6729 if ((bbr->rc_use_google == 0) && 6730 (match == BBR_RTT_BY_TIMESTAMP)) { 6731 /* 6732 * We don't do b/w update with 6733 * these since they are not really 6734 * reliable. 6735 */ 6736 return; 6737 } 6738 if (bbr->r_ctl.r_app_limited_until && 6739 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) { 6740 /* We are no longer app-limited */ 6741 bbr->r_ctl.r_app_limited_until = 0; 6742 } 6743 if (bbr->rc_use_google) { 6744 bbr_google_measurement(bbr, rsm, rtt, cts); 6745 } else { 6746 bbr_nf_measurement(bbr, rsm, rtt, cts); 6747 } 6748 } 6749 6750 /* 6751 * Convert a timestamp that the main stack 6752 * uses (milliseconds) into one that bbr uses 6753 * (microseconds). Return that converted timestamp. 6754 */ 6755 static uint32_t 6756 bbr_ts_convert(uint32_t cts) { 6757 uint32_t sec, msec; 6758 6759 sec = cts / MS_IN_USEC; 6760 msec = cts - (MS_IN_USEC * sec); 6761 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC)); 6762 } 6763 6764 /* 6765 * Return 0 if we did not update the RTT time, return 6766 * 1 if we did. 6767 */ 6768 static int 6769 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, 6770 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack) 6771 { 6772 int32_t i; 6773 uint32_t t, uts = 0; 6774 6775 if ((rsm->r_flags & BBR_ACKED) || 6776 (rsm->r_flags & BBR_WAS_RENEGED) || 6777 (rsm->r_flags & BBR_RXT_CLEARED)) { 6778 /* Already done */ 6779 return (0); 6780 } 6781 if (rsm->r_rtt_not_allowed) { 6782 /* Not allowed */ 6783 return (0); 6784 } 6785 if (rsm->r_rtr_cnt == 1) { 6786 /* 6787 * Only one transmit. Hopefully the normal case. 6788 */ 6789 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0])) 6790 t = cts - rsm->r_tim_lastsent[0]; 6791 else 6792 t = 1; 6793 if ((int)t <= 0) 6794 t = 1; 6795 bbr->r_ctl.rc_last_rtt = t; 6796 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6797 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to); 6798 return (1); 6799 } 6800 /* Convert to usecs */ 6801 if ((bbr_can_use_ts_for_rtt == 1) && 6802 (bbr->rc_use_google == 1) && 6803 (ack_type == BBR_CUM_ACKED) && 6804 (to->to_flags & TOF_TS) && 6805 (to->to_tsecr != 0)) { 6806 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr; 6807 if (t < 1) 6808 t = 1; 6809 t *= MS_IN_USEC; 6810 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6811 BBR_RTT_BY_TIMESTAMP, 6812 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)], 6813 ack_type, to); 6814 return (1); 6815 } 6816 uts = bbr_ts_convert(to->to_tsecr); 6817 if ((to->to_flags & TOF_TS) && 6818 (to->to_tsecr != 0) && 6819 (ack_type == BBR_CUM_ACKED) && 6820 ((rsm->r_flags & BBR_OVERMAX) == 0)) { 6821 /* 6822 * Now which timestamp does it match? In this block the ACK 6823 * may be coming from a previous transmission. 6824 */ 6825 uint32_t fudge; 6826 6827 fudge = BBR_TIMER_FUDGE; 6828 for (i = 0; i < rsm->r_rtr_cnt; i++) { 6829 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) && 6830 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) { 6831 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6832 t = cts - rsm->r_tim_lastsent[i]; 6833 else 6834 t = 1; 6835 if ((int)t <= 0) 6836 t = 1; 6837 bbr->r_ctl.rc_last_rtt = t; 6838 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING, 6839 rsm->r_tim_lastsent[i], ack_type, to); 6840 if ((i + 1) < rsm->r_rtr_cnt) { 6841 /* Likely */ 6842 return (0); 6843 } else if (rsm->r_flags & BBR_TLP) { 6844 bbr->rc_tlp_rtx_out = 0; 6845 } 6846 return (1); 6847 } 6848 } 6849 /* Fall through if we can't find a matching timestamp */ 6850 } 6851 /* 6852 * Ok its a SACK block that we retransmitted. or a windows 6853 * machine without timestamps. We can tell nothing from the 6854 * time-stamp since its not there or the time the peer last 6855 * recieved a segment that moved forward its cum-ack point. 6856 * 6857 * Lets look at the last retransmit and see what we can tell 6858 * (with BBR for space we only keep 2 note we have to keep 6859 * at least 2 so the map can not be condensed more). 6860 */ 6861 i = rsm->r_rtr_cnt - 1; 6862 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6863 t = cts - rsm->r_tim_lastsent[i]; 6864 else 6865 goto not_sure; 6866 if (t < bbr->r_ctl.rc_lowest_rtt) { 6867 /* 6868 * We retransmitted and the ack came back in less 6869 * than the smallest rtt we have observed in the 6870 * windowed rtt. We most likey did an improper 6871 * retransmit as outlined in 4.2 Step 3 point 2 in 6872 * the rack-draft. 6873 * 6874 * Use the prior transmission to update all the 6875 * information as long as there is only one prior 6876 * transmission. 6877 */ 6878 if ((rsm->r_flags & BBR_OVERMAX) == 0) { 6879 #ifdef BBR_INVARIANTS 6880 if (rsm->r_rtr_cnt == 1) 6881 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags); 6882 #endif 6883 i = rsm->r_rtr_cnt - 2; 6884 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6885 t = cts - rsm->r_tim_lastsent[i]; 6886 else 6887 t = 1; 6888 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET, 6889 rsm->r_tim_lastsent[i], ack_type, to); 6890 return (0); 6891 } else { 6892 /* 6893 * Too many prior transmissions, just 6894 * updated BBR delivered 6895 */ 6896 not_sure: 6897 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6898 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6899 } 6900 } else { 6901 /* 6902 * We retransmitted it and the retransmit did the 6903 * job. 6904 */ 6905 if (rsm->r_flags & BBR_TLP) 6906 bbr->rc_tlp_rtx_out = 0; 6907 if ((rsm->r_flags & BBR_OVERMAX) == 0) 6908 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, 6909 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to); 6910 else 6911 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6912 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6913 return (1); 6914 } 6915 return (0); 6916 } 6917 6918 /* 6919 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 6920 */ 6921 static void 6922 bbr_log_sack_passed(struct tcpcb *tp, 6923 struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 6924 { 6925 struct bbr_sendmap *nrsm; 6926 6927 nrsm = rsm; 6928 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap, 6929 bbr_head, r_tnext) { 6930 if (nrsm == rsm) { 6931 /* Skip original segment he is acked */ 6932 continue; 6933 } 6934 if (nrsm->r_flags & BBR_ACKED) { 6935 /* Skip ack'd segments */ 6936 continue; 6937 } 6938 if (nrsm->r_flags & BBR_SACK_PASSED) { 6939 /* 6940 * We found one that is already marked 6941 * passed, we have been here before and 6942 * so all others below this are marked. 6943 */ 6944 break; 6945 } 6946 BBR_STAT_INC(bbr_sack_passed); 6947 nrsm->r_flags |= BBR_SACK_PASSED; 6948 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) && 6949 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) { 6950 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start; 6951 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start; 6952 nrsm->r_flags |= BBR_MARKED_LOST; 6953 } 6954 nrsm->r_flags &= ~BBR_WAS_SACKPASS; 6955 } 6956 } 6957 6958 /* 6959 * Returns the number of bytes that were 6960 * newly ack'd by sack blocks. 6961 */ 6962 static uint32_t 6963 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack, 6964 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts) 6965 { 6966 int32_t times = 0; 6967 uint32_t start, end, changed = 0; 6968 struct bbr_sendmap *rsm, *nrsm; 6969 int32_t used_ref = 1; 6970 uint8_t went_back = 0, went_fwd = 0; 6971 6972 start = sack->start; 6973 end = sack->end; 6974 rsm = *prsm; 6975 if (rsm == NULL) 6976 used_ref = 0; 6977 6978 /* Do we locate the block behind where we last were? */ 6979 if (rsm && SEQ_LT(start, rsm->r_start)) { 6980 went_back = 1; 6981 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 6982 if (SEQ_GEQ(start, rsm->r_start) && 6983 SEQ_LT(start, rsm->r_end)) { 6984 goto do_rest_ofb; 6985 } 6986 } 6987 } 6988 start_at_beginning: 6989 went_fwd = 1; 6990 /* 6991 * Ok lets locate the block where this guy is fwd from rsm (if its 6992 * set) 6993 */ 6994 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) { 6995 if (SEQ_GEQ(start, rsm->r_start) && 6996 SEQ_LT(start, rsm->r_end)) { 6997 break; 6998 } 6999 } 7000 do_rest_ofb: 7001 if (rsm == NULL) { 7002 /* 7003 * This happens when we get duplicate sack blocks with the 7004 * same end. For example SACK 4: 100 SACK 3: 100 The sort 7005 * will not change there location so we would just start at 7006 * the end of the first one and get lost. 7007 */ 7008 if (tp->t_flags & TF_SENTFIN) { 7009 /* 7010 * Check to see if we have not logged the FIN that 7011 * went out. 7012 */ 7013 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7014 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 7015 /* 7016 * Ok we did not get the FIN logged. 7017 */ 7018 nrsm->r_end++; 7019 rsm = nrsm; 7020 goto do_rest_ofb; 7021 } 7022 } 7023 if (times == 1) { 7024 #ifdef BBR_INVARIANTS 7025 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p", 7026 tp, bbr, sack, to, prsm); 7027 #else 7028 goto out; 7029 #endif 7030 } 7031 times++; 7032 BBR_STAT_INC(bbr_sack_proc_restart); 7033 rsm = NULL; 7034 goto start_at_beginning; 7035 } 7036 /* Ok we have an ACK for some piece of rsm */ 7037 if (rsm->r_start != start) { 7038 /* 7039 * Need to split this in two pieces the before and after. 7040 */ 7041 if (bbr_sack_mergable(rsm, start, end)) 7042 nrsm = bbr_alloc_full_limit(bbr); 7043 else 7044 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7045 if (nrsm == NULL) { 7046 /* We could not allocate ignore the sack */ 7047 struct sackblk blk; 7048 7049 blk.start = start; 7050 blk.end = end; 7051 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7052 goto out; 7053 } 7054 bbr_clone_rsm(bbr, nrsm, rsm, start); 7055 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7056 if (rsm->r_in_tmap) { 7057 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7058 nrsm->r_in_tmap = 1; 7059 } 7060 rsm->r_flags &= (~BBR_HAS_FIN); 7061 rsm = nrsm; 7062 } 7063 if (SEQ_GEQ(end, rsm->r_end)) { 7064 /* 7065 * The end of this block is either beyond this guy or right 7066 * at this guy. 7067 */ 7068 if ((rsm->r_flags & BBR_ACKED) == 0) { 7069 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7070 changed += (rsm->r_end - rsm->r_start); 7071 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7072 bbr_log_sack_passed(tp, bbr, rsm); 7073 if (rsm->r_flags & BBR_MARKED_LOST) { 7074 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7075 } 7076 /* Is Reordering occuring? */ 7077 if (rsm->r_flags & BBR_SACK_PASSED) { 7078 BBR_STAT_INC(bbr_reorder_seen); 7079 bbr->r_ctl.rc_reorder_ts = cts; 7080 if (rsm->r_flags & BBR_MARKED_LOST) { 7081 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7082 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7083 /* LT sampling also needs adjustment */ 7084 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7085 } 7086 } 7087 rsm->r_flags |= BBR_ACKED; 7088 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7089 if (rsm->r_in_tmap) { 7090 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7091 rsm->r_in_tmap = 0; 7092 } 7093 } 7094 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7095 if (end == rsm->r_end) { 7096 /* This block only - done */ 7097 goto out; 7098 } 7099 /* There is more not coverend by this rsm move on */ 7100 start = rsm->r_end; 7101 nrsm = TAILQ_NEXT(rsm, r_next); 7102 rsm = nrsm; 7103 times = 0; 7104 goto do_rest_ofb; 7105 } 7106 if (rsm->r_flags & BBR_ACKED) { 7107 /* Been here done that */ 7108 goto out; 7109 } 7110 /* Ok we need to split off this one at the tail */ 7111 if (bbr_sack_mergable(rsm, start, end)) 7112 nrsm = bbr_alloc_full_limit(bbr); 7113 else 7114 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7115 if (nrsm == NULL) { 7116 /* failed XXXrrs what can we do but loose the sack info? */ 7117 struct sackblk blk; 7118 7119 blk.start = start; 7120 blk.end = end; 7121 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7122 goto out; 7123 } 7124 /* Clone it */ 7125 bbr_clone_rsm(bbr, nrsm, rsm, end); 7126 /* The sack block does not cover this guy fully */ 7127 rsm->r_flags &= (~BBR_HAS_FIN); 7128 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7129 if (rsm->r_in_tmap) { 7130 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7131 nrsm->r_in_tmap = 1; 7132 } 7133 nrsm->r_dupack = 0; 7134 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7135 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7136 changed += (rsm->r_end - rsm->r_start); 7137 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7138 bbr_log_sack_passed(tp, bbr, rsm); 7139 /* Is Reordering occuring? */ 7140 if (rsm->r_flags & BBR_MARKED_LOST) { 7141 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7142 } 7143 if (rsm->r_flags & BBR_SACK_PASSED) { 7144 BBR_STAT_INC(bbr_reorder_seen); 7145 bbr->r_ctl.rc_reorder_ts = cts; 7146 if (rsm->r_flags & BBR_MARKED_LOST) { 7147 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7148 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7149 /* LT sampling also needs adjustment */ 7150 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7151 } 7152 } 7153 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7154 rsm->r_flags |= BBR_ACKED; 7155 if (rsm->r_in_tmap) { 7156 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7157 rsm->r_in_tmap = 0; 7158 } 7159 out: 7160 if (rsm && (rsm->r_flags & BBR_ACKED)) { 7161 /* 7162 * Now can we merge this newly acked 7163 * block with either the previous or 7164 * next block? 7165 */ 7166 nrsm = TAILQ_NEXT(rsm, r_next); 7167 if (nrsm && 7168 (nrsm->r_flags & BBR_ACKED)) { 7169 /* yep this and next can be merged */ 7170 rsm = bbr_merge_rsm(bbr, rsm, nrsm); 7171 } 7172 /* Now what about the previous? */ 7173 nrsm = TAILQ_PREV(rsm, bbr_head, r_next); 7174 if (nrsm && 7175 (nrsm->r_flags & BBR_ACKED)) { 7176 /* yep the previous and this can be merged */ 7177 rsm = bbr_merge_rsm(bbr, nrsm, rsm); 7178 } 7179 } 7180 if (used_ref == 0) { 7181 BBR_STAT_INC(bbr_sack_proc_all); 7182 } else { 7183 BBR_STAT_INC(bbr_sack_proc_short); 7184 } 7185 if (went_fwd && went_back) { 7186 BBR_STAT_INC(bbr_sack_search_both); 7187 } else if (went_fwd) { 7188 BBR_STAT_INC(bbr_sack_search_fwd); 7189 } else if (went_back) { 7190 BBR_STAT_INC(bbr_sack_search_back); 7191 } 7192 /* Save off where the next seq is */ 7193 if (rsm) 7194 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 7195 else 7196 bbr->r_ctl.rc_sacklast = NULL; 7197 *prsm = rsm; 7198 return (changed); 7199 } 7200 7201 static void inline 7202 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack) 7203 { 7204 struct bbr_sendmap *tmap; 7205 7206 BBR_STAT_INC(bbr_reneges_seen); 7207 tmap = NULL; 7208 while (rsm && (rsm->r_flags & BBR_ACKED)) { 7209 /* Its no longer sacked, mark it so */ 7210 uint32_t oflags; 7211 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7212 #ifdef BBR_INVARIANTS 7213 if (rsm->r_in_tmap) { 7214 panic("bbr:%p rsm:%p flags:0x%x in tmap?", 7215 bbr, rsm, rsm->r_flags); 7216 } 7217 #endif 7218 oflags = rsm->r_flags; 7219 if (rsm->r_flags & BBR_MARKED_LOST) { 7220 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7221 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7222 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7223 /* LT sampling also needs adjustment */ 7224 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7225 } 7226 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST); 7227 rsm->r_flags |= BBR_WAS_RENEGED; 7228 rsm->r_flags |= BBR_RXT_CLEARED; 7229 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__); 7230 /* Rebuild it into our tmap */ 7231 if (tmap == NULL) { 7232 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7233 tmap = rsm; 7234 } else { 7235 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext); 7236 tmap = rsm; 7237 } 7238 tmap->r_in_tmap = 1; 7239 /* 7240 * XXXrrs Delivered? Should we do anything here? 7241 * 7242 * Of course we don't on a rxt timeout so maybe its ok that 7243 * we don't? 7244 * 7245 * For now lets not. 7246 */ 7247 rsm = TAILQ_NEXT(rsm, r_next); 7248 } 7249 /* 7250 * Now lets possibly clear the sack filter so we start recognizing 7251 * sacks that cover this area. 7252 */ 7253 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack); 7254 } 7255 7256 static void 7257 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to) 7258 { 7259 struct tcp_bbr *bbr; 7260 struct bbr_sendmap *rsm; 7261 uint32_t cts; 7262 7263 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7264 cts = bbr->r_ctl.rc_rcvtime; 7265 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7266 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) { 7267 if ((rsm->r_end - rsm->r_start) <= 1) { 7268 /* Log out the SYN completely */ 7269 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7270 rsm->r_rtr_bytes = 0; 7271 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7272 if (rsm->r_in_tmap) { 7273 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7274 rsm->r_in_tmap = 0; 7275 } 7276 if (bbr->r_ctl.rc_next == rsm) { 7277 /* scoot along the marker */ 7278 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7279 } 7280 if (to != NULL) 7281 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0); 7282 bbr_free(bbr, rsm); 7283 } else { 7284 /* There is more (Fast open)? strip out SYN. */ 7285 rsm->r_flags &= ~BBR_HAS_SYN; 7286 rsm->r_start++; 7287 } 7288 } 7289 } 7290 7291 /* 7292 * Returns the number of bytes that were 7293 * acknowledged by SACK blocks. 7294 */ 7295 7296 static uint32_t 7297 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, 7298 uint32_t *prev_acked) 7299 { 7300 uint32_t changed, last_seq, entered_recovery = 0; 7301 struct tcp_bbr *bbr; 7302 struct bbr_sendmap *rsm; 7303 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 7304 register uint32_t th_ack; 7305 int32_t i, j, k, new_sb, num_sack_blks = 0; 7306 uint32_t cts, acked, ack_point, sack_changed = 0; 7307 uint32_t p_maxseg, maxseg, p_acked = 0; 7308 7309 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7310 if (tcp_get_flags(th) & TH_RST) { 7311 /* We don't log resets */ 7312 return (0); 7313 } 7314 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7315 cts = bbr->r_ctl.rc_rcvtime; 7316 7317 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7318 changed = 0; 7319 maxseg = tp->t_maxseg - bbr->rc_last_options; 7320 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg); 7321 th_ack = th->th_ack; 7322 if (SEQ_GT(th_ack, tp->snd_una)) { 7323 acked = th_ack - tp->snd_una; 7324 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__); 7325 bbr->rc_tp->t_acktime = ticks; 7326 } else 7327 acked = 0; 7328 if (SEQ_LEQ(th_ack, tp->snd_una)) { 7329 /* Only sent here for sack processing */ 7330 goto proc_sack; 7331 } 7332 if (rsm && SEQ_GT(th_ack, rsm->r_start)) { 7333 changed = th_ack - rsm->r_start; 7334 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) { 7335 /* 7336 * For the SYN incoming case we will not have called 7337 * tcp_output for the sending of the SYN, so there will be 7338 * no map. All other cases should probably be a panic. 7339 */ 7340 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) { 7341 /* 7342 * We have a timestamp that can be used to generate 7343 * an initial RTT. 7344 */ 7345 uint32_t ts, now, rtt; 7346 7347 ts = bbr_ts_convert(to->to_tsecr); 7348 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv)); 7349 rtt = now - ts; 7350 if (rtt < 1) 7351 rtt = 1; 7352 bbr_log_type_bbrrttprop(bbr, rtt, 7353 tp->iss, 0, cts, 7354 BBR_RTT_BY_TIMESTAMP, tp->iss, 0); 7355 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 7356 changed = 1; 7357 bbr->r_wanted_output = 1; 7358 goto out; 7359 } 7360 goto proc_sack; 7361 } else if (rsm == NULL) { 7362 goto out; 7363 } 7364 if (changed) { 7365 /* 7366 * The ACK point is advancing to th_ack, we must drop off 7367 * the packets in the rack log and calculate any eligble 7368 * RTT's. 7369 */ 7370 bbr->r_wanted_output = 1; 7371 more: 7372 if (rsm == NULL) { 7373 if (tp->t_flags & TF_SENTFIN) { 7374 /* if we send a FIN we will not hav a map */ 7375 goto proc_sack; 7376 } 7377 #ifdef BBR_INVARIANTS 7378 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n", 7379 tp, 7380 th, tp->t_state, bbr, 7381 tp->snd_una, tp->snd_max, changed); 7382 #endif 7383 goto proc_sack; 7384 } 7385 } 7386 if (SEQ_LT(th_ack, rsm->r_start)) { 7387 /* Huh map is missing this */ 7388 #ifdef BBR_INVARIANTS 7389 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n", 7390 rsm->r_start, 7391 th_ack, tp->t_state, 7392 bbr->r_state, bbr); 7393 panic("th-ack is bad bbr:%p tp:%p", bbr, tp); 7394 #endif 7395 goto proc_sack; 7396 } else if (th_ack == rsm->r_start) { 7397 /* None here to ack */ 7398 goto proc_sack; 7399 } 7400 /* 7401 * Clear the dup ack counter, it will 7402 * either be freed or if there is some 7403 * remaining we need to start it at zero. 7404 */ 7405 rsm->r_dupack = 0; 7406 /* Now do we consume the whole thing? */ 7407 if (SEQ_GEQ(th_ack, rsm->r_end)) { 7408 /* Its all consumed. */ 7409 uint32_t left; 7410 7411 if (rsm->r_flags & BBR_ACKED) { 7412 /* 7413 * It was acked on the scoreboard -- remove it from 7414 * total 7415 */ 7416 p_acked += (rsm->r_end - rsm->r_start); 7417 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7418 if (bbr->r_ctl.rc_sacked == 0) 7419 bbr->r_ctl.rc_sacklast = NULL; 7420 } else { 7421 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack); 7422 if (rsm->r_flags & BBR_MARKED_LOST) { 7423 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7424 } 7425 if (rsm->r_flags & BBR_SACK_PASSED) { 7426 /* 7427 * There are acked segments ACKED on the 7428 * scoreboard further up. We are seeing 7429 * reordering. 7430 */ 7431 BBR_STAT_INC(bbr_reorder_seen); 7432 bbr->r_ctl.rc_reorder_ts = cts; 7433 if (rsm->r_flags & BBR_MARKED_LOST) { 7434 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7435 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7436 /* LT sampling also needs adjustment */ 7437 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7438 } 7439 } 7440 rsm->r_flags &= ~BBR_MARKED_LOST; 7441 } 7442 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7443 rsm->r_rtr_bytes = 0; 7444 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7445 if (rsm->r_in_tmap) { 7446 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7447 rsm->r_in_tmap = 0; 7448 } 7449 if (bbr->r_ctl.rc_next == rsm) { 7450 /* scoot along the marker */ 7451 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7452 } 7453 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7454 /* Adjust the packet counts */ 7455 left = th_ack - rsm->r_end; 7456 /* Free back to zone */ 7457 bbr_free(bbr, rsm); 7458 if (left) { 7459 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7460 goto more; 7461 } 7462 goto proc_sack; 7463 } 7464 if (rsm->r_flags & BBR_ACKED) { 7465 /* 7466 * It was acked on the scoreboard -- remove it from total 7467 * for the part being cum-acked. 7468 */ 7469 p_acked += (rsm->r_end - rsm->r_start); 7470 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 7471 if (bbr->r_ctl.rc_sacked == 0) 7472 bbr->r_ctl.rc_sacklast = NULL; 7473 } else { 7474 /* 7475 * It was acked up to th_ack point for the first time 7476 */ 7477 struct bbr_sendmap lrsm; 7478 7479 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap)); 7480 lrsm.r_end = th_ack; 7481 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack); 7482 } 7483 if ((rsm->r_flags & BBR_MARKED_LOST) && 7484 ((rsm->r_flags & BBR_ACKED) == 0)) { 7485 /* 7486 * It was marked lost and partly ack'd now 7487 * for the first time. We lower the rc_lost_bytes 7488 * and still leave it MARKED. 7489 */ 7490 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start; 7491 } 7492 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7493 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7494 rsm->r_rtr_bytes = 0; 7495 /* adjust packet count */ 7496 rsm->r_start = th_ack; 7497 proc_sack: 7498 /* Check for reneging */ 7499 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7500 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) { 7501 /* 7502 * The peer has moved snd_una up to the edge of this send, 7503 * i.e. one that it had previously acked. The only way that 7504 * can be true if the peer threw away data (space issues) 7505 * that it had previously sacked (else it would have given 7506 * us snd_una up to (rsm->r_end). We need to undo the acked 7507 * markings here. 7508 * 7509 * Note we have to look to make sure th_ack is our 7510 * rsm->r_start in case we get an old ack where th_ack is 7511 * behind snd_una. 7512 */ 7513 bbr_peer_reneges(bbr, rsm, th->th_ack); 7514 } 7515 if ((to->to_flags & TOF_SACK) == 0) { 7516 /* We are done nothing left to log */ 7517 goto out; 7518 } 7519 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7520 if (rsm) { 7521 last_seq = rsm->r_end; 7522 } else { 7523 last_seq = tp->snd_max; 7524 } 7525 /* Sack block processing */ 7526 if (SEQ_GT(th_ack, tp->snd_una)) 7527 ack_point = th_ack; 7528 else 7529 ack_point = tp->snd_una; 7530 for (i = 0; i < to->to_nsacks; i++) { 7531 bcopy((to->to_sacks + i * TCPOLEN_SACK), 7532 &sack, sizeof(sack)); 7533 sack.start = ntohl(sack.start); 7534 sack.end = ntohl(sack.end); 7535 if (SEQ_GT(sack.end, sack.start) && 7536 SEQ_GT(sack.start, ack_point) && 7537 SEQ_LT(sack.start, tp->snd_max) && 7538 SEQ_GT(sack.end, ack_point) && 7539 SEQ_LEQ(sack.end, tp->snd_max)) { 7540 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) && 7541 (SEQ_LT(sack.end, last_seq)) && 7542 ((sack.end - sack.start) < (p_maxseg / 8))) { 7543 /* 7544 * Not the last piece and its smaller than 7545 * 1/8th of a p_maxseg. We ignore this. 7546 */ 7547 BBR_STAT_INC(bbr_runt_sacks); 7548 continue; 7549 } 7550 sack_blocks[num_sack_blks] = sack; 7551 num_sack_blks++; 7552 } else if (SEQ_LEQ(sack.start, th_ack) && 7553 SEQ_LEQ(sack.end, th_ack)) { 7554 /* 7555 * Its a D-SACK block. 7556 */ 7557 tcp_record_dsack(tp, sack.start, sack.end, 0); 7558 } 7559 } 7560 if (num_sack_blks == 0) 7561 goto out; 7562 /* 7563 * Sort the SACK blocks so we can update the rack scoreboard with 7564 * just one pass. 7565 */ 7566 new_sb = sack_filter_blks(&bbr->r_ctl.bbr_sf, sack_blocks, 7567 num_sack_blks, th->th_ack); 7568 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks); 7569 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks); 7570 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb)); 7571 num_sack_blks = new_sb; 7572 if (num_sack_blks < 2) { 7573 goto do_sack_work; 7574 } 7575 /* Sort the sacks */ 7576 for (i = 0; i < num_sack_blks; i++) { 7577 for (j = i + 1; j < num_sack_blks; j++) { 7578 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 7579 sack = sack_blocks[i]; 7580 sack_blocks[i] = sack_blocks[j]; 7581 sack_blocks[j] = sack; 7582 } 7583 } 7584 } 7585 /* 7586 * Now are any of the sack block ends the same (yes some 7587 * implememtations send these)? 7588 */ 7589 again: 7590 if (num_sack_blks > 1) { 7591 for (i = 0; i < num_sack_blks; i++) { 7592 for (j = i + 1; j < num_sack_blks; j++) { 7593 if (sack_blocks[i].end == sack_blocks[j].end) { 7594 /* 7595 * Ok these two have the same end we 7596 * want the smallest end and then 7597 * throw away the larger and start 7598 * again. 7599 */ 7600 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 7601 /* 7602 * The second block covers 7603 * more area use that 7604 */ 7605 sack_blocks[i].start = sack_blocks[j].start; 7606 } 7607 /* 7608 * Now collapse out the dup-sack and 7609 * lower the count 7610 */ 7611 for (k = (j + 1); k < num_sack_blks; k++) { 7612 sack_blocks[j].start = sack_blocks[k].start; 7613 sack_blocks[j].end = sack_blocks[k].end; 7614 j++; 7615 } 7616 num_sack_blks--; 7617 goto again; 7618 } 7619 } 7620 } 7621 } 7622 do_sack_work: 7623 rsm = bbr->r_ctl.rc_sacklast; 7624 for (i = 0; i < num_sack_blks; i++) { 7625 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts); 7626 if (acked) { 7627 bbr->r_wanted_output = 1; 7628 changed += acked; 7629 sack_changed += acked; 7630 } 7631 } 7632 out: 7633 *prev_acked = p_acked; 7634 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 7635 /* 7636 * Ok we have a high probability that we need to go in to 7637 * recovery since we have data sack'd 7638 */ 7639 struct bbr_sendmap *rsm; 7640 7641 rsm = bbr_check_recovery_mode(tp, bbr, cts); 7642 if (rsm) { 7643 /* Enter recovery */ 7644 entered_recovery = 1; 7645 bbr->r_wanted_output = 1; 7646 /* 7647 * When we enter recovery we need to assure we send 7648 * one packet. 7649 */ 7650 if (bbr->r_ctl.rc_resend == NULL) { 7651 bbr->r_ctl.rc_resend = rsm; 7652 } 7653 } 7654 } 7655 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 7656 /* 7657 * See if we need to rack-retransmit anything if so set it 7658 * up as the thing to resend assuming something else is not 7659 * already in that position. 7660 */ 7661 if (bbr->r_ctl.rc_resend == NULL) { 7662 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 7663 } 7664 } 7665 /* 7666 * We return the amount that changed via sack, this is used by the 7667 * ack-received code to augment what was changed between th_ack <-> 7668 * snd_una. 7669 */ 7670 return (sack_changed); 7671 } 7672 7673 static void 7674 bbr_strike_dupack(struct tcp_bbr *bbr) 7675 { 7676 struct bbr_sendmap *rsm; 7677 7678 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 7679 if (rsm && (rsm->r_dupack < 0xff)) { 7680 rsm->r_dupack++; 7681 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) 7682 bbr->r_wanted_output = 1; 7683 } 7684 } 7685 7686 /* 7687 * Return value of 1, we do not need to call bbr_process_data(). 7688 * return value of 0, bbr_process_data can be called. 7689 * For ret_val if its 0 the TCB is locked and valid, if its non-zero 7690 * its unlocked and probably unsafe to touch the TCB. 7691 */ 7692 static int 7693 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 7694 struct tcpcb *tp, struct tcpopt *to, 7695 uint32_t tiwin, int32_t tlen, 7696 int32_t * ofia, int32_t thflags, int32_t * ret_val) 7697 { 7698 int32_t ourfinisacked = 0; 7699 int32_t acked_amount; 7700 uint16_t nsegs; 7701 int32_t acked; 7702 uint32_t lost, sack_changed = 0; 7703 struct mbuf *mfree; 7704 struct tcp_bbr *bbr; 7705 uint32_t prev_acked = 0; 7706 7707 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7708 lost = bbr->r_ctl.rc_lost; 7709 nsegs = max(1, m->m_pkthdr.lro_nsegs); 7710 if (SEQ_GT(th->th_ack, tp->snd_max)) { 7711 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7712 bbr->r_wanted_output = 1; 7713 return (1); 7714 } 7715 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 7716 /* Process the ack */ 7717 if (bbr->rc_in_persist) 7718 tp->t_rxtshift = 0; 7719 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd)) 7720 bbr_strike_dupack(bbr); 7721 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 7722 } 7723 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost)); 7724 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 7725 /* 7726 * Old ack, behind the last one rcv'd or a duplicate ack 7727 * with SACK info. 7728 */ 7729 if (th->th_ack == tp->snd_una) { 7730 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0); 7731 if (bbr->r_state == TCPS_SYN_SENT) { 7732 /* 7733 * Special case on where we sent SYN. When 7734 * the SYN-ACK is processed in syn_sent 7735 * state it bumps the snd_una. This causes 7736 * us to hit here even though we did ack 1 7737 * byte. 7738 * 7739 * Go through the nothing left case so we 7740 * send data. 7741 */ 7742 goto nothing_left; 7743 } 7744 } 7745 return (0); 7746 } 7747 /* 7748 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 7749 * something we sent. 7750 */ 7751 if (tp->t_flags & TF_NEEDSYN) { 7752 /* 7753 * T/TCP: Connection was half-synchronized, and our SYN has 7754 * been ACK'd (so connection is now fully synchronized). Go 7755 * to non-starred state, increment snd_una for ACK of SYN, 7756 * and check if we can do window scaling. 7757 */ 7758 tp->t_flags &= ~TF_NEEDSYN; 7759 tp->snd_una++; 7760 /* Do window scaling? */ 7761 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 7762 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 7763 tp->rcv_scale = tp->request_r_scale; 7764 /* Send window already scaled. */ 7765 } 7766 } 7767 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7768 7769 acked = BYTES_THIS_ACK(tp, th); 7770 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 7771 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 7772 7773 /* 7774 * If we just performed our first retransmit, and the ACK arrives 7775 * within our recovery window, then it was a mistake to do the 7776 * retransmit in the first place. Recover our original cwnd and 7777 * ssthresh, and proceed to transmit where we left off. 7778 */ 7779 if (tp->t_flags & TF_PREVVALID) { 7780 tp->t_flags &= ~TF_PREVVALID; 7781 if (tp->t_rxtshift == 1 && 7782 (int)(ticks - tp->t_badrxtwin) < 0) 7783 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 7784 } 7785 SOCKBUF_LOCK(&so->so_snd); 7786 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 7787 tp->snd_wnd -= acked_amount; 7788 mfree = sbcut_locked(&so->so_snd, acked_amount); 7789 /* NB: sowwakeup_locked() does an implicit unlock. */ 7790 sowwakeup_locked(so); 7791 m_freem(mfree); 7792 if (SEQ_GT(th->th_ack, tp->snd_una)) { 7793 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 7794 } 7795 tp->snd_una = th->th_ack; 7796 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost)); 7797 if (IN_RECOVERY(tp->t_flags)) { 7798 if (SEQ_LT(th->th_ack, tp->snd_recover) && 7799 (SEQ_LT(th->th_ack, tp->snd_max))) { 7800 tcp_bbr_partialack(tp); 7801 } else { 7802 bbr_post_recovery(tp); 7803 } 7804 } 7805 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 7806 tp->snd_recover = tp->snd_una; 7807 } 7808 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 7809 tp->snd_nxt = tp->snd_max; 7810 } 7811 if (tp->snd_una == tp->snd_max) { 7812 /* Nothing left outstanding */ 7813 nothing_left: 7814 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 7815 if (sbavail(&so->so_snd) == 0) 7816 bbr->rc_tp->t_acktime = 0; 7817 if ((sbused(&so->so_snd) == 0) && 7818 (tp->t_flags & TF_SENTFIN)) { 7819 ourfinisacked = 1; 7820 } 7821 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 7822 if (bbr->rc_in_persist == 0) { 7823 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 7824 } 7825 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 7826 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 7827 /* 7828 * We invalidate the last ack here since we 7829 * don't want to transfer forward the time 7830 * for our sum's calculations. 7831 */ 7832 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7833 (sbavail(&so->so_snd) == 0) && 7834 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 7835 /* 7836 * The socket was gone and the peer sent data, time 7837 * to reset him. 7838 */ 7839 *ret_val = 1; 7840 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 7841 /* tcp_close will kill the inp pre-log the Reset */ 7842 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 7843 tp = tcp_close(tp); 7844 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 7845 BBR_STAT_INC(bbr_dropped_af_data); 7846 return (1); 7847 } 7848 /* Set need output so persist might get set */ 7849 bbr->r_wanted_output = 1; 7850 } 7851 if (ofia) 7852 *ofia = ourfinisacked; 7853 return (0); 7854 } 7855 7856 static void 7857 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7858 { 7859 if (bbr->rc_in_persist == 0) { 7860 bbr_timer_cancel(bbr, __LINE__, cts); 7861 bbr->r_ctl.rc_last_delay_val = 0; 7862 tp->t_rxtshift = 0; 7863 bbr->rc_in_persist = 1; 7864 bbr->r_ctl.rc_went_idle_time = cts; 7865 /* We should be capped when rw went to 0 but just in case */ 7866 bbr_log_type_pesist(bbr, cts, 0, line, 1); 7867 /* Time freezes for the state, so do the accounting now */ 7868 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 7869 uint32_t time_in; 7870 7871 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 7872 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7873 int32_t idx; 7874 7875 idx = bbr_state_val(bbr); 7876 counter_u64_add(bbr_state_time[(idx + 5)], time_in); 7877 } else { 7878 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 7879 } 7880 } 7881 bbr->r_ctl.rc_bbr_state_time = cts; 7882 } 7883 } 7884 7885 static void 7886 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time) 7887 { 7888 /* 7889 * Note that if idle time does not exceed our 7890 * threshold, we do nothing continuing the state 7891 * transitions we were last walking through. 7892 */ 7893 if (idle_time >= bbr_idle_restart_threshold) { 7894 if (bbr->rc_use_idle_restart) { 7895 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT; 7896 /* 7897 * Set our target using BBR_UNIT, so 7898 * we increase at a dramatic rate but 7899 * we stop when we get the pipe 7900 * full again for our current b/w estimate. 7901 */ 7902 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 7903 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 7904 bbr_set_state_target(bbr, __LINE__); 7905 /* Now setup our gains to ramp up */ 7906 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 7907 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 7908 bbr_log_type_statechange(bbr, cts, __LINE__); 7909 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7910 bbr_substate_change(bbr, cts, __LINE__, 1); 7911 } 7912 } 7913 } 7914 7915 static void 7916 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7917 { 7918 uint32_t idle_time; 7919 7920 if (bbr->rc_in_persist == 0) 7921 return; 7922 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time); 7923 bbr->rc_in_persist = 0; 7924 bbr->rc_hit_state_1 = 0; 7925 bbr->r_ctl.rc_del_time = cts; 7926 /* 7927 * We invalidate the last ack here since we 7928 * don't want to transfer forward the time 7929 * for our sum's calculations. 7930 */ 7931 if (tcp_in_hpts(bbr->rc_inp)) { 7932 tcp_hpts_remove(bbr->rc_inp); 7933 bbr->rc_timer_first = 0; 7934 bbr->r_ctl.rc_hpts_flags = 0; 7935 bbr->r_ctl.rc_last_delay_val = 0; 7936 bbr->r_ctl.rc_hptsi_agg_delay = 0; 7937 bbr->r_agg_early_set = 0; 7938 bbr->r_ctl.rc_agg_early = 0; 7939 } 7940 bbr_log_type_pesist(bbr, cts, idle_time, line, 0); 7941 if (idle_time >= bbr_rtt_probe_time) { 7942 /* 7943 * This qualifies as a RTT_PROBE session since we drop the 7944 * data outstanding to nothing and waited more than 7945 * bbr_rtt_probe_time. 7946 */ 7947 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0); 7948 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts; 7949 } 7950 tp->t_rxtshift = 0; 7951 /* 7952 * If in probeBW and we have persisted more than an RTT lets do 7953 * special handling. 7954 */ 7955 /* Force a time based epoch */ 7956 bbr_set_epoch(bbr, cts, __LINE__); 7957 /* 7958 * Setup the lost so we don't count anything against the guy 7959 * we have been stuck with during persists. 7960 */ 7961 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 7962 /* Time un-freezes for the state */ 7963 bbr->r_ctl.rc_bbr_state_time = cts; 7964 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) || 7965 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) { 7966 /* 7967 * If we are going back to probe-bw 7968 * or probe_rtt, we may need to possibly 7969 * do a fast restart. 7970 */ 7971 bbr_restart_after_idle(bbr, cts, idle_time); 7972 } 7973 } 7974 7975 static void 7976 bbr_collapsed_window(struct tcp_bbr *bbr) 7977 { 7978 /* 7979 * Now we must walk the 7980 * send map and divide the 7981 * ones left stranded. These 7982 * guys can't cause us to abort 7983 * the connection and are really 7984 * "unsent". However if a buggy 7985 * client actually did keep some 7986 * of the data i.e. collapsed the win 7987 * and refused to ack and then opened 7988 * the win and acked that data. We would 7989 * get into an ack war, the simplier 7990 * method then of just pretending we 7991 * did not send those segments something 7992 * won't work. 7993 */ 7994 struct bbr_sendmap *rsm, *nrsm; 7995 tcp_seq max_seq; 7996 uint32_t maxseg; 7997 int can_split = 0; 7998 int fnd = 0; 7999 8000 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 8001 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd; 8002 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0); 8003 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 8004 /* Find the first seq past or at maxseq */ 8005 if (rsm->r_flags & BBR_RWND_COLLAPSED) 8006 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8007 if (SEQ_GEQ(max_seq, rsm->r_start) && 8008 SEQ_GEQ(rsm->r_end, max_seq)) { 8009 fnd = 1; 8010 break; 8011 } 8012 } 8013 bbr->rc_has_collapsed = 0; 8014 if (!fnd) { 8015 /* Nothing to do strange */ 8016 return; 8017 } 8018 /* 8019 * Now can we split? 8020 * 8021 * We don't want to split if splitting 8022 * would generate too many small segments 8023 * less we let an attacker fragment our 8024 * send_map and leave us out of memory. 8025 */ 8026 if ((max_seq != rsm->r_start) && 8027 (max_seq != rsm->r_end)){ 8028 /* can we split? */ 8029 int res1, res2; 8030 8031 res1 = max_seq - rsm->r_start; 8032 res2 = rsm->r_end - max_seq; 8033 if ((res1 >= (maxseg/8)) && 8034 (res2 >= (maxseg/8))) { 8035 /* No small pieces here */ 8036 can_split = 1; 8037 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) { 8038 /* We are under the limit */ 8039 can_split = 1; 8040 } 8041 } 8042 /* Ok do we need to split this rsm? */ 8043 if (max_seq == rsm->r_start) { 8044 /* It's this guy no split required */ 8045 nrsm = rsm; 8046 } else if (max_seq == rsm->r_end) { 8047 /* It's the next one no split required. */ 8048 nrsm = TAILQ_NEXT(rsm, r_next); 8049 if (nrsm == NULL) { 8050 /* Huh? */ 8051 return; 8052 } 8053 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) { 8054 /* yep we need to split it */ 8055 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 8056 if (nrsm == NULL) { 8057 /* failed XXXrrs what can we do mark the whole? */ 8058 nrsm = rsm; 8059 goto no_split; 8060 } 8061 /* Clone it */ 8062 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0); 8063 bbr_clone_rsm(bbr, nrsm, rsm, max_seq); 8064 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 8065 if (rsm->r_in_tmap) { 8066 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8067 nrsm->r_in_tmap = 1; 8068 } 8069 } else { 8070 /* 8071 * Split not allowed just start here just 8072 * use this guy. 8073 */ 8074 nrsm = rsm; 8075 } 8076 no_split: 8077 BBR_STAT_INC(bbr_collapsed_win); 8078 /* reuse fnd as a count */ 8079 fnd = 0; 8080 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) { 8081 nrsm->r_flags |= BBR_RWND_COLLAPSED; 8082 fnd++; 8083 bbr->rc_has_collapsed = 1; 8084 } 8085 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd); 8086 } 8087 8088 static void 8089 bbr_un_collapse_window(struct tcp_bbr *bbr) 8090 { 8091 struct bbr_sendmap *rsm; 8092 int cleared = 0; 8093 8094 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 8095 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 8096 /* Clear the flag */ 8097 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8098 cleared++; 8099 } else 8100 break; 8101 } 8102 bbr_log_type_rwnd_collapse(bbr, 8103 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared); 8104 bbr->rc_has_collapsed = 0; 8105 } 8106 8107 /* 8108 * Return value of 1, the TCB is unlocked and most 8109 * likely gone, return value of 0, the TCB is still 8110 * locked. 8111 */ 8112 static int 8113 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 8114 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 8115 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 8116 { 8117 /* 8118 * Update window information. Don't look at window if no ACK: TAC's 8119 * send garbage on first SYN. 8120 */ 8121 uint16_t nsegs; 8122 int32_t tfo_syn; 8123 struct tcp_bbr *bbr; 8124 8125 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8126 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8127 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8128 if ((thflags & TH_ACK) && 8129 (SEQ_LT(tp->snd_wl1, th->th_seq) || 8130 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 8131 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 8132 /* keep track of pure window updates */ 8133 if (tlen == 0 && 8134 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 8135 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 8136 tp->snd_wnd = tiwin; 8137 tp->snd_wl1 = th->th_seq; 8138 tp->snd_wl2 = th->th_ack; 8139 if (tp->snd_wnd > tp->max_sndwnd) 8140 tp->max_sndwnd = tp->snd_wnd; 8141 bbr->r_wanted_output = 1; 8142 } else if (thflags & TH_ACK) { 8143 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 8144 tp->snd_wnd = tiwin; 8145 tp->snd_wl1 = th->th_seq; 8146 tp->snd_wl2 = th->th_ack; 8147 } 8148 } 8149 if (tp->snd_wnd < ctf_outstanding(tp)) 8150 /* The peer collapsed its window on us */ 8151 bbr_collapsed_window(bbr); 8152 else if (bbr->rc_has_collapsed) 8153 bbr_un_collapse_window(bbr); 8154 /* Was persist timer active and now we have window space? */ 8155 if ((bbr->rc_in_persist != 0) && 8156 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8157 bbr_minseg(bbr)))) { 8158 /* 8159 * Make the rate persist at end of persist mode if idle long 8160 * enough 8161 */ 8162 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8163 8164 /* Make sure we output to start the timer */ 8165 bbr->r_wanted_output = 1; 8166 } 8167 /* Do we need to enter persist? */ 8168 if ((bbr->rc_in_persist == 0) && 8169 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8170 TCPS_HAVEESTABLISHED(tp->t_state) && 8171 (tp->snd_max == tp->snd_una) && 8172 sbavail(&so->so_snd) && 8173 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8174 /* No send window.. we must enter persist */ 8175 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8176 } 8177 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 8178 m_freem(m); 8179 return (0); 8180 } 8181 /* 8182 * We don't support urgent data but 8183 * drag along the up just to make sure 8184 * if there is a stack switch no one 8185 * is surprised. 8186 */ 8187 tp->rcv_up = tp->rcv_nxt; 8188 8189 /* 8190 * Process the segment text, merging it into the TCP sequencing 8191 * queue, and arranging for acknowledgment of receipt if necessary. 8192 * This process logically involves adjusting tp->rcv_wnd as data is 8193 * presented to the user (this happens in tcp_usrreq.c, case 8194 * PRU_RCVD). If a FIN has already been received on this connection 8195 * then we just ignore the text. 8196 */ 8197 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 8198 IS_FASTOPEN(tp->t_flags)); 8199 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 8200 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8201 tcp_seq save_start = th->th_seq; 8202 tcp_seq save_rnxt = tp->rcv_nxt; 8203 int save_tlen = tlen; 8204 8205 m_adj(m, drop_hdrlen); /* delayed header drop */ 8206 /* 8207 * Insert segment which includes th into TCP reassembly 8208 * queue with control block tp. Set thflags to whether 8209 * reassembly now includes a segment with FIN. This handles 8210 * the common case inline (segment is the next to be 8211 * received on an established connection, and the queue is 8212 * empty), avoiding linkage into and removal from the queue 8213 * and repetition of various conversions. Set DELACK for 8214 * segments received in order, but ack immediately when 8215 * segments are out of order (so fast retransmit can work). 8216 */ 8217 if (th->th_seq == tp->rcv_nxt && 8218 SEGQ_EMPTY(tp) && 8219 (TCPS_HAVEESTABLISHED(tp->t_state) || 8220 tfo_syn)) { 8221 #ifdef NETFLIX_SB_LIMITS 8222 u_int mcnt, appended; 8223 8224 if (so->so_rcv.sb_shlim) { 8225 mcnt = m_memcnt(m); 8226 appended = 0; 8227 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8228 CFO_NOSLEEP, NULL) == false) { 8229 counter_u64_add(tcp_sb_shlim_fails, 1); 8230 m_freem(m); 8231 return (0); 8232 } 8233 } 8234 8235 #endif 8236 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) { 8237 bbr->bbr_segs_rcvd += max(1, nsegs); 8238 tp->t_flags |= TF_DELACK; 8239 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8240 } else { 8241 bbr->r_wanted_output = 1; 8242 tp->t_flags |= TF_ACKNOW; 8243 } 8244 tp->rcv_nxt += tlen; 8245 if (tlen && 8246 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8247 (tp->t_fbyte_in == 0)) { 8248 tp->t_fbyte_in = ticks; 8249 if (tp->t_fbyte_in == 0) 8250 tp->t_fbyte_in = 1; 8251 if (tp->t_fbyte_out && tp->t_fbyte_in) 8252 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8253 } 8254 thflags = tcp_get_flags(th) & TH_FIN; 8255 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8256 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8257 SOCKBUF_LOCK(&so->so_rcv); 8258 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 8259 m_freem(m); 8260 else 8261 #ifdef NETFLIX_SB_LIMITS 8262 appended = 8263 #endif 8264 sbappendstream_locked(&so->so_rcv, m, 0); 8265 /* NB: sorwakeup_locked() does an implicit unlock. */ 8266 sorwakeup_locked(so); 8267 #ifdef NETFLIX_SB_LIMITS 8268 if (so->so_rcv.sb_shlim && appended != mcnt) 8269 counter_fo_release(so->so_rcv.sb_shlim, 8270 mcnt - appended); 8271 #endif 8272 8273 } else { 8274 /* 8275 * XXX: Due to the header drop above "th" is 8276 * theoretically invalid by now. Fortunately 8277 * m_adj() doesn't actually frees any mbufs when 8278 * trimming from the head. 8279 */ 8280 tcp_seq temp = save_start; 8281 8282 thflags = tcp_reass(tp, th, &temp, &tlen, m); 8283 tp->t_flags |= TF_ACKNOW; 8284 if (tp->t_flags & TF_WAKESOR) { 8285 tp->t_flags &= ~TF_WAKESOR; 8286 /* NB: sorwakeup_locked() does an implicit unlock. */ 8287 sorwakeup_locked(so); 8288 } 8289 } 8290 if ((tp->t_flags & TF_SACK_PERMIT) && 8291 (save_tlen > 0) && 8292 TCPS_HAVEESTABLISHED(tp->t_state)) { 8293 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 8294 /* 8295 * DSACK actually handled in the fastpath 8296 * above. 8297 */ 8298 tcp_update_sack_list(tp, save_start, 8299 save_start + save_tlen); 8300 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 8301 if ((tp->rcv_numsacks >= 1) && 8302 (tp->sackblks[0].end == save_start)) { 8303 /* 8304 * Partial overlap, recorded at todrop 8305 * above. 8306 */ 8307 tcp_update_sack_list(tp, 8308 tp->sackblks[0].start, 8309 tp->sackblks[0].end); 8310 } else { 8311 tcp_update_dsack_list(tp, save_start, 8312 save_start + save_tlen); 8313 } 8314 } else if (tlen >= save_tlen) { 8315 /* Update of sackblks. */ 8316 tcp_update_dsack_list(tp, save_start, 8317 save_start + save_tlen); 8318 } else if (tlen > 0) { 8319 tcp_update_dsack_list(tp, save_start, 8320 save_start + tlen); 8321 } 8322 } 8323 } else { 8324 m_freem(m); 8325 thflags &= ~TH_FIN; 8326 } 8327 8328 /* 8329 * If FIN is received ACK the FIN and let the user know that the 8330 * connection is closing. 8331 */ 8332 if (thflags & TH_FIN) { 8333 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8334 /* The socket upcall is handled by socantrcvmore. */ 8335 socantrcvmore(so); 8336 /* 8337 * If connection is half-synchronized (ie NEEDSYN 8338 * flag on) then delay ACK, so it may be piggybacked 8339 * when SYN is sent. Otherwise, since we received a 8340 * FIN then no more input can be expected, send ACK 8341 * now. 8342 */ 8343 if (tp->t_flags & TF_NEEDSYN) { 8344 tp->t_flags |= TF_DELACK; 8345 bbr_timer_cancel(bbr, 8346 __LINE__, bbr->r_ctl.rc_rcvtime); 8347 } else { 8348 tp->t_flags |= TF_ACKNOW; 8349 } 8350 tp->rcv_nxt++; 8351 } 8352 switch (tp->t_state) { 8353 /* 8354 * In SYN_RECEIVED and ESTABLISHED STATES enter the 8355 * CLOSE_WAIT state. 8356 */ 8357 case TCPS_SYN_RECEIVED: 8358 tp->t_starttime = ticks; 8359 /* FALLTHROUGH */ 8360 case TCPS_ESTABLISHED: 8361 tcp_state_change(tp, TCPS_CLOSE_WAIT); 8362 break; 8363 8364 /* 8365 * If still in FIN_WAIT_1 STATE FIN has not been 8366 * acked so enter the CLOSING state. 8367 */ 8368 case TCPS_FIN_WAIT_1: 8369 tcp_state_change(tp, TCPS_CLOSING); 8370 break; 8371 8372 /* 8373 * In FIN_WAIT_2 state enter the TIME_WAIT state, 8374 * starting the time-wait timer, turning off the 8375 * other standard timers. 8376 */ 8377 case TCPS_FIN_WAIT_2: 8378 bbr->rc_timer_first = 1; 8379 bbr_timer_cancel(bbr, 8380 __LINE__, bbr->r_ctl.rc_rcvtime); 8381 tcp_twstart(tp); 8382 return (1); 8383 } 8384 } 8385 /* 8386 * Return any desired output. 8387 */ 8388 if ((tp->t_flags & TF_ACKNOW) || 8389 (sbavail(&so->so_snd) > ctf_outstanding(tp))) { 8390 bbr->r_wanted_output = 1; 8391 } 8392 return (0); 8393 } 8394 8395 /* 8396 * Here nothing is really faster, its just that we 8397 * have broken out the fast-data path also just like 8398 * the fast-ack. Return 1 if we processed the packet 8399 * return 0 if you need to take the "slow-path". 8400 */ 8401 static int 8402 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 8403 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8404 uint32_t tiwin, int32_t nxt_pkt) 8405 { 8406 uint16_t nsegs; 8407 int32_t newsize = 0; /* automatic sockbuf scaling */ 8408 struct tcp_bbr *bbr; 8409 #ifdef NETFLIX_SB_LIMITS 8410 u_int mcnt, appended; 8411 #endif 8412 8413 /* On the hpts and we would have called output */ 8414 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8415 8416 /* 8417 * If last ACK falls within this segment's sequence numbers, record 8418 * the timestamp. NOTE that the test is modified according to the 8419 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8420 */ 8421 if (bbr->r_ctl.rc_resend != NULL) { 8422 return (0); 8423 } 8424 if (tiwin && tiwin != tp->snd_wnd) { 8425 return (0); 8426 } 8427 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 8428 return (0); 8429 } 8430 if (__predict_false((to->to_flags & TOF_TS) && 8431 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 8432 return (0); 8433 } 8434 if (__predict_false((th->th_ack != tp->snd_una))) { 8435 return (0); 8436 } 8437 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 8438 return (0); 8439 } 8440 if ((to->to_flags & TOF_TS) != 0 && 8441 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8442 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 8443 tp->ts_recent = to->to_tsval; 8444 } 8445 /* 8446 * This is a pure, in-sequence data packet with nothing on the 8447 * reassembly queue and we have enough buffer space to take it. 8448 */ 8449 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8450 8451 #ifdef NETFLIX_SB_LIMITS 8452 if (so->so_rcv.sb_shlim) { 8453 mcnt = m_memcnt(m); 8454 appended = 0; 8455 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8456 CFO_NOSLEEP, NULL) == false) { 8457 counter_u64_add(tcp_sb_shlim_fails, 1); 8458 m_freem(m); 8459 return (1); 8460 } 8461 } 8462 #endif 8463 /* Clean receiver SACK report if present */ 8464 if (tp->rcv_numsacks) 8465 tcp_clean_sackreport(tp); 8466 KMOD_TCPSTAT_INC(tcps_preddat); 8467 tp->rcv_nxt += tlen; 8468 if (tlen && 8469 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8470 (tp->t_fbyte_in == 0)) { 8471 tp->t_fbyte_in = ticks; 8472 if (tp->t_fbyte_in == 0) 8473 tp->t_fbyte_in = 1; 8474 if (tp->t_fbyte_out && tp->t_fbyte_in) 8475 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8476 } 8477 /* 8478 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 8479 */ 8480 tp->snd_wl1 = th->th_seq; 8481 /* 8482 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 8483 */ 8484 tp->rcv_up = tp->rcv_nxt; 8485 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8486 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8487 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 8488 8489 /* Add data to socket buffer. */ 8490 SOCKBUF_LOCK(&so->so_rcv); 8491 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8492 m_freem(m); 8493 } else { 8494 /* 8495 * Set new socket buffer size. Give up when limit is 8496 * reached. 8497 */ 8498 if (newsize) 8499 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 8500 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 8501 m_adj(m, drop_hdrlen); /* delayed header drop */ 8502 8503 #ifdef NETFLIX_SB_LIMITS 8504 appended = 8505 #endif 8506 sbappendstream_locked(&so->so_rcv, m, 0); 8507 ctf_calc_rwin(so, tp); 8508 } 8509 /* NB: sorwakeup_locked() does an implicit unlock. */ 8510 sorwakeup_locked(so); 8511 #ifdef NETFLIX_SB_LIMITS 8512 if (so->so_rcv.sb_shlim && mcnt != appended) 8513 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 8514 #endif 8515 if (DELAY_ACK(tp, bbr, nsegs)) { 8516 bbr->bbr_segs_rcvd += max(1, nsegs); 8517 tp->t_flags |= TF_DELACK; 8518 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8519 } else { 8520 bbr->r_wanted_output = 1; 8521 tp->t_flags |= TF_ACKNOW; 8522 } 8523 return (1); 8524 } 8525 8526 /* 8527 * This subfunction is used to try to highly optimize the 8528 * fast path. We again allow window updates that are 8529 * in sequence to remain in the fast-path. We also add 8530 * in the __predict's to attempt to help the compiler. 8531 * Note that if we return a 0, then we can *not* process 8532 * it and the caller should push the packet into the 8533 * slow-path. If we return 1, then all is well and 8534 * the packet is fully processed. 8535 */ 8536 static int 8537 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 8538 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8539 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 8540 { 8541 int32_t acked; 8542 uint16_t nsegs; 8543 uint32_t sack_changed; 8544 uint32_t prev_acked = 0; 8545 struct tcp_bbr *bbr; 8546 8547 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 8548 /* Old ack, behind (or duplicate to) the last one rcv'd */ 8549 return (0); 8550 } 8551 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 8552 /* Above what we have sent? */ 8553 return (0); 8554 } 8555 if (__predict_false(tiwin == 0)) { 8556 /* zero window */ 8557 return (0); 8558 } 8559 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 8560 /* We need a SYN or a FIN, unlikely.. */ 8561 return (0); 8562 } 8563 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 8564 /* Timestamp is behind .. old ack with seq wrap? */ 8565 return (0); 8566 } 8567 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 8568 /* Still recovering */ 8569 return (0); 8570 } 8571 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8572 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) { 8573 /* We are retransmitting */ 8574 return (0); 8575 } 8576 if (__predict_false(bbr->rc_in_persist != 0)) { 8577 /* In persist mode */ 8578 return (0); 8579 } 8580 if (bbr->r_ctl.rc_sacked) { 8581 /* We have sack holes on our scoreboard */ 8582 return (0); 8583 } 8584 /* Ok if we reach here, we can process a fast-ack */ 8585 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8586 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 8587 /* 8588 * We never detect loss in fast ack [we can't 8589 * have a sack and can't be in recovery so 8590 * we always pass 0 (nothing detected)]. 8591 */ 8592 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0); 8593 /* Did the window get updated? */ 8594 if (tiwin != tp->snd_wnd) { 8595 tp->snd_wnd = tiwin; 8596 tp->snd_wl1 = th->th_seq; 8597 if (tp->snd_wnd > tp->max_sndwnd) 8598 tp->max_sndwnd = tp->snd_wnd; 8599 } 8600 /* Do we need to exit persists? */ 8601 if ((bbr->rc_in_persist != 0) && 8602 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8603 bbr_minseg(bbr)))) { 8604 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8605 bbr->r_wanted_output = 1; 8606 } 8607 /* Do we need to enter persists? */ 8608 if ((bbr->rc_in_persist == 0) && 8609 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8610 TCPS_HAVEESTABLISHED(tp->t_state) && 8611 (tp->snd_max == tp->snd_una) && 8612 sbavail(&so->so_snd) && 8613 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8614 /* No send window.. we must enter persist */ 8615 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8616 } 8617 /* 8618 * If last ACK falls within this segment's sequence numbers, record 8619 * the timestamp. NOTE that the test is modified according to the 8620 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8621 */ 8622 if ((to->to_flags & TOF_TS) != 0 && 8623 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8624 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime; 8625 tp->ts_recent = to->to_tsval; 8626 } 8627 /* 8628 * This is a pure ack for outstanding data. 8629 */ 8630 KMOD_TCPSTAT_INC(tcps_predack); 8631 8632 /* 8633 * "bad retransmit" recovery. 8634 */ 8635 if (tp->t_flags & TF_PREVVALID) { 8636 tp->t_flags &= ~TF_PREVVALID; 8637 if (tp->t_rxtshift == 1 && 8638 (int)(ticks - tp->t_badrxtwin) < 0) 8639 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 8640 } 8641 /* 8642 * Recalculate the transmit timer / rtt. 8643 * 8644 * Some boxes send broken timestamp replies during the SYN+ACK 8645 * phase, ignore timestamps of 0 or we could calculate a huge RTT 8646 * and blow up the retransmit timer. 8647 */ 8648 acked = BYTES_THIS_ACK(tp, th); 8649 8650 #ifdef TCP_HHOOK 8651 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 8652 hhook_run_tcp_est_in(tp, th, to); 8653 #endif 8654 8655 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 8656 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 8657 sbdrop(&so->so_snd, acked); 8658 8659 if (SEQ_GT(th->th_ack, tp->snd_una)) 8660 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 8661 tp->snd_una = th->th_ack; 8662 if (tp->snd_wnd < ctf_outstanding(tp)) 8663 /* The peer collapsed its window on us */ 8664 bbr_collapsed_window(bbr); 8665 else if (bbr->rc_has_collapsed) 8666 bbr_un_collapse_window(bbr); 8667 8668 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 8669 tp->snd_recover = tp->snd_una; 8670 } 8671 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0); 8672 /* 8673 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 8674 */ 8675 tp->snd_wl2 = th->th_ack; 8676 m_freem(m); 8677 /* 8678 * If all outstanding data are acked, stop retransmit timer, 8679 * otherwise restart timer using current (possibly backed-off) 8680 * value. If process is waiting for space, wakeup/selwakeup/signal. 8681 * If data are ready to send, let tcp_output decide between more 8682 * output or persist. 8683 * Wake up the socket if we have room to write more. 8684 */ 8685 sowwakeup(so); 8686 if (tp->snd_una == tp->snd_max) { 8687 /* Nothing left outstanding */ 8688 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 8689 if (sbavail(&so->so_snd) == 0) 8690 bbr->rc_tp->t_acktime = 0; 8691 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8692 if (bbr->rc_in_persist == 0) { 8693 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 8694 } 8695 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 8696 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 8697 /* 8698 * We invalidate the last ack here since we 8699 * don't want to transfer forward the time 8700 * for our sum's calculations. 8701 */ 8702 bbr->r_wanted_output = 1; 8703 } 8704 if (sbavail(&so->so_snd)) { 8705 bbr->r_wanted_output = 1; 8706 } 8707 return (1); 8708 } 8709 8710 /* 8711 * Return value of 1, the TCB is unlocked and most 8712 * likely gone, return value of 0, the TCB is still 8713 * locked. 8714 */ 8715 static int 8716 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 8717 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8718 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8719 { 8720 int32_t todrop; 8721 int32_t ourfinisacked = 0; 8722 struct tcp_bbr *bbr; 8723 int32_t ret_val = 0; 8724 8725 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8726 8727 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8728 ctf_calc_rwin(so, tp); 8729 /* 8730 * If the state is SYN_SENT: if seg contains an ACK, but not for our 8731 * SYN, drop the input. if seg contains a RST, then drop the 8732 * connection. if seg does not contain SYN, then drop it. Otherwise 8733 * this is an acceptable SYN segment initialize tp->rcv_nxt and 8734 * tp->irs if seg contains ack then advance tp->snd_una. BRR does 8735 * not support ECN so we will not say we are capable. if SYN has 8736 * been acked change to ESTABLISHED else SYN_RCVD state arrange for 8737 * segment to be acked (eventually) continue processing rest of 8738 * data/controls, beginning with URG 8739 */ 8740 if ((thflags & TH_ACK) && 8741 (SEQ_LEQ(th->th_ack, tp->iss) || 8742 SEQ_GT(th->th_ack, tp->snd_max))) { 8743 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8744 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8745 return (1); 8746 } 8747 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 8748 TCP_PROBE5(connect__refused, NULL, tp, 8749 mtod(m, const char *), tp, th); 8750 tp = tcp_drop(tp, ECONNREFUSED); 8751 ctf_do_drop(m, tp); 8752 return (1); 8753 } 8754 if (thflags & TH_RST) { 8755 ctf_do_drop(m, tp); 8756 return (1); 8757 } 8758 if (!(thflags & TH_SYN)) { 8759 ctf_do_drop(m, tp); 8760 return (1); 8761 } 8762 tp->irs = th->th_seq; 8763 tcp_rcvseqinit(tp); 8764 if (thflags & TH_ACK) { 8765 int tfo_partial = 0; 8766 8767 KMOD_TCPSTAT_INC(tcps_connects); 8768 soisconnected(so); 8769 #ifdef MAC 8770 mac_socketpeer_set_from_mbuf(m, so); 8771 #endif 8772 /* Do window scaling on this connection? */ 8773 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 8774 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 8775 tp->rcv_scale = tp->request_r_scale; 8776 } 8777 tp->rcv_adv += min(tp->rcv_wnd, 8778 TCP_MAXWIN << tp->rcv_scale); 8779 /* 8780 * If not all the data that was sent in the TFO SYN 8781 * has been acked, resend the remainder right away. 8782 */ 8783 if (IS_FASTOPEN(tp->t_flags) && 8784 (tp->snd_una != tp->snd_max)) { 8785 tp->snd_nxt = th->th_ack; 8786 tfo_partial = 1; 8787 } 8788 /* 8789 * If there's data, delay ACK; if there's also a FIN ACKNOW 8790 * will be turned on later. 8791 */ 8792 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) { 8793 bbr->bbr_segs_rcvd += 1; 8794 tp->t_flags |= TF_DELACK; 8795 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8796 } else { 8797 bbr->r_wanted_output = 1; 8798 tp->t_flags |= TF_ACKNOW; 8799 } 8800 if (SEQ_GT(th->th_ack, tp->iss)) { 8801 /* 8802 * The SYN is acked 8803 * handle it specially. 8804 */ 8805 bbr_log_syn(tp, to); 8806 } 8807 if (SEQ_GT(th->th_ack, tp->snd_una)) { 8808 /* 8809 * We advance snd_una for the 8810 * fast open case. If th_ack is 8811 * acknowledging data beyond 8812 * snd_una we can't just call 8813 * ack-processing since the 8814 * data stream in our send-map 8815 * will start at snd_una + 1 (one 8816 * beyond the SYN). If its just 8817 * equal we don't need to do that 8818 * and there is no send_map. 8819 */ 8820 tp->snd_una++; 8821 } 8822 /* 8823 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 8824 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 8825 */ 8826 tp->t_starttime = ticks; 8827 if (tp->t_flags & TF_NEEDFIN) { 8828 tcp_state_change(tp, TCPS_FIN_WAIT_1); 8829 tp->t_flags &= ~TF_NEEDFIN; 8830 thflags &= ~TH_SYN; 8831 } else { 8832 tcp_state_change(tp, TCPS_ESTABLISHED); 8833 TCP_PROBE5(connect__established, NULL, tp, 8834 mtod(m, const char *), tp, th); 8835 cc_conn_init(tp); 8836 } 8837 } else { 8838 /* 8839 * Received initial SYN in SYN-SENT[*] state => simultaneous 8840 * open. If segment contains CC option and there is a 8841 * cached CC, apply TAO test. If it succeeds, connection is * 8842 * half-synchronized. Otherwise, do 3-way handshake: 8843 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 8844 * there was no CC option, clear cached CC value. 8845 */ 8846 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 8847 tcp_state_change(tp, TCPS_SYN_RECEIVED); 8848 } 8849 /* 8850 * Advance th->th_seq to correspond to first data byte. If data, 8851 * trim to stay within window, dropping FIN if necessary. 8852 */ 8853 th->th_seq++; 8854 if (tlen > tp->rcv_wnd) { 8855 todrop = tlen - tp->rcv_wnd; 8856 m_adj(m, -todrop); 8857 tlen = tp->rcv_wnd; 8858 thflags &= ~TH_FIN; 8859 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 8860 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 8861 } 8862 tp->snd_wl1 = th->th_seq - 1; 8863 tp->rcv_up = th->th_seq; 8864 /* 8865 * Client side of transaction: already sent SYN and data. If the 8866 * remote host used T/TCP to validate the SYN, our data will be 8867 * ACK'd; if so, enter normal data segment processing in the middle 8868 * of step 5, ack processing. Otherwise, goto step 6. 8869 */ 8870 if (thflags & TH_ACK) { 8871 if ((to->to_flags & TOF_TS) != 0) { 8872 uint32_t t, rtt; 8873 8874 t = tcp_tv_to_mssectick(&bbr->rc_tv); 8875 if (TSTMP_GEQ(t, to->to_tsecr)) { 8876 rtt = t - to->to_tsecr; 8877 if (rtt == 0) { 8878 rtt = 1; 8879 } 8880 rtt *= MS_IN_USEC; 8881 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 8882 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, 8883 rtt, bbr->r_ctl.rc_rcvtime); 8884 } 8885 } 8886 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 8887 return (ret_val); 8888 /* We may have changed to FIN_WAIT_1 above */ 8889 if (tp->t_state == TCPS_FIN_WAIT_1) { 8890 /* 8891 * In FIN_WAIT_1 STATE in addition to the processing 8892 * for the ESTABLISHED state if our FIN is now 8893 * acknowledged then enter FIN_WAIT_2. 8894 */ 8895 if (ourfinisacked) { 8896 /* 8897 * If we can't receive any more data, then 8898 * closing user can proceed. Starting the 8899 * timer is contrary to the specification, 8900 * but if we don't get a FIN we'll hang 8901 * forever. 8902 * 8903 * XXXjl: we should release the tp also, and 8904 * use a compressed state. 8905 */ 8906 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8907 soisdisconnected(so); 8908 tcp_timer_activate(tp, TT_2MSL, 8909 (tcp_fast_finwait2_recycle ? 8910 tcp_finwait2_timeout : 8911 TP_MAXIDLE(tp))); 8912 } 8913 tcp_state_change(tp, TCPS_FIN_WAIT_2); 8914 } 8915 } 8916 } 8917 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 8918 tiwin, thflags, nxt_pkt)); 8919 } 8920 8921 /* 8922 * Return value of 1, the TCB is unlocked and most 8923 * likely gone, return value of 0, the TCB is still 8924 * locked. 8925 */ 8926 static int 8927 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 8928 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8929 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8930 { 8931 int32_t ourfinisacked = 0; 8932 int32_t ret_val; 8933 struct tcp_bbr *bbr; 8934 8935 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8936 8937 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8938 ctf_calc_rwin(so, tp); 8939 if ((thflags & TH_ACK) && 8940 (SEQ_LEQ(th->th_ack, tp->snd_una) || 8941 SEQ_GT(th->th_ack, tp->snd_max))) { 8942 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8943 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8944 return (1); 8945 } 8946 if (IS_FASTOPEN(tp->t_flags)) { 8947 /* 8948 * When a TFO connection is in SYN_RECEIVED, the only valid 8949 * packets are the initial SYN, a retransmit/copy of the 8950 * initial SYN (possibly with a subset of the original 8951 * data), a valid ACK, a FIN, or a RST. 8952 */ 8953 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 8954 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8955 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8956 return (1); 8957 } else if (thflags & TH_SYN) { 8958 /* non-initial SYN is ignored */ 8959 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 8960 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 8961 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 8962 ctf_do_drop(m, NULL); 8963 return (0); 8964 } 8965 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 8966 ctf_do_drop(m, NULL); 8967 return (0); 8968 } 8969 } 8970 if ((thflags & TH_RST) || 8971 (tp->t_fin_is_rst && (thflags & TH_FIN))) 8972 return (ctf_process_rst(m, th, so, tp)); 8973 /* 8974 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 8975 * it's less than ts_recent, drop it. 8976 */ 8977 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 8978 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 8979 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 8980 return (ret_val); 8981 } 8982 /* 8983 * In the SYN-RECEIVED state, validate that the packet belongs to 8984 * this connection before trimming the data to fit the receive 8985 * window. Check the sequence number versus IRS since we know the 8986 * sequence numbers haven't wrapped. This is a partial fix for the 8987 * "LAND" DoS attack. 8988 */ 8989 if (SEQ_LT(th->th_seq, tp->irs)) { 8990 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8991 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8992 return (1); 8993 } 8994 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 8995 return (ret_val); 8996 } 8997 /* 8998 * If last ACK falls within this segment's sequence numbers, record 8999 * its timestamp. NOTE: 1) That the test incorporates suggestions 9000 * from the latest proposal of the tcplw@cray.com list (Braden 9001 * 1993/04/26). 2) That updating only on newer timestamps interferes 9002 * with our earlier PAWS tests, so this check should be solely 9003 * predicated on the sequence space of this segment. 3) That we 9004 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9005 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9006 * SEG.Len, This modified check allows us to overcome RFC1323's 9007 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9008 * p.869. In such cases, we can still calculate the RTT correctly 9009 * when RCV.NXT == Last.ACK.Sent. 9010 */ 9011 if ((to->to_flags & TOF_TS) != 0 && 9012 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9013 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9014 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9015 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9016 tp->ts_recent = to->to_tsval; 9017 } 9018 tp->snd_wnd = tiwin; 9019 /* 9020 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9021 * is on (half-synchronized state), then queue data for later 9022 * processing; else drop segment and return. 9023 */ 9024 if ((thflags & TH_ACK) == 0) { 9025 if (IS_FASTOPEN(tp->t_flags)) { 9026 cc_conn_init(tp); 9027 } 9028 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9029 tiwin, thflags, nxt_pkt)); 9030 } 9031 KMOD_TCPSTAT_INC(tcps_connects); 9032 if (tp->t_flags & TF_SONOTCONN) { 9033 tp->t_flags &= ~TF_SONOTCONN; 9034 soisconnected(so); 9035 } 9036 /* Do window scaling? */ 9037 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 9038 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 9039 tp->rcv_scale = tp->request_r_scale; 9040 } 9041 /* 9042 * ok for the first time in lets see if we can use the ts to figure 9043 * out what the initial RTT was. 9044 */ 9045 if ((to->to_flags & TOF_TS) != 0) { 9046 uint32_t t, rtt; 9047 9048 t = tcp_tv_to_mssectick(&bbr->rc_tv); 9049 if (TSTMP_GEQ(t, to->to_tsecr)) { 9050 rtt = t - to->to_tsecr; 9051 if (rtt == 0) { 9052 rtt = 1; 9053 } 9054 rtt *= MS_IN_USEC; 9055 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 9056 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime); 9057 } 9058 } 9059 /* Drop off any SYN in the send map (probably not there) */ 9060 if (thflags & TH_ACK) 9061 bbr_log_syn(tp, to); 9062 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 9063 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 9064 tp->t_tfo_pending = NULL; 9065 } 9066 /* 9067 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 9068 * FIN-WAIT-1 9069 */ 9070 tp->t_starttime = ticks; 9071 if (tp->t_flags & TF_NEEDFIN) { 9072 tcp_state_change(tp, TCPS_FIN_WAIT_1); 9073 tp->t_flags &= ~TF_NEEDFIN; 9074 } else { 9075 tcp_state_change(tp, TCPS_ESTABLISHED); 9076 TCP_PROBE5(accept__established, NULL, tp, 9077 mtod(m, const char *), tp, th); 9078 /* 9079 * TFO connections call cc_conn_init() during SYN 9080 * processing. Calling it again here for such connections 9081 * is not harmless as it would undo the snd_cwnd reduction 9082 * that occurs when a TFO SYN|ACK is retransmitted. 9083 */ 9084 if (!IS_FASTOPEN(tp->t_flags)) 9085 cc_conn_init(tp); 9086 } 9087 /* 9088 * Account for the ACK of our SYN prior to 9089 * regular ACK processing below, except for 9090 * simultaneous SYN, which is handled later. 9091 */ 9092 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 9093 tp->snd_una++; 9094 /* 9095 * If segment contains data or ACK, will call tcp_reass() later; if 9096 * not, do so now to pass queued data to user. 9097 */ 9098 if (tlen == 0 && (thflags & TH_FIN) == 0) { 9099 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 9100 (struct mbuf *)0); 9101 if (tp->t_flags & TF_WAKESOR) { 9102 tp->t_flags &= ~TF_WAKESOR; 9103 /* NB: sorwakeup_locked() does an implicit unlock. */ 9104 sorwakeup_locked(so); 9105 } 9106 } 9107 tp->snd_wl1 = th->th_seq - 1; 9108 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9109 return (ret_val); 9110 } 9111 if (tp->t_state == TCPS_FIN_WAIT_1) { 9112 /* We could have went to FIN_WAIT_1 (or EST) above */ 9113 /* 9114 * In FIN_WAIT_1 STATE in addition to the processing for the 9115 * ESTABLISHED state if our FIN is now acknowledged then 9116 * enter FIN_WAIT_2. 9117 */ 9118 if (ourfinisacked) { 9119 /* 9120 * If we can't receive any more data, then closing 9121 * user can proceed. Starting the timer is contrary 9122 * to the specification, but if we don't get a FIN 9123 * we'll hang forever. 9124 * 9125 * XXXjl: we should release the tp also, and use a 9126 * compressed state. 9127 */ 9128 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9129 soisdisconnected(so); 9130 tcp_timer_activate(tp, TT_2MSL, 9131 (tcp_fast_finwait2_recycle ? 9132 tcp_finwait2_timeout : 9133 TP_MAXIDLE(tp))); 9134 } 9135 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9136 } 9137 } 9138 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9139 tiwin, thflags, nxt_pkt)); 9140 } 9141 9142 /* 9143 * Return value of 1, the TCB is unlocked and most 9144 * likely gone, return value of 0, the TCB is still 9145 * locked. 9146 */ 9147 static int 9148 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 9149 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9150 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9151 { 9152 struct tcp_bbr *bbr; 9153 int32_t ret_val; 9154 9155 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9156 9157 /* 9158 * Header prediction: check for the two common cases of a 9159 * uni-directional data xfer. If the packet has no control flags, 9160 * is in-sequence, the window didn't change and we're not 9161 * retransmitting, it's a candidate. If the length is zero and the 9162 * ack moved forward, we're the sender side of the xfer. Just free 9163 * the data acked & wake any higher level process that was blocked 9164 * waiting for space. If the length is non-zero and the ack didn't 9165 * move, we're the receiver side. If we're getting packets in-order 9166 * (the reassembly queue is empty), add the data toc The socket 9167 * buffer and note that we need a delayed ack. Make sure that the 9168 * hidden state-flags are also off. Since we check for 9169 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 9170 */ 9171 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9172 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) { 9173 /* 9174 * If we have delived under 4 segments increase the initial 9175 * window if raised by the peer. We use this to determine 9176 * dynamic and static rwnd's at the end of a connection. 9177 */ 9178 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd); 9179 } 9180 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 9181 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 9182 __predict_true(SEGQ_EMPTY(tp)) && 9183 __predict_true(th->th_seq == tp->rcv_nxt)) { 9184 if (tlen == 0) { 9185 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 9186 tiwin, nxt_pkt, iptos)) { 9187 return (0); 9188 } 9189 } else { 9190 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 9191 tiwin, nxt_pkt)) { 9192 return (0); 9193 } 9194 } 9195 } 9196 ctf_calc_rwin(so, tp); 9197 9198 if ((thflags & TH_RST) || 9199 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9200 return (ctf_process_rst(m, th, so, tp)); 9201 /* 9202 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9203 * synchronized state. 9204 */ 9205 if (thflags & TH_SYN) { 9206 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9207 return (ret_val); 9208 } 9209 /* 9210 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9211 * it's less than ts_recent, drop it. 9212 */ 9213 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9214 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9215 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9216 return (ret_val); 9217 } 9218 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9219 return (ret_val); 9220 } 9221 /* 9222 * If last ACK falls within this segment's sequence numbers, record 9223 * its timestamp. NOTE: 1) That the test incorporates suggestions 9224 * from the latest proposal of the tcplw@cray.com list (Braden 9225 * 1993/04/26). 2) That updating only on newer timestamps interferes 9226 * with our earlier PAWS tests, so this check should be solely 9227 * predicated on the sequence space of this segment. 3) That we 9228 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9229 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9230 * SEG.Len, This modified check allows us to overcome RFC1323's 9231 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9232 * p.869. In such cases, we can still calculate the RTT correctly 9233 * when RCV.NXT == Last.ACK.Sent. 9234 */ 9235 if ((to->to_flags & TOF_TS) != 0 && 9236 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9237 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9238 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9239 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9240 tp->ts_recent = to->to_tsval; 9241 } 9242 /* 9243 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9244 * is on (half-synchronized state), then queue data for later 9245 * processing; else drop segment and return. 9246 */ 9247 if ((thflags & TH_ACK) == 0) { 9248 if (tp->t_flags & TF_NEEDSYN) { 9249 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9250 tiwin, thflags, nxt_pkt)); 9251 } else if (tp->t_flags & TF_ACKNOW) { 9252 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9253 bbr->r_wanted_output = 1; 9254 return (ret_val); 9255 } else { 9256 ctf_do_drop(m, NULL); 9257 return (0); 9258 } 9259 } 9260 /* 9261 * Ack processing. 9262 */ 9263 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9264 return (ret_val); 9265 } 9266 if (sbavail(&so->so_snd)) { 9267 if (ctf_progress_timeout_check(tp, true)) { 9268 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9269 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9270 return (1); 9271 } 9272 } 9273 /* State changes only happen in bbr_process_data() */ 9274 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9275 tiwin, thflags, nxt_pkt)); 9276 } 9277 9278 /* 9279 * Return value of 1, the TCB is unlocked and most 9280 * likely gone, return value of 0, the TCB is still 9281 * locked. 9282 */ 9283 static int 9284 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 9285 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9286 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9287 { 9288 struct tcp_bbr *bbr; 9289 int32_t ret_val; 9290 9291 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9292 9293 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9294 ctf_calc_rwin(so, tp); 9295 if ((thflags & TH_RST) || 9296 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9297 return (ctf_process_rst(m, th, so, tp)); 9298 /* 9299 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9300 * synchronized state. 9301 */ 9302 if (thflags & TH_SYN) { 9303 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9304 return (ret_val); 9305 } 9306 /* 9307 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9308 * it's less than ts_recent, drop it. 9309 */ 9310 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9311 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9312 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9313 return (ret_val); 9314 } 9315 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9316 return (ret_val); 9317 } 9318 /* 9319 * If last ACK falls within this segment's sequence numbers, record 9320 * its timestamp. NOTE: 1) That the test incorporates suggestions 9321 * from the latest proposal of the tcplw@cray.com list (Braden 9322 * 1993/04/26). 2) That updating only on newer timestamps interferes 9323 * with our earlier PAWS tests, so this check should be solely 9324 * predicated on the sequence space of this segment. 3) That we 9325 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9326 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9327 * SEG.Len, This modified check allows us to overcome RFC1323's 9328 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9329 * p.869. In such cases, we can still calculate the RTT correctly 9330 * when RCV.NXT == Last.ACK.Sent. 9331 */ 9332 if ((to->to_flags & TOF_TS) != 0 && 9333 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9334 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9335 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9336 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9337 tp->ts_recent = to->to_tsval; 9338 } 9339 /* 9340 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9341 * is on (half-synchronized state), then queue data for later 9342 * processing; else drop segment and return. 9343 */ 9344 if ((thflags & TH_ACK) == 0) { 9345 if (tp->t_flags & TF_NEEDSYN) { 9346 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9347 tiwin, thflags, nxt_pkt)); 9348 } else if (tp->t_flags & TF_ACKNOW) { 9349 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9350 bbr->r_wanted_output = 1; 9351 return (ret_val); 9352 } else { 9353 ctf_do_drop(m, NULL); 9354 return (0); 9355 } 9356 } 9357 /* 9358 * Ack processing. 9359 */ 9360 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9361 return (ret_val); 9362 } 9363 if (sbavail(&so->so_snd)) { 9364 if (ctf_progress_timeout_check(tp, true)) { 9365 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9366 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9367 return (1); 9368 } 9369 } 9370 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9371 tiwin, thflags, nxt_pkt)); 9372 } 9373 9374 static int 9375 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr, 9376 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so) 9377 { 9378 9379 if (bbr->rc_allow_data_af_clo == 0) { 9380 close_now: 9381 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 9382 /* tcp_close will kill the inp pre-log the Reset */ 9383 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 9384 tp = tcp_close(tp); 9385 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 9386 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 9387 return (1); 9388 } 9389 if (sbavail(&so->so_snd) == 0) 9390 goto close_now; 9391 /* Ok we allow data that is ignored and a followup reset */ 9392 tp->rcv_nxt = th->th_seq + *tlen; 9393 tp->t_flags2 |= TF2_DROP_AF_DATA; 9394 bbr->r_wanted_output = 1; 9395 *tlen = 0; 9396 return (0); 9397 } 9398 9399 /* 9400 * Return value of 1, the TCB is unlocked and most 9401 * likely gone, return value of 0, the TCB is still 9402 * locked. 9403 */ 9404 static int 9405 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 9406 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9407 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9408 { 9409 int32_t ourfinisacked = 0; 9410 int32_t ret_val; 9411 struct tcp_bbr *bbr; 9412 9413 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9414 9415 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9416 ctf_calc_rwin(so, tp); 9417 if ((thflags & TH_RST) || 9418 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9419 return (ctf_process_rst(m, th, so, tp)); 9420 /* 9421 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9422 * synchronized state. 9423 */ 9424 if (thflags & TH_SYN) { 9425 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9426 return (ret_val); 9427 } 9428 /* 9429 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9430 * it's less than ts_recent, drop it. 9431 */ 9432 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9433 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9434 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9435 return (ret_val); 9436 } 9437 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9438 return (ret_val); 9439 } 9440 /* 9441 * If new data are received on a connection after the user processes 9442 * are gone, then RST the other end. 9443 * We call a new function now so we might continue and setup 9444 * to reset at all data being ack'd. 9445 */ 9446 if ((tp->t_flags & TF_CLOSED) && tlen && 9447 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9448 return (1); 9449 /* 9450 * If last ACK falls within this segment's sequence numbers, record 9451 * its timestamp. NOTE: 1) That the test incorporates suggestions 9452 * from the latest proposal of the tcplw@cray.com list (Braden 9453 * 1993/04/26). 2) That updating only on newer timestamps interferes 9454 * with our earlier PAWS tests, so this check should be solely 9455 * predicated on the sequence space of this segment. 3) That we 9456 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9457 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9458 * SEG.Len, This modified check allows us to overcome RFC1323's 9459 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9460 * p.869. In such cases, we can still calculate the RTT correctly 9461 * when RCV.NXT == Last.ACK.Sent. 9462 */ 9463 if ((to->to_flags & TOF_TS) != 0 && 9464 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9465 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9466 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9467 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9468 tp->ts_recent = to->to_tsval; 9469 } 9470 /* 9471 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9472 * is on (half-synchronized state), then queue data for later 9473 * processing; else drop segment and return. 9474 */ 9475 if ((thflags & TH_ACK) == 0) { 9476 if (tp->t_flags & TF_NEEDSYN) { 9477 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9478 tiwin, thflags, nxt_pkt)); 9479 } else if (tp->t_flags & TF_ACKNOW) { 9480 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9481 bbr->r_wanted_output = 1; 9482 return (ret_val); 9483 } else { 9484 ctf_do_drop(m, NULL); 9485 return (0); 9486 } 9487 } 9488 /* 9489 * Ack processing. 9490 */ 9491 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9492 return (ret_val); 9493 } 9494 if (ourfinisacked) { 9495 /* 9496 * If we can't receive any more data, then closing user can 9497 * proceed. Starting the timer is contrary to the 9498 * specification, but if we don't get a FIN we'll hang 9499 * forever. 9500 * 9501 * XXXjl: we should release the tp also, and use a 9502 * compressed state. 9503 */ 9504 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9505 soisdisconnected(so); 9506 tcp_timer_activate(tp, TT_2MSL, 9507 (tcp_fast_finwait2_recycle ? 9508 tcp_finwait2_timeout : 9509 TP_MAXIDLE(tp))); 9510 } 9511 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9512 } 9513 if (sbavail(&so->so_snd)) { 9514 if (ctf_progress_timeout_check(tp, true)) { 9515 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9516 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9517 return (1); 9518 } 9519 } 9520 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9521 tiwin, thflags, nxt_pkt)); 9522 } 9523 9524 /* 9525 * Return value of 1, the TCB is unlocked and most 9526 * likely gone, return value of 0, the TCB is still 9527 * locked. 9528 */ 9529 static int 9530 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 9531 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9532 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9533 { 9534 int32_t ourfinisacked = 0; 9535 int32_t ret_val; 9536 struct tcp_bbr *bbr; 9537 9538 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9539 9540 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9541 ctf_calc_rwin(so, tp); 9542 if ((thflags & TH_RST) || 9543 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9544 return (ctf_process_rst(m, th, so, tp)); 9545 /* 9546 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9547 * synchronized state. 9548 */ 9549 if (thflags & TH_SYN) { 9550 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9551 return (ret_val); 9552 } 9553 /* 9554 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9555 * it's less than ts_recent, drop it. 9556 */ 9557 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9558 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9559 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9560 return (ret_val); 9561 } 9562 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9563 return (ret_val); 9564 } 9565 /* 9566 * If new data are received on a connection after the user processes 9567 * are gone, then RST the other end. 9568 * We call a new function now so we might continue and setup 9569 * to reset at all data being ack'd. 9570 */ 9571 if ((tp->t_flags & TF_CLOSED) && tlen && 9572 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9573 return (1); 9574 /* 9575 * If last ACK falls within this segment's sequence numbers, record 9576 * its timestamp. NOTE: 1) That the test incorporates suggestions 9577 * from the latest proposal of the tcplw@cray.com list (Braden 9578 * 1993/04/26). 2) That updating only on newer timestamps interferes 9579 * with our earlier PAWS tests, so this check should be solely 9580 * predicated on the sequence space of this segment. 3) That we 9581 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9582 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9583 * SEG.Len, This modified check allows us to overcome RFC1323's 9584 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9585 * p.869. In such cases, we can still calculate the RTT correctly 9586 * when RCV.NXT == Last.ACK.Sent. 9587 */ 9588 if ((to->to_flags & TOF_TS) != 0 && 9589 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9590 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9591 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9592 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9593 tp->ts_recent = to->to_tsval; 9594 } 9595 /* 9596 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9597 * is on (half-synchronized state), then queue data for later 9598 * processing; else drop segment and return. 9599 */ 9600 if ((thflags & TH_ACK) == 0) { 9601 if (tp->t_flags & TF_NEEDSYN) { 9602 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9603 tiwin, thflags, nxt_pkt)); 9604 } else if (tp->t_flags & TF_ACKNOW) { 9605 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9606 bbr->r_wanted_output = 1; 9607 return (ret_val); 9608 } else { 9609 ctf_do_drop(m, NULL); 9610 return (0); 9611 } 9612 } 9613 /* 9614 * Ack processing. 9615 */ 9616 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9617 return (ret_val); 9618 } 9619 if (ourfinisacked) { 9620 tcp_twstart(tp); 9621 m_freem(m); 9622 return (1); 9623 } 9624 if (sbavail(&so->so_snd)) { 9625 if (ctf_progress_timeout_check(tp, true)) { 9626 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9627 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9628 return (1); 9629 } 9630 } 9631 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9632 tiwin, thflags, nxt_pkt)); 9633 } 9634 9635 /* 9636 * Return value of 1, the TCB is unlocked and most 9637 * likely gone, return value of 0, the TCB is still 9638 * locked. 9639 */ 9640 static int 9641 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 9642 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9643 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9644 { 9645 int32_t ourfinisacked = 0; 9646 int32_t ret_val; 9647 struct tcp_bbr *bbr; 9648 9649 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9650 9651 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9652 ctf_calc_rwin(so, tp); 9653 if ((thflags & TH_RST) || 9654 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9655 return (ctf_process_rst(m, th, so, tp)); 9656 /* 9657 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9658 * synchronized state. 9659 */ 9660 if (thflags & TH_SYN) { 9661 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9662 return (ret_val); 9663 } 9664 /* 9665 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9666 * it's less than ts_recent, drop it. 9667 */ 9668 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9669 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9670 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9671 return (ret_val); 9672 } 9673 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9674 return (ret_val); 9675 } 9676 /* 9677 * If new data are received on a connection after the user processes 9678 * are gone, then RST the other end. 9679 * We call a new function now so we might continue and setup 9680 * to reset at all data being ack'd. 9681 */ 9682 if ((tp->t_flags & TF_CLOSED) && tlen && 9683 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9684 return (1); 9685 /* 9686 * If last ACK falls within this segment's sequence numbers, record 9687 * its timestamp. NOTE: 1) That the test incorporates suggestions 9688 * from the latest proposal of the tcplw@cray.com list (Braden 9689 * 1993/04/26). 2) That updating only on newer timestamps interferes 9690 * with our earlier PAWS tests, so this check should be solely 9691 * predicated on the sequence space of this segment. 3) That we 9692 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9693 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9694 * SEG.Len, This modified check allows us to overcome RFC1323's 9695 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9696 * p.869. In such cases, we can still calculate the RTT correctly 9697 * when RCV.NXT == Last.ACK.Sent. 9698 */ 9699 if ((to->to_flags & TOF_TS) != 0 && 9700 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9701 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9702 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9703 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9704 tp->ts_recent = to->to_tsval; 9705 } 9706 /* 9707 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9708 * is on (half-synchronized state), then queue data for later 9709 * processing; else drop segment and return. 9710 */ 9711 if ((thflags & TH_ACK) == 0) { 9712 if (tp->t_flags & TF_NEEDSYN) { 9713 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9714 tiwin, thflags, nxt_pkt)); 9715 } else if (tp->t_flags & TF_ACKNOW) { 9716 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9717 bbr->r_wanted_output = 1; 9718 return (ret_val); 9719 } else { 9720 ctf_do_drop(m, NULL); 9721 return (0); 9722 } 9723 } 9724 /* 9725 * case TCPS_LAST_ACK: Ack processing. 9726 */ 9727 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9728 return (ret_val); 9729 } 9730 if (ourfinisacked) { 9731 tp = tcp_close(tp); 9732 ctf_do_drop(m, tp); 9733 return (1); 9734 } 9735 if (sbavail(&so->so_snd)) { 9736 if (ctf_progress_timeout_check(tp, true)) { 9737 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9738 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9739 return (1); 9740 } 9741 } 9742 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9743 tiwin, thflags, nxt_pkt)); 9744 } 9745 9746 /* 9747 * Return value of 1, the TCB is unlocked and most 9748 * likely gone, return value of 0, the TCB is still 9749 * locked. 9750 */ 9751 static int 9752 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 9753 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9754 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9755 { 9756 int32_t ourfinisacked = 0; 9757 int32_t ret_val; 9758 struct tcp_bbr *bbr; 9759 9760 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9761 9762 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9763 ctf_calc_rwin(so, tp); 9764 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 9765 if ((thflags & TH_RST) || 9766 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9767 return (ctf_process_rst(m, th, so, tp)); 9768 9769 /* 9770 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9771 * synchronized state. 9772 */ 9773 if (thflags & TH_SYN) { 9774 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9775 return (ret_val); 9776 } 9777 /* 9778 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9779 * it's less than ts_recent, drop it. 9780 */ 9781 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9782 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9783 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9784 return (ret_val); 9785 } 9786 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9787 return (ret_val); 9788 } 9789 /* 9790 * If new data are received on a connection after the user processes 9791 * are gone, then we may RST the other end depending on the outcome 9792 * of bbr_check_data_after_close. 9793 * We call a new function now so we might continue and setup 9794 * to reset at all data being ack'd. 9795 */ 9796 if ((tp->t_flags & TF_CLOSED) && tlen && 9797 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9798 return (1); 9799 /* 9800 * If last ACK falls within this segment's sequence numbers, record 9801 * its timestamp. NOTE: 1) That the test incorporates suggestions 9802 * from the latest proposal of the tcplw@cray.com list (Braden 9803 * 1993/04/26). 2) That updating only on newer timestamps interferes 9804 * with our earlier PAWS tests, so this check should be solely 9805 * predicated on the sequence space of this segment. 3) That we 9806 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9807 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9808 * SEG.Len, This modified check allows us to overcome RFC1323's 9809 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9810 * p.869. In such cases, we can still calculate the RTT correctly 9811 * when RCV.NXT == Last.ACK.Sent. 9812 */ 9813 if ((to->to_flags & TOF_TS) != 0 && 9814 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9815 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9816 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9817 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9818 tp->ts_recent = to->to_tsval; 9819 } 9820 /* 9821 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9822 * is on (half-synchronized state), then queue data for later 9823 * processing; else drop segment and return. 9824 */ 9825 if ((thflags & TH_ACK) == 0) { 9826 if (tp->t_flags & TF_NEEDSYN) { 9827 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9828 tiwin, thflags, nxt_pkt)); 9829 } else if (tp->t_flags & TF_ACKNOW) { 9830 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9831 bbr->r_wanted_output = 1; 9832 return (ret_val); 9833 } else { 9834 ctf_do_drop(m, NULL); 9835 return (0); 9836 } 9837 } 9838 /* 9839 * Ack processing. 9840 */ 9841 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9842 return (ret_val); 9843 } 9844 if (sbavail(&so->so_snd)) { 9845 if (ctf_progress_timeout_check(tp, true)) { 9846 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9847 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9848 return (1); 9849 } 9850 } 9851 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9852 tiwin, thflags, nxt_pkt)); 9853 } 9854 9855 static void 9856 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr) 9857 { 9858 /* 9859 * Assure no timers are running. 9860 */ 9861 if (tcp_timer_active(tp, TT_PERSIST)) { 9862 /* We enter in persists, set the flag appropriately */ 9863 bbr->rc_in_persist = 1; 9864 } 9865 } 9866 9867 static void 9868 bbr_google_mode_on(struct tcp_bbr *bbr) 9869 { 9870 bbr->rc_use_google = 1; 9871 bbr->rc_no_pacing = 0; 9872 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9873 bbr->r_use_policer = bbr_policer_detection_enabled; 9874 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 9875 bbr->bbr_use_rack_cheat = 0; 9876 bbr->r_ctl.rc_incr_tmrs = 0; 9877 bbr->r_ctl.rc_inc_tcp_oh = 0; 9878 bbr->r_ctl.rc_inc_ip_oh = 0; 9879 bbr->r_ctl.rc_inc_enet_oh = 0; 9880 reset_time(&bbr->r_ctl.rc_delrate, 9881 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 9882 reset_time_small(&bbr->r_ctl.rc_rttprop, 9883 (11 * USECS_IN_SECOND)); 9884 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9885 } 9886 9887 static void 9888 bbr_google_mode_off(struct tcp_bbr *bbr) 9889 { 9890 bbr->rc_use_google = 0; 9891 bbr->r_ctl.bbr_google_discount = 0; 9892 bbr->no_pacing_until = bbr_no_pacing_until; 9893 bbr->r_use_policer = 0; 9894 if (bbr->no_pacing_until) 9895 bbr->rc_no_pacing = 1; 9896 else 9897 bbr->rc_no_pacing = 0; 9898 if (bbr_use_rack_resend_cheat) 9899 bbr->bbr_use_rack_cheat = 1; 9900 else 9901 bbr->bbr_use_rack_cheat = 0; 9902 if (bbr_incr_timers) 9903 bbr->r_ctl.rc_incr_tmrs = 1; 9904 else 9905 bbr->r_ctl.rc_incr_tmrs = 0; 9906 if (bbr_include_tcp_oh) 9907 bbr->r_ctl.rc_inc_tcp_oh = 1; 9908 else 9909 bbr->r_ctl.rc_inc_tcp_oh = 0; 9910 if (bbr_include_ip_oh) 9911 bbr->r_ctl.rc_inc_ip_oh = 1; 9912 else 9913 bbr->r_ctl.rc_inc_ip_oh = 0; 9914 if (bbr_include_enet_oh) 9915 bbr->r_ctl.rc_inc_enet_oh = 1; 9916 else 9917 bbr->r_ctl.rc_inc_enet_oh = 0; 9918 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 9919 reset_time(&bbr->r_ctl.rc_delrate, 9920 bbr_num_pktepo_for_del_limit); 9921 reset_time_small(&bbr->r_ctl.rc_rttprop, 9922 (bbr_filter_len_sec * USECS_IN_SECOND)); 9923 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9924 } 9925 /* 9926 * Return 0 on success, non-zero on failure 9927 * which indicates the error (usually no memory). 9928 */ 9929 static int 9930 bbr_init(struct tcpcb *tp, void **ptr) 9931 { 9932 struct inpcb *inp = tptoinpcb(tp); 9933 struct tcp_bbr *bbr = NULL; 9934 uint32_t cts; 9935 9936 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO)); 9937 if (*ptr == NULL) { 9938 /* 9939 * We need to allocate memory but cant. The INP and INP_INFO 9940 * locks and they are recursive (happens during setup. So a 9941 * scheme to drop the locks fails :( 9942 * 9943 */ 9944 return (ENOMEM); 9945 } 9946 bbr = (struct tcp_bbr *)*ptr; 9947 bbr->rtt_valid = 0; 9948 inp->inp_flags2 |= INP_CANNOT_DO_ECN; 9949 inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 9950 /* Take off any undesired flags */ 9951 inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY; 9952 inp->inp_flags2 &= ~INP_DONT_SACK_QUEUE; 9953 inp->inp_flags2 &= ~INP_MBUF_ACKCMP; 9954 inp->inp_flags2 &= ~INP_MBUF_L_ACKS; 9955 9956 TAILQ_INIT(&bbr->r_ctl.rc_map); 9957 TAILQ_INIT(&bbr->r_ctl.rc_free); 9958 TAILQ_INIT(&bbr->r_ctl.rc_tmap); 9959 bbr->rc_tp = tp; 9960 bbr->rc_inp = inp; 9961 cts = tcp_get_usecs(&bbr->rc_tv); 9962 tp->t_acktime = 0; 9963 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close; 9964 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade; 9965 bbr->rc_tlp_threshold = bbr_tlp_thresh; 9966 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh; 9967 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay; 9968 bbr->r_ctl.rc_min_to = bbr_min_to; 9969 bbr->rc_bbr_state = BBR_STATE_STARTUP; 9970 bbr->r_ctl.bbr_lost_at_state = 0; 9971 bbr->r_ctl.rc_lost_at_startup = 0; 9972 bbr->rc_all_timers_stopped = 0; 9973 bbr->r_ctl.rc_bbr_lastbtlbw = 0; 9974 bbr->r_ctl.rc_pkt_epoch_del = 0; 9975 bbr->r_ctl.rc_pkt_epoch = 0; 9976 bbr->r_ctl.rc_lowest_rtt = 0xffffffff; 9977 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain; 9978 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 9979 bbr->r_ctl.rc_went_idle_time = cts; 9980 bbr->rc_pacer_started = cts; 9981 bbr->r_ctl.rc_pkt_epoch_time = cts; 9982 bbr->r_ctl.rc_rcvtime = cts; 9983 bbr->r_ctl.rc_bbr_state_time = cts; 9984 bbr->r_ctl.rc_del_time = cts; 9985 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 9986 bbr->r_ctl.last_in_probertt = cts; 9987 bbr->skip_gain = 0; 9988 bbr->gain_is_limited = 0; 9989 bbr->no_pacing_until = bbr_no_pacing_until; 9990 if (bbr->no_pacing_until) 9991 bbr->rc_no_pacing = 1; 9992 if (bbr_use_google_algo) { 9993 bbr->rc_no_pacing = 0; 9994 bbr->rc_use_google = 1; 9995 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9996 bbr->r_use_policer = bbr_policer_detection_enabled; 9997 } else { 9998 bbr->rc_use_google = 0; 9999 bbr->r_ctl.bbr_google_discount = 0; 10000 bbr->r_use_policer = 0; 10001 } 10002 if (bbr_ts_limiting) 10003 bbr->rc_use_ts_limit = 1; 10004 else 10005 bbr->rc_use_ts_limit = 0; 10006 if (bbr_ts_can_raise) 10007 bbr->ts_can_raise = 1; 10008 else 10009 bbr->ts_can_raise = 0; 10010 if (V_tcp_delack_enabled == 1) 10011 tp->t_delayed_ack = 2; 10012 else if (V_tcp_delack_enabled == 0) 10013 tp->t_delayed_ack = 0; 10014 else if (V_tcp_delack_enabled < 100) 10015 tp->t_delayed_ack = V_tcp_delack_enabled; 10016 else 10017 tp->t_delayed_ack = 2; 10018 if (bbr->rc_use_google == 0) 10019 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10020 else 10021 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 10022 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms; 10023 bbr->rc_max_rto_sec = bbr_rto_max_sec; 10024 bbr->rc_init_win = bbr_def_init_win; 10025 if (tp->t_flags & TF_REQ_TSTMP) 10026 bbr->rc_last_options = TCP_TS_OVERHEAD; 10027 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options; 10028 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd; 10029 bbr->r_init_rtt = 1; 10030 10031 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 10032 if (bbr_allow_hdwr_pacing) 10033 bbr->bbr_hdw_pace_ena = 1; 10034 else 10035 bbr->bbr_hdw_pace_ena = 0; 10036 if (bbr_sends_full_iwnd) 10037 bbr->bbr_init_win_cheat = 1; 10038 else 10039 bbr->bbr_init_win_cheat = 0; 10040 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max; 10041 bbr->r_ctl.rc_drain_pg = bbr_drain_gain; 10042 bbr->r_ctl.rc_startup_pg = bbr_high_gain; 10043 bbr->rc_loss_exit = bbr_exit_startup_at_loss; 10044 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain; 10045 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second; 10046 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar; 10047 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max; 10048 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor; 10049 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min; 10050 bbr->r_ctl.bbr_cross_over = bbr_cross_over; 10051 bbr->r_ctl.rc_rtt_shrinks = cts; 10052 if (bbr->rc_use_google) { 10053 setup_time_filter(&bbr->r_ctl.rc_delrate, 10054 FILTER_TYPE_MAX, 10055 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 10056 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10057 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND)); 10058 } else { 10059 setup_time_filter(&bbr->r_ctl.rc_delrate, 10060 FILTER_TYPE_MAX, 10061 bbr_num_pktepo_for_del_limit); 10062 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10063 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND)); 10064 } 10065 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0); 10066 if (bbr_uses_idle_restart) 10067 bbr->rc_use_idle_restart = 1; 10068 else 10069 bbr->rc_use_idle_restart = 0; 10070 bbr->r_ctl.rc_bbr_cur_del_rate = 0; 10071 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps; 10072 if (bbr_resends_use_tso) 10073 bbr->rc_resends_use_tso = 1; 10074 #ifdef NETFLIX_PEAKRATE 10075 tp->t_peakrate_thr = tp->t_maxpeakrate; 10076 #endif 10077 if (tp->snd_una != tp->snd_max) { 10078 /* Create a send map for the current outstanding data */ 10079 struct bbr_sendmap *rsm; 10080 10081 rsm = bbr_alloc(bbr); 10082 if (rsm == NULL) { 10083 uma_zfree(bbr_pcb_zone, *ptr); 10084 *ptr = NULL; 10085 return (ENOMEM); 10086 } 10087 rsm->r_rtt_not_allowed = 1; 10088 rsm->r_tim_lastsent[0] = cts; 10089 rsm->r_rtr_cnt = 1; 10090 rsm->r_rtr_bytes = 0; 10091 rsm->r_start = tp->snd_una; 10092 rsm->r_end = tp->snd_max; 10093 rsm->r_dupack = 0; 10094 rsm->r_delivered = bbr->r_ctl.rc_delivered; 10095 rsm->r_ts_valid = 0; 10096 rsm->r_del_ack_ts = tp->ts_recent; 10097 rsm->r_del_time = cts; 10098 if (bbr->r_ctl.r_app_limited_until) 10099 rsm->r_app_limited = 1; 10100 else 10101 rsm->r_app_limited = 0; 10102 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 10103 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 10104 rsm->r_in_tmap = 1; 10105 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 10106 rsm->r_bbr_state = bbr_state_val(bbr); 10107 else 10108 rsm->r_bbr_state = 8; 10109 } 10110 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0)) 10111 bbr->bbr_use_rack_cheat = 1; 10112 if (bbr_incr_timers && (bbr->rc_use_google == 0)) 10113 bbr->r_ctl.rc_incr_tmrs = 1; 10114 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0)) 10115 bbr->r_ctl.rc_inc_tcp_oh = 1; 10116 if (bbr_include_ip_oh && (bbr->rc_use_google == 0)) 10117 bbr->r_ctl.rc_inc_ip_oh = 1; 10118 if (bbr_include_enet_oh && (bbr->rc_use_google == 0)) 10119 bbr->r_ctl.rc_inc_enet_oh = 1; 10120 10121 bbr_log_type_statechange(bbr, cts, __LINE__); 10122 if (TCPS_HAVEESTABLISHED(tp->t_state) && 10123 (tp->t_srtt)) { 10124 uint32_t rtt; 10125 10126 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 10127 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 10128 } 10129 /* announce the settings and state */ 10130 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT); 10131 tcp_bbr_tso_size_check(bbr, cts); 10132 /* 10133 * Now call the generic function to start a timer. This will place 10134 * the TCB on the hptsi wheel if a timer is needed with appropriate 10135 * flags. 10136 */ 10137 bbr_stop_all_timers(tp, bbr); 10138 /* 10139 * Validate the timers are not in usec, if they are convert. 10140 * BBR should in theory move to USEC and get rid of a 10141 * lot of the TICKS_2 calls.. but for now we stay 10142 * with tick timers. 10143 */ 10144 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 10145 TCPT_RANGESET(tp->t_rxtcur, 10146 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 10147 tp->t_rttmin, TCPTV_REXMTMAX); 10148 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0); 10149 return (0); 10150 } 10151 10152 /* 10153 * Return 0 if we can accept the connection. Return 10154 * non-zero if we can't handle the connection. A EAGAIN 10155 * means you need to wait until the connection is up. 10156 * a EADDRNOTAVAIL means we can never handle the connection 10157 * (no SACK). 10158 */ 10159 static int 10160 bbr_handoff_ok(struct tcpcb *tp) 10161 { 10162 if ((tp->t_state == TCPS_CLOSED) || 10163 (tp->t_state == TCPS_LISTEN)) { 10164 /* Sure no problem though it may not stick */ 10165 return (0); 10166 } 10167 if ((tp->t_state == TCPS_SYN_SENT) || 10168 (tp->t_state == TCPS_SYN_RECEIVED)) { 10169 /* 10170 * We really don't know you have to get to ESTAB or beyond 10171 * to tell. 10172 */ 10173 return (EAGAIN); 10174 } 10175 if (tp->t_flags & TF_SENTFIN) 10176 return (EINVAL); 10177 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) { 10178 return (0); 10179 } 10180 /* 10181 * If we reach here we don't do SACK on this connection so we can 10182 * never do rack. 10183 */ 10184 return (EINVAL); 10185 } 10186 10187 static void 10188 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged) 10189 { 10190 if (tp->t_fb_ptr) { 10191 uint32_t calc; 10192 struct tcp_bbr *bbr; 10193 struct bbr_sendmap *rsm; 10194 10195 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 10196 if (bbr->r_ctl.crte) 10197 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 10198 bbr_log_flowend(bbr); 10199 bbr->rc_tp = NULL; 10200 if (bbr->bbr_hdrw_pacing) 10201 counter_u64_add(bbr_flows_whdwr_pacing, -1); 10202 else 10203 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 10204 if (bbr->r_ctl.crte != NULL) { 10205 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 10206 bbr->r_ctl.crte = NULL; 10207 } 10208 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10209 while (rsm) { 10210 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 10211 uma_zfree(bbr_zone, rsm); 10212 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10213 } 10214 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10215 while (rsm) { 10216 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 10217 uma_zfree(bbr_zone, rsm); 10218 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10219 } 10220 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd; 10221 if (calc > (bbr->r_ctl.rc_init_rwnd / 10)) 10222 BBR_STAT_INC(bbr_dynamic_rwnd); 10223 else 10224 BBR_STAT_INC(bbr_static_rwnd); 10225 bbr->r_ctl.rc_free_cnt = 0; 10226 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr); 10227 tp->t_fb_ptr = NULL; 10228 } 10229 /* Make sure snd_nxt is correctly set */ 10230 tp->snd_nxt = tp->snd_max; 10231 } 10232 10233 static void 10234 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win) 10235 { 10236 switch (tp->t_state) { 10237 case TCPS_SYN_SENT: 10238 bbr->r_state = TCPS_SYN_SENT; 10239 bbr->r_substate = bbr_do_syn_sent; 10240 break; 10241 case TCPS_SYN_RECEIVED: 10242 bbr->r_state = TCPS_SYN_RECEIVED; 10243 bbr->r_substate = bbr_do_syn_recv; 10244 break; 10245 case TCPS_ESTABLISHED: 10246 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd); 10247 bbr->r_state = TCPS_ESTABLISHED; 10248 bbr->r_substate = bbr_do_established; 10249 break; 10250 case TCPS_CLOSE_WAIT: 10251 bbr->r_state = TCPS_CLOSE_WAIT; 10252 bbr->r_substate = bbr_do_close_wait; 10253 break; 10254 case TCPS_FIN_WAIT_1: 10255 bbr->r_state = TCPS_FIN_WAIT_1; 10256 bbr->r_substate = bbr_do_fin_wait_1; 10257 break; 10258 case TCPS_CLOSING: 10259 bbr->r_state = TCPS_CLOSING; 10260 bbr->r_substate = bbr_do_closing; 10261 break; 10262 case TCPS_LAST_ACK: 10263 bbr->r_state = TCPS_LAST_ACK; 10264 bbr->r_substate = bbr_do_lastack; 10265 break; 10266 case TCPS_FIN_WAIT_2: 10267 bbr->r_state = TCPS_FIN_WAIT_2; 10268 bbr->r_substate = bbr_do_fin_wait_2; 10269 break; 10270 case TCPS_LISTEN: 10271 case TCPS_CLOSED: 10272 case TCPS_TIME_WAIT: 10273 default: 10274 break; 10275 }; 10276 } 10277 10278 static void 10279 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog) 10280 { 10281 /* 10282 * Now what state are we going into now? Is there adjustments 10283 * needed? 10284 */ 10285 int32_t old_state; 10286 10287 old_state = bbr_state_val(bbr); 10288 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) { 10289 /* Save the lowest srtt we saw in our end of the sub-state */ 10290 bbr->rc_hit_state_1 = 0; 10291 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff) 10292 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state; 10293 } 10294 bbr->rc_bbr_substate++; 10295 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) { 10296 /* Cycle back to first state-> gain */ 10297 bbr->rc_bbr_substate = 0; 10298 } 10299 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10300 /* 10301 * We enter the gain(5/4) cycle (possibly less if 10302 * shallow buffer detection is enabled) 10303 */ 10304 if (bbr->skip_gain) { 10305 /* 10306 * Hardware pacing has set our rate to 10307 * the max and limited our b/w just 10308 * do level i.e. no gain. 10309 */ 10310 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1]; 10311 } else if (bbr->gain_is_limited && 10312 bbr->bbr_hdrw_pacing && 10313 bbr->r_ctl.crte) { 10314 /* 10315 * We can't gain above the hardware pacing 10316 * rate which is less than our rate + the gain 10317 * calculate the gain needed to reach the hardware 10318 * pacing rate.. 10319 */ 10320 uint64_t bw, rate, gain_calc; 10321 10322 bw = bbr_get_bw(bbr); 10323 rate = bbr->r_ctl.crte->rate; 10324 if ((rate > bw) && 10325 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) { 10326 gain_calc = (rate * BBR_UNIT) / bw; 10327 if (gain_calc < BBR_UNIT) 10328 gain_calc = BBR_UNIT; 10329 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc; 10330 } else { 10331 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10332 } 10333 } else 10334 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10335 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) { 10336 bbr->r_ctl.rc_bbr_state_atflight = cts; 10337 } else 10338 bbr->r_ctl.rc_bbr_state_atflight = 0; 10339 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10340 bbr->rc_hit_state_1 = 1; 10341 bbr->r_ctl.rc_exta_time_gd = 0; 10342 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10343 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10344 if (bbr_state_drain_2_tar) { 10345 bbr->r_ctl.rc_bbr_state_atflight = 0; 10346 } else 10347 bbr->r_ctl.rc_bbr_state_atflight = cts; 10348 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN]; 10349 } else { 10350 /* All other cycles hit here 2-7 */ 10351 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) { 10352 if (bbr_sub_drain_slam_cwnd && 10353 (bbr->rc_use_google == 0) && 10354 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10355 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10356 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10357 } 10358 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP)) 10359 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) - 10360 bbr_get_rtt(bbr, BBR_RTT_PROP)); 10361 else 10362 bbr->r_ctl.rc_exta_time_gd = 0; 10363 if (bbr->r_ctl.rc_exta_time_gd) { 10364 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd; 10365 /* Now chop up the time for each state (div by 7) */ 10366 bbr->r_ctl.rc_level_state_extra /= 7; 10367 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) { 10368 /* Add a randomization */ 10369 bbr_randomize_extra_state_time(bbr); 10370 } 10371 } 10372 } 10373 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10374 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)]; 10375 } 10376 if (bbr->rc_use_google) { 10377 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10378 } 10379 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10380 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10381 if (dolog) 10382 bbr_log_type_statechange(bbr, cts, line); 10383 10384 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10385 uint32_t time_in; 10386 10387 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10388 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 10389 counter_u64_add(bbr_state_time[(old_state + 5)], time_in); 10390 } else { 10391 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10392 } 10393 } 10394 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 10395 bbr_set_state_target(bbr, __LINE__); 10396 if (bbr_sub_drain_slam_cwnd && 10397 (bbr->rc_use_google == 0) && 10398 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10399 /* Slam down the cwnd */ 10400 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10401 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10402 if (bbr_sub_drain_app_limit) { 10403 /* Go app limited if we are on a long drain */ 10404 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + 10405 ctf_flight_size(bbr->rc_tp, 10406 (bbr->r_ctl.rc_sacked + 10407 bbr->r_ctl.rc_lost_bytes))); 10408 } 10409 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10410 } 10411 if (bbr->rc_lt_use_bw) { 10412 /* In policed mode we clamp pacing_gain to BBR_UNIT */ 10413 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10414 } 10415 /* Google changes TSO size every cycle */ 10416 if (bbr->rc_use_google) 10417 tcp_bbr_tso_size_check(bbr, cts); 10418 bbr->r_ctl.gain_epoch = cts; 10419 bbr->r_ctl.rc_bbr_state_time = cts; 10420 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch; 10421 } 10422 10423 static void 10424 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10425 { 10426 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) && 10427 (google_allow_early_out == 1) && 10428 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) { 10429 /* We have reached out target flight size possibly early */ 10430 goto change_state; 10431 } 10432 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10433 return; 10434 } 10435 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) { 10436 /* 10437 * Must be a rttProp movement forward before 10438 * we can change states. 10439 */ 10440 return; 10441 } 10442 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10443 /* 10444 * The needed time has passed but for 10445 * the gain cycle extra rules apply: 10446 * 1) If we have seen loss, we exit 10447 * 2) If we have not reached the target 10448 * we stay in GAIN (gain-to-target). 10449 */ 10450 if (google_consider_lost && losses) 10451 goto change_state; 10452 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) { 10453 return; 10454 } 10455 } 10456 change_state: 10457 /* For gain we must reach our target, all others last 1 rttProp */ 10458 bbr_substate_change(bbr, cts, __LINE__, 1); 10459 } 10460 10461 static void 10462 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10463 { 10464 uint32_t flight, bbr_cur_cycle_time; 10465 10466 if (bbr->rc_use_google) { 10467 bbr_set_probebw_google_gains(bbr, cts, losses); 10468 return; 10469 } 10470 if (cts == 0) { 10471 /* 10472 * Never alow cts to be 0 we 10473 * do this so we can judge if 10474 * we have set a timestamp. 10475 */ 10476 cts = 1; 10477 } 10478 if (bbr_state_is_pkt_epoch) 10479 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 10480 else 10481 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP); 10482 10483 if (bbr->r_ctl.rc_bbr_state_atflight == 0) { 10484 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10485 flight = ctf_flight_size(bbr->rc_tp, 10486 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10487 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) { 10488 /* Keep it slam down */ 10489 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) { 10490 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10491 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10492 } 10493 if (bbr_sub_drain_app_limit) { 10494 /* Go app limited if we are on a long drain */ 10495 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight); 10496 } 10497 } 10498 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) && 10499 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) || 10500 (flight >= bbr->r_ctl.flightsize_at_drain))) { 10501 /* 10502 * Still here after the same time as 10503 * the gain. We need to drain harder 10504 * for the next srtt. Reduce by a set amount 10505 * the gain drop is capped at DRAIN states 10506 * value (88). 10507 */ 10508 bbr->r_ctl.flightsize_at_drain = flight; 10509 if (bbr_drain_drop_mul && 10510 bbr_drain_drop_div && 10511 (bbr_drain_drop_mul < bbr_drain_drop_div)) { 10512 /* Use your specific drop value (def 4/5 = 20%) */ 10513 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul; 10514 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div; 10515 } else { 10516 /* You get drop of 20% */ 10517 bbr->r_ctl.rc_bbr_hptsi_gain *= 4; 10518 bbr->r_ctl.rc_bbr_hptsi_gain /= 5; 10519 } 10520 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) { 10521 /* Reduce our gain again to the bottom */ 10522 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 10523 } 10524 bbr_log_exit_gain(bbr, cts, 4); 10525 /* 10526 * Extend out so we wait another 10527 * epoch before dropping again. 10528 */ 10529 bbr->r_ctl.gain_epoch = cts; 10530 } 10531 if (flight <= bbr->r_ctl.rc_target_at_state) { 10532 if (bbr_sub_drain_slam_cwnd && 10533 (bbr->rc_use_google == 0) && 10534 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10535 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10536 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10537 } 10538 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10539 bbr_log_exit_gain(bbr, cts, 3); 10540 } 10541 } else { 10542 /* Its a gain */ 10543 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) { 10544 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10545 goto change_state; 10546 } 10547 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) || 10548 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >= 10549 bbr->rc_tp->snd_wnd)) { 10550 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10551 bbr_log_exit_gain(bbr, cts, 2); 10552 } 10553 } 10554 /** 10555 * We fall through and return always one of two things has 10556 * occurred. 10557 * 1) We are still not at target 10558 * <or> 10559 * 2) We reached the target and set rc_bbr_state_atflight 10560 * which means we no longer hit this block 10561 * next time we are called. 10562 */ 10563 return; 10564 } 10565 change_state: 10566 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) 10567 return; 10568 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) { 10569 /* Less than a full time-period has passed */ 10570 return; 10571 } 10572 if (bbr->r_ctl.rc_level_state_extra && 10573 (bbr_state_val(bbr) > BBR_SUB_DRAIN) && 10574 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10575 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10576 /* Less than a full time-period + extra has passed */ 10577 return; 10578 } 10579 if (bbr_gain_gets_extra_too && 10580 bbr->r_ctl.rc_level_state_extra && 10581 (bbr_state_val(bbr) == BBR_SUB_GAIN) && 10582 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10583 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10584 /* Less than a full time-period + extra has passed */ 10585 return; 10586 } 10587 bbr_substate_change(bbr, cts, __LINE__, 1); 10588 } 10589 10590 static uint32_t 10591 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain) 10592 { 10593 uint32_t mss, tar; 10594 10595 if (bbr->rc_use_google) { 10596 /* Google just uses the cwnd target */ 10597 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain); 10598 } else { 10599 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 10600 bbr->r_ctl.rc_pace_max_segs); 10601 /* Get the base cwnd with gain rounded to a mss */ 10602 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr), 10603 gain), mss); 10604 /* Make sure it is within our min */ 10605 if (tar < get_min_cwnd(bbr)) 10606 return (get_min_cwnd(bbr)); 10607 } 10608 return (tar); 10609 } 10610 10611 static void 10612 bbr_set_state_target(struct tcp_bbr *bbr, int line) 10613 { 10614 uint32_t tar, meth; 10615 10616 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 10617 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 10618 /* Special case using old probe-rtt method */ 10619 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10620 meth = 1; 10621 } else { 10622 /* Non-probe-rtt case and reduced probe-rtt */ 10623 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 10624 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) { 10625 /* For gain cycle we use the hptsi gain */ 10626 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10627 meth = 2; 10628 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) { 10629 /* 10630 * If configured, or for google all other states 10631 * get BBR_UNIT. 10632 */ 10633 tar = bbr_get_a_state_target(bbr, BBR_UNIT); 10634 meth = 3; 10635 } else { 10636 /* 10637 * Or we set a target based on the pacing gain 10638 * for non-google mode and default (non-configured). 10639 * Note we don't set a target goal below drain (192). 10640 */ 10641 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) { 10642 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]); 10643 meth = 4; 10644 } else { 10645 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10646 meth = 5; 10647 } 10648 } 10649 } 10650 bbr_log_set_of_state_target(bbr, tar, line, meth); 10651 bbr->r_ctl.rc_target_at_state = tar; 10652 } 10653 10654 static void 10655 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 10656 { 10657 /* Change to probe_rtt */ 10658 uint32_t time_in; 10659 10660 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10661 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10662 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10663 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain 10664 + bbr->r_ctl.rc_delivered); 10665 /* Setup so we force feed the filter */ 10666 if (bbr->rc_use_google || bbr_probertt_sets_rtt) 10667 bbr->rc_prtt_set_ts = 1; 10668 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10669 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10670 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10671 } 10672 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0); 10673 bbr->r_ctl.rc_rtt_shrinks = cts; 10674 bbr->r_ctl.last_in_probertt = cts; 10675 bbr->r_ctl.rc_probertt_srttchktim = cts; 10676 bbr->r_ctl.rc_bbr_state_time = cts; 10677 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT; 10678 /* We need to force the filter to update */ 10679 10680 if ((bbr_sub_drain_slam_cwnd) && 10681 bbr->rc_hit_state_1 && 10682 (bbr->rc_use_google == 0) && 10683 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10684 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd) 10685 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10686 } else 10687 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10688 /* Update the lost */ 10689 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10690 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){ 10691 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */ 10692 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10693 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10694 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10695 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10696 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6); 10697 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd; 10698 } else { 10699 /* 10700 * We bring it down slowly by using a hptsi gain that is 10701 * probably 75%. This will slowly float down our outstanding 10702 * without tampering with the cwnd. 10703 */ 10704 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 10705 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10706 bbr_set_state_target(bbr, __LINE__); 10707 if (bbr_prtt_slam_cwnd && 10708 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 10709 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10710 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10711 } 10712 } 10713 if (ctf_flight_size(bbr->rc_tp, 10714 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 10715 bbr->r_ctl.rc_target_at_state) { 10716 /* We are at target */ 10717 bbr->r_ctl.rc_bbr_enters_probertt = cts; 10718 } else { 10719 /* We need to come down to reach target before our time begins */ 10720 bbr->r_ctl.rc_bbr_enters_probertt = 0; 10721 } 10722 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch; 10723 BBR_STAT_INC(bbr_enter_probertt); 10724 bbr_log_exit_gain(bbr, cts, 0); 10725 bbr_log_type_statechange(bbr, cts, line); 10726 } 10727 10728 static void 10729 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts) 10730 { 10731 /* 10732 * Sanity check on probe-rtt intervals. 10733 * In crazy situations where we are competing 10734 * against new-reno flows with huge buffers 10735 * our rtt-prop interval could come to dominate 10736 * things if we can't get through a full set 10737 * of cycles, we need to adjust it. 10738 */ 10739 if (bbr_can_adjust_probertt && 10740 (bbr->rc_use_google == 0)) { 10741 uint16_t val = 0; 10742 uint32_t cur_rttp, fval, newval, baseval; 10743 10744 /* Are we to small and go into probe-rtt to often? */ 10745 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1)); 10746 cur_rttp = roundup(baseval, USECS_IN_SECOND); 10747 fval = bbr_filter_len_sec * USECS_IN_SECOND; 10748 if (bbr_is_ratio == 0) { 10749 if (fval > bbr_rtt_probe_limit) 10750 newval = cur_rttp + (fval - bbr_rtt_probe_limit); 10751 else 10752 newval = cur_rttp; 10753 } else { 10754 int mul; 10755 10756 mul = fval / bbr_rtt_probe_limit; 10757 newval = cur_rttp * mul; 10758 } 10759 if (cur_rttp > bbr->r_ctl.rc_probertt_int) { 10760 bbr->r_ctl.rc_probertt_int = cur_rttp; 10761 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10762 val = 1; 10763 } else { 10764 /* 10765 * No adjustments were made 10766 * do we need to shrink it? 10767 */ 10768 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) { 10769 if (cur_rttp <= bbr_rtt_probe_limit) { 10770 /* 10771 * Things have calmed down lets 10772 * shrink all the way to default 10773 */ 10774 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10775 reset_time_small(&bbr->r_ctl.rc_rttprop, 10776 (bbr_filter_len_sec * USECS_IN_SECOND)); 10777 cur_rttp = bbr_rtt_probe_limit; 10778 newval = (bbr_filter_len_sec * USECS_IN_SECOND); 10779 val = 2; 10780 } else { 10781 /* 10782 * Well does some adjustment make sense? 10783 */ 10784 if (cur_rttp < bbr->r_ctl.rc_probertt_int) { 10785 /* We can reduce interval time some */ 10786 bbr->r_ctl.rc_probertt_int = cur_rttp; 10787 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10788 val = 3; 10789 } 10790 } 10791 } 10792 } 10793 if (val) 10794 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val); 10795 } 10796 } 10797 10798 static void 10799 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 10800 { 10801 /* Exit probe-rtt */ 10802 10803 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) { 10804 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10805 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10806 } 10807 bbr_log_exit_gain(bbr, cts, 1); 10808 bbr->rc_hit_state_1 = 0; 10809 bbr->r_ctl.rc_rtt_shrinks = cts; 10810 bbr->r_ctl.last_in_probertt = cts; 10811 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0); 10812 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10813 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, 10814 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 10815 bbr->r_ctl.rc_delivered); 10816 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10817 uint32_t time_in; 10818 10819 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10820 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10821 } 10822 if (bbr->rc_filled_pipe) { 10823 /* Switch to probe_bw */ 10824 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 10825 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 10826 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10827 bbr_substate_change(bbr, cts, __LINE__, 0); 10828 bbr_log_type_statechange(bbr, cts, __LINE__); 10829 } else { 10830 /* Back to startup */ 10831 bbr->rc_bbr_state = BBR_STATE_STARTUP; 10832 bbr->r_ctl.rc_bbr_state_time = cts; 10833 /* 10834 * We don't want to give a complete free 3 10835 * measurements until we exit, so we use 10836 * the number of pe's we were in probe-rtt 10837 * to add to the startup_epoch. That way 10838 * we will still retain the old state. 10839 */ 10840 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt); 10841 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10842 /* Make sure to use the lower pg when shifting back in */ 10843 if (bbr->r_ctl.rc_lost && 10844 bbr_use_lower_gain_in_startup && 10845 (bbr->rc_use_google == 0)) 10846 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10847 else 10848 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 10849 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 10850 /* Probably not needed but set it anyway */ 10851 bbr_set_state_target(bbr, __LINE__); 10852 bbr_log_type_statechange(bbr, cts, __LINE__); 10853 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10854 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0); 10855 } 10856 bbr_check_probe_rtt_limits(bbr, cts); 10857 } 10858 10859 static int32_t inline 10860 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts) 10861 { 10862 if ((bbr->rc_past_init_win == 1) && 10863 (bbr->rc_in_persist == 0) && 10864 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) { 10865 return (1); 10866 } 10867 if (bbr_can_force_probertt && 10868 (bbr->rc_in_persist == 0) && 10869 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 10870 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 10871 return (1); 10872 } 10873 return (0); 10874 } 10875 10876 static int32_t 10877 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch) 10878 { 10879 uint64_t btlbw, gain; 10880 if (pkt_epoch == 0) { 10881 /* 10882 * Need to be on a pkt-epoch to continue. 10883 */ 10884 return (0); 10885 } 10886 btlbw = bbr_get_full_bw(bbr); 10887 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10888 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10889 if (btlbw >= gain) { 10890 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 10891 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10892 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 10893 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10894 } 10895 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) 10896 return (1); 10897 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10898 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 10899 return(0); 10900 } 10901 10902 static int32_t inline 10903 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch) 10904 { 10905 /* Have we gained 25% in the last 3 packet based epoch's? */ 10906 uint64_t btlbw, gain; 10907 int do_exit; 10908 int delta, rtt_gain; 10909 10910 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 10911 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 10912 /* 10913 * This qualifies as a RTT_PROBE session since we drop the 10914 * data outstanding to nothing and waited more than 10915 * bbr_rtt_probe_time. 10916 */ 10917 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 10918 bbr_set_reduced_rtt(bbr, cts, __LINE__); 10919 } 10920 if (bbr_should_enter_probe_rtt(bbr, cts)) { 10921 bbr_enter_probe_rtt(bbr, cts, __LINE__); 10922 return (0); 10923 } 10924 if (bbr->rc_use_google) 10925 return (bbr_google_startup(bbr, cts, pkt_epoch)); 10926 10927 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 10928 (bbr_use_lower_gain_in_startup)) { 10929 /* Drop to a lower gain 1.5 x since we saw loss */ 10930 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10931 } 10932 if (pkt_epoch == 0) { 10933 /* 10934 * Need to be on a pkt-epoch to continue. 10935 */ 10936 return (0); 10937 } 10938 if (bbr_rtt_gain_thresh) { 10939 /* 10940 * Do we allow a flow to stay 10941 * in startup with no loss and no 10942 * gain in rtt over a set threshold? 10943 */ 10944 if (bbr->r_ctl.rc_pkt_epoch_rtt && 10945 bbr->r_ctl.startup_last_srtt && 10946 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) { 10947 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt; 10948 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt; 10949 } else 10950 rtt_gain = 0; 10951 if ((bbr->r_ctl.startup_last_srtt == 0) || 10952 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt)) 10953 /* First time or new lower value */ 10954 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 10955 10956 if ((bbr->r_ctl.rc_lost == 0) && 10957 (rtt_gain < bbr_rtt_gain_thresh)) { 10958 /* 10959 * No loss, and we are under 10960 * our gain threhold for 10961 * increasing RTT. 10962 */ 10963 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10964 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10965 bbr_log_startup_event(bbr, cts, rtt_gain, 10966 delta, bbr->r_ctl.startup_last_srtt, 10); 10967 return (0); 10968 } 10969 } 10970 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) && 10971 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) && 10972 (!IN_RECOVERY(bbr->rc_tp->t_flags))) { 10973 /* 10974 * We only assess if we have a new measurement when 10975 * we have no loss and are not in recovery. 10976 * Drag up by one our last_startup epoch so we will hold 10977 * the number of non-gain we have already accumulated. 10978 */ 10979 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10980 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10981 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10982 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9); 10983 return (0); 10984 } 10985 /* Case where we reduced the lost (bad retransmit) */ 10986 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost) 10987 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10988 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count; 10989 btlbw = bbr_get_full_bw(bbr); 10990 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower) 10991 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10992 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10993 else 10994 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10995 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10996 do_exit = 0; 10997 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw) 10998 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10999 if (btlbw >= gain) { 11000 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 11001 /* Update the lost so we won't exit in next set of tests */ 11002 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11003 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11004 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 11005 } 11006 if ((bbr->rc_loss_exit && 11007 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 11008 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) && 11009 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) { 11010 /* 11011 * If we had no gain, we had loss and that loss was above 11012 * our threshould, the rwnd is not constrained, and we have 11013 * had at least 3 packet epochs exit. Note that this is 11014 * switched off by sysctl. Google does not do this by the 11015 * way. 11016 */ 11017 if ((ctf_flight_size(bbr->rc_tp, 11018 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 11019 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) { 11020 do_exit = 1; 11021 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11022 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4); 11023 } else { 11024 /* Just record an updated loss value */ 11025 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11026 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11027 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5); 11028 } 11029 } else 11030 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11031 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) || 11032 do_exit) { 11033 /* Return 1 to exit the startup state. */ 11034 return (1); 11035 } 11036 /* Stay in startup */ 11037 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11038 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 11039 return (0); 11040 } 11041 11042 static void 11043 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses) 11044 { 11045 /* 11046 * A tick occurred in the rtt epoch do we need to do anything? 11047 */ 11048 #ifdef BBR_INVARIANTS 11049 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) && 11050 (bbr->rc_bbr_state != BBR_STATE_DRAIN) && 11051 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) && 11052 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 11053 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) { 11054 /* Debug code? */ 11055 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state); 11056 } 11057 #endif 11058 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 11059 /* Do we exit the startup state? */ 11060 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) { 11061 uint32_t time_in; 11062 11063 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11064 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6); 11065 bbr->rc_filled_pipe = 1; 11066 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11067 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11068 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11069 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11070 } else 11071 time_in = 0; 11072 if (bbr->rc_no_pacing) 11073 bbr->rc_no_pacing = 0; 11074 bbr->r_ctl.rc_bbr_state_time = cts; 11075 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg; 11076 bbr->rc_bbr_state = BBR_STATE_DRAIN; 11077 bbr_set_state_target(bbr, __LINE__); 11078 if ((bbr->rc_use_google == 0) && 11079 bbr_slam_cwnd_in_main_drain) { 11080 /* Here we don't have to worry about probe-rtt */ 11081 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 11082 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11083 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11084 } 11085 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 11086 bbr_log_type_statechange(bbr, cts, __LINE__); 11087 if (ctf_flight_size(bbr->rc_tp, 11088 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 11089 bbr->r_ctl.rc_target_at_state) { 11090 /* 11091 * Switch to probe_bw if we are already 11092 * there 11093 */ 11094 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11095 bbr_substate_change(bbr, cts, __LINE__, 0); 11096 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11097 bbr_log_type_statechange(bbr, cts, __LINE__); 11098 } 11099 } 11100 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) { 11101 uint32_t inflight; 11102 struct tcpcb *tp; 11103 11104 tp = bbr->rc_tp; 11105 inflight = ctf_flight_size(tp, 11106 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11107 if (inflight >= bbr->r_ctl.rc_target_at_state) { 11108 /* We have reached a flight of the cwnd target */ 11109 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11110 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11111 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 11112 bbr_set_state_target(bbr, __LINE__); 11113 /* 11114 * Rig it so we don't do anything crazy and 11115 * start fresh with a new randomization. 11116 */ 11117 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 11118 bbr->rc_bbr_substate = BBR_SUB_LEVEL6; 11119 bbr_substate_change(bbr, cts, __LINE__, 1); 11120 } 11121 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) { 11122 /* Has in-flight reached the bdp (or less)? */ 11123 uint32_t inflight; 11124 struct tcpcb *tp; 11125 11126 tp = bbr->rc_tp; 11127 inflight = ctf_flight_size(tp, 11128 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11129 if ((bbr->rc_use_google == 0) && 11130 bbr_slam_cwnd_in_main_drain && 11131 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11132 /* 11133 * Here we don't have to worry about probe-rtt 11134 * re-slam it, but keep it slammed down. 11135 */ 11136 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11137 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11138 } 11139 if (inflight <= bbr->r_ctl.rc_target_at_state) { 11140 /* We have drained */ 11141 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11142 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11143 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11144 uint32_t time_in; 11145 11146 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11147 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11148 } 11149 if ((bbr->rc_use_google == 0) && 11150 bbr_slam_cwnd_in_main_drain && 11151 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 11152 /* Restore the cwnd */ 11153 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 11154 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11155 } 11156 /* Setup probe-rtt has being done now RRS-HERE */ 11157 bbr->r_ctl.rc_rtt_shrinks = cts; 11158 bbr->r_ctl.last_in_probertt = cts; 11159 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0); 11160 /* Randomly pick a sub-state */ 11161 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11162 bbr_substate_change(bbr, cts, __LINE__, 0); 11163 bbr_log_type_statechange(bbr, cts, __LINE__); 11164 } 11165 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) { 11166 uint32_t flight; 11167 11168 flight = ctf_flight_size(bbr->rc_tp, 11169 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11170 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered); 11171 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) && 11172 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11173 /* 11174 * We must keep cwnd at the desired MSS. 11175 */ 11176 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 11177 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11178 } else if ((bbr_prtt_slam_cwnd) && 11179 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11180 /* Re-slam it */ 11181 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11182 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11183 } 11184 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) { 11185 /* Has outstanding reached our target? */ 11186 if (flight <= bbr->r_ctl.rc_target_at_state) { 11187 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0); 11188 bbr->r_ctl.rc_bbr_enters_probertt = cts; 11189 /* If time is exactly 0, be 1usec off */ 11190 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) 11191 bbr->r_ctl.rc_bbr_enters_probertt = 1; 11192 if (bbr->rc_use_google == 0) { 11193 /* 11194 * Restore any lowering that as occurred to 11195 * reach here 11196 */ 11197 if (bbr->r_ctl.bbr_rttprobe_gain_val) 11198 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 11199 else 11200 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11201 } 11202 } 11203 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) && 11204 (bbr->rc_use_google == 0) && 11205 bbr->r_ctl.bbr_rttprobe_gain_val && 11206 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) || 11207 (flight >= bbr->r_ctl.flightsize_at_drain))) { 11208 /* 11209 * We have doddled with our current hptsi 11210 * gain an srtt and have still not made it 11211 * to target, or we have increased our flight. 11212 * Lets reduce the gain by xx% 11213 * flooring the reduce at DRAIN (based on 11214 * mul/div) 11215 */ 11216 int red; 11217 11218 bbr->r_ctl.flightsize_at_drain = flight; 11219 bbr->r_ctl.rc_probertt_srttchktim = cts; 11220 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1); 11221 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) { 11222 /* Reduce our gain again */ 11223 bbr->r_ctl.rc_bbr_hptsi_gain -= red; 11224 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0); 11225 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) { 11226 /* one more chance before we give up */ 11227 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 11228 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0); 11229 } else { 11230 /* At the very bottom */ 11231 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1); 11232 } 11233 } 11234 } 11235 if (bbr->r_ctl.rc_bbr_enters_probertt && 11236 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) && 11237 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) { 11238 /* Time to exit probe RTT normally */ 11239 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts); 11240 } 11241 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 11242 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 11243 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 11244 /* 11245 * This qualifies as a RTT_PROBE session since we 11246 * drop the data outstanding to nothing and waited 11247 * more than bbr_rtt_probe_time. 11248 */ 11249 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 11250 bbr_set_reduced_rtt(bbr, cts, __LINE__); 11251 } 11252 if (bbr_should_enter_probe_rtt(bbr, cts)) { 11253 bbr_enter_probe_rtt(bbr, cts, __LINE__); 11254 } else { 11255 bbr_set_probebw_gains(bbr, cts, losses); 11256 } 11257 } 11258 } 11259 11260 static void 11261 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses) 11262 { 11263 int32_t epoch = 0; 11264 11265 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 11266 bbr_set_epoch(bbr, cts, line); 11267 /* At each epoch doe lt bw sampling */ 11268 epoch = 1; 11269 } 11270 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses); 11271 } 11272 11273 static int 11274 bbr_do_segment_nounlock(struct mbuf *m, struct tcphdr *th, struct socket *so, 11275 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, 11276 int32_t nxt_pkt, struct timeval *tv) 11277 { 11278 struct inpcb *inp = tptoinpcb(tp); 11279 int32_t thflags, retval; 11280 uint32_t cts, lcts; 11281 uint32_t tiwin; 11282 struct tcpopt to; 11283 struct tcp_bbr *bbr; 11284 struct bbr_sendmap *rsm; 11285 struct timeval ltv; 11286 int32_t did_out = 0; 11287 uint16_t nsegs; 11288 int32_t prev_state; 11289 uint32_t lost; 11290 11291 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11292 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11293 /* add in our stats */ 11294 kern_prefetch(bbr, &prev_state); 11295 prev_state = 0; 11296 thflags = tcp_get_flags(th); 11297 /* 11298 * If this is either a state-changing packet or current state isn't 11299 * established, we require a write lock on tcbinfo. Otherwise, we 11300 * allow the tcbinfo to be in either alocked or unlocked, as the 11301 * caller may have unnecessarily acquired a write lock due to a 11302 * race. 11303 */ 11304 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11305 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 11306 __func__)); 11307 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 11308 __func__)); 11309 11310 tp->t_rcvtime = ticks; 11311 /* 11312 * Unscale the window into a 32-bit value. For the SYN_SENT state 11313 * the scale is zero. 11314 */ 11315 tiwin = th->th_win << tp->snd_scale; 11316 #ifdef STATS 11317 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 11318 #endif 11319 11320 if (m->m_flags & M_TSTMP) { 11321 /* Prefer the hardware timestamp if present */ 11322 struct timespec ts; 11323 11324 mbuf_tstmp2timespec(m, &ts); 11325 bbr->rc_tv.tv_sec = ts.tv_sec; 11326 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11327 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11328 } else if (m->m_flags & M_TSTMP_LRO) { 11329 /* Next the arrival timestamp */ 11330 struct timespec ts; 11331 11332 mbuf_tstmp2timespec(m, &ts); 11333 bbr->rc_tv.tv_sec = ts.tv_sec; 11334 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11335 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11336 } else { 11337 /* 11338 * Ok just get the current time. 11339 */ 11340 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv); 11341 } 11342 /* 11343 * Parse options on any incoming segment. 11344 */ 11345 tcp_dooptions(&to, (u_char *)(th + 1), 11346 (th->th_off << 2) - sizeof(struct tcphdr), 11347 (thflags & TH_SYN) ? TO_SYN : 0); 11348 11349 /* 11350 * If timestamps were negotiated during SYN/ACK and a 11351 * segment without a timestamp is received, silently drop 11352 * the segment, unless it is a RST segment or missing timestamps are 11353 * tolerated. 11354 * See section 3.2 of RFC 7323. 11355 */ 11356 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 11357 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 11358 retval = 0; 11359 m_freem(m); 11360 goto done_with_input; 11361 } 11362 /* 11363 * If echoed timestamp is later than the current time, fall back to 11364 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 11365 * were used when this connection was established. 11366 */ 11367 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 11368 to.to_tsecr -= tp->ts_offset; 11369 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv))) 11370 to.to_tsecr = 0; 11371 } 11372 /* 11373 * If its the first time in we need to take care of options and 11374 * verify we can do SACK for rack! 11375 */ 11376 if (bbr->r_state == 0) { 11377 /* 11378 * Process options only when we get SYN/ACK back. The SYN 11379 * case for incoming connections is handled in tcp_syncache. 11380 * According to RFC1323 the window field in a SYN (i.e., a 11381 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 11382 * this is traditional behavior, may need to be cleaned up. 11383 */ 11384 if (bbr->rc_inp == NULL) { 11385 bbr->rc_inp = inp; 11386 } 11387 /* 11388 * We need to init rc_inp here since its not init'd when 11389 * bbr_init is called 11390 */ 11391 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 11392 if ((to.to_flags & TOF_SCALE) && 11393 (tp->t_flags & TF_REQ_SCALE)) { 11394 tp->t_flags |= TF_RCVD_SCALE; 11395 tp->snd_scale = to.to_wscale; 11396 } else 11397 tp->t_flags &= ~TF_REQ_SCALE; 11398 /* 11399 * Initial send window. It will be updated with the 11400 * next incoming segment to the scaled value. 11401 */ 11402 tp->snd_wnd = th->th_win; 11403 if ((to.to_flags & TOF_TS) && 11404 (tp->t_flags & TF_REQ_TSTMP)) { 11405 tp->t_flags |= TF_RCVD_TSTMP; 11406 tp->ts_recent = to.to_tsval; 11407 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 11408 } else 11409 tp->t_flags &= ~TF_REQ_TSTMP; 11410 if (to.to_flags & TOF_MSS) 11411 tcp_mss(tp, to.to_mss); 11412 if ((tp->t_flags & TF_SACK_PERMIT) && 11413 (to.to_flags & TOF_SACKPERM) == 0) 11414 tp->t_flags &= ~TF_SACK_PERMIT; 11415 if (IS_FASTOPEN(tp->t_flags)) { 11416 if (to.to_flags & TOF_FASTOPEN) { 11417 uint16_t mss; 11418 11419 if (to.to_flags & TOF_MSS) 11420 mss = to.to_mss; 11421 else 11422 if ((inp->inp_vflag & INP_IPV6) != 0) 11423 mss = TCP6_MSS; 11424 else 11425 mss = TCP_MSS; 11426 tcp_fastopen_update_cache(tp, mss, 11427 to.to_tfo_len, to.to_tfo_cookie); 11428 } else 11429 tcp_fastopen_disable_path(tp); 11430 } 11431 } 11432 /* 11433 * At this point we are at the initial call. Here we decide 11434 * if we are doing RACK or not. We do this by seeing if 11435 * TF_SACK_PERMIT is set, if not rack is *not* possible and 11436 * we switch to the default code. 11437 */ 11438 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 11439 /* Bail */ 11440 tcp_switch_back_to_default(tp); 11441 (*tp->t_fb->tfb_tcp_do_segment) (m, th, so, tp, drop_hdrlen, 11442 tlen, iptos); 11443 return (1); 11444 } 11445 /* Set the flag */ 11446 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 11447 tcp_set_hpts(inp); 11448 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack); 11449 } 11450 if (thflags & TH_ACK) { 11451 /* Track ack types */ 11452 if (to.to_flags & TOF_SACK) 11453 BBR_STAT_INC(bbr_acks_with_sacks); 11454 else 11455 BBR_STAT_INC(bbr_plain_acks); 11456 } 11457 /* 11458 * This is the one exception case where we set the rack state 11459 * always. All other times (timers etc) we must have a rack-state 11460 * set (so we assure we have done the checks above for SACK). 11461 */ 11462 if (thflags & TH_FIN) 11463 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 11464 if (bbr->r_state != tp->t_state) 11465 bbr_set_state(tp, bbr, tiwin); 11466 11467 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL) 11468 kern_prefetch(rsm, &prev_state); 11469 prev_state = bbr->r_state; 11470 bbr->rc_ack_was_delayed = 0; 11471 lost = bbr->r_ctl.rc_lost; 11472 bbr->rc_is_pkt_epoch_now = 0; 11473 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) { 11474 /* Get the real time into lcts and figure the real delay */ 11475 lcts = tcp_get_usecs(<v); 11476 if (TSTMP_GT(lcts, cts)) { 11477 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts; 11478 bbr->rc_ack_was_delayed = 1; 11479 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay, 11480 bbr->r_ctl.highest_hdwr_delay)) 11481 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay; 11482 } else { 11483 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11484 bbr->rc_ack_was_delayed = 0; 11485 } 11486 } else { 11487 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11488 bbr->rc_ack_was_delayed = 0; 11489 } 11490 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m); 11491 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 11492 retval = 0; 11493 m_freem(m); 11494 goto done_with_input; 11495 } 11496 /* 11497 * If a segment with the ACK-bit set arrives in the SYN-SENT state 11498 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 11499 */ 11500 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 11501 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 11502 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11503 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11504 return (1); 11505 } 11506 if (tiwin > bbr->r_ctl.rc_high_rwnd) 11507 bbr->r_ctl.rc_high_rwnd = tiwin; 11508 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp, 11509 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11510 bbr->rtt_valid = 0; 11511 if (to.to_flags & TOF_TS) { 11512 bbr->rc_ts_valid = 1; 11513 bbr->r_ctl.last_inbound_ts = to.to_tsval; 11514 } else { 11515 bbr->rc_ts_valid = 0; 11516 bbr->r_ctl.last_inbound_ts = 0; 11517 } 11518 retval = (*bbr->r_substate) (m, th, so, 11519 tp, &to, drop_hdrlen, 11520 tlen, tiwin, thflags, nxt_pkt, iptos); 11521 if (nxt_pkt == 0) 11522 BBR_STAT_INC(bbr_rlock_left_ret0); 11523 else 11524 BBR_STAT_INC(bbr_rlock_left_ret1); 11525 if (retval == 0) { 11526 /* 11527 * If retval is 1 the tcb is unlocked and most likely the tp 11528 * is gone. 11529 */ 11530 INP_WLOCK_ASSERT(inp); 11531 tcp_bbr_xmit_timer_commit(bbr, tp, cts); 11532 if (bbr->rc_is_pkt_epoch_now) 11533 bbr_set_pktepoch(bbr, cts, __LINE__); 11534 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost)); 11535 if (nxt_pkt == 0) { 11536 if (bbr->r_wanted_output != 0) { 11537 bbr->rc_output_starts_timer = 0; 11538 did_out = 1; 11539 if (tcp_output(tp) < 0) 11540 return (1); 11541 } else 11542 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0); 11543 } 11544 if ((nxt_pkt == 0) && 11545 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 11546 (SEQ_GT(tp->snd_max, tp->snd_una) || 11547 (tp->t_flags & TF_DELACK) || 11548 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 11549 (tp->t_state <= TCPS_CLOSING)))) { 11550 /* 11551 * We could not send (probably in the hpts but 11552 * stopped the timer)? 11553 */ 11554 if ((tp->snd_max == tp->snd_una) && 11555 ((tp->t_flags & TF_DELACK) == 0) && 11556 (tcp_in_hpts(bbr->rc_inp)) && 11557 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11558 /* 11559 * keep alive not needed if we are hptsi 11560 * output yet 11561 */ 11562 ; 11563 } else { 11564 if (tcp_in_hpts(bbr->rc_inp)) { 11565 tcp_hpts_remove(bbr->rc_inp); 11566 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11567 (TSTMP_GT(lcts, bbr->rc_pacer_started))) { 11568 uint32_t del; 11569 11570 del = lcts - bbr->rc_pacer_started; 11571 if (bbr->r_ctl.rc_last_delay_val > del) { 11572 BBR_STAT_INC(bbr_force_timer_start); 11573 bbr->r_ctl.rc_last_delay_val -= del; 11574 bbr->rc_pacer_started = lcts; 11575 } else { 11576 /* We are late */ 11577 bbr->r_ctl.rc_last_delay_val = 0; 11578 BBR_STAT_INC(bbr_force_output); 11579 if (tcp_output(tp) < 0) 11580 return (1); 11581 } 11582 } 11583 } 11584 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val, 11585 0); 11586 } 11587 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) { 11588 /* Do we have the correct timer running? */ 11589 bbr_timer_audit(tp, bbr, lcts, &so->so_snd); 11590 } 11591 /* Do we have a new state */ 11592 if (bbr->r_state != tp->t_state) 11593 bbr_set_state(tp, bbr, tiwin); 11594 done_with_input: 11595 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out); 11596 if (did_out) 11597 bbr->r_wanted_output = 0; 11598 } 11599 return (retval); 11600 } 11601 11602 static void 11603 bbr_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 11604 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 11605 { 11606 struct timeval tv; 11607 int retval; 11608 11609 /* First lets see if we have old packets */ 11610 if (tp->t_in_pkt) { 11611 if (ctf_do_queued_segments(so, tp, 1)) { 11612 m_freem(m); 11613 return; 11614 } 11615 } 11616 if (m->m_flags & M_TSTMP_LRO) { 11617 mbuf_tstmp2timeval(m, &tv); 11618 } else { 11619 /* Should not be should we kassert instead? */ 11620 tcp_get_usecs(&tv); 11621 } 11622 retval = bbr_do_segment_nounlock(m, th, so, tp, 11623 drop_hdrlen, tlen, iptos, 0, &tv); 11624 if (retval == 0) { 11625 INP_WUNLOCK(tptoinpcb(tp)); 11626 } 11627 } 11628 11629 /* 11630 * Return how much data can be sent without violating the 11631 * cwnd or rwnd. 11632 */ 11633 11634 static inline uint32_t 11635 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin, 11636 uint32_t avail, int32_t sb_offset, uint32_t cts) 11637 { 11638 uint32_t len; 11639 11640 if (ctf_outstanding(tp) >= tp->snd_wnd) { 11641 /* We never want to go over our peers rcv-window */ 11642 len = 0; 11643 } else { 11644 uint32_t flight; 11645 11646 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11647 if (flight >= sendwin) { 11648 /* 11649 * We have in flight what we are allowed by cwnd (if 11650 * it was rwnd blocking it would have hit above out 11651 * >= tp->snd_wnd). 11652 */ 11653 return (0); 11654 } 11655 len = sendwin - flight; 11656 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 11657 /* We would send too much (beyond the rwnd) */ 11658 len = tp->snd_wnd - ctf_outstanding(tp); 11659 } 11660 if ((len + sb_offset) > avail) { 11661 /* 11662 * We don't have that much in the SB, how much is 11663 * there? 11664 */ 11665 len = avail - sb_offset; 11666 } 11667 } 11668 return (len); 11669 } 11670 11671 static inline void 11672 bbr_do_error_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11673 { 11674 #ifdef NETFLIX_STATS 11675 KMOD_TCPSTAT_INC(tcps_sndpack_error); 11676 KMOD_TCPSTAT_ADD(tcps_sndbyte_error, len); 11677 #endif 11678 } 11679 11680 static inline void 11681 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11682 { 11683 if (error) { 11684 bbr_do_error_accounting(tp, bbr, rsm, len, error); 11685 return; 11686 } 11687 if (rsm) { 11688 if (rsm->r_flags & BBR_TLP) { 11689 /* 11690 * TLP should not count in retran count, but in its 11691 * own bin 11692 */ 11693 #ifdef NETFLIX_STATS 11694 KMOD_TCPSTAT_INC(tcps_tlpresends); 11695 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len); 11696 #endif 11697 } else { 11698 /* Retransmit */ 11699 tp->t_sndrexmitpack++; 11700 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 11701 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 11702 #ifdef STATS 11703 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 11704 len); 11705 #endif 11706 } 11707 /* 11708 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is 11709 * sub-state 11710 */ 11711 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len); 11712 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) { 11713 /* Non probe_bw log in 1, 2, or 4. */ 11714 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len); 11715 } else { 11716 /* 11717 * Log our probe state 3, and log also 5-13 to show 11718 * us the recovery sub-state for the send. This 11719 * means that 3 == (5+6+7+8+9+10+11+12+13) 11720 */ 11721 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len); 11722 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len); 11723 } 11724 /* Place in both 16's the totals of retransmitted */ 11725 counter_u64_add(bbr_state_lost[16], len); 11726 counter_u64_add(bbr_state_resend[16], len); 11727 /* Place in 17's the total sent */ 11728 counter_u64_add(bbr_state_resend[17], len); 11729 counter_u64_add(bbr_state_lost[17], len); 11730 11731 } else { 11732 /* New sends */ 11733 KMOD_TCPSTAT_INC(tcps_sndpack); 11734 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 11735 /* Place in 17's the total sent */ 11736 counter_u64_add(bbr_state_resend[17], len); 11737 counter_u64_add(bbr_state_lost[17], len); 11738 #ifdef STATS 11739 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 11740 len); 11741 #endif 11742 } 11743 } 11744 11745 static void 11746 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level) 11747 { 11748 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) { 11749 /* 11750 * Limit the cwnd to not be above N x the target plus whats 11751 * is outstanding. The target is based on the current b/w 11752 * estimate. 11753 */ 11754 uint32_t target; 11755 11756 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT); 11757 target += ctf_outstanding(tp); 11758 target *= bbr_target_cwnd_mult_limit; 11759 if (tp->snd_cwnd > target) 11760 tp->snd_cwnd = target; 11761 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__); 11762 } 11763 } 11764 11765 static int 11766 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg) 11767 { 11768 /* 11769 * "adv" is the amount we could increase the window, taking into 11770 * account that we are limited by TCP_MAXWIN << tp->rcv_scale. 11771 */ 11772 int32_t adv; 11773 int32_t oldwin; 11774 11775 adv = recwin; 11776 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 11777 oldwin = (tp->rcv_adv - tp->rcv_nxt); 11778 if (adv > oldwin) 11779 adv -= oldwin; 11780 else { 11781 /* We can't increase the window */ 11782 adv = 0; 11783 } 11784 } else 11785 oldwin = 0; 11786 11787 /* 11788 * If the new window size ends up being the same as or less 11789 * than the old size when it is scaled, then don't force 11790 * a window update. 11791 */ 11792 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 11793 return (0); 11794 11795 if (adv >= (2 * maxseg) && 11796 (adv >= (so->so_rcv.sb_hiwat / 4) || 11797 recwin <= (so->so_rcv.sb_hiwat / 8) || 11798 so->so_rcv.sb_hiwat <= 8 * maxseg)) { 11799 return (1); 11800 } 11801 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 11802 return (1); 11803 return (0); 11804 } 11805 11806 /* 11807 * Return 0 on success and a errno on failure to send. 11808 * Note that a 0 return may not mean we sent anything 11809 * if the TCB was on the hpts. A non-zero return 11810 * does indicate the error we got from ip[6]_output. 11811 */ 11812 static int 11813 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv) 11814 { 11815 struct socket *so; 11816 int32_t len; 11817 uint32_t cts; 11818 uint32_t recwin, sendwin; 11819 int32_t sb_offset; 11820 int32_t flags, abandon, error = 0; 11821 struct tcp_log_buffer *lgb = NULL; 11822 struct mbuf *m; 11823 struct mbuf *mb; 11824 uint32_t if_hw_tsomaxsegcount = 0; 11825 uint32_t if_hw_tsomaxsegsize = 0; 11826 uint32_t if_hw_tsomax = 0; 11827 struct ip *ip = NULL; 11828 struct tcp_bbr *bbr; 11829 struct tcphdr *th; 11830 struct udphdr *udp = NULL; 11831 u_char opt[TCP_MAXOLEN]; 11832 unsigned ipoptlen, optlen, hdrlen; 11833 unsigned ulen; 11834 uint32_t bbr_seq; 11835 uint32_t delay_calc=0; 11836 uint8_t doing_tlp = 0; 11837 uint8_t local_options; 11838 #ifdef BBR_INVARIANTS 11839 uint8_t doing_retran_from = 0; 11840 uint8_t picked_up_retran = 0; 11841 #endif 11842 uint8_t wanted_cookie = 0; 11843 uint8_t more_to_rxt=0; 11844 int32_t prefetch_so_done = 0; 11845 int32_t prefetch_rsm = 0; 11846 uint32_t tot_len = 0; 11847 uint32_t maxseg, pace_max_segs, p_maxseg; 11848 int32_t csum_flags = 0; 11849 int32_t hw_tls; 11850 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 11851 unsigned ipsec_optlen = 0; 11852 11853 #endif 11854 volatile int32_t sack_rxmit; 11855 struct bbr_sendmap *rsm = NULL; 11856 int32_t tso, mtu; 11857 struct tcpopt to; 11858 int32_t slot = 0; 11859 struct inpcb *inp; 11860 struct sockbuf *sb; 11861 uint32_t hpts_calling; 11862 #ifdef INET6 11863 struct ip6_hdr *ip6 = NULL; 11864 int32_t isipv6; 11865 #endif 11866 uint8_t app_limited = BBR_JR_SENT_DATA; 11867 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11868 /* We take a cache hit here */ 11869 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval)); 11870 cts = tcp_tv_to_usectick(&bbr->rc_tv); 11871 inp = bbr->rc_inp; 11872 so = inp->inp_socket; 11873 sb = &so->so_snd; 11874 if (tp->t_nic_ktls_xmit) 11875 hw_tls = 1; 11876 else 11877 hw_tls = 0; 11878 kern_prefetch(sb, &maxseg); 11879 maxseg = tp->t_maxseg - bbr->rc_last_options; 11880 if (bbr_minseg(bbr) < maxseg) { 11881 tcp_bbr_tso_size_check(bbr, cts); 11882 } 11883 /* Remove any flags that indicate we are pacing on the inp */ 11884 pace_max_segs = bbr->r_ctl.rc_pace_max_segs; 11885 p_maxseg = min(maxseg, pace_max_segs); 11886 INP_WLOCK_ASSERT(inp); 11887 #ifdef TCP_OFFLOAD 11888 if (tp->t_flags & TF_TOE) 11889 return (tcp_offload_output(tp)); 11890 #endif 11891 11892 #ifdef INET6 11893 if (bbr->r_state) { 11894 /* Use the cache line loaded if possible */ 11895 isipv6 = bbr->r_is_v6; 11896 } else { 11897 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 11898 } 11899 #endif 11900 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 11901 tcp_in_hpts(inp)) { 11902 /* 11903 * We are on the hpts for some timer but not hptsi output. 11904 * Possibly remove from the hpts so we can send/recv etc. 11905 */ 11906 if ((tp->t_flags & TF_ACKNOW) == 0) { 11907 /* 11908 * No immediate demand right now to send an ack, but 11909 * the user may have read, making room for new data 11910 * (a window update). If so we may want to cancel 11911 * whatever timer is running (KEEP/DEL-ACK?) and 11912 * continue to send out a window update. Or we may 11913 * have gotten more data into the socket buffer to 11914 * send. 11915 */ 11916 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 11917 (long)TCP_MAXWIN << tp->rcv_scale); 11918 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) && 11919 ((tcp_outflags[tp->t_state] & TH_RST) == 0) && 11920 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <= 11921 (tp->snd_max - tp->snd_una))) { 11922 /* 11923 * Nothing new to send and no window update 11924 * is needed to send. Lets just return and 11925 * let the timer-run off. 11926 */ 11927 return (0); 11928 } 11929 } 11930 tcp_hpts_remove(inp); 11931 bbr_timer_cancel(bbr, __LINE__, cts); 11932 } 11933 if (bbr->r_ctl.rc_last_delay_val) { 11934 /* Calculate a rough delay for early escape to sending */ 11935 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11936 delay_calc = cts - bbr->rc_pacer_started; 11937 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11938 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11939 else 11940 delay_calc = 0; 11941 } 11942 /* Mark that we have called bbr_output(). */ 11943 if ((bbr->r_timer_override) || 11944 (tp->t_state < TCPS_ESTABLISHED)) { 11945 /* Timeouts or early states are exempt */ 11946 if (tcp_in_hpts(inp)) 11947 tcp_hpts_remove(inp); 11948 } else if (tcp_in_hpts(inp)) { 11949 if ((bbr->r_ctl.rc_last_delay_val) && 11950 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11951 delay_calc) { 11952 /* 11953 * We were being paced for output and the delay has 11954 * already exceeded when we were supposed to be 11955 * called, lets go ahead and pull out of the hpts 11956 * and call output. 11957 */ 11958 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1); 11959 bbr->r_ctl.rc_last_delay_val = 0; 11960 tcp_hpts_remove(inp); 11961 } else if (tp->t_state == TCPS_CLOSED) { 11962 bbr->r_ctl.rc_last_delay_val = 0; 11963 tcp_hpts_remove(inp); 11964 } else { 11965 /* 11966 * On the hpts, you shall not pass! even if ACKNOW 11967 * is on, we will when the hpts fires, unless of 11968 * course we are overdue. 11969 */ 11970 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1); 11971 return (0); 11972 } 11973 } 11974 bbr->rc_cwnd_limited = 0; 11975 if (bbr->r_ctl.rc_last_delay_val) { 11976 /* recalculate the real delay and deal with over/under */ 11977 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11978 delay_calc = cts - bbr->rc_pacer_started; 11979 else 11980 delay_calc = 0; 11981 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11982 /* Setup the delay which will be added in */ 11983 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11984 else { 11985 /* 11986 * We are early setup to adjust 11987 * our slot time. 11988 */ 11989 uint64_t merged_val; 11990 11991 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc); 11992 bbr->r_agg_early_set = 1; 11993 if (bbr->r_ctl.rc_hptsi_agg_delay) { 11994 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) { 11995 /* Nope our previous late cancels out the early */ 11996 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early; 11997 bbr->r_agg_early_set = 0; 11998 bbr->r_ctl.rc_agg_early = 0; 11999 } else { 12000 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay; 12001 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12002 } 12003 } 12004 merged_val = bbr->rc_pacer_started; 12005 merged_val <<= 32; 12006 merged_val |= bbr->r_ctl.rc_last_delay_val; 12007 bbr_log_pacing_delay_calc(bbr, inp->inp_hpts_calls, 12008 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val, 12009 bbr->r_agg_early_set, 3); 12010 bbr->r_ctl.rc_last_delay_val = 0; 12011 BBR_STAT_INC(bbr_early); 12012 delay_calc = 0; 12013 } 12014 } else { 12015 /* We were not delayed due to hptsi */ 12016 if (bbr->r_agg_early_set) 12017 bbr->r_ctl.rc_agg_early = 0; 12018 bbr->r_agg_early_set = 0; 12019 delay_calc = 0; 12020 } 12021 if (delay_calc) { 12022 /* 12023 * We had a hptsi delay which means we are falling behind on 12024 * sending at the expected rate. Calculate an extra amount 12025 * of data we can send, if any, to put us back on track. 12026 */ 12027 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay) 12028 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff; 12029 else 12030 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc; 12031 } 12032 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12033 if ((tp->snd_una == tp->snd_max) && 12034 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 12035 (sbavail(sb))) { 12036 /* 12037 * Ok we have been idle with nothing outstanding 12038 * we possibly need to start fresh with either a new 12039 * suite of states or a fast-ramp up. 12040 */ 12041 bbr_restart_after_idle(bbr, 12042 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time)); 12043 } 12044 /* 12045 * Now was there a hptsi delay where we are behind? We only count 12046 * being behind if: a) We are not in recovery. b) There was a delay. 12047 * <and> c) We had room to send something. 12048 * 12049 */ 12050 hpts_calling = inp->inp_hpts_calls; 12051 inp->inp_hpts_calls = 0; 12052 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 12053 int retval; 12054 12055 retval = bbr_process_timers(tp, bbr, cts, hpts_calling); 12056 if (retval != 0) { 12057 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1); 12058 /* 12059 * If timers want tcp_drop(), then pass error out, 12060 * otherwise suppress it. 12061 */ 12062 return (retval < 0 ? retval : 0); 12063 } 12064 } 12065 bbr->rc_inp->inp_flags2 &= ~INP_MBUF_QUEUE_READY; 12066 if (hpts_calling && 12067 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 12068 bbr->r_ctl.rc_last_delay_val = 0; 12069 } 12070 bbr->r_timer_override = 0; 12071 bbr->r_wanted_output = 0; 12072 /* 12073 * For TFO connections in SYN_RECEIVED, only allow the initial 12074 * SYN|ACK and those sent by the retransmit timer. 12075 */ 12076 if (IS_FASTOPEN(tp->t_flags) && 12077 ((tp->t_state == TCPS_SYN_RECEIVED) || 12078 (tp->t_state == TCPS_SYN_SENT)) && 12079 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 12080 (tp->t_rxtshift == 0)) { /* not a retransmit */ 12081 len = 0; 12082 goto just_return_nolock; 12083 } 12084 /* 12085 * Before sending anything check for a state update. For hpts 12086 * calling without input this is important. If its input calling 12087 * then this was already done. 12088 */ 12089 if (bbr->rc_use_google == 0) 12090 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12091 again: 12092 /* 12093 * If we've recently taken a timeout, snd_max will be greater than 12094 * snd_max. BBR in general does not pay much attention to snd_nxt 12095 * for historic reasons the persist timer still uses it. This means 12096 * we have to look at it. All retransmissions that are not persits 12097 * use the rsm that needs to be sent so snd_nxt is ignored. At the 12098 * end of this routine we pull snd_nxt always up to snd_max. 12099 */ 12100 doing_tlp = 0; 12101 #ifdef BBR_INVARIANTS 12102 doing_retran_from = picked_up_retran = 0; 12103 #endif 12104 error = 0; 12105 tso = 0; 12106 slot = 0; 12107 mtu = 0; 12108 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12109 sb_offset = tp->snd_max - tp->snd_una; 12110 flags = tcp_outflags[tp->t_state]; 12111 sack_rxmit = 0; 12112 len = 0; 12113 rsm = NULL; 12114 if (flags & TH_RST) { 12115 SOCKBUF_LOCK(sb); 12116 goto send; 12117 } 12118 recheck_resend: 12119 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 12120 /* We need to always have one in reserve */ 12121 rsm = bbr_alloc(bbr); 12122 if (rsm == NULL) { 12123 error = ENOMEM; 12124 /* Lie to get on the hpts */ 12125 tot_len = tp->t_maxseg; 12126 if (hpts_calling) 12127 /* Retry in a ms */ 12128 slot = 1001; 12129 goto just_return_nolock; 12130 } 12131 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 12132 bbr->r_ctl.rc_free_cnt++; 12133 rsm = NULL; 12134 } 12135 /* What do we send, a resend? */ 12136 if (bbr->r_ctl.rc_resend == NULL) { 12137 /* Check for rack timeout */ 12138 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 12139 if (bbr->r_ctl.rc_resend) { 12140 #ifdef BBR_INVARIANTS 12141 picked_up_retran = 1; 12142 #endif 12143 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend); 12144 } 12145 } 12146 if (bbr->r_ctl.rc_resend) { 12147 rsm = bbr->r_ctl.rc_resend; 12148 #ifdef BBR_INVARIANTS 12149 doing_retran_from = 1; 12150 #endif 12151 /* Remove any TLP flags its a RACK or T-O */ 12152 rsm->r_flags &= ~BBR_TLP; 12153 bbr->r_ctl.rc_resend = NULL; 12154 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 12155 #ifdef BBR_INVARIANTS 12156 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n", 12157 tp, bbr, rsm, rsm->r_start, tp->snd_una); 12158 goto recheck_resend; 12159 #else 12160 /* TSNH */ 12161 rsm = NULL; 12162 goto recheck_resend; 12163 #endif 12164 } 12165 if (rsm->r_flags & BBR_HAS_SYN) { 12166 /* Only retransmit a SYN by itself */ 12167 len = 0; 12168 if ((flags & TH_SYN) == 0) { 12169 /* Huh something is wrong */ 12170 rsm->r_start++; 12171 if (rsm->r_start == rsm->r_end) { 12172 /* Clean it up, somehow we missed the ack? */ 12173 bbr_log_syn(tp, NULL); 12174 } else { 12175 /* TFO with data? */ 12176 rsm->r_flags &= ~BBR_HAS_SYN; 12177 len = rsm->r_end - rsm->r_start; 12178 } 12179 } else { 12180 /* Retransmitting SYN */ 12181 rsm = NULL; 12182 SOCKBUF_LOCK(sb); 12183 goto send; 12184 } 12185 } else 12186 len = rsm->r_end - rsm->r_start; 12187 if ((bbr->rc_resends_use_tso == 0) && 12188 (len > maxseg)) { 12189 len = maxseg; 12190 more_to_rxt = 1; 12191 } 12192 sb_offset = rsm->r_start - tp->snd_una; 12193 if (len > 0) { 12194 sack_rxmit = 1; 12195 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 12196 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 12197 min(len, maxseg)); 12198 } else { 12199 /* I dont think this can happen */ 12200 rsm = NULL; 12201 goto recheck_resend; 12202 } 12203 BBR_STAT_INC(bbr_resends_set); 12204 } else if (bbr->r_ctl.rc_tlp_send) { 12205 /* 12206 * Tail loss probe 12207 */ 12208 doing_tlp = 1; 12209 rsm = bbr->r_ctl.rc_tlp_send; 12210 bbr->r_ctl.rc_tlp_send = NULL; 12211 sack_rxmit = 1; 12212 len = rsm->r_end - rsm->r_start; 12213 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12214 len = maxseg; 12215 12216 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 12217 #ifdef BBR_INVARIANTS 12218 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u", 12219 tp, bbr, tp->snd_una, rsm, rsm->r_start); 12220 #else 12221 /* TSNH */ 12222 rsm = NULL; 12223 goto recheck_resend; 12224 #endif 12225 } 12226 sb_offset = rsm->r_start - tp->snd_una; 12227 BBR_STAT_INC(bbr_tlp_set); 12228 } 12229 /* 12230 * Enforce a connection sendmap count limit if set 12231 * as long as we are not retransmiting. 12232 */ 12233 if ((rsm == NULL) && 12234 (V_tcp_map_entries_limit > 0) && 12235 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 12236 BBR_STAT_INC(bbr_alloc_limited); 12237 if (!bbr->alloc_limit_reported) { 12238 bbr->alloc_limit_reported = 1; 12239 BBR_STAT_INC(bbr_alloc_limited_conns); 12240 } 12241 goto just_return_nolock; 12242 } 12243 #ifdef BBR_INVARIANTS 12244 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) { 12245 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u", 12246 tp, bbr, rsm, sb_offset, len); 12247 } 12248 #endif 12249 /* 12250 * Get standard flags, and add SYN or FIN if requested by 'hidden' 12251 * state flags. 12252 */ 12253 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL)) 12254 flags |= TH_FIN; 12255 if (tp->t_flags & TF_NEEDSYN) 12256 flags |= TH_SYN; 12257 12258 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) { 12259 /* we are retransmitting the fin */ 12260 len--; 12261 if (len) { 12262 /* 12263 * When retransmitting data do *not* include the 12264 * FIN. This could happen from a TLP probe if we 12265 * allowed data with a FIN. 12266 */ 12267 flags &= ~TH_FIN; 12268 } 12269 } else if (rsm) { 12270 if (flags & TH_FIN) 12271 flags &= ~TH_FIN; 12272 } 12273 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 12274 void *end_rsm; 12275 12276 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 12277 if (end_rsm) 12278 kern_prefetch(end_rsm, &prefetch_rsm); 12279 prefetch_rsm = 1; 12280 } 12281 SOCKBUF_LOCK(sb); 12282 /* 12283 * If snd_nxt == snd_max and we have transmitted a FIN, the 12284 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 12285 * negative length. This can also occur when TCP opens up its 12286 * congestion window while receiving additional duplicate acks after 12287 * fast-retransmit because TCP will reset snd_nxt to snd_max after 12288 * the fast-retransmit. 12289 * 12290 * In the normal retransmit-FIN-only case, however, snd_nxt will be 12291 * set to snd_una, the sb_offset will be 0, and the length may wind 12292 * up 0. 12293 * 12294 * If sack_rxmit is true we are retransmitting from the scoreboard 12295 * in which case len is already set. 12296 */ 12297 if (sack_rxmit == 0) { 12298 uint32_t avail; 12299 12300 avail = sbavail(sb); 12301 if (SEQ_GT(tp->snd_max, tp->snd_una)) 12302 sb_offset = tp->snd_max - tp->snd_una; 12303 else 12304 sb_offset = 0; 12305 if (bbr->rc_tlp_new_data) { 12306 /* TLP is forcing out new data */ 12307 uint32_t tlplen; 12308 12309 doing_tlp = 1; 12310 tlplen = maxseg; 12311 12312 if (tlplen > (uint32_t)(avail - sb_offset)) { 12313 tlplen = (uint32_t)(avail - sb_offset); 12314 } 12315 if (tlplen > tp->snd_wnd) { 12316 len = tp->snd_wnd; 12317 } else { 12318 len = tlplen; 12319 } 12320 bbr->rc_tlp_new_data = 0; 12321 } else { 12322 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts); 12323 if ((len < p_maxseg) && 12324 (bbr->rc_in_persist == 0) && 12325 (ctf_outstanding(tp) >= (2 * p_maxseg)) && 12326 ((avail - sb_offset) >= p_maxseg)) { 12327 /* 12328 * We are not completing whats in the socket 12329 * buffer (i.e. there is at least a segment 12330 * waiting to send) and we have 2 or more 12331 * segments outstanding. There is no sense 12332 * of sending a little piece. Lets defer and 12333 * and wait until we can send a whole 12334 * segment. 12335 */ 12336 len = 0; 12337 } 12338 if (bbr->rc_in_persist) { 12339 /* 12340 * We are in persists, figure out if 12341 * a retransmit is available (maybe the previous 12342 * persists we sent) or if we have to send new 12343 * data. 12344 */ 12345 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 12346 if (rsm) { 12347 len = rsm->r_end - rsm->r_start; 12348 if (rsm->r_flags & BBR_HAS_FIN) 12349 len--; 12350 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12351 len = maxseg; 12352 if (len > 1) 12353 BBR_STAT_INC(bbr_persist_reneg); 12354 /* 12355 * XXXrrs we could force the len to 12356 * 1 byte here to cause the chunk to 12357 * split apart.. but that would then 12358 * mean we always retransmit it as 12359 * one byte even after the window 12360 * opens. 12361 */ 12362 sack_rxmit = 1; 12363 sb_offset = rsm->r_start - tp->snd_una; 12364 } else { 12365 /* 12366 * First time through in persists or peer 12367 * acked our one byte. Though we do have 12368 * to have something in the sb. 12369 */ 12370 len = 1; 12371 sb_offset = 0; 12372 if (avail == 0) 12373 len = 0; 12374 } 12375 } 12376 } 12377 } 12378 if (prefetch_so_done == 0) { 12379 kern_prefetch(so, &prefetch_so_done); 12380 prefetch_so_done = 1; 12381 } 12382 /* 12383 * Lop off SYN bit if it has already been sent. However, if this is 12384 * SYN-SENT state and if segment contains data and if we don't know 12385 * that foreign host supports TAO, suppress sending segment. 12386 */ 12387 if ((flags & TH_SYN) && (rsm == NULL) && 12388 SEQ_GT(tp->snd_max, tp->snd_una)) { 12389 if (tp->t_state != TCPS_SYN_RECEIVED) 12390 flags &= ~TH_SYN; 12391 /* 12392 * When sending additional segments following a TFO SYN|ACK, 12393 * do not include the SYN bit. 12394 */ 12395 if (IS_FASTOPEN(tp->t_flags) && 12396 (tp->t_state == TCPS_SYN_RECEIVED)) 12397 flags &= ~TH_SYN; 12398 sb_offset--, len++; 12399 if (sbavail(sb) == 0) 12400 len = 0; 12401 } else if ((flags & TH_SYN) && rsm) { 12402 /* 12403 * Subtract one from the len for the SYN being 12404 * retransmitted. 12405 */ 12406 len--; 12407 } 12408 /* 12409 * Be careful not to send data and/or FIN on SYN segments. This 12410 * measure is needed to prevent interoperability problems with not 12411 * fully conformant TCP implementations. 12412 */ 12413 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 12414 len = 0; 12415 flags &= ~TH_FIN; 12416 } 12417 /* 12418 * On TFO sockets, ensure no data is sent in the following cases: 12419 * 12420 * - When retransmitting SYN|ACK on a passively-created socket 12421 * - When retransmitting SYN on an actively created socket 12422 * - When sending a zero-length cookie (cookie request) on an 12423 * actively created socket 12424 * - When the socket is in the CLOSED state (RST is being sent) 12425 */ 12426 if (IS_FASTOPEN(tp->t_flags) && 12427 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 12428 ((tp->t_state == TCPS_SYN_SENT) && 12429 (tp->t_tfo_client_cookie_len == 0)) || 12430 (flags & TH_RST))) { 12431 len = 0; 12432 sack_rxmit = 0; 12433 rsm = NULL; 12434 } 12435 /* Without fast-open there should never be data sent on a SYN */ 12436 if ((flags & TH_SYN) && (!IS_FASTOPEN(tp->t_flags))) 12437 len = 0; 12438 if (len <= 0) { 12439 /* 12440 * If FIN has been sent but not acked, but we haven't been 12441 * called to retransmit, len will be < 0. Otherwise, window 12442 * shrank after we sent into it. If window shrank to 0, 12443 * cancel pending retransmit, pull snd_nxt back to (closed) 12444 * window, and set the persist timer if it isn't already 12445 * going. If the window didn't close completely, just wait 12446 * for an ACK. 12447 * 12448 * We also do a general check here to ensure that we will 12449 * set the persist timer when we have data to send, but a 12450 * 0-byte window. This makes sure the persist timer is set 12451 * even if the packet hits one of the "goto send" lines 12452 * below. 12453 */ 12454 len = 0; 12455 if ((tp->snd_wnd == 0) && 12456 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12457 (tp->snd_una == tp->snd_max) && 12458 (sb_offset < (int)sbavail(sb))) { 12459 /* 12460 * Not enough room in the rwnd to send 12461 * a paced segment out. 12462 */ 12463 bbr_enter_persist(tp, bbr, cts, __LINE__); 12464 } 12465 } else if ((rsm == NULL) && 12466 (doing_tlp == 0) && 12467 (len < bbr->r_ctl.rc_pace_max_segs)) { 12468 /* 12469 * We are not sending a full segment for 12470 * some reason. Should we not send anything (think 12471 * sws or persists)? 12472 */ 12473 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12474 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12475 (len < (int)(sbavail(sb) - sb_offset))) { 12476 /* 12477 * Here the rwnd is less than 12478 * the pacing size, this is not a retransmit, 12479 * we are established and 12480 * the send is not the last in the socket buffer 12481 * lets not send, and possibly enter persists. 12482 */ 12483 len = 0; 12484 if (tp->snd_max == tp->snd_una) 12485 bbr_enter_persist(tp, bbr, cts, __LINE__); 12486 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) && 12487 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12488 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12489 (len < (int)(sbavail(sb) - sb_offset)) && 12490 (len < bbr_minseg(bbr))) { 12491 /* 12492 * Here we are not retransmitting, and 12493 * the cwnd is not so small that we could 12494 * not send at least a min size (rxt timer 12495 * not having gone off), We have 2 segments or 12496 * more already in flight, its not the tail end 12497 * of the socket buffer and the cwnd is blocking 12498 * us from sending out minimum pacing segment size. 12499 * Lets not send anything. 12500 */ 12501 bbr->rc_cwnd_limited = 1; 12502 len = 0; 12503 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 12504 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12505 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12506 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12507 (len < (int)(sbavail(sb) - sb_offset)) && 12508 (TCPS_HAVEESTABLISHED(tp->t_state))) { 12509 /* 12510 * Here we have a send window but we have 12511 * filled it up and we can't send another pacing segment. 12512 * We also have in flight more than 2 segments 12513 * and we are not completing the sb i.e. we allow 12514 * the last bytes of the sb to go out even if 12515 * its not a full pacing segment. 12516 */ 12517 len = 0; 12518 } 12519 } 12520 /* len will be >= 0 after this point. */ 12521 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 12522 tcp_sndbuf_autoscale(tp, so, sendwin); 12523 /* 12524 * 12525 */ 12526 if (bbr->rc_in_persist && 12527 len && 12528 (rsm == NULL) && 12529 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) { 12530 /* 12531 * We are in persist, not doing a retransmit and don't have enough space 12532 * yet to send a full TSO. So is it at the end of the sb 12533 * if so we need to send else nuke to 0 and don't send. 12534 */ 12535 int sbleft; 12536 if (sbavail(sb) > sb_offset) 12537 sbleft = sbavail(sb) - sb_offset; 12538 else 12539 sbleft = 0; 12540 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) { 12541 /* not at end of sb lets not send */ 12542 len = 0; 12543 } 12544 } 12545 /* 12546 * Decide if we can use TCP Segmentation Offloading (if supported by 12547 * hardware). 12548 * 12549 * TSO may only be used if we are in a pure bulk sending state. The 12550 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 12551 * options prevent using TSO. With TSO the TCP header is the same 12552 * (except for the sequence number) for all generated packets. This 12553 * makes it impossible to transmit any options which vary per 12554 * generated segment or packet. 12555 * 12556 * IPv4 handling has a clear separation of ip options and ip header 12557 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() 12558 * does the right thing below to provide length of just ip options 12559 * and thus checking for ipoptlen is enough to decide if ip options 12560 * are present. 12561 */ 12562 #ifdef INET6 12563 if (isipv6) 12564 ipoptlen = ip6_optlen(inp); 12565 else 12566 #endif 12567 if (inp->inp_options) 12568 ipoptlen = inp->inp_options->m_len - 12569 offsetof(struct ipoption, ipopt_list); 12570 else 12571 ipoptlen = 0; 12572 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12573 /* 12574 * Pre-calculate here as we save another lookup into the darknesses 12575 * of IPsec that way and can actually decide if TSO is ok. 12576 */ 12577 #ifdef INET6 12578 if (isipv6 && IPSEC_ENABLED(ipv6)) 12579 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 12580 #ifdef INET 12581 else 12582 #endif 12583 #endif /* INET6 */ 12584 #ifdef INET 12585 if (IPSEC_ENABLED(ipv4)) 12586 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 12587 #endif /* INET */ 12588 #endif /* IPSEC */ 12589 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12590 ipoptlen += ipsec_optlen; 12591 #endif 12592 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && 12593 (len > maxseg) && 12594 (tp->t_port == 0) && 12595 ((tp->t_flags & TF_SIGNATURE) == 0) && 12596 tp->rcv_numsacks == 0 && 12597 ipoptlen == 0) 12598 tso = 1; 12599 12600 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 12601 (long)TCP_MAXWIN << tp->rcv_scale); 12602 /* 12603 * Sender silly window avoidance. We transmit under the following 12604 * conditions when len is non-zero: 12605 * 12606 * - We have a full segment (or more with TSO) - This is the last 12607 * buffer in a write()/send() and we are either idle or running 12608 * NODELAY - we've timed out (e.g. persist timer) - we have more 12609 * then 1/2 the maximum send window's worth of data (receiver may be 12610 * limited the window size) - we need to retransmit 12611 */ 12612 if (rsm) 12613 goto send; 12614 if (len) { 12615 if (sack_rxmit) 12616 goto send; 12617 if (len >= p_maxseg) 12618 goto send; 12619 /* 12620 * NOTE! on localhost connections an 'ack' from the remote 12621 * end may occur synchronously with the output and cause us 12622 * to flush a buffer queued with moretocome. XXX 12623 * 12624 */ 12625 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */ 12626 ((tp->t_flags & TF_NODELAY) || 12627 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) && 12628 (tp->t_flags & TF_NOPUSH) == 0) { 12629 goto send; 12630 } 12631 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 12632 goto send; 12633 } 12634 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 12635 goto send; 12636 } 12637 } 12638 /* 12639 * Sending of standalone window updates. 12640 * 12641 * Window updates are important when we close our window due to a 12642 * full socket buffer and are opening it again after the application 12643 * reads data from it. Once the window has opened again and the 12644 * remote end starts to send again the ACK clock takes over and 12645 * provides the most current window information. 12646 * 12647 * We must avoid the silly window syndrome whereas every read from 12648 * the receive buffer, no matter how small, causes a window update 12649 * to be sent. We also should avoid sending a flurry of window 12650 * updates when the socket buffer had queued a lot of data and the 12651 * application is doing small reads. 12652 * 12653 * Prevent a flurry of pointless window updates by only sending an 12654 * update when we can increase the advertized window by more than 12655 * 1/4th of the socket buffer capacity. When the buffer is getting 12656 * full or is very small be more aggressive and send an update 12657 * whenever we can increase by two mss sized segments. In all other 12658 * situations the ACK's to new incoming data will carry further 12659 * window increases. 12660 * 12661 * Don't send an independent window update if a delayed ACK is 12662 * pending (it will get piggy-backed on it) or the remote side 12663 * already has done a half-close and won't send more data. Skip 12664 * this if the connection is in T/TCP half-open state. 12665 */ 12666 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 12667 !(tp->t_flags & TF_DELACK) && 12668 !TCPS_HAVERCVDFIN(tp->t_state)) { 12669 /* Check to see if we should do a window update */ 12670 if (bbr_window_update_needed(tp, so, recwin, maxseg)) 12671 goto send; 12672 } 12673 /* 12674 * Send if we owe the peer an ACK, RST, SYN. ACKNOW 12675 * is also a catch-all for the retransmit timer timeout case. 12676 */ 12677 if (tp->t_flags & TF_ACKNOW) { 12678 goto send; 12679 } 12680 if (flags & TH_RST) { 12681 /* Always send a RST if one is due */ 12682 goto send; 12683 } 12684 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) { 12685 goto send; 12686 } 12687 /* 12688 * If our state indicates that FIN should be sent and we have not 12689 * yet done so, then we need to send. 12690 */ 12691 if (flags & TH_FIN && 12692 ((tp->t_flags & TF_SENTFIN) == 0)) { 12693 goto send; 12694 } 12695 /* 12696 * No reason to send a segment, just return. 12697 */ 12698 just_return: 12699 SOCKBUF_UNLOCK(sb); 12700 just_return_nolock: 12701 if (tot_len) 12702 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 12703 if (bbr->rc_no_pacing) 12704 slot = 0; 12705 if (tot_len == 0) { 12706 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >= 12707 tp->snd_wnd) { 12708 BBR_STAT_INC(bbr_rwnd_limited); 12709 app_limited = BBR_JR_RWND_LIMITED; 12710 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12711 if ((bbr->rc_in_persist == 0) && 12712 TCPS_HAVEESTABLISHED(tp->t_state) && 12713 (tp->snd_max == tp->snd_una) && 12714 sbavail(&so->so_snd)) { 12715 /* No send window.. we must enter persist */ 12716 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 12717 } 12718 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 12719 BBR_STAT_INC(bbr_app_limited); 12720 app_limited = BBR_JR_APP_LIMITED; 12721 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12722 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12723 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) { 12724 BBR_STAT_INC(bbr_cwnd_limited); 12725 app_limited = BBR_JR_CWND_LIMITED; 12726 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12727 bbr->r_ctl.rc_lost_bytes))); 12728 bbr->rc_cwnd_limited = 1; 12729 } else { 12730 BBR_STAT_INC(bbr_app_limited); 12731 app_limited = BBR_JR_APP_LIMITED; 12732 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12733 } 12734 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12735 bbr->r_agg_early_set = 0; 12736 bbr->r_ctl.rc_agg_early = 0; 12737 bbr->r_ctl.rc_last_delay_val = 0; 12738 } else if (bbr->rc_use_google == 0) 12739 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12740 /* Are we app limited? */ 12741 if ((app_limited == BBR_JR_APP_LIMITED) || 12742 (app_limited == BBR_JR_RWND_LIMITED)) { 12743 /** 12744 * We are application limited. 12745 */ 12746 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12747 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered); 12748 } 12749 if (tot_len == 0) 12750 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1); 12751 /* Dont update the time if we did not send */ 12752 bbr->r_ctl.rc_last_delay_val = 0; 12753 bbr->rc_output_starts_timer = 1; 12754 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len); 12755 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len); 12756 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 12757 /* Make sure snd_nxt is drug up */ 12758 tp->snd_nxt = tp->snd_max; 12759 } 12760 return (error); 12761 12762 send: 12763 if (doing_tlp == 0) { 12764 /* 12765 * Data not a TLP, and its not the rxt firing. If it is the 12766 * rxt firing, we want to leave the tlp_in_progress flag on 12767 * so we don't send another TLP. It has to be a rack timer 12768 * or normal send (response to acked data) to clear the tlp 12769 * in progress flag. 12770 */ 12771 bbr->rc_tlp_in_progress = 0; 12772 bbr->rc_tlp_rtx_out = 0; 12773 } else { 12774 /* 12775 * Its a TLP. 12776 */ 12777 bbr->rc_tlp_in_progress = 1; 12778 } 12779 bbr_timer_cancel(bbr, __LINE__, cts); 12780 if (rsm == NULL) { 12781 if (sbused(sb) > 0) { 12782 /* 12783 * This is sub-optimal. We only send a stand alone 12784 * FIN on its own segment. 12785 */ 12786 if (flags & TH_FIN) { 12787 flags &= ~TH_FIN; 12788 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) { 12789 /* Lets not send this */ 12790 slot = 0; 12791 goto just_return; 12792 } 12793 } 12794 } 12795 } else { 12796 /* 12797 * We do *not* send a FIN on a retransmit if it has data. 12798 * The if clause here where len > 1 should never come true. 12799 */ 12800 if ((len > 0) && 12801 (((rsm->r_flags & BBR_HAS_FIN) == 0) && 12802 (flags & TH_FIN))) { 12803 flags &= ~TH_FIN; 12804 len--; 12805 } 12806 } 12807 SOCKBUF_LOCK_ASSERT(sb); 12808 if (len > 0) { 12809 if ((tp->snd_una == tp->snd_max) && 12810 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 12811 /* 12812 * This qualifies as a RTT_PROBE session since we 12813 * drop the data outstanding to nothing and waited 12814 * more than bbr_rtt_probe_time. 12815 */ 12816 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 12817 bbr_set_reduced_rtt(bbr, cts, __LINE__); 12818 } 12819 if (len >= maxseg) 12820 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 12821 else 12822 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 12823 } 12824 /* 12825 * Before ESTABLISHED, force sending of initial options unless TCP 12826 * set not to do any options. NOTE: we assume that the IP/TCP header 12827 * plus TCP options always fit in a single mbuf, leaving room for a 12828 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 12829 * + optlen <= MCLBYTES 12830 */ 12831 optlen = 0; 12832 #ifdef INET6 12833 if (isipv6) 12834 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12835 else 12836 #endif 12837 hdrlen = sizeof(struct tcpiphdr); 12838 12839 /* 12840 * Compute options for segment. We only have to care about SYN and 12841 * established connection segments. Options for SYN-ACK segments 12842 * are handled in TCP syncache. 12843 */ 12844 to.to_flags = 0; 12845 local_options = 0; 12846 if ((tp->t_flags & TF_NOOPT) == 0) { 12847 /* Maximum segment size. */ 12848 if (flags & TH_SYN) { 12849 to.to_mss = tcp_mssopt(&inp->inp_inc); 12850 if (tp->t_port) 12851 to.to_mss -= V_tcp_udp_tunneling_overhead; 12852 to.to_flags |= TOF_MSS; 12853 /* 12854 * On SYN or SYN|ACK transmits on TFO connections, 12855 * only include the TFO option if it is not a 12856 * retransmit, as the presence of the TFO option may 12857 * have caused the original SYN or SYN|ACK to have 12858 * been dropped by a middlebox. 12859 */ 12860 if (IS_FASTOPEN(tp->t_flags) && 12861 (tp->t_rxtshift == 0)) { 12862 if (tp->t_state == TCPS_SYN_RECEIVED) { 12863 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 12864 to.to_tfo_cookie = 12865 (u_int8_t *)&tp->t_tfo_cookie.server; 12866 to.to_flags |= TOF_FASTOPEN; 12867 wanted_cookie = 1; 12868 } else if (tp->t_state == TCPS_SYN_SENT) { 12869 to.to_tfo_len = 12870 tp->t_tfo_client_cookie_len; 12871 to.to_tfo_cookie = 12872 tp->t_tfo_cookie.client; 12873 to.to_flags |= TOF_FASTOPEN; 12874 wanted_cookie = 1; 12875 } 12876 } 12877 } 12878 /* Window scaling. */ 12879 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 12880 to.to_wscale = tp->request_r_scale; 12881 to.to_flags |= TOF_SCALE; 12882 } 12883 /* Timestamps. */ 12884 if ((tp->t_flags & TF_RCVD_TSTMP) || 12885 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 12886 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset; 12887 to.to_tsecr = tp->ts_recent; 12888 to.to_flags |= TOF_TS; 12889 local_options += TCPOLEN_TIMESTAMP + 2; 12890 } 12891 /* Set receive buffer autosizing timestamp. */ 12892 if (tp->rfbuf_ts == 0 && 12893 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 12894 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv); 12895 /* Selective ACK's. */ 12896 if (flags & TH_SYN) 12897 to.to_flags |= TOF_SACKPERM; 12898 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 12899 tp->rcv_numsacks > 0) { 12900 to.to_flags |= TOF_SACK; 12901 to.to_nsacks = tp->rcv_numsacks; 12902 to.to_sacks = (u_char *)tp->sackblks; 12903 } 12904 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 12905 /* TCP-MD5 (RFC2385). */ 12906 if (tp->t_flags & TF_SIGNATURE) 12907 to.to_flags |= TOF_SIGNATURE; 12908 #endif /* TCP_SIGNATURE */ 12909 12910 /* Processing the options. */ 12911 hdrlen += (optlen = tcp_addoptions(&to, opt)); 12912 /* 12913 * If we wanted a TFO option to be added, but it was unable 12914 * to fit, ensure no data is sent. 12915 */ 12916 if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 12917 !(to.to_flags & TOF_FASTOPEN)) 12918 len = 0; 12919 } 12920 if (tp->t_port) { 12921 if (V_tcp_udp_tunneling_port == 0) { 12922 /* The port was removed?? */ 12923 SOCKBUF_UNLOCK(&so->so_snd); 12924 return (EHOSTUNREACH); 12925 } 12926 hdrlen += sizeof(struct udphdr); 12927 } 12928 #ifdef INET6 12929 if (isipv6) 12930 ipoptlen = ip6_optlen(inp); 12931 else 12932 #endif 12933 if (inp->inp_options) 12934 ipoptlen = inp->inp_options->m_len - 12935 offsetof(struct ipoption, ipopt_list); 12936 else 12937 ipoptlen = 0; 12938 ipoptlen = 0; 12939 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12940 ipoptlen += ipsec_optlen; 12941 #endif 12942 if (bbr->rc_last_options != local_options) { 12943 /* 12944 * Cache the options length this generally does not change 12945 * on a connection. We use this to calculate TSO. 12946 */ 12947 bbr->rc_last_options = local_options; 12948 } 12949 maxseg = tp->t_maxseg - (ipoptlen + optlen); 12950 p_maxseg = min(maxseg, pace_max_segs); 12951 /* 12952 * Adjust data length if insertion of options will bump the packet 12953 * length beyond the t_maxseg length. Clear the FIN bit because we 12954 * cut off the tail of the segment. 12955 */ 12956 if (len > maxseg) { 12957 if (len != 0 && (flags & TH_FIN)) { 12958 flags &= ~TH_FIN; 12959 } 12960 if (tso) { 12961 uint32_t moff; 12962 int32_t max_len; 12963 12964 /* extract TSO information */ 12965 if_hw_tsomax = tp->t_tsomax; 12966 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 12967 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 12968 KASSERT(ipoptlen == 0, 12969 ("%s: TSO can't do IP options", __func__)); 12970 12971 /* 12972 * Check if we should limit by maximum payload 12973 * length: 12974 */ 12975 if (if_hw_tsomax != 0) { 12976 /* compute maximum TSO length */ 12977 max_len = (if_hw_tsomax - hdrlen - 12978 max_linkhdr); 12979 if (max_len <= 0) { 12980 len = 0; 12981 } else if (len > max_len) { 12982 len = max_len; 12983 } 12984 } 12985 /* 12986 * Prevent the last segment from being fractional 12987 * unless the send sockbuf can be emptied: 12988 */ 12989 if ((sb_offset + len) < sbavail(sb)) { 12990 moff = len % (uint32_t)maxseg; 12991 if (moff != 0) { 12992 len -= moff; 12993 } 12994 } 12995 /* 12996 * In case there are too many small fragments don't 12997 * use TSO: 12998 */ 12999 if (len <= maxseg) { 13000 len = maxseg; 13001 tso = 0; 13002 } 13003 } else { 13004 /* Not doing TSO */ 13005 if (optlen + ipoptlen >= tp->t_maxseg) { 13006 /* 13007 * Since we don't have enough space to put 13008 * the IP header chain and the TCP header in 13009 * one packet as required by RFC 7112, don't 13010 * send it. Also ensure that at least one 13011 * byte of the payload can be put into the 13012 * TCP segment. 13013 */ 13014 SOCKBUF_UNLOCK(&so->so_snd); 13015 error = EMSGSIZE; 13016 sack_rxmit = 0; 13017 goto out; 13018 } 13019 len = maxseg; 13020 } 13021 } else { 13022 /* Not doing TSO */ 13023 if_hw_tsomaxsegcount = 0; 13024 tso = 0; 13025 } 13026 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 13027 ("%s: len > IP_MAXPACKET", __func__)); 13028 #ifdef DIAGNOSTIC 13029 #ifdef INET6 13030 if (max_linkhdr + hdrlen > MCLBYTES) 13031 #else 13032 if (max_linkhdr + hdrlen > MHLEN) 13033 #endif 13034 panic("tcphdr too big"); 13035 #endif 13036 /* 13037 * This KASSERT is here to catch edge cases at a well defined place. 13038 * Before, those had triggered (random) panic conditions further 13039 * down. 13040 */ 13041 #ifdef BBR_INVARIANTS 13042 if (sack_rxmit) { 13043 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 13044 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u", 13045 rsm, tp, bbr, rsm->r_start, tp->snd_una); 13046 } 13047 } 13048 #endif 13049 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 13050 if ((len == 0) && 13051 (flags & TH_FIN) && 13052 (sbused(sb))) { 13053 /* 13054 * We have outstanding data, don't send a fin by itself!. 13055 */ 13056 slot = 0; 13057 goto just_return; 13058 } 13059 /* 13060 * Grab a header mbuf, attaching a copy of data to be transmitted, 13061 * and initialize the header from the template for sends on this 13062 * connection. 13063 */ 13064 if (len) { 13065 uint32_t moff; 13066 13067 /* 13068 * We place a limit on sending with hptsi. 13069 */ 13070 if ((rsm == NULL) && len > pace_max_segs) 13071 len = pace_max_segs; 13072 if (len <= maxseg) 13073 tso = 0; 13074 #ifdef INET6 13075 if (MHLEN < hdrlen + max_linkhdr) 13076 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13077 else 13078 #endif 13079 m = m_gethdr(M_NOWAIT, MT_DATA); 13080 13081 if (m == NULL) { 13082 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13083 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13084 SOCKBUF_UNLOCK(sb); 13085 error = ENOBUFS; 13086 sack_rxmit = 0; 13087 goto out; 13088 } 13089 m->m_data += max_linkhdr; 13090 m->m_len = hdrlen; 13091 /* 13092 * Start the m_copy functions from the closest mbuf to the 13093 * sb_offset in the socket buffer chain. 13094 */ 13095 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) { 13096 #ifdef BBR_INVARIANTS 13097 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) 13098 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u", 13099 tp, bbr, len, sb_offset, sbavail(sb), rsm, 13100 doing_retran_from, 13101 picked_up_retran, 13102 doing_tlp); 13103 13104 #endif 13105 /* 13106 * In this messed up situation we have two choices, 13107 * a) pretend the send worked, and just start timers 13108 * and what not (not good since that may lead us 13109 * back here a lot). <or> b) Send the lowest segment 13110 * in the map. <or> c) Drop the connection. Lets do 13111 * <b> which if it continues to happen will lead to 13112 * <c> via timeouts. 13113 */ 13114 BBR_STAT_INC(bbr_offset_recovery); 13115 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 13116 sb_offset = 0; 13117 if (rsm == NULL) { 13118 sack_rxmit = 0; 13119 len = sbavail(sb); 13120 } else { 13121 sack_rxmit = 1; 13122 if (rsm->r_start != tp->snd_una) { 13123 /* 13124 * Things are really messed up, <c> 13125 * is the only thing to do. 13126 */ 13127 BBR_STAT_INC(bbr_offset_drop); 13128 SOCKBUF_UNLOCK(sb); 13129 (void)m_free(m); 13130 return (-EFAULT); /* tcp_drop() */ 13131 } 13132 len = rsm->r_end - rsm->r_start; 13133 } 13134 if (len > sbavail(sb)) 13135 len = sbavail(sb); 13136 if (len > maxseg) 13137 len = maxseg; 13138 } 13139 mb = sbsndptr_noadv(sb, sb_offset, &moff); 13140 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 13141 m_copydata(mb, moff, (int)len, 13142 mtod(m, caddr_t)+hdrlen); 13143 if (rsm == NULL) 13144 sbsndptr_adv(sb, mb, len); 13145 m->m_len += len; 13146 } else { 13147 struct sockbuf *msb; 13148 13149 if (rsm) 13150 msb = NULL; 13151 else 13152 msb = sb; 13153 #ifdef BBR_INVARIANTS 13154 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) { 13155 if (rsm) { 13156 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u rsm:%p snd_una:%u rsm_start:%u flg:%x %u:%u:%u sr:%d ", 13157 tp, bbr, len, moff, 13158 sbavail(sb), rsm, 13159 tp->snd_una, rsm->r_flags, rsm->r_start, 13160 doing_retran_from, 13161 picked_up_retran, 13162 doing_tlp, sack_rxmit); 13163 } else { 13164 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u", 13165 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una); 13166 } 13167 } 13168 #endif 13169 m->m_next = tcp_m_copym( 13170 mb, moff, &len, 13171 if_hw_tsomaxsegcount, 13172 if_hw_tsomaxsegsize, msb, 13173 ((rsm == NULL) ? hw_tls : 0) 13174 #ifdef NETFLIX_COPY_ARGS 13175 , NULL, NULL 13176 #endif 13177 ); 13178 if (len <= maxseg) { 13179 /* 13180 * Must have ran out of mbufs for the copy 13181 * shorten it to no longer need tso. Lets 13182 * not put on sendalot since we are low on 13183 * mbufs. 13184 */ 13185 tso = 0; 13186 } 13187 if (m->m_next == NULL) { 13188 SOCKBUF_UNLOCK(sb); 13189 (void)m_free(m); 13190 error = ENOBUFS; 13191 sack_rxmit = 0; 13192 goto out; 13193 } 13194 } 13195 #ifdef BBR_INVARIANTS 13196 if (tso && len < maxseg) { 13197 panic("tp:%p tso on, but len:%d < maxseg:%d", 13198 tp, len, maxseg); 13199 } 13200 if (tso && if_hw_tsomaxsegcount) { 13201 int32_t seg_cnt = 0; 13202 struct mbuf *foo; 13203 13204 foo = m; 13205 while (foo) { 13206 seg_cnt++; 13207 foo = foo->m_next; 13208 } 13209 if (seg_cnt > if_hw_tsomaxsegcount) { 13210 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount); 13211 } 13212 } 13213 #endif 13214 /* 13215 * If we're sending everything we've got, set PUSH. (This 13216 * will keep happy those implementations which only give 13217 * data to the user when a buffer fills or a PUSH comes in.) 13218 */ 13219 if (sb_offset + len == sbused(sb) && 13220 sbused(sb) && 13221 !(flags & TH_SYN)) { 13222 flags |= TH_PUSH; 13223 } 13224 SOCKBUF_UNLOCK(sb); 13225 } else { 13226 SOCKBUF_UNLOCK(sb); 13227 if (tp->t_flags & TF_ACKNOW) 13228 KMOD_TCPSTAT_INC(tcps_sndacks); 13229 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 13230 KMOD_TCPSTAT_INC(tcps_sndctrl); 13231 else 13232 KMOD_TCPSTAT_INC(tcps_sndwinup); 13233 13234 m = m_gethdr(M_NOWAIT, MT_DATA); 13235 if (m == NULL) { 13236 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13237 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13238 error = ENOBUFS; 13239 /* Fudge the send time since we could not send */ 13240 sack_rxmit = 0; 13241 goto out; 13242 } 13243 #ifdef INET6 13244 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 13245 MHLEN >= hdrlen) { 13246 M_ALIGN(m, hdrlen); 13247 } else 13248 #endif 13249 m->m_data += max_linkhdr; 13250 m->m_len = hdrlen; 13251 } 13252 SOCKBUF_UNLOCK_ASSERT(sb); 13253 m->m_pkthdr.rcvif = (struct ifnet *)0; 13254 #ifdef MAC 13255 mac_inpcb_create_mbuf(inp, m); 13256 #endif 13257 #ifdef INET6 13258 if (isipv6) { 13259 ip6 = mtod(m, struct ip6_hdr *); 13260 if (tp->t_port) { 13261 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 13262 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13263 udp->uh_dport = tp->t_port; 13264 ulen = hdrlen + len - sizeof(struct ip6_hdr); 13265 udp->uh_ulen = htons(ulen); 13266 th = (struct tcphdr *)(udp + 1); 13267 } else { 13268 th = (struct tcphdr *)(ip6 + 1); 13269 } 13270 tcpip_fillheaders(inp, tp->t_port, ip6, th); 13271 } else 13272 #endif /* INET6 */ 13273 { 13274 ip = mtod(m, struct ip *); 13275 if (tp->t_port) { 13276 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 13277 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13278 udp->uh_dport = tp->t_port; 13279 ulen = hdrlen + len - sizeof(struct ip); 13280 udp->uh_ulen = htons(ulen); 13281 th = (struct tcphdr *)(udp + 1); 13282 } else { 13283 th = (struct tcphdr *)(ip + 1); 13284 } 13285 tcpip_fillheaders(inp, tp->t_port, ip, th); 13286 } 13287 /* 13288 * If we are doing retransmissions, then snd_nxt will not reflect 13289 * the first unsent octet. For ACK only packets, we do not want the 13290 * sequence number of the retransmitted packet, we want the sequence 13291 * number of the next unsent octet. So, if there is no data (and no 13292 * SYN or FIN), use snd_max instead of snd_nxt when filling in 13293 * ti_seq. But if we are in persist state, snd_max might reflect 13294 * one byte beyond the right edge of the window, so use snd_nxt in 13295 * that case, since we know we aren't doing a retransmission. 13296 * (retransmit and persist are mutually exclusive...) 13297 */ 13298 if (sack_rxmit == 0) { 13299 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) { 13300 /* New data (including new persists) */ 13301 th->th_seq = htonl(tp->snd_max); 13302 bbr_seq = tp->snd_max; 13303 } else if (flags & TH_SYN) { 13304 /* Syn's always send from iss */ 13305 th->th_seq = htonl(tp->iss); 13306 bbr_seq = tp->iss; 13307 } else if (flags & TH_FIN) { 13308 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) { 13309 /* 13310 * If we sent the fin already its 1 minus 13311 * snd_max 13312 */ 13313 th->th_seq = (htonl(tp->snd_max - 1)); 13314 bbr_seq = (tp->snd_max - 1); 13315 } else { 13316 /* First time FIN use snd_max */ 13317 th->th_seq = htonl(tp->snd_max); 13318 bbr_seq = tp->snd_max; 13319 } 13320 } else { 13321 /* 13322 * len == 0 and not persist we use snd_max, sending 13323 * an ack unless we have sent the fin then its 1 13324 * minus. 13325 */ 13326 /* 13327 * XXXRRS Question if we are in persists and we have 13328 * nothing outstanding to send and we have not sent 13329 * a FIN, we will send an ACK. In such a case it 13330 * might be better to send (tp->snd_una - 1) which 13331 * would force the peer to ack. 13332 */ 13333 if (tp->t_flags & TF_SENTFIN) { 13334 th->th_seq = htonl(tp->snd_max - 1); 13335 bbr_seq = (tp->snd_max - 1); 13336 } else { 13337 th->th_seq = htonl(tp->snd_max); 13338 bbr_seq = tp->snd_max; 13339 } 13340 } 13341 } else { 13342 /* All retransmits use the rsm to guide the send */ 13343 th->th_seq = htonl(rsm->r_start); 13344 bbr_seq = rsm->r_start; 13345 } 13346 th->th_ack = htonl(tp->rcv_nxt); 13347 if (optlen) { 13348 bcopy(opt, th + 1, optlen); 13349 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 13350 } 13351 tcp_set_flags(th, flags); 13352 /* 13353 * Calculate receive window. Don't shrink window, but avoid silly 13354 * window syndrome. 13355 */ 13356 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) && 13357 recwin < maxseg))) 13358 recwin = 0; 13359 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 13360 recwin < (tp->rcv_adv - tp->rcv_nxt)) 13361 recwin = (tp->rcv_adv - tp->rcv_nxt); 13362 if (recwin > TCP_MAXWIN << tp->rcv_scale) 13363 recwin = TCP_MAXWIN << tp->rcv_scale; 13364 13365 /* 13366 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 13367 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 13368 * handled in syncache. 13369 */ 13370 if (flags & TH_SYN) 13371 th->th_win = htons((u_short) 13372 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 13373 else { 13374 /* Avoid shrinking window with window scaling. */ 13375 recwin = roundup2(recwin, 1 << tp->rcv_scale); 13376 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13377 } 13378 /* 13379 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 13380 * window. This may cause the remote transmitter to stall. This 13381 * flag tells soreceive() to disable delayed acknowledgements when 13382 * draining the buffer. This can occur if the receiver is 13383 * attempting to read more data than can be buffered prior to 13384 * transmitting on the connection. 13385 */ 13386 if (th->th_win == 0) { 13387 tp->t_sndzerowin++; 13388 tp->t_flags |= TF_RXWIN0SENT; 13389 } else 13390 tp->t_flags &= ~TF_RXWIN0SENT; 13391 /* 13392 * We don't support urgent data, but drag along 13393 * the pointer in case of a stack switch. 13394 */ 13395 tp->snd_up = tp->snd_una; 13396 13397 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 13398 if (to.to_flags & TOF_SIGNATURE) { 13399 /* 13400 * Calculate MD5 signature and put it into the place 13401 * determined before. NOTE: since TCP options buffer doesn't 13402 * point into mbuf's data, calculate offset and use it. 13403 */ 13404 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 13405 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 13406 /* 13407 * Do not send segment if the calculation of MD5 13408 * digest has failed. 13409 */ 13410 goto out; 13411 } 13412 } 13413 #endif 13414 13415 /* 13416 * Put TCP length in extended header, and then checksum extended 13417 * header and data. 13418 */ 13419 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 13420 #ifdef INET6 13421 if (isipv6) { 13422 /* 13423 * ip6_plen is not need to be filled now, and will be filled 13424 * in ip6_output. 13425 */ 13426 if (tp->t_port) { 13427 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13428 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13429 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13430 th->th_sum = htons(0); 13431 UDPSTAT_INC(udps_opackets); 13432 } else { 13433 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13434 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13435 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 13436 optlen + len, IPPROTO_TCP, 0); 13437 } 13438 } 13439 #endif 13440 #if defined(INET6) && defined(INET) 13441 else 13442 #endif 13443 #ifdef INET 13444 { 13445 if (tp->t_port) { 13446 m->m_pkthdr.csum_flags = CSUM_UDP; 13447 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13448 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13449 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13450 th->th_sum = htons(0); 13451 UDPSTAT_INC(udps_opackets); 13452 } else { 13453 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP; 13454 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13455 th->th_sum = in_pseudo(ip->ip_src.s_addr, 13456 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13457 IPPROTO_TCP + len + optlen)); 13458 } 13459 /* IP version must be set here for ipv4/ipv6 checking later */ 13460 KASSERT(ip->ip_v == IPVERSION, 13461 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 13462 } 13463 #endif 13464 13465 /* 13466 * Enable TSO and specify the size of the segments. The TCP pseudo 13467 * header checksum is always provided. XXX: Fixme: This is currently 13468 * not the case for IPv6. 13469 */ 13470 if (tso) { 13471 KASSERT(len > maxseg, 13472 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg)); 13473 m->m_pkthdr.csum_flags |= CSUM_TSO; 13474 csum_flags |= CSUM_TSO; 13475 m->m_pkthdr.tso_segsz = maxseg; 13476 } 13477 KASSERT(len + hdrlen == m_length(m, NULL), 13478 ("%s: mbuf chain different than expected: %d + %u != %u", 13479 __func__, len, hdrlen, m_length(m, NULL))); 13480 13481 #ifdef TCP_HHOOK 13482 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */ 13483 hhook_run_tcp_est_out(tp, th, &to, len, tso); 13484 #endif 13485 13486 /* Log to the black box */ 13487 if (tcp_bblogging_on(tp)) { 13488 union tcp_log_stackspecific log; 13489 13490 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 13491 /* Record info on type of transmission */ 13492 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay; 13493 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3); 13494 log.u_bbr.flex3 = maxseg; 13495 log.u_bbr.flex4 = delay_calc; 13496 log.u_bbr.flex5 = bbr->rc_past_init_win; 13497 log.u_bbr.flex5 <<= 1; 13498 log.u_bbr.flex5 |= bbr->rc_no_pacing; 13499 log.u_bbr.flex5 <<= 29; 13500 log.u_bbr.flex5 |= tp->t_maxseg; 13501 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs; 13502 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr); 13503 /* lets poke in the low and the high here for debugging */ 13504 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 13505 if (rsm || sack_rxmit) { 13506 if (doing_tlp) 13507 log.u_bbr.flex8 = 2; 13508 else 13509 log.u_bbr.flex8 = 1; 13510 } else { 13511 log.u_bbr.flex8 = 0; 13512 } 13513 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 13514 len, &log, false, NULL, NULL, 0, tv); 13515 } else { 13516 lgb = NULL; 13517 } 13518 /* 13519 * Fill in IP length and desired time to live and send to IP level. 13520 * There should be a better way to handle ttl and tos; we could keep 13521 * them in the template, but need a way to checksum without them. 13522 */ 13523 /* 13524 * m->m_pkthdr.len should have been set before cksum calcuration, 13525 * because in6_cksum() need it. 13526 */ 13527 #ifdef INET6 13528 if (isipv6) { 13529 /* 13530 * we separately set hoplimit for every segment, since the 13531 * user might want to change the value via setsockopt. Also, 13532 * desired default hop limit might be changed via Neighbor 13533 * Discovery. 13534 */ 13535 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 13536 13537 /* 13538 * Set the packet size here for the benefit of DTrace 13539 * probes. ip6_output() will set it properly; it's supposed 13540 * to include the option header lengths as well. 13541 */ 13542 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 13543 13544 if (V_path_mtu_discovery && maxseg > V_tcp_minmss) 13545 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13546 else 13547 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13548 13549 if (tp->t_state == TCPS_SYN_SENT) 13550 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 13551 13552 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 13553 /* TODO: IPv6 IP6TOS_ECT bit on */ 13554 error = ip6_output(m, inp->in6p_outputopts, 13555 &inp->inp_route6, 13556 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 13557 NULL, NULL, inp); 13558 13559 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 13560 mtu = inp->inp_route6.ro_nh->nh_mtu; 13561 } 13562 #endif /* INET6 */ 13563 #if defined(INET) && defined(INET6) 13564 else 13565 #endif 13566 #ifdef INET 13567 { 13568 ip->ip_len = htons(m->m_pkthdr.len); 13569 #ifdef INET6 13570 if (isipv6) 13571 ip->ip_ttl = in6_selecthlim(inp, NULL); 13572 #endif /* INET6 */ 13573 /* 13574 * If we do path MTU discovery, then we set DF on every 13575 * packet. This might not be the best thing to do according 13576 * to RFC3390 Section 2. However the tcp hostcache migitates 13577 * the problem so it affects only the first tcp connection 13578 * with a host. 13579 * 13580 * NB: Don't set DF on small MTU/MSS to have a safe 13581 * fallback. 13582 */ 13583 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 13584 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13585 if (tp->t_port == 0 || len < V_tcp_minmss) { 13586 ip->ip_off |= htons(IP_DF); 13587 } 13588 } else { 13589 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13590 } 13591 13592 if (tp->t_state == TCPS_SYN_SENT) 13593 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 13594 13595 TCP_PROBE5(send, NULL, tp, ip, tp, th); 13596 13597 error = ip_output(m, inp->inp_options, &inp->inp_route, 13598 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 13599 inp); 13600 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 13601 mtu = inp->inp_route.ro_nh->nh_mtu; 13602 } 13603 #endif /* INET */ 13604 out: 13605 13606 if (lgb) { 13607 lgb->tlb_errno = error; 13608 lgb = NULL; 13609 } 13610 /* 13611 * In transmit state, time the transmission and arrange for the 13612 * retransmit. In persist state, just set snd_max. 13613 */ 13614 if (error == 0) { 13615 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 13616 if (TCPS_HAVEESTABLISHED(tp->t_state) && 13617 (tp->t_flags & TF_SACK_PERMIT) && 13618 tp->rcv_numsacks > 0) 13619 tcp_clean_dsack_blocks(tp); 13620 /* We sent an ack clear the bbr_segs_rcvd count */ 13621 bbr->output_error_seen = 0; 13622 bbr->oerror_cnt = 0; 13623 bbr->bbr_segs_rcvd = 0; 13624 if (len == 0) 13625 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1); 13626 /* Do accounting for new sends */ 13627 if ((len > 0) && (rsm == NULL)) { 13628 int idx; 13629 if (tp->snd_una == tp->snd_max) { 13630 /* 13631 * Special case to match google, when 13632 * nothing is in flight the delivered 13633 * time does get updated to the current 13634 * time (see tcp_rate_bsd.c). 13635 */ 13636 bbr->r_ctl.rc_del_time = cts; 13637 } 13638 if (len >= maxseg) { 13639 idx = (len / maxseg) + 3; 13640 if (idx >= TCP_MSS_ACCT_ATIMER) 13641 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1); 13642 else 13643 counter_u64_add(bbr_out_size[idx], 1); 13644 } else { 13645 /* smaller than a MSS */ 13646 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options); 13647 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV) 13648 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1); 13649 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1); 13650 } 13651 } 13652 } 13653 abandon = 0; 13654 /* 13655 * We must do the send accounting before we log the output, 13656 * otherwise the state of the rsm could change and we account to the 13657 * wrong bucket. 13658 */ 13659 if (len > 0) { 13660 bbr_do_send_accounting(tp, bbr, rsm, len, error); 13661 if (error == 0) { 13662 if (tp->snd_una == tp->snd_max) 13663 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 13664 } 13665 } 13666 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error, 13667 cts, mb, &abandon, rsm, 0, sb); 13668 if (abandon) { 13669 /* 13670 * If bbr_log_output destroys the TCB or sees a TH_RST being 13671 * sent we should hit this condition. 13672 */ 13673 return (0); 13674 } 13675 if (bbr->rc_in_persist == 0) { 13676 /* 13677 * Advance snd_nxt over sequence space of this segment. 13678 */ 13679 if (error) 13680 /* We don't log or do anything with errors */ 13681 goto skip_upd; 13682 13683 if (tp->snd_una == tp->snd_max && 13684 (len || (flags & (TH_SYN | TH_FIN)))) { 13685 /* 13686 * Update the time we just added data since none was 13687 * outstanding. 13688 */ 13689 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13690 bbr->rc_tp->t_acktime = ticks; 13691 } 13692 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) { 13693 if (flags & TH_SYN) { 13694 /* 13695 * Smack the snd_max to iss + 1 13696 * if its a FO we will add len below. 13697 */ 13698 tp->snd_max = tp->iss + 1; 13699 } 13700 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13701 tp->snd_max++; 13702 tp->t_flags |= TF_SENTFIN; 13703 } 13704 } 13705 if (sack_rxmit == 0) 13706 tp->snd_max += len; 13707 skip_upd: 13708 if ((error == 0) && len) 13709 tot_len += len; 13710 } else { 13711 /* Persists case */ 13712 int32_t xlen = len; 13713 13714 if (error) 13715 goto nomore; 13716 13717 if (flags & TH_SYN) 13718 ++xlen; 13719 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13720 ++xlen; 13721 tp->t_flags |= TF_SENTFIN; 13722 } 13723 if (xlen && (tp->snd_una == tp->snd_max)) { 13724 /* 13725 * Update the time we just added data since none was 13726 * outstanding. 13727 */ 13728 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13729 bbr->rc_tp->t_acktime = ticks; 13730 } 13731 if (sack_rxmit == 0) 13732 tp->snd_max += xlen; 13733 tot_len += (len + optlen + ipoptlen); 13734 } 13735 nomore: 13736 if (error) { 13737 /* 13738 * Failures do not advance the seq counter above. For the 13739 * case of ENOBUFS we will fall out and become ack-clocked. 13740 * capping the cwnd at the current flight. 13741 * Everything else will just have to retransmit with the timer 13742 * (no pacer). 13743 */ 13744 SOCKBUF_UNLOCK_ASSERT(sb); 13745 BBR_STAT_INC(bbr_saw_oerr); 13746 /* Clear all delay/early tracks */ 13747 bbr->r_ctl.rc_hptsi_agg_delay = 0; 13748 bbr->r_ctl.rc_agg_early = 0; 13749 bbr->r_agg_early_set = 0; 13750 bbr->output_error_seen = 1; 13751 if (bbr->oerror_cnt < 0xf) 13752 bbr->oerror_cnt++; 13753 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) { 13754 /* drop the session */ 13755 return (-ENETDOWN); 13756 } 13757 switch (error) { 13758 case ENOBUFS: 13759 /* 13760 * Make this guy have to get ack's to send 13761 * more but lets make sure we don't 13762 * slam him below a T-O (1MSS). 13763 */ 13764 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 13765 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13766 bbr->r_ctl.rc_lost_bytes)) - maxseg; 13767 if (tp->snd_cwnd < maxseg) 13768 tp->snd_cwnd = maxseg; 13769 } 13770 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt; 13771 BBR_STAT_INC(bbr_saw_enobuf); 13772 if (bbr->bbr_hdrw_pacing) 13773 counter_u64_add(bbr_hdwr_pacing_enobuf, 1); 13774 else 13775 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1); 13776 /* 13777 * Here even in the enobuf's case we want to do our 13778 * state update. The reason being we may have been 13779 * called by the input function. If so we have had 13780 * things change. 13781 */ 13782 error = 0; 13783 goto enobufs; 13784 case EMSGSIZE: 13785 /* 13786 * For some reason the interface we used initially 13787 * to send segments changed to another or lowered 13788 * its MTU. If TSO was active we either got an 13789 * interface without TSO capabilits or TSO was 13790 * turned off. If we obtained mtu from ip_output() 13791 * then update it and try again. 13792 */ 13793 /* Turn on tracing (or try to) */ 13794 { 13795 int old_maxseg; 13796 13797 old_maxseg = tp->t_maxseg; 13798 BBR_STAT_INC(bbr_saw_emsgsiz); 13799 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts); 13800 if (mtu != 0) 13801 tcp_mss_update(tp, -1, mtu, NULL, NULL); 13802 if (old_maxseg <= tp->t_maxseg) { 13803 /* Huh it did not shrink? */ 13804 tp->t_maxseg = old_maxseg - 40; 13805 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts); 13806 } 13807 /* 13808 * Nuke all other things that can interfere 13809 * with slot 13810 */ 13811 if ((tot_len + len) && (len >= tp->t_maxseg)) { 13812 slot = bbr_get_pacing_delay(bbr, 13813 bbr->r_ctl.rc_bbr_hptsi_gain, 13814 (tot_len + len), cts, 0); 13815 if (slot < bbr_error_base_paceout) 13816 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13817 } else 13818 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13819 bbr->rc_output_starts_timer = 1; 13820 bbr_start_hpts_timer(bbr, tp, cts, 10, slot, 13821 tot_len); 13822 return (error); 13823 } 13824 case EPERM: 13825 tp->t_softerror = error; 13826 /* Fall through */ 13827 case EHOSTDOWN: 13828 case EHOSTUNREACH: 13829 case ENETDOWN: 13830 case ENETUNREACH: 13831 if (TCPS_HAVERCVDSYN(tp->t_state)) { 13832 tp->t_softerror = error; 13833 } 13834 /* FALLTHROUGH */ 13835 default: 13836 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt; 13837 bbr->rc_output_starts_timer = 1; 13838 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0); 13839 return (error); 13840 } 13841 #ifdef STATS 13842 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) && 13843 len && 13844 (rsm == NULL) && 13845 (bbr->rc_in_persist == 0)) { 13846 tp->gput_seq = bbr_seq; 13847 tp->gput_ack = bbr_seq + 13848 min(sbavail(&so->so_snd) - sb_offset, sendwin); 13849 tp->gput_ts = cts; 13850 tp->t_flags |= TF_GPUTINPROG; 13851 #endif 13852 } 13853 KMOD_TCPSTAT_INC(tcps_sndtotal); 13854 if ((bbr->bbr_hdw_pace_ena) && 13855 (bbr->bbr_attempt_hdwr_pace == 0) && 13856 (bbr->rc_past_init_win) && 13857 (bbr->rc_bbr_state != BBR_STATE_STARTUP) && 13858 (get_filter_value(&bbr->r_ctl.rc_delrate)) && 13859 (inp->inp_route.ro_nh && 13860 inp->inp_route.ro_nh->nh_ifp)) { 13861 /* 13862 * We are past the initial window and 13863 * have at least one measurement so we 13864 * could use hardware pacing if its available. 13865 * We have an interface and we have not attempted 13866 * to setup hardware pacing, lets try to now. 13867 */ 13868 uint64_t rate_wanted; 13869 int err = 0; 13870 13871 rate_wanted = bbr_get_hardware_rate(bbr); 13872 bbr->bbr_attempt_hdwr_pace = 1; 13873 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp, 13874 inp->inp_route.ro_nh->nh_ifp, 13875 rate_wanted, 13876 (RS_PACING_GEQ|RS_PACING_SUB_OK), 13877 &err, NULL); 13878 if (bbr->r_ctl.crte) { 13879 bbr_type_log_hdwr_pacing(bbr, 13880 bbr->r_ctl.crte->ptbl->rs_ifp, 13881 rate_wanted, 13882 bbr->r_ctl.crte->rate, 13883 __LINE__, cts, err); 13884 BBR_STAT_INC(bbr_hdwr_rl_add_ok); 13885 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 13886 counter_u64_add(bbr_flows_whdwr_pacing, 1); 13887 bbr->bbr_hdrw_pacing = 1; 13888 /* Now what is our gain status? */ 13889 if (bbr->r_ctl.crte->rate < rate_wanted) { 13890 /* We have a problem */ 13891 bbr_setup_less_of_rate(bbr, cts, 13892 bbr->r_ctl.crte->rate, rate_wanted); 13893 } else { 13894 /* We are good */ 13895 bbr->gain_is_limited = 0; 13896 bbr->skip_gain = 0; 13897 } 13898 tcp_bbr_tso_size_check(bbr, cts); 13899 } else { 13900 bbr_type_log_hdwr_pacing(bbr, 13901 inp->inp_route.ro_nh->nh_ifp, 13902 rate_wanted, 13903 0, 13904 __LINE__, cts, err); 13905 BBR_STAT_INC(bbr_hdwr_rl_add_fail); 13906 } 13907 } 13908 if (bbr->bbr_hdrw_pacing) { 13909 /* 13910 * Worry about cases where the route 13911 * changes or something happened that we 13912 * lost our hardware pacing possibly during 13913 * the last ip_output call. 13914 */ 13915 if (inp->inp_snd_tag == NULL) { 13916 /* A change during ip output disabled hw pacing? */ 13917 bbr->bbr_hdrw_pacing = 0; 13918 } else if ((inp->inp_route.ro_nh == NULL) || 13919 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) { 13920 /* 13921 * We had an interface or route change, 13922 * detach from the current hdwr pacing 13923 * and setup to re-attempt next go 13924 * round. 13925 */ 13926 bbr->bbr_hdrw_pacing = 0; 13927 bbr->bbr_attempt_hdwr_pace = 0; 13928 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 13929 tcp_bbr_tso_size_check(bbr, cts); 13930 } 13931 } 13932 /* 13933 * Data sent (as far as we can tell). If this advertises a larger 13934 * window than any other segment, then remember the size of the 13935 * advertised window. Any pending ACK has now been sent. 13936 */ 13937 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 13938 tp->rcv_adv = tp->rcv_nxt + recwin; 13939 13940 tp->last_ack_sent = tp->rcv_nxt; 13941 if ((error == 0) && 13942 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) && 13943 (doing_tlp == 0) && 13944 (tso == 0) && 13945 (len > 0) && 13946 ((flags & TH_RST) == 0) && 13947 ((flags & TH_SYN) == 0) && 13948 (IN_RECOVERY(tp->t_flags) == 0) && 13949 (bbr->rc_in_persist == 0) && 13950 (tot_len < bbr->r_ctl.rc_pace_max_segs)) { 13951 /* 13952 * For non-tso we need to goto again until we have sent out 13953 * enough data to match what we are hptsi out every hptsi 13954 * interval. 13955 */ 13956 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13957 /* Make sure snd_nxt is drug up */ 13958 tp->snd_nxt = tp->snd_max; 13959 } 13960 if (rsm != NULL) { 13961 rsm = NULL; 13962 goto skip_again; 13963 } 13964 rsm = NULL; 13965 sack_rxmit = 0; 13966 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13967 goto again; 13968 } 13969 skip_again: 13970 if ((error == 0) && (flags & TH_FIN)) 13971 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 13972 if ((error == 0) && (flags & TH_RST)) 13973 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 13974 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) { 13975 /* 13976 * Calculate/Re-Calculate the hptsi slot in usecs based on 13977 * what we have sent so far 13978 */ 13979 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 13980 if (bbr->rc_no_pacing) 13981 slot = 0; 13982 } 13983 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13984 enobufs: 13985 if (bbr->rc_use_google == 0) 13986 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 13987 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13988 bbr->r_ctl.rc_lost_bytes))); 13989 bbr->rc_output_starts_timer = 1; 13990 if (bbr->bbr_use_rack_cheat && 13991 (more_to_rxt || 13992 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) { 13993 /* Rack cheats and shotguns out all rxt's 1ms apart */ 13994 if (slot > 1000) 13995 slot = 1000; 13996 } 13997 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) { 13998 /* 13999 * We don't change the tso size until some number of sends 14000 * to give the hardware commands time to get down 14001 * to the interface. 14002 */ 14003 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++; 14004 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) { 14005 bbr->hw_pacing_set = 1; 14006 tcp_bbr_tso_size_check(bbr, cts); 14007 } 14008 } 14009 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len); 14010 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 14011 /* Make sure snd_nxt is drug up */ 14012 tp->snd_nxt = tp->snd_max; 14013 } 14014 return (error); 14015 14016 } 14017 14018 /* 14019 * See bbr_output_wtime() for return values. 14020 */ 14021 static int 14022 bbr_output(struct tcpcb *tp) 14023 { 14024 int32_t ret; 14025 struct timeval tv; 14026 14027 NET_EPOCH_ASSERT(); 14028 14029 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14030 (void)tcp_get_usecs(&tv); 14031 ret = bbr_output_wtime(tp, &tv); 14032 return (ret); 14033 } 14034 14035 static void 14036 bbr_mtu_chg(struct tcpcb *tp) 14037 { 14038 struct tcp_bbr *bbr; 14039 struct bbr_sendmap *rsm, *frsm = NULL; 14040 uint32_t maxseg; 14041 14042 /* 14043 * The MTU has changed. a) Clear the sack filter. b) Mark everything 14044 * over the current size as SACK_PASS so a retransmit will occur. 14045 */ 14046 14047 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14048 maxseg = tp->t_maxseg - bbr->rc_last_options; 14049 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 14050 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 14051 /* Don't mess with ones acked (by sack?) */ 14052 if (rsm->r_flags & BBR_ACKED) 14053 continue; 14054 if ((rsm->r_end - rsm->r_start) > maxseg) { 14055 /* 14056 * We mark sack-passed on all the previous large 14057 * sends we did. This will force them to retransmit. 14058 */ 14059 rsm->r_flags |= BBR_SACK_PASSED; 14060 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) && 14061 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) { 14062 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 14063 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 14064 rsm->r_flags |= BBR_MARKED_LOST; 14065 } 14066 if (frsm == NULL) 14067 frsm = rsm; 14068 } 14069 } 14070 if (frsm) { 14071 bbr->r_ctl.rc_resend = frsm; 14072 } 14073 } 14074 14075 static int 14076 bbr_pru_options(struct tcpcb *tp, int flags) 14077 { 14078 if (flags & PRUS_OOB) 14079 return (EOPNOTSUPP); 14080 return (0); 14081 } 14082 14083 static void 14084 bbr_switch_failed(struct tcpcb *tp) 14085 { 14086 /* 14087 * If a switch fails we only need to 14088 * make sure mbuf_queuing is still in place. 14089 * We also need to make sure we are still in 14090 * ticks granularity (though we should probably 14091 * change bbr to go to USECs). 14092 * 14093 * For timers we need to see if we are still in the 14094 * pacer (if our flags are up) if so we are good, if 14095 * not we need to get back into the pacer. 14096 */ 14097 struct inpcb *inp = tptoinpcb(tp); 14098 struct timeval tv; 14099 uint32_t cts; 14100 uint32_t toval; 14101 struct tcp_bbr *bbr; 14102 struct hpts_diag diag; 14103 14104 inp->inp_flags2 |= INP_CANNOT_DO_ECN; 14105 inp->inp_flags2 |= INP_SUPPORTS_MBUFQ; 14106 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 14107 if (inp->inp_in_hpts) { 14108 return; 14109 } 14110 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14111 cts = tcp_get_usecs(&tv); 14112 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14113 if (TSTMP_GT(bbr->rc_pacer_started, cts)) { 14114 toval = bbr->rc_pacer_started - cts; 14115 } else { 14116 /* one slot please */ 14117 toval = HPTS_TICKS_PER_SLOT; 14118 } 14119 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 14120 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 14121 toval = bbr->r_ctl.rc_timer_exp - cts; 14122 } else { 14123 /* one slot please */ 14124 toval = HPTS_TICKS_PER_SLOT; 14125 } 14126 } else 14127 toval = HPTS_TICKS_PER_SLOT; 14128 (void)tcp_hpts_insert_diag(inp, HPTS_USEC_TO_SLOTS(toval), 14129 __LINE__, &diag); 14130 bbr_log_hpts_diag(bbr, cts, &diag); 14131 } 14132 14133 struct tcp_function_block __tcp_bbr = { 14134 .tfb_tcp_block_name = __XSTRING(STACKNAME), 14135 .tfb_tcp_output = bbr_output, 14136 .tfb_do_queued_segments = ctf_do_queued_segments, 14137 .tfb_do_segment_nounlock = bbr_do_segment_nounlock, 14138 .tfb_tcp_do_segment = bbr_do_segment, 14139 .tfb_tcp_ctloutput = bbr_ctloutput, 14140 .tfb_tcp_fb_init = bbr_init, 14141 .tfb_tcp_fb_fini = bbr_fini, 14142 .tfb_tcp_timer_stop_all = bbr_stopall, 14143 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr, 14144 .tfb_tcp_handoff_ok = bbr_handoff_ok, 14145 .tfb_tcp_mtu_chg = bbr_mtu_chg, 14146 .tfb_pru_options = bbr_pru_options, 14147 .tfb_switch_failed = bbr_switch_failed, 14148 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP, 14149 }; 14150 14151 /* 14152 * bbr_ctloutput() must drop the inpcb lock before performing copyin on 14153 * socket option arguments. When it re-acquires the lock after the copy, it 14154 * has to revalidate that the connection is still valid for the socket 14155 * option. 14156 */ 14157 static int 14158 bbr_set_sockopt(struct inpcb *inp, struct sockopt *sopt) 14159 { 14160 struct epoch_tracker et; 14161 struct tcpcb *tp; 14162 struct tcp_bbr *bbr; 14163 int32_t error = 0, optval; 14164 14165 switch (sopt->sopt_level) { 14166 case IPPROTO_IPV6: 14167 case IPPROTO_IP: 14168 return (tcp_default_ctloutput(inp, sopt)); 14169 } 14170 14171 switch (sopt->sopt_name) { 14172 case TCP_RACK_PACE_MAX_SEG: 14173 case TCP_RACK_MIN_TO: 14174 case TCP_RACK_REORD_THRESH: 14175 case TCP_RACK_REORD_FADE: 14176 case TCP_RACK_TLP_THRESH: 14177 case TCP_RACK_PKT_DELAY: 14178 case TCP_BBR_ALGORITHM: 14179 case TCP_BBR_TSLIMITS: 14180 case TCP_BBR_IWINTSO: 14181 case TCP_BBR_RECFORCE: 14182 case TCP_BBR_STARTUP_PG: 14183 case TCP_BBR_DRAIN_PG: 14184 case TCP_BBR_RWND_IS_APP: 14185 case TCP_BBR_PROBE_RTT_INT: 14186 case TCP_BBR_PROBE_RTT_GAIN: 14187 case TCP_BBR_PROBE_RTT_LEN: 14188 case TCP_BBR_STARTUP_LOSS_EXIT: 14189 case TCP_BBR_USEDEL_RATE: 14190 case TCP_BBR_MIN_RTO: 14191 case TCP_BBR_MAX_RTO: 14192 case TCP_BBR_PACE_PER_SEC: 14193 case TCP_DELACK: 14194 case TCP_BBR_PACE_DEL_TAR: 14195 case TCP_BBR_SEND_IWND_IN_TSO: 14196 case TCP_BBR_EXTRA_STATE: 14197 case TCP_BBR_UTTER_MAX_TSO: 14198 case TCP_BBR_MIN_TOPACEOUT: 14199 case TCP_BBR_FLOOR_MIN_TSO: 14200 case TCP_BBR_TSTMP_RAISES: 14201 case TCP_BBR_POLICER_DETECT: 14202 case TCP_BBR_USE_RACK_CHEAT: 14203 case TCP_DATA_AFTER_CLOSE: 14204 case TCP_BBR_HDWR_PACE: 14205 case TCP_BBR_PACE_SEG_MAX: 14206 case TCP_BBR_PACE_SEG_MIN: 14207 case TCP_BBR_PACE_CROSS: 14208 case TCP_BBR_PACE_OH: 14209 #ifdef NETFLIX_PEAKRATE 14210 case TCP_MAXPEAKRATE: 14211 #endif 14212 case TCP_BBR_TMR_PACE_OH: 14213 case TCP_BBR_RACK_RTT_USE: 14214 case TCP_BBR_RETRAN_WTSO: 14215 break; 14216 default: 14217 return (tcp_default_ctloutput(inp, sopt)); 14218 break; 14219 } 14220 INP_WUNLOCK(inp); 14221 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 14222 if (error) 14223 return (error); 14224 INP_WLOCK(inp); 14225 if (inp->inp_flags & INP_DROPPED) { 14226 INP_WUNLOCK(inp); 14227 return (ECONNRESET); 14228 } 14229 tp = intotcpcb(inp); 14230 if (tp->t_fb != &__tcp_bbr) { 14231 INP_WUNLOCK(inp); 14232 return (ENOPROTOOPT); 14233 } 14234 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14235 switch (sopt->sopt_name) { 14236 case TCP_BBR_PACE_PER_SEC: 14237 BBR_OPTS_INC(tcp_bbr_pace_per_sec); 14238 bbr->r_ctl.bbr_hptsi_per_second = optval; 14239 break; 14240 case TCP_BBR_PACE_DEL_TAR: 14241 BBR_OPTS_INC(tcp_bbr_pace_del_tar); 14242 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval; 14243 break; 14244 case TCP_BBR_PACE_SEG_MAX: 14245 BBR_OPTS_INC(tcp_bbr_pace_seg_max); 14246 bbr->r_ctl.bbr_hptsi_segments_max = optval; 14247 break; 14248 case TCP_BBR_PACE_SEG_MIN: 14249 BBR_OPTS_INC(tcp_bbr_pace_seg_min); 14250 bbr->r_ctl.bbr_hptsi_bytes_min = optval; 14251 break; 14252 case TCP_BBR_PACE_CROSS: 14253 BBR_OPTS_INC(tcp_bbr_pace_cross); 14254 bbr->r_ctl.bbr_cross_over = optval; 14255 break; 14256 case TCP_BBR_ALGORITHM: 14257 BBR_OPTS_INC(tcp_bbr_algorithm); 14258 if (optval && (bbr->rc_use_google == 0)) { 14259 /* Turn on the google mode */ 14260 bbr_google_mode_on(bbr); 14261 if ((optval > 3) && (optval < 500)) { 14262 /* 14263 * Must be at least greater than .3% 14264 * and must be less than 50.0%. 14265 */ 14266 bbr->r_ctl.bbr_google_discount = optval; 14267 } 14268 } else if ((optval == 0) && (bbr->rc_use_google == 1)) { 14269 /* Turn off the google mode */ 14270 bbr_google_mode_off(bbr); 14271 } 14272 break; 14273 case TCP_BBR_TSLIMITS: 14274 BBR_OPTS_INC(tcp_bbr_tslimits); 14275 if (optval == 1) 14276 bbr->rc_use_ts_limit = 1; 14277 else if (optval == 0) 14278 bbr->rc_use_ts_limit = 0; 14279 else 14280 error = EINVAL; 14281 break; 14282 14283 case TCP_BBR_IWINTSO: 14284 BBR_OPTS_INC(tcp_bbr_iwintso); 14285 if ((optval >= 0) && (optval < 128)) { 14286 uint32_t twin; 14287 14288 bbr->rc_init_win = optval; 14289 twin = bbr_initial_cwnd(bbr, tp); 14290 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd)) 14291 tp->snd_cwnd = twin; 14292 else 14293 error = EBUSY; 14294 } else 14295 error = EINVAL; 14296 break; 14297 case TCP_BBR_STARTUP_PG: 14298 BBR_OPTS_INC(tcp_bbr_startup_pg); 14299 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) { 14300 bbr->r_ctl.rc_startup_pg = optval; 14301 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 14302 bbr->r_ctl.rc_bbr_hptsi_gain = optval; 14303 } 14304 } else 14305 error = EINVAL; 14306 break; 14307 case TCP_BBR_DRAIN_PG: 14308 BBR_OPTS_INC(tcp_bbr_drain_pg); 14309 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) 14310 bbr->r_ctl.rc_drain_pg = optval; 14311 else 14312 error = EINVAL; 14313 break; 14314 case TCP_BBR_PROBE_RTT_LEN: 14315 BBR_OPTS_INC(tcp_bbr_probertt_len); 14316 if (optval <= 1) 14317 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND)); 14318 else 14319 error = EINVAL; 14320 break; 14321 case TCP_BBR_PROBE_RTT_GAIN: 14322 BBR_OPTS_INC(tcp_bbr_probertt_gain); 14323 if (optval <= BBR_UNIT) 14324 bbr->r_ctl.bbr_rttprobe_gain_val = optval; 14325 else 14326 error = EINVAL; 14327 break; 14328 case TCP_BBR_PROBE_RTT_INT: 14329 BBR_OPTS_INC(tcp_bbr_probe_rtt_int); 14330 if (optval > 1000) 14331 bbr->r_ctl.rc_probertt_int = optval; 14332 else 14333 error = EINVAL; 14334 break; 14335 case TCP_BBR_MIN_TOPACEOUT: 14336 BBR_OPTS_INC(tcp_bbr_topaceout); 14337 if (optval == 0) { 14338 bbr->no_pacing_until = 0; 14339 bbr->rc_no_pacing = 0; 14340 } else if (optval <= 0x00ff) { 14341 bbr->no_pacing_until = optval; 14342 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) && 14343 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){ 14344 /* Turn on no pacing */ 14345 bbr->rc_no_pacing = 1; 14346 } 14347 } else 14348 error = EINVAL; 14349 break; 14350 case TCP_BBR_STARTUP_LOSS_EXIT: 14351 BBR_OPTS_INC(tcp_bbr_startup_loss_exit); 14352 bbr->rc_loss_exit = optval; 14353 break; 14354 case TCP_BBR_USEDEL_RATE: 14355 error = EINVAL; 14356 break; 14357 case TCP_BBR_MIN_RTO: 14358 BBR_OPTS_INC(tcp_bbr_min_rto); 14359 bbr->r_ctl.rc_min_rto_ms = optval; 14360 break; 14361 case TCP_BBR_MAX_RTO: 14362 BBR_OPTS_INC(tcp_bbr_max_rto); 14363 bbr->rc_max_rto_sec = optval; 14364 break; 14365 case TCP_RACK_MIN_TO: 14366 /* Minimum time between rack t-o's in ms */ 14367 BBR_OPTS_INC(tcp_rack_min_to); 14368 bbr->r_ctl.rc_min_to = optval; 14369 break; 14370 case TCP_RACK_REORD_THRESH: 14371 /* RACK reorder threshold (shift amount) */ 14372 BBR_OPTS_INC(tcp_rack_reord_thresh); 14373 if ((optval > 0) && (optval < 31)) 14374 bbr->r_ctl.rc_reorder_shift = optval; 14375 else 14376 error = EINVAL; 14377 break; 14378 case TCP_RACK_REORD_FADE: 14379 /* Does reordering fade after ms time */ 14380 BBR_OPTS_INC(tcp_rack_reord_fade); 14381 bbr->r_ctl.rc_reorder_fade = optval; 14382 break; 14383 case TCP_RACK_TLP_THRESH: 14384 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14385 BBR_OPTS_INC(tcp_rack_tlp_thresh); 14386 if (optval) 14387 bbr->rc_tlp_threshold = optval; 14388 else 14389 error = EINVAL; 14390 break; 14391 case TCP_BBR_USE_RACK_CHEAT: 14392 BBR_OPTS_INC(tcp_use_rackcheat); 14393 if (bbr->rc_use_google) { 14394 error = EINVAL; 14395 break; 14396 } 14397 BBR_OPTS_INC(tcp_rack_cheat); 14398 if (optval) 14399 bbr->bbr_use_rack_cheat = 1; 14400 else 14401 bbr->bbr_use_rack_cheat = 0; 14402 break; 14403 case TCP_BBR_FLOOR_MIN_TSO: 14404 BBR_OPTS_INC(tcp_utter_max_tso); 14405 if ((optval >= 0) && (optval < 40)) 14406 bbr->r_ctl.bbr_hptsi_segments_floor = optval; 14407 else 14408 error = EINVAL; 14409 break; 14410 case TCP_BBR_UTTER_MAX_TSO: 14411 BBR_OPTS_INC(tcp_utter_max_tso); 14412 if ((optval >= 0) && (optval < 0xffff)) 14413 bbr->r_ctl.bbr_utter_max = optval; 14414 else 14415 error = EINVAL; 14416 break; 14417 14418 case TCP_BBR_EXTRA_STATE: 14419 BBR_OPTS_INC(tcp_extra_state); 14420 if (optval) 14421 bbr->rc_use_idle_restart = 1; 14422 else 14423 bbr->rc_use_idle_restart = 0; 14424 break; 14425 case TCP_BBR_SEND_IWND_IN_TSO: 14426 BBR_OPTS_INC(tcp_iwnd_tso); 14427 if (optval) { 14428 bbr->bbr_init_win_cheat = 1; 14429 if (bbr->rc_past_init_win == 0) { 14430 uint32_t cts; 14431 cts = tcp_get_usecs(&bbr->rc_tv); 14432 tcp_bbr_tso_size_check(bbr, cts); 14433 } 14434 } else 14435 bbr->bbr_init_win_cheat = 0; 14436 break; 14437 case TCP_BBR_HDWR_PACE: 14438 BBR_OPTS_INC(tcp_hdwr_pacing); 14439 if (optval){ 14440 bbr->bbr_hdw_pace_ena = 1; 14441 bbr->bbr_attempt_hdwr_pace = 0; 14442 } else { 14443 bbr->bbr_hdw_pace_ena = 0; 14444 #ifdef RATELIMIT 14445 if (bbr->r_ctl.crte != NULL) { 14446 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 14447 bbr->r_ctl.crte = NULL; 14448 } 14449 #endif 14450 } 14451 break; 14452 14453 case TCP_DELACK: 14454 BBR_OPTS_INC(tcp_delack); 14455 if (optval < 100) { 14456 if (optval == 0) /* off */ 14457 tp->t_delayed_ack = 0; 14458 else if (optval == 1) /* on which is 2 */ 14459 tp->t_delayed_ack = 2; 14460 else /* higher than 2 and less than 100 */ 14461 tp->t_delayed_ack = optval; 14462 if (tp->t_flags & TF_DELACK) { 14463 tp->t_flags &= ~TF_DELACK; 14464 tp->t_flags |= TF_ACKNOW; 14465 NET_EPOCH_ENTER(et); 14466 bbr_output(tp); 14467 NET_EPOCH_EXIT(et); 14468 } 14469 } else 14470 error = EINVAL; 14471 break; 14472 case TCP_RACK_PKT_DELAY: 14473 /* RACK added ms i.e. rack-rtt + reord + N */ 14474 BBR_OPTS_INC(tcp_rack_pkt_delay); 14475 bbr->r_ctl.rc_pkt_delay = optval; 14476 break; 14477 #ifdef NETFLIX_PEAKRATE 14478 case TCP_MAXPEAKRATE: 14479 BBR_OPTS_INC(tcp_maxpeak); 14480 error = tcp_set_maxpeakrate(tp, optval); 14481 if (!error) 14482 tp->t_peakrate_thr = tp->t_maxpeakrate; 14483 break; 14484 #endif 14485 case TCP_BBR_RETRAN_WTSO: 14486 BBR_OPTS_INC(tcp_retran_wtso); 14487 if (optval) 14488 bbr->rc_resends_use_tso = 1; 14489 else 14490 bbr->rc_resends_use_tso = 0; 14491 break; 14492 case TCP_DATA_AFTER_CLOSE: 14493 BBR_OPTS_INC(tcp_data_ac); 14494 if (optval) 14495 bbr->rc_allow_data_af_clo = 1; 14496 else 14497 bbr->rc_allow_data_af_clo = 0; 14498 break; 14499 case TCP_BBR_POLICER_DETECT: 14500 BBR_OPTS_INC(tcp_policer_det); 14501 if (bbr->rc_use_google == 0) 14502 error = EINVAL; 14503 else if (optval) 14504 bbr->r_use_policer = 1; 14505 else 14506 bbr->r_use_policer = 0; 14507 break; 14508 14509 case TCP_BBR_TSTMP_RAISES: 14510 BBR_OPTS_INC(tcp_ts_raises); 14511 if (optval) 14512 bbr->ts_can_raise = 1; 14513 else 14514 bbr->ts_can_raise = 0; 14515 break; 14516 case TCP_BBR_TMR_PACE_OH: 14517 BBR_OPTS_INC(tcp_pacing_oh_tmr); 14518 if (bbr->rc_use_google) { 14519 error = EINVAL; 14520 } else { 14521 if (optval) 14522 bbr->r_ctl.rc_incr_tmrs = 1; 14523 else 14524 bbr->r_ctl.rc_incr_tmrs = 0; 14525 } 14526 break; 14527 case TCP_BBR_PACE_OH: 14528 BBR_OPTS_INC(tcp_pacing_oh); 14529 if (bbr->rc_use_google) { 14530 error = EINVAL; 14531 } else { 14532 if (optval > (BBR_INCL_TCP_OH| 14533 BBR_INCL_IP_OH| 14534 BBR_INCL_ENET_OH)) { 14535 error = EINVAL; 14536 break; 14537 } 14538 if (optval & BBR_INCL_TCP_OH) 14539 bbr->r_ctl.rc_inc_tcp_oh = 1; 14540 else 14541 bbr->r_ctl.rc_inc_tcp_oh = 0; 14542 if (optval & BBR_INCL_IP_OH) 14543 bbr->r_ctl.rc_inc_ip_oh = 1; 14544 else 14545 bbr->r_ctl.rc_inc_ip_oh = 0; 14546 if (optval & BBR_INCL_ENET_OH) 14547 bbr->r_ctl.rc_inc_enet_oh = 1; 14548 else 14549 bbr->r_ctl.rc_inc_enet_oh = 0; 14550 } 14551 break; 14552 default: 14553 return (tcp_default_ctloutput(inp, sopt)); 14554 break; 14555 } 14556 #ifdef NETFLIX_STATS 14557 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 14558 #endif 14559 INP_WUNLOCK(inp); 14560 return (error); 14561 } 14562 14563 /* 14564 * return 0 on success, error-num on failure 14565 */ 14566 static int 14567 bbr_get_sockopt(struct inpcb *inp, struct sockopt *sopt) 14568 { 14569 struct tcpcb *tp; 14570 struct tcp_bbr *bbr; 14571 int32_t error, optval; 14572 14573 tp = intotcpcb(inp); 14574 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14575 if (bbr == NULL) { 14576 INP_WUNLOCK(inp); 14577 return (EINVAL); 14578 } 14579 /* 14580 * Because all our options are either boolean or an int, we can just 14581 * pull everything into optval and then unlock and copy. If we ever 14582 * add a option that is not a int, then this will have quite an 14583 * impact to this routine. 14584 */ 14585 switch (sopt->sopt_name) { 14586 case TCP_BBR_PACE_PER_SEC: 14587 optval = bbr->r_ctl.bbr_hptsi_per_second; 14588 break; 14589 case TCP_BBR_PACE_DEL_TAR: 14590 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar; 14591 break; 14592 case TCP_BBR_PACE_SEG_MAX: 14593 optval = bbr->r_ctl.bbr_hptsi_segments_max; 14594 break; 14595 case TCP_BBR_MIN_TOPACEOUT: 14596 optval = bbr->no_pacing_until; 14597 break; 14598 case TCP_BBR_PACE_SEG_MIN: 14599 optval = bbr->r_ctl.bbr_hptsi_bytes_min; 14600 break; 14601 case TCP_BBR_PACE_CROSS: 14602 optval = bbr->r_ctl.bbr_cross_over; 14603 break; 14604 case TCP_BBR_ALGORITHM: 14605 optval = bbr->rc_use_google; 14606 break; 14607 case TCP_BBR_TSLIMITS: 14608 optval = bbr->rc_use_ts_limit; 14609 break; 14610 case TCP_BBR_IWINTSO: 14611 optval = bbr->rc_init_win; 14612 break; 14613 case TCP_BBR_STARTUP_PG: 14614 optval = bbr->r_ctl.rc_startup_pg; 14615 break; 14616 case TCP_BBR_DRAIN_PG: 14617 optval = bbr->r_ctl.rc_drain_pg; 14618 break; 14619 case TCP_BBR_PROBE_RTT_INT: 14620 optval = bbr->r_ctl.rc_probertt_int; 14621 break; 14622 case TCP_BBR_PROBE_RTT_LEN: 14623 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND); 14624 break; 14625 case TCP_BBR_PROBE_RTT_GAIN: 14626 optval = bbr->r_ctl.bbr_rttprobe_gain_val; 14627 break; 14628 case TCP_BBR_STARTUP_LOSS_EXIT: 14629 optval = bbr->rc_loss_exit; 14630 break; 14631 case TCP_BBR_USEDEL_RATE: 14632 error = EINVAL; 14633 break; 14634 case TCP_BBR_MIN_RTO: 14635 optval = bbr->r_ctl.rc_min_rto_ms; 14636 break; 14637 case TCP_BBR_MAX_RTO: 14638 optval = bbr->rc_max_rto_sec; 14639 break; 14640 case TCP_RACK_PACE_MAX_SEG: 14641 /* Max segments in a pace */ 14642 optval = bbr->r_ctl.rc_pace_max_segs; 14643 break; 14644 case TCP_RACK_MIN_TO: 14645 /* Minimum time between rack t-o's in ms */ 14646 optval = bbr->r_ctl.rc_min_to; 14647 break; 14648 case TCP_RACK_REORD_THRESH: 14649 /* RACK reorder threshold (shift amount) */ 14650 optval = bbr->r_ctl.rc_reorder_shift; 14651 break; 14652 case TCP_RACK_REORD_FADE: 14653 /* Does reordering fade after ms time */ 14654 optval = bbr->r_ctl.rc_reorder_fade; 14655 break; 14656 case TCP_BBR_USE_RACK_CHEAT: 14657 /* Do we use the rack cheat for rxt */ 14658 optval = bbr->bbr_use_rack_cheat; 14659 break; 14660 case TCP_BBR_FLOOR_MIN_TSO: 14661 optval = bbr->r_ctl.bbr_hptsi_segments_floor; 14662 break; 14663 case TCP_BBR_UTTER_MAX_TSO: 14664 optval = bbr->r_ctl.bbr_utter_max; 14665 break; 14666 case TCP_BBR_SEND_IWND_IN_TSO: 14667 /* Do we send TSO size segments initially */ 14668 optval = bbr->bbr_init_win_cheat; 14669 break; 14670 case TCP_BBR_EXTRA_STATE: 14671 optval = bbr->rc_use_idle_restart; 14672 break; 14673 case TCP_RACK_TLP_THRESH: 14674 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14675 optval = bbr->rc_tlp_threshold; 14676 break; 14677 case TCP_RACK_PKT_DELAY: 14678 /* RACK added ms i.e. rack-rtt + reord + N */ 14679 optval = bbr->r_ctl.rc_pkt_delay; 14680 break; 14681 case TCP_BBR_RETRAN_WTSO: 14682 optval = bbr->rc_resends_use_tso; 14683 break; 14684 case TCP_DATA_AFTER_CLOSE: 14685 optval = bbr->rc_allow_data_af_clo; 14686 break; 14687 case TCP_DELACK: 14688 optval = tp->t_delayed_ack; 14689 break; 14690 case TCP_BBR_HDWR_PACE: 14691 optval = bbr->bbr_hdw_pace_ena; 14692 break; 14693 case TCP_BBR_POLICER_DETECT: 14694 optval = bbr->r_use_policer; 14695 break; 14696 case TCP_BBR_TSTMP_RAISES: 14697 optval = bbr->ts_can_raise; 14698 break; 14699 case TCP_BBR_TMR_PACE_OH: 14700 optval = bbr->r_ctl.rc_incr_tmrs; 14701 break; 14702 case TCP_BBR_PACE_OH: 14703 optval = 0; 14704 if (bbr->r_ctl.rc_inc_tcp_oh) 14705 optval |= BBR_INCL_TCP_OH; 14706 if (bbr->r_ctl.rc_inc_ip_oh) 14707 optval |= BBR_INCL_IP_OH; 14708 if (bbr->r_ctl.rc_inc_enet_oh) 14709 optval |= BBR_INCL_ENET_OH; 14710 break; 14711 default: 14712 return (tcp_default_ctloutput(inp, sopt)); 14713 break; 14714 } 14715 INP_WUNLOCK(inp); 14716 error = sooptcopyout(sopt, &optval, sizeof optval); 14717 return (error); 14718 } 14719 14720 /* 14721 * return 0 on success, error-num on failure 14722 */ 14723 static int 14724 bbr_ctloutput(struct inpcb *inp, struct sockopt *sopt) 14725 { 14726 if (sopt->sopt_dir == SOPT_SET) { 14727 return (bbr_set_sockopt(inp, sopt)); 14728 } else if (sopt->sopt_dir == SOPT_GET) { 14729 return (bbr_get_sockopt(inp, sopt)); 14730 } else { 14731 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 14732 } 14733 } 14734 14735 static const char *bbr_stack_names[] = { 14736 __XSTRING(STACKNAME), 14737 #ifdef STACKALIAS 14738 __XSTRING(STACKALIAS), 14739 #endif 14740 }; 14741 14742 static bool bbr_mod_inited = false; 14743 14744 static int 14745 tcp_addbbr(module_t mod, int32_t type, void *data) 14746 { 14747 int32_t err = 0; 14748 int num_stacks; 14749 14750 switch (type) { 14751 case MOD_LOAD: 14752 printf("Attempting to load " __XSTRING(MODNAME) "\n"); 14753 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 14754 sizeof(struct bbr_sendmap), 14755 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 14756 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 14757 sizeof(struct tcp_bbr), 14758 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 14759 sysctl_ctx_init(&bbr_sysctl_ctx); 14760 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 14761 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 14762 OID_AUTO, 14763 #ifdef STACKALIAS 14764 __XSTRING(STACKALIAS), 14765 #else 14766 __XSTRING(STACKNAME), 14767 #endif 14768 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 14769 ""); 14770 if (bbr_sysctl_root == NULL) { 14771 printf("Failed to add sysctl node\n"); 14772 err = EFAULT; 14773 goto free_uma; 14774 } 14775 bbr_init_sysctls(); 14776 num_stacks = nitems(bbr_stack_names); 14777 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK, 14778 bbr_stack_names, &num_stacks); 14779 if (err) { 14780 printf("Failed to register %s stack name for " 14781 "%s module\n", bbr_stack_names[num_stacks], 14782 __XSTRING(MODNAME)); 14783 sysctl_ctx_free(&bbr_sysctl_ctx); 14784 free_uma: 14785 uma_zdestroy(bbr_zone); 14786 uma_zdestroy(bbr_pcb_zone); 14787 bbr_counter_destroy(); 14788 printf("Failed to register " __XSTRING(MODNAME) 14789 " module err:%d\n", err); 14790 return (err); 14791 } 14792 tcp_lro_reg_mbufq(); 14793 bbr_mod_inited = true; 14794 printf(__XSTRING(MODNAME) " is now available\n"); 14795 break; 14796 case MOD_QUIESCE: 14797 err = deregister_tcp_functions(&__tcp_bbr, true, false); 14798 break; 14799 case MOD_UNLOAD: 14800 err = deregister_tcp_functions(&__tcp_bbr, false, true); 14801 if (err == EBUSY) 14802 break; 14803 if (bbr_mod_inited) { 14804 uma_zdestroy(bbr_zone); 14805 uma_zdestroy(bbr_pcb_zone); 14806 sysctl_ctx_free(&bbr_sysctl_ctx); 14807 bbr_counter_destroy(); 14808 printf(__XSTRING(MODNAME) 14809 " is now no longer available\n"); 14810 bbr_mod_inited = false; 14811 } 14812 tcp_lro_dereg_mbufq(); 14813 err = 0; 14814 break; 14815 default: 14816 return (EOPNOTSUPP); 14817 } 14818 return (err); 14819 } 14820 14821 static moduledata_t tcp_bbr = { 14822 .name = __XSTRING(MODNAME), 14823 .evhand = tcp_addbbr, 14824 .priv = 0 14825 }; 14826 14827 MODULE_VERSION(MODNAME, 1); 14828 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 14829 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 14830