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, 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 (sbavail(&inp->inp_socket->so_snd) && 982 (tmr_up == PACE_TMR_RXT)) { 983 /* 984 * if we hit enobufs then we would expect the 985 * possibility of nothing outstanding and the RXT up 986 * (and the hptsi timer). 987 */ 988 return; 989 } else if (((V_tcp_always_keepalive || 990 inp->inp_socket->so_options & SO_KEEPALIVE) && 991 (tp->t_state <= TCPS_CLOSING)) && 992 (tmr_up == PACE_TMR_KEEP) && 993 (tp->snd_max == tp->snd_una)) { 994 /* We should have keep alive up and we do */ 995 return; 996 } 997 } 998 if (rsm && (rsm->r_flags & BBR_SACK_PASSED)) { 999 if ((tp->t_flags & TF_SENTFIN) && 1000 ((tp->snd_max - tp->snd_una) == 1) && 1001 (rsm->r_flags & BBR_HAS_FIN)) { 1002 /* needs to be a RXT */ 1003 if (tmr_up == PACE_TMR_RXT) 1004 return; 1005 else 1006 goto wrong_timer; 1007 } else if (tmr_up == PACE_TMR_RACK) 1008 return; 1009 else 1010 goto wrong_timer; 1011 } else if (rsm && (tmr_up == PACE_TMR_RACK)) { 1012 /* Rack timer has priority if we have data out */ 1013 return; 1014 } else if (SEQ_GT(tp->snd_max, tp->snd_una) && 1015 ((tmr_up == PACE_TMR_TLP) || 1016 (tmr_up == PACE_TMR_RXT))) { 1017 /* 1018 * Either a TLP or RXT is fine if no sack-passed is in place 1019 * and data is outstanding. 1020 */ 1021 return; 1022 } else if (tmr_up == PACE_TMR_DELACK) { 1023 /* 1024 * If the delayed ack was going to go off before the 1025 * rtx/tlp/rack timer were going to expire, then that would 1026 * be the timer in control. Note we don't check the time 1027 * here trusting the code is correct. 1028 */ 1029 return; 1030 } 1031 if (SEQ_GT(tp->snd_max, tp->snd_una) && 1032 ((tmr_up == PACE_TMR_RXT) || 1033 (tmr_up == PACE_TMR_TLP) || 1034 (tmr_up == PACE_TMR_RACK))) { 1035 /* 1036 * We have outstanding data and 1037 * we *do* have a RACK, TLP or RXT 1038 * timer running. We won't restart 1039 * anything here since thats probably ok we 1040 * will get called with some timer here shortly. 1041 */ 1042 return; 1043 } 1044 /* 1045 * Ok the timer originally started is not what we want now. We will 1046 * force the hpts to be stopped if any, and restart with the slot 1047 * set to what was in the saved slot. 1048 */ 1049 wrong_timer: 1050 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) { 1051 if (tcp_in_hpts(tp)) 1052 tcp_hpts_remove(tp); 1053 bbr_timer_cancel(bbr, __LINE__, cts); 1054 bbr_start_hpts_timer(bbr, tp, cts, 1, bbr->r_ctl.rc_last_delay_val, 1055 0); 1056 } else { 1057 /* 1058 * Output is hptsi so we just need to switch the type of 1059 * timer. We don't bother with keep-alive, since when we 1060 * jump through the output, it will start the keep-alive if 1061 * nothing is sent. 1062 * 1063 * We only need a delayed-ack added and or the hpts_timeout. 1064 */ 1065 hpts_timeout = bbr_timer_start(tp, bbr, cts); 1066 if (tp->t_flags & TF_DELACK) { 1067 if (hpts_timeout == 0) { 1068 hpts_timeout = bbr_delack_time; 1069 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1070 } 1071 else if (hpts_timeout > bbr_delack_time) { 1072 hpts_timeout = bbr_delack_time; 1073 bbr->r_ctl.rc_hpts_flags = PACE_TMR_DELACK; 1074 } 1075 } 1076 if (hpts_timeout) { 1077 if (hpts_timeout > 0x7ffffffe) 1078 hpts_timeout = 0x7ffffffe; 1079 bbr->r_ctl.rc_timer_exp = cts + hpts_timeout; 1080 } 1081 } 1082 } 1083 1084 int32_t bbr_clear_lost = 0; 1085 1086 /* 1087 * Considers the two time values now (cts) and earlier. 1088 * If cts is smaller than earlier, we could have 1089 * had a sequence wrap (our counter wraps every 1090 * 70 min or so) or it could be just clock skew 1091 * getting us two different time values. Clock skew 1092 * will show up within 10ms or so. So in such 1093 * a case (where cts is behind earlier time by 1094 * less than 10ms) we return 0. Otherwise we 1095 * return the true difference between them. 1096 */ 1097 static inline uint32_t 1098 bbr_calc_time(uint32_t cts, uint32_t earlier_time) { 1099 /* 1100 * Given two timestamps, the current time stamp cts, and some other 1101 * time-stamp taken in theory earlier return the difference. The 1102 * trick is here sometimes locking will get the other timestamp 1103 * after the cts. If this occurs we need to return 0. 1104 */ 1105 if (TSTMP_GEQ(cts, earlier_time)) 1106 return (cts - earlier_time); 1107 /* 1108 * cts is behind earlier_time if its less than 10ms consider it 0. 1109 * If its more than 10ms difference then we had a time wrap. Else 1110 * its just the normal locking foo. I wonder if we should not go to 1111 * 64bit TS and get rid of this issue. 1112 */ 1113 if (TSTMP_GEQ((cts + 10000), earlier_time)) 1114 return (0); 1115 /* 1116 * Ok the time must have wrapped. So we need to answer a large 1117 * amount of time, which the normal subtraction should do. 1118 */ 1119 return (cts - earlier_time); 1120 } 1121 1122 static int 1123 sysctl_bbr_clear_lost(SYSCTL_HANDLER_ARGS) 1124 { 1125 uint32_t stat; 1126 int32_t error; 1127 1128 error = SYSCTL_OUT(req, &bbr_clear_lost, sizeof(uint32_t)); 1129 if (error || req->newptr == NULL) 1130 return error; 1131 1132 error = SYSCTL_IN(req, &stat, sizeof(uint32_t)); 1133 if (error) 1134 return (error); 1135 if (stat == 1) { 1136 #ifdef BBR_INVARIANTS 1137 printf("Clearing BBR lost counters\n"); 1138 #endif 1139 COUNTER_ARRAY_ZERO(bbr_state_lost, BBR_MAX_STAT); 1140 COUNTER_ARRAY_ZERO(bbr_state_time, BBR_MAX_STAT); 1141 COUNTER_ARRAY_ZERO(bbr_state_resend, BBR_MAX_STAT); 1142 } else if (stat == 2) { 1143 #ifdef BBR_INVARIANTS 1144 printf("Clearing BBR option counters\n"); 1145 #endif 1146 COUNTER_ARRAY_ZERO(bbr_opts_arry, BBR_OPTS_SIZE); 1147 } else if (stat == 3) { 1148 #ifdef BBR_INVARIANTS 1149 printf("Clearing BBR stats counters\n"); 1150 #endif 1151 COUNTER_ARRAY_ZERO(bbr_stat_arry, BBR_STAT_SIZE); 1152 } else if (stat == 4) { 1153 #ifdef BBR_INVARIANTS 1154 printf("Clearing BBR out-size counters\n"); 1155 #endif 1156 COUNTER_ARRAY_ZERO(bbr_out_size, TCP_MSS_ACCT_SIZE); 1157 } 1158 bbr_clear_lost = 0; 1159 return (0); 1160 } 1161 1162 static void 1163 bbr_init_sysctls(void) 1164 { 1165 struct sysctl_oid *bbr_probertt; 1166 struct sysctl_oid *bbr_hptsi; 1167 struct sysctl_oid *bbr_measure; 1168 struct sysctl_oid *bbr_cwnd; 1169 struct sysctl_oid *bbr_timeout; 1170 struct sysctl_oid *bbr_states; 1171 struct sysctl_oid *bbr_startup; 1172 struct sysctl_oid *bbr_policer; 1173 1174 /* Probe rtt controls */ 1175 bbr_probertt = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1176 SYSCTL_CHILDREN(bbr_sysctl_root), 1177 OID_AUTO, 1178 "probertt", 1179 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1180 ""); 1181 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1182 SYSCTL_CHILDREN(bbr_probertt), 1183 OID_AUTO, "gain", CTLFLAG_RW, 1184 &bbr_rttprobe_gain, 192, 1185 "What is the filter gain drop in probe_rtt (0=disable)?"); 1186 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1187 SYSCTL_CHILDREN(bbr_probertt), 1188 OID_AUTO, "cwnd", CTLFLAG_RW, 1189 &bbr_rtt_probe_cwndtarg, 4, 1190 "How many mss's are outstanding during probe-rtt"); 1191 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1192 SYSCTL_CHILDREN(bbr_probertt), 1193 OID_AUTO, "int", CTLFLAG_RW, 1194 &bbr_rtt_probe_limit, 4000000, 1195 "If RTT has not shrank in this many micro-seconds enter probe-rtt"); 1196 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1197 SYSCTL_CHILDREN(bbr_probertt), 1198 OID_AUTO, "mintime", CTLFLAG_RW, 1199 &bbr_rtt_probe_time, 200000, 1200 "How many microseconds in probe-rtt"); 1201 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1202 SYSCTL_CHILDREN(bbr_probertt), 1203 OID_AUTO, "filter_len_sec", CTLFLAG_RW, 1204 &bbr_filter_len_sec, 6, 1205 "How long in seconds does the rttProp filter run?"); 1206 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1207 SYSCTL_CHILDREN(bbr_probertt), 1208 OID_AUTO, "drain_rtt", CTLFLAG_RW, 1209 &bbr_drain_rtt, BBR_SRTT, 1210 "What is the drain rtt to use in probeRTT (rtt_prop=0, rtt_rack=1, rtt_pkt=2, rtt_srtt=3?"); 1211 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1212 SYSCTL_CHILDREN(bbr_probertt), 1213 OID_AUTO, "can_force", CTLFLAG_RW, 1214 &bbr_can_force_probertt, 0, 1215 "If we keep setting new low rtt's but delay going in probe-rtt can we force in??"); 1216 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1217 SYSCTL_CHILDREN(bbr_probertt), 1218 OID_AUTO, "enter_sets_force", CTLFLAG_RW, 1219 &bbr_probertt_sets_rtt, 0, 1220 "In NF mode, do we imitate google_mode and set the rttProp on entry to probe-rtt?"); 1221 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1222 SYSCTL_CHILDREN(bbr_probertt), 1223 OID_AUTO, "can_adjust", CTLFLAG_RW, 1224 &bbr_can_adjust_probertt, 1, 1225 "Can we dynamically adjust the probe-rtt limits and times?"); 1226 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1227 SYSCTL_CHILDREN(bbr_probertt), 1228 OID_AUTO, "is_ratio", CTLFLAG_RW, 1229 &bbr_is_ratio, 0, 1230 "is the limit to filter a ratio?"); 1231 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1232 SYSCTL_CHILDREN(bbr_probertt), 1233 OID_AUTO, "use_cwnd", CTLFLAG_RW, 1234 &bbr_prtt_slam_cwnd, 0, 1235 "Should we set/recover cwnd?"); 1236 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1237 SYSCTL_CHILDREN(bbr_probertt), 1238 OID_AUTO, "can_use_ts", CTLFLAG_RW, 1239 &bbr_can_use_ts_for_rtt, 1, 1240 "Can we use the ms timestamp if available for retransmistted rtt calculations?"); 1241 1242 /* Pacing controls */ 1243 bbr_hptsi = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1244 SYSCTL_CHILDREN(bbr_sysctl_root), 1245 OID_AUTO, 1246 "pacing", 1247 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1248 ""); 1249 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1250 SYSCTL_CHILDREN(bbr_hptsi), 1251 OID_AUTO, "hw_pacing", CTLFLAG_RW, 1252 &bbr_allow_hdwr_pacing, 1, 1253 "Do we allow hardware pacing?"); 1254 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1255 SYSCTL_CHILDREN(bbr_hptsi), 1256 OID_AUTO, "hw_pacing_limit", CTLFLAG_RW, 1257 &bbr_hardware_pacing_limit, 4000, 1258 "Do we have a limited number of connections for pacing chelsio (0=no limit)?"); 1259 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1260 SYSCTL_CHILDREN(bbr_hptsi), 1261 OID_AUTO, "hw_pacing_adj", CTLFLAG_RW, 1262 &bbr_hdwr_pace_adjust, 2, 1263 "Multiplier to calculated tso size?"); 1264 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1265 SYSCTL_CHILDREN(bbr_hptsi), 1266 OID_AUTO, "hw_pacing_floor", CTLFLAG_RW, 1267 &bbr_hdwr_pace_floor, 1, 1268 "Do we invoke the hardware pacing floor?"); 1269 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1270 SYSCTL_CHILDREN(bbr_hptsi), 1271 OID_AUTO, "hw_pacing_delay_cnt", CTLFLAG_RW, 1272 &bbr_hdwr_pacing_delay_cnt, 10, 1273 "How many packets must be sent after hdwr pacing is enabled"); 1274 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1275 SYSCTL_CHILDREN(bbr_hptsi), 1276 OID_AUTO, "bw_cross", CTLFLAG_RW, 1277 &bbr_cross_over, 3000000, 1278 "What is the point where we cross over to linux like TSO size set"); 1279 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1280 SYSCTL_CHILDREN(bbr_hptsi), 1281 OID_AUTO, "seg_deltarg", CTLFLAG_RW, 1282 &bbr_hptsi_segments_delay_tar, 7000, 1283 "What is the worse case delay target for hptsi < 48Mbp connections"); 1284 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1285 SYSCTL_CHILDREN(bbr_hptsi), 1286 OID_AUTO, "enet_oh", CTLFLAG_RW, 1287 &bbr_include_enet_oh, 0, 1288 "Do we include the ethernet overhead in calculating pacing delay?"); 1289 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1290 SYSCTL_CHILDREN(bbr_hptsi), 1291 OID_AUTO, "ip_oh", CTLFLAG_RW, 1292 &bbr_include_ip_oh, 1, 1293 "Do we include the IP overhead in calculating pacing delay?"); 1294 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1295 SYSCTL_CHILDREN(bbr_hptsi), 1296 OID_AUTO, "tcp_oh", CTLFLAG_RW, 1297 &bbr_include_tcp_oh, 0, 1298 "Do we include the TCP overhead in calculating pacing delay?"); 1299 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1300 SYSCTL_CHILDREN(bbr_hptsi), 1301 OID_AUTO, "google_discount", CTLFLAG_RW, 1302 &bbr_google_discount, 10, 1303 "What is the default google discount percentage wise for pacing (11 = 1.1%%)?"); 1304 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1305 SYSCTL_CHILDREN(bbr_hptsi), 1306 OID_AUTO, "all_get_min", CTLFLAG_RW, 1307 &bbr_all_get_min, 0, 1308 "If you are less than a MSS do you just get the min?"); 1309 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1310 SYSCTL_CHILDREN(bbr_hptsi), 1311 OID_AUTO, "tso_min", CTLFLAG_RW, 1312 &bbr_hptsi_bytes_min, 1460, 1313 "For 0 -> 24Mbps what is floor number of segments for TSO"); 1314 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1315 SYSCTL_CHILDREN(bbr_hptsi), 1316 OID_AUTO, "seg_tso_max", CTLFLAG_RW, 1317 &bbr_hptsi_segments_max, 6, 1318 "For 0 -> 24Mbps what is top number of segments for TSO"); 1319 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1320 SYSCTL_CHILDREN(bbr_hptsi), 1321 OID_AUTO, "seg_floor", CTLFLAG_RW, 1322 &bbr_hptsi_segments_floor, 1, 1323 "Minimum TSO size we will fall too in segments"); 1324 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1325 SYSCTL_CHILDREN(bbr_hptsi), 1326 OID_AUTO, "utter_max", CTLFLAG_RW, 1327 &bbr_hptsi_utter_max, 0, 1328 "The absolute maximum that any pacing (outside of hardware) can be"); 1329 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1330 SYSCTL_CHILDREN(bbr_hptsi), 1331 OID_AUTO, "seg_divisor", CTLFLAG_RW, 1332 &bbr_hptsi_per_second, 100, 1333 "What is the divisor in our hptsi TSO calculation 512Mbps < X > 24Mbps "); 1334 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1335 SYSCTL_CHILDREN(bbr_hptsi), 1336 OID_AUTO, "srtt_mul", CTLFLAG_RW, 1337 &bbr_hptsi_max_mul, 1, 1338 "The multiplier for pace len max"); 1339 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1340 SYSCTL_CHILDREN(bbr_hptsi), 1341 OID_AUTO, "srtt_div", CTLFLAG_RW, 1342 &bbr_hptsi_max_div, 2, 1343 "The divisor for pace len max"); 1344 /* Measurement controls */ 1345 bbr_measure = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1346 SYSCTL_CHILDREN(bbr_sysctl_root), 1347 OID_AUTO, 1348 "measure", 1349 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1350 "Measurement controls"); 1351 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1352 SYSCTL_CHILDREN(bbr_measure), 1353 OID_AUTO, "min_i_bw", CTLFLAG_RW, 1354 &bbr_initial_bw_bps, 62500, 1355 "Minimum initial b/w in bytes per second"); 1356 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1357 SYSCTL_CHILDREN(bbr_measure), 1358 OID_AUTO, "no_sack_needed", CTLFLAG_RW, 1359 &bbr_sack_not_required, 0, 1360 "Do we allow bbr to run on connections not supporting SACK?"); 1361 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1362 SYSCTL_CHILDREN(bbr_measure), 1363 OID_AUTO, "use_google", CTLFLAG_RW, 1364 &bbr_use_google_algo, 0, 1365 "Use has close to google V1.0 has possible?"); 1366 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1367 SYSCTL_CHILDREN(bbr_measure), 1368 OID_AUTO, "ts_limiting", CTLFLAG_RW, 1369 &bbr_ts_limiting, 1, 1370 "Do we attempt to use the peers timestamp to limit b/w caculations?"); 1371 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1372 SYSCTL_CHILDREN(bbr_measure), 1373 OID_AUTO, "ts_can_raise", CTLFLAG_RW, 1374 &bbr_ts_can_raise, 0, 1375 "Can we raise the b/w via timestamp b/w calculation?"); 1376 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1377 SYSCTL_CHILDREN(bbr_measure), 1378 OID_AUTO, "ts_delta", CTLFLAG_RW, 1379 &bbr_min_usec_delta, 20000, 1380 "How long in usec between ts of our sends in ts validation code?"); 1381 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1382 SYSCTL_CHILDREN(bbr_measure), 1383 OID_AUTO, "ts_peer_delta", CTLFLAG_RW, 1384 &bbr_min_peer_delta, 20, 1385 "What min numerical value should be between the peer deltas?"); 1386 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1387 SYSCTL_CHILDREN(bbr_measure), 1388 OID_AUTO, "ts_delta_percent", CTLFLAG_RW, 1389 &bbr_delta_percent, 150, 1390 "What percentage (150 = 15.0) do we allow variance for?"); 1391 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1392 SYSCTL_CHILDREN(bbr_measure), 1393 OID_AUTO, "min_measure_good_bw", CTLFLAG_RW, 1394 &bbr_min_measurements_req, 1, 1395 "What is the minimum measurement count we need before we switch to our b/w estimate"); 1396 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1397 SYSCTL_CHILDREN(bbr_measure), 1398 OID_AUTO, "min_measure_before_pace", CTLFLAG_RW, 1399 &bbr_no_pacing_until, 4, 1400 "How many pkt-epoch's (0 is off) do we need before pacing is on?"); 1401 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1402 SYSCTL_CHILDREN(bbr_measure), 1403 OID_AUTO, "quanta", CTLFLAG_RW, 1404 &bbr_quanta, 2, 1405 "Extra quanta to add when calculating the target (ID section 4.2.3.2)."); 1406 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1407 SYSCTL_CHILDREN(bbr_measure), 1408 OID_AUTO, "noretran", CTLFLAG_RW, 1409 &bbr_no_retran, 0, 1410 "Should google mode not use retransmission measurements for the b/w estimation?"); 1411 /* State controls */ 1412 bbr_states = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1413 SYSCTL_CHILDREN(bbr_sysctl_root), 1414 OID_AUTO, 1415 "states", 1416 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1417 "State controls"); 1418 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1419 SYSCTL_CHILDREN(bbr_states), 1420 OID_AUTO, "idle_restart", CTLFLAG_RW, 1421 &bbr_uses_idle_restart, 0, 1422 "Do we use a new special idle_restart state to ramp back up quickly?"); 1423 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1424 SYSCTL_CHILDREN(bbr_states), 1425 OID_AUTO, "idle_restart_threshold", CTLFLAG_RW, 1426 &bbr_idle_restart_threshold, 100000, 1427 "How long must we be idle before we restart??"); 1428 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1429 SYSCTL_CHILDREN(bbr_states), 1430 OID_AUTO, "use_pkt_epoch", CTLFLAG_RW, 1431 &bbr_state_is_pkt_epoch, 0, 1432 "Do we use a pkt-epoch for substate if 0 rttProp?"); 1433 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1434 SYSCTL_CHILDREN(bbr_states), 1435 OID_AUTO, "startup_rtt_gain", CTLFLAG_RW, 1436 &bbr_rtt_gain_thresh, 0, 1437 "What increase in RTT triggers us to stop ignoring no-loss and possibly exit startup?"); 1438 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1439 SYSCTL_CHILDREN(bbr_states), 1440 OID_AUTO, "drain_floor", CTLFLAG_RW, 1441 &bbr_drain_floor, 88, 1442 "What is the lowest we can drain (pg) too?"); 1443 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1444 SYSCTL_CHILDREN(bbr_states), 1445 OID_AUTO, "drain_2_target", CTLFLAG_RW, 1446 &bbr_state_drain_2_tar, 1, 1447 "Do we drain to target in drain substate?"); 1448 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1449 SYSCTL_CHILDREN(bbr_states), 1450 OID_AUTO, "gain_2_target", CTLFLAG_RW, 1451 &bbr_gain_to_target, 1, 1452 "Does probe bw gain to target??"); 1453 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1454 SYSCTL_CHILDREN(bbr_states), 1455 OID_AUTO, "gain_extra_time", CTLFLAG_RW, 1456 &bbr_gain_gets_extra_too, 1, 1457 "Does probe bw gain get the extra time too?"); 1458 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1459 SYSCTL_CHILDREN(bbr_states), 1460 OID_AUTO, "ld_div", CTLFLAG_RW, 1461 &bbr_drain_drop_div, 5, 1462 "Long drain drop divider?"); 1463 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1464 SYSCTL_CHILDREN(bbr_states), 1465 OID_AUTO, "ld_mul", CTLFLAG_RW, 1466 &bbr_drain_drop_mul, 4, 1467 "Long drain drop multiplier?"); 1468 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1469 SYSCTL_CHILDREN(bbr_states), 1470 OID_AUTO, "rand_ot_disc", CTLFLAG_RW, 1471 &bbr_rand_ot, 50, 1472 "Random discount of the ot?"); 1473 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1474 SYSCTL_CHILDREN(bbr_states), 1475 OID_AUTO, "dr_filter_life", CTLFLAG_RW, 1476 &bbr_num_pktepo_for_del_limit, BBR_NUM_RTTS_FOR_DEL_LIMIT, 1477 "How many packet-epochs does the b/w delivery rate last?"); 1478 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1479 SYSCTL_CHILDREN(bbr_states), 1480 OID_AUTO, "subdrain_applimited", CTLFLAG_RW, 1481 &bbr_sub_drain_app_limit, 0, 1482 "Does our sub-state drain invoke app limited if its long?"); 1483 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1484 SYSCTL_CHILDREN(bbr_states), 1485 OID_AUTO, "use_cwnd_subdrain", CTLFLAG_RW, 1486 &bbr_sub_drain_slam_cwnd, 0, 1487 "Should we set/recover cwnd for sub-state drain?"); 1488 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1489 SYSCTL_CHILDREN(bbr_states), 1490 OID_AUTO, "use_cwnd_maindrain", CTLFLAG_RW, 1491 &bbr_slam_cwnd_in_main_drain, 0, 1492 "Should we set/recover cwnd for main-state drain?"); 1493 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1494 SYSCTL_CHILDREN(bbr_states), 1495 OID_AUTO, "google_gets_earlyout", CTLFLAG_RW, 1496 &google_allow_early_out, 1, 1497 "Should we allow google probe-bw/drain to exit early at flight target?"); 1498 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1499 SYSCTL_CHILDREN(bbr_states), 1500 OID_AUTO, "google_exit_loss", CTLFLAG_RW, 1501 &google_consider_lost, 1, 1502 "Should we have losses exit gain of probebw in google mode??"); 1503 /* Startup controls */ 1504 bbr_startup = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1505 SYSCTL_CHILDREN(bbr_sysctl_root), 1506 OID_AUTO, 1507 "startup", 1508 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1509 "Startup controls"); 1510 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1511 SYSCTL_CHILDREN(bbr_startup), 1512 OID_AUTO, "cheat_iwnd", CTLFLAG_RW, 1513 &bbr_sends_full_iwnd, 1, 1514 "Do we not pace but burst out initial windows has our TSO size?"); 1515 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1516 SYSCTL_CHILDREN(bbr_startup), 1517 OID_AUTO, "loss_threshold", CTLFLAG_RW, 1518 &bbr_startup_loss_thresh, 2000, 1519 "In startup what is the loss threshold in a pe that will exit us from startup?"); 1520 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1521 SYSCTL_CHILDREN(bbr_startup), 1522 OID_AUTO, "use_lowerpg", CTLFLAG_RW, 1523 &bbr_use_lower_gain_in_startup, 1, 1524 "Should we use a lower hptsi gain if we see loss in startup?"); 1525 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1526 SYSCTL_CHILDREN(bbr_startup), 1527 OID_AUTO, "gain", CTLFLAG_RW, 1528 &bbr_start_exit, 25, 1529 "What gain percent do we need to see to stay in startup??"); 1530 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1531 SYSCTL_CHILDREN(bbr_startup), 1532 OID_AUTO, "low_gain", CTLFLAG_RW, 1533 &bbr_low_start_exit, 15, 1534 "What gain percent do we need to see to stay in the lower gain startup??"); 1535 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1536 SYSCTL_CHILDREN(bbr_startup), 1537 OID_AUTO, "loss_exit", CTLFLAG_RW, 1538 &bbr_exit_startup_at_loss, 1, 1539 "Should we exit startup at loss in an epoch if we are not gaining?"); 1540 /* CWND controls */ 1541 bbr_cwnd = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1542 SYSCTL_CHILDREN(bbr_sysctl_root), 1543 OID_AUTO, 1544 "cwnd", 1545 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1546 "Cwnd controls"); 1547 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1548 SYSCTL_CHILDREN(bbr_cwnd), 1549 OID_AUTO, "tar_rtt", CTLFLAG_RW, 1550 &bbr_cwndtarget_rtt_touse, 0, 1551 "Target cwnd rtt measurement to use (0=rtt_prop, 1=rtt_rack, 2=pkt_rtt, 3=srtt)?"); 1552 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1553 SYSCTL_CHILDREN(bbr_cwnd), 1554 OID_AUTO, "may_shrink", CTLFLAG_RW, 1555 &bbr_cwnd_may_shrink, 0, 1556 "Can the cwnd shrink if it would grow to more than the target?"); 1557 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1558 SYSCTL_CHILDREN(bbr_cwnd), 1559 OID_AUTO, "max_target_limit", CTLFLAG_RW, 1560 &bbr_target_cwnd_mult_limit, 8, 1561 "Do we limit the cwnd to some multiple of the cwnd target if cwnd can't shrink 0=no?"); 1562 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1563 SYSCTL_CHILDREN(bbr_cwnd), 1564 OID_AUTO, "highspeed_min", CTLFLAG_RW, 1565 &bbr_cwnd_min_val_hs, BBR_HIGHSPEED_NUM_MSS, 1566 "What is the high-speed min cwnd (rttProp under 1ms)"); 1567 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1568 SYSCTL_CHILDREN(bbr_cwnd), 1569 OID_AUTO, "lowspeed_min", CTLFLAG_RW, 1570 &bbr_cwnd_min_val, BBR_PROBERTT_NUM_MSS, 1571 "What is the min cwnd (rttProp > 1ms)"); 1572 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1573 SYSCTL_CHILDREN(bbr_cwnd), 1574 OID_AUTO, "initwin", CTLFLAG_RW, 1575 &bbr_def_init_win, 10, 1576 "What is the BBR initial window, if 0 use tcp version"); 1577 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1578 SYSCTL_CHILDREN(bbr_cwnd), 1579 OID_AUTO, "do_loss_red", CTLFLAG_RW, 1580 &bbr_do_red, 600, 1581 "Do we reduce the b/w at exit from recovery based on ratio of prop/srtt (800=80.0, 0=off)?"); 1582 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1583 SYSCTL_CHILDREN(bbr_cwnd), 1584 OID_AUTO, "red_scale", CTLFLAG_RW, 1585 &bbr_red_scale, 20000, 1586 "What RTT do we scale with?"); 1587 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1588 SYSCTL_CHILDREN(bbr_cwnd), 1589 OID_AUTO, "red_growslow", CTLFLAG_RW, 1590 &bbr_red_growth_restrict, 1, 1591 "Do we restrict cwnd growth for whats in flight?"); 1592 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1593 SYSCTL_CHILDREN(bbr_cwnd), 1594 OID_AUTO, "red_div", CTLFLAG_RW, 1595 &bbr_red_div, 2, 1596 "If we reduce whats the divisor?"); 1597 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1598 SYSCTL_CHILDREN(bbr_cwnd), 1599 OID_AUTO, "red_mul", CTLFLAG_RW, 1600 &bbr_red_mul, 1, 1601 "If we reduce whats the mulitiplier?"); 1602 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1603 SYSCTL_CHILDREN(bbr_cwnd), 1604 OID_AUTO, "target_is_unit", CTLFLAG_RW, 1605 &bbr_target_is_bbunit, 0, 1606 "Is the state target the pacing_gain or BBR_UNIT?"); 1607 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1608 SYSCTL_CHILDREN(bbr_cwnd), 1609 OID_AUTO, "drop_limit", CTLFLAG_RW, 1610 &bbr_drop_limit, 0, 1611 "Number of segments limit for drop (0=use min_cwnd w/flight)?"); 1612 1613 /* Timeout controls */ 1614 bbr_timeout = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1615 SYSCTL_CHILDREN(bbr_sysctl_root), 1616 OID_AUTO, 1617 "timeout", 1618 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1619 "Time out controls"); 1620 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1621 SYSCTL_CHILDREN(bbr_timeout), 1622 OID_AUTO, "delack", CTLFLAG_RW, 1623 &bbr_delack_time, 100000, 1624 "BBR's delayed ack time"); 1625 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1626 SYSCTL_CHILDREN(bbr_timeout), 1627 OID_AUTO, "tlp_uses", CTLFLAG_RW, 1628 &bbr_tlp_type_to_use, 3, 1629 "RTT that TLP uses in its calculations, 0=rttProp, 1=Rack_rtt, 2=pkt_rtt and 3=srtt"); 1630 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1631 SYSCTL_CHILDREN(bbr_timeout), 1632 OID_AUTO, "persmin", CTLFLAG_RW, 1633 &bbr_persist_min, 250000, 1634 "What is the minimum time in microseconds between persists"); 1635 SYSCTL_ADD_U32(&bbr_sysctl_ctx, 1636 SYSCTL_CHILDREN(bbr_timeout), 1637 OID_AUTO, "persmax", CTLFLAG_RW, 1638 &bbr_persist_max, 1000000, 1639 "What is the largest delay in microseconds between persists"); 1640 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1641 SYSCTL_CHILDREN(bbr_timeout), 1642 OID_AUTO, "tlp_minto", CTLFLAG_RW, 1643 &bbr_tlp_min, 10000, 1644 "TLP Min timeout in usecs"); 1645 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1646 SYSCTL_CHILDREN(bbr_timeout), 1647 OID_AUTO, "tlp_dack_time", CTLFLAG_RW, 1648 &bbr_delayed_ack_time, 200000, 1649 "TLP delayed ack compensation value"); 1650 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1651 SYSCTL_CHILDREN(bbr_sysctl_root), 1652 OID_AUTO, "minrto", CTLFLAG_RW, 1653 &bbr_rto_min_ms, 30, 1654 "Minimum RTO in ms"); 1655 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1656 SYSCTL_CHILDREN(bbr_timeout), 1657 OID_AUTO, "maxrto", CTLFLAG_RW, 1658 &bbr_rto_max_sec, 4, 1659 "Maximum RTO in seconds -- should be at least as large as min_rto"); 1660 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1661 SYSCTL_CHILDREN(bbr_timeout), 1662 OID_AUTO, "tlp_retry", CTLFLAG_RW, 1663 &bbr_tlp_max_resend, 2, 1664 "How many times does TLP retry a single segment or multiple with no ACK"); 1665 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1666 SYSCTL_CHILDREN(bbr_timeout), 1667 OID_AUTO, "minto", CTLFLAG_RW, 1668 &bbr_min_to, 1000, 1669 "Minimum rack timeout in useconds"); 1670 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1671 SYSCTL_CHILDREN(bbr_timeout), 1672 OID_AUTO, "pktdelay", CTLFLAG_RW, 1673 &bbr_pkt_delay, 1000, 1674 "Extra RACK time (in useconds) besides reordering thresh"); 1675 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1676 SYSCTL_CHILDREN(bbr_timeout), 1677 OID_AUTO, "incr_tmrs", CTLFLAG_RW, 1678 &bbr_incr_timers, 1, 1679 "Increase the RXT/TLP timer by the pacing time used?"); 1680 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1681 SYSCTL_CHILDREN(bbr_timeout), 1682 OID_AUTO, "rxtmark_sackpassed", CTLFLAG_RW, 1683 &bbr_marks_rxt_sack_passed, 0, 1684 "Mark sack passed on all those not ack'd when a RXT hits?"); 1685 /* Policer controls */ 1686 bbr_policer = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 1687 SYSCTL_CHILDREN(bbr_sysctl_root), 1688 OID_AUTO, 1689 "policer", 1690 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1691 "Policer controls"); 1692 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1693 SYSCTL_CHILDREN(bbr_policer), 1694 OID_AUTO, "detect_enable", CTLFLAG_RW, 1695 &bbr_policer_detection_enabled, 1, 1696 "Is policer detection enabled??"); 1697 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1698 SYSCTL_CHILDREN(bbr_policer), 1699 OID_AUTO, "min_pes", CTLFLAG_RW, 1700 &bbr_lt_intvl_min_rtts, 4, 1701 "Minimum number of PE's?"); 1702 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1703 SYSCTL_CHILDREN(bbr_policer), 1704 OID_AUTO, "bwdiff", CTLFLAG_RW, 1705 &bbr_lt_bw_diff, (4000/8), 1706 "Minimal bw diff?"); 1707 SYSCTL_ADD_U64(&bbr_sysctl_ctx, 1708 SYSCTL_CHILDREN(bbr_policer), 1709 OID_AUTO, "bwratio", CTLFLAG_RW, 1710 &bbr_lt_bw_ratio, 8, 1711 "Minimal bw diff?"); 1712 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1713 SYSCTL_CHILDREN(bbr_policer), 1714 OID_AUTO, "from_rack_rxt", CTLFLAG_RW, 1715 &bbr_policer_call_from_rack_to, 0, 1716 "Do we call the policer detection code from a rack-timeout?"); 1717 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1718 SYSCTL_CHILDREN(bbr_policer), 1719 OID_AUTO, "false_postive", CTLFLAG_RW, 1720 &bbr_lt_intvl_fp, 0, 1721 "What packet epoch do we do false-positive detection at (0=no)?"); 1722 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1723 SYSCTL_CHILDREN(bbr_policer), 1724 OID_AUTO, "loss_thresh", CTLFLAG_RW, 1725 &bbr_lt_loss_thresh, 196, 1726 "Loss threshold 196 = 19.6%?"); 1727 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1728 SYSCTL_CHILDREN(bbr_policer), 1729 OID_AUTO, "false_postive_thresh", CTLFLAG_RW, 1730 &bbr_lt_fd_thresh, 100, 1731 "What percentage is the false detection threshold (150=15.0)?"); 1732 /* All the rest */ 1733 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1734 SYSCTL_CHILDREN(bbr_sysctl_root), 1735 OID_AUTO, "cheat_rxt", CTLFLAG_RW, 1736 &bbr_use_rack_resend_cheat, 0, 1737 "Do we burst 1ms between sends on retransmissions (like rack)?"); 1738 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1739 SYSCTL_CHILDREN(bbr_sysctl_root), 1740 OID_AUTO, "error_paceout", CTLFLAG_RW, 1741 &bbr_error_base_paceout, 10000, 1742 "When we hit an error what is the min to pace out in usec's?"); 1743 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1744 SYSCTL_CHILDREN(bbr_sysctl_root), 1745 OID_AUTO, "kill_paceout", CTLFLAG_RW, 1746 &bbr_max_net_error_cnt, 10, 1747 "When we hit this many errors in a row, kill the session?"); 1748 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1749 SYSCTL_CHILDREN(bbr_sysctl_root), 1750 OID_AUTO, "data_after_close", CTLFLAG_RW, 1751 &bbr_ignore_data_after_close, 1, 1752 "Do we hold off sending a RST until all pending data is ack'd"); 1753 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1754 SYSCTL_CHILDREN(bbr_sysctl_root), 1755 OID_AUTO, "resend_use_tso", CTLFLAG_RW, 1756 &bbr_resends_use_tso, 0, 1757 "Can resends use TSO?"); 1758 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1759 SYSCTL_CHILDREN(bbr_sysctl_root), 1760 OID_AUTO, "sblklimit", CTLFLAG_RW, 1761 &bbr_sack_block_limit, 128, 1762 "When do we start ignoring small sack blocks"); 1763 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1764 SYSCTL_CHILDREN(bbr_sysctl_root), 1765 OID_AUTO, "bb_verbose", CTLFLAG_RW, 1766 &bbr_verbose_logging, 0, 1767 "Should BBR black box logging be verbose"); 1768 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1769 SYSCTL_CHILDREN(bbr_sysctl_root), 1770 OID_AUTO, "reorder_thresh", CTLFLAG_RW, 1771 &bbr_reorder_thresh, 2, 1772 "What factor for rack will be added when seeing reordering (shift right)"); 1773 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1774 SYSCTL_CHILDREN(bbr_sysctl_root), 1775 OID_AUTO, "reorder_fade", CTLFLAG_RW, 1776 &bbr_reorder_fade, 0, 1777 "Does reorder detection fade, if so how many ms (0 means never)"); 1778 SYSCTL_ADD_S32(&bbr_sysctl_ctx, 1779 SYSCTL_CHILDREN(bbr_sysctl_root), 1780 OID_AUTO, "rtt_tlp_thresh", CTLFLAG_RW, 1781 &bbr_tlp_thresh, 1, 1782 "what divisor for TLP rtt/retran will be added (1=rtt, 2=1/2 rtt etc)"); 1783 /* Stats and counters */ 1784 /* The pacing counters for hdwr/software can't be in the array */ 1785 bbr_nohdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1786 bbr_hdwr_pacing_enobuf = counter_u64_alloc(M_WAITOK); 1787 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1788 SYSCTL_CHILDREN(bbr_sysctl_root), 1789 OID_AUTO, "enob_hdwr_pacing", CTLFLAG_RD, 1790 &bbr_hdwr_pacing_enobuf, 1791 "Total number of enobufs for hardware paced flows"); 1792 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1793 SYSCTL_CHILDREN(bbr_sysctl_root), 1794 OID_AUTO, "enob_no_hdwr_pacing", CTLFLAG_RD, 1795 &bbr_nohdwr_pacing_enobuf, 1796 "Total number of enobufs for non-hardware paced flows"); 1797 1798 bbr_flows_whdwr_pacing = counter_u64_alloc(M_WAITOK); 1799 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1800 SYSCTL_CHILDREN(bbr_sysctl_root), 1801 OID_AUTO, "hdwr_pacing", CTLFLAG_RD, 1802 &bbr_flows_whdwr_pacing, 1803 "Total number of hardware paced flows"); 1804 bbr_flows_nohdwr_pacing = counter_u64_alloc(M_WAITOK); 1805 SYSCTL_ADD_COUNTER_U64(&bbr_sysctl_ctx, 1806 SYSCTL_CHILDREN(bbr_sysctl_root), 1807 OID_AUTO, "software_pacing", CTLFLAG_RD, 1808 &bbr_flows_nohdwr_pacing, 1809 "Total number of software paced flows"); 1810 COUNTER_ARRAY_ALLOC(bbr_stat_arry, BBR_STAT_SIZE, M_WAITOK); 1811 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1812 OID_AUTO, "stats", CTLFLAG_RD, 1813 bbr_stat_arry, BBR_STAT_SIZE, "BBR Stats"); 1814 COUNTER_ARRAY_ALLOC(bbr_opts_arry, BBR_OPTS_SIZE, M_WAITOK); 1815 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1816 OID_AUTO, "opts", CTLFLAG_RD, 1817 bbr_opts_arry, BBR_OPTS_SIZE, "BBR Option Stats"); 1818 COUNTER_ARRAY_ALLOC(bbr_state_lost, BBR_MAX_STAT, M_WAITOK); 1819 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1820 OID_AUTO, "lost", CTLFLAG_RD, 1821 bbr_state_lost, BBR_MAX_STAT, "Stats of when losses occur"); 1822 COUNTER_ARRAY_ALLOC(bbr_state_resend, BBR_MAX_STAT, M_WAITOK); 1823 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1824 OID_AUTO, "stateresend", CTLFLAG_RD, 1825 bbr_state_resend, BBR_MAX_STAT, "Stats of what states resend"); 1826 COUNTER_ARRAY_ALLOC(bbr_state_time, BBR_MAX_STAT, M_WAITOK); 1827 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1828 OID_AUTO, "statetime", CTLFLAG_RD, 1829 bbr_state_time, BBR_MAX_STAT, "Stats of time spent in the states"); 1830 COUNTER_ARRAY_ALLOC(bbr_out_size, TCP_MSS_ACCT_SIZE, M_WAITOK); 1831 SYSCTL_ADD_COUNTER_U64_ARRAY(&bbr_sysctl_ctx, SYSCTL_CHILDREN(bbr_sysctl_root), 1832 OID_AUTO, "outsize", CTLFLAG_RD, 1833 bbr_out_size, TCP_MSS_ACCT_SIZE, "Size of output calls"); 1834 SYSCTL_ADD_PROC(&bbr_sysctl_ctx, 1835 SYSCTL_CHILDREN(bbr_sysctl_root), 1836 OID_AUTO, "clrlost", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 1837 &bbr_clear_lost, 0, sysctl_bbr_clear_lost, "IU", "Clear lost counters"); 1838 } 1839 1840 static void 1841 bbr_counter_destroy(void) 1842 { 1843 COUNTER_ARRAY_FREE(bbr_stat_arry, BBR_STAT_SIZE); 1844 COUNTER_ARRAY_FREE(bbr_opts_arry, BBR_OPTS_SIZE); 1845 COUNTER_ARRAY_FREE(bbr_out_size, TCP_MSS_ACCT_SIZE); 1846 COUNTER_ARRAY_FREE(bbr_state_lost, BBR_MAX_STAT); 1847 COUNTER_ARRAY_FREE(bbr_state_time, BBR_MAX_STAT); 1848 COUNTER_ARRAY_FREE(bbr_state_resend, BBR_MAX_STAT); 1849 counter_u64_free(bbr_nohdwr_pacing_enobuf); 1850 counter_u64_free(bbr_hdwr_pacing_enobuf); 1851 counter_u64_free(bbr_flows_whdwr_pacing); 1852 counter_u64_free(bbr_flows_nohdwr_pacing); 1853 1854 } 1855 1856 static __inline void 1857 bbr_fill_in_logging_data(struct tcp_bbr *bbr, struct tcp_log_bbr *l, uint32_t cts) 1858 { 1859 memset(l, 0, sizeof(union tcp_log_stackspecific)); 1860 l->cur_del_rate = bbr->r_ctl.rc_bbr_cur_del_rate; 1861 l->delRate = get_filter_value(&bbr->r_ctl.rc_delrate); 1862 l->rttProp = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 1863 l->bw_inuse = bbr_get_bw(bbr); 1864 l->inflight = ctf_flight_size(bbr->rc_tp, 1865 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 1866 l->applimited = bbr->r_ctl.r_app_limited_until; 1867 l->delivered = bbr->r_ctl.rc_delivered; 1868 l->timeStamp = cts; 1869 l->lost = bbr->r_ctl.rc_lost; 1870 l->bbr_state = bbr->rc_bbr_state; 1871 l->bbr_substate = bbr_state_val(bbr); 1872 l->epoch = bbr->r_ctl.rc_rtt_epoch; 1873 l->lt_epoch = bbr->r_ctl.rc_lt_epoch; 1874 l->pacing_gain = bbr->r_ctl.rc_bbr_hptsi_gain; 1875 l->cwnd_gain = bbr->r_ctl.rc_bbr_cwnd_gain; 1876 l->inhpts = tcp_in_hpts(bbr->rc_tp); 1877 l->use_lt_bw = bbr->rc_lt_use_bw; 1878 l->pkts_out = bbr->r_ctl.rc_flight_at_input; 1879 l->pkt_epoch = bbr->r_ctl.rc_pkt_epoch; 1880 } 1881 1882 static void 1883 bbr_log_type_bw_reduce(struct tcp_bbr *bbr, int reason) 1884 { 1885 if (tcp_bblogging_on(bbr->rc_tp)) { 1886 union tcp_log_stackspecific log; 1887 1888 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1889 log.u_bbr.flex1 = 0; 1890 log.u_bbr.flex2 = 0; 1891 log.u_bbr.flex5 = 0; 1892 log.u_bbr.flex3 = 0; 1893 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_loss_rate; 1894 log.u_bbr.flex7 = reason; 1895 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_enters_probertt; 1896 log.u_bbr.flex8 = 0; 1897 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1898 &bbr->rc_inp->inp_socket->so_rcv, 1899 &bbr->rc_inp->inp_socket->so_snd, 1900 BBR_LOG_BW_RED_EV, 0, 1901 0, &log, false, &bbr->rc_tv); 1902 } 1903 } 1904 1905 static void 1906 bbr_log_type_rwnd_collapse(struct tcp_bbr *bbr, int seq, int mode, uint32_t count) 1907 { 1908 if (tcp_bblogging_on(bbr->rc_tp)) { 1909 union tcp_log_stackspecific log; 1910 1911 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1912 log.u_bbr.flex1 = seq; 1913 log.u_bbr.flex2 = count; 1914 log.u_bbr.flex8 = mode; 1915 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1916 &bbr->rc_inp->inp_socket->so_rcv, 1917 &bbr->rc_inp->inp_socket->so_snd, 1918 BBR_LOG_LOWGAIN, 0, 1919 0, &log, false, &bbr->rc_tv); 1920 } 1921 } 1922 1923 static void 1924 bbr_log_type_just_return(struct tcp_bbr *bbr, uint32_t cts, uint32_t tlen, uint8_t hpts_calling, 1925 uint8_t reason, uint32_t p_maxseg, int len) 1926 { 1927 if (tcp_bblogging_on(bbr->rc_tp)) { 1928 union tcp_log_stackspecific log; 1929 1930 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1931 log.u_bbr.flex1 = p_maxseg; 1932 log.u_bbr.flex2 = bbr->r_ctl.rc_hpts_flags; 1933 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 1934 log.u_bbr.flex4 = reason; 1935 log.u_bbr.flex5 = bbr->rc_in_persist; 1936 log.u_bbr.flex6 = bbr->r_ctl.rc_last_delay_val; 1937 log.u_bbr.flex7 = p_maxseg; 1938 log.u_bbr.flex8 = bbr->rc_in_persist; 1939 log.u_bbr.pkts_out = 0; 1940 log.u_bbr.applimited = len; 1941 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1942 &bbr->rc_inp->inp_socket->so_rcv, 1943 &bbr->rc_inp->inp_socket->so_snd, 1944 BBR_LOG_JUSTRET, 0, 1945 tlen, &log, false, &bbr->rc_tv); 1946 } 1947 } 1948 1949 static void 1950 bbr_log_type_enter_rec(struct tcp_bbr *bbr, uint32_t seq) 1951 { 1952 if (tcp_bblogging_on(bbr->rc_tp)) { 1953 union tcp_log_stackspecific log; 1954 1955 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 1956 log.u_bbr.flex1 = seq; 1957 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 1958 log.u_bbr.flex3 = bbr->r_ctl.rc_recovery_start; 1959 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 1960 &bbr->rc_inp->inp_socket->so_rcv, 1961 &bbr->rc_inp->inp_socket->so_snd, 1962 BBR_LOG_ENTREC, 0, 1963 0, &log, false, &bbr->rc_tv); 1964 } 1965 } 1966 1967 static void 1968 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) 1969 { 1970 if (tcp_bblogging_on(tp)) { 1971 union tcp_log_stackspecific log; 1972 1973 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 1974 log.u_bbr.flex1 = tso; 1975 log.u_bbr.flex2 = maxseg; 1976 log.u_bbr.flex3 = mtu; 1977 log.u_bbr.flex4 = csum_flags; 1978 TCP_LOG_EVENTP(tp, NULL, 1979 &bbr->rc_inp->inp_socket->so_rcv, 1980 &bbr->rc_inp->inp_socket->so_snd, 1981 BBR_LOG_MSGSIZE, 0, 1982 0, &log, false, &bbr->rc_tv); 1983 } 1984 } 1985 1986 static void 1987 bbr_log_flowend(struct tcp_bbr *bbr) 1988 { 1989 if (tcp_bblogging_on(bbr->rc_tp)) { 1990 union tcp_log_stackspecific log; 1991 struct sockbuf *r, *s; 1992 struct timeval tv; 1993 1994 if (bbr->rc_inp->inp_socket) { 1995 r = &bbr->rc_inp->inp_socket->so_rcv; 1996 s = &bbr->rc_inp->inp_socket->so_snd; 1997 } else { 1998 r = s = NULL; 1999 } 2000 bbr_fill_in_logging_data(bbr, &log.u_bbr, tcp_get_usecs(&tv)); 2001 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2002 r, s, 2003 TCP_LOG_FLOWEND, 0, 2004 0, &log, false, &tv); 2005 } 2006 } 2007 2008 static void 2009 bbr_log_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, 2010 uint32_t lost, uint32_t del) 2011 { 2012 if (tcp_bblogging_on(bbr->rc_tp)) { 2013 union tcp_log_stackspecific log; 2014 2015 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2016 log.u_bbr.flex1 = lost; 2017 log.u_bbr.flex2 = del; 2018 log.u_bbr.flex3 = bbr->r_ctl.rc_bbr_lastbtlbw; 2019 log.u_bbr.flex4 = bbr->r_ctl.rc_pkt_epoch_rtt; 2020 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2021 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2022 log.u_bbr.flex7 = line; 2023 log.u_bbr.flex8 = 0; 2024 log.u_bbr.inflight = bbr->r_ctl.r_measurement_count; 2025 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2026 &bbr->rc_inp->inp_socket->so_rcv, 2027 &bbr->rc_inp->inp_socket->so_snd, 2028 BBR_LOG_PKT_EPOCH, 0, 2029 0, &log, false, &bbr->rc_tv); 2030 } 2031 } 2032 2033 static void 2034 bbr_log_time_epoch(struct tcp_bbr *bbr, uint32_t cts, uint32_t line, uint32_t epoch_time) 2035 { 2036 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2037 union tcp_log_stackspecific log; 2038 2039 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2040 log.u_bbr.flex1 = bbr->r_ctl.rc_lost; 2041 log.u_bbr.flex2 = bbr->rc_inp->inp_socket->so_snd.sb_lowat; 2042 log.u_bbr.flex3 = bbr->rc_inp->inp_socket->so_snd.sb_hiwat; 2043 log.u_bbr.flex7 = line; 2044 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2045 &bbr->rc_inp->inp_socket->so_rcv, 2046 &bbr->rc_inp->inp_socket->so_snd, 2047 BBR_LOG_TIME_EPOCH, 0, 2048 0, &log, false, &bbr->rc_tv); 2049 } 2050 } 2051 2052 static void 2053 bbr_log_set_of_state_target(struct tcp_bbr *bbr, uint32_t new_tar, int line, int meth) 2054 { 2055 if (tcp_bblogging_on(bbr->rc_tp)) { 2056 union tcp_log_stackspecific log; 2057 2058 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2059 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2060 log.u_bbr.flex2 = new_tar; 2061 log.u_bbr.flex3 = line; 2062 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2063 log.u_bbr.flex5 = bbr_quanta; 2064 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_min_segs; 2065 log.u_bbr.flex7 = bbr->rc_last_options; 2066 log.u_bbr.flex8 = meth; 2067 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2068 &bbr->rc_inp->inp_socket->so_rcv, 2069 &bbr->rc_inp->inp_socket->so_snd, 2070 BBR_LOG_STATE_TARGET, 0, 2071 0, &log, false, &bbr->rc_tv); 2072 } 2073 2074 } 2075 2076 static void 2077 bbr_log_type_statechange(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2078 { 2079 if (tcp_bblogging_on(bbr->rc_tp)) { 2080 union tcp_log_stackspecific log; 2081 2082 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2083 log.u_bbr.flex1 = line; 2084 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2085 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2086 if (bbr_state_is_pkt_epoch) 2087 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 2088 else 2089 log.u_bbr.flex4 = bbr_get_rtt(bbr, BBR_RTT_PROP); 2090 log.u_bbr.flex5 = bbr->r_ctl.rc_bbr_last_startup_epoch; 2091 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2092 log.u_bbr.flex7 = (bbr->r_ctl.rc_target_at_state/1000); 2093 log.u_bbr.lt_epoch = bbr->r_ctl.rc_level_state_extra; 2094 log.u_bbr.pkts_out = bbr->r_ctl.rc_target_at_state; 2095 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2096 &bbr->rc_inp->inp_socket->so_rcv, 2097 &bbr->rc_inp->inp_socket->so_snd, 2098 BBR_LOG_STATE, 0, 2099 0, &log, false, &bbr->rc_tv); 2100 } 2101 } 2102 2103 static void 2104 bbr_log_rtt_shrinks(struct tcp_bbr *bbr, uint32_t cts, uint32_t applied, 2105 uint32_t rtt, uint32_t line, uint8_t reas, uint16_t cond) 2106 { 2107 if (tcp_bblogging_on(bbr->rc_tp)) { 2108 union tcp_log_stackspecific log; 2109 2110 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2111 log.u_bbr.flex1 = line; 2112 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2113 log.u_bbr.flex3 = bbr->r_ctl.last_in_probertt; 2114 log.u_bbr.flex4 = applied; 2115 log.u_bbr.flex5 = rtt; 2116 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2117 log.u_bbr.flex7 = cond; 2118 log.u_bbr.flex8 = reas; 2119 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2120 &bbr->rc_inp->inp_socket->so_rcv, 2121 &bbr->rc_inp->inp_socket->so_snd, 2122 BBR_LOG_RTT_SHRINKS, 0, 2123 0, &log, false, &bbr->rc_tv); 2124 } 2125 } 2126 2127 static void 2128 bbr_log_type_exit_rec(struct tcp_bbr *bbr) 2129 { 2130 if (tcp_bblogging_on(bbr->rc_tp)) { 2131 union tcp_log_stackspecific log; 2132 2133 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2134 log.u_bbr.flex1 = bbr->r_ctl.rc_recovery_start; 2135 log.u_bbr.flex2 = bbr->r_ctl.rc_cwnd_on_ent; 2136 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2137 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2138 &bbr->rc_inp->inp_socket->so_rcv, 2139 &bbr->rc_inp->inp_socket->so_snd, 2140 BBR_LOG_EXITREC, 0, 2141 0, &log, false, &bbr->rc_tv); 2142 } 2143 } 2144 2145 static void 2146 bbr_log_type_cwndupd(struct tcp_bbr *bbr, uint32_t bytes_this_ack, uint32_t chg, 2147 uint32_t prev_acked, int32_t meth, uint32_t target, uint32_t th_ack, int32_t line) 2148 { 2149 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2150 union tcp_log_stackspecific log; 2151 2152 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2153 log.u_bbr.flex1 = line; 2154 log.u_bbr.flex2 = prev_acked; 2155 log.u_bbr.flex3 = bytes_this_ack; 2156 log.u_bbr.flex4 = chg; 2157 log.u_bbr.flex5 = th_ack; 2158 log.u_bbr.flex6 = target; 2159 log.u_bbr.flex8 = meth; 2160 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2161 &bbr->rc_inp->inp_socket->so_rcv, 2162 &bbr->rc_inp->inp_socket->so_snd, 2163 BBR_LOG_CWND, 0, 2164 0, &log, false, &bbr->rc_tv); 2165 } 2166 } 2167 2168 static void 2169 bbr_log_rtt_sample(struct tcp_bbr *bbr, uint32_t rtt, uint32_t tsin) 2170 { 2171 /* 2172 * Log the rtt sample we are applying to the srtt algorithm in 2173 * useconds. 2174 */ 2175 if (tcp_bblogging_on(bbr->rc_tp)) { 2176 union tcp_log_stackspecific log; 2177 2178 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2179 log.u_bbr.flex1 = rtt; 2180 log.u_bbr.flex2 = bbr->r_ctl.rc_bbr_state_time; 2181 log.u_bbr.flex3 = bbr->r_ctl.rc_ack_hdwr_delay; 2182 log.u_bbr.flex4 = bbr->rc_tp->ts_offset; 2183 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2184 log.u_bbr.pkts_out = tcp_tv_to_mssectick(&bbr->rc_tv); 2185 log.u_bbr.flex6 = tsin; 2186 log.u_bbr.flex7 = 0; 2187 log.u_bbr.flex8 = bbr->rc_ack_was_delayed; 2188 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2189 &bbr->rc_inp->inp_socket->so_rcv, 2190 &bbr->rc_inp->inp_socket->so_snd, 2191 TCP_LOG_RTT, 0, 2192 0, &log, false, &bbr->rc_tv); 2193 } 2194 } 2195 2196 static void 2197 bbr_log_type_pesist(struct tcp_bbr *bbr, uint32_t cts, uint32_t time_in, int32_t line, uint8_t enter_exit) 2198 { 2199 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2200 union tcp_log_stackspecific log; 2201 2202 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2203 log.u_bbr.flex1 = time_in; 2204 log.u_bbr.flex2 = line; 2205 log.u_bbr.flex8 = enter_exit; 2206 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2207 &bbr->rc_inp->inp_socket->so_rcv, 2208 &bbr->rc_inp->inp_socket->so_snd, 2209 BBR_LOG_PERSIST, 0, 2210 0, &log, false, &bbr->rc_tv); 2211 } 2212 } 2213 static void 2214 bbr_log_ack_clear(struct tcp_bbr *bbr, uint32_t cts) 2215 { 2216 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2217 union tcp_log_stackspecific log; 2218 2219 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2220 log.u_bbr.flex1 = bbr->rc_tp->ts_recent_age; 2221 log.u_bbr.flex2 = bbr->r_ctl.rc_rtt_shrinks; 2222 log.u_bbr.flex3 = bbr->r_ctl.rc_probertt_int; 2223 log.u_bbr.flex4 = bbr->r_ctl.rc_went_idle_time; 2224 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2225 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2226 &bbr->rc_inp->inp_socket->so_rcv, 2227 &bbr->rc_inp->inp_socket->so_snd, 2228 BBR_LOG_ACKCLEAR, 0, 2229 0, &log, false, &bbr->rc_tv); 2230 } 2231 } 2232 2233 static void 2234 bbr_log_ack_event(struct tcp_bbr *bbr, struct tcphdr *th, struct tcpopt *to, uint32_t tlen, 2235 uint16_t nsegs, uint32_t cts, int32_t nxt_pkt, struct mbuf *m) 2236 { 2237 if (tcp_bblogging_on(bbr->rc_tp)) { 2238 union tcp_log_stackspecific log; 2239 struct timeval tv; 2240 2241 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2242 log.u_bbr.flex1 = nsegs; 2243 log.u_bbr.flex2 = bbr->r_ctl.rc_lost_bytes; 2244 if (m) { 2245 struct timespec ts; 2246 2247 log.u_bbr.flex3 = m->m_flags; 2248 if (m->m_flags & M_TSTMP) { 2249 mbuf_tstmp2timespec(m, &ts); 2250 tv.tv_sec = ts.tv_sec; 2251 tv.tv_usec = ts.tv_nsec / 1000; 2252 log.u_bbr.lt_epoch = tcp_tv_to_usectick(&tv); 2253 } else { 2254 log.u_bbr.lt_epoch = 0; 2255 } 2256 if (m->m_flags & M_TSTMP_LRO) { 2257 mbuf_tstmp2timeval(m, &tv); 2258 log.u_bbr.flex5 = tcp_tv_to_usectick(&tv); 2259 } else { 2260 /* No arrival timestamp */ 2261 log.u_bbr.flex5 = 0; 2262 } 2263 2264 log.u_bbr.pkts_out = tcp_get_usecs(&tv); 2265 } else { 2266 log.u_bbr.flex3 = 0; 2267 log.u_bbr.flex5 = 0; 2268 log.u_bbr.flex6 = 0; 2269 log.u_bbr.pkts_out = 0; 2270 } 2271 log.u_bbr.flex4 = bbr->r_ctl.rc_target_at_state; 2272 log.u_bbr.flex7 = bbr->r_wanted_output; 2273 log.u_bbr.flex8 = bbr->rc_in_persist; 2274 TCP_LOG_EVENTP(bbr->rc_tp, th, 2275 &bbr->rc_inp->inp_socket->so_rcv, 2276 &bbr->rc_inp->inp_socket->so_snd, 2277 TCP_LOG_IN, 0, 2278 tlen, &log, true, &bbr->rc_tv); 2279 } 2280 } 2281 2282 static void 2283 bbr_log_doseg_done(struct tcp_bbr *bbr, uint32_t cts, int32_t nxt_pkt, int32_t did_out) 2284 { 2285 if (tcp_bblogging_on(bbr->rc_tp)) { 2286 union tcp_log_stackspecific log; 2287 2288 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2289 log.u_bbr.flex1 = did_out; 2290 log.u_bbr.flex2 = nxt_pkt; 2291 log.u_bbr.flex3 = bbr->r_ctl.rc_last_delay_val; 2292 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2293 log.u_bbr.flex5 = bbr->r_ctl.rc_timer_exp; 2294 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_bytes; 2295 log.u_bbr.flex7 = bbr->r_wanted_output; 2296 log.u_bbr.flex8 = bbr->rc_in_persist; 2297 log.u_bbr.pkts_out = bbr->r_ctl.highest_hdwr_delay; 2298 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2299 &bbr->rc_inp->inp_socket->so_rcv, 2300 &bbr->rc_inp->inp_socket->so_snd, 2301 BBR_LOG_DOSEG_DONE, 0, 2302 0, &log, true, &bbr->rc_tv); 2303 } 2304 } 2305 2306 static void 2307 bbr_log_enobuf_jmp(struct tcp_bbr *bbr, uint32_t len, uint32_t cts, 2308 int32_t line, uint32_t o_len, uint32_t segcnt, uint32_t segsiz) 2309 { 2310 if (tcp_bblogging_on(bbr->rc_tp)) { 2311 union tcp_log_stackspecific log; 2312 2313 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2314 log.u_bbr.flex1 = line; 2315 log.u_bbr.flex2 = o_len; 2316 log.u_bbr.flex3 = segcnt; 2317 log.u_bbr.flex4 = segsiz; 2318 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2319 &bbr->rc_inp->inp_socket->so_rcv, 2320 &bbr->rc_inp->inp_socket->so_snd, 2321 BBR_LOG_ENOBUF_JMP, ENOBUFS, 2322 len, &log, true, &bbr->rc_tv); 2323 } 2324 } 2325 2326 static void 2327 bbr_log_to_processing(struct tcp_bbr *bbr, uint32_t cts, int32_t ret, int32_t timers, uint8_t hpts_calling) 2328 { 2329 if (tcp_bblogging_on(bbr->rc_tp)) { 2330 union tcp_log_stackspecific log; 2331 2332 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2333 log.u_bbr.flex1 = timers; 2334 log.u_bbr.flex2 = ret; 2335 log.u_bbr.flex3 = bbr->r_ctl.rc_timer_exp; 2336 log.u_bbr.flex4 = bbr->r_ctl.rc_hpts_flags; 2337 log.u_bbr.flex5 = cts; 2338 log.u_bbr.flex6 = bbr->r_ctl.rc_target_at_state; 2339 log.u_bbr.flex8 = hpts_calling; 2340 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2341 &bbr->rc_inp->inp_socket->so_rcv, 2342 &bbr->rc_inp->inp_socket->so_snd, 2343 BBR_LOG_TO_PROCESS, 0, 2344 0, &log, false, &bbr->rc_tv); 2345 } 2346 } 2347 2348 static void 2349 bbr_log_to_event(struct tcp_bbr *bbr, uint32_t cts, int32_t to_num) 2350 { 2351 if (tcp_bblogging_on(bbr->rc_tp)) { 2352 union tcp_log_stackspecific log; 2353 uint64_t ar; 2354 2355 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2356 log.u_bbr.flex1 = bbr->bbr_timer_src; 2357 log.u_bbr.flex2 = 0; 2358 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2359 ar = (uintptr_t)(bbr->r_ctl.rc_resend); 2360 ar >>= 32; 2361 ar &= 0x00000000ffffffff; 2362 log.u_bbr.flex4 = (uint32_t)ar; 2363 ar = (uintptr_t)bbr->r_ctl.rc_resend; 2364 ar &= 0x00000000ffffffff; 2365 log.u_bbr.flex5 = (uint32_t)ar; 2366 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2367 log.u_bbr.flex8 = to_num; 2368 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2369 &bbr->rc_inp->inp_socket->so_rcv, 2370 &bbr->rc_inp->inp_socket->so_snd, 2371 BBR_LOG_RTO, 0, 2372 0, &log, false, &bbr->rc_tv); 2373 } 2374 } 2375 2376 static void 2377 bbr_log_startup_event(struct tcp_bbr *bbr, uint32_t cts, uint32_t flex1, uint32_t flex2, uint32_t flex3, uint8_t reason) 2378 { 2379 if (tcp_bblogging_on(bbr->rc_tp)) { 2380 union tcp_log_stackspecific log; 2381 2382 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2383 log.u_bbr.flex1 = flex1; 2384 log.u_bbr.flex2 = flex2; 2385 log.u_bbr.flex3 = flex3; 2386 log.u_bbr.flex4 = 0; 2387 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2388 log.u_bbr.flex6 = bbr->r_ctl.rc_lost_at_startup; 2389 log.u_bbr.flex8 = reason; 2390 log.u_bbr.cur_del_rate = bbr->r_ctl.rc_bbr_lastbtlbw; 2391 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2392 &bbr->rc_inp->inp_socket->so_rcv, 2393 &bbr->rc_inp->inp_socket->so_snd, 2394 BBR_LOG_REDUCE, 0, 2395 0, &log, false, &bbr->rc_tv); 2396 } 2397 } 2398 2399 static void 2400 bbr_log_hpts_diag(struct tcp_bbr *bbr, uint32_t cts, struct hpts_diag *diag) 2401 { 2402 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2403 union tcp_log_stackspecific log; 2404 2405 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2406 log.u_bbr.flex1 = diag->p_nxt_slot; 2407 log.u_bbr.flex2 = diag->p_cur_slot; 2408 log.u_bbr.flex3 = diag->slot_req; 2409 log.u_bbr.flex4 = diag->inp_hptsslot; 2410 log.u_bbr.flex5 = diag->slot_remaining; 2411 log.u_bbr.flex6 = diag->need_new_to; 2412 log.u_bbr.flex7 = diag->p_hpts_active; 2413 log.u_bbr.flex8 = diag->p_on_min_sleep; 2414 /* Hijack other fields as needed */ 2415 log.u_bbr.epoch = diag->have_slept; 2416 log.u_bbr.lt_epoch = diag->yet_to_sleep; 2417 log.u_bbr.pkts_out = diag->co_ret; 2418 log.u_bbr.applimited = diag->hpts_sleep_time; 2419 log.u_bbr.delivered = diag->p_prev_slot; 2420 log.u_bbr.inflight = diag->p_runningslot; 2421 log.u_bbr.bw_inuse = diag->wheel_slot; 2422 log.u_bbr.rttProp = diag->wheel_cts; 2423 log.u_bbr.delRate = diag->maxslots; 2424 log.u_bbr.cur_del_rate = diag->p_curtick; 2425 log.u_bbr.cur_del_rate <<= 32; 2426 log.u_bbr.cur_del_rate |= diag->p_lasttick; 2427 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2428 &bbr->rc_inp->inp_socket->so_rcv, 2429 &bbr->rc_inp->inp_socket->so_snd, 2430 BBR_LOG_HPTSDIAG, 0, 2431 0, &log, false, &bbr->rc_tv); 2432 } 2433 } 2434 2435 static void 2436 bbr_log_timer_var(struct tcp_bbr *bbr, int mode, uint32_t cts, uint32_t time_since_sent, uint32_t srtt, 2437 uint32_t thresh, uint32_t to) 2438 { 2439 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2440 union tcp_log_stackspecific log; 2441 2442 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2443 log.u_bbr.flex1 = bbr->rc_tp->t_rttvar; 2444 log.u_bbr.flex2 = time_since_sent; 2445 log.u_bbr.flex3 = srtt; 2446 log.u_bbr.flex4 = thresh; 2447 log.u_bbr.flex5 = to; 2448 log.u_bbr.flex6 = bbr->rc_tp->t_srtt; 2449 log.u_bbr.flex8 = mode; 2450 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2451 &bbr->rc_inp->inp_socket->so_rcv, 2452 &bbr->rc_inp->inp_socket->so_snd, 2453 BBR_LOG_TIMERPREP, 0, 2454 0, &log, false, &bbr->rc_tv); 2455 } 2456 } 2457 2458 static void 2459 bbr_log_pacing_delay_calc(struct tcp_bbr *bbr, uint16_t gain, uint32_t len, 2460 uint32_t cts, uint32_t usecs, uint64_t bw, uint32_t override, int mod) 2461 { 2462 if (tcp_bblogging_on(bbr->rc_tp)) { 2463 union tcp_log_stackspecific log; 2464 2465 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2466 log.u_bbr.flex1 = usecs; 2467 log.u_bbr.flex2 = len; 2468 log.u_bbr.flex3 = (uint32_t)((bw >> 32) & 0x00000000ffffffff); 2469 log.u_bbr.flex4 = (uint32_t)(bw & 0x00000000ffffffff); 2470 if (override) 2471 log.u_bbr.flex5 = (1 << 2); 2472 else 2473 log.u_bbr.flex5 = 0; 2474 log.u_bbr.flex6 = override; 2475 log.u_bbr.flex7 = gain; 2476 log.u_bbr.flex8 = mod; 2477 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2478 &bbr->rc_inp->inp_socket->so_rcv, 2479 &bbr->rc_inp->inp_socket->so_snd, 2480 BBR_LOG_HPTSI_CALC, 0, 2481 len, &log, false, &bbr->rc_tv); 2482 } 2483 } 2484 2485 static void 2486 bbr_log_to_start(struct tcp_bbr *bbr, uint32_t cts, uint32_t to, int32_t slot, uint8_t which) 2487 { 2488 if (tcp_bblogging_on(bbr->rc_tp)) { 2489 union tcp_log_stackspecific log; 2490 2491 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2492 2493 log.u_bbr.flex1 = bbr->bbr_timer_src; 2494 log.u_bbr.flex2 = to; 2495 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2496 log.u_bbr.flex4 = slot; 2497 log.u_bbr.flex5 = bbr->rc_tp->t_hpts_slot; 2498 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2499 log.u_bbr.pkts_out = bbr->rc_tp->t_flags2; 2500 log.u_bbr.flex8 = which; 2501 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2502 &bbr->rc_inp->inp_socket->so_rcv, 2503 &bbr->rc_inp->inp_socket->so_snd, 2504 BBR_LOG_TIMERSTAR, 0, 2505 0, &log, false, &bbr->rc_tv); 2506 } 2507 } 2508 2509 static void 2510 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) 2511 { 2512 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2513 union tcp_log_stackspecific log; 2514 2515 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2516 log.u_bbr.flex1 = thresh; 2517 log.u_bbr.flex2 = lro; 2518 log.u_bbr.flex3 = bbr->r_ctl.rc_reorder_ts; 2519 log.u_bbr.flex4 = rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]; 2520 log.u_bbr.flex5 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2521 log.u_bbr.flex6 = srtt; 2522 log.u_bbr.flex7 = bbr->r_ctl.rc_reorder_shift; 2523 log.u_bbr.flex8 = frm; 2524 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2525 &bbr->rc_inp->inp_socket->so_rcv, 2526 &bbr->rc_inp->inp_socket->so_snd, 2527 BBR_LOG_THRESH_CALC, 0, 2528 0, &log, false, &bbr->rc_tv); 2529 } 2530 } 2531 2532 static void 2533 bbr_log_to_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts, uint8_t hpts_removed) 2534 { 2535 if (tcp_bblogging_on(bbr->rc_tp)) { 2536 union tcp_log_stackspecific log; 2537 2538 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2539 log.u_bbr.flex1 = line; 2540 log.u_bbr.flex2 = bbr->bbr_timer_src; 2541 log.u_bbr.flex3 = bbr->r_ctl.rc_hpts_flags; 2542 log.u_bbr.flex4 = bbr->rc_in_persist; 2543 log.u_bbr.flex5 = bbr->r_ctl.rc_target_at_state; 2544 log.u_bbr.flex6 = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 2545 log.u_bbr.flex8 = hpts_removed; 2546 log.u_bbr.pkts_out = bbr->rc_pacer_started; 2547 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2548 &bbr->rc_inp->inp_socket->so_rcv, 2549 &bbr->rc_inp->inp_socket->so_snd, 2550 BBR_LOG_TIMERCANC, 0, 2551 0, &log, false, &bbr->rc_tv); 2552 } 2553 } 2554 2555 static void 2556 bbr_log_tstmp_validation(struct tcp_bbr *bbr, uint64_t peer_delta, uint64_t delta) 2557 { 2558 if (tcp_bblogging_on(bbr->rc_tp)) { 2559 union tcp_log_stackspecific log; 2560 2561 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2562 log.u_bbr.flex1 = bbr->r_ctl.bbr_peer_tsratio; 2563 log.u_bbr.flex2 = (peer_delta >> 32); 2564 log.u_bbr.flex3 = (peer_delta & 0x00000000ffffffff); 2565 log.u_bbr.flex4 = (delta >> 32); 2566 log.u_bbr.flex5 = (delta & 0x00000000ffffffff); 2567 log.u_bbr.flex7 = bbr->rc_ts_clock_set; 2568 log.u_bbr.flex8 = bbr->rc_ts_cant_be_used; 2569 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2570 &bbr->rc_inp->inp_socket->so_rcv, 2571 &bbr->rc_inp->inp_socket->so_snd, 2572 BBR_LOG_TSTMP_VAL, 0, 2573 0, &log, false, &bbr->rc_tv); 2574 } 2575 } 2576 2577 static void 2578 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) 2579 { 2580 if (tcp_bblogging_on(bbr->rc_tp)) { 2581 union tcp_log_stackspecific log; 2582 2583 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2584 log.u_bbr.flex1 = tsosz; 2585 log.u_bbr.flex2 = tls; 2586 log.u_bbr.flex3 = tcp_min_hptsi_time; 2587 log.u_bbr.flex4 = bbr->r_ctl.bbr_hptsi_bytes_min; 2588 log.u_bbr.flex5 = old_val; 2589 log.u_bbr.flex6 = maxseg; 2590 log.u_bbr.flex7 = bbr->rc_no_pacing; 2591 log.u_bbr.flex7 <<= 1; 2592 log.u_bbr.flex7 |= bbr->rc_past_init_win; 2593 if (hdwr) 2594 log.u_bbr.flex8 = 0x80 | bbr->rc_use_google; 2595 else 2596 log.u_bbr.flex8 = bbr->rc_use_google; 2597 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2598 &bbr->rc_inp->inp_socket->so_rcv, 2599 &bbr->rc_inp->inp_socket->so_snd, 2600 BBR_LOG_BBRTSO, 0, 2601 0, &log, false, &bbr->rc_tv); 2602 } 2603 } 2604 2605 static void 2606 bbr_log_type_rsmclear(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, 2607 uint32_t flags, uint32_t line) 2608 { 2609 if (tcp_bblogging_on(bbr->rc_tp)) { 2610 union tcp_log_stackspecific log; 2611 2612 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2613 log.u_bbr.flex1 = line; 2614 log.u_bbr.flex2 = rsm->r_start; 2615 log.u_bbr.flex3 = rsm->r_end; 2616 log.u_bbr.flex4 = rsm->r_delivered; 2617 log.u_bbr.flex5 = rsm->r_rtr_cnt; 2618 log.u_bbr.flex6 = rsm->r_dupack; 2619 log.u_bbr.flex7 = rsm->r_tim_lastsent[0]; 2620 log.u_bbr.flex8 = rsm->r_flags; 2621 /* Hijack the pkts_out fids */ 2622 log.u_bbr.applimited = flags; 2623 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2624 &bbr->rc_inp->inp_socket->so_rcv, 2625 &bbr->rc_inp->inp_socket->so_snd, 2626 BBR_RSM_CLEARED, 0, 2627 0, &log, false, &bbr->rc_tv); 2628 } 2629 } 2630 2631 static void 2632 bbr_log_type_bbrupd(struct tcp_bbr *bbr, uint8_t flex8, uint32_t cts, 2633 uint32_t flex3, uint32_t flex2, uint32_t flex5, 2634 uint32_t flex6, uint32_t pkts_out, int flex7, 2635 uint32_t flex4, uint32_t flex1) 2636 { 2637 2638 if (tcp_bblogging_on(bbr->rc_tp)) { 2639 union tcp_log_stackspecific log; 2640 2641 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2642 log.u_bbr.flex1 = flex1; 2643 log.u_bbr.flex2 = flex2; 2644 log.u_bbr.flex3 = flex3; 2645 log.u_bbr.flex4 = flex4; 2646 log.u_bbr.flex5 = flex5; 2647 log.u_bbr.flex6 = flex6; 2648 log.u_bbr.flex7 = flex7; 2649 /* Hijack the pkts_out fids */ 2650 log.u_bbr.pkts_out = pkts_out; 2651 log.u_bbr.flex8 = flex8; 2652 if (bbr->rc_ack_was_delayed) 2653 log.u_bbr.epoch = bbr->r_ctl.rc_ack_hdwr_delay; 2654 else 2655 log.u_bbr.epoch = 0; 2656 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2657 &bbr->rc_inp->inp_socket->so_rcv, 2658 &bbr->rc_inp->inp_socket->so_snd, 2659 BBR_LOG_BBRUPD, 0, 2660 flex2, &log, false, &bbr->rc_tv); 2661 } 2662 } 2663 2664 static void 2665 bbr_log_type_ltbw(struct tcp_bbr *bbr, uint32_t cts, int32_t reason, 2666 uint32_t newbw, uint32_t obw, uint32_t diff, 2667 uint32_t tim) 2668 { 2669 if (/*bbr_verbose_logging && */tcp_bblogging_on(bbr->rc_tp)) { 2670 union tcp_log_stackspecific log; 2671 2672 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2673 log.u_bbr.flex1 = reason; 2674 log.u_bbr.flex2 = newbw; 2675 log.u_bbr.flex3 = obw; 2676 log.u_bbr.flex4 = diff; 2677 log.u_bbr.flex5 = bbr->r_ctl.rc_lt_lost; 2678 log.u_bbr.flex6 = bbr->r_ctl.rc_lt_del; 2679 log.u_bbr.flex7 = bbr->rc_lt_is_sampling; 2680 log.u_bbr.pkts_out = tim; 2681 log.u_bbr.bw_inuse = bbr->r_ctl.rc_lt_bw; 2682 if (bbr->rc_lt_use_bw == 0) 2683 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 2684 else 2685 log.u_bbr.epoch = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 2686 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2687 &bbr->rc_inp->inp_socket->so_rcv, 2688 &bbr->rc_inp->inp_socket->so_snd, 2689 BBR_LOG_BWSAMP, 0, 2690 0, &log, false, &bbr->rc_tv); 2691 } 2692 } 2693 2694 static inline void 2695 bbr_log_progress_event(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t tick, int event, int line) 2696 { 2697 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2698 union tcp_log_stackspecific log; 2699 2700 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2701 log.u_bbr.flex1 = line; 2702 log.u_bbr.flex2 = tick; 2703 log.u_bbr.flex3 = tp->t_maxunacktime; 2704 log.u_bbr.flex4 = tp->t_acktime; 2705 log.u_bbr.flex8 = event; 2706 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2707 &bbr->rc_inp->inp_socket->so_rcv, 2708 &bbr->rc_inp->inp_socket->so_snd, 2709 BBR_LOG_PROGRESS, 0, 2710 0, &log, false, &bbr->rc_tv); 2711 } 2712 } 2713 2714 static void 2715 bbr_type_log_hdwr_pacing(struct tcp_bbr *bbr, const struct ifnet *ifp, 2716 uint64_t rate, uint64_t hw_rate, int line, uint32_t cts, 2717 int error) 2718 { 2719 if (tcp_bblogging_on(bbr->rc_tp)) { 2720 union tcp_log_stackspecific log; 2721 uint64_t ifp64 = (uintptr_t)ifp; 2722 2723 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2724 log.u_bbr.flex1 = ((hw_rate >> 32) & 0x00000000ffffffff); 2725 log.u_bbr.flex2 = (hw_rate & 0x00000000ffffffff); 2726 log.u_bbr.flex3 = ((ifp64 >> 32) & 0x00000000ffffffff); 2727 log.u_bbr.flex4 = (ifp64 & 0x00000000ffffffff); 2728 log.u_bbr.bw_inuse = rate; 2729 log.u_bbr.flex5 = line; 2730 log.u_bbr.flex6 = error; 2731 log.u_bbr.flex8 = bbr->skip_gain; 2732 log.u_bbr.flex8 <<= 1; 2733 log.u_bbr.flex8 |= bbr->gain_is_limited; 2734 log.u_bbr.flex8 <<= 1; 2735 log.u_bbr.flex8 |= bbr->bbr_hdrw_pacing; 2736 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 2737 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2738 &bbr->rc_inp->inp_socket->so_rcv, 2739 &bbr->rc_inp->inp_socket->so_snd, 2740 BBR_LOG_HDWR_PACE, 0, 2741 0, &log, false, &bbr->rc_tv); 2742 } 2743 } 2744 2745 static void 2746 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) 2747 { 2748 if (tcp_bblogging_on(bbr->rc_tp)) { 2749 union tcp_log_stackspecific log; 2750 2751 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2752 log.u_bbr.flex1 = slot; 2753 log.u_bbr.flex2 = del_by; 2754 log.u_bbr.flex3 = prev_delay; 2755 log.u_bbr.flex4 = line; 2756 log.u_bbr.flex5 = bbr->r_ctl.rc_last_delay_val; 2757 log.u_bbr.flex6 = bbr->r_ctl.rc_hptsi_agg_delay; 2758 log.u_bbr.flex7 = (0x0000ffff & bbr->r_ctl.rc_hpts_flags); 2759 log.u_bbr.flex8 = bbr->rc_in_persist; 2760 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2761 &bbr->rc_inp->inp_socket->so_rcv, 2762 &bbr->rc_inp->inp_socket->so_snd, 2763 BBR_LOG_BBRSND, 0, 2764 len, &log, false, &bbr->rc_tv); 2765 } 2766 } 2767 2768 static void 2769 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) 2770 { 2771 if (tcp_bblogging_on(bbr->rc_tp)) { 2772 union tcp_log_stackspecific log; 2773 2774 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2775 log.u_bbr.flex1 = bbr->r_ctl.rc_delivered; 2776 log.u_bbr.flex2 = 0; 2777 log.u_bbr.flex3 = bbr->r_ctl.rc_lowest_rtt; 2778 log.u_bbr.flex4 = end; 2779 log.u_bbr.flex5 = seq; 2780 log.u_bbr.flex6 = t; 2781 log.u_bbr.flex7 = match; 2782 log.u_bbr.flex8 = flags; 2783 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2784 &bbr->rc_inp->inp_socket->so_rcv, 2785 &bbr->rc_inp->inp_socket->so_snd, 2786 BBR_LOG_BBRRTT, 0, 2787 0, &log, false, &bbr->rc_tv); 2788 } 2789 } 2790 2791 static void 2792 bbr_log_exit_gain(struct tcp_bbr *bbr, uint32_t cts, int32_t entry_method) 2793 { 2794 if (tcp_bblogging_on(bbr->rc_tp)) { 2795 union tcp_log_stackspecific log; 2796 2797 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 2798 log.u_bbr.flex1 = bbr->r_ctl.rc_target_at_state; 2799 log.u_bbr.flex2 = (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 2800 log.u_bbr.flex3 = bbr->r_ctl.gain_epoch; 2801 log.u_bbr.flex4 = bbr->r_ctl.rc_pace_max_segs; 2802 log.u_bbr.flex5 = bbr->r_ctl.rc_pace_min_segs; 2803 log.u_bbr.flex6 = bbr->r_ctl.rc_bbr_state_atflight; 2804 log.u_bbr.flex7 = 0; 2805 log.u_bbr.flex8 = entry_method; 2806 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2807 &bbr->rc_inp->inp_socket->so_rcv, 2808 &bbr->rc_inp->inp_socket->so_snd, 2809 BBR_LOG_EXIT_GAIN, 0, 2810 0, &log, false, &bbr->rc_tv); 2811 } 2812 } 2813 2814 static void 2815 bbr_log_settings_change(struct tcp_bbr *bbr, int settings_desired) 2816 { 2817 if (bbr_verbose_logging && tcp_bblogging_on(bbr->rc_tp)) { 2818 union tcp_log_stackspecific log; 2819 2820 bbr_fill_in_logging_data(bbr, &log.u_bbr, bbr->r_ctl.rc_rcvtime); 2821 /* R-HU */ 2822 log.u_bbr.flex1 = 0; 2823 log.u_bbr.flex2 = 0; 2824 log.u_bbr.flex3 = 0; 2825 log.u_bbr.flex4 = 0; 2826 log.u_bbr.flex7 = 0; 2827 log.u_bbr.flex8 = settings_desired; 2828 2829 TCP_LOG_EVENTP(bbr->rc_tp, NULL, 2830 &bbr->rc_inp->inp_socket->so_rcv, 2831 &bbr->rc_inp->inp_socket->so_snd, 2832 BBR_LOG_SETTINGS_CHG, 0, 2833 0, &log, false, &bbr->rc_tv); 2834 } 2835 } 2836 2837 /* 2838 * Returns the bw from the our filter. 2839 */ 2840 static inline uint64_t 2841 bbr_get_full_bw(struct tcp_bbr *bbr) 2842 { 2843 uint64_t bw; 2844 2845 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2846 2847 return (bw); 2848 } 2849 2850 static inline void 2851 bbr_set_pktepoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2852 { 2853 uint64_t calclr; 2854 uint32_t lost, del; 2855 2856 if (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_pktepoch) 2857 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lost_at_pktepoch; 2858 else 2859 lost = 0; 2860 del = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_pkt_epoch_del; 2861 if (lost == 0) { 2862 calclr = 0; 2863 } else if (del) { 2864 calclr = lost; 2865 calclr *= (uint64_t)1000; 2866 calclr /= (uint64_t)del; 2867 } else { 2868 /* Nothing delivered? 100.0% loss */ 2869 calclr = 1000; 2870 } 2871 bbr->r_ctl.rc_pkt_epoch_loss_rate = (uint32_t)calclr; 2872 if (IN_RECOVERY(bbr->rc_tp->t_flags)) 2873 bbr->r_ctl.recovery_lr += (uint32_t)calclr; 2874 bbr->r_ctl.rc_pkt_epoch++; 2875 if (bbr->rc_no_pacing && 2876 (bbr->r_ctl.rc_pkt_epoch >= bbr->no_pacing_until)) { 2877 bbr->rc_no_pacing = 0; 2878 tcp_bbr_tso_size_check(bbr, cts); 2879 } 2880 bbr->r_ctl.rc_pkt_epoch_rtt = bbr_calc_time(cts, bbr->r_ctl.rc_pkt_epoch_time); 2881 bbr->r_ctl.rc_pkt_epoch_time = cts; 2882 /* What was our loss rate */ 2883 bbr_log_pkt_epoch(bbr, cts, line, lost, del); 2884 bbr->r_ctl.rc_pkt_epoch_del = bbr->r_ctl.rc_delivered; 2885 bbr->r_ctl.rc_lost_at_pktepoch = bbr->r_ctl.rc_lost; 2886 } 2887 2888 static inline void 2889 bbr_set_epoch(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 2890 { 2891 uint32_t epoch_time; 2892 2893 /* Tick the RTT clock */ 2894 bbr->r_ctl.rc_rtt_epoch++; 2895 epoch_time = cts - bbr->r_ctl.rc_rcv_epoch_start; 2896 bbr_log_time_epoch(bbr, cts, line, epoch_time); 2897 bbr->r_ctl.rc_rcv_epoch_start = cts; 2898 } 2899 2900 static inline void 2901 bbr_isit_a_pkt_epoch(struct tcp_bbr *bbr, uint32_t cts, struct bbr_sendmap *rsm, int32_t line, int32_t cum_acked) 2902 { 2903 if (SEQ_GEQ(rsm->r_delivered, bbr->r_ctl.rc_pkt_epoch_del)) { 2904 bbr->rc_is_pkt_epoch_now = 1; 2905 } 2906 } 2907 2908 /* 2909 * Returns the bw from either the b/w filter 2910 * or from the lt_bw (if the connection is being 2911 * policed). 2912 */ 2913 static inline uint64_t 2914 __bbr_get_bw(struct tcp_bbr *bbr) 2915 { 2916 uint64_t bw, min_bw; 2917 uint64_t rtt; 2918 int gm_measure_cnt = 1; 2919 2920 /* 2921 * For startup we make, like google, a 2922 * minimum b/w. This is generated from the 2923 * IW and the rttProp. We do fall back to srtt 2924 * if for some reason (initial handshake) we don't 2925 * have a rttProp. We, in the worst case, fall back 2926 * to the configured min_bw (rc_initial_hptsi_bw). 2927 */ 2928 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 2929 /* Attempt first to use rttProp */ 2930 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2931 if (rtt && (rtt < 0xffffffff)) { 2932 measure: 2933 min_bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2934 ((uint64_t)1000000); 2935 min_bw /= rtt; 2936 if (min_bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2937 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2938 } 2939 2940 } else if (bbr->rc_tp->t_srtt != 0) { 2941 /* No rttProp, use srtt? */ 2942 rtt = bbr_get_rtt(bbr, BBR_SRTT); 2943 goto measure; 2944 } else { 2945 min_bw = bbr->r_ctl.rc_initial_hptsi_bw; 2946 } 2947 } else 2948 min_bw = 0; 2949 2950 if ((bbr->rc_past_init_win == 0) && 2951 (bbr->r_ctl.rc_delivered > bbr_initial_cwnd(bbr, bbr->rc_tp))) 2952 bbr->rc_past_init_win = 1; 2953 if ((bbr->rc_use_google) && (bbr->r_ctl.r_measurement_count >= 1)) 2954 gm_measure_cnt = 0; 2955 if (gm_measure_cnt && 2956 ((bbr->r_ctl.r_measurement_count < bbr_min_measurements_req) || 2957 (bbr->rc_past_init_win == 0))) { 2958 /* For google we use our guess rate until we get 1 measurement */ 2959 2960 use_initial_window: 2961 rtt = (uint64_t)get_filter_value_small(&bbr->r_ctl.rc_rttprop); 2962 if (rtt && (rtt < 0xffffffff)) { 2963 /* 2964 * We have an RTT measurement. Use that in 2965 * combination with our initial window to calculate 2966 * a b/w. 2967 */ 2968 bw = (uint64_t)(bbr_initial_cwnd(bbr, bbr->rc_tp)) * 2969 ((uint64_t)1000000); 2970 bw /= rtt; 2971 if (bw < bbr->r_ctl.rc_initial_hptsi_bw) { 2972 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2973 } 2974 } else { 2975 /* Drop back to the 40 and punt to a default */ 2976 bw = bbr->r_ctl.rc_initial_hptsi_bw; 2977 } 2978 if (bw < 1) 2979 /* Probably should panic */ 2980 bw = 1; 2981 if (bw > min_bw) 2982 return (bw); 2983 else 2984 return (min_bw); 2985 } 2986 if (bbr->rc_lt_use_bw) 2987 bw = bbr->r_ctl.rc_lt_bw; 2988 else if (bbr->r_recovery_bw && (bbr->rc_use_google == 0)) 2989 bw = bbr->r_ctl.red_bw; 2990 else 2991 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 2992 if (bw == 0) { 2993 /* We should not be at 0, go to the initial window then */ 2994 goto use_initial_window; 2995 } 2996 if (bw < 1) 2997 /* Probably should panic */ 2998 bw = 1; 2999 if (bw < min_bw) 3000 bw = min_bw; 3001 return (bw); 3002 } 3003 3004 static inline uint64_t 3005 bbr_get_bw(struct tcp_bbr *bbr) 3006 { 3007 uint64_t bw; 3008 3009 bw = __bbr_get_bw(bbr); 3010 return (bw); 3011 } 3012 3013 static inline void 3014 bbr_reset_lt_bw_interval(struct tcp_bbr *bbr, uint32_t cts) 3015 { 3016 bbr->r_ctl.rc_lt_epoch = bbr->r_ctl.rc_pkt_epoch; 3017 bbr->r_ctl.rc_lt_time = bbr->r_ctl.rc_del_time; 3018 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3019 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3020 } 3021 3022 static inline void 3023 bbr_reset_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts) 3024 { 3025 bbr->rc_lt_is_sampling = 0; 3026 bbr->rc_lt_use_bw = 0; 3027 bbr->r_ctl.rc_lt_bw = 0; 3028 bbr_reset_lt_bw_interval(bbr, cts); 3029 } 3030 3031 static inline void 3032 bbr_lt_bw_samp_done(struct tcp_bbr *bbr, uint64_t bw, uint32_t cts, uint32_t timin) 3033 { 3034 uint64_t diff; 3035 3036 /* Do we have a previous sample? */ 3037 if (bbr->r_ctl.rc_lt_bw) { 3038 /* Get the diff in bytes per second */ 3039 if (bbr->r_ctl.rc_lt_bw > bw) 3040 diff = bbr->r_ctl.rc_lt_bw - bw; 3041 else 3042 diff = bw - bbr->r_ctl.rc_lt_bw; 3043 if ((diff <= bbr_lt_bw_diff) || 3044 (diff <= (bbr->r_ctl.rc_lt_bw / bbr_lt_bw_ratio))) { 3045 /* Consider us policed */ 3046 uint32_t saved_bw; 3047 3048 saved_bw = (uint32_t)bbr->r_ctl.rc_lt_bw; 3049 bbr->r_ctl.rc_lt_bw = (bw + bbr->r_ctl.rc_lt_bw) / 2; /* average of two */ 3050 bbr->rc_lt_use_bw = 1; 3051 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 3052 /* 3053 * Use pkt based epoch for measuring length of 3054 * policer up 3055 */ 3056 bbr->r_ctl.rc_lt_epoch_use = bbr->r_ctl.rc_pkt_epoch; 3057 /* 3058 * reason 4 is we need to start consider being 3059 * policed 3060 */ 3061 bbr_log_type_ltbw(bbr, cts, 4, (uint32_t)bw, saved_bw, (uint32_t)diff, timin); 3062 return; 3063 } 3064 } 3065 bbr->r_ctl.rc_lt_bw = bw; 3066 bbr_reset_lt_bw_interval(bbr, cts); 3067 bbr_log_type_ltbw(bbr, cts, 5, 0, (uint32_t)bw, 0, timin); 3068 } 3069 3070 static void 3071 bbr_randomize_extra_state_time(struct tcp_bbr *bbr) 3072 { 3073 uint32_t ran, deduct; 3074 3075 ran = arc4random_uniform(bbr_rand_ot); 3076 if (ran) { 3077 deduct = bbr->r_ctl.rc_level_state_extra / ran; 3078 bbr->r_ctl.rc_level_state_extra -= deduct; 3079 } 3080 } 3081 /* 3082 * Return randomly the starting state 3083 * to use in probebw. 3084 */ 3085 static uint8_t 3086 bbr_pick_probebw_substate(struct tcp_bbr *bbr, uint32_t cts) 3087 { 3088 uint32_t ran; 3089 uint8_t ret_val; 3090 3091 /* Initialize the offset to 0 */ 3092 bbr->r_ctl.rc_exta_time_gd = 0; 3093 bbr->rc_hit_state_1 = 0; 3094 bbr->r_ctl.rc_level_state_extra = 0; 3095 ran = arc4random_uniform((BBR_SUBSTATE_COUNT-1)); 3096 /* 3097 * The math works funny here :) the return value is used to set the 3098 * substate and then the state change is called which increments by 3099 * one. So if we return 1 (DRAIN) we will increment to 2 (LEVEL1) when 3100 * we fully enter the state. Note that the (8 - 1 - ran) assures that 3101 * we return 1 - 7, so we dont return 0 and end up starting in 3102 * state 1 (DRAIN). 3103 */ 3104 ret_val = BBR_SUBSTATE_COUNT - 1 - ran; 3105 /* Set an epoch */ 3106 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) 3107 bbr_set_epoch(bbr, cts, __LINE__); 3108 3109 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 3110 return (ret_val); 3111 } 3112 3113 static void 3114 bbr_lt_bw_sampling(struct tcp_bbr *bbr, uint32_t cts, int32_t loss_detected) 3115 { 3116 uint32_t diff, d_time; 3117 uint64_t del_time, bw, lost, delivered; 3118 3119 if (bbr->r_use_policer == 0) 3120 return; 3121 if (bbr->rc_lt_use_bw) { 3122 /* We are using lt bw do we stop yet? */ 3123 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch_use; 3124 if (diff > bbr_lt_bw_max_rtts) { 3125 /* Reset it all */ 3126 reset_all: 3127 bbr_reset_lt_bw_sampling(bbr, cts); 3128 if (bbr->rc_filled_pipe) { 3129 bbr_set_epoch(bbr, cts, __LINE__); 3130 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 3131 bbr_substate_change(bbr, cts, __LINE__, 0); 3132 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 3133 bbr_log_type_statechange(bbr, cts, __LINE__); 3134 } else { 3135 /* 3136 * This should not happen really 3137 * unless we remove the startup/drain 3138 * restrictions above. 3139 */ 3140 bbr->rc_bbr_state = BBR_STATE_STARTUP; 3141 bbr_set_epoch(bbr, cts, __LINE__); 3142 bbr->r_ctl.rc_bbr_state_time = cts; 3143 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 3144 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 3145 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 3146 bbr_set_state_target(bbr, __LINE__); 3147 bbr_log_type_statechange(bbr, cts, __LINE__); 3148 } 3149 /* reason 0 is to stop using lt-bw */ 3150 bbr_log_type_ltbw(bbr, cts, 0, 0, 0, 0, 0); 3151 return; 3152 } 3153 if (bbr_lt_intvl_fp == 0) { 3154 /* Not doing false-positive detection */ 3155 return; 3156 } 3157 /* False positive detection */ 3158 if (diff == bbr_lt_intvl_fp) { 3159 /* At bbr_lt_intvl_fp we record the lost */ 3160 bbr->r_ctl.rc_lt_del = bbr->r_ctl.rc_delivered; 3161 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 3162 } else if (diff > (bbr_lt_intvl_min_rtts + bbr_lt_intvl_fp)) { 3163 /* Now is our loss rate still high? */ 3164 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3165 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3166 if ((delivered == 0) || 3167 (((lost * 1000)/delivered) < bbr_lt_fd_thresh)) { 3168 /* No still below our threshold */ 3169 bbr_log_type_ltbw(bbr, cts, 7, lost, delivered, 0, 0); 3170 } else { 3171 /* Yikes its still high, it must be a false positive */ 3172 bbr_log_type_ltbw(bbr, cts, 8, lost, delivered, 0, 0); 3173 goto reset_all; 3174 } 3175 } 3176 return; 3177 } 3178 /* 3179 * Wait for the first loss before sampling, to let the policer 3180 * exhaust its tokens and estimate the steady-state rate allowed by 3181 * the policer. Starting samples earlier includes bursts that 3182 * over-estimate the bw. 3183 */ 3184 if (bbr->rc_lt_is_sampling == 0) { 3185 /* reason 1 is to begin doing the sampling */ 3186 if (loss_detected == 0) 3187 return; 3188 bbr_reset_lt_bw_interval(bbr, cts); 3189 bbr->rc_lt_is_sampling = 1; 3190 bbr_log_type_ltbw(bbr, cts, 1, 0, 0, 0, 0); 3191 return; 3192 } 3193 /* Now how long were we delivering long term last> */ 3194 if (TSTMP_GEQ(bbr->r_ctl.rc_del_time, bbr->r_ctl.rc_lt_time)) 3195 d_time = bbr->r_ctl.rc_del_time - bbr->r_ctl.rc_lt_time; 3196 else 3197 d_time = 0; 3198 3199 /* To avoid underestimates, reset sampling if we run out of data. */ 3200 if (bbr->r_ctl.r_app_limited_until) { 3201 /* Can not measure in app-limited state */ 3202 bbr_reset_lt_bw_sampling(bbr, cts); 3203 /* reason 2 is to reset sampling due to app limits */ 3204 bbr_log_type_ltbw(bbr, cts, 2, 0, 0, 0, d_time); 3205 return; 3206 } 3207 diff = bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_lt_epoch; 3208 if (diff < bbr_lt_intvl_min_rtts) { 3209 /* 3210 * need more samples (we don't 3211 * start on a round like linux so 3212 * we need 1 more). 3213 */ 3214 /* 6 is not_enough time or no-loss */ 3215 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3216 return; 3217 } 3218 if (diff > (4 * bbr_lt_intvl_min_rtts)) { 3219 /* 3220 * For now if we wait too long, reset all sampling. We need 3221 * to do some research here, its possible that we should 3222 * base this on how much loss as occurred.. something like 3223 * if its under 10% (or some thresh) reset all otherwise 3224 * don't. Thats for phase II I guess. 3225 */ 3226 bbr_reset_lt_bw_sampling(bbr, cts); 3227 /* reason 3 is to reset sampling due too long of sampling */ 3228 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3229 return; 3230 } 3231 /* 3232 * End sampling interval when a packet is lost, so we estimate the 3233 * policer tokens were exhausted. Stopping the sampling before the 3234 * tokens are exhausted under-estimates the policed rate. 3235 */ 3236 if (loss_detected == 0) { 3237 /* 6 is not_enough time or no-loss */ 3238 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3239 return; 3240 } 3241 /* Calculate packets lost and delivered in sampling interval. */ 3242 lost = bbr->r_ctl.rc_lost - bbr->r_ctl.rc_lt_lost; 3243 delivered = bbr->r_ctl.rc_delivered - bbr->r_ctl.rc_lt_del; 3244 if ((delivered == 0) || 3245 (((lost * 1000)/delivered) < bbr_lt_loss_thresh)) { 3246 bbr_log_type_ltbw(bbr, cts, 6, lost, delivered, 0, d_time); 3247 return; 3248 } 3249 if (d_time < 1000) { 3250 /* Not enough time. wait */ 3251 /* 6 is not_enough time or no-loss */ 3252 bbr_log_type_ltbw(bbr, cts, 6, 0, 0, 0, d_time); 3253 return; 3254 } 3255 if (d_time >= (0xffffffff / USECS_IN_MSEC)) { 3256 /* Too long */ 3257 bbr_reset_lt_bw_sampling(bbr, cts); 3258 /* reason 3 is to reset sampling due too long of sampling */ 3259 bbr_log_type_ltbw(bbr, cts, 3, 0, 0, 0, d_time); 3260 return; 3261 } 3262 del_time = d_time; 3263 bw = delivered; 3264 bw *= (uint64_t)USECS_IN_SECOND; 3265 bw /= del_time; 3266 bbr_lt_bw_samp_done(bbr, bw, cts, d_time); 3267 } 3268 3269 /* 3270 * Allocate a sendmap from our zone. 3271 */ 3272 static struct bbr_sendmap * 3273 bbr_alloc(struct tcp_bbr *bbr) 3274 { 3275 struct bbr_sendmap *rsm; 3276 3277 BBR_STAT_INC(bbr_to_alloc); 3278 rsm = uma_zalloc(bbr_zone, (M_NOWAIT | M_ZERO)); 3279 if (rsm) { 3280 bbr->r_ctl.rc_num_maps_alloced++; 3281 return (rsm); 3282 } 3283 if (bbr->r_ctl.rc_free_cnt) { 3284 BBR_STAT_INC(bbr_to_alloc_emerg); 3285 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 3286 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 3287 bbr->r_ctl.rc_free_cnt--; 3288 return (rsm); 3289 } 3290 BBR_STAT_INC(bbr_to_alloc_failed); 3291 return (NULL); 3292 } 3293 3294 static struct bbr_sendmap * 3295 bbr_alloc_full_limit(struct tcp_bbr *bbr) 3296 { 3297 if ((V_tcp_map_entries_limit > 0) && 3298 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 3299 BBR_STAT_INC(bbr_alloc_limited); 3300 if (!bbr->alloc_limit_reported) { 3301 bbr->alloc_limit_reported = 1; 3302 BBR_STAT_INC(bbr_alloc_limited_conns); 3303 } 3304 return (NULL); 3305 } 3306 return (bbr_alloc(bbr)); 3307 } 3308 3309 /* wrapper to allocate a sendmap entry, subject to a specific limit */ 3310 static struct bbr_sendmap * 3311 bbr_alloc_limit(struct tcp_bbr *bbr, uint8_t limit_type) 3312 { 3313 struct bbr_sendmap *rsm; 3314 3315 if (limit_type) { 3316 /* currently there is only one limit type */ 3317 if (V_tcp_map_split_limit > 0 && 3318 bbr->r_ctl.rc_num_split_allocs >= V_tcp_map_split_limit) { 3319 BBR_STAT_INC(bbr_split_limited); 3320 if (!bbr->alloc_limit_reported) { 3321 bbr->alloc_limit_reported = 1; 3322 BBR_STAT_INC(bbr_alloc_limited_conns); 3323 } 3324 return (NULL); 3325 } 3326 } 3327 3328 /* allocate and mark in the limit type, if set */ 3329 rsm = bbr_alloc(bbr); 3330 if (rsm != NULL && limit_type) { 3331 rsm->r_limit_type = limit_type; 3332 bbr->r_ctl.rc_num_split_allocs++; 3333 } 3334 return (rsm); 3335 } 3336 3337 static void 3338 bbr_free(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 3339 { 3340 if (rsm->r_limit_type) { 3341 /* currently there is only one limit type */ 3342 bbr->r_ctl.rc_num_split_allocs--; 3343 } 3344 if (rsm->r_is_smallmap) 3345 bbr->r_ctl.rc_num_small_maps_alloced--; 3346 if (bbr->r_ctl.rc_tlp_send == rsm) 3347 bbr->r_ctl.rc_tlp_send = NULL; 3348 if (bbr->r_ctl.rc_resend == rsm) { 3349 bbr->r_ctl.rc_resend = NULL; 3350 } 3351 if (bbr->r_ctl.rc_next == rsm) 3352 bbr->r_ctl.rc_next = NULL; 3353 if (bbr->r_ctl.rc_sacklast == rsm) 3354 bbr->r_ctl.rc_sacklast = NULL; 3355 if (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 3356 memset(rsm, 0, sizeof(struct bbr_sendmap)); 3357 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 3358 rsm->r_limit_type = 0; 3359 bbr->r_ctl.rc_free_cnt++; 3360 return; 3361 } 3362 bbr->r_ctl.rc_num_maps_alloced--; 3363 uma_zfree(bbr_zone, rsm); 3364 } 3365 3366 /* 3367 * Returns the BDP. 3368 */ 3369 static uint64_t 3370 bbr_get_bw_delay_prod(uint64_t rtt, uint64_t bw) { 3371 /* 3372 * Calculate the bytes in flight needed given the bw (in bytes per 3373 * second) and the specifyed rtt in useconds. We need to put out the 3374 * returned value per RTT to match that rate. Gain will normally 3375 * raise it up from there. 3376 * 3377 * This should not overflow as long as the bandwidth is below 1 3378 * TByte per second (bw < 10**12 = 2**40) and the rtt is smaller 3379 * than 1000 seconds (rtt < 10**3 * 10**6 = 10**9 = 2**30). 3380 */ 3381 uint64_t usec_per_sec; 3382 3383 usec_per_sec = USECS_IN_SECOND; 3384 return ((rtt * bw) / usec_per_sec); 3385 } 3386 3387 /* 3388 * Return the initial cwnd. 3389 */ 3390 static uint32_t 3391 bbr_initial_cwnd(struct tcp_bbr *bbr, struct tcpcb *tp) 3392 { 3393 uint32_t i_cwnd; 3394 3395 if (bbr->rc_init_win) { 3396 i_cwnd = bbr->rc_init_win * tp->t_maxseg; 3397 } else if (V_tcp_initcwnd_segments) 3398 i_cwnd = min((V_tcp_initcwnd_segments * tp->t_maxseg), 3399 max(2 * tp->t_maxseg, 14600)); 3400 else if (V_tcp_do_rfc3390) 3401 i_cwnd = min(4 * tp->t_maxseg, 3402 max(2 * tp->t_maxseg, 4380)); 3403 else { 3404 /* Per RFC5681 Section 3.1 */ 3405 if (tp->t_maxseg > 2190) 3406 i_cwnd = 2 * tp->t_maxseg; 3407 else if (tp->t_maxseg > 1095) 3408 i_cwnd = 3 * tp->t_maxseg; 3409 else 3410 i_cwnd = 4 * tp->t_maxseg; 3411 } 3412 return (i_cwnd); 3413 } 3414 3415 /* 3416 * Given a specified gain, return the target 3417 * cwnd based on that gain. 3418 */ 3419 static uint32_t 3420 bbr_get_raw_target_cwnd(struct tcp_bbr *bbr, uint32_t gain, uint64_t bw) 3421 { 3422 uint64_t bdp, rtt; 3423 uint32_t cwnd; 3424 3425 if ((get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) || 3426 (bbr_get_full_bw(bbr) == 0)) { 3427 /* No measurements yet */ 3428 return (bbr_initial_cwnd(bbr, bbr->rc_tp)); 3429 } 3430 /* 3431 * Get bytes per RTT needed (rttProp is normally in 3432 * bbr_cwndtarget_rtt_touse) 3433 */ 3434 rtt = bbr_get_rtt(bbr, bbr_cwndtarget_rtt_touse); 3435 /* Get the bdp from the two values */ 3436 bdp = bbr_get_bw_delay_prod(rtt, bw); 3437 /* Now apply the gain */ 3438 cwnd = (uint32_t)(((bdp * ((uint64_t)gain)) + (uint64_t)(BBR_UNIT - 1)) / ((uint64_t)BBR_UNIT)); 3439 3440 return (cwnd); 3441 } 3442 3443 static uint32_t 3444 bbr_get_target_cwnd(struct tcp_bbr *bbr, uint64_t bw, uint32_t gain) 3445 { 3446 uint32_t cwnd, mss; 3447 3448 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 3449 /* Get the base cwnd with gain rounded to a mss */ 3450 cwnd = roundup(bbr_get_raw_target_cwnd(bbr, bw, gain), mss); 3451 /* 3452 * Add in N (2 default since we do not have a 3453 * fq layer to trap packets in) quanta's per the I-D 3454 * section 4.2.3.2 quanta adjust. 3455 */ 3456 cwnd += (bbr_quanta * bbr->r_ctl.rc_pace_max_segs); 3457 if (bbr->rc_use_google) { 3458 if((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3459 (bbr_state_val(bbr) == BBR_SUB_GAIN)) { 3460 /* 3461 * The linux implementation adds 3462 * an extra 2 x mss in gain cycle which 3463 * is documented no-where except in the code. 3464 * so we add more for Neal undocumented feature 3465 */ 3466 cwnd += 2 * mss; 3467 } 3468 if ((cwnd / mss) & 0x1) { 3469 /* Round up for odd num mss */ 3470 cwnd += mss; 3471 } 3472 } 3473 /* Are we below the min cwnd? */ 3474 if (cwnd < get_min_cwnd(bbr)) 3475 return (get_min_cwnd(bbr)); 3476 return (cwnd); 3477 } 3478 3479 static uint16_t 3480 bbr_gain_adjust(struct tcp_bbr *bbr, uint16_t gain) 3481 { 3482 if (gain < 1) 3483 gain = 1; 3484 return (gain); 3485 } 3486 3487 static uint32_t 3488 bbr_get_header_oh(struct tcp_bbr *bbr) 3489 { 3490 int seg_oh; 3491 3492 seg_oh = 0; 3493 if (bbr->r_ctl.rc_inc_tcp_oh) { 3494 /* Do we include TCP overhead? */ 3495 seg_oh = (bbr->rc_last_options + sizeof(struct tcphdr)); 3496 } 3497 if (bbr->r_ctl.rc_inc_ip_oh) { 3498 /* Do we include IP overhead? */ 3499 #ifdef INET6 3500 if (bbr->r_is_v6) { 3501 seg_oh += sizeof(struct ip6_hdr); 3502 } else 3503 #endif 3504 { 3505 3506 #ifdef INET 3507 seg_oh += sizeof(struct ip); 3508 #endif 3509 } 3510 } 3511 if (bbr->r_ctl.rc_inc_enet_oh) { 3512 /* Do we include the ethernet overhead? */ 3513 seg_oh += sizeof(struct ether_header); 3514 } 3515 return(seg_oh); 3516 } 3517 3518 static uint32_t 3519 bbr_get_pacing_length(struct tcp_bbr *bbr, uint16_t gain, uint32_t useconds_time, uint64_t bw) 3520 { 3521 uint64_t divor, res, tim; 3522 3523 if (useconds_time == 0) 3524 return (0); 3525 gain = bbr_gain_adjust(bbr, gain); 3526 divor = (uint64_t)USECS_IN_SECOND * (uint64_t)BBR_UNIT; 3527 tim = useconds_time; 3528 res = (tim * bw * gain) / divor; 3529 if (res == 0) 3530 res = 1; 3531 return ((uint32_t)res); 3532 } 3533 3534 /* 3535 * Given a gain and a length return the delay in useconds that 3536 * should be used to evenly space out packets 3537 * on the connection (based on the gain factor). 3538 */ 3539 static uint32_t 3540 bbr_get_pacing_delay(struct tcp_bbr *bbr, uint16_t gain, int32_t len, uint32_t cts, int nolog) 3541 { 3542 uint64_t bw, lentim, res; 3543 uint32_t usecs, srtt, over = 0; 3544 uint32_t seg_oh, num_segs, maxseg; 3545 3546 if (len == 0) 3547 return (0); 3548 3549 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 3550 num_segs = (len + maxseg - 1) / maxseg; 3551 if (bbr->rc_use_google == 0) { 3552 seg_oh = bbr_get_header_oh(bbr); 3553 len += (num_segs * seg_oh); 3554 } 3555 gain = bbr_gain_adjust(bbr, gain); 3556 bw = bbr_get_bw(bbr); 3557 if (bbr->rc_use_google) { 3558 uint64_t cbw; 3559 3560 /* 3561 * Reduce the b/w by the google discount 3562 * factor 10 = 1%. 3563 */ 3564 cbw = bw * (uint64_t)(1000 - bbr->r_ctl.bbr_google_discount); 3565 cbw /= (uint64_t)1000; 3566 /* We don't apply a discount if it results in 0 */ 3567 if (cbw > 0) 3568 bw = cbw; 3569 } 3570 lentim = ((uint64_t)len * 3571 (uint64_t)USECS_IN_SECOND * 3572 (uint64_t)BBR_UNIT); 3573 res = lentim / ((uint64_t)gain * bw); 3574 if (res == 0) 3575 res = 1; 3576 usecs = (uint32_t)res; 3577 srtt = bbr_get_rtt(bbr, BBR_SRTT); 3578 if (bbr_hptsi_max_mul && bbr_hptsi_max_div && 3579 (bbr->rc_use_google == 0) && 3580 (usecs > ((srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div))) { 3581 /* 3582 * We cannot let the delay be more than 1/2 the srtt time. 3583 * Otherwise we cannot pace out or send properly. 3584 */ 3585 over = usecs = (srtt * bbr_hptsi_max_mul) / bbr_hptsi_max_div; 3586 BBR_STAT_INC(bbr_hpts_min_time); 3587 } 3588 if (!nolog) 3589 bbr_log_pacing_delay_calc(bbr, gain, len, cts, usecs, bw, over, 1); 3590 return (usecs); 3591 } 3592 3593 static void 3594 bbr_ack_received(struct tcpcb *tp, struct tcp_bbr *bbr, struct tcphdr *th, uint32_t bytes_this_ack, 3595 uint32_t sack_changed, uint32_t prev_acked, int32_t line, uint32_t losses) 3596 { 3597 uint64_t bw; 3598 uint32_t cwnd, target_cwnd, saved_bytes, maxseg; 3599 int32_t meth; 3600 3601 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3602 3603 #ifdef STATS 3604 if ((tp->t_flags & TF_GPUTINPROG) && 3605 SEQ_GEQ(th->th_ack, tp->gput_ack)) { 3606 /* 3607 * Strech acks and compressed acks will cause this to 3608 * oscillate but we are doing it the same way as the main 3609 * stack so it will be compariable (though possibly not 3610 * ideal). 3611 */ 3612 int32_t cgput; 3613 int64_t gput, time_stamp; 3614 3615 gput = (int64_t) (th->th_ack - tp->gput_seq) * 8; 3616 time_stamp = max(1, ((bbr->r_ctl.rc_rcvtime - tp->gput_ts) / 1000)); 3617 cgput = gput / time_stamp; 3618 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 3619 cgput); 3620 if (tp->t_stats_gput_prev > 0) 3621 stats_voi_update_abs_s32(tp->t_stats, 3622 VOI_TCP_GPUT_ND, 3623 ((gput - tp->t_stats_gput_prev) * 100) / 3624 tp->t_stats_gput_prev); 3625 tp->t_flags &= ~TF_GPUTINPROG; 3626 tp->t_stats_gput_prev = cgput; 3627 } 3628 #endif 3629 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3630 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 3631 /* We don't change anything in probe-rtt */ 3632 return; 3633 } 3634 maxseg = tp->t_maxseg - bbr->rc_last_options; 3635 saved_bytes = bytes_this_ack; 3636 bytes_this_ack += sack_changed; 3637 if (bytes_this_ack > prev_acked) { 3638 bytes_this_ack -= prev_acked; 3639 /* 3640 * A byte ack'd gives us a full mss 3641 * to be like linux i.e. they count packets. 3642 */ 3643 if ((bytes_this_ack < maxseg) && bbr->rc_use_google) 3644 bytes_this_ack = maxseg; 3645 } else { 3646 /* Unlikely */ 3647 bytes_this_ack = 0; 3648 } 3649 cwnd = tp->snd_cwnd; 3650 bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3651 if (bw) 3652 target_cwnd = bbr_get_target_cwnd(bbr, 3653 bw, 3654 (uint32_t)bbr->r_ctl.rc_bbr_cwnd_gain); 3655 else 3656 target_cwnd = bbr_initial_cwnd(bbr, bbr->rc_tp); 3657 if (IN_RECOVERY(tp->t_flags) && 3658 (bbr->bbr_prev_in_rec == 0)) { 3659 /* 3660 * We are entering recovery and 3661 * thus packet conservation. 3662 */ 3663 bbr->pkt_conservation = 1; 3664 bbr->r_ctl.rc_recovery_start = bbr->r_ctl.rc_rcvtime; 3665 cwnd = ctf_flight_size(tp, 3666 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3667 bytes_this_ack; 3668 } 3669 if (IN_RECOVERY(tp->t_flags)) { 3670 uint32_t flight; 3671 3672 bbr->bbr_prev_in_rec = 1; 3673 if (cwnd > losses) { 3674 cwnd -= losses; 3675 if (cwnd < maxseg) 3676 cwnd = maxseg; 3677 } else 3678 cwnd = maxseg; 3679 flight = ctf_flight_size(tp, 3680 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3681 bbr_log_type_cwndupd(bbr, flight, 0, 3682 losses, 10, 0, 0, line); 3683 if (bbr->pkt_conservation) { 3684 uint32_t time_in; 3685 3686 if (TSTMP_GEQ(bbr->r_ctl.rc_rcvtime, bbr->r_ctl.rc_recovery_start)) 3687 time_in = bbr->r_ctl.rc_rcvtime - bbr->r_ctl.rc_recovery_start; 3688 else 3689 time_in = 0; 3690 3691 if (time_in >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 3692 /* Clear packet conservation after an rttProp */ 3693 bbr->pkt_conservation = 0; 3694 } else { 3695 if ((flight + bytes_this_ack) > cwnd) 3696 cwnd = flight + bytes_this_ack; 3697 if (cwnd < get_min_cwnd(bbr)) 3698 cwnd = get_min_cwnd(bbr); 3699 tp->snd_cwnd = cwnd; 3700 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, 3701 prev_acked, 1, target_cwnd, th->th_ack, line); 3702 return; 3703 } 3704 } 3705 } else 3706 bbr->bbr_prev_in_rec = 0; 3707 if ((bbr->rc_use_google == 0) && bbr->r_ctl.restrict_growth) { 3708 bbr->r_ctl.restrict_growth--; 3709 if (bytes_this_ack > maxseg) 3710 bytes_this_ack = maxseg; 3711 } 3712 if (bbr->rc_filled_pipe) { 3713 /* 3714 * Here we have exited startup and filled the pipe. We will 3715 * thus allow the cwnd to shrink to the target. We hit here 3716 * mostly. 3717 */ 3718 uint32_t s_cwnd; 3719 3720 meth = 2; 3721 s_cwnd = min((cwnd + bytes_this_ack), target_cwnd); 3722 if (s_cwnd > cwnd) 3723 cwnd = s_cwnd; 3724 else if (bbr_cwnd_may_shrink || bbr->rc_use_google || bbr->rc_no_pacing) 3725 cwnd = s_cwnd; 3726 } else { 3727 /* 3728 * Here we are still in startup, we increase cwnd by what 3729 * has been acked. 3730 */ 3731 if ((cwnd < target_cwnd) || 3732 (bbr->rc_past_init_win == 0)) { 3733 meth = 3; 3734 cwnd += bytes_this_ack; 3735 } else { 3736 /* 3737 * Method 4 means we are at target so no gain in 3738 * startup and past the initial window. 3739 */ 3740 meth = 4; 3741 } 3742 } 3743 tp->snd_cwnd = max(cwnd, get_min_cwnd(bbr)); 3744 bbr_log_type_cwndupd(bbr, saved_bytes, sack_changed, prev_acked, meth, target_cwnd, th->th_ack, line); 3745 } 3746 3747 static void 3748 tcp_bbr_partialack(struct tcpcb *tp) 3749 { 3750 struct tcp_bbr *bbr; 3751 3752 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3753 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3754 if (ctf_flight_size(tp, 3755 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 3756 tp->snd_cwnd) { 3757 bbr->r_wanted_output = 1; 3758 } 3759 } 3760 3761 static void 3762 bbr_post_recovery(struct tcpcb *tp) 3763 { 3764 struct tcp_bbr *bbr; 3765 uint32_t flight; 3766 3767 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3768 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3769 /* 3770 * Here we just exit recovery. 3771 */ 3772 EXIT_RECOVERY(tp->t_flags); 3773 /* Lock in our b/w reduction for the specified number of pkt-epochs */ 3774 bbr->r_recovery_bw = 0; 3775 tp->snd_recover = tp->snd_una; 3776 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3777 bbr->pkt_conservation = 0; 3778 if (bbr->rc_use_google == 0) { 3779 /* 3780 * For non-google mode lets 3781 * go ahead and make sure we clear 3782 * the recovery state so if we 3783 * bounce back in to recovery we 3784 * will do PC. 3785 */ 3786 bbr->bbr_prev_in_rec = 0; 3787 } 3788 bbr_log_type_exit_rec(bbr); 3789 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3790 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3791 bbr_log_type_cwndupd(bbr, 0, 0, 0, 15, 0, 0, __LINE__); 3792 } else { 3793 /* For probe-rtt case lets fix up its saved_cwnd */ 3794 if (bbr->r_ctl.rc_saved_cwnd < bbr->r_ctl.rc_cwnd_on_ent) { 3795 bbr->r_ctl.rc_saved_cwnd = bbr->r_ctl.rc_cwnd_on_ent; 3796 bbr_log_type_cwndupd(bbr, 0, 0, 0, 16, 0, 0, __LINE__); 3797 } 3798 } 3799 flight = ctf_flight_size(tp, 3800 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 3801 if ((bbr->rc_use_google == 0) && 3802 bbr_do_red) { 3803 uint64_t val, lr2use; 3804 uint32_t maxseg, newcwnd, acks_inflight, ratio, cwnd; 3805 uint32_t *cwnd_p; 3806 3807 if (bbr_get_rtt(bbr, BBR_SRTT)) { 3808 val = ((uint64_t)bbr_get_rtt(bbr, BBR_RTT_PROP) * (uint64_t)1000); 3809 val /= bbr_get_rtt(bbr, BBR_SRTT); 3810 ratio = (uint32_t)val; 3811 } else 3812 ratio = 1000; 3813 3814 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, 3815 bbr->r_ctl.recovery_lr, 21, 3816 ratio, 3817 bbr->r_ctl.rc_red_cwnd_pe, 3818 __LINE__); 3819 if ((ratio < bbr_do_red) || (bbr_do_red == 0)) 3820 goto done; 3821 if (((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 3822 bbr_prtt_slam_cwnd) || 3823 (bbr_sub_drain_slam_cwnd && 3824 (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 3825 bbr->rc_hit_state_1 && 3826 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) || 3827 ((bbr->rc_bbr_state == BBR_STATE_DRAIN) && 3828 bbr_slam_cwnd_in_main_drain)) { 3829 /* 3830 * Here we must poke at the saved cwnd 3831 * as well as the cwnd. 3832 */ 3833 cwnd = bbr->r_ctl.rc_saved_cwnd; 3834 cwnd_p = &bbr->r_ctl.rc_saved_cwnd; 3835 } else { 3836 cwnd = tp->snd_cwnd; 3837 cwnd_p = &tp->snd_cwnd; 3838 } 3839 maxseg = tp->t_maxseg - bbr->rc_last_options; 3840 /* Add the overall lr with the recovery lr */ 3841 if (bbr->r_ctl.rc_lost == 0) 3842 lr2use = 0; 3843 else if (bbr->r_ctl.rc_delivered == 0) 3844 lr2use = 1000; 3845 else { 3846 lr2use = bbr->r_ctl.rc_lost * 1000; 3847 lr2use /= bbr->r_ctl.rc_delivered; 3848 } 3849 lr2use += bbr->r_ctl.recovery_lr; 3850 acks_inflight = (flight / (maxseg * 2)); 3851 if (bbr_red_scale) { 3852 lr2use *= bbr_get_rtt(bbr, BBR_SRTT); 3853 lr2use /= bbr_red_scale; 3854 if ((bbr_red_growth_restrict) && 3855 ((bbr_get_rtt(bbr, BBR_SRTT)/bbr_red_scale) > 1)) 3856 bbr->r_ctl.restrict_growth += acks_inflight; 3857 } 3858 if (lr2use) { 3859 val = (uint64_t)cwnd * lr2use; 3860 val /= 1000; 3861 if (cwnd > val) 3862 newcwnd = roundup((cwnd - val), maxseg); 3863 else 3864 newcwnd = maxseg; 3865 } else { 3866 val = (uint64_t)cwnd * (uint64_t)bbr_red_mul; 3867 val /= (uint64_t)bbr_red_div; 3868 newcwnd = roundup((uint32_t)val, maxseg); 3869 } 3870 /* with standard delayed acks how many acks can I expect? */ 3871 if (bbr_drop_limit == 0) { 3872 /* 3873 * Anticpate how much we will 3874 * raise the cwnd based on the acks. 3875 */ 3876 if ((newcwnd + (acks_inflight * maxseg)) < get_min_cwnd(bbr)) { 3877 /* We do enforce the min (with the acks) */ 3878 newcwnd = (get_min_cwnd(bbr) - acks_inflight); 3879 } 3880 } else { 3881 /* 3882 * A strict drop limit of N is inplace 3883 */ 3884 if (newcwnd < (bbr_drop_limit * maxseg)) { 3885 newcwnd = bbr_drop_limit * maxseg; 3886 } 3887 } 3888 /* For the next N acks do we restrict the growth */ 3889 *cwnd_p = newcwnd; 3890 if (tp->snd_cwnd > newcwnd) 3891 tp->snd_cwnd = newcwnd; 3892 bbr_log_type_cwndupd(bbr, bbr_red_mul, bbr_red_div, val, 22, 3893 (uint32_t)lr2use, 3894 bbr_get_rtt(bbr, BBR_SRTT), __LINE__); 3895 bbr->r_ctl.rc_red_cwnd_pe = bbr->r_ctl.rc_pkt_epoch; 3896 } 3897 done: 3898 bbr->r_ctl.recovery_lr = 0; 3899 if (flight <= tp->snd_cwnd) { 3900 bbr->r_wanted_output = 1; 3901 } 3902 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3903 } 3904 3905 static void 3906 bbr_setup_red_bw(struct tcp_bbr *bbr, uint32_t cts) 3907 { 3908 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 3909 /* Limit the drop in b/w to 1/2 our current filter. */ 3910 if (bbr->r_ctl.red_bw > bbr->r_ctl.rc_bbr_cur_del_rate) 3911 bbr->r_ctl.red_bw = bbr->r_ctl.rc_bbr_cur_del_rate; 3912 if (bbr->r_ctl.red_bw < (get_filter_value(&bbr->r_ctl.rc_delrate) / 2)) 3913 bbr->r_ctl.red_bw = get_filter_value(&bbr->r_ctl.rc_delrate) / 2; 3914 tcp_bbr_tso_size_check(bbr, cts); 3915 } 3916 3917 static void 3918 bbr_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type, struct bbr_sendmap *rsm) 3919 { 3920 struct tcp_bbr *bbr; 3921 3922 INP_WLOCK_ASSERT(tptoinpcb(tp)); 3923 #ifdef STATS 3924 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 3925 #endif 3926 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 3927 switch (type) { 3928 case CC_NDUPACK: 3929 if (!IN_RECOVERY(tp->t_flags)) { 3930 tp->snd_recover = tp->snd_max; 3931 /* Start a new epoch */ 3932 bbr_set_pktepoch(bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 3933 if (bbr->rc_lt_is_sampling || bbr->rc_lt_use_bw) { 3934 /* 3935 * Move forward the lt epoch 3936 * so it won't count the truncated 3937 * epoch. 3938 */ 3939 bbr->r_ctl.rc_lt_epoch++; 3940 } 3941 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 3942 /* 3943 * Just like the policer detection code 3944 * if we are in startup we must push 3945 * forward the last startup epoch 3946 * to hide the truncated PE. 3947 */ 3948 bbr->r_ctl.rc_bbr_last_startup_epoch++; 3949 } 3950 bbr->r_ctl.rc_cwnd_on_ent = tp->snd_cwnd; 3951 ENTER_RECOVERY(tp->t_flags); 3952 bbr->rc_tlp_rtx_out = 0; 3953 bbr->r_ctl.recovery_lr = bbr->r_ctl.rc_pkt_epoch_loss_rate; 3954 tcp_bbr_tso_size_check(bbr, bbr->r_ctl.rc_rcvtime); 3955 if (tcp_in_hpts(bbr->rc_tp) && 3956 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK) == 0)) { 3957 /* 3958 * When we enter recovery, we need to restart 3959 * any timers. This may mean we gain an agg 3960 * early, which will be made up for at the last 3961 * rxt out. 3962 */ 3963 bbr->rc_timer_first = 1; 3964 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 3965 } 3966 /* 3967 * Calculate a new cwnd based on to the current 3968 * delivery rate with no gain. We get the bdp 3969 * without gaining it up like we normally would and 3970 * we use the last cur_del_rate. 3971 */ 3972 if ((bbr->rc_use_google == 0) && 3973 (bbr->r_ctl.bbr_rttprobe_gain_val || 3974 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT))) { 3975 tp->snd_cwnd = ctf_flight_size(tp, 3976 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 3977 (tp->t_maxseg - bbr->rc_last_options); 3978 if (tp->snd_cwnd < get_min_cwnd(bbr)) { 3979 /* We always gate to min cwnd */ 3980 tp->snd_cwnd = get_min_cwnd(bbr); 3981 } 3982 bbr_log_type_cwndupd(bbr, 0, 0, 0, 14, 0, 0, __LINE__); 3983 } 3984 bbr_log_type_enter_rec(bbr, rsm->r_start); 3985 } 3986 break; 3987 case CC_RTO_ERR: 3988 KMOD_TCPSTAT_INC(tcps_sndrexmitbad); 3989 /* RTO was unnecessary, so reset everything. */ 3990 bbr_reset_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime); 3991 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 3992 tp->snd_cwnd = tp->snd_cwnd_prev; 3993 tp->snd_ssthresh = tp->snd_ssthresh_prev; 3994 tp->snd_recover = tp->snd_recover_prev; 3995 tp->snd_cwnd = max(tp->snd_cwnd, bbr->r_ctl.rc_cwnd_on_ent); 3996 bbr_log_type_cwndupd(bbr, 0, 0, 0, 13, 0, 0, __LINE__); 3997 } 3998 tp->t_badrxtwin = 0; 3999 break; 4000 } 4001 } 4002 4003 /* 4004 * Indicate whether this ack should be delayed. We can delay the ack if 4005 * following conditions are met: 4006 * - There is no delayed ack timer in progress. 4007 * - Our last ack wasn't a 0-sized window. We never want to delay 4008 * the ack that opens up a 0-sized window. 4009 * - LRO wasn't used for this segment. We make sure by checking that the 4010 * segment size is not larger than the MSS. 4011 * - Delayed acks are enabled or this is a half-synchronized T/TCP 4012 * connection. 4013 * - The data being acked is less than a full segment (a stretch ack 4014 * of more than a segment we should ack. 4015 * - nsegs is 1 (if its more than that we received more than 1 ack). 4016 */ 4017 #define DELAY_ACK(tp, bbr, nsegs) \ 4018 (((tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4019 ((tp->t_flags & TF_DELACK) == 0) && \ 4020 ((bbr->bbr_segs_rcvd + nsegs) < tp->t_delayed_ack) && \ 4021 (tp->t_delayed_ack || (tp->t_flags & TF_NEEDSYN))) 4022 4023 /* 4024 * Return the lowest RSM in the map of 4025 * packets still in flight that is not acked. 4026 * This should normally find on the first one 4027 * since we remove packets from the send 4028 * map after they are marked ACKED. 4029 */ 4030 static struct bbr_sendmap * 4031 bbr_find_lowest_rsm(struct tcp_bbr *bbr) 4032 { 4033 struct bbr_sendmap *rsm; 4034 4035 /* 4036 * Walk the time-order transmitted list looking for an rsm that is 4037 * not acked. This will be the one that was sent the longest time 4038 * ago that is still outstanding. 4039 */ 4040 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_tmap, r_tnext) { 4041 if (rsm->r_flags & BBR_ACKED) { 4042 continue; 4043 } 4044 goto finish; 4045 } 4046 finish: 4047 return (rsm); 4048 } 4049 4050 static struct bbr_sendmap * 4051 bbr_find_high_nonack(struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 4052 { 4053 struct bbr_sendmap *prsm; 4054 4055 /* 4056 * Walk the sequence order list backward until we hit and arrive at 4057 * the highest seq not acked. In theory when this is called it 4058 * should be the last segment (which it was not). 4059 */ 4060 prsm = rsm; 4061 TAILQ_FOREACH_REVERSE_FROM(prsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4062 if (prsm->r_flags & (BBR_ACKED | BBR_HAS_FIN)) { 4063 continue; 4064 } 4065 return (prsm); 4066 } 4067 return (NULL); 4068 } 4069 4070 /* 4071 * Returns to the caller the number of microseconds that 4072 * the packet can be outstanding before we think we 4073 * should have had an ack returned. 4074 */ 4075 static uint32_t 4076 bbr_calc_thresh_rack(struct tcp_bbr *bbr, uint32_t srtt, uint32_t cts, struct bbr_sendmap *rsm) 4077 { 4078 /* 4079 * lro is the flag we use to determine if we have seen reordering. 4080 * If it gets set we have seen reordering. The reorder logic either 4081 * works in one of two ways: 4082 * 4083 * If reorder-fade is configured, then we track the last time we saw 4084 * re-ordering occur. If we reach the point where enough time as 4085 * passed we no longer consider reordering has occuring. 4086 * 4087 * Or if reorder-face is 0, then once we see reordering we consider 4088 * the connection to alway be subject to reordering and just set lro 4089 * to 1. 4090 * 4091 * In the end if lro is non-zero we add the extra time for 4092 * reordering in. 4093 */ 4094 int32_t lro; 4095 uint32_t thresh, t_rxtcur; 4096 4097 if (srtt == 0) 4098 srtt = 1; 4099 if (bbr->r_ctl.rc_reorder_ts) { 4100 if (bbr->r_ctl.rc_reorder_fade) { 4101 if (SEQ_GEQ(cts, bbr->r_ctl.rc_reorder_ts)) { 4102 lro = cts - bbr->r_ctl.rc_reorder_ts; 4103 if (lro == 0) { 4104 /* 4105 * No time as passed since the last 4106 * reorder, mark it as reordering. 4107 */ 4108 lro = 1; 4109 } 4110 } else { 4111 /* Negative time? */ 4112 lro = 0; 4113 } 4114 if (lro > bbr->r_ctl.rc_reorder_fade) { 4115 /* Turn off reordering seen too */ 4116 bbr->r_ctl.rc_reorder_ts = 0; 4117 lro = 0; 4118 } 4119 } else { 4120 /* Reodering does not fade */ 4121 lro = 1; 4122 } 4123 } else { 4124 lro = 0; 4125 } 4126 thresh = srtt + bbr->r_ctl.rc_pkt_delay; 4127 if (lro) { 4128 /* It must be set, if not you get 1/4 rtt */ 4129 if (bbr->r_ctl.rc_reorder_shift) 4130 thresh += (srtt >> bbr->r_ctl.rc_reorder_shift); 4131 else 4132 thresh += (srtt >> 2); 4133 } else { 4134 thresh += 1000; 4135 } 4136 /* We don't let the rack timeout be above a RTO */ 4137 if ((bbr->rc_tp)->t_srtt == 0) 4138 t_rxtcur = BBR_INITIAL_RTO; 4139 else 4140 t_rxtcur = TICKS_2_USEC(bbr->rc_tp->t_rxtcur); 4141 if (thresh > t_rxtcur) { 4142 thresh = t_rxtcur; 4143 } 4144 /* And we don't want it above the RTO max either */ 4145 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4146 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4147 } 4148 bbr_log_thresh_choice(bbr, cts, thresh, lro, srtt, rsm, BBR_TO_FRM_RACK); 4149 return (thresh); 4150 } 4151 4152 /* 4153 * Return to the caller the amount of time in mico-seconds 4154 * that should be used for the TLP timer from the last 4155 * send time of this packet. 4156 */ 4157 static uint32_t 4158 bbr_calc_thresh_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, 4159 struct bbr_sendmap *rsm, uint32_t srtt, 4160 uint32_t cts) 4161 { 4162 uint32_t thresh, len, maxseg, t_rxtcur; 4163 struct bbr_sendmap *prsm; 4164 4165 if (srtt == 0) 4166 srtt = 1; 4167 if (bbr->rc_tlp_threshold) 4168 thresh = srtt + (srtt / bbr->rc_tlp_threshold); 4169 else 4170 thresh = (srtt * 2); 4171 maxseg = tp->t_maxseg - bbr->rc_last_options; 4172 /* Get the previous sent packet, if any */ 4173 len = rsm->r_end - rsm->r_start; 4174 4175 /* 2.1 behavior */ 4176 prsm = TAILQ_PREV(rsm, bbr_head, r_tnext); 4177 if (prsm && (len <= maxseg)) { 4178 /* 4179 * Two packets outstanding, thresh should be (2*srtt) + 4180 * possible inter-packet delay (if any). 4181 */ 4182 uint32_t inter_gap = 0; 4183 int idx, nidx; 4184 4185 idx = rsm->r_rtr_cnt - 1; 4186 nidx = prsm->r_rtr_cnt - 1; 4187 if (TSTMP_GEQ(rsm->r_tim_lastsent[nidx], prsm->r_tim_lastsent[idx])) { 4188 /* Yes it was sent later (or at the same time) */ 4189 inter_gap = rsm->r_tim_lastsent[idx] - prsm->r_tim_lastsent[nidx]; 4190 } 4191 thresh += inter_gap; 4192 } else if (len <= maxseg) { 4193 /* 4194 * Possibly compensate for delayed-ack. 4195 */ 4196 uint32_t alt_thresh; 4197 4198 alt_thresh = srtt + (srtt / 2) + bbr_delayed_ack_time; 4199 if (alt_thresh > thresh) 4200 thresh = alt_thresh; 4201 } 4202 /* Not above the current RTO */ 4203 if (tp->t_srtt == 0) 4204 t_rxtcur = BBR_INITIAL_RTO; 4205 else 4206 t_rxtcur = TICKS_2_USEC(tp->t_rxtcur); 4207 4208 bbr_log_thresh_choice(bbr, cts, thresh, t_rxtcur, srtt, rsm, BBR_TO_FRM_TLP); 4209 /* Not above an RTO */ 4210 if (thresh > t_rxtcur) { 4211 thresh = t_rxtcur; 4212 } 4213 /* Not above a RTO max */ 4214 if (thresh > (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND)) { 4215 thresh = (((uint32_t)bbr->rc_max_rto_sec) * USECS_IN_SECOND); 4216 } 4217 /* And now apply the user TLP min */ 4218 if (thresh < bbr_tlp_min) { 4219 thresh = bbr_tlp_min; 4220 } 4221 return (thresh); 4222 } 4223 4224 /* 4225 * Return one of three RTTs to use (in microseconds). 4226 */ 4227 static __inline uint32_t 4228 bbr_get_rtt(struct tcp_bbr *bbr, int32_t rtt_type) 4229 { 4230 uint32_t f_rtt; 4231 uint32_t srtt; 4232 4233 f_rtt = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 4234 if (get_filter_value_small(&bbr->r_ctl.rc_rttprop) == 0xffffffff) { 4235 /* We have no rtt at all */ 4236 if (bbr->rc_tp->t_srtt == 0) 4237 f_rtt = BBR_INITIAL_RTO; 4238 else 4239 f_rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4240 /* 4241 * Since we don't know how good the rtt is apply a 4242 * delayed-ack min 4243 */ 4244 if (f_rtt < bbr_delayed_ack_time) { 4245 f_rtt = bbr_delayed_ack_time; 4246 } 4247 } 4248 /* Take the filter version or last measured pkt-rtt */ 4249 if (rtt_type == BBR_RTT_PROP) { 4250 srtt = f_rtt; 4251 } else if (rtt_type == BBR_RTT_PKTRTT) { 4252 if (bbr->r_ctl.rc_pkt_epoch_rtt) { 4253 srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 4254 } else { 4255 /* No pkt rtt yet */ 4256 srtt = f_rtt; 4257 } 4258 } else if (rtt_type == BBR_RTT_RACK) { 4259 srtt = bbr->r_ctl.rc_last_rtt; 4260 /* We need to add in any internal delay for our timer */ 4261 if (bbr->rc_ack_was_delayed) 4262 srtt += bbr->r_ctl.rc_ack_hdwr_delay; 4263 } else if (rtt_type == BBR_SRTT) { 4264 srtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 4265 } else { 4266 /* TSNH */ 4267 srtt = f_rtt; 4268 #ifdef BBR_INVARIANTS 4269 panic("Unknown rtt request type %d", rtt_type); 4270 #endif 4271 } 4272 return (srtt); 4273 } 4274 4275 static int 4276 bbr_is_lost(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t cts) 4277 { 4278 uint32_t thresh; 4279 4280 thresh = bbr_calc_thresh_rack(bbr, bbr_get_rtt(bbr, BBR_RTT_RACK), 4281 cts, rsm); 4282 if ((cts - rsm->r_tim_lastsent[(rsm->r_rtr_cnt - 1)]) >= thresh) { 4283 /* It is lost (past time) */ 4284 return (1); 4285 } 4286 return (0); 4287 } 4288 4289 /* 4290 * Return a sendmap if we need to retransmit something. 4291 */ 4292 static struct bbr_sendmap * 4293 bbr_check_recovery_mode(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4294 { 4295 /* 4296 * Check to see that we don't need to fall into recovery. We will 4297 * need to do so if our oldest transmit is past the time we should 4298 * have had an ack. 4299 */ 4300 4301 struct bbr_sendmap *rsm; 4302 int32_t idx; 4303 4304 if (TAILQ_EMPTY(&bbr->r_ctl.rc_map)) { 4305 /* Nothing outstanding that we know of */ 4306 return (NULL); 4307 } 4308 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 4309 if (rsm == NULL) { 4310 /* Nothing in the transmit map */ 4311 return (NULL); 4312 } 4313 if (tp->t_flags & TF_SENTFIN) { 4314 /* Fin restricted, don't find anything once a fin is sent */ 4315 return (NULL); 4316 } 4317 if (rsm->r_flags & BBR_ACKED) { 4318 /* 4319 * Ok the first one is acked (this really should not happen 4320 * since we remove the from the tmap once they are acked) 4321 */ 4322 rsm = bbr_find_lowest_rsm(bbr); 4323 if (rsm == NULL) 4324 return (NULL); 4325 } 4326 idx = rsm->r_rtr_cnt - 1; 4327 if (SEQ_LEQ(cts, rsm->r_tim_lastsent[idx])) { 4328 /* Send timestamp is the same or less? can't be ready */ 4329 return (NULL); 4330 } 4331 /* Get our RTT time */ 4332 if (bbr_is_lost(bbr, rsm, cts) && 4333 ((rsm->r_dupack >= DUP_ACK_THRESHOLD) || 4334 (rsm->r_flags & BBR_SACK_PASSED))) { 4335 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4336 rsm->r_flags |= BBR_MARKED_LOST; 4337 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4338 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4339 } 4340 bbr_cong_signal(tp, NULL, CC_NDUPACK, rsm); 4341 #ifdef BBR_INVARIANTS 4342 if ((rsm->r_end - rsm->r_start) == 0) 4343 panic("tp:%p bbr:%p rsm:%p length is 0?", tp, bbr, rsm); 4344 #endif 4345 return (rsm); 4346 } 4347 return (NULL); 4348 } 4349 4350 /* 4351 * RACK Timer, here we simply do logging and house keeping. 4352 * the normal bbr_output_wtime() function will call the 4353 * appropriate thing to check if we need to do a RACK retransmit. 4354 * We return 1, saying don't proceed with bbr_output_wtime only 4355 * when all timers have been stopped (destroyed PCB?). 4356 */ 4357 static int 4358 bbr_timeout_rack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4359 { 4360 /* 4361 * This timer simply provides an internal trigger to send out data. 4362 * The check_recovery_mode call will see if there are needed 4363 * retransmissions, if so we will enter fast-recovery. The output 4364 * call may or may not do the same thing depending on sysctl 4365 * settings. 4366 */ 4367 uint32_t lost; 4368 4369 if (bbr->rc_all_timers_stopped) { 4370 return (1); 4371 } 4372 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4373 /* Its not time yet */ 4374 return (0); 4375 } 4376 BBR_STAT_INC(bbr_to_tot); 4377 lost = bbr->r_ctl.rc_lost; 4378 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4379 bbr_set_state(tp, bbr, 0); 4380 bbr_log_to_event(bbr, cts, BBR_TO_FRM_RACK); 4381 if (bbr->r_ctl.rc_resend == NULL) { 4382 /* Lets do the check here */ 4383 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 4384 } 4385 if (bbr_policer_call_from_rack_to) 4386 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4387 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RACK; 4388 return (0); 4389 } 4390 4391 static __inline void 4392 bbr_clone_rsm(struct tcp_bbr *bbr, struct bbr_sendmap *nrsm, struct bbr_sendmap *rsm, uint32_t start) 4393 { 4394 int idx; 4395 4396 nrsm->r_start = start; 4397 nrsm->r_end = rsm->r_end; 4398 nrsm->r_rtr_cnt = rsm->r_rtr_cnt; 4399 nrsm-> r_rtt_not_allowed = rsm->r_rtt_not_allowed; 4400 nrsm->r_flags = rsm->r_flags; 4401 /* We don't transfer forward the SYN flag */ 4402 nrsm->r_flags &= ~BBR_HAS_SYN; 4403 /* We move forward the FIN flag, not that this should happen */ 4404 rsm->r_flags &= ~BBR_HAS_FIN; 4405 nrsm->r_dupack = rsm->r_dupack; 4406 nrsm->r_rtr_bytes = 0; 4407 nrsm->r_is_gain = rsm->r_is_gain; 4408 nrsm->r_is_drain = rsm->r_is_drain; 4409 nrsm->r_delivered = rsm->r_delivered; 4410 nrsm->r_ts_valid = rsm->r_ts_valid; 4411 nrsm->r_del_ack_ts = rsm->r_del_ack_ts; 4412 nrsm->r_del_time = rsm->r_del_time; 4413 nrsm->r_app_limited = rsm->r_app_limited; 4414 nrsm->r_first_sent_time = rsm->r_first_sent_time; 4415 nrsm->r_flight_at_send = rsm->r_flight_at_send; 4416 /* We split a piece the lower section looses any just_ret flag. */ 4417 nrsm->r_bbr_state = rsm->r_bbr_state; 4418 for (idx = 0; idx < nrsm->r_rtr_cnt; idx++) { 4419 nrsm->r_tim_lastsent[idx] = rsm->r_tim_lastsent[idx]; 4420 } 4421 rsm->r_end = nrsm->r_start; 4422 idx = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), bbr->r_ctl.rc_pace_max_segs); 4423 idx /= 8; 4424 /* Check if we got too small */ 4425 if ((rsm->r_is_smallmap == 0) && 4426 ((rsm->r_end - rsm->r_start) <= idx)) { 4427 bbr->r_ctl.rc_num_small_maps_alloced++; 4428 rsm->r_is_smallmap = 1; 4429 } 4430 /* Check the new one as well */ 4431 if ((nrsm->r_end - nrsm->r_start) <= idx) { 4432 bbr->r_ctl.rc_num_small_maps_alloced++; 4433 nrsm->r_is_smallmap = 1; 4434 } 4435 } 4436 4437 static int 4438 bbr_sack_mergable(struct bbr_sendmap *at, 4439 uint32_t start, uint32_t end) 4440 { 4441 /* 4442 * Given a sack block defined by 4443 * start and end, and a current position 4444 * at. Return 1 if either side of at 4445 * would show that the block is mergable 4446 * to that side. A block to be mergable 4447 * must have overlap with the start/end 4448 * and be in the SACK'd state. 4449 */ 4450 struct bbr_sendmap *l_rsm; 4451 struct bbr_sendmap *r_rsm; 4452 4453 /* first get the either side blocks */ 4454 l_rsm = TAILQ_PREV(at, bbr_head, r_next); 4455 r_rsm = TAILQ_NEXT(at, r_next); 4456 if (l_rsm && (l_rsm->r_flags & BBR_ACKED)) { 4457 /* Potentially mergeable */ 4458 if ((l_rsm->r_end == start) || 4459 (SEQ_LT(start, l_rsm->r_end) && 4460 SEQ_GT(end, l_rsm->r_end))) { 4461 /* 4462 * map blk |------| 4463 * sack blk |------| 4464 * <or> 4465 * map blk |------| 4466 * sack blk |------| 4467 */ 4468 return (1); 4469 } 4470 } 4471 if (r_rsm && (r_rsm->r_flags & BBR_ACKED)) { 4472 /* Potentially mergeable */ 4473 if ((r_rsm->r_start == end) || 4474 (SEQ_LT(start, r_rsm->r_start) && 4475 SEQ_GT(end, r_rsm->r_start))) { 4476 /* 4477 * map blk |---------| 4478 * sack blk |----| 4479 * <or> 4480 * map blk |---------| 4481 * sack blk |-------| 4482 */ 4483 return (1); 4484 } 4485 } 4486 return (0); 4487 } 4488 4489 static struct bbr_sendmap * 4490 bbr_merge_rsm(struct tcp_bbr *bbr, 4491 struct bbr_sendmap *l_rsm, 4492 struct bbr_sendmap *r_rsm) 4493 { 4494 /* 4495 * We are merging two ack'd RSM's, 4496 * the l_rsm is on the left (lower seq 4497 * values) and the r_rsm is on the right 4498 * (higher seq value). The simplest way 4499 * to merge these is to move the right 4500 * one into the left. I don't think there 4501 * is any reason we need to try to find 4502 * the oldest (or last oldest retransmitted). 4503 */ 4504 l_rsm->r_end = r_rsm->r_end; 4505 if (l_rsm->r_dupack < r_rsm->r_dupack) 4506 l_rsm->r_dupack = r_rsm->r_dupack; 4507 if (r_rsm->r_rtr_bytes) 4508 l_rsm->r_rtr_bytes += r_rsm->r_rtr_bytes; 4509 if (r_rsm->r_in_tmap) { 4510 /* This really should not happen */ 4511 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, r_rsm, r_tnext); 4512 } 4513 if (r_rsm->r_app_limited) 4514 l_rsm->r_app_limited = r_rsm->r_app_limited; 4515 /* Now the flags */ 4516 if (r_rsm->r_flags & BBR_HAS_FIN) 4517 l_rsm->r_flags |= BBR_HAS_FIN; 4518 if (r_rsm->r_flags & BBR_TLP) 4519 l_rsm->r_flags |= BBR_TLP; 4520 if (r_rsm->r_flags & BBR_RWND_COLLAPSED) 4521 l_rsm->r_flags |= BBR_RWND_COLLAPSED; 4522 if (r_rsm->r_flags & BBR_MARKED_LOST) { 4523 /* This really should not happen */ 4524 bbr->r_ctl.rc_lost_bytes -= r_rsm->r_end - r_rsm->r_start; 4525 } 4526 TAILQ_REMOVE(&bbr->r_ctl.rc_map, r_rsm, r_next); 4527 if ((r_rsm->r_limit_type == 0) && (l_rsm->r_limit_type != 0)) { 4528 /* Transfer the split limit to the map we free */ 4529 r_rsm->r_limit_type = l_rsm->r_limit_type; 4530 l_rsm->r_limit_type = 0; 4531 } 4532 bbr_free(bbr, r_rsm); 4533 return(l_rsm); 4534 } 4535 4536 /* 4537 * TLP Timer, here we simply setup what segment we want to 4538 * have the TLP expire on, the normal bbr_output_wtime() will then 4539 * send it out. 4540 * 4541 * We return 1, saying don't proceed with bbr_output_wtime only 4542 * when all timers have been stopped (destroyed PCB?). 4543 */ 4544 static int 4545 bbr_timeout_tlp(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4546 { 4547 /* 4548 * Tail Loss Probe. 4549 */ 4550 struct bbr_sendmap *rsm = NULL; 4551 struct socket *so; 4552 uint32_t amm; 4553 uint32_t out, avail; 4554 uint32_t maxseg; 4555 int collapsed_win = 0; 4556 4557 if (bbr->rc_all_timers_stopped) { 4558 return (1); 4559 } 4560 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 4561 /* Its not time yet */ 4562 return (0); 4563 } 4564 if (ctf_progress_timeout_check(tp, true)) { 4565 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4566 return (-ETIMEDOUT); /* tcp_drop() */ 4567 } 4568 /* Did we somehow get into persists? */ 4569 if (bbr->rc_in_persist) { 4570 return (0); 4571 } 4572 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4573 bbr_set_state(tp, bbr, 0); 4574 BBR_STAT_INC(bbr_tlp_tot); 4575 maxseg = tp->t_maxseg - bbr->rc_last_options; 4576 /* 4577 * A TLP timer has expired. We have been idle for 2 rtts. So we now 4578 * need to figure out how to force a full MSS segment out. 4579 */ 4580 so = tptosocket(tp); 4581 avail = sbavail(&so->so_snd); 4582 out = ctf_outstanding(tp); 4583 if (out > tp->snd_wnd) { 4584 /* special case, we need a retransmission */ 4585 collapsed_win = 1; 4586 goto need_retran; 4587 } 4588 if (avail > out) { 4589 /* New data is available */ 4590 amm = avail - out; 4591 if (amm > maxseg) { 4592 amm = maxseg; 4593 } else if ((amm < maxseg) && ((tp->t_flags & TF_NODELAY) == 0)) { 4594 /* not enough to fill a MTU and no-delay is off */ 4595 goto need_retran; 4596 } 4597 /* Set the send-new override */ 4598 if ((out + amm) <= tp->snd_wnd) { 4599 bbr->rc_tlp_new_data = 1; 4600 } else { 4601 goto need_retran; 4602 } 4603 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4604 bbr->r_ctl.rc_last_tlp_seq = tp->snd_max; 4605 bbr->r_ctl.rc_tlp_send = NULL; 4606 /* cap any slots */ 4607 BBR_STAT_INC(bbr_tlp_newdata); 4608 goto send; 4609 } 4610 need_retran: 4611 /* 4612 * Ok we need to arrange the last un-acked segment to be re-sent, or 4613 * optionally the first un-acked segment. 4614 */ 4615 if (collapsed_win == 0) { 4616 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 4617 if (rsm && (rsm->r_flags & (BBR_ACKED | BBR_HAS_FIN))) { 4618 rsm = bbr_find_high_nonack(bbr, rsm); 4619 } 4620 if (rsm == NULL) { 4621 goto restore; 4622 } 4623 } else { 4624 /* 4625 * We must find the last segment 4626 * that was acceptable by the client. 4627 */ 4628 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 4629 if ((rsm->r_flags & BBR_RWND_COLLAPSED) == 0) { 4630 /* Found one */ 4631 break; 4632 } 4633 } 4634 if (rsm == NULL) { 4635 /* None? if so send the first */ 4636 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4637 if (rsm == NULL) 4638 goto restore; 4639 } 4640 } 4641 if ((rsm->r_end - rsm->r_start) > maxseg) { 4642 /* 4643 * We need to split this the last segment in two. 4644 */ 4645 struct bbr_sendmap *nrsm; 4646 4647 nrsm = bbr_alloc_full_limit(bbr); 4648 if (nrsm == NULL) { 4649 /* 4650 * We can't get memory to split, we can either just 4651 * not split it. Or retransmit the whole piece, lets 4652 * do the large send (BTLP :-) ). 4653 */ 4654 goto go_for_it; 4655 } 4656 bbr_clone_rsm(bbr, nrsm, rsm, (rsm->r_end - maxseg)); 4657 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 4658 if (rsm->r_in_tmap) { 4659 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 4660 nrsm->r_in_tmap = 1; 4661 } 4662 rsm->r_flags &= (~BBR_HAS_FIN); 4663 rsm = nrsm; 4664 } 4665 go_for_it: 4666 bbr->r_ctl.rc_tlp_send = rsm; 4667 bbr->rc_tlp_rtx_out = 1; 4668 if (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) { 4669 bbr->r_ctl.rc_tlp_seg_send_cnt++; 4670 tp->t_rxtshift++; 4671 } else { 4672 bbr->r_ctl.rc_last_tlp_seq = rsm->r_start; 4673 bbr->r_ctl.rc_tlp_seg_send_cnt = 1; 4674 } 4675 send: 4676 if (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend) { 4677 /* 4678 * Can't [re]/transmit a segment we have retransmitted the 4679 * max times. We need the retransmit timer to take over. 4680 */ 4681 restore: 4682 bbr->rc_tlp_new_data = 0; 4683 bbr->r_ctl.rc_tlp_send = NULL; 4684 if (rsm) 4685 rsm->r_flags &= ~BBR_TLP; 4686 BBR_STAT_INC(bbr_tlp_retran_fail); 4687 return (0); 4688 } else if (rsm) { 4689 rsm->r_flags |= BBR_TLP; 4690 } 4691 if (rsm && (rsm->r_start == bbr->r_ctl.rc_last_tlp_seq) && 4692 (bbr->r_ctl.rc_tlp_seg_send_cnt > bbr_tlp_max_resend)) { 4693 /* 4694 * We have retransmitted to many times for TLP. Switch to 4695 * the regular RTO timer 4696 */ 4697 goto restore; 4698 } 4699 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TLP); 4700 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_TLP; 4701 return (0); 4702 } 4703 4704 /* 4705 * Delayed ack Timer, here we simply need to setup the 4706 * ACK_NOW flag and remove the DELACK flag. From there 4707 * the output routine will send the ack out. 4708 * 4709 * We only return 1, saying don't proceed, if all timers 4710 * are stopped (destroyed PCB?). 4711 */ 4712 static int 4713 bbr_timeout_delack(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4714 { 4715 if (bbr->rc_all_timers_stopped) { 4716 return (1); 4717 } 4718 bbr_log_to_event(bbr, cts, BBR_TO_FRM_DELACK); 4719 tp->t_flags &= ~TF_DELACK; 4720 tp->t_flags |= TF_ACKNOW; 4721 KMOD_TCPSTAT_INC(tcps_delack); 4722 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_DELACK; 4723 return (0); 4724 } 4725 4726 /* 4727 * Here we send a KEEP-ALIVE like probe to the 4728 * peer, we do not send data. 4729 * 4730 * We only return 1, saying don't proceed, if all timers 4731 * are stopped (destroyed PCB?). 4732 */ 4733 static int 4734 bbr_timeout_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4735 { 4736 struct tcptemp *t_template; 4737 int32_t retval = 1; 4738 4739 if (bbr->rc_all_timers_stopped) { 4740 return (1); 4741 } 4742 if (bbr->rc_in_persist == 0) 4743 return (0); 4744 4745 /* 4746 * Persistence timer into zero window. Force a byte to be output, if 4747 * possible. 4748 */ 4749 bbr_log_to_event(bbr, cts, BBR_TO_FRM_PERSIST); 4750 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_PERSIT; 4751 KMOD_TCPSTAT_INC(tcps_persisttimeo); 4752 /* 4753 * Have we exceeded the user specified progress time? 4754 */ 4755 if (ctf_progress_timeout_check(tp, true)) { 4756 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4757 return (-ETIMEDOUT); /* tcp_drop() */ 4758 } 4759 /* 4760 * Hack: if the peer is dead/unreachable, we do not time out if the 4761 * window is closed. After a full backoff, drop the connection if 4762 * the idle time (no responses to probes) reaches the maximum 4763 * backoff that we would use if retransmitting. 4764 */ 4765 if (tp->t_rxtshift >= V_tcp_retries && 4766 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 4767 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 4768 KMOD_TCPSTAT_INC(tcps_persistdrop); 4769 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4770 return (-ETIMEDOUT); /* tcp_drop() */ 4771 } 4772 if ((sbavail(&bbr->rc_inp->inp_socket->so_snd) == 0) && 4773 tp->snd_una == tp->snd_max) { 4774 bbr_exit_persist(tp, bbr, cts, __LINE__); 4775 retval = 0; 4776 goto out; 4777 } 4778 /* 4779 * If the user has closed the socket then drop a persisting 4780 * connection after a much reduced timeout. 4781 */ 4782 if (tp->t_state > TCPS_CLOSE_WAIT && 4783 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 4784 KMOD_TCPSTAT_INC(tcps_persistdrop); 4785 tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX); 4786 return (-ETIMEDOUT); /* tcp_drop() */ 4787 } 4788 t_template = tcpip_maketemplate(bbr->rc_inp); 4789 if (t_template) { 4790 tcp_respond(tp, t_template->tt_ipgen, 4791 &t_template->tt_t, (struct mbuf *)NULL, 4792 tp->rcv_nxt, tp->snd_una - 1, 0); 4793 /* This sends an ack */ 4794 if (tp->t_flags & TF_DELACK) 4795 tp->t_flags &= ~TF_DELACK; 4796 free(t_template, M_TEMP); 4797 } 4798 if (tp->t_rxtshift < V_tcp_retries) 4799 tp->t_rxtshift++; 4800 bbr_start_hpts_timer(bbr, tp, cts, 3, 0, 0); 4801 out: 4802 return (retval); 4803 } 4804 4805 /* 4806 * If a keepalive goes off, we had no other timers 4807 * happening. We always return 1 here since this 4808 * routine either drops the connection or sends 4809 * out a segment with respond. 4810 */ 4811 static int 4812 bbr_timeout_keepalive(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4813 { 4814 struct tcptemp *t_template; 4815 struct inpcb *inp = tptoinpcb(tp); 4816 4817 if (bbr->rc_all_timers_stopped) { 4818 return (1); 4819 } 4820 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_KEEP; 4821 bbr_log_to_event(bbr, cts, BBR_TO_FRM_KEEP); 4822 /* 4823 * Keep-alive timer went off; send something or drop connection if 4824 * idle for too long. 4825 */ 4826 KMOD_TCPSTAT_INC(tcps_keeptimeo); 4827 if (tp->t_state < TCPS_ESTABLISHED) 4828 goto dropit; 4829 if ((V_tcp_always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 4830 tp->t_state <= TCPS_CLOSING) { 4831 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 4832 goto dropit; 4833 /* 4834 * Send a packet designed to force a response if the peer is 4835 * up and reachable: either an ACK if the connection is 4836 * still alive, or an RST if the peer has closed the 4837 * connection due to timeout or reboot. Using sequence 4838 * number tp->snd_una-1 causes the transmitted zero-length 4839 * segment to lie outside the receive window; by the 4840 * protocol spec, this requires the correspondent TCP to 4841 * respond. 4842 */ 4843 KMOD_TCPSTAT_INC(tcps_keepprobe); 4844 t_template = tcpip_maketemplate(inp); 4845 if (t_template) { 4846 tcp_respond(tp, t_template->tt_ipgen, 4847 &t_template->tt_t, (struct mbuf *)NULL, 4848 tp->rcv_nxt, tp->snd_una - 1, 0); 4849 free(t_template, M_TEMP); 4850 } 4851 } 4852 bbr_start_hpts_timer(bbr, tp, cts, 4, 0, 0); 4853 return (1); 4854 dropit: 4855 KMOD_TCPSTAT_INC(tcps_keepdrops); 4856 tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX); 4857 return (-ETIMEDOUT); /* tcp_drop() */ 4858 } 4859 4860 /* 4861 * Retransmit helper function, clear up all the ack 4862 * flags and take care of important book keeping. 4863 */ 4864 static void 4865 bbr_remxt_tmr(struct tcpcb *tp) 4866 { 4867 /* 4868 * The retransmit timer went off, all sack'd blocks must be 4869 * un-acked. 4870 */ 4871 struct bbr_sendmap *rsm, *trsm = NULL; 4872 struct tcp_bbr *bbr; 4873 uint32_t cts, lost; 4874 4875 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 4876 cts = tcp_get_usecs(&bbr->rc_tv); 4877 lost = bbr->r_ctl.rc_lost; 4878 if (bbr->r_state && (bbr->r_state != tp->t_state)) 4879 bbr_set_state(tp, bbr, 0); 4880 4881 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 4882 if (rsm->r_flags & BBR_ACKED) { 4883 uint32_t old_flags; 4884 4885 rsm->r_dupack = 0; 4886 if (rsm->r_in_tmap == 0) { 4887 /* We must re-add it back to the tlist */ 4888 if (trsm == NULL) { 4889 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 4890 } else { 4891 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, trsm, rsm, r_tnext); 4892 } 4893 rsm->r_in_tmap = 1; 4894 } 4895 old_flags = rsm->r_flags; 4896 rsm->r_flags |= BBR_RXT_CLEARED; 4897 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS); 4898 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 4899 } else { 4900 if ((tp->t_state < TCPS_ESTABLISHED) && 4901 (rsm->r_start == tp->snd_una)) { 4902 /* 4903 * Special case for TCP FO. Where 4904 * we sent more data beyond the snd_max. 4905 * We don't mark that as lost and stop here. 4906 */ 4907 break; 4908 } 4909 if ((rsm->r_flags & BBR_MARKED_LOST) == 0) { 4910 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 4911 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 4912 } 4913 if (bbr_marks_rxt_sack_passed) { 4914 /* 4915 * With this option, we will rack out 4916 * in 1ms increments the rest of the packets. 4917 */ 4918 rsm->r_flags |= BBR_SACK_PASSED | BBR_MARKED_LOST; 4919 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4920 } else { 4921 /* 4922 * With this option we only mark them lost 4923 * and remove all sack'd markings. We will run 4924 * another RXT or a TLP. This will cause 4925 * us to eventually send more based on what 4926 * ack's come in. 4927 */ 4928 rsm->r_flags |= BBR_MARKED_LOST; 4929 rsm->r_flags &= ~BBR_WAS_SACKPASS; 4930 rsm->r_flags &= ~BBR_SACK_PASSED; 4931 } 4932 } 4933 trsm = rsm; 4934 } 4935 bbr->r_ctl.rc_resend = TAILQ_FIRST(&bbr->r_ctl.rc_map); 4936 /* Clear the count (we just un-acked them) */ 4937 bbr_log_to_event(bbr, cts, BBR_TO_FRM_TMR); 4938 bbr->rc_tlp_new_data = 0; 4939 bbr->r_ctl.rc_tlp_seg_send_cnt = 0; 4940 /* zap the behindness on a rxt */ 4941 bbr->r_ctl.rc_hptsi_agg_delay = 0; 4942 bbr->r_agg_early_set = 0; 4943 bbr->r_ctl.rc_agg_early = 0; 4944 bbr->rc_tlp_rtx_out = 0; 4945 bbr->r_ctl.rc_sacked = 0; 4946 bbr->r_ctl.rc_sacklast = NULL; 4947 bbr->r_timer_override = 1; 4948 bbr_lt_bw_sampling(bbr, cts, (bbr->r_ctl.rc_lost > lost)); 4949 } 4950 4951 /* 4952 * Re-transmit timeout! If we drop the PCB we will return 1, otherwise 4953 * we will setup to retransmit the lowest seq number outstanding. 4954 */ 4955 static int 4956 bbr_timeout_rxt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 4957 { 4958 struct inpcb *inp = tptoinpcb(tp); 4959 int32_t rexmt; 4960 int32_t retval = 0; 4961 bool isipv6; 4962 4963 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_RXT; 4964 if (bbr->rc_all_timers_stopped) { 4965 return (1); 4966 } 4967 if (TCPS_HAVEESTABLISHED(tp->t_state) && 4968 (tp->snd_una == tp->snd_max)) { 4969 /* Nothing outstanding .. nothing to do */ 4970 return (0); 4971 } 4972 /* 4973 * Retransmission timer went off. Message has not been acked within 4974 * retransmit interval. Back off to a longer retransmit interval 4975 * and retransmit one segment. 4976 */ 4977 if (ctf_progress_timeout_check(tp, true)) { 4978 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 4979 return (-ETIMEDOUT); /* tcp_drop() */ 4980 } 4981 bbr_remxt_tmr(tp); 4982 if ((bbr->r_ctl.rc_resend == NULL) || 4983 ((bbr->r_ctl.rc_resend->r_flags & BBR_RWND_COLLAPSED) == 0)) { 4984 /* 4985 * If the rwnd collapsed on 4986 * the one we are retransmitting 4987 * it does not count against the 4988 * rxt count. 4989 */ 4990 tp->t_rxtshift++; 4991 } 4992 if (tp->t_rxtshift > V_tcp_retries) { 4993 tp->t_rxtshift = V_tcp_retries; 4994 KMOD_TCPSTAT_INC(tcps_timeoutdrop); 4995 tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN); 4996 /* XXXGL: previously t_softerror was casted to uint16_t */ 4997 MPASS(tp->t_softerror >= 0); 4998 retval = tp->t_softerror ? -tp->t_softerror : -ETIMEDOUT; 4999 return (retval); /* tcp_drop() */ 5000 } 5001 if (tp->t_state == TCPS_SYN_SENT) { 5002 /* 5003 * If the SYN was retransmitted, indicate CWND to be limited 5004 * to 1 segment in cc_conn_init(). 5005 */ 5006 tp->snd_cwnd = 1; 5007 } else if (tp->t_rxtshift == 1) { 5008 /* 5009 * first retransmit; record ssthresh and cwnd so they can be 5010 * recovered if this turns out to be a "bad" retransmit. A 5011 * retransmit is considered "bad" if an ACK for this segment 5012 * is received within RTT/2 interval; the assumption here is 5013 * that the ACK was already in flight. See "On Estimating 5014 * End-to-End Network Path Properties" by Allman and Paxson 5015 * for more details. 5016 */ 5017 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5018 if (!IN_RECOVERY(tp->t_flags)) { 5019 tp->snd_cwnd_prev = tp->snd_cwnd; 5020 tp->snd_ssthresh_prev = tp->snd_ssthresh; 5021 tp->snd_recover_prev = tp->snd_recover; 5022 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 5023 tp->t_flags |= TF_PREVVALID; 5024 } else { 5025 tp->t_flags &= ~TF_PREVVALID; 5026 } 5027 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5028 } else { 5029 tp->snd_cwnd = tp->t_maxseg - bbr->rc_last_options; 5030 tp->t_flags &= ~TF_PREVVALID; 5031 } 5032 KMOD_TCPSTAT_INC(tcps_rexmttimeo); 5033 if ((tp->t_state == TCPS_SYN_SENT) || 5034 (tp->t_state == TCPS_SYN_RECEIVED)) 5035 rexmt = USEC_2_TICKS(BBR_INITIAL_RTO) * tcp_backoff[tp->t_rxtshift]; 5036 else 5037 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 5038 TCPT_RANGESET(tp->t_rxtcur, rexmt, 5039 MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), 5040 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 5041 /* 5042 * We enter the path for PLMTUD if connection is established or, if 5043 * connection is FIN_WAIT_1 status, reason for the last is that if 5044 * amount of data we send is very small, we could send it in couple 5045 * of packets and process straight to FIN. In that case we won't 5046 * catch ESTABLISHED state. 5047 */ 5048 #ifdef INET6 5049 isipv6 = (inp->inp_vflag & INP_IPV6) ? true : false; 5050 #else 5051 isipv6 = false; 5052 #endif 5053 if (((V_tcp_pmtud_blackhole_detect == 1) || 5054 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) || 5055 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) && 5056 ((tp->t_state == TCPS_ESTABLISHED) || 5057 (tp->t_state == TCPS_FIN_WAIT_1))) { 5058 /* 5059 * Idea here is that at each stage of mtu probe (usually, 5060 * 1448 -> 1188 -> 524) should be given 2 chances to recover 5061 * before further clamping down. 'tp->t_rxtshift % 2 == 0' 5062 * should take care of that. 5063 */ 5064 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) == 5065 (TF2_PLPMTU_PMTUD | TF2_PLPMTU_MAXSEGSNT)) && 5066 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 5067 tp->t_rxtshift % 2 == 0)) { 5068 /* 5069 * Enter Path MTU Black-hole Detection mechanism: - 5070 * Disable Path MTU Discovery (IP "DF" bit). - 5071 * Reduce MTU to lower value than what we negotiated 5072 * with peer. 5073 */ 5074 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 5075 /* 5076 * Record that we may have found a black 5077 * hole. 5078 */ 5079 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 5080 /* Keep track of previous MSS. */ 5081 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 5082 } 5083 /* 5084 * Reduce the MSS to blackhole value or to the 5085 * default in an attempt to retransmit. 5086 */ 5087 #ifdef INET6 5088 isipv6 = bbr->r_is_v6; 5089 if (isipv6 && 5090 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 5091 /* Use the sysctl tuneable blackhole MSS. */ 5092 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 5093 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5094 } else if (isipv6) { 5095 /* Use the default MSS. */ 5096 tp->t_maxseg = V_tcp_v6mssdflt; 5097 /* 5098 * Disable Path MTU Discovery when we switch 5099 * to minmss. 5100 */ 5101 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5102 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5103 } 5104 #endif 5105 #if defined(INET6) && defined(INET) 5106 else 5107 #endif 5108 #ifdef INET 5109 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 5110 /* Use the sysctl tuneable blackhole MSS. */ 5111 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 5112 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated); 5113 } else { 5114 /* Use the default MSS. */ 5115 tp->t_maxseg = V_tcp_mssdflt; 5116 /* 5117 * Disable Path MTU Discovery when we switch 5118 * to minmss. 5119 */ 5120 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 5121 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 5122 } 5123 #endif 5124 } else { 5125 /* 5126 * If further retransmissions are still unsuccessful 5127 * with a lowered MTU, maybe this isn't a blackhole 5128 * and we restore the previous MSS and blackhole 5129 * detection flags. The limit '6' is determined by 5130 * giving each probe stage (1448, 1188, 524) 2 5131 * chances to recover. 5132 */ 5133 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 5134 (tp->t_rxtshift >= 6)) { 5135 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 5136 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 5137 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 5138 if (tp->t_maxseg < V_tcp_mssdflt) { 5139 /* 5140 * The MSS is so small we should not 5141 * process incoming SACK's since we are 5142 * subject to attack in such a case. 5143 */ 5144 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 5145 } else { 5146 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 5147 } 5148 KMOD_TCPSTAT_INC(tcps_pmtud_blackhole_failed); 5149 } 5150 } 5151 } 5152 /* 5153 * Disable RFC1323 and SACK if we haven't got any response to our 5154 * third SYN to work-around some broken terminal servers (most of 5155 * which have hopefully been retired) that have bad VJ header 5156 * compression code which trashes TCP segments containing 5157 * unknown-to-them TCP options. 5158 */ 5159 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 5160 (tp->t_rxtshift == 3)) 5161 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_SACK_PERMIT); 5162 /* 5163 * If we backed off this far, our srtt estimate is probably bogus. 5164 * Clobber it so we'll take the next rtt measurement as our srtt; 5165 * move the current srtt into rttvar to keep the current retransmit 5166 * times until then. 5167 */ 5168 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 5169 #ifdef INET6 5170 if (bbr->r_is_v6) 5171 in6_losing(inp); 5172 else 5173 #endif 5174 in_losing(inp); 5175 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 5176 tp->t_srtt = 0; 5177 } 5178 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 5179 tp->snd_recover = tp->snd_max; 5180 tp->t_flags |= TF_ACKNOW; 5181 tp->t_rtttime = 0; 5182 5183 return (retval); 5184 } 5185 5186 static int 5187 bbr_process_timers(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, uint8_t hpts_calling) 5188 { 5189 int32_t ret = 0; 5190 int32_t timers = (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK); 5191 5192 if (timers == 0) { 5193 return (0); 5194 } 5195 if (tp->t_state == TCPS_LISTEN) { 5196 /* no timers on listen sockets */ 5197 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) 5198 return (0); 5199 return (1); 5200 } 5201 if (TSTMP_LT(cts, bbr->r_ctl.rc_timer_exp)) { 5202 uint32_t left; 5203 5204 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 5205 ret = -1; 5206 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5207 return (0); 5208 } 5209 if (hpts_calling == 0) { 5210 ret = -2; 5211 bbr_log_to_processing(bbr, cts, ret, 0, hpts_calling); 5212 return (0); 5213 } 5214 /* 5215 * Ok our timer went off early and we are not paced false 5216 * alarm, go back to sleep. 5217 */ 5218 left = bbr->r_ctl.rc_timer_exp - cts; 5219 ret = -3; 5220 bbr_log_to_processing(bbr, cts, ret, left, hpts_calling); 5221 tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(left)); 5222 return (1); 5223 } 5224 bbr->rc_tmr_stopped = 0; 5225 bbr->r_ctl.rc_hpts_flags &= ~PACE_TMR_MASK; 5226 if (timers & PACE_TMR_DELACK) { 5227 ret = bbr_timeout_delack(tp, bbr, cts); 5228 } else if (timers & PACE_TMR_PERSIT) { 5229 ret = bbr_timeout_persist(tp, bbr, cts); 5230 } else if (timers & PACE_TMR_RACK) { 5231 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5232 ret = bbr_timeout_rack(tp, bbr, cts); 5233 } else if (timers & PACE_TMR_TLP) { 5234 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5235 ret = bbr_timeout_tlp(tp, bbr, cts); 5236 } else if (timers & PACE_TMR_RXT) { 5237 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 5238 ret = bbr_timeout_rxt(tp, bbr, cts); 5239 } else if (timers & PACE_TMR_KEEP) { 5240 ret = bbr_timeout_keepalive(tp, bbr, cts); 5241 } 5242 bbr_log_to_processing(bbr, cts, ret, timers, hpts_calling); 5243 return (ret); 5244 } 5245 5246 static void 5247 bbr_timer_cancel(struct tcp_bbr *bbr, int32_t line, uint32_t cts) 5248 { 5249 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 5250 uint8_t hpts_removed = 0; 5251 5252 if (tcp_in_hpts(bbr->rc_tp) && 5253 (bbr->rc_timer_first == 1)) { 5254 /* 5255 * If we are canceling timer's when we have the 5256 * timer ahead of the output being paced. We also 5257 * must remove ourselves from the hpts. 5258 */ 5259 hpts_removed = 1; 5260 tcp_hpts_remove(bbr->rc_tp); 5261 if (bbr->r_ctl.rc_last_delay_val) { 5262 /* Update the last hptsi delay too */ 5263 uint32_t time_since_send; 5264 5265 if (TSTMP_GT(cts, bbr->rc_pacer_started)) 5266 time_since_send = cts - bbr->rc_pacer_started; 5267 else 5268 time_since_send = 0; 5269 if (bbr->r_ctl.rc_last_delay_val > time_since_send) { 5270 /* Cut down our slot time */ 5271 bbr->r_ctl.rc_last_delay_val -= time_since_send; 5272 } else { 5273 bbr->r_ctl.rc_last_delay_val = 0; 5274 } 5275 bbr->rc_pacer_started = cts; 5276 } 5277 } 5278 bbr->rc_timer_first = 0; 5279 bbr_log_to_cancel(bbr, line, cts, hpts_removed); 5280 bbr->rc_tmr_stopped = bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK; 5281 bbr->r_ctl.rc_hpts_flags &= ~(PACE_TMR_MASK); 5282 } 5283 } 5284 5285 static int 5286 bbr_stopall(struct tcpcb *tp) 5287 { 5288 struct tcp_bbr *bbr; 5289 5290 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 5291 bbr->rc_all_timers_stopped = 1; 5292 5293 tcp_hpts_remove(tp); 5294 5295 return (0); 5296 } 5297 5298 static uint32_t 5299 bbr_get_earliest_send_outstanding(struct tcp_bbr *bbr, struct bbr_sendmap *u_rsm, uint32_t cts) 5300 { 5301 struct bbr_sendmap *rsm; 5302 5303 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 5304 if ((rsm == NULL) || (u_rsm == rsm)) 5305 return (cts); 5306 return(rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)]); 5307 } 5308 5309 static void 5310 bbr_update_rsm(struct tcpcb *tp, struct tcp_bbr *bbr, 5311 struct bbr_sendmap *rsm, uint32_t cts, uint32_t pacing_time) 5312 { 5313 int32_t idx; 5314 5315 rsm->r_rtr_cnt++; 5316 rsm->r_dupack = 0; 5317 if (rsm->r_rtr_cnt > BBR_NUM_OF_RETRANS) { 5318 rsm->r_rtr_cnt = BBR_NUM_OF_RETRANS; 5319 rsm->r_flags |= BBR_OVERMAX; 5320 } 5321 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 5322 /* Take off the collapsed flag at rxt */ 5323 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 5324 } 5325 if (rsm->r_flags & BBR_MARKED_LOST) { 5326 /* We have retransmitted, its no longer lost */ 5327 rsm->r_flags &= ~BBR_MARKED_LOST; 5328 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 5329 } 5330 if (rsm->r_flags & BBR_RXT_CLEARED) { 5331 /* 5332 * We hit a RXT timer on it and 5333 * we cleared the "acked" flag. 5334 * We now have it going back into 5335 * flight, we can remove the cleared 5336 * flag and possibly do accounting on 5337 * this piece. 5338 */ 5339 rsm->r_flags &= ~BBR_RXT_CLEARED; 5340 } 5341 if ((rsm->r_rtr_cnt > 1) && ((rsm->r_flags & BBR_TLP) == 0)) { 5342 bbr->r_ctl.rc_holes_rxt += (rsm->r_end - rsm->r_start); 5343 rsm->r_rtr_bytes += (rsm->r_end - rsm->r_start); 5344 } 5345 idx = rsm->r_rtr_cnt - 1; 5346 rsm->r_tim_lastsent[idx] = cts; 5347 rsm->r_pacing_delay = pacing_time; 5348 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5349 rsm->r_ts_valid = bbr->rc_ts_valid; 5350 if (bbr->rc_ts_valid) 5351 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5352 if (bbr->r_ctl.r_app_limited_until) 5353 rsm->r_app_limited = 1; 5354 else 5355 rsm->r_app_limited = 0; 5356 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5357 rsm->r_bbr_state = bbr_state_val(bbr); 5358 else 5359 rsm->r_bbr_state = 8; 5360 if (rsm->r_flags & BBR_ACKED) { 5361 /* Problably MTU discovery messing with us */ 5362 uint32_t old_flags; 5363 5364 old_flags = rsm->r_flags; 5365 rsm->r_flags &= ~BBR_ACKED; 5366 bbr_log_type_rsmclear(bbr, cts, rsm, old_flags, __LINE__); 5367 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 5368 if (bbr->r_ctl.rc_sacked == 0) 5369 bbr->r_ctl.rc_sacklast = NULL; 5370 } 5371 if (rsm->r_in_tmap) { 5372 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5373 } 5374 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5375 rsm->r_in_tmap = 1; 5376 if (rsm->r_flags & BBR_SACK_PASSED) { 5377 /* We have retransmitted due to the SACK pass */ 5378 rsm->r_flags &= ~BBR_SACK_PASSED; 5379 rsm->r_flags |= BBR_WAS_SACKPASS; 5380 } 5381 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5382 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5383 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5384 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 5385 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5386 rsm->r_is_gain = 1; 5387 rsm->r_is_drain = 0; 5388 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 5389 rsm->r_is_drain = 1; 5390 rsm->r_is_gain = 0; 5391 } else { 5392 rsm->r_is_drain = 0; 5393 rsm->r_is_gain = 0; 5394 } 5395 rsm->r_del_time = bbr->r_ctl.rc_del_time; /* TEMP GOOGLE CODE */ 5396 } 5397 5398 /* 5399 * Returns 0, or the sequence where we stopped 5400 * updating. We also update the lenp to be the amount 5401 * of data left. 5402 */ 5403 5404 static uint32_t 5405 bbr_update_entry(struct tcpcb *tp, struct tcp_bbr *bbr, 5406 struct bbr_sendmap *rsm, uint32_t cts, int32_t *lenp, uint32_t pacing_time) 5407 { 5408 /* 5409 * We (re-)transmitted starting at rsm->r_start for some length 5410 * (possibly less than r_end. 5411 */ 5412 struct bbr_sendmap *nrsm; 5413 uint32_t c_end; 5414 int32_t len; 5415 5416 len = *lenp; 5417 c_end = rsm->r_start + len; 5418 if (SEQ_GEQ(c_end, rsm->r_end)) { 5419 /* 5420 * We retransmitted the whole piece or more than the whole 5421 * slopping into the next rsm. 5422 */ 5423 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5424 if (c_end == rsm->r_end) { 5425 *lenp = 0; 5426 return (0); 5427 } else { 5428 int32_t act_len; 5429 5430 /* Hangs over the end return whats left */ 5431 act_len = rsm->r_end - rsm->r_start; 5432 *lenp = (len - act_len); 5433 return (rsm->r_end); 5434 } 5435 /* We don't get out of this block. */ 5436 } 5437 /* 5438 * Here we retransmitted less than the whole thing which means we 5439 * have to split this into what was transmitted and what was not. 5440 */ 5441 nrsm = bbr_alloc_full_limit(bbr); 5442 if (nrsm == NULL) { 5443 *lenp = 0; 5444 return (0); 5445 } 5446 /* 5447 * So here we are going to take the original rsm and make it what we 5448 * retransmitted. nrsm will be the tail portion we did not 5449 * retransmit. For example say the chunk was 1, 11 (10 bytes). And 5450 * we retransmitted 5 bytes i.e. 1, 5. The original piece shrinks to 5451 * 1, 6 and the new piece will be 6, 11. 5452 */ 5453 bbr_clone_rsm(bbr, nrsm, rsm, c_end); 5454 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 5455 nrsm->r_dupack = 0; 5456 if (rsm->r_in_tmap) { 5457 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 5458 nrsm->r_in_tmap = 1; 5459 } 5460 rsm->r_flags &= (~BBR_HAS_FIN); 5461 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 5462 *lenp = 0; 5463 return (0); 5464 } 5465 5466 static uint64_t 5467 bbr_get_hardware_rate(struct tcp_bbr *bbr) 5468 { 5469 uint64_t bw; 5470 5471 bw = bbr_get_bw(bbr); 5472 bw *= (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]; 5473 bw /= (uint64_t)BBR_UNIT; 5474 return(bw); 5475 } 5476 5477 static void 5478 bbr_setup_less_of_rate(struct tcp_bbr *bbr, uint32_t cts, 5479 uint64_t act_rate, uint64_t rate_wanted) 5480 { 5481 /* 5482 * We could not get a full gains worth 5483 * of rate. 5484 */ 5485 if (get_filter_value(&bbr->r_ctl.rc_delrate) >= act_rate) { 5486 /* we can't even get the real rate */ 5487 uint64_t red; 5488 5489 bbr->skip_gain = 1; 5490 bbr->gain_is_limited = 0; 5491 red = get_filter_value(&bbr->r_ctl.rc_delrate) - act_rate; 5492 if (red) 5493 filter_reduce_by(&bbr->r_ctl.rc_delrate, red, cts); 5494 } else { 5495 /* We can use a lower gain */ 5496 bbr->skip_gain = 0; 5497 bbr->gain_is_limited = 1; 5498 } 5499 } 5500 5501 static void 5502 bbr_update_hardware_pacing_rate(struct tcp_bbr *bbr, uint32_t cts) 5503 { 5504 const struct tcp_hwrate_limit_table *nrte; 5505 int error, rate = -1; 5506 5507 if (bbr->r_ctl.crte == NULL) 5508 return; 5509 if ((bbr->rc_inp->inp_route.ro_nh == NULL) || 5510 (bbr->rc_inp->inp_route.ro_nh->nh_ifp == NULL)) { 5511 /* Lost our routes? */ 5512 /* Clear the way for a re-attempt */ 5513 bbr->bbr_attempt_hdwr_pace = 0; 5514 lost_rate: 5515 bbr->gain_is_limited = 0; 5516 bbr->skip_gain = 0; 5517 bbr->bbr_hdrw_pacing = 0; 5518 counter_u64_add(bbr_flows_whdwr_pacing, -1); 5519 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 5520 tcp_bbr_tso_size_check(bbr, cts); 5521 return; 5522 } 5523 rate = bbr_get_hardware_rate(bbr); 5524 nrte = tcp_chg_pacing_rate(bbr->r_ctl.crte, 5525 bbr->rc_tp, 5526 bbr->rc_inp->inp_route.ro_nh->nh_ifp, 5527 rate, 5528 (RS_PACING_GEQ|RS_PACING_SUB_OK), 5529 &error, NULL); 5530 if (nrte == NULL) { 5531 goto lost_rate; 5532 } 5533 if (nrte != bbr->r_ctl.crte) { 5534 bbr->r_ctl.crte = nrte; 5535 if (error == 0) { 5536 BBR_STAT_INC(bbr_hdwr_rl_mod_ok); 5537 if (bbr->r_ctl.crte->rate < rate) { 5538 /* We have a problem */ 5539 bbr_setup_less_of_rate(bbr, cts, 5540 bbr->r_ctl.crte->rate, rate); 5541 } else { 5542 /* We are good */ 5543 bbr->gain_is_limited = 0; 5544 bbr->skip_gain = 0; 5545 } 5546 } else { 5547 /* A failure should release the tag */ 5548 BBR_STAT_INC(bbr_hdwr_rl_mod_fail); 5549 bbr->gain_is_limited = 0; 5550 bbr->skip_gain = 0; 5551 bbr->bbr_hdrw_pacing = 0; 5552 } 5553 bbr_type_log_hdwr_pacing(bbr, 5554 bbr->r_ctl.crte->ptbl->rs_ifp, 5555 rate, 5556 ((bbr->r_ctl.crte == NULL) ? 0 : bbr->r_ctl.crte->rate), 5557 __LINE__, 5558 cts, 5559 error); 5560 } 5561 } 5562 5563 static void 5564 bbr_adjust_for_hw_pacing(struct tcp_bbr *bbr, uint32_t cts) 5565 { 5566 /* 5567 * If we have hardware pacing support 5568 * we need to factor that in for our 5569 * TSO size. 5570 */ 5571 const struct tcp_hwrate_limit_table *rlp; 5572 uint32_t cur_delay, seg_sz, maxseg, new_tso, delta, hdwr_delay; 5573 5574 if ((bbr->bbr_hdrw_pacing == 0) || 5575 (IN_RECOVERY(bbr->rc_tp->t_flags)) || 5576 (bbr->r_ctl.crte == NULL)) 5577 return; 5578 if (bbr->hw_pacing_set == 0) { 5579 /* Not yet by the hdwr pacing count delay */ 5580 return; 5581 } 5582 if (bbr_hdwr_pace_adjust == 0) { 5583 /* No adjustment */ 5584 return; 5585 } 5586 rlp = bbr->r_ctl.crte; 5587 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) 5588 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5589 else 5590 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5591 /* 5592 * So lets first get the 5593 * time we will take between 5594 * TSO sized sends currently without 5595 * hardware help. 5596 */ 5597 cur_delay = bbr_get_pacing_delay(bbr, BBR_UNIT, 5598 bbr->r_ctl.rc_pace_max_segs, cts, 1); 5599 hdwr_delay = bbr->r_ctl.rc_pace_max_segs / maxseg; 5600 hdwr_delay *= rlp->time_between; 5601 if (cur_delay > hdwr_delay) 5602 delta = cur_delay - hdwr_delay; 5603 else 5604 delta = 0; 5605 bbr_log_type_tsosize(bbr, cts, delta, cur_delay, hdwr_delay, 5606 (bbr->r_ctl.rc_pace_max_segs / maxseg), 5607 1); 5608 if (delta && 5609 (delta < (max(rlp->time_between, 5610 bbr->r_ctl.bbr_hptsi_segments_delay_tar)))) { 5611 /* 5612 * Now lets divide by the pacing 5613 * time between each segment the 5614 * hardware sends rounding up and 5615 * derive a bytes from that. We multiply 5616 * that by bbr_hdwr_pace_adjust to get 5617 * more bang for our buck. 5618 * 5619 * The goal is to have the software pacer 5620 * waiting no more than an additional 5621 * pacing delay if we can (without the 5622 * compensation i.e. x bbr_hdwr_pace_adjust). 5623 */ 5624 seg_sz = max(((cur_delay + rlp->time_between)/rlp->time_between), 5625 (bbr->r_ctl.rc_pace_max_segs/maxseg)); 5626 seg_sz *= bbr_hdwr_pace_adjust; 5627 if (bbr_hdwr_pace_floor && 5628 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5629 /* Currently hardware paces 5630 * out rs_min_seg segments at a time. 5631 * We need to make sure we always send at least 5632 * a full burst of bbr_hdwr_pace_floor down. 5633 */ 5634 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5635 } 5636 seg_sz *= maxseg; 5637 } else if (delta == 0) { 5638 /* 5639 * The highest pacing rate is 5640 * above our b/w gained. This means 5641 * we probably are going quite fast at 5642 * the hardware highest rate. Lets just multiply 5643 * the calculated TSO size by the 5644 * multiplier factor (its probably 5645 * 4 segments in the default config for 5646 * mlx). 5647 */ 5648 seg_sz = bbr->r_ctl.rc_pace_max_segs * bbr_hdwr_pace_adjust; 5649 if (bbr_hdwr_pace_floor && 5650 (seg_sz < bbr->r_ctl.crte->ptbl->rs_min_seg)) { 5651 /* Currently hardware paces 5652 * out rs_min_seg segments at a time. 5653 * We need to make sure we always send at least 5654 * a full burst of bbr_hdwr_pace_floor down. 5655 */ 5656 seg_sz = bbr->r_ctl.crte->ptbl->rs_min_seg; 5657 } 5658 } else { 5659 /* 5660 * The pacing time difference is so 5661 * big that the hardware will 5662 * pace out more rapidly then we 5663 * really want and then we 5664 * will have a long delay. Lets just keep 5665 * the same TSO size so its as if 5666 * we were not using hdwr pacing (we 5667 * just gain a bit of spacing from the 5668 * hardware if seg_sz > 1). 5669 */ 5670 seg_sz = bbr->r_ctl.rc_pace_max_segs; 5671 } 5672 if (seg_sz > bbr->r_ctl.rc_pace_max_segs) 5673 new_tso = seg_sz; 5674 else 5675 new_tso = bbr->r_ctl.rc_pace_max_segs; 5676 if (new_tso >= (PACE_MAX_IP_BYTES-maxseg)) 5677 new_tso = PACE_MAX_IP_BYTES - maxseg; 5678 5679 if (new_tso != bbr->r_ctl.rc_pace_max_segs) { 5680 bbr_log_type_tsosize(bbr, cts, new_tso, 0, bbr->r_ctl.rc_pace_max_segs, maxseg, 0); 5681 bbr->r_ctl.rc_pace_max_segs = new_tso; 5682 } 5683 } 5684 5685 static void 5686 tcp_bbr_tso_size_check(struct tcp_bbr *bbr, uint32_t cts) 5687 { 5688 uint64_t bw; 5689 uint32_t old_tso = 0, new_tso; 5690 uint32_t maxseg, bytes; 5691 uint32_t tls_seg=0; 5692 /* 5693 * Google/linux uses the following algorithm to determine 5694 * the TSO size based on the b/w of the link (from Neal Cardwell email 9/27/18): 5695 * 5696 * bytes = bw_in_bytes_per_second / 1000 5697 * bytes = min(bytes, 64k) 5698 * tso_segs = bytes / MSS 5699 * if (bw < 1.2Mbs) 5700 * min_tso_segs = 1 5701 * else 5702 * min_tso_segs = 2 5703 * tso_segs = max(tso_segs, min_tso_segs) 5704 * 5705 * * Note apply a device specific limit (we apply this in the 5706 * tcp_m_copym). 5707 * Note that before the initial measurement is made google bursts out 5708 * a full iwnd just like new-reno/cubic. 5709 * 5710 * We do not use this algorithm. Instead we 5711 * use a two phased approach: 5712 * 5713 * if ( bw <= per-tcb-cross-over) 5714 * goal_tso = calculate how much with this bw we 5715 * can send in goal-time seconds. 5716 * if (goal_tso > mss) 5717 * seg = goal_tso / mss 5718 * tso = seg * mss 5719 * else 5720 * tso = mss 5721 * if (tso > per-tcb-max) 5722 * tso = per-tcb-max 5723 * else if ( bw > 512Mbps) 5724 * tso = max-tso (64k/mss) 5725 * else 5726 * goal_tso = bw / per-tcb-divsor 5727 * seg = (goal_tso + mss-1)/mss 5728 * tso = seg * mss 5729 * 5730 * if (tso < per-tcb-floor) 5731 * tso = per-tcb-floor 5732 * if (tso > per-tcb-utter_max) 5733 * tso = per-tcb-utter_max 5734 * 5735 * Note the default per-tcb-divisor is 1000 (same as google). 5736 * the goal cross over is 30Mbps however. To recreate googles 5737 * algorithm you need to set: 5738 * 5739 * cross-over = 23,168,000 bps 5740 * goal-time = 18000 5741 * per-tcb-max = 2 5742 * per-tcb-divisor = 1000 5743 * per-tcb-floor = 1 5744 * 5745 * This will get you "google bbr" behavior with respect to tso size. 5746 * 5747 * Note we do set anything TSO size until we are past the initial 5748 * window. Before that we gnerally use either a single MSS 5749 * or we use the full IW size (so we burst a IW at a time) 5750 */ 5751 5752 if (bbr->rc_tp->t_maxseg > bbr->rc_last_options) { 5753 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5754 } else { 5755 maxseg = BBR_MIN_SEG - bbr->rc_last_options; 5756 } 5757 old_tso = bbr->r_ctl.rc_pace_max_segs; 5758 if (bbr->rc_past_init_win == 0) { 5759 /* 5760 * Not enough data has been acknowledged to make a 5761 * judgement. Set up the initial TSO based on if we 5762 * are sending a full IW at once or not. 5763 */ 5764 if (bbr->rc_use_google) 5765 bbr->r_ctl.rc_pace_max_segs = ((bbr->rc_tp->t_maxseg - bbr->rc_last_options) * 2); 5766 else if (bbr->bbr_init_win_cheat) 5767 bbr->r_ctl.rc_pace_max_segs = bbr_initial_cwnd(bbr, bbr->rc_tp); 5768 else 5769 bbr->r_ctl.rc_pace_max_segs = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 5770 if (bbr->r_ctl.rc_pace_min_segs != bbr->rc_tp->t_maxseg) 5771 bbr->r_ctl.rc_pace_min_segs = bbr->rc_tp->t_maxseg; 5772 if (bbr->r_ctl.rc_pace_max_segs == 0) { 5773 bbr->r_ctl.rc_pace_max_segs = maxseg; 5774 } 5775 bbr_log_type_tsosize(bbr, cts, bbr->r_ctl.rc_pace_max_segs, tls_seg, old_tso, maxseg, 0); 5776 bbr_adjust_for_hw_pacing(bbr, cts); 5777 return; 5778 } 5779 /** 5780 * Now lets set the TSO goal based on our delivery rate in 5781 * bytes per second. Note we only do this if 5782 * we have acked at least the initial cwnd worth of data. 5783 */ 5784 bw = bbr_get_bw(bbr); 5785 if (IN_RECOVERY(bbr->rc_tp->t_flags) && 5786 (bbr->rc_use_google == 0)) { 5787 /* We clamp to one MSS in recovery */ 5788 new_tso = maxseg; 5789 } else if (bbr->rc_use_google) { 5790 int min_tso_segs; 5791 5792 /* Google considers the gain too */ 5793 if (bbr->r_ctl.rc_bbr_hptsi_gain != BBR_UNIT) { 5794 bw *= bbr->r_ctl.rc_bbr_hptsi_gain; 5795 bw /= BBR_UNIT; 5796 } 5797 bytes = bw / 1024; 5798 if (bytes > (64 * 1024)) 5799 bytes = 64 * 1024; 5800 new_tso = bytes / maxseg; 5801 if (bw < ONE_POINT_TWO_MEG) 5802 min_tso_segs = 1; 5803 else 5804 min_tso_segs = 2; 5805 if (new_tso < min_tso_segs) 5806 new_tso = min_tso_segs; 5807 new_tso *= maxseg; 5808 } else if (bbr->rc_no_pacing) { 5809 new_tso = (PACE_MAX_IP_BYTES / maxseg) * maxseg; 5810 } else if (bw <= bbr->r_ctl.bbr_cross_over) { 5811 /* 5812 * Calculate the worse case b/w TSO if we are inserting no 5813 * more than a delay_target number of TSO's. 5814 */ 5815 uint32_t tso_len, min_tso; 5816 5817 tso_len = bbr_get_pacing_length(bbr, BBR_UNIT, bbr->r_ctl.bbr_hptsi_segments_delay_tar, bw); 5818 if (tso_len > maxseg) { 5819 new_tso = tso_len / maxseg; 5820 if (new_tso > bbr->r_ctl.bbr_hptsi_segments_max) 5821 new_tso = bbr->r_ctl.bbr_hptsi_segments_max; 5822 new_tso *= maxseg; 5823 } else { 5824 /* 5825 * less than a full sized frame yikes.. long rtt or 5826 * low bw? 5827 */ 5828 min_tso = bbr_minseg(bbr); 5829 if ((tso_len > min_tso) && (bbr_all_get_min == 0)) 5830 new_tso = rounddown(tso_len, min_tso); 5831 else 5832 new_tso = min_tso; 5833 } 5834 } else if (bw > FIVETWELVE_MBPS) { 5835 /* 5836 * This guy is so fast b/w wise that we can TSO as large as 5837 * possible of segments that the NIC will allow. 5838 */ 5839 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5840 } else { 5841 /* 5842 * This formula is based on attempting to send a segment or 5843 * more every bbr_hptsi_per_second. The default is 1000 5844 * which means you are targeting what you can send every 1ms 5845 * based on the peers bw. 5846 * 5847 * If the number drops to say 500, then you are looking more 5848 * at 2ms and you will raise how much we send in a single 5849 * TSO thus saving CPU (less bbr_output_wtime() calls). The 5850 * trade off of course is you will send more at once and 5851 * thus tend to clump up the sends into larger "bursts" 5852 * building a queue. 5853 */ 5854 bw /= bbr->r_ctl.bbr_hptsi_per_second; 5855 new_tso = roundup(bw, (uint64_t)maxseg); 5856 /* 5857 * Gate the floor to match what our lower than 48Mbps 5858 * algorithm does. The ceiling (bbr_hptsi_segments_max) thus 5859 * becomes the floor for this calculation. 5860 */ 5861 if (new_tso < (bbr->r_ctl.bbr_hptsi_segments_max * maxseg)) 5862 new_tso = (bbr->r_ctl.bbr_hptsi_segments_max * maxseg); 5863 } 5864 if (bbr->r_ctl.bbr_hptsi_segments_floor && (new_tso < (maxseg * bbr->r_ctl.bbr_hptsi_segments_floor))) 5865 new_tso = maxseg * bbr->r_ctl.bbr_hptsi_segments_floor; 5866 if (new_tso > PACE_MAX_IP_BYTES) 5867 new_tso = rounddown(PACE_MAX_IP_BYTES, maxseg); 5868 /* Enforce an utter maximum. */ 5869 if (bbr->r_ctl.bbr_utter_max && (new_tso > (bbr->r_ctl.bbr_utter_max * maxseg))) { 5870 new_tso = bbr->r_ctl.bbr_utter_max * maxseg; 5871 } 5872 if (old_tso != new_tso) { 5873 /* Only log changes */ 5874 bbr_log_type_tsosize(bbr, cts, new_tso, tls_seg, old_tso, maxseg, 0); 5875 bbr->r_ctl.rc_pace_max_segs = new_tso; 5876 } 5877 /* We have hardware pacing! */ 5878 bbr_adjust_for_hw_pacing(bbr, cts); 5879 } 5880 5881 static void 5882 bbr_log_output(struct tcp_bbr *bbr, struct tcpcb *tp, struct tcpopt *to, int32_t len, 5883 uint32_t seq_out, uint16_t th_flags, int32_t err, uint32_t cts, 5884 struct mbuf *mb, int32_t * abandon, struct bbr_sendmap *hintrsm, uint32_t delay_calc, 5885 struct sockbuf *sb) 5886 { 5887 5888 struct bbr_sendmap *rsm, *nrsm; 5889 register uint32_t snd_max, snd_una; 5890 uint32_t pacing_time; 5891 /* 5892 * Add to the RACK log of packets in flight or retransmitted. If 5893 * there is a TS option we will use the TS echoed, if not we will 5894 * grab a TS. 5895 * 5896 * Retransmissions will increment the count and move the ts to its 5897 * proper place. Note that if options do not include TS's then we 5898 * won't be able to effectively use the ACK for an RTT on a retran. 5899 * 5900 * Notes about r_start and r_end. Lets consider a send starting at 5901 * sequence 1 for 10 bytes. In such an example the r_start would be 5902 * 1 (starting sequence) but the r_end would be r_start+len i.e. 11. 5903 * This means that r_end is actually the first sequence for the next 5904 * slot (11). 5905 * 5906 */ 5907 INP_WLOCK_ASSERT(tptoinpcb(tp)); 5908 if (err) { 5909 /* 5910 * We don't log errors -- we could but snd_max does not 5911 * advance in this case either. 5912 */ 5913 return; 5914 } 5915 if (th_flags & TH_RST) { 5916 /* 5917 * We don't log resets and we return immediately from 5918 * sending 5919 */ 5920 *abandon = 1; 5921 return; 5922 } 5923 snd_una = tp->snd_una; 5924 if (th_flags & (TH_SYN | TH_FIN) && (hintrsm == NULL)) { 5925 /* 5926 * The call to bbr_log_output is made before bumping 5927 * snd_max. This means we can record one extra byte on a SYN 5928 * or FIN if seq_out is adding more on and a FIN is present 5929 * (and we are not resending). 5930 */ 5931 if ((th_flags & TH_SYN) && (tp->iss == seq_out)) 5932 len++; 5933 if (th_flags & TH_FIN) 5934 len++; 5935 } 5936 if (SEQ_LEQ((seq_out + len), snd_una)) { 5937 /* Are sending an old segment to induce an ack (keep-alive)? */ 5938 return; 5939 } 5940 if (SEQ_LT(seq_out, snd_una)) { 5941 /* huh? should we panic? */ 5942 uint32_t end; 5943 5944 end = seq_out + len; 5945 seq_out = snd_una; 5946 len = end - seq_out; 5947 } 5948 snd_max = tp->snd_max; 5949 if (len == 0) { 5950 /* We don't log zero window probes */ 5951 return; 5952 } 5953 pacing_time = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, len, cts, 1); 5954 /* First question is it a retransmission? */ 5955 if (seq_out == snd_max) { 5956 again: 5957 rsm = bbr_alloc(bbr); 5958 if (rsm == NULL) { 5959 return; 5960 } 5961 rsm->r_flags = 0; 5962 if (th_flags & TH_SYN) 5963 rsm->r_flags |= BBR_HAS_SYN; 5964 if (th_flags & TH_FIN) 5965 rsm->r_flags |= BBR_HAS_FIN; 5966 rsm->r_tim_lastsent[0] = cts; 5967 rsm->r_rtr_cnt = 1; 5968 rsm->r_rtr_bytes = 0; 5969 rsm->r_start = seq_out; 5970 rsm->r_end = rsm->r_start + len; 5971 rsm->r_dupack = 0; 5972 rsm->r_delivered = bbr->r_ctl.rc_delivered; 5973 rsm->r_pacing_delay = pacing_time; 5974 rsm->r_ts_valid = bbr->rc_ts_valid; 5975 if (bbr->rc_ts_valid) 5976 rsm->r_del_ack_ts = bbr->r_ctl.last_inbound_ts; 5977 rsm->r_del_time = bbr->r_ctl.rc_del_time; 5978 if (bbr->r_ctl.r_app_limited_until) 5979 rsm->r_app_limited = 1; 5980 else 5981 rsm->r_app_limited = 0; 5982 rsm->r_first_sent_time = bbr_get_earliest_send_outstanding(bbr, rsm, cts); 5983 rsm->r_flight_at_send = ctf_flight_size(bbr->rc_tp, 5984 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 5985 /* 5986 * Here we must also add in this rsm since snd_max 5987 * is updated after we return from a new send. 5988 */ 5989 rsm->r_flight_at_send += len; 5990 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 5991 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 5992 rsm->r_in_tmap = 1; 5993 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 5994 rsm->r_bbr_state = bbr_state_val(bbr); 5995 else 5996 rsm->r_bbr_state = 8; 5997 if (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT) { 5998 rsm->r_is_gain = 1; 5999 rsm->r_is_drain = 0; 6000 } else if (bbr->r_ctl.rc_bbr_hptsi_gain < BBR_UNIT) { 6001 rsm->r_is_drain = 1; 6002 rsm->r_is_gain = 0; 6003 } else { 6004 rsm->r_is_drain = 0; 6005 rsm->r_is_gain = 0; 6006 } 6007 return; 6008 } 6009 /* 6010 * If we reach here its a retransmission and we need to find it. 6011 */ 6012 more: 6013 if (hintrsm && (hintrsm->r_start == seq_out)) { 6014 rsm = hintrsm; 6015 hintrsm = NULL; 6016 } else if (bbr->r_ctl.rc_next) { 6017 /* We have a hint from a previous run */ 6018 rsm = bbr->r_ctl.rc_next; 6019 } else { 6020 /* No hints sorry */ 6021 rsm = NULL; 6022 } 6023 if ((rsm) && (rsm->r_start == seq_out)) { 6024 /* 6025 * We used rc_next or hintrsm to retransmit, hopefully the 6026 * likely case. 6027 */ 6028 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6029 if (len == 0) { 6030 return; 6031 } else { 6032 goto more; 6033 } 6034 } 6035 /* Ok it was not the last pointer go through it the hard way. */ 6036 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6037 if (rsm->r_start == seq_out) { 6038 seq_out = bbr_update_entry(tp, bbr, rsm, cts, &len, pacing_time); 6039 bbr->r_ctl.rc_next = TAILQ_NEXT(rsm, r_next); 6040 if (len == 0) { 6041 return; 6042 } else { 6043 continue; 6044 } 6045 } 6046 if (SEQ_GEQ(seq_out, rsm->r_start) && SEQ_LT(seq_out, rsm->r_end)) { 6047 /* Transmitted within this piece */ 6048 /* 6049 * Ok we must split off the front and then let the 6050 * update do the rest 6051 */ 6052 nrsm = bbr_alloc_full_limit(bbr); 6053 if (nrsm == NULL) { 6054 bbr_update_rsm(tp, bbr, rsm, cts, pacing_time); 6055 return; 6056 } 6057 /* 6058 * copy rsm to nrsm and then trim the front of rsm 6059 * to not include this part. 6060 */ 6061 bbr_clone_rsm(bbr, nrsm, rsm, seq_out); 6062 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 6063 if (rsm->r_in_tmap) { 6064 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 6065 nrsm->r_in_tmap = 1; 6066 } 6067 rsm->r_flags &= (~BBR_HAS_FIN); 6068 seq_out = bbr_update_entry(tp, bbr, nrsm, cts, &len, pacing_time); 6069 if (len == 0) { 6070 return; 6071 } 6072 } 6073 } 6074 /* 6075 * Hmm not found in map did they retransmit both old and on into the 6076 * new? 6077 */ 6078 if (seq_out == tp->snd_max) { 6079 goto again; 6080 } else if (SEQ_LT(seq_out, tp->snd_max)) { 6081 #ifdef BBR_INVARIANTS 6082 printf("seq_out:%u len:%d snd_una:%u snd_max:%u -- but rsm not found?\n", 6083 seq_out, len, tp->snd_una, tp->snd_max); 6084 printf("Starting Dump of all rack entries\n"); 6085 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 6086 printf("rsm:%p start:%u end:%u\n", 6087 rsm, rsm->r_start, rsm->r_end); 6088 } 6089 printf("Dump complete\n"); 6090 panic("seq_out not found rack:%p tp:%p", 6091 bbr, tp); 6092 #endif 6093 } else { 6094 #ifdef BBR_INVARIANTS 6095 /* 6096 * Hmm beyond sndmax? (only if we are using the new rtt-pack 6097 * flag) 6098 */ 6099 panic("seq_out:%u(%d) is beyond snd_max:%u tp:%p", 6100 seq_out, len, tp->snd_max, tp); 6101 #endif 6102 } 6103 } 6104 6105 static void 6106 bbr_collapse_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, int32_t rtt) 6107 { 6108 /* 6109 * Collapse timeout back the cum-ack moved. 6110 */ 6111 tp->t_rxtshift = 0; 6112 tp->t_softerror = 0; 6113 } 6114 6115 static void 6116 tcp_bbr_xmit_timer(struct tcp_bbr *bbr, uint32_t rtt_usecs, uint32_t rsm_send_time, uint32_t r_start, uint32_t tsin) 6117 { 6118 bbr->rtt_valid = 1; 6119 bbr->r_ctl.cur_rtt = rtt_usecs; 6120 bbr->r_ctl.ts_in = tsin; 6121 if (rsm_send_time) 6122 bbr->r_ctl.cur_rtt_send_time = rsm_send_time; 6123 } 6124 6125 static void 6126 bbr_make_timestamp_determination(struct tcp_bbr *bbr) 6127 { 6128 /** 6129 * We have in our bbr control: 6130 * 1) The timestamp we started observing cum-acks (bbr->r_ctl.bbr_ts_check_tstmp). 6131 * 2) Our timestamp indicating when we sent that packet (bbr->r_ctl.rsm->bbr_ts_check_our_cts). 6132 * 3) The current timestamp that just came in (bbr->r_ctl.last_inbound_ts) 6133 * 4) The time that the packet that generated that ack was sent (bbr->r_ctl.cur_rtt_send_time) 6134 * 6135 * Now we can calculate the time between the sends by doing: 6136 * 6137 * delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts 6138 * 6139 * And the peer's time between receiving them by doing: 6140 * 6141 * peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp 6142 * 6143 * We want to figure out if the timestamp values are in msec, 10msec or usec. 6144 * We also may find that we can't use the timestamps if say we see 6145 * that the peer_delta indicates that though we may have taken 10ms to 6146 * pace out the data, it only saw 1ms between the two packets. This would 6147 * indicate that somewhere on the path is a batching entity that is giving 6148 * out time-slices of the actual b/w. This would mean we could not use 6149 * reliably the peers timestamps. 6150 * 6151 * We expect delta > peer_delta initially. Until we figure out the 6152 * timestamp difference which we will store in bbr->r_ctl.bbr_peer_tsratio. 6153 * If we place 1000 there then its a ms vs our usec. If we place 10000 there 6154 * then its 10ms vs our usec. If the peer is running a usec clock we would 6155 * put a 1 there. If the value is faster then ours, we will disable the 6156 * use of timestamps (though we could revist this later if we find it to be not 6157 * just an isolated one or two flows)). 6158 * 6159 * To detect the batching middle boxes we will come up with our compensation and 6160 * if with it in place, we find the peer is drastically off (by some margin) in 6161 * the smaller direction, then we will assume the worst case and disable use of timestamps. 6162 * 6163 */ 6164 uint64_t delta, peer_delta, delta_up; 6165 6166 delta = bbr->r_ctl.cur_rtt_send_time - bbr->r_ctl.bbr_ts_check_our_cts; 6167 if (delta < bbr_min_usec_delta) { 6168 /* 6169 * Have not seen a min amount of time 6170 * between our send times so we can 6171 * make a determination of the timestamp 6172 * yet. 6173 */ 6174 return; 6175 } 6176 peer_delta = bbr->r_ctl.last_inbound_ts - bbr->r_ctl.bbr_ts_check_tstmp; 6177 if (peer_delta < bbr_min_peer_delta) { 6178 /* 6179 * We may have enough in the form of 6180 * our delta but the peers number 6181 * has not changed that much. It could 6182 * be its clock ratio is such that 6183 * we need more data (10ms tick) or 6184 * there may be other compression scenarios 6185 * going on. In any event we need the 6186 * spread to be larger. 6187 */ 6188 return; 6189 } 6190 /* Ok lets first see which way our delta is going */ 6191 if (peer_delta > delta) { 6192 /* Very unlikely, the peer without 6193 * compensation shows that it saw 6194 * the two sends arrive further apart 6195 * then we saw then in micro-seconds. 6196 */ 6197 if (peer_delta < (delta + ((delta * (uint64_t)1000)/ (uint64_t)bbr_delta_percent))) { 6198 /* well it looks like the peer is a micro-second clock. */ 6199 bbr->rc_ts_clock_set = 1; 6200 bbr->r_ctl.bbr_peer_tsratio = 1; 6201 } else { 6202 bbr->rc_ts_cant_be_used = 1; 6203 bbr->rc_ts_clock_set = 1; 6204 } 6205 return; 6206 } 6207 /* Ok we know that the peer_delta is smaller than our send distance */ 6208 bbr->rc_ts_clock_set = 1; 6209 /* First question is it within the percentage that they are using usec time? */ 6210 delta_up = (peer_delta * 1000) / (uint64_t)bbr_delta_percent; 6211 if ((peer_delta + delta_up) >= delta) { 6212 /* Its a usec clock */ 6213 bbr->r_ctl.bbr_peer_tsratio = 1; 6214 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6215 return; 6216 } 6217 /* Ok if not usec, what about 10usec (though unlikely)? */ 6218 delta_up = (peer_delta * 1000 * 10) / (uint64_t)bbr_delta_percent; 6219 if (((peer_delta * 10) + delta_up) >= delta) { 6220 bbr->r_ctl.bbr_peer_tsratio = 10; 6221 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6222 return; 6223 } 6224 /* And what about 100usec (though again unlikely)? */ 6225 delta_up = (peer_delta * 1000 * 100) / (uint64_t)bbr_delta_percent; 6226 if (((peer_delta * 100) + delta_up) >= delta) { 6227 bbr->r_ctl.bbr_peer_tsratio = 100; 6228 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6229 return; 6230 } 6231 /* And how about 1 msec (the most likely one)? */ 6232 delta_up = (peer_delta * 1000 * 1000) / (uint64_t)bbr_delta_percent; 6233 if (((peer_delta * 1000) + delta_up) >= delta) { 6234 bbr->r_ctl.bbr_peer_tsratio = 1000; 6235 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6236 return; 6237 } 6238 /* Ok if not msec could it be 10 msec? */ 6239 delta_up = (peer_delta * 1000 * 10000) / (uint64_t)bbr_delta_percent; 6240 if (((peer_delta * 10000) + delta_up) >= delta) { 6241 bbr->r_ctl.bbr_peer_tsratio = 10000; 6242 return; 6243 } 6244 /* If we fall down here the clock tick so slowly we can't use it */ 6245 bbr->rc_ts_cant_be_used = 1; 6246 bbr->r_ctl.bbr_peer_tsratio = 0; 6247 bbr_log_tstmp_validation(bbr, peer_delta, delta); 6248 } 6249 6250 /* 6251 * Collect new round-trip time estimate 6252 * and update averages and current timeout. 6253 */ 6254 static void 6255 tcp_bbr_xmit_timer_commit(struct tcp_bbr *bbr, struct tcpcb *tp, uint32_t cts) 6256 { 6257 int32_t delta; 6258 uint32_t rtt, tsin; 6259 int32_t rtt_ticks; 6260 6261 if (bbr->rtt_valid == 0) 6262 /* No valid sample */ 6263 return; 6264 6265 rtt = bbr->r_ctl.cur_rtt; 6266 tsin = bbr->r_ctl.ts_in; 6267 if (bbr->rc_prtt_set_ts) { 6268 /* 6269 * We are to force feed the rttProp filter due 6270 * to an entry into PROBE_RTT. This assures 6271 * that the times are sync'd between when we 6272 * go into PROBE_RTT and the filter expiration. 6273 * 6274 * Google does not use a true filter, so they do 6275 * this implicitly since they only keep one value 6276 * and when they enter probe-rtt they update the 6277 * value to the newest rtt. 6278 */ 6279 uint32_t rtt_prop; 6280 6281 bbr->rc_prtt_set_ts = 0; 6282 rtt_prop = get_filter_value_small(&bbr->r_ctl.rc_rttprop); 6283 if (rtt > rtt_prop) 6284 filter_increase_by_small(&bbr->r_ctl.rc_rttprop, (rtt - rtt_prop), cts); 6285 else 6286 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6287 } 6288 #ifdef STATS 6289 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_PATHRTT, imax(0, rtt)); 6290 #endif 6291 if (bbr->rc_ack_was_delayed) 6292 rtt += bbr->r_ctl.rc_ack_hdwr_delay; 6293 6294 if (rtt < bbr->r_ctl.rc_lowest_rtt) 6295 bbr->r_ctl.rc_lowest_rtt = rtt; 6296 bbr_log_rtt_sample(bbr, rtt, tsin); 6297 if (bbr->r_init_rtt) { 6298 /* 6299 * The initial rtt is not-trusted, nuke it and lets get 6300 * our first valid measurement in. 6301 */ 6302 bbr->r_init_rtt = 0; 6303 tp->t_srtt = 0; 6304 } 6305 if ((bbr->rc_ts_clock_set == 0) && bbr->rc_ts_valid) { 6306 /* 6307 * So we have not yet figured out 6308 * what the peers TSTMP value is 6309 * in (most likely ms). We need a 6310 * series of cum-ack's to determine 6311 * this reliably. 6312 */ 6313 if (bbr->rc_ack_is_cumack) { 6314 if (bbr->rc_ts_data_set) { 6315 /* Lets attempt to determine the timestamp granularity. */ 6316 bbr_make_timestamp_determination(bbr); 6317 } else { 6318 bbr->rc_ts_data_set = 1; 6319 bbr->r_ctl.bbr_ts_check_tstmp = bbr->r_ctl.last_inbound_ts; 6320 bbr->r_ctl.bbr_ts_check_our_cts = bbr->r_ctl.cur_rtt_send_time; 6321 } 6322 } else { 6323 /* 6324 * We have to have consecutive acks 6325 * reset any "filled" state to none. 6326 */ 6327 bbr->rc_ts_data_set = 0; 6328 } 6329 } 6330 /* Round it up */ 6331 rtt_ticks = USEC_2_TICKS((rtt + (USECS_IN_MSEC - 1))); 6332 if (tp->t_srtt != 0) { 6333 /* 6334 * srtt is stored as fixed point with 5 bits after the 6335 * binary point (i.e., scaled by 8). The following magic is 6336 * equivalent to the smoothing algorithm in rfc793 with an 6337 * alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed point). 6338 * Adjust rtt to origin 0. 6339 */ 6340 6341 delta = ((rtt_ticks - 1) << TCP_DELTA_SHIFT) 6342 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 6343 6344 tp->t_srtt += delta; 6345 if (tp->t_srtt <= 0) 6346 tp->t_srtt = 1; 6347 6348 /* 6349 * We accumulate a smoothed rtt variance (actually, a 6350 * smoothed mean difference), then set the retransmit timer 6351 * to smoothed rtt + 4 times the smoothed variance. rttvar 6352 * is stored as fixed point with 4 bits after the binary 6353 * point (scaled by 16). The following is equivalent to 6354 * rfc793 smoothing with an alpha of .75 (rttvar = 6355 * rttvar*3/4 + |delta| / 4). This replaces rfc793's 6356 * wired-in beta. 6357 */ 6358 if (delta < 0) 6359 delta = -delta; 6360 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 6361 tp->t_rttvar += delta; 6362 if (tp->t_rttvar <= 0) 6363 tp->t_rttvar = 1; 6364 } else { 6365 /* 6366 * No rtt measurement yet - use the unsmoothed rtt. Set the 6367 * variance to half the rtt (so our first retransmit happens 6368 * at 3*rtt). 6369 */ 6370 tp->t_srtt = rtt_ticks << TCP_RTT_SHIFT; 6371 tp->t_rttvar = rtt_ticks << (TCP_RTTVAR_SHIFT - 1); 6372 } 6373 KMOD_TCPSTAT_INC(tcps_rttupdated); 6374 if (tp->t_rttupdated < UCHAR_MAX) 6375 tp->t_rttupdated++; 6376 #ifdef STATS 6377 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, imax(0, rtt_ticks)); 6378 #endif 6379 /* 6380 * the retransmit should happen at rtt + 4 * rttvar. Because of the 6381 * way we do the smoothing, srtt and rttvar will each average +1/2 6382 * tick of bias. When we compute the retransmit timer, we want 1/2 6383 * tick of rounding and 1 extra tick because of +-1/2 tick 6384 * uncertainty in the firing of the timer. The bias will give us 6385 * exactly the 1.5 tick we need. But, because the bias is 6386 * statistical, we have to test that we don't drop below the minimum 6387 * feasible timer (which is 2 ticks). 6388 */ 6389 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 6390 max(MSEC_2_TICKS(bbr->r_ctl.rc_min_rto_ms), rtt_ticks + 2), 6391 MSEC_2_TICKS(((uint32_t)bbr->rc_max_rto_sec) * 1000)); 6392 6393 /* 6394 * We received an ack for a packet that wasn't retransmitted; it is 6395 * probably safe to discard any error indications we've received 6396 * recently. This isn't quite right, but close enough for now (a 6397 * route might have failed after we sent a segment, and the return 6398 * path might not be symmetrical). 6399 */ 6400 tp->t_softerror = 0; 6401 rtt = (TICKS_2_USEC(bbr->rc_tp->t_srtt) >> TCP_RTT_SHIFT); 6402 if (bbr->r_ctl.bbr_smallest_srtt_this_state > rtt) 6403 bbr->r_ctl.bbr_smallest_srtt_this_state = rtt; 6404 } 6405 6406 static void 6407 bbr_set_reduced_rtt(struct tcp_bbr *bbr, uint32_t cts, uint32_t line) 6408 { 6409 bbr->r_ctl.rc_rtt_shrinks = cts; 6410 if (bbr_can_force_probertt && 6411 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 6412 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 6413 /* 6414 * We should enter probe-rtt its been too long 6415 * since we have been there. 6416 */ 6417 bbr_enter_probe_rtt(bbr, cts, __LINE__); 6418 } else 6419 bbr_check_probe_rtt_limits(bbr, cts); 6420 } 6421 6422 static void 6423 tcp_bbr_commit_bw(struct tcp_bbr *bbr, uint32_t cts) 6424 { 6425 uint64_t orig_bw; 6426 6427 if (bbr->r_ctl.rc_bbr_cur_del_rate == 0) { 6428 /* We never apply a zero measurement */ 6429 bbr_log_type_bbrupd(bbr, 20, cts, 0, 0, 6430 0, 0, 0, 0, 0, 0); 6431 return; 6432 } 6433 if (bbr->r_ctl.r_measurement_count < 0xffffffff) 6434 bbr->r_ctl.r_measurement_count++; 6435 orig_bw = get_filter_value(&bbr->r_ctl.rc_delrate); 6436 apply_filter_max(&bbr->r_ctl.rc_delrate, bbr->r_ctl.rc_bbr_cur_del_rate, bbr->r_ctl.rc_pkt_epoch); 6437 bbr_log_type_bbrupd(bbr, 21, cts, (uint32_t)orig_bw, 6438 (uint32_t)get_filter_value(&bbr->r_ctl.rc_delrate), 6439 0, 0, 0, 0, 0, 0); 6440 if (orig_bw && 6441 (orig_bw != get_filter_value(&bbr->r_ctl.rc_delrate))) { 6442 if (bbr->bbr_hdrw_pacing) { 6443 /* 6444 * Apply a new rate to the hardware 6445 * possibly. 6446 */ 6447 bbr_update_hardware_pacing_rate(bbr, cts); 6448 } 6449 bbr_set_state_target(bbr, __LINE__); 6450 tcp_bbr_tso_size_check(bbr, cts); 6451 if (bbr->r_recovery_bw) { 6452 bbr_setup_red_bw(bbr, cts); 6453 bbr_log_type_bw_reduce(bbr, BBR_RED_BW_USELRBW); 6454 } 6455 } else if ((orig_bw == 0) && get_filter_value(&bbr->r_ctl.rc_delrate)) 6456 tcp_bbr_tso_size_check(bbr, cts); 6457 } 6458 6459 static void 6460 bbr_nf_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6461 { 6462 if (bbr->rc_in_persist == 0) { 6463 /* We log only when not in persist */ 6464 /* Translate to a Bytes Per Second */ 6465 uint64_t tim, bw, ts_diff, ts_bw; 6466 uint32_t delivered; 6467 6468 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6469 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6470 else 6471 tim = 1; 6472 /* 6473 * Now that we have processed the tim (skipping the sample 6474 * or possibly updating the time, go ahead and 6475 * calculate the cdr. 6476 */ 6477 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6478 bw = (uint64_t)delivered; 6479 bw *= (uint64_t)USECS_IN_SECOND; 6480 bw /= tim; 6481 if (bw == 0) { 6482 /* We must have a calculatable amount */ 6483 return; 6484 } 6485 /* 6486 * If we are using this b/w shove it in now so we 6487 * can see in the trace viewer if it gets over-ridden. 6488 */ 6489 if (rsm->r_ts_valid && 6490 bbr->rc_ts_valid && 6491 bbr->rc_ts_clock_set && 6492 (bbr->rc_ts_cant_be_used == 0) && 6493 bbr->rc_use_ts_limit) { 6494 ts_diff = max((bbr->r_ctl.last_inbound_ts - rsm->r_del_ack_ts), 1); 6495 ts_diff *= bbr->r_ctl.bbr_peer_tsratio; 6496 if ((delivered == 0) || 6497 (rtt < 1000)) { 6498 /* Can't use the ts */ 6499 bbr_log_type_bbrupd(bbr, 61, cts, 6500 ts_diff, 6501 bbr->r_ctl.last_inbound_ts, 6502 rsm->r_del_ack_ts, 0, 6503 0, 0, 0, delivered); 6504 } else { 6505 ts_bw = (uint64_t)delivered; 6506 ts_bw *= (uint64_t)USECS_IN_SECOND; 6507 ts_bw /= ts_diff; 6508 bbr_log_type_bbrupd(bbr, 62, cts, 6509 (ts_bw >> 32), 6510 (ts_bw & 0xffffffff), 0, 0, 6511 0, 0, ts_diff, delivered); 6512 if ((bbr->ts_can_raise) && 6513 (ts_bw > bw)) { 6514 bbr_log_type_bbrupd(bbr, 8, cts, 6515 delivered, 6516 ts_diff, 6517 (bw >> 32), 6518 (bw & 0x00000000ffffffff), 6519 0, 0, 0, 0); 6520 bw = ts_bw; 6521 } else if (ts_bw && (ts_bw < bw)) { 6522 bbr_log_type_bbrupd(bbr, 7, cts, 6523 delivered, 6524 ts_diff, 6525 (bw >> 32), 6526 (bw & 0x00000000ffffffff), 6527 0, 0, 0, 0); 6528 bw = ts_bw; 6529 } 6530 } 6531 } 6532 if (rsm->r_first_sent_time && 6533 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6534 uint64_t sbw, sti; 6535 /* 6536 * We use what was in flight at the time of our 6537 * send and the size of this send to figure 6538 * out what we have been sending at (amount). 6539 * For the time we take from the time of 6540 * the send of the first send outstanding 6541 * until this send plus this sends pacing 6542 * time. This gives us a good calculation 6543 * as to the rate we have been sending at. 6544 */ 6545 6546 sbw = (uint64_t)(rsm->r_flight_at_send); 6547 sbw *= (uint64_t)USECS_IN_SECOND; 6548 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6549 sti += rsm->r_pacing_delay; 6550 sbw /= sti; 6551 if (sbw < bw) { 6552 bbr_log_type_bbrupd(bbr, 6, cts, 6553 delivered, 6554 (uint32_t)sti, 6555 (bw >> 32), 6556 (uint32_t)bw, 6557 rsm->r_first_sent_time, 0, (sbw >> 32), 6558 (uint32_t)sbw); 6559 bw = sbw; 6560 } 6561 } 6562 /* Use the google algorithm for b/w measurements */ 6563 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6564 if ((rsm->r_app_limited == 0) || 6565 (bw > get_filter_value(&bbr->r_ctl.rc_delrate))) { 6566 tcp_bbr_commit_bw(bbr, cts); 6567 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6568 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6569 } 6570 } 6571 } 6572 6573 static void 6574 bbr_google_measurement(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts) 6575 { 6576 if (bbr->rc_in_persist == 0) { 6577 /* We log only when not in persist */ 6578 /* Translate to a Bytes Per Second */ 6579 uint64_t tim, bw; 6580 uint32_t delivered; 6581 int no_apply = 0; 6582 6583 if (TSTMP_GT(bbr->r_ctl.rc_del_time, rsm->r_del_time)) 6584 tim = (uint64_t)(bbr->r_ctl.rc_del_time - rsm->r_del_time); 6585 else 6586 tim = 1; 6587 /* 6588 * Now that we have processed the tim (skipping the sample 6589 * or possibly updating the time, go ahead and 6590 * calculate the cdr. 6591 */ 6592 delivered = (bbr->r_ctl.rc_delivered - rsm->r_delivered); 6593 bw = (uint64_t)delivered; 6594 bw *= (uint64_t)USECS_IN_SECOND; 6595 bw /= tim; 6596 if (tim < bbr->r_ctl.rc_lowest_rtt) { 6597 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6598 tim, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6599 6600 no_apply = 1; 6601 } 6602 /* 6603 * If we are using this b/w shove it in now so we 6604 * can see in the trace viewer if it gets over-ridden. 6605 */ 6606 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6607 /* Gate by the sending rate */ 6608 if (rsm->r_first_sent_time && 6609 TSTMP_GT(rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)],rsm->r_first_sent_time)) { 6610 uint64_t sbw, sti; 6611 /* 6612 * We use what was in flight at the time of our 6613 * send and the size of this send to figure 6614 * out what we have been sending at (amount). 6615 * For the time we take from the time of 6616 * the send of the first send outstanding 6617 * until this send plus this sends pacing 6618 * time. This gives us a good calculation 6619 * as to the rate we have been sending at. 6620 */ 6621 6622 sbw = (uint64_t)(rsm->r_flight_at_send); 6623 sbw *= (uint64_t)USECS_IN_SECOND; 6624 sti = rsm->r_tim_lastsent[(rsm->r_rtr_cnt -1)] - rsm->r_first_sent_time; 6625 sti += rsm->r_pacing_delay; 6626 sbw /= sti; 6627 if (sbw < bw) { 6628 bbr_log_type_bbrupd(bbr, 6, cts, 6629 delivered, 6630 (uint32_t)sti, 6631 (bw >> 32), 6632 (uint32_t)bw, 6633 rsm->r_first_sent_time, 0, (sbw >> 32), 6634 (uint32_t)sbw); 6635 bw = sbw; 6636 } 6637 if ((sti > tim) && 6638 (sti < bbr->r_ctl.rc_lowest_rtt)) { 6639 bbr_log_type_bbrupd(bbr, 99, cts, (uint32_t)tim, delivered, 6640 (uint32_t)sti, bbr->r_ctl.rc_lowest_rtt, 0, 0, 0, 0); 6641 no_apply = 1; 6642 } else 6643 no_apply = 0; 6644 } 6645 bbr->r_ctl.rc_bbr_cur_del_rate = bw; 6646 if ((no_apply == 0) && 6647 ((rsm->r_app_limited == 0) || 6648 (bw > get_filter_value(&bbr->r_ctl.rc_delrate)))) { 6649 tcp_bbr_commit_bw(bbr, cts); 6650 bbr_log_type_bbrupd(bbr, 10, cts, (uint32_t)tim, delivered, 6651 0, 0, 0, 0, bbr->r_ctl.rc_del_time, rsm->r_del_time); 6652 } 6653 } 6654 } 6655 6656 static void 6657 bbr_update_bbr_info(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, uint32_t rtt, uint32_t cts, uint32_t tsin, 6658 uint32_t uts, int32_t match, uint32_t rsm_send_time, int32_t ack_type, struct tcpopt *to) 6659 { 6660 uint64_t old_rttprop; 6661 6662 /* Update our delivery time and amount */ 6663 bbr->r_ctl.rc_delivered += (rsm->r_end - rsm->r_start); 6664 bbr->r_ctl.rc_del_time = cts; 6665 if (rtt == 0) { 6666 /* 6667 * 0 means its a retransmit, for now we don't use these for 6668 * the rest of BBR. 6669 */ 6670 return; 6671 } 6672 if ((bbr->rc_use_google == 0) && 6673 (match != BBR_RTT_BY_EXACTMATCH) && 6674 (match != BBR_RTT_BY_TIMESTAMP)){ 6675 /* 6676 * We get a lot of rtt updates, lets not pay attention to 6677 * any that are not an exact match. That way we don't have 6678 * to worry about timestamps and the whole nonsense of 6679 * unsure if its a retransmission etc (if we ever had the 6680 * timestamp fixed to always have the last thing sent this 6681 * would not be a issue). 6682 */ 6683 return; 6684 } 6685 if ((bbr_no_retran && bbr->rc_use_google) && 6686 (match != BBR_RTT_BY_EXACTMATCH) && 6687 (match != BBR_RTT_BY_TIMESTAMP)){ 6688 /* 6689 * We only do measurements in google mode 6690 * with bbr_no_retran on for sure things. 6691 */ 6692 return; 6693 } 6694 /* Only update srtt if we know by exact match */ 6695 tcp_bbr_xmit_timer(bbr, rtt, rsm_send_time, rsm->r_start, tsin); 6696 if (ack_type == BBR_CUM_ACKED) 6697 bbr->rc_ack_is_cumack = 1; 6698 else 6699 bbr->rc_ack_is_cumack = 0; 6700 old_rttprop = bbr_get_rtt(bbr, BBR_RTT_PROP); 6701 /* 6702 * Note the following code differs to the original 6703 * BBR spec. It calls for <= not <. However after a 6704 * long discussion in email with Neal, he acknowledged 6705 * that it should be < than so that we will have flows 6706 * going into probe-rtt (we were seeing cases where that 6707 * did not happen and caused ugly things to occur). We 6708 * have added this agreed upon fix to our code base. 6709 */ 6710 if (rtt < old_rttprop) { 6711 /* Update when we last saw a rtt drop */ 6712 bbr_log_rtt_shrinks(bbr, cts, 0, rtt, __LINE__, BBR_RTTS_NEWRTT, 0); 6713 bbr_set_reduced_rtt(bbr, cts, __LINE__); 6714 } 6715 bbr_log_type_bbrrttprop(bbr, rtt, (rsm ? rsm->r_end : 0), uts, cts, 6716 match, rsm->r_start, rsm->r_flags); 6717 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 6718 if (old_rttprop != bbr_get_rtt(bbr, BBR_RTT_PROP)) { 6719 /* 6720 * The RTT-prop moved, reset the target (may be a 6721 * nop for some states). 6722 */ 6723 bbr_set_state_target(bbr, __LINE__); 6724 if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) 6725 bbr_log_rtt_shrinks(bbr, cts, 0, 0, 6726 __LINE__, BBR_RTTS_NEW_TARGET, 0); 6727 else if (old_rttprop < bbr_get_rtt(bbr, BBR_RTT_PROP)) 6728 /* It went up */ 6729 bbr_check_probe_rtt_limits(bbr, cts); 6730 } 6731 if ((bbr->rc_use_google == 0) && 6732 (match == BBR_RTT_BY_TIMESTAMP)) { 6733 /* 6734 * We don't do b/w update with 6735 * these since they are not really 6736 * reliable. 6737 */ 6738 return; 6739 } 6740 if (bbr->r_ctl.r_app_limited_until && 6741 (bbr->r_ctl.rc_delivered >= bbr->r_ctl.r_app_limited_until)) { 6742 /* We are no longer app-limited */ 6743 bbr->r_ctl.r_app_limited_until = 0; 6744 } 6745 if (bbr->rc_use_google) { 6746 bbr_google_measurement(bbr, rsm, rtt, cts); 6747 } else { 6748 bbr_nf_measurement(bbr, rsm, rtt, cts); 6749 } 6750 } 6751 6752 /* 6753 * Convert a timestamp that the main stack 6754 * uses (milliseconds) into one that bbr uses 6755 * (microseconds). Return that converted timestamp. 6756 */ 6757 static uint32_t 6758 bbr_ts_convert(uint32_t cts) { 6759 uint32_t sec, msec; 6760 6761 sec = cts / MS_IN_USEC; 6762 msec = cts - (MS_IN_USEC * sec); 6763 return ((sec * USECS_IN_SECOND) + (msec * MS_IN_USEC)); 6764 } 6765 6766 /* 6767 * Return 0 if we did not update the RTT time, return 6768 * 1 if we did. 6769 */ 6770 static int 6771 bbr_update_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, 6772 struct bbr_sendmap *rsm, struct tcpopt *to, uint32_t cts, int32_t ack_type, uint32_t th_ack) 6773 { 6774 int32_t i; 6775 uint32_t t, uts = 0; 6776 6777 if ((rsm->r_flags & BBR_ACKED) || 6778 (rsm->r_flags & BBR_WAS_RENEGED) || 6779 (rsm->r_flags & BBR_RXT_CLEARED)) { 6780 /* Already done */ 6781 return (0); 6782 } 6783 if (rsm->r_rtt_not_allowed) { 6784 /* Not allowed */ 6785 return (0); 6786 } 6787 if (rsm->r_rtr_cnt == 1) { 6788 /* 6789 * Only one transmit. Hopefully the normal case. 6790 */ 6791 if (TSTMP_GT(cts, rsm->r_tim_lastsent[0])) 6792 t = cts - rsm->r_tim_lastsent[0]; 6793 else 6794 t = 1; 6795 if ((int)t <= 0) 6796 t = 1; 6797 bbr->r_ctl.rc_last_rtt = t; 6798 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6799 BBR_RTT_BY_EXACTMATCH, rsm->r_tim_lastsent[0], ack_type, to); 6800 return (1); 6801 } 6802 /* Convert to usecs */ 6803 if ((bbr_can_use_ts_for_rtt == 1) && 6804 (bbr->rc_use_google == 1) && 6805 (ack_type == BBR_CUM_ACKED) && 6806 (to->to_flags & TOF_TS) && 6807 (to->to_tsecr != 0)) { 6808 t = tcp_tv_to_mssectick(&bbr->rc_tv) - to->to_tsecr; 6809 if (t < 1) 6810 t = 1; 6811 t *= MS_IN_USEC; 6812 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, 0, 6813 BBR_RTT_BY_TIMESTAMP, 6814 rsm->r_tim_lastsent[(rsm->r_rtr_cnt-1)], 6815 ack_type, to); 6816 return (1); 6817 } 6818 uts = bbr_ts_convert(to->to_tsecr); 6819 if ((to->to_flags & TOF_TS) && 6820 (to->to_tsecr != 0) && 6821 (ack_type == BBR_CUM_ACKED) && 6822 ((rsm->r_flags & BBR_OVERMAX) == 0)) { 6823 /* 6824 * Now which timestamp does it match? In this block the ACK 6825 * may be coming from a previous transmission. 6826 */ 6827 uint32_t fudge; 6828 6829 fudge = BBR_TIMER_FUDGE; 6830 for (i = 0; i < rsm->r_rtr_cnt; i++) { 6831 if ((SEQ_GEQ(uts, (rsm->r_tim_lastsent[i] - fudge))) && 6832 (SEQ_LEQ(uts, (rsm->r_tim_lastsent[i] + fudge)))) { 6833 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6834 t = cts - rsm->r_tim_lastsent[i]; 6835 else 6836 t = 1; 6837 if ((int)t <= 0) 6838 t = 1; 6839 bbr->r_ctl.rc_last_rtt = t; 6840 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_TSMATCHING, 6841 rsm->r_tim_lastsent[i], ack_type, to); 6842 if ((i + 1) < rsm->r_rtr_cnt) { 6843 /* Likely */ 6844 return (0); 6845 } else if (rsm->r_flags & BBR_TLP) { 6846 bbr->rc_tlp_rtx_out = 0; 6847 } 6848 return (1); 6849 } 6850 } 6851 /* Fall through if we can't find a matching timestamp */ 6852 } 6853 /* 6854 * Ok its a SACK block that we retransmitted. or a windows 6855 * machine without timestamps. We can tell nothing from the 6856 * time-stamp since its not there or the time the peer last 6857 * received a segment that moved forward its cum-ack point. 6858 * 6859 * Lets look at the last retransmit and see what we can tell 6860 * (with BBR for space we only keep 2 note we have to keep 6861 * at least 2 so the map can not be condensed more). 6862 */ 6863 i = rsm->r_rtr_cnt - 1; 6864 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6865 t = cts - rsm->r_tim_lastsent[i]; 6866 else 6867 goto not_sure; 6868 if (t < bbr->r_ctl.rc_lowest_rtt) { 6869 /* 6870 * We retransmitted and the ack came back in less 6871 * than the smallest rtt we have observed in the 6872 * windowed rtt. We most likey did an improper 6873 * retransmit as outlined in 4.2 Step 3 point 2 in 6874 * the rack-draft. 6875 * 6876 * Use the prior transmission to update all the 6877 * information as long as there is only one prior 6878 * transmission. 6879 */ 6880 if ((rsm->r_flags & BBR_OVERMAX) == 0) { 6881 #ifdef BBR_INVARIANTS 6882 if (rsm->r_rtr_cnt == 1) 6883 panic("rsm:%p bbr:%p rsm has overmax and only 1 retranmit flags:%x?", rsm, bbr, rsm->r_flags); 6884 #endif 6885 i = rsm->r_rtr_cnt - 2; 6886 if (TSTMP_GT(cts, rsm->r_tim_lastsent[i])) 6887 t = cts - rsm->r_tim_lastsent[i]; 6888 else 6889 t = 1; 6890 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, BBR_RTT_BY_EARLIER_RET, 6891 rsm->r_tim_lastsent[i], ack_type, to); 6892 return (0); 6893 } else { 6894 /* 6895 * Too many prior transmissions, just 6896 * updated BBR delivered 6897 */ 6898 not_sure: 6899 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6900 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6901 } 6902 } else { 6903 /* 6904 * We retransmitted it and the retransmit did the 6905 * job. 6906 */ 6907 if (rsm->r_flags & BBR_TLP) 6908 bbr->rc_tlp_rtx_out = 0; 6909 if ((rsm->r_flags & BBR_OVERMAX) == 0) 6910 bbr_update_bbr_info(bbr, rsm, t, cts, to->to_tsecr, uts, 6911 BBR_RTT_BY_THIS_RETRAN, 0, ack_type, to); 6912 else 6913 bbr_update_bbr_info(bbr, rsm, 0, cts, to->to_tsecr, uts, 6914 BBR_RTT_BY_SOME_RETRAN, 0, ack_type, to); 6915 return (1); 6916 } 6917 return (0); 6918 } 6919 6920 /* 6921 * Mark the SACK_PASSED flag on all entries prior to rsm send wise. 6922 */ 6923 static void 6924 bbr_log_sack_passed(struct tcpcb *tp, 6925 struct tcp_bbr *bbr, struct bbr_sendmap *rsm) 6926 { 6927 struct bbr_sendmap *nrsm; 6928 6929 nrsm = rsm; 6930 TAILQ_FOREACH_REVERSE_FROM(nrsm, &bbr->r_ctl.rc_tmap, 6931 bbr_head, r_tnext) { 6932 if (nrsm == rsm) { 6933 /* Skip original segment he is acked */ 6934 continue; 6935 } 6936 if (nrsm->r_flags & BBR_ACKED) { 6937 /* Skip ack'd segments */ 6938 continue; 6939 } 6940 if (nrsm->r_flags & BBR_SACK_PASSED) { 6941 /* 6942 * We found one that is already marked 6943 * passed, we have been here before and 6944 * so all others below this are marked. 6945 */ 6946 break; 6947 } 6948 BBR_STAT_INC(bbr_sack_passed); 6949 nrsm->r_flags |= BBR_SACK_PASSED; 6950 if (((nrsm->r_flags & BBR_MARKED_LOST) == 0) && 6951 bbr_is_lost(bbr, nrsm, bbr->r_ctl.rc_rcvtime)) { 6952 bbr->r_ctl.rc_lost += nrsm->r_end - nrsm->r_start; 6953 bbr->r_ctl.rc_lost_bytes += nrsm->r_end - nrsm->r_start; 6954 nrsm->r_flags |= BBR_MARKED_LOST; 6955 } 6956 nrsm->r_flags &= ~BBR_WAS_SACKPASS; 6957 } 6958 } 6959 6960 /* 6961 * Returns the number of bytes that were 6962 * newly ack'd by sack blocks. 6963 */ 6964 static uint32_t 6965 bbr_proc_sack_blk(struct tcpcb *tp, struct tcp_bbr *bbr, struct sackblk *sack, 6966 struct tcpopt *to, struct bbr_sendmap **prsm, uint32_t cts) 6967 { 6968 int32_t times = 0; 6969 uint32_t start, end, changed = 0; 6970 struct bbr_sendmap *rsm, *nrsm; 6971 int32_t used_ref = 1; 6972 uint8_t went_back = 0, went_fwd = 0; 6973 6974 start = sack->start; 6975 end = sack->end; 6976 rsm = *prsm; 6977 if (rsm == NULL) 6978 used_ref = 0; 6979 6980 /* Do we locate the block behind where we last were? */ 6981 if (rsm && SEQ_LT(start, rsm->r_start)) { 6982 went_back = 1; 6983 TAILQ_FOREACH_REVERSE_FROM(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 6984 if (SEQ_GEQ(start, rsm->r_start) && 6985 SEQ_LT(start, rsm->r_end)) { 6986 goto do_rest_ofb; 6987 } 6988 } 6989 } 6990 start_at_beginning: 6991 went_fwd = 1; 6992 /* 6993 * Ok lets locate the block where this guy is fwd from rsm (if its 6994 * set) 6995 */ 6996 TAILQ_FOREACH_FROM(rsm, &bbr->r_ctl.rc_map, r_next) { 6997 if (SEQ_GEQ(start, rsm->r_start) && 6998 SEQ_LT(start, rsm->r_end)) { 6999 break; 7000 } 7001 } 7002 do_rest_ofb: 7003 if (rsm == NULL) { 7004 /* 7005 * This happens when we get duplicate sack blocks with the 7006 * same end. For example SACK 4: 100 SACK 3: 100 The sort 7007 * will not change there location so we would just start at 7008 * the end of the first one and get lost. 7009 */ 7010 if (tp->t_flags & TF_SENTFIN) { 7011 /* 7012 * Check to see if we have not logged the FIN that 7013 * went out. 7014 */ 7015 nrsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7016 if (nrsm && (nrsm->r_end + 1) == tp->snd_max) { 7017 /* 7018 * Ok we did not get the FIN logged. 7019 */ 7020 nrsm->r_end++; 7021 rsm = nrsm; 7022 goto do_rest_ofb; 7023 } 7024 } 7025 if (times == 1) { 7026 #ifdef BBR_INVARIANTS 7027 panic("tp:%p bbr:%p sack:%p to:%p prsm:%p", 7028 tp, bbr, sack, to, prsm); 7029 #else 7030 goto out; 7031 #endif 7032 } 7033 times++; 7034 BBR_STAT_INC(bbr_sack_proc_restart); 7035 rsm = NULL; 7036 goto start_at_beginning; 7037 } 7038 /* Ok we have an ACK for some piece of rsm */ 7039 if (rsm->r_start != start) { 7040 /* 7041 * Need to split this in two pieces the before and after. 7042 */ 7043 if (bbr_sack_mergable(rsm, start, end)) 7044 nrsm = bbr_alloc_full_limit(bbr); 7045 else 7046 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7047 if (nrsm == NULL) { 7048 /* We could not allocate ignore the sack */ 7049 struct sackblk blk; 7050 7051 blk.start = start; 7052 blk.end = end; 7053 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7054 goto out; 7055 } 7056 bbr_clone_rsm(bbr, nrsm, rsm, start); 7057 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7058 if (rsm->r_in_tmap) { 7059 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7060 nrsm->r_in_tmap = 1; 7061 } 7062 rsm->r_flags &= (~BBR_HAS_FIN); 7063 rsm = nrsm; 7064 } 7065 if (SEQ_GEQ(end, rsm->r_end)) { 7066 /* 7067 * The end of this block is either beyond this guy or right 7068 * at this guy. 7069 */ 7070 if ((rsm->r_flags & BBR_ACKED) == 0) { 7071 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7072 changed += (rsm->r_end - rsm->r_start); 7073 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7074 bbr_log_sack_passed(tp, bbr, rsm); 7075 if (rsm->r_flags & BBR_MARKED_LOST) { 7076 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7077 } 7078 /* Is Reordering occuring? */ 7079 if (rsm->r_flags & BBR_SACK_PASSED) { 7080 BBR_STAT_INC(bbr_reorder_seen); 7081 bbr->r_ctl.rc_reorder_ts = cts; 7082 if (rsm->r_flags & BBR_MARKED_LOST) { 7083 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7084 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7085 /* LT sampling also needs adjustment */ 7086 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7087 } 7088 } 7089 rsm->r_flags |= BBR_ACKED; 7090 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7091 if (rsm->r_in_tmap) { 7092 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7093 rsm->r_in_tmap = 0; 7094 } 7095 } 7096 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7097 if (end == rsm->r_end) { 7098 /* This block only - done */ 7099 goto out; 7100 } 7101 /* There is more not coverend by this rsm move on */ 7102 start = rsm->r_end; 7103 nrsm = TAILQ_NEXT(rsm, r_next); 7104 rsm = nrsm; 7105 times = 0; 7106 goto do_rest_ofb; 7107 } 7108 if (rsm->r_flags & BBR_ACKED) { 7109 /* Been here done that */ 7110 goto out; 7111 } 7112 /* Ok we need to split off this one at the tail */ 7113 if (bbr_sack_mergable(rsm, start, end)) 7114 nrsm = bbr_alloc_full_limit(bbr); 7115 else 7116 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 7117 if (nrsm == NULL) { 7118 /* failed XXXrrs what can we do but loose the sack info? */ 7119 struct sackblk blk; 7120 7121 blk.start = start; 7122 blk.end = end; 7123 sack_filter_reject(&bbr->r_ctl.bbr_sf, &blk); 7124 goto out; 7125 } 7126 /* Clone it */ 7127 bbr_clone_rsm(bbr, nrsm, rsm, end); 7128 /* The sack block does not cover this guy fully */ 7129 rsm->r_flags &= (~BBR_HAS_FIN); 7130 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 7131 if (rsm->r_in_tmap) { 7132 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 7133 nrsm->r_in_tmap = 1; 7134 } 7135 nrsm->r_dupack = 0; 7136 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_SACKED, 0); 7137 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_SACKED); 7138 changed += (rsm->r_end - rsm->r_start); 7139 bbr->r_ctl.rc_sacked += (rsm->r_end - rsm->r_start); 7140 bbr_log_sack_passed(tp, bbr, rsm); 7141 /* Is Reordering occuring? */ 7142 if (rsm->r_flags & BBR_MARKED_LOST) { 7143 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7144 } 7145 if (rsm->r_flags & BBR_SACK_PASSED) { 7146 BBR_STAT_INC(bbr_reorder_seen); 7147 bbr->r_ctl.rc_reorder_ts = cts; 7148 if (rsm->r_flags & BBR_MARKED_LOST) { 7149 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7150 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7151 /* LT sampling also needs adjustment */ 7152 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7153 } 7154 } 7155 rsm->r_flags &= ~(BBR_TLP|BBR_WAS_RENEGED|BBR_RXT_CLEARED|BBR_MARKED_LOST); 7156 rsm->r_flags |= BBR_ACKED; 7157 if (rsm->r_in_tmap) { 7158 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7159 rsm->r_in_tmap = 0; 7160 } 7161 out: 7162 if (rsm && (rsm->r_flags & BBR_ACKED)) { 7163 /* 7164 * Now can we merge this newly acked 7165 * block with either the previous or 7166 * next block? 7167 */ 7168 nrsm = TAILQ_NEXT(rsm, r_next); 7169 if (nrsm && 7170 (nrsm->r_flags & BBR_ACKED)) { 7171 /* yep this and next can be merged */ 7172 rsm = bbr_merge_rsm(bbr, rsm, nrsm); 7173 } 7174 /* Now what about the previous? */ 7175 nrsm = TAILQ_PREV(rsm, bbr_head, r_next); 7176 if (nrsm && 7177 (nrsm->r_flags & BBR_ACKED)) { 7178 /* yep the previous and this can be merged */ 7179 rsm = bbr_merge_rsm(bbr, nrsm, rsm); 7180 } 7181 } 7182 if (used_ref == 0) { 7183 BBR_STAT_INC(bbr_sack_proc_all); 7184 } else { 7185 BBR_STAT_INC(bbr_sack_proc_short); 7186 } 7187 if (went_fwd && went_back) { 7188 BBR_STAT_INC(bbr_sack_search_both); 7189 } else if (went_fwd) { 7190 BBR_STAT_INC(bbr_sack_search_fwd); 7191 } else if (went_back) { 7192 BBR_STAT_INC(bbr_sack_search_back); 7193 } 7194 /* Save off where the next seq is */ 7195 if (rsm) 7196 bbr->r_ctl.rc_sacklast = TAILQ_NEXT(rsm, r_next); 7197 else 7198 bbr->r_ctl.rc_sacklast = NULL; 7199 *prsm = rsm; 7200 return (changed); 7201 } 7202 7203 static void inline 7204 bbr_peer_reneges(struct tcp_bbr *bbr, struct bbr_sendmap *rsm, tcp_seq th_ack) 7205 { 7206 struct bbr_sendmap *tmap; 7207 7208 BBR_STAT_INC(bbr_reneges_seen); 7209 tmap = NULL; 7210 while (rsm && (rsm->r_flags & BBR_ACKED)) { 7211 /* Its no longer sacked, mark it so */ 7212 uint32_t oflags; 7213 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7214 #ifdef BBR_INVARIANTS 7215 if (rsm->r_in_tmap) { 7216 panic("bbr:%p rsm:%p flags:0x%x in tmap?", 7217 bbr, rsm, rsm->r_flags); 7218 } 7219 #endif 7220 oflags = rsm->r_flags; 7221 if (rsm->r_flags & BBR_MARKED_LOST) { 7222 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7223 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7224 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7225 /* LT sampling also needs adjustment */ 7226 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7227 } 7228 rsm->r_flags &= ~(BBR_ACKED | BBR_SACK_PASSED | BBR_WAS_SACKPASS | BBR_MARKED_LOST); 7229 rsm->r_flags |= BBR_WAS_RENEGED; 7230 rsm->r_flags |= BBR_RXT_CLEARED; 7231 bbr_log_type_rsmclear(bbr, bbr->r_ctl.rc_rcvtime, rsm, oflags, __LINE__); 7232 /* Rebuild it into our tmap */ 7233 if (tmap == NULL) { 7234 TAILQ_INSERT_HEAD(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7235 tmap = rsm; 7236 } else { 7237 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, tmap, rsm, r_tnext); 7238 tmap = rsm; 7239 } 7240 tmap->r_in_tmap = 1; 7241 /* 7242 * XXXrrs Delivered? Should we do anything here? 7243 * 7244 * Of course we don't on a rxt timeout so maybe its ok that 7245 * we don't? 7246 * 7247 * For now lets not. 7248 */ 7249 rsm = TAILQ_NEXT(rsm, r_next); 7250 } 7251 /* 7252 * Now lets possibly clear the sack filter so we start recognizing 7253 * sacks that cover this area. 7254 */ 7255 sack_filter_clear(&bbr->r_ctl.bbr_sf, th_ack); 7256 } 7257 7258 static void 7259 bbr_log_syn(struct tcpcb *tp, struct tcpopt *to) 7260 { 7261 struct tcp_bbr *bbr; 7262 struct bbr_sendmap *rsm; 7263 uint32_t cts; 7264 7265 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7266 cts = bbr->r_ctl.rc_rcvtime; 7267 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7268 if (rsm && (rsm->r_flags & BBR_HAS_SYN)) { 7269 if ((rsm->r_end - rsm->r_start) <= 1) { 7270 /* Log out the SYN completely */ 7271 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7272 rsm->r_rtr_bytes = 0; 7273 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7274 if (rsm->r_in_tmap) { 7275 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7276 rsm->r_in_tmap = 0; 7277 } 7278 if (bbr->r_ctl.rc_next == rsm) { 7279 /* scoot along the marker */ 7280 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7281 } 7282 if (to != NULL) 7283 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, 0); 7284 bbr_free(bbr, rsm); 7285 } else { 7286 /* There is more (Fast open)? strip out SYN. */ 7287 rsm->r_flags &= ~BBR_HAS_SYN; 7288 rsm->r_start++; 7289 } 7290 } 7291 } 7292 7293 /* 7294 * Returns the number of bytes that were 7295 * acknowledged by SACK blocks. 7296 */ 7297 7298 static uint32_t 7299 bbr_log_ack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, 7300 uint32_t *prev_acked) 7301 { 7302 uint32_t changed, last_seq, entered_recovery = 0; 7303 struct tcp_bbr *bbr; 7304 struct bbr_sendmap *rsm; 7305 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1]; 7306 register uint32_t th_ack; 7307 int32_t i, j, k, new_sb, num_sack_blks = 0; 7308 uint32_t cts, acked, ack_point, sack_changed = 0; 7309 uint32_t p_maxseg, maxseg, p_acked = 0; 7310 7311 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7312 if (tcp_get_flags(th) & TH_RST) { 7313 /* We don't log resets */ 7314 return (0); 7315 } 7316 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7317 cts = bbr->r_ctl.rc_rcvtime; 7318 7319 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7320 changed = 0; 7321 maxseg = tp->t_maxseg - bbr->rc_last_options; 7322 p_maxseg = min(bbr->r_ctl.rc_pace_max_segs, maxseg); 7323 th_ack = th->th_ack; 7324 if (SEQ_GT(th_ack, tp->snd_una)) { 7325 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_UPDATE, __LINE__); 7326 bbr->rc_tp->t_acktime = ticks; 7327 } 7328 if (SEQ_LEQ(th_ack, tp->snd_una)) { 7329 /* Only sent here for sack processing */ 7330 goto proc_sack; 7331 } 7332 if (rsm && SEQ_GT(th_ack, rsm->r_start)) { 7333 changed = th_ack - rsm->r_start; 7334 } else if ((rsm == NULL) && ((th_ack - 1) == tp->iss)) { 7335 /* 7336 * For the SYN incoming case we will not have called 7337 * tcp_output for the sending of the SYN, so there will be 7338 * no map. All other cases should probably be a panic. 7339 */ 7340 if ((to->to_flags & TOF_TS) && (to->to_tsecr != 0)) { 7341 /* 7342 * We have a timestamp that can be used to generate 7343 * an initial RTT. 7344 */ 7345 uint32_t ts, now, rtt; 7346 7347 ts = bbr_ts_convert(to->to_tsecr); 7348 now = bbr_ts_convert(tcp_tv_to_mssectick(&bbr->rc_tv)); 7349 rtt = now - ts; 7350 if (rtt < 1) 7351 rtt = 1; 7352 bbr_log_type_bbrrttprop(bbr, rtt, 7353 tp->iss, 0, cts, 7354 BBR_RTT_BY_TIMESTAMP, tp->iss, 0); 7355 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 7356 changed = 1; 7357 bbr->r_wanted_output = 1; 7358 goto out; 7359 } 7360 goto proc_sack; 7361 } else if (rsm == NULL) { 7362 goto out; 7363 } 7364 if (changed) { 7365 /* 7366 * The ACK point is advancing to th_ack, we must drop off 7367 * the packets in the rack log and calculate any eligble 7368 * RTT's. 7369 */ 7370 bbr->r_wanted_output = 1; 7371 more: 7372 if (rsm == NULL) { 7373 if (tp->t_flags & TF_SENTFIN) { 7374 /* if we send a FIN we will not hav a map */ 7375 goto proc_sack; 7376 } 7377 #ifdef BBR_INVARIANTS 7378 panic("No rack map tp:%p for th:%p state:%d bbr:%p snd_una:%u snd_max:%u chg:%d\n", 7379 tp, 7380 th, tp->t_state, bbr, 7381 tp->snd_una, tp->snd_max, changed); 7382 #endif 7383 goto proc_sack; 7384 } 7385 } 7386 if (SEQ_LT(th_ack, rsm->r_start)) { 7387 /* Huh map is missing this */ 7388 #ifdef BBR_INVARIANTS 7389 printf("Rack map starts at r_start:%u for th_ack:%u huh? ts:%d rs:%d bbr:%p\n", 7390 rsm->r_start, 7391 th_ack, tp->t_state, 7392 bbr->r_state, bbr); 7393 panic("th-ack is bad bbr:%p tp:%p", bbr, tp); 7394 #endif 7395 goto proc_sack; 7396 } else if (th_ack == rsm->r_start) { 7397 /* None here to ack */ 7398 goto proc_sack; 7399 } 7400 /* 7401 * Clear the dup ack counter, it will 7402 * either be freed or if there is some 7403 * remaining we need to start it at zero. 7404 */ 7405 rsm->r_dupack = 0; 7406 /* Now do we consume the whole thing? */ 7407 if (SEQ_GEQ(th_ack, rsm->r_end)) { 7408 /* Its all consumed. */ 7409 uint32_t left; 7410 7411 if (rsm->r_flags & BBR_ACKED) { 7412 /* 7413 * It was acked on the scoreboard -- remove it from 7414 * total 7415 */ 7416 p_acked += (rsm->r_end - rsm->r_start); 7417 bbr->r_ctl.rc_sacked -= (rsm->r_end - rsm->r_start); 7418 if (bbr->r_ctl.rc_sacked == 0) 7419 bbr->r_ctl.rc_sacklast = NULL; 7420 } else { 7421 bbr_update_rtt(tp, bbr, rsm, to, cts, BBR_CUM_ACKED, th_ack); 7422 if (rsm->r_flags & BBR_MARKED_LOST) { 7423 bbr->r_ctl.rc_lost_bytes -= rsm->r_end - rsm->r_start; 7424 } 7425 if (rsm->r_flags & BBR_SACK_PASSED) { 7426 /* 7427 * There are acked segments ACKED on the 7428 * scoreboard further up. We are seeing 7429 * reordering. 7430 */ 7431 BBR_STAT_INC(bbr_reorder_seen); 7432 bbr->r_ctl.rc_reorder_ts = cts; 7433 if (rsm->r_flags & BBR_MARKED_LOST) { 7434 bbr->r_ctl.rc_lost -= rsm->r_end - rsm->r_start; 7435 if (SEQ_GT(bbr->r_ctl.rc_lt_lost, bbr->r_ctl.rc_lost)) 7436 /* LT sampling also needs adjustment */ 7437 bbr->r_ctl.rc_lt_lost = bbr->r_ctl.rc_lost; 7438 } 7439 } 7440 rsm->r_flags &= ~BBR_MARKED_LOST; 7441 } 7442 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7443 rsm->r_rtr_bytes = 0; 7444 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 7445 if (rsm->r_in_tmap) { 7446 TAILQ_REMOVE(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 7447 rsm->r_in_tmap = 0; 7448 } 7449 if (bbr->r_ctl.rc_next == rsm) { 7450 /* scoot along the marker */ 7451 bbr->r_ctl.rc_next = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7452 } 7453 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7454 /* Adjust the packet counts */ 7455 left = th_ack - rsm->r_end; 7456 /* Free back to zone */ 7457 bbr_free(bbr, rsm); 7458 if (left) { 7459 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7460 goto more; 7461 } 7462 goto proc_sack; 7463 } 7464 if (rsm->r_flags & BBR_ACKED) { 7465 /* 7466 * It was acked on the scoreboard -- remove it from total 7467 * for the part being cum-acked. 7468 */ 7469 p_acked += (rsm->r_end - rsm->r_start); 7470 bbr->r_ctl.rc_sacked -= (th_ack - rsm->r_start); 7471 if (bbr->r_ctl.rc_sacked == 0) 7472 bbr->r_ctl.rc_sacklast = NULL; 7473 } else { 7474 /* 7475 * It was acked up to th_ack point for the first time 7476 */ 7477 struct bbr_sendmap lrsm; 7478 7479 memcpy(&lrsm, rsm, sizeof(struct bbr_sendmap)); 7480 lrsm.r_end = th_ack; 7481 bbr_update_rtt(tp, bbr, &lrsm, to, cts, BBR_CUM_ACKED, th_ack); 7482 } 7483 if ((rsm->r_flags & BBR_MARKED_LOST) && 7484 ((rsm->r_flags & BBR_ACKED) == 0)) { 7485 /* 7486 * It was marked lost and partly ack'd now 7487 * for the first time. We lower the rc_lost_bytes 7488 * and still leave it MARKED. 7489 */ 7490 bbr->r_ctl.rc_lost_bytes -= th_ack - rsm->r_start; 7491 } 7492 bbr_isit_a_pkt_epoch(bbr, cts, rsm, __LINE__, BBR_CUM_ACKED); 7493 bbr->r_ctl.rc_holes_rxt -= rsm->r_rtr_bytes; 7494 rsm->r_rtr_bytes = 0; 7495 /* adjust packet count */ 7496 rsm->r_start = th_ack; 7497 proc_sack: 7498 /* Check for reneging */ 7499 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 7500 if (rsm && (rsm->r_flags & BBR_ACKED) && (th_ack == rsm->r_start)) { 7501 /* 7502 * The peer has moved snd_una up to the edge of this send, 7503 * i.e. one that it had previously acked. The only way that 7504 * can be true if the peer threw away data (space issues) 7505 * that it had previously sacked (else it would have given 7506 * us snd_una up to (rsm->r_end). We need to undo the acked 7507 * markings here. 7508 * 7509 * Note we have to look to make sure th_ack is our 7510 * rsm->r_start in case we get an old ack where th_ack is 7511 * behind snd_una. 7512 */ 7513 bbr_peer_reneges(bbr, rsm, th->th_ack); 7514 } 7515 if ((to->to_flags & TOF_SACK) == 0) { 7516 /* We are done nothing left to log */ 7517 goto out; 7518 } 7519 rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_map, bbr_sendmap, r_next); 7520 if (rsm) { 7521 last_seq = rsm->r_end; 7522 } else { 7523 last_seq = tp->snd_max; 7524 } 7525 /* Sack block processing */ 7526 if (SEQ_GT(th_ack, tp->snd_una)) 7527 ack_point = th_ack; 7528 else 7529 ack_point = tp->snd_una; 7530 for (i = 0; i < to->to_nsacks; i++) { 7531 bcopy((to->to_sacks + i * TCPOLEN_SACK), 7532 &sack, sizeof(sack)); 7533 sack.start = ntohl(sack.start); 7534 sack.end = ntohl(sack.end); 7535 if (SEQ_GT(sack.end, sack.start) && 7536 SEQ_GT(sack.start, ack_point) && 7537 SEQ_LT(sack.start, tp->snd_max) && 7538 SEQ_GT(sack.end, ack_point) && 7539 SEQ_LEQ(sack.end, tp->snd_max)) { 7540 if ((bbr->r_ctl.rc_num_small_maps_alloced > bbr_sack_block_limit) && 7541 (SEQ_LT(sack.end, last_seq)) && 7542 ((sack.end - sack.start) < (p_maxseg / 8))) { 7543 /* 7544 * Not the last piece and its smaller than 7545 * 1/8th of a p_maxseg. We ignore this. 7546 */ 7547 BBR_STAT_INC(bbr_runt_sacks); 7548 continue; 7549 } 7550 sack_blocks[num_sack_blks] = sack; 7551 num_sack_blks++; 7552 } else if (SEQ_LEQ(sack.start, th_ack) && 7553 SEQ_LEQ(sack.end, th_ack)) { 7554 /* 7555 * Its a D-SACK block. 7556 */ 7557 tcp_record_dsack(tp, sack.start, sack.end, 0); 7558 } 7559 } 7560 if (num_sack_blks == 0) 7561 goto out; 7562 /* 7563 * Sort the SACK blocks so we can update the rack scoreboard with 7564 * just one pass. 7565 */ 7566 new_sb = sack_filter_blks(tp, &bbr->r_ctl.bbr_sf, sack_blocks, 7567 num_sack_blks, th->th_ack); 7568 ctf_log_sack_filter(bbr->rc_tp, new_sb, sack_blocks); 7569 BBR_STAT_ADD(bbr_sack_blocks, num_sack_blks); 7570 BBR_STAT_ADD(bbr_sack_blocks_skip, (num_sack_blks - new_sb)); 7571 num_sack_blks = new_sb; 7572 if (num_sack_blks < 2) { 7573 goto do_sack_work; 7574 } 7575 /* Sort the sacks */ 7576 for (i = 0; i < num_sack_blks; i++) { 7577 for (j = i + 1; j < num_sack_blks; j++) { 7578 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 7579 sack = sack_blocks[i]; 7580 sack_blocks[i] = sack_blocks[j]; 7581 sack_blocks[j] = sack; 7582 } 7583 } 7584 } 7585 /* 7586 * Now are any of the sack block ends the same (yes some 7587 * implememtations send these)? 7588 */ 7589 again: 7590 if (num_sack_blks > 1) { 7591 for (i = 0; i < num_sack_blks; i++) { 7592 for (j = i + 1; j < num_sack_blks; j++) { 7593 if (sack_blocks[i].end == sack_blocks[j].end) { 7594 /* 7595 * Ok these two have the same end we 7596 * want the smallest end and then 7597 * throw away the larger and start 7598 * again. 7599 */ 7600 if (SEQ_LT(sack_blocks[j].start, sack_blocks[i].start)) { 7601 /* 7602 * The second block covers 7603 * more area use that 7604 */ 7605 sack_blocks[i].start = sack_blocks[j].start; 7606 } 7607 /* 7608 * Now collapse out the dup-sack and 7609 * lower the count 7610 */ 7611 for (k = (j + 1); k < num_sack_blks; k++) { 7612 sack_blocks[j].start = sack_blocks[k].start; 7613 sack_blocks[j].end = sack_blocks[k].end; 7614 j++; 7615 } 7616 num_sack_blks--; 7617 goto again; 7618 } 7619 } 7620 } 7621 } 7622 do_sack_work: 7623 rsm = bbr->r_ctl.rc_sacklast; 7624 for (i = 0; i < num_sack_blks; i++) { 7625 acked = bbr_proc_sack_blk(tp, bbr, &sack_blocks[i], to, &rsm, cts); 7626 if (acked) { 7627 bbr->r_wanted_output = 1; 7628 changed += acked; 7629 sack_changed += acked; 7630 } 7631 } 7632 out: 7633 *prev_acked = p_acked; 7634 if ((sack_changed) && (!IN_RECOVERY(tp->t_flags))) { 7635 /* 7636 * Ok we have a high probability that we need to go in to 7637 * recovery since we have data sack'd 7638 */ 7639 struct bbr_sendmap *rsm; 7640 7641 rsm = bbr_check_recovery_mode(tp, bbr, cts); 7642 if (rsm) { 7643 /* Enter recovery */ 7644 entered_recovery = 1; 7645 bbr->r_wanted_output = 1; 7646 /* 7647 * When we enter recovery we need to assure we send 7648 * one packet. 7649 */ 7650 if (bbr->r_ctl.rc_resend == NULL) { 7651 bbr->r_ctl.rc_resend = rsm; 7652 } 7653 } 7654 } 7655 if (IN_RECOVERY(tp->t_flags) && (entered_recovery == 0)) { 7656 /* 7657 * See if we need to rack-retransmit anything if so set it 7658 * up as the thing to resend assuming something else is not 7659 * already in that position. 7660 */ 7661 if (bbr->r_ctl.rc_resend == NULL) { 7662 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 7663 } 7664 } 7665 /* 7666 * We return the amount that changed via sack, this is used by the 7667 * ack-received code to augment what was changed between th_ack <-> 7668 * snd_una. 7669 */ 7670 return (sack_changed); 7671 } 7672 7673 static void 7674 bbr_strike_dupack(struct tcp_bbr *bbr) 7675 { 7676 struct bbr_sendmap *rsm; 7677 7678 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_tmap); 7679 if (rsm && (rsm->r_dupack < 0xff)) { 7680 rsm->r_dupack++; 7681 if (rsm->r_dupack >= DUP_ACK_THRESHOLD) 7682 bbr->r_wanted_output = 1; 7683 } 7684 } 7685 7686 /* 7687 * Return value of 1, we do not need to call bbr_process_data(). 7688 * return value of 0, bbr_process_data can be called. 7689 * For ret_val if its 0 the TCB is locked and valid, if its non-zero 7690 * its unlocked and probably unsafe to touch the TCB. 7691 */ 7692 static int 7693 bbr_process_ack(struct mbuf *m, struct tcphdr *th, struct socket *so, 7694 struct tcpcb *tp, struct tcpopt *to, 7695 uint32_t tiwin, int32_t tlen, 7696 int32_t * ofia, int32_t thflags, int32_t * ret_val) 7697 { 7698 int32_t ourfinisacked = 0; 7699 int32_t acked_amount; 7700 uint16_t nsegs; 7701 int32_t acked; 7702 uint32_t lost, sack_changed = 0; 7703 struct mbuf *mfree; 7704 struct tcp_bbr *bbr; 7705 uint32_t prev_acked = 0; 7706 7707 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 7708 lost = bbr->r_ctl.rc_lost; 7709 nsegs = max(1, m->m_pkthdr.lro_nsegs); 7710 if (SEQ_GEQ(tp->snd_una, tp->iss + (65535 << tp->snd_scale))) { 7711 /* Checking SEG.ACK against ISS is definitely redundant. */ 7712 tp->t_flags2 |= TF2_NO_ISS_CHECK; 7713 } 7714 if (!V_tcp_insecure_ack) { 7715 tcp_seq seq_min; 7716 bool ghost_ack_check; 7717 7718 if (tp->t_flags2 & TF2_NO_ISS_CHECK) { 7719 /* Check for too old ACKs (RFC 5961, Section 5.2). */ 7720 seq_min = tp->snd_una - tp->max_sndwnd; 7721 ghost_ack_check = false; 7722 } else { 7723 if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) { 7724 /* Checking for ghost ACKs is stricter. */ 7725 seq_min = tp->iss + 1; 7726 ghost_ack_check = true; 7727 } else { 7728 /* 7729 * Checking for too old ACKs (RFC 5961, 7730 * Section 5.2) is stricter. 7731 */ 7732 seq_min = tp->snd_una - tp->max_sndwnd; 7733 ghost_ack_check = false; 7734 } 7735 } 7736 if (SEQ_LT(th->th_ack, seq_min)) { 7737 if (ghost_ack_check) 7738 TCPSTAT_INC(tcps_rcvghostack); 7739 else 7740 TCPSTAT_INC(tcps_rcvacktooold); 7741 /* Send challenge ACK. */ 7742 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7743 bbr->r_wanted_output = 1; 7744 return (1); 7745 } 7746 } 7747 if (SEQ_GT(th->th_ack, tp->snd_max)) { 7748 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 7749 bbr->r_wanted_output = 1; 7750 return (1); 7751 } 7752 if (SEQ_GEQ(th->th_ack, tp->snd_una) || to->to_nsacks) { 7753 /* Process the ack */ 7754 if (bbr->rc_in_persist) 7755 tp->t_rxtshift = 0; 7756 if ((th->th_ack == tp->snd_una) && (tiwin == tp->snd_wnd)) 7757 bbr_strike_dupack(bbr); 7758 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 7759 } 7760 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, (bbr->r_ctl.rc_lost > lost)); 7761 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 7762 /* 7763 * Old ack, behind the last one rcv'd or a duplicate ack 7764 * with SACK info. 7765 */ 7766 if (th->th_ack == tp->snd_una) { 7767 bbr_ack_received(tp, bbr, th, 0, sack_changed, prev_acked, __LINE__, 0); 7768 if (bbr->r_state == TCPS_SYN_SENT) { 7769 /* 7770 * Special case on where we sent SYN. When 7771 * the SYN-ACK is processed in syn_sent 7772 * state it bumps the snd_una. This causes 7773 * us to hit here even though we did ack 1 7774 * byte. 7775 * 7776 * Go through the nothing left case so we 7777 * send data. 7778 */ 7779 goto nothing_left; 7780 } 7781 } 7782 return (0); 7783 } 7784 /* 7785 * If we reach this point, ACK is not a duplicate, i.e., it ACKs 7786 * something we sent. 7787 */ 7788 if (tp->t_flags & TF_NEEDSYN) { 7789 /* 7790 * T/TCP: Connection was half-synchronized, and our SYN has 7791 * been ACK'd (so connection is now fully synchronized). Go 7792 * to non-starred state, increment snd_una for ACK of SYN, 7793 * and check if we can do window scaling. 7794 */ 7795 tp->t_flags &= ~TF_NEEDSYN; 7796 tp->snd_una++; 7797 /* Do window scaling? */ 7798 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 7799 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 7800 tp->rcv_scale = tp->request_r_scale; 7801 /* Send window already scaled. */ 7802 } 7803 } 7804 INP_WLOCK_ASSERT(tptoinpcb(tp)); 7805 7806 acked = BYTES_THIS_ACK(tp, th); 7807 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 7808 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 7809 7810 /* 7811 * If we just performed our first retransmit, and the ACK arrives 7812 * within our recovery window, then it was a mistake to do the 7813 * retransmit in the first place. Recover our original cwnd and 7814 * ssthresh, and proceed to transmit where we left off. 7815 */ 7816 if (tp->t_flags & TF_PREVVALID) { 7817 tp->t_flags &= ~TF_PREVVALID; 7818 if (tp->t_rxtshift == 1 && 7819 (int)(ticks - tp->t_badrxtwin) < 0) 7820 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 7821 } 7822 SOCK_SENDBUF_LOCK(so); 7823 acked_amount = min(acked, (int)sbavail(&so->so_snd)); 7824 tp->snd_wnd -= acked_amount; 7825 mfree = sbcut_locked(&so->so_snd, acked_amount); 7826 /* NB: sowwakeup_locked() does an implicit unlock. */ 7827 sowwakeup_locked(so); 7828 m_freem(mfree); 7829 if (SEQ_GT(th->th_ack, tp->snd_una)) { 7830 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 7831 } 7832 tp->snd_una = th->th_ack; 7833 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, (bbr->r_ctl.rc_lost - lost)); 7834 if (IN_RECOVERY(tp->t_flags)) { 7835 if (SEQ_LT(th->th_ack, tp->snd_recover) && 7836 (SEQ_LT(th->th_ack, tp->snd_max))) { 7837 tcp_bbr_partialack(tp); 7838 } else { 7839 bbr_post_recovery(tp); 7840 } 7841 } 7842 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 7843 tp->snd_recover = tp->snd_una; 7844 } 7845 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 7846 tp->snd_nxt = tp->snd_max; 7847 } 7848 if (tp->snd_una == tp->snd_max) { 7849 /* Nothing left outstanding */ 7850 nothing_left: 7851 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 7852 if (sbavail(&so->so_snd) == 0) 7853 bbr->rc_tp->t_acktime = 0; 7854 if ((sbused(&so->so_snd) == 0) && 7855 (tp->t_flags & TF_SENTFIN)) { 7856 ourfinisacked = 1; 7857 } 7858 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 7859 if (bbr->rc_in_persist == 0) { 7860 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 7861 } 7862 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 7863 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 7864 /* 7865 * We invalidate the last ack here since we 7866 * don't want to transfer forward the time 7867 * for our sum's calculations. 7868 */ 7869 if ((tp->t_state >= TCPS_FIN_WAIT_1) && 7870 (sbavail(&so->so_snd) == 0) && 7871 (tp->t_flags2 & TF2_DROP_AF_DATA)) { 7872 /* 7873 * The socket was gone and the peer sent data, time 7874 * to reset him. 7875 */ 7876 *ret_val = 1; 7877 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 7878 /* tcp_close will kill the inp pre-log the Reset */ 7879 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 7880 tp = tcp_close(tp); 7881 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, tlen); 7882 BBR_STAT_INC(bbr_dropped_af_data); 7883 return (1); 7884 } 7885 /* Set need output so persist might get set */ 7886 bbr->r_wanted_output = 1; 7887 } 7888 if (ofia) 7889 *ofia = ourfinisacked; 7890 return (0); 7891 } 7892 7893 static void 7894 bbr_enter_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7895 { 7896 if (bbr->rc_in_persist == 0) { 7897 bbr_timer_cancel(bbr, __LINE__, cts); 7898 bbr->r_ctl.rc_last_delay_val = 0; 7899 tp->t_rxtshift = 0; 7900 bbr->rc_in_persist = 1; 7901 bbr->r_ctl.rc_went_idle_time = cts; 7902 /* We should be capped when rw went to 0 but just in case */ 7903 bbr_log_type_pesist(bbr, cts, 0, line, 1); 7904 /* Time freezes for the state, so do the accounting now */ 7905 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 7906 uint32_t time_in; 7907 7908 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 7909 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7910 int32_t idx; 7911 7912 idx = bbr_state_val(bbr); 7913 counter_u64_add(bbr_state_time[(idx + 5)], time_in); 7914 } else { 7915 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 7916 } 7917 } 7918 bbr->r_ctl.rc_bbr_state_time = cts; 7919 } 7920 } 7921 7922 static void 7923 bbr_restart_after_idle(struct tcp_bbr *bbr, uint32_t cts, uint32_t idle_time) 7924 { 7925 /* 7926 * Note that if idle time does not exceed our 7927 * threshold, we do nothing continuing the state 7928 * transitions we were last walking through. 7929 */ 7930 if (idle_time >= bbr_idle_restart_threshold) { 7931 if (bbr->rc_use_idle_restart) { 7932 bbr->rc_bbr_state = BBR_STATE_IDLE_EXIT; 7933 /* 7934 * Set our target using BBR_UNIT, so 7935 * we increase at a dramatic rate but 7936 * we stop when we get the pipe 7937 * full again for our current b/w estimate. 7938 */ 7939 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 7940 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 7941 bbr_set_state_target(bbr, __LINE__); 7942 /* Now setup our gains to ramp up */ 7943 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 7944 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 7945 bbr_log_type_statechange(bbr, cts, __LINE__); 7946 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 7947 bbr_substate_change(bbr, cts, __LINE__, 1); 7948 } 7949 } 7950 } 7951 7952 static void 7953 bbr_exit_persist(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts, int32_t line) 7954 { 7955 uint32_t idle_time; 7956 7957 if (bbr->rc_in_persist == 0) 7958 return; 7959 idle_time = bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time); 7960 bbr->rc_in_persist = 0; 7961 bbr->rc_hit_state_1 = 0; 7962 bbr->r_ctl.rc_del_time = cts; 7963 /* 7964 * We invalidate the last ack here since we 7965 * don't want to transfer forward the time 7966 * for our sum's calculations. 7967 */ 7968 if (tcp_in_hpts(bbr->rc_tp)) { 7969 tcp_hpts_remove(bbr->rc_tp); 7970 bbr->rc_timer_first = 0; 7971 bbr->r_ctl.rc_hpts_flags = 0; 7972 bbr->r_ctl.rc_last_delay_val = 0; 7973 bbr->r_ctl.rc_hptsi_agg_delay = 0; 7974 bbr->r_agg_early_set = 0; 7975 bbr->r_ctl.rc_agg_early = 0; 7976 } 7977 bbr_log_type_pesist(bbr, cts, idle_time, line, 0); 7978 if (idle_time >= bbr_rtt_probe_time) { 7979 /* 7980 * This qualifies as a RTT_PROBE session since we drop the 7981 * data outstanding to nothing and waited more than 7982 * bbr_rtt_probe_time. 7983 */ 7984 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_PERSIST, 0); 7985 bbr->r_ctl.last_in_probertt = bbr->r_ctl.rc_rtt_shrinks = cts; 7986 } 7987 tp->t_rxtshift = 0; 7988 /* 7989 * If in probeBW and we have persisted more than an RTT lets do 7990 * special handling. 7991 */ 7992 /* Force a time based epoch */ 7993 bbr_set_epoch(bbr, cts, __LINE__); 7994 /* 7995 * Setup the lost so we don't count anything against the guy 7996 * we have been stuck with during persists. 7997 */ 7998 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 7999 /* Time un-freezes for the state */ 8000 bbr->r_ctl.rc_bbr_state_time = cts; 8001 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) || 8002 (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT)) { 8003 /* 8004 * If we are going back to probe-bw 8005 * or probe_rtt, we may need to possibly 8006 * do a fast restart. 8007 */ 8008 bbr_restart_after_idle(bbr, cts, idle_time); 8009 } 8010 } 8011 8012 static void 8013 bbr_collapsed_window(struct tcp_bbr *bbr) 8014 { 8015 /* 8016 * Now we must walk the 8017 * send map and divide the 8018 * ones left stranded. These 8019 * guys can't cause us to abort 8020 * the connection and are really 8021 * "unsent". However if a buggy 8022 * client actually did keep some 8023 * of the data i.e. collapsed the win 8024 * and refused to ack and then opened 8025 * the win and acked that data. We would 8026 * get into an ack war, the simplier 8027 * method then of just pretending we 8028 * did not send those segments something 8029 * won't work. 8030 */ 8031 struct bbr_sendmap *rsm, *nrsm; 8032 tcp_seq max_seq; 8033 uint32_t maxseg; 8034 int can_split = 0; 8035 int fnd = 0; 8036 8037 maxseg = bbr->rc_tp->t_maxseg - bbr->rc_last_options; 8038 max_seq = bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd; 8039 bbr_log_type_rwnd_collapse(bbr, max_seq, 1, 0); 8040 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 8041 /* Find the first seq past or at maxseq */ 8042 if (rsm->r_flags & BBR_RWND_COLLAPSED) 8043 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8044 if (SEQ_GEQ(max_seq, rsm->r_start) && 8045 SEQ_GEQ(rsm->r_end, max_seq)) { 8046 fnd = 1; 8047 break; 8048 } 8049 } 8050 bbr->rc_has_collapsed = 0; 8051 if (!fnd) { 8052 /* Nothing to do strange */ 8053 return; 8054 } 8055 /* 8056 * Now can we split? 8057 * 8058 * We don't want to split if splitting 8059 * would generate too many small segments 8060 * less we let an attacker fragment our 8061 * send_map and leave us out of memory. 8062 */ 8063 if ((max_seq != rsm->r_start) && 8064 (max_seq != rsm->r_end)){ 8065 /* can we split? */ 8066 int res1, res2; 8067 8068 res1 = max_seq - rsm->r_start; 8069 res2 = rsm->r_end - max_seq; 8070 if ((res1 >= (maxseg/8)) && 8071 (res2 >= (maxseg/8))) { 8072 /* No small pieces here */ 8073 can_split = 1; 8074 } else if (bbr->r_ctl.rc_num_small_maps_alloced < bbr_sack_block_limit) { 8075 /* We are under the limit */ 8076 can_split = 1; 8077 } 8078 } 8079 /* Ok do we need to split this rsm? */ 8080 if (max_seq == rsm->r_start) { 8081 /* It's this guy no split required */ 8082 nrsm = rsm; 8083 } else if (max_seq == rsm->r_end) { 8084 /* It's the next one no split required. */ 8085 nrsm = TAILQ_NEXT(rsm, r_next); 8086 if (nrsm == NULL) { 8087 /* Huh? */ 8088 return; 8089 } 8090 } else if (can_split && SEQ_LT(max_seq, rsm->r_end)) { 8091 /* yep we need to split it */ 8092 nrsm = bbr_alloc_limit(bbr, BBR_LIMIT_TYPE_SPLIT); 8093 if (nrsm == NULL) { 8094 /* failed XXXrrs what can we do mark the whole? */ 8095 nrsm = rsm; 8096 goto no_split; 8097 } 8098 /* Clone it */ 8099 bbr_log_type_rwnd_collapse(bbr, max_seq, 3, 0); 8100 bbr_clone_rsm(bbr, nrsm, rsm, max_seq); 8101 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_map, rsm, nrsm, r_next); 8102 if (rsm->r_in_tmap) { 8103 TAILQ_INSERT_AFTER(&bbr->r_ctl.rc_tmap, rsm, nrsm, r_tnext); 8104 nrsm->r_in_tmap = 1; 8105 } 8106 } else { 8107 /* 8108 * Split not allowed just start here just 8109 * use this guy. 8110 */ 8111 nrsm = rsm; 8112 } 8113 no_split: 8114 BBR_STAT_INC(bbr_collapsed_win); 8115 /* reuse fnd as a count */ 8116 fnd = 0; 8117 TAILQ_FOREACH_FROM(nrsm, &bbr->r_ctl.rc_map, r_next) { 8118 nrsm->r_flags |= BBR_RWND_COLLAPSED; 8119 fnd++; 8120 bbr->rc_has_collapsed = 1; 8121 } 8122 bbr_log_type_rwnd_collapse(bbr, max_seq, 4, fnd); 8123 } 8124 8125 static void 8126 bbr_un_collapse_window(struct tcp_bbr *bbr) 8127 { 8128 struct bbr_sendmap *rsm; 8129 int cleared = 0; 8130 8131 TAILQ_FOREACH_REVERSE(rsm, &bbr->r_ctl.rc_map, bbr_head, r_next) { 8132 if (rsm->r_flags & BBR_RWND_COLLAPSED) { 8133 /* Clear the flag */ 8134 rsm->r_flags &= ~BBR_RWND_COLLAPSED; 8135 cleared++; 8136 } else 8137 break; 8138 } 8139 bbr_log_type_rwnd_collapse(bbr, 8140 (bbr->rc_tp->snd_una + bbr->rc_tp->snd_wnd), 0, cleared); 8141 bbr->rc_has_collapsed = 0; 8142 } 8143 8144 /* 8145 * Return value of 1, the TCB is unlocked and most 8146 * likely gone, return value of 0, the TCB is still 8147 * locked. 8148 */ 8149 static int 8150 bbr_process_data(struct mbuf *m, struct tcphdr *th, struct socket *so, 8151 struct tcpcb *tp, int32_t drop_hdrlen, int32_t tlen, 8152 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt) 8153 { 8154 /* 8155 * Update window information. Don't look at window if no ACK: TAC's 8156 * send garbage on first SYN. 8157 */ 8158 uint16_t nsegs; 8159 int32_t tfo_syn; 8160 struct tcp_bbr *bbr; 8161 8162 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8163 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8164 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8165 if ((thflags & TH_ACK) && 8166 (SEQ_LT(tp->snd_wl1, th->th_seq) || 8167 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 8168 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 8169 /* keep track of pure window updates */ 8170 if (tlen == 0 && 8171 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 8172 KMOD_TCPSTAT_INC(tcps_rcvwinupd); 8173 tp->snd_wnd = tiwin; 8174 tp->snd_wl1 = th->th_seq; 8175 tp->snd_wl2 = th->th_ack; 8176 if (tp->snd_wnd > tp->max_sndwnd) 8177 tp->max_sndwnd = tp->snd_wnd; 8178 bbr->r_wanted_output = 1; 8179 } else if (thflags & TH_ACK) { 8180 if ((tp->snd_wl2 == th->th_ack) && (tiwin < tp->snd_wnd)) { 8181 tp->snd_wnd = tiwin; 8182 tp->snd_wl1 = th->th_seq; 8183 tp->snd_wl2 = th->th_ack; 8184 } 8185 } 8186 if (tp->snd_wnd < ctf_outstanding(tp)) 8187 /* The peer collapsed its window on us */ 8188 bbr_collapsed_window(bbr); 8189 else if (bbr->rc_has_collapsed) 8190 bbr_un_collapse_window(bbr); 8191 /* Was persist timer active and now we have window space? */ 8192 if ((bbr->rc_in_persist != 0) && 8193 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8194 bbr_minseg(bbr)))) { 8195 /* 8196 * Make the rate persist at end of persist mode if idle long 8197 * enough 8198 */ 8199 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8200 8201 /* Make sure we output to start the timer */ 8202 bbr->r_wanted_output = 1; 8203 } 8204 /* Do we need to enter persist? */ 8205 if ((bbr->rc_in_persist == 0) && 8206 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8207 TCPS_HAVEESTABLISHED(tp->t_state) && 8208 (tp->snd_max == tp->snd_una) && 8209 sbavail(&so->so_snd) && 8210 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8211 /* No send window.. we must enter persist */ 8212 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8213 } 8214 if (tp->t_flags2 & TF2_DROP_AF_DATA) { 8215 m_freem(m); 8216 return (0); 8217 } 8218 /* 8219 * We don't support urgent data but 8220 * drag along the up just to make sure 8221 * if there is a stack switch no one 8222 * is surprised. 8223 */ 8224 tp->rcv_up = tp->rcv_nxt; 8225 8226 /* 8227 * Process the segment text, merging it into the TCP sequencing 8228 * queue, and arranging for acknowledgment of receipt if necessary. 8229 * This process logically involves adjusting tp->rcv_wnd as data is 8230 * presented to the user (this happens in tcp_usrreq.c, case 8231 * PRU_RCVD). If a FIN has already been received on this connection 8232 * then we just ignore the text. 8233 */ 8234 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 8235 (tp->t_flags & TF_FASTOPEN)); 8236 if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 8237 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8238 tcp_seq save_start = th->th_seq; 8239 tcp_seq save_rnxt = tp->rcv_nxt; 8240 int save_tlen = tlen; 8241 8242 m_adj(m, drop_hdrlen); /* delayed header drop */ 8243 /* 8244 * Insert segment which includes th into TCP reassembly 8245 * queue with control block tp. Set thflags to whether 8246 * reassembly now includes a segment with FIN. This handles 8247 * the common case inline (segment is the next to be 8248 * received on an established connection, and the queue is 8249 * empty), avoiding linkage into and removal from the queue 8250 * and repetition of various conversions. Set DELACK for 8251 * segments received in order, but ack immediately when 8252 * segments are out of order (so fast retransmit can work). 8253 */ 8254 if (th->th_seq == tp->rcv_nxt && 8255 SEGQ_EMPTY(tp) && 8256 (TCPS_HAVEESTABLISHED(tp->t_state) || 8257 tfo_syn)) { 8258 #ifdef NETFLIX_SB_LIMITS 8259 u_int mcnt, appended; 8260 8261 if (so->so_rcv.sb_shlim) { 8262 mcnt = m_memcnt(m); 8263 appended = 0; 8264 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8265 CFO_NOSLEEP, NULL) == false) { 8266 counter_u64_add(tcp_sb_shlim_fails, 1); 8267 m_freem(m); 8268 return (0); 8269 } 8270 } 8271 8272 #endif 8273 if (DELAY_ACK(tp, bbr, nsegs) || tfo_syn) { 8274 bbr->bbr_segs_rcvd += max(1, nsegs); 8275 tp->t_flags |= TF_DELACK; 8276 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8277 } else { 8278 bbr->r_wanted_output = 1; 8279 tp->t_flags |= TF_ACKNOW; 8280 } 8281 tp->rcv_nxt += tlen; 8282 if (tlen && 8283 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8284 (tp->t_fbyte_in == 0)) { 8285 tp->t_fbyte_in = ticks; 8286 if (tp->t_fbyte_in == 0) 8287 tp->t_fbyte_in = 1; 8288 if (tp->t_fbyte_out && tp->t_fbyte_in) 8289 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8290 } 8291 thflags = tcp_get_flags(th) & TH_FIN; 8292 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8293 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8294 SOCK_RECVBUF_LOCK(so); 8295 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 8296 m_freem(m); 8297 else 8298 #ifdef NETFLIX_SB_LIMITS 8299 appended = 8300 #endif 8301 sbappendstream_locked(&so->so_rcv, m, 0); 8302 /* NB: sorwakeup_locked() does an implicit unlock. */ 8303 sorwakeup_locked(so); 8304 #ifdef NETFLIX_SB_LIMITS 8305 if (so->so_rcv.sb_shlim && appended != mcnt) 8306 counter_fo_release(so->so_rcv.sb_shlim, 8307 mcnt - appended); 8308 #endif 8309 8310 } else { 8311 /* 8312 * XXX: Due to the header drop above "th" is 8313 * theoretically invalid by now. Fortunately 8314 * m_adj() doesn't actually frees any mbufs when 8315 * trimming from the head. 8316 */ 8317 tcp_seq temp = save_start; 8318 8319 thflags = tcp_reass(tp, th, &temp, &tlen, m); 8320 tp->t_flags |= TF_ACKNOW; 8321 if (tp->t_flags & TF_WAKESOR) { 8322 tp->t_flags &= ~TF_WAKESOR; 8323 /* NB: sorwakeup_locked() does an implicit unlock. */ 8324 sorwakeup_locked(so); 8325 } 8326 } 8327 if ((tp->t_flags & TF_SACK_PERMIT) && 8328 (save_tlen > 0) && 8329 TCPS_HAVEESTABLISHED(tp->t_state)) { 8330 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 8331 /* 8332 * DSACK actually handled in the fastpath 8333 * above. 8334 */ 8335 tcp_update_sack_list(tp, save_start, 8336 save_start + save_tlen); 8337 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 8338 if ((tp->rcv_numsacks >= 1) && 8339 (tp->sackblks[0].end == save_start)) { 8340 /* 8341 * Partial overlap, recorded at todrop 8342 * above. 8343 */ 8344 tcp_update_sack_list(tp, 8345 tp->sackblks[0].start, 8346 tp->sackblks[0].end); 8347 } else { 8348 tcp_update_dsack_list(tp, save_start, 8349 save_start + save_tlen); 8350 } 8351 } else if (tlen >= save_tlen) { 8352 /* Update of sackblks. */ 8353 tcp_update_dsack_list(tp, save_start, 8354 save_start + save_tlen); 8355 } else if (tlen > 0) { 8356 tcp_update_dsack_list(tp, save_start, 8357 save_start + tlen); 8358 } 8359 } 8360 } else { 8361 m_freem(m); 8362 thflags &= ~TH_FIN; 8363 } 8364 8365 /* 8366 * If FIN is received ACK the FIN and let the user know that the 8367 * connection is closing. 8368 */ 8369 if (thflags & TH_FIN) { 8370 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8371 /* The socket upcall is handled by socantrcvmore. */ 8372 socantrcvmore(so); 8373 /* 8374 * If connection is half-synchronized (ie NEEDSYN 8375 * flag on) then delay ACK, so it may be piggybacked 8376 * when SYN is sent. Otherwise, since we received a 8377 * FIN then no more input can be expected, send ACK 8378 * now. 8379 */ 8380 if (tp->t_flags & TF_NEEDSYN) { 8381 tp->t_flags |= TF_DELACK; 8382 bbr_timer_cancel(bbr, 8383 __LINE__, bbr->r_ctl.rc_rcvtime); 8384 } else { 8385 tp->t_flags |= TF_ACKNOW; 8386 } 8387 tp->rcv_nxt++; 8388 } 8389 switch (tp->t_state) { 8390 /* 8391 * In SYN_RECEIVED and ESTABLISHED STATES enter the 8392 * CLOSE_WAIT state. 8393 */ 8394 case TCPS_SYN_RECEIVED: 8395 tp->t_starttime = ticks; 8396 /* FALLTHROUGH */ 8397 case TCPS_ESTABLISHED: 8398 tcp_state_change(tp, TCPS_CLOSE_WAIT); 8399 break; 8400 8401 /* 8402 * If still in FIN_WAIT_1 STATE FIN has not been 8403 * acked so enter the CLOSING state. 8404 */ 8405 case TCPS_FIN_WAIT_1: 8406 tcp_state_change(tp, TCPS_CLOSING); 8407 break; 8408 8409 /* 8410 * In FIN_WAIT_2 state enter the TIME_WAIT state, 8411 * starting the time-wait timer, turning off the 8412 * other standard timers. 8413 */ 8414 case TCPS_FIN_WAIT_2: 8415 bbr->rc_timer_first = 1; 8416 bbr_timer_cancel(bbr, 8417 __LINE__, bbr->r_ctl.rc_rcvtime); 8418 tcp_twstart(tp); 8419 return (1); 8420 } 8421 } 8422 /* 8423 * Return any desired output. 8424 */ 8425 if ((tp->t_flags & TF_ACKNOW) || 8426 (sbavail(&so->so_snd) > ctf_outstanding(tp))) { 8427 bbr->r_wanted_output = 1; 8428 } 8429 return (0); 8430 } 8431 8432 /* 8433 * Here nothing is really faster, its just that we 8434 * have broken out the fast-data path also just like 8435 * the fast-ack. Return 1 if we processed the packet 8436 * return 0 if you need to take the "slow-path". 8437 */ 8438 static int 8439 bbr_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 8440 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8441 uint32_t tiwin, int32_t nxt_pkt) 8442 { 8443 uint16_t nsegs; 8444 int32_t newsize = 0; /* automatic sockbuf scaling */ 8445 struct tcp_bbr *bbr; 8446 #ifdef NETFLIX_SB_LIMITS 8447 u_int mcnt, appended; 8448 #endif 8449 8450 /* On the hpts and we would have called output */ 8451 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8452 8453 /* 8454 * If last ACK falls within this segment's sequence numbers, record 8455 * the timestamp. NOTE that the test is modified according to the 8456 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8457 */ 8458 if (bbr->r_ctl.rc_resend != NULL) { 8459 return (0); 8460 } 8461 if (tiwin && tiwin != tp->snd_wnd) { 8462 return (0); 8463 } 8464 if (__predict_false((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)))) { 8465 return (0); 8466 } 8467 if (__predict_false((to->to_flags & TOF_TS) && 8468 (TSTMP_LT(to->to_tsval, tp->ts_recent)))) { 8469 return (0); 8470 } 8471 if (__predict_false((th->th_ack != tp->snd_una))) { 8472 return (0); 8473 } 8474 if (__predict_false(tlen > sbspace(&so->so_rcv))) { 8475 return (0); 8476 } 8477 if ((to->to_flags & TOF_TS) != 0 && 8478 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8479 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 8480 tp->ts_recent = to->to_tsval; 8481 } 8482 /* 8483 * This is a pure, in-sequence data packet with nothing on the 8484 * reassembly queue and we have enough buffer space to take it. 8485 */ 8486 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8487 8488 #ifdef NETFLIX_SB_LIMITS 8489 if (so->so_rcv.sb_shlim) { 8490 mcnt = m_memcnt(m); 8491 appended = 0; 8492 if (counter_fo_get(so->so_rcv.sb_shlim, mcnt, 8493 CFO_NOSLEEP, NULL) == false) { 8494 counter_u64_add(tcp_sb_shlim_fails, 1); 8495 m_freem(m); 8496 return (1); 8497 } 8498 } 8499 #endif 8500 /* Clean receiver SACK report if present */ 8501 if (tp->rcv_numsacks) 8502 tcp_clean_sackreport(tp); 8503 KMOD_TCPSTAT_INC(tcps_preddat); 8504 tp->rcv_nxt += tlen; 8505 if (tlen && 8506 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 8507 (tp->t_fbyte_in == 0)) { 8508 tp->t_fbyte_in = ticks; 8509 if (tp->t_fbyte_in == 0) 8510 tp->t_fbyte_in = 1; 8511 if (tp->t_fbyte_out && tp->t_fbyte_in) 8512 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 8513 } 8514 /* 8515 * Pull snd_wl1 up to prevent seq wrap relative to th_seq. 8516 */ 8517 tp->snd_wl1 = th->th_seq; 8518 /* 8519 * Pull rcv_up up to prevent seq wrap relative to rcv_nxt. 8520 */ 8521 tp->rcv_up = tp->rcv_nxt; 8522 KMOD_TCPSTAT_ADD(tcps_rcvpack, (int)nsegs); 8523 KMOD_TCPSTAT_ADD(tcps_rcvbyte, tlen); 8524 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 8525 8526 /* Add data to socket buffer. */ 8527 SOCK_RECVBUF_LOCK(so); 8528 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8529 m_freem(m); 8530 } else { 8531 /* 8532 * Set new socket buffer size. Give up when limit is 8533 * reached. 8534 */ 8535 if (newsize) 8536 if (!sbreserve_locked(so, SO_RCV, newsize, NULL)) 8537 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 8538 m_adj(m, drop_hdrlen); /* delayed header drop */ 8539 8540 #ifdef NETFLIX_SB_LIMITS 8541 appended = 8542 #endif 8543 sbappendstream_locked(&so->so_rcv, m, 0); 8544 ctf_calc_rwin(so, tp); 8545 } 8546 /* NB: sorwakeup_locked() does an implicit unlock. */ 8547 sorwakeup_locked(so); 8548 #ifdef NETFLIX_SB_LIMITS 8549 if (so->so_rcv.sb_shlim && mcnt != appended) 8550 counter_fo_release(so->so_rcv.sb_shlim, mcnt - appended); 8551 #endif 8552 if (DELAY_ACK(tp, bbr, nsegs)) { 8553 bbr->bbr_segs_rcvd += max(1, nsegs); 8554 tp->t_flags |= TF_DELACK; 8555 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8556 } else { 8557 bbr->r_wanted_output = 1; 8558 tp->t_flags |= TF_ACKNOW; 8559 } 8560 return (1); 8561 } 8562 8563 /* 8564 * This subfunction is used to try to highly optimize the 8565 * fast path. We again allow window updates that are 8566 * in sequence to remain in the fast-path. We also add 8567 * in the __predict's to attempt to help the compiler. 8568 * Note that if we return a 0, then we can *not* process 8569 * it and the caller should push the packet into the 8570 * slow-path. If we return 1, then all is well and 8571 * the packet is fully processed. 8572 */ 8573 static int 8574 bbr_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 8575 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8576 uint32_t tiwin, int32_t nxt_pkt, uint8_t iptos) 8577 { 8578 int32_t acked; 8579 uint16_t nsegs; 8580 uint32_t sack_changed; 8581 uint32_t prev_acked = 0; 8582 struct tcp_bbr *bbr; 8583 8584 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 8585 /* Old ack, behind (or duplicate to) the last one rcv'd */ 8586 return (0); 8587 } 8588 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 8589 /* Above what we have sent? */ 8590 return (0); 8591 } 8592 if (__predict_false(tiwin == 0)) { 8593 /* zero window */ 8594 return (0); 8595 } 8596 if (__predict_false(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN))) { 8597 /* We need a SYN or a FIN, unlikely.. */ 8598 return (0); 8599 } 8600 if ((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 8601 /* Timestamp is behind .. old ack with seq wrap? */ 8602 return (0); 8603 } 8604 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 8605 /* Still recovering */ 8606 return (0); 8607 } 8608 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8609 if (__predict_false(bbr->r_ctl.rc_resend != NULL)) { 8610 /* We are retransmitting */ 8611 return (0); 8612 } 8613 if (__predict_false(bbr->rc_in_persist != 0)) { 8614 /* In persist mode */ 8615 return (0); 8616 } 8617 if (bbr->r_ctl.rc_sacked) { 8618 /* We have sack holes on our scoreboard */ 8619 return (0); 8620 } 8621 /* Ok if we reach here, we can process a fast-ack */ 8622 nsegs = max(1, m->m_pkthdr.lro_nsegs); 8623 sack_changed = bbr_log_ack(tp, to, th, &prev_acked); 8624 /* 8625 * We never detect loss in fast ack [we can't 8626 * have a sack and can't be in recovery so 8627 * we always pass 0 (nothing detected)]. 8628 */ 8629 bbr_lt_bw_sampling(bbr, bbr->r_ctl.rc_rcvtime, 0); 8630 /* Did the window get updated? */ 8631 if (tiwin != tp->snd_wnd) { 8632 tp->snd_wnd = tiwin; 8633 tp->snd_wl1 = th->th_seq; 8634 if (tp->snd_wnd > tp->max_sndwnd) 8635 tp->max_sndwnd = tp->snd_wnd; 8636 } 8637 /* Do we need to exit persists? */ 8638 if ((bbr->rc_in_persist != 0) && 8639 (tp->snd_wnd >= min((bbr->r_ctl.rc_high_rwnd/2), 8640 bbr_minseg(bbr)))) { 8641 bbr_exit_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8642 bbr->r_wanted_output = 1; 8643 } 8644 /* Do we need to enter persists? */ 8645 if ((bbr->rc_in_persist == 0) && 8646 (tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 8647 TCPS_HAVEESTABLISHED(tp->t_state) && 8648 (tp->snd_max == tp->snd_una) && 8649 sbavail(&so->so_snd) && 8650 (sbavail(&so->so_snd) > tp->snd_wnd)) { 8651 /* No send window.. we must enter persist */ 8652 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 8653 } 8654 /* 8655 * If last ACK falls within this segment's sequence numbers, record 8656 * the timestamp. NOTE that the test is modified according to the 8657 * latest proposal of the tcplw@cray.com list (Braden 1993/04/26). 8658 */ 8659 if ((to->to_flags & TOF_TS) != 0 && 8660 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8661 tp->ts_recent_age = bbr->r_ctl.rc_rcvtime; 8662 tp->ts_recent = to->to_tsval; 8663 } 8664 /* 8665 * This is a pure ack for outstanding data. 8666 */ 8667 KMOD_TCPSTAT_INC(tcps_predack); 8668 8669 /* 8670 * "bad retransmit" recovery. 8671 */ 8672 if (tp->t_flags & TF_PREVVALID) { 8673 tp->t_flags &= ~TF_PREVVALID; 8674 if (tp->t_rxtshift == 1 && 8675 (int)(ticks - tp->t_badrxtwin) < 0) 8676 bbr_cong_signal(tp, th, CC_RTO_ERR, NULL); 8677 } 8678 /* 8679 * Recalculate the transmit timer / rtt. 8680 * 8681 * Some boxes send broken timestamp replies during the SYN+ACK 8682 * phase, ignore timestamps of 0 or we could calculate a huge RTT 8683 * and blow up the retransmit timer. 8684 */ 8685 acked = BYTES_THIS_ACK(tp, th); 8686 8687 #ifdef TCP_HHOOK 8688 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 8689 hhook_run_tcp_est_in(tp, th, to); 8690 #endif 8691 8692 KMOD_TCPSTAT_ADD(tcps_rcvackpack, (int)nsegs); 8693 KMOD_TCPSTAT_ADD(tcps_rcvackbyte, acked); 8694 sbdrop(&so->so_snd, acked); 8695 8696 if (SEQ_GT(th->th_ack, tp->snd_una)) 8697 bbr_collapse_rtt(tp, bbr, TCP_REXMTVAL(tp)); 8698 tp->snd_una = th->th_ack; 8699 if (tp->snd_wnd < ctf_outstanding(tp)) 8700 /* The peer collapsed its window on us */ 8701 bbr_collapsed_window(bbr); 8702 else if (bbr->rc_has_collapsed) 8703 bbr_un_collapse_window(bbr); 8704 8705 if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 8706 tp->snd_recover = tp->snd_una; 8707 } 8708 bbr_ack_received(tp, bbr, th, acked, sack_changed, prev_acked, __LINE__, 0); 8709 /* 8710 * Pull snd_wl2 up to prevent seq wrap relative to th_ack. 8711 */ 8712 tp->snd_wl2 = th->th_ack; 8713 m_freem(m); 8714 /* 8715 * If all outstanding data are acked, stop retransmit timer, 8716 * otherwise restart timer using current (possibly backed-off) 8717 * value. If process is waiting for space, wakeup/selwakeup/signal. 8718 * If data are ready to send, let tcp_output decide between more 8719 * output or persist. 8720 * Wake up the socket if we have room to write more. 8721 */ 8722 sowwakeup(so); 8723 if (tp->snd_una == tp->snd_max) { 8724 /* Nothing left outstanding */ 8725 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_CLEAR, __LINE__); 8726 if (sbavail(&so->so_snd) == 0) 8727 bbr->rc_tp->t_acktime = 0; 8728 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8729 if (bbr->rc_in_persist == 0) { 8730 bbr->r_ctl.rc_went_idle_time = bbr->r_ctl.rc_rcvtime; 8731 } 8732 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 8733 bbr_log_ack_clear(bbr, bbr->r_ctl.rc_rcvtime); 8734 /* 8735 * We invalidate the last ack here since we 8736 * don't want to transfer forward the time 8737 * for our sum's calculations. 8738 */ 8739 bbr->r_wanted_output = 1; 8740 } 8741 if (sbavail(&so->so_snd)) { 8742 bbr->r_wanted_output = 1; 8743 } 8744 return (1); 8745 } 8746 8747 /* 8748 * Return value of 1, the TCB is unlocked and most 8749 * likely gone, return value of 0, the TCB is still 8750 * locked. 8751 */ 8752 static int 8753 bbr_do_syn_sent(struct mbuf *m, struct tcphdr *th, struct socket *so, 8754 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8755 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8756 { 8757 int32_t todrop; 8758 int32_t ourfinisacked = 0; 8759 struct tcp_bbr *bbr; 8760 int32_t ret_val = 0; 8761 8762 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8763 8764 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8765 ctf_calc_rwin(so, tp); 8766 /* 8767 * If the state is SYN_SENT: if seg contains an ACK, but not for our 8768 * SYN, drop the input. if seg contains a RST, then drop the 8769 * connection. if seg does not contain SYN, then drop it. Otherwise 8770 * this is an acceptable SYN segment initialize tp->rcv_nxt and 8771 * tp->irs if seg contains ack then advance tp->snd_una. BRR does 8772 * not support ECN so we will not say we are capable. if SYN has 8773 * been acked change to ESTABLISHED else SYN_RCVD state arrange for 8774 * segment to be acked (eventually) continue processing rest of 8775 * data/controls, beginning with URG 8776 */ 8777 if ((thflags & TH_ACK) && 8778 (SEQ_LEQ(th->th_ack, tp->iss) || 8779 SEQ_GT(th->th_ack, tp->snd_max))) { 8780 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8781 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8782 return (1); 8783 } 8784 if ((thflags & (TH_ACK | TH_RST)) == (TH_ACK | TH_RST)) { 8785 TCP_PROBE5(connect__refused, NULL, tp, 8786 mtod(m, const char *), tp, th); 8787 tp = tcp_drop(tp, ECONNREFUSED); 8788 ctf_do_drop(m, tp); 8789 return (1); 8790 } 8791 if (thflags & TH_RST) { 8792 ctf_do_drop(m, tp); 8793 return (1); 8794 } 8795 if (!(thflags & TH_SYN)) { 8796 ctf_do_drop(m, tp); 8797 return (1); 8798 } 8799 tp->irs = th->th_seq; 8800 tcp_rcvseqinit(tp); 8801 if (thflags & TH_ACK) { 8802 int tfo_partial = 0; 8803 8804 KMOD_TCPSTAT_INC(tcps_connects); 8805 soisconnected(so); 8806 #ifdef MAC 8807 mac_socketpeer_set_from_mbuf(m, so); 8808 #endif 8809 /* Do window scaling on this connection? */ 8810 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 8811 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 8812 tp->rcv_scale = tp->request_r_scale; 8813 } 8814 tp->rcv_adv += min(tp->rcv_wnd, 8815 TCP_MAXWIN << tp->rcv_scale); 8816 /* 8817 * If not all the data that was sent in the TFO SYN 8818 * has been acked, resend the remainder right away. 8819 */ 8820 if ((tp->t_flags & TF_FASTOPEN) && 8821 (tp->snd_una != tp->snd_max)) { 8822 tp->snd_nxt = th->th_ack; 8823 tfo_partial = 1; 8824 } 8825 /* 8826 * If there's data, delay ACK; if there's also a FIN ACKNOW 8827 * will be turned on later. 8828 */ 8829 if (DELAY_ACK(tp, bbr, 1) && tlen != 0 && !tfo_partial) { 8830 bbr->bbr_segs_rcvd += 1; 8831 tp->t_flags |= TF_DELACK; 8832 bbr_timer_cancel(bbr, __LINE__, bbr->r_ctl.rc_rcvtime); 8833 } else { 8834 bbr->r_wanted_output = 1; 8835 tp->t_flags |= TF_ACKNOW; 8836 } 8837 if (SEQ_GT(th->th_ack, tp->iss)) { 8838 /* 8839 * The SYN is acked 8840 * handle it specially. 8841 */ 8842 bbr_log_syn(tp, to); 8843 } 8844 if (SEQ_GT(th->th_ack, tp->snd_una)) { 8845 /* 8846 * We advance snd_una for the 8847 * fast open case. If th_ack is 8848 * acknowledging data beyond 8849 * snd_una we can't just call 8850 * ack-processing since the 8851 * data stream in our send-map 8852 * will start at snd_una + 1 (one 8853 * beyond the SYN). If its just 8854 * equal we don't need to do that 8855 * and there is no send_map. 8856 */ 8857 tp->snd_una++; 8858 } 8859 /* 8860 * Received <SYN,ACK> in SYN_SENT[*] state. Transitions: 8861 * SYN_SENT --> ESTABLISHED SYN_SENT* --> FIN_WAIT_1 8862 */ 8863 tp->t_starttime = ticks; 8864 if (tp->t_flags & TF_NEEDFIN) { 8865 tcp_state_change(tp, TCPS_FIN_WAIT_1); 8866 tp->t_flags &= ~TF_NEEDFIN; 8867 thflags &= ~TH_SYN; 8868 } else { 8869 tcp_state_change(tp, TCPS_ESTABLISHED); 8870 TCP_PROBE5(connect__established, NULL, tp, 8871 mtod(m, const char *), tp, th); 8872 cc_conn_init(tp); 8873 } 8874 } else { 8875 /* 8876 * Received initial SYN in SYN-SENT[*] state => simultaneous 8877 * open. If segment contains CC option and there is a 8878 * cached CC, apply TAO test. If it succeeds, connection is * 8879 * half-synchronized. Otherwise, do 3-way handshake: 8880 * SYN-SENT -> SYN-RECEIVED SYN-SENT* -> SYN-RECEIVED* If 8881 * there was no CC option, clear cached CC value. 8882 */ 8883 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 8884 tcp_state_change(tp, TCPS_SYN_RECEIVED); 8885 } 8886 /* 8887 * Advance th->th_seq to correspond to first data byte. If data, 8888 * trim to stay within window, dropping FIN if necessary. 8889 */ 8890 th->th_seq++; 8891 if (tlen > tp->rcv_wnd) { 8892 todrop = tlen - tp->rcv_wnd; 8893 m_adj(m, -todrop); 8894 tlen = tp->rcv_wnd; 8895 thflags &= ~TH_FIN; 8896 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 8897 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 8898 } 8899 tp->snd_wl1 = th->th_seq - 1; 8900 tp->rcv_up = th->th_seq; 8901 /* 8902 * Client side of transaction: already sent SYN and data. If the 8903 * remote host used T/TCP to validate the SYN, our data will be 8904 * ACK'd; if so, enter normal data segment processing in the middle 8905 * of step 5, ack processing. Otherwise, goto step 6. 8906 */ 8907 if (thflags & TH_ACK) { 8908 if ((to->to_flags & TOF_TS) != 0) { 8909 uint32_t t, rtt; 8910 8911 t = tcp_tv_to_mssectick(&bbr->rc_tv); 8912 if (TSTMP_GEQ(t, to->to_tsecr)) { 8913 rtt = t - to->to_tsecr; 8914 if (rtt == 0) { 8915 rtt = 1; 8916 } 8917 rtt *= MS_IN_USEC; 8918 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 8919 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, 8920 rtt, bbr->r_ctl.rc_rcvtime); 8921 } 8922 } 8923 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) 8924 return (ret_val); 8925 /* We may have changed to FIN_WAIT_1 above */ 8926 if (tp->t_state == TCPS_FIN_WAIT_1) { 8927 /* 8928 * In FIN_WAIT_1 STATE in addition to the processing 8929 * for the ESTABLISHED state if our FIN is now 8930 * acknowledged then enter FIN_WAIT_2. 8931 */ 8932 if (ourfinisacked) { 8933 /* 8934 * If we can't receive any more data, then 8935 * closing user can proceed. Starting the 8936 * timer is contrary to the specification, 8937 * but if we don't get a FIN we'll hang 8938 * forever. 8939 * 8940 * XXXjl: we should release the tp also, and 8941 * use a compressed state. 8942 */ 8943 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 8944 soisdisconnected(so); 8945 tcp_timer_activate(tp, TT_2MSL, 8946 (tcp_fast_finwait2_recycle ? 8947 tcp_finwait2_timeout : 8948 TP_MAXIDLE(tp))); 8949 } 8950 tcp_state_change(tp, TCPS_FIN_WAIT_2); 8951 } 8952 } 8953 } 8954 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 8955 tiwin, thflags, nxt_pkt)); 8956 } 8957 8958 /* 8959 * Return value of 1, the TCB is unlocked and most 8960 * likely gone, return value of 0, the TCB is still 8961 * locked. 8962 */ 8963 static int 8964 bbr_do_syn_recv(struct mbuf *m, struct tcphdr *th, struct socket *so, 8965 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 8966 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 8967 { 8968 int32_t ourfinisacked = 0; 8969 int32_t ret_val; 8970 struct tcp_bbr *bbr; 8971 8972 INP_WLOCK_ASSERT(tptoinpcb(tp)); 8973 8974 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 8975 ctf_calc_rwin(so, tp); 8976 if ((thflags & TH_RST) || 8977 (tp->t_fin_is_rst && (thflags & TH_FIN))) 8978 return (ctf_process_rst(m, th, so, tp)); 8979 if ((thflags & TH_ACK) && 8980 (SEQ_LEQ(th->th_ack, tp->snd_una) || 8981 SEQ_GT(th->th_ack, tp->snd_max))) { 8982 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8983 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8984 return (1); 8985 } 8986 if (tp->t_flags & TF_FASTOPEN) { 8987 /* 8988 * When a TFO connection is in SYN_RECEIVED, the only valid 8989 * packets are the initial SYN, a retransmit/copy of the 8990 * initial SYN (possibly with a subset of the original 8991 * data), a valid ACK, a FIN, or a RST. 8992 */ 8993 if ((thflags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) { 8994 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 8995 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 8996 return (1); 8997 } else if (thflags & TH_SYN) { 8998 /* non-initial SYN is ignored */ 8999 if ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_RXT) || 9000 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_TLP) || 9001 (bbr->r_ctl.rc_hpts_flags & PACE_TMR_RACK)) { 9002 ctf_do_drop(m, NULL); 9003 return (0); 9004 } 9005 } else if (!(thflags & (TH_ACK | TH_FIN | TH_RST))) { 9006 ctf_do_drop(m, NULL); 9007 return (0); 9008 } 9009 } 9010 /* 9011 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9012 * it's less than ts_recent, drop it. 9013 */ 9014 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9015 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9016 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9017 return (ret_val); 9018 } 9019 /* 9020 * In the SYN-RECEIVED state, validate that the packet belongs to 9021 * this connection before trimming the data to fit the receive 9022 * window. Check the sequence number versus IRS since we know the 9023 * sequence numbers haven't wrapped. This is a partial fix for the 9024 * "LAND" DoS attack. 9025 */ 9026 if (SEQ_LT(th->th_seq, tp->irs)) { 9027 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 9028 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9029 return (1); 9030 } 9031 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9032 return (ret_val); 9033 } 9034 /* 9035 * If last ACK falls within this segment's sequence numbers, record 9036 * its timestamp. NOTE: 1) That the test incorporates suggestions 9037 * from the latest proposal of the tcplw@cray.com list (Braden 9038 * 1993/04/26). 2) That updating only on newer timestamps interferes 9039 * with our earlier PAWS tests, so this check should be solely 9040 * predicated on the sequence space of this segment. 3) That we 9041 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9042 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9043 * SEG.Len, This modified check allows us to overcome RFC1323's 9044 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9045 * p.869. In such cases, we can still calculate the RTT correctly 9046 * when RCV.NXT == Last.ACK.Sent. 9047 */ 9048 if ((to->to_flags & TOF_TS) != 0 && 9049 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9050 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9051 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9052 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9053 tp->ts_recent = to->to_tsval; 9054 } 9055 tp->snd_wnd = tiwin; 9056 /* 9057 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9058 * is on (half-synchronized state), then queue data for later 9059 * processing; else drop segment and return. 9060 */ 9061 if ((thflags & TH_ACK) == 0) { 9062 if (tp->t_flags & TF_FASTOPEN) { 9063 cc_conn_init(tp); 9064 } 9065 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9066 tiwin, thflags, nxt_pkt)); 9067 } 9068 KMOD_TCPSTAT_INC(tcps_connects); 9069 if (tp->t_flags & TF_SONOTCONN) { 9070 tp->t_flags &= ~TF_SONOTCONN; 9071 soisconnected(so); 9072 } 9073 /* Do window scaling? */ 9074 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 9075 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 9076 tp->rcv_scale = tp->request_r_scale; 9077 } 9078 /* 9079 * ok for the first time in lets see if we can use the ts to figure 9080 * out what the initial RTT was. 9081 */ 9082 if ((to->to_flags & TOF_TS) != 0) { 9083 uint32_t t, rtt; 9084 9085 t = tcp_tv_to_mssectick(&bbr->rc_tv); 9086 if (TSTMP_GEQ(t, to->to_tsecr)) { 9087 rtt = t - to->to_tsecr; 9088 if (rtt == 0) { 9089 rtt = 1; 9090 } 9091 rtt *= MS_IN_USEC; 9092 tcp_bbr_xmit_timer(bbr, rtt, 0, 0, 0); 9093 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, bbr->r_ctl.rc_rcvtime); 9094 } 9095 } 9096 /* Drop off any SYN in the send map (probably not there) */ 9097 if (thflags & TH_ACK) 9098 bbr_log_syn(tp, to); 9099 if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 9100 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 9101 tp->t_tfo_pending = NULL; 9102 } 9103 /* 9104 * Make transitions: SYN-RECEIVED -> ESTABLISHED SYN-RECEIVED* -> 9105 * FIN-WAIT-1 9106 */ 9107 tp->t_starttime = ticks; 9108 if (tp->t_flags & TF_NEEDFIN) { 9109 tcp_state_change(tp, TCPS_FIN_WAIT_1); 9110 tp->t_flags &= ~TF_NEEDFIN; 9111 } else { 9112 tcp_state_change(tp, TCPS_ESTABLISHED); 9113 TCP_PROBE5(accept__established, NULL, tp, 9114 mtod(m, const char *), tp, th); 9115 /* 9116 * TFO connections call cc_conn_init() during SYN 9117 * processing. Calling it again here for such connections 9118 * is not harmless as it would undo the snd_cwnd reduction 9119 * that occurs when a TFO SYN|ACK is retransmitted. 9120 */ 9121 if (!(tp->t_flags & TF_FASTOPEN)) 9122 cc_conn_init(tp); 9123 } 9124 /* 9125 * Account for the ACK of our SYN prior to 9126 * regular ACK processing below, except for 9127 * simultaneous SYN, which is handled later. 9128 */ 9129 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 9130 tp->snd_una++; 9131 /* 9132 * If segment contains data or ACK, will call tcp_reass() later; if 9133 * not, do so now to pass queued data to user. 9134 */ 9135 if (tlen == 0 && (thflags & TH_FIN) == 0) { 9136 (void)tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 9137 (struct mbuf *)0); 9138 if (tp->t_flags & TF_WAKESOR) { 9139 tp->t_flags &= ~TF_WAKESOR; 9140 /* NB: sorwakeup_locked() does an implicit unlock. */ 9141 sorwakeup_locked(so); 9142 } 9143 } 9144 tp->snd_wl1 = th->th_seq - 1; 9145 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9146 return (ret_val); 9147 } 9148 if (tp->t_state == TCPS_FIN_WAIT_1) { 9149 /* We could have went to FIN_WAIT_1 (or EST) above */ 9150 /* 9151 * In FIN_WAIT_1 STATE in addition to the processing for the 9152 * ESTABLISHED state if our FIN is now acknowledged then 9153 * enter FIN_WAIT_2. 9154 */ 9155 if (ourfinisacked) { 9156 /* 9157 * If we can't receive any more data, then closing 9158 * user can proceed. Starting the timer is contrary 9159 * to the specification, but if we don't get a FIN 9160 * we'll hang forever. 9161 * 9162 * XXXjl: we should release the tp also, and use a 9163 * compressed state. 9164 */ 9165 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9166 soisdisconnected(so); 9167 tcp_timer_activate(tp, TT_2MSL, 9168 (tcp_fast_finwait2_recycle ? 9169 tcp_finwait2_timeout : 9170 TP_MAXIDLE(tp))); 9171 } 9172 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9173 } 9174 } 9175 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9176 tiwin, thflags, nxt_pkt)); 9177 } 9178 9179 /* 9180 * Return value of 1, the TCB is unlocked and most 9181 * likely gone, return value of 0, the TCB is still 9182 * locked. 9183 */ 9184 static int 9185 bbr_do_established(struct mbuf *m, struct tcphdr *th, struct socket *so, 9186 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9187 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9188 { 9189 struct tcp_bbr *bbr; 9190 int32_t ret_val; 9191 9192 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9193 9194 /* 9195 * Header prediction: check for the two common cases of a 9196 * uni-directional data xfer. If the packet has no control flags, 9197 * is in-sequence, the window didn't change and we're not 9198 * retransmitting, it's a candidate. If the length is zero and the 9199 * ack moved forward, we're the sender side of the xfer. Just free 9200 * the data acked & wake any higher level process that was blocked 9201 * waiting for space. If the length is non-zero and the ack didn't 9202 * move, we're the receiver side. If we're getting packets in-order 9203 * (the reassembly queue is empty), add the data toc The socket 9204 * buffer and note that we need a delayed ack. Make sure that the 9205 * hidden state-flags are also off. Since we check for 9206 * TCPS_ESTABLISHED first, it can only be TH_NEEDSYN. 9207 */ 9208 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9209 if (bbr->r_ctl.rc_delivered < (4 * tp->t_maxseg)) { 9210 /* 9211 * If we have delived under 4 segments increase the initial 9212 * window if raised by the peer. We use this to determine 9213 * dynamic and static rwnd's at the end of a connection. 9214 */ 9215 bbr->r_ctl.rc_init_rwnd = max(tiwin, tp->snd_wnd); 9216 } 9217 if (__predict_true(((to->to_flags & TOF_SACK) == 0)) && 9218 __predict_true((thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK)) == TH_ACK) && 9219 __predict_true(SEGQ_EMPTY(tp)) && 9220 __predict_true(th->th_seq == tp->rcv_nxt)) { 9221 if (tlen == 0) { 9222 if (bbr_fastack(m, th, so, tp, to, drop_hdrlen, tlen, 9223 tiwin, nxt_pkt, iptos)) { 9224 return (0); 9225 } 9226 } else { 9227 if (bbr_do_fastnewdata(m, th, so, tp, to, drop_hdrlen, tlen, 9228 tiwin, nxt_pkt)) { 9229 return (0); 9230 } 9231 } 9232 } 9233 ctf_calc_rwin(so, tp); 9234 9235 if ((thflags & TH_RST) || 9236 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9237 return (ctf_process_rst(m, th, so, tp)); 9238 /* 9239 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9240 * synchronized state. 9241 */ 9242 if (thflags & TH_SYN) { 9243 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9244 return (ret_val); 9245 } 9246 /* 9247 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9248 * it's less than ts_recent, drop it. 9249 */ 9250 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9251 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9252 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9253 return (ret_val); 9254 } 9255 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9256 return (ret_val); 9257 } 9258 /* 9259 * If last ACK falls within this segment's sequence numbers, record 9260 * its timestamp. NOTE: 1) That the test incorporates suggestions 9261 * from the latest proposal of the tcplw@cray.com list (Braden 9262 * 1993/04/26). 2) That updating only on newer timestamps interferes 9263 * with our earlier PAWS tests, so this check should be solely 9264 * predicated on the sequence space of this segment. 3) That we 9265 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9266 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9267 * SEG.Len, This modified check allows us to overcome RFC1323's 9268 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9269 * p.869. In such cases, we can still calculate the RTT correctly 9270 * when RCV.NXT == Last.ACK.Sent. 9271 */ 9272 if ((to->to_flags & TOF_TS) != 0 && 9273 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9274 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9275 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9276 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9277 tp->ts_recent = to->to_tsval; 9278 } 9279 /* 9280 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9281 * is on (half-synchronized state), then queue data for later 9282 * processing; else drop segment and return. 9283 */ 9284 if ((thflags & TH_ACK) == 0) { 9285 if (tp->t_flags & TF_NEEDSYN) { 9286 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9287 tiwin, thflags, nxt_pkt)); 9288 } else if (tp->t_flags & TF_ACKNOW) { 9289 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9290 bbr->r_wanted_output = 1; 9291 return (ret_val); 9292 } else { 9293 ctf_do_drop(m, NULL); 9294 return (0); 9295 } 9296 } 9297 /* 9298 * Ack processing. 9299 */ 9300 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9301 return (ret_val); 9302 } 9303 if (sbavail(&so->so_snd)) { 9304 if (ctf_progress_timeout_check(tp, true)) { 9305 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9306 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9307 return (1); 9308 } 9309 } 9310 /* State changes only happen in bbr_process_data() */ 9311 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9312 tiwin, thflags, nxt_pkt)); 9313 } 9314 9315 /* 9316 * Return value of 1, the TCB is unlocked and most 9317 * likely gone, return value of 0, the TCB is still 9318 * locked. 9319 */ 9320 static int 9321 bbr_do_close_wait(struct mbuf *m, struct tcphdr *th, struct socket *so, 9322 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9323 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9324 { 9325 struct tcp_bbr *bbr; 9326 int32_t ret_val; 9327 9328 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9329 9330 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9331 ctf_calc_rwin(so, tp); 9332 if ((thflags & TH_RST) || 9333 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9334 return (ctf_process_rst(m, th, so, tp)); 9335 /* 9336 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9337 * synchronized state. 9338 */ 9339 if (thflags & TH_SYN) { 9340 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9341 return (ret_val); 9342 } 9343 /* 9344 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9345 * it's less than ts_recent, drop it. 9346 */ 9347 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9348 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9349 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9350 return (ret_val); 9351 } 9352 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9353 return (ret_val); 9354 } 9355 /* 9356 * If last ACK falls within this segment's sequence numbers, record 9357 * its timestamp. NOTE: 1) That the test incorporates suggestions 9358 * from the latest proposal of the tcplw@cray.com list (Braden 9359 * 1993/04/26). 2) That updating only on newer timestamps interferes 9360 * with our earlier PAWS tests, so this check should be solely 9361 * predicated on the sequence space of this segment. 3) That we 9362 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9363 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9364 * SEG.Len, This modified check allows us to overcome RFC1323's 9365 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9366 * p.869. In such cases, we can still calculate the RTT correctly 9367 * when RCV.NXT == Last.ACK.Sent. 9368 */ 9369 if ((to->to_flags & TOF_TS) != 0 && 9370 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9371 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9372 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9373 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9374 tp->ts_recent = to->to_tsval; 9375 } 9376 /* 9377 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9378 * is on (half-synchronized state), then queue data for later 9379 * processing; else drop segment and return. 9380 */ 9381 if ((thflags & TH_ACK) == 0) { 9382 if (tp->t_flags & TF_NEEDSYN) { 9383 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9384 tiwin, thflags, nxt_pkt)); 9385 } else if (tp->t_flags & TF_ACKNOW) { 9386 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9387 bbr->r_wanted_output = 1; 9388 return (ret_val); 9389 } else { 9390 ctf_do_drop(m, NULL); 9391 return (0); 9392 } 9393 } 9394 /* 9395 * Ack processing. 9396 */ 9397 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, NULL, thflags, &ret_val)) { 9398 return (ret_val); 9399 } 9400 if (sbavail(&so->so_snd)) { 9401 if (ctf_progress_timeout_check(tp, true)) { 9402 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9403 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9404 return (1); 9405 } 9406 } 9407 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9408 tiwin, thflags, nxt_pkt)); 9409 } 9410 9411 static int 9412 bbr_check_data_after_close(struct mbuf *m, struct tcp_bbr *bbr, 9413 struct tcpcb *tp, int32_t * tlen, struct tcphdr *th, struct socket *so) 9414 { 9415 9416 if (bbr->rc_allow_data_af_clo == 0) { 9417 close_now: 9418 tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 9419 /* tcp_close will kill the inp pre-log the Reset */ 9420 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 9421 tp = tcp_close(tp); 9422 KMOD_TCPSTAT_INC(tcps_rcvafterclose); 9423 ctf_do_dropwithreset(m, tp, th, BANDLIM_UNLIMITED, (*tlen)); 9424 return (1); 9425 } 9426 if (sbavail(&so->so_snd) == 0) 9427 goto close_now; 9428 /* Ok we allow data that is ignored and a followup reset */ 9429 tp->rcv_nxt = th->th_seq + *tlen; 9430 tp->t_flags2 |= TF2_DROP_AF_DATA; 9431 bbr->r_wanted_output = 1; 9432 *tlen = 0; 9433 return (0); 9434 } 9435 9436 /* 9437 * Return value of 1, the TCB is unlocked and most 9438 * likely gone, return value of 0, the TCB is still 9439 * locked. 9440 */ 9441 static int 9442 bbr_do_fin_wait_1(struct mbuf *m, struct tcphdr *th, struct socket *so, 9443 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9444 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9445 { 9446 int32_t ourfinisacked = 0; 9447 int32_t ret_val; 9448 struct tcp_bbr *bbr; 9449 9450 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9451 9452 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9453 ctf_calc_rwin(so, tp); 9454 if ((thflags & TH_RST) || 9455 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9456 return (ctf_process_rst(m, th, so, tp)); 9457 /* 9458 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9459 * synchronized state. 9460 */ 9461 if (thflags & TH_SYN) { 9462 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9463 return (ret_val); 9464 } 9465 /* 9466 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9467 * it's less than ts_recent, drop it. 9468 */ 9469 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9470 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9471 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9472 return (ret_val); 9473 } 9474 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9475 return (ret_val); 9476 } 9477 /* 9478 * If new data are received on a connection after the user processes 9479 * are gone, then RST the other end. 9480 * We call a new function now so we might continue and setup 9481 * to reset at all data being ack'd. 9482 */ 9483 if ((tp->t_flags & TF_CLOSED) && tlen && 9484 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9485 return (1); 9486 /* 9487 * If last ACK falls within this segment's sequence numbers, record 9488 * its timestamp. NOTE: 1) That the test incorporates suggestions 9489 * from the latest proposal of the tcplw@cray.com list (Braden 9490 * 1993/04/26). 2) That updating only on newer timestamps interferes 9491 * with our earlier PAWS tests, so this check should be solely 9492 * predicated on the sequence space of this segment. 3) That we 9493 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9494 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9495 * SEG.Len, This modified check allows us to overcome RFC1323's 9496 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9497 * p.869. In such cases, we can still calculate the RTT correctly 9498 * when RCV.NXT == Last.ACK.Sent. 9499 */ 9500 if ((to->to_flags & TOF_TS) != 0 && 9501 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9502 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9503 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9504 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9505 tp->ts_recent = to->to_tsval; 9506 } 9507 /* 9508 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9509 * is on (half-synchronized state), then queue data for later 9510 * processing; else drop segment and return. 9511 */ 9512 if ((thflags & TH_ACK) == 0) { 9513 if (tp->t_flags & TF_NEEDSYN) { 9514 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9515 tiwin, thflags, nxt_pkt)); 9516 } else if (tp->t_flags & TF_ACKNOW) { 9517 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9518 bbr->r_wanted_output = 1; 9519 return (ret_val); 9520 } else { 9521 ctf_do_drop(m, NULL); 9522 return (0); 9523 } 9524 } 9525 /* 9526 * Ack processing. 9527 */ 9528 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9529 return (ret_val); 9530 } 9531 if (ourfinisacked) { 9532 /* 9533 * If we can't receive any more data, then closing user can 9534 * proceed. Starting the timer is contrary to the 9535 * specification, but if we don't get a FIN we'll hang 9536 * forever. 9537 * 9538 * XXXjl: we should release the tp also, and use a 9539 * compressed state. 9540 */ 9541 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 9542 soisdisconnected(so); 9543 tcp_timer_activate(tp, TT_2MSL, 9544 (tcp_fast_finwait2_recycle ? 9545 tcp_finwait2_timeout : 9546 TP_MAXIDLE(tp))); 9547 } 9548 tcp_state_change(tp, TCPS_FIN_WAIT_2); 9549 } 9550 if (sbavail(&so->so_snd)) { 9551 if (ctf_progress_timeout_check(tp, true)) { 9552 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9553 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9554 return (1); 9555 } 9556 } 9557 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9558 tiwin, thflags, nxt_pkt)); 9559 } 9560 9561 /* 9562 * Return value of 1, the TCB is unlocked and most 9563 * likely gone, return value of 0, the TCB is still 9564 * locked. 9565 */ 9566 static int 9567 bbr_do_closing(struct mbuf *m, struct tcphdr *th, struct socket *so, 9568 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9569 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9570 { 9571 int32_t ourfinisacked = 0; 9572 int32_t ret_val; 9573 struct tcp_bbr *bbr; 9574 9575 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9576 9577 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9578 ctf_calc_rwin(so, tp); 9579 if ((thflags & TH_RST) || 9580 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9581 return (ctf_process_rst(m, th, so, tp)); 9582 /* 9583 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9584 * synchronized state. 9585 */ 9586 if (thflags & TH_SYN) { 9587 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9588 return (ret_val); 9589 } 9590 /* 9591 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9592 * it's less than ts_recent, drop it. 9593 */ 9594 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9595 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9596 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9597 return (ret_val); 9598 } 9599 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9600 return (ret_val); 9601 } 9602 /* 9603 * If last ACK falls within this segment's sequence numbers, record 9604 * its timestamp. NOTE: 1) That the test incorporates suggestions 9605 * from the latest proposal of the tcplw@cray.com list (Braden 9606 * 1993/04/26). 2) That updating only on newer timestamps interferes 9607 * with our earlier PAWS tests, so this check should be solely 9608 * predicated on the sequence space of this segment. 3) That we 9609 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9610 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9611 * SEG.Len, This modified check allows us to overcome RFC1323's 9612 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9613 * p.869. In such cases, we can still calculate the RTT correctly 9614 * when RCV.NXT == Last.ACK.Sent. 9615 */ 9616 if ((to->to_flags & TOF_TS) != 0 && 9617 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9618 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9619 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9620 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9621 tp->ts_recent = to->to_tsval; 9622 } 9623 /* 9624 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9625 * is on (half-synchronized state), then queue data for later 9626 * processing; else drop segment and return. 9627 */ 9628 if ((thflags & TH_ACK) == 0) { 9629 if (tp->t_flags & TF_NEEDSYN) { 9630 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9631 tiwin, thflags, nxt_pkt)); 9632 } else if (tp->t_flags & TF_ACKNOW) { 9633 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9634 bbr->r_wanted_output = 1; 9635 return (ret_val); 9636 } else { 9637 ctf_do_drop(m, NULL); 9638 return (0); 9639 } 9640 } 9641 /* 9642 * Ack processing. 9643 */ 9644 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9645 return (ret_val); 9646 } 9647 if (ourfinisacked) { 9648 tcp_twstart(tp); 9649 m_freem(m); 9650 return (1); 9651 } 9652 if (sbavail(&so->so_snd)) { 9653 if (ctf_progress_timeout_check(tp, true)) { 9654 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9655 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9656 return (1); 9657 } 9658 } 9659 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9660 tiwin, thflags, nxt_pkt)); 9661 } 9662 9663 /* 9664 * Return value of 1, the TCB is unlocked and most 9665 * likely gone, return value of 0, the TCB is still 9666 * locked. 9667 */ 9668 static int 9669 bbr_do_lastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 9670 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9671 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9672 { 9673 int32_t ourfinisacked = 0; 9674 int32_t ret_val; 9675 struct tcp_bbr *bbr; 9676 9677 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9678 9679 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9680 ctf_calc_rwin(so, tp); 9681 if ((thflags & TH_RST) || 9682 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9683 return (ctf_process_rst(m, th, so, tp)); 9684 /* 9685 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9686 * synchronized state. 9687 */ 9688 if (thflags & TH_SYN) { 9689 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9690 return (ret_val); 9691 } 9692 /* 9693 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9694 * it's less than ts_recent, drop it. 9695 */ 9696 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9697 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9698 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9699 return (ret_val); 9700 } 9701 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9702 return (ret_val); 9703 } 9704 /* 9705 * If last ACK falls within this segment's sequence numbers, record 9706 * its timestamp. NOTE: 1) That the test incorporates suggestions 9707 * from the latest proposal of the tcplw@cray.com list (Braden 9708 * 1993/04/26). 2) That updating only on newer timestamps interferes 9709 * with our earlier PAWS tests, so this check should be solely 9710 * predicated on the sequence space of this segment. 3) That we 9711 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9712 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9713 * SEG.Len, This modified check allows us to overcome RFC1323's 9714 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9715 * p.869. In such cases, we can still calculate the RTT correctly 9716 * when RCV.NXT == Last.ACK.Sent. 9717 */ 9718 if ((to->to_flags & TOF_TS) != 0 && 9719 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9720 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9721 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9722 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9723 tp->ts_recent = to->to_tsval; 9724 } 9725 /* 9726 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9727 * is on (half-synchronized state), then queue data for later 9728 * processing; else drop segment and return. 9729 */ 9730 if ((thflags & TH_ACK) == 0) { 9731 if (tp->t_flags & TF_NEEDSYN) { 9732 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9733 tiwin, thflags, nxt_pkt)); 9734 } else if (tp->t_flags & TF_ACKNOW) { 9735 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9736 bbr->r_wanted_output = 1; 9737 return (ret_val); 9738 } else { 9739 ctf_do_drop(m, NULL); 9740 return (0); 9741 } 9742 } 9743 /* 9744 * case TCPS_LAST_ACK: Ack processing. 9745 */ 9746 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9747 return (ret_val); 9748 } 9749 if (ourfinisacked) { 9750 tp = tcp_close(tp); 9751 ctf_do_drop(m, tp); 9752 return (1); 9753 } 9754 if (sbavail(&so->so_snd)) { 9755 if (ctf_progress_timeout_check(tp, true)) { 9756 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9757 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9758 return (1); 9759 } 9760 } 9761 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9762 tiwin, thflags, nxt_pkt)); 9763 } 9764 9765 /* 9766 * Return value of 1, the TCB is unlocked and most 9767 * likely gone, return value of 0, the TCB is still 9768 * locked. 9769 */ 9770 static int 9771 bbr_do_fin_wait_2(struct mbuf *m, struct tcphdr *th, struct socket *so, 9772 struct tcpcb *tp, struct tcpopt *to, int32_t drop_hdrlen, int32_t tlen, 9773 uint32_t tiwin, int32_t thflags, int32_t nxt_pkt, uint8_t iptos) 9774 { 9775 int32_t ourfinisacked = 0; 9776 int32_t ret_val; 9777 struct tcp_bbr *bbr; 9778 9779 INP_WLOCK_ASSERT(tptoinpcb(tp)); 9780 9781 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 9782 ctf_calc_rwin(so, tp); 9783 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 9784 if ((thflags & TH_RST) || 9785 (tp->t_fin_is_rst && (thflags & TH_FIN))) 9786 return (ctf_process_rst(m, th, so, tp)); 9787 9788 /* 9789 * RFC5961 Section 4.2 Send challenge ACK for any SYN in 9790 * synchronized state. 9791 */ 9792 if (thflags & TH_SYN) { 9793 ctf_challenge_ack(m, th, tp, iptos, &ret_val); 9794 return (ret_val); 9795 } 9796 /* 9797 * RFC 1323 PAWS: If we have a timestamp reply on this segment and 9798 * it's less than ts_recent, drop it. 9799 */ 9800 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 9801 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 9802 if (ctf_ts_check(m, th, tp, tlen, thflags, &ret_val)) 9803 return (ret_val); 9804 } 9805 if (ctf_drop_checks(to, m, th, tp, &tlen, &thflags, &drop_hdrlen, &ret_val)) { 9806 return (ret_val); 9807 } 9808 /* 9809 * If new data are received on a connection after the user processes 9810 * are gone, then we may RST the other end depending on the outcome 9811 * of bbr_check_data_after_close. 9812 * We call a new function now so we might continue and setup 9813 * to reset at all data being ack'd. 9814 */ 9815 if ((tp->t_flags & TF_CLOSED) && tlen && 9816 bbr_check_data_after_close(m, bbr, tp, &tlen, th, so)) 9817 return (1); 9818 /* 9819 * If last ACK falls within this segment's sequence numbers, record 9820 * its timestamp. NOTE: 1) That the test incorporates suggestions 9821 * from the latest proposal of the tcplw@cray.com list (Braden 9822 * 1993/04/26). 2) That updating only on newer timestamps interferes 9823 * with our earlier PAWS tests, so this check should be solely 9824 * predicated on the sequence space of this segment. 3) That we 9825 * modify the segment boundary check to be Last.ACK.Sent <= SEG.SEQ 9826 * + SEG.Len instead of RFC1323's Last.ACK.Sent < SEG.SEQ + 9827 * SEG.Len, This modified check allows us to overcome RFC1323's 9828 * limitations as described in Stevens TCP/IP Illustrated Vol. 2 9829 * p.869. In such cases, we can still calculate the RTT correctly 9830 * when RCV.NXT == Last.ACK.Sent. 9831 */ 9832 if ((to->to_flags & TOF_TS) != 0 && 9833 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 9834 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 9835 ((thflags & (TH_SYN | TH_FIN)) != 0))) { 9836 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 9837 tp->ts_recent = to->to_tsval; 9838 } 9839 /* 9840 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN flag 9841 * is on (half-synchronized state), then queue data for later 9842 * processing; else drop segment and return. 9843 */ 9844 if ((thflags & TH_ACK) == 0) { 9845 if (tp->t_flags & TF_NEEDSYN) { 9846 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9847 tiwin, thflags, nxt_pkt)); 9848 } else if (tp->t_flags & TF_ACKNOW) { 9849 ctf_do_dropafterack(m, tp, th, thflags, tlen, &ret_val); 9850 bbr->r_wanted_output = 1; 9851 return (ret_val); 9852 } else { 9853 ctf_do_drop(m, NULL); 9854 return (0); 9855 } 9856 } 9857 /* 9858 * Ack processing. 9859 */ 9860 if (bbr_process_ack(m, th, so, tp, to, tiwin, tlen, &ourfinisacked, thflags, &ret_val)) { 9861 return (ret_val); 9862 } 9863 if (sbavail(&so->so_snd)) { 9864 if (ctf_progress_timeout_check(tp, true)) { 9865 bbr_log_progress_event(bbr, tp, tick, PROGRESS_DROP, __LINE__); 9866 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 9867 return (1); 9868 } 9869 } 9870 return (bbr_process_data(m, th, so, tp, drop_hdrlen, tlen, 9871 tiwin, thflags, nxt_pkt)); 9872 } 9873 9874 static void 9875 bbr_stop_all_timers(struct tcpcb *tp, struct tcp_bbr *bbr) 9876 { 9877 /* 9878 * Assure no timers are running. 9879 */ 9880 if (tcp_timer_active(tp, TT_PERSIST)) { 9881 /* We enter in persists, set the flag appropriately */ 9882 bbr->rc_in_persist = 1; 9883 } 9884 if (tcp_in_hpts(bbr->rc_tp)) { 9885 tcp_hpts_remove(bbr->rc_tp); 9886 } 9887 } 9888 9889 static void 9890 bbr_google_mode_on(struct tcp_bbr *bbr) 9891 { 9892 bbr->rc_use_google = 1; 9893 bbr->rc_no_pacing = 0; 9894 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 9895 bbr->r_use_policer = bbr_policer_detection_enabled; 9896 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 9897 bbr->bbr_use_rack_cheat = 0; 9898 bbr->r_ctl.rc_incr_tmrs = 0; 9899 bbr->r_ctl.rc_inc_tcp_oh = 0; 9900 bbr->r_ctl.rc_inc_ip_oh = 0; 9901 bbr->r_ctl.rc_inc_enet_oh = 0; 9902 reset_time(&bbr->r_ctl.rc_delrate, 9903 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 9904 reset_time_small(&bbr->r_ctl.rc_rttprop, 9905 (11 * USECS_IN_SECOND)); 9906 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9907 } 9908 9909 static void 9910 bbr_google_mode_off(struct tcp_bbr *bbr) 9911 { 9912 bbr->rc_use_google = 0; 9913 bbr->r_ctl.bbr_google_discount = 0; 9914 bbr->no_pacing_until = bbr_no_pacing_until; 9915 bbr->r_use_policer = 0; 9916 if (bbr->no_pacing_until) 9917 bbr->rc_no_pacing = 1; 9918 else 9919 bbr->rc_no_pacing = 0; 9920 if (bbr_use_rack_resend_cheat) 9921 bbr->bbr_use_rack_cheat = 1; 9922 else 9923 bbr->bbr_use_rack_cheat = 0; 9924 if (bbr_incr_timers) 9925 bbr->r_ctl.rc_incr_tmrs = 1; 9926 else 9927 bbr->r_ctl.rc_incr_tmrs = 0; 9928 if (bbr_include_tcp_oh) 9929 bbr->r_ctl.rc_inc_tcp_oh = 1; 9930 else 9931 bbr->r_ctl.rc_inc_tcp_oh = 0; 9932 if (bbr_include_ip_oh) 9933 bbr->r_ctl.rc_inc_ip_oh = 1; 9934 else 9935 bbr->r_ctl.rc_inc_ip_oh = 0; 9936 if (bbr_include_enet_oh) 9937 bbr->r_ctl.rc_inc_enet_oh = 1; 9938 else 9939 bbr->r_ctl.rc_inc_enet_oh = 0; 9940 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 9941 reset_time(&bbr->r_ctl.rc_delrate, 9942 bbr_num_pktepo_for_del_limit); 9943 reset_time_small(&bbr->r_ctl.rc_rttprop, 9944 (bbr_filter_len_sec * USECS_IN_SECOND)); 9945 tcp_bbr_tso_size_check(bbr, tcp_get_usecs(&bbr->rc_tv)); 9946 } 9947 /* 9948 * Return 0 on success, non-zero on failure 9949 * which indicates the error (usually no memory). 9950 */ 9951 static int 9952 bbr_init(struct tcpcb *tp, void **ptr) 9953 { 9954 struct inpcb *inp = tptoinpcb(tp); 9955 struct tcp_bbr *bbr = NULL; 9956 uint32_t cts; 9957 9958 tcp_hpts_init(tp); 9959 9960 *ptr = uma_zalloc(bbr_pcb_zone, (M_NOWAIT | M_ZERO)); 9961 if (*ptr == NULL) { 9962 /* 9963 * We need to allocate memory but cant. The INP and INP_INFO 9964 * locks and they are recursive (happens during setup. So a 9965 * scheme to drop the locks fails :( 9966 * 9967 */ 9968 return (ENOMEM); 9969 } 9970 bbr = (struct tcp_bbr *)*ptr; 9971 bbr->rtt_valid = 0; 9972 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 9973 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 9974 /* Take off any undesired flags */ 9975 tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 9976 tp->t_flags2 &= ~TF2_DONT_SACK_QUEUE; 9977 tp->t_flags2 &= ~TF2_MBUF_ACKCMP; 9978 tp->t_flags2 &= ~TF2_MBUF_L_ACKS; 9979 9980 TAILQ_INIT(&bbr->r_ctl.rc_map); 9981 TAILQ_INIT(&bbr->r_ctl.rc_free); 9982 TAILQ_INIT(&bbr->r_ctl.rc_tmap); 9983 bbr->rc_tp = tp; 9984 bbr->rc_inp = inp; 9985 cts = tcp_get_usecs(&bbr->rc_tv); 9986 tp->t_acktime = 0; 9987 bbr->rc_allow_data_af_clo = bbr_ignore_data_after_close; 9988 bbr->r_ctl.rc_reorder_fade = bbr_reorder_fade; 9989 bbr->rc_tlp_threshold = bbr_tlp_thresh; 9990 bbr->r_ctl.rc_reorder_shift = bbr_reorder_thresh; 9991 bbr->r_ctl.rc_pkt_delay = bbr_pkt_delay; 9992 bbr->r_ctl.rc_min_to = bbr_min_to; 9993 bbr->rc_bbr_state = BBR_STATE_STARTUP; 9994 bbr->r_ctl.bbr_lost_at_state = 0; 9995 bbr->r_ctl.rc_lost_at_startup = 0; 9996 bbr->rc_all_timers_stopped = 0; 9997 bbr->r_ctl.rc_bbr_lastbtlbw = 0; 9998 bbr->r_ctl.rc_pkt_epoch_del = 0; 9999 bbr->r_ctl.rc_pkt_epoch = 0; 10000 bbr->r_ctl.rc_lowest_rtt = 0xffffffff; 10001 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_high_gain; 10002 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 10003 bbr->r_ctl.rc_went_idle_time = cts; 10004 bbr->rc_pacer_started = cts; 10005 bbr->r_ctl.rc_pkt_epoch_time = cts; 10006 bbr->r_ctl.rc_rcvtime = cts; 10007 bbr->r_ctl.rc_bbr_state_time = cts; 10008 bbr->r_ctl.rc_del_time = cts; 10009 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 10010 bbr->r_ctl.last_in_probertt = cts; 10011 bbr->skip_gain = 0; 10012 bbr->gain_is_limited = 0; 10013 bbr->no_pacing_until = bbr_no_pacing_until; 10014 if (bbr->no_pacing_until) 10015 bbr->rc_no_pacing = 1; 10016 if (bbr_use_google_algo) { 10017 bbr->rc_no_pacing = 0; 10018 bbr->rc_use_google = 1; 10019 bbr->r_ctl.bbr_google_discount = bbr_google_discount; 10020 bbr->r_use_policer = bbr_policer_detection_enabled; 10021 } else { 10022 bbr->rc_use_google = 0; 10023 bbr->r_ctl.bbr_google_discount = 0; 10024 bbr->r_use_policer = 0; 10025 } 10026 if (bbr_ts_limiting) 10027 bbr->rc_use_ts_limit = 1; 10028 else 10029 bbr->rc_use_ts_limit = 0; 10030 if (bbr_ts_can_raise) 10031 bbr->ts_can_raise = 1; 10032 else 10033 bbr->ts_can_raise = 0; 10034 if (V_tcp_delack_enabled == 1) 10035 tp->t_delayed_ack = 2; 10036 else if (V_tcp_delack_enabled == 0) 10037 tp->t_delayed_ack = 0; 10038 else if (V_tcp_delack_enabled < 100) 10039 tp->t_delayed_ack = V_tcp_delack_enabled; 10040 else 10041 tp->t_delayed_ack = 2; 10042 if (bbr->rc_use_google == 0) 10043 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10044 else 10045 bbr->r_ctl.rc_probertt_int = (USECS_IN_SECOND * 10); 10046 bbr->r_ctl.rc_min_rto_ms = bbr_rto_min_ms; 10047 bbr->rc_max_rto_sec = bbr_rto_max_sec; 10048 bbr->rc_init_win = bbr_def_init_win; 10049 if (tp->t_flags & TF_REQ_TSTMP) 10050 bbr->rc_last_options = TCP_TS_OVERHEAD; 10051 bbr->r_ctl.rc_pace_max_segs = tp->t_maxseg - bbr->rc_last_options; 10052 bbr->r_ctl.rc_high_rwnd = tp->snd_wnd; 10053 bbr->r_init_rtt = 1; 10054 10055 counter_u64_add(bbr_flows_nohdwr_pacing, 1); 10056 if (bbr_allow_hdwr_pacing) 10057 bbr->bbr_hdw_pace_ena = 1; 10058 else 10059 bbr->bbr_hdw_pace_ena = 0; 10060 if (bbr_sends_full_iwnd) 10061 bbr->bbr_init_win_cheat = 1; 10062 else 10063 bbr->bbr_init_win_cheat = 0; 10064 bbr->r_ctl.bbr_utter_max = bbr_hptsi_utter_max; 10065 bbr->r_ctl.rc_drain_pg = bbr_drain_gain; 10066 bbr->r_ctl.rc_startup_pg = bbr_high_gain; 10067 bbr->rc_loss_exit = bbr_exit_startup_at_loss; 10068 bbr->r_ctl.bbr_rttprobe_gain_val = bbr_rttprobe_gain; 10069 bbr->r_ctl.bbr_hptsi_per_second = bbr_hptsi_per_second; 10070 bbr->r_ctl.bbr_hptsi_segments_delay_tar = bbr_hptsi_segments_delay_tar; 10071 bbr->r_ctl.bbr_hptsi_segments_max = bbr_hptsi_segments_max; 10072 bbr->r_ctl.bbr_hptsi_segments_floor = bbr_hptsi_segments_floor; 10073 bbr->r_ctl.bbr_hptsi_bytes_min = bbr_hptsi_bytes_min; 10074 bbr->r_ctl.bbr_cross_over = bbr_cross_over; 10075 bbr->r_ctl.rc_rtt_shrinks = cts; 10076 if (bbr->rc_use_google) { 10077 setup_time_filter(&bbr->r_ctl.rc_delrate, 10078 FILTER_TYPE_MAX, 10079 BBR_NUM_RTTS_FOR_GOOG_DEL_LIMIT); 10080 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10081 FILTER_TYPE_MIN, (11 * USECS_IN_SECOND)); 10082 } else { 10083 setup_time_filter(&bbr->r_ctl.rc_delrate, 10084 FILTER_TYPE_MAX, 10085 bbr_num_pktepo_for_del_limit); 10086 setup_time_filter_small(&bbr->r_ctl.rc_rttprop, 10087 FILTER_TYPE_MIN, (bbr_filter_len_sec * USECS_IN_SECOND)); 10088 } 10089 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_INIT, 0); 10090 if (bbr_uses_idle_restart) 10091 bbr->rc_use_idle_restart = 1; 10092 else 10093 bbr->rc_use_idle_restart = 0; 10094 bbr->r_ctl.rc_bbr_cur_del_rate = 0; 10095 bbr->r_ctl.rc_initial_hptsi_bw = bbr_initial_bw_bps; 10096 if (bbr_resends_use_tso) 10097 bbr->rc_resends_use_tso = 1; 10098 if (tp->snd_una != tp->snd_max) { 10099 /* Create a send map for the current outstanding data */ 10100 struct bbr_sendmap *rsm; 10101 10102 rsm = bbr_alloc(bbr); 10103 if (rsm == NULL) { 10104 uma_zfree(bbr_pcb_zone, *ptr); 10105 *ptr = NULL; 10106 return (ENOMEM); 10107 } 10108 rsm->r_rtt_not_allowed = 1; 10109 rsm->r_tim_lastsent[0] = cts; 10110 rsm->r_rtr_cnt = 1; 10111 rsm->r_rtr_bytes = 0; 10112 rsm->r_start = tp->snd_una; 10113 rsm->r_end = tp->snd_max; 10114 rsm->r_dupack = 0; 10115 rsm->r_delivered = bbr->r_ctl.rc_delivered; 10116 rsm->r_ts_valid = 0; 10117 rsm->r_del_ack_ts = tp->ts_recent; 10118 rsm->r_del_time = cts; 10119 if (bbr->r_ctl.r_app_limited_until) 10120 rsm->r_app_limited = 1; 10121 else 10122 rsm->r_app_limited = 0; 10123 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_map, rsm, r_next); 10124 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_tmap, rsm, r_tnext); 10125 rsm->r_in_tmap = 1; 10126 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) 10127 rsm->r_bbr_state = bbr_state_val(bbr); 10128 else 10129 rsm->r_bbr_state = 8; 10130 } 10131 if (bbr_use_rack_resend_cheat && (bbr->rc_use_google == 0)) 10132 bbr->bbr_use_rack_cheat = 1; 10133 if (bbr_incr_timers && (bbr->rc_use_google == 0)) 10134 bbr->r_ctl.rc_incr_tmrs = 1; 10135 if (bbr_include_tcp_oh && (bbr->rc_use_google == 0)) 10136 bbr->r_ctl.rc_inc_tcp_oh = 1; 10137 if (bbr_include_ip_oh && (bbr->rc_use_google == 0)) 10138 bbr->r_ctl.rc_inc_ip_oh = 1; 10139 if (bbr_include_enet_oh && (bbr->rc_use_google == 0)) 10140 bbr->r_ctl.rc_inc_enet_oh = 1; 10141 10142 bbr_log_type_statechange(bbr, cts, __LINE__); 10143 if (TCPS_HAVEESTABLISHED(tp->t_state) && 10144 (tp->t_srtt)) { 10145 uint32_t rtt; 10146 10147 rtt = (TICKS_2_USEC(tp->t_srtt) >> TCP_RTT_SHIFT); 10148 apply_filter_min_small(&bbr->r_ctl.rc_rttprop, rtt, cts); 10149 } 10150 /* announce the settings and state */ 10151 bbr_log_settings_change(bbr, BBR_RECOVERY_LOWRTT); 10152 tcp_bbr_tso_size_check(bbr, cts); 10153 /* 10154 * Now call the generic function to start a timer. This will place 10155 * the TCB on the hptsi wheel if a timer is needed with appropriate 10156 * flags. 10157 */ 10158 bbr_stop_all_timers(tp, bbr); 10159 /* 10160 * Validate the timers are not in usec, if they are convert. 10161 * BBR should in theory move to USEC and get rid of a 10162 * lot of the TICKS_2 calls.. but for now we stay 10163 * with tick timers. 10164 */ 10165 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 10166 TCPT_RANGESET(tp->t_rxtcur, 10167 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 10168 tp->t_rttmin, TCPTV_REXMTMAX); 10169 bbr_start_hpts_timer(bbr, tp, cts, 5, 0, 0); 10170 return (0); 10171 } 10172 10173 /* 10174 * Return 0 if we can accept the connection. Return 10175 * non-zero if we can't handle the connection. A EAGAIN 10176 * means you need to wait until the connection is up. 10177 * a EADDRNOTAVAIL means we can never handle the connection 10178 * (no SACK). 10179 */ 10180 static int 10181 bbr_handoff_ok(struct tcpcb *tp) 10182 { 10183 if ((tp->t_state == TCPS_CLOSED) || 10184 (tp->t_state == TCPS_LISTEN)) { 10185 /* Sure no problem though it may not stick */ 10186 return (0); 10187 } 10188 if ((tp->t_state == TCPS_SYN_SENT) || 10189 (tp->t_state == TCPS_SYN_RECEIVED)) { 10190 /* 10191 * We really don't know you have to get to ESTAB or beyond 10192 * to tell. 10193 */ 10194 return (EAGAIN); 10195 } 10196 if (tp->t_flags & TF_SENTFIN) 10197 return (EINVAL); 10198 if ((tp->t_flags & TF_SACK_PERMIT) || bbr_sack_not_required) { 10199 return (0); 10200 } 10201 /* 10202 * If we reach here we don't do SACK on this connection so we can 10203 * never do rack. 10204 */ 10205 return (EINVAL); 10206 } 10207 10208 static void 10209 bbr_fini(struct tcpcb *tp, int32_t tcb_is_purged) 10210 { 10211 if (tp->t_fb_ptr) { 10212 uint32_t calc; 10213 struct tcp_bbr *bbr; 10214 struct bbr_sendmap *rsm; 10215 10216 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 10217 if (bbr->r_ctl.crte) 10218 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 10219 bbr_log_flowend(bbr); 10220 bbr->rc_tp = NULL; 10221 if (bbr->bbr_hdrw_pacing) 10222 counter_u64_add(bbr_flows_whdwr_pacing, -1); 10223 else 10224 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 10225 if (bbr->r_ctl.crte != NULL) { 10226 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 10227 bbr->r_ctl.crte = NULL; 10228 } 10229 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10230 while (rsm) { 10231 TAILQ_REMOVE(&bbr->r_ctl.rc_map, rsm, r_next); 10232 uma_zfree(bbr_zone, rsm); 10233 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 10234 } 10235 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10236 while (rsm) { 10237 TAILQ_REMOVE(&bbr->r_ctl.rc_free, rsm, r_next); 10238 uma_zfree(bbr_zone, rsm); 10239 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_free); 10240 } 10241 calc = bbr->r_ctl.rc_high_rwnd - bbr->r_ctl.rc_init_rwnd; 10242 if (calc > (bbr->r_ctl.rc_init_rwnd / 10)) 10243 BBR_STAT_INC(bbr_dynamic_rwnd); 10244 else 10245 BBR_STAT_INC(bbr_static_rwnd); 10246 bbr->r_ctl.rc_free_cnt = 0; 10247 uma_zfree(bbr_pcb_zone, tp->t_fb_ptr); 10248 tp->t_fb_ptr = NULL; 10249 } 10250 /* Make sure snd_nxt is correctly set */ 10251 tp->snd_nxt = tp->snd_max; 10252 } 10253 10254 static void 10255 bbr_set_state(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t win) 10256 { 10257 switch (tp->t_state) { 10258 case TCPS_SYN_SENT: 10259 bbr->r_state = TCPS_SYN_SENT; 10260 bbr->r_substate = bbr_do_syn_sent; 10261 break; 10262 case TCPS_SYN_RECEIVED: 10263 bbr->r_state = TCPS_SYN_RECEIVED; 10264 bbr->r_substate = bbr_do_syn_recv; 10265 break; 10266 case TCPS_ESTABLISHED: 10267 bbr->r_ctl.rc_init_rwnd = max(win, bbr->rc_tp->snd_wnd); 10268 bbr->r_state = TCPS_ESTABLISHED; 10269 bbr->r_substate = bbr_do_established; 10270 break; 10271 case TCPS_CLOSE_WAIT: 10272 bbr->r_state = TCPS_CLOSE_WAIT; 10273 bbr->r_substate = bbr_do_close_wait; 10274 break; 10275 case TCPS_FIN_WAIT_1: 10276 bbr->r_state = TCPS_FIN_WAIT_1; 10277 bbr->r_substate = bbr_do_fin_wait_1; 10278 break; 10279 case TCPS_CLOSING: 10280 bbr->r_state = TCPS_CLOSING; 10281 bbr->r_substate = bbr_do_closing; 10282 break; 10283 case TCPS_LAST_ACK: 10284 bbr->r_state = TCPS_LAST_ACK; 10285 bbr->r_substate = bbr_do_lastack; 10286 break; 10287 case TCPS_FIN_WAIT_2: 10288 bbr->r_state = TCPS_FIN_WAIT_2; 10289 bbr->r_substate = bbr_do_fin_wait_2; 10290 break; 10291 case TCPS_LISTEN: 10292 case TCPS_CLOSED: 10293 case TCPS_TIME_WAIT: 10294 default: 10295 break; 10296 }; 10297 } 10298 10299 static void 10300 bbr_substate_change(struct tcp_bbr *bbr, uint32_t cts, int32_t line, int dolog) 10301 { 10302 /* 10303 * Now what state are we going into now? Is there adjustments 10304 * needed? 10305 */ 10306 int32_t old_state; 10307 10308 old_state = bbr_state_val(bbr); 10309 if (bbr_state_val(bbr) == BBR_SUB_LEVEL1) { 10310 /* Save the lowest srtt we saw in our end of the sub-state */ 10311 bbr->rc_hit_state_1 = 0; 10312 if (bbr->r_ctl.bbr_smallest_srtt_this_state != 0xffffffff) 10313 bbr->r_ctl.bbr_smallest_srtt_state2 = bbr->r_ctl.bbr_smallest_srtt_this_state; 10314 } 10315 bbr->rc_bbr_substate++; 10316 if (bbr->rc_bbr_substate >= BBR_SUBSTATE_COUNT) { 10317 /* Cycle back to first state-> gain */ 10318 bbr->rc_bbr_substate = 0; 10319 } 10320 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10321 /* 10322 * We enter the gain(5/4) cycle (possibly less if 10323 * shallow buffer detection is enabled) 10324 */ 10325 if (bbr->skip_gain) { 10326 /* 10327 * Hardware pacing has set our rate to 10328 * the max and limited our b/w just 10329 * do level i.e. no gain. 10330 */ 10331 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_LEVEL1]; 10332 } else if (bbr->gain_is_limited && 10333 bbr->bbr_hdrw_pacing && 10334 bbr->r_ctl.crte) { 10335 /* 10336 * We can't gain above the hardware pacing 10337 * rate which is less than our rate + the gain 10338 * calculate the gain needed to reach the hardware 10339 * pacing rate.. 10340 */ 10341 uint64_t bw, rate, gain_calc; 10342 10343 bw = bbr_get_bw(bbr); 10344 rate = bbr->r_ctl.crte->rate; 10345 if ((rate > bw) && 10346 (((bw * (uint64_t)bbr_hptsi_gain[BBR_SUB_GAIN]) / (uint64_t)BBR_UNIT) > rate)) { 10347 gain_calc = (rate * BBR_UNIT) / bw; 10348 if (gain_calc < BBR_UNIT) 10349 gain_calc = BBR_UNIT; 10350 bbr->r_ctl.rc_bbr_hptsi_gain = (uint16_t)gain_calc; 10351 } else { 10352 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10353 } 10354 } else 10355 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_GAIN]; 10356 if ((bbr->rc_use_google == 0) && (bbr_gain_to_target == 0)) { 10357 bbr->r_ctl.rc_bbr_state_atflight = cts; 10358 } else 10359 bbr->r_ctl.rc_bbr_state_atflight = 0; 10360 } else if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10361 bbr->rc_hit_state_1 = 1; 10362 bbr->r_ctl.rc_exta_time_gd = 0; 10363 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10364 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10365 if (bbr_state_drain_2_tar) { 10366 bbr->r_ctl.rc_bbr_state_atflight = 0; 10367 } else 10368 bbr->r_ctl.rc_bbr_state_atflight = cts; 10369 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[BBR_SUB_DRAIN]; 10370 } else { 10371 /* All other cycles hit here 2-7 */ 10372 if ((old_state == BBR_SUB_DRAIN) && bbr->rc_hit_state_1) { 10373 if (bbr_sub_drain_slam_cwnd && 10374 (bbr->rc_use_google == 0) && 10375 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10376 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10377 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10378 } 10379 if ((cts - bbr->r_ctl.rc_bbr_state_time) > bbr_get_rtt(bbr, BBR_RTT_PROP)) 10380 bbr->r_ctl.rc_exta_time_gd += ((cts - bbr->r_ctl.rc_bbr_state_time) - 10381 bbr_get_rtt(bbr, BBR_RTT_PROP)); 10382 else 10383 bbr->r_ctl.rc_exta_time_gd = 0; 10384 if (bbr->r_ctl.rc_exta_time_gd) { 10385 bbr->r_ctl.rc_level_state_extra = bbr->r_ctl.rc_exta_time_gd; 10386 /* Now chop up the time for each state (div by 7) */ 10387 bbr->r_ctl.rc_level_state_extra /= 7; 10388 if (bbr_rand_ot && bbr->r_ctl.rc_level_state_extra) { 10389 /* Add a randomization */ 10390 bbr_randomize_extra_state_time(bbr); 10391 } 10392 } 10393 } 10394 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10395 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_hptsi_gain[bbr_state_val(bbr)]; 10396 } 10397 if (bbr->rc_use_google) { 10398 bbr->r_ctl.rc_bbr_state_atflight = max(1, cts); 10399 } 10400 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10401 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10402 if (dolog) 10403 bbr_log_type_statechange(bbr, cts, line); 10404 10405 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10406 uint32_t time_in; 10407 10408 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10409 if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 10410 counter_u64_add(bbr_state_time[(old_state + 5)], time_in); 10411 } else { 10412 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10413 } 10414 } 10415 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 10416 bbr_set_state_target(bbr, __LINE__); 10417 if (bbr_sub_drain_slam_cwnd && 10418 (bbr->rc_use_google == 0) && 10419 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10420 /* Slam down the cwnd */ 10421 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10422 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10423 if (bbr_sub_drain_app_limit) { 10424 /* Go app limited if we are on a long drain */ 10425 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + 10426 ctf_flight_size(bbr->rc_tp, 10427 (bbr->r_ctl.rc_sacked + 10428 bbr->r_ctl.rc_lost_bytes))); 10429 } 10430 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10431 } 10432 if (bbr->rc_lt_use_bw) { 10433 /* In policed mode we clamp pacing_gain to BBR_UNIT */ 10434 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10435 } 10436 /* Google changes TSO size every cycle */ 10437 if (bbr->rc_use_google) 10438 tcp_bbr_tso_size_check(bbr, cts); 10439 bbr->r_ctl.gain_epoch = cts; 10440 bbr->r_ctl.rc_bbr_state_time = cts; 10441 bbr->r_ctl.substate_pe = bbr->r_ctl.rc_pkt_epoch; 10442 } 10443 10444 static void 10445 bbr_set_probebw_google_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10446 { 10447 if ((bbr_state_val(bbr) == BBR_SUB_DRAIN) && 10448 (google_allow_early_out == 1) && 10449 (bbr->r_ctl.rc_flight_at_input <= bbr->r_ctl.rc_target_at_state)) { 10450 /* We have reached out target flight size possibly early */ 10451 goto change_state; 10452 } 10453 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10454 return; 10455 } 10456 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_get_rtt(bbr, BBR_RTT_PROP)) { 10457 /* 10458 * Must be a rttProp movement forward before 10459 * we can change states. 10460 */ 10461 return; 10462 } 10463 if (bbr_state_val(bbr) == BBR_SUB_GAIN) { 10464 /* 10465 * The needed time has passed but for 10466 * the gain cycle extra rules apply: 10467 * 1) If we have seen loss, we exit 10468 * 2) If we have not reached the target 10469 * we stay in GAIN (gain-to-target). 10470 */ 10471 if (google_consider_lost && losses) 10472 goto change_state; 10473 if (bbr->r_ctl.rc_target_at_state > bbr->r_ctl.rc_flight_at_input) { 10474 return; 10475 } 10476 } 10477 change_state: 10478 /* For gain we must reach our target, all others last 1 rttProp */ 10479 bbr_substate_change(bbr, cts, __LINE__, 1); 10480 } 10481 10482 static void 10483 bbr_set_probebw_gains(struct tcp_bbr *bbr, uint32_t cts, uint32_t losses) 10484 { 10485 uint32_t flight, bbr_cur_cycle_time; 10486 10487 if (bbr->rc_use_google) { 10488 bbr_set_probebw_google_gains(bbr, cts, losses); 10489 return; 10490 } 10491 if (cts == 0) { 10492 /* 10493 * Never alow cts to be 0 we 10494 * do this so we can judge if 10495 * we have set a timestamp. 10496 */ 10497 cts = 1; 10498 } 10499 if (bbr_state_is_pkt_epoch) 10500 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PKTRTT); 10501 else 10502 bbr_cur_cycle_time = bbr_get_rtt(bbr, BBR_RTT_PROP); 10503 10504 if (bbr->r_ctl.rc_bbr_state_atflight == 0) { 10505 if (bbr_state_val(bbr) == BBR_SUB_DRAIN) { 10506 flight = ctf_flight_size(bbr->rc_tp, 10507 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10508 if (bbr_sub_drain_slam_cwnd && bbr->rc_hit_state_1) { 10509 /* Keep it slam down */ 10510 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state) { 10511 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10512 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10513 } 10514 if (bbr_sub_drain_app_limit) { 10515 /* Go app limited if we are on a long drain */ 10516 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.rc_delivered + flight); 10517 } 10518 } 10519 if (TSTMP_GT(cts, bbr->r_ctl.gain_epoch) && 10520 (((cts - bbr->r_ctl.gain_epoch) > bbr_get_rtt(bbr, BBR_RTT_PROP)) || 10521 (flight >= bbr->r_ctl.flightsize_at_drain))) { 10522 /* 10523 * Still here after the same time as 10524 * the gain. We need to drain harder 10525 * for the next srtt. Reduce by a set amount 10526 * the gain drop is capped at DRAIN states 10527 * value (88). 10528 */ 10529 bbr->r_ctl.flightsize_at_drain = flight; 10530 if (bbr_drain_drop_mul && 10531 bbr_drain_drop_div && 10532 (bbr_drain_drop_mul < bbr_drain_drop_div)) { 10533 /* Use your specific drop value (def 4/5 = 20%) */ 10534 bbr->r_ctl.rc_bbr_hptsi_gain *= bbr_drain_drop_mul; 10535 bbr->r_ctl.rc_bbr_hptsi_gain /= bbr_drain_drop_div; 10536 } else { 10537 /* You get drop of 20% */ 10538 bbr->r_ctl.rc_bbr_hptsi_gain *= 4; 10539 bbr->r_ctl.rc_bbr_hptsi_gain /= 5; 10540 } 10541 if (bbr->r_ctl.rc_bbr_hptsi_gain <= bbr_drain_floor) { 10542 /* Reduce our gain again to the bottom */ 10543 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 10544 } 10545 bbr_log_exit_gain(bbr, cts, 4); 10546 /* 10547 * Extend out so we wait another 10548 * epoch before dropping again. 10549 */ 10550 bbr->r_ctl.gain_epoch = cts; 10551 } 10552 if (flight <= bbr->r_ctl.rc_target_at_state) { 10553 if (bbr_sub_drain_slam_cwnd && 10554 (bbr->rc_use_google == 0) && 10555 (bbr->rc_tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 10556 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10557 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10558 } 10559 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10560 bbr_log_exit_gain(bbr, cts, 3); 10561 } 10562 } else { 10563 /* Its a gain */ 10564 if (bbr->r_ctl.rc_lost > bbr->r_ctl.bbr_lost_at_state) { 10565 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10566 goto change_state; 10567 } 10568 if ((ctf_outstanding(bbr->rc_tp) >= bbr->r_ctl.rc_target_at_state) || 10569 ((ctf_outstanding(bbr->rc_tp) + bbr->rc_tp->t_maxseg - 1) >= 10570 bbr->rc_tp->snd_wnd)) { 10571 bbr->r_ctl.rc_bbr_state_atflight = max(cts, 1); 10572 bbr_log_exit_gain(bbr, cts, 2); 10573 } 10574 } 10575 /** 10576 * We fall through and return always one of two things has 10577 * occurred. 10578 * 1) We are still not at target 10579 * <or> 10580 * 2) We reached the target and set rc_bbr_state_atflight 10581 * which means we no longer hit this block 10582 * next time we are called. 10583 */ 10584 return; 10585 } 10586 change_state: 10587 if (TSTMP_LT(cts, bbr->r_ctl.rc_bbr_state_time)) 10588 return; 10589 if ((cts - bbr->r_ctl.rc_bbr_state_time) < bbr_cur_cycle_time) { 10590 /* Less than a full time-period has passed */ 10591 return; 10592 } 10593 if (bbr->r_ctl.rc_level_state_extra && 10594 (bbr_state_val(bbr) > BBR_SUB_DRAIN) && 10595 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10596 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10597 /* Less than a full time-period + extra has passed */ 10598 return; 10599 } 10600 if (bbr_gain_gets_extra_too && 10601 bbr->r_ctl.rc_level_state_extra && 10602 (bbr_state_val(bbr) == BBR_SUB_GAIN) && 10603 ((cts - bbr->r_ctl.rc_bbr_state_time) < 10604 (bbr_cur_cycle_time + bbr->r_ctl.rc_level_state_extra))) { 10605 /* Less than a full time-period + extra has passed */ 10606 return; 10607 } 10608 bbr_substate_change(bbr, cts, __LINE__, 1); 10609 } 10610 10611 static uint32_t 10612 bbr_get_a_state_target(struct tcp_bbr *bbr, uint32_t gain) 10613 { 10614 uint32_t mss, tar; 10615 10616 if (bbr->rc_use_google) { 10617 /* Google just uses the cwnd target */ 10618 tar = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), gain); 10619 } else { 10620 mss = min((bbr->rc_tp->t_maxseg - bbr->rc_last_options), 10621 bbr->r_ctl.rc_pace_max_segs); 10622 /* Get the base cwnd with gain rounded to a mss */ 10623 tar = roundup(bbr_get_raw_target_cwnd(bbr, bbr_get_bw(bbr), 10624 gain), mss); 10625 /* Make sure it is within our min */ 10626 if (tar < get_min_cwnd(bbr)) 10627 return (get_min_cwnd(bbr)); 10628 } 10629 return (tar); 10630 } 10631 10632 static void 10633 bbr_set_state_target(struct tcp_bbr *bbr, int line) 10634 { 10635 uint32_t tar, meth; 10636 10637 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) && 10638 ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google)) { 10639 /* Special case using old probe-rtt method */ 10640 tar = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10641 meth = 1; 10642 } else { 10643 /* Non-probe-rtt case and reduced probe-rtt */ 10644 if ((bbr->rc_bbr_state == BBR_STATE_PROBE_BW) && 10645 (bbr->r_ctl.rc_bbr_hptsi_gain > BBR_UNIT)) { 10646 /* For gain cycle we use the hptsi gain */ 10647 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10648 meth = 2; 10649 } else if ((bbr_target_is_bbunit) || bbr->rc_use_google) { 10650 /* 10651 * If configured, or for google all other states 10652 * get BBR_UNIT. 10653 */ 10654 tar = bbr_get_a_state_target(bbr, BBR_UNIT); 10655 meth = 3; 10656 } else { 10657 /* 10658 * Or we set a target based on the pacing gain 10659 * for non-google mode and default (non-configured). 10660 * Note we don't set a target goal below drain (192). 10661 */ 10662 if (bbr->r_ctl.rc_bbr_hptsi_gain < bbr_hptsi_gain[BBR_SUB_DRAIN]) { 10663 tar = bbr_get_a_state_target(bbr, bbr_hptsi_gain[BBR_SUB_DRAIN]); 10664 meth = 4; 10665 } else { 10666 tar = bbr_get_a_state_target(bbr, bbr->r_ctl.rc_bbr_hptsi_gain); 10667 meth = 5; 10668 } 10669 } 10670 } 10671 bbr_log_set_of_state_target(bbr, tar, line, meth); 10672 bbr->r_ctl.rc_target_at_state = tar; 10673 } 10674 10675 static void 10676 bbr_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts, int32_t line) 10677 { 10678 /* Change to probe_rtt */ 10679 uint32_t time_in; 10680 10681 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10682 bbr->r_ctl.flightsize_at_drain = ctf_flight_size(bbr->rc_tp, 10683 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 10684 bbr->r_ctl.r_app_limited_until = (bbr->r_ctl.flightsize_at_drain 10685 + bbr->r_ctl.rc_delivered); 10686 /* Setup so we force feed the filter */ 10687 if (bbr->rc_use_google || bbr_probertt_sets_rtt) 10688 bbr->rc_prtt_set_ts = 1; 10689 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10690 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10691 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10692 } 10693 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_ENTERPROBE, 0); 10694 bbr->r_ctl.rc_rtt_shrinks = cts; 10695 bbr->r_ctl.last_in_probertt = cts; 10696 bbr->r_ctl.rc_probertt_srttchktim = cts; 10697 bbr->r_ctl.rc_bbr_state_time = cts; 10698 bbr->rc_bbr_state = BBR_STATE_PROBE_RTT; 10699 /* We need to force the filter to update */ 10700 10701 if ((bbr_sub_drain_slam_cwnd) && 10702 bbr->rc_hit_state_1 && 10703 (bbr->rc_use_google == 0) && 10704 (bbr_state_val(bbr) == BBR_SUB_DRAIN)) { 10705 if (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_saved_cwnd) 10706 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10707 } else 10708 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 10709 /* Update the lost */ 10710 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10711 if ((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google){ 10712 /* Set to the non-configurable default of 4 (PROBE_RTT_MIN) */ 10713 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 10714 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10715 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 10716 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10717 bbr_log_set_of_state_target(bbr, bbr->rc_tp->snd_cwnd, __LINE__, 6); 10718 bbr->r_ctl.rc_target_at_state = bbr->rc_tp->snd_cwnd; 10719 } else { 10720 /* 10721 * We bring it down slowly by using a hptsi gain that is 10722 * probably 75%. This will slowly float down our outstanding 10723 * without tampering with the cwnd. 10724 */ 10725 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 10726 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 10727 bbr_set_state_target(bbr, __LINE__); 10728 if (bbr_prtt_slam_cwnd && 10729 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 10730 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 10731 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10732 } 10733 } 10734 if (ctf_flight_size(bbr->rc_tp, 10735 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 10736 bbr->r_ctl.rc_target_at_state) { 10737 /* We are at target */ 10738 bbr->r_ctl.rc_bbr_enters_probertt = cts; 10739 } else { 10740 /* We need to come down to reach target before our time begins */ 10741 bbr->r_ctl.rc_bbr_enters_probertt = 0; 10742 } 10743 bbr->r_ctl.rc_pe_of_prtt = bbr->r_ctl.rc_pkt_epoch; 10744 BBR_STAT_INC(bbr_enter_probertt); 10745 bbr_log_exit_gain(bbr, cts, 0); 10746 bbr_log_type_statechange(bbr, cts, line); 10747 } 10748 10749 static void 10750 bbr_check_probe_rtt_limits(struct tcp_bbr *bbr, uint32_t cts) 10751 { 10752 /* 10753 * Sanity check on probe-rtt intervals. 10754 * In crazy situations where we are competing 10755 * against new-reno flows with huge buffers 10756 * our rtt-prop interval could come to dominate 10757 * things if we can't get through a full set 10758 * of cycles, we need to adjust it. 10759 */ 10760 if (bbr_can_adjust_probertt && 10761 (bbr->rc_use_google == 0)) { 10762 uint16_t val = 0; 10763 uint32_t cur_rttp, fval, newval, baseval; 10764 10765 /* Are we to small and go into probe-rtt to often? */ 10766 baseval = (bbr_get_rtt(bbr, BBR_RTT_PROP) * (BBR_SUBSTATE_COUNT + 1)); 10767 cur_rttp = roundup(baseval, USECS_IN_SECOND); 10768 fval = bbr_filter_len_sec * USECS_IN_SECOND; 10769 if (bbr_is_ratio == 0) { 10770 if (fval > bbr_rtt_probe_limit) 10771 newval = cur_rttp + (fval - bbr_rtt_probe_limit); 10772 else 10773 newval = cur_rttp; 10774 } else { 10775 int mul; 10776 10777 mul = fval / bbr_rtt_probe_limit; 10778 newval = cur_rttp * mul; 10779 } 10780 if (cur_rttp > bbr->r_ctl.rc_probertt_int) { 10781 bbr->r_ctl.rc_probertt_int = cur_rttp; 10782 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10783 val = 1; 10784 } else { 10785 /* 10786 * No adjustments were made 10787 * do we need to shrink it? 10788 */ 10789 if (bbr->r_ctl.rc_probertt_int > bbr_rtt_probe_limit) { 10790 if (cur_rttp <= bbr_rtt_probe_limit) { 10791 /* 10792 * Things have calmed down lets 10793 * shrink all the way to default 10794 */ 10795 bbr->r_ctl.rc_probertt_int = bbr_rtt_probe_limit; 10796 reset_time_small(&bbr->r_ctl.rc_rttprop, 10797 (bbr_filter_len_sec * USECS_IN_SECOND)); 10798 cur_rttp = bbr_rtt_probe_limit; 10799 newval = (bbr_filter_len_sec * USECS_IN_SECOND); 10800 val = 2; 10801 } else { 10802 /* 10803 * Well does some adjustment make sense? 10804 */ 10805 if (cur_rttp < bbr->r_ctl.rc_probertt_int) { 10806 /* We can reduce interval time some */ 10807 bbr->r_ctl.rc_probertt_int = cur_rttp; 10808 reset_time_small(&bbr->r_ctl.rc_rttprop, newval); 10809 val = 3; 10810 } 10811 } 10812 } 10813 } 10814 if (val) 10815 bbr_log_rtt_shrinks(bbr, cts, cur_rttp, newval, __LINE__, BBR_RTTS_RESETS_VALUES, val); 10816 } 10817 } 10818 10819 static void 10820 bbr_exit_probe_rtt(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t cts) 10821 { 10822 /* Exit probe-rtt */ 10823 10824 if (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd) { 10825 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 10826 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 10827 } 10828 bbr_log_exit_gain(bbr, cts, 1); 10829 bbr->rc_hit_state_1 = 0; 10830 bbr->r_ctl.rc_rtt_shrinks = cts; 10831 bbr->r_ctl.last_in_probertt = cts; 10832 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_RTTPROBE, 0); 10833 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 10834 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, 10835 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 10836 bbr->r_ctl.rc_delivered); 10837 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 10838 uint32_t time_in; 10839 10840 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 10841 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 10842 } 10843 if (bbr->rc_filled_pipe) { 10844 /* Switch to probe_bw */ 10845 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 10846 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 10847 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_cwnd_gain; 10848 bbr_substate_change(bbr, cts, __LINE__, 0); 10849 bbr_log_type_statechange(bbr, cts, __LINE__); 10850 } else { 10851 /* Back to startup */ 10852 bbr->rc_bbr_state = BBR_STATE_STARTUP; 10853 bbr->r_ctl.rc_bbr_state_time = cts; 10854 /* 10855 * We don't want to give a complete free 3 10856 * measurements until we exit, so we use 10857 * the number of pe's we were in probe-rtt 10858 * to add to the startup_epoch. That way 10859 * we will still retain the old state. 10860 */ 10861 bbr->r_ctl.rc_bbr_last_startup_epoch += (bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_pe_of_prtt); 10862 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 10863 /* Make sure to use the lower pg when shifting back in */ 10864 if (bbr->r_ctl.rc_lost && 10865 bbr_use_lower_gain_in_startup && 10866 (bbr->rc_use_google == 0)) 10867 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10868 else 10869 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_startup_pg; 10870 bbr->r_ctl.rc_bbr_cwnd_gain = bbr->r_ctl.rc_startup_pg; 10871 /* Probably not needed but set it anyway */ 10872 bbr_set_state_target(bbr, __LINE__); 10873 bbr_log_type_statechange(bbr, cts, __LINE__); 10874 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10875 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 0); 10876 } 10877 bbr_check_probe_rtt_limits(bbr, cts); 10878 } 10879 10880 static int32_t inline 10881 bbr_should_enter_probe_rtt(struct tcp_bbr *bbr, uint32_t cts) 10882 { 10883 if ((bbr->rc_past_init_win == 1) && 10884 (bbr->rc_in_persist == 0) && 10885 (bbr_calc_time(cts, bbr->r_ctl.rc_rtt_shrinks) >= bbr->r_ctl.rc_probertt_int)) { 10886 return (1); 10887 } 10888 if (bbr_can_force_probertt && 10889 (bbr->rc_in_persist == 0) && 10890 (TSTMP_GT(cts, bbr->r_ctl.last_in_probertt)) && 10891 ((cts - bbr->r_ctl.last_in_probertt) > bbr->r_ctl.rc_probertt_int)) { 10892 return (1); 10893 } 10894 return (0); 10895 } 10896 10897 static int32_t 10898 bbr_google_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t pkt_epoch) 10899 { 10900 uint64_t btlbw, gain; 10901 if (pkt_epoch == 0) { 10902 /* 10903 * Need to be on a pkt-epoch to continue. 10904 */ 10905 return (0); 10906 } 10907 btlbw = bbr_get_full_bw(bbr); 10908 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 10909 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 10910 if (btlbw >= gain) { 10911 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 10912 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10913 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 10914 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 10915 } 10916 if ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) 10917 return (1); 10918 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 10919 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 10920 return(0); 10921 } 10922 10923 static int32_t inline 10924 bbr_state_startup(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch) 10925 { 10926 /* Have we gained 25% in the last 3 packet based epoch's? */ 10927 uint64_t btlbw, gain; 10928 int do_exit; 10929 int delta, rtt_gain; 10930 10931 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 10932 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 10933 /* 10934 * This qualifies as a RTT_PROBE session since we drop the 10935 * data outstanding to nothing and waited more than 10936 * bbr_rtt_probe_time. 10937 */ 10938 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 10939 bbr_set_reduced_rtt(bbr, cts, __LINE__); 10940 } 10941 if (bbr_should_enter_probe_rtt(bbr, cts)) { 10942 bbr_enter_probe_rtt(bbr, cts, __LINE__); 10943 return (0); 10944 } 10945 if (bbr->rc_use_google) 10946 return (bbr_google_startup(bbr, cts, pkt_epoch)); 10947 10948 if ((bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 10949 (bbr_use_lower_gain_in_startup)) { 10950 /* Drop to a lower gain 1.5 x since we saw loss */ 10951 bbr->r_ctl.rc_bbr_hptsi_gain = bbr_startup_lower; 10952 } 10953 if (pkt_epoch == 0) { 10954 /* 10955 * Need to be on a pkt-epoch to continue. 10956 */ 10957 return (0); 10958 } 10959 if (bbr_rtt_gain_thresh) { 10960 /* 10961 * Do we allow a flow to stay 10962 * in startup with no loss and no 10963 * gain in rtt over a set threshold? 10964 */ 10965 if (bbr->r_ctl.rc_pkt_epoch_rtt && 10966 bbr->r_ctl.startup_last_srtt && 10967 (bbr->r_ctl.rc_pkt_epoch_rtt > bbr->r_ctl.startup_last_srtt)) { 10968 delta = bbr->r_ctl.rc_pkt_epoch_rtt - bbr->r_ctl.startup_last_srtt; 10969 rtt_gain = (delta * 100) / bbr->r_ctl.startup_last_srtt; 10970 } else 10971 rtt_gain = 0; 10972 if ((bbr->r_ctl.startup_last_srtt == 0) || 10973 (bbr->r_ctl.rc_pkt_epoch_rtt < bbr->r_ctl.startup_last_srtt)) 10974 /* First time or new lower value */ 10975 bbr->r_ctl.startup_last_srtt = bbr->r_ctl.rc_pkt_epoch_rtt; 10976 10977 if ((bbr->r_ctl.rc_lost == 0) && 10978 (rtt_gain < bbr_rtt_gain_thresh)) { 10979 /* 10980 * No loss, and we are under 10981 * our gain threhold for 10982 * increasing RTT. 10983 */ 10984 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 10985 bbr->r_ctl.rc_bbr_last_startup_epoch++; 10986 bbr_log_startup_event(bbr, cts, rtt_gain, 10987 delta, bbr->r_ctl.startup_last_srtt, 10); 10988 return (0); 10989 } 10990 } 10991 if ((bbr->r_ctl.r_measurement_count == bbr->r_ctl.last_startup_measure) && 10992 (bbr->r_ctl.rc_lost_at_startup == bbr->r_ctl.rc_lost) && 10993 (!IN_RECOVERY(bbr->rc_tp->t_flags))) { 10994 /* 10995 * We only assess if we have a new measurement when 10996 * we have no loss and are not in recovery. 10997 * Drag up by one our last_startup epoch so we will hold 10998 * the number of non-gain we have already accumulated. 10999 */ 11000 if (bbr->r_ctl.rc_bbr_last_startup_epoch < bbr->r_ctl.rc_pkt_epoch) 11001 bbr->r_ctl.rc_bbr_last_startup_epoch++; 11002 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11003 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 9); 11004 return (0); 11005 } 11006 /* Case where we reduced the lost (bad retransmit) */ 11007 if (bbr->r_ctl.rc_lost_at_startup > bbr->r_ctl.rc_lost) 11008 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11009 bbr->r_ctl.last_startup_measure = bbr->r_ctl.r_measurement_count; 11010 btlbw = bbr_get_full_bw(bbr); 11011 if (bbr->r_ctl.rc_bbr_hptsi_gain == bbr_startup_lower) 11012 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 11013 (uint64_t)bbr_low_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 11014 else 11015 gain = ((bbr->r_ctl.rc_bbr_lastbtlbw * 11016 (uint64_t)bbr_start_exit) / (uint64_t)100) + bbr->r_ctl.rc_bbr_lastbtlbw; 11017 do_exit = 0; 11018 if (btlbw > bbr->r_ctl.rc_bbr_lastbtlbw) 11019 bbr->r_ctl.rc_bbr_lastbtlbw = btlbw; 11020 if (btlbw >= gain) { 11021 bbr->r_ctl.rc_bbr_last_startup_epoch = bbr->r_ctl.rc_pkt_epoch; 11022 /* Update the lost so we won't exit in next set of tests */ 11023 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11024 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11025 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 3); 11026 } 11027 if ((bbr->rc_loss_exit && 11028 (bbr->r_ctl.rc_lost > bbr->r_ctl.rc_lost_at_startup) && 11029 (bbr->r_ctl.rc_pkt_epoch_loss_rate > bbr_startup_loss_thresh)) && 11030 ((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS)) { 11031 /* 11032 * If we had no gain, we had loss and that loss was above 11033 * our threshould, the rwnd is not constrained, and we have 11034 * had at least 3 packet epochs exit. Note that this is 11035 * switched off by sysctl. Google does not do this by the 11036 * way. 11037 */ 11038 if ((ctf_flight_size(bbr->rc_tp, 11039 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) + 11040 (2 * max(bbr->r_ctl.rc_pace_max_segs, bbr->rc_tp->t_maxseg))) <= bbr->rc_tp->snd_wnd) { 11041 do_exit = 1; 11042 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11043 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 4); 11044 } else { 11045 /* Just record an updated loss value */ 11046 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11047 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11048 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 5); 11049 } 11050 } else 11051 bbr->r_ctl.rc_lost_at_startup = bbr->r_ctl.rc_lost; 11052 if (((bbr->r_ctl.rc_pkt_epoch - bbr->r_ctl.rc_bbr_last_startup_epoch) >= BBR_STARTUP_EPOCHS) || 11053 do_exit) { 11054 /* Return 1 to exit the startup state. */ 11055 return (1); 11056 } 11057 /* Stay in startup */ 11058 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11059 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 8); 11060 return (0); 11061 } 11062 11063 static void 11064 bbr_state_change(struct tcp_bbr *bbr, uint32_t cts, int32_t epoch, int32_t pkt_epoch, uint32_t losses) 11065 { 11066 /* 11067 * A tick occurred in the rtt epoch do we need to do anything? 11068 */ 11069 #ifdef BBR_INVARIANTS 11070 if ((bbr->rc_bbr_state != BBR_STATE_STARTUP) && 11071 (bbr->rc_bbr_state != BBR_STATE_DRAIN) && 11072 (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) && 11073 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 11074 (bbr->rc_bbr_state != BBR_STATE_PROBE_BW)) { 11075 /* Debug code? */ 11076 panic("Unknown BBR state %d?\n", bbr->rc_bbr_state); 11077 } 11078 #endif 11079 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 11080 /* Do we exit the startup state? */ 11081 if (bbr_state_startup(bbr, cts, epoch, pkt_epoch)) { 11082 uint32_t time_in; 11083 11084 bbr_log_startup_event(bbr, cts, bbr->r_ctl.rc_bbr_last_startup_epoch, 11085 bbr->r_ctl.rc_lost_at_startup, bbr_start_exit, 6); 11086 bbr->rc_filled_pipe = 1; 11087 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11088 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11089 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11090 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11091 } else 11092 time_in = 0; 11093 if (bbr->rc_no_pacing) 11094 bbr->rc_no_pacing = 0; 11095 bbr->r_ctl.rc_bbr_state_time = cts; 11096 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.rc_drain_pg; 11097 bbr->rc_bbr_state = BBR_STATE_DRAIN; 11098 bbr_set_state_target(bbr, __LINE__); 11099 if ((bbr->rc_use_google == 0) && 11100 bbr_slam_cwnd_in_main_drain) { 11101 /* Here we don't have to worry about probe-rtt */ 11102 bbr->r_ctl.rc_saved_cwnd = bbr->rc_tp->snd_cwnd; 11103 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11104 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11105 } 11106 bbr->r_ctl.rc_bbr_cwnd_gain = bbr_high_gain; 11107 bbr_log_type_statechange(bbr, cts, __LINE__); 11108 if (ctf_flight_size(bbr->rc_tp, 11109 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)) <= 11110 bbr->r_ctl.rc_target_at_state) { 11111 /* 11112 * Switch to probe_bw if we are already 11113 * there 11114 */ 11115 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11116 bbr_substate_change(bbr, cts, __LINE__, 0); 11117 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11118 bbr_log_type_statechange(bbr, cts, __LINE__); 11119 } 11120 } 11121 } else if (bbr->rc_bbr_state == BBR_STATE_IDLE_EXIT) { 11122 uint32_t inflight; 11123 struct tcpcb *tp; 11124 11125 tp = bbr->rc_tp; 11126 inflight = ctf_flight_size(tp, 11127 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11128 if (inflight >= bbr->r_ctl.rc_target_at_state) { 11129 /* We have reached a flight of the cwnd target */ 11130 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11131 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11132 bbr->r_ctl.rc_bbr_cwnd_gain = BBR_UNIT; 11133 bbr_set_state_target(bbr, __LINE__); 11134 /* 11135 * Rig it so we don't do anything crazy and 11136 * start fresh with a new randomization. 11137 */ 11138 bbr->r_ctl.bbr_smallest_srtt_this_state = 0xffffffff; 11139 bbr->rc_bbr_substate = BBR_SUB_LEVEL6; 11140 bbr_substate_change(bbr, cts, __LINE__, 1); 11141 } 11142 } else if (bbr->rc_bbr_state == BBR_STATE_DRAIN) { 11143 /* Has in-flight reached the bdp (or less)? */ 11144 uint32_t inflight; 11145 struct tcpcb *tp; 11146 11147 tp = bbr->rc_tp; 11148 inflight = ctf_flight_size(tp, 11149 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11150 if ((bbr->rc_use_google == 0) && 11151 bbr_slam_cwnd_in_main_drain && 11152 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11153 /* 11154 * Here we don't have to worry about probe-rtt 11155 * re-slam it, but keep it slammed down. 11156 */ 11157 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11158 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11159 } 11160 if (inflight <= bbr->r_ctl.rc_target_at_state) { 11161 /* We have drained */ 11162 bbr->rc_bbr_state = BBR_STATE_PROBE_BW; 11163 bbr->r_ctl.bbr_lost_at_state = bbr->r_ctl.rc_lost; 11164 if (SEQ_GT(cts, bbr->r_ctl.rc_bbr_state_time)) { 11165 uint32_t time_in; 11166 11167 time_in = cts - bbr->r_ctl.rc_bbr_state_time; 11168 counter_u64_add(bbr_state_time[bbr->rc_bbr_state], time_in); 11169 } 11170 if ((bbr->rc_use_google == 0) && 11171 bbr_slam_cwnd_in_main_drain && 11172 (tp->snd_cwnd < bbr->r_ctl.rc_saved_cwnd)) { 11173 /* Restore the cwnd */ 11174 tp->snd_cwnd = bbr->r_ctl.rc_saved_cwnd; 11175 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11176 } 11177 /* Setup probe-rtt has being done now RRS-HERE */ 11178 bbr->r_ctl.rc_rtt_shrinks = cts; 11179 bbr->r_ctl.last_in_probertt = cts; 11180 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_LEAVE_DRAIN, 0); 11181 /* Randomly pick a sub-state */ 11182 bbr->rc_bbr_substate = bbr_pick_probebw_substate(bbr, cts); 11183 bbr_substate_change(bbr, cts, __LINE__, 0); 11184 bbr_log_type_statechange(bbr, cts, __LINE__); 11185 } 11186 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_RTT) { 11187 uint32_t flight; 11188 11189 flight = ctf_flight_size(bbr->rc_tp, 11190 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11191 bbr->r_ctl.r_app_limited_until = (flight + bbr->r_ctl.rc_delivered); 11192 if (((bbr->r_ctl.bbr_rttprobe_gain_val == 0) || bbr->rc_use_google) && 11193 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11194 /* 11195 * We must keep cwnd at the desired MSS. 11196 */ 11197 bbr->rc_tp->snd_cwnd = bbr_rtt_probe_cwndtarg * (bbr->rc_tp->t_maxseg - bbr->rc_last_options); 11198 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11199 } else if ((bbr_prtt_slam_cwnd) && 11200 (bbr->rc_tp->snd_cwnd > bbr->r_ctl.rc_target_at_state)) { 11201 /* Re-slam it */ 11202 bbr->rc_tp->snd_cwnd = bbr->r_ctl.rc_target_at_state; 11203 bbr_log_type_cwndupd(bbr, 0, 0, 0, 12, 0, 0, __LINE__); 11204 } 11205 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) { 11206 /* Has outstanding reached our target? */ 11207 if (flight <= bbr->r_ctl.rc_target_at_state) { 11208 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_REACHTAR, 0); 11209 bbr->r_ctl.rc_bbr_enters_probertt = cts; 11210 /* If time is exactly 0, be 1usec off */ 11211 if (bbr->r_ctl.rc_bbr_enters_probertt == 0) 11212 bbr->r_ctl.rc_bbr_enters_probertt = 1; 11213 if (bbr->rc_use_google == 0) { 11214 /* 11215 * Restore any lowering that as occurred to 11216 * reach here 11217 */ 11218 if (bbr->r_ctl.bbr_rttprobe_gain_val) 11219 bbr->r_ctl.rc_bbr_hptsi_gain = bbr->r_ctl.bbr_rttprobe_gain_val; 11220 else 11221 bbr->r_ctl.rc_bbr_hptsi_gain = BBR_UNIT; 11222 } 11223 } 11224 if ((bbr->r_ctl.rc_bbr_enters_probertt == 0) && 11225 (bbr->rc_use_google == 0) && 11226 bbr->r_ctl.bbr_rttprobe_gain_val && 11227 (((cts - bbr->r_ctl.rc_probertt_srttchktim) > bbr_get_rtt(bbr, bbr_drain_rtt)) || 11228 (flight >= bbr->r_ctl.flightsize_at_drain))) { 11229 /* 11230 * We have doddled with our current hptsi 11231 * gain an srtt and have still not made it 11232 * to target, or we have increased our flight. 11233 * Lets reduce the gain by xx% 11234 * flooring the reduce at DRAIN (based on 11235 * mul/div) 11236 */ 11237 int red; 11238 11239 bbr->r_ctl.flightsize_at_drain = flight; 11240 bbr->r_ctl.rc_probertt_srttchktim = cts; 11241 red = max((bbr->r_ctl.bbr_rttprobe_gain_val / 10), 1); 11242 if ((bbr->r_ctl.rc_bbr_hptsi_gain - red) > max(bbr_drain_floor, 1)) { 11243 /* Reduce our gain again */ 11244 bbr->r_ctl.rc_bbr_hptsi_gain -= red; 11245 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG, 0); 11246 } else if (bbr->r_ctl.rc_bbr_hptsi_gain > max(bbr_drain_floor, 1)) { 11247 /* one more chance before we give up */ 11248 bbr->r_ctl.rc_bbr_hptsi_gain = max(bbr_drain_floor, 1); 11249 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_SHRINK_PG_FINAL, 0); 11250 } else { 11251 /* At the very bottom */ 11252 bbr->r_ctl.rc_bbr_hptsi_gain = max((bbr_drain_floor-1), 1); 11253 } 11254 } 11255 } 11256 if (bbr->r_ctl.rc_bbr_enters_probertt && 11257 (TSTMP_GT(cts, bbr->r_ctl.rc_bbr_enters_probertt)) && 11258 ((cts - bbr->r_ctl.rc_bbr_enters_probertt) >= bbr_rtt_probe_time)) { 11259 /* Time to exit probe RTT normally */ 11260 bbr_exit_probe_rtt(bbr->rc_tp, bbr, cts); 11261 } 11262 } else if (bbr->rc_bbr_state == BBR_STATE_PROBE_BW) { 11263 if ((bbr->rc_tp->snd_una == bbr->rc_tp->snd_max) && 11264 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 11265 /* 11266 * This qualifies as a RTT_PROBE session since we 11267 * drop the data outstanding to nothing and waited 11268 * more than bbr_rtt_probe_time. 11269 */ 11270 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 11271 bbr_set_reduced_rtt(bbr, cts, __LINE__); 11272 } 11273 if (bbr_should_enter_probe_rtt(bbr, cts)) { 11274 bbr_enter_probe_rtt(bbr, cts, __LINE__); 11275 } else { 11276 bbr_set_probebw_gains(bbr, cts, losses); 11277 } 11278 } 11279 } 11280 11281 static void 11282 bbr_check_bbr_for_state(struct tcp_bbr *bbr, uint32_t cts, int32_t line, uint32_t losses) 11283 { 11284 int32_t epoch = 0; 11285 11286 if ((cts - bbr->r_ctl.rc_rcv_epoch_start) >= bbr_get_rtt(bbr, BBR_RTT_PROP)) { 11287 bbr_set_epoch(bbr, cts, line); 11288 /* At each epoch doe lt bw sampling */ 11289 epoch = 1; 11290 } 11291 bbr_state_change(bbr, cts, epoch, bbr->rc_is_pkt_epoch_now, losses); 11292 } 11293 11294 static int 11295 bbr_do_segment_nounlock(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11296 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos, int32_t nxt_pkt, 11297 struct timeval *tv) 11298 { 11299 struct inpcb *inp = tptoinpcb(tp); 11300 struct socket *so = tptosocket(tp); 11301 int32_t thflags, retval; 11302 uint32_t cts, lcts; 11303 uint32_t tiwin; 11304 struct tcpopt to; 11305 struct tcp_bbr *bbr; 11306 struct bbr_sendmap *rsm; 11307 struct timeval ltv; 11308 int32_t did_out = 0; 11309 uint16_t nsegs; 11310 int32_t prev_state; 11311 uint32_t lost; 11312 11313 nsegs = max(1, m->m_pkthdr.lro_nsegs); 11314 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11315 /* add in our stats */ 11316 kern_prefetch(bbr, &prev_state); 11317 prev_state = 0; 11318 thflags = tcp_get_flags(th); 11319 /* 11320 * If this is either a state-changing packet or current state isn't 11321 * established, we require a write lock on tcbinfo. Otherwise, we 11322 * allow the tcbinfo to be in either alocked or unlocked, as the 11323 * caller may have unnecessarily acquired a write lock due to a 11324 * race. 11325 */ 11326 INP_WLOCK_ASSERT(tptoinpcb(tp)); 11327 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 11328 __func__)); 11329 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 11330 __func__)); 11331 11332 tp->t_rcvtime = ticks; 11333 /* 11334 * Unscale the window into a 32-bit value. For the SYN_SENT state 11335 * the scale is zero. 11336 */ 11337 tiwin = th->th_win << tp->snd_scale; 11338 #ifdef STATS 11339 stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 11340 #endif 11341 11342 if (m->m_flags & M_TSTMP) { 11343 /* Prefer the hardware timestamp if present */ 11344 struct timespec ts; 11345 11346 mbuf_tstmp2timespec(m, &ts); 11347 bbr->rc_tv.tv_sec = ts.tv_sec; 11348 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11349 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11350 } else if (m->m_flags & M_TSTMP_LRO) { 11351 /* Next the arrival timestamp */ 11352 struct timespec ts; 11353 11354 mbuf_tstmp2timespec(m, &ts); 11355 bbr->rc_tv.tv_sec = ts.tv_sec; 11356 bbr->rc_tv.tv_usec = ts.tv_nsec / 1000; 11357 bbr->r_ctl.rc_rcvtime = cts = tcp_tv_to_usectick(&bbr->rc_tv); 11358 } else { 11359 /* 11360 * Ok just get the current time. 11361 */ 11362 bbr->r_ctl.rc_rcvtime = lcts = cts = tcp_get_usecs(&bbr->rc_tv); 11363 } 11364 /* 11365 * Parse options on any incoming segment. 11366 */ 11367 tcp_dooptions(&to, (u_char *)(th + 1), 11368 (th->th_off << 2) - sizeof(struct tcphdr), 11369 (thflags & TH_SYN) ? TO_SYN : 0); 11370 if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) { 11371 /* 11372 * We don't look at sack's from the 11373 * peer because the MSS is too small which 11374 * can subject us to an attack. 11375 */ 11376 to.to_flags &= ~TOF_SACK; 11377 } 11378 /* 11379 * If timestamps were negotiated during SYN/ACK and a 11380 * segment without a timestamp is received, silently drop 11381 * the segment, unless it is a RST segment or missing timestamps are 11382 * tolerated. 11383 * See section 3.2 of RFC 7323. 11384 */ 11385 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS) && 11386 ((thflags & TH_RST) == 0) && (V_tcp_tolerate_missing_ts == 0)) { 11387 retval = 0; 11388 m_freem(m); 11389 goto done_with_input; 11390 } 11391 /* 11392 * If echoed timestamp is later than the current time, fall back to 11393 * non RFC1323 RTT calculation. Normalize timestamp if syncookies 11394 * were used when this connection was established. 11395 */ 11396 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 11397 to.to_tsecr -= tp->ts_offset; 11398 if (TSTMP_GT(to.to_tsecr, tcp_tv_to_mssectick(&bbr->rc_tv))) 11399 to.to_tsecr = 0; 11400 } 11401 /* 11402 * If its the first time in we need to take care of options and 11403 * verify we can do SACK for rack! 11404 */ 11405 if (bbr->r_state == 0) { 11406 /* 11407 * Process options only when we get SYN/ACK back. The SYN 11408 * case for incoming connections is handled in tcp_syncache. 11409 * According to RFC1323 the window field in a SYN (i.e., a 11410 * <SYN> or <SYN,ACK>) segment itself is never scaled. XXX 11411 * this is traditional behavior, may need to be cleaned up. 11412 */ 11413 if (bbr->rc_inp == NULL) { 11414 bbr->rc_inp = inp; 11415 } 11416 /* 11417 * We need to init rc_inp here since its not init'd when 11418 * bbr_init is called 11419 */ 11420 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 11421 if ((to.to_flags & TOF_SCALE) && 11422 (tp->t_flags & TF_REQ_SCALE)) { 11423 tp->t_flags |= TF_RCVD_SCALE; 11424 tp->snd_scale = to.to_wscale; 11425 } else 11426 tp->t_flags &= ~TF_REQ_SCALE; 11427 /* 11428 * Initial send window. It will be updated with the 11429 * next incoming segment to the scaled value. 11430 */ 11431 tp->snd_wnd = th->th_win; 11432 if ((to.to_flags & TOF_TS) && 11433 (tp->t_flags & TF_REQ_TSTMP)) { 11434 tp->t_flags |= TF_RCVD_TSTMP; 11435 tp->ts_recent = to.to_tsval; 11436 tp->ts_recent_age = tcp_tv_to_mssectick(&bbr->rc_tv); 11437 } else 11438 tp->t_flags &= ~TF_REQ_TSTMP; 11439 if (to.to_flags & TOF_MSS) 11440 tcp_mss(tp, to.to_mss); 11441 if ((tp->t_flags & TF_SACK_PERMIT) && 11442 (to.to_flags & TOF_SACKPERM) == 0) 11443 tp->t_flags &= ~TF_SACK_PERMIT; 11444 if (tp->t_flags & TF_FASTOPEN) { 11445 if (to.to_flags & TOF_FASTOPEN) { 11446 uint16_t mss; 11447 11448 if (to.to_flags & TOF_MSS) 11449 mss = to.to_mss; 11450 else 11451 if ((inp->inp_vflag & INP_IPV6) != 0) 11452 mss = TCP6_MSS; 11453 else 11454 mss = TCP_MSS; 11455 tcp_fastopen_update_cache(tp, mss, 11456 to.to_tfo_len, to.to_tfo_cookie); 11457 } else 11458 tcp_fastopen_disable_path(tp); 11459 } 11460 } 11461 /* 11462 * At this point we are at the initial call. Here we decide 11463 * if we are doing RACK or not. We do this by seeing if 11464 * TF_SACK_PERMIT is set, if not rack is *not* possible and 11465 * we switch to the default code. 11466 */ 11467 if ((tp->t_flags & TF_SACK_PERMIT) == 0) { 11468 /* Bail */ 11469 tcp_switch_back_to_default(tp); 11470 (*tp->t_fb->tfb_tcp_do_segment)(tp, m, th, drop_hdrlen, 11471 tlen, iptos); 11472 return (1); 11473 } 11474 /* Set the flag */ 11475 bbr->r_is_v6 = (inp->inp_vflag & INP_IPV6) != 0; 11476 tcp_set_hpts(tp); 11477 sack_filter_clear(&bbr->r_ctl.bbr_sf, th->th_ack); 11478 } 11479 if (thflags & TH_ACK) { 11480 /* Track ack types */ 11481 if (to.to_flags & TOF_SACK) 11482 BBR_STAT_INC(bbr_acks_with_sacks); 11483 else 11484 BBR_STAT_INC(bbr_plain_acks); 11485 } 11486 /* 11487 * This is the one exception case where we set the rack state 11488 * always. All other times (timers etc) we must have a rack-state 11489 * set (so we assure we have done the checks above for SACK). 11490 */ 11491 if (thflags & TH_FIN) 11492 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 11493 if (bbr->r_state != tp->t_state) 11494 bbr_set_state(tp, bbr, tiwin); 11495 11496 if (SEQ_GT(th->th_ack, tp->snd_una) && (rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map)) != NULL) 11497 kern_prefetch(rsm, &prev_state); 11498 prev_state = bbr->r_state; 11499 bbr->rc_ack_was_delayed = 0; 11500 lost = bbr->r_ctl.rc_lost; 11501 bbr->rc_is_pkt_epoch_now = 0; 11502 if (m->m_flags & (M_TSTMP|M_TSTMP_LRO)) { 11503 /* Get the real time into lcts and figure the real delay */ 11504 lcts = tcp_get_usecs(<v); 11505 if (TSTMP_GT(lcts, cts)) { 11506 bbr->r_ctl.rc_ack_hdwr_delay = lcts - cts; 11507 bbr->rc_ack_was_delayed = 1; 11508 if (TSTMP_GT(bbr->r_ctl.rc_ack_hdwr_delay, 11509 bbr->r_ctl.highest_hdwr_delay)) 11510 bbr->r_ctl.highest_hdwr_delay = bbr->r_ctl.rc_ack_hdwr_delay; 11511 } else { 11512 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11513 bbr->rc_ack_was_delayed = 0; 11514 } 11515 } else { 11516 bbr->r_ctl.rc_ack_hdwr_delay = 0; 11517 bbr->rc_ack_was_delayed = 0; 11518 } 11519 bbr_log_ack_event(bbr, th, &to, tlen, nsegs, cts, nxt_pkt, m); 11520 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 11521 retval = 0; 11522 m_freem(m); 11523 goto done_with_input; 11524 } 11525 /* 11526 * If a segment with the ACK-bit set arrives in the SYN-SENT state 11527 * check SEQ.ACK first as described on page 66 of RFC 793, section 3.9. 11528 */ 11529 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 11530 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 11531 tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 11532 ctf_do_dropwithreset_conn(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 11533 return (1); 11534 } 11535 if (tiwin > bbr->r_ctl.rc_high_rwnd) 11536 bbr->r_ctl.rc_high_rwnd = tiwin; 11537 bbr->r_ctl.rc_flight_at_input = ctf_flight_size(tp, 11538 (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11539 bbr->rtt_valid = 0; 11540 if (to.to_flags & TOF_TS) { 11541 bbr->rc_ts_valid = 1; 11542 bbr->r_ctl.last_inbound_ts = to.to_tsval; 11543 } else { 11544 bbr->rc_ts_valid = 0; 11545 bbr->r_ctl.last_inbound_ts = 0; 11546 } 11547 retval = (*bbr->r_substate) (m, th, so, 11548 tp, &to, drop_hdrlen, 11549 tlen, tiwin, thflags, nxt_pkt, iptos); 11550 if (nxt_pkt == 0) 11551 BBR_STAT_INC(bbr_rlock_left_ret0); 11552 else 11553 BBR_STAT_INC(bbr_rlock_left_ret1); 11554 if (retval == 0) { 11555 /* 11556 * If retval is 1 the tcb is unlocked and most likely the tp 11557 * is gone. 11558 */ 11559 INP_WLOCK_ASSERT(inp); 11560 tcp_bbr_xmit_timer_commit(bbr, tp, cts); 11561 if (bbr->rc_is_pkt_epoch_now) 11562 bbr_set_pktepoch(bbr, cts, __LINE__); 11563 bbr_check_bbr_for_state(bbr, cts, __LINE__, (bbr->r_ctl.rc_lost - lost)); 11564 if (nxt_pkt == 0) { 11565 if ((bbr->r_wanted_output != 0) || 11566 (tp->t_flags & TF_ACKNOW)) { 11567 11568 bbr->rc_output_starts_timer = 0; 11569 did_out = 1; 11570 if (tcp_output(tp) < 0) 11571 return (1); 11572 } else 11573 bbr_start_hpts_timer(bbr, tp, cts, 6, 0, 0); 11574 } 11575 if ((nxt_pkt == 0) && 11576 ((bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) == 0) && 11577 (SEQ_GT(tp->snd_max, tp->snd_una) || 11578 (tp->t_flags & TF_DELACK) || 11579 ((V_tcp_always_keepalive || bbr->rc_inp->inp_socket->so_options & SO_KEEPALIVE) && 11580 (tp->t_state <= TCPS_CLOSING)))) { 11581 /* 11582 * We could not send (probably in the hpts but 11583 * stopped the timer)? 11584 */ 11585 if ((tp->snd_max == tp->snd_una) && 11586 ((tp->t_flags & TF_DELACK) == 0) && 11587 (tcp_in_hpts(tp)) && 11588 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 11589 /* 11590 * keep alive not needed if we are hptsi 11591 * output yet 11592 */ 11593 ; 11594 } else { 11595 if (tcp_in_hpts(tp)) { 11596 tcp_hpts_remove(tp); 11597 if ((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11598 (TSTMP_GT(lcts, bbr->rc_pacer_started))) { 11599 uint32_t del; 11600 11601 del = lcts - bbr->rc_pacer_started; 11602 if (bbr->r_ctl.rc_last_delay_val > del) { 11603 BBR_STAT_INC(bbr_force_timer_start); 11604 bbr->r_ctl.rc_last_delay_val -= del; 11605 bbr->rc_pacer_started = lcts; 11606 } else { 11607 /* We are late */ 11608 bbr->r_ctl.rc_last_delay_val = 0; 11609 BBR_STAT_INC(bbr_force_output); 11610 if (tcp_output(tp) < 0) 11611 return (1); 11612 } 11613 } 11614 } 11615 bbr_start_hpts_timer(bbr, tp, cts, 8, bbr->r_ctl.rc_last_delay_val, 11616 0); 11617 } 11618 } else if ((bbr->rc_output_starts_timer == 0) && (nxt_pkt == 0)) { 11619 /* Do we have the correct timer running? */ 11620 bbr_timer_audit(tp, bbr, lcts, &so->so_snd); 11621 } 11622 /* Clear the flag, it may have been cleared by output but we may not have */ 11623 if ((nxt_pkt == 0) && (tp->t_flags2 & TF2_HPTS_CALLS)) 11624 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11625 /* Do we have a new state */ 11626 if (bbr->r_state != tp->t_state) 11627 bbr_set_state(tp, bbr, tiwin); 11628 done_with_input: 11629 bbr_log_doseg_done(bbr, cts, nxt_pkt, did_out); 11630 if (did_out) 11631 bbr->r_wanted_output = 0; 11632 } 11633 return (retval); 11634 } 11635 11636 static void 11637 bbr_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 11638 int32_t drop_hdrlen, int32_t tlen, uint8_t iptos) 11639 { 11640 struct timeval tv; 11641 int retval; 11642 11643 /* First lets see if we have old packets */ 11644 if (!STAILQ_EMPTY(&tp->t_inqueue)) { 11645 if (ctf_do_queued_segments(tp, 1)) { 11646 m_freem(m); 11647 return; 11648 } 11649 } 11650 if (m->m_flags & M_TSTMP_LRO) { 11651 mbuf_tstmp2timeval(m, &tv); 11652 } else { 11653 /* Should not be should we kassert instead? */ 11654 tcp_get_usecs(&tv); 11655 } 11656 retval = bbr_do_segment_nounlock(tp, m, th, drop_hdrlen, tlen, iptos, 11657 0, &tv); 11658 if (retval == 0) { 11659 INP_WUNLOCK(tptoinpcb(tp)); 11660 } 11661 } 11662 11663 /* 11664 * Return how much data can be sent without violating the 11665 * cwnd or rwnd. 11666 */ 11667 11668 static inline uint32_t 11669 bbr_what_can_we_send(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t sendwin, 11670 uint32_t avail, int32_t sb_offset, uint32_t cts) 11671 { 11672 uint32_t len; 11673 11674 if (ctf_outstanding(tp) >= tp->snd_wnd) { 11675 /* We never want to go over our peers rcv-window */ 11676 len = 0; 11677 } else { 11678 uint32_t flight; 11679 11680 flight = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + bbr->r_ctl.rc_lost_bytes)); 11681 if (flight >= sendwin) { 11682 /* 11683 * We have in flight what we are allowed by cwnd (if 11684 * it was rwnd blocking it would have hit above out 11685 * >= tp->snd_wnd). 11686 */ 11687 return (0); 11688 } 11689 len = sendwin - flight; 11690 if ((len + ctf_outstanding(tp)) > tp->snd_wnd) { 11691 /* We would send too much (beyond the rwnd) */ 11692 len = tp->snd_wnd - ctf_outstanding(tp); 11693 } 11694 if ((len + sb_offset) > avail) { 11695 /* 11696 * We don't have that much in the SB, how much is 11697 * there? 11698 */ 11699 len = avail - sb_offset; 11700 } 11701 } 11702 return (len); 11703 } 11704 11705 static inline void 11706 bbr_do_send_accounting(struct tcpcb *tp, struct tcp_bbr *bbr, struct bbr_sendmap *rsm, int32_t len, int32_t error) 11707 { 11708 if (error) { 11709 return; 11710 } 11711 if (rsm) { 11712 if (rsm->r_flags & BBR_TLP) { 11713 /* 11714 * TLP should not count in retran count, but in its 11715 * own bin 11716 */ 11717 KMOD_TCPSTAT_INC(tcps_tlpresends); 11718 KMOD_TCPSTAT_ADD(tcps_tlpresend_bytes, len); 11719 } else { 11720 /* Retransmit */ 11721 tp->t_sndrexmitpack++; 11722 KMOD_TCPSTAT_INC(tcps_sndrexmitpack); 11723 KMOD_TCPSTAT_ADD(tcps_sndrexmitbyte, len); 11724 #ifdef STATS 11725 stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 11726 len); 11727 #endif 11728 } 11729 /* 11730 * Logs in 0 - 8, 8 is all non probe_bw states 0-7 is 11731 * sub-state 11732 */ 11733 counter_u64_add(bbr_state_lost[rsm->r_bbr_state], len); 11734 if (bbr->rc_bbr_state != BBR_STATE_PROBE_BW) { 11735 /* Non probe_bw log in 1, 2, or 4. */ 11736 counter_u64_add(bbr_state_resend[bbr->rc_bbr_state], len); 11737 } else { 11738 /* 11739 * Log our probe state 3, and log also 5-13 to show 11740 * us the recovery sub-state for the send. This 11741 * means that 3 == (5+6+7+8+9+10+11+12+13) 11742 */ 11743 counter_u64_add(bbr_state_resend[BBR_STATE_PROBE_BW], len); 11744 counter_u64_add(bbr_state_resend[(bbr_state_val(bbr) + 5)], len); 11745 } 11746 /* Place in both 16's the totals of retransmitted */ 11747 counter_u64_add(bbr_state_lost[16], len); 11748 counter_u64_add(bbr_state_resend[16], len); 11749 /* Place in 17's the total sent */ 11750 counter_u64_add(bbr_state_resend[17], len); 11751 counter_u64_add(bbr_state_lost[17], len); 11752 11753 } else { 11754 /* New sends */ 11755 KMOD_TCPSTAT_INC(tcps_sndpack); 11756 KMOD_TCPSTAT_ADD(tcps_sndbyte, len); 11757 /* Place in 17's the total sent */ 11758 counter_u64_add(bbr_state_resend[17], len); 11759 counter_u64_add(bbr_state_lost[17], len); 11760 #ifdef STATS 11761 stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 11762 len); 11763 #endif 11764 } 11765 } 11766 11767 static void 11768 bbr_cwnd_limiting(struct tcpcb *tp, struct tcp_bbr *bbr, uint32_t in_level) 11769 { 11770 if (bbr->rc_filled_pipe && bbr_target_cwnd_mult_limit && (bbr->rc_use_google == 0)) { 11771 /* 11772 * Limit the cwnd to not be above N x the target plus whats 11773 * is outstanding. The target is based on the current b/w 11774 * estimate. 11775 */ 11776 uint32_t target; 11777 11778 target = bbr_get_target_cwnd(bbr, bbr_get_bw(bbr), BBR_UNIT); 11779 target += ctf_outstanding(tp); 11780 target *= bbr_target_cwnd_mult_limit; 11781 if (tp->snd_cwnd > target) 11782 tp->snd_cwnd = target; 11783 bbr_log_type_cwndupd(bbr, 0, 0, 0, 10, 0, 0, __LINE__); 11784 } 11785 } 11786 11787 static int 11788 bbr_window_update_needed(struct tcpcb *tp, struct socket *so, uint32_t recwin, int32_t maxseg) 11789 { 11790 /* 11791 * "adv" is the amount we could increase the window, taking into 11792 * account that we are limited by TCP_MAXWIN << tp->rcv_scale. 11793 */ 11794 int32_t adv; 11795 int32_t oldwin; 11796 11797 adv = recwin; 11798 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 11799 oldwin = (tp->rcv_adv - tp->rcv_nxt); 11800 if (adv > oldwin) 11801 adv -= oldwin; 11802 else { 11803 /* We can't increase the window */ 11804 adv = 0; 11805 } 11806 } else 11807 oldwin = 0; 11808 11809 /* 11810 * If the new window size ends up being the same as or less 11811 * than the old size when it is scaled, then don't force 11812 * a window update. 11813 */ 11814 if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 11815 return (0); 11816 11817 if (adv >= (2 * maxseg) && 11818 (adv >= (so->so_rcv.sb_hiwat / 4) || 11819 recwin <= (so->so_rcv.sb_hiwat / 8) || 11820 so->so_rcv.sb_hiwat <= 8 * maxseg)) { 11821 return (1); 11822 } 11823 if (2 * adv >= (int32_t) so->so_rcv.sb_hiwat) 11824 return (1); 11825 return (0); 11826 } 11827 11828 /* 11829 * Return 0 on success and a errno on failure to send. 11830 * Note that a 0 return may not mean we sent anything 11831 * if the TCB was on the hpts. A non-zero return 11832 * does indicate the error we got from ip[6]_output. 11833 */ 11834 static int 11835 bbr_output_wtime(struct tcpcb *tp, const struct timeval *tv) 11836 { 11837 struct socket *so; 11838 int32_t len; 11839 uint32_t cts; 11840 uint32_t recwin, sendwin; 11841 int32_t sb_offset; 11842 int32_t flags, abandon, error = 0; 11843 struct tcp_log_buffer *lgb; 11844 struct mbuf *m; 11845 struct mbuf *mb; 11846 uint32_t if_hw_tsomaxsegcount = 0; 11847 uint32_t if_hw_tsomaxsegsize = 0; 11848 uint32_t if_hw_tsomax = 0; 11849 struct ip *ip = NULL; 11850 struct tcp_bbr *bbr; 11851 struct tcphdr *th; 11852 struct udphdr *udp = NULL; 11853 u_char opt[TCP_MAXOLEN]; 11854 unsigned ipoptlen, optlen, hdrlen; 11855 unsigned ulen; 11856 uint32_t bbr_seq; 11857 uint32_t delay_calc=0; 11858 uint8_t doing_tlp = 0; 11859 uint8_t local_options; 11860 #ifdef BBR_INVARIANTS 11861 uint8_t doing_retran_from = 0; 11862 uint8_t picked_up_retran = 0; 11863 #endif 11864 uint8_t wanted_cookie = 0; 11865 uint8_t more_to_rxt=0; 11866 int32_t prefetch_so_done = 0; 11867 int32_t prefetch_rsm = 0; 11868 uint32_t tot_len = 0; 11869 uint32_t maxseg, pace_max_segs, p_maxseg; 11870 int32_t csum_flags = 0; 11871 int32_t hw_tls; 11872 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 11873 unsigned ipsec_optlen = 0; 11874 11875 #endif 11876 volatile int32_t sack_rxmit; 11877 struct bbr_sendmap *rsm = NULL; 11878 int32_t tso, mtu; 11879 struct tcpopt to; 11880 int32_t slot = 0; 11881 struct inpcb *inp; 11882 struct sockbuf *sb; 11883 bool hpts_calling; 11884 #ifdef INET6 11885 struct ip6_hdr *ip6 = NULL; 11886 int32_t isipv6; 11887 #endif 11888 uint8_t app_limited = BBR_JR_SENT_DATA; 11889 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 11890 /* We take a cache hit here */ 11891 memcpy(&bbr->rc_tv, tv, sizeof(struct timeval)); 11892 cts = tcp_tv_to_usectick(&bbr->rc_tv); 11893 inp = bbr->rc_inp; 11894 hpts_calling = !!(tp->t_flags2 & TF2_HPTS_CALLS); 11895 tp->t_flags2 &= ~TF2_HPTS_CALLS; 11896 so = inp->inp_socket; 11897 sb = &so->so_snd; 11898 if (tp->t_nic_ktls_xmit) 11899 hw_tls = 1; 11900 else 11901 hw_tls = 0; 11902 kern_prefetch(sb, &maxseg); 11903 maxseg = tp->t_maxseg - bbr->rc_last_options; 11904 if (bbr_minseg(bbr) < maxseg) { 11905 tcp_bbr_tso_size_check(bbr, cts); 11906 } 11907 /* Remove any flags that indicate we are pacing on the inp */ 11908 pace_max_segs = bbr->r_ctl.rc_pace_max_segs; 11909 p_maxseg = min(maxseg, pace_max_segs); 11910 INP_WLOCK_ASSERT(inp); 11911 #ifdef TCP_OFFLOAD 11912 if (tp->t_flags & TF_TOE) 11913 return (tcp_offload_output(tp)); 11914 #endif 11915 11916 #ifdef INET6 11917 if (bbr->r_state) { 11918 /* Use the cache line loaded if possible */ 11919 isipv6 = bbr->r_is_v6; 11920 } else { 11921 isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 11922 } 11923 #endif 11924 if (((bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) == 0) && 11925 tcp_in_hpts(tp)) { 11926 /* 11927 * We are on the hpts for some timer but not hptsi output. 11928 * Possibly remove from the hpts so we can send/recv etc. 11929 */ 11930 if ((tp->t_flags & TF_ACKNOW) == 0) { 11931 /* 11932 * No immediate demand right now to send an ack, but 11933 * the user may have read, making room for new data 11934 * (a window update). If so we may want to cancel 11935 * whatever timer is running (KEEP/DEL-ACK?) and 11936 * continue to send out a window update. Or we may 11937 * have gotten more data into the socket buffer to 11938 * send. 11939 */ 11940 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 11941 (long)TCP_MAXWIN << tp->rcv_scale); 11942 if ((bbr_window_update_needed(tp, so, recwin, maxseg) == 0) && 11943 ((tcp_outflags[tp->t_state] & TH_RST) == 0) && 11944 ((sbavail(sb) + ((tcp_outflags[tp->t_state] & TH_FIN) ? 1 : 0)) <= 11945 (tp->snd_max - tp->snd_una))) { 11946 /* 11947 * Nothing new to send and no window update 11948 * is needed to send. Lets just return and 11949 * let the timer-run off. 11950 */ 11951 return (0); 11952 } 11953 } 11954 tcp_hpts_remove(tp); 11955 bbr_timer_cancel(bbr, __LINE__, cts); 11956 } 11957 if (bbr->r_ctl.rc_last_delay_val) { 11958 /* Calculate a rough delay for early escape to sending */ 11959 if (SEQ_GT(cts, bbr->rc_pacer_started)) 11960 delay_calc = cts - bbr->rc_pacer_started; 11961 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 11962 delay_calc -= bbr->r_ctl.rc_last_delay_val; 11963 else 11964 delay_calc = 0; 11965 } 11966 /* Mark that we have called bbr_output(). */ 11967 if ((bbr->r_timer_override) || 11968 (tp->t_state < TCPS_ESTABLISHED)) { 11969 /* Timeouts or early states are exempt */ 11970 if (tcp_in_hpts(tp)) 11971 tcp_hpts_remove(tp); 11972 } else if (tcp_in_hpts(tp)) { 11973 if ((bbr->r_ctl.rc_last_delay_val) && 11974 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) && 11975 delay_calc) { 11976 /* 11977 * We were being paced for output and the delay has 11978 * already exceeded when we were supposed to be 11979 * called, lets go ahead and pull out of the hpts 11980 * and call output. 11981 */ 11982 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_LATE], 1); 11983 bbr->r_ctl.rc_last_delay_val = 0; 11984 tcp_hpts_remove(tp); 11985 } else if (tp->t_state == TCPS_CLOSED) { 11986 bbr->r_ctl.rc_last_delay_val = 0; 11987 tcp_hpts_remove(tp); 11988 } else { 11989 /* 11990 * On the hpts, you shall not pass! even if ACKNOW 11991 * is on, we will when the hpts fires, unless of 11992 * course we are overdue. 11993 */ 11994 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_INPACE], 1); 11995 return (0); 11996 } 11997 } 11998 bbr->rc_cwnd_limited = 0; 11999 if (bbr->r_ctl.rc_last_delay_val) { 12000 /* recalculate the real delay and deal with over/under */ 12001 if (SEQ_GT(cts, bbr->rc_pacer_started)) 12002 delay_calc = cts - bbr->rc_pacer_started; 12003 else 12004 delay_calc = 0; 12005 if (delay_calc >= bbr->r_ctl.rc_last_delay_val) 12006 /* Setup the delay which will be added in */ 12007 delay_calc -= bbr->r_ctl.rc_last_delay_val; 12008 else { 12009 /* 12010 * We are early setup to adjust 12011 * our slot time. 12012 */ 12013 uint64_t merged_val; 12014 12015 bbr->r_ctl.rc_agg_early += (bbr->r_ctl.rc_last_delay_val - delay_calc); 12016 bbr->r_agg_early_set = 1; 12017 if (bbr->r_ctl.rc_hptsi_agg_delay) { 12018 if (bbr->r_ctl.rc_hptsi_agg_delay >= bbr->r_ctl.rc_agg_early) { 12019 /* Nope our previous late cancels out the early */ 12020 bbr->r_ctl.rc_hptsi_agg_delay -= bbr->r_ctl.rc_agg_early; 12021 bbr->r_agg_early_set = 0; 12022 bbr->r_ctl.rc_agg_early = 0; 12023 } else { 12024 bbr->r_ctl.rc_agg_early -= bbr->r_ctl.rc_hptsi_agg_delay; 12025 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12026 } 12027 } 12028 merged_val = bbr->rc_pacer_started; 12029 merged_val <<= 32; 12030 merged_val |= bbr->r_ctl.rc_last_delay_val; 12031 bbr_log_pacing_delay_calc(bbr, hpts_calling, 12032 bbr->r_ctl.rc_agg_early, cts, delay_calc, merged_val, 12033 bbr->r_agg_early_set, 3); 12034 bbr->r_ctl.rc_last_delay_val = 0; 12035 BBR_STAT_INC(bbr_early); 12036 delay_calc = 0; 12037 } 12038 } else { 12039 /* We were not delayed due to hptsi */ 12040 if (bbr->r_agg_early_set) 12041 bbr->r_ctl.rc_agg_early = 0; 12042 bbr->r_agg_early_set = 0; 12043 delay_calc = 0; 12044 } 12045 if (delay_calc) { 12046 /* 12047 * We had a hptsi delay which means we are falling behind on 12048 * sending at the expected rate. Calculate an extra amount 12049 * of data we can send, if any, to put us back on track. 12050 */ 12051 if ((bbr->r_ctl.rc_hptsi_agg_delay + delay_calc) < bbr->r_ctl.rc_hptsi_agg_delay) 12052 bbr->r_ctl.rc_hptsi_agg_delay = 0xffffffff; 12053 else 12054 bbr->r_ctl.rc_hptsi_agg_delay += delay_calc; 12055 } 12056 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12057 if ((tp->snd_una == tp->snd_max) && 12058 (bbr->rc_bbr_state != BBR_STATE_IDLE_EXIT) && 12059 (sbavail(sb))) { 12060 /* 12061 * Ok we have been idle with nothing outstanding 12062 * we possibly need to start fresh with either a new 12063 * suite of states or a fast-ramp up. 12064 */ 12065 bbr_restart_after_idle(bbr, 12066 cts, bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time)); 12067 } 12068 /* 12069 * Now was there a hptsi delay where we are behind? We only count 12070 * being behind if: a) We are not in recovery. b) There was a delay. 12071 * <and> c) We had room to send something. 12072 * 12073 */ 12074 if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 12075 int retval; 12076 12077 retval = bbr_process_timers(tp, bbr, cts, hpts_calling); 12078 if (retval != 0) { 12079 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_ATIMER], 1); 12080 /* 12081 * If timers want tcp_drop(), then pass error out, 12082 * otherwise suppress it. 12083 */ 12084 return (retval < 0 ? retval : 0); 12085 } 12086 } 12087 bbr->rc_tp->t_flags2 &= ~TF2_MBUF_QUEUE_READY; 12088 if (hpts_calling && 12089 (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT)) { 12090 bbr->r_ctl.rc_last_delay_val = 0; 12091 } 12092 bbr->r_timer_override = 0; 12093 bbr->r_wanted_output = 0; 12094 /* 12095 * For TFO connections in SYN_RECEIVED, only allow the initial 12096 * SYN|ACK and those sent by the retransmit timer. 12097 */ 12098 if ((tp->t_flags & TF_FASTOPEN) && 12099 ((tp->t_state == TCPS_SYN_RECEIVED) || 12100 (tp->t_state == TCPS_SYN_SENT)) && 12101 SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 12102 (tp->t_rxtshift == 0)) { /* not a retransmit */ 12103 len = 0; 12104 goto just_return_nolock; 12105 } 12106 /* 12107 * Before sending anything check for a state update. For hpts 12108 * calling without input this is important. If its input calling 12109 * then this was already done. 12110 */ 12111 if (bbr->rc_use_google == 0) 12112 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12113 again: 12114 /* 12115 * If we've recently taken a timeout, snd_max will be greater than 12116 * snd_max. BBR in general does not pay much attention to snd_nxt 12117 * for historic reasons the persist timer still uses it. This means 12118 * we have to look at it. All retransmissions that are not persits 12119 * use the rsm that needs to be sent so snd_nxt is ignored. At the 12120 * end of this routine we pull snd_nxt always up to snd_max. 12121 */ 12122 doing_tlp = 0; 12123 #ifdef BBR_INVARIANTS 12124 doing_retran_from = picked_up_retran = 0; 12125 #endif 12126 error = 0; 12127 tso = 0; 12128 slot = 0; 12129 mtu = 0; 12130 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 12131 sb_offset = tp->snd_max - tp->snd_una; 12132 flags = tcp_outflags[tp->t_state]; 12133 sack_rxmit = 0; 12134 len = 0; 12135 rsm = NULL; 12136 if (flags & TH_RST) { 12137 SOCK_SENDBUF_LOCK(so); 12138 goto send; 12139 } 12140 recheck_resend: 12141 while (bbr->r_ctl.rc_free_cnt < bbr_min_req_free) { 12142 /* We need to always have one in reserve */ 12143 rsm = bbr_alloc(bbr); 12144 if (rsm == NULL) { 12145 error = ENOMEM; 12146 /* Lie to get on the hpts */ 12147 tot_len = tp->t_maxseg; 12148 if (hpts_calling) 12149 /* Retry in a ms */ 12150 slot = 1001; 12151 goto just_return_nolock; 12152 } 12153 TAILQ_INSERT_TAIL(&bbr->r_ctl.rc_free, rsm, r_next); 12154 bbr->r_ctl.rc_free_cnt++; 12155 rsm = NULL; 12156 } 12157 /* What do we send, a resend? */ 12158 if (bbr->r_ctl.rc_resend == NULL) { 12159 /* Check for rack timeout */ 12160 bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts); 12161 if (bbr->r_ctl.rc_resend) { 12162 #ifdef BBR_INVARIANTS 12163 picked_up_retran = 1; 12164 #endif 12165 bbr_cong_signal(tp, NULL, CC_NDUPACK, bbr->r_ctl.rc_resend); 12166 } 12167 } 12168 if (bbr->r_ctl.rc_resend) { 12169 rsm = bbr->r_ctl.rc_resend; 12170 #ifdef BBR_INVARIANTS 12171 doing_retran_from = 1; 12172 #endif 12173 /* Remove any TLP flags its a RACK or T-O */ 12174 rsm->r_flags &= ~BBR_TLP; 12175 bbr->r_ctl.rc_resend = NULL; 12176 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 12177 #ifdef BBR_INVARIANTS 12178 panic("Huh, tp:%p bbr:%p rsm:%p start:%u < snd_una:%u\n", 12179 tp, bbr, rsm, rsm->r_start, tp->snd_una); 12180 goto recheck_resend; 12181 #else 12182 /* TSNH */ 12183 rsm = NULL; 12184 goto recheck_resend; 12185 #endif 12186 } 12187 if (rsm->r_flags & BBR_HAS_SYN) { 12188 /* Only retransmit a SYN by itself */ 12189 len = 0; 12190 if ((flags & TH_SYN) == 0) { 12191 /* Huh something is wrong */ 12192 rsm->r_start++; 12193 if (rsm->r_start == rsm->r_end) { 12194 /* Clean it up, somehow we missed the ack? */ 12195 bbr_log_syn(tp, NULL); 12196 } else { 12197 /* TFO with data? */ 12198 rsm->r_flags &= ~BBR_HAS_SYN; 12199 len = rsm->r_end - rsm->r_start; 12200 } 12201 } else { 12202 /* Retransmitting SYN */ 12203 rsm = NULL; 12204 SOCK_SENDBUF_LOCK(so); 12205 goto send; 12206 } 12207 } else 12208 len = rsm->r_end - rsm->r_start; 12209 if ((bbr->rc_resends_use_tso == 0) && 12210 (len > maxseg)) { 12211 len = maxseg; 12212 more_to_rxt = 1; 12213 } 12214 sb_offset = rsm->r_start - tp->snd_una; 12215 if (len > 0) { 12216 sack_rxmit = 1; 12217 KMOD_TCPSTAT_INC(tcps_sack_rexmits); 12218 KMOD_TCPSTAT_ADD(tcps_sack_rexmit_bytes, 12219 min(len, maxseg)); 12220 } else { 12221 /* I dont think this can happen */ 12222 rsm = NULL; 12223 goto recheck_resend; 12224 } 12225 BBR_STAT_INC(bbr_resends_set); 12226 } else if (bbr->r_ctl.rc_tlp_send) { 12227 /* 12228 * Tail loss probe 12229 */ 12230 doing_tlp = 1; 12231 rsm = bbr->r_ctl.rc_tlp_send; 12232 bbr->r_ctl.rc_tlp_send = NULL; 12233 sack_rxmit = 1; 12234 len = rsm->r_end - rsm->r_start; 12235 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12236 len = maxseg; 12237 12238 if (SEQ_GT(tp->snd_una, rsm->r_start)) { 12239 #ifdef BBR_INVARIANTS 12240 panic("tp:%p bbc:%p snd_una:%u rsm:%p r_start:%u", 12241 tp, bbr, tp->snd_una, rsm, rsm->r_start); 12242 #else 12243 /* TSNH */ 12244 rsm = NULL; 12245 goto recheck_resend; 12246 #endif 12247 } 12248 sb_offset = rsm->r_start - tp->snd_una; 12249 BBR_STAT_INC(bbr_tlp_set); 12250 } 12251 /* 12252 * Enforce a connection sendmap count limit if set 12253 * as long as we are not retransmiting. 12254 */ 12255 if ((rsm == NULL) && 12256 (V_tcp_map_entries_limit > 0) && 12257 (bbr->r_ctl.rc_num_maps_alloced >= V_tcp_map_entries_limit)) { 12258 BBR_STAT_INC(bbr_alloc_limited); 12259 if (!bbr->alloc_limit_reported) { 12260 bbr->alloc_limit_reported = 1; 12261 BBR_STAT_INC(bbr_alloc_limited_conns); 12262 } 12263 goto just_return_nolock; 12264 } 12265 #ifdef BBR_INVARIANTS 12266 if (rsm && SEQ_LT(rsm->r_start, tp->snd_una)) { 12267 panic("tp:%p bbr:%p rsm:%p sb_offset:%u len:%u", 12268 tp, bbr, rsm, sb_offset, len); 12269 } 12270 #endif 12271 /* 12272 * Get standard flags, and add SYN or FIN if requested by 'hidden' 12273 * state flags. 12274 */ 12275 if (tp->t_flags & TF_NEEDFIN && (rsm == NULL)) 12276 flags |= TH_FIN; 12277 if (tp->t_flags & TF_NEEDSYN) 12278 flags |= TH_SYN; 12279 12280 if (rsm && (rsm->r_flags & BBR_HAS_FIN)) { 12281 /* we are retransmitting the fin */ 12282 len--; 12283 if (len) { 12284 /* 12285 * When retransmitting data do *not* include the 12286 * FIN. This could happen from a TLP probe if we 12287 * allowed data with a FIN. 12288 */ 12289 flags &= ~TH_FIN; 12290 } 12291 } else if (rsm) { 12292 if (flags & TH_FIN) 12293 flags &= ~TH_FIN; 12294 } 12295 if ((sack_rxmit == 0) && (prefetch_rsm == 0)) { 12296 void *end_rsm; 12297 12298 end_rsm = TAILQ_LAST_FAST(&bbr->r_ctl.rc_tmap, bbr_sendmap, r_tnext); 12299 if (end_rsm) 12300 kern_prefetch(end_rsm, &prefetch_rsm); 12301 prefetch_rsm = 1; 12302 } 12303 SOCK_SENDBUF_LOCK(so); 12304 /* 12305 * If snd_nxt == snd_max and we have transmitted a FIN, the 12306 * sb_offset will be > 0 even if so_snd.sb_cc is 0, resulting in a 12307 * negative length. This can also occur when TCP opens up its 12308 * congestion window while receiving additional duplicate acks after 12309 * fast-retransmit because TCP will reset snd_nxt to snd_max after 12310 * the fast-retransmit. 12311 * 12312 * In the normal retransmit-FIN-only case, however, snd_nxt will be 12313 * set to snd_una, the sb_offset will be 0, and the length may wind 12314 * up 0. 12315 * 12316 * If sack_rxmit is true we are retransmitting from the scoreboard 12317 * in which case len is already set. 12318 */ 12319 if (sack_rxmit == 0) { 12320 uint32_t avail; 12321 12322 avail = sbavail(sb); 12323 if (SEQ_GT(tp->snd_max, tp->snd_una)) 12324 sb_offset = tp->snd_max - tp->snd_una; 12325 else 12326 sb_offset = 0; 12327 if (bbr->rc_tlp_new_data) { 12328 /* TLP is forcing out new data */ 12329 uint32_t tlplen; 12330 12331 doing_tlp = 1; 12332 tlplen = maxseg; 12333 12334 if (tlplen > (uint32_t)(avail - sb_offset)) { 12335 tlplen = (uint32_t)(avail - sb_offset); 12336 } 12337 if (tlplen > tp->snd_wnd) { 12338 len = tp->snd_wnd; 12339 } else { 12340 len = tlplen; 12341 } 12342 bbr->rc_tlp_new_data = 0; 12343 } else { 12344 len = bbr_what_can_we_send(tp, bbr, sendwin, avail, sb_offset, cts); 12345 if ((len < p_maxseg) && 12346 (bbr->rc_in_persist == 0) && 12347 (ctf_outstanding(tp) >= (2 * p_maxseg)) && 12348 ((avail - sb_offset) >= p_maxseg)) { 12349 /* 12350 * We are not completing whats in the socket 12351 * buffer (i.e. there is at least a segment 12352 * waiting to send) and we have 2 or more 12353 * segments outstanding. There is no sense 12354 * of sending a little piece. Lets defer and 12355 * and wait until we can send a whole 12356 * segment. 12357 */ 12358 len = 0; 12359 } 12360 if (bbr->rc_in_persist) { 12361 /* 12362 * We are in persists, figure out if 12363 * a retransmit is available (maybe the previous 12364 * persists we sent) or if we have to send new 12365 * data. 12366 */ 12367 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 12368 if (rsm) { 12369 len = rsm->r_end - rsm->r_start; 12370 if (rsm->r_flags & BBR_HAS_FIN) 12371 len--; 12372 if ((bbr->rc_resends_use_tso == 0) && (len > maxseg)) 12373 len = maxseg; 12374 if (len > 1) 12375 BBR_STAT_INC(bbr_persist_reneg); 12376 /* 12377 * XXXrrs we could force the len to 12378 * 1 byte here to cause the chunk to 12379 * split apart.. but that would then 12380 * mean we always retransmit it as 12381 * one byte even after the window 12382 * opens. 12383 */ 12384 sack_rxmit = 1; 12385 sb_offset = rsm->r_start - tp->snd_una; 12386 } else { 12387 /* 12388 * First time through in persists or peer 12389 * acked our one byte. Though we do have 12390 * to have something in the sb. 12391 */ 12392 len = 1; 12393 sb_offset = 0; 12394 if (avail == 0) 12395 len = 0; 12396 } 12397 } 12398 } 12399 } 12400 if (prefetch_so_done == 0) { 12401 kern_prefetch(so, &prefetch_so_done); 12402 prefetch_so_done = 1; 12403 } 12404 /* 12405 * Lop off SYN bit if it has already been sent. However, if this is 12406 * SYN-SENT state and if segment contains data and if we don't know 12407 * that foreign host supports TAO, suppress sending segment. 12408 */ 12409 if ((flags & TH_SYN) && (rsm == NULL) && 12410 SEQ_GT(tp->snd_max, tp->snd_una)) { 12411 if (tp->t_state != TCPS_SYN_RECEIVED) 12412 flags &= ~TH_SYN; 12413 /* 12414 * When sending additional segments following a TFO SYN|ACK, 12415 * do not include the SYN bit. 12416 */ 12417 if ((tp->t_flags & TF_FASTOPEN) && 12418 (tp->t_state == TCPS_SYN_RECEIVED)) 12419 flags &= ~TH_SYN; 12420 sb_offset--, len++; 12421 if (sbavail(sb) == 0) 12422 len = 0; 12423 } else if ((flags & TH_SYN) && rsm) { 12424 /* 12425 * Subtract one from the len for the SYN being 12426 * retransmitted. 12427 */ 12428 len--; 12429 } 12430 /* 12431 * Be careful not to send data and/or FIN on SYN segments. This 12432 * measure is needed to prevent interoperability problems with not 12433 * fully conformant TCP implementations. 12434 */ 12435 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 12436 len = 0; 12437 flags &= ~TH_FIN; 12438 } 12439 /* 12440 * On TFO sockets, ensure no data is sent in the following cases: 12441 * 12442 * - When retransmitting SYN|ACK on a passively-created socket 12443 * - When retransmitting SYN on an actively created socket 12444 * - When sending a zero-length cookie (cookie request) on an 12445 * actively created socket 12446 * - When the socket is in the CLOSED state (RST is being sent) 12447 */ 12448 if ((tp->t_flags & TF_FASTOPEN) && 12449 (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 12450 ((tp->t_state == TCPS_SYN_SENT) && 12451 (tp->t_tfo_client_cookie_len == 0)) || 12452 (flags & TH_RST))) { 12453 len = 0; 12454 sack_rxmit = 0; 12455 rsm = NULL; 12456 } 12457 /* Without fast-open there should never be data sent on a SYN */ 12458 if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) 12459 len = 0; 12460 if (len <= 0) { 12461 /* 12462 * If FIN has been sent but not acked, but we haven't been 12463 * called to retransmit, len will be < 0. Otherwise, window 12464 * shrank after we sent into it. If window shrank to 0, 12465 * cancel pending retransmit, pull snd_nxt back to (closed) 12466 * window, and set the persist timer if it isn't already 12467 * going. If the window didn't close completely, just wait 12468 * for an ACK. 12469 * 12470 * We also do a general check here to ensure that we will 12471 * set the persist timer when we have data to send, but a 12472 * 0-byte window. This makes sure the persist timer is set 12473 * even if the packet hits one of the "goto send" lines 12474 * below. 12475 */ 12476 len = 0; 12477 if ((tp->snd_wnd == 0) && 12478 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12479 (tp->snd_una == tp->snd_max) && 12480 (sb_offset < (int)sbavail(sb))) { 12481 /* 12482 * Not enough room in the rwnd to send 12483 * a paced segment out. 12484 */ 12485 bbr_enter_persist(tp, bbr, cts, __LINE__); 12486 } 12487 } else if ((rsm == NULL) && 12488 (doing_tlp == 0) && 12489 (len < bbr->r_ctl.rc_pace_max_segs)) { 12490 /* 12491 * We are not sending a full segment for 12492 * some reason. Should we not send anything (think 12493 * sws or persists)? 12494 */ 12495 if ((tp->snd_wnd < min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12496 (TCPS_HAVEESTABLISHED(tp->t_state)) && 12497 (len < (int)(sbavail(sb) - sb_offset))) { 12498 /* 12499 * Here the rwnd is less than 12500 * the pacing size, this is not a retransmit, 12501 * we are established and 12502 * the send is not the last in the socket buffer 12503 * lets not send, and possibly enter persists. 12504 */ 12505 len = 0; 12506 if (tp->snd_max == tp->snd_una) 12507 bbr_enter_persist(tp, bbr, cts, __LINE__); 12508 } else if ((tp->snd_cwnd >= bbr->r_ctl.rc_pace_max_segs) && 12509 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12510 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12511 (len < (int)(sbavail(sb) - sb_offset)) && 12512 (len < bbr_minseg(bbr))) { 12513 /* 12514 * Here we are not retransmitting, and 12515 * the cwnd is not so small that we could 12516 * not send at least a min size (rxt timer 12517 * not having gone off), We have 2 segments or 12518 * more already in flight, its not the tail end 12519 * of the socket buffer and the cwnd is blocking 12520 * us from sending out minimum pacing segment size. 12521 * Lets not send anything. 12522 */ 12523 bbr->rc_cwnd_limited = 1; 12524 len = 0; 12525 } else if (((tp->snd_wnd - ctf_outstanding(tp)) < 12526 min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) && 12527 (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12528 bbr->r_ctl.rc_lost_bytes)) > (2 * maxseg)) && 12529 (len < (int)(sbavail(sb) - sb_offset)) && 12530 (TCPS_HAVEESTABLISHED(tp->t_state))) { 12531 /* 12532 * Here we have a send window but we have 12533 * filled it up and we can't send another pacing segment. 12534 * We also have in flight more than 2 segments 12535 * and we are not completing the sb i.e. we allow 12536 * the last bytes of the sb to go out even if 12537 * its not a full pacing segment. 12538 */ 12539 len = 0; 12540 } 12541 } 12542 /* len will be >= 0 after this point. */ 12543 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 12544 tcp_sndbuf_autoscale(tp, so, sendwin); 12545 /* 12546 * 12547 */ 12548 if (bbr->rc_in_persist && 12549 len && 12550 (rsm == NULL) && 12551 (len < min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs))) { 12552 /* 12553 * We are in persist, not doing a retransmit and don't have enough space 12554 * yet to send a full TSO. So is it at the end of the sb 12555 * if so we need to send else nuke to 0 and don't send. 12556 */ 12557 int sbleft; 12558 if (sbavail(sb) > sb_offset) 12559 sbleft = sbavail(sb) - sb_offset; 12560 else 12561 sbleft = 0; 12562 if (sbleft >= min((bbr->r_ctl.rc_high_rwnd/2), bbr->r_ctl.rc_pace_max_segs)) { 12563 /* not at end of sb lets not send */ 12564 len = 0; 12565 } 12566 } 12567 /* 12568 * Decide if we can use TCP Segmentation Offloading (if supported by 12569 * hardware). 12570 * 12571 * TSO may only be used if we are in a pure bulk sending state. The 12572 * presence of TCP-MD5, SACK retransmits, SACK advertizements and IP 12573 * options prevent using TSO. With TSO the TCP header is the same 12574 * (except for the sequence number) for all generated packets. This 12575 * makes it impossible to transmit any options which vary per 12576 * generated segment or packet. 12577 * 12578 * IPv4 handling has a clear separation of ip options and ip header 12579 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() 12580 * does the right thing below to provide length of just ip options 12581 * and thus checking for ipoptlen is enough to decide if ip options 12582 * are present. 12583 */ 12584 #ifdef INET6 12585 if (isipv6) 12586 ipoptlen = ip6_optlen(inp); 12587 else 12588 #endif 12589 if (inp->inp_options) 12590 ipoptlen = inp->inp_options->m_len - 12591 offsetof(struct ipoption, ipopt_list); 12592 else 12593 ipoptlen = 0; 12594 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12595 /* 12596 * Pre-calculate here as we save another lookup into the darknesses 12597 * of IPsec that way and can actually decide if TSO is ok. 12598 */ 12599 #ifdef INET6 12600 if (isipv6 && IPSEC_ENABLED(ipv6)) 12601 ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 12602 #ifdef INET 12603 else 12604 #endif 12605 #endif /* INET6 */ 12606 #ifdef INET 12607 if (IPSEC_ENABLED(ipv4)) 12608 ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 12609 #endif /* INET */ 12610 #endif /* IPSEC */ 12611 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12612 ipoptlen += ipsec_optlen; 12613 #endif 12614 if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && 12615 (len > maxseg) && 12616 (tp->t_port == 0) && 12617 ((tp->t_flags & TF_SIGNATURE) == 0) && 12618 ipoptlen == 0) 12619 tso = 1; 12620 12621 recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 12622 (long)TCP_MAXWIN << tp->rcv_scale); 12623 /* 12624 * Sender silly window avoidance. We transmit under the following 12625 * conditions when len is non-zero: 12626 * 12627 * - We have a full segment (or more with TSO) - This is the last 12628 * buffer in a write()/send() and we are either idle or running 12629 * NODELAY - we've timed out (e.g. persist timer) - we have more 12630 * then 1/2 the maximum send window's worth of data (receiver may be 12631 * limited the window size) - we need to retransmit 12632 */ 12633 if (rsm) 12634 goto send; 12635 if (len) { 12636 if (sack_rxmit) 12637 goto send; 12638 if (len >= p_maxseg) 12639 goto send; 12640 /* 12641 * NOTE! on localhost connections an 'ack' from the remote 12642 * end may occur synchronously with the output and cause us 12643 * to flush a buffer queued with moretocome. XXX 12644 * 12645 */ 12646 if (((tp->t_flags & TF_MORETOCOME) == 0) && /* normal case */ 12647 ((tp->t_flags & TF_NODELAY) || 12648 ((uint32_t)len + (uint32_t)sb_offset) >= sbavail(&so->so_snd)) && 12649 (tp->t_flags & TF_NOPUSH) == 0) { 12650 goto send; 12651 } 12652 if ((tp->snd_una == tp->snd_max) && len) { /* Nothing outstanding */ 12653 goto send; 12654 } 12655 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) { 12656 goto send; 12657 } 12658 } 12659 /* 12660 * Sending of standalone window updates. 12661 * 12662 * Window updates are important when we close our window due to a 12663 * full socket buffer and are opening it again after the application 12664 * reads data from it. Once the window has opened again and the 12665 * remote end starts to send again the ACK clock takes over and 12666 * provides the most current window information. 12667 * 12668 * We must avoid the silly window syndrome whereas every read from 12669 * the receive buffer, no matter how small, causes a window update 12670 * to be sent. We also should avoid sending a flurry of window 12671 * updates when the socket buffer had queued a lot of data and the 12672 * application is doing small reads. 12673 * 12674 * Prevent a flurry of pointless window updates by only sending an 12675 * update when we can increase the advertized window by more than 12676 * 1/4th of the socket buffer capacity. When the buffer is getting 12677 * full or is very small be more aggressive and send an update 12678 * whenever we can increase by two mss sized segments. In all other 12679 * situations the ACK's to new incoming data will carry further 12680 * window increases. 12681 * 12682 * Don't send an independent window update if a delayed ACK is 12683 * pending (it will get piggy-backed on it) or the remote side 12684 * already has done a half-close and won't send more data. Skip 12685 * this if the connection is in T/TCP half-open state. 12686 */ 12687 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 12688 !(tp->t_flags & TF_DELACK) && 12689 !TCPS_HAVERCVDFIN(tp->t_state)) { 12690 /* Check to see if we should do a window update */ 12691 if (bbr_window_update_needed(tp, so, recwin, maxseg)) 12692 goto send; 12693 } 12694 /* 12695 * Send if we owe the peer an ACK, RST, SYN. ACKNOW 12696 * is also a catch-all for the retransmit timer timeout case. 12697 */ 12698 if (tp->t_flags & TF_ACKNOW) { 12699 goto send; 12700 } 12701 if (flags & TH_RST) { 12702 /* Always send a RST if one is due */ 12703 goto send; 12704 } 12705 if ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0) { 12706 goto send; 12707 } 12708 /* 12709 * If our state indicates that FIN should be sent and we have not 12710 * yet done so, then we need to send. 12711 */ 12712 if (flags & TH_FIN && 12713 ((tp->t_flags & TF_SENTFIN) == 0)) { 12714 goto send; 12715 } 12716 /* 12717 * No reason to send a segment, just return. 12718 */ 12719 just_return: 12720 SOCK_SENDBUF_UNLOCK(so); 12721 just_return_nolock: 12722 if (tot_len) 12723 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 12724 if (bbr->rc_no_pacing) 12725 slot = 0; 12726 if (tot_len == 0) { 12727 if ((ctf_outstanding(tp) + min((bbr->r_ctl.rc_high_rwnd/2), bbr_minseg(bbr))) >= 12728 tp->snd_wnd) { 12729 BBR_STAT_INC(bbr_rwnd_limited); 12730 app_limited = BBR_JR_RWND_LIMITED; 12731 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12732 if ((bbr->rc_in_persist == 0) && 12733 TCPS_HAVEESTABLISHED(tp->t_state) && 12734 (tp->snd_max == tp->snd_una) && 12735 sbavail(&so->so_snd)) { 12736 /* No send window.. we must enter persist */ 12737 bbr_enter_persist(tp, bbr, bbr->r_ctl.rc_rcvtime, __LINE__); 12738 } 12739 } else if (ctf_outstanding(tp) >= sbavail(sb)) { 12740 BBR_STAT_INC(bbr_app_limited); 12741 app_limited = BBR_JR_APP_LIMITED; 12742 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12743 } else if ((ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12744 bbr->r_ctl.rc_lost_bytes)) + p_maxseg) >= tp->snd_cwnd) { 12745 BBR_STAT_INC(bbr_cwnd_limited); 12746 app_limited = BBR_JR_CWND_LIMITED; 12747 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12748 bbr->r_ctl.rc_lost_bytes))); 12749 bbr->rc_cwnd_limited = 1; 12750 } else { 12751 BBR_STAT_INC(bbr_app_limited); 12752 app_limited = BBR_JR_APP_LIMITED; 12753 bbr_cwnd_limiting(tp, bbr, ctf_outstanding(tp)); 12754 } 12755 bbr->r_ctl.rc_hptsi_agg_delay = 0; 12756 bbr->r_agg_early_set = 0; 12757 bbr->r_ctl.rc_agg_early = 0; 12758 bbr->r_ctl.rc_last_delay_val = 0; 12759 } else if (bbr->rc_use_google == 0) 12760 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 12761 /* Are we app limited? */ 12762 if ((app_limited == BBR_JR_APP_LIMITED) || 12763 (app_limited == BBR_JR_RWND_LIMITED)) { 12764 /** 12765 * We are application limited. 12766 */ 12767 bbr->r_ctl.r_app_limited_until = (ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 12768 bbr->r_ctl.rc_lost_bytes)) + bbr->r_ctl.rc_delivered); 12769 } 12770 if (tot_len == 0) 12771 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_JUSTRET], 1); 12772 /* Dont update the time if we did not send */ 12773 bbr->r_ctl.rc_last_delay_val = 0; 12774 bbr->rc_output_starts_timer = 1; 12775 bbr_start_hpts_timer(bbr, tp, cts, 9, slot, tot_len); 12776 bbr_log_type_just_return(bbr, cts, tot_len, hpts_calling, app_limited, p_maxseg, len); 12777 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 12778 /* Make sure snd_nxt is drug up */ 12779 tp->snd_nxt = tp->snd_max; 12780 } 12781 return (error); 12782 12783 send: 12784 if (doing_tlp == 0) { 12785 /* 12786 * Data not a TLP, and its not the rxt firing. If it is the 12787 * rxt firing, we want to leave the tlp_in_progress flag on 12788 * so we don't send another TLP. It has to be a rack timer 12789 * or normal send (response to acked data) to clear the tlp 12790 * in progress flag. 12791 */ 12792 bbr->rc_tlp_in_progress = 0; 12793 bbr->rc_tlp_rtx_out = 0; 12794 } else { 12795 /* 12796 * Its a TLP. 12797 */ 12798 bbr->rc_tlp_in_progress = 1; 12799 } 12800 bbr_timer_cancel(bbr, __LINE__, cts); 12801 if (rsm == NULL) { 12802 if (sbused(sb) > 0) { 12803 /* 12804 * This is sub-optimal. We only send a stand alone 12805 * FIN on its own segment. 12806 */ 12807 if (flags & TH_FIN) { 12808 flags &= ~TH_FIN; 12809 if ((len == 0) && ((tp->t_flags & TF_ACKNOW) == 0)) { 12810 /* Lets not send this */ 12811 slot = 0; 12812 goto just_return; 12813 } 12814 } 12815 } 12816 } else { 12817 /* 12818 * We do *not* send a FIN on a retransmit if it has data. 12819 * The if clause here where len > 1 should never come true. 12820 */ 12821 if ((len > 0) && 12822 (((rsm->r_flags & BBR_HAS_FIN) == 0) && 12823 (flags & TH_FIN))) { 12824 flags &= ~TH_FIN; 12825 len--; 12826 } 12827 } 12828 SOCK_SENDBUF_LOCK_ASSERT(so); 12829 if (len > 0) { 12830 if ((tp->snd_una == tp->snd_max) && 12831 (bbr_calc_time(cts, bbr->r_ctl.rc_went_idle_time) >= bbr_rtt_probe_time)) { 12832 /* 12833 * This qualifies as a RTT_PROBE session since we 12834 * drop the data outstanding to nothing and waited 12835 * more than bbr_rtt_probe_time. 12836 */ 12837 bbr_log_rtt_shrinks(bbr, cts, 0, 0, __LINE__, BBR_RTTS_WASIDLE, 0); 12838 bbr_set_reduced_rtt(bbr, cts, __LINE__); 12839 } 12840 if (len >= maxseg) 12841 tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 12842 else 12843 tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 12844 } 12845 /* 12846 * Before ESTABLISHED, force sending of initial options unless TCP 12847 * set not to do any options. NOTE: we assume that the IP/TCP header 12848 * plus TCP options always fit in a single mbuf, leaving room for a 12849 * maximum link header, i.e. max_linkhdr + sizeof (struct tcpiphdr) 12850 * + optlen <= MCLBYTES 12851 */ 12852 optlen = 0; 12853 #ifdef INET6 12854 if (isipv6) 12855 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 12856 else 12857 #endif 12858 hdrlen = sizeof(struct tcpiphdr); 12859 12860 /* 12861 * Compute options for segment. We only have to care about SYN and 12862 * established connection segments. Options for SYN-ACK segments 12863 * are handled in TCP syncache. 12864 */ 12865 to.to_flags = 0; 12866 local_options = 0; 12867 if ((tp->t_flags & TF_NOOPT) == 0) { 12868 /* Maximum segment size. */ 12869 if (flags & TH_SYN) { 12870 to.to_mss = tcp_mssopt(&inp->inp_inc); 12871 if (tp->t_port) 12872 to.to_mss -= V_tcp_udp_tunneling_overhead; 12873 to.to_flags |= TOF_MSS; 12874 /* 12875 * On SYN or SYN|ACK transmits on TFO connections, 12876 * only include the TFO option if it is not a 12877 * retransmit, as the presence of the TFO option may 12878 * have caused the original SYN or SYN|ACK to have 12879 * been dropped by a middlebox. 12880 */ 12881 if ((tp->t_flags & TF_FASTOPEN) && 12882 (tp->t_rxtshift == 0)) { 12883 if (tp->t_state == TCPS_SYN_RECEIVED) { 12884 to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 12885 to.to_tfo_cookie = 12886 (u_int8_t *)&tp->t_tfo_cookie.server; 12887 to.to_flags |= TOF_FASTOPEN; 12888 wanted_cookie = 1; 12889 } else if (tp->t_state == TCPS_SYN_SENT) { 12890 to.to_tfo_len = 12891 tp->t_tfo_client_cookie_len; 12892 to.to_tfo_cookie = 12893 tp->t_tfo_cookie.client; 12894 to.to_flags |= TOF_FASTOPEN; 12895 wanted_cookie = 1; 12896 } 12897 } 12898 } 12899 /* Window scaling. */ 12900 if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 12901 to.to_wscale = tp->request_r_scale; 12902 to.to_flags |= TOF_SCALE; 12903 } 12904 /* Timestamps. */ 12905 if ((tp->t_flags & TF_RCVD_TSTMP) || 12906 ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 12907 to.to_tsval = tcp_tv_to_mssectick(&bbr->rc_tv) + tp->ts_offset; 12908 to.to_tsecr = tp->ts_recent; 12909 to.to_flags |= TOF_TS; 12910 local_options += TCPOLEN_TIMESTAMP + 2; 12911 } 12912 /* Set receive buffer autosizing timestamp. */ 12913 if (tp->rfbuf_ts == 0 && 12914 (so->so_rcv.sb_flags & SB_AUTOSIZE)) 12915 tp->rfbuf_ts = tcp_tv_to_mssectick(&bbr->rc_tv); 12916 /* Selective ACK's. */ 12917 if (flags & TH_SYN) 12918 to.to_flags |= TOF_SACKPERM; 12919 else if (TCPS_HAVEESTABLISHED(tp->t_state) && 12920 tp->rcv_numsacks > 0) { 12921 to.to_flags |= TOF_SACK; 12922 to.to_nsacks = tp->rcv_numsacks; 12923 to.to_sacks = (u_char *)tp->sackblks; 12924 } 12925 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 12926 /* TCP-MD5 (RFC2385). */ 12927 if (tp->t_flags & TF_SIGNATURE) 12928 to.to_flags |= TOF_SIGNATURE; 12929 #endif /* TCP_SIGNATURE */ 12930 12931 /* Processing the options. */ 12932 hdrlen += (optlen = tcp_addoptions(&to, opt)); 12933 /* 12934 * If we wanted a TFO option to be added, but it was unable 12935 * to fit, ensure no data is sent. 12936 */ 12937 if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 12938 !(to.to_flags & TOF_FASTOPEN)) 12939 len = 0; 12940 } 12941 if (tp->t_port) { 12942 if (V_tcp_udp_tunneling_port == 0) { 12943 /* The port was removed?? */ 12944 SOCK_SENDBUF_UNLOCK(so); 12945 return (EHOSTUNREACH); 12946 } 12947 hdrlen += sizeof(struct udphdr); 12948 } 12949 #ifdef INET6 12950 if (isipv6) 12951 ipoptlen = ip6_optlen(inp); 12952 else 12953 #endif 12954 if (inp->inp_options) 12955 ipoptlen = inp->inp_options->m_len - 12956 offsetof(struct ipoption, ipopt_list); 12957 else 12958 ipoptlen = 0; 12959 ipoptlen = 0; 12960 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12961 ipoptlen += ipsec_optlen; 12962 #endif 12963 if (bbr->rc_last_options != local_options) { 12964 /* 12965 * Cache the options length this generally does not change 12966 * on a connection. We use this to calculate TSO. 12967 */ 12968 bbr->rc_last_options = local_options; 12969 } 12970 maxseg = tp->t_maxseg - (ipoptlen + optlen); 12971 p_maxseg = min(maxseg, pace_max_segs); 12972 /* 12973 * Adjust data length if insertion of options will bump the packet 12974 * length beyond the t_maxseg length. Clear the FIN bit because we 12975 * cut off the tail of the segment. 12976 */ 12977 if (len > maxseg) { 12978 if (len != 0 && (flags & TH_FIN)) { 12979 flags &= ~TH_FIN; 12980 } 12981 if (tso) { 12982 uint32_t moff; 12983 int32_t max_len; 12984 12985 /* extract TSO information */ 12986 if_hw_tsomax = tp->t_tsomax; 12987 if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 12988 if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 12989 KASSERT(ipoptlen == 0, 12990 ("%s: TSO can't do IP options", __func__)); 12991 12992 /* 12993 * Check if we should limit by maximum payload 12994 * length: 12995 */ 12996 if (if_hw_tsomax != 0) { 12997 /* compute maximum TSO length */ 12998 max_len = (if_hw_tsomax - hdrlen - 12999 max_linkhdr); 13000 if (max_len <= 0) { 13001 len = 0; 13002 } else if (len > max_len) { 13003 len = max_len; 13004 } 13005 } 13006 /* 13007 * Prevent the last segment from being fractional 13008 * unless the send sockbuf can be emptied: 13009 */ 13010 if ((sb_offset + len) < sbavail(sb)) { 13011 moff = len % (uint32_t)maxseg; 13012 if (moff != 0) { 13013 len -= moff; 13014 } 13015 } 13016 /* 13017 * In case there are too many small fragments don't 13018 * use TSO: 13019 */ 13020 if (len <= maxseg) { 13021 len = maxseg; 13022 tso = 0; 13023 } 13024 } else { 13025 /* Not doing TSO */ 13026 if (optlen + ipoptlen >= tp->t_maxseg) { 13027 /* 13028 * Since we don't have enough space to put 13029 * the IP header chain and the TCP header in 13030 * one packet as required by RFC 7112, don't 13031 * send it. Also ensure that at least one 13032 * byte of the payload can be put into the 13033 * TCP segment. 13034 */ 13035 SOCK_SENDBUF_UNLOCK(so); 13036 error = EMSGSIZE; 13037 sack_rxmit = 0; 13038 goto out; 13039 } 13040 len = maxseg; 13041 } 13042 } else { 13043 /* Not doing TSO */ 13044 if_hw_tsomaxsegcount = 0; 13045 tso = 0; 13046 } 13047 KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 13048 ("%s: len > IP_MAXPACKET", __func__)); 13049 #ifdef DIAGNOSTIC 13050 #ifdef INET6 13051 if (max_linkhdr + hdrlen > MCLBYTES) 13052 #else 13053 if (max_linkhdr + hdrlen > MHLEN) 13054 #endif 13055 panic("tcphdr too big"); 13056 #endif 13057 /* 13058 * This KASSERT is here to catch edge cases at a well defined place. 13059 * Before, those had triggered (random) panic conditions further 13060 * down. 13061 */ 13062 #ifdef BBR_INVARIANTS 13063 if (sack_rxmit) { 13064 if (SEQ_LT(rsm->r_start, tp->snd_una)) { 13065 panic("RSM:%p TP:%p bbr:%p start:%u is < snd_una:%u", 13066 rsm, tp, bbr, rsm->r_start, tp->snd_una); 13067 } 13068 } 13069 #endif 13070 KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 13071 if ((len == 0) && 13072 (flags & TH_FIN) && 13073 (sbused(sb))) { 13074 /* 13075 * We have outstanding data, don't send a fin by itself!. 13076 */ 13077 slot = 0; 13078 goto just_return; 13079 } 13080 /* 13081 * Grab a header mbuf, attaching a copy of data to be transmitted, 13082 * and initialize the header from the template for sends on this 13083 * connection. 13084 */ 13085 if (len) { 13086 uint32_t moff; 13087 13088 /* 13089 * We place a limit on sending with hptsi. 13090 */ 13091 if ((rsm == NULL) && len > pace_max_segs) 13092 len = pace_max_segs; 13093 if (len <= maxseg) 13094 tso = 0; 13095 #ifdef INET6 13096 if (MHLEN < hdrlen + max_linkhdr) 13097 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13098 else 13099 #endif 13100 m = m_gethdr(M_NOWAIT, MT_DATA); 13101 13102 if (m == NULL) { 13103 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13104 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13105 SOCK_SENDBUF_UNLOCK(so); 13106 error = ENOBUFS; 13107 sack_rxmit = 0; 13108 goto out; 13109 } 13110 m->m_data += max_linkhdr; 13111 m->m_len = hdrlen; 13112 /* 13113 * Start the m_copy functions from the closest mbuf to the 13114 * sb_offset in the socket buffer chain. 13115 */ 13116 if ((sb_offset > sbavail(sb)) || ((len + sb_offset) > sbavail(sb))) { 13117 #ifdef BBR_INVARIANTS 13118 if ((len + sb_offset) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) 13119 panic("tp:%p bbr:%p len:%u sb_offset:%u sbavail:%u rsm:%p %u:%u:%u", 13120 tp, bbr, len, sb_offset, sbavail(sb), rsm, 13121 doing_retran_from, 13122 picked_up_retran, 13123 doing_tlp); 13124 13125 #endif 13126 /* 13127 * In this messed up situation we have two choices, 13128 * a) pretend the send worked, and just start timers 13129 * and what not (not good since that may lead us 13130 * back here a lot). <or> b) Send the lowest segment 13131 * in the map. <or> c) Drop the connection. Lets do 13132 * <b> which if it continues to happen will lead to 13133 * <c> via timeouts. 13134 */ 13135 BBR_STAT_INC(bbr_offset_recovery); 13136 rsm = TAILQ_FIRST(&bbr->r_ctl.rc_map); 13137 sb_offset = 0; 13138 if (rsm == NULL) { 13139 sack_rxmit = 0; 13140 len = sbavail(sb); 13141 } else { 13142 sack_rxmit = 1; 13143 if (rsm->r_start != tp->snd_una) { 13144 /* 13145 * Things are really messed up, <c> 13146 * is the only thing to do. 13147 */ 13148 BBR_STAT_INC(bbr_offset_drop); 13149 SOCK_SENDBUF_UNLOCK(so); 13150 (void)m_free(m); 13151 return (-EFAULT); /* tcp_drop() */ 13152 } 13153 len = rsm->r_end - rsm->r_start; 13154 } 13155 if (len > sbavail(sb)) 13156 len = sbavail(sb); 13157 if (len > maxseg) 13158 len = maxseg; 13159 } 13160 mb = sbsndptr_noadv(sb, sb_offset, &moff); 13161 if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 13162 m_copydata(mb, moff, (int)len, 13163 mtod(m, caddr_t)+hdrlen); 13164 if (rsm == NULL) 13165 sbsndptr_adv(sb, mb, len); 13166 m->m_len += len; 13167 } else { 13168 struct sockbuf *msb; 13169 13170 if (rsm) 13171 msb = NULL; 13172 else 13173 msb = sb; 13174 #ifdef BBR_INVARIANTS 13175 if ((len + moff) > (sbavail(sb) + ((flags & (TH_FIN | TH_SYN)) ? 1 : 0))) { 13176 if (rsm) { 13177 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 ", 13178 tp, bbr, len, moff, 13179 sbavail(sb), rsm, 13180 tp->snd_una, rsm->r_flags, rsm->r_start, 13181 doing_retran_from, 13182 picked_up_retran, 13183 doing_tlp, sack_rxmit); 13184 } else { 13185 panic("tp:%p bbr:%p len:%u moff:%u sbavail:%u sb_offset:%u snd_una:%u", 13186 tp, bbr, len, moff, sbavail(sb), sb_offset, tp->snd_una); 13187 } 13188 } 13189 #endif 13190 m->m_next = tcp_m_copym( 13191 mb, moff, &len, 13192 if_hw_tsomaxsegcount, 13193 if_hw_tsomaxsegsize, msb, 13194 ((rsm == NULL) ? hw_tls : 0) 13195 #ifdef NETFLIX_COPY_ARGS 13196 , NULL, NULL 13197 #endif 13198 ); 13199 if (len <= maxseg) { 13200 /* 13201 * Must have ran out of mbufs for the copy 13202 * shorten it to no longer need tso. Lets 13203 * not put on sendalot since we are low on 13204 * mbufs. 13205 */ 13206 tso = 0; 13207 } 13208 if (m->m_next == NULL) { 13209 SOCK_SENDBUF_UNLOCK(so); 13210 (void)m_free(m); 13211 error = ENOBUFS; 13212 sack_rxmit = 0; 13213 goto out; 13214 } 13215 } 13216 #ifdef BBR_INVARIANTS 13217 if (tso && len < maxseg) { 13218 panic("tp:%p tso on, but len:%d < maxseg:%d", 13219 tp, len, maxseg); 13220 } 13221 if (tso && if_hw_tsomaxsegcount) { 13222 int32_t seg_cnt = 0; 13223 struct mbuf *foo; 13224 13225 foo = m; 13226 while (foo) { 13227 seg_cnt++; 13228 foo = foo->m_next; 13229 } 13230 if (seg_cnt > if_hw_tsomaxsegcount) { 13231 panic("seg_cnt:%d > max:%d", seg_cnt, if_hw_tsomaxsegcount); 13232 } 13233 } 13234 #endif 13235 /* 13236 * If we're sending everything we've got, set PUSH. (This 13237 * will keep happy those implementations which only give 13238 * data to the user when a buffer fills or a PUSH comes in.) 13239 */ 13240 if (sb_offset + len == sbused(sb) && 13241 sbused(sb) && 13242 !(flags & TH_SYN)) { 13243 flags |= TH_PUSH; 13244 } 13245 SOCK_SENDBUF_UNLOCK(so); 13246 } else { 13247 SOCK_SENDBUF_UNLOCK(so); 13248 if (tp->t_flags & TF_ACKNOW) 13249 KMOD_TCPSTAT_INC(tcps_sndacks); 13250 else if (flags & (TH_SYN | TH_FIN | TH_RST)) 13251 KMOD_TCPSTAT_INC(tcps_sndctrl); 13252 else 13253 KMOD_TCPSTAT_INC(tcps_sndwinup); 13254 13255 m = m_gethdr(M_NOWAIT, MT_DATA); 13256 if (m == NULL) { 13257 BBR_STAT_INC(bbr_failed_mbuf_aloc); 13258 bbr_log_enobuf_jmp(bbr, len, cts, __LINE__, len, 0, 0); 13259 error = ENOBUFS; 13260 /* Fudge the send time since we could not send */ 13261 sack_rxmit = 0; 13262 goto out; 13263 } 13264 #ifdef INET6 13265 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 13266 MHLEN >= hdrlen) { 13267 M_ALIGN(m, hdrlen); 13268 } else 13269 #endif 13270 m->m_data += max_linkhdr; 13271 m->m_len = hdrlen; 13272 } 13273 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13274 m->m_pkthdr.rcvif = (struct ifnet *)0; 13275 #ifdef MAC 13276 mac_inpcb_create_mbuf(inp, m); 13277 #endif 13278 #ifdef INET6 13279 if (isipv6) { 13280 ip6 = mtod(m, struct ip6_hdr *); 13281 if (tp->t_port) { 13282 udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 13283 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13284 udp->uh_dport = tp->t_port; 13285 ulen = hdrlen + len - sizeof(struct ip6_hdr); 13286 udp->uh_ulen = htons(ulen); 13287 th = (struct tcphdr *)(udp + 1); 13288 } else { 13289 th = (struct tcphdr *)(ip6 + 1); 13290 } 13291 tcpip_fillheaders(inp, tp->t_port, ip6, th); 13292 } else 13293 #endif /* INET6 */ 13294 { 13295 ip = mtod(m, struct ip *); 13296 if (tp->t_port) { 13297 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 13298 udp->uh_sport = htons(V_tcp_udp_tunneling_port); 13299 udp->uh_dport = tp->t_port; 13300 ulen = hdrlen + len - sizeof(struct ip); 13301 udp->uh_ulen = htons(ulen); 13302 th = (struct tcphdr *)(udp + 1); 13303 } else { 13304 th = (struct tcphdr *)(ip + 1); 13305 } 13306 tcpip_fillheaders(inp, tp->t_port, ip, th); 13307 } 13308 /* 13309 * If we are doing retransmissions, then snd_nxt will not reflect 13310 * the first unsent octet. For ACK only packets, we do not want the 13311 * sequence number of the retransmitted packet, we want the sequence 13312 * number of the next unsent octet. So, if there is no data (and no 13313 * SYN or FIN), use snd_max instead of snd_nxt when filling in 13314 * ti_seq. But if we are in persist state, snd_max might reflect 13315 * one byte beyond the right edge of the window, so use snd_nxt in 13316 * that case, since we know we aren't doing a retransmission. 13317 * (retransmit and persist are mutually exclusive...) 13318 */ 13319 if (sack_rxmit == 0) { 13320 if (len && ((flags & (TH_FIN | TH_SYN | TH_RST)) == 0)) { 13321 /* New data (including new persists) */ 13322 th->th_seq = htonl(tp->snd_max); 13323 bbr_seq = tp->snd_max; 13324 } else if (flags & TH_SYN) { 13325 /* Syn's always send from iss */ 13326 th->th_seq = htonl(tp->iss); 13327 bbr_seq = tp->iss; 13328 } else if (flags & TH_FIN) { 13329 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN) { 13330 /* 13331 * If we sent the fin already its 1 minus 13332 * snd_max 13333 */ 13334 th->th_seq = (htonl(tp->snd_max - 1)); 13335 bbr_seq = (tp->snd_max - 1); 13336 } else { 13337 /* First time FIN use snd_max */ 13338 th->th_seq = htonl(tp->snd_max); 13339 bbr_seq = tp->snd_max; 13340 } 13341 } else { 13342 /* 13343 * len == 0 and not persist we use snd_max, sending 13344 * an ack unless we have sent the fin then its 1 13345 * minus. 13346 */ 13347 /* 13348 * XXXRRS Question if we are in persists and we have 13349 * nothing outstanding to send and we have not sent 13350 * a FIN, we will send an ACK. In such a case it 13351 * might be better to send (tp->snd_una - 1) which 13352 * would force the peer to ack. 13353 */ 13354 if (tp->t_flags & TF_SENTFIN) { 13355 th->th_seq = htonl(tp->snd_max - 1); 13356 bbr_seq = (tp->snd_max - 1); 13357 } else { 13358 th->th_seq = htonl(tp->snd_max); 13359 bbr_seq = tp->snd_max; 13360 } 13361 } 13362 } else { 13363 /* All retransmits use the rsm to guide the send */ 13364 th->th_seq = htonl(rsm->r_start); 13365 bbr_seq = rsm->r_start; 13366 } 13367 th->th_ack = htonl(tp->rcv_nxt); 13368 if (optlen) { 13369 bcopy(opt, th + 1, optlen); 13370 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 13371 } 13372 tcp_set_flags(th, flags); 13373 /* 13374 * Calculate receive window. Don't shrink window, but avoid silly 13375 * window syndrome. 13376 */ 13377 if ((flags & TH_RST) || ((recwin < (so->so_rcv.sb_hiwat / 4) && 13378 recwin < maxseg))) 13379 recwin = 0; 13380 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 13381 recwin < (tp->rcv_adv - tp->rcv_nxt)) 13382 recwin = (tp->rcv_adv - tp->rcv_nxt); 13383 if (recwin > TCP_MAXWIN << tp->rcv_scale) 13384 recwin = TCP_MAXWIN << tp->rcv_scale; 13385 13386 /* 13387 * According to RFC1323 the window field in a SYN (i.e., a <SYN> or 13388 * <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> case is 13389 * handled in syncache. 13390 */ 13391 if (flags & TH_SYN) 13392 th->th_win = htons((u_short) 13393 (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 13394 else { 13395 /* Avoid shrinking window with window scaling. */ 13396 recwin = roundup2(recwin, 1 << tp->rcv_scale); 13397 th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13398 } 13399 /* 13400 * Adjust the RXWIN0SENT flag - indicate that we have advertised a 0 13401 * window. This may cause the remote transmitter to stall. This 13402 * flag tells soreceive() to disable delayed acknowledgements when 13403 * draining the buffer. This can occur if the receiver is 13404 * attempting to read more data than can be buffered prior to 13405 * transmitting on the connection. 13406 */ 13407 if (th->th_win == 0) { 13408 tp->t_sndzerowin++; 13409 tp->t_flags |= TF_RXWIN0SENT; 13410 } else 13411 tp->t_flags &= ~TF_RXWIN0SENT; 13412 /* 13413 * We don't support urgent data, but drag along 13414 * the pointer in case of a stack switch. 13415 */ 13416 tp->snd_up = tp->snd_una; 13417 /* 13418 * Put TCP length in extended header, and then checksum extended 13419 * header and data. 13420 */ 13421 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 13422 13423 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 13424 if (to.to_flags & TOF_SIGNATURE) { 13425 /* 13426 * Calculate MD5 signature and put it into the place 13427 * determined before. NOTE: since TCP options buffer doesn't 13428 * point into mbuf's data, calculate offset and use it. 13429 */ 13430 if (!TCPMD5_ENABLED() || TCPMD5_OUTPUT(m, th, 13431 (u_char *)(th + 1) + (to.to_signature - opt)) != 0) { 13432 /* 13433 * Do not send segment if the calculation of MD5 13434 * digest has failed. 13435 */ 13436 goto out; 13437 } 13438 } 13439 #endif 13440 13441 #ifdef INET6 13442 if (isipv6) { 13443 /* 13444 * ip6_plen is not need to be filled now, and will be filled 13445 * in ip6_output. 13446 */ 13447 if (tp->t_port) { 13448 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13449 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13450 udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13451 th->th_sum = htons(0); 13452 UDPSTAT_INC(udps_opackets); 13453 } else { 13454 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13455 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13456 th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 13457 optlen + len, IPPROTO_TCP, 0); 13458 } 13459 } 13460 #endif 13461 #if defined(INET6) && defined(INET) 13462 else 13463 #endif 13464 #ifdef INET 13465 { 13466 if (tp->t_port) { 13467 m->m_pkthdr.csum_flags = CSUM_UDP; 13468 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13469 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13470 ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13471 th->th_sum = htons(0); 13472 UDPSTAT_INC(udps_opackets); 13473 } else { 13474 csum_flags = m->m_pkthdr.csum_flags = CSUM_TCP; 13475 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13476 th->th_sum = in_pseudo(ip->ip_src.s_addr, 13477 ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13478 IPPROTO_TCP + len + optlen)); 13479 } 13480 /* IP version must be set here for ipv4/ipv6 checking later */ 13481 KASSERT(ip->ip_v == IPVERSION, 13482 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 13483 } 13484 #endif 13485 13486 /* 13487 * Enable TSO and specify the size of the segments. The TCP pseudo 13488 * header checksum is always provided. XXX: Fixme: This is currently 13489 * not the case for IPv6. 13490 */ 13491 if (tso) { 13492 KASSERT(len > maxseg, 13493 ("%s: len:%d <= tso_segsz:%d", __func__, len, maxseg)); 13494 m->m_pkthdr.csum_flags |= CSUM_TSO; 13495 csum_flags |= CSUM_TSO; 13496 m->m_pkthdr.tso_segsz = maxseg; 13497 } 13498 KASSERT(len + hdrlen == m_length(m, NULL), 13499 ("%s: mbuf chain different than expected: %d + %u != %u", 13500 __func__, len, hdrlen, m_length(m, NULL))); 13501 13502 #ifdef TCP_HHOOK 13503 /* Run HHOOK_TC_ESTABLISHED_OUT helper hooks. */ 13504 hhook_run_tcp_est_out(tp, th, &to, len, tso); 13505 #endif 13506 13507 /* Log to the black box */ 13508 if (tcp_bblogging_on(tp)) { 13509 union tcp_log_stackspecific log; 13510 13511 bbr_fill_in_logging_data(bbr, &log.u_bbr, cts); 13512 /* Record info on type of transmission */ 13513 log.u_bbr.flex1 = bbr->r_ctl.rc_hptsi_agg_delay; 13514 log.u_bbr.flex2 = (bbr->r_recovery_bw << 3); 13515 log.u_bbr.flex3 = maxseg; 13516 log.u_bbr.flex4 = delay_calc; 13517 log.u_bbr.flex5 = bbr->rc_past_init_win; 13518 log.u_bbr.flex5 <<= 1; 13519 log.u_bbr.flex5 |= bbr->rc_no_pacing; 13520 log.u_bbr.flex5 <<= 29; 13521 log.u_bbr.flex5 |= tp->t_maxseg; 13522 log.u_bbr.flex6 = bbr->r_ctl.rc_pace_max_segs; 13523 log.u_bbr.flex7 = (bbr->rc_bbr_state << 8) | bbr_state_val(bbr); 13524 /* lets poke in the low and the high here for debugging */ 13525 log.u_bbr.pkts_out = bbr->rc_tp->t_maxseg; 13526 if (rsm || sack_rxmit) { 13527 if (doing_tlp) 13528 log.u_bbr.flex8 = 2; 13529 else 13530 log.u_bbr.flex8 = 1; 13531 } else { 13532 log.u_bbr.flex8 = 0; 13533 } 13534 lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 13535 len, &log, false, NULL, NULL, 0, tv); 13536 } else { 13537 lgb = NULL; 13538 } 13539 /* 13540 * Fill in IP length and desired time to live and send to IP level. 13541 * There should be a better way to handle ttl and tos; we could keep 13542 * them in the template, but need a way to checksum without them. 13543 */ 13544 /* 13545 * m->m_pkthdr.len should have been set before cksum calcuration, 13546 * because in6_cksum() need it. 13547 */ 13548 #ifdef INET6 13549 if (isipv6) { 13550 /* 13551 * we separately set hoplimit for every segment, since the 13552 * user might want to change the value via setsockopt. Also, 13553 * desired default hop limit might be changed via Neighbor 13554 * Discovery. 13555 */ 13556 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 13557 13558 /* 13559 * Set the packet size here for the benefit of DTrace 13560 * probes. ip6_output() will set it properly; it's supposed 13561 * to include the option header lengths as well. 13562 */ 13563 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 13564 13565 if (V_path_mtu_discovery && maxseg > V_tcp_minmss) 13566 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13567 else 13568 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13569 13570 if (tp->t_state == TCPS_SYN_SENT) 13571 TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 13572 13573 TCP_PROBE5(send, NULL, tp, ip6, tp, th); 13574 /* TODO: IPv6 IP6TOS_ECT bit on */ 13575 error = ip6_output(m, inp->in6p_outputopts, 13576 &inp->inp_route6, 13577 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 13578 NULL, NULL, inp); 13579 13580 if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 13581 mtu = inp->inp_route6.ro_nh->nh_mtu; 13582 } 13583 #endif /* INET6 */ 13584 #if defined(INET) && defined(INET6) 13585 else 13586 #endif 13587 #ifdef INET 13588 { 13589 ip->ip_len = htons(m->m_pkthdr.len); 13590 #ifdef INET6 13591 if (isipv6) 13592 ip->ip_ttl = in6_selecthlim(inp, NULL); 13593 #endif /* INET6 */ 13594 /* 13595 * If we do path MTU discovery, then we set DF on every 13596 * packet. This might not be the best thing to do according 13597 * to RFC3390 Section 2. However the tcp hostcache migitates 13598 * the problem so it affects only the first tcp connection 13599 * with a host. 13600 * 13601 * NB: Don't set DF on small MTU/MSS to have a safe 13602 * fallback. 13603 */ 13604 if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 13605 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13606 if (tp->t_port == 0 || len < V_tcp_minmss) { 13607 ip->ip_off |= htons(IP_DF); 13608 } 13609 } else { 13610 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13611 } 13612 13613 if (tp->t_state == TCPS_SYN_SENT) 13614 TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 13615 13616 TCP_PROBE5(send, NULL, tp, ip, tp, th); 13617 13618 error = ip_output(m, inp->inp_options, &inp->inp_route, 13619 ((rsm || sack_rxmit) ? IP_NO_SND_TAG_RL : 0), 0, 13620 inp); 13621 if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 13622 mtu = inp->inp_route.ro_nh->nh_mtu; 13623 } 13624 #endif /* INET */ 13625 if (lgb) { 13626 lgb->tlb_errno = error; 13627 lgb = NULL; 13628 } 13629 13630 out: 13631 /* 13632 * In transmit state, time the transmission and arrange for the 13633 * retransmit. In persist state, just set snd_max. 13634 */ 13635 if (error == 0) { 13636 tcp_account_for_send(tp, len, (rsm != NULL), doing_tlp, hw_tls); 13637 if (TCPS_HAVEESTABLISHED(tp->t_state) && 13638 (tp->t_flags & TF_SACK_PERMIT) && 13639 tp->rcv_numsacks > 0) 13640 tcp_clean_dsack_blocks(tp); 13641 /* We sent an ack clear the bbr_segs_rcvd count */ 13642 bbr->output_error_seen = 0; 13643 bbr->oerror_cnt = 0; 13644 bbr->bbr_segs_rcvd = 0; 13645 if (len == 0) 13646 counter_u64_add(bbr_out_size[TCP_MSS_ACCT_SNDACK], 1); 13647 /* Do accounting for new sends */ 13648 if ((len > 0) && (rsm == NULL)) { 13649 int idx; 13650 if (tp->snd_una == tp->snd_max) { 13651 /* 13652 * Special case to match google, when 13653 * nothing is in flight the delivered 13654 * time does get updated to the current 13655 * time (see tcp_rate_bsd.c). 13656 */ 13657 bbr->r_ctl.rc_del_time = cts; 13658 } 13659 if (len >= maxseg) { 13660 idx = (len / maxseg) + 3; 13661 if (idx >= TCP_MSS_ACCT_ATIMER) 13662 counter_u64_add(bbr_out_size[(TCP_MSS_ACCT_ATIMER - 1)], 1); 13663 else 13664 counter_u64_add(bbr_out_size[idx], 1); 13665 } else { 13666 /* smaller than a MSS */ 13667 idx = len / (bbr_hptsi_bytes_min - bbr->rc_last_options); 13668 if (idx >= TCP_MSS_SMALL_MAX_SIZE_DIV) 13669 idx = (TCP_MSS_SMALL_MAX_SIZE_DIV - 1); 13670 counter_u64_add(bbr_out_size[(idx + TCP_MSS_SMALL_SIZE_OFF)], 1); 13671 } 13672 } 13673 } 13674 abandon = 0; 13675 /* 13676 * We must do the send accounting before we log the output, 13677 * otherwise the state of the rsm could change and we account to the 13678 * wrong bucket. 13679 */ 13680 if (len > 0) { 13681 bbr_do_send_accounting(tp, bbr, rsm, len, error); 13682 if (error == 0) { 13683 if (tp->snd_una == tp->snd_max) 13684 bbr->r_ctl.rc_tlp_rxt_last_time = cts; 13685 } 13686 } 13687 bbr_log_output(bbr, tp, &to, len, bbr_seq, (uint8_t) flags, error, 13688 cts, mb, &abandon, rsm, 0, sb); 13689 if (abandon) { 13690 /* 13691 * If bbr_log_output destroys the TCB or sees a TH_RST being 13692 * sent we should hit this condition. 13693 */ 13694 return (0); 13695 } 13696 if (bbr->rc_in_persist == 0) { 13697 /* 13698 * Advance snd_nxt over sequence space of this segment. 13699 */ 13700 if (error) 13701 /* We don't log or do anything with errors */ 13702 goto skip_upd; 13703 13704 if (tp->snd_una == tp->snd_max && 13705 (len || (flags & (TH_SYN | TH_FIN)))) { 13706 /* 13707 * Update the time we just added data since none was 13708 * outstanding. 13709 */ 13710 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13711 bbr->rc_tp->t_acktime = ticks; 13712 } 13713 if (flags & (TH_SYN | TH_FIN) && (rsm == NULL)) { 13714 if (flags & TH_SYN) { 13715 /* 13716 * Smack the snd_max to iss + 1 13717 * if its a FO we will add len below. 13718 */ 13719 tp->snd_max = tp->iss + 1; 13720 } 13721 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13722 tp->snd_max++; 13723 tp->t_flags |= TF_SENTFIN; 13724 } 13725 } 13726 if (sack_rxmit == 0) 13727 tp->snd_max += len; 13728 skip_upd: 13729 if ((error == 0) && len) 13730 tot_len += len; 13731 } else { 13732 /* Persists case */ 13733 int32_t xlen = len; 13734 13735 if (error) 13736 goto nomore; 13737 13738 if (flags & TH_SYN) 13739 ++xlen; 13740 if ((flags & TH_FIN) && ((tp->t_flags & TF_SENTFIN) == 0)) { 13741 ++xlen; 13742 tp->t_flags |= TF_SENTFIN; 13743 } 13744 if (xlen && (tp->snd_una == tp->snd_max)) { 13745 /* 13746 * Update the time we just added data since none was 13747 * outstanding. 13748 */ 13749 bbr_log_progress_event(bbr, tp, ticks, PROGRESS_START, __LINE__); 13750 bbr->rc_tp->t_acktime = ticks; 13751 } 13752 if (sack_rxmit == 0) 13753 tp->snd_max += xlen; 13754 tot_len += (len + optlen + ipoptlen); 13755 } 13756 nomore: 13757 if (error) { 13758 /* 13759 * Failures do not advance the seq counter above. For the 13760 * case of ENOBUFS we will fall out and become ack-clocked. 13761 * capping the cwnd at the current flight. 13762 * Everything else will just have to retransmit with the timer 13763 * (no pacer). 13764 */ 13765 SOCK_SENDBUF_UNLOCK_ASSERT(so); 13766 BBR_STAT_INC(bbr_saw_oerr); 13767 /* Clear all delay/early tracks */ 13768 bbr->r_ctl.rc_hptsi_agg_delay = 0; 13769 bbr->r_ctl.rc_agg_early = 0; 13770 bbr->r_agg_early_set = 0; 13771 bbr->output_error_seen = 1; 13772 if (bbr->oerror_cnt < 0xf) 13773 bbr->oerror_cnt++; 13774 if (bbr_max_net_error_cnt && (bbr->oerror_cnt >= bbr_max_net_error_cnt)) { 13775 /* drop the session */ 13776 return (-ENETDOWN); 13777 } 13778 switch (error) { 13779 case ENOBUFS: 13780 /* 13781 * Make this guy have to get ack's to send 13782 * more but lets make sure we don't 13783 * slam him below a T-O (1MSS). 13784 */ 13785 if (bbr->rc_bbr_state != BBR_STATE_PROBE_RTT) { 13786 tp->snd_cwnd = ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 13787 bbr->r_ctl.rc_lost_bytes)) - maxseg; 13788 if (tp->snd_cwnd < maxseg) 13789 tp->snd_cwnd = maxseg; 13790 } 13791 slot = (bbr_error_base_paceout + 1) << bbr->oerror_cnt; 13792 BBR_STAT_INC(bbr_saw_enobuf); 13793 if (bbr->bbr_hdrw_pacing) 13794 counter_u64_add(bbr_hdwr_pacing_enobuf, 1); 13795 else 13796 counter_u64_add(bbr_nohdwr_pacing_enobuf, 1); 13797 /* 13798 * Here even in the enobuf's case we want to do our 13799 * state update. The reason being we may have been 13800 * called by the input function. If so we have had 13801 * things change. 13802 */ 13803 error = 0; 13804 goto enobufs; 13805 case EMSGSIZE: 13806 /* 13807 * For some reason the interface we used initially 13808 * to send segments changed to another or lowered 13809 * its MTU. If TSO was active we either got an 13810 * interface without TSO capabilits or TSO was 13811 * turned off. If we obtained mtu from ip_output() 13812 * then update it and try again. 13813 */ 13814 /* Turn on tracing (or try to) */ 13815 { 13816 int old_maxseg; 13817 13818 old_maxseg = tp->t_maxseg; 13819 BBR_STAT_INC(bbr_saw_emsgsiz); 13820 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, csum_flags, tso, cts); 13821 if (mtu != 0) 13822 tcp_mss_update(tp, -1, mtu, NULL, NULL); 13823 if (old_maxseg <= tp->t_maxseg) { 13824 /* Huh it did not shrink? */ 13825 tp->t_maxseg = old_maxseg - 40; 13826 if (tp->t_maxseg < V_tcp_mssdflt) { 13827 /* 13828 * The MSS is so small we should not 13829 * process incoming SACK's since we are 13830 * subject to attack in such a case. 13831 */ 13832 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 13833 } else { 13834 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 13835 } 13836 bbr_log_msgsize_fail(bbr, tp, len, maxseg, mtu, 0, tso, cts); 13837 } 13838 /* 13839 * Nuke all other things that can interfere 13840 * with slot 13841 */ 13842 if ((tot_len + len) && (len >= tp->t_maxseg)) { 13843 slot = bbr_get_pacing_delay(bbr, 13844 bbr->r_ctl.rc_bbr_hptsi_gain, 13845 (tot_len + len), cts, 0); 13846 if (slot < bbr_error_base_paceout) 13847 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13848 } else 13849 slot = (bbr_error_base_paceout + 2) << bbr->oerror_cnt; 13850 bbr->rc_output_starts_timer = 1; 13851 bbr_start_hpts_timer(bbr, tp, cts, 10, slot, 13852 tot_len); 13853 return (error); 13854 } 13855 case EPERM: 13856 case EACCES: 13857 tp->t_softerror = error; 13858 /* FALLTHROUGH */ 13859 case EHOSTDOWN: 13860 case EHOSTUNREACH: 13861 case ENETDOWN: 13862 case ENETUNREACH: 13863 if (TCPS_HAVERCVDSYN(tp->t_state)) { 13864 tp->t_softerror = error; 13865 error = 0; 13866 } 13867 /* FALLTHROUGH */ 13868 default: 13869 slot = (bbr_error_base_paceout + 3) << bbr->oerror_cnt; 13870 bbr->rc_output_starts_timer = 1; 13871 bbr_start_hpts_timer(bbr, tp, cts, 11, slot, 0); 13872 return (error); 13873 } 13874 #ifdef STATS 13875 } else if (((tp->t_flags & TF_GPUTINPROG) == 0) && 13876 len && 13877 (rsm == NULL) && 13878 (bbr->rc_in_persist == 0)) { 13879 tp->gput_seq = bbr_seq; 13880 tp->gput_ack = bbr_seq + 13881 min(sbavail(&so->so_snd) - sb_offset, sendwin); 13882 tp->gput_ts = cts; 13883 tp->t_flags |= TF_GPUTINPROG; 13884 #endif 13885 } 13886 KMOD_TCPSTAT_INC(tcps_sndtotal); 13887 if ((bbr->bbr_hdw_pace_ena) && 13888 (bbr->bbr_attempt_hdwr_pace == 0) && 13889 (bbr->rc_past_init_win) && 13890 (bbr->rc_bbr_state != BBR_STATE_STARTUP) && 13891 (get_filter_value(&bbr->r_ctl.rc_delrate)) && 13892 (inp->inp_route.ro_nh && 13893 inp->inp_route.ro_nh->nh_ifp)) { 13894 /* 13895 * We are past the initial window and 13896 * have at least one measurement so we 13897 * could use hardware pacing if its available. 13898 * We have an interface and we have not attempted 13899 * to setup hardware pacing, lets try to now. 13900 */ 13901 uint64_t rate_wanted; 13902 int err = 0; 13903 13904 rate_wanted = bbr_get_hardware_rate(bbr); 13905 bbr->bbr_attempt_hdwr_pace = 1; 13906 bbr->r_ctl.crte = tcp_set_pacing_rate(bbr->rc_tp, 13907 inp->inp_route.ro_nh->nh_ifp, 13908 rate_wanted, 13909 (RS_PACING_GEQ|RS_PACING_SUB_OK), 13910 &err, NULL); 13911 if (bbr->r_ctl.crte) { 13912 bbr_type_log_hdwr_pacing(bbr, 13913 bbr->r_ctl.crte->ptbl->rs_ifp, 13914 rate_wanted, 13915 bbr->r_ctl.crte->rate, 13916 __LINE__, cts, err); 13917 BBR_STAT_INC(bbr_hdwr_rl_add_ok); 13918 counter_u64_add(bbr_flows_nohdwr_pacing, -1); 13919 counter_u64_add(bbr_flows_whdwr_pacing, 1); 13920 bbr->bbr_hdrw_pacing = 1; 13921 /* Now what is our gain status? */ 13922 if (bbr->r_ctl.crte->rate < rate_wanted) { 13923 /* We have a problem */ 13924 bbr_setup_less_of_rate(bbr, cts, 13925 bbr->r_ctl.crte->rate, rate_wanted); 13926 } else { 13927 /* We are good */ 13928 bbr->gain_is_limited = 0; 13929 bbr->skip_gain = 0; 13930 } 13931 tcp_bbr_tso_size_check(bbr, cts); 13932 } else { 13933 bbr_type_log_hdwr_pacing(bbr, 13934 inp->inp_route.ro_nh->nh_ifp, 13935 rate_wanted, 13936 0, 13937 __LINE__, cts, err); 13938 BBR_STAT_INC(bbr_hdwr_rl_add_fail); 13939 } 13940 } 13941 if (bbr->bbr_hdrw_pacing) { 13942 /* 13943 * Worry about cases where the route 13944 * changes or something happened that we 13945 * lost our hardware pacing possibly during 13946 * the last ip_output call. 13947 */ 13948 if (inp->inp_snd_tag == NULL) { 13949 /* A change during ip output disabled hw pacing? */ 13950 bbr->bbr_hdrw_pacing = 0; 13951 } else if ((inp->inp_route.ro_nh == NULL) || 13952 (inp->inp_route.ro_nh->nh_ifp != inp->inp_snd_tag->ifp)) { 13953 /* 13954 * We had an interface or route change, 13955 * detach from the current hdwr pacing 13956 * and setup to re-attempt next go 13957 * round. 13958 */ 13959 bbr->bbr_hdrw_pacing = 0; 13960 bbr->bbr_attempt_hdwr_pace = 0; 13961 tcp_rel_pacing_rate(bbr->r_ctl.crte, bbr->rc_tp); 13962 tcp_bbr_tso_size_check(bbr, cts); 13963 } 13964 } 13965 /* 13966 * Data sent (as far as we can tell). If this advertises a larger 13967 * window than any other segment, then remember the size of the 13968 * advertised window. Any pending ACK has now been sent. 13969 */ 13970 if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 13971 tp->rcv_adv = tp->rcv_nxt + recwin; 13972 13973 tp->last_ack_sent = tp->rcv_nxt; 13974 if ((error == 0) && 13975 (bbr->r_ctl.rc_pace_max_segs > tp->t_maxseg) && 13976 (doing_tlp == 0) && 13977 (tso == 0) && 13978 (len > 0) && 13979 ((flags & TH_RST) == 0) && 13980 ((flags & TH_SYN) == 0) && 13981 (IN_RECOVERY(tp->t_flags) == 0) && 13982 (bbr->rc_in_persist == 0) && 13983 (tot_len < bbr->r_ctl.rc_pace_max_segs)) { 13984 /* 13985 * For non-tso we need to goto again until we have sent out 13986 * enough data to match what we are hptsi out every hptsi 13987 * interval. 13988 */ 13989 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 13990 /* Make sure snd_nxt is drug up */ 13991 tp->snd_nxt = tp->snd_max; 13992 } 13993 if (rsm != NULL) { 13994 rsm = NULL; 13995 goto skip_again; 13996 } 13997 rsm = NULL; 13998 sack_rxmit = 0; 13999 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 14000 goto again; 14001 } 14002 skip_again: 14003 if ((error == 0) && (flags & TH_FIN)) 14004 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_FIN); 14005 if ((error == 0) && (flags & TH_RST)) 14006 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 14007 if (((flags & (TH_RST | TH_SYN | TH_FIN)) == 0) && tot_len) { 14008 /* 14009 * Calculate/Re-Calculate the hptsi slot in usecs based on 14010 * what we have sent so far 14011 */ 14012 slot = bbr_get_pacing_delay(bbr, bbr->r_ctl.rc_bbr_hptsi_gain, tot_len, cts, 0); 14013 if (bbr->rc_no_pacing) 14014 slot = 0; 14015 } 14016 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 14017 enobufs: 14018 if (bbr->rc_use_google == 0) 14019 bbr_check_bbr_for_state(bbr, cts, __LINE__, 0); 14020 bbr_cwnd_limiting(tp, bbr, ctf_flight_size(tp, (bbr->r_ctl.rc_sacked + 14021 bbr->r_ctl.rc_lost_bytes))); 14022 bbr->rc_output_starts_timer = 1; 14023 if (bbr->bbr_use_rack_cheat && 14024 (more_to_rxt || 14025 ((bbr->r_ctl.rc_resend = bbr_check_recovery_mode(tp, bbr, cts)) != NULL))) { 14026 /* Rack cheats and shotguns out all rxt's 1ms apart */ 14027 if (slot > 1000) 14028 slot = 1000; 14029 } 14030 if (bbr->bbr_hdrw_pacing && (bbr->hw_pacing_set == 0)) { 14031 /* 14032 * We don't change the tso size until some number of sends 14033 * to give the hardware commands time to get down 14034 * to the interface. 14035 */ 14036 bbr->r_ctl.bbr_hdwr_cnt_noset_snt++; 14037 if (bbr->r_ctl.bbr_hdwr_cnt_noset_snt >= bbr_hdwr_pacing_delay_cnt) { 14038 bbr->hw_pacing_set = 1; 14039 tcp_bbr_tso_size_check(bbr, cts); 14040 } 14041 } 14042 bbr_start_hpts_timer(bbr, tp, cts, 12, slot, tot_len); 14043 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 14044 /* Make sure snd_nxt is drug up */ 14045 tp->snd_nxt = tp->snd_max; 14046 } 14047 return (error); 14048 14049 } 14050 14051 /* 14052 * See bbr_output_wtime() for return values. 14053 */ 14054 static int 14055 bbr_output(struct tcpcb *tp) 14056 { 14057 int32_t ret; 14058 struct timeval tv; 14059 14060 NET_EPOCH_ASSERT(); 14061 14062 INP_WLOCK_ASSERT(tptoinpcb(tp)); 14063 (void)tcp_get_usecs(&tv); 14064 ret = bbr_output_wtime(tp, &tv); 14065 return (ret); 14066 } 14067 14068 static void 14069 bbr_mtu_chg(struct tcpcb *tp) 14070 { 14071 struct tcp_bbr *bbr; 14072 struct bbr_sendmap *rsm, *frsm = NULL; 14073 uint32_t maxseg; 14074 14075 /* 14076 * The MTU has changed. a) Clear the sack filter. b) Mark everything 14077 * over the current size as SACK_PASS so a retransmit will occur. 14078 */ 14079 14080 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14081 maxseg = tp->t_maxseg - bbr->rc_last_options; 14082 sack_filter_clear(&bbr->r_ctl.bbr_sf, tp->snd_una); 14083 TAILQ_FOREACH(rsm, &bbr->r_ctl.rc_map, r_next) { 14084 /* Don't mess with ones acked (by sack?) */ 14085 if (rsm->r_flags & BBR_ACKED) 14086 continue; 14087 if ((rsm->r_end - rsm->r_start) > maxseg) { 14088 /* 14089 * We mark sack-passed on all the previous large 14090 * sends we did. This will force them to retransmit. 14091 */ 14092 rsm->r_flags |= BBR_SACK_PASSED; 14093 if (((rsm->r_flags & BBR_MARKED_LOST) == 0) && 14094 bbr_is_lost(bbr, rsm, bbr->r_ctl.rc_rcvtime)) { 14095 bbr->r_ctl.rc_lost_bytes += rsm->r_end - rsm->r_start; 14096 bbr->r_ctl.rc_lost += rsm->r_end - rsm->r_start; 14097 rsm->r_flags |= BBR_MARKED_LOST; 14098 } 14099 if (frsm == NULL) 14100 frsm = rsm; 14101 } 14102 } 14103 if (frsm) { 14104 bbr->r_ctl.rc_resend = frsm; 14105 } 14106 } 14107 14108 static int 14109 bbr_pru_options(struct tcpcb *tp, int flags) 14110 { 14111 if (flags & PRUS_OOB) 14112 return (EOPNOTSUPP); 14113 return (0); 14114 } 14115 14116 static void 14117 bbr_switch_failed(struct tcpcb *tp) 14118 { 14119 /* 14120 * If a switch fails we only need to 14121 * make sure mbuf_queuing is still in place. 14122 * We also need to make sure we are still in 14123 * ticks granularity (though we should probably 14124 * change bbr to go to USECs). 14125 * 14126 * For timers we need to see if we are still in the 14127 * pacer (if our flags are up) if so we are good, if 14128 * not we need to get back into the pacer. 14129 */ 14130 struct timeval tv; 14131 uint32_t cts; 14132 uint32_t toval; 14133 struct tcp_bbr *bbr; 14134 struct hpts_diag diag; 14135 14136 tp->t_flags2 |= TF2_CANNOT_DO_ECN; 14137 tp->t_flags2 |= TF2_SUPPORTS_MBUFQ; 14138 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 14139 if (tp->t_in_hpts > IHPTS_NONE) { 14140 return; 14141 } 14142 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14143 cts = tcp_get_usecs(&tv); 14144 if (bbr->r_ctl.rc_hpts_flags & PACE_PKT_OUTPUT) { 14145 if (TSTMP_GT(bbr->rc_pacer_started, cts)) { 14146 toval = bbr->rc_pacer_started - cts; 14147 } else { 14148 /* one slot please */ 14149 toval = HPTS_TICKS_PER_SLOT; 14150 } 14151 } else if (bbr->r_ctl.rc_hpts_flags & PACE_TMR_MASK) { 14152 if (TSTMP_GT(bbr->r_ctl.rc_timer_exp, cts)) { 14153 toval = bbr->r_ctl.rc_timer_exp - cts; 14154 } else { 14155 /* one slot please */ 14156 toval = HPTS_TICKS_PER_SLOT; 14157 } 14158 } else 14159 toval = HPTS_TICKS_PER_SLOT; 14160 (void)tcp_hpts_insert_diag(tp, HPTS_USEC_TO_SLOTS(toval), 14161 __LINE__, &diag); 14162 bbr_log_hpts_diag(bbr, cts, &diag); 14163 } 14164 14165 struct tcp_function_block __tcp_bbr = { 14166 .tfb_tcp_block_name = __XSTRING(STACKNAME), 14167 .tfb_tcp_output = bbr_output, 14168 .tfb_do_queued_segments = ctf_do_queued_segments, 14169 .tfb_do_segment_nounlock = bbr_do_segment_nounlock, 14170 .tfb_tcp_do_segment = bbr_do_segment, 14171 .tfb_tcp_ctloutput = bbr_ctloutput, 14172 .tfb_tcp_fb_init = bbr_init, 14173 .tfb_tcp_fb_fini = bbr_fini, 14174 .tfb_tcp_timer_stop_all = bbr_stopall, 14175 .tfb_tcp_rexmit_tmr = bbr_remxt_tmr, 14176 .tfb_tcp_handoff_ok = bbr_handoff_ok, 14177 .tfb_tcp_mtu_chg = bbr_mtu_chg, 14178 .tfb_pru_options = bbr_pru_options, 14179 .tfb_switch_failed = bbr_switch_failed, 14180 .tfb_flags = TCP_FUNC_OUTPUT_CANDROP | TCP_FUNC_DEFAULT_OK, 14181 }; 14182 14183 /* 14184 * bbr_ctloutput() must drop the inpcb lock before performing copyin on 14185 * socket option arguments. When it re-acquires the lock after the copy, it 14186 * has to revalidate that the connection is still valid for the socket 14187 * option. 14188 */ 14189 static int 14190 bbr_set_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14191 { 14192 struct epoch_tracker et; 14193 struct inpcb *inp = tptoinpcb(tp); 14194 struct tcp_bbr *bbr; 14195 int32_t error = 0, optval; 14196 14197 switch (sopt->sopt_level) { 14198 case IPPROTO_IPV6: 14199 case IPPROTO_IP: 14200 return (tcp_default_ctloutput(tp, sopt)); 14201 } 14202 14203 switch (sopt->sopt_name) { 14204 case TCP_RACK_PACE_MAX_SEG: 14205 case TCP_RACK_MIN_TO: 14206 case TCP_RACK_REORD_THRESH: 14207 case TCP_RACK_REORD_FADE: 14208 case TCP_RACK_TLP_THRESH: 14209 case TCP_RACK_PKT_DELAY: 14210 case TCP_BBR_ALGORITHM: 14211 case TCP_BBR_TSLIMITS: 14212 case TCP_BBR_IWINTSO: 14213 case TCP_BBR_STARTUP_PG: 14214 case TCP_BBR_DRAIN_PG: 14215 case TCP_BBR_PROBE_RTT_INT: 14216 case TCP_BBR_PROBE_RTT_GAIN: 14217 case TCP_BBR_PROBE_RTT_LEN: 14218 case TCP_BBR_STARTUP_LOSS_EXIT: 14219 case TCP_BBR_USEDEL_RATE: 14220 case TCP_BBR_MIN_RTO: 14221 case TCP_BBR_MAX_RTO: 14222 case TCP_BBR_PACE_PER_SEC: 14223 case TCP_DELACK: 14224 case TCP_BBR_PACE_DEL_TAR: 14225 case TCP_BBR_SEND_IWND_IN_TSO: 14226 case TCP_BBR_EXTRA_STATE: 14227 case TCP_BBR_UTTER_MAX_TSO: 14228 case TCP_BBR_MIN_TOPACEOUT: 14229 case TCP_BBR_FLOOR_MIN_TSO: 14230 case TCP_BBR_TSTMP_RAISES: 14231 case TCP_BBR_POLICER_DETECT: 14232 case TCP_BBR_USE_RACK_CHEAT: 14233 case TCP_DATA_AFTER_CLOSE: 14234 case TCP_BBR_HDWR_PACE: 14235 case TCP_BBR_PACE_SEG_MAX: 14236 case TCP_BBR_PACE_SEG_MIN: 14237 case TCP_BBR_PACE_CROSS: 14238 case TCP_BBR_PACE_OH: 14239 case TCP_BBR_TMR_PACE_OH: 14240 case TCP_BBR_RACK_RTT_USE: 14241 case TCP_BBR_RETRAN_WTSO: 14242 break; 14243 default: 14244 return (tcp_default_ctloutput(tp, sopt)); 14245 break; 14246 } 14247 INP_WUNLOCK(inp); 14248 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); 14249 if (error) 14250 return (error); 14251 INP_WLOCK(inp); 14252 if (inp->inp_flags & INP_DROPPED) { 14253 INP_WUNLOCK(inp); 14254 return (ECONNRESET); 14255 } 14256 if (tp->t_fb != &__tcp_bbr) { 14257 INP_WUNLOCK(inp); 14258 return (ENOPROTOOPT); 14259 } 14260 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14261 switch (sopt->sopt_name) { 14262 case TCP_BBR_PACE_PER_SEC: 14263 BBR_OPTS_INC(tcp_bbr_pace_per_sec); 14264 bbr->r_ctl.bbr_hptsi_per_second = optval; 14265 break; 14266 case TCP_BBR_PACE_DEL_TAR: 14267 BBR_OPTS_INC(tcp_bbr_pace_del_tar); 14268 bbr->r_ctl.bbr_hptsi_segments_delay_tar = optval; 14269 break; 14270 case TCP_BBR_PACE_SEG_MAX: 14271 BBR_OPTS_INC(tcp_bbr_pace_seg_max); 14272 bbr->r_ctl.bbr_hptsi_segments_max = optval; 14273 break; 14274 case TCP_BBR_PACE_SEG_MIN: 14275 BBR_OPTS_INC(tcp_bbr_pace_seg_min); 14276 bbr->r_ctl.bbr_hptsi_bytes_min = optval; 14277 break; 14278 case TCP_BBR_PACE_CROSS: 14279 BBR_OPTS_INC(tcp_bbr_pace_cross); 14280 bbr->r_ctl.bbr_cross_over = optval; 14281 break; 14282 case TCP_BBR_ALGORITHM: 14283 BBR_OPTS_INC(tcp_bbr_algorithm); 14284 if (optval && (bbr->rc_use_google == 0)) { 14285 /* Turn on the google mode */ 14286 bbr_google_mode_on(bbr); 14287 if ((optval > 3) && (optval < 500)) { 14288 /* 14289 * Must be at least greater than .3% 14290 * and must be less than 50.0%. 14291 */ 14292 bbr->r_ctl.bbr_google_discount = optval; 14293 } 14294 } else if ((optval == 0) && (bbr->rc_use_google == 1)) { 14295 /* Turn off the google mode */ 14296 bbr_google_mode_off(bbr); 14297 } 14298 break; 14299 case TCP_BBR_TSLIMITS: 14300 BBR_OPTS_INC(tcp_bbr_tslimits); 14301 if (optval == 1) 14302 bbr->rc_use_ts_limit = 1; 14303 else if (optval == 0) 14304 bbr->rc_use_ts_limit = 0; 14305 else 14306 error = EINVAL; 14307 break; 14308 14309 case TCP_BBR_IWINTSO: 14310 BBR_OPTS_INC(tcp_bbr_iwintso); 14311 if ((optval >= 0) && (optval < 128)) { 14312 uint32_t twin; 14313 14314 bbr->rc_init_win = optval; 14315 twin = bbr_initial_cwnd(bbr, tp); 14316 if ((bbr->rc_past_init_win == 0) && (twin > tp->snd_cwnd)) 14317 tp->snd_cwnd = twin; 14318 else 14319 error = EBUSY; 14320 } else 14321 error = EINVAL; 14322 break; 14323 case TCP_BBR_STARTUP_PG: 14324 BBR_OPTS_INC(tcp_bbr_startup_pg); 14325 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) { 14326 bbr->r_ctl.rc_startup_pg = optval; 14327 if (bbr->rc_bbr_state == BBR_STATE_STARTUP) { 14328 bbr->r_ctl.rc_bbr_hptsi_gain = optval; 14329 } 14330 } else 14331 error = EINVAL; 14332 break; 14333 case TCP_BBR_DRAIN_PG: 14334 BBR_OPTS_INC(tcp_bbr_drain_pg); 14335 if ((optval > 0) && (optval < BBR_MAX_GAIN_VALUE)) 14336 bbr->r_ctl.rc_drain_pg = optval; 14337 else 14338 error = EINVAL; 14339 break; 14340 case TCP_BBR_PROBE_RTT_LEN: 14341 BBR_OPTS_INC(tcp_bbr_probertt_len); 14342 if (optval <= 1) 14343 reset_time_small(&bbr->r_ctl.rc_rttprop, (optval * USECS_IN_SECOND)); 14344 else 14345 error = EINVAL; 14346 break; 14347 case TCP_BBR_PROBE_RTT_GAIN: 14348 BBR_OPTS_INC(tcp_bbr_probertt_gain); 14349 if (optval <= BBR_UNIT) 14350 bbr->r_ctl.bbr_rttprobe_gain_val = optval; 14351 else 14352 error = EINVAL; 14353 break; 14354 case TCP_BBR_PROBE_RTT_INT: 14355 BBR_OPTS_INC(tcp_bbr_probe_rtt_int); 14356 if (optval > 1000) 14357 bbr->r_ctl.rc_probertt_int = optval; 14358 else 14359 error = EINVAL; 14360 break; 14361 case TCP_BBR_MIN_TOPACEOUT: 14362 BBR_OPTS_INC(tcp_bbr_topaceout); 14363 if (optval == 0) { 14364 bbr->no_pacing_until = 0; 14365 bbr->rc_no_pacing = 0; 14366 } else if (optval <= 0x00ff) { 14367 bbr->no_pacing_until = optval; 14368 if ((bbr->r_ctl.rc_pkt_epoch < bbr->no_pacing_until) && 14369 (bbr->rc_bbr_state == BBR_STATE_STARTUP)){ 14370 /* Turn on no pacing */ 14371 bbr->rc_no_pacing = 1; 14372 } 14373 } else 14374 error = EINVAL; 14375 break; 14376 case TCP_BBR_STARTUP_LOSS_EXIT: 14377 BBR_OPTS_INC(tcp_bbr_startup_loss_exit); 14378 bbr->rc_loss_exit = optval; 14379 break; 14380 case TCP_BBR_USEDEL_RATE: 14381 error = EINVAL; 14382 break; 14383 case TCP_BBR_MIN_RTO: 14384 BBR_OPTS_INC(tcp_bbr_min_rto); 14385 bbr->r_ctl.rc_min_rto_ms = optval; 14386 break; 14387 case TCP_BBR_MAX_RTO: 14388 BBR_OPTS_INC(tcp_bbr_max_rto); 14389 bbr->rc_max_rto_sec = optval; 14390 break; 14391 case TCP_RACK_MIN_TO: 14392 /* Minimum time between rack t-o's in ms */ 14393 BBR_OPTS_INC(tcp_rack_min_to); 14394 bbr->r_ctl.rc_min_to = optval; 14395 break; 14396 case TCP_RACK_REORD_THRESH: 14397 /* RACK reorder threshold (shift amount) */ 14398 BBR_OPTS_INC(tcp_rack_reord_thresh); 14399 if ((optval > 0) && (optval < 31)) 14400 bbr->r_ctl.rc_reorder_shift = optval; 14401 else 14402 error = EINVAL; 14403 break; 14404 case TCP_RACK_REORD_FADE: 14405 /* Does reordering fade after ms time */ 14406 BBR_OPTS_INC(tcp_rack_reord_fade); 14407 bbr->r_ctl.rc_reorder_fade = optval; 14408 break; 14409 case TCP_RACK_TLP_THRESH: 14410 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14411 BBR_OPTS_INC(tcp_rack_tlp_thresh); 14412 if (optval) 14413 bbr->rc_tlp_threshold = optval; 14414 else 14415 error = EINVAL; 14416 break; 14417 case TCP_BBR_USE_RACK_CHEAT: 14418 BBR_OPTS_INC(tcp_use_rackcheat); 14419 if (bbr->rc_use_google) { 14420 error = EINVAL; 14421 break; 14422 } 14423 BBR_OPTS_INC(tcp_rack_cheat); 14424 if (optval) 14425 bbr->bbr_use_rack_cheat = 1; 14426 else 14427 bbr->bbr_use_rack_cheat = 0; 14428 break; 14429 case TCP_BBR_FLOOR_MIN_TSO: 14430 BBR_OPTS_INC(tcp_utter_max_tso); 14431 if ((optval >= 0) && (optval < 40)) 14432 bbr->r_ctl.bbr_hptsi_segments_floor = optval; 14433 else 14434 error = EINVAL; 14435 break; 14436 case TCP_BBR_UTTER_MAX_TSO: 14437 BBR_OPTS_INC(tcp_utter_max_tso); 14438 if ((optval >= 0) && (optval < 0xffff)) 14439 bbr->r_ctl.bbr_utter_max = optval; 14440 else 14441 error = EINVAL; 14442 break; 14443 14444 case TCP_BBR_EXTRA_STATE: 14445 BBR_OPTS_INC(tcp_extra_state); 14446 if (optval) 14447 bbr->rc_use_idle_restart = 1; 14448 else 14449 bbr->rc_use_idle_restart = 0; 14450 break; 14451 case TCP_BBR_SEND_IWND_IN_TSO: 14452 BBR_OPTS_INC(tcp_iwnd_tso); 14453 if (optval) { 14454 bbr->bbr_init_win_cheat = 1; 14455 if (bbr->rc_past_init_win == 0) { 14456 uint32_t cts; 14457 cts = tcp_get_usecs(&bbr->rc_tv); 14458 tcp_bbr_tso_size_check(bbr, cts); 14459 } 14460 } else 14461 bbr->bbr_init_win_cheat = 0; 14462 break; 14463 case TCP_BBR_HDWR_PACE: 14464 BBR_OPTS_INC(tcp_hdwr_pacing); 14465 if (optval){ 14466 bbr->bbr_hdw_pace_ena = 1; 14467 bbr->bbr_attempt_hdwr_pace = 0; 14468 } else { 14469 bbr->bbr_hdw_pace_ena = 0; 14470 #ifdef RATELIMIT 14471 if (bbr->r_ctl.crte != NULL) { 14472 tcp_rel_pacing_rate(bbr->r_ctl.crte, tp); 14473 bbr->r_ctl.crte = NULL; 14474 } 14475 #endif 14476 } 14477 break; 14478 14479 case TCP_DELACK: 14480 BBR_OPTS_INC(tcp_delack); 14481 if (optval < 100) { 14482 if (optval == 0) /* off */ 14483 tp->t_delayed_ack = 0; 14484 else if (optval == 1) /* on which is 2 */ 14485 tp->t_delayed_ack = 2; 14486 else /* higher than 2 and less than 100 */ 14487 tp->t_delayed_ack = optval; 14488 if (tp->t_flags & TF_DELACK) { 14489 tp->t_flags &= ~TF_DELACK; 14490 tp->t_flags |= TF_ACKNOW; 14491 NET_EPOCH_ENTER(et); 14492 bbr_output(tp); 14493 NET_EPOCH_EXIT(et); 14494 } 14495 } else 14496 error = EINVAL; 14497 break; 14498 case TCP_RACK_PKT_DELAY: 14499 /* RACK added ms i.e. rack-rtt + reord + N */ 14500 BBR_OPTS_INC(tcp_rack_pkt_delay); 14501 bbr->r_ctl.rc_pkt_delay = optval; 14502 break; 14503 14504 case TCP_BBR_RETRAN_WTSO: 14505 BBR_OPTS_INC(tcp_retran_wtso); 14506 if (optval) 14507 bbr->rc_resends_use_tso = 1; 14508 else 14509 bbr->rc_resends_use_tso = 0; 14510 break; 14511 case TCP_DATA_AFTER_CLOSE: 14512 BBR_OPTS_INC(tcp_data_ac); 14513 if (optval) 14514 bbr->rc_allow_data_af_clo = 1; 14515 else 14516 bbr->rc_allow_data_af_clo = 0; 14517 break; 14518 case TCP_BBR_POLICER_DETECT: 14519 BBR_OPTS_INC(tcp_policer_det); 14520 if (bbr->rc_use_google == 0) 14521 error = EINVAL; 14522 else if (optval) 14523 bbr->r_use_policer = 1; 14524 else 14525 bbr->r_use_policer = 0; 14526 break; 14527 14528 case TCP_BBR_TSTMP_RAISES: 14529 BBR_OPTS_INC(tcp_ts_raises); 14530 if (optval) 14531 bbr->ts_can_raise = 1; 14532 else 14533 bbr->ts_can_raise = 0; 14534 break; 14535 case TCP_BBR_TMR_PACE_OH: 14536 BBR_OPTS_INC(tcp_pacing_oh_tmr); 14537 if (bbr->rc_use_google) { 14538 error = EINVAL; 14539 } else { 14540 if (optval) 14541 bbr->r_ctl.rc_incr_tmrs = 1; 14542 else 14543 bbr->r_ctl.rc_incr_tmrs = 0; 14544 } 14545 break; 14546 case TCP_BBR_PACE_OH: 14547 BBR_OPTS_INC(tcp_pacing_oh); 14548 if (bbr->rc_use_google) { 14549 error = EINVAL; 14550 } else { 14551 if (optval > (BBR_INCL_TCP_OH| 14552 BBR_INCL_IP_OH| 14553 BBR_INCL_ENET_OH)) { 14554 error = EINVAL; 14555 break; 14556 } 14557 if (optval & BBR_INCL_TCP_OH) 14558 bbr->r_ctl.rc_inc_tcp_oh = 1; 14559 else 14560 bbr->r_ctl.rc_inc_tcp_oh = 0; 14561 if (optval & BBR_INCL_IP_OH) 14562 bbr->r_ctl.rc_inc_ip_oh = 1; 14563 else 14564 bbr->r_ctl.rc_inc_ip_oh = 0; 14565 if (optval & BBR_INCL_ENET_OH) 14566 bbr->r_ctl.rc_inc_enet_oh = 1; 14567 else 14568 bbr->r_ctl.rc_inc_enet_oh = 0; 14569 } 14570 break; 14571 default: 14572 return (tcp_default_ctloutput(tp, sopt)); 14573 break; 14574 } 14575 tcp_log_socket_option(tp, sopt->sopt_name, optval, error); 14576 INP_WUNLOCK(inp); 14577 return (error); 14578 } 14579 14580 /* 14581 * return 0 on success, error-num on failure 14582 */ 14583 static int 14584 bbr_get_sockopt(struct tcpcb *tp, struct sockopt *sopt) 14585 { 14586 struct inpcb *inp = tptoinpcb(tp); 14587 struct tcp_bbr *bbr; 14588 uint64_t loptval; 14589 int32_t error, optval; 14590 14591 bbr = (struct tcp_bbr *)tp->t_fb_ptr; 14592 if (bbr == NULL) { 14593 INP_WUNLOCK(inp); 14594 return (EINVAL); 14595 } 14596 /* 14597 * Because all our options are either boolean or an int, we can just 14598 * pull everything into optval and then unlock and copy. If we ever 14599 * add a option that is not a int, then this will have quite an 14600 * impact to this routine. 14601 */ 14602 switch (sopt->sopt_name) { 14603 case TCP_BBR_PACE_PER_SEC: 14604 optval = bbr->r_ctl.bbr_hptsi_per_second; 14605 break; 14606 case TCP_BBR_PACE_DEL_TAR: 14607 optval = bbr->r_ctl.bbr_hptsi_segments_delay_tar; 14608 break; 14609 case TCP_BBR_PACE_SEG_MAX: 14610 optval = bbr->r_ctl.bbr_hptsi_segments_max; 14611 break; 14612 case TCP_BBR_MIN_TOPACEOUT: 14613 optval = bbr->no_pacing_until; 14614 break; 14615 case TCP_BBR_PACE_SEG_MIN: 14616 optval = bbr->r_ctl.bbr_hptsi_bytes_min; 14617 break; 14618 case TCP_BBR_PACE_CROSS: 14619 optval = bbr->r_ctl.bbr_cross_over; 14620 break; 14621 case TCP_BBR_ALGORITHM: 14622 optval = bbr->rc_use_google; 14623 break; 14624 case TCP_BBR_TSLIMITS: 14625 optval = bbr->rc_use_ts_limit; 14626 break; 14627 case TCP_BBR_IWINTSO: 14628 optval = bbr->rc_init_win; 14629 break; 14630 case TCP_BBR_STARTUP_PG: 14631 optval = bbr->r_ctl.rc_startup_pg; 14632 break; 14633 case TCP_BBR_DRAIN_PG: 14634 optval = bbr->r_ctl.rc_drain_pg; 14635 break; 14636 case TCP_BBR_PROBE_RTT_INT: 14637 optval = bbr->r_ctl.rc_probertt_int; 14638 break; 14639 case TCP_BBR_PROBE_RTT_LEN: 14640 optval = (bbr->r_ctl.rc_rttprop.cur_time_limit / USECS_IN_SECOND); 14641 break; 14642 case TCP_BBR_PROBE_RTT_GAIN: 14643 optval = bbr->r_ctl.bbr_rttprobe_gain_val; 14644 break; 14645 case TCP_BBR_STARTUP_LOSS_EXIT: 14646 optval = bbr->rc_loss_exit; 14647 break; 14648 case TCP_BBR_USEDEL_RATE: 14649 loptval = get_filter_value(&bbr->r_ctl.rc_delrate); 14650 break; 14651 case TCP_BBR_MIN_RTO: 14652 optval = bbr->r_ctl.rc_min_rto_ms; 14653 break; 14654 case TCP_BBR_MAX_RTO: 14655 optval = bbr->rc_max_rto_sec; 14656 break; 14657 case TCP_RACK_PACE_MAX_SEG: 14658 /* Max segments in a pace */ 14659 optval = bbr->r_ctl.rc_pace_max_segs; 14660 break; 14661 case TCP_RACK_MIN_TO: 14662 /* Minimum time between rack t-o's in ms */ 14663 optval = bbr->r_ctl.rc_min_to; 14664 break; 14665 case TCP_RACK_REORD_THRESH: 14666 /* RACK reorder threshold (shift amount) */ 14667 optval = bbr->r_ctl.rc_reorder_shift; 14668 break; 14669 case TCP_RACK_REORD_FADE: 14670 /* Does reordering fade after ms time */ 14671 optval = bbr->r_ctl.rc_reorder_fade; 14672 break; 14673 case TCP_BBR_USE_RACK_CHEAT: 14674 /* Do we use the rack cheat for rxt */ 14675 optval = bbr->bbr_use_rack_cheat; 14676 break; 14677 case TCP_BBR_FLOOR_MIN_TSO: 14678 optval = bbr->r_ctl.bbr_hptsi_segments_floor; 14679 break; 14680 case TCP_BBR_UTTER_MAX_TSO: 14681 optval = bbr->r_ctl.bbr_utter_max; 14682 break; 14683 case TCP_BBR_SEND_IWND_IN_TSO: 14684 /* Do we send TSO size segments initially */ 14685 optval = bbr->bbr_init_win_cheat; 14686 break; 14687 case TCP_BBR_EXTRA_STATE: 14688 optval = bbr->rc_use_idle_restart; 14689 break; 14690 case TCP_RACK_TLP_THRESH: 14691 /* RACK TLP theshold i.e. srtt+(srtt/N) */ 14692 optval = bbr->rc_tlp_threshold; 14693 break; 14694 case TCP_RACK_PKT_DELAY: 14695 /* RACK added ms i.e. rack-rtt + reord + N */ 14696 optval = bbr->r_ctl.rc_pkt_delay; 14697 break; 14698 case TCP_BBR_RETRAN_WTSO: 14699 optval = bbr->rc_resends_use_tso; 14700 break; 14701 case TCP_DATA_AFTER_CLOSE: 14702 optval = bbr->rc_allow_data_af_clo; 14703 break; 14704 case TCP_DELACK: 14705 optval = tp->t_delayed_ack; 14706 break; 14707 case TCP_BBR_HDWR_PACE: 14708 optval = bbr->bbr_hdw_pace_ena; 14709 break; 14710 case TCP_BBR_POLICER_DETECT: 14711 optval = bbr->r_use_policer; 14712 break; 14713 case TCP_BBR_TSTMP_RAISES: 14714 optval = bbr->ts_can_raise; 14715 break; 14716 case TCP_BBR_TMR_PACE_OH: 14717 optval = bbr->r_ctl.rc_incr_tmrs; 14718 break; 14719 case TCP_BBR_PACE_OH: 14720 optval = 0; 14721 if (bbr->r_ctl.rc_inc_tcp_oh) 14722 optval |= BBR_INCL_TCP_OH; 14723 if (bbr->r_ctl.rc_inc_ip_oh) 14724 optval |= BBR_INCL_IP_OH; 14725 if (bbr->r_ctl.rc_inc_enet_oh) 14726 optval |= BBR_INCL_ENET_OH; 14727 break; 14728 default: 14729 return (tcp_default_ctloutput(tp, sopt)); 14730 break; 14731 } 14732 INP_WUNLOCK(inp); 14733 if (sopt->sopt_name == TCP_BBR_USEDEL_RATE) 14734 error = sooptcopyout(sopt, &loptval, sizeof loptval); 14735 else 14736 error = sooptcopyout(sopt, &optval, sizeof optval); 14737 return (error); 14738 } 14739 14740 /* 14741 * return 0 on success, error-num on failure 14742 */ 14743 static int 14744 bbr_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 14745 { 14746 if (sopt->sopt_dir == SOPT_SET) { 14747 return (bbr_set_sockopt(tp, sopt)); 14748 } else if (sopt->sopt_dir == SOPT_GET) { 14749 return (bbr_get_sockopt(tp, sopt)); 14750 } else { 14751 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 14752 } 14753 } 14754 14755 static const char *bbr_stack_names[] = { 14756 __XSTRING(STACKNAME), 14757 #ifdef STACKALIAS 14758 __XSTRING(STACKALIAS), 14759 #endif 14760 }; 14761 14762 static bool bbr_mod_inited = false; 14763 14764 static int 14765 tcp_addbbr(module_t mod, int32_t type, void *data) 14766 { 14767 int32_t err = 0; 14768 int num_stacks; 14769 14770 switch (type) { 14771 case MOD_LOAD: 14772 printf("Attempting to load " __XSTRING(MODNAME) "\n"); 14773 bbr_zone = uma_zcreate(__XSTRING(MODNAME) "_map", 14774 sizeof(struct bbr_sendmap), 14775 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 14776 bbr_pcb_zone = uma_zcreate(__XSTRING(MODNAME) "_pcb", 14777 sizeof(struct tcp_bbr), 14778 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 14779 sysctl_ctx_init(&bbr_sysctl_ctx); 14780 bbr_sysctl_root = SYSCTL_ADD_NODE(&bbr_sysctl_ctx, 14781 SYSCTL_STATIC_CHILDREN(_net_inet_tcp), 14782 OID_AUTO, 14783 #ifdef STACKALIAS 14784 __XSTRING(STACKALIAS), 14785 #else 14786 __XSTRING(STACKNAME), 14787 #endif 14788 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 14789 ""); 14790 if (bbr_sysctl_root == NULL) { 14791 printf("Failed to add sysctl node\n"); 14792 err = EFAULT; 14793 goto free_uma; 14794 } 14795 bbr_init_sysctls(); 14796 num_stacks = nitems(bbr_stack_names); 14797 err = register_tcp_functions_as_names(&__tcp_bbr, M_WAITOK, 14798 bbr_stack_names, &num_stacks); 14799 if (err) { 14800 printf("Failed to register %s stack name for " 14801 "%s module\n", bbr_stack_names[num_stacks], 14802 __XSTRING(MODNAME)); 14803 sysctl_ctx_free(&bbr_sysctl_ctx); 14804 free_uma: 14805 uma_zdestroy(bbr_zone); 14806 uma_zdestroy(bbr_pcb_zone); 14807 bbr_counter_destroy(); 14808 printf("Failed to register " __XSTRING(MODNAME) 14809 " module err:%d\n", err); 14810 return (err); 14811 } 14812 tcp_lro_reg_mbufq(); 14813 bbr_mod_inited = true; 14814 printf(__XSTRING(MODNAME) " is now available\n"); 14815 break; 14816 case MOD_QUIESCE: 14817 err = deregister_tcp_functions(&__tcp_bbr, true, false); 14818 break; 14819 case MOD_UNLOAD: 14820 err = deregister_tcp_functions(&__tcp_bbr, false, true); 14821 if (err == EBUSY) 14822 break; 14823 if (bbr_mod_inited) { 14824 uma_zdestroy(bbr_zone); 14825 uma_zdestroy(bbr_pcb_zone); 14826 sysctl_ctx_free(&bbr_sysctl_ctx); 14827 bbr_counter_destroy(); 14828 printf(__XSTRING(MODNAME) 14829 " is now no longer available\n"); 14830 bbr_mod_inited = false; 14831 } 14832 tcp_lro_dereg_mbufq(); 14833 err = 0; 14834 break; 14835 default: 14836 return (EOPNOTSUPP); 14837 } 14838 return (err); 14839 } 14840 14841 static moduledata_t tcp_bbr = { 14842 .name = __XSTRING(MODNAME), 14843 .evhand = tcp_addbbr, 14844 .priv = 0 14845 }; 14846 14847 MODULE_VERSION(MODNAME, 1); 14848 DECLARE_MODULE(MODNAME, tcp_bbr, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 14849 MODULE_DEPEND(MODNAME, tcphpts, 1, 1, 1); 14850