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 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 #include "opt_ratelimit.h" 38 #include <sys/param.h> 39 #include <sys/arb.h> 40 #include <sys/module.h> 41 #include <sys/kernel.h> 42 #include <sys/libkern.h> 43 #ifdef TCP_HHOOK 44 #include <sys/hhook.h> 45 #endif 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 #ifdef STATS 54 #include <sys/qmath.h> 55 #include <sys/tree.h> 56 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 57 #endif 58 #include <sys/refcount.h> 59 #include <sys/queue.h> 60 #include <sys/eventhandler.h> 61 #include <sys/smp.h> 62 #include <sys/kthread.h> 63 #include <sys/lock.h> 64 #include <sys/mutex.h> 65 #include <sys/tim_filter.h> 66 #include <sys/time.h> 67 #include <sys/protosw.h> 68 #include <vm/uma.h> 69 #include <sys/kern_prefetch.h> 70 71 #include <net/route.h> 72 #include <net/route/nhop.h> 73 #include <net/vnet.h> 74 75 #define TCPSTATES /* for logging */ 76 77 #include <netinet/in.h> 78 #include <netinet/in_kdtrace.h> 79 #include <netinet/in_pcb.h> 80 #include <netinet/ip.h> 81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 83 #include <netinet/ip_var.h> 84 #include <netinet/ip6.h> 85 #include <netinet6/in6_pcb.h> 86 #include <netinet6/ip6_var.h> 87 #define TCPOUTFLAGS 88 #include <netinet/tcp.h> 89 #include <netinet/tcp_fsm.h> 90 #include <netinet/tcp_seq.h> 91 #include <netinet/tcp_timer.h> 92 #include <netinet/tcp_var.h> 93 #include <netinet/tcpip.h> 94 #include <netinet/tcp_hpts.h> 95 #include <netinet/cc/cc.h> 96 #include <netinet/tcp_log_buf.h> 97 #include <netinet/tcp_ratelimit.h> 98 #include <netinet/tcp_lro.h> 99 #ifdef TCP_OFFLOAD 100 #include <netinet/tcp_offload.h> 101 #endif 102 #ifdef INET6 103 #include <netinet6/tcp6_var.h> 104 #endif 105 #include <netinet/tcp_fastopen.h> 106 107 #include <netipsec/ipsec_support.h> 108 #include <net/if.h> 109 #include <net/if_var.h> 110 #include <net/ethernet.h> 111 112 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 113 #include <netipsec/ipsec.h> 114 #include <netipsec/ipsec6.h> 115 #endif /* IPSEC */ 116 117 #include <netinet/udp.h> 118 #include <netinet/udp_var.h> 119 #include <machine/in_cksum.h> 120 121 #ifdef MAC 122 #include <security/mac/mac_framework.h> 123 #endif 124 125 #include "sack_filter.h" 126 #include "tcp_bbr.h" 127 #include "rack_bbr_common.h" 128 uma_zone_t bbr_zone; 129 uma_zone_t bbr_pcb_zone; 130 131 struct sysctl_ctx_list bbr_sysctl_ctx; 132 struct sysctl_oid *bbr_sysctl_root; 133 134 #define TCPT_RANGESET_NOSLOP(tv, value, tvmin, tvmax) do { \ 135 (tv) = (value); \ 136 if ((u_long)(tv) < (u_long)(tvmin)) \ 137 (tv) = (tvmin); \ 138 if ((u_long)(tv) > (u_long)(tvmax)) \ 139 (tv) = (tvmax); \ 140 } while(0) 141 142 /*#define BBR_INVARIANT 1*/ 143 144 /* 145 * initial window 146 */ 147 static uint32_t bbr_def_init_win = 10; 148 static int32_t bbr_persist_min = 250000; /* 250ms */ 149 static int32_t bbr_persist_max = 1000000; /* 1 Second */ 150 static int32_t bbr_cwnd_may_shrink = 0; 151 static int32_t bbr_cwndtarget_rtt_touse = BBR_RTT_PROP; 152 static int32_t bbr_num_pktepo_for_del_limit = BBR_NUM_RTTS_FOR_DEL_LIMIT; 153 static int32_t bbr_hardware_pacing_limit = 8000; 154 static int32_t bbr_quanta = 3; /* How much extra quanta do we get? */ 155 static int32_t bbr_no_retran = 0; 156 157 static int32_t bbr_error_base_paceout = 10000; /* usec to pace */ 158 static int32_t bbr_max_net_error_cnt = 10; 159 /* Should the following be dynamic too -- loss wise */ 160 static int32_t bbr_rtt_gain_thresh = 0; 161 /* Measurement controls */ 162 static int32_t bbr_use_google_algo = 1; 163 static int32_t bbr_ts_limiting = 1; 164 static int32_t bbr_ts_can_raise = 0; 165 static int32_t bbr_do_red = 600; 166 static int32_t bbr_red_scale = 20000; 167 static int32_t bbr_red_mul = 1; 168 static int32_t bbr_red_div = 2; 169 static int32_t bbr_red_growth_restrict = 1; 170 static int32_t bbr_target_is_bbunit = 0; 171 static int32_t bbr_drop_limit = 0; 172 /* 173 * How much gain do we need to see to 174 * stay in startup? 175 */ 176 static int32_t bbr_marks_rxt_sack_passed = 0; 177 static int32_t bbr_start_exit = 25; 178 static int32_t bbr_low_start_exit = 25; /* When we are in reduced gain */ 179 static int32_t bbr_startup_loss_thresh = 2000; /* 20.00% loss */ 180 static int32_t bbr_hptsi_max_mul = 1; /* These two mul/div assure a min pacing */ 181 static int32_t bbr_hptsi_max_div = 2; /* time, 0 means turned off. We need this 182 * if we go back ever to where the pacer 183 * has priority over timers. 184 */ 185 static int32_t bbr_policer_call_from_rack_to = 0; 186 static int32_t bbr_policer_detection_enabled = 1; 187 static int32_t bbr_min_measurements_req = 1; /* We need at least 2 188 * measurements before we are 189 * "good" note that 2 == 1. 190 * This is because we use a > 191 * comparison. This means if 192 * min_measure was 0, it takes 193 * num-measures > min(0) and 194 * you get 1 measurement and 195 * you are good. Set to 1, you 196 * have to have two 197 * measurements (this is done 198 * to prevent it from being ok 199 * to have no measurements). */ 200 static int32_t bbr_no_pacing_until = 4; 201 202 static int32_t bbr_min_usec_delta = 20000; /* 20,000 usecs */ 203 static int32_t bbr_min_peer_delta = 20; /* 20 units */ 204 static int32_t bbr_delta_percent = 150; /* 15.0 % */ 205 206 static int32_t bbr_target_cwnd_mult_limit = 8; 207 /* 208 * bbr_cwnd_min_val is the number of 209 * segments we hold to in the RTT probe 210 * state typically 4. 211 */ 212 static int32_t bbr_cwnd_min_val = BBR_PROBERTT_NUM_MSS; 213 214 static int32_t bbr_cwnd_min_val_hs = BBR_HIGHSPEED_NUM_MSS; 215 216 static int32_t bbr_gain_to_target = 1; 217 static int32_t bbr_gain_gets_extra_too = 1; 218 /* 219 * bbr_high_gain is the 2/ln(2) value we need 220 * to double the sending rate in startup. This 221 * is used for both cwnd and hptsi gain's. 222 */ 223 static int32_t bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1; 224 static int32_t bbr_startup_lower = BBR_UNIT * 1500 / 1000 + 1; 225 static int32_t bbr_use_lower_gain_in_startup = 1; 226 227 /* thresholds for reduction on drain in sub-states/drain */ 228 static int32_t bbr_drain_rtt = BBR_SRTT; 229 static int32_t bbr_drain_floor = 88; 230 static int32_t google_allow_early_out = 1; 231 static int32_t google_consider_lost = 1; 232 static int32_t bbr_drain_drop_mul = 4; 233 static int32_t bbr_drain_drop_div = 5; 234 static int32_t bbr_rand_ot = 50; 235 static int32_t bbr_can_force_probertt = 0; 236 static int32_t bbr_can_adjust_probertt = 1; 237 static int32_t bbr_probertt_sets_rtt = 0; 238 static int32_t bbr_can_use_ts_for_rtt = 1; 239 static int32_t bbr_is_ratio = 0; 240 static int32_t bbr_sub_drain_app_limit = 1; 241 static int32_t bbr_prtt_slam_cwnd = 1; 242 static int32_t bbr_sub_drain_slam_cwnd = 1; 243 static int32_t bbr_slam_cwnd_in_main_drain = 1; 244 static int32_t bbr_filter_len_sec = 6; /* How long does the rttProp filter 245 * hold */ 246 static uint32_t bbr_rtt_probe_limit = (USECS_IN_SECOND * 4); 247 /* 248 * bbr_drain_gain is the reverse of the high_gain 249 * designed to drain back out the standing queue 250 * that is formed in startup by causing a larger 251 * hptsi gain and thus drainging the packets 252 * in flight. 253 */ 254 static int32_t bbr_drain_gain = BBR_UNIT * 1000 / 2885; 255 static int32_t bbr_rttprobe_gain = 192; 256 257 /* 258 * The cwnd_gain is the default cwnd gain applied when 259 * calculating a target cwnd. Note that the cwnd is 260 * a secondary factor in the way BBR works (see the 261 * paper and think about it, it will take some time). 262 * Basically the hptsi_gain spreads the packets out 263 * so you never get more than BDP to the peer even 264 * if the cwnd is high. In our implemenation that 265 * means in non-recovery/retransmission scenarios 266 * cwnd will never be reached by the flight-size. 267 */ 268 static int32_t bbr_cwnd_gain = BBR_UNIT * 2; 269 static int32_t bbr_tlp_type_to_use = BBR_SRTT; 270 static int32_t bbr_delack_time = 100000; /* 100ms in useconds */ 271 static int32_t bbr_sack_not_required = 0; /* set to one to allow non-sack to use bbr */ 272 static int32_t bbr_initial_bw_bps = 62500; /* 500kbps in bytes ps */ 273 static int32_t bbr_ignore_data_after_close = 1; 274 static int16_t bbr_hptsi_gain[] = { 275 (BBR_UNIT *5 / 4), 276 (BBR_UNIT * 3 / 4), 277 BBR_UNIT, 278 BBR_UNIT, 279 BBR_UNIT, 280 BBR_UNIT, 281 BBR_UNIT, 282 BBR_UNIT 283 }; 284 int32_t bbr_use_rack_resend_cheat = 1; 285 int32_t bbr_sends_full_iwnd = 1; 286 287 #define BBR_HPTSI_GAIN_MAX 8 288 /* 289 * The BBR module incorporates a number of 290 * TCP ideas that have been put out into the IETF 291 * over the last few years: 292 * - Yuchung Cheng's RACK TCP (for which its named) that 293 * will stop us using the number of dup acks and instead 294 * use time as the gage of when we retransmit. 295 * - Reorder Detection of RFC4737 and the Tail-Loss probe draft 296 * of Dukkipati et.al. 297 * - Van Jacobson's et.al BBR. 298 * 299 * RACK depends on SACK, so if an endpoint arrives that 300 * cannot do SACK the state machine below will shuttle the 301 * connection back to using the "default" TCP stack that is 302 * in FreeBSD. 303 * 304 * To implement BBR and RACK the original TCP stack was first decomposed 305 * into a functional state machine with individual states 306 * for each of the possible TCP connection states. The do_segment 307 * functions role in life is to mandate the connection supports SACK 308 * initially and then assure that the RACK state matches the conenction 309 * state before calling the states do_segment function. Data processing 310 * of inbound segments also now happens in the hpts_do_segment in general 311 * with only one exception. This is so we can keep the connection on 312 * a single CPU. 313 * 314 * Each state is simplified due to the fact that the original do_segment 315 * has been decomposed and we *know* what state we are in (no 316 * switches on the state) and all tests for SACK are gone. This 317 * greatly simplifies what each state does. 318 * 319 * TCP output is also over-written with a new version since it 320 * must maintain the new rack scoreboard and has had hptsi 321 * integrated as a requirment. Still todo is to eliminate the 322 * use of the callout_() system and use the hpts for all 323 * timers as well. 324 */ 325 static uint32_t bbr_rtt_probe_time = 200000; /* 200ms in micro seconds */ 326 static uint32_t bbr_rtt_probe_cwndtarg = 4; /* How many mss's outstanding */ 327 static const int32_t bbr_min_req_free = 2; /* The min we must have on the 328 * free list */ 329 static int32_t bbr_tlp_thresh = 1; 330 static int32_t bbr_reorder_thresh = 2; 331 static int32_t bbr_reorder_fade = 60000000; /* 0 - never fade, def 332 * 60,000,000 - 60 seconds */ 333 static int32_t bbr_pkt_delay = 1000; 334 static int32_t bbr_min_to = 1000; /* Number of usec's minimum timeout */ 335 static int32_t bbr_incr_timers = 1; 336 337 static int32_t bbr_tlp_min = 10000; /* 10ms in usecs */ 338 static int32_t bbr_delayed_ack_time = 200000; /* 200ms in usecs */ 339 static int32_t bbr_exit_startup_at_loss = 1; 340 341 /* 342 * bbr_lt_bw_ratio is 1/8th 343 * bbr_lt_bw_diff is < 4 Kbit/sec 344 */ 345 static uint64_t bbr_lt_bw_diff = 4000 / 8; /* In bytes per second */ 346 static uint64_t bbr_lt_bw_ratio = 8; /* For 1/8th */ 347 static uint32_t bbr_lt_bw_max_rtts = 48; /* How many rtt's do we use 348 * the lt_bw for */ 349 static uint32_t bbr_lt_intvl_min_rtts = 4; /* Min num of RTT's to measure 350 * lt_bw */ 351 static int32_t bbr_lt_intvl_fp = 0; /* False positive epoch diff */ 352 static int32_t bbr_lt_loss_thresh = 196; /* Lost vs delivered % */ 353 static int32_t bbr_lt_fd_thresh = 100; /* false detection % */ 354 355 static int32_t bbr_verbose_logging = 0; 356 /* 357 * Currently regular tcp has a rto_min of 30ms 358 * the backoff goes 12 times so that ends up 359 * being a total of 122.850 seconds before a 360 * connection is killed. 361 */ 362 static int32_t bbr_rto_min_ms = 30; /* 30ms same as main freebsd */ 363 static int32_t bbr_rto_max_sec = 4; /* 4 seconds */ 364 365 /****************************************************/ 366 /* DEFAULT TSO SIZING (cpu performance impacting) */ 367 /****************************************************/ 368 /* What amount is our formula using to get TSO size */ 369 static int32_t bbr_hptsi_per_second = 1000; 370 371 /* 372 * For hptsi under bbr_cross_over connections what is delay 373 * target 7ms (in usec) combined with a seg_max of 2 374 * gets us close to identical google behavior in 375 * TSO size selection (possibly more 1MSS sends). 376 */ 377 static int32_t bbr_hptsi_segments_delay_tar = 7000; 378 379 /* Does pacing delay include overhead's in its time calculations? */ 380 static int32_t bbr_include_enet_oh = 0; 381 static int32_t bbr_include_ip_oh = 1; 382 static int32_t bbr_include_tcp_oh = 1; 383 static int32_t bbr_google_discount = 10; 384 385 /* Do we use (nf mode) pkt-epoch to drive us or rttProp? */ 386 static int32_t bbr_state_is_pkt_epoch = 0; 387 static int32_t bbr_state_drain_2_tar = 1; 388 /* What is the max the 0 - bbr_cross_over MBPS TSO target 389 * can reach using our delay target. Note that this 390 * value becomes the floor for the cross over 391 * algorithm. 392 */ 393 static int32_t bbr_hptsi_segments_max = 2; 394 static int32_t bbr_hptsi_segments_floor = 1; 395 static int32_t bbr_hptsi_utter_max = 0; 396 397 /* What is the min the 0 - bbr_cross-over MBPS TSO target can be */ 398 static int32_t bbr_hptsi_bytes_min = 1460; 399 static int32_t bbr_all_get_min = 0; 400 401 /* Cross over point from algo-a to algo-b */ 402 static uint32_t bbr_cross_over = TWENTY_THREE_MBPS; 403 404 /* Do we deal with our restart state? */ 405 static int32_t bbr_uses_idle_restart = 0; 406 static int32_t bbr_idle_restart_threshold = 100000; /* 100ms in useconds */ 407 408 /* Do we allow hardware pacing? */ 409 static int32_t bbr_allow_hdwr_pacing = 0; 410 static int32_t bbr_hdwr_pace_adjust = 2; /* multipler when we calc the tso size */ 411 static int32_t bbr_hdwr_pace_floor = 1; 412 static int32_t bbr_hdwr_pacing_delay_cnt = 10; 413 414 /****************************************************/ 415 static int32_t bbr_resends_use_tso = 0; 416 static int32_t bbr_tlp_max_resend = 2; 417 static int32_t bbr_sack_block_limit = 128; 418 419 #define BBR_MAX_STAT 19 420 counter_u64_t bbr_state_time[BBR_MAX_STAT]; 421 counter_u64_t bbr_state_lost[BBR_MAX_STAT]; 422 counter_u64_t bbr_state_resend[BBR_MAX_STAT]; 423 counter_u64_t bbr_stat_arry[BBR_STAT_SIZE]; 424 counter_u64_t bbr_opts_arry[BBR_OPTS_SIZE]; 425 counter_u64_t bbr_out_size[TCP_MSS_ACCT_SIZE]; 426 counter_u64_t bbr_flows_whdwr_pacing; 427 counter_u64_t bbr_flows_nohdwr_pacing; 428 429 counter_u64_t bbr_nohdwr_pacing_enobuf; 430 counter_u64_t bbr_hdwr_pacing_enobuf; 431 432 static inline uint64_t bbr_get_bw(struct tcp_bbr *bbr); 433 434 /* 435 * Static defintions we need for forward declarations. 436 */ 437 static uint32_t 438 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, 439 uint32_t useconds_time, uint64_t bw); 440 static uint32_t 441 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain); 442 static void 443 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win); 444 static void 445 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses); 446 static void 447 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int line, 448 int dolog); 449 static uint32_t 450 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain); 451 static void 452 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, 453 int32_t pkt_epoch, uint32_t losses); 454 static uint32_t 455 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, 456 struct bbr_sendmap *rsm); 457 static uint32_t 458 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp); 459 static uint32_t 460 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 461 struct bbr_sendmap *rsm, uint32_t srtt, uint32_t cts); 462 static void 463 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 464 int32_t line); 465 static void 466 bbr_set_state_target(struct tcp_bbr *bbr, int line); 467 static void 468 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line); 469 static void 470 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, 471 int event, int line); 472 static void 473 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts); 474 static void 475 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts); 476 static void 477 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 478 uint32_t rtt, uint32_t line, uint8_t is_start, 479 uint16_t set); 480 static struct bbr_sendmap * 481 bbr_find_lowest_rsm(struct tcp_bbr *bbr); 482 static __inline uint32_t 483 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type); 484 static void 485 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, 486 uint8_t which); 487 static void 488 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, 489 uint32_t time_since_sent, uint32_t srtt, 490 uint32_t thresh, uint32_t to); 491 static void 492 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag); 493 static void 494 bbr_log_type_bbrsnd(struct tcp_bbr *bbr, uint32_t len, uint32_t slot, 495 uint32_t del_by, uint32_t cts, uint32_t sloton, 496 uint32_t prev_delay); 497 static void 498 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, 499 int32_t line); 500 static void 501 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr); 502 static void 503 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts); 504 static void 505 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts); 506 static void 507 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts); 508 static void 509 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 510 uint32_t cts, uint32_t usecs, uint64_t bw, 511 uint32_t override, int mod); 512 static int bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt); 513 514 static inline uint8_t 515 bbr_state_val(struct tcp_bbr *bbr) 516 { 517 return(bbr->rc_bbr_substate); 518 } 519 520 static inline uint32_t 521 get_min_cwnd(struct tcp_bbr *bbr) 522 { 523 int mss; 524 525 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 526 bbr->r_ctl.rc_pace_max_segs); 527 if (bbr_get_rtt(bbr, BBR_RTT_PROP) < BBR_HIGH_SPEED) 528 return (bbr_cwnd_min_val_hs * mss); 529 else 530 return (bbr_cwnd_min_val * mss); 531 } 532 533 static uint32_t 534 bbr_get_persists_timer_val(struct tcpcb *tp, struct tcp_bbr *bbr) 535 { 536 uint64_t srtt, var; 537 uint64_t ret_val; 538 539 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_PERSIT; 540 if (tp->t_srtt == 0) { 541 srtt = (uint64_t)BBR_INITIAL_RTO; 542 var = 0; 543 } else { 544 srtt = ((uint64_t)TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 545 var = ((uint64_t)TICKS_2_USEC(tp->t_rttvar) >> TCP_RTT_SHIFT); 546 } 547 TCPT_RANGESET_NOSLOP(ret_val, ((srtt + var) * tcp_backoff[tp->t_rxtshift]), 548 bbr_persist_min, bbr_persist_max); 549 return ((uint32_t)ret_val); 550 } 551 552 static uint32_t 553 bbr_timer_start(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 554 { 555 /* 556 * Start the FR timer, we do this based on getting the first one in 557 * the rc_tmap. Note that if its NULL we must stop the timer. in all 558 * events we need to stop the running timer (if its running) before 559 * starting the new one. 560 */ 561 uint32_t thresh, exp, to, srtt, time_since_sent, tstmp_touse; 562 int32_t idx; 563 int32_t is_tlp_timer = 0; 564 struct bbr_sendmap *rsm; 565 566 if (bbr->rc_all_timers_stopped) { 567 /* All timers have been stopped none are to run */ 568 return (0); 569 } 570 if (bbr->rc_in_persist) { 571 /* We can't start any timer in persists */ 572 return (bbr_get_persists_timer_val(tp, bbr)); 573 } 574 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 575 if ((rsm == NULL) || 576 ((tp->t_flags & TF_SACK_PERMIT) == 0) || 577 (tp->t_state < TCPS_ESTABLISHED)) { 578 /* Nothing on the send map */ 579 activate_rxt: 580 if (SEQ_LT(tp->snd_una, tp->snd_max) || 581 sbavail(&tptosocket(tp)->so_snd)) { 582 uint64_t tov; 583 584 time_since_sent = 0; 585 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 586 if (rsm) { 587 idx = rsm->r_rtr_cnt - 1; 588 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 589 tstmp_touse = rsm->r_tim_lastsent[idx]; 590 else 591 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 592 if (TSTMP_GT(tstmp_touse, cts)) 593 time_since_sent = cts - tstmp_touse; 594 } 595 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RXT; 596 if (tp->t_srtt == 0) 597 tov = BBR_INITIAL_RTO; 598 else 599 tov = ((uint64_t)(TICKS_2_USEC(tp->t_srtt) + 600 ((uint64_t)TICKS_2_USEC(tp->t_rttvar) * (uint64_t)4)) >> TCP_RTT_SHIFT); 601 if (tp->t_rxtshift) 602 tov *= tcp_backoff[tp->t_rxtshift]; 603 if (tov > time_since_sent) 604 tov -= time_since_sent; 605 else 606 tov = bbr->r_ctl.rc_min_to; 607 TCPT_RANGESET_NOSLOP(to, tov, 608 (bbr->r_ctl.rc_min_rto_ms * MS_IN_USEC), 609 (bbr->rc_max_rto_sec * USECS_IN_SECOND)); 610 bbr_log_timer_var(bbr, 2, cts, 0, bbr_get_rtt(bbr, BBR_SRTT), 0, to); 611 return (to); 612 } 613 return (0); 614 } 615 if (rsm->r_flags & BBR_ACKED) { 616 rsm = bbr_find_lowest_rsm(bbr); 617 if (rsm == NULL) { 618 /* No lowest? */ 619 goto activate_rxt; 620 } 621 } 622 /* Convert from ms to usecs */ 623 if (rsm->r_flags & BBR_SACK_PASSED) { 624 if ((tp->t_flags & TF_SENTFIN) && 625 ((tp->snd_max - tp->snd_una) == 1) && 626 (rsm->r_flags & BBR_HAS_FIN)) { 627 /* 628 * We don't start a bbr rack timer if all we have is 629 * a FIN outstanding. 630 */ 631 goto activate_rxt; 632 } 633 srtt = bbr_get_rtt(bbr, BBR_RTT_RACK); 634 thresh = bbr_calc_thresh_rack(bbr, srtt, cts, rsm); 635 idx = rsm->r_rtr_cnt - 1; 636 exp = rsm->r_tim_lastsent[idx] + thresh; 637 if (SEQ_GEQ(exp, cts)) { 638 to = exp - cts; 639 if (to < bbr->r_ctl.rc_min_to) { 640 to = bbr->r_ctl.rc_min_to; 641 } 642 } else { 643 to = bbr->r_ctl.rc_min_to; 644 } 645 } else { 646 /* Ok we need to do a TLP not RACK */ 647 if (bbr->rc_tlp_in_progress != 0) { 648 /* 649 * The previous send was a TLP. 650 */ 651 goto activate_rxt; 652 } 653 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 654 if (rsm == NULL) { 655 /* We found no rsm to TLP with. */ 656 goto activate_rxt; 657 } 658 if (rsm->r_flags & BBR_HAS_FIN) { 659 /* If its a FIN we don't do TLP */ 660 rsm = NULL; 661 goto activate_rxt; 662 } 663 time_since_sent = 0; 664 idx = rsm->r_rtr_cnt - 1; 665 if (TSTMP_GEQ(rsm->r_tim_lastsent[idx], bbr->r_ctl.rc_tlp_rxt_last_time)) 666 tstmp_touse = rsm->r_tim_lastsent[idx]; 667 else 668 tstmp_touse = bbr->r_ctl.rc_tlp_rxt_last_time; 669 if (TSTMP_GT(tstmp_touse, cts)) 670 time_since_sent = cts - tstmp_touse; 671 is_tlp_timer = 1; 672 srtt = bbr_get_rtt(bbr, bbr_tlp_type_to_use); 673 thresh = bbr_calc_thresh_tlp(tp, bbr, rsm, srtt, cts); 674 if (thresh > time_since_sent) 675 to = thresh - time_since_sent; 676 else 677 to = bbr->r_ctl.rc_min_to; 678 if (to > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 679 /* 680 * If the TLP time works out to larger than the max 681 * RTO lets not do TLP.. just RTO. 682 */ 683 goto activate_rxt; 684 } 685 if ((bbr->rc_tlp_rtx_out == 1) && 686 (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq)) { 687 /* 688 * Second retransmit of the same TLP 689 * lets not. 690 */ 691 bbr->rc_tlp_rtx_out = 0; 692 goto activate_rxt; 693 } 694 if (rsm->r_start != bbr->r_ctl.rc_last_tlp_seq) { 695 /* 696 * The tail is no longer the last one I did a probe 697 * on 698 */ 699 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 700 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 701 } 702 } 703 if (is_tlp_timer == 0) { 704 BBR_STAT_INC(bbr_to_arm_rack); 705 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_RACK; 706 } else { 707 bbr_log_timer_var(bbr, 1, cts, time_since_sent, srtt, thresh, to); 708 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 709 /* 710 * We have exceeded how many times we can retran the 711 * current TLP timer, switch to the RTO timer. 712 */ 713 goto activate_rxt; 714 } else { 715 BBR_STAT_INC(bbr_to_arm_tlp); 716 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_TLP; 717 } 718 } 719 return (to); 720 } 721 722 static inline int32_t 723 bbr_minseg(struct tcp_bbr *bbr) 724 { 725 return (bbr->r_ctl.rc_pace_min_segs - bbr->rc_last_options); 726 } 727 728 static void 729 bbr_start_hpts_timer(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts, int32_t frm, int32_t slot, uint32_t tot_len) 730 { 731 struct inpcb *inp = tptoinpcb(tp); 732 struct hpts_diag diag; 733 uint32_t delayed_ack = 0; 734 uint32_t left = 0; 735 uint32_t hpts_timeout; 736 uint8_t stopped; 737 int32_t delay_calc = 0; 738 uint32_t prev_delay = 0; 739 740 if (tcp_in_hpts(tp)) { 741 /* A previous call is already set up */ 742 return; 743 } 744 if ((tp->t_state == TCPS_CLOSED) || 745 (tp->t_state == TCPS_LISTEN)) { 746 return; 747 } 748 stopped = bbr->rc_tmr_stopped; 749 if (stopped && TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 750 left = bbr->r_ctl.rc_timer_exp - cts; 751 } 752 bbr->r_ctl.rc_hpts_flags = 0; 753 bbr->r_ctl.rc_timer_exp = 0; 754 prev_delay = bbr->r_ctl.rc_last_delay_val; 755 if (bbr->r_ctl.rc_last_delay_val && 756 (slot == 0)) { 757 /* 758 * If a previous pacer delay was in place we 759 * are not coming from the output side (where 760 * we calculate a delay, more likely a timer). 761 */ 762 slot = bbr->r_ctl.rc_last_delay_val; 763 if (TSTMP_GT(cts, bbr->rc_pacer_started)) { 764 /* Compensate for time passed */ 765 delay_calc = cts - bbr->rc_pacer_started; 766 if (delay_calc <= slot) 767 slot -= delay_calc; 768 } 769 } 770 /* Do we have early to make up for by pushing out the pacing time? */ 771 if (bbr->r_agg_early_set) { 772 bbr_log_pacing_delay_calc(bbr, 0, bbr->r_ctl.rc_agg_early, cts, slot, 0, bbr->r_agg_early_set, 2); 773 slot += bbr->r_ctl.rc_agg_early; 774 bbr->r_ctl.rc_agg_early = 0; 775 bbr->r_agg_early_set = 0; 776 } 777 /* Are we running a total debt that needs to be compensated for? */ 778 if (bbr->r_ctl.rc_hptsi_agg_delay) { 779 if (slot > bbr->r_ctl.rc_hptsi_agg_delay) { 780 /* We nuke the delay */ 781 slot -= bbr->r_ctl.rc_hptsi_agg_delay; 782 bbr->r_ctl.rc_hptsi_agg_delay = 0; 783 } else { 784 /* We nuke some of the delay, put in a minimal 100usecs */ 785 bbr->r_ctl.rc_hptsi_agg_delay -= slot; 786 bbr->r_ctl.rc_last_delay_val = slot = 100; 787 } 788 } 789 bbr->r_ctl.rc_last_delay_val = slot; 790 hpts_timeout = bbr_timer_start(tp, bbr, cts); 791 if (tp->t_flags & TF_DELACK) { 792 if (bbr->rc_in_persist == 0) { 793 delayed_ack = bbr_delack_time; 794 } else { 795 /* 796 * We are in persists and have 797 * gotten a new data element. 798 */ 799 if (hpts_timeout > bbr_delack_time) { 800 /* 801 * Lets make the persists timer (which acks) 802 * be the smaller of hpts_timeout and bbr_delack_time. 803 */ 804 hpts_timeout = bbr_delack_time; 805 } 806 } 807 } 808 if (delayed_ack && 809 ((hpts_timeout == 0) || 810 (delayed_ack < hpts_timeout))) { 811 /* We need a Delayed ack timer */ 812 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 813 hpts_timeout = delayed_ack; 814 } 815 if (slot) { 816 /* Mark that we have a pacing timer up */ 817 BBR_STAT_INC(bbr_paced_segments); 818 bbr->r_ctl.rc_hpts_flags |= PACE_PKT_OUTPUT; 819 } 820 /* 821 * If no timers are going to run and we will fall off thfe hptsi 822 * wheel, we resort to a keep-alive timer if its configured. 823 */ 824 if ((hpts_timeout == 0) && 825 (slot == 0)) { 826 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 827 (tp->t_state <= TCPS_CLOSING)) { 828 /* 829 * Ok we have no timer (persists, rack, tlp, rxt or 830 * del-ack), we don't have segments being paced. So 831 * all that is left is the keepalive timer. 832 */ 833 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 834 hpts_timeout = TICKS_2_USEC(TP_KEEPIDLE(tp)); 835 } else { 836 hpts_timeout = TICKS_2_USEC(TP_KEEPINIT(tp)); 837 } 838 bbr->r_ctl.rc_hpts_flags |= PACE_TMR_KEEP; 839 } 840 } 841 if (left && (stopped & (PACE_TMR_KEEP | PACE_TMR_DELACK)) == 842 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK)) { 843 /* 844 * RACK, TLP, persists and RXT timers all are restartable 845 * based on actions input .. i.e we received a packet (ack 846 * or sack) and that changes things (rw, or snd_una etc). 847 * Thus we can restart them with a new value. For 848 * keep-alive, delayed_ack we keep track of what was left 849 * and restart the timer with a smaller value. 850 */ 851 if (left < hpts_timeout) 852 hpts_timeout = left; 853 } 854 if (bbr->r_ctl.rc_incr_tmrs && slot && 855 (bbr->r_ctl.rc_hpts_flags & (PACE_TMR_TLP|PACE_TMR_RXT))) { 856 /* 857 * If configured to do so, and the timer is either 858 * the TLP or RXT timer, we need to increase the timeout 859 * by the pacing time. Consider the bottleneck at my 860 * machine as an example, we are sending something 861 * to start a TLP on. The last packet won't be emitted 862 * fully until the pacing time (the bottleneck will hold 863 * the data in place). Once the packet is emitted that 864 * is when we want to start waiting for the TLP. This 865 * is most evident with hardware pacing (where the nic 866 * is holding the packet(s) before emitting). But it 867 * can also show up in the network so we do it for all 868 * cases. Technically we would take off one packet from 869 * this extra delay but this is easier and being more 870 * conservative is probably better. 871 */ 872 hpts_timeout += slot; 873 } 874 if (hpts_timeout) { 875 /* 876 * Hack alert for now we can't time-out over 2147 seconds (a 877 * bit more than 35min) 878 */ 879 if (hpts_timeout > 0x7ffffffe) 880 hpts_timeout = 0x7ffffffe; 881 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 882 } else 883 bbr->r_ctl.rc_timer_exp = 0; 884 if ((slot) && 885 (bbr->rc_use_google || 886 bbr->output_error_seen || 887 (slot <= hpts_timeout)) ) { 888 /* 889 * Tell LRO that it can queue packets while 890 * we pace. 891 */ 892 bbr->rc_tp->t_flags2 |= TF2_MBUF_QUEUE_READY; 893 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 894 (bbr->rc_cwnd_limited == 0)) { 895 /* 896 * If we are not cwnd limited and we 897 * are running a rack timer we put on 898 * the do not disturbe even for sack. 899 */ 900 tp->t_flags2 |= TF2_DONT_SACK_QUEUE; 901 } else 902 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 903 bbr->rc_pacer_started = cts; 904 905 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(slot), 906 __LINE__, &diag); 907 bbr->rc_timer_first = 0; 908 bbr->bbr_timer_src = frm; 909 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 1); 910 bbr_log_hpts_diag(bbr, cts, &diag); 911 } else if (hpts_timeout) { 912 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(hpts_timeout), 913 __LINE__, &diag); 914 /* 915 * We add the flag here as well if the slot is set, 916 * since hpts will call in to clear the queue first before 917 * calling the output routine (which does our timers). 918 * We don't want to set the flag if its just a timer 919 * else the arrival of data might (that causes us 920 * to send more) might get delayed. Imagine being 921 * on a keep-alive timer and a request comes in for 922 * more data. 923 */ 924 if (slot) 925 bbr->rc_pacer_started = cts; 926 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) && 927 (bbr->rc_cwnd_limited == 0)) { 928 /* 929 * For a rack timer, don't wake us even 930 * if a sack arrives as long as we are 931 * not cwnd limited. 932 */ 933 tp->t_flags2 |= (TF2_MBUF_QUEUE_READY | 934 TF2_DONT_SACK_QUEUE); 935 } else { 936 /* All other timers wake us up */ 937 tp->t_flags2 &= ~(TF2_MBUF_QUEUE_READY | 938 TF2_DONT_SACK_QUEUE); 939 } 940 bbr->bbr_timer_src = frm; 941 bbr_log_to_start(bbr, cts, hpts_timeout, slot, 0); 942 bbr_log_hpts_diag(bbr, cts, &diag); 943 bbr->rc_timer_first = 1; 944 } 945 bbr->rc_tmr_stopped = 0; 946 bbr_log_type_bbrsnd(bbr, tot_len, slot, delay_calc, cts, frm, prev_delay); 947 } 948 949 static void 950 bbr_timer_audit(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, struct sockbuf *sb) 951 { 952 /* 953 * We received an ack, and then did not call send or were bounced 954 * out due to the hpts was running. Now a timer is up as well, is it 955 * the right timer? 956 */ 957 struct inpcb *inp; 958 struct bbr_sendmap *rsm; 959 uint32_t hpts_timeout; 960 int tmr_up; 961 962 tmr_up = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 963 if (bbr->rc_in_persist && (tmr_up == PACE_TMR_PERSIT)) 964 return; 965 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 966 if (((rsm == NULL) || (tp->t_state < TCPS_ESTABLISHED)) && 967 (tmr_up == PACE_TMR_RXT)) { 968 /* Should be an RXT */ 969 return; 970 } 971 inp = bbr->rc_inp; 972 if (rsm == NULL) { 973 /* Nothing outstanding? */ 974 if (tp->t_flags & TF_DELACK) { 975 if (tmr_up == PACE_TMR_DELACK) 976 /* 977 * We are supposed to have delayed ack up 978 * and we do 979 */ 980 return; 981 } else if (((V_tcp_always_keepalive || 982 inp->inp_socket->so_options & SO_KEEPALIVE) && 983 (tp->t_state <= TCPS_CLOSING)) && 984 (tmr_up == PACE_TMR_KEEP) && 985 (tp->snd_max == tp->snd_una)) { 986 /* We should have keep alive up and we do */ 987 return; 988 } 989 } 990 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) { 991 if ((tp->t_flags & TF_SENTFIN) && 992 ((tp->snd_max - tp->snd_una) == 1) && 993 (rsm->r_flags & BBR_HAS_FIN)) { 994 /* needs to be a RXT */ 995 if (tmr_up == PACE_TMR_RXT) 996 return; 997 else 998 goto wrong_timer; 999 } else if (tmr_up == PACE_TMR_RACK) 1000 return; 1001 else 1002 goto wrong_timer; 1003 } else if (rsm && (tmr_up == PACE_TMR_RACK)) { 1004 /* Rack timer has priority if we have data out */ 1005 return; 1006 } else if (SEQ_GT(tp->snd_max, tp->snd_una) && 1007 ((tmr_up == PACE_TMR_TLP) || 1008 (tmr_up == PACE_TMR_RXT))) { 1009 /* 1010 * Either a TLP or RXT is fine if no sack-passed is in place 1011 * and data is outstanding. 1012 */ 1013 return; 1014 } else if (tmr_up == PACE_TMR_DELACK) { 1015 /* 1016 * If the delayed ack was going to go off before the 1017 * rtx/tlp/rack timer were going to expire, then that would 1018 * be the timer in control. Note we don't check the time 1019 * here trusting the code is correct. 1020 */ 1021 return; 1022 } 1023 if (SEQ_GT(tp->snd_max, tp->snd_una) && 1024 ((tmr_up == PACE_TMR_RXT) || 1025 (tmr_up == PACE_TMR_TLP) || 1026 (tmr_up == PACE_TMR_RACK))) { 1027 /* 1028 * We have outstanding data and 1029 * we *do* have a RACK, TLP or RXT 1030 * timer running. We won't restart 1031 * anything here since thats probably ok we 1032 * will get called with some timer here shortly. 1033 */ 1034 return; 1035 } 1036 /* 1037 * Ok the timer originally started is not what we want now. We will 1038 * force the hpts to be stopped if any, and restart with the slot 1039 * set to what was in the saved slot. 1040 */ 1041 wrong_timer: 1042 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) { 1043 if (tcp_in_hpts(tp)) 1044 tcp_hpts_remove(tp); 1045 bbr_timer_cancel(bbr, __LINE__, cts); 1046 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val, 1047 0); 1048 } else { 1049 /* 1050 * Output is hptsi so we just need to switch the type of 1051 * timer. We don't bother with keep-alive, since when we 1052 * jump through the output, it will start the keep-alive if 1053 * nothing is sent. 1054 * 1055 * We only need a delayed-ack added and or the hpts_timeout. 1056 */ 1057 hpts_timeout = bbr_timer_start(tp, bbr, cts); 1058 if (tp->t_flags & TF_DELACK) { 1059 if (hpts_timeout == 0) { 1060 hpts_timeout = bbr_delack_time; 1061 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1062 } 1063 else if (hpts_timeout > bbr_delack_time) { 1064 hpts_timeout = bbr_delack_time; 1065 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1066 } 1067 } 1068 if (hpts_timeout) { 1069 if (hpts_timeout > 0x7ffffffe) 1070 hpts_timeout = 0x7ffffffe; 1071 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 1072 } 1073 } 1074 } 1075 1076 int32_t bbr_clear_lost = 0; 1077 1078 /* 1079 * Considers the two time values now (cts) and earlier. 1080 * If cts is smaller than earlier, we could have 1081 * had a sequence wrap (our counter wraps every 1082 * 70 min or so) or it could be just clock skew 1083 * getting us two different time values. Clock skew 1084 * will show up within 10ms or so. So in such 1085 * a case (where cts is behind earlier time by 1086 * less than 10ms) we return 0. Otherwise we 1087 * return the true difference between them. 1088 */ 1089 static inline uint32_t 1090 bbr_calc_time(uint32_t cts, uint32_t earlier_time) { 1091 /* 1092 * Given two timestamps, the current time stamp cts, and some other 1093 * time-stamp taken in theory earlier return the difference. The 1094 * trick is here sometimes locking will get the other timestamp 1095 * after the cts. If this occurs we need to return 0. 1096 */ 1097 if (TSTMP_GEQ(cts, earlier_time)) 1098 return (cts - earlier_time); 1099 /* 1100 * cts is behind earlier_time if its less than 10ms consider it 0. 1101 * If its more than 10ms difference then we had a time wrap. Else 1102 * its just the normal locking foo. I wonder if we should not go to 1103 * 64bit TS and get rid of this issue. 1104 */ 1105 if (TSTMP_GEQ((cts + 10000), earlier_time)) 1106 return (0); 1107 /* 1108 * Ok the time must have wrapped. So we need to answer a large 1109 * amount of time, which the normal subtraction should do. 1110 */ 1111 return (cts - earlier_time); 1112 } 1113 1114 static int 1115 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS) 1116 { 1117 uint32_t stat; 1118 int32_t error; 1119 1120 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t)); 1121 if (error || req->newptr == NULL) 1122 return error; 1123 1124 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 1125 if (error) 1126 return (error); 1127 if (stat == 1) { 1128 #ifdef BBR_INVARIANTS 1129 printf("Clearing BBR lost counters\n"); 1130 #endif 1131 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT); 1132 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT); 1133 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT); 1134 } else if (stat == 2) { 1135 #ifdef BBR_INVARIANTS 1136 printf("Clearing BBR option counters\n"); 1137 #endif 1138 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE); 1139 } else if (stat == 3) { 1140 #ifdef BBR_INVARIANTS 1141 printf("Clearing BBR stats counters\n"); 1142 #endif 1143 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE); 1144 } else if (stat == 4) { 1145 #ifdef BBR_INVARIANTS 1146 printf("Clearing BBR out-size counters\n"); 1147 #endif 1148 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE); 1149 } 1150 bbr_clear_lost = 0; 1151 return (0); 1152 } 1153 1154 static void 1155 bbr_init_sysctls(void) 1156 { 1157 struct sysctl_oid *bbr_probertt; 1158 struct sysctl_oid *bbr_hptsi; 1159 struct sysctl_oid *bbr_measure; 1160 struct sysctl_oid *bbr_cwnd; 1161 struct sysctl_oid *bbr_timeout; 1162 struct sysctl_oid *bbr_states; 1163 struct sysctl_oid *bbr_startup; 1164 struct sysctl_oid *bbr_policer; 1165 1166 /* Probe rtt controls */ 1167 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1168 SYSCTL_CHILDREN(bbr_sysctl_root), 1169 OID_AUTO, 1170 "probertt", 1171 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1172 ""); 1173 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1174 SYSCTL_CHILDREN(bbr_probertt), 1175 OID_AUTO, "gain", CTLFLAG_RW, 1176 &bbr_rttprobe_gain, 192, 1177 "What is the filter gain drop in probe_rtt (0=disable)?"); 1178 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1179 SYSCTL_CHILDREN(bbr_probertt), 1180 OID_AUTO, "cwnd", CTLFLAG_RW, 1181 &bbr_rtt_probe_cwndtarg, 4, 1182 "How many mss's are outstanding during probe-rtt"); 1183 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1184 SYSCTL_CHILDREN(bbr_probertt), 1185 OID_AUTO, "int", CTLFLAG_RW, 1186 &bbr_rtt_probe_limit, 4000000, 1187 "If RTT has not shrank in this many micro-seconds enter probe-rtt"); 1188 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1189 SYSCTL_CHILDREN(bbr_probertt), 1190 OID_AUTO, "mintime", CTLFLAG_RW, 1191 &bbr_rtt_probe_time, 200000, 1192 "How many microseconds in probe-rtt"); 1193 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1194 SYSCTL_CHILDREN(bbr_probertt), 1195 OID_AUTO, "filter_len_sec", CTLFLAG_RW, 1196 &bbr_filter_len_sec, 6, 1197 "How long in seconds does the rttProp filter run?"); 1198 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1199 SYSCTL_CHILDREN(bbr_probertt), 1200 OID_AUTO, "drain_rtt", CTLFLAG_RW, 1201 &bbr_drain_rtt, BBR_SRTT, 1202 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?"); 1203 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1204 SYSCTL_CHILDREN(bbr_probertt), 1205 OID_AUTO, "can_force", CTLFLAG_RW, 1206 &bbr_can_force_probertt, 0, 1207 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??"); 1208 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1209 SYSCTL_CHILDREN(bbr_probertt), 1210 OID_AUTO, "enter_sets_force", CTLFLAG_RW, 1211 &bbr_probertt_sets_rtt, 0, 1212 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?"); 1213 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1214 SYSCTL_CHILDREN(bbr_probertt), 1215 OID_AUTO, "can_adjust", CTLFLAG_RW, 1216 &bbr_can_adjust_probertt, 1, 1217 "Can we dynamically adjust the probe-rtt limits and times?"); 1218 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1219 SYSCTL_CHILDREN(bbr_probertt), 1220 OID_AUTO, "is_ratio", CTLFLAG_RW, 1221 &bbr_is_ratio, 0, 1222 "is the limit to filter a ratio?"); 1223 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1224 SYSCTL_CHILDREN(bbr_probertt), 1225 OID_AUTO, "use_cwnd", CTLFLAG_RW, 1226 &bbr_prtt_slam_cwnd, 0, 1227 "Should we set/recover cwnd?"); 1228 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1229 SYSCTL_CHILDREN(bbr_probertt), 1230 OID_AUTO, "can_use_ts", CTLFLAG_RW, 1231 &bbr_can_use_ts_for_rtt, 1, 1232 "Can we use the ms timestamp if available for retransmistted rtt calculations?"); 1233 1234 /* Pacing controls */ 1235 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1236 SYSCTL_CHILDREN(bbr_sysctl_root), 1237 OID_AUTO, 1238 "pacing", 1239 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1240 ""); 1241 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1242 SYSCTL_CHILDREN(bbr_hptsi), 1243 OID_AUTO, "hw_pacing", CTLFLAG_RW, 1244 &bbr_allow_hdwr_pacing, 1, 1245 "Do we allow hardware pacing?"); 1246 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1247 SYSCTL_CHILDREN(bbr_hptsi), 1248 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW, 1249 &bbr_hardware_pacing_limit, 4000, 1250 "Do we have a limited number of connections for pacing chelsio (0=no limit)?"); 1251 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1252 SYSCTL_CHILDREN(bbr_hptsi), 1253 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW, 1254 &bbr_hdwr_pace_adjust, 2, 1255 "Multiplier to calculated tso size?"); 1256 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1257 SYSCTL_CHILDREN(bbr_hptsi), 1258 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW, 1259 &bbr_hdwr_pace_floor, 1, 1260 "Do we invoke the hardware pacing floor?"); 1261 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1262 SYSCTL_CHILDREN(bbr_hptsi), 1263 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW, 1264 &bbr_hdwr_pacing_delay_cnt, 10, 1265 "How many packets must be sent after hdwr pacing is enabled"); 1266 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1267 SYSCTL_CHILDREN(bbr_hptsi), 1268 OID_AUTO, "bw_cross", CTLFLAG_RW, 1269 &bbr_cross_over, 3000000, 1270 "What is the point where we cross over to linux like TSO size set"); 1271 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1272 SYSCTL_CHILDREN(bbr_hptsi), 1273 OID_AUTO, "seg_deltarg", CTLFLAG_RW, 1274 &bbr_hptsi_segments_delay_tar, 7000, 1275 "What is the worse case delay target for hptsi < 48Mbp connections"); 1276 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1277 SYSCTL_CHILDREN(bbr_hptsi), 1278 OID_AUTO, "enet_oh", CTLFLAG_RW, 1279 &bbr_include_enet_oh, 0, 1280 "Do we include the ethernet overhead in calculating pacing delay?"); 1281 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1282 SYSCTL_CHILDREN(bbr_hptsi), 1283 OID_AUTO, "ip_oh", CTLFLAG_RW, 1284 &bbr_include_ip_oh, 1, 1285 "Do we include the IP overhead in calculating pacing delay?"); 1286 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1287 SYSCTL_CHILDREN(bbr_hptsi), 1288 OID_AUTO, "tcp_oh", CTLFLAG_RW, 1289 &bbr_include_tcp_oh, 0, 1290 "Do we include the TCP overhead in calculating pacing delay?"); 1291 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1292 SYSCTL_CHILDREN(bbr_hptsi), 1293 OID_AUTO, "google_discount", CTLFLAG_RW, 1294 &bbr_google_discount, 10, 1295 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?"); 1296 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1297 SYSCTL_CHILDREN(bbr_hptsi), 1298 OID_AUTO, "all_get_min", CTLFLAG_RW, 1299 &bbr_all_get_min, 0, 1300 "If you are less than a MSS do you just get the min?"); 1301 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1302 SYSCTL_CHILDREN(bbr_hptsi), 1303 OID_AUTO, "tso_min", CTLFLAG_RW, 1304 &bbr_hptsi_bytes_min, 1460, 1305 "For 0 -> 24Mbps what is floor number of segments for TSO"); 1306 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1307 SYSCTL_CHILDREN(bbr_hptsi), 1308 OID_AUTO, "seg_tso_max", CTLFLAG_RW, 1309 &bbr_hptsi_segments_max, 6, 1310 "For 0 -> 24Mbps what is top number of segments for TSO"); 1311 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1312 SYSCTL_CHILDREN(bbr_hptsi), 1313 OID_AUTO, "seg_floor", CTLFLAG_RW, 1314 &bbr_hptsi_segments_floor, 1, 1315 "Minimum TSO size we will fall too in segments"); 1316 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1317 SYSCTL_CHILDREN(bbr_hptsi), 1318 OID_AUTO, "utter_max", CTLFLAG_RW, 1319 &bbr_hptsi_utter_max, 0, 1320 "The absolute maximum that any pacing (outside of hardware) can be"); 1321 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1322 SYSCTL_CHILDREN(bbr_hptsi), 1323 OID_AUTO, "seg_divisor", CTLFLAG_RW, 1324 &bbr_hptsi_per_second, 100, 1325 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps "); 1326 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1327 SYSCTL_CHILDREN(bbr_hptsi), 1328 OID_AUTO, "srtt_mul", CTLFLAG_RW, 1329 &bbr_hptsi_max_mul, 1, 1330 "The multiplier for pace len max"); 1331 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1332 SYSCTL_CHILDREN(bbr_hptsi), 1333 OID_AUTO, "srtt_div", CTLFLAG_RW, 1334 &bbr_hptsi_max_div, 2, 1335 "The divisor for pace len max"); 1336 /* Measurement controls */ 1337 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1338 SYSCTL_CHILDREN(bbr_sysctl_root), 1339 OID_AUTO, 1340 "measure", 1341 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1342 "Measurement controls"); 1343 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1344 SYSCTL_CHILDREN(bbr_measure), 1345 OID_AUTO, "min_i_bw", CTLFLAG_RW, 1346 &bbr_initial_bw_bps, 62500, 1347 "Minimum initial b/w in bytes per second"); 1348 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1349 SYSCTL_CHILDREN(bbr_measure), 1350 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1351 &bbr_sack_not_required, 0, 1352 "Do we allow bbr to run on connections not supporting SACK?"); 1353 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1354 SYSCTL_CHILDREN(bbr_measure), 1355 OID_AUTO, "use_google", CTLFLAG_RW, 1356 &bbr_use_google_algo, 0, 1357 "Use has close to google V1.0 has possible?"); 1358 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1359 SYSCTL_CHILDREN(bbr_measure), 1360 OID_AUTO, "ts_limiting", CTLFLAG_RW, 1361 &bbr_ts_limiting, 1, 1362 "Do we attempt to use the peers timestamp to limit b/w caculations?"); 1363 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1364 SYSCTL_CHILDREN(bbr_measure), 1365 OID_AUTO, "ts_can_raise", CTLFLAG_RW, 1366 &bbr_ts_can_raise, 0, 1367 "Can we raise the b/w via timestamp b/w calculation?"); 1368 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1369 SYSCTL_CHILDREN(bbr_measure), 1370 OID_AUTO, "ts_delta", CTLFLAG_RW, 1371 &bbr_min_usec_delta, 20000, 1372 "How long in usec between ts of our sends in ts validation code?"); 1373 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1374 SYSCTL_CHILDREN(bbr_measure), 1375 OID_AUTO, "ts_peer_delta", CTLFLAG_RW, 1376 &bbr_min_peer_delta, 20, 1377 "What min numerical value should be between the peer deltas?"); 1378 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1379 SYSCTL_CHILDREN(bbr_measure), 1380 OID_AUTO, "ts_delta_percent", CTLFLAG_RW, 1381 &bbr_delta_percent, 150, 1382 "What percentage (150 = 15.0) do we allow variance for?"); 1383 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1384 SYSCTL_CHILDREN(bbr_measure), 1385 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW, 1386 &bbr_min_measurements_req, 1, 1387 "What is the minimum measurement count we need before we switch to our b/w estimate"); 1388 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1389 SYSCTL_CHILDREN(bbr_measure), 1390 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW, 1391 &bbr_no_pacing_until, 4, 1392 "How many pkt-epoch's (0 is off) do we need before pacing is on?"); 1393 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1394 SYSCTL_CHILDREN(bbr_measure), 1395 OID_AUTO, "quanta", CTLFLAG_RW, 1396 &bbr_quanta, 2, 1397 "Extra quanta to add when calculating the target (ID section 4.2.3.2)."); 1398 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1399 SYSCTL_CHILDREN(bbr_measure), 1400 OID_AUTO, "noretran", CTLFLAG_RW, 1401 &bbr_no_retran, 0, 1402 "Should google mode not use retransmission measurements for the b/w estimation?"); 1403 /* State controls */ 1404 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1405 SYSCTL_CHILDREN(bbr_sysctl_root), 1406 OID_AUTO, 1407 "states", 1408 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1409 "State controls"); 1410 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1411 SYSCTL_CHILDREN(bbr_states), 1412 OID_AUTO, "idle_restart", CTLFLAG_RW, 1413 &bbr_uses_idle_restart, 0, 1414 "Do we use a new special idle_restart state to ramp back up quickly?"); 1415 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1416 SYSCTL_CHILDREN(bbr_states), 1417 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW, 1418 &bbr_idle_restart_threshold, 100000, 1419 "How long must we be idle before we restart??"); 1420 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1421 SYSCTL_CHILDREN(bbr_states), 1422 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW, 1423 &bbr_state_is_pkt_epoch, 0, 1424 "Do we use a pkt-epoch for substate if 0 rttProp?"); 1425 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1426 SYSCTL_CHILDREN(bbr_states), 1427 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW, 1428 &bbr_rtt_gain_thresh, 0, 1429 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?"); 1430 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1431 SYSCTL_CHILDREN(bbr_states), 1432 OID_AUTO, "drain_floor", CTLFLAG_RW, 1433 &bbr_drain_floor, 88, 1434 "What is the lowest we can drain (pg) too?"); 1435 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1436 SYSCTL_CHILDREN(bbr_states), 1437 OID_AUTO, "drain_2_target", CTLFLAG_RW, 1438 &bbr_state_drain_2_tar, 1, 1439 "Do we drain to target in drain substate?"); 1440 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1441 SYSCTL_CHILDREN(bbr_states), 1442 OID_AUTO, "gain_2_target", CTLFLAG_RW, 1443 &bbr_gain_to_target, 1, 1444 "Does probe bw gain to target??"); 1445 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1446 SYSCTL_CHILDREN(bbr_states), 1447 OID_AUTO, "gain_extra_time", CTLFLAG_RW, 1448 &bbr_gain_gets_extra_too, 1, 1449 "Does probe bw gain get the extra time too?"); 1450 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1451 SYSCTL_CHILDREN(bbr_states), 1452 OID_AUTO, "ld_div", CTLFLAG_RW, 1453 &bbr_drain_drop_div, 5, 1454 "Long drain drop divider?"); 1455 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1456 SYSCTL_CHILDREN(bbr_states), 1457 OID_AUTO, "ld_mul", CTLFLAG_RW, 1458 &bbr_drain_drop_mul, 4, 1459 "Long drain drop multiplier?"); 1460 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1461 SYSCTL_CHILDREN(bbr_states), 1462 OID_AUTO, "rand_ot_disc", CTLFLAG_RW, 1463 &bbr_rand_ot, 50, 1464 "Random discount of the ot?"); 1465 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1466 SYSCTL_CHILDREN(bbr_states), 1467 OID_AUTO, "dr_filter_life", CTLFLAG_RW, 1468 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT, 1469 "How many packet-epochs does the b/w delivery rate last?"); 1470 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1471 SYSCTL_CHILDREN(bbr_states), 1472 OID_AUTO, "subdrain_applimited", CTLFLAG_RW, 1473 &bbr_sub_drain_app_limit, 0, 1474 "Does our sub-state drain invoke app limited if its long?"); 1475 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1476 SYSCTL_CHILDREN(bbr_states), 1477 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW, 1478 &bbr_sub_drain_slam_cwnd, 0, 1479 "Should we set/recover cwnd for sub-state drain?"); 1480 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1481 SYSCTL_CHILDREN(bbr_states), 1482 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW, 1483 &bbr_slam_cwnd_in_main_drain, 0, 1484 "Should we set/recover cwnd for main-state drain?"); 1485 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1486 SYSCTL_CHILDREN(bbr_states), 1487 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW, 1488 &google_allow_early_out, 1, 1489 "Should we allow google probe-bw/drain to exit early at flight target?"); 1490 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1491 SYSCTL_CHILDREN(bbr_states), 1492 OID_AUTO, "google_exit_loss", CTLFLAG_RW, 1493 &google_consider_lost, 1, 1494 "Should we have losses exit gain of probebw in google mode??"); 1495 /* Startup controls */ 1496 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1497 SYSCTL_CHILDREN(bbr_sysctl_root), 1498 OID_AUTO, 1499 "startup", 1500 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1501 "Startup controls"); 1502 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1503 SYSCTL_CHILDREN(bbr_startup), 1504 OID_AUTO, "cheat_iwnd", CTLFLAG_RW, 1505 &bbr_sends_full_iwnd, 1, 1506 "Do we not pace but burst out initial windows has our TSO size?"); 1507 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1508 SYSCTL_CHILDREN(bbr_startup), 1509 OID_AUTO, "loss_threshold", CTLFLAG_RW, 1510 &bbr_startup_loss_thresh, 2000, 1511 "In startup what is the loss threshold in a pe that will exit us from startup?"); 1512 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1513 SYSCTL_CHILDREN(bbr_startup), 1514 OID_AUTO, "use_lowerpg", CTLFLAG_RW, 1515 &bbr_use_lower_gain_in_startup, 1, 1516 "Should we use a lower hptsi gain if we see loss in startup?"); 1517 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1518 SYSCTL_CHILDREN(bbr_startup), 1519 OID_AUTO, "gain", CTLFLAG_RW, 1520 &bbr_start_exit, 25, 1521 "What gain percent do we need to see to stay in startup??"); 1522 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1523 SYSCTL_CHILDREN(bbr_startup), 1524 OID_AUTO, "low_gain", CTLFLAG_RW, 1525 &bbr_low_start_exit, 15, 1526 "What gain percent do we need to see to stay in the lower gain startup??"); 1527 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1528 SYSCTL_CHILDREN(bbr_startup), 1529 OID_AUTO, "loss_exit", CTLFLAG_RW, 1530 &bbr_exit_startup_at_loss, 1, 1531 "Should we exit startup at loss in an epoch if we are not gaining?"); 1532 /* CWND controls */ 1533 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1534 SYSCTL_CHILDREN(bbr_sysctl_root), 1535 OID_AUTO, 1536 "cwnd", 1537 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1538 "Cwnd controls"); 1539 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1540 SYSCTL_CHILDREN(bbr_cwnd), 1541 OID_AUTO, "tar_rtt", CTLFLAG_RW, 1542 &bbr_cwndtarget_rtt_touse, 0, 1543 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?"); 1544 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1545 SYSCTL_CHILDREN(bbr_cwnd), 1546 OID_AUTO, "may_shrink", CTLFLAG_RW, 1547 &bbr_cwnd_may_shrink, 0, 1548 "Can the cwnd shrink if it would grow to more than the target?"); 1549 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1550 SYSCTL_CHILDREN(bbr_cwnd), 1551 OID_AUTO, "max_target_limit", CTLFLAG_RW, 1552 &bbr_target_cwnd_mult_limit, 8, 1553 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?"); 1554 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1555 SYSCTL_CHILDREN(bbr_cwnd), 1556 OID_AUTO, "highspeed_min", CTLFLAG_RW, 1557 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS, 1558 "What is the high-speed min cwnd (rttProp under 1ms)"); 1559 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1560 SYSCTL_CHILDREN(bbr_cwnd), 1561 OID_AUTO, "lowspeed_min", CTLFLAG_RW, 1562 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS, 1563 "What is the min cwnd (rttProp > 1ms)"); 1564 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1565 SYSCTL_CHILDREN(bbr_cwnd), 1566 OID_AUTO, "initwin", CTLFLAG_RW, 1567 &bbr_def_init_win, 10, 1568 "What is the BBR initial window, if 0 use tcp version"); 1569 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1570 SYSCTL_CHILDREN(bbr_cwnd), 1571 OID_AUTO, "do_loss_red", CTLFLAG_RW, 1572 &bbr_do_red, 600, 1573 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?"); 1574 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1575 SYSCTL_CHILDREN(bbr_cwnd), 1576 OID_AUTO, "red_scale", CTLFLAG_RW, 1577 &bbr_red_scale, 20000, 1578 "What RTT do we scale with?"); 1579 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1580 SYSCTL_CHILDREN(bbr_cwnd), 1581 OID_AUTO, "red_growslow", CTLFLAG_RW, 1582 &bbr_red_growth_restrict, 1, 1583 "Do we restrict cwnd growth for whats in flight?"); 1584 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1585 SYSCTL_CHILDREN(bbr_cwnd), 1586 OID_AUTO, "red_div", CTLFLAG_RW, 1587 &bbr_red_div, 2, 1588 "If we reduce whats the divisor?"); 1589 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1590 SYSCTL_CHILDREN(bbr_cwnd), 1591 OID_AUTO, "red_mul", CTLFLAG_RW, 1592 &bbr_red_mul, 1, 1593 "If we reduce whats the mulitiplier?"); 1594 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1595 SYSCTL_CHILDREN(bbr_cwnd), 1596 OID_AUTO, "target_is_unit", CTLFLAG_RW, 1597 &bbr_target_is_bbunit, 0, 1598 "Is the state target the pacing_gain or BBR_UNIT?"); 1599 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1600 SYSCTL_CHILDREN(bbr_cwnd), 1601 OID_AUTO, "drop_limit", CTLFLAG_RW, 1602 &bbr_drop_limit, 0, 1603 "Number of segments limit for drop (0=use min_cwnd w/flight)?"); 1604 1605 /* Timeout controls */ 1606 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1607 SYSCTL_CHILDREN(bbr_sysctl_root), 1608 OID_AUTO, 1609 "timeout", 1610 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1611 "Time out controls"); 1612 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1613 SYSCTL_CHILDREN(bbr_timeout), 1614 OID_AUTO, "delack", CTLFLAG_RW, 1615 &bbr_delack_time, 100000, 1616 "BBR's delayed ack time"); 1617 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1618 SYSCTL_CHILDREN(bbr_timeout), 1619 OID_AUTO, "tlp_uses", CTLFLAG_RW, 1620 &bbr_tlp_type_to_use, 3, 1621 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt"); 1622 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1623 SYSCTL_CHILDREN(bbr_timeout), 1624 OID_AUTO, "persmin", CTLFLAG_RW, 1625 &bbr_persist_min, 250000, 1626 "What is the minimum time in microseconds between persists"); 1627 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1628 SYSCTL_CHILDREN(bbr_timeout), 1629 OID_AUTO, "persmax", CTLFLAG_RW, 1630 &bbr_persist_max, 1000000, 1631 "What is the largest delay in microseconds between persists"); 1632 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1633 SYSCTL_CHILDREN(bbr_timeout), 1634 OID_AUTO, "tlp_minto", CTLFLAG_RW, 1635 &bbr_tlp_min, 10000, 1636 "TLP Min timeout in usecs"); 1637 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1638 SYSCTL_CHILDREN(bbr_timeout), 1639 OID_AUTO, "tlp_dack_time", CTLFLAG_RW, 1640 &bbr_delayed_ack_time, 200000, 1641 "TLP delayed ack compensation value"); 1642 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1643 SYSCTL_CHILDREN(bbr_sysctl_root), 1644 OID_AUTO, "minrto", CTLFLAG_RW, 1645 &bbr_rto_min_ms, 30, 1646 "Minimum RTO in ms"); 1647 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1648 SYSCTL_CHILDREN(bbr_timeout), 1649 OID_AUTO, "maxrto", CTLFLAG_RW, 1650 &bbr_rto_max_sec, 4, 1651 "Maximum RTO in seconds -- should be at least as large as min_rto"); 1652 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1653 SYSCTL_CHILDREN(bbr_timeout), 1654 OID_AUTO, "tlp_retry", CTLFLAG_RW, 1655 &bbr_tlp_max_resend, 2, 1656 "How many times does TLP retry a single segment or multiple with no ACK"); 1657 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1658 SYSCTL_CHILDREN(bbr_timeout), 1659 OID_AUTO, "minto", CTLFLAG_RW, 1660 &bbr_min_to, 1000, 1661 "Minimum rack timeout in useconds"); 1662 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1663 SYSCTL_CHILDREN(bbr_timeout), 1664 OID_AUTO, "pktdelay", CTLFLAG_RW, 1665 &bbr_pkt_delay, 1000, 1666 "Extra RACK time (in useconds) besides reordering thresh"); 1667 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1668 SYSCTL_CHILDREN(bbr_timeout), 1669 OID_AUTO, "incr_tmrs", CTLFLAG_RW, 1670 &bbr_incr_timers, 1, 1671 "Increase the RXT/TLP timer by the pacing time used?"); 1672 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1673 SYSCTL_CHILDREN(bbr_timeout), 1674 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW, 1675 &bbr_marks_rxt_sack_passed, 0, 1676 "Mark sack passed on all those not ack'd when a RXT hits?"); 1677 /* Policer controls */ 1678 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1679 SYSCTL_CHILDREN(bbr_sysctl_root), 1680 OID_AUTO, 1681 "policer", 1682 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1683 "Policer controls"); 1684 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1685 SYSCTL_CHILDREN(bbr_policer), 1686 OID_AUTO, "detect_enable", CTLFLAG_RW, 1687 &bbr_policer_detection_enabled, 1, 1688 "Is policer detection enabled??"); 1689 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1690 SYSCTL_CHILDREN(bbr_policer), 1691 OID_AUTO, "min_pes", CTLFLAG_RW, 1692 &bbr_lt_intvl_min_rtts, 4, 1693 "Minimum number of PE's?"); 1694 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1695 SYSCTL_CHILDREN(bbr_policer), 1696 OID_AUTO, "bwdiff", CTLFLAG_RW, 1697 &bbr_lt_bw_diff, (4000/8), 1698 "Minimal bw diff?"); 1699 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1700 SYSCTL_CHILDREN(bbr_policer), 1701 OID_AUTO, "bwratio", CTLFLAG_RW, 1702 &bbr_lt_bw_ratio, 8, 1703 "Minimal bw diff?"); 1704 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1705 SYSCTL_CHILDREN(bbr_policer), 1706 OID_AUTO, "from_rack_rxt", CTLFLAG_RW, 1707 &bbr_policer_call_from_rack_to, 0, 1708 "Do we call the policer detection code from a rack-timeout?"); 1709 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1710 SYSCTL_CHILDREN(bbr_policer), 1711 OID_AUTO, "false_postive", CTLFLAG_RW, 1712 &bbr_lt_intvl_fp, 0, 1713 "What packet epoch do we do false-positive detection at (0=no)?"); 1714 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1715 SYSCTL_CHILDREN(bbr_policer), 1716 OID_AUTO, "loss_thresh", CTLFLAG_RW, 1717 &bbr_lt_loss_thresh, 196, 1718 "Loss threshold 196 = 19.6%?"); 1719 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1720 SYSCTL_CHILDREN(bbr_policer), 1721 OID_AUTO, "false_postive_thresh", CTLFLAG_RW, 1722 &bbr_lt_fd_thresh, 100, 1723 "What percentage is the false detection threshold (150=15.0)?"); 1724 /* All the rest */ 1725 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1726 SYSCTL_CHILDREN(bbr_sysctl_root), 1727 OID_AUTO, "cheat_rxt", CTLFLAG_RW, 1728 &bbr_use_rack_resend_cheat, 0, 1729 "Do we burst 1ms between sends on retransmissions (like rack)?"); 1730 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1731 SYSCTL_CHILDREN(bbr_sysctl_root), 1732 OID_AUTO, "error_paceout", CTLFLAG_RW, 1733 &bbr_error_base_paceout, 10000, 1734 "When we hit an error what is the min to pace out in usec's?"); 1735 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1736 SYSCTL_CHILDREN(bbr_sysctl_root), 1737 OID_AUTO, "kill_paceout", CTLFLAG_RW, 1738 &bbr_max_net_error_cnt, 10, 1739 "When we hit this many errors in a row, kill the session?"); 1740 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1741 SYSCTL_CHILDREN(bbr_sysctl_root), 1742 OID_AUTO, "data_after_close", CTLFLAG_RW, 1743 &bbr_ignore_data_after_close, 1, 1744 "Do we hold off sending a RST until all pending data is ack'd"); 1745 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1746 SYSCTL_CHILDREN(bbr_sysctl_root), 1747 OID_AUTO, "resend_use_tso", CTLFLAG_RW, 1748 &bbr_resends_use_tso, 0, 1749 "Can resends use TSO?"); 1750 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1751 SYSCTL_CHILDREN(bbr_sysctl_root), 1752 OID_AUTO, "sblklimit", CTLFLAG_RW, 1753 &bbr_sack_block_limit, 128, 1754 "When do we start ignoring small sack blocks"); 1755 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1756 SYSCTL_CHILDREN(bbr_sysctl_root), 1757 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1758 &bbr_verbose_logging, 0, 1759 "Should BBR black box logging be verbose"); 1760 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1761 SYSCTL_CHILDREN(bbr_sysctl_root), 1762 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1763 &bbr_reorder_thresh, 2, 1764 "What factor for rack will be added when seeing reordering (shift right)"); 1765 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1766 SYSCTL_CHILDREN(bbr_sysctl_root), 1767 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1768 &bbr_reorder_fade, 0, 1769 "Does reorder detection fade, if so how many ms (0 means never)"); 1770 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1771 SYSCTL_CHILDREN(bbr_sysctl_root), 1772 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1773 &bbr_tlp_thresh, 1, 1774 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1775 /* Stats and counters */ 1776 /* The pacing counters for hdwr/software can't be in the array */ 1777 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1778 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1779 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1780 SYSCTL_CHILDREN(bbr_sysctl_root), 1781 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD, 1782 &bbr_hdwr_pacing_enobuf, 1783 "Total number of enobufs for hardware paced flows"); 1784 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1785 SYSCTL_CHILDREN(bbr_sysctl_root), 1786 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD, 1787 &bbr_nohdwr_pacing_enobuf, 1788 "Total number of enobufs for non-hardware paced flows"); 1789 1790 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK); 1791 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1792 SYSCTL_CHILDREN(bbr_sysctl_root), 1793 OID_AUTO, "hdwr_pacing", CTLFLAG_RD, 1794 &bbr_flows_whdwr_pacing, 1795 "Total number of hardware paced flows"); 1796 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK); 1797 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1798 SYSCTL_CHILDREN(bbr_sysctl_root), 1799 OID_AUTO, "software_pacing", CTLFLAG_RD, 1800 &bbr_flows_nohdwr_pacing, 1801 "Total number of software paced flows"); 1802 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK); 1803 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1804 OID_AUTO, "stats", CTLFLAG_RD, 1805 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats"); 1806 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK); 1807 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1808 OID_AUTO, "opts", CTLFLAG_RD, 1809 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats"); 1810 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK); 1811 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1812 OID_AUTO, "lost", CTLFLAG_RD, 1813 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur"); 1814 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK); 1815 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1816 OID_AUTO, "stateresend", CTLFLAG_RD, 1817 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend"); 1818 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK); 1819 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1820 OID_AUTO, "statetime", CTLFLAG_RD, 1821 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states"); 1822 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1823 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1824 OID_AUTO, "outsize", CTLFLAG_RD, 1825 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls"); 1826 SYSCTL_ADD_PROC(&bbr_sysctl_ctx, 1827 SYSCTL_CHILDREN(bbr_sysctl_root), 1828 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1829 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters"); 1830 } 1831 1832 static void 1833 bbr_counter_destroy(void) 1834 { 1835 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE); 1836 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE); 1837 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE); 1838 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT); 1839 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT); 1840 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT); 1841 counter_u64_free(bbr_nohdwr_pacing_enobuf); 1842 counter_u64_free(bbr_hdwr_pacing_enobuf); 1843 counter_u64_free(bbr_flows_whdwr_pacing); 1844 counter_u64_free(bbr_flows_nohdwr_pacing); 1845 1846 } 1847 1848 static __inline void 1849 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts) 1850 { 1851 memset(l, 0, sizeof(union tcp_log_stackspecific)); 1852 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate; 1853 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate); 1854 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 1855 l->bw_inuse = bbr_get_bw(bbr); 1856 l->inflight = ctf_flight_size(bbr->rc_tp, 1857 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 1858 l->applimited = bbr->r_ctl.r_app_limited_until; 1859 l->delivered = bbr->r_ctl.rc_delivered; 1860 l->timeStamp = cts; 1861 l->lost = bbr->r_ctl.rc_lost; 1862 l->bbr_state = bbr->rc_bbr_state; 1863 l->bbr_substate = bbr_state_val(bbr); 1864 l->epoch = bbr->r_ctl.rc_rtt_epoch; 1865 l->lt_epoch = bbr->r_ctl.rc_lt_epoch; 1866 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain; 1867 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain; 1868 l->inhpts = tcp_in_hpts(bbr->rc_tp); 1869 l->use_lt_bw = bbr->rc_lt_use_bw; 1870 l->pkts_out = bbr->r_ctl.rc_flight_at_input; 1871 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch; 1872 } 1873 1874 static void 1875 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason) 1876 { 1877 if (tcp_bblogging_on(bbr->rc_tp)) { 1878 union tcp_log_stackspecific log; 1879 1880 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1881 log.u_bbr.flex1 = 0; 1882 log.u_bbr.flex2 = 0; 1883 log.u_bbr.flex5 = 0; 1884 log.u_bbr.flex3 = 0; 1885 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate; 1886 log.u_bbr.flex7 = reason; 1887 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt; 1888 log.u_bbr.flex8 = 0; 1889 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1890 &bbr->rc_inp->inp_socket->so_rcv, 1891 &bbr->rc_inp->inp_socket->so_snd, 1892 BBR_LOG_BW_RED_EV, 0, 1893 0, &log, false, &bbr->rc_tv); 1894 } 1895 } 1896 1897 static void 1898 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count) 1899 { 1900 if (tcp_bblogging_on(bbr->rc_tp)) { 1901 union tcp_log_stackspecific log; 1902 1903 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1904 log.u_bbr.flex1 = seq; 1905 log.u_bbr.flex2 = count; 1906 log.u_bbr.flex8 = mode; 1907 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1908 &bbr->rc_inp->inp_socket->so_rcv, 1909 &bbr->rc_inp->inp_socket->so_snd, 1910 BBR_LOG_LOWGAIN, 0, 1911 0, &log, false, &bbr->rc_tv); 1912 } 1913 } 1914 1915 static void 1916 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling, 1917 uint8_t reason, uint32_t p_maxseg, int len) 1918 { 1919 if (tcp_bblogging_on(bbr->rc_tp)) { 1920 union tcp_log_stackspecific log; 1921 1922 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1923 log.u_bbr.flex1 = p_maxseg; 1924 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags; 1925 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 1926 log.u_bbr.flex4 = reason; 1927 log.u_bbr.flex5 = bbr->rc_in_persist; 1928 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val; 1929 log.u_bbr.flex7 = p_maxseg; 1930 log.u_bbr.flex8 = bbr->rc_in_persist; 1931 log.u_bbr.pkts_out = 0; 1932 log.u_bbr.applimited = len; 1933 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1934 &bbr->rc_inp->inp_socket->so_rcv, 1935 &bbr->rc_inp->inp_socket->so_snd, 1936 BBR_LOG_JUSTRET, 0, 1937 tlen, &log, false, &bbr->rc_tv); 1938 } 1939 } 1940 1941 static void 1942 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq) 1943 { 1944 if (tcp_bblogging_on(bbr->rc_tp)) { 1945 union tcp_log_stackspecific log; 1946 1947 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1948 log.u_bbr.flex1 = seq; 1949 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 1950 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start; 1951 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1952 &bbr->rc_inp->inp_socket->so_rcv, 1953 &bbr->rc_inp->inp_socket->so_snd, 1954 BBR_LOG_ENTREC, 0, 1955 0, &log, false, &bbr->rc_tv); 1956 } 1957 } 1958 1959 static void 1960 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) 1961 { 1962 if (tcp_bblogging_on(tp)) { 1963 union tcp_log_stackspecific log; 1964 1965 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1966 log.u_bbr.flex1 = tso; 1967 log.u_bbr.flex2 = maxseg; 1968 log.u_bbr.flex3 = mtu; 1969 log.u_bbr.flex4 = csum_flags; 1970 TCP_LOG_EVENTP(tp, NULL, 1971 &bbr->rc_inp->inp_socket->so_rcv, 1972 &bbr->rc_inp->inp_socket->so_snd, 1973 BBR_LOG_MSGSIZE, 0, 1974 0, &log, false, &bbr->rc_tv); 1975 } 1976 } 1977 1978 static void 1979 bbr_log_flowend(struct tcp_bbr *bbr) 1980 { 1981 if (tcp_bblogging_on(bbr->rc_tp)) { 1982 union tcp_log_stackspecific log; 1983 struct sockbuf *r, *s; 1984 struct timeval tv; 1985 1986 if (bbr->rc_inp->inp_socket) { 1987 r = &bbr->rc_inp->inp_socket->so_rcv; 1988 s = &bbr->rc_inp->inp_socket->so_snd; 1989 } else { 1990 r = s = NULL; 1991 } 1992 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv)); 1993 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1994 r, s, 1995 TCP_LOG_FLOWEND, 0, 1996 0, &log, false, &tv); 1997 } 1998 } 1999 2000 static void 2001 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, 2002 uint32_t lost, uint32_t del) 2003 { 2004 if (tcp_bblogging_on(bbr->rc_tp)) { 2005 union tcp_log_stackspecific log; 2006 2007 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2008 log.u_bbr.flex1 = lost; 2009 log.u_bbr.flex2 = del; 2010 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw; 2011 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt; 2012 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2013 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2014 log.u_bbr.flex7 = line; 2015 log.u_bbr.flex8 = 0; 2016 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count; 2017 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2018 &bbr->rc_inp->inp_socket->so_rcv, 2019 &bbr->rc_inp->inp_socket->so_snd, 2020 BBR_LOG_PKT_EPOCH, 0, 2021 0, &log, false, &bbr->rc_tv); 2022 } 2023 } 2024 2025 static void 2026 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time) 2027 { 2028 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2029 union tcp_log_stackspecific log; 2030 2031 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2032 log.u_bbr.flex1 = bbr->r_ctl.rc_lost; 2033 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat; 2034 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat; 2035 log.u_bbr.flex7 = line; 2036 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2037 &bbr->rc_inp->inp_socket->so_rcv, 2038 &bbr->rc_inp->inp_socket->so_snd, 2039 BBR_LOG_TIME_EPOCH, 0, 2040 0, &log, false, &bbr->rc_tv); 2041 } 2042 } 2043 2044 static void 2045 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth) 2046 { 2047 if (tcp_bblogging_on(bbr->rc_tp)) { 2048 union tcp_log_stackspecific log; 2049 2050 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2051 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2052 log.u_bbr.flex2 = new_tar; 2053 log.u_bbr.flex3 = line; 2054 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2055 log.u_bbr.flex5 = bbr_quanta; 2056 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs; 2057 log.u_bbr.flex7 = bbr->rc_last_options; 2058 log.u_bbr.flex8 = meth; 2059 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2060 &bbr->rc_inp->inp_socket->so_rcv, 2061 &bbr->rc_inp->inp_socket->so_snd, 2062 BBR_LOG_STATE_TARGET, 0, 2063 0, &log, false, &bbr->rc_tv); 2064 } 2065 2066 } 2067 2068 static void 2069 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2070 { 2071 if (tcp_bblogging_on(bbr->rc_tp)) { 2072 union tcp_log_stackspecific log; 2073 2074 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2075 log.u_bbr.flex1 = line; 2076 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2077 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2078 if (bbr_state_is_pkt_epoch) 2079 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 2080 else 2081 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP); 2082 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2083 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2084 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000); 2085 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra; 2086 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state; 2087 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2088 &bbr->rc_inp->inp_socket->so_rcv, 2089 &bbr->rc_inp->inp_socket->so_snd, 2090 BBR_LOG_STATE, 0, 2091 0, &log, false, &bbr->rc_tv); 2092 } 2093 } 2094 2095 static void 2096 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 2097 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond) 2098 { 2099 if (tcp_bblogging_on(bbr->rc_tp)) { 2100 union tcp_log_stackspecific log; 2101 2102 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2103 log.u_bbr.flex1 = line; 2104 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2105 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt; 2106 log.u_bbr.flex4 = applied; 2107 log.u_bbr.flex5 = rtt; 2108 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2109 log.u_bbr.flex7 = cond; 2110 log.u_bbr.flex8 = reas; 2111 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2112 &bbr->rc_inp->inp_socket->so_rcv, 2113 &bbr->rc_inp->inp_socket->so_snd, 2114 BBR_LOG_RTT_SHRINKS, 0, 2115 0, &log, false, &bbr->rc_tv); 2116 } 2117 } 2118 2119 static void 2120 bbr_log_type_exit_rec(struct tcp_bbr *bbr) 2121 { 2122 if (tcp_bblogging_on(bbr->rc_tp)) { 2123 union tcp_log_stackspecific log; 2124 2125 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2126 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start; 2127 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 2128 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2129 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2130 &bbr->rc_inp->inp_socket->so_rcv, 2131 &bbr->rc_inp->inp_socket->so_snd, 2132 BBR_LOG_EXITREC, 0, 2133 0, &log, false, &bbr->rc_tv); 2134 } 2135 } 2136 2137 static void 2138 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg, 2139 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line) 2140 { 2141 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2142 union tcp_log_stackspecific log; 2143 2144 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2145 log.u_bbr.flex1 = line; 2146 log.u_bbr.flex2 = prev_acked; 2147 log.u_bbr.flex3 = bytes_this_ack; 2148 log.u_bbr.flex4 = chg; 2149 log.u_bbr.flex5 = th_ack; 2150 log.u_bbr.flex6 = target; 2151 log.u_bbr.flex8 = meth; 2152 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2153 &bbr->rc_inp->inp_socket->so_rcv, 2154 &bbr->rc_inp->inp_socket->so_snd, 2155 BBR_LOG_CWND, 0, 2156 0, &log, false, &bbr->rc_tv); 2157 } 2158 } 2159 2160 static void 2161 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin) 2162 { 2163 /* 2164 * Log the rtt sample we are applying to the srtt algorithm in 2165 * useconds. 2166 */ 2167 if (tcp_bblogging_on(bbr->rc_tp)) { 2168 union tcp_log_stackspecific log; 2169 2170 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2171 log.u_bbr.flex1 = rtt; 2172 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time; 2173 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay; 2174 log.u_bbr.flex4 = bbr->rc_tp->ts_offset; 2175 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2176 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv); 2177 log.u_bbr.flex6 = tsin; 2178 log.u_bbr.flex7 = 0; 2179 log.u_bbr.flex8 = bbr->rc_ack_was_delayed; 2180 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2181 &bbr->rc_inp->inp_socket->so_rcv, 2182 &bbr->rc_inp->inp_socket->so_snd, 2183 TCP_LOG_RTT, 0, 2184 0, &log, false, &bbr->rc_tv); 2185 } 2186 } 2187 2188 static void 2189 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit) 2190 { 2191 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2192 union tcp_log_stackspecific log; 2193 2194 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2195 log.u_bbr.flex1 = time_in; 2196 log.u_bbr.flex2 = line; 2197 log.u_bbr.flex8 = enter_exit; 2198 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2199 &bbr->rc_inp->inp_socket->so_rcv, 2200 &bbr->rc_inp->inp_socket->so_snd, 2201 BBR_LOG_PERSIST, 0, 2202 0, &log, false, &bbr->rc_tv); 2203 } 2204 } 2205 static void 2206 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts) 2207 { 2208 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2209 union tcp_log_stackspecific log; 2210 2211 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2212 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age; 2213 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2214 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2215 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time; 2216 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2217 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2218 &bbr->rc_inp->inp_socket->so_rcv, 2219 &bbr->rc_inp->inp_socket->so_snd, 2220 BBR_LOG_ACKCLEAR, 0, 2221 0, &log, false, &bbr->rc_tv); 2222 } 2223 } 2224 2225 static void 2226 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen, 2227 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m) 2228 { 2229 if (tcp_bblogging_on(bbr->rc_tp)) { 2230 union tcp_log_stackspecific log; 2231 struct timeval tv; 2232 2233 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2234 log.u_bbr.flex1 = nsegs; 2235 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes; 2236 if (m) { 2237 struct timespec ts; 2238 2239 log.u_bbr.flex3 = m->m_flags; 2240 if (m->m_flags & M_TSTMP) { 2241 mbuf_tstmp2timespec(m, &ts); 2242 tv.tv_sec = ts.tv_sec; 2243 tv.tv_usec = ts.tv_nsec / 1000; 2244 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv); 2245 } else { 2246 log.u_bbr.lt_epoch = 0; 2247 } 2248 if (m->m_flags & M_TSTMP_LRO) { 2249 mbuf_tstmp2timeval(m, &tv); 2250 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv); 2251 } else { 2252 /* No arrival timestamp */ 2253 log.u_bbr.flex5 = 0; 2254 } 2255 2256 log.u_bbr.pkts_out = tcp_get_usecs(&tv); 2257 } else { 2258 log.u_bbr.flex3 = 0; 2259 log.u_bbr.flex5 = 0; 2260 log.u_bbr.flex6 = 0; 2261 log.u_bbr.pkts_out = 0; 2262 } 2263 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state; 2264 log.u_bbr.flex7 = bbr->r_wanted_output; 2265 log.u_bbr.flex8 = bbr->rc_in_persist; 2266 TCP_LOG_EVENTP(bbr->rc_tp, th, 2267 &bbr->rc_inp->inp_socket->so_rcv, 2268 &bbr->rc_inp->inp_socket->so_snd, 2269 TCP_LOG_IN, 0, 2270 tlen, &log, true, &bbr->rc_tv); 2271 } 2272 } 2273 2274 static void 2275 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out) 2276 { 2277 if (tcp_bblogging_on(bbr->rc_tp)) { 2278 union tcp_log_stackspecific log; 2279 2280 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2281 log.u_bbr.flex1 = did_out; 2282 log.u_bbr.flex2 = nxt_pkt; 2283 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val; 2284 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2285 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp; 2286 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes; 2287 log.u_bbr.flex7 = bbr->r_wanted_output; 2288 log.u_bbr.flex8 = bbr->rc_in_persist; 2289 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay; 2290 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2291 &bbr->rc_inp->inp_socket->so_rcv, 2292 &bbr->rc_inp->inp_socket->so_snd, 2293 BBR_LOG_DOSEG_DONE, 0, 2294 0, &log, true, &bbr->rc_tv); 2295 } 2296 } 2297 2298 static void 2299 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts, 2300 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz) 2301 { 2302 if (tcp_bblogging_on(bbr->rc_tp)) { 2303 union tcp_log_stackspecific log; 2304 2305 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2306 log.u_bbr.flex1 = line; 2307 log.u_bbr.flex2 = o_len; 2308 log.u_bbr.flex3 = segcnt; 2309 log.u_bbr.flex4 = segsiz; 2310 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2311 &bbr->rc_inp->inp_socket->so_rcv, 2312 &bbr->rc_inp->inp_socket->so_snd, 2313 BBR_LOG_ENOBUF_JMP, ENOBUFS, 2314 len, &log, true, &bbr->rc_tv); 2315 } 2316 } 2317 2318 static void 2319 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling) 2320 { 2321 if (tcp_bblogging_on(bbr->rc_tp)) { 2322 union tcp_log_stackspecific log; 2323 2324 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2325 log.u_bbr.flex1 = timers; 2326 log.u_bbr.flex2 = ret; 2327 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 2328 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2329 log.u_bbr.flex5 = cts; 2330 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2331 log.u_bbr.flex8 = hpts_calling; 2332 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2333 &bbr->rc_inp->inp_socket->so_rcv, 2334 &bbr->rc_inp->inp_socket->so_snd, 2335 BBR_LOG_TO_PROCESS, 0, 2336 0, &log, false, &bbr->rc_tv); 2337 } 2338 } 2339 2340 static void 2341 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num) 2342 { 2343 if (tcp_bblogging_on(bbr->rc_tp)) { 2344 union tcp_log_stackspecific log; 2345 uint64_t ar; 2346 2347 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2348 log.u_bbr.flex1 = bbr->bbr_timer_src; 2349 log.u_bbr.flex2 = 0; 2350 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2351 ar = (uintptr_t)(bbr->r_ctl.rc_resend); 2352 ar >>= 32; 2353 ar &= 0x00000000ffffffff; 2354 log.u_bbr.flex4 = (uint32_t)ar; 2355 ar = (uintptr_t)bbr->r_ctl.rc_resend; 2356 ar &= 0x00000000ffffffff; 2357 log.u_bbr.flex5 = (uint32_t)ar; 2358 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2359 log.u_bbr.flex8 = to_num; 2360 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2361 &bbr->rc_inp->inp_socket->so_rcv, 2362 &bbr->rc_inp->inp_socket->so_snd, 2363 BBR_LOG_RTO, 0, 2364 0, &log, false, &bbr->rc_tv); 2365 } 2366 } 2367 2368 static void 2369 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason) 2370 { 2371 if (tcp_bblogging_on(bbr->rc_tp)) { 2372 union tcp_log_stackspecific log; 2373 2374 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2375 log.u_bbr.flex1 = flex1; 2376 log.u_bbr.flex2 = flex2; 2377 log.u_bbr.flex3 = flex3; 2378 log.u_bbr.flex4 = 0; 2379 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2380 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2381 log.u_bbr.flex8 = reason; 2382 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw; 2383 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2384 &bbr->rc_inp->inp_socket->so_rcv, 2385 &bbr->rc_inp->inp_socket->so_snd, 2386 BBR_LOG_REDUCE, 0, 2387 0, &log, false, &bbr->rc_tv); 2388 } 2389 } 2390 2391 static void 2392 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag) 2393 { 2394 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2395 union tcp_log_stackspecific log; 2396 2397 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2398 log.u_bbr.flex1 = diag->p_nxt_slot; 2399 log.u_bbr.flex2 = diag->p_cur_slot; 2400 log.u_bbr.flex3 = diag->slot_req; 2401 log.u_bbr.flex4 = diag->inp_hptsslot; 2402 log.u_bbr.flex5 = diag->slot_remaining; 2403 log.u_bbr.flex6 = diag->need_new_to; 2404 log.u_bbr.flex7 = diag->p_hpts_active; 2405 log.u_bbr.flex8 = diag->p_on_min_sleep; 2406 /* Hijack other fields as needed */ 2407 log.u_bbr.epoch = diag->have_slept; 2408 log.u_bbr.lt_epoch = diag->yet_to_sleep; 2409 log.u_bbr.pkts_out = diag->co_ret; 2410 log.u_bbr.applimited = diag->hpts_sleep_time; 2411 log.u_bbr.delivered = diag->p_prev_slot; 2412 log.u_bbr.inflight = diag->p_runningslot; 2413 log.u_bbr.bw_inuse = diag->wheel_slot; 2414 log.u_bbr.rttProp = diag->wheel_cts; 2415 log.u_bbr.delRate = diag->maxslots; 2416 log.u_bbr.cur_del_rate = diag->p_curtick; 2417 log.u_bbr.cur_del_rate <<= 32; 2418 log.u_bbr.cur_del_rate |= diag->p_lasttick; 2419 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2420 &bbr->rc_inp->inp_socket->so_rcv, 2421 &bbr->rc_inp->inp_socket->so_snd, 2422 BBR_LOG_HPTSDIAG, 0, 2423 0, &log, false, &bbr->rc_tv); 2424 } 2425 } 2426 2427 static void 2428 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt, 2429 uint32_t thresh, uint32_t to) 2430 { 2431 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2432 union tcp_log_stackspecific log; 2433 2434 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2435 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar; 2436 log.u_bbr.flex2 = time_since_sent; 2437 log.u_bbr.flex3 = srtt; 2438 log.u_bbr.flex4 = thresh; 2439 log.u_bbr.flex5 = to; 2440 log.u_bbr.flex6 = bbr->rc_tp->t_srtt; 2441 log.u_bbr.flex8 = mode; 2442 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2443 &bbr->rc_inp->inp_socket->so_rcv, 2444 &bbr->rc_inp->inp_socket->so_snd, 2445 BBR_LOG_TIMERPREP, 0, 2446 0, &log, false, &bbr->rc_tv); 2447 } 2448 } 2449 2450 static void 2451 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 2452 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod) 2453 { 2454 if (tcp_bblogging_on(bbr->rc_tp)) { 2455 union tcp_log_stackspecific log; 2456 2457 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2458 log.u_bbr.flex1 = usecs; 2459 log.u_bbr.flex2 = len; 2460 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff); 2461 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff); 2462 if (override) 2463 log.u_bbr.flex5 = (1 << 2); 2464 else 2465 log.u_bbr.flex5 = 0; 2466 log.u_bbr.flex6 = override; 2467 log.u_bbr.flex7 = gain; 2468 log.u_bbr.flex8 = mod; 2469 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2470 &bbr->rc_inp->inp_socket->so_rcv, 2471 &bbr->rc_inp->inp_socket->so_snd, 2472 BBR_LOG_HPTSI_CALC, 0, 2473 len, &log, false, &bbr->rc_tv); 2474 } 2475 } 2476 2477 static void 2478 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2479 { 2480 if (tcp_bblogging_on(bbr->rc_tp)) { 2481 union tcp_log_stackspecific log; 2482 2483 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2484 2485 log.u_bbr.flex1 = bbr->bbr_timer_src; 2486 log.u_bbr.flex2 = to; 2487 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2488 log.u_bbr.flex4 = slot; 2489 log.u_bbr.flex5 = bbr->rc_tp->t_hpts_slot; 2490 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2491 log.u_bbr.pkts_out = bbr->rc_tp->t_flags2; 2492 log.u_bbr.flex8 = which; 2493 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2494 &bbr->rc_inp->inp_socket->so_rcv, 2495 &bbr->rc_inp->inp_socket->so_snd, 2496 BBR_LOG_TIMERSTAR, 0, 2497 0, &log, false, &bbr->rc_tv); 2498 } 2499 } 2500 2501 static void 2502 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) 2503 { 2504 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2505 union tcp_log_stackspecific log; 2506 2507 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2508 log.u_bbr.flex1 = thresh; 2509 log.u_bbr.flex2 = lro; 2510 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts; 2511 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 2512 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2513 log.u_bbr.flex6 = srtt; 2514 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift; 2515 log.u_bbr.flex8 = frm; 2516 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2517 &bbr->rc_inp->inp_socket->so_rcv, 2518 &bbr->rc_inp->inp_socket->so_snd, 2519 BBR_LOG_THRESH_CALC, 0, 2520 0, &log, false, &bbr->rc_tv); 2521 } 2522 } 2523 2524 static void 2525 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed) 2526 { 2527 if (tcp_bblogging_on(bbr->rc_tp)) { 2528 union tcp_log_stackspecific log; 2529 2530 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2531 log.u_bbr.flex1 = line; 2532 log.u_bbr.flex2 = bbr->bbr_timer_src; 2533 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2534 log.u_bbr.flex4 = bbr->rc_in_persist; 2535 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2536 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2537 log.u_bbr.flex8 = hpts_removed; 2538 log.u_bbr.pkts_out = bbr->rc_pacer_started; 2539 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2540 &bbr->rc_inp->inp_socket->so_rcv, 2541 &bbr->rc_inp->inp_socket->so_snd, 2542 BBR_LOG_TIMERCANC, 0, 2543 0, &log, false, &bbr->rc_tv); 2544 } 2545 } 2546 2547 static void 2548 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta) 2549 { 2550 if (tcp_bblogging_on(bbr->rc_tp)) { 2551 union tcp_log_stackspecific log; 2552 2553 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2554 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio; 2555 log.u_bbr.flex2 = (peer_delta >> 32); 2556 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff); 2557 log.u_bbr.flex4 = (delta >> 32); 2558 log.u_bbr.flex5 = (delta & 0x00000000ffffffff); 2559 log.u_bbr.flex7 = bbr->rc_ts_clock_set; 2560 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used; 2561 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2562 &bbr->rc_inp->inp_socket->so_rcv, 2563 &bbr->rc_inp->inp_socket->so_snd, 2564 BBR_LOG_TSTMP_VAL, 0, 2565 0, &log, false, &bbr->rc_tv); 2566 } 2567 } 2568 2569 static void 2570 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) 2571 { 2572 if (tcp_bblogging_on(bbr->rc_tp)) { 2573 union tcp_log_stackspecific log; 2574 2575 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2576 log.u_bbr.flex1 = tsosz; 2577 log.u_bbr.flex2 = tls; 2578 log.u_bbr.flex3 = tcp_min_hptsi_time; 2579 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min; 2580 log.u_bbr.flex5 = old_val; 2581 log.u_bbr.flex6 = maxseg; 2582 log.u_bbr.flex7 = bbr->rc_no_pacing; 2583 log.u_bbr.flex7 <<= 1; 2584 log.u_bbr.flex7 |= bbr->rc_past_init_win; 2585 if (hdwr) 2586 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google; 2587 else 2588 log.u_bbr.flex8 = bbr->rc_use_google; 2589 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2590 &bbr->rc_inp->inp_socket->so_rcv, 2591 &bbr->rc_inp->inp_socket->so_snd, 2592 BBR_LOG_BBRTSO, 0, 2593 0, &log, false, &bbr->rc_tv); 2594 } 2595 } 2596 2597 static void 2598 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, 2599 uint32_t flags, uint32_t line) 2600 { 2601 if (tcp_bblogging_on(bbr->rc_tp)) { 2602 union tcp_log_stackspecific log; 2603 2604 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2605 log.u_bbr.flex1 = line; 2606 log.u_bbr.flex2 = rsm->r_start; 2607 log.u_bbr.flex3 = rsm->r_end; 2608 log.u_bbr.flex4 = rsm->r_delivered; 2609 log.u_bbr.flex5 = rsm->r_rtr_cnt; 2610 log.u_bbr.flex6 = rsm->r_dupack; 2611 log.u_bbr.flex7 = rsm->r_tim_lastsent[0]; 2612 log.u_bbr.flex8 = rsm->r_flags; 2613 /* Hijack the pkts_out fids */ 2614 log.u_bbr.applimited = flags; 2615 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2616 &bbr->rc_inp->inp_socket->so_rcv, 2617 &bbr->rc_inp->inp_socket->so_snd, 2618 BBR_RSM_CLEARED, 0, 2619 0, &log, false, &bbr->rc_tv); 2620 } 2621 } 2622 2623 static void 2624 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts, 2625 uint32_t flex3, uint32_t flex2, uint32_t flex5, 2626 uint32_t flex6, uint32_t pkts_out, int flex7, 2627 uint32_t flex4, uint32_t flex1) 2628 { 2629 2630 if (tcp_bblogging_on(bbr->rc_tp)) { 2631 union tcp_log_stackspecific log; 2632 2633 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2634 log.u_bbr.flex1 = flex1; 2635 log.u_bbr.flex2 = flex2; 2636 log.u_bbr.flex3 = flex3; 2637 log.u_bbr.flex4 = flex4; 2638 log.u_bbr.flex5 = flex5; 2639 log.u_bbr.flex6 = flex6; 2640 log.u_bbr.flex7 = flex7; 2641 /* Hijack the pkts_out fids */ 2642 log.u_bbr.pkts_out = pkts_out; 2643 log.u_bbr.flex8 = flex8; 2644 if (bbr->rc_ack_was_delayed) 2645 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay; 2646 else 2647 log.u_bbr.epoch = 0; 2648 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2649 &bbr->rc_inp->inp_socket->so_rcv, 2650 &bbr->rc_inp->inp_socket->so_snd, 2651 BBR_LOG_BBRUPD, 0, 2652 flex2, &log, false, &bbr->rc_tv); 2653 } 2654 } 2655 2656 static void 2657 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason, 2658 uint32_t newbw, uint32_t obw, uint32_t diff, 2659 uint32_t tim) 2660 { 2661 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) { 2662 union tcp_log_stackspecific log; 2663 2664 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2665 log.u_bbr.flex1 = reason; 2666 log.u_bbr.flex2 = newbw; 2667 log.u_bbr.flex3 = obw; 2668 log.u_bbr.flex4 = diff; 2669 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost; 2670 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del; 2671 log.u_bbr.flex7 = bbr->rc_lt_is_sampling; 2672 log.u_bbr.pkts_out = tim; 2673 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw; 2674 if (bbr->rc_lt_use_bw == 0) 2675 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 2676 else 2677 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 2678 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2679 &bbr->rc_inp->inp_socket->so_rcv, 2680 &bbr->rc_inp->inp_socket->so_snd, 2681 BBR_LOG_BWSAMP, 0, 2682 0, &log, false, &bbr->rc_tv); 2683 } 2684 } 2685 2686 static inline void 2687 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line) 2688 { 2689 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2690 union tcp_log_stackspecific log; 2691 2692 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2693 log.u_bbr.flex1 = line; 2694 log.u_bbr.flex2 = tick; 2695 log.u_bbr.flex3 = tp->t_maxunacktime; 2696 log.u_bbr.flex4 = tp->t_acktime; 2697 log.u_bbr.flex8 = event; 2698 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2699 &bbr->rc_inp->inp_socket->so_rcv, 2700 &bbr->rc_inp->inp_socket->so_snd, 2701 BBR_LOG_PROGRESS, 0, 2702 0, &log, false, &bbr->rc_tv); 2703 } 2704 } 2705 2706 static void 2707 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp, 2708 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts, 2709 int error) 2710 { 2711 if (tcp_bblogging_on(bbr->rc_tp)) { 2712 union tcp_log_stackspecific log; 2713 uint64_t ifp64 = (uintptr_t)ifp; 2714 2715 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2716 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2717 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2718 log.u_bbr.flex3 = ((ifp64 >> 32) & 0x00000000ffffffff); 2719 log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff); 2720 log.u_bbr.bw_inuse = rate; 2721 log.u_bbr.flex5 = line; 2722 log.u_bbr.flex6 = error; 2723 log.u_bbr.flex8 = bbr->skip_gain; 2724 log.u_bbr.flex8 <<= 1; 2725 log.u_bbr.flex8 |= bbr->gain_is_limited; 2726 log.u_bbr.flex8 <<= 1; 2727 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing; 2728 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 2729 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2730 &bbr->rc_inp->inp_socket->so_rcv, 2731 &bbr->rc_inp->inp_socket->so_snd, 2732 BBR_LOG_HDWR_PACE, 0, 2733 0, &log, false, &bbr->rc_tv); 2734 } 2735 } 2736 2737 static void 2738 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) 2739 { 2740 if (tcp_bblogging_on(bbr->rc_tp)) { 2741 union tcp_log_stackspecific log; 2742 2743 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2744 log.u_bbr.flex1 = slot; 2745 log.u_bbr.flex2 = del_by; 2746 log.u_bbr.flex3 = prev_delay; 2747 log.u_bbr.flex4 = line; 2748 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val; 2749 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay; 2750 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags); 2751 log.u_bbr.flex8 = bbr->rc_in_persist; 2752 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2753 &bbr->rc_inp->inp_socket->so_rcv, 2754 &bbr->rc_inp->inp_socket->so_snd, 2755 BBR_LOG_BBRSND, 0, 2756 len, &log, false, &bbr->rc_tv); 2757 } 2758 } 2759 2760 static void 2761 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) 2762 { 2763 if (tcp_bblogging_on(bbr->rc_tp)) { 2764 union tcp_log_stackspecific log; 2765 2766 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2767 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered; 2768 log.u_bbr.flex2 = 0; 2769 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt; 2770 log.u_bbr.flex4 = end; 2771 log.u_bbr.flex5 = seq; 2772 log.u_bbr.flex6 = t; 2773 log.u_bbr.flex7 = match; 2774 log.u_bbr.flex8 = flags; 2775 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2776 &bbr->rc_inp->inp_socket->so_rcv, 2777 &bbr->rc_inp->inp_socket->so_snd, 2778 BBR_LOG_BBRRTT, 0, 2779 0, &log, false, &bbr->rc_tv); 2780 } 2781 } 2782 2783 static void 2784 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method) 2785 { 2786 if (tcp_bblogging_on(bbr->rc_tp)) { 2787 union tcp_log_stackspecific log; 2788 2789 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2790 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2791 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 2792 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch; 2793 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2794 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs; 2795 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight; 2796 log.u_bbr.flex7 = 0; 2797 log.u_bbr.flex8 = entry_method; 2798 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2799 &bbr->rc_inp->inp_socket->so_rcv, 2800 &bbr->rc_inp->inp_socket->so_snd, 2801 BBR_LOG_EXIT_GAIN, 0, 2802 0, &log, false, &bbr->rc_tv); 2803 } 2804 } 2805 2806 static void 2807 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired) 2808 { 2809 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2810 union tcp_log_stackspecific log; 2811 2812 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2813 /* R-HU */ 2814 log.u_bbr.flex1 = 0; 2815 log.u_bbr.flex2 = 0; 2816 log.u_bbr.flex3 = 0; 2817 log.u_bbr.flex4 = 0; 2818 log.u_bbr.flex7 = 0; 2819 log.u_bbr.flex8 = settings_desired; 2820 2821 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2822 &bbr->rc_inp->inp_socket->so_rcv, 2823 &bbr->rc_inp->inp_socket->so_snd, 2824 BBR_LOG_SETTINGS_CHG, 0, 2825 0, &log, false, &bbr->rc_tv); 2826 } 2827 } 2828 2829 /* 2830 * Returns the bw from the our filter. 2831 */ 2832 static inline uint64_t 2833 bbr_get_full_bw(struct tcp_bbr *bbr) 2834 { 2835 uint64_t bw; 2836 2837 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2838 2839 return (bw); 2840 } 2841 2842 static inline void 2843 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2844 { 2845 uint64_t calclr; 2846 uint32_t lost, del; 2847 2848 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch) 2849 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch; 2850 else 2851 lost = 0; 2852 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del; 2853 if (lost == 0) { 2854 calclr = 0; 2855 } else if (del) { 2856 calclr = lost; 2857 calclr *= (uint64_t)1000; 2858 calclr /= (uint64_t)del; 2859 } else { 2860 /* Nothing delivered? 100.0% loss */ 2861 calclr = 1000; 2862 } 2863 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr; 2864 if (IN_RECOVERY(bbr->rc_tp->t_flags)) 2865 bbr->r_ctl.recovery_lr += (uint32_t)calclr; 2866 bbr->r_ctl.rc_pkt_epoch++; 2867 if (bbr->rc_no_pacing && 2868 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) { 2869 bbr->rc_no_pacing = 0; 2870 tcp_bbr_tso_size_check(bbr, cts); 2871 } 2872 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time); 2873 bbr->r_ctl.rc_pkt_epoch_time = cts; 2874 /* What was our loss rate */ 2875 bbr_log_pkt_epoch(bbr, cts, line, lost, del); 2876 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered; 2877 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost; 2878 } 2879 2880 static inline void 2881 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2882 { 2883 uint32_t epoch_time; 2884 2885 /* Tick the RTT clock */ 2886 bbr->r_ctl.rc_rtt_epoch++; 2887 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start; 2888 bbr_log_time_epoch(bbr, cts, line, epoch_time); 2889 bbr->r_ctl.rc_rcv_epoch_start = cts; 2890 } 2891 2892 static inline void 2893 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked) 2894 { 2895 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) { 2896 bbr->rc_is_pkt_epoch_now = 1; 2897 } 2898 } 2899 2900 /* 2901 * Returns the bw from either the b/w filter 2902 * or from the lt_bw (if the connection is being 2903 * policed). 2904 */ 2905 static inline uint64_t 2906 __bbr_get_bw(struct tcp_bbr *bbr) 2907 { 2908 uint64_t bw, min_bw; 2909 uint64_t rtt; 2910 int gm_measure_cnt = 1; 2911 2912 /* 2913 * For startup we make, like google, a 2914 * minimum b/w. This is generated from the 2915 * IW and the rttProp. We do fall back to srtt 2916 * if for some reason (initial handshake) we don't 2917 * have a rttProp. We, in the worst case, fall back 2918 * to the configured min_bw (rc_initial_hptsi_bw). 2919 */ 2920 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 2921 /* Attempt first to use rttProp */ 2922 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2923 if (rtt && (rtt < 0xffffffff)) { 2924 measure: 2925 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2926 ((uint64_t)1000000); 2927 min_bw /= rtt; 2928 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2929 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2930 } 2931 2932 } else if (bbr->rc_tp->t_srtt != 0) { 2933 /* No rttProp, use srtt? */ 2934 rtt = bbr_get_rtt(bbr, BBR_SRTT); 2935 goto measure; 2936 } else { 2937 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2938 } 2939 } else 2940 min_bw = 0; 2941 2942 if ((bbr->rc_past_init_win == 0) && 2943 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp))) 2944 bbr->rc_past_init_win = 1; 2945 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1)) 2946 gm_measure_cnt = 0; 2947 if (gm_measure_cnt && 2948 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) || 2949 (bbr->rc_past_init_win == 0))) { 2950 /* For google we use our guess rate until we get 1 measurement */ 2951 2952 use_initial_window: 2953 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2954 if (rtt && (rtt < 0xffffffff)) { 2955 /* 2956 * We have an RTT measurement. Use that in 2957 * combination with our initial window to calculate 2958 * a b/w. 2959 */ 2960 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2961 ((uint64_t)1000000); 2962 bw /= rtt; 2963 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2964 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2965 } 2966 } else { 2967 /* Drop back to the 40 and punt to a default */ 2968 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2969 } 2970 if (bw < 1) 2971 /* Probably should panic */ 2972 bw = 1; 2973 if (bw > min_bw) 2974 return (bw); 2975 else 2976 return (min_bw); 2977 } 2978 if (bbr->rc_lt_use_bw) 2979 bw = bbr->r_ctl.rc_lt_bw; 2980 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0)) 2981 bw = bbr->r_ctl.red_bw; 2982 else 2983 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2984 if (bw == 0) { 2985 /* We should not be at 0, go to the initial window then */ 2986 goto use_initial_window; 2987 } 2988 if (bw < min_bw) 2989 bw = min_bw; 2990 return (bw); 2991 } 2992 2993 static inline uint64_t 2994 bbr_get_bw(struct tcp_bbr *bbr) 2995 { 2996 uint64_t bw; 2997 2998 bw = __bbr_get_bw(bbr); 2999 return (bw); 3000 } 3001 3002 static inline void 3003 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts) 3004 { 3005 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch; 3006 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time; 3007 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3008 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3009 } 3010 3011 static inline void 3012 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts) 3013 { 3014 bbr->rc_lt_is_sampling = 0; 3015 bbr->rc_lt_use_bw = 0; 3016 bbr->r_ctl.rc_lt_bw = 0; 3017 bbr_reset_lt_bw_interval(bbr, cts); 3018 } 3019 3020 static inline void 3021 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin) 3022 { 3023 uint64_t diff; 3024 3025 /* Do we have a previous sample? */ 3026 if (bbr->r_ctl.rc_lt_bw) { 3027 /* Get the diff in bytes per second */ 3028 if (bbr->r_ctl.rc_lt_bw > bw) 3029 diff = bbr->r_ctl.rc_lt_bw - bw; 3030 else 3031 diff = bw - bbr->r_ctl.rc_lt_bw; 3032 if ((diff <= bbr_lt_bw_diff) || 3033 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) { 3034 /* Consider us policed */ 3035 uint32_t saved_bw; 3036 3037 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw; 3038 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */ 3039 bbr->rc_lt_use_bw = 1; 3040 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 3041 /* 3042 * Use pkt based epoch for measuring length of 3043 * policer up 3044 */ 3045 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch; 3046 /* 3047 * reason 4 is we need to start consider being 3048 * policed 3049 */ 3050 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin); 3051 return; 3052 } 3053 } 3054 bbr->r_ctl.rc_lt_bw = bw; 3055 bbr_reset_lt_bw_interval(bbr, cts); 3056 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin); 3057 } 3058 3059 static void 3060 bbr_randomize_extra_state_time(struct tcp_bbr *bbr) 3061 { 3062 uint32_t ran, deduct; 3063 3064 ran = arc4random_uniform(bbr_rand_ot); 3065 if (ran) { 3066 deduct = bbr->r_ctl.rc_level_state_extra / ran; 3067 bbr->r_ctl.rc_level_state_extra -= deduct; 3068 } 3069 } 3070 /* 3071 * Return randomly the starting state 3072 * to use in probebw. 3073 */ 3074 static uint8_t 3075 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts) 3076 { 3077 uint32_t ran; 3078 uint8_t ret_val; 3079 3080 /* Initialize the offset to 0 */ 3081 bbr->r_ctl.rc_exta_time_gd = 0; 3082 bbr->rc_hit_state_1 = 0; 3083 bbr->r_ctl.rc_level_state_extra = 0; 3084 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1)); 3085 /* 3086 * The math works funny here :) the return value is used to set the 3087 * substate and then the state change is called which increments by 3088 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when 3089 * we fully enter the state. Note that the (8 - 1 - ran) assures that 3090 * we return 1 - 7, so we dont return 0 and end up starting in 3091 * state 1 (DRAIN). 3092 */ 3093 ret_val = BBR_SUBSTATE_COUNT - 1 - ran; 3094 /* Set an epoch */ 3095 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) 3096 bbr_set_epoch(bbr, cts, __LINE__); 3097 3098 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 3099 return (ret_val); 3100 } 3101 3102 static void 3103 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected) 3104 { 3105 uint32_t diff, d_time; 3106 uint64_t del_time, bw, lost, delivered; 3107 3108 if (bbr->r_use_policer == 0) 3109 return; 3110 if (bbr->rc_lt_use_bw) { 3111 /* We are using lt bw do we stop yet? */ 3112 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 3113 if (diff > bbr_lt_bw_max_rtts) { 3114 /* Reset it all */ 3115 reset_all: 3116 bbr_reset_lt_bw_sampling(bbr, cts); 3117 if (bbr->rc_filled_pipe) { 3118 bbr_set_epoch(bbr, cts, __LINE__); 3119 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 3120 bbr_substate_change(bbr, cts, __LINE__, 0); 3121 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 3122 bbr_log_type_statechange(bbr, cts, __LINE__); 3123 } else { 3124 /* 3125 * This should not happen really 3126 * unless we remove the startup/drain 3127 * restrictions above. 3128 */ 3129 bbr->rc_bbr_state = BBR_STATE_STARTUP; 3130 bbr_set_epoch(bbr, cts, __LINE__); 3131 bbr->r_ctl.rc_bbr_state_time = cts; 3132 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 3133 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 3134 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 3135 bbr_set_state_target(bbr, __LINE__); 3136 bbr_log_type_statechange(bbr, cts, __LINE__); 3137 } 3138 /* reason 0 is to stop using lt-bw */ 3139 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0); 3140 return; 3141 } 3142 if (bbr_lt_intvl_fp == 0) { 3143 /* Not doing false-positive detection */ 3144 return; 3145 } 3146 /* False positive detection */ 3147 if (diff == bbr_lt_intvl_fp) { 3148 /* At bbr_lt_intvl_fp we record the lost */ 3149 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3150 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3151 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) { 3152 /* Now is our loss rate still high? */ 3153 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3154 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3155 if ((delivered == 0) || 3156 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) { 3157 /* No still below our threshold */ 3158 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0); 3159 } else { 3160 /* Yikes its still high, it must be a false positive */ 3161 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0); 3162 goto reset_all; 3163 } 3164 } 3165 return; 3166 } 3167 /* 3168 * Wait for the first loss before sampling, to let the policer 3169 * exhaust its tokens and estimate the steady-state rate allowed by 3170 * the policer. Starting samples earlier includes bursts that 3171 * over-estimate the bw. 3172 */ 3173 if (bbr->rc_lt_is_sampling == 0) { 3174 /* reason 1 is to begin doing the sampling */ 3175 if (loss_detected == 0) 3176 return; 3177 bbr_reset_lt_bw_interval(bbr, cts); 3178 bbr->rc_lt_is_sampling = 1; 3179 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0); 3180 return; 3181 } 3182 /* Now how long were we delivering long term last> */ 3183 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time)) 3184 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time; 3185 else 3186 d_time = 0; 3187 3188 /* To avoid underestimates, reset sampling if we run out of data. */ 3189 if (bbr->r_ctl.r_app_limited_until) { 3190 /* Can not measure in app-limited state */ 3191 bbr_reset_lt_bw_sampling(bbr, cts); 3192 /* reason 2 is to reset sampling due to app limits */ 3193 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time); 3194 return; 3195 } 3196 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 3197 if (diff < bbr_lt_intvl_min_rtts) { 3198 /* 3199 * need more samples (we don't 3200 * start on a round like linux so 3201 * we need 1 more). 3202 */ 3203 /* 6 is not_enough time or no-loss */ 3204 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3205 return; 3206 } 3207 if (diff > (4 * bbr_lt_intvl_min_rtts)) { 3208 /* 3209 * For now if we wait too long, reset all sampling. We need 3210 * to do some research here, its possible that we should 3211 * base this on how much loss as occurred.. something like 3212 * if its under 10% (or some thresh) reset all otherwise 3213 * don't. Thats for phase II I guess. 3214 */ 3215 bbr_reset_lt_bw_sampling(bbr, cts); 3216 /* reason 3 is to reset sampling due too long of sampling */ 3217 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3218 return; 3219 } 3220 /* 3221 * End sampling interval when a packet is lost, so we estimate the 3222 * policer tokens were exhausted. Stopping the sampling before the 3223 * tokens are exhausted under-estimates the policed rate. 3224 */ 3225 if (loss_detected == 0) { 3226 /* 6 is not_enough time or no-loss */ 3227 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3228 return; 3229 } 3230 /* Calculate packets lost and delivered in sampling interval. */ 3231 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3232 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3233 if ((delivered == 0) || 3234 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) { 3235 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time); 3236 return; 3237 } 3238 if (d_time < 1000) { 3239 /* Not enough time. wait */ 3240 /* 6 is not_enough time or no-loss */ 3241 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3242 return; 3243 } 3244 if (d_time >= (0xffffffff / USECS_IN_MSEC)) { 3245 /* Too long */ 3246 bbr_reset_lt_bw_sampling(bbr, cts); 3247 /* reason 3 is to reset sampling due too long of sampling */ 3248 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3249 return; 3250 } 3251 del_time = d_time; 3252 bw = delivered; 3253 bw *= (uint64_t)USECS_IN_SECOND; 3254 bw /= del_time; 3255 bbr_lt_bw_samp_done(bbr, bw, cts, d_time); 3256 } 3257 3258 /* 3259 * Allocate a sendmap from our zone. 3260 */ 3261 static struct bbr_sendmap * 3262 bbr_alloc(struct tcp_bbr *bbr) 3263 { 3264 struct bbr_sendmap *rsm; 3265 3266 BBR_STAT_INC(bbr_to_alloc); 3267 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO)); 3268 if (rsm) { 3269 bbr->r_ctl.rc_num_maps_alloced++; 3270 return (rsm); 3271 } 3272 if (bbr->r_ctl.rc_free_cnt) { 3273 BBR_STAT_INC(bbr_to_alloc_emerg); 3274 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 3275 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 3276 bbr->r_ctl.rc_free_cnt--; 3277 return (rsm); 3278 } 3279 BBR_STAT_INC(bbr_to_alloc_failed); 3280 return (NULL); 3281 } 3282 3283 static struct bbr_sendmap * 3284 bbr_alloc_full_limit(struct tcp_bbr *bbr) 3285 { 3286 if ((V_tcp_map_entries_limit > 0) && 3287 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3288 BBR_STAT_INC(bbr_alloc_limited); 3289 if (!bbr->alloc_limit_reported) { 3290 bbr->alloc_limit_reported = 1; 3291 BBR_STAT_INC(bbr_alloc_limited_conns); 3292 } 3293 return (NULL); 3294 } 3295 return (bbr_alloc(bbr)); 3296 } 3297 3298 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3299 static struct bbr_sendmap * 3300 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type) 3301 { 3302 struct bbr_sendmap *rsm; 3303 3304 if (limit_type) { 3305 /* currently there is only one limit type */ 3306 if (V_tcp_map_split_limit > 0 && 3307 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3308 BBR_STAT_INC(bbr_split_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 } 3316 3317 /* allocate and mark in the limit type, if set */ 3318 rsm = bbr_alloc(bbr); 3319 if (rsm != NULL && limit_type) { 3320 rsm->r_limit_type = limit_type; 3321 bbr->r_ctl.rc_num_split_allocs++; 3322 } 3323 return (rsm); 3324 } 3325 3326 static void 3327 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 3328 { 3329 if (rsm->r_limit_type) { 3330 /* currently there is only one limit type */ 3331 bbr->r_ctl.rc_num_split_allocs--; 3332 } 3333 if (rsm->r_is_smallmap) 3334 bbr->r_ctl.rc_num_small_maps_alloced--; 3335 if (bbr->r_ctl.rc_tlp_send == rsm) 3336 bbr->r_ctl.rc_tlp_send = NULL; 3337 if (bbr->r_ctl.rc_resend == rsm) { 3338 bbr->r_ctl.rc_resend = NULL; 3339 } 3340 if (bbr->r_ctl.rc_next == rsm) 3341 bbr->r_ctl.rc_next = NULL; 3342 if (bbr->r_ctl.rc_sacklast == rsm) 3343 bbr->r_ctl.rc_sacklast = NULL; 3344 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 3345 memset(rsm, 0, sizeof(struct bbr_sendmap)); 3346 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 3347 rsm->r_limit_type = 0; 3348 bbr->r_ctl.rc_free_cnt++; 3349 return; 3350 } 3351 bbr->r_ctl.rc_num_maps_alloced--; 3352 uma_zfree(bbr_zone, rsm); 3353 } 3354 3355 /* 3356 * Returns the BDP. 3357 */ 3358 static uint64_t 3359 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) { 3360 /* 3361 * Calculate the bytes in flight needed given the bw (in bytes per 3362 * second) and the specifyed rtt in useconds. We need to put out the 3363 * returned value per RTT to match that rate. Gain will normally 3364 * raise it up from there. 3365 * 3366 * This should not overflow as long as the bandwidth is below 1 3367 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller 3368 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30). 3369 */ 3370 uint64_t usec_per_sec; 3371 3372 usec_per_sec = USECS_IN_SECOND; 3373 return ((rtt * bw) / usec_per_sec); 3374 } 3375 3376 /* 3377 * Return the initial cwnd. 3378 */ 3379 static uint32_t 3380 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp) 3381 { 3382 uint32_t i_cwnd; 3383 3384 if (bbr->rc_init_win) { 3385 i_cwnd = bbr->rc_init_win * tp->t_maxseg; 3386 } else if (V_tcp_initcwnd_segments) 3387 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg), 3388 max(2 * tp->t_maxseg, 14600)); 3389 else if (V_tcp_do_rfc3390) 3390 i_cwnd = min(4 * tp->t_maxseg, 3391 max(2 * tp->t_maxseg, 4380)); 3392 else { 3393 /* Per RFC5681 Section 3.1 */ 3394 if (tp->t_maxseg > 2190) 3395 i_cwnd = 2 * tp->t_maxseg; 3396 else if (tp->t_maxseg > 1095) 3397 i_cwnd = 3 * tp->t_maxseg; 3398 else 3399 i_cwnd = 4 * tp->t_maxseg; 3400 } 3401 return (i_cwnd); 3402 } 3403 3404 /* 3405 * Given a specified gain, return the target 3406 * cwnd based on that gain. 3407 */ 3408 static uint32_t 3409 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw) 3410 { 3411 uint64_t bdp, rtt; 3412 uint32_t cwnd; 3413 3414 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) || 3415 (bbr_get_full_bw(bbr) == 0)) { 3416 /* No measurements yet */ 3417 return (bbr_initial_cwnd(bbr, bbr->rc_tp)); 3418 } 3419 /* 3420 * Get bytes per RTT needed (rttProp is normally in 3421 * bbr_cwndtarget_rtt_touse) 3422 */ 3423 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse); 3424 /* Get the bdp from the two values */ 3425 bdp = bbr_get_bw_delay_prod(rtt, bw); 3426 /* Now apply the gain */ 3427 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT)); 3428 3429 return (cwnd); 3430 } 3431 3432 static uint32_t 3433 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain) 3434 { 3435 uint32_t cwnd, mss; 3436 3437 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 3438 /* Get the base cwnd with gain rounded to a mss */ 3439 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss); 3440 /* 3441 * Add in N (2 default since we do not have a 3442 * fq layer to trap packets in) quanta's per the I-D 3443 * section 4.2.3.2 quanta adjust. 3444 */ 3445 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs); 3446 if (bbr->rc_use_google) { 3447 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3448 (bbr_state_val(bbr) == BBR_SUB_GAIN)) { 3449 /* 3450 * The linux implementation adds 3451 * an extra 2 x mss in gain cycle which 3452 * is documented no-where except in the code. 3453 * so we add more for Neal undocumented feature 3454 */ 3455 cwnd += 2 * mss; 3456 } 3457 if ((cwnd / mss) & 0x1) { 3458 /* Round up for odd num mss */ 3459 cwnd += mss; 3460 } 3461 } 3462 /* Are we below the min cwnd? */ 3463 if (cwnd < get_min_cwnd(bbr)) 3464 return (get_min_cwnd(bbr)); 3465 return (cwnd); 3466 } 3467 3468 static uint16_t 3469 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain) 3470 { 3471 if (gain < 1) 3472 gain = 1; 3473 return (gain); 3474 } 3475 3476 static uint32_t 3477 bbr_get_header_oh(struct tcp_bbr *bbr) 3478 { 3479 int seg_oh; 3480 3481 seg_oh = 0; 3482 if (bbr->r_ctl.rc_inc_tcp_oh) { 3483 /* Do we include TCP overhead? */ 3484 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr)); 3485 } 3486 if (bbr->r_ctl.rc_inc_ip_oh) { 3487 /* Do we include IP overhead? */ 3488 #ifdef INET6 3489 if (bbr->r_is_v6) { 3490 seg_oh += sizeof(struct ip6_hdr); 3491 } else 3492 #endif 3493 { 3494 3495 #ifdef INET 3496 seg_oh += sizeof(struct ip); 3497 #endif 3498 } 3499 } 3500 if (bbr->r_ctl.rc_inc_enet_oh) { 3501 /* Do we include the ethernet overhead? */ 3502 seg_oh += sizeof(struct ether_header); 3503 } 3504 return(seg_oh); 3505 } 3506 3507 static uint32_t 3508 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw) 3509 { 3510 uint64_t divor, res, tim; 3511 3512 if (useconds_time == 0) 3513 return (0); 3514 gain = bbr_gain_adjust(bbr, gain); 3515 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT; 3516 tim = useconds_time; 3517 res = (tim * bw * gain) / divor; 3518 if (res == 0) 3519 res = 1; 3520 return ((uint32_t)res); 3521 } 3522 3523 /* 3524 * Given a gain and a length return the delay in useconds that 3525 * should be used to evenly space out packets 3526 * on the connection (based on the gain factor). 3527 */ 3528 static uint32_t 3529 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog) 3530 { 3531 uint64_t bw, lentim, res; 3532 uint32_t usecs, srtt, over = 0; 3533 uint32_t seg_oh, num_segs, maxseg; 3534 3535 if (len == 0) 3536 return (0); 3537 3538 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 3539 num_segs = (len + maxseg - 1) / maxseg; 3540 if (bbr->rc_use_google == 0) { 3541 seg_oh = bbr_get_header_oh(bbr); 3542 len += (num_segs * seg_oh); 3543 } 3544 gain = bbr_gain_adjust(bbr, gain); 3545 bw = bbr_get_bw(bbr); 3546 if (bbr->rc_use_google) { 3547 uint64_t cbw; 3548 3549 /* 3550 * Reduce the b/w by the google discount 3551 * factor 10 = 1%. 3552 */ 3553 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount); 3554 cbw /= (uint64_t)1000; 3555 /* We don't apply a discount if it results in 0 */ 3556 if (cbw > 0) 3557 bw = cbw; 3558 } 3559 lentim = ((uint64_t)len * 3560 (uint64_t)USECS_IN_SECOND * 3561 (uint64_t)BBR_UNIT); 3562 res = lentim / ((uint64_t)gain * bw); 3563 if (res == 0) 3564 res = 1; 3565 usecs = (uint32_t)res; 3566 srtt = bbr_get_rtt(bbr, BBR_SRTT); 3567 if (bbr_hptsi_max_mul && bbr_hptsi_max_div && 3568 (bbr->rc_use_google == 0) && 3569 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) { 3570 /* 3571 * We cannot let the delay be more than 1/2 the srtt time. 3572 * Otherwise we cannot pace out or send properly. 3573 */ 3574 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div; 3575 BBR_STAT_INC(bbr_hpts_min_time); 3576 } 3577 if (!nolog) 3578 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1); 3579 return (usecs); 3580 } 3581 3582 static void 3583 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack, 3584 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses) 3585 { 3586 uint64_t bw; 3587 uint32_t cwnd, target_cwnd, saved_bytes, maxseg; 3588 int32_t meth; 3589 3590 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3591 3592 #ifdef STATS 3593 if ((tp->t_flags & TF_GPUTINPROG) && 3594 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 3595 /* 3596 * Strech acks and compressed acks will cause this to 3597 * oscillate but we are doing it the same way as the main 3598 * stack so it will be compariable (though possibly not 3599 * ideal). 3600 */ 3601 int32_t cgput; 3602 int64_t gput, time_stamp; 3603 3604 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8; 3605 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000)); 3606 cgput = gput / time_stamp; 3607 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 3608 cgput); 3609 if (tp->t_stats_gput_prev > 0) 3610 stats_voi_update_abs_s32(tp->t_stats, 3611 VOI_TCP_GPUT_ND, 3612 ((gput - tp->t_stats_gput_prev) * 100) / 3613 tp->t_stats_gput_prev); 3614 tp->t_flags &= ~TF_GPUTINPROG; 3615 tp->t_stats_gput_prev = cgput; 3616 } 3617 #endif 3618 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3619 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 3620 /* We don't change anything in probe-rtt */ 3621 return; 3622 } 3623 maxseg = tp->t_maxseg - bbr->rc_last_options; 3624 saved_bytes = bytes_this_ack; 3625 bytes_this_ack += sack_changed; 3626 if (bytes_this_ack > prev_acked) { 3627 bytes_this_ack -= prev_acked; 3628 /* 3629 * A byte ack'd gives us a full mss 3630 * to be like linux i.e. they count packets. 3631 */ 3632 if ((bytes_this_ack < maxseg) && bbr->rc_use_google) 3633 bytes_this_ack = maxseg; 3634 } else { 3635 /* Unlikely */ 3636 bytes_this_ack = 0; 3637 } 3638 cwnd = tp->snd_cwnd; 3639 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3640 if (bw) 3641 target_cwnd = bbr_get_target_cwnd(bbr, 3642 bw, 3643 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain); 3644 else 3645 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp); 3646 if (IN_RECOVERY(tp->t_flags) && 3647 (bbr->bbr_prev_in_rec == 0)) { 3648 /* 3649 * We are entering recovery and 3650 * thus packet conservation. 3651 */ 3652 bbr->pkt_conservation = 1; 3653 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime; 3654 cwnd = ctf_flight_size(tp, 3655 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3656 bytes_this_ack; 3657 } 3658 if (IN_RECOVERY(tp->t_flags)) { 3659 uint32_t flight; 3660 3661 bbr->bbr_prev_in_rec = 1; 3662 if (cwnd > losses) { 3663 cwnd -= losses; 3664 if (cwnd < maxseg) 3665 cwnd = maxseg; 3666 } else 3667 cwnd = maxseg; 3668 flight = ctf_flight_size(tp, 3669 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3670 bbr_log_type_cwndupd(bbr, flight, 0, 3671 losses, 10, 0, 0, line); 3672 if (bbr->pkt_conservation) { 3673 uint32_t time_in; 3674 3675 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start)) 3676 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start; 3677 else 3678 time_in = 0; 3679 3680 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 3681 /* Clear packet conservation after an rttProp */ 3682 bbr->pkt_conservation = 0; 3683 } else { 3684 if ((flight + bytes_this_ack) > cwnd) 3685 cwnd = flight + bytes_this_ack; 3686 if (cwnd < get_min_cwnd(bbr)) 3687 cwnd = get_min_cwnd(bbr); 3688 tp->snd_cwnd = cwnd; 3689 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, 3690 prev_acked, 1, target_cwnd, th->th_ack, line); 3691 return; 3692 } 3693 } 3694 } else 3695 bbr->bbr_prev_in_rec = 0; 3696 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) { 3697 bbr->r_ctl.restrict_growth--; 3698 if (bytes_this_ack > maxseg) 3699 bytes_this_ack = maxseg; 3700 } 3701 if (bbr->rc_filled_pipe) { 3702 /* 3703 * Here we have exited startup and filled the pipe. We will 3704 * thus allow the cwnd to shrink to the target. We hit here 3705 * mostly. 3706 */ 3707 uint32_t s_cwnd; 3708 3709 meth = 2; 3710 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd); 3711 if (s_cwnd > cwnd) 3712 cwnd = s_cwnd; 3713 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing) 3714 cwnd = s_cwnd; 3715 } else { 3716 /* 3717 * Here we are still in startup, we increase cwnd by what 3718 * has been acked. 3719 */ 3720 if ((cwnd < target_cwnd) || 3721 (bbr->rc_past_init_win == 0)) { 3722 meth = 3; 3723 cwnd += bytes_this_ack; 3724 } else { 3725 /* 3726 * Method 4 means we are at target so no gain in 3727 * startup and past the initial window. 3728 */ 3729 meth = 4; 3730 } 3731 } 3732 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr)); 3733 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line); 3734 } 3735 3736 static void 3737 tcp_bbr_partialack(struct tcpcb *tp) 3738 { 3739 struct tcp_bbr *bbr; 3740 3741 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3742 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3743 if (ctf_flight_size(tp, 3744 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 3745 tp->snd_cwnd) { 3746 bbr->r_wanted_output = 1; 3747 } 3748 } 3749 3750 static void 3751 bbr_post_recovery(struct tcpcb *tp) 3752 { 3753 struct tcp_bbr *bbr; 3754 uint32_t flight; 3755 3756 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3757 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3758 /* 3759 * Here we just exit recovery. 3760 */ 3761 EXIT_RECOVERY(tp->t_flags); 3762 /* Lock in our b/w reduction for the specified number of pkt-epochs */ 3763 bbr->r_recovery_bw = 0; 3764 tp->snd_recover = tp->snd_una; 3765 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3766 bbr->pkt_conservation = 0; 3767 if (bbr->rc_use_google == 0) { 3768 /* 3769 * For non-google mode lets 3770 * go ahead and make sure we clear 3771 * the recovery state so if we 3772 * bounce back in to recovery we 3773 * will do PC. 3774 */ 3775 bbr->bbr_prev_in_rec = 0; 3776 } 3777 bbr_log_type_exit_rec(bbr); 3778 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3779 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3780 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__); 3781 } else { 3782 /* For probe-rtt case lets fix up its saved_cwnd */ 3783 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) { 3784 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent; 3785 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__); 3786 } 3787 } 3788 flight = ctf_flight_size(tp, 3789 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3790 if ((bbr->rc_use_google == 0) && 3791 bbr_do_red) { 3792 uint64_t val, lr2use; 3793 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd; 3794 uint32_t *cwnd_p; 3795 3796 if (bbr_get_rtt(bbr, BBR_SRTT)) { 3797 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000); 3798 val /= bbr_get_rtt(bbr, BBR_SRTT); 3799 ratio = (uint32_t)val; 3800 } else 3801 ratio = 1000; 3802 3803 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, 3804 bbr->r_ctl.recovery_lr, 21, 3805 ratio, 3806 bbr->r_ctl.rc_red_cwnd_pe, 3807 __LINE__); 3808 if ((ratio < bbr_do_red) || (bbr_do_red == 0)) 3809 goto done; 3810 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3811 bbr_prtt_slam_cwnd) || 3812 (bbr_sub_drain_slam_cwnd && 3813 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3814 bbr->rc_hit_state_1 && 3815 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) || 3816 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) && 3817 bbr_slam_cwnd_in_main_drain)) { 3818 /* 3819 * Here we must poke at the saved cwnd 3820 * as well as the cwnd. 3821 */ 3822 cwnd = bbr->r_ctl.rc_saved_cwnd; 3823 cwnd_p = &bbr->r_ctl.rc_saved_cwnd; 3824 } else { 3825 cwnd = tp->snd_cwnd; 3826 cwnd_p = &tp->snd_cwnd; 3827 } 3828 maxseg = tp->t_maxseg - bbr->rc_last_options; 3829 /* Add the overall lr with the recovery lr */ 3830 if (bbr->r_ctl.rc_lost == 0) 3831 lr2use = 0; 3832 else if (bbr->r_ctl.rc_delivered == 0) 3833 lr2use = 1000; 3834 else { 3835 lr2use = (uint64_t)bbr->r_ctl.rc_lost * (uint64_t)1000; 3836 lr2use /= bbr->r_ctl.rc_delivered; 3837 } 3838 lr2use += bbr->r_ctl.recovery_lr; 3839 acks_inflight = (flight / (maxseg * 2)); 3840 if (bbr_red_scale) { 3841 lr2use *= bbr_get_rtt(bbr, BBR_SRTT); 3842 lr2use /= bbr_red_scale; 3843 if ((bbr_red_growth_restrict) && 3844 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1)) 3845 bbr->r_ctl.restrict_growth += acks_inflight; 3846 } 3847 if (lr2use) { 3848 val = (uint64_t)cwnd * lr2use; 3849 val /= 1000; 3850 if (cwnd > val) 3851 newcwnd = roundup((cwnd - val), maxseg); 3852 else 3853 newcwnd = maxseg; 3854 } else { 3855 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul; 3856 val /= (uint64_t)bbr_red_div; 3857 newcwnd = roundup((uint32_t)val, maxseg); 3858 } 3859 /* with standard delayed acks how many acks can I expect? */ 3860 if (bbr_drop_limit == 0) { 3861 /* 3862 * Anticpate how much we will 3863 * raise the cwnd based on the acks. 3864 */ 3865 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) { 3866 /* We do enforce the min (with the acks) */ 3867 newcwnd = (get_min_cwnd(bbr) - acks_inflight); 3868 } 3869 } else { 3870 /* 3871 * A strict drop limit of N is inplace 3872 */ 3873 if (newcwnd < (bbr_drop_limit * maxseg)) { 3874 newcwnd = bbr_drop_limit * maxseg; 3875 } 3876 } 3877 /* For the next N acks do we restrict the growth */ 3878 *cwnd_p = newcwnd; 3879 if (tp->snd_cwnd > newcwnd) 3880 tp->snd_cwnd = newcwnd; 3881 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22, 3882 (uint32_t)lr2use, 3883 bbr_get_rtt(bbr, BBR_SRTT), __LINE__); 3884 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch; 3885 } 3886 done: 3887 bbr->r_ctl.recovery_lr = 0; 3888 if (flight <= tp->snd_cwnd) { 3889 bbr->r_wanted_output = 1; 3890 } 3891 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3892 } 3893 3894 static void 3895 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts) 3896 { 3897 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3898 /* Limit the drop in b/w to 1/2 our current filter. */ 3899 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate) 3900 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate; 3901 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2)) 3902 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2; 3903 tcp_bbr_tso_size_check(bbr, cts); 3904 } 3905 3906 static void 3907 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm) 3908 { 3909 struct tcp_bbr *bbr; 3910 3911 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3912 #ifdef STATS 3913 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 3914 #endif 3915 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3916 switch (type) { 3917 case CC_NDUPACK: 3918 if (!IN_RECOVERY(tp->t_flags)) { 3919 tp->snd_recover = tp->snd_max; 3920 /* Start a new epoch */ 3921 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 3922 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) { 3923 /* 3924 * Move forward the lt epoch 3925 * so it won't count the truncated 3926 * epoch. 3927 */ 3928 bbr->r_ctl.rc_lt_epoch++; 3929 } 3930 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 3931 /* 3932 * Just like the policer detection code 3933 * if we are in startup we must push 3934 * forward the last startup epoch 3935 * to hide the truncated PE. 3936 */ 3937 bbr->r_ctl.rc_bbr_last_startup_epoch++; 3938 } 3939 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd; 3940 ENTER_RECOVERY(tp->t_flags); 3941 bbr->rc_tlp_rtx_out = 0; 3942 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate; 3943 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3944 if (tcp_in_hpts(bbr->rc_tp) && 3945 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) { 3946 /* 3947 * When we enter recovery, we need to restart 3948 * any timers. This may mean we gain an agg 3949 * early, which will be made up for at the last 3950 * rxt out. 3951 */ 3952 bbr->rc_timer_first = 1; 3953 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 3954 } 3955 /* 3956 * Calculate a new cwnd based on to the current 3957 * delivery rate with no gain. We get the bdp 3958 * without gaining it up like we normally would and 3959 * we use the last cur_del_rate. 3960 */ 3961 if ((bbr->rc_use_google == 0) && 3962 (bbr->r_ctl.bbr_rttprobe_gain_val || 3963 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) { 3964 tp->snd_cwnd = ctf_flight_size(tp, 3965 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3966 (tp->t_maxseg - bbr->rc_last_options); 3967 if (tp->snd_cwnd < get_min_cwnd(bbr)) { 3968 /* We always gate to min cwnd */ 3969 tp->snd_cwnd = get_min_cwnd(bbr); 3970 } 3971 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__); 3972 } 3973 bbr_log_type_enter_rec(bbr, rsm->r_start); 3974 } 3975 break; 3976 case CC_RTO_ERR: 3977 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 3978 /* RTO was unnecessary, so reset everything. */ 3979 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime); 3980 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3981 tp->snd_cwnd = tp->snd_cwnd_prev; 3982 tp->snd_ssthresh = tp->snd_ssthresh_prev; 3983 tp->snd_recover = tp->snd_recover_prev; 3984 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3985 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__); 3986 } 3987 tp->t_badrxtwin = 0; 3988 break; 3989 } 3990 } 3991 3992 /* 3993 * Indicate whether this ack should be delayed. We can delay the ack if 3994 * following conditions are met: 3995 * - There is no delayed ack timer in progress. 3996 * - Our last ack wasn't a 0-sized window. We never want to delay 3997 * the ack that opens up a 0-sized window. 3998 * - LRO wasn't used for this segment. We make sure by checking that the 3999 * segment size is not larger than the MSS. 4000 * - Delayed acks are enabled or this is a half-synchronized T/TCP 4001 * connection. 4002 * - The data being acked is less than a full segment (a stretch ack 4003 * of more than a segment we should ack. 4004 * - nsegs is 1 (if its more than that we received more than 1 ack). 4005 */ 4006 #define DELAY_ACK(tp, bbr, nsegs) \ 4007 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4008 ((tp->t_flags & TF_DELACK) == 0) && \ 4009 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \ 4010 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 4011 4012 /* 4013 * Return the lowest RSM in the map of 4014 * packets still in flight that is not acked. 4015 * This should normally find on the first one 4016 * since we remove packets from the send 4017 * map after they are marked ACKED. 4018 */ 4019 static struct bbr_sendmap * 4020 bbr_find_lowest_rsm(struct tcp_bbr *bbr) 4021 { 4022 struct bbr_sendmap *rsm; 4023 4024 /* 4025 * Walk the time-order transmitted list looking for an rsm that is 4026 * not acked. This will be the one that was sent the longest time 4027 * ago that is still outstanding. 4028 */ 4029 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) { 4030 if (rsm->r_flags & BBR_ACKED) { 4031 continue; 4032 } 4033 goto finish; 4034 } 4035 finish: 4036 return (rsm); 4037 } 4038 4039 static struct bbr_sendmap * 4040 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 4041 { 4042 struct bbr_sendmap *prsm; 4043 4044 /* 4045 * Walk the sequence order list backward until we hit and arrive at 4046 * the highest seq not acked. In theory when this is called it 4047 * should be the last segment (which it was not). 4048 */ 4049 prsm = rsm; 4050 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4051 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) { 4052 continue; 4053 } 4054 return (prsm); 4055 } 4056 return (NULL); 4057 } 4058 4059 /* 4060 * Returns to the caller the number of microseconds that 4061 * the packet can be outstanding before we think we 4062 * should have had an ack returned. 4063 */ 4064 static uint32_t 4065 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm) 4066 { 4067 /* 4068 * lro is the flag we use to determine if we have seen reordering. 4069 * If it gets set we have seen reordering. The reorder logic either 4070 * works in one of two ways: 4071 * 4072 * If reorder-fade is configured, then we track the last time we saw 4073 * re-ordering occur. If we reach the point where enough time as 4074 * passed we no longer consider reordering has occuring. 4075 * 4076 * Or if reorder-face is 0, then once we see reordering we consider 4077 * the connection to alway be subject to reordering and just set lro 4078 * to 1. 4079 * 4080 * In the end if lro is non-zero we add the extra time for 4081 * reordering in. 4082 */ 4083 int32_t lro; 4084 uint32_t thresh, t_rxtcur; 4085 4086 if (srtt == 0) 4087 srtt = 1; 4088 if (bbr->r_ctl.rc_reorder_ts) { 4089 if (bbr->r_ctl.rc_reorder_fade) { 4090 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) { 4091 lro = cts - bbr->r_ctl.rc_reorder_ts; 4092 if (lro == 0) { 4093 /* 4094 * No time as passed since the last 4095 * reorder, mark it as reordering. 4096 */ 4097 lro = 1; 4098 } 4099 } else { 4100 /* Negative time? */ 4101 lro = 0; 4102 } 4103 if (lro > bbr->r_ctl.rc_reorder_fade) { 4104 /* Turn off reordering seen too */ 4105 bbr->r_ctl.rc_reorder_ts = 0; 4106 lro = 0; 4107 } 4108 } else { 4109 /* Reodering does not fade */ 4110 lro = 1; 4111 } 4112 } else { 4113 lro = 0; 4114 } 4115 thresh = srtt + bbr->r_ctl.rc_pkt_delay; 4116 if (lro) { 4117 /* It must be set, if not you get 1/4 rtt */ 4118 if (bbr->r_ctl.rc_reorder_shift) 4119 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift); 4120 else 4121 thresh += (srtt >> 2); 4122 } else { 4123 thresh += 1000; 4124 } 4125 /* We don't let the rack timeout be above a RTO */ 4126 if ((bbr->rc_tp)->t_srtt == 0) 4127 t_rxtcur = BBR_INITIAL_RTO; 4128 else 4129 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 4130 if (thresh > t_rxtcur) { 4131 thresh = t_rxtcur; 4132 } 4133 /* And we don't want it above the RTO max either */ 4134 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4135 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4136 } 4137 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK); 4138 return (thresh); 4139 } 4140 4141 /* 4142 * Return to the caller the amount of time in mico-seconds 4143 * that should be used for the TLP timer from the last 4144 * send time of this packet. 4145 */ 4146 static uint32_t 4147 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 4148 struct bbr_sendmap *rsm, uint32_t srtt, 4149 uint32_t cts) 4150 { 4151 uint32_t thresh, len, maxseg, t_rxtcur; 4152 struct bbr_sendmap *prsm; 4153 4154 if (srtt == 0) 4155 srtt = 1; 4156 if (bbr->rc_tlp_threshold) 4157 thresh = srtt + (srtt / bbr->rc_tlp_threshold); 4158 else 4159 thresh = (srtt * 2); 4160 maxseg = tp->t_maxseg - bbr->rc_last_options; 4161 /* Get the previous sent packet, if any */ 4162 len = rsm->r_end - rsm->r_start; 4163 4164 /* 2.1 behavior */ 4165 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext); 4166 if (prsm && (len <= maxseg)) { 4167 /* 4168 * Two packets outstanding, thresh should be (2*srtt) + 4169 * possible inter-packet delay (if any). 4170 */ 4171 uint32_t inter_gap = 0; 4172 int idx, nidx; 4173 4174 idx = rsm->r_rtr_cnt - 1; 4175 nidx = prsm->r_rtr_cnt - 1; 4176 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 4177 /* Yes it was sent later (or at the same time) */ 4178 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 4179 } 4180 thresh += inter_gap; 4181 } else if (len <= maxseg) { 4182 /* 4183 * Possibly compensate for delayed-ack. 4184 */ 4185 uint32_t alt_thresh; 4186 4187 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time; 4188 if (alt_thresh > thresh) 4189 thresh = alt_thresh; 4190 } 4191 /* Not above the current RTO */ 4192 if (tp->t_srtt == 0) 4193 t_rxtcur = BBR_INITIAL_RTO; 4194 else 4195 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur); 4196 4197 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP); 4198 /* Not above an RTO */ 4199 if (thresh > t_rxtcur) { 4200 thresh = t_rxtcur; 4201 } 4202 /* Not above a RTO max */ 4203 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4204 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4205 } 4206 /* And now apply the user TLP min */ 4207 if (thresh < bbr_tlp_min) { 4208 thresh = bbr_tlp_min; 4209 } 4210 return (thresh); 4211 } 4212 4213 /* 4214 * Return one of three RTTs to use (in microseconds). 4215 */ 4216 static __inline uint32_t 4217 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type) 4218 { 4219 uint32_t f_rtt; 4220 uint32_t srtt; 4221 4222 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 4223 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) { 4224 /* We have no rtt at all */ 4225 if (bbr->rc_tp->t_srtt == 0) 4226 f_rtt = BBR_INITIAL_RTO; 4227 else 4228 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4229 /* 4230 * Since we don't know how good the rtt is apply a 4231 * delayed-ack min 4232 */ 4233 if (f_rtt < bbr_delayed_ack_time) { 4234 f_rtt = bbr_delayed_ack_time; 4235 } 4236 } 4237 /* Take the filter version or last measured pkt-rtt */ 4238 if (rtt_type == BBR_RTT_PROP) { 4239 srtt = f_rtt; 4240 } else if (rtt_type == BBR_RTT_PKTRTT) { 4241 if (bbr->r_ctl.rc_pkt_epoch_rtt) { 4242 srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 4243 } else { 4244 /* No pkt rtt yet */ 4245 srtt = f_rtt; 4246 } 4247 } else if (rtt_type == BBR_RTT_RACK) { 4248 srtt = bbr->r_ctl.rc_last_rtt; 4249 /* We need to add in any internal delay for our timer */ 4250 if (bbr->rc_ack_was_delayed) 4251 srtt += bbr->r_ctl.rc_ack_hdwr_delay; 4252 } else if (rtt_type == BBR_SRTT) { 4253 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4254 } else { 4255 /* TSNH */ 4256 srtt = f_rtt; 4257 #ifdef BBR_INVARIANTS 4258 panic("Unknown rtt request type %d", rtt_type); 4259 #endif 4260 } 4261 return (srtt); 4262 } 4263 4264 static int 4265 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts) 4266 { 4267 uint32_t thresh; 4268 4269 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK), 4270 cts, rsm); 4271 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) { 4272 /* It is lost (past time) */ 4273 return (1); 4274 } 4275 return (0); 4276 } 4277 4278 /* 4279 * Return a sendmap if we need to retransmit something. 4280 */ 4281 static struct bbr_sendmap * 4282 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4283 { 4284 /* 4285 * Check to see that we don't need to fall into recovery. We will 4286 * need to do so if our oldest transmit is past the time we should 4287 * have had an ack. 4288 */ 4289 4290 struct bbr_sendmap *rsm; 4291 int32_t idx; 4292 4293 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) { 4294 /* Nothing outstanding that we know of */ 4295 return (NULL); 4296 } 4297 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 4298 if (rsm == NULL) { 4299 /* Nothing in the transmit map */ 4300 return (NULL); 4301 } 4302 if (tp->t_flags & TF_SENTFIN) { 4303 /* Fin restricted, don't find anything once a fin is sent */ 4304 return (NULL); 4305 } 4306 if (rsm->r_flags & BBR_ACKED) { 4307 /* 4308 * Ok the first one is acked (this really should not happen 4309 * since we remove the from the tmap once they are acked) 4310 */ 4311 rsm = bbr_find_lowest_rsm(bbr); 4312 if (rsm == NULL) 4313 return (NULL); 4314 } 4315 idx = rsm->r_rtr_cnt - 1; 4316 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) { 4317 /* Send timestamp is the same or less? can't be ready */ 4318 return (NULL); 4319 } 4320 /* Get our RTT time */ 4321 if (bbr_is_lost(bbr, rsm, cts) && 4322 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 4323 (rsm->r_flags & BBR_SACK_PASSED))) { 4324 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4325 rsm->r_flags |= BBR_MARKED_LOST; 4326 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4327 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4328 } 4329 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm); 4330 #ifdef BBR_INVARIANTS 4331 if ((rsm->r_end - rsm->r_start) == 0) 4332 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm); 4333 #endif 4334 return (rsm); 4335 } 4336 return (NULL); 4337 } 4338 4339 /* 4340 * RACK Timer, here we simply do logging and house keeping. 4341 * the normal bbr_output_wtime() function will call the 4342 * appropriate thing to check if we need to do a RACK retransmit. 4343 * We return 1, saying don't proceed with bbr_output_wtime only 4344 * when all timers have been stopped (destroyed PCB?). 4345 */ 4346 static int 4347 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4348 { 4349 /* 4350 * This timer simply provides an internal trigger to send out data. 4351 * The check_recovery_mode call will see if there are needed 4352 * retransmissions, if so we will enter fast-recovery. The output 4353 * call may or may not do the same thing depending on sysctl 4354 * settings. 4355 */ 4356 uint32_t lost; 4357 4358 if (bbr->rc_all_timers_stopped) { 4359 return (1); 4360 } 4361 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4362 /* Its not time yet */ 4363 return (0); 4364 } 4365 BBR_STAT_INC(bbr_to_tot); 4366 lost = bbr->r_ctl.rc_lost; 4367 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4368 bbr_set_state(tp, bbr, 0); 4369 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK); 4370 if (bbr->r_ctl.rc_resend == NULL) { 4371 /* Lets do the check here */ 4372 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 4373 } 4374 if (bbr_policer_call_from_rack_to) 4375 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4376 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 4377 return (0); 4378 } 4379 4380 static __inline void 4381 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start) 4382 { 4383 int idx; 4384 4385 nrsm->r_start = start; 4386 nrsm->r_end = rsm->r_end; 4387 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4388 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed; 4389 nrsm->r_flags = rsm->r_flags; 4390 /* We don't transfer forward the SYN flag */ 4391 nrsm->r_flags &= ~BBR_HAS_SYN; 4392 /* We move forward the FIN flag, not that this should happen */ 4393 rsm->r_flags &= ~BBR_HAS_FIN; 4394 nrsm->r_dupack = rsm->r_dupack; 4395 nrsm->r_rtr_bytes = 0; 4396 nrsm->r_is_gain = rsm->r_is_gain; 4397 nrsm->r_is_drain = rsm->r_is_drain; 4398 nrsm->r_delivered = rsm->r_delivered; 4399 nrsm->r_ts_valid = rsm->r_ts_valid; 4400 nrsm->r_del_ack_ts = rsm->r_del_ack_ts; 4401 nrsm->r_del_time = rsm->r_del_time; 4402 nrsm->r_app_limited = rsm->r_app_limited; 4403 nrsm->r_first_sent_time = rsm->r_first_sent_time; 4404 nrsm->r_flight_at_send = rsm->r_flight_at_send; 4405 /* We split a piece the lower section looses any just_ret flag. */ 4406 nrsm->r_bbr_state = rsm->r_bbr_state; 4407 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4408 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4409 } 4410 rsm->r_end = nrsm->r_start; 4411 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 4412 idx /= 8; 4413 /* Check if we got too small */ 4414 if ((rsm->r_is_smallmap == 0) && 4415 ((rsm->r_end - rsm->r_start) <= idx)) { 4416 bbr->r_ctl.rc_num_small_maps_alloced++; 4417 rsm->r_is_smallmap = 1; 4418 } 4419 /* Check the new one as well */ 4420 if ((nrsm->r_end - nrsm->r_start) <= idx) { 4421 bbr->r_ctl.rc_num_small_maps_alloced++; 4422 nrsm->r_is_smallmap = 1; 4423 } 4424 } 4425 4426 static int 4427 bbr_sack_mergable(struct bbr_sendmap *at, 4428 uint32_t start, uint32_t end) 4429 { 4430 /* 4431 * Given a sack block defined by 4432 * start and end, and a current position 4433 * at. Return 1 if either side of at 4434 * would show that the block is mergable 4435 * to that side. A block to be mergable 4436 * must have overlap with the start/end 4437 * and be in the SACK'd state. 4438 */ 4439 struct bbr_sendmap *l_rsm; 4440 struct bbr_sendmap *r_rsm; 4441 4442 /* first get the either side blocks */ 4443 l_rsm = TAILQ_PREV(at, bbr_head, r_next); 4444 r_rsm = TAILQ_NEXT(at, r_next); 4445 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) { 4446 /* Potentially mergeable */ 4447 if ((l_rsm->r_end == start) || 4448 (SEQ_LT(start, l_rsm->r_end) && 4449 SEQ_GT(end, l_rsm->r_end))) { 4450 /* 4451 * map blk |------| 4452 * sack blk |------| 4453 * <or> 4454 * map blk |------| 4455 * sack blk |------| 4456 */ 4457 return (1); 4458 } 4459 } 4460 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) { 4461 /* Potentially mergeable */ 4462 if ((r_rsm->r_start == end) || 4463 (SEQ_LT(start, r_rsm->r_start) && 4464 SEQ_GT(end, r_rsm->r_start))) { 4465 /* 4466 * map blk |---------| 4467 * sack blk |----| 4468 * <or> 4469 * map blk |---------| 4470 * sack blk |-------| 4471 */ 4472 return (1); 4473 } 4474 } 4475 return (0); 4476 } 4477 4478 static struct bbr_sendmap * 4479 bbr_merge_rsm(struct tcp_bbr *bbr, 4480 struct bbr_sendmap *l_rsm, 4481 struct bbr_sendmap *r_rsm) 4482 { 4483 /* 4484 * We are merging two ack'd RSM's, 4485 * the l_rsm is on the left (lower seq 4486 * values) and the r_rsm is on the right 4487 * (higher seq value). The simplest way 4488 * to merge these is to move the right 4489 * one into the left. I don't think there 4490 * is any reason we need to try to find 4491 * the oldest (or last oldest retransmitted). 4492 */ 4493 l_rsm->r_end = r_rsm->r_end; 4494 if (l_rsm->r_dupack < r_rsm->r_dupack) 4495 l_rsm->r_dupack = r_rsm->r_dupack; 4496 if (r_rsm->r_rtr_bytes) 4497 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 4498 if (r_rsm->r_in_tmap) { 4499 /* This really should not happen */ 4500 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext); 4501 } 4502 if (r_rsm->r_app_limited) 4503 l_rsm->r_app_limited = r_rsm->r_app_limited; 4504 /* Now the flags */ 4505 if (r_rsm->r_flags & BBR_HAS_FIN) 4506 l_rsm->r_flags |= BBR_HAS_FIN; 4507 if (r_rsm->r_flags & BBR_TLP) 4508 l_rsm->r_flags |= BBR_TLP; 4509 if (r_rsm->r_flags & BBR_RWND_COLLAPSED) 4510 l_rsm->r_flags |= BBR_RWND_COLLAPSED; 4511 if (r_rsm->r_flags & BBR_MARKED_LOST) { 4512 /* This really should not happen */ 4513 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start; 4514 } 4515 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next); 4516 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 4517 /* Transfer the split limit to the map we free */ 4518 r_rsm->r_limit_type = l_rsm->r_limit_type; 4519 l_rsm->r_limit_type = 0; 4520 } 4521 bbr_free(bbr, r_rsm); 4522 return(l_rsm); 4523 } 4524 4525 /* 4526 * TLP Timer, here we simply setup what segment we want to 4527 * have the TLP expire on, the normal bbr_output_wtime() will then 4528 * send it out. 4529 * 4530 * We return 1, saying don't proceed with bbr_output_wtime only 4531 * when all timers have been stopped (destroyed PCB?). 4532 */ 4533 static int 4534 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4535 { 4536 /* 4537 * Tail Loss Probe. 4538 */ 4539 struct bbr_sendmap *rsm = NULL; 4540 struct socket *so; 4541 uint32_t amm; 4542 uint32_t out, avail; 4543 uint32_t maxseg; 4544 int collapsed_win = 0; 4545 4546 if (bbr->rc_all_timers_stopped) { 4547 return (1); 4548 } 4549 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4550 /* Its not time yet */ 4551 return (0); 4552 } 4553 if (ctf_progress_timeout_check(tp, true)) { 4554 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4555 return (-ETIMEDOUT); /* tcp_drop() */ 4556 } 4557 /* Did we somehow get into persists? */ 4558 if (bbr->rc_in_persist) { 4559 return (0); 4560 } 4561 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4562 bbr_set_state(tp, bbr, 0); 4563 BBR_STAT_INC(bbr_tlp_tot); 4564 maxseg = tp->t_maxseg - bbr->rc_last_options; 4565 /* 4566 * A TLP timer has expired. We have been idle for 2 rtts. So we now 4567 * need to figure out how to force a full MSS segment out. 4568 */ 4569 so = tptosocket(tp); 4570 avail = sbavail(&so->so_snd); 4571 out = ctf_outstanding(tp); 4572 if (out > tp->snd_wnd) { 4573 /* special case, we need a retransmission */ 4574 collapsed_win = 1; 4575 goto need_retran; 4576 } 4577 if (avail > out) { 4578 /* New data is available */ 4579 amm = avail - out; 4580 if (amm > maxseg) { 4581 amm = maxseg; 4582 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 4583 /* not enough to fill a MTU and no-delay is off */ 4584 goto need_retran; 4585 } 4586 /* Set the send-new override */ 4587 if ((out + amm) <= tp->snd_wnd) { 4588 bbr->rc_tlp_new_data = 1; 4589 } else { 4590 goto need_retran; 4591 } 4592 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4593 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max; 4594 bbr->r_ctl.rc_tlp_send = NULL; 4595 /* cap any slots */ 4596 BBR_STAT_INC(bbr_tlp_newdata); 4597 goto send; 4598 } 4599 need_retran: 4600 /* 4601 * Ok we need to arrange the last un-acked segment to be re-sent, or 4602 * optionally the first un-acked segment. 4603 */ 4604 if (collapsed_win == 0) { 4605 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 4606 if (rsm && (rsm->r_flags & (BBR_ACKED | BBR_HAS_FIN))) { 4607 rsm = bbr_find_high_nonack(bbr, rsm); 4608 } 4609 if (rsm == NULL) { 4610 goto restore; 4611 } 4612 } else { 4613 /* 4614 * We must find the last segment 4615 * that was acceptable by the client. 4616 */ 4617 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4618 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) { 4619 /* Found one */ 4620 break; 4621 } 4622 } 4623 if (rsm == NULL) { 4624 /* None? if so send the first */ 4625 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4626 if (rsm == NULL) 4627 goto restore; 4628 } 4629 } 4630 if ((rsm->r_end - rsm->r_start) > maxseg) { 4631 /* 4632 * We need to split this the last segment in two. 4633 */ 4634 struct bbr_sendmap *nrsm; 4635 4636 nrsm = bbr_alloc_full_limit(bbr); 4637 if (nrsm == NULL) { 4638 /* 4639 * We can't get memory to split, we can either just 4640 * not split it. Or retransmit the whole piece, lets 4641 * do the large send (BTLP :-) ). 4642 */ 4643 goto go_for_it; 4644 } 4645 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg)); 4646 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 4647 if (rsm->r_in_tmap) { 4648 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4649 nrsm->r_in_tmap = 1; 4650 } 4651 rsm->r_flags &= (~BBR_HAS_FIN); 4652 rsm = nrsm; 4653 } 4654 go_for_it: 4655 bbr->r_ctl.rc_tlp_send = rsm; 4656 bbr->rc_tlp_rtx_out = 1; 4657 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) { 4658 bbr->r_ctl.rc_tlp_seg_send_cnt++; 4659 tp->t_rxtshift++; 4660 } else { 4661 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 4662 bbr->r_ctl.rc_tlp_seg_send_cnt = 1; 4663 } 4664 send: 4665 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 4666 /* 4667 * Can't [re]/transmit a segment we have retransmitted the 4668 * max times. We need the retransmit timer to take over. 4669 */ 4670 restore: 4671 bbr->rc_tlp_new_data = 0; 4672 bbr->r_ctl.rc_tlp_send = NULL; 4673 if (rsm) 4674 rsm->r_flags &= ~BBR_TLP; 4675 BBR_STAT_INC(bbr_tlp_retran_fail); 4676 return (0); 4677 } else if (rsm) { 4678 rsm->r_flags |= BBR_TLP; 4679 } 4680 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) && 4681 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) { 4682 /* 4683 * We have retransmitted to many times for TLP. Switch to 4684 * the regular RTO timer 4685 */ 4686 goto restore; 4687 } 4688 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP); 4689 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 4690 return (0); 4691 } 4692 4693 /* 4694 * Delayed ack Timer, here we simply need to setup the 4695 * ACK_NOW flag and remove the DELACK flag. From there 4696 * the output routine will send the ack out. 4697 * 4698 * We only return 1, saying don't proceed, if all timers 4699 * are stopped (destroyed PCB?). 4700 */ 4701 static int 4702 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4703 { 4704 if (bbr->rc_all_timers_stopped) { 4705 return (1); 4706 } 4707 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK); 4708 tp->t_flags &= ~TF_DELACK; 4709 tp->t_flags |= TF_ACKNOW; 4710 KMOD_TCPSTAT_INC(tcps_delack); 4711 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 4712 return (0); 4713 } 4714 4715 /* 4716 * Here we send a KEEP-ALIVE like probe to the 4717 * peer, we do not send data. 4718 * 4719 * We only return 1, saying don't proceed, if all timers 4720 * are stopped (destroyed PCB?). 4721 */ 4722 static int 4723 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4724 { 4725 struct tcptemp *t_template; 4726 int32_t retval = 1; 4727 4728 if (bbr->rc_all_timers_stopped) { 4729 return (1); 4730 } 4731 if (bbr->rc_in_persist == 0) 4732 return (0); 4733 4734 /* 4735 * Persistence timer into zero window. Force a byte to be output, if 4736 * possible. 4737 */ 4738 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST); 4739 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 4740 KMOD_TCPSTAT_INC(tcps_persisttimeo); 4741 /* 4742 * Have we exceeded the user specified progress time? 4743 */ 4744 if (ctf_progress_timeout_check(tp, true)) { 4745 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4746 return (-ETIMEDOUT); /* tcp_drop() */ 4747 } 4748 /* 4749 * Hack: if the peer is dead/unreachable, we do not time out if the 4750 * window is closed. After a full backoff, drop the connection if 4751 * the idle time (no responses to probes) reaches the maximum 4752 * backoff that we would use if retransmitting. 4753 */ 4754 if (tp->t_rxtshift >= V_tcp_retries && 4755 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 4756 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 4757 KMOD_TCPSTAT_INC(tcps_persistdrop); 4758 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4759 return (-ETIMEDOUT); /* tcp_drop() */ 4760 } 4761 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) && 4762 tp->snd_una == tp->snd_max) { 4763 bbr_exit_persist(tp, bbr, cts, __LINE__); 4764 retval = 0; 4765 goto out; 4766 } 4767 /* 4768 * If the user has closed the socket then drop a persisting 4769 * connection after a much reduced timeout. 4770 */ 4771 if (tp->t_state > TCPS_CLOSE_WAIT && 4772 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 4773 KMOD_TCPSTAT_INC(tcps_persistdrop); 4774 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4775 return (-ETIMEDOUT); /* tcp_drop() */ 4776 } 4777 t_template = tcpip_maketemplate(bbr->rc_inp); 4778 if (t_template) { 4779 tcp_respond(tp, t_template->tt_ipgen, 4780 &t_template->tt_t, (struct mbuf *)NULL, 4781 tp->rcv_nxt, tp->snd_una - 1, 0); 4782 /* This sends an ack */ 4783 if (tp->t_flags & TF_DELACK) 4784 tp->t_flags &= ~TF_DELACK; 4785 free(t_template, M_TEMP); 4786 } 4787 if (tp->t_rxtshift < V_tcp_retries) 4788 tp->t_rxtshift++; 4789 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0); 4790 out: 4791 return (retval); 4792 } 4793 4794 /* 4795 * If a keepalive goes off, we had no other timers 4796 * happening. We always return 1 here since this 4797 * routine either drops the connection or sends 4798 * out a segment with respond. 4799 */ 4800 static int 4801 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4802 { 4803 struct tcptemp *t_template; 4804 struct inpcb *inp = tptoinpcb(tp); 4805 4806 if (bbr->rc_all_timers_stopped) { 4807 return (1); 4808 } 4809 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 4810 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP); 4811 /* 4812 * Keep-alive timer went off; send something or drop connection if 4813 * idle for too long. 4814 */ 4815 KMOD_TCPSTAT_INC(tcps_keeptimeo); 4816 if (tp->t_state < TCPS_ESTABLISHED) 4817 goto dropit; 4818 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 4819 tp->t_state <= TCPS_CLOSING) { 4820 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 4821 goto dropit; 4822 /* 4823 * Send a packet designed to force a response if the peer is 4824 * up and reachable: either an ACK if the connection is 4825 * still alive, or an RST if the peer has closed the 4826 * connection due to timeout or reboot. Using sequence 4827 * number tp->snd_una-1 causes the transmitted zero-length 4828 * segment to lie outside the receive window; by the 4829 * protocol spec, this requires the correspondent TCP to 4830 * respond. 4831 */ 4832 KMOD_TCPSTAT_INC(tcps_keepprobe); 4833 t_template = tcpip_maketemplate(inp); 4834 if (t_template) { 4835 tcp_respond(tp, t_template->tt_ipgen, 4836 &t_template->tt_t, (struct mbuf *)NULL, 4837 tp->rcv_nxt, tp->snd_una - 1, 0); 4838 free(t_template, M_TEMP); 4839 } 4840 } 4841 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0); 4842 return (1); 4843 dropit: 4844 KMOD_TCPSTAT_INC(tcps_keepdrops); 4845 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 4846 return (-ETIMEDOUT); /* tcp_drop() */ 4847 } 4848 4849 /* 4850 * Retransmit helper function, clear up all the ack 4851 * flags and take care of important book keeping. 4852 */ 4853 static void 4854 bbr_remxt_tmr(struct tcpcb *tp) 4855 { 4856 /* 4857 * The retransmit timer went off, all sack'd blocks must be 4858 * un-acked. 4859 */ 4860 struct bbr_sendmap *rsm, *trsm = NULL; 4861 struct tcp_bbr *bbr; 4862 uint32_t cts, lost; 4863 4864 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 4865 cts = tcp_get_usecs(&bbr->rc_tv); 4866 lost = bbr->r_ctl.rc_lost; 4867 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4868 bbr_set_state(tp, bbr, 0); 4869 4870 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 4871 if (rsm->r_flags & BBR_ACKED) { 4872 uint32_t old_flags; 4873 4874 rsm->r_dupack = 0; 4875 if (rsm->r_in_tmap == 0) { 4876 /* We must re-add it back to the tlist */ 4877 if (trsm == NULL) { 4878 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 4879 } else { 4880 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext); 4881 } 4882 rsm->r_in_tmap = 1; 4883 } 4884 old_flags = rsm->r_flags; 4885 rsm->r_flags |= BBR_RXT_CLEARED; 4886 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS); 4887 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 4888 } else { 4889 if ((tp->t_state < TCPS_ESTABLISHED) && 4890 (rsm->r_start == tp->snd_una)) { 4891 /* 4892 * Special case for TCP FO. Where 4893 * we sent more data beyond the snd_max. 4894 * We don't mark that as lost and stop here. 4895 */ 4896 break; 4897 } 4898 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4899 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4900 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4901 } 4902 if (bbr_marks_rxt_sack_passed) { 4903 /* 4904 * With this option, we will rack out 4905 * in 1ms increments the rest of the packets. 4906 */ 4907 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST; 4908 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4909 } else { 4910 /* 4911 * With this option we only mark them lost 4912 * and remove all sack'd markings. We will run 4913 * another RXT or a TLP. This will cause 4914 * us to eventually send more based on what 4915 * ack's come in. 4916 */ 4917 rsm->r_flags |= BBR_MARKED_LOST; 4918 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4919 rsm->r_flags &= ~BBR_SACK_PASSED; 4920 } 4921 } 4922 trsm = rsm; 4923 } 4924 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4925 /* Clear the count (we just un-acked them) */ 4926 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR); 4927 bbr->rc_tlp_new_data = 0; 4928 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4929 /* zap the behindness on a rxt */ 4930 bbr->r_ctl.rc_hptsi_agg_delay = 0; 4931 bbr->r_agg_early_set = 0; 4932 bbr->r_ctl.rc_agg_early = 0; 4933 bbr->rc_tlp_rtx_out = 0; 4934 bbr->r_ctl.rc_sacked = 0; 4935 bbr->r_ctl.rc_sacklast = NULL; 4936 bbr->r_timer_override = 1; 4937 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4938 } 4939 4940 /* 4941 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 4942 * we will setup to retransmit the lowest seq number outstanding. 4943 */ 4944 static int 4945 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4946 { 4947 struct inpcb *inp = tptoinpcb(tp); 4948 int32_t rexmt; 4949 int32_t retval = 0; 4950 bool isipv6; 4951 4952 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 4953 if (bbr->rc_all_timers_stopped) { 4954 return (1); 4955 } 4956 if (TCPS_HAVEESTABLISHED(tp->t_state) && 4957 (tp->snd_una == tp->snd_max)) { 4958 /* Nothing outstanding .. nothing to do */ 4959 return (0); 4960 } 4961 /* 4962 * Retransmission timer went off. Message has not been acked within 4963 * retransmit interval. Back off to a longer retransmit interval 4964 * and retransmit one segment. 4965 */ 4966 if (ctf_progress_timeout_check(tp, true)) { 4967 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4968 return (-ETIMEDOUT); /* tcp_drop() */ 4969 } 4970 bbr_remxt_tmr(tp); 4971 if ((bbr->r_ctl.rc_resend == NULL) || 4972 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) { 4973 /* 4974 * If the rwnd collapsed on 4975 * the one we are retransmitting 4976 * it does not count against the 4977 * rxt count. 4978 */ 4979 tp->t_rxtshift++; 4980 } 4981 if (tp->t_rxtshift > V_tcp_retries) { 4982 tp->t_rxtshift = V_tcp_retries; 4983 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 4984 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 4985 /* XXXGL: previously t_softerror was casted to uint16_t */ 4986 MPASS(tp->t_softerror >= 0); 4987 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 4988 return (retval); /* tcp_drop() */ 4989 } 4990 if (tp->t_state == TCPS_SYN_SENT) { 4991 /* 4992 * If the SYN was retransmitted, indicate CWND to be limited 4993 * to 1 segment in cc_conn_init(). 4994 */ 4995 tp->snd_cwnd = 1; 4996 } else if (tp->t_rxtshift == 1) { 4997 /* 4998 * first retransmit; record ssthresh and cwnd so they can be 4999 * recovered if this turns out to be a "bad" retransmit. A 5000 * retransmit is considered "bad" if an ACK for this segment 5001 * is received within RTT/2 interval; the assumption here is 5002 * that the ACK was already in flight. See "On Estimating 5003 * End-to-End Network Path Properties" by Allman and Paxson 5004 * for more details. 5005 */ 5006 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5007 if (!IN_RECOVERY(tp->t_flags)) { 5008 tp->snd_cwnd_prev = tp->snd_cwnd; 5009 tp->snd_ssthresh_prev = tp->snd_ssthresh; 5010 tp->snd_recover_prev = tp->snd_recover; 5011 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 5012 tp->t_flags |= TF_PREVVALID; 5013 } else { 5014 tp->t_flags &= ~TF_PREVVALID; 5015 } 5016 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5017 } else { 5018 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5019 tp->t_flags &= ~TF_PREVVALID; 5020 } 5021 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 5022 if ((tp->t_state == TCPS_SYN_SENT) || 5023 (tp->t_state == TCPS_SYN_RECEIVED)) 5024 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift]; 5025 else 5026 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 5027 TCPT_RANGESET(tp->t_rxtcur, rexmt, 5028 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), 5029 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 5030 /* 5031 * We enter the path for PLMTUD if connection is established or, if 5032 * connection is FIN_WAIT_1 status, reason for the last is that if 5033 * amount of data we send is very small, we could send it in couple 5034 * of packets and process straight to FIN. In that case we won't 5035 * catch ESTABLISHED state. 5036 */ 5037 #ifdef INET6 5038 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 5039 #else 5040 isipv6 = false; 5041 #endif 5042 if (((V_tcp_pmtud_blackhole_detect == 1) || 5043 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 5044 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 5045 ((tp->t_state == TCPS_ESTABLISHED) || 5046 (tp->t_state == TCPS_FIN_WAIT_1))) { 5047 /* 5048 * Idea here is that at each stage of mtu probe (usually, 5049 * 1448 -> 1188 -> 524) should be given 2 chances to recover 5050 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 5051 * should take care of that. 5052 */ 5053 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 5054 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 5055 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 5056 tp->t_rxtshift % 2 == 0)) { 5057 /* 5058 * Enter Path MTU Black-hole Detection mechanism: - 5059 * Disable Path MTU Discovery (IP "DF" bit). - 5060 * Reduce MTU to lower value than what we negotiated 5061 * with peer. 5062 */ 5063 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 5064 /* 5065 * Record that we may have found a black 5066 * hole. 5067 */ 5068 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 5069 /* Keep track of previous MSS. */ 5070 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 5071 } 5072 /* 5073 * Reduce the MSS to blackhole value or to the 5074 * default in an attempt to retransmit. 5075 */ 5076 #ifdef INET6 5077 isipv6 = bbr->r_is_v6; 5078 if (isipv6 && 5079 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 5080 /* Use the sysctl tuneable blackhole MSS. */ 5081 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 5082 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5083 } else if (isipv6) { 5084 /* Use the default MSS. */ 5085 tp->t_maxseg = V_tcp_v6mssdflt; 5086 /* 5087 * Disable Path MTU Discovery when we switch 5088 * to minmss. 5089 */ 5090 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5091 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5092 } 5093 #endif 5094 #if defined(INET6) && defined(INET) 5095 else 5096 #endif 5097 #ifdef INET 5098 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 5099 /* Use the sysctl tuneable blackhole MSS. */ 5100 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 5101 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5102 } else { 5103 /* Use the default MSS. */ 5104 tp->t_maxseg = V_tcp_mssdflt; 5105 /* 5106 * Disable Path MTU Discovery when we switch 5107 * to minmss. 5108 */ 5109 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5110 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5111 } 5112 #endif 5113 } else { 5114 /* 5115 * If further retransmissions are still unsuccessful 5116 * with a lowered MTU, maybe this isn't a blackhole 5117 * and we restore the previous MSS and blackhole 5118 * detection flags. The limit '6' is determined by 5119 * giving each probe stage (1448, 1188, 524) 2 5120 * chances to recover. 5121 */ 5122 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 5123 (tp->t_rxtshift >= 6)) { 5124 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 5125 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 5126 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 5127 if (tp->t_maxseg < V_tcp_mssdflt) { 5128 /* 5129 * The MSS is so small we should not 5130 * process incoming SACK's since we are 5131 * subject to attack in such a case. 5132 */ 5133 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 5134 } else { 5135 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 5136 } 5137 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 5138 } 5139 } 5140 } 5141 /* 5142 * Disable RFC1323 and SACK if we haven't got any response to our 5143 * third SYN to work-around some broken terminal servers (most of 5144 * which have hopefully been retired) that have bad VJ header 5145 * compression code which trashes TCP segments containing 5146 * unknown-to-them TCP options. 5147 */ 5148 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 5149 (tp->t_rxtshift == 3)) 5150 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 5151 /* 5152 * If we backed off this far, our srtt estimate is probably bogus. 5153 * Clobber it so we'll take the next rtt measurement as our srtt; 5154 * move the current srtt into rttvar to keep the current retransmit 5155 * times until then. 5156 */ 5157 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 5158 #ifdef INET6 5159 if (bbr->r_is_v6) 5160 in6_losing(inp); 5161 else 5162 #endif 5163 in_losing(inp); 5164 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 5165 tp->t_srtt = 0; 5166 } 5167 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 5168 tp->snd_recover = tp->snd_max; 5169 tp->t_flags |= TF_ACKNOW; 5170 tp->t_rtttime = 0; 5171 5172 return (retval); 5173 } 5174 5175 static int 5176 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling) 5177 { 5178 int32_t ret = 0; 5179 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 5180 5181 if (timers == 0) { 5182 return (0); 5183 } 5184 if (tp->t_state == TCPS_LISTEN) { 5185 /* no timers on listen sockets */ 5186 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 5187 return (0); 5188 return (1); 5189 } 5190 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 5191 uint32_t left; 5192 5193 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 5194 ret = -1; 5195 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5196 return (0); 5197 } 5198 if (hpts_calling == 0) { 5199 ret = -2; 5200 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5201 return (0); 5202 } 5203 /* 5204 * Ok our timer went off early and we are not paced false 5205 * alarm, go back to sleep. 5206 */ 5207 left = bbr->r_ctl.rc_timer_exp - cts; 5208 ret = -3; 5209 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling); 5210 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left)); 5211 return (1); 5212 } 5213 bbr->rc_tmr_stopped = 0; 5214 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 5215 if (timers & PACE_TMR_DELACK) { 5216 ret = bbr_timeout_delack(tp, bbr, cts); 5217 } else if (timers & PACE_TMR_PERSIT) { 5218 ret = bbr_timeout_persist(tp, bbr, cts); 5219 } else if (timers & PACE_TMR_RACK) { 5220 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5221 ret = bbr_timeout_rack(tp, bbr, cts); 5222 } else if (timers & PACE_TMR_TLP) { 5223 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5224 ret = bbr_timeout_tlp(tp, bbr, cts); 5225 } else if (timers & PACE_TMR_RXT) { 5226 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5227 ret = bbr_timeout_rxt(tp, bbr, cts); 5228 } else if (timers & PACE_TMR_KEEP) { 5229 ret = bbr_timeout_keepalive(tp, bbr, cts); 5230 } 5231 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling); 5232 return (ret); 5233 } 5234 5235 static void 5236 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts) 5237 { 5238 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 5239 uint8_t hpts_removed = 0; 5240 5241 if (tcp_in_hpts(bbr->rc_tp) && 5242 (bbr->rc_timer_first == 1)) { 5243 /* 5244 * If we are canceling timer's when we have the 5245 * timer ahead of the output being paced. We also 5246 * must remove ourselves from the hpts. 5247 */ 5248 hpts_removed = 1; 5249 tcp_hpts_remove(bbr->rc_tp); 5250 if (bbr->r_ctl.rc_last_delay_val) { 5251 /* Update the last hptsi delay too */ 5252 uint32_t time_since_send; 5253 5254 if (TSTMP_GT(cts, bbr->rc_pacer_started)) 5255 time_since_send = cts - bbr->rc_pacer_started; 5256 else 5257 time_since_send = 0; 5258 if (bbr->r_ctl.rc_last_delay_val > time_since_send) { 5259 /* Cut down our slot time */ 5260 bbr->r_ctl.rc_last_delay_val -= time_since_send; 5261 } else { 5262 bbr->r_ctl.rc_last_delay_val = 0; 5263 } 5264 bbr->rc_pacer_started = cts; 5265 } 5266 } 5267 bbr->rc_timer_first = 0; 5268 bbr_log_to_cancel(bbr, line, cts, hpts_removed); 5269 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 5270 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 5271 } 5272 } 5273 5274 static int 5275 bbr_stopall(struct tcpcb *tp) 5276 { 5277 struct tcp_bbr *bbr; 5278 5279 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 5280 bbr->rc_all_timers_stopped = 1; 5281 5282 tcp_hpts_remove(tp); 5283 5284 return (0); 5285 } 5286 5287 static uint32_t 5288 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts) 5289 { 5290 struct bbr_sendmap *rsm; 5291 5292 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 5293 if ((rsm == NULL) || (u_rsm == rsm)) 5294 return (cts); 5295 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 5296 } 5297 5298 static void 5299 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr, 5300 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time) 5301 { 5302 int32_t idx; 5303 5304 rsm->r_rtr_cnt++; 5305 rsm->r_dupack = 0; 5306 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) { 5307 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS; 5308 rsm->r_flags |= BBR_OVERMAX; 5309 } 5310 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 5311 /* Take off the collapsed flag at rxt */ 5312 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 5313 } 5314 if (rsm->r_flags & BBR_MARKED_LOST) { 5315 /* We have retransmitted, its no longer lost */ 5316 rsm->r_flags &= ~BBR_MARKED_LOST; 5317 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 5318 } 5319 if (rsm->r_flags & BBR_RXT_CLEARED) { 5320 /* 5321 * We hit a RXT timer on it and 5322 * we cleared the "acked" flag. 5323 * We now have it going back into 5324 * flight, we can remove the cleared 5325 * flag and possibly do accounting on 5326 * this piece. 5327 */ 5328 rsm->r_flags &= ~BBR_RXT_CLEARED; 5329 } 5330 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) { 5331 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 5332 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 5333 } 5334 idx = rsm->r_rtr_cnt - 1; 5335 rsm->r_tim_lastsent[idx] = cts; 5336 rsm->r_pacing_delay = pacing_time; 5337 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5338 rsm->r_ts_valid = bbr->rc_ts_valid; 5339 if (bbr->rc_ts_valid) 5340 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5341 if (bbr->r_ctl.r_app_limited_until) 5342 rsm->r_app_limited = 1; 5343 else 5344 rsm->r_app_limited = 0; 5345 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5346 rsm->r_bbr_state = bbr_state_val(bbr); 5347 else 5348 rsm->r_bbr_state = 8; 5349 if (rsm->r_flags & BBR_ACKED) { 5350 /* Problably MTU discovery messing with us */ 5351 uint32_t old_flags; 5352 5353 old_flags = rsm->r_flags; 5354 rsm->r_flags &= ~BBR_ACKED; 5355 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 5356 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 5357 if (bbr->r_ctl.rc_sacked == 0) 5358 bbr->r_ctl.rc_sacklast = NULL; 5359 } 5360 if (rsm->r_in_tmap) { 5361 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5362 } 5363 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5364 rsm->r_in_tmap = 1; 5365 if (rsm->r_flags & BBR_SACK_PASSED) { 5366 /* We have retransmitted due to the SACK pass */ 5367 rsm->r_flags &= ~BBR_SACK_PASSED; 5368 rsm->r_flags |= BBR_WAS_SACKPASS; 5369 } 5370 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5371 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5372 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5373 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 5374 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5375 rsm->r_is_gain = 1; 5376 rsm->r_is_drain = 0; 5377 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5378 rsm->r_is_drain = 1; 5379 rsm->r_is_gain = 0; 5380 } else { 5381 rsm->r_is_drain = 0; 5382 rsm->r_is_gain = 0; 5383 } 5384 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */ 5385 } 5386 5387 /* 5388 * Returns 0, or the sequence where we stopped 5389 * updating. We also update the lenp to be the amount 5390 * of data left. 5391 */ 5392 5393 static uint32_t 5394 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr, 5395 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time) 5396 { 5397 /* 5398 * We (re-)transmitted starting at rsm->r_start for some length 5399 * (possibly less than r_end. 5400 */ 5401 struct bbr_sendmap *nrsm; 5402 uint32_t c_end; 5403 int32_t len; 5404 5405 len = *lenp; 5406 c_end = rsm->r_start + len; 5407 if (SEQ_GEQ(c_end, rsm->r_end)) { 5408 /* 5409 * We retransmitted the whole piece or more than the whole 5410 * slopping into the next rsm. 5411 */ 5412 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5413 if (c_end == rsm->r_end) { 5414 *lenp = 0; 5415 return (0); 5416 } else { 5417 int32_t act_len; 5418 5419 /* Hangs over the end return whats left */ 5420 act_len = rsm->r_end - rsm->r_start; 5421 *lenp = (len - act_len); 5422 return (rsm->r_end); 5423 } 5424 /* We don't get out of this block. */ 5425 } 5426 /* 5427 * Here we retransmitted less than the whole thing which means we 5428 * have to split this into what was transmitted and what was not. 5429 */ 5430 nrsm = bbr_alloc_full_limit(bbr); 5431 if (nrsm == NULL) { 5432 *lenp = 0; 5433 return (0); 5434 } 5435 /* 5436 * So here we are going to take the original rsm and make it what we 5437 * retransmitted. nrsm will be the tail portion we did not 5438 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 5439 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 5440 * 1, 6 and the new piece will be 6, 11. 5441 */ 5442 bbr_clone_rsm(bbr, nrsm, rsm, c_end); 5443 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 5444 nrsm->r_dupack = 0; 5445 if (rsm->r_in_tmap) { 5446 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 5447 nrsm->r_in_tmap = 1; 5448 } 5449 rsm->r_flags &= (~BBR_HAS_FIN); 5450 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5451 *lenp = 0; 5452 return (0); 5453 } 5454 5455 static uint64_t 5456 bbr_get_hardware_rate(struct tcp_bbr *bbr) 5457 { 5458 uint64_t bw; 5459 5460 bw = bbr_get_bw(bbr); 5461 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]; 5462 bw /= (uint64_t)BBR_UNIT; 5463 return(bw); 5464 } 5465 5466 static void 5467 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts, 5468 uint64_t act_rate, uint64_t rate_wanted) 5469 { 5470 /* 5471 * We could not get a full gains worth 5472 * of rate. 5473 */ 5474 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) { 5475 /* we can't even get the real rate */ 5476 uint64_t red; 5477 5478 bbr->skip_gain = 1; 5479 bbr->gain_is_limited = 0; 5480 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate; 5481 if (red) 5482 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts); 5483 } else { 5484 /* We can use a lower gain */ 5485 bbr->skip_gain = 0; 5486 bbr->gain_is_limited = 1; 5487 } 5488 } 5489 5490 static void 5491 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts) 5492 { 5493 const struct tcp_hwrate_limit_table *nrte; 5494 int error, rate = -1; 5495 5496 if (bbr->r_ctl.crte == NULL) 5497 return; 5498 if ((bbr->rc_inp->inp_route.ro_nh == NULL) || 5499 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) { 5500 /* Lost our routes? */ 5501 /* Clear the way for a re-attempt */ 5502 bbr->bbr_attempt_hdwr_pace = 0; 5503 lost_rate: 5504 bbr->gain_is_limited = 0; 5505 bbr->skip_gain = 0; 5506 bbr->bbr_hdrw_pacing = 0; 5507 counter_u64_add(bbr_flows_whdwr_pacing, -1); 5508 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 5509 tcp_bbr_tso_size_check(bbr, cts); 5510 return; 5511 } 5512 rate = bbr_get_hardware_rate(bbr); 5513 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte, 5514 bbr->rc_tp, 5515 bbr->rc_inp->inp_route.ro_nh->nh_ifp, 5516 rate, 5517 (RS_PACING_GEQ|RS_PACING_SUB_OK), 5518 &error, NULL); 5519 if (nrte == NULL) { 5520 goto lost_rate; 5521 } 5522 if (nrte != bbr->r_ctl.crte) { 5523 bbr->r_ctl.crte = nrte; 5524 if (error == 0) { 5525 BBR_STAT_INC(bbr_hdwr_rl_mod_ok); 5526 if (bbr->r_ctl.crte->rate < rate) { 5527 /* We have a problem */ 5528 bbr_setup_less_of_rate(bbr, cts, 5529 bbr->r_ctl.crte->rate, rate); 5530 } else { 5531 /* We are good */ 5532 bbr->gain_is_limited = 0; 5533 bbr->skip_gain = 0; 5534 } 5535 } else { 5536 /* A failure should release the tag */ 5537 BBR_STAT_INC(bbr_hdwr_rl_mod_fail); 5538 bbr->gain_is_limited = 0; 5539 bbr->skip_gain = 0; 5540 bbr->bbr_hdrw_pacing = 0; 5541 } 5542 bbr_type_log_hdwr_pacing(bbr, 5543 bbr->r_ctl.crte->ptbl->rs_ifp, 5544 rate, 5545 bbr->r_ctl.crte->rate, 5546 __LINE__, 5547 cts, 5548 error); 5549 } 5550 } 5551 5552 static void 5553 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts) 5554 { 5555 /* 5556 * If we have hardware pacing support 5557 * we need to factor that in for our 5558 * TSO size. 5559 */ 5560 const struct tcp_hwrate_limit_table *rlp; 5561 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay; 5562 5563 if ((bbr->bbr_hdrw_pacing == 0) || 5564 (IN_RECOVERY(bbr->rc_tp->t_flags)) || 5565 (bbr->r_ctl.crte == NULL)) 5566 return; 5567 if (bbr->hw_pacing_set == 0) { 5568 /* Not yet by the hdwr pacing count delay */ 5569 return; 5570 } 5571 if (bbr_hdwr_pace_adjust == 0) { 5572 /* No adjustment */ 5573 return; 5574 } 5575 rlp = bbr->r_ctl.crte; 5576 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) 5577 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5578 else 5579 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5580 /* 5581 * So lets first get the 5582 * time we will take between 5583 * TSO sized sends currently without 5584 * hardware help. 5585 */ 5586 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT, 5587 bbr->r_ctl.rc_pace_max_segs, cts, 1); 5588 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg; 5589 hdwr_delay *= rlp->time_between; 5590 if (cur_delay > hdwr_delay) 5591 delta = cur_delay - hdwr_delay; 5592 else 5593 delta = 0; 5594 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay, 5595 (bbr->r_ctl.rc_pace_max_segs / maxseg), 5596 1); 5597 if (delta && 5598 (delta < (max(rlp->time_between, 5599 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) { 5600 /* 5601 * Now lets divide by the pacing 5602 * time between each segment the 5603 * hardware sends rounding up and 5604 * derive a bytes from that. We multiply 5605 * that by bbr_hdwr_pace_adjust to get 5606 * more bang for our buck. 5607 * 5608 * The goal is to have the software pacer 5609 * waiting no more than an additional 5610 * pacing delay if we can (without the 5611 * compensation i.e. x bbr_hdwr_pace_adjust). 5612 */ 5613 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between), 5614 (bbr->r_ctl.rc_pace_max_segs/maxseg)); 5615 seg_sz *= bbr_hdwr_pace_adjust; 5616 if (bbr_hdwr_pace_floor && 5617 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5618 /* Currently hardware paces 5619 * out rs_min_seg segments at a time. 5620 * We need to make sure we always send at least 5621 * a full burst of bbr_hdwr_pace_floor down. 5622 */ 5623 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5624 } 5625 seg_sz *= maxseg; 5626 } else if (delta == 0) { 5627 /* 5628 * The highest pacing rate is 5629 * above our b/w gained. This means 5630 * we probably are going quite fast at 5631 * the hardware highest rate. Lets just multiply 5632 * the calculated TSO size by the 5633 * multiplier factor (its probably 5634 * 4 segments in the default config for 5635 * mlx). 5636 */ 5637 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust; 5638 if (bbr_hdwr_pace_floor && 5639 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5640 /* Currently hardware paces 5641 * out rs_min_seg segments at a time. 5642 * We need to make sure we always send at least 5643 * a full burst of bbr_hdwr_pace_floor down. 5644 */ 5645 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5646 } 5647 } else { 5648 /* 5649 * The pacing time difference is so 5650 * big that the hardware will 5651 * pace out more rapidly then we 5652 * really want and then we 5653 * will have a long delay. Lets just keep 5654 * the same TSO size so its as if 5655 * we were not using hdwr pacing (we 5656 * just gain a bit of spacing from the 5657 * hardware if seg_sz > 1). 5658 */ 5659 seg_sz = bbr->r_ctl.rc_pace_max_segs; 5660 } 5661 if (seg_sz > bbr->r_ctl.rc_pace_max_segs) 5662 new_tso = seg_sz; 5663 else 5664 new_tso = bbr->r_ctl.rc_pace_max_segs; 5665 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg)) 5666 new_tso = PACE_MAX_IP_BYTES - maxseg; 5667 5668 if (new_tso != bbr->r_ctl.rc_pace_max_segs) { 5669 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0); 5670 bbr->r_ctl.rc_pace_max_segs = new_tso; 5671 } 5672 } 5673 5674 static void 5675 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts) 5676 { 5677 uint64_t bw; 5678 uint32_t old_tso = 0, new_tso; 5679 uint32_t maxseg, bytes; 5680 uint32_t tls_seg=0; 5681 /* 5682 * Google/linux uses the following algorithm to determine 5683 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18): 5684 * 5685 * bytes = bw_in_bytes_per_second / 1000 5686 * bytes = min(bytes, 64k) 5687 * tso_segs = bytes / MSS 5688 * if (bw < 1.2Mbs) 5689 * min_tso_segs = 1 5690 * else 5691 * min_tso_segs = 2 5692 * tso_segs = max(tso_segs, min_tso_segs) 5693 * 5694 * * Note apply a device specific limit (we apply this in the 5695 * tcp_m_copym). 5696 * Note that before the initial measurement is made google bursts out 5697 * a full iwnd just like new-reno/cubic. 5698 * 5699 * We do not use this algorithm. Instead we 5700 * use a two phased approach: 5701 * 5702 * if ( bw <= per-tcb-cross-over) 5703 * goal_tso = calculate how much with this bw we 5704 * can send in goal-time seconds. 5705 * if (goal_tso > mss) 5706 * seg = goal_tso / mss 5707 * tso = seg * mss 5708 * else 5709 * tso = mss 5710 * if (tso > per-tcb-max) 5711 * tso = per-tcb-max 5712 * else if ( bw > 512Mbps) 5713 * tso = max-tso (64k/mss) 5714 * else 5715 * goal_tso = bw / per-tcb-divsor 5716 * seg = (goal_tso + mss-1)/mss 5717 * tso = seg * mss 5718 * 5719 * if (tso < per-tcb-floor) 5720 * tso = per-tcb-floor 5721 * if (tso > per-tcb-utter_max) 5722 * tso = per-tcb-utter_max 5723 * 5724 * Note the default per-tcb-divisor is 1000 (same as google). 5725 * the goal cross over is 30Mbps however. To recreate googles 5726 * algorithm you need to set: 5727 * 5728 * cross-over = 23,168,000 bps 5729 * goal-time = 18000 5730 * per-tcb-max = 2 5731 * per-tcb-divisor = 1000 5732 * per-tcb-floor = 1 5733 * 5734 * This will get you "google bbr" behavior with respect to tso size. 5735 * 5736 * Note we do set anything TSO size until we are past the initial 5737 * window. Before that we gnerally use either a single MSS 5738 * or we use the full IW size (so we burst a IW at a time) 5739 */ 5740 5741 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) { 5742 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5743 } else { 5744 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5745 } 5746 old_tso = bbr->r_ctl.rc_pace_max_segs; 5747 if (bbr->rc_past_init_win == 0) { 5748 /* 5749 * Not enough data has been acknowledged to make a 5750 * judgement. Set up the initial TSO based on if we 5751 * are sending a full IW at once or not. 5752 */ 5753 if (bbr->rc_use_google) 5754 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2); 5755 else if (bbr->bbr_init_win_cheat) 5756 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp); 5757 else 5758 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5759 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg) 5760 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg; 5761 if (bbr->r_ctl.rc_pace_max_segs == 0) { 5762 bbr->r_ctl.rc_pace_max_segs = maxseg; 5763 } 5764 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0); 5765 bbr_adjust_for_hw_pacing(bbr, cts); 5766 return; 5767 } 5768 /** 5769 * Now lets set the TSO goal based on our delivery rate in 5770 * bytes per second. Note we only do this if 5771 * we have acked at least the initial cwnd worth of data. 5772 */ 5773 bw = bbr_get_bw(bbr); 5774 if (IN_RECOVERY(bbr->rc_tp->t_flags) && 5775 (bbr->rc_use_google == 0)) { 5776 /* We clamp to one MSS in recovery */ 5777 new_tso = maxseg; 5778 } else if (bbr->rc_use_google) { 5779 int min_tso_segs; 5780 5781 /* Google considers the gain too */ 5782 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) { 5783 bw *= bbr->r_ctl.rc_bbr_hptsi_gain; 5784 bw /= BBR_UNIT; 5785 } 5786 bytes = bw / 1024; 5787 if (bytes > (64 * 1024)) 5788 bytes = 64 * 1024; 5789 new_tso = bytes / maxseg; 5790 if (bw < ONE_POINT_TWO_MEG) 5791 min_tso_segs = 1; 5792 else 5793 min_tso_segs = 2; 5794 if (new_tso < min_tso_segs) 5795 new_tso = min_tso_segs; 5796 new_tso *= maxseg; 5797 } else if (bbr->rc_no_pacing) { 5798 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg; 5799 } else if (bw <= bbr->r_ctl.bbr_cross_over) { 5800 /* 5801 * Calculate the worse case b/w TSO if we are inserting no 5802 * more than a delay_target number of TSO's. 5803 */ 5804 uint32_t tso_len, min_tso; 5805 5806 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw); 5807 if (tso_len > maxseg) { 5808 new_tso = tso_len / maxseg; 5809 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max) 5810 new_tso = bbr->r_ctl.bbr_hptsi_segments_max; 5811 new_tso *= maxseg; 5812 } else { 5813 /* 5814 * less than a full sized frame yikes.. long rtt or 5815 * low bw? 5816 */ 5817 min_tso = bbr_minseg(bbr); 5818 if ((tso_len > min_tso) && (bbr_all_get_min == 0)) 5819 new_tso = rounddown(tso_len, min_tso); 5820 else 5821 new_tso = min_tso; 5822 } 5823 } else if (bw > FIVETWELVE_MBPS) { 5824 /* 5825 * This guy is so fast b/w wise that we can TSO as large as 5826 * possible of segments that the NIC will allow. 5827 */ 5828 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5829 } else { 5830 /* 5831 * This formula is based on attempting to send a segment or 5832 * more every bbr_hptsi_per_second. The default is 1000 5833 * which means you are targeting what you can send every 1ms 5834 * based on the peers bw. 5835 * 5836 * If the number drops to say 500, then you are looking more 5837 * at 2ms and you will raise how much we send in a single 5838 * TSO thus saving CPU (less bbr_output_wtime() calls). The 5839 * trade off of course is you will send more at once and 5840 * thus tend to clump up the sends into larger "bursts" 5841 * building a queue. 5842 */ 5843 bw /= bbr->r_ctl.bbr_hptsi_per_second; 5844 new_tso = roundup(bw, (uint64_t)maxseg); 5845 /* 5846 * Gate the floor to match what our lower than 48Mbps 5847 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus 5848 * becomes the floor for this calculation. 5849 */ 5850 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg)) 5851 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg); 5852 } 5853 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor))) 5854 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor; 5855 if (new_tso > PACE_MAX_IP_BYTES) 5856 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5857 /* Enforce an utter maximum. */ 5858 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) { 5859 new_tso = bbr->r_ctl.bbr_utter_max * maxseg; 5860 } 5861 if (old_tso != new_tso) { 5862 /* Only log changes */ 5863 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0); 5864 bbr->r_ctl.rc_pace_max_segs = new_tso; 5865 } 5866 /* We have hardware pacing! */ 5867 bbr_adjust_for_hw_pacing(bbr, cts); 5868 } 5869 5870 static void 5871 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len, 5872 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts, 5873 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc, 5874 struct sockbuf *sb) 5875 { 5876 5877 struct bbr_sendmap *rsm, *nrsm; 5878 register uint32_t snd_max, snd_una; 5879 uint32_t pacing_time; 5880 /* 5881 * Add to the RACK log of packets in flight or retransmitted. If 5882 * there is a TS option we will use the TS echoed, if not we will 5883 * grab a TS. 5884 * 5885 * Retransmissions will increment the count and move the ts to its 5886 * proper place. Note that if options do not include TS's then we 5887 * won't be able to effectively use the ACK for an RTT on a retran. 5888 * 5889 * Notes about r_start and r_end. Lets consider a send starting at 5890 * sequence 1 for 10 bytes. In such an example the r_start would be 5891 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 5892 * This means that r_end is actually the first sequence for the next 5893 * slot (11). 5894 * 5895 */ 5896 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5897 if (err) { 5898 /* 5899 * We don't log errors -- we could but snd_max does not 5900 * advance in this case either. 5901 */ 5902 return; 5903 } 5904 if (th_flags & TH_RST) { 5905 /* 5906 * We don't log resets and we return immediately from 5907 * sending 5908 */ 5909 *abandon = 1; 5910 return; 5911 } 5912 snd_una = tp->snd_una; 5913 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) { 5914 /* 5915 * The call to bbr_log_output is made before bumping 5916 * snd_max. This means we can record one extra byte on a SYN 5917 * or FIN if seq_out is adding more on and a FIN is present 5918 * (and we are not resending). 5919 */ 5920 if ((th_flags & TH_SYN) && (tp->iss == seq_out)) 5921 len++; 5922 if (th_flags & TH_FIN) 5923 len++; 5924 } 5925 if (SEQ_LEQ((seq_out + len), snd_una)) { 5926 /* Are sending an old segment to induce an ack (keep-alive)? */ 5927 return; 5928 } 5929 if (SEQ_LT(seq_out, snd_una)) { 5930 /* huh? should we panic? */ 5931 uint32_t end; 5932 5933 end = seq_out + len; 5934 seq_out = snd_una; 5935 len = end - seq_out; 5936 } 5937 snd_max = tp->snd_max; 5938 if (len == 0) { 5939 /* We don't log zero window probes */ 5940 return; 5941 } 5942 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1); 5943 /* First question is it a retransmission? */ 5944 if (seq_out == snd_max) { 5945 again: 5946 rsm = bbr_alloc(bbr); 5947 if (rsm == NULL) { 5948 return; 5949 } 5950 rsm->r_flags = 0; 5951 if (th_flags & TH_SYN) 5952 rsm->r_flags |= BBR_HAS_SYN; 5953 if (th_flags & TH_FIN) 5954 rsm->r_flags |= BBR_HAS_FIN; 5955 rsm->r_tim_lastsent[0] = cts; 5956 rsm->r_rtr_cnt = 1; 5957 rsm->r_rtr_bytes = 0; 5958 rsm->r_start = seq_out; 5959 rsm->r_end = rsm->r_start + len; 5960 rsm->r_dupack = 0; 5961 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5962 rsm->r_pacing_delay = pacing_time; 5963 rsm->r_ts_valid = bbr->rc_ts_valid; 5964 if (bbr->rc_ts_valid) 5965 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5966 rsm->r_del_time = bbr->r_ctl.rc_del_time; 5967 if (bbr->r_ctl.r_app_limited_until) 5968 rsm->r_app_limited = 1; 5969 else 5970 rsm->r_app_limited = 0; 5971 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5972 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5973 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5974 /* 5975 * Here we must also add in this rsm since snd_max 5976 * is updated after we return from a new send. 5977 */ 5978 rsm->r_flight_at_send += len; 5979 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 5980 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5981 rsm->r_in_tmap = 1; 5982 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5983 rsm->r_bbr_state = bbr_state_val(bbr); 5984 else 5985 rsm->r_bbr_state = 8; 5986 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5987 rsm->r_is_gain = 1; 5988 rsm->r_is_drain = 0; 5989 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5990 rsm->r_is_drain = 1; 5991 rsm->r_is_gain = 0; 5992 } else { 5993 rsm->r_is_drain = 0; 5994 rsm->r_is_gain = 0; 5995 } 5996 return; 5997 } 5998 /* 5999 * If we reach here its a retransmission and we need to find it. 6000 */ 6001 more: 6002 if (hintrsm && (hintrsm->r_start == seq_out)) { 6003 rsm = hintrsm; 6004 hintrsm = NULL; 6005 } else if (bbr->r_ctl.rc_next) { 6006 /* We have a hint from a previous run */ 6007 rsm = bbr->r_ctl.rc_next; 6008 } else { 6009 /* No hints sorry */ 6010 rsm = NULL; 6011 } 6012 if ((rsm) && (rsm->r_start == seq_out)) { 6013 /* 6014 * We used rc_next or hintrsm to retransmit, hopefully the 6015 * likely case. 6016 */ 6017 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6018 if (len == 0) { 6019 return; 6020 } else { 6021 goto more; 6022 } 6023 } 6024 /* Ok it was not the last pointer go through it the hard way. */ 6025 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6026 if (rsm->r_start == seq_out) { 6027 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6028 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 6029 if (len == 0) { 6030 return; 6031 } else { 6032 continue; 6033 } 6034 } 6035 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 6036 /* Transmitted within this piece */ 6037 /* 6038 * Ok we must split off the front and then let the 6039 * update do the rest 6040 */ 6041 nrsm = bbr_alloc_full_limit(bbr); 6042 if (nrsm == NULL) { 6043 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 6044 return; 6045 } 6046 /* 6047 * copy rsm to nrsm and then trim the front of rsm 6048 * to not include this part. 6049 */ 6050 bbr_clone_rsm(bbr, nrsm, rsm, seq_out); 6051 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 6052 if (rsm->r_in_tmap) { 6053 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6054 nrsm->r_in_tmap = 1; 6055 } 6056 rsm->r_flags &= (~BBR_HAS_FIN); 6057 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time); 6058 if (len == 0) { 6059 return; 6060 } 6061 } 6062 } 6063 /* 6064 * Hmm not found in map did they retransmit both old and on into the 6065 * new? 6066 */ 6067 if (seq_out == tp->snd_max) { 6068 goto again; 6069 } else if (SEQ_LT(seq_out, tp->snd_max)) { 6070 #ifdef BBR_INVARIANTS 6071 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 6072 seq_out, len, tp->snd_una, tp->snd_max); 6073 printf("Starting Dump of all rack entries\n"); 6074 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6075 printf("rsm:%p start:%u end:%u\n", 6076 rsm, rsm->r_start, rsm->r_end); 6077 } 6078 printf("Dump complete\n"); 6079 panic("seq_out not found rack:%p tp:%p", 6080 bbr, tp); 6081 #endif 6082 } else { 6083 #ifdef BBR_INVARIANTS 6084 /* 6085 * Hmm beyond sndmax? (only if we are using the new rtt-pack 6086 * flag) 6087 */ 6088 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 6089 seq_out, len, tp->snd_max, tp); 6090 #endif 6091 } 6092 } 6093 6094 static void 6095 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt) 6096 { 6097 /* 6098 * Collapse timeout back the cum-ack moved. 6099 */ 6100 tp->t_rxtshift = 0; 6101 tp->t_softerror = 0; 6102 } 6103 6104 static void 6105 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin) 6106 { 6107 bbr->rtt_valid = 1; 6108 bbr->r_ctl.cur_rtt = rtt_usecs; 6109 bbr->r_ctl.ts_in = tsin; 6110 if (rsm_send_time) 6111 bbr->r_ctl.cur_rtt_send_time = rsm_send_time; 6112 } 6113 6114 static void 6115 bbr_make_timestamp_determination(struct tcp_bbr *bbr) 6116 { 6117 /** 6118 * We have in our bbr control: 6119 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp). 6120 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts). 6121 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts) 6122 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time) 6123 * 6124 * Now we can calculate the time between the sends by doing: 6125 * 6126 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts 6127 * 6128 * And the peer's time between receiving them by doing: 6129 * 6130 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp 6131 * 6132 * We want to figure out if the timestamp values are in msec, 10msec or usec. 6133 * We also may find that we can't use the timestamps if say we see 6134 * that the peer_delta indicates that though we may have taken 10ms to 6135 * pace out the data, it only saw 1ms between the two packets. This would 6136 * indicate that somewhere on the path is a batching entity that is giving 6137 * out time-slices of the actual b/w. This would mean we could not use 6138 * reliably the peers timestamps. 6139 * 6140 * We expect delta > peer_delta initially. Until we figure out the 6141 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio. 6142 * If we place 1000 there then its a ms vs our usec. If we place 10000 there 6143 * then its 10ms vs our usec. If the peer is running a usec clock we would 6144 * put a 1 there. If the value is faster then ours, we will disable the 6145 * use of timestamps (though we could revist this later if we find it to be not 6146 * just an isolated one or two flows)). 6147 * 6148 * To detect the batching middle boxes we will come up with our compensation and 6149 * if with it in place, we find the peer is drastically off (by some margin) in 6150 * the smaller direction, then we will assume the worst case and disable use of timestamps. 6151 * 6152 */ 6153 uint64_t delta, peer_delta, delta_up; 6154 6155 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts; 6156 if (delta < bbr_min_usec_delta) { 6157 /* 6158 * Have not seen a min amount of time 6159 * between our send times so we can 6160 * make a determination of the timestamp 6161 * yet. 6162 */ 6163 return; 6164 } 6165 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp; 6166 if (peer_delta < bbr_min_peer_delta) { 6167 /* 6168 * We may have enough in the form of 6169 * our delta but the peers number 6170 * has not changed that much. It could 6171 * be its clock ratio is such that 6172 * we need more data (10ms tick) or 6173 * there may be other compression scenarios 6174 * going on. In any event we need the 6175 * spread to be larger. 6176 */ 6177 return; 6178 } 6179 /* Ok lets first see which way our delta is going */ 6180 if (peer_delta > delta) { 6181 /* Very unlikely, the peer without 6182 * compensation shows that it saw 6183 * the two sends arrive further apart 6184 * then we saw then in micro-seconds. 6185 */ 6186 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) { 6187 /* well it looks like the peer is a micro-second clock. */ 6188 bbr->rc_ts_clock_set = 1; 6189 bbr->r_ctl.bbr_peer_tsratio = 1; 6190 } else { 6191 bbr->rc_ts_cant_be_used = 1; 6192 bbr->rc_ts_clock_set = 1; 6193 } 6194 return; 6195 } 6196 /* Ok we know that the peer_delta is smaller than our send distance */ 6197 bbr->rc_ts_clock_set = 1; 6198 /* First question is it within the percentage that they are using usec time? */ 6199 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent; 6200 if ((peer_delta + delta_up) >= delta) { 6201 /* Its a usec clock */ 6202 bbr->r_ctl.bbr_peer_tsratio = 1; 6203 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6204 return; 6205 } 6206 /* Ok if not usec, what about 10usec (though unlikely)? */ 6207 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent; 6208 if (((peer_delta * 10) + delta_up) >= delta) { 6209 bbr->r_ctl.bbr_peer_tsratio = 10; 6210 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6211 return; 6212 } 6213 /* And what about 100usec (though again unlikely)? */ 6214 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent; 6215 if (((peer_delta * 100) + delta_up) >= delta) { 6216 bbr->r_ctl.bbr_peer_tsratio = 100; 6217 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6218 return; 6219 } 6220 /* And how about 1 msec (the most likely one)? */ 6221 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent; 6222 if (((peer_delta * 1000) + delta_up) >= delta) { 6223 bbr->r_ctl.bbr_peer_tsratio = 1000; 6224 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6225 return; 6226 } 6227 /* Ok if not msec could it be 10 msec? */ 6228 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent; 6229 if (((peer_delta * 10000) + delta_up) >= delta) { 6230 bbr->r_ctl.bbr_peer_tsratio = 10000; 6231 return; 6232 } 6233 /* If we fall down here the clock tick so slowly we can't use it */ 6234 bbr->rc_ts_cant_be_used = 1; 6235 bbr->r_ctl.bbr_peer_tsratio = 0; 6236 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6237 } 6238 6239 /* 6240 * Collect new round-trip time estimate 6241 * and update averages and current timeout. 6242 */ 6243 static void 6244 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts) 6245 { 6246 int32_t delta; 6247 uint32_t rtt, tsin; 6248 int32_t rtt_ticks; 6249 6250 if (bbr->rtt_valid == 0) 6251 /* No valid sample */ 6252 return; 6253 6254 rtt = bbr->r_ctl.cur_rtt; 6255 tsin = bbr->r_ctl.ts_in; 6256 if (bbr->rc_prtt_set_ts) { 6257 /* 6258 * We are to force feed the rttProp filter due 6259 * to an entry into PROBE_RTT. This assures 6260 * that the times are sync'd between when we 6261 * go into PROBE_RTT and the filter expiration. 6262 * 6263 * Google does not use a true filter, so they do 6264 * this implicitly since they only keep one value 6265 * and when they enter probe-rtt they update the 6266 * value to the newest rtt. 6267 */ 6268 uint32_t rtt_prop; 6269 6270 bbr->rc_prtt_set_ts = 0; 6271 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 6272 if (rtt > rtt_prop) 6273 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts); 6274 else 6275 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6276 } 6277 #ifdef STATS 6278 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt)); 6279 #endif 6280 if (bbr->rc_ack_was_delayed) 6281 rtt += bbr->r_ctl.rc_ack_hdwr_delay; 6282 6283 if (rtt < bbr->r_ctl.rc_lowest_rtt) 6284 bbr->r_ctl.rc_lowest_rtt = rtt; 6285 bbr_log_rtt_sample(bbr, rtt, tsin); 6286 if (bbr->r_init_rtt) { 6287 /* 6288 * The initial rtt is not-trusted, nuke it and lets get 6289 * our first valid measurement in. 6290 */ 6291 bbr->r_init_rtt = 0; 6292 tp->t_srtt = 0; 6293 } 6294 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) { 6295 /* 6296 * So we have not yet figured out 6297 * what the peers TSTMP value is 6298 * in (most likely ms). We need a 6299 * series of cum-ack's to determine 6300 * this reliably. 6301 */ 6302 if (bbr->rc_ack_is_cumack) { 6303 if (bbr->rc_ts_data_set) { 6304 /* Lets attempt to determine the timestamp granularity. */ 6305 bbr_make_timestamp_determination(bbr); 6306 } else { 6307 bbr->rc_ts_data_set = 1; 6308 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts; 6309 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time; 6310 } 6311 } else { 6312 /* 6313 * We have to have consecutive acks 6314 * reset any "filled" state to none. 6315 */ 6316 bbr->rc_ts_data_set = 0; 6317 } 6318 } 6319 /* Round it up */ 6320 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1))); 6321 if (tp->t_srtt != 0) { 6322 /* 6323 * srtt is stored as fixed point with 5 bits after the 6324 * binary point (i.e., scaled by 8). The following magic is 6325 * equivalent to the smoothing algorithm in rfc793 with an 6326 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 6327 * Adjust rtt to origin 0. 6328 */ 6329 6330 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT) 6331 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 6332 6333 tp->t_srtt += delta; 6334 if (tp->t_srtt <= 0) 6335 tp->t_srtt = 1; 6336 6337 /* 6338 * We accumulate a smoothed rtt variance (actually, a 6339 * smoothed mean difference), then set the retransmit timer 6340 * to smoothed rtt + 4 times the smoothed variance. rttvar 6341 * is stored as fixed point with 4 bits after the binary 6342 * point (scaled by 16). The following is equivalent to 6343 * rfc793 smoothing with an alpha of .75 (rttvar = 6344 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 6345 * wired-in beta. 6346 */ 6347 if (delta < 0) 6348 delta = -delta; 6349 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 6350 tp->t_rttvar += delta; 6351 if (tp->t_rttvar <= 0) 6352 tp->t_rttvar = 1; 6353 } else { 6354 /* 6355 * No rtt measurement yet - use the unsmoothed rtt. Set the 6356 * variance to half the rtt (so our first retransmit happens 6357 * at 3*rtt). 6358 */ 6359 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT; 6360 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1); 6361 } 6362 KMOD_TCPSTAT_INC(tcps_rttupdated); 6363 if (tp->t_rttupdated < UCHAR_MAX) 6364 tp->t_rttupdated++; 6365 #ifdef STATS 6366 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks)); 6367 #endif 6368 /* 6369 * the retransmit should happen at rtt + 4 * rttvar. Because of the 6370 * way we do the smoothing, srtt and rttvar will each average +1/2 6371 * tick of bias. When we compute the retransmit timer, we want 1/2 6372 * tick of rounding and 1 extra tick because of +-1/2 tick 6373 * uncertainty in the firing of the timer. The bias will give us 6374 * exactly the 1.5 tick we need. But, because the bias is 6375 * statistical, we have to test that we don't drop below the minimum 6376 * feasible timer (which is 2 ticks). 6377 */ 6378 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 6379 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2), 6380 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 6381 6382 /* 6383 * We received an ack for a packet that wasn't retransmitted; it is 6384 * probably safe to discard any error indications we've received 6385 * recently. This isn't quite right, but close enough for now (a 6386 * route might have failed after we sent a segment, and the return 6387 * path might not be symmetrical). 6388 */ 6389 tp->t_softerror = 0; 6390 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 6391 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt) 6392 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt; 6393 } 6394 6395 static void 6396 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line) 6397 { 6398 bbr->r_ctl.rc_rtt_shrinks = cts; 6399 if (bbr_can_force_probertt && 6400 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 6401 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 6402 /* 6403 * We should enter probe-rtt its been too long 6404 * since we have been there. 6405 */ 6406 bbr_enter_probe_rtt(bbr, cts, __LINE__); 6407 } else 6408 bbr_check_probe_rtt_limits(bbr, cts); 6409 } 6410 6411 static void 6412 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts) 6413 { 6414 uint64_t orig_bw; 6415 6416 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) { 6417 /* We never apply a zero measurement */ 6418 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0, 6419 0, 0, 0, 0, 0, 0); 6420 return; 6421 } 6422 if (bbr->r_ctl.r_measurement_count < 0xffffffff) 6423 bbr->r_ctl.r_measurement_count++; 6424 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 6425 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch); 6426 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw, 6427 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate), 6428 0, 0, 0, 0, 0, 0); 6429 if (orig_bw && 6430 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) { 6431 if (bbr->bbr_hdrw_pacing) { 6432 /* 6433 * Apply a new rate to the hardware 6434 * possibly. 6435 */ 6436 bbr_update_hardware_pacing_rate(bbr, cts); 6437 } 6438 bbr_set_state_target(bbr, __LINE__); 6439 tcp_bbr_tso_size_check(bbr, cts); 6440 if (bbr->r_recovery_bw) { 6441 bbr_setup_red_bw(bbr, cts); 6442 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW); 6443 } 6444 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate)) 6445 tcp_bbr_tso_size_check(bbr, cts); 6446 } 6447 6448 static void 6449 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6450 { 6451 if (bbr->rc_in_persist == 0) { 6452 /* We log only when not in persist */ 6453 /* Translate to a Bytes Per Second */ 6454 uint64_t tim, bw, ts_diff, ts_bw; 6455 uint32_t delivered; 6456 6457 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6458 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6459 else 6460 tim = 1; 6461 /* 6462 * Now that we have processed the tim (skipping the sample 6463 * or possibly updating the time, go ahead and 6464 * calculate the cdr. 6465 */ 6466 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6467 bw = (uint64_t)delivered; 6468 bw *= (uint64_t)USECS_IN_SECOND; 6469 bw /= tim; 6470 if (bw == 0) { 6471 /* We must have a calculatable amount */ 6472 return; 6473 } 6474 /* 6475 * If we are using this b/w shove it in now so we 6476 * can see in the trace viewer if it gets over-ridden. 6477 */ 6478 if (rsm->r_ts_valid && 6479 bbr->rc_ts_valid && 6480 bbr->rc_ts_clock_set && 6481 (bbr->rc_ts_cant_be_used == 0) && 6482 bbr->rc_use_ts_limit) { 6483 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1); 6484 ts_diff *= bbr->r_ctl.bbr_peer_tsratio; 6485 if ((delivered == 0) || 6486 (rtt < 1000)) { 6487 /* Can't use the ts */ 6488 bbr_log_type_bbrupd(bbr, 61, cts, 6489 ts_diff, 6490 bbr->r_ctl.last_inbound_ts, 6491 rsm->r_del_ack_ts, 0, 6492 0, 0, 0, delivered); 6493 } else { 6494 ts_bw = (uint64_t)delivered; 6495 ts_bw *= (uint64_t)USECS_IN_SECOND; 6496 ts_bw /= ts_diff; 6497 bbr_log_type_bbrupd(bbr, 62, cts, 6498 (ts_bw >> 32), 6499 (ts_bw & 0xffffffff), 0, 0, 6500 0, 0, ts_diff, delivered); 6501 if ((bbr->ts_can_raise) && 6502 (ts_bw > bw)) { 6503 bbr_log_type_bbrupd(bbr, 8, cts, 6504 delivered, 6505 ts_diff, 6506 (bw >> 32), 6507 (bw & 0x00000000ffffffff), 6508 0, 0, 0, 0); 6509 bw = ts_bw; 6510 } else if (ts_bw && (ts_bw < bw)) { 6511 bbr_log_type_bbrupd(bbr, 7, cts, 6512 delivered, 6513 ts_diff, 6514 (bw >> 32), 6515 (bw & 0x00000000ffffffff), 6516 0, 0, 0, 0); 6517 bw = ts_bw; 6518 } 6519 } 6520 } 6521 if (rsm->r_first_sent_time && 6522 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6523 uint64_t sbw, sti; 6524 /* 6525 * We use what was in flight at the time of our 6526 * send and the size of this send to figure 6527 * out what we have been sending at (amount). 6528 * For the time we take from the time of 6529 * the send of the first send outstanding 6530 * until this send plus this sends pacing 6531 * time. This gives us a good calculation 6532 * as to the rate we have been sending at. 6533 */ 6534 6535 sbw = (uint64_t)(rsm->r_flight_at_send); 6536 sbw *= (uint64_t)USECS_IN_SECOND; 6537 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6538 sti += rsm->r_pacing_delay; 6539 sbw /= sti; 6540 if (sbw < bw) { 6541 bbr_log_type_bbrupd(bbr, 6, cts, 6542 delivered, 6543 (uint32_t)sti, 6544 (bw >> 32), 6545 (uint32_t)bw, 6546 rsm->r_first_sent_time, 0, (sbw >> 32), 6547 (uint32_t)sbw); 6548 bw = sbw; 6549 } 6550 } 6551 /* Use the google algorithm for b/w measurements */ 6552 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6553 if ((rsm->r_app_limited == 0) || 6554 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) { 6555 tcp_bbr_commit_bw(bbr, cts); 6556 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6557 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6558 } 6559 } 6560 } 6561 6562 static void 6563 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6564 { 6565 if (bbr->rc_in_persist == 0) { 6566 /* We log only when not in persist */ 6567 /* Translate to a Bytes Per Second */ 6568 uint64_t tim, bw; 6569 uint32_t delivered; 6570 int no_apply = 0; 6571 6572 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6573 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6574 else 6575 tim = 1; 6576 /* 6577 * Now that we have processed the tim (skipping the sample 6578 * or possibly updating the time, go ahead and 6579 * calculate the cdr. 6580 */ 6581 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6582 bw = (uint64_t)delivered; 6583 bw *= (uint64_t)USECS_IN_SECOND; 6584 bw /= tim; 6585 if (tim < bbr->r_ctl.rc_lowest_rtt) { 6586 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6587 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6588 6589 no_apply = 1; 6590 } 6591 /* 6592 * If we are using this b/w shove it in now so we 6593 * can see in the trace viewer if it gets over-ridden. 6594 */ 6595 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6596 /* Gate by the sending rate */ 6597 if (rsm->r_first_sent_time && 6598 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6599 uint64_t sbw, sti; 6600 /* 6601 * We use what was in flight at the time of our 6602 * send and the size of this send to figure 6603 * out what we have been sending at (amount). 6604 * For the time we take from the time of 6605 * the send of the first send outstanding 6606 * until this send plus this sends pacing 6607 * time. This gives us a good calculation 6608 * as to the rate we have been sending at. 6609 */ 6610 6611 sbw = (uint64_t)(rsm->r_flight_at_send); 6612 sbw *= (uint64_t)USECS_IN_SECOND; 6613 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6614 sti += rsm->r_pacing_delay; 6615 sbw /= sti; 6616 if (sbw < bw) { 6617 bbr_log_type_bbrupd(bbr, 6, cts, 6618 delivered, 6619 (uint32_t)sti, 6620 (bw >> 32), 6621 (uint32_t)bw, 6622 rsm->r_first_sent_time, 0, (sbw >> 32), 6623 (uint32_t)sbw); 6624 bw = sbw; 6625 } 6626 if ((sti > tim) && 6627 (sti < bbr->r_ctl.rc_lowest_rtt)) { 6628 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6629 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6630 no_apply = 1; 6631 } else 6632 no_apply = 0; 6633 } 6634 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6635 if ((no_apply == 0) && 6636 ((rsm->r_app_limited == 0) || 6637 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) { 6638 tcp_bbr_commit_bw(bbr, cts); 6639 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6640 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6641 } 6642 } 6643 } 6644 6645 static void 6646 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin, 6647 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to) 6648 { 6649 uint64_t old_rttprop; 6650 6651 /* Update our delivery time and amount */ 6652 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start); 6653 bbr->r_ctl.rc_del_time = cts; 6654 if (rtt == 0) { 6655 /* 6656 * 0 means its a retransmit, for now we don't use these for 6657 * the rest of BBR. 6658 */ 6659 return; 6660 } 6661 if ((bbr->rc_use_google == 0) && 6662 (match != BBR_RTT_BY_EXACTMATCH) && 6663 (match != BBR_RTT_BY_TIMESTAMP)){ 6664 /* 6665 * We get a lot of rtt updates, lets not pay attention to 6666 * any that are not an exact match. That way we don't have 6667 * to worry about timestamps and the whole nonsense of 6668 * unsure if its a retransmission etc (if we ever had the 6669 * timestamp fixed to always have the last thing sent this 6670 * would not be a issue). 6671 */ 6672 return; 6673 } 6674 if ((bbr_no_retran && bbr->rc_use_google) && 6675 (match != BBR_RTT_BY_EXACTMATCH) && 6676 (match != BBR_RTT_BY_TIMESTAMP)){ 6677 /* 6678 * We only do measurements in google mode 6679 * with bbr_no_retran on for sure things. 6680 */ 6681 return; 6682 } 6683 /* Only update srtt if we know by exact match */ 6684 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin); 6685 if (ack_type == BBR_CUM_ACKED) 6686 bbr->rc_ack_is_cumack = 1; 6687 else 6688 bbr->rc_ack_is_cumack = 0; 6689 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP); 6690 /* 6691 * Note the following code differs to the original 6692 * BBR spec. It calls for <= not <. However after a 6693 * long discussion in email with Neal, he acknowledged 6694 * that it should be < than so that we will have flows 6695 * going into probe-rtt (we were seeing cases where that 6696 * did not happen and caused ugly things to occur). We 6697 * have added this agreed upon fix to our code base. 6698 */ 6699 if (rtt < old_rttprop) { 6700 /* Update when we last saw a rtt drop */ 6701 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0); 6702 bbr_set_reduced_rtt(bbr, cts, __LINE__); 6703 } 6704 bbr_log_type_bbrrttprop(bbr, rtt, rsm->r_end, uts, cts, 6705 match, rsm->r_start, rsm->r_flags); 6706 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6707 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) { 6708 /* 6709 * The RTT-prop moved, reset the target (may be a 6710 * nop for some states). 6711 */ 6712 bbr_set_state_target(bbr, __LINE__); 6713 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) 6714 bbr_log_rtt_shrinks(bbr, cts, 0, 0, 6715 __LINE__, BBR_RTTS_NEW_TARGET, 0); 6716 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP)) 6717 /* It went up */ 6718 bbr_check_probe_rtt_limits(bbr, cts); 6719 } 6720 if ((bbr->rc_use_google == 0) && 6721 (match == BBR_RTT_BY_TIMESTAMP)) { 6722 /* 6723 * We don't do b/w update with 6724 * these since they are not really 6725 * reliable. 6726 */ 6727 return; 6728 } 6729 if (bbr->r_ctl.r_app_limited_until && 6730 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) { 6731 /* We are no longer app-limited */ 6732 bbr->r_ctl.r_app_limited_until = 0; 6733 } 6734 if (bbr->rc_use_google) { 6735 bbr_google_measurement(bbr, rsm, rtt, cts); 6736 } else { 6737 bbr_nf_measurement(bbr, rsm, rtt, cts); 6738 } 6739 } 6740 6741 /* 6742 * Convert a timestamp that the main stack 6743 * uses (milliseconds) into one that bbr uses 6744 * (microseconds). Return that converted timestamp. 6745 */ 6746 static uint32_t 6747 bbr_ts_convert(uint32_t cts) { 6748 uint32_t sec, msec; 6749 6750 sec = cts / MS_IN_USEC; 6751 msec = cts - (MS_IN_USEC * sec); 6752 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC)); 6753 } 6754 6755 /* 6756 * Return 0 if we did not update the RTT time, return 6757 * 1 if we did. 6758 */ 6759 static int 6760 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, 6761 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack) 6762 { 6763 int32_t i; 6764 uint32_t t, uts = 0; 6765 6766 if ((rsm->r_flags & BBR_ACKED) || 6767 (rsm->r_flags & BBR_WAS_RENEGED) || 6768 (rsm->r_flags & BBR_RXT_CLEARED)) { 6769 /* Already done */ 6770 return (0); 6771 } 6772 if (rsm->r_rtt_not_allowed) { 6773 /* Not allowed */ 6774 return (0); 6775 } 6776 if (rsm->r_rtr_cnt == 1) { 6777 /* 6778 * Only one transmit. Hopefully the normal case. 6779 */ 6780 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0])) 6781 t = cts - rsm->r_tim_lastsent[0]; 6782 else 6783 t = 1; 6784 bbr->r_ctl.rc_last_rtt = t; 6785 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6786 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to); 6787 return (1); 6788 } 6789 /* Convert to usecs */ 6790 if ((bbr_can_use_ts_for_rtt == 1) && 6791 (bbr->rc_use_google == 1) && 6792 (ack_type == BBR_CUM_ACKED) && 6793 (to->to_flags & TOF_TS) && 6794 (to->to_tsecr != 0)) { 6795 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr; 6796 if (t < 1) 6797 t = 1; 6798 t *= MS_IN_USEC; 6799 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6800 BBR_RTT_BY_TIMESTAMP, 6801 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)], 6802 ack_type, to); 6803 return (1); 6804 } 6805 uts = bbr_ts_convert(to->to_tsecr); 6806 if ((to->to_flags & TOF_TS) && 6807 (to->to_tsecr != 0) && 6808 (ack_type == BBR_CUM_ACKED) && 6809 ((rsm->r_flags & BBR_OVERMAX) == 0)) { 6810 /* 6811 * Now which timestamp does it match? In this block the ACK 6812 * may be coming from a previous transmission. 6813 */ 6814 uint32_t fudge; 6815 6816 fudge = BBR_TIMER_FUDGE; 6817 for (i = 0; i < rsm->r_rtr_cnt; i++) { 6818 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) && 6819 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) { 6820 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6821 t = cts - rsm->r_tim_lastsent[i]; 6822 else 6823 t = 1; 6824 bbr->r_ctl.rc_last_rtt = t; 6825 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING, 6826 rsm->r_tim_lastsent[i], ack_type, to); 6827 if ((i + 1) < rsm->r_rtr_cnt) { 6828 /* Likely */ 6829 return (0); 6830 } else if (rsm->r_flags & BBR_TLP) { 6831 bbr->rc_tlp_rtx_out = 0; 6832 } 6833 return (1); 6834 } 6835 } 6836 /* Fall through if we can't find a matching timestamp */ 6837 } 6838 /* 6839 * Ok its a SACK block that we retransmitted. or a windows 6840 * machine without timestamps. We can tell nothing from the 6841 * time-stamp since its not there or the time the peer last 6842 * received a segment that moved forward its cum-ack point. 6843 * 6844 * Lets look at the last retransmit and see what we can tell 6845 * (with BBR for space we only keep 2 note we have to keep 6846 * at least 2 so the map can not be condensed more). 6847 */ 6848 i = rsm->r_rtr_cnt - 1; 6849 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6850 t = cts - rsm->r_tim_lastsent[i]; 6851 else 6852 goto not_sure; 6853 if (t < bbr->r_ctl.rc_lowest_rtt) { 6854 /* 6855 * We retransmitted and the ack came back in less 6856 * than the smallest rtt we have observed in the 6857 * windowed rtt. We most likey did an improper 6858 * retransmit as outlined in 4.2 Step 3 point 2 in 6859 * the rack-draft. 6860 * 6861 * Use the prior transmission to update all the 6862 * information as long as there is only one prior 6863 * transmission. 6864 */ 6865 if ((rsm->r_flags & BBR_OVERMAX) == 0) { 6866 #ifdef BBR_INVARIANTS 6867 if (rsm->r_rtr_cnt == 1) 6868 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags); 6869 #endif 6870 i = rsm->r_rtr_cnt - 2; 6871 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6872 t = cts - rsm->r_tim_lastsent[i]; 6873 else 6874 t = 1; 6875 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET, 6876 rsm->r_tim_lastsent[i], ack_type, to); 6877 return (0); 6878 } else { 6879 /* 6880 * Too many prior transmissions, just 6881 * updated BBR delivered 6882 */ 6883 not_sure: 6884 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6885 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6886 } 6887 } else { 6888 /* 6889 * We retransmitted it and the retransmit did the 6890 * job. 6891 */ 6892 if (rsm->r_flags & BBR_TLP) 6893 bbr->rc_tlp_rtx_out = 0; 6894 if ((rsm->r_flags & BBR_OVERMAX) == 0) 6895 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, 6896 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to); 6897 else 6898 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6899 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6900 return (1); 6901 } 6902 return (0); 6903 } 6904 6905 /* 6906 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 6907 */ 6908 static void 6909 bbr_log_sack_passed(struct tcpcb *tp, 6910 struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 6911 { 6912 struct bbr_sendmap *nrsm; 6913 6914 nrsm = rsm; 6915 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap, 6916 bbr_head, r_tnext) { 6917 if (nrsm == rsm) { 6918 /* Skip original segment he is acked */ 6919 continue; 6920 } 6921 if (nrsm->r_flags & BBR_ACKED) { 6922 /* Skip ack'd segments */ 6923 continue; 6924 } 6925 if (nrsm->r_flags & BBR_SACK_PASSED) { 6926 /* 6927 * We found one that is already marked 6928 * passed, we have been here before and 6929 * so all others below this are marked. 6930 */ 6931 break; 6932 } 6933 BBR_STAT_INC(bbr_sack_passed); 6934 nrsm->r_flags |= BBR_SACK_PASSED; 6935 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) && 6936 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) { 6937 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start; 6938 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start; 6939 nrsm->r_flags |= BBR_MARKED_LOST; 6940 } 6941 nrsm->r_flags &= ~BBR_WAS_SACKPASS; 6942 } 6943 } 6944 6945 /* 6946 * Returns the number of bytes that were 6947 * newly ack'd by sack blocks. 6948 */ 6949 static uint32_t 6950 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack, 6951 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts) 6952 { 6953 int32_t times = 0; 6954 uint32_t start, end, changed = 0; 6955 struct bbr_sendmap *rsm, *nrsm; 6956 int32_t used_ref = 1; 6957 uint8_t went_back = 0, went_fwd = 0; 6958 6959 start = sack->start; 6960 end = sack->end; 6961 rsm = *prsm; 6962 if (rsm == NULL) 6963 used_ref = 0; 6964 6965 /* Do we locate the block behind where we last were? */ 6966 if (rsm && SEQ_LT(start, rsm->r_start)) { 6967 went_back = 1; 6968 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 6969 if (SEQ_GEQ(start, rsm->r_start) && 6970 SEQ_LT(start, rsm->r_end)) { 6971 goto do_rest_ofb; 6972 } 6973 } 6974 } 6975 start_at_beginning: 6976 went_fwd = 1; 6977 /* 6978 * Ok lets locate the block where this guy is fwd from rsm (if its 6979 * set) 6980 */ 6981 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) { 6982 if (SEQ_GEQ(start, rsm->r_start) && 6983 SEQ_LT(start, rsm->r_end)) { 6984 break; 6985 } 6986 } 6987 do_rest_ofb: 6988 if (rsm == NULL) { 6989 /* 6990 * This happens when we get duplicate sack blocks with the 6991 * same end. For example SACK 4: 100 SACK 3: 100 The sort 6992 * will not change there location so we would just start at 6993 * the end of the first one and get lost. 6994 */ 6995 if (tp->t_flags & TF_SENTFIN) { 6996 /* 6997 * Check to see if we have not logged the FIN that 6998 * went out. 6999 */ 7000 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7001 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 7002 /* 7003 * Ok we did not get the FIN logged. 7004 */ 7005 nrsm->r_end++; 7006 rsm = nrsm; 7007 goto do_rest_ofb; 7008 } 7009 } 7010 if (times == 1) { 7011 #ifdef BBR_INVARIANTS 7012 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p", 7013 tp, bbr, sack, to, prsm); 7014 #else 7015 goto out; 7016 #endif 7017 } 7018 times++; 7019 BBR_STAT_INC(bbr_sack_proc_restart); 7020 rsm = NULL; 7021 goto start_at_beginning; 7022 } 7023 /* Ok we have an ACK for some piece of rsm */ 7024 if (rsm->r_start != start) { 7025 /* 7026 * Need to split this in two pieces the before and after. 7027 */ 7028 if (bbr_sack_mergable(rsm, start, end)) 7029 nrsm = bbr_alloc_full_limit(bbr); 7030 else 7031 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7032 if (nrsm == NULL) { 7033 /* We could not allocate ignore the sack */ 7034 struct sackblk blk; 7035 7036 blk.start = start; 7037 blk.end = end; 7038 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7039 goto out; 7040 } 7041 bbr_clone_rsm(bbr, nrsm, rsm, start); 7042 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7043 if (rsm->r_in_tmap) { 7044 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7045 nrsm->r_in_tmap = 1; 7046 } 7047 rsm->r_flags &= (~BBR_HAS_FIN); 7048 rsm = nrsm; 7049 } 7050 if (SEQ_GEQ(end, rsm->r_end)) { 7051 /* 7052 * The end of this block is either beyond this guy or right 7053 * at this guy. 7054 */ 7055 if ((rsm->r_flags & BBR_ACKED) == 0) { 7056 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7057 changed += (rsm->r_end - rsm->r_start); 7058 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7059 bbr_log_sack_passed(tp, bbr, rsm); 7060 if (rsm->r_flags & BBR_MARKED_LOST) { 7061 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7062 } 7063 /* Is Reordering occuring? */ 7064 if (rsm->r_flags & BBR_SACK_PASSED) { 7065 BBR_STAT_INC(bbr_reorder_seen); 7066 bbr->r_ctl.rc_reorder_ts = cts; 7067 if (rsm->r_flags & BBR_MARKED_LOST) { 7068 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7069 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7070 /* LT sampling also needs adjustment */ 7071 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7072 } 7073 } 7074 rsm->r_flags |= BBR_ACKED; 7075 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7076 if (rsm->r_in_tmap) { 7077 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7078 rsm->r_in_tmap = 0; 7079 } 7080 } 7081 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7082 if (end == rsm->r_end) { 7083 /* This block only - done */ 7084 goto out; 7085 } 7086 /* There is more not coverend by this rsm move on */ 7087 start = rsm->r_end; 7088 nrsm = TAILQ_NEXT(rsm, r_next); 7089 rsm = nrsm; 7090 times = 0; 7091 goto do_rest_ofb; 7092 } 7093 if (rsm->r_flags & BBR_ACKED) { 7094 /* Been here done that */ 7095 goto out; 7096 } 7097 /* Ok we need to split off this one at the tail */ 7098 if (bbr_sack_mergable(rsm, start, end)) 7099 nrsm = bbr_alloc_full_limit(bbr); 7100 else 7101 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7102 if (nrsm == NULL) { 7103 /* failed XXXrrs what can we do but loose the sack info? */ 7104 struct sackblk blk; 7105 7106 blk.start = start; 7107 blk.end = end; 7108 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7109 goto out; 7110 } 7111 /* Clone it */ 7112 bbr_clone_rsm(bbr, nrsm, rsm, end); 7113 /* The sack block does not cover this guy fully */ 7114 rsm->r_flags &= (~BBR_HAS_FIN); 7115 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7116 if (rsm->r_in_tmap) { 7117 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7118 nrsm->r_in_tmap = 1; 7119 } 7120 nrsm->r_dupack = 0; 7121 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7122 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7123 changed += (rsm->r_end - rsm->r_start); 7124 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7125 bbr_log_sack_passed(tp, bbr, rsm); 7126 /* Is Reordering occuring? */ 7127 if (rsm->r_flags & BBR_MARKED_LOST) { 7128 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7129 } 7130 if (rsm->r_flags & BBR_SACK_PASSED) { 7131 BBR_STAT_INC(bbr_reorder_seen); 7132 bbr->r_ctl.rc_reorder_ts = cts; 7133 if (rsm->r_flags & BBR_MARKED_LOST) { 7134 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7135 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7136 /* LT sampling also needs adjustment */ 7137 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7138 } 7139 } 7140 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7141 rsm->r_flags |= BBR_ACKED; 7142 if (rsm->r_in_tmap) { 7143 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7144 rsm->r_in_tmap = 0; 7145 } 7146 out: 7147 if (rsm && (rsm->r_flags & BBR_ACKED)) { 7148 /* 7149 * Now can we merge this newly acked 7150 * block with either the previous or 7151 * next block? 7152 */ 7153 nrsm = TAILQ_NEXT(rsm, r_next); 7154 if (nrsm && 7155 (nrsm->r_flags & BBR_ACKED)) { 7156 /* yep this and next can be merged */ 7157 rsm = bbr_merge_rsm(bbr, rsm, nrsm); 7158 } 7159 /* Now what about the previous? */ 7160 nrsm = TAILQ_PREV(rsm, bbr_head, r_next); 7161 if (nrsm && 7162 (nrsm->r_flags & BBR_ACKED)) { 7163 /* yep the previous and this can be merged */ 7164 rsm = bbr_merge_rsm(bbr, nrsm, rsm); 7165 } 7166 } 7167 if (used_ref == 0) { 7168 BBR_STAT_INC(bbr_sack_proc_all); 7169 } else { 7170 BBR_STAT_INC(bbr_sack_proc_short); 7171 } 7172 if (went_fwd && went_back) { 7173 BBR_STAT_INC(bbr_sack_search_both); 7174 } else if (went_fwd) { 7175 BBR_STAT_INC(bbr_sack_search_fwd); 7176 } else if (went_back) { 7177 BBR_STAT_INC(bbr_sack_search_back); 7178 } 7179 /* Save off where the next seq is */ 7180 if (rsm) 7181 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 7182 else 7183 bbr->r_ctl.rc_sacklast = NULL; 7184 *prsm = rsm; 7185 return (changed); 7186 } 7187 7188 static void inline 7189 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack) 7190 { 7191 struct bbr_sendmap *tmap; 7192 7193 BBR_STAT_INC(bbr_reneges_seen); 7194 tmap = NULL; 7195 while (rsm && (rsm->r_flags & BBR_ACKED)) { 7196 /* Its no longer sacked, mark it so */ 7197 uint32_t oflags; 7198 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7199 #ifdef BBR_INVARIANTS 7200 if (rsm->r_in_tmap) { 7201 panic("bbr:%p rsm:%p flags:0x%x in tmap?", 7202 bbr, rsm, rsm->r_flags); 7203 } 7204 #endif 7205 oflags = rsm->r_flags; 7206 if (rsm->r_flags & BBR_MARKED_LOST) { 7207 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7208 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7209 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7210 /* LT sampling also needs adjustment */ 7211 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7212 } 7213 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST); 7214 rsm->r_flags |= BBR_WAS_RENEGED; 7215 rsm->r_flags |= BBR_RXT_CLEARED; 7216 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__); 7217 /* Rebuild it into our tmap */ 7218 if (tmap == NULL) { 7219 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7220 tmap = rsm; 7221 } else { 7222 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext); 7223 tmap = rsm; 7224 } 7225 tmap->r_in_tmap = 1; 7226 /* 7227 * XXXrrs Delivered? Should we do anything here? 7228 * 7229 * Of course we don't on a rxt timeout so maybe its ok that 7230 * we don't? 7231 * 7232 * For now lets not. 7233 */ 7234 rsm = TAILQ_NEXT(rsm, r_next); 7235 } 7236 /* 7237 * Now lets possibly clear the sack filter so we start recognizing 7238 * sacks that cover this area. 7239 */ 7240 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack); 7241 } 7242 7243 static void 7244 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to) 7245 { 7246 struct tcp_bbr *bbr; 7247 struct bbr_sendmap *rsm; 7248 uint32_t cts; 7249 7250 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7251 cts = bbr->r_ctl.rc_rcvtime; 7252 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7253 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) { 7254 if ((rsm->r_end - rsm->r_start) <= 1) { 7255 /* Log out the SYN completely */ 7256 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7257 rsm->r_rtr_bytes = 0; 7258 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7259 if (rsm->r_in_tmap) { 7260 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7261 rsm->r_in_tmap = 0; 7262 } 7263 if (bbr->r_ctl.rc_next == rsm) { 7264 /* scoot along the marker */ 7265 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7266 } 7267 if (to != NULL) 7268 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0); 7269 bbr_free(bbr, rsm); 7270 } else { 7271 /* There is more (Fast open)? strip out SYN. */ 7272 rsm->r_flags &= ~BBR_HAS_SYN; 7273 rsm->r_start++; 7274 } 7275 } 7276 } 7277 7278 /* 7279 * Returns the number of bytes that were 7280 * acknowledged by SACK blocks. 7281 */ 7282 7283 static uint32_t 7284 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, 7285 uint32_t *prev_acked) 7286 { 7287 uint32_t changed, last_seq, entered_recovery = 0; 7288 struct tcp_bbr *bbr; 7289 struct bbr_sendmap *rsm; 7290 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 7291 register uint32_t th_ack; 7292 int32_t i, j, k, new_sb, num_sack_blks = 0; 7293 uint32_t cts, acked, ack_point, sack_changed = 0; 7294 uint32_t p_maxseg, maxseg, p_acked = 0; 7295 7296 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7297 if (tcp_get_flags(th) & TH_RST) { 7298 /* We don't log resets */ 7299 return (0); 7300 } 7301 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7302 cts = bbr->r_ctl.rc_rcvtime; 7303 7304 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7305 changed = 0; 7306 maxseg = tp->t_maxseg - bbr->rc_last_options; 7307 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg); 7308 th_ack = th->th_ack; 7309 if (SEQ_GT(th_ack, tp->snd_una)) { 7310 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__); 7311 bbr->rc_tp->t_acktime = ticks; 7312 } 7313 if (SEQ_LEQ(th_ack, tp->snd_una)) { 7314 /* Only sent here for sack processing */ 7315 goto proc_sack; 7316 } 7317 if (rsm && SEQ_GT(th_ack, rsm->r_start)) { 7318 changed = th_ack - rsm->r_start; 7319 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) { 7320 /* 7321 * For the SYN incoming case we will not have called 7322 * tcp_output for the sending of the SYN, so there will be 7323 * no map. All other cases should probably be a panic. 7324 */ 7325 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) { 7326 /* 7327 * We have a timestamp that can be used to generate 7328 * an initial RTT. 7329 */ 7330 uint32_t ts, now, rtt; 7331 7332 ts = bbr_ts_convert(to->to_tsecr); 7333 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv)); 7334 rtt = now - ts; 7335 if (rtt < 1) 7336 rtt = 1; 7337 bbr_log_type_bbrrttprop(bbr, rtt, 7338 tp->iss, 0, cts, 7339 BBR_RTT_BY_TIMESTAMP, tp->iss, 0); 7340 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 7341 changed = 1; 7342 bbr->r_wanted_output = 1; 7343 goto out; 7344 } 7345 goto proc_sack; 7346 } else if (rsm == NULL) { 7347 goto out; 7348 } 7349 if (changed) { 7350 /* 7351 * The ACK point is advancing to th_ack, we must drop off 7352 * the packets in the rack log and calculate any eligble 7353 * RTT's. 7354 */ 7355 bbr->r_wanted_output = 1; 7356 more: 7357 if (rsm == NULL) { 7358 if (tp->t_flags & TF_SENTFIN) { 7359 /* if we send a FIN we will not hav a map */ 7360 goto proc_sack; 7361 } 7362 #ifdef BBR_INVARIANTS 7363 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n", 7364 tp, 7365 th, tp->t_state, bbr, 7366 tp->snd_una, tp->snd_max, changed); 7367 #endif 7368 goto proc_sack; 7369 } 7370 } 7371 if (SEQ_LT(th_ack, rsm->r_start)) { 7372 /* Huh map is missing this */ 7373 #ifdef BBR_INVARIANTS 7374 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n", 7375 rsm->r_start, 7376 th_ack, tp->t_state, 7377 bbr->r_state, bbr); 7378 panic("th-ack is bad bbr:%p tp:%p", bbr, tp); 7379 #endif 7380 goto proc_sack; 7381 } else if (th_ack == rsm->r_start) { 7382 /* None here to ack */ 7383 goto proc_sack; 7384 } 7385 /* 7386 * Clear the dup ack counter, it will 7387 * either be freed or if there is some 7388 * remaining we need to start it at zero. 7389 */ 7390 rsm->r_dupack = 0; 7391 /* Now do we consume the whole thing? */ 7392 if (SEQ_GEQ(th_ack, rsm->r_end)) { 7393 /* Its all consumed. */ 7394 uint32_t left; 7395 7396 if (rsm->r_flags & BBR_ACKED) { 7397 /* 7398 * It was acked on the scoreboard -- remove it from 7399 * total 7400 */ 7401 p_acked += (rsm->r_end - rsm->r_start); 7402 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7403 if (bbr->r_ctl.rc_sacked == 0) 7404 bbr->r_ctl.rc_sacklast = NULL; 7405 } else { 7406 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack); 7407 if (rsm->r_flags & BBR_MARKED_LOST) { 7408 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7409 } 7410 if (rsm->r_flags & BBR_SACK_PASSED) { 7411 /* 7412 * There are acked segments ACKED on the 7413 * scoreboard further up. We are seeing 7414 * reordering. 7415 */ 7416 BBR_STAT_INC(bbr_reorder_seen); 7417 bbr->r_ctl.rc_reorder_ts = cts; 7418 if (rsm->r_flags & BBR_MARKED_LOST) { 7419 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7420 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7421 /* LT sampling also needs adjustment */ 7422 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7423 } 7424 } 7425 rsm->r_flags &= ~BBR_MARKED_LOST; 7426 } 7427 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7428 rsm->r_rtr_bytes = 0; 7429 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7430 if (rsm->r_in_tmap) { 7431 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7432 rsm->r_in_tmap = 0; 7433 } 7434 if (bbr->r_ctl.rc_next == rsm) { 7435 /* scoot along the marker */ 7436 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7437 } 7438 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7439 /* Adjust the packet counts */ 7440 left = th_ack - rsm->r_end; 7441 /* Free back to zone */ 7442 bbr_free(bbr, rsm); 7443 if (left) { 7444 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7445 goto more; 7446 } 7447 goto proc_sack; 7448 } 7449 if (rsm->r_flags & BBR_ACKED) { 7450 /* 7451 * It was acked on the scoreboard -- remove it from total 7452 * for the part being cum-acked. 7453 */ 7454 p_acked += (rsm->r_end - rsm->r_start); 7455 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 7456 if (bbr->r_ctl.rc_sacked == 0) 7457 bbr->r_ctl.rc_sacklast = NULL; 7458 } else { 7459 /* 7460 * It was acked up to th_ack point for the first time 7461 */ 7462 struct bbr_sendmap lrsm; 7463 7464 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap)); 7465 lrsm.r_end = th_ack; 7466 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack); 7467 } 7468 if ((rsm->r_flags & BBR_MARKED_LOST) && 7469 ((rsm->r_flags & BBR_ACKED) == 0)) { 7470 /* 7471 * It was marked lost and partly ack'd now 7472 * for the first time. We lower the rc_lost_bytes 7473 * and still leave it MARKED. 7474 */ 7475 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start; 7476 } 7477 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7478 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7479 rsm->r_rtr_bytes = 0; 7480 /* adjust packet count */ 7481 rsm->r_start = th_ack; 7482 proc_sack: 7483 /* Check for reneging */ 7484 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7485 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) { 7486 /* 7487 * The peer has moved snd_una up to the edge of this send, 7488 * i.e. one that it had previously acked. The only way that 7489 * can be true if the peer threw away data (space issues) 7490 * that it had previously sacked (else it would have given 7491 * us snd_una up to (rsm->r_end). We need to undo the acked 7492 * markings here. 7493 * 7494 * Note we have to look to make sure th_ack is our 7495 * rsm->r_start in case we get an old ack where th_ack is 7496 * behind snd_una. 7497 */ 7498 bbr_peer_reneges(bbr, rsm, th->th_ack); 7499 } 7500 if ((to->to_flags & TOF_SACK) == 0) { 7501 /* We are done nothing left to log */ 7502 goto out; 7503 } 7504 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7505 if (rsm) { 7506 last_seq = rsm->r_end; 7507 } else { 7508 last_seq = tp->snd_max; 7509 } 7510 /* Sack block processing */ 7511 if (SEQ_GT(th_ack, tp->snd_una)) 7512 ack_point = th_ack; 7513 else 7514 ack_point = tp->snd_una; 7515 for (i = 0; i < to->to_nsacks; i++) { 7516 bcopy((to->to_sacks + i * TCPOLEN_SACK), 7517 &sack, sizeof(sack)); 7518 sack.start = ntohl(sack.start); 7519 sack.end = ntohl(sack.end); 7520 if (SEQ_GT(sack.end, sack.start) && 7521 SEQ_GT(sack.start, ack_point) && 7522 SEQ_LT(sack.start, tp->snd_max) && 7523 SEQ_GT(sack.end, ack_point) && 7524 SEQ_LEQ(sack.end, tp->snd_max)) { 7525 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) && 7526 (SEQ_LT(sack.end, last_seq)) && 7527 ((sack.end - sack.start) < (p_maxseg / 8))) { 7528 /* 7529 * Not the last piece and its smaller than 7530 * 1/8th of a p_maxseg. We ignore this. 7531 */ 7532 BBR_STAT_INC(bbr_runt_sacks); 7533 continue; 7534 } 7535 sack_blocks[num_sack_blks] = sack; 7536 num_sack_blks++; 7537 } else if (SEQ_LEQ(sack.start, th_ack) && 7538 SEQ_LEQ(sack.end, th_ack)) { 7539 /* 7540 * Its a D-SACK block. 7541 */ 7542 tcp_record_dsack(tp, sack.start, sack.end, 0); 7543 } 7544 } 7545 if (num_sack_blks == 0) 7546 goto out; 7547 /* 7548 * Sort the SACK blocks so we can update the rack scoreboard with 7549 * just one pass. 7550 */ 7551 new_sb = sack_filter_blks(tp, &bbr->r_ctl.bbr_sf, sack_blocks, 7552 num_sack_blks, th->th_ack); 7553 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks); 7554 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks); 7555 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb)); 7556 num_sack_blks = new_sb; 7557 if (num_sack_blks < 2) { 7558 goto do_sack_work; 7559 } 7560 /* Sort the sacks */ 7561 for (i = 0; i < num_sack_blks; i++) { 7562 for (j = i + 1; j < num_sack_blks; j++) { 7563 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 7564 sack = sack_blocks[i]; 7565 sack_blocks[i] = sack_blocks[j]; 7566 sack_blocks[j] = sack; 7567 } 7568 } 7569 } 7570 /* 7571 * Now are any of the sack block ends the same (yes some 7572 * implememtations send these)? 7573 */ 7574 again: 7575 if (num_sack_blks > 1) { 7576 for (i = 0; i < num_sack_blks; i++) { 7577 for (j = i + 1; j < num_sack_blks; j++) { 7578 if (sack_blocks[i].end == sack_blocks[j].end) { 7579 /* 7580 * Ok these two have the same end we 7581 * want the smallest end and then 7582 * throw away the larger and start 7583 * again. 7584 */ 7585 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 7586 /* 7587 * The second block covers 7588 * more area use that 7589 */ 7590 sack_blocks[i].start = sack_blocks[j].start; 7591 } 7592 /* 7593 * Now collapse out the dup-sack and 7594 * lower the count 7595 */ 7596 for (k = (j + 1); k < num_sack_blks; k++) { 7597 sack_blocks[j].start = sack_blocks[k].start; 7598 sack_blocks[j].end = sack_blocks[k].end; 7599 j++; 7600 } 7601 num_sack_blks--; 7602 goto again; 7603 } 7604 } 7605 } 7606 } 7607 do_sack_work: 7608 rsm = bbr->r_ctl.rc_sacklast; 7609 for (i = 0; i < num_sack_blks; i++) { 7610 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts); 7611 if (acked) { 7612 bbr->r_wanted_output = 1; 7613 changed += acked; 7614 sack_changed += acked; 7615 } 7616 } 7617 out: 7618 *prev_acked = p_acked; 7619 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 7620 /* 7621 * Ok we have a high probability that we need to go in to 7622 * recovery since we have data sack'd 7623 */ 7624 struct bbr_sendmap *rsm; 7625 7626 rsm = bbr_check_recovery_mode(tp, bbr, cts); 7627 if (rsm) { 7628 /* Enter recovery */ 7629 entered_recovery = 1; 7630 bbr->r_wanted_output = 1; 7631 /* 7632 * When we enter recovery we need to assure we send 7633 * one packet. 7634 */ 7635 if (bbr->r_ctl.rc_resend == NULL) { 7636 bbr->r_ctl.rc_resend = rsm; 7637 } 7638 } 7639 } 7640 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 7641 /* 7642 * See if we need to rack-retransmit anything if so set it 7643 * up as the thing to resend assuming something else is not 7644 * already in that position. 7645 */ 7646 if (bbr->r_ctl.rc_resend == NULL) { 7647 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 7648 } 7649 } 7650 /* 7651 * We return the amount that changed via sack, this is used by the 7652 * ack-received code to augment what was changed between th_ack <-> 7653 * snd_una. 7654 */ 7655 return (sack_changed); 7656 } 7657 7658 static void 7659 bbr_strike_dupack(struct tcp_bbr *bbr) 7660 { 7661 struct bbr_sendmap *rsm; 7662 7663 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 7664 if (rsm && (rsm->r_dupack < 0xff)) { 7665 rsm->r_dupack++; 7666 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) 7667 bbr->r_wanted_output = 1; 7668 } 7669 } 7670 7671 /* 7672 * Return value of 1, we do not need to call bbr_process_data(). 7673 * return value of 0, bbr_process_data can be called. 7674 * For ret_val if its 0 the TCB is locked and valid, if its non-zero 7675 * its unlocked and probably unsafe to touch the TCB. 7676 */ 7677 static int 7678 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 7679 struct tcpcb *tp, struct tcpopt *to, 7680 uint32_t tiwin, int32_t tlen, 7681 int32_t * ofia, int32_t thflags, int32_t * ret_val) 7682 { 7683 int32_t ourfinisacked = 0; 7684 int32_t acked_amount; 7685 uint16_t nsegs; 7686 int32_t acked; 7687 uint32_t lost, sack_changed = 0; 7688 struct mbuf *mfree; 7689 struct tcp_bbr *bbr; 7690 uint32_t prev_acked = 0; 7691 7692 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7693 lost = bbr->r_ctl.rc_lost; 7694 nsegs = max(1, m->m_pkthdr.lro_nsegs); 7695 if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) { 7696 /* Checking SEG.ACK against ISS is definitely redundant. */ 7697 tp->t_flags2 |= TF2_NO_ISS_CHECK; 7698 } 7699 if (!V_tcp_insecure_ack) { 7700 tcp_seq seq_min; 7701 bool ghost_ack_check; 7702 7703 if (tp->t_flags2 & TF2_NO_ISS_CHECK) { 7704 /* Check for too old ACKs (RFC 5961, Section 5.2). */ 7705 seq_min = tp->snd_una - tp->max_sndwnd; 7706 ghost_ack_check = false; 7707 } else { 7708 if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) { 7709 /* Checking for ghost ACKs is stricter. */ 7710 seq_min = tp->iss + 1; 7711 ghost_ack_check = true; 7712 } else { 7713 /* 7714 * Checking for too old ACKs (RFC 5961, 7715 * Section 5.2) is stricter. 7716 */ 7717 seq_min = tp->snd_una - tp->max_sndwnd; 7718 ghost_ack_check = false; 7719 } 7720 } 7721 if (SEQ_LT(th->th_ack, seq_min)) { 7722 if (ghost_ack_check) 7723 TCPSTAT_INC(tcps_rcvghostack); 7724 else 7725 TCPSTAT_INC(tcps_rcvacktooold); 7726 /* Send challenge ACK. */ 7727 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7728 bbr->r_wanted_output = 1; 7729 return (1); 7730 } 7731 } 7732 if (SEQ_GT(th->th_ack, tp->snd_max)) { 7733 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7734 bbr->r_wanted_output = 1; 7735 return (1); 7736 } 7737 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 7738 /* Process the ack */ 7739 if (bbr->rc_in_persist) 7740 tp->t_rxtshift = 0; 7741 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd)) 7742 bbr_strike_dupack(bbr); 7743 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 7744 } 7745 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost)); 7746 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 7747 /* 7748 * Old ack, behind the last one rcv'd or a duplicate ack 7749 * with SACK info. 7750 */ 7751 if (th->th_ack == tp->snd_una) { 7752 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0); 7753 if (bbr->r_state == TCPS_SYN_SENT) { 7754 /* 7755 * Special case on where we sent SYN. When 7756 * the SYN-ACK is processed in syn_sent 7757 * state it bumps the snd_una. This causes 7758 * us to hit here even though we did ack 1 7759 * byte. 7760 * 7761 * Go through the nothing left case so we 7762 * send data. 7763 */ 7764 goto nothing_left; 7765 } 7766 } 7767 return (0); 7768 } 7769 /* 7770 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 7771 * something we sent. 7772 */ 7773 if (tp->t_flags & TF_NEEDSYN) { 7774 /* 7775 * T/TCP: Connection was half-synchronized, and our SYN has 7776 * been ACK'd (so connection is now fully synchronized). Go 7777 * to non-starred state, increment snd_una for ACK of SYN, 7778 * and check if we can do window scaling. 7779 */ 7780 tp->t_flags &= ~TF_NEEDSYN; 7781 tp->snd_una++; 7782 /* Do window scaling? */ 7783 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 7784 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 7785 tp->rcv_scale = tp->request_r_scale; 7786 /* Send window already scaled. */ 7787 } 7788 } 7789 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7790 7791 acked = BYTES_THIS_ACK(tp, th); 7792 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 7793 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 7794 7795 /* 7796 * If we just performed our first retransmit, and the ACK arrives 7797 * within our recovery window, then it was a mistake to do the 7798 * retransmit in the first place. Recover our original cwnd and 7799 * ssthresh, and proceed to transmit where we left off. 7800 */ 7801 if (tp->t_flags & TF_PREVVALID) { 7802 tp->t_flags &= ~TF_PREVVALID; 7803 if (tp->t_rxtshift == 1 && 7804 (int)(ticks - tp->t_badrxtwin) < 0) 7805 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 7806 } 7807 SOCK_SENDBUF_LOCK(so); 7808 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 7809 tp->snd_wnd -= acked_amount; 7810 mfree = sbcut_locked(&so->so_snd, acked_amount); 7811 /* NB: sowwakeup_locked() does an implicit unlock. */ 7812 sowwakeup_locked(so); 7813 m_freem(mfree); 7814 if (SEQ_GT(th->th_ack, tp->snd_una)) { 7815 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 7816 } 7817 tp->snd_una = th->th_ack; 7818 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost)); 7819 if (IN_RECOVERY(tp->t_flags)) { 7820 if (SEQ_LT(th->th_ack, tp->snd_recover) && 7821 (SEQ_LT(th->th_ack, tp->snd_max))) { 7822 tcp_bbr_partialack(tp); 7823 } else { 7824 bbr_post_recovery(tp); 7825 } 7826 } 7827 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 7828 tp->snd_recover = tp->snd_una; 7829 } 7830 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 7831 tp->snd_nxt = tp->snd_max; 7832 } 7833 if (tp->snd_una == tp->snd_max) { 7834 /* Nothing left outstanding */ 7835 nothing_left: 7836 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 7837 if (sbavail(&so->so_snd) == 0) 7838 bbr->rc_tp->t_acktime = 0; 7839 if ((sbused(&so->so_snd) == 0) && 7840 (tp->t_flags & TF_SENTFIN)) { 7841 ourfinisacked = 1; 7842 } 7843 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 7844 if (bbr->rc_in_persist == 0) { 7845 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 7846 } 7847 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 7848 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 7849 /* 7850 * We invalidate the last ack here since we 7851 * don't want to transfer forward the time 7852 * for our sum's calculations. 7853 */ 7854 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7855 (sbavail(&so->so_snd) == 0) && 7856 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 7857 /* 7858 * The socket was gone and the peer sent data, time 7859 * to reset him. 7860 */ 7861 *ret_val = 1; 7862 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 7863 /* tcp_close will kill the inp pre-log the Reset */ 7864 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 7865 tp = tcp_close(tp); 7866 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 7867 BBR_STAT_INC(bbr_dropped_af_data); 7868 return (1); 7869 } 7870 /* Set need output so persist might get set */ 7871 bbr->r_wanted_output = 1; 7872 } 7873 if (ofia) 7874 *ofia = ourfinisacked; 7875 return (0); 7876 } 7877 7878 static void 7879 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7880 { 7881 if (bbr->rc_in_persist == 0) { 7882 bbr_timer_cancel(bbr, __LINE__, cts); 7883 bbr->r_ctl.rc_last_delay_val = 0; 7884 tp->t_rxtshift = 0; 7885 bbr->rc_in_persist = 1; 7886 bbr->r_ctl.rc_went_idle_time = cts; 7887 /* We should be capped when rw went to 0 but just in case */ 7888 bbr_log_type_pesist(bbr, cts, 0, line, 1); 7889 /* Time freezes for the state, so do the accounting now */ 7890 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 7891 uint32_t time_in; 7892 7893 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 7894 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7895 int32_t idx; 7896 7897 idx = bbr_state_val(bbr); 7898 counter_u64_add(bbr_state_time[(idx + 5)], time_in); 7899 } else { 7900 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 7901 } 7902 } 7903 bbr->r_ctl.rc_bbr_state_time = cts; 7904 } 7905 } 7906 7907 static void 7908 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time) 7909 { 7910 /* 7911 * Note that if idle time does not exceed our 7912 * threshold, we do nothing continuing the state 7913 * transitions we were last walking through. 7914 */ 7915 if (idle_time >= bbr_idle_restart_threshold) { 7916 if (bbr->rc_use_idle_restart) { 7917 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT; 7918 /* 7919 * Set our target using BBR_UNIT, so 7920 * we increase at a dramatic rate but 7921 * we stop when we get the pipe 7922 * full again for our current b/w estimate. 7923 */ 7924 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 7925 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 7926 bbr_set_state_target(bbr, __LINE__); 7927 /* Now setup our gains to ramp up */ 7928 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 7929 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 7930 bbr_log_type_statechange(bbr, cts, __LINE__); 7931 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7932 bbr_substate_change(bbr, cts, __LINE__, 1); 7933 } 7934 } 7935 } 7936 7937 static void 7938 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7939 { 7940 uint32_t idle_time; 7941 7942 if (bbr->rc_in_persist == 0) 7943 return; 7944 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time); 7945 bbr->rc_in_persist = 0; 7946 bbr->rc_hit_state_1 = 0; 7947 bbr->r_ctl.rc_del_time = cts; 7948 /* 7949 * We invalidate the last ack here since we 7950 * don't want to transfer forward the time 7951 * for our sum's calculations. 7952 */ 7953 if (tcp_in_hpts(bbr->rc_tp)) { 7954 tcp_hpts_remove(bbr->rc_tp); 7955 bbr->rc_timer_first = 0; 7956 bbr->r_ctl.rc_hpts_flags = 0; 7957 bbr->r_ctl.rc_last_delay_val = 0; 7958 bbr->r_ctl.rc_hptsi_agg_delay = 0; 7959 bbr->r_agg_early_set = 0; 7960 bbr->r_ctl.rc_agg_early = 0; 7961 } 7962 bbr_log_type_pesist(bbr, cts, idle_time, line, 0); 7963 if (idle_time >= bbr_rtt_probe_time) { 7964 /* 7965 * This qualifies as a RTT_PROBE session since we drop the 7966 * data outstanding to nothing and waited more than 7967 * bbr_rtt_probe_time. 7968 */ 7969 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0); 7970 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts; 7971 } 7972 tp->t_rxtshift = 0; 7973 /* 7974 * If in probeBW and we have persisted more than an RTT lets do 7975 * special handling. 7976 */ 7977 /* Force a time based epoch */ 7978 bbr_set_epoch(bbr, cts, __LINE__); 7979 /* 7980 * Setup the lost so we don't count anything against the guy 7981 * we have been stuck with during persists. 7982 */ 7983 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 7984 /* Time un-freezes for the state */ 7985 bbr->r_ctl.rc_bbr_state_time = cts; 7986 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) || 7987 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) { 7988 /* 7989 * If we are going back to probe-bw 7990 * or probe_rtt, we may need to possibly 7991 * do a fast restart. 7992 */ 7993 bbr_restart_after_idle(bbr, cts, idle_time); 7994 } 7995 } 7996 7997 static void 7998 bbr_collapsed_window(struct tcp_bbr *bbr) 7999 { 8000 /* 8001 * Now we must walk the 8002 * send map and divide the 8003 * ones left stranded. These 8004 * guys can't cause us to abort 8005 * the connection and are really 8006 * "unsent". However if a buggy 8007 * client actually did keep some 8008 * of the data i.e. collapsed the win 8009 * and refused to ack and then opened 8010 * the win and acked that data. We would 8011 * get into an ack war, the simplier 8012 * method then of just pretending we 8013 * did not send those segments something 8014 * won't work. 8015 */ 8016 struct bbr_sendmap *rsm, *nrsm; 8017 tcp_seq max_seq; 8018 uint32_t maxseg; 8019 int can_split = 0; 8020 int fnd = 0; 8021 8022 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 8023 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd; 8024 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0); 8025 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 8026 /* Find the first seq past or at maxseq */ 8027 if (rsm->r_flags & BBR_RWND_COLLAPSED) 8028 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8029 if (SEQ_GEQ(max_seq, rsm->r_start) && 8030 SEQ_GEQ(rsm->r_end, max_seq)) { 8031 fnd = 1; 8032 break; 8033 } 8034 } 8035 bbr->rc_has_collapsed = 0; 8036 if (!fnd) { 8037 /* Nothing to do strange */ 8038 return; 8039 } 8040 /* 8041 * Now can we split? 8042 * 8043 * We don't want to split if splitting 8044 * would generate too many small segments 8045 * less we let an attacker fragment our 8046 * send_map and leave us out of memory. 8047 */ 8048 if ((max_seq != rsm->r_start) && 8049 (max_seq != rsm->r_end)){ 8050 /* can we split? */ 8051 int res1, res2; 8052 8053 res1 = max_seq - rsm->r_start; 8054 res2 = rsm->r_end - max_seq; 8055 if ((res1 >= (maxseg/8)) && 8056 (res2 >= (maxseg/8))) { 8057 /* No small pieces here */ 8058 can_split = 1; 8059 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) { 8060 /* We are under the limit */ 8061 can_split = 1; 8062 } 8063 } 8064 /* Ok do we need to split this rsm? */ 8065 if (max_seq == rsm->r_start) { 8066 /* It's this guy no split required */ 8067 nrsm = rsm; 8068 } else if (max_seq == rsm->r_end) { 8069 /* It's the next one no split required. */ 8070 nrsm = TAILQ_NEXT(rsm, r_next); 8071 if (nrsm == NULL) { 8072 /* Huh? */ 8073 return; 8074 } 8075 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) { 8076 /* yep we need to split it */ 8077 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 8078 if (nrsm == NULL) { 8079 /* failed XXXrrs what can we do mark the whole? */ 8080 nrsm = rsm; 8081 goto no_split; 8082 } 8083 /* Clone it */ 8084 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0); 8085 bbr_clone_rsm(bbr, nrsm, rsm, max_seq); 8086 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 8087 if (rsm->r_in_tmap) { 8088 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8089 nrsm->r_in_tmap = 1; 8090 } 8091 } else { 8092 /* 8093 * Split not allowed just start here just 8094 * use this guy. 8095 */ 8096 nrsm = rsm; 8097 } 8098 no_split: 8099 BBR_STAT_INC(bbr_collapsed_win); 8100 /* reuse fnd as a count */ 8101 fnd = 0; 8102 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) { 8103 nrsm->r_flags |= BBR_RWND_COLLAPSED; 8104 fnd++; 8105 bbr->rc_has_collapsed = 1; 8106 } 8107 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd); 8108 } 8109 8110 static void 8111 bbr_un_collapse_window(struct tcp_bbr *bbr) 8112 { 8113 struct bbr_sendmap *rsm; 8114 int cleared = 0; 8115 8116 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 8117 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 8118 /* Clear the flag */ 8119 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8120 cleared++; 8121 } else 8122 break; 8123 } 8124 bbr_log_type_rwnd_collapse(bbr, 8125 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared); 8126 bbr->rc_has_collapsed = 0; 8127 } 8128 8129 /* 8130 * Return value of 1, the TCB is unlocked and most 8131 * likely gone, return value of 0, the TCB is still 8132 * locked. 8133 */ 8134 static int 8135 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 8136 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 8137 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 8138 { 8139 /* 8140 * Update window information. Don't look at window if no ACK: TAC's 8141 * send garbage on first SYN. 8142 */ 8143 uint16_t nsegs; 8144 int32_t tfo_syn; 8145 struct tcp_bbr *bbr; 8146 8147 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8148 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8149 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8150 if ((thflags & TH_ACK) && 8151 (SEQ_LT(tp->snd_wl1, th->th_seq) || 8152 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 8153 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 8154 /* keep track of pure window updates */ 8155 if (tlen == 0 && 8156 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 8157 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 8158 tp->snd_wnd = tiwin; 8159 tp->snd_wl1 = th->th_seq; 8160 tp->snd_wl2 = th->th_ack; 8161 if (tp->snd_wnd > tp->max_sndwnd) 8162 tp->max_sndwnd = tp->snd_wnd; 8163 bbr->r_wanted_output = 1; 8164 } else if (thflags & TH_ACK) { 8165 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 8166 tp->snd_wnd = tiwin; 8167 tp->snd_wl1 = th->th_seq; 8168 tp->snd_wl2 = th->th_ack; 8169 } 8170 } 8171 if (tp->snd_wnd < ctf_outstanding(tp)) 8172 /* The peer collapsed its window on us */ 8173 bbr_collapsed_window(bbr); 8174 else if (bbr->rc_has_collapsed) 8175 bbr_un_collapse_window(bbr); 8176 /* Was persist timer active and now we have window space? */ 8177 if ((bbr->rc_in_persist != 0) && 8178 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8179 bbr_minseg(bbr)))) { 8180 /* 8181 * Make the rate persist at end of persist mode if idle long 8182 * enough 8183 */ 8184 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8185 8186 /* Make sure we output to start the timer */ 8187 bbr->r_wanted_output = 1; 8188 } 8189 /* Do we need to enter persist? */ 8190 if ((bbr->rc_in_persist == 0) && 8191 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8192 TCPS_HAVEESTABLISHED(tp->t_state) && 8193 (tp->snd_max == tp->snd_una) && 8194 sbavail(&so->so_snd) && 8195 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8196 /* No send window.. we must enter persist */ 8197 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8198 } 8199 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 8200 m_freem(m); 8201 return (0); 8202 } 8203 /* 8204 * We don't support urgent data but 8205 * drag along the up just to make sure 8206 * if there is a stack switch no one 8207 * is surprised. 8208 */ 8209 tp->rcv_up = tp->rcv_nxt; 8210 8211 /* 8212 * Process the segment text, merging it into the TCP sequencing 8213 * queue, and arranging for acknowledgment of receipt if necessary. 8214 * This process logically involves adjusting tp->rcv_wnd as data is 8215 * presented to the user (this happens in tcp_usrreq.c, case 8216 * PRU_RCVD). If a FIN has already been received on this connection 8217 * then we just ignore the text. 8218 */ 8219 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 8220 (tp->t_flags & TF_FASTOPEN)); 8221 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 8222 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8223 tcp_seq save_start = th->th_seq; 8224 tcp_seq save_rnxt = tp->rcv_nxt; 8225 int save_tlen = tlen; 8226 8227 m_adj(m, drop_hdrlen); /* delayed header drop */ 8228 /* 8229 * Insert segment which includes th into TCP reassembly 8230 * queue with control block tp. Set thflags to whether 8231 * reassembly now includes a segment with FIN. This handles 8232 * the common case inline (segment is the next to be 8233 * received on an established connection, and the queue is 8234 * empty), avoiding linkage into and removal from the queue 8235 * and repetition of various conversions. Set DELACK for 8236 * segments received in order, but ack immediately when 8237 * segments are out of order (so fast retransmit can work). 8238 */ 8239 if (th->th_seq == tp->rcv_nxt && 8240 SEGQ_EMPTY(tp) && 8241 (TCPS_HAVEESTABLISHED(tp->t_state) || 8242 tfo_syn)) { 8243 #ifdef NETFLIX_SB_LIMITS 8244 u_int mcnt, appended; 8245 8246 if (so->so_rcv.sb_shlim) { 8247 mcnt = m_memcnt(m); 8248 appended = 0; 8249 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8250 CFO_NOSLEEP, NULL) == false) { 8251 counter_u64_add(tcp_sb_shlim_fails, 1); 8252 m_freem(m); 8253 return (0); 8254 } 8255 } 8256 8257 #endif 8258 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) { 8259 bbr->bbr_segs_rcvd += max(1, nsegs); 8260 tp->t_flags |= TF_DELACK; 8261 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8262 } else { 8263 bbr->r_wanted_output = 1; 8264 tp->t_flags |= TF_ACKNOW; 8265 } 8266 tp->rcv_nxt += tlen; 8267 if (tlen && 8268 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8269 (tp->t_fbyte_in == 0)) { 8270 tp->t_fbyte_in = ticks; 8271 if (tp->t_fbyte_in == 0) 8272 tp->t_fbyte_in = 1; 8273 if (tp->t_fbyte_out && tp->t_fbyte_in) 8274 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8275 } 8276 thflags = tcp_get_flags(th) & TH_FIN; 8277 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8278 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8279 SOCK_RECVBUF_LOCK(so); 8280 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 8281 m_freem(m); 8282 else 8283 #ifdef NETFLIX_SB_LIMITS 8284 appended = 8285 #endif 8286 sbappendstream_locked(&so->so_rcv, m, 0); 8287 /* NB: sorwakeup_locked() does an implicit unlock. */ 8288 sorwakeup_locked(so); 8289 #ifdef NETFLIX_SB_LIMITS 8290 if (so->so_rcv.sb_shlim && appended != mcnt) 8291 counter_fo_release(so->so_rcv.sb_shlim, 8292 mcnt - appended); 8293 #endif 8294 8295 } else { 8296 /* 8297 * XXX: Due to the header drop above "th" is 8298 * theoretically invalid by now. Fortunately 8299 * m_adj() doesn't actually frees any mbufs when 8300 * trimming from the head. 8301 */ 8302 tcp_seq temp = save_start; 8303 8304 thflags = tcp_reass(tp, th, &temp, &tlen, m); 8305 tp->t_flags |= TF_ACKNOW; 8306 if (tp->t_flags & TF_WAKESOR) { 8307 tp->t_flags &= ~TF_WAKESOR; 8308 /* NB: sorwakeup_locked() does an implicit unlock. */ 8309 sorwakeup_locked(so); 8310 } 8311 } 8312 if ((tp->t_flags & TF_SACK_PERMIT) && 8313 (save_tlen > 0) && 8314 TCPS_HAVEESTABLISHED(tp->t_state)) { 8315 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 8316 /* 8317 * DSACK actually handled in the fastpath 8318 * above. 8319 */ 8320 tcp_update_sack_list(tp, save_start, 8321 save_start + save_tlen); 8322 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 8323 if ((tp->rcv_numsacks >= 1) && 8324 (tp->sackblks[0].end == save_start)) { 8325 /* 8326 * Partial overlap, recorded at todrop 8327 * above. 8328 */ 8329 tcp_update_sack_list(tp, 8330 tp->sackblks[0].start, 8331 tp->sackblks[0].end); 8332 } else { 8333 tcp_update_dsack_list(tp, save_start, 8334 save_start + save_tlen); 8335 } 8336 } else if (tlen >= save_tlen) { 8337 /* Update of sackblks. */ 8338 tcp_update_dsack_list(tp, save_start, 8339 save_start + save_tlen); 8340 } else if (tlen > 0) { 8341 tcp_update_dsack_list(tp, save_start, 8342 save_start + tlen); 8343 } 8344 } 8345 } else { 8346 m_freem(m); 8347 thflags &= ~TH_FIN; 8348 } 8349 8350 /* 8351 * If FIN is received ACK the FIN and let the user know that the 8352 * connection is closing. 8353 */ 8354 if (thflags & TH_FIN) { 8355 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8356 /* The socket upcall is handled by socantrcvmore. */ 8357 socantrcvmore(so); 8358 /* 8359 * If connection is half-synchronized (ie NEEDSYN 8360 * flag on) then delay ACK, so it may be piggybacked 8361 * when SYN is sent. Otherwise, since we received a 8362 * FIN then no more input can be expected, send ACK 8363 * now. 8364 */ 8365 if (tp->t_flags & TF_NEEDSYN) { 8366 tp->t_flags |= TF_DELACK; 8367 bbr_timer_cancel(bbr, 8368 __LINE__, bbr->r_ctl.rc_rcvtime); 8369 } else { 8370 tp->t_flags |= TF_ACKNOW; 8371 } 8372 tp->rcv_nxt++; 8373 } 8374 switch (tp->t_state) { 8375 /* 8376 * In SYN_RECEIVED and ESTABLISHED STATES enter the 8377 * CLOSE_WAIT state. 8378 */ 8379 case TCPS_SYN_RECEIVED: 8380 tp->t_starttime = ticks; 8381 /* FALLTHROUGH */ 8382 case TCPS_ESTABLISHED: 8383 tcp_state_change(tp, TCPS_CLOSE_WAIT); 8384 break; 8385 8386 /* 8387 * If still in FIN_WAIT_1 STATE FIN has not been 8388 * acked so enter the CLOSING state. 8389 */ 8390 case TCPS_FIN_WAIT_1: 8391 tcp_state_change(tp, TCPS_CLOSING); 8392 break; 8393 8394 /* 8395 * In FIN_WAIT_2 state enter the TIME_WAIT state, 8396 * starting the time-wait timer, turning off the 8397 * other standard timers. 8398 */ 8399 case TCPS_FIN_WAIT_2: 8400 bbr->rc_timer_first = 1; 8401 bbr_timer_cancel(bbr, 8402 __LINE__, bbr->r_ctl.rc_rcvtime); 8403 tcp_twstart(tp); 8404 return (1); 8405 } 8406 } 8407 /* 8408 * Return any desired output. 8409 */ 8410 if ((tp->t_flags & TF_ACKNOW) || 8411 (sbavail(&so->so_snd) > ctf_outstanding(tp))) { 8412 bbr->r_wanted_output = 1; 8413 } 8414 return (0); 8415 } 8416 8417 /* 8418 * Here nothing is really faster, its just that we 8419 * have broken out the fast-data path also just like 8420 * the fast-ack. Return 1 if we processed the packet 8421 * return 0 if you need to take the "slow-path". 8422 */ 8423 static int 8424 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 8425 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8426 uint32_t tiwin, int32_t nxt_pkt) 8427 { 8428 uint16_t nsegs; 8429 int32_t newsize = 0; /* automatic sockbuf scaling */ 8430 struct tcp_bbr *bbr; 8431 #ifdef NETFLIX_SB_LIMITS 8432 u_int mcnt, appended; 8433 #endif 8434 8435 /* On the hpts and we would have called output */ 8436 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8437 8438 /* 8439 * If last ACK falls within this segment's sequence numbers, record 8440 * the timestamp. NOTE that the test is modified according to the 8441 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8442 */ 8443 if (bbr->r_ctl.rc_resend != NULL) { 8444 return (0); 8445 } 8446 if (tiwin && tiwin != tp->snd_wnd) { 8447 return (0); 8448 } 8449 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 8450 return (0); 8451 } 8452 if (__predict_false((to->to_flags & TOF_TS) && 8453 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 8454 return (0); 8455 } 8456 if (__predict_false((th->th_ack != tp->snd_una))) { 8457 return (0); 8458 } 8459 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 8460 return (0); 8461 } 8462 if ((to->to_flags & TOF_TS) != 0 && 8463 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8464 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 8465 tp->ts_recent = to->to_tsval; 8466 } 8467 /* 8468 * This is a pure, in-sequence data packet with nothing on the 8469 * reassembly queue and we have enough buffer space to take it. 8470 */ 8471 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8472 8473 #ifdef NETFLIX_SB_LIMITS 8474 if (so->so_rcv.sb_shlim) { 8475 mcnt = m_memcnt(m); 8476 appended = 0; 8477 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8478 CFO_NOSLEEP, NULL) == false) { 8479 counter_u64_add(tcp_sb_shlim_fails, 1); 8480 m_freem(m); 8481 return (1); 8482 } 8483 } 8484 #endif 8485 /* Clean receiver SACK report if present */ 8486 if (tp->rcv_numsacks) 8487 tcp_clean_sackreport(tp); 8488 KMOD_TCPSTAT_INC(tcps_preddat); 8489 tp->rcv_nxt += tlen; 8490 if (tlen && 8491 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8492 (tp->t_fbyte_in == 0)) { 8493 tp->t_fbyte_in = ticks; 8494 if (tp->t_fbyte_in == 0) 8495 tp->t_fbyte_in = 1; 8496 if (tp->t_fbyte_out && tp->t_fbyte_in) 8497 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8498 } 8499 /* 8500 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 8501 */ 8502 tp->snd_wl1 = th->th_seq; 8503 /* 8504 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 8505 */ 8506 tp->rcv_up = tp->rcv_nxt; 8507 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8508 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8509 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 8510 8511 /* Add data to socket buffer. */ 8512 SOCK_RECVBUF_LOCK(so); 8513 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8514 m_freem(m); 8515 } else { 8516 /* 8517 * Set new socket buffer size. Give up when limit is 8518 * reached. 8519 */ 8520 if (newsize) 8521 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 8522 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 8523 m_adj(m, drop_hdrlen); /* delayed header drop */ 8524 8525 #ifdef NETFLIX_SB_LIMITS 8526 appended = 8527 #endif 8528 sbappendstream_locked(&so->so_rcv, m, 0); 8529 ctf_calc_rwin(so, tp); 8530 } 8531 /* NB: sorwakeup_locked() does an implicit unlock. */ 8532 sorwakeup_locked(so); 8533 #ifdef NETFLIX_SB_LIMITS 8534 if (so->so_rcv.sb_shlim && mcnt != appended) 8535 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 8536 #endif 8537 if (DELAY_ACK(tp, bbr, nsegs)) { 8538 bbr->bbr_segs_rcvd += max(1, nsegs); 8539 tp->t_flags |= TF_DELACK; 8540 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8541 } else { 8542 bbr->r_wanted_output = 1; 8543 tp->t_flags |= TF_ACKNOW; 8544 } 8545 return (1); 8546 } 8547 8548 /* 8549 * This subfunction is used to try to highly optimize the 8550 * fast path. We again allow window updates that are 8551 * in sequence to remain in the fast-path. We also add 8552 * in the __predict's to attempt to help the compiler. 8553 * Note that if we return a 0, then we can *not* process 8554 * it and the caller should push the packet into the 8555 * slow-path. If we return 1, then all is well and 8556 * the packet is fully processed. 8557 */ 8558 static int 8559 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 8560 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8561 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 8562 { 8563 int32_t acked; 8564 uint16_t nsegs; 8565 uint32_t sack_changed; 8566 uint32_t prev_acked = 0; 8567 struct tcp_bbr *bbr; 8568 8569 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 8570 /* Old ack, behind (or duplicate to) the last one rcv'd */ 8571 return (0); 8572 } 8573 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 8574 /* Above what we have sent? */ 8575 return (0); 8576 } 8577 if (__predict_false(tiwin == 0)) { 8578 /* zero window */ 8579 return (0); 8580 } 8581 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 8582 /* We need a SYN or a FIN, unlikely.. */ 8583 return (0); 8584 } 8585 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 8586 /* Timestamp is behind .. old ack with seq wrap? */ 8587 return (0); 8588 } 8589 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 8590 /* Still recovering */ 8591 return (0); 8592 } 8593 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8594 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) { 8595 /* We are retransmitting */ 8596 return (0); 8597 } 8598 if (__predict_false(bbr->rc_in_persist != 0)) { 8599 /* In persist mode */ 8600 return (0); 8601 } 8602 if (bbr->r_ctl.rc_sacked) { 8603 /* We have sack holes on our scoreboard */ 8604 return (0); 8605 } 8606 /* Ok if we reach here, we can process a fast-ack */ 8607 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8608 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 8609 /* 8610 * We never detect loss in fast ack [we can't 8611 * have a sack and can't be in recovery so 8612 * we always pass 0 (nothing detected)]. 8613 */ 8614 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0); 8615 /* Did the window get updated? */ 8616 if (tiwin != tp->snd_wnd) { 8617 tp->snd_wnd = tiwin; 8618 tp->snd_wl1 = th->th_seq; 8619 if (tp->snd_wnd > tp->max_sndwnd) 8620 tp->max_sndwnd = tp->snd_wnd; 8621 } 8622 /* Do we need to exit persists? */ 8623 if ((bbr->rc_in_persist != 0) && 8624 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8625 bbr_minseg(bbr)))) { 8626 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8627 bbr->r_wanted_output = 1; 8628 } 8629 /* Do we need to enter persists? */ 8630 if ((bbr->rc_in_persist == 0) && 8631 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8632 TCPS_HAVEESTABLISHED(tp->t_state) && 8633 (tp->snd_max == tp->snd_una) && 8634 sbavail(&so->so_snd) && 8635 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8636 /* No send window.. we must enter persist */ 8637 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8638 } 8639 /* 8640 * If last ACK falls within this segment's sequence numbers, record 8641 * the timestamp. NOTE that the test is modified according to the 8642 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8643 */ 8644 if ((to->to_flags & TOF_TS) != 0 && 8645 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8646 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime; 8647 tp->ts_recent = to->to_tsval; 8648 } 8649 /* 8650 * This is a pure ack for outstanding data. 8651 */ 8652 KMOD_TCPSTAT_INC(tcps_predack); 8653 8654 /* 8655 * "bad retransmit" recovery. 8656 */ 8657 if (tp->t_flags & TF_PREVVALID) { 8658 tp->t_flags &= ~TF_PREVVALID; 8659 if (tp->t_rxtshift == 1 && 8660 (int)(ticks - tp->t_badrxtwin) < 0) 8661 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 8662 } 8663 /* 8664 * Recalculate the transmit timer / rtt. 8665 * 8666 * Some boxes send broken timestamp replies during the SYN+ACK 8667 * phase, ignore timestamps of 0 or we could calculate a huge RTT 8668 * and blow up the retransmit timer. 8669 */ 8670 acked = BYTES_THIS_ACK(tp, th); 8671 8672 #ifdef TCP_HHOOK 8673 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 8674 hhook_run_tcp_est_in(tp, th, to); 8675 #endif 8676 8677 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 8678 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 8679 sbdrop(&so->so_snd, acked); 8680 8681 if (SEQ_GT(th->th_ack, tp->snd_una)) 8682 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 8683 tp->snd_una = th->th_ack; 8684 if (tp->snd_wnd < ctf_outstanding(tp)) 8685 /* The peer collapsed its window on us */ 8686 bbr_collapsed_window(bbr); 8687 else if (bbr->rc_has_collapsed) 8688 bbr_un_collapse_window(bbr); 8689 8690 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 8691 tp->snd_recover = tp->snd_una; 8692 } 8693 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0); 8694 /* 8695 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 8696 */ 8697 tp->snd_wl2 = th->th_ack; 8698 m_freem(m); 8699 /* 8700 * If all outstanding data are acked, stop retransmit timer, 8701 * otherwise restart timer using current (possibly backed-off) 8702 * value. If process is waiting for space, wakeup/selwakeup/signal. 8703 * If data are ready to send, let tcp_output decide between more 8704 * output or persist. 8705 * Wake up the socket if we have room to write more. 8706 */ 8707 sowwakeup(so); 8708 if (tp->snd_una == tp->snd_max) { 8709 /* Nothing left outstanding */ 8710 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 8711 if (sbavail(&so->so_snd) == 0) 8712 bbr->rc_tp->t_acktime = 0; 8713 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8714 if (bbr->rc_in_persist == 0) { 8715 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 8716 } 8717 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 8718 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 8719 /* 8720 * We invalidate the last ack here since we 8721 * don't want to transfer forward the time 8722 * for our sum's calculations. 8723 */ 8724 bbr->r_wanted_output = 1; 8725 } 8726 if (sbavail(&so->so_snd)) { 8727 bbr->r_wanted_output = 1; 8728 } 8729 return (1); 8730 } 8731 8732 /* 8733 * Return value of 1, the TCB is unlocked and most 8734 * likely gone, return value of 0, the TCB is still 8735 * locked. 8736 */ 8737 static int 8738 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 8739 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8740 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8741 { 8742 int32_t todrop; 8743 int32_t ourfinisacked = 0; 8744 struct tcp_bbr *bbr; 8745 int32_t ret_val = 0; 8746 8747 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8748 8749 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8750 ctf_calc_rwin(so, tp); 8751 /* 8752 * If the state is SYN_SENT: if seg contains an ACK, but not for our 8753 * SYN, drop the input. if seg contains a RST, then drop the 8754 * connection. if seg does not contain SYN, then drop it. Otherwise 8755 * this is an acceptable SYN segment initialize tp->rcv_nxt and 8756 * tp->irs if seg contains ack then advance tp->snd_una. BRR does 8757 * not support ECN so we will not say we are capable. if SYN has 8758 * been acked change to ESTABLISHED else SYN_RCVD state arrange for 8759 * segment to be acked (eventually) continue processing rest of 8760 * data/controls, beginning with URG 8761 */ 8762 if ((thflags & TH_ACK) && 8763 (SEQ_LEQ(th->th_ack, tp->iss) || 8764 SEQ_GT(th->th_ack, tp->snd_max))) { 8765 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8766 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8767 return (1); 8768 } 8769 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 8770 TCP_PROBE5(connect__refused, NULL, tp, 8771 mtod(m, const char *), tp, th); 8772 tp = tcp_drop(tp, ECONNREFUSED); 8773 ctf_do_drop(m, tp); 8774 return (1); 8775 } 8776 if (thflags & TH_RST) { 8777 ctf_do_drop(m, tp); 8778 return (1); 8779 } 8780 if (!(thflags & TH_SYN)) { 8781 ctf_do_drop(m, tp); 8782 return (1); 8783 } 8784 tp->irs = th->th_seq; 8785 tcp_rcvseqinit(tp); 8786 if (thflags & TH_ACK) { 8787 int tfo_partial = 0; 8788 8789 KMOD_TCPSTAT_INC(tcps_connects); 8790 soisconnected(so); 8791 #ifdef MAC 8792 mac_socketpeer_set_from_mbuf(m, so); 8793 #endif 8794 /* Do window scaling on this connection? */ 8795 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 8796 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 8797 tp->rcv_scale = tp->request_r_scale; 8798 } 8799 tp->rcv_adv += min(tp->rcv_wnd, 8800 TCP_MAXWIN << tp->rcv_scale); 8801 /* 8802 * If not all the data that was sent in the TFO SYN 8803 * has been acked, resend the remainder right away. 8804 */ 8805 if ((tp->t_flags & TF_FASTOPEN) && 8806 (tp->snd_una != tp->snd_max)) { 8807 tp->snd_nxt = th->th_ack; 8808 tfo_partial = 1; 8809 } 8810 /* 8811 * If there's data, delay ACK; if there's also a FIN ACKNOW 8812 * will be turned on later. 8813 */ 8814 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) { 8815 bbr->bbr_segs_rcvd += 1; 8816 tp->t_flags |= TF_DELACK; 8817 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8818 } else { 8819 bbr->r_wanted_output = 1; 8820 tp->t_flags |= TF_ACKNOW; 8821 } 8822 if (SEQ_GT(th->th_ack, tp->iss)) { 8823 /* 8824 * The SYN is acked 8825 * handle it specially. 8826 */ 8827 bbr_log_syn(tp, to); 8828 } 8829 if (SEQ_GT(th->th_ack, tp->snd_una)) { 8830 /* 8831 * We advance snd_una for the 8832 * fast open case. If th_ack is 8833 * acknowledging data beyond 8834 * snd_una we can't just call 8835 * ack-processing since the 8836 * data stream in our send-map 8837 * will start at snd_una + 1 (one 8838 * beyond the SYN). If its just 8839 * equal we don't need to do that 8840 * and there is no send_map. 8841 */ 8842 tp->snd_una++; 8843 } 8844 /* 8845 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 8846 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 8847 */ 8848 tp->t_starttime = ticks; 8849 if (tp->t_flags & TF_NEEDFIN) { 8850 tcp_state_change(tp, TCPS_FIN_WAIT_1); 8851 tp->t_flags &= ~TF_NEEDFIN; 8852 thflags &= ~TH_SYN; 8853 } else { 8854 tcp_state_change(tp, TCPS_ESTABLISHED); 8855 TCP_PROBE5(connect__established, NULL, tp, 8856 mtod(m, const char *), tp, th); 8857 cc_conn_init(tp); 8858 } 8859 } else { 8860 /* 8861 * Received initial SYN in SYN-SENT[*] state => simultaneous 8862 * open. If segment contains CC option and there is a 8863 * cached CC, apply TAO test. If it succeeds, connection is * 8864 * half-synchronized. Otherwise, do 3-way handshake: 8865 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 8866 * there was no CC option, clear cached CC value. 8867 */ 8868 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 8869 tcp_state_change(tp, TCPS_SYN_RECEIVED); 8870 } 8871 /* 8872 * Advance th->th_seq to correspond to first data byte. If data, 8873 * trim to stay within window, dropping FIN if necessary. 8874 */ 8875 th->th_seq++; 8876 if (tlen > tp->rcv_wnd) { 8877 todrop = tlen - tp->rcv_wnd; 8878 m_adj(m, -todrop); 8879 tlen = tp->rcv_wnd; 8880 thflags &= ~TH_FIN; 8881 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 8882 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 8883 } 8884 tp->snd_wl1 = th->th_seq - 1; 8885 tp->rcv_up = th->th_seq; 8886 /* 8887 * Client side of transaction: already sent SYN and data. If the 8888 * remote host used T/TCP to validate the SYN, our data will be 8889 * ACK'd; if so, enter normal data segment processing in the middle 8890 * of step 5, ack processing. Otherwise, goto step 6. 8891 */ 8892 if (thflags & TH_ACK) { 8893 if ((to->to_flags & TOF_TS) != 0) { 8894 uint32_t t, rtt; 8895 8896 t = tcp_tv_to_mssectick(&bbr->rc_tv); 8897 if (TSTMP_GEQ(t, to->to_tsecr)) { 8898 rtt = t - to->to_tsecr; 8899 if (rtt == 0) { 8900 rtt = 1; 8901 } 8902 rtt *= MS_IN_USEC; 8903 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 8904 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, 8905 rtt, bbr->r_ctl.rc_rcvtime); 8906 } 8907 } 8908 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 8909 return (ret_val); 8910 /* We may have changed to FIN_WAIT_1 above */ 8911 if (tp->t_state == TCPS_FIN_WAIT_1) { 8912 /* 8913 * In FIN_WAIT_1 STATE in addition to the processing 8914 * for the ESTABLISHED state if our FIN is now 8915 * acknowledged then enter FIN_WAIT_2. 8916 */ 8917 if (ourfinisacked) { 8918 /* 8919 * If we can't receive any more data, then 8920 * closing user can proceed. Starting the 8921 * timer is contrary to the specification, 8922 * but if we don't get a FIN we'll hang 8923 * forever. 8924 * 8925 * XXXjl: we should release the tp also, and 8926 * use a compressed state. 8927 */ 8928 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8929 soisdisconnected(so); 8930 tcp_timer_activate(tp, TT_2MSL, 8931 (tcp_fast_finwait2_recycle ? 8932 tcp_finwait2_timeout : 8933 TP_MAXIDLE(tp))); 8934 } 8935 tcp_state_change(tp, TCPS_FIN_WAIT_2); 8936 } 8937 } 8938 } 8939 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 8940 tiwin, thflags, nxt_pkt)); 8941 } 8942 8943 /* 8944 * Return value of 1, the TCB is unlocked and most 8945 * likely gone, return value of 0, the TCB is still 8946 * locked. 8947 */ 8948 static int 8949 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 8950 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8951 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8952 { 8953 int32_t ourfinisacked = 0; 8954 int32_t ret_val; 8955 struct tcp_bbr *bbr; 8956 8957 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8958 8959 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8960 ctf_calc_rwin(so, tp); 8961 if ((thflags & TH_RST) || 8962 (tp->t_fin_is_rst && (thflags & TH_FIN))) 8963 return (ctf_process_rst(m, th, so, tp)); 8964 if ((thflags & TH_ACK) && 8965 (SEQ_LEQ(th->th_ack, tp->snd_una) || 8966 SEQ_GT(th->th_ack, tp->snd_max))) { 8967 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8968 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8969 return (1); 8970 } 8971 if (tp->t_flags & TF_FASTOPEN) { 8972 /* 8973 * When a TFO connection is in SYN_RECEIVED, the only valid 8974 * packets are the initial SYN, a retransmit/copy of the 8975 * initial SYN (possibly with a subset of the original 8976 * data), a valid ACK, a FIN, or a RST. 8977 */ 8978 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 8979 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8980 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8981 return (1); 8982 } else if (thflags & TH_SYN) { 8983 /* non-initial SYN is ignored */ 8984 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 8985 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 8986 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 8987 ctf_do_drop(m, NULL); 8988 return (0); 8989 } 8990 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 8991 ctf_do_drop(m, NULL); 8992 return (0); 8993 } 8994 } 8995 /* 8996 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 8997 * it's less than ts_recent, drop it. 8998 */ 8999 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9000 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9001 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9002 return (ret_val); 9003 } 9004 /* 9005 * In the SYN-RECEIVED state, validate that the packet belongs to 9006 * this connection before trimming the data to fit the receive 9007 * window. Check the sequence number versus IRS since we know the 9008 * sequence numbers haven't wrapped. This is a partial fix for the 9009 * "LAND" DoS attack. 9010 */ 9011 if (SEQ_LT(th->th_seq, tp->irs)) { 9012 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 9013 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9014 return (1); 9015 } 9016 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9017 return (ret_val); 9018 } 9019 /* 9020 * If last ACK falls within this segment's sequence numbers, record 9021 * its timestamp. NOTE: 1) That the test incorporates suggestions 9022 * from the latest proposal of the tcplw@cray.com list (Braden 9023 * 1993/04/26). 2) That updating only on newer timestamps interferes 9024 * with our earlier PAWS tests, so this check should be solely 9025 * predicated on the sequence space of this segment. 3) That we 9026 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9027 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9028 * SEG.Len, This modified check allows us to overcome RFC1323's 9029 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9030 * p.869. In such cases, we can still calculate the RTT correctly 9031 * when RCV.NXT == Last.ACK.Sent. 9032 */ 9033 if ((to->to_flags & TOF_TS) != 0 && 9034 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9035 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9036 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9037 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9038 tp->ts_recent = to->to_tsval; 9039 } 9040 tp->snd_wnd = tiwin; 9041 /* 9042 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9043 * is on (half-synchronized state), then queue data for later 9044 * processing; else drop segment and return. 9045 */ 9046 if ((thflags & TH_ACK) == 0) { 9047 if (tp->t_flags & TF_FASTOPEN) { 9048 cc_conn_init(tp); 9049 } 9050 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9051 tiwin, thflags, nxt_pkt)); 9052 } 9053 KMOD_TCPSTAT_INC(tcps_connects); 9054 if (tp->t_flags & TF_SONOTCONN) { 9055 tp->t_flags &= ~TF_SONOTCONN; 9056 soisconnected(so); 9057 } 9058 /* Do window scaling? */ 9059 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 9060 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 9061 tp->rcv_scale = tp->request_r_scale; 9062 } 9063 /* 9064 * ok for the first time in lets see if we can use the ts to figure 9065 * out what the initial RTT was. 9066 */ 9067 if ((to->to_flags & TOF_TS) != 0) { 9068 uint32_t t, rtt; 9069 9070 t = tcp_tv_to_mssectick(&bbr->rc_tv); 9071 if (TSTMP_GEQ(t, to->to_tsecr)) { 9072 rtt = t - to->to_tsecr; 9073 if (rtt == 0) { 9074 rtt = 1; 9075 } 9076 rtt *= MS_IN_USEC; 9077 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 9078 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime); 9079 } 9080 } 9081 /* Drop off any SYN in the send map (probably not there) */ 9082 if (thflags & TH_ACK) 9083 bbr_log_syn(tp, to); 9084 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 9085 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 9086 tp->t_tfo_pending = NULL; 9087 } 9088 /* 9089 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 9090 * FIN-WAIT-1 9091 */ 9092 tp->t_starttime = ticks; 9093 if (tp->t_flags & TF_NEEDFIN) { 9094 tcp_state_change(tp, TCPS_FIN_WAIT_1); 9095 tp->t_flags &= ~TF_NEEDFIN; 9096 } else { 9097 tcp_state_change(tp, TCPS_ESTABLISHED); 9098 TCP_PROBE5(accept__established, NULL, tp, 9099 mtod(m, const char *), tp, th); 9100 /* 9101 * TFO connections call cc_conn_init() during SYN 9102 * processing. Calling it again here for such connections 9103 * is not harmless as it would undo the snd_cwnd reduction 9104 * that occurs when a TFO SYN|ACK is retransmitted. 9105 */ 9106 if (!(tp->t_flags & TF_FASTOPEN)) 9107 cc_conn_init(tp); 9108 } 9109 /* 9110 * Account for the ACK of our SYN prior to 9111 * regular ACK processing below, except for 9112 * simultaneous SYN, which is handled later. 9113 */ 9114 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 9115 tp->snd_una++; 9116 /* 9117 * If segment contains data or ACK, will call tcp_reass() later; if 9118 * not, do so now to pass queued data to user. 9119 */ 9120 if (tlen == 0 && (thflags & TH_FIN) == 0) { 9121 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 9122 (struct mbuf *)0); 9123 if (tp->t_flags & TF_WAKESOR) { 9124 tp->t_flags &= ~TF_WAKESOR; 9125 /* NB: sorwakeup_locked() does an implicit unlock. */ 9126 sorwakeup_locked(so); 9127 } 9128 } 9129 tp->snd_wl1 = th->th_seq - 1; 9130 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9131 return (ret_val); 9132 } 9133 if (tp->t_state == TCPS_FIN_WAIT_1) { 9134 /* We could have went to FIN_WAIT_1 (or EST) above */ 9135 /* 9136 * In FIN_WAIT_1 STATE in addition to the processing for the 9137 * ESTABLISHED state if our FIN is now acknowledged then 9138 * enter FIN_WAIT_2. 9139 */ 9140 if (ourfinisacked) { 9141 /* 9142 * If we can't receive any more data, then closing 9143 * user can proceed. Starting the timer is contrary 9144 * to the specification, but if we don't get a FIN 9145 * we'll hang forever. 9146 * 9147 * XXXjl: we should release the tp also, and use a 9148 * compressed state. 9149 */ 9150 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9151 soisdisconnected(so); 9152 tcp_timer_activate(tp, TT_2MSL, 9153 (tcp_fast_finwait2_recycle ? 9154 tcp_finwait2_timeout : 9155 TP_MAXIDLE(tp))); 9156 } 9157 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9158 } 9159 } 9160 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9161 tiwin, thflags, nxt_pkt)); 9162 } 9163 9164 /* 9165 * Return value of 1, the TCB is unlocked and most 9166 * likely gone, return value of 0, the TCB is still 9167 * locked. 9168 */ 9169 static int 9170 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 9171 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9172 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9173 { 9174 struct tcp_bbr *bbr; 9175 int32_t ret_val; 9176 9177 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9178 9179 /* 9180 * Header prediction: check for the two common cases of a 9181 * uni-directional data xfer. If the packet has no control flags, 9182 * is in-sequence, the window didn't change and we're not 9183 * retransmitting, it's a candidate. If the length is zero and the 9184 * ack moved forward, we're the sender side of the xfer. Just free 9185 * the data acked & wake any higher level process that was blocked 9186 * waiting for space. If the length is non-zero and the ack didn't 9187 * move, we're the receiver side. If we're getting packets in-order 9188 * (the reassembly queue is empty), add the data toc The socket 9189 * buffer and note that we need a delayed ack. Make sure that the 9190 * hidden state-flags are also off. Since we check for 9191 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 9192 */ 9193 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9194 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) { 9195 /* 9196 * If we have delived under 4 segments increase the initial 9197 * window if raised by the peer. We use this to determine 9198 * dynamic and static rwnd's at the end of a connection. 9199 */ 9200 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd); 9201 } 9202 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 9203 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 9204 __predict_true(SEGQ_EMPTY(tp)) && 9205 __predict_true(th->th_seq == tp->rcv_nxt)) { 9206 if (tlen == 0) { 9207 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 9208 tiwin, nxt_pkt, iptos)) { 9209 return (0); 9210 } 9211 } else { 9212 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 9213 tiwin, nxt_pkt)) { 9214 return (0); 9215 } 9216 } 9217 } 9218 ctf_calc_rwin(so, tp); 9219 9220 if ((thflags & TH_RST) || 9221 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9222 return (ctf_process_rst(m, th, so, tp)); 9223 /* 9224 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9225 * synchronized state. 9226 */ 9227 if (thflags & TH_SYN) { 9228 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9229 return (ret_val); 9230 } 9231 /* 9232 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9233 * it's less than ts_recent, drop it. 9234 */ 9235 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9236 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9237 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9238 return (ret_val); 9239 } 9240 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9241 return (ret_val); 9242 } 9243 /* 9244 * If last ACK falls within this segment's sequence numbers, record 9245 * its timestamp. NOTE: 1) That the test incorporates suggestions 9246 * from the latest proposal of the tcplw@cray.com list (Braden 9247 * 1993/04/26). 2) That updating only on newer timestamps interferes 9248 * with our earlier PAWS tests, so this check should be solely 9249 * predicated on the sequence space of this segment. 3) That we 9250 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9251 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9252 * SEG.Len, This modified check allows us to overcome RFC1323's 9253 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9254 * p.869. In such cases, we can still calculate the RTT correctly 9255 * when RCV.NXT == Last.ACK.Sent. 9256 */ 9257 if ((to->to_flags & TOF_TS) != 0 && 9258 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9259 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9260 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9261 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9262 tp->ts_recent = to->to_tsval; 9263 } 9264 /* 9265 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9266 * is on (half-synchronized state), then queue data for later 9267 * processing; else drop segment and return. 9268 */ 9269 if ((thflags & TH_ACK) == 0) { 9270 if (tp->t_flags & TF_NEEDSYN) { 9271 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9272 tiwin, thflags, nxt_pkt)); 9273 } else if (tp->t_flags & TF_ACKNOW) { 9274 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9275 bbr->r_wanted_output = 1; 9276 return (ret_val); 9277 } else { 9278 ctf_do_drop(m, NULL); 9279 return (0); 9280 } 9281 } 9282 /* 9283 * Ack processing. 9284 */ 9285 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9286 return (ret_val); 9287 } 9288 if (sbavail(&so->so_snd)) { 9289 if (ctf_progress_timeout_check(tp, true)) { 9290 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9291 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9292 return (1); 9293 } 9294 } 9295 /* State changes only happen in bbr_process_data() */ 9296 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9297 tiwin, thflags, nxt_pkt)); 9298 } 9299 9300 /* 9301 * Return value of 1, the TCB is unlocked and most 9302 * likely gone, return value of 0, the TCB is still 9303 * locked. 9304 */ 9305 static int 9306 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 9307 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9308 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9309 { 9310 struct tcp_bbr *bbr; 9311 int32_t ret_val; 9312 9313 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9314 9315 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9316 ctf_calc_rwin(so, tp); 9317 if ((thflags & TH_RST) || 9318 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9319 return (ctf_process_rst(m, th, so, tp)); 9320 /* 9321 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9322 * synchronized state. 9323 */ 9324 if (thflags & TH_SYN) { 9325 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9326 return (ret_val); 9327 } 9328 /* 9329 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9330 * it's less than ts_recent, drop it. 9331 */ 9332 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9333 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9334 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9335 return (ret_val); 9336 } 9337 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9338 return (ret_val); 9339 } 9340 /* 9341 * If last ACK falls within this segment's sequence numbers, record 9342 * its timestamp. NOTE: 1) That the test incorporates suggestions 9343 * from the latest proposal of the tcplw@cray.com list (Braden 9344 * 1993/04/26). 2) That updating only on newer timestamps interferes 9345 * with our earlier PAWS tests, so this check should be solely 9346 * predicated on the sequence space of this segment. 3) That we 9347 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9348 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9349 * SEG.Len, This modified check allows us to overcome RFC1323's 9350 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9351 * p.869. In such cases, we can still calculate the RTT correctly 9352 * when RCV.NXT == Last.ACK.Sent. 9353 */ 9354 if ((to->to_flags & TOF_TS) != 0 && 9355 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9356 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9357 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9358 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9359 tp->ts_recent = to->to_tsval; 9360 } 9361 /* 9362 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9363 * is on (half-synchronized state), then queue data for later 9364 * processing; else drop segment and return. 9365 */ 9366 if ((thflags & TH_ACK) == 0) { 9367 if (tp->t_flags & TF_NEEDSYN) { 9368 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9369 tiwin, thflags, nxt_pkt)); 9370 } else if (tp->t_flags & TF_ACKNOW) { 9371 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9372 bbr->r_wanted_output = 1; 9373 return (ret_val); 9374 } else { 9375 ctf_do_drop(m, NULL); 9376 return (0); 9377 } 9378 } 9379 /* 9380 * Ack processing. 9381 */ 9382 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9383 return (ret_val); 9384 } 9385 if (sbavail(&so->so_snd)) { 9386 if (ctf_progress_timeout_check(tp, true)) { 9387 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9388 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9389 return (1); 9390 } 9391 } 9392 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9393 tiwin, thflags, nxt_pkt)); 9394 } 9395 9396 static int 9397 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr, 9398 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so) 9399 { 9400 9401 if (bbr->rc_allow_data_af_clo == 0) { 9402 close_now: 9403 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 9404 /* tcp_close will kill the inp pre-log the Reset */ 9405 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 9406 tp = tcp_close(tp); 9407 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 9408 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 9409 return (1); 9410 } 9411 if (sbavail(&so->so_snd) == 0) 9412 goto close_now; 9413 /* Ok we allow data that is ignored and a followup reset */ 9414 tp->rcv_nxt = th->th_seq + *tlen; 9415 tp->t_flags2 |= TF2_DROP_AF_DATA; 9416 bbr->r_wanted_output = 1; 9417 *tlen = 0; 9418 return (0); 9419 } 9420 9421 /* 9422 * Return value of 1, the TCB is unlocked and most 9423 * likely gone, return value of 0, the TCB is still 9424 * locked. 9425 */ 9426 static int 9427 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 9428 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9429 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9430 { 9431 int32_t ourfinisacked = 0; 9432 int32_t ret_val; 9433 struct tcp_bbr *bbr; 9434 9435 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9436 9437 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9438 ctf_calc_rwin(so, tp); 9439 if ((thflags & TH_RST) || 9440 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9441 return (ctf_process_rst(m, th, so, tp)); 9442 /* 9443 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9444 * synchronized state. 9445 */ 9446 if (thflags & TH_SYN) { 9447 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9448 return (ret_val); 9449 } 9450 /* 9451 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9452 * it's less than ts_recent, drop it. 9453 */ 9454 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9455 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9456 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9457 return (ret_val); 9458 } 9459 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9460 return (ret_val); 9461 } 9462 /* 9463 * If new data are received on a connection after the user processes 9464 * are gone, then RST the other end. 9465 * We call a new function now so we might continue and setup 9466 * to reset at all data being ack'd. 9467 */ 9468 if ((tp->t_flags & TF_CLOSED) && tlen && 9469 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9470 return (1); 9471 /* 9472 * If last ACK falls within this segment's sequence numbers, record 9473 * its timestamp. NOTE: 1) That the test incorporates suggestions 9474 * from the latest proposal of the tcplw@cray.com list (Braden 9475 * 1993/04/26). 2) That updating only on newer timestamps interferes 9476 * with our earlier PAWS tests, so this check should be solely 9477 * predicated on the sequence space of this segment. 3) That we 9478 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9479 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9480 * SEG.Len, This modified check allows us to overcome RFC1323's 9481 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9482 * p.869. In such cases, we can still calculate the RTT correctly 9483 * when RCV.NXT == Last.ACK.Sent. 9484 */ 9485 if ((to->to_flags & TOF_TS) != 0 && 9486 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9487 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9488 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9489 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9490 tp->ts_recent = to->to_tsval; 9491 } 9492 /* 9493 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9494 * is on (half-synchronized state), then queue data for later 9495 * processing; else drop segment and return. 9496 */ 9497 if ((thflags & TH_ACK) == 0) { 9498 if (tp->t_flags & TF_NEEDSYN) { 9499 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9500 tiwin, thflags, nxt_pkt)); 9501 } else if (tp->t_flags & TF_ACKNOW) { 9502 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9503 bbr->r_wanted_output = 1; 9504 return (ret_val); 9505 } else { 9506 ctf_do_drop(m, NULL); 9507 return (0); 9508 } 9509 } 9510 /* 9511 * Ack processing. 9512 */ 9513 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9514 return (ret_val); 9515 } 9516 if (ourfinisacked) { 9517 /* 9518 * If we can't receive any more data, then closing user can 9519 * proceed. Starting the timer is contrary to the 9520 * specification, but if we don't get a FIN we'll hang 9521 * forever. 9522 * 9523 * XXXjl: we should release the tp also, and use a 9524 * compressed state. 9525 */ 9526 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9527 soisdisconnected(so); 9528 tcp_timer_activate(tp, TT_2MSL, 9529 (tcp_fast_finwait2_recycle ? 9530 tcp_finwait2_timeout : 9531 TP_MAXIDLE(tp))); 9532 } 9533 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9534 } 9535 if (sbavail(&so->so_snd)) { 9536 if (ctf_progress_timeout_check(tp, true)) { 9537 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9538 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9539 return (1); 9540 } 9541 } 9542 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9543 tiwin, thflags, nxt_pkt)); 9544 } 9545 9546 /* 9547 * Return value of 1, the TCB is unlocked and most 9548 * likely gone, return value of 0, the TCB is still 9549 * locked. 9550 */ 9551 static int 9552 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 9553 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9554 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9555 { 9556 int32_t ourfinisacked = 0; 9557 int32_t ret_val; 9558 struct tcp_bbr *bbr; 9559 9560 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9561 9562 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9563 ctf_calc_rwin(so, tp); 9564 if ((thflags & TH_RST) || 9565 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9566 return (ctf_process_rst(m, th, so, tp)); 9567 /* 9568 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9569 * synchronized state. 9570 */ 9571 if (thflags & TH_SYN) { 9572 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9573 return (ret_val); 9574 } 9575 /* 9576 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9577 * it's less than ts_recent, drop it. 9578 */ 9579 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9580 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9581 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9582 return (ret_val); 9583 } 9584 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9585 return (ret_val); 9586 } 9587 /* 9588 * If last ACK falls within this segment's sequence numbers, record 9589 * its timestamp. NOTE: 1) That the test incorporates suggestions 9590 * from the latest proposal of the tcplw@cray.com list (Braden 9591 * 1993/04/26). 2) That updating only on newer timestamps interferes 9592 * with our earlier PAWS tests, so this check should be solely 9593 * predicated on the sequence space of this segment. 3) That we 9594 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9595 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9596 * SEG.Len, This modified check allows us to overcome RFC1323's 9597 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9598 * p.869. In such cases, we can still calculate the RTT correctly 9599 * when RCV.NXT == Last.ACK.Sent. 9600 */ 9601 if ((to->to_flags & TOF_TS) != 0 && 9602 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9603 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9604 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9605 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9606 tp->ts_recent = to->to_tsval; 9607 } 9608 /* 9609 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9610 * is on (half-synchronized state), then queue data for later 9611 * processing; else drop segment and return. 9612 */ 9613 if ((thflags & TH_ACK) == 0) { 9614 if (tp->t_flags & TF_NEEDSYN) { 9615 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9616 tiwin, thflags, nxt_pkt)); 9617 } else if (tp->t_flags & TF_ACKNOW) { 9618 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9619 bbr->r_wanted_output = 1; 9620 return (ret_val); 9621 } else { 9622 ctf_do_drop(m, NULL); 9623 return (0); 9624 } 9625 } 9626 /* 9627 * Ack processing. 9628 */ 9629 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9630 return (ret_val); 9631 } 9632 if (ourfinisacked) { 9633 tcp_twstart(tp); 9634 m_freem(m); 9635 return (1); 9636 } 9637 if (sbavail(&so->so_snd)) { 9638 if (ctf_progress_timeout_check(tp, true)) { 9639 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9640 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9641 return (1); 9642 } 9643 } 9644 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9645 tiwin, thflags, nxt_pkt)); 9646 } 9647 9648 /* 9649 * Return value of 1, the TCB is unlocked and most 9650 * likely gone, return value of 0, the TCB is still 9651 * locked. 9652 */ 9653 static int 9654 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 9655 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9656 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9657 { 9658 int32_t ourfinisacked = 0; 9659 int32_t ret_val; 9660 struct tcp_bbr *bbr; 9661 9662 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9663 9664 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9665 ctf_calc_rwin(so, tp); 9666 if ((thflags & TH_RST) || 9667 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9668 return (ctf_process_rst(m, th, so, tp)); 9669 /* 9670 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9671 * synchronized state. 9672 */ 9673 if (thflags & TH_SYN) { 9674 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9675 return (ret_val); 9676 } 9677 /* 9678 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9679 * it's less than ts_recent, drop it. 9680 */ 9681 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9682 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9683 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9684 return (ret_val); 9685 } 9686 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9687 return (ret_val); 9688 } 9689 /* 9690 * If last ACK falls within this segment's sequence numbers, record 9691 * its timestamp. NOTE: 1) That the test incorporates suggestions 9692 * from the latest proposal of the tcplw@cray.com list (Braden 9693 * 1993/04/26). 2) That updating only on newer timestamps interferes 9694 * with our earlier PAWS tests, so this check should be solely 9695 * predicated on the sequence space of this segment. 3) That we 9696 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9697 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9698 * SEG.Len, This modified check allows us to overcome RFC1323's 9699 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9700 * p.869. In such cases, we can still calculate the RTT correctly 9701 * when RCV.NXT == Last.ACK.Sent. 9702 */ 9703 if ((to->to_flags & TOF_TS) != 0 && 9704 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9705 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9706 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9707 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9708 tp->ts_recent = to->to_tsval; 9709 } 9710 /* 9711 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9712 * is on (half-synchronized state), then queue data for later 9713 * processing; else drop segment and return. 9714 */ 9715 if ((thflags & TH_ACK) == 0) { 9716 if (tp->t_flags & TF_NEEDSYN) { 9717 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9718 tiwin, thflags, nxt_pkt)); 9719 } else if (tp->t_flags & TF_ACKNOW) { 9720 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9721 bbr->r_wanted_output = 1; 9722 return (ret_val); 9723 } else { 9724 ctf_do_drop(m, NULL); 9725 return (0); 9726 } 9727 } 9728 /* 9729 * case TCPS_LAST_ACK: Ack processing. 9730 */ 9731 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9732 return (ret_val); 9733 } 9734 if (ourfinisacked) { 9735 tp = tcp_close(tp); 9736 ctf_do_drop(m, tp); 9737 return (1); 9738 } 9739 if (sbavail(&so->so_snd)) { 9740 if (ctf_progress_timeout_check(tp, true)) { 9741 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9742 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9743 return (1); 9744 } 9745 } 9746 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9747 tiwin, thflags, nxt_pkt)); 9748 } 9749 9750 /* 9751 * Return value of 1, the TCB is unlocked and most 9752 * likely gone, return value of 0, the TCB is still 9753 * locked. 9754 */ 9755 static int 9756 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 9757 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9758 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9759 { 9760 int32_t ourfinisacked = 0; 9761 int32_t ret_val; 9762 struct tcp_bbr *bbr; 9763 9764 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9765 9766 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9767 ctf_calc_rwin(so, tp); 9768 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 9769 if ((thflags & TH_RST) || 9770 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9771 return (ctf_process_rst(m, th, so, tp)); 9772 9773 /* 9774 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9775 * synchronized state. 9776 */ 9777 if (thflags & TH_SYN) { 9778 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9779 return (ret_val); 9780 } 9781 /* 9782 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9783 * it's less than ts_recent, drop it. 9784 */ 9785 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9786 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9787 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9788 return (ret_val); 9789 } 9790 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9791 return (ret_val); 9792 } 9793 /* 9794 * If new data are received on a connection after the user processes 9795 * are gone, then we may RST the other end depending on the outcome 9796 * of bbr_check_data_after_close. 9797 * We call a new function now so we might continue and setup 9798 * to reset at all data being ack'd. 9799 */ 9800 if ((tp->t_flags & TF_CLOSED) && tlen && 9801 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9802 return (1); 9803 /* 9804 * If last ACK falls within this segment's sequence numbers, record 9805 * its timestamp. NOTE: 1) That the test incorporates suggestions 9806 * from the latest proposal of the tcplw@cray.com list (Braden 9807 * 1993/04/26). 2) That updating only on newer timestamps interferes 9808 * with our earlier PAWS tests, so this check should be solely 9809 * predicated on the sequence space of this segment. 3) That we 9810 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9811 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9812 * SEG.Len, This modified check allows us to overcome RFC1323's 9813 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9814 * p.869. In such cases, we can still calculate the RTT correctly 9815 * when RCV.NXT == Last.ACK.Sent. 9816 */ 9817 if ((to->to_flags & TOF_TS) != 0 && 9818 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9819 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9820 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9821 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9822 tp->ts_recent = to->to_tsval; 9823 } 9824 /* 9825 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9826 * is on (half-synchronized state), then queue data for later 9827 * processing; else drop segment and return. 9828 */ 9829 if ((thflags & TH_ACK) == 0) { 9830 if (tp->t_flags & TF_NEEDSYN) { 9831 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9832 tiwin, thflags, nxt_pkt)); 9833 } else if (tp->t_flags & TF_ACKNOW) { 9834 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9835 bbr->r_wanted_output = 1; 9836 return (ret_val); 9837 } else { 9838 ctf_do_drop(m, NULL); 9839 return (0); 9840 } 9841 } 9842 /* 9843 * Ack processing. 9844 */ 9845 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9846 return (ret_val); 9847 } 9848 if (sbavail(&so->so_snd)) { 9849 if (ctf_progress_timeout_check(tp, true)) { 9850 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9851 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9852 return (1); 9853 } 9854 } 9855 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9856 tiwin, thflags, nxt_pkt)); 9857 } 9858 9859 static void 9860 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr) 9861 { 9862 /* 9863 * Assure no timers are running. 9864 */ 9865 if (tcp_timer_active(tp, TT_PERSIST)) { 9866 /* We enter in persists, set the flag appropriately */ 9867 bbr->rc_in_persist = 1; 9868 } 9869 if (tcp_in_hpts(bbr->rc_tp)) { 9870 tcp_hpts_remove(bbr->rc_tp); 9871 } 9872 } 9873 9874 static void 9875 bbr_google_mode_on(struct tcp_bbr *bbr) 9876 { 9877 bbr->rc_use_google = 1; 9878 bbr->rc_no_pacing = 0; 9879 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9880 bbr->r_use_policer = bbr_policer_detection_enabled; 9881 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 9882 bbr->bbr_use_rack_cheat = 0; 9883 bbr->r_ctl.rc_incr_tmrs = 0; 9884 bbr->r_ctl.rc_inc_tcp_oh = 0; 9885 bbr->r_ctl.rc_inc_ip_oh = 0; 9886 bbr->r_ctl.rc_inc_enet_oh = 0; 9887 reset_time(&bbr->r_ctl.rc_delrate, 9888 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 9889 reset_time_small(&bbr->r_ctl.rc_rttprop, 9890 (11 * USECS_IN_SECOND)); 9891 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9892 } 9893 9894 static void 9895 bbr_google_mode_off(struct tcp_bbr *bbr) 9896 { 9897 bbr->rc_use_google = 0; 9898 bbr->r_ctl.bbr_google_discount = 0; 9899 bbr->no_pacing_until = bbr_no_pacing_until; 9900 bbr->r_use_policer = 0; 9901 if (bbr->no_pacing_until) 9902 bbr->rc_no_pacing = 1; 9903 else 9904 bbr->rc_no_pacing = 0; 9905 if (bbr_use_rack_resend_cheat) 9906 bbr->bbr_use_rack_cheat = 1; 9907 else 9908 bbr->bbr_use_rack_cheat = 0; 9909 if (bbr_incr_timers) 9910 bbr->r_ctl.rc_incr_tmrs = 1; 9911 else 9912 bbr->r_ctl.rc_incr_tmrs = 0; 9913 if (bbr_include_tcp_oh) 9914 bbr->r_ctl.rc_inc_tcp_oh = 1; 9915 else 9916 bbr->r_ctl.rc_inc_tcp_oh = 0; 9917 if (bbr_include_ip_oh) 9918 bbr->r_ctl.rc_inc_ip_oh = 1; 9919 else 9920 bbr->r_ctl.rc_inc_ip_oh = 0; 9921 if (bbr_include_enet_oh) 9922 bbr->r_ctl.rc_inc_enet_oh = 1; 9923 else 9924 bbr->r_ctl.rc_inc_enet_oh = 0; 9925 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 9926 reset_time(&bbr->r_ctl.rc_delrate, 9927 bbr_num_pktepo_for_del_limit); 9928 reset_time_small(&bbr->r_ctl.rc_rttprop, 9929 (bbr_filter_len_sec * USECS_IN_SECOND)); 9930 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9931 } 9932 /* 9933 * Return 0 on success, non-zero on failure 9934 * which indicates the error (usually no memory). 9935 */ 9936 static int 9937 bbr_init(struct tcpcb *tp, void **ptr) 9938 { 9939 struct inpcb *inp = tptoinpcb(tp); 9940 struct tcp_bbr *bbr = NULL; 9941 uint32_t cts; 9942 9943 tcp_hpts_init(tp); 9944 9945 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO)); 9946 if (*ptr == NULL) { 9947 /* 9948 * We need to allocate memory but cant. The INP and INP_INFO 9949 * locks and they are recursive (happens during setup. So a 9950 * scheme to drop the locks fails :( 9951 * 9952 */ 9953 return (ENOMEM); 9954 } 9955 bbr = (struct tcp_bbr *)*ptr; 9956 bbr->rtt_valid = 0; 9957 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 9958 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 9959 /* Take off any undesired flags */ 9960 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 9961 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 9962 tp->t_flags2 &= ~TF2_MBUF_ACKCMP; 9963 tp->t_flags2 &= ~TF2_MBUF_L_ACKS; 9964 9965 TAILQ_INIT(&bbr->r_ctl.rc_map); 9966 TAILQ_INIT(&bbr->r_ctl.rc_free); 9967 TAILQ_INIT(&bbr->r_ctl.rc_tmap); 9968 bbr->rc_tp = tp; 9969 bbr->rc_inp = inp; 9970 cts = tcp_get_usecs(&bbr->rc_tv); 9971 tp->t_acktime = 0; 9972 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close; 9973 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade; 9974 bbr->rc_tlp_threshold = bbr_tlp_thresh; 9975 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh; 9976 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay; 9977 bbr->r_ctl.rc_min_to = bbr_min_to; 9978 bbr->rc_bbr_state = BBR_STATE_STARTUP; 9979 bbr->r_ctl.bbr_lost_at_state = 0; 9980 bbr->r_ctl.rc_lost_at_startup = 0; 9981 bbr->rc_all_timers_stopped = 0; 9982 bbr->r_ctl.rc_bbr_lastbtlbw = 0; 9983 bbr->r_ctl.rc_pkt_epoch_del = 0; 9984 bbr->r_ctl.rc_pkt_epoch = 0; 9985 bbr->r_ctl.rc_lowest_rtt = 0xffffffff; 9986 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain; 9987 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 9988 bbr->r_ctl.rc_went_idle_time = cts; 9989 bbr->rc_pacer_started = cts; 9990 bbr->r_ctl.rc_pkt_epoch_time = cts; 9991 bbr->r_ctl.rc_rcvtime = cts; 9992 bbr->r_ctl.rc_bbr_state_time = cts; 9993 bbr->r_ctl.rc_del_time = cts; 9994 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 9995 bbr->r_ctl.last_in_probertt = cts; 9996 bbr->skip_gain = 0; 9997 bbr->gain_is_limited = 0; 9998 bbr->no_pacing_until = bbr_no_pacing_until; 9999 if (bbr->no_pacing_until) 10000 bbr->rc_no_pacing = 1; 10001 if (bbr_use_google_algo) { 10002 bbr->rc_no_pacing = 0; 10003 bbr->rc_use_google = 1; 10004 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 10005 bbr->r_use_policer = bbr_policer_detection_enabled; 10006 } else { 10007 bbr->rc_use_google = 0; 10008 bbr->r_ctl.bbr_google_discount = 0; 10009 bbr->r_use_policer = 0; 10010 } 10011 if (bbr_ts_limiting) 10012 bbr->rc_use_ts_limit = 1; 10013 else 10014 bbr->rc_use_ts_limit = 0; 10015 if (bbr_ts_can_raise) 10016 bbr->ts_can_raise = 1; 10017 else 10018 bbr->ts_can_raise = 0; 10019 if (V_tcp_delack_enabled == 1) 10020 tp->t_delayed_ack = 2; 10021 else if (V_tcp_delack_enabled == 0) 10022 tp->t_delayed_ack = 0; 10023 else if (V_tcp_delack_enabled < 100) 10024 tp->t_delayed_ack = V_tcp_delack_enabled; 10025 else 10026 tp->t_delayed_ack = 2; 10027 if (bbr->rc_use_google == 0) 10028 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10029 else 10030 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 10031 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms; 10032 bbr->rc_max_rto_sec = bbr_rto_max_sec; 10033 bbr->rc_init_win = bbr_def_init_win; 10034 if (tp->t_flags & TF_REQ_TSTMP) 10035 bbr->rc_last_options = TCP_TS_OVERHEAD; 10036 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options; 10037 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd; 10038 bbr->r_init_rtt = 1; 10039 10040 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 10041 if (bbr_allow_hdwr_pacing) 10042 bbr->bbr_hdw_pace_ena = 1; 10043 else 10044 bbr->bbr_hdw_pace_ena = 0; 10045 if (bbr_sends_full_iwnd) 10046 bbr->bbr_init_win_cheat = 1; 10047 else 10048 bbr->bbr_init_win_cheat = 0; 10049 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max; 10050 bbr->r_ctl.rc_drain_pg = bbr_drain_gain; 10051 bbr->r_ctl.rc_startup_pg = bbr_high_gain; 10052 bbr->rc_loss_exit = bbr_exit_startup_at_loss; 10053 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain; 10054 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second; 10055 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar; 10056 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max; 10057 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor; 10058 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min; 10059 bbr->r_ctl.bbr_cross_over = bbr_cross_over; 10060 bbr->r_ctl.rc_rtt_shrinks = cts; 10061 if (bbr->rc_use_google) { 10062 setup_time_filter(&bbr->r_ctl.rc_delrate, 10063 FILTER_TYPE_MAX, 10064 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 10065 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10066 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND)); 10067 } else { 10068 setup_time_filter(&bbr->r_ctl.rc_delrate, 10069 FILTER_TYPE_MAX, 10070 bbr_num_pktepo_for_del_limit); 10071 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10072 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND)); 10073 } 10074 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0); 10075 if (bbr_uses_idle_restart) 10076 bbr->rc_use_idle_restart = 1; 10077 else 10078 bbr->rc_use_idle_restart = 0; 10079 bbr->r_ctl.rc_bbr_cur_del_rate = 0; 10080 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps; 10081 if (bbr_resends_use_tso) 10082 bbr->rc_resends_use_tso = 1; 10083 if (tp->snd_una != tp->snd_max) { 10084 /* Create a send map for the current outstanding data */ 10085 struct bbr_sendmap *rsm; 10086 10087 rsm = bbr_alloc(bbr); 10088 if (rsm == NULL) { 10089 uma_zfree(bbr_pcb_zone, *ptr); 10090 *ptr = NULL; 10091 return (ENOMEM); 10092 } 10093 rsm->r_rtt_not_allowed = 1; 10094 rsm->r_tim_lastsent[0] = cts; 10095 rsm->r_rtr_cnt = 1; 10096 rsm->r_rtr_bytes = 0; 10097 rsm->r_start = tp->snd_una; 10098 rsm->r_end = tp->snd_max; 10099 rsm->r_dupack = 0; 10100 rsm->r_delivered = bbr->r_ctl.rc_delivered; 10101 rsm->r_ts_valid = 0; 10102 rsm->r_del_ack_ts = tp->ts_recent; 10103 rsm->r_del_time = cts; 10104 if (bbr->r_ctl.r_app_limited_until) 10105 rsm->r_app_limited = 1; 10106 else 10107 rsm->r_app_limited = 0; 10108 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 10109 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 10110 rsm->r_in_tmap = 1; 10111 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 10112 rsm->r_bbr_state = bbr_state_val(bbr); 10113 else 10114 rsm->r_bbr_state = 8; 10115 } 10116 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0)) 10117 bbr->bbr_use_rack_cheat = 1; 10118 if (bbr_incr_timers && (bbr->rc_use_google == 0)) 10119 bbr->r_ctl.rc_incr_tmrs = 1; 10120 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0)) 10121 bbr->r_ctl.rc_inc_tcp_oh = 1; 10122 if (bbr_include_ip_oh && (bbr->rc_use_google == 0)) 10123 bbr->r_ctl.rc_inc_ip_oh = 1; 10124 if (bbr_include_enet_oh && (bbr->rc_use_google == 0)) 10125 bbr->r_ctl.rc_inc_enet_oh = 1; 10126 10127 bbr_log_type_statechange(bbr, cts, __LINE__); 10128 if (TCPS_HAVEESTABLISHED(tp->t_state) && 10129 (tp->t_srtt)) { 10130 uint32_t rtt; 10131 10132 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 10133 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 10134 } 10135 /* announce the settings and state */ 10136 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT); 10137 tcp_bbr_tso_size_check(bbr, cts); 10138 /* 10139 * Now call the generic function to start a timer. This will place 10140 * the TCB on the hptsi wheel if a timer is needed with appropriate 10141 * flags. 10142 */ 10143 bbr_stop_all_timers(tp, bbr); 10144 /* 10145 * Validate the timers are not in usec, if they are convert. 10146 * BBR should in theory move to USEC and get rid of a 10147 * lot of the TICKS_2 calls.. but for now we stay 10148 * with tick timers. 10149 */ 10150 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 10151 TCPT_RANGESET(tp->t_rxtcur, 10152 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 10153 tp->t_rttmin, TCPTV_REXMTMAX); 10154 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0); 10155 return (0); 10156 } 10157 10158 /* 10159 * Return 0 if we can accept the connection. Return 10160 * non-zero if we can't handle the connection. A EAGAIN 10161 * means you need to wait until the connection is up. 10162 * a EADDRNOTAVAIL means we can never handle the connection 10163 * (no SACK). 10164 */ 10165 static int 10166 bbr_handoff_ok(struct tcpcb *tp) 10167 { 10168 if ((tp->t_state == TCPS_CLOSED) || 10169 (tp->t_state == TCPS_LISTEN)) { 10170 /* Sure no problem though it may not stick */ 10171 return (0); 10172 } 10173 if ((tp->t_state == TCPS_SYN_SENT) || 10174 (tp->t_state == TCPS_SYN_RECEIVED)) { 10175 /* 10176 * We really don't know you have to get to ESTAB or beyond 10177 * to tell. 10178 */ 10179 return (EAGAIN); 10180 } 10181 if (tp->t_flags & TF_SENTFIN) 10182 return (EINVAL); 10183 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) { 10184 return (0); 10185 } 10186 /* 10187 * If we reach here we don't do SACK on this connection so we can 10188 * never do rack. 10189 */ 10190 return (EINVAL); 10191 } 10192 10193 static void 10194 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged) 10195 { 10196 if (tp->t_fb_ptr) { 10197 uint32_t calc; 10198 struct tcp_bbr *bbr; 10199 struct bbr_sendmap *rsm; 10200 10201 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 10202 if (bbr->r_ctl.crte) 10203 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 10204 bbr_log_flowend(bbr); 10205 bbr->rc_tp = NULL; 10206 if (bbr->bbr_hdrw_pacing) 10207 counter_u64_add(bbr_flows_whdwr_pacing, -1); 10208 else 10209 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 10210 if (bbr->r_ctl.crte != NULL) { 10211 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 10212 bbr->r_ctl.crte = NULL; 10213 } 10214 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10215 while (rsm) { 10216 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 10217 uma_zfree(bbr_zone, rsm); 10218 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10219 } 10220 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10221 while (rsm) { 10222 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 10223 uma_zfree(bbr_zone, rsm); 10224 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10225 } 10226 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd; 10227 if (calc > (bbr->r_ctl.rc_init_rwnd / 10)) 10228 BBR_STAT_INC(bbr_dynamic_rwnd); 10229 else 10230 BBR_STAT_INC(bbr_static_rwnd); 10231 bbr->r_ctl.rc_free_cnt = 0; 10232 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr); 10233 tp->t_fb_ptr = NULL; 10234 } 10235 /* Make sure snd_nxt is correctly set */ 10236 tp->snd_nxt = tp->snd_max; 10237 } 10238 10239 static void 10240 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win) 10241 { 10242 switch (tp->t_state) { 10243 case TCPS_SYN_SENT: 10244 bbr->r_state = TCPS_SYN_SENT; 10245 bbr->r_substate = bbr_do_syn_sent; 10246 break; 10247 case TCPS_SYN_RECEIVED: 10248 bbr->r_state = TCPS_SYN_RECEIVED; 10249 bbr->r_substate = bbr_do_syn_recv; 10250 break; 10251 case TCPS_ESTABLISHED: 10252 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd); 10253 bbr->r_state = TCPS_ESTABLISHED; 10254 bbr->r_substate = bbr_do_established; 10255 break; 10256 case TCPS_CLOSE_WAIT: 10257 bbr->r_state = TCPS_CLOSE_WAIT; 10258 bbr->r_substate = bbr_do_close_wait; 10259 break; 10260 case TCPS_FIN_WAIT_1: 10261 bbr->r_state = TCPS_FIN_WAIT_1; 10262 bbr->r_substate = bbr_do_fin_wait_1; 10263 break; 10264 case TCPS_CLOSING: 10265 bbr->r_state = TCPS_CLOSING; 10266 bbr->r_substate = bbr_do_closing; 10267 break; 10268 case TCPS_LAST_ACK: 10269 bbr->r_state = TCPS_LAST_ACK; 10270 bbr->r_substate = bbr_do_lastack; 10271 break; 10272 case TCPS_FIN_WAIT_2: 10273 bbr->r_state = TCPS_FIN_WAIT_2; 10274 bbr->r_substate = bbr_do_fin_wait_2; 10275 break; 10276 case TCPS_LISTEN: 10277 case TCPS_CLOSED: 10278 case TCPS_TIME_WAIT: 10279 default: 10280 break; 10281 }; 10282 } 10283 10284 static void 10285 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog) 10286 { 10287 /* 10288 * Now what state are we going into now? Is there adjustments 10289 * needed? 10290 */ 10291 int32_t old_state; 10292 10293 old_state = bbr_state_val(bbr); 10294 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) { 10295 /* Save the lowest srtt we saw in our end of the sub-state */ 10296 bbr->rc_hit_state_1 = 0; 10297 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff) 10298 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state; 10299 } 10300 bbr->rc_bbr_substate++; 10301 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10302 /* 10303 * We enter the gain(5/4) cycle (possibly less if 10304 * shallow buffer detection is enabled) 10305 */ 10306 if (bbr->skip_gain) { 10307 /* 10308 * Hardware pacing has set our rate to 10309 * the max and limited our b/w just 10310 * do level i.e. no gain. 10311 */ 10312 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1]; 10313 } else if (bbr->gain_is_limited && 10314 bbr->bbr_hdrw_pacing && 10315 bbr->r_ctl.crte) { 10316 /* 10317 * We can't gain above the hardware pacing 10318 * rate which is less than our rate + the gain 10319 * calculate the gain needed to reach the hardware 10320 * pacing rate.. 10321 */ 10322 uint64_t bw, rate, gain_calc; 10323 10324 bw = bbr_get_bw(bbr); 10325 rate = bbr->r_ctl.crte->rate; 10326 if ((rate > bw) && 10327 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) { 10328 gain_calc = (rate * BBR_UNIT) / bw; 10329 if (gain_calc < BBR_UNIT) 10330 gain_calc = BBR_UNIT; 10331 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc; 10332 } else { 10333 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10334 } 10335 } else 10336 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10337 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) { 10338 bbr->r_ctl.rc_bbr_state_atflight = cts; 10339 } else 10340 bbr->r_ctl.rc_bbr_state_atflight = 0; 10341 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10342 bbr->rc_hit_state_1 = 1; 10343 bbr->r_ctl.rc_exta_time_gd = 0; 10344 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10345 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10346 if (bbr_state_drain_2_tar) { 10347 bbr->r_ctl.rc_bbr_state_atflight = 0; 10348 } else 10349 bbr->r_ctl.rc_bbr_state_atflight = cts; 10350 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN]; 10351 } else { 10352 /* All other cycles hit here 2-7 */ 10353 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) { 10354 if (bbr_sub_drain_slam_cwnd && 10355 (bbr->rc_use_google == 0) && 10356 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10357 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10358 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10359 } 10360 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP)) 10361 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) - 10362 bbr_get_rtt(bbr, BBR_RTT_PROP)); 10363 else 10364 bbr->r_ctl.rc_exta_time_gd = 0; 10365 if (bbr->r_ctl.rc_exta_time_gd) { 10366 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd; 10367 /* Now chop up the time for each state (div by 7) */ 10368 bbr->r_ctl.rc_level_state_extra /= 7; 10369 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) { 10370 /* Add a randomization */ 10371 bbr_randomize_extra_state_time(bbr); 10372 } 10373 } 10374 } 10375 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10376 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)]; 10377 } 10378 if (bbr->rc_use_google) { 10379 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10380 } 10381 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10382 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10383 if (dolog) 10384 bbr_log_type_statechange(bbr, cts, line); 10385 10386 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10387 uint32_t time_in; 10388 10389 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10390 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 10391 counter_u64_add(bbr_state_time[(old_state + 5)], time_in); 10392 } else { 10393 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10394 } 10395 } 10396 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 10397 bbr_set_state_target(bbr, __LINE__); 10398 if (bbr_sub_drain_slam_cwnd && 10399 (bbr->rc_use_google == 0) && 10400 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10401 /* Slam down the cwnd */ 10402 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10403 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10404 if (bbr_sub_drain_app_limit) { 10405 /* Go app limited if we are on a long drain */ 10406 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + 10407 ctf_flight_size(bbr->rc_tp, 10408 (bbr->r_ctl.rc_sacked + 10409 bbr->r_ctl.rc_lost_bytes))); 10410 } 10411 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10412 } 10413 if (bbr->rc_lt_use_bw) { 10414 /* In policed mode we clamp pacing_gain to BBR_UNIT */ 10415 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10416 } 10417 /* Google changes TSO size every cycle */ 10418 if (bbr->rc_use_google) 10419 tcp_bbr_tso_size_check(bbr, cts); 10420 bbr->r_ctl.gain_epoch = cts; 10421 bbr->r_ctl.rc_bbr_state_time = cts; 10422 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch; 10423 } 10424 10425 static void 10426 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10427 { 10428 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) && 10429 (google_allow_early_out == 1) && 10430 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) { 10431 /* We have reached out target flight size possibly early */ 10432 goto change_state; 10433 } 10434 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10435 return; 10436 } 10437 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) { 10438 /* 10439 * Must be a rttProp movement forward before 10440 * we can change states. 10441 */ 10442 return; 10443 } 10444 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10445 /* 10446 * The needed time has passed but for 10447 * the gain cycle extra rules apply: 10448 * 1) If we have seen loss, we exit 10449 * 2) If we have not reached the target 10450 * we stay in GAIN (gain-to-target). 10451 */ 10452 if (google_consider_lost && losses) 10453 goto change_state; 10454 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) { 10455 return; 10456 } 10457 } 10458 change_state: 10459 /* For gain we must reach our target, all others last 1 rttProp */ 10460 bbr_substate_change(bbr, cts, __LINE__, 1); 10461 } 10462 10463 static void 10464 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10465 { 10466 uint32_t flight, bbr_cur_cycle_time; 10467 10468 if (bbr->rc_use_google) { 10469 bbr_set_probebw_google_gains(bbr, cts, losses); 10470 return; 10471 } 10472 if (cts == 0) { 10473 /* 10474 * Never alow cts to be 0 we 10475 * do this so we can judge if 10476 * we have set a timestamp. 10477 */ 10478 cts = 1; 10479 } 10480 if (bbr_state_is_pkt_epoch) 10481 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 10482 else 10483 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP); 10484 10485 if (bbr->r_ctl.rc_bbr_state_atflight == 0) { 10486 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10487 flight = ctf_flight_size(bbr->rc_tp, 10488 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10489 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) { 10490 /* Keep it slam down */ 10491 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) { 10492 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10493 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10494 } 10495 if (bbr_sub_drain_app_limit) { 10496 /* Go app limited if we are on a long drain */ 10497 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight); 10498 } 10499 } 10500 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) && 10501 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) || 10502 (flight >= bbr->r_ctl.flightsize_at_drain))) { 10503 /* 10504 * Still here after the same time as 10505 * the gain. We need to drain harder 10506 * for the next srtt. Reduce by a set amount 10507 * the gain drop is capped at DRAIN states 10508 * value (88). 10509 */ 10510 bbr->r_ctl.flightsize_at_drain = flight; 10511 if (bbr_drain_drop_mul && 10512 bbr_drain_drop_div && 10513 (bbr_drain_drop_mul < bbr_drain_drop_div)) { 10514 /* Use your specific drop value (def 4/5 = 20%) */ 10515 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul; 10516 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div; 10517 } else { 10518 /* You get drop of 20% */ 10519 bbr->r_ctl.rc_bbr_hptsi_gain *= 4; 10520 bbr->r_ctl.rc_bbr_hptsi_gain /= 5; 10521 } 10522 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) { 10523 /* Reduce our gain again to the bottom */ 10524 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 10525 } 10526 bbr_log_exit_gain(bbr, cts, 4); 10527 /* 10528 * Extend out so we wait another 10529 * epoch before dropping again. 10530 */ 10531 bbr->r_ctl.gain_epoch = cts; 10532 } 10533 if (flight <= bbr->r_ctl.rc_target_at_state) { 10534 if (bbr_sub_drain_slam_cwnd && 10535 (bbr->rc_use_google == 0) && 10536 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10537 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10538 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10539 } 10540 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10541 bbr_log_exit_gain(bbr, cts, 3); 10542 } 10543 } else { 10544 /* Its a gain */ 10545 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) { 10546 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10547 goto change_state; 10548 } 10549 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) || 10550 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >= 10551 bbr->rc_tp->snd_wnd)) { 10552 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10553 bbr_log_exit_gain(bbr, cts, 2); 10554 } 10555 } 10556 /** 10557 * We fall through and return always one of two things has 10558 * occurred. 10559 * 1) We are still not at target 10560 * <or> 10561 * 2) We reached the target and set rc_bbr_state_atflight 10562 * which means we no longer hit this block 10563 * next time we are called. 10564 */ 10565 return; 10566 } 10567 change_state: 10568 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) 10569 return; 10570 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) { 10571 /* Less than a full time-period has passed */ 10572 return; 10573 } 10574 if (bbr->r_ctl.rc_level_state_extra && 10575 (bbr_state_val(bbr) > BBR_SUB_DRAIN) && 10576 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10577 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10578 /* Less than a full time-period + extra has passed */ 10579 return; 10580 } 10581 if (bbr_gain_gets_extra_too && 10582 bbr->r_ctl.rc_level_state_extra && 10583 (bbr_state_val(bbr) == BBR_SUB_GAIN) && 10584 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10585 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10586 /* Less than a full time-period + extra has passed */ 10587 return; 10588 } 10589 bbr_substate_change(bbr, cts, __LINE__, 1); 10590 } 10591 10592 static uint32_t 10593 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain) 10594 { 10595 uint32_t mss, tar; 10596 10597 if (bbr->rc_use_google) { 10598 /* Google just uses the cwnd target */ 10599 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain); 10600 } else { 10601 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 10602 bbr->r_ctl.rc_pace_max_segs); 10603 /* Get the base cwnd with gain rounded to a mss */ 10604 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr), 10605 gain), mss); 10606 /* Make sure it is within our min */ 10607 if (tar < get_min_cwnd(bbr)) 10608 return (get_min_cwnd(bbr)); 10609 } 10610 return (tar); 10611 } 10612 10613 static void 10614 bbr_set_state_target(struct tcp_bbr *bbr, int line) 10615 { 10616 uint32_t tar, meth; 10617 10618 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 10619 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 10620 /* Special case using old probe-rtt method */ 10621 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10622 meth = 1; 10623 } else { 10624 /* Non-probe-rtt case and reduced probe-rtt */ 10625 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 10626 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) { 10627 /* For gain cycle we use the hptsi gain */ 10628 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10629 meth = 2; 10630 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) { 10631 /* 10632 * If configured, or for google all other states 10633 * get BBR_UNIT. 10634 */ 10635 tar = bbr_get_a_state_target(bbr, BBR_UNIT); 10636 meth = 3; 10637 } else { 10638 /* 10639 * Or we set a target based on the pacing gain 10640 * for non-google mode and default (non-configured). 10641 * Note we don't set a target goal below drain (192). 10642 */ 10643 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) { 10644 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]); 10645 meth = 4; 10646 } else { 10647 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10648 meth = 5; 10649 } 10650 } 10651 } 10652 bbr_log_set_of_state_target(bbr, tar, line, meth); 10653 bbr->r_ctl.rc_target_at_state = tar; 10654 } 10655 10656 static void 10657 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 10658 { 10659 /* Change to probe_rtt */ 10660 uint32_t time_in; 10661 10662 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10663 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10664 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10665 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain 10666 + bbr->r_ctl.rc_delivered); 10667 /* Setup so we force feed the filter */ 10668 if (bbr->rc_use_google || bbr_probertt_sets_rtt) 10669 bbr->rc_prtt_set_ts = 1; 10670 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10671 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10672 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10673 } 10674 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0); 10675 bbr->r_ctl.rc_rtt_shrinks = cts; 10676 bbr->r_ctl.last_in_probertt = cts; 10677 bbr->r_ctl.rc_probertt_srttchktim = cts; 10678 bbr->r_ctl.rc_bbr_state_time = cts; 10679 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT; 10680 /* We need to force the filter to update */ 10681 10682 if ((bbr_sub_drain_slam_cwnd) && 10683 bbr->rc_hit_state_1 && 10684 (bbr->rc_use_google == 0) && 10685 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10686 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd) 10687 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10688 } else 10689 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10690 /* Update the lost */ 10691 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10692 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){ 10693 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */ 10694 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10695 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10696 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10697 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10698 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6); 10699 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd; 10700 } else { 10701 /* 10702 * We bring it down slowly by using a hptsi gain that is 10703 * probably 75%. This will slowly float down our outstanding 10704 * without tampering with the cwnd. 10705 */ 10706 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 10707 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10708 bbr_set_state_target(bbr, __LINE__); 10709 if (bbr_prtt_slam_cwnd && 10710 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 10711 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10712 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10713 } 10714 } 10715 if (ctf_flight_size(bbr->rc_tp, 10716 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 10717 bbr->r_ctl.rc_target_at_state) { 10718 /* We are at target */ 10719 bbr->r_ctl.rc_bbr_enters_probertt = cts; 10720 } else { 10721 /* We need to come down to reach target before our time begins */ 10722 bbr->r_ctl.rc_bbr_enters_probertt = 0; 10723 } 10724 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch; 10725 BBR_STAT_INC(bbr_enter_probertt); 10726 bbr_log_exit_gain(bbr, cts, 0); 10727 bbr_log_type_statechange(bbr, cts, line); 10728 } 10729 10730 static void 10731 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts) 10732 { 10733 /* 10734 * Sanity check on probe-rtt intervals. 10735 * In crazy situations where we are competing 10736 * against new-reno flows with huge buffers 10737 * our rtt-prop interval could come to dominate 10738 * things if we can't get through a full set 10739 * of cycles, we need to adjust it. 10740 */ 10741 if (bbr_can_adjust_probertt && 10742 (bbr->rc_use_google == 0)) { 10743 uint16_t val = 0; 10744 uint32_t cur_rttp, fval, newval, baseval; 10745 10746 /* Are we to small and go into probe-rtt to often? */ 10747 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1)); 10748 cur_rttp = roundup(baseval, USECS_IN_SECOND); 10749 fval = bbr_filter_len_sec * USECS_IN_SECOND; 10750 if (bbr_is_ratio == 0) { 10751 if (fval > bbr_rtt_probe_limit) 10752 newval = cur_rttp + (fval - bbr_rtt_probe_limit); 10753 else 10754 newval = cur_rttp; 10755 } else { 10756 int mul; 10757 10758 mul = fval / bbr_rtt_probe_limit; 10759 newval = cur_rttp * mul; 10760 } 10761 if (cur_rttp > bbr->r_ctl.rc_probertt_int) { 10762 bbr->r_ctl.rc_probertt_int = cur_rttp; 10763 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10764 val = 1; 10765 } else { 10766 /* 10767 * No adjustments were made 10768 * do we need to shrink it? 10769 */ 10770 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) { 10771 if (cur_rttp <= bbr_rtt_probe_limit) { 10772 /* 10773 * Things have calmed down lets 10774 * shrink all the way to default 10775 */ 10776 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10777 reset_time_small(&bbr->r_ctl.rc_rttprop, 10778 (bbr_filter_len_sec * USECS_IN_SECOND)); 10779 cur_rttp = bbr_rtt_probe_limit; 10780 newval = (bbr_filter_len_sec * USECS_IN_SECOND); 10781 val = 2; 10782 } else { 10783 /* 10784 * Well does some adjustment make sense? 10785 */ 10786 if (cur_rttp < bbr->r_ctl.rc_probertt_int) { 10787 /* We can reduce interval time some */ 10788 bbr->r_ctl.rc_probertt_int = cur_rttp; 10789 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10790 val = 3; 10791 } 10792 } 10793 } 10794 } 10795 if (val) 10796 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val); 10797 } 10798 } 10799 10800 static void 10801 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 10802 { 10803 /* Exit probe-rtt */ 10804 10805 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) { 10806 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10807 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10808 } 10809 bbr_log_exit_gain(bbr, cts, 1); 10810 bbr->rc_hit_state_1 = 0; 10811 bbr->r_ctl.rc_rtt_shrinks = cts; 10812 bbr->r_ctl.last_in_probertt = cts; 10813 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0); 10814 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10815 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, 10816 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 10817 bbr->r_ctl.rc_delivered); 10818 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10819 uint32_t time_in; 10820 10821 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10822 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10823 } 10824 if (bbr->rc_filled_pipe) { 10825 /* Switch to probe_bw */ 10826 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 10827 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 10828 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10829 bbr_substate_change(bbr, cts, __LINE__, 0); 10830 bbr_log_type_statechange(bbr, cts, __LINE__); 10831 } else { 10832 /* Back to startup */ 10833 bbr->rc_bbr_state = BBR_STATE_STARTUP; 10834 bbr->r_ctl.rc_bbr_state_time = cts; 10835 /* 10836 * We don't want to give a complete free 3 10837 * measurements until we exit, so we use 10838 * the number of pe's we were in probe-rtt 10839 * to add to the startup_epoch. That way 10840 * we will still retain the old state. 10841 */ 10842 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt); 10843 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10844 /* Make sure to use the lower pg when shifting back in */ 10845 if (bbr->r_ctl.rc_lost && 10846 bbr_use_lower_gain_in_startup && 10847 (bbr->rc_use_google == 0)) 10848 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10849 else 10850 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 10851 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 10852 /* Probably not needed but set it anyway */ 10853 bbr_set_state_target(bbr, __LINE__); 10854 bbr_log_type_statechange(bbr, cts, __LINE__); 10855 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10856 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0); 10857 } 10858 bbr_check_probe_rtt_limits(bbr, cts); 10859 } 10860 10861 static int32_t inline 10862 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts) 10863 { 10864 if ((bbr->rc_past_init_win == 1) && 10865 (bbr->rc_in_persist == 0) && 10866 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) { 10867 return (1); 10868 } 10869 if (bbr_can_force_probertt && 10870 (bbr->rc_in_persist == 0) && 10871 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 10872 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 10873 return (1); 10874 } 10875 return (0); 10876 } 10877 10878 static int32_t 10879 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch) 10880 { 10881 uint64_t btlbw, gain; 10882 if (pkt_epoch == 0) { 10883 /* 10884 * Need to be on a pkt-epoch to continue. 10885 */ 10886 return (0); 10887 } 10888 btlbw = bbr_get_full_bw(bbr); 10889 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10890 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10891 if (btlbw >= gain) { 10892 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 10893 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10894 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 10895 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10896 } 10897 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) 10898 return (1); 10899 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10900 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 10901 return(0); 10902 } 10903 10904 static int32_t inline 10905 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch) 10906 { 10907 /* Have we gained 25% in the last 3 packet based epoch's? */ 10908 uint64_t btlbw, gain; 10909 int do_exit; 10910 int delta, rtt_gain; 10911 10912 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 10913 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 10914 /* 10915 * This qualifies as a RTT_PROBE session since we drop the 10916 * data outstanding to nothing and waited more than 10917 * bbr_rtt_probe_time. 10918 */ 10919 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 10920 bbr_set_reduced_rtt(bbr, cts, __LINE__); 10921 } 10922 if (bbr_should_enter_probe_rtt(bbr, cts)) { 10923 bbr_enter_probe_rtt(bbr, cts, __LINE__); 10924 return (0); 10925 } 10926 if (bbr->rc_use_google) 10927 return (bbr_google_startup(bbr, cts, pkt_epoch)); 10928 10929 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 10930 (bbr_use_lower_gain_in_startup)) { 10931 /* Drop to a lower gain 1.5 x since we saw loss */ 10932 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10933 } 10934 if (pkt_epoch == 0) { 10935 /* 10936 * Need to be on a pkt-epoch to continue. 10937 */ 10938 return (0); 10939 } 10940 if (bbr_rtt_gain_thresh) { 10941 /* 10942 * Do we allow a flow to stay 10943 * in startup with no loss and no 10944 * gain in rtt over a set threshold? 10945 */ 10946 if (bbr->r_ctl.rc_pkt_epoch_rtt && 10947 bbr->r_ctl.startup_last_srtt && 10948 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) { 10949 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt; 10950 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt; 10951 } else 10952 rtt_gain = 0; 10953 if ((bbr->r_ctl.startup_last_srtt == 0) || 10954 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt)) 10955 /* First time or new lower value */ 10956 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 10957 10958 if ((bbr->r_ctl.rc_lost == 0) && 10959 (rtt_gain < bbr_rtt_gain_thresh)) { 10960 /* 10961 * No loss, and we are under 10962 * our gain threhold for 10963 * increasing RTT. 10964 */ 10965 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10966 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10967 bbr_log_startup_event(bbr, cts, rtt_gain, 10968 delta, bbr->r_ctl.startup_last_srtt, 10); 10969 return (0); 10970 } 10971 } 10972 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) && 10973 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) && 10974 (!IN_RECOVERY(bbr->rc_tp->t_flags))) { 10975 /* 10976 * We only assess if we have a new measurement when 10977 * we have no loss and are not in recovery. 10978 * Drag up by one our last_startup epoch so we will hold 10979 * the number of non-gain we have already accumulated. 10980 */ 10981 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10982 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10983 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10984 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9); 10985 return (0); 10986 } 10987 /* Case where we reduced the lost (bad retransmit) */ 10988 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost) 10989 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10990 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count; 10991 btlbw = bbr_get_full_bw(bbr); 10992 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower) 10993 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10994 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10995 else 10996 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10997 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10998 do_exit = 0; 10999 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw) 11000 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 11001 if (btlbw >= gain) { 11002 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 11003 /* Update the lost so we won't exit in next set of tests */ 11004 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11005 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11006 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 11007 } 11008 if ((bbr->rc_loss_exit && 11009 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 11010 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) && 11011 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) { 11012 /* 11013 * If we had no gain, we had loss and that loss was above 11014 * our threshould, the rwnd is not constrained, and we have 11015 * had at least 3 packet epochs exit. Note that this is 11016 * switched off by sysctl. Google does not do this by the 11017 * way. 11018 */ 11019 if ((ctf_flight_size(bbr->rc_tp, 11020 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 11021 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) { 11022 do_exit = 1; 11023 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11024 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4); 11025 } else { 11026 /* Just record an updated loss value */ 11027 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11028 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11029 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5); 11030 } 11031 } else 11032 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11033 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) || 11034 do_exit) { 11035 /* Return 1 to exit the startup state. */ 11036 return (1); 11037 } 11038 /* Stay in startup */ 11039 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11040 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 11041 return (0); 11042 } 11043 11044 static void 11045 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses) 11046 { 11047 /* 11048 * A tick occurred in the rtt epoch do we need to do anything? 11049 */ 11050 #ifdef BBR_INVARIANTS 11051 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) && 11052 (bbr->rc_bbr_state != BBR_STATE_DRAIN) && 11053 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) && 11054 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 11055 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) { 11056 /* Debug code? */ 11057 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state); 11058 } 11059 #endif 11060 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 11061 /* Do we exit the startup state? */ 11062 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) { 11063 uint32_t time_in; 11064 11065 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11066 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6); 11067 bbr->rc_filled_pipe = 1; 11068 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11069 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11070 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11071 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11072 } else 11073 time_in = 0; 11074 if (bbr->rc_no_pacing) 11075 bbr->rc_no_pacing = 0; 11076 bbr->r_ctl.rc_bbr_state_time = cts; 11077 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg; 11078 bbr->rc_bbr_state = BBR_STATE_DRAIN; 11079 bbr_set_state_target(bbr, __LINE__); 11080 if ((bbr->rc_use_google == 0) && 11081 bbr_slam_cwnd_in_main_drain) { 11082 /* Here we don't have to worry about probe-rtt */ 11083 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 11084 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11085 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11086 } 11087 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 11088 bbr_log_type_statechange(bbr, cts, __LINE__); 11089 if (ctf_flight_size(bbr->rc_tp, 11090 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 11091 bbr->r_ctl.rc_target_at_state) { 11092 /* 11093 * Switch to probe_bw if we are already 11094 * there 11095 */ 11096 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11097 bbr_substate_change(bbr, cts, __LINE__, 0); 11098 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11099 bbr_log_type_statechange(bbr, cts, __LINE__); 11100 } 11101 } 11102 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) { 11103 uint32_t inflight; 11104 struct tcpcb *tp; 11105 11106 tp = bbr->rc_tp; 11107 inflight = ctf_flight_size(tp, 11108 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11109 if (inflight >= bbr->r_ctl.rc_target_at_state) { 11110 /* We have reached a flight of the cwnd target */ 11111 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11112 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11113 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 11114 bbr_set_state_target(bbr, __LINE__); 11115 /* 11116 * Rig it so we don't do anything crazy and 11117 * start fresh with a new randomization. 11118 */ 11119 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 11120 bbr->rc_bbr_substate = BBR_SUB_LEVEL6; 11121 bbr_substate_change(bbr, cts, __LINE__, 1); 11122 } 11123 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) { 11124 /* Has in-flight reached the bdp (or less)? */ 11125 uint32_t inflight; 11126 struct tcpcb *tp; 11127 11128 tp = bbr->rc_tp; 11129 inflight = ctf_flight_size(tp, 11130 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11131 if ((bbr->rc_use_google == 0) && 11132 bbr_slam_cwnd_in_main_drain && 11133 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11134 /* 11135 * Here we don't have to worry about probe-rtt 11136 * re-slam it, but keep it slammed down. 11137 */ 11138 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11139 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11140 } 11141 if (inflight <= bbr->r_ctl.rc_target_at_state) { 11142 /* We have drained */ 11143 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11144 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11145 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11146 uint32_t time_in; 11147 11148 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11149 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11150 } 11151 if ((bbr->rc_use_google == 0) && 11152 bbr_slam_cwnd_in_main_drain && 11153 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 11154 /* Restore the cwnd */ 11155 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 11156 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11157 } 11158 /* Setup probe-rtt has being done now RRS-HERE */ 11159 bbr->r_ctl.rc_rtt_shrinks = cts; 11160 bbr->r_ctl.last_in_probertt = cts; 11161 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0); 11162 /* Randomly pick a sub-state */ 11163 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11164 bbr_substate_change(bbr, cts, __LINE__, 0); 11165 bbr_log_type_statechange(bbr, cts, __LINE__); 11166 } 11167 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) { 11168 uint32_t flight; 11169 11170 flight = ctf_flight_size(bbr->rc_tp, 11171 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11172 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered); 11173 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) && 11174 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11175 /* 11176 * We must keep cwnd at the desired MSS. 11177 */ 11178 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 11179 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11180 } else if ((bbr_prtt_slam_cwnd) && 11181 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11182 /* Re-slam it */ 11183 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11184 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11185 } 11186 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) { 11187 /* Has outstanding reached our target? */ 11188 if (flight <= bbr->r_ctl.rc_target_at_state) { 11189 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0); 11190 bbr->r_ctl.rc_bbr_enters_probertt = cts; 11191 /* If time is exactly 0, be 1usec off */ 11192 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) 11193 bbr->r_ctl.rc_bbr_enters_probertt = 1; 11194 if (bbr->rc_use_google == 0) { 11195 /* 11196 * Restore any lowering that as occurred to 11197 * reach here 11198 */ 11199 if (bbr->r_ctl.bbr_rttprobe_gain_val) 11200 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 11201 else 11202 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11203 } 11204 } 11205 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) && 11206 (bbr->rc_use_google == 0) && 11207 bbr->r_ctl.bbr_rttprobe_gain_val && 11208 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) || 11209 (flight >= bbr->r_ctl.flightsize_at_drain))) { 11210 /* 11211 * We have doddled with our current hptsi 11212 * gain an srtt and have still not made it 11213 * to target, or we have increased our flight. 11214 * Lets reduce the gain by xx% 11215 * flooring the reduce at DRAIN (based on 11216 * mul/div) 11217 */ 11218 int red; 11219 11220 bbr->r_ctl.flightsize_at_drain = flight; 11221 bbr->r_ctl.rc_probertt_srttchktim = cts; 11222 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1); 11223 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) { 11224 /* Reduce our gain again */ 11225 bbr->r_ctl.rc_bbr_hptsi_gain -= red; 11226 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0); 11227 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) { 11228 /* one more chance before we give up */ 11229 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 11230 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0); 11231 } else { 11232 /* At the very bottom */ 11233 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1); 11234 } 11235 } 11236 } 11237 if (bbr->r_ctl.rc_bbr_enters_probertt && 11238 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) && 11239 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) { 11240 /* Time to exit probe RTT normally */ 11241 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts); 11242 } 11243 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 11244 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 11245 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 11246 /* 11247 * This qualifies as a RTT_PROBE session since we 11248 * drop the data outstanding to nothing and waited 11249 * more than bbr_rtt_probe_time. 11250 */ 11251 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 11252 bbr_set_reduced_rtt(bbr, cts, __LINE__); 11253 } 11254 if (bbr_should_enter_probe_rtt(bbr, cts)) { 11255 bbr_enter_probe_rtt(bbr, cts, __LINE__); 11256 } else { 11257 bbr_set_probebw_gains(bbr, cts, losses); 11258 } 11259 } 11260 } 11261 11262 static void 11263 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses) 11264 { 11265 int32_t epoch = 0; 11266 11267 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 11268 bbr_set_epoch(bbr, cts, line); 11269 /* At each epoch doe lt bw sampling */ 11270 epoch = 1; 11271 } 11272 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses); 11273 } 11274 11275 static int 11276 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11277 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt, 11278 struct timeval *tv) 11279 { 11280 struct inpcb *inp = tptoinpcb(tp); 11281 struct socket *so = tptosocket(tp); 11282 int32_t thflags, retval; 11283 uint32_t cts, lcts; 11284 uint32_t tiwin; 11285 struct tcpopt to; 11286 struct tcp_bbr *bbr; 11287 struct bbr_sendmap *rsm; 11288 struct timeval ltv; 11289 int32_t did_out = 0; 11290 uint16_t nsegs; 11291 int32_t prev_state; 11292 uint32_t lost; 11293 11294 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11295 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11296 /* add in our stats */ 11297 kern_prefetch(bbr, &prev_state); 11298 prev_state = 0; 11299 thflags = tcp_get_flags(th); 11300 /* 11301 * If this is either a state-changing packet or current state isn't 11302 * established, we require a write lock on tcbinfo. Otherwise, we 11303 * allow the tcbinfo to be in either alocked or unlocked, as the 11304 * caller may have unnecessarily acquired a write lock due to a 11305 * race. 11306 */ 11307 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11308 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 11309 __func__)); 11310 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 11311 __func__)); 11312 11313 tp->t_rcvtime = ticks; 11314 /* 11315 * Unscale the window into a 32-bit value. For the SYN_SENT state 11316 * the scale is zero. 11317 */ 11318 tiwin = th->th_win << tp->snd_scale; 11319 #ifdef STATS 11320 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 11321 #endif 11322 11323 if (m->m_flags & M_TSTMP) { 11324 /* Prefer the hardware timestamp if present */ 11325 struct timespec ts; 11326 11327 mbuf_tstmp2timespec(m, &ts); 11328 bbr->rc_tv.tv_sec = ts.tv_sec; 11329 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11330 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11331 } else if (m->m_flags & M_TSTMP_LRO) { 11332 /* Next the arrival timestamp */ 11333 struct timespec ts; 11334 11335 mbuf_tstmp2timespec(m, &ts); 11336 bbr->rc_tv.tv_sec = ts.tv_sec; 11337 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11338 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11339 } else { 11340 /* 11341 * Ok just get the current time. 11342 */ 11343 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv); 11344 } 11345 /* 11346 * Parse options on any incoming segment. 11347 */ 11348 tcp_dooptions(&to, (u_char *)(th + 1), 11349 (th->th_off << 2) - sizeof(struct tcphdr), 11350 (thflags & TH_SYN) ? TO_SYN : 0); 11351 if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) { 11352 /* 11353 * We don't look at sack's from the 11354 * peer because the MSS is too small which 11355 * can subject us to an attack. 11356 */ 11357 to.to_flags &= ~TOF_SACK; 11358 } 11359 /* 11360 * If timestamps were negotiated during SYN/ACK and a 11361 * segment without a timestamp is received, silently drop 11362 * the segment, unless it is a RST segment or missing timestamps are 11363 * tolerated. 11364 * See section 3.2 of RFC 7323. 11365 */ 11366 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 11367 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 11368 retval = 0; 11369 m_freem(m); 11370 goto done_with_input; 11371 } 11372 /* 11373 * If echoed timestamp is later than the current time, fall back to 11374 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 11375 * were used when this connection was established. 11376 */ 11377 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 11378 to.to_tsecr -= tp->ts_offset; 11379 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv))) 11380 to.to_tsecr = 0; 11381 } 11382 /* 11383 * If its the first time in we need to take care of options and 11384 * verify we can do SACK for rack! 11385 */ 11386 if (bbr->r_state == 0) { 11387 /* 11388 * Process options only when we get SYN/ACK back. The SYN 11389 * case for incoming connections is handled in tcp_syncache. 11390 * According to RFC1323 the window field in a SYN (i.e., a 11391 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 11392 * this is traditional behavior, may need to be cleaned up. 11393 */ 11394 if (bbr->rc_inp == NULL) { 11395 bbr->rc_inp = inp; 11396 } 11397 /* 11398 * We need to init rc_inp here since its not init'd when 11399 * bbr_init is called 11400 */ 11401 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 11402 if ((to.to_flags & TOF_SCALE) && 11403 (tp->t_flags & TF_REQ_SCALE)) { 11404 tp->t_flags |= TF_RCVD_SCALE; 11405 tp->snd_scale = to.to_wscale; 11406 } else 11407 tp->t_flags &= ~TF_REQ_SCALE; 11408 /* 11409 * Initial send window. It will be updated with the 11410 * next incoming segment to the scaled value. 11411 */ 11412 tp->snd_wnd = th->th_win; 11413 if ((to.to_flags & TOF_TS) && 11414 (tp->t_flags & TF_REQ_TSTMP)) { 11415 tp->t_flags |= TF_RCVD_TSTMP; 11416 tp->ts_recent = to.to_tsval; 11417 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 11418 } else 11419 tp->t_flags &= ~TF_REQ_TSTMP; 11420 if (to.to_flags & TOF_MSS) 11421 tcp_mss(tp, to.to_mss); 11422 if ((tp->t_flags & TF_SACK_PERMIT) && 11423 (to.to_flags & TOF_SACKPERM) == 0) 11424 tp->t_flags &= ~TF_SACK_PERMIT; 11425 if (tp->t_flags & TF_FASTOPEN) { 11426 if (to.to_flags & TOF_FASTOPEN) { 11427 uint16_t mss; 11428 11429 if (to.to_flags & TOF_MSS) 11430 mss = to.to_mss; 11431 else 11432 if ((inp->inp_vflag & INP_IPV6) != 0) 11433 mss = TCP6_MSS; 11434 else 11435 mss = TCP_MSS; 11436 tcp_fastopen_update_cache(tp, mss, 11437 to.to_tfo_len, to.to_tfo_cookie); 11438 } else 11439 tcp_fastopen_disable_path(tp); 11440 } 11441 } 11442 /* 11443 * At this point we are at the initial call. Here we decide 11444 * if we are doing RACK or not. We do this by seeing if 11445 * TF_SACK_PERMIT is set, if not rack is *not* possible and 11446 * we switch to the default code. 11447 */ 11448 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 11449 /* Bail */ 11450 tcp_switch_back_to_default(tp); 11451 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen, 11452 tlen, iptos); 11453 return (1); 11454 } 11455 /* Set the flag */ 11456 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 11457 tcp_set_hpts(tp); 11458 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack); 11459 } 11460 if (thflags & TH_ACK) { 11461 /* Track ack types */ 11462 if (to.to_flags & TOF_SACK) 11463 BBR_STAT_INC(bbr_acks_with_sacks); 11464 else 11465 BBR_STAT_INC(bbr_plain_acks); 11466 } 11467 /* 11468 * This is the one exception case where we set the rack state 11469 * always. All other times (timers etc) we must have a rack-state 11470 * set (so we assure we have done the checks above for SACK). 11471 */ 11472 if (thflags & TH_FIN) 11473 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 11474 if (bbr->r_state != tp->t_state) 11475 bbr_set_state(tp, bbr, tiwin); 11476 11477 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL) 11478 kern_prefetch(rsm, &prev_state); 11479 prev_state = bbr->r_state; 11480 bbr->rc_ack_was_delayed = 0; 11481 lost = bbr->r_ctl.rc_lost; 11482 bbr->rc_is_pkt_epoch_now = 0; 11483 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) { 11484 /* Get the real time into lcts and figure the real delay */ 11485 lcts = tcp_get_usecs(<v); 11486 if (TSTMP_GT(lcts, cts)) { 11487 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts; 11488 bbr->rc_ack_was_delayed = 1; 11489 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay, 11490 bbr->r_ctl.highest_hdwr_delay)) 11491 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay; 11492 } else { 11493 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11494 bbr->rc_ack_was_delayed = 0; 11495 } 11496 } else { 11497 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11498 bbr->rc_ack_was_delayed = 0; 11499 } 11500 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m); 11501 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 11502 retval = 0; 11503 m_freem(m); 11504 goto done_with_input; 11505 } 11506 /* 11507 * If a segment with the ACK-bit set arrives in the SYN-SENT state 11508 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 11509 */ 11510 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 11511 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 11512 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11513 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11514 return (1); 11515 } 11516 if (tiwin > bbr->r_ctl.rc_high_rwnd) 11517 bbr->r_ctl.rc_high_rwnd = tiwin; 11518 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp, 11519 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11520 bbr->rtt_valid = 0; 11521 if (to.to_flags & TOF_TS) { 11522 bbr->rc_ts_valid = 1; 11523 bbr->r_ctl.last_inbound_ts = to.to_tsval; 11524 } else { 11525 bbr->rc_ts_valid = 0; 11526 bbr->r_ctl.last_inbound_ts = 0; 11527 } 11528 retval = (*bbr->r_substate) (m, th, so, 11529 tp, &to, drop_hdrlen, 11530 tlen, tiwin, thflags, nxt_pkt, iptos); 11531 if (nxt_pkt == 0) 11532 BBR_STAT_INC(bbr_rlock_left_ret0); 11533 else 11534 BBR_STAT_INC(bbr_rlock_left_ret1); 11535 if (retval == 0) { 11536 /* 11537 * If retval is 1 the tcb is unlocked and most likely the tp 11538 * is gone. 11539 */ 11540 INP_WLOCK_ASSERT(inp); 11541 tcp_bbr_xmit_timer_commit(bbr, tp, cts); 11542 if (bbr->rc_is_pkt_epoch_now) 11543 bbr_set_pktepoch(bbr, cts, __LINE__); 11544 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost)); 11545 if (nxt_pkt == 0) { 11546 if ((bbr->r_wanted_output != 0) || 11547 (tp->t_flags & TF_ACKNOW)) { 11548 11549 bbr->rc_output_starts_timer = 0; 11550 did_out = 1; 11551 if (tcp_output(tp) < 0) 11552 return (1); 11553 } else 11554 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0); 11555 } 11556 if ((nxt_pkt == 0) && 11557 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 11558 (SEQ_GT(tp->snd_max, tp->snd_una) || 11559 (tp->t_flags & TF_DELACK) || 11560 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 11561 (tp->t_state <= TCPS_CLOSING)))) { 11562 /* 11563 * We could not send (probably in the hpts but 11564 * stopped the timer)? 11565 */ 11566 if ((tp->snd_max == tp->snd_una) && 11567 ((tp->t_flags & TF_DELACK) == 0) && 11568 (tcp_in_hpts(tp)) && 11569 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11570 /* 11571 * keep alive not needed if we are hptsi 11572 * output yet 11573 */ 11574 ; 11575 } else { 11576 if (tcp_in_hpts(tp)) { 11577 tcp_hpts_remove(tp); 11578 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11579 (TSTMP_GT(lcts, bbr->rc_pacer_started))) { 11580 uint32_t del; 11581 11582 del = lcts - bbr->rc_pacer_started; 11583 if (bbr->r_ctl.rc_last_delay_val > del) { 11584 BBR_STAT_INC(bbr_force_timer_start); 11585 bbr->r_ctl.rc_last_delay_val -= del; 11586 bbr->rc_pacer_started = lcts; 11587 } else { 11588 /* We are late */ 11589 bbr->r_ctl.rc_last_delay_val = 0; 11590 BBR_STAT_INC(bbr_force_output); 11591 if (tcp_output(tp) < 0) 11592 return (1); 11593 } 11594 } 11595 } 11596 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val, 11597 0); 11598 } 11599 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) { 11600 /* Do we have the correct timer running? */ 11601 bbr_timer_audit(tp, bbr, lcts, &so->so_snd); 11602 } 11603 /* Clear the flag, it may have been cleared by output but we may not have */ 11604 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS)) 11605 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11606 /* Do we have a new state */ 11607 if (bbr->r_state != tp->t_state) 11608 bbr_set_state(tp, bbr, tiwin); 11609 done_with_input: 11610 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out); 11611 if (did_out) 11612 bbr->r_wanted_output = 0; 11613 } 11614 return (retval); 11615 } 11616 11617 static void 11618 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11619 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 11620 { 11621 struct timeval tv; 11622 int retval; 11623 11624 /* First lets see if we have old packets */ 11625 if (!STAILQ_EMPTY(&tp->t_inqueue)) { 11626 if (ctf_do_queued_segments(tp, 1)) { 11627 m_freem(m); 11628 return; 11629 } 11630 } 11631 if (m->m_flags & M_TSTMP_LRO) { 11632 mbuf_tstmp2timeval(m, &tv); 11633 } else { 11634 /* Should not be should we kassert instead? */ 11635 tcp_get_usecs(&tv); 11636 } 11637 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 11638 0, &tv); 11639 if (retval == 0) { 11640 INP_WUNLOCK(tptoinpcb(tp)); 11641 } 11642 } 11643 11644 /* 11645 * Return how much data can be sent without violating the 11646 * cwnd or rwnd. 11647 */ 11648 11649 static inline uint32_t 11650 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin, 11651 uint32_t avail, int32_t sb_offset, uint32_t cts) 11652 { 11653 uint32_t len; 11654 11655 if (ctf_outstanding(tp) >= tp->snd_wnd) { 11656 /* We never want to go over our peers rcv-window */ 11657 len = 0; 11658 } else { 11659 uint32_t flight; 11660 11661 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11662 if (flight >= sendwin) { 11663 /* 11664 * We have in flight what we are allowed by cwnd (if 11665 * it was rwnd blocking it would have hit above out 11666 * >= tp->snd_wnd). 11667 */ 11668 return (0); 11669 } 11670 len = sendwin - flight; 11671 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 11672 /* We would send too much (beyond the rwnd) */ 11673 len = tp->snd_wnd - ctf_outstanding(tp); 11674 } 11675 if ((len + sb_offset) > avail) { 11676 /* 11677 * We don't have that much in the SB, how much is 11678 * there? 11679 */ 11680 len = avail - sb_offset; 11681 } 11682 } 11683 return (len); 11684 } 11685 11686 static inline void 11687 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11688 { 11689 if (error) { 11690 return; 11691 } 11692 if (rsm) { 11693 if (rsm->r_flags & BBR_TLP) { 11694 /* 11695 * TLP should not count in retran count, but in its 11696 * own bin 11697 */ 11698 KMOD_TCPSTAT_INC(tcps_tlpresends); 11699 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len); 11700 } else { 11701 /* Retransmit */ 11702 tp->t_sndrexmitpack++; 11703 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 11704 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 11705 #ifdef STATS 11706 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 11707 len); 11708 #endif 11709 } 11710 /* 11711 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is 11712 * sub-state 11713 */ 11714 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len); 11715 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) { 11716 /* Non probe_bw log in 1, 2, or 4. */ 11717 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len); 11718 } else { 11719 /* 11720 * Log our probe state 3, and log also 5-13 to show 11721 * us the recovery sub-state for the send. This 11722 * means that 3 == (5+6+7+8+9+10+11+12+13) 11723 */ 11724 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len); 11725 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len); 11726 } 11727 /* Place in both 16's the totals of retransmitted */ 11728 counter_u64_add(bbr_state_lost[16], len); 11729 counter_u64_add(bbr_state_resend[16], len); 11730 /* Place in 17's the total sent */ 11731 counter_u64_add(bbr_state_resend[17], len); 11732 counter_u64_add(bbr_state_lost[17], len); 11733 11734 } else { 11735 /* New sends */ 11736 KMOD_TCPSTAT_INC(tcps_sndpack); 11737 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 11738 /* Place in 17's the total sent */ 11739 counter_u64_add(bbr_state_resend[17], len); 11740 counter_u64_add(bbr_state_lost[17], len); 11741 #ifdef STATS 11742 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 11743 len); 11744 #endif 11745 } 11746 } 11747 11748 static void 11749 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level) 11750 { 11751 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) { 11752 /* 11753 * Limit the cwnd to not be above N x the target plus whats 11754 * is outstanding. The target is based on the current b/w 11755 * estimate. 11756 */ 11757 uint32_t target; 11758 11759 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT); 11760 target += ctf_outstanding(tp); 11761 target *= bbr_target_cwnd_mult_limit; 11762 if (tp->snd_cwnd > target) 11763 tp->snd_cwnd = target; 11764 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__); 11765 } 11766 } 11767 11768 static int 11769 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg) 11770 { 11771 /* 11772 * "adv" is the amount we could increase the window, taking into 11773 * account that we are limited by TCP_MAXWIN << tp->rcv_scale. 11774 */ 11775 int32_t adv; 11776 int32_t oldwin; 11777 11778 adv = recwin; 11779 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 11780 oldwin = (tp->rcv_adv - tp->rcv_nxt); 11781 if (adv > oldwin) 11782 adv -= oldwin; 11783 else { 11784 /* We can't increase the window */ 11785 adv = 0; 11786 } 11787 } else 11788 oldwin = 0; 11789 11790 /* 11791 * If the new window size ends up being the same as or less 11792 * than the old size when it is scaled, then don't force 11793 * a window update. 11794 */ 11795 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 11796 return (0); 11797 11798 if (adv >= (2 * maxseg) && 11799 (adv >= (so->so_rcv.sb_hiwat / 4) || 11800 recwin <= (so->so_rcv.sb_hiwat / 8) || 11801 so->so_rcv.sb_hiwat <= 8 * maxseg)) { 11802 return (1); 11803 } 11804 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 11805 return (1); 11806 return (0); 11807 } 11808 11809 /* 11810 * Return 0 on success and a errno on failure to send. 11811 * Note that a 0 return may not mean we sent anything 11812 * if the TCB was on the hpts. A non-zero return 11813 * does indicate the error we got from ip[6]_output. 11814 */ 11815 static int 11816 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv) 11817 { 11818 struct socket *so; 11819 int32_t len; 11820 uint32_t cts; 11821 uint32_t recwin, sendwin; 11822 int32_t sb_offset; 11823 int32_t flags, abandon, error = 0; 11824 struct tcp_log_buffer *lgb; 11825 struct mbuf *m; 11826 struct mbuf *mb; 11827 uint32_t if_hw_tsomaxsegcount = 0; 11828 uint32_t if_hw_tsomaxsegsize = 0; 11829 uint32_t if_hw_tsomax = 0; 11830 struct ip *ip = NULL; 11831 struct tcp_bbr *bbr; 11832 struct tcphdr *th; 11833 struct udphdr *udp = NULL; 11834 u_char opt[TCP_MAXOLEN]; 11835 unsigned ipoptlen, optlen, hdrlen; 11836 unsigned ulen; 11837 uint32_t bbr_seq; 11838 uint32_t delay_calc=0; 11839 uint8_t doing_tlp = 0; 11840 uint8_t local_options; 11841 #ifdef BBR_INVARIANTS 11842 uint8_t doing_retran_from = 0; 11843 uint8_t picked_up_retran = 0; 11844 #endif 11845 uint8_t wanted_cookie = 0; 11846 uint8_t more_to_rxt=0; 11847 int32_t prefetch_so_done = 0; 11848 int32_t prefetch_rsm = 0; 11849 uint32_t tot_len = 0; 11850 uint32_t maxseg, pace_max_segs, p_maxseg; 11851 int32_t csum_flags = 0; 11852 int32_t hw_tls; 11853 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 11854 unsigned ipsec_optlen = 0; 11855 11856 #endif 11857 volatile int32_t sack_rxmit; 11858 struct bbr_sendmap *rsm = NULL; 11859 int32_t tso, mtu; 11860 struct tcpopt to; 11861 int32_t slot = 0; 11862 struct inpcb *inp; 11863 struct sockbuf *sb; 11864 bool hpts_calling; 11865 #ifdef INET6 11866 struct ip6_hdr *ip6 = NULL; 11867 int32_t isipv6; 11868 #endif 11869 uint8_t app_limited = BBR_JR_SENT_DATA; 11870 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11871 /* We take a cache hit here */ 11872 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval)); 11873 cts = tcp_tv_to_usectick(&bbr->rc_tv); 11874 inp = bbr->rc_inp; 11875 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS); 11876 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11877 so = inp->inp_socket; 11878 sb = &so->so_snd; 11879 if (tp->t_nic_ktls_xmit) 11880 hw_tls = 1; 11881 else 11882 hw_tls = 0; 11883 kern_prefetch(sb, &maxseg); 11884 maxseg = tp->t_maxseg - bbr->rc_last_options; 11885 if (bbr_minseg(bbr) < maxseg) { 11886 tcp_bbr_tso_size_check(bbr, cts); 11887 } 11888 /* Remove any flags that indicate we are pacing on the inp */ 11889 pace_max_segs = bbr->r_ctl.rc_pace_max_segs; 11890 p_maxseg = min(maxseg, pace_max_segs); 11891 INP_WLOCK_ASSERT(inp); 11892 #ifdef TCP_OFFLOAD 11893 if (tp->t_flags & TF_TOE) 11894 return (tcp_offload_output(tp)); 11895 #endif 11896 11897 #ifdef INET6 11898 if (bbr->r_state) { 11899 /* Use the cache line loaded if possible */ 11900 isipv6 = bbr->r_is_v6; 11901 } else { 11902 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 11903 } 11904 #endif 11905 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 11906 tcp_in_hpts(tp)) { 11907 /* 11908 * We are on the hpts for some timer but not hptsi output. 11909 * Possibly remove from the hpts so we can send/recv etc. 11910 */ 11911 if ((tp->t_flags & TF_ACKNOW) == 0) { 11912 /* 11913 * No immediate demand right now to send an ack, but 11914 * the user may have read, making room for new data 11915 * (a window update). If so we may want to cancel 11916 * whatever timer is running (KEEP/DEL-ACK?) and 11917 * continue to send out a window update. Or we may 11918 * have gotten more data into the socket buffer to 11919 * send. 11920 */ 11921 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 11922 (long)TCP_MAXWIN << tp->rcv_scale); 11923 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) && 11924 ((tcp_outflags[tp->t_state] & TH_RST) == 0) && 11925 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <= 11926 (tp->snd_max - tp->snd_una))) { 11927 /* 11928 * Nothing new to send and no window update 11929 * is needed to send. Lets just return and 11930 * let the timer-run off. 11931 */ 11932 return (0); 11933 } 11934 } 11935 tcp_hpts_remove(tp); 11936 bbr_timer_cancel(bbr, __LINE__, cts); 11937 } 11938 if (bbr->r_ctl.rc_last_delay_val) { 11939 /* Calculate a rough delay for early escape to sending */ 11940 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11941 delay_calc = cts - bbr->rc_pacer_started; 11942 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11943 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11944 else 11945 delay_calc = 0; 11946 } 11947 /* Mark that we have called bbr_output(). */ 11948 if ((bbr->r_timer_override) || 11949 (tp->t_state < TCPS_ESTABLISHED)) { 11950 /* Timeouts or early states are exempt */ 11951 if (tcp_in_hpts(tp)) 11952 tcp_hpts_remove(tp); 11953 } else if (tcp_in_hpts(tp)) { 11954 if ((bbr->r_ctl.rc_last_delay_val) && 11955 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11956 delay_calc) { 11957 /* 11958 * We were being paced for output and the delay has 11959 * already exceeded when we were supposed to be 11960 * called, lets go ahead and pull out of the hpts 11961 * and call output. 11962 */ 11963 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1); 11964 bbr->r_ctl.rc_last_delay_val = 0; 11965 tcp_hpts_remove(tp); 11966 } else if (tp->t_state == TCPS_CLOSED) { 11967 bbr->r_ctl.rc_last_delay_val = 0; 11968 tcp_hpts_remove(tp); 11969 } else { 11970 /* 11971 * On the hpts, you shall not pass! even if ACKNOW 11972 * is on, we will when the hpts fires, unless of 11973 * course we are overdue. 11974 */ 11975 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1); 11976 return (0); 11977 } 11978 } 11979 bbr->rc_cwnd_limited = 0; 11980 if (bbr->r_ctl.rc_last_delay_val) { 11981 /* recalculate the real delay and deal with over/under */ 11982 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11983 delay_calc = cts - bbr->rc_pacer_started; 11984 else 11985 delay_calc = 0; 11986 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11987 /* Setup the delay which will be added in */ 11988 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11989 else { 11990 /* 11991 * We are early setup to adjust 11992 * our slot time. 11993 */ 11994 uint64_t merged_val; 11995 11996 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc); 11997 bbr->r_agg_early_set = 1; 11998 if (bbr->r_ctl.rc_hptsi_agg_delay) { 11999 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) { 12000 /* Nope our previous late cancels out the early */ 12001 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early; 12002 bbr->r_agg_early_set = 0; 12003 bbr->r_ctl.rc_agg_early = 0; 12004 } else { 12005 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay; 12006 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12007 } 12008 } 12009 merged_val = bbr->rc_pacer_started; 12010 merged_val <<= 32; 12011 merged_val |= bbr->r_ctl.rc_last_delay_val; 12012 bbr_log_pacing_delay_calc(bbr, hpts_calling, 12013 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val, 12014 bbr->r_agg_early_set, 3); 12015 bbr->r_ctl.rc_last_delay_val = 0; 12016 BBR_STAT_INC(bbr_early); 12017 delay_calc = 0; 12018 } 12019 } else { 12020 /* We were not delayed due to hptsi */ 12021 if (bbr->r_agg_early_set) 12022 bbr->r_ctl.rc_agg_early = 0; 12023 bbr->r_agg_early_set = 0; 12024 delay_calc = 0; 12025 } 12026 if (delay_calc) { 12027 /* 12028 * We had a hptsi delay which means we are falling behind on 12029 * sending at the expected rate. Calculate an extra amount 12030 * of data we can send, if any, to put us back on track. 12031 */ 12032 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay) 12033 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff; 12034 else 12035 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc; 12036 } 12037 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12038 if ((tp->snd_una == tp->snd_max) && 12039 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 12040 (sbavail(sb))) { 12041 /* 12042 * Ok we have been idle with nothing outstanding 12043 * we possibly need to start fresh with either a new 12044 * suite of states or a fast-ramp up. 12045 */ 12046 bbr_restart_after_idle(bbr, 12047 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time)); 12048 } 12049 /* 12050 * Now was there a hptsi delay where we are behind? We only count 12051 * being behind if: a) We are not in recovery. b) There was a delay. 12052 * <and> c) We had room to send something. 12053 * 12054 */ 12055 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 12056 int retval; 12057 12058 retval = bbr_process_timers(tp, bbr, cts, hpts_calling); 12059 if (retval != 0) { 12060 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1); 12061 /* 12062 * If timers want tcp_drop(), then pass error out, 12063 * otherwise suppress it. 12064 */ 12065 return (retval < 0 ? retval : 0); 12066 } 12067 } 12068 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 12069 if (hpts_calling && 12070 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 12071 bbr->r_ctl.rc_last_delay_val = 0; 12072 } 12073 bbr->r_timer_override = 0; 12074 bbr->r_wanted_output = 0; 12075 /* 12076 * For TFO connections in SYN_RECEIVED, only allow the initial 12077 * SYN|ACK and those sent by the retransmit timer. 12078 */ 12079 if ((tp->t_flags & TF_FASTOPEN) && 12080 ((tp->t_state == TCPS_SYN_RECEIVED) || 12081 (tp->t_state == TCPS_SYN_SENT)) && 12082 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 12083 (tp->t_rxtshift == 0)) { /* not a retransmit */ 12084 len = 0; 12085 goto just_return_nolock; 12086 } 12087 /* 12088 * Before sending anything check for a state update. For hpts 12089 * calling without input this is important. If its input calling 12090 * then this was already done. 12091 */ 12092 if (bbr->rc_use_google == 0) 12093 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12094 again: 12095 /* 12096 * If we've recently taken a timeout, snd_max will be greater than 12097 * snd_max. BBR in general does not pay much attention to snd_nxt 12098 * for historic reasons the persist timer still uses it. This means 12099 * we have to look at it. All retransmissions that are not persits 12100 * use the rsm that needs to be sent so snd_nxt is ignored. At the 12101 * end of this routine we pull snd_nxt always up to snd_max. 12102 */ 12103 doing_tlp = 0; 12104 #ifdef BBR_INVARIANTS 12105 doing_retran_from = picked_up_retran = 0; 12106 #endif 12107 error = 0; 12108 tso = 0; 12109 slot = 0; 12110 mtu = 0; 12111 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12112 sb_offset = tp->snd_max - tp->snd_una; 12113 flags = tcp_outflags[tp->t_state]; 12114 sack_rxmit = 0; 12115 len = 0; 12116 rsm = NULL; 12117 if (flags & TH_RST) { 12118 SOCK_SENDBUF_LOCK(so); 12119 goto send; 12120 } 12121 recheck_resend: 12122 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 12123 /* We need to always have one in reserve */ 12124 rsm = bbr_alloc(bbr); 12125 if (rsm == NULL) { 12126 error = ENOMEM; 12127 /* Lie to get on the hpts */ 12128 tot_len = tp->t_maxseg; 12129 if (hpts_calling) 12130 /* Retry in a ms */ 12131 slot = 1001; 12132 goto just_return_nolock; 12133 } 12134 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 12135 bbr->r_ctl.rc_free_cnt++; 12136 rsm = NULL; 12137 } 12138 /* What do we send, a resend? */ 12139 if (bbr->r_ctl.rc_resend == NULL) { 12140 /* Check for rack timeout */ 12141 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 12142 if (bbr->r_ctl.rc_resend) { 12143 #ifdef BBR_INVARIANTS 12144 picked_up_retran = 1; 12145 #endif 12146 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend); 12147 } 12148 } 12149 if (bbr->r_ctl.rc_resend) { 12150 rsm = bbr->r_ctl.rc_resend; 12151 #ifdef BBR_INVARIANTS 12152 doing_retran_from = 1; 12153 #endif 12154 /* Remove any TLP flags its a RACK or T-O */ 12155 rsm->r_flags &= ~BBR_TLP; 12156 bbr->r_ctl.rc_resend = NULL; 12157 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 12158 #ifdef BBR_INVARIANTS 12159 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n", 12160 tp, bbr, rsm, rsm->r_start, tp->snd_una); 12161 goto recheck_resend; 12162 #else 12163 /* TSNH */ 12164 rsm = NULL; 12165 goto recheck_resend; 12166 #endif 12167 } 12168 if (rsm->r_flags & BBR_HAS_SYN) { 12169 /* Only retransmit a SYN by itself */ 12170 len = 0; 12171 if ((flags & TH_SYN) == 0) { 12172 /* Huh something is wrong */ 12173 rsm->r_start++; 12174 if (rsm->r_start == rsm->r_end) { 12175 /* Clean it up, somehow we missed the ack? */ 12176 bbr_log_syn(tp, NULL); 12177 } else { 12178 /* TFO with data? */ 12179 rsm->r_flags &= ~BBR_HAS_SYN; 12180 len = rsm->r_end - rsm->r_start; 12181 } 12182 } else { 12183 /* Retransmitting SYN */ 12184 rsm = NULL; 12185 SOCK_SENDBUF_LOCK(so); 12186 goto send; 12187 } 12188 } else 12189 len = rsm->r_end - rsm->r_start; 12190 if ((bbr->rc_resends_use_tso == 0) && 12191 (len > maxseg)) { 12192 len = maxseg; 12193 more_to_rxt = 1; 12194 } 12195 sb_offset = rsm->r_start - tp->snd_una; 12196 if (len > 0) { 12197 sack_rxmit = 1; 12198 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 12199 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 12200 min(len, maxseg)); 12201 } else { 12202 /* I dont think this can happen */ 12203 rsm = NULL; 12204 goto recheck_resend; 12205 } 12206 BBR_STAT_INC(bbr_resends_set); 12207 } else if (bbr->r_ctl.rc_tlp_send) { 12208 /* 12209 * Tail loss probe 12210 */ 12211 doing_tlp = 1; 12212 rsm = bbr->r_ctl.rc_tlp_send; 12213 bbr->r_ctl.rc_tlp_send = NULL; 12214 sack_rxmit = 1; 12215 len = rsm->r_end - rsm->r_start; 12216 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12217 len = maxseg; 12218 12219 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 12220 #ifdef BBR_INVARIANTS 12221 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u", 12222 tp, bbr, tp->snd_una, rsm, rsm->r_start); 12223 #else 12224 /* TSNH */ 12225 rsm = NULL; 12226 goto recheck_resend; 12227 #endif 12228 } 12229 sb_offset = rsm->r_start - tp->snd_una; 12230 BBR_STAT_INC(bbr_tlp_set); 12231 } 12232 /* 12233 * Enforce a connection sendmap count limit if set 12234 * as long as we are not retransmiting. 12235 */ 12236 if ((rsm == NULL) && 12237 (V_tcp_map_entries_limit > 0) && 12238 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 12239 BBR_STAT_INC(bbr_alloc_limited); 12240 if (!bbr->alloc_limit_reported) { 12241 bbr->alloc_limit_reported = 1; 12242 BBR_STAT_INC(bbr_alloc_limited_conns); 12243 } 12244 goto just_return_nolock; 12245 } 12246 #ifdef BBR_INVARIANTS 12247 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) { 12248 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u", 12249 tp, bbr, rsm, sb_offset, len); 12250 } 12251 #endif 12252 /* 12253 * Get standard flags, and add SYN or FIN if requested by 'hidden' 12254 * state flags. 12255 */ 12256 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL)) 12257 flags |= TH_FIN; 12258 if (tp->t_flags & TF_NEEDSYN) 12259 flags |= TH_SYN; 12260 12261 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) { 12262 /* we are retransmitting the fin */ 12263 len--; 12264 if (len) { 12265 /* 12266 * When retransmitting data do *not* include the 12267 * FIN. This could happen from a TLP probe if we 12268 * allowed data with a FIN. 12269 */ 12270 flags &= ~TH_FIN; 12271 } 12272 } else if (rsm) { 12273 if (flags & TH_FIN) 12274 flags &= ~TH_FIN; 12275 } 12276 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 12277 void *end_rsm; 12278 12279 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 12280 if (end_rsm) 12281 kern_prefetch(end_rsm, &prefetch_rsm); 12282 prefetch_rsm = 1; 12283 } 12284 SOCK_SENDBUF_LOCK(so); 12285 /* 12286 * If snd_nxt == snd_max and we have transmitted a FIN, the 12287 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 12288 * negative length. This can also occur when TCP opens up its 12289 * congestion window while receiving additional duplicate acks after 12290 * fast-retransmit because TCP will reset snd_nxt to snd_max after 12291 * the fast-retransmit. 12292 * 12293 * In the normal retransmit-FIN-only case, however, snd_nxt will be 12294 * set to snd_una, the sb_offset will be 0, and the length may wind 12295 * up 0. 12296 * 12297 * If sack_rxmit is true we are retransmitting from the scoreboard 12298 * in which case len is already set. 12299 */ 12300 if (sack_rxmit == 0) { 12301 uint32_t avail; 12302 12303 avail = sbavail(sb); 12304 if (SEQ_GT(tp->snd_max, tp->snd_una)) 12305 sb_offset = tp->snd_max - tp->snd_una; 12306 else 12307 sb_offset = 0; 12308 if (bbr->rc_tlp_new_data) { 12309 /* TLP is forcing out new data */ 12310 uint32_t tlplen; 12311 12312 doing_tlp = 1; 12313 tlplen = maxseg; 12314 12315 if (tlplen > (uint32_t)(avail - sb_offset)) { 12316 tlplen = (uint32_t)(avail - sb_offset); 12317 } 12318 if (tlplen > tp->snd_wnd) { 12319 len = tp->snd_wnd; 12320 } else { 12321 len = tlplen; 12322 } 12323 bbr->rc_tlp_new_data = 0; 12324 } else { 12325 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts); 12326 if ((len < p_maxseg) && 12327 (bbr->rc_in_persist == 0) && 12328 (ctf_outstanding(tp) >= (2 * p_maxseg)) && 12329 ((avail - sb_offset) >= p_maxseg)) { 12330 /* 12331 * We are not completing whats in the socket 12332 * buffer (i.e. there is at least a segment 12333 * waiting to send) and we have 2 or more 12334 * segments outstanding. There is no sense 12335 * of sending a little piece. Lets defer and 12336 * and wait until we can send a whole 12337 * segment. 12338 */ 12339 len = 0; 12340 } 12341 if (bbr->rc_in_persist) { 12342 /* 12343 * We are in persists, figure out if 12344 * a retransmit is available (maybe the previous 12345 * persists we sent) or if we have to send new 12346 * data. 12347 */ 12348 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 12349 if (rsm) { 12350 len = rsm->r_end - rsm->r_start; 12351 if (rsm->r_flags & BBR_HAS_FIN) 12352 len--; 12353 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12354 len = maxseg; 12355 if (len > 1) 12356 BBR_STAT_INC(bbr_persist_reneg); 12357 /* 12358 * XXXrrs we could force the len to 12359 * 1 byte here to cause the chunk to 12360 * split apart.. but that would then 12361 * mean we always retransmit it as 12362 * one byte even after the window 12363 * opens. 12364 */ 12365 sack_rxmit = 1; 12366 sb_offset = rsm->r_start - tp->snd_una; 12367 } else { 12368 /* 12369 * First time through in persists or peer 12370 * acked our one byte. Though we do have 12371 * to have something in the sb. 12372 */ 12373 len = 1; 12374 sb_offset = 0; 12375 if (avail == 0) 12376 len = 0; 12377 } 12378 } 12379 } 12380 } 12381 if (prefetch_so_done == 0) { 12382 kern_prefetch(so, &prefetch_so_done); 12383 prefetch_so_done = 1; 12384 } 12385 /* 12386 * Lop off SYN bit if it has already been sent. However, if this is 12387 * SYN-SENT state and if segment contains data and if we don't know 12388 * that foreign host supports TAO, suppress sending segment. 12389 */ 12390 if ((flags & TH_SYN) && (rsm == NULL) && 12391 SEQ_GT(tp->snd_max, tp->snd_una)) { 12392 if (tp->t_state != TCPS_SYN_RECEIVED) 12393 flags &= ~TH_SYN; 12394 /* 12395 * When sending additional segments following a TFO SYN|ACK, 12396 * do not include the SYN bit. 12397 */ 12398 if ((tp->t_flags & TF_FASTOPEN) && 12399 (tp->t_state == TCPS_SYN_RECEIVED)) 12400 flags &= ~TH_SYN; 12401 sb_offset--, len++; 12402 if (sbavail(sb) == 0) 12403 len = 0; 12404 } else if ((flags & TH_SYN) && rsm) { 12405 /* 12406 * Subtract one from the len for the SYN being 12407 * retransmitted. 12408 */ 12409 len--; 12410 } 12411 /* 12412 * Be careful not to send data and/or FIN on SYN segments. This 12413 * measure is needed to prevent interoperability problems with not 12414 * fully conformant TCP implementations. 12415 */ 12416 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 12417 len = 0; 12418 flags &= ~TH_FIN; 12419 } 12420 /* 12421 * On TFO sockets, ensure no data is sent in the following cases: 12422 * 12423 * - When retransmitting SYN|ACK on a passively-created socket 12424 * - When retransmitting SYN on an actively created socket 12425 * - When sending a zero-length cookie (cookie request) on an 12426 * actively created socket 12427 * - When the socket is in the CLOSED state (RST is being sent) 12428 */ 12429 if ((tp->t_flags & TF_FASTOPEN) && 12430 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 12431 ((tp->t_state == TCPS_SYN_SENT) && 12432 (tp->t_tfo_client_cookie_len == 0)) || 12433 (flags & TH_RST))) { 12434 len = 0; 12435 sack_rxmit = 0; 12436 rsm = NULL; 12437 } 12438 /* Without fast-open there should never be data sent on a SYN */ 12439 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) 12440 len = 0; 12441 if (len <= 0) { 12442 /* 12443 * If FIN has been sent but not acked, but we haven't been 12444 * called to retransmit, len will be < 0. Otherwise, window 12445 * shrank after we sent into it. If window shrank to 0, 12446 * cancel pending retransmit, pull snd_nxt back to (closed) 12447 * window, and set the persist timer if it isn't already 12448 * going. If the window didn't close completely, just wait 12449 * for an ACK. 12450 * 12451 * We also do a general check here to ensure that we will 12452 * set the persist timer when we have data to send, but a 12453 * 0-byte window. This makes sure the persist timer is set 12454 * even if the packet hits one of the "goto send" lines 12455 * below. 12456 */ 12457 len = 0; 12458 if ((tp->snd_wnd == 0) && 12459 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12460 (tp->snd_una == tp->snd_max) && 12461 (sb_offset < (int)sbavail(sb))) { 12462 /* 12463 * Not enough room in the rwnd to send 12464 * a paced segment out. 12465 */ 12466 bbr_enter_persist(tp, bbr, cts, __LINE__); 12467 } 12468 } else if ((rsm == NULL) && 12469 (doing_tlp == 0) && 12470 (len < bbr->r_ctl.rc_pace_max_segs)) { 12471 /* 12472 * We are not sending a full segment for 12473 * some reason. Should we not send anything (think 12474 * sws or persists)? 12475 */ 12476 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12477 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12478 (len < (int)(sbavail(sb) - sb_offset))) { 12479 /* 12480 * Here the rwnd is less than 12481 * the pacing size, this is not a retransmit, 12482 * we are established and 12483 * the send is not the last in the socket buffer 12484 * lets not send, and possibly enter persists. 12485 */ 12486 len = 0; 12487 if (tp->snd_max == tp->snd_una) 12488 bbr_enter_persist(tp, bbr, cts, __LINE__); 12489 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) && 12490 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12491 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12492 (len < (int)(sbavail(sb) - sb_offset)) && 12493 (len < bbr_minseg(bbr))) { 12494 /* 12495 * Here we are not retransmitting, and 12496 * the cwnd is not so small that we could 12497 * not send at least a min size (rxt timer 12498 * not having gone off), We have 2 segments or 12499 * more already in flight, its not the tail end 12500 * of the socket buffer and the cwnd is blocking 12501 * us from sending out minimum pacing segment size. 12502 * Lets not send anything. 12503 */ 12504 bbr->rc_cwnd_limited = 1; 12505 len = 0; 12506 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 12507 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12508 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12509 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12510 (len < (int)(sbavail(sb) - sb_offset)) && 12511 (TCPS_HAVEESTABLISHED(tp->t_state))) { 12512 /* 12513 * Here we have a send window but we have 12514 * filled it up and we can't send another pacing segment. 12515 * We also have in flight more than 2 segments 12516 * and we are not completing the sb i.e. we allow 12517 * the last bytes of the sb to go out even if 12518 * its not a full pacing segment. 12519 */ 12520 len = 0; 12521 } 12522 } 12523 /* len will be >= 0 after this point. */ 12524 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 12525 tcp_sndbuf_autoscale(tp, so, sendwin); 12526 /* 12527 * 12528 */ 12529 if (bbr->rc_in_persist && 12530 len && 12531 (rsm == NULL) && 12532 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) { 12533 /* 12534 * We are in persist, not doing a retransmit and don't have enough space 12535 * yet to send a full TSO. So is it at the end of the sb 12536 * if so we need to send else nuke to 0 and don't send. 12537 */ 12538 int sbleft; 12539 if (sbavail(sb) > sb_offset) 12540 sbleft = sbavail(sb) - sb_offset; 12541 else 12542 sbleft = 0; 12543 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) { 12544 /* not at end of sb lets not send */ 12545 len = 0; 12546 } 12547 } 12548 /* 12549 * Decide if we can use TCP Segmentation Offloading (if supported by 12550 * hardware). 12551 * 12552 * TSO may only be used if we are in a pure bulk sending state. The 12553 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 12554 * options prevent using TSO. With TSO the TCP header is the same 12555 * (except for the sequence number) for all generated packets. This 12556 * makes it impossible to transmit any options which vary per 12557 * generated segment or packet. 12558 * 12559 * IPv4 handling has a clear separation of ip options and ip header 12560 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() 12561 * does the right thing below to provide length of just ip options 12562 * and thus checking for ipoptlen is enough to decide if ip options 12563 * are present. 12564 */ 12565 #ifdef INET6 12566 if (isipv6) 12567 ipoptlen = ip6_optlen(inp); 12568 else 12569 #endif 12570 if (inp->inp_options) 12571 ipoptlen = inp->inp_options->m_len - 12572 offsetof(struct ipoption, ipopt_list); 12573 else 12574 ipoptlen = 0; 12575 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12576 /* 12577 * Pre-calculate here as we save another lookup into the darknesses 12578 * of IPsec that way and can actually decide if TSO is ok. 12579 */ 12580 #ifdef INET6 12581 if (isipv6 && IPSEC_ENABLED(ipv6)) 12582 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 12583 #ifdef INET 12584 else 12585 #endif 12586 #endif /* INET6 */ 12587 #ifdef INET 12588 if (IPSEC_ENABLED(ipv4)) 12589 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 12590 #endif /* INET */ 12591 #endif /* IPSEC */ 12592 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12593 ipoptlen += ipsec_optlen; 12594 #endif 12595 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && 12596 (len > maxseg) && 12597 (tp->t_port == 0) && 12598 ((tp->t_flags & TF_SIGNATURE) == 0) && 12599 ipoptlen == 0) 12600 tso = 1; 12601 12602 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 12603 (long)TCP_MAXWIN << tp->rcv_scale); 12604 /* 12605 * Sender silly window avoidance. We transmit under the following 12606 * conditions when len is non-zero: 12607 * 12608 * - We have a full segment (or more with TSO) - This is the last 12609 * buffer in a write()/send() and we are either idle or running 12610 * NODELAY - we've timed out (e.g. persist timer) - we have more 12611 * then 1/2 the maximum send window's worth of data (receiver may be 12612 * limited the window size) - we need to retransmit 12613 */ 12614 if (rsm) 12615 goto send; 12616 if (len) { 12617 if (sack_rxmit) 12618 goto send; 12619 if (len >= p_maxseg) 12620 goto send; 12621 /* 12622 * NOTE! on localhost connections an 'ack' from the remote 12623 * end may occur synchronously with the output and cause us 12624 * to flush a buffer queued with moretocome. XXX 12625 * 12626 */ 12627 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */ 12628 ((tp->t_flags & TF_NODELAY) || 12629 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) && 12630 (tp->t_flags & TF_NOPUSH) == 0) { 12631 goto send; 12632 } 12633 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 12634 goto send; 12635 } 12636 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 12637 goto send; 12638 } 12639 } 12640 /* 12641 * Sending of standalone window updates. 12642 * 12643 * Window updates are important when we close our window due to a 12644 * full socket buffer and are opening it again after the application 12645 * reads data from it. Once the window has opened again and the 12646 * remote end starts to send again the ACK clock takes over and 12647 * provides the most current window information. 12648 * 12649 * We must avoid the silly window syndrome whereas every read from 12650 * the receive buffer, no matter how small, causes a window update 12651 * to be sent. We also should avoid sending a flurry of window 12652 * updates when the socket buffer had queued a lot of data and the 12653 * application is doing small reads. 12654 * 12655 * Prevent a flurry of pointless window updates by only sending an 12656 * update when we can increase the advertized window by more than 12657 * 1/4th of the socket buffer capacity. When the buffer is getting 12658 * full or is very small be more aggressive and send an update 12659 * whenever we can increase by two mss sized segments. In all other 12660 * situations the ACK's to new incoming data will carry further 12661 * window increases. 12662 * 12663 * Don't send an independent window update if a delayed ACK is 12664 * pending (it will get piggy-backed on it) or the remote side 12665 * already has done a half-close and won't send more data. Skip 12666 * this if the connection is in T/TCP half-open state. 12667 */ 12668 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 12669 !(tp->t_flags & TF_DELACK) && 12670 !TCPS_HAVERCVDFIN(tp->t_state)) { 12671 /* Check to see if we should do a window update */ 12672 if (bbr_window_update_needed(tp, so, recwin, maxseg)) 12673 goto send; 12674 } 12675 /* 12676 * Send if we owe the peer an ACK, RST, SYN. ACKNOW 12677 * is also a catch-all for the retransmit timer timeout case. 12678 */ 12679 if (tp->t_flags & TF_ACKNOW) { 12680 goto send; 12681 } 12682 if (flags & TH_RST) { 12683 /* Always send a RST if one is due */ 12684 goto send; 12685 } 12686 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) { 12687 goto send; 12688 } 12689 /* 12690 * If our state indicates that FIN should be sent and we have not 12691 * yet done so, then we need to send. 12692 */ 12693 if (flags & TH_FIN && 12694 ((tp->t_flags & TF_SENTFIN) == 0)) { 12695 goto send; 12696 } 12697 /* 12698 * No reason to send a segment, just return. 12699 */ 12700 just_return: 12701 SOCK_SENDBUF_UNLOCK(so); 12702 just_return_nolock: 12703 if (tot_len) 12704 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 12705 if (bbr->rc_no_pacing) 12706 slot = 0; 12707 if (tot_len == 0) { 12708 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >= 12709 tp->snd_wnd) { 12710 BBR_STAT_INC(bbr_rwnd_limited); 12711 app_limited = BBR_JR_RWND_LIMITED; 12712 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12713 if ((bbr->rc_in_persist == 0) && 12714 TCPS_HAVEESTABLISHED(tp->t_state) && 12715 (tp->snd_max == tp->snd_una) && 12716 sbavail(&so->so_snd)) { 12717 /* No send window.. we must enter persist */ 12718 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 12719 } 12720 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 12721 BBR_STAT_INC(bbr_app_limited); 12722 app_limited = BBR_JR_APP_LIMITED; 12723 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12724 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12725 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) { 12726 BBR_STAT_INC(bbr_cwnd_limited); 12727 app_limited = BBR_JR_CWND_LIMITED; 12728 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12729 bbr->r_ctl.rc_lost_bytes))); 12730 bbr->rc_cwnd_limited = 1; 12731 } else { 12732 BBR_STAT_INC(bbr_app_limited); 12733 app_limited = BBR_JR_APP_LIMITED; 12734 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12735 } 12736 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12737 bbr->r_agg_early_set = 0; 12738 bbr->r_ctl.rc_agg_early = 0; 12739 bbr->r_ctl.rc_last_delay_val = 0; 12740 } else if (bbr->rc_use_google == 0) 12741 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12742 /* Are we app limited? */ 12743 if ((app_limited == BBR_JR_APP_LIMITED) || 12744 (app_limited == BBR_JR_RWND_LIMITED)) { 12745 /** 12746 * We are application limited. 12747 */ 12748 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12749 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered); 12750 } 12751 if (tot_len == 0) 12752 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1); 12753 /* Dont update the time if we did not send */ 12754 bbr->r_ctl.rc_last_delay_val = 0; 12755 bbr->rc_output_starts_timer = 1; 12756 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len); 12757 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len); 12758 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 12759 /* Make sure snd_nxt is drug up */ 12760 tp->snd_nxt = tp->snd_max; 12761 } 12762 return (error); 12763 12764 send: 12765 if (doing_tlp == 0) { 12766 /* 12767 * Data not a TLP, and its not the rxt firing. If it is the 12768 * rxt firing, we want to leave the tlp_in_progress flag on 12769 * so we don't send another TLP. It has to be a rack timer 12770 * or normal send (response to acked data) to clear the tlp 12771 * in progress flag. 12772 */ 12773 bbr->rc_tlp_in_progress = 0; 12774 bbr->rc_tlp_rtx_out = 0; 12775 } else { 12776 /* 12777 * Its a TLP. 12778 */ 12779 bbr->rc_tlp_in_progress = 1; 12780 } 12781 bbr_timer_cancel(bbr, __LINE__, cts); 12782 if (rsm == NULL) { 12783 if (sbused(sb) > 0) { 12784 /* 12785 * This is sub-optimal. We only send a stand alone 12786 * FIN on its own segment. 12787 */ 12788 if (flags & TH_FIN) { 12789 flags &= ~TH_FIN; 12790 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) { 12791 /* Lets not send this */ 12792 slot = 0; 12793 goto just_return; 12794 } 12795 } 12796 } 12797 } else { 12798 /* 12799 * We do *not* send a FIN on a retransmit if it has data. 12800 * The if clause here where len > 1 should never come true. 12801 */ 12802 if ((len > 0) && 12803 (((rsm->r_flags & BBR_HAS_FIN) == 0) && 12804 (flags & TH_FIN))) { 12805 flags &= ~TH_FIN; 12806 len--; 12807 } 12808 } 12809 SOCK_SENDBUF_LOCK_ASSERT(so); 12810 if (len > 0) { 12811 if ((tp->snd_una == tp->snd_max) && 12812 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 12813 /* 12814 * This qualifies as a RTT_PROBE session since we 12815 * drop the data outstanding to nothing and waited 12816 * more than bbr_rtt_probe_time. 12817 */ 12818 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 12819 bbr_set_reduced_rtt(bbr, cts, __LINE__); 12820 } 12821 if (len >= maxseg) 12822 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 12823 else 12824 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 12825 } 12826 /* 12827 * Before ESTABLISHED, force sending of initial options unless TCP 12828 * set not to do any options. NOTE: we assume that the IP/TCP header 12829 * plus TCP options always fit in a single mbuf, leaving room for a 12830 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 12831 * + optlen <= MCLBYTES 12832 */ 12833 optlen = 0; 12834 #ifdef INET6 12835 if (isipv6) 12836 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12837 else 12838 #endif 12839 hdrlen = sizeof(struct tcpiphdr); 12840 12841 /* 12842 * Compute options for segment. We only have to care about SYN and 12843 * established connection segments. Options for SYN-ACK segments 12844 * are handled in TCP syncache. 12845 */ 12846 to.to_flags = 0; 12847 local_options = 0; 12848 if ((tp->t_flags & TF_NOOPT) == 0) { 12849 /* Maximum segment size. */ 12850 if (flags & TH_SYN) { 12851 to.to_mss = tcp_mssopt(&inp->inp_inc); 12852 if (tp->t_port) 12853 to.to_mss -= V_tcp_udp_tunneling_overhead; 12854 to.to_flags |= TOF_MSS; 12855 /* 12856 * On SYN or SYN|ACK transmits on TFO connections, 12857 * only include the TFO option if it is not a 12858 * retransmit, as the presence of the TFO option may 12859 * have caused the original SYN or SYN|ACK to have 12860 * been dropped by a middlebox. 12861 */ 12862 if ((tp->t_flags & TF_FASTOPEN) && 12863 (tp->t_rxtshift == 0)) { 12864 if (tp->t_state == TCPS_SYN_RECEIVED) { 12865 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 12866 to.to_tfo_cookie = 12867 (u_int8_t *)&tp->t_tfo_cookie.server; 12868 to.to_flags |= TOF_FASTOPEN; 12869 wanted_cookie = 1; 12870 } else if (tp->t_state == TCPS_SYN_SENT) { 12871 to.to_tfo_len = 12872 tp->t_tfo_client_cookie_len; 12873 to.to_tfo_cookie = 12874 tp->t_tfo_cookie.client; 12875 to.to_flags |= TOF_FASTOPEN; 12876 wanted_cookie = 1; 12877 } 12878 } 12879 } 12880 /* Window scaling. */ 12881 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 12882 to.to_wscale = tp->request_r_scale; 12883 to.to_flags |= TOF_SCALE; 12884 } 12885 /* Timestamps. */ 12886 if ((tp->t_flags & TF_RCVD_TSTMP) || 12887 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 12888 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset; 12889 to.to_tsecr = tp->ts_recent; 12890 to.to_flags |= TOF_TS; 12891 local_options += TCPOLEN_TIMESTAMP + 2; 12892 } 12893 /* Set receive buffer autosizing timestamp. */ 12894 if (tp->rfbuf_ts == 0 && 12895 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 12896 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv); 12897 /* Selective ACK's. */ 12898 if (flags & TH_SYN) 12899 to.to_flags |= TOF_SACKPERM; 12900 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 12901 tp->rcv_numsacks > 0) { 12902 to.to_flags |= TOF_SACK; 12903 to.to_nsacks = tp->rcv_numsacks; 12904 to.to_sacks = (u_char *)tp->sackblks; 12905 } 12906 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 12907 /* TCP-MD5 (RFC2385). */ 12908 if (tp->t_flags & TF_SIGNATURE) 12909 to.to_flags |= TOF_SIGNATURE; 12910 #endif /* TCP_SIGNATURE */ 12911 12912 /* Processing the options. */ 12913 hdrlen += (optlen = tcp_addoptions(&to, opt)); 12914 /* 12915 * If we wanted a TFO option to be added, but it was unable 12916 * to fit, ensure no data is sent. 12917 */ 12918 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 12919 !(to.to_flags & TOF_FASTOPEN)) 12920 len = 0; 12921 } 12922 if (tp->t_port) { 12923 if (V_tcp_udp_tunneling_port == 0) { 12924 /* The port was removed?? */ 12925 SOCK_SENDBUF_UNLOCK(so); 12926 return (EHOSTUNREACH); 12927 } 12928 hdrlen += sizeof(struct udphdr); 12929 } 12930 #ifdef INET6 12931 if (isipv6) 12932 ipoptlen = ip6_optlen(inp); 12933 else 12934 #endif 12935 if (inp->inp_options) 12936 ipoptlen = inp->inp_options->m_len - 12937 offsetof(struct ipoption, ipopt_list); 12938 else 12939 ipoptlen = 0; 12940 ipoptlen = 0; 12941 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12942 ipoptlen += ipsec_optlen; 12943 #endif 12944 if (bbr->rc_last_options != local_options) { 12945 /* 12946 * Cache the options length this generally does not change 12947 * on a connection. We use this to calculate TSO. 12948 */ 12949 bbr->rc_last_options = local_options; 12950 } 12951 maxseg = tp->t_maxseg - (ipoptlen + optlen); 12952 p_maxseg = min(maxseg, pace_max_segs); 12953 /* 12954 * Adjust data length if insertion of options will bump the packet 12955 * length beyond the t_maxseg length. Clear the FIN bit because we 12956 * cut off the tail of the segment. 12957 */ 12958 if (len > maxseg) { 12959 if (len != 0 && (flags & TH_FIN)) { 12960 flags &= ~TH_FIN; 12961 } 12962 if (tso) { 12963 uint32_t moff; 12964 int32_t max_len; 12965 12966 /* extract TSO information */ 12967 if_hw_tsomax = tp->t_tsomax; 12968 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 12969 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 12970 KASSERT(ipoptlen == 0, 12971 ("%s: TSO can't do IP options", __func__)); 12972 12973 /* 12974 * Check if we should limit by maximum payload 12975 * length: 12976 */ 12977 if (if_hw_tsomax != 0) { 12978 /* compute maximum TSO length */ 12979 max_len = (if_hw_tsomax - hdrlen - 12980 max_linkhdr); 12981 if (max_len <= 0) { 12982 len = 0; 12983 } else if (len > max_len) { 12984 len = max_len; 12985 } 12986 } 12987 /* 12988 * Prevent the last segment from being fractional 12989 * unless the send sockbuf can be emptied: 12990 */ 12991 if ((sb_offset + len) < sbavail(sb)) { 12992 moff = len % (uint32_t)maxseg; 12993 if (moff != 0) { 12994 len -= moff; 12995 } 12996 } 12997 /* 12998 * In case there are too many small fragments don't 12999 * use TSO: 13000 */ 13001 if (len <= maxseg) { 13002 len = maxseg; 13003 tso = 0; 13004 } 13005 } else { 13006 /* Not doing TSO */ 13007 if (optlen + ipoptlen >= tp->t_maxseg) { 13008 /* 13009 * Since we don't have enough space to put 13010 * the IP header chain and the TCP header in 13011 * one packet as required by RFC 7112, don't 13012 * send it. Also ensure that at least one 13013 * byte of the payload can be put into the 13014 * TCP segment. 13015 */ 13016 SOCK_SENDBUF_UNLOCK(so); 13017 error = EMSGSIZE; 13018 sack_rxmit = 0; 13019 goto out; 13020 } 13021 len = maxseg; 13022 } 13023 } else { 13024 /* Not doing TSO */ 13025 if_hw_tsomaxsegcount = 0; 13026 tso = 0; 13027 } 13028 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 13029 ("%s: len > IP_MAXPACKET", __func__)); 13030 #ifdef DIAGNOSTIC 13031 #ifdef INET6 13032 if (max_linkhdr + hdrlen > MCLBYTES) 13033 #else 13034 if (max_linkhdr + hdrlen > MHLEN) 13035 #endif 13036 panic("tcphdr too big"); 13037 #endif 13038 /* 13039 * This KASSERT is here to catch edge cases at a well defined place. 13040 * Before, those had triggered (random) panic conditions further 13041 * down. 13042 */ 13043 #ifdef BBR_INVARIANTS 13044 if (sack_rxmit) { 13045 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 13046 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u", 13047 rsm, tp, bbr, rsm->r_start, tp->snd_una); 13048 } 13049 } 13050 #endif 13051 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 13052 if ((len == 0) && 13053 (flags & TH_FIN) && 13054 (sbused(sb))) { 13055 /* 13056 * We have outstanding data, don't send a fin by itself!. 13057 */ 13058 slot = 0; 13059 goto just_return; 13060 } 13061 /* 13062 * Grab a header mbuf, attaching a copy of data to be transmitted, 13063 * and initialize the header from the template for sends on this 13064 * connection. 13065 */ 13066 if (len) { 13067 uint32_t moff; 13068 13069 /* 13070 * We place a limit on sending with hptsi. 13071 */ 13072 if ((rsm == NULL) && len > pace_max_segs) 13073 len = pace_max_segs; 13074 if (len <= maxseg) 13075 tso = 0; 13076 #ifdef INET6 13077 if (MHLEN < hdrlen + max_linkhdr) 13078 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13079 else 13080 #endif 13081 m = m_gethdr(M_NOWAIT, MT_DATA); 13082 13083 if (m == NULL) { 13084 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13085 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13086 SOCK_SENDBUF_UNLOCK(so); 13087 error = ENOBUFS; 13088 sack_rxmit = 0; 13089 goto out; 13090 } 13091 m->m_data += max_linkhdr; 13092 m->m_len = hdrlen; 13093 /* 13094 * Start the m_copy functions from the closest mbuf to the 13095 * sb_offset in the socket buffer chain. 13096 */ 13097 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) { 13098 #ifdef BBR_INVARIANTS 13099 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) 13100 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u", 13101 tp, bbr, len, sb_offset, sbavail(sb), rsm, 13102 doing_retran_from, 13103 picked_up_retran, 13104 doing_tlp); 13105 13106 #endif 13107 /* 13108 * In this messed up situation we have two choices, 13109 * a) pretend the send worked, and just start timers 13110 * and what not (not good since that may lead us 13111 * back here a lot). <or> b) Send the lowest segment 13112 * in the map. <or> c) Drop the connection. Lets do 13113 * <b> which if it continues to happen will lead to 13114 * <c> via timeouts. 13115 */ 13116 BBR_STAT_INC(bbr_offset_recovery); 13117 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 13118 sb_offset = 0; 13119 if (rsm == NULL) { 13120 sack_rxmit = 0; 13121 len = sbavail(sb); 13122 } else { 13123 sack_rxmit = 1; 13124 if (rsm->r_start != tp->snd_una) { 13125 /* 13126 * Things are really messed up, <c> 13127 * is the only thing to do. 13128 */ 13129 BBR_STAT_INC(bbr_offset_drop); 13130 SOCK_SENDBUF_UNLOCK(so); 13131 (void)m_free(m); 13132 return (-EFAULT); /* tcp_drop() */ 13133 } 13134 len = rsm->r_end - rsm->r_start; 13135 } 13136 if (len > sbavail(sb)) 13137 len = sbavail(sb); 13138 if (len > maxseg) 13139 len = maxseg; 13140 } 13141 mb = sbsndptr_noadv(sb, sb_offset, &moff); 13142 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 13143 m_copydata(mb, moff, (int)len, 13144 mtod(m, caddr_t)+hdrlen); 13145 if (rsm == NULL) 13146 sbsndptr_adv(sb, mb, len); 13147 m->m_len += len; 13148 } else { 13149 struct sockbuf *msb; 13150 13151 if (rsm) 13152 msb = NULL; 13153 else 13154 msb = sb; 13155 #ifdef BBR_INVARIANTS 13156 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) { 13157 if (rsm) { 13158 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 ", 13159 tp, bbr, len, moff, 13160 sbavail(sb), rsm, 13161 tp->snd_una, rsm->r_flags, rsm->r_start, 13162 doing_retran_from, 13163 picked_up_retran, 13164 doing_tlp, sack_rxmit); 13165 } else { 13166 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u", 13167 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una); 13168 } 13169 } 13170 #endif 13171 m->m_next = tcp_m_copym( 13172 mb, moff, &len, 13173 if_hw_tsomaxsegcount, 13174 if_hw_tsomaxsegsize, msb, 13175 ((rsm == NULL) ? hw_tls : 0) 13176 #ifdef NETFLIX_COPY_ARGS 13177 , NULL, NULL 13178 #endif 13179 ); 13180 if (len <= maxseg) { 13181 /* 13182 * Must have ran out of mbufs for the copy 13183 * shorten it to no longer need tso. Lets 13184 * not put on sendalot since we are low on 13185 * mbufs. 13186 */ 13187 tso = 0; 13188 } 13189 if (m->m_next == NULL) { 13190 SOCK_SENDBUF_UNLOCK(so); 13191 (void)m_free(m); 13192 error = ENOBUFS; 13193 sack_rxmit = 0; 13194 goto out; 13195 } 13196 } 13197 #ifdef BBR_INVARIANTS 13198 if (tso && len < maxseg) { 13199 panic("tp:%p tso on, but len:%d < maxseg:%d", 13200 tp, len, maxseg); 13201 } 13202 if (tso && if_hw_tsomaxsegcount) { 13203 int32_t seg_cnt = 0; 13204 struct mbuf *foo; 13205 13206 foo = m; 13207 while (foo) { 13208 seg_cnt++; 13209 foo = foo->m_next; 13210 } 13211 if (seg_cnt > if_hw_tsomaxsegcount) { 13212 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount); 13213 } 13214 } 13215 #endif 13216 /* 13217 * If we're sending everything we've got, set PUSH. (This 13218 * will keep happy those implementations which only give 13219 * data to the user when a buffer fills or a PUSH comes in.) 13220 */ 13221 if (sb_offset + len == sbused(sb) && 13222 sbused(sb) && 13223 !(flags & TH_SYN)) { 13224 flags |= TH_PUSH; 13225 } 13226 SOCK_SENDBUF_UNLOCK(so); 13227 } else { 13228 SOCK_SENDBUF_UNLOCK(so); 13229 if (tp->t_flags & TF_ACKNOW) 13230 KMOD_TCPSTAT_INC(tcps_sndacks); 13231 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 13232 KMOD_TCPSTAT_INC(tcps_sndctrl); 13233 else 13234 KMOD_TCPSTAT_INC(tcps_sndwinup); 13235 13236 m = m_gethdr(M_NOWAIT, MT_DATA); 13237 if (m == NULL) { 13238 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13239 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13240 error = ENOBUFS; 13241 /* Fudge the send time since we could not send */ 13242 sack_rxmit = 0; 13243 goto out; 13244 } 13245 #ifdef INET6 13246 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 13247 MHLEN >= hdrlen) { 13248 M_ALIGN(m, hdrlen); 13249 } else 13250 #endif 13251 m->m_data += max_linkhdr; 13252 m->m_len = hdrlen; 13253 } 13254 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13255 m->m_pkthdr.rcvif = (struct ifnet *)0; 13256 #ifdef MAC 13257 mac_inpcb_create_mbuf(inp, m); 13258 #endif 13259 #ifdef INET6 13260 if (isipv6) { 13261 ip6 = mtod(m, struct ip6_hdr *); 13262 if (tp->t_port) { 13263 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 13264 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13265 udp->uh_dport = tp->t_port; 13266 ulen = hdrlen + len - sizeof(struct ip6_hdr); 13267 udp->uh_ulen = htons(ulen); 13268 th = (struct tcphdr *)(udp + 1); 13269 } else { 13270 th = (struct tcphdr *)(ip6 + 1); 13271 } 13272 tcpip_fillheaders(inp, tp->t_port, ip6, th); 13273 } else 13274 #endif /* INET6 */ 13275 { 13276 ip = mtod(m, struct ip *); 13277 if (tp->t_port) { 13278 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 13279 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13280 udp->uh_dport = tp->t_port; 13281 ulen = hdrlen + len - sizeof(struct ip); 13282 udp->uh_ulen = htons(ulen); 13283 th = (struct tcphdr *)(udp + 1); 13284 } else { 13285 th = (struct tcphdr *)(ip + 1); 13286 } 13287 tcpip_fillheaders(inp, tp->t_port, ip, th); 13288 } 13289 /* 13290 * If we are doing retransmissions, then snd_nxt will not reflect 13291 * the first unsent octet. For ACK only packets, we do not want the 13292 * sequence number of the retransmitted packet, we want the sequence 13293 * number of the next unsent octet. So, if there is no data (and no 13294 * SYN or FIN), use snd_max instead of snd_nxt when filling in 13295 * ti_seq. But if we are in persist state, snd_max might reflect 13296 * one byte beyond the right edge of the window, so use snd_nxt in 13297 * that case, since we know we aren't doing a retransmission. 13298 * (retransmit and persist are mutually exclusive...) 13299 */ 13300 if (sack_rxmit == 0) { 13301 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) { 13302 /* New data (including new persists) */ 13303 th->th_seq = htonl(tp->snd_max); 13304 bbr_seq = tp->snd_max; 13305 } else if (flags & TH_SYN) { 13306 /* Syn's always send from iss */ 13307 th->th_seq = htonl(tp->iss); 13308 bbr_seq = tp->iss; 13309 } else if (flags & TH_FIN) { 13310 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) { 13311 /* 13312 * If we sent the fin already its 1 minus 13313 * snd_max 13314 */ 13315 th->th_seq = (htonl(tp->snd_max - 1)); 13316 bbr_seq = (tp->snd_max - 1); 13317 } else { 13318 /* First time FIN use snd_max */ 13319 th->th_seq = htonl(tp->snd_max); 13320 bbr_seq = tp->snd_max; 13321 } 13322 } else { 13323 /* 13324 * len == 0 and not persist we use snd_max, sending 13325 * an ack unless we have sent the fin then its 1 13326 * minus. 13327 */ 13328 /* 13329 * XXXRRS Question if we are in persists and we have 13330 * nothing outstanding to send and we have not sent 13331 * a FIN, we will send an ACK. In such a case it 13332 * might be better to send (tp->snd_una - 1) which 13333 * would force the peer to ack. 13334 */ 13335 if (tp->t_flags & TF_SENTFIN) { 13336 th->th_seq = htonl(tp->snd_max - 1); 13337 bbr_seq = (tp->snd_max - 1); 13338 } else { 13339 th->th_seq = htonl(tp->snd_max); 13340 bbr_seq = tp->snd_max; 13341 } 13342 } 13343 } else { 13344 /* All retransmits use the rsm to guide the send */ 13345 th->th_seq = htonl(rsm->r_start); 13346 bbr_seq = rsm->r_start; 13347 } 13348 th->th_ack = htonl(tp->rcv_nxt); 13349 if (optlen) { 13350 bcopy(opt, th + 1, optlen); 13351 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 13352 } 13353 tcp_set_flags(th, flags); 13354 /* 13355 * Calculate receive window. Don't shrink window, but avoid silly 13356 * window syndrome. 13357 */ 13358 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) && 13359 recwin < maxseg))) 13360 recwin = 0; 13361 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 13362 recwin < (tp->rcv_adv - tp->rcv_nxt)) 13363 recwin = (tp->rcv_adv - tp->rcv_nxt); 13364 if (recwin > TCP_MAXWIN << tp->rcv_scale) 13365 recwin = TCP_MAXWIN << tp->rcv_scale; 13366 13367 /* 13368 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 13369 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 13370 * handled in syncache. 13371 */ 13372 if (flags & TH_SYN) 13373 th->th_win = htons((u_short) 13374 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 13375 else { 13376 /* Avoid shrinking window with window scaling. */ 13377 recwin = roundup2(recwin, 1 << tp->rcv_scale); 13378 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13379 } 13380 /* 13381 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 13382 * window. This may cause the remote transmitter to stall. This 13383 * flag tells soreceive() to disable delayed acknowledgements when 13384 * draining the buffer. This can occur if the receiver is 13385 * attempting to read more data than can be buffered prior to 13386 * transmitting on the connection. 13387 */ 13388 if (th->th_win == 0) { 13389 tp->t_sndzerowin++; 13390 tp->t_flags |= TF_RXWIN0SENT; 13391 } else 13392 tp->t_flags &= ~TF_RXWIN0SENT; 13393 /* 13394 * We don't support urgent data, but drag along 13395 * the pointer in case of a stack switch. 13396 */ 13397 tp->snd_up = tp->snd_una; 13398 /* 13399 * Put TCP length in extended header, and then checksum extended 13400 * header and data. 13401 */ 13402 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 13403 13404 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 13405 if (to.to_flags & TOF_SIGNATURE) { 13406 /* 13407 * Calculate MD5 signature and put it into the place 13408 * determined before. NOTE: since TCP options buffer doesn't 13409 * point into mbuf's data, calculate offset and use it. 13410 */ 13411 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 13412 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 13413 /* 13414 * Do not send segment if the calculation of MD5 13415 * digest has failed. 13416 */ 13417 goto out; 13418 } 13419 } 13420 #endif 13421 13422 #ifdef INET6 13423 if (isipv6) { 13424 /* 13425 * ip6_plen is not need to be filled now, and will be filled 13426 * in ip6_output. 13427 */ 13428 if (tp->t_port) { 13429 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13430 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13431 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13432 th->th_sum = htons(0); 13433 UDPSTAT_INC(udps_opackets); 13434 } else { 13435 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13436 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13437 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 13438 optlen + len, IPPROTO_TCP, 0); 13439 } 13440 } 13441 #endif 13442 #if defined(INET6) && defined(INET) 13443 else 13444 #endif 13445 #ifdef INET 13446 { 13447 if (tp->t_port) { 13448 m->m_pkthdr.csum_flags = CSUM_UDP; 13449 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13450 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13451 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13452 th->th_sum = htons(0); 13453 UDPSTAT_INC(udps_opackets); 13454 } else { 13455 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP; 13456 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13457 th->th_sum = in_pseudo(ip->ip_src.s_addr, 13458 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13459 IPPROTO_TCP + len + optlen)); 13460 } 13461 /* IP version must be set here for ipv4/ipv6 checking later */ 13462 KASSERT(ip->ip_v == IPVERSION, 13463 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 13464 } 13465 #endif 13466 13467 /* 13468 * Enable TSO and specify the size of the segments. The TCP pseudo 13469 * header checksum is always provided. XXX: Fixme: This is currently 13470 * not the case for IPv6. 13471 */ 13472 if (tso) { 13473 KASSERT(len > maxseg, 13474 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg)); 13475 m->m_pkthdr.csum_flags |= CSUM_TSO; 13476 csum_flags |= CSUM_TSO; 13477 m->m_pkthdr.tso_segsz = maxseg; 13478 } 13479 KASSERT(len + hdrlen == m_length(m, NULL), 13480 ("%s: mbuf chain different than expected: %d + %u != %u", 13481 __func__, len, hdrlen, m_length(m, NULL))); 13482 13483 #ifdef TCP_HHOOK 13484 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */ 13485 hhook_run_tcp_est_out(tp, th, &to, len, tso); 13486 #endif 13487 13488 /* Log to the black box */ 13489 if (tcp_bblogging_on(tp)) { 13490 union tcp_log_stackspecific log; 13491 13492 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 13493 /* Record info on type of transmission */ 13494 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay; 13495 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3); 13496 log.u_bbr.flex3 = maxseg; 13497 log.u_bbr.flex4 = delay_calc; 13498 log.u_bbr.flex5 = bbr->rc_past_init_win; 13499 log.u_bbr.flex5 <<= 1; 13500 log.u_bbr.flex5 |= bbr->rc_no_pacing; 13501 log.u_bbr.flex5 <<= 29; 13502 log.u_bbr.flex5 |= tp->t_maxseg; 13503 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs; 13504 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr); 13505 /* lets poke in the low and the high here for debugging */ 13506 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 13507 if (rsm || sack_rxmit) { 13508 if (doing_tlp) 13509 log.u_bbr.flex8 = 2; 13510 else 13511 log.u_bbr.flex8 = 1; 13512 } else { 13513 log.u_bbr.flex8 = 0; 13514 } 13515 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 13516 len, &log, false, NULL, NULL, 0, tv); 13517 } else { 13518 lgb = NULL; 13519 } 13520 /* 13521 * Fill in IP length and desired time to live and send to IP level. 13522 * There should be a better way to handle ttl and tos; we could keep 13523 * them in the template, but need a way to checksum without them. 13524 */ 13525 /* 13526 * m->m_pkthdr.len should have been set before cksum calcuration, 13527 * because in6_cksum() need it. 13528 */ 13529 #ifdef INET6 13530 if (isipv6) { 13531 /* 13532 * we separately set hoplimit for every segment, since the 13533 * user might want to change the value via setsockopt. Also, 13534 * desired default hop limit might be changed via Neighbor 13535 * Discovery. 13536 */ 13537 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 13538 13539 /* 13540 * Set the packet size here for the benefit of DTrace 13541 * probes. ip6_output() will set it properly; it's supposed 13542 * to include the option header lengths as well. 13543 */ 13544 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 13545 13546 if (V_path_mtu_discovery && maxseg > V_tcp_minmss) 13547 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13548 else 13549 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13550 13551 if (tp->t_state == TCPS_SYN_SENT) 13552 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 13553 13554 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 13555 /* TODO: IPv6 IP6TOS_ECT bit on */ 13556 error = ip6_output(m, inp->in6p_outputopts, 13557 &inp->inp_route6, 13558 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 13559 NULL, NULL, inp); 13560 13561 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 13562 mtu = inp->inp_route6.ro_nh->nh_mtu; 13563 } 13564 #endif /* INET6 */ 13565 #if defined(INET) && defined(INET6) 13566 else 13567 #endif 13568 #ifdef INET 13569 { 13570 ip->ip_len = htons(m->m_pkthdr.len); 13571 #ifdef INET6 13572 if (isipv6) 13573 ip->ip_ttl = in6_selecthlim(inp, NULL); 13574 #endif /* INET6 */ 13575 /* 13576 * If we do path MTU discovery, then we set DF on every 13577 * packet. This might not be the best thing to do according 13578 * to RFC3390 Section 2. However the tcp hostcache migitates 13579 * the problem so it affects only the first tcp connection 13580 * with a host. 13581 * 13582 * NB: Don't set DF on small MTU/MSS to have a safe 13583 * fallback. 13584 */ 13585 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 13586 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13587 if (tp->t_port == 0 || len < V_tcp_minmss) { 13588 ip->ip_off |= htons(IP_DF); 13589 } 13590 } else { 13591 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13592 } 13593 13594 if (tp->t_state == TCPS_SYN_SENT) 13595 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 13596 13597 TCP_PROBE5(send, NULL, tp, ip, tp, th); 13598 13599 error = ip_output(m, inp->inp_options, &inp->inp_route, 13600 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 13601 inp); 13602 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 13603 mtu = inp->inp_route.ro_nh->nh_mtu; 13604 } 13605 #endif /* INET */ 13606 if (lgb) { 13607 lgb->tlb_errno = error; 13608 lgb = NULL; 13609 } 13610 13611 out: 13612 /* 13613 * In transmit state, time the transmission and arrange for the 13614 * retransmit. In persist state, just set snd_max. 13615 */ 13616 if (error == 0) { 13617 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 13618 if (TCPS_HAVEESTABLISHED(tp->t_state) && 13619 (tp->t_flags & TF_SACK_PERMIT) && 13620 tp->rcv_numsacks > 0) 13621 tcp_clean_dsack_blocks(tp); 13622 /* We sent an ack clear the bbr_segs_rcvd count */ 13623 bbr->output_error_seen = 0; 13624 bbr->oerror_cnt = 0; 13625 bbr->bbr_segs_rcvd = 0; 13626 if (len == 0) 13627 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1); 13628 /* Do accounting for new sends */ 13629 if ((len > 0) && (rsm == NULL)) { 13630 int idx; 13631 if (tp->snd_una == tp->snd_max) { 13632 /* 13633 * Special case to match google, when 13634 * nothing is in flight the delivered 13635 * time does get updated to the current 13636 * time (see tcp_rate_bsd.c). 13637 */ 13638 bbr->r_ctl.rc_del_time = cts; 13639 } 13640 if (len >= maxseg) { 13641 idx = (len / maxseg) + 3; 13642 if (idx >= TCP_MSS_ACCT_ATIMER) 13643 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1); 13644 else 13645 counter_u64_add(bbr_out_size[idx], 1); 13646 } else { 13647 /* smaller than a MSS */ 13648 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options); 13649 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV) 13650 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1); 13651 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1); 13652 } 13653 } 13654 } 13655 abandon = 0; 13656 /* 13657 * We must do the send accounting before we log the output, 13658 * otherwise the state of the rsm could change and we account to the 13659 * wrong bucket. 13660 */ 13661 if (len > 0) { 13662 bbr_do_send_accounting(tp, bbr, rsm, len, error); 13663 if (error == 0) { 13664 if (tp->snd_una == tp->snd_max) 13665 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 13666 } 13667 } 13668 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error, 13669 cts, mb, &abandon, rsm, 0, sb); 13670 if (abandon) { 13671 /* 13672 * If bbr_log_output destroys the TCB or sees a TH_RST being 13673 * sent we should hit this condition. 13674 */ 13675 return (0); 13676 } 13677 if (bbr->rc_in_persist == 0) { 13678 /* 13679 * Advance snd_nxt over sequence space of this segment. 13680 */ 13681 if (error) 13682 /* We don't log or do anything with errors */ 13683 goto skip_upd; 13684 13685 if (tp->snd_una == tp->snd_max && 13686 (len || (flags & (TH_SYN | TH_FIN)))) { 13687 /* 13688 * Update the time we just added data since none was 13689 * outstanding. 13690 */ 13691 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13692 bbr->rc_tp->t_acktime = ticks; 13693 } 13694 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) { 13695 if (flags & TH_SYN) { 13696 /* 13697 * Smack the snd_max to iss + 1 13698 * if its a FO we will add len below. 13699 */ 13700 tp->snd_max = tp->iss + 1; 13701 } 13702 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13703 tp->snd_max++; 13704 tp->t_flags |= TF_SENTFIN; 13705 } 13706 } 13707 if (sack_rxmit == 0) 13708 tp->snd_max += len; 13709 skip_upd: 13710 if ((error == 0) && len) 13711 tot_len += len; 13712 } else { 13713 /* Persists case */ 13714 int32_t xlen = len; 13715 13716 if (error) 13717 goto nomore; 13718 13719 if (flags & TH_SYN) 13720 ++xlen; 13721 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13722 ++xlen; 13723 tp->t_flags |= TF_SENTFIN; 13724 } 13725 if (xlen && (tp->snd_una == tp->snd_max)) { 13726 /* 13727 * Update the time we just added data since none was 13728 * outstanding. 13729 */ 13730 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13731 bbr->rc_tp->t_acktime = ticks; 13732 } 13733 if (sack_rxmit == 0) 13734 tp->snd_max += xlen; 13735 tot_len += (len + optlen + ipoptlen); 13736 } 13737 nomore: 13738 if (error) { 13739 /* 13740 * Failures do not advance the seq counter above. For the 13741 * case of ENOBUFS we will fall out and become ack-clocked. 13742 * capping the cwnd at the current flight. 13743 * Everything else will just have to retransmit with the timer 13744 * (no pacer). 13745 */ 13746 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13747 BBR_STAT_INC(bbr_saw_oerr); 13748 /* Clear all delay/early tracks */ 13749 bbr->r_ctl.rc_hptsi_agg_delay = 0; 13750 bbr->r_ctl.rc_agg_early = 0; 13751 bbr->r_agg_early_set = 0; 13752 bbr->output_error_seen = 1; 13753 if (bbr->oerror_cnt < 0xf) 13754 bbr->oerror_cnt++; 13755 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) { 13756 /* drop the session */ 13757 return (-ENETDOWN); 13758 } 13759 switch (error) { 13760 case ENOBUFS: 13761 /* 13762 * Make this guy have to get ack's to send 13763 * more but lets make sure we don't 13764 * slam him below a T-O (1MSS). 13765 */ 13766 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 13767 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13768 bbr->r_ctl.rc_lost_bytes)) - maxseg; 13769 if (tp->snd_cwnd < maxseg) 13770 tp->snd_cwnd = maxseg; 13771 } 13772 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt; 13773 BBR_STAT_INC(bbr_saw_enobuf); 13774 if (bbr->bbr_hdrw_pacing) 13775 counter_u64_add(bbr_hdwr_pacing_enobuf, 1); 13776 else 13777 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1); 13778 /* 13779 * Here even in the enobuf's case we want to do our 13780 * state update. The reason being we may have been 13781 * called by the input function. If so we have had 13782 * things change. 13783 */ 13784 error = 0; 13785 goto enobufs; 13786 case EMSGSIZE: 13787 /* 13788 * For some reason the interface we used initially 13789 * to send segments changed to another or lowered 13790 * its MTU. If TSO was active we either got an 13791 * interface without TSO capabilits or TSO was 13792 * turned off. If we obtained mtu from ip_output() 13793 * then update it and try again. 13794 */ 13795 /* Turn on tracing (or try to) */ 13796 { 13797 int old_maxseg; 13798 13799 old_maxseg = tp->t_maxseg; 13800 BBR_STAT_INC(bbr_saw_emsgsiz); 13801 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts); 13802 if (mtu != 0) 13803 tcp_mss_update(tp, -1, mtu, NULL, NULL); 13804 if (old_maxseg <= tp->t_maxseg) { 13805 /* Huh it did not shrink? */ 13806 tp->t_maxseg = old_maxseg - 40; 13807 if (tp->t_maxseg < V_tcp_mssdflt) { 13808 /* 13809 * The MSS is so small we should not 13810 * process incoming SACK's since we are 13811 * subject to attack in such a case. 13812 */ 13813 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 13814 } else { 13815 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 13816 } 13817 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts); 13818 } 13819 /* 13820 * Nuke all other things that can interfere 13821 * with slot 13822 */ 13823 if ((tot_len + len) && (len >= tp->t_maxseg)) { 13824 slot = bbr_get_pacing_delay(bbr, 13825 bbr->r_ctl.rc_bbr_hptsi_gain, 13826 (tot_len + len), cts, 0); 13827 if (slot < bbr_error_base_paceout) 13828 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13829 } else 13830 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13831 bbr->rc_output_starts_timer = 1; 13832 bbr_start_hpts_timer(bbr, tp, cts, 10, slot, 13833 tot_len); 13834 return (error); 13835 } 13836 case EPERM: 13837 case EACCES: 13838 tp->t_softerror = error; 13839 /* FALLTHROUGH */ 13840 case EHOSTDOWN: 13841 case EHOSTUNREACH: 13842 case ENETDOWN: 13843 case ENETUNREACH: 13844 if (TCPS_HAVERCVDSYN(tp->t_state)) { 13845 tp->t_softerror = error; 13846 error = 0; 13847 } 13848 /* FALLTHROUGH */ 13849 default: 13850 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt; 13851 bbr->rc_output_starts_timer = 1; 13852 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0); 13853 return (error); 13854 } 13855 #ifdef STATS 13856 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) && 13857 len && 13858 (rsm == NULL) && 13859 (bbr->rc_in_persist == 0)) { 13860 tp->gput_seq = bbr_seq; 13861 tp->gput_ack = bbr_seq + 13862 min(sbavail(&so->so_snd) - sb_offset, sendwin); 13863 tp->gput_ts = cts; 13864 tp->t_flags |= TF_GPUTINPROG; 13865 #endif 13866 } 13867 KMOD_TCPSTAT_INC(tcps_sndtotal); 13868 if ((bbr->bbr_hdw_pace_ena) && 13869 (bbr->bbr_attempt_hdwr_pace == 0) && 13870 (bbr->rc_past_init_win) && 13871 (bbr->rc_bbr_state != BBR_STATE_STARTUP) && 13872 (get_filter_value(&bbr->r_ctl.rc_delrate)) && 13873 (inp->inp_route.ro_nh && 13874 inp->inp_route.ro_nh->nh_ifp)) { 13875 /* 13876 * We are past the initial window and 13877 * have at least one measurement so we 13878 * could use hardware pacing if its available. 13879 * We have an interface and we have not attempted 13880 * to setup hardware pacing, lets try to now. 13881 */ 13882 uint64_t rate_wanted; 13883 int err = 0; 13884 13885 rate_wanted = bbr_get_hardware_rate(bbr); 13886 bbr->bbr_attempt_hdwr_pace = 1; 13887 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp, 13888 inp->inp_route.ro_nh->nh_ifp, 13889 rate_wanted, 13890 (RS_PACING_GEQ|RS_PACING_SUB_OK), 13891 &err, NULL); 13892 if (bbr->r_ctl.crte) { 13893 bbr_type_log_hdwr_pacing(bbr, 13894 bbr->r_ctl.crte->ptbl->rs_ifp, 13895 rate_wanted, 13896 bbr->r_ctl.crte->rate, 13897 __LINE__, cts, err); 13898 BBR_STAT_INC(bbr_hdwr_rl_add_ok); 13899 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 13900 counter_u64_add(bbr_flows_whdwr_pacing, 1); 13901 bbr->bbr_hdrw_pacing = 1; 13902 /* Now what is our gain status? */ 13903 if (bbr->r_ctl.crte->rate < rate_wanted) { 13904 /* We have a problem */ 13905 bbr_setup_less_of_rate(bbr, cts, 13906 bbr->r_ctl.crte->rate, rate_wanted); 13907 } else { 13908 /* We are good */ 13909 bbr->gain_is_limited = 0; 13910 bbr->skip_gain = 0; 13911 } 13912 tcp_bbr_tso_size_check(bbr, cts); 13913 } else { 13914 bbr_type_log_hdwr_pacing(bbr, 13915 inp->inp_route.ro_nh->nh_ifp, 13916 rate_wanted, 13917 0, 13918 __LINE__, cts, err); 13919 BBR_STAT_INC(bbr_hdwr_rl_add_fail); 13920 } 13921 } 13922 if (bbr->bbr_hdrw_pacing) { 13923 /* 13924 * Worry about cases where the route 13925 * changes or something happened that we 13926 * lost our hardware pacing possibly during 13927 * the last ip_output call. 13928 */ 13929 if (inp->inp_snd_tag == NULL) { 13930 /* A change during ip output disabled hw pacing? */ 13931 bbr->bbr_hdrw_pacing = 0; 13932 } else if ((inp->inp_route.ro_nh == NULL) || 13933 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) { 13934 /* 13935 * We had an interface or route change, 13936 * detach from the current hdwr pacing 13937 * and setup to re-attempt next go 13938 * round. 13939 */ 13940 bbr->bbr_hdrw_pacing = 0; 13941 bbr->bbr_attempt_hdwr_pace = 0; 13942 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 13943 tcp_bbr_tso_size_check(bbr, cts); 13944 } 13945 } 13946 /* 13947 * Data sent (as far as we can tell). If this advertises a larger 13948 * window than any other segment, then remember the size of the 13949 * advertised window. Any pending ACK has now been sent. 13950 */ 13951 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 13952 tp->rcv_adv = tp->rcv_nxt + recwin; 13953 13954 tp->last_ack_sent = tp->rcv_nxt; 13955 if ((error == 0) && 13956 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) && 13957 (doing_tlp == 0) && 13958 (tso == 0) && 13959 (len > 0) && 13960 ((flags & TH_RST) == 0) && 13961 ((flags & TH_SYN) == 0) && 13962 (IN_RECOVERY(tp->t_flags) == 0) && 13963 (bbr->rc_in_persist == 0) && 13964 (tot_len < bbr->r_ctl.rc_pace_max_segs)) { 13965 /* 13966 * For non-tso we need to goto again until we have sent out 13967 * enough data to match what we are hptsi out every hptsi 13968 * interval. 13969 */ 13970 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13971 /* Make sure snd_nxt is drug up */ 13972 tp->snd_nxt = tp->snd_max; 13973 } 13974 if (rsm != NULL) { 13975 rsm = NULL; 13976 goto skip_again; 13977 } 13978 rsm = NULL; 13979 sack_rxmit = 0; 13980 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13981 goto again; 13982 } 13983 skip_again: 13984 if ((error == 0) && (flags & TH_FIN)) 13985 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 13986 if ((error == 0) && (flags & TH_RST)) 13987 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 13988 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) { 13989 /* 13990 * Calculate/Re-Calculate the hptsi slot in usecs based on 13991 * what we have sent so far 13992 */ 13993 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 13994 if (bbr->rc_no_pacing) 13995 slot = 0; 13996 } 13997 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 13998 enobufs: 13999 if (bbr->rc_use_google == 0) 14000 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 14001 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 14002 bbr->r_ctl.rc_lost_bytes))); 14003 bbr->rc_output_starts_timer = 1; 14004 if (bbr->bbr_use_rack_cheat && 14005 (more_to_rxt || 14006 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) { 14007 /* Rack cheats and shotguns out all rxt's 1ms apart */ 14008 if (slot > 1000) 14009 slot = 1000; 14010 } 14011 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) { 14012 /* 14013 * We don't change the tso size until some number of sends 14014 * to give the hardware commands time to get down 14015 * to the interface. 14016 */ 14017 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++; 14018 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) { 14019 bbr->hw_pacing_set = 1; 14020 tcp_bbr_tso_size_check(bbr, cts); 14021 } 14022 } 14023 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len); 14024 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 14025 /* Make sure snd_nxt is drug up */ 14026 tp->snd_nxt = tp->snd_max; 14027 } 14028 return (error); 14029 14030 } 14031 14032 /* 14033 * See bbr_output_wtime() for return values. 14034 */ 14035 static int 14036 bbr_output(struct tcpcb *tp) 14037 { 14038 int32_t ret; 14039 struct timeval tv; 14040 14041 NET_EPOCH_ASSERT(); 14042 14043 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14044 (void)tcp_get_usecs(&tv); 14045 ret = bbr_output_wtime(tp, &tv); 14046 return (ret); 14047 } 14048 14049 static void 14050 bbr_mtu_chg(struct tcpcb *tp) 14051 { 14052 struct tcp_bbr *bbr; 14053 struct bbr_sendmap *rsm, *frsm = NULL; 14054 uint32_t maxseg; 14055 14056 /* 14057 * The MTU has changed. a) Clear the sack filter. b) Mark everything 14058 * over the current size as SACK_PASS so a retransmit will occur. 14059 */ 14060 14061 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14062 maxseg = tp->t_maxseg - bbr->rc_last_options; 14063 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 14064 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 14065 /* Don't mess with ones acked (by sack?) */ 14066 if (rsm->r_flags & BBR_ACKED) 14067 continue; 14068 if ((rsm->r_end - rsm->r_start) > maxseg) { 14069 /* 14070 * We mark sack-passed on all the previous large 14071 * sends we did. This will force them to retransmit. 14072 */ 14073 rsm->r_flags |= BBR_SACK_PASSED; 14074 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) && 14075 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) { 14076 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 14077 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 14078 rsm->r_flags |= BBR_MARKED_LOST; 14079 } 14080 if (frsm == NULL) 14081 frsm = rsm; 14082 } 14083 } 14084 if (frsm) { 14085 bbr->r_ctl.rc_resend = frsm; 14086 } 14087 } 14088 14089 static int 14090 bbr_pru_options(struct tcpcb *tp, int flags) 14091 { 14092 if (flags & PRUS_OOB) 14093 return (EOPNOTSUPP); 14094 return (0); 14095 } 14096 14097 static void 14098 bbr_switch_failed(struct tcpcb *tp) 14099 { 14100 /* 14101 * If a switch fails we only need to 14102 * make sure mbuf_queuing is still in place. 14103 * We also need to make sure we are still in 14104 * ticks granularity (though we should probably 14105 * change bbr to go to USECs). 14106 * 14107 * For timers we need to see if we are still in the 14108 * pacer (if our flags are up) if so we are good, if 14109 * not we need to get back into the pacer. 14110 */ 14111 struct timeval tv; 14112 uint32_t cts; 14113 uint32_t toval; 14114 struct tcp_bbr *bbr; 14115 struct hpts_diag diag; 14116 14117 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 14118 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 14119 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 14120 if (tp->t_in_hpts > IHPTS_NONE) { 14121 return; 14122 } 14123 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14124 cts = tcp_get_usecs(&tv); 14125 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14126 if (TSTMP_GT(bbr->rc_pacer_started, cts)) { 14127 toval = bbr->rc_pacer_started - cts; 14128 } else { 14129 /* one slot please */ 14130 toval = HPTS_TICKS_PER_SLOT; 14131 } 14132 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 14133 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 14134 toval = bbr->r_ctl.rc_timer_exp - cts; 14135 } else { 14136 /* one slot please */ 14137 toval = HPTS_TICKS_PER_SLOT; 14138 } 14139 } else 14140 toval = HPTS_TICKS_PER_SLOT; 14141 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval), 14142 __LINE__, &diag); 14143 bbr_log_hpts_diag(bbr, cts, &diag); 14144 } 14145 14146 struct tcp_function_block __tcp_bbr = { 14147 .tfb_tcp_block_name = __XSTRING(STACKNAME), 14148 .tfb_tcp_output = bbr_output, 14149 .tfb_do_queued_segments = ctf_do_queued_segments, 14150 .tfb_do_segment_nounlock = bbr_do_segment_nounlock, 14151 .tfb_tcp_do_segment = bbr_do_segment, 14152 .tfb_tcp_ctloutput = bbr_ctloutput, 14153 .tfb_tcp_fb_init = bbr_init, 14154 .tfb_tcp_fb_fini = bbr_fini, 14155 .tfb_tcp_timer_stop_all = bbr_stopall, 14156 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr, 14157 .tfb_tcp_handoff_ok = bbr_handoff_ok, 14158 .tfb_tcp_mtu_chg = bbr_mtu_chg, 14159 .tfb_pru_options = bbr_pru_options, 14160 .tfb_switch_failed = bbr_switch_failed, 14161 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK, 14162 }; 14163 14164 /* 14165 * bbr_ctloutput() must drop the inpcb lock before performing copyin on 14166 * socket option arguments. When it re-acquires the lock after the copy, it 14167 * has to revalidate that the connection is still valid for the socket 14168 * option. 14169 */ 14170 static int 14171 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14172 { 14173 struct epoch_tracker et; 14174 struct inpcb *inp = tptoinpcb(tp); 14175 struct tcp_bbr *bbr; 14176 int32_t error = 0, optval; 14177 14178 switch (sopt->sopt_level) { 14179 case IPPROTO_IPV6: 14180 case IPPROTO_IP: 14181 return (tcp_default_ctloutput(tp, sopt)); 14182 } 14183 14184 switch (sopt->sopt_name) { 14185 case TCP_RACK_PACE_MAX_SEG: 14186 case TCP_RACK_MIN_TO: 14187 case TCP_RACK_REORD_THRESH: 14188 case TCP_RACK_REORD_FADE: 14189 case TCP_RACK_TLP_THRESH: 14190 case TCP_RACK_PKT_DELAY: 14191 case TCP_BBR_ALGORITHM: 14192 case TCP_BBR_TSLIMITS: 14193 case TCP_BBR_IWINTSO: 14194 case TCP_BBR_STARTUP_PG: 14195 case TCP_BBR_DRAIN_PG: 14196 case TCP_BBR_PROBE_RTT_INT: 14197 case TCP_BBR_PROBE_RTT_GAIN: 14198 case TCP_BBR_PROBE_RTT_LEN: 14199 case TCP_BBR_STARTUP_LOSS_EXIT: 14200 case TCP_BBR_USEDEL_RATE: 14201 case TCP_BBR_MIN_RTO: 14202 case TCP_BBR_MAX_RTO: 14203 case TCP_BBR_PACE_PER_SEC: 14204 case TCP_DELACK: 14205 case TCP_BBR_PACE_DEL_TAR: 14206 case TCP_BBR_SEND_IWND_IN_TSO: 14207 case TCP_BBR_EXTRA_STATE: 14208 case TCP_BBR_UTTER_MAX_TSO: 14209 case TCP_BBR_MIN_TOPACEOUT: 14210 case TCP_BBR_FLOOR_MIN_TSO: 14211 case TCP_BBR_TSTMP_RAISES: 14212 case TCP_BBR_POLICER_DETECT: 14213 case TCP_BBR_USE_RACK_CHEAT: 14214 case TCP_DATA_AFTER_CLOSE: 14215 case TCP_BBR_HDWR_PACE: 14216 case TCP_BBR_PACE_SEG_MAX: 14217 case TCP_BBR_PACE_SEG_MIN: 14218 case TCP_BBR_PACE_CROSS: 14219 case TCP_BBR_PACE_OH: 14220 case TCP_BBR_TMR_PACE_OH: 14221 case TCP_BBR_RACK_RTT_USE: 14222 case TCP_BBR_RETRAN_WTSO: 14223 break; 14224 default: 14225 return (tcp_default_ctloutput(tp, sopt)); 14226 break; 14227 } 14228 INP_WUNLOCK(inp); 14229 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 14230 if (error) 14231 return (error); 14232 INP_WLOCK(inp); 14233 if (inp->inp_flags & INP_DROPPED) { 14234 INP_WUNLOCK(inp); 14235 return (ECONNRESET); 14236 } 14237 if (tp->t_fb != &__tcp_bbr) { 14238 INP_WUNLOCK(inp); 14239 return (ENOPROTOOPT); 14240 } 14241 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14242 switch (sopt->sopt_name) { 14243 case TCP_BBR_PACE_PER_SEC: 14244 BBR_OPTS_INC(tcp_bbr_pace_per_sec); 14245 bbr->r_ctl.bbr_hptsi_per_second = optval; 14246 break; 14247 case TCP_BBR_PACE_DEL_TAR: 14248 BBR_OPTS_INC(tcp_bbr_pace_del_tar); 14249 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval; 14250 break; 14251 case TCP_BBR_PACE_SEG_MAX: 14252 BBR_OPTS_INC(tcp_bbr_pace_seg_max); 14253 bbr->r_ctl.bbr_hptsi_segments_max = optval; 14254 break; 14255 case TCP_BBR_PACE_SEG_MIN: 14256 BBR_OPTS_INC(tcp_bbr_pace_seg_min); 14257 bbr->r_ctl.bbr_hptsi_bytes_min = optval; 14258 break; 14259 case TCP_BBR_PACE_CROSS: 14260 BBR_OPTS_INC(tcp_bbr_pace_cross); 14261 bbr->r_ctl.bbr_cross_over = optval; 14262 break; 14263 case TCP_BBR_ALGORITHM: 14264 BBR_OPTS_INC(tcp_bbr_algorithm); 14265 if (optval && (bbr->rc_use_google == 0)) { 14266 /* Turn on the google mode */ 14267 bbr_google_mode_on(bbr); 14268 if ((optval > 3) && (optval < 500)) { 14269 /* 14270 * Must be at least greater than .3% 14271 * and must be less than 50.0%. 14272 */ 14273 bbr->r_ctl.bbr_google_discount = optval; 14274 } 14275 } else if ((optval == 0) && (bbr->rc_use_google == 1)) { 14276 /* Turn off the google mode */ 14277 bbr_google_mode_off(bbr); 14278 } 14279 break; 14280 case TCP_BBR_TSLIMITS: 14281 BBR_OPTS_INC(tcp_bbr_tslimits); 14282 if (optval == 1) 14283 bbr->rc_use_ts_limit = 1; 14284 else if (optval == 0) 14285 bbr->rc_use_ts_limit = 0; 14286 else 14287 error = EINVAL; 14288 break; 14289 14290 case TCP_BBR_IWINTSO: 14291 BBR_OPTS_INC(tcp_bbr_iwintso); 14292 if ((optval >= 0) && (optval < 128)) { 14293 uint32_t twin; 14294 14295 bbr->rc_init_win = optval; 14296 twin = bbr_initial_cwnd(bbr, tp); 14297 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd)) 14298 tp->snd_cwnd = twin; 14299 else 14300 error = EBUSY; 14301 } else 14302 error = EINVAL; 14303 break; 14304 case TCP_BBR_STARTUP_PG: 14305 BBR_OPTS_INC(tcp_bbr_startup_pg); 14306 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) { 14307 bbr->r_ctl.rc_startup_pg = optval; 14308 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 14309 bbr->r_ctl.rc_bbr_hptsi_gain = optval; 14310 } 14311 } else 14312 error = EINVAL; 14313 break; 14314 case TCP_BBR_DRAIN_PG: 14315 BBR_OPTS_INC(tcp_bbr_drain_pg); 14316 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) 14317 bbr->r_ctl.rc_drain_pg = optval; 14318 else 14319 error = EINVAL; 14320 break; 14321 case TCP_BBR_PROBE_RTT_LEN: 14322 BBR_OPTS_INC(tcp_bbr_probertt_len); 14323 if (optval <= 1) 14324 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND)); 14325 else 14326 error = EINVAL; 14327 break; 14328 case TCP_BBR_PROBE_RTT_GAIN: 14329 BBR_OPTS_INC(tcp_bbr_probertt_gain); 14330 if (optval <= BBR_UNIT) 14331 bbr->r_ctl.bbr_rttprobe_gain_val = optval; 14332 else 14333 error = EINVAL; 14334 break; 14335 case TCP_BBR_PROBE_RTT_INT: 14336 BBR_OPTS_INC(tcp_bbr_probe_rtt_int); 14337 if (optval > 1000) 14338 bbr->r_ctl.rc_probertt_int = optval; 14339 else 14340 error = EINVAL; 14341 break; 14342 case TCP_BBR_MIN_TOPACEOUT: 14343 BBR_OPTS_INC(tcp_bbr_topaceout); 14344 if (optval == 0) { 14345 bbr->no_pacing_until = 0; 14346 bbr->rc_no_pacing = 0; 14347 } else if (optval <= 0x00ff) { 14348 bbr->no_pacing_until = optval; 14349 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) && 14350 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){ 14351 /* Turn on no pacing */ 14352 bbr->rc_no_pacing = 1; 14353 } 14354 } else 14355 error = EINVAL; 14356 break; 14357 case TCP_BBR_STARTUP_LOSS_EXIT: 14358 BBR_OPTS_INC(tcp_bbr_startup_loss_exit); 14359 bbr->rc_loss_exit = optval; 14360 break; 14361 case TCP_BBR_USEDEL_RATE: 14362 error = EINVAL; 14363 break; 14364 case TCP_BBR_MIN_RTO: 14365 BBR_OPTS_INC(tcp_bbr_min_rto); 14366 bbr->r_ctl.rc_min_rto_ms = optval; 14367 break; 14368 case TCP_BBR_MAX_RTO: 14369 BBR_OPTS_INC(tcp_bbr_max_rto); 14370 bbr->rc_max_rto_sec = optval; 14371 break; 14372 case TCP_RACK_MIN_TO: 14373 /* Minimum time between rack t-o's in ms */ 14374 BBR_OPTS_INC(tcp_rack_min_to); 14375 bbr->r_ctl.rc_min_to = optval; 14376 break; 14377 case TCP_RACK_REORD_THRESH: 14378 /* RACK reorder threshold (shift amount) */ 14379 BBR_OPTS_INC(tcp_rack_reord_thresh); 14380 if ((optval > 0) && (optval < 31)) 14381 bbr->r_ctl.rc_reorder_shift = optval; 14382 else 14383 error = EINVAL; 14384 break; 14385 case TCP_RACK_REORD_FADE: 14386 /* Does reordering fade after ms time */ 14387 BBR_OPTS_INC(tcp_rack_reord_fade); 14388 bbr->r_ctl.rc_reorder_fade = optval; 14389 break; 14390 case TCP_RACK_TLP_THRESH: 14391 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14392 BBR_OPTS_INC(tcp_rack_tlp_thresh); 14393 if (optval) 14394 bbr->rc_tlp_threshold = optval; 14395 else 14396 error = EINVAL; 14397 break; 14398 case TCP_BBR_USE_RACK_CHEAT: 14399 BBR_OPTS_INC(tcp_use_rackcheat); 14400 if (bbr->rc_use_google) { 14401 error = EINVAL; 14402 break; 14403 } 14404 BBR_OPTS_INC(tcp_rack_cheat); 14405 if (optval) 14406 bbr->bbr_use_rack_cheat = 1; 14407 else 14408 bbr->bbr_use_rack_cheat = 0; 14409 break; 14410 case TCP_BBR_FLOOR_MIN_TSO: 14411 BBR_OPTS_INC(tcp_utter_max_tso); 14412 if ((optval >= 0) && (optval < 40)) 14413 bbr->r_ctl.bbr_hptsi_segments_floor = optval; 14414 else 14415 error = EINVAL; 14416 break; 14417 case TCP_BBR_UTTER_MAX_TSO: 14418 BBR_OPTS_INC(tcp_utter_max_tso); 14419 if ((optval >= 0) && (optval < 0xffff)) 14420 bbr->r_ctl.bbr_utter_max = optval; 14421 else 14422 error = EINVAL; 14423 break; 14424 14425 case TCP_BBR_EXTRA_STATE: 14426 BBR_OPTS_INC(tcp_extra_state); 14427 if (optval) 14428 bbr->rc_use_idle_restart = 1; 14429 else 14430 bbr->rc_use_idle_restart = 0; 14431 break; 14432 case TCP_BBR_SEND_IWND_IN_TSO: 14433 BBR_OPTS_INC(tcp_iwnd_tso); 14434 if (optval) { 14435 bbr->bbr_init_win_cheat = 1; 14436 if (bbr->rc_past_init_win == 0) { 14437 uint32_t cts; 14438 cts = tcp_get_usecs(&bbr->rc_tv); 14439 tcp_bbr_tso_size_check(bbr, cts); 14440 } 14441 } else 14442 bbr->bbr_init_win_cheat = 0; 14443 break; 14444 case TCP_BBR_HDWR_PACE: 14445 BBR_OPTS_INC(tcp_hdwr_pacing); 14446 if (optval){ 14447 bbr->bbr_hdw_pace_ena = 1; 14448 bbr->bbr_attempt_hdwr_pace = 0; 14449 } else { 14450 bbr->bbr_hdw_pace_ena = 0; 14451 #ifdef RATELIMIT 14452 if (bbr->r_ctl.crte != NULL) { 14453 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 14454 bbr->r_ctl.crte = NULL; 14455 } 14456 #endif 14457 } 14458 break; 14459 14460 case TCP_DELACK: 14461 BBR_OPTS_INC(tcp_delack); 14462 if (optval < 100) { 14463 if (optval == 0) /* off */ 14464 tp->t_delayed_ack = 0; 14465 else if (optval == 1) /* on which is 2 */ 14466 tp->t_delayed_ack = 2; 14467 else /* higher than 2 and less than 100 */ 14468 tp->t_delayed_ack = optval; 14469 if (tp->t_flags & TF_DELACK) { 14470 tp->t_flags &= ~TF_DELACK; 14471 tp->t_flags |= TF_ACKNOW; 14472 NET_EPOCH_ENTER(et); 14473 bbr_output(tp); 14474 NET_EPOCH_EXIT(et); 14475 } 14476 } else 14477 error = EINVAL; 14478 break; 14479 case TCP_RACK_PKT_DELAY: 14480 /* RACK added ms i.e. rack-rtt + reord + N */ 14481 BBR_OPTS_INC(tcp_rack_pkt_delay); 14482 bbr->r_ctl.rc_pkt_delay = optval; 14483 break; 14484 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(tp, sopt)); 14554 break; 14555 } 14556 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 14557 INP_WUNLOCK(inp); 14558 return (error); 14559 } 14560 14561 /* 14562 * return 0 on success, error-num on failure 14563 */ 14564 static int 14565 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14566 { 14567 struct inpcb *inp = tptoinpcb(tp); 14568 struct tcp_bbr *bbr; 14569 uint64_t loptval; 14570 int32_t error, optval; 14571 14572 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14573 if (bbr == NULL) { 14574 INP_WUNLOCK(inp); 14575 return (EINVAL); 14576 } 14577 /* 14578 * Because all our options are either boolean or an int, we can just 14579 * pull everything into optval and then unlock and copy. If we ever 14580 * add a option that is not a int, then this will have quite an 14581 * impact to this routine. 14582 */ 14583 switch (sopt->sopt_name) { 14584 case TCP_BBR_PACE_PER_SEC: 14585 optval = bbr->r_ctl.bbr_hptsi_per_second; 14586 break; 14587 case TCP_BBR_PACE_DEL_TAR: 14588 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar; 14589 break; 14590 case TCP_BBR_PACE_SEG_MAX: 14591 optval = bbr->r_ctl.bbr_hptsi_segments_max; 14592 break; 14593 case TCP_BBR_MIN_TOPACEOUT: 14594 optval = bbr->no_pacing_until; 14595 break; 14596 case TCP_BBR_PACE_SEG_MIN: 14597 optval = bbr->r_ctl.bbr_hptsi_bytes_min; 14598 break; 14599 case TCP_BBR_PACE_CROSS: 14600 optval = bbr->r_ctl.bbr_cross_over; 14601 break; 14602 case TCP_BBR_ALGORITHM: 14603 optval = bbr->rc_use_google; 14604 break; 14605 case TCP_BBR_TSLIMITS: 14606 optval = bbr->rc_use_ts_limit; 14607 break; 14608 case TCP_BBR_IWINTSO: 14609 optval = bbr->rc_init_win; 14610 break; 14611 case TCP_BBR_STARTUP_PG: 14612 optval = bbr->r_ctl.rc_startup_pg; 14613 break; 14614 case TCP_BBR_DRAIN_PG: 14615 optval = bbr->r_ctl.rc_drain_pg; 14616 break; 14617 case TCP_BBR_PROBE_RTT_INT: 14618 optval = bbr->r_ctl.rc_probertt_int; 14619 break; 14620 case TCP_BBR_PROBE_RTT_LEN: 14621 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND); 14622 break; 14623 case TCP_BBR_PROBE_RTT_GAIN: 14624 optval = bbr->r_ctl.bbr_rttprobe_gain_val; 14625 break; 14626 case TCP_BBR_STARTUP_LOSS_EXIT: 14627 optval = bbr->rc_loss_exit; 14628 break; 14629 case TCP_BBR_USEDEL_RATE: 14630 loptval = get_filter_value(&bbr->r_ctl.rc_delrate); 14631 break; 14632 case TCP_BBR_MIN_RTO: 14633 optval = bbr->r_ctl.rc_min_rto_ms; 14634 break; 14635 case TCP_BBR_MAX_RTO: 14636 optval = bbr->rc_max_rto_sec; 14637 break; 14638 case TCP_RACK_PACE_MAX_SEG: 14639 /* Max segments in a pace */ 14640 optval = bbr->r_ctl.rc_pace_max_segs; 14641 break; 14642 case TCP_RACK_MIN_TO: 14643 /* Minimum time between rack t-o's in ms */ 14644 optval = bbr->r_ctl.rc_min_to; 14645 break; 14646 case TCP_RACK_REORD_THRESH: 14647 /* RACK reorder threshold (shift amount) */ 14648 optval = bbr->r_ctl.rc_reorder_shift; 14649 break; 14650 case TCP_RACK_REORD_FADE: 14651 /* Does reordering fade after ms time */ 14652 optval = bbr->r_ctl.rc_reorder_fade; 14653 break; 14654 case TCP_BBR_USE_RACK_CHEAT: 14655 /* Do we use the rack cheat for rxt */ 14656 optval = bbr->bbr_use_rack_cheat; 14657 break; 14658 case TCP_BBR_FLOOR_MIN_TSO: 14659 optval = bbr->r_ctl.bbr_hptsi_segments_floor; 14660 break; 14661 case TCP_BBR_UTTER_MAX_TSO: 14662 optval = bbr->r_ctl.bbr_utter_max; 14663 break; 14664 case TCP_BBR_SEND_IWND_IN_TSO: 14665 /* Do we send TSO size segments initially */ 14666 optval = bbr->bbr_init_win_cheat; 14667 break; 14668 case TCP_BBR_EXTRA_STATE: 14669 optval = bbr->rc_use_idle_restart; 14670 break; 14671 case TCP_RACK_TLP_THRESH: 14672 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14673 optval = bbr->rc_tlp_threshold; 14674 break; 14675 case TCP_RACK_PKT_DELAY: 14676 /* RACK added ms i.e. rack-rtt + reord + N */ 14677 optval = bbr->r_ctl.rc_pkt_delay; 14678 break; 14679 case TCP_BBR_RETRAN_WTSO: 14680 optval = bbr->rc_resends_use_tso; 14681 break; 14682 case TCP_DATA_AFTER_CLOSE: 14683 optval = bbr->rc_allow_data_af_clo; 14684 break; 14685 case TCP_DELACK: 14686 optval = tp->t_delayed_ack; 14687 break; 14688 case TCP_BBR_HDWR_PACE: 14689 optval = bbr->bbr_hdw_pace_ena; 14690 break; 14691 case TCP_BBR_POLICER_DETECT: 14692 optval = bbr->r_use_policer; 14693 break; 14694 case TCP_BBR_TSTMP_RAISES: 14695 optval = bbr->ts_can_raise; 14696 break; 14697 case TCP_BBR_TMR_PACE_OH: 14698 optval = bbr->r_ctl.rc_incr_tmrs; 14699 break; 14700 case TCP_BBR_PACE_OH: 14701 optval = 0; 14702 if (bbr->r_ctl.rc_inc_tcp_oh) 14703 optval |= BBR_INCL_TCP_OH; 14704 if (bbr->r_ctl.rc_inc_ip_oh) 14705 optval |= BBR_INCL_IP_OH; 14706 if (bbr->r_ctl.rc_inc_enet_oh) 14707 optval |= BBR_INCL_ENET_OH; 14708 break; 14709 default: 14710 return (tcp_default_ctloutput(tp, sopt)); 14711 break; 14712 } 14713 INP_WUNLOCK(inp); 14714 if (sopt->sopt_name == TCP_BBR_USEDEL_RATE) 14715 error = sooptcopyout(sopt, &loptval, sizeof loptval); 14716 else 14717 error = sooptcopyout(sopt, &optval, sizeof optval); 14718 return (error); 14719 } 14720 14721 /* 14722 * return 0 on success, error-num on failure 14723 */ 14724 static int 14725 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 14726 { 14727 if (sopt->sopt_dir == SOPT_SET) { 14728 return (bbr_set_sockopt(tp, sopt)); 14729 } else if (sopt->sopt_dir == SOPT_GET) { 14730 return (bbr_get_sockopt(tp, sopt)); 14731 } else { 14732 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 14733 } 14734 } 14735 14736 static const char *bbr_stack_names[] = { 14737 __XSTRING(STACKNAME), 14738 #ifdef STACKALIAS 14739 __XSTRING(STACKALIAS), 14740 #endif 14741 }; 14742 14743 static bool bbr_mod_inited = false; 14744 14745 static int 14746 tcp_addbbr(module_t mod, int32_t type, void *data) 14747 { 14748 int32_t err = 0; 14749 int num_stacks; 14750 14751 switch (type) { 14752 case MOD_LOAD: 14753 printf("Attempting to load " __XSTRING(MODNAME) "\n"); 14754 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 14755 sizeof(struct bbr_sendmap), 14756 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 14757 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 14758 sizeof(struct tcp_bbr), 14759 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 14760 sysctl_ctx_init(&bbr_sysctl_ctx); 14761 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 14762 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 14763 OID_AUTO, 14764 #ifdef STACKALIAS 14765 __XSTRING(STACKALIAS), 14766 #else 14767 __XSTRING(STACKNAME), 14768 #endif 14769 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 14770 ""); 14771 if (bbr_sysctl_root == NULL) { 14772 printf("Failed to add sysctl node\n"); 14773 err = EFAULT; 14774 goto free_uma; 14775 } 14776 bbr_init_sysctls(); 14777 num_stacks = nitems(bbr_stack_names); 14778 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK, 14779 bbr_stack_names, &num_stacks); 14780 if (err) { 14781 printf("Failed to register %s stack name for " 14782 "%s module\n", bbr_stack_names[num_stacks], 14783 __XSTRING(MODNAME)); 14784 sysctl_ctx_free(&bbr_sysctl_ctx); 14785 free_uma: 14786 uma_zdestroy(bbr_zone); 14787 uma_zdestroy(bbr_pcb_zone); 14788 bbr_counter_destroy(); 14789 printf("Failed to register " __XSTRING(MODNAME) 14790 " module err:%d\n", err); 14791 return (err); 14792 } 14793 tcp_lro_reg_mbufq(); 14794 bbr_mod_inited = true; 14795 printf(__XSTRING(MODNAME) " is now available\n"); 14796 break; 14797 case MOD_QUIESCE: 14798 err = deregister_tcp_functions(&__tcp_bbr, true, false); 14799 break; 14800 case MOD_UNLOAD: 14801 err = deregister_tcp_functions(&__tcp_bbr, false, true); 14802 if (err == EBUSY) 14803 break; 14804 if (bbr_mod_inited) { 14805 uma_zdestroy(bbr_zone); 14806 uma_zdestroy(bbr_pcb_zone); 14807 sysctl_ctx_free(&bbr_sysctl_ctx); 14808 bbr_counter_destroy(); 14809 printf(__XSTRING(MODNAME) 14810 " is now no longer available\n"); 14811 bbr_mod_inited = false; 14812 } 14813 tcp_lro_dereg_mbufq(); 14814 err = 0; 14815 break; 14816 default: 14817 return (EOPNOTSUPP); 14818 } 14819 return (err); 14820 } 14821 14822 static moduledata_t tcp_bbr = { 14823 .name = __XSTRING(MODNAME), 14824 .evhand = tcp_addbbr, 14825 .priv = 0 14826 }; 14827 14828 MODULE_VERSION(MODNAME, 1); 14829 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 14830 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 14831